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

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