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

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID07 Demo Source File                             */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. // same as tvguid06 except for improved draw method
  10.  
  11. #include <stdlib.h>             // for exit(), random()
  12. #include <iostream.h>
  13. #include <fstream.h>            // for ifstream
  14. #include <stdio.h>              // for puts() etc
  15. #include <string.h>             // for strlen etc
  16. #include <ctype.h>
  17.  
  18. #define Uses_TEventQueue
  19. #define Uses_TEvent
  20. #define Uses_TProgram
  21. #define Uses_TApplication
  22. #define Uses_TKeys
  23. #define Uses_TRect
  24. #define Uses_TMenuBar
  25. #define Uses_TSubMenu
  26. #define Uses_TMenuItem
  27. #define Uses_TStatusLine
  28. #define Uses_TStatusItem
  29. #define Uses_TStatusDef
  30. #define Uses_TDeskTop
  31. #define Uses_TView
  32. #define Uses_TWindow
  33. #include <tv.h>
  34.  
  35. const int cmMyFileOpen = 200; // assign new command values
  36. const int cmMyNewWin   = 201;
  37.  
  38. const char *fileToRead = "tvguid07.cpp";
  39. const int maxLineLength = maxViewWidth+1;
  40. const int maxLines      = 100;
  41. char *lines[maxLines];
  42. int lineCount = 0;
  43.  
  44. void readFile( const char *fileName )
  45. {
  46.     ifstream fileToView( fileName );
  47.     if( !fileToView )
  48.         {
  49.         cout << "Invalid file name..." << endl;
  50.         exit( 1 );
  51.         }
  52.     else
  53.         {
  54.         char buf[maxLineLength];
  55.         while( lineCount < maxLines &&
  56.                fileToView.getline( buf, maxLineLength ) != 0 )
  57.             {
  58.             lines[lineCount] = newStr( buf );
  59.             lineCount++;
  60.             }
  61.         }
  62. }
  63.  
  64. void deleteFile()
  65. {
  66.     for( int i = 0; i < lineCount; i++ )
  67.         delete lines[i];
  68. }
  69.  
  70. class TMyApp : public TApplication
  71. {
  72.  
  73. public:
  74.     TMyApp();
  75.     static TStatusLine *initStatusLine( TRect r );
  76.     static TMenuBar *initMenuBar( TRect r );
  77.     virtual void handleEvent( TEvent& event);
  78.     void myNewWindow();
  79. };
  80.  
  81.  
  82. static short winNumber = 0;             // initialize window number
  83.  
  84. class TDemoWindow : public TWindow      // define a new window class
  85. {
  86.  
  87. public:
  88.  
  89.    TDemoWindow( const TRect& r, const char *aTitle, short aNumber );
  90.  
  91. };
  92.  
  93. class TInterior : public TView
  94. {
  95.  
  96. public:
  97.  
  98.     TInterior( const TRect& bounds );   // constructor
  99.     virtual void draw();                // override TView::draw
  100. };
  101.  
  102. TInterior::TInterior( const TRect& bounds ) : TView( bounds )
  103. {
  104.     growMode = gfGrowHiX | gfGrowHiY;   //make size follow that of the window
  105.     options = options | ofFramed;
  106. }
  107.  
  108. void TInterior::draw()
  109. {
  110.     ushort color = getColor(0x0301);
  111.     for( int i = 0; i < size.y; i++ )
  112.         {
  113.         TDrawBuffer b;
  114.         b.moveChar( 0, ' ', color, size.x );
  115.         // fill line buffer with spaces
  116.         if( lines[i] )
  117.             {
  118.             char s[maxLineLength];
  119.             strncpy( s, lines[i], size.x );
  120.             s[size.x] = EOS;
  121.             b.moveStr( 0, s, color );
  122.             }
  123.         writeLine( 0, i, size.x, 1, b);
  124.         }
  125. }
  126.  
  127. TMyApp::TMyApp() :
  128.     TProgInit( &TMyApp::initStatusLine,
  129.                &TMyApp::initMenuBar,
  130.                &TMyApp::initDeskTop
  131.              )
  132. {
  133. }
  134.  
  135. TStatusLine *TMyApp::initStatusLine(TRect r)
  136. {
  137.     r.a.y = r.b.y - 1;     // move top to 1 line above bottom
  138.     return new TStatusLine( r,
  139.         *new TStatusDef( 0, 0xFFFF ) +
  140.         // set range of help contexts
  141.             *new TStatusItem( 0, kbF10, cmMenu ) +
  142.             // define an item
  143.             *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  144.             // and another one
  145.             *new TStatusItem( "~Alt-F3~ Close", kbAltF3, cmClose )
  146.             // and another one
  147.         );
  148. }
  149.  
  150. TMenuBar *TMyApp::initMenuBar( TRect r )
  151. {
  152.     r.b.y = r.a.y + 1;    // set bottom line 1 line below top line
  153.     return new TMenuBar( r,
  154.         *new TSubMenu( "~F~ile", kbAltF )+
  155.             *new TMenuItem( "~O~pen", cmMyFileOpen, kbF3, hcNoContext, "F3" )+
  156.             *new TMenuItem( "~N~ew",  cmMyNewWin,   kbF4, hcNoContext, "F4" )+
  157.             newLine()+
  158.             *new TMenuItem( "E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X" )+
  159.         *new TSubMenu( "~W~indow", kbAltW )+
  160.             *new TMenuItem( "~N~ext", cmNext,     kbF6, hcNoContext, "F6" )+
  161.             *new TMenuItem( "~Z~oom", cmZoom,     kbF5, hcNoContext, "F5" )
  162.         );
  163. }
  164.  
  165. void TMyApp::handleEvent(TEvent& event)
  166. {
  167.     TApplication::handleEvent(event);   // act like base!
  168.     if( event.what == evCommand )
  169.         {
  170.         switch( event.message.command )
  171.             {
  172.             case cmMyNewWin:            // but respond to additional commands
  173.                 myNewWindow();          // define action for cmMyNewWin
  174.                                         //   command
  175.                 break;
  176.             default:
  177.                 return;
  178.             }
  179.         clearEvent( event );            // clear event after handling
  180.         }
  181. }
  182.  
  183. void TMyApp::myNewWindow()
  184. {
  185.     TRect r( 0, 0, 26, 7 );             // set initial size and position
  186.     r.move( random(53), random(16) );   // randomly move around screen
  187.     TDemoWindow *window = new TDemoWindow ( r, "Demo Window", ++winNumber);
  188.     deskTop->insert(window);    // put window into desktop and draw it
  189. }
  190.  
  191.  
  192. TDemoWindow::TDemoWindow( const TRect& bounds, const char *aTitle,
  193.               short aNumber) :
  194.          TWindow( bounds, aTitle, aNumber),
  195.          TWindowInit( &TDemoWindow::initFrame )
  196. {
  197.     TRect r = getClipRect();    // get exposed area
  198.     r.grow(-1, -1);             // make interior fit inside window frame
  199.     insert( new TInterior(r) ); // add interior to window
  200. }
  201.  
  202.  
  203. int main()
  204. {
  205.     readFile( fileToRead );
  206.     TMyApp myApp;
  207.     myApp.run();
  208.     deleteFile();
  209.     return 0;
  210. }
  211.