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

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

Make fixed layout a UI control and hook it up to the window

  • Property mode set to 100644
File size: 6.1 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 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_pos_event(void *, pos_event_t *);
49
50/** Push button control ops */
51ui_control_ops_t ui_fixed_ops = {
52 .destroy = ui_fixed_ctl_destroy,
53 .paint = ui_fixed_ctl_paint,
54 .pos_event = ui_fixed_ctl_pos_event
55};
56
57/** Create new fixed layout.
58 *
59 * @param rfixed Place to store pointer to new fixed layout
60 * @return EOK on success, ENOMEM if out of memory
61 */
62errno_t ui_fixed_create(ui_fixed_t **rfixed)
63{
64 ui_fixed_t *fixed;
65 errno_t rc;
66
67 fixed = calloc(1, sizeof(ui_fixed_t));
68 if (fixed == NULL)
69 return ENOMEM;
70
71 rc = ui_control_new(&ui_fixed_ops, (void *) fixed, &fixed->control);
72 if (rc != EOK) {
73 free(fixed);
74 return rc;
75 }
76
77 list_initialize(&fixed->elem);
78 *rfixed = fixed;
79 return EOK;
80}
81
82/** Destroy fixed layout.
83 *
84 * @param fixed Fixed layout or @c NULL
85 */
86void ui_fixed_destroy(ui_fixed_t *fixed)
87{
88 ui_fixed_elem_t *elem;
89 ui_control_t *control;
90
91 if (fixed == NULL)
92 return;
93
94 elem = ui_fixed_first(fixed);
95 while (elem != NULL) {
96 control = elem->control;
97 ui_fixed_remove(fixed, control);
98 ui_control_destroy(control);
99
100 elem = ui_fixed_first(fixed);
101 }
102
103 ui_control_delete(fixed->control);
104 free(fixed);
105}
106
107/** Get base control from fixed layout.
108 *
109 * @param fixed Fixed layout
110 * @return Control
111 */
112ui_control_t *ui_fixed_ctl(ui_fixed_t *fixed)
113{
114 return fixed->control;
115}
116
117/** Add control to fixed layout.
118 *
119 * @param fixed Fixed layout
120 * @param control Control
121 * @return EOK on success, ENOMEM if out of memory
122 */
123errno_t ui_fixed_add(ui_fixed_t *fixed, ui_control_t *control)
124{
125 ui_fixed_elem_t *elem;
126
127 elem = calloc(1, sizeof(ui_fixed_elem_t));
128 if (elem == NULL)
129 return ENOMEM;
130
131 elem->fixed = fixed;
132 elem->control = control;
133 control->elemp = (void *) elem;
134 list_append(&elem->lelems, &fixed->elem);
135
136 return EOK;
137}
138
139/** Remove control from fixed layout.
140 *
141 * @param fixed Fixed layout
142 * @param control Control
143 */
144void ui_fixed_remove(ui_fixed_t *fixed, ui_control_t *control)
145{
146 ui_fixed_elem_t *elem;
147
148 elem = (ui_fixed_elem_t *) control->elemp;
149 assert(elem->fixed == fixed);
150
151 list_remove(&elem->lelems);
152 control->elemp = NULL;
153
154 free(elem);
155}
156
157/** Get first element of fixed layout.
158 *
159 * @param fixed Fixed layout
160 * @return First element or @c NULL
161 */
162ui_fixed_elem_t *ui_fixed_first(ui_fixed_t *fixed)
163{
164 link_t *link;
165
166 link = list_first(&fixed->elem);
167 if (link == NULL)
168 return NULL;
169
170 return list_get_instance(link, ui_fixed_elem_t, lelems);
171}
172
173/** Get next element of fixed layout.
174 *
175 * @param cur Current element
176 * @return Next element or @c NULL
177 */
178ui_fixed_elem_t *ui_fixed_next(ui_fixed_elem_t *cur)
179{
180 link_t *link;
181
182 link = list_next(&cur->lelems, &cur->fixed->elem);
183 if (link == NULL)
184 return NULL;
185
186 return list_get_instance(link, ui_fixed_elem_t, lelems);
187}
188
189/** Paint fixed layout.
190 *
191 * @param fixed Fixed layout
192 * @return EOK on success or an error code
193 */
194errno_t ui_fixed_paint(ui_fixed_t *fixed)
195{
196 ui_fixed_elem_t *elem;
197 errno_t rc;
198
199 elem = ui_fixed_first(fixed);
200 while (elem != NULL) {
201 rc = ui_control_paint(elem->control);
202 if (rc != EOK)
203 return rc;
204
205 elem = ui_fixed_next(elem);
206 }
207
208 return EOK;
209}
210
211/** Handle fixed layout position event.
212 *
213 * @param fixed Fixed layout
214 * @param pos_event Position event
215 * @return @c ui_claimed iff the event is claimed
216 */
217ui_evclaim_t ui_fixed_pos_event(ui_fixed_t *fixed, pos_event_t *event)
218{
219 ui_fixed_elem_t *elem;
220 ui_evclaim_t claimed;
221
222 elem = ui_fixed_first(fixed);
223 while (elem != NULL) {
224 claimed = ui_control_pos_event(elem->control, event);
225 if (claimed == ui_claimed)
226 return ui_claimed;
227
228 elem = ui_fixed_next(elem);
229 }
230
231 return ui_unclaimed;
232}
233
234/** Destroy fixed layout control.
235 *
236 * @param arg Argument (ui_fixed_t *)
237 */
238void ui_fixed_ctl_destroy(void *arg)
239{
240 ui_fixed_t *fixed = (ui_fixed_t *) arg;
241
242 ui_fixed_destroy(fixed);
243}
244
245/** Paint fixed layout control.
246 *
247 * @param arg Argument (ui_fixed_t *)
248 * @return EOK on success or an error code
249 */
250errno_t ui_fixed_ctl_paint(void *arg)
251{
252 ui_fixed_t *fixed = (ui_fixed_t *) arg;
253
254 return ui_fixed_paint(fixed);
255}
256
257/** Handle fixed layout control position event.
258 *
259 * @param arg Argument (ui_fixed_t *)
260 * @param pos_event Position event
261 * @return @c ui_claimed iff the event is claimed
262 */
263ui_evclaim_t ui_fixed_ctl_pos_event(void *arg, pos_event_t *event)
264{
265 ui_fixed_t *fixed = (ui_fixed_t *) arg;
266
267 return ui_fixed_pos_event(fixed, event);
268}
269
270/** @}
271 */
Note: See TracBrowser for help on using the repository browser.