]> icculus.org git repositories - btb/d2x.git/blob - ui/checkbox.c
remove rcs tags
[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 "pstypes.h"
24 #include "gr.h"
25 #include "ui.h"
26 #include "key.h"
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         keypress = keypress;
93
94         OnMe = ui_mouse_on_gadget( (UI_GADGET *)checkbox );
95
96         checkbox->oldposition = checkbox->position;
97
98         if ((selected_gadget != NULL) && (selected_gadget->kind !=5))
99                 ButtonLastSelected = 0;
100         else
101                 ButtonLastSelected = 1;
102
103         if ( B1_PRESSED && OnMe && ButtonLastSelected )
104         {
105                 checkbox->position = 1;
106         } else  {
107                 checkbox->position = 0;
108         }
109
110
111         if ((CurWindow->keyboard_focus_gadget==(UI_GADGET *)checkbox) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER] ) )
112                 checkbox->position = 2;
113
114         if ((checkbox->position==0) && (checkbox->oldposition==1) && OnMe )
115                 checkbox->pressed = 1;
116         else if ((checkbox->position==0) && (checkbox->oldposition==2) && (CurWindow->keyboard_focus_gadget==(UI_GADGET *)checkbox) )
117                 checkbox->pressed = 1;
118         else
119                 checkbox->pressed = 0;
120
121         if (checkbox->pressed == 1)
122                 checkbox->flag ^= 1;
123
124         ui_draw_checkbox( checkbox );
125
126 }
127
128 void ui_checkbox_check(UI_GADGET_CHECKBOX * checkbox, sbyte check)
129 {
130         check = check != 0;
131         if (checkbox->flag == check)
132                 return;
133         
134         checkbox->flag = check;
135         checkbox->status = 1;   // redraw
136 }