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);
52 static void gtk_file_chooser_dialog_style_set (GtkWidget *widget,
53 GtkStyle *previous_style);
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;
75 widget_class->style_set = gtk_file_chooser_dialog_style_set;
77 _gtk_file_chooser_install_properties (gobject_class);
79 g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
83 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
85 GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
86 GTK_TYPE_FILE_CHOOSER_DIALOG,
87 GtkFileChooserDialogPrivate);
89 dialog->priv->default_width = -1;
90 dialog->priv->default_height = -1;
91 dialog->priv->resize_horizontally = TRUE;
92 dialog->priv->resize_vertically = TRUE;
93 dialog->priv->response_requested = FALSE;
95 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
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);
116 /* Callback used when the user activates a file in the file chooser widget */
118 file_chooser_widget_file_activated (GtkFileChooser *chooser,
119 GtkFileChooserDialog *dialog)
123 if (gtk_window_activate_default (GTK_WINDOW (dialog)))
126 /* There probably isn't a default widget, so make things easier for the
127 * programmer by looking for a reasonable button on our own.
130 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
132 for (l = children; l; l = l->next)
137 widget = GTK_WIDGET (l->data);
138 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
139 if (response_id == GTK_RESPONSE_ACCEPT
140 || response_id == GTK_RESPONSE_OK
141 || response_id == GTK_RESPONSE_YES
142 || response_id == GTK_RESPONSE_APPLY)
144 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
149 g_list_free (children);
153 file_chooser_widget_update_hints (GtkFileChooserDialog *dialog)
155 GtkFileChooserDialogPrivate *priv;
156 GdkGeometry geometry;
158 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
160 geometry.min_width = -1;
161 geometry.min_height = -1;
162 geometry.max_width = (priv->resize_horizontally?G_MAXSHORT:-1);
163 geometry.max_height = (priv->resize_vertically?G_MAXSHORT:-1);
165 gtk_window_set_geometry_hints (GTK_WINDOW (dialog), NULL,
167 GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
171 clamp_to_screen (GtkWidget *widget,
177 GdkRectangle monitor;
179 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
181 screen = gtk_widget_get_screen (widget);
182 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
184 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
187 *width = MIN (*width, (monitor.width * 3) / 4);
190 *height = MIN (*height, (monitor.height * 3) / 4);
194 file_chooser_widget_default_realized_size_changed (GtkWidget *widget,
195 GtkFileChooserDialog *dialog)
197 GtkFileChooserDialogPrivate *priv;
200 gint default_width, default_height;
202 gboolean resize_horizontally;
203 gboolean resize_vertically;
204 gboolean update_hints;
206 gint cur_width, cur_height;
208 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
210 /* Force a size request of everything before we start. This will make sure
211 * that widget->requisition is meaningful. */
212 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
213 gtk_window_get_size (GTK_WINDOW (dialog), &cur_width, &cur_height);
214 width = GTK_WIDGET (dialog)->requisition.width - priv->widget->requisition.width;
215 height = GTK_WIDGET (dialog)->requisition.height - priv->widget->requisition.height;
216 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
217 &default_width, &default_height);
219 /* Ideal target size modulo any resizing */
220 width = default_width + width;
221 height = default_height + height;
223 /* Now, we test for resizability */
224 update_hints = FALSE;
225 _gtk_file_chooser_embed_get_resizable_hints (GTK_FILE_CHOOSER_EMBED (priv->widget),
226 &resize_horizontally,
228 resize_vertically = (!! resize_vertically); /* normalize */
229 resize_horizontally = (!! resize_horizontally);
231 if (resize_horizontally && priv->resize_horizontally)
233 dx = default_width - priv->default_width;
234 priv->default_width = default_width;
236 else if (resize_horizontally && ! priv->resize_horizontally)
238 /* We restore to the ideal size + any change in default_size (which is not
239 * expected). It would be nicer to store the older size to restore to in
241 dx = default_width - priv->default_width;
242 dx += width - cur_width;
243 priv->default_width = default_width;
251 if (resize_vertically && priv->resize_vertically)
253 dy = default_height - priv->default_height;
254 priv->default_height = default_height;
256 else if (resize_vertically && ! priv->resize_vertically)
258 dy = default_height - priv->default_height;
259 dy += height - cur_height;
260 priv->default_height = default_height;
268 priv->resize_horizontally = resize_horizontally;
269 priv->resize_vertically = resize_vertically;
271 if (dx != 0 || dy != 0)
273 gint new_width = cur_width + dx;
274 gint new_height = cur_height + dy;
276 clamp_to_screen (GTK_WIDGET (dialog), &new_width, &new_height);
278 gtk_window_resize (GTK_WINDOW (dialog), new_width, new_height);
281 /* Only store the size if we can resize in that direction. */
283 file_chooser_widget_update_hints (dialog);
287 file_chooser_widget_default_unrealized_size_changed (GtkWidget *widget,
288 GtkFileChooserDialog *dialog)
290 GtkFileChooserDialogPrivate *priv;
294 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
295 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
297 _gtk_file_chooser_embed_get_resizable_hints (GTK_FILE_CHOOSER_EMBED (priv->widget),
298 &(priv->resize_horizontally),
299 &(priv->resize_vertically));
300 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
301 &(priv->default_width), &(priv->default_height));
303 /* Determine how much space the rest of the dialog uses compared to priv->widget */
304 width = priv->default_width + GTK_WIDGET (dialog)->requisition.width - priv->widget->requisition.width;
305 height = priv->default_height + GTK_WIDGET (dialog)->requisition.height - priv->widget->requisition.height;
307 gtk_window_set_default_size (GTK_WINDOW (dialog), width, height);
308 file_chooser_widget_update_hints (dialog);
312 file_chooser_widget_default_size_changed (GtkWidget *widget,
313 GtkFileChooserDialog *dialog)
315 if (GTK_WIDGET_REALIZED (dialog))
316 file_chooser_widget_default_realized_size_changed (widget, dialog);
318 file_chooser_widget_default_unrealized_size_changed (widget, dialog);
322 file_chooser_widget_response_requested (GtkWidget *widget,
323 GtkFileChooserDialog *dialog)
327 /* There probably isn't a default widget, so make things easier for the
328 * programmer by looking for a reasonable button on our own.
331 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
333 for (l = children; l; l = l->next)
338 widget = GTK_WIDGET (l->data);
339 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
340 if (response_id == GTK_RESPONSE_ACCEPT
341 || response_id == GTK_RESPONSE_OK
342 || response_id == GTK_RESPONSE_YES
343 || response_id == GTK_RESPONSE_APPLY)
345 dialog->priv->response_requested = TRUE;
346 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
351 g_list_free (children);
355 gtk_file_chooser_dialog_constructor (GType type,
356 guint n_construct_properties,
357 GObjectConstructParam *construct_params)
359 GtkFileChooserDialogPrivate *priv;
362 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
363 n_construct_properties,
365 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
367 gtk_widget_push_composite_child ();
369 if (priv->file_system)
370 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
371 "file-system-backend", priv->file_system,
374 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
376 g_signal_connect (priv->widget, "file-activated",
377 G_CALLBACK (file_chooser_widget_file_activated), object);
378 g_signal_connect (priv->widget, "default-size-changed",
379 G_CALLBACK (file_chooser_widget_default_size_changed), object);
380 g_signal_connect (priv->widget, "response-requested",
381 G_CALLBACK (file_chooser_widget_response_requested), object);
383 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
385 gtk_widget_show (priv->widget);
387 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
388 GTK_FILE_CHOOSER (priv->widget));
390 gtk_widget_pop_composite_child ();
396 gtk_file_chooser_dialog_set_property (GObject *object,
402 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
406 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
407 g_free (priv->file_system);
408 priv->file_system = g_value_dup_string (value);
411 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
417 gtk_file_chooser_dialog_get_property (GObject *object,
422 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
424 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
429 set_default_size (GtkFileChooserDialog *dialog)
433 int default_width, default_height;
439 GdkRectangle monitor;
441 widget = GTK_WIDGET (dialog);
442 window = GTK_WINDOW (dialog);
444 /* Size based on characters */
446 font_size = pango_font_description_get_size (widget->style->font_desc);
447 font_size = PANGO_PIXELS (font_size);
449 width = font_size * NUM_CHARS;
450 height = font_size * NUM_LINES;
452 /* Use at least the requisition size... */
454 gtk_widget_size_request (widget, &req);
455 width = MAX (width, req.width);
456 height = MAX (height, req.height);
458 /* ... but no larger than the monitor */
460 screen = gtk_widget_get_screen (widget);
461 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
463 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
465 width = MIN (width, monitor.width * 3 / 4);
466 height = MIN (height, monitor.height * 3 / 4);
470 gtk_window_get_default_size (window, &default_width, &default_height);
472 gtk_window_set_default_size (window,
473 (default_width == -1) ? width : default_width,
474 (default_height == -1) ? height : default_height);
478 /* GtkWidget::map handler */
480 gtk_file_chooser_dialog_map (GtkWidget *widget)
482 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
483 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
485 if (!GTK_WIDGET_MAPPED (priv->widget))
486 gtk_widget_map (priv->widget);
488 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
490 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
493 /* GtkWidget::unmap handler */
495 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
497 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
498 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
500 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
502 /* See bug #145470. We unmap the GtkFileChooserWidget so that if the dialog
503 * is remapped, the widget will be remapped as well. Implementations should
504 * refresh their contents when this happens, as some applications keep a
505 * single file chooser alive and map/unmap it as needed, rather than creating
506 * a new file chooser every time they need one.
508 gtk_widget_unmap (priv->widget);
512 gtk_file_chooser_dialog_style_set (GtkWidget *widget,
513 GtkStyle *previous_style)
517 if (GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->style_set)
518 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->style_set (widget, previous_style);
520 dialog = GTK_DIALOG (widget);
522 /* Override the style properties with HIG-compliant spacings. Ugh.
523 * http://developer.gnome.org/projects/gup/hig/1.0/layout.html#layout-dialogs
524 * http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-spacing
527 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), 12);
528 gtk_box_set_spacing (GTK_BOX (dialog->vbox), 24);
530 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 0);
531 gtk_box_set_spacing (GTK_BOX (dialog->action_area), 6);
534 /* GtkDialog::response handler */
536 response_cb (GtkDialog *dialog,
539 GtkFileChooserDialogPrivate *priv;
541 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
543 /* Act only on response IDs we recognize */
544 if (!(response_id == GTK_RESPONSE_ACCEPT
545 || response_id == GTK_RESPONSE_OK
546 || response_id == GTK_RESPONSE_YES
547 || response_id == GTK_RESPONSE_APPLY))
550 if (!priv->response_requested && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
552 g_signal_stop_emission_by_name (dialog, "response");
553 priv->response_requested = FALSE;
558 gtk_file_chooser_dialog_new_valist (const gchar *title,
560 GtkFileChooserAction action,
561 const gchar *backend,
562 const gchar *first_button_text,
566 const char *button_text = first_button_text;
569 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
572 "file-system-backend", backend,
576 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
580 response_id = va_arg (varargs, gint);
581 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
582 button_text = va_arg (varargs, const gchar *);
589 * gtk_file_chooser_dialog_new:
590 * @title: Title of the dialog, or %NULL
591 * @parent: Transient parent of the dialog, or %NULL
592 * @action: Open or save mode for the dialog
593 * @first_button_text: stock ID or text to go in the first button, or %NULL
594 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
596 * Creates a new #GtkFileChooserDialog. This function is analogous to
597 * gtk_dialog_new_with_buttons().
599 * Return value: a new #GtkFileChooserDialog
604 gtk_file_chooser_dialog_new (const gchar *title,
606 GtkFileChooserAction action,
607 const gchar *first_button_text,
613 va_start (varargs, first_button_text);
614 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
615 NULL, first_button_text,
623 * gtk_file_chooser_dialog_new_with_backend:
624 * @title: Title of the dialog, or %NULL
625 * @parent: Transient parent of the dialog, or %NULL
626 * @action: Open or save mode for the dialog
627 * @backend: The name of the specific filesystem backend to use.
628 * @first_button_text: stock ID or text to go in the first button, or %NULL
629 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
631 * Creates a new #GtkFileChooserDialog with a specified backend. This is
632 * especially useful if you use gtk_file_chooser_set_local_only() to allow
633 * non-local files and you use a more expressive vfs, such as gnome-vfs,
636 * Return value: a new #GtkFileChooserDialog
641 gtk_file_chooser_dialog_new_with_backend (const gchar *title,
643 GtkFileChooserAction action,
644 const gchar *backend,
645 const gchar *first_button_text,
651 va_start (varargs, first_button_text);
652 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
653 backend, first_button_text,
660 #define __GTK_FILE_CHOOSER_DIALOG_C__
661 #include "gtkaliasdef.c"