Changeset 19b3cc6 in mainline for uspace/lib/gui/button.c
- Timestamp:
- 2014-01-17T23:12:10Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- e26a9d95
- Parents:
- fddffb2 (diff), facc34d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gui/button.c
rfddffb2 r19b3cc6 36 36 #include <str.h> 37 37 #include <malloc.h> 38 39 38 #include <drawctx.h> 40 39 #include <surface.h> 41 40 #include "common.h" 42 41 #include "window.h" 43 42 #include "button.h" 44 43 45 static void paint_internal(widget_t *w) 46 { 47 button_t *btn = (button_t *) w; 48 44 static pixel_t color_highlight = PIXEL(255, 255, 255, 255); 45 static pixel_t color_shadow = PIXEL(255, 85, 85, 85); 46 47 static void paint_internal(widget_t *widget) 48 { 49 button_t *btn = (button_t *) widget; 50 49 51 surface_t *surface = window_claim(btn->widget.window); 50 if (!surface) {52 if (!surface) 51 53 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); 52 74 } 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 66 76 sysarg_t cpt_width; 67 77 sysarg_t cpt_height; 68 78 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); 71 85 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) 75 88 drawctx_print(&drawctx, btn->caption, x, y); 76 }77 89 } 78 90 79 91 window_yield(btn->widget.window); 80 92 } … … 90 102 { 91 103 button_t *btn = (button_t *) widget; 92 104 93 105 deinit_button(btn); 94 95 106 free(btn); 96 107 } … … 117 128 { 118 129 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) 120 132 sig_send(&btn->clicked, NULL); 121 }122 133 } 123 134 … … 126 137 button_t *btn = (button_t *) widget; 127 138 widget->window->focus = widget; 128 139 129 140 // TODO make the click logic more robust (mouse grabbing, mouse moves) 130 141 if (event.btn_num == 1) { 131 if (event.type == POS_RELEASE) {142 if (event.type == POS_RELEASE) 132 143 sig_send(&btn->clicked, NULL); 133 }134 144 } 135 145 } 136 146 137 bool init_button(button_t *btn, widget_t *parent, 138 const char *caption, uint16_t points, pixel_t background, pixel_t foreground)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) 139 149 { 140 150 widget_init(&btn->widget, parent); 141 151 142 152 btn->widget.destroy = button_destroy; 143 153 btn->widget.reconfigure = button_reconfigure; … … 146 156 btn->widget.handle_keyboard_event = button_handle_keyboard_event; 147 157 btn->widget.handle_position_event = button_handle_position_event; 148 158 149 159 source_init(&btn->background); 150 160 source_set_color(&btn->background, background); 161 151 162 source_init(&btn->foreground); 152 163 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) 155 169 btn->caption = NULL; 156 } else {170 else 157 171 btn->caption = str_dup(caption); 158 }172 159 173 font_init(&btn->font, FONT_DECODER_EMBEDDED, NULL, points); 160 174 161 175 sysarg_t cpt_width; 162 176 sysarg_t cpt_height; 163 177 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 169 183 return true; 170 184 } 171 185 172 button_t *create_button(widget_t *parent, 173 const char *caption, uint16_t points, pixel_t background, pixel_t foreground)186 button_t *create_button(widget_t *parent, const char *caption, uint16_t points, 187 pixel_t background, pixel_t foreground, pixel_t text) 174 188 { 175 189 button_t *btn = (button_t *) malloc(sizeof(button_t)); 176 if (!btn) {190 if (!btn) 177 191 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)) 181 195 return btn; 182 } else { 183 free(btn); 184 return NULL; 185 } 196 197 free(btn); 198 return NULL; 186 199 } 187 200
Note:
See TracChangeset
for help on using the changeset viewer.