[d70dc1c4] | 1 | /*
|
---|
[2ab8ab3] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[d70dc1c4] | 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 Check box
|
---|
| 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/checkbox.h>
|
---|
| 47 | #include "../private/checkbox.h"
|
---|
| 48 | #include "../private/resource.h"
|
---|
| 49 |
|
---|
| 50 | enum {
|
---|
| 51 | checkbox_box_w = 16,
|
---|
| 52 | checkbox_box_h = 16,
|
---|
| 53 | checkbox_label_margin = 8,
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | static void ui_checkbox_ctl_destroy(void *);
|
---|
| 57 | static errno_t ui_checkbox_ctl_paint(void *);
|
---|
| 58 | static ui_evclaim_t ui_checkbox_ctl_pos_event(void *, pos_event_t *);
|
---|
| 59 |
|
---|
| 60 | /** Check box control ops */
|
---|
| 61 | ui_control_ops_t ui_checkbox_ops = {
|
---|
| 62 | .destroy = ui_checkbox_ctl_destroy,
|
---|
| 63 | .paint = ui_checkbox_ctl_paint,
|
---|
| 64 | .pos_event = ui_checkbox_ctl_pos_event
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | /** Create new check box.
|
---|
| 68 | *
|
---|
| 69 | * @param resource UI resource
|
---|
| 70 | * @param caption Caption
|
---|
| 71 | * @param rcheckbox Place to store pointer to new check box
|
---|
| 72 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 73 | */
|
---|
| 74 | errno_t ui_checkbox_create(ui_resource_t *resource, const char *caption,
|
---|
| 75 | ui_checkbox_t **rcheckbox)
|
---|
| 76 | {
|
---|
| 77 | ui_checkbox_t *checkbox;
|
---|
| 78 | errno_t rc;
|
---|
| 79 |
|
---|
| 80 | checkbox = calloc(1, sizeof(ui_checkbox_t));
|
---|
| 81 | if (checkbox == NULL)
|
---|
| 82 | return ENOMEM;
|
---|
| 83 |
|
---|
| 84 | rc = ui_control_new(&ui_checkbox_ops, (void *) checkbox,
|
---|
| 85 | &checkbox->control);
|
---|
| 86 | if (rc != EOK) {
|
---|
| 87 | free(checkbox);
|
---|
| 88 | return rc;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | checkbox->caption = str_dup(caption);
|
---|
| 92 | if (checkbox->caption == NULL) {
|
---|
| 93 | ui_control_delete(checkbox->control);
|
---|
| 94 | free(checkbox);
|
---|
| 95 | return ENOMEM;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | checkbox->res = resource;
|
---|
| 99 | *rcheckbox = checkbox;
|
---|
| 100 | return EOK;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /** Destroy check box.
|
---|
| 104 | *
|
---|
| 105 | * @param checkbox Check box or @c NULL
|
---|
| 106 | */
|
---|
| 107 | void ui_checkbox_destroy(ui_checkbox_t *checkbox)
|
---|
| 108 | {
|
---|
| 109 | if (checkbox == NULL)
|
---|
| 110 | return;
|
---|
| 111 |
|
---|
| 112 | ui_control_delete(checkbox->control);
|
---|
| 113 | free(checkbox);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /** Get base control from check box.
|
---|
| 117 | *
|
---|
| 118 | * @param checkbox Check box
|
---|
| 119 | * @return Control
|
---|
| 120 | */
|
---|
| 121 | ui_control_t *ui_checkbox_ctl(ui_checkbox_t *checkbox)
|
---|
| 122 | {
|
---|
| 123 | return checkbox->control;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** Set check box callbacks.
|
---|
| 127 | *
|
---|
| 128 | * @param checkbox Check box
|
---|
| 129 | * @param cb Check box callbacks
|
---|
| 130 | * @param arg Callback argument
|
---|
| 131 | */
|
---|
| 132 | void ui_checkbox_set_cb(ui_checkbox_t *checkbox, ui_checkbox_cb_t *cb, void *arg)
|
---|
| 133 | {
|
---|
| 134 | checkbox->cb = cb;
|
---|
| 135 | checkbox->arg = arg;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** Set button rectangle.
|
---|
| 139 | *
|
---|
| 140 | * @param checkbox Button
|
---|
| 141 | * @param rect New button rectangle
|
---|
| 142 | */
|
---|
| 143 | void ui_checkbox_set_rect(ui_checkbox_t *checkbox, gfx_rect_t *rect)
|
---|
| 144 | {
|
---|
| 145 | checkbox->rect = *rect;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[307d4d2] | 148 | /** Paint check box in graphics mode.
|
---|
[d70dc1c4] | 149 | *
|
---|
| 150 | * @param checkbox Check box
|
---|
| 151 | * @return EOK on success or an error code
|
---|
| 152 | */
|
---|
[307d4d2] | 153 | errno_t ui_checkbox_paint_gfx(ui_checkbox_t *checkbox)
|
---|
[d70dc1c4] | 154 | {
|
---|
| 155 | gfx_coord2_t pos;
|
---|
| 156 | gfx_text_fmt_t fmt;
|
---|
| 157 | gfx_rect_t box_rect;
|
---|
| 158 | gfx_rect_t box_inside;
|
---|
| 159 | gfx_coord2_t box_center;
|
---|
| 160 | bool depressed;
|
---|
| 161 | errno_t rc;
|
---|
| 162 |
|
---|
| 163 | box_rect.p0 = checkbox->rect.p0;
|
---|
| 164 | box_rect.p1.x = box_rect.p0.x + checkbox_box_w;
|
---|
| 165 | box_rect.p1.y = box_rect.p0.y + checkbox_box_h;
|
---|
| 166 |
|
---|
| 167 | /* Paint checkbox frame */
|
---|
| 168 |
|
---|
| 169 | rc = ui_paint_inset_frame(checkbox->res, &box_rect, &box_inside);
|
---|
| 170 | if (rc != EOK)
|
---|
| 171 | goto error;
|
---|
| 172 |
|
---|
| 173 | /* Paint checkbox interior */
|
---|
| 174 |
|
---|
| 175 | depressed = checkbox->held && checkbox->inside;
|
---|
| 176 |
|
---|
| 177 | rc = gfx_set_color(checkbox->res->gc, depressed ?
|
---|
| 178 | checkbox->res->entry_act_bg_color :
|
---|
| 179 | checkbox->res->entry_bg_color);
|
---|
| 180 | if (rc != EOK)
|
---|
| 181 | goto error;
|
---|
| 182 |
|
---|
| 183 | rc = gfx_fill_rect(checkbox->res->gc, &box_inside);
|
---|
| 184 | if (rc != EOK)
|
---|
| 185 | goto error;
|
---|
| 186 |
|
---|
| 187 | /* Paint cross mark */
|
---|
| 188 |
|
---|
| 189 | if (checkbox->checked) {
|
---|
| 190 | box_center.x = (box_inside.p0.x + box_inside.p1.x) / 2;
|
---|
| 191 | box_center.y = (box_inside.p0.y + box_inside.p1.y) / 2;
|
---|
| 192 |
|
---|
| 193 | gfx_text_fmt_init(&fmt);
|
---|
[4583015] | 194 | fmt.font = checkbox->res->font;
|
---|
[b433f68] | 195 | fmt.color = checkbox->res->entry_fg_color;
|
---|
[d70dc1c4] | 196 | fmt.halign = gfx_halign_center;
|
---|
| 197 | fmt.valign = gfx_valign_center;
|
---|
| 198 |
|
---|
[4583015] | 199 | rc = gfx_puttext(&box_center, &fmt, "X");
|
---|
[d70dc1c4] | 200 | if (rc != EOK)
|
---|
| 201 | goto error;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /* Paint checkbox label */
|
---|
| 205 |
|
---|
| 206 | pos.x = box_rect.p1.x + checkbox_label_margin;
|
---|
| 207 | pos.y = (box_rect.p0.y + box_rect.p1.y) / 2;
|
---|
| 208 |
|
---|
| 209 | gfx_text_fmt_init(&fmt);
|
---|
[4583015] | 210 | fmt.font = checkbox->res->font;
|
---|
[b433f68] | 211 | fmt.color = checkbox->res->wnd_text_color;
|
---|
[d70dc1c4] | 212 | fmt.halign = gfx_halign_left;
|
---|
| 213 | fmt.valign = gfx_valign_center;
|
---|
| 214 |
|
---|
[4583015] | 215 | rc = gfx_puttext(&pos, &fmt, checkbox->caption);
|
---|
[d70dc1c4] | 216 | if (rc != EOK)
|
---|
| 217 | goto error;
|
---|
| 218 |
|
---|
[2ab8ab3] | 219 | rc = gfx_update(checkbox->res->gc);
|
---|
| 220 | if (rc != EOK)
|
---|
| 221 | goto error;
|
---|
| 222 |
|
---|
[d70dc1c4] | 223 | return EOK;
|
---|
| 224 | error:
|
---|
| 225 | return rc;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[307d4d2] | 228 | /** Paint check box in text mode.
|
---|
| 229 | *
|
---|
| 230 | * @param checkbox Check box
|
---|
| 231 | * @return EOK on success or an error code
|
---|
| 232 | */
|
---|
| 233 | errno_t ui_checkbox_paint_text(ui_checkbox_t *checkbox)
|
---|
| 234 | {
|
---|
| 235 | gfx_coord2_t pos;
|
---|
| 236 | gfx_text_fmt_t fmt;
|
---|
| 237 | bool depressed;
|
---|
| 238 | errno_t rc;
|
---|
| 239 |
|
---|
| 240 | /* Paint checkbox */
|
---|
| 241 |
|
---|
| 242 | depressed = checkbox->held && checkbox->inside;
|
---|
| 243 |
|
---|
| 244 | pos.x = checkbox->rect.p0.x;
|
---|
| 245 | pos.y = checkbox->rect.p0.y;
|
---|
| 246 |
|
---|
| 247 | gfx_text_fmt_init(&fmt);
|
---|
[4583015] | 248 | fmt.font = checkbox->res->font;
|
---|
[307d4d2] | 249 | fmt.color = depressed ? checkbox->res->entry_act_bg_color :
|
---|
| 250 | checkbox->res->wnd_text_color;
|
---|
| 251 | fmt.halign = gfx_halign_left;
|
---|
| 252 | fmt.valign = gfx_valign_top;
|
---|
| 253 |
|
---|
[4583015] | 254 | rc = gfx_puttext(&pos, &fmt, checkbox->checked ? "[X]" : "[ ]");
|
---|
[307d4d2] | 255 | if (rc != EOK)
|
---|
| 256 | goto error;
|
---|
| 257 |
|
---|
| 258 | /* Paint checkbox label */
|
---|
| 259 |
|
---|
| 260 | pos.x += 4;
|
---|
| 261 | fmt.color = checkbox->res->wnd_text_color;
|
---|
| 262 |
|
---|
[4583015] | 263 | rc = gfx_puttext(&pos, &fmt, checkbox->caption);
|
---|
[307d4d2] | 264 | if (rc != EOK)
|
---|
| 265 | goto error;
|
---|
| 266 |
|
---|
| 267 | rc = gfx_update(checkbox->res->gc);
|
---|
| 268 | if (rc != EOK)
|
---|
| 269 | goto error;
|
---|
| 270 |
|
---|
| 271 | return EOK;
|
---|
| 272 | error:
|
---|
| 273 | return rc;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /** Paint check box.
|
---|
| 277 | *
|
---|
| 278 | * @param checkbox Check box
|
---|
| 279 | * @return EOK on success or an error code
|
---|
| 280 | */
|
---|
| 281 | errno_t ui_checkbox_paint(ui_checkbox_t *checkbox)
|
---|
| 282 | {
|
---|
| 283 | if (checkbox->res->textmode)
|
---|
| 284 | return ui_checkbox_paint_text(checkbox);
|
---|
| 285 | else
|
---|
| 286 | return ui_checkbox_paint_gfx(checkbox);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[d70dc1c4] | 289 | /** Press down button.
|
---|
| 290 | *
|
---|
| 291 | * @param checkbox Check box
|
---|
| 292 | */
|
---|
| 293 | void ui_checkbox_press(ui_checkbox_t *checkbox)
|
---|
| 294 | {
|
---|
| 295 | if (checkbox->held)
|
---|
| 296 | return;
|
---|
| 297 |
|
---|
| 298 | checkbox->inside = true;
|
---|
| 299 | checkbox->held = true;
|
---|
| 300 | (void) ui_checkbox_paint(checkbox);
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | /** Release button.
|
---|
| 304 | *
|
---|
| 305 | * @param checkbox Check box
|
---|
| 306 | */
|
---|
| 307 | void ui_checkbox_release(ui_checkbox_t *checkbox)
|
---|
| 308 | {
|
---|
| 309 | if (!checkbox->held)
|
---|
| 310 | return;
|
---|
| 311 |
|
---|
| 312 | checkbox->held = false;
|
---|
| 313 |
|
---|
| 314 | if (checkbox->inside) {
|
---|
| 315 | /* Toggle check box state */
|
---|
| 316 | checkbox->checked = !checkbox->checked;
|
---|
| 317 |
|
---|
| 318 | /* Repaint and notify */
|
---|
| 319 | (void) ui_checkbox_paint(checkbox);
|
---|
| 320 | ui_checkbox_switched(checkbox);
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | /** Pointer entered button.
|
---|
| 325 | *
|
---|
| 326 | * @param checkbox Check box
|
---|
| 327 | */
|
---|
| 328 | void ui_checkbox_enter(ui_checkbox_t *checkbox)
|
---|
| 329 | {
|
---|
| 330 | if (checkbox->inside)
|
---|
| 331 | return;
|
---|
| 332 |
|
---|
| 333 | checkbox->inside = true;
|
---|
| 334 | if (checkbox->held)
|
---|
| 335 | (void) ui_checkbox_paint(checkbox);
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | /** Pointer left button.
|
---|
| 339 | *
|
---|
| 340 | * @param checkbox Check box
|
---|
| 341 | */
|
---|
| 342 | void ui_checkbox_leave(ui_checkbox_t *checkbox)
|
---|
| 343 | {
|
---|
| 344 | if (!checkbox->inside)
|
---|
| 345 | return;
|
---|
| 346 |
|
---|
| 347 | checkbox->inside = false;
|
---|
| 348 | if (checkbox->held)
|
---|
| 349 | (void) ui_checkbox_paint(checkbox);
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /** Button was switched.
|
---|
| 353 | *
|
---|
| 354 | * @param checkbox Check box
|
---|
| 355 | */
|
---|
| 356 | void ui_checkbox_switched(ui_checkbox_t *checkbox)
|
---|
| 357 | {
|
---|
| 358 | if (checkbox->cb != NULL && checkbox->cb->switched != NULL) {
|
---|
| 359 | checkbox->cb->switched(checkbox, checkbox->arg,
|
---|
| 360 | checkbox->checked);
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /** Handle check box position event.
|
---|
| 365 | *
|
---|
| 366 | * @param checkbox Check box
|
---|
| 367 | * @param pos_event Position event
|
---|
| 368 | * @return @c ui_claimed iff the event is claimed
|
---|
| 369 | */
|
---|
| 370 | ui_evclaim_t ui_checkbox_pos_event(ui_checkbox_t *checkbox, pos_event_t *event)
|
---|
| 371 | {
|
---|
| 372 | gfx_coord2_t pos;
|
---|
| 373 | bool inside;
|
---|
| 374 |
|
---|
| 375 | pos.x = event->hpos;
|
---|
| 376 | pos.y = event->vpos;
|
---|
| 377 |
|
---|
| 378 | inside = gfx_pix_inside_rect(&pos, &checkbox->rect);
|
---|
| 379 |
|
---|
| 380 | switch (event->type) {
|
---|
| 381 | case POS_PRESS:
|
---|
| 382 | if (inside) {
|
---|
| 383 | ui_checkbox_press(checkbox);
|
---|
| 384 | return ui_claimed;
|
---|
| 385 | }
|
---|
| 386 | break;
|
---|
| 387 | case POS_RELEASE:
|
---|
| 388 | if (checkbox->held) {
|
---|
| 389 | ui_checkbox_release(checkbox);
|
---|
| 390 | return ui_claimed;
|
---|
| 391 | }
|
---|
| 392 | break;
|
---|
| 393 | case POS_UPDATE:
|
---|
| 394 | if (inside && !checkbox->inside) {
|
---|
| 395 | ui_checkbox_enter(checkbox);
|
---|
| 396 | return ui_claimed;
|
---|
| 397 | } else if (!inside && checkbox->inside) {
|
---|
| 398 | ui_checkbox_leave(checkbox);
|
---|
| 399 | }
|
---|
| 400 | break;
|
---|
[8edec53] | 401 | case POS_DCLICK:
|
---|
| 402 | break;
|
---|
[d70dc1c4] | 403 | }
|
---|
| 404 |
|
---|
| 405 | return ui_unclaimed;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | /** Destroy check box control.
|
---|
| 409 | *
|
---|
| 410 | * @param arg Argument (ui_checkbox_t *)
|
---|
| 411 | */
|
---|
| 412 | void ui_checkbox_ctl_destroy(void *arg)
|
---|
| 413 | {
|
---|
| 414 | ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
|
---|
| 415 |
|
---|
| 416 | ui_checkbox_destroy(checkbox);
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | /** Paint check box control.
|
---|
| 420 | *
|
---|
| 421 | * @param arg Argument (ui_checkbox_t *)
|
---|
| 422 | * @return EOK on success or an error code
|
---|
| 423 | */
|
---|
| 424 | errno_t ui_checkbox_ctl_paint(void *arg)
|
---|
| 425 | {
|
---|
| 426 | ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
|
---|
| 427 |
|
---|
| 428 | return ui_checkbox_paint(checkbox);
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | /** Handle check box control position event.
|
---|
| 432 | *
|
---|
| 433 | * @param arg Argument (ui_checkbox_t *)
|
---|
| 434 | * @param pos_event Position event
|
---|
| 435 | * @return @c ui_claimed iff the event is claimed
|
---|
| 436 | */
|
---|
| 437 | ui_evclaim_t ui_checkbox_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 438 | {
|
---|
| 439 | ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
|
---|
| 440 |
|
---|
| 441 | return ui_checkbox_pos_event(checkbox, event);
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | /** @}
|
---|
| 445 | */
|
---|