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

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID06 Demo Source File                             */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. /****** imperfect draw method--see tvguid07 for improvement *****/
  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 = "tvguid06.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.  
  75.     TMyApp();
  76.     static TStatusLine *initStatusLine( TRect r );
  77.     static TMenuBar *initMenuBar( TRect r );
  78.     virtual void handleEvent( TEvent& event);
  79.     void myNewWindow();
  80.  
  81. };
  82.  
  83.  
  84. static short winNumber = 0;          // initialize window number
  85.  
  86. class TDemoWindow : public TWindow   // define a new window class
  87. {
  88.  
  89. public:
  90.  
  91.     TDemoWindow( const TRect& r, const char *aTitle, short aNumber );
  92.     static const char * const name;
  93.  
  94. };
  95.  
  96. class TInterior : public TView
  97. {
  98.  
  99. public:
  100.  
  101.     TInterior( const TRect& bounds ); // constructor
  102.     virtual void draw();              // override TView::draw
  103. };
  104.  
  105. TInterior::TInterior( const TRect& bounds ) : TView( bounds )
  106. {
  107.     growMode = gfGrowHiX | gfGrowHiY;   //make size follow that of the window
  108.     options = options | ofFramed;
  109. }
  110.  
  111. void TInterior::draw()
  112. {
  113.     for( int i = 0; i < size.y; i++ )
  114.         writeStr( 0, i, lines[i], 1 );
  115. }
  116.  
  117. TMyApp::TMyApp() :
  118.     TProgInit( &TMyApp::initStatusLine,
  119.                &TMyApp::initMenuBar,
  120.                &TMyApp::initDeskTop
  121.              )
  122. {
  123. }
  124.  
  125. TStatusLine *TMyApp::initStatusLine(TRect r)
  126. {
  127.     r.a.y = r.b.y - 1;     // move top to 1 line above bottom
  128.     return new TStatusLine( r,
  129.         *new TStatusDef( 0, 0xFFFF ) +
  130.         // set range of help contexts
  131.             *new TStatusItem( 0, kbF10, cmMenu ) +
  132.             // define an item
  133.             *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  134.             // and another one
  135.             *new TStatusItem( "~Alt-F3~ Close", kbAltF3, cmClose )
  136.             // and another one
  137.         );
  138. }
  139.  
  140. TMenuBar *TMyApp::initMenuBar( TRect r )
  141. {
  142.     r.b.y = r.a.y + 1;    // set bottom line 1 line below top line
  143.     return new TMenuBar( r,
  144.         *new TSubMenu( "~F~ile", kbAltF )+
  145.             *new TMenuItem( "~O~pen", cmMyFileOpen, kbF3, hcNoContext, "F3" )+
  146.             *new TMenuItem( "~N~ew",  cmMyNewWin,   kbF4, hcNoContext, "F4" )+
  147.             newLine()+
  148.             *new TMenuItem( "E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X" )+
  149.         *new TSubMenu( "~W~indow", kbAltW )+
  150.             *new TMenuItem( "~N~ext", cmNext,     kbF6, hcNoContext, "F6" )+
  151.             *new TMenuItem( "~Z~oom", cmZoom,     kbF5, hcNoContext, "F5" )
  152.         );
  153. }
  154.  
  155. void TMyApp::handleEvent(TEvent& event)
  156. {
  157.     TApplication::handleEvent(event);   // act like base!
  158.     if( event.what == evCommand )
  159.         {
  160.         switch( event.message.command )
  161.             {
  162.             case cmMyNewWin:            // but respond to additional commands
  163.                 myNewWindow();          // define action for cmMyNewWin                                // command
  164.                 break;
  165.             default:
  166.                 return;
  167.             }
  168.         clearEvent( event );            // clear event after handling
  169.         }
  170. }
  171.  
  172. void TMyApp::myNewWindow()
  173. {
  174.     TRect r( 0, 0, 26, 7 );           // set initial size and position
  175.     r.move( random(53), random(16) ); // randomly move around screen
  176.     TDemoWindow *window = new TDemoWindow ( r, "Demo Window", ++winNumber);
  177.     deskTop->insert(window);    // put window into desktop and draw it
  178. }
  179.  
  180. // const char * const TDemoWindow::name = "TDemoWindow";
  181.  
  182. TDemoWindow::TDemoWindow( const TRect& bounds, const char *aTitle,
  183.               short aNumber) :
  184.          TWindow( bounds, aTitle, aNumber),
  185.          TWindowInit( &TDemoWindow::initFrame )
  186. {
  187.     TRect r = getClipRect();    // get exposed area
  188.     r.grow(-1, -1);             // make interior fit inside window frame
  189.     insert( new TInterior(r) ); // add interior to window
  190. }
  191.  
  192. int main()
  193. {
  194.     readFile( fileToRead );
  195.     TMyApp myApp;
  196.     myApp.run();
  197.     deleteFile();
  198.     return 0;
  199. }
  200.