Changeset 296e124e in mainline for uspace/lib/gui/button.c


Ignore:
Timestamp:
2014-01-10T17:06:50Z (10 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8bb0f5d6
Parents:
4edd71f6
Message:

improve visual appearance of GUI windows and buttons

File:
1 edited

Legend:

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

    r4edd71f6 r296e124e  
    3636#include <str.h>
    3737#include <malloc.h>
    38 
    3938#include <drawctx.h>
    4039#include <surface.h>
    41 
     40#include "common.h"
    4241#include "window.h"
    4342#include "button.h"
    4443
    45 static void paint_internal(widget_t *w)
    46 {
    47         button_t *btn = (button_t *) w;
    48 
     44static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
     45static pixel_t color_shadow = PIXEL(255, 85, 85, 85);
     46
     47static void paint_internal(widget_t *widget)
     48{
     49        button_t *btn = (button_t *) widget;
     50       
    4951        surface_t *surface = window_claim(btn->widget.window);
    50         if (!surface) {
     52        if (!surface)
    5153                window_yield(btn->widget.window);
     54       
     55        source_t source;
     56        source_init(&source);
     57       
     58        drawctx_t drawctx;
     59        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);
    5274        }
    53 
    54         drawctx_t drawctx;
    55 
    56         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 
     75       
    6676        sysarg_t cpt_width;
    6777        sysarg_t cpt_height;
    6878        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);
     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);
    7185                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) {
     86               
     87                if (btn->caption)
    7588                        drawctx_print(&drawctx, btn->caption, x, y);
    76                 }
    7789        }
    78 
     90       
    7991        window_yield(btn->widget.window);
    8092}
     
    90102{
    91103        button_t *btn = (button_t *) widget;
    92 
     104       
    93105        deinit_button(btn);
    94        
    95106        free(btn);
    96107}
     
    117128{
    118129        button_t *btn = (button_t *) widget;
    119         if (event.key == KC_ENTER && event.type == KEY_PRESS) {
     130       
     131        if (event.key == KC_ENTER && event.type == KEY_PRESS)
    120132                sig_send(&btn->clicked, NULL);
    121         }
    122133}
    123134
     
    126137        button_t *btn = (button_t *) widget;
    127138        widget->window->focus = widget;
    128 
     139       
    129140        // TODO make the click logic more robust (mouse grabbing, mouse moves)
    130141        if (event.btn_num == 1) {
    131                 if (event.type == POS_RELEASE) {
     142                if (event.type == POS_RELEASE)
    132143                        sig_send(&btn->clicked, NULL);
    133                 }
    134144        }
    135145}
    136146
    137 bool init_button(button_t *btn, widget_t *parent,
    138     const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
     147bool init_button(button_t *btn, widget_t *parent, const char *caption,
     148    uint16_t points, pixel_t background, pixel_t foreground, pixel_t text)
    139149{
    140150        widget_init(&btn->widget, parent);
    141 
     151       
    142152        btn->widget.destroy = button_destroy;
    143153        btn->widget.reconfigure = button_reconfigure;
     
    146156        btn->widget.handle_keyboard_event = button_handle_keyboard_event;
    147157        btn->widget.handle_position_event = button_handle_position_event;
    148 
     158       
    149159        source_init(&btn->background);
    150160        source_set_color(&btn->background, background);
     161       
    151162        source_init(&btn->foreground);
    152163        source_set_color(&btn->foreground, foreground);
    153 
    154         if (caption == NULL) {
     164       
     165        source_init(&btn->text);
     166        source_set_color(&btn->text, text);
     167       
     168        if (caption == NULL)
    155169                btn->caption = NULL;
    156         } else {
     170        else
    157171                btn->caption = str_dup(caption);
    158         }
     172       
    159173        font_init(&btn->font, FONT_DECODER_EMBEDDED, NULL, points);
    160 
     174       
    161175        sysarg_t cpt_width;
    162176        sysarg_t cpt_height;
    163177        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 
     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       
    169183        return true;
    170184}
    171185
    172 button_t *create_button(widget_t *parent,
    173     const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
     186button_t *create_button(widget_t *parent, const char *caption, uint16_t points,
     187    pixel_t background, pixel_t foreground, pixel_t text)
    174188{
    175189        button_t *btn = (button_t *) malloc(sizeof(button_t));
    176         if (!btn) {
     190        if (!btn)
    177191                return NULL;
    178         }
    179 
    180         if (init_button(btn, parent, caption, points, background, foreground)) {
     192       
     193        if (init_button(btn, parent, caption, points, background, foreground,
     194            text))
    181195                return btn;
    182         } else {
    183                 free(btn);
    184                 return NULL;
    185         }
     196       
     197        free(btn);
     198        return NULL;
    186199}
    187200
Note: See TracChangeset for help on using the changeset viewer.