Changeset 86fff971 in mainline


Ignore:
Timestamp:
2022-04-04T18:49:30Z (2 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fd05ea6
Parents:
d68239a1
Message:

'X' does not mark the spot

Stop misusing 'X' character as a cross mark, create a routine for
drawing a cross and use it as a custom decoration for the close button.
Also use it for checkbox cross.

Location:
uspace/lib/ui
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/include/ui/paint.h

    rd68239a1 r86fff971  
    6363extern errno_t ui_paint_right_triangle(gfx_context_t *, gfx_coord2_t *,
    6464    gfx_coord_t);
     65extern errno_t ui_paint_cross(gfx_context_t *, gfx_coord2_t *, gfx_coord_t,
     66    gfx_coord_t, gfx_coord_t);
    6567extern errno_t ui_paint_text_box(ui_resource_t *, gfx_rect_t *,
    6668    ui_box_style_t, gfx_color_t *);
  • uspace/lib/ui/src/checkbox.c

    rd68239a1 r86fff971  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2022 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    5252        checkbox_box_h = 16,
    5353        checkbox_label_margin = 8,
     54        checkbox_cross_n = 5,
     55        checkbox_cross_w = 2,
     56        checkbox_cross_h = 2
    5457};
    5558
     
    188191
    189192        if (checkbox->checked) {
    190                 box_center.x = (box_inside.p0.x + box_inside.p1.x) / 2;
    191                 box_center.y = (box_inside.p0.y + box_inside.p1.y) / 2;
    192 
    193                 gfx_text_fmt_init(&fmt);
    194                 fmt.font = checkbox->res->font;
    195                 fmt.color = checkbox->res->entry_fg_color;
    196                 fmt.halign = gfx_halign_center;
    197                 fmt.valign = gfx_valign_center;
    198 
    199                 rc = gfx_puttext(&box_center, &fmt, "X");
     193                rc = gfx_set_color(checkbox->res->gc,
     194                    checkbox->res->entry_fg_color);
     195                if (rc != EOK)
     196                        goto error;
     197
     198                box_center.x = (box_inside.p0.x + box_inside.p1.x) / 2 - 1;
     199                box_center.y = (box_inside.p0.y + box_inside.p1.y) / 2 - 1;
     200
     201                rc = ui_paint_cross(checkbox->res->gc, &box_center,
     202                    checkbox_cross_n, checkbox_cross_w, checkbox_cross_h);
    200203                if (rc != EOK)
    201204                        goto error;
  • uspace/lib/ui/src/paint.c

    rd68239a1 r86fff971  
    365365 * @param pos Center position
    366366 * @param n Length of triangle side
     367 * @return EOK on success or an error code
    367368 */
    368369errno_t ui_paint_up_triangle(gfx_context_t *gc, gfx_coord2_t *pos,
     
    391392 * @param pos Center position
    392393 * @param n Length of triangle side
     394 * @return EOK on success or an error code
    393395 */
    394396errno_t ui_paint_down_triangle(gfx_context_t *gc, gfx_coord2_t *pos,
     
    417419 * @param pos Center position
    418420 * @param n Length of triangle side
     421 * @return EOK on success or an error code
    419422 */
    420423errno_t ui_paint_left_triangle(gfx_context_t *gc, gfx_coord2_t *pos,
     
    443446 * @param pos Center position
    444447 * @param n Length of triangle side
     448 * @return EOK on success or an error code
    445449 */
    446450errno_t ui_paint_right_triangle(gfx_context_t *gc, gfx_coord2_t *pos,
     
    456460                rect.p1.x = pos->x + n / 2 - i + 1;
    457461                rect.p1.y = pos->y + i + 1;
     462                rc = gfx_fill_rect(gc, &rect);
     463                if (rc != EOK)
     464                        return rc;
     465        }
     466
     467        return EOK;
     468}
     469
     470/** Paint diagonal cross (X).
     471 *
     472 * @param gc Graphic context
     473 * @param pos Center position
     474 * @param n Length of each leg
     475 * @param w Pen width
     476 * @param h Pen height
     477 * @return EOK on success or an error code
     478 */
     479errno_t ui_paint_cross(gfx_context_t *gc, gfx_coord2_t *pos,
     480    gfx_coord_t n, gfx_coord_t w, gfx_coord_t h)
     481{
     482        gfx_coord_t i;
     483        gfx_rect_t rect;
     484        errno_t rc;
     485
     486        rect.p0.x = pos->x;
     487        rect.p0.y = pos->y;
     488        rect.p1.x = pos->x + w;
     489        rect.p1.y = pos->y + h;
     490        rc = gfx_fill_rect(gc, &rect);
     491        if (rc != EOK)
     492                return rc;
     493
     494        for (i = 1; i < n; i++) {
     495                rect.p0.x = pos->x - i;
     496                rect.p0.y = pos->y - i;
     497                rect.p1.x = pos->x - i + w;
     498                rect.p1.y = pos->y - i + h;
     499                rc = gfx_fill_rect(gc, &rect);
     500                if (rc != EOK)
     501                        return rc;
     502
     503                rect.p0.x = pos->x - i;
     504                rect.p0.y = pos->y + i;
     505                rect.p1.x = pos->x - i + w;
     506                rect.p1.y = pos->y + i + h;
     507                rc = gfx_fill_rect(gc, &rect);
     508                if (rc != EOK)
     509                        return rc;
     510
     511                rect.p0.x = pos->x + i;
     512                rect.p0.y = pos->y - i;
     513                rect.p1.x = pos->x + i + w;
     514                rect.p1.y = pos->y - i + h;
     515                rc = gfx_fill_rect(gc, &rect);
     516                if (rc != EOK)
     517                        return rc;
     518
     519                rect.p0.x = pos->x + i;
     520                rect.p0.y = pos->y + i;
     521                rect.p1.x = pos->x + i + w;
     522                rect.p1.y = pos->y + i + h;
    458523                rc = gfx_fill_rect(gc, &rect);
    459524                if (rc != EOK)
  • uspace/lib/ui/src/wdecor.c

    rd68239a1 r86fff971  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2022 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    5050
    5151static void ui_wdecor_btn_clicked(ui_pbutton_t *, void *);
     52static errno_t ui_wdecor_btn_close_paint(ui_pbutton_t *, void *,
     53    gfx_coord2_t *);
    5254
    5355static ui_pbutton_cb_t ui_wdecor_btn_close_cb = {
    5456        .clicked = ui_wdecor_btn_clicked
     57};
     58
     59static ui_pbutton_decor_ops_t ui_wdecor_btn_close_decor_ops = {
     60        .paint = ui_wdecor_btn_close_paint
    5561};
    5662
     
    6268        wdecor_tbar_h = 22,
    6369        wdecor_frame_w = 4,
    64         wdecor_frame_w_text = 1
     70        wdecor_frame_w_text = 1,
     71        wdecor_close_cross_n = 5,
     72        wdecor_close_cross_w = 2,
     73        wdecor_close_cross_h = 1
    6574};
    6675
     
    98107        ui_pbutton_set_cb(wdecor->btn_close, &ui_wdecor_btn_close_cb,
    99108            (void *)wdecor);
     109
     110        ui_pbutton_set_decor_ops(wdecor->btn_close,
     111            &ui_wdecor_btn_close_decor_ops, (void *)wdecor);
    100112
    101113        wdecor->res = resource;
     
    647659}
    648660
     661/** Paint close button decoration.
     662 *
     663 * @param pbutton Push button
     664 * @param arg Argument (ui_wdecor_t *)
     665 * @param pos Center position
     666 */
     667static errno_t ui_wdecor_btn_close_paint(ui_pbutton_t *pbutton,
     668    void *arg, gfx_coord2_t *pos)
     669{
     670        ui_wdecor_t *wdecor = (ui_wdecor_t *)arg;
     671        gfx_coord2_t p;
     672        errno_t rc;
     673
     674        rc = gfx_set_color(wdecor->res->gc, wdecor->res->btn_text_color);
     675        if (rc != EOK)
     676                return rc;
     677
     678        p.x = pos->x - 1;
     679        p.y = pos->y - 1;
     680        return ui_paint_cross(wdecor->res->gc, &p, wdecor_close_cross_n,
     681            wdecor_close_cross_w, wdecor_close_cross_h);
     682}
     683
    649684/** @}
    650685 */
  • uspace/lib/ui/test/paint.c

    rd68239a1 r86fff971  
    332332}
    333333
     334/** Paint diagonal cross (X) */
     335PCUT_TEST(cross)
     336{
     337        errno_t rc;
     338        gfx_context_t *gc = NULL;
     339        test_gc_t tgc;
     340        gfx_coord2_t center;
     341
     342        memset(&tgc, 0, sizeof(tgc));
     343        rc = gfx_context_new(&ops, &tgc, &gc);
     344        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     345
     346        center.x = 0;
     347        center.y = 0;
     348
     349        rc = ui_paint_cross(gc, &center, 5, 1, 2);
     350        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     351
     352        rc = gfx_context_delete(gc);
     353        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     354}
     355
    334356/** Paint text box */
    335357PCUT_TEST(text_box)
Note: See TracChangeset for help on using the changeset viewer.