]> icculus.org git repositories - btb/d2x.git/blob - ui/checkbox.c
more header cleanup
[btb/d2x.git] / ui / checkbox.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include "conf.h"
16 #endif
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "u_mem.h"
22 #include "fix.h"
23 #include "gr.h"
24 #include "ui.h"
25 #include "key.h"
26
27
28 #define Middle(x) ((2*(x)+1)/4)
29
30 void ui_draw_checkbox( UI_GADGET_CHECKBOX * checkbox )
31 {
32
33         if ((checkbox->status==1) || (checkbox->position != checkbox->oldposition))
34         {
35                 checkbox->status = 0;
36
37                 ui_mouse_hide();
38                 gr_set_current_canvas( checkbox->canvas );
39
40                 if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)checkbox)
41                         gr_set_fontcolor( CRED, -1 );
42                 else
43                         gr_set_fontcolor( CBLACK, -1 );
44
45                 if (checkbox->position == 0 )
46                 {
47                         ui_draw_box_out( 0, 0, checkbox->width-1, checkbox->height-1 );
48                         if (checkbox->flag)
49                                 ui_string_centered(  Middle(checkbox->width), Middle(checkbox->height), "X" );
50                         else
51                                 ui_string_centered(  Middle(checkbox->width), Middle(checkbox->height), " " );
52                 } else {
53                         ui_draw_box_in( 0, 0, checkbox->width-1, checkbox->height-1 );
54                         if (checkbox->flag)
55                                 ui_string_centered(  Middle(checkbox->width)+1, Middle(checkbox->height)+1, "X" );
56                         else
57                                 ui_string_centered(  Middle(checkbox->width)+1, Middle(checkbox->height)+1, " " );
58                 }
59
60                 gr_ustring( checkbox->width+4, 2, checkbox->text );
61
62                 ui_mouse_show();
63         }
64 }
65
66
67 UI_GADGET_CHECKBOX * ui_add_gadget_checkbox( UI_WINDOW * wnd, short x, short y, short w, short h, short group, char * text )
68 {
69         UI_GADGET_CHECKBOX * checkbox;
70
71         checkbox = (UI_GADGET_CHECKBOX *)ui_gadget_add( wnd, 5, x, y, x+w-1, y+h-1 );
72
73         checkbox->text = d_malloc(strlen(text) + 5);
74         strcpy(checkbox->text,text);
75         checkbox->width = w;
76         checkbox->height = h;
77         checkbox->position = 0;
78         checkbox->oldposition = 0;
79         checkbox->pressed = 0;
80         checkbox->flag = 0;
81         checkbox->group = group;
82
83         return checkbox;
84
85 }
86
87
88 void ui_checkbox_do( UI_GADGET_CHECKBOX * checkbox, int keypress )
89 {
90         int OnMe, ButtonLastSelected;
91
92         OnMe = ui_mouse_on_gadget( (UI_GADGET *)checkbox );
93
94         checkbox->oldposition = checkbox->position;
95
96         if ((selected_gadget != NULL) && (selected_gadget->kind !=5))
97                 ButtonLastSelected = 0;
98         else
99                 ButtonLastSelected = 1;
100
101         if ( B1_PRESSED && OnMe && ButtonLastSelected )
102         {
103                 checkbox->position = 1;
104         } else  {
105                 checkbox->position = 0;
106         }
107
108
109         if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)checkbox) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
110                 checkbox->position = 2;
111
112         if ((checkbox->position==0) && (checkbox->oldposition==1) && OnMe )
113                 checkbox->pressed = 1;
114         else if ((checkbox->position==0) && (checkbox->oldposition==2) && (CurWindow->keyboard_focus_gadget==(UI_GADGET *)checkbox) )
115                 checkbox->pressed = 1;
116         else
117                 checkbox->pressed = 0;
118
119         if (checkbox->pressed == 1)
120                 checkbox->flag ^= 1;
121
122         ui_draw_checkbox( checkbox );
123
124 }
125
126 void ui_checkbox_check(UI_GADGET_CHECKBOX * checkbox, sbyte check)
127 {
128         check = check != 0;
129         if (checkbox->flag == check)
130                 return;
131         
132         checkbox->flag = check;
133         checkbox->status = 1;   // redraw
134 }