ui/common/document.c

changeset 1
eb5269000bc8
child 2
eeb50c534497
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/common/document.c	Sun Dec 08 11:20:41 2013 +0000
@@ -0,0 +1,117 @@
+/*
+ * 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 "document.h"
+
+static UcxMap *documents;
+
+void uic_docmgr_init() {
+    documents = ucx_map_new(32);
+}
+
+void* ui_document_create(UiObject *obj, size_t size) {
+    UcxMempool *mp = ucx_mempool_new(256);
+    UiDocument *uidoc = ucx_mempool_malloc(mp, sizeof(UiDocument));
+    uidoc->obj = obj;
+    uidoc->allocator = ucx_mempool_allocator(mp);
+    uidoc->mempool = mp;
+    uidoc->vars = ucx_map_new_a(uidoc->allocator, 16);
+    
+    void *document = ucx_mempool_calloc(mp, 1, size);
+    ucx_map_put(documents, ucx_key(&document, sizeof(void*)), uidoc);
+    return document;
+}
+
+void ui_document_destroy(void *doc) {
+    // TODO
+}
+
+void* ui_document_malloc(void *doc, size_t size) {
+    UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
+    return ucx_mempool_malloc(uidoc->mempool, size);
+}
+
+void* ui_document_calloc(void *doc, size_t nelem, size_t elsize) {
+    UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
+    return ucx_mempool_calloc(uidoc->mempool, nelem, elsize);
+}
+
+void  ui_document_free(void *doc, void *ptr) {
+    UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
+    ucx_mempool_free(uidoc->mempool, ptr);
+}
+
+void* ui_document_realloc(void *doc, void *ptr, size_t size) {
+    UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
+    return ucx_mempool_realloc(uidoc->mempool, ptr, size);
+}
+
+
+UiVar* uic_document_getvar(UiDocument *doc, char *name) {
+    return ucx_map_cstr_get(doc->vars, name);
+}
+
+void ui_document_addint(void *doc, char *name) {
+    UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
+    if(!uidoc) {
+        return;
+    }
+    
+    UiVar *dvar = uic_document_getvar(uidoc, name);
+    UiVar *wvar = uic_getvar(uidoc->obj, name);
+    if(!dvar) {
+        UiVar *var = ucx_mempool_malloc(uidoc->mempool, sizeof(UiVar));
+        var->isextern = 0;
+        var->type = 1;
+        UiInteger *i = ucx_mempool_calloc(uidoc->mempool, 1, sizeof(UiVar));
+        if(wvar && !wvar->isextern) {
+            *i = *(UiInteger*)wvar->value;
+        }
+    }
+    
+    if(wvar && wvar->isextern) {
+        fprintf(stderr, "UI Error: window variable moved to document!\n");
+    }
+    uic_rmvar(uidoc->obj, name);
+}
+
+void ui_document_regint(void *doc, char *name, UiInteger *i) {
+    
+}
+
+void ui_document_setint(void *doc, char *name, int val) {
+    
+}
+
+int  ui_document_getint(void *doc, char *name) {
+    
+}

mercurial