Changeset d942ca4 in mainline
- Timestamp:
- 2020-11-10T09:46:07Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 66a2becf
- Parents:
- 03145ee
- git-author:
- Jiri Svoboda <jiri@…> (2020-11-09 20:45:53)
- git-committer:
- Jiri Svoboda <jiri@…> (2020-11-10 09:46:07)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/calculator/calculator.c
r03145ee rd942ca4 38 38 39 39 #include <ctype.h> 40 #include <io/kbd_event.h> 40 41 #include <stdbool.h> 41 42 #include <stdio.h> … … 96 97 typedef struct { 97 98 ui_t *ui; 99 ui_pbutton_t *btn_eval; 100 ui_pbutton_t *btn_clear; 101 ui_pbutton_t *btn_add; 102 ui_pbutton_t *btn_sub; 103 ui_pbutton_t *btn_mul; 104 ui_pbutton_t *btn_div; 105 ui_pbutton_t *btn_0; 106 ui_pbutton_t *btn_1; 107 ui_pbutton_t *btn_2; 108 ui_pbutton_t *btn_3; 109 ui_pbutton_t *btn_4; 110 ui_pbutton_t *btn_5; 111 ui_pbutton_t *btn_6; 112 ui_pbutton_t *btn_7; 113 ui_pbutton_t *btn_8; 114 ui_pbutton_t *btn_9; 98 115 } calc_t; 99 116 … … 115 132 116 133 static void wnd_close(ui_window_t *, void *); 134 static void wnd_kbd_event(ui_window_t *, void *, kbd_event_t *); 117 135 118 136 static ui_window_cb_t window_cb = { 119 .close = wnd_close 137 .close = wnd_close, 138 .kbd = wnd_kbd_event 120 139 }; 121 140 122 /** Window close button was clicked.141 /** Window close request 123 142 * 124 143 * @param window Window … … 130 149 131 150 ui_quit(calc->ui); 151 } 152 153 /** Window keyboard event 154 * 155 * @param window Window 156 * @param arg Argument (calc_t *) 157 * @param event Keyboard event 158 */ 159 static void wnd_kbd_event(ui_window_t *window, void *arg, kbd_event_t *event) 160 { 161 calc_t *calc = (calc_t *) arg; 162 163 switch (event->key) { 164 case KC_ENTER: 165 if (event->type == KEY_PRESS) 166 ui_pbutton_press(calc->btn_eval); 167 else 168 ui_pbutton_release(calc->btn_eval); 169 break; 170 case KC_BACKSPACE: 171 if (event->type == KEY_PRESS) 172 ui_pbutton_press(calc->btn_clear); 173 else 174 ui_pbutton_release(calc->btn_clear); 175 break; 176 case KC_MINUS: 177 if (event->type == KEY_PRESS) 178 ui_pbutton_press(calc->btn_sub); 179 else 180 ui_pbutton_release(calc->btn_sub); 181 break; 182 case KC_EQUALS: 183 if (event->type == KEY_PRESS) 184 ui_pbutton_press(calc->btn_add); 185 else 186 ui_pbutton_release(calc->btn_add); 187 break; 188 case KC_SLASH: 189 if (event->type == KEY_PRESS) 190 ui_pbutton_press(calc->btn_div); 191 else 192 ui_pbutton_release(calc->btn_div); 193 break; 194 case KC_0: 195 if (event->type == KEY_PRESS) 196 ui_pbutton_press(calc->btn_0); 197 else 198 ui_pbutton_release(calc->btn_0); 199 break; 200 case KC_1: 201 if (event->type == KEY_PRESS) 202 ui_pbutton_press(calc->btn_1); 203 else 204 ui_pbutton_release(calc->btn_1); 205 break; 206 case KC_2: 207 if (event->type == KEY_PRESS) 208 ui_pbutton_press(calc->btn_2); 209 else 210 ui_pbutton_release(calc->btn_2); 211 break; 212 case KC_3: 213 if (event->type == KEY_PRESS) 214 ui_pbutton_press(calc->btn_3); 215 else 216 ui_pbutton_release(calc->btn_3); 217 break; 218 case KC_4: 219 if (event->type == KEY_PRESS) 220 ui_pbutton_press(calc->btn_4); 221 else 222 ui_pbutton_release(calc->btn_4); 223 break; 224 case KC_5: 225 if (event->type == KEY_PRESS) 226 ui_pbutton_press(calc->btn_5); 227 else 228 ui_pbutton_release(calc->btn_5); 229 break; 230 case KC_6: 231 if (event->type == KEY_PRESS) 232 ui_pbutton_press(calc->btn_6); 233 else 234 ui_pbutton_release(calc->btn_6); 235 break; 236 case KC_7: 237 if (event->type == KEY_PRESS) 238 ui_pbutton_press(calc->btn_7); 239 else 240 ui_pbutton_release(calc->btn_7); 241 break; 242 case KC_8: 243 if ((event->mods & KM_SHIFT) != 0) { 244 if (event->type == KEY_PRESS) 245 ui_pbutton_press(calc->btn_mul); 246 else 247 ui_pbutton_release(calc->btn_mul); 248 } else { 249 if (event->type == KEY_PRESS) 250 ui_pbutton_press(calc->btn_8); 251 else 252 ui_pbutton_release(calc->btn_8); 253 } 254 break; 255 case KC_9: 256 if (event->type == KEY_PRESS) 257 ui_pbutton_press(calc->btn_9); 258 else 259 ui_pbutton_release(calc->btn_9); 260 break; 261 default: 262 break; 263 } 132 264 } 133 265 … … 564 696 ui_wnd_params_t params; 565 697 ui_window_t *window; 566 ui_pbutton_t *pb_eval;567 698 gfx_rect_t rect; 568 699 calc_t calc; … … 638 769 639 770 rc = calc_button_create(ui_res, fixed, 0, 0, "7", &calc_pbutton_cb, 640 (void *) "7", NULL);771 (void *) "7", &calc.btn_7); 641 772 if (rc != EOK) 642 773 return rc; 643 774 644 775 rc = calc_button_create(ui_res, fixed, 1, 0, "8", &calc_pbutton_cb, 645 (void *) "8", NULL);776 (void *) "8", &calc.btn_8); 646 777 if (rc != EOK) 647 778 return rc; 648 779 649 780 rc = calc_button_create(ui_res, fixed, 2, 0, "9", &calc_pbutton_cb, 650 (void *) "9", NULL);781 (void *) "9", &calc.btn_9); 651 782 if (rc != EOK) 652 783 return rc; 653 784 654 785 rc = calc_button_create(ui_res, fixed, 3, 0, "/", &calc_pbutton_cb, 655 (void *) "/", NULL);786 (void *) "/", &calc.btn_div); 656 787 if (rc != EOK) 657 788 return rc; 658 789 659 790 rc = calc_button_create(ui_res, fixed, 0, 1, "4", &calc_pbutton_cb, 660 (void *) "4", NULL);791 (void *) "4", &calc.btn_4); 661 792 if (rc != EOK) 662 793 return rc; 663 794 664 795 rc = calc_button_create(ui_res, fixed, 1, 1, "5", &calc_pbutton_cb, 665 (void *) "5", NULL);796 (void *) "5", &calc.btn_5); 666 797 if (rc != EOK) 667 798 return rc; 668 799 669 800 rc = calc_button_create(ui_res, fixed, 2, 1, "6", &calc_pbutton_cb, 670 (void *) "6", NULL);801 (void *) "6", &calc.btn_6); 671 802 if (rc != EOK) 672 803 return rc; 673 804 674 805 rc = calc_button_create(ui_res, fixed, 3, 1, "*", &calc_pbutton_cb, 675 (void *) "*", NULL);806 (void *) "*", &calc.btn_mul); 676 807 if (rc != EOK) 677 808 return rc; 678 809 679 810 rc = calc_button_create(ui_res, fixed, 0, 2, "1", &calc_pbutton_cb, 680 (void *) "1", NULL);811 (void *) "1", &calc.btn_1); 681 812 if (rc != EOK) 682 813 return rc; 683 814 684 815 rc = calc_button_create(ui_res, fixed, 1, 2, "2", &calc_pbutton_cb, 685 (void *) "2", NULL);816 (void *) "2", &calc.btn_2); 686 817 if (rc != EOK) 687 818 return rc; 688 819 689 820 rc = calc_button_create(ui_res, fixed, 2, 2, "3", &calc_pbutton_cb, 690 (void *) "3", NULL);821 (void *) "3", &calc.btn_3); 691 822 if (rc != EOK) 692 823 return rc; 693 824 694 825 rc = calc_button_create(ui_res, fixed, 3, 2, "-", &calc_pbutton_cb, 695 (void *) "-", NULL);826 (void *) "-", &calc.btn_sub); 696 827 if (rc != EOK) 697 828 return rc; 698 829 699 830 rc = calc_button_create(ui_res, fixed, 0, 3, "0", &calc_pbutton_cb, 700 (void *) "0", NULL);831 (void *) "0", &calc.btn_0); 701 832 if (rc != EOK) 702 833 return rc; 703 834 704 835 rc = calc_button_create(ui_res, fixed, 1, 3, "C", &calc_clear_cb, 705 (void *) "C", NULL);836 (void *) "C", &calc.btn_clear); 706 837 if (rc != EOK) 707 838 return rc; 708 839 709 840 rc = calc_button_create(ui_res, fixed, 2, 3, "=", &calc_eval_cb, 710 (void *) "=", & pb_eval);841 (void *) "=", &calc.btn_eval); 711 842 if (rc != EOK) 712 843 return rc; 713 844 714 845 rc = calc_button_create(ui_res, fixed, 3, 3, "+", &calc_pbutton_cb, 715 (void *) "+", NULL);716 if (rc != EOK) 717 return rc; 718 719 ui_pbutton_set_default( pb_eval, true);846 (void *) "+", &calc.btn_add); 847 if (rc != EOK) 848 return rc; 849 850 ui_pbutton_set_default(calc.btn_eval, true); 720 851 721 852 ui_window_add(window, ui_fixed_ctl(fixed));
Note:
See TracChangeset
for help on using the changeset viewer.