From 017025c1a7fc37f442b1556de7573ea6eeb35e2a Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 3 Jul 2015 22:45:15 -0400 Subject: [PATCH] file chooser dialog: Make action changes work We were not listening for action changes to update the save entry. --- gtk/gtkfilechooserdialog.c | 156 +++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 74 deletions(-) diff --git a/gtk/gtkfilechooserdialog.c b/gtk/gtkfilechooserdialog.c index 9fb53e9be7..5b1dfe9752 100644 --- a/gtk/gtkfilechooserdialog.c +++ b/gtk/gtkfilechooserdialog.c @@ -138,7 +138,7 @@ * * - To save a file for the first time. Use #GTK_FILE_CHOOSER_ACTION_SAVE, * and suggest a name such as “Untitled” with gtk_file_chooser_set_current_name(). - * + * * - To save a file under a different name. Use #GTK_FILE_CHOOSER_ACTION_SAVE, * and set the existing filename with gtk_file_chooser_set_filename(). * @@ -213,32 +213,36 @@ struct _GtkFileChooserDialogPrivate }; static void gtk_file_chooser_dialog_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec); + guint prop_id, + const GValue *value, + GParamSpec *pspec); static void gtk_file_chooser_dialog_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec); + guint prop_id, + GValue *value, + GParamSpec *pspec); +static void gtk_file_chooser_dialog_notify (GObject *object, + GParamSpec *pspec); static void gtk_file_chooser_dialog_map (GtkWidget *widget); static void gtk_file_chooser_dialog_unmap (GtkWidget *widget); static void file_chooser_widget_file_activated (GtkFileChooser *chooser, - GtkFileChooserDialog *dialog); + GtkFileChooserDialog *dialog); static void file_chooser_widget_default_size_changed (GtkWidget *widget, - GtkFileChooserDialog *dialog); + GtkFileChooserDialog *dialog); static void file_chooser_widget_response_requested (GtkWidget *widget, - GtkFileChooserDialog *dialog); + GtkFileChooserDialog *dialog); static void file_chooser_widget_selection_changed (GtkWidget *widget, - GtkFileChooserDialog *dialog); + GtkFileChooserDialog *dialog); static void response_cb (GtkDialog *dialog, - gint response_id); + gint response_id); + +static void setup_save_entry (GtkFileChooserDialog *dialog); G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDialog, gtk_file_chooser_dialog, GTK_TYPE_DIALOG, G_ADD_PRIVATE (GtkFileChooserDialog) - G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, - _gtk_file_chooser_delegate_iface_init)) + G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, + _gtk_file_chooser_delegate_iface_init)) static void gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class) @@ -248,9 +252,10 @@ gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class) gobject_class->set_property = gtk_file_chooser_dialog_set_property; gobject_class->get_property = gtk_file_chooser_dialog_get_property; + gobject_class->notify = gtk_file_chooser_dialog_notify; - widget_class->map = gtk_file_chooser_dialog_map; - widget_class->unmap = gtk_file_chooser_dialog_unmap; + widget_class->map = gtk_file_chooser_dialog_map; + widget_class->unmap = gtk_file_chooser_dialog_unmap; gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FILE_CHOOSER); @@ -259,7 +264,7 @@ gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class) /* Bind class to template */ gtk_widget_class_set_template_from_resource (widget_class, - "/org/gtk/libgtk/ui/gtkfilechooserdialog.ui"); + "/org/gtk/libgtk/ui/gtkfilechooserdialog.ui"); gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserDialog, widget); gtk_widget_class_bind_template_child_private (widget_class, GtkFileChooserDialog, buttons); @@ -280,7 +285,7 @@ gtk_file_chooser_dialog_init (GtkFileChooserDialog *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)); + GTK_FILE_CHOOSER (dialog->priv->widget)); } static GtkWidget * @@ -313,18 +318,18 @@ get_accept_action_widget (GtkDialog *dialog, } static gboolean -is_stock_accept_response_id (int response_id) +is_stock_accept_response_id (gint response_id) { - return (response_id == GTK_RESPONSE_ACCEPT - || response_id == GTK_RESPONSE_OK - || response_id == GTK_RESPONSE_YES - || response_id == GTK_RESPONSE_APPLY); + return (response_id == GTK_RESPONSE_ACCEPT || + response_id == GTK_RESPONSE_OK || + response_id == GTK_RESPONSE_YES || + response_id == GTK_RESPONSE_APPLY); } /* Callback used when the user activates a file in the file chooser widget */ static void file_chooser_widget_file_activated (GtkFileChooser *chooser, - GtkFileChooserDialog *dialog) + GtkFileChooserDialog *dialog) { GtkWidget *widget; @@ -336,7 +341,7 @@ file_chooser_widget_file_activated (GtkFileChooser *chooser, */ widget = get_accept_action_widget (GTK_DIALOG (dialog), TRUE); if (widget) - gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */ + gtk_widget_activate (widget); } #if 0 @@ -360,7 +365,7 @@ load_position (int *out_xpos, int *out_ypos) static void file_chooser_widget_default_size_changed (GtkWidget *widget, - GtkFileChooserDialog *dialog) + GtkFileChooserDialog *dialog) { GtkFileChooserDialogPrivate *priv; gint default_width, default_height; @@ -373,16 +378,15 @@ file_chooser_widget_default_size_changed (GtkWidget *widget, if (gtk_widget_is_drawable (widget)) { - /* Force a size request of everything before we start. This will make sure - * that widget->requisition is meaningful. */ - gtk_widget_get_preferred_size (GTK_WIDGET (dialog), - &req, NULL); - gtk_widget_get_preferred_size (widget, - &widget_req, NULL); + /* Force a size request of everything before we start. This will make sure + * that widget->requisition is meaningful. + */ + gtk_widget_get_preferred_size (GTK_WIDGET (dialog), &req, NULL); + gtk_widget_get_preferred_size (widget, &widget_req, NULL); } _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget), - &default_width, &default_height); + &default_width, &default_height); gtk_window_resize (GTK_WINDOW (dialog), default_width, default_height); @@ -396,10 +400,10 @@ file_chooser_widget_default_size_changed (GtkWidget *widget, */ load_position (&xpos, &ypos); if (xpos >= 0 && ypos >= 0) - { - gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE); - gtk_window_move (GTK_WINDOW (dialog), xpos, ypos); - } + { + gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE); + gtk_window_move (GTK_WINDOW (dialog), xpos, ypos); + } #endif } } @@ -426,7 +430,7 @@ file_chooser_widget_selection_changed (GtkWidget *widget, static void file_chooser_widget_response_requested (GtkWidget *widget, - GtkFileChooserDialog *dialog) + GtkFileChooserDialog *dialog) { GtkWidget *button; @@ -449,37 +453,43 @@ file_chooser_widget_response_requested (GtkWidget *widget, } static void -gtk_file_chooser_dialog_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) +gtk_file_chooser_dialog_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) { GtkFileChooserDialogPrivate *priv; priv = gtk_file_chooser_dialog_get_instance_private (GTK_FILE_CHOOSER_DIALOG (object)); - switch (prop_id) - { - default: - g_object_set_property (G_OBJECT (priv->widget), pspec->name, value); - break; - } + g_object_set_property (G_OBJECT (priv->widget), pspec->name, value); } static void -gtk_file_chooser_dialog_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) +gtk_file_chooser_dialog_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) { GtkFileChooserDialogPrivate *priv; - + priv = gtk_file_chooser_dialog_get_instance_private (GTK_FILE_CHOOSER_DIALOG (object)); g_object_get_property (G_OBJECT (priv->widget), pspec->name, value); } +static void +gtk_file_chooser_dialog_notify (GObject *object, + GParamSpec *pspec) +{ + if (strcmp (pspec->name, "action") == 0) + setup_save_entry (GTK_FILE_CHOOSER_DIALOG (object)); + + if (G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->notify) + G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->notify (object, pspec); +} + static void add_button (GtkWidget *button, gpointer data) { @@ -583,10 +593,9 @@ ensure_default_response (GtkFileChooserDialog *dialog) widget = get_accept_action_widget (GTK_DIALOG (dialog), TRUE); if (widget) - gtk_widget_grab_default (widget); + gtk_widget_grab_default (widget); } -/* GtkWidget::map handler */ static void gtk_file_chooser_dialog_map (GtkWidget *widget) { @@ -620,7 +629,6 @@ save_dialog_geometry (GtkFileChooserDialog *dialog) g_settings_set (settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", width, height); } -/* GtkWidget::unmap handler */ static void gtk_file_chooser_dialog_unmap (GtkWidget *widget) { @@ -638,16 +646,16 @@ gtk_file_chooser_dialog_unmap (GtkWidget *widget) */ static void response_cb (GtkDialog *dialog, - gint response_id) + gint response_id) { GtkFileChooserDialogPrivate *priv; priv = gtk_file_chooser_dialog_get_instance_private (GTK_FILE_CHOOSER_DIALOG (dialog)); /* Act only on response IDs we recognize */ - if (is_stock_accept_response_id (response_id) - && !priv->response_requested - && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget))) + if (is_stock_accept_response_id (response_id) && + !priv->response_requested && + !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget))) { g_signal_stop_emission_by_name (dialog, "response"); } @@ -657,19 +665,19 @@ response_cb (GtkDialog *dialog, static GtkWidget * gtk_file_chooser_dialog_new_valist (const gchar *title, - GtkWindow *parent, - GtkFileChooserAction action, - const gchar *first_button_text, - va_list varargs) + GtkWindow *parent, + GtkFileChooserAction action, + const gchar *first_button_text, + va_list varargs) { GtkWidget *result; const char *button_text = first_button_text; gint response_id; result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG, - "title", title, - "action", action, - NULL); + "title", title, + "action", action, + NULL); if (parent) gtk_window_set_transient_for (GTK_WINDOW (result), parent); @@ -700,19 +708,19 @@ gtk_file_chooser_dialog_new_valist (const gchar *title, * Since: 2.4 **/ GtkWidget * -gtk_file_chooser_dialog_new (const gchar *title, - GtkWindow *parent, - GtkFileChooserAction action, - const gchar *first_button_text, - ...) +gtk_file_chooser_dialog_new (const gchar *title, + GtkWindow *parent, + GtkFileChooserAction action, + const gchar *first_button_text, + ...) { GtkWidget *result; va_list varargs; va_start (varargs, first_button_text); result = gtk_file_chooser_dialog_new_valist (title, parent, action, - first_button_text, - varargs); + first_button_text, + varargs); va_end (varargs); return result;