# HG changeset patch
# User Olaf Wintermann <olaf.wintermann@gmail.com>
# Date 1730195068 -3600
# Node ID d8991e166f2ac9984254b179f2b26377d6ccc705
# Parent  79a9aadf1c70164766c3669aa63e03643b5139a6
add toolkit window close fix

diff -r 79a9aadf1c70 -r d8991e166f2a ui/common/context.c
--- a/ui/common/context.c	Tue Oct 29 10:27:16 2024 +0100
+++ b/ui/common/context.c	Tue Oct 29 10:44:28 2024 +0100
@@ -81,6 +81,11 @@
     return ctx->parent ? uic_root_context(ctx->parent) : ctx;
 }
 
+void uic_context_prepare_close(UiContext *ctx) {
+    cxListClear(ctx->groups);
+    cxListClear(ctx->group_widgets);
+}
+
 void uic_context_attach_document(UiContext *ctx, void *document) {
     cxListAdd(ctx->documents, document);
     ctx->document = document;
diff -r 79a9aadf1c70 -r d8991e166f2a ui/common/context.h
--- a/ui/common/context.h	Tue Oct 29 10:27:16 2024 +0100
+++ b/ui/common/context.h	Tue Oct 29 10:44:28 2024 +0100
@@ -115,6 +115,8 @@
 void uic_context_set_document(UiContext *ctx, void *document); // deprecated
 void uic_context_detach_document(UiContext *ctx); // deprecated
 
+void uic_context_prepare_close(UiContext *ctx);
+
 void uic_context_attach_document(UiContext *ctx, void *document);
 void uic_context_detach_document2(UiContext *ctx, void *document);
 void uic_context_detach_all(UiContext *ctx);
diff -r 79a9aadf1c70 -r d8991e166f2a ui/gtk/toolkit.c
--- a/ui/gtk/toolkit.c	Tue Oct 29 10:27:16 2024 +0100
+++ b/ui/gtk/toolkit.c	Tue Oct 29 10:44:28 2024 +0100
@@ -166,6 +166,7 @@
 }
 
 void ui_close(UiObject *obj) {
+    uic_context_prepare_close(obj->ctx);
 #if GTK_CHECK_VERSION(4, 0, 0)
     gtk_window_close(GTK_WINDOW(obj->widget));
 #else
diff -r 79a9aadf1c70 -r d8991e166f2a ui/gtk/window.c
--- a/ui/gtk/window.c	Tue Oct 29 10:27:16 2024 +0100
+++ b/ui/gtk/window.c	Tue Oct 29 10:44:28 2024 +0100
@@ -71,6 +71,18 @@
 #endif
 }
 
+#if GTK_MAJOR_VERSION >= 4
+static gboolean close_request(GtkWindow* self, UiContext *ctx) {
+    uic_context_prepare_close(ctx);
+    return FALSE;
+}
+#else
+static gboolean close_request(GtkWidget* self, GdkEvent* event, UiContext *ctx) {
+    uic_context_prepare_close(ctx);
+    return FALSE;
+}
+#endif
+
 static UiObject* create_window(const char *title, void *window_data, UiBool simple) {
     CxMempool *mp = cxBasicMempoolCreate(256);
     UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); 
@@ -114,6 +126,19 @@
             "destroy",
             G_CALLBACK(ui_exit_event),
             obj);
+#if GTK_MAJOR_VERSION >= 4
+    g_signal_connect(
+            obj->widget,
+            "close-request",
+            G_CALLBACK(close_request),
+            obj->ctx);
+#else
+    g_signal_connect(
+            obj->widget,
+            "delete-event",
+            G_CALLBACK(close_request),
+            obj->ctx);
+#endif
     
     GtkWidget *vbox = ui_gtk_vbox_new(0);
 #ifdef UI_LIBADWAITA