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

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID10 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 tvguid09 except for better handling of resizing
  13. // add TDemoWindow::sizeLimits
  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_TFrame
  38. #define Uses_TScroller
  39. #define Uses_TScrollBar
  40. #include <tvision/tv.h>
  41.  
  42. const int cmMyFileOpen = 200;   // assign new command values
  43. const int cmMyNewWin   = 201;
  44.  
  45. /* SS: micro change here */
  46.  
  47. const char *fileToRead = "tvguid10.cc";
  48. //const char *fileToRead = "tvguid10.cpp";
  49. const int maxLineLength = maxViewWidth+1;
  50. const int maxLines      = 100;
  51. char *lines[maxLines];
  52. int lineCount = 0;
  53. static short winNumber  = 0;    // initialize window number
  54.  
  55. class TMyApp : public TApplication
  56. {
  57.  
  58. public:
  59.     TMyApp();
  60.     static TStatusLine *initStatusLine( TRect r );
  61.     static TMenuBar *initMenuBar( TRect r );
  62.     virtual void handleEvent( TEvent& event);
  63.     void newWindow();
  64. };
  65.  
  66. class TInterior : public TScroller
  67. {
  68.  
  69. public:
  70.  
  71.     TInterior( const TRect& bounds, TScrollBar *aHScrollBar,
  72.            TScrollBar *aVScrollBar );   // constructor
  73.     virtual void draw();                // override TView::draw
  74. };
  75.  
  76. class TDemoWindow : public TWindow      // define a new window class
  77. {
  78.  
  79. public:
  80.  
  81.     TDemoWindow( const TRect& bounds, const char *aTitle, short aNumber );
  82.     TInterior *makeInterior( const TRect& r, Boolean left );
  83.     virtual void sizeLimits( TPoint& minP, TPoint& maxP );
  84.     // override TWindow::sizeLimits
  85.  
  86. private:
  87.  
  88.     TInterior *lInterior, *rInterior;
  89.  
  90. };
  91.  
  92. void readFile( const char *fileName )
  93. {
  94.     ifstream fileToView( fileName );
  95.     if( !fileToView )
  96.         {
  97.         cout << "Invalid file name..." << endl;
  98.         exit( 1 );
  99.         }
  100.     else
  101.         {
  102.         char buf[maxLineLength];
  103.         while( lineCount < maxLines &&
  104.                fileToView.getline( buf, maxLineLength ) != 0 )
  105.             {
  106.             lines[lineCount] = newStr( buf );
  107.             lineCount++;
  108.             }
  109.         }
  110. }
  111.  
  112. void deleteFile()
  113. {
  114.     for( int i = 0; i < lineCount; i++ )
  115.         delete lines[i];
  116. }
  117.  
  118. TInterior::TInterior( const TRect& bounds, TScrollBar *aHScrollBar,
  119.               TScrollBar *aVScrollBar ) :
  120.        TScroller( bounds, aHScrollBar, aVScrollBar )
  121. {
  122.     options = options | ofFramed;
  123.     setLimit( maxLineLength, lineCount );
  124. }
  125.  
  126. void TInterior::draw()       // modified for scroller
  127. {
  128.     ushort color = getColor(0x0301);
  129.     for( int i = 0; i < size.y; i++ )
  130.         // for each line:
  131.         {
  132.         TDrawBuffer b;
  133.         b.moveChar( 0, ' ', color, size.x );
  134.         // fill line buffer with spaces
  135.         int j = delta.y + i;       // delta is scroller offset
  136.         if( j < lineCount && lines[j] != 0 )
  137.             {
  138.             char s[maxLineLength];
  139.             if( delta.x > strlen(lines[j] ) )
  140.                 s[0] = EOS;
  141.             else
  142.                 {
  143.                 strncpy( s, lines[j]+delta.x, size.x );
  144.                 s[size.x] = EOS;
  145.                 }
  146.             b.moveCStr( 0, s, color );
  147.             }
  148.         writeLine( 0, i, size.x, 1, b);
  149.         }
  150.  
  151. }
  152.  
  153. TDemoWindow::TDemoWindow( const TRect& bounds, const char *aTitle,
  154.               short aNumber) :
  155.          TWindow( bounds, aTitle, aNumber),
  156.          TWindowInit( &TDemoWindow::initFrame )
  157. {
  158.     TRect lbounds = getExtent();
  159.     TRect r( lbounds.a.x, lbounds.a.y, lbounds.b.x/2+1, lbounds.b.y );
  160.     lInterior = makeInterior( r, True );
  161.     lInterior->growMode = gfGrowHiY;
  162.     insert( lInterior );
  163.     // creates left-side scrollable interior and inserts into window
  164.     r = TRect( lbounds.b.x/2, lbounds.a.y, lbounds.b.x, lbounds.b.y );
  165.     rInterior = makeInterior( r, False );
  166.     rInterior->growMode = gfGrowHiX | gfGrowHiY;
  167.     insert( rInterior );
  168.     // likewise for right-side scroller
  169. }
  170.  
  171. TInterior *TDemoWindow::makeInterior( const TRect& bounds, Boolean left )
  172. {
  173.     TRect r = TRect( bounds.b.x-1, bounds.a.y+1, bounds.b.x, bounds.b.y-1 );
  174.     TScrollBar *vScrollBar = new TScrollBar( r );
  175.     if( vScrollBar == 0 )
  176.         {
  177.         cout << "vScrollbar init error" << endl;
  178.         exit(1);
  179.         }
  180.         // production code would display error dialog box
  181.     vScrollBar->options |= ofPostProcess;
  182.     if( left )
  183.         vScrollBar->growMode = gfGrowHiY;
  184.     insert( vScrollBar );
  185.  
  186.     r = TRect( bounds.a.x+2, bounds.b.y-1, bounds.b.x-2, bounds.b.y );
  187.     TScrollBar *hScrollBar = new TScrollBar( r );
  188.     if( hScrollBar == 0 )
  189.         {
  190.         cout << "hScrollbar init error" << endl;
  191.         exit(1);
  192.         }
  193.     hScrollBar->options |= ofPostProcess;
  194.     if( left )
  195.         hScrollBar->growMode = (gfGrowHiY | gfGrowLoY);
  196.     insert( hScrollBar );
  197.  
  198.     r = bounds;
  199.     r.grow( -1, -1 );
  200.     return new TInterior( r, hScrollBar, vScrollBar );
  201. }
  202.  
  203. void TDemoWindow::sizeLimits( TPoint& minP, TPoint& maxP )
  204. {
  205.     TWindow::sizeLimits( minP, maxP );
  206.     minP.x = lInterior->size.x+9;
  207. }
  208.  
  209. TMyApp::TMyApp() :
  210.     TProgInit( &TMyApp::initStatusLine,
  211.                &TMyApp::initMenuBar,
  212.                &TMyApp::initDeskTop
  213.              )
  214. {
  215. }
  216.  
  217. void TMyApp::handleEvent(TEvent& event)
  218. {
  219.     TApplication::handleEvent(event);   // act like base!
  220.     if( event.what == evCommand )
  221.         {
  222.         switch( event.message.command )
  223.             {
  224.             case cmMyNewWin:            // but respond to additional commands
  225.                 newWindow();            // define action for cmMyNewWin
  226.                                         // command
  227.                 break;
  228.             default:
  229.                 return;
  230.             }
  231.         clearEvent( event );            // clear event after handling
  232.         }
  233. }
  234.  
  235. TMenuBar *TMyApp::initMenuBar( TRect r )
  236. {
  237.     r.b.y = r.a.y + 1;    // set bottom line 1 line below top line
  238.     return new TMenuBar( r,
  239.         *new TSubMenu( "~F~ile", kbAltF )+
  240.             *new TMenuItem( "~O~pen", cmMyFileOpen, kbF3, hcNoContext, "F3" )+
  241.             *new TMenuItem( "~N~ew",  cmMyNewWin,   kbF4, hcNoContext, "F4" )+
  242.             newLine()+
  243.             *new TMenuItem( "E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X" )+
  244.         *new TSubMenu( "~W~indow", kbAltW )+
  245.             *new TMenuItem( "~N~ext", cmNext,     kbF6, hcNoContext, "F6" )+
  246.             *new TMenuItem( "~Z~oom", cmZoom,     kbF5, hcNoContext, "F5" )
  247.         );
  248. }
  249.  
  250. TStatusLine *TMyApp::initStatusLine( TRect r )
  251. {
  252.     r.a.y = r.b.y - 1;     // move top to 1 line above bottom
  253.     return new TStatusLine( r,
  254.         *new TStatusDef( 0, 0xFFFF ) +
  255.         // set range of help contexts
  256.             *new TStatusItem( 0, kbF10, cmMenu ) +
  257.             // define an item
  258.             *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  259.             // and another one
  260.             *new TStatusItem( "~Alt-F3~ Close", kbAltF3, cmClose )
  261.             // and another one
  262.         );
  263. }
  264.  
  265. void TMyApp::newWindow()
  266. {
  267.     TRect r( 0, 0, 45, 13 );            // set initial size and position
  268.  
  269.     /* SS: micro change here */
  270.  
  271.     //r.move( random(34), random(11) ); // randomly move around screen
  272.     r.move( random() % 34, random() % 11 ); // randomly move around screen
  273.     TDemoWindow *window = new TDemoWindow ( r, "Demo Window", ++winNumber);
  274.     deskTop->insert(window);    // put window into desktop and draw it
  275. }
  276.  
  277. int main()
  278. {
  279.     readFile( fileToRead );
  280.     TMyApp myApp;
  281.     myApp.run();
  282.     deleteFile();
  283.     return 0;
  284. }
  285.