Ignore:
File:
1 edited

Legend:

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

    rbc52b5b rb769ca0  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2026 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 ops.
     145 *
     146 * These allow overriding the function for painting the button or
     147 * painting the button decoration.
     148 *
     149 * @param pbutton Push button
     150 * @param ops Push button ops
     151 * @param arg Decoration ops argument
     152 */
     153void ui_pbutton_set_ops(ui_pbutton_t *pbutton, ui_pbutton_ops_t *ops, void *arg)
     154{
     155        pbutton->ops = ops;
     156        pbutton->ops_arg = arg;
     157}
     158
     159/** Set push button flag.s
     160 *
     161 * @param pbutton Push button
     162 * @param flags Flags
     163 */
     164void ui_pbutton_set_flags(ui_pbutton_t *pbutton, ui_pbutton_flags_t flags)
     165{
     166        pbutton->flags = flags;
     167}
     168
    138169/** Set button rectangle.
    139170 *
     
    157188{
    158189        pbutton->isdefault = isdefault;
     190}
     191
     192/** Get button light status.
     193 *
     194 * @param pbutton Button
     195 * @return @c true iff light is on
     196 */
     197bool ui_pbutton_get_light(ui_pbutton_t *pbutton)
     198{
     199        return pbutton->light;
     200}
     201
     202/** Turn button light on or off.
     203 *
     204 * @param pbutton Button
     205 * @param light @c true iff button should be lit
     206 */
     207void ui_pbutton_set_light(ui_pbutton_t *pbutton, bool light)
     208{
     209        pbutton->light = light;
     210}
     211
     212/** Set push button caption.
     213 *
     214 * @param pbutton Button
     215 * @param caption New caption
     216 * @return EOK on success, ENOMEM if out of memory
     217 */
     218errno_t ui_pbutton_set_caption(ui_pbutton_t *pbutton, const char *caption)
     219{
     220        char *dcaption;
     221
     222        dcaption = str_dup(caption);
     223        if (dcaption == NULL)
     224                return ENOMEM;
     225
     226        free(pbutton->caption);
     227        pbutton->caption = dcaption;
     228        return EOK;
    159229}
    160230
     
    277347        gfx_text_fmt_t fmt;
    278348        gfx_rect_t rect;
     349        gfx_rect_t irect;
    279350        gfx_coord_t thickness;
     351        gfx_color_t *color;
    280352        bool depressed;
    281353        errno_t rc;
     
    289361        rect.p1.y = pbutton->rect.p1.y - thickness;
    290362
    291         rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
     363        color = pbutton->light ? pbutton->res->btn_face_lit_color :
     364            pbutton->res->btn_face_color;
     365
     366        rc = gfx_set_color(pbutton->res->gc, color);
    292367        if (rc != EOK)
    293368                goto error;
     
    306381        }
    307382
    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;
     383        if (pbutton->ops != NULL && pbutton->ops->decor_paint != NULL) {
     384                /* Custom decoration */
     385                rc = pbutton->ops->decor_paint(pbutton, pbutton->ops_arg,
     386                    &pos);
     387                if (rc != EOK)
     388                        goto error;
     389        } else {
     390                /* Text decoration */
     391                ui_paint_get_inset_frame_inside(pbutton->res, &rect, &irect);
     392                gfx_text_fmt_init(&fmt);
     393                fmt.font = pbutton->res->font;
     394                fmt.color = pbutton->res->btn_text_color;
     395                fmt.halign = gfx_halign_center;
     396                fmt.valign = gfx_valign_center;
     397                fmt.abbreviate = true;
     398                fmt.width = irect.p1.x - irect.p0.x - 2 * ui_pb_pad_x;
     399
     400                rc = gfx_puttext(&pos, &fmt, pbutton->caption);
     401                if (rc != EOK)
     402                        goto error;
     403        }
    316404
    317405        rc = ui_pbutton_paint_frame(pbutton);
     
    351439        errno_t rc;
    352440
    353         depressed = pbutton->held && pbutton->inside;
     441        if ((pbutton->flags & ui_pbf_no_text_depress) == 0)
     442                depressed = pbutton->held && pbutton->inside;
     443        else
     444                depressed = false;
    354445
    355446        rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color);
     
    379470
    380471        gfx_text_fmt_init(&fmt);
     472        fmt.font = pbutton->res->font;
    381473        fmt.color = pbutton->res->btn_text_color;
    382474        fmt.halign = gfx_halign_center;
    383475        fmt.valign = gfx_valign_center;
    384 
    385         rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
     476        fmt.abbreviate = true;
     477        fmt.width = rect.p1.x - rect.p0.x - 2 * ui_pb_pad_x_text;
     478        if (fmt.width < 1)
     479                fmt.width = 1;
     480
     481        rc = gfx_puttext(&pos, &fmt, pbutton->caption);
    386482        if (rc != EOK)
    387483                goto error;
     
    409505errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
    410506{
    411         if (pbutton->res->textmode)
     507        if (pbutton->ops != NULL && pbutton->ops->paint != NULL) {
     508                /* Custom paint routine */
     509                return pbutton->ops->paint(pbutton, pbutton->ops_arg);
     510        } else if (pbutton->res->textmode) {
    412511                return ui_pbutton_paint_text(pbutton);
    413         else
     512        } else {
    414513                return ui_pbutton_paint_gfx(pbutton);
     514        }
    415515}
    416516
     
    427527        pbutton->held = true;
    428528        (void) ui_pbutton_paint(pbutton);
     529        ui_pbutton_down(pbutton);
    429530}
    430531
     
    439540
    440541        pbutton->held = false;
     542        ui_pbutton_up(pbutton);
    441543
    442544        if (pbutton->inside) {
     
    474576}
    475577
    476 /** Button was clicked.
     578/** Send button clicked event.
    477579 *
    478580 * @param pbutton Push button
     
    482584        if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
    483585                pbutton->cb->clicked(pbutton, pbutton->arg);
     586}
     587
     588/** Send button down event.
     589 *
     590 * @param pbutton Push button
     591 */
     592void ui_pbutton_down(ui_pbutton_t *pbutton)
     593{
     594        if (pbutton->cb != NULL && pbutton->cb->down != NULL)
     595                pbutton->cb->down(pbutton, pbutton->arg);
     596}
     597
     598/** Send button up event.
     599 *
     600 * @param pbutton Push button
     601 */
     602void ui_pbutton_up(ui_pbutton_t *pbutton)
     603{
     604        if (pbutton->cb != NULL && pbutton->cb->up != NULL)
     605                pbutton->cb->up(pbutton, pbutton->arg);
    484606}
    485607
     
    521643                }
    522644                break;
     645        case POS_DCLICK:
     646                break;
    523647        }
    524648
Note: See TracChangeset for help on using the changeset viewer.