[6d5e378] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Petr Koupy
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup gui
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <str.h>
|
---|
[38d150e] | 37 | #include <stdlib.h>
|
---|
[2bb6d04] | 38 | #include <draw/drawctx.h>
|
---|
| 39 | #include <draw/surface.h>
|
---|
| 40 | #include <draw/font.h>
|
---|
[2cc1ec0] | 41 | #include <errno.h>
|
---|
[296e124e] | 42 | #include "common.h"
|
---|
[6d5e378] | 43 | #include "window.h"
|
---|
| 44 | #include "button.h"
|
---|
| 45 |
|
---|
[296e124e] | 46 | static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
|
---|
| 47 | static pixel_t color_shadow = PIXEL(255, 85, 85, 85);
|
---|
[6d5e378] | 48 |
|
---|
[296e124e] | 49 | static void paint_internal(widget_t *widget)
|
---|
| 50 | {
|
---|
| 51 | button_t *btn = (button_t *) widget;
|
---|
[a35b458] | 52 |
|
---|
[6d5e378] | 53 | surface_t *surface = window_claim(btn->widget.window);
|
---|
[296e124e] | 54 | if (!surface)
|
---|
[6d5e378] | 55 | window_yield(btn->widget.window);
|
---|
[a35b458] | 56 |
|
---|
[296e124e] | 57 | source_t source;
|
---|
| 58 | source_init(&source);
|
---|
[a35b458] | 59 |
|
---|
[6d5e378] | 60 | drawctx_t drawctx;
|
---|
| 61 | drawctx_init(&drawctx, surface);
|
---|
[a35b458] | 62 |
|
---|
[2cea4c58] | 63 | drawctx_set_compose(&drawctx, compose_over);
|
---|
[296e124e] | 64 | drawctx_set_source(&drawctx, &btn->background);
|
---|
| 65 | drawctx_transfer(&drawctx, widget->hpos, widget->vpos,
|
---|
| 66 | widget->width, widget->height);
|
---|
[a35b458] | 67 |
|
---|
[296e124e] | 68 | if ((widget->width >= 8) && (widget->height >= 8)) {
|
---|
| 69 | drawctx_set_source(&drawctx, &source);
|
---|
| 70 | draw_bevel(&drawctx, &source, widget->hpos + 3, widget->vpos + 3,
|
---|
| 71 | widget->width - 6, widget->height - 6, color_highlight,
|
---|
| 72 | color_shadow);
|
---|
[a35b458] | 73 |
|
---|
[296e124e] | 74 | drawctx_set_source(&drawctx, &btn->foreground);
|
---|
| 75 | drawctx_transfer(&drawctx, widget->hpos + 4, widget->vpos + 4,
|
---|
| 76 | widget->width - 8, widget->height - 8);
|
---|
[6d5e378] | 77 | }
|
---|
[a35b458] | 78 |
|
---|
[6d5e378] | 79 | sysarg_t cpt_width;
|
---|
| 80 | sysarg_t cpt_height;
|
---|
[2cc1ec0] | 81 | font_get_box(btn->font, btn->caption, &cpt_width, &cpt_height);
|
---|
[a35b458] | 82 |
|
---|
[296e124e] | 83 | if ((widget->width >= cpt_width) && (widget->height >= cpt_height)) {
|
---|
| 84 | sysarg_t x = ((widget->width - cpt_width) / 2) + widget->hpos;
|
---|
| 85 | sysarg_t y = ((widget->height - cpt_height) / 2) + widget->vpos;
|
---|
[a35b458] | 86 |
|
---|
[296e124e] | 87 | drawctx_set_source(&drawctx, &btn->text);
|
---|
[2cc1ec0] | 88 | drawctx_set_font(&drawctx, btn->font);
|
---|
[a35b458] | 89 |
|
---|
[296e124e] | 90 | if (btn->caption)
|
---|
[6d5e378] | 91 | drawctx_print(&drawctx, btn->caption, x, y);
|
---|
| 92 | }
|
---|
[a35b458] | 93 |
|
---|
[6d5e378] | 94 | window_yield(btn->widget.window);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void deinit_button(button_t *btn)
|
---|
| 98 | {
|
---|
| 99 | widget_deinit(&btn->widget);
|
---|
| 100 | free(btn->caption);
|
---|
[2cc1ec0] | 101 | font_release(btn->font);
|
---|
[6d5e378] | 102 | }
|
---|
| 103 |
|
---|
| 104 | static void button_destroy(widget_t *widget)
|
---|
| 105 | {
|
---|
| 106 | button_t *btn = (button_t *) widget;
|
---|
[a35b458] | 107 |
|
---|
[296e124e] | 108 | deinit_button(btn);
|
---|
[6d5e378] | 109 | free(btn);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | static void button_reconfigure(widget_t *widget)
|
---|
| 113 | {
|
---|
| 114 | /* no-op */
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | static void button_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
|
---|
| 118 | sysarg_t width, sysarg_t height)
|
---|
| 119 | {
|
---|
| 120 | widget_modify(widget, hpos, vpos, width, height);
|
---|
| 121 | paint_internal(widget);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | static void button_repaint(widget_t *widget)
|
---|
| 125 | {
|
---|
| 126 | paint_internal(widget);
|
---|
| 127 | window_damage(widget->window);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | static void button_handle_keyboard_event(widget_t *widget, kbd_event_t event)
|
---|
| 131 | {
|
---|
| 132 | button_t *btn = (button_t *) widget;
|
---|
[a35b458] | 133 |
|
---|
[296e124e] | 134 | if (event.key == KC_ENTER && event.type == KEY_PRESS)
|
---|
[6d5e378] | 135 | sig_send(&btn->clicked, NULL);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | static void button_handle_position_event(widget_t *widget, pos_event_t event)
|
---|
| 139 | {
|
---|
| 140 | button_t *btn = (button_t *) widget;
|
---|
| 141 | widget->window->focus = widget;
|
---|
[a35b458] | 142 |
|
---|
[6d5e378] | 143 | // TODO make the click logic more robust (mouse grabbing, mouse moves)
|
---|
| 144 | if (event.btn_num == 1) {
|
---|
[296e124e] | 145 | if (event.type == POS_RELEASE)
|
---|
[6d5e378] | 146 | sig_send(&btn->clicked, NULL);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[10cb47e] | 150 | bool init_button(button_t *btn, widget_t *parent, const void *data,
|
---|
| 151 | const char *caption, uint16_t points, pixel_t background,
|
---|
| 152 | pixel_t foreground, pixel_t text)
|
---|
[6d5e378] | 153 | {
|
---|
[10cb47e] | 154 | widget_init(&btn->widget, parent, data);
|
---|
[a35b458] | 155 |
|
---|
[6d5e378] | 156 | btn->widget.destroy = button_destroy;
|
---|
| 157 | btn->widget.reconfigure = button_reconfigure;
|
---|
| 158 | btn->widget.rearrange = button_rearrange;
|
---|
| 159 | btn->widget.repaint = button_repaint;
|
---|
| 160 | btn->widget.handle_keyboard_event = button_handle_keyboard_event;
|
---|
| 161 | btn->widget.handle_position_event = button_handle_position_event;
|
---|
[a35b458] | 162 |
|
---|
[6d5e378] | 163 | source_init(&btn->background);
|
---|
| 164 | source_set_color(&btn->background, background);
|
---|
[a35b458] | 165 |
|
---|
[6d5e378] | 166 | source_init(&btn->foreground);
|
---|
| 167 | source_set_color(&btn->foreground, foreground);
|
---|
[a35b458] | 168 |
|
---|
[296e124e] | 169 | source_init(&btn->text);
|
---|
| 170 | source_set_color(&btn->text, text);
|
---|
[a35b458] | 171 |
|
---|
[296e124e] | 172 | if (caption == NULL)
|
---|
[6d5e378] | 173 | btn->caption = NULL;
|
---|
[296e124e] | 174 | else
|
---|
[6d5e378] | 175 | btn->caption = str_dup(caption);
|
---|
[a35b458] | 176 |
|
---|
[b7fd2a0] | 177 | errno_t rc = embedded_font_create(&btn->font, points);
|
---|
[2cc1ec0] | 178 | if (rc != EOK) {
|
---|
| 179 | free(btn->caption);
|
---|
| 180 | btn->caption = NULL;
|
---|
| 181 | return false;
|
---|
| 182 | }
|
---|
[a35b458] | 183 |
|
---|
[6d5e378] | 184 | sysarg_t cpt_width;
|
---|
| 185 | sysarg_t cpt_height;
|
---|
[2cc1ec0] | 186 | font_get_box(btn->font, btn->caption, &cpt_width, &cpt_height);
|
---|
[296e124e] | 187 | btn->widget.width_min = cpt_width + 10;
|
---|
| 188 | btn->widget.height_min = cpt_height + 10;
|
---|
| 189 | btn->widget.width_ideal = cpt_width + 30;
|
---|
| 190 | btn->widget.height_ideal = cpt_height + 10;
|
---|
[a35b458] | 191 |
|
---|
[6d5e378] | 192 | return true;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[10cb47e] | 195 | button_t *create_button(widget_t *parent, const void *data, const char *caption,
|
---|
| 196 | uint16_t points, pixel_t background, pixel_t foreground, pixel_t text)
|
---|
[6d5e378] | 197 | {
|
---|
| 198 | button_t *btn = (button_t *) malloc(sizeof(button_t));
|
---|
[296e124e] | 199 | if (!btn)
|
---|
[6d5e378] | 200 | return NULL;
|
---|
[a35b458] | 201 |
|
---|
[10cb47e] | 202 | if (init_button(btn, parent, data, caption, points, background, foreground,
|
---|
[296e124e] | 203 | text))
|
---|
[6d5e378] | 204 | return btn;
|
---|
[a35b458] | 205 |
|
---|
[296e124e] | 206 | free(btn);
|
---|
| 207 | return NULL;
|
---|
[6d5e378] | 208 | }
|
---|
| 209 |
|
---|
| 210 | /** @}
|
---|
| 211 | */
|
---|