UPLOAD - DOSUIT07.ZIP - TEXT.CPP

 
Output of file : TEXT.CPP contained in archive : DOSUIT07.ZIP

#ifdef ALL
#define TEXTA
#define TEXTB
#define TEXTC
#define TEXTD
#define TEXTE
#define TEXTF
#define TEXTG
#define TEXTH
#endif
#ifndef TEXTA
#define LIBRARY_MODULE
#endif

#include
#include "text.hpp"
#include "window.hpp"


#ifdef TEXTA
static char *emptyText = "Out of memory !";

void Text::build(const char *string,int length)
{
if(length <= 0)
length = strlen(string);
textlen = length ? length : length + 1;
text = new char[maxlen()+1];
if(text)
{
text[maxlen()] = 0;
setString(string);
}
else
{
text = emptyText;
setFlag(SFdisabled);
}
}

Text::~Text()
{
if(text != emptyText)
delete text;
}

void Text::setString(const char *string)
{
if(maxlen() > 0 && text != emptyText)
{
strncpy(text,string,maxlen());
text[maxlen()] = 0;
}
dirty();
}

coord Text::minwidth() const
{
return textlen;
}

void Text::paint(Rectangle r)
{
VObject::paint(r);
window->printstring(topleft(),r,getcolor(),string());
}

#ifdef NINSPECT
char *Text::inspectinfo(char *buf,const char *s) const
{
return "";
}
#else
char *Text::inspectinfo(char *buf,const char *s) const
{
if(!s)
s = "Text ";
buf = VObject::inspectinfo(buf,s);
return strcat(buf,form("Text : '%s'\n",text));
}
#endif
#ifdef NDEBUG
char *Text::debuginfo(char *buf) const
{
return buf;
}
#else
char *Text::debuginfo(char *buf) const
{
buf = VObject::debuginfo(buf);
return strcat(buf,form("'%.10s'",text));
}
#endif
#endif

#ifdef TEXTB
Text::Text(coord width,coord height,const char *string,int length)
: VObject(width,height,"Text")
{
build(string,length);
}
#endif

#ifdef TEXTC
Text::Text(const Point &p,const char *string,int length)
: VObject(p.x,p.y,"Text")
{
build(string,length);
}
#endif

#ifdef TEXTD
Text::Text(const char *string,int length)
: VObject(1,1,"Text")
{
build(string,length);
setwidth(textlen);
}
#endif

#ifdef TEXTE
CenterText::CenterText(const char *string,int length)
: Text(string,length)
{
}

void CenterText::paint(Rectangle r)
{
VObject::paint(r);
Point p = topleft();
int l = strlen(string());
if(l < width())
p.x += (width() - l) / 2;
window->printstring(p,r,getcolor(),string());
}
#endif

#ifdef TEXTE
RightText::RightText(const char *string,int length)
: Text(string,length)
{
}

void RightText::paint(Rectangle r)
{
VObject::paint(r);
Point p = topleft();
int l = strlen(string());
if(l < width())
p.x += width() - l;
window->printstring(p,r,getcolor(),string());
}
#endif

#ifdef TEXTF
static Point calcsize(const char *string,int length)
{
Point p;

p.x = 0;
p.y = 1;
if(length <= 0)
length = -1; // never terminate loop on length
for(int w = 0; *string && length--; string++, w++)
if(*string == '\n')
{
if(p.y < maxcoord)
p.y++;
if(w > maxcoord)
w = maxcoord;
if(w > p.x)
p.x = w;
w = -1;
}
if(w > maxcoord)
w = maxcoord;
if(w > p.x)
p.x = w;
return p;
}

MultiLineText::MultiLineText(const char *string,int length)
: Text(calcsize(string,length),string,length)
{
}

coord MultiLineText::minwidth() const
{
Point p = calcsize(string(),maxlen());
return p.x;
}

coord MultiLineText::minheight() const
{
Point p = calcsize(string(),maxlen());
return p.y;
}

void MultiLineText::paint(Rectangle r)
{
r &= *this;
VObject::paint(r);

const char *s = string();
Point p;
for(p.x = r.left(), p.y = top(); *s && p.y <= r.bottom(); p.y++)
{
const char *line = s;
for(int len = 0; *s && *s++ != '\n'; len++)
;
len -= r.left() - left();
if(len > 0 && p.y >= r.top())
{
line += r.left() - left();
if(len > r.width())
len = r.width();
window->printstring(p,getcolor(),line,len);
}
}
}
#endif

#ifdef TEXTG
MultiLineText::MultiLineText(coord width,coord height,const char *string,
int length)
: Text(width,height,string,length)
{
}
#endif

#ifdef TEXTH
FixedText::FixedText(const char *string)
: VObject(strlen(string),1,string)
{
}

coord FixedText::minwidth() const
{
return strlen(name());
}

void FixedText::paint(Rectangle r)
{
VObject::paint(r);
window->printstring(topleft(),r,getcolor(),name());
}
#endif