ui/common/object.c

changeset 108
77254bd6dccb
parent 101
7b3a3130be44
child 110
c00e968d018b
equal deleted inserted replaced
107:b34bd1557c6c 108:77254bd6dccb
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 }
82 UiEvent ev; 122 UiEvent ev;
83 ev.window = obj->window; 123 ev.window = obj->window;
84 ev.document = obj->ctx->document; 124 ev.document = obj->ctx->document;
85 ev.obj = obj; 125 ev.obj = obj;
86 ev.eventdata = NULL; 126 ev.eventdata = NULL;
127 ev.eventdatatype = 0;
87 ev.intval = 0; 128 ev.intval = 0;
88 obj->ctx->close_callback(&ev, obj->ctx->close_data); 129 obj->ctx->close_callback(&ev, obj->ctx->close_data);
89 } 130 }
131 uic_object_destroyed(obj);
90 cxMempoolFree(obj->ctx->mp); 132 cxMempoolFree(obj->ctx->mp);
133 }
134
135 UiObject* uic_object_new_toplevel(void) {
136 CxMempool *mp = cxMempoolCreateSimple(256);
137 UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject));
138 obj->ctx = uic_context(obj, mp);
139 uic_object_created(obj);
140 return obj;
91 } 141 }
92 142
93 UiObject* uic_object_new(UiObject *toplevel, UIWIDGET widget) { 143 UiObject* uic_object_new(UiObject *toplevel, UIWIDGET widget) {
94 return uic_ctx_object_new(toplevel->ctx, widget); 144 return uic_ctx_object_new(toplevel->ctx, widget);
95 } 145 }
96 146
97 UiObject* uic_ctx_object_new(UiContext *ctx, UIWIDGET widget) { 147 UiObject* uic_ctx_object_new(UiContext *ctx, UIWIDGET widget) {
98 UiObject *newobj = cxCalloc(ctx->allocator, 1, sizeof(UiObject)); 148 UiObject *newobj = cxCalloc(ctx->allocator, 1, sizeof(UiObject));
99 newobj->ctx = ctx; 149 newobj->ctx = ctx;
100 newobj->widget = widget; 150 newobj->widget = widget;
101 151 uic_object_created(newobj);
102 return newobj; 152 return newobj;
103 } 153 }
104 154
105 void uic_obj_add(UiObject *toplevel, UiObject *ctobj) { 155 void uic_obj_add(UiObject *toplevel, UiObject *ctobj) {
106 UiObject *current = uic_current_obj(toplevel); 156 UiObject *current = uic_current_obj(toplevel);

mercurial