1 /* $Id: button.c,v 1.4 2005-01-24 22:19:10 schaffner Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
16 static char rcsid[] = "$Id: button.c,v 1.4 2005-01-24 22:19:10 schaffner Exp $";
34 #define Middle(x) ((2*(x)+1)/4)
36 #define BUTTON_EXTRA_WIDTH 15
37 #define BUTTON_EXTRA_HEIGHT 2
39 int ui_button_any_drawn = 0;
41 void ui_get_button_size( char * text, int * width, int * height )
45 gr_get_string_size(text, width, height, &avg );
47 *width += BUTTON_EXTRA_WIDTH*2;
50 *height += BUTTON_EXTRA_HEIGHT*2;
56 void ui_draw_button( UI_GADGET_BUTTON * button )
60 if ((button->status==1) || (button->position != button->oldposition))
62 ui_button_any_drawn = 1;
64 gr_set_current_canvas( button->canvas );
65 color = button->canvas->cv_color;
67 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)button)
68 gr_set_fontcolor( CRED, -1 );
71 if ((button->user_function==NULL) && button->dim_if_no_function )
72 gr_set_fontcolor( CGREY, -1 );
74 gr_set_fontcolor( CBLACK, -1 );
78 if (button->position == 0 )
81 ui_draw_box_out( 0, 0, button->width-1, button->height-1 );
82 ui_string_centered( Middle(button->width), Middle(button->height), button->text );
84 gr_setcolor( CBLACK );
85 gr_rect( 0, 0, button->width, button->height );
87 gr_rect( 1, 1, button->width-1, button->height-1 );
91 ui_draw_box_in( 0, 0, button->width-1, button->height-1 );
92 ui_string_centered( Middle(button->width)+1, Middle(button->height)+1, button->text );
94 gr_setcolor( CBLACK );
95 gr_rect( 0, 0, button->width, button->height );
97 gr_rect( 2, 2, button->width, button->height );
100 button->canvas->cv_color = color;
106 UI_GADGET_BUTTON * ui_add_gadget_button( UI_WINDOW * wnd, short x, short y, short w, short h, char * text, int (*function_to_call)(void) )
108 UI_GADGET_BUTTON * button;
110 button = (UI_GADGET_BUTTON *)ui_gadget_add( wnd, 1, x, y, x+w-1, y+h-1 );
114 MALLOC( button->text, char, strlen(text)+1 );
115 strcpy( button->text, text );
121 button->position = 0;
122 button->oldposition = 0;
124 button->user_function = function_to_call;
125 button->user_function1 = NULL;
127 button->dim_if_no_function = 0;
134 void ui_button_do( UI_GADGET_BUTTON * button, int keypress )
137 int OnMe, ButtonLastSelected;
139 OnMe = ui_mouse_on_gadget( (UI_GADGET *)button );
141 button->oldposition = button->position;
143 if (selected_gadget != NULL)
145 if (selected_gadget->kind==1)
146 ButtonLastSelected = 1;
148 ButtonLastSelected = 0;
150 ButtonLastSelected = 1;
153 if ( B1_PRESSED && OnMe && ButtonLastSelected )
156 button->position = 1;
158 button->position = 0;
161 if (keypress == button->hotkey )
163 button->position = 2;
167 if ((keypress == button->hotkey1) && button->user_function1 )
169 result = button->user_function1();
174 //if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
175 // button->position = 2;
177 if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) && ((keypress==KEY_SPACEBAR) || (keypress==KEY_ENTER)) )
178 button->position = 2;
180 if (CurWindow->keyboard_focus_gadget==(UI_GADGET *)button)
181 if ((button->oldposition==2) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
182 button->position = 2;
186 if (button->position==0) {
187 if ( (button->oldposition==1) && OnMe )
189 if ( (button->oldposition==2) && (CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) )
193 ui_draw_button( button );
195 if (button->pressed && button->user_function )
197 result = button->user_function();