Tue, 16 Feb 2016 17:39:33 +0100
fixed build with older gtk3
/* * 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 <inttypes.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)); 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; } void uic_context_set_document(UiContext *ctx, void *document) { UiContext *docctx = ui_document_context(document); if(!docctx) { return; } docctx->obj = ctx->obj; if(ctx->document) { uic_context_detach_document(ctx, ctx->document); } //obj->document = document; ctx->document = document; UcxMapIterator i = ucx_map_iterator(docctx->vars); UiVar *var; UCX_MAP_FOREACH(key, var, i) { UiVar *v = ucx_map_get(ctx->vars, key); if(v) { if(v->isextern) { fprintf( stderr, "UI Error: external variable cannot be moved\n"); return; } // check type if(var->type != v->type) { fprintf(stderr, "UI Error: var has incompatible type.\n"); return; } // copy value uic_move_var(v, var, 1); var->from = v->from; // TODO: free var struct ucx_map_remove(ctx->vars, key); } } } void uic_context_detach_document(UiContext *ctx, void *document) { UiContext *docctx = ui_document_context(document); if(!docctx) { fprintf( stderr, "UiError: ui_detach_document: document is not registered\n"); return; } if(ctx->document != document) { fprintf(stderr, "UiError: ui_detach_document: wrong document\n"); return; } UcxMapIterator i = ucx_map_iterator(docctx->vars); UiVar *var; UCX_MAP_FOREACH(key, var, i) { if(var->from && var->from != docctx->vars) { // this var is bind to an outer widget, so we move it UcxAllocator *a = var->from->allocator; UiVar *newvar = a->malloc(a->pool, sizeof(UiVar)); newvar->value = uic_create_value(a, var->type); uic_move_var(var, newvar, 0); newvar->type = var->type; newvar->from = var->from; newvar->isextern = 0; ucx_map_put(var->from, key, newvar); //ucx_map_remove(doc->vars, key); // TODO: dont remove! } } if(docctx->obj) { docctx->obj->ctx->document = NULL; } docctx->obj = NULL; } 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_connect_var(UiContext *ctx, char *name, int type) { // TODO: get current map (Document Container, Tabbed Pane) UcxMap *from = ctx->vars; UiVar *var = uic_get_var(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 to the context 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_move_var(UiVar *from, UiVar *to, int set) { switch(from->type) { case UI_VAR_INTEGER: { //memcpy(to->value, from->value, sizeof(UiInteger)); UiInteger *f = from->value; UiInteger *t = to->value; t->get = f->get; t->set = f->set; t->obj = f->obj; if(set) { t->set(t, t->value); } else { //t->value = t->get(t); f->value = f->get(f); //t->value = 0; } break; } case UI_VAR_STRING: { // TODO break; } case UI_VAR_TEXT: { UiText *f = from->value; UiText *t = to->value; char *tvalue = t->value; int tpos = t->pos; memcpy(t, f, sizeof(UiText)); if(set && tvalue) { t->set(t, tvalue); t->setposition(t, tpos); } else { f->value = f->get(f); f->pos = f->position(f); } break; } case UI_VAR_LIST: { UiListVar *f = from->value; UiListVar *t = to->value; UiList *list = t->listptr->list; UiObserver *observers = f->listptr->list->observers; t->listptr = f->listptr; if(set) { t->listptr->list = list; list->observers = observers; ui_notify(observers, list); } break; } } } void uic_reg_var(UiContext *ctx, char *name, int type, size_t vs, void *value) { UiVar *newvar = ucx_mempool_malloc(ctx->mempool, sizeof(UiVar)); newvar->isextern = 1; newvar->type = type; newvar->value = value; newvar->from = NULL; uic_add_var(ctx, name, newvar, type, vs); } void uic_add_var( UiContext *ctx, char *name, UiVar *newvar, int type, size_t vs) { // if a parent context has a variable with this name, we remove it and put // it to this context UiVar *var = ctx->obj ? uic_get_var(ctx->obj->ctx, name) : NULL; if(var && var->from != ctx->vars) { // external variables cannot be moved if(var->isextern) { fprintf( stderr, "UI Error: external variable %s " "cannot be moved to the document.\n", name); return; } // check type if(var->type != type) { fprintf(stderr, "UI Error: var %s has incompatible type.\n", name); return; } // override document var with window context var memcpy(newvar->value, var->value, vs); newvar->from = var->from; // TODO: free var struct ucx_map_cstr_remove(var->from, name); } // finally, add the new variable to the document ucx_map_cstr_put(ctx->vars, name, newvar); } 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; } case UI_VAR_TEXT: { UiText *t = a->calloc( a->pool, 1, sizeof(UiText)); return t; } case UI_VAR_LIST: { UiListVar *l = a->malloc(a->pool, sizeof(UiListVar)); UiListPtr *lp = a->malloc(a->pool, sizeof(UiListPtr)); l->listptr = lp; lp->list = NULL; // TODO: create empty list return l; } } return NULL; } // public API void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata) { ctx->close_callback = fnc; ctx->close_data = udata; } int ui_getint(UiObject *obj, char *name) { UiVar *var = uic_get_var(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; } char* ui_getstr(UiObject *obj, char *name) { UiVar *var = uic_get_var(obj->ctx, name); if(var) { if(var->type == UI_VAR_STRING) { UiString *s = var->value; if(s->get) { return s->get(s); } else { fprintf( stderr, "UI Error: variable %s is not connected.\n", name); } } else { fprintf( stderr, "UI Error: requested variable %s is not an string.\n", name); } } else { fprintf(stderr, "UI Error: unkown variable %s.\n", name); } return NULL; } char* ui_gettext(UiObject *obj, char *name) { UiVar *var = uic_get_var(obj->ctx, name); if(var) { if(var->type == UI_VAR_TEXT) { UiText *s = var->value; if(s->get) { return s->get(s); } else { fprintf( stderr, "UI Error: variable %s is not connected.\n", name); } } else { fprintf( stderr, "UI Error: requested variable %s is not a text.\n", name); } } else { fprintf(stderr, "UI Error: unkown variable %s.\n", name); } return NULL; } 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; }