1 | /*
|
---|
2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
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 libui
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Label
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <gfx/context.h>
|
---|
38 | #include <gfx/render.h>
|
---|
39 | #include <gfx/text.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <str.h>
|
---|
42 | #include <ui/control.h>
|
---|
43 | #include <ui/paint.h>
|
---|
44 | #include <ui/label.h>
|
---|
45 | #include "../private/label.h"
|
---|
46 | #include "../private/resource.h"
|
---|
47 |
|
---|
48 | static void ui_label_ctl_destroy(void *);
|
---|
49 | static errno_t ui_label_ctl_paint(void *);
|
---|
50 | static ui_evclaim_t ui_label_ctl_pos_event(void *, pos_event_t *);
|
---|
51 |
|
---|
52 | /** Label control ops */
|
---|
53 | ui_control_ops_t ui_label_ops = {
|
---|
54 | .destroy = ui_label_ctl_destroy,
|
---|
55 | .paint = ui_label_ctl_paint,
|
---|
56 | .pos_event = ui_label_ctl_pos_event
|
---|
57 | };
|
---|
58 |
|
---|
59 | /** Create new label.
|
---|
60 | *
|
---|
61 | * @param resource UI resource
|
---|
62 | * @param text Text
|
---|
63 | * @param rlabel Place to store pointer to new label
|
---|
64 | * @return EOK on success, ENOMEM if out of memory
|
---|
65 | */
|
---|
66 | errno_t ui_label_create(ui_resource_t *resource, const char *text,
|
---|
67 | ui_label_t **rlabel)
|
---|
68 | {
|
---|
69 | ui_label_t *label;
|
---|
70 | errno_t rc;
|
---|
71 |
|
---|
72 | label = calloc(1, sizeof(ui_label_t));
|
---|
73 | if (label == NULL)
|
---|
74 | return ENOMEM;
|
---|
75 |
|
---|
76 | rc = ui_control_new(&ui_label_ops, (void *) label, &label->control);
|
---|
77 | if (rc != EOK) {
|
---|
78 | free(label);
|
---|
79 | return rc;
|
---|
80 | }
|
---|
81 |
|
---|
82 | label->text = str_dup(text);
|
---|
83 | if (label->text == NULL) {
|
---|
84 | ui_control_delete(label->control);
|
---|
85 | free(label);
|
---|
86 | return ENOMEM;
|
---|
87 | }
|
---|
88 |
|
---|
89 | label->res = resource;
|
---|
90 | label->halign = gfx_halign_left;
|
---|
91 | *rlabel = label;
|
---|
92 | return EOK;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** Destroy label.
|
---|
96 | *
|
---|
97 | * @param label Label or @c NULL
|
---|
98 | */
|
---|
99 | void ui_label_destroy(ui_label_t *label)
|
---|
100 | {
|
---|
101 | if (label == NULL)
|
---|
102 | return;
|
---|
103 |
|
---|
104 | ui_control_delete(label->control);
|
---|
105 | free(label);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Get base control from label.
|
---|
109 | *
|
---|
110 | * @param label Label
|
---|
111 | * @return Control
|
---|
112 | */
|
---|
113 | ui_control_t *ui_label_ctl(ui_label_t *label)
|
---|
114 | {
|
---|
115 | return label->control;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Set label rectangle.
|
---|
119 | *
|
---|
120 | * @param label Label
|
---|
121 | * @param rect New label rectangle
|
---|
122 | */
|
---|
123 | void ui_label_set_rect(ui_label_t *label, gfx_rect_t *rect)
|
---|
124 | {
|
---|
125 | label->rect = *rect;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Set label horizontal text alignment.
|
---|
129 | *
|
---|
130 | * @param label Label
|
---|
131 | * @param halign Horizontal alignment
|
---|
132 | */
|
---|
133 | void ui_label_set_halign(ui_label_t *label, gfx_halign_t halign)
|
---|
134 | {
|
---|
135 | label->halign = halign;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Set label vertical text alignment.
|
---|
139 | *
|
---|
140 | * @param label Label
|
---|
141 | * @param valign Vertical alignment
|
---|
142 | */
|
---|
143 | void ui_label_set_valign(ui_label_t *label, gfx_valign_t valign)
|
---|
144 | {
|
---|
145 | label->valign = valign;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Set label text.
|
---|
149 | *
|
---|
150 | * @param label Label
|
---|
151 | * @param text New label text
|
---|
152 | * @return EOK on success, ENOMEM if out of memory
|
---|
153 | */
|
---|
154 | errno_t ui_label_set_text(ui_label_t *label, const char *text)
|
---|
155 | {
|
---|
156 | char *tcopy;
|
---|
157 |
|
---|
158 | tcopy = str_dup(text);
|
---|
159 | if (tcopy == NULL)
|
---|
160 | return ENOMEM;
|
---|
161 |
|
---|
162 | free(label->text);
|
---|
163 | label->text = tcopy;
|
---|
164 |
|
---|
165 | return EOK;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Paint label.
|
---|
169 | *
|
---|
170 | * @param label Label
|
---|
171 | * @return EOK on success or an error code
|
---|
172 | */
|
---|
173 | errno_t ui_label_paint(ui_label_t *label)
|
---|
174 | {
|
---|
175 | gfx_text_fmt_t fmt;
|
---|
176 | gfx_coord2_t pos;
|
---|
177 | errno_t rc;
|
---|
178 |
|
---|
179 | /* Paint label background */
|
---|
180 |
|
---|
181 | rc = gfx_set_color(label->res->gc, label->res->wnd_face_color);
|
---|
182 | if (rc != EOK)
|
---|
183 | goto error;
|
---|
184 |
|
---|
185 | rc = gfx_fill_rect(label->res->gc, &label->rect);
|
---|
186 | if (rc != EOK)
|
---|
187 | goto error;
|
---|
188 |
|
---|
189 | switch (label->halign) {
|
---|
190 | case gfx_halign_left:
|
---|
191 | case gfx_halign_justify:
|
---|
192 | pos.x = label->rect.p0.x;
|
---|
193 | break;
|
---|
194 | case gfx_halign_center:
|
---|
195 | pos.x = (label->rect.p0.x + label->rect.p1.x) / 2;
|
---|
196 | break;
|
---|
197 | case gfx_halign_right:
|
---|
198 | pos.x = label->rect.p1.x;
|
---|
199 | break;
|
---|
200 | }
|
---|
201 |
|
---|
202 | switch (label->valign) {
|
---|
203 | case gfx_valign_top:
|
---|
204 | pos.y = label->rect.p0.y;
|
---|
205 | break;
|
---|
206 | case gfx_valign_center:
|
---|
207 | pos.y = (label->rect.p0.y + label->rect.p1.y) / 2;
|
---|
208 | break;
|
---|
209 | case gfx_valign_bottom:
|
---|
210 | pos.y = label->rect.p1.y;
|
---|
211 | break;
|
---|
212 | case gfx_valign_baseline:
|
---|
213 | return EINVAL;
|
---|
214 | }
|
---|
215 |
|
---|
216 | gfx_text_fmt_init(&fmt);
|
---|
217 | fmt.font = label->res->font;
|
---|
218 | fmt.color = label->res->wnd_text_color;
|
---|
219 | fmt.halign = label->halign;
|
---|
220 | fmt.valign = label->valign;
|
---|
221 |
|
---|
222 | rc = gfx_puttext(&pos, &fmt, label->text);
|
---|
223 | if (rc != EOK)
|
---|
224 | goto error;
|
---|
225 |
|
---|
226 | rc = gfx_update(label->res->gc);
|
---|
227 | if (rc != EOK)
|
---|
228 | goto error;
|
---|
229 |
|
---|
230 | return EOK;
|
---|
231 | error:
|
---|
232 | return rc;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /** Destroy label control.
|
---|
236 | *
|
---|
237 | * @param arg Argument (ui_label_t *)
|
---|
238 | */
|
---|
239 | void ui_label_ctl_destroy(void *arg)
|
---|
240 | {
|
---|
241 | ui_label_t *label = (ui_label_t *) arg;
|
---|
242 |
|
---|
243 | ui_label_destroy(label);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /** Paint label control.
|
---|
247 | *
|
---|
248 | * @param arg Argument (ui_label_t *)
|
---|
249 | * @return EOK on success or an error code
|
---|
250 | */
|
---|
251 | errno_t ui_label_ctl_paint(void *arg)
|
---|
252 | {
|
---|
253 | ui_label_t *label = (ui_label_t *) arg;
|
---|
254 |
|
---|
255 | return ui_label_paint(label);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /** Handle label control position event.
|
---|
259 | *
|
---|
260 | * @param arg Argument (ui_label_t *)
|
---|
261 | * @param pos_event Position event
|
---|
262 | * @return @c ui_claimed iff the event is claimed
|
---|
263 | */
|
---|
264 | ui_evclaim_t ui_label_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
265 | {
|
---|
266 | ui_label_t *label = (ui_label_t *) arg;
|
---|
267 |
|
---|
268 | (void) label;
|
---|
269 | return ui_unclaimed;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /** @}
|
---|
273 | */
|
---|