Category : Recently Uploaded Files
Archive   : DOSUIT07.ZIP
Filename : CLUSTER.CPP

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

#ifdef ALL
#define CLUSTERA
#define CLUSTERB
#define CLUSTERC
#define CLUSTERD
#define CLUSTERE
#define CLUSTERF
#define CLUSTERG
#define CLUSTERH
#define CLUSTERI
#define CLUSTERJ
#define CLUSTERK
#define CLUSTERL
#define CLUSTERM
#define CLUSTERN
#define CLUSTERO
#define CLUSTERP
#define CLUSTERQ
#define CLUSTERR
#define CLUSTERS
#define CLUSTERT
#define CLUSTERU
#define CLUSTERV
#define CLUSTERW
#define CLUSTERX
#define CLUSTERY
#endif
#ifndef CLUSTERA
#define LIBRARY_MODULE
#endif

/*
* Clusters - Groups with automatic placing
*/

#include
#include "cluster.hpp"
#include "window.hpp"
#include "event.hpp"


#ifdef CLUSTERA
#ifdef NINSPECT
char *Cluster::inspectinfo(char *buf,const char *s) const
{
return "";
}
#else
char *Cluster::inspectinfo(char *buf,const char *s) const
{
static char *halign[] = { "HCenter", "Left", "Right", "HExpand" };
static char *valign[] = { "VCenter", "Top", "Bottom", "VExpand" };
static char *bordertype[] = { "No", "Single", "Double", "Bold", "Active" };
BorderFlag f = testbFlag(BorderFlag(BFshowactive|BFstyle));

if(f > BFshowactive)
f = BFshowactive;
if(!s)
s = "Cluster ";
buf = Group::inspectinfo(buf,s);
strcat(buf,form("Align:%s,%s\n",
halign[alignment & AFHoriz],
valign[(alignment & AFVert) >> 2]));
return strcat(buf,form("%s Border%s\n",bordertype[f],
testbFlag(BFtitle) ? " with title" : ""));
}
#endif

Cluster::Cluster(AlignFlag a,const char *n,BorderFlag f)
: Group(n), alignment(AlignFlag(a & ~AFtabstop)), bflags(BorderFlag(f & ~BFtabstop))
{
if((f & BFtabstop) || (a & AFtabstop))
setFlag(SFtabstop);
}

void Cluster::setwidth(const coord w)
{
if(width() == w)
return;
Group::setwidth(w);
realign();
}

void Cluster::setheight(const coord h)
{
if(height() == h)
return;
Group::setheight(h);
realign();
}

void Cluster::paint(Rectangle r)
{
if(bflags == BFnone) // no border
{
Group::paint(r);
return;
}
r &= *this;
VObject::paint(r);
if(testbFlag(BFshowactive))
bflags = BorderFlag(testbFlag(BorderFlag(~BFstyle)) | (testcFlag(CFactive) ? BFdouble: BFsingle));
window->drawbox(*this,r,getcolor(),bflags,name());
paintcontents(r);
}

int Cluster::setfocus(int tabstop)
{
if(testcFlag(CFfocus))
return 1;
if(!Group::setfocus(tabstop))
return 0;
return 1;
}

void Cluster::resetfocus()
{
if(!testcFlag(CFfocus))
return;
Group::resetfocus();
}

void Cluster::addDone()
{
realign();
}
#endif

#ifdef CLUSTERB
HCluster::HCluster(VObject *v,...)
:Cluster(AlignFlag(AFVexpand|AFHexpand),"HCluster")
{
va_list args;

va_start(args,v);
addvalist(v,args);
va_end(args);
}
#endif

#ifdef CLUSTERC
HCluster::HCluster(AlignFlag a,VObject *v,...)
:Cluster(a,"HCluster")
{
va_list args;

va_start(args,v);
addvalist(v,args);
va_end(args);
}
#endif

#ifdef CLUSTERD
HCluster::HCluster(const char *n,AlignFlag a,VObject *v,...)
:Cluster(a,n)
{
va_list args;

va_start(args,v);
addvalist(v,args);
va_end(args);
}
#endif

#ifdef CLUSTERE
HCluster::HCluster(const char *n,AlignFlag a,BorderFlag f,VObject *v,...)
:Cluster(a,n,f)
{
va_list args;

va_start(args,v);
addvalist(v,args);
va_end(args);
}
#endif

#ifdef CLUSTERF
HCluster::HCluster(const char *n,AlignFlag a,BorderFlag f)
: Cluster(a,n,f)
{
}

HCluster::HCluster(const ObjectCollection &c,const char *n,AlignFlag a,
BorderFlag f)
: Cluster(a,n,f)
{
*this + c;
}

HCluster::HCluster(const ObjectCollection &c,AlignFlag a,
BorderFlag f)
: Cluster(a,"HCluster",f)
{
*this + c;
}
#endif

#ifdef CLUSTERG
VCluster::VCluster(VObject *v,...)
:Cluster(AFexpand,"VCluster")
{
va_list args;

va_start(args,v);
addvalist(v,args);
va_end(args);
}
#endif

#ifdef CLUSTERH
VCluster::VCluster(AlignFlag a,VObject *v,...)
:Cluster(a,"VCluster")
{
va_list args;

va_start(args,v);
addvalist(v,args);
va_end(args);
}
#endif

#ifdef CLUSTERI
VCluster::VCluster(const char *n,AlignFlag a,VObject *v,...)
:Cluster(a,n)
{
va_list args;

va_start(args,v);
addvalist(v,args);
va_end(args);
}
#endif

#ifdef CLUSTERJ
VCluster::VCluster(const char *n,AlignFlag a,BorderFlag f,VObject *v,...)
:Cluster(a,n,f)
{
va_list args;

va_start(args,v);
addvalist(v,args);
va_end(args);
}
#endif

#ifdef CLUSTERK
VCluster::VCluster(const char *n,AlignFlag a,BorderFlag f)
: Cluster(a,n,f)
{
}

VCluster::VCluster(const ObjectCollection &c,const char *n,AlignFlag a,
BorderFlag f)
: Cluster(a,n,f)
{
*this + c;
}

VCluster::VCluster(const ObjectCollection &c,AlignFlag a,
BorderFlag f)
: Cluster(a,"VCluster",f)
{
*this + c;
}
#endif

#ifdef CLUSTERL
void HCluster::realign(int resize)
{
coord skipsize = 0, freesize = 0, expand = 0, leftmargin = 0;
coord w = minwidth(), h = minheight();
coord maxw = maxwidth();

if(resize || w > width())
Group::setwidth(w);
if(resize || h > height())
Group::setheight(h);
expand = width() - w;
freesize = width() - maxw;
if(freesize < 0)
freesize = 0;
switch(alignment & AFHoriz)
{
case AFHright:
leftmargin = freesize;
break;
case AFHcenter:
leftmargin = freesize / 2;
break;
case AFHexpand:
int gap = objects.size() - 1;
skipsize = gap ? freesize / gap : 0;
leftmargin = (freesize - (skipsize * gap)) / 2;
break;
}

Rectangle r(*this);

if(testbFlag(BorderFlag(~BFnone)))
{
r.setheight(height() - 2);
r.move(leftmargin + 1,1);
}
else
r.move(leftmargin,0);

ObjectIterator next(objects);
VObject *v;

while((v = next()) != 0)
{
w = v->minwidth();
maxw = v->maxwidth();
if(maxw > w + expand)
maxw = w + expand;
expand -= maxw - w;
r.setwidth(maxw);
v->align(AlignFlag(alignment|AFHexpand),r);
r.move(skipsize + maxw,0);
}
}

coord HCluster::minwidth() const
{
ObjectIterator next(objects);
VObject *v;
coord x;

for(coord w = 0; (v = next()) != 0; w += x)
x = v->minwidth();
if(testbFlag(BFtitle))
{
x = strlen(name());
if(x > w)
w = x;
}
if(testbFlag(BorderFlag(~BFnone)))
w += 2;
return w;
}

coord HCluster::minheight() const
{
coord h = Group::minheight();

if(testbFlag(BorderFlag(~BFnone)))
h += 2;
return h;
}

coord HCluster::maxwidth() const
{
ObjectIterator next(objects);
VObject *v;
coord x;

for(coord w = 0; (v = next()) != 0; w += x)
x = v->maxwidth();
if(testbFlag(BFtitle))
{
x = strlen(name());
if(x > w)
w = x;
}
if(testbFlag(BorderFlag(~BFnone)))
w += 2;
return w;
}

coord HCluster::maxheight() const
{
coord h = Group::maxheight();

if(testbFlag(BorderFlag(~BFnone)))
h += 2;
return h;
}

int HCluster::kbdevent(KeyboardEvent& e)
{
int r = 0;

switch(e.value())
{
case Kright:
if(incfocus(0))
r = 1;
break;
case Kleft:
if(decfocus(0))
r = 1;
break;
default:
r = Group::kbdevent(e);
break;
}
return r;
}
#endif

#ifdef CLUSTERM
void VCluster::realign(int resize)
{
coord skipsize = 0, freesize = 0, expand = 0, topmargin = 0;
coord w = minwidth(), h = minheight();
coord maxh = maxheight();

if(resize || w > width())
Group::setwidth(w);
if(resize || h > height())
Group::setheight(h);
expand = height() - h;
freesize = height() - maxh;
if(freesize < 0)
freesize = 0;
switch(alignment & AFVert)
{
case AFVbottom:
topmargin = freesize;
break;
case AFVcenter:
topmargin = freesize / 2;
break;
case AFVexpand:
int gap = objects.size() - 1;
skipsize = gap ? freesize / gap : 0;
topmargin = (freesize - (skipsize * gap)) / 2;
break;
}

Rectangle r(*this);

if(testbFlag(BorderFlag(~BFnone)))
{
r.setwidth(width() - 2);
r.move(1,topmargin + 1);
}
else
r.move(0,topmargin);

ObjectIterator next(objects);
VObject *v;

while((v = next()) != 0)
{
h = v->minheight();
maxh = v->maxheight();
if(maxh > h + expand)
maxh = h + expand;
expand -= maxh - h;
r.setheight(maxh);
v->align(AlignFlag(alignment|AFVexpand),r);
r.move(0,skipsize + maxh);
}
}

coord VCluster::minwidth() const
{
coord w = Group::minwidth();

if(testbFlag(BFtitle))
{
coord m = strlen(name());
if(m > w)
w = m;
}
if(testbFlag(BorderFlag(~BFnone)))
w += 2;
return w;
}

coord VCluster::minheight() const
{
ObjectIterator next(objects);
VObject *v;
coord y;

for(coord h = 0; (v = next()) != 0; h += y)
y = v->minheight();
if(testbFlag(BorderFlag(~BFnone)))
h += 2;
return h;
}

coord VCluster::maxwidth() const
{
coord w = Group::maxwidth();

if(testbFlag(BFtitle))
{
coord m = strlen(name());
if(m > w)
w = m;
}
if(testbFlag(BorderFlag(~BFnone)))
w += 2;
return w;
}

coord VCluster::maxheight() const
{
ObjectIterator next(objects);
VObject *v;
coord y;

for(coord h = 0; (v = next()) != 0; h += y)
y = v->maxheight();
if(testbFlag(BorderFlag(~BFnone)))
h += 2;
return h;
}

int VCluster::kbdevent(KeyboardEvent& e)
{
int r = 0;

switch(e.value())
{
case Kdown:
if(incfocus(0))
r = 1;
break;
case Kup:
if(decfocus(0))
r = 1;
break;
default:
r = Group::kbdevent(e);
break;
}
return r;
}
#endif

#ifdef CLUSTERN
CCluster::CCluster(int itemsPerColumn, const char *name,AlignFlag a,
BorderFlag f)
: HCluster(name,a,f), nitems(itemsPerColumn)
{
}
#endif

#ifdef CLUSTERO
CCluster::CCluster(const ObjectCollection &contents, int itemsPerColumn,
const char *name, AlignFlag a, BorderFlag f)
: HCluster(name,a,f), nitems(itemsPerColumn)
{
build(contents);
}
#endif

#ifdef CLUSTERP
CCluster::CCluster(const ObjectCollection &contents, int itemsPerColumn,
AlignFlag a,BorderFlag f)
: HCluster("HCluster",a,f), nitems(itemsPerColumn)
{
build(contents);
}
#endif

#ifdef CLUSTERQ
void CCluster::build(const ObjectCollection &contents)
{
ObjectIterator next(contents);
VObject *v;
VCluster *c = 0;

ASSERT(nitems > 0);
for(int i = nitems; (v = next()) != 0; i++)
{
if(i == nitems)
{
if(c)
HCluster::add1(c);
c = new VCluster;
i = 0;
}
c->add1(v);
}
if(c)
HCluster::add1(c);
realign();
}
#endif

#ifdef CLUSTERR
void CCluster::add1(VObject *v)
{
VCluster *c = (VCluster *)(objects.last());

if(c && strcmp(c->name(),"VCluster"))
HCluster::add1(v);
else
{
if(!c || c->count() == nitems)
{
c = new VCluster;
HCluster::add1(c);
}
c->add1(v);
}
}

void CCluster::realign(int resize)
{
ObjectIterator next(objects);
VCluster *c;

while((c = (VCluster *)next()) != 0)
if(!strcmp(c->name(),"VCluster"))
c->realign(resize);
HCluster::realign(resize);
}
#endif

#ifdef CLUSTERS
int CCluster::remove1(VObject *v)
{
ObjectIterator next(objects);
VCluster *c;

while((c = (VCluster *)next()) != 0)
if(!strcmp(c->name(),"VCluster"))
if(c->remove1(v))
return 1;
return 0;
}
#endif

#ifdef CLUSTERT
RCluster::RCluster(int itemsPerRow, const char *name,AlignFlag a,
BorderFlag f)
: VCluster(name,a,f), nitems(itemsPerRow)
{
}
#endif

#ifdef CLUSTERU
RCluster::RCluster(const ObjectCollection &contents, int itemsPerRow,
const char *name, AlignFlag a,BorderFlag f)
: VCluster(name,a,f), nitems(itemsPerRow)
{
build(contents);
}
#endif

#ifdef CLUSTERV
RCluster::RCluster(const ObjectCollection &contents, int itemsPerRow,
AlignFlag a,BorderFlag f)
: VCluster("VCluster",a,f), nitems(itemsPerRow)
{
build(contents);
}
#endif

#ifdef CLUSTERW
void RCluster::build(const ObjectCollection &contents)
{
ObjectIterator next(contents);
VObject *v;
HCluster *c = 0;

ASSERT(nitems > 0);
for(int i = nitems; (v = next()) != 0; i++)
{
if(i == nitems)
{
if(c)
VCluster::add1(c);
c = new HCluster;
i = 0;
}
c->add1(v);
}
if(c)
VCluster::add1(c);
realign();
}
#endif

#ifdef CLUSTERX
void RCluster::add1(VObject *v)
{
HCluster *c = (HCluster *)(objects.last());

if(c && strcmp(c->name(),"HCluster"))
VCluster::add1(v);
else
{
if(!c || c->count() == nitems)
{
c = new HCluster;
VCluster::add1(c);
}
c->add1(v);
}
}

void RCluster::realign(int resize)
{
ObjectIterator next(objects);
HCluster *c;

while((c = (HCluster *)next()) != 0)
if(!strcmp(c->name(),"HCluster"))
c->realign(resize);
VCluster::realign(resize);
}
#endif

#ifdef CLUSTERY
int RCluster::remove1(VObject *v)
{
ObjectIterator next(objects);
HCluster *c;

while((c = (HCluster *)next()) != 0)
if(!strcmp(c->name(),"HCluster"))
if(c->remove1(v))
return 1;
return 0;
}
#endif