| 44 |
44 |
| 45 void client_init(UiMessageHandler *handler) { |
45 void client_init(UiMessageHandler *handler) { |
| 46 io = handler; |
46 io = handler; |
| 47 |
47 |
| 48 msg_types = cxHashMapCreateSimple(CX_STORE_POINTERS); |
48 msg_types = cxHashMapCreateSimple(CX_STORE_POINTERS); |
| 49 cxMapPut(msg_types, "simple_window", msg_simple_window); |
49 cxMapPut(msg_types, "window", msg_window); |
| |
50 cxMapPut(msg_types, "sidebar_window", msg_window); |
| |
51 cxMapPut(msg_types, "splitview_window", msg_window); |
| |
52 cxMapPut(msg_types, "simple_window", msg_window); |
| 50 cxMapPut(msg_types, "show", msg_show); |
53 cxMapPut(msg_types, "show", msg_show); |
| 51 |
54 |
| 52 cxMapPut(msg_types, "vbox", msg_vbox); |
55 cxMapPut(msg_types, "vbox", msg_vbox); |
| 53 cxMapPut(msg_types, "hbox", msg_vbox); |
56 cxMapPut(msg_types, "hbox", msg_vbox); |
| 54 cxMapPut(msg_types, "grid", msg_vbox); |
57 cxMapPut(msg_types, "grid", msg_vbox); |
| 63 CxJsonValue *value = cxJsonObjGet(obj, name); |
66 CxJsonValue *value = cxJsonObjGet(obj, name); |
| 64 if(value->type == CX_JSON_STRING) { |
67 if(value->type == CX_JSON_STRING) { |
| 65 return value->value.string; |
68 return value->value.string; |
| 66 } else { |
69 } else { |
| 67 return (cxmutstr){ NULL, 0 }; |
70 return (cxmutstr){ NULL, 0 }; |
| |
71 } |
| |
72 } |
| |
73 |
| |
74 static UiBool jsonobj_getbool(const CxJsonValue *obj, const char *name, int *error) { |
| |
75 CxJsonValue *value = cxJsonObjGet(obj, name); |
| |
76 if(value->type == CX_JSON_LITERAL) { |
| |
77 if(*error) { |
| |
78 *error = 0; |
| |
79 } |
| |
80 return value->value.literal == CX_JSON_TRUE ? 1 : 0; |
| |
81 } else { |
| |
82 if(error) { |
| |
83 *error = 1; |
| |
84 } |
| |
85 return FALSE; |
| 68 } |
86 } |
| 69 } |
87 } |
| 70 |
88 |
| 71 /* |
89 /* |
| 72 * UI thread callback |
90 * UI thread callback |
| 112 json_msg_handler handler = cxMapGet(msg_types, type->value.string); |
130 json_msg_handler handler = cxMapGet(msg_types, type->value.string); |
| 113 if(!handler) { |
131 if(!handler) { |
| 114 return 1; |
132 return 1; |
| 115 } |
133 } |
| 116 |
134 |
| 117 return handler(obj, value); |
135 return handler(obj, value, type->value.string); |
| 118 } |
136 } |
| 119 |
137 |
| 120 int client_handle_children(UiObject *parent, const CxJsonValue *value) { |
138 int client_handle_children(UiObject *parent, const CxJsonValue *value) { |
| 121 CxJsonValue *children = cxJsonObjGet(value, "children"); |
139 CxJsonValue *children = cxJsonObjGet(value, "children"); |
| 122 if(children && children->type == CX_JSON_ARRAY) { |
140 if(children && children->type == CX_JSON_ARRAY) { |
| 172 return NULL; |
190 return NULL; |
| 173 } |
191 } |
| 174 return client_get_mapped_obj(obj_id->value.string); |
192 return client_get_mapped_obj(obj_id->value.string); |
| 175 } |
193 } |
| 176 |
194 |
| 177 int msg_simple_window(UiObject *parent, const CxJsonValue *value) { |
195 int msg_window(UiObject *parent, const CxJsonValue *value, cxmutstr type) { |
| 178 cxmutstr obj_id = jsonobj_getstring(value, "obj_id"); |
196 cxmutstr obj_id = jsonobj_getstring(value, "obj"); |
| 179 cxmutstr id = jsonobj_getstring(value, "id"); |
197 cxmutstr id = jsonobj_getstring(value, "id"); |
| 180 cxmutstr title = jsonobj_getstring(value, "title"); |
198 cxmutstr title = jsonobj_getstring(value, "title"); |
| 181 |
199 |
| 182 if(!obj_id.ptr) { |
200 if(!obj_id.ptr) { |
| 183 return 1; |
201 return 1; |
| 184 } |
202 } |
| 185 if(!id.ptr) { |
203 if(!id.ptr) { |
| 186 return 1; |
204 return 1; |
| 187 } |
205 } |
| 188 |
206 |
| 189 UiObject *obj = ui_simple_window(title.ptr, NULL); |
207 UiObject *obj; |
| |
208 if(!cx_strcmp(type, "window")) { |
| |
209 obj = ui_window(title.ptr, NULL); |
| |
210 } else if(!cx_strcmp(type, "sidebar_window")) { |
| |
211 obj = ui_sidebar_window(title.ptr, NULL); |
| |
212 } else if(!cx_strcmp(type, "splitview_window")) { |
| |
213 int err; |
| |
214 bool sidebar = jsonobj_getbool(value, "sidebar", &err); |
| |
215 if(err) { |
| |
216 return 1; |
| |
217 } |
| |
218 obj = ui_splitview_window(title.ptr, sidebar); |
| |
219 } else if(!cx_strcmp(type, "simple_window")) { |
| |
220 obj = ui_simple_window(title.ptr, NULL); |
| |
221 } |
| |
222 |
| 190 client_add_obj_mapping(obj, obj_id); |
223 client_add_obj_mapping(obj, obj_id); |
| 191 |
224 |
| 192 if(obj->widget) { |
225 if(obj->widget) { |
| 193 client_reg_widget(obj, id, obj->widget); |
226 client_reg_widget(obj, id, obj->widget); |
| 194 } |
227 } |
| 195 |
228 |
| 196 return client_handle_children(obj, value); |
229 return client_handle_children(obj, value); |
| 197 } |
230 } |
| 198 |
231 |
| 199 int msg_show(UiObject *parent, const CxJsonValue *value) { |
232 int msg_show(UiObject *parent, const CxJsonValue *value, cxmutstr type) { |
| 200 UiObject *obj = client_get_mapped_obj(jsonobj_getstring(value, "obj")); |
233 UiObject *obj = client_get_mapped_obj(jsonobj_getstring(value, "obj")); |
| 201 if(!obj) { |
234 if(!obj) { |
| 202 return 1; |
235 return 1; |
| 203 } |
236 } |
| 204 ui_show(obj); |
237 ui_show(obj); |
| 224 client_reg_widget(obj, id, w); |
257 client_reg_widget(obj, id, w); |
| 225 |
258 |
| 226 return 0; |
259 return 0; |
| 227 } |
260 } |
| 228 |
261 |
| 229 int msg_vbox(UiObject *parent, const CxJsonValue *value) { |
262 int msg_vbox(UiObject *parent, const CxJsonValue *value, cxmutstr type) { |
| 230 return msg_container(parent, value, ui_vbox_create); |
263 return msg_container(parent, value, ui_vbox_create); |
| 231 } |
264 } |
| 232 |
265 |
| 233 int msg_hbox(UiObject *parent, const CxJsonValue *value) { |
266 int msg_hbox(UiObject *parent, const CxJsonValue *value, cxmutstr type) { |
| 234 return msg_container(parent, value, ui_hbox_create); |
267 return msg_container(parent, value, ui_hbox_create); |
| 235 } |
268 } |
| 236 |
269 |
| 237 int msg_grid(UiObject *parent, const CxJsonValue *value) { |
270 int msg_grid(UiObject *parent, const CxJsonValue *value, cxmutstr type) { |
| 238 return msg_container(parent, value, ui_grid_create); |
271 return msg_container(parent, value, ui_grid_create); |
| 239 } |
272 } |
| 240 |
273 |
| 241 int msg_end(UiObject *parent, const CxJsonValue *value) { |
274 int msg_end(UiObject *parent, const CxJsonValue *value, cxmutstr type) { |
| 242 UiObject *obj = get_msg_obj(parent, value); |
275 UiObject *obj = get_msg_obj(parent, value); |
| 243 if(!obj) { |
276 if(!obj) { |
| 244 return 1; |
277 return 1; |
| 245 } |
278 } |
| 246 ui_end_new(obj); |
279 ui_end_new(obj); |
| 247 return 0; |
280 return 0; |
| 248 } |
281 } |
| 249 |
282 |
| 250 int msg_button(UiObject *parent, const CxJsonValue *value) { |
283 int msg_button(UiObject *parent, const CxJsonValue *value, cxmutstr type) { |
| 251 CxJsonValue *args_value = cxJsonObjGet(value, "args"); |
284 CxJsonValue *args_value = cxJsonObjGet(value, "args"); |
| 252 cxmutstr id = jsonobj_getstring(value, "id"); |
285 cxmutstr id = jsonobj_getstring(value, "id"); |
| 253 if(!id.ptr) { |
286 if(!id.ptr) { |
| 254 return 1; |
287 return 1; |
| 255 } |
288 } |
| 264 client_reg_widget(obj, id, w); |
297 client_reg_widget(obj, id, w); |
| 265 |
298 |
| 266 return 0; |
299 return 0; |
| 267 } |
300 } |
| 268 |
301 |
| 269 int msg_togglebutton(UiObject *parent, const CxJsonValue *value) { |
302 int msg_togglebutton(UiObject *parent, const CxJsonValue *value, cxmutstr type) { |
| 270 CxJsonValue *args_value = cxJsonObjGet(value, "args"); |
303 CxJsonValue *args_value = cxJsonObjGet(value, "args"); |
| 271 cxmutstr id = jsonobj_getstring(value, "id"); |
304 cxmutstr id = jsonobj_getstring(value, "id"); |
| 272 if(!id.ptr) { |
305 if(!id.ptr) { |
| 273 return 1; |
306 return 1; |
| 274 } |
307 } |