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"
37 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o) (GTK_FILE_CHOOSER_DIALOG (o)->priv)
39 static void gtk_file_chooser_dialog_finalize (GObject *object);
41 static GObject* gtk_file_chooser_dialog_constructor (GType type,
42 guint n_construct_properties,
43 GObjectConstructParam *construct_params);
44 static void gtk_file_chooser_dialog_set_property (GObject *object,
48 static void gtk_file_chooser_dialog_get_property (GObject *object,
53 static void gtk_file_chooser_dialog_map (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;
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_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
96 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
97 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
99 /* We do a signal connection here rather than overriding the method in
100 * class_init because GtkDialog::response is a RUN_LAST signal. We want *our*
101 * handler to be run *first*, regardless of whether the user installs response
102 * handlers of his own.
104 g_signal_connect (dialog, "response",
105 G_CALLBACK (response_cb), NULL);
109 gtk_file_chooser_dialog_finalize (GObject *object)
111 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
113 g_free (dialog->priv->file_system);
115 G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);
119 is_stock_accept_response_id (int response_id)
121 return (response_id == GTK_RESPONSE_ACCEPT
122 || response_id == GTK_RESPONSE_OK
123 || response_id == GTK_RESPONSE_YES
124 || response_id == GTK_RESPONSE_APPLY);
127 /* Callback used when the user activates a file in the file chooser widget */
129 file_chooser_widget_file_activated (GtkFileChooser *chooser,
130 GtkFileChooserDialog *dialog)
132 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
133 GtkWidget *action_area;
136 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
139 /* There probably isn't a default widget, so make things easier for the
140 * programmer by looking for a reasonable button on our own.
142 action_area = gtk_dialog_get_action_area (fc_dialog);
143 children = gtk_container_get_children (GTK_CONTAINER (action_area));
145 for (l = children; l; l = l->next)
150 widget = GTK_WIDGET (l->data);
151 response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
152 if (gtk_widget_is_sensitive (widget) &&
153 is_stock_accept_response_id (response_id))
155 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
160 g_list_free (children);
164 /* FIXME: to see why this function is ifdef-ed out, see the comment below in
165 * file_chooser_widget_default_size_changed().
168 load_position (int *out_xpos, int *out_ypos)
170 GtkFileChooserSettings *settings;
171 int x, y, width, height;
173 settings = _gtk_file_chooser_settings_new ();
174 _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
175 g_object_unref (settings);
183 file_chooser_widget_default_size_changed (GtkWidget *widget,
184 GtkFileChooserDialog *dialog)
186 GtkFileChooserDialogPrivate *priv;
187 gint default_width, default_height;
188 GtkRequisition req, widget_req;
190 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
192 /* Unset any previously set size */
193 gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
195 if (gtk_widget_is_drawable (widget))
197 /* Force a size request of everything before we start. This will make sure
198 * that widget->requisition is meaningful. */
199 gtk_widget_get_preferred_size (GTK_WIDGET (dialog),
201 gtk_widget_get_preferred_size (widget,
205 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
206 &default_width, &default_height);
208 gtk_window_resize (GTK_WINDOW (dialog), default_width, default_height);
210 if (!gtk_widget_get_mapped (GTK_WIDGET (dialog)))
213 /* FIXME: the code to restore the position does not work yet. It is not
214 * clear whether it is actually desirable --- if enabled, applications
215 * would not be able to say "center the file chooser on top of my toplevel
216 * window". So, we don't use this code at all.
218 load_position (&xpos, &ypos);
219 if (xpos >= 0 && ypos >= 0)
221 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE);
222 gtk_window_move (GTK_WINDOW (dialog), xpos, ypos);
229 file_chooser_widget_response_requested (GtkWidget *widget,
230 GtkFileChooserDialog *dialog)
232 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
233 GtkWidget *action_area;
236 dialog->priv->response_requested = TRUE;
238 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
241 /* There probably isn't a default widget, so make things easier for the
242 * programmer by looking for a reasonable button on our own.
244 action_area = gtk_dialog_get_action_area (fc_dialog);
245 children = gtk_container_get_children (GTK_CONTAINER (action_area));
247 for (l = children; l; l = l->next)
252 widget = GTK_WIDGET (l->data);
253 response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
254 if (gtk_widget_is_sensitive (widget) &&
255 is_stock_accept_response_id (response_id))
257 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
263 dialog->priv->response_requested = FALSE;
265 g_list_free (children);
269 gtk_file_chooser_dialog_constructor (GType type,
270 guint n_construct_properties,
271 GObjectConstructParam *construct_params)
273 GtkFileChooserDialogPrivate *priv;
274 GtkWidget *content_area;
277 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
278 n_construct_properties,
280 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
282 gtk_widget_push_composite_child ();
284 if (priv->file_system)
285 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
288 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
290 g_signal_connect (priv->widget, "file-activated",
291 G_CALLBACK (file_chooser_widget_file_activated), object);
292 g_signal_connect (priv->widget, "default-size-changed",
293 G_CALLBACK (file_chooser_widget_default_size_changed), object);
294 g_signal_connect (priv->widget, "response-requested",
295 G_CALLBACK (file_chooser_widget_response_requested), object);
297 content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
299 gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
300 gtk_box_pack_start (GTK_BOX (content_area), priv->widget, TRUE, TRUE, 0);
302 gtk_widget_show (priv->widget);
304 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
305 GTK_FILE_CHOOSER (priv->widget));
307 gtk_widget_pop_composite_child ();
313 gtk_file_chooser_dialog_set_property (GObject *object,
319 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
324 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
330 gtk_file_chooser_dialog_get_property (GObject *object,
335 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
337 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
341 foreach_ensure_default_response_cb (GtkWidget *widget,
344 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
347 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
348 if (is_stock_accept_response_id (response_id))
349 gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
353 ensure_default_response (GtkFileChooserDialog *dialog)
355 GtkWidget *action_area;
357 action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
358 gtk_container_foreach (GTK_CONTAINER (action_area),
359 foreach_ensure_default_response_cb,
363 /* GtkWidget::map handler */
365 gtk_file_chooser_dialog_map (GtkWidget *widget)
367 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
368 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
370 ensure_default_response (dialog);
372 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
374 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
377 /* GtkDialog::response handler */
379 response_cb (GtkDialog *dialog,
382 GtkFileChooserDialogPrivate *priv;
384 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
386 /* Act only on response IDs we recognize */
387 if (is_stock_accept_response_id (response_id)
388 && !priv->response_requested
389 && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
391 g_signal_stop_emission_by_name (dialog, "response");
394 priv->response_requested = FALSE;
398 gtk_file_chooser_dialog_new_valist (const gchar *title,
400 GtkFileChooserAction action,
401 const gchar *first_button_text,
405 const char *button_text = first_button_text;
408 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
414 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
418 response_id = va_arg (varargs, gint);
419 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
420 button_text = va_arg (varargs, const gchar *);
427 * gtk_file_chooser_dialog_new:
428 * @title: (allow-none): Title of the dialog, or %NULL
429 * @parent: (allow-none): Transient parent of the dialog, or %NULL
430 * @action: Open or save mode for the dialog
431 * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
432 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
434 * Creates a new #GtkFileChooserDialog. This function is analogous to
435 * gtk_dialog_new_with_buttons().
437 * Return value: a new #GtkFileChooserDialog
442 gtk_file_chooser_dialog_new (const gchar *title,
444 GtkFileChooserAction action,
445 const gchar *first_button_text,
451 va_start (varargs, first_button_text);
452 result = gtk_file_chooser_dialog_new_valist (title, parent, action,