Ignore:
File:
1 edited

Legend:

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

    r10cb47e r6d5e378  
    3636#include <str.h>
    3737#include <malloc.h>
     38
    3839#include <drawctx.h>
    3940#include <surface.h>
    40 #include <font/embedded.h>
    41 #include <errno.h>
    42 #include "common.h"
     41
    4342#include "window.h"
    4443#include "button.h"
    4544
    46 static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
    47 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;
    4848
    49 static void paint_internal(widget_t *widget)
    50 {
    51         button_t *btn = (button_t *) widget;
    52        
    5349        surface_t *surface = window_claim(btn->widget.window);
    54         if (!surface)
     50        if (!surface) {
    5551                window_yield(btn->widget.window);
    56        
    57         source_t source;
    58         source_init(&source);
    59        
     52        }
     53
    6054        drawctx_t drawctx;
     55
    6156        drawctx_init(&drawctx, surface);
    62        
    63         drawctx_set_source(&drawctx, &btn->background);
    64         drawctx_transfer(&drawctx, widget->hpos, widget->vpos,
    65             widget->width, widget->height);
    66        
    67         if ((widget->width >= 8) && (widget->height >= 8)) {
    68                 drawctx_set_source(&drawctx, &source);
    69                 draw_bevel(&drawctx, &source, widget->hpos + 3, widget->vpos + 3,
    70                     widget->width - 6, widget->height - 6, color_highlight,
    71                     color_shadow);
    72                
    73                 drawctx_set_source(&drawctx, &btn->foreground);
    74                 drawctx_transfer(&drawctx, widget->hpos + 4, widget->vpos + 4,
    75                     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);
    7664        }
    77        
     65
    7866        sysarg_t cpt_width;
    7967        sysarg_t cpt_height;
    80         font_get_box(btn->font, btn->caption, &cpt_width, &cpt_height);
    81        
    82         if ((widget->width >= cpt_width) && (widget->height >= cpt_height)) {
    83                 sysarg_t x = ((widget->width - cpt_width) / 2) + widget->hpos;
    84                 sysarg_t y = ((widget->height - cpt_height) / 2) + widget->vpos;
    85                
    86                 drawctx_set_source(&drawctx, &btn->text);
    87                 drawctx_set_font(&drawctx, btn->font);
    88                
    89                 if (btn->caption)
     68        font_get_box(&btn->font, btn->caption, &cpt_width, &cpt_height);
     69        if (w->width >= cpt_width && w->height >= cpt_height) {
     70                drawctx_set_source(&drawctx, &btn->foreground);
     71                drawctx_set_font(&drawctx, &btn->font);
     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) {
    9075                        drawctx_print(&drawctx, btn->caption, x, y);
     76                }
    9177        }
    92        
     78
    9379        window_yield(btn->widget.window);
    9480}
     
    9884        widget_deinit(&btn->widget);
    9985        free(btn->caption);
    100         font_release(btn->font);
     86        font_release(&btn->font);
    10187}
    10288
     
    10490{
    10591        button_t *btn = (button_t *) widget;
     92
     93        deinit_button(btn);
    10694       
    107         deinit_button(btn);
    10895        free(btn);
    10996}
     
    130117{
    131118        button_t *btn = (button_t *) widget;
    132        
    133         if (event.key == KC_ENTER && event.type == KEY_PRESS)
     119        if (event.key == KC_ENTER && event.type == KEY_PRESS) {
    134120                sig_send(&btn->clicked, NULL);
     121        }
    135122}
    136123
     
    139126        button_t *btn = (button_t *) widget;
    140127        widget->window->focus = widget;
    141        
     128
    142129        // TODO make the click logic more robust (mouse grabbing, mouse moves)
    143130        if (event.btn_num == 1) {
    144                 if (event.type == POS_RELEASE)
     131                if (event.type == POS_RELEASE) {
    145132                        sig_send(&btn->clicked, NULL);
     133                }
    146134        }
    147135}
    148136
    149 bool init_button(button_t *btn, widget_t *parent, const void *data,
    150     const char *caption, uint16_t points, pixel_t background,
    151     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)
    152139{
    153         widget_init(&btn->widget, parent, data);
    154        
     140        widget_init(&btn->widget, parent);
     141
    155142        btn->widget.destroy = button_destroy;
    156143        btn->widget.reconfigure = button_reconfigure;
     
    159146        btn->widget.handle_keyboard_event = button_handle_keyboard_event;
    160147        btn->widget.handle_position_event = button_handle_position_event;
    161        
     148
    162149        source_init(&btn->background);
    163150        source_set_color(&btn->background, background);
    164        
    165151        source_init(&btn->foreground);
    166152        source_set_color(&btn->foreground, foreground);
    167        
    168         source_init(&btn->text);
    169         source_set_color(&btn->text, text);
    170        
    171         if (caption == NULL)
     153
     154        if (caption == NULL) {
    172155                btn->caption = NULL;
    173         else
     156        } else {
    174157                btn->caption = str_dup(caption);
    175        
    176         int rc = embedded_font_create(&btn->font, points);
    177         if (rc != EOK) {
    178                 free(btn->caption);
    179                 btn->caption = NULL;
    180                 return false;
    181158        }
    182        
     159        font_init(&btn->font, FONT_DECODER_EMBEDDED, NULL, points);
     160
    183161        sysarg_t cpt_width;
    184162        sysarg_t cpt_height;
    185         font_get_box(btn->font, btn->caption, &cpt_width, &cpt_height);
    186         btn->widget.width_min = cpt_width + 10;
    187         btn->widget.height_min = cpt_height + 10;
    188         btn->widget.width_ideal = cpt_width + 30;
    189         btn->widget.height_ideal = cpt_height + 10;
    190        
     163        font_get_box(&btn->font, btn->caption, &cpt_width, &cpt_height);
     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
    191169        return true;
    192170}
    193171
    194 button_t *create_button(widget_t *parent, const void *data, const char *caption,
    195     uint16_t points, 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)
    196174{
    197175        button_t *btn = (button_t *) malloc(sizeof(button_t));
    198         if (!btn)
     176        if (!btn) {
    199177                return NULL;
    200        
    201         if (init_button(btn, parent, data, caption, points, background, foreground,
    202             text))
     178        }
     179
     180        if (init_button(btn, parent, caption, points, background, foreground)) {
    203181                return btn;
    204        
    205         free(btn);
    206         return NULL;
     182        } else {
     183                free(btn);
     184                return NULL;
     185        }
    207186}
    208187
Note: See TracChangeset for help on using the changeset viewer.