Re: Gtk::FileChooser, multiple select, and select_filename

punting to gtk-devel-list, as this appears to be a gtk+ issue and not a binding one. (C code at the bottom, for those not versed in gtk2-perl; i've quoted the whole message for context.)

On Jul 17, 2004, at 2:09 PM, Daniel Flemming wrote:

I've found that, with a FileChooserDialog anyway, using the select_filename method doesn't work correctly with multiple file selection enabled.

Here's the relevant bits of code. This is how I would like the code to work:

	    my @scanFiles = $fileChooser->get_filenames;
	    my @selectedFiles = ();
	    foreach (@scanFiles) {
		push(@selectedFiles, $_) if (file matches pattern);
	    $fileChooser->select_filename ($_) foreach (@selectedFiles);

This is how I have the code working:

	    my @scanFiles = $fileChooser->get_filenames;
	    my @unselectedFiles = ();
	    foreach (@scanFiles) {
		push(@unselectedFiles, $_) if (file doesn't match pattern);
	    $fileChooser->unselect_filename ($_) foreach (@unselectedFiles);

The second fragment works fine, but it's rather inefficient, especially when you want to select, say, 12 files out of 200 or more. The problem with the first fragment is that the select_filename method doesn't seem to respect the multiple selection mode set in the FileChooser, and the result is that the last filename to receive a select_filename event is the only one that gets selected.

This could be a bug in gtk itself, of course. I've got Gtk 2.4.3 on my system.

the binding for gtk_file_chooser_set_filename() is trivial -- it just passes the args down to gtk+, which does all the work.

punting to gtk-devel-list, but you'd be best off filing a bug in bugzilla. perhaps they can tell us first if it's known/expected/intended behavior.

here's an example using the first fragment from above, ported to C. pass the extension of the files to select as the first command-line argument, e.g.

   ./chooser .pl   # filenames ending in .pl

#include <gtk/gtk.h>

select_by_ext (GtkFileChooser * chooser,
               char * extension)
        GSList * scanfiles, * selectedfiles, * i;
        int len = strlen (extension);

	/* $fileChooser->select_all; */
        gtk_file_chooser_select_all (chooser);

        /* my @scanFiles = $fileChooser->get_filenames; */
        scanfiles = gtk_file_chooser_get_filenames (chooser);

        /* my @selectedFiles = (); */
        selectedfiles = NULL;

        /* foreach (@scanFiles) { */
        for (i = scanfiles ; i != NULL ; i = i->next) {
                /* push(@selectedFiles, $_) if (/$ext$/); */
                char * filename = (char*) i->data;
                int flen = strlen (filename);
                if (flen > len &&
                    0 == strcmp (filename + flen - len, extension))
                       selectedfiles = g_slist_append (selectedfiles,

        /* $fileChooser->unselect_all; */
        gtk_file_chooser_unselect_all (chooser);

/* $fileChooser->select_filename ($_) foreach (@selectedFiles); */
        for (i = selectedfiles ; i != NULL ; i = i->next)
gtk_file_chooser_select_filename (chooser, (gchar*) i->data);

        g_slist_free (scanfiles);
        g_slist_free (selectedfiles);

main (int argc,
      char * argv[])
        GtkWidget * chooser;
        char * extension = ".pl";

        /* use Gtk2 -init; */
        gtk_init (&argc, &argv);
        if (argc > 1)
                extension = argv[1];

/* $fileChooser = Gtk2::FileChooserDialog->new ('foo', undef, 'open', 'gtk-ok' => 'ok');
        chooser = gtk_file_chooser_dialog_new
                        ("foo", NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
                         GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);

        /* $fileChooser->set_select_multiple (1); */
gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser),

/* $fileChooser->signal_connect_after (show => \&select_by_ext); */
        g_signal_connect_after (G_OBJECT (chooser), "show",
                                G_CALLBACK (select_by_ext), extension);

        /* $fileChooser->run; */
        gtk_dialog_run (GTK_DIALOG (chooser));

        return 0;

muppet <scott at asofyet dot org>

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