[gtk+/dialogs] Make the gtk-dialogs-use-header setting work reliably



commit b9aaa19263f7675bbb4443447437e01b44915bc1
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jan 16 22:35:47 2014 -0500

    Make the gtk-dialogs-use-header setting work reliably
    
    We need to invest a little more effort to get the intended
    behaviour for cases where 3rd party dialogs are derived
    from e.g. GtkFileChooserDialog.

 gtk/Makefile.am                |    1 +
 gtk/gtkappchooserdialog.c      |   12 +--------
 gtk/gtkcolorchooserdialog.c    |    8 +----
 gtk/gtkcustompaperunixdialog.c |    8 ++----
 gtk/gtkdialog.c                |   50 ++++++++++++++++++++++++++++++---------
 gtk/gtkdialogprivate.h         |   36 ++++++++++++++++++++++++++++
 gtk/gtkfilechooserdialog.c     |    7 +----
 gtk/gtkfontchooserdialog.c     |    7 +----
 gtk/gtkpagesetupunixdialog.c   |    7 +----
 gtk/gtkprintunixdialog.c       |    7 +----
 gtk/gtkrecentchooserdialog.c   |    8 +----
 11 files changed, 92 insertions(+), 59 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/gtkappchooserdialog.c b/gtk/gtkappchooserdialog.c
index a26374a..f7264f9 100644
--- a/gtk/gtkappchooserdialog.c
+++ b/gtk/gtkappchooserdialog.c
@@ -52,6 +52,7 @@
 #include "gtkbutton.h"
 #include "gtkmenuitem.h"
 #include "gtkheaderbar.h"
+#include "gtkdialogprivate.h"
 
 #include <string.h>
 #include <glib/gi18n-lib.h>
@@ -619,6 +620,7 @@ gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
   self->priv = gtk_app_chooser_dialog_get_instance_private (self);
 
   gtk_widget_init_template (GTK_WIDGET (self));
+  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (self));
   gtk_dialog_add_buttons (GTK_DIALOG (self),
                           _("_Cancel"), GTK_RESPONSE_CANCEL,
                           _("_Select"), GTK_RESPONSE_OK,
@@ -673,15 +675,10 @@ gtk_app_chooser_dialog_new (GtkWindow      *parent,
                             GFile          *file)
 {
   GtkWidget *retval;
-  gboolean use_header;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
   retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
-                         "use-header-bar", use_header,
                          "gfile", file,
                          NULL);
 
@@ -709,15 +706,10 @@ gtk_app_chooser_dialog_new_for_content_type (GtkWindow      *parent,
                                              const gchar    *content_type)
 {
   GtkWidget *retval;
-  gboolean use_header;
 
   g_return_val_if_fail (content_type != NULL, NULL);
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
   retval = g_object_new (GTK_TYPE_APP_CHOOSER_DIALOG,
-                         "use-header-bar", use_header,
                          "content-type", content_type,
                          NULL);
 
diff --git a/gtk/gtkcolorchooserdialog.c b/gtk/gtkcolorchooserdialog.c
index d2543c0..f9316b1 100644
--- a/gtk/gtkcolorchooserdialog.c
+++ b/gtk/gtkcolorchooserdialog.c
@@ -18,6 +18,7 @@
 #include "config.h"
 
 #include "gtkdialog.h"
+#include "gtkdialogprivate.h"
 #include "gtkbutton.h"
 #include "gtkbox.h"
 #include "gtkprivate.h"
@@ -104,6 +105,7 @@ gtk_color_chooser_dialog_init (GtkColorChooserDialog *cc)
   cc->priv = gtk_color_chooser_dialog_get_instance_private (cc);
 
   gtk_widget_init_template (GTK_WIDGET (cc));
+  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (cc));
   gtk_dialog_add_buttons (GTK_DIALOG (cc),
                           _("_Cancel"), GTK_RESPONSE_CANCEL,
                           _("_Select"), GTK_RESPONSE_OK,
@@ -277,14 +279,8 @@ gtk_color_chooser_dialog_new (const gchar *title,
                               GtkWindow   *parent)
 {
   GtkColorChooserDialog *dialog;
-  gboolean use_header;
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
-  g_print ("use header: %d\n", use_header);
   dialog = g_object_new (GTK_TYPE_COLOR_CHOOSER_DIALOG,
-                         "use-header-bar", use_header,
                          "title", title,
                          "transient-for", parent,
                          NULL);
diff --git a/gtk/gtkcustompaperunixdialog.c b/gtk/gtkcustompaperunixdialog.c
index 579727e..ec2c117 100644
--- a/gtk/gtkcustompaperunixdialog.c
+++ b/gtk/gtkcustompaperunixdialog.c
@@ -37,6 +37,7 @@
 #include "gtkcustompaperunixdialog.h"
 #include "gtkprintbackend.h"
 #include "gtkprintutils.h"
+#include "gtkdialogprivate.h"
 
 #define LEGACY_CUSTOM_PAPER_FILENAME ".gtk-custom-papers"
 #define CUSTOM_PAPER_FILENAME "custom-papers"
@@ -295,6 +296,8 @@ gtk_custom_paper_unix_dialog_init (GtkCustomPaperUnixDialog *dialog)
   dialog->priv = gtk_custom_paper_unix_dialog_get_instance_private (dialog);
   priv = dialog->priv;
 
+  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (dialog));
+
   priv->print_backends = NULL;
 
   priv->request_details_printer = NULL;
@@ -404,16 +407,11 @@ _gtk_custom_paper_unix_dialog_new (GtkWindow   *parent,
                                    const gchar *title)
 {
   GtkWidget *result;
-  gboolean use_header;
 
   if (title == NULL)
     title = _("Manage Custom Sizes");
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
   result = g_object_new (GTK_TYPE_CUSTOM_PAPER_UNIX_DIALOG,
-                         "use-header-bar", use_header,
                          "title", title,
                          "transient-for", parent,
                          "modal", parent != NULL,
diff --git a/gtk/gtkdialog.c b/gtk/gtkdialog.c
index ebdb51d..41c2a83 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;
   GList *action_widgets;
   gint default_response;
@@ -235,13 +235,38 @@ G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_dialog_buildable_interface_init))
 
-static void
+void
 set_use_header_bar (GtkDialog *dialog,
                     gboolean   use_header_bar)
 {
   GtkDialogPrivate *priv = dialog->priv;
 
+  if (use_header_bar == -1)
+    return;
+
   priv->use_header_bar = use_header_bar;
+}
+
+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;
+
+  if (priv->use_header_bar == -1)
+    priv->use_header_bar = FALSE;
+
   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)
@@ -259,7 +284,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:
@@ -280,7 +305,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:
@@ -411,8 +436,8 @@ gtk_dialog_constructor (GType                  type,
 
   priv->constructed = TRUE;
 
-  if (priv->action_widgets != NULL)
-    add_action_widgets (dialog);
+  apply_use_header_bar (dialog);
+  add_action_widgets (dialog);
 
   return object;
 }
@@ -530,12 +555,12 @@ gtk_dialog_class_init (GtkDialogClass *class)
    * 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));
+                                   PROP_USE_HEADER_BAR,
+                                   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);
@@ -584,6 +609,7 @@ gtk_dialog_init (GtkDialog *dialog)
 {
   dialog->priv = gtk_dialog_get_instance_private (dialog);
 
+  dialog->priv->use_header_bar = -1;
   dialog->priv->default_response = GTK_RESPONSE_NONE;
 
   gtk_widget_init_template (GTK_WIDGET (dialog));
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__ */
diff --git a/gtk/gtkfilechooserdialog.c b/gtk/gtkfilechooserdialog.c
index 84b5c1e..533c7b9 100644
--- a/gtk/gtkfilechooserdialog.c
+++ b/gtk/gtkfilechooserdialog.c
@@ -30,6 +30,7 @@
 #include "gtktypebuiltins.h"
 #include "gtkintl.h"
 #include "gtksettings.h"
+#include "gtkdialogprivate.h"
 
 #include <stdarg.h>
 
@@ -263,6 +264,7 @@ gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
   dialog->priv->response_requested = FALSE;
 
   gtk_widget_init_template (GTK_WIDGET (dialog));
+  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (dialog));
 
   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (dialog),
                                  GTK_FILE_CHOOSER (dialog->priv->widget));
@@ -525,15 +527,10 @@ gtk_file_chooser_dialog_new_valist (const gchar          *title,
   GtkWidget *result;
   const char *button_text = first_button_text;
   gint response_id;
-  gboolean use_header;
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
   result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
                         "title", title,
                         "action", action,
-                         "use-header-bar", use_header,
                         NULL);
 
   if (parent)
diff --git a/gtk/gtkfontchooserdialog.c b/gtk/gtkfontchooserdialog.c
index fa27d18..af962e4 100644
--- a/gtk/gtkfontchooserdialog.c
+++ b/gtk/gtkfontchooserdialog.c
@@ -35,6 +35,7 @@
 #include "gtkprivate.h"
 #include "gtkwidget.h"
 #include "gtksettings.h"
+#include "gtkdialogprivate.h"
 
 struct _GtkFontChooserDialogPrivate
 {
@@ -150,6 +151,7 @@ gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
   priv = fontchooserdiag->priv;
 
   gtk_widget_init_template (GTK_WIDGET (fontchooserdiag));
+  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (fontchooserdiag));
   gtk_dialog_add_buttons (GTK_DIALOG (fontchooserdiag),
                           _("_Cancel"), GTK_RESPONSE_CANCEL,
                           _("_Select"), GTK_RESPONSE_OK,
@@ -184,13 +186,8 @@ gtk_font_chooser_dialog_new (const gchar *title,
                              GtkWindow   *parent)
 {
   GtkFontChooserDialog *dialog;
-  gboolean use_header;
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
   dialog = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG,
-                         "use-header-bar", use_header,
                          "title", title,
                          "transient-for", parent,
                          NULL);
diff --git a/gtk/gtkpagesetupunixdialog.c b/gtk/gtkpagesetupunixdialog.c
index 9133206..5601aef 100644
--- a/gtk/gtkpagesetupunixdialog.c
+++ b/gtk/gtkpagesetupunixdialog.c
@@ -43,6 +43,7 @@
 #include "gtkprintbackend.h"
 #include "gtkpapersize.h"
 #include "gtkprintutils.h"
+#include "gtkdialogprivate.h"
 
 /**
  * SECTION:gtkpagesetupunixdialog
@@ -190,6 +191,7 @@ gtk_page_setup_unix_dialog_init (GtkPageSetupUnixDialog *dialog)
   priv->print_backends = NULL;
 
   gtk_widget_init_template (GTK_WIDGET (dialog));
+  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (dialog));
   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                           _("_Cancel"), GTK_RESPONSE_CANCEL,
                           _("_Apply"), GTK_RESPONSE_OK,
@@ -897,16 +899,11 @@ gtk_page_setup_unix_dialog_new (const gchar *title,
                                 GtkWindow   *parent)
 {
   GtkWidget *result;
-  gboolean use_header;
 
   if (title == NULL)
     title = _("Page Setup");
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
   result = g_object_new (GTK_TYPE_PAGE_SETUP_UNIX_DIALOG,
-                         "use-header-bar", use_header,
                          "title", title,
                          NULL);
 
diff --git a/gtk/gtkprintunixdialog.c b/gtk/gtkprintunixdialog.c
index 7dd54f5..0bb6b34 100644
--- a/gtk/gtkprintunixdialog.c
+++ b/gtk/gtkprintunixdialog.c
@@ -55,6 +55,7 @@
 #include "gtkintl.h"
 #include "gtkprivate.h"
 #include "gtktypebuiltins.h"
+#include "gtkdialogprivate.h"
 
 
 /**
@@ -737,6 +738,7 @@ gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
   priv->has_selection = FALSE;
 
   gtk_widget_init_template (GTK_WIDGET (dialog));
+  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (dialog));
   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                           _("Pre_view"), GTK_RESPONSE_APPLY,
                           _("_Cancel"), GTK_RESPONSE_CANCEL,
@@ -3300,13 +3302,8 @@ gtk_print_unix_dialog_new (const gchar *title,
                            GtkWindow   *parent)
 {
   GtkWidget *result;
-  gboolean use_header;
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
   result = g_object_new (GTK_TYPE_PRINT_UNIX_DIALOG,
-                         "use-header-bar", use_header,
                          "transient-for", parent,
                          "title", title ? title : _("Print"),
                          NULL);
diff --git a/gtk/gtkrecentchooserdialog.c b/gtk/gtkrecentchooserdialog.c
index 4cdb558..181bff5 100644
--- a/gtk/gtkrecentchooserdialog.c
+++ b/gtk/gtkrecentchooserdialog.c
@@ -24,6 +24,7 @@
 #include "gtkrecentmanager.h"
 #include "gtktypebuiltins.h"
 #include "gtksettings.h"
+#include "gtkdialogprivate.h"
 
 #include <stdarg.h>
 
@@ -128,6 +129,7 @@ gtk_recent_chooser_dialog_init (GtkRecentChooserDialog *dialog)
 
   priv = gtk_recent_chooser_dialog_get_instance_private (dialog);
   dialog->priv = priv;
+  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (dialog));
 
   content_area = gtk_dialog_get_content_area (rc_dialog);
 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
@@ -137,7 +139,6 @@ G_GNUC_END_IGNORE_DEPRECATIONS
   gtk_container_set_border_width (GTK_CONTAINER (rc_dialog), 5);
   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
-
 }
 
 /* we intercept the GtkRecentChooser::item_activated signal and try to
@@ -279,13 +280,8 @@ gtk_recent_chooser_dialog_new_valist (const gchar      *title,
   GtkWidget *result;
   const char *button_text = first_button_text;
   gint response_id;
-  gboolean use_header;
 
-  g_object_get (gtk_settings_get_default (),
-                "gtk-dialogs-use-header", &use_header,
-                NULL);
   result = g_object_new (GTK_TYPE_RECENT_CHOOSER_DIALOG,
-                         "use-header-bar", use_header,
                          "title", title,
                          "recent-manager", manager,
                          NULL);


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