added d2demo.mn2
[btb/d2x.git] / unused / ui / gadget.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: gadget.c,v 1.1.1.1 2001-01-19 03:30:14 bradleyb Exp $";
16 #pragma on (unreferenced)
17
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include "mem.h"
22 #include "fix.h"
23 #include "types.h"
24 #include "gr.h"
25 #include "ui.h"
26
27 #include "key.h"
28
29 UI_GADGET * selected_gadget;
30
31 UI_GADGET * ui_gadget_add( UI_WINDOW * wnd, short kind, short x1, short y1, short x2, short y2 )
32 {
33         UI_GADGET * gadget;
34
35         gadget = (UI_GADGET *)malloc( sizeof(UI_GADGET) );
36         if (gadget==NULL) exit(1);
37
38         if (wnd->gadget == NULL )
39         {
40                 wnd->gadget = gadget;
41                 gadget->prev = gadget;
42                 gadget->next = gadget;
43         } else {
44                 wnd->gadget->prev->next = gadget;
45                 gadget->next = wnd->gadget;
46                 gadget->prev = wnd->gadget->prev;
47                 wnd->gadget->prev = gadget;
48         }
49
50         gadget->when_tab = NULL;
51         gadget->when_btab = NULL;
52         gadget->when_up = NULL;
53         gadget->when_down = NULL;
54         gadget->when_left = NULL;
55         gadget->when_right = NULL;
56         gadget->kind = kind;
57         gadget->status = 1;
58         gadget->oldstatus = 0;
59         if ( x1==0 && x2==0 && y1==0 && y2== 0 )
60                 gadget->canvas = NULL;
61         else
62                 gadget->canvas = gr_create_sub_canvas( wnd->canvas, x1, y1, x2-x1+1, y2-y1+1 );
63         gadget->x1 = gadget->canvas->cv_bitmap.bm_x;
64         gadget->y1 = gadget->canvas->cv_bitmap.bm_y;
65         gadget->x2 = gadget->canvas->cv_bitmap.bm_x+x2-x1+1;
66         gadget->y2 = gadget->canvas->cv_bitmap.bm_y+y2-y1+1;
67         gadget->parent = NULL;
68         gadget->hotkey = -1;
69         return gadget;
70
71 }
72
73 void ui_gadget_delete_all( UI_WINDOW * wnd )
74 {
75         UI_GADGET * tmp;
76
77         ui_pad_deactivate();
78
79         while( wnd->gadget != NULL )
80         {
81                 tmp = wnd->gadget;
82                 if (tmp->next == tmp )
83                 {
84                         wnd->gadget = NULL;
85                 } else {
86                         tmp->next->prev = tmp->prev;
87                         tmp->prev->next = tmp->next;
88                         wnd->gadget = tmp->next;
89                 }
90                 if (tmp->canvas)
91                         gr_free_sub_canvas( tmp->canvas );
92
93
94                 if (tmp->kind == 1 )    // Button
95                 {
96                         UI_GADGET_BUTTON * but1 = (UI_GADGET_BUTTON *)tmp;
97                         if (but1->text)
98                                 free( but1->text );
99                 }
100
101                 if (tmp->kind == 6 )    // Inputbox
102                 {
103                         UI_GADGET_INPUTBOX * but1 = (UI_GADGET_INPUTBOX *)tmp;
104                         free( but1->text );
105                 }
106
107                 if (tmp->kind == 5 )    // Checkbox
108                 {
109                         UI_GADGET_CHECKBOX * but1 = (UI_GADGET_CHECKBOX *)tmp;
110                         free( but1->text );
111                 }
112                 
113                 if (tmp->kind == 9 )    // Icon
114                 {
115                         UI_GADGET_ICON * but1 = (UI_GADGET_ICON *)tmp;
116                         free( but1->text );
117                 }
118
119
120                 free( tmp );
121         }
122 }
123
124
125 int is_under_another_window( UI_WINDOW * win, UI_GADGET * gadget )
126 {
127         UI_WINDOW * temp;
128
129         temp = win->next;
130
131         while( temp != NULL )   {
132                 if (    ( gadget->x1 > temp->x)                                         &&
133                                 ( gadget->x1 < (temp->x+temp->width) )  &&
134                                 ( gadget->y1 > temp->y)                                         && 
135                                 ( gadget->y1 < (temp->y+temp->height) )
136                         )       
137                 {
138                                 //gadget->status =1;
139                                 return 1;
140                 }
141                 
142
143                 if (    ( gadget->x2 > temp->x)                                         &&
144                                 ( gadget->x2 < (temp->x+temp->width) )  &&
145                                 ( gadget->y2 > temp->y)                                         && 
146                                 ( gadget->y2 < (temp->y+temp->height) )
147                         )
148                 {
149                                 //gadget->status =1;
150                                 return 1;
151                 }
152                 
153
154                 temp = temp->next;
155         }
156         return 0;
157 }
158
159
160 int ui_mouse_on_gadget( UI_GADGET * gadget )
161 {
162         if ((Mouse.x >= gadget->x1) && (Mouse.x <= gadget->x2-1) &&     (Mouse.y >= gadget->y1) &&      (Mouse.y <= gadget->y2-1) )     {
163                 if (is_under_another_window(CurWindow, gadget))
164                         return 0;
165                 return 1;
166         } else
167                 return 0;
168 }
169
170 void ui_window_do_gadgets( UI_WINDOW * wnd )
171 {
172         int keypress;
173         UI_GADGET * tmp, * tmp1;
174
175         CurWindow = wnd;
176
177         keypress = last_keypress;
178
179         tmp = wnd->gadget;
180
181         if (tmp == NULL ) return;
182
183         if (selected_gadget==NULL)
184                 selected_gadget = tmp;
185
186         tmp1 = wnd->keyboard_focus_gadget;
187
188         do
189         {
190                 if (ui_mouse_on_gadget(tmp) && B1_JUST_PRESSED )
191                 {
192                         selected_gadget = tmp;
193                         if (tmp->parent!=NULL)
194                         {
195                                 while (tmp->parent != NULL )
196                                         tmp = tmp->parent;
197                                 wnd->keyboard_focus_gadget = tmp;
198                                 break;
199                         }
200                         else
201                         {
202                                 wnd->keyboard_focus_gadget = tmp;
203                                 break;
204                         }
205                 }
206                 if ( tmp->hotkey == keypress )
207                 {
208                         wnd->keyboard_focus_gadget = tmp;
209                         break;
210                 }
211                 tmp = tmp->next;
212         } while( tmp != wnd->gadget );
213
214         if (wnd->keyboard_focus_gadget != NULL)
215         {
216                 switch (keypress )
217                 {
218                         case (KEY_TAB):
219                                 if ( wnd->keyboard_focus_gadget->when_tab != NULL )
220                                         wnd->keyboard_focus_gadget = wnd->keyboard_focus_gadget->when_tab;
221                                 break;
222                         case (KEY_TAB+KEY_SHIFTED):
223                                 if ( wnd->keyboard_focus_gadget->when_btab != NULL )
224                                         wnd->keyboard_focus_gadget = wnd->keyboard_focus_gadget->when_btab;
225                                 break;
226                         case (KEY_UP):
227                                 if ( wnd->keyboard_focus_gadget->when_up != NULL )
228                                         wnd->keyboard_focus_gadget = wnd->keyboard_focus_gadget->when_up;
229                                 break;
230                         case (KEY_DOWN):
231                                 if ( wnd->keyboard_focus_gadget->when_down != NULL )
232                                         wnd->keyboard_focus_gadget = wnd->keyboard_focus_gadget->when_down;
233                                 break;
234                         case (KEY_LEFT):
235                                 if ( wnd->keyboard_focus_gadget->when_left != NULL )
236                                         wnd->keyboard_focus_gadget = wnd->keyboard_focus_gadget->when_left;
237                                 break;
238                         case (KEY_RIGHT):
239                                 if ( wnd->keyboard_focus_gadget->when_right != NULL )
240                                         wnd->keyboard_focus_gadget = wnd->keyboard_focus_gadget->when_right;
241                                 break;
242                 }
243         }
244
245         if (wnd->keyboard_focus_gadget != tmp1)
246         {
247                 if (wnd->keyboard_focus_gadget != NULL )
248                         wnd->keyboard_focus_gadget->status = 1;
249                 if (tmp1 != NULL )
250                         tmp1->status = 1;
251         }
252
253         tmp = wnd->gadget;
254         do
255         {
256                 if (!is_under_another_window( CurWindow, tmp )) {
257                         UI_WINDOW *curwindow_save=CurWindow;
258
259                         switch( tmp->kind )
260                         {
261                         case 1:
262                                 ui_button_do( (UI_GADGET_BUTTON *)tmp, keypress );
263                                 break;
264                         case 2:
265                                 ui_listbox_do( (UI_GADGET_LISTBOX *)tmp, keypress );
266                                 break;
267                         case 3:
268                                 ui_scrollbar_do( (UI_GADGET_SCROLLBAR *)tmp, keypress );
269                                 break;
270                         case 4:
271                                 ui_radio_do( (UI_GADGET_RADIO *)tmp, keypress );
272                                 break;
273                         case 5:
274                                 ui_checkbox_do( (UI_GADGET_CHECKBOX *)tmp, keypress );
275                                 break;
276                         case 6:
277                                 ui_inputbox_do( (UI_GADGET_INPUTBOX *)tmp, keypress );
278                                 break;
279                         case 7:
280                                 ui_userbox_do( (UI_GADGET_USERBOX *)tmp, keypress );
281                                 break;
282                         case 8:
283                                 ui_keytrap_do( (UI_GADGET_KEYTRAP *)tmp, keypress );
284                                 break;
285                         case 9:
286                                 ui_icon_do( (UI_GADGET_ICON *)tmp, keypress );
287                                 break;
288                         }
289
290                         CurWindow=curwindow_save;
291                 }
292
293                 tmp = tmp->next;
294         } while( tmp != wnd->gadget );
295 }
296
297
298
299 UI_GADGET * ui_gadget_get_next( UI_GADGET * gadget )
300 {
301         UI_GADGET * tmp;
302
303         tmp = gadget->next;
304
305         while( tmp != gadget && (tmp->parent!=NULL) )
306                 tmp = tmp->next;
307
308         return tmp;
309 }
310
311 UI_GADGET * ui_gadget_get_prev( UI_GADGET * gadget )
312 {
313         UI_GADGET * tmp;
314
315         tmp = gadget->prev;
316
317         while( tmp != gadget && (tmp->parent!=NULL) )
318                 tmp = tmp->prev;
319
320         return tmp;
321 }
322
323 void ui_gadget_calc_keys( UI_WINDOW * wnd)
324 {
325         UI_GADGET * tmp;
326
327         tmp = wnd->gadget;
328
329         if (tmp==NULL) return;
330
331         do
332         {
333                 tmp->when_tab = ui_gadget_get_next(tmp);
334                 tmp->when_btab = ui_gadget_get_prev(tmp);
335
336                 tmp = tmp->next;
337         } while( tmp != wnd->gadget );
338
339 }