ui/common/context.c

changeset 128
c284c15509a8
parent 121
1cc365c34125
child 131
774b741984a2
equal deleted inserted replaced
127:ce342364fad5 128:c284c15509a8
54 #endif 54 #endif
55 55
56 return ctx; 56 return ctx;
57 } 57 }
58 58
59 UiContext* uic_root_context(UiContext *ctx) {
60 return ctx->parent ? uic_root_context(ctx->parent) : ctx;
61 }
62
59 void uic_context_set_document(UiContext *ctx, void *document) { 63 void uic_context_set_document(UiContext *ctx, void *document) {
60 UiContext *docctx = ui_document_context(document); 64 UiContext *docctx = ui_document_context(document);
61 if(!docctx) { 65 if(!docctx) {
62 return; 66 return;
63 } 67 }
64 docctx->obj = ctx->obj; 68 docctx->obj = ctx->obj;
69 docctx->parent = ctx;
65 70
66 if(ctx->document) { 71 if(ctx->document) {
67 uic_context_detach_document(ctx, ctx->document); 72 uic_context_detach_document(ctx, ctx->document);
68 } 73 }
69 //obj->document = document; 74 //obj->document = document;
70 ctx->document = document; 75 ctx->document = document;
71 76
72 UcxMapIterator i = ucx_map_iterator(docctx->vars); 77 UcxMapIterator i = ucx_map_iterator(docctx->vars);
73 UiVar *var; 78 UiVar *var;
74 UCX_MAP_FOREACH(key, var, i) { 79 UCX_MAP_FOREACH(key, var, i) {
75 UiVar *v = ucx_map_get(ctx->vars, key); 80 UiVar *v = ucx_map_get(uic_root_context(ctx)->vars, key);
76 if(v) { 81 if(v) {
77 if(v->isextern) { 82 if(v->isextern) {
78 fprintf( 83 fprintf(
79 stderr, 84 stderr,
80 "UI Error: external variable cannot be moved\n"); 85 "UI Error: external variable cannot be moved\n");
85 fprintf(stderr, "UI Error: var has incompatible type.\n"); 90 fprintf(stderr, "UI Error: var has incompatible type.\n");
86 return; 91 return;
87 } 92 }
88 93
89 // copy value 94 // copy value
90 uic_move_var(v, var, 1); 95 uic_move_var(v, var, TRUE);
91 var->from = v->from; 96 var->from = v->from;
92 97
93 // TODO: free var struct 98 // TODO: free var struct
94 ucx_map_remove(ctx->vars, key); 99 ucx_map_remove(ctx->vars, key);
95 } 100 }
126 131
127 //ucx_map_remove(doc->vars, key); // TODO: dont remove! 132 //ucx_map_remove(doc->vars, key); // TODO: dont remove!
128 } 133 }
129 } 134 }
130 135
131 if(docctx->obj) { 136 if(docctx->parent) {
132 docctx->obj->ctx->document = NULL; 137 docctx->parent->document = NULL;
133 } 138 }
134 139
135 docctx->obj = NULL; 140 docctx->obj = NULL;
141 docctx->parent = NULL;
136 } 142 }
137 143
138 UiVar* uic_get_var(UiContext *ctx, char *name) { 144 UiVar* uic_get_var(UiContext *ctx, char *name) {
139 // check document variables first 145 // check document variables first
140 UiVar *var = NULL; 146 UiVar *var = NULL;
186 ucx_map_cstr_put(ctx->vars, name, var); 192 ucx_map_cstr_put(ctx->vars, name, var);
187 return var; 193 return var;
188 } 194 }
189 } 195 }
190 196
191 void uic_move_var(UiVar *from, UiVar *to, int set) { 197 void uic_move_var(UiVar *from, UiVar *to, UiBool set) {
192 switch(from->type) { 198 switch(from->type) {
193 case UI_VAR_INTEGER: { 199 case UI_VAR_INTEGER: {
194 //memcpy(to->value, from->value, sizeof(UiInteger)); 200 //memcpy(to->value, from->value, sizeof(UiInteger));
195 UiInteger *f = from->value; 201 UiInteger *f = from->value;
196 UiInteger *t = to->value; 202 UiInteger *t = to->value;
211 break; 217 break;
212 } 218 }
213 case UI_VAR_TEXT: { 219 case UI_VAR_TEXT: {
214 UiText *f = from->value; 220 UiText *f = from->value;
215 UiText *t = to->value; 221 UiText *t = to->value;
216 char *tvalue = t->value; 222 char *tvalue = t->value ? t->value : "";
217 int tpos = t->pos; 223 int tpos = t->pos;
218 memcpy(t, f, sizeof(UiText)); 224 memcpy(t, f, sizeof(UiText));
219 if(set && tvalue) { 225 if(set) {
220 t->set(t, tvalue); 226 t->set(t, tvalue);
221 t->setposition(t, tpos); 227 t->setposition(t, tpos);
222 } else { 228 } else {
223 f->value = f->get(f); 229 f->value = f->get(f);
224 f->pos = f->position(f); 230 f->pos = f->position(f);

mercurial