add document attach/detach callbacks

Sat, 13 Jun 2026 17:08:09 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 13 Jun 2026 17:08:09 +0200
changeset 1195
6ec3c71ba298
parent 1194
91696f7fa475
child 1196
296b8b6eaae6

add document attach/detach callbacks

ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/context.h file | annotate | diff | comparison | revisions
ui/common/document.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/common/context.c	Sat Jun 13 14:12:43 2026 +0200
+++ b/ui/common/context.c	Sat Jun 13 17:08:09 2026 +0200
@@ -124,8 +124,8 @@
     ev.intval = 0;
     ev.set = 0;
 
-    if(ctx->close_callback) {
-        ctx->close_callback(&ev, ctx->close_data);
+    if(ctx->onclose) {
+        ctx->onclose(&ev, ctx->onclosedata);
     }
     
     CxIterator i = cxListIterator(ctx->destroy_handler);
@@ -190,6 +190,12 @@
     doc_ctx->ref++;
     
     uic_context_update_bindings(doc_ctx);
+    if(doc_ctx->onattach) {
+        UiEvent event;
+        memset(&event, 0, sizeof(UiEvent));
+        event.document = document;
+        doc_ctx->onattach(&event, doc_ctx->onattachdata);
+    }
 }
 
 static void uic_context_unbind_vars(UiContext *ctx) {
@@ -233,12 +239,18 @@
     cxListRemove(ctx->documents, docIndex);
     ctx->document = cxListAt(ctx->documents, 0);
     
-    UiContext *docctx = ui_document_context(document);
-    uic_context_unbind_vars(docctx); // unbind all doc/subdoc vars from the parent
-    docctx->parent = NULL;
+    UiContext *doc_ctx = ui_document_context(document);
+    uic_context_unbind_vars(doc_ctx); // unbind all doc/subdoc vars from the parent
+    doc_ctx->parent = NULL;
     ui_document_unref(document);
     
     ui_update_action_bindings(ctx);
+    if(doc_ctx->ondetach) {
+        UiEvent event;
+        memset(&event, 0, sizeof(UiEvent));
+        event.document = document;
+        doc_ctx->ondetach(&event, doc_ctx->ondetachdata);
+    }
 }
 
 void uic_context_detach_all(UiContext *ctx) {
@@ -600,8 +612,8 @@
 }
 
 void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata) {
-    ctx->close_callback = fnc;
-    ctx->close_data = udata;
+    ctx->onclose = fnc;
+    ctx->onclosedata = udata;
 }
 
 UiContext* ui_context_parent(UiContext *ctx) {
--- a/ui/common/context.h	Sat Jun 13 14:12:43 2026 +0200
+++ b/ui/common/context.h	Sat Jun 13 17:08:09 2026 +0200
@@ -98,8 +98,14 @@
     // attaching a document will automatically detach the current document
     UiBool single_document_mode;
     
-    ui_callback   close_callback;
-    void          *close_data;
+    ui_callback   onattach;
+    void          *onattachdata;
+    
+    ui_callback   ondetach;
+    void          *ondetachdata;
+    
+    ui_callback   onclose;
+    void          *onclosedata;
     
     unsigned int  ref;
 };
--- a/ui/common/document.c	Sat Jun 13 14:12:43 2026 +0200
+++ b/ui/common/document.c	Sat Jun 13 17:08:09 2026 +0200
@@ -86,3 +86,15 @@
 UiObject* ui_context_obj(UiContext *ctx) {
     return ctx->obj;
 }
+
+void  ui_document_onattach(void *doc, ui_callback cb, void *data) {
+    UiContext *ctx = ui_document_context(doc);
+    ctx->onattach = cb;
+    ctx->onattachdata = data;
+}
+
+void  ui_document_ondetach(void *doc, ui_callback cb, void *data) {
+    UiContext *ctx = ui_document_context(doc);
+    ctx->ondetach = cb;
+    ctx->ondetachdata = data;
+}
--- a/ui/ui/toolkit.h	Sat Jun 13 14:12:43 2026 +0200
+++ b/ui/ui/toolkit.h	Sat Jun 13 17:08:09 2026 +0200
@@ -576,6 +576,8 @@
 UIEXPORT void  ui_document_ref(void *doc);
 UIEXPORT void  ui_document_unref(void *doc);
 UIEXPORT void  ui_document_destroy(void *doc);
+UIEXPORT void  ui_document_onattach(void *doc, ui_callback cb, void *data);
+UIEXPORT void  ui_document_ondetach(void *doc, ui_callback cb, void *data);
 
 UIEXPORT UiContext* ui_document_context(void *doc);
 UIEXPORT void* ui_context_document(UiContext *ctx);

mercurial