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