[214aefb] | 1 | /*
|
---|
| 2 | * Copyright (c) 2021 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 Menu entry
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <adt/list.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <gfx/color.h>
|
---|
| 39 | #include <gfx/context.h>
|
---|
| 40 | #include <gfx/render.h>
|
---|
| 41 | #include <gfx/text.h>
|
---|
| 42 | #include <io/pos_event.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
| 44 | #include <str.h>
|
---|
| 45 | #include <ui/control.h>
|
---|
| 46 | #include <ui/paint.h>
|
---|
| 47 | #include <ui/menuentry.h>
|
---|
| 48 | #include "../private/menubar.h"
|
---|
| 49 | #include "../private/menuentry.h"
|
---|
| 50 | #include "../private/menu.h"
|
---|
| 51 | #include "../private/resource.h"
|
---|
| 52 |
|
---|
| 53 | enum {
|
---|
| 54 | menu_entry_hpad = 4,
|
---|
| 55 | menu_entry_vpad = 4,
|
---|
| 56 | menu_entry_hpad_text = 1,
|
---|
| 57 | menu_entry_vpad_text = 0
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | /** Create new menu entry.
|
---|
| 61 | *
|
---|
| 62 | * @param menu Menu
|
---|
| 63 | * @param caption Caption
|
---|
| 64 | * @param rmentry Place to store pointer to new menu entry
|
---|
| 65 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 66 | */
|
---|
| 67 | errno_t ui_menu_entry_create(ui_menu_t *menu, const char *caption,
|
---|
| 68 | ui_menu_entry_t **rmentry)
|
---|
| 69 | {
|
---|
| 70 | ui_menu_entry_t *mentry;
|
---|
| 71 |
|
---|
| 72 | mentry = calloc(1, sizeof(ui_menu_entry_t));
|
---|
| 73 | if (mentry == NULL)
|
---|
| 74 | return ENOMEM;
|
---|
| 75 |
|
---|
| 76 | mentry->caption = str_dup(caption);
|
---|
| 77 | if (mentry->caption == NULL) {
|
---|
| 78 | free(mentry);
|
---|
| 79 | return ENOMEM;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | mentry->menu = menu;
|
---|
| 83 | list_append(&mentry->lentries, &menu->entries);
|
---|
| 84 |
|
---|
| 85 | *rmentry = mentry;
|
---|
| 86 | return EOK;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /** Destroy menu entry.
|
---|
| 90 | *
|
---|
| 91 | * @param mentry Menu entry or @c NULL
|
---|
| 92 | */
|
---|
| 93 | void ui_menu_entry_destroy(ui_menu_entry_t *mentry)
|
---|
| 94 | {
|
---|
| 95 | if (mentry == NULL)
|
---|
| 96 | return;
|
---|
| 97 |
|
---|
| 98 | list_remove(&mentry->lentries);
|
---|
| 99 | free(mentry->caption);
|
---|
| 100 | free(mentry);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /** Set menu entry callback.
|
---|
| 104 | *
|
---|
| 105 | * @param mentry Menu entry
|
---|
| 106 | * @param cb Menu entry callback
|
---|
| 107 | * @param arg Callback argument
|
---|
| 108 | */
|
---|
| 109 | void ui_menu_entry_set_cb(ui_menu_entry_t *mentry, ui_menu_entry_cb_t cb,
|
---|
| 110 | void *arg)
|
---|
| 111 | {
|
---|
| 112 | mentry->cb = cb;
|
---|
| 113 | mentry->arg = arg;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /** Get first menu entry in menu.
|
---|
| 117 | *
|
---|
| 118 | * @param menu Menu
|
---|
| 119 | * @return First menu entry or @c NULL if there is none
|
---|
| 120 | */
|
---|
| 121 | ui_menu_entry_t *ui_menu_entry_first(ui_menu_t *menu)
|
---|
| 122 | {
|
---|
| 123 | link_t *link;
|
---|
| 124 |
|
---|
| 125 | link = list_first(&menu->entries);
|
---|
| 126 | if (link == NULL)
|
---|
| 127 | return NULL;
|
---|
| 128 |
|
---|
| 129 | return list_get_instance(link, ui_menu_entry_t, lentries);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /** Get next menu entry in menu.
|
---|
| 133 | *
|
---|
| 134 | * @param cur Current menu entry
|
---|
| 135 | * @return Next menu entry or @c NULL if @a cur is the last one
|
---|
| 136 | */
|
---|
| 137 | ui_menu_entry_t *ui_menu_entry_next(ui_menu_entry_t *cur)
|
---|
| 138 | {
|
---|
| 139 | link_t *link;
|
---|
| 140 |
|
---|
| 141 | link = list_next(&cur->lentries, &cur->menu->entries);
|
---|
| 142 | if (link == NULL)
|
---|
| 143 | return NULL;
|
---|
| 144 |
|
---|
| 145 | return list_get_instance(link, ui_menu_entry_t, lentries);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /** Get width of menu entry.
|
---|
| 149 | *
|
---|
| 150 | * @param mentry Menu entry
|
---|
| 151 | * @return Width in pixels
|
---|
| 152 | */
|
---|
| 153 | gfx_coord_t ui_menu_entry_width(ui_menu_entry_t *mentry)
|
---|
| 154 | {
|
---|
| 155 | ui_resource_t *res;
|
---|
| 156 | gfx_coord_t hpad;
|
---|
| 157 |
|
---|
| 158 | res = mentry->menu->mbar->res;
|
---|
| 159 |
|
---|
| 160 | if (res->textmode) {
|
---|
| 161 | hpad = menu_entry_hpad_text;
|
---|
| 162 | } else {
|
---|
| 163 | hpad = menu_entry_hpad;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | return gfx_text_width(res->font, mentry->caption) + 2 * hpad;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** Get height of menu entry.
|
---|
| 170 | *
|
---|
| 171 | * @param mentry Menu entry
|
---|
| 172 | * @return Width in pixels
|
---|
| 173 | */
|
---|
| 174 | gfx_coord_t ui_menu_entry_height(ui_menu_entry_t *mentry)
|
---|
| 175 | {
|
---|
| 176 | ui_resource_t *res;
|
---|
| 177 | gfx_font_metrics_t metrics;
|
---|
| 178 | gfx_coord_t height;
|
---|
| 179 | gfx_coord_t vpad;
|
---|
| 180 |
|
---|
| 181 | res = mentry->menu->mbar->res;
|
---|
| 182 |
|
---|
| 183 | if (res->textmode) {
|
---|
| 184 | vpad = menu_entry_vpad_text;
|
---|
| 185 | } else {
|
---|
| 186 | vpad = menu_entry_vpad;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | gfx_font_get_metrics(res->font, &metrics);
|
---|
| 190 | height = metrics.ascent + metrics.descent;
|
---|
| 191 | return height + 2 * vpad;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /** Paint menu entry.
|
---|
| 195 | *
|
---|
| 196 | * @param mentry Menu entry
|
---|
| 197 | * @param pos Position where to paint entry
|
---|
| 198 | * @return EOK on success or an error code
|
---|
| 199 | */
|
---|
| 200 | errno_t ui_menu_entry_paint(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
|
---|
| 201 | {
|
---|
| 202 | ui_resource_t *res;
|
---|
| 203 | gfx_text_fmt_t fmt;
|
---|
| 204 | gfx_color_t *bg_color;
|
---|
| 205 | const char *caption;
|
---|
| 206 | ui_menu_entry_geom_t geom;
|
---|
| 207 | errno_t rc;
|
---|
| 208 |
|
---|
| 209 | res = mentry->menu->mbar->res;
|
---|
| 210 |
|
---|
| 211 | ui_menu_entry_get_geom(mentry, pos, &geom);
|
---|
| 212 |
|
---|
| 213 | gfx_text_fmt_init(&fmt);
|
---|
| 214 | fmt.halign = gfx_halign_left;
|
---|
| 215 | fmt.valign = gfx_valign_top;
|
---|
| 216 |
|
---|
| 217 | caption = mentry->caption;
|
---|
| 218 |
|
---|
[0262f16c] | 219 | if ((mentry->held && mentry->inside) ||
|
---|
| 220 | mentry == mentry->menu->selected) {
|
---|
[214aefb] | 221 | fmt.color = res->wnd_sel_text_color;
|
---|
| 222 | bg_color = res->wnd_sel_text_bg_color;
|
---|
| 223 | } else {
|
---|
| 224 | fmt.color = res->wnd_text_color;
|
---|
| 225 | bg_color = res->wnd_face_color;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | rc = gfx_set_color(res->gc, bg_color);
|
---|
| 229 | if (rc != EOK)
|
---|
| 230 | goto error;
|
---|
| 231 |
|
---|
| 232 | rc = gfx_fill_rect(res->gc, &geom.outer_rect);
|
---|
| 233 | if (rc != EOK)
|
---|
| 234 | goto error;
|
---|
| 235 |
|
---|
| 236 | rc = gfx_puttext(res->font, &geom.text_pos, &fmt, caption);
|
---|
| 237 | if (rc != EOK)
|
---|
| 238 | goto error;
|
---|
| 239 |
|
---|
| 240 | rc = gfx_update(res->gc);
|
---|
| 241 | if (rc != EOK)
|
---|
| 242 | goto error;
|
---|
| 243 |
|
---|
| 244 | return EOK;
|
---|
| 245 | error:
|
---|
| 246 | return rc;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /** Handle button press in menu entry.
|
---|
| 250 | *
|
---|
| 251 | * @param mentry Menu entry
|
---|
[0262f16c] | 252 | * @param pos Menu entry position
|
---|
[214aefb] | 253 | */
|
---|
[0262f16c] | 254 | void ui_menu_entry_press(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
|
---|
[214aefb] | 255 | {
|
---|
[0262f16c] | 256 | if (mentry->held)
|
---|
| 257 | return;
|
---|
[214aefb] | 258 |
|
---|
[0262f16c] | 259 | mentry->inside = true;
|
---|
| 260 | mentry->held = true;
|
---|
| 261 | ui_menu_entry_paint(mentry, pos);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /** Handle button release in menu entry.
|
---|
| 265 | *
|
---|
| 266 | * @param mentry Menu entry
|
---|
| 267 | */
|
---|
| 268 | void ui_menu_entry_release(ui_menu_entry_t *mentry)
|
---|
| 269 | {
|
---|
| 270 | if (!mentry->held)
|
---|
| 271 | return;
|
---|
[214aefb] | 272 |
|
---|
[0262f16c] | 273 | mentry->held = false;
|
---|
[214aefb] | 274 |
|
---|
[0262f16c] | 275 | if (mentry->inside) {
|
---|
[214aefb] | 276 | /* Close menu */
|
---|
| 277 | ui_menu_bar_select(mentry->menu->mbar,
|
---|
| 278 | &mentry->menu->mbar->sel_pos, NULL);
|
---|
| 279 |
|
---|
| 280 | /* Call back */
|
---|
| 281 | if (mentry->cb != NULL)
|
---|
| 282 | mentry->cb(mentry, mentry->arg);
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[0262f16c] | 286 | /** Pointer entered menu entry.
|
---|
| 287 | *
|
---|
| 288 | * @param mentry Menu entry
|
---|
| 289 | * @param pos Menu entry position
|
---|
| 290 | */
|
---|
| 291 | void ui_menu_entry_enter(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
|
---|
| 292 | {
|
---|
| 293 | if (mentry->inside)
|
---|
| 294 | return;
|
---|
| 295 |
|
---|
| 296 | mentry->inside = true;
|
---|
| 297 | if (mentry->held)
|
---|
| 298 | (void) ui_menu_entry_paint(mentry, pos);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /** Pointer left menu entry.
|
---|
| 302 | *
|
---|
| 303 | * @param mentry Menu entry
|
---|
| 304 | * @param pos Menu entry position
|
---|
| 305 | */
|
---|
| 306 | void ui_menu_entry_leave(ui_menu_entry_t *mentry, gfx_coord2_t *pos)
|
---|
| 307 | {
|
---|
| 308 | if (!mentry->inside)
|
---|
| 309 | return;
|
---|
| 310 |
|
---|
| 311 | mentry->inside = false;
|
---|
| 312 | if (mentry->held)
|
---|
| 313 | (void) ui_menu_entry_paint(mentry, pos);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | /** Handle menu entry position event.
|
---|
| 317 | *
|
---|
| 318 | * @param mentry Menu entry
|
---|
| 319 | * @param pos Menu entry position (top-left corner)
|
---|
| 320 | * @param pos_event Position event
|
---|
| 321 | * @return @c ui_claimed iff the event is claimed
|
---|
| 322 | */
|
---|
| 323 | ui_evclaim_t ui_menu_entry_pos_event(ui_menu_entry_t *mentry,
|
---|
| 324 | gfx_coord2_t *pos, pos_event_t *event)
|
---|
| 325 | {
|
---|
| 326 | ui_menu_entry_geom_t geom;
|
---|
| 327 | gfx_coord2_t ppos;
|
---|
| 328 | bool inside;
|
---|
| 329 |
|
---|
| 330 | ppos.x = event->hpos;
|
---|
| 331 | ppos.y = event->vpos;
|
---|
| 332 |
|
---|
| 333 | ui_menu_entry_get_geom(mentry, pos, &geom);
|
---|
| 334 | inside = gfx_pix_inside_rect(&ppos, &geom.outer_rect);
|
---|
| 335 |
|
---|
| 336 | switch (event->type) {
|
---|
| 337 | case POS_PRESS:
|
---|
| 338 | if (inside) {
|
---|
| 339 | ui_menu_entry_press(mentry, pos);
|
---|
| 340 | return ui_claimed;
|
---|
| 341 | }
|
---|
| 342 | break;
|
---|
| 343 | case POS_RELEASE:
|
---|
| 344 | if (mentry->held) {
|
---|
| 345 | ui_menu_entry_release(mentry);
|
---|
| 346 | return ui_claimed;
|
---|
| 347 | }
|
---|
| 348 | break;
|
---|
| 349 | case POS_UPDATE:
|
---|
| 350 | if (inside && !mentry->inside) {
|
---|
| 351 | ui_menu_entry_enter(mentry, pos);
|
---|
| 352 | return ui_claimed;
|
---|
| 353 | } else if (!inside && mentry->inside) {
|
---|
| 354 | ui_menu_entry_leave(mentry, pos);
|
---|
| 355 | }
|
---|
| 356 | break;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | return ui_unclaimed;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[214aefb] | 362 | /** Get menu entry geometry.
|
---|
| 363 | *
|
---|
| 364 | * @param mentry Menu entry
|
---|
| 365 | * @param spos Entry position
|
---|
| 366 | * @param geom Structure to fill in with computed geometry
|
---|
| 367 | */
|
---|
| 368 | void ui_menu_entry_get_geom(ui_menu_entry_t *mentry, gfx_coord2_t *pos,
|
---|
| 369 | ui_menu_entry_geom_t *geom)
|
---|
| 370 | {
|
---|
| 371 | ui_resource_t *res;
|
---|
| 372 | gfx_coord_t hpad;
|
---|
| 373 | gfx_coord_t vpad;
|
---|
| 374 | const char *caption;
|
---|
| 375 | gfx_coord_t width;
|
---|
| 376 |
|
---|
| 377 | res = mentry->menu->mbar->res;
|
---|
| 378 |
|
---|
| 379 | if (res->textmode) {
|
---|
| 380 | hpad = menu_entry_hpad_text;
|
---|
| 381 | vpad = menu_entry_vpad_text;
|
---|
| 382 | } else {
|
---|
| 383 | hpad = menu_entry_hpad;
|
---|
| 384 | vpad = menu_entry_vpad;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | caption = mentry->caption;
|
---|
| 388 | width = gfx_text_width(res->font, caption) + 2 * hpad;
|
---|
| 389 | geom->text_pos.x = pos->x + hpad;
|
---|
| 390 | geom->text_pos.y = pos->y + vpad;
|
---|
| 391 |
|
---|
| 392 | geom->outer_rect.p0 = *pos;
|
---|
| 393 | geom->outer_rect.p1.x = geom->outer_rect.p0.x + width;
|
---|
| 394 | geom->outer_rect.p1.y = geom->outer_rect.p0.y +
|
---|
| 395 | ui_menu_entry_height(mentry);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | /** @}
|
---|
| 399 | */
|
---|