[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>
|
---|
| 37 | #include <malloc.h>
|
---|
| 38 |
|
---|
| 39 | #include <drawctx.h>
|
---|
| 40 | #include <surface.h>
|
---|
| 41 |
|
---|
| 42 | #include "window.h"
|
---|
| 43 | #include "button.h"
|
---|
| 44 |
|
---|
| 45 | static void paint_internal(widget_t *w)
|
---|
| 46 | {
|
---|
| 47 | button_t *btn = (button_t *) w;
|
---|
| 48 |
|
---|
| 49 | surface_t *surface = window_claim(btn->widget.window);
|
---|
| 50 | if (!surface) {
|
---|
| 51 | window_yield(btn->widget.window);
|
---|
| 52 | }
|
---|
| 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 |
|
---|
| 66 | sysarg_t cpt_width;
|
---|
| 67 | 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) {
|
---|
| 75 | drawctx_print(&drawctx, btn->caption, x, y);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | window_yield(btn->widget.window);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void deinit_button(button_t *btn)
|
---|
| 83 | {
|
---|
| 84 | widget_deinit(&btn->widget);
|
---|
| 85 | free(btn->caption);
|
---|
| 86 | font_release(&btn->font);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | static void button_destroy(widget_t *widget)
|
---|
| 90 | {
|
---|
| 91 | button_t *btn = (button_t *) widget;
|
---|
| 92 |
|
---|
| 93 | deinit_button(btn);
|
---|
| 94 |
|
---|
| 95 | free(btn);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | static void button_reconfigure(widget_t *widget)
|
---|
| 99 | {
|
---|
| 100 | /* no-op */
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | static void button_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
|
---|
| 104 | sysarg_t width, sysarg_t height)
|
---|
| 105 | {
|
---|
| 106 | widget_modify(widget, hpos, vpos, width, height);
|
---|
| 107 | paint_internal(widget);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | static void button_repaint(widget_t *widget)
|
---|
| 111 | {
|
---|
| 112 | paint_internal(widget);
|
---|
| 113 | window_damage(widget->window);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | static void button_handle_keyboard_event(widget_t *widget, kbd_event_t event)
|
---|
| 117 | {
|
---|
| 118 | button_t *btn = (button_t *) widget;
|
---|
| 119 | if (event.key == KC_ENTER && event.type == KEY_PRESS) {
|
---|
| 120 | sig_send(&btn->clicked, NULL);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | static void button_handle_position_event(widget_t *widget, pos_event_t event)
|
---|
| 125 | {
|
---|
| 126 | button_t *btn = (button_t *) widget;
|
---|
| 127 | widget->window->focus = widget;
|
---|
| 128 |
|
---|
| 129 | // TODO make the click logic more robust (mouse grabbing, mouse moves)
|
---|
| 130 | if (event.btn_num == 1) {
|
---|
| 131 | if (event.type == POS_RELEASE) {
|
---|
| 132 | 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)
|
---|
| 139 | {
|
---|
| 140 | widget_init(&btn->widget, parent);
|
---|
| 141 |
|
---|
| 142 | btn->widget.destroy = button_destroy;
|
---|
| 143 | btn->widget.reconfigure = button_reconfigure;
|
---|
| 144 | btn->widget.rearrange = button_rearrange;
|
---|
| 145 | btn->widget.repaint = button_repaint;
|
---|
| 146 | btn->widget.handle_keyboard_event = button_handle_keyboard_event;
|
---|
| 147 | btn->widget.handle_position_event = button_handle_position_event;
|
---|
| 148 |
|
---|
| 149 | source_init(&btn->background);
|
---|
| 150 | source_set_color(&btn->background, background);
|
---|
| 151 | source_init(&btn->foreground);
|
---|
| 152 | source_set_color(&btn->foreground, foreground);
|
---|
| 153 |
|
---|
| 154 | if (caption == NULL) {
|
---|
| 155 | btn->caption = NULL;
|
---|
| 156 | } else {
|
---|
| 157 | btn->caption = str_dup(caption);
|
---|
| 158 | }
|
---|
| 159 | font_init(&btn->font, FONT_DECODER_EMBEDDED, NULL, points);
|
---|
| 160 |
|
---|
| 161 | sysarg_t cpt_width;
|
---|
| 162 | 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 |
|
---|
| 169 | return true;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | button_t *create_button(widget_t *parent,
|
---|
| 173 | const char *caption, uint16_t points, pixel_t background, pixel_t foreground)
|
---|
| 174 | {
|
---|
| 175 | button_t *btn = (button_t *) malloc(sizeof(button_t));
|
---|
| 176 | if (!btn) {
|
---|
| 177 | return NULL;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | if (init_button(btn, parent, caption, points, background, foreground)) {
|
---|
| 181 | return btn;
|
---|
| 182 | } else {
|
---|
| 183 | free(btn);
|
---|
| 184 | return NULL;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /** @}
|
---|
| 189 | */
|
---|