Re: Gtk::FileChooserButton



On 05/01/2011 04:15 AM, John Emmas wrote:
** GTK+ (almost) exonerated **

I've spent quite a bit of time delving into the problems I encountered with Gtk::FileChooserButton.  I 
started by writing a minimal app using a main window with a child button.  The button launched a modal dialog 
box which contained a Gtk::FileChooserButton.  I built the app for Windows (using VC++ and gtk-win32) and 
then for Linux (using gcc and gtk-x11).  It behaved exactly as expected (and pretty much identically) on both 
OS's.

The only problem with the above was that I couldn't find am obvious way to preselect a startup folder for the 
FileChooser dialog.  The only way I found was to create a Gtk::FileChooser dialog separately, then attach it 
to a button using gtk_file_chooser_new_with_dialog().  However, when I do that I see some marked differences 
in the Linux behaviour compared to the Windows behaviour.

The main difference is that under Windows there seems to be an inherent link between the FileChooser dialog and its 
associated FileChooserButton.  e.g. if I choose a particular file then press "Open", the chosen filename 
appears automatically in the associated FileChooserButton.  This doesn't work when I build under Linux with gcc.  
Likewise, with this new strategy I can preselect a startup folder using gtk_file_chooser_set_filename() (see the code 
below).  This works every time in my Windows build but almost never works in the Linux build.

I've listed the code below on the assumption that I've maybe missed out an important step somewhere.  Can 
anyone see any obvious mistakes with my approach?

John


static void
button_clicked (GtkWidget *window, gpointer data)
{
gsize bytes_written, bytes_read;
GtkWidget *vbox;
GtkWidget *dialog;
GtkWidget *file_chooser_button;
GError *error = 0;

     dialog = gtk_dialog_new_with_buttons("File Chooser", (GtkWindow*)data,
                         GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                         GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
                         NULL);

     if (dialog)
     {
// Choose an arbitrary file for pre-selection which is known to exist on both OS's
#ifdef WIN32
         const gchar* home = "C:\\Users\\johne53\\.jackdrc";
#else
         const gchar* home = "/home/johne53/.jackdrc";
#endif

         GtkWidget* file_chooser_dialog = gtk_file_chooser_dialog_new ("Choose a file", GTK_WINDOW(dialog),
                         GTK_FILE_CHOOSER_ACTION_OPEN,
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                         GTK_STOCK_OPEN, GTK_RESPONSE_OK,
                         NULL);

         if (file_chooser_dialog)
         {
             file_chooser_button = gtk_file_chooser_button_new_with_dialog (file_chooser_dialog);

             if (file_chooser_button)
             {
                 // This always works
                 printf ("Initializing FileChooser file to:- %s\n", home);

                 // This works in the Win32 build but not in the Linux build
                 gtk_file_chooser_set_filename((GtkFileChooser*)file_chooser_dialog,
                         g_filename_to_utf8(home, strlen(home),&bytes_read,&bytes_written,&error))

                 vbox = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
                 gtk_box_pack_start (GTK_BOX(vbox), file_chooser_button, TRUE, TRUE, 0);
                 gtk_widget_show_all (dialog);
             }
         }

         gtk_dialog_run((GtkDialog*)dialog);
         gtk_widget_destroy(dialog);
     }
}


I know this is an old thread, but I just discovered a solution to a problem I've been having that I believe is the same issue. gtk_file_chooser_set_filename and gtk_file_chooser_select_filename do not behave correctly under certain circumstances. If you have enabled the display of hidden files in any app (right click popup menu in any file chooser dialog) AND you do not set a filter on your file chooser dialog with gtk_file_chooser_set_filter, then set/select_filename do not work. Simply setting a filter with "*" as the glob works around the issue.

gtk 2.22 definitely still has the problem (fedora 15). A quick read of the code indicates 2.24 still has the problem. git head looks like it's fixed. Problem code is in gtkfilechooserdefault.c:show_and_select_files(). It only walks the file list and selects an item if can_have_hidden || can_have_filtered.




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