home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / tutorial / tvguid06.cpp < prev    next >
Text File  |  1998-01-19  |  6KB  |  210 lines

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