Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/src/menuentry.c

    rc68c18b9 ree3b28a9  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2024 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4343#include <stdlib.h>
    4444#include <str.h>
     45#include <ui/accel.h>
    4546#include <ui/control.h>
    4647#include <ui/paint.h>
     48#include <ui/menubar.h>
    4749#include <ui/menuentry.h>
    4850#include <ui/window.h>
     
    143145                return;
    144146
     147        mentry->menu->total_h -= ui_menu_entry_height(mentry);
     148        /* NOTE: max_caption_w/max_shortcut_w not updated (speed) */
     149
    145150        list_remove(&mentry->lentries);
     151
     152        /*
     153         * If we emptied the menu, reset accumulated dims so they
     154         * can be correctly calculated when (if) the menu is
     155         * re-populated.
     156         */
     157        if (list_empty(&mentry->menu->entries)) {
     158                mentry->menu->total_h = 0;
     159                mentry->menu->max_caption_w = 0;
     160                mentry->menu->max_shortcut_w = 0;
     161        }
     162
    146163        free(mentry->caption);
    147164        free(mentry);
     
    159176        mentry->cb = cb;
    160177        mentry->arg = arg;
     178}
     179
     180/** Set menu entry disabled flag.
     181 *
     182 * @param mentry Menu entry
     183 * @param disabled @c true iff entry is to be disabled, @c false otherwise
     184 */
     185void ui_menu_entry_set_disabled(ui_menu_entry_t *mentry, bool disabled)
     186{
     187        mentry->disabled = disabled;
     188}
     189
     190/** Get menu entry disabled flag.
     191 *
     192 * @param mentry Menu entry
     193 * @return disabled @c true iff entry is disabled, @c false otherwise
     194 */
     195bool ui_menu_entry_is_disabled(ui_menu_entry_t *mentry)
     196{
     197        return mentry->disabled;
    161198}
    162199
     
    177214}
    178215
     216/** Get last menu entry in menu.
     217 *
     218 * @param menu Menu
     219 * @return Last menu entry or @c NULL if there is none
     220 */
     221ui_menu_entry_t *ui_menu_entry_last(ui_menu_t *menu)
     222{
     223        link_t *link;
     224
     225        link = list_last(&menu->entries);
     226        if (link == NULL)
     227                return NULL;
     228
     229        return list_get_instance(link, ui_menu_entry_t, lentries);
     230}
     231
    179232/** Get next menu entry in menu.
    180233 *
     
    193246}
    194247
     248/** Get previous menu entry in menu.
     249 *
     250 * @param cur Current menu entry
     251 * @return Next menu entry or @c NULL if @a cur is the last one
     252 */
     253ui_menu_entry_t *ui_menu_entry_prev(ui_menu_entry_t *cur)
     254{
     255        link_t *link;
     256
     257        link = list_prev(&cur->lentries, &cur->menu->entries);
     258        if (link == NULL)
     259                return NULL;
     260
     261        return list_get_instance(link, ui_menu_entry_t, lentries);
     262}
     263
    195264/** Get width of menu entry.
    196265 *
     
    207276         * This needs to work even if the menu is not open, so we cannot
    208277         * use the menu's resource, which is only created after the menu
    209          * is open (and its window is created). Use the menu bar's
     278         * is open (and its window is created). Use the parent window's
    210279         * resource instead.
    211280         */
    212         res = ui_window_get_res(mentry->menu->mbar->window);
    213 
    214         *caption_w = gfx_text_width(res->font, mentry->caption);
    215         *shortcut_w = gfx_text_width(res->font, mentry->shortcut);
     281        res = ui_window_get_res(mentry->menu->parent);
     282
     283        *caption_w = ui_text_width(res->font, mentry->caption);
     284        *shortcut_w = ui_text_width(res->font, mentry->shortcut);
    216285}
    217286
     
    233302         * This needs to work even if the menu is not open, so we cannot
    234303         * use the menu's resource, which is only created after the menu
    235          * is open (and its window is created). Use the menu bar's
     304         * is open (and its window is created). Use the parent window's
    236305         * resource instead.
    237306         */
    238         res = ui_window_get_res(menu->mbar->window);
     307        res = ui_window_get_res(menu->parent);
    239308
    240309        if (res->textmode)
     
    272341         * This needs to work even if the menu is not open, so we cannot
    273342         * use the menu's resource, which is only created after the menu
    274          * is open (and its window is created). Use the menu bar's
     343         * is open (and its window is created). Use the parent window's
    275344         * resource instead.
    276345         */
    277         res = ui_window_get_res(mentry->menu->mbar->window);
     346        res = ui_window_get_res(mentry->menu->parent);
    278347
    279348        if (res->textmode) {
     
    298367}
    299368
     369/** Get menu entry accelerator character.
     370 *
     371 * @param mentry Menu entry
     372 * @return Accelerator character (lowercase) or the null character if
     373 *         the menu entry has no accelerator.
     374 */
     375char32_t ui_menu_entry_get_accel(ui_menu_entry_t *mentry)
     376{
     377        return ui_accel_get(mentry->caption);
     378}
     379
    300380/** Paint menu entry.
    301381 *
     
    307387{
    308388        ui_resource_t *res;
    309         gfx_text_fmt_t fmt;
     389        ui_text_fmt_t fmt;
    310390        gfx_color_t *bg_color;
    311391        ui_menu_entry_geom_t geom;
     
    317397        ui_menu_entry_get_geom(mentry, pos, &geom);
    318398
    319         gfx_text_fmt_init(&fmt);
     399        ui_text_fmt_init(&fmt);
     400        fmt.font = res->font;
    320401        fmt.halign = gfx_halign_left;
    321402        fmt.valign = gfx_valign_top;
     
    324405            mentry == mentry->menu->selected) {
    325406                fmt.color = res->wnd_sel_text_color;
     407                fmt.hgl_color = res->wnd_sel_text_hgl_color;
    326408                bg_color = res->wnd_sel_text_bg_color;
     409        } else if (mentry->disabled) {
     410                fmt.color = res->wnd_dis_text_color;
     411                fmt.hgl_color = res->wnd_dis_text_color;
     412                bg_color = res->wnd_face_color;
    327413        } else {
    328414                fmt.color = res->wnd_text_color;
     415                fmt.hgl_color = res->wnd_text_hgl_color;
    329416                bg_color = res->wnd_face_color;
    330417        }
     
    338425                goto error;
    339426
    340         rc = gfx_puttext(res->font, &geom.caption_pos, &fmt, mentry->caption);
     427        rc = ui_paint_text(&geom.caption_pos, &fmt, mentry->caption);
    341428        if (rc != EOK)
    342429                goto error;
     
    344431        fmt.halign = gfx_halign_right;
    345432
    346         rc = gfx_puttext(res->font, &geom.shortcut_pos, &fmt, mentry->shortcut);
     433        rc = ui_paint_text(&geom.shortcut_pos, &fmt, mentry->shortcut);
    347434        if (rc != EOK)
    348435                goto error;
    349436
    350437        if (mentry->separator) {
    351                 rect.p0 = geom.caption_pos;
    352                 rect.p1.x = geom.shortcut_pos.x;
    353                 rect.p1.y = rect.p0.y + 2;
    354                 rc = ui_paint_bevel(res->gc, &rect, res->wnd_shadow_color,
    355                     res->wnd_highlight_color, 1, NULL);
    356                 if (rc != EOK)
    357                         goto error;
     438                if (res->textmode) {
     439                        rect = geom.outer_rect;
     440                        rect.p0.x -= 1;
     441                        rect.p1.x += 1;
     442
     443                        rc = ui_paint_text_hbrace(res, &rect, ui_box_single,
     444                            res->wnd_face_color);
     445                        if (rc != EOK)
     446                                goto error;
     447                } else {
     448                        rect.p0 = geom.caption_pos;
     449                        rect.p1.x = geom.shortcut_pos.x;
     450                        rect.p1.y = rect.p0.y + 2;
     451                        rc = ui_paint_bevel(res->gc, &rect, res->wnd_shadow_color,
     452                            res->wnd_highlight_color, 1, NULL);
     453                        if (rc != EOK)
     454                                goto error;
     455                }
    358456        }
    359457
     
    367465}
    368466
     467/** Determine if entry is selectable.
     468 *
     469 * @return @c true iff entry is selectable
     470 */
     471bool ui_menu_entry_selectable(ui_menu_entry_t *mentry)
     472{
     473        return !mentry->separator;
     474}
     475
    369476/** Handle button press in menu entry.
    370477 *
     
    377484                return;
    378485
    379         if (mentry->separator)
     486        if (mentry->separator || mentry->disabled)
    380487                return;
    381488
     
    396503        mentry->held = false;
    397504
    398         if (mentry->inside) {
    399                 /* Close menu */
    400                 ui_menu_bar_select(mentry->menu->mbar, NULL, NULL);
    401 
    402                 /* Call back */
    403                 ui_menu_entry_cb(mentry);
    404         }
     505        if (mentry->inside)
     506                ui_menu_entry_activate(mentry);
     507}
     508
     509/** Activate menu entry.
     510 *
     511 * @param mentry Menu entry
     512 */
     513void ui_menu_entry_activate(ui_menu_entry_t *mentry)
     514{
     515        /* Close menu */
     516        ui_menu_close_req(mentry->menu);
     517
     518        /* Call back */
     519        ui_menu_entry_cb(mentry);
    405520}
    406521
     
    486601                }
    487602                break;
     603        case POS_DCLICK:
     604                break;
    488605        }
    489606
Note: See TracChangeset for help on using the changeset viewer.