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

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