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

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