Chapter 41. File Selection Dialogs

Inheritance Hierarchy

Object
   +--- Widget
         +--- Container
               +--- Bin
                     +--- Window
                           +--- FileSelectionDialog
         

The file selection dialog is a quick and simple way to display a dialog box to prompt the user for a filename. It comes complete with Ok, Cancel, and Help buttons, and it's a great way to cut down on programming time.

To create a new file selection box use:

$file_dialog = new Gtk::FileSelection( $title );

To set the filename, for example to bring up a specific directory, or give a default filename, use this function:

$file_dialog->set_filename( $filename );

To grab the text that the user has entered or clicked on, use this function:

$file_dialog->get_filename();

You can match a pattern to a filename in the current directory using the following function:

$file_dialog->complete( $pattern );

If a match can be made, the matched filename will appear in the text entry field of the file selection dialog. If a partial match can be made, the list of files will contain those filenames which have been partially matched.

An example of a pattern would be *.txt or gtk*.

The File Selection Dialog can show some file operation buttons. You can show these buttons using:

$file_dialog->show_fileop_buttons();

These buttons can be hidden using:

$file_dialog->hide_fileop_buttons();

You can also access 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 widgets in signaling their use.

41.1. File Selection Example

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.

File Selection Dialog Source

      
#!/usr/bin/perl -w

use 
Gtk
;
use 
strict
;

set_locale Gtk;
init Gtk;


my
 $false = 0;

my
 $true = 1;


my
 $file_dialog;

# Create a new file selection widget
$file_dialog = new Gtk::FileSelection( "File Selection" );
$file_dialog->signal_connect( "destroy", 
sub
 { Gtk->
exit
( 0 ); } );

# Connect the ok_button to file_ok_sel function
$file_dialog->ok_button->signal_connect( "clicked",
					 \&file_ok_sel,
					 $file_dialog );

# Connect the cancel_button to destroy the widget
$file_dialog->cancel_button->signal_connect( "clicked",
					     
sub
 { Gtk->
exit
( 0 ); } );

# Lets set the filename, as if this were a save dialog, and we are giving
# a default filename
$file_dialog->set_filename( "penguin.png" );

$file_dialog->show();
main Gtk;

exit
( 0 );



# Get the selected filename and print it to the console

sub
 
file_ok_sel

{
   
my
 ( $widget, $file_selection ) = @_;
   
my
 $file = $file_selection->get_filename();

   
print
( "$file\n" );
}


# END EXAMPLE PROGRAM
      
   

File Dialog Example Screenshot