source: mainline/uspace/lib/ui/src/pbutton.c@ 8b22d44

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b22d44 was 8b22d44, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Scrollbar buttons should not depress in text mode

  • Property mode set to 100644
File size: 13.9 KB
Line 
1/*
2 * Copyright (c) 2022 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 * Push button either uses text as decoration, or it can use a caller-provided
36 * function to paint the decoration.
37 */
38
39#include <errno.h>
40#include <gfx/color.h>
41#include <gfx/context.h>
42#include <gfx/render.h>
43#include <gfx/text.h>
44#include <io/pos_event.h>
45#include <stdlib.h>
46#include <str.h>
47#include <ui/control.h>
48#include <ui/paint.h>
49#include <ui/pbutton.h>
50#include "../private/pbutton.h"
51#include "../private/resource.h"
52
53/** Caption movement when button is pressed down */
54enum {
55 ui_pb_press_dx = 1,
56 ui_pb_press_dy = 1
57};
58
59static void ui_pbutton_ctl_destroy(void *);
60static errno_t ui_pbutton_ctl_paint(void *);
61static ui_evclaim_t ui_pbutton_ctl_pos_event(void *, pos_event_t *);
62
63/** Push button control ops */
64ui_control_ops_t ui_pbutton_ops = {
65 .destroy = ui_pbutton_ctl_destroy,
66 .paint = ui_pbutton_ctl_paint,
67 .pos_event = ui_pbutton_ctl_pos_event
68};
69
70/** Create new push button.
71 *
72 * @param resource UI resource
73 * @param caption Caption
74 * @param rpbutton Place to store pointer to new push button
75 * @return EOK on success, ENOMEM if out of memory
76 */
77errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
78 ui_pbutton_t **rpbutton)
79{
80 ui_pbutton_t *pbutton;
81 errno_t rc;
82
83 pbutton = calloc(1, sizeof(ui_pbutton_t));
84 if (pbutton == NULL)
85 return ENOMEM;
86
87 rc = ui_control_new(&ui_pbutton_ops, (void *) pbutton,
88 &pbutton->control);
89 if (rc != EOK) {
90 free(pbutton);
91 return rc;
92 }
93
94 pbutton->caption = str_dup(caption);
95 if (pbutton->caption == NULL) {
96 ui_control_delete(pbutton->control);
97 free(pbutton);
98 return ENOMEM;
99 }
100
101 pbutton->res = resource;
102 *rpbutton = pbutton;
103 return EOK;
104}
105
106/** Destroy push button.
107 *
108 * @param pbutton Push button or @c NULL
109 */
110void ui_pbutton_destroy(ui_pbutton_t *pbutton)
111{
112 if (pbutton == NULL)
113 return;
114
115 ui_control_delete(pbutton->control);
116 free(pbutton);
117}
118
119/** Get base control from push button.
120 *
121 * @param pbutton Push button
122 * @return Control
123 */
124ui_control_t *ui_pbutton_ctl(ui_pbutton_t *pbutton)
125{
126 return pbutton->control;
127}
128
129/** Set push button callbacks.
130 *
131 * @param pbutton Push button
132 * @param cb Push button callbacks
133 * @param arg Callback argument
134 */
135void ui_pbutton_set_cb(ui_pbutton_t *pbutton, ui_pbutton_cb_t *cb, void *arg)
136{
137 pbutton->cb = cb;
138 pbutton->arg = arg;
139}
140
141/** Set push button decoration ops.
142 *
143 * @param pbutton Push button
144 * @param ops Push button decoration callbacks
145 * @param arg Decoration ops argument
146 */
147void ui_pbutton_set_decor_ops(ui_pbutton_t *pbutton,
148 ui_pbutton_decor_ops_t *ops, void *arg)
149{
150 pbutton->decor_ops = ops;
151 pbutton->decor_arg = arg;
152}
153
154/** Set push button flag.s
155 *
156 * @param pbutton Push button
157 * @param flags Flags
158 */
159void ui_pbutton_set_flags(ui_pbutton_t *pbutton, ui_pbutton_flags_t flags)
160{
161 pbutton->flags = flags;
162}
163
164/** Set button rectangle.
165 *
166 * @param pbutton Button
167 * @param rect New button rectangle
168 */
169void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
170{
171 pbutton->rect = *rect;
172}
173
174/** Set default flag.
175 *
176 * Default button is the one activated by Enter key, it is marked
177 * by thicker frame.
178 *
179 * @param pbutton Button
180 * @param isdefault @c true iff button is default
181 */
182void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault)
183{
184 pbutton->isdefault = isdefault;
185}
186
187/** Paint outer button frame.
188 *
189 * @param pbutton Push button
190 * @return EOK on success or an error code
191 */
192static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
193{
194 gfx_rect_t rect;
195 gfx_coord_t thickness;
196 errno_t rc;
197
198 thickness = pbutton->isdefault ? 2 : 1;
199
200 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_frame_color);
201 if (rc != EOK)
202 goto error;
203
204 rect.p0.x = pbutton->rect.p0.x + 1;
205 rect.p0.y = pbutton->rect.p0.y;
206 rect.p1.x = pbutton->rect.p1.x - 1;
207 rect.p1.y = pbutton->rect.p0.y + thickness;
208 rc = gfx_fill_rect(pbutton->res->gc, &rect);
209 if (rc != EOK)
210 goto error;
211
212 rect.p0.x = pbutton->rect.p0.x + 1;
213 rect.p0.y = pbutton->rect.p1.y - thickness;
214 rect.p1.x = pbutton->rect.p1.x - 1;
215 rect.p1.y = pbutton->rect.p1.y;
216 rc = gfx_fill_rect(pbutton->res->gc, &rect);
217 if (rc != EOK)
218 goto error;
219
220 rect.p0.x = pbutton->rect.p0.x;
221 rect.p0.y = pbutton->rect.p0.y + 1;
222 rect.p1.x = pbutton->rect.p0.x + thickness;
223 rect.p1.y = pbutton->rect.p1.y - 1;
224 rc = gfx_fill_rect(pbutton->res->gc, &rect);
225 if (rc != EOK)
226 goto error;
227
228 rect.p0.x = pbutton->rect.p1.x - thickness;
229 rect.p0.y = pbutton->rect.p0.y + 1;
230 rect.p1.x = pbutton->rect.p1.x;
231 rect.p1.y = pbutton->rect.p1.y - 1;
232 rc = gfx_fill_rect(pbutton->res->gc, &rect);
233 if (rc != EOK)
234 goto error;
235
236 return EOK;
237error:
238 return rc;
239}
240
241/** Paint outset button bevel.
242 *
243 * @param pbutton Push button
244 * @return EOK on success or an error code
245 */
246static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton,
247 gfx_rect_t *rect)
248{
249 return ui_paint_bevel(pbutton->res->gc, rect,
250 pbutton->res->btn_highlight_color,
251 pbutton->res->btn_shadow_color, 2, NULL);
252}
253
254/** Paint inset button bevel.
255 *
256 * @param pbutton Push button
257 * @return EOK on success or an error code
258 */
259static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton,
260 gfx_rect_t *rect)
261{
262 return ui_paint_bevel(pbutton->res->gc, rect,
263 pbutton->res->btn_shadow_color,
264 pbutton->res->btn_face_color, 2, NULL);
265}
266
267/** Paint button text shadow.
268 *
269 * @param pbutton Push button
270 * @return EOK on success or an error code
271 */
272static errno_t ui_pbutton_paint_text_shadow(ui_pbutton_t *pbutton)
273{
274 gfx_rect_t rect;
275 errno_t rc;
276
277 rect.p0.x = pbutton->rect.p0.x + 1;
278 rect.p0.y = pbutton->rect.p0.y + 1;
279 rect.p1.x = pbutton->rect.p1.x;
280 rect.p1.y = pbutton->rect.p1.y;
281
282 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_shadow_color);
283 if (rc != EOK)
284 goto error;
285
286 rc = gfx_fill_rect(pbutton->res->gc, &rect);
287 if (rc != EOK)
288 goto error;
289
290 return EOK;
291error:
292 return rc;
293}
294
295/** Paint push button in graphic mode.
296 *
297 * @param pbutton Push button
298 * @return EOK on success or an error code
299 */
300static errno_t ui_pbutton_paint_gfx(ui_pbutton_t *pbutton)
301{
302 gfx_coord2_t pos;
303 gfx_text_fmt_t fmt;
304 gfx_rect_t rect;
305 gfx_coord_t thickness;
306 bool depressed;
307 errno_t rc;
308
309 thickness = pbutton->isdefault ? 2 : 1;
310 depressed = pbutton->held && pbutton->inside;
311
312 rect.p0.x = pbutton->rect.p0.x + thickness;
313 rect.p0.y = pbutton->rect.p0.y + thickness;
314 rect.p1.x = pbutton->rect.p1.x - thickness;
315 rect.p1.y = pbutton->rect.p1.y - thickness;
316
317 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
318 if (rc != EOK)
319 goto error;
320
321 rc = gfx_fill_rect(pbutton->res->gc, &rect);
322 if (rc != EOK)
323 goto error;
324
325 /* Center of button rectangle */
326 pos.x = (rect.p0.x + rect.p1.x) / 2;
327 pos.y = (rect.p0.y + rect.p1.y) / 2;
328
329 if (depressed) {
330 pos.x += ui_pb_press_dx;
331 pos.y += ui_pb_press_dy;
332 }
333
334 if (pbutton->decor_ops != NULL && pbutton->decor_ops->paint != NULL) {
335 /* Custom decoration */
336 rc = pbutton->decor_ops->paint(pbutton, pbutton->decor_arg,
337 &pos);
338 if (rc != EOK)
339 goto error;
340 } else {
341 /* Text decoration */
342 gfx_text_fmt_init(&fmt);
343 fmt.font = pbutton->res->font;
344 fmt.color = pbutton->res->btn_text_color;
345 fmt.halign = gfx_halign_center;
346 fmt.valign = gfx_valign_center;
347
348 rc = gfx_puttext(&pos, &fmt, pbutton->caption);
349 if (rc != EOK)
350 goto error;
351 }
352
353 rc = ui_pbutton_paint_frame(pbutton);
354 if (rc != EOK)
355 goto error;
356
357 if (depressed) {
358 rc = ui_pbutton_paint_inset(pbutton, &rect);
359 if (rc != EOK)
360 goto error;
361 } else {
362 rc = ui_pbutton_paint_outset(pbutton, &rect);
363 if (rc != EOK)
364 goto error;
365 }
366
367 rc = gfx_update(pbutton->res->gc);
368 if (rc != EOK)
369 goto error;
370
371 return EOK;
372error:
373 return rc;
374}
375
376/** Paint push button in text mode.
377 *
378 * @param pbutton Push button
379 * @return EOK on success or an error code
380 */
381static errno_t ui_pbutton_paint_text(ui_pbutton_t *pbutton)
382{
383 gfx_coord2_t pos;
384 gfx_text_fmt_t fmt;
385 gfx_rect_t rect;
386 bool depressed;
387 errno_t rc;
388
389 if ((pbutton->flags & ui_pbf_no_text_depress) == 0)
390 depressed = pbutton->held && pbutton->inside;
391 else
392 depressed = false;
393
394 rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color);
395 if (rc != EOK)
396 goto error;
397
398 rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect);
399 if (rc != EOK)
400 goto error;
401
402 rect.p0.x = pbutton->rect.p0.x + (depressed ? 1 : 0);
403 rect.p0.y = pbutton->rect.p0.y;
404 rect.p1.x = pbutton->rect.p1.x - 1 + (depressed ? 1 : 0);
405 rect.p1.y = pbutton->rect.p0.y + 1;
406
407 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
408 if (rc != EOK)
409 goto error;
410
411 rc = gfx_fill_rect(pbutton->res->gc, &rect);
412 if (rc != EOK)
413 goto error;
414
415 /* Center of button rectangle */
416 pos.x = (rect.p0.x + rect.p1.x) / 2;
417 pos.y = (rect.p0.y + rect.p1.y) / 2;
418
419 gfx_text_fmt_init(&fmt);
420 fmt.font = pbutton->res->font;
421 fmt.color = pbutton->res->btn_text_color;
422 fmt.halign = gfx_halign_center;
423 fmt.valign = gfx_valign_center;
424
425 rc = gfx_puttext(&pos, &fmt, pbutton->caption);
426 if (rc != EOK)
427 goto error;
428
429 if (!depressed) {
430 rc = ui_pbutton_paint_text_shadow(pbutton);
431 if (rc != EOK)
432 goto error;
433 }
434
435 rc = gfx_update(pbutton->res->gc);
436 if (rc != EOK)
437 goto error;
438
439 return EOK;
440error:
441 return rc;
442}
443
444/** Paint push button.
445 *
446 * @param pbutton Push button
447 * @return EOK on success or an error code
448 */
449errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
450{
451 if (pbutton->res->textmode)
452 return ui_pbutton_paint_text(pbutton);
453 else
454 return ui_pbutton_paint_gfx(pbutton);
455}
456
457/** Press down button.
458 *
459 * @param pbutton Push button
460 */
461void ui_pbutton_press(ui_pbutton_t *pbutton)
462{
463 if (pbutton->held)
464 return;
465
466 pbutton->inside = true;
467 pbutton->held = true;
468 (void) ui_pbutton_paint(pbutton);
469 ui_pbutton_down(pbutton);
470}
471
472/** Release button.
473 *
474 * @param pbutton Push button
475 */
476void ui_pbutton_release(ui_pbutton_t *pbutton)
477{
478 if (!pbutton->held)
479 return;
480
481 pbutton->held = false;
482
483 if (pbutton->inside) {
484 (void) ui_pbutton_paint(pbutton);
485 ui_pbutton_clicked(pbutton);
486 }
487
488 ui_pbutton_up(pbutton);
489}
490
491/** Pointer entered button.
492 *
493 * @param pbutton Push button
494 */
495void ui_pbutton_enter(ui_pbutton_t *pbutton)
496{
497 if (pbutton->inside)
498 return;
499
500 pbutton->inside = true;
501 if (pbutton->held)
502 (void) ui_pbutton_paint(pbutton);
503}
504
505/** Pointer left button.
506 *
507 * @param pbutton Push button
508 */
509void ui_pbutton_leave(ui_pbutton_t *pbutton)
510{
511 if (!pbutton->inside)
512 return;
513
514 pbutton->inside = false;
515 if (pbutton->held)
516 (void) ui_pbutton_paint(pbutton);
517}
518
519/** Send button clicked event.
520 *
521 * @param pbutton Push button
522 */
523void ui_pbutton_clicked(ui_pbutton_t *pbutton)
524{
525 if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
526 pbutton->cb->clicked(pbutton, pbutton->arg);
527}
528
529/** Send button down event.
530 *
531 * @param pbutton Push button
532 */
533void ui_pbutton_down(ui_pbutton_t *pbutton)
534{
535 if (pbutton->cb != NULL && pbutton->cb->down != NULL)
536 pbutton->cb->down(pbutton, pbutton->arg);
537}
538
539/** Send button up event.
540 *
541 * @param pbutton Push button
542 */
543void ui_pbutton_up(ui_pbutton_t *pbutton)
544{
545 if (pbutton->cb != NULL && pbutton->cb->up != NULL)
546 pbutton->cb->up(pbutton, pbutton->arg);
547}
548
549/** Handle push button position event.
550 *
551 * @param pbutton Push button
552 * @param pos_event Position event
553 * @return @c ui_claimed iff the event is claimed
554 */
555ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
556{
557 gfx_coord2_t pos;
558 bool inside;
559
560 pos.x = event->hpos;
561 pos.y = event->vpos;
562
563 inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
564
565 switch (event->type) {
566 case POS_PRESS:
567 if (inside) {
568 ui_pbutton_press(pbutton);
569 return ui_claimed;
570 }
571 break;
572 case POS_RELEASE:
573 if (pbutton->held) {
574 ui_pbutton_release(pbutton);
575 return ui_claimed;
576 }
577 break;
578 case POS_UPDATE:
579 if (inside && !pbutton->inside) {
580 ui_pbutton_enter(pbutton);
581 return ui_claimed;
582 } else if (!inside && pbutton->inside) {
583 ui_pbutton_leave(pbutton);
584 }
585 break;
586 case POS_DCLICK:
587 break;
588 }
589
590 return ui_unclaimed;
591}
592
593/** Destroy push button control.
594 *
595 * @param arg Argument (ui_pbutton_t *)
596 */
597void ui_pbutton_ctl_destroy(void *arg)
598{
599 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
600
601 ui_pbutton_destroy(pbutton);
602}
603
604/** Paint push button control.
605 *
606 * @param arg Argument (ui_pbutton_t *)
607 * @return EOK on success or an error code
608 */
609errno_t ui_pbutton_ctl_paint(void *arg)
610{
611 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
612
613 return ui_pbutton_paint(pbutton);
614}
615
616/** Handle push button control position event.
617 *
618 * @param arg Argument (ui_pbutton_t *)
619 * @param pos_event Position event
620 * @return @c ui_claimed iff the event is claimed
621 */
622ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
623{
624 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
625
626 return ui_pbutton_pos_event(pbutton, event);
627}
628
629/** @}
630 */
Note: See TracBrowser for help on using the repository browser.