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

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

Prototype control base class and fixed layout class

So far only position event delivery is handled via layout

  • Property mode set to 100644
File size: 4.2 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
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{
70 if (fixed == NULL)
71 return;
72
73 assert(list_empty(&fixed->elem));
74 free(fixed);
75}
76
77/** Add control to fixed layout.
78 *
79 * @param fixed Fixed layout
80 * @param control Control
81 * @return EOK on success, ENOMEM if out of memory
82 */
83errno_t ui_fixed_add(ui_fixed_t *fixed, ui_control_t *control)
84{
85 ui_fixed_elem_t *elem;
86
87 elem = calloc(1, sizeof(ui_fixed_elem_t));
88 if (elem == NULL)
89 return ENOMEM;
90
91 elem->fixed = fixed;
92 elem->control = control;
93 control->elemp = (void *) elem;
94 list_append(&elem->lelems, &fixed->elem);
95
96 return EOK;
97}
98
99/** Remove control from fixed layout.
100 *
101 * @param fixed Fixed layout
102 * @param control Control
103 */
104void ui_fixed_remove(ui_fixed_t *fixed, ui_control_t *control)
105{
106 ui_fixed_elem_t *elem;
107
108 elem = (ui_fixed_elem_t *) control->elemp;
109 assert(elem->fixed == fixed);
110
111 list_remove(&elem->lelems);
112 control->elemp = NULL;
113
114 free(elem);
115}
116
117/** Get first element of fixed layout.
118 *
119 * @param fixed Fixed layout
120 * @return First element or @c NULL
121 */
122ui_fixed_elem_t *ui_fixed_first(ui_fixed_t *fixed)
123{
124 link_t *link;
125
126 link = list_first(&fixed->elem);
127 if (link == NULL)
128 return NULL;
129
130 return list_get_instance(link, ui_fixed_elem_t, lelems);
131}
132
133/** Get next element of fixed layout.
134 *
135 * @param cur Current element
136 * @return Next element or @c NULL
137 */
138ui_fixed_elem_t *ui_fixed_next(ui_fixed_elem_t *cur)
139{
140 link_t *link;
141
142 link = list_next(&cur->lelems, &cur->fixed->elem);
143 if (link == NULL)
144 return NULL;
145
146 return list_get_instance(link, ui_fixed_elem_t, lelems);
147}
148
149/** Handle fixed layout position event.
150 *
151 * @param fixed Fixed layout
152 * @param pos_event Position event
153 * @return @c ui_claimed iff the event is claimed
154 */
155ui_evclaim_t ui_fixed_pos_event(ui_fixed_t *fixed, pos_event_t *event)
156{
157 ui_fixed_elem_t *elem;
158 ui_evclaim_t claimed;
159
160 elem = ui_fixed_first(fixed);
161 while (elem != NULL) {
162 claimed = ui_control_pos_event(elem->control, event);
163 if (claimed == ui_claimed)
164 return ui_claimed;
165
166 elem = ui_fixed_next(elem);
167 }
168
169 return ui_unclaimed;
170}
171
172/** @}
173 */
Note: See TracBrowser for help on using the repository browser.