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.
23 #include "gtkfilechooserprivate.h"
24 #include "gtkfilechooserdialog.h"
25 #include "gtkfilechooserwidget.h"
26 #include "gtkfilechooserutils.h"
27 #include "gtkfilechooserembed.h"
28 #include "gtkfilechoosersettings.h"
29 #include "gtkfilesystem.h"
30 #include "gtktypebuiltins.h"
35 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o) (GTK_FILE_CHOOSER_DIALOG (o)->priv)
37 static void gtk_file_chooser_dialog_finalize (GObject *object);
39 static GObject* gtk_file_chooser_dialog_constructor (GType type,
40 guint n_construct_properties,
41 GObjectConstructParam *construct_params);
42 static void gtk_file_chooser_dialog_set_property (GObject *object,
46 static void gtk_file_chooser_dialog_get_property (GObject *object,
51 static void gtk_file_chooser_dialog_map (GtkWidget *widget);
52 static void gtk_file_chooser_dialog_unmap (GtkWidget *widget);
54 static void response_cb (GtkDialog *dialog,
57 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDialog, gtk_file_chooser_dialog, GTK_TYPE_DIALOG,
58 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
59 _gtk_file_chooser_delegate_iface_init))
62 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
64 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
65 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
67 gobject_class->constructor = gtk_file_chooser_dialog_constructor;
68 gobject_class->set_property = gtk_file_chooser_dialog_set_property;
69 gobject_class->get_property = gtk_file_chooser_dialog_get_property;
70 gobject_class->finalize = gtk_file_chooser_dialog_finalize;
72 widget_class->map = gtk_file_chooser_dialog_map;
73 widget_class->unmap = gtk_file_chooser_dialog_unmap;
75 _gtk_file_chooser_install_properties (gobject_class);
77 g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
81 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
83 GtkWidget *action_area, *content_area;
84 GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
85 GTK_TYPE_FILE_CHOOSER_DIALOG,
86 GtkFileChooserDialogPrivate);
87 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
90 dialog->priv->response_requested = FALSE;
92 content_area = gtk_dialog_get_content_area (fc_dialog);
93 action_area = gtk_dialog_get_action_area (fc_dialog);
95 gtk_dialog_set_has_separator (fc_dialog, FALSE);
96 gtk_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
97 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
98 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
100 /* We do a signal connection here rather than overriding the method in
101 * class_init because GtkDialog::response is a RUN_LAST signal. We want *our*
102 * handler to be run *first*, regardless of whether the user installs response
103 * handlers of his own.
105 g_signal_connect (dialog, "response",
106 G_CALLBACK (response_cb), NULL);
110 gtk_file_chooser_dialog_finalize (GObject *object)
112 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
114 g_free (dialog->priv->file_system);
116 G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);
120 is_stock_accept_response_id (int response_id)
122 return (response_id == GTK_RESPONSE_ACCEPT
123 || response_id == GTK_RESPONSE_OK
124 || response_id == GTK_RESPONSE_YES
125 || response_id == GTK_RESPONSE_APPLY);
128 /* Callback used when the user activates a file in the file chooser widget */
130 file_chooser_widget_file_activated (GtkFileChooser *chooser,
131 GtkFileChooserDialog *dialog)
133 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
134 GtkWidget *action_area;
137 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
140 /* There probably isn't a default widget, so make things easier for the
141 * programmer by looking for a reasonable button on our own.
143 action_area = gtk_dialog_get_action_area (fc_dialog);
144 children = gtk_container_get_children (GTK_CONTAINER (action_area));
146 for (l = children; l; l = l->next)
151 widget = GTK_WIDGET (l->data);
152 response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
153 if (gtk_widget_is_sensitive (widget) &&
154 is_stock_accept_response_id (response_id))
156 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
161 g_list_free (children);
165 /* FIXME: to see why this function is ifdef-ed out, see the comment below in
166 * file_chooser_widget_default_size_changed().
169 load_position (int *out_xpos, int *out_ypos)
171 GtkFileChooserSettings *settings;
172 int x, y, width, height;
174 settings = _gtk_file_chooser_settings_new ();
175 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
176 g_object_unref (settings);
184 file_chooser_widget_default_size_changed (GtkWidget *widget,
185 GtkFileChooserDialog *dialog)
187 GtkFileChooserDialogPrivate *priv;
188 gint default_width, default_height;
189 GtkRequisition req, widget_req;
191 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
193 /* Unset any previously set size */
194 gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
196 if (gtk_widget_is_drawable (widget))
198 /* Force a size request of everything before we start. This will make sure
199 * that widget->requisition is meaningful. */
200 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
201 gtk_widget_size_request (widget, &widget_req);
204 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
205 &default_width, &default_height);
207 gtk_window_resize (GTK_WINDOW (dialog), default_width, default_height);
209 if (!gtk_widget_get_mapped (GTK_WIDGET (dialog)))
212 /* FIXME: the code to restore the position does not work yet. It is not
213 * clear whether it is actually desirable --- if enabled, applications
214 * would not be able to say "center the file chooser on top of my toplevel
215 * window". So, we don't use this code at all.
217 load_position (&xpos, &ypos);
218 if (xpos >= 0 && ypos >= 0)
220 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE);
221 gtk_window_move (GTK_WINDOW (dialog), xpos, ypos);
228 file_chooser_widget_response_requested (GtkWidget *widget,
229 GtkFileChooserDialog *dialog)
231 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
232 GtkWidget *action_area;
235 dialog->priv->response_requested = TRUE;
237 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
240 /* There probably isn't a default widget, so make things easier for the
241 * programmer by looking for a reasonable button on our own.
243 action_area = gtk_dialog_get_action_area (fc_dialog);
244 children = gtk_container_get_children (GTK_CONTAINER (action_area));
246 for (l = children; l; l = l->next)
251 widget = GTK_WIDGET (l->data);
252 response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
253 if (gtk_widget_is_sensitive (widget) &&
254 is_stock_accept_response_id (response_id))
256 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
262 dialog->priv->response_requested = FALSE;
264 g_list_free (children);
268 gtk_file_chooser_dialog_constructor (GType type,
269 guint n_construct_properties,
270 GObjectConstructParam *construct_params)
272 GtkFileChooserDialogPrivate *priv;
273 GtkWidget *content_area;
276 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
277 n_construct_properties,
279 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
281 gtk_widget_push_composite_child ();
283 if (priv->file_system)
284 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
287 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
289 g_signal_connect (priv->widget, "file-activated",
290 G_CALLBACK (file_chooser_widget_file_activated), object);
291 g_signal_connect (priv->widget, "default-size-changed",
292 G_CALLBACK (file_chooser_widget_default_size_changed), object);
293 g_signal_connect (priv->widget, "response-requested",
294 G_CALLBACK (file_chooser_widget_response_requested), object);
296 content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
298 gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
299 gtk_box_pack_start (GTK_BOX (content_area), priv->widget, TRUE, TRUE, 0);
301 gtk_widget_show (priv->widget);
303 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
304 GTK_FILE_CHOOSER (priv->widget));
306 gtk_widget_pop_composite_child ();
312 gtk_file_chooser_dialog_set_property (GObject *object,
318 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
323 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
329 gtk_file_chooser_dialog_get_property (GObject *object,
334 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
336 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
340 foreach_ensure_default_response_cb (GtkWidget *widget,
343 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
346 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
347 if (is_stock_accept_response_id (response_id))
348 gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
352 ensure_default_response (GtkFileChooserDialog *dialog)
354 GtkWidget *action_area;
356 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
357 gtk_container_foreach (GTK_CONTAINER (action_area),
358 foreach_ensure_default_response_cb,
362 /* GtkWidget::map handler */
364 gtk_file_chooser_dialog_map (GtkWidget *widget)
366 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
367 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
369 ensure_default_response (dialog);
371 if (!gtk_widget_get_mapped (priv->widget))
372 gtk_widget_map (priv->widget);
374 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
376 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
379 /* GtkWidget::unmap handler */
381 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
383 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
384 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
386 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
388 /* See bug #145470. We unmap the GtkFileChooserWidget so that if the dialog
389 * is remapped, the widget will be remapped as well. Implementations should
390 * refresh their contents when this happens, as some applications keep a
391 * single file chooser alive and map/unmap it as needed, rather than creating
392 * a new file chooser every time they need one.
394 gtk_widget_unmap (priv->widget);
397 /* GtkDialog::response handler */
399 response_cb (GtkDialog *dialog,
402 GtkFileChooserDialogPrivate *priv;
404 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
406 /* Act only on response IDs we recognize */
407 if (is_stock_accept_response_id (response_id)
408 && !priv->response_requested
409 && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
411 g_signal_stop_emission_by_name (dialog, "response");
414 priv->response_requested = FALSE;
418 gtk_file_chooser_dialog_new_valist (const gchar *title,
420 GtkFileChooserAction action,
421 const gchar *first_button_text,
425 const char *button_text = first_button_text;
428 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
434 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
438 response_id = va_arg (varargs, gint);
439 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
440 button_text = va_arg (varargs, const gchar *);
447 * gtk_file_chooser_dialog_new:
448 * @title: (allow-none): Title of the dialog, or %NULL
449 * @parent: (allow-none): Transient parent of the dialog, or %NULL
450 * @action: Open or save mode for the dialog
451 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
452 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
454 * Creates a new #GtkFileChooserDialog. This function is analogous to
455 * gtk_dialog_new_with_buttons().
457 * Return value: a new #GtkFileChooserDialog
462 gtk_file_chooser_dialog_new (const gchar *title,
464 GtkFileChooserAction action,
465 const gchar *first_button_text,
471 va_start (varargs, first_button_text);
472 result = gtk_file_chooser_dialog_new_valist (title, parent, action,