2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
35 #include "../ui/tree.h"
39 UiObserver* ui_observer_new(ui_callback f, void *data) {
40 UiObserver *observer = malloc(sizeof(UiObserver));
41 observer->callback = f;
42 observer->data = data;
43 observer->next = NULL;
47 UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer) {
60 UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data) {
61 UiObserver *observer = ui_observer_new(f, data);
62 return ui_obsvlist_add(list, observer);
65 void ui_notify(UiObserver *observer, void *data) {
66 ui_notify_except(observer, NULL, data);
69 void ui_notify_except(UiObserver *observer, UiObserver *exc, void *data) {
79 observer->callback(&evt, observer->data);
81 observer = observer->next;
85 void ui_notify_evt(UiObserver *observer, UiEvent *event) {
87 observer->callback(event, observer->data);
88 observer = observer->next;
92 /* --------------------------- UiList --------------------------- */
94 UiList* ui_list_new(UiContext *ctx, char *name) {
95 UiList *list = malloc(sizeof(UiList));
96 list->first = ui_list_first;
97 list->next = ui_list_next;
98 list->get = ui_list_get;
99 list->count = ui_list_count;
100 list->observers = NULL;
109 uic_reg_var(ctx, name, UI_VAR_LIST, list);
115 void ui_list_free(UiList *list) {
116 ucx_list_free(list->data);
120 void* ui_list_first(UiList *list) {
121 UcxList *elm = list->data;
123 return elm ? elm->data : NULL;
126 void* ui_list_next(UiList *list) {
127 UcxList *elm = list->iter;
138 void* ui_list_get(UiList *list, int i) {
139 UcxList *elm = ucx_list_get(list->data, i);
148 int ui_list_count(UiList *list) {
149 UcxList *elm = list->data;
150 return (int)ucx_list_size(elm);
153 void ui_list_append(UiList *list, void *data) {
154 list->data = ucx_list_append(list->data, data);
157 void ui_list_prepend(UiList *list, void *data) {
158 list->data = ucx_list_prepend(list->data, data);
161 void ui_list_clear(UiList *list) {
162 ucx_list_free(list->data);
166 void ui_list_addobsv(UiList *list, ui_callback f, void *data) {
167 list->observers = ui_add_observer(list->observers, f, data);
170 void ui_list_notify(UiList *list) {
171 ui_notify(list->observers, list);
180 UiModel* ui_model(UiContext *ctx, ...) {
181 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel));
186 UcxList *cols = NULL;
188 while((type = va_arg(ap, int)) != -1) {
189 char *name = va_arg(ap, char*);
191 UiColumn *column = malloc(sizeof(UiColumn));
195 cols = ucx_list_append(cols, column);
200 size_t len = ucx_list_size(cols);
202 info->types = ui_calloc(ctx, len, sizeof(UiModelType));
203 info->titles = ui_calloc(ctx, len, sizeof(char*));
206 UCX_FOREACH(elm, cols) {
207 UiColumn *c = elm->data;
208 info->types[i] = c->type;
209 info->titles[i] = c->name;
218 void ui_model_free(UiContext *ctx, UiModel *mi) {
219 ucx_mempool_free(ctx->mempool, mi->types);
220 ucx_mempool_free(ctx->mempool, mi->titles);
221 ucx_mempool_free(ctx->mempool, mi);
227 UiInteger* ui_int_new(UiContext *ctx, char *name) {
228 UiInteger *i = ui_malloc(ctx, sizeof(UiInteger));
229 memset(i, 0, sizeof(UiInteger));
231 uic_reg_var(ctx, name, UI_VAR_INTEGER, i);
236 UiDouble* ui_double_new(UiContext *ctx, char *name) {
237 UiDouble *d = ui_malloc(ctx, sizeof(UiDouble));
238 memset(d, 0, sizeof(UiDouble));
240 uic_reg_var(ctx, name, UI_VAR_DOUBLE, d);
245 UiString* ui_string_new(UiContext *ctx, char *name) {
246 UiString *s = ui_malloc(ctx, sizeof(UiString));
247 memset(s, 0, sizeof(UiString));
249 uic_reg_var(ctx, name, UI_VAR_STRING, s);
254 UiText* ui_text_new(UiContext *ctx, char *name) {
255 UiText *t = ui_malloc(ctx, sizeof(UiText));
256 memset(t, 0, sizeof(UiText));
258 uic_reg_var(ctx, name, UI_VAR_TEXT, t);
263 UiRange* ui_range_new(UiContext *ctx, char *name) {
264 UiRange *r = ui_malloc(ctx, sizeof(UiRange));
265 memset(r, 0, sizeof(UiRange));
267 uic_reg_var(ctx, name, UI_VAR_RANGE, r);
274 void uic_int_copy(UiInteger *from, UiInteger *to) {
280 void uic_double_copy(UiDouble *from, UiDouble *to) {
286 void uic_string_copy(UiString *from, UiString *to) {
292 void uic_text_copy(UiText *from, UiText *to) {
295 to->getsubstr = from->getsubstr;
296 to->insert = from->insert;
297 to->setposition = from->setposition;
298 to->position = from->position;
299 to->selection = from->selection;
300 to->length = from->length;
301 to->remove = from->remove;
304 // do not copy the undo manager
307 void uic_range_copy(UiRange *from, UiRange *to) {
310 to->setrange = from->setrange;
311 to->setextent = from->setextent;
315 void uic_list_copy(UiList *from, UiList *to) {
316 to->update = from->update;
321 void uic_int_save(UiInteger *i) {
323 i->value = i->get(i);
326 void uic_double_save(UiDouble *d) {
328 d->value = d->get(d);
331 void uic_string_save(UiString *s) {
336 void uic_text_save(UiText *t) {
342 void uic_range_save(UiRange *r) {
348 void uic_int_unbind(UiInteger *i) {
354 void uic_double_unbind(UiDouble *d) {
360 void uic_string_unbind(UiString *s) {
366 void uic_text_unbind(UiText *t) {
371 t->setposition = NULL;
380 void uic_range_unbind(UiRange *r) {
388 void uic_list_unbind(UiList *l) {