home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 15.ddi / TVDOCDEM.ZIP / TVGUID11.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  8.4 KB  |  295 lines

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