[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 | #include <drawctx.h>
|
---|
| 39 | #include <surface.h>
|
---|
| 40 | #include "window.h"
|
---|
| 41 | #include "label.h"
|
---|
| 42 |
|
---|
[296e124e] | 43 | static void paint_internal(widget_t *widget)
|
---|
[6d5e378] | 44 | {
|
---|
[296e124e] | 45 | label_t *lbl = (label_t *) widget;
|
---|
[6d5e378] | 46 |
|
---|
| 47 | surface_t *surface = window_claim(lbl->widget.window);
|
---|
[296e124e] | 48 | if (!surface)
|
---|
[6d5e378] | 49 | window_yield(lbl->widget.window);
|
---|
[296e124e] | 50 |
|
---|
[6d5e378] | 51 | drawctx_t drawctx;
|
---|
| 52 | drawctx_init(&drawctx, surface);
|
---|
[296e124e] | 53 |
|
---|
[6d5e378] | 54 | drawctx_set_source(&drawctx, &lbl->background);
|
---|
[296e124e] | 55 | drawctx_transfer(&drawctx, widget->hpos, widget->vpos, widget->width,
|
---|
| 56 | widget->height);
|
---|
| 57 |
|
---|
[6d5e378] | 58 | sysarg_t cpt_width;
|
---|
| 59 | sysarg_t cpt_height;
|
---|
| 60 | font_get_box(&lbl->font, lbl->caption, &cpt_width, &cpt_height);
|
---|
[296e124e] | 61 |
|
---|
| 62 | if ((widget->width >= cpt_width) && (widget->height >= cpt_height)) {
|
---|
| 63 | sysarg_t x = ((widget->width - cpt_width) / 2) + widget->hpos;
|
---|
| 64 | sysarg_t y = ((widget->height - cpt_height) / 2) + widget->vpos;
|
---|
| 65 |
|
---|
| 66 | drawctx_set_source(&drawctx, &lbl->text);
|
---|
[6d5e378] | 67 | drawctx_set_font(&drawctx, &lbl->font);
|
---|
[296e124e] | 68 |
|
---|
| 69 | if (lbl->caption)
|
---|
[6d5e378] | 70 | drawctx_print(&drawctx, lbl->caption, x, y);
|
---|
| 71 | }
|
---|
[296e124e] | 72 |
|
---|
[6d5e378] | 73 | window_yield(lbl->widget.window);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | static void on_rewrite(widget_t *widget, void *data)
|
---|
| 77 | {
|
---|
| 78 | if (data != NULL) {
|
---|
| 79 | label_t *lbl = (label_t *) widget;
|
---|
[296e124e] | 80 |
|
---|
[6d5e378] | 81 | const char *new_caption = (const char *) data;
|
---|
| 82 | lbl->caption = str_dup(new_caption);
|
---|
[296e124e] | 83 |
|
---|
[6d5e378] | 84 | sysarg_t cpt_width;
|
---|
| 85 | sysarg_t cpt_height;
|
---|
| 86 | font_get_box(&lbl->font, lbl->caption, &cpt_width, &cpt_height);
|
---|
[296e124e] | 87 |
|
---|
[6d5e378] | 88 | lbl->widget.width_min = cpt_width + 4;
|
---|
| 89 | lbl->widget.height_min = cpt_height + 4;
|
---|
| 90 | lbl->widget.width_ideal = lbl->widget.width_min;
|
---|
| 91 | lbl->widget.height_ideal = lbl->widget.height_min;
|
---|
[296e124e] | 92 |
|
---|
[6d5e378] | 93 | window_refresh(lbl->widget.window);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void deinit_label(label_t *lbl)
|
---|
| 98 | {
|
---|
| 99 | widget_deinit(&lbl->widget);
|
---|
| 100 | free(lbl->caption);
|
---|
| 101 | font_release(&lbl->font);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static void label_destroy(widget_t *widget)
|
---|
| 105 | {
|
---|
| 106 | label_t *lbl = (label_t *) widget;
|
---|
| 107 |
|
---|
[296e124e] | 108 | deinit_label(lbl);
|
---|
[6d5e378] | 109 | free(lbl);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | static void label_reconfigure(widget_t *widget)
|
---|
| 113 | {
|
---|
| 114 | /* no-op */
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | static void label_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 label_repaint(widget_t *widget)
|
---|
| 125 | {
|
---|
| 126 | paint_internal(widget);
|
---|
| 127 | window_damage(widget->window);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | static void label_handle_keyboard_event(widget_t *widget, kbd_event_t event)
|
---|
| 131 | {
|
---|
| 132 | /* no-op */
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | static void label_handle_position_event(widget_t *widget, pos_event_t event)
|
---|
| 136 | {
|
---|
| 137 | /* no-op */
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[296e124e] | 140 | bool init_label(label_t *lbl, widget_t *parent, const char *caption,
|
---|
| 141 | uint16_t points, pixel_t background, pixel_t text)
|
---|
[6d5e378] | 142 | {
|
---|
| 143 | widget_init(&lbl->widget, parent);
|
---|
[296e124e] | 144 |
|
---|
[6d5e378] | 145 | lbl->widget.destroy = label_destroy;
|
---|
| 146 | lbl->widget.reconfigure = label_reconfigure;
|
---|
| 147 | lbl->widget.rearrange = label_rearrange;
|
---|
| 148 | lbl->widget.repaint = label_repaint;
|
---|
| 149 | lbl->widget.handle_keyboard_event = label_handle_keyboard_event;
|
---|
| 150 | lbl->widget.handle_position_event = label_handle_position_event;
|
---|
[296e124e] | 151 |
|
---|
[6d5e378] | 152 | source_init(&lbl->background);
|
---|
| 153 | source_set_color(&lbl->background, background);
|
---|
[296e124e] | 154 |
|
---|
| 155 | source_init(&lbl->text);
|
---|
| 156 | source_set_color(&lbl->text, text);
|
---|
| 157 |
|
---|
| 158 | if (caption == NULL)
|
---|
[6d5e378] | 159 | lbl->caption = NULL;
|
---|
[296e124e] | 160 | else
|
---|
[6d5e378] | 161 | lbl->caption = str_dup(caption);
|
---|
[296e124e] | 162 |
|
---|
[6d5e378] | 163 | font_init(&lbl->font, FONT_DECODER_EMBEDDED, NULL, points);
|
---|
[296e124e] | 164 |
|
---|
[6d5e378] | 165 | sysarg_t cpt_width;
|
---|
| 166 | sysarg_t cpt_height;
|
---|
| 167 | font_get_box(&lbl->font, lbl->caption, &cpt_width, &cpt_height);
|
---|
[296e124e] | 168 |
|
---|
[6d5e378] | 169 | lbl->widget.width_min = cpt_width + 4;
|
---|
| 170 | lbl->widget.height_min = cpt_height + 4;
|
---|
| 171 | lbl->widget.width_ideal = lbl->widget.width_min;
|
---|
| 172 | lbl->widget.height_ideal = lbl->widget.height_min;
|
---|
[296e124e] | 173 |
|
---|
[6d5e378] | 174 | lbl->rewrite = on_rewrite;
|
---|
[296e124e] | 175 |
|
---|
[6d5e378] | 176 | return true;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[296e124e] | 179 | label_t *create_label(widget_t *parent, const char *caption, uint16_t points,
|
---|
| 180 | pixel_t background, pixel_t text)
|
---|
[6d5e378] | 181 | {
|
---|
| 182 | label_t *lbl = (label_t *) malloc(sizeof(label_t));
|
---|
[296e124e] | 183 | if (!lbl)
|
---|
[6d5e378] | 184 | return NULL;
|
---|
[296e124e] | 185 |
|
---|
| 186 | if (init_label(lbl, parent, caption, points, background, text))
|
---|
[6d5e378] | 187 | return lbl;
|
---|
[296e124e] | 188 |
|
---|
| 189 | free(lbl);
|
---|
| 190 | return NULL;
|
---|
[6d5e378] | 191 | }
|
---|
| 192 |
|
---|
| 193 | /** @}
|
---|
| 194 | */
|
---|