1 | /*
|
---|
2 | * Copyright (c) 2020 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 ui_evclaim_t ui_label_ctl_pos_event(void *, pos_event_t *);
|
---|
49 |
|
---|
50 | /** Label control ops */
|
---|
51 | ui_control_ops_t ui_label_ops = {
|
---|
52 | .pos_event = ui_label_ctl_pos_event
|
---|
53 | };
|
---|
54 |
|
---|
55 | /** Create new label.
|
---|
56 | *
|
---|
57 | * @param resource UI resource
|
---|
58 | * @param text Text
|
---|
59 | * @param rlabel Place to store pointer to new label
|
---|
60 | * @return EOK on success, ENOMEM if out of memory
|
---|
61 | */
|
---|
62 | errno_t ui_label_create(ui_resource_t *resource, const char *text,
|
---|
63 | ui_label_t **rlabel)
|
---|
64 | {
|
---|
65 | ui_label_t *label;
|
---|
66 | errno_t rc;
|
---|
67 |
|
---|
68 | label = calloc(1, sizeof(ui_label_t));
|
---|
69 | if (label == NULL)
|
---|
70 | return ENOMEM;
|
---|
71 |
|
---|
72 | rc = ui_control_new(&ui_label_ops, (void *) label, &label->control);
|
---|
73 | if (rc != EOK) {
|
---|
74 | free(label);
|
---|
75 | return rc;
|
---|
76 | }
|
---|
77 |
|
---|
78 | label->text = str_dup(text);
|
---|
79 | if (label->text == NULL) {
|
---|
80 | ui_control_delete(label->control);
|
---|
81 | free(label);
|
---|
82 | return ENOMEM;
|
---|
83 | }
|
---|
84 |
|
---|
85 | label->res = resource;
|
---|
86 | label->halign = gfx_halign_left;
|
---|
87 | *rlabel = label;
|
---|
88 | return EOK;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Destroy label.
|
---|
92 | *
|
---|
93 | * @param label Label or @c NULL
|
---|
94 | */
|
---|
95 | void ui_label_destroy(ui_label_t *label)
|
---|
96 | {
|
---|
97 | if (label == NULL)
|
---|
98 | return;
|
---|
99 |
|
---|
100 | ui_control_delete(label->control);
|
---|
101 | free(label);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Get base control from label.
|
---|
105 | *
|
---|
106 | * @param label Label
|
---|
107 | * @return Control
|
---|
108 | */
|
---|
109 | ui_control_t *ui_label_ctl(ui_label_t *label)
|
---|
110 | {
|
---|
111 | return label->control;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /** Set label rectangle.
|
---|
115 | *
|
---|
116 | * @param label Label
|
---|
117 | * @param rect New label rectangle
|
---|
118 | */
|
---|
119 | void ui_label_set_rect(ui_label_t *label, gfx_rect_t *rect)
|
---|
120 | {
|
---|
121 | label->rect = *rect;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** Set label horizontal text alignment.
|
---|
125 | *
|
---|
126 | * @param label Label
|
---|
127 | * @param halign Horizontal alignment
|
---|
128 | */
|
---|
129 | void ui_label_set_halign(ui_label_t *label, gfx_halign_t halign)
|
---|
130 | {
|
---|
131 | label->halign = halign;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Set label text.
|
---|
135 | *
|
---|
136 | * @param label Label
|
---|
137 | * @param text New label text
|
---|
138 | * @return EOK on success, ENOMEM if out of memory
|
---|
139 | */
|
---|
140 | errno_t ui_label_set_text(ui_label_t *label, const char *text)
|
---|
141 | {
|
---|
142 | char *tcopy;
|
---|
143 |
|
---|
144 | tcopy = str_dup(text);
|
---|
145 | if (tcopy == NULL)
|
---|
146 | return ENOMEM;
|
---|
147 |
|
---|
148 | free(label->text);
|
---|
149 | label->text = tcopy;
|
---|
150 |
|
---|
151 | return EOK;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Paint label.
|
---|
155 | *
|
---|
156 | * @param label Label
|
---|
157 | * @return EOK on success or an error code
|
---|
158 | */
|
---|
159 | errno_t ui_label_paint(ui_label_t *label)
|
---|
160 | {
|
---|
161 | gfx_text_fmt_t fmt;
|
---|
162 | gfx_coord2_t pos;
|
---|
163 | errno_t rc;
|
---|
164 |
|
---|
165 | /* Paint label background */
|
---|
166 |
|
---|
167 | rc = gfx_set_color(label->res->gc, label->res->wnd_face_color);
|
---|
168 | if (rc != EOK)
|
---|
169 | goto error;
|
---|
170 |
|
---|
171 | rc = gfx_fill_rect(label->res->gc, &label->rect);
|
---|
172 | if (rc != EOK)
|
---|
173 | goto error;
|
---|
174 |
|
---|
175 | switch (label->halign) {
|
---|
176 | case gfx_halign_left:
|
---|
177 | case gfx_halign_justify:
|
---|
178 | pos.x = label->rect.p0.x;
|
---|
179 | break;
|
---|
180 | case gfx_halign_center:
|
---|
181 | pos.x = (label->rect.p0.x + label->rect.p1.x) / 2;
|
---|
182 | break;
|
---|
183 | case gfx_halign_right:
|
---|
184 | pos.y = label->rect.p1.x;
|
---|
185 | break;
|
---|
186 | }
|
---|
187 |
|
---|
188 | pos.y = label->rect.p0.y;
|
---|
189 |
|
---|
190 | gfx_text_fmt_init(&fmt);
|
---|
191 | fmt.halign = label->halign;
|
---|
192 | fmt.valign = gfx_valign_top;
|
---|
193 |
|
---|
194 | rc = gfx_set_color(label->res->gc, label->res->wnd_text_color);
|
---|
195 | if (rc != EOK)
|
---|
196 | goto error;
|
---|
197 |
|
---|
198 | rc = gfx_puttext(label->res->font, &pos, &fmt, label->text);
|
---|
199 | if (rc != EOK)
|
---|
200 | goto error;
|
---|
201 |
|
---|
202 | return EOK;
|
---|
203 | error:
|
---|
204 | return rc;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /** Handle label control position event.
|
---|
208 | *
|
---|
209 | * @param arg Argument (ui_label_t *)
|
---|
210 | * @param pos_event Position event
|
---|
211 | * @return @c ui_claimed iff the event is claimed
|
---|
212 | */
|
---|
213 | ui_evclaim_t ui_label_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
214 | {
|
---|
215 | ui_label_t *label = (ui_label_t *) arg;
|
---|
216 |
|
---|
217 | (void) label;
|
---|
218 | return ui_unclaimed;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** @}
|
---|
222 | */
|
---|