source: mainline/uspace/lib/ui/src/label.c@ 4df6607

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

Paint controls via layout

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