home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / TVCOLR.ZIP / TCOLTXT.CPP < prev    next >
C/C++ Source or Header  |  1994-02-08  |  4KB  |  158 lines

  1. // Program : TCOLTXT.CPP
  2. // Author  : Eric Woodruff,  CIS ID: 72134,1150
  3. // Updated : Tue 02/08/94 10:27:47
  4. // Note    : Hereby declared public domain
  5. // Compiler: Borland C++ 3.1/4.0
  6. //
  7. // Member functions for the TColorText class.  Demonstrates the use of the
  8. // concepts described in TVCOLR.DOC.
  9. //
  10. // TColorText is derived from TStaticText.  It adds the ability to use
  11. // color for the data displayed.  One of five color types can be used.
  12. //
  13. //      1. Normal Color Text (just like TStaticText)
  14. //      2. Information Color Text
  15. //      3. Notification Color Text
  16. //      4. Warning Color Text
  17. //      5. Error Color Text
  18. //
  19. // This makes use of TVCOLR.H so the colors are modifiable via a
  20. // TColorDialog object.
  21. //
  22.  
  23. #include <ctype.h>
  24. #include <string.h>
  25.  
  26. #define Uses_TDrawBuffer
  27. #define Uses_TStaticText
  28. #define Uses_TStreamableClass
  29. #define Uses_TVCOLR             // Use this if you modified the TV.H file.
  30. #define Uses_ipstream
  31. #define Uses_opstream
  32. #include <tv.h>
  33.  
  34. __link(RStaticText)
  35.  
  36. #if !defined(cpDefSize)
  37. // Use this if you choose not to modify the Turbo Vision files.
  38. #include <tvcolr.h>
  39. #endif
  40.  
  41. #include <tcoltxt.h>        // The TColorText header file.
  42.  
  43. // ****************************************************************************
  44. // Create a character array where each element is a color index.
  45. // This forms the palette for the object.
  46.  
  47. char cpColorText[] = { '\x06', cCTxtInfoColor, cCTxtNotifyColor,
  48.                                cCTxtWarnColor, cCTxtErrorColor };
  49.  
  50. // ****************************************************************************
  51. // Make it streamable.
  52. //
  53. const char * const near TColorText::name = "TColorText";
  54.  
  55. TStreamableClass RColorText(TColorText::name, TColorText::build,
  56.     __DELTA(TColorText));
  57.  
  58. void TColorText::write(opstream &os)
  59. {
  60.     TStaticText::write(os);
  61.     os << ColorType;
  62. }
  63.  
  64. void *TColorText::read(ipstream &is)
  65. {
  66.     TStaticText::read(is);
  67.     is >> ColorType;
  68.     return this;
  69. }
  70.  
  71. TStreamable *TColorText::build()
  72. {
  73.     return new TColorText( streamableInit );
  74. }
  75.  
  76. // ****************************************************************************
  77. TPalette& TColorText::getPalette() const
  78. {
  79.     // Note the lack of a "- 1" after the sizeof().  There is no NULL on
  80.     // this type of palette because it is a simple array, not a string.
  81.     // Don't account for one!
  82.     static TPalette palette( cpColorText, sizeof( cpColorText) );
  83.     return palette;
  84. }
  85.  
  86. void TColorText::draw()
  87. {
  88.     TDrawBuffer b;
  89.     Boolean center = False;
  90.  
  91.     uchar color = getColor(1 + ColorType);        // Get the right color.
  92.     int startPos, wrapPos, len, pos, curLine;
  93.     char s[256];
  94.  
  95.     getText(s);
  96.     len = strlen(s);
  97.     pos = curLine = 0;
  98.  
  99.     // Draw each line.
  100.     while(curLine < size.y)
  101.     {
  102.         b.moveChar(0, ' ', color, size.x);
  103.  
  104.         // Fill in line if not at the end of the string.
  105.         if(pos < len)
  106.         {
  107.             // Center the line?
  108.             if(s[pos] == '\003')
  109.             {
  110.                 center = True;
  111.                 ++pos;
  112.             }
  113.  
  114.             startPos = pos;
  115.  
  116.             // Find end of line.
  117.             do
  118.             {
  119.                wrapPos = pos;
  120.  
  121.                while(pos < len && s[pos] == ' ')
  122.                    ++pos;
  123.  
  124.                while(pos < len && s[pos] != ' ' && s[pos] != '\n')
  125.                    ++pos;
  126.  
  127.             } while(pos < len && pos < startPos + size.x && s[pos] != '\n');
  128.  
  129.             // Word wrap the text?
  130.             if(pos > startPos + size.x)
  131.                 if(wrapPos > startPos)
  132.                     pos = wrapPos;
  133.                 else
  134.                     pos = startPos + size.x;
  135.  
  136.             if(center)
  137.                wrapPos = (size.x - pos + startPos) / 2 ;
  138.             else
  139.                wrapPos = 0;
  140.  
  141.             b.moveBuf(wrapPos, &s[startPos], color, (pos - startPos));
  142.  
  143.             // Skip trailing blanks.
  144.             while((pos < len) && (s[pos] == ' '))
  145.                 pos++;
  146.  
  147.             // If line ended in a line feed, skip it and turn
  148.             // off centering.
  149.             if(pos < len && s[pos] == '\n')
  150.             {
  151.                 center = False;
  152.                 pos++;
  153.             }
  154.         }
  155.         writeLine(0, curLine++, size.x, 1, b);
  156.     }
  157. }
  158.