43 |
43 |
44 |
44 |
45 static UiContext* global_context; |
45 static UiContext* global_context; |
46 |
46 |
47 void uic_init_global_context(void) { |
47 void uic_init_global_context(void) { |
48 CxMempool *mp = cxBasicMempoolCreate(32); |
48 CxMempool *mp = cxMempoolCreateSimple(32); |
49 global_context = uic_context(NULL, mp); |
49 global_context = uic_context(NULL, mp); |
50 } |
50 } |
51 |
51 |
52 UiContext* ui_global_context(void) { |
52 UiContext* ui_global_context(void) { |
53 return global_context; |
53 return global_context; |
59 ctx->mp = mp; |
59 ctx->mp = mp; |
60 ctx->allocator = mp->allocator; |
60 ctx->allocator = mp->allocator; |
61 ctx->obj = toplevel; |
61 ctx->obj = toplevel; |
62 ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16); |
62 ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16); |
63 |
63 |
64 ctx->documents = cxLinkedListCreate(mp->allocator, cx_cmp_intptr, CX_STORE_POINTERS); |
64 ctx->documents = cxLinkedListCreate(mp->allocator, cx_cmp_ptr, CX_STORE_POINTERS); |
65 ctx->group_widgets = cxLinkedListCreate(mp->allocator, cx_cmp_ptr, sizeof(UiGroupWidget)); |
65 ctx->group_widgets = cxLinkedListCreate(mp->allocator, cx_cmp_ptr, sizeof(UiGroupWidget)); |
66 ctx->groups = cxArrayListCreate(mp->allocator, cx_cmp_int, sizeof(int), 32); |
66 ctx->groups = cxArrayListCreate(mp->allocator, cx_cmp_int, sizeof(int), 32); |
67 |
67 |
68 ctx->attach_document = uic_context_attach_document; |
68 ctx->attach_document = uic_context_attach_document; |
69 ctx->detach_document2 = uic_context_detach_document2; |
69 ctx->detach_document2 = uic_context_detach_document2; |
90 void uic_context_attach_document(UiContext *ctx, void *document) { |
90 void uic_context_attach_document(UiContext *ctx, void *document) { |
91 cxListAdd(ctx->documents, document); |
91 cxListAdd(ctx->documents, document); |
92 ctx->document = document; |
92 ctx->document = document; |
93 |
93 |
94 UiContext *doc_ctx = ui_document_context(document); |
94 UiContext *doc_ctx = ui_document_context(document); |
|
95 doc_ctx->parent = ctx; |
95 |
96 |
96 // check if any parent context has an unbound variable with the same name |
97 // check if any parent context has an unbound variable with the same name |
97 // as any document variable |
98 // as any document variable |
98 UiContext *var_ctx = ctx; |
99 UiContext *var_ctx = ctx; |
99 while(var_ctx) { |
100 while(var_ctx) { |
100 if(var_ctx->vars_unbound && cxMapSize(var_ctx->vars_unbound) > 0) { |
101 if(var_ctx->vars_unbound && cxMapSize(var_ctx->vars_unbound) > 0) { |
101 CxIterator i = cxMapIterator(var_ctx->vars_unbound); |
102 CxMapIterator i = cxMapIterator(var_ctx->vars_unbound); |
102 cx_foreach(CxMapEntry*, entry, i) { |
103 cx_foreach(CxMapEntry*, entry, i) { |
103 printf("attach %s\n", entry->key->data); |
104 printf("attach %s\n", entry->key->data); |
104 UiVar *var = entry->value; |
105 UiVar *var = entry->value; |
105 UiVar *docvar = cxMapGet(doc_ctx->vars, *entry->key); |
106 UiVar *docvar = cxMapGet(doc_ctx->vars, *entry->key); |
106 if(docvar) { |
107 if(docvar) { |
109 cxIteratorFlagRemoval(i); |
110 cxIteratorFlagRemoval(i); |
110 } |
111 } |
111 } |
112 } |
112 } |
113 } |
113 |
114 |
114 var_ctx = ctx->parent; |
115 var_ctx = var_ctx->parent; |
115 } |
116 } |
116 } |
117 } |
117 |
118 |
118 static void uic_context_unbind_vars(UiContext *ctx) { |
119 static void uic_context_unbind_vars(UiContext *ctx) { |
119 CxIterator i = cxMapIterator(ctx->vars); |
120 CxMapIterator mi = cxMapIterator(ctx->vars); |
120 cx_foreach(CxMapEntry*, entry, i) { |
121 cx_foreach(CxMapEntry*, entry, mi) { |
121 UiVar *var = entry->value; |
122 UiVar *var = entry->value; |
122 if(var->from && var->from_ctx) { |
123 if(var->from && var->from_ctx && var->from_ctx != ctx) { |
123 uic_save_var2(var); |
124 uic_save_var2(var); |
124 uic_copy_binding(var, var->from, FALSE); |
125 uic_copy_binding(var, var->from, FALSE); |
125 cxMapPut(var->from_ctx->vars_unbound, *entry->key, var->from); |
126 cxMapPut(var->from_ctx->vars_unbound, *entry->key, var->from); |
126 var->from_ctx = ctx; |
127 var->from_ctx = ctx; |
127 } |
128 } |
128 } |
129 } |
129 |
130 |
130 if(ctx->documents) { |
131 if(ctx->documents) { |
131 i = cxListIterator(ctx->documents); |
132 CxIterator i = cxListIterator(ctx->documents); |
132 cx_foreach(void *, doc, i) { |
133 cx_foreach(void *, doc, i) { |
133 UiContext *subctx = ui_document_context(doc); |
134 UiContext *subctx = ui_document_context(doc); |
134 uic_context_unbind_vars(subctx); |
135 uic_context_unbind_vars(subctx); |
135 } |
136 } |
136 } |
137 } |
146 cxListRemove(ctx->documents, docIndex); |
147 cxListRemove(ctx->documents, docIndex); |
147 ctx->document = cxListAt(ctx->documents, 0); |
148 ctx->document = cxListAt(ctx->documents, 0); |
148 |
149 |
149 UiContext *docctx = ui_document_context(document); |
150 UiContext *docctx = ui_document_context(document); |
150 uic_context_unbind_vars(docctx); // unbind all doc/subdoc vars from the parent |
151 uic_context_unbind_vars(docctx); // unbind all doc/subdoc vars from the parent |
|
152 docctx->parent = NULL; |
151 } |
153 } |
152 |
154 |
153 void uic_context_detach_all(UiContext *ctx) { |
155 void uic_context_detach_all(UiContext *ctx) { |
154 // copy list |
156 // copy list |
155 CxList *ls = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS); |
157 CxList *ls = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS); |
469 cxMempoolFree(ctx->mp); |
471 cxMempoolFree(ctx->mp); |
470 } |
472 } |
471 |
473 |
472 |
474 |
473 void ui_set_group(UiContext *ctx, int group) { |
475 void ui_set_group(UiContext *ctx, int group) { |
474 if(cxListFind(ctx->groups, &group) == -1) { |
476 if(!cxListIndexValid(ctx->groups, cxListFind(ctx->groups, &group))) { |
475 cxListAdd(ctx->groups, &group); |
477 cxListAdd(ctx->groups, &group); |
476 } |
478 } |
477 |
479 |
478 // enable/disable group widgets |
480 // enable/disable group widgets |
479 uic_check_group_widgets(ctx); |
481 uic_check_group_widgets(ctx); |