source: mainline/uspace/lib/ui/src/entry.c@ f14a900

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

Check box

  • Property mode set to 100644
File size: 5.9 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 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
51static void ui_entry_ctl_destroy(void *);
52static errno_t ui_entry_ctl_paint(void *);
53static ui_evclaim_t ui_entry_ctl_pos_event(void *, pos_event_t *);
54
55enum {
56 ui_entry_hpad = 4,
57 ui_entry_vpad = 4
58};
59
60/** Text entry control ops */
61ui_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 */
74errno_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 */
107void 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 */
121ui_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 */
131void 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 */
141void 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 */
152errno_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 */
171errno_t ui_entry_paint(ui_entry_t *entry)
172{
173 gfx_text_fmt_t fmt;
174 gfx_coord2_t pos;
175 gfx_rect_t inside;
176 errno_t rc;
177
178 /* Paint inset frame */
179
180 rc = ui_paint_inset_frame(entry->res, &entry->rect, &inside);
181 if (rc != EOK)
182 goto error;
183
184 /* Paint entry background */
185
186 rc = gfx_set_color(entry->res->gc, entry->res->entry_bg_color);
187 if (rc != EOK)
188 goto error;
189
190 rc = gfx_fill_rect(entry->res->gc, &inside);
191 if (rc != EOK)
192 goto error;
193
194 switch (entry->halign) {
195 case gfx_halign_left:
196 case gfx_halign_justify:
197 pos.x = inside.p0.x + ui_entry_hpad;
198 break;
199 case gfx_halign_center:
200 pos.x = (inside.p0.x + inside.p1.x) / 2;
201 break;
202 case gfx_halign_right:
203 pos.x = inside.p1.x - ui_entry_hpad;
204 break;
205 }
206
207 pos.y = inside.p0.y + ui_entry_vpad;
208
209 gfx_text_fmt_init(&fmt);
210 fmt.halign = entry->halign;
211 fmt.valign = gfx_valign_top;
212
213 rc = gfx_set_color(entry->res->gc, entry->res->entry_fg_color);
214 if (rc != EOK)
215 goto error;
216
217 rc = gfx_puttext(entry->res->font, &pos, &fmt, entry->text);
218 if (rc != EOK)
219 goto error;
220
221 return EOK;
222error:
223 return rc;
224}
225
226/** Destroy text entry control.
227 *
228 * @param arg Argument (ui_entry_t *)
229 */
230void ui_entry_ctl_destroy(void *arg)
231{
232 ui_entry_t *entry = (ui_entry_t *) arg;
233
234 ui_entry_destroy(entry);
235}
236
237/** Paint text entry control.
238 *
239 * @param arg Argument (ui_entry_t *)
240 * @return EOK on success or an error code
241 */
242errno_t ui_entry_ctl_paint(void *arg)
243{
244 ui_entry_t *entry = (ui_entry_t *) arg;
245
246 return ui_entry_paint(entry);
247}
248
249/** Handle text entry control position event.
250 *
251 * @param arg Argument (ui_entry_t *)
252 * @param pos_event Position event
253 * @return @c ui_claimed iff the event is claimed
254 */
255ui_evclaim_t ui_entry_ctl_pos_event(void *arg, pos_event_t *event)
256{
257 ui_entry_t *entry = (ui_entry_t *) arg;
258
259 (void) entry;
260 return ui_unclaimed;
261}
262
263/** @}
264 */
Note: See TracBrowser for help on using the repository browser.