]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/radio.cpp
automatic language selection support
[taylor/freespace2.git] / src / ui / radio.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/UI/RADIO.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code to handle radio buttons.
16  *
17  * $Log$
18  * Revision 1.2  2002/06/09 04:41:29  relnev
19  * added copyright header
20  *
21  * Revision 1.1.1.1  2002/05/03 03:28:11  root
22  * Initial import.
23  *
24  * 
25  * 4     12/02/98 5:47p Dave
26  * Put in interface xstr code. Converted barracks screen to new format.
27  * 
28  * 3     10/13/98 9:29a Dave
29  * Started neatening up freespace.h. Many variables renamed and
30  * reorganized. Added AlphaColors.[h,cpp]
31  * 
32  * 2     10/07/98 10:54a Dave
33  * Initial checkin.
34  * 
35  * 1     10/07/98 10:51a Dave
36  * 
37  * 9     3/10/98 4:19p John
38  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
39  * & Glide have popups and print screen.  Took out all >8bpp software
40  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
41  * support Fred.  Made zbuffering key off of functions rather than one
42  * global variable.
43  * 
44  * 8     2/03/98 4:21p Hoffoss
45  * Made UI controls draw white text when disabled.
46  * 
47  * 7     1/14/98 6:44p Hoffoss
48  * Massive changes to UI code.  A lot cleaner and better now.  Did all
49  * this to get the new UI_DOT_SLIDER to work properly, which the old code
50  * wasn't flexible enough to handle.
51  * 
52  * 6     6/12/97 12:39p John
53  * made ui use freespace colors
54  * 
55  * 5     6/11/97 1:13p John
56  * Started fixing all the text colors in the game.
57  * 
58  * 4     5/26/97 10:26a Lawrance
59  * get slider control working 100%
60  * 
61  * 3     1/01/97 6:46p Lawrance
62  * changed text color of radio button to green from black
63  * 
64  * 2     11/15/96 11:43a John
65  * 
66  * 1     11/14/96 6:55p John
67  *
68  * $NoKeywords: $
69  */
70
71 #include "uidefs.h"
72 #include "ui.h"
73 #include "alphacolors.h"
74
75 void UI_RADIO::create(UI_WINDOW *wnd, char *_text, int _x, int _y, int _state, int _group )
76 {
77         int _w, _h;
78
79 //      gr_get_string_size( &_w, &_h, "X" );
80         _w = 18;
81         _h = 18;
82
83         if (_text)      
84                 text = strdup(_text);
85         else
86                 text = NULL;
87
88         base_create( wnd, UI_KIND_RADIO, _x, _y, _w, _h );
89
90         position = 0;
91         pressed_down = 0;
92         flag = _state;
93         group = _group;
94 };
95
96 void UI_RADIO::destroy()
97 {
98         if (text)
99                 free(text);
100
101         UI_GADGET::destroy();
102 }
103
104 void UI_RADIO::draw()
105 {
106         int offset;
107
108
109         if ( uses_bmaps ) {
110
111                 if ( disabled_flag ) {
112                         if ( flag ) {
113                                 if ( bmap_ids[RADIO_DISABLED_MARKED] != -1 ) {
114                                         gr_set_bitmap(bmap_ids[RADIO_DISABLED_MARKED]);
115                                         gr_bitmap(x,y);
116                                 }
117                         }
118                         else {
119                                 if ( bmap_ids[RADIO_DISABLED_CLEAR] != -1 ) {
120                                         gr_set_bitmap(bmap_ids[RADIO_DISABLED_CLEAR]);
121                                         gr_bitmap(x,y);
122                                 }
123                         }
124                 }
125                 else {          // not disabled
126                         if ( position == 0 )    {       // up
127                                 if ( flag ) {                   // marked
128                                         if ( bmap_ids[RADIO_UP_MARKED] != -1 ) {
129                                                 gr_set_bitmap(bmap_ids[RADIO_UP_MARKED]);
130                                                 gr_bitmap(x,y);
131                                         }
132                                 }
133                                 else {                                  // not marked
134                                         if ( bmap_ids[RADIO_UP_CLEAR] != -1 ) {
135                                                 gr_set_bitmap(bmap_ids[RADIO_UP_CLEAR]);
136                                                 gr_bitmap(x,y);
137                                         }
138                                 }
139                         }
140                         else {                                          // down 
141                                 if ( flag ) {                   // marked
142                                         if ( bmap_ids[RADIO_DOWN_MARKED] != -1 ) {
143                                                 gr_set_bitmap(bmap_ids[RADIO_DOWN_MARKED]);
144                                                 gr_bitmap(x,y);
145                                         }
146                                 }
147                                 else {                                  // not marked
148                                         if ( bmap_ids[RADIO_DOWN_CLEAR] != -1 ) {
149                                                 gr_set_bitmap(bmap_ids[RADIO_DOWN_CLEAR]);
150                                                 gr_bitmap(x,y);
151                                         }
152                                 }
153                         }
154                 }
155         }
156         else {
157                 gr_set_font(my_wnd->f_id);
158                 gr_set_clip( x, y, w, h );
159
160                 if (position == 0 )     {
161                         ui_draw_box_out( 0, 0, w-1, h-1 );
162                         offset = 0;
163                 } else {
164                         ui_draw_box_in( 0, 0, w-1, h-1 );
165                         offset = 1;
166                 }
167
168                 if (disabled_flag)
169                         gr_set_color_fast(&CDARK_GRAY);
170                 else if (my_wnd->selected_gadget == this)
171                         gr_set_color_fast(&CBRIGHT_GREEN);
172                 else 
173                         gr_set_color_fast(&CGREEN);
174
175         //      if (flag)
176         //              ui_string_centered(  Middle(w)+offset, Middle(h)+offset, "*" );
177         //      else
178         //              ui_string_centered(  Middle(w)+offset, Middle(h)+offset, "o" );
179                 if (flag)       {
180                         gr_circle( Middle(w)+offset, Middle(h)+offset, 8 );
181                 } else {
182                         gr_circle( Middle(w)+offset, Middle(h)+offset, 8 );
183                         gr_set_color_fast( &CWHITE );
184                         gr_circle( Middle(w)+offset, Middle(h)+offset, 4 );
185                 }
186
187                 if (disabled_flag)
188                         gr_set_color_fast(&CDARK_GRAY);
189                 else if (my_wnd->selected_gadget == this)
190                         gr_set_color_fast(&CBRIGHT_GREEN);
191                 else 
192                         gr_set_color_fast(&CGREEN);
193
194                 if ( text )     {
195                         gr_reset_clip();
196                         gr_string( x+w+4, y+2, text );
197                 }
198         }
199 }
200
201 void UI_RADIO::process(int focus)
202 {
203         int OnMe, oldposition;
204
205         if (disabled_flag)      {
206                 position = 0;
207                 return;
208         }
209
210         if (my_wnd->selected_gadget == this)
211                 focus = 1;
212
213         OnMe = is_mouse_on();
214
215         oldposition = position;
216
217         if (B1_PRESSED && OnMe) {
218                 position = 1;
219         } else  {
220                 position = 0;
221         }
222
223         if (my_wnd->keypress == hotkey) {
224                 position = 2;
225                 my_wnd->last_keypress = 0;
226         }
227                 
228         if ( focus && ((my_wnd->keypress == KEY_SPACEBAR) || (my_wnd->keypress == KEY_ENTER)) )
229                 position = 2;
230
231         if (focus)
232                 if ( (oldposition == 2) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER]) )
233                         position = 2;
234
235         pressed_down = 0;
236
237         if (position) {
238                 if ( (oldposition == 1) && OnMe )
239                         pressed_down = 1;
240                 if ( (oldposition == 2) && focus )
241                         pressed_down = 1;
242         }
243
244         if (pressed_down && user_function) {
245                 user_function();
246         }
247
248         if (pressed_down && (flag == 0)) {
249                 UI_GADGET *tmp = (UI_GADGET *) next;
250                 UI_RADIO *tmpr;
251
252                 while (tmp != this)     {
253                         if (tmp->kind == UI_KIND_RADIO) {
254                                 tmpr = (UI_RADIO *) tmp;
255                                 if ((tmpr->group == group) && tmpr->flag) {
256                                         tmpr->flag = 0;
257                                         tmpr->pressed_down = 0;
258                                 }
259                         }
260
261                         tmp = tmp->next;
262                 }
263
264                 flag = 1;
265         }
266 }
267
268 int UI_RADIO::changed()
269 {
270         return pressed_down;
271 }
272
273 int UI_RADIO::checked()
274 {
275         return flag;
276 }
277
278