FileChooser is an interface that can be implemented by file selection widgets.
In GTK+, the main objects that implement this interface are FileChooserWidget
, FileChooserDialog, and
FileChooserButton. You do not need to write an object that implements the
FileChooser interface unless you are trying to adapt an existing file selector to expose a standard programming
interface.
FileChooser allows for shortcuts to various places in the filesystem. In the default implementation these are
displayed in the left pane. It may be a bit confusing at first that these shortcuts come from various sources and in various flavours, so lets
explain the terminology here:
- Bookmarks: are created by the user, by dragging folders from the right pane to the left pane, or by using the “Add”. Bookmarks can be
renamed and deleted by the user.
- Shortcuts: can be provided by the application. For example, a Paint program may want to add a shortcut for a Clipart folder. Shortcuts
cannot be modified by the user.
- Volumes: are provided by the underlying filesystem abstraction. They are the “roots” of the filesystem.
File Names and Encodings
When the user is finished selecting files in a FileChooser, your program can get the selected names either as
filenames or as URIs. For URIs, the normal escaping rules are applied if the URI contains non-ASCII characters. However, filenames are always
returned in the character set specified by the `G_FILENAME_ENCODING` environment variable. Please see the GLib documentation for more details
about this variable.
This means that while you can pass the result of get_filename to
g_open
or open, you may not be able to directly set it as
the text of a Label widget unless you convert it first to UTF-8, which all GTK+ widgets
expect. You should use to_utf8 to convert filenames into strings that can be
passed to GTK+ widgets.
Adding a Preview Widget
You can add a custom preview widget to a file chooser and then get notification about when the preview needs to be updated. To install a
preview widget, use set_preview_widget. Then, connect to the
update_preview signal to get notified when you need to update the
contents of the preview.
Your callback should use get_preview_filename to see what
needs previewing. Once you have generated the preview for the corresponding file, you must call
set_preview_widget_active with a boolean flag that
indicates whether your callback could successfully generate a preview.
Example: Using a Preview Widget
{
GtkImage *preview;
...
preview = gtk_image_new ();
gtk_file_chooser_set_preview_widget (my_file_chooser, preview);
g_signal_connect (my_file_chooser, "update-preview",
G_CALLBACK (update_preview_cb), preview);
}
static void
update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
{
GtkWidget *preview;
char *filename;
GdkPixbuf *pixbuf;
gboolean have_preview;
preview = GTK_WIDGET (data);
filename = gtk_file_chooser_get_preview_filename (file_chooser);
pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
have_preview = (pixbuf != NULL);
g_free (filename);
gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
if (pixbuf)
g_object_unref (pixbuf);
gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
}
Adding Extra Widgets
You can add extra widgets to a file chooser to provide options that are not present in the default design. For example, you can add a toggle
button to give the user the option to open a file in read-only mode. You can use
set_extra_widget to insert additional widgets in a file chooser.
An example for adding extra widgets:
GtkWidget *toggle;
...
toggle = gtk_check_button_new_with_label ("Open file read-only");
gtk_widget_show (toggle);
gtk_file_chooser_set_extra_widget (my_file_chooser, toggle);
}
If you want to set more than one extra widget in the file chooser, you can a container such as a
Box or a Grid and include your widgets in
it. Then, set the container as the whole extra widget.
- public void add_choice (string id, string label, string[]? options, string[]? option_labels)
Adds a 'choice' to the file chooser.
- public void add_filter (owned FileFilter filter)
Adds filter
to the list of filters that the user can select
between.
- public bool add_shortcut_folder (string folder) throws Error
Adds a folder to be displayed with the shortcut folders in a file chooser.
- public bool add_shortcut_folder_uri (string uri) throws Error
Adds a folder URI to be displayed with the shortcut folders in a file
chooser.
- public FileChooserAction get_action ()
Gets the type of operation that the file chooser is performing; see
set_action.
- public unowned string get_choice (string id)
Gets the currently selected option in the 'choice' with the given ID.
- public bool get_create_folders ()
Gets whether file choser will offer to create new folders.
- public string? get_current_folder ()
Gets the current folder of this as a local
filename.
- public File? get_current_folder_file ()
Gets the current folder of this as
File.
- public string? get_current_folder_uri ()
Gets the current folder of this as an URI.
- public string get_current_name ()
Gets the current name in the file selector, as entered by the user in the
text entry for “Name”.
- public bool get_do_overwrite_confirmation ()
Queries whether a file chooser is set to confirm for overwriting when the
user types a file name that already exists.
- public unowned Widget? get_extra_widget ()
- public File get_file ()
Gets the File for
the currently selected file in the file selector.
- public string? get_filename ()
Gets the filename for the currently selected file in the file selector.
- public SList<string> get_filenames ()
Lists all the selected files and subfolders in the current folder of
this.
- public SList<File> get_files ()
Lists all the selected files and subfolders in the current folder of
this as File.
- public unowned FileFilter? get_filter ()
- public bool get_local_only ()
Gets whether only local files can be selected in the file selector.
- public File? get_preview_file ()
Gets the File that
should be previewed in a custom preview Internal function, see
get_preview_uri.
- public string? get_preview_filename ()
Gets the filename that should be previewed in a custom preview widget.
- public string? get_preview_uri ()
Gets the URI that should be previewed in a custom preview widget.
- public unowned Widget? get_preview_widget ()
- public bool get_preview_widget_active ()
Gets whether the preview widget set by
set_preview_widget should be shown for the current filename.
- public bool get_select_multiple ()
Gets whether multiple files can be selected in the file selector.
- public bool get_show_hidden ()
Gets whether hidden files and folders are displayed in the file selector.
- public string? get_uri ()
Gets the URI for the currently selected file in the file selector.
- public SList<string> get_uris ()
Lists all the selected files and subfolders in the current folder of
this.
- public bool get_use_preview_label ()
Gets whether a stock label should be drawn with the name of the previewed
file.
- public SList<unowned FileFilter> list_filters ()
- public SList<string>? list_shortcut_folder_uris ()
- public SList<string>? list_shortcut_folders ()
- public void remove_choice (string id)
Removes a 'choice' that has been added with
add_choice.
- public void remove_filter (FileFilter filter)
Removes filter
from the list of filters that the user can
select between.
- public bool remove_shortcut_folder (string folder) throws Error
Removes a folder from a file chooser’s list of shortcut folders.
- public bool remove_shortcut_folder_uri (string uri) throws Error
Removes a folder URI from a file chooser’s list of shortcut folders.
- public void select_all ()
Selects all the files in the current folder of a file chooser.
- public bool select_file (File file) throws Error
Selects the file referred to by file
.
- public bool select_filename (string filename)
Selects a filename.
- public bool select_uri (string uri)
Selects the file to by uri
.
- public void set_action (FileChooserAction action)
Sets the type of operation that the chooser is performing; the user
interface is adapted to suit the selected action.
- public void set_choice (string id, string option)
Selects an option in a 'choice' that has been added with
add_choice.
- public void set_create_folders (bool create_folders)
Sets whether file choser will offer to create new folders.
- public bool set_current_folder (string filename)
Sets the current folder for this from a
local filename.
- public bool set_current_folder_file (File file) throws Error
Sets the current folder for this from a
File.
- public bool set_current_folder_uri (string uri)
Sets the current folder for this from an
URI.
- public void set_current_name (string name)
Sets the current name in the file selector, as if entered by the user.
- public void set_do_overwrite_confirmation (bool do_overwrite_confirmation)
Sets whether a file chooser in
gtk_file_chooser_action_save mode will present a confirmation dialog if the user types a file name that already exists.
- public void set_extra_widget (Widget extra_widget)
Sets an application-supplied widget to provide extra options to the user.
- public bool set_file (File file) throws Error
Sets file
as the current filename for the file chooser, by
changing to the file’s parent folder and actually selecting the file in list.
- public bool set_filename (string filename)
Sets filename
as the current filename for the file chooser, by
changing to the file’s parent folder and actually selecting the file in list; all other files will be unselected.
- public void set_filter (FileFilter filter)
Sets the current filter; only the files that pass the filter will be
displayed.
- public void set_local_only (bool local_only)
Sets whether only local files can be selected in the file selector.
- public void set_preview_widget (Widget preview_widget)
Sets an application-supplied widget to use to display a custom preview of
the currently selected file.
- public void set_preview_widget_active (bool active)
Sets whether the preview widget set by
set_preview_widget should be shown for the current filename.
- public void set_select_multiple (bool select_multiple)
Sets whether multiple files can be selected in the file selector.
- public void set_show_hidden (bool show_hidden)
Sets whether hidden files and folders are displayed in the file selector.
- public bool set_uri (string uri)
Sets the file referred to by uri
as the current file for the
file chooser, by changing to the URI’s parent folder and actually selecting the URI in the list.
- public void set_use_preview_label (bool use_label)
Sets whether the file chooser should display a stock label with the name of
the file that is being previewed; the default is true.
- public void unselect_all ()
Unselects all the files in the current folder of a file chooser.
- public void unselect_file (File file)
Unselects the file referred to by file
.
- public void unselect_filename (string filename)
Unselects a currently selected filename.
- public void unselect_uri (string uri)
Unselects the file referred to by uri
.