get rid of compiler warnings
[btb/d2x.git] / ui / button.c
1 /* $Id: button.c,v 1.4 2005-01-24 22:19:10 schaffner Exp $ */
2 /*
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.
13 */
14
15 #ifdef RCS
16 static char rcsid[] = "$Id: button.c,v 1.4 2005-01-24 22:19:10 schaffner Exp $";
17 #endif
18
19 #ifdef HAVE_CONFIG_H
20 #include "conf.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "u_mem.h"
27 #include "fix.h"
28 #include "pstypes.h"
29 #include "gr.h"
30 #include "ui.h"
31 #include "key.h"
32 #include "mono.h"
33
34 #define Middle(x) ((2*(x)+1)/4)
35
36 #define BUTTON_EXTRA_WIDTH  15
37 #define BUTTON_EXTRA_HEIGHT 2
38
39 int ui_button_any_drawn = 0;
40
41 void ui_get_button_size( char * text, int * width, int * height )
42 {
43         int avg;
44
45         gr_get_string_size(text, width, height, &avg  );
46
47         *width += BUTTON_EXTRA_WIDTH*2;
48         *width += 6;
49
50         *height += BUTTON_EXTRA_HEIGHT*2;
51         *height += 6;
52
53 }
54
55
56 void ui_draw_button( UI_GADGET_BUTTON * button )
57 {
58         int color;
59
60         if ((button->status==1) || (button->position != button->oldposition))
61         {
62                 ui_button_any_drawn = 1;
63                 ui_mouse_hide();
64                 gr_set_current_canvas( button->canvas );
65                 color = button->canvas->cv_color;
66
67                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)button)
68                         gr_set_fontcolor( CRED, -1 );
69                 else
70                 {
71                         if ((button->user_function==NULL) && button->dim_if_no_function )
72                                 gr_set_fontcolor( CGREY, -1 );
73                         else 
74                                 gr_set_fontcolor( CBLACK, -1 );
75                 }
76
77                 button->status = 0;
78                 if (button->position == 0 )
79                 {
80                         if (button->text )      {
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 );
83                         } else  {
84                                 gr_setcolor( CBLACK );
85                                 gr_rect( 0, 0, button->width, button->height );
86                                 gr_setcolor( color );
87                                 gr_rect( 1, 1, button->width-1, button->height-1 );
88                         }                               
89                 } else {
90                         if (button->text )      {
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 );
93                         } else  {
94                                 gr_setcolor( CBLACK );
95                                 gr_rect( 0, 0, button->width, button->height );
96                                 gr_setcolor( color );
97                                 gr_rect( 2, 2, button->width, button->height );
98                         }                       
99                 }
100                 button->canvas->cv_color = color;
101                 ui_mouse_show();
102         }
103 }
104
105
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) )
107 {
108         UI_GADGET_BUTTON * button;
109
110         button = (UI_GADGET_BUTTON *)ui_gadget_add( wnd, 1, x, y, x+w-1, y+h-1 );
111
112         if ( text )
113         {
114                 MALLOC( button->text, char, strlen(text)+1 );
115                 strcpy( button->text, text );
116         } else {
117                 button->text = NULL;
118         }
119         button->width = w;
120         button->height = h;
121         button->position = 0;
122         button->oldposition = 0;
123         button->pressed = 0;
124         button->user_function = function_to_call;
125         button->user_function1 = NULL;
126         button->hotkey1= -1;
127         button->dim_if_no_function = 0;
128         
129         return button;
130
131 }
132
133
134 void ui_button_do( UI_GADGET_BUTTON * button, int keypress )
135 {
136         int result;
137         int OnMe, ButtonLastSelected;
138
139         OnMe = ui_mouse_on_gadget( (UI_GADGET *)button );
140
141         button->oldposition = button->position;
142
143         if (selected_gadget != NULL)
144         {
145                 if (selected_gadget->kind==1)
146                         ButtonLastSelected = 1;
147                 else
148                         ButtonLastSelected = 0;
149         } else
150                 ButtonLastSelected = 1;
151
152
153         if ( B1_PRESSED && OnMe && ButtonLastSelected )
154         {
155
156                 button->position = 1;
157         } else  {
158                 button->position = 0;
159         }
160
161         if (keypress == button->hotkey )
162         {
163                 button->position = 2;
164                 last_keypress = 0;
165         }
166
167         if ((keypress == button->hotkey1) && button->user_function1 )
168         {
169                 result = button->user_function1();
170                 last_keypress = 0;
171         }
172
173         
174         //if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
175         //      button->position = 2;
176
177         if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) && ((keypress==KEY_SPACEBAR) || (keypress==KEY_ENTER)) )
178                 button->position = 2;
179
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;
183
184         button->pressed = 0;
185
186         if (button->position==0) {
187                 if ( (button->oldposition==1) && OnMe )
188                         button->pressed = 1;
189                 if ( (button->oldposition==2) && (CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) )
190                         button->pressed = 1;
191         }
192
193         ui_draw_button( button );
194
195         if (button->pressed && button->user_function )
196         {
197                 result = button->user_function();
198         }
199 }
200
201
202
203
204