ui/common/context.c

changeset 0
2483f517c562
child 3
f154867f54dc
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <inttypes.h>
33 #include <stdarg.h>
34
35 #include <cx/array_list.h>
36 #include <cx/compare.h>
37 #include <cx/mempool.h>
38
39 #include "context.h"
40 #include "../ui/window.h"
41 #include "document.h"
42 #include "types.h"
43
44 static UiContext* global_context;
45
46 void uic_init_global_context(void) {
47 CxMempool *mp = cxBasicMempoolCreate(32);
48 global_context = uic_context(NULL, mp);
49 }
50
51 UiContext* ui_global_context(void) {
52 return global_context;
53 }
54
55 UiContext* uic_context(UiObject *toplevel, CxMempool *mp) {
56 UiContext *ctx = cxMalloc(mp->allocator, sizeof(UiContext));
57 memset(ctx, 0, sizeof(UiContext));
58 ctx->mp = mp;
59 ctx->allocator = mp->allocator;
60 ctx->obj = toplevel;
61 ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16);
62
63 ctx->documents = cxLinkedListCreate(mp->allocator, cx_cmp_intptr, CX_STORE_POINTERS);
64 ctx->group_widgets = cxLinkedListCreate(mp->allocator, NULL, sizeof(UiGroupWidget));
65 ctx->groups = cxArrayListCreate(mp->allocator, cx_cmp_int, sizeof(int), 32);
66
67 ctx->attach_document = uic_context_attach_document;
68 ctx->detach_document2 = uic_context_detach_document2;
69
70 #ifdef UI_GTK
71 if(toplevel && toplevel->widget) {
72 ctx->accel_group = gtk_accel_group_new();
73 gtk_window_add_accel_group(GTK_WINDOW(toplevel->widget), ctx->accel_group);
74 }
75 #endif
76
77 return ctx;
78 }
79
80 UiContext* uic_root_context(UiContext *ctx) {
81 return ctx->parent ? uic_root_context(ctx->parent) : ctx;
82 }
83
84 void uic_context_attach_document(UiContext *ctx, void *document) {
85 cxListAdd(ctx->documents, document);
86 ctx->document = document;
87
88 UiContext *doc_ctx = ui_document_context(document);
89
90 // check if any parent context has an unbound variable with the same name
91 // as any document variable
92 UiContext *var_ctx = ctx;
93 while(var_ctx) {
94 if(var_ctx->vars_unbound && var_ctx->vars_unbound->size > 0) {
95 CxIterator i = cxMapIterator(var_ctx->vars_unbound);
96 cx_foreach(CxMapEntry*, entry, i) {
97 UiVar *var = entry->value;
98 UiVar *docvar = cxMapGet(doc_ctx->vars, *entry->key);
99 if(docvar) {
100 // bind var to document var
101 uic_copy_binding(var, docvar, TRUE);
102 cxIteratorFlagRemoval(i);
103 }
104 }
105 }
106
107 var_ctx = ctx->parent;
108 }
109 }
110
111 static void uic_context_unbind_vars(UiContext *ctx) {
112 CxIterator i = cxMapIterator(ctx->vars);
113 cx_foreach(CxMapEntry*, entry, i) {
114 UiVar *var = entry->value;
115 if(var->from && var->from_ctx) {
116 uic_save_var2(var);
117 uic_copy_binding(var, var->from, FALSE);
118 cxMapPut(var->from_ctx->vars_unbound, *entry->key, var->from);
119 var->from_ctx = ctx;
120 }
121 }
122
123 if(ctx->documents) {
124 i = cxListIterator(ctx->documents);
125 cx_foreach(void *, doc, i) {
126 UiContext *subctx = ui_document_context(doc);
127 uic_context_unbind_vars(subctx);
128 }
129 }
130 }
131
132 void uic_context_detach_document2(UiContext *ctx, void *document) {
133 // find the document in the documents list
134 ssize_t docIndex = cxListFind(ctx->documents, document);
135 if(docIndex < 0) {
136 return;
137 }
138
139 cxListRemove(ctx->documents, docIndex);
140 ctx->document = cxListAt(ctx->documents, 0);
141
142 UiContext *docctx = ui_document_context(document);
143 uic_context_unbind_vars(docctx); // unbind all doc/subdoc vars from the parent
144 }
145
146 void uic_context_detach_all(UiContext *ctx) {
147 // copy list
148 CxList *ls = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS);
149 CxIterator i = cxListIterator(ctx->documents);
150 cx_foreach(void *, doc, i) {
151 cxListAdd(ls, doc);
152 }
153
154 // detach documents
155 i = cxListIterator(ls);
156 cx_foreach(void *, doc, i) {
157 ctx->detach_document2(ctx, doc);
158 }
159
160 cxListDestroy(ls);
161 }
162
163 static UiVar* ctx_getvar(UiContext *ctx, CxHashKey key) {
164 UiVar *var = cxMapGet(ctx->vars, key);
165 if(!var) {
166 CxIterator i = cxListIterator(ctx->documents);
167 cx_foreach(void *, doc, i) {
168 UiContext *subctx = ui_document_context(doc);
169 var = ctx_getvar(subctx, key);
170 if(var) {
171 break;
172 }
173 }
174 }
175 return var;
176 }
177
178 UiVar* uic_get_var(UiContext *ctx, const char *name) {
179 CxHashKey key = cx_hash_key(name, strlen(name));
180 return ctx_getvar(ctx, key);
181 }
182
183 UiVar* uic_create_var(UiContext *ctx, const char *name, UiVarType type) {
184 UiVar *var = uic_get_var(ctx, name);
185 if(var) {
186 if(var->type == type) {
187 return var;
188 } else {
189 fprintf(stderr, "UiError: var '%s' already bound with different type\n", name);
190 }
191 }
192
193 var = ui_malloc(ctx, sizeof(UiVar));
194 var->type = type;
195 var->value = uic_create_value(ctx, type);
196 var->from = NULL;
197 var->from_ctx = ctx;
198
199 cxMempoolRegister(ctx->mp, var, (cx_destructor_func)uic_unbind_var);
200
201 if(!ctx->vars_unbound) {
202 ctx->vars_unbound = cxHashMapCreate(ctx->allocator, CX_STORE_POINTERS, 16);
203 }
204 cxMapPut(ctx->vars_unbound, name, var);
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;
215 return var;
216 }
217
218 void* uic_create_value(UiContext *ctx, UiVarType type) {
219 void *val = NULL;
220 switch(type) {
221 case UI_VAR_SPECIAL: break;
222 case UI_VAR_INTEGER: {
223 val = ui_int_new(ctx, NULL);
224 break;
225 }
226 case UI_VAR_DOUBLE: {
227 val = ui_double_new(ctx, NULL);
228 break;
229 }
230 case UI_VAR_STRING: {
231 val = ui_string_new(ctx, NULL);
232 break;
233 }
234 case UI_VAR_TEXT: {
235 val = ui_text_new(ctx, NULL);
236 break;
237 }
238 case UI_VAR_LIST: {
239 val = ui_list_new(ctx, NULL);
240 break;
241 }
242 case UI_VAR_RANGE: {
243 val = ui_range_new(ctx, NULL);
244 break;
245 }
246 }
247 return val;
248 }
249
250
251 UiVar* uic_widget_var(UiContext* toplevel, UiContext* current, void* value, const char* varname, UiVarType type) {
252 if (value) {
253 return uic_create_value_var(current, value);
254 }
255 if (varname) {
256 return uic_create_var(toplevel, varname, type);
257 }
258 return NULL;
259 }
260
261
262 void uic_copy_binding(UiVar *from, UiVar *to, UiBool copytodoc) {
263 // check type
264 if(from->type != to->type) {
265 fprintf(stderr, "UI Error: var has incompatible type.\n");
266 return;
267 }
268
269 void *fromvalue = from->value;
270 // update var
271 if(copytodoc) {
272 to->from = from;
273 to->from_ctx = from->from_ctx;
274 }
275
276 // copy binding
277 // we don't copy the observer, because the from var has never one
278 switch(from->type) {
279 default: fprintf(stderr, "uic_copy_binding: wtf!\n"); break;
280 case UI_VAR_SPECIAL: break;
281 case UI_VAR_INTEGER: {
282 UiInteger *f = fromvalue;
283 UiInteger *t = to->value;
284 if(!f->obj) break;
285 uic_int_copy(f, t);
286 t->set(t, t->value);
287 break;
288 }
289 case UI_VAR_DOUBLE: {
290 UiDouble *f = fromvalue;
291 UiDouble *t = to->value;
292 if(!f->obj) break;
293 uic_double_copy(f, t);
294 t->set(t, t->value);
295 break;
296 }
297 case UI_VAR_STRING: {
298 UiString *f = fromvalue;
299 UiString *t = to->value;
300 if(!f->obj) break;
301 uic_string_copy(f, t);
302 char *tvalue = t->value.ptr ? t->value.ptr : "";
303 t->set(t, tvalue);
304 break;
305 }
306 case UI_VAR_TEXT: {
307 UiText *f = fromvalue;
308 UiText *t = to->value;
309 if(!f->obj) break;
310 uic_text_copy(f, t);
311 char *tvalue = t->value.ptr ? t->value.ptr : "";
312 t->set(t, tvalue);
313 t->setposition(t, t->pos);
314 break;
315 }
316 case UI_VAR_LIST: {
317 UiList *f = fromvalue;
318 UiList *t = to->value;
319 if(!f->obj) break;
320 uic_list_copy(f, t);
321 t->update(t, -1);
322 break;
323 }
324 case UI_VAR_RANGE: {
325 UiRange *f = fromvalue;
326 UiRange *t = to->value;
327 if(!f->obj) break;
328 uic_range_copy(f, t);
329 t->setextent(t, t->extent);
330 t->setrange(t, t->min, t->max);
331 t->set(t, t->value);
332 break;
333 }
334 }
335 }
336
337 void uic_save_var2(UiVar *var) {
338 switch(var->type) {
339 case UI_VAR_SPECIAL: break;
340 case UI_VAR_INTEGER: uic_int_save(var->value); break;
341 case UI_VAR_DOUBLE: uic_double_save(var->value); break;
342 case UI_VAR_STRING: uic_string_save(var->value); break;
343 case UI_VAR_TEXT: uic_text_save(var->value); break;
344 case UI_VAR_LIST: break;
345 case UI_VAR_RANGE: uic_range_save(var->value); break;
346 }
347 }
348
349 void uic_unbind_var(UiVar *var) {
350 switch(var->type) {
351 case UI_VAR_SPECIAL: break;
352 case UI_VAR_INTEGER: uic_int_unbind(var->value); break;
353 case UI_VAR_DOUBLE: uic_double_unbind(var->value); break;
354 case UI_VAR_STRING: uic_string_unbind(var->value); break;
355 case UI_VAR_TEXT: uic_text_unbind(var->value); break;
356 case UI_VAR_LIST: uic_list_unbind(var->value); break;
357 case UI_VAR_RANGE: uic_range_unbind(var->value); break;
358 }
359 }
360
361 void uic_reg_var(UiContext *ctx, char *name, UiVarType type, void *value) {
362 // TODO: do we need/want this? Why adding vars to a context after
363 // widgets reference these? Workarounds:
364 // 1. add vars to ctx before creating ui
365 // 2. create ui, create new document with vars, attach doc
366 // also it would be possible to create a function, that scans unbound vars
367 // and connects them to available vars
368 /*
369 UiContext *rootctx = uic_root_context(ctx);
370 UiVar *b = NULL;
371 if(rootctx->bound) {
372 // some widgets are already bound to some vars
373 b = ucx_map_cstr_get(rootctx->bound, name);
374 if(b) {
375 // a widget is bound to a var with this name
376 // if ctx is the root context we can remove the var from bound
377 // because set_doc or detach can't fuck things up
378 if(ctx == rootctx) {
379 ucx_map_cstr_remove(ctx->bound, name);
380 // TODO: free stuff
381 }
382 }
383 }
384 */
385
386 // create new var and add it to doc's vars
387 UiVar *var = ui_malloc(ctx, sizeof(UiVar));
388 var->type = type;
389 var->value = value;
390 var->from = NULL;
391 var->from_ctx = ctx;
392 size_t oldcount = ctx->vars->size;
393 cxMapPut(ctx->vars, name, var);
394 if(ctx->vars->size != oldcount + 1) {
395 fprintf(stderr, "UiError: var '%s' already exists\n", name);
396 }
397
398 // TODO: remove?
399 // a widget is already bound to a var with this name
400 // copy the binding (like uic_context_set_document)
401 /*
402 if(b) {
403 uic_copy_binding(b, var, TRUE);
404 }
405 */
406 }
407
408 void uic_remove_bound_var(UiContext *ctx, UiVar *var) {
409 // TODO
410 }
411
412
413 // public API
414
415 void ui_attach_document(UiContext *ctx, void *document) {
416 uic_context_attach_document(ctx, document);
417 }
418
419 void ui_detach_document2(UiContext *ctx, void *document) {
420 uic_context_detach_document2(ctx, document);
421 }
422
423 void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata) {
424 ctx->close_callback = fnc;
425 ctx->close_data = udata;
426 }
427
428
429 void ui_set_group(UiContext *ctx, int group) {
430 if(cxListFind(ctx->groups, &group) == -1) {
431 cxListAdd(ctx->groups, &group);
432 }
433
434 // enable/disable group widgets
435 uic_check_group_widgets(ctx);
436 }
437
438 void ui_unset_group(UiContext *ctx, int group) {
439 int i = cxListFind(ctx->groups, &group);
440 if(i != -1) {
441 cxListRemove(ctx->groups, i);
442 }
443
444 // enable/disable group widgets
445 uic_check_group_widgets(ctx);
446 }
447
448 int* ui_active_groups(UiContext *ctx, int *ngroups) {
449 *ngroups = ctx->groups->size;
450 return cxListAt(ctx->groups, 0);
451 }
452
453 void uic_check_group_widgets(UiContext *ctx) {
454 int ngroups = 0;
455 int *groups = ui_active_groups(ctx, &ngroups);
456
457 CxIterator i = cxListIterator(ctx->group_widgets);
458 cx_foreach(UiGroupWidget *, gw, i) {
459 char *check = calloc(1, gw->numgroups);
460
461 for(int i=0;i<ngroups;i++) {
462 for(int k=0;k<gw->numgroups;k++) {
463 if(groups[i] == gw->groups[k]) {
464 check[k] = 1;
465 }
466 }
467 }
468
469 int enable = 1;
470 for(int i=0;i<gw->numgroups;i++) {
471 if(check[i] == 0) {
472 enable = 0;
473 break;
474 }
475 }
476 free(check);
477 gw->enable(gw->widget, enable);
478 }
479 }
480
481 void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) {
482 // get groups
483 CxList *groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
484 va_list ap;
485 va_start(ap, enable);
486 int group;
487 while((group = va_arg(ap, int)) != -1) {
488 cxListAdd(groups, &group);
489 }
490 va_end(ap);
491
492 uic_add_group_widget(ctx, widget, enable, groups);
493
494 cxListDestroy(groups);
495 }
496
497 void uic_add_group_widget(UiContext *ctx, void *widget, ui_enablefunc enable, CxList *groups) {
498 const CxAllocator *a = ctx->allocator;
499 UiGroupWidget gw;
500
501 gw.widget = widget;
502 gw.enable = enable;
503 gw.numgroups = groups->size;
504 gw.groups = cxCalloc(a, gw.numgroups, sizeof(int));
505
506 // copy groups
507 int *intgroups = cxListAt(groups, 0);
508 if(intgroups) {
509 memcpy(gw.groups, intgroups, gw.numgroups * sizeof(int));
510 }
511
512 cxListAdd(ctx->group_widgets, &gw);
513 }
514
515 void* ui_malloc(UiContext *ctx, size_t size) {
516 return ctx ? cxMalloc(ctx->allocator, size) : NULL;
517 }
518
519 void* ui_calloc(UiContext *ctx, size_t nelem, size_t elsize) {
520 return ctx ? cxCalloc(ctx->allocator, nelem, elsize) : NULL;
521 }
522
523 void ui_free(UiContext *ctx, void *ptr) {
524 if(ctx) {
525 cxFree(ctx->allocator, ptr);
526 }
527 }
528
529 void* ui_realloc(UiContext *ctx, void *ptr, size_t size) {
530 return ctx ? cxRealloc(ctx->allocator, ptr, size) : NULL;
531 }
532

mercurial