[gtk+/wip/matthiasc/kill-event-signals: 27/81] window: Add a hide-on-close property



commit 12e9ff99283e458f44b59e8f2741994c77f82c96
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Dec 31 10:42:10 2017 -0500

    window: Add a hide-on-close property
    
    This lets us avoid ::delete-event signal handlers for just
    this purpose.

 docs/reference/gtk/gtk4-sections.txt |    2 +
 gtk/gtkwindow.c                      |   79 ++++++++++++++++++++++++++++++++++
 gtk/gtkwindow.h                      |    5 ++
 3 files changed, 86 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index f183557..6550e54 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -4697,6 +4697,8 @@ gtk_window_set_modal
 gtk_window_set_default_size
 gtk_window_set_gravity
 gtk_window_get_gravity
+gtk_window_set_hide_on_close
+gtk_window_get_hide_on_close
 GtkWindowPosition
 gtk_window_set_position
 gtk_window_set_transient_for
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index 0553e6b..f120168 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -264,6 +264,7 @@ struct _GtkWindowPrivate
   guint    tiled                     : 1;
 
   guint    use_subsurface            : 1;
+  guint    hide_on_close             : 1;
 
   GdkWindowTypeHint type_hint;
 
@@ -309,6 +310,7 @@ enum {
   PROP_DEFAULT_WIDTH,
   PROP_DEFAULT_HEIGHT,
   PROP_DESTROY_WITH_PARENT,
+  PROP_HIDE_ON_CLOSE,
   PROP_ICON,
   PROP_ICON_NAME,
   PROP_DISPLAY,
@@ -415,6 +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_map_event      (GtkWidget         *widget,
                                            GdkEventAny       *event);
 static gint gtk_window_configure_event    (GtkWidget         *widget,
@@ -798,6 +802,7 @@ 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;
@@ -907,6 +912,14 @@ gtk_window_class_init (GtkWindowClass *klass)
                             FALSE,
                             GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
 
+  window_props[PROP_HIDE_ON_CLOSE] =
+      g_param_spec_boolean ("hide-on-close",
+                            P_("Hide on close"),
+                            P_("If this window should be hidden when the user clicks the close button"),
+                            FALSE,
+                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
+
+
   window_props[PROP_ICON] =
       g_param_spec_object ("icon",
                            P_("Icon"),
@@ -2042,6 +2055,9 @@ gtk_window_set_property (GObject      *object,
     case PROP_DESTROY_WITH_PARENT:
       gtk_window_set_destroy_with_parent (window, g_value_get_boolean (value));
       break;
+    case PROP_HIDE_ON_CLOSE:
+      gtk_window_set_hide_on_close (window, g_value_get_boolean (value));
+      break;
     case PROP_ICON:
       gtk_window_set_icon (window,
                            g_value_get_object (value));
@@ -2153,6 +2169,9 @@ gtk_window_get_property (GObject      *object,
     case PROP_DESTROY_WITH_PARENT:
       g_value_set_boolean (value, priv->destroy_with_parent);
       break;
+    case PROP_HIDE_ON_CLOSE:
+      g_value_set_boolean (value, priv->hide_on_close);
+      break;
     case PROP_ICON:
       g_value_set_object (value, gtk_window_get_icon (window));
       break;
@@ -4055,6 +4074,50 @@ gtk_window_get_destroy_with_parent (GtkWindow *window)
   return window->priv->destroy_with_parent;
 }
 
+/**
+ * gtk_window_set_hide_on_close:
+ * @window: a #GtkWindow
+ * @setting: whether to hide the window when it is closed
+ *
+ * If @setting is %TRUE, then clicking the close button on the window
+ * will not destroy it, but only hide it.
+ *
+ * Since: 3.94
+ */
+void
+gtk_window_set_hide_on_close (GtkWindow *window,
+                              gboolean   setting)
+{
+  GtkWindowPrivate *priv = window->priv;
+
+  g_return_if_fail (GTK_IS_WINDOW (window));
+
+  if (priv->hide_on_close == setting)
+    return;
+
+  priv->hide_on_close = setting;
+
+  g_object_notify_by_pspec (G_OBJECT (window), window_props[PROP_HIDE_ON_CLOSE]);
+}
+
+/**
+ * gtk_window_get_hide_on_close:
+ * @window: a #GtkWindow
+ *
+ * Returns whether the window will be hidden when the close button is clicked.
+ *
+ * Returns: %TRUE if the window will be hidden
+ *
+ * Since: 3.94
+ */
+gboolean
+gtk_window_get_hide_on_close (GtkWindow *window)
+{
+  g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
+
+  return window->priv->hide_on_close;
+}
+
 static GtkWindowGeometryInfo*
 gtk_window_get_geometry_info (GtkWindow *window,
                              gboolean   create)
@@ -5903,6 +5966,22 @@ gtk_window_destroy (GtkWidget *widget)
   GTK_WIDGET_CLASS (gtk_window_parent_class)->destroy (widget);
 }
 
+static gboolean
+gtk_window_delete_event (GtkWidget   *widget,
+                         GdkEventAny *event)
+{
+  GtkWindow *window = GTK_WINDOW (widget);
+  GtkWindowPrivate *priv = window->priv;
+
+  if (priv->hide_on_close)
+    {
+      gtk_widget_hide (widget);
+      return GDK_EVENT_STOP;
+    }
+
+  return GDK_EVENT_PROPAGATE;
+}
+
 static void
 gtk_window_finalize (GObject *object)
 {
diff --git a/gtk/gtkwindow.h b/gtk/gtkwindow.h
index 47f8263..34f619c 100644
--- a/gtk/gtkwindow.h
+++ b/gtk/gtkwindow.h
@@ -227,6 +227,11 @@ void       gtk_window_set_destroy_with_parent  (GtkWindow           *window,
                                                 gboolean             setting);
 GDK_AVAILABLE_IN_ALL
 gboolean   gtk_window_get_destroy_with_parent  (GtkWindow           *window);
+GDK_AVAILABLE_IN_3_94
+void       gtk_window_set_hide_on_close        (GtkWindow           *window,
+                                                gboolean             setting);
+GDK_AVAILABLE_IN_3_94
+gboolean   gtk_window_get_hide_on_close        (GtkWindow           *window);
 GDK_AVAILABLE_IN_ALL
 void       gtk_window_set_mnemonics_visible    (GtkWindow           *window,
                                                 gboolean             setting);


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