| 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 */
|
|---|
| 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 button text shadow.
|
|---|
| 242 | *
|
|---|
| 243 | * @param pbutton Push button
|
|---|
| 244 | * @return EOK on success or an error code
|
|---|
| 245 | */
|
|---|
| 246 | static 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;
|
|---|
| 265 | error:
|
|---|
| 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 | */
|
|---|
| 274 | static 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.color = pbutton->res->btn_text_color;
|
|---|
| 310 | fmt.halign = gfx_halign_center;
|
|---|
| 311 | fmt.valign = gfx_valign_center;
|
|---|
| 312 |
|
|---|
| 313 | rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
|
|---|
| 314 | if (rc != EOK)
|
|---|
| 315 | goto error;
|
|---|
| 316 |
|
|---|
| 317 | rc = ui_pbutton_paint_frame(pbutton);
|
|---|
| 318 | if (rc != EOK)
|
|---|
| 319 | goto error;
|
|---|
| 320 |
|
|---|
| 321 | if (depressed) {
|
|---|
| 322 | rc = ui_pbutton_paint_inset(pbutton, &rect);
|
|---|
| 323 | if (rc != EOK)
|
|---|
| 324 | goto error;
|
|---|
| 325 | } else {
|
|---|
| 326 | rc = ui_pbutton_paint_outset(pbutton, &rect);
|
|---|
| 327 | if (rc != EOK)
|
|---|
| 328 | goto error;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | rc = gfx_update(pbutton->res->gc);
|
|---|
| 332 | if (rc != EOK)
|
|---|
| 333 | goto error;
|
|---|
| 334 |
|
|---|
| 335 | return EOK;
|
|---|
| 336 | error:
|
|---|
| 337 | return rc;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | /** Paint push button in text mode.
|
|---|
| 341 | *
|
|---|
| 342 | * @param pbutton Push button
|
|---|
| 343 | * @return EOK on success or an error code
|
|---|
| 344 | */
|
|---|
| 345 | static errno_t ui_pbutton_paint_text(ui_pbutton_t *pbutton)
|
|---|
| 346 | {
|
|---|
| 347 | gfx_coord2_t pos;
|
|---|
| 348 | gfx_text_fmt_t fmt;
|
|---|
| 349 | gfx_rect_t rect;
|
|---|
| 350 | bool depressed;
|
|---|
| 351 | errno_t rc;
|
|---|
| 352 |
|
|---|
| 353 | depressed = pbutton->held && pbutton->inside;
|
|---|
| 354 |
|
|---|
| 355 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color);
|
|---|
| 356 | if (rc != EOK)
|
|---|
| 357 | goto error;
|
|---|
| 358 |
|
|---|
| 359 | rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect);
|
|---|
| 360 | if (rc != EOK)
|
|---|
| 361 | goto error;
|
|---|
| 362 |
|
|---|
| 363 | rect.p0.x = pbutton->rect.p0.x + (depressed ? 1 : 0);
|
|---|
| 364 | rect.p0.y = pbutton->rect.p0.y;
|
|---|
| 365 | rect.p1.x = pbutton->rect.p1.x - 1 + (depressed ? 1 : 0);
|
|---|
| 366 | rect.p1.y = pbutton->rect.p0.y + 1;
|
|---|
| 367 |
|
|---|
| 368 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_highlight_color);
|
|---|
| 369 | if (rc != EOK)
|
|---|
| 370 | goto error;
|
|---|
| 371 |
|
|---|
| 372 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
|---|
| 373 | if (rc != EOK)
|
|---|
| 374 | goto error;
|
|---|
| 375 |
|
|---|
| 376 | /* Center of button rectangle */
|
|---|
| 377 | pos.x = (rect.p0.x + rect.p1.x) / 2;
|
|---|
| 378 | pos.y = (rect.p0.y + rect.p1.y) / 2;
|
|---|
| 379 |
|
|---|
| 380 | gfx_text_fmt_init(&fmt);
|
|---|
| 381 | fmt.color = pbutton->res->btn_text_color;
|
|---|
| 382 | fmt.halign = gfx_halign_center;
|
|---|
| 383 | fmt.valign = gfx_valign_center;
|
|---|
| 384 |
|
|---|
| 385 | rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
|
|---|
| 386 | if (rc != EOK)
|
|---|
| 387 | goto error;
|
|---|
| 388 |
|
|---|
| 389 | if (!depressed) {
|
|---|
| 390 | rc = ui_pbutton_paint_text_shadow(pbutton);
|
|---|
| 391 | if (rc != EOK)
|
|---|
| 392 | goto error;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | rc = gfx_update(pbutton->res->gc);
|
|---|
| 396 | if (rc != EOK)
|
|---|
| 397 | goto error;
|
|---|
| 398 |
|
|---|
| 399 | return EOK;
|
|---|
| 400 | error:
|
|---|
| 401 | return rc;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | /** Paint push button.
|
|---|
| 405 | *
|
|---|
| 406 | * @param pbutton Push button
|
|---|
| 407 | * @return EOK on success or an error code
|
|---|
| 408 | */
|
|---|
| 409 | errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
|
|---|
| 410 | {
|
|---|
| 411 | if (pbutton->res->textmode)
|
|---|
| 412 | return ui_pbutton_paint_text(pbutton);
|
|---|
| 413 | else
|
|---|
| 414 | return ui_pbutton_paint_gfx(pbutton);
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | /** Press down button.
|
|---|
| 418 | *
|
|---|
| 419 | * @param pbutton Push button
|
|---|
| 420 | */
|
|---|
| 421 | void ui_pbutton_press(ui_pbutton_t *pbutton)
|
|---|
| 422 | {
|
|---|
| 423 | if (pbutton->held)
|
|---|
| 424 | return;
|
|---|
| 425 |
|
|---|
| 426 | pbutton->inside = true;
|
|---|
| 427 | pbutton->held = true;
|
|---|
| 428 | (void) ui_pbutton_paint(pbutton);
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | /** Release button.
|
|---|
| 432 | *
|
|---|
| 433 | * @param pbutton Push button
|
|---|
| 434 | */
|
|---|
| 435 | void ui_pbutton_release(ui_pbutton_t *pbutton)
|
|---|
| 436 | {
|
|---|
| 437 | if (!pbutton->held)
|
|---|
| 438 | return;
|
|---|
| 439 |
|
|---|
| 440 | pbutton->held = false;
|
|---|
| 441 |
|
|---|
| 442 | if (pbutton->inside) {
|
|---|
| 443 | (void) ui_pbutton_paint(pbutton);
|
|---|
| 444 | ui_pbutton_clicked(pbutton);
|
|---|
| 445 | }
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | /** Pointer entered button.
|
|---|
| 449 | *
|
|---|
| 450 | * @param pbutton Push button
|
|---|
| 451 | */
|
|---|
| 452 | void ui_pbutton_enter(ui_pbutton_t *pbutton)
|
|---|
| 453 | {
|
|---|
| 454 | if (pbutton->inside)
|
|---|
| 455 | return;
|
|---|
| 456 |
|
|---|
| 457 | pbutton->inside = true;
|
|---|
| 458 | if (pbutton->held)
|
|---|
| 459 | (void) ui_pbutton_paint(pbutton);
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | /** Pointer left button.
|
|---|
| 463 | *
|
|---|
| 464 | * @param pbutton Push button
|
|---|
| 465 | */
|
|---|
| 466 | void ui_pbutton_leave(ui_pbutton_t *pbutton)
|
|---|
| 467 | {
|
|---|
| 468 | if (!pbutton->inside)
|
|---|
| 469 | return;
|
|---|
| 470 |
|
|---|
| 471 | pbutton->inside = false;
|
|---|
| 472 | if (pbutton->held)
|
|---|
| 473 | (void) ui_pbutton_paint(pbutton);
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | /** Button was clicked.
|
|---|
| 477 | *
|
|---|
| 478 | * @param pbutton Push button
|
|---|
| 479 | */
|
|---|
| 480 | void ui_pbutton_clicked(ui_pbutton_t *pbutton)
|
|---|
| 481 | {
|
|---|
| 482 | if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
|
|---|
| 483 | pbutton->cb->clicked(pbutton, pbutton->arg);
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | /** Handle push button position event.
|
|---|
| 487 | *
|
|---|
| 488 | * @param pbutton Push button
|
|---|
| 489 | * @param pos_event Position event
|
|---|
| 490 | * @return @c ui_claimed iff the event is claimed
|
|---|
| 491 | */
|
|---|
| 492 | ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
|
|---|
| 493 | {
|
|---|
| 494 | gfx_coord2_t pos;
|
|---|
| 495 | bool inside;
|
|---|
| 496 |
|
|---|
| 497 | pos.x = event->hpos;
|
|---|
| 498 | pos.y = event->vpos;
|
|---|
| 499 |
|
|---|
| 500 | inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
|
|---|
| 501 |
|
|---|
| 502 | switch (event->type) {
|
|---|
| 503 | case POS_PRESS:
|
|---|
| 504 | if (inside) {
|
|---|
| 505 | ui_pbutton_press(pbutton);
|
|---|
| 506 | return ui_claimed;
|
|---|
| 507 | }
|
|---|
| 508 | break;
|
|---|
| 509 | case POS_RELEASE:
|
|---|
| 510 | if (pbutton->held) {
|
|---|
| 511 | ui_pbutton_release(pbutton);
|
|---|
| 512 | return ui_claimed;
|
|---|
| 513 | }
|
|---|
| 514 | break;
|
|---|
| 515 | case POS_UPDATE:
|
|---|
| 516 | if (inside && !pbutton->inside) {
|
|---|
| 517 | ui_pbutton_enter(pbutton);
|
|---|
| 518 | return ui_claimed;
|
|---|
| 519 | } else if (!inside && pbutton->inside) {
|
|---|
| 520 | ui_pbutton_leave(pbutton);
|
|---|
| 521 | }
|
|---|
| 522 | break;
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | return ui_unclaimed;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | /** Destroy push button control.
|
|---|
| 529 | *
|
|---|
| 530 | * @param arg Argument (ui_pbutton_t *)
|
|---|
| 531 | */
|
|---|
| 532 | void ui_pbutton_ctl_destroy(void *arg)
|
|---|
| 533 | {
|
|---|
| 534 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
|---|
| 535 |
|
|---|
| 536 | ui_pbutton_destroy(pbutton);
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | /** Paint push button control.
|
|---|
| 540 | *
|
|---|
| 541 | * @param arg Argument (ui_pbutton_t *)
|
|---|
| 542 | * @return EOK on success or an error code
|
|---|
| 543 | */
|
|---|
| 544 | errno_t ui_pbutton_ctl_paint(void *arg)
|
|---|
| 545 | {
|
|---|
| 546 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
|---|
| 547 |
|
|---|
| 548 | return ui_pbutton_paint(pbutton);
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | /** Handle push button control position event.
|
|---|
| 552 | *
|
|---|
| 553 | * @param arg Argument (ui_pbutton_t *)
|
|---|
| 554 | * @param pos_event Position event
|
|---|
| 555 | * @return @c ui_claimed iff the event is claimed
|
|---|
| 556 | */
|
|---|
| 557 | ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
|
|---|
| 558 | {
|
|---|
| 559 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
|---|
| 560 |
|
|---|
| 561 | return ui_pbutton_pos_event(pbutton, event);
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | /** @}
|
|---|
| 565 | */
|
|---|