Re: Gtk::FileChooserButton



** 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);
    }
}


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