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