ui/common/context.c

Sat, 05 Dec 2020 10:32:49 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 05 Dec 2020 10:32:49 +0100
changeset 160
b3f36bfe1b41
parent 154
8a4451fcb736
child 163
b70e2a77dea0
permissions
-rw-r--r--

Motif: enable xft text rendering by default

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


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->set_document = uic_context_set_document;
    ctx->detach_document = uic_context_detach_document;
    
#ifdef UI_GTK
    if(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_set_document(UiContext *ctx, void *document) {
    if(ctx->document == document) {
        return;
    }
    if(ctx->document) {
        uic_context_detach_document(ctx);
    }
    ctx->document = document;
    
    UiContext *docctx = ui_document_context(document);
    if(!docctx) {
        fprintf(stderr, "UiError: uic_context_set_document: pointer is not a document\n");
        return;
    }
    docctx->obj = ctx->obj;
    docctx->parent = ctx;
    
    UiContext *root = uic_root_context(ctx);
    if(!root->bound || root->bound->count == 0) {
        return;
    }
    
    // there are bound variables in the root ctx
    // copy bindings with correct name to doc vars
    UcxMapIterator i = ucx_map_iterator(docctx->vars);
    UiVar *var;
    UCX_MAP_FOREACH(key, var, i) {
        UiVar *v = ucx_map_get(root->bound, key);
        if(v) {
            // copy binding: after this, all doc vars with names of previously
            // bound variables are bound to the widgets
            // the widgets still hold a pointer to the root ctx vars, but this
            // vars have a pointer to the document variable value - confusing
            uic_copy_binding(v, var, TRUE);
        }
    }
}

void uic_context_detach_document(UiContext *ctx) {
    if(!ctx->document) {
        return;
    }
    
    UiContext *docctx = ui_document_context(ctx->document);
    if(!docctx) {
        fprintf(stderr, "UiError: Cannot detach document: no context\n");
        return;
    }
    ctx->document = NULL;
    docctx->parent = NULL;
    docctx->obj = NULL;
    
    // unbind all vars
    UcxMapIterator i = ucx_map_iterator(docctx->vars);
    UiVar *var;
    UCX_MAP_FOREACH(key, var, i) {
        uic_save_var(var);
        if(var->from) {
            // restore old root bound var val
            var->from->value = var->from->orig_val;
            var->from->orig_val = NULL;
            // copy
            uic_copy_binding(var, var->from, FALSE);
        }
        uic_unbind_var(var);
    }
}

UiVar* uic_get_var(UiContext *ctx, char *name) {
    // check document variables first
    UiVar *var = NULL;
    UiContext *doc_ctx = ui_document_context(ctx->document);
    if(doc_ctx) {
        var = uic_get_var(doc_ctx, name);
    }
    
    // check variables of this context
    if(!var) {
        var = ucx_map_cstr_get(ctx->vars, name);
    }
    
    return var;
}

UiVar* uic_create_var(UiContext *ctx, char *name, UiVarType type) {
    // check if this context has a var with this name
    // otherweise add it to the bound map
    UiVar *cv = ucx_map_cstr_get(ctx->vars, name);
    if(cv) {
        if(cv->type == type) {
            return cv;
        } else {
            fprintf(stderr, "UiError: var '%s' already exists with different type\n", name);
        }
    }
    
    UiVar *var;
    if(ctx->bound) {
        var = ucx_map_cstr_get(ctx->bound, name);
        if(var) {
            if(var->type != type) {
                fprintf(stderr, "UiError: var '%s' already bound with different type\n", name);
            } else {
                return var;
            }
        }
    }
    
    // create var and add it to the bound map
    // this map contains vars that are created by widgets
    // the vars can later be moved to subdocuments
    var = ui_malloc(ctx, sizeof(UiVar));
    var->type = type;
    var->value = uic_create_value(ctx, type);
    var->orig_val = NULL;
    var->from = NULL; // not connected to a doc var
    
    if(!ctx->bound) {
        ctx->bound = ucx_map_new_a(ctx->mempool->allocator, 16);
    }
    ucx_map_cstr_put(ctx->bound, name, var);
    
    // if a subdocument has a variable with this name, we must copy
    // the binding to the doc var
    UiContext *doc_ctx = ui_document_context(ctx->document);
    if(doc_ctx) {
        UiVar *docvar = uic_get_var(doc_ctx, name);
        if(docvar) {
            var->orig_val = var->value;
            var->value = docvar->value;
            docvar->from = var;
        }
    }
    
    return var;
}

void* uic_create_value(UiContext *ctx, UiVarType type) {
    void *val = NULL;
    switch(type) {
        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) {
    if(copytodoc && from->from) {
        fprintf(stderr, "UiError: var already connected to a document\n");
        return;
    }
    
    // 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;
    
        from->orig_val = from->value;
        from->value = to->value;
    }
    
    // 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_var(UiVar *var) {
    switch(var->type) {
        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_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) {
    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->from = NULL;
    var->orig_val = NULL;
    var->type = type;
    var->value = value;
    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);
    }
    
    // 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
}


// public API

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;
            }
        }
        ui_set_enabled(gw->widget, enable);
    }
    
    if(groups) {
        free(groups);
    }
}

void uic_add_group_widget(UiContext *ctx, void *widget, UcxList *groups) {
    UcxMempool *mp = ctx->mempool;
    UiGroupWidget *gw = ucx_mempool_malloc(mp, sizeof(UiGroupWidget));
    
    gw->widget = widget;
    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;
}

mercurial