GNOME Bugzilla – Bug 581873
gtk_file_chooser_set_filter does nothing / file chooser has no filter selected
Last modified: 2009-10-20 21:12:43 UTC
$ pkg-config --modversion glib-2.0 2.18.2 $ pkg-config --modversion gtk+-2.0 2.14.4 its Ubuntu 8.10, same issue on opensuse 11.1. I add a couple of filters to my filechooser using gtk_file_chooser_add_filter() if(!file_name) { GST_DEBUG("use defaults %s/%s",folder_name,name); /* the user just created a new document */ gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), folder_name); gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER(dialog), name); } else { /* the user edited an existing document */ gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),file_name); } in the first case (!file_name) the filechooser shows the first filter in the filter list. in the second case the filter list shows no entry. but if I click on it I see all filters. I then added some code that uses gtk_file_chooser_set_filter() to preselect the filter that matches the current format. But the gtk_file_chooser_set_filter() has no visual effect. For the record I tried two ways to select the current filter: gtk_file_filter_filter() and just matching the extensions myself. gtk_file_filter_filter() unfortunately never matches. The extension comparission works though.
With gtk from git HEAD. The file_filter seems to work, but still gtk_file_chooser_set_filter() has no visual effect. One more detail, the application is installed under an own prefix. I have set XDG_DATA_DIRS to contain $prefix/share - i wonder if that is enough to ensure glib finds the mime-info I have installed there.
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog),filter); is doing a g_object_set(chooser, "filter", filter, NULL); and in gtkfilechooserdefault.c this is calling set_current_filter(); I set a break point on set_current_filter() but its never stopping there. Will add printfs next :/ Breakpoint on gtk_file_chooser_set_filter() works.
I looked into it again. I added two printfs in gtk+/gtk/gtkfilechooserdefault.c: * at the begin of set_current_filter() * at show_and_select_files_finished_loading() before is calls set_current_filter (data->impl, NULL); And this is what happens. I create my file chooser, as we have an existing file (save-as) I do gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),file_name);. Then I match the file-type and set the filter to show only those files gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog),filter); Finally bring the dialog up: puts("ui ready"); gtk_widget_show_all(dialog); puts("ui shown"); result=gtk_dialog_run(GTK_DIALOG(dialog)); Below is the log and it confirms that gtk+ for some reason resets my filter. As usual for gtk+ that code there is almost uncommented and that keep me wondering what the intention here. Another printf shows that it checks the file I have set as a filename. So if that is a hidden file if will show hidden files. And if my selected filter matches it resets the fiter. I'd say that logic is flawed. Imho the filter should be reset if all the filter is not matching one or many of the selected filenames. And indeed that makes it work. I'll attach a patch. -- log -- use last path /home/ensonic/buzztard/share/buzztard/songs/kick.xml, format is 'buzztard song without externals', filter 0x8719dc0 changing file filter to: 0x8719dc0, buzztard song without externals changing file filter to: 0x8719dc0, buzztard song without externals ui ready ui shown resetting the filter changing file filter to: (nil), (NULL) changing file filter to: (nil), (NULL)
Created attachment 145546 [details] [review] flip the if condition to not wrongly reset the filter Please review. If the fix is okay, please let me know if I should apply to to some branch as well.
Created attachment 145547 [details] [review] same patch with the printfs to easy verification
Thanks for tracking this down, Stefan. Could you please check the show_and_select_files() function in the "filesystemmodel" branch? The code is a bit different there, but it should follow the same logic that you describe. We are going to merge that branch soon; feel free to commit your fix to it and it will go into the merged version later.
Hmm, with the code in git HEAD its already fixed. In the code here, my patch would still help. http://git.gnome.org/cgit/gtk+/tree/gtk/gtkfilechooserdefault.c?h=gtk-2-18#n6670 I now just tested under ubunto jaunty and they might have a local patch. Its gtk+-2.16.1 and I did not get the full problem there. Will retest at home under opensuse 11.1 later today.
2.16.4 still has it. Something in the 2.17.X series fixed it.
I guess there is no point in applying anything to 2.16.X as 2.18.X is stable anyway.