/* $Id: TCOLORTX.CPP 2.4 1995/07/20 11:24:33 leon Exp $
 */
/*------------------------------------------------------------*/
/* filename -       tcolortx.doc                              */
/*                                                            */
/* function(s)                                                */
/*                  TColoredText member functions             */
/*------------------------------------------------------------*/

/*----------------------------------------------------------------------
TColoredText is a descendent of TStaticText designed to allow the writing
of colored text when color monitors are used.  With a monochrome or BW
monitor, TColoredText acts the same as TStaticText.

TColoredText is used in exactly the same way as TStaticText except that
the constructor has an extra ushort parameter specifying the attribute
desired.  (Do not use a 0 attribute, black on black).
----------------------------------------------------------------------*/

#define Uses_TColoredText

#define Uses_TStaticText

#define Uses_TApplication

#define Uses_TDrawBuffer

#define Uses_opstream

#define Uses_ipstream

#include <tvision/tv.h>

#include "tcolortx.h"


#if !defined( __CTYPE_H )

#include <ctype.h>

#endif  // __CTYPE_H


#if !defined( __STRING_H )

#include <String.h>

#endif  // __STRING_H


/**
 ** Colored text constructor
 **/
TColoredText::TColoredText( const TRect& bounds, const char *aText,
				ushort attribute ) :
    TStaticText( bounds, aText ),
    attr(  attribute )
{
}

/**
 ** Sets new color text
 **/
void TColoredText::set(const char *aText)
{
  delete (char *)text;
  text =  newStr( aText ) ;
  draw();
}


/**
 ** Returns color
 **/
ushort TColoredText::getTheColor()
{
    if (TProgram::application->appPalette == apColor)
      return attr;
    else return getColor(1);
}

/**
 ** Draws the colored text
 **/
void TColoredText::draw()
{
    uchar color;
    Boolean center;
    int i, j, l, p, y;
    TDrawBuffer b;
    char s[256];

    color = getTheColor();
    getText(s);
    l = strlen(s);
    p = 0;
    y = 0;
    center = False;
    while (y < size.y)
        {
        b.moveChar(0, ' ', color, size.x);
        if (p < l)
            {
            if (s[p] == 3)
                {
                center = True;
                ++p;
                }
            i = p;
            do {
               j = p;
               while ((p < l) && (s[p] == ' '))
                   ++p;
               while ((p < l) && (s[p] != ' ') && (s[p] != '\n'))
                   ++p;
               } while ((p < l) && (p < i + size.x) && (s[p] != '\n'));
            if (p > i + size.x)
                if (j > i)
                    p = j;
                else
                    p = i + size.x;
            if (center == True)
               j = (size.x - p + i) / 2 ;
            else
               j = 0;
            b.moveBuf(j, &s[i], color, (p - i));
            while ((p < l) && (s[p] == ' '))
                p++;
            if ((p < l) && (s[p] == '\n'))
                {
                center = False;
                p++;
                }
            }
        writeLine(0, y++, size.x, 1, b);
        }
}


/**
 ** Writes to the stream
 **/
void TColoredText::write( opstream& os )
{
    TStaticText::write( os );
    os << attr;
}

/**
 ** Reads from stream
 **/
void *TColoredText::read( ipstream& is )
{
    TStaticText::read( is );
    is >> attr;
    return this;
}

/**
 ** Builds view
 **/
TStreamable *TColoredText::build()
{
    return new TColoredText( streamableInit );
}

/**
 ** Destructor
 **/
TColoredText::TColoredText( StreamableInit ) : TStaticText( streamableInit )
{
}

const char * const near TColoredText::name = "TColoredText";




syntax highlighted by Code2HTML, v. 0.9.1