[f80690a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2020 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>
|
---|
[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 |
|
---|
[8009dc27] | 56 | static ui_evclaim_t ui_pbutton_ctl_pos_event(void *, pos_event_t *);
|
---|
| 57 |
|
---|
| 58 | /** Push button control ops */
|
---|
| 59 | ui_control_ops_t ui_pbutton_ops = {
|
---|
| 60 | .pos_event = ui_pbutton_ctl_pos_event
|
---|
| 61 | };
|
---|
| 62 |
|
---|
[f80690a] | 63 | /** Create new push button.
|
---|
| 64 | *
|
---|
[47728678] | 65 | * @param resource UI resource
|
---|
[f80690a] | 66 | * @param caption Caption
|
---|
| 67 | * @param rpbutton Place to store pointer to new push button
|
---|
| 68 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 69 | */
|
---|
[47728678] | 70 | errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
|
---|
| 71 | ui_pbutton_t **rpbutton)
|
---|
[f80690a] | 72 | {
|
---|
| 73 | ui_pbutton_t *pbutton;
|
---|
[8009dc27] | 74 | errno_t rc;
|
---|
[f80690a] | 75 |
|
---|
| 76 | pbutton = calloc(1, sizeof(ui_pbutton_t));
|
---|
| 77 | if (pbutton == NULL)
|
---|
| 78 | return ENOMEM;
|
---|
| 79 |
|
---|
[8009dc27] | 80 | rc = ui_control_new(&ui_pbutton_ops, (void *) pbutton,
|
---|
| 81 | &pbutton->control);
|
---|
| 82 | if (rc != EOK) {
|
---|
| 83 | free(pbutton);
|
---|
| 84 | return rc;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[47728678] | 87 | pbutton->caption = str_dup(caption);
|
---|
| 88 | if (pbutton->caption == NULL) {
|
---|
[8009dc27] | 89 | ui_control_delete(pbutton->control);
|
---|
[47728678] | 90 | free(pbutton);
|
---|
| 91 | return ENOMEM;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | pbutton->res = resource;
|
---|
[f80690a] | 95 | *rpbutton = pbutton;
|
---|
| 96 | return EOK;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /** Destroy push button.
|
---|
| 100 | *
|
---|
| 101 | * @param pbutton Push button or @c NULL
|
---|
| 102 | */
|
---|
| 103 | void ui_pbutton_destroy(ui_pbutton_t *pbutton)
|
---|
| 104 | {
|
---|
| 105 | if (pbutton == NULL)
|
---|
| 106 | return;
|
---|
| 107 |
|
---|
[8009dc27] | 108 | ui_control_delete(pbutton->control);
|
---|
[f80690a] | 109 | free(pbutton);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[8009dc27] | 112 | /** Get base control from push button.
|
---|
| 113 | *
|
---|
| 114 | * @param pbutton Push button
|
---|
| 115 | * @return Control
|
---|
| 116 | */
|
---|
| 117 | ui_control_t *ui_pbutton_ctl(ui_pbutton_t *pbutton)
|
---|
| 118 | {
|
---|
| 119 | return pbutton->control;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[8ef48ece] | 122 | /** Set push button callbacks.
|
---|
| 123 | *
|
---|
| 124 | * @param pbutton Push button
|
---|
| 125 | * @param cb Push button callbacks
|
---|
| 126 | * @param arg Callback argument
|
---|
| 127 | */
|
---|
| 128 | void ui_pbutton_set_cb(ui_pbutton_t *pbutton, ui_pbutton_cb_t *cb, void *arg)
|
---|
| 129 | {
|
---|
| 130 | pbutton->cb = cb;
|
---|
| 131 | pbutton->arg = arg;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[47728678] | 134 | /** Set button rectangle.
|
---|
| 135 | *
|
---|
| 136 | * @param pbutton Button
|
---|
[ba09d06] | 137 | * @param rect New button rectangle
|
---|
[47728678] | 138 | */
|
---|
| 139 | void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
|
---|
| 140 | {
|
---|
| 141 | pbutton->rect = *rect;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[c9a7adc] | 144 | /** Set default flag.
|
---|
| 145 | *
|
---|
| 146 | * Default button is the one activated by Enter key, it is marked
|
---|
| 147 | * by thicker frame.
|
---|
| 148 | *
|
---|
| 149 | * @param pbutton Button
|
---|
| 150 | * @param isdefault @c true iff button is default
|
---|
| 151 | */
|
---|
| 152 | void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault)
|
---|
| 153 | {
|
---|
| 154 | pbutton->isdefault = isdefault;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /** Paint outer button frame.
|
---|
[47728678] | 158 | *
|
---|
| 159 | * @param pbutton Push button
|
---|
| 160 | * @return EOK on success or an error code
|
---|
| 161 | */
|
---|
[c9a7adc] | 162 | static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton)
|
---|
[47728678] | 163 | {
|
---|
[c9a7adc] | 164 | gfx_rect_t rect;
|
---|
| 165 | gfx_coord_t thickness;
|
---|
[47728678] | 166 | errno_t rc;
|
---|
| 167 |
|
---|
[c9a7adc] | 168 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
| 169 |
|
---|
[de9992c] | 170 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_frame_color);
|
---|
[c9a7adc] | 171 | if (rc != EOK)
|
---|
| 172 | goto error;
|
---|
| 173 |
|
---|
| 174 | rect.p0.x = pbutton->rect.p0.x + 1;
|
---|
| 175 | rect.p0.y = pbutton->rect.p0.y;
|
---|
| 176 | rect.p1.x = pbutton->rect.p1.x - 1;
|
---|
| 177 | rect.p1.y = pbutton->rect.p0.y + thickness;
|
---|
| 178 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
| 179 | if (rc != EOK)
|
---|
| 180 | goto error;
|
---|
| 181 |
|
---|
| 182 | rect.p0.x = pbutton->rect.p0.x + 1;
|
---|
| 183 | rect.p0.y = pbutton->rect.p1.y - thickness;
|
---|
| 184 | rect.p1.x = pbutton->rect.p1.x - 1;
|
---|
| 185 | rect.p1.y = pbutton->rect.p1.y;
|
---|
| 186 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
| 187 | if (rc != EOK)
|
---|
| 188 | goto error;
|
---|
| 189 |
|
---|
| 190 | rect.p0.x = pbutton->rect.p0.x;
|
---|
| 191 | rect.p0.y = pbutton->rect.p0.y + 1;
|
---|
| 192 | rect.p1.x = pbutton->rect.p0.x + thickness;
|
---|
| 193 | rect.p1.y = pbutton->rect.p1.y - 1;
|
---|
| 194 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
| 195 | if (rc != EOK)
|
---|
| 196 | goto error;
|
---|
| 197 |
|
---|
| 198 | rect.p0.x = pbutton->rect.p1.x - thickness;
|
---|
| 199 | rect.p0.y = pbutton->rect.p0.y + 1;
|
---|
| 200 | rect.p1.x = pbutton->rect.p1.x;
|
---|
| 201 | rect.p1.y = pbutton->rect.p1.y - 1;
|
---|
| 202 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
| 203 | if (rc != EOK)
|
---|
| 204 | goto error;
|
---|
| 205 |
|
---|
| 206 | return EOK;
|
---|
| 207 | error:
|
---|
| 208 | return rc;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /** Paint outset button bevel.
|
---|
| 212 | *
|
---|
| 213 | * @param pbutton Push button
|
---|
| 214 | * @return EOK on success or an error code
|
---|
| 215 | */
|
---|
| 216 | static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton,
|
---|
| 217 | gfx_rect_t *rect)
|
---|
| 218 | {
|
---|
[de9992c] | 219 | return ui_paint_bevel(pbutton->res->gc, rect,
|
---|
| 220 | pbutton->res->btn_highlight_color,
|
---|
| 221 | pbutton->res->btn_shadow_color, 2, NULL);
|
---|
[c9a7adc] | 222 | }
|
---|
| 223 |
|
---|
| 224 | /** Paint inset button bevel.
|
---|
| 225 | *
|
---|
| 226 | * @param pbutton Push button
|
---|
| 227 | * @return EOK on success or an error code
|
---|
| 228 | */
|
---|
| 229 | static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton,
|
---|
| 230 | gfx_rect_t *rect)
|
---|
| 231 | {
|
---|
[de9992c] | 232 | return ui_paint_bevel(pbutton->res->gc, rect,
|
---|
| 233 | pbutton->res->btn_shadow_color,
|
---|
| 234 | pbutton->res->btn_face_color, 2, NULL);
|
---|
[c9a7adc] | 235 | }
|
---|
| 236 |
|
---|
| 237 | /** Paint push button.
|
---|
| 238 | *
|
---|
| 239 | * @param pbutton Push button
|
---|
| 240 | * @return EOK on success or an error code
|
---|
| 241 | */
|
---|
| 242 | errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
|
---|
| 243 | {
|
---|
| 244 | gfx_coord2_t pos;
|
---|
| 245 | gfx_text_fmt_t fmt;
|
---|
| 246 | gfx_rect_t rect;
|
---|
| 247 | gfx_coord_t thickness;
|
---|
[8ef48ece] | 248 | bool depressed;
|
---|
[c9a7adc] | 249 | errno_t rc;
|
---|
| 250 |
|
---|
| 251 | thickness = pbutton->isdefault ? 2 : 1;
|
---|
[8ef48ece] | 252 | depressed = pbutton->held && pbutton->inside;
|
---|
[c9a7adc] | 253 |
|
---|
| 254 | rect.p0.x = pbutton->rect.p0.x + thickness;
|
---|
| 255 | rect.p0.y = pbutton->rect.p0.y + thickness;
|
---|
| 256 | rect.p1.x = pbutton->rect.p1.x - thickness;
|
---|
| 257 | rect.p1.y = pbutton->rect.p1.y - thickness;
|
---|
| 258 |
|
---|
[de9992c] | 259 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
|
---|
[c9a7adc] | 260 | if (rc != EOK)
|
---|
| 261 | goto error;
|
---|
| 262 |
|
---|
| 263 | rc = gfx_fill_rect(pbutton->res->gc, &rect);
|
---|
| 264 | if (rc != EOK)
|
---|
| 265 | goto error;
|
---|
| 266 |
|
---|
[de9992c] | 267 | rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_text_color);
|
---|
[47728678] | 268 | if (rc != EOK)
|
---|
| 269 | goto error;
|
---|
| 270 |
|
---|
| 271 | /* Center of button rectangle */
|
---|
[c9a7adc] | 272 | pos.x = (rect.p0.x + rect.p1.x) / 2;
|
---|
| 273 | pos.y = (rect.p0.y + rect.p1.y) / 2;
|
---|
[47728678] | 274 |
|
---|
[8ef48ece] | 275 | if (depressed) {
|
---|
[f6df5a3] | 276 | pos.x += ui_pb_press_dx;
|
---|
| 277 | pos.y += ui_pb_press_dy;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[47728678] | 280 | gfx_text_fmt_init(&fmt);
|
---|
| 281 | fmt.halign = gfx_halign_center;
|
---|
| 282 | fmt.valign = gfx_valign_center;
|
---|
| 283 |
|
---|
| 284 | rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
|
---|
| 285 | if (rc != EOK)
|
---|
| 286 | goto error;
|
---|
| 287 |
|
---|
[c9a7adc] | 288 | rc = ui_pbutton_paint_frame(pbutton);
|
---|
| 289 | if (rc != EOK)
|
---|
| 290 | goto error;
|
---|
| 291 |
|
---|
[8ef48ece] | 292 | if (depressed) {
|
---|
[c9a7adc] | 293 | rc = ui_pbutton_paint_inset(pbutton, &rect);
|
---|
| 294 | if (rc != EOK)
|
---|
| 295 | goto error;
|
---|
| 296 | } else {
|
---|
| 297 | rc = ui_pbutton_paint_outset(pbutton, &rect);
|
---|
| 298 | if (rc != EOK)
|
---|
| 299 | goto error;
|
---|
| 300 | }
|
---|
[47728678] | 301 |
|
---|
| 302 | return EOK;
|
---|
| 303 | error:
|
---|
| 304 | return rc;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[f6df5a3] | 307 | /** Press down button.
|
---|
| 308 | *
|
---|
| 309 | * @param pbutton Push button
|
---|
| 310 | */
|
---|
| 311 | void ui_pbutton_press(ui_pbutton_t *pbutton)
|
---|
| 312 | {
|
---|
[8ef48ece] | 313 | if (pbutton->held)
|
---|
| 314 | return;
|
---|
| 315 |
|
---|
| 316 | pbutton->inside = true;
|
---|
[f6df5a3] | 317 | pbutton->held = true;
|
---|
[8ef48ece] | 318 | (void) ui_pbutton_paint(pbutton);
|
---|
[f6df5a3] | 319 | }
|
---|
| 320 |
|
---|
| 321 | /** Release button.
|
---|
| 322 | *
|
---|
| 323 | * @param pbutton Push button
|
---|
| 324 | */
|
---|
| 325 | void ui_pbutton_release(ui_pbutton_t *pbutton)
|
---|
| 326 | {
|
---|
[8ef48ece] | 327 | if (!pbutton->held)
|
---|
| 328 | return;
|
---|
| 329 |
|
---|
[f6df5a3] | 330 | pbutton->held = false;
|
---|
[8ef48ece] | 331 |
|
---|
| 332 | if (pbutton->inside) {
|
---|
| 333 | (void) ui_pbutton_paint(pbutton);
|
---|
| 334 | ui_pbutton_clicked(pbutton);
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | /** Pointer entered button.
|
---|
| 339 | *
|
---|
| 340 | * @param pbutton Push button
|
---|
| 341 | */
|
---|
| 342 | void ui_pbutton_enter(ui_pbutton_t *pbutton)
|
---|
| 343 | {
|
---|
| 344 | if (pbutton->inside)
|
---|
| 345 | return;
|
---|
| 346 |
|
---|
| 347 | pbutton->inside = true;
|
---|
| 348 | if (pbutton->held)
|
---|
| 349 | (void) ui_pbutton_paint(pbutton);
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /** Pointer left button.
|
---|
| 353 | *
|
---|
| 354 | * @param pbutton Push button
|
---|
| 355 | */
|
---|
| 356 | void ui_pbutton_leave(ui_pbutton_t *pbutton)
|
---|
| 357 | {
|
---|
| 358 | if (!pbutton->inside)
|
---|
| 359 | return;
|
---|
| 360 |
|
---|
| 361 | pbutton->inside = false;
|
---|
| 362 | if (pbutton->held)
|
---|
| 363 | (void) ui_pbutton_paint(pbutton);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /** Button was clicked.
|
---|
| 367 | *
|
---|
| 368 | * @param pbutton Push button
|
---|
| 369 | */
|
---|
| 370 | void ui_pbutton_clicked(ui_pbutton_t *pbutton)
|
---|
| 371 | {
|
---|
| 372 | if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
|
---|
| 373 | pbutton->cb->clicked(pbutton, pbutton->arg);
|
---|
[f6df5a3] | 374 | }
|
---|
| 375 |
|
---|
[faca61b8] | 376 | /** Handle push button position event.
|
---|
| 377 | *
|
---|
| 378 | * @param pbutton Push button
|
---|
| 379 | * @param pos_event Position event
|
---|
[a2f173b] | 380 | * @return @c ui_claimed iff the event is claimed
|
---|
[faca61b8] | 381 | */
|
---|
[a2f173b] | 382 | ui_evclaim_t ui_pbutton_pos_event(ui_pbutton_t *pbutton, pos_event_t *event)
|
---|
[faca61b8] | 383 | {
|
---|
| 384 | gfx_coord2_t pos;
|
---|
[8ef48ece] | 385 | bool inside;
|
---|
[faca61b8] | 386 |
|
---|
| 387 | pos.x = event->hpos;
|
---|
| 388 | pos.y = event->vpos;
|
---|
| 389 |
|
---|
[8ef48ece] | 390 | inside = gfx_pix_inside_rect(&pos, &pbutton->rect);
|
---|
[faca61b8] | 391 |
|
---|
[8ef48ece] | 392 | switch (event->type) {
|
---|
| 393 | case POS_PRESS:
|
---|
[a2f173b] | 394 | if (inside) {
|
---|
[8ef48ece] | 395 | ui_pbutton_press(pbutton);
|
---|
[a2f173b] | 396 | return ui_claimed;
|
---|
| 397 | }
|
---|
[8ef48ece] | 398 | break;
|
---|
| 399 | case POS_RELEASE:
|
---|
[a2f173b] | 400 | if (pbutton->held) {
|
---|
| 401 | ui_pbutton_release(pbutton);
|
---|
| 402 | return ui_claimed;
|
---|
| 403 | }
|
---|
[8ef48ece] | 404 | break;
|
---|
| 405 | case POS_UPDATE:
|
---|
| 406 | if (inside && !pbutton->inside) {
|
---|
| 407 | ui_pbutton_enter(pbutton);
|
---|
[a2f173b] | 408 | return ui_claimed;
|
---|
[8ef48ece] | 409 | } else if (!inside && pbutton->inside) {
|
---|
| 410 | ui_pbutton_leave(pbutton);
|
---|
| 411 | }
|
---|
| 412 | break;
|
---|
[faca61b8] | 413 | }
|
---|
[a2f173b] | 414 |
|
---|
| 415 | return ui_unclaimed;
|
---|
[faca61b8] | 416 | }
|
---|
| 417 |
|
---|
[8009dc27] | 418 | /** Handle push button control position event.
|
---|
| 419 | *
|
---|
| 420 | * @param arg Argument (ui_pbutton_t *)
|
---|
| 421 | * @param pos_event Position event
|
---|
| 422 | * @return @c ui_claimed iff the event is claimed
|
---|
| 423 | */
|
---|
| 424 | ui_evclaim_t ui_pbutton_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 425 | {
|
---|
| 426 | ui_pbutton_t *pbutton = (ui_pbutton_t *) arg;
|
---|
| 427 |
|
---|
| 428 | return ui_pbutton_pos_event(pbutton, event);
|
---|
| 429 | }
|
---|
| 430 |
|
---|
[f80690a] | 431 | /** @}
|
---|
| 432 | */
|
---|