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

  1. /*****************************************************************************
  2.  * This sample program is provided by the Borland technical support staff as
  3.  * an example of constructing Turbo Vision menus with directly selectable
  4.  * items on the menu bar.
  5.  *****************************************************************************
  6.  *
  7.  * Demonstration of a main menu bar containing members that send commands
  8.  *   instead of further drop down menus.
  9.  *
  10.  *
  11.  * The way the menus are constructed in reality looks like this:
  12.  *
  13.  *    TMenuBar -> TMenu -> TMenuItem  { -> TMenu -> TMenuItem ... }
  14.  *                             |
  15.  *                         TMenuItem  { -> TMenu -> TMenuItem ... }
  16.  *                             |
  17.  *                         TMenuItem  { -> TMenu -> TMenuItem ... }
  18.  *                             |
  19.  *                            ...
  20.  *
  21.  * The vertical column of TMenuItems (above) is the main menu bar.  If
  22.  * there is a drop down menu bar for a given label, then the information
  23.  * in the curly bracket's applies, and further submenus may be instantiated
  24.  * in more nested TMenu objects.
  25.  *
  26.  * For a coded example of the above structure, see the initMenuBar()
  27.  * function in this program.
  28.  *
  29.  *****************************************************************************/
  30.  
  31. #define Uses_TKeys
  32. #define Uses_TRect
  33. #define Uses_TEvent
  34. #define Uses_TMenu
  35. #define Uses_TMenuBar
  36. #define Uses_TMenuItem
  37. #define Uses_TApplication
  38. #define Uses_MsgBox
  39. #include <tv.h>                 // TV header file
  40.  
  41. #include <string.h>
  42.  
  43. const int cmOne = 110;          // Commands sent by various menu items
  44. const int cmTwoA = 120;
  45. const int cmTwoB = 121;
  46. const int cmThree = 130;
  47. const int cmFourA = 140;
  48. const int cmFourB = 141;
  49.  
  50. class testApp : public TApplication         // Application class
  51. {
  52.  
  53. public:
  54.  
  55.     testApp() : TProgInit( &testApp::initStatusLine,
  56.                            &testApp::initMenuBar,
  57.                            &testApp::initDeskTop ) { }
  58.     static TMenuBar *initMenuBar( TRect r );
  59.     void handleEvent( TEvent& event );
  60.  
  61. };
  62.  
  63.  
  64. TMenuBar *testApp::initMenuBar( TRect r )
  65. {
  66.     r.b.y =  r.a.y + 1;
  67.  
  68. //
  69. // The individual menus are created as a sequence TMenuItems, each taking
  70. // a pointer to the previous as the last parameter in the constructor.
  71. // The drop down portions are created with nested calls to the new operator
  72. // for a TMenu, then for the sub menu items.
  73. //
  74.     TMenuItem *four =
  75.         new TMenuItem( "~F~our", 0,
  76.             new TMenu(
  77.                 *new TMenuItem( "Four A", cmFourA, kbNoKey, hcNoContext, 0,
  78.                  new TMenuItem( "Four B", cmFourB, kbNoKey )
  79.             )));
  80.  
  81.     TMenuItem *three =
  82.         new TMenuItem( "~T~hree", cmThree, kbNoKey, hcNoContext, 0, four );
  83.  
  84.     TMenuItem *two =
  85.         new TMenuItem( "~T~wo", 0,
  86.             new TMenu(
  87.                 *new TMenuItem( "Two A", cmTwoA, kbNoKey, hcNoContext, 0,
  88.                  new TMenuItem( "Two B", cmTwoB, kbNoKey )
  89.             )),
  90.             hcNoContext, three);
  91.  
  92.     TMenuItem *one =
  93.         new TMenuItem( "~O~ne", cmOne, kbNoKey, hcNoContext, 0, two );
  94.  
  95. //
  96. // Finally, we collect the list into one final TMenu object and pass that
  97. // to TMenuBar, which we return to the application.
  98. //
  99.     return new TMenuBar( r, new TMenu( *one ) );
  100. }
  101.  
  102.  
  103. void testApp::handleEvent( TEvent& event )
  104. {
  105.     static char buffer[80] = "Received message ";
  106.  
  107.     TApplication::handleEvent(event);
  108.  
  109.     if( event.what == evCommand )
  110.     {
  111.         switch( event.message.command )
  112.         {
  113.         case cmOne:
  114.             strcpy(&buffer[17], "cmOne.   ");
  115.             break;
  116.         case cmTwoA:
  117.             strcpy(&buffer[17], "cmTwo A. ");
  118.             break;
  119.         case cmTwoB:
  120.             strcpy(&buffer[17], "cmTwo B. ");
  121.             break;
  122.         case cmThree:
  123.             strcpy(&buffer[17], "cmThree. ");
  124.             break;
  125.         case cmFourA:
  126.             strcpy(&buffer[17], "cmFour A.");
  127.             break;
  128.         case cmFourB:
  129.             strcpy(&buffer[17], "cmFour B.");
  130.             break;
  131.         default:
  132.             return;
  133.         }
  134.         messageBox( buffer, mfOKButton | mfInformation );
  135.         clearEvent( event );
  136.     }
  137. }
  138.  
  139.  
  140. void main(void)
  141. {
  142.     testApp lessonTwo;
  143.  
  144.     lessonTwo.run();
  145. }
  146.