1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
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 "gdk/gdkkeysyms.h"
22 #include "gtkalignment.h"
23 #include "gtkbindings.h"
24 #include "gtkbutton.h"
25 #include "gtkcellrendererpixbuf.h"
26 #include "gtkcellrendererseptext.h"
27 #include "gtkcellrenderertext.h"
28 #include "gtkcombobox.h"
30 #include "gtkexpander.h"
31 #include "gtkfilechooserdefault.h"
32 #include "gtkfilechooserembed.h"
33 #include "gtkfilechooserentry.h"
34 #include "gtkfilechooserutils.h"
35 #include "gtkfilechooser.h"
36 #include "gtkfilesystemmodel.h"
39 #include "gtkhpaned.h"
40 #include "gtkicontheme.h"
44 #include "gtkmarshalers.h"
45 #include "gtkmenuitem.h"
46 #include "gtkmessagedialog.h"
47 #include "gtkpathbar.h"
48 #include "gtkprivate.h"
49 #include "gtkscrolledwindow.h"
50 #include "gtksizegroup.h"
53 #include "gtktreeview.h"
54 #include "gtktreemodelsort.h"
55 #include "gtktreeselection.h"
56 #include "gtktreestore.h"
57 #include "gtktypebuiltins.h"
60 #if defined (G_OS_UNIX)
61 #include "gtkfilesystemunix.h"
62 #elif defined (G_OS_WIN32)
63 #include "gtkfilesystemwin32.h"
69 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
71 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
72 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
73 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
75 #define PREVIEW_HBOX_SPACING 12
77 struct _GtkFileChooserDefaultClass
79 GtkVBoxClass parent_class;
82 struct _GtkFileChooserDefault
84 GtkVBox parent_instance;
86 GtkFileChooserAction action;
88 GtkFileSystem *file_system;
90 /* Save mode widgets */
91 GtkWidget *save_widgets;
93 GtkWidget *save_file_name_entry;
94 GtkWidget *save_folder_label;
95 GtkWidget *save_folder_combo;
96 GtkWidget *save_extra_align;
97 GtkWidget *save_expander;
99 /* The file browsing widgets */
100 GtkWidget *browse_widgets;
101 GtkWidget *browse_shortcuts_tree_view;
102 GtkWidget *browse_shortcuts_swin;
103 GtkWidget *browse_shortcuts_add_button;
104 GtkWidget *browse_shortcuts_remove_button;
105 GtkWidget *browse_files_swin;
106 GtkWidget *browse_files_tree_view;
107 GtkWidget *browse_directories_swin;
108 GtkWidget *browse_directories_tree_view;
109 GtkWidget *browse_new_folder_button;
110 GtkWidget *browse_path_bar;
111 GtkWidget *browse_extra_align;
112 GtkTreeModel *browse_shortcuts_model;
113 GtkFileSystemModel *browse_files_model;
114 GtkFileSystemModel *browse_directories_model;
116 GtkWidget *filter_combo;
117 GtkWidget *preview_widget;
118 GtkWidget *extra_widget;
120 GtkListStore *shortcuts_model;
121 GtkTreeModelSort *sort_model;
123 GtkFileFilter *current_filter;
127 gboolean has_desktop;
133 guint volumes_changed_id;
134 guint bookmarks_changed_id;
136 GtkFilePath *current_volume_path;
137 GtkFilePath *current_folder;
138 GtkFilePath *preview_path;
140 GtkWidget *preview_frame;
142 GtkTreeViewColumn *list_name_column;
143 GtkCellRenderer *list_name_renderer;
148 guint local_only : 1;
149 guint preview_widget_active : 1;
150 guint select_multiple : 1;
151 guint show_hidden : 1;
152 guint list_sort_ascending : 1;
153 guint changing_folder : 1;
164 static guint signals[LAST_SIGNAL] = { 0 };
166 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
168 SHORTCUTS_COL_PIXBUF,
171 SHORTCUTS_COL_REMOVABLE,
172 SHORTCUTS_COL_PIXBUF_VISIBLE,
173 SHORTCUTS_COL_NUM_COLUMNS
176 /* Column numbers for the file list */
181 FILE_LIST_COL_NUM_COLUMNS
184 /* Identifiers for target types */
189 /* Target types for DnD in the shortcuts list */
190 static GtkTargetEntry shortcuts_targets[] = {
191 { "text/uri-list", 0, TEXT_URI_LIST }
194 static const int num_shortcuts_targets = sizeof (shortcuts_targets) / sizeof (shortcuts_targets[0]);
196 /* Interesting places in the shortcuts bar */
206 /* Standard icon size */
207 /* FIXME: maybe this should correspond to the font size in the tree views... */
210 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
211 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
212 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
213 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
215 static GObject* gtk_file_chooser_default_constructor (GType type,
216 guint n_construct_properties,
217 GObjectConstructParam *construct_params);
218 static void gtk_file_chooser_default_finalize (GObject *object);
219 static void gtk_file_chooser_default_set_property (GObject *object,
223 static void gtk_file_chooser_default_get_property (GObject *object,
227 static void gtk_file_chooser_default_dispose (GObject *object);
228 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
229 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
230 GtkStyle *previous_style);
231 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
232 GdkScreen *previous_screen);
234 static void gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
235 const GtkFilePath *path);
236 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
237 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
239 static void gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
240 const GtkFilePath *path);
241 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
242 const GtkFilePath *path);
243 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
244 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
245 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
246 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
247 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
248 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
249 GtkFileFilter *filter);
250 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
251 GtkFileFilter *filter);
252 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
253 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
254 const GtkFilePath *path,
256 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
257 const GtkFilePath *path,
259 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
260 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
262 gint *default_height);
263 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
264 gboolean *resize_horizontally,
265 gboolean *resize_vertically);
267 static void location_popup_handler (GtkFileChooserDefault *impl);
268 static void up_folder_handler (GtkFileChooserDefault *impl);
269 static void home_folder_handler (GtkFileChooserDefault *impl);
270 static void update_appearance (GtkFileChooserDefault *impl);
272 static void set_current_filter (GtkFileChooserDefault *impl,
273 GtkFileFilter *filter);
274 static void check_preview_change (GtkFileChooserDefault *impl);
276 static void filter_combo_changed (GtkComboBox *combo_box,
277 GtkFileChooserDefault *impl);
278 static void tree_selection_changed (GtkTreeSelection *tree_selection,
279 GtkFileChooserDefault *impl);
281 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
283 GtkTreeViewColumn *column,
284 GtkFileChooserDefault *impl);
285 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
288 gboolean path_currently_selected,
291 static void list_selection_changed (GtkTreeSelection *tree_selection,
292 GtkFileChooserDefault *impl);
293 static void list_row_activated (GtkTreeView *tree_view,
295 GtkTreeViewColumn *column,
296 GtkFileChooserDefault *impl);
298 static void path_bar_clicked (GtkPathBar *path_bar,
299 GtkFilePath *file_path,
300 GtkFileChooserDefault *impl);
302 static void add_bookmark_button_clicked_cb (GtkButton *button,
303 GtkFileChooserDefault *impl);
304 static void remove_bookmark_button_clicked_cb (GtkButton *button,
305 GtkFileChooserDefault *impl);
307 static void tree_name_data_func (GtkTreeViewColumn *tree_column,
308 GtkCellRenderer *cell,
309 GtkTreeModel *tree_model,
312 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
313 GtkCellRenderer *cell,
314 GtkTreeModel *tree_model,
317 static void list_name_data_func (GtkTreeViewColumn *tree_column,
318 GtkCellRenderer *cell,
319 GtkTreeModel *tree_model,
323 static void list_size_data_func (GtkTreeViewColumn *tree_column,
324 GtkCellRenderer *cell,
325 GtkTreeModel *tree_model,
329 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
330 GtkCellRenderer *cell,
331 GtkTreeModel *tree_model,
335 static GObjectClass *parent_class;
338 _gtk_file_chooser_default_get_type (void)
340 static GType file_chooser_default_type = 0;
342 if (!file_chooser_default_type)
344 static const GTypeInfo file_chooser_default_info =
346 sizeof (GtkFileChooserDefaultClass),
347 NULL, /* base_init */
348 NULL, /* base_finalize */
349 (GClassInitFunc) gtk_file_chooser_default_class_init,
350 NULL, /* class_finalize */
351 NULL, /* class_data */
352 sizeof (GtkFileChooserDefault),
354 (GInstanceInitFunc) gtk_file_chooser_default_init,
357 static const GInterfaceInfo file_chooser_info =
359 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
360 NULL, /* interface_finalize */
361 NULL /* interface_data */
364 static const GInterfaceInfo file_chooser_embed_info =
366 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
367 NULL, /* interface_finalize */
368 NULL /* interface_data */
371 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
372 &file_chooser_default_info, 0);
374 g_type_add_interface_static (file_chooser_default_type,
375 GTK_TYPE_FILE_CHOOSER,
377 g_type_add_interface_static (file_chooser_default_type,
378 GTK_TYPE_FILE_CHOOSER_EMBED,
379 &file_chooser_embed_info);
382 return file_chooser_default_type;
386 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
388 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
389 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
390 GtkBindingSet *binding_set;
392 parent_class = g_type_class_peek_parent (class);
394 gobject_class->finalize = gtk_file_chooser_default_finalize;
395 gobject_class->constructor = gtk_file_chooser_default_constructor;
396 gobject_class->set_property = gtk_file_chooser_default_set_property;
397 gobject_class->get_property = gtk_file_chooser_default_get_property;
398 gobject_class->dispose = gtk_file_chooser_default_dispose;
400 widget_class->show_all = gtk_file_chooser_default_show_all;
401 widget_class->style_set = gtk_file_chooser_default_style_set;
402 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
404 signals[LOCATION_POPUP] =
405 _gtk_binding_signal_new ("location-popup",
406 G_OBJECT_CLASS_TYPE (class),
407 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
408 G_CALLBACK (location_popup_handler),
410 _gtk_marshal_VOID__VOID,
413 _gtk_binding_signal_new ("up-folder",
414 G_OBJECT_CLASS_TYPE (class),
415 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
416 G_CALLBACK (up_folder_handler),
418 _gtk_marshal_VOID__VOID,
420 signals[HOME_FOLDER] =
421 _gtk_binding_signal_new ("home-folder",
422 G_OBJECT_CLASS_TYPE (class),
423 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
424 G_CALLBACK (home_folder_handler),
426 _gtk_marshal_VOID__VOID,
429 binding_set = gtk_binding_set_by_class (class);
431 gtk_binding_entry_add_signal (binding_set,
432 GDK_l, GDK_CONTROL_MASK,
436 gtk_binding_entry_add_signal (binding_set,
437 GDK_Up, GDK_MOD1_MASK,
440 gtk_binding_entry_add_signal (binding_set,
441 GDK_KP_Up, GDK_MOD1_MASK,
445 gtk_binding_entry_add_signal (binding_set,
446 GDK_Home, GDK_MOD1_MASK,
449 gtk_binding_entry_add_signal (binding_set,
450 GDK_KP_Home, GDK_MOD1_MASK,
454 _gtk_file_chooser_install_properties (gobject_class);
458 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
460 iface->select_path = gtk_file_chooser_default_select_path;
461 iface->unselect_path = gtk_file_chooser_default_unselect_path;
462 iface->select_all = gtk_file_chooser_default_select_all;
463 iface->unselect_all = gtk_file_chooser_default_unselect_all;
464 iface->get_paths = gtk_file_chooser_default_get_paths;
465 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
466 iface->get_file_system = gtk_file_chooser_default_get_file_system;
467 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
468 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
469 iface->set_current_name = gtk_file_chooser_default_set_current_name;
470 iface->add_filter = gtk_file_chooser_default_add_filter;
471 iface->remove_filter = gtk_file_chooser_default_remove_filter;
472 iface->list_filters = gtk_file_chooser_default_list_filters;
473 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
474 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
475 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
479 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
481 iface->get_default_size = gtk_file_chooser_default_get_default_size;
482 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
485 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
487 impl->local_only = TRUE;
488 impl->preview_widget_active = TRUE;
489 impl->select_multiple = FALSE;
490 impl->show_hidden = FALSE;
492 gtk_box_set_spacing (GTK_BOX (impl), 12);
496 gtk_file_chooser_default_finalize (GObject *object)
498 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
501 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
502 impl->volumes_changed_id = 0;
503 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
504 impl->bookmarks_changed_id = 0;
505 g_object_unref (impl->file_system);
507 for (l = impl->filters; l; l = l->next)
509 GtkFileFilter *filter;
511 filter = GTK_FILE_FILTER (l->data);
512 g_object_unref (filter);
514 g_slist_free (impl->filters);
516 if (impl->current_filter)
517 g_object_unref (impl->current_filter);
519 if (impl->current_volume_path)
520 gtk_file_path_free (impl->current_volume_path);
522 if (impl->current_folder)
523 gtk_file_path_free (impl->current_folder);
525 if (impl->preview_path)
526 gtk_file_path_free (impl->preview_path);
528 G_OBJECT_CLASS (parent_class)->finalize (object);
531 /* Shows an error dialog set as transient for the specified window */
533 error_message_with_parent (GtkWindow *parent,
538 dialog = gtk_message_dialog_new (parent,
539 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
544 gtk_dialog_run (GTK_DIALOG (dialog));
545 gtk_widget_destroy (dialog);
548 /* Shows an error dialog for the file chooser */
550 error_message (GtkFileChooserDefault *impl,
555 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
556 if (!GTK_WIDGET_TOPLEVEL (toplevel))
559 error_message_with_parent (toplevel ? GTK_WINDOW (toplevel) : NULL,
563 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
565 error_dialog (GtkFileChooserDefault *impl,
567 const GtkFilePath *path,
572 text = g_strdup_printf (msg,
573 gtk_file_path_get_string (path),
575 error_message (impl, text);
577 g_error_free (error);
580 /* Displays an error message about not being able to get information for a file.
581 * Frees the GError as well.
584 error_getting_info_dialog (GtkFileChooserDefault *impl,
585 const GtkFilePath *path,
589 _("Could not retrieve information about %s:\n%s"),
593 /* Shows an error dialog about not being able to add a bookmark */
595 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
596 const GtkFilePath *path,
600 _("Could not add a bookmark for %s:\n%s"),
604 /* Shows an error dialog about not being able to compose a filename */
606 error_building_filename_dialog (GtkFileChooserDefault *impl,
607 const GtkFilePath *base_path,
608 const char *file_part,
613 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
614 gtk_file_path_get_string (base_path),
617 error_message (impl, msg);
619 g_error_free (error);
623 update_preview_widget_visibility (GtkFileChooserDefault *impl)
625 if (impl->preview_widget_active && impl->preview_widget)
626 gtk_widget_show (impl->preview_frame);
628 gtk_widget_hide (impl->preview_frame);
630 g_signal_emit_by_name (impl, "default-size-changed");
634 set_preview_widget (GtkFileChooserDefault *impl,
635 GtkWidget *preview_widget)
637 if (preview_widget == impl->preview_widget)
640 if (impl->preview_widget)
641 gtk_container_remove (GTK_CONTAINER (impl->preview_frame),
642 impl->preview_widget);
644 impl->preview_widget = preview_widget;
645 if (impl->preview_widget)
647 gtk_widget_show_all (impl->preview_widget);
648 gtk_container_add (GTK_CONTAINER (impl->preview_frame),
649 impl->preview_widget);
652 update_preview_widget_visibility (impl);
655 /* Clears the selection in the shortcuts tree */
657 shortcuts_unselect_all (GtkFileChooserDefault *impl)
659 GtkTreeSelection *selection;
661 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
662 gtk_tree_selection_unselect_all (selection);
665 /* Convenience function to get the display name and icon info for a path */
667 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
669 GtkFilePath *parent_path;
670 GtkFileFolder *parent_folder;
673 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
676 parent_folder = gtk_file_system_get_folder (file_system, parent_path,
677 GTK_FILE_INFO_DISPLAY_NAME
681 | GTK_FILE_INFO_IS_FOLDER,
683 gtk_file_path_free (parent_path);
688 info = gtk_file_folder_get_info (parent_folder, path, error);
689 g_object_unref (parent_folder);
694 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
695 * inserts a volume. A position of -1 indicates the end of the tree.
698 shortcuts_insert_path (GtkFileChooserDefault *impl,
701 GtkFileSystemVolume *volume,
702 const GtkFilePath *path,
715 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
716 pixbuf = gtk_file_system_volume_render_icon (impl->file_system,
726 info = get_file_info (impl->file_system, path, error);
730 data = gtk_file_path_copy (path);
733 label_copy = g_strdup (label);
735 label_copy = g_strdup (gtk_file_info_get_display_name (info));
737 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
739 gtk_file_info_free (info);
743 gtk_list_store_append (impl->shortcuts_model, &iter);
745 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
747 gtk_list_store_set (impl->shortcuts_model, &iter,
748 SHORTCUTS_COL_PIXBUF, pixbuf,
749 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
750 SHORTCUTS_COL_NAME, label_copy,
751 SHORTCUTS_COL_PATH, data,
752 SHORTCUTS_COL_REMOVABLE, removable,
758 g_object_unref (pixbuf);
763 /* Appends an item for the user's home directory to the shortcuts model */
765 shortcuts_append_home (GtkFileChooserDefault *impl)
768 GtkFilePath *home_path;
771 home = g_get_home_dir ();
772 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
775 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
777 error_getting_info_dialog (impl, home_path, error);
779 gtk_file_path_free (home_path);
782 /* Appends the ~/Desktop directory to the shortcuts model */
784 shortcuts_append_desktop (GtkFileChooserDefault *impl)
789 name = g_build_filename (g_get_home_dir (), "Desktop", NULL);
790 path = gtk_file_system_filename_to_path (impl->file_system, name);
793 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
794 /* We do not actually pop up an error dialog if there is no desktop directory
795 * because some people may really not want to have one.
798 gtk_file_path_free (path);
801 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
803 shortcuts_append_paths (GtkFileChooserDefault *impl,
810 for (; paths; paths = paths->next)
818 /* NULL GError, but we don't really want to show error boxes here */
820 if (shortcuts_insert_path (impl, -1, FALSE, NULL, path, NULL, TRUE, NULL))
827 /* Returns the index for the corresponding item in the shortcuts bar */
829 shortcuts_get_index (GtkFileChooserDefault *impl,
830 ShortcutsIndex where)
836 if (where == SHORTCUTS_HOME)
839 n += impl->has_home ? 1 : 0;
841 if (where == SHORTCUTS_DESKTOP)
844 n += impl->has_desktop ? 1 : 0;
846 if (where == SHORTCUTS_VOLUMES)
849 n += impl->num_volumes;
851 if (where == SHORTCUTS_SHORTCUTS)
854 n += impl->num_shortcuts;
856 if (where == SHORTCUTS_SEPARATOR)
859 /* If there are no bookmarks there won't be a separator */
860 n += impl->num_shortcuts > 0 ? 1 : 0;
862 if (where == SHORTCUTS_BOOKMARKS)
865 g_assert_not_reached ();
872 typedef void (* RemoveFunc) (GtkFileChooserDefault *impl, gpointer data);
874 /* Removes the specified number of rows from the shortcuts list */
876 shortcuts_remove_rows (GtkFileChooserDefault *impl,
879 RemoveFunc remove_fn)
883 path = gtk_tree_path_new_from_indices (start_row, -1);
885 for (; n_rows; n_rows--)
890 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
891 g_assert_not_reached ();
895 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
896 (* remove_fn) (impl, data);
899 gtk_list_store_remove (impl->shortcuts_model, &iter);
902 gtk_tree_path_free (path);
905 /* Used from shortcuts_remove_rows() */
907 volume_remove_cb (GtkFileChooserDefault *impl, gpointer data)
909 GtkFileSystemVolume *volume;
912 gtk_file_system_volume_free (impl->file_system, volume);
915 /* Adds all the file system volumes to the shortcuts model */
917 shortcuts_add_volumes (GtkFileChooserDefault *impl)
923 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
924 shortcuts_remove_rows (impl, start_row, impl->num_volumes, volume_remove_cb);
925 impl->num_volumes = 0;
927 list = gtk_file_system_list_volumes (impl->file_system);
931 for (l = list; l; l = l->next)
933 GtkFileSystemVolume *volume;
937 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL);
941 impl->num_volumes = n;
946 /* Used from shortcuts_remove_rows() */
948 remove_bookmark_cb (GtkFileChooserDefault *impl, gpointer data)
953 gtk_file_path_free (path);
956 /* Inserts the bookmarks separator node */
958 shortcuts_insert_separator (GtkFileChooserDefault *impl)
962 gtk_list_store_insert (impl->shortcuts_model, &iter,
963 shortcuts_get_index (impl, SHORTCUTS_SEPARATOR));
964 gtk_list_store_set (impl->shortcuts_model, &iter,
965 SHORTCUTS_COL_PIXBUF, NULL,
966 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
967 SHORTCUTS_COL_NAME, NULL,
968 SHORTCUTS_COL_PATH, NULL,
972 /* Creates the GtkTreeStore used as the shortcuts model */
973 /* Updates the list of bookmarks */
975 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
979 if (impl->num_bookmarks > 0)
981 shortcuts_remove_rows (impl,
982 shortcuts_get_index (impl, SHORTCUTS_SEPARATOR),
983 impl->num_bookmarks + 1,
988 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
989 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
990 gtk_file_paths_free (bookmarks);
992 if (impl->num_bookmarks > 0)
994 shortcuts_insert_separator (impl);
999 shortcuts_model_create (GtkFileChooserDefault *impl)
1001 if (impl->shortcuts_model)
1002 g_object_unref (impl->shortcuts_model);
1004 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1005 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1006 GDK_TYPE_PIXBUF, /* pixbuf */
1007 G_TYPE_STRING, /* name */
1008 G_TYPE_POINTER, /* path or volume */
1009 G_TYPE_BOOLEAN, /* removable */
1010 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1012 if (impl->file_system)
1014 shortcuts_append_home (impl);
1015 shortcuts_append_desktop (impl);
1016 shortcuts_add_volumes (impl);
1017 shortcuts_add_bookmarks (impl);
1020 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), GTK_TREE_MODEL (impl->shortcuts_model));
1023 /* Callback used when the "New Folder" toolbar button is clicked */
1025 new_folder_button_clicked (GtkButton *button,
1026 GtkFileChooserDefault *impl)
1031 /* FIXME: this doesn't work for folder mode, just for file mode */
1033 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1034 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1036 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1037 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1039 impl->list_name_column,
1043 /* Callback used from the text cell renderer when the new folder is named */
1045 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1047 const gchar *new_text,
1048 GtkFileChooserDefault *impl)
1051 GtkFilePath *file_path;
1053 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1054 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1057 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, new_text, &error);
1060 error_building_filename_dialog (impl, impl->current_folder, new_text, error);
1065 if (!gtk_file_system_create_folder (impl->file_system, file_path, &error))
1067 _("Could not create folder %s:\n%s"),
1070 gtk_file_path_free (file_path);
1072 /* FIXME: scroll to the new folder and select it */
1075 /* Callback used from the text cell renderer when the new folder edition gets
1079 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1080 GtkFileChooserDefault *impl)
1082 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1083 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1086 /* Creates the widgets for the filter combo box */
1088 filter_create (GtkFileChooserDefault *impl)
1090 impl->filter_combo = gtk_combo_box_new_text ();
1091 g_signal_connect (impl->filter_combo, "changed",
1092 G_CALLBACK (filter_combo_changed), impl);
1094 return impl->filter_combo;
1098 button_new (GtkFileChooserDefault *impl,
1100 const char *stock_id,
1110 button = gtk_button_new ();
1111 hbox = gtk_hbox_new (FALSE, 2);
1112 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1114 gtk_container_add (GTK_CONTAINER (button), align);
1115 gtk_container_add (GTK_CONTAINER (align), hbox);
1116 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1118 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1120 widget = gtk_label_new_with_mnemonic (text);
1121 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1122 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1124 gtk_widget_set_sensitive (button, sensitive);
1125 g_signal_connect (button, "clicked", callback, impl);
1127 gtk_widget_show_all (align);
1130 gtk_widget_show (button);
1135 /* Creates the widgets for the folder tree */
1137 create_folder_tree (GtkFileChooserDefault *impl)
1139 GtkTreeSelection *selection;
1141 /* Scrolled window */
1143 impl->browse_directories_swin = gtk_scrolled_window_new (NULL, NULL);
1144 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_directories_swin),
1145 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1146 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_directories_swin),
1150 impl->browse_directories_tree_view = gtk_tree_view_new ();
1151 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_directories_tree_view), FALSE);
1153 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
1154 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_directories_tree_view),
1157 num_shortcuts_targets,
1160 g_signal_connect (selection, "changed",
1161 G_CALLBACK (tree_selection_changed), impl);
1163 gtk_container_add (GTK_CONTAINER (impl->browse_directories_swin), impl->browse_directories_tree_view);
1164 gtk_widget_show (impl->browse_directories_tree_view);
1168 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (impl->browse_directories_tree_view), 0,
1170 gtk_cell_renderer_text_new (),
1171 tree_name_data_func, impl, NULL);
1172 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_directories_tree_view),
1173 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
1175 return impl->browse_directories_swin;
1178 /* Returns whether a path is already present in the shortcuts list */
1180 shortcut_exists (GtkFileChooserDefault *impl,
1181 const GtkFilePath *path)
1190 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1194 separator_idx = shortcuts_get_index (impl, SHORTCUTS_SEPARATOR);
1195 volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1203 if (i == separator_idx)
1206 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1208 if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
1210 GtkFileSystemVolume *volume;
1211 GtkFilePath *base_path;
1214 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1216 exists = strcmp (gtk_file_path_get_string (path),
1217 gtk_file_path_get_string (base_path)) == 0;
1225 GtkFilePath *model_path;
1229 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1236 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
1242 /* Tries to add a bookmark from a path name */
1244 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1245 const GtkFilePath *path)
1250 if (shortcut_exists (impl, path))
1254 info = get_file_info (impl->file_system, path, &error);
1257 error_getting_info_dialog (impl, path, error);
1258 else if (!gtk_file_info_get_is_folder (info))
1262 msg = g_strdup_printf (_("Could not add bookmark for %s because it is not a folder."),
1263 gtk_file_path_get_string (path));
1264 error_message (impl, msg);
1270 if (!gtk_file_system_insert_bookmark (impl->file_system, path, -1, &error))
1271 error_could_not_add_bookmark_dialog (impl, path, error);
1276 add_bookmark_foreach_cb (GtkTreeModel *model,
1281 GtkFileChooserDefault *impl;
1282 GtkFileSystemModel *fs_model;
1283 GtkTreeIter child_iter;
1284 const GtkFilePath *file_path;
1286 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1288 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1289 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1291 fs_model = impl->browse_directories_model;
1296 fs_model = impl->browse_files_model;
1297 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1300 file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &child_iter);
1301 shortcuts_add_bookmark_from_path (impl, file_path);
1304 /* Callback used when the "Add bookmark" button is clicked */
1306 add_bookmark_button_clicked_cb (GtkButton *button,
1307 GtkFileChooserDefault *impl)
1309 GtkWidget *tree_view;
1310 GtkTreeSelection *selection;
1312 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1313 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1314 tree_view = impl->browse_directories_tree_view;
1316 tree_view = impl->browse_files_tree_view;
1318 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
1319 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1320 shortcuts_add_bookmark_from_path (impl, impl->current_folder);
1322 gtk_tree_selection_selected_foreach (selection,
1323 add_bookmark_foreach_cb,
1327 /* Callback used when the "Remove bookmark" button is clicked */
1329 remove_bookmark_button_clicked_cb (GtkButton *button,
1330 GtkFileChooserDefault *impl)
1332 GtkTreeSelection *selection;
1338 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1341 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1343 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1344 SHORTCUTS_COL_PATH, &path,
1345 SHORTCUTS_COL_REMOVABLE, &removable, -1);
1348 g_assert_not_reached ();
1353 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1355 _("Could not remove bookmark for %s:\n%s"),
1361 struct is_folders_foreach_closure {
1362 GtkFileChooserDefault *impl;
1363 gboolean all_folders;
1366 /* Used from gtk_tree_selection_selected_foreach() */
1368 is_folders_foreach_cb (GtkTreeModel *model,
1373 struct is_folders_foreach_closure *closure;
1374 GtkTreeIter child_iter;
1375 const GtkFileInfo *info;
1379 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1381 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
1382 closure->all_folders &= gtk_file_info_get_is_folder (info);
1385 /* Returns whether the selected items in the file list are all folders */
1387 selection_is_folders (GtkFileChooserDefault *impl)
1389 struct is_folders_foreach_closure closure;
1390 GtkTreeSelection *selection;
1392 closure.impl = impl;
1393 closure.all_folders = TRUE;
1395 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1396 gtk_tree_selection_selected_foreach (selection,
1397 is_folders_foreach_cb,
1400 return closure.all_folders;
1403 /* Sensitize the "add bookmark" button if all the selected items are folders, or
1404 * if there are no selected items *and* the current folder is not in the
1405 * bookmarks list. De-sensitize the button otherwise.
1408 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1410 GtkWidget *tree_view;
1411 GtkTreeSelection *selection;
1414 /* Check selection */
1416 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1417 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1418 tree_view = impl->browse_directories_tree_view;
1420 tree_view = impl->browse_files_tree_view;
1422 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
1424 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1425 active = !shortcut_exists (impl, impl->current_folder);
1427 active = (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1428 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER ||
1429 selection_is_folders (impl));
1431 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
1434 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
1435 * bookmark row is selected in the shortcuts tree.
1438 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
1440 GtkTreeSelection *selection;
1442 gboolean removable = FALSE;
1444 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1446 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1447 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1448 SHORTCUTS_COL_REMOVABLE, &removable,
1451 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
1454 /* Converts raw selection data from text/uri-list to a list of strings */
1456 split_uris (const char *data)
1459 const char *p, *start;
1465 for (p = start; *p != 0; p++)
1466 if (*p == '\r' && *(p + 1) == '\n')
1470 name = g_strndup (start, p - start);
1471 uris = g_slist_prepend (uris, name);
1477 uris = g_slist_reverse (uris);
1481 /* Callback used when we get the drag data for the bookmarks list. We add the
1482 * received URIs as bookmarks if they are folders.
1485 shortcuts_drag_data_received_cb (GtkWidget *widget,
1486 GdkDragContext *context,
1489 GtkSelectionData *selection_data,
1494 GtkFileChooserDefault *impl;
1497 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1499 uris = split_uris (selection_data->data);
1501 for (l = uris; l; l = l->next)
1507 path = gtk_file_system_uri_to_path (impl->file_system, uri);
1511 shortcuts_add_bookmark_from_path (impl, path);
1512 gtk_file_path_free (path);
1518 msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
1520 error_message (impl, msg);
1527 g_slist_free (uris);
1530 /* Callback used when the selection in the shortcuts tree changes */
1532 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
1533 GtkFileChooserDefault *impl)
1535 bookmarks_check_remove_sensitivity (impl);
1538 /* Creates the widgets for the shortcuts and bookmarks tree */
1540 shortcuts_list_create (GtkFileChooserDefault *impl)
1542 GtkTreeSelection *selection;
1543 GtkTreeViewColumn *column;
1544 GtkCellRenderer *renderer;
1546 /* Scrolled window */
1548 impl->browse_shortcuts_swin = gtk_scrolled_window_new (NULL, NULL);
1549 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_shortcuts_swin),
1550 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1551 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_shortcuts_swin),
1553 gtk_widget_show (impl->browse_shortcuts_swin);
1557 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
1558 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
1560 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
1561 GTK_DEST_DEFAULT_ALL,
1563 num_shortcuts_targets,
1566 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1567 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
1568 gtk_tree_selection_set_select_function (selection,
1569 shortcuts_select_func,
1572 g_signal_connect (selection, "changed",
1573 G_CALLBACK (shortcuts_selection_changed_cb), impl);
1575 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
1576 G_CALLBACK (shortcuts_row_activated_cb), impl);
1578 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
1579 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
1581 gtk_container_add (GTK_CONTAINER (impl->browse_shortcuts_swin), impl->browse_shortcuts_tree_view);
1582 gtk_widget_show (impl->browse_shortcuts_tree_view);
1586 shortcuts_model_create (impl);
1590 column = gtk_tree_view_column_new ();
1591 gtk_tree_view_column_set_title (column, _("Folder"));
1593 renderer = gtk_cell_renderer_pixbuf_new ();
1594 gtk_tree_view_column_pack_start (column, renderer, FALSE);
1595 gtk_tree_view_column_set_attributes (column, renderer,
1596 "pixbuf", SHORTCUTS_COL_PIXBUF,
1597 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
1600 renderer = _gtk_cell_renderer_sep_text_new ();
1601 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1602 gtk_tree_view_column_set_attributes (column, renderer,
1603 "text", SHORTCUTS_COL_NAME,
1606 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
1608 return impl->browse_shortcuts_swin;
1611 /* Creates the widgets for the shortcuts/bookmarks pane */
1613 shortcuts_pane_create (GtkFileChooserDefault *impl,
1614 GtkSizeGroup *size_group)
1620 vbox = gtk_vbox_new (FALSE, 6);
1621 gtk_widget_show (vbox);
1623 /* Shortcuts tree */
1625 widget = shortcuts_list_create (impl);
1626 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
1628 /* Box for buttons */
1630 hbox = gtk_hbox_new (TRUE, 6);
1631 gtk_size_group_add_widget (size_group, hbox);
1632 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1633 gtk_widget_show (hbox);
1635 /* Add bookmark button */
1637 impl->browse_shortcuts_add_button = button_new (impl,
1642 G_CALLBACK (add_bookmark_button_clicked_cb));
1643 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
1645 /* Remove bookmark button */
1647 impl->browse_shortcuts_remove_button = button_new (impl,
1652 G_CALLBACK (remove_bookmark_button_clicked_cb));
1653 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
1658 /* Creates the widgets for the file list */
1660 create_file_list (GtkFileChooserDefault *impl)
1662 GtkTreeSelection *selection;
1663 GtkTreeViewColumn *column;
1664 GtkCellRenderer *renderer;
1666 /* Scrolled window */
1668 impl->browse_files_swin = gtk_scrolled_window_new (NULL, NULL);
1669 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->browse_files_swin),
1670 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1671 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->browse_files_swin),
1674 /* Tree/list view */
1676 impl->browse_files_tree_view = gtk_tree_view_new ();
1677 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
1678 gtk_container_add (GTK_CONTAINER (impl->browse_files_swin), impl->browse_files_tree_view);
1679 g_signal_connect (impl->browse_files_tree_view, "row_activated",
1680 G_CALLBACK (list_row_activated), impl);
1681 gtk_widget_show (impl->browse_files_tree_view);
1683 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1684 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
1687 num_shortcuts_targets,
1690 g_signal_connect (selection, "changed",
1691 G_CALLBACK (list_selection_changed), impl);
1693 /* Filename column */
1695 impl->list_name_column = gtk_tree_view_column_new ();
1696 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
1697 gtk_tree_view_column_set_title (impl->list_name_column, _("File name"));
1698 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
1700 renderer = gtk_cell_renderer_pixbuf_new ();
1701 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
1702 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
1703 list_icon_data_func, impl, NULL);
1705 impl->list_name_renderer = gtk_cell_renderer_text_new ();
1706 g_signal_connect (impl->list_name_renderer, "edited",
1707 G_CALLBACK (renderer_edited_cb), impl);
1708 g_signal_connect (impl->list_name_renderer, "editing-canceled",
1709 G_CALLBACK (renderer_editing_canceled_cb), impl);
1710 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
1711 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
1712 list_name_data_func, impl, NULL);
1714 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
1718 column = gtk_tree_view_column_new ();
1719 gtk_tree_view_column_set_title (column, _("Size"));
1721 renderer = gtk_cell_renderer_text_new ();
1722 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1723 gtk_tree_view_column_set_cell_data_func (column, renderer,
1724 list_size_data_func, impl, NULL);
1725 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
1726 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
1728 /* Modification time column */
1730 column = gtk_tree_view_column_new ();
1731 gtk_tree_view_column_set_title (column, _("Modified"));
1733 renderer = gtk_cell_renderer_text_new ();
1734 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1735 gtk_tree_view_column_set_cell_data_func (column, renderer,
1736 list_mtime_data_func, impl, NULL);
1737 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
1738 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
1740 return impl->browse_files_swin;
1744 create_filename_entry_and_filter_combo (GtkFileChooserDefault *impl)
1749 hbox = gtk_hbox_new (FALSE, 12);
1750 gtk_widget_show (hbox);
1754 widget = filter_create (impl);
1755 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1760 /* Creates the widgets for the files/folders pane */
1762 file_pane_create (GtkFileChooserDefault *impl,
1763 GtkSizeGroup *size_group)
1769 vbox = gtk_vbox_new (FALSE, 6);
1770 gtk_widget_show (vbox);
1772 /* The path bar and 'Create Folder' button */
1773 hbox = gtk_hbox_new (FALSE, 12);
1774 gtk_widget_show (hbox);
1775 impl->browse_path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
1776 g_signal_connect (impl->browse_path_bar, "path_clicked", G_CALLBACK (path_bar_clicked), impl);
1777 gtk_widget_show_all (impl->browse_path_bar);
1778 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
1781 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create _Folder"));
1782 g_signal_connect (impl->browse_new_folder_button, "clicked",
1783 G_CALLBACK (new_folder_button_clicked), impl);
1784 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
1785 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1787 /* Box for lists and preview */
1789 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
1790 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
1791 gtk_widget_show (hbox);
1795 widget = create_folder_tree (impl);
1796 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1800 widget = create_file_list (impl);
1801 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1805 impl->preview_frame = gtk_frame_new (_("Preview"));
1806 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_frame, FALSE, FALSE, 0);
1807 /* Don't show preview frame initially */
1809 /* Filename entry and filter combo */
1810 hbox = gtk_hbox_new (FALSE, 0);
1811 gtk_size_group_add_widget (size_group, hbox);
1812 widget = create_filename_entry_and_filter_combo (impl);
1813 gtk_box_pack_end (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1814 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1815 gtk_widget_show (hbox);
1819 /* Callback used when the "Browse for more folders" expander is toggled */
1821 expander_changed_cb (GtkExpander *expander,
1823 GtkFileChooserDefault *impl)
1827 active = gtk_expander_get_expanded (expander);
1828 update_appearance (impl);
1831 /* Creates the widgets specific to Save mode */
1833 save_widgets_create (GtkFileChooserDefault *impl)
1838 GtkWidget *alignment;
1840 vbox = gtk_vbox_new (FALSE, 12);
1842 table = gtk_table_new (2, 2, FALSE);
1843 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
1844 gtk_widget_show (table);
1845 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
1846 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1850 widget = gtk_label_new_with_mnemonic (_("_Name:"));
1851 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
1852 gtk_table_attach (GTK_TABLE (table), widget,
1856 gtk_widget_show (widget);
1858 impl->save_file_name_entry = gtk_entry_new ();
1859 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 25);
1860 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
1861 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
1863 GTK_EXPAND | GTK_FILL, 0,
1865 gtk_widget_show (impl->save_file_name_entry);
1866 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
1869 impl->save_folder_label = gtk_label_new_with_mnemonic (_("Save in _Folder:"));
1870 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
1871 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
1875 gtk_widget_show (impl->save_folder_label);
1877 /* FIXME: create the combo */
1880 impl->save_extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
1881 gtk_box_pack_start (GTK_BOX (vbox), impl->save_extra_align, FALSE, FALSE, 0);
1884 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
1885 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
1887 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
1888 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
1889 g_signal_connect (impl->save_expander, "notify::expanded",
1890 G_CALLBACK (expander_changed_cb),
1892 gtk_widget_show_all (alignment);
1897 /* Creates the main hpaned with the widgets shared by Open and Save mode */
1899 browse_widgets_create (GtkFileChooserDefault *impl)
1904 GtkSizeGroup *size_group;
1906 /* size group is used by the [+][-] buttons and the filter combo */
1907 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
1908 vbox = gtk_vbox_new (FALSE, 12);
1911 hpaned = gtk_hpaned_new ();
1912 gtk_widget_show (hpaned);
1913 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
1914 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
1916 widget = shortcuts_pane_create (impl, size_group);
1917 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
1918 widget = file_pane_create (impl, size_group);
1919 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
1921 /* Alignment to hold custom widget */
1922 impl->browse_extra_align = gtk_alignment_new (0.0, .5, 1.0, 1.0);
1923 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_extra_align, FALSE, FALSE, 0);
1929 gtk_file_chooser_default_constructor (GType type,
1930 guint n_construct_properties,
1931 GObjectConstructParam *construct_params)
1933 GtkFileChooserDefault *impl;
1936 object = parent_class->constructor (type,
1937 n_construct_properties,
1939 impl = GTK_FILE_CHOOSER_DEFAULT (object);
1941 g_assert (impl->file_system);
1943 gtk_widget_push_composite_child ();
1945 /* Widgets for Save mode */
1946 impl->save_widgets = save_widgets_create (impl);
1947 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
1949 /* The browse widgets */
1950 impl->browse_widgets = browse_widgets_create (impl);
1951 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
1953 gtk_widget_pop_composite_child ();
1954 update_appearance (impl);
1959 /* Sets the extra_widget by packing it in the appropriate place */
1961 set_extra_widget (GtkFileChooserDefault *impl,
1962 GtkWidget *extra_widget)
1966 g_object_ref (extra_widget);
1967 /* FIXME: is this right ? */
1968 gtk_widget_show (extra_widget);
1971 if (impl->extra_widget)
1972 g_object_unref (impl->extra_widget);
1974 impl->extra_widget = extra_widget;
1978 volumes_changed_cb (GtkFileSystem *file_system,
1979 GtkFileChooserDefault *impl)
1981 shortcuts_add_volumes (impl);
1984 /* Callback used when the set of bookmarks changes in the file system */
1986 bookmarks_changed_cb (GtkFileSystem *file_system,
1987 GtkFileChooserDefault *impl)
1989 shortcuts_add_bookmarks (impl);
1991 bookmarks_check_add_sensitivity (impl);
1992 bookmarks_check_remove_sensitivity (impl);
1995 /* Sets the file chooser to multiple selection mode */
1997 set_select_multiple (GtkFileChooserDefault *impl,
1998 gboolean select_multiple,
1999 gboolean property_notify)
2001 GtkTreeSelection *selection;
2002 GtkSelectionMode mode;
2004 if (select_multiple == impl->select_multiple)
2007 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
2009 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
2010 gtk_tree_selection_set_mode (selection, mode);
2012 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2013 gtk_tree_selection_set_mode (selection, mode);
2015 impl->select_multiple = select_multiple;
2016 g_object_notify (G_OBJECT (impl), "select-multiple");
2018 /* FIXME #132255: See note in check_preview_change() */
2019 check_preview_change (impl);
2023 set_file_system_backend (GtkFileChooserDefault *impl,
2024 const char *backend)
2026 if (impl->file_system)
2028 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
2029 impl->volumes_changed_id = 0;
2030 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
2031 impl->bookmarks_changed_id = 0;
2032 g_object_unref (impl->file_system);
2035 impl->file_system = NULL;
2037 impl->file_system = _gtk_file_system_create (backend);
2039 if (!impl->file_system)
2041 #if defined (G_OS_UNIX)
2042 impl->file_system = gtk_file_system_unix_new ();
2043 #elif defined (G_OS_WIN32)
2044 impl->file_system = gtk_file_system_win32_new ();
2046 #error "No default filesystem implementation on the platform"
2050 if (impl->file_system)
2052 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
2053 G_CALLBACK (volumes_changed_cb),
2055 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
2056 G_CALLBACK (bookmarks_changed_cb),
2061 /* This function is basically a do_all function.
2063 * It sets the visibility on all the widgets based on the current state, and
2064 * moves the custom_widget if needed.
2067 update_appearance (GtkFileChooserDefault *impl)
2071 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
2072 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2074 gtk_widget_show (impl->save_widgets);
2076 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
2078 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
2079 /*gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);*/
2080 gtk_widget_show (impl->browse_widgets);
2084 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
2085 /*gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);*/
2086 gtk_widget_hide (impl->browse_widgets);
2089 gtk_widget_show (impl->browse_new_folder_button);
2091 if (impl->select_multiple)
2093 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
2094 "Re-setting to single selection mode.");
2095 set_select_multiple (impl, FALSE, TRUE);
2098 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2099 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
2101 gtk_widget_hide (impl->save_widgets);
2102 gtk_widget_show (impl->browse_widgets);
2105 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2106 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2108 gtk_widget_show (impl->browse_new_folder_button);
2109 gtk_widget_hide (impl->browse_files_swin);
2110 gtk_widget_show (impl->browse_directories_swin);
2114 gtk_widget_hide (impl->browse_new_folder_button);
2115 gtk_widget_hide (impl->browse_directories_swin);
2116 gtk_widget_show (impl->browse_files_swin);
2119 if (impl->extra_widget)
2122 GtkWidget *unused_align;
2124 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2126 align = impl->save_extra_align;
2127 unused_align = impl->browse_extra_align;
2131 align = impl->browse_extra_align;
2132 unused_align = impl->save_extra_align;
2135 /* We own a ref on extra_widget, so it's safe to do this */
2136 child = GTK_BIN (unused_align)->child;
2138 gtk_container_remove (GTK_CONTAINER (unused_align), child);
2140 child = GTK_BIN (align)->child;
2141 if (child && child != impl->extra_widget)
2143 gtk_container_remove (GTK_CONTAINER (align), child);
2144 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
2146 else if (child == NULL)
2148 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
2151 gtk_widget_show (align);
2152 gtk_widget_hide (unused_align);
2156 child = GTK_BIN (impl->browse_extra_align)->child;
2158 gtk_container_remove (GTK_CONTAINER (impl->browse_extra_align), child);
2160 child = GTK_BIN (impl->save_extra_align)->child;
2162 gtk_container_remove (GTK_CONTAINER (impl->save_extra_align), child);
2164 gtk_widget_hide (impl->save_extra_align);
2165 gtk_widget_hide (impl->browse_extra_align);
2167 g_signal_emit_by_name (impl, "default-size-changed");
2171 gtk_file_chooser_default_set_property (GObject *object,
2173 const GValue *value,
2177 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2181 case GTK_FILE_CHOOSER_PROP_ACTION:
2183 GtkFileChooserAction action = g_value_get_enum (value);
2185 if (action != impl->action)
2187 if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
2189 g_warning ("Multiple selection mode is not allowed in Save mode");
2190 set_select_multiple (impl, FALSE, TRUE);
2192 impl->action = action;
2193 update_appearance (impl);
2197 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
2198 set_file_system_backend (impl, g_value_get_string (value));
2200 case GTK_FILE_CHOOSER_PROP_FILTER:
2201 set_current_filter (impl, g_value_get_object (value));
2203 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2204 impl->local_only = g_value_get_boolean (value);
2206 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2207 set_preview_widget (impl, g_value_get_object (value));
2209 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2210 impl->preview_widget_active = g_value_get_boolean (value);
2211 update_preview_widget_visibility (impl);
2213 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2214 set_extra_widget (impl, g_value_get_object (value));
2215 update_appearance (impl);
2217 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2219 gboolean select_multiple = g_value_get_boolean (value);
2220 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
2222 g_warning ("Multiple selection mode is not allowed in Save mode");
2226 set_select_multiple (impl, select_multiple, FALSE);
2229 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2231 gboolean show_hidden = g_value_get_boolean (value);
2232 if (show_hidden != impl->show_hidden)
2234 impl->show_hidden = show_hidden;
2235 _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2237 _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_files_model),
2243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2249 gtk_file_chooser_default_get_property (GObject *object,
2254 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
2258 case GTK_FILE_CHOOSER_PROP_ACTION:
2259 g_value_set_enum (value, impl->action);
2261 case GTK_FILE_CHOOSER_PROP_FILTER:
2262 g_value_set_object (value, impl->current_filter);
2264 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
2265 g_value_set_boolean (value, impl->local_only);
2267 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
2268 g_value_set_object (value, impl->preview_widget);
2270 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
2271 g_value_set_boolean (value, impl->preview_widget_active);
2273 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
2274 g_value_set_object (value, impl->extra_widget);
2276 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
2277 g_value_set_boolean (value, impl->select_multiple);
2279 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
2280 g_value_set_boolean (value, impl->show_hidden);
2283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2290 gtk_file_chooser_default_dispose (GObject *object)
2292 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
2294 if (impl->extra_widget)
2296 g_object_unref (impl->extra_widget);
2297 impl->extra_widget = NULL;
2299 G_OBJECT_CLASS (parent_class)->dispose (object);
2302 /* We override show-all since we have internal widgets that
2303 * shouldn't be shown when you call show_all(), like the filter
2307 gtk_file_chooser_default_show_all (GtkWidget *widget)
2309 gtk_widget_show (widget);
2313 gtk_file_chooser_default_style_set (GtkWidget *widget,
2314 GtkStyle *previous_style)
2316 if (GTK_WIDGET_CLASS (parent_class)->style_set)
2317 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
2319 g_signal_emit_by_name (widget, "default-size-changed");
2323 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
2324 GdkScreen *previous_screen)
2326 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
2327 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
2329 g_signal_emit_by_name (widget, "default-size-changed");
2333 expand_and_select_func (GtkFileSystemModel *model,
2338 GtkFileChooserDefault *impl = user_data;
2339 GtkTreeView *tree_view;
2341 if (model == impl->browse_directories_model)
2342 tree_view = GTK_TREE_VIEW (impl->browse_directories_tree_view);
2344 tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2346 gtk_tree_view_expand_to_path (tree_view, path);
2347 gtk_tree_view_expand_row (tree_view, path, FALSE);
2348 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
2349 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_directories_tree_view), path, NULL, TRUE, 0.3, 0.5);
2353 list_model_filter_func (GtkFileSystemModel *model,
2355 const GtkFileInfo *file_info,
2358 GtkFileChooserDefault *impl = user_data;
2359 GtkFileFilterInfo filter_info;
2360 GtkFileFilterFlags needed;
2363 if (!impl->current_filter)
2366 if (gtk_file_info_get_is_folder (file_info))
2369 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
2371 needed = gtk_file_filter_get_needed (impl->current_filter);
2373 filter_info.display_name = gtk_file_info_get_display_name (file_info);
2374 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
2376 if (needed & GTK_FILE_FILTER_FILENAME)
2378 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
2379 if (filter_info.filename)
2380 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
2383 filter_info.filename = NULL;
2385 if (needed & GTK_FILE_FILTER_URI)
2387 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
2388 if (filter_info.filename)
2389 filter_info.contains |= GTK_FILE_FILTER_URI;
2392 filter_info.uri = NULL;
2394 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
2396 if (filter_info.filename)
2397 g_free ((gchar *)filter_info.filename);
2398 if (filter_info.uri)
2399 g_free ((gchar *)filter_info.uri);
2405 install_list_model_filter (GtkFileChooserDefault *impl)
2407 if (impl->current_filter)
2408 _gtk_file_system_model_set_filter (impl->browse_files_model,
2409 list_model_filter_func,
2413 #define COMPARE_DIRECTORIES \
2414 GtkFileChooserDefault *impl = user_data; \
2415 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
2416 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
2417 gboolean dir_a, dir_b; \
2420 dir_a = gtk_file_info_get_is_folder (info_a); \
2422 return impl->list_sort_ascending ? -1 : 1; \
2425 dir_b = gtk_file_info_get_is_folder (info_b); \
2427 return impl->list_sort_ascending ? 1 : -1; \
2429 if (dir_a != dir_b) \
2430 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
2432 /* Sort callback for the filename column */
2434 name_sort_func (GtkTreeModel *model,
2439 COMPARE_DIRECTORIES;
2441 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
2444 /* Sort callback for the size column */
2446 size_sort_func (GtkTreeModel *model,
2451 COMPARE_DIRECTORIES;
2454 gint64 size_a = gtk_file_info_get_size (info_a);
2455 gint64 size_b = gtk_file_info_get_size (info_b);
2457 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
2461 /* Sort callback for the mtime column */
2463 mtime_sort_func (GtkTreeModel *model,
2468 COMPARE_DIRECTORIES;
2471 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
2472 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
2474 return ta > tb ? -1 : (ta == tb ? 0 : 1);
2478 /* Callback used when the sort column changes. We cache the sort order for use
2479 * in name_sort_func().
2482 list_sort_column_changed_cb (GtkTreeSortable *sortable,
2483 GtkFileChooserDefault *impl)
2485 GtkSortType sort_type;
2487 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
2488 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
2491 /* Gets rid of the old list model and creates a new one for the current folder */
2493 set_list_model (GtkFileChooserDefault *impl)
2495 if (impl->browse_files_model)
2497 g_object_unref (impl->browse_files_model);
2498 g_object_unref (impl->sort_model);
2501 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
2502 impl->current_folder, 0,
2504 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
2505 install_list_model_filter (impl);
2507 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
2508 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
2509 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
2510 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
2511 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
2512 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
2513 impl->list_sort_ascending = TRUE;
2515 g_signal_connect (impl->sort_model, "sort_column_changed",
2516 G_CALLBACK (list_sort_column_changed_cb), impl);
2518 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
2519 GTK_TREE_MODEL (impl->sort_model));
2520 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
2521 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
2522 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
2525 /* Gets rid of the old folder tree model and creates a new one for the volume
2526 * corresponding to the specified path.
2529 set_tree_model (GtkFileChooserDefault *impl, const GtkFilePath *path)
2531 GtkFileSystemVolume *volume;
2532 GtkFilePath *base_path, *parent_path;
2536 volume = gtk_file_system_get_volume_for_path (impl->file_system, path);
2539 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2541 if (base_path == NULL)
2543 base_path = gtk_file_path_copy (path);
2544 while (gtk_file_system_get_parent (impl->file_system,
2548 parent_path != NULL)
2550 gtk_file_path_free (base_path);
2551 base_path = parent_path;
2555 if (impl->current_volume_path && gtk_file_path_compare (base_path, impl->current_volume_path) == 0)
2558 if (impl->browse_directories_model)
2559 g_object_unref (impl->browse_directories_model);
2561 impl->current_volume_path = gtk_file_path_copy (base_path);
2563 impl->browse_directories_model = _gtk_file_system_model_new (impl->file_system, impl->current_volume_path, -1,
2564 GTK_FILE_INFO_DISPLAY_NAME);
2565 _gtk_file_system_model_set_show_files (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2567 _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2570 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_directories_tree_view),
2571 GTK_TREE_MODEL (impl->browse_directories_model));
2575 gtk_file_path_free (base_path);
2577 gtk_file_system_volume_free (impl->file_system, volume);
2581 update_chooser_entry (GtkFileChooserDefault *impl)
2583 GtkTreeSelection *selection;
2584 const GtkFileInfo *info;
2586 GtkTreeIter child_iter;
2588 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
2591 g_assert (!impl->select_multiple);
2592 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2594 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2597 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2601 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
2603 if (!gtk_file_info_get_is_folder (info))
2604 gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry),
2605 gtk_file_info_get_display_name (info));
2609 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
2610 const GtkFilePath *path)
2612 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2614 if (impl->current_folder)
2615 gtk_file_path_free (impl->current_folder);
2617 impl->current_folder = gtk_file_path_copy (path);
2619 /* Change the current folder label */
2620 gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, impl->file_system, NULL);
2622 /* Update the folder tree */
2624 if (!impl->changing_folder)
2626 impl->changing_folder = TRUE;
2627 set_tree_model (impl, impl->current_folder);
2628 _gtk_file_system_model_path_do (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
2629 path, expand_and_select_func, impl);
2630 impl->changing_folder = FALSE;
2633 /* Create a new list model */
2634 set_list_model (impl);
2636 /* Refresh controls */
2638 shortcuts_unselect_all (impl);
2640 g_signal_emit_by_name (impl, "current-folder-changed", 0);
2642 check_preview_change (impl);
2643 bookmarks_check_add_sensitivity (impl);
2645 g_signal_emit_by_name (impl, "selection-changed", 0);
2648 static GtkFilePath *
2649 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
2651 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2653 return gtk_file_path_copy (impl->current_folder);
2657 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
2660 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2662 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE);
2664 gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry), name);
2668 select_func (GtkFileSystemModel *model,
2673 GtkFileChooserDefault *impl = user_data;
2674 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2675 GtkTreePath *sorted_path;
2677 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
2678 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
2679 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_directories_tree_view), sorted_path, NULL, TRUE, 0.3, 0.0);
2680 gtk_tree_path_free (sorted_path);
2684 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
2685 const GtkFilePath *path)
2687 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2688 GtkFilePath *parent_path;
2692 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, &error))
2694 error_getting_info_dialog (impl, path, error);
2700 _gtk_file_chooser_set_current_folder_path (chooser, path);
2704 _gtk_file_chooser_set_current_folder_path (chooser, parent_path);
2705 gtk_file_path_free (parent_path);
2706 _gtk_file_system_model_path_do (impl->browse_files_model, path,
2712 unselect_func (GtkFileSystemModel *model,
2717 GtkFileChooserDefault *impl = user_data;
2718 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
2719 GtkTreePath *sorted_path;
2721 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
2723 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
2725 gtk_tree_path_free (sorted_path);
2729 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
2730 const GtkFilePath *path)
2732 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2734 _gtk_file_system_model_path_do (impl->browse_files_model, path,
2735 unselect_func, impl);
2739 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
2741 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2742 if (impl->select_multiple)
2744 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2745 gtk_tree_selection_select_all (selection);
2750 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
2752 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2753 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2755 gtk_tree_selection_unselect_all (selection);
2758 struct get_paths_closure {
2759 GtkFileChooserDefault *impl;
2761 GtkFilePath *path_from_entry;
2765 get_paths_foreach (GtkTreeModel *model,
2770 struct get_paths_closure *info;
2771 const GtkFilePath *file_path;
2772 GtkFileSystemModel *fs_model;
2773 GtkTreeIter sel_iter;
2777 if (info->impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2778 info->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2780 fs_model = info->impl->browse_directories_model;
2785 fs_model = info->impl->browse_files_model;
2786 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
2789 file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &sel_iter);
2791 if (!info->path_from_entry
2792 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
2793 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
2797 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
2799 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2800 struct get_paths_closure info;
2804 info.path_from_entry = NULL;
2806 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
2808 const char *filename;
2810 filename = gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry));
2812 if (filename != NULL && filename[0] != '\0')
2814 GtkFilePath *selected;
2815 GError *error = NULL;
2817 selected = gtk_file_system_make_path (impl->file_system, impl->current_folder, filename, &error);
2821 error_building_filename_dialog (impl, impl->current_folder, filename, error);
2825 info.path_from_entry = selected;
2829 if (!info.path_from_entry || impl->select_multiple)
2831 GtkTreeSelection *selection;
2835 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2836 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
2838 if (impl->browse_directories_model)
2839 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_directories_tree_view));
2843 if (impl->sort_model)
2844 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2848 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
2851 if (info.path_from_entry)
2852 info.result = g_slist_prepend (info.result, info.path_from_entry);
2854 return g_slist_reverse (info.result);
2857 static GtkFilePath *
2858 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
2860 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2862 if (impl->preview_path)
2863 return gtk_file_path_copy (impl->preview_path);
2868 static GtkFileSystem *
2869 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
2871 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2873 return impl->file_system;
2876 /* Shows or hides the filter widgets */
2878 toolbar_show_filters (GtkFileChooserDefault *impl,
2882 gtk_widget_show (impl->filter_combo);
2884 gtk_widget_hide (impl->filter_combo);
2888 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
2889 GtkFileFilter *filter)
2891 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2894 if (g_slist_find (impl->filters, filter))
2896 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
2900 g_object_ref (filter);
2901 gtk_object_sink (GTK_OBJECT (filter));
2902 impl->filters = g_slist_append (impl->filters, filter);
2904 name = gtk_file_filter_get_name (filter);
2906 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
2908 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
2910 if (!g_slist_find (impl->filters, impl->current_filter))
2911 set_current_filter (impl, filter);
2913 toolbar_show_filters (impl, TRUE);
2917 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
2918 GtkFileFilter *filter)
2920 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2921 GtkTreeModel *model;
2925 filter_index = g_slist_index (impl->filters, filter);
2927 if (filter_index < 0)
2929 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
2933 impl->filters = g_slist_remove (impl->filters, filter);
2935 if (filter == impl->current_filter)
2938 set_current_filter (impl, impl->filters->data);
2940 set_current_filter (impl, NULL);
2943 /* Remove row from the combo box */
2944 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
2945 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
2946 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
2948 g_object_unref (filter);
2951 toolbar_show_filters (impl, FALSE);
2955 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
2957 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2959 return g_slist_copy (impl->filters);
2962 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
2964 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
2967 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
2971 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
2972 const GtkFilePath *path,
2975 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2979 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
2981 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
2984 impl->num_shortcuts++;
2990 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
2991 const GtkFilePath *path,
2994 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
2999 if (impl->num_shortcuts == 0)
3002 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
3003 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
3004 g_assert_not_reached ();
3006 for (i = 0; i < impl->num_shortcuts; i++)
3008 GtkFilePath *shortcut;
3010 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
3011 g_assert (shortcut != NULL);
3013 if (gtk_file_path_compare (shortcut, path) == 0)
3015 /* The other columns are freed by the GtkTreeStore */
3016 gtk_file_path_free (shortcut);
3017 gtk_list_store_remove (impl->shortcuts_model, &iter);
3018 impl->num_shortcuts--;
3022 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
3023 g_assert_not_reached ();
3029 GTK_FILE_CHOOSER_ERROR,
3030 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
3031 _("shortcut %s does not exist"),
3032 gtk_file_path_get_string (path));
3038 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
3040 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3046 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
3047 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
3048 g_assert_not_reached ();
3052 for (i = 0; i < impl->num_shortcuts; i++)
3054 GtkFilePath *shortcut;
3056 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
3057 g_assert (shortcut != NULL);
3059 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
3061 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
3062 g_assert_not_reached ();
3065 return g_slist_reverse (list);
3068 #define NUM_LINES 40
3069 #define NUM_CHARS 50
3071 /* Guesses a size based upon font sizes */
3073 find_good_size_from_style (GtkWidget *widget,
3077 GtkFileChooserDefault *impl;
3078 gint default_width, default_height;
3081 GtkRequisition preview_req;
3083 g_assert (widget->style != NULL);
3084 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3086 font_size = pango_font_description_get_size (widget->style->font_desc);
3087 font_size = PANGO_PIXELS (font_size);
3089 default_width = font_size * NUM_CHARS;
3090 default_height = font_size * NUM_LINES;
3092 /* Use at least the requisition size not including the preview widget */
3093 gtk_widget_size_request (widget, &req);
3095 if (impl->preview_widget_active && impl->preview_widget)
3096 gtk_widget_size_request (impl->preview_frame, &preview_req);
3098 preview_req.width = 0;
3100 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
3101 default_height = MAX (default_height, req.height);
3103 *width = default_width;
3104 *height = default_height;
3108 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
3109 gint *default_width,
3110 gint *default_height)
3112 GtkFileChooserDefault *impl;
3114 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
3116 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
3118 if (impl->preview_widget_active && impl->preview_widget)
3119 *default_width += impl->preview_widget->requisition.width + PREVIEW_HBOX_SPACING;
3123 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
3124 gboolean *resize_horizontally,
3125 gboolean *resize_vertically)
3127 GtkFileChooserDefault *impl;
3129 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
3131 if (*resize_horizontally)
3132 *resize_horizontally = TRUE;
3134 if (resize_vertically)
3136 *resize_vertically = TRUE;
3138 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3139 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3141 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3142 *resize_vertically = FALSE;
3148 set_current_filter (GtkFileChooserDefault *impl,
3149 GtkFileFilter *filter)
3151 if (impl->current_filter != filter)
3155 /* If we have filters, new filter must be one of them
3157 filter_index = g_slist_index (impl->filters, filter);
3158 if (impl->filters && filter_index < 0)
3161 if (impl->current_filter)
3162 g_object_unref (impl->current_filter);
3163 impl->current_filter = filter;
3164 if (impl->current_filter)
3166 g_object_ref (impl->current_filter);
3167 gtk_object_sink (GTK_OBJECT (filter));
3171 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
3174 install_list_model_filter (impl);
3176 g_object_notify (G_OBJECT (impl), "filter");
3181 open_and_close (GtkTreeView *tree_view,
3182 GtkTreePath *target_path)
3184 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
3188 path = gtk_tree_path_new ();
3189 gtk_tree_path_append_index (path, 0);
3191 gtk_tree_model_get_iter (model, &iter, path);
3195 if (gtk_tree_path_is_ancestor (path, target_path) ||
3196 gtk_tree_path_compare (path, target_path) == 0)
3198 GtkTreeIter child_iter;
3199 gtk_tree_view_expand_row (tree_view, path, FALSE);
3200 if (gtk_tree_model_iter_children (model, &child_iter, &iter))
3203 gtk_tree_path_down (path);
3208 gtk_tree_view_collapse_row (tree_view, path);
3212 GtkTreeIter parent_iter;
3213 GtkTreeIter next_iter;
3216 if (gtk_tree_model_iter_next (model, &next_iter))
3219 gtk_tree_path_next (path);
3223 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
3227 gtk_tree_path_up (path);
3234 gtk_tree_path_free (path);
3238 filter_combo_changed (GtkComboBox *combo_box,
3239 GtkFileChooserDefault *impl)
3241 gint new_index = gtk_combo_box_get_active (combo_box);
3242 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
3244 set_current_filter (impl, new_filter);
3248 check_preview_change (GtkFileChooserDefault *impl)
3250 const GtkFilePath *new_path = NULL;
3252 /* FIXME #132255: Fixing preview for multiple selection involves getting the
3253 * full selection and diffing to find out what the most recently selected file
3254 * is; there is logic in GtkFileSelection that probably can be
3257 if (impl->sort_model && !impl->select_multiple)
3259 GtkTreeSelection *selection;
3262 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3263 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
3265 GtkTreeIter child_iter;
3267 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3268 &child_iter, &iter);
3270 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3274 if (new_path != impl->preview_path &&
3275 !(new_path && impl->preview_path &&
3276 gtk_file_path_compare (new_path, impl->preview_path) == 0))
3278 if (impl->preview_path)
3279 gtk_file_path_free (impl->preview_path);
3282 impl->preview_path = gtk_file_path_copy (new_path);
3284 impl->preview_path = NULL;
3286 g_signal_emit_by_name (impl, "update-preview");
3291 tree_selection_changed (GtkTreeSelection *selection,
3292 GtkFileChooserDefault *impl)
3295 const GtkFilePath *file_path;
3298 /* FIXME #132255: Fixing this for multiple selection involves getting the full
3299 * selection and diffing to find out what the most recently selected file is;
3300 * there is logic in GtkFileSelection that probably can be copied;
3301 * check_preview_change() is similar.
3303 if (impl->select_multiple
3304 || !gtk_tree_selection_get_selected (selection, NULL, &iter))
3307 file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
3309 if (impl->current_folder && gtk_file_path_compare (file_path, impl->current_folder) == 0)
3312 /* Close the tree up to only the parents of the newly selected
3313 * node and it's immediate children are visible.
3315 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_directories_model), &iter);
3316 open_and_close (GTK_TREE_VIEW (impl->browse_directories_tree_view), path);
3317 gtk_tree_path_free (path);
3319 if (!impl->changing_folder)
3320 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3323 /* Activates a volume by mounting it if necessary and then switching to its
3327 shortcuts_activate_volume (GtkFileChooserDefault *impl,
3328 GtkFileSystemVolume *volume)
3332 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
3337 if (!gtk_file_system_volume_mount (impl->file_system, volume, &error))
3341 msg = g_strdup_printf ("Could not mount %s:\n%s",
3342 gtk_file_system_volume_get_display_name (impl->file_system, volume),
3344 error_message (impl, msg);
3346 g_error_free (error);
3352 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
3353 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path);
3354 gtk_file_path_free (path);
3357 /* Callback used when a row in the shortcuts list is activated */
3359 shortcuts_row_activated_cb (GtkTreeView *tree_view,
3361 GtkTreeViewColumn *column,
3362 GtkFileChooserDefault *impl)
3365 int selected, start_row;
3368 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3371 selected = *gtk_tree_path_get_indices (path);
3373 if (selected == shortcuts_get_index (impl, SHORTCUTS_SEPARATOR))
3376 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
3378 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
3379 if (selected >= start_row && selected < start_row + impl->num_volumes)
3381 GtkFileSystemVolume *volume;
3384 shortcuts_activate_volume (impl, volume);
3388 GtkFilePath *file_path;
3391 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3396 shortcuts_select_func (GtkTreeSelection *selection,
3397 GtkTreeModel *model,
3399 gboolean path_currently_selected,
3402 GtkFileChooserDefault *impl = data;
3404 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_SEPARATOR));
3408 list_selection_changed (GtkTreeSelection *selection,
3409 GtkFileChooserDefault *impl)
3411 /* See if we are in the new folder editable row for Save mode */
3412 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3414 GtkTreeSelection *selection;
3415 GtkTreeIter iter, child_iter;
3416 const GtkFileInfo *info;
3418 g_assert (!impl->select_multiple);
3419 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3420 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
3423 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3427 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3429 return; /* We are on the editable row for New Folder */
3432 update_chooser_entry (impl);
3433 check_preview_change (impl);
3434 bookmarks_check_add_sensitivity (impl);
3436 g_signal_emit_by_name (impl, "selection-changed", 0);
3439 /* Callback used when a row in the file list is activated */
3441 list_row_activated (GtkTreeView *tree_view,
3443 GtkTreeViewColumn *column,
3444 GtkFileChooserDefault *impl)
3446 GtkTreeIter iter, child_iter;
3447 const GtkFileInfo *info;
3449 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
3452 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
3454 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3456 if (gtk_file_info_get_is_folder (info))
3458 const GtkFilePath *file_path;
3460 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3461 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3466 g_signal_emit_by_name (impl, "file-activated");
3470 path_bar_clicked (GtkPathBar *path_bar,
3471 GtkFilePath *file_path,
3472 GtkFileChooserDefault *impl)
3474 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
3477 static const GtkFileInfo *
3478 get_list_file_info (GtkFileChooserDefault *impl,
3481 GtkTreeIter child_iter;
3483 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3487 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3491 tree_name_data_func (GtkTreeViewColumn *tree_column,
3492 GtkCellRenderer *cell,
3493 GtkTreeModel *tree_model,
3497 GtkFileChooserDefault *impl = data;
3498 const GtkFileInfo *info;
3500 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (impl->browse_directories_model),
3506 "text", gtk_file_info_get_display_name (info),
3512 list_icon_data_func (GtkTreeViewColumn *tree_column,
3513 GtkCellRenderer *cell,
3514 GtkTreeModel *tree_model,
3518 GtkFileChooserDefault *impl = data;
3519 GtkTreeIter child_iter;
3520 const GtkFilePath *path;
3523 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3526 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
3530 /* FIXME: NULL GError */
3531 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl), ICON_SIZE, NULL);
3537 g_object_unref (pixbuf);
3540 /* Sets a cellrenderer's text, making it bold if the GtkFileInfo is a folder */
3542 set_cell_text_bold_if_folder (const GtkFileInfo *info, GtkCellRenderer *cell, const char *text)
3546 "weight", gtk_file_info_get_is_folder (info) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
3551 list_name_data_func (GtkTreeViewColumn *tree_column,
3552 GtkCellRenderer *cell,
3553 GtkTreeModel *tree_model,
3557 GtkFileChooserDefault *impl = data;
3558 const GtkFileInfo *info = get_list_file_info (impl, iter);
3563 "text", _("Type name of new folder"),
3568 set_cell_text_bold_if_folder (info, cell, gtk_file_info_get_display_name (info));
3573 list_size_data_func (GtkTreeViewColumn *tree_column,
3574 GtkCellRenderer *cell,
3575 GtkTreeModel *tree_model,
3579 GtkFileChooserDefault *impl = data;
3580 const GtkFileInfo *info = get_list_file_info (impl, iter);
3584 if (!info || gtk_file_info_get_is_folder (info))
3587 size = gtk_file_info_get_size (info);
3589 if (size < (gint64)1024)
3590 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
3591 else if (size < (gint64)1024*1024)
3592 str = g_strdup_printf (_("%.1f K"), size / (1024.));
3593 else if (size < (gint64)1024*1024*1024)
3594 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
3596 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
3606 /* Tree column data callback for the file list; fetches the mtime of a file */
3608 list_mtime_data_func (GtkTreeViewColumn *tree_column,
3609 GtkCellRenderer *cell,
3610 GtkTreeModel *tree_model,
3614 GtkFileChooserDefault *impl;
3615 const GtkFileInfo *info;
3616 GtkFileTime time_mtime, time_now;
3623 info = get_list_file_info (impl, iter);
3632 time_mtime = gtk_file_info_get_modification_time (info);
3633 g_date_set_time (&mtime, (GTime) time_mtime);
3635 time_now = (GTime ) time (NULL);
3636 g_date_set_time (&now, (GTime) time_now);
3638 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
3641 strcpy (buf, _("Today"));
3642 else if (days_diff == 1)
3643 strcpy (buf, _("Yesterday"));
3648 if (days_diff > 1 && days_diff < 7)
3649 format = "%A"; /* Days from last week */
3651 /* FIXME: Get the right format for the locale */
3652 format = _("%d/%b/%Y"); /* Any other date */
3654 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
3655 strcpy (buf, _("Unknown"));
3658 set_cell_text_bold_if_folder (info, cell, buf);
3662 _gtk_file_chooser_default_new (const char *file_system)
3664 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
3665 "file-system-backend", file_system,
3670 location_entry_create (GtkFileChooserDefault *impl)
3674 entry = _gtk_file_chooser_entry_new ();
3675 /* Pick a good width for the entry */
3676 gtk_entry_set_width_chars (GTK_ENTRY (entry), 25);
3677 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
3678 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
3679 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
3681 return GTK_WIDGET (entry);
3685 update_from_entry (GtkFileChooserDefault *impl,
3687 GtkFileChooserEntry *chooser_entry)
3689 const GtkFilePath *folder_path;
3690 const char *file_part;
3692 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
3693 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
3695 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
3697 error_message_with_parent (parent,
3698 _("Cannot change to the folder you specified as it is an invalid path."));
3702 if (file_part[0] == '\0')
3704 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), folder_path);
3709 GtkFileFolder *folder = NULL;
3710 GtkFilePath *subfolder_path = NULL;
3711 GtkFileInfo *info = NULL;
3714 /* If the file part is non-empty, we need to figure out if it refers to a
3715 * folder within folder. We could optimize the case here where the folder
3716 * is already loaded for one of our tree models.
3720 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
3724 error_getting_info_dialog (impl, folder_path, error);
3729 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
3731 if (!subfolder_path)
3735 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
3736 gtk_file_path_get_string (folder_path),
3739 error_message (impl, msg);
3741 g_object_unref (folder);
3746 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
3751 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3753 g_object_unref (folder);
3754 gtk_file_path_free (subfolder_path);
3758 error_getting_info_dialog (impl, subfolder_path, error);
3759 g_object_unref (folder);
3760 gtk_file_path_free (subfolder_path);
3764 if (gtk_file_info_get_is_folder (info))
3765 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), subfolder_path);
3767 _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path);
3769 g_object_unref (folder);
3770 gtk_file_path_free (subfolder_path);
3771 gtk_file_info_free (info);
3776 location_popup_handler (GtkFileChooserDefault *impl)
3779 GtkWidget *toplevel;
3786 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
3787 if (!GTK_WIDGET_TOPLEVEL (toplevel))
3790 dialog = gtk_dialog_new_with_buttons (_("Open Location"),
3791 GTK_WINDOW (toplevel),
3792 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
3793 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3794 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
3796 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
3797 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
3798 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
3799 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
3801 hbox = gtk_hbox_new (FALSE, 12);
3802 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
3803 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
3805 label = gtk_label_new_with_mnemonic (_("_Location:"));
3806 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
3808 entry = location_entry_create (impl);
3809 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
3810 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
3814 gtk_widget_show_all (dialog);
3815 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
3816 update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry));
3818 gtk_widget_destroy (dialog);
3821 /* Handler for the "up-folder" keybinding signal */
3823 up_folder_handler (GtkFileChooserDefault *impl)
3825 GtkFilePath *parent_path;
3829 if (gtk_file_system_get_parent (impl->file_system, impl->current_folder, &parent_path, &error))
3831 if (parent_path) /* If we were on a root, parent_path will be NULL */
3833 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), parent_path);
3834 gtk_file_path_free (parent_path);
3839 _("Could not go to the parent folder of %s:\n%s"),
3840 impl->current_folder,
3844 /* Handler for the "home-folder" keybinding signal */
3846 home_folder_handler (GtkFileChooserDefault *impl)
3850 /* Should we pull this information from impl->has_home and the shortcuts data
3851 * instead? Sounds like a bit of overkill...
3854 home = g_get_home_dir ();
3855 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), home);