ui/common/context.c

changeset 128
c284c15509a8
parent 121
1cc365c34125
child 131
774b741984a2
--- a/ui/common/context.c	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/common/context.c	Sat Sep 17 19:32:44 2016 +0200
@@ -56,12 +56,17 @@
     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) {
     UiContext *docctx = ui_document_context(document);
     if(!docctx) {
         return;
     }
     docctx->obj = ctx->obj;
+    docctx->parent = ctx;
     
     if(ctx->document) {
         uic_context_detach_document(ctx, ctx->document);
@@ -72,7 +77,7 @@
     UcxMapIterator i = ucx_map_iterator(docctx->vars);
     UiVar *var;
     UCX_MAP_FOREACH(key, var, i) {
-        UiVar *v = ucx_map_get(ctx->vars, key);
+        UiVar *v = ucx_map_get(uic_root_context(ctx)->vars, key);
         if(v) {
             if(v->isextern) {
                 fprintf(
@@ -87,7 +92,7 @@
             }
             
             // copy value
-            uic_move_var(v, var, 1);
+            uic_move_var(v, var, TRUE);
             var->from = v->from;
             
             // TODO: free var struct
@@ -128,11 +133,12 @@
         }
     }
     
-    if(docctx->obj) {
-        docctx->obj->ctx->document = NULL;
+    if(docctx->parent) {
+        docctx->parent->document = NULL;
     }
     
     docctx->obj = NULL;
+    docctx->parent = NULL;
 }
 
 UiVar* uic_get_var(UiContext *ctx, char *name) {
@@ -188,7 +194,7 @@
     }
 }
 
-void uic_move_var(UiVar *from, UiVar *to, int set) {
+void uic_move_var(UiVar *from, UiVar *to, UiBool set) {
     switch(from->type) {
         case UI_VAR_INTEGER: {
             //memcpy(to->value, from->value, sizeof(UiInteger));
@@ -213,10 +219,10 @@
         case UI_VAR_TEXT: {
             UiText *f = from->value;
             UiText *t = to->value;
-            char *tvalue = t->value;
+            char *tvalue = t->value ? t->value : "";
             int tpos = t->pos;
             memcpy(t, f, sizeof(UiText));
-            if(set && tvalue) {
+            if(set) {
                 t->set(t, tvalue);
                 t->setposition(t, tpos);
             } else {

mercurial