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

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