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

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

Add font to gfx_text_fmt_t

This is quite logical and saves us one argument that we need to pass to
all text formatting functions.

  • Property mode set to 100644
File size: 12.5 KB
Line 
1/*
2 * Copyright (c) 2021 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 */
51enum {
52 ui_pb_press_dx = 1,
53 ui_pb_press_dy = 1
54};
55
56static void ui_pbutton_ctl_destroy(void *);
57static errno_t ui_pbutton_ctl_paint(void *);
58static ui_evclaim_t ui_pbutton_ctl_pos_event(void *, pos_event_t *);
59
60/** Push button control ops */
61ui_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 */
74errno_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 */
107void 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 */
121ui_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 */
132void 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 */
143void 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 */
156void 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 */
166static 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;
211error:
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 */
220static 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 */
233static 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 button text shadow.
242 *
243 * @param pbutton Push button
244 * @return EOK on success or an error code
245 */
246static errno_t ui_pbutton_paint_text_shadow(ui_pbutton_t *pbutton)
247{
248 gfx_rect_t rect;
249 errno_t rc;
250
251 rect.p0.x = pbutton->rect.p0.x + 1;
252 rect.p0.y = pbutton->rect.p0.y + 1;
253 rect.p1.x = pbutton->rect.p1.x;
254 rect.p1.y = pbutton->rect.p1.y;
255
256 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_shadow_color);
257 if (rc != EOK)
258 goto error;
259
260 rc = gfx_fill_rect(pbutton->res->gc, &rect);
261 if (rc != EOK)
262 goto error;
263
264 return EOK;
265error:
266 return rc;
267}
268
269/** Paint push button in graphic mode.
270 *
271 * @param pbutton Push button
272 * @return EOK on success or an error code
273 */
274static errno_t ui_pbutton_paint_gfx(ui_pbutton_t *pbutton)
275{
276 gfx_coord2_t pos;
277 gfx_text_fmt_t fmt;
278 gfx_rect_t rect;
279 gfx_coord_t thickness;
280 bool depressed;
281 errno_t rc;
282
283 thickness = pbutton->isdefault ? 2 : 1;
284 depressed = pbutton->held && pbutton->inside;
285
286 rect.p0.x = pbutton->rect.p0.x + thickness;
287 rect.p0.y = pbutton->rect.p0.y + thickness;
288 rect.p1.x = pbutton->rect.p1.x - thickness;
289 rect.p1.y = pbutton->rect.p1.y - thickness;
290
291 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
292 if (rc != EOK)
293 goto error;
294
295 rc = gfx_fill_rect(pbutton->res->gc, &rect);
296 if (rc != EOK)
297 goto error;
298
299 /* Center of button rectangle */
300 pos.x = (rect.p0.x + rect.p1.x) / 2;
301 pos.y = (rect.p0.y + rect.p1.y) / 2;
302
303 if (depressed) {
304 pos.x += ui_pb_press_dx;
305 pos.y += ui_pb_press_dy;
306 }
307
308 gfx_text_fmt_init(&fmt);
309 fmt.font = pbutton->res->font;
310 fmt.color = pbutton->res->btn_text_color;
311 fmt.halign = gfx_halign_center;
312 fmt.valign = gfx_valign_center;
313
314 rc = gfx_puttext(&pos, &fmt, pbutton->caption);
315 if (rc != EOK)
316 goto error;
317
318 rc = ui_pbutton_paint_frame(pbutton);
319 if (rc != EOK)
320 goto error;
321
322 if (depressed) {
323 rc = ui_pbutton_paint_inset(pbutton, &rect);
324 if (rc != EOK)
325 goto error;
326 } else {
327 rc = ui_pbutton_paint_outset(pbutton, &rect);
328 if (rc != EOK)
329 goto error;
330 }
331
332 rc = gfx_update(pbutton->res->gc);
333 if (rc != EOK)
334 goto error;
335
336 return EOK;
337error:
338 return rc;
339}
340
341/** Paint push button in text mode.
342 *
343 * @param pbutton Push button
344 * @return EOK on success or an error code
345 */
346static errno_t ui_pbutton_paint_text(ui_pbutton_t *pbutton)
347{
348 gfx_coord2_t pos;
349 gfx_text_fmt_t fmt;
350 gfx_rect_t rect;
351 bool depressed;
352 errno_t rc;
353
354 depressed = pbutton->held && pbutton->inside;
355
356 rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color);
357 if (rc != EOK)
358 goto error;
359
360 rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect);
361 if (rc != EOK)
362 goto error;
363
364 rect.p0.x = pbutton->rect.p0.x + (depressed ? 1 : 0);
365 rect.p0.y = pbutton->rect.p0.y;
366 rect.p1.x = pbutton->rect.p1.x - 1 + (depressed ? 1 : 0);
367 rect.p1.y = pbutton->rect.p0.y + 1;
368
369 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
370 if (rc != EOK)
371 goto error;
372
373 rc = gfx_fill_rect(pbutton->res->gc, &rect);
374 if (rc != EOK)
375 goto error;
376
377 /* Center of button rectangle */
378 pos.x = (rect.p0.x + rect.p1.x) / 2;
379 pos.y = (rect.p0.y + rect.p1.y) / 2;
380
381 gfx_text_fmt_init(&fmt);
382 fmt.font = pbutton->res->font;
383 fmt.color = pbutton->res->btn_text_color;
384 fmt.halign = gfx_halign_center;
385 fmt.valign = gfx_valign_center;
386
387 rc = gfx_puttext(&pos, &fmt, pbutton->caption);
388 if (rc != EOK)
389 goto error;
390
391 if (!depressed) {
392 rc = ui_pbutton_paint_text_shadow(pbutton);
393 if (rc != EOK)
394 goto error;
395 }
396
397 rc = gfx_update(pbutton->res->gc);
398 if (rc != EOK)
399 goto error;
400
401 return EOK;
402error:
403 return rc;
404}
405
406/** Paint push button.
407 *
408 * @param pbutton Push button
409 * @return EOK on success or an error code
410 */
411errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
412{
413 if (pbutton->res->textmode)
414 return ui_pbutton_paint_text(pbutton);
415 else
416 return ui_pbutton_paint_gfx(pbutton);
417}
418
419/** Press down button.
420 *
421 * @param pbutton Push button
422 */
423void ui_pbutton_press(ui_pbutton_t *pbutton)
424{
425 if (pbutton->held)
426 return;
427
428 pbutton->inside = true;
429 pbutton->held = true;
430 (void) ui_pbutton_paint(pbutton);
431}
432
433/** Release button.
434 *
435 * @param pbutton Push button
436 */
437void ui_pbutton_release(ui_pbutton_t *pbutton)
438{
439 if (!pbutton->held)
440 return;
441
442 pbutton->held = false;
443
444 if (pbutton->inside) {
445 (void) ui_pbutton_paint(pbutton);
446 ui_pbutton_clicked(pbutton);
447 }
448}
449
450/** Pointer entered button.
451 *
452 * @param pbutton Push button
453 */
454void ui_pbutton_enter(ui_pbutton_t *pbutton)
455{
456 if (pbutton->inside)
457 return;
458
459 pbutton->inside = true;
460 if (pbutton->held)
461 (void) ui_pbutton_paint(pbutton);
462}
463
464/** Pointer left button.
465 *
466 * @param pbutton Push button
467 */
468void ui_pbutton_leave(ui_pbutton_t *pbutton)
469{
470 if (!pbutton->inside)
471 return;
472
473 pbutton->inside = false;
474 if (pbutton->held)
475 (void) ui_pbutton_paint(pbutton);
476}
477
478/** Button was clicked.
479 *
480 * @param pbutton Push button
481 */
482void ui_pbutton_clicked(ui_pbutton_t *pbutton)
483{
484 if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
485 pbutton->cb->clicked(pbutton, pbutton->arg);
486}
487
488/** Handle push button position event.
489 *
490 * @param pbutton Push button
491 * @param pos_event Position event
492 * @return @c ui_claimed iff the event is claimed
493 */
494ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
495{
496 gfx_coord2_t pos;
497 bool inside;
498
499 pos.x = event->hpos;
500 pos.y = event->vpos;
501
502 inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
503
504 switch (event->type) {
505 case POS_PRESS:
506 if (inside) {
507 ui_pbutton_press(pbutton);
508 return ui_claimed;
509 }
510 break;
511 case POS_RELEASE:
512 if (pbutton->held) {
513 ui_pbutton_release(pbutton);
514 return ui_claimed;
515 }
516 break;
517 case POS_UPDATE:
518 if (inside && !pbutton->inside) {
519 ui_pbutton_enter(pbutton);
520 return ui_claimed;
521 } else if (!inside && pbutton->inside) {
522 ui_pbutton_leave(pbutton);
523 }
524 break;
525 case POS_DCLICK:
526 break;
527 }
528
529 return ui_unclaimed;
530}
531
532/** Destroy push button control.
533 *
534 * @param arg Argument (ui_pbutton_t *)
535 */
536void ui_pbutton_ctl_destroy(void *arg)
537{
538 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
539
540 ui_pbutton_destroy(pbutton);
541}
542
543/** Paint push button control.
544 *
545 * @param arg Argument (ui_pbutton_t *)
546 * @return EOK on success or an error code
547 */
548errno_t ui_pbutton_ctl_paint(void *arg)
549{
550 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
551
552 return ui_pbutton_paint(pbutton);
553}
554
555/** Handle push button control position event.
556 *
557 * @param arg Argument (ui_pbutton_t *)
558 * @param pos_event Position event
559 * @return @c ui_claimed iff the event is claimed
560 */
561ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
562{
563 ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
564
565 return ui_pbutton_pos_event(pbutton, event);
566}
567
568/** @}
569 */
Note: See TracBrowser for help on using the repository browser.