added d2demo.mn2
[btb/d2x.git] / unused / ui / button.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #pragma off (unreferenced)
15 static char rcsid[] = "$Id: button.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
16 #pragma on (unreferenced)
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "mem.h"
22 #include "fix.h"
23 #include "types.h"
24 #include "gr.h"
25 #include "ui.h"
26 #include "key.h"
27 #include "mono.h"
28
29 #define Middle(x) ((2*(x)+1)/4)
30
31 #define BUTTON_EXTRA_WIDTH  15
32 #define BUTTON_EXTRA_HEIGHT 2
33
34 int ui_button_any_drawn = 0;
35
36 void ui_get_button_size( char * text, int * width, int * height )
37 {
38         int avg;
39
40         gr_get_string_size(text, width, height, &avg  );
41
42         *width += BUTTON_EXTRA_WIDTH*2;
43         *width += 6;
44
45         *height += BUTTON_EXTRA_HEIGHT*2;
46         *height += 6;
47
48 }
49
50
51 void ui_draw_button( UI_GADGET_BUTTON * button )
52 {
53         int color;
54
55         if ((button->status==1) || (button->position != button->oldposition))
56         {
57                 ui_button_any_drawn = 1;
58                 ui_mouse_hide();
59                 gr_set_current_canvas( button->canvas );
60                 color = button->canvas->cv_color;
61
62                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)button)
63                         gr_set_fontcolor( CRED, -1 );
64                 else
65                 {
66                         if ((button->user_function==NULL) && button->dim_if_no_function )
67                                 gr_set_fontcolor( CGREY, -1 );
68                         else 
69                                 gr_set_fontcolor( CBLACK, -1 );
70                 }
71
72                 button->status = 0;
73                 if (button->position == 0 )
74                 {
75                         if (button->text )      {
76                                 ui_draw_box_out( 0, 0, button->width-1, button->height-1 );
77                                 ui_string_centered(  Middle(button->width), Middle(button->height), button->text );
78                         } else  {
79                                 gr_setcolor( CBLACK );
80                                 gr_rect( 0, 0, button->width, button->height );
81                                 gr_setcolor( color );
82                                 gr_rect( 1, 1, button->width-1, button->height-1 );
83                         }                               
84                 } else {
85                         if (button->text )      {
86                                 ui_draw_box_in( 0, 0, button->width-1, button->height-1 );
87                                 ui_string_centered(  Middle(button->width)+1, Middle(button->height)+1, button->text );
88                         } else  {
89                                 gr_setcolor( CBLACK );
90                                 gr_rect( 0, 0, button->width, button->height );
91                                 gr_setcolor( color );
92                                 gr_rect( 2, 2, button->width, button->height );
93                         }                       
94                 }
95                 button->canvas->cv_color = color;
96                 ui_mouse_show();
97         }
98 }
99
100
101 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) )
102 {
103         UI_GADGET_BUTTON * button;
104
105         button = (UI_GADGET_BUTTON *)ui_gadget_add( wnd, 1, x, y, x+w-1, y+h-1 );
106
107         if ( text )
108         {
109                 MALLOC( button->text, char, strlen(text)+1 );
110                 strcpy( button->text, text );
111         } else {
112                 button->text = NULL;
113         }
114         button->width = w;
115         button->height = h;
116         button->position = 0;
117         button->oldposition = 0;
118         button->pressed = 0;
119         button->user_function = function_to_call;
120         button->user_function1 = NULL;
121         button->hotkey1= -1;
122         button->dim_if_no_function = 0;
123         
124         return button;
125
126 }
127
128
129 void ui_button_do( UI_GADGET_BUTTON * button, int keypress )
130 {
131         int result;
132         int OnMe, ButtonLastSelected;
133
134         OnMe = ui_mouse_on_gadget( (UI_GADGET *)button );
135
136         button->oldposition = button->position;
137
138         if (selected_gadget != NULL)
139         {
140                 if (selected_gadget->kind==1)
141                         ButtonLastSelected = 1;
142                 else
143                         ButtonLastSelected = 0;
144         } else
145                 ButtonLastSelected = 1;
146
147
148         if ( B1_PRESSED && OnMe && ButtonLastSelected )
149         {
150
151                 button->position = 1;
152         } else  {
153                 button->position = 0;
154         }
155
156         if (keypress == button->hotkey )
157         {
158                 button->position = 2;
159                 last_keypress = 0;
160         }
161
162         if ((keypress == button->hotkey1) && button->user_function1 )
163         {
164                 result = button->user_function1();
165                 last_keypress = 0;
166         }
167
168         
169         //if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
170         //      button->position = 2;
171
172         if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) && ((keypress==KEY_SPACEBAR) || (keypress==KEY_ENTER)) )
173                 button->position = 2;
174
175         if (CurWindow->keyboard_focus_gadget==(UI_GADGET *)button)      
176                 if ((button->oldposition==2) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] )  )
177                         button->position = 2;
178
179         button->pressed = 0;
180
181         if (button->position==0) {
182                 if ( (button->oldposition==1) && OnMe )
183                         button->pressed = 1;
184                 if ( (button->oldposition==2) && (CurWindow->keyboard_focus_gadget==(UI_GADGET *)button) )
185                         button->pressed = 1;
186         }
187
188         ui_draw_button( button );
189
190         if (button->pressed && button->user_function )
191         {
192                 result = button->user_function();
193         }
194 }
195
196
197
198
199