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"
36 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o) (GTK_FILE_CHOOSER_DIALOG (o)->priv)
38 static void gtk_file_chooser_dialog_finalize (GObject *object);
40 static GObject* gtk_file_chooser_dialog_constructor (GType type,
41 guint n_construct_properties,
42 GObjectConstructParam *construct_params);
43 static void gtk_file_chooser_dialog_set_property (GObject *object,
47 static void gtk_file_chooser_dialog_get_property (GObject *object,
52 static void gtk_file_chooser_dialog_map (GtkWidget *widget);
53 static void gtk_file_chooser_dialog_unmap (GtkWidget *widget);
55 static void response_cb (GtkDialog *dialog,
58 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDialog, gtk_file_chooser_dialog, GTK_TYPE_DIALOG,
59 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
60 _gtk_file_chooser_delegate_iface_init))
63 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
65 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
66 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
68 gobject_class->constructor = gtk_file_chooser_dialog_constructor;
69 gobject_class->set_property = gtk_file_chooser_dialog_set_property;
70 gobject_class->get_property = gtk_file_chooser_dialog_get_property;
71 gobject_class->finalize = gtk_file_chooser_dialog_finalize;
73 widget_class->map = gtk_file_chooser_dialog_map;
74 widget_class->unmap = gtk_file_chooser_dialog_unmap;
76 _gtk_file_chooser_install_properties (gobject_class);
78 g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
82 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
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 gtk_dialog_set_has_separator (fc_dialog, FALSE);
93 gtk_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
94 gtk_box_set_spacing (GTK_BOX (fc_dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
95 gtk_container_set_border_width (GTK_CONTAINER (fc_dialog->action_area), 5);
97 /* We do a signal connection here rather than overriding the method in
98 * class_init because GtkDialog::response is a RUN_LAST signal. We want *our*
99 * handler to be run *first*, regardless of whether the user installs response
100 * handlers of his own.
102 g_signal_connect (dialog, "response",
103 G_CALLBACK (response_cb), NULL);
107 gtk_file_chooser_dialog_finalize (GObject *object)
109 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
111 g_free (dialog->priv->file_system);
113 G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);
117 is_stock_accept_response_id (int response_id)
119 return (response_id == GTK_RESPONSE_ACCEPT
120 || response_id == GTK_RESPONSE_OK
121 || response_id == GTK_RESPONSE_YES
122 || response_id == GTK_RESPONSE_APPLY);
125 /* Callback used when the user activates a file in the file chooser widget */
127 file_chooser_widget_file_activated (GtkFileChooser *chooser,
128 GtkFileChooserDialog *dialog)
132 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
135 /* There probably isn't a default widget, so make things easier for the
136 * programmer by looking for a reasonable button on our own.
139 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
141 for (l = children; l; l = l->next)
146 widget = GTK_WIDGET (l->data);
147 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
148 if (is_stock_accept_response_id (response_id))
150 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
155 g_list_free (children);
159 /* FIXME: to see why this function is ifdef-ed out, see the comment below in
160 * file_chooser_widget_default_size_changed().
163 load_position (int *out_xpos, int *out_ypos)
165 GtkFileChooserSettings *settings;
166 int x, y, width, height;
168 settings = _gtk_file_chooser_settings_new ();
169 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
170 g_object_unref (settings);
178 file_chooser_widget_default_size_changed (GtkWidget *widget,
179 GtkFileChooserDialog *dialog)
181 GtkFileChooserDialogPrivate *priv;
182 gint default_width, default_height;
183 GtkRequisition req, widget_req;
186 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
188 /* Unset any previously set size */
189 gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
191 if (GTK_WIDGET_DRAWABLE (widget))
193 /* Force a size request of everything before we start. This will make sure
194 * that widget->requisition is meaningful. */
195 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
196 gtk_widget_size_request (widget, &widget_req);
199 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
200 &default_width, &default_height);
202 gtk_window_resize (GTK_WINDOW (dialog), default_width, default_height);
204 if (!GTK_WIDGET_MAPPED (dialog))
207 /* FIXME: the code to restore the position does not work yet. It is not
208 * clear whether it is actually desirable --- if enabled, applications
209 * would not be able to say "center the file chooser on top of my toplevel
210 * window". So, we don't use this code at all.
212 load_position (&xpos, &ypos);
213 if (xpos >= 0 && ypos >= 0)
215 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE);
216 gtk_window_move (GTK_WINDOW (dialog), xpos, ypos);
223 file_chooser_widget_response_requested (GtkWidget *widget,
224 GtkFileChooserDialog *dialog)
228 /* There probably isn't a default widget, so make things easier for the
229 * programmer by looking for a reasonable button on our own.
232 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
234 for (l = children; l; l = l->next)
239 widget = GTK_WIDGET (l->data);
240 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
241 if (is_stock_accept_response_id (response_id))
243 dialog->priv->response_requested = TRUE;
244 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
249 g_list_free (children);
253 gtk_file_chooser_dialog_constructor (GType type,
254 guint n_construct_properties,
255 GObjectConstructParam *construct_params)
257 GtkFileChooserDialogPrivate *priv;
260 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
261 n_construct_properties,
263 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
265 gtk_widget_push_composite_child ();
267 if (priv->file_system)
268 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
269 "file-system-backend", priv->file_system,
272 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
274 g_signal_connect (priv->widget, "file-activated",
275 G_CALLBACK (file_chooser_widget_file_activated), object);
276 g_signal_connect (priv->widget, "default-size-changed",
277 G_CALLBACK (file_chooser_widget_default_size_changed), object);
278 g_signal_connect (priv->widget, "response-requested",
279 G_CALLBACK (file_chooser_widget_response_requested), object);
281 gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
282 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
284 gtk_widget_show (priv->widget);
286 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
287 GTK_FILE_CHOOSER (priv->widget));
289 gtk_widget_pop_composite_child ();
295 gtk_file_chooser_dialog_set_property (GObject *object,
301 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
305 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
306 g_free (priv->file_system);
307 priv->file_system = g_value_dup_string (value);
310 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
316 gtk_file_chooser_dialog_get_property (GObject *object,
321 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
323 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
327 foreach_ensure_default_response_cb (GtkWidget *widget,
330 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
333 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
334 if (is_stock_accept_response_id (response_id))
335 gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
339 ensure_default_response (GtkFileChooserDialog *dialog)
341 gtk_container_foreach (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area),
342 foreach_ensure_default_response_cb,
346 /* GtkWidget::map handler */
348 gtk_file_chooser_dialog_map (GtkWidget *widget)
350 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
351 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
353 ensure_default_response (dialog);
355 if (!GTK_WIDGET_MAPPED (priv->widget))
356 gtk_widget_map (priv->widget);
358 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
360 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
363 /* GtkWidget::unmap handler */
365 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
367 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
368 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
370 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
372 /* See bug #145470. We unmap the GtkFileChooserWidget so that if the dialog
373 * is remapped, the widget will be remapped as well. Implementations should
374 * refresh their contents when this happens, as some applications keep a
375 * single file chooser alive and map/unmap it as needed, rather than creating
376 * a new file chooser every time they need one.
378 gtk_widget_unmap (priv->widget);
381 /* GtkDialog::response handler */
383 response_cb (GtkDialog *dialog,
386 GtkFileChooserDialogPrivate *priv;
388 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
390 /* Act only on response IDs we recognize */
391 if (is_stock_accept_response_id (response_id)
392 && !priv->response_requested
393 && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
395 g_signal_stop_emission_by_name (dialog, "response");
398 priv->response_requested = FALSE;
402 gtk_file_chooser_dialog_new_valist (const gchar *title,
404 GtkFileChooserAction action,
405 const gchar *backend,
406 const gchar *first_button_text,
410 const char *button_text = first_button_text;
413 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
419 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
423 response_id = va_arg (varargs, gint);
424 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
425 button_text = va_arg (varargs, const gchar *);
432 * gtk_file_chooser_dialog_new:
433 * @title: Title of the dialog, or %NULL
434 * @parent: Transient parent of the dialog, or %NULL
435 * @action: Open or save mode for the dialog
436 * @first_button_text: stock ID or text to go in the first button, or %NULL
437 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
439 * Creates a new #GtkFileChooserDialog. This function is analogous to
440 * gtk_dialog_new_with_buttons().
442 * Return value: a new #GtkFileChooserDialog
447 gtk_file_chooser_dialog_new (const gchar *title,
449 GtkFileChooserAction action,
450 const gchar *first_button_text,
456 va_start (varargs, first_button_text);
457 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
458 NULL, first_button_text,
466 * gtk_file_chooser_dialog_new_with_backend:
467 * @title: Title of the dialog, or %NULL
468 * @parent: Transient parent of the dialog, or %NULL
469 * @action: Open or save mode for the dialog
470 * @backend: The name of the specific filesystem backend to use.
471 * @first_button_text: stock ID or text to go in the first button, or %NULL
472 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
474 * Creates a new #GtkFileChooserDialog with a specified backend. This is
475 * especially useful if you use gtk_file_chooser_set_local_only() to allow
476 * non-local files and you use a more expressive vfs, such as gnome-vfs,
479 * Return value: a new #GtkFileChooserDialog
485 gtk_file_chooser_dialog_new_with_backend (const gchar *title,
487 GtkFileChooserAction action,
488 const gchar *backend,
489 const gchar *first_button_text,
495 va_start (varargs, first_button_text);
496 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
497 backend, first_button_text,
504 #define __GTK_FILE_CHOOSER_DIALOG_C__
505 #include "gtkaliasdef.c"