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