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