[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 | */
|
---|
| 51 | errno_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 | */
|
---|
| 68 | void 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 | */
|
---|
| 83 | errno_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 | */
|
---|
| 104 | void 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 | */
|
---|
| 122 | ui_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 | */
|
---|
| 138 | ui_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 |
|
---|
[4df6607] | 149 | /** Paint fixed layout.
|
---|
| 150 | *
|
---|
| 151 | * @param fixed Fixed layout
|
---|
| 152 | * @return EOK on success or an error code
|
---|
| 153 | */
|
---|
| 154 | errno_t ui_fixed_paint(ui_fixed_t *fixed)
|
---|
| 155 | {
|
---|
| 156 | ui_fixed_elem_t *elem;
|
---|
| 157 | errno_t rc;
|
---|
| 158 |
|
---|
| 159 | elem = ui_fixed_first(fixed);
|
---|
| 160 | while (elem != NULL) {
|
---|
| 161 | rc = ui_control_paint(elem->control);
|
---|
| 162 | if (rc != EOK)
|
---|
| 163 | return rc;
|
---|
| 164 |
|
---|
| 165 | elem = ui_fixed_next(elem);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | return EOK;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[8009dc27] | 171 | /** Handle fixed layout position event.
|
---|
| 172 | *
|
---|
| 173 | * @param fixed Fixed layout
|
---|
| 174 | * @param pos_event Position event
|
---|
| 175 | * @return @c ui_claimed iff the event is claimed
|
---|
| 176 | */
|
---|
| 177 | ui_evclaim_t ui_fixed_pos_event(ui_fixed_t *fixed, pos_event_t *event)
|
---|
| 178 | {
|
---|
| 179 | ui_fixed_elem_t *elem;
|
---|
| 180 | ui_evclaim_t claimed;
|
---|
| 181 |
|
---|
| 182 | elem = ui_fixed_first(fixed);
|
---|
| 183 | while (elem != NULL) {
|
---|
| 184 | claimed = ui_control_pos_event(elem->control, event);
|
---|
| 185 | if (claimed == ui_claimed)
|
---|
| 186 | return ui_claimed;
|
---|
| 187 |
|
---|
| 188 | elem = ui_fixed_next(elem);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | return ui_unclaimed;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /** @}
|
---|
| 195 | */
|
---|