Changeset 0d1d0ea in mainline for uspace/lib/ui/src


Ignore:
Timestamp:
2022-03-31T14:37:31Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d68239a1
Parents:
7ca7215
git-author:
Jiri Svoboda <jiri@…> (2022-03-30 17:37:11)
git-committer:
Jiri Svoboda <jiri@…> (2022-03-31 14:37:31)
Message:

Routine to fill rectangle with character

Use it to simplify scrollbar rendering.

Location:
uspace/lib/ui/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/src/paint.c

    r7ca7215 r0d1d0ea  
    555555}
    556556
     557/** Fill rectangle with text character.
     558 *
     559 * @param resource UI resource
     560 * @param rect Rectangle
     561 * @param color Color
     562 * @param gchar Character to fill with
     563 * @return EOK on success or an error code
     564 */
     565errno_t ui_paint_text_rect(ui_resource_t *resource, gfx_rect_t *rect,
     566    gfx_color_t *color, const char *gchar)
     567{
     568        gfx_coord2_t pos;
     569        gfx_text_fmt_t fmt;
     570        gfx_rect_t srect;
     571        gfx_coord_t w, i;
     572        char *buf;
     573        size_t gcharsz;
     574        errno_t rc;
     575
     576        gfx_rect_points_sort(rect, &srect);
     577
     578        gfx_text_fmt_init(&fmt);
     579        fmt.font = resource->font;
     580        fmt.color = color;
     581        fmt.halign = gfx_halign_left;
     582        fmt.valign = gfx_valign_top;
     583
     584        w = srect.p1.x - srect.p0.x;
     585        if (w == 0)
     586                return EOK;
     587
     588        gcharsz = str_size(gchar);
     589
     590        buf = malloc(w * gcharsz + 1);
     591        if (buf == NULL)
     592                return ENOMEM;
     593
     594        for (i = 0; i < w; i++)
     595                str_cpy(buf + i * gcharsz, (w - i) * gcharsz + 1, gchar);
     596        buf[w * gcharsz] = '\0';
     597
     598        pos.x = srect.p0.x;
     599        for (pos.y = srect.p0.y; pos.y < srect.p1.y; pos.y++) {
     600                rc = gfx_puttext(&pos, &fmt, buf);
     601                if (rc != EOK)
     602                        goto error;
     603        }
     604
     605        free(buf);
     606        return EOK;
     607error:
     608        free(buf);
     609        return rc;
     610}
     611
    557612/** Initialize UI text formatting structure.
    558613 *
  • uspace/lib/ui/src/scrollbar.c

    r7ca7215 r0d1d0ea  
    526526}
    527527
    528 /** Paint horizontal scrollbar in text mode.
     528/** Paint scrollbar in text mode.
    529529 *
    530530 * @param scrollbar Scrollbar
    531531 * @return EOK on success or an error code
    532532 */
    533 errno_t ui_scrollbar_paint_text_horiz(ui_scrollbar_t *scrollbar)
     533errno_t ui_scrollbar_paint_text(ui_scrollbar_t *scrollbar)
    534534{
    535535        ui_resource_t *resource;
    536         gfx_coord2_t pos;
    537         gfx_text_fmt_t fmt;
    538         gfx_coord_t w, i;
    539         char *buf;
    540         const char *gchar;
    541         size_t gcharsz;
     536        ui_scrollbar_geom_t geom;
    542537        errno_t rc;
    543538
    544539        resource = ui_window_get_res(scrollbar->window);
     540        ui_scrollbar_get_geom(scrollbar, &geom);
    545541
    546542        /* Paint scrollbar through */
    547543
    548         pos = scrollbar->rect.p0;
    549         pos.x += ui_scrollbar_btn_len_text;
    550 
    551         gfx_text_fmt_init(&fmt);
    552         fmt.font = resource->font;
    553         fmt.color = resource->sbar_through_color;
    554         fmt.halign = gfx_halign_left;
    555         fmt.valign = gfx_valign_top;
    556 
    557         w = scrollbar->rect.p1.x - scrollbar->rect.p0.x -
    558             2 * ui_scrollbar_btn_len_text;
    559         assert(w >= 0);
    560         if (w < 0)
    561                 return EINVAL;
    562 
    563         gchar = "\u2592";
    564         gcharsz = str_size(gchar);
    565 
    566         buf = malloc(w * gcharsz + 1);
    567         if (buf == NULL)
    568                 return ENOMEM;
    569 
    570         for (i = 0; i < w; i++)
    571                 str_cpy(buf + i * gcharsz, (w - i) * gcharsz + 1, gchar);
    572         buf[w * gcharsz] = '\0';
    573 
    574         rc = gfx_puttext(&pos, &fmt, buf);
    575         free(buf);
     544        rc = ui_paint_text_rect(resource, &geom.through_rect,
     545            resource->sbar_through_color, "\u2592");
    576546        if (rc != EOK)
    577547                goto error;
     
    579549        /* Paint scrollbar thumb */
    580550
    581         pos.x += scrollbar->pos;
    582 
    583         gchar = "\u25a0";
    584         gcharsz = str_size(gchar);
    585         w = scrollbar->thumb_len;
    586 
    587         buf = malloc(w * gcharsz + 1);
    588         if (buf == NULL)
    589                 return ENOMEM;
    590 
    591         for (i = 0; i < w; i++)
    592                 str_cpy(buf + i * gcharsz, (w - i) * gcharsz + 1, gchar);
    593         buf[w * gcharsz] = '\0';
    594 
    595         rc = gfx_puttext(&pos, &fmt, buf);
    596         free(buf);
    597         if (rc != EOK)
    598                 goto error;
     551        rc = ui_paint_text_rect(resource, &geom.thumb_rect,
     552            resource->sbar_through_color, "\u25a0");
     553        if (rc != EOK)
     554                goto error;
     555
     556        /* Paint buttons */
    599557
    600558        rc = ui_pbutton_paint(scrollbar->up_btn);
     
    615573}
    616574
    617 /** Paint vertical scrollbar in text mode.
     575/** Paint scrollbar.
    618576 *
    619577 * @param scrollbar Scrollbar
    620578 * @return EOK on success or an error code
    621579 */
    622 errno_t ui_scrollbar_paint_text_vert(ui_scrollbar_t *scrollbar)
    623 {
    624         ui_resource_t *resource;
    625         ui_scrollbar_geom_t geom;
    626         gfx_coord2_t pos;
    627         gfx_text_fmt_t fmt;
    628         errno_t rc;
    629 
    630         resource = ui_window_get_res(scrollbar->window);
    631         ui_scrollbar_get_geom(scrollbar, &geom);
    632 
    633         /* Paint scrollbar through */
    634 
    635         gfx_text_fmt_init(&fmt);
    636         fmt.font = resource->font;
    637         fmt.color = resource->sbar_through_color;
    638         fmt.halign = gfx_halign_left;
    639         fmt.valign = gfx_valign_top;
    640 
    641         pos.x = scrollbar->rect.p0.x;
    642         for (pos.y = geom.through_rect.p0.y; pos.y < geom.through_rect.p1.y;
    643             pos.y++) {
    644                 rc = gfx_puttext(&pos, &fmt, "\u2592");
    645                 if (rc != EOK)
    646                         goto error;
    647         }
    648 
    649         /* Paint scrollbar thumb */
    650 
    651         pos.x = geom.thumb_rect.p0.x;
    652         for (pos.y = geom.thumb_rect.p0.y; pos.y < geom.thumb_rect.p1.y;
    653             pos.y++) {
    654                 rc = gfx_puttext(&pos, &fmt, "\u25a0");
    655                 if (rc != EOK)
    656                         goto error;
    657         }
    658 
    659         rc = ui_pbutton_paint(scrollbar->up_btn);
    660         if (rc != EOK)
    661                 goto error;
    662 
    663         rc = ui_pbutton_paint(scrollbar->down_btn);
    664         if (rc != EOK)
    665                 goto error;
    666 
    667         rc = gfx_update(resource->gc);
    668         if (rc != EOK)
    669                 goto error;
    670 
    671         return EOK;
    672 error:
    673         return rc;
    674 }
    675 
    676 /** Paint scrollbar.
    677  *
    678  * @param scrollbar Scrollbar
    679  * @return EOK on success or an error code
    680  */
    681580errno_t ui_scrollbar_paint(ui_scrollbar_t *scrollbar)
    682581{
     
    684583
    685584        if (resource->textmode) {
    686                 if (scrollbar->dir == ui_sbd_horiz)
    687                         return ui_scrollbar_paint_text_horiz(scrollbar);
    688                 else
    689                         return ui_scrollbar_paint_text_vert(scrollbar);
     585                return ui_scrollbar_paint_text(scrollbar);
    690586        } else {
    691587                return ui_scrollbar_paint_gfx(scrollbar);
Note: See TracChangeset for help on using the changeset viewer.