New treeviewized GtkFileSelection and GTK_SELECTION_MULTIPLE



Since the port to using the treeview in GtkFileSelection, it's impossible
to have a file selection dialog that lets you select multiple files. The
attached patch fixes that, but things could be cleaner if there was a 
gtk_file_selection_set_selection_mode function or some such and an API which
lets you get at all the selections so that the internals are all abstracted
out. Behaviorwise it can work like it does now with this patch: you can
selected multiple items, and the first selected item is what is placed in
the files selection text box.

Proposed API additions:

1) gtk_file_selection_set_selection_mode (): simply a wrapper around
   gtk_tree_selection_set_mode () for the fs->file_list

2) gtk_file_selection_get_selections (): Returns an array of filenames,
   freed by the caller.

Also, it'd be nice if the GtkTreeSelection API could be changed so the
foreach function returns a boolean, to stop the foreach early.

-Yosh
Index: gtkfilesel.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkfilesel.c,v
retrieving revision 1.97
diff -u -p -r1.97 gtkfilesel.c
--- gtkfilesel.c	2002/01/29 20:53:16	1.97
+++ gtkfilesel.c	2002/01/31 00:27:41
@@ -1872,23 +1872,67 @@ gtk_file_selection_file_activate (GtkTre
 }
 
 static void
+file_changed_selected_foreach (GtkTreeModel *model,
+			       GtkTreePath  *path,
+			       GtkTreeIter  *iter,
+			       gpointer      data)
+{
+  gboolean *selected = data;
+  GtkTreeIter *sel_iter;
+
+  if (! *selected)
+    {
+      *selected = TRUE;
+
+      sel_iter = gtk_tree_iter_copy (iter);
+      g_object_set_data (G_OBJECT (model),
+			 "gtk-file-selection-selected-iter",
+			 sel_iter);
+      
+    }
+}
+
+static void
 gtk_file_selection_file_changed (GtkTreeSelection *selection,
 				 gpointer          user_data)
 {
   GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
   GtkTreeModel *model;
-  GtkTreeIter iter;
+  GtkTreeIter iter, *sel_iter;
+  gboolean selected = FALSE;
   
-  if (gtk_tree_selection_get_selected (selection, &model, &iter))
+  if (gtk_tree_selection_get_mode (selection) == GTK_SELECTION_MULTIPLE)
     {
+      gtk_tree_selection_selected_foreach (selection,
+					   file_changed_selected_foreach,
+					   &selected);
+
+      if (selected)
+	{
+	  model = gtk_tree_view_get_model (GTK_TREE_VIEW (fs->file_list));
+	  sel_iter = g_object_get_data (G_OBJECT (model),
+					"gtk-file-selection-selected-iter");
+	}
+    }
+  else
+    {
+      sel_iter = &iter;
+      selected = gtk_tree_selection_get_selected (selection, &model, sel_iter);
+    }
+
+  if (selected)
+    {
       gchar *filename;
       
-      gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
+      gtk_tree_model_get (model, sel_iter, FILE_COLUMN, &filename, -1);
       filename = get_real_filename (filename);
 
       gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
 
       g_free (filename);
+
+      if (sel_iter != &iter)
+	gtk_tree_iter_free (sel_iter);
     }
 }
 


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