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