home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / tutorial / tvguid16.cpp < prev   
C/C++ Source or Header  |  1999-05-24  |  11KB  |  370 lines

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