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