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 | * |
||
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 | void ui_draw_radio( UI_DIALOG *dlg, UI_GADGET_RADIO * radio ) |
||
43 | { |
||
44 | #if 0 //ndef OGL |
||
45 | if ((radio->status==1) || (radio->position != radio->oldposition)) |
||
46 | #endif |
||
47 | { |
||
48 | radio->status = 0; |
||
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 | 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 * text) |
||
70 | { |
||
71 | std::unique_ptr<UI_GADGET_RADIO> radio{ui_gadget_add<UI_GADGET_RADIO>(dlg, x, y, x+w-1, y+h-1)}; |
||
72 | radio->text = RAIIdmem<char[]>(d_strdup(text)); |
||
73 | radio->width = w; |
||
74 | radio->height = h; |
||
75 | radio->position = 0; |
||
76 | radio->oldposition = 0; |
||
77 | radio->pressed = 0; |
||
78 | radio->flag = 0; |
||
79 | radio->group = group; |
||
80 | return radio; |
||
81 | } |
||
82 | |||
83 | window_event_result ui_radio_do( UI_DIALOG *dlg, UI_GADGET_RADIO * radio,const d_event &event ) |
||
84 | { |
||
85 | UI_GADGET * tmp; |
||
86 | radio->oldposition = radio->position; |
||
87 | radio->pressed = 0; |
||
88 | |||
89 | window_event_result rval = window_event_result::ignored; |
||
90 | if (event.type == EVENT_MOUSE_BUTTON_DOWN || event.type == EVENT_MOUSE_BUTTON_UP) |
||
91 | { |
||
92 | int OnMe; |
||
93 | |||
94 | OnMe = ui_mouse_on_gadget( radio ); |
||
95 | |||
96 | if ( B1_JUST_PRESSED && OnMe) |
||
97 | { |
||
98 | radio->position = 1; |
||
99 | rval = window_event_result::handled; |
||
100 | } |
||
101 | else if (B1_JUST_RELEASED) |
||
102 | { |
||
103 | if ((radio->position==1) && OnMe) |
||
104 | radio->pressed = 1; |
||
105 | |||
106 | radio->position = 0; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | |||
111 | if (event.type == EVENT_KEY_COMMAND) |
||
112 | { |
||
113 | int key; |
||
114 | |||
115 | key = event_key_get(event); |
||
116 | |||
117 | if ((dlg->keyboard_focus_gadget==radio) && ((key==KEY_SPACEBAR) || (key==KEY_ENTER)) ) |
||
118 | { |
||
119 | radio->position = 2; |
||
120 | rval = window_event_result::handled; |
||
121 | } |
||
122 | } |
||
123 | else if (event.type == EVENT_KEY_RELEASE) |
||
124 | { |
||
125 | int key; |
||
126 | |||
127 | key = event_key_get(event); |
||
128 | |||
129 | radio->position = 0; |
||
130 | |||
131 | if ((dlg->keyboard_focus_gadget==radio) && ((key==KEY_SPACEBAR) || (key==KEY_ENTER)) ) |
||
132 | radio->pressed = 1; |
||
133 | } |
||
134 | |||
135 | if ((radio->pressed == 1) && (radio->flag==0)) |
||
136 | { |
||
137 | tmp = radio->next; |
||
138 | |||
139 | while (tmp != radio ) |
||
140 | { |
||
141 | if (tmp->kind==UI_GADGET_RADIO::s_kind) |
||
142 | { |
||
143 | auto tmpr = static_cast<UI_GADGET_RADIO *>(tmp); |
||
144 | if ((tmpr->group == radio->group ) && (tmpr->flag) ) |
||
145 | { |
||
146 | tmpr->flag = 0; |
||
147 | tmpr->status = 1; |
||
148 | tmpr->pressed = 0; |
||
149 | } |
||
150 | } |
||
151 | tmp = tmp->next; |
||
152 | } |
||
153 | radio->flag = 1; |
||
154 | rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, radio); |
||
155 | if (rval == window_event_result::ignored) |
||
156 | rval = window_event_result::handled; |
||
157 | } |
||
158 | |||
159 | if (event.type == EVENT_WINDOW_DRAW) |
||
160 | ui_draw_radio( dlg, radio ); |
||
161 | |||
162 | return rval; |
||
163 | } |
||
164 | |||
165 | void ui_radio_set_value(UI_GADGET_RADIO *radio, int value) |
||
166 | { |
||
167 | value = value != 0; |
||
168 | if (radio->flag == value) |
||
169 | return; |
||
170 | |||
171 | radio->flag = value; |
||
172 | radio->status = 1; // redraw |
||
173 | |||
174 | for (auto tmp = radio; (tmp = static_cast<UI_GADGET_RADIO *>(tmp->next)) != radio;) |
||
175 | { |
||
176 | if ((tmp->kind == UI_GADGET_RADIO::s_kind) && (tmp->group == radio->group) && tmp->flag) |
||
177 | { |
||
178 | tmp->flag = 0; |
||
179 | tmp->status = 1; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | } |