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

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