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

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