home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / c / actlib11 / tvtools / fileview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-14  |  4.4 KB  |  186 lines

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