| 1 | /*
|
|---|
| 2 | * Copyright (c) 2022 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
|
|---|
| 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 <uchar.h>
|
|---|
| 46 | #include <ui/accel.h>
|
|---|
| 47 | #include <ui/control.h>
|
|---|
| 48 | #include <ui/paint.h>
|
|---|
| 49 | #include <ui/popup.h>
|
|---|
| 50 | #include <ui/menu.h>
|
|---|
| 51 | #include <ui/menubar.h>
|
|---|
| 52 | #include <ui/menuentry.h>
|
|---|
| 53 | #include <ui/resource.h>
|
|---|
| 54 | #include <ui/window.h>
|
|---|
| 55 | #include "../private/menubar.h"
|
|---|
| 56 | #include "../private/menu.h"
|
|---|
| 57 | #include "../private/resource.h"
|
|---|
| 58 |
|
|---|
| 59 | enum {
|
|---|
| 60 | menu_frame_w = 4,
|
|---|
| 61 | menu_frame_h = 4,
|
|---|
| 62 | menu_frame_w_text = 2,
|
|---|
| 63 | menu_frame_h_text = 1,
|
|---|
| 64 | menu_frame_h_margin_text = 1
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | static void ui_menu_popup_close(ui_popup_t *, void *);
|
|---|
| 68 | static void ui_menu_popup_kbd(ui_popup_t *, void *, kbd_event_t *);
|
|---|
| 69 | static void ui_menu_popup_pos(ui_popup_t *, void *, pos_event_t *);
|
|---|
| 70 | static void ui_menu_key_press_unmod(ui_menu_t *, kbd_event_t *);
|
|---|
| 71 |
|
|---|
| 72 | static ui_popup_cb_t ui_menu_popup_cb = {
|
|---|
| 73 | .close = ui_menu_popup_close,
|
|---|
| 74 | .kbd = ui_menu_popup_kbd,
|
|---|
| 75 | .pos = ui_menu_popup_pos
|
|---|
| 76 | };
|
|---|
| 77 |
|
|---|
| 78 | /** Create new menu.
|
|---|
| 79 | *
|
|---|
| 80 | * @param mbar Menu bar
|
|---|
| 81 | * @param caption Caption
|
|---|
| 82 | * @param rmenu Place to store pointer to new menu
|
|---|
| 83 | * @return EOK on success, ENOMEM if out of memory
|
|---|
| 84 | */
|
|---|
| 85 | errno_t ui_menu_create(ui_menu_bar_t *mbar, const char *caption,
|
|---|
| 86 | ui_menu_t **rmenu)
|
|---|
| 87 | {
|
|---|
| 88 | ui_menu_t *menu;
|
|---|
| 89 |
|
|---|
| 90 | menu = calloc(1, sizeof(ui_menu_t));
|
|---|
| 91 | if (menu == NULL)
|
|---|
| 92 | return ENOMEM;
|
|---|
| 93 |
|
|---|
| 94 | menu->caption = str_dup(caption);
|
|---|
| 95 | if (menu->caption == NULL) {
|
|---|
| 96 | free(menu);
|
|---|
| 97 | return ENOMEM;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | menu->mbar = mbar;
|
|---|
| 101 | list_append(&menu->lmenus, &mbar->menus);
|
|---|
| 102 | list_initialize(&menu->entries);
|
|---|
| 103 |
|
|---|
| 104 | *rmenu = menu;
|
|---|
| 105 | return EOK;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Destroy menu.
|
|---|
| 109 | *
|
|---|
| 110 | * @param menu Menu or @c NULL
|
|---|
| 111 | */
|
|---|
| 112 | void ui_menu_destroy(ui_menu_t *menu)
|
|---|
| 113 | {
|
|---|
| 114 | ui_menu_entry_t *mentry;
|
|---|
| 115 |
|
|---|
| 116 | if (menu == NULL)
|
|---|
| 117 | return;
|
|---|
| 118 |
|
|---|
| 119 | /* Destroy entries */
|
|---|
| 120 | mentry = ui_menu_entry_first(menu);
|
|---|
| 121 | while (mentry != NULL) {
|
|---|
| 122 | ui_menu_entry_destroy(mentry);
|
|---|
| 123 | mentry = ui_menu_entry_first(menu);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | list_remove(&menu->lmenus);
|
|---|
| 127 | free(menu->caption);
|
|---|
| 128 | free(menu);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /** Get first menu in menu bar.
|
|---|
| 132 | *
|
|---|
| 133 | * @param mbar Menu bar
|
|---|
| 134 | * @return First menu or @c NULL if there is none
|
|---|
| 135 | */
|
|---|
| 136 | ui_menu_t *ui_menu_first(ui_menu_bar_t *mbar)
|
|---|
| 137 | {
|
|---|
| 138 | link_t *link;
|
|---|
| 139 |
|
|---|
| 140 | link = list_first(&mbar->menus);
|
|---|
| 141 | if (link == NULL)
|
|---|
| 142 | return NULL;
|
|---|
| 143 |
|
|---|
| 144 | return list_get_instance(link, ui_menu_t, lmenus);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /** Get next menu in menu bar.
|
|---|
| 148 | *
|
|---|
| 149 | * @param cur Current menu
|
|---|
| 150 | * @return Next menu or @c NULL if @a cur is the last one
|
|---|
| 151 | */
|
|---|
| 152 | ui_menu_t *ui_menu_next(ui_menu_t *cur)
|
|---|
| 153 | {
|
|---|
| 154 | link_t *link;
|
|---|
| 155 |
|
|---|
| 156 | link = list_next(&cur->lmenus, &cur->mbar->menus);
|
|---|
| 157 | if (link == NULL)
|
|---|
| 158 | return NULL;
|
|---|
| 159 |
|
|---|
| 160 | return list_get_instance(link, ui_menu_t, lmenus);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /** Get last menu in menu bar.
|
|---|
| 164 | *
|
|---|
| 165 | * @param mbar Menu bar
|
|---|
| 166 | * @return Last menu or @c NULL if there is none
|
|---|
| 167 | */
|
|---|
| 168 | ui_menu_t *ui_menu_last(ui_menu_bar_t *mbar)
|
|---|
| 169 | {
|
|---|
| 170 | link_t *link;
|
|---|
| 171 |
|
|---|
| 172 | link = list_last(&mbar->menus);
|
|---|
| 173 | if (link == NULL)
|
|---|
| 174 | return NULL;
|
|---|
| 175 |
|
|---|
| 176 | return list_get_instance(link, ui_menu_t, lmenus);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /** Get previous menu in menu bar.
|
|---|
| 180 | *
|
|---|
| 181 | * @param cur Current menu
|
|---|
| 182 | * @return Previous menu or @c NULL if @a cur is the fist one
|
|---|
| 183 | */
|
|---|
| 184 | ui_menu_t *ui_menu_prev(ui_menu_t *cur)
|
|---|
| 185 | {
|
|---|
| 186 | link_t *link;
|
|---|
| 187 |
|
|---|
| 188 | link = list_prev(&cur->lmenus, &cur->mbar->menus);
|
|---|
| 189 | if (link == NULL)
|
|---|
| 190 | return NULL;
|
|---|
| 191 |
|
|---|
| 192 | return list_get_instance(link, ui_menu_t, lmenus);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /** Get menu caption.
|
|---|
| 196 | *
|
|---|
| 197 | * @param menu Menu
|
|---|
| 198 | * @return Caption (owned by @a menu)
|
|---|
| 199 | */
|
|---|
| 200 | const char *ui_menu_caption(ui_menu_t *menu)
|
|---|
| 201 | {
|
|---|
| 202 | return menu->caption;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** Get menu geometry.
|
|---|
| 206 | *
|
|---|
| 207 | * @param menu Menu
|
|---|
| 208 | * @param spos Starting position
|
|---|
| 209 | * @param geom Structure to fill in with computed geometry
|
|---|
| 210 | */
|
|---|
| 211 | void ui_menu_get_geom(ui_menu_t *menu, gfx_coord2_t *spos,
|
|---|
| 212 | ui_menu_geom_t *geom)
|
|---|
| 213 | {
|
|---|
| 214 | ui_resource_t *res;
|
|---|
| 215 | gfx_coord2_t edim;
|
|---|
| 216 | gfx_coord_t frame_w;
|
|---|
| 217 | gfx_coord_t frame_h;
|
|---|
| 218 |
|
|---|
| 219 | res = ui_window_get_res(menu->mbar->window);
|
|---|
| 220 |
|
|---|
| 221 | if (res->textmode) {
|
|---|
| 222 | frame_w = menu_frame_w_text;
|
|---|
| 223 | frame_h = menu_frame_h_text;
|
|---|
| 224 | } else {
|
|---|
| 225 | frame_w = menu_frame_w;
|
|---|
| 226 | frame_h = menu_frame_h;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | edim.x = ui_menu_entry_calc_width(menu, menu->max_caption_w,
|
|---|
| 230 | menu->max_shortcut_w);
|
|---|
| 231 | edim.y = menu->total_h;
|
|---|
| 232 |
|
|---|
| 233 | geom->outer_rect.p0 = *spos;
|
|---|
| 234 | geom->outer_rect.p1.x = spos->x + edim.x + 2 * frame_w;
|
|---|
| 235 | geom->outer_rect.p1.y = spos->y + edim.y + 2 * frame_h;
|
|---|
| 236 |
|
|---|
| 237 | geom->entries_rect.p0.x = spos->x + frame_w;
|
|---|
| 238 | geom->entries_rect.p0.y = spos->y + frame_h;
|
|---|
| 239 | geom->entries_rect.p1.x = geom->entries_rect.p0.x + edim.x;
|
|---|
| 240 | geom->entries_rect.p1.y = geom->entries_rect.p0.x + edim.y;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /** Get menu rectangle.
|
|---|
| 244 | *
|
|---|
| 245 | * @param menu Menu
|
|---|
| 246 | * @param spos Starting position (top-left corner)
|
|---|
| 247 | * @param rect Place to store menu rectangle
|
|---|
| 248 | */
|
|---|
| 249 | void ui_menu_get_rect(ui_menu_t *menu, gfx_coord2_t *spos, gfx_rect_t *rect)
|
|---|
| 250 | {
|
|---|
| 251 | ui_menu_geom_t geom;
|
|---|
| 252 |
|
|---|
| 253 | ui_menu_get_geom(menu, spos, &geom);
|
|---|
| 254 | *rect = geom.outer_rect;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /** Get menu accelerator character.
|
|---|
| 258 | *
|
|---|
| 259 | * @param menu Menu
|
|---|
| 260 | * @return Accelerator character (lowercase) or the null character if
|
|---|
| 261 | * the menu has no accelerator.
|
|---|
| 262 | */
|
|---|
| 263 | char32_t ui_menu_get_accel(ui_menu_t *menu)
|
|---|
| 264 | {
|
|---|
| 265 | return ui_accel_get(menu->caption);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /** Get UI resource from menu.
|
|---|
| 269 | *
|
|---|
| 270 | * @param menu Menu
|
|---|
| 271 | * @return UI resource
|
|---|
| 272 | */
|
|---|
| 273 | ui_resource_t *ui_menu_get_res(ui_menu_t *menu)
|
|---|
| 274 | {
|
|---|
| 275 | return ui_popup_get_res(menu->popup);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /** Open menu.
|
|---|
| 279 | *
|
|---|
| 280 | * @param menu Menu
|
|---|
| 281 | * @param prect Parent rectangle around which the menu should be placed
|
|---|
| 282 | */
|
|---|
| 283 | errno_t ui_menu_open(ui_menu_t *menu, gfx_rect_t *prect)
|
|---|
| 284 | {
|
|---|
| 285 | ui_popup_t *popup = NULL;
|
|---|
| 286 | ui_popup_params_t params;
|
|---|
| 287 | ui_menu_geom_t geom;
|
|---|
| 288 | gfx_coord2_t mpos;
|
|---|
| 289 | errno_t rc;
|
|---|
| 290 |
|
|---|
| 291 | /* Select first entry */
|
|---|
| 292 | menu->selected = ui_menu_entry_first(menu);
|
|---|
| 293 |
|
|---|
| 294 | /* Determine menu dimensions */
|
|---|
| 295 |
|
|---|
| 296 | mpos.x = 0;
|
|---|
| 297 | mpos.y = 0;
|
|---|
| 298 | ui_menu_get_geom(menu, &mpos, &geom);
|
|---|
| 299 |
|
|---|
| 300 | ui_popup_params_init(¶ms);
|
|---|
| 301 | params.rect = geom.outer_rect;
|
|---|
| 302 | params.place = *prect;
|
|---|
| 303 |
|
|---|
| 304 | rc = ui_popup_create(menu->mbar->ui, menu->mbar->window, ¶ms,
|
|---|
| 305 | &popup);
|
|---|
| 306 | if (rc != EOK)
|
|---|
| 307 | return rc;
|
|---|
| 308 |
|
|---|
| 309 | menu->popup = popup;
|
|---|
| 310 | ui_popup_set_cb(popup, &ui_menu_popup_cb, menu);
|
|---|
| 311 |
|
|---|
| 312 | return ui_menu_paint(menu, &mpos);
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /** Close menu.
|
|---|
| 316 | *
|
|---|
| 317 | * @param menu Menu
|
|---|
| 318 | */
|
|---|
| 319 | void ui_menu_close(ui_menu_t *menu)
|
|---|
| 320 | {
|
|---|
| 321 | ui_popup_destroy(menu->popup);
|
|---|
| 322 | menu->popup = NULL;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | /** Determine if menu is open.
|
|---|
| 326 | *
|
|---|
| 327 | * @param menu Menu
|
|---|
| 328 | * @return @c true iff menu is open
|
|---|
| 329 | */
|
|---|
| 330 | bool ui_menu_is_open(ui_menu_t *menu)
|
|---|
| 331 | {
|
|---|
| 332 | return menu->popup != NULL;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | /** Paint menu.
|
|---|
| 336 | *
|
|---|
| 337 | * @param menu Menu
|
|---|
| 338 | * @param spos Starting position (top-left corner)
|
|---|
| 339 | * @return EOK on success or an error code
|
|---|
| 340 | */
|
|---|
| 341 | errno_t ui_menu_paint_bg_gfx(ui_menu_t *menu, gfx_coord2_t *spos)
|
|---|
| 342 | {
|
|---|
| 343 | ui_resource_t *res;
|
|---|
| 344 | ui_menu_geom_t geom;
|
|---|
| 345 | gfx_rect_t bg_rect;
|
|---|
| 346 | errno_t rc;
|
|---|
| 347 |
|
|---|
| 348 | res = ui_menu_get_res(menu);
|
|---|
| 349 | ui_menu_get_geom(menu, spos, &geom);
|
|---|
| 350 |
|
|---|
| 351 | /* Paint menu frame */
|
|---|
| 352 |
|
|---|
| 353 | rc = gfx_set_color(res->gc, res->wnd_face_color);
|
|---|
| 354 | if (rc != EOK)
|
|---|
| 355 | goto error;
|
|---|
| 356 |
|
|---|
| 357 | rc = ui_paint_outset_frame(res, &geom.outer_rect, &bg_rect);
|
|---|
| 358 | if (rc != EOK)
|
|---|
| 359 | goto error;
|
|---|
| 360 |
|
|---|
| 361 | /* Paint menu background */
|
|---|
| 362 |
|
|---|
| 363 | rc = gfx_set_color(res->gc, res->wnd_face_color);
|
|---|
| 364 | if (rc != EOK)
|
|---|
| 365 | goto error;
|
|---|
| 366 |
|
|---|
| 367 | rc = gfx_fill_rect(res->gc, &bg_rect);
|
|---|
| 368 | if (rc != EOK)
|
|---|
| 369 | goto error;
|
|---|
| 370 |
|
|---|
| 371 | return EOK;
|
|---|
| 372 | error:
|
|---|
| 373 | return rc;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | /** Paint menu.
|
|---|
| 377 | *
|
|---|
| 378 | * @param menu Menu
|
|---|
| 379 | * @param spos Starting position (top-left corner)
|
|---|
| 380 | * @return EOK on success or an error code
|
|---|
| 381 | */
|
|---|
| 382 | errno_t ui_menu_paint_bg_text(ui_menu_t *menu, gfx_coord2_t *spos)
|
|---|
| 383 | {
|
|---|
| 384 | ui_resource_t *res;
|
|---|
| 385 | ui_menu_geom_t geom;
|
|---|
| 386 | gfx_rect_t rect;
|
|---|
| 387 | errno_t rc;
|
|---|
| 388 |
|
|---|
| 389 | res = ui_menu_get_res(menu);
|
|---|
| 390 | ui_menu_get_geom(menu, spos, &geom);
|
|---|
| 391 |
|
|---|
| 392 | /* Paint menu background */
|
|---|
| 393 |
|
|---|
| 394 | rc = gfx_set_color(res->gc, res->wnd_face_color);
|
|---|
| 395 | if (rc != EOK)
|
|---|
| 396 | goto error;
|
|---|
| 397 |
|
|---|
| 398 | rc = gfx_fill_rect(res->gc, &geom.outer_rect);
|
|---|
| 399 | if (rc != EOK)
|
|---|
| 400 | goto error;
|
|---|
| 401 |
|
|---|
| 402 | /* Paint menu box */
|
|---|
| 403 |
|
|---|
| 404 | rect = geom.outer_rect;
|
|---|
| 405 | rect.p0.x += menu_frame_h_margin_text;
|
|---|
| 406 | rect.p1.x -= menu_frame_h_margin_text;
|
|---|
| 407 |
|
|---|
| 408 | rc = ui_paint_text_box(res, &rect, ui_box_single, res->wnd_face_color);
|
|---|
| 409 | if (rc != EOK)
|
|---|
| 410 | goto error;
|
|---|
| 411 |
|
|---|
| 412 | return EOK;
|
|---|
| 413 | error:
|
|---|
| 414 | return rc;
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | /** Paint menu.
|
|---|
| 418 | *
|
|---|
| 419 | * @param menu Menu
|
|---|
| 420 | * @param spos Starting position (top-left corner)
|
|---|
| 421 | * @return EOK on success or an error code
|
|---|
| 422 | */
|
|---|
| 423 | errno_t ui_menu_paint(ui_menu_t *menu, gfx_coord2_t *spos)
|
|---|
| 424 | {
|
|---|
| 425 | ui_resource_t *res;
|
|---|
| 426 | gfx_coord2_t pos;
|
|---|
| 427 | ui_menu_entry_t *mentry;
|
|---|
| 428 | ui_menu_geom_t geom;
|
|---|
| 429 | errno_t rc;
|
|---|
| 430 |
|
|---|
| 431 | res = ui_menu_get_res(menu);
|
|---|
| 432 | ui_menu_get_geom(menu, spos, &geom);
|
|---|
| 433 |
|
|---|
| 434 | /* Paint menu frame and background */
|
|---|
| 435 | if (res->textmode)
|
|---|
| 436 | rc = ui_menu_paint_bg_text(menu, spos);
|
|---|
| 437 | else
|
|---|
| 438 | rc = ui_menu_paint_bg_gfx(menu, spos);
|
|---|
| 439 | if (rc != EOK)
|
|---|
| 440 | goto error;
|
|---|
| 441 |
|
|---|
| 442 | /* Paint entries */
|
|---|
| 443 | pos = geom.entries_rect.p0;
|
|---|
| 444 |
|
|---|
| 445 | mentry = ui_menu_entry_first(menu);
|
|---|
| 446 | while (mentry != NULL) {
|
|---|
| 447 | rc = ui_menu_entry_paint(mentry, &pos);
|
|---|
| 448 | if (rc != EOK)
|
|---|
| 449 | goto error;
|
|---|
| 450 |
|
|---|
| 451 | pos.y += ui_menu_entry_height(mentry);
|
|---|
| 452 | mentry = ui_menu_entry_next(mentry);
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | rc = gfx_update(res->gc);
|
|---|
| 456 | if (rc != EOK)
|
|---|
| 457 | goto error;
|
|---|
| 458 |
|
|---|
| 459 | return EOK;
|
|---|
| 460 | error:
|
|---|
| 461 | return rc;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /** Handle position event in menu.
|
|---|
| 465 | *
|
|---|
| 466 | * @param menu Menu
|
|---|
| 467 | * @param spos Starting position (top-left corner)
|
|---|
| 468 | * @param event Position event
|
|---|
| 469 | * @return ui_claimed iff the event was claimed
|
|---|
| 470 | */
|
|---|
| 471 | ui_evclaim_t ui_menu_pos_event(ui_menu_t *menu, gfx_coord2_t *spos,
|
|---|
| 472 | pos_event_t *event)
|
|---|
| 473 | {
|
|---|
| 474 | ui_menu_geom_t geom;
|
|---|
| 475 | ui_menu_entry_t *mentry;
|
|---|
| 476 | gfx_coord2_t pos;
|
|---|
| 477 | gfx_coord2_t epos;
|
|---|
| 478 | ui_evclaim_t claimed;
|
|---|
| 479 |
|
|---|
| 480 | ui_menu_get_geom(menu, spos, &geom);
|
|---|
| 481 | epos.x = event->hpos;
|
|---|
| 482 | epos.y = event->vpos;
|
|---|
| 483 |
|
|---|
| 484 | pos = geom.entries_rect.p0;
|
|---|
| 485 |
|
|---|
| 486 | mentry = ui_menu_entry_first(menu);
|
|---|
| 487 | while (mentry != NULL) {
|
|---|
| 488 | claimed = ui_menu_entry_pos_event(mentry, &pos, event);
|
|---|
| 489 | if (claimed == ui_claimed)
|
|---|
| 490 | return ui_claimed;
|
|---|
| 491 |
|
|---|
| 492 | pos.y += ui_menu_entry_height(mentry);
|
|---|
| 493 | mentry = ui_menu_entry_next(mentry);
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | /* Event inside menu? */
|
|---|
| 497 | if (gfx_pix_inside_rect(&epos, &geom.outer_rect)) {
|
|---|
| 498 | /* Claim event */
|
|---|
| 499 | return ui_claimed;
|
|---|
| 500 | } else {
|
|---|
| 501 | /* Press outside menu - close it */
|
|---|
| 502 | if (event->type == POS_PRESS)
|
|---|
| 503 | ui_menu_bar_deactivate(menu->mbar);
|
|---|
| 504 | }
|
|---|
| 505 |
|
|---|
| 506 | return ui_unclaimed;
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | /** Handle keyboard event in menu.
|
|---|
| 510 | *
|
|---|
| 511 | * @param menu Menu
|
|---|
| 512 | * @param event Keyboard event
|
|---|
| 513 | * @return ui_claimed iff the event was claimed
|
|---|
| 514 | */
|
|---|
| 515 | ui_evclaim_t ui_menu_kbd_event(ui_menu_t *menu, kbd_event_t *event)
|
|---|
| 516 | {
|
|---|
| 517 | if (event->type == KEY_PRESS && (event->mods &
|
|---|
| 518 | (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
|
|---|
| 519 | ui_menu_key_press_unmod(menu, event);
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | if (event->type == KEY_PRESS && (event->mods & KM_ALT) != 0 &&
|
|---|
| 523 | (event->mods & (KM_CTRL | KM_SHIFT)) == 0 && event->c != '\0')
|
|---|
| 524 | ui_menu_bar_press_accel(menu->mbar, event->c);
|
|---|
| 525 |
|
|---|
| 526 | return ui_claimed;
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | /** Move one entry up.
|
|---|
| 530 | *
|
|---|
| 531 | * Non-selectable entries are skipped. If we are already at the top,
|
|---|
| 532 | * we wrap around.
|
|---|
| 533 | *
|
|---|
| 534 | * @param menu Menu
|
|---|
| 535 | */
|
|---|
| 536 | void ui_menu_up(ui_menu_t *menu)
|
|---|
| 537 | {
|
|---|
| 538 | gfx_coord2_t mpos;
|
|---|
| 539 | ui_menu_entry_t *nentry;
|
|---|
| 540 |
|
|---|
| 541 | if (menu->selected == NULL)
|
|---|
| 542 | return;
|
|---|
| 543 |
|
|---|
| 544 | nentry = ui_menu_entry_prev(menu->selected);
|
|---|
| 545 | if (nentry == NULL)
|
|---|
| 546 | nentry = ui_menu_entry_last(menu);
|
|---|
| 547 |
|
|---|
| 548 | /* Need to find a selectable entry */
|
|---|
| 549 | while (!ui_menu_entry_selectable(nentry)) {
|
|---|
| 550 | nentry = ui_menu_entry_prev(nentry);
|
|---|
| 551 | if (nentry == NULL)
|
|---|
| 552 | nentry = ui_menu_entry_last(menu);
|
|---|
| 553 |
|
|---|
| 554 | /* Went completely around and found nothing? */
|
|---|
| 555 | if (nentry == menu->selected)
|
|---|
| 556 | return;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | menu->selected = nentry;
|
|---|
| 560 |
|
|---|
| 561 | mpos.x = 0;
|
|---|
| 562 | mpos.y = 0;
|
|---|
| 563 | (void) ui_menu_paint(menu, &mpos);
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | /** Move one entry down.
|
|---|
| 567 | *
|
|---|
| 568 | * Non-selectable entries are skipped. If we are already at the bottom,
|
|---|
| 569 | * we wrap around.
|
|---|
| 570 | *
|
|---|
| 571 | * @param menu Menu
|
|---|
| 572 | */
|
|---|
| 573 | void ui_menu_down(ui_menu_t *menu)
|
|---|
| 574 | {
|
|---|
| 575 | gfx_coord2_t mpos;
|
|---|
| 576 | ui_menu_entry_t *nentry;
|
|---|
| 577 |
|
|---|
| 578 | if (menu->selected == NULL)
|
|---|
| 579 | return;
|
|---|
| 580 |
|
|---|
| 581 | nentry = ui_menu_entry_next(menu->selected);
|
|---|
| 582 | if (nentry == NULL)
|
|---|
| 583 | nentry = ui_menu_entry_first(menu);
|
|---|
| 584 |
|
|---|
| 585 | /* Need to find a selectable entry */
|
|---|
| 586 | while (!ui_menu_entry_selectable(nentry)) {
|
|---|
| 587 | nentry = ui_menu_entry_next(nentry);
|
|---|
| 588 | if (nentry == NULL)
|
|---|
| 589 | nentry = ui_menu_entry_first(menu);
|
|---|
| 590 |
|
|---|
| 591 | /* Went completely around and found nothing? */
|
|---|
| 592 | if (nentry == menu->selected)
|
|---|
| 593 | return;
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | menu->selected = nentry;
|
|---|
| 597 |
|
|---|
| 598 | mpos.x = 0;
|
|---|
| 599 | mpos.y = 0;
|
|---|
| 600 | (void) ui_menu_paint(menu, &mpos);
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | /** Handle key press without modifiers in menu popup window.
|
|---|
| 604 | *
|
|---|
| 605 | * @param menu Menu
|
|---|
| 606 | * @param event Keyboard event
|
|---|
| 607 | */
|
|---|
| 608 | static void ui_menu_key_press_unmod(ui_menu_t *menu, kbd_event_t *event)
|
|---|
| 609 | {
|
|---|
| 610 | ui_menu_entry_t *mentry;
|
|---|
| 611 | char32_t c;
|
|---|
| 612 |
|
|---|
| 613 | switch (event->key) {
|
|---|
| 614 | case KC_ESCAPE:
|
|---|
| 615 | ui_menu_bar_deactivate(menu->mbar);
|
|---|
| 616 | break;
|
|---|
| 617 | case KC_LEFT:
|
|---|
| 618 | ui_menu_bar_left(menu->mbar);
|
|---|
| 619 | break;
|
|---|
| 620 | case KC_RIGHT:
|
|---|
| 621 | ui_menu_bar_right(menu->mbar);
|
|---|
| 622 | break;
|
|---|
| 623 | case KC_UP:
|
|---|
| 624 | ui_menu_up(menu);
|
|---|
| 625 | break;
|
|---|
| 626 | case KC_DOWN:
|
|---|
| 627 | ui_menu_down(menu);
|
|---|
| 628 | break;
|
|---|
| 629 | case KC_ENTER:
|
|---|
| 630 | if (menu->selected != NULL)
|
|---|
| 631 | ui_menu_entry_activate(menu->selected);
|
|---|
| 632 | break;
|
|---|
| 633 | default:
|
|---|
| 634 | if (event->c != '\0') {
|
|---|
| 635 | mentry = ui_menu_entry_first(menu);
|
|---|
| 636 | while (mentry != NULL) {
|
|---|
| 637 | c = ui_menu_entry_get_accel(mentry);
|
|---|
| 638 | if (c == event->c && menu->selected != NULL) {
|
|---|
| 639 | ui_menu_entry_activate(mentry);
|
|---|
| 640 | break;
|
|---|
| 641 | }
|
|---|
| 642 | mentry = ui_menu_entry_next(mentry);
|
|---|
| 643 | }
|
|---|
| 644 | }
|
|---|
| 645 | break;
|
|---|
| 646 | }
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | /** Handle close event in menu popup window.
|
|---|
| 650 | *
|
|---|
| 651 | * @param popup Menu popup window
|
|---|
| 652 | * @param arg Argument (ui_menu_t *)
|
|---|
| 653 | */
|
|---|
| 654 | static void ui_menu_popup_close(ui_popup_t *popup, void *arg)
|
|---|
| 655 | {
|
|---|
| 656 | ui_menu_t *menu = (ui_menu_t *)arg;
|
|---|
| 657 |
|
|---|
| 658 | /* Deactivate menu bar, close menu */
|
|---|
| 659 | ui_menu_bar_deactivate(menu->mbar);
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | /** Handle keyboard event in menu popup window.
|
|---|
| 663 | *
|
|---|
| 664 | * @param popup Menu popup window
|
|---|
| 665 | * @param arg Argument (ui_menu_t *)
|
|---|
| 666 | * @param event Keyboard event
|
|---|
| 667 | */
|
|---|
| 668 | static void ui_menu_popup_kbd(ui_popup_t *popup, void *arg, kbd_event_t *event)
|
|---|
| 669 | {
|
|---|
| 670 | ui_menu_t *menu = (ui_menu_t *)arg;
|
|---|
| 671 |
|
|---|
| 672 | ui_menu_kbd_event(menu, event);
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 | /** Handle position event in menu popup window.
|
|---|
| 676 | *
|
|---|
| 677 | * @param popup Menu popup window
|
|---|
| 678 | * @param arg Argument (ui_menu_t *)
|
|---|
| 679 | * @param event Position event
|
|---|
| 680 | */
|
|---|
| 681 | static void ui_menu_popup_pos(ui_popup_t *popup, void *arg, pos_event_t *event)
|
|---|
| 682 | {
|
|---|
| 683 | ui_menu_t *menu = (ui_menu_t *)arg;
|
|---|
| 684 | gfx_coord2_t spos;
|
|---|
| 685 |
|
|---|
| 686 | spos.x = 0;
|
|---|
| 687 | spos.y = 0;
|
|---|
| 688 | ui_menu_pos_event(menu, &spos, event);
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | /** @}
|
|---|
| 692 | */
|
|---|