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

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