home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / nmenu / nmenu.cpp
Encoding:
C/C++ Source or Header  |  1991-09-25  |  4.5 KB  |  129 lines

  1. // nmenu.cpp: example of TV menu nesting.
  2. // Robert J. Connolly, 9/91.
  3. // CIS: 72240,2664
  4. // BPROGB, DOSFRAMEWORKS
  5.  
  6. #define Uses_TApplication
  7. #define Uses_TMenu
  8. #define Uses_TMenuBar
  9. #define Uses_TMenuItem
  10. #define Uses_TStatusLine
  11. #define Uses_TStatusItem
  12. #define Uses_TStatusDef
  13. #define Uses_TKeys
  14. #include <tv.h>
  15.  
  16. //-------------------------------------------------------------------------
  17. class TMyApp : public TApplication
  18. {
  19.   public:
  20.     TMyApp();  // constructor to set the application up.
  21.     static TMenuBar *initMenuBar( TRect r);  // init menus.
  22. };
  23. //-------------------------------------------------------------------------
  24.  
  25.  
  26. //-------------------------------------------------------------------------
  27. TMyApp::TMyApp() : TProgInit( &TMyApp::initStatusLine,
  28.                           &TMyApp::initMenuBar,
  29.                       &TMyApp::initDeskTop )
  30. {
  31. }
  32. //-------------------------------------------------------------------------
  33.  
  34. //-------------------------------------------------------------------------
  35. // initMenuBar:  example nested menus.
  36. // -- After playing around with the TV menu system, I came up with the
  37. //    following technique for coding menus.
  38. //
  39. // 1) Just forget TSubMenu and newLine().
  40. // 2) TMenuBar takes a pointer to a TMenu.
  41. // 3) TMenu takes a REFERENCE to a TMenuItem: *new *TMenuItem(...).
  42. // 3) TMenuItem has two constructors, each of which requires a pointer
  43. //    to another TMenuItem (used for sequential entries in a given menu),
  44. //    or 0 for the last entry, and the other form which has an additional
  45. //    parameter for a pointer to a TMenu (another nested menu).
  46. //
  47. //    To make it easier to visualize:
  48. //
  49. //  TMenuBar
  50. //    |
  51. //    |--> TMenu-->*TMenuItem-->TMenuItem-->TMenuItem-->0
  52. //                    |
  53. //                    |--> TMenu-->*TMenuItem-->TMenuItem-->TMenuItem-->0
  54. //                                    |
  55. //                                    |-->TMenu-->*TMenuItem-->0
  56. //
  57. //    Where: *TMxxx is a reference, and --> is a pointer argument to an
  58. //    instance of type TMxxx.
  59. //
  60. //    So, TMenu always starts a menu.  TMenuItem provides the entries in
  61. //    a menu, with a constructor form that allows the entry to be a branch
  62. //    to a nested menu.
  63. //
  64. //    Now, the only difficulty is remembering the order of the parameters
  65. //    for each TMenuItem form and keeping the parantheses correct.
  66. //
  67. //    Note:  newLine just provides a reference to a TMenuItem of the
  68. //    construction *new MenuItem(0,0,0,hcNoContext,0,0).  Therefore,
  69. //    a TMenuItem pointer with these arguments provides the line
  70. //    seperator. see below.
  71. //
  72. //  In the demo, the file menu nests to 3 levels, the edit menu does not
  73. //  nest at all, and the rest nest to just 1 level.
  74. //-------------------------------------------------------------------------
  75.  
  76. TMenuBar* TMyApp::initMenuBar( TRect r)
  77. {
  78.   r.b.y = r.a.y + 1;  // set bottom 1 line below top.
  79.   return new TMenuBar
  80.   (r,new TMenu(
  81.  
  82.     *new TMenuItem("~F~ile",kbAltF,new TMenu(
  83.  
  84.        *new TMenuItem("~P~ack",cmError,kbNoKey,hcNoContext,"",
  85.         new TMenuItem("~R~eindex",cmError,kbNoKey,hcNoContext,"",
  86.         new TMenuItem(0,0,0,hcNoContext,0,  // newLine().
  87.         new TMenuItem("~T~est",kbNoKey,new TMenu(
  88.  
  89.           *new TMenuItem("Test ~1~",cmError,kbNoKey,hcNoContext,"",
  90.            new TMenuItem("Test ~2~",kbNoKey,new TMenu(
  91.  
  92.              *new TMenuItem("Test ~1~a",cmError,kbNoKey,hcNoContext,"",
  93.               new TMenuItem("Test ~2~b",cmError,kbNoKey,hcNoContext,"",0))
  94.  
  95.            ), hcNoContext,0))
  96.  
  97.         ), hcNoContext,
  98.  
  99.        new TMenuItem(0,0,0,hcNoContext,0,   // newLine().
  100.        new TMenuItem("~E~xit",cmQuit,kbAltX,hcNoContext,"Alt-X",0))))))
  101.  
  102.     ), hcNoContext,
  103.  
  104.     new TMenuItem("~E~dit",cmError,kbNoKey,hcNoContext,"",
  105.     new TMenuItem("~R~eport",kbNoKey,new TMenu(
  106.  
  107.       *new TMenuItem("~S~elect",cmError,kbNoKey,hcNoContext,"",
  108.        new TMenuItem("~P~rint",cmError,kbNoKey,hcNoContext,"",0))
  109.  
  110.     ), hcNoContext,
  111.  
  112.     new TMenuItem("~W~indow",kbNoKey,new TMenu(
  113.  
  114.       *new TMenuItem("~N~ext",cmNext,kbF6,hcNoContext,"F6",
  115.        new TMenuItem("~Z~oom",cmZoom,kbF5,hcNoContext,"F5",0))
  116.  
  117.     ), hcNoContext,0))))
  118.   ));
  119. }
  120. //-------------------------------------------------------------------------
  121.  
  122. //-------------------------------------------------------------------------
  123. int main()
  124. {
  125.     TMyApp myApp;
  126.     myApp.run();
  127.     return 0;
  128. }
  129. //-------------------------------------------------------------------------