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

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