source: mainline/uspace/lib/gui/label.c@ a163d10

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a163d10 was 2bb6d04, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Curb the proliferation of libdraw headers

libdraw provides a lot of ambiguously named headers, which makes it
confusing. This change merges the subdirectories into single headers,
and moves all headers into draw subdirectory, so that it's obvious
at a glance what library the header belongs to.

Compare:

#include <path.h>
#include <source.h>
#include <font/bitmap_backend.h>
#include <font/pcf.h>

vs.

#include <draw/path.h>
#include <draw/source.h>
#include <draw/font.h>

  • Property mode set to 100644
File size: 5.3 KB
Line 
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 <draw/drawctx.h>
38#include <stdlib.h>
39#include <draw/surface.h>
40#include <draw/font.h>
41#include <errno.h>
42#include "window.h"
43#include "label.h"
44
45static void paint_internal(widget_t *widget)
46{
47 label_t *lbl = (label_t *) widget;
48
49 surface_t *surface = window_claim(lbl->widget.window);
50 if (!surface)
51 window_yield(lbl->widget.window);
52
53 drawctx_t drawctx;
54 drawctx_init(&drawctx, surface);
55
56 drawctx_set_source(&drawctx, &lbl->background);
57 drawctx_transfer(&drawctx, widget->hpos, widget->vpos, widget->width,
58 widget->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
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;
67
68 drawctx_set_source(&drawctx, &lbl->text);
69 drawctx_set_font(&drawctx, lbl->font);
70
71 if (lbl->caption)
72 drawctx_print(&drawctx, lbl->caption, x, y);
73 }
74
75 window_yield(lbl->widget.window);
76}
77
78static void on_rewrite(widget_t *widget, void *data)
79{
80 if (data != NULL) {
81 label_t *lbl = (label_t *) widget;
82
83 const char *new_caption = (const char *) data;
84 lbl->caption = str_dup(new_caption);
85
86 sysarg_t cpt_width;
87 sysarg_t cpt_height;
88 font_get_box(lbl->font, lbl->caption, &cpt_width, &cpt_height);
89
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;
94
95 window_refresh(lbl->widget.window);
96 }
97}
98
99void deinit_label(label_t *lbl)
100{
101 widget_deinit(&lbl->widget);
102 free(lbl->caption);
103 font_release(lbl->font);
104}
105
106static void label_destroy(widget_t *widget)
107{
108 label_t *lbl = (label_t *) widget;
109
110 deinit_label(lbl);
111 free(lbl);
112}
113
114static void label_reconfigure(widget_t *widget)
115{
116 /* no-op */
117}
118
119static 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
126static void label_repaint(widget_t *widget)
127{
128 paint_internal(widget);
129 window_damage(widget->window);
130}
131
132static void label_handle_keyboard_event(widget_t *widget, kbd_event_t event)
133{
134 /* no-op */
135}
136
137static void label_handle_position_event(widget_t *widget, pos_event_t event)
138{
139 /* no-op */
140}
141
142bool init_label(label_t *lbl, widget_t *parent, const void *data,
143 const char *caption, uint16_t points, pixel_t background, pixel_t text)
144{
145 widget_init(&lbl->widget, parent, data);
146
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;
153
154 source_init(&lbl->background);
155 source_set_color(&lbl->background, background);
156
157 source_init(&lbl->text);
158 source_set_color(&lbl->text, text);
159
160 if (caption == NULL)
161 lbl->caption = NULL;
162 else
163 lbl->caption = str_dup(caption);
164
165 errno_t rc = embedded_font_create(&lbl->font, points);
166 if (rc != EOK) {
167 free(lbl->caption);
168 lbl->caption = NULL;
169 return false;
170 }
171
172 sysarg_t cpt_width;
173 sysarg_t cpt_height;
174 font_get_box(lbl->font, lbl->caption, &cpt_width, &cpt_height);
175
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;
180
181 lbl->rewrite = on_rewrite;
182
183 return true;
184}
185
186label_t *create_label(widget_t *parent, const void *data, const char *caption,
187 uint16_t points, pixel_t background, pixel_t text)
188{
189 label_t *lbl = (label_t *) malloc(sizeof(label_t));
190 if (!lbl)
191 return NULL;
192
193 if (init_label(lbl, parent, data, caption, points, background, text))
194 return lbl;
195
196 free(lbl);
197 return NULL;
198}
199
200/** @}
201 */
Note: See TracBrowser for help on using the repository browser.