home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / tutorial / tvguid04.cpp < prev    next >
C/C++ Source or Header  |  1998-01-19  |  4KB  |  146 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID04 Demo Source File                             */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8. /*
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #include <stdlib.h>               // for random()
  13.  
  14. #define Uses_TEvent
  15. #define Uses_TApplication
  16. #define Uses_TKeys
  17. #define Uses_TRect
  18. #define Uses_TMenuBar
  19. #define Uses_TSubMenu
  20. #define Uses_TMenuItem
  21. #define Uses_TStatusLine
  22. #define Uses_TStatusItem
  23. #define Uses_TStatusDef
  24. #define Uses_TDeskTop
  25. #define Uses_TWindow
  26. #include <tvision/tv.h>
  27.  
  28. const int cmMyFileOpen = 200; // assign new command values
  29. const int cmMyNewWin   = 201;
  30.  
  31.  
  32. class TMyApp : public TApplication
  33. {
  34.  
  35. public:
  36.     TMyApp();
  37.     static TStatusLine *initStatusLine( TRect r );
  38.     static TMenuBar *initMenuBar( TRect r );
  39.     virtual void handleEvent( TEvent& event);
  40.     void myNewWindow();
  41. };
  42.  
  43.  
  44. static short winNumber = 0;          // initialize window number
  45.  
  46. class TDemoWindow : public TWindow   // define a new window class
  47. {
  48.  
  49. public:
  50.  
  51.    TDemoWindow( const TRect& r, const char *aTitle, short aNumber );
  52.     // declare a constructor
  53.  
  54.     //   static TFrame *initFrame( TRect r );
  55.     // override needed only if you want a nonstandard frame
  56.     // Here we'll inherit TWindow::initFrame unchanged
  57.     // so TWindowInit will take &TDemoWindow::initFrame to give
  58.     // a standard frame
  59.  
  60. };
  61.  
  62.  
  63. TMyApp::TMyApp() :
  64.     TProgInit( &TMyApp::initStatusLine,
  65.                &TMyApp::initMenuBar,
  66.                &TMyApp::initDeskTop
  67.              )
  68. {
  69. }
  70.  
  71. TStatusLine *TMyApp::initStatusLine(TRect r)
  72. {
  73.     r.a.y = r.b.y - 1;     // move top to 1 line above bottom
  74.     return new TStatusLine( r,
  75.         *new TStatusDef( 0, 0xFFFF ) +
  76.         // set range of help contexts
  77.             *new TStatusItem( 0, kbF10, cmMenu ) +
  78.             // define an item
  79.             *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  80.             // define an item
  81.             *new TStatusItem( "~Alt-F3~ Close", kbAltF3, cmClose )
  82.             // and another one
  83.         );
  84. }
  85.  
  86. TMenuBar *TMyApp::initMenuBar( TRect r )
  87. {
  88.  
  89.     r.b.y = r.a.y + 1;    // set bottom line 1 line below top line
  90.     return new TMenuBar( r,
  91.         *new TSubMenu( "~F~ile", kbAltF )+
  92.             *new TMenuItem( "~O~pen", cmMyFileOpen, kbF3, hcNoContext, "F3" )+
  93.             *new TMenuItem( "~N~ew",  cmMyNewWin,   kbF4, hcNoContext, "F4" )+
  94.             newLine()+
  95.             *new TMenuItem( "E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X" )+
  96.         *new TSubMenu( "~W~indow", kbAltW )+
  97.             *new TMenuItem( "~N~ext", cmNext,     kbF6, hcNoContext, "F6" )+
  98.             *new TMenuItem( "~Z~oom", cmZoom,     kbF5, hcNoContext, "F5" )
  99.         );
  100. }
  101.  
  102. void TMyApp::handleEvent(TEvent& event)
  103. {
  104.     TApplication::handleEvent(event); // act like base!
  105.     if( event.what == evCommand )
  106.         {
  107.         switch( event.message.command )
  108.             {
  109.             case cmMyNewWin:       // but respond to additional commands
  110.                 myNewWindow();     // define action for cmMyNewWin
  111.                 break;
  112.             default:
  113.                 return;
  114.             }
  115.         clearEvent( event );       // clear event after handling
  116.         }
  117. }
  118.  
  119. void TMyApp::myNewWindow()
  120. {
  121.     TRect r( 0, 0, 26, 7 );           // set initial size and position
  122.  
  123.     /* SS: micro change here */
  124.  
  125.     //r.move( random(53), random(16) ); // randomly move around screen
  126.     r.move( random() % 53, random() % 16 ); // randomly move around screen
  127.     TDemoWindow *window = new TDemoWindow ( r, "Demo Window", ++winNumber);
  128.     deskTop->insert(window); // put window into desktop and draw it
  129. }
  130.  
  131.  
  132. TDemoWindow::TDemoWindow( const TRect& r, const char *aTitle, short aNumber):
  133.                           TWindow( r, aTitle, aNumber),
  134.                           TWindowInit( &TDemoWindow::initFrame
  135.                         )
  136.  
  137. {
  138. }
  139.  
  140. int main()
  141. {
  142.     TMyApp myApp;
  143.     myApp.run();
  144.     return 0;
  145. }
  146.