ui/gtk/window.c

changeset 86
8e7c57c23133
parent 72
d5307e9ee384
--- a/ui/gtk/window.c	Thu Nov 14 23:22:35 2024 +0100
+++ b/ui/gtk/window.c	Fri Nov 15 21:50:20 2024 +0100
@@ -51,18 +51,7 @@
 
 static gboolean ui_window_destroy(void *data)  {
     UiObject *obj = data;
-    UiEvent ev;
-    ev.window = obj->window;
-    ev.document = obj->ctx->document;
-    ev.obj = obj;
-    ev.eventdata = NULL;
-    ev.intval = 0;
-    
-    if(obj->ctx->close_callback) {
-        obj->ctx->close_callback(&ev, obj->ctx->close_data);
-    }
-    
-    cxMempoolDestroy(obj->ctx->mp);
+    uic_object_destroy(obj);
     
     nwindows--;
 #ifdef UI_GTK2
@@ -74,27 +63,48 @@
     return FALSE;
 }
 
+void ui_window_widget_destroy(UiObject *obj) {
+#if GTK_MAJOR_VERSION >= 4
+    gtk_window_destroy(GTK_WINDOW(obj->widget));
+#else
+    gtk_widget_destroy(obj->widget);
+#endif
+}
+
 void ui_exit_event(GtkWidget *widget, gpointer data) {
     // delay exit handler  
-    UiObject *obj = data;
     g_idle_add(ui_window_destroy, data);
 }
 
+static gboolean ui_window_close_request(UiObject *obj) {
+    uic_context_prepare_close(obj->ctx);
+    obj->ref--;
+    if(obj->ref > 0) {
+#if GTK_CHECK_VERSION(2, 18, 0)
+        gtk_widget_set_visible(obj->widget, FALSE);
+#else
+        gtk_widget_hide(obj->widget);
+#endif
+        return TRUE;
+    } else {
+        return FALSE;
+    }
+}
+
 #if GTK_MAJOR_VERSION >= 4
-static gboolean close_request(GtkWindow* self, UiContext *ctx) {
-    uic_context_prepare_close(ctx);
-    return FALSE;
+static gboolean close_request(GtkWindow* self, UiObject *obj) {
+    return ui_window_close_request(obj);
 }
 #else
-static gboolean close_request(GtkWidget* self, GdkEvent* event, UiContext *ctx) {
-    uic_context_prepare_close(ctx);
-    return FALSE;
+static gboolean close_request(GtkWidget* self, GdkEvent* event, UiObject *obj) {
+    return ui_window_close_request(obj);
 }
 #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)); 
+    UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject));
+    obj->ref = 0;
    
 #ifdef UI_LIBADWAITA
     obj->widget = adw_application_window_new(ui_get_application());
@@ -130,6 +140,7 @@
                 window_default_height);
     }
     
+    obj->destroy = ui_window_widget_destroy;
     g_signal_connect(
             obj->widget,
             "destroy",
@@ -140,13 +151,13 @@
             obj->widget,
             "close-request",
             G_CALLBACK(close_request),
-            obj->ctx);
+            obj);
 #else
     g_signal_connect(
             obj->widget,
             "delete-event",
             G_CALLBACK(close_request),
-            obj->ctx);
+            obj);
 #endif
     
     GtkWidget *vbox = ui_gtk_vbox_new(0);
@@ -683,6 +694,8 @@
     UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); 
     obj->ctx = uic_context(obj, mp);
     obj->widget = dialog;
+    obj->ref = 0;
+    obj->destroy = ui_window_widget_destroy;
     nwindows++;
     
     if(args.title != NULL) {
@@ -710,6 +723,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);
+#else
+    g_signal_connect(
+            obj->widget,
+            "delete-event",
+            G_CALLBACK(close_request),
+            obj);
+#endif
     
 #if GTK_MAJOR_VERSION < 4
     GtkWidget *c = gtk_dialog_get_content_area(GTK_DIALOG(dialog));

mercurial