[f80690a] | 1 | /*
|
---|
[d68239a1] | 2 | * Copyright (c) 2022 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
|
---|
[d68239a1] | 34 | *
|
---|
| 35 | * Push button either uses text as decoration, or it can use a caller-provided
|
---|
| 36 | * function to paint the decoration.
|
---|
[f80690a] | 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <errno.h>
|
---|
[47728678] | 40 | #include <gfx/color.h>
|
---|
| 41 | #include <gfx/context.h>
|
---|
| 42 | #include <gfx/render.h>
|
---|
| 43 | #include <gfx/text.h>
|
---|
[faca61b8] | 44 | #include <io/pos_event.h>
|
---|
[f80690a] | 45 | #include <stdlib.h>
|
---|
[47728678] | 46 | #include <str.h>
|
---|
[8009dc27] | 47 | #include <ui/control.h>
|
---|
[de9992c] | 48 | #include <ui/paint.h>
|
---|
[f80690a] | 49 | #include <ui/pbutton.h>
|
---|
| 50 | #include "../private/pbutton.h"
|
---|
[47728678] | 51 | #include "../private/resource.h"
|
---|
[f80690a] | 52 |
|
---|
[f6df5a3] | 53 | /** Caption movement when button is pressed down */
|
---|
| 54 | enum {
|
---|
[c9a7adc] | 55 | ui_pb_press_dx = 1,
|
---|
| 56 | ui_pb_press_dy = 1
|
---|
[f6df5a3] | 57 | };
|
---|
| 58 |
|
---|
[c6f00b40] | 59 | static void ui_pbutton_ctl_destroy(void *);
|
---|
[4df6607] | 60 | static errno_t ui_pbutton_ctl_paint(void *);
|
---|
[8009dc27] | 61 | static ui_evclaim_t ui_pbutton_ctl_pos_event(void *, pos_event_t *);
|
---|
| 62 |
|
---|
| 63 | /** Push button control ops */
|
---|
| 64 | ui_control_ops_t ui_pbutton_ops = {
|
---|
[c6f00b40] | 65 | .destroy = ui_pbutton_ctl_destroy,
|
---|
[4df6607] | 66 | .paint = ui_pbutton_ctl_paint,
|
---|
[8009dc27] | 67 | .pos_event = ui_pbutton_ctl_pos_event
|
---|
| 68 | };
|
---|
| 69 |
|
---|
[f80690a] | 70 | /** Create new push button.
|
---|
| 71 | *
|
---|
[47728678] | 72 | * @param resource UI resource
|
---|
[f80690a] | 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 | */
|
---|
[3583ffb] | 77 | errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
|
---|
[47728678] | 78 | ui_pbutton_t **rpbutton)
|
---|
[f80690a] | 79 | {
|
---|
| 80 | ui_pbutton_t *pbutton;
|
---|
[8009dc27] | 81 | errno_t rc;
|
---|
[f80690a] | 82 |
|
---|
| 83 | pbutton = calloc(1, sizeof(ui_pbutton_t));
|
---|
| 84 | if (pbutton == NULL)
|
---|
| 85 | return ENOMEM;
|
---|
| 86 |
|
---|
[8009dc27] | 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 |
|
---|
[47728678] | 94 | pbutton->caption = str_dup(caption);
|
---|
| 95 | if (pbutton->caption == NULL) {
|
---|
[8009dc27] | 96 | ui_control_delete(pbutton->control);
|
---|
[47728678] | 97 | free(pbutton);
|
---|
| 98 | return ENOMEM;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[3583ffb] | 101 | pbutton->res = resource;
|
---|
[f80690a] | 102 | *rpbutton = pbutton;
|
---|
| 103 | return EOK;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** Destroy push button.
|
---|
| 107 | *
|
---|
| 108 | * @param pbutton Push button or @c NULL
|
---|
| 109 | */
|
---|
| 110 | void ui_pbutton_destroy(ui_pbutton_t *pbutton)
|
---|
| 111 | {
|
---|
| 112 | if (pbutton == NULL)
|
---|
| 113 | return;
|
---|
| 114 |
|
---|
[8009dc27] | 115 | ui_control_delete(pbutton->control);
|
---|
[f80690a] | 116 | free(pbutton);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[8009dc27] | 119 | /** Get base control from push button.
|
---|
| 120 | *
|
---|
| 121 | * @param pbutton Push button
|
---|
| 122 | * @return Control
|
---|
| 123 | */
|
---|
| 124 | ui_control_t *ui_pbutton_ctl(ui_pbutton_t *pbutton)
|
---|
| 125 | {
|
---|
| 126 | return pbutton->control;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[8ef48ece] | 129 | /** Set push button callbacks.
|
---|
| 130 | *
|
---|
| 131 | * @param pbutton Push button
|
---|
| 132 | * @param cb Push button callbacks
|
---|
| 133 | * @param arg Callback argument
|
---|
| 134 | */
|
---|
| 135 | void 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 |
|
---|
[d68239a1] | 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 | */
|
---|
| 147 | void 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 |
|
---|
[8b22d44] | 154 | /** Set push button flag.s
|
---|
| 155 | *
|
---|
| 156 | * @param pbutton Push button
|
---|
| 157 | * @param flags Flags
|
---|
| 158 | */
|
---|
| 159 | void ui_pbutton_set_flags(ui_pbutton_t *pbutton, ui_pbutton_flags_t flags)
|
---|
| 160 | {
|
---|
| 161 | pbutton->flags = flags;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[47728678] | 164 | /** Set button rectangle.
|
---|
| 165 | *
|
---|
| 166 | * @param pbutton Button
|
---|
[ba09d06] | 167 | * @param rect New button rectangle
|
---|
[47728678] | 168 | */
|
---|
| 169 | void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
|
---|
| 170 | {
|
---|
| 171 | pbutton->rect = *rect;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[c9a7adc] | 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 | */
|
---|
| 182 | void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault)
|
---|
| 183 | {
|
---|
| 184 | pbutton->isdefault = isdefault;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /** Paint outer button frame.
|
---|
[47728678] | 188 | *
|
---|
| 189 | * @param pbutton Push button
|
---|
| 190 | * @return EOK on success or an error code
|
---|
| 191 | */
|
---|
[c9a7adc] | 192 | static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
|
---|
[47728678] | 193 | {
|
---|
[c9a7adc] | 194 | gfx_rect_t rect;
|
---|
| 195 | gfx_coord_t thickness;
|
---|
[47728678] | 196 | errno_t rc;
|
---|
| 197 |
|
---|
[c9a7adc] | 198 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
| 199 |
|
---|
[de9992c] | 200 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_frame_color);
|
---|
[c9a7adc] | 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;
|
---|
| 237 | error:
|
---|
| 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 | */
|
---|
| 246 | static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton,
|
---|
| 247 | gfx_rect_t *rect)
|
---|
| 248 | {
|
---|
[de9992c] | 249 | return ui_paint_bevel(pbutton->res->gc, rect,
|
---|
| 250 | pbutton->res->btn_highlight_color,
|
---|
| 251 | pbutton->res->btn_shadow_color, 2, NULL);
|
---|
[c9a7adc] | 252 | }
|
---|
| 253 |
|
---|
| 254 | /** Paint inset button bevel.
|
---|
| 255 | *
|
---|
| 256 | * @param pbutton Push button
|
---|
| 257 | * @return EOK on success or an error code
|
---|
| 258 | */
|
---|
| 259 | static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton,
|
---|
| 260 | gfx_rect_t *rect)
|
---|
| 261 | {
|
---|
[de9992c] | 262 | return ui_paint_bevel(pbutton->res->gc, rect,
|
---|
| 263 | pbutton->res->btn_shadow_color,
|
---|
| 264 | pbutton->res->btn_face_color, 2, NULL);
|
---|
[c9a7adc] | 265 | }
|
---|
| 266 |
|
---|
[cd74fa8] | 267 | /** Paint button text shadow.
|
---|
[c9a7adc] | 268 | *
|
---|
| 269 | * @param pbutton Push button
|
---|
| 270 | * @return EOK on success or an error code
|
---|
| 271 | */
|
---|
[cd74fa8] | 272 | static 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;
|
---|
| 291 | error:
|
---|
| 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 | */
|
---|
| 300 | static errno_t ui_pbutton_paint_gfx(ui_pbutton_t *pbutton)
|
---|
[c9a7adc] | 301 | {
|
---|
| 302 | gfx_coord2_t pos;
|
---|
| 303 | gfx_text_fmt_t fmt;
|
---|
| 304 | gfx_rect_t rect;
|
---|
| 305 | gfx_coord_t thickness;
|
---|
[8ef48ece] | 306 | bool depressed;
|
---|
[c9a7adc] | 307 | errno_t rc;
|
---|
| 308 |
|
---|
| 309 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
[8ef48ece] | 310 | depressed = pbutton->held && pbutton->inside;
|
---|
[c9a7adc] | 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 |
|
---|
[de9992c] | 317 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
|
---|
[c9a7adc] | 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 |
|
---|
[47728678] | 325 | /* Center of button rectangle */
|
---|
[c9a7adc] | 326 | pos.x = (rect.p0.x + rect.p1.x) / 2;
|
---|
| 327 | pos.y = (rect.p0.y + rect.p1.y) / 2;
|
---|
[47728678] | 328 |
|
---|
[8ef48ece] | 329 | if (depressed) {
|
---|
[f6df5a3] | 330 | pos.x += ui_pb_press_dx;
|
---|
| 331 | pos.y += ui_pb_press_dy;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[d68239a1] | 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 | }
|
---|
[47728678] | 352 |
|
---|
[c9a7adc] | 353 | rc = ui_pbutton_paint_frame(pbutton);
|
---|
| 354 | if (rc != EOK)
|
---|
| 355 | goto error;
|
---|
| 356 |
|
---|
[8ef48ece] | 357 | if (depressed) {
|
---|
[c9a7adc] | 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 | }
|
---|
[47728678] | 366 |
|
---|
[2ab8ab3] | 367 | rc = gfx_update(pbutton->res->gc);
|
---|
| 368 | if (rc != EOK)
|
---|
| 369 | goto error;
|
---|
| 370 |
|
---|
[47728678] | 371 | return EOK;
|
---|
| 372 | error:
|
---|
| 373 | return rc;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[cd74fa8] | 376 | /** Paint push button in text mode.
|
---|
| 377 | *
|
---|
| 378 | * @param pbutton Push button
|
---|
| 379 | * @return EOK on success or an error code
|
---|
| 380 | */
|
---|
| 381 | static 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 |
|
---|
[8b22d44] | 389 | if ((pbutton->flags & ui_pbf_no_text_depress) == 0)
|
---|
| 390 | depressed = pbutton->held && pbutton->inside;
|
---|
| 391 | else
|
---|
| 392 | depressed = false;
|
---|
[cd74fa8] | 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 |
|
---|
[bc52b5b] | 407 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
|
---|
[cd74fa8] | 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);
|
---|
[4583015] | 420 | fmt.font = pbutton->res->font;
|
---|
[cd74fa8] | 421 | fmt.color = pbutton->res->btn_text_color;
|
---|
| 422 | fmt.halign = gfx_halign_center;
|
---|
| 423 | fmt.valign = gfx_valign_center;
|
---|
| 424 |
|
---|
[4583015] | 425 | rc = gfx_puttext(&pos, &fmt, pbutton->caption);
|
---|
[cd74fa8] | 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;
|
---|
| 440 | error:
|
---|
| 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 | */
|
---|
| 449 | errno_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 |
|
---|
[f6df5a3] | 457 | /** Press down button.
|
---|
| 458 | *
|
---|
| 459 | * @param pbutton Push button
|
---|
| 460 | */
|
---|
| 461 | void ui_pbutton_press(ui_pbutton_t *pbutton)
|
---|
| 462 | {
|
---|
[8ef48ece] | 463 | if (pbutton->held)
|
---|
| 464 | return;
|
---|
| 465 |
|
---|
| 466 | pbutton->inside = true;
|
---|
[f6df5a3] | 467 | pbutton->held = true;
|
---|
[8ef48ece] | 468 | (void) ui_pbutton_paint(pbutton);
|
---|
[d4ea1f6] | 469 | ui_pbutton_down(pbutton);
|
---|
[f6df5a3] | 470 | }
|
---|
| 471 |
|
---|
| 472 | /** Release button.
|
---|
| 473 | *
|
---|
| 474 | * @param pbutton Push button
|
---|
| 475 | */
|
---|
| 476 | void ui_pbutton_release(ui_pbutton_t *pbutton)
|
---|
| 477 | {
|
---|
[8ef48ece] | 478 | if (!pbutton->held)
|
---|
| 479 | return;
|
---|
| 480 |
|
---|
[f6df5a3] | 481 | pbutton->held = false;
|
---|
[174be87] | 482 | ui_pbutton_up(pbutton);
|
---|
[8ef48ece] | 483 |
|
---|
| 484 | if (pbutton->inside) {
|
---|
| 485 | (void) ui_pbutton_paint(pbutton);
|
---|
| 486 | ui_pbutton_clicked(pbutton);
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | /** Pointer entered button.
|
---|
| 491 | *
|
---|
| 492 | * @param pbutton Push button
|
---|
| 493 | */
|
---|
| 494 | void ui_pbutton_enter(ui_pbutton_t *pbutton)
|
---|
| 495 | {
|
---|
| 496 | if (pbutton->inside)
|
---|
| 497 | return;
|
---|
| 498 |
|
---|
| 499 | pbutton->inside = true;
|
---|
| 500 | if (pbutton->held)
|
---|
| 501 | (void) ui_pbutton_paint(pbutton);
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | /** Pointer left button.
|
---|
| 505 | *
|
---|
| 506 | * @param pbutton Push button
|
---|
| 507 | */
|
---|
| 508 | void ui_pbutton_leave(ui_pbutton_t *pbutton)
|
---|
| 509 | {
|
---|
| 510 | if (!pbutton->inside)
|
---|
| 511 | return;
|
---|
| 512 |
|
---|
| 513 | pbutton->inside = false;
|
---|
| 514 | if (pbutton->held)
|
---|
| 515 | (void) ui_pbutton_paint(pbutton);
|
---|
| 516 | }
|
---|
| 517 |
|
---|
[d4ea1f6] | 518 | /** Send button clicked event.
|
---|
[8ef48ece] | 519 | *
|
---|
| 520 | * @param pbutton Push button
|
---|
| 521 | */
|
---|
| 522 | void ui_pbutton_clicked(ui_pbutton_t *pbutton)
|
---|
| 523 | {
|
---|
| 524 | if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
|
---|
| 525 | pbutton->cb->clicked(pbutton, pbutton->arg);
|
---|
[f6df5a3] | 526 | }
|
---|
| 527 |
|
---|
[d4ea1f6] | 528 | /** Send button down event.
|
---|
| 529 | *
|
---|
| 530 | * @param pbutton Push button
|
---|
| 531 | */
|
---|
| 532 | void ui_pbutton_down(ui_pbutton_t *pbutton)
|
---|
| 533 | {
|
---|
| 534 | if (pbutton->cb != NULL && pbutton->cb->down != NULL)
|
---|
| 535 | pbutton->cb->down(pbutton, pbutton->arg);
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | /** Send button up event.
|
---|
| 539 | *
|
---|
| 540 | * @param pbutton Push button
|
---|
| 541 | */
|
---|
| 542 | void ui_pbutton_up(ui_pbutton_t *pbutton)
|
---|
| 543 | {
|
---|
| 544 | if (pbutton->cb != NULL && pbutton->cb->up != NULL)
|
---|
| 545 | pbutton->cb->up(pbutton, pbutton->arg);
|
---|
| 546 | }
|
---|
| 547 |
|
---|
[faca61b8] | 548 | /** Handle push button position event.
|
---|
| 549 | *
|
---|
| 550 | * @param pbutton Push button
|
---|
| 551 | * @param pos_event Position event
|
---|
[a2f173b] | 552 | * @return @c ui_claimed iff the event is claimed
|
---|
[faca61b8] | 553 | */
|
---|
[a2f173b] | 554 | ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
|
---|
[faca61b8] | 555 | {
|
---|
| 556 | gfx_coord2_t pos;
|
---|
[8ef48ece] | 557 | bool inside;
|
---|
[faca61b8] | 558 |
|
---|
| 559 | pos.x = event->hpos;
|
---|
| 560 | pos.y = event->vpos;
|
---|
| 561 |
|
---|
[8ef48ece] | 562 | inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
|
---|
[faca61b8] | 563 |
|
---|
[8ef48ece] | 564 | switch (event->type) {
|
---|
| 565 | case POS_PRESS:
|
---|
[a2f173b] | 566 | if (inside) {
|
---|
[8ef48ece] | 567 | ui_pbutton_press(pbutton);
|
---|
[a2f173b] | 568 | return ui_claimed;
|
---|
| 569 | }
|
---|
[8ef48ece] | 570 | break;
|
---|
| 571 | case POS_RELEASE:
|
---|
[a2f173b] | 572 | if (pbutton->held) {
|
---|
| 573 | ui_pbutton_release(pbutton);
|
---|
| 574 | return ui_claimed;
|
---|
| 575 | }
|
---|
[8ef48ece] | 576 | break;
|
---|
| 577 | case POS_UPDATE:
|
---|
| 578 | if (inside && !pbutton->inside) {
|
---|
| 579 | ui_pbutton_enter(pbutton);
|
---|
[a2f173b] | 580 | return ui_claimed;
|
---|
[8ef48ece] | 581 | } else if (!inside && pbutton->inside) {
|
---|
| 582 | ui_pbutton_leave(pbutton);
|
---|
| 583 | }
|
---|
| 584 | break;
|
---|
[8edec53] | 585 | case POS_DCLICK:
|
---|
| 586 | break;
|
---|
[faca61b8] | 587 | }
|
---|
[a2f173b] | 588 |
|
---|
| 589 | return ui_unclaimed;
|
---|
[faca61b8] | 590 | }
|
---|
| 591 |
|
---|
[c6f00b40] | 592 | /** Destroy push button control.
|
---|
| 593 | *
|
---|
| 594 | * @param arg Argument (ui_pbutton_t *)
|
---|
| 595 | */
|
---|
| 596 | void ui_pbutton_ctl_destroy(void *arg)
|
---|
| 597 | {
|
---|
| 598 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
---|
| 599 |
|
---|
| 600 | ui_pbutton_destroy(pbutton);
|
---|
| 601 | }
|
---|
| 602 |
|
---|
[4df6607] | 603 | /** Paint push button control.
|
---|
| 604 | *
|
---|
| 605 | * @param arg Argument (ui_pbutton_t *)
|
---|
| 606 | * @return EOK on success or an error code
|
---|
| 607 | */
|
---|
| 608 | errno_t ui_pbutton_ctl_paint(void *arg)
|
---|
| 609 | {
|
---|
| 610 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
---|
| 611 |
|
---|
| 612 | return ui_pbutton_paint(pbutton);
|
---|
| 613 | }
|
---|
| 614 |
|
---|
[8009dc27] | 615 | /** Handle push button control position event.
|
---|
| 616 | *
|
---|
| 617 | * @param arg Argument (ui_pbutton_t *)
|
---|
| 618 | * @param pos_event Position event
|
---|
| 619 | * @return @c ui_claimed iff the event is claimed
|
---|
| 620 | */
|
---|
| 621 | ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 622 | {
|
---|
| 623 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
---|
| 624 |
|
---|
| 625 | return ui_pbutton_pos_event(pbutton, event);
|
---|
| 626 | }
|
---|
| 627 |
|
---|
[f80690a] | 628 | /** @}
|
---|
| 629 | */
|
---|