Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/gui/button.c

    r296e124e r6d5e378  
    3636#include <str.h>
    3737#include <malloc.h>
     38
    3839#include <drawctx.h>
    3940#include <surface.h>
    40 #include "common.h"
     41
    4142#include "window.h"
    4243#include "button.h"
    4344
    44 static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
    45 static pixel_t color_shadow = PIXEL(255, 85, 85, 85);
     45static void paint_internal(widget_t *w)
     46{
     47        button_t *btn = (button_t *) w;
    4648
    47 static void paint_internal(widget_t *widget)
    48 {
    49         button_t *btn = (button_t *) widget;
    50        
    5149        surface_t *surface = window_claim(btn->widget.window);
    52         if (!surface)
     50        if (!surface) {
    5351                window_yield(btn->widget.window);
    54        
    55         source_t source;
    56         source_init(&source);
    57        
     52        }
     53
    5854        drawctx_t drawctx;
     55
    5956        drawctx_init(&drawctx, surface);
    60        
    61         drawctx_set_source(&drawctx, &btn->background);
    62         drawctx_transfer(&drawctx, widget->hpos, widget->vpos,
    63             widget->width, widget->height);
    64        
    65         if ((widget->width >= 8) && (widget->height >= 8)) {
    66                 drawctx_set_source(&drawctx, &source);
    67                 draw_bevel(&drawctx, &source, widget->hpos + 3, widget->vpos + 3,
    68                     widget->width - 6, widget->height - 6, color_highlight,
    69                     color_shadow);
    70                
    71                 drawctx_set_source(&drawctx, &btn->foreground);
    72                 drawctx_transfer(&drawctx, widget->hpos + 4, widget->vpos + 4,
    73                     widget->width - 8, widget->height - 8);
     57        drawctx_set_source(&drawctx, &btn->foreground);
     58        drawctx_transfer(&drawctx, w->hpos, w->vpos, w->width, w->height);
     59
     60        if (w->width >= 6 && w->height >= 6) {
     61                drawctx_set_source(&drawctx, &btn->background);
     62                drawctx_transfer(&drawctx,
     63                    w->hpos + 3, w->vpos + 3, w->width - 6, w->height - 6);
    7464        }
    75        
     65
    7666        sysarg_t cpt_width;
    7767        sysarg_t cpt_height;
    7868        font_get_box(&btn->font, btn->caption, &cpt_width, &cpt_height);
    79        
    80         if ((widget->width >= cpt_width) && (widget->height >= cpt_height)) {
    81                 sysarg_t x = ((widget->width - cpt_width) / 2) + widget->hpos;
    82                 sysarg_t y = ((widget->height - cpt_height) / 2) + widget->vpos;
    83                
    84                 drawctx_set_source(&drawctx, &btn->text);
     69        if (w->width >= cpt_width && w->height >= cpt_height) {
     70                drawctx_set_source(&drawctx, &btn->foreground);
    8571                drawctx_set_font(&drawctx, &btn->font);
    86                
    87                 if (btn->caption)
     72                sysarg_t x = ((w->width - cpt_width) / 2) + w->hpos;
     73                sysarg_t y = ((w->height - cpt_height) / 2) + w->vpos;
     74                if (btn->caption) {
    8875                        drawctx_print(&drawctx, btn->caption, x, y);
     76                }
    8977        }
    90        
     78
    9179        window_yield(btn->widget.window);
    9280}
     
    10290{
    10391        button_t *btn = (button_t *) widget;
     92
     93        deinit_button(btn);
    10494       
    105         deinit_button(btn);
    10695        free(btn);
    10796}
     
    128117{
    129118        button_t *btn = (button_t *) widget;
    130        
    131         if (event.key == KC_ENTER && event.type == KEY_PRESS)
     119        if (event.key == KC_ENTER && event.type == KEY_PRESS) {
    132120                sig_send(&btn->clicked, NULL);
     121        }
    133122}
    134123
     
    137126        button_t *btn = (button_t *) widget;
    138127        widget->window->focus = widget;
    139        
     128
    140129        // TODO make the click logic more robust (mouse grabbing, mouse moves)
    141130        if (event.btn_num == 1) {
    142                 if (event.type == POS_RELEASE)
     131                if (event.type == POS_RELEASE) {
    143132                        sig_send(&btn->clicked, NULL);
     133                }
    144134        }
    145135}
    146136
    147 bool init_button(button_t *btn, widget_t *parent, const char *caption,
    148     uint16_t points, pixel_t background, pixel_t foreground, pixel_t text)
     137bool init_button(button_t *btn, widget_t *parent,
     138    const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
    149139{
    150140        widget_init(&btn->widget, parent);
    151        
     141
    152142        btn->widget.destroy = button_destroy;
    153143        btn->widget.reconfigure = button_reconfigure;
     
    156146        btn->widget.handle_keyboard_event = button_handle_keyboard_event;
    157147        btn->widget.handle_position_event = button_handle_position_event;
    158        
     148
    159149        source_init(&btn->background);
    160150        source_set_color(&btn->background, background);
    161        
    162151        source_init(&btn->foreground);
    163152        source_set_color(&btn->foreground, foreground);
    164        
    165         source_init(&btn->text);
    166         source_set_color(&btn->text, text);
    167        
    168         if (caption == NULL)
     153
     154        if (caption == NULL) {
    169155                btn->caption = NULL;
    170         else
     156        } else {
    171157                btn->caption = str_dup(caption);
    172        
     158        }
    173159        font_init(&btn->font, FONT_DECODER_EMBEDDED, NULL, points);
    174        
     160
    175161        sysarg_t cpt_width;
    176162        sysarg_t cpt_height;
    177163        font_get_box(&btn->font, btn->caption, &cpt_width, &cpt_height);
    178         btn->widget.width_min = cpt_width + 10;
    179         btn->widget.height_min = cpt_height + 10;
    180         btn->widget.width_ideal = cpt_width + 30;
    181         btn->widget.height_ideal = cpt_height + 10;
    182        
     164        btn->widget.width_min = cpt_width + 8;
     165        btn->widget.height_min = cpt_height + 8;
     166        btn->widget.width_ideal = cpt_width + 28;
     167        btn->widget.height_ideal = cpt_height + 8;
     168
    183169        return true;
    184170}
    185171
    186 button_t *create_button(widget_t *parent, const char *caption, uint16_t points,
    187     pixel_t background, pixel_t foreground, pixel_t text)
     172button_t *create_button(widget_t *parent,
     173    const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
    188174{
    189175        button_t *btn = (button_t *) malloc(sizeof(button_t));
    190         if (!btn)
     176        if (!btn) {
    191177                return NULL;
    192        
    193         if (init_button(btn, parent, caption, points, background, foreground,
    194             text))
     178        }
     179
     180        if (init_button(btn, parent, caption, points, background, foreground)) {
    195181                return btn;
    196        
    197         free(btn);
    198         return NULL;
     182        } else {
     183                free(btn);
     184                return NULL;
     185        }
    199186}
    200187
Note: See TracChangeset for help on using the changeset viewer.