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

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

#ifdef ALL
#define BUTTONA
#define BUTTONB
#define BUTTONC
#define BUTTOND
#define BUTTONE
#define BUTTONF
#define BUTTONG
#define BUTTONH
#define BUTTONI
#define BUTTONJ
#define BUTTONK
#define BUTTONL
#define BUTTONM
#define BUTTONN
#define BUTTONO
#define BUTTONP
#define BUTTONQ
#define BUTTONR
#endif
#ifndef BUTTONA
#define LIBRARY_MODULE
#endif

/*
* Buttons - Active, Check, Radio
*/

#include
#include
#include
#include "button.hpp"
#include "window.hpp"
#include "event.hpp"


// Constructors & Destructor
#ifdef BUTTONA
Button::Button(const char *n,Keys hotkey,int on)
: Control(lenWithoutTilde(n),1,n), hot(hotkey), controller(0),
quick(checkForTilde(n))
{
setFlag(SFshowfocus);
if(on)
press();
}
#endif

#ifdef BUTTONB
Button::Button(const coord w,const coord h,const char *n,Keys hotkey,int on)
: Control(w,h,n), hot(hotkey), controller(0),
quick(checkForTilde(n))
{
setFlag(SFshowfocus);
if(on)

press();
}
#endif

#ifdef BUTTONC
implement(OCollection,Button)

// User has clicked on button - take any necessary action and return result
int Button::setpressed()
{
if(testFlag(SFdisabled))
return 0;
#ifndef NDEBUG
if(debug)
fprintf(debug,"%s->setpressed()\n",debuginfo());
#endif
press();
if(controller)
controller->buttonPressed(this);
return 1;
}

int Button::leftdown(MouseEvent&)
{
setFlag(SFhighlight);
setcFlag(CFdirty);
setmoufocus();
return 1;
}

int Button::leftup(MouseEvent&)
{
int t = testFlag(SFhighlight);

resetmoufocus();
if(t)
{
clearFlag(SFhighlight);
setfocus();
setpressed();
}
return 1;
}

int Button::mouseenter(MouseEvent& e)
{
if(e.buttons() & MBleft)
{
highlight();
return 0;
}
else
return Control::mouseenter(e);
}

int Button::mouseexit(MouseEvent& e)
{
if(e.buttons() & MBleft)
{
unhighlight();
return 0;
}
else
return Control::mouseexit(e);
}

int Button::dohotkey(KeyboardEvent& e)
{
if(e.value() == hot)
{
#ifndef NDEBUG
if(debug)
fprintf(debug,"%s->hotkey(%s)\n",debuginfo(),keyname(hot));
#endif
setpressed();
return 1;
}
return 0;
}

void paintWithoutTilde(Window *w,const char *s,int color,Point p,
const Rectangle &clip)
{
const char *c = strchr(s,'~');
int l = (c && c[1]) ? (c - s) : strlen(s);

if(l)
{
w->printstring(p,clip,color,s,l);
p.x += l;
}
if(c)
{
w->printstring(p,clip,color^SFhighlight,c+1,1);
p.x++;
if(c[2])
w->printstring(p,clip,color,c+2);
}
}

int lenWithoutTilde(const char *s)
{
int l = strlen(s);

if(strchr(s,'~'))
l--;
return l;
}

int checkForTilde(const char *s)
{
s = strchr(s,'~');
return s ? toupper(s[1]) : 0;
}

#ifdef NINSPECT
char *Button::inspectinfo(char *buf,const char *s) const
{
return "";
}
#else
char *Button::inspectinfo(char *buf,const char *s) const
{
if(!s)
s = "Button ";
return Control::inspectinfo(buf,s);
}
#endif
#ifdef NDEBUG
char *Button::debuginfo(char *buf) const
{
return buf;
}
#else
char *Button::debuginfo(char *buf) const
{
if(!buf)
{
buf = Control::debugbuf;
*buf = 0;
}
strcat(buf,"Button");
return Control::debuginfo(buf);
}
#endif
#endif

#ifdef BUTTOND
ButtonController::ButtonController(int hintSize)
: btns(hintSize)
{
}

ButtonController::ButtonController(const OCollection(Button)& c)
: btns(c)
{
setController(btns);
}

void ButtonController::setController(Button *b)
{
b->controller = this;
}

void ButtonController::setController(const OCollection(Button)& c)
{
OIterator(Button) next(c);
Button *b;

while((b = next()) != 0)
setController(b);
}

int ButtonController::buttonPressed(Button *)
{
return 1;
}

int ButtonController::kbdevent(KeyboardEvent& e)
{
unsigned int k = e.value();

if(k > 255)
return 0;

OIterator(Button) next(btns);
Button *b;

k = toupper(k);
while((b = next()) != 0)
if(b->quickkey() == k)
{
if(b->setpressed())
{
b->setfocus();
return 1;
}
return 0;
}
return 0;
}

OneButtonController::OneButtonController(int hintSize)
: ButtonController(hintSize), theone(0)
{
}

OneButtonController::OneButtonController(const OCollection(Button) &c,
int whichone)
: ButtonController(c), theone(btns[whichone])
{
}

int OneButtonController::buttonPressed(Button *b)
{
if(b != theone)
{
if(btns.elementNo(b) < 0)
return 0;
if(theone)
theone->unpress();
theone = b;
}
return 1;
}
#endif

#ifdef BUTTONE
ActionButton::ActionButton(Command *cmd,const char *n,
Keys hotkey)
: Button(n,hotkey), command(cmd)
{
}

ActionButton::ActionButton(coord width,coord height,
Command *cmd,const char *n,Keys hotkey)
: Button(width,height,n,hotkey), command(cmd)
{
}
#endif

#ifdef BUTTONF
ActionButton::~ActionButton()
{
if(command)
delete command;
}

int ActionButton::setpressed()
{
int r;

if(!Button::setpressed())
return 0;
if(pressed())
{
r = doit();
unpress();
}
return r;
}

int ActionButton::doit()
{
if(command)
return command->doit(name());
return 1;
}

int ActionButton::kbdevent(KeyboardEvent& e)
{
int r;

switch(e.value())
{
case '\n':
case '\r':
if(!testFlag(SFdisabled))
{
setFlag(SFhighlight);
draw();
setpressed();
r = 1;
unhighlight();
}
break;
default:
r = Control::kbdevent(e);
}
return r;
}

coord ActionButton::minwidth() const
{
return lenWithoutTilde(name());
}

void ActionButton::paint(Rectangle r)
{
Button::paint(r);
paintWithoutTilde(window,name(),getcolor(),topleft(),r);
}
#endif

#ifdef BUTTONG
ActiveButton::ActiveButton(Command *cmd,const char *n,
Keys hotkey,ButtonDefault makedefault)
: ActionButton(lenWithoutTilde(n)+2,3,cmd,n,hotkey), def(makedefault)
{
}
#endif

#ifdef BUTTONH
ActiveButton::ActiveButton(const coord w,const coord h,
Command *cmd,const char *n,Keys hotkey,
ButtonDefault makedefault)
: ActionButton(w+2,h+2,cmd,n,hotkey), def(makedefault)
{
}
#endif

#ifdef BUTTONI
void ActiveButton::makedefault(ButtonDefault d)
{
def = d;
if(window)
{
if(d)
window->setactive(this);
else
window->clearactive(this);
}
}
#endif

#ifdef BUTTONJ
void ActiveButton::setwindow(Window *w)
{
ActionButton::setwindow(w);
if(def && w)
w->setactive(this);
}

coord ActiveButton::minwidth() const
{
return ActionButton::minwidth()+2;
}

coord ActiveButton::minheight() const
{
return 3;
}

int ActiveButton::setfocus(int tabstop)
{
if(!Button::setfocus(tabstop))
return 0;
if(window)
window->setactive(this);
return 1;
}

void ActiveButton::paint(Rectangle r)
{
Control::paint(r);
window->drawbox(*this,r,getcolor(),
testFlag(SFactfocus) ? BFdouble : BFsingle);

Point p = topleft();

p += 1;
paintWithoutTilde(window,name(),getcolor(),p,r);
}
#endif

#ifdef BUTTONK
// Constructors & Destructor
CloseButton::CloseButton(const char *n,Keys hotkey,Command *cmd,
ButtonDefault makedefault)
: ActiveButton(cmd,n,hotkey,makedefault)
{
}
#endif

#ifdef BUTTONL
CloseButton::CloseButton(Command *cmd,ButtonDefault makedefault,
const char *n)
: ActiveButton(cmd,n,Knone,makedefault)
{
}
#endif

#ifdef BUTTONM
int CloseButton::doit()
{
return (ActiveButton::doit() && window->closeok());
}
#endif

#ifdef BUTTONN
CancelButton::CancelButton(const char *n,Keys hotkey,
Command *cmd,ButtonDefault makedefault)
: ActiveButton(cmd,n,hotkey,makedefault)
{
}
#endif

#ifdef BUTTONO
CancelButton::CancelButton(Command *cmd,ButtonDefault makedefault,
const char *n)
: ActiveButton(cmd,n,Kesc,makedefault)
{
}
#endif

#ifdef BUTTONP
int CancelButton::doit()
{
return (ActiveButton::doit() && window->cancelok());
}

#endif

#ifdef BUTTONQ
ButtonHCluster::ButtonHCluster(const char *name, AlignFlag a, BorderFlag f)
: HCluster(name,a,f)
{
}

ButtonHCluster::ButtonHCluster(const OCollection(Button)& buttons,
const char *name, AlignFlag a, BorderFlag f)
: HCluster(buttons,name,a,f), ButtonController(buttons)
{
}

ButtonHCluster::ButtonHCluster(const OCollection(Button)& buttons,
AlignFlag a, BorderFlag f)
: HCluster(buttons,"ButtonHCluster",a,f), ButtonController(buttons)
{
}

void ButtonHCluster::add(Button *btn)
{
ButtonController::add(btn);
HCluster::add(btn);
}

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

if(!r)
r = ButtonController::kbdevent(e);
return r;
}

#endif

#ifdef BUTTONR
ButtonVCluster::ButtonVCluster(const char *name, AlignFlag a, BorderFlag f)
: VCluster(name,a,f)
{
}

ButtonVCluster::ButtonVCluster(const OCollection(Button)& buttons,
const char *name, AlignFlag a, BorderFlag f)
: VCluster(buttons,name,a,f), ButtonController(buttons)
{
}

ButtonVCluster::ButtonVCluster(const OCollection(Button)& buttons,
AlignFlag a, BorderFlag f)
: VCluster(buttons,"ButtonVCluster",a,f), ButtonController(buttons)
{
}

void ButtonVCluster::add(Button *btn)
{
ButtonController::add(btn);
VCluster::add(btn);
}

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

if(!r)
r = ButtonController::kbdevent(e);
return r;
}

#endif