home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / nesmnu / menunest.cpp
Encoding:
C/C++ Source or Header  |  1991-09-26  |  5.4 KB  |  160 lines

  1. //========================================================================
  2. //  The following routines have been uploaded to the Borland Forum BPROGB
  3. //  on CompuServe by the Technical Support staff.  They are provided as a
  4. //  courtesy and not as part of a Borland product; and, as such, are
  5. //  provided without the assurance of technical support or any specific
  6. //  guarantees.
  7. //========================================================================
  8. //  Turbo Vision - nested menus
  9. //
  10. //  - This sample code demonstrates creating nested menus using the
  11. //  TSubMenu class.
  12. //========================================================================
  13. #define Uses_MsgBox
  14. #define Uses_TApplication
  15. #define Uses_TButton
  16. #define Uses_TDeskTop
  17. #define Uses_TDialog
  18. #define Uses_TKeys
  19. #define Uses_TMenu
  20. #define Uses_TMenuBar
  21. #define Uses_TMenuItem
  22. #define Uses_TStaticText
  23. #define Uses_TStatusDef
  24. #define Uses_TStatusItem
  25. #define Uses_TStatusLine
  26. #define Uses_TSubMenu
  27. #include <tv.h>
  28.  
  29. //========================================================================
  30. //  global data
  31. //------------------------------------------------------------------------
  32. const cmAbout       = 100;  // user request: about box
  33. const cmItem1       = 101;  // user request: Item 1
  34. const cmItem2       = 102;  // user request: Item 2
  35.  
  36. //========================================================================
  37. //  class definitions
  38. //------------------------------------------------------------------------
  39. class TV6 : public TApplication {
  40.     //  main application class
  41.  
  42. public:
  43.     TV6();
  44.  
  45.     // virtual functions to be locally redefined
  46.     static TStatusLine *initStatusLine(TRect r);
  47.     static TMenuBar *initMenuBar(TRect r);
  48.     void handleEvent (TEvent &event);
  49.  
  50.     // declare new functions
  51.     void AboutBox();
  52.  
  53. };
  54.  
  55. //========================================================================
  56. //  implementation of class TV6
  57. //------------------------------------------------------------------------
  58. TV6::TV6() : TProgInit( &TV6::initStatusLine,
  59.                     &TV6::initMenuBar, &TApplication::initDeskTop )
  60. {
  61. }
  62.  
  63. //------------------------------------------------------------------------
  64. // define status line
  65. //------------------------------------------------------------------------
  66. TStatusLine *TV6::initStatusLine(TRect r)
  67. {
  68.     r.a.y = r.b.y - 1;
  69.     return new TStatusLine (r,
  70.         *new TStatusDef( 0, 0xffff ) +
  71.         *new TStatusItem ("~Alt-X~ Exit", kbAltX, cmQuit) +
  72.         *new TStatusItem ("~Alt-F3~ Close", kbAltF3, cmClose) +
  73.         *new TStatusItem ("~F10~ Menu", kbF10, cmMenu)
  74.         );
  75. }
  76.  
  77. //------------------------------------------------------------------------
  78. //  define menu bar
  79. //------------------------------------------------------------------------
  80. TMenuBar *TV6::initMenuBar(TRect r)
  81. {
  82.     r.b.y = r.a.y + 1;
  83.     TMenuBar *newMenu = new TMenuBar(r,
  84.         *new TSubMenu( "~\360~", kbAltSpace ) +
  85.         *new TMenuItem( "~A~bout", cmAbout, kbAltA, hcNoContext ) +
  86.         newLine() +
  87.         *new TMenuItem( "Exit", 0, new TMenu(
  88.             *new TMenuItem( "Exit & ~S~ave", cmQuit, kbAltX, hcNoContext, 0,
  89.             new TMenuItem( "Exit & ~A~bandon", cmQuit, kbAltY, hcNoContext, 0,
  90.             new TMenuItem( "Just ~Q~uit", cmQuit, kbAltZ, hcNoContext, 0
  91.         )))), hcNoContext) +
  92.         *new TSubMenu( "~D~o It", kbAltF) +
  93.         *new TMenuItem( "~I~tem 1", cmItem1, kbNoKey, hcNoContext, 0 )+
  94.         *new TMenuItem( "I~t~em 2", cmItem2, kbNoKey, hcNoContext, 0 )
  95.         );
  96.     newMenu->options |= 0xff;
  97.     return newMenu;
  98. }
  99.  
  100. //------------------------------------------------------------------------
  101. // event-handler
  102. //------------------------------------------------------------------------
  103. void TV6::handleEvent (TEvent &event)
  104. {
  105.     TApplication::handleEvent (event);
  106.     if (event.what == evCommand)
  107.     {
  108.         switch (event.message.command)
  109.         {
  110.             case cmAbout:       // 'about' box
  111.                 AboutBox();
  112.                 break;
  113.             case cmItem1:       // selected menu item 1
  114.                 messageBox( "\003Item 1 selected", mfInformation );
  115.                 break;
  116.             case cmItem2:       // selected menu item 2
  117.                 messageBox( "\003Item 2 selected", mfInformation );
  118.                 break;
  119.         }
  120.         clearEvent (event);
  121.     }
  122. }
  123.  
  124. //------------------------------------------------------------------------
  125. // create modal About dialog box
  126. //------------------------------------------------------------------------
  127. void TV6::AboutBox()
  128. {
  129.     // instantiate the basic dialog box
  130.     TDialog *pd = new TDialog( TRect(0, 0, 30, 12), "About");
  131.     if (pd)
  132.     {
  133.         //text strings centered within TRect extents
  134.         pd->insert (new TStaticText(TRect(1, 2, 29, 7),
  135.                 "\003Turbo Vision\n"
  136.                 "\003\n"
  137.                 "\003Example Program\n"
  138.                 "\003\n"
  139.                 "\003Nested Menus"));
  140.         pd->insert (new TButton( TRect(3,9,27,11), "~O~key-d~O~key",
  141.                 cmOK, bfDefault));
  142.  
  143.         //center box on desktop
  144.         pd->options |= ofCentered;
  145.  
  146.         // execute the modal dialog box
  147.         deskTop->execView (pd);
  148.     }
  149.     destroy (pd);
  150. }
  151.  
  152. //========================================================================
  153. int main(void)
  154. {
  155.     TV6 tv6App;
  156.     tv6App.run();
  157.     return 0;
  158. }
  159.  
  160.