Debian Codegraph @

common/ui/radio.cpp

raw
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 *
22 * Radio box gadget stuff.
23 *
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include "maths.h"
29 #include "pstypes.h"
30 #include "event.h"
31 #include "gr.h"
32 #include "ui.h"
33 #include "mouse.h"
34 #include "key.h"
35 #include "u_mem.h"
36 #include "strutil.h"
37
38 namespace dcx {
39
40 #define Middle(x) ((2*(x)+1)/4)
41
42 namespace {
43
44 void ui_draw_radio(UI_DIALOG &dlg, UI_GADGET_RADIO &radio)
45 {
46 #if 0 //ndef OGL
47 if ((radio->status==1) || (radio->position != radio->oldposition))
48 #endif
49 {
50 gr_set_current_canvas(radio.canvas);
51 auto &canvas = *grd_curcanv;
52 gr_set_fontcolor(canvas, dlg.keyboard_focus_gadget == &radio ? CRED : CBLACK, -1);
53
54 unsigned bias;
55 if (radio.position == 0)
56 {
57 ui_draw_box_out(canvas, 0, 0, radio.width - 1, radio.height - 1);
58 bias = 0;
59 } else {
60 ui_draw_box_in(canvas, 0, 0, radio.width - 1, radio.height - 1);
61 bias = 1;
62 }
63 ui_string_centered(canvas, Middle(radio.width) + bias, Middle(radio.height) + bias, radio.flag ? "O" : " ");
64 gr_ustring(canvas, *canvas.cv_font, radio.width + 4, 2, radio.text.get());
65 }
66 }
67
68 }
69
70
71 std::unique_ptr<UI_GADGET_RADIO> ui_add_gadget_radio(UI_DIALOG &dlg, short x, short y, short w, short h, short group, const char *const text)
72 {
73 auto radio = ui_gadget_add<UI_GADGET_RADIO>(dlg, x, y, x + w - 1, y + h - 1);
74 radio->text = d_strdup(text);
75 radio->width = w;
76 radio->height = h;
77 radio->position = 0;
78 radio->oldposition = 0;
79 radio->pressed = 0;
80 radio->flag = 0;
81 radio->group = group;
82 return radio;
83 }
84
85 window_event_result UI_GADGET_RADIO::event_handler(UI_DIALOG &dlg, const d_event &event)
86 {
87 oldposition = position;
88 pressed = 0;
89
90 window_event_result rval = window_event_result::ignored;
91 if (event.type == event_type::mouse_button_down || event.type == event_type::mouse_button_up)
92 {
93 const auto OnMe = ui_mouse_on_gadget(*this);
94
95 if ( B1_JUST_PRESSED && OnMe)
96 {
97 position = 1;
98 rval = window_event_result::handled;
99 }
100 else if (B1_JUST_RELEASED)
101 {
102 if ((position==1) && OnMe)
103 pressed = 1;
104
105 position = 0;
106 }
107 }
108
109 if (event.type == event_type::key_command)
110 {
111 const auto key = event_key_get(event);
112 if (dlg.keyboard_focus_gadget == this && (key == KEY_SPACEBAR || key==KEY_ENTER))
113 {
114 position = 2;
115 rval = window_event_result::handled;
116 }
117 }
118 else if (event.type == event_type::key_release)
119 {
120 const auto key = event_key_get(event);
121 position = 0;
122 if (dlg.keyboard_focus_gadget == this && (key == KEY_SPACEBAR || key == KEY_ENTER))
123 pressed = 1;
124 }
125 if (pressed == 1 && flag == 0)
126 {
127 auto tmp = next;
128
129 while (tmp != this)
130 {
131 if (tmp->kind==UI_GADGET_RADIO::s_kind)
132 {
133 auto tmpr = static_cast<UI_GADGET_RADIO *>(tmp);
134 if ((tmpr->group == group ) && (tmpr->flag) )
135 {
136 tmpr->flag = 0;
137 tmpr->pressed = 0;
138 }
139 }
140 tmp = tmp->next;
141 }
142 flag = 1;
143 rval = ui_gadget_send_event(dlg, event_type::ui_gadget_pressed, *this);
144 if (rval == window_event_result::ignored)
145 rval = window_event_result::handled;
146 }
147
148 if (event.type == event_type::window_draw)
149 ui_draw_radio(dlg, *this);
150
151 return rval;
152 }
153
154 void ui_radio_set_value(UI_GADGET_RADIO &radio, int value)
155 {
156 value = value != 0;
157 if (radio.flag == value)
158 return;
159
160 radio.flag = value;
161 for (auto tmp = &radio; (tmp = static_cast<UI_GADGET_RADIO *>(tmp->next)) != &radio;)
162 {
163 if (tmp->kind == UI_GADGET_RADIO::s_kind && tmp->group == radio.group && tmp->flag)
164 {
165 tmp->flag = 0;
166 }
167 }
168 }
169
170 }