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

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

Add virtual destructor for UI control

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[8009dc27]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
46/** Create new fixed layout.
47 *
48 * @param rfixed Place to store pointer to new fixed layout
49 * @return EOK on success, ENOMEM if out of memory
50 */
51errno_t ui_fixed_create(ui_fixed_t **rfixed)
52{
53 ui_fixed_t *fixed;
54
55 fixed = calloc(1, sizeof(ui_fixed_t));
56 if (fixed == NULL)
57 return ENOMEM;
58
59 list_initialize(&fixed->elem);
60 *rfixed = fixed;
61 return EOK;
62}
63
64/** Destroy fixed layout.
65 *
66 * @param fixed Fixed layout or @c NULL
67 */
68void ui_fixed_destroy(ui_fixed_t *fixed)
69{
[c6f00b40]70 ui_fixed_elem_t *elem;
71 ui_control_t *control;
72
[8009dc27]73 if (fixed == NULL)
74 return;
75
[c6f00b40]76 elem = ui_fixed_first(fixed);
77 while (elem != NULL) {
78 control = elem->control;
79 ui_fixed_remove(fixed, control);
80 ui_control_destroy(control);
81
82 elem = ui_fixed_first(fixed);
83 }
84
[8009dc27]85 free(fixed);
86}
87
88/** Add control to fixed layout.
89 *
90 * @param fixed Fixed layout
91 * @param control Control
92 * @return EOK on success, ENOMEM if out of memory
93 */
94errno_t ui_fixed_add(ui_fixed_t *fixed, ui_control_t *control)
95{
96 ui_fixed_elem_t *elem;
97
98 elem = calloc(1, sizeof(ui_fixed_elem_t));
99 if (elem == NULL)
100 return ENOMEM;
101
102 elem->fixed = fixed;
103 elem->control = control;
104 control->elemp = (void *) elem;
105 list_append(&elem->lelems, &fixed->elem);
106
107 return EOK;
108}
109
110/** Remove control from fixed layout.
111 *
112 * @param fixed Fixed layout
113 * @param control Control
114 */
115void ui_fixed_remove(ui_fixed_t *fixed, ui_control_t *control)
116{
117 ui_fixed_elem_t *elem;
118
119 elem = (ui_fixed_elem_t *) control->elemp;
120 assert(elem->fixed == fixed);
121
122 list_remove(&elem->lelems);
123 control->elemp = NULL;
124
125 free(elem);
126}
127
128/** Get first element of fixed layout.
129 *
130 * @param fixed Fixed layout
131 * @return First element or @c NULL
132 */
133ui_fixed_elem_t *ui_fixed_first(ui_fixed_t *fixed)
134{
135 link_t *link;
136
137 link = list_first(&fixed->elem);
138 if (link == NULL)
139 return NULL;
140
141 return list_get_instance(link, ui_fixed_elem_t, lelems);
142}
143
144/** Get next element of fixed layout.
145 *
146 * @param cur Current element
147 * @return Next element or @c NULL
148 */
149ui_fixed_elem_t *ui_fixed_next(ui_fixed_elem_t *cur)
150{
151 link_t *link;
152
153 link = list_next(&cur->lelems, &cur->fixed->elem);
154 if (link == NULL)
155 return NULL;
156
157 return list_get_instance(link, ui_fixed_elem_t, lelems);
158}
159
[4df6607]160/** Paint fixed layout.
161 *
162 * @param fixed Fixed layout
163 * @return EOK on success or an error code
164 */
165errno_t ui_fixed_paint(ui_fixed_t *fixed)
166{
167 ui_fixed_elem_t *elem;
168 errno_t rc;
169
170 elem = ui_fixed_first(fixed);
171 while (elem != NULL) {
172 rc = ui_control_paint(elem->control);
173 if (rc != EOK)
174 return rc;
175
176 elem = ui_fixed_next(elem);
177 }
178
179 return EOK;
180}
181
[8009dc27]182/** Handle fixed layout position event.
183 *
184 * @param fixed Fixed layout
185 * @param pos_event Position event
186 * @return @c ui_claimed iff the event is claimed
187 */
188ui_evclaim_t ui_fixed_pos_event(ui_fixed_t *fixed, pos_event_t *event)
189{
190 ui_fixed_elem_t *elem;
191 ui_evclaim_t claimed;
192
193 elem = ui_fixed_first(fixed);
194 while (elem != NULL) {
195 claimed = ui_control_pos_event(elem->control, event);
196 if (claimed == ui_claimed)
197 return ui_claimed;
198
199 elem = ui_fixed_next(elem);
200 }
201
202 return ui_unclaimed;
203}
204
205/** @}
206 */
Note: See TracBrowser for help on using the repository browser.