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