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;
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_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_size_request (GTK_WIDGET (dialog), &req);
200 gtk_widget_size_request (widget, &widget_req);
202 width = req.width - widget_req.width;
203 height = req.height - widget_req.height;
207 width = GTK_WIDGET (dialog)->allocation.width - widget->allocation.width;
208 height = GTK_WIDGET (dialog)->allocation.height - widget->allocation.height;
211 resizable = _gtk_file_chooser_embed_get_resizable (GTK_FILE_CHOOSER_EMBED (priv->widget));
212 _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
213 &default_width, &default_height);
215 /* Ideal target size plus any extra size */
216 width = default_width + width + (2 * GTK_CONTAINER (dialog)->border_width);
217 height = default_height + height + (2 * GTK_CONTAINER (dialog)->border_width);
219 if (GTK_WIDGET_REALIZED (dialog))
220 clamp_to_screen (GTK_WIDGET (dialog), &width, &height);
224 gtk_window_set_resizable (GTK_WINDOW (dialog), resizable);
225 gtk_window_resize (GTK_WINDOW (dialog), width, height);
229 gtk_widget_set_size_request (GTK_WIDGET (dialog), width, -1);
230 gtk_window_set_resizable (GTK_WINDOW (dialog), resizable);
235 file_chooser_widget_response_requested (GtkWidget *widget,
236 GtkFileChooserDialog *dialog)
240 /* There probably isn't a default widget, so make things easier for the
241 * programmer by looking for a reasonable button on our own.
244 children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
246 for (l = children; l; l = l->next)
251 widget = GTK_WIDGET (l->data);
252 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
253 if (is_stock_accept_response_id (response_id))
255 dialog->priv->response_requested = TRUE;
256 gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
261 g_list_free (children);
265 gtk_file_chooser_dialog_constructor (GType type,
266 guint n_construct_properties,
267 GObjectConstructParam *construct_params)
269 GtkFileChooserDialogPrivate *priv;
272 object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
273 n_construct_properties,
275 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
277 gtk_widget_push_composite_child ();
279 if (priv->file_system)
280 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
281 "file-system-backend", priv->file_system,
284 priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
286 g_signal_connect (priv->widget, "file-activated",
287 G_CALLBACK (file_chooser_widget_file_activated), object);
288 g_signal_connect (priv->widget, "default-size-changed",
289 G_CALLBACK (file_chooser_widget_default_size_changed), object);
290 g_signal_connect (priv->widget, "response-requested",
291 G_CALLBACK (file_chooser_widget_response_requested), object);
293 gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
294 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
296 gtk_widget_show (priv->widget);
298 _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
299 GTK_FILE_CHOOSER (priv->widget));
301 gtk_widget_pop_composite_child ();
307 gtk_file_chooser_dialog_set_property (GObject *object,
313 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
317 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
318 g_free (priv->file_system);
319 priv->file_system = g_value_dup_string (value);
322 g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
328 gtk_file_chooser_dialog_get_property (GObject *object,
333 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
335 g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
340 set_default_size (GtkFileChooserDialog *dialog)
344 int default_width, default_height;
350 GdkRectangle monitor;
352 widget = GTK_WIDGET (dialog);
353 window = GTK_WINDOW (dialog);
355 /* Size based on characters */
357 font_size = pango_font_description_get_size (widget->style->font_desc);
358 font_size = PANGO_PIXELS (font_size);
360 width = font_size * NUM_CHARS;
361 height = font_size * NUM_LINES;
363 /* Use at least the requisition size... */
365 gtk_widget_size_request (widget, &req);
366 width = MAX (width, req.width);
367 height = MAX (height, req.height);
369 /* ... but no larger than the monitor */
371 screen = gtk_widget_get_screen (widget);
372 monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
374 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
376 width = MIN (width, monitor.width * 3 / 4);
377 height = MIN (height, monitor.height * 3 / 4);
381 gtk_window_get_default_size (window, &default_width, &default_height);
383 gtk_window_set_default_size (window,
384 (default_width == -1) ? width : default_width,
385 (default_height == -1) ? height : default_height);
390 foreach_ensure_default_response_cb (GtkWidget *widget,
393 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
396 response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
397 if (is_stock_accept_response_id (response_id))
398 gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
402 ensure_default_response (GtkFileChooserDialog *dialog)
404 gtk_container_foreach (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area),
405 foreach_ensure_default_response_cb,
409 /* GtkWidget::map handler */
411 gtk_file_chooser_dialog_map (GtkWidget *widget)
413 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
414 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
416 ensure_default_response (dialog);
418 if (!GTK_WIDGET_MAPPED (priv->widget))
419 gtk_widget_map (priv->widget);
421 file_chooser_widget_default_size_changed (priv->widget, dialog);
422 _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
424 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
427 /* GtkWidget::unmap handler */
429 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
431 GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
432 GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
434 GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
436 /* See bug #145470. We unmap the GtkFileChooserWidget so that if the dialog
437 * is remapped, the widget will be remapped as well. Implementations should
438 * refresh their contents when this happens, as some applications keep a
439 * single file chooser alive and map/unmap it as needed, rather than creating
440 * a new file chooser every time they need one.
442 gtk_widget_unmap (priv->widget);
445 /* GtkDialog::response handler */
447 response_cb (GtkDialog *dialog,
450 GtkFileChooserDialogPrivate *priv;
452 priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
454 /* Act only on response IDs we recognize */
455 if (is_stock_accept_response_id (response_id)
456 && !priv->response_requested
457 && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
459 g_signal_stop_emission_by_name (dialog, "response");
462 priv->response_requested = FALSE;
466 gtk_file_chooser_dialog_new_valist (const gchar *title,
468 GtkFileChooserAction action,
469 const gchar *backend,
470 const gchar *first_button_text,
474 const char *button_text = first_button_text;
477 result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
480 "file-system-backend", backend,
484 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
488 response_id = va_arg (varargs, gint);
489 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
490 button_text = va_arg (varargs, const gchar *);
497 * gtk_file_chooser_dialog_new:
498 * @title: Title of the dialog, or %NULL
499 * @parent: Transient parent of the dialog, or %NULL
500 * @action: Open or save mode for the dialog
501 * @first_button_text: stock ID or text to go in the first button, or %NULL
502 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
504 * Creates a new #GtkFileChooserDialog. This function is analogous to
505 * gtk_dialog_new_with_buttons().
507 * Return value: a new #GtkFileChooserDialog
512 gtk_file_chooser_dialog_new (const gchar *title,
514 GtkFileChooserAction action,
515 const gchar *first_button_text,
521 va_start (varargs, first_button_text);
522 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
523 NULL, first_button_text,
531 * gtk_file_chooser_dialog_new_with_backend:
532 * @title: Title of the dialog, or %NULL
533 * @parent: Transient parent of the dialog, or %NULL
534 * @action: Open or save mode for the dialog
535 * @backend: The name of the specific filesystem backend to use.
536 * @first_button_text: stock ID or text to go in the first button, or %NULL
537 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
539 * Creates a new #GtkFileChooserDialog with a specified backend. This is
540 * especially useful if you use gtk_file_chooser_set_local_only() to allow
541 * non-local files and you use a more expressive vfs, such as gnome-vfs,
544 * Return value: a new #GtkFileChooserDialog
549 gtk_file_chooser_dialog_new_with_backend (const gchar *title,
551 GtkFileChooserAction action,
552 const gchar *backend,
553 const gchar *first_button_text,
559 va_start (varargs, first_button_text);
560 result = gtk_file_chooser_dialog_new_valist (title, parent, action,
561 backend, first_button_text,
568 #define __GTK_FILE_CHOOSER_DIALOG_C__
569 #include "gtkaliasdef.c"