Ignore:
File:
1 edited

Legend:

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

    rbc52b5b r3c54869  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2023 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3232/**
    3333 * @file Push button
     34 *
     35 * Push button either uses text as decoration, or it can use a caller-provided
     36 * function to paint the decoration.
    3437 */
    3538
     
    5154enum {
    5255        ui_pb_press_dx = 1,
    53         ui_pb_press_dy = 1
     56        ui_pb_press_dy = 1,
     57        ui_pb_pad_x = 2,
     58        ui_pb_pad_x_text = 1
    5459};
    5560
     
    111116
    112117        ui_control_delete(pbutton->control);
     118        free(pbutton->caption);
    113119        free(pbutton);
    114120}
     
    136142}
    137143
     144/** Set push button decoration ops.
     145 *
     146 * @param pbutton Push button
     147 * @param ops Push button decoration callbacks
     148 * @param arg Decoration ops argument
     149 */
     150void ui_pbutton_set_decor_ops(ui_pbutton_t *pbutton,
     151    ui_pbutton_decor_ops_t *ops, void *arg)
     152{
     153        pbutton->decor_ops = ops;
     154        pbutton->decor_arg = arg;
     155}
     156
     157/** Set push button flag.s
     158 *
     159 * @param pbutton Push button
     160 * @param flags Flags
     161 */
     162void ui_pbutton_set_flags(ui_pbutton_t *pbutton, ui_pbutton_flags_t flags)
     163{
     164        pbutton->flags = flags;
     165}
     166
    138167/** Set button rectangle.
    139168 *
     
    157186{
    158187        pbutton->isdefault = isdefault;
     188}
     189
     190/** Get button light status.
     191 *
     192 * @param pbutton Button
     193 * @return @c true iff light is on
     194 */
     195bool ui_pbutton_get_light(ui_pbutton_t *pbutton)
     196{
     197        return pbutton->light;
     198}
     199
     200/** Turn button light on or off.
     201 *
     202 * @param pbutton Button
     203 * @param light @c true iff button should be lit
     204 */
     205void ui_pbutton_set_light(ui_pbutton_t *pbutton, bool light)
     206{
     207        pbutton->light = light;
     208}
     209
     210/** Set push button caption.
     211 *
     212 * @param pbutton Button
     213 * @param caption New caption
     214 * @return EOK on success, ENOMEM if out of memory
     215 */
     216errno_t ui_pbutton_set_caption(ui_pbutton_t *pbutton, const char *caption)
     217{
     218        char *dcaption;
     219
     220        dcaption = str_dup(caption);
     221        if (dcaption == NULL)
     222                return ENOMEM;
     223
     224        free(pbutton->caption);
     225        pbutton->caption = dcaption;
     226        return EOK;
    159227}
    160228
     
    277345        gfx_text_fmt_t fmt;
    278346        gfx_rect_t rect;
     347        gfx_rect_t irect;
    279348        gfx_coord_t thickness;
     349        gfx_color_t *color;
    280350        bool depressed;
    281351        errno_t rc;
     
    289359        rect.p1.y = pbutton->rect.p1.y - thickness;
    290360
    291         rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
     361        color = pbutton->light ? pbutton->res->btn_face_lit_color :
     362            pbutton->res->btn_face_color;
     363
     364        rc = gfx_set_color(pbutton->res->gc, color);
    292365        if (rc != EOK)
    293366                goto error;
     
    306379        }
    307380
    308         gfx_text_fmt_init(&fmt);
    309         fmt.color = pbutton->res->btn_text_color;
    310         fmt.halign = gfx_halign_center;
    311         fmt.valign = gfx_valign_center;
    312 
    313         rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
    314         if (rc != EOK)
    315                 goto error;
     381        if (pbutton->decor_ops != NULL && pbutton->decor_ops->paint != NULL) {
     382                /* Custom decoration */
     383                rc = pbutton->decor_ops->paint(pbutton, pbutton->decor_arg,
     384                    &pos);
     385                if (rc != EOK)
     386                        goto error;
     387        } else {
     388                /* Text decoration */
     389                ui_paint_get_inset_frame_inside(pbutton->res, &rect, &irect);
     390                gfx_text_fmt_init(&fmt);
     391                fmt.font = pbutton->res->font;
     392                fmt.color = pbutton->res->btn_text_color;
     393                fmt.halign = gfx_halign_center;
     394                fmt.valign = gfx_valign_center;
     395                fmt.abbreviate = true;
     396                fmt.width = irect.p1.x - irect.p0.x - 2 * ui_pb_pad_x;
     397
     398                rc = gfx_puttext(&pos, &fmt, pbutton->caption);
     399                if (rc != EOK)
     400                        goto error;
     401        }
    316402
    317403        rc = ui_pbutton_paint_frame(pbutton);
     
    351437        errno_t rc;
    352438
    353         depressed = pbutton->held && pbutton->inside;
     439        if ((pbutton->flags & ui_pbf_no_text_depress) == 0)
     440                depressed = pbutton->held && pbutton->inside;
     441        else
     442                depressed = false;
    354443
    355444        rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color);
     
    379468
    380469        gfx_text_fmt_init(&fmt);
     470        fmt.font = pbutton->res->font;
    381471        fmt.color = pbutton->res->btn_text_color;
    382472        fmt.halign = gfx_halign_center;
    383473        fmt.valign = gfx_valign_center;
    384 
    385         rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
     474        fmt.abbreviate = true;
     475        fmt.width = rect.p1.x - rect.p0.x - 2 * ui_pb_pad_x_text;
     476        if (fmt.width < 1)
     477                fmt.width = 1;
     478
     479        rc = gfx_puttext(&pos, &fmt, pbutton->caption);
    386480        if (rc != EOK)
    387481                goto error;
     
    427521        pbutton->held = true;
    428522        (void) ui_pbutton_paint(pbutton);
     523        ui_pbutton_down(pbutton);
    429524}
    430525
     
    439534
    440535        pbutton->held = false;
     536        ui_pbutton_up(pbutton);
    441537
    442538        if (pbutton->inside) {
     
    474570}
    475571
    476 /** Button was clicked.
     572/** Send button clicked event.
    477573 *
    478574 * @param pbutton Push button
     
    482578        if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
    483579                pbutton->cb->clicked(pbutton, pbutton->arg);
     580}
     581
     582/** Send button down event.
     583 *
     584 * @param pbutton Push button
     585 */
     586void ui_pbutton_down(ui_pbutton_t *pbutton)
     587{
     588        if (pbutton->cb != NULL && pbutton->cb->down != NULL)
     589                pbutton->cb->down(pbutton, pbutton->arg);
     590}
     591
     592/** Send button up event.
     593 *
     594 * @param pbutton Push button
     595 */
     596void ui_pbutton_up(ui_pbutton_t *pbutton)
     597{
     598        if (pbutton->cb != NULL && pbutton->cb->up != NULL)
     599                pbutton->cb->up(pbutton, pbutton->arg);
    484600}
    485601
     
    521637                }
    522638                break;
     639        case POS_DCLICK:
     640                break;
    523641        }
    524642
Note: See TracChangeset for help on using the changeset viewer.