ui/common/types.c

changeset 108
77254bd6dccb
parent 103
6606616eca9f
child 109
c3dfcb8f0be7
equal deleted inserted replaced
107:b34bd1557c6c 108:77254bd6dccb
34 #include <cx/list.h> 34 #include <cx/list.h>
35 #include <cx/array_list.h> 35 #include <cx/array_list.h>
36 #include "../ui/tree.h" 36 #include "../ui/tree.h"
37 #include "types.h" 37 #include "types.h"
38 #include "context.h" 38 #include "context.h"
39 39 #include "../ui/image.h"
40 40
41 static ui_list_init_func default_list_init;
42 static void *default_list_init_userdata;
41 43
42 UiObserver* ui_observer_new(ui_callback f, void *data) { 44 UiObserver* ui_observer_new(ui_callback f, void *data) {
43 UiObserver *observer = malloc(sizeof(UiObserver)); 45 UiObserver *observer = malloc(sizeof(UiObserver));
44 observer->callback = f; 46 observer->callback = f;
45 observer->data = data; 47 observer->data = data;
73 UiEvent evt; 75 UiEvent evt;
74 evt.obj = NULL; 76 evt.obj = NULL;
75 evt.window = NULL; 77 evt.window = NULL;
76 evt.document = NULL; 78 evt.document = NULL;
77 evt.eventdata = data; 79 evt.eventdata = data;
80 evt.eventdatatype = UI_EVENT_DATA_POINTER;
78 evt.intval = 0; 81 evt.intval = 0;
79 82
80 while(observer) { 83 while(observer) {
81 if(observer != exc) { 84 if(observer != exc) {
82 observer->callback(&evt, observer->data); 85 observer->callback(&evt, observer->data);
92 } 95 }
93 } 96 }
94 97
95 /* --------------------------- UiList --------------------------- */ 98 /* --------------------------- UiList --------------------------- */
96 99
97 UiList* ui_list_new(UiContext *ctx, char *name) { 100 void uic_ucx_list_init(UiContext *ctx, UiList *list, void *unused) {
98 UiList *list = malloc(sizeof(UiList)); 101 list->data = cxArrayListCreate(ctx->mp->allocator, NULL, CX_STORE_POINTERS, 32);
99 list->first = ui_list_first; 102 list->first = ui_list_first;
100 list->next = ui_list_next; 103 list->next = ui_list_next;
101 list->get = ui_list_get; 104 list->get = ui_list_get;
102 list->count = ui_list_count; 105 list->count = ui_list_count;
103 list->observers = NULL; 106 }
104 107
105 list->data = cxArrayListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS, 32); 108 UiList* ui_list_new(UiContext *ctx, const char *name) {
106 list->iter = NULL; 109 return ui_list_new2(ctx, name, default_list_init ? default_list_init : uic_ucx_list_init, default_list_init_userdata);
107 110 }
108 list->update = NULL; 111
109 list->getselection = NULL; 112 UiList* ui_list_new2(UiContext *ctx, const char *name, ui_list_init_func listinit, void *userdata) {
110 list->obj = NULL; 113 UiList *list = cxMalloc(ctx->mp->allocator, sizeof(UiList));
114 memset(list, 0, sizeof(UiList));
115 listinit(ctx, list, userdata);
111 116
112 if(name) { 117 if(name) {
113 uic_reg_var(ctx, name, UI_VAR_LIST, list); 118 uic_reg_var(ctx, name, UI_VAR_LIST, list);
114 } 119 }
115 120
246 } 251 }
247 252
248 // types 253 // types
249 254
250 // public functions 255 // public functions
251 UiInteger* ui_int_new(UiContext *ctx, char *name) { 256 UiInteger* ui_int_new(UiContext *ctx, const char *name) {
252 UiInteger *i = ui_malloc(ctx, sizeof(UiInteger)); 257 UiInteger *i = ui_malloc(ctx, sizeof(UiInteger));
253 memset(i, 0, sizeof(UiInteger)); 258 memset(i, 0, sizeof(UiInteger));
254 if(name) { 259 if(name) {
255 uic_reg_var(ctx, name, UI_VAR_INTEGER, i); 260 uic_reg_var(ctx, name, UI_VAR_INTEGER, i);
256 } 261 }
257 return i; 262 return i;
258 } 263 }
259 264
260 UiDouble* ui_double_new(UiContext *ctx, char *name) { 265 UiDouble* ui_double_new(UiContext *ctx, const char *name) {
261 UiDouble *d = ui_malloc(ctx, sizeof(UiDouble)); 266 UiDouble *d = ui_malloc(ctx, sizeof(UiDouble));
262 memset(d, 0, sizeof(UiDouble)); 267 memset(d, 0, sizeof(UiDouble));
263 if(name) { 268 if(name) {
264 uic_reg_var(ctx, name, UI_VAR_DOUBLE, d); 269 uic_reg_var(ctx, name, UI_VAR_DOUBLE, d);
265 } 270 }
266 return d; 271 return d;
267 } 272 }
268 273
269 UiString* ui_string_new(UiContext *ctx, char *name) { 274 UiString* ui_string_new(UiContext *ctx, const char *name) {
270 UiString *s = ui_malloc(ctx, sizeof(UiString)); 275 UiString *s = ui_malloc(ctx, sizeof(UiString));
271 memset(s, 0, sizeof(UiString)); 276 memset(s, 0, sizeof(UiString));
272 if(name) { 277 if(name) {
273 uic_reg_var(ctx, name, UI_VAR_STRING, s); 278 uic_reg_var(ctx, name, UI_VAR_STRING, s);
274 } 279 }
275 return s; 280 return s;
276 } 281 }
277 282
278 UiText* ui_text_new(UiContext *ctx, char *name) { 283 UiText* ui_text_new(UiContext *ctx, const char *name) {
279 UiText *t = ui_malloc(ctx, sizeof(UiText)); 284 UiText *t = ui_malloc(ctx, sizeof(UiText));
280 memset(t, 0, sizeof(UiText)); 285 memset(t, 0, sizeof(UiText));
281 if(name) { 286 if(name) {
282 uic_reg_var(ctx, name, UI_VAR_TEXT, t); 287 uic_reg_var(ctx, name, UI_VAR_TEXT, t);
283 } 288 }
284 return t; 289 return t;
285 } 290 }
286 291
287 UiRange* ui_range_new(UiContext *ctx, char *name) { 292 UiRange* ui_range_new(UiContext *ctx, const char *name) {
288 UiRange *r = ui_malloc(ctx, sizeof(UiRange)); 293 UiRange *r = ui_malloc(ctx, sizeof(UiRange));
289 memset(r, 0, sizeof(UiRange)); 294 memset(r, 0, sizeof(UiRange));
290 if(name) { 295 if(name) {
291 uic_reg_var(ctx, name, UI_VAR_RANGE, r); 296 uic_reg_var(ctx, name, UI_VAR_RANGE, r);
292 } 297 }
293 return r; 298 return r;
294 } 299 }
295 300
296 UIEXPORT UiGeneric* ui_generic_new(UiContext *ctx, char *name) { 301 UIEXPORT UiGeneric* ui_generic_new(UiContext *ctx, const char *name) {
297 UiGeneric *g = ui_malloc(ctx, sizeof(UiGeneric)); 302 UiGeneric *g = ui_malloc(ctx, sizeof(UiGeneric));
298 memset(g, 0, sizeof(UiGeneric)); 303 memset(g, 0, sizeof(UiGeneric));
299 if(name) { 304 if(name) {
300 uic_reg_var(ctx, name, UI_VAR_GENERIC, g); 305 uic_reg_var(ctx, name, UI_VAR_GENERIC, g);
301 } 306 }
402 else { 407 else {
403 return 0; 408 return 0;
404 } 409 }
405 } 410 }
406 411
412 void ui_generic_set_image(UiGeneric *g, void *img) {
413 if(g->set) {
414 g->set(g, img, UI_IMAGE_OBJECT_TYPE);
415 } else {
416 if(g->value) {
417 ui_image_unref(g->value);
418 }
419 ui_image_ref(img);
420 g->value = img;
421 g->type = UI_IMAGE_OBJECT_TYPE;
422 }
423 }
424
425 void* ui_generic_get_image(UiGeneric *g) {
426 if(g->type) {
427 if(!strcmp(g->type, UI_IMAGE_OBJECT_TYPE)) {
428 return g->value;
429 } else {
430 return NULL;
431 }
432 }
433 return g->value;
434 }
435
407 436
408 // private functions 437 // private functions
409 void uic_int_copy(UiInteger *from, UiInteger *to) { 438 void uic_int_copy(UiInteger *from, UiInteger *to) {
410 to->get = from->get; 439 to->get = from->get;
411 to->set = from->set; 440 to->set = from->set;
450 to->obj = from->obj; 479 to->obj = from->obj;
451 } 480 }
452 481
453 void uic_list_copy(UiList *from, UiList *to) { 482 void uic_list_copy(UiList *from, UiList *to) {
454 to->update = from->update; 483 to->update = from->update;
484 to->getselection = from->getselection;
485 to->setselection = from->setselection;
455 to->obj = from->obj; 486 to->obj = from->obj;
456 } 487 }
457 488
458 void uic_generic_copy(UiGeneric *from, UiGeneric *to) { 489 void uic_generic_copy(UiGeneric *from, UiGeneric *to) {
459 to->get = from->get; 490 to->get = from->get;
632 } 663 }
633 664
634 int ui_get_setop(void) { 665 int ui_get_setop(void) {
635 return ui_set_op; 666 return ui_set_op;
636 } 667 }
668
669 /* ---------------- List initializers and wrapper functions ---------------- */
670
671 void ui_global_list_initializer(ui_list_init_func func, void *userdata) {
672 default_list_init = func;
673 default_list_init_userdata = userdata;
674 }
675
676 void ui_list_class_set_first(UiList *list, void*(*first)(UiList *list)) {
677 list->first = first;
678 }
679
680 void ui_list_class_set_next(UiList *list, void*(*next)(UiList *list)) {
681 list->next = next;
682 }
683
684 void ui_list_class_set_get(UiList *list, void*(*get)(UiList *list, int i)) {
685 list->get = get;
686 }
687
688 void ui_list_class_set_count(UiList *list, int(*count)(UiList *list)) {
689 list->count = count;
690 }
691
692 void ui_list_class_set_data(UiList *list, void *data) {
693 list->data = data;
694 }
695
696 void ui_list_class_set_iter(UiList *list, void *iter) {
697 list->iter = iter;
698 }

mercurial