ui/common/context.c

Sun, 07 Dec 2025 19:20:48 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 19:20:48 +0100
changeset 976
e2763e880938
parent 967
ff4a8d10307b
permissions
-rw-r--r--

implement radiobutton (Client)

/*
 * 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 "../ui/widget.h"
#include "document.h"
#include "types.h"


static UiContext* global_context;

void uic_init_global_context(void) {
    CxMempool *mp = cxMempoolCreateSimple(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->destroy_handler = cxArrayListCreate(ctx->allocator, NULL, sizeof(UiDestroyHandler), 16);
    ctx->obj = toplevel;
    ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16);
    
    ctx->documents = cxLinkedListCreate(mp->allocator, cx_cmp_ptr, CX_STORE_POINTERS);
    ctx->state_widgets = cxLinkedListCreate(mp->allocator, cx_cmp_ptr, sizeof(UiStateWidget));
    ctx->states = cxArrayListCreate(mp->allocator, cx_cmp_int, sizeof(int), 32);
    
    ctx->attach_document = uic_context_attach_document;
    ctx->detach_document2 = uic_context_detach_document;
    
#if UI_GTK2 || UI_GTK3
    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_add_destructor(UiContext *ctx, cx_destructor_func func, void *data) {
    UiDestroyHandler handler;
    handler.destructor = func;
    handler.data = data;
    cxListAdd(ctx->destroy_handler, &handler);
}

void uic_context_prepare_close(UiContext *ctx) {
    cxListClear(ctx->states);
    cxListClear(ctx->state_widgets);
}

void uic_context_attach_document(UiContext *ctx, void *document) {
    if(ctx->single_document_mode) {
        if(ctx->document) {
            uic_context_detach_document(ctx, ctx->document);
        }
    }
    
    cxListAdd(ctx->documents, document);
    ctx->document = document;
    
    UiContext *doc_ctx = ui_document_context(document);
    doc_ctx->parent = ctx;
    
    // if a document variable has the same name as a parent variable,
    // move the bindings to the document
    UiContext *var_ctx = ctx;
    while(var_ctx) {
        CxMapIterator i = cxMapIterator(var_ctx->vars);
        cx_foreach(CxMapEntry*, entry, i) {
            printf("attach %.*s\n", (int)entry->key->len, (char*)entry->key->data);
            UiVar *var = entry->value;
            UiVar *docvar = cxMapGet(doc_ctx->vars, *entry->key);
            if(docvar) {
                // bind var to document var
                uic_copy_var_binding(var, docvar, TRUE);
                cxIteratorFlagRemoval(i);
            }
        }
        
        var_ctx = var_ctx->parent;
    }
}

static void uic_context_unbind_vars(UiContext *ctx) {
    ui_onchange_events_enable(FALSE);
    CxMapIterator mi = cxMapIterator(ctx->vars);
    cx_foreach(CxMapEntry*, entry, mi) {
        printf("detach %.*s\n", (int)entry->key->len, (char*)entry->key->data);
        UiVar *var = entry->value;
        // var->from && var->from_ctx && var->from_ctx != ctx
        uic_save_var(var);
        if(var->from) {
            uic_copy_var_binding(var, var->from, FALSE);
            cxMapPut(var->from->from_ctx->vars, *entry->key, var->from);
            var->from = NULL;
        }
        uic_unbind_var(var);
    }
    
    if(ctx->documents) {
        CxIterator i = cxListIterator(ctx->documents);
        cx_foreach(void *, doc, i) {
            UiContext *subctx = ui_document_context(doc);
            uic_context_unbind_vars(subctx);
        }
    }
    
    ui_onchange_events_enable(TRUE);
}

void uic_context_detach_document(UiContext *ctx, void *document) {
    // find the document in the documents list
    size_t docIndex = cxListFind(ctx->documents, document);
    if(!cxListIndexValid(ctx->documents, docIndex)) {
        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
    docctx->parent = NULL;
}

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);
    }
    
    cxListFree(ls);
}

static UiVar* ctx_getvar(UiContext *ctx, CxHashKey key) {
    UiVar *var = cxMapGet(ctx->vars, key);
    if(!var && ctx->documents) {
        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_get_var_t(UiContext *ctx,const char *name, UiVarType type) {
    UiVar *var = uic_get_var(ctx, name);
    if(var && var->type == type) {
        return var;
    }
    return NULL;
}

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->original_value = NULL;
    var->from = NULL;
    var->from_ctx = ctx;
    var->bound = FALSE;
    
    cxMempoolSetDestructor(var, (cx_destructor_func)uic_unbind_var); // TODO: use another destructor that cleans the value (UiString free, UiText destroy, ...)

    cxMapPut(ctx->vars, 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 = ctx;
    var->value = value;
    var->original_value = NULL;
    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;
        }
        case UI_VAR_GENERIC: {
            val = ui_generic_new(ctx, NULL);
        }
    }
    return val;
}

// destroys a value, that was created by uic_create_value
void uic_destroy_value(UiContext *ctx, UiVarType type, void *value) {
    switch(type) {
        default: {
            ui_free(ctx, value);
            break;
        }
        case UI_VAR_SPECIAL: break;
        case UI_VAR_STRING: {
            UiString *s = value;
            if(s->value.free) {
                s->value.free(s->value.ptr);
            }
            ui_free(ctx, value);
        }
        case UI_VAR_TEXT: {
            UiText *t = value;
            if(t->value.free) {
                t->value.free(t->value.ptr);
                t->value.free = NULL;
                t->value.ptr = NULL;
            }
            if(t->destroy) {
                t->destroy(t);
            }
            ui_free(ctx, value);
        }
        case UI_VAR_LIST: {
            ui_list_free(ctx, value);
            break;
        }
    }
}


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_var_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;
    void *tovalue = to->value;
    // update var
    if(copytodoc) {
        to->from = from; // from which UiVar are the bindings copied
        from->original_value = fromvalue; // save original value otherwise it would be lost
        // widgets store a reference to the UiVar with their value
        // the UiVar object must be updated to contain the current value object
        from->value = tovalue;
    } else {
        if(to->original_value) {
            to->value = to->original_value;
            tovalue = to->value;
        }
    }
    
    uic_copy_value_binding(from->type, fromvalue, tovalue);
}

void uic_copy_value_binding(UiVarType type, void *from, void *to) {
     ui_setop_enable(TRUE);
    
    // copy binding
    // we don't copy the observer, because the from value never has oberservers
    switch(type) {
        default: fprintf(stderr, "uic_copy_binding: wtf!\n"); break;
        case UI_VAR_SPECIAL: break;
        case UI_VAR_INTEGER: {
            UiInteger *f = from;
            UiInteger *t = to;
            if(!f->obj) break;
            uic_int_copy(f, t);
            t->set(t, t->value);
            break;
        }
        case UI_VAR_DOUBLE: {
            UiDouble *f = from;
            UiDouble *t = to;
            if(!f->obj) break;
            uic_double_copy(f, t);
            t->set(t, t->value);
            break;
        }
        case UI_VAR_STRING: {
            UiString *f = from;
            UiString *t = to;
            if(!f->obj) break;
            uic_string_copy(f, t);
            char *tvalue = t->value.ptr ? t->value.ptr : "";
            char *fvalue = f->value.ptr ? f->value.ptr : "";
            t->set(t, tvalue);
            break;
        }
        case UI_VAR_TEXT: {
            UiText *f = from;
            UiText *t = to;
            if(!f->obj) break;
            uic_text_copy(f, t);
            t->restore(t);
            break;
        }
        case UI_VAR_LIST: {         
            UiList *f = from;
            UiList *t = to;
            uic_list_copy(f, t);
            ui_list_update(t);
            break;
        }
        case UI_VAR_RANGE: {
            UiRange *f = from;
            UiRange *t = to;
            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;
        }
        case UI_VAR_GENERIC: {
            UiGeneric *f = from;
            UiGeneric *t = to;
            if(!f->obj) break;
            uic_generic_copy(f, t);
            t->set(t, t->value, t->type);
            break;
        }
    }
    
    ui_setop_enable(FALSE);
}

void uic_save_var(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;
        case UI_VAR_GENERIC: uic_generic_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;
        case UI_VAR_GENERIC: uic_generic_unbind(var->value); break;
    }
}

const char *uic_type2str(UiVarType type) {
    switch(type) {
        default: return "";
        case UI_VAR_INTEGER: return "int";
        case UI_VAR_DOUBLE: return "double";
        case UI_VAR_STRING: return "string";
        case UI_VAR_TEXT: return "text";
        case UI_VAR_LIST: return "list";
        case UI_VAR_RANGE: return "range";
        case UI_VAR_GENERIC: return "generic";
    }
}

void uic_reg_var(UiContext *ctx, const char *name, UiVarType type, void *value) {
    UiVar *var = cxMapGet(ctx->vars, name);
    if(!var) {
        // create new var and add it to the context var map
        var = ui_malloc(ctx, sizeof(UiVar));
        cxMapPut(ctx->vars, name, var);
    } else {
        // override var with new value
        if(var->type != type) {
            fprintf(stderr, "Error: var %s type mismatch: %s - %s\n", name, uic_type2str(var->type), uic_type2str(type));
            return;
        }
        if(var->bound) {
            fprintf(stderr, "Error: var %s already bound\n", name);
            return;
        }
        UiInteger *prev_value = var->value;
        uic_copy_value_binding(type, prev_value, value);
        uic_destroy_value(var->from_ctx, var->type, var->value);
    }
    
    var->type = type;
    var->value = value;
    var->from = NULL;
    var->from_ctx = ctx;
    var->bound = TRUE;
}

// public API

void* ui_context_get_document(UiContext *ctx) {
    return ctx->document;
}

void ui_context_single_attachment_mode(UiContext *ctx, UiBool enable) {
    ctx->single_document_mode = enable;
}

void ui_attach_document(UiContext *ctx, void *document) {
    uic_context_attach_document(ctx, document);
}

void ui_detach_document(UiContext *ctx, void *document) {
    uic_context_detach_document(ctx, document);
}

void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata) {
    ctx->close_callback = fnc;
    ctx->close_data = udata;
}

void ui_context_destroy(UiContext *ctx) {
    CxIterator i = cxListIterator(ctx->destroy_handler);
    cx_foreach(UiDestroyHandler *, h, i) {
        h->destructor(h->data);
    }
    cxMempoolFree(ctx->mp);
}

UiContext* ui_context_parent(UiContext *ctx) {
    return ctx->parent;
}


void ui_set_state(UiContext *ctx, int state) {
    if(!cxListIndexValid(ctx->states, cxListFind(ctx->states, &state))) {
        cxListAdd(ctx->states, &state);
    }
    
    // enable/disable group widgets
    uic_check_state_widgets(ctx);
}

void ui_unset_state(UiContext *ctx, int state) {
    int i = cxListFind(ctx->states, &state);
    if(i != -1) {
        cxListRemove(ctx->states, i);
    }
    
    // enable/disable group widgets
    uic_check_state_widgets(ctx);
}

int* ui_active_states(UiContext *ctx, int *nstates) {
    *nstates = cxListSize(ctx->states);
    return cxListAt(ctx->states, 0);
}

void uic_check_state_widgets(UiContext *ctx) {
    int ngroups = 0;
    int *groups = ui_active_states(ctx, &ngroups);
    
    CxIterator i = cxListIterator(ctx->state_widgets);
    cx_foreach(UiStateWidget *, gw, i) {
        char *check = calloc(1, gw->numstates);
        
        for(int i=0;i<ngroups;i++) {
            for(int k=0;k<gw->numstates;k++) {
                if(groups[i] == gw->states[k]) {
                    check[k] = 1;
                }
            }
        }
        
        int enable = 1;
        for(int i=0;i<gw->numstates;i++) {
            if(check[i] == 0) {
                enable = 0;
                break;
            }
        }
        free(check);
        gw->enable(gw->widget, enable);
    }
}

void ui_widget_set_states(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) {
    if(enable == NULL) {
        enable = (ui_enablefunc)ui_set_enabled;
    }
    // get states
    CxList *states = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
    va_list ap;
    va_start(ap, enable);
    int state;
    while((state = va_arg(ap, int)) != -1) {
        cxListAdd(states, &state);
    }
    va_end(ap);
    
    uic_add_state_widget(ctx, widget, enable, states);
    
    cxListFree(states);
}

void ui_widget_set_states2(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, const int *states, int nstates) {
    if(enable == NULL) {
        enable = (ui_enablefunc)ui_set_enabled;
    }
    CxList *ls = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), nstates);
    for(int i=0;i<nstates;i++) {
        cxListAdd(ls, states+i);
    }
    uic_add_state_widget(ctx, widget, enable, ls);
    cxListFree(ls);
}

void ui_widget_set_visibility_states(UiContext *ctx, UIWIDGET widget, const int *states, int nstates) {
    ui_widget_set_states2(ctx, widget, (ui_enablefunc)ui_set_visible, states, nstates);
}

size_t uic_state_array_size(const int *states) {
    int i;
    for(i=0;states[i] >= 0;i++) { }
    return i;
}

void uic_add_state_widget(UiContext *ctx, void *widget, ui_enablefunc enable, CxList *states) {
    uic_add_state_widget_i(ctx, widget, enable, cxListAt(states, 0), cxListSize(states));
}

void uic_add_state_widget_i(UiContext *ctx, void *widget, ui_enablefunc enable, const int *states, size_t numstates) {
    const CxAllocator *a = ctx->allocator;
    UiStateWidget gw;
    
    gw.widget = widget;
    gw.enable = enable;
    gw.numstates = numstates;
    gw.states = cxCalloc(a, numstates, sizeof(int));
    
    // copy states
    if(states) {
        memcpy(gw.states, states, gw.numstates * sizeof(int));
    }
    
    cxListAdd(ctx->state_widgets, &gw);
}

void uic_remove_state_widget(UiContext *ctx, void *widget) {
    (void)cxListFindRemove(ctx->state_widgets, widget);
}

UIEXPORT void *ui_allocator(UiContext *ctx) {
    return (void*)ctx->allocator;
}

void* ui_cx_mempool(UiContext *ctx) {
    return ctx->mp;
}

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 && ptr) {
        cxFree(ctx->allocator, ptr);
    }
}

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

char* ui_strdup(UiContext *ctx, const char *str) {
    if(!ctx) {
        return NULL;
    }
    cxstring s = cx_str(str);
    cxmutstr d = cx_strdup_a(ctx->allocator, s);
    return d.ptr;
}

void  ui_reg_destructor(UiContext *ctx, void *data, ui_destructor_func destr) {
    cxMempoolRegister(ctx->mp, data, (cx_destructor_func)destr);
}

void  ui_set_destructor(void *mem, ui_destructor_func destr) {
    cxMempoolSetDestructor(mem, (cx_destructor_func)destr);
}

UiInteger* ui_get_int_var(UiContext *ctx, const char *name) {
    UiVar *var = uic_get_var_t(ctx, name, UI_VAR_INTEGER);
    return var ? var->value : NULL;
}

UiDouble* ui_get_double_var(UiContext *ctx, const char *name) {
    UiVar *var = uic_get_var_t(ctx, name, UI_VAR_DOUBLE);
    return var ? var->value : NULL;
}

UiString* ui_get_string_var(UiContext *ctx, const char *name) {
    UiVar *var = uic_get_var_t(ctx, name, UI_VAR_STRING);
    return var ? var->value : NULL;
}

UiText* ui_get_text_var(UiContext *ctx, const char *name) {
    UiVar *var = uic_get_var_t(ctx, name, UI_VAR_TEXT);
    return var ? var->value : NULL;
}

UiRange* ui_get_range_var(UiContext *ctx, const char *name) {
    UiVar *var = uic_get_var_t(ctx, name, UI_VAR_RANGE);
    return var ? var->value : NULL;
}

UiGeneric* ui_get_generic_var(UiContext *ctx, const char *name) {
    UiVar *var = uic_get_var_t(ctx, name, UI_VAR_GENERIC);
    return var ? var->value : NULL;
}

mercurial