ifdef RCS for rcsid
[btb/d2x.git] / ui / radio.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 #ifdef RCS
15 static char rcsid[] = "$Id: radio.c,v 1.2 2004-12-19 14:10:33 btb Exp $";
16 #endif
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "fix.h"
21 #include "types.h"
22 #include "gr.h"
23 #include "ui.h"
24 #include "key.h"
25
26 #include "mono.h"
27
28 #define Middle(x) ((2*(x)+1)/4)
29
30 void ui_draw_radio( UI_GADGET_RADIO * radio )
31 {
32
33         if ((radio->status==1) || (radio->position != radio->oldposition))
34         {
35                 radio->status = 0;
36
37                 ui_mouse_hide();
38                 gr_set_current_canvas( radio->canvas );
39
40                 if (radio->flag)
41                         gr_set_fontcolor( CRED, -1 );
42                 else
43                         gr_set_fontcolor( CBLACK, -1 );
44
45                 if (radio->position == 0 )
46                 {
47                         ui_draw_box_out( 0, 0, radio->width-1, radio->height-1 );
48                         ui_string_centered(  Middle(radio->width), Middle(radio->height), "þ" );
49                 } else {
50                         ui_draw_box_in( 0, 0, radio->width-1, radio->height-1 );
51                         ui_string_centered(  Middle(radio->width)+1, Middle(radio->height)+1, "þ" );
52                 }
53
54                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)radio)
55                         gr_set_fontcolor( CRED, CWHITE );
56                 else
57                         gr_set_fontcolor( CBLACK, CWHITE );
58
59                 gr_ustring( radio->width+4, 2, radio->text );
60
61                 ui_mouse_show();
62         }
63 }
64
65
66 UI_GADGET_RADIO * ui_add_gadget_radio( UI_WINDOW * wnd, short x, short y, short w, short h, short group, char * text )
67 {
68         UI_GADGET_RADIO * radio;
69
70         radio = (UI_GADGET_RADIO *)ui_gadget_add( wnd, 4, x, y, x+w-1, y+h-1 );
71
72         radio->text = strdup(text);
73         radio->width = w;
74         radio->height = h;
75         radio->position = 0;
76         radio->oldposition = 0;
77         radio->pressed = 0;
78         radio->flag = 0;
79         radio->group = group;
80
81         return radio;
82
83 }
84
85
86 void ui_radio_do( UI_GADGET_RADIO * radio, int keypress )
87 {
88         UI_GADGET * tmp;
89         UI_GADGET_RADIO * tmpr;
90         int OnMe, ButtonLastSelected;
91         keypress  = keypress;
92
93         OnMe = ui_mouse_on_gadget( (UI_GADGET *)radio );
94
95         radio->oldposition = radio->position;
96
97         if (selected_gadget != NULL)
98         {
99                 if (selected_gadget->kind==4)
100                         ButtonLastSelected = 1;
101                 else
102                         ButtonLastSelected = 0;
103         } else
104                 ButtonLastSelected = 1;
105
106
107         if ( B1_PRESSED && OnMe && ButtonLastSelected )
108         {
109
110                 radio->position = 1;
111         } else  {
112                 radio->position = 0;
113         }
114
115
116         if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)radio) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
117                 radio->position = 2;
118
119         if ((radio->position==0) && (radio->oldposition==1) && OnMe )
120                 radio->pressed = 1;
121         else if ((radio->position==0) && (radio->oldposition==2) && (CurWindow->keyboard_focus_gadget==(UI_GADGET *)radio) )
122                 radio->pressed = 1;
123         else
124                 radio->pressed = 0;
125
126         if ((radio->pressed == 1) && (radio->flag==0))
127         {
128                 tmp = (UI_GADGET *)radio->next;
129
130                 while (tmp != (UI_GADGET *)radio )
131                 {
132                         if (tmp->kind==4)
133                         {
134                                 tmpr = (UI_GADGET_RADIO *)tmp;
135                                 if ((tmpr->group == radio->group ) && (tmpr->flag) )
136                                 {
137                                         tmpr->flag = 0;
138                                         tmpr->status = 1;
139                                         tmpr->pressed = 0;
140                                 }
141                         }
142                         tmp = tmp->next;
143                 }
144                 radio->flag = 1;
145         }
146
147         ui_draw_radio( radio );
148
149 }
150
151