[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 */
|
---|
| 51 | enum {
|
---|
[c9a7adc] | 52 | ui_pb_press_dx = 1,
|
---|
| 53 | ui_pb_press_dy = 1
|
---|
[f6df5a3] | 54 | };
|
---|
| 55 |
|
---|
[c6f00b40] | 56 | static void ui_pbutton_ctl_destroy(void *);
|
---|
[4df6607] | 57 | static errno_t ui_pbutton_ctl_paint(void *);
|
---|
[8009dc27] | 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 = {
|
---|
[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] | 74 | errno_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 | */
|
---|
| 107 | void 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 | */
|
---|
| 121 | ui_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 | */
|
---|
| 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 |
|
---|
[47728678] | 138 | /** Set button rectangle.
|
---|
| 139 | *
|
---|
| 140 | * @param pbutton Button
|
---|
[ba09d06] | 141 | * @param rect New button rectangle
|
---|
[47728678] | 142 | */
|
---|
| 143 | void 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 | */
|
---|
| 156 | void 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] | 166 | static 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;
|
---|
| 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 | {
|
---|
[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 | */
|
---|
| 233 | static 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] | 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)
|
---|
[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);
|
---|
[b433f68] | 309 | fmt.color = pbutton->res->btn_text_color;
|
---|
[47728678] | 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 |
|
---|
[c9a7adc] | 317 | rc = ui_pbutton_paint_frame(pbutton);
|
---|
| 318 | if (rc != EOK)
|
---|
| 319 | goto error;
|
---|
| 320 |
|
---|
[8ef48ece] | 321 | if (depressed) {
|
---|
[c9a7adc] | 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 | }
|
---|
[47728678] | 330 |
|
---|
[2ab8ab3] | 331 | rc = gfx_update(pbutton->res->gc);
|
---|
| 332 | if (rc != EOK)
|
---|
| 333 | goto error;
|
---|
| 334 |
|
---|
[47728678] | 335 | return EOK;
|
---|
| 336 | error:
|
---|
| 337 | return rc;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[cd74fa8] | 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 |
|
---|
[f6df5a3] | 417 | /** Press down button.
|
---|
| 418 | *
|
---|
| 419 | * @param pbutton Push button
|
---|
| 420 | */
|
---|
| 421 | void ui_pbutton_press(ui_pbutton_t *pbutton)
|
---|
| 422 | {
|
---|
[8ef48ece] | 423 | if (pbutton->held)
|
---|
| 424 | return;
|
---|
| 425 |
|
---|
| 426 | pbutton->inside = true;
|
---|
[f6df5a3] | 427 | pbutton->held = true;
|
---|
[8ef48ece] | 428 | (void) ui_pbutton_paint(pbutton);
|
---|
[f6df5a3] | 429 | }
|
---|
| 430 |
|
---|
| 431 | /** Release button.
|
---|
| 432 | *
|
---|
| 433 | * @param pbutton Push button
|
---|
| 434 | */
|
---|
| 435 | void ui_pbutton_release(ui_pbutton_t *pbutton)
|
---|
| 436 | {
|
---|
[8ef48ece] | 437 | if (!pbutton->held)
|
---|
| 438 | return;
|
---|
| 439 |
|
---|
[f6df5a3] | 440 | pbutton->held = false;
|
---|
[8ef48ece] | 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);
|
---|
[f6df5a3] | 484 | }
|
---|
| 485 |
|
---|
[faca61b8] | 486 | /** Handle push button position event.
|
---|
| 487 | *
|
---|
| 488 | * @param pbutton Push button
|
---|
| 489 | * @param pos_event Position event
|
---|
[a2f173b] | 490 | * @return @c ui_claimed iff the event is claimed
|
---|
[faca61b8] | 491 | */
|
---|
[a2f173b] | 492 | ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
|
---|
[faca61b8] | 493 | {
|
---|
| 494 | gfx_coord2_t pos;
|
---|
[8ef48ece] | 495 | bool inside;
|
---|
[faca61b8] | 496 |
|
---|
| 497 | pos.x = event->hpos;
|
---|
| 498 | pos.y = event->vpos;
|
---|
| 499 |
|
---|
[8ef48ece] | 500 | inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
|
---|
[faca61b8] | 501 |
|
---|
[8ef48ece] | 502 | switch (event->type) {
|
---|
| 503 | case POS_PRESS:
|
---|
[a2f173b] | 504 | if (inside) {
|
---|
[8ef48ece] | 505 | ui_pbutton_press(pbutton);
|
---|
[a2f173b] | 506 | return ui_claimed;
|
---|
| 507 | }
|
---|
[8ef48ece] | 508 | break;
|
---|
| 509 | case POS_RELEASE:
|
---|
[a2f173b] | 510 | if (pbutton->held) {
|
---|
| 511 | ui_pbutton_release(pbutton);
|
---|
| 512 | return ui_claimed;
|
---|
| 513 | }
|
---|
[8ef48ece] | 514 | break;
|
---|
| 515 | case POS_UPDATE:
|
---|
| 516 | if (inside && !pbutton->inside) {
|
---|
| 517 | ui_pbutton_enter(pbutton);
|
---|
[a2f173b] | 518 | return ui_claimed;
|
---|
[8ef48ece] | 519 | } else if (!inside && pbutton->inside) {
|
---|
| 520 | ui_pbutton_leave(pbutton);
|
---|
| 521 | }
|
---|
| 522 | break;
|
---|
[faca61b8] | 523 | }
|
---|
[a2f173b] | 524 |
|
---|
| 525 | return ui_unclaimed;
|
---|
[faca61b8] | 526 | }
|
---|
| 527 |
|
---|
[c6f00b40] | 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 |
|
---|
[4df6607] | 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 |
|
---|
[8009dc27] | 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 |
|
---|
[f80690a] | 564 | /** @}
|
---|
| 565 | */
|
---|