ui/common/context.c

changeset 2
eeb50c534497
parent 1
eb5269000bc8
child 3
c1a75454b444
--- a/ui/common/context.c	Sun Dec 08 11:20:41 2013 +0000
+++ b/ui/common/context.c	Fri Mar 21 13:20:53 2014 +0100
@@ -32,80 +32,117 @@
 
 #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));
-    UcxAllocator *allocator = ucx_mempool_allocator(mp);
     ctx->mempool = mp;
-    ctx->allocator = allocator;
     
     ctx->toplevel = toplevel;
-    ctx->vars = ucx_map_new_a(allocator, 16);
+    ctx->vars = ucx_map_new_a(mp->allocator, 16);
     
     return ctx;
 }
 
-UiVar* uic_getvar(UiObject *obj, char *name) {
-    if(!obj) {
-        return NULL;
+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 ucx_map_cstr_get(obj->ctx->vars, name);
+    
+    return var;
 }
 
-void uic_rmvar(UiObject *obj, char *name) {
-    if(obj) {
-        UiVar *var = uic_getvar(obj, name);
-        if(var) {
-            if(var->isextern) {
-                ucx_mempool_free(obj->ctx->mempool, var->value);
-            }
-            ucx_mempool_free(obj->ctx->mempool, 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
+            
+            // TODO: get current 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 ui_window_addint(UiObject *obj, char *name) {
-    if(uic_getvar(obj, name)) {
-        // var already exists
-        return;
+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;
+        }
     }
-    UiInteger *i = ucx_mempool_calloc(obj->ctx->mempool, 1, sizeof(UiInteger));
-    UiVar *var = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiVar));
-    var->value = i;
-    var->type = 1;
-    var->isextern = 0;
-    ucx_map_cstr_put(obj->ctx->vars, name, var);
+    return NULL;
 }
 
-void ui_window_regint(UiObject *obj, char *name, UiInteger *i) {
-    if(uic_getvar(obj, name)) {
-        // var already exists
-        return;
+
+
+// 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);
     }
-    UiVar *var = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiVar));
-    var->value = i;
-    var->type = 1;
-    var->isextern = 1;
-    ucx_map_cstr_put(obj->ctx->vars, name, var);
+    return 0;
 }
 
-void ui_window_setint(UiObject *obj, char *name, int val) {
-    UiVar *var = uic_getvar(obj, name);
-    if(var && var->type == 1) {
-        UiInteger *i = var->value;
-        i->set(i, val);
-    } else {
-        // TODO: error message
-    }
-}
-
-int ui_window_getint(UiObject *obj, char *name) {
-    UiVar *var = uic_getvar(obj, name);
-    if(var && var->type == 1) {
-        UiInteger *i = var->value;
-        return i->get(i);
-    } else {
-        return 0; // TODO: error message
-    }
-}

mercurial