Ignore:
File:
1 edited

Legend:

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

    r6d5e378 r2cc1ec0  
    3636#include <str.h>
    3737#include <malloc.h>
    38 
    3938#include <drawctx.h>
    4039#include <surface.h>
    41 
     40#include <font/embedded.h>
     41#include <errno.h>
     42#include "common.h"
    4243#include "window.h"
    4344#include "button.h"
    4445
    45 static void paint_internal(widget_t *w)
    46 {
    47         button_t *btn = (button_t *) w;
    48 
     46static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
     47static pixel_t color_shadow = PIXEL(255, 85, 85, 85);
     48
     49static void paint_internal(widget_t *widget)
     50{
     51        button_t *btn = (button_t *) widget;
     52       
    4953        surface_t *surface = window_claim(btn->widget.window);
    50         if (!surface) {
     54        if (!surface)
    5155                window_yield(btn->widget.window);
    52         }
    53 
     56       
     57        source_t source;
     58        source_init(&source);
     59       
    5460        drawctx_t drawctx;
    55 
    5661        drawctx_init(&drawctx, surface);
    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);
    64         }
    65 
     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);
     76        }
     77       
    6678        sysarg_t cpt_width;
    6779        sysarg_t cpt_height;
    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) {
     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)
    7590                        drawctx_print(&drawctx, btn->caption, x, y);
    76                 }
    77         }
    78 
     91        }
     92       
    7993        window_yield(btn->widget.window);
    8094}
     
    8498        widget_deinit(&btn->widget);
    8599        free(btn->caption);
    86         font_release(&btn->font);
     100        font_release(btn->font);
    87101}
    88102
     
    90104{
    91105        button_t *btn = (button_t *) widget;
    92 
     106       
    93107        deinit_button(btn);
    94        
    95108        free(btn);
    96109}
     
    117130{
    118131        button_t *btn = (button_t *) widget;
    119         if (event.key == KC_ENTER && event.type == KEY_PRESS) {
     132       
     133        if (event.key == KC_ENTER && event.type == KEY_PRESS)
    120134                sig_send(&btn->clicked, NULL);
    121         }
    122135}
    123136
     
    126139        button_t *btn = (button_t *) widget;
    127140        widget->window->focus = widget;
    128 
     141       
    129142        // TODO make the click logic more robust (mouse grabbing, mouse moves)
    130143        if (event.btn_num == 1) {
    131                 if (event.type == POS_RELEASE) {
     144                if (event.type == POS_RELEASE)
    132145                        sig_send(&btn->clicked, NULL);
    133                 }
    134         }
    135 }
    136 
    137 bool init_button(button_t *btn, widget_t *parent,
    138     const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
     146        }
     147}
     148
     149bool init_button(button_t *btn, widget_t *parent, const char *caption,
     150    uint16_t points, pixel_t background, pixel_t foreground, pixel_t text)
    139151{
    140152        widget_init(&btn->widget, parent);
    141 
     153       
    142154        btn->widget.destroy = button_destroy;
    143155        btn->widget.reconfigure = button_reconfigure;
     
    146158        btn->widget.handle_keyboard_event = button_handle_keyboard_event;
    147159        btn->widget.handle_position_event = button_handle_position_event;
    148 
     160       
    149161        source_init(&btn->background);
    150162        source_set_color(&btn->background, background);
     163       
    151164        source_init(&btn->foreground);
    152165        source_set_color(&btn->foreground, foreground);
    153 
    154         if (caption == NULL) {
     166       
     167        source_init(&btn->text);
     168        source_set_color(&btn->text, text);
     169       
     170        if (caption == NULL)
    155171                btn->caption = NULL;
    156         } else {
     172        else
    157173                btn->caption = str_dup(caption);
    158         }
    159         font_init(&btn->font, FONT_DECODER_EMBEDDED, NULL, points);
    160 
     174       
     175        int rc = embedded_font_create(&btn->font, points);
     176        if (rc != EOK) {
     177                free(btn->caption);
     178                btn->caption = NULL;
     179                return false;
     180        }
     181       
    161182        sysarg_t cpt_width;
    162183        sysarg_t cpt_height;
    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 
     184        font_get_box(btn->font, btn->caption, &cpt_width, &cpt_height);
     185        btn->widget.width_min = cpt_width + 10;
     186        btn->widget.height_min = cpt_height + 10;
     187        btn->widget.width_ideal = cpt_width + 30;
     188        btn->widget.height_ideal = cpt_height + 10;
     189       
    169190        return true;
    170191}
    171192
    172 button_t *create_button(widget_t *parent,
    173     const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
     193button_t *create_button(widget_t *parent, const char *caption, uint16_t points,
     194    pixel_t background, pixel_t foreground, pixel_t text)
    174195{
    175196        button_t *btn = (button_t *) malloc(sizeof(button_t));
    176         if (!btn) {
     197        if (!btn)
    177198                return NULL;
    178         }
    179 
    180         if (init_button(btn, parent, caption, points, background, foreground)) {
     199       
     200        if (init_button(btn, parent, caption, points, background, foreground,
     201            text))
    181202                return btn;
    182         } else {
    183                 free(btn);
    184                 return NULL;
    185         }
     203       
     204        free(btn);
     205        return NULL;
    186206}
    187207
Note: See TracChangeset for help on using the changeset viewer.