Changes in uspace/lib/gui/button.c [10cb47e:6d5e378] in mainline
- File:
-
- 1 edited
-
uspace/lib/gui/button.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gui/button.c
r10cb47e r6d5e378 36 36 #include <str.h> 37 37 #include <malloc.h> 38 38 39 #include <drawctx.h> 39 40 #include <surface.h> 40 #include <font/embedded.h> 41 #include <errno.h> 42 #include "common.h" 41 43 42 #include "window.h" 44 43 #include "button.h" 45 44 46 static pixel_t color_highlight = PIXEL(255, 255, 255, 255); 47 static pixel_t color_shadow = PIXEL(255, 85, 85, 85); 45 static void paint_internal(widget_t *w) 46 { 47 button_t *btn = (button_t *) w; 48 48 49 static void paint_internal(widget_t *widget)50 {51 button_t *btn = (button_t *) widget;52 53 49 surface_t *surface = window_claim(btn->widget.window); 54 if (!surface) 50 if (!surface) { 55 51 window_yield(btn->widget.window); 56 57 source_t source; 58 source_init(&source); 59 52 } 53 60 54 drawctx_t drawctx; 55 61 56 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); 76 64 } 77 65 78 66 sysarg_t cpt_width; 79 67 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) { 90 75 drawctx_print(&drawctx, btn->caption, x, y); 76 } 91 77 } 92 78 93 79 window_yield(btn->widget.window); 94 80 } … … 98 84 widget_deinit(&btn->widget); 99 85 free(btn->caption); 100 font_release( btn->font);86 font_release(&btn->font); 101 87 } 102 88 … … 104 90 { 105 91 button_t *btn = (button_t *) widget; 92 93 deinit_button(btn); 106 94 107 deinit_button(btn);108 95 free(btn); 109 96 } … … 130 117 { 131 118 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) { 134 120 sig_send(&btn->clicked, NULL); 121 } 135 122 } 136 123 … … 139 126 button_t *btn = (button_t *) widget; 140 127 widget->window->focus = widget; 141 128 142 129 // TODO make the click logic more robust (mouse grabbing, mouse moves) 143 130 if (event.btn_num == 1) { 144 if (event.type == POS_RELEASE) 131 if (event.type == POS_RELEASE) { 145 132 sig_send(&btn->clicked, NULL); 133 } 146 134 } 147 135 } 148 136 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) 137 bool init_button(button_t *btn, widget_t *parent, 138 const char *caption, uint16_t points, pixel_t background, pixel_t foreground) 152 139 { 153 widget_init(&btn->widget, parent , data);154 140 widget_init(&btn->widget, parent); 141 155 142 btn->widget.destroy = button_destroy; 156 143 btn->widget.reconfigure = button_reconfigure; … … 159 146 btn->widget.handle_keyboard_event = button_handle_keyboard_event; 160 147 btn->widget.handle_position_event = button_handle_position_event; 161 148 162 149 source_init(&btn->background); 163 150 source_set_color(&btn->background, background); 164 165 151 source_init(&btn->foreground); 166 152 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) { 172 155 btn->caption = NULL; 173 else156 } else { 174 157 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;181 158 } 182 159 font_init(&btn->font, FONT_DECODER_EMBEDDED, NULL, points); 160 183 161 sysarg_t cpt_width; 184 162 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 191 169 return true; 192 170 } 193 171 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)172 button_t *create_button(widget_t *parent, 173 const char *caption, uint16_t points, pixel_t background, pixel_t foreground) 196 174 { 197 175 button_t *btn = (button_t *) malloc(sizeof(button_t)); 198 if (!btn) 176 if (!btn) { 199 177 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)) { 203 181 return btn; 204 205 free(btn); 206 return NULL; 182 } else { 183 free(btn); 184 return NULL; 185 } 207 186 } 208 187
Note:
See TracChangeset
for help on using the changeset viewer.
