37 void uic_docmgr_init() { |
37 void uic_docmgr_init() { |
38 documents = ucx_map_new(32); |
38 documents = ucx_map_new(32); |
39 } |
39 } |
40 |
40 |
41 void ui_set_document(UiObject *obj, void *document) { |
41 void ui_set_document(UiObject *obj, void *document) { |
42 UiContext *ctx = ucx_map_get(documents, ucx_key(&document, sizeof(void*))); |
42 obj->ctx->set_document(obj->ctx, document); |
43 if(!ctx) { |
|
44 return; |
|
45 } |
|
46 ctx->obj = obj; |
|
47 |
|
48 if(obj->document) { |
|
49 ui_detach_document(obj, obj->document); |
|
50 } |
|
51 obj->document = document; |
|
52 obj->ctx->document = document; |
|
53 |
|
54 UcxMapIterator i = ucx_map_iterator(ctx->vars); |
|
55 UiVar *var; |
|
56 UCX_MAP_FOREACH(key, var, i) { |
|
57 UiVar *v = ucx_map_get(obj->ctx->vars, key); |
|
58 if(v) { |
|
59 if(v->isextern) { |
|
60 fprintf( |
|
61 stderr, |
|
62 "UI Error: external variable cannot be moved\n"); |
|
63 return; |
|
64 } |
|
65 // check type |
|
66 if(var->type != v->type) { |
|
67 fprintf(stderr, "UI Error: var has incompatible type.\n"); |
|
68 return; |
|
69 } |
|
70 |
|
71 // copy value |
|
72 uic_move_var(v, var, 1); |
|
73 var->from = v->from; |
|
74 |
|
75 // TODO: free var struct |
|
76 ucx_map_remove(obj->ctx->vars, key); |
|
77 } |
|
78 } |
|
79 |
|
80 } |
43 } |
81 |
44 |
82 void ui_detach_document(UiObject *obj, void *document) { |
45 void ui_detach_document(UiObject *obj, void *document) { |
83 UiContext *ctx = ucx_map_get(documents, ucx_key(&document, sizeof(void*))); |
46 obj->ctx->detach_document(obj->ctx, document); |
|
47 } |
|
48 |
|
49 void* ui_get_document(UiObject *obj) { |
|
50 return obj->ctx->document; |
|
51 } |
|
52 |
|
53 void ui_set_subdocument(void *document, void *sub) { |
|
54 UiContext *ctx = ui_document_context(document); |
84 if(!ctx) { |
55 if(!ctx) { |
85 fprintf( |
56 fprintf(stderr, "UI Error: pointer is not a document\n"); |
86 stderr, |
|
87 "UiError: ui_detach_document: document is not registered\n"); |
|
88 return; |
|
89 } |
57 } |
90 if(obj->document != document) { |
58 uic_context_set_document(ctx, sub); |
91 fprintf(stderr, "UiError: ui_detach_document: wrong document\n"); |
59 } |
92 return; |
60 |
|
61 void ui_detach_subdocument(void *document, void *sub) { |
|
62 UiContext *ctx = ui_document_context(document); |
|
63 if(!ctx) { |
|
64 fprintf(stderr, "UI Error: pointer is not a document\n"); |
93 } |
65 } |
94 |
66 uic_context_detach_document(ctx, sub); |
95 UcxMapIterator i = ucx_map_iterator(ctx->vars); |
67 } |
96 UiVar *var; |
68 |
97 UCX_MAP_FOREACH(key, var, i) { |
69 void* ui_get_subdocument(void *document) { |
98 if(var->from && var->from != ctx->vars) { |
70 UiContext *ctx = ui_document_context(document); |
99 // this var is bind to an outer widget, so we move it |
71 if(!ctx) { |
100 UcxAllocator *a = var->from->allocator; |
72 fprintf(stderr, "UI Error: pointer is not a document\n"); |
101 UiVar *newvar = a->malloc(a->pool, sizeof(UiVar)); |
|
102 newvar->value = uic_create_value(a, var->type); |
|
103 uic_move_var(var, newvar, 0); |
|
104 newvar->type = var->type; |
|
105 newvar->from = var->from; |
|
106 newvar->isextern = 0; |
|
107 |
|
108 ucx_map_put(var->from, key, newvar); |
|
109 |
|
110 //ucx_map_remove(doc->vars, key); // TODO: dont remove! |
|
111 } |
|
112 } |
73 } |
113 |
74 return ctx->document; |
114 ctx->obj->document = NULL; |
|
115 ctx->obj->ctx->document = NULL; |
|
116 |
|
117 ctx->obj = NULL; |
|
118 } |
75 } |
119 |
76 |
120 void* ui_document_new(size_t size) { |
77 void* ui_document_new(size_t size) { |
121 UcxMempool *mp = ucx_mempool_new(256); |
78 UcxMempool *mp = ucx_mempool_new(256); |
122 UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext)); |
79 UiContext *ctx = ucx_mempool_calloc(mp, 1, sizeof(UiContext)); |
123 ctx->obj = NULL; |
80 ctx->set_document = uic_context_set_document; |
|
81 ctx->detach_document = uic_context_detach_document; |
124 ctx->mempool = mp; |
82 ctx->mempool = mp; |
125 ctx->vars = ucx_map_new_a(mp->allocator, 16); |
83 ctx->vars = ucx_map_new_a(mp->allocator, 16); |
126 //uidoc->subdocument = NULL; |
|
127 |
84 |
128 void *document = ucx_mempool_calloc(mp, 1, size); |
85 void *document = ucx_mempool_calloc(mp, 1, size); |
129 ucx_map_put(documents, ucx_key(&document, sizeof(void*)), ctx); |
86 ucx_map_put(documents, ucx_key(&document, sizeof(void*)), ctx); |
130 return document; |
87 return document; |
131 } |
88 } |
190 } |
147 } |
191 |
148 |
192 void ui_document_regtext(void *doc, char *name, UiText *text) { |
149 void ui_document_regtext(void *doc, char *name, UiText *text) { |
193 UiContext *ctx = ui_document_context(doc); |
150 UiContext *ctx = ui_document_context(doc); |
194 if(ctx) { |
151 if(ctx) { |
195 uic_reg_var(doc, name, UI_VAR_TEXT, sizeof(UiText), text); |
152 uic_reg_var(ctx, name, UI_VAR_TEXT, sizeof(UiText), text); |
196 } |
153 } |
197 } |
154 } |
198 |
155 |
199 void ui_document_reglist(void *doc, char *name, UiList *list) { |
156 void ui_document_reglist(void *doc, char *name, UiList *list) { |
200 UiContext *ctx = ui_document_context(doc); |
157 UiContext *ctx = ui_document_context(doc); |
201 if(ctx) { |
158 if(ctx) { |
202 UiListVar *lv = ui_document_malloc(doc, sizeof(UiListVar)); |
159 UiListVar *lv = ui_document_malloc(doc, sizeof(UiListVar)); |
203 UiListPtr *lp = ui_document_malloc(doc, sizeof(UiListPtr)); |
160 UiListPtr *lp = ui_document_malloc(doc, sizeof(UiListPtr)); |
204 lv->listptr = lp; |
161 lv->listptr = lp; |
205 lp->list = list; |
162 lp->list = list; |
206 uic_reg_var(doc, name, UI_VAR_LIST, sizeof(UiListPtr), lv); |
163 uic_reg_var(ctx, name, UI_VAR_LIST, sizeof(UiListPtr), lv); |
207 } |
164 } |
208 } |
165 } |