home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 2
/
ctrom_ii_b.zip
/
ctrom_ii_b
/
PROGRAM
/
C
/
OMEGA2
/
OMBUTTON.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-25
|
11KB
|
445 lines
//
// *************************************************************************
// * *
// * OMEGA C++ Windowing Class Library *
// * ================================= *
// * *
// * Copyright 1991,92 Tom Clancy *
// * Submitted to the public domain, April 1992 *
// * *
// *************************************************************************
// * *
// * Push Button Methods *
// * *
// *************************************************************************
//
#include <string.h>
#include <dos.h>
#include "ombutton.hpp"
oButton::oButton() {
Nb=0;
Current=1;
Pressed=FALSE;
Picked=0;
Pushed=FALSE;
bShaddir=shBottomleft;
}
oButton::~oButton() {
register int i;
for(i=1; i<=Nb; i++) {
delete Button[i]->message;
delete Button[i];
}
}
void oButton::addbutton(char *mess, int x, int y, int Nfg, int Nbk, int Hfg, int Hbk, int HotFg) {
Nb++;
if(Nb>255)
Nb=255;
else {
Button[Nb]=new butts;
Button[Nb]->message = new char[strlen(mess)+1];
if(strlen(mess))
strcpy(Button[Nb]->message,mess);
else
Button[Nb]->message[0]='\0';
Button[Nb]->x=x;
Button[Nb]->y=y;
Button[Nb]->Nfg=Nfg;
Button[Nb]->Nbk=Nbk;
Button[Nb]->Hfg=Hfg;
Button[Nb]->Hbk=Hbk;
Button[Nb]->HotFg=HotFg;
addtohotlist(Nb);
}
}
void oButton::normwriteoption(int i) {
char s[80];
int pos;
register int j,k;
if(!strchr(Button[i]->message,'~'))
writeat(Button[i]->x,Button[i]->y,Button[i]->Nfg,Button[i]->Nbk,Button[i]->message);
else {
pos=strchr(Button[i]->message,'~')-Button[i]->message;
for(j=0; j<pos; j++)
s[j]=Button[i]->message[j];
k=j;
for(j=pos+1;j<strlen(Button[i]->message); j++,k++)
s[k]=Button[i]->message[j];
s[k]='\0';
writeat(Button[i]->x,Button[i]->y,Button[i]->Nfg,Button[i]->Nbk,s);
chwriteat(Button[i]->x+pos,Button[i]->y,Button[i]->HotFg,Button[i]->Nbk,Button[i]->message[pos+1]);
}
}
void oButton::highwriteoption(int i) {
char s[80];
int pos;
register int j,k;
if(!strchr(Button[i]->message,'~'))
writeat(Button[i]->x,Button[i]->y,Button[i]->Hfg,Button[i]->Hbk,Button[i]->message);
else {
pos=strchr(Button[i]->message,'~')-Button[i]->message;
for(j=0; j<pos; j++)
s[j]=Button[i]->message[j];
k=j;
for(j=pos+1;j<strlen(Button[i]->message); j++,k++)
s[k]=Button[i]->message[j];
s[k]='\0';
writeat(Button[i]->x,Button[i]->y,Button[i]->Hfg,Button[i]->Hbk,s);
chwriteat(Button[i]->x+pos,Button[i]->y,Button[i]->HotFg,Button[i]->Hbk,Button[i]->message[pos+1]);
}
}
void oButton::addtohotlist(int i) {
if(strchr(Button[i]->message,'~')) {
hotlist[i]=Button[i]->message[(strchr(Button[i]->message,'~')-Button[i]->message)+1];
hotlist[i]=makealt(hotlist[i]);
}
else
hotlist[i]=0;
}
void oButton::writebuttonshadow(int b) {
register int i,off=0;
if(strchr(Button[i]->message,'~'))
off=1;
switch(getshaddir()) {
case shTopright :
for(i=Button[b]->x+1; i<=Button[b]->x+strlen(Button[b]->message)-off; i++)
chwriteat(i,Button[b]->y-1,BLACK,getbkcolor(),220);
chwriteat(Button[b]->x+strlen(Button[b]->message)-off,Button[b]->y,BLACK,getbkcolor(),223);
break;
case shTopleft :
for(i=Button[b]->x-1; i<Button[b]->x+strlen(Button[b]->message)-off; i++)
chwriteat(i,Button[b]->y-1,BLACK,getbkcolor(),220);
chwriteat(Button[b]->x-1,Button[b]->y,BLACK,getbkcolor(),223);
break;
case shBottomright :
for(i=Button[b]->x+1; i<=Button[b]->x+strlen(Button[b]->message)-off; i++)
chwriteat(i,Button[b]->y+1,BLACK,getbkcolor(),223);
chwriteat(Button[b]->x+strlen(Button[b]->message)-off,Button[b]->y,BLACK,getbkcolor(),220);
break;
case shBottomleft :
for(i=Button[b]->x-1; i<Button[b]->x+strlen(Button[b]->message)-off; i++)
chwriteat(i,Button[b]->y+1,BLACK,getbkcolor(),223);
chwriteat(Button[b]->x-1+off,Button[b]->y,BLACK,getbkcolor(),220);
break;
}
showmouse();
}
void oButton::writebutton(int i) {
//writeat(Button[i]->x,Button[i]->y,Button[i]->Nfg,Button[i]->Nbk,Button[i]->message);
normwriteoption(i);
writebuttonshadow(i);
showmouse();
}
void oButton::highlight() {
//writeat(Button[Current]->x,Button[Current]->y,Button[Current]->Hfg,Button[Current]->Hbk,Button[Current]->message);
highwriteoption(Current);
showmouse();
}
void oButton::unhighlight() {
//writeat(Button[Current]->x,Button[Current]->y,Button[Current]->Nfg,Button[Current]->Nbk,Button[Current]->message);
normwriteoption(Current);
showmouse();
}
int oButton::withinbutton(int i) {
if(!Pushed)
return(buttonx()>=Button[i]->x && buttonx()<=Button[i]->x+strlen(Button[i]->message)-1 &&
buttony()==Button[i]->y);
else {
if(getshaddir()==shTopleft || getshaddir()==shBottomleft)
return(buttonx()>=Button[i]->x && buttonx()<=Button[i]->x+strlen(Button[i]->message) &&
buttony()==Button[i]->y);
else
return(buttonx()>=Button[i]->x-1 && buttonx()<=Button[i]->x+strlen(Button[i]->message)-1 &&
buttony()==Button[i]->y);
}
}
int oButton::onwhichbutton() {
register int i;
for(i=1; i<=Nb; i++)
if(withinbutton(i))
return i;
return 0;
}
void oButton::hidebutton(int b) {
int off=0;
if(strchr(Button[b]->message,'~'))
off=1;
switch(getshaddir()) {
case shTopleft :
fillarea(Button[b]->x-1,Button[b]->y-1,Button[b]->x+strlen(Button[b]->message)-1-off,Button[b]->y,getfgcolor(),getbkcolor(),32);
break;
case shBottomleft :
fillarea(Button[b]->x-1,Button[b]->y,Button[b]->x+strlen(Button[b]->message)-1-off,Button[b]->y+1,getfgcolor(),getbkcolor(),32);
break;
case shTopright :
fillarea(Button[b]->x,Button[b]->y-1,Button[b]->x+strlen(Button[b]->message)-off,Button[b]->y,getfgcolor(),getbkcolor(),32);
break;
case shBottomright :
fillarea(Button[b]->x,Button[b]->y,Button[b]->x+strlen(Button[b]->message)-off,Button[b]->y+1,getfgcolor(),getbkcolor(),32);
break;
}
}
void oButton::pressbutton(int b) {
if(!Pressed) {
Pressed=TRUE;
hidebutton(b);
if(getshaddir()==shTopright || getshaddir()==shBottomright)
Button[b]->x+=1;
else
Button[b]->x-=1;
highwriteoption(b);
}
}
void oButton::unpressbutton(int b) {
if(Pressed) {
Pressed=FALSE;
hidebutton(b);
if(getshaddir()==shTopright || getshaddir()==shBottomright)
Button[b]->x-=1;
else
Button[b]->x+=1;
highwriteoption(b);
writebuttonshadow(b);
}
}
int oButton::pushbutton() {
int ox=buttonx();
int oy=buttony();
int ob;
int xx,yy;
int sx,sy;
int bb=buttonb();
int b=onwhichbutton();
if(b) {
while(bb==1) {
if(withinbutton(b)) {
Picked=0;
unhighlight();
Current=b;
highlight();
hidebutton(b);
if(getshaddir()==shTopright || getshaddir()==shBottomright) {
Pushed=TRUE;
mouse::getmouse(ox,oy,ob);
sx=ox;
sy=oy;
pressbutton(b);
mouse::setmouse(ox+1,oy);
}
else {
Pushed=TRUE;
mouse::getmouse(ox,oy,ob);
sx=ox;
sy=oy;
pressbutton(b);
mouse::setmouse(ox-1,oy);
}
}
getmouse(xx,yy,bb);
ox=xx;
oy=yy;
ob=bb;
setbuttonx(xx);
setbuttony(yy);
while(withinbutton(b) && bb==1) {
getmouse(xx,yy,bb);
setbuttonx(xx);
setbuttony(yy);
showmouse();
}
if(bb && !withinbutton(b)) {
Pushed=FALSE;
unpressbutton(b);
highlight();
while(!withinbutton(b) && bb) {
getmouse(xx,yy,bb);
setbuttonx(xx);
setbuttony(yy);
showmouse();
}
}
}
unpressbutton(b);
if(withinbutton(b)) {
setmouse(sx,sy);
Picked=b;
}
}
return Picked;
}
void oButton::display_buttons() {
register int i;
if(Nb) {
for(i=1; i<=Nb; i++)
writebutton(i);
highlight();
}
}
void oButton::display_info() {
Picked=0;
display_buttons();
}
void oButton::gonextbutton() {
if(Nb) {
unhighlight();
Current++;
if(Current>Nb)
Current=1;
highlight();
}
}
void oButton::goprevbutton() {
if(Nb) {
unhighlight();
Current--;
if(Current<1)
Current=Nb;
highlight();
}
}
void oButton::setcurrent(int c) {
if(c && c<=Nb) {
unhighlight();
Current=c;
highlight();
}
}
int oButton::handle_mouse_event() {
if(pushbutton())
return cmdPushedButton;
else
return cmdNocmd;
}
int oButton::handle_key_event() {
int r=getkeycode();
switch(r) {
case 27 :
Picked=0;
return cmdClose;
break;
case RightArrow :
gonextbutton();
return cmdNextButton;
break;
case LeftArrow :
goprevbutton();
return cmdPrevButton;
break;
case UpArrow :
goprevbutton();
return cmdPrevButton;
break;
case DownArrow:
gonextbutton();
return cmdNextButton;
break;
case 32 : // Space Bar
if(Nb) {
Picked=Current;
pressbutton(Current);
delay(100);
unpressbutton(Current);
return cmdPushedButton;
}
break;
case Enter :
if(Nb) {
Picked=Current;
pressbutton(Current);
delay(100);
unpressbutton(Current);
return cmdPushedButton;
}
break;
default:
int i;
for(i=1;(i<=Nb) && r!=hotlist[i]; i++);
if(r==hotlist[i]) {
unhighlight();
Current=i;
pressbutton(Current);
delay(100);
unpressbutton(Current);
highlight();
Picked=Current;
return cmdPushedButton;
}
break;
}
return cmdNocmd;
}
void oButton::run() {
while(getlastcmd()!=cmdClose)
handle_events();
}