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
RevLine 
[f80690a]1/*
[2ab8ab3]2 * Copyright (c) 2021 Jiri Svoboda
[f80690a]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>
[47728678]37#include <gfx/color.h>
38#include <gfx/context.h>
39#include <gfx/render.h>
40#include <gfx/text.h>
[faca61b8]41#include <io/pos_event.h>
[f80690a]42#include <stdlib.h>
[47728678]43#include <str.h>
[8009dc27]44#include <ui/control.h>
[de9992c]45#include <ui/paint.h>
[f80690a]46#include <ui/pbutton.h>
47#include "../private/pbutton.h"
[47728678]48#include "../private/resource.h"
[f80690a]49
[f6df5a3]50/** Caption movement when button is pressed down */
51enum {
[c9a7adc]52 ui_pb_press_dx = 1,
53 ui_pb_press_dy = 1
[f6df5a3]54};
55
[c6f00b40]56static void ui_pbutton_ctl_destroy(void *);
[4df6607]57static errno_t ui_pbutton_ctl_paint(void *);
[8009dc27]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 = {
[c6f00b40]62 .destroy = ui_pbutton_ctl_destroy,
[4df6607]63 .paint = ui_pbutton_ctl_paint,
[8009dc27]64 .pos_event = ui_pbutton_ctl_pos_event
65};
66
[f80690a]67/** Create new push button.
68 *
[47728678]69 * @param resource UI resource
[f80690a]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 */
[3583ffb]74errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
[47728678]75 ui_pbutton_t **rpbutton)
[f80690a]76{
77 ui_pbutton_t *pbutton;
[8009dc27]78 errno_t rc;
[f80690a]79
80 pbutton = calloc(1, sizeof(ui_pbutton_t));
81 if (pbutton == NULL)
82 return ENOMEM;
83
[8009dc27]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
[47728678]91 pbutton->caption = str_dup(caption);
92 if (pbutton->caption == NULL) {
[8009dc27]93 ui_control_delete(pbutton->control);
[47728678]94 free(pbutton);
95 return ENOMEM;
96 }
97
[3583ffb]98 pbutton->res = resource;
[f80690a]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
[8009dc27]112 ui_control_delete(pbutton->control);
[f80690a]113 free(pbutton);
114}
115
[8009dc27]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
[8ef48ece]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
[47728678]138/** Set button rectangle.
139 *
140 * @param pbutton Button
[ba09d06]141 * @param rect New button rectangle
[47728678]142 */
143void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
144{
145 pbutton->rect = *rect;
146}
147
[c9a7adc]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.
[47728678]162 *
163 * @param pbutton Push button
164 * @return EOK on success or an error code
165 */
[c9a7adc]166static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
[47728678]167{
[c9a7adc]168 gfx_rect_t rect;
169 gfx_coord_t thickness;
[47728678]170 errno_t rc;
171
[c9a7adc]172 thickness = pbutton->isdefault ? 2 : 1;
173
[de9992c]174 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_frame_color);
[c9a7adc]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{
[de9992c]223 return ui_paint_bevel(pbutton->res->gc, rect,
224 pbutton->res->btn_highlight_color,
225 pbutton->res->btn_shadow_color, 2, NULL);
[c9a7adc]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{
[de9992c]236 return ui_paint_bevel(pbutton->res->gc, rect,
237 pbutton->res->btn_shadow_color,
238 pbutton->res->btn_face_color, 2, NULL);
[c9a7adc]239}
240
[cd74fa8]241/** Paint button text shadow.
[c9a7adc]242 *
243 * @param pbutton Push button
244 * @return EOK on success or an error code
245 */
[cd74fa8]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)
[c9a7adc]275{
276 gfx_coord2_t pos;
277 gfx_text_fmt_t fmt;
278 gfx_rect_t rect;
279 gfx_coord_t thickness;
[8ef48ece]280 bool depressed;
[c9a7adc]281 errno_t rc;
282
283 thickness = pbutton->isdefault ? 2 : 1;
[8ef48ece]284 depressed = pbutton->held && pbutton->inside;
[c9a7adc]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
[de9992c]291 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
[c9a7adc]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
[47728678]299 /* Center of button rectangle */
[c9a7adc]300 pos.x = (rect.p0.x + rect.p1.x) / 2;
301 pos.y = (rect.p0.y + rect.p1.y) / 2;
[47728678]302
[8ef48ece]303 if (depressed) {
[f6df5a3]304 pos.x += ui_pb_press_dx;
305 pos.y += ui_pb_press_dy;
306 }
307
[47728678]308 gfx_text_fmt_init(&fmt);
[4583015]309 fmt.font = pbutton->res->font;
[b433f68]310 fmt.color = pbutton->res->btn_text_color;
[47728678]311 fmt.halign = gfx_halign_center;
312 fmt.valign = gfx_valign_center;
313
[4583015]314 rc = gfx_puttext(&pos, &fmt, pbutton->caption);
[47728678]315 if (rc != EOK)
316 goto error;
317
[c9a7adc]318 rc = ui_pbutton_paint_frame(pbutton);
319 if (rc != EOK)
320 goto error;
321
[8ef48ece]322 if (depressed) {
[c9a7adc]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 }
[47728678]331
[2ab8ab3]332 rc = gfx_update(pbutton->res->gc);
333 if (rc != EOK)
334 goto error;
335
[47728678]336 return EOK;
337error:
338 return rc;
339}
340
[cd74fa8]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
[bc52b5b]369 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
[cd74fa8]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);
[4583015]382 fmt.font = pbutton->res->font;
[cd74fa8]383 fmt.color = pbutton->res->btn_text_color;
384 fmt.halign = gfx_halign_center;
385 fmt.valign = gfx_valign_center;
386
[4583015]387 rc = gfx_puttext(&pos, &fmt, pbutton->caption);
[cd74fa8]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
[f6df5a3]419/** Press down button.
420 *
421 * @param pbutton Push button
422 */
423void ui_pbutton_press(ui_pbutton_t *pbutton)
424{
[8ef48ece]425 if (pbutton->held)
426 return;
427
428 pbutton->inside = true;
[f6df5a3]429 pbutton->held = true;
[8ef48ece]430 (void) ui_pbutton_paint(pbutton);
[f6df5a3]431}
432
433/** Release button.
434 *
435 * @param pbutton Push button
436 */
437void ui_pbutton_release(ui_pbutton_t *pbutton)
438{
[8ef48ece]439 if (!pbutton->held)
440 return;
441
[f6df5a3]442 pbutton->held = false;
[8ef48ece]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);
[f6df5a3]486}
487
[faca61b8]488/** Handle push button position event.
489 *
490 * @param pbutton Push button
491 * @param pos_event Position event
[a2f173b]492 * @return @c ui_claimed iff the event is claimed
[faca61b8]493 */
[a2f173b]494ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
[faca61b8]495{
496 gfx_coord2_t pos;
[8ef48ece]497 bool inside;
[faca61b8]498
499 pos.x = event->hpos;
500 pos.y = event->vpos;
501
[8ef48ece]502 inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
[faca61b8]503
[8ef48ece]504 switch (event->type) {
505 case POS_PRESS:
[a2f173b]506 if (inside) {
[8ef48ece]507 ui_pbutton_press(pbutton);
[a2f173b]508 return ui_claimed;
509 }
[8ef48ece]510 break;
511 case POS_RELEASE:
[a2f173b]512 if (pbutton->held) {
513 ui_pbutton_release(pbutton);
514 return ui_claimed;
515 }
[8ef48ece]516 break;
517 case POS_UPDATE:
518 if (inside && !pbutton->inside) {
519 ui_pbutton_enter(pbutton);
[a2f173b]520 return ui_claimed;
[8ef48ece]521 } else if (!inside && pbutton->inside) {
522 ui_pbutton_leave(pbutton);
523 }
524 break;
[8edec53]525 case POS_DCLICK:
526 break;
[faca61b8]527 }
[a2f173b]528
529 return ui_unclaimed;
[faca61b8]530}
531
[c6f00b40]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
[4df6607]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
[8009dc27]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
[f80690a]568/** @}
569 */
Note: See TracBrowser for help on using the repository browser.