amd64 fixes
[btb/d2x.git] / ui / radio.c
1 /* $Id: radio.c,v 1.6 2005-07-03 13:12:47 chris 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: radio.c,v 1.6 2005-07-03 13:12:47 chris 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 "fix.h"
27 #include "pstypes.h"
28 #include "gr.h"
29 #include "ui.h"
30 #include "key.h"
31 #include "u_mem.h"
32
33 #include "mono.h"
34
35 #define Middle(x) ((2*(x)+1)/4)
36
37 void ui_draw_radio( UI_GADGET_RADIO * radio )
38 {
39
40         if ((radio->status==1) || (radio->position != radio->oldposition))
41         {
42                 radio->status = 0;
43
44                 ui_mouse_hide();
45                 gr_set_current_canvas( radio->canvas );
46
47                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *) radio)
48                         gr_set_fontcolor(CRED, -1);
49                 else
50                         gr_set_fontcolor(CBLACK, -1);
51
52                 if (radio->position == 0 )
53                 {
54                         ui_draw_box_out( 0, 0, radio->width-1, radio->height-1 );
55                         if (radio->flag)
56                                 ui_string_centered(Middle(radio->width), Middle(radio->height), "O");
57                         else
58                                 ui_string_centered(Middle(radio->width), Middle(radio->height), " ");
59                 } else {
60                         ui_draw_box_in( 0, 0, radio->width-1, radio->height-1 );
61                         if (radio->flag)
62                                 ui_string_centered(Middle(radio->width) + 1, Middle(radio->height) + 1, "O");
63                         else
64                                 ui_string_centered(Middle(radio->width) + 1, Middle(radio->height) + 1, " ");
65                 }
66
67                 gr_ustring( radio->width+4, 2, radio->text );
68
69                 ui_mouse_show();
70         }
71 }
72
73
74 UI_GADGET_RADIO * ui_add_gadget_radio( UI_WINDOW * wnd, short x, short y, short w, short h, short group, char * text )
75 {
76         UI_GADGET_RADIO * radio;
77
78         radio = (UI_GADGET_RADIO *)ui_gadget_add( wnd, 4, x, y, x+w-1, y+h-1 );
79
80         radio->text = d_strdup(text);
81         radio->width = w;
82         radio->height = h;
83         radio->position = 0;
84         radio->oldposition = 0;
85         radio->pressed = 0;
86         radio->flag = 0;
87         radio->group = group;
88
89         return radio;
90
91 }
92
93
94 void ui_radio_do( UI_GADGET_RADIO * radio, int keypress )
95 {
96         UI_GADGET * tmp;
97         UI_GADGET_RADIO * tmpr;
98         int OnMe, ButtonLastSelected;
99         keypress  = keypress;
100
101         OnMe = ui_mouse_on_gadget( (UI_GADGET *)radio );
102
103         radio->oldposition = radio->position;
104
105         if (selected_gadget != NULL)
106         {
107                 if (selected_gadget->kind==4)
108                         ButtonLastSelected = 1;
109                 else
110                         ButtonLastSelected = 0;
111         } else
112                 ButtonLastSelected = 1;
113
114
115         if ( B1_PRESSED && OnMe && ButtonLastSelected )
116         {
117
118                 radio->position = 1;
119         } else  {
120                 radio->position = 0;
121         }
122
123
124         if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)radio) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
125                 radio->position = 2;
126
127         if ((radio->position==0) && (radio->oldposition==1) && OnMe )
128                 radio->pressed = 1;
129         else if ((radio->position==0) && (radio->oldposition==2) && (CurWindow->keyboard_focus_gadget==(UI_GADGET *)radio) )
130                 radio->pressed = 1;
131         else
132                 radio->pressed = 0;
133
134         if ((radio->pressed == 1) && (radio->flag==0))
135         {
136                 tmp = (UI_GADGET *)radio->next;
137
138                 while (tmp != (UI_GADGET *)radio )
139                 {
140                         if (tmp->kind==4)
141                         {
142                                 tmpr = (UI_GADGET_RADIO *)tmp;
143                                 if ((tmpr->group == radio->group ) && (tmpr->flag) )
144                                 {
145                                         tmpr->flag = 0;
146                                         tmpr->status = 1;
147                                         tmpr->pressed = 0;
148                                 }
149                         }
150                         tmp = tmp->next;
151                 }
152                 radio->flag = 1;
153         }
154
155         ui_draw_radio( radio );
156
157 }
158
159 void ui_radio_set_value(UI_GADGET_RADIO *radio, sbyte value)
160 {
161         UI_GADGET_RADIO *tmp;
162
163         if (radio->flag == value)
164                 return;
165
166         radio->flag = value;
167         radio->status = 1;      // redraw
168
169         tmp = (UI_GADGET_RADIO *) radio->next;
170
171         while (tmp != radio)
172         {
173                 if ((tmp->kind == 4) && (tmp->group == radio->group) && tmp->flag)
174                 {
175                         tmp->flag = 0;
176                         tmp->status = 1;
177                 }
178                 tmp = (UI_GADGET_RADIO *) tmp->next;
179         }
180 }