Changeset b8b64a8 in mainline for uspace/lib/ui/src/menuentry.c


Ignore:
Timestamp:
2021-04-12T15:52:12Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6186f9f
Parents:
d65accb
Message:

Add column with keyboard shortcuts to menu

File:
1 edited

Legend:

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

    rd65accb rb8b64a8  
    5454        menu_entry_hpad = 4,
    5555        menu_entry_vpad = 4,
     56        menu_entry_column_pad = 8,
    5657        menu_entry_hpad_text = 1,
    57         menu_entry_vpad_text = 0
     58        menu_entry_vpad_text = 0,
     59        menu_entry_column_pad_text = 2
    5860};
    5961
     
    6264 * @param menu Menu
    6365 * @param caption Caption
     66 * @param shortcut Shotcut key(s) or empty string
    6467 * @param rmentry Place to store pointer to new menu entry
    6568 * @return EOK on success, ENOMEM if out of memory
    6669 */
    6770errno_t ui_menu_entry_create(ui_menu_t *menu, const char *caption,
    68     ui_menu_entry_t **rmentry)
     71    const char *shortcut, ui_menu_entry_t **rmentry)
    6972{
    7073        ui_menu_entry_t *mentry;
    71         gfx_coord_t width;
     74        gfx_coord_t caption_w;
     75        gfx_coord_t shortcut_w;
    7276
    7377        mentry = calloc(1, sizeof(ui_menu_entry_t));
     
    8185        }
    8286
     87        mentry->shortcut = str_dup(shortcut);
     88        if (mentry->caption == NULL) {
     89                free(mentry->caption);
     90                free(mentry);
     91                return ENOMEM;
     92        }
     93
    8394        mentry->menu = menu;
    8495        list_append(&mentry->lentries, &menu->entries);
    8596
    8697        /* Update accumulated menu entry dimensions */
    87         width = ui_menu_entry_width(mentry);
    88         if (width > menu->max_w)
    89                 menu->max_w = width;
     98        ui_menu_entry_column_widths(mentry, &caption_w, &shortcut_w);
     99        if (caption_w > menu->max_caption_w)
     100                menu->max_caption_w = caption_w;
     101        if (shortcut_w > menu->max_shortcut_w)
     102                menu->max_shortcut_w = shortcut_w;
    90103        menu->total_h += ui_menu_entry_height(mentry);
    91104
     
    156169 *
    157170 * @param mentry Menu entry
     171 * @param caption_w Place to store caption width
     172 * @param shortcut_w Place to store shortcut width
     173 */
     174void ui_menu_entry_column_widths(ui_menu_entry_t *mentry,
     175    gfx_coord_t *caption_w, gfx_coord_t *shortcut_w)
     176{
     177        ui_resource_t *res;
     178
     179        res = mentry->menu->mbar->res;
     180
     181        *caption_w = gfx_text_width(res->font, mentry->caption);
     182        *shortcut_w = gfx_text_width(res->font, mentry->shortcut);
     183}
     184
     185/** Compute width of menu entry.
     186 *
     187 * @param menu Menu
     188 * @param caption_w Widht of caption text
     189 * @param shortcut_w Width of shortcut text
    158190 * @return Width in pixels
    159191 */
    160 gfx_coord_t ui_menu_entry_width(ui_menu_entry_t *mentry)
     192gfx_coord_t ui_menu_entry_calc_width(ui_menu_t *menu, gfx_coord_t caption_w,
     193    gfx_coord_t shortcut_w)
    161194{
    162195        ui_resource_t *res;
    163196        gfx_coord_t hpad;
    164 
    165         res = mentry->menu->mbar->res;
    166 
    167         if (res->textmode) {
     197        gfx_coord_t width;
     198
     199        res = menu->mbar->res;
     200
     201        if (res->textmode)
    168202                hpad = menu_entry_hpad_text;
    169         } else {
     203        else
    170204                hpad = menu_entry_hpad;
    171         }
    172 
    173         return gfx_text_width(res->font, mentry->caption) + 2 * hpad;
     205
     206        width = caption_w + 2 * hpad;
     207
     208        if (shortcut_w != 0) {
     209                if (res->textmode)
     210                        width += menu_entry_column_pad_text;
     211                else
     212                        width += menu_entry_column_pad;
     213
     214                width += shortcut_w;
     215        }
     216
     217        return width;
    174218}
    175219
     
    210254        gfx_text_fmt_t fmt;
    211255        gfx_color_t *bg_color;
    212         const char *caption;
    213256        ui_menu_entry_geom_t geom;
    214257        errno_t rc;
     
    221264        fmt.halign = gfx_halign_left;
    222265        fmt.valign = gfx_valign_top;
    223 
    224         caption = mentry->caption;
    225266
    226267        if ((mentry->held && mentry->inside) ||
     
    241282                goto error;
    242283
    243         rc = gfx_puttext(res->font, &geom.text_pos, &fmt, caption);
     284        rc = gfx_puttext(res->font, &geom.caption_pos, &fmt, mentry->caption);
     285        if (rc != EOK)
     286                goto error;
     287
     288        fmt.halign = gfx_halign_right;
     289
     290        rc = gfx_puttext(res->font, &geom.shortcut_pos, &fmt, mentry->shortcut);
    244291        if (rc != EOK)
    245292                goto error;
     
    400447        }
    401448
    402         width = mentry->menu->max_w;
    403         geom->text_pos.x = pos->x + hpad;
    404         geom->text_pos.y = pos->y + vpad;
     449        /* Compute total width of menu entry */
     450        width = ui_menu_entry_calc_width(mentry->menu,
     451            mentry->menu->max_caption_w, mentry->menu->max_shortcut_w);
    405452
    406453        geom->outer_rect.p0 = *pos;
     
    408455        geom->outer_rect.p1.y = geom->outer_rect.p0.y +
    409456            ui_menu_entry_height(mentry);
     457
     458        geom->caption_pos.x = pos->x + hpad;
     459        geom->caption_pos.y = pos->y + vpad;
     460
     461        geom->shortcut_pos.x = geom->outer_rect.p1.x - hpad;
     462        geom->shortcut_pos.y = pos->y + vpad;
    410463}
    411464
Note: See TracChangeset for help on using the changeset viewer.