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

  1. /*-------------------------------------------------------------*/
  2. /* filename -       textview.cpp                               */
  3. /*                                                             */
  4. /* function(s)                                                 */
  5. /*                  TTerminal and TTextDevice member functions */
  6. /*-------------------------------------------------------------*/
  7.  
  8. /*-------------------------------------------------------------*/
  9. /*                                                             */
  10. /*    Turbo Vision -  Version 1.0                              */
  11. /*                                                             */
  12. /*                                                             */
  13. /*    Copyright (c) 1991 by Borland International              */
  14. /*    All Rights Reserved.                                     */
  15. /*                                                             */
  16. /*-------------------------------------------------------------*/
  17.  
  18. #define Uses_TTextDevice
  19. #define Uses_TTerminal
  20. #define Uses_otstream
  21. #include <tv.h>
  22.  
  23. #if !defined( __STRING_H )
  24. #include <String.h>
  25. #endif  // __STRING_H
  26.  
  27. TTextDevice::TTextDevice( const TRect& bounds,
  28.                           TScrollBar *aHScrollBar,
  29.                           TScrollBar *aVScrollBar) :
  30.     TScroller(bounds,aHScrollBar,aVScrollBar)
  31. {
  32. }
  33.  
  34. int TTextDevice::overflow( int c )
  35. {
  36.     if( c != EOF )
  37.         {
  38.         char b = c;
  39.         do_sputn( &b, 1 );
  40.         }
  41.     return 1;
  42. }
  43.  
  44. TTerminal::TTerminal( const TRect& bounds,
  45.                       TScrollBar *aHScrollBar,
  46.                       TScrollBar *aVScrollBar,
  47.                       ushort aBufSize ) :
  48.     TTextDevice(bounds, aHScrollBar, aVScrollBar),
  49.     queFront( 0 ),
  50.     queBack( 0 )
  51. {
  52.     growMode = gfGrowHiX + gfGrowHiY;
  53.     bufSize = min( 32000U, aBufSize );
  54.     buffer = new char[ bufSize ];
  55.     setLimit( 0, 1 );
  56.     setCursor( 0, 0 );
  57.     showCursor();
  58. }
  59.  
  60.  
  61. TTerminal::~TTerminal()
  62. {
  63.     delete buffer;
  64. }
  65.  
  66. void TTerminal::bufDec( ushort& val )
  67. {
  68.     if (val == 0)
  69.         val = bufSize - 1;
  70.     else
  71.         val--;
  72. }
  73.  
  74. void TTerminal::bufInc( ushort& val )
  75. {
  76.     if( ++val >= bufSize )
  77.         val = 0;
  78. }
  79.  
  80. Boolean TTerminal::canInsert( ushort amount )
  81. {
  82.     long T = (queFront < queBack) ?
  83.         ( queFront +  amount ) :
  84.         ( long(queFront) - bufSize + amount);   // cast needed so we get
  85.                                                 // signed comparison
  86.     return Boolean( queBack > T );
  87. }
  88.  
  89. void TTerminal::draw()
  90. {
  91.     short  i;
  92.     ushort begLine, endLine;
  93.     char s[256];
  94.     ushort bottomLine;
  95.  
  96.     bottomLine = size.y + delta.y;
  97.     if( limit.y > bottomLine )
  98.         {
  99.         endLine = prevLines( queFront, limit.y - bottomLine );
  100.         bufDec( endLine );
  101.         }
  102.     else
  103.         endLine = queFront;
  104.  
  105.     if( limit.y > size.y )
  106.         i = size.y - 1;
  107.     else
  108.         {
  109.         for( i = limit.y; i <= size.y - 1; i++ )
  110.             writeChar(0, i, ' ', 1, size.x);
  111.         i =  limit.y -  1;
  112.         }
  113.  
  114.     for( ; i >= 0; i-- )
  115.         {
  116.         begLine = prevLines(endLine, 1);
  117.         if (endLine >= begLine)
  118.             {
  119.             int T = int( endLine - begLine );
  120.             memcpy( s, &buffer[begLine], T );
  121.             s[T] = EOS;
  122.             }
  123.         else
  124.             {
  125.             int T = int( bufSize - begLine);
  126.             memcpy( s, &buffer[begLine], T );
  127.             memcpy( s+T, buffer, endLine );
  128.             s[T+endLine] = EOS;
  129.             }
  130.         if( delta.x >= strlen(s) )
  131.             *s = EOS;
  132.         else
  133.             strcpy( s, &s[delta.x] );
  134.  
  135.         writeStr( 0, i, s, 1 );
  136.         writeChar( strlen(s), i, ' ', 1, size.x );
  137.         endLine = begLine;
  138.         bufDec( endLine );
  139.         }
  140. }
  141.  
  142. ushort TTerminal::nextLine( ushort pos )
  143. {
  144.     if( pos != queFront )
  145.         {
  146.         while( buffer[pos] != '\n' && pos != queFront )
  147.             bufInc(pos);
  148.         if( pos != queFront )
  149.             bufInc( pos );
  150.         }
  151.     return pos;
  152. }
  153.  
  154. int TTerminal::do_sputn( const char *s, int count )
  155. {
  156.     ushort screenLines = limit.y;
  157.     for( ushort i = 0; i < count; i++ )
  158.         if( s[i] == '\n' )
  159.             screenLines++;
  160.  
  161.     while( !canInsert( count ) )
  162.         {
  163.         queBack = nextLine( queBack );
  164.         screenLines--;
  165.         }
  166.  
  167.     if( queFront + count >= bufSize )
  168.         {
  169.         i = bufSize - queFront;
  170.         memcpy( &buffer[queFront], s, i );
  171.         memcpy( buffer, &s[i], count - i );
  172.         queFront = count - i;
  173.         }
  174.     else
  175.         {
  176.         memcpy( &buffer[queFront], s, count );
  177.         queFront += count;
  178.         }
  179.  
  180.     setLimit( limit.x, screenLines );
  181.     scrollTo( 0, screenLines + 1 );
  182.     i = prevLines( queFront, 1 );
  183.     if( i <= queFront )
  184.         i = queFront - i;
  185.     else
  186.         i = bufSize - (i - queFront);
  187.     setCursor( i, screenLines - delta.y - 1 );
  188.     drawView();
  189.     return count;
  190. }
  191.  
  192. Boolean TTerminal::queEmpty()
  193. {
  194.     return Boolean( queBack == queFront );
  195. }
  196.  
  197. otstream::otstream( TTerminal *tt )
  198. {
  199.     ios::init( tt );
  200. }
  201.