1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdialog.c: File selector dialog
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gtkfilechooserdialog.h"
26 #include "gtkfilechooserprivate.h"
27 #include "gtkfilechooserwidget.h"
28 #include "gtkfilechooserutils.h"
29 #include "gtkfilechooserembed.h"
30 #include "gtkfilesystem.h"
31 #include "gtksizerequest.h"
32 #include "gtktypebuiltins.h"
39 * SECTION:gtkfilechooserdialog
40 * @Short_description: A file chooser dialog, suitable for "File/Open" or "File/Save" commands
41 * @Title: GtkFileChooserDialog
42 * @See_also: #GtkFileChooser, #GtkDialog
44 * #GtkFileChooserDialog is a dialog box suitable for use with
45 * "File/Open" or "File/Save as" commands. This widget works by
46 * putting a #GtkFileChooserWidget inside a #GtkDialog. It exposes
47 * the #GtkFileChooserIface interface, so you can use all of the
48 * #GtkFileChooser functions on the file chooser dialog as well as
49 * those for #GtkDialog.
51 * Note that #GtkFileChooserDialog does not have any methods of its
52 * own. Instead, you should use the functions that work on a
55 * <example id="gtkfilechooser-typical-usage">
56 * <title>Typical usage</title>
57 * In the simplest of cases, you can the following code to use
58 * #GtkFileChooserDialog to select a file for opening:
60 * <informalexample><programlisting>
63 * dialog = gtk_file_chooser_dialog_new ("Open File",
65 * GTK_FILE_CHOOSER_ACTION_OPEN,
66 * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
67 * GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
70 * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
74 * filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
75 * open_file (filename);
79 * gtk_widget_destroy (dialog);
80 * </programlisting></informalexample>
82 * To use a dialog for saving, you can use this:
84 * <informalexample><programlisting>
87 * dialog = gtk_file_chooser_dialog_new ("Save File",
89 * GTK_FILE_CHOOSER_ACTION_SAVE,
90 * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
91 * GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
93 * gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
95 * if (user_edited_a_new_document)
96 * gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), "Untitled document");
98 * gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), filename_for_existing_document);
100 * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
104 * filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
105 * save_to_file (filename);
109 * gtk_widget_destroy (dialog);
110 * </programlisting></informalexample>
113 * <section id="gtkfilechooserdialog-setting-up">
114 * <title>Setting up a file chooser dialog</title>
115 * There are various cases in which you may need to use a #GtkFileChooserDialog:
116 * <itemizedlist><listitem>To select a file for opening, as for a
117 * <guimenuitem>File/Open</guimenuitem> command. Use
118 * #GTK_FILE_CHOOSER_ACTION_OPEN.
120 * <listitem>To save a file for the first time, as for a
121 * <guimenuitem>File/Save</guimenuitem> command. Use
122 * #GTK_FILE_CHOOSER_ACTION_SAVE, and suggest a name such as
123 * "Untitled" with gtk_file_chooser_set_current_name().
125 * <listitem>To save a file under a different name, as for a
126 * <guimenuitem>File/Save As</guimenuitem> command. Use
127 * #GTK_FILE_CHOOSER_ACTION_SAVE, and set the existing filename
128 * with gtk_file_chooser_set_filename().
130 * <listitem>To choose a folder instead of a file. Use
131 * #GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
132 * </listitem></itemizedlist>
135 * Old versions of the file chooser's documentation suggested
136 * using gtk_file_chooser_set_current_folder() in various
137 * situations, with the intention of letting the application
138 * suggest a reasonable default folder. This is no longer
139 * considered to be a good policy, as now the file chooser is
140 * able to make good suggestions on its own. In general, you
141 * should only cause the file chooser to show a specific folder
142 * when it is appropriate to use gtk_file_chooser_set_filename(),
143 * i.e. when you are doing a <guimenuitem>File/Save
144 * As</guimenuitem> command <emphasis>and</emphasis> you already
145 * have a file saved somewhere.
149 * <section id="gtkfilechooserdialog-response-codes">
150 * <title>Response Codes</title>
151 * #GtkFileChooserDialog inherits from #GtkDialog, so buttons that
152 * go in its action area have response codes such as
153 * #GTK_RESPONSE_ACCEPT and #GTK_RESPONSE_CANCEL. For example, you
154 * could call gtk_file_chooser_dialog_new() as follows:
156 * <informalexample><programlisting>
159 * dialog = gtk_file_chooser_dialog_new ("Open File",
161 * GTK_FILE_CHOOSER_ACTION_OPEN,
162 * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
163 * GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
165 * </programlisting></informalexample>
167 * This will create buttons for "Cancel" and "Open" that use stock
168 * response identifiers from #GtkResponseType. For most dialog
169 * boxes you can use your own custom response codes rather than the
170 * ones in #GtkResponseType, but #GtkFileChooserDialog assumes that
171 * its "accept"-type action, e.g. an "Open" or "Save" button,
172 * <emphasis>will</emphasis> have one of the following response
175 * <simplelist id="gtkfilechooserdialog-responses">
176 * <member>#GTK_RESPONSE_ACCEPT</member>
177 * <member>#GTK_RESPONSE_OK</member>
178 * <member>#GTK_RESPONSE_YES</member>
179 * <member>#GTK_RESPONSE_APPLY</member>
182 * This is because #GtkFileChooserDialog must intercept responses
183 * and switch to folders if appropriate, rather than letting the
184 * dialog terminate — the implementation uses these known
185 * response codes to know which responses can be blocked if
189 * To summarize, make sure you use a
190 * <link linkend="gtkfilechooserdialog-responses">stock response code</link>
191 * when you use #GtkFileChooserDialog to ensure proper operation.
198 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o) (GTK_FILE_CHOOSER_DIALOG (o)->priv)
200 static void gtk_file_chooser_dialog_finalize (GObject *object);
202 static GObject* gtk_file_chooser_dialog_constructor (GType type,
203 guint n_construct_properties,
204 GObjectConstructParam *construct_params);
205 static void gtk_file_chooser_dialog_set_property (GObject *object,
209 static void gtk_file_chooser_dialog_get_property (GObject *object,
214 static void gtk_file_chooser_dialog_map (GtkWidget *widget);
216 static void response_cb (GtkDialog *dialog,
219 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDialog, gtk_file_chooser_dialog, GTK_TYPE_DIALOG,
220 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
221 _gtk_file_chooser_delegate_iface_init))
224 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
226 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
227 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
229 gobject_class->constructor = gtk_file_chooser_dialog_constructor;
230 gobject_class->set_property = gtk_file_chooser_dialog_set_property;
231 gobject_class->get_property = gtk_file_chooser_dialog_get_property;
232 gobject_class->finalize = gtk_file_chooser_dialog_finalize;
234 widget_class->map = gtk_file_chooser_dialog_map;
236 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FILE_CHOOSER);
238 _gtk_file_chooser_install_properties (gobject_class);
240 g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
244 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
246 GtkWidget *action_area, *content_area;
247 GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
248 GTK_TYPE_FILE_CHOOSER_DIALOG,
249 GtkFileChooserDialogPrivate);
250 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
253 dialog->priv->response_requested = FALSE;
255 content_area = gtk_dialog_get_content_area (fc_dialog);
256 action_area = gtk_dialog_get_action_area (fc_dialog);
258 gtk_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
259 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
260 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
262 /* We do a signal connection here rather than overriding the method in
263 * class_init because GtkDialog::response is a RUN_LAST signal. We want *our*
264 * handler to be run *first*, regardless of whether the user installs response
265 * handlers of his own.
267 g_signal_connect (dialog, "response",
268 G_CALLBACK (response_cb), NULL);
272 gtk_file_chooser_dialog_finalize (GObject *object)
274 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
276 g_free (dialog->priv->file_system);
278 G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);
282 is_stock_accept_response_id (int response_id)
284 return (response_id == GTK_RESPONSE_ACCEPT
285 || response_id == GTK_RESPONSE_OK
286 || response_id == GTK_RESPONSE_YES
287 || response_id == GTK_RESPONSE_APPLY);
290 /* Callback used when the user activates a file in the file chooser widget */
292 file_chooser_widget_file_activated (GtkFileChooser *chooser,
293 GtkFileChooserDialog *dialog)
295 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
296 GtkWidget *action_area;
299 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
302 /* There probably isn't a default widget, so make things easier for the
303 * programmer by looking for a reasonable button on our own.
305 action_area = gtk_dialog_get_action_area (fc_dialog);
306 children = gtk_container_get_children (GTK_CONTAINER (action_area));
308 for (l = children; l; l = l->next)
313 widget = GTK_WIDGET (l->data);
314 response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
315 if (gtk_widget_is_sensitive (widget) &&
316 is_stock_accept_response_id (response_id))
318 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
323 g_list_free (children);
327 /* FIXME: to see why this function is ifdef-ed out, see the comment below in
328 * file_chooser_widget_default_size_changed().
331 load_position (int *out_xpos, int *out_ypos)
333 GtkFileChooserSettings *settings;
334 int x, y, width, height;
336 settings = _gtk_file_chooser_settings_new ();
337 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
338 g_object_unref (settings);
346 file_chooser_widget_default_size_changed (GtkWidget *widget,
347 GtkFileChooserDialog *dialog)
349 GtkFileChooserDialogPrivate *priv;
350 gint default_width, default_height;
351 GtkRequisition req, widget_req;
353 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
355 /* Unset any previously set size */
356 gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
358 if (gtk_widget_is_drawable (widget))
360 /* Force a size request of everything before we start. This will make sure
361 * that widget->requisition is meaningful. */
362 gtk_widget_get_preferred_size (GTK_WIDGET (dialog),
364 gtk_widget_get_preferred_size (widget,
368 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
369 &default_width, &default_height);
371 gtk_window_resize (GTK_WINDOW (dialog), default_width, default_height);
373 if (!gtk_widget_get_mapped (GTK_WIDGET (dialog)))
376 /* FIXME: the code to restore the position does not work yet. It is not
377 * clear whether it is actually desirable --- if enabled, applications
378 * would not be able to say "center the file chooser on top of my toplevel
379 * window". So, we don't use this code at all.
381 load_position (&xpos, &ypos);
382 if (xpos >= 0 && ypos >= 0)
384 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE);
385 gtk_window_move (GTK_WINDOW (dialog), xpos, ypos);
392 file_chooser_widget_response_requested (GtkWidget *widget,
393 GtkFileChooserDialog *dialog)
395 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
396 GtkWidget *action_area;
399 dialog->priv->response_requested = TRUE;
401 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
404 /* There probably isn't a default widget, so make things easier for the
405 * programmer by looking for a reasonable button on our own.
407 action_area = gtk_dialog_get_action_area (fc_dialog);
408 children = gtk_container_get_children (GTK_CONTAINER (action_area));
410 for (l = children; l; l = l->next)
415 widget = GTK_WIDGET (l->data);
416 response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
417 if (gtk_widget_is_sensitive (widget) &&
418 is_stock_accept_response_id (response_id))
420 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
426 dialog->priv->response_requested = FALSE;
428 g_list_free (children);
432 gtk_file_chooser_dialog_constructor (GType type,
433 guint n_construct_properties,
434 GObjectConstructParam *construct_params)
436 GtkFileChooserDialogPrivate *priv;
437 GtkWidget *content_area;
440 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
441 n_construct_properties,
443 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
445 gtk_widget_push_composite_child ();
447 if (priv->file_system)
448 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
451 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
453 g_signal_connect (priv->widget, "file-activated",
454 G_CALLBACK (file_chooser_widget_file_activated), object);
455 g_signal_connect (priv->widget, "default-size-changed",
456 G_CALLBACK (file_chooser_widget_default_size_changed), object);
457 g_signal_connect (priv->widget, "response-requested",
458 G_CALLBACK (file_chooser_widget_response_requested), object);
460 content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
462 gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
463 gtk_box_pack_start (GTK_BOX (content_area), priv->widget, TRUE, TRUE, 0);
465 gtk_widget_show (priv->widget);
467 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
468 GTK_FILE_CHOOSER (priv->widget));
470 gtk_widget_pop_composite_child ();
476 gtk_file_chooser_dialog_set_property (GObject *object,
482 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
487 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
493 gtk_file_chooser_dialog_get_property (GObject *object,
498 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
500 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
504 foreach_ensure_default_response_cb (GtkWidget *widget,
507 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
510 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
511 if (is_stock_accept_response_id (response_id))
512 gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
516 ensure_default_response (GtkFileChooserDialog *dialog)
518 GtkWidget *action_area;
520 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
521 gtk_container_foreach (GTK_CONTAINER (action_area),
522 foreach_ensure_default_response_cb,
526 /* GtkWidget::map handler */
528 gtk_file_chooser_dialog_map (GtkWidget *widget)
530 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
531 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
533 ensure_default_response (dialog);
535 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
537 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
540 /* GtkDialog::response handler */
542 response_cb (GtkDialog *dialog,
545 GtkFileChooserDialogPrivate *priv;
547 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
549 /* Act only on response IDs we recognize */
550 if (is_stock_accept_response_id (response_id)
551 && !priv->response_requested
552 && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
554 g_signal_stop_emission_by_name (dialog, "response");
557 priv->response_requested = FALSE;
561 gtk_file_chooser_dialog_new_valist (const gchar *title,
563 GtkFileChooserAction action,
564 const gchar *first_button_text,
568 const char *button_text = first_button_text;
571 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
577 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
581 response_id = va_arg (varargs, gint);
582 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
583 button_text = va_arg (varargs, const gchar *);
590 * gtk_file_chooser_dialog_new:
591 * @title: (allow-none): Title of the dialog, or %NULL
592 * @parent: (allow-none): Transient parent of the dialog, or %NULL
593 * @action: Open or save mode for the dialog
594 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
595 * @...: response ID for the first button, then additional (button, id) pairs, ending with %NULL
597 * Creates a new #GtkFileChooserDialog. This function is analogous to
598 * gtk_dialog_new_with_buttons().
600 * Return value: a new #GtkFileChooserDialog
605 gtk_file_chooser_dialog_new (const gchar *title,
607 GtkFileChooserAction action,
608 const gchar *first_button_text,
614 va_start (varargs, first_button_text);
615 result = gtk_file_chooser_dialog_new_valist (title, parent, action,