32 #include <inttypes.h> |
32 #include <inttypes.h> |
33 #include <stdarg.h> |
33 #include <stdarg.h> |
34 |
34 |
35 #include <cx/array_list.h> |
35 #include <cx/array_list.h> |
36 #include <cx/compare.h> |
36 #include <cx/compare.h> |
37 #include <cx/basic_mempool.h> |
37 #include <cx/mempool.h> |
38 |
38 |
39 #include "context.h" |
39 #include "context.h" |
40 #include "../ui/window.h" |
40 #include "../ui/window.h" |
41 #include "document.h" |
41 #include "document.h" |
42 #include "types.h" |
42 #include "types.h" |
43 |
43 |
44 static UiContext* global_context; |
44 static UiContext* global_context; |
45 |
45 |
46 void uic_init_global_context(void) { |
46 void uic_init_global_context(void) { |
47 CxMempool *mp = cxBasicMempoolCreate(32); |
47 CxMempool *mp = cxBasicMempoolCreate(32); |
48 global_context = uic_context(NULL, mp->allocator); |
48 global_context = uic_context(NULL, mp); |
49 } |
49 } |
50 |
50 |
51 UiContext* ui_global_context(void) { |
51 UiContext* ui_global_context(void) { |
52 return global_context; |
52 return global_context; |
53 } |
53 } |
54 |
54 |
55 UiContext* uic_context(UiObject *toplevel, const CxAllocator *a) { |
55 UiContext* uic_context(UiObject *toplevel, CxMempool *mp) { |
56 UiContext *ctx = cxMalloc(a, sizeof(UiContext)); |
56 UiContext *ctx = cxMalloc(mp->allocator, sizeof(UiContext)); |
57 memset(ctx, 0, sizeof(UiContext)); |
57 memset(ctx, 0, sizeof(UiContext)); |
58 ctx->allocator = a; |
58 ctx->mp = mp; |
|
59 ctx->allocator = mp->allocator; |
59 ctx->obj = toplevel; |
60 ctx->obj = toplevel; |
60 ctx->vars = cxHashMapCreate(a, CX_STORE_POINTERS, 16); |
61 ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16); |
61 |
62 |
62 ctx->documents = cxLinkedListCreate(a, cx_cmp_intptr, CX_STORE_POINTERS); |
63 ctx->documents = cxLinkedListCreate(mp->allocator, cx_cmp_intptr, CX_STORE_POINTERS); |
63 ctx->group_widgets = cxLinkedListCreate(a, NULL, sizeof(UiGroupWidget)); |
64 ctx->group_widgets = cxLinkedListCreate(mp->allocator, NULL, sizeof(UiGroupWidget)); |
64 ctx->groups = cxArrayListCreate(a, cx_cmp_int, sizeof(int), 32); |
65 ctx->groups = cxArrayListCreate(mp->allocator, cx_cmp_int, sizeof(int), 32); |
65 |
66 |
66 ctx->attach_document = uic_context_attach_document; |
67 ctx->attach_document = uic_context_attach_document; |
67 ctx->detach_document2 = uic_context_detach_document2; |
68 ctx->detach_document2 = uic_context_detach_document2; |
68 |
69 |
69 #ifdef UI_GTK |
70 #ifdef UI_GTK |
193 var->type = type; |
194 var->type = type; |
194 var->value = uic_create_value(ctx, type); |
195 var->value = uic_create_value(ctx, type); |
195 var->from = NULL; |
196 var->from = NULL; |
196 var->from_ctx = ctx; |
197 var->from_ctx = ctx; |
197 |
198 |
|
199 cxMempoolRegister(ctx->mp, var, (cx_destructor_func)uic_unbind_var); |
|
200 |
198 if(!ctx->vars_unbound) { |
201 if(!ctx->vars_unbound) { |
199 ctx->vars_unbound = cxHashMapCreate(ctx->allocator, CX_STORE_POINTERS, 16); |
202 ctx->vars_unbound = cxHashMapCreate(ctx->allocator, CX_STORE_POINTERS, 16); |
200 } |
203 } |
201 cxMapPut(ctx->vars_unbound, name, var); |
204 cxMapPut(ctx->vars_unbound, name, var); |
202 |
205 |
|
206 return var; |
|
207 } |
|
208 |
|
209 UiVar* uic_create_value_var(UiContext* ctx, void* value) { |
|
210 UiVar *var = (UiVar*)ui_malloc(ctx, sizeof(UiVar)); |
|
211 var->from = NULL; |
|
212 var->from_ctx = NULL; |
|
213 var->value = value; |
|
214 var->type = UI_VAR_SPECIAL; |
203 return var; |
215 return var; |
204 } |
216 } |
205 |
217 |
206 void* uic_create_value(UiContext *ctx, UiVarType type) { |
218 void* uic_create_value(UiContext *ctx, UiVarType type) { |
207 void *val = NULL; |
219 void *val = NULL; |