[gtk+] Replace GtkWidget::delete-event by GtkWindow::close-request



commit a739ee690517da1a53258a5d0ca58e22b47400f5
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jan 1 15:24:16 2018 -0500

    Replace GtkWidget::delete-event by GtkWindow::close-request
    
    The event is not useful at all, so we are better off
    with a signal that doesn't have it, and it is only
    relevant on toplevel windows, so we don't need it on
    GtkWidget.
    
    With this commit, delete events no longer go through the
    ::event, ::delete-event, ::event-after widget signals,
    but just cause the ::close-request signal on GtkWindow to be
    emitted.

 gtk/gtkassistant.c |   16 +++++----
 gtk/gtkdialog.c    |   15 ++++----
 gtk/gtkwindow.c    |   91 ++++++++++++++++++++++++++++-----------------------
 gtk/gtkwindow.h    |    2 +
 4 files changed, 69 insertions(+), 55 deletions(-)
---
diff --git a/gtk/gtkassistant.c b/gtk/gtkassistant.c
index 03462d9..e6b5b49 100644
--- a/gtk/gtkassistant.c
+++ b/gtk/gtkassistant.c
@@ -133,8 +133,8 @@ struct _GtkAssistantPrivate
 static void     gtk_assistant_destroy            (GtkWidget         *widget);
 static void     gtk_assistant_map                (GtkWidget         *widget);
 static void     gtk_assistant_unmap              (GtkWidget         *widget);
-static gboolean gtk_assistant_delete_event       (GtkWidget         *widget,
-                                                  GdkEventAny       *event);
+static gboolean gtk_assistant_close_request      (GtkWindow         *window);
+
 static void     gtk_assistant_add                (GtkContainer      *container,
                                                   GtkWidget         *page);
 static void     gtk_assistant_remove             (GtkContainer      *container,
@@ -377,11 +377,13 @@ gtk_assistant_class_init (GtkAssistantClass *class)
   GObjectClass *gobject_class;
   GtkWidgetClass *widget_class;
   GtkContainerClass *container_class;
+  GtkWindowClass *window_class;
   GtkBindingSet *binding_set;
 
   gobject_class   = (GObjectClass *) class;
   widget_class    = (GtkWidgetClass *) class;
   container_class = (GtkContainerClass *) class;
+  window_class    = (GtkWindowClass *) class;
 
   gobject_class->constructed  = gtk_assistant_constructed;
   gobject_class->set_property = gtk_assistant_set_property;
@@ -390,7 +392,6 @@ gtk_assistant_class_init (GtkAssistantClass *class)
   widget_class->destroy = gtk_assistant_destroy;
   widget_class->map = gtk_assistant_map;
   widget_class->unmap = gtk_assistant_unmap;
-  widget_class->delete_event = gtk_assistant_delete_event;
 
   gtk_widget_class_set_accessible_type (widget_class, _gtk_assistant_accessible_get_type ());
 
@@ -399,6 +400,8 @@ gtk_assistant_class_init (GtkAssistantClass *class)
   container_class->set_child_property = gtk_assistant_set_child_property;
   container_class->get_child_property = gtk_assistant_get_child_property;
 
+  window_class->close_request = gtk_assistant_close_request;
+
   /**
    * GtkAssistant::cancel:
    * @assistant: the #GtkAssistant
@@ -1333,17 +1336,16 @@ gtk_assistant_unmap (GtkWidget *widget)
 }
 
 static gboolean
-gtk_assistant_delete_event (GtkWidget   *widget,
-                            GdkEventAny *event)
+gtk_assistant_close_request (GtkWindow *window)
 {
-  GtkAssistant *assistant = GTK_ASSISTANT (widget);
+  GtkAssistant *assistant = GTK_ASSISTANT (window);
   GtkAssistantPrivate *priv = assistant->priv;
 
   /* Do not allow cancelling in the middle of a progress page */
   if (priv->current_page &&
       (priv->current_page->type != GTK_ASSISTANT_PAGE_PROGRESS ||
        priv->current_page->complete))
-    g_signal_emit (widget, signals [CANCEL], 0, NULL);
+    g_signal_emit (assistant, signals [CANCEL], 0, NULL);
 
   return TRUE;
 }
diff --git a/gtk/gtkdialog.c b/gtk/gtkdialog.c
index adebb3d..25e0d92 100644
--- a/gtk/gtkdialog.c
+++ b/gtk/gtkdialog.c
@@ -191,8 +191,7 @@ static void      gtk_dialog_add_buttons_valist   (GtkDialog    *dialog,
                                                   const gchar  *first_button_text,
                                                   va_list       args);
 
-static gboolean  gtk_dialog_delete_event         (GtkWidget    *widget,
-                                                  GdkEventAny  *event);
+static gboolean  gtk_dialog_close_request        (GtkWindow    *window);
 static void      gtk_dialog_map                  (GtkWidget    *widget);
 
 static void      gtk_dialog_close                (GtkDialog    *dialog);
@@ -510,10 +509,12 @@ gtk_dialog_class_init (GtkDialogClass *class)
 {
   GObjectClass *gobject_class;
   GtkWidgetClass *widget_class;
+  GtkWindowClass *window_class;
   GtkBindingSet *binding_set;
 
   gobject_class = G_OBJECT_CLASS (class);
   widget_class = GTK_WIDGET_CLASS (class);
+  window_class = GTK_WINDOW_CLASS (class);
 
   gobject_class->constructed  = gtk_dialog_constructed;
   gobject_class->set_property = gtk_dialog_set_property;
@@ -521,7 +522,8 @@ gtk_dialog_class_init (GtkDialogClass *class)
   gobject_class->finalize = gtk_dialog_finalize;
 
   widget_class->map = gtk_dialog_map;
-  widget_class->delete_event = gtk_dialog_delete_event;
+
+  window_class->close_request = gtk_dialog_close_request;
 
   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_DIALOG);
 
@@ -622,13 +624,12 @@ gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
 }
 
 static gboolean
-gtk_dialog_delete_event (GtkWidget   *widget,
-                         GdkEventAny *event)
+gtk_dialog_close_request (GtkWindow *window)
 {
   /* emit response signal, this will shut down the loop if we are in gtk_dialog_run */
-  gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
+  gtk_dialog_response (GTK_DIALOG (window), GTK_RESPONSE_DELETE_EVENT);
 
-  return GTK_WIDGET_CLASS (gtk_dialog_parent_class)->delete_event (widget, event);
+  return GTK_WINDOW_CLASS (gtk_dialog_parent_class)->close_request (window);
 }
 
 static GList *
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index 400bd2a..c00d55e 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -200,7 +200,6 @@ struct _GtkWindowPrivate
   gchar   *wm_role;
 
   guint    keys_changed_handler;
-  guint    delete_event_handler;
 
   guint32  initial_timestamp;
 
@@ -292,6 +291,7 @@ enum {
   ACTIVATE_DEFAULT,
   KEYS_CHANGED,
   ENABLE_DEBUGGING,
+  CLOSE_REQUEST,
   LAST_SIGNAL
 };
 
@@ -417,8 +417,8 @@ static void gtk_window_size_allocate      (GtkWidget           *widget,
                                            const GtkAllocation *allocation,
                                            int                  baseline,
                                            GtkAllocation       *out_clip);
-static gboolean gtk_window_delete_event   (GtkWidget         *widget,
-                                           GdkEventAny       *event);
+static gboolean gtk_window_close_request  (GtkWindow         *window);
+static gboolean gtk_window_emit_close_request (GtkWindow *window);
 static gboolean gtk_window_map_event      (GtkWidget         *widget,
                                            GdkEventAny       *event);
 static gint gtk_window_configure_event    (GtkWidget         *widget,
@@ -802,7 +802,6 @@ gtk_window_class_init (GtkWindowClass *klass)
   widget_class->realize = gtk_window_realize;
   widget_class->unrealize = gtk_window_unrealize;
   widget_class->size_allocate = gtk_window_size_allocate;
-  widget_class->delete_event = gtk_window_delete_event;
   widget_class->configure_event = gtk_window_configure_event;
   widget_class->event = gtk_window_event;
   widget_class->key_press_event = gtk_window_key_press_event;
@@ -828,6 +827,7 @@ gtk_window_class_init (GtkWindowClass *klass)
   klass->activate_focus = gtk_window_real_activate_focus;
   klass->keys_changed = gtk_window_keys_changed;
   klass->enable_debugging = gtk_window_enable_debugging;
+  klass->close_request = gtk_window_close_request;
 
   window_props[PROP_TYPE] =
       g_param_spec_enum ("type",
@@ -1258,6 +1258,25 @@ gtk_window_class_init (GtkWindowClass *klass)
                   G_TYPE_BOOLEAN,
                   1, G_TYPE_BOOLEAN);
 
+  /**
+   * GtkWindow::close-request:
+   * @window: the window on which the signal is emitted
+   *
+   * The ::close-request signal is emitted when the user clicks on the close
+   * button of the window.
+   *
+   * Return: %TRUE to stop other handlers from being invoked for the signal
+   */
+  window_signals[CLOSE_REQUEST] =
+    g_signal_new (I_("close-request"),
+                  G_TYPE_FROM_CLASS (gobject_class),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (GtkWindowClass, close_request),
+                  _gtk_boolean_handled_accumulator, NULL,
+                  NULL,
+                  G_TYPE_BOOLEAN,
+                  0);
+
   /*
    * Key bindings
    */
@@ -1334,26 +1353,6 @@ _gtk_window_toggle_maximized (GtkWindow *window)
     gtk_window_maximize (window);
 }
 
-static gboolean
-send_delete_event (gpointer data)
-{
-  GtkWidget *window = data;
-  GtkWindowPrivate *priv = GTK_WINDOW (window)->priv;
-
-  GdkEvent *event;
-
-  event = gdk_event_new (GDK_DELETE);
-
-  event->any.window = g_object_ref (_gtk_widget_get_window (window));
-  event->any.send_event = TRUE;
-  priv->delete_event_handler = 0;
-
-  gtk_main_do_event (event);
-  g_object_unref (event);
-
-  return G_SOURCE_REMOVE;
-}
-
 /**
  * gtk_window_close:
  * @window: a #GtkWindow
@@ -1372,8 +1371,12 @@ gtk_window_close (GtkWindow *window)
   if (!_gtk_widget_get_realized (GTK_WIDGET (window)))
     return;
 
-  window->priv->delete_event_handler = gdk_threads_add_idle_full (G_PRIORITY_DEFAULT, send_delete_event, 
window, NULL);
-  g_source_set_name_by_id (window->priv->delete_event_handler, "[gtk+] send_delete_event");
+  g_object_ref (window);
+
+  if (!gtk_window_emit_close_request (window))
+    gtk_widget_destroy (GTK_WIDGET (window));
+
+  g_object_unref (window);
 }
 
 static void
@@ -5971,19 +5974,27 @@ gtk_window_destroy (GtkWidget *widget)
 }
 
 static gboolean
-gtk_window_delete_event (GtkWidget   *widget,
-                         GdkEventAny *event)
+gtk_window_close_request (GtkWindow *window)
 {
-  GtkWindow *window = GTK_WINDOW (widget);
   GtkWindowPrivate *priv = window->priv;
 
   if (priv->hide_on_close)
     {
-      gtk_widget_hide (widget);
-      return GDK_EVENT_STOP;
+      gtk_widget_hide (GTK_WIDGET (window));
+      return TRUE;
     }
 
-  return GDK_EVENT_PROPAGATE;
+  return FALSE;
+}
+
+static gboolean
+gtk_window_emit_close_request (GtkWindow *window)
+{
+  gboolean handled;
+
+  g_signal_emit (window, window_signals[CLOSE_REQUEST], 0, &handled);
+
+  return handled;
 }
 
 static void
@@ -6012,12 +6023,6 @@ gtk_window_finalize (GObject *object)
       priv->keys_changed_handler = 0;
     }
 
-  if (priv->delete_event_handler)
-    {
-      g_source_remove (priv->delete_event_handler);
-      priv->delete_event_handler = 0;
-    }
-
 #ifdef GDK_WINDOWING_X11
   g_signal_handlers_disconnect_by_func (gtk_settings_get_for_display (priv->display),
                                         gtk_window_on_theme_variant_changed,
@@ -7667,7 +7672,12 @@ static gboolean
 gtk_window_event (GtkWidget *widget,
                   GdkEvent  *event)
 {
-  if (widget != gtk_get_event_target (event))
+  if (gdk_event_get_event_type (event) == GDK_DELETE)
+    {
+      if (gtk_window_emit_close_request (GTK_WINDOW (widget)))
+        return GDK_EVENT_STOP;
+    }
+  else if (widget != gtk_get_event_target (event))
     return gtk_window_handle_wm_event (GTK_WINDOW (widget), event, FALSE);
 
   return GDK_EVENT_PROPAGATE;
@@ -8228,8 +8238,7 @@ close_window_clicked (GtkMenuItem *menuitem,
 {
   GtkWindow *window = (GtkWindow *)user_data;
 
-  if (window->priv->delete_event_handler == 0)
-    send_delete_event (window);
+  gtk_window_close (window);
 }
 
 static void
diff --git a/gtk/gtkwindow.h b/gtk/gtkwindow.h
index 859191f..e196030 100644
--- a/gtk/gtkwindow.h
+++ b/gtk/gtkwindow.h
@@ -67,6 +67,7 @@ struct _GtkWindow
  *   mnemonics that are associated with window changes.
  * @enable_debugging: Class handler for the #GtkWindow::enable-debugging
  *   keybinding signal. Since: 3.14
+ * @close_request: Class handler for the #GtkWindow::close-request signal. Since: 3.94
  */
 struct _GtkWindowClass
 {
@@ -84,6 +85,7 @@ struct _GtkWindowClass
   void    (* keys_changed)     (GtkWindow *window);
   gboolean (* enable_debugging) (GtkWindow *window,
                                  gboolean   toggle);
+  gboolean (* close_request)    (GtkWindow *window);
 
   /*< private >*/
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]