The file selection widget is a quick and simple way to display a File dialog box. It comes complete with Ok, Cancel, and Help buttons, a great way to cut down on programming time.
To create a new file selection box use:
GtkWidget *gtk_file_selection_new( gchar *title ); |
To set the filename, for example to bring up a specific directory, or give a default filename, use this function:
void gtk_file_selection_set_filename( GtkFileSelection *filesel, gchar *filename ); |
To grab the text that the user has entered or clicked on, use this function:
gchar *gtk_file_selection_get_filename( GtkFileSelection *filesel ); |
There are also pointers to the widgets contained within the file selection widget. These are:
dir_list file_list selection_entry selection_text main_vbox ok_button cancel_button help_button |
Most likely you will want to use the ok_button, cancel_button, and help_button pointers in signaling their use.
Included here is an example stolen from testgtk.c, modified to run on its own. As you will see, there is nothing much to creating a file selection widget. While in this example the Help button appears on the screen, it does nothing as there is not a signal attached to it.
/* example-start filesel filesel.c */ #include <gtk/gtk.h> /* Get the selected filename and print it to the console */ void file_ok_sel( GtkWidget *w, GtkFileSelection *fs ) { g_print ("%s\n", gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs))); } void destroy( GtkWidget *widget, gpointer data ) { gtk_main_quit (); } int main( int argc, char *argv[] ) { GtkWidget *filew; gtk_init (&argc, &argv); /* Create a new file selection widget */ filew = gtk_file_selection_new ("File selection"); gtk_signal_connect (GTK_OBJECT (filew), "destroy", (GtkSignalFunc) destroy, &filew); /* Connect the ok_button to file_ok_sel function */ gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (filew)->ok_button), "clicked", (GtkSignalFunc) file_ok_sel, filew ); /* Connect the cancel_button to destroy the widget */ gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (filew)->cancel_button), "clicked", (GtkSignalFunc) gtk_widget_destroy, GTK_OBJECT (filew)); /* Lets set the filename, as if this were a save dialog, and we are giving a default filename */ gtk_file_selection_set_filename (GTK_FILE_SELECTION(filew), "penguin.png"); gtk_widget_show(filew); gtk_main (); return 0; } /* example-end */ |