ui/common/context.c

Wed, 26 Mar 2014 15:53:43 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 26 Mar 2014 15:53:43 +0100
changeset 8
84a541c6e093
parent 3
c1a75454b444
child 18
06be29a56f8b
permissions
-rw-r--r--

added redo

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2014 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 "context.h"
#include "../ui/window.h"
#include "document.h"


UiContext* uic_context(UiObject *toplevel, UcxMempool *mp) {
    UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext));
    ctx->mempool = mp;
    
    ctx->toplevel = toplevel;
    ctx->vars = ucx_map_new_a(mp->allocator, 16);
    
    return ctx;
}

UiVar* uic_getvar(UiContext *ctx, char *name) {
    // check document variables first
    UiVar *var = uic_document_getvar(
            uic_getdocument(ctx->toplevel->document),
            name);
    // check window vars
    if(!var) {
        var = ucx_map_cstr_get(ctx->vars, name);
    }
    
    return var;
}

UiVar* uic_connect_var(UiContext *ctx, char *name, int type) {
    // TODO: get current map
    UcxMap *from = ctx->vars;
    
    UiVar *var = uic_getvar(ctx, name);
    if(var) {
        // the value is registered
        
        // a little type check
        if(var->type != type) {
            fprintf(stderr, "UI Error: var %s has incompatible type.\n", name);
            return NULL;
        } else {
            // register the current document/wdata map
            // if the document is closed, the var will be moved to this map
            var->from = from;
            
            return var;
        }
    } else {
        // create an empty value and add it first to the window variables
        // it can be moved to the document vars later
        void *value = uic_create_value(ctx->mempool->allocator, type);
        if(!value) {
            fprintf(stderr, "UI Error: Cannot create empty value.\n");
            return NULL;
        }
        UiVar *var = ucx_mempool_malloc(ctx->mempool, sizeof(UiVar));
        var->value = value;
        var->type = type;
        var->isextern = 0;
        var->from = from;
        ucx_map_cstr_put(ctx->vars, name, var);
        return var;
    }
}

void* uic_create_value(UcxAllocator *a, int type) {
    switch(type) {
        case UI_VAR_INTEGER: {
            UiInteger *i = a->calloc(
                    a->pool,
                    1,
                    sizeof(UiInteger));
            return i;
        }
        case UI_VAR_STRING: {
            UiString *s = a->calloc(
                    a->pool,
                    1,
                    sizeof(UiInteger));
            return s;
        }
    }
    return NULL;
}



// public API

int ui_getint(UiObject *obj, char *name) {
    UiVar *var = uic_getvar(obj->ctx, name);
    if(var) {
        if(var->type == UI_VAR_INTEGER) {
            UiInteger *i = var->value;
            if(i->get) {
                return i->get(i);
            } else {
                fprintf(
                        stderr,
                        "UI Error: variable %s is not connected.\n",
                        name);
            }
        } else {
            fprintf(
                    stderr,
                    "UI Error: requested variable %s is not an integer.\n",
                    name);
        }
    } else {
        fprintf(stderr, "UI Error: unkown variable %s.\n", name);
    }
    return 0;
}

mercurial