51 #endif |
51 #endif |
52 |
52 |
53 return ctx; |
53 return ctx; |
54 } |
54 } |
55 |
55 |
56 UiVar* uic_getvar(UiContext *ctx, char *name) { |
56 UiVar* uic_get_var(UiContext *ctx, char *name) { |
57 // check document variables first |
57 // check document variables first |
58 UiVar *var = uic_document_getvar( |
58 UiVar *var = NULL; |
59 uic_getdocument(ctx->toplevel->document), |
59 UiContext *doc_ctx = ui_document_context(ctx->document); |
60 name); |
60 if(doc_ctx) { |
61 // check window vars |
61 var = uic_get_var(doc_ctx, name); |
|
62 } |
|
63 |
|
64 // check variables of this context |
62 if(!var) { |
65 if(!var) { |
63 var = ucx_map_cstr_get(ctx->vars, name); |
66 var = ucx_map_cstr_get(ctx->vars, name); |
64 } |
67 } |
65 |
68 |
66 return var; |
69 return var; |
67 } |
70 } |
68 |
71 |
69 UiVar* uic_connect_var(UiContext *ctx, char *name, int type) { |
72 UiVar* uic_connect_var(UiContext *ctx, char *name, int type) { |
70 // TODO: get current map |
73 // TODO: get current map (Document Container, Tabbed Pane) |
71 UcxMap *from = ctx->vars; |
74 UcxMap *from = ctx->vars; |
72 |
75 |
73 UiVar *var = uic_getvar(ctx, name); |
76 UiVar *var = uic_get_var(ctx, name); |
74 if(var) { |
77 if(var) { |
75 // the value is registered |
78 // the value is registered |
76 |
79 |
77 // a little type check |
80 // a little type check |
78 if(var->type != type) { |
81 if(var->type != type) { |
101 ucx_map_cstr_put(ctx->vars, name, var); |
104 ucx_map_cstr_put(ctx->vars, name, var); |
102 return var; |
105 return var; |
103 } |
106 } |
104 } |
107 } |
105 |
108 |
|
109 void uic_move_var(UiVar *from, UiVar *to, int set) { |
|
110 switch(from->type) { |
|
111 case UI_VAR_INTEGER: { |
|
112 //memcpy(to->value, from->value, sizeof(UiInteger)); |
|
113 UiInteger *f = from->value; |
|
114 UiInteger *t = to->value; |
|
115 t->get = f->get; |
|
116 t->set = f->set; |
|
117 t->obj = f->obj; |
|
118 if(set) { |
|
119 t->set(t, t->value); |
|
120 } else { |
|
121 //t->value = t->get(t); |
|
122 f->value = f->get(f); |
|
123 //t->value = 0; |
|
124 } |
|
125 break; |
|
126 } |
|
127 case UI_VAR_STRING: { |
|
128 // TODO |
|
129 break; |
|
130 } |
|
131 case UI_VAR_TEXT: { |
|
132 UiText *f = from->value; |
|
133 UiText *t = to->value; |
|
134 char *tvalue = t->value; |
|
135 memcpy(t, f, sizeof(UiText)); |
|
136 if(set) { |
|
137 t->set(t, tvalue); |
|
138 } else { |
|
139 f->value = f->get(f); |
|
140 } |
|
141 break; |
|
142 } |
|
143 case UI_VAR_LIST: { |
|
144 UiListVar *f = from->value; |
|
145 UiListVar *t = to->value; |
|
146 UiList *list = t->listptr->list; |
|
147 UiObserver *observers = f->listptr->list->observers; |
|
148 t->listptr = f->listptr; |
|
149 if(set) { |
|
150 t->listptr->list = list; |
|
151 list->observers = observers; |
|
152 ui_notify(observers, list); |
|
153 } |
|
154 break; |
|
155 } |
|
156 } |
|
157 } |
|
158 |
|
159 void uic_reg_var(UiContext *ctx, char *name, int type, size_t vs, void *value) { |
|
160 UiVar *newvar = ucx_mempool_malloc(ctx->mempool, sizeof(UiVar)); |
|
161 newvar->isextern = 1; |
|
162 newvar->type = type; |
|
163 newvar->value = value; |
|
164 newvar->from = NULL; |
|
165 |
|
166 uic_add_var(ctx, name, newvar, type, vs); |
|
167 } |
|
168 |
|
169 void uic_add_var( |
|
170 UiContext *ctx, |
|
171 char *name, |
|
172 UiVar *newvar, |
|
173 int type, |
|
174 size_t vs) |
|
175 { |
|
176 // if a parent context has a variable with this name, we remove it and put |
|
177 // it to this context |
|
178 UiVar *var = ctx->obj ? uic_get_var(ctx->obj->ctx, name) : NULL; |
|
179 |
|
180 if(var && var->from != ctx->vars) { |
|
181 // external variables cannot be moved |
|
182 if(var->isextern) { |
|
183 fprintf( |
|
184 stderr, |
|
185 "UI Error: external variable %s " |
|
186 "cannot be moved to the document.\n", |
|
187 name); |
|
188 return; |
|
189 } |
|
190 |
|
191 // check type |
|
192 if(var->type != type) { |
|
193 fprintf(stderr, "UI Error: var %s has incompatible type.\n", name); |
|
194 return; |
|
195 } |
|
196 |
|
197 // override document var with window context var |
|
198 memcpy(newvar->value, var->value, vs); |
|
199 |
|
200 newvar->from = var->from; |
|
201 |
|
202 // TODO: free var struct |
|
203 ucx_map_cstr_remove(var->from, name); |
|
204 } |
|
205 |
|
206 // finally, add the new variable to the document |
|
207 ucx_map_cstr_put(ctx->vars, name, newvar); |
|
208 } |
|
209 |
106 void* uic_create_value(UcxAllocator *a, int type) { |
210 void* uic_create_value(UcxAllocator *a, int type) { |
107 switch(type) { |
211 switch(type) { |
108 case UI_VAR_INTEGER: { |
212 case UI_VAR_INTEGER: { |
109 UiInteger *i = a->calloc( |
213 UiInteger *i = a->calloc( |
110 a->pool, |
214 a->pool, |