ui/common/object.c

changeset 626
724c7036a03e
parent 440
7c4b9cba09ca
child 627
3f0c9fe60c68
equal deleted inserted replaced
625:1726c5109ccd 626:724c7036a03e
30 #include <stdlib.h> 30 #include <stdlib.h>
31 31
32 #include "object.h" 32 #include "object.h"
33 #include "context.h" 33 #include "context.h"
34 34
35 #include <cx/linked_list.h>
36
35 #include "../ui/container.h" 37 #include "../ui/container.h"
38
39 static CxList *creation_callbacks;
40 static CxList *destruction_callbacks;
41
42 typedef struct objcallback {
43 ui_object_callback func;
44 void *userdata;
45 } objcallback;
46
47 void ui_register_object_creation_callback(ui_object_callback func, void *userdata) {
48 if(!creation_callbacks) {
49 creation_callbacks = cxLinkedListCreateSimple(sizeof(objcallback));
50 }
51 objcallback cb = { func, userdata };
52 cxListAdd(creation_callbacks, &cb);
53 }
54
55 void ui_register_object_destruction_callback(ui_object_callback func, void *userdata) {
56 if(!destruction_callbacks) {
57 destruction_callbacks = cxLinkedListCreateSimple(sizeof(objcallback));
58 }
59 objcallback cb = { func, userdata };
60 cxListAdd(destruction_callbacks, &cb);
61 }
62
63 void uic_object_created(UiObject *obj) {
64 CxIterator i = cxListIterator(creation_callbacks);
65 cx_foreach(objcallback *, cb, i) {
66 cb->func(obj, cb->userdata);
67 }
68 }
69
70 void uic_object_destroyed(UiObject *obj) {
71 CxIterator i = cxListIterator(destruction_callbacks);
72 cx_foreach(objcallback *, cb, i) {
73 cb->func(obj, cb->userdata);
74 }
75 }
36 76
37 void ui_end(UiObject *obj) { 77 void ui_end(UiObject *obj) {
38 if(!obj->next) { 78 if(!obj->next) {
39 return; 79 return;
40 } 80 }
85 ev.obj = obj; 125 ev.obj = obj;
86 ev.eventdata = NULL; 126 ev.eventdata = NULL;
87 ev.intval = 0; 127 ev.intval = 0;
88 obj->ctx->close_callback(&ev, obj->ctx->close_data); 128 obj->ctx->close_callback(&ev, obj->ctx->close_data);
89 } 129 }
130 uic_object_destroyed(obj);
90 cxMempoolFree(obj->ctx->mp); 131 cxMempoolFree(obj->ctx->mp);
91 } 132 }
92 133
93 UiObject* uic_object_new(UiObject *toplevel, UIWIDGET widget) { 134 UiObject* uic_object_new(UiObject *toplevel, UIWIDGET widget) {
94 return uic_ctx_object_new(toplevel->ctx, widget); 135 return uic_ctx_object_new(toplevel->ctx, widget);
96 137
97 UiObject* uic_ctx_object_new(UiContext *ctx, UIWIDGET widget) { 138 UiObject* uic_ctx_object_new(UiContext *ctx, UIWIDGET widget) {
98 UiObject *newobj = cxCalloc(ctx->allocator, 1, sizeof(UiObject)); 139 UiObject *newobj = cxCalloc(ctx->allocator, 1, sizeof(UiObject));
99 newobj->ctx = ctx; 140 newobj->ctx = ctx;
100 newobj->widget = widget; 141 newobj->widget = widget;
101 142 uic_object_created(newobj);
102 return newobj; 143 return newobj;
103 } 144 }
104 145
105 void uic_obj_add(UiObject *toplevel, UiObject *ctobj) { 146 void uic_obj_add(UiObject *toplevel, UiObject *ctobj) {
106 UiObject *current = uic_current_obj(toplevel); 147 UiObject *current = uic_current_obj(toplevel);

mercurial