ui/common/context.c

Fri, 09 Feb 2024 16:50:23 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 09 Feb 2024 16:50:23 +0100
changeset 26
40d6af793c1a
parent 21
3060a5a1d5fd
permissions
-rw-r--r--

implement resource deletion

/*
 * 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 <cx/array_list.h>
#include <cx/compare.h>
#include <cx/mempool.h>

#include "context.h"
#include "../ui/window.h"
#include "document.h"
#include "types.h"

static UiContext* global_context;

void uic_init_global_context(void) {
    CxMempool *mp = cxBasicMempoolCreate(32);
    global_context = uic_context(NULL, mp);
}

UiContext* ui_global_context(void) {
    return global_context;
}

UiContext* uic_context(UiObject *toplevel, CxMempool *mp) {
    UiContext *ctx = cxMalloc(mp->allocator, sizeof(UiContext));
    memset(ctx, 0, sizeof(UiContext));
    ctx->mp = mp;
    ctx->allocator = mp->allocator;
    ctx->obj = toplevel;
    ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16);
    
    ctx->documents = cxLinkedListCreate(mp->allocator, cx_cmp_intptr, CX_STORE_POINTERS);
    ctx->group_widgets = cxLinkedListCreate(mp->allocator, NULL, sizeof(UiGroupWidget));
    ctx->groups = cxArrayListCreate(mp->allocator, cx_cmp_int, sizeof(int), 32);
    
    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) {
    cxListAdd(ctx->documents, document);
    ctx->document = document;
    
    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->size > 0) {
            CxIterator i = cxMapIterator(var_ctx->vars_unbound);
            cx_foreach(CxMapEntry*, entry, i) {
                UiVar *var = entry->value;
                UiVar *docvar = cxMapGet(doc_ctx->vars, *entry->key);
                if(docvar) {
                    // bind var to document var
                    uic_copy_binding(var, docvar, TRUE);
                    cxIteratorFlagRemoval(i);
                }
            }
        }
        
        var_ctx = ctx->parent;
    }
}

static void uic_context_unbind_vars(UiContext *ctx) {
    CxIterator i = cxMapIterator(ctx->vars);
    cx_foreach(CxMapEntry*, entry, i) {
        UiVar *var = entry->value;
        if(var->from && var->from_ctx) {
            uic_save_var2(var);
            uic_copy_binding(var, var->from, FALSE);
            cxMapPut(var->from_ctx->vars_unbound, *entry->key, var->from);
            var->from_ctx = ctx;
        }
    }
    
    if(ctx->documents) {
        i = cxListIterator(ctx->documents);
        cx_foreach(void *, doc, i) {
            UiContext *subctx = ui_document_context(doc);
            uic_context_unbind_vars(subctx);
        }
    }
}

void uic_context_detach_document2(UiContext *ctx, void *document) {
    // find the document in the documents list
    ssize_t docIndex = cxListFind(ctx->documents, document);
    if(docIndex < 0) {
        return;
    }
    
    cxListRemove(ctx->documents, docIndex);
    ctx->document = cxListAt(ctx->documents, 0);
    
    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) {
    // copy list
    CxList *ls = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS);
    CxIterator i = cxListIterator(ctx->documents);
    cx_foreach(void *, doc, i) {
        cxListAdd(ls, doc);
    }
    
    // detach documents
    i = cxListIterator(ls);
    cx_foreach(void *, doc, i) {
        ctx->detach_document2(ctx, doc);
    }
    
    cxListDestroy(ls);
}

static UiVar* ctx_getvar(UiContext *ctx, CxHashKey key) {
    UiVar *var = cxMapGet(ctx->vars, key);
    if(!var) {
        CxIterator i = cxListIterator(ctx->documents);
        cx_foreach(void *, doc, i) {
            UiContext *subctx = ui_document_context(doc);
            var = ctx_getvar(subctx, key);
            if(var) {
                break;
            }
        }
    }
    return var;
}

UiVar* uic_get_var(UiContext *ctx, const char *name) {
    CxHashKey key = cx_hash_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;

    cxMempoolRegister(ctx->mp, var, (cx_destructor_func)uic_unbind_var);

    if(!ctx->vars_unbound) {
        ctx->vars_unbound = cxHashMapCreate(ctx->allocator, CX_STORE_POINTERS, 16);
    }
    cxMapPut(ctx->vars_unbound, name, var);
    
    return var;
}

UiVar* uic_create_value_var(UiContext* ctx, void* value) {
    UiVar *var = (UiVar*)ui_malloc(ctx, sizeof(UiVar));
    var->from = NULL;
    var->from_ctx = NULL;
    var->value = value;
    var->type = UI_VAR_SPECIAL;
    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;
}


UiVar* uic_widget_var(UiContext* toplevel, UiContext* current, void* value, const char* varname, UiVarType type) {
    if (value) {
        return uic_create_value_var(current, value);
    }
    if (varname) {
        return uic_create_var(toplevel, varname, type);
    }
    return NULL;
}


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: {
            // TODO: not sure how correct this is

            UiList *f = from->value;
            UiList *t = to->value;
            if (f->obj) {
                t->obj = f->obj;
                t->update = f->update;
                t->getselection = f->getselection;
            }

            UiVar tmp = *from;
            *from = *to;
            *to = tmp;

            UiList* t2 = to->value;
            ui_notify(t2->observers, NULL);
            
            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->size;
    cxMapPut(ctx->vars, name, var);
    if(ctx->vars->size != 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
}


// 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;
}

UIEXPORT void ui_context_destroy(UiContext *ctx) {
    cxMempoolDestroy(ctx->mp);
}


void ui_set_group(UiContext *ctx, int group) {
    if(cxListFind(ctx->groups, &group) == -1) {
        cxListAdd(ctx->groups, &group);
    }
    
    // enable/disable group widgets
    uic_check_group_widgets(ctx);
}

void ui_unset_group(UiContext *ctx, int group) {
    int i = cxListFind(ctx->groups, &group);
    if(i != -1) {
        cxListRemove(ctx->groups, i);
    }
    
    // enable/disable group widgets
    uic_check_group_widgets(ctx);
}

int* ui_active_groups(UiContext *ctx, int *ngroups) {
    *ngroups = ctx->groups->size;
    return cxListAt(ctx->groups, 0);
}

void uic_check_group_widgets(UiContext *ctx) {
    int ngroups = 0;
    int *groups = ui_active_groups(ctx, &ngroups);
    
    CxIterator i = cxListIterator(ctx->group_widgets);
    cx_foreach(UiGroupWidget *, gw, i) {
        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;
            }
        }
        free(check);
        gw->enable(gw->widget, enable);
    }
}

void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) {
    // get groups
    CxList *groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
    va_list ap;
    va_start(ap, enable);
    int group;
    while((group = va_arg(ap, int)) != -1) {
        cxListAdd(groups, &group);
    }
    va_end(ap);
    
    uic_add_group_widget(ctx, widget, enable, groups);
    
    cxListDestroy(groups);
}

void uic_add_group_widget(UiContext *ctx, void *widget, ui_enablefunc enable, CxList *groups) {
    const CxAllocator *a = ctx->allocator;
    UiGroupWidget gw;
    
    gw.widget = widget;
    gw.enable = enable;
    gw.numgroups = groups->size;
    gw.groups = cxCalloc(a, gw.numgroups, sizeof(int));
    
    // copy groups
    int *intgroups = cxListAt(groups, 0);
    if(intgroups) {
        memcpy(gw.groups, intgroups, gw.numgroups * sizeof(int));
    }
    
    cxListAdd(ctx->group_widgets, &gw);
}

void* ui_malloc(UiContext *ctx, size_t size) {
    return ctx ? cxMalloc(ctx->allocator, size) : NULL;
}

void* ui_calloc(UiContext *ctx, size_t nelem, size_t elsize) {
    return ctx ? cxCalloc(ctx->allocator, nelem, elsize) : NULL;
}

void ui_free(UiContext *ctx, void *ptr) {
    if(ctx) {
        cxFree(ctx->allocator, ptr);
    }
}

void* ui_realloc(UiContext *ctx, void *ptr, size_t size) {
    return ctx ? cxRealloc(ctx->allocator, ptr, size) : NULL;
}

mercurial