Changeset 1746ede in mainline
- Timestamp:
- 2021-04-09T22:41:22Z (3 years ago)
- Branches:
- master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 95a9cbc
- Parents:
- 7727cfd
- git-author:
- Jiri Svoboda <jiri@…> (2021-04-04 18:14:10)
- git-committer:
- jxsvoboda <5887334+jxsvoboda@…> (2021-04-09 22:41:22)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/calculator/calculator.c
r7727cfd r1746ede 37 37 */ 38 38 39 #include <clipboard.h> 39 40 #include <ctype.h> 40 41 #include <io/kbd_event.h> … … 45 46 #include <ui/entry.h> 46 47 #include <ui/fixed.h> 48 #include <ui/menubar.h> 49 #include <ui/menu.h> 50 #include <ui/menuentry.h> 47 51 #include <ui/pbutton.h> 48 52 #include <ui/ui.h> … … 97 101 /** Dimensions. Most of this should not be needed with auto layout */ 98 102 typedef struct { 103 gfx_rect_t menubar_rect; 99 104 gfx_rect_t entry_rect; 100 105 gfx_coord2_t btn_orig; … … 122 127 ui_pbutton_t *btn_8; 123 128 ui_pbutton_t *btn_9; 129 ui_menu_bar_t *menubar; 124 130 calc_geom_t geom; 125 131 } calc_t; 132 133 static void display_update(void); 134 135 static void calc_file_exit(ui_menu_entry_t *, void *); 136 static void calc_edit_copy(ui_menu_entry_t *, void *); 137 static void calc_edit_paste(ui_menu_entry_t *, void *); 126 138 127 139 static void calc_pb_clicked(ui_pbutton_t *, void *); … … 148 160 .kbd = wnd_kbd_event 149 161 }; 162 163 static char *expr = NULL; 164 static ui_entry_t *display; 150 165 151 166 /** Window close request … … 170 185 { 171 186 calc_t *calc = (calc_t *) arg; 187 188 if (event->type == KEY_PRESS && (event->mods & KM_CTRL) != 0) { 189 switch (event->key) { 190 case KC_C: 191 calc_edit_copy(NULL, calc); 192 break; 193 case KC_V: 194 calc_edit_paste(NULL, calc); 195 break; 196 default: 197 break; 198 } 199 } 172 200 173 201 switch (event->key) { … … 274 302 } 275 303 276 static char *expr = NULL; 277 static ui_entry_t *display; 304 /** File / Exit menu entry selected. 305 * 306 * @param mentry Menu entry 307 * @param arg Argument (calc_t *) 308 */ 309 static void calc_file_exit(ui_menu_entry_t *mentry, void *arg) 310 { 311 calc_t *calc = (calc_t *) arg; 312 313 ui_quit(calc->ui); 314 } 315 316 /** Edit / Copy menu entry selected. 317 * 318 * @param mentry Menu entry 319 * @param arg Argument (calc_t *) 320 */ 321 static void calc_edit_copy(ui_menu_entry_t *mentry, void *arg) 322 { 323 const char *str; 324 325 (void) arg; 326 str = (expr != NULL) ? expr : NULL_DISPLAY; 327 328 (void) clipboard_put_str(str); 329 } 330 331 /** Edit / Paste menu entry selected. 332 * 333 * @param mentry Menu entry 334 * @param arg Argument (calc_t *) 335 */ 336 static void calc_edit_paste(ui_menu_entry_t *mentry, void *arg) 337 { 338 char *str; 339 char *old; 340 char *cp; 341 errno_t rc; 342 343 (void) arg; 344 345 rc = clipboard_get_str(&str); 346 if (rc != EOK) 347 return; 348 349 /* Make sure string only contains allowed characters */ 350 cp = str; 351 while (*cp != '\0') { 352 if (!isdigit(*cp) && *cp != '+' && *cp != '-' && 353 *cp != '*' && *cp != '/') 354 return; 355 ++cp; 356 } 357 358 /* Update expression */ 359 old = expr; 360 expr = str; 361 free(old); 362 363 display_update(); 364 } 278 365 279 366 static bool is_digit(char c) … … 706 793 ui_wnd_params_t params; 707 794 ui_window_t *window; 795 ui_menu_t *mfile; 796 ui_menu_entry_t *mexit; 797 ui_menu_t *medit; 798 ui_menu_entry_t *mcopy; 799 ui_menu_entry_t *mpaste; 708 800 calc_t calc; 709 801 errno_t rc; … … 741 833 if (ui_is_textmode(ui)) { 742 834 params.rect.p1.x = 38; 743 params.rect.p1.y = 18; 744 835 params.rect.p1.y = 19; 836 837 calc.geom.menubar_rect.p0.x = 1; 838 calc.geom.menubar_rect.p0.y = 2; 839 calc.geom.menubar_rect.p1.x = params.rect.p1.x - 1; 840 calc.geom.menubar_rect.p1.y = 3; 745 841 calc.geom.entry_rect.p0.x = 4; 746 calc.geom.entry_rect.p0.y = 3;842 calc.geom.entry_rect.p0.y = 4; 747 843 calc.geom.entry_rect.p1.x = 34; 748 calc.geom.entry_rect.p1.y = 4;844 calc.geom.entry_rect.p1.y = 5; 749 845 calc.geom.btn_orig.x = 4; 750 calc.geom.btn_orig.y = 5;846 calc.geom.btn_orig.y = 6; 751 847 calc.geom.btn_dim.x = 6; 752 848 calc.geom.btn_dim.y = 2; … … 757 853 params.rect.p1.y = 270; 758 854 759 calc.geom.entry_rect.p0.x = 15; 760 calc.geom.entry_rect.p0.y = 45; 761 calc.geom.entry_rect.p1.x = 235; 762 calc.geom.entry_rect.p1.y = 70; 855 calc.geom.menubar_rect.p0.x = 4; 856 calc.geom.menubar_rect.p0.y = 30; 857 calc.geom.menubar_rect.p1.x = params.rect.p1.x - 4; 858 calc.geom.menubar_rect.p1.y = 52; 859 calc.geom.entry_rect.p0.x = 10; 860 calc.geom.entry_rect.p0.y = 45+6; 861 calc.geom.entry_rect.p1.x = 240; 862 calc.geom.entry_rect.p1.y = 70+6; 763 863 calc.geom.btn_orig.x = 10; 764 864 calc.geom.btn_orig.y = 90; … … 787 887 } 788 888 889 rc = ui_menu_bar_create(ui_res, &calc.menubar); 890 if (rc != EOK) { 891 printf("Error creating menu bar.\n"); 892 return rc; 893 } 894 895 rc = ui_menu_create(calc.menubar, "File", &mfile); 896 if (rc != EOK) { 897 printf("Error creating menu.\n"); 898 return rc; 899 } 900 901 rc = ui_menu_entry_create(mfile, "Exit", &mexit); 902 if (rc != EOK) { 903 printf("Error creating menu.\n"); 904 return rc; 905 } 906 907 ui_menu_entry_set_cb(mexit, calc_file_exit, (void *) &calc); 908 909 rc = ui_menu_create(calc.menubar, "Edit", &medit); 910 if (rc != EOK) { 911 printf("Error creating menu.\n"); 912 return rc; 913 } 914 915 rc = ui_menu_entry_create(medit, "Copy", &mcopy); 916 if (rc != EOK) { 917 printf("Error creating menu.\n"); 918 return rc; 919 } 920 921 ui_menu_entry_set_cb(mcopy, calc_edit_copy, (void *) &calc); 922 923 rc = ui_menu_entry_create(medit, "Paste", &mpaste); 924 if (rc != EOK) { 925 printf("Error creating menu.\n"); 926 return rc; 927 } 928 929 ui_menu_entry_set_cb(mpaste, calc_edit_paste, (void *) &calc); 930 931 ui_menu_bar_set_rect(calc.menubar, &calc.geom.menubar_rect); 932 933 rc = ui_fixed_add(fixed, ui_menu_bar_ctl(calc.menubar)); 934 if (rc != EOK) { 935 printf("Error adding control to layout.\n"); 936 return rc; 937 } 938 789 939 rc = ui_entry_create(ui_res, NULL_DISPLAY, &display); 790 940 if (rc != EOK) {
Note:
See TracChangeset
for help on using the changeset viewer.