home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1992 / number1 / fileview.cpp < prev    next >
C/C++ Source or Header  |  1992-01-04  |  5KB  |  185 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.  
  9. #define Uses_MsgBox
  10. #define Uses_TKeys
  11. #define Uses_TScroller
  12. #define Uses_TDrawBuffer
  13. #define Uses_TRect
  14. #define Uses_TProgram
  15. #define Uses_TDeskTop
  16. #define Uses_TStreamableClass
  17. #include <tv.h>
  18. __link(RScroller)
  19. __link(RScrollBar)
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25.  
  26. #include <fstream.h>
  27.  
  28. // PRFILE CHANGE: removed #include "tvcmds.h".
  29.  
  30. #include "fileview.h"
  31.  
  32. // PRFILE CHANGE: added #include "prfile.h".
  33. #include "prfile.h"
  34.  
  35. const char * const TFileViewer::name = "TFileViewer";
  36.  
  37. TFileViewer::TFileViewer( const TRect& bounds,
  38.               TScrollBar *aHScrollBar,
  39.               TScrollBar *aVScrollBar,
  40.               const char *aFileName) :
  41.     TScroller( bounds, aHScrollBar, aVScrollBar )
  42. {
  43.     growMode = gfGrowHiX | gfGrowHiY;
  44.     isValid = True;
  45.     fileName = 0;
  46.     readFile( aFileName );
  47.  
  48.     // PRFILE CHANGE: added enable of cmPrintFile.
  49.     if (!filesOpen++)
  50.       enableCommand(cmPrintFile);
  51. }
  52.  
  53. TFileViewer::~TFileViewer()
  54. {
  55.     // PRFILE CHANGE: added disable of cmPrintFile.
  56.     if (!--filesOpen)
  57.       disableCommand(cmPrintFile);
  58.  
  59.      delete fileName;
  60.      delete fileLines;
  61. }
  62.  
  63. void TFileViewer::draw()
  64. {
  65.     char *p;
  66.  
  67.     ushort c =  getColor(0x0301);
  68.     for( int i = 0; i < size.y; i++ )
  69.         {
  70.         TDrawBuffer b;
  71.         b.moveChar( 0, ' ', c, size.x );
  72.  
  73.         if( delta.y + i < fileLines->getCount() )
  74.             {
  75.             char s[maxLineLength+1];
  76.             p = (char *)( fileLines->at(delta.y+i) );
  77.             if( p == 0 || strlen(p) < delta.x )
  78.                 s[0] = EOS;
  79.             else
  80.                 {
  81.                 strncpy( s, p+delta.x, size.x );
  82.                 if( strlen( p + delta.x ) > size.x )
  83.                     s[size.x] = EOS;
  84.                 }
  85.             b.moveStr( 0, s, c );
  86.             }
  87.         writeBuf( 0, i, size.x, 1, b );
  88.         }
  89. }
  90.  
  91. void TFileViewer::scrollDraw()
  92. {
  93.     TScroller::scrollDraw();
  94.     draw();
  95. }
  96.  
  97. void TFileViewer::readFile( const char *fName )
  98. {
  99.     delete fileName;
  100.  
  101.     limit.x = 0;
  102.     fileName = newStr( fName );
  103.     fileLines = new TLineCollection(5, 5);
  104.     ifstream fileToView( fName );
  105.     if( !fileToView )
  106.         {
  107.         messageBox( "Invalid drive or directory", mfError | mfOKButton );
  108.         isValid = False;
  109.         }
  110.     else
  111.         {
  112.         char line[maxLineLength+1];
  113.         while( !fileToView.eof() && fileToView.get( line, sizeof line ) != 0 )
  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 int winNumber = 0;
  169.  
  170. TFileWindow::TFileWindow( const char *fileName ) :
  171.     // PRFILE CHANGE: changed winNumber++ to ++winNumber (bug).
  172.     TWindow( TProgram::deskTop->getExtent(), fileName, ++winNumber ),
  173.     TWindowInit( &TFileWindow::initFrame )
  174. {
  175.     options |= ofTileable;
  176.     TRect r( getExtent() );
  177.     r.grow(-1, -1);
  178.     insert(new TFileViewer( r,
  179.                             standardScrollBar(sbHorizontal | sbHandleKeyboard),
  180.                             standardScrollBar(sbVertical | sbHandleKeyboard),
  181.                             fileName) );
  182. }
  183.  
  184.             
  185.