Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

checkbox.c

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  AUTHOR: Wilson T
00004  *
00005  *   TITLE: checkbox.c
00006  *
00007  *  DATE: April 25, 1993
00008  *
00009  *  PURPOSE:  Checkbox button definitions.
00010  *
00011  *************************************************************************/
00012 
00013 #include <graphics.h>
00014 #include <string.h>
00015 #include <stdio.h>
00016 #include <math.h>
00017 
00018 #include"checkbox.h"
00019 
00020 void CheckBox::set_constants(char *name, int color, int fcolor, int Id, int type)
00021 {
00022   /*****************************************************************
00023 
00024     Flag states of CheckBoxes...
00025 
00026       Activated - User input detected...
00027       BStatus - Whether the option is selected or not...
00028       Able    - Allow/disallow interaction with user...
00029 
00030    *****************************************************************/
00031 
00032   Activated   = False;
00033   BStatus   = Off;  // For now, may be a user-defined field later.
00034   Able      = Yes;
00035 
00036   Color   = color;
00037   COLORFILL   = fcolor;
00038   strcpy(string, name);
00039 
00040   IdNum = Id;
00041   Drawn = No;
00042   Type = type;
00043   X=0; Y=0;
00044 
00045   Viewport surf;
00046   surf.SetTextStyle(CFONT, HORIZ_DIR, CFONT_SIZE);
00047 
00048   string_height = surf.TextHeight(string);
00049   // Adjust for differing fonts...
00050   string_height = (int)(string_height+(string_height*.33));
00051   string_width  = surf.TextWidth(string);
00052 }
00053 
00054 void CheckBox::Init(Display *d, int x, int y)
00055 {
00056   X = x; X2+=X;
00057   Y = y; Y2+=Y;
00058 
00059   Disp = d;
00060   BkColor=Disp->GetBkColor();
00061 }
00062 
00063 void CheckBox::Draw()
00064 {
00065   int x=X+CBORDERX;
00066   int y=(int)Y+abs((string_height-BOXSIZE)/2);
00067 
00068   Disp->FastDraw();
00069   Disp->SetFillStyle(SOLID_FILL, BOXCOLOR);
00070   Disp->SetColor(BLACK);
00071 
00072   switch(Type) {
00073     case Square:
00074       Disp->Bar(x, y, x+BOXSIZE, y+BOXSIZE);
00075       Disp->Rectangle(x, y, x+BOXSIZE, y+BOXSIZE);
00076       break;
00077     case Circle:
00078       int radius = (int)(BOXSIZE/2);
00079       Disp->FillEllipse(x+radius, y+radius, radius, radius);
00080       break;
00081   }
00082 
00083   name_button(Color);
00084   Disp->Done();
00085   Drawn=Yes;
00086 }
00087 
00088 void CheckBox::Erase()
00089 {
00090   Disp->FastDraw();
00091 
00092   if(Drawn == Yes) {
00093     Drawn = No;
00094     Disp->SetFillStyle(SOLID_FILL, Disp->GetBkColor());
00095     Disp->Bar(X, Y, X2, Y2);
00096   }
00097   Disp->Done();
00098 }
00099 
00100 void CheckBox::empty_box(int color)
00101 {
00102   Disp->FastDraw();
00103   int x=X+CBORDERX;
00104   int y=(int)Y+abs((string_height-BOXSIZE)/2);
00105 
00106   Disp->SetFillStyle(SOLID_FILL, color);
00107   switch(Type) {
00108     case Square:
00109       Disp->Bar(x+1, y+1, x+BOXSIZE-1, y+BOXSIZE-1);
00110       break;
00111     case Circle:
00112       int radius = (int)(BOXSIZE/2);
00113       Disp->SetColor(BLACK);
00114       Disp->FillEllipse(x+radius, y+radius, radius, radius);
00115       break;
00116   }
00117   Disp->Done();
00118 
00119   BStatus = Off;
00120 }
00121 
00122 void CheckBox::fill_box()
00123 {
00124   Disp->FastDraw();
00125   int x=X+CBORDERX;
00126   int y=(int)Y+abs((string_height-BOXSIZE)/2);
00127 
00128   Disp->SetFillStyle(SOLID_FILL, COLORFILL);
00129   Disp->SetColor(COLORFILL);
00130   switch(Type) {
00131     case Square:
00132       draw_check();
00133       break;
00134     case Circle:
00135       int radius = (int)(BOXSIZE/2);
00136       Disp->FillEllipse(x+radius, y+radius, radius-FILLSIZE-2, radius-FILLSIZE-2);
00137       break;
00138   }
00139   Disp->Done();
00140 
00141   BStatus = On;
00142 }
00143 
00144 void CheckBox::draw_check()
00145 {
00146   int thickness=3;
00147 
00148   int x=X+CBORDERX+FILLSIZE, x2=X+CBORDERX+BOXSIZE-FILLSIZE;
00149   int y=(int)Y+abs((string_height-BOXSIZE)/2)+FILLSIZE;
00150   int y2=(int)Y+abs((string_height-BOXSIZE)/2)+BOXSIZE-FILLSIZE;
00151 
00152   int midx=x+(int)(.3333*(x2-x));
00153   int midy=y+(int)(.6666*(y2-y));
00154 
00155   Disp->SetLineStyle(SOLID_LINE, 0, 1);
00156   for(int count=0; count<thickness; count++) {
00157     Disp->Line(x, midy, midx, y2-thickness+count);
00158     Disp->Line(midx, y2-thickness+count, x2, y);
00159   }
00160 }
00161 
00162 void CheckBox::animate_frame()
00163 {
00164   int x=X+CBORDERX;
00165   int y=(int)Y+((string_height-BOXSIZE)/2);
00166   int radius;
00167 
00168   Disp->FastDraw();
00169 
00170   if (Activated)
00171     Disp->SetColor(BLACK);
00172   else
00173     Disp->SetColor(BkColor);
00174 
00175   Disp->SetLineStyle(SOLID_LINE, 0, CFRAME);
00176   switch(Type) {
00177     case Square:
00178       Disp->Rectangle(x-CFRAME+1, y-CFRAME+1, x+BOXSIZE+CFRAME-1, y+BOXSIZE+CFRAME-1);
00179       break;
00180     case Circle:
00181       radius = (int)(BOXSIZE/2);
00182       Disp->Circle(x+radius, y+radius, radius+CFRAME-1);
00183       break;
00184   }
00185 
00186   Disp->Done();
00187 }
00188 
00189 BOOLEAN CheckBox::ButtonBounds(int Mx, int My)
00190 {
00191   // Mouse will call and supply the coordinates of its position
00192   //  It's up to the button itself to determine it's bounds.
00193 
00194   if ((Mx<=X2) && (Mx>=X) && (My<=Y2) && (My>=Y))
00195     return True;
00196   else
00197     return False;
00198 }
00199 
00200 void CheckBox::name_button(int color)
00201 {
00202   int x, y, x2, y2;
00203 
00204   x = X+CBORDERX+BOXSIZE+BOXBORDER;
00205   y = Y;
00206   x2  = X2;
00207   y2  = Y2;
00208 
00209   Viewport surf (Disp, x, y, x2, y2);
00210 
00211   surf.SetColor(color);
00212   surf.SetTextStyle (CFONT, HORIZ_DIR, CFONT_SIZE);
00213   surf.OutTextXY(0, 0, string);
00214 }
00215 
00216 Message CheckBox::Dispatch(Message &msg)
00217 {
00218   switch(msg.origin) {
00219     case MouseMsg:
00220       return mouse_msg(msg);
00221     case KeyMsg:
00222       return key_msg(msg);
00223     case GodMsg:
00224       return command_msg(msg);
00225   }
00226   return msg;
00227 }
00228 
00229 Message CheckBox::mouse_msg(Message &msg)
00230 {
00231   if (ButtonBounds(msg.d1, msg.d2) && Able) {
00232     switch(msg.code) {
00233       case LMouseDown:
00234         Activated = True;
00235         animate_frame();
00236         return Acknowledged;
00237       case LMouseStillDown:
00238         if(!Activated) {
00239           Activated = True;
00240           animate_frame();
00241         }
00242         return NullMsg;
00243       case LMouseUp:
00244         Activated = False;
00245         BStatus = BStatus? Off : On;
00246         animate_frame();
00247         animate_box();
00248         return Message(ButtonMsg, Status, IdNum, BStatus);
00249       default:
00250         return NullMsg;
00251     }
00252   }
00253   if(msg.code==LMouseStillDown && Activated && Able) {
00254     Activated = False;
00255     animate_frame();
00256     animate_box();
00257   }
00258   return NullMsg;
00259 }
00260 
00261 Message CheckBox::key_msg(Message &)
00262 {
00263 /*  if (KeyBounds(msg.d1, msg.d2) && Able)
00264       return NullMsg; */
00265 
00266 return NullMsg;
00267 }
00268 
00269 Message CheckBox::command_msg(Message &msg)
00270 {
00271   if(msg.d1 != IdNum)
00272     return NullMsg;
00273 
00274   switch (msg.code) {
00275     case Disable:
00276       BStatus=Off;
00277       animate_box();
00278       disable_box();
00279       return Acknowledged;
00280     case Enable:
00281       enable_box();
00282       return Acknowledged;
00283     case RequestStatus:
00284       return Message(ButtonMsg, Status, IdNum, (int)status());
00285   }
00286   return NullMsg;
00287 }
00288 

Generated on Sun Jul 6 23:07:15 2003 for Scotland Yard by doxygen1.2.15