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 "gtkfilesystem.h"
29 #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 GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
84 GTK_TYPE_FILE_CHOOSER_DIALOG,
85 GtkFileChooserDialogPrivate);
86 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
89 dialog->priv->response_requested = FALSE;
91 gtk_dialog_set_has_separator (fc_dialog, FALSE);
92 gtk_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
93 gtk_box_set_spacing (GTK_BOX (fc_dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
94 gtk_container_set_border_width (GTK_CONTAINER (fc_dialog->action_area), 5);
96 /* We do a signal connection here rather than overriding the method in
97 * class_init because GtkDialog::response is a RUN_LAST signal. We want *our*
98 * handler to be run *first*, regardless of whether the user installs response
99 * handlers of his own.
101 g_signal_connect (dialog, "response",
102 G_CALLBACK (response_cb), NULL);
106 gtk_file_chooser_dialog_finalize (GObject *object)
108 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
110 g_free (dialog->priv->file_system);
112 G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);
116 is_stock_accept_response_id (int response_id)
118 return (response_id == GTK_RESPONSE_ACCEPT
119 || response_id == GTK_RESPONSE_OK
120 || response_id == GTK_RESPONSE_YES
121 || response_id == GTK_RESPONSE_APPLY);
124 /* Callback used when the user activates a file in the file chooser widget */
126 file_chooser_widget_file_activated (GtkFileChooser *chooser,
127 GtkFileChooserDialog *dialog)
131 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
134 /* There probably isn't a default widget, so make things easier for the
135 * programmer by looking for a reasonable button on our own.
138 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
140 for (l = children; l; l = l->next)
145 widget = GTK_WIDGET (l->data);
146 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
147 if (is_stock_accept_response_id (response_id))
149 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
154 g_list_free (children);
158 clamp_to_screen (GtkWidget *widget,
164 GdkRectangle monitor;
166 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
168 screen = gtk_widget_get_screen (widget);
169 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
171 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
174 *width = MIN (*width, (monitor.width * 3) / 4);
177 *height = MIN (*height, (monitor.height * 3) / 4);
181 file_chooser_widget_default_size_changed (GtkWidget *widget,
182 GtkFileChooserDialog *dialog)
184 GtkFileChooserDialogPrivate *priv;
186 gint default_width, default_height;
187 GtkRequisition req, widget_req;
189 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
191 /* Unset any previously set size */
192 gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
194 if (GTK_WIDGET_DRAWABLE (widget))
196 /* Force a size request of everything before we start. This will make sure
197 * that widget->requisition is meaningful. */
198 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
199 gtk_widget_size_request (widget, &widget_req);
201 width = req.width - widget_req.width;
202 height = req.height - widget_req.height;
206 width = GTK_WIDGET (dialog)->allocation.width - widget->allocation.width;
207 height = GTK_WIDGET (dialog)->allocation.height - widget->allocation.height;
210 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
211 &default_width, &default_height);
213 /* Ideal target size plus any extra size */
214 width = default_width + width + (2 * GTK_CONTAINER (dialog)->border_width);
215 height = default_height + height + (2 * GTK_CONTAINER (dialog)->border_width);
217 if (GTK_WIDGET_REALIZED (dialog))
218 clamp_to_screen (GTK_WIDGET (dialog), &width, &height);
220 gtk_window_resize (GTK_WINDOW (dialog), width, height);
224 file_chooser_widget_response_requested (GtkWidget *widget,
225 GtkFileChooserDialog *dialog)
229 /* There probably isn't a default widget, so make things easier for the
230 * programmer by looking for a reasonable button on our own.
233 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
235 for (l = children; l; l = l->next)
240 widget = GTK_WIDGET (l->data);
241 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
242 if (is_stock_accept_response_id (response_id))
244 dialog->priv->response_requested = TRUE;
245 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
250 g_list_free (children);
254 gtk_file_chooser_dialog_constructor (GType type,
255 guint n_construct_properties,
256 GObjectConstructParam *construct_params)
258 GtkFileChooserDialogPrivate *priv;
261 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
262 n_construct_properties,
264 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
266 gtk_widget_push_composite_child ();
268 if (priv->file_system)
269 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
270 "file-system-backend", priv->file_system,
273 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
275 g_signal_connect (priv->widget, "file-activated",
276 G_CALLBACK (file_chooser_widget_file_activated), object);
277 g_signal_connect (priv->widget, "default-size-changed",
278 G_CALLBACK (file_chooser_widget_default_size_changed), object);
279 g_signal_connect (priv->widget, "response-requested",
280 G_CALLBACK (file_chooser_widget_response_requested), object);
282 gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
283 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
285 gtk_widget_show (priv->widget);
287 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
288 GTK_FILE_CHOOSER (priv->widget));
290 gtk_widget_pop_composite_child ();
296 gtk_file_chooser_dialog_set_property (GObject *object,
302 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
306 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
307 g_free (priv->file_system);
308 priv->file_system = g_value_dup_string (value);
311 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
317 gtk_file_chooser_dialog_get_property (GObject *object,
322 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
324 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
329 set_default_size (GtkFileChooserDialog *dialog)
333 int default_width, default_height;
339 GdkRectangle monitor;
341 widget = GTK_WIDGET (dialog);
342 window = GTK_WINDOW (dialog);
344 /* Size based on characters */
346 font_size = pango_font_description_get_size (widget->style->font_desc);
347 font_size = PANGO_PIXELS (font_size);
349 width = font_size * NUM_CHARS;
350 height = font_size * NUM_LINES;
352 /* Use at least the requisition size... */
354 gtk_widget_size_request (widget, &req);
355 width = MAX (width, req.width);
356 height = MAX (height, req.height);
358 /* ... but no larger than the monitor */
360 screen = gtk_widget_get_screen (widget);
361 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
363 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
365 width = MIN (width, monitor.width * 3 / 4);
366 height = MIN (height, monitor.height * 3 / 4);
370 gtk_window_get_default_size (window, &default_width, &default_height);
372 gtk_window_set_default_size (window,
373 (default_width == -1) ? width : default_width,
374 (default_height == -1) ? height : default_height);
379 foreach_ensure_default_response_cb (GtkWidget *widget,
382 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
385 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
386 if (is_stock_accept_response_id (response_id))
387 gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
391 ensure_default_response (GtkFileChooserDialog *dialog)
393 gtk_container_foreach (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area),
394 foreach_ensure_default_response_cb,
398 /* GtkWidget::map handler */
400 gtk_file_chooser_dialog_map (GtkWidget *widget)
402 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
403 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
405 ensure_default_response (dialog);
407 if (!GTK_WIDGET_MAPPED (priv->widget))
408 gtk_widget_map (priv->widget);
410 file_chooser_widget_default_size_changed (priv->widget, dialog);
411 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
413 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
416 /* GtkWidget::unmap handler */
418 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
420 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
421 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
423 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
425 /* See bug #145470. We unmap the GtkFileChooserWidget so that if the dialog
426 * is remapped, the widget will be remapped as well. Implementations should
427 * refresh their contents when this happens, as some applications keep a
428 * single file chooser alive and map/unmap it as needed, rather than creating
429 * a new file chooser every time they need one.
431 gtk_widget_unmap (priv->widget);
434 /* GtkDialog::response handler */
436 response_cb (GtkDialog *dialog,
439 GtkFileChooserDialogPrivate *priv;
441 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
443 /* Act only on response IDs we recognize */
444 if (is_stock_accept_response_id (response_id)
445 && !priv->response_requested
446 && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
448 g_signal_stop_emission_by_name (dialog, "response");
451 priv->response_requested = FALSE;
455 gtk_file_chooser_dialog_new_valist (const gchar *title,
457 GtkFileChooserAction action,
458 const gchar *backend,
459 const gchar *first_button_text,
463 const char *button_text = first_button_text;
466 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
469 "file-system-backend", backend,
473 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
477 response_id = va_arg (varargs, gint);
478 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
479 button_text = va_arg (varargs, const gchar *);
486 * gtk_file_chooser_dialog_new:
487 * @title: Title of the dialog, or %NULL
488 * @parent: Transient parent of the dialog, or %NULL
489 * @action: Open or save mode for the dialog
490 * @first_button_text: stock ID or text to go in the first button, or %NULL
491 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
493 * Creates a new #GtkFileChooserDialog. This function is analogous to
494 * gtk_dialog_new_with_buttons().
496 * Return value: a new #GtkFileChooserDialog
501 gtk_file_chooser_dialog_new (const gchar *title,
503 GtkFileChooserAction action,
504 const gchar *first_button_text,
510 va_start (varargs, first_button_text);
511 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
512 NULL, first_button_text,
520 * gtk_file_chooser_dialog_new_with_backend:
521 * @title: Title of the dialog, or %NULL
522 * @parent: Transient parent of the dialog, or %NULL
523 * @action: Open or save mode for the dialog
524 * @backend: The name of the specific filesystem backend to use.
525 * @first_button_text: stock ID or text to go in the first button, or %NULL
526 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
528 * Creates a new #GtkFileChooserDialog with a specified backend. This is
529 * especially useful if you use gtk_file_chooser_set_local_only() to allow
530 * non-local files and you use a more expressive vfs, such as gnome-vfs,
533 * Return value: a new #GtkFileChooserDialog
538 gtk_file_chooser_dialog_new_with_backend (const gchar *title,
540 GtkFileChooserAction action,
541 const gchar *backend,
542 const gchar *first_button_text,
548 va_start (varargs, first_button_text);
549 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
550 backend, first_button_text,
557 #define __GTK_FILE_CHOOSER_DIALOG_C__
558 #include "gtkaliasdef.c"