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