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.
36 #include "../ui/window.h"
40 static UiContext* global_context;
42 void uic_init_global_context(void) {
43 UcxMempool *mp = ucx_mempool_new(32);
44 global_context = uic_context(NULL, mp);
47 UiContext* ui_global_context(void) {
48 return global_context;
51 UiContext* uic_context(UiObject *toplevel, UcxMempool *mp) {
52 UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext));
53 memset(ctx, 0, sizeof(UiContext));
56 ctx->vars = ucx_map_new_a(mp->allocator, 16);
58 ctx->attach_document = uic_context_attach_document;
59 ctx->detach_document2 = uic_context_detach_document2;
62 if(toplevel && toplevel->widget) {
63 ctx->accel_group = gtk_accel_group_new();
64 gtk_window_add_accel_group(GTK_WINDOW(toplevel->widget), ctx->accel_group);
71 UiContext* uic_root_context(UiContext *ctx) {
72 return ctx->parent ? uic_root_context(ctx->parent) : ctx;
75 void uic_context_attach_document(UiContext *ctx, void *document) {
76 ctx->documents = ucx_list_append_a(ctx->mempool->allocator, ctx->documents, document);
77 ctx->document = ctx->documents->data;
79 UiContext *doc_ctx = ui_document_context(document);
81 // check if any parent context has an unbound variable with the same name
82 // as any document variable
83 UiContext *var_ctx = ctx;
85 if(var_ctx->vars_unbound && var_ctx->vars_unbound->count > 0) {
86 UcxMapIterator i = ucx_map_iterator(var_ctx->vars_unbound);
88 // rmkeys holds all keys, that shall be removed from vars_unbound
89 UcxKey *rmkeys = calloc(var_ctx->vars_unbound->count, sizeof(UcxKey));
91 UCX_MAP_FOREACH(key, var, i) {
92 UiVar *docvar = ucx_map_get(doc_ctx->vars, key);
94 // bind var to document var
95 uic_copy_binding(var, docvar, TRUE);
96 rmkeys[numkeys++] = key; // save the key for removal
99 // now that we may have bound some vars to the document,
100 // we can remove them from the unbound map
101 for(size_t k=0;k<numkeys;k++) {
102 ucx_map_remove(var_ctx->vars_unbound, rmkeys[k]);
106 var_ctx = ctx->parent;
110 static void uic_context_unbind_vars(UiContext *ctx) {
111 UcxMapIterator i = ucx_map_iterator(ctx->vars);
113 UCX_MAP_FOREACH(key, var, i) {
114 if(var->from && var->from_ctx) {
116 uic_copy_binding(var, var->from, FALSE);
117 ucx_map_put(var->from_ctx->vars_unbound, key, var->from);
122 UCX_FOREACH(elm, ctx->documents) {
123 UiContext *subctx = ui_document_context(elm->data);
124 uic_context_unbind_vars(subctx);
128 void uic_context_detach_document2(UiContext *ctx, void *document) {
129 // find the document in the documents list
131 UCX_FOREACH(elm, ctx->documents) {
132 if(elm->data == document) {
138 return; // document is not a subdocument of this context
141 ctx->documents = ucx_list_remove_a(ctx->mempool->allocator, ctx->documents, doc);
142 ctx->document = ctx->documents ? ctx->documents->data : NULL;
144 UiContext *docctx = ui_document_context(document);
145 uic_context_unbind_vars(docctx); // unbind all doc/subdoc vars from the parent
148 void uic_context_detach_all(UiContext *ctx) {
149 UcxList *ls = ucx_list_clone(ctx->documents, NULL, NULL);
150 UCX_FOREACH(elm, ls) {
151 ctx->detach_document2(ctx, elm->data);
156 static UiVar* ctx_getvar(UiContext *ctx, UcxKey key) {
157 UiVar *var = ucx_map_get(ctx->vars, key);
159 UCX_FOREACH(elm, ctx->documents) {
160 UiContext *subctx = ui_document_context(elm->data);
161 var = ctx_getvar(subctx, key);
170 UiVar* uic_get_var(UiContext *ctx, const char *name) {
171 UcxKey key = ucx_key(name, strlen(name));
172 return ctx_getvar(ctx, key);
175 UiVar* uic_create_var(UiContext *ctx, const char *name, UiVarType type) {
176 UiVar *var = uic_get_var(ctx, name);
178 if(var->type == type) {
181 fprintf(stderr, "UiError: var '%s' already bound with different type\n", name);
185 var = ui_malloc(ctx, sizeof(UiVar));
187 var->value = uic_create_value(ctx, type);
191 if(!ctx->vars_unbound) {
192 ctx->vars_unbound = ucx_map_new_a(ctx->mempool->allocator, 16);
194 ucx_map_cstr_put(ctx->vars_unbound, name, var);
199 void* uic_create_value(UiContext *ctx, UiVarType type) {
202 case UI_VAR_SPECIAL: break;
203 case UI_VAR_INTEGER: {
204 val = ui_int_new(ctx, NULL);
207 case UI_VAR_DOUBLE: {
208 val = ui_double_new(ctx, NULL);
211 case UI_VAR_STRING: {
212 val = ui_string_new(ctx, NULL);
216 val = ui_text_new(ctx, NULL);
220 val = ui_list_new(ctx, NULL);
224 val = ui_range_new(ctx, NULL);
231 void uic_copy_binding(UiVar *from, UiVar *to, UiBool copytodoc) {
233 if(from->type != to->type) {
234 fprintf(stderr, "UI Error: var has incompatible type.\n");
238 void *fromvalue = from->value;
242 to->from_ctx = from->from_ctx;
246 // we don't copy the observer, because the from var has never one
248 default: fprintf(stderr, "uic_copy_binding: wtf!\n"); break;
249 case UI_VAR_SPECIAL: break;
250 case UI_VAR_INTEGER: {
251 UiInteger *f = fromvalue;
252 UiInteger *t = to->value;
258 case UI_VAR_DOUBLE: {
259 UiDouble *f = fromvalue;
260 UiDouble *t = to->value;
262 uic_double_copy(f, t);
266 case UI_VAR_STRING: {
267 UiString *f = fromvalue;
268 UiString *t = to->value;
270 uic_string_copy(f, t);
271 char *tvalue = t->value.ptr ? t->value.ptr : "";
276 UiText *f = fromvalue;
277 UiText *t = to->value;
280 char *tvalue = t->value.ptr ? t->value.ptr : "";
282 t->setposition(t, t->pos);
286 UiList *f = fromvalue;
287 UiList *t = to->value;
294 UiRange *f = fromvalue;
295 UiRange *t = to->value;
297 uic_range_copy(f, t);
298 t->setextent(t, t->extent);
299 t->setrange(t, t->min, t->max);
306 void uic_save_var2(UiVar *var) {
308 case UI_VAR_SPECIAL: break;
309 case UI_VAR_INTEGER: uic_int_save(var->value); break;
310 case UI_VAR_DOUBLE: uic_double_save(var->value); break;
311 case UI_VAR_STRING: uic_string_save(var->value); break;
312 case UI_VAR_TEXT: uic_text_save(var->value); break;
313 case UI_VAR_LIST: break;
314 case UI_VAR_RANGE: uic_range_save(var->value); break;
318 void uic_unbind_var(UiVar *var) {
320 case UI_VAR_SPECIAL: break;
321 case UI_VAR_INTEGER: uic_int_unbind(var->value); break;
322 case UI_VAR_DOUBLE: uic_double_unbind(var->value); break;
323 case UI_VAR_STRING: uic_string_unbind(var->value); break;
324 case UI_VAR_TEXT: uic_text_unbind(var->value); break;
325 case UI_VAR_LIST: uic_list_unbind(var->value); break;
326 case UI_VAR_RANGE: uic_range_unbind(var->value); break;
330 void uic_reg_var(UiContext *ctx, char *name, UiVarType type, void *value) {
331 // TODO: do we need/want this? Why adding vars to a context after
332 // widgets reference these? Workarounds:
333 // 1. add vars to ctx before creating ui
334 // 2. create ui, create new document with vars, attach doc
335 // also it would be possible to create a function, that scans unbound vars
336 // and connects them to available vars
338 UiContext *rootctx = uic_root_context(ctx);
341 // some widgets are already bound to some vars
342 b = ucx_map_cstr_get(rootctx->bound, name);
344 // a widget is bound to a var with this name
345 // if ctx is the root context we can remove the var from bound
346 // because set_doc or detach can't fuck things up
348 ucx_map_cstr_remove(ctx->bound, name);
355 // create new var and add it to doc's vars
356 UiVar *var = ui_malloc(ctx, sizeof(UiVar));
361 size_t oldcount = ctx->vars->count;
362 ucx_map_cstr_put(ctx->vars, name, var);
363 if(ctx->vars->count != oldcount + 1) {
364 fprintf(stderr, "UiError: var '%s' already exists\n", name);
368 // a widget is already bound to a var with this name
369 // copy the binding (like uic_context_set_document)
372 uic_copy_binding(b, var, TRUE);
377 void uic_remove_bound_var(UiContext *ctx, UiVar *var) {
379 printf("TODO: implement uic_remove_bound_var\n");
385 void ui_attach_document(UiContext *ctx, void *document) {
386 uic_context_attach_document(ctx, document);
389 void ui_detach_document2(UiContext *ctx, void *document) {
390 uic_context_detach_document2(ctx, document);
393 void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata) {
394 ctx->close_callback = fnc;
395 ctx->close_data = udata;
399 void ui_set_group(UiContext *ctx, int group) {
400 if(ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL) == -1) {
401 ctx->groups = ucx_list_append_a(ctx->mempool->allocator, ctx->groups, (void*)(intptr_t)group);
404 // enable/disable group widgets
405 uic_check_group_widgets(ctx);
408 void ui_unset_group(UiContext *ctx, int group) {
409 int i = ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL);
411 UcxList *elm = ucx_list_get(ctx->groups, i);
412 ctx->groups = ucx_list_remove_a(ctx->mempool->allocator, ctx->groups, elm);
415 // enable/disable group widgets
416 uic_check_group_widgets(ctx);
419 int* ui_active_groups(UiContext *ctx, int *ngroups) {
424 int nelm = ucx_list_size(ctx->groups);
425 int *groups = calloc(sizeof(int), nelm);
428 UCX_FOREACH(elm, ctx->groups) {
429 groups[i++] = (intptr_t)elm->data;
436 void uic_check_group_widgets(UiContext *ctx) {
438 int *groups = ui_active_groups(ctx, &ngroups);
440 UCX_FOREACH(elm, ctx->group_widgets) {
441 UiGroupWidget *gw = elm->data;
442 char *check = calloc(1, gw->numgroups);
444 for(int i=0;i<ngroups;i++) {
445 for(int k=0;k<gw->numgroups;k++) {
446 if(groups[i] == gw->groups[k]) {
453 for(int i=0;i<gw->numgroups;i++) {
459 gw->enable(gw->widget, enable);
467 void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) {
469 UcxList *groups = NULL;
471 va_start(ap, enable);
473 while((group = va_arg(ap, int)) != -1) {
474 groups = ucx_list_append(groups, (void*)(intptr_t)group);
478 uic_add_group_widget(ctx, widget, enable, groups);
480 ucx_list_free(groups);
483 void uic_add_group_widget(UiContext *ctx, void *widget, ui_enablefunc enable, UcxList *groups) {
484 UcxMempool *mp = ctx->mempool;
485 UiGroupWidget *gw = ucx_mempool_malloc(mp, sizeof(UiGroupWidget));
489 gw->numgroups = ucx_list_size(groups);
490 gw->groups = ucx_mempool_calloc(mp, gw->numgroups, sizeof(int));
492 UCX_FOREACH(elm, groups) {
493 gw->groups[i++] = (intptr_t)elm->data;
496 ctx->group_widgets = ucx_list_append_a(
502 void* ui_malloc(UiContext *ctx, size_t size) {
503 return ctx ? ucx_mempool_malloc(ctx->mempool, size) : NULL;
506 void* ui_calloc(UiContext *ctx, size_t nelem, size_t elsize) {
507 return ctx ? ucx_mempool_calloc(ctx->mempool, nelem, elsize) : NULL;
510 void ui_free(UiContext *ctx, void *ptr) {
512 ucx_mempool_free(ctx->mempool, ptr);
516 void* ui_realloc(UiContext *ctx, void *ptr, size_t size) {
517 return ctx ? ucx_mempool_realloc(ctx->mempool, ptr, size) : NULL;