1 | /*
|
---|
2 | * Copyright (c) 2020 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libui
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Push button
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <gfx/color.h>
|
---|
38 | #include <gfx/context.h>
|
---|
39 | #include <gfx/render.h>
|
---|
40 | #include <gfx/text.h>
|
---|
41 | #include <io/pos_event.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include <ui/pbutton.h>
|
---|
45 | #include "../private/pbutton.h"
|
---|
46 | #include "../private/resource.h"
|
---|
47 |
|
---|
48 | /** Caption movement when button is pressed down */
|
---|
49 | enum {
|
---|
50 | ui_pb_press_dx = 1,
|
---|
51 | ui_pb_press_dy = 1
|
---|
52 | };
|
---|
53 |
|
---|
54 | /** Create new push button.
|
---|
55 | *
|
---|
56 | * @param resource UI resource
|
---|
57 | * @param caption Caption
|
---|
58 | * @param rpbutton Place to store pointer to new push button
|
---|
59 | * @return EOK on success, ENOMEM if out of memory
|
---|
60 | */
|
---|
61 | errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
|
---|
62 | ui_pbutton_t **rpbutton)
|
---|
63 | {
|
---|
64 | ui_pbutton_t *pbutton;
|
---|
65 |
|
---|
66 | pbutton = calloc(1, sizeof(ui_pbutton_t));
|
---|
67 | if (pbutton == NULL)
|
---|
68 | return ENOMEM;
|
---|
69 |
|
---|
70 | pbutton->caption = str_dup(caption);
|
---|
71 | if (pbutton->caption == NULL) {
|
---|
72 | free(pbutton);
|
---|
73 | return ENOMEM;
|
---|
74 | }
|
---|
75 |
|
---|
76 | pbutton->res = resource;
|
---|
77 | *rpbutton = pbutton;
|
---|
78 | return EOK;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Destroy push button.
|
---|
82 | *
|
---|
83 | * @param pbutton Push button or @c NULL
|
---|
84 | */
|
---|
85 | void ui_pbutton_destroy(ui_pbutton_t *pbutton)
|
---|
86 | {
|
---|
87 | if (pbutton == NULL)
|
---|
88 | return;
|
---|
89 |
|
---|
90 | free(pbutton);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** Set button rectangle.
|
---|
94 | *
|
---|
95 | * @param pbutton Button
|
---|
96 | * @param rect New button rectanle
|
---|
97 | */
|
---|
98 | void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
|
---|
99 | {
|
---|
100 | pbutton->rect = *rect;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Set default flag.
|
---|
104 | *
|
---|
105 | * Default button is the one activated by Enter key, it is marked
|
---|
106 | * by thicker frame.
|
---|
107 | *
|
---|
108 | * @param pbutton Button
|
---|
109 | * @param isdefault @c true iff button is default
|
---|
110 | */
|
---|
111 | void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault)
|
---|
112 | {
|
---|
113 | pbutton->isdefault = isdefault;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Paint outer button frame.
|
---|
117 | *
|
---|
118 | * @param pbutton Push button
|
---|
119 | * @return EOK on success or an error code
|
---|
120 | */
|
---|
121 | static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
|
---|
122 | {
|
---|
123 | gfx_color_t *color = NULL;
|
---|
124 | gfx_rect_t rect;
|
---|
125 | gfx_coord_t thickness;
|
---|
126 | errno_t rc;
|
---|
127 |
|
---|
128 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
129 |
|
---|
130 | rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
|
---|
131 | if (rc != EOK)
|
---|
132 | goto error;
|
---|
133 |
|
---|
134 | rc = gfx_set_color(pbutton->res->gc, color);
|
---|
135 | if (rc != EOK)
|
---|
136 | goto error;
|
---|
137 |
|
---|
138 | rect.p0.x = pbutton->rect.p0.x + 1;
|
---|
139 | rect.p0.y = pbutton->rect.p0.y;
|
---|
140 | rect.p1.x = pbutton->rect.p1.x - 1;
|
---|
141 | rect.p1.y = pbutton->rect.p0.y + thickness;
|
---|
142 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
143 | if (rc != EOK)
|
---|
144 | goto error;
|
---|
145 |
|
---|
146 | rect.p0.x = pbutton->rect.p0.x + 1;
|
---|
147 | rect.p0.y = pbutton->rect.p1.y - thickness;
|
---|
148 | rect.p1.x = pbutton->rect.p1.x - 1;
|
---|
149 | rect.p1.y = pbutton->rect.p1.y;
|
---|
150 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
151 | if (rc != EOK)
|
---|
152 | goto error;
|
---|
153 |
|
---|
154 | rect.p0.x = pbutton->rect.p0.x;
|
---|
155 | rect.p0.y = pbutton->rect.p0.y + 1;
|
---|
156 | rect.p1.x = pbutton->rect.p0.x + thickness;
|
---|
157 | rect.p1.y = pbutton->rect.p1.y - 1;
|
---|
158 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
159 | if (rc != EOK)
|
---|
160 | goto error;
|
---|
161 |
|
---|
162 | rect.p0.x = pbutton->rect.p1.x - thickness;
|
---|
163 | rect.p0.y = pbutton->rect.p0.y + 1;
|
---|
164 | rect.p1.x = pbutton->rect.p1.x;
|
---|
165 | rect.p1.y = pbutton->rect.p1.y - 1;
|
---|
166 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
167 | if (rc != EOK)
|
---|
168 | goto error;
|
---|
169 |
|
---|
170 | gfx_color_delete(color);
|
---|
171 | return EOK;
|
---|
172 | error:
|
---|
173 | if (color != NULL)
|
---|
174 | gfx_color_delete(color);
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Paint outset button bevel.
|
---|
179 | *
|
---|
180 | * @param pbutton Push button
|
---|
181 | * @return EOK on success or an error code
|
---|
182 | */
|
---|
183 | static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton,
|
---|
184 | gfx_rect_t *rect)
|
---|
185 | {
|
---|
186 | gfx_color_t *color = NULL;
|
---|
187 | gfx_rect_t frect;
|
---|
188 | gfx_coord_t i;
|
---|
189 | errno_t rc;
|
---|
190 |
|
---|
191 | /* Highlight */
|
---|
192 |
|
---|
193 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
---|
194 | if (rc != EOK)
|
---|
195 | goto error;
|
---|
196 |
|
---|
197 | rc = gfx_set_color(pbutton->res->gc, color);
|
---|
198 | if (rc != EOK)
|
---|
199 | goto error;
|
---|
200 |
|
---|
201 | for (i = 0; i < 2; i++) {
|
---|
202 | frect.p0.x = rect->p0.x + i;
|
---|
203 | frect.p0.y = rect->p0.y + i;
|
---|
204 | frect.p1.x = rect->p1.x - i - 1;
|
---|
205 | frect.p1.y = rect->p0.y + i + 1;
|
---|
206 | rc = gfx_fill_rect(pbutton->res->gc, &frect);
|
---|
207 | if (rc != EOK)
|
---|
208 | goto error;
|
---|
209 |
|
---|
210 | frect.p0.x = rect->p0.x + i;
|
---|
211 | frect.p0.y = rect->p0.y + i + 1;
|
---|
212 | frect.p1.x = rect->p0.x + i + 1;
|
---|
213 | frect.p1.y = rect->p1.y - i - 1;
|
---|
214 | rc = gfx_fill_rect(pbutton->res->gc, &frect);
|
---|
215 | if (rc != EOK)
|
---|
216 | goto error;
|
---|
217 | }
|
---|
218 |
|
---|
219 | gfx_color_delete(color);
|
---|
220 | color = NULL;
|
---|
221 |
|
---|
222 | /* Shadow */
|
---|
223 |
|
---|
224 | rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &color);
|
---|
225 | if (rc != EOK)
|
---|
226 | goto error;
|
---|
227 |
|
---|
228 | rc = gfx_set_color(pbutton->res->gc, color);
|
---|
229 | if (rc != EOK)
|
---|
230 | goto error;
|
---|
231 |
|
---|
232 | for (i = 0; i < 2; i++) {
|
---|
233 | frect.p0.x = rect->p0.x + i;
|
---|
234 | frect.p0.y = rect->p1.y - i - 1;
|
---|
235 | frect.p1.x = rect->p1.x - i - 1;
|
---|
236 | frect.p1.y = rect->p1.y - i;
|
---|
237 | rc = gfx_fill_rect(pbutton->res->gc, &frect);
|
---|
238 | if (rc != EOK)
|
---|
239 | goto error;
|
---|
240 |
|
---|
241 | frect.p0.x = rect->p1.x - i - 1;
|
---|
242 | frect.p0.y = rect->p0.y + i;
|
---|
243 | frect.p1.x = rect->p1.x - i;
|
---|
244 | frect.p1.y = rect->p1.y - i;
|
---|
245 | rc = gfx_fill_rect(pbutton->res->gc, &frect);
|
---|
246 | if (rc != EOK)
|
---|
247 | goto error;
|
---|
248 | }
|
---|
249 |
|
---|
250 | gfx_color_delete(color);
|
---|
251 |
|
---|
252 | return EOK;
|
---|
253 | error:
|
---|
254 | if (color != NULL)
|
---|
255 | gfx_color_delete(color);
|
---|
256 | return rc;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /** Paint inset button bevel.
|
---|
260 | *
|
---|
261 | * @param pbutton Push button
|
---|
262 | * @return EOK on success or an error code
|
---|
263 | */
|
---|
264 | static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton,
|
---|
265 | gfx_rect_t *rect)
|
---|
266 | {
|
---|
267 | gfx_color_t *color = NULL;
|
---|
268 | gfx_rect_t frect;
|
---|
269 | errno_t rc;
|
---|
270 |
|
---|
271 | rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &color);
|
---|
272 | if (rc != EOK)
|
---|
273 | goto error;
|
---|
274 |
|
---|
275 | rc = gfx_set_color(pbutton->res->gc, color);
|
---|
276 | if (rc != EOK)
|
---|
277 | goto error;
|
---|
278 |
|
---|
279 | frect.p0.x = rect->p0.x;
|
---|
280 | frect.p0.y = rect->p0.y;
|
---|
281 | frect.p1.x = rect->p1.x;
|
---|
282 | frect.p1.y = rect->p0.y + 2;
|
---|
283 | rc = gfx_fill_rect(pbutton->res->gc, &frect);
|
---|
284 | if (rc != EOK)
|
---|
285 | goto error;
|
---|
286 |
|
---|
287 | frect.p0.x = rect->p0.x;
|
---|
288 | frect.p0.y = rect->p0.y + 2;
|
---|
289 | frect.p1.x = rect->p0.x + 2;
|
---|
290 | frect.p1.y = rect->p1.y;
|
---|
291 | rc = gfx_fill_rect(pbutton->res->gc, &frect);
|
---|
292 | if (rc != EOK)
|
---|
293 | goto error;
|
---|
294 |
|
---|
295 | gfx_color_delete(color);
|
---|
296 |
|
---|
297 | return EOK;
|
---|
298 | error:
|
---|
299 | if (color != NULL)
|
---|
300 | gfx_color_delete(color);
|
---|
301 | return rc;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /** Paint push button.
|
---|
305 | *
|
---|
306 | * @param pbutton Push button
|
---|
307 | * @return EOK on success or an error code
|
---|
308 | */
|
---|
309 | errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
|
---|
310 | {
|
---|
311 | gfx_color_t *color = NULL;
|
---|
312 | gfx_coord2_t pos;
|
---|
313 | gfx_text_fmt_t fmt;
|
---|
314 | gfx_rect_t rect;
|
---|
315 | gfx_coord_t thickness;
|
---|
316 | errno_t rc;
|
---|
317 |
|
---|
318 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
319 |
|
---|
320 | rect.p0.x = pbutton->rect.p0.x + thickness;
|
---|
321 | rect.p0.y = pbutton->rect.p0.y + thickness;
|
---|
322 | rect.p1.x = pbutton->rect.p1.x - thickness;
|
---|
323 | rect.p1.y = pbutton->rect.p1.y - thickness;
|
---|
324 |
|
---|
325 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
|
---|
326 | if (rc != EOK)
|
---|
327 | goto error;
|
---|
328 |
|
---|
329 | rc = gfx_set_color(pbutton->res->gc, color);
|
---|
330 | if (rc != EOK)
|
---|
331 | goto error;
|
---|
332 |
|
---|
333 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
334 | if (rc != EOK)
|
---|
335 | goto error;
|
---|
336 |
|
---|
337 | gfx_color_delete(color);
|
---|
338 | color = NULL;
|
---|
339 |
|
---|
340 | rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
|
---|
341 | if (rc != EOK)
|
---|
342 | goto error;
|
---|
343 |
|
---|
344 | rc = gfx_set_color(pbutton->res->gc, color);
|
---|
345 | if (rc != EOK)
|
---|
346 | goto error;
|
---|
347 |
|
---|
348 | /* Center of button rectangle */
|
---|
349 | pos.x = (rect.p0.x + rect.p1.x) / 2;
|
---|
350 | pos.y = (rect.p0.y + rect.p1.y) / 2;
|
---|
351 |
|
---|
352 | if (pbutton->held) {
|
---|
353 | pos.x += ui_pb_press_dx;
|
---|
354 | pos.y += ui_pb_press_dy;
|
---|
355 | }
|
---|
356 |
|
---|
357 | gfx_text_fmt_init(&fmt);
|
---|
358 | fmt.halign = gfx_halign_center;
|
---|
359 | fmt.valign = gfx_valign_center;
|
---|
360 |
|
---|
361 | rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
|
---|
362 | if (rc != EOK)
|
---|
363 | goto error;
|
---|
364 |
|
---|
365 | gfx_color_delete(color);
|
---|
366 | color = NULL;
|
---|
367 |
|
---|
368 | rc = ui_pbutton_paint_frame(pbutton);
|
---|
369 | if (rc != EOK)
|
---|
370 | goto error;
|
---|
371 |
|
---|
372 | if (pbutton->held) {
|
---|
373 | rc = ui_pbutton_paint_inset(pbutton, &rect);
|
---|
374 | if (rc != EOK)
|
---|
375 | goto error;
|
---|
376 | } else {
|
---|
377 | rc = ui_pbutton_paint_outset(pbutton, &rect);
|
---|
378 | if (rc != EOK)
|
---|
379 | goto error;
|
---|
380 | }
|
---|
381 |
|
---|
382 | return EOK;
|
---|
383 | error:
|
---|
384 | if (color != NULL)
|
---|
385 | gfx_color_delete(color);
|
---|
386 | return rc;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /** Press down button.
|
---|
390 | *
|
---|
391 | * This does not automatically repaint the button.
|
---|
392 | *
|
---|
393 | * @param pbutton Push button
|
---|
394 | */
|
---|
395 | void ui_pbutton_press(ui_pbutton_t *pbutton)
|
---|
396 | {
|
---|
397 | pbutton->held = true;
|
---|
398 | }
|
---|
399 |
|
---|
400 | /** Release button.
|
---|
401 | *
|
---|
402 | * This does not automatically repaint the button.
|
---|
403 | *
|
---|
404 | * @param pbutton Push button
|
---|
405 | */
|
---|
406 | void ui_pbutton_release(ui_pbutton_t *pbutton)
|
---|
407 | {
|
---|
408 | pbutton->held = false;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /** Handle push button position event.
|
---|
412 | *
|
---|
413 | * @param pbutton Push button
|
---|
414 | * @param pos_event Position event
|
---|
415 | */
|
---|
416 | void ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
|
---|
417 | {
|
---|
418 | gfx_coord2_t pos;
|
---|
419 |
|
---|
420 | pos.x = event->hpos;
|
---|
421 | pos.y = event->vpos;
|
---|
422 |
|
---|
423 | if (gfx_pix_inside_rect(&pos, &pbutton->rect)) {
|
---|
424 | if (event->type == POS_PRESS) {
|
---|
425 | ui_pbutton_press(pbutton);
|
---|
426 | (void) ui_pbutton_paint(pbutton);
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | if (event->type == POS_RELEASE && pbutton->held) {
|
---|
431 | ui_pbutton_release(pbutton);
|
---|
432 | (void) ui_pbutton_paint(pbutton);
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | /** @}
|
---|
437 | */
|
---|