]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/checkbox.cpp
fix event and state help text
[taylor/freespace2.git] / src / ui / checkbox.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/CHECKBOX.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Code to handle checkboxes.
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  * 5     2/11/99 3:08p Dave
29  * PXO refresh button. Very preliminary squad war support.
30  * 
31  * 4     12/02/98 5:47p Dave
32  * Put in interface xstr code. Converted barracks screen to new format.
33  * 
34  * 3     10/13/98 9:29a Dave
35  * Started neatening up freespace.h. Many variables renamed and
36  * reorganized. Added AlphaColors.[h,cpp]
37  * 
38  * 2     10/07/98 10:54a Dave
39  * Initial checkin.
40  * 
41  * 1     10/07/98 10:51a Dave
42  * 
43  * 9     2/03/98 4:21p Hoffoss
44  * Made UI controls draw white text when disabled.
45  * 
46  * 8     1/14/98 6:43p Hoffoss
47  * Massive changes to UI code.  A lot cleaner and better now.  Did all
48  * this to get the new UI_DOT_SLIDER to work properly, which the old code
49  * wasn't flexible enough to handle.
50  * 
51  * 7     9/07/97 10:05p Lawrance
52  * don't set hotspot_num, done in gadget constructor
53  * 
54  * 6     6/12/97 12:39p John
55  * made ui use freespace colors
56  * 
57  * 5     6/11/97 1:13p John
58  * Started fixing all the text colors in the game.
59  * 
60  * 4     5/22/97 5:36p Lawrance
61  * allowing custom art for scrollbars
62  * 
63  * 3     12/08/96 1:58a Lawrance
64  * changed text color for check-box from black to green
65  * 
66  * 2     11/29/96 6:08p Lawrance
67  * enabled check-boxes to be set to a specific value outside of the
68  * create() function
69  * 
70  * 1     11/14/96 6:55p John
71  *
72  * $NoKeywords: $
73  */
74
75 #include "uidefs.h"
76 #include "ui.h"
77 #include "alphacolors.h"
78 #include "font.h"
79
80 void UI_CHECKBOX::create(UI_WINDOW *wnd, const char *_text, int _x, int _y, int _state )
81 {
82         int _w, _h;
83
84 //      gr_get_string_size( &_w, &_h, "X" );
85         _w = 18;
86         _h = 18;
87
88         if ( _text )    
89                 text = strdup( _text );
90         else
91                 text = NULL;
92
93         base_create( wnd, UI_KIND_CHECKBOX, _x, _y, _w, _h );
94
95         position = 0;
96         pressed_down = 0;
97         flag = _state;
98 };
99
100 void UI_CHECKBOX::destroy()
101 {
102         if (text)
103                 free(text);
104
105         UI_GADGET::destroy();
106 }
107
108
109 void UI_CHECKBOX::draw()
110 {
111         int offset;
112
113         if ( uses_bmaps ) {
114
115                 if ( disabled_flag ) {
116                         if ( flag ) {
117                                 if ( bmap_ids[CBOX_DISABLED_MARKED] != -1 ) {
118                                         gr_set_bitmap(bmap_ids[CBOX_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[CBOX_DISABLED_CLEAR] != -1 ) {
124                                         gr_set_bitmap(bmap_ids[CBOX_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[CBOX_UP_MARKED] != -1 ) {
133                                                 gr_set_bitmap(bmap_ids[CBOX_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[CBOX_UP_CLEAR] != -1 ) {
139                                                 gr_set_bitmap(bmap_ids[CBOX_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[CBOX_DOWN_MARKED] != -1 ) {
147                                                 gr_set_bitmap(bmap_ids[CBOX_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[CBOX_DOWN_CLEAR] != -1 ) {
153                                                 gr_set_bitmap(bmap_ids[CBOX_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
168                 } else {
169                         ui_draw_box_in( 0, 0, w-1, h-1 );
170                         offset = 1;
171                 }
172
173                 if (disabled_flag)
174                         gr_set_color_fast( &CGRAY );
175                 else if (my_wnd->selected_gadget == this)
176                         gr_set_color_fast( &CBRIGHT_GREEN );
177                 else 
178                         gr_set_color_fast( &CGREEN );
179
180                 if (flag)
181                         ui_string_centered( Middle(w) + offset, Middle(h) + offset, "X" );
182                 else
183                         ui_string_centered( Middle(w) + offset, Middle(h) + offset, " " );
184
185                 if (text) {
186                         gr_reset_clip();
187                         gr_string( x+w+4, y+2, text );
188                 }
189         }
190 }
191
192 void UI_CHECKBOX::process(int focus)
193 {
194         int OnMe, oldposition;
195
196         if (disabled_flag) {
197                 position = 0;
198                 return;
199         }
200
201         if (my_wnd->selected_gadget == this)
202                 focus = 1;
203
204         OnMe = is_mouse_on();
205
206         oldposition = position;
207
208         if ( B1_PRESSED && OnMe ) {
209                 position = 1;
210         } else  {
211                 position = 0;
212         }
213
214         if (my_wnd->keypress == hotkey )        {
215                 position = 2;
216                 my_wnd->last_keypress = 0;
217         }
218                 
219         if ( focus && ((my_wnd->keypress == SDLK_SPACE) || (my_wnd->keypress == SDLK_RETURN)) )
220                 position = 2;
221
222         if (focus)
223                 if ( (oldposition == 2) && (key_pressed(SDLK_SPACE) || key_pressed(SDLK_RETURN)) )
224                         position = 2;
225
226         pressed_down = 0;
227
228         if (position == 0) {
229                 if ( (oldposition == 1) && OnMe ){
230                         pressed_down = 1;
231                 }
232                 if ( (oldposition == 2) && focus ){
233                         pressed_down = 1;
234                 }
235         }
236
237         if (pressed_down && user_function )     {
238                 user_function();
239         }
240
241         if (pressed_down)
242                 flag = !flag;
243 }
244
245 int UI_CHECKBOX::changed()
246 {
247         return pressed_down;
248 }
249
250 int UI_CHECKBOX::checked()
251 {
252         return flag;
253 }
254
255 void UI_CHECKBOX::set_state(int _state)
256 {
257         flag = _state;
258 }
259