]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/checkbox.cpp
const-char warning fixes
[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
79 void UI_CHECKBOX::create(UI_WINDOW *wnd, const char *_text, int _x, int _y, int _state )
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_CHECKBOX, _x, _y, _w, _h );
93
94         position = 0;
95         pressed_down = 0;
96         flag = _state;
97 };
98
99 void UI_CHECKBOX::destroy()
100 {
101         if (text)
102                 free(text);
103
104         UI_GADGET::destroy();
105 }
106
107
108 void UI_CHECKBOX::draw()
109 {
110         int offset;
111
112         if ( uses_bmaps ) {
113
114                 if ( disabled_flag ) {
115                         if ( flag ) {
116                                 if ( bmap_ids[CBOX_DISABLED_MARKED] != -1 ) {
117                                         gr_set_bitmap(bmap_ids[CBOX_DISABLED_MARKED], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
118                                         gr_bitmap(x,y);
119                                 }
120                         }
121                         else {
122                                 if ( bmap_ids[CBOX_DISABLED_CLEAR] != -1 ) {
123                                         gr_set_bitmap(bmap_ids[CBOX_DISABLED_CLEAR], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
124                                         gr_bitmap(x,y);
125                                 }
126                         }
127                 }
128                 else {          // not disabled
129                         if ( position == 0 )    {       // up
130                                 if ( flag ) {                   // marked
131                                         if ( bmap_ids[CBOX_UP_MARKED] != -1 ) {
132                                                 gr_set_bitmap(bmap_ids[CBOX_UP_MARKED], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
133                                                 gr_bitmap(x,y);
134                                         }
135                                 }
136                                 else {                                  // not marked
137                                         if ( bmap_ids[CBOX_UP_CLEAR] != -1 ) {
138                                                 gr_set_bitmap(bmap_ids[CBOX_UP_CLEAR], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
139                                                 gr_bitmap(x,y);
140                                         }
141                                 }
142                         }
143                         else {                                          // down 
144                                 if ( flag ) {                   // marked
145                                         if ( bmap_ids[CBOX_DOWN_MARKED] != -1 ) {
146                                                 gr_set_bitmap(bmap_ids[CBOX_DOWN_MARKED], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
147                                                 gr_bitmap(x,y);
148                                         }
149                                 }
150                                 else {                                  // not marked
151                                         if ( bmap_ids[CBOX_DOWN_CLEAR] != -1 ) {
152                                                 gr_set_bitmap(bmap_ids[CBOX_DOWN_CLEAR], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
153                                                 gr_bitmap(x,y);
154                                         }
155                                 }
156                         }
157                 }
158
159         } else {
160                 gr_set_font(my_wnd->f_id);
161                 gr_set_clip( x, y, w, h );
162
163                 if (position == 0 )     {
164                         ui_draw_box_out( 0, 0, w-1, h-1 );
165                         offset = 0;
166
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( &CGRAY );
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, "X" );
181                 else
182                         ui_string_centered( Middle(w) + offset, Middle(h) + offset, " " );
183
184                 if (text) {
185                         gr_reset_clip();
186                         gr_string( x+w+4, y+2, text );
187                 }
188         }
189 }
190
191 void UI_CHECKBOX::process(int focus)
192 {
193         int OnMe, oldposition;
194
195         if (disabled_flag) {
196                 position = 0;
197                 return;
198         }
199
200         if (my_wnd->selected_gadget == this)
201                 focus = 1;
202
203         OnMe = is_mouse_on();
204
205         oldposition = position;
206
207         if ( B1_PRESSED && OnMe ) {
208                 position = 1;
209         } else  {
210                 position = 0;
211         }
212
213         if (my_wnd->keypress == hotkey )        {
214                 position = 2;
215                 my_wnd->last_keypress = 0;
216         }
217                 
218         if ( focus && ((my_wnd->keypress == KEY_SPACEBAR) || (my_wnd->keypress == KEY_ENTER)) )
219                 position = 2;
220
221         if (focus)
222                 if ( (oldposition == 2) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER]) )
223                         position = 2;
224
225         pressed_down = 0;
226
227         if (position == 0) {
228                 if ( (oldposition == 1) && OnMe ){
229                         pressed_down = 1;
230                 }
231                 if ( (oldposition == 2) && focus ){
232                         pressed_down = 1;
233                 }
234         }
235
236         if (pressed_down && user_function )     {
237                 user_function();
238         }
239
240         if (pressed_down)
241                 flag = !flag;
242 }
243
244 int UI_CHECKBOX::changed()
245 {
246         return pressed_down;
247 }
248
249 int UI_CHECKBOX::checked()
250 {
251         return flag;
252 }
253
254 void UI_CHECKBOX::set_state(int _state)
255 {
256         flag = _state;
257 }
258