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

button.c

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  AUTHOR: Wilson T
00004  *
00005  *   TITLE: button.c
00006  *
00007  *  DATE: April 22, 1993
00008  *
00009  *  PURPOSE:  Query-type button definitions.
00010  *
00011  *************************************************************************/
00012 
00013 #include <graphics.h>
00014 #include <assert.h>
00015 #include <string.h>
00016 #include <stdio.h>
00017 
00018 #include"button.h"
00019 
00020 void PushButton::set_constants(char *name, int color, int Id)
00021 {
00022   Activated   = False;
00023   Color   = color;
00024   surface   = NULL;
00025   strcpy(string, name);
00026   IdNum = Id;
00027   Drawn = No;
00028   X=0; Y=0;
00029 
00030   Viewport surf;
00031   surf.SetTextStyle(FONT, HORIZ_DIR, FONT_SIZE);
00032 
00033   string_height = surf.TextHeight(string);
00034   // Adjust for differing fonts...
00035   string_height = (int)(string_height+(string_height*.33));
00036   string_width  = surf.TextWidth(string);
00037 }
00038 
00039 void PushButton::Init(Display *d, int x, int y)
00040 {
00041   X = x; X2+=X;
00042   Y = y; Y2+=Y;
00043 
00044   Disp = d;
00045 }
00046 
00047 void PushButton::Draw()
00048 {
00049   Activated = False;
00050   Drawn = Yes;
00051 
00052   Disp->FastDraw();
00053   Disp->SetFillStyle(SOLID_FILL, LIGHTGRAY);
00054   Disp->Bar(X+FRAME, Y+FRAME, X2-FRAME, Y2-FRAME);
00055 
00056   draw_shadows(BOTTOM_RIGHT, DARKGRAY);
00057   draw_shadows(TOP_LEFT, WHITE);
00058   draw_frame();
00059   name_button();
00060   Disp->Done();
00061 }
00062 
00063 void PushButton::Erase()
00064 {
00065   if(Drawn == Yes) {
00066     Drawn = No;
00067     Disp->SetFillStyle(SOLID_FILL, Disp->GetBkColor());
00068     Disp->Bar(X, Y, X2, Y2);
00069   }
00070 }
00071 
00072 void PushButton::redraw()
00073 {
00074   Disp->FastDraw();
00075 
00076   draw_shadows(BOTTOM_RIGHT, DARKGRAY);
00077   draw_shadows(TOP_LEFT, WHITE);
00078   name_button();
00079   Disp->Done();
00080 }
00081 
00082 void PushButton::depress_button()
00083 {
00084   Disp->FastDraw();
00085 
00086   // Re-modified for test...
00087   draw_shadows(BOTTOM_RIGHT, LIGHTGRAY);
00088   draw_shadows(TOP_LEFT, LIGHTGRAY);
00089   Disp->SetColor(DARKGRAY);
00090   Disp->Line(X+FRAME, Y+FRAME, X2-FRAME, Y+FRAME);
00091   Disp->Line(X+FRAME, Y+FRAME, X+FRAME, Y2-FRAME);
00092 
00093   name_button();
00094   Disp->Done();
00095 }
00096 
00097 void PushButton::draw_shadows(SHADOW_STYLE style, int color)
00098 {
00099   // Side shadows...
00100   int x = X+FRAME, y = Y+FRAME, x2 = X2-SHADOWS+FRAME, y2 = Y2-SHADOWS+FRAME;
00101 
00102   Disp->SetColor(color);
00103   for(int count=0; count<SHADOWS; count++)
00104   {
00105     switch(style) {
00106       case TOP_LEFT:
00107         Disp->Line(x+count, y+count, x2-count, y+count);
00108         Disp->Line(x+count, y+count, x+count, y2-count);
00109       break;
00110       case BOTTOM_RIGHT:
00111         Disp->Line(x+count, y2-count, x2-count, y2-count);
00112         Disp->Line(x2-count, y+count, x2-count, y2-count);
00113       break;
00114       case TOP_RIGHT:
00115         // Undefined for now...
00116       break;
00117       case BOTTOM_LEFT:
00118         // Undefined for now...
00119       break;
00120     }
00121   }
00122 }
00123 
00124 BOOLEAN PushButton::ButtonBounds(int Mx, int My)
00125 {
00126   // Mouse will call and supply the coordinates of its position
00127   //  It's up to the button itself to determine it's bounds.
00128 
00129   BOOLEAN flag = False;
00130 
00131   if ((Mx<=X2-SHADOWS) && (Mx>=X) && (My<=Y2-SHADOWS) && (My>=Y))
00132     flag = True;
00133   else
00134     flag = False;
00135 
00136   return flag;
00137 }
00138 
00139 void PushButton::draw_frame()
00140 {
00141   Disp->SetLineStyle(SOLID_LINE, 0, FRAME+1);
00142   Disp->SetColor(BLACK);
00143   Disp->Rectangle(X, Y, X2, Y2);
00144   Disp->SetLineStyle(SOLID_LINE, 0, 1);
00145 }
00146 
00147 void PushButton::name_button()
00148 {
00149   int displacement, x, y, x2, y2;
00150 
00151   displacement = (Activated)? 1 : 0;
00152 
00153   x = X+BORDERX+SHADOWS+FRAME+displacement+1;
00154   y = Y+BORDERY+SHADOWS+FRAME+displacement-2;
00155   x2  = X2-BORDERX-SHADOWS-FRAME+displacement+1;
00156   y2  = Y2-BORDERY-SHADOWS-FRAME+displacement+1;
00157 
00158   Disp->SetColor(LIGHTGRAY);
00159   Disp->Rectangle(x-displacement, y-displacement, x2+1, y2+1);
00160 
00161   Viewport surf (Disp, x, y, x2, y2);
00162 
00163   if(surface==NULL) {
00164     int s = imagesize(x, y, x2, y2);
00165     surface = (void*) new byte [s];
00166     assert (surface != NULL);
00167     surf.SetColor(Color);
00168     surf.SetTextStyle (FONT, HORIZ_DIR, FONT_SIZE);
00169     surf.SetTextJustify(CENTER_TEXT, CENTER_TEXT);
00170     surf.OutTextXY(surf.GetMaxX()/2, surf.GetMaxY()/2, string);
00171     surf.GetImage(0, 0, surf.GetMaxX(), surf.GetMaxY(), surface);
00172   }
00173   else
00174     surf.PutImage(0, 0, surface, COPY_PUT);
00175 }
00176 
00177 Message PushButton::Dispatch(Message &msg)
00178 {
00179   switch(msg.origin) {
00180     case MouseMsg:
00181       if (ButtonBounds(msg.d1, msg.d2)) {
00182         switch(msg.code) {
00183           case LMouseDown:
00184             Activated = True;
00185             animate_button();
00186             return Acknowledged;
00187           case MouseStillDown:
00188             if(!Activated) {
00189               Activated = True;
00190               animate_button();
00191             }
00192             return Acknowledged;
00193           case LMouseUp:
00194             Activated = False;
00195             animate_button();
00196             return Message (ButtonMsg, ButtonPress, IdNum);
00197           default:
00198             return NullMsg;
00199         }
00200       }
00201       if(msg.code==MouseStillDown && Activated) {
00202         Activated = False;
00203         animate_button();
00204       }
00205       return NullMsg;
00206 
00207 /*    case KeyMsg:
00208       if (KeyBounds(msg.d1, msg.d2)
00209       return NullMsg;           */
00210   }
00211 
00212 return NullMsg;
00213 }

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