Changeset b8b64a8 in mainline for uspace/lib/ui/src/menuentry.c
- Timestamp:
- 2021-04-12T15:52:12Z (4 years ago)
- Branches:
- master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 6186f9f
- Parents:
- d65accb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/src/menuentry.c
rd65accb rb8b64a8 54 54 menu_entry_hpad = 4, 55 55 menu_entry_vpad = 4, 56 menu_entry_column_pad = 8, 56 57 menu_entry_hpad_text = 1, 57 menu_entry_vpad_text = 0 58 menu_entry_vpad_text = 0, 59 menu_entry_column_pad_text = 2 58 60 }; 59 61 … … 62 64 * @param menu Menu 63 65 * @param caption Caption 66 * @param shortcut Shotcut key(s) or empty string 64 67 * @param rmentry Place to store pointer to new menu entry 65 68 * @return EOK on success, ENOMEM if out of memory 66 69 */ 67 70 errno_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) 69 72 { 70 73 ui_menu_entry_t *mentry; 71 gfx_coord_t width; 74 gfx_coord_t caption_w; 75 gfx_coord_t shortcut_w; 72 76 73 77 mentry = calloc(1, sizeof(ui_menu_entry_t)); … … 81 85 } 82 86 87 mentry->shortcut = str_dup(shortcut); 88 if (mentry->caption == NULL) { 89 free(mentry->caption); 90 free(mentry); 91 return ENOMEM; 92 } 93 83 94 mentry->menu = menu; 84 95 list_append(&mentry->lentries, &menu->entries); 85 96 86 97 /* 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; 90 103 menu->total_h += ui_menu_entry_height(mentry); 91 104 … … 156 169 * 157 170 * @param mentry Menu entry 171 * @param caption_w Place to store caption width 172 * @param shortcut_w Place to store shortcut width 173 */ 174 void 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 158 190 * @return Width in pixels 159 191 */ 160 gfx_coord_t ui_menu_entry_width(ui_menu_entry_t *mentry) 192 gfx_coord_t ui_menu_entry_calc_width(ui_menu_t *menu, gfx_coord_t caption_w, 193 gfx_coord_t shortcut_w) 161 194 { 162 195 ui_resource_t *res; 163 196 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) 168 202 hpad = menu_entry_hpad_text; 169 } else {203 else 170 204 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; 174 218 } 175 219 … … 210 254 gfx_text_fmt_t fmt; 211 255 gfx_color_t *bg_color; 212 const char *caption;213 256 ui_menu_entry_geom_t geom; 214 257 errno_t rc; … … 221 264 fmt.halign = gfx_halign_left; 222 265 fmt.valign = gfx_valign_top; 223 224 caption = mentry->caption;225 266 226 267 if ((mentry->held && mentry->inside) || … … 241 282 goto error; 242 283 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); 244 291 if (rc != EOK) 245 292 goto error; … … 400 447 } 401 448 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); 405 452 406 453 geom->outer_rect.p0 = *pos; … … 408 455 geom->outer_rect.p1.y = geom->outer_rect.p0.y + 409 456 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; 410 463 } 411 464
Note:
See TracChangeset
for help on using the changeset viewer.