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

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID08 Demo Source File                             */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. // same as tvguid07 except for scrolling interior
  10. // add TDemoWindow::makeInterior
  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. #include <tv.h>
  37.  
  38. const int cmMyFileOpen = 200; // assign new command values
  39. const int cmMyNewWin   = 201;
  40.  
  41. const char *fileToRead = "tvguid08.cpp";
  42. const int maxLineLength = maxViewWidth+1;
  43. const int maxLines      = 100;
  44. char *lines[maxLines];
  45. int lineCount = 0;
  46.  
  47. void readFile( const char *fileName )
  48. {
  49.     ifstream fileToView( fileName );
  50.     if( !fileToView )
  51.         {
  52.         cout << "Invalid file name..." << endl;
  53.         exit( 1 );
  54.         }
  55.     else
  56.         {
  57.         char buf[maxLineLength];
  58.         while( lineCount < maxLines &&
  59.                fileToView.getline( buf, maxLineLength ) != 0 )
  60.             {
  61.             lines[lineCount] = newStr( buf );
  62.             lineCount++;
  63.             }
  64.         }
  65. }
  66.  
  67. void deleteFile()
  68. {
  69.     for( int i = 0; i < lineCount; i++ )
  70.         delete lines[i];
  71. }
  72.  
  73. class TMyApp : public TApplication
  74. {
  75.  
  76. public:
  77.     TMyApp();
  78.     static TStatusLine *initStatusLine( TRect r );
  79.     static TMenuBar *initMenuBar( TRect r );
  80.     virtual void handleEvent( TEvent& event);
  81.     void myNewWindow();
  82. };
  83.  
  84.  
  85. static short winNumber = 0;          // initialize window number
  86.  
  87. class TDemoWindow : public TWindow   // define a new window class
  88. {
  89.  
  90. public:
  91.  
  92.     TDemoWindow( const TRect& bounds, const char *aTitle, short aNumber );
  93.     void makeInterior();
  94.  
  95. };
  96.  
  97. class TInterior : public TScroller
  98. {
  99.  
  100. public:
  101.  
  102.     TInterior( const TRect& bounds, TScrollBar *aHScrollBar,
  103.            TScrollBar *aVScrollBar ); // constructor
  104.     virtual void draw();              // override TView::draw
  105. };
  106.  
  107. TInterior::TInterior( const TRect& bounds, TScrollBar *aHScrollBar,
  108.               TScrollBar *aVScrollBar ) :
  109.        TScroller( bounds, aHScrollBar, aVScrollBar )
  110. {
  111.     growMode = gfGrowHiX | gfGrowHiY;
  112.     options = options | ofFramed;
  113.     setLimit( maxLineLength, maxLines );
  114. }
  115.  
  116. void TInterior::draw()       // modified for scroller
  117. {
  118.     ushort color = getColor(0x0301);
  119.     for( int i = 0; i < size.y; i++ )
  120.         // for each line:
  121.         {
  122.         TDrawBuffer b;
  123.         b.moveChar( 0, ' ', color, size.x );
  124.         // fill line buffer with spaces
  125.         int j = delta.y + i;       // delta is scroller offset
  126.         if( lines[j] )
  127.             {
  128.             char s[maxLineLength];
  129.             if( delta.x > strlen(lines[j] ) )
  130.                 s[0] = EOS;
  131.             else
  132.                 {
  133.                 strncpy( s, lines[j]+delta.x, size.x );
  134.                 s[size.x] = EOS;
  135.                 }
  136.             b.moveStr( 0, s, color );
  137.             }
  138.         writeLine( 0, i, size.x, 1, b);
  139.         }
  140. }
  141.  
  142. void TDemoWindow::makeInterior()
  143. {
  144.     TScrollBar *vScrollBar =
  145.         standardScrollBar( sbVertical | sbHandleKeyboard );
  146.     TScrollBar *hScrollBar =
  147.         standardScrollBar( sbHorizontal |  sbHandleKeyboard );
  148.     TRect r = getClipRect();    // get exposed view bounds
  149.     r.grow( -1, -1 );           // shrink to fit inside window frame
  150.     insert( new TInterior( r, hScrollBar, vScrollBar ));
  151. }
  152.  
  153. TMyApp::TMyApp() :
  154.     TProgInit( &TMyApp::initStatusLine,
  155.                &TMyApp::initMenuBar,
  156.                &TMyApp::initDeskTop
  157.              )
  158. {
  159. }
  160.  
  161. TStatusLine *TMyApp::initStatusLine(TRect r)
  162. {
  163.     r.a.y = r.b.y - 1;     // move top to 1 line above bottom
  164.     return new TStatusLine( r,
  165.         *new TStatusDef( 0, 0xFFFF ) +
  166.         // set range of help contexts
  167.             *new TStatusItem( 0, kbF10, cmMenu ) +
  168.             // define an item
  169.             *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit ) +
  170.             // and another one
  171.             *new TStatusItem( "~Alt-F3~ Close", kbAltF3, cmClose )
  172.             // and another one
  173.         );
  174. }
  175.  
  176. TMenuBar *TMyApp::initMenuBar( TRect r )
  177. {
  178.     r.b.y = r.a.y + 1;    // set bottom line 1 line below top line
  179.     return new TMenuBar( r,
  180.         *new TSubMenu( "~F~ile", kbAltF )+
  181.             *new TMenuItem( "~O~pen", cmMyFileOpen, kbF3, hcNoContext, "F3" )+
  182.             *new TMenuItem( "~N~ew",  cmMyNewWin,   kbF4, hcNoContext, "F4" )+
  183.             newLine()+
  184.             *new TMenuItem( "E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X" )+
  185.         *new TSubMenu( "~W~indow", kbAltW )+
  186.             *new TMenuItem( "~N~ext", cmNext,     kbF6, hcNoContext, "F6" )+
  187.             *new TMenuItem( "~Z~oom", cmZoom,     kbF5, hcNoContext, "F5" )
  188.         );
  189. }
  190.  
  191. void TMyApp::handleEvent(TEvent& event)
  192. {
  193.     TApplication::handleEvent(event);   // act like base!
  194.     if( event.what == evCommand )
  195.         {
  196.         switch( event.message.command )
  197.             {
  198.             case cmMyNewWin:            // but respond to additional commands
  199.                 myNewWindow();          // define action for cmMyNewWin                                // command
  200.                 break;
  201.             default:
  202.                 return;
  203.             }
  204.         clearEvent( event );            // clear event after handling
  205.         }
  206. }
  207.  
  208. void TMyApp::myNewWindow()
  209. {
  210.     TRect r( 0, 0, 26, 7 );             // set initial size and position
  211.     r.move( random(53), random(16) );   // randomly move around screen
  212.     TDemoWindow *window = new TDemoWindow ( r, "Demo Window", ++winNumber);
  213.     deskTop->insert(window);    // put window into desktop and draw it
  214. }
  215.  
  216. TDemoWindow::TDemoWindow( const TRect& bounds, const char *aTitle,
  217.               short aNumber) :
  218.          TWindow( bounds, aTitle, aNumber),
  219.          TWindowInit( &TDemoWindow::initFrame )
  220. {
  221.     makeInterior(); // creates scrollable interior and inserts into window
  222. }
  223.  
  224.  
  225. int main()
  226. {
  227.     readFile( fileToRead );
  228.     TMyApp myApp;
  229.     myApp.run();
  230.     deleteFile();
  231.     return 0;
  232. }
  233.