Changeset b8b64a8 in mainline
- 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
- Location:
- uspace
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/calculator/calculator.c
rd65accb rb8b64a8 899 899 } 900 900 901 rc = ui_menu_entry_create(mfile, "Exit", &mexit);901 rc = ui_menu_entry_create(mfile, "Exit", "Alt-F4", &mexit); 902 902 if (rc != EOK) { 903 903 printf("Error creating menu.\n"); … … 913 913 } 914 914 915 rc = ui_menu_entry_create(medit, "Copy", &mcopy);915 rc = ui_menu_entry_create(medit, "Copy", "Ctrl-C", &mcopy); 916 916 if (rc != EOK) { 917 917 printf("Error creating menu.\n"); … … 921 921 ui_menu_entry_set_cb(mcopy, calc_edit_copy, (void *) &calc); 922 922 923 rc = ui_menu_entry_create(medit, "Paste", &mpaste);923 rc = ui_menu_entry_create(medit, "Paste", "Ctrl-V", &mpaste); 924 924 if (rc != EOK) { 925 925 printf("Error creating menu.\n"); -
uspace/app/uidemo/uidemo.c
rd65accb rb8b64a8 269 269 } 270 270 271 rc = ui_menu_entry_create(demo.mfile, "Foo", &mfoo);272 if (rc != EOK) { 273 printf("Error creating menu.\n"); 274 return rc; 275 } 276 277 rc = ui_menu_entry_create(demo.mfile, "Bar", &mbar);278 if (rc != EOK) { 279 printf("Error creating menu.\n"); 280 return rc; 281 } 282 283 rc = ui_menu_entry_create(demo.mfile, "Foobar", &mfoobar);284 if (rc != EOK) { 285 printf("Error creating menu.\n"); 286 return rc; 287 } 288 289 rc = ui_menu_entry_create(demo.mfile, "Exit", &mexit);271 rc = ui_menu_entry_create(demo.mfile, "Foo", "Ctrl-Alt-Del", &mfoo); 272 if (rc != EOK) { 273 printf("Error creating menu.\n"); 274 return rc; 275 } 276 277 rc = ui_menu_entry_create(demo.mfile, "Bar", "", &mbar); 278 if (rc != EOK) { 279 printf("Error creating menu.\n"); 280 return rc; 281 } 282 283 rc = ui_menu_entry_create(demo.mfile, "Foobar", "", &mfoobar); 284 if (rc != EOK) { 285 printf("Error creating menu.\n"); 286 return rc; 287 } 288 289 rc = ui_menu_entry_create(demo.mfile, "Exit", "Alt-F4", &mexit); 290 290 if (rc != EOK) { 291 291 printf("Error creating menu.\n"); … … 313 313 } 314 314 315 rc = ui_menu_entry_create(demo.mhelp, "About", &mabout);315 rc = ui_menu_entry_create(demo.mhelp, "About", "Ctrl-H, F1", &mabout); 316 316 if (rc != EOK) { 317 317 printf("Error creating menu.\n"); -
uspace/lib/ui/include/ui/menuentry.h
rd65accb rb8b64a8 43 43 #include <types/ui/event.h> 44 44 45 extern errno_t ui_menu_entry_create(ui_menu_t *, const char *, 45 extern errno_t ui_menu_entry_create(ui_menu_t *, const char *, const char *, 46 46 ui_menu_entry_t **); 47 47 extern void ui_menu_entry_destroy(ui_menu_entry_t *); … … 50 50 extern ui_menu_entry_t *ui_menu_entry_first(ui_menu_t *); 51 51 extern ui_menu_entry_t *ui_menu_entry_next(ui_menu_entry_t *); 52 extern gfx_coord_t ui_menu_entry_width(ui_menu_entry_t *); 52 extern gfx_coord_t ui_menu_entry_calc_width(ui_menu_t *, 53 gfx_coord_t, gfx_coord_t); 54 extern void ui_menu_entry_column_widths(ui_menu_entry_t *, 55 gfx_coord_t *, gfx_coord_t *); 53 56 extern gfx_coord_t ui_menu_entry_height(ui_menu_entry_t *); 54 57 extern errno_t ui_menu_entry_paint(ui_menu_entry_t *, gfx_coord2_t *); -
uspace/lib/ui/private/menu.h
rd65accb rb8b64a8 58 58 /** Selected menu entry or @c NULL */ 59 59 struct ui_menu_entry *selected; 60 /** Maximum entry width */ 61 gfx_coord_t max_w; 60 /** Maximum caption width */ 61 gfx_coord_t max_caption_w; 62 /** Maximum shortcut width */ 63 gfx_coord_t max_shortcut_w; 62 64 /** Total entry height */ 63 65 gfx_coord_t total_h; -
uspace/lib/ui/private/menuentry.h
rd65accb rb8b64a8 61 61 /** Caption */ 62 62 char *caption; 63 /** Shortcut key(s) */ 64 char *shortcut; 63 65 }; 64 66 … … 70 72 /** Outer rectangle */ 71 73 gfx_rect_t outer_rect; 72 /** Text position */ 73 gfx_coord2_t text_pos; 74 /** Caption position */ 75 gfx_coord2_t caption_pos; 76 /** Shortcut position */ 77 gfx_coord2_t shortcut_pos; 74 78 } ui_menu_entry_geom_t; 75 79 -
uspace/lib/ui/src/menu.c
rd65accb rb8b64a8 175 175 } 176 176 177 edim.x = menu->max_w; 177 edim.x = ui_menu_entry_calc_width(menu, menu->max_caption_w, 178 menu->max_shortcut_w); 178 179 edim.y = menu->total_h; 179 180 -
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 -
uspace/lib/ui/test/menu.c
rd65accb rb8b64a8 205 205 PCUT_ASSERT_INT_EQUALS(0, rect.p0.x); 206 206 PCUT_ASSERT_INT_EQUALS(0, rect.p0.y); 207 PCUT_ASSERT_INT_EQUALS( 8, rect.p1.x);207 PCUT_ASSERT_INT_EQUALS(16, rect.p1.x); 208 208 PCUT_ASSERT_INT_EQUALS(8, rect.p1.y); 209 209 … … 420 420 PCUT_ASSERT_INT_EQUALS(0, geom.outer_rect.p0.x); 421 421 PCUT_ASSERT_INT_EQUALS(0, geom.outer_rect.p0.y); 422 PCUT_ASSERT_INT_EQUALS( 8, geom.outer_rect.p1.x);422 PCUT_ASSERT_INT_EQUALS(16, geom.outer_rect.p1.x); 423 423 PCUT_ASSERT_INT_EQUALS(8, geom.outer_rect.p1.y); 424 424 PCUT_ASSERT_INT_EQUALS(4, geom.entries_rect.p0.x); 425 425 PCUT_ASSERT_INT_EQUALS(4, geom.entries_rect.p0.y); 426 PCUT_ASSERT_INT_EQUALS( 4, geom.entries_rect.p1.x);426 PCUT_ASSERT_INT_EQUALS(12, geom.entries_rect.p1.x); 427 427 PCUT_ASSERT_INT_EQUALS(4, geom.entries_rect.p1.y); 428 428 -
uspace/lib/ui/test/menuentry.c
rd65accb rb8b64a8 99 99 PCUT_ASSERT_NOT_NULL(menu); 100 100 101 rc = ui_menu_entry_create(menu, "Foo", &mentry);101 rc = ui_menu_entry_create(menu, "Foo", "F1", &mentry); 102 102 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 103 103 PCUT_ASSERT_NOT_NULL(mentry); … … 144 144 PCUT_ASSERT_NOT_NULL(menu); 145 145 146 rc = ui_menu_entry_create(menu, "Foo", &entry1);146 rc = ui_menu_entry_create(menu, "Foo", "F1", &entry1); 147 147 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 148 148 PCUT_ASSERT_NOT_NULL(entry1); 149 149 150 rc = ui_menu_entry_create(menu, "Bar", &entry2);150 rc = ui_menu_entry_create(menu, "Bar", "F2", &entry2); 151 151 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 152 152 PCUT_ASSERT_NOT_NULL(entry2); … … 166 166 } 167 167 168 /** ui_menu_entry_width() / ui_menu_entry_height() */ 169 PCUT_TEST(width_height) 170 { 171 dummy_gc_t *dgc; 172 gfx_context_t *gc; 173 ui_resource_t *resource = NULL; 174 ui_menu_bar_t *mbar = NULL; 175 ui_menu_t *menu = NULL; 176 ui_menu_entry_t *mentry = NULL; 168 /** ui_menu_entry_widths() / ui_menu_entry_height() */ 169 PCUT_TEST(widths_height) 170 { 171 dummy_gc_t *dgc; 172 gfx_context_t *gc; 173 ui_resource_t *resource = NULL; 174 ui_menu_bar_t *mbar = NULL; 175 ui_menu_t *menu = NULL; 176 ui_menu_entry_t *mentry = NULL; 177 gfx_coord_t caption_w; 178 gfx_coord_t shortcut_w; 177 179 gfx_coord_t width; 178 180 gfx_coord_t height; … … 196 198 PCUT_ASSERT_NOT_NULL(menu); 197 199 198 rc = ui_menu_entry_create(menu, "X", &mentry); 199 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 200 PCUT_ASSERT_NOT_NULL(mentry); 201 202 width = ui_menu_entry_width(mentry); 203 PCUT_ASSERT_INT_EQUALS(11 + 8, width); 200 rc = ui_menu_entry_create(menu, "X", "Y", &mentry); 201 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 202 PCUT_ASSERT_NOT_NULL(mentry); 203 204 ui_menu_entry_column_widths(mentry, &caption_w, &shortcut_w); 205 PCUT_ASSERT_INT_EQUALS(11, caption_w); 206 PCUT_ASSERT_INT_EQUALS(10, shortcut_w); 207 208 width = ui_menu_entry_calc_width(menu, caption_w, shortcut_w); 209 PCUT_ASSERT_INT_EQUALS(4 + 11 + 8 + 10 + 4, width); 204 210 205 211 height = ui_menu_entry_height(mentry); … … 240 246 PCUT_ASSERT_NOT_NULL(menu); 241 247 242 rc = ui_menu_entry_create(menu, "Foo", &mentry);248 rc = ui_menu_entry_create(menu, "Foo", "F1", &mentry); 243 249 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 244 250 PCUT_ASSERT_NOT_NULL(mentry); … … 284 290 PCUT_ASSERT_NOT_NULL(menu); 285 291 286 rc = ui_menu_entry_create(menu, "X", &mentry);292 rc = ui_menu_entry_create(menu, "X", "Y", &mentry); 287 293 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 288 294 PCUT_ASSERT_NOT_NULL(mentry); … … 337 343 PCUT_ASSERT_NOT_NULL(menu); 338 344 339 rc = ui_menu_entry_create(menu, "X", &mentry);345 rc = ui_menu_entry_create(menu, "X", "Y", &mentry); 340 346 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 341 347 PCUT_ASSERT_NOT_NULL(mentry); … … 395 401 PCUT_ASSERT_NOT_NULL(menu); 396 402 397 rc = ui_menu_entry_create(menu, "X", &mentry);403 rc = ui_menu_entry_create(menu, "X", "Y", &mentry); 398 404 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 399 405 PCUT_ASSERT_NOT_NULL(mentry); … … 458 464 PCUT_ASSERT_NOT_NULL(menu); 459 465 460 rc = ui_menu_entry_create(menu, "X", &mentry);466 rc = ui_menu_entry_create(menu, "X", "Y", &mentry); 461 467 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 462 468 PCUT_ASSERT_NOT_NULL(mentry); … … 508 514 PCUT_ASSERT_NOT_NULL(menu); 509 515 510 rc = ui_menu_entry_create(menu, "X", &mentry);516 rc = ui_menu_entry_create(menu, "X", "Y", &mentry); 511 517 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 512 518 PCUT_ASSERT_NOT_NULL(mentry); … … 516 522 517 523 event.type = POS_PRESS; 518 event.hpos = 20;524 event.hpos = 40; 519 525 event.vpos = 20; 520 526 … … 558 564 PCUT_ASSERT_NOT_NULL(menu); 559 565 560 rc = ui_menu_entry_create(menu, "X", &mentry);566 rc = ui_menu_entry_create(menu, "X", "Y", &mentry); 561 567 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 562 568 PCUT_ASSERT_NOT_NULL(mentry); … … 569 575 570 576 event.type = POS_UPDATE; 571 event.hpos = 20;577 event.hpos = 40; 572 578 event.vpos = 20; 573 579 … … 611 617 PCUT_ASSERT_NOT_NULL(menu); 612 618 613 rc = ui_menu_entry_create(menu, "X", &mentry);619 rc = ui_menu_entry_create(menu, "X", "Y", &mentry); 614 620 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 615 621 PCUT_ASSERT_NOT_NULL(mentry);
Note:
See TracChangeset
for help on using the changeset viewer.