home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / tv_tools / mulmnu.cpp < prev   
Encoding:
C/C++ Source or Header  |  1992-04-29  |  3.2 KB  |  131 lines

  1. /*
  2.     MULTMENU.CPP
  3.  
  4.     Jeffrey Willaim Gillette
  5.     18 October 1991
  6.  
  7.     MULTMENU is a TurboVision demo program that sets up two menus --
  8.     menuBar1 and menuBar2.  The user can toggle back and forth between
  9.     them.  This little exercise shows how to change active menus based
  10.     on program mode.  A little further work and you could build menus
  11.     dynamically to implement, e.g., entries on the File menu for the most
  12.     recently opened documents, or a Windows-style "Windows" menu.
  13.  
  14.     Additionally the menuBar1 menu has an item that can be alternatively
  15.     selected and de-selected.  The item will display an 'X' if it is on,
  16.     no 'X' if it is off.  This checkbox effect is accomplished by keeping
  17.     a state flag and switching between two different name strings.
  18.  
  19.     The rest of this program is a bald-faced plagarism from one of the
  20.     "TVGUID##.CPP" files included with TV.
  21. */
  22.  
  23. #define Uses_TApplication
  24. #define Uses_TKeys
  25. #define Uses_TRect
  26. #define Uses_TMenuBar
  27. #define Uses_TSubMenu
  28. #define Uses_TMenuItem
  29. #define Uses_TStatusLine
  30. #define Uses_TStatusItem
  31. #define Uses_TStatusDef
  32. #define Uses_TDeskTop
  33. #define Uses_TEvent
  34. #include <tv.h>
  35.  
  36. const int cmMenu1    = 201;
  37. const int cmMenu2    = 202;
  38. const int cmCheck    = 203;
  39.  
  40. const char *check    = " X ~C~heck Item";
  41. const char *nocheck = "   ~C~heck Item";
  42.  
  43. class TMyApp : public TApplication
  44. {
  45.  
  46. public:
  47.     static TMenuBar    *menuBar1, *menuBar2;    //    initMenuBar is static so
  48.     static TMenuItem *checkMenuItem;        //  these must also be static
  49.     int checkItemFlag;
  50.  
  51.     TMyApp();
  52.     static TMenuBar *initMenuBar( TRect r );
  53.     void handleEvent(TEvent& event);
  54. };
  55.  
  56. TMenuBar *TMyApp::menuBar1, *TMyApp::menuBar2;
  57. TMenuItem *TMyApp::checkMenuItem;
  58.  
  59. TMyApp::TMyApp() :
  60.     TProgInit( &TMyApp::initStatusLine,
  61.                &TMyApp::initMenuBar,
  62.                &TMyApp::initDeskTop
  63.              )
  64. {
  65.     checkItemFlag = 1;
  66. }
  67.  
  68. TMenuBar *TMyApp::initMenuBar( TRect r )
  69. {
  70.  
  71.     r.b.y = r.a.y + 1;    // set bottom line 1 line below top line
  72.  
  73.     checkMenuItem = new TMenuItem(check, cmCheck, kbNoKey, hcNoContext, 0);
  74.  
  75.     menuBar1 = new TMenuBar( r,
  76.         *new TSubMenu( "Menu ~1~", kbAlt1 )+
  77.             *new TMenuItem( "Goto ~2~", cmMenu2, kbNoKey,hcNoContext, 0 )+
  78.             newLine()+
  79.             *checkMenuItem +
  80.             newLine()+
  81.             *new TMenuItem( "E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X" )
  82.         );
  83.  
  84.     menuBar2 = new TMenuBar( r,
  85.         *new TSubMenu( "Menu ~2~", kbAlt2 )+
  86.             *new TMenuItem( "Goto ~1~", cmMenu1, kbNoKey, hcNoContext, 0 )+
  87.             newLine()+
  88.             *new TMenuItem( "E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X" )
  89.         );
  90.  
  91.     return menuBar1;
  92. }
  93.  
  94. void TMyApp::handleEvent(TEvent& event)
  95. {
  96.     TApplication::handleEvent(event); // act like base!
  97.     if( event.what == evCommand )
  98.         {
  99.         switch( event.message.command )
  100.             {
  101.             case cmMenu1:            // make menuBar1 active
  102.                 remove(menuBar);
  103.                 menuBar = menuBar1;
  104.                 insert(menuBar1);
  105.                 break;
  106.             case cmMenu2:            // make menuBar2 active
  107.                 remove(menuBar);
  108.                 menuBar = menuBar2;
  109.                 insert(menuBar2);
  110.                 break;
  111.             case cmCheck:            // toggle checked menu item
  112.                 checkItemFlag = !checkItemFlag;
  113.                 checkMenuItem->name = checkItemFlag ?
  114.                     check : nocheck;
  115.                 break;
  116.             default:
  117.                 return;
  118.             }
  119.         clearEvent( event );       // clear event after handling
  120.         }
  121. }
  122.  
  123.  
  124.  
  125. int main()
  126. {
  127.     TMyApp myApp;
  128.     myApp.run();
  129.     return 0;
  130. }
  131.