home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / demos / fileview.cpp < prev    next >
C/C++ Source or Header  |  1998-01-19  |  4KB  |  182 lines

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