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, see <http://www.gnu.org/licenses/>.
22 #include "gtkfilechooserdialog.h"
24 #include "gtkfilechooserprivate.h"
25 #include "gtkfilechooserwidget.h"
26 #include "gtkfilechooserutils.h"
27 #include "gtkfilechooserembed.h"
28 #include "gtkfilesystem.h"
29 #include "gtksizerequest.h"
30 #include "gtktypebuiltins.h"
37 * SECTION:gtkfilechooserdialog
38 * @Short_description: A file chooser dialog, suitable for "File/Open" or "File/Save" commands
39 * @Title: GtkFileChooserDialog
40 * @See_also: #GtkFileChooser, #GtkDialog
42 * #GtkFileChooserDialog is a dialog box suitable for use with
43 * "File/Open" or "File/Save as" commands. This widget works by
44 * putting a #GtkFileChooserWidget inside a #GtkDialog. It exposes
45 * the #GtkFileChooser interface, so you can use all of the
46 * #GtkFileChooser functions on the file chooser dialog as well as
47 * those for #GtkDialog.
49 * Note that #GtkFileChooserDialog does not have any methods of its
50 * own. Instead, you should use the functions that work on a
53 * <example id="gtkfilechooser-typical-usage">
54 * <title>Typical usage</title>
55 * In the simplest of cases, you can the following code to use
56 * #GtkFileChooserDialog to select a file for opening:
58 * <informalexample><programlisting>
61 * dialog = gtk_file_chooser_dialog_new ("Open File",
63 * GTK_FILE_CHOOSER_ACTION_OPEN,
64 * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
65 * GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
68 * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
72 * filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
73 * open_file (filename);
77 * gtk_widget_destroy (dialog);
78 * </programlisting></informalexample>
80 * To use a dialog for saving, you can use this:
82 * <informalexample><programlisting>
85 * dialog = gtk_file_chooser_dialog_new ("Save File",
87 * GTK_FILE_CHOOSER_ACTION_SAVE,
88 * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
89 * GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
91 * gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
93 * if (user_edited_a_new_document)
94 * gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), "Untitled document");
96 * gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), filename_for_existing_document);
98 * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
102 * filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
103 * save_to_file (filename);
107 * gtk_widget_destroy (dialog);
108 * </programlisting></informalexample>
111 * <section id="gtkfilechooserdialog-setting-up">
112 * <title>Setting up a file chooser dialog</title>
113 * There are various cases in which you may need to use a #GtkFileChooserDialog:
114 * <itemizedlist><listitem>To select a file for opening, as for a
115 * <guimenuitem>File/Open</guimenuitem> command. Use
116 * #GTK_FILE_CHOOSER_ACTION_OPEN.
118 * <listitem>To save a file for the first time, as for a
119 * <guimenuitem>File/Save</guimenuitem> command. Use
120 * #GTK_FILE_CHOOSER_ACTION_SAVE, and suggest a name such as
121 * "Untitled" with gtk_file_chooser_set_current_name().
123 * <listitem>To save a file under a different name, as for a
124 * <guimenuitem>File/Save As</guimenuitem> command. Use
125 * #GTK_FILE_CHOOSER_ACTION_SAVE, and set the existing filename
126 * with gtk_file_chooser_set_filename().
128 * <listitem>To choose a folder instead of a file. Use
129 * #GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
130 * </listitem></itemizedlist>
133 * Old versions of the file chooser's documentation suggested
134 * using gtk_file_chooser_set_current_folder() in various
135 * situations, with the intention of letting the application
136 * suggest a reasonable default folder. This is no longer
137 * considered to be a good policy, as now the file chooser is
138 * able to make good suggestions on its own. In general, you
139 * should only cause the file chooser to show a specific folder
140 * when it is appropriate to use gtk_file_chooser_set_filename(),
141 * i.e. when you are doing a <guimenuitem>File/Save
142 * As</guimenuitem> command <emphasis>and</emphasis> you already
143 * have a file saved somewhere.
147 * <section id="gtkfilechooserdialog-response-codes">
148 * <title>Response Codes</title>
149 * #GtkFileChooserDialog inherits from #GtkDialog, so buttons that
150 * go in its action area have response codes such as
151 * #GTK_RESPONSE_ACCEPT and #GTK_RESPONSE_CANCEL. For example, you
152 * could call gtk_file_chooser_dialog_new() as follows:
154 * <informalexample><programlisting>
157 * dialog = gtk_file_chooser_dialog_new ("Open File",
159 * GTK_FILE_CHOOSER_ACTION_OPEN,
160 * GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
161 * GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
163 * </programlisting></informalexample>
165 * This will create buttons for "Cancel" and "Open" that use stock
166 * response identifiers from #GtkResponseType. For most dialog
167 * boxes you can use your own custom response codes rather than the
168 * ones in #GtkResponseType, but #GtkFileChooserDialog assumes that
169 * its "accept"-type action, e.g. an "Open" or "Save" button,
170 * <emphasis>will</emphasis> have one of the following response
173 * <simplelist id="gtkfilechooserdialog-responses">
174 * <member>#GTK_RESPONSE_ACCEPT</member>
175 * <member>#GTK_RESPONSE_OK</member>
176 * <member>#GTK_RESPONSE_YES</member>
177 * <member>#GTK_RESPONSE_APPLY</member>
180 * This is because #GtkFileChooserDialog must intercept responses
181 * and switch to folders if appropriate, rather than letting the
182 * dialog terminate — the implementation uses these known
183 * response codes to know which responses can be blocked if
187 * To summarize, make sure you use a
188 * <link linkend="gtkfilechooserdialog-responses">stock response code</link>
189 * when you use #GtkFileChooserDialog to ensure proper operation.
196 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o) (GTK_FILE_CHOOSER_DIALOG (o)->priv)
198 static void gtk_file_chooser_dialog_finalize (GObject *object);
200 static GObject* gtk_file_chooser_dialog_constructor (GType type,
201 guint n_construct_properties,
202 GObjectConstructParam *construct_params);
203 static void gtk_file_chooser_dialog_set_property (GObject *object,
207 static void gtk_file_chooser_dialog_get_property (GObject *object,
212 static void gtk_file_chooser_dialog_map (GtkWidget *widget);
213 static void gtk_file_chooser_dialog_unmap (GtkWidget *widget);
215 static void response_cb (GtkDialog *dialog,
218 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDialog, gtk_file_chooser_dialog, GTK_TYPE_DIALOG,
219 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
220 _gtk_file_chooser_delegate_iface_init))
223 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
225 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
226 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
228 gobject_class->constructor = gtk_file_chooser_dialog_constructor;
229 gobject_class->set_property = gtk_file_chooser_dialog_set_property;
230 gobject_class->get_property = gtk_file_chooser_dialog_get_property;
231 gobject_class->finalize = gtk_file_chooser_dialog_finalize;
233 widget_class->map = gtk_file_chooser_dialog_map;
234 widget_class->unmap = gtk_file_chooser_dialog_unmap;
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 gtk_window_set_role (GTK_WINDOW (dialog), "GtkFileChooserDialog");
264 /* We do a signal connection here rather than overriding the method in
265 * class_init because GtkDialog::response is a RUN_LAST signal. We want *our*
266 * handler to be run *first*, regardless of whether the user installs response
267 * handlers of his own.
269 g_signal_connect (dialog, "response",
270 G_CALLBACK (response_cb), NULL);
274 gtk_file_chooser_dialog_finalize (GObject *object)
276 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
278 g_free (dialog->priv->file_system);
280 G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);
284 is_stock_accept_response_id (int response_id)
286 return (response_id == GTK_RESPONSE_ACCEPT
287 || response_id == GTK_RESPONSE_OK
288 || response_id == GTK_RESPONSE_YES
289 || response_id == GTK_RESPONSE_APPLY);
292 /* Callback used when the user activates a file in the file chooser widget */
294 file_chooser_widget_file_activated (GtkFileChooser *chooser,
295 GtkFileChooserDialog *dialog)
297 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
298 GtkWidget *action_area;
301 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
304 /* There probably isn't a default widget, so make things easier for the
305 * programmer by looking for a reasonable button on our own.
307 action_area = gtk_dialog_get_action_area (fc_dialog);
308 children = gtk_container_get_children (GTK_CONTAINER (action_area));
310 for (l = children; l; l = l->next)
315 widget = GTK_WIDGET (l->data);
316 response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
317 if (gtk_widget_is_sensitive (widget) &&
318 is_stock_accept_response_id (response_id))
320 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
325 g_list_free (children);
329 /* FIXME: to see why this function is ifdef-ed out, see the comment below in
330 * file_chooser_widget_default_size_changed().
333 load_position (int *out_xpos, int *out_ypos)
335 GtkFileChooserSettings *settings;
336 int x, y, width, height;
338 settings = _gtk_file_chooser_settings_new ();
339 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
340 g_object_unref (settings);
348 file_chooser_widget_default_size_changed (GtkWidget *widget,
349 GtkFileChooserDialog *dialog)
351 GtkFileChooserDialogPrivate *priv;
352 gint default_width, default_height;
353 GtkRequisition req, widget_req;
355 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
357 /* Unset any previously set size */
358 gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
360 if (gtk_widget_is_drawable (widget))
362 /* Force a size request of everything before we start. This will make sure
363 * that widget->requisition is meaningful. */
364 gtk_widget_get_preferred_size (GTK_WIDGET (dialog),
366 gtk_widget_get_preferred_size (widget,
370 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
371 &default_width, &default_height);
373 gtk_window_resize (GTK_WINDOW (dialog), default_width, default_height);
375 if (!gtk_widget_get_mapped (GTK_WIDGET (dialog)))
378 /* FIXME: the code to restore the position does not work yet. It is not
379 * clear whether it is actually desirable --- if enabled, applications
380 * would not be able to say "center the file chooser on top of my toplevel
381 * window". So, we don't use this code at all.
383 load_position (&xpos, &ypos);
384 if (xpos >= 0 && ypos >= 0)
386 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE);
387 gtk_window_move (GTK_WINDOW (dialog), xpos, ypos);
394 file_chooser_widget_response_requested (GtkWidget *widget,
395 GtkFileChooserDialog *dialog)
397 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
398 GtkWidget *action_area;
401 dialog->priv->response_requested = TRUE;
403 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
406 /* There probably isn't a default widget, so make things easier for the
407 * programmer by looking for a reasonable button on our own.
409 action_area = gtk_dialog_get_action_area (fc_dialog);
410 children = gtk_container_get_children (GTK_CONTAINER (action_area));
412 for (l = children; l; l = l->next)
417 widget = GTK_WIDGET (l->data);
418 response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
419 if (gtk_widget_is_sensitive (widget) &&
420 is_stock_accept_response_id (response_id))
422 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
428 dialog->priv->response_requested = FALSE;
430 g_list_free (children);
434 gtk_file_chooser_dialog_constructor (GType type,
435 guint n_construct_properties,
436 GObjectConstructParam *construct_params)
438 GtkFileChooserDialogPrivate *priv;
439 GtkWidget *content_area;
442 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
443 n_construct_properties,
445 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
447 gtk_widget_push_composite_child ();
449 if (priv->file_system)
450 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
453 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
455 g_signal_connect (priv->widget, "file-activated",
456 G_CALLBACK (file_chooser_widget_file_activated), object);
457 g_signal_connect (priv->widget, "default-size-changed",
458 G_CALLBACK (file_chooser_widget_default_size_changed), object);
459 g_signal_connect (priv->widget, "response-requested",
460 G_CALLBACK (file_chooser_widget_response_requested), object);
462 content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
464 gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
465 gtk_box_pack_start (GTK_BOX (content_area), priv->widget, TRUE, TRUE, 0);
467 gtk_widget_show (priv->widget);
469 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
470 GTK_FILE_CHOOSER (priv->widget));
472 gtk_widget_pop_composite_child ();
478 gtk_file_chooser_dialog_set_property (GObject *object,
484 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
489 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
495 gtk_file_chooser_dialog_get_property (GObject *object,
500 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
502 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
506 foreach_ensure_default_response_cb (GtkWidget *widget,
509 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
512 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
513 if (is_stock_accept_response_id (response_id))
514 gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
518 ensure_default_response (GtkFileChooserDialog *dialog)
520 GtkWidget *action_area;
522 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
523 gtk_container_foreach (GTK_CONTAINER (action_area),
524 foreach_ensure_default_response_cb,
528 /* GtkWidget::map handler */
530 gtk_file_chooser_dialog_map (GtkWidget *widget)
532 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
533 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
535 ensure_default_response (dialog);
537 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
539 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
543 save_dialog_geometry (GtkFileChooserDialog *dialog)
547 int x, y, width, height;
549 settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (dialog));
551 window = GTK_WINDOW (dialog);
553 gtk_window_get_position (window, &x, &y);
554 gtk_window_get_size (window, &width, &height);
556 g_settings_set (settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", x, y);
557 g_settings_set (settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", width, height);
560 /* GtkWidget::unmap handler */
562 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
564 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
566 save_dialog_geometry (dialog);
568 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
571 /* GtkDialog::response handler */
573 response_cb (GtkDialog *dialog,
576 GtkFileChooserDialogPrivate *priv;
578 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
580 /* Act only on response IDs we recognize */
581 if (is_stock_accept_response_id (response_id)
582 && !priv->response_requested
583 && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
585 g_signal_stop_emission_by_name (dialog, "response");
588 priv->response_requested = FALSE;
592 gtk_file_chooser_dialog_new_valist (const gchar *title,
594 GtkFileChooserAction action,
595 const gchar *first_button_text,
599 const char *button_text = first_button_text;
602 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
608 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
612 response_id = va_arg (varargs, gint);
613 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
614 button_text = va_arg (varargs, const gchar *);
621 * gtk_file_chooser_dialog_new:
622 * @title: (allow-none): Title of the dialog, or %NULL
623 * @parent: (allow-none): Transient parent of the dialog, or %NULL
624 * @action: Open or save mode for the dialog
625 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
626 * @...: response ID for the first button, then additional (button, id) pairs, ending with %NULL
628 * Creates a new #GtkFileChooserDialog. This function is analogous to
629 * gtk_dialog_new_with_buttons().
631 * Return value: a new #GtkFileChooserDialog
636 gtk_file_chooser_dialog_new (const gchar *title,
638 GtkFileChooserAction action,
639 const gchar *first_button_text,
645 va_start (varargs, first_button_text);
646 result = gtk_file_chooser_dialog_new_valist (title, parent, action,