Ignore:
File:
1 edited

Legend:

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

    r3c54869 rcd74fa8  
    11/*
    2  * Copyright (c) 2023 Jiri Svoboda
     2 * Copyright (c) 2021 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.
    3734 */
    3835
     
    5451enum {
    5552        ui_pb_press_dx = 1,
    56         ui_pb_press_dy = 1,
    57         ui_pb_pad_x = 2,
    58         ui_pb_pad_x_text = 1
     53        ui_pb_press_dy = 1
    5954};
    6055
     
    116111
    117112        ui_control_delete(pbutton->control);
    118         free(pbutton->caption);
    119113        free(pbutton);
    120114}
     
    142136}
    143137
    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  */
    150 void 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  */
    162 void ui_pbutton_set_flags(ui_pbutton_t *pbutton, ui_pbutton_flags_t flags)
    163 {
    164         pbutton->flags = flags;
    165 }
    166 
    167138/** Set button rectangle.
    168139 *
     
    186157{
    187158        pbutton->isdefault = isdefault;
    188 }
    189 
    190 /** Get button light status.
    191  *
    192  * @param pbutton Button
    193  * @return @c true iff light is on
    194  */
    195 bool 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  */
    205 void 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  */
    216 errno_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;
    227159}
    228160
     
    345277        gfx_text_fmt_t fmt;
    346278        gfx_rect_t rect;
    347         gfx_rect_t irect;
    348279        gfx_coord_t thickness;
    349         gfx_color_t *color;
    350280        bool depressed;
    351281        errno_t rc;
     
    359289        rect.p1.y = pbutton->rect.p1.y - thickness;
    360290
    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);
     291        rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
    365292        if (rc != EOK)
    366293                goto error;
     
    379306        }
    380307
    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         }
     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;
    402316
    403317        rc = ui_pbutton_paint_frame(pbutton);
     
    437351        errno_t rc;
    438352
    439         if ((pbutton->flags & ui_pbf_no_text_depress) == 0)
    440                 depressed = pbutton->held && pbutton->inside;
    441         else
    442                 depressed = false;
     353        depressed = pbutton->held && pbutton->inside;
    443354
    444355        rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color);
     
    455366        rect.p1.y = pbutton->rect.p0.y + 1;
    456367
    457         rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color);
     368        rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_highlight_color);
    458369        if (rc != EOK)
    459370                goto error;
     
    468379
    469380        gfx_text_fmt_init(&fmt);
    470         fmt.font = pbutton->res->font;
    471381        fmt.color = pbutton->res->btn_text_color;
    472382        fmt.halign = gfx_halign_center;
    473383        fmt.valign = gfx_valign_center;
    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);
     384
     385        rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
    480386        if (rc != EOK)
    481387                goto error;
     
    521427        pbutton->held = true;
    522428        (void) ui_pbutton_paint(pbutton);
    523         ui_pbutton_down(pbutton);
    524429}
    525430
     
    534439
    535440        pbutton->held = false;
    536         ui_pbutton_up(pbutton);
    537441
    538442        if (pbutton->inside) {
     
    570474}
    571475
    572 /** Send button clicked event.
     476/** Button was clicked.
    573477 *
    574478 * @param pbutton Push button
     
    578482        if (pbutton->cb != NULL && pbutton->cb->clicked != NULL)
    579483                pbutton->cb->clicked(pbutton, pbutton->arg);
    580 }
    581 
    582 /** Send button down event.
    583  *
    584  * @param pbutton Push button
    585  */
    586 void 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  */
    596 void ui_pbutton_up(ui_pbutton_t *pbutton)
    597 {
    598         if (pbutton->cb != NULL && pbutton->cb->up != NULL)
    599                 pbutton->cb->up(pbutton, pbutton->arg);
    600484}
    601485
     
    637521                }
    638522                break;
    639         case POS_DCLICK:
    640                 break;
    641523        }
    642524
Note: See TracChangeset for help on using the changeset viewer.