source: mainline/uspace/lib/ui/src/label.c@ 65ec18d

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 65ec18d was b433f68, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Puttext needs to know the color of the text being printed

So far we were using the GC's current drawing color. But unless there
was a way to read it, we could not render text-mode text in the correct
color.

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[ba09d06]1/*
[2ab8ab3]2 * Copyright (c) 2021 Jiri Svoboda
[ba09d06]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>
[8009dc27]42#include <ui/control.h>
[ba09d06]43#include <ui/paint.h>
44#include <ui/label.h>
45#include "../private/label.h"
46#include "../private/resource.h"
47
[c6f00b40]48static void ui_label_ctl_destroy(void *);
[4df6607]49static errno_t ui_label_ctl_paint(void *);
[8009dc27]50static ui_evclaim_t ui_label_ctl_pos_event(void *, pos_event_t *);
51
52/** Label control ops */
53ui_control_ops_t ui_label_ops = {
[c6f00b40]54 .destroy = ui_label_ctl_destroy,
[4df6607]55 .paint = ui_label_ctl_paint,
[8009dc27]56 .pos_event = ui_label_ctl_pos_event
57};
58
[ba09d06]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 */
[3583ffb]66errno_t ui_label_create(ui_resource_t *resource, const char *text,
[ba09d06]67 ui_label_t **rlabel)
68{
69 ui_label_t *label;
[8009dc27]70 errno_t rc;
[ba09d06]71
72 label = calloc(1, sizeof(ui_label_t));
73 if (label == NULL)
74 return ENOMEM;
75
[8009dc27]76 rc = ui_control_new(&ui_label_ops, (void *) label, &label->control);
77 if (rc != EOK) {
78 free(label);
79 return rc;
80 }
81
[ba09d06]82 label->text = str_dup(text);
83 if (label->text == NULL) {
[8009dc27]84 ui_control_delete(label->control);
[ba09d06]85 free(label);
86 return ENOMEM;
87 }
88
[3583ffb]89 label->res = resource;
[58a67050]90 label->halign = gfx_halign_left;
[ba09d06]91 *rlabel = label;
92 return EOK;
93}
94
95/** Destroy label.
96 *
97 * @param label Label or @c NULL
98 */
99void ui_label_destroy(ui_label_t *label)
100{
101 if (label == NULL)
102 return;
103
[8009dc27]104 ui_control_delete(label->control);
[ba09d06]105 free(label);
106}
107
[8009dc27]108/** Get base control from label.
109 *
110 * @param label Label
111 * @return Control
112 */
113ui_control_t *ui_label_ctl(ui_label_t *label)
114{
115 return label->control;
116}
117
[ba09d06]118/** Set label rectangle.
119 *
120 * @param label Label
121 * @param rect New label rectangle
122 */
123void ui_label_set_rect(ui_label_t *label, gfx_rect_t *rect)
124{
125 label->rect = *rect;
126}
127
[58a67050]128/** Set label horizontal text alignment.
129 *
130 * @param label Label
131 * @param halign Horizontal alignment
132 */
133void ui_label_set_halign(ui_label_t *label, gfx_halign_t halign)
134{
135 label->halign = halign;
136}
137
[ba09d06]138/** Set label text.
139 *
140 * @param label Label
141 * @param text New label text
142 * @return EOK on success, ENOMEM if out of memory
143 */
144errno_t ui_label_set_text(ui_label_t *label, const char *text)
145{
146 char *tcopy;
147
148 tcopy = str_dup(text);
149 if (tcopy == NULL)
150 return ENOMEM;
151
152 free(label->text);
153 label->text = tcopy;
154
155 return EOK;
156}
157
158/** Paint label.
159 *
160 * @param label Label
161 * @return EOK on success or an error code
162 */
163errno_t ui_label_paint(ui_label_t *label)
164{
165 gfx_text_fmt_t fmt;
166 gfx_coord2_t pos;
167 errno_t rc;
168
169 /* Paint label background */
170
171 rc = gfx_set_color(label->res->gc, label->res->wnd_face_color);
172 if (rc != EOK)
173 goto error;
174
175 rc = gfx_fill_rect(label->res->gc, &label->rect);
176 if (rc != EOK)
177 goto error;
178
[58a67050]179 switch (label->halign) {
180 case gfx_halign_left:
181 case gfx_halign_justify:
182 pos.x = label->rect.p0.x;
183 break;
184 case gfx_halign_center:
185 pos.x = (label->rect.p0.x + label->rect.p1.x) / 2;
186 break;
187 case gfx_halign_right:
[b41564c]188 pos.x = label->rect.p1.x;
[58a67050]189 break;
190 }
191
192 pos.y = label->rect.p0.y;
[ba09d06]193
194 gfx_text_fmt_init(&fmt);
[b433f68]195 fmt.color = label->res->wnd_text_color;
[58a67050]196 fmt.halign = label->halign;
[ba09d06]197 fmt.valign = gfx_valign_top;
198
199 rc = gfx_puttext(label->res->font, &pos, &fmt, label->text);
200 if (rc != EOK)
201 goto error;
202
[2ab8ab3]203 rc = gfx_update(label->res->gc);
204 if (rc != EOK)
205 goto error;
206
[ba09d06]207 return EOK;
208error:
209 return rc;
210}
211
[c6f00b40]212/** Destroy label control.
213 *
214 * @param arg Argument (ui_label_t *)
215 */
216void ui_label_ctl_destroy(void *arg)
217{
218 ui_label_t *label = (ui_label_t *) arg;
219
220 ui_label_destroy(label);
221}
222
223/** Paint label control.
[4df6607]224 *
225 * @param arg Argument (ui_label_t *)
226 * @return EOK on success or an error code
227 */
228errno_t ui_label_ctl_paint(void *arg)
229{
230 ui_label_t *label = (ui_label_t *) arg;
231
232 return ui_label_paint(label);
233}
234
[8009dc27]235/** Handle label control position event.
236 *
237 * @param arg Argument (ui_label_t *)
238 * @param pos_event Position event
239 * @return @c ui_claimed iff the event is claimed
240 */
241ui_evclaim_t ui_label_ctl_pos_event(void *arg, pos_event_t *event)
242{
243 ui_label_t *label = (ui_label_t *) arg;
244
245 (void) label;
246 return ui_unclaimed;
247}
248
[ba09d06]249/** @}
250 */
Note: See TracBrowser for help on using the repository browser.