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