Changeset 1eaead4 in mainline for uspace/lib/ui/src/paint.c
- Timestamp:
- 2023-02-07T16:11:53Z (2 years ago)
- Branches:
- master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 0366d09d
- Parents:
- 7c5320c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/src/paint.c
r7c5320c r1eaead4 1 1 /* 2 * Copyright (c) 202 2Jiri Svoboda2 * Copyright (c) 2023 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 632 632 } 633 633 634 /** Paint a text box. 634 /** Paint a custom text box. 635 * 636 * Paint a text box using user-provided box chars. 635 637 * 636 638 * @param resource UI resource 637 639 * @param rect Rectangle inside which to paint the box 638 640 * @param style Box style 641 * @param boxc Box characters 639 642 * @param color Color 640 643 * @return EOK on success or an error code 641 644 */ 642 errno_t ui_paint_text_box (ui_resource_t *resource, gfx_rect_t *rect,643 ui_box_ style_t style, gfx_color_t *color)645 errno_t ui_paint_text_box_custom(ui_resource_t *resource, gfx_rect_t *rect, 646 ui_box_chars_t *boxc, gfx_color_t *color) 644 647 { 645 648 errno_t rc; … … 653 656 gfx_coord_t y; 654 657 char *str = NULL; 655 ui_box_chars_t *boxc = NULL;656 658 657 659 gfx_rect_points_sort(rect, &srect); … … 661 663 if (dim.x < 2 || dim.y < 2) 662 664 return EOK; 665 666 gfx_text_fmt_init(&fmt); 667 fmt.font = resource->font; 668 fmt.color = color; 669 670 bufsz = str_size(boxc->c[0][0]) + 671 str_size(boxc->c[0][1]) * (dim.x - 2) + 672 str_size(boxc->c[0][2]) + 1; 673 674 str = malloc(bufsz); 675 if (str == NULL) 676 return ENOMEM; 677 678 /* Top edge and corners */ 679 680 str_cpy(str, bufsz, boxc->c[0][0]); 681 off = str_size(boxc->c[0][0]); 682 683 for (i = 1; i < dim.x - 1; i++) { 684 str_cpy(str + off, bufsz - off, boxc->c[0][1]); 685 off += str_size(boxc->c[0][1]); 686 } 687 688 str_cpy(str + off, bufsz - off, boxc->c[0][2]); 689 off += str_size(boxc->c[0][2]); 690 str[off] = '\0'; 691 692 pos = rect->p0; 693 rc = gfx_puttext(&pos, &fmt, str); 694 if (rc != EOK) 695 goto error; 696 697 /* Vertical edges */ 698 for (y = rect->p0.y + 1; y < rect->p1.y - 1; y++) { 699 pos.y = y; 700 701 pos.x = rect->p0.x; 702 rc = gfx_puttext(&pos, &fmt, boxc->c[1][0]); 703 if (rc != EOK) 704 goto error; 705 706 pos.x = rect->p1.x - 1; 707 rc = gfx_puttext(&pos, &fmt, boxc->c[1][2]); 708 if (rc != EOK) 709 goto error; 710 } 711 712 /* Bottom edge and corners */ 713 714 str_cpy(str, bufsz, boxc->c[2][0]); 715 off = str_size(boxc->c[2][0]); 716 717 for (i = 1; i < dim.x - 1; i++) { 718 str_cpy(str + off, bufsz - off, boxc->c[2][1]); 719 off += str_size(boxc->c[2][1]); 720 } 721 722 str_cpy(str + off, bufsz - off, boxc->c[2][2]); 723 off += str_size(boxc->c[2][2]); 724 str[off] = '\0'; 725 726 pos.x = rect->p0.x; 727 pos.y = rect->p1.y - 1; 728 rc = gfx_puttext(&pos, &fmt, str); 729 if (rc != EOK) 730 goto error; 731 732 free(str); 733 return EOK; 734 error: 735 if (str != NULL) 736 free(str); 737 return rc; 738 } 739 740 /** Paint a text box. 741 * 742 * Paint a text box with the specified style. 743 * 744 * @param resource UI resource 745 * @param rect Rectangle inside which to paint the box 746 * @param style Box style 747 * @param color Color 748 * @return EOK on success or an error code 749 */ 750 errno_t ui_paint_text_box(ui_resource_t *resource, gfx_rect_t *rect, 751 ui_box_style_t style, gfx_color_t *color) 752 { 753 ui_box_chars_t *boxc = NULL; 663 754 664 755 switch (style) { … … 674 765 return EINVAL; 675 766 676 gfx_text_fmt_init(&fmt); 677 fmt.font = resource->font; 678 fmt.color = color; 679 680 bufsz = str_size(boxc->c[0][0]) + 681 str_size(boxc->c[0][1]) * (dim.x - 2) + 682 str_size(boxc->c[0][2]) + 1; 683 684 str = malloc(bufsz); 685 if (str == NULL) 686 return ENOMEM; 687 688 /* Top edge and corners */ 689 690 str_cpy(str, bufsz, boxc->c[0][0]); 691 off = str_size(boxc->c[0][0]); 692 693 for (i = 1; i < dim.x - 1; i++) { 694 str_cpy(str + off, bufsz - off, boxc->c[0][1]); 695 off += str_size(boxc->c[0][1]); 696 } 697 698 str_cpy(str + off, bufsz - off, boxc->c[0][2]); 699 off += str_size(boxc->c[0][2]); 700 str[off] = '\0'; 701 702 pos = rect->p0; 703 rc = gfx_puttext(&pos, &fmt, str); 704 if (rc != EOK) 705 goto error; 706 707 /* Vertical edges */ 708 for (y = rect->p0.y + 1; y < rect->p1.y - 1; y++) { 709 pos.y = y; 710 711 pos.x = rect->p0.x; 712 rc = gfx_puttext(&pos, &fmt, boxc->c[1][0]); 713 if (rc != EOK) 714 goto error; 715 716 pos.x = rect->p1.x - 1; 717 rc = gfx_puttext(&pos, &fmt, boxc->c[1][2]); 718 if (rc != EOK) 719 goto error; 720 } 721 722 /* Bottom edge and corners */ 723 724 str_cpy(str, bufsz, boxc->c[2][0]); 725 off = str_size(boxc->c[2][0]); 726 727 for (i = 1; i < dim.x - 1; i++) { 728 str_cpy(str + off, bufsz - off, boxc->c[2][1]); 729 off += str_size(boxc->c[2][1]); 730 } 731 732 str_cpy(str + off, bufsz - off, boxc->c[2][2]); 733 off += str_size(boxc->c[2][2]); 734 str[off] = '\0'; 735 736 pos.x = rect->p0.x; 737 pos.y = rect->p1.y - 1; 738 rc = gfx_puttext(&pos, &fmt, str); 739 if (rc != EOK) 740 goto error; 741 742 free(str); 743 return EOK; 744 error: 745 if (str != NULL) 746 free(str); 747 return rc; 767 return ui_paint_text_box_custom(resource, rect, boxc, color); 748 768 } 749 769
Note:
See TracChangeset
for help on using the changeset viewer.