1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserdialog.c: File selector dialog
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "gtkfilechooserprivate.h"
23 #include "gtkfilechooserdialog.h"
24 #include "gtkfilechooserwidget.h"
25 #include "gtkfilechooserutils.h"
26 #include "gtkfilechooserembed.h"
27 #include "gtkfilesystem.h"
28 #include "gtktypebuiltins.h"
34 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o) (GTK_FILE_CHOOSER_DIALOG (o)->priv)
36 static void gtk_file_chooser_dialog_finalize (GObject *object);
38 static GObject* gtk_file_chooser_dialog_constructor (GType type,
39 guint n_construct_properties,
40 GObjectConstructParam *construct_params);
41 static void gtk_file_chooser_dialog_set_property (GObject *object,
45 static void gtk_file_chooser_dialog_get_property (GObject *object,
50 static void gtk_file_chooser_dialog_map (GtkWidget *widget);
51 static void gtk_file_chooser_dialog_unmap (GtkWidget *widget);
53 static void response_cb (GtkDialog *dialog,
56 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDialog, gtk_file_chooser_dialog, GTK_TYPE_DIALOG,
57 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
58 _gtk_file_chooser_delegate_iface_init))
61 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
63 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
64 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
66 gobject_class->constructor = gtk_file_chooser_dialog_constructor;
67 gobject_class->set_property = gtk_file_chooser_dialog_set_property;
68 gobject_class->get_property = gtk_file_chooser_dialog_get_property;
69 gobject_class->finalize = gtk_file_chooser_dialog_finalize;
71 widget_class->map = gtk_file_chooser_dialog_map;
72 widget_class->unmap = gtk_file_chooser_dialog_unmap;
74 _gtk_file_chooser_install_properties (gobject_class);
76 g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
80 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
82 GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
83 GTK_TYPE_FILE_CHOOSER_DIALOG,
84 GtkFileChooserDialogPrivate);
85 GtkDialog *fc_dialog = GTK_DIALOG (dialog);
88 dialog->priv->default_width = -1;
89 dialog->priv->default_height = -1;
90 dialog->priv->resize_horizontally = TRUE;
91 dialog->priv->resize_vertically = TRUE;
92 dialog->priv->response_requested = FALSE;
94 gtk_dialog_set_has_separator (fc_dialog, FALSE);
95 gtk_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
96 gtk_box_set_spacing (GTK_BOX (fc_dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
97 gtk_container_set_border_width (GTK_CONTAINER (fc_dialog->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);
118 /* Callback used when the user activates a file in the file chooser widget */
120 file_chooser_widget_file_activated (GtkFileChooser *chooser,
121 GtkFileChooserDialog *dialog)
125 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
128 /* There probably isn't a default widget, so make things easier for the
129 * programmer by looking for a reasonable button on our own.
132 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
134 for (l = children; l; l = l->next)
139 widget = GTK_WIDGET (l->data);
140 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
141 if (response_id == GTK_RESPONSE_ACCEPT
142 || response_id == GTK_RESPONSE_OK
143 || response_id == GTK_RESPONSE_YES
144 || response_id == GTK_RESPONSE_APPLY)
146 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
151 g_list_free (children);
155 file_chooser_widget_update_hints (GtkFileChooserDialog *dialog)
157 GtkFileChooserDialogPrivate *priv;
158 GdkGeometry geometry;
160 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
162 geometry.min_width = -1;
163 geometry.min_height = -1;
164 geometry.max_width = (priv->resize_horizontally?G_MAXSHORT:-1);
165 geometry.max_height = (priv->resize_vertically?G_MAXSHORT:-1);
167 gtk_window_set_geometry_hints (GTK_WINDOW (dialog), NULL,
169 GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
173 clamp_to_screen (GtkWidget *widget,
179 GdkRectangle monitor;
181 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
183 screen = gtk_widget_get_screen (widget);
184 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
186 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
189 *width = MIN (*width, (monitor.width * 3) / 4);
192 *height = MIN (*height, (monitor.height * 3) / 4);
196 file_chooser_widget_default_realized_size_changed (GtkWidget *widget,
197 GtkFileChooserDialog *dialog)
199 GtkFileChooserDialogPrivate *priv;
202 gint default_width, default_height;
204 gboolean resize_horizontally;
205 gboolean resize_vertically;
206 gboolean update_hints;
208 gint cur_width, cur_height;
210 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
212 /* Force a size request of everything before we start. This will make sure
213 * that widget->requisition is meaningful. */
214 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
215 gtk_window_get_size (GTK_WINDOW (dialog), &cur_width, &cur_height);
216 width = GTK_WIDGET (dialog)->requisition.width - priv->widget->requisition.width;
217 height = GTK_WIDGET (dialog)->requisition.height - priv->widget->requisition.height;
218 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
219 &default_width, &default_height);
221 /* Ideal target size modulo any resizing */
222 width = default_width + width;
223 height = default_height + height;
225 /* Now, we test for resizability */
226 update_hints = FALSE;
227 _gtk_file_chooser_embed_get_resizable_hints (GTK_FILE_CHOOSER_EMBED (priv->widget),
228 &resize_horizontally,
230 resize_vertically = (!! resize_vertically); /* normalize */
231 resize_horizontally = (!! resize_horizontally);
233 if (resize_horizontally && priv->resize_horizontally)
235 dx = default_width - priv->default_width;
236 priv->default_width = default_width;
238 else if (resize_horizontally && ! priv->resize_horizontally)
240 /* We restore to the ideal size + any change in default_size (which is not
241 * expected). It would be nicer to store the older size to restore to in
243 dx = default_width - priv->default_width;
244 dx += width - cur_width;
245 priv->default_width = default_width;
253 if (resize_vertically && priv->resize_vertically)
255 dy = default_height - priv->default_height;
256 priv->default_height = default_height;
258 else if (resize_vertically && ! priv->resize_vertically)
260 dy = default_height - priv->default_height;
261 dy += height - cur_height;
262 priv->default_height = default_height;
270 priv->resize_horizontally = resize_horizontally;
271 priv->resize_vertically = resize_vertically;
273 if (dx != 0 || dy != 0)
275 gint new_width = cur_width + dx;
276 gint new_height = cur_height + dy;
278 clamp_to_screen (GTK_WIDGET (dialog), &new_width, &new_height);
280 gtk_window_resize (GTK_WINDOW (dialog), new_width, new_height);
283 /* Only store the size if we can resize in that direction. */
285 file_chooser_widget_update_hints (dialog);
289 file_chooser_widget_default_unrealized_size_changed (GtkWidget *widget,
290 GtkFileChooserDialog *dialog)
292 GtkFileChooserDialogPrivate *priv;
296 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
297 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
299 _gtk_file_chooser_embed_get_resizable_hints (GTK_FILE_CHOOSER_EMBED (priv->widget),
300 &(priv->resize_horizontally),
301 &(priv->resize_vertically));
302 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
303 &(priv->default_width), &(priv->default_height));
305 /* Determine how much space the rest of the dialog uses compared to priv->widget */
306 width = priv->default_width + GTK_WIDGET (dialog)->requisition.width - priv->widget->requisition.width;
307 height = priv->default_height + GTK_WIDGET (dialog)->requisition.height - priv->widget->requisition.height;
309 gtk_window_set_default_size (GTK_WINDOW (dialog), width, height);
310 file_chooser_widget_update_hints (dialog);
314 file_chooser_widget_default_size_changed (GtkWidget *widget,
315 GtkFileChooserDialog *dialog)
317 if (GTK_WIDGET_REALIZED (dialog))
318 file_chooser_widget_default_realized_size_changed (widget, dialog);
320 file_chooser_widget_default_unrealized_size_changed (widget, dialog);
324 file_chooser_widget_response_requested (GtkWidget *widget,
325 GtkFileChooserDialog *dialog)
329 /* There probably isn't a default widget, so make things easier for the
330 * programmer by looking for a reasonable button on our own.
333 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
335 for (l = children; l; l = l->next)
340 widget = GTK_WIDGET (l->data);
341 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
342 if (response_id == GTK_RESPONSE_ACCEPT
343 || response_id == GTK_RESPONSE_OK
344 || response_id == GTK_RESPONSE_YES
345 || response_id == GTK_RESPONSE_APPLY)
347 dialog->priv->response_requested = TRUE;
348 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
353 g_list_free (children);
357 gtk_file_chooser_dialog_constructor (GType type,
358 guint n_construct_properties,
359 GObjectConstructParam *construct_params)
361 GtkFileChooserDialogPrivate *priv;
364 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
365 n_construct_properties,
367 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
369 gtk_widget_push_composite_child ();
371 if (priv->file_system)
372 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
373 "file-system-backend", priv->file_system,
376 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
378 g_signal_connect (priv->widget, "file-activated",
379 G_CALLBACK (file_chooser_widget_file_activated), object);
380 g_signal_connect (priv->widget, "default-size-changed",
381 G_CALLBACK (file_chooser_widget_default_size_changed), object);
382 g_signal_connect (priv->widget, "response-requested",
383 G_CALLBACK (file_chooser_widget_response_requested), object);
385 gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
386 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
388 gtk_widget_show (priv->widget);
390 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
391 GTK_FILE_CHOOSER (priv->widget));
393 gtk_widget_pop_composite_child ();
399 gtk_file_chooser_dialog_set_property (GObject *object,
405 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
409 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
410 g_free (priv->file_system);
411 priv->file_system = g_value_dup_string (value);
414 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
420 gtk_file_chooser_dialog_get_property (GObject *object,
425 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
427 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
432 set_default_size (GtkFileChooserDialog *dialog)
436 int default_width, default_height;
442 GdkRectangle monitor;
444 widget = GTK_WIDGET (dialog);
445 window = GTK_WINDOW (dialog);
447 /* Size based on characters */
449 font_size = pango_font_description_get_size (widget->style->font_desc);
450 font_size = PANGO_PIXELS (font_size);
452 width = font_size * NUM_CHARS;
453 height = font_size * NUM_LINES;
455 /* Use at least the requisition size... */
457 gtk_widget_size_request (widget, &req);
458 width = MAX (width, req.width);
459 height = MAX (height, req.height);
461 /* ... but no larger than the monitor */
463 screen = gtk_widget_get_screen (widget);
464 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
466 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
468 width = MIN (width, monitor.width * 3 / 4);
469 height = MIN (height, monitor.height * 3 / 4);
473 gtk_window_get_default_size (window, &default_width, &default_height);
475 gtk_window_set_default_size (window,
476 (default_width == -1) ? width : default_width,
477 (default_height == -1) ? height : default_height);
481 /* GtkWidget::map handler */
483 gtk_file_chooser_dialog_map (GtkWidget *widget)
485 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
486 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
488 if (!GTK_WIDGET_MAPPED (priv->widget))
489 gtk_widget_map (priv->widget);
491 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
493 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
496 /* GtkWidget::unmap handler */
498 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
500 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
501 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
503 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
505 /* See bug #145470. We unmap the GtkFileChooserWidget so that if the dialog
506 * is remapped, the widget will be remapped as well. Implementations should
507 * refresh their contents when this happens, as some applications keep a
508 * single file chooser alive and map/unmap it as needed, rather than creating
509 * a new file chooser every time they need one.
511 gtk_widget_unmap (priv->widget);
514 /* GtkDialog::response handler */
516 response_cb (GtkDialog *dialog,
519 GtkFileChooserDialogPrivate *priv;
521 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
523 /* Act only on response IDs we recognize */
524 if (!(response_id == GTK_RESPONSE_ACCEPT
525 || response_id == GTK_RESPONSE_OK
526 || response_id == GTK_RESPONSE_YES
527 || response_id == GTK_RESPONSE_APPLY))
530 if (!priv->response_requested && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
532 g_signal_stop_emission_by_name (dialog, "response");
533 priv->response_requested = FALSE;
538 gtk_file_chooser_dialog_new_valist (const gchar *title,
540 GtkFileChooserAction action,
541 const gchar *backend,
542 const gchar *first_button_text,
546 const char *button_text = first_button_text;
549 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
552 "file-system-backend", backend,
556 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
560 response_id = va_arg (varargs, gint);
561 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
562 button_text = va_arg (varargs, const gchar *);
569 * gtk_file_chooser_dialog_new:
570 * @title: Title of the dialog, or %NULL
571 * @parent: Transient parent of the dialog, or %NULL
572 * @action: Open or save mode for the dialog
573 * @first_button_text: stock ID or text to go in the first button, or %NULL
574 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
576 * Creates a new #GtkFileChooserDialog. This function is analogous to
577 * gtk_dialog_new_with_buttons().
579 * Return value: a new #GtkFileChooserDialog
584 gtk_file_chooser_dialog_new (const gchar *title,
586 GtkFileChooserAction action,
587 const gchar *first_button_text,
593 va_start (varargs, first_button_text);
594 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
595 NULL, first_button_text,
603 * gtk_file_chooser_dialog_new_with_backend:
604 * @title: Title of the dialog, or %NULL
605 * @parent: Transient parent of the dialog, or %NULL
606 * @action: Open or save mode for the dialog
607 * @backend: The name of the specific filesystem backend to use.
608 * @first_button_text: stock ID or text to go in the first button, or %NULL
609 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
611 * Creates a new #GtkFileChooserDialog with a specified backend. This is
612 * especially useful if you use gtk_file_chooser_set_local_only() to allow
613 * non-local files and you use a more expressive vfs, such as gnome-vfs,
616 * Return value: a new #GtkFileChooserDialog
621 gtk_file_chooser_dialog_new_with_backend (const gchar *title,
623 GtkFileChooserAction action,
624 const gchar *backend,
625 const gchar *first_button_text,
631 va_start (varargs, first_button_text);
632 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
633 backend, first_button_text,
640 #define __GTK_FILE_CHOOSER_DIALOG_C__
641 #include "gtkaliasdef.c"