| Close | Back |
/*
* pb.c
*
*****************************************************************************
*
* Pushbuttons for C in graphics mode
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*****************************************************************************
*
* Author : Rajesh _ThE gReAt
* Built on : 08/04/2001
* Last Modified on : 08/04/2001
*
* Known errors : The functions in the file use standard
* library functions like 'setcolor' 'getcolor'
* 'setbkcolor' and some function might have forgotten
* to reset the original value . Though much care is
* taken to avoid this by the user it just might be
* there . So double check your drawing colors after
* you have called any fuction in PB.C
*
*****************************************************************************
* DISCLAIMER:
*****************************************************************************
*
* Programmers may incorporate any or all code into their programs,
* giving proper credit within the source. Publication of the
* source routines is permitted so long as proper credit is given
* to Rajesh _ThE gReAt.
*
* Copyright (C) 2001, 2002 by Rajesh _ThE gReAt. You may use this program, or
* code or tables extracted from it, as desired without restriction.
* I can not and will not be held responsible for any damage caused from
* the use of this software.
*
*****************************************************************************
* This source works with Turbo C 2.0 and Turbo C++ 3.0 and above.
*****************************************************************************
* Rajesh _ThE gReAt is a synonym of Rajesh Goli
* Source freely distributable
*****************************************************************************/
#ifndef __GRAPHICS_H
#include<graphics.h>
#endif
#ifndef __STRING_H
#include<string.h>
#endif
#ifndef NOOFPBS
#define NOOFPBS 128
#endif
#define DEFAULT -2
#define DEFAULT_FONT GOTHIC_FONT
#define DEFAULT_COLOR LIGHTGRAY
#define DEFAULT_TOP_COLOR WHITE
#define DEFAULT_BOTTOM_COLOR BLACK
#define DEFAULT_TEXT_COLOR WHITE
#define SIZE 2
#define MAX_CHARS 128
#define ADDITIONALH_SPACES 2
#define ADDITIONALV_SPACES 6
#ifdef DEBUG
#include<stdio.h>
#endif
typedef struct
{
signed top; /* Cant be DEFAULT*/
signed left; /* Ditto*/
signed right; /* Can be DEFAULT*/
signed bottom; /* Ditto*/
}
PBDIMENSION;
typedef struct
{
/* These can either be a color (Enum or int) or DEFAULT */
signed color; /*Color of body of PB*/
signed topbor; /*Top Border Color*/
signed botbor; /*Bottom Border color*/
signed text; /*Color of text*/
}
PBCOLOR;
typedef struct
{
PBDIMENSION dim; /*Dimemsions of Push Button */
PBCOLOR col; /*Color definations */
int font2use; /* Font to use*/
char string[MAX_CHARS]; /* String to appear on PB */
}
PUSHBUTTON;
/* Function Prototypes */
void initialset(PUSHBUTTON *,PUSHBUTTON );
signed drawpb(PUSHBUTTON );
void drawrect(int ,int ,int ,int ,int );
void drawborders(PBDIMENSION ,unsigned ,unsigned );
int pbcpy(PUSHBUTTON *,PUSHBUTTON );
int registerpb(PUSHBUTTON );
int getstatus(int ,int );
void clicked(int ,int );
int unregisterpb(int );
int reportpb(int );
void destroydb(void);
signed drawpb(PUSHBUTTON pb)
{
PUSHBUTTON pbuse;
unsigned savecolor,textx,texty;
initialset(&pbuse,pb);
drawrect(pbuse.dim.top,pbuse.dim.bottom,pbuse.dim.right,pbuse.dim.left,pbuse.col.color);
savecolor = getcolor();
setcolor(pbuse.col.text);
/*
outtextxy((pbuse.dim.left+((ADDITIONALH_SPACES)/2)),(pbuse.dim.top / *+((ADDITIONALV_SPACES)/2) * /),pbuse.string);
*/
settextstyle(pbuse.font2use,HORIZ_DIR,SIZE);
textx = (unsigned) ( ( (float)(((float)pbuse.dim.right - (float)pbuse.dim.left)/2.0) + (float)(pbuse.dim.left - ((float)ADDITIONALH_SPACES/2.0) )) - ( (float)((float)textwidth(pbuse.string)/2.0) ) );
texty = (unsigned)( (float) (( ((float)pbuse.dim.bottom - (float)pbuse.dim.top)/2.0) + (float)((float)pbuse.dim.top - ((float)ADDITIONALV_SPACES/2.0) )) - (float) ((float)textheight(pbuse.string)/2.0) );
#ifdef DEBUG
printf("textx = %d texty = %d",textx,texty);
#endif
outtextxy(textx,texty,pbuse.string);
setcolor(savecolor);
drawborders(pbuse.dim,pbuse.col.topbor,pbuse.col.botbor);
setcolor(savecolor);
return registerpb(pbuse);
}
void initialset(PUSHBUTTON *pbuse,PUSHBUTTON pb)
{
if(pb.font2use == DEFAULT)
{
pbuse->font2use = DEFAULT_FONT;
settextstyle(DEFAULT_FONT,HORIZ_DIR,SIZE);
}
else
{
pbuse->font2use = pb.font2use;
settextstyle(pbuse->font2use,HORIZ_DIR,SIZE);
}
if(pb.col.color == DEFAULT)
pbuse->col.color = DEFAULT_COLOR;
else
pbuse->col.color = pb.col.color;
if(pb.col.topbor == DEFAULT)
pbuse->col.topbor = DEFAULT_TOP_COLOR;
else
pbuse->col.topbor = pb.col.topbor;
if(pb.col.botbor == DEFAULT)
pbuse->col.botbor = DEFAULT_BOTTOM_COLOR;
else
pbuse->col.botbor = pb.col.botbor;
if(pb.col.text == DEFAULT)
pbuse->col.text = DEFAULT_TEXT_COLOR;
else
pbuse->col.text = pb.col.text;
strcpy(pbuse->string,pb.string);
pbuse->dim.top = pb.dim.top;
pbuse->dim.left = pb.dim.left;
if(pb.dim.right == DEFAULT)
pbuse->dim.right = ((pbuse->dim.left) + textwidth(pbuse->string) + ADDITIONALH_SPACES);
else
pbuse->dim.right = pb.dim.right;
if(pb.dim.bottom == DEFAULT)
pbuse->dim.bottom = ((pbuse->dim.top) + textheight(pbuse->string) + ADDITIONALV_SPACES);
else
pbuse->dim.bottom = pb.dim.bottom;
}
void drawrect(int top,int bottom,int right,int left,int color)
{
int rect[5*2];
rect[0] = left;
rect[1] = top;
rect[2] = left;
rect[3] = bottom;
rect[4] = right;
rect[5] = bottom;
rect[6] = right;
rect[7] = top;
rect[8] = rect[0];
rect[9] = rect[1];
setfillstyle(SOLID_FILL,color);
fillpoly(5,rect);
}
void drawborders(PBDIMENSION dim,unsigned topbor,unsigned botbor)
{
unsigned nowcolor;
nowcolor = getcolor();
setcolor(topbor);
line(dim.left,dim.bottom,dim.left,dim.top);
line(dim.left,dim.top,dim.right,dim.top);
setcolor(botbor);
line(dim.left,dim.bottom,dim.right,dim.bottom);
line(dim.right,dim.bottom,dim.right,dim.top);
setcolor(nowcolor);
}
int pbcpy(PUSHBUTTON *dest,PUSHBUTTON src)
{
dest->font2use = src.font2use;
dest->dim.top = src.dim.top;
dest->dim.left = src.dim.left;
dest->dim.right = src.dim.right;
dest->dim.bottom = src.dim.bottom;
dest->col.color = src.col.color;
dest->col.topbor = src.col.topbor;
dest->col.botbor = src.col.botbor;
dest->col.text = src.col.text;
strcpy(dest->string,src.string);
}
unsigned char dont_report[NOOFPBS];
int reportpb(int i)
{
dont_report[i] = !dont_report[i];
return dont_report[i];
}
int no_of_pb_in_db=0;
PUSHBUTTON pbdb[NOOFPBS];
void destroydb(void)
{
no_of_pb_in_db = 0;
}
int registerpb(PUSHBUTTON pb)
{
if(no_of_pb_in_db == (NOOFPBS - 1))
return -1;
pbcpy(&pbdb[no_of_pb_in_db],pb);
no_of_pb_in_db++;
return no_of_pb_in_db;
}
int unregisterpb(int i)
{
i--;
if(i > no_of_pb_in_db )
return -1;
for(/**/ ; i < no_of_pb_in_db ; i++)
pbcpy(&pbdb[i],pbdb[i+1]);
no_of_pb_in_db--;
return 0;
}
int getstatus(int x,int y)
{
int index;
static char flag=0;
if(flag == 0)
{
int i;
for(i=0;i<NOOFPBS;i++)
dont_report[i] = 0;
flag = !flag;
}
for(index = 0;index<no_of_pb_in_db;index++)
{
if(((x >= pbdb[index].dim.left) && (x <= pbdb[index].dim.right)) && ((y >= pbdb[index].dim.top) && (y <= pbdb[index].dim.bottom)))
if(!dont_report[index])
return (index+1);
}
return 0;
}
void clicked(int i,int color)
{
int temp;
PUSHBUTTON invpb;
i--;
pbcpy(&invpb,pbdb[i]);
if(color != DEFAULT)
invpb.col.text = color;
temp = invpb.col.botbor;
invpb.col.botbor = invpb.col.topbor;
invpb.col.topbor = temp;
temp = drawpb(invpb);
unregisterpb(temp);
}