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

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