Re: New treeviewized GtkFileSelection and GTK_SELECTION_MULTIPLE



On Thu, Feb 14, 2002 at 10:09:54PM -0800, Manish Singh wrote:
> Ok, that sounds good. I should have a revised patch tomorrow or so.

And here it is. The diff code is still the same though.

-Yosh
Index: gtkfilesel.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkfilesel.c,v
retrieving revision 1.101
diff -u -p -r1.101 gtkfilesel.c
--- gtkfilesel.c	2002/02/11 21:17:11	1.101
+++ gtkfilesel.c	2002/02/15 23:49:04
@@ -396,11 +396,18 @@ static void gtk_file_selection_abort    
 static void gtk_file_selection_update_history_menu (GtkFileSelection       *fs,
 						    gchar                  *current_dir);
 
-static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
+static void gtk_file_selection_create_dir  (GtkWidget *widget, gpointer data);
 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
 
+static void free_selected_names (GPtrArray *names);
 
+#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
+#define compare_filenames(a, b) strcmp(a, b)
+#else
+#define compare_filenames(a, b) g_strcasecmp(a, b)
+#endif
+
 
 static GtkWindowClass *parent_class = NULL;
 
@@ -1199,7 +1206,19 @@ gtk_file_selection_destroy (GtkObject *o
       cmpl_free_state (filesel->cmpl_state);
       filesel->cmpl_state = NULL;
     }
-  
+ 
+  if (filesel->selected_names)
+    {
+      free_selected_names (filesel->selected_names);
+      filesel->selected_names = NULL;
+    } 
+
+  if (filesel->last_selected)
+    {
+      g_free (filesel->last_selected);
+      filesel->last_selected = NULL;
+    }
+
   GTK_OBJECT_CLASS (parent_class)->destroy (object);
 }
 
@@ -1815,7 +1834,8 @@ gtk_file_selection_update_history_menu (
 }
 
 static gchar *
-get_real_filename (gchar *filename)
+get_real_filename (gchar    *filename,
+                   gboolean  free_old)
 {
 #ifdef G_WITH_CYGWIN
   /* Check to see if the selection was a drive selector */
@@ -1823,7 +1843,10 @@ get_real_filename (gchar *filename)
     {
       /* It is... map it to a CYGWIN32 drive */
       gchar *temp_filename = g_strdup_printf ("//%c/", tolower (filename[0]));
-      g_free(filename);
+
+      if (free_old)
+	g_free (filename);
+
       return temp_filename;
     }
 #else
@@ -1844,7 +1867,7 @@ gtk_file_selection_file_activate (GtkTre
   
   gtk_tree_model_get_iter (model, &iter, path);
   gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
-  filename = get_real_filename (filename);
+  filename = get_real_filename (filename, TRUE);
 
   gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
   gtk_button_clicked (GTK_BUTTON (fs->ok_button));
@@ -1853,27 +1876,6 @@ gtk_file_selection_file_activate (GtkTre
 }
 
 static void
-gtk_file_selection_file_changed (GtkTreeSelection *selection,
-				 gpointer          user_data)
-{
-  GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
-  GtkTreeModel *model;
-  GtkTreeIter iter;
-  
-  if (gtk_tree_selection_get_selected (selection, &model, &iter))
-    {
-      gchar *filename;
-      
-      gtk_tree_model_get (model, &iter, FILE_COLUMN, &filename, -1);
-      filename = get_real_filename (filename);
-
-      gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
-
-      g_free (filename);
-    }
-}
-
-static void
 gtk_file_selection_dir_activate (GtkTreeView       *tree_view,
 				 GtkTreePath       *path,
 				 GtkTreeViewColumn *column,
@@ -2091,6 +2093,203 @@ gtk_file_selection_abort (GtkFileSelecti
     gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
 }
 
+void
+gtk_file_selection_set_select_multiple (GtkFileSelection *filesel,
+					gboolean          select_multiple)
+{
+  GtkTreeSelection *sel;
+  GtkSelectionMode mode;
+
+  g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
+
+  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
+
+  mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_SINGLE;
+
+  gtk_tree_selection_set_mode (sel, mode);
+}
+
+gboolean
+gtk_file_selection_get_select_multiple (GtkFileSelection *filesel)
+{
+  GtkTreeSelection *sel;
+
+  g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), FALSE);
+
+  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (filesel->file_list));
+  return (gtk_tree_selection_get_mode (sel) == GTK_SELECTION_MULTIPLE);
+}
+
+static void
+multiple_changed_foreach (GtkTreeModel *model,
+			  GtkTreePath  *path,
+			  GtkTreeIter  *iter,
+			  gpointer      data)
+{
+  GPtrArray *names = data;
+  gchar *filename;
+
+  gtk_tree_model_get (model, iter, FILE_COLUMN, &filename, -1);
+
+  g_ptr_array_add (names, filename);
+}
+
+static void
+free_selected_names (GPtrArray *names)
+{
+  gchar **vector;
+
+  g_ptr_array_add (names, NULL);
+  vector = (gchar **) g_ptr_array_free (names, FALSE);
+  g_strfreev (vector);
+}
+
+static void
+gtk_file_selection_file_changed (GtkTreeSelection *selection,
+				 gpointer          user_data)
+{
+  GtkFileSelection *fs = GTK_FILE_SELECTION (user_data);
+  GPtrArray *new_names;
+  gchar *filename;
+  gint i, index = 0;
+
+  new_names = g_ptr_array_sized_new (8);
+
+  gtk_tree_selection_selected_foreach (selection,
+				       multiple_changed_foreach,
+				       new_names);
+  /* nothing selected */
+  if (new_names->len == 0)
+    {
+      g_ptr_array_free (new_names, TRUE);
+
+      if (fs->selected_names != NULL)
+	{
+	  free_selected_names (fs->selected_names);
+	  fs->selected_names = NULL;
+	}
+
+      return;
+    }
+
+  if (new_names->len != 1)
+    {
+      GPtrArray *old_names = fs->selected_names;
+
+      if (old_names != NULL)
+	{
+	  /* FIXME: unfinished logic here, need to get the cases for
+	   * multiremoval correct
+	   */
+	  if (new_names->len > old_names->len)
+	    {
+	      /* A common case is shift-click a range of files from top to
+	       * bottom, so quickly check for that to avoid looping over the
+	       * entire list
+	       */
+	      if (compare_filenames (g_ptr_array_index (old_names, old_names->len - 1),
+				     g_ptr_array_index (new_names, new_names->len - 1)) < 0)
+		index = new_names->len - 1;
+	      else
+		{
+		  for (i = 0; i < old_names->len; i++)
+		    {
+		      if (compare_filenames (g_ptr_array_index (old_names, i),
+					     g_ptr_array_index (new_names, i)) != 0)
+			{
+			  index = i;
+			  break;
+			}
+		    }
+		}
+	    }
+	  else if (new_names->len < old_names->len)
+  	    {
+	    }
+	  else
+	    {
+	    }
+	}
+      else
+	{
+	  /* ctrl-click deselected the last selected item, but the selection
+	   * remembers that for subsequent shift clicks and uses it as an
+	   * anchor. So search up from there.
+	   */
+	  if (compare_filenames (fs->last_selected,
+				 g_ptr_array_index (new_names, 0)) == 0)
+	    index = new_names->len - 1;
+	  else
+	    index = 0;
+	}
+    }
+  else
+    index = 0;
+
+  if (fs->selected_names != NULL)
+    free_selected_names (fs->selected_names);
+
+  fs->selected_names = new_names;
+
+  if (fs->last_selected != NULL)
+    g_free (fs->last_selected);
+
+  fs->last_selected = g_strdup (g_ptr_array_index (new_names, index));
+  filename = get_real_filename (fs->last_selected, FALSE);
+
+  gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
+
+  if (filename != fs->last_selected)
+    g_free (filename);
+}
+
+gchar **
+gtk_file_selection_get_selections (GtkFileSelection *filesel)
+{
+  GPtrArray *names;
+  gchar **selections;
+  gchar *filename, *dirname, *current;
+  gint i, count;
+
+  g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), NULL);
+
+  names = filesel->selected_names;
+
+  selections = g_new (gchar *, names->len + 2);
+
+  filename = g_strdup (gtk_file_selection_get_filename (filesel));
+
+  if (strlen (filename) == 0)
+    {
+      g_free (filename);
+      return NULL;
+    }
+
+  count = 0;
+  selections[count++] = filename;
+
+  if (names != NULL)
+    {
+      dirname = g_path_get_dirname (filename);
+
+      for (i = 0; i < names->len; i++)
+	{
+	  current = g_build_filename (dirname,
+				      g_ptr_array_index (names, i),
+				      NULL);
+
+	  if (compare_filenames (current, filename) != 0)
+	    selections[count++] = current;
+	  else
+	    g_free (current);
+	}
+    }
+
+  selections[count] = NULL;
+
+  return selections;
+}
+
 /**********************************************************************/
 /*			  External Interface                          */
 /**********************************************************************/
@@ -3524,13 +3723,8 @@ static gint
 compare_cmpl_dir (const void *a,
 		  const void *b)
 {
-#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
-  return strcmp ((((CompletionDirEntry*)a))->entry_name,
-		 (((CompletionDirEntry*)b))->entry_name);
-#else
-  return g_strcasecmp ((((CompletionDirEntry*)a))->entry_name,
-		       (((CompletionDirEntry*)b))->entry_name);
-#endif
+  return compare_filenames ((((CompletionDirEntry*)a))->entry_name,
+			    (((CompletionDirEntry*)b))->entry_name);
 }
 
 static gint
Index: gtkfilesel.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkfilesel.h,v
retrieving revision 1.13
diff -u -p -r1.13 gtkfilesel.h
--- gtkfilesel.h	2001/03/23 23:39:24	1.13
+++ gtkfilesel.h	2002/02/15 23:49:04
@@ -74,7 +74,9 @@ struct _GtkFileSelection
   
   GtkWidget *button_area;
   GtkWidget *action_area;
-  
+
+  GPtrArray *selected_names;
+  gchar     *last_selected;
 };
 
 struct _GtkFileSelectionClass
@@ -100,6 +102,11 @@ void	   gtk_file_selection_complete		  (
 void       gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel);
 void       gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel);
 
+gchar**    gtk_file_selection_get_selections      (GtkFileSelection *filesel);
+
+void       gtk_file_selection_set_select_multiple (GtkFileSelection *filesel,
+						   gboolean          select_multiple);
+gboolean   gtk_file_selection_get_select_multiple (GtkFileSelection *filesel);
 
 #ifdef __cplusplus
 }
@@ -107,13 +114,3 @@ void       gtk_file_selection_hide_fileo
 
 
 #endif /* __GTK_FILESEL_H__ */
-
-
-
-
-
-
-
-
-
-


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