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