home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / TStaticText.cpp < prev    next >
C/C++ Source or Header  |  1998-01-19  |  3KB  |  131 lines

  1. /*
  2.  * TStaticText.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #define Uses_TStaticText
  13. #define Uses_TDrawBuffer
  14. #define Uses_opstream
  15. #define Uses_ipstream
  16. #include <tvision/tv.h>
  17.  
  18. #include <ctype.h>
  19. #include <string.h>
  20.  
  21. #define cpStaticText "\x06"
  22.  
  23. TStaticText::TStaticText( const TRect& bounds, const char *aText ) :
  24.     TView( bounds ),
  25.     text( newStr( aText ) )
  26. {
  27.     growMode |= gfFixed;
  28. }
  29.  
  30. TStaticText::~TStaticText()
  31. {
  32.     delete (char *)text;
  33. }
  34.  
  35. void TStaticText::draw()
  36. {
  37.     uchar color;
  38.     Boolean center;
  39.     int i, j, l, p, y;
  40.     TDrawBuffer b;
  41.     char s[256];
  42.  
  43.     color = getColor(1);
  44.     getText(s);
  45.     l = strlen(s);
  46.     p = 0;
  47.     y = 0;
  48.     center = False;
  49.     while (y < size.y)
  50.         {
  51.         b.moveChar(0, ' ', color, size.x);
  52.         if (p < l)
  53.             {
  54.             if (s[p] == 3)
  55.                 {
  56.                 center = True;
  57.                 ++p;
  58.                 }
  59.             i = p;
  60.             do {
  61.                j = p;
  62.                while ((p < l) && (s[p] == ' '))
  63.                    ++p;
  64.                while ((p < l) && (s[p] != ' ') && (s[p] != '\n'))
  65.                    ++p;
  66.                } while ((p < l) && (p < i + size.x) && (s[p] != '\n'));
  67.             if (p > i + size.x)
  68.                 if (j > i)
  69.                     p = j;
  70.                 else
  71.                     p = i + size.x;
  72.             if (center == True)
  73.                j = (size.x - p + i) / 2 ;
  74.             else
  75.                j = 0;
  76.             b.moveBuf(j, &s[i], color, (p - i));
  77.             while ((p < l) && (s[p] == ' '))
  78.                 p++;
  79.             if ((p < l) && (s[p] == '\n'))
  80.                 {
  81.                 center = False;
  82.                 p++;
  83.                 }
  84.             }
  85.         writeLine(0, y++, size.x, 1, b);
  86.         }
  87. }
  88.  
  89. TPalette& TStaticText::getPalette() const
  90. {
  91.     static TPalette palette( cpStaticText, sizeof( cpStaticText )-1 );
  92.     return palette;
  93. }
  94.  
  95. void TStaticText::getText( char *s )
  96. {
  97.     if( text == 0 )
  98.         *s = EOS;
  99.     else
  100.     {
  101.         strncpy( s, text, 255 );
  102.         s[255] = EOS;
  103.     }
  104. }
  105.  
  106. #if !defined(NO_STREAMABLE)
  107.  
  108. void TStaticText::write( opstream& os )
  109. {
  110.     TView::write( os );
  111.     os.writeString( text );
  112. }
  113.  
  114. void *TStaticText::read( ipstream& is )
  115. {
  116.     TView::read( is );
  117.     text = is.readString();
  118.     return this;
  119. }
  120.  
  121. TStreamable *TStaticText::build()
  122. {
  123.     return new TStaticText( streamableInit );
  124. }
  125.  
  126. TStaticText::TStaticText( StreamableInit ) : TView( streamableInit )
  127. {
  128. }
  129.  
  130. #endif
  131.