[gtk+] Make it possible to set use-header-bar from a setting



commit 106bcc7f5e3ff116f8a8c265627e20332e200615
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jan 17 17:17:23 2014 -0500

    Make it possible to set use-header-bar from a setting
    
    This commit introduces a private convenience API that derived
    dialogs can call in their instance init. This is necessary to
    make the setting work as intended in the face of 3rd party
    dialogs derived e.g. from GtkFileChooserDialog, which are
    created with g_object_new.

 gtk/Makefile.am        |    1 +
 gtk/gtkdialog.c        |   52 ++++++++++++++++++++++++++++++++++++++---------
 gtk/gtkdialogprivate.h |   36 +++++++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+), 10 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 6e71fba..1c4d2d8 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -467,6 +467,7 @@ gtk_private_h_sources =             \
        gtkcsstypedvalueprivate.h       \
        gtkcssvalueprivate.h    \
        gtkcustompaperunixdialog.h \
+       gtkdialogprivate.h      \
        gtkentryprivate.h       \
        gtkfilechooserdefault.h \
        gtkfilechooserembed.h   \
diff --git a/gtk/gtkdialog.c b/gtk/gtkdialog.c
index 5b2258b..90eeabc 100644
--- a/gtk/gtkdialog.c
+++ b/gtk/gtkdialog.c
@@ -175,7 +175,7 @@ struct _GtkDialogPrivate
   GtkWidget *headerbar;
   GtkWidget *action_area;
 
-  gboolean use_header_bar;
+  gint use_header_bar;
   gboolean constructed;
 };
 
@@ -235,11 +235,34 @@ G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
 
 static void
 set_use_header_bar (GtkDialog *dialog,
-                    gboolean   use_header_bar)
+                    gint       use_header_bar)
 {
   GtkDialogPrivate *priv = dialog->priv;
 
+  if (use_header_bar == -1)
+    return;
+
   priv->use_header_bar = use_header_bar;
+}
+
+/* A convenience helper for built-in dialogs */
+void
+gtk_dialog_set_use_header_bar_from_setting (GtkDialog *dialog)
+{
+  GtkDialogPrivate *priv = dialog->priv;
+
+  g_assert (!priv->constructed);
+
+  g_object_get (gtk_widget_get_settings (GTK_WIDGET (dialog)),
+                "gtk-dialogs-use-header", &priv->use_header_bar,
+                NULL);
+}
+
+static void
+apply_use_header_bar (GtkDialog *dialog)
+{
+  GtkDialogPrivate *priv = dialog->priv;
+
   gtk_widget_set_visible (priv->action_area, !priv->use_header_bar);
   gtk_widget_set_visible (priv->headerbar, priv->use_header_bar);
   if (!priv->use_header_bar)
@@ -257,7 +280,7 @@ gtk_dialog_set_property (GObject      *object,
   switch (prop_id)
     {
     case PROP_USE_HEADER_BAR:
-      set_use_header_bar (dialog, g_value_get_boolean (value));
+      set_use_header_bar (dialog, g_value_get_int (value));
       break;
 
     default:
@@ -278,7 +301,7 @@ gtk_dialog_get_property (GObject      *object,
   switch (prop_id)
     {
     case PROP_USE_HEADER_BAR:
-      g_value_set_boolean (value, priv->use_header_bar);
+      g_value_set_int (value, priv->use_header_bar);
       break;
 
     default:
@@ -412,8 +435,11 @@ gtk_dialog_constructor (GType                  type,
   priv = dialog->priv;
 
   priv->constructed = TRUE;
+  if (priv->use_header_bar == -1)
+    priv->use_header_bar = FALSE;
 
   add_action_widgets (dialog);
+  apply_use_header_bar (dialog);
 
   return object;
 }
@@ -528,15 +554,18 @@ gtk_dialog_class_init (GtkDialogClass *class)
    * %TRUE if the dialog uses a #GtkHeaderBar for action buttons
    * instead of the action-area.
    *
+   * For technical reasons, this property is declared as an integer
+   * property, but you should only set it to %TRUE or %FALSE.
+   *
    * Since: 3.12
    */
   g_object_class_install_property (gobject_class,
                                    PROP_USE_HEADER_BAR,
-                                   g_param_spec_boolean ("use-header-bar",
-                                                         P_("Use Header Bar"),
-                                                         P_("Use Header Bar for actions."),
-                                                         FALSE,
-                                                         GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
+                                   g_param_spec_int ("use-header-bar",
+                                                     P_("Use Header Bar"),
+                                                     P_("Use Header Bar for actions."),
+                                                     -1, 1, -1,
+                                                     GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
   binding_set = gtk_binding_set_by_class (class);
   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
@@ -585,6 +614,8 @@ gtk_dialog_init (GtkDialog *dialog)
 {
   dialog->priv = gtk_dialog_get_instance_private (dialog);
 
+  dialog->priv->use_header_bar = -1;
+
   gtk_widget_init_template (GTK_WIDGET (dialog));
 
   update_spacings (dialog);
@@ -1448,11 +1479,12 @@ gtk_dialog_set_alternative_button_order (GtkDialog *dialog,
                                         gint       first_response_id,
                                         ...)
 {
+  GtkDialogPrivate *priv = dialog->priv;
   va_list args;
 
   g_return_if_fail (GTK_IS_DIALOG (dialog));
 
-  if (dialog->priv->use_header_bar)
+  if (priv->constructed && priv->use_header_bar)
     return;
 
   if (!gtk_alt_dialog_button_order ())
diff --git a/gtk/gtkdialogprivate.h b/gtk/gtkdialogprivate.h
new file mode 100644
index 0000000..626d188
--- /dev/null
+++ b/gtk/gtkdialogprivate.h
@@ -0,0 +1,36 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+#ifndef __GTK_DIALOG_PRIVATE_H__
+#define __GTK_DIALOG_PRIVATE_H__
+
+#include "gtkdialog.h"
+
+G_BEGIN_DECLS
+
+void gtk_dialog_set_use_header_bar_from_setting (GtkDialog *dialog);
+
+G_END_DECLS
+
+#endif /* __GTK_DIALOG_PRIVATE_H__ */


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