[gtk+] file-chooser: make accept action sensitive only when has selection



commit 1e87b82048d73214b1af24a05cecadb06f875c94
Author: William Jon McCann <william jon mccann gmail com>
Date:   Thu Feb 20 15:44:47 2014 -0500

    file-chooser: make accept action sensitive only when has selection
    
    https://bugzilla.gnome.org/show_bug.cgi?id=547988

 gtk/gtkfilechooserdialog.c               |   42 +++++++++++++++++++++++++----
 gtk/resources/ui/gtkfilechooserdialog.ui |    1 +
 2 files changed, 37 insertions(+), 6 deletions(-)
---
diff --git a/gtk/gtkfilechooserdialog.c b/gtk/gtkfilechooserdialog.c
index e67e3e1..617b1fb 100644
--- a/gtk/gtkfilechooserdialog.c
+++ b/gtk/gtkfilechooserdialog.c
@@ -219,6 +219,8 @@ static void     file_chooser_widget_default_size_changed (GtkWidget            *
                                                          GtkFileChooserDialog *dialog);
 static void     file_chooser_widget_response_requested (GtkWidget            *widget,
                                                        GtkFileChooserDialog *dialog);
+static void     file_chooser_widget_selection_changed (GtkWidget            *widget,
+                                                       GtkFileChooserDialog *dialog);
 
 static void response_cb (GtkDialog *dialog,
                         gint       response_id);
@@ -254,6 +256,7 @@ gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
   gtk_widget_class_bind_template_callback (widget_class, file_chooser_widget_file_activated);
   gtk_widget_class_bind_template_callback (widget_class, file_chooser_widget_default_size_changed);
   gtk_widget_class_bind_template_callback (widget_class, file_chooser_widget_response_requested);
+  gtk_widget_class_bind_template_callback (widget_class, file_chooser_widget_selection_changed);
 }
 
 static void
@@ -270,7 +273,8 @@ gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
 }
 
 static GtkWidget *
-get_accept_action_widget (GtkDialog *dialog)
+get_accept_action_widget (GtkDialog *dialog,
+                          gboolean   sensitive_only)
 {
   gint response[] = {
     GTK_RESPONSE_ACCEPT,
@@ -284,8 +288,14 @@ get_accept_action_widget (GtkDialog *dialog)
   for (i = 0; i < G_N_ELEMENTS (response); i++)
     {
       widget = gtk_dialog_get_widget_for_response (dialog, response[i]);
-      if (widget && gtk_widget_is_sensitive (widget))
-       return widget;
+      if (widget)
+        {
+          if (!sensitive_only)
+            return widget;
+
+          if (gtk_widget_is_sensitive (widget))
+            return widget;
+        }
     }
 
   return NULL;
@@ -313,7 +323,7 @@ file_chooser_widget_file_activated (GtkFileChooser       *chooser,
   /* There probably isn't a default widget, so make things easier for the
    * programmer by looking for a reasonable button on our own.
    */
-  widget = get_accept_action_widget (GTK_DIALOG (dialog));
+  widget = get_accept_action_widget (GTK_DIALOG (dialog), TRUE);
   if (widget)
     gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
 }
@@ -384,6 +394,26 @@ file_chooser_widget_default_size_changed (GtkWidget            *widget,
 }
 
 static void
+file_chooser_widget_selection_changed (GtkWidget            *widget,
+                                       GtkFileChooserDialog *dialog)
+{
+  GtkWidget *button;
+  GSList *uris;
+  gboolean sensitive;
+
+  button = get_accept_action_widget (GTK_DIALOG (dialog), FALSE);
+  if (button == NULL)
+    return;
+
+  uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog->priv->widget));
+  sensitive = (uris != NULL);
+  gtk_widget_set_sensitive (button, sensitive);
+
+  if (uris)
+    g_slist_free_full (uris, g_free);
+}
+
+static void
 file_chooser_widget_response_requested (GtkWidget            *widget,
                                        GtkFileChooserDialog *dialog)
 {
@@ -397,7 +427,7 @@ file_chooser_widget_response_requested (GtkWidget            *widget,
   /* There probably isn't a default widget, so make things easier for the
    * programmer by looking for a reasonable button on our own.
    */
-  button = get_accept_action_widget (GTK_DIALOG (dialog));
+  button = get_accept_action_widget (GTK_DIALOG (dialog), TRUE);
   if (button)
     {
       gtk_widget_activate (button);
@@ -444,7 +474,7 @@ ensure_default_response (GtkFileChooserDialog *dialog)
 {
   GtkWidget *widget;
 
-  widget = get_accept_action_widget (GTK_DIALOG (dialog));
+  widget = get_accept_action_widget (GTK_DIALOG (dialog), TRUE);
   if (widget)
     gtk_widget_grab_default (widget); 
 }
diff --git a/gtk/resources/ui/gtkfilechooserdialog.ui b/gtk/resources/ui/gtkfilechooserdialog.ui
index 2b242f2..d9c7e73 100644
--- a/gtk/resources/ui/gtkfilechooserdialog.ui
+++ b/gtk/resources/ui/gtkfilechooserdialog.ui
@@ -40,6 +40,7 @@
             <signal name="default-size-changed" handler="file_chooser_widget_default_size_changed" 
swapped="no"/>
             <signal name="file-activated" handler="file_chooser_widget_file_activated" swapped="no"/>
             <signal name="response-requested" handler="file_chooser_widget_response_requested" swapped="no"/>
+            <signal name="selection-changed" handler="file_chooser_widget_selection_changed" swapped="no"/>
           </object>
           <packing>
             <property name="expand">True</property>


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