home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / popup1 / popup1.cpp
Encoding:
C/C++ Source or Header  |  1991-10-06  |  5.9 KB  |  166 lines

  1. //========================================================================
  2. //  The following example routines have been provided by the Technical
  3. //  Support staff at Borland International.  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 - Creating a Popup menu
  9. //
  10. //  - This sample code demonstrates one method of creating a simple
  11. //  pop-up menu using class TMenuBox
  12. //
  13. //  - If the popup menu is inserted into the desktop (or another view),
  14. //  then the handleEvent procedure of that view must be prepared to
  15. //  recieve the commands generated by the menu (and to close the menu)
  16. //
  17. //  - If the popup menu is invoked using execView, then you will have to
  18. //  derive from TMenuBox and override TMenuBox::handleEvent to do some
  19. //  useful chore as a result of the menu selection.
  20. //------------------------------------------------------------------------
  21. #define Uses_MsgBox
  22. #define Uses_TApplication
  23. #define Uses_TButton
  24. #define Uses_TKeys
  25. #define Uses_TDeskTop
  26. #define Uses_TDialog
  27. #define Uses_TMenu
  28. #define Uses_TMenuBar
  29. #define Uses_TMenuBox
  30. #define Uses_TMenuItem
  31. #define Uses_TStaticText
  32. #include <tv.h>
  33. #include <stdlib.h>
  34.  
  35. //========================================================================
  36. //  global data
  37. //------------------------------------------------------------------------
  38. const cmAbout   = 100;  // User selected menu item 'About'
  39. const cmPopup   = 101;  // User selected menu item 'Popup'
  40. const cmOne     = 102;  // User selected popup item one
  41. const cmTwo     = 103;  // User selected popup item two
  42.  
  43. //========================================================================
  44. //  class definitions
  45. //------------------------------------------------------------------------
  46. class TApp : public TApplication {
  47.     //  main application class
  48.  
  49. public:
  50.     TApp();
  51.  
  52.     // virtual functions to be locally redefined
  53.     static TMenuBar *initMenuBar( TRect r );
  54.     void handleEvent( TEvent &event );
  55.  
  56.     // declare new functions
  57.     void AboutDialog();
  58.     void Popup();
  59. };
  60.  
  61. //========================================================================
  62. //  implementation of TApp
  63. //------------------------------------------------------------------------
  64. TApp::TApp() : TProgInit( &TApplication::initStatusLine,
  65.                     &TApp::initMenuBar, &TApplication::initDeskTop )
  66. {
  67.     // default status line and desk top
  68. }
  69.  
  70. //------------------------------------------------------------------------
  71. // define menu bar
  72. //------------------------------------------------------------------------
  73. TMenuBar *TApp::initMenuBar( TRect r )
  74. {
  75.     r.b.y = r.a.y + 1;
  76.     return( new TMenuBar( r, new TMenu(
  77.         *new TMenuItem( "~A~bout", cmAbout, kbAltA, hcNoContext, 0,
  78.         new TMenuItem( "~P~opup", cmPopup, kbAltP, hcNoContext, 0 )
  79.         ))));
  80. }
  81.  
  82. //------------------------------------------------------------------------
  83. // event-handler
  84. //------------------------------------------------------------------------
  85. void TApp::handleEvent( TEvent &event )
  86. {
  87.     TApplication::handleEvent( event );
  88.     if( event.what == evCommand )
  89.     {
  90.         switch( event.message.command )
  91.         {
  92.             case cmAbout:       // display the about box
  93.                 AboutDialog();
  94.                 break;
  95.             case cmPopup:       // display modal popup menu
  96.                 Popup();
  97.                 break;
  98.             case cmOne:         // received from modless popup
  99.                 messageBox( "Item 1 selected", mfOKButton );
  100.                 break;
  101.             case cmTwo:         // received from modless popup
  102.                 messageBox( "Item 2 selected", mfOKButton );
  103.                 break;
  104.         }
  105.         clearEvent( event );
  106.     }
  107. }
  108.  
  109. //------------------------------------------------------------------------
  110. // create modal About dialog box
  111. //------------------------------------------------------------------------
  112. void TApp::AboutDialog()
  113. {
  114.     // the About box reminds the user what is being demonstrated
  115.     TDialog *pd = new TDialog( TRect( 0, 0, 35, 12 ), "About" );
  116.     if( validView( pd ) )
  117.     {
  118.         pd->options |= ofCentered;
  119.         pd->insert ( new TStaticText( TRect( 1, 2, 34, 7 ),
  120.                 "\003Turbo Vision Example\n\003\n"
  121.                 "\003Creating a Popup Menu\n\003\n"
  122.                 "\003Borland Technical Support" ) );
  123.         pd->insert( new TButton( TRect( 3, 9, 32, 11 ), "~O~k",
  124.                                 cmOK, bfDefault ) );
  125.         deskTop->execView( pd );
  126.     }
  127.     destroy( pd );
  128. }
  129.  
  130. //------------------------------------------------------------------------
  131. //  display a modal popup menu
  132. //------------------------------------------------------------------------
  133. void TApp::Popup()
  134. {
  135.     // the rectangle will be sized to fit the item text
  136.     TRect bounds( 0, 0, 0, 0 );
  137.  
  138.     // for clarity, we'll create separately the menu to
  139.     // insert into the menubox
  140.     TMenu *theMenu = new TMenu (
  141.             *new TMenuItem( "Item ~1~", cmOne, kbAltI, hcNoContext, "Alt-1",
  142.             new TMenuItem ( "Item ~2~", cmTwo, kbAltT, hcNoContext, "Alt-2",
  143.             new TMenuItem ( "E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X"
  144.         ) ) ) );
  145.  
  146.     // now create the menu with no parent
  147.     TMenuBox *mb = new TMenuBox( bounds, theMenu, 0 );
  148.  
  149.     // however, if we don't specify appropriate bounds, we must do
  150.     // something to ensure it is appropriately positioned on the desktop
  151.     mb->options |= ofCentered;
  152.  
  153.     // then make sure it's valid and execute it
  154.     if( validView( mb ) )
  155.         deskTop->insert( mb );
  156. }
  157.  
  158. //========================================================================
  159. int main(void)
  160. {
  161.     // now for the tought stuff
  162.     TApp myApp;
  163.     myApp.run();
  164.     return 0;
  165. }
  166.