Changeset b41564c in mainline
- Timestamp:
- 2020-11-09T18:21:05Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 6dca10f
- Parents:
- 2109961
- Location:
- uspace
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/vcalc/meson.build
r2109961 rb41564c 27 27 # 28 28 29 deps = [ ' gui' ]29 deps = [ 'ui' ] 30 30 src = files('vcalc.c') -
uspace/app/vcalc/vcalc.c
r2109961 rb41564c 1 1 /* 2 * Copyright (c) 2020 Jiri Svoboda 2 3 * Copyright (c) 2016 Martin Decky 3 4 * All rights reserved. … … 36 37 */ 37 38 39 #include <ctype.h> 38 40 #include <stdbool.h> 39 41 #include <stdio.h> 40 42 #include <stdlib.h> 41 #include <io/pixel.h>42 #include <task.h>43 #include <window.h>44 #include <grid.h>45 #include <button.h>46 #include <label.h>47 #include <ctype.h>48 #include <stdlib.h>49 43 #include <str.h> 44 #include <ui/label.h> 45 #include <ui/fixed.h> 46 #include <ui/pbutton.h> 47 #include <ui/ui.h> 48 #include <ui/window.h> 50 49 51 50 #define NAME "vcalc" 52 51 53 #define NULL_DISPLAY "." 54 55 #define SYNTAX_ERROR_DISPLAY "syntax error" 56 #define NUMERIC_ERROR_DISPLAY "numerical error" 57 #define UNKNOWN_ERROR_DISPLAY "unknown error" 52 #define NULL_DISPLAY "0" 53 54 #define SYNTAX_ERROR_DISPLAY "Syntax error" 55 #define NUMERIC_ERROR_DISPLAY "Numerical error" 56 #define UNKNOWN_ERROR_DISPLAY "Unknown error" 57 58 #define EXPR_MAX_LEN 22 58 59 59 60 typedef enum { … … 93 94 } stack_item_t; 94 95 96 typedef struct { 97 ui_t *ui; 98 } calc_t; 99 100 static void calc_pb_clicked(ui_pbutton_t *, void *); 101 static void calc_eval_clicked(ui_pbutton_t *, void *); 102 static void calc_clear_clicked(ui_pbutton_t *, void *); 103 104 static ui_pbutton_cb_t calc_pbutton_cb = { 105 .clicked = calc_pb_clicked 106 }; 107 108 static ui_pbutton_cb_t calc_clear_cb = { 109 .clicked = calc_clear_clicked 110 }; 111 112 static ui_pbutton_cb_t calc_eval_cb = { 113 .clicked = calc_eval_clicked 114 }; 115 116 static void wnd_close(ui_window_t *, void *); 117 118 static ui_window_cb_t window_cb = { 119 .close = wnd_close 120 }; 121 122 /** Window close button was clicked. 123 * 124 * @param window Window 125 * @param arg Argument (calc_t *) 126 */ 127 static void wnd_close(ui_window_t *window, void *arg) 128 { 129 calc_t *calc = (calc_t *) arg; 130 131 ui_quit(calc->ui); 132 } 133 95 134 static char *expr = NULL; 96 static label_t *display;135 static ui_label_t *display; 97 136 98 137 static bool is_digit(char c) … … 311 350 { 312 351 if (expr != NULL) 313 display->rewrite(&display->widget, (void *) expr);352 (void) ui_label_set_text(display, (void *) expr); 314 353 else 315 display->rewrite(&display->widget, (void *) NULL_DISPLAY); 354 (void) ui_label_set_text(display, (void *) NULL_DISPLAY); 355 356 ui_label_paint(display); 316 357 } 317 358 … … 325 366 switch (error_type) { 326 367 case ERROR_SYNTAX: 327 display->rewrite(&display->widget, (void *) SYNTAX_ERROR_DISPLAY); 368 (void) ui_label_set_text(display, 369 (void *) SYNTAX_ERROR_DISPLAY); 328 370 break; 329 371 case ERROR_NUMERIC: 330 display->rewrite(&display->widget, (void *) NUMERIC_ERROR_DISPLAY); 372 (void) ui_label_set_text(display, 373 (void *) NUMERIC_ERROR_DISPLAY); 331 374 break; 332 375 default: 333 display->rewrite(&display->widget, (void *) UNKNOWN_ERROR_DISPLAY); 334 } 335 } 336 337 static void on_btn_click(widget_t *widget, void *data) 338 { 339 const char *subexpr = (const char *) widget_get_data(widget); 376 (void) ui_label_set_text(display, 377 (void *) UNKNOWN_ERROR_DISPLAY); 378 break; 379 } 380 381 ui_label_paint(display); 382 } 383 384 static void calc_pb_clicked(ui_pbutton_t *pbutton, void *arg) 385 { 386 const char *subexpr = (const char *) arg; 340 387 341 388 if (expr != NULL) { 342 389 char *new_expr; 343 390 344 asprintf(&new_expr, "%s%s", expr, subexpr); 345 free(expr); 346 expr = new_expr; 347 } else 391 if (str_length(expr) < EXPR_MAX_LEN) { 392 asprintf(&new_expr, "%s%s", expr, subexpr); 393 free(expr); 394 expr = new_expr; 395 } 396 } else { 348 397 expr = str_dup(subexpr); 398 } 349 399 350 400 display_update(); 351 401 } 352 402 353 static void on_c_click(widget_t *widget, void *data)403 static void calc_clear_clicked(ui_pbutton_t *pbutton, void *arg) 354 404 { 355 405 if (expr != NULL) { … … 361 411 } 362 412 363 static void on_eval_click(widget_t *widget, void *data)413 static void calc_eval_clicked(ui_pbutton_t *pbutton, void *arg) 364 414 { 365 415 if (expr == NULL) … … 468 518 } 469 519 520 static errno_t calc_button_create(ui_resource_t *ui_res, ui_fixed_t *fixed, 521 int x, int y, const char *text, ui_pbutton_cb_t *cb, void *arg, 522 ui_pbutton_t **rbutton) 523 { 524 ui_pbutton_t *pb; 525 gfx_rect_t rect; 526 errno_t rc; 527 528 rc = ui_pbutton_create(ui_res, text, &pb); 529 if (rc != EOK) { 530 printf("Error creating button.\n"); 531 return rc; 532 } 533 534 ui_pbutton_set_cb(pb, cb, arg); 535 536 rect.p0.x = 10 + 60 * x; 537 rect.p0.y = 90 + 45 * y; 538 rect.p1.x = 60 + 60 * x; 539 rect.p1.y = 125 + 45 * y; 540 ui_pbutton_set_rect(pb, &rect); 541 542 rc = ui_fixed_add(fixed, ui_pbutton_ctl(pb)); 543 if (rc != EOK) { 544 printf("Error adding control to layout.\n"); 545 return rc; 546 } 547 548 if (rbutton != NULL) 549 *rbutton = pb; 550 return EOK; 551 } 552 470 553 static void print_syntax(void) 471 554 { 472 printf("Syntax: %s [-d <display >]\n", NAME);555 printf("Syntax: %s [-d <display-spec>]\n", NAME); 473 556 } 474 557 475 558 int main(int argc, char *argv[]) 476 559 { 477 const char *display_svc = DISPLAY_DEFAULT; 560 const char *display_spec = UI_DISPLAY_DEFAULT; 561 ui_t *ui; 562 ui_resource_t *ui_res; 563 ui_fixed_t *fixed; 564 ui_wnd_params_t params; 565 ui_window_t *window; 566 ui_pbutton_t *pb_eval; 567 gfx_rect_t rect; 568 calc_t calc; 569 errno_t rc; 478 570 int i; 479 571 … … 488 580 } 489 581 490 display_s vc = argv[i++];582 display_spec = argv[i++]; 491 583 } else { 492 584 printf("Invalid option '%s'.\n", argv[i]); … … 496 588 } 497 589 498 window_t *main_window = window_open(display_svc, NULL, 499 WINDOW_MAIN | WINDOW_DECORATED | WINDOW_RESIZEABLE, NAME); 500 if (!main_window) { 501 printf("%s: Cannot open main window.\n", NAME); 502 return 2; 503 } 504 505 pixel_t grd_bg = PIXEL(255, 240, 240, 240); 506 507 pixel_t btn_bg = PIXEL(255, 0, 0, 0); 508 pixel_t btn_fg = PIXEL(200, 200, 200, 200); 509 510 pixel_t lbl_bg = PIXEL(255, 240, 240, 240); 511 pixel_t lbl_fg = PIXEL(255, 0, 0, 0); 512 513 grid_t *grid = create_grid(window_root(main_window), NULL, 4, 5, grd_bg); 514 515 display = create_label(NULL, NULL, NULL_DISPLAY, 16, lbl_bg, lbl_fg); 516 517 button_t *btn_1 = create_button(NULL, "1", "1", 16, btn_bg, btn_fg, 518 lbl_fg); 519 button_t *btn_2 = create_button(NULL, "2", "2", 16, btn_bg, btn_fg, 520 lbl_fg); 521 button_t *btn_3 = create_button(NULL, "3", "3", 16, btn_bg, btn_fg, 522 lbl_fg); 523 button_t *btn_4 = create_button(NULL, "4", "4", 16, btn_bg, btn_fg, 524 lbl_fg); 525 button_t *btn_5 = create_button(NULL, "5", "5", 16, btn_bg, btn_fg, 526 lbl_fg); 527 button_t *btn_6 = create_button(NULL, "6", "6", 16, btn_bg, btn_fg, 528 lbl_fg); 529 button_t *btn_7 = create_button(NULL, "7", "7", 16, btn_bg, btn_fg, 530 lbl_fg); 531 button_t *btn_8 = create_button(NULL, "8", "8", 16, btn_bg, btn_fg, 532 lbl_fg); 533 button_t *btn_9 = create_button(NULL, "9", "9", 16, btn_bg, btn_fg, 534 lbl_fg); 535 button_t *btn_0 = create_button(NULL, "0", "0", 16, btn_bg, btn_fg, 536 lbl_fg); 537 538 button_t *btn_add = create_button(NULL, "+", "+", 16, btn_bg, btn_fg, 539 lbl_fg); 540 button_t *btn_sub = create_button(NULL, "-", "-", 16, btn_bg, btn_fg, 541 lbl_fg); 542 button_t *btn_mul = create_button(NULL, "*", "*", 16, btn_bg, btn_fg, 543 lbl_fg); 544 button_t *btn_div = create_button(NULL, "/", "/", 16, btn_bg, btn_fg, 545 lbl_fg); 546 547 button_t *btn_eval = create_button(NULL, NULL, "=", 16, btn_bg, btn_fg, 548 lbl_fg); 549 button_t *btn_c = create_button(NULL, NULL, "C", 16, btn_bg, btn_fg, 550 lbl_fg); 551 552 if ((!grid) || (!display) || (!btn_1) || (!btn_2) || (!btn_3) || 553 (!btn_4) || (!btn_5) || (!btn_6) || (!btn_7) || (!btn_8) || 554 (!btn_9) || (!btn_0) || (!btn_add) || (!btn_sub) || (!btn_mul) || 555 (!btn_div) || (!btn_eval) || (!btn_c)) { 556 window_close(main_window); 557 printf("%s: Cannot create widgets.\n", NAME); 558 return 3; 559 } 560 561 sig_connect(&btn_1->clicked, &btn_1->widget, on_btn_click); 562 sig_connect(&btn_2->clicked, &btn_2->widget, on_btn_click); 563 sig_connect(&btn_3->clicked, &btn_3->widget, on_btn_click); 564 sig_connect(&btn_4->clicked, &btn_4->widget, on_btn_click); 565 sig_connect(&btn_5->clicked, &btn_5->widget, on_btn_click); 566 sig_connect(&btn_6->clicked, &btn_6->widget, on_btn_click); 567 sig_connect(&btn_7->clicked, &btn_7->widget, on_btn_click); 568 sig_connect(&btn_8->clicked, &btn_8->widget, on_btn_click); 569 sig_connect(&btn_9->clicked, &btn_9->widget, on_btn_click); 570 sig_connect(&btn_0->clicked, &btn_0->widget, on_btn_click); 571 572 sig_connect(&btn_add->clicked, &btn_add->widget, on_btn_click); 573 sig_connect(&btn_sub->clicked, &btn_sub->widget, on_btn_click); 574 sig_connect(&btn_div->clicked, &btn_div->widget, on_btn_click); 575 sig_connect(&btn_mul->clicked, &btn_mul->widget, on_btn_click); 576 577 sig_connect(&btn_eval->clicked, &btn_eval->widget, on_eval_click); 578 sig_connect(&btn_c->clicked, &btn_c->widget, on_c_click); 579 580 grid->add(grid, &display->widget, 0, 0, 4, 1); 581 582 grid->add(grid, &btn_1->widget, 0, 1, 1, 1); 583 grid->add(grid, &btn_2->widget, 1, 1, 1, 1); 584 grid->add(grid, &btn_3->widget, 2, 1, 1, 1); 585 grid->add(grid, &btn_add->widget, 3, 1, 1, 1); 586 587 grid->add(grid, &btn_4->widget, 0, 2, 1, 1); 588 grid->add(grid, &btn_5->widget, 1, 2, 1, 1); 589 grid->add(grid, &btn_6->widget, 2, 2, 1, 1); 590 grid->add(grid, &btn_sub->widget, 3, 2, 1, 1); 591 592 grid->add(grid, &btn_7->widget, 0, 3, 1, 1); 593 grid->add(grid, &btn_8->widget, 1, 3, 1, 1); 594 grid->add(grid, &btn_9->widget, 2, 3, 1, 1); 595 grid->add(grid, &btn_mul->widget, 3, 3, 1, 1); 596 597 grid->add(grid, &btn_c->widget, 0, 4, 1, 1); 598 grid->add(grid, &btn_0->widget, 1, 4, 1, 1); 599 grid->add(grid, &btn_eval->widget, 2, 4, 1, 1); 600 grid->add(grid, &btn_div->widget, 3, 4, 1, 1); 601 602 window_resize(main_window, 0, 0, 400, 400, WINDOW_PLACEMENT_ANY); 603 window_exec(main_window); 604 605 task_retval(0); 606 async_manager(); 590 rc = ui_create(display_spec, &ui); 591 if (rc != EOK) { 592 printf("Error creating UI on display %s.\n", display_spec); 593 return rc; 594 } 595 596 ui_wnd_params_init(¶ms); 597 params.caption = "Calculator"; 598 params.rect.p0.x = 0; 599 params.rect.p0.y = 0; 600 params.rect.p1.x = 250; 601 params.rect.p1.y = 270; 602 603 rc = ui_window_create(ui, ¶ms, &window); 604 if (rc != EOK) { 605 printf("Error creating window.\n"); 606 return rc; 607 } 608 609 ui_window_set_cb(window, &window_cb, (void *) &calc); 610 calc.ui = ui; 611 612 ui_res = ui_window_get_res(window); 613 614 rc = ui_fixed_create(&fixed); 615 if (rc != EOK) { 616 printf("Error creating fixed layout.\n"); 617 return rc; 618 } 619 620 rc = ui_label_create(ui_res, NULL_DISPLAY, &display); 621 if (rc != EOK) { 622 printf("Error creating label.\n"); 623 return rc; 624 } 625 626 rect.p0.x = 15; 627 rect.p0.y = 50; 628 rect.p1.x = 235; 629 rect.p1.y = 70; 630 ui_label_set_rect(display, &rect); 631 ui_label_set_halign(display, gfx_halign_right); 632 633 rc = ui_fixed_add(fixed, ui_label_ctl(display)); 634 if (rc != EOK) { 635 printf("Error adding control to layout.\n"); 636 return rc; 637 } 638 639 rc = calc_button_create(ui_res, fixed, 0, 0, "7", &calc_pbutton_cb, 640 (void *) "7", NULL); 641 if (rc != EOK) 642 return rc; 643 644 rc = calc_button_create(ui_res, fixed, 1, 0, "8", &calc_pbutton_cb, 645 (void *) "8", NULL); 646 if (rc != EOK) 647 return rc; 648 649 rc = calc_button_create(ui_res, fixed, 2, 0, "9", &calc_pbutton_cb, 650 (void *) "9", NULL); 651 if (rc != EOK) 652 return rc; 653 654 rc = calc_button_create(ui_res, fixed, 3, 0, "/", &calc_pbutton_cb, 655 (void *) "/", NULL); 656 if (rc != EOK) 657 return rc; 658 659 rc = calc_button_create(ui_res, fixed, 0, 1, "4", &calc_pbutton_cb, 660 (void *) "4", NULL); 661 if (rc != EOK) 662 return rc; 663 664 rc = calc_button_create(ui_res, fixed, 1, 1, "5", &calc_pbutton_cb, 665 (void *) "5", NULL); 666 if (rc != EOK) 667 return rc; 668 669 rc = calc_button_create(ui_res, fixed, 2, 1, "6", &calc_pbutton_cb, 670 (void *) "6", NULL); 671 if (rc != EOK) 672 return rc; 673 674 rc = calc_button_create(ui_res, fixed, 3, 1, "*", &calc_pbutton_cb, 675 (void *) "*", NULL); 676 if (rc != EOK) 677 return rc; 678 679 rc = calc_button_create(ui_res, fixed, 0, 2, "1", &calc_pbutton_cb, 680 (void *) "1", NULL); 681 if (rc != EOK) 682 return rc; 683 684 rc = calc_button_create(ui_res, fixed, 1, 2, "2", &calc_pbutton_cb, 685 (void *) "2", NULL); 686 if (rc != EOK) 687 return rc; 688 689 rc = calc_button_create(ui_res, fixed, 2, 2, "3", &calc_pbutton_cb, 690 (void *) "3", NULL); 691 if (rc != EOK) 692 return rc; 693 694 rc = calc_button_create(ui_res, fixed, 3, 2, "-", &calc_pbutton_cb, 695 (void *) "-", NULL); 696 if (rc != EOK) 697 return rc; 698 699 rc = calc_button_create(ui_res, fixed, 0, 3, "0", &calc_pbutton_cb, 700 (void *) "0", NULL); 701 if (rc != EOK) 702 return rc; 703 704 rc = calc_button_create(ui_res, fixed, 1, 3, "C", &calc_clear_cb, 705 (void *) "C", NULL); 706 if (rc != EOK) 707 return rc; 708 709 rc = calc_button_create(ui_res, fixed, 2, 3, "=", &calc_eval_cb, 710 (void *) "=", &pb_eval); 711 if (rc != EOK) 712 return rc; 713 714 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); 720 721 ui_window_add(window, ui_fixed_ctl(fixed)); 722 723 rc = ui_window_paint(window); 724 if (rc != EOK) { 725 printf("Error painting window.\n"); 726 return rc; 727 } 728 729 ui_run(ui); 730 ui_window_destroy(window); 731 ui_destroy(ui); 607 732 608 733 return 0; -
uspace/lib/ui/src/label.c
r2109961 rb41564c 186 186 break; 187 187 case gfx_halign_right: 188 pos. y= label->rect.p1.x;188 pos.x = label->rect.p1.x; 189 189 break; 190 190 }
Note:
See TracChangeset
for help on using the changeset viewer.