source: mainline/uspace/lib/ui/src/fixed.c@ bfc0fc6

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

Basic editable text entry

  • Property mode set to 100644
File size: 7.6 KB
Line 
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 Fixed layout
34 */
35
36#include <adt/list.h>
37#include <assert.h>
38#include <errno.h>
39#include <io/pos_event.h>
40#include <stdlib.h>
41#include <ui/control.h>
42#include <ui/fixed.h>
43#include "../private/control.h"
44#include "../private/fixed.h"
45
46static void ui_fixed_ctl_destroy(void *);
47static errno_t ui_fixed_ctl_paint(void *);
48static ui_evclaim_t ui_fixed_ctl_kbd_event(void *, kbd_event_t *);
49static ui_evclaim_t ui_fixed_ctl_pos_event(void *, pos_event_t *);
50static void ui_fixed_ctl_unfocus(void *);
51
52/** Push button control ops */
53ui_control_ops_t ui_fixed_ops = {
54 .destroy = ui_fixed_ctl_destroy,
55 .paint = ui_fixed_ctl_paint,
56 .kbd_event = ui_fixed_ctl_kbd_event,
57 .pos_event = ui_fixed_ctl_pos_event,
58 .unfocus = ui_fixed_ctl_unfocus
59};
60
61/** Create new fixed layout.
62 *
63 * @param rfixed Place to store pointer to new fixed layout
64 * @return EOK on success, ENOMEM if out of memory
65 */
66errno_t ui_fixed_create(ui_fixed_t **rfixed)
67{
68 ui_fixed_t *fixed;
69 errno_t rc;
70
71 fixed = calloc(1, sizeof(ui_fixed_t));
72 if (fixed == NULL)
73 return ENOMEM;
74
75 rc = ui_control_new(&ui_fixed_ops, (void *) fixed, &fixed->control);
76 if (rc != EOK) {
77 free(fixed);
78 return rc;
79 }
80
81 list_initialize(&fixed->elem);
82 *rfixed = fixed;
83 return EOK;
84}
85
86/** Destroy fixed layout.
87 *
88 * @param fixed Fixed layout or @c NULL
89 */
90void ui_fixed_destroy(ui_fixed_t *fixed)
91{
92 ui_fixed_elem_t *elem;
93 ui_control_t *control;
94
95 if (fixed == NULL)
96 return;
97
98 elem = ui_fixed_first(fixed);
99 while (elem != NULL) {
100 control = elem->control;
101 ui_fixed_remove(fixed, control);
102 ui_control_destroy(control);
103
104 elem = ui_fixed_first(fixed);
105 }
106
107 ui_control_delete(fixed->control);
108 free(fixed);
109}
110
111/** Get base control from fixed layout.
112 *
113 * @param fixed Fixed layout
114 * @return Control
115 */
116ui_control_t *ui_fixed_ctl(ui_fixed_t *fixed)
117{
118 return fixed->control;
119}
120
121/** Add control to fixed layout.
122 *
123 * @param fixed Fixed layout
124 * @param control Control
125 * @return EOK on success, ENOMEM if out of memory
126 */
127errno_t ui_fixed_add(ui_fixed_t *fixed, ui_control_t *control)
128{
129 ui_fixed_elem_t *elem;
130
131 elem = calloc(1, sizeof(ui_fixed_elem_t));
132 if (elem == NULL)
133 return ENOMEM;
134
135 elem->fixed = fixed;
136 elem->control = control;
137 control->elemp = (void *) elem;
138 list_append(&elem->lelems, &fixed->elem);
139
140 return EOK;
141}
142
143/** Remove control from fixed layout.
144 *
145 * @param fixed Fixed layout
146 * @param control Control
147 */
148void ui_fixed_remove(ui_fixed_t *fixed, ui_control_t *control)
149{
150 ui_fixed_elem_t *elem;
151
152 elem = (ui_fixed_elem_t *) control->elemp;
153 assert(elem->fixed == fixed);
154
155 list_remove(&elem->lelems);
156 control->elemp = NULL;
157
158 free(elem);
159}
160
161/** Get first element of fixed layout.
162 *
163 * @param fixed Fixed layout
164 * @return First element or @c NULL
165 */
166ui_fixed_elem_t *ui_fixed_first(ui_fixed_t *fixed)
167{
168 link_t *link;
169
170 link = list_first(&fixed->elem);
171 if (link == NULL)
172 return NULL;
173
174 return list_get_instance(link, ui_fixed_elem_t, lelems);
175}
176
177/** Get next element of fixed layout.
178 *
179 * @param cur Current element
180 * @return Next element or @c NULL
181 */
182ui_fixed_elem_t *ui_fixed_next(ui_fixed_elem_t *cur)
183{
184 link_t *link;
185
186 link = list_next(&cur->lelems, &cur->fixed->elem);
187 if (link == NULL)
188 return NULL;
189
190 return list_get_instance(link, ui_fixed_elem_t, lelems);
191}
192
193/** Paint fixed layout.
194 *
195 * @param fixed Fixed layout
196 * @return EOK on success or an error code
197 */
198errno_t ui_fixed_paint(ui_fixed_t *fixed)
199{
200 ui_fixed_elem_t *elem;
201 errno_t rc;
202
203 elem = ui_fixed_first(fixed);
204 while (elem != NULL) {
205 rc = ui_control_paint(elem->control);
206 if (rc != EOK)
207 return rc;
208
209 elem = ui_fixed_next(elem);
210 }
211
212 return EOK;
213}
214
215/** Handle fixed layout keyboard event.
216 *
217 * @param fixed Fixed layout
218 * @param kbd_event Keyboard event
219 * @return @c ui_claimed iff the event is claimed
220 */
221ui_evclaim_t ui_fixed_kbd_event(ui_fixed_t *fixed, kbd_event_t *event)
222{
223 ui_fixed_elem_t *elem;
224 ui_evclaim_t claimed;
225
226 elem = ui_fixed_first(fixed);
227 while (elem != NULL) {
228 claimed = ui_control_kbd_event(elem->control, event);
229 if (claimed == ui_claimed)
230 return ui_claimed;
231
232 elem = ui_fixed_next(elem);
233 }
234
235 return ui_unclaimed;
236}
237
238/** Handle fixed layout position event.
239 *
240 * @param fixed Fixed layout
241 * @param pos_event Position event
242 * @return @c ui_claimed iff the event is claimed
243 */
244ui_evclaim_t ui_fixed_pos_event(ui_fixed_t *fixed, pos_event_t *event)
245{
246 ui_fixed_elem_t *elem;
247 ui_evclaim_t claimed;
248
249 elem = ui_fixed_first(fixed);
250 while (elem != NULL) {
251 claimed = ui_control_pos_event(elem->control, event);
252 if (claimed == ui_claimed)
253 return ui_claimed;
254
255 elem = ui_fixed_next(elem);
256 }
257
258 return ui_unclaimed;
259}
260
261/** Handle fixed layout window unfocus notification.
262 *
263 * @param fixed Fixed layout
264 */
265void ui_fixed_unfocus(ui_fixed_t *fixed)
266{
267 ui_fixed_elem_t *elem;
268
269 elem = ui_fixed_first(fixed);
270 while (elem != NULL) {
271 ui_control_unfocus(elem->control);
272
273 elem = ui_fixed_next(elem);
274 }
275}
276
277/** Destroy fixed layout control.
278 *
279 * @param arg Argument (ui_fixed_t *)
280 */
281void ui_fixed_ctl_destroy(void *arg)
282{
283 ui_fixed_t *fixed = (ui_fixed_t *) arg;
284
285 ui_fixed_destroy(fixed);
286}
287
288/** Paint fixed layout control.
289 *
290 * @param arg Argument (ui_fixed_t *)
291 * @return EOK on success or an error code
292 */
293errno_t ui_fixed_ctl_paint(void *arg)
294{
295 ui_fixed_t *fixed = (ui_fixed_t *) arg;
296
297 return ui_fixed_paint(fixed);
298}
299
300/** Handle fixed layout control keyboard event.
301 *
302 * @param arg Argument (ui_fixed_t *)
303 * @param kbd_event Keyboard event
304 * @return @c ui_claimed iff the event is claimed
305 */
306ui_evclaim_t ui_fixed_ctl_kbd_event(void *arg, kbd_event_t *event)
307{
308 ui_fixed_t *fixed = (ui_fixed_t *) arg;
309
310 return ui_fixed_kbd_event(fixed, event);
311}
312
313/** Handle fixed layout control position event.
314 *
315 * @param arg Argument (ui_fixed_t *)
316 * @param pos_event Position event
317 * @return @c ui_claimed iff the event is claimed
318 */
319ui_evclaim_t ui_fixed_ctl_pos_event(void *arg, pos_event_t *event)
320{
321 ui_fixed_t *fixed = (ui_fixed_t *) arg;
322
323 return ui_fixed_pos_event(fixed, event);
324}
325
326/** Handle fixed layout control window unfocus notification.
327 *
328 * @param arg Argument (ui_fixed_t *)
329 */
330void ui_fixed_ctl_unfocus(void *arg)
331{
332 ui_fixed_t *fixed = (ui_fixed_t *) arg;
333
334 ui_fixed_unfocus(fixed);
335}
336
337/** @}
338 */
Note: See TracBrowser for help on using the repository browser.