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

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