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

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   Turbo Vision FileViewer Demo Support File             */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8. #define Uses_MsgBox
  9. #define Uses_TKeys
  10. #define Uses_TScroller
  11. #define Uses_TDrawBuffer
  12. #define Uses_TRect
  13. #define Uses_TProgram
  14. #define Uses_TDeskTop
  15. #define Uses_TStreamableClass
  16. #include <tv.h>
  17. __link(RScroller)
  18. __link(RScrollBar)
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24.  
  25. #include <fstream.h>
  26.  
  27. #include "tvcmds.h"
  28. #include "fileview.h"
  29.  
  30.  
  31. const char * const TFileViewer::name = "TFileViewer";
  32.  
  33. TFileViewer::TFileViewer( const TRect& bounds,
  34.                           TScrollBar *aHScrollBar,
  35.                           TScrollBar *aVScrollBar,
  36.                           const char *aFileName) :
  37.     TScroller( bounds, aHScrollBar, aVScrollBar )
  38. {
  39.     growMode = gfGrowHiX | gfGrowHiY;
  40.     isValid = True;
  41.     fileName = 0;
  42.     readFile( aFileName );
  43. }
  44.  
  45. TFileViewer::~TFileViewer()
  46. {
  47.      delete fileName;
  48.      destroy (fileLines);
  49. }
  50.  
  51. void TFileViewer::draw()
  52. {
  53.     char *p;
  54.  
  55.     ushort c =  getColor(0x0301);
  56.     for( int i = 0; i < size.y; i++ )
  57.         {
  58.         TDrawBuffer b;
  59.     b.moveChar( 0, ' ', c, size.x );
  60.  
  61.         if( delta.y + i < fileLines->getCount() )
  62.             {
  63.             char s[maxLineLength+1];
  64.             p = (char *)( fileLines->at(delta.y+i) );
  65.             if( p == 0 || strlen(p) < delta.x )
  66.                 s[0] = EOS;
  67.             else
  68.         {
  69.         strncpy( s, p+delta.x, size.x );
  70.         if( strlen( p + delta.x ) > size.x )
  71.                     s[size.x] = EOS;
  72.                 }
  73.             b.moveStr( 0, s, c );
  74.             }
  75.         writeBuf( 0, i, size.x, 1, b );
  76.         }
  77. }
  78.  
  79. void TFileViewer::scrollDraw()
  80. {
  81.     TScroller::scrollDraw();
  82.     draw();
  83. }
  84.  
  85. void TFileViewer::readFile( const char *fName )
  86. {
  87.     delete fileName;
  88.  
  89.     limit.x = 0;
  90.     fileName = newStr( fName );
  91.     fileLines = new TLineCollection(5, 5);
  92.     ifstream fileToView( fName );
  93.     if( !fileToView )
  94.         {
  95.         messageBox( "Invalid drive or directory", mfError | mfOKButton );
  96.         isValid = False;
  97.         }
  98.     else
  99.         {
  100.         char line[maxLineLength+1];
  101.         while( !lowMemory() &&
  102.                !fileToView.eof() && 
  103.                fileToView.get( line, sizeof line ) != 0 
  104.              )
  105.             {
  106.             char c;
  107.             fileToView.get(c);      // grab trailing newline
  108.             limit.x = max( limit.x, strlen( line ) );
  109.             fileLines->insert( newStr( line ) );
  110.             }
  111.         isValid = True;
  112.         }
  113.     limit.y = fileLines->getCount();
  114. }
  115.  
  116. void TFileViewer::setState( ushort aState, Boolean enable )
  117. {
  118.     TScroller::setState( aState, enable );
  119.     if( enable && (aState & sfExposed) )
  120.         setLimit( limit.x, limit.y );
  121. }
  122.  
  123. Boolean TFileViewer::valid( ushort )
  124. {
  125.     return isValid;
  126. }
  127.  
  128. void *TFileViewer::read(ipstream& is)
  129. {
  130.     char *fName;
  131.  
  132.     TScroller::read(is);
  133.     fName = is.readString();
  134.     fileName = 0;
  135.     readFile(fName);
  136.     delete fName; 
  137.     return this;
  138. }
  139.  
  140. void TFileViewer::write(opstream& os)
  141. {
  142.     TScroller::write(os);
  143.     os.writeString(fileName);
  144. }
  145.  
  146. TStreamable *TFileViewer::build()
  147. {
  148.     return new TFileViewer( streamableInit );
  149. }
  150.  
  151.  
  152. TStreamableClass RFileView( TFileViewer::name,
  153.                             TFileViewer::build,
  154.                               __DELTA(TFileViewer)
  155.                           );
  156.  
  157.  
  158.  
  159. static int winNumber = 0;
  160.  
  161. TFileWindow::TFileWindow( const char *fileName ) :
  162.     TWindow( TProgram::deskTop->getExtent(), fileName, winNumber++ ),
  163.     TWindowInit( &TFileWindow::initFrame )
  164. {
  165.     options |= ofTileable;
  166.     TRect r( getExtent() );
  167.     r.grow(-1, -1);
  168.     insert(new TFileViewer( r,
  169.                             standardScrollBar(sbHorizontal | sbHandleKeyboard),
  170.                             standardScrollBar(sbVertical | sbHandleKeyboard),
  171.                             fileName) );
  172. }
  173.  
  174.  
  175.