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