Sun, 23 May 2021 09:44:43 +0200
refactor widget groups/document tree (Motif)
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2017 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <inttypes.h> #include <stdarg.h> #include "context.h" #include "../ui/window.h" #include "document.h" #include "types.h" static UiContext* global_context; void uic_init_global_context(void) { UcxMempool *mp = ucx_mempool_new(32); global_context = uic_context(NULL, mp); } UiContext* ui_global_context(void) { return global_context; } UiContext* uic_context(UiObject *toplevel, UcxMempool *mp) { UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext)); memset(ctx, 0, sizeof(UiContext)); ctx->mempool = mp; ctx->obj = toplevel; ctx->vars = ucx_map_new_a(mp->allocator, 16); ctx->attach_document = uic_context_attach_document; ctx->detach_document2 = uic_context_detach_document2; #ifdef UI_GTK if(toplevel && toplevel->widget) { ctx->accel_group = gtk_accel_group_new(); gtk_window_add_accel_group(GTK_WINDOW(toplevel->widget), ctx->accel_group); } #endif return ctx; } UiContext* uic_root_context(UiContext *ctx) { return ctx->parent ? uic_root_context(ctx->parent) : ctx; } void uic_context_attach_document(UiContext *ctx, void *document) { ctx->documents = ucx_list_append_a(ctx->mempool->allocator, ctx->documents, document); ctx->document = ctx->documents->data; UiContext *doc_ctx = ui_document_context(document); // check if any parent context has an unbound variable with the same name // as any document variable UiContext *var_ctx = ctx; while(var_ctx) { if(var_ctx->vars_unbound && var_ctx->vars_unbound->count > 0) { UcxMapIterator i = ucx_map_iterator(var_ctx->vars_unbound); UiVar *var; // rmkeys holds all keys, that shall be removed from vars_unbound UcxKey *rmkeys = calloc(var_ctx->vars_unbound->count, sizeof(UcxKey)); size_t numkeys = 0; UCX_MAP_FOREACH(key, var, i) { UiVar *docvar = ucx_map_get(doc_ctx->vars, key); if(docvar) { // bind var to document var uic_copy_binding(var, docvar, TRUE); rmkeys[numkeys++] = key; // save the key for removal } } // now that we may have bound some vars to the document, // we can remove them from the unbound map for(size_t k=0;k<numkeys;k++) { ucx_map_remove(var_ctx->vars_unbound, rmkeys[k]); } } var_ctx = ctx->parent; } } static void uic_context_unbind_vars(UiContext *ctx) { UcxMapIterator i = ucx_map_iterator(ctx->vars); UiVar *var; UCX_MAP_FOREACH(key, var, i) { if(var->from && var->from_ctx) { uic_save_var2(var); uic_copy_binding(var, var->from, FALSE); ucx_map_put(var->from_ctx->vars_unbound, key, var->from); var->from_ctx = ctx; } } UCX_FOREACH(elm, ctx->documents) { UiContext *subctx = ui_document_context(elm->data); uic_context_unbind_vars(subctx); } } void uic_context_detach_document2(UiContext *ctx, void *document) { // find the document in the documents list UcxList *doc = NULL; UCX_FOREACH(elm, ctx->documents) { if(elm->data == document) { doc = elm; break; } } if(!doc) { return; // document is not a subdocument of this context } ctx->documents = ucx_list_remove_a(ctx->mempool->allocator, ctx->documents, doc); ctx->document = ctx->documents ? ctx->documents->data : NULL; UiContext *docctx = ui_document_context(document); uic_context_unbind_vars(docctx); // unbind all doc/subdoc vars from the parent } void uic_context_detach_all(UiContext *ctx) { UcxList *ls = ucx_list_clone(ctx->documents, NULL, NULL); UCX_FOREACH(elm, ls) { ctx->detach_document2(ctx, elm->data); } ucx_list_free(ls); } static UiVar* ctx_getvar(UiContext *ctx, UcxKey key) { UiVar *var = ucx_map_get(ctx->vars, key); if(!var) { UCX_FOREACH(elm, ctx->documents) { UiContext *subctx = ui_document_context(elm->data); var = ctx_getvar(subctx, key); if(var) { break; } } } return var; } UiVar* uic_get_var(UiContext *ctx, const char *name) { UcxKey key = ucx_key(name, strlen(name)); return ctx_getvar(ctx, key); } UiVar* uic_create_var(UiContext *ctx, const char *name, UiVarType type) { UiVar *var = uic_get_var(ctx, name); if(var) { if(var->type == type) { return var; } else { fprintf(stderr, "UiError: var '%s' already bound with different type\n", name); } } var = ui_malloc(ctx, sizeof(UiVar)); var->type = type; var->value = uic_create_value(ctx, type); var->from = NULL; var->from_ctx = ctx; if(!ctx->vars_unbound) { ctx->vars_unbound = ucx_map_new_a(ctx->mempool->allocator, 16); } ucx_map_cstr_put(ctx->vars_unbound, name, var); return var; } void* uic_create_value(UiContext *ctx, UiVarType type) { void *val = NULL; switch(type) { case UI_VAR_SPECIAL: break; case UI_VAR_INTEGER: { val = ui_int_new(ctx, NULL); break; } case UI_VAR_DOUBLE: { val = ui_double_new(ctx, NULL); break; } case UI_VAR_STRING: { val = ui_string_new(ctx, NULL); break; } case UI_VAR_TEXT: { val = ui_text_new(ctx, NULL); break; } case UI_VAR_LIST: { val = ui_list_new(ctx, NULL); break; } case UI_VAR_RANGE: { val = ui_range_new(ctx, NULL); break; } } return val; } void uic_copy_binding(UiVar *from, UiVar *to, UiBool copytodoc) { // check type if(from->type != to->type) { fprintf(stderr, "UI Error: var has incompatible type.\n"); return; } void *fromvalue = from->value; // update var if(copytodoc) { to->from = from; to->from_ctx = from->from_ctx; } // copy binding // we don't copy the observer, because the from var has never one switch(from->type) { default: fprintf(stderr, "uic_copy_binding: wtf!\n"); break; case UI_VAR_SPECIAL: break; case UI_VAR_INTEGER: { UiInteger *f = fromvalue; UiInteger *t = to->value; if(!f->obj) break; uic_int_copy(f, t); t->set(t, t->value); break; } case UI_VAR_DOUBLE: { UiDouble *f = fromvalue; UiDouble *t = to->value; if(!f->obj) break; uic_double_copy(f, t); t->set(t, t->value); break; } case UI_VAR_STRING: { UiString *f = fromvalue; UiString *t = to->value; if(!f->obj) break; uic_string_copy(f, t); char *tvalue = t->value.ptr ? t->value.ptr : ""; t->set(t, tvalue); break; } case UI_VAR_TEXT: { UiText *f = fromvalue; UiText *t = to->value; if(!f->obj) break; uic_text_copy(f, t); char *tvalue = t->value.ptr ? t->value.ptr : ""; t->set(t, tvalue); t->setposition(t, t->pos); break; } case UI_VAR_LIST: { UiList *f = fromvalue; UiList *t = to->value; if(!f->obj) break; uic_list_copy(f, t); t->update(t, -1); break; } case UI_VAR_RANGE: { UiRange *f = fromvalue; UiRange *t = to->value; if(!f->obj) break; uic_range_copy(f, t); t->setextent(t, t->extent); t->setrange(t, t->min, t->max); t->set(t, t->value); break; } } } void uic_save_var2(UiVar *var) { switch(var->type) { case UI_VAR_SPECIAL: break; case UI_VAR_INTEGER: uic_int_save(var->value); break; case UI_VAR_DOUBLE: uic_double_save(var->value); break; case UI_VAR_STRING: uic_string_save(var->value); break; case UI_VAR_TEXT: uic_text_save(var->value); break; case UI_VAR_LIST: break; case UI_VAR_RANGE: uic_range_save(var->value); break; } } void uic_unbind_var(UiVar *var) { switch(var->type) { case UI_VAR_SPECIAL: break; case UI_VAR_INTEGER: uic_int_unbind(var->value); break; case UI_VAR_DOUBLE: uic_double_unbind(var->value); break; case UI_VAR_STRING: uic_string_unbind(var->value); break; case UI_VAR_TEXT: uic_text_unbind(var->value); break; case UI_VAR_LIST: uic_list_unbind(var->value); break; case UI_VAR_RANGE: uic_range_unbind(var->value); break; } } void uic_reg_var(UiContext *ctx, char *name, UiVarType type, void *value) { // TODO: do we need/want this? Why adding vars to a context after // widgets reference these? Workarounds: // 1. add vars to ctx before creating ui // 2. create ui, create new document with vars, attach doc // also it would be possible to create a function, that scans unbound vars // and connects them to available vars /* UiContext *rootctx = uic_root_context(ctx); UiVar *b = NULL; if(rootctx->bound) { // some widgets are already bound to some vars b = ucx_map_cstr_get(rootctx->bound, name); if(b) { // a widget is bound to a var with this name // if ctx is the root context we can remove the var from bound // because set_doc or detach can't fuck things up if(ctx == rootctx) { ucx_map_cstr_remove(ctx->bound, name); // TODO: free stuff } } } */ // create new var and add it to doc's vars UiVar *var = ui_malloc(ctx, sizeof(UiVar)); var->type = type; var->value = value; var->from = NULL; var->from_ctx = ctx; size_t oldcount = ctx->vars->count; ucx_map_cstr_put(ctx->vars, name, var); if(ctx->vars->count != oldcount + 1) { fprintf(stderr, "UiError: var '%s' already exists\n", name); } // TODO: remove? // a widget is already bound to a var with this name // copy the binding (like uic_context_set_document) /* if(b) { uic_copy_binding(b, var, TRUE); } */ } void uic_remove_bound_var(UiContext *ctx, UiVar *var) { // TODO: implement printf("TODO: implement uic_remove_bound_var\n"); } // public API void ui_attach_document(UiContext *ctx, void *document) { uic_context_attach_document(ctx, document); } void ui_detach_document2(UiContext *ctx, void *document) { uic_context_detach_document2(ctx, document); } void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata) { ctx->close_callback = fnc; ctx->close_data = udata; } void ui_set_group(UiContext *ctx, int group) { if(ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL) == -1) { ctx->groups = ucx_list_append_a(ctx->mempool->allocator, ctx->groups, (void*)(intptr_t)group); } // enable/disable group widgets uic_check_group_widgets(ctx); } void ui_unset_group(UiContext *ctx, int group) { int i = ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL); if(i != -1) { UcxList *elm = ucx_list_get(ctx->groups, i); ctx->groups = ucx_list_remove_a(ctx->mempool->allocator, ctx->groups, elm); } // enable/disable group widgets uic_check_group_widgets(ctx); } int* ui_active_groups(UiContext *ctx, int *ngroups) { if(!ctx->groups) { return NULL; } int nelm = ucx_list_size(ctx->groups); int *groups = calloc(sizeof(int), nelm); int i = 0; UCX_FOREACH(elm, ctx->groups) { groups[i++] = (intptr_t)elm->data; } *ngroups = nelm; return groups; } void uic_check_group_widgets(UiContext *ctx) { int ngroups = 0; int *groups = ui_active_groups(ctx, &ngroups); UCX_FOREACH(elm, ctx->group_widgets) { UiGroupWidget *gw = elm->data; char *check = calloc(1, gw->numgroups); for(int i=0;i<ngroups;i++) { for(int k=0;k<gw->numgroups;k++) { if(groups[i] == gw->groups[k]) { check[k] = 1; } } } int enable = 1; for(int i=0;i<gw->numgroups;i++) { if(check[i] == 0) { enable = 0; break; } } gw->enable(gw->widget, enable); } if(groups) { free(groups); } } void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) { // get groups UcxList *groups = NULL; va_list ap; va_start(ap, enable); int group; while((group = va_arg(ap, int)) != -1) { groups = ucx_list_append(groups, (void*)(intptr_t)group); } va_end(ap); uic_add_group_widget(ctx, widget, enable, groups); ucx_list_free(groups); } void uic_add_group_widget(UiContext *ctx, void *widget, ui_enablefunc enable, UcxList *groups) { UcxMempool *mp = ctx->mempool; UiGroupWidget *gw = ucx_mempool_malloc(mp, sizeof(UiGroupWidget)); gw->widget = widget; gw->enable = enable; gw->numgroups = ucx_list_size(groups); gw->groups = ucx_mempool_calloc(mp, gw->numgroups, sizeof(int)); int i = 0; UCX_FOREACH(elm, groups) { gw->groups[i++] = (intptr_t)elm->data; } ctx->group_widgets = ucx_list_append_a( mp->allocator, ctx->group_widgets, gw); } void* ui_malloc(UiContext *ctx, size_t size) { return ctx ? ucx_mempool_malloc(ctx->mempool, size) : NULL; } void* ui_calloc(UiContext *ctx, size_t nelem, size_t elsize) { return ctx ? ucx_mempool_calloc(ctx->mempool, nelem, elsize) : NULL; } void ui_free(UiContext *ctx, void *ptr) { if(ctx) { ucx_mempool_free(ctx->mempool, ptr); } } void* ui_realloc(UiContext *ctx, void *ptr, size_t size) { return ctx ? ucx_mempool_realloc(ctx->mempool, ptr, size) : NULL; }