ui/common/object.c

changeset 108
77254bd6dccb
parent 101
7b3a3130be44
child 110
c00e968d018b
--- a/ui/common/object.c	Sat Apr 05 17:57:04 2025 +0200
+++ b/ui/common/object.c	Sun Jul 20 22:04:39 2025 +0200
@@ -32,8 +32,48 @@
 #include "object.h"
 #include "context.h"
 
+#include <cx/linked_list.h>
+
 #include "../ui/container.h"
 
+static CxList *creation_callbacks;
+static CxList *destruction_callbacks;
+
+typedef struct objcallback {
+    ui_object_callback func;
+    void *userdata;
+} objcallback;
+
+void ui_register_object_creation_callback(ui_object_callback func, void *userdata) {
+    if(!creation_callbacks) {
+        creation_callbacks = cxLinkedListCreateSimple(sizeof(objcallback));
+    }
+    objcallback cb = { func, userdata };
+    cxListAdd(creation_callbacks, &cb);
+}
+
+void ui_register_object_destruction_callback(ui_object_callback func, void *userdata) {
+    if(!destruction_callbacks) {
+        destruction_callbacks = cxLinkedListCreateSimple(sizeof(objcallback));
+    }
+    objcallback cb = { func, userdata };
+    cxListAdd(destruction_callbacks, &cb);
+}
+
+void uic_object_created(UiObject *obj) {
+    CxIterator i = cxListIterator(creation_callbacks);
+    cx_foreach(objcallback *, cb, i) {
+        cb->func(obj, cb->userdata);
+    }
+}
+
+void uic_object_destroyed(UiObject *obj) {
+    CxIterator i = cxListIterator(destruction_callbacks);
+    cx_foreach(objcallback *, cb, i) {
+        cb->func(obj, cb->userdata);
+    }
+}
+
 void ui_end(UiObject *obj) {
     if(!obj->next) {
         return;
@@ -84,12 +124,22 @@
         ev.document = obj->ctx->document;
         ev.obj = obj;
         ev.eventdata = NULL;
+        ev.eventdatatype = 0;
         ev.intval = 0;
         obj->ctx->close_callback(&ev, obj->ctx->close_data);
     }
+    uic_object_destroyed(obj);
     cxMempoolFree(obj->ctx->mp);
 }
 
+UiObject* uic_object_new_toplevel(void) {
+    CxMempool *mp = cxMempoolCreateSimple(256);
+    UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject));
+    obj->ctx = uic_context(obj, mp);
+    uic_object_created(obj);
+    return obj;
+}
+
 UiObject* uic_object_new(UiObject *toplevel, UIWIDGET widget) {
     return uic_ctx_object_new(toplevel->ctx, widget);
 }
@@ -98,7 +148,7 @@
     UiObject *newobj = cxCalloc(ctx->allocator, 1, sizeof(UiObject));
     newobj->ctx = ctx;
     newobj->widget = widget;
-    
+    uic_object_created(newobj);
     return newobj;
 }
 

mercurial