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

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

UI label

  • Property mode set to 100644
File size: 3.6 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/paint.h>
43#include <ui/label.h>
44#include "../private/label.h"
45#include "../private/resource.h"
46
47/** Create new label.
48 *
49 * @param resource UI resource
50 * @param text Text
51 * @param rlabel Place to store pointer to new label
52 * @return EOK on success, ENOMEM if out of memory
53 */
54errno_t ui_label_create(ui_resource_t *resource, const char *text,
55 ui_label_t **rlabel)
56{
57 ui_label_t *label;
58
59 label = calloc(1, sizeof(ui_label_t));
60 if (label == NULL)
61 return ENOMEM;
62
63 label->text = str_dup(text);
64 if (label->text == NULL) {
65 free(label);
66 return ENOMEM;
67 }
68
69 label->res = resource;
70 *rlabel = label;
71 return EOK;
72}
73
74/** Destroy label.
75 *
76 * @param label Label or @c NULL
77 */
78void ui_label_destroy(ui_label_t *label)
79{
80 if (label == NULL)
81 return;
82
83 free(label);
84}
85
86/** Set label rectangle.
87 *
88 * @param label Label
89 * @param rect New label rectangle
90 */
91void ui_label_set_rect(ui_label_t *label, gfx_rect_t *rect)
92{
93 label->rect = *rect;
94}
95
96/** Set label text.
97 *
98 * @param label Label
99 * @param text New label text
100 * @return EOK on success, ENOMEM if out of memory
101 */
102errno_t ui_label_set_text(ui_label_t *label, const char *text)
103{
104 char *tcopy;
105
106 tcopy = str_dup(text);
107 if (tcopy == NULL)
108 return ENOMEM;
109
110 free(label->text);
111 label->text = tcopy;
112
113 return EOK;
114}
115
116/** Paint label.
117 *
118 * @param label Label
119 * @return EOK on success or an error code
120 */
121errno_t ui_label_paint(ui_label_t *label)
122{
123 gfx_text_fmt_t fmt;
124 gfx_coord2_t pos;
125 errno_t rc;
126
127 /* Paint label background */
128
129 rc = gfx_set_color(label->res->gc, label->res->wnd_face_color);
130 if (rc != EOK)
131 goto error;
132
133 rc = gfx_fill_rect(label->res->gc, &label->rect);
134 if (rc != EOK)
135 goto error;
136
137 pos = label->rect.p0;
138
139 gfx_text_fmt_init(&fmt);
140 fmt.halign = gfx_halign_left;
141 fmt.valign = gfx_valign_top;
142
143 rc = gfx_set_color(label->res->gc, label->res->wnd_text_color);
144 if (rc != EOK)
145 goto error;
146
147 rc = gfx_puttext(label->res->font, &pos, &fmt, label->text);
148 if (rc != EOK)
149 goto error;
150
151 return EOK;
152error:
153 return rc;
154}
155
156/** @}
157 */
Note: See TracBrowser for help on using the repository browser.