Changeset 46bd63c9 in mainline for uspace/lib/ui/src/menu.c
- Timestamp:
- 2023-09-08T08:47:30Z (13 months ago)
- Branches:
- master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1af103e
- Parents:
- 0b6fad9
- git-author:
- Jiri Svoboda <jiri@…> (2023-09-07 18:47:09)
- git-committer:
- Jiri Svoboda <jiri@…> (2023-09-08 08:47:30)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/src/menu.c
r0b6fad9 r46bd63c9 44 44 #include <str.h> 45 45 #include <uchar.h> 46 #include <ui/ui.h> 46 47 #include <ui/accel.h> 47 48 #include <ui/control.h> … … 49 50 #include <ui/popup.h> 50 51 #include <ui/menu.h> 51 #include <ui/menubar.h>52 52 #include <ui/menuentry.h> 53 53 #include <ui/resource.h> 54 54 #include <ui/window.h> 55 #include "../private/menubar.h"56 55 #include "../private/menu.h" 57 56 #include "../private/resource.h" … … 78 77 /** Create new menu. 79 78 * 79 * @param parent Parent window 80 80 * @param mbar Menu bar 81 * @param caption Caption82 81 * @param rmenu Place to store pointer to new menu 83 82 * @return EOK on success, ENOMEM if out of memory 84 83 */ 85 errno_t ui_menu_create(ui_menu_bar_t *mbar, const char *caption, 86 ui_menu_t **rmenu) 84 errno_t ui_menu_create(ui_window_t *parent, ui_menu_t **rmenu) 87 85 { 88 86 ui_menu_t *menu; … … 92 90 return ENOMEM; 93 91 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); 92 menu->parent = parent; 102 93 list_initialize(&menu->entries); 103 94 … … 124 115 } 125 116 126 list_remove(&menu->lmenus);127 117 free(menu->caption); 128 118 free(menu); 129 119 } 130 120 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; 121 /** Set menu callbacks. 122 * 123 * @param menu Menu 124 * @param cb Callbacks 125 * @param arg Callback argument 126 */ 127 void ui_menu_set_cb(ui_menu_t *menu, ui_menu_cb_t *cb, void *arg) 128 { 129 menu->cb = cb; 130 menu->arg = arg; 203 131 } 204 132 … … 212 140 ui_menu_geom_t *geom) 213 141 { 214 ui_resource_t *res;215 142 gfx_coord2_t edim; 216 143 gfx_coord_t frame_w; 217 144 gfx_coord_t frame_h; 218 219 res = ui_window_get_res(menu->mbar->window); 145 ui_resource_t *res; 146 147 res = ui_window_get_res(menu->parent); 220 148 221 149 if (res->textmode) { … … 241 169 } 242 170 243 /** Get menu rectangle.244 *245 * @param menu Menu246 * @param spos Starting position (top-left corner)247 * @param rect Place to store menu rectangle248 */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 Menu260 * @return Accelerator character (lowercase) or the null character if261 * 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 171 /** Get UI resource from menu. 269 172 * … … 304 207 params.idev_id = idev_id; 305 208 306 rc = ui_popup_create( menu->mbar->ui, menu->mbar->window, ¶ms,307 &p opup);209 rc = ui_popup_create(ui_window_get_ui(menu->parent), menu->parent, 210 ¶ms, &popup); 308 211 if (rc != EOK) 309 212 return rc; … … 503 406 /* Press outside menu - close it */ 504 407 if (event->type == POS_PRESS) 505 ui_menu_ bar_deactivate(menu->mbar);408 ui_menu_close_req(menu); 506 409 } 507 410 … … 524 427 if (event->type == KEY_PRESS && (event->mods & KM_ALT) != 0 && 525 428 (event->mods & (KM_CTRL | KM_SHIFT)) == 0 && event->c != '\0') 526 ui_menu_ bar_press_accel(menu->mbar, event->c, event->kbd_id);429 ui_menu_press_accel(menu, event->c, event->kbd_id); 527 430 528 431 return ui_claimed; … … 615 518 switch (event->key) { 616 519 case KC_ESCAPE: 617 ui_menu_ bar_deactivate(menu->mbar);520 ui_menu_close_req(menu); 618 521 break; 619 522 case KC_LEFT: 620 ui_menu_ bar_left(menu->mbar, event->kbd_id);523 ui_menu_left(menu, event->kbd_id); 621 524 break; 622 525 case KC_RIGHT: 623 ui_menu_ bar_right(menu->mbar, event->kbd_id);526 ui_menu_right(menu, event->kbd_id); 624 527 break; 625 528 case KC_UP: … … 658 561 ui_menu_t *menu = (ui_menu_t *)arg; 659 562 660 /* Deactivate menu bar, close menu*/661 ui_menu_ bar_deactivate(menu->mbar);563 /* Forward close request to caller */ 564 ui_menu_close_req(menu); 662 565 } 663 566 … … 691 594 } 692 595 596 /** Send menu left event. 597 * 598 * @param menu Menu 599 * @param idev_id Input device ID 600 */ 601 void ui_menu_left(ui_menu_t *menu, sysarg_t idev_id) 602 { 603 if (menu->cb != NULL && menu->cb->left != NULL) 604 menu->cb->left(menu, menu->arg, idev_id); 605 } 606 607 /** Send menu right event. 608 * 609 * @param menu Menu 610 * @param idev_id Input device ID 611 */ 612 void ui_menu_right(ui_menu_t *menu, sysarg_t idev_id) 613 { 614 if (menu->cb != NULL && menu->cb->right != NULL) 615 menu->cb->right(menu, menu->arg, idev_id); 616 } 617 618 /** Send menu close request event. 619 * 620 * @param menu Menu 621 */ 622 void ui_menu_close_req(ui_menu_t *menu) 623 { 624 if (menu->cb != NULL && menu->cb->close_req != NULL) 625 menu->cb->close_req(menu, menu->arg); 626 } 627 628 /** Send menu accelerator key press event. 629 * 630 * @param menu Menu 631 * @param c Character 632 * @param kbd_id Keyboard ID 633 */ 634 void ui_menu_press_accel(ui_menu_t *menu, char32_t c, sysarg_t kbd_id) 635 { 636 if (menu->cb != NULL && menu->cb->press_accel != NULL) 637 menu->cb->press_accel(menu, menu->arg, c, kbd_id); 638 } 639 693 640 /** @} 694 641 */
Note:
See TracChangeset
for help on using the changeset viewer.