ui/common/document.c

changeset 52
25e5390cce41
parent 37
56016468753d
child 140
c03c338a7dcf
--- a/ui/common/document.c	Fri Jun 13 10:39:54 2014 +0200
+++ b/ui/common/document.c	Tue Jul 22 09:51:17 2014 +0200
@@ -39,91 +39,48 @@
 }
 
 void ui_set_document(UiObject *obj, void *document) {
-    UiContext *ctx = ucx_map_get(documents, ucx_key(&document, sizeof(void*)));
-    if(!ctx) {
-        return;
-    }
-    ctx->obj = obj;
-    
-    if(obj->document) {
-        ui_detach_document(obj, obj->document);
-    }
-    obj->document = document;
-    obj->ctx->document = document;
-    
-    UcxMapIterator i = ucx_map_iterator(ctx->vars);
-    UiVar *var;
-    UCX_MAP_FOREACH(key, var, i) {
-        UiVar *v = ucx_map_get(obj->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(obj->ctx->vars, key);
-        }
-    }
-    
+    obj->ctx->set_document(obj->ctx, document);
 }
 
 void ui_detach_document(UiObject *obj, void *document) {
-    UiContext *ctx = ucx_map_get(documents, ucx_key(&document, sizeof(void*)));
+    obj->ctx->detach_document(obj->ctx, document);
+}
+
+void* ui_get_document(UiObject *obj) {
+    return obj->ctx->document;
+}
+
+void ui_set_subdocument(void *document, void *sub) {
+    UiContext *ctx = ui_document_context(document);
     if(!ctx) {
-        fprintf(
-                stderr,
-                "UiError: ui_detach_document: document is not registered\n");
-        return;
-    }
-    if(obj->document != document) {
-        fprintf(stderr, "UiError: ui_detach_document: wrong document\n");
-        return;
+        fprintf(stderr, "UI Error: pointer is not a document\n");
     }
-    
-    UcxMapIterator i = ucx_map_iterator(ctx->vars);
-    UiVar *var;
-    UCX_MAP_FOREACH(key, var, i) {
-        if(var->from && var->from != ctx->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!
-        }
+    uic_context_set_document(ctx, sub);
+}
+
+void ui_detach_subdocument(void *document, void *sub) {
+    UiContext *ctx = ui_document_context(document);
+    if(!ctx) {
+        fprintf(stderr, "UI Error: pointer is not a document\n");
     }
-    
-    ctx->obj->document = NULL;
-    ctx->obj->ctx->document = NULL;
-    
-    ctx->obj = NULL;
+    uic_context_detach_document(ctx, sub);
+}
+
+void* ui_get_subdocument(void *document) {
+    UiContext *ctx = ui_document_context(document);
+    if(!ctx) {
+        fprintf(stderr, "UI Error: pointer is not a document\n");
+    }
+    return ctx->document;
 }
 
 void* ui_document_new(size_t size) {
     UcxMempool *mp = ucx_mempool_new(256);
-    UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext));
-    ctx->obj = NULL;
+    UiContext *ctx = ucx_mempool_calloc(mp, 1, sizeof(UiContext));
+    ctx->set_document = uic_context_set_document;
+    ctx->detach_document = uic_context_detach_document;
     ctx->mempool = mp;
     ctx->vars = ucx_map_new_a(mp->allocator, 16);
-    //uidoc->subdocument = NULL;
     
     void *document = ucx_mempool_calloc(mp, 1, size);
     ucx_map_put(documents, ucx_key(&document, sizeof(void*)), ctx);
@@ -192,7 +149,7 @@
 void ui_document_regtext(void *doc, char *name, UiText *text) {
     UiContext *ctx = ui_document_context(doc);
     if(ctx) {
-        uic_reg_var(doc, name, UI_VAR_TEXT, sizeof(UiText), text);
+        uic_reg_var(ctx, name, UI_VAR_TEXT, sizeof(UiText), text);
     }
 }
 
@@ -203,6 +160,6 @@
         UiListPtr *lp = ui_document_malloc(doc, sizeof(UiListPtr));
         lv->listptr = lp;
         lp->list = list;
-        uic_reg_var(doc, name, UI_VAR_LIST, sizeof(UiListPtr), lv);
+        uic_reg_var(ctx, name, UI_VAR_LIST, sizeof(UiListPtr), lv);
     }
 }

mercurial