1 | /*
|
---|
2 | * Copyright (c) 2023 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 |
|
---|
46 | static void ui_fixed_ctl_destroy(void *);
|
---|
47 | static errno_t ui_fixed_ctl_paint(void *);
|
---|
48 | static ui_evclaim_t ui_fixed_ctl_kbd_event(void *, kbd_event_t *);
|
---|
49 | static ui_evclaim_t ui_fixed_ctl_pos_event(void *, pos_event_t *);
|
---|
50 | static void ui_fixed_ctl_unfocus(void *, unsigned);
|
---|
51 |
|
---|
52 | /** Push button control ops */
|
---|
53 | ui_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 | */
|
---|
66 | errno_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 | */
|
---|
90 | void 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 | */
|
---|
116 | ui_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 | */
|
---|
127 | errno_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 | */
|
---|
148 | void 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 | */
|
---|
166 | ui_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 | */
|
---|
182 | ui_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 | */
|
---|
198 | errno_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 | */
|
---|
221 | ui_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 | */
|
---|
244 | ui_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 | * @param nfocus Number of remaining foci
|
---|
265 | */
|
---|
266 | void ui_fixed_unfocus(ui_fixed_t *fixed, unsigned nfocus)
|
---|
267 | {
|
---|
268 | ui_fixed_elem_t *elem;
|
---|
269 |
|
---|
270 | elem = ui_fixed_first(fixed);
|
---|
271 | while (elem != NULL) {
|
---|
272 | ui_control_unfocus(elem->control, nfocus);
|
---|
273 |
|
---|
274 | elem = ui_fixed_next(elem);
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | /** Destroy fixed layout control.
|
---|
279 | *
|
---|
280 | * @param arg Argument (ui_fixed_t *)
|
---|
281 | */
|
---|
282 | void ui_fixed_ctl_destroy(void *arg)
|
---|
283 | {
|
---|
284 | ui_fixed_t *fixed = (ui_fixed_t *) arg;
|
---|
285 |
|
---|
286 | ui_fixed_destroy(fixed);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** Paint fixed layout control.
|
---|
290 | *
|
---|
291 | * @param arg Argument (ui_fixed_t *)
|
---|
292 | * @return EOK on success or an error code
|
---|
293 | */
|
---|
294 | errno_t ui_fixed_ctl_paint(void *arg)
|
---|
295 | {
|
---|
296 | ui_fixed_t *fixed = (ui_fixed_t *) arg;
|
---|
297 |
|
---|
298 | return ui_fixed_paint(fixed);
|
---|
299 | }
|
---|
300 |
|
---|
301 | /** Handle fixed layout control keyboard event.
|
---|
302 | *
|
---|
303 | * @param arg Argument (ui_fixed_t *)
|
---|
304 | * @param kbd_event Keyboard event
|
---|
305 | * @return @c ui_claimed iff the event is claimed
|
---|
306 | */
|
---|
307 | ui_evclaim_t ui_fixed_ctl_kbd_event(void *arg, kbd_event_t *event)
|
---|
308 | {
|
---|
309 | ui_fixed_t *fixed = (ui_fixed_t *) arg;
|
---|
310 |
|
---|
311 | return ui_fixed_kbd_event(fixed, event);
|
---|
312 | }
|
---|
313 |
|
---|
314 | /** Handle fixed layout control position event.
|
---|
315 | *
|
---|
316 | * @param arg Argument (ui_fixed_t *)
|
---|
317 | * @param pos_event Position event
|
---|
318 | * @return @c ui_claimed iff the event is claimed
|
---|
319 | */
|
---|
320 | ui_evclaim_t ui_fixed_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
321 | {
|
---|
322 | ui_fixed_t *fixed = (ui_fixed_t *) arg;
|
---|
323 |
|
---|
324 | return ui_fixed_pos_event(fixed, event);
|
---|
325 | }
|
---|
326 |
|
---|
327 | /** Handle fixed layout control window unfocus notification.
|
---|
328 | *
|
---|
329 | * @param arg Argument (ui_fixed_t *)
|
---|
330 | * @param nfocus Number of remaining foci
|
---|
331 | */
|
---|
332 | void ui_fixed_ctl_unfocus(void *arg, unsigned nfocus)
|
---|
333 | {
|
---|
334 | ui_fixed_t *fixed = (ui_fixed_t *) arg;
|
---|
335 |
|
---|
336 | ui_fixed_unfocus(fixed, nfocus);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** @}
|
---|
340 | */
|
---|