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.
21 #include "gtkfilechooserdialog.h"
22 #include "gtkfilechooserwidget.h"
23 #include "gtkfilechooserutils.h"
24 #include "gtkfilechooserembed.h"
25 #include "gtkfilesystem.h"
26 #include "gtktypebuiltins.h"
30 struct _GtkFileChooserDialogPrivate
36 /* for use with GtkFileChooserEmbed */
39 gboolean resize_horizontally;
40 gboolean resize_vertically;
43 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o) (GTK_FILE_CHOOSER_DIALOG (o)->priv)
45 static void gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class);
46 static void gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog);
47 static void gtk_file_chooser_dialog_finalize (GObject *object);
49 static GObject* gtk_file_chooser_dialog_constructor (GType type,
50 guint n_construct_properties,
51 GObjectConstructParam *construct_params);
52 static void gtk_file_chooser_dialog_set_property (GObject *object,
56 static void gtk_file_chooser_dialog_get_property (GObject *object,
61 static void gtk_file_chooser_dialog_style_set (GtkWidget *widget,
62 GtkStyle *previous_style);
64 static GObjectClass *parent_class;
67 gtk_file_chooser_dialog_get_type (void)
69 static GType file_chooser_dialog_type = 0;
71 if (!file_chooser_dialog_type)
73 static const GTypeInfo file_chooser_dialog_info =
75 sizeof (GtkFileChooserDialogClass),
77 NULL, /* base_finalize */
78 (GClassInitFunc) gtk_file_chooser_dialog_class_init,
79 NULL, /* class_finalize */
80 NULL, /* class_data */
81 sizeof (GtkFileChooserDialog),
83 (GInstanceInitFunc) gtk_file_chooser_dialog_init,
86 static const GInterfaceInfo file_chooser_info =
88 (GInterfaceInitFunc) _gtk_file_chooser_delegate_iface_init, /* interface_init */
89 NULL, /* interface_finalize */
90 NULL /* interface_data */
93 file_chooser_dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "GtkFileChooserDialog",
94 &file_chooser_dialog_info, 0);
95 g_type_add_interface_static (file_chooser_dialog_type,
96 GTK_TYPE_FILE_CHOOSER,
100 return file_chooser_dialog_type;
104 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
106 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
107 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
109 parent_class = g_type_class_peek_parent (class);
111 gobject_class->constructor = gtk_file_chooser_dialog_constructor;
112 gobject_class->set_property = gtk_file_chooser_dialog_set_property;
113 gobject_class->get_property = gtk_file_chooser_dialog_get_property;
114 gobject_class->finalize = gtk_file_chooser_dialog_finalize;
116 widget_class->style_set = gtk_file_chooser_dialog_style_set;
118 _gtk_file_chooser_install_properties (gobject_class);
120 g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
124 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
126 GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
127 GTK_TYPE_FILE_CHOOSER_DIALOG,
128 GtkFileChooserDialogPrivate);
130 dialog->priv->default_width = -1;
131 dialog->priv->default_height = -1;
132 dialog->priv->resize_horizontally = TRUE;
133 dialog->priv->resize_vertically = TRUE;
135 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
139 gtk_file_chooser_dialog_finalize (GObject *object)
141 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
143 g_free (dialog->priv->file_system);
145 G_OBJECT_CLASS (parent_class)->finalize (object);
148 /* Callback used when the user activates a file in the file chooser widget */
150 file_chooser_widget_file_activated (GtkFileChooser *chooser,
151 GtkFileChooserDialog *dialog)
153 gtk_window_activate_default (GTK_WINDOW (dialog));
157 file_chooser_widget_update_hints (GtkFileChooserDialog *dialog)
159 GtkFileChooserDialogPrivate *priv;
160 GdkGeometry geometry;
162 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
164 geometry.min_width = -1;
165 geometry.min_height = -1;
166 geometry.max_width = (priv->resize_horizontally?G_MAXSHORT:-1);
167 geometry.max_height = (priv->resize_vertically?G_MAXSHORT:-1);
169 gtk_window_set_geometry_hints (GTK_WINDOW (dialog), NULL,
171 GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
175 file_chooser_widget_default_realized_size_changed (GtkWidget *widget,
176 GtkFileChooserDialog *dialog)
178 GtkFileChooserDialogPrivate *priv;
181 gint default_width, default_height;
183 gboolean resize_horizontally;
184 gboolean resize_vertically;
185 gboolean update_hints;
187 gint cur_width, cur_height;
189 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
191 /* Force a size request of everything before we start. This will make sure
192 * that widget->requisition is meaningful. */
193 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
194 gtk_window_get_size (GTK_WINDOW (dialog), &cur_width, &cur_height);
195 width = GTK_WIDGET (dialog)->requisition.width - priv->widget->requisition.width;
196 height = GTK_WIDGET (dialog)->requisition.height - priv->widget->requisition.height;
197 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
198 &default_width, &default_height);
200 /* Ideal target size modulo any resizing */
201 width = default_width + width;
202 height = default_height + height;
204 /* Now, we test for resizability */
205 update_hints = FALSE;
206 _gtk_file_chooser_embed_get_resizable_hints (GTK_FILE_CHOOSER_EMBED (priv->widget),
207 &resize_horizontally,
209 resize_vertically = (!! resize_vertically); /* normalize */
210 resize_horizontally = (!! resize_horizontally);
212 if (resize_horizontally && priv->resize_horizontally)
214 dx = default_width - priv->default_width;
215 priv->default_width = default_width;
217 else if (resize_horizontally && ! priv->resize_horizontally)
219 /* We restore to the ideal size + any change in default_size (which is not
220 * expected). It would be nicer to store the older size to restore to in
222 dx = default_width - priv->default_width;
223 dx += width - cur_width;
224 priv->default_width = default_width;
232 if (resize_vertically && priv->resize_vertically)
234 dy = default_height - priv->default_height;
235 priv->default_height = default_height;
237 else if (resize_vertically && ! priv->resize_vertically)
239 dy = default_height - priv->default_height;
240 dy += height - cur_height;
241 priv->default_height = default_height;
249 priv->resize_horizontally = resize_horizontally;
250 priv->resize_vertically = resize_vertically;
252 /* FIXME: We should make sure that we arent' bigger than the current screen */
253 if (dx != 0 || dy != 0)
255 gtk_window_resize (GTK_WINDOW (dialog),
260 /* Only store the size if we can resize in that direction. */
262 file_chooser_widget_update_hints (dialog);
266 file_chooser_widget_default_unrealized_size_changed (GtkWidget *widget,
267 GtkFileChooserDialog *dialog)
269 GtkFileChooserDialogPrivate *priv;
273 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
274 gtk_widget_size_request (GTK_WIDGET (dialog), &req);
276 _gtk_file_chooser_embed_get_resizable_hints (GTK_FILE_CHOOSER_EMBED (priv->widget),
277 &(priv->resize_horizontally),
278 &(priv->resize_vertically));
279 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
280 &(priv->default_width), &(priv->default_height));
282 /* Determine how much space the rest of the dialog uses compared to priv->widget */
283 width = priv->default_width + GTK_WIDGET (dialog)->requisition.width - priv->widget->requisition.width;
284 height = priv->default_height + GTK_WIDGET (dialog)->requisition.height - priv->widget->requisition.height;
286 gtk_window_set_default_size (GTK_WINDOW (dialog), width, height);
287 file_chooser_widget_update_hints (dialog);
291 file_chooser_widget_default_size_changed (GtkWidget *widget,
292 GtkFileChooserDialog *dialog)
294 if (GTK_WIDGET_REALIZED (dialog))
295 file_chooser_widget_default_realized_size_changed (widget, dialog);
297 file_chooser_widget_default_unrealized_size_changed (widget, dialog);
301 gtk_file_chooser_dialog_constructor (GType type,
302 guint n_construct_properties,
303 GObjectConstructParam *construct_params)
305 GtkFileChooserDialogPrivate *priv;
308 object = parent_class->constructor (type,
309 n_construct_properties,
311 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
313 gtk_widget_push_composite_child ();
315 if (priv->file_system)
316 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
317 "file-system-backend", priv->file_system,
320 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
322 g_signal_connect (priv->widget, "file-activated",
323 G_CALLBACK (file_chooser_widget_file_activated), object);
324 g_signal_connect (priv->widget, "default-size-changed",
325 G_CALLBACK (file_chooser_widget_default_size_changed), object);
327 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
328 gtk_widget_show (priv->widget);
330 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
331 GTK_FILE_CHOOSER (priv->widget));
333 gtk_widget_pop_composite_child ();
339 gtk_file_chooser_dialog_set_property (GObject *object,
345 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
349 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
350 g_free (priv->file_system);
351 priv->file_system = g_value_dup_string (value);
354 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
360 gtk_file_chooser_dialog_get_property (GObject *object,
365 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
367 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
372 set_default_size (GtkFileChooserDialog *dialog)
376 int default_width, default_height;
382 GdkRectangle monitor;
384 widget = GTK_WIDGET (dialog);
385 window = GTK_WINDOW (dialog);
387 /* Size based on characters */
389 font_size = pango_font_description_get_size (widget->style->font_desc);
390 font_size = PANGO_PIXELS (font_size);
392 width = font_size * NUM_CHARS;
393 height = font_size * NUM_LINES;
395 /* Use at least the requisition size... */
397 gtk_widget_size_request (widget, &req);
398 width = MAX (width, req.width);
399 height = MAX (height, req.height);
401 /* ... but no larger than the monitor */
403 screen = gtk_widget_get_screen (widget);
404 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
406 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
408 width = MIN (width, monitor.width * 3 / 4);
409 height = MIN (height, monitor.height * 3 / 4);
413 gtk_window_get_default_size (window, &default_width, &default_height);
415 gtk_window_set_default_size (window,
416 (default_width == -1) ? width : default_width,
417 (default_height == -1) ? height : default_height);
422 gtk_file_chooser_dialog_style_set (GtkWidget *widget,
423 GtkStyle *previous_style)
427 if (GTK_WIDGET_CLASS (parent_class)->style_set)
428 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
430 dialog = GTK_DIALOG (widget);
432 /* Override the style properties with HIG-compliant spacings. Ugh.
433 * http://developer.gnome.org/projects/gup/hig/1.0/layout.html#layout-dialogs
434 * http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-spacing
437 gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), 12);
438 gtk_box_set_spacing (GTK_BOX (dialog->vbox), 24);
440 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 0);
441 gtk_box_set_spacing (GTK_BOX (dialog->action_area), 6);
445 gtk_file_chooser_dialog_new_valist (const gchar *title,
447 GtkFileChooserAction action,
448 const gchar *backend,
449 const gchar *first_button_text,
453 const char *button_text = first_button_text;
456 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
459 "file-system-backend", backend,
463 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
467 response_id = va_arg (varargs, gint);
468 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
469 button_text = va_arg (varargs, const gchar *);
476 * gtk_file_chooser_dialog_new:
477 * @title: Title of the dialog, or %NULL
478 * @parent: Transient parent of the dialog, or %NULL
479 * @action: Open or save mode for the dialog
480 * @first_button_text: stock ID or text to go in the first button, or %NULL
481 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
483 * Creates a new #GtkFileChooserDialog. This function is analogous to
484 * gtk_dialog_new_with_buttons().
486 * Return value: a new #GtkFileChooserDialog
491 gtk_file_chooser_dialog_new (const gchar *title,
493 GtkFileChooserAction action,
494 const gchar *first_button_text,
500 va_start (varargs, first_button_text);
501 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
502 NULL, first_button_text,
510 * gtk_file_chooser_dialog_new_with_backend:
511 * @title: Title of the dialog, or %NULL
512 * @parent: Transient parent of the dialog, or %NULL
513 * @backend: The name of the specific filesystem backend to use.
514 * @action: Open or save mode for the dialog
515 * @first_button_text: stock ID or text to go in the first button, or %NULL
516 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
518 * Creates a new #GtkFileChooserDialog with a specified backend. This is
519 * especially useful if you use gtk_file_chooser_set_local_only() to allow
520 * non-local files and you use a more expressive vfs, such as gnome-vfs,
523 * Return value: a new #GtkFileChooserDialog
528 gtk_file_chooser_dialog_new_with_backend (const gchar *title,
530 GtkFileChooserAction action,
531 const gchar *backend,
532 const gchar *first_button_text,
538 va_start (varargs, first_button_text);
539 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
540 backend, first_button_text,