[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 |
|
---|
| 148 | /** Paint check box.
|
---|
| 149 | *
|
---|
| 150 | * @param checkbox Check box
|
---|
| 151 | * @return EOK on success or an error code
|
---|
| 152 | */
|
---|
| 153 | errno_t ui_checkbox_paint(ui_checkbox_t *checkbox)
|
---|
| 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);
|
---|
[b433f68] | 194 | fmt.color = checkbox->res->entry_fg_color;
|
---|
[d70dc1c4] | 195 | fmt.halign = gfx_halign_center;
|
---|
| 196 | fmt.valign = gfx_valign_center;
|
---|
| 197 |
|
---|
| 198 | rc = gfx_puttext(checkbox->res->font, &box_center, &fmt, "X");
|
---|
| 199 | if (rc != EOK)
|
---|
| 200 | goto error;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /* Paint checkbox label */
|
---|
| 204 |
|
---|
| 205 | pos.x = box_rect.p1.x + checkbox_label_margin;
|
---|
| 206 | pos.y = (box_rect.p0.y + box_rect.p1.y) / 2;
|
---|
| 207 |
|
---|
| 208 | gfx_text_fmt_init(&fmt);
|
---|
[b433f68] | 209 | fmt.color = checkbox->res->wnd_text_color;
|
---|
[d70dc1c4] | 210 | fmt.halign = gfx_halign_left;
|
---|
| 211 | fmt.valign = gfx_valign_center;
|
---|
| 212 |
|
---|
| 213 | rc = gfx_puttext(checkbox->res->font, &pos, &fmt, checkbox->caption);
|
---|
| 214 | if (rc != EOK)
|
---|
| 215 | goto error;
|
---|
| 216 |
|
---|
[2ab8ab3] | 217 | rc = gfx_update(checkbox->res->gc);
|
---|
| 218 | if (rc != EOK)
|
---|
| 219 | goto error;
|
---|
| 220 |
|
---|
[d70dc1c4] | 221 | return EOK;
|
---|
| 222 | error:
|
---|
| 223 | return rc;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | /** Press down button.
|
---|
| 227 | *
|
---|
| 228 | * @param checkbox Check box
|
---|
| 229 | */
|
---|
| 230 | void ui_checkbox_press(ui_checkbox_t *checkbox)
|
---|
| 231 | {
|
---|
| 232 | if (checkbox->held)
|
---|
| 233 | return;
|
---|
| 234 |
|
---|
| 235 | checkbox->inside = true;
|
---|
| 236 | checkbox->held = true;
|
---|
| 237 | (void) ui_checkbox_paint(checkbox);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /** Release button.
|
---|
| 241 | *
|
---|
| 242 | * @param checkbox Check box
|
---|
| 243 | */
|
---|
| 244 | void ui_checkbox_release(ui_checkbox_t *checkbox)
|
---|
| 245 | {
|
---|
| 246 | if (!checkbox->held)
|
---|
| 247 | return;
|
---|
| 248 |
|
---|
| 249 | checkbox->held = false;
|
---|
| 250 |
|
---|
| 251 | if (checkbox->inside) {
|
---|
| 252 | /* Toggle check box state */
|
---|
| 253 | checkbox->checked = !checkbox->checked;
|
---|
| 254 |
|
---|
| 255 | /* Repaint and notify */
|
---|
| 256 | (void) ui_checkbox_paint(checkbox);
|
---|
| 257 | ui_checkbox_switched(checkbox);
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /** Pointer entered button.
|
---|
| 262 | *
|
---|
| 263 | * @param checkbox Check box
|
---|
| 264 | */
|
---|
| 265 | void ui_checkbox_enter(ui_checkbox_t *checkbox)
|
---|
| 266 | {
|
---|
| 267 | if (checkbox->inside)
|
---|
| 268 | return;
|
---|
| 269 |
|
---|
| 270 | checkbox->inside = true;
|
---|
| 271 | if (checkbox->held)
|
---|
| 272 | (void) ui_checkbox_paint(checkbox);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /** Pointer left button.
|
---|
| 276 | *
|
---|
| 277 | * @param checkbox Check box
|
---|
| 278 | */
|
---|
| 279 | void ui_checkbox_leave(ui_checkbox_t *checkbox)
|
---|
| 280 | {
|
---|
| 281 | if (!checkbox->inside)
|
---|
| 282 | return;
|
---|
| 283 |
|
---|
| 284 | checkbox->inside = false;
|
---|
| 285 | if (checkbox->held)
|
---|
| 286 | (void) ui_checkbox_paint(checkbox);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /** Button was switched.
|
---|
| 290 | *
|
---|
| 291 | * @param checkbox Check box
|
---|
| 292 | */
|
---|
| 293 | void ui_checkbox_switched(ui_checkbox_t *checkbox)
|
---|
| 294 | {
|
---|
| 295 | if (checkbox->cb != NULL && checkbox->cb->switched != NULL) {
|
---|
| 296 | checkbox->cb->switched(checkbox, checkbox->arg,
|
---|
| 297 | checkbox->checked);
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /** Handle check box position event.
|
---|
| 302 | *
|
---|
| 303 | * @param checkbox Check box
|
---|
| 304 | * @param pos_event Position event
|
---|
| 305 | * @return @c ui_claimed iff the event is claimed
|
---|
| 306 | */
|
---|
| 307 | ui_evclaim_t ui_checkbox_pos_event(ui_checkbox_t *checkbox, pos_event_t *event)
|
---|
| 308 | {
|
---|
| 309 | gfx_coord2_t pos;
|
---|
| 310 | bool inside;
|
---|
| 311 |
|
---|
| 312 | pos.x = event->hpos;
|
---|
| 313 | pos.y = event->vpos;
|
---|
| 314 |
|
---|
| 315 | inside = gfx_pix_inside_rect(&pos, &checkbox->rect);
|
---|
| 316 |
|
---|
| 317 | switch (event->type) {
|
---|
| 318 | case POS_PRESS:
|
---|
| 319 | if (inside) {
|
---|
| 320 | ui_checkbox_press(checkbox);
|
---|
| 321 | return ui_claimed;
|
---|
| 322 | }
|
---|
| 323 | break;
|
---|
| 324 | case POS_RELEASE:
|
---|
| 325 | if (checkbox->held) {
|
---|
| 326 | ui_checkbox_release(checkbox);
|
---|
| 327 | return ui_claimed;
|
---|
| 328 | }
|
---|
| 329 | break;
|
---|
| 330 | case POS_UPDATE:
|
---|
| 331 | if (inside && !checkbox->inside) {
|
---|
| 332 | ui_checkbox_enter(checkbox);
|
---|
| 333 | return ui_claimed;
|
---|
| 334 | } else if (!inside && checkbox->inside) {
|
---|
| 335 | ui_checkbox_leave(checkbox);
|
---|
| 336 | }
|
---|
| 337 | break;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | return ui_unclaimed;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | /** Destroy check box control.
|
---|
| 344 | *
|
---|
| 345 | * @param arg Argument (ui_checkbox_t *)
|
---|
| 346 | */
|
---|
| 347 | void ui_checkbox_ctl_destroy(void *arg)
|
---|
| 348 | {
|
---|
| 349 | ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
|
---|
| 350 |
|
---|
| 351 | ui_checkbox_destroy(checkbox);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /** Paint check box control.
|
---|
| 355 | *
|
---|
| 356 | * @param arg Argument (ui_checkbox_t *)
|
---|
| 357 | * @return EOK on success or an error code
|
---|
| 358 | */
|
---|
| 359 | errno_t ui_checkbox_ctl_paint(void *arg)
|
---|
| 360 | {
|
---|
| 361 | ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
|
---|
| 362 |
|
---|
| 363 | return ui_checkbox_paint(checkbox);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /** Handle check box control position event.
|
---|
| 367 | *
|
---|
| 368 | * @param arg Argument (ui_checkbox_t *)
|
---|
| 369 | * @param pos_event Position event
|
---|
| 370 | * @return @c ui_claimed iff the event is claimed
|
---|
| 371 | */
|
---|
| 372 | ui_evclaim_t ui_checkbox_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 373 | {
|
---|
| 374 | ui_checkbox_t *checkbox = (ui_checkbox_t *) arg;
|
---|
| 375 |
|
---|
| 376 | return ui_checkbox_pos_event(checkbox, event);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | /** @}
|
---|
| 380 | */
|
---|