/* MyGui.h */
#ifndef __MYGUI_H
#define __MYGUI_H
#include "MyGui.cpp"
extern void initGraphDrv(void);
extern void gettextcenterpos(int,int,int,int,char *,int *,int *) /* 这个自己加一下,这个我在写帖子的时候加的,呵呵不好意思 */
#endif
/* MyGui.cpp */
#define dx 1
#define dy 1
#define SCREEN_WIDTH
640
#define SCREEN_HEIGHT 480
#define MAX_BORDER_WIDTH
10
#define DEFAULT_REGION
50,50,550,400
#define DEFAULT_ANIMATION_DELAY
500
#define DEFAULT_BORDER_WIDTH
2
#define DEFAULT_FILL_STYLE
SOLID_FILL
#define DEFAULT_BC_UPPER
WHITE
#define DEFAULT_BC_LOWER
DARKGRAY
#define DEFAULT_BGCOLOR
LIGHTGRAY
#define DEFAULT_TITLE_BGCOLOR
BLUE
#define DEFAULT_TEXT_COLOR
WHITE
#define DEFAULT_TITLE_HEIGHT
20
#ifndef DATATYPE_BOOL
#define DATATYPE_BOOL
typedef enum {FALSE=0,TRUE} BOOL;
#endif
void initGraphDrv(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
/* return with error code */
}
}
void gettextcenterpos(int bx,int by,int w,int h,char *text,int *x,int *y)
{
*x=(w-textwidth(text))/2+bx;
*y=(h-textheight(text))/2+by;
}
class Region
{
public:
/* Construction */
Region();
Region(int,int,int,int);
/* Member functions */
BOOL create_region(int,int,int,int);
int getleft(void);
int gettop(void);
int getright(void);
int getbottom(void);
void setleft(int);
void settop(int);
void setright(int);
void setbottom(int);
protected:
int left;
int top;
int right;
int bottom;
};
class Rect : public Region
{
public:
/* Construction */
Rect();
Rect(int,int,int,int);
/* Member functions */
BOOL adjust_region(int,int,int,int);
BOOL set_border_width(int);
BOOL set_border_colors(int,int);
BOOL set_bgcolor(int);
BOOL set_fill_style(int);
BOOL set_animation_delay(int);
int get_border_width(void);
void paint(BOOL,BOOL);
void animation(void);
private:
int border_width;
int border_color_upper;
int border_color_lower;
int bgcolor;
int fs;
int animation_delay;
};
class objWin
{
public:
/* Construction */
objWin();
objWin(int,int,int,int);
/* Settings for objWin */
BOOL set_caption(char *);
BOOL set_caption_color(int);
BOOL set_body_colors(int,int,int);
BOOL set_title_colors(int,int,int);
BOOL set_body_border_width(int);
BOOL set_title_border_width(int);
BOOL set_body_fill_style(int);
BOOL set_title_fill_style(int);
/* ReadOnly properties */
int getwidth(void);
int getheight(void);
char *getcaption(void);
BOOL adjust_size(int,int);
BOOL move(int,int);
BOOL isVisible(void);
/* Methods */
void hide(void);
void show(void);
void paint(void);
/* Events */
virtual void onHide(void)
{};
virtual void onShow(void)
{};
virtual void onPaint(void)
{};
virtual void onMove(void)
{};
virtual void onAdjust(void) {};
/* Destruction */
~objWin();
private:
Rect body;
Rect title;
IFile image;
char *caption;
int caption_color;
BOOL Visible;
};
Region::Region()
{
/* Initalizing variable table */
left=100;
top=100;
right=300;
bottom=300;
}
Region::Region(int x1,int y1,int x2,int y2)
{
if(x1>=x2||y1>=y2||x1<=0||y1<=0||x2<=0||y2<=0)
{
left=100;
top=100;
right=300;
bottom=300;
}
else
{
left=x1;
top=y1;
right=x2;
bottom=y2;
}
}
BOOL Region::create_region(int x1,int y1,int x2,int y2)
{
if(x1>=x2||y1>=y2||x1<0||y1<0||x2<0||y2<0) return FALSE;
left=x1;
top=y1;
right=x2;
bottom=y2;
return TRUE;
}
int Region::getleft(void)
{
return left;
}
int Region::gettop(void)
{
return top;
}
int Region::getright(void)
{
return right;
}
int Region::getbottom(void)
{
return bottom;
}
void Region::setleft(int value)
{
if(value>=0&&value<SCREEN_WIDTH) left=value;
}
void Region::settop(int value)
{
if(value>=0&&value<SCREEN_HEIGHT) top=value;
}
void Region::setright(int value)
{
if(value>0&&value>left&&value<SCREEN_WIDTH) right=value;
}
void Region::setbottom(int value)
{
if(value>0&&value>top&&value<SCREEN_HEIGHT) bottom=value;
}
Rect::Rect()
{
create_region(DEFAULT_REGION);
border_width=DEFAULT_BORDER_WIDTH;
border_color_upper=DEFAULT_BC_UPPER;
border_color_lower=DEFAULT_BC_LOWER;
bgcolor=DEFAULT_BGCOLOR;
fs=DEFAULT_FILL_STYLE;
animation_delay=DEFAULT_ANIMATION_DELAY;
}
Rect::Rect(int x1,int y1,int x2,int y2)
{
if(!create_region(x1,y1,x2,y2)) create_region(DEFAULT_REGION);
border_width=DEFAULT_BORDER_WIDTH;
border_color_upper=DEFAULT_BC_UPPER;
border_color_lower=DEFAULT_BC_LOWER;
bgcolor=DEFAULT_BGCOLOR;
fs=DEFAULT_FILL_STYLE;
animation_delay=DEFAULT_ANIMATION_DELAY;
}
BOOL Rect::adjust_region(int x1,int y1,int x2,int y2)
{
return create_region(x1,y1,x2,y2);
}
BOOL Rect::set_border_width(int bw)
{
if(bw<=0||bw>MAX_BORDER_WIDTH) return FALSE;
border_width=bw;
return TRUE;
}
BOOL Rect::set_border_colors(int bc_upper,int bc_lower)
{
if(bc_upper<0||bc_lower<0) return FALSE;
border_color_upper=bc_upper;
border_color_lower=bc_lower;
return TRUE;
}
BOOL Rect::set_bgcolor(int bgc)
{
if(bgc<0) return FALSE;
bgcolor=bgc;
return TRUE;
}
BOOL Rect::set_fill_style(int fstyle)
{
if(fstyle<0) return FALSE;
fs=fstyle;
return TRUE;
}
BOOL Rect::set_animation_delay(int mi_second)
{
if(mi_second<=0) return FALSE;
animation_delay=mi_second;
return TRUE;
}
int Rect::get_border_width(void)
{
return border_width;
}
void Rect::paint(BOOL transparent,BOOL border_inverse)
{
register int i;
int bleft=left,btop=top,bright=right,bbottom=bottom;
int bkpen_color=getcolor();
for(i=0;i<border_width;i++)
{
if(!border_inverse) setcolor(border_color_upper);
else setcolor(border_color_lower);
line(bleft,btop,bleft,bbottom);
line(bleft,btop,bright,btop);
if(!border_inverse) setcolor(border_color_lower);
else setcolor(border_color_upper);
line(bright,btop,bright,bbottom);
line(bleft,bbottom,bright,bbottom);
bleft+=dx;
bright-=dx;
btop+=dy;
bbottom-=dy;
}
if(!transparent)
{
setfillstyle(fs,bgcolor);
bar(bleft,btop,bright,bbottom);
}
setcolor(bkpen_color);
}
void Rect::animation(void)
{
paint(TRUE,TRUE);
delay(animation_delay);
paint(TRUE,FALSE);
}
objWin::objWin()
{
int x_redirect=body.get_border_width()*dx;
int y_redirect=body.get_border_width()*dy;
title.set_bgcolor(DEFAULT_TITLE_BGCOLOR);
title.set_border_colors(DEFAULT_BC_LOWER,DEFAULT_BC_UPPER);
title.set_border_width(1);
title.adjust_region(body.getleft()+x_redirect+2*dx,body.gettop()+y_redirect+2*dy,
body.getright()-x_redirect-2*dx,body.gettop()+y_redirect+2*dy+DEFAULT_TITLE_HEIGHT);
caption=NULL;
caption_color=DEFAULT_TEXT_COLOR;
Visible=FALSE;
}
objWin::objWin(int x1,int y1,int x2,int y2)
{
int x_redirect=body.get_border_width()*dx;
int y_redirect=body.get_border_width()*dy;
body.adjust_region(x1,y1,x2,y2);
title.set_bgcolor(DEFAULT_TITLE_BGCOLOR);
title.set_border_colors(DEFAULT_BC_LOWER,DEFAULT_BC_UPPER);
title.set_border_width(1);
title.adjust_region(body.getleft()+x_redirect+2*dx,body.gettop()+y_redirect+2*dy,
body.getright()-x_redirect-2*dx,body.gettop()+y_redirect+2*dy+DEFAULT_TITLE_HEIGHT);
caption=NULL;
caption_color=DEFAULT_TEXT_COLOR;
Visible=FALSE;
}
BOOL objWin::set_caption(char *s)
{
if(s)
{
if(caption)
{
free(caption);
caption=NULL;
}
caption=(char *)malloc((strlen(s)+1)*sizeof(char));
if(!caption)
{
printf("Cannot allocate memory for string!\n");
exit(1);
}
strcpy(caption,s);
return TRUE;
}
return FALSE;
}
BOOL objWin::set_caption_color(int c)
{
if(c<0) return FALSE;
caption_color=c;
return TRUE;
}
BOOL objWin::set_body_colors(int bbc_upper,int bbc_lower,int bbgcolor)
{
if(bbc_upper<0||bbc_lower<0||bbgcolor<0) return FALSE;
body.set_border_colors(bbc_upper,bbc_lower);
body.set_bgcolor(bbgcolor);
return TRUE;
}
BOOL objWin::set_title_colors(int tbc_upper,int tbc_lower,int tbgcolor)
{
if(tbc_upper<0||tbc_lower<0||tbgcolor<0) return FALSE;
title.set_border_colors(tbc_upper,tbc_lower);
title.set_bgcolor(tbgcolor);
return FALSE;
}
BOOL objWin::set_body_border_width(int bbw)
{
if(bbw<=0||bbw>MAX_BORDER_WIDTH) return FALSE;
body.set_border_width(bbw);
return TRUE;
}
BOOL objWin::set_title_border_width(int tbw)
{
if(tbw<=0||tbw>MAX_BORDER_WIDTH) return FALSE;
title.set_border_width(tbw);
return TRUE;
}
BOOL objWin::set_body_fill_style(int bfs)
{
if(bfs<0) return FALSE;
body.set_fill_style(bfs);
return TRUE;
}
BOOL objWin::set_title_fill_style(int tfs)
{
if(tfs<0) return FALSE;
title.set_fill_style(tfs);
return TRUE;
}
char *objWin::getcaption(void)
{
return caption;
}
int objWin::getwidth(void)
{
return body.getright()-body.getleft();
}
int objWin::getheight(void)
{
return body.getbottom()-body.gettop();
}
BOOL objWin::isVisible(void)
{
return Visible;
}
BOOL objWin::adjust_size(int w,int h)
{
int x_redirect=body.get_border_width()*dx;
int y_redirect=body.get_border_width()*dy;
if(w-body.getleft()>SCREEN_WIDTH||w-body.getleft()<0||h-body.gettop()>SCREEN_HEIGHT||h-body.gettop()<0)
return FALSE;
body.setright(w-body.getleft());
body.setbottom(h-body.gettop());
title.adjust_region(body.getleft()+x_redirect+2*dx,body.gettop()+y_redirect+2*dy,
body.getright()-x_redirect-2*dx,body.gettop()+y_redirect+2*dy+DEFAULT_TITLE_HEIGHT);
if(Visible)
{
image.put_region();
Visible=FALSE;
paint();
}
onAdjust();
return TRUE;
}
BOOL objWin::move(int x,int y)
{
int x_redirect=body.get_border_width()*dx;
int y_redirect=body.get_border_width()*dy;
if(x>SCREEN_WIDTH||x<0||y>SCREEN_HEIGHT||y<0) return FALSE;
if(getwidth()-x>SCREEN_WIDTH||getwidth()-x<0||getheight()-y>SCREEN_HEIGHT||getheight()-y<0)
return FALSE;
body.setleft(x);
body.setright(y);
title.adjust_region(body.getleft()+x_redirect+2*dx,body.gettop()+y_redirect+2*dy,
body.getright()-x_redirect-2*dx,body.gettop()+y_redirect+2*dy+DEFAULT_TITLE_HEIGHT);
if(Visible)
{
image.put_region();
Visible=FALSE;
paint();
}
onMove();
return TRUE;
}
void objWin::paint(void)
{
if(!Visible)
{
int textx=0,texty=0;
int tw=title.getright()-title.getleft(),th=title.getbottom()-title.gettop();
int bkpen_color=getcolor();
image.clear();
image.get_region(body.getleft(),body.gettop(),body.getright(),body.getbottom());
body.paint(FALSE,FALSE);
title.paint(FALSE,FALSE);
gettextcenterpos(title.getleft(),title.gettop(),tw,th,caption,&textx,&texty);
setcolor(caption_color);
outtextxy(textx,texty,caption);
setcolor(bkpen_color);
onPaint();
Visible=TRUE;
}
}
void objWin::hide(void)
{
if(Visible)
{
image.put_region();
Visible=FALSE;
onHide();
}
}
void objWin::show(void)
{
if(!Visible)
{
paint();
onShow();
}
}
objWin::~objWin()
{
if(caption)
{
free(caption);
caption=NULL;
}
}