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