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/control.h>
|
---|
45 | #include <ui/paint.h>
|
---|
46 | #include <ui/pbutton.h>
|
---|
47 | #include "../private/pbutton.h"
|
---|
48 | #include "../private/resource.h"
|
---|
49 |
|
---|
50 | /** Caption movement when button is pressed down */
|
---|
51 | enum {
|
---|
52 | ui_pb_press_dx = 1,
|
---|
53 | ui_pb_press_dy = 1
|
---|
54 | };
|
---|
55 |
|
---|
56 | static void ui_pbutton_ctl_destroy(void *);
|
---|
57 | static errno_t ui_pbutton_ctl_paint(void *);
|
---|
58 | static ui_evclaim_t ui_pbutton_ctl_pos_event(void *, pos_event_t *);
|
---|
59 |
|
---|
60 | /** Push button control ops */
|
---|
61 | ui_control_ops_t ui_pbutton_ops = {
|
---|
62 | .destroy = ui_pbutton_ctl_destroy,
|
---|
63 | .paint = ui_pbutton_ctl_paint,
|
---|
64 | .pos_event = ui_pbutton_ctl_pos_event
|
---|
65 | };
|
---|
66 |
|
---|
67 | /** Create new push button.
|
---|
68 | *
|
---|
69 | * @param resource UI resource
|
---|
70 | * @param caption Caption
|
---|
71 | * @param rpbutton Place to store pointer to new push button
|
---|
72 | * @return EOK on success, ENOMEM if out of memory
|
---|
73 | */
|
---|
74 | errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
|
---|
75 | ui_pbutton_t **rpbutton)
|
---|
76 | {
|
---|
77 | ui_pbutton_t *pbutton;
|
---|
78 | errno_t rc;
|
---|
79 |
|
---|
80 | pbutton = calloc(1, sizeof(ui_pbutton_t));
|
---|
81 | if (pbutton == NULL)
|
---|
82 | return ENOMEM;
|
---|
83 |
|
---|
84 | rc = ui_control_new(&ui_pbutton_ops, (void *) pbutton,
|
---|
85 | &pbutton->control);
|
---|
86 | if (rc != EOK) {
|
---|
87 | free(pbutton);
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | pbutton->caption = str_dup(caption);
|
---|
92 | if (pbutton->caption == NULL) {
|
---|
93 | ui_control_delete(pbutton->control);
|
---|
94 | free(pbutton);
|
---|
95 | return ENOMEM;
|
---|
96 | }
|
---|
97 |
|
---|
98 | pbutton->res = resource;
|
---|
99 | *rpbutton = pbutton;
|
---|
100 | return EOK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Destroy push button.
|
---|
104 | *
|
---|
105 | * @param pbutton Push button or @c NULL
|
---|
106 | */
|
---|
107 | void ui_pbutton_destroy(ui_pbutton_t *pbutton)
|
---|
108 | {
|
---|
109 | if (pbutton == NULL)
|
---|
110 | return;
|
---|
111 |
|
---|
112 | ui_control_delete(pbutton->control);
|
---|
113 | free(pbutton);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Get base control from push button.
|
---|
117 | *
|
---|
118 | * @param pbutton Push button
|
---|
119 | * @return Control
|
---|
120 | */
|
---|
121 | ui_control_t *ui_pbutton_ctl(ui_pbutton_t *pbutton)
|
---|
122 | {
|
---|
123 | return pbutton->control;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Set push button callbacks.
|
---|
127 | *
|
---|
128 | * @param pbutton Push button
|
---|
129 | * @param cb Push button callbacks
|
---|
130 | * @param arg Callback argument
|
---|
131 | */
|
---|
132 | void ui_pbutton_set_cb(ui_pbutton_t *pbutton, ui_pbutton_cb_t *cb, void *arg)
|
---|
133 | {
|
---|
134 | pbutton->cb = cb;
|
---|
135 | pbutton->arg = arg;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Set button rectangle.
|
---|
139 | *
|
---|
140 | * @param pbutton Button
|
---|
141 | * @param rect New button rectangle
|
---|
142 | */
|
---|
143 | void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
|
---|
144 | {
|
---|
145 | pbutton->rect = *rect;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Set default flag.
|
---|
149 | *
|
---|
150 | * Default button is the one activated by Enter key, it is marked
|
---|
151 | * by thicker frame.
|
---|
152 | *
|
---|
153 | * @param pbutton Button
|
---|
154 | * @param isdefault @c true iff button is default
|
---|
155 | */
|
---|
156 | void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault)
|
---|
157 | {
|
---|
158 | pbutton->isdefault = isdefault;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Paint outer button frame.
|
---|
162 | *
|
---|
163 | * @param pbutton Push button
|
---|
164 | * @return EOK on success or an error code
|
---|
165 | */
|
---|
166 | static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
|
---|
167 | {
|
---|
168 | gfx_rect_t rect;
|
---|
169 | gfx_coord_t thickness;
|
---|
170 | errno_t rc;
|
---|
171 |
|
---|
172 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
173 |
|
---|
174 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_frame_color);
|
---|
175 | if (rc != EOK)
|
---|
176 | goto error;
|
---|
177 |
|
---|
178 | rect.p0.x = pbutton->rect.p0.x + 1;
|
---|
179 | rect.p0.y = pbutton->rect.p0.y;
|
---|
180 | rect.p1.x = pbutton->rect.p1.x - 1;
|
---|
181 | rect.p1.y = pbutton->rect.p0.y + thickness;
|
---|
182 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
183 | if (rc != EOK)
|
---|
184 | goto error;
|
---|
185 |
|
---|
186 | rect.p0.x = pbutton->rect.p0.x + 1;
|
---|
187 | rect.p0.y = pbutton->rect.p1.y - thickness;
|
---|
188 | rect.p1.x = pbutton->rect.p1.x - 1;
|
---|
189 | rect.p1.y = pbutton->rect.p1.y;
|
---|
190 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
191 | if (rc != EOK)
|
---|
192 | goto error;
|
---|
193 |
|
---|
194 | rect.p0.x = pbutton->rect.p0.x;
|
---|
195 | rect.p0.y = pbutton->rect.p0.y + 1;
|
---|
196 | rect.p1.x = pbutton->rect.p0.x + thickness;
|
---|
197 | rect.p1.y = pbutton->rect.p1.y - 1;
|
---|
198 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
199 | if (rc != EOK)
|
---|
200 | goto error;
|
---|
201 |
|
---|
202 | rect.p0.x = pbutton->rect.p1.x - thickness;
|
---|
203 | rect.p0.y = pbutton->rect.p0.y + 1;
|
---|
204 | rect.p1.x = pbutton->rect.p1.x;
|
---|
205 | rect.p1.y = pbutton->rect.p1.y - 1;
|
---|
206 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
207 | if (rc != EOK)
|
---|
208 | goto error;
|
---|
209 |
|
---|
210 | return EOK;
|
---|
211 | error:
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** Paint outset button bevel.
|
---|
216 | *
|
---|
217 | * @param pbutton Push button
|
---|
218 | * @return EOK on success or an error code
|
---|
219 | */
|
---|
220 | static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton,
|
---|
221 | gfx_rect_t *rect)
|
---|
222 | {
|
---|
223 | return ui_paint_bevel(pbutton->res->gc, rect,
|
---|
224 | pbutton->res->btn_highlight_color,
|
---|
225 | pbutton->res->btn_shadow_color, 2, NULL);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /** Paint inset button bevel.
|
---|
229 | *
|
---|
230 | * @param pbutton Push button
|
---|
231 | * @return EOK on success or an error code
|
---|
232 | */
|
---|
233 | static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton,
|
---|
234 | gfx_rect_t *rect)
|
---|
235 | {
|
---|
236 | return ui_paint_bevel(pbutton->res->gc, rect,
|
---|
237 | pbutton->res->btn_shadow_color,
|
---|
238 | pbutton->res->btn_face_color, 2, NULL);
|
---|
239 | }
|
---|
240 |
|
---|
241 | /** Paint push button.
|
---|
242 | *
|
---|
243 | * @param pbutton Push button
|
---|
244 | * @return EOK on success or an error code
|
---|
245 | */
|
---|
246 | errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
|
---|
247 | {
|
---|
248 | gfx_coord2_t pos;
|
---|
249 | gfx_text_fmt_t fmt;
|
---|
250 | gfx_rect_t rect;
|
---|
251 | gfx_coord_t thickness;
|
---|
252 | bool depressed;
|
---|
253 | errno_t rc;
|
---|
254 |
|
---|
255 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
256 | depressed = pbutton->held && pbutton->inside;
|
---|
257 |
|
---|
258 | rect.p0.x = pbutton->rect.p0.x + thickness;
|
---|
259 | rect.p0.y = pbutton->rect.p0.y + thickness;
|
---|
260 | rect.p1.x = pbutton->rect.p1.x - thickness;
|
---|
261 | rect.p1.y = pbutton->rect.p1.y - thickness;
|
---|
262 |
|
---|
263 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
|
---|
264 | if (rc != EOK)
|
---|
265 | goto error;
|
---|
266 |
|
---|
267 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
268 | if (rc != EOK)
|
---|
269 | goto error;
|
---|
270 |
|
---|
271 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_text_color);
|
---|
272 | if (rc != EOK)
|
---|
273 | goto error;
|
---|
274 |
|
---|
275 | /* Center of button rectangle */
|
---|
276 | pos.x = (rect.p0.x + rect.p1.x) / 2;
|
---|
277 | pos.y = (rect.p0.y + rect.p1.y) / 2;
|
---|
278 |
|
---|
279 | if (depressed) {
|
---|
280 | pos.x += ui_pb_press_dx;
|
---|
281 | pos.y += ui_pb_press_dy;
|
---|
282 | }
|
---|
283 |
|
---|
284 | gfx_text_fmt_init(&fmt);
|
---|
285 | fmt.halign = gfx_halign_center;
|
---|
286 | fmt.valign = gfx_valign_center;
|
---|
287 |
|
---|
288 | rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
|
---|
289 | if (rc != EOK)
|
---|
290 | goto error;
|
---|
291 |
|
---|
292 | rc = ui_pbutton_paint_frame(pbutton);
|
---|
293 | if (rc != EOK)
|
---|
294 | goto error;
|
---|
295 |
|
---|
296 | if (depressed) {
|
---|
297 | rc = ui_pbutton_paint_inset(pbutton, &rect);
|
---|
298 | if (rc != EOK)
|
---|
299 | goto error;
|
---|
300 | } else {
|
---|
301 | rc = ui_pbutton_paint_outset(pbutton, &rect);
|
---|
302 | if (rc != EOK)
|
---|
303 | goto error;
|
---|
304 | }
|
---|
305 |
|
---|
306 | return EOK;
|
---|
307 | error:
|
---|
308 | return rc;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /** Press down button.
|
---|
312 | *
|
---|
313 | * @param pbutton Push button
|
---|
314 | */
|
---|
315 | void ui_pbutton_press(ui_pbutton_t *pbutton)
|
---|
316 | {
|
---|
317 | if (pbutton->held)
|
---|
318 | return;
|
---|
319 |
|
---|
320 | pbutton->inside = true;
|
---|
321 | pbutton->held = true;
|
---|
322 | (void) ui_pbutton_paint(pbutton);
|
---|
323 | }
|
---|
324 |
|
---|
325 | /** Release button.
|
---|
326 | *
|
---|
327 | * @param pbutton Push button
|
---|
328 | */
|
---|
329 | void ui_pbutton_release(ui_pbutton_t *pbutton)
|
---|
330 | {
|
---|
331 | if (!pbutton->held)
|
---|
332 | return;
|
---|
333 |
|
---|
334 | pbutton->held = false;
|
---|
335 |
|
---|
336 | if (pbutton->inside) {
|
---|
337 | (void) ui_pbutton_paint(pbutton);
|
---|
338 | ui_pbutton_clicked(pbutton);
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | /** Pointer entered button.
|
---|
343 | *
|
---|
344 | * @param pbutton Push button
|
---|
345 | */
|
---|
346 | void ui_pbutton_enter(ui_pbutton_t *pbutton)
|
---|
347 | {
|
---|
348 | if (pbutton->inside)
|
---|
349 | return;
|
---|
350 |
|
---|
351 | pbutton->inside = true;
|
---|
352 | if (pbutton->held)
|
---|
353 | (void) ui_pbutton_paint(pbutton);
|
---|
354 | }
|
---|
355 |
|
---|
356 | /** Pointer left button.
|
---|
357 | *
|
---|
358 | * @param pbutton Push button
|
---|
359 | */
|
---|
360 | void ui_pbutton_leave(ui_pbutton_t *pbutton)
|
---|
361 | {
|
---|
362 | if (!pbutton->inside)
|
---|
363 | return;
|
---|
364 |
|
---|
365 | pbutton->inside = false;
|
---|
366 | if (pbutton->held)
|
---|
367 | (void) ui_pbutton_paint(pbutton);
|
---|
368 | }
|
---|
369 |
|
---|
370 | /** Button was clicked.
|
---|
371 | *
|
---|
372 | * @param pbutton Push button
|
---|
373 | */
|
---|
374 | void ui_pbutton_clicked(ui_pbutton_t *pbutton)
|
---|
375 | {
|
---|
376 | if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
|
---|
377 | pbutton->cb->clicked(pbutton, pbutton->arg);
|
---|
378 | }
|
---|
379 |
|
---|
380 | /** Handle push button position event.
|
---|
381 | *
|
---|
382 | * @param pbutton Push button
|
---|
383 | * @param pos_event Position event
|
---|
384 | * @return @c ui_claimed iff the event is claimed
|
---|
385 | */
|
---|
386 | ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
|
---|
387 | {
|
---|
388 | gfx_coord2_t pos;
|
---|
389 | bool inside;
|
---|
390 |
|
---|
391 | pos.x = event->hpos;
|
---|
392 | pos.y = event->vpos;
|
---|
393 |
|
---|
394 | inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
|
---|
395 |
|
---|
396 | switch (event->type) {
|
---|
397 | case POS_PRESS:
|
---|
398 | if (inside) {
|
---|
399 | ui_pbutton_press(pbutton);
|
---|
400 | return ui_claimed;
|
---|
401 | }
|
---|
402 | break;
|
---|
403 | case POS_RELEASE:
|
---|
404 | if (pbutton->held) {
|
---|
405 | ui_pbutton_release(pbutton);
|
---|
406 | return ui_claimed;
|
---|
407 | }
|
---|
408 | break;
|
---|
409 | case POS_UPDATE:
|
---|
410 | if (inside && !pbutton->inside) {
|
---|
411 | ui_pbutton_enter(pbutton);
|
---|
412 | return ui_claimed;
|
---|
413 | } else if (!inside && pbutton->inside) {
|
---|
414 | ui_pbutton_leave(pbutton);
|
---|
415 | }
|
---|
416 | break;
|
---|
417 | }
|
---|
418 |
|
---|
419 | return ui_unclaimed;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** Destroy push button control.
|
---|
423 | *
|
---|
424 | * @param arg Argument (ui_pbutton_t *)
|
---|
425 | */
|
---|
426 | void ui_pbutton_ctl_destroy(void *arg)
|
---|
427 | {
|
---|
428 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
---|
429 |
|
---|
430 | ui_pbutton_destroy(pbutton);
|
---|
431 | }
|
---|
432 |
|
---|
433 | /** Paint push button control.
|
---|
434 | *
|
---|
435 | * @param arg Argument (ui_pbutton_t *)
|
---|
436 | * @return EOK on success or an error code
|
---|
437 | */
|
---|
438 | errno_t ui_pbutton_ctl_paint(void *arg)
|
---|
439 | {
|
---|
440 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
---|
441 |
|
---|
442 | return ui_pbutton_paint(pbutton);
|
---|
443 | }
|
---|
444 |
|
---|
445 | /** Handle push button control position event.
|
---|
446 | *
|
---|
447 | * @param arg Argument (ui_pbutton_t *)
|
---|
448 | * @param pos_event Position event
|
---|
449 | * @return @c ui_claimed iff the event is claimed
|
---|
450 | */
|
---|
451 | ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
452 | {
|
---|
453 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
---|
454 |
|
---|
455 | return ui_pbutton_pos_event(pbutton, event);
|
---|
456 | }
|
---|
457 |
|
---|
458 | /** @}
|
---|
459 | */
|
---|