30 #include <stdlib.h> |
30 #include <stdlib.h> |
31 #include <string.h> |
31 #include <string.h> |
32 |
32 |
33 #include "context.h" |
33 #include "context.h" |
34 #include "../ui/window.h" |
34 #include "../ui/window.h" |
|
35 #include "document.h" |
35 |
36 |
36 |
37 |
37 UiContext* uic_context(UiObject *toplevel, UcxMempool *mp) { |
38 UiContext* uic_context(UiObject *toplevel, UcxMempool *mp) { |
38 UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext)); |
39 UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext)); |
39 UcxAllocator *allocator = ucx_mempool_allocator(mp); |
|
40 ctx->mempool = mp; |
40 ctx->mempool = mp; |
41 ctx->allocator = allocator; |
|
42 |
41 |
43 ctx->toplevel = toplevel; |
42 ctx->toplevel = toplevel; |
44 ctx->vars = ucx_map_new_a(allocator, 16); |
43 ctx->vars = ucx_map_new_a(mp->allocator, 16); |
45 |
44 |
46 return ctx; |
45 return ctx; |
47 } |
46 } |
48 |
47 |
49 UiVar* uic_getvar(UiObject *obj, char *name) { |
48 UiVar* uic_getvar(UiContext *ctx, char *name) { |
50 if(!obj) { |
49 // check document variables first |
51 return NULL; |
50 UiVar *var = uic_document_getvar( |
|
51 uic_getdocument(ctx->toplevel->document), |
|
52 name); |
|
53 // check window vars |
|
54 if(!var) { |
|
55 var = ucx_map_cstr_get(ctx->vars, name); |
52 } |
56 } |
53 return ucx_map_cstr_get(obj->ctx->vars, name); |
57 |
|
58 return var; |
54 } |
59 } |
55 |
60 |
56 void uic_rmvar(UiObject *obj, char *name) { |
61 UiVar* uic_connect_var(UiContext *ctx, char *name, int type) { |
57 if(obj) { |
62 // TODO: get current map |
58 UiVar *var = uic_getvar(obj, name); |
63 UcxMap *from = ctx->vars; |
59 if(var) { |
64 |
60 if(var->isextern) { |
65 UiVar *var = uic_getvar(ctx, name); |
61 ucx_mempool_free(obj->ctx->mempool, var->value); |
66 if(var) { |
62 } |
67 // the value is registered |
63 ucx_mempool_free(obj->ctx->mempool, var); |
68 |
|
69 // a little type check |
|
70 if(var->type != type) { |
|
71 fprintf(stderr, "UI Error: var %s has incompatible type.\n", name); |
|
72 return NULL; |
|
73 } else { |
|
74 // register the current document/wdata map |
|
75 // if the document is closed, the var will be moved to this map |
|
76 |
|
77 // TODO: get current map |
|
78 var->from = from; |
|
79 |
|
80 return var; |
64 } |
81 } |
|
82 } else { |
|
83 // create an empty value and add it first to the window variables |
|
84 // it can be moved to the document vars later |
|
85 void *value = uic_create_value(ctx->mempool->allocator, type); |
|
86 if(!value) { |
|
87 fprintf(stderr, "UI Error: Cannot create empty value.\n"); |
|
88 return NULL; |
|
89 } |
|
90 UiVar *var = ucx_mempool_malloc(ctx->mempool, sizeof(UiVar)); |
|
91 var->value = value; |
|
92 var->type = type; |
|
93 var->isextern = 0; |
|
94 var->from = from; |
|
95 ucx_map_cstr_put(ctx->vars, name, var); |
|
96 return var; |
65 } |
97 } |
66 } |
98 } |
67 |
99 |
68 void ui_window_addint(UiObject *obj, char *name) { |
100 void* uic_create_value(UcxAllocator *a, int type) { |
69 if(uic_getvar(obj, name)) { |
101 switch(type) { |
70 // var already exists |
102 case UI_VAR_INTEGER: { |
71 return; |
103 UiInteger *i = a->calloc( |
|
104 a->pool, |
|
105 1, |
|
106 sizeof(UiInteger)); |
|
107 return i; |
|
108 } |
|
109 case UI_VAR_STRING: { |
|
110 UiString *s = a->calloc( |
|
111 a->pool, |
|
112 1, |
|
113 sizeof(UiInteger)); |
|
114 return s; |
|
115 } |
72 } |
116 } |
73 UiInteger *i = ucx_mempool_calloc(obj->ctx->mempool, 1, sizeof(UiInteger)); |
117 return NULL; |
74 UiVar *var = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiVar)); |
|
75 var->value = i; |
|
76 var->type = 1; |
|
77 var->isextern = 0; |
|
78 ucx_map_cstr_put(obj->ctx->vars, name, var); |
|
79 } |
118 } |
80 |
119 |
81 void ui_window_regint(UiObject *obj, char *name, UiInteger *i) { |
120 |
82 if(uic_getvar(obj, name)) { |
121 |
83 // var already exists |
122 // public API |
84 return; |
123 |
|
124 int ui_getint(UiObject *obj, char *name) { |
|
125 UiVar *var = uic_getvar(obj->ctx, name); |
|
126 if(var) { |
|
127 if(var->type == UI_VAR_INTEGER) { |
|
128 UiInteger *i = var->value; |
|
129 if(i->get) { |
|
130 return i->get(i); |
|
131 } else { |
|
132 fprintf( |
|
133 stderr, |
|
134 "UI Error: variable %s is not connected.\n", |
|
135 name); |
|
136 } |
|
137 } else { |
|
138 fprintf( |
|
139 stderr, |
|
140 "UI Error: requested variable %s is not an integer.\n", |
|
141 name); |
|
142 } |
|
143 } else { |
|
144 fprintf(stderr, "UI Error: unkown variable %s.\n", name); |
85 } |
145 } |
86 UiVar *var = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiVar)); |
146 return 0; |
87 var->value = i; |
|
88 var->type = 1; |
|
89 var->isextern = 1; |
|
90 ucx_map_cstr_put(obj->ctx->vars, name, var); |
|
91 } |
147 } |
92 |
148 |
93 void ui_window_setint(UiObject *obj, char *name, int val) { |
|
94 UiVar *var = uic_getvar(obj, name); |
|
95 if(var && var->type == 1) { |
|
96 UiInteger *i = var->value; |
|
97 i->set(i, val); |
|
98 } else { |
|
99 // TODO: error message |
|
100 } |
|
101 } |
|
102 |
|
103 int ui_window_getint(UiObject *obj, char *name) { |
|
104 UiVar *var = uic_getvar(obj, name); |
|
105 if(var && var->type == 1) { |
|
106 UiInteger *i = var->value; |
|
107 return i->get(i); |
|
108 } else { |
|
109 return 0; // TODO: error message |
|
110 } |
|
111 } |
|