UPLOAD - DOSUIT07.ZIP - COLSET.CPP

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

#ifdef TEST_COLSET
#define ALL
#endif
#ifdef ALL
#define COLSETA
#define COLSETB
#define COLSETC
#endif
#ifndef COLSETB
#define LIBRARY_MODULE
#endif

#include "colset.hpp"
#include "dialog.hpp"
#include "applic.hpp"
#include "event.hpp"
#include "panel.hpp"
#include "menu.hpp"

#ifdef COLSETA
ColoredButton::ColoredButton(color &color, const char *name, Command *cmd)
: RadioButton(name), col(color), command(cmd)
{
}

int ColoredButton::setpressed()
{
if(!RadioButton::setpressed())
return 0;
if(command)
command->doit(name());
return 1;
}

void ColoredButton::paint(Rectangle r)
{
r &= *this;
ASSERT(window);
window->setCurrentColor(col);
window->fillrect(r,-1,' ');

Point p = topleft();

window->printstring(p,r,getcolor(),pressed() ? RadioOn : RadioOff);
p.x++;
window->printstring(p,r,getcolor()," ");
p.x++;
window->setCurrentColor(col);
paintWithoutTilde(window,name(),-1,p,r);
}
#endif

#ifdef COLSETB
const int FGColCount = 16, BGColCount = 8;

color allFGColors[FGColCount] = {
Color(Black,White), Color(Blue,Black), Color(Green,Black), Color(Cyan,Black),
Color(Red,Black), Color(Magenta,Black), Color(Brown,Black), Color(White,Black),
Color(Gray,Black), Color(LightBlue,Black), Color(LightGreen,Black), Color(LightCyan,Black),
Color(LightRed,Black), Color(LightMagenta,Black), Color(Yellow,Black), Color(BrightWhite,Black),
};

color allBGColors[BGColCount] = {
Color(White,Black), Color(Black,Blue), Color(Black,Green), Color(Black,Cyan),
Color(Black,Red), Color(Black,Magenta), Color(Black,Brown), Color(Black,White)
};

const char * const colorNames[FGColCount] = {
"Black", "Blue", "Green", "Cyan",
"Red", "Magenta", "Brown", "White",
"Gray", "LightBlue", "LightGreen", "LightCyan",
"LightRed", "LightMagenta", "Yellow", "BrightWhite"
};

implement(GCommand,ColorSelect)

ColorSelect::ColorSelect(color *c, VObject *e)
: HCluster("ColourSelect"), fgcol(-1), bgcol(-1), blink(0), col(0), example(0),
foreground(new RadioCCluster(fgcol,8,"Foreground",
BorderFlag(BFsingle|BFtitle|BFtabstop))),
background(new RadioVCluster(bgcol,"Background",
BorderFlag(BFsingle|BFtitle|BFtabstop)))
{
for(int i = 0; i < FGColCount; i++)
foreground->add(new ColoredButton(allFGColors[i], colorNames[i],
new GCommand(ColorSelect)(&ColorSelect::updateColor,this)));
for(i = 0; i < BGColCount; i++)
background->add(new ColoredButton(allFGColors[i], colorNames[i],
new GCommand(ColorSelect)(&ColorSelect::updateColor,this)));
add(ObjectCollection(2) + foreground + background);
newColor(c,e);
}

void ColorSelect::setFGCol(int newcol)
{
ASSERT(newcol >= -1 && newcol < FGColCount);
fgcol = newcol;
foreground->read();
}

void ColorSelect::setBGCol(int newcol)
{
ASSERT(newcol >= -1 && newcol < BGColCount);
bgcol = newcol;
background->read();
}

int ColorSelect::updateColor(const char *)
{
if(!col)
return 0;
foreground->write();
background->write();
*col = Color(Colors(fgcol), Colors(bgcol), blink);
if(example)
example->dirty();
return 1;
}

void ColorSelect::setColor(color c)
{
if(!col)
return;
blink = c & 0x80;
setFGCol(c & 0x0f);
setBGCol((c >> 4) & 0x07);
updateColor();
}

int ColorSelect::newColor(color *c, VObject *e)
{
example = e;
col = c;
if(c)
setColor(*c);
else
{
setFGCol(-1);
setBGCol(-1);
}
return 1;
}
#endif

#ifdef COLSETC
implement(GCommand,SetColorControl)

const char *const exampleNames[8] = {
"Normal", "Disabled", "Focus", "Disabled+Focus",
"Highlight", "Highlight+Disabled", "Highlight+Focus",
"Highlight+Disabled+Focus"
};

SetColorControl::SetColorControl(ColorSet &c)
: HCluster("SetColorControl"), colors(c), tempColors(c), state(0),
select(new ColorSelect),
pickstate(new RadioVCluster(state,"Attributes",
BorderFlag(BFsingle|BFtitle|BFtabstop)))
{
for(int i = 0; i < 8; i++)
{
examples[i] = new ColoredButton(tempColors.colors[i], exampleNames[i],
new GCommand(SetColorControl)(&SetColorControl::setState,this));
pickstate->add(examples[i]);
}
add(ObjectCollection(2) + select + pickstate);
}

int SetColorControl::setState(const char *o)
{
pickstate->write();

VObject *v = 0;
for(int i = 0; i < 8; i++)
if(o == examples[i]->name())
{
v = examples[i];
break;
}
select->newColor(&tempColors.colors[state],v);
return 1;
}

void SetColorControl::read()
{
tempColors = colors;
pickstate->read();
select->newColor(&tempColors.colors[state],examples[state]);
dirty();
}

int SetColorControl::writeback()
{
colors = tempColors;
return 1;
}

VObject *SetColorControl::inheritingVObject()
{
return this;
}

int setColorDialog(ColorSet &colors)
{
SetColorControl *setcol = new SetColorControl(colors);

ModalDialog *dlg = new ModalDialog(
new VCluster(
setcol,
new HCluster(ObjectCollection(3)
+ new CloseEditButton(setcol)
+ new ResetEditButton(setcol)
+ new CancelButton,
AFexpand,BorderFlag(BFsingle|BFtabstop)
),
EndList),
"Configure colors");
return dlg->enter();
}
#endif

#ifdef TEST_COLSET
Panel *mainPanel;
EventQueue eventQueue;
Application app;
extern ColorSet alertColors, helpColors;

int setDefaultColor(const char *)
{
while(!setColorDialog(defaultColors))
;
mainPanel->setcolors(defaultColors);
return 1;
}

int setAlertColor(const char *)
{
while(!setColorDialog(alertColors))
;
return 1;
}

int setHelpColor(const char *)
{
while(!setColorDialog(helpColors))
;
return 1;
}

main()
{
VObject::debug = fopen("colset.log","a");
mainPanel = new Panel(
new MenuBar(KF10,
OCollection(MenuButton)() + DefaultHelpMenu),
new HCluster(ObjectCollection(4)
+ new ActiveButton(
new FunctionCommand(setDefaultColor),"Default Colours")
+ new ActiveButton(
new FunctionCommand(setAlertColor),"Alert Colours")
+ new ActiveButton(
new FunctionCommand(setHelpColor),"Help Colours")
+ new CloseButton,
AFHexpand|AFVcenter
),
"Colour settings test");
mainPanel->show();
app.enter();
return 0;
}
#endif