Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | /* |
2 | * Portions of this file are copyright Rebirth contributors and licensed as |
||
3 | * described in COPYING.txt. |
||
4 | * Portions of this file are copyright Parallax Software and licensed |
||
5 | * according to the Parallax license below. |
||
6 | * See COPYING.txt for license details. |
||
7 | |||
8 | THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
||
9 | SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
||
10 | END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
||
11 | ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
||
12 | IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
||
13 | SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
||
14 | FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
||
15 | CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
||
16 | AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
||
17 | COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
||
18 | */ |
||
19 | |||
20 | |||
21 | #include <stdlib.h> |
||
22 | #include <string.h> |
||
23 | |||
24 | #include "u_mem.h" |
||
25 | #include "maths.h" |
||
26 | #include "pstypes.h" |
||
27 | #include "event.h" |
||
28 | #include "gr.h" |
||
29 | #include "ui.h" |
||
30 | #include "mouse.h" |
||
31 | #include "key.h" |
||
32 | |||
33 | namespace dcx { |
||
34 | |||
35 | #define Middle(x) ((2*(x)+1)/4) |
||
36 | |||
37 | void ui_draw_checkbox( UI_DIALOG *dlg, UI_GADGET_CHECKBOX * checkbox ) |
||
38 | { |
||
39 | #if 0 //ndef OGL |
||
40 | if ((checkbox->status==1) || (checkbox->position != checkbox->oldposition)) |
||
41 | #endif |
||
42 | { |
||
43 | checkbox->status = 0; |
||
44 | |||
45 | gr_set_current_canvas( checkbox->canvas ); |
||
46 | auto &canvas = *grd_curcanv; |
||
47 | gr_set_fontcolor(canvas, dlg->keyboard_focus_gadget == checkbox |
||
48 | ? CRED |
||
49 | : CBLACK, -1); |
||
50 | |||
51 | unsigned offset; |
||
52 | if (checkbox->position == 0 ) |
||
53 | { |
||
54 | ui_draw_box_out(canvas, 0, 0, checkbox->width-1, checkbox->height-1); |
||
55 | offset = 0; |
||
56 | } else { |
||
57 | ui_draw_box_in(canvas, 0, 0, checkbox->width-1, checkbox->height-1); |
||
58 | offset = 1; |
||
59 | } |
||
60 | ui_string_centered(canvas, Middle(checkbox->width) + offset, Middle(checkbox->height) + offset, checkbox->flag ? "X" : " "); |
||
61 | gr_ustring(canvas, *canvas.cv_font, checkbox->width + 4, 2, checkbox->text.get()); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | |||
66 | std::unique_ptr<UI_GADGET_CHECKBOX> ui_add_gadget_checkbox(UI_DIALOG * dlg, short x, short y, short w, short h, short group, const char * text) |
||
67 | { |
||
68 | std::unique_ptr<UI_GADGET_CHECKBOX> checkbox{ui_gadget_add<UI_GADGET_CHECKBOX>(dlg, x, y, x+w-1, y+h-1)}; |
||
69 | auto ltext = strlen(text) + 1; |
||
70 | MALLOC(checkbox->text, char[], ltext + 4); |
||
71 | memcpy(checkbox->text.get(), text, ltext); |
||
72 | checkbox->width = w; |
||
73 | checkbox->height = h; |
||
74 | checkbox->position = 0; |
||
75 | checkbox->oldposition = 0; |
||
76 | checkbox->pressed = 0; |
||
77 | checkbox->flag = 0; |
||
78 | checkbox->group = group; |
||
79 | return checkbox; |
||
80 | } |
||
81 | |||
82 | window_event_result ui_checkbox_do( UI_DIALOG *dlg, UI_GADGET_CHECKBOX * checkbox,const d_event &event ) |
||
83 | { |
||
84 | checkbox->oldposition = checkbox->position; |
||
85 | checkbox->pressed = 0; |
||
86 | |||
87 | if (event.type == EVENT_MOUSE_BUTTON_DOWN || event.type == EVENT_MOUSE_BUTTON_UP) |
||
88 | { |
||
89 | int OnMe; |
||
90 | |||
91 | OnMe = ui_mouse_on_gadget( checkbox ); |
||
92 | |||
93 | if (B1_JUST_PRESSED && OnMe) |
||
94 | { |
||
95 | checkbox->position = 1; |
||
96 | return window_event_result::handled; |
||
97 | } |
||
98 | else if (B1_JUST_RELEASED) |
||
99 | { |
||
100 | if ((checkbox->position==1) && OnMe) |
||
101 | checkbox->pressed = 1; |
||
102 | |||
103 | checkbox->position = 0; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | |||
108 | if (event.type == EVENT_KEY_COMMAND) |
||
109 | { |
||
110 | int key; |
||
111 | |||
112 | key = event_key_get(event); |
||
113 | |||
114 | if ((dlg->keyboard_focus_gadget==checkbox) && ((key==KEY_SPACEBAR) || (key==KEY_ENTER)) ) |
||
115 | { |
||
116 | checkbox->position = 2; |
||
117 | return window_event_result::handled; |
||
118 | } |
||
119 | } |
||
120 | else if (event.type == EVENT_KEY_RELEASE) |
||
121 | { |
||
122 | int key; |
||
123 | |||
124 | key = event_key_get(event); |
||
125 | |||
126 | checkbox->position = 0; |
||
127 | |||
128 | if ((dlg->keyboard_focus_gadget==checkbox) && ((key==KEY_SPACEBAR) || (key==KEY_ENTER)) ) |
||
129 | checkbox->pressed = 1; |
||
130 | } |
||
131 | |||
132 | if (checkbox->pressed == 1) |
||
133 | { |
||
134 | checkbox->flag ^= 1; |
||
135 | auto rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, checkbox); |
||
136 | if (rval == window_event_result::ignored) |
||
137 | rval = window_event_result::handled; |
||
138 | return rval; |
||
139 | } |
||
140 | |||
141 | if (event.type == EVENT_WINDOW_DRAW) |
||
142 | ui_draw_checkbox( dlg, checkbox ); |
||
143 | |||
144 | return window_event_result::ignored; |
||
145 | } |
||
146 | |||
147 | void ui_checkbox_check(UI_GADGET_CHECKBOX * checkbox, int check) |
||
148 | { |
||
149 | check = check != 0; |
||
150 | if (checkbox->flag == check) |
||
151 | return; |
||
152 | |||
153 | checkbox->flag = check; |
||
154 | checkbox->status = 1; // redraw |
||
155 | } |
||
156 | |||
157 | } |