home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 17.ddi / TVDOCDEM.ZIP / TVGUID05.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.6 KB  |  162 lines

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