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

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