Debian Codegraph @

common/ui/button.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 * Routines for manipulating the button gadgets.
23 *
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "strutil.h"
30 #include "u_mem.h"
31 #include "maths.h"
32 #include "pstypes.h"
33 #include "event.h"
34 #include "gr.h"
35 #include "ui.h"
36 #include "mouse.h"
37 #include "key.h"
38
39 namespace dcx {
40
41 #define Middle(x) ((2*(x)+1)/4)
42
43 #define BUTTON_EXTRA_WIDTH 15
44 #define BUTTON_EXTRA_HEIGHT 2
45
46 int ui_button_any_drawn{0};
47
48 gr_string_size ui_get_button_size(const grs_font &cv_font, const char *text)
49 {
50 const auto r = gr_get_string_size(cv_font, text);
51 return {r.width + BUTTON_EXTRA_WIDTH * 2 + 6, r.height + BUTTON_EXTRA_HEIGHT * 2 + 6};
52 }
53
54 namespace {
55
56 void ui_draw_button(UI_DIALOG &dlg, UI_GADGET_BUTTON &button)
57 {
58 #if 0 //ndef OGL
59 if ((button.status==1) || (button.position != button.oldposition))
60 #endif
61 {
62 ui_button_any_drawn = 1;
63 gr_set_current_canvas(button.canvas);
64 auto &canvas = *grd_curcanv;
65 color_palette_index color{0};
66
67 gr_set_fontcolor(canvas, dlg.keyboard_focus_gadget == &button
68 ? CRED
69 : (!button.user_function && button.dim_if_no_function
70 ? CGREY
71 : CBLACK
72 ), -1);
73
74 if (!button.text.empty())
75 {
76 unsigned offset;
77 if (button.position == 0)
78 {
79 ui_draw_box_out(canvas, 0, 0, button.width-1, button.height-1);
80 offset = 0;
81 }
82 else
83 {
84 ui_draw_box_in(canvas, 0, 0, button.width-1, button.height-1);
85 offset = 1;
86 }
87 ui_string_centered(canvas, Middle(button.width) + offset, Middle(button.height) + offset, button.text.c_str());
88 } else {
89 unsigned left, top, right, bottom;
90 if (button.position == 0)
91 {
92 left = top = 1;
93 right = button.width - 1;
94 bottom = button.height - 1;
95 }
96 else
97 {
98 left = top = 2;
99 right = button.width;
100 bottom = button.height;
101 }
102 gr_rect(canvas, 0, 0, button.width, button.height, CBLACK);
103 gr_rect(canvas, left, top, right, bottom, color);
104 }
105 }
106 }
107
108 }
109
110 std::unique_ptr<UI_GADGET_BUTTON> ui_add_gadget_button(UI_DIALOG &dlg, short x, short y, short w, short h, const char *const text, int (*const function_to_call)())
111 {
112 auto button = ui_gadget_add<UI_GADGET_BUTTON>(dlg, x, y, x + w - 1, y + h - 1);
113
114 if ( text )
115 {
116 button->text = text;
117 } else {
118 button->text.clear();
119 }
120 button->width = w;
121 button->height = h;
122 button->position = 0;
123 button->oldposition = 0;
124 button->pressed = 0;
125 button->user_function = function_to_call;
126 button->user_function1 = NULL;
127 button->hotkey1= -1;
128 button->dim_if_no_function = 0;
129 return button;
130 }
131
132 window_event_result UI_GADGET_BUTTON::event_handler(UI_DIALOG &dlg, const d_event &event)
133 {
134 window_event_result rval = window_event_result::ignored;
135
136 oldposition = position;
137 pressed = 0;
138
139 if (event.type == event_type::mouse_button_down || event.type == event_type::mouse_button_up)
140 {
141 const auto OnMe = ui_mouse_on_gadget(*this);
142
143 if (B1_JUST_PRESSED && OnMe)
144 {
145 position = 1;
146 rval = window_event_result::handled;
147 }
148 else if (B1_JUST_RELEASED)
149 {
150 if ((position == 1) && OnMe)
151 pressed = 1;
152
153 position = 0;
154 }
155 }
156
157
158 if (event.type == event_type::key_command)
159 {
160 const auto keypress = event_key_get(event);
161 if (keypress == hotkey ||
162 (keypress == hotkey1 && user_function1) ||
163 (dlg.keyboard_focus_gadget == this && (keypress == KEY_SPACEBAR || keypress == KEY_ENTER)))
164 {
165 position = 2;
166 rval = window_event_result::handled;
167 }
168 }
169 else if (event.type == event_type::key_release)
170 {
171 const auto keypress = event_key_get(event);
172 position = 0;
173
174 if (keypress == hotkey ||
175 (dlg.keyboard_focus_gadget == this && (keypress == KEY_SPACEBAR || keypress == KEY_ENTER)))
176 pressed = 1;
177
178 if (keypress == hotkey1 && user_function1)
179 {
180 user_function1();
181 rval = window_event_result::handled;
182 }
183 }
184
185 if (event.type == event_type::window_draw)
186 ui_draw_button(dlg, *this);
187
188 if (pressed && user_function )
189 {
190 user_function();
191 return window_event_result::handled;
192 }
193 else if (pressed)
194 {
195 rval = ui_gadget_send_event(dlg, event_type::ui_gadget_pressed, *this);
196 if (rval == window_event_result::ignored)
197 rval = window_event_result::handled;
198 }
199
200 return rval;
201 }
202
203 }