ui/common/document.c

Mon, 24 Mar 2014 11:21:59 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 24 Mar 2014 11:21:59 +0100
changeset 6
05a18c56d9ca
parent 3
c1a75454b444
child 33
458831c574f4
permissions
-rw-r--r--

added undo for text

1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "document.h"
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 static UcxMap *documents;
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 void uic_docmgr_init() {
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 documents = ucx_map_new(32);
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
41 void ui_set_document(UiObject *obj, void *document) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
42 UiDocument *doc = ucx_map_get(documents, ucx_key(&document, sizeof(void*)));
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
43 if(!doc) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
44 return;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
45 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
46 doc->obj = obj;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
47
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
48 if(obj->document) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
49 ui_detach_document(obj, obj->document);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
50 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
51 obj->document = document;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
52
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
53 UcxMapIterator i = ucx_map_iterator(doc->vars);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
54 UiVar *var;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
55 UCX_MAP_FOREACH(key, var, i) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
56 UiVar *v = ucx_map_get(obj->ctx->vars, key);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
57 if(v) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
58 if(v->isextern) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
59 fprintf(
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
60 stderr,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
61 "UI Error: external variable cannot be moved\n");
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
62 return;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
63 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
64 // check type
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
65 if(var->type != v->type) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
66 fprintf(stderr, "UI Error: var has incompatible type.\n");
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
67 return;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
68 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
69
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
70 // copy value
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
71 uic_var_move(v, var, 1);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
72 var->from = v->from;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
73
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
74 // TODO: free var struct
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
75 ucx_map_remove(obj->ctx->vars, key);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
76 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
77 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
78
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
79 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
80
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
81 void ui_detach_document(UiObject *obj, void *document) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
82 UiDocument *doc = ucx_map_get(documents, ucx_key(&document, sizeof(void*)));
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
83 if(!doc) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
84 return;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
85 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
86
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
87 UcxMapIterator i = ucx_map_iterator(doc->vars);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
88 UiVar *var;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
89 UCX_MAP_FOREACH(key, var, i) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
90 if(var->from && var->from != doc->vars) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
91 // this var is bind to an outer widget, so we move it
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
92 UcxAllocator *a = var->from->allocator;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
93 UiVar *newvar = a->malloc(a->pool, sizeof(UiVar));
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
94 newvar->value = uic_create_value(a, var->type);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
95 uic_var_move(var, newvar, 0);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
96 newvar->type = var->type;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
97 newvar->from = var->from;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
98 newvar->isextern = 0;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
99
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
100 ucx_map_put(var->from, key, newvar);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
101
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
102 //ucx_map_remove(doc->vars, key); // TODO: dont remove!
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
103 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
104 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
105
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
106 doc->obj->document = NULL;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
107 doc->obj = NULL;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
108 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
109
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
110 void* ui_document_new(size_t size) {
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 UcxMempool *mp = ucx_mempool_new(256);
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 UiDocument *uidoc = ucx_mempool_malloc(mp, sizeof(UiDocument));
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
113 uidoc->obj = NULL;
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 uidoc->mempool = mp;
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
115 uidoc->vars = ucx_map_new_a(mp->allocator, 16);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
116 uidoc->subdocument = NULL;
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 void *document = ucx_mempool_calloc(mp, 1, size);
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 ucx_map_put(documents, ucx_key(&document, sizeof(void*)), uidoc);
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 return document;
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 void ui_document_destroy(void *doc) {
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 // TODO
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 void* ui_document_malloc(void *doc, size_t size) {
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 return ucx_mempool_malloc(uidoc->mempool, size);
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 void* ui_document_calloc(void *doc, size_t nelem, size_t elsize) {
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 return ucx_mempool_calloc(uidoc->mempool, nelem, elsize);
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 void ui_document_free(void *doc, void *ptr) {
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 ucx_mempool_free(uidoc->mempool, ptr);
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 void* ui_document_realloc(void *doc, void *ptr, size_t size) {
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 return ucx_mempool_realloc(uidoc->mempool, ptr, size);
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
147 UiDocument* uic_getdocument(void *doc) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
148 return doc ? ucx_map_get(documents, ucx_key(&doc, sizeof(void*))) : NULL;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
149 }
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 UiVar* uic_document_getvar(UiDocument *doc, char *name) {
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
152 return doc ? ucx_map_cstr_get(doc->vars, name) : NULL;
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
155 void uic_document_addvar(void *doc, char *name, int type, size_t vs) {
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 if(!uidoc) {
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 return;
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
161 UiVar *newvar = ucx_mempool_malloc(uidoc->mempool, sizeof(UiVar));
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
162 newvar->isextern = 0;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
163 newvar->type = type;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
164 newvar->value = ucx_mempool_calloc(uidoc->mempool, 1, vs);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
165 newvar->from = NULL;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
166
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
167 uic_document_addvar_v(uidoc, name, newvar, type, vs);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
168 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
169
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
170 void uic_document_addvar_v(
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
171 UiDocument *uidoc,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
172 char *name,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
173 UiVar *newvar,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
174 int type,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
175 size_t vs)
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
176 {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
177 // if the window context has this variable, we remove it and put it to
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
178 // the document vars
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
179 UiVar *var = uidoc->obj ?
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
180 ucx_map_cstr_get(uidoc->obj->ctx->vars, name) : NULL;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
181 if(var) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
182 // external variables cannot be moved
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
183 if(var->isextern) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
184 fprintf(
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
185 stderr,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
186 "UI Error: external variable %s "
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
187 "cannot be moved to the document.\n",
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
188 name);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
189 return;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
190 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
191
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
192 // check type
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
193 if(var->type != type) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
194 fprintf(stderr, "UI Error: var %s has incompatible type.\n", name);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
195 return;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
196 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
197
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
198 // override document var with window context var
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
199 memcpy(newvar->value, var->value, vs);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
200
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
201 newvar->from = var->from;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
202
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
203 // TODO: free var struct
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
204 ucx_map_cstr_remove(uidoc->obj->ctx->vars, name);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
205 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
206
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
207 // finally, add the new variable to the document
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
208 ucx_map_cstr_put(uidoc->vars, name, newvar);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
209 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
210
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
211 void uic_document_regvar(
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
212 void *doc,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
213 char *name,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
214 int type,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
215 size_t vs,
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
216 void *value)
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
217 {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
218 UiDocument *uidoc = ucx_map_get(documents, ucx_key(&doc, sizeof(void*)));
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
219 if(!uidoc) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
220 return;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
221 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
222
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
223 UiVar *newvar = ucx_mempool_malloc(uidoc->mempool, sizeof(UiVar));
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
224 newvar->isextern = 1;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
225 newvar->type = type;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
226 newvar->value = value;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
227 newvar->from = NULL;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
228
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
229 uic_document_addvar_v(uidoc, name, newvar, type, vs);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
230 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
231
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
232 void uic_var_move(UiVar *from, UiVar *to, int set) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
233 switch(from->type) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
234 case UI_VAR_INTEGER: {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
235 //memcpy(to->value, from->value, sizeof(UiInteger));
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
236 UiInteger *f = from->value;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
237 UiInteger *t = to->value;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
238 t->get = f->get;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
239 t->set = f->set;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
240 t->obj = f->obj;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
241 if(set) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
242 t->set(t, t->value);
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
243 } else {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
244 //t->value = t->get(t);
3
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
245 f->value = f->get(f);
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
246 //t->value = 0;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
247 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
248 break;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
249 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
250 case UI_VAR_STRING: {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
251 break;
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 }
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
254 }
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
255
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
256 void ui_document_addint(void *doc, char *name) {
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
257 uic_document_addvar(doc, name, UI_VAR_INTEGER, sizeof(UiInteger));
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 void ui_document_regint(void *doc, char *name, UiInteger *i) {
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
261 uic_document_regvar(doc, name, UI_VAR_INTEGER, sizeof(UiInteger), i);
1
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 }
eb5269000bc8 added some document functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263

mercurial