fileselection patch.



Here is a slightly cleaned up version of the patch I submitted
to the gtk-list on friday.

After looking into it some more this is not as necessary for me
as I thought it was on friday morning. It appears that the file
completion will give effective filtering for single wildcard patterns. 
This patch has the advantage that multiple wildcards can be matched
(ie. *.jpg and *.gif ). 

To make good use of this feature the filedialog would have to be 
customized by the programmer for the user to specify the filter.

This patch was created with cvs diff -u in gtk+/gtk. Let me know
if that doesn't work well.


Todd.

-- 
-----------------------------------------
Todd & Lisa Dukes
tjdukes@flash.net
? gtkfilesel.c.new
? gtkfilesel.h.new
? cvs.diff
Index: gtkfilesel.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkfilesel.c,v
retrieving revision 1.44
diff -u -r1.44 gtkfilesel.c
--- gtkfilesel.c	1999/01/09 22:34:28	1.44
+++ gtkfilesel.c	1999/01/17 04:12:38
@@ -292,23 +292,27 @@
 					      GdkEventKey           *event,
 					      gpointer               user_data);
 
-static void gtk_file_selection_file_button (GtkWidget *widget,
-					    gint row, 
-					    gint column, 
+static void gtk_file_selection_file_button (GtkWidget      *widget,
+					    gint            row, 
+					    gint            column, 
 					    GdkEventButton *bevent,
-					    gpointer user_data);
+					    gpointer        user_data);
 
-static void gtk_file_selection_dir_button (GtkWidget *widget,
-					   gint row, 
-					   gint column, 
+static void gtk_file_selection_dir_button (GtkWidget      *widget,
+					   gint            row, 
+					   gint            column, 
 					   GdkEventButton *bevent,
-					   gpointer data);
+					   gpointer        data);
 
-static void gtk_file_selection_populate      (GtkFileSelection      *fs,
-					      gchar                 *rel_path,
-					      gint                   try_complete);
-static void gtk_file_selection_abort         (GtkFileSelection      *fs);
+static gboolean	gtk_file_selection_match_filter  (GtkFileSelection *filesel, 
+						  gchar            *filename);
 
+static void gtk_file_selection_populate (GtkFileSelection *fs,
+					 gchar            *rel_path,
+					 gint              try_complete);
+
+static void gtk_file_selection_abort (GtkFileSelection *fs);
+
 static void gtk_file_selection_update_history_menu (GtkFileSelection       *fs,
 						    gchar                  *current_dir);
 
@@ -493,6 +497,9 @@
   gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
   gtk_widget_show (filesel->selection_entry);
 
+  filesel->filter = NULL;
+  filesel->numfilter = 0;
+
   if (!cmpl_state_okay (filesel->cmpl_state))
     {
       gchar err_buf[256];
@@ -1069,31 +1076,33 @@
   gtk_widget_show (dialog);
 }
 
-
 static gint
 gtk_file_selection_key_press (GtkWidget   *widget,
 			      GdkEventKey *event,
 			      gpointer     user_data)
 {
-  GtkFileSelection *fs;
-  char *text;
 
-  g_return_val_if_fail (widget != NULL, FALSE);
-  g_return_val_if_fail (event != NULL, FALSE);
+  g_return_val_if_fail ( widget != NULL, FALSE);
+  g_return_val_if_fail ( event != NULL, FALSE);
+  g_return_val_if_fail ( user_data != NULL, FALSE);
+  g_return_val_if_fail ( GTK_IS_FILE_SELECTION( user_data), FALSE);
 
-  if (event->keyval == GDK_Tab)
+  if ( event->keyval == GDK_Tab)
     {
-      fs = GTK_FILE_SELECTION (user_data);
-      text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
 
-      text = g_strdup (text);
+      char             *text;
+      GtkFileSelection *filesel;
 
-      gtk_file_selection_populate (fs, text, TRUE);
+      filesel = GTK_FILE_SELECTION ( user_data);
+      text = gtk_entry_get_text ( GTK_ENTRY ( filesel->selection_entry));
+      text = g_strdup ( text);
 
-      g_free (text);
+      gtk_file_selection_populate (filesel, text, TRUE); 
 
-      gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
+      g_free ( text);
 
+      gtk_signal_emit_stop_by_name ( GTK_OBJECT (widget), "key_press_event");
+
       return TRUE;
     }
 
@@ -1279,6 +1288,55 @@
     }
 }
 
+void
+gtk_file_selection_set_filter ( GtkFileSelection  *filesel,
+				gchar            **filter,
+				gint 	           numfilter)
+{
+  gint i;
+
+  g_return_if_fail( filesel != NULL);
+  g_return_if_fail( GTK_IS_FILE_SELECTION ( filesel));
+  
+  if ( filesel->numfilter) 
+    {
+      for (i = 0; i < filesel->numfilter; i++) 
+	{
+	  g_free ( filesel->filter[ i]);
+	}
+    }
+  
+  g_free ( filesel->filter);
+
+  filesel->filter = (gchar **) g_malloc ( numfilter * sizeof( gchar *));
+
+  for ( i = 0; i < numfilter; i++) 
+    {
+      filesel->filter[i] = g_strdup ( filter[i]);
+    }
+
+  filesel->numfilter = numfilter;
+
+  gtk_file_selection_populate ( filesel, "", FALSE);
+
+}
+
+static gboolean
+gtk_file_selection_match_filter( GtkFileSelection *filesel, gchar *filename)
+{
+  gint i = 0;
+
+  g_return_val_if_fail( filesel != NULL, FALSE);
+  g_return_val_if_fail( GTK_IS_FILE_SELECTION ( filesel), FALSE);
+  g_return_val_if_fail( filename != NULL, FALSE);
+
+  for (i = 0; i < filesel->numfilter; i++) 
+    if ( !fnmatch( filesel->filter[i], filename, 0))
+      return TRUE;
+
+  return FALSE;
+}
+
 static void
 gtk_file_selection_populate (GtkFileSelection *fs,
 			     gchar            *rel_path,
@@ -1357,17 +1415,21 @@
 		    }
  		}
 	    }
-          else
+	  else 
 	    {
-	      int width = gdk_string_width(fs->file_list->style->font,
-				           filename);
-	      row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
-	      if(width > file_list_width)
-	        {
-	          file_list_width = width;
-	          gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,
-					     width);
-	        }
+	      if (!fs->filter || gtk_file_selection_match_filter(fs, filename))
+		{
+		  int width = gdk_string_width(fs->file_list->style->font,
+					       filename);
+		  
+		  row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
+		  if(width > file_list_width)
+		    {
+		      file_list_width = width;
+		      gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,
+						 width);
+		    }
+		}
             }
 	}
 
Index: gtkfilesel.h
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtkfilesel.h,v
retrieving revision 1.5
diff -u -r1.5 gtkfilesel.h
--- gtkfilesel.h	1998/10/22 00:54:56	1.5
+++ gtkfilesel.h	1999/01/17 04:12:38
@@ -65,6 +65,8 @@
   
   GtkWidget *button_area;
   GtkWidget *action_area;
+  gchar     **filter;
+  gint      numfilter;
   
 };
 
@@ -83,8 +85,9 @@
 						   const gchar	    *pattern);
 void       gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel);
 void       gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel);
-
-
+void	   gtk_file_selection_set_filter	  (GtkFileSelection *filesel,
+						   gchar	   **filter,
+						   gint 	     numfilter);
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
Index: testgtk.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/testgtk.c,v
retrieving revision 1.157
diff -u -r1.157 testgtk.c
--- testgtk.c	1999/01/11 18:49:52	1.157
+++ testgtk.c	1999/01/17 04:12:54
@@ -5195,7 +5195,31 @@
  * GtkFileSelection
  */
 
+void 
+file_selection_set_filters( GtkWidget        *widget,
+			    GtkFileSelection *fs) 
+{
+  gchar * filter_text[] = 
+  { "*.xpm",
+    "*.c",
+    "*.h"
+  };
+
+  gint num_filters;
+  
+  num_filters = sizeof ( filter_text) / sizeof ( gchar *);
+  gtk_file_selection_set_filter ( fs, filter_text, num_filters);
+
+}
+
 void
+file_selection_clear_filter( GtkWidget *widget,
+				GtkFileSelection * fs) 
+{
+  gtk_file_selection_set_filter( fs, NULL, 0);
+}
+
+void
 file_selection_hide_fileops (GtkWidget *widget,
 			     GtkFileSelection *fs)
 {
@@ -5206,8 +5230,18 @@
 file_selection_ok (GtkWidget        *w,
 		   GtkFileSelection *fs)
 {
-  g_print ("%s\n", gtk_file_selection_get_filename (fs));
-  gtk_widget_destroy (GTK_WIDGET (fs));
+  gchar * text;
+
+  text = gtk_file_selection_get_filename( fs);
+
+  if ( strchr ( text, '*') || strchr ( text, '?')) 
+    {
+      /* don't close when filename contains  wildcards */
+      return;
+    }
+
+  g_print ( "%s\n", gtk_file_selection_get_filename ( fs));
+  gtk_widget_destroy ( GTK_WIDGET ( fs));
 }
 
 void
@@ -5218,6 +5252,7 @@
 
   if (!window)
     {
+
       window = gtk_file_selection_new ("file selection dialog");
 
       gtk_file_selection_hide_fileop_buttons (GTK_FILE_SELECTION (window));
@@ -5244,12 +5279,29 @@
       gtk_widget_show (button);
 
       button = gtk_button_new_with_label ("Show Fileops");
-      gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
-				 (GtkSignalFunc) gtk_file_selection_show_fileop_buttons, 
-				 (gpointer) window);
-      gtk_box_pack_start (GTK_BOX (GTK_FILE_SELECTION (window)->action_area), 
-			  button, FALSE, FALSE, 0);
-      gtk_widget_show (button);
+      gtk_signal_connect_object ( GTK_OBJECT (button), "clicked",
+				  (GtkSignalFunc) gtk_file_selection_show_fileop_buttons, 
+				  (gpointer) window);
+      gtk_box_pack_start ( GTK_BOX (GTK_FILE_SELECTION (window)->action_area), 
+			   button, FALSE, FALSE, 0);
+      gtk_widget_show ( button);
+
+      button = gtk_button_new_with_label ( "set filter");
+      gtk_signal_connect ( GTK_OBJECT (button), "clicked",
+			   GTK_SIGNAL_FUNC ( file_selection_set_filters),
+			   window);
+      gtk_box_pack_start ( GTK_BOX ( GTK_FILE_SELECTION ( window)->action_area), 
+			   button, FALSE, FALSE, 0);
+      gtk_widget_show ( button);
+
+      button = gtk_button_new_with_label ( "clear filter");
+      gtk_signal_connect ( GTK_OBJECT ( button), "clicked",
+			   GTK_SIGNAL_FUNC ( file_selection_clear_filter),
+			   window);
+      gtk_box_pack_start ( GTK_BOX ( GTK_FILE_SELECTION ( window)->action_area), 
+			   button, FALSE, FALSE, 0);
+      gtk_widget_show ( button);
+
     }
   
   if (!GTK_WIDGET_VISIBLE (window))


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