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