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.
22 #include "gdk/gdkkeysyms.h"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcellrenderertext.h"
31 #include "gtkcheckmenuitem.h"
32 #include "gtkcombobox.h"
34 #include "gtkeventbox.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechooserutils.h"
40 #include "gtkfilechooser.h"
41 #include "gtkfilesystemmodel.h"
44 #include "gtkhpaned.h"
45 #include "gtkiconfactory.h"
46 #include "gtkicontheme.h"
48 #include "gtkimagemenuitem.h"
51 #include "gtkmarshalers.h"
52 #include "gtkmenuitem.h"
53 #include "gtkmessagedialog.h"
54 #include "gtkpathbar.h"
55 #include "gtkprivate.h"
56 #include "gtkscrolledwindow.h"
57 #include "gtkseparatormenuitem.h"
58 #include "gtksizegroup.h"
61 #include "gtktreednd.h"
62 #include "gtktreeprivate.h"
63 #include "gtktreeview.h"
64 #include "gtktreemodelsort.h"
65 #include "gtktreeselection.h"
66 #include "gtktreestore.h"
67 #include "gtktooltips.h"
68 #include "gtktypebuiltins.h"
71 #if defined (G_OS_UNIX)
72 #include "gtkfilesystemunix.h"
73 #elif defined (G_OS_WIN32)
74 #include "gtkfilesystemwin32.h"
81 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
83 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
84 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
85 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
88 LOAD_EMPTY, /* There is no model */
89 LOAD_PRELOAD, /* Model is loading and a timer is running; model isn't inserted into the tree yet */
90 LOAD_LOADING, /* Timeout expired, model is inserted into the tree, but not fully loaded yet */
91 LOAD_FINISHED /* Model is fully loaded and inserted into the tree */
94 #define MAX_LOADING_TIME 500
96 struct _GtkFileChooserDefaultClass
98 GtkVBoxClass parent_class;
101 struct _GtkFileChooserDefault
103 GtkVBox parent_instance;
105 GtkFileChooserAction action;
107 GtkFileSystem *file_system;
109 /* Save mode widgets */
110 GtkWidget *save_widgets;
112 GtkWidget *save_file_name_entry;
113 GtkWidget *save_folder_label;
114 GtkWidget *save_folder_combo;
115 GtkWidget *save_expander;
117 /* The file browsing widgets */
118 GtkWidget *browse_widgets;
119 GtkWidget *browse_shortcuts_tree_view;
120 GtkWidget *browse_shortcuts_add_button;
121 GtkWidget *browse_shortcuts_remove_button;
122 GtkWidget *browse_files_tree_view;
123 GtkWidget *browse_files_popup_menu;
124 GtkWidget *browse_files_popup_menu_add_shortcut_item;
125 GtkWidget *browse_files_popup_menu_hidden_files_item;
126 GtkWidget *browse_new_folder_button;
127 GtkWidget *browse_path_bar;
129 GtkFileSystemModel *browse_files_model;
131 GtkWidget *filter_combo_hbox;
132 GtkWidget *filter_combo;
133 GtkWidget *preview_box;
134 GtkWidget *preview_label;
135 GtkWidget *preview_widget;
136 GtkWidget *extra_align;
137 GtkWidget *extra_widget;
139 GtkListStore *shortcuts_model;
140 GtkTreeModel *shortcuts_filter_model;
142 GtkTreeModelSort *sort_model;
144 LoadState load_state;
145 guint load_timeout_id;
147 GSList *pending_select_paths;
149 GtkFileFilter *current_filter;
152 GtkTooltips *tooltips;
155 gboolean has_desktop;
161 gulong volumes_changed_id;
162 gulong bookmarks_changed_id;
164 GtkFilePath *current_volume_path;
165 GtkFilePath *current_folder;
166 GtkFilePath *preview_path;
167 char *preview_display_name;
169 GtkTreeViewColumn *list_name_column;
170 GtkCellRenderer *list_name_renderer;
172 GSource *edited_idle;
173 char *edited_new_text;
175 gulong settings_signal_id;
178 gulong toplevel_set_focus_id;
179 GtkWidget *toplevel_last_focus_widget;
182 GdkDragContext *shortcuts_drag_context;
183 GSource *shortcuts_drag_outside_idle;
188 guint local_only : 1;
189 guint preview_widget_active : 1;
190 guint use_preview_label : 1;
191 guint select_multiple : 1;
192 guint show_hidden : 1;
193 guint list_sort_ascending : 1;
194 guint changing_folder : 1;
195 guint shortcuts_current_folder_active : 1;
198 guint shortcuts_drag_outside : 1;
211 static guint signals[LAST_SIGNAL] = { 0 };
213 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
215 SHORTCUTS_COL_PIXBUF,
218 SHORTCUTS_COL_IS_VOLUME,
219 SHORTCUTS_COL_REMOVABLE,
220 SHORTCUTS_COL_PIXBUF_VISIBLE,
221 SHORTCUTS_COL_NUM_COLUMNS
224 /* Column numbers for the file list */
229 FILE_LIST_COL_NUM_COLUMNS
232 /* Identifiers for target types */
238 /* Target types for dragging from the shortcuts list */
239 static GtkTargetEntry shortcuts_source_targets[] = {
240 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
243 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
244 / sizeof (shortcuts_source_targets[0]));
246 /* Target types for dropping into the shortcuts list */
247 static GtkTargetEntry shortcuts_dest_targets[] = {
248 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
249 { "text/uri-list", 0, TEXT_URI_LIST }
252 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
253 / sizeof (shortcuts_dest_targets[0]));
255 /* Target types for DnD from the file list */
256 static GtkTargetEntry file_list_source_targets[] = {
257 { "text/uri-list", 0, TEXT_URI_LIST }
260 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
261 / sizeof (file_list_source_targets[0]));
263 /* Interesting places in the shortcuts bar */
269 SHORTCUTS_BOOKMARKS_SEPARATOR,
271 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
272 SHORTCUTS_CURRENT_FOLDER
275 /* Icon size for if we can't get it from the theme */
276 #define FALLBACK_ICON_SIZE 16
278 #define PREVIEW_HBOX_SPACING 12
282 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
283 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
284 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
285 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
287 static GObject* gtk_file_chooser_default_constructor (GType type,
288 guint n_construct_properties,
289 GObjectConstructParam *construct_params);
290 static void gtk_file_chooser_default_finalize (GObject *object);
291 static void gtk_file_chooser_default_set_property (GObject *object,
295 static void gtk_file_chooser_default_get_property (GObject *object,
299 static void gtk_file_chooser_default_dispose (GObject *object);
300 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
301 static void gtk_file_chooser_default_map (GtkWidget *widget);
302 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
303 GtkWidget *previous_toplevel);
304 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
305 GtkStyle *previous_style);
306 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
307 GdkScreen *previous_screen);
309 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
310 const GtkFilePath *path,
312 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
313 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
315 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
316 const GtkFilePath *path,
318 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
319 const GtkFilePath *path);
320 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
321 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
322 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
323 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
324 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
325 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
326 GtkFileFilter *filter);
327 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
328 GtkFileFilter *filter);
329 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
330 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
331 const GtkFilePath *path,
333 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
334 const GtkFilePath *path,
336 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
338 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
340 gint *default_height);
341 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
342 gboolean *resize_horizontally,
343 gboolean *resize_vertically);
344 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
345 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
347 static void location_popup_handler (GtkFileChooserDefault *impl,
349 static void up_folder_handler (GtkFileChooserDefault *impl);
350 static void down_folder_handler (GtkFileChooserDefault *impl);
351 static void home_folder_handler (GtkFileChooserDefault *impl);
352 static void update_appearance (GtkFileChooserDefault *impl);
354 static void set_current_filter (GtkFileChooserDefault *impl,
355 GtkFileFilter *filter);
356 static void check_preview_change (GtkFileChooserDefault *impl);
358 static void filter_combo_changed (GtkComboBox *combo_box,
359 GtkFileChooserDefault *impl);
360 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
362 GtkTreeViewColumn *column,
363 GtkFileChooserDefault *impl);
365 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
367 GtkFileChooserDefault *impl);
369 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
372 gboolean path_currently_selected,
374 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
376 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
378 static int shortcuts_get_index (GtkFileChooserDefault *impl,
379 ShortcutsIndex where);
380 static int shortcut_find_position (GtkFileChooserDefault *impl,
381 const GtkFilePath *path);
383 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
385 static gboolean list_select_func (GtkTreeSelection *selection,
388 gboolean path_currently_selected,
391 static void list_selection_changed (GtkTreeSelection *tree_selection,
392 GtkFileChooserDefault *impl);
393 static void list_row_activated (GtkTreeView *tree_view,
395 GtkTreeViewColumn *column,
396 GtkFileChooserDefault *impl);
398 static void select_func (GtkFileSystemModel *model,
403 static void path_bar_clicked (GtkPathBar *path_bar,
404 GtkFilePath *file_path,
405 gboolean child_is_hidden,
406 GtkFileChooserDefault *impl);
408 static void add_bookmark_button_clicked_cb (GtkButton *button,
409 GtkFileChooserDefault *impl);
410 static void remove_bookmark_button_clicked_cb (GtkButton *button,
411 GtkFileChooserDefault *impl);
413 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
414 GtkCellRenderer *cell,
415 GtkTreeModel *tree_model,
418 static void list_name_data_func (GtkTreeViewColumn *tree_column,
419 GtkCellRenderer *cell,
420 GtkTreeModel *tree_model,
424 static void list_size_data_func (GtkTreeViewColumn *tree_column,
425 GtkCellRenderer *cell,
426 GtkTreeModel *tree_model,
430 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
431 GtkCellRenderer *cell,
432 GtkTreeModel *tree_model,
436 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
439 static void load_remove_timer (GtkFileChooserDefault *impl);
441 static GObjectClass *parent_class;
445 /* Drag and drop interface declarations */
448 GtkTreeModelFilter parent;
450 GtkFileChooserDefault *impl;
451 } ShortcutsModelFilter;
454 GtkTreeModelFilterClass parent_class;
455 } ShortcutsModelFilterClass;
457 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
458 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
460 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
462 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
463 _shortcuts_model_filter,
464 GTK_TYPE_TREE_MODEL_FILTER,
465 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
466 shortcuts_model_filter_drag_source_iface_init));
468 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
469 GtkTreeModel *child_model,
475 _gtk_file_chooser_default_get_type (void)
477 static GType file_chooser_default_type = 0;
479 if (!file_chooser_default_type)
481 static const GTypeInfo file_chooser_default_info =
483 sizeof (GtkFileChooserDefaultClass),
484 NULL, /* base_init */
485 NULL, /* base_finalize */
486 (GClassInitFunc) gtk_file_chooser_default_class_init,
487 NULL, /* class_finalize */
488 NULL, /* class_data */
489 sizeof (GtkFileChooserDefault),
491 (GInstanceInitFunc) gtk_file_chooser_default_init,
494 static const GInterfaceInfo file_chooser_info =
496 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
497 NULL, /* interface_finalize */
498 NULL /* interface_data */
501 static const GInterfaceInfo file_chooser_embed_info =
503 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
504 NULL, /* interface_finalize */
505 NULL /* interface_data */
508 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
509 &file_chooser_default_info, 0);
511 g_type_add_interface_static (file_chooser_default_type,
512 GTK_TYPE_FILE_CHOOSER,
514 g_type_add_interface_static (file_chooser_default_type,
515 GTK_TYPE_FILE_CHOOSER_EMBED,
516 &file_chooser_embed_info);
519 return file_chooser_default_type;
523 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
525 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
526 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
527 GtkBindingSet *binding_set;
529 parent_class = g_type_class_peek_parent (class);
531 gobject_class->finalize = gtk_file_chooser_default_finalize;
532 gobject_class->constructor = gtk_file_chooser_default_constructor;
533 gobject_class->set_property = gtk_file_chooser_default_set_property;
534 gobject_class->get_property = gtk_file_chooser_default_get_property;
535 gobject_class->dispose = gtk_file_chooser_default_dispose;
537 widget_class->show_all = gtk_file_chooser_default_show_all;
538 widget_class->map = gtk_file_chooser_default_map;
539 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
540 widget_class->style_set = gtk_file_chooser_default_style_set;
541 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
543 signals[LOCATION_POPUP] =
544 _gtk_binding_signal_new ("location-popup",
545 G_OBJECT_CLASS_TYPE (class),
546 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
547 G_CALLBACK (location_popup_handler),
549 _gtk_marshal_VOID__STRING,
550 G_TYPE_NONE, 1, G_TYPE_STRING);
552 _gtk_binding_signal_new ("up-folder",
553 G_OBJECT_CLASS_TYPE (class),
554 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
555 G_CALLBACK (up_folder_handler),
557 _gtk_marshal_VOID__VOID,
559 signals[DOWN_FOLDER] =
560 _gtk_binding_signal_new ("down-folder",
561 G_OBJECT_CLASS_TYPE (class),
562 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
563 G_CALLBACK (down_folder_handler),
565 _gtk_marshal_VOID__VOID,
567 signals[HOME_FOLDER] =
568 _gtk_binding_signal_new ("home-folder",
569 G_OBJECT_CLASS_TYPE (class),
570 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
571 G_CALLBACK (home_folder_handler),
573 _gtk_marshal_VOID__VOID,
576 binding_set = gtk_binding_set_by_class (class);
578 gtk_binding_entry_add_signal (binding_set,
579 GDK_l, GDK_CONTROL_MASK,
581 1, G_TYPE_STRING, "");
583 gtk_binding_entry_add_signal (binding_set,
586 1, G_TYPE_STRING, "/");
588 gtk_binding_entry_add_signal (binding_set,
589 GDK_Up, GDK_MOD1_MASK,
592 gtk_binding_entry_add_signal (binding_set,
596 gtk_binding_entry_add_signal (binding_set,
597 GDK_KP_Up, GDK_MOD1_MASK,
601 gtk_binding_entry_add_signal (binding_set,
602 GDK_Down, GDK_MOD1_MASK,
605 gtk_binding_entry_add_signal (binding_set,
606 GDK_KP_Down, GDK_MOD1_MASK,
610 gtk_binding_entry_add_signal (binding_set,
611 GDK_Home, GDK_MOD1_MASK,
614 gtk_binding_entry_add_signal (binding_set,
615 GDK_KP_Home, GDK_MOD1_MASK,
619 _gtk_file_chooser_install_properties (gobject_class);
621 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
622 P_("Default file chooser backend"),
623 P_("Name of the GtkFileChooser backend to use by default"),
629 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
631 iface->select_path = gtk_file_chooser_default_select_path;
632 iface->unselect_path = gtk_file_chooser_default_unselect_path;
633 iface->select_all = gtk_file_chooser_default_select_all;
634 iface->unselect_all = gtk_file_chooser_default_unselect_all;
635 iface->get_paths = gtk_file_chooser_default_get_paths;
636 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
637 iface->get_file_system = gtk_file_chooser_default_get_file_system;
638 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
639 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
640 iface->set_current_name = gtk_file_chooser_default_set_current_name;
641 iface->add_filter = gtk_file_chooser_default_add_filter;
642 iface->remove_filter = gtk_file_chooser_default_remove_filter;
643 iface->list_filters = gtk_file_chooser_default_list_filters;
644 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
645 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
646 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
650 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
652 iface->get_default_size = gtk_file_chooser_default_get_default_size;
653 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
654 iface->should_respond = gtk_file_chooser_default_should_respond;
655 iface->initial_focus = gtk_file_chooser_default_initial_focus;
658 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
660 impl->local_only = TRUE;
661 impl->preview_widget_active = TRUE;
662 impl->use_preview_label = TRUE;
663 impl->select_multiple = FALSE;
664 impl->show_hidden = FALSE;
665 impl->icon_size = FALLBACK_ICON_SIZE;
666 impl->load_state = LOAD_EMPTY;
667 impl->pending_select_paths = NULL;
669 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (impl), TRUE);
670 gtk_box_set_spacing (GTK_BOX (impl), 12);
672 impl->tooltips = gtk_tooltips_new ();
673 g_object_ref (impl->tooltips);
674 gtk_object_sink (GTK_OBJECT (impl->tooltips));
677 /* Frees the data columns for the specified iter in the shortcuts model*/
679 shortcuts_free_row_data (GtkFileChooserDefault *impl,
685 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
686 SHORTCUTS_COL_DATA, &col_data,
687 SHORTCUTS_COL_IS_VOLUME, &is_volume,
694 GtkFileSystemVolume *volume;
697 gtk_file_system_volume_free (impl->file_system, volume);
704 gtk_file_path_free (path);
708 /* Frees all the data columns in the shortcuts model */
710 shortcuts_free (GtkFileChooserDefault *impl)
714 if (!impl->shortcuts_model)
717 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
720 shortcuts_free_row_data (impl, &iter);
722 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
724 g_object_unref (impl->shortcuts_model);
725 impl->shortcuts_model = NULL;
729 pending_select_paths_free (GtkFileChooserDefault *impl)
733 for (l = impl->pending_select_paths; l; l = l->next)
738 gtk_file_path_free (path);
741 g_slist_free (impl->pending_select_paths);
742 impl->pending_select_paths = NULL;
746 pending_select_paths_add (GtkFileChooserDefault *impl,
747 const GtkFilePath *path)
749 impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
752 /* Used from gtk_tree_selection_selected_foreach() */
754 store_selection_foreach (GtkTreeModel *model,
759 GtkFileChooserDefault *impl;
760 GtkTreeIter child_iter;
761 const GtkFilePath *file_path;
763 impl = GTK_FILE_CHOOSER_DEFAULT (data);
765 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
767 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
768 pending_select_paths_add (impl, file_path);
771 /* Stores the current selection in the list of paths to select; this is used to
772 * preserve the selection when reloading the current folder.
775 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
777 GtkTreeSelection *selection;
779 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
780 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
784 gtk_file_chooser_default_finalize (GObject *object)
786 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
789 if (impl->shortcuts_filter_model)
790 g_object_unref (impl->shortcuts_filter_model);
792 shortcuts_free (impl);
794 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
795 impl->volumes_changed_id = 0;
796 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
797 impl->bookmarks_changed_id = 0;
798 g_object_unref (impl->file_system);
800 for (l = impl->filters; l; l = l->next)
802 GtkFileFilter *filter;
804 filter = GTK_FILE_FILTER (l->data);
805 g_object_unref (filter);
807 g_slist_free (impl->filters);
809 if (impl->current_filter)
810 g_object_unref (impl->current_filter);
812 if (impl->current_volume_path)
813 gtk_file_path_free (impl->current_volume_path);
815 if (impl->current_folder)
816 gtk_file_path_free (impl->current_folder);
818 if (impl->preview_path)
819 gtk_file_path_free (impl->preview_path);
821 pending_select_paths_free (impl);
823 load_remove_timer (impl);
825 /* Free all the Models we have */
826 if (impl->browse_files_model)
827 g_object_unref (impl->browse_files_model);
829 if (impl->sort_model)
830 g_object_unref (impl->sort_model);
832 g_free (impl->preview_display_name);
834 g_free (impl->edited_new_text);
836 g_object_unref (impl->tooltips);
838 G_OBJECT_CLASS (parent_class)->finalize (object);
841 /* Shows an error dialog set as transient for the specified window */
843 error_message_with_parent (GtkWindow *parent,
849 dialog = gtk_message_dialog_new (parent,
850 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
855 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
857 gtk_dialog_run (GTK_DIALOG (dialog));
858 gtk_widget_destroy (dialog);
861 /* Returns a toplevel GtkWindow, or NULL if none */
863 get_toplevel (GtkWidget *widget)
867 toplevel = gtk_widget_get_toplevel (widget);
868 if (!GTK_WIDGET_TOPLEVEL (toplevel))
871 return GTK_WINDOW (toplevel);
874 /* Shows an error dialog for the file chooser */
876 error_message (GtkFileChooserDefault *impl,
880 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
883 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
885 error_dialog (GtkFileChooserDefault *impl,
887 const GtkFilePath *path,
896 uri = gtk_file_system_path_to_uri (impl->file_system, path);
897 text = g_strdup_printf (msg, uri);
898 error_message (impl, text, error->message);
901 g_error_free (error);
905 /* Displays an error message about not being able to get information for a file.
906 * Frees the GError as well.
909 error_getting_info_dialog (GtkFileChooserDefault *impl,
910 const GtkFilePath *path,
914 _("Could not retrieve information about the file"),
918 /* Shows an error dialog about not being able to add a bookmark */
920 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
921 const GtkFilePath *path,
925 _("Could not add a bookmark"),
929 /* Shows an error dialog about not being able to remove a bookmark */
931 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
932 const GtkFilePath *path,
936 _("Could not remove bookmark"),
940 /* Shows an error dialog about not being able to create a folder */
942 error_creating_folder_dialog (GtkFileChooserDefault *impl,
943 const GtkFilePath *path,
947 _("The folder could not be created"),
951 /* Shows an error dialog about not being able to create a filename */
953 error_building_filename_dialog (GtkFileChooserDefault *impl,
954 const GtkFilePath *folder_part,
955 const char *file_part,
958 error_dialog (impl, _("Invalid file name"),
962 /* Shows an error dialog when we cannot switch to a folder */
964 error_changing_folder_dialog (GtkFileChooserDefault *impl,
965 const GtkFilePath *path,
968 error_dialog (impl, _("The folder contents could not be displayed"),
972 /* Changes folders, displaying an error dialog if this fails */
974 change_folder_and_display_error (GtkFileChooserDefault *impl,
975 const GtkFilePath *path)
979 GtkFilePath *path_copy;
981 /* We copy the path because of this case:
983 * list_row_activated()
984 * fetches path from model; path belongs to the model (*)
985 * calls change_folder_and_display_error()
986 * calls _gtk_file_chooser_set_current_folder_path()
987 * changing folders fails, sets model to NULL, thus freeing the path in (*)
990 path_copy = gtk_file_path_copy (path);
993 result = _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path_copy, &error);
996 error_changing_folder_dialog (impl, path_copy, error);
998 gtk_file_path_free (path_copy);
1004 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1006 if (impl->use_preview_label)
1008 if (!impl->preview_label)
1010 impl->preview_label = gtk_label_new (impl->preview_display_name);
1011 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1012 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1013 gtk_widget_show (impl->preview_label);
1018 if (impl->preview_label)
1020 gtk_widget_destroy (impl->preview_label);
1021 impl->preview_label = NULL;
1025 if (impl->preview_widget_active && impl->preview_widget)
1026 gtk_widget_show (impl->preview_box);
1028 gtk_widget_hide (impl->preview_box);
1030 g_signal_emit_by_name (impl, "default-size-changed");
1034 set_preview_widget (GtkFileChooserDefault *impl,
1035 GtkWidget *preview_widget)
1037 if (preview_widget == impl->preview_widget)
1040 if (impl->preview_widget)
1041 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1042 impl->preview_widget);
1044 impl->preview_widget = preview_widget;
1045 if (impl->preview_widget)
1047 gtk_widget_show (impl->preview_widget);
1048 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1049 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1050 impl->preview_widget,
1051 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1054 update_preview_widget_visibility (impl);
1057 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1059 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1063 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1069 gboolean pixbuf_visible;
1072 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1073 SHORTCUTS_COL_DATA, &data,
1074 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1075 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1078 if (pixbuf_visible && data)
1082 GtkFileSystemVolume *volume;
1085 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1086 impl->icon_size, NULL);
1090 const GtkFilePath *path;
1093 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1094 impl->icon_size, NULL);
1097 gtk_list_store_set (impl->shortcuts_model, &iter,
1098 SHORTCUTS_COL_PIXBUF, pixbuf,
1101 g_object_unref (pixbuf);
1103 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1107 shortcuts_find_folder (GtkFileChooserDefault *impl,
1108 GtkFilePath *folder)
1110 GtkTreeSelection *selection;
1114 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1116 g_assert (folder != NULL);
1117 pos = shortcut_find_position (impl, folder);
1120 gtk_tree_selection_unselect_all (selection);
1124 path = gtk_tree_path_new_from_indices (pos, -1);
1125 gtk_tree_selection_select_path (selection, path);
1126 gtk_tree_path_free (path);
1129 /* If a shortcut corresponds to the current folder, selects it */
1131 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1133 shortcuts_find_folder (impl, impl->current_folder);
1136 /* Convenience function to get the display name and icon info for a path */
1137 static GtkFileInfo *
1138 get_file_info (GtkFileSystem *file_system,
1139 const GtkFilePath *path,
1143 GtkFilePath *parent_path;
1144 GtkFileFolder *parent_folder;
1151 if (!gtk_file_system_get_parent (file_system, path, &parent_path, &tmp))
1154 parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
1155 GTK_FILE_INFO_DISPLAY_NAME
1156 | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
1161 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, &tmp);
1162 g_object_unref (parent_folder);
1166 gtk_file_path_free (parent_path);
1171 GTK_FILE_CHOOSER_ERROR,
1172 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
1173 _("Could not get information about '%s': %s"),
1174 gtk_file_path_get_string (path),
1182 /* Returns whether a path is a folder */
1184 check_is_folder (GtkFileSystem *file_system,
1185 const GtkFilePath *path,
1188 GtkFileFolder *folder;
1190 folder = gtk_file_system_get_folder (file_system, path, 0, error);
1194 g_object_unref (folder);
1198 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1199 * inserts a volume. A position of -1 indicates the end of the tree.
1202 shortcuts_insert_path (GtkFileChooserDefault *impl,
1205 GtkFileSystemVolume *volume,
1206 const GtkFilePath *path,
1219 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1220 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1221 impl->icon_size, NULL);
1225 if (!check_is_folder (impl->file_system, path, error))
1229 label_copy = g_strdup (label);
1232 GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
1237 label_copy = g_strdup (gtk_file_info_get_display_name (info));
1238 gtk_file_info_free (info);
1241 data = gtk_file_path_copy (path);
1242 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1243 impl->icon_size, NULL);
1247 gtk_list_store_append (impl->shortcuts_model, &iter);
1249 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1251 gtk_list_store_set (impl->shortcuts_model, &iter,
1252 SHORTCUTS_COL_PIXBUF, pixbuf,
1253 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1254 SHORTCUTS_COL_NAME, label_copy,
1255 SHORTCUTS_COL_DATA, data,
1256 SHORTCUTS_COL_IS_VOLUME, is_volume,
1257 SHORTCUTS_COL_REMOVABLE, removable,
1260 g_free (label_copy);
1263 g_object_unref (pixbuf);
1268 /* Appends an item for the user's home directory to the shortcuts model */
1270 shortcuts_append_home (GtkFileChooserDefault *impl)
1273 GtkFilePath *home_path;
1276 home = g_get_home_dir ();
1280 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1283 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1284 if (!impl->has_home)
1285 error_getting_info_dialog (impl, home_path, error);
1287 gtk_file_path_free (home_path);
1290 /* Appends the ~/Desktop directory to the shortcuts model */
1292 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1298 name = _gtk_file_system_win32_get_desktop ();
1300 const char *home = g_get_home_dir ();
1304 name = g_build_filename (home, "Desktop", NULL);
1307 path = gtk_file_system_filename_to_path (impl->file_system, name);
1310 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1311 /* We do not actually pop up an error dialog if there is no desktop directory
1312 * because some people may really not want to have one.
1315 gtk_file_path_free (path);
1318 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1320 shortcuts_append_paths (GtkFileChooserDefault *impl,
1326 /* As there is no separator now, we want to start there.
1328 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1331 for (; paths; paths = paths->next)
1339 if (impl->local_only &&
1340 !gtk_file_system_path_is_local (impl->file_system, path))
1343 /* NULL GError, but we don't really want to show error boxes here */
1344 if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, NULL, TRUE, NULL))
1348 return num_inserted;
1351 /* Returns the index for the corresponding item in the shortcuts bar */
1353 shortcuts_get_index (GtkFileChooserDefault *impl,
1354 ShortcutsIndex where)
1360 if (where == SHORTCUTS_HOME)
1363 n += impl->has_home ? 1 : 0;
1365 if (where == SHORTCUTS_DESKTOP)
1368 n += impl->has_desktop ? 1 : 0;
1370 if (where == SHORTCUTS_VOLUMES)
1373 n += impl->num_volumes;
1375 if (where == SHORTCUTS_SHORTCUTS)
1378 n += impl->num_shortcuts;
1380 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1383 /* If there are no bookmarks there won't be a separator */
1384 n += (impl->num_bookmarks > 0) ? 1 : 0;
1386 if (where == SHORTCUTS_BOOKMARKS)
1389 n += impl->num_bookmarks;
1391 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1396 if (where == SHORTCUTS_CURRENT_FOLDER)
1399 g_assert_not_reached ();
1406 /* Removes the specified number of rows from the shortcuts list */
1408 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1414 path = gtk_tree_path_new_from_indices (start_row, -1);
1416 for (; n_rows; n_rows--)
1420 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1421 g_assert_not_reached ();
1423 shortcuts_free_row_data (impl, &iter);
1424 gtk_list_store_remove (impl->shortcuts_model, &iter);
1427 gtk_tree_path_free (path);
1430 /* Adds all the file system volumes to the shortcuts model */
1432 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1437 gboolean old_changing_folders;
1439 old_changing_folders = impl->changing_folder;
1440 impl->changing_folder = TRUE;
1442 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1443 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1444 impl->num_volumes = 0;
1446 list = gtk_file_system_list_volumes (impl->file_system);
1450 for (l = list; l; l = l->next)
1452 GtkFileSystemVolume *volume;
1456 if (impl->local_only)
1458 GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1459 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1460 gtk_file_path_free (base_path);
1464 gtk_file_system_volume_free (impl->file_system, volume);
1469 if (shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL))
1472 gtk_file_system_volume_free (impl->file_system, volume);
1475 impl->num_volumes = n;
1476 g_slist_free (list);
1478 if (impl->shortcuts_filter_model)
1479 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1481 impl->changing_folder = old_changing_folders;
1484 /* Inserts a separator node in the shortcuts list */
1486 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1487 ShortcutsIndex where)
1491 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1493 gtk_list_store_insert (impl->shortcuts_model, &iter,
1494 shortcuts_get_index (impl, where));
1495 gtk_list_store_set (impl->shortcuts_model, &iter,
1496 SHORTCUTS_COL_PIXBUF, NULL,
1497 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1498 SHORTCUTS_COL_NAME, NULL,
1499 SHORTCUTS_COL_DATA, NULL,
1503 /* Updates the list of bookmarks */
1505 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1508 gboolean old_changing_folders;
1510 GtkFilePath *list_selected = NULL;
1511 GtkFilePath *combo_selected = NULL;
1515 old_changing_folders = impl->changing_folder;
1516 impl->changing_folder = TRUE;
1518 if (shortcuts_get_selected (impl, &iter))
1520 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1522 SHORTCUTS_COL_DATA, &col_data,
1523 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1526 if (col_data && !is_volume)
1527 list_selected = gtk_file_path_copy (col_data);
1530 if (impl->save_folder_combo &&
1531 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
1534 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1536 SHORTCUTS_COL_DATA, &col_data,
1537 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1540 if (col_data && !is_volume)
1541 combo_selected = gtk_file_path_copy (col_data);
1544 if (impl->num_bookmarks > 0)
1545 shortcuts_remove_rows (impl,
1546 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1547 impl->num_bookmarks + 1);
1549 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1550 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1551 gtk_file_paths_free (bookmarks);
1553 if (impl->num_bookmarks > 0)
1554 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1556 if (impl->shortcuts_filter_model)
1557 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1561 shortcuts_find_folder (impl, list_selected);
1562 gtk_file_path_free (list_selected);
1569 pos = shortcut_find_position (impl, combo_selected);
1571 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
1573 gtk_file_path_free (combo_selected);
1576 impl->changing_folder = old_changing_folders;
1579 /* Appends a separator and a row to the shortcuts list for the current folder */
1581 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1586 g_assert (!impl->shortcuts_current_folder_active);
1590 g_assert (impl->current_folder != NULL);
1592 pos = shortcut_find_position (impl, impl->current_folder);
1595 GtkFileSystemVolume *volume;
1596 GtkFilePath *base_path;
1600 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1604 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1606 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1608 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1613 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1615 success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1620 success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1623 gtk_file_system_volume_free (impl->file_system, volume);
1626 gtk_file_path_free (base_path);
1629 shortcuts_remove_rows (impl, pos - 1, 1); /* remove the separator */
1631 impl->shortcuts_current_folder_active = success;
1635 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1638 /* Updates the current folder row in the shortcuts model */
1640 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1644 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1646 if (impl->shortcuts_current_folder_active)
1648 shortcuts_remove_rows (impl, pos, 2);
1649 impl->shortcuts_current_folder_active = FALSE;
1652 shortcuts_add_current_folder (impl);
1655 /* Filter function used for the shortcuts filter model */
1657 shortcuts_filter_cb (GtkTreeModel *model,
1661 GtkFileChooserDefault *impl;
1665 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1667 path = gtk_tree_model_get_path (model, iter);
1671 pos = *gtk_tree_path_get_indices (path);
1672 gtk_tree_path_free (path);
1674 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1677 /* Creates the list model for shortcuts */
1679 shortcuts_model_create (GtkFileChooserDefault *impl)
1681 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1682 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1683 GDK_TYPE_PIXBUF, /* pixbuf */
1684 G_TYPE_STRING, /* name */
1685 G_TYPE_POINTER, /* path or volume */
1686 G_TYPE_BOOLEAN, /* is the previous column a volume? */
1687 G_TYPE_BOOLEAN, /* removable */
1688 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1690 if (impl->file_system)
1692 shortcuts_append_home (impl);
1693 shortcuts_append_desktop (impl);
1694 shortcuts_add_volumes (impl);
1695 shortcuts_add_bookmarks (impl);
1698 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1699 GTK_TREE_MODEL (impl->shortcuts_model),
1702 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1703 shortcuts_filter_cb,
1708 /* Callback used when the "New Folder" button is clicked */
1710 new_folder_button_clicked (GtkButton *button,
1711 GtkFileChooserDefault *impl)
1716 if (!impl->browse_files_model)
1717 return; /* FIXME: this sucks. Disable the New Folder button or something. */
1719 /* Prevent button from being clicked twice */
1720 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
1722 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1724 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1725 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1726 path, impl->list_name_column,
1729 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1730 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1732 impl->list_name_column,
1735 gtk_tree_path_free (path);
1738 /* Idle handler for creating a new folder after editing its name cell, or for
1739 * canceling the editing.
1742 edited_idle_cb (GtkFileChooserDefault *impl)
1744 GDK_THREADS_ENTER ();
1746 g_source_destroy (impl->edited_idle);
1747 impl->edited_idle = NULL;
1749 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1750 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1752 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
1754 if (impl->edited_new_text) /* not cancelled? */
1757 GtkFilePath *file_path;
1760 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, impl->edited_new_text,
1765 if (gtk_file_system_create_folder (impl->file_system, file_path, &error))
1766 change_folder_and_display_error (impl, file_path);
1768 error_creating_folder_dialog (impl, file_path, error);
1770 gtk_file_path_free (file_path);
1773 error_creating_folder_dialog (impl, file_path, error);
1775 g_free (impl->edited_new_text);
1776 impl->edited_new_text = NULL;
1779 GDK_THREADS_LEAVE ();
1785 queue_edited_idle (GtkFileChooserDefault *impl,
1786 const gchar *new_text)
1788 /* We create the folder in an idle handler so that we don't modify the tree
1792 g_assert (!impl->edited_idle);
1793 g_assert (!impl->edited_new_text);
1795 impl->edited_idle = g_idle_source_new ();
1796 g_source_set_closure (impl->edited_idle,
1797 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
1799 g_source_attach (impl->edited_idle, NULL);
1802 impl->edited_new_text = g_strdup (new_text);
1805 /* Callback used from the text cell renderer when the new folder is named */
1807 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1809 const gchar *new_text,
1810 GtkFileChooserDefault *impl)
1812 /* work around bug #154921 */
1813 g_object_set (cell_renderer_text,
1814 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1815 queue_edited_idle (impl, new_text);
1818 /* Callback used from the text cell renderer when the new folder edition gets
1822 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1823 GtkFileChooserDefault *impl)
1825 /* work around bug #154921 */
1826 g_object_set (cell_renderer_text,
1827 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1828 queue_edited_idle (impl, NULL);
1831 /* Creates the widgets for the filter combo box */
1833 filter_create (GtkFileChooserDefault *impl)
1835 impl->filter_combo = gtk_combo_box_new_text ();
1836 g_signal_connect (impl->filter_combo, "changed",
1837 G_CALLBACK (filter_combo_changed), impl);
1839 return impl->filter_combo;
1843 button_new (GtkFileChooserDefault *impl,
1845 const char *stock_id,
1855 button = gtk_button_new ();
1856 hbox = gtk_hbox_new (FALSE, 2);
1857 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1859 gtk_container_add (GTK_CONTAINER (button), align);
1860 gtk_container_add (GTK_CONTAINER (align), hbox);
1861 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1863 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1865 widget = gtk_label_new_with_mnemonic (text);
1866 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1867 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1869 gtk_widget_set_sensitive (button, sensitive);
1870 g_signal_connect (button, "clicked", callback, impl);
1872 gtk_widget_show_all (align);
1875 gtk_widget_show (button);
1880 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1882 shortcut_find_position (GtkFileChooserDefault *impl,
1883 const GtkFilePath *path)
1887 int current_folder_separator_idx;
1889 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1892 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1894 for (i = 0; i < current_folder_separator_idx; i++)
1899 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1900 SHORTCUTS_COL_DATA, &col_data,
1901 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1908 GtkFileSystemVolume *volume;
1909 GtkFilePath *base_path;
1913 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1915 exists = strcmp (gtk_file_path_get_string (path),
1916 gtk_file_path_get_string (base_path)) == 0;
1924 GtkFilePath *model_path;
1926 model_path = col_data;
1928 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1933 gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1939 /* Tries to add a bookmark from a path name */
1941 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1942 const GtkFilePath *path,
1947 if (shortcut_find_position (impl, path) != -1)
1950 /* FIXME: this check really belongs in gtk_file_system_insert_bookmark. */
1952 if (!check_is_folder (impl->file_system, path, &error))
1954 error_adding_bookmark_dialog (impl, path, error);
1959 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
1961 error_adding_bookmark_dialog (impl, path, error);
1969 add_bookmark_foreach_cb (GtkTreeModel *model,
1974 GtkFileChooserDefault *impl;
1975 GtkFileSystemModel *fs_model;
1976 GtkTreeIter child_iter;
1977 const GtkFilePath *file_path;
1979 impl = (GtkFileChooserDefault *) data;
1981 fs_model = impl->browse_files_model;
1982 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1984 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
1985 shortcuts_add_bookmark_from_path (impl, file_path, -1);
1988 /* Adds a bookmark from the currently selected item in the file list */
1990 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
1992 GtkTreeSelection *selection;
1994 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1996 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1997 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
1999 gtk_tree_selection_selected_foreach (selection,
2000 add_bookmark_foreach_cb,
2004 /* Callback used when the "Add bookmark" button is clicked */
2006 add_bookmark_button_clicked_cb (GtkButton *button,
2007 GtkFileChooserDefault *impl)
2009 bookmarks_add_selected_folder (impl);
2012 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2013 * returns FALSE if no shortcut is selected.
2016 shortcuts_get_selected (GtkFileChooserDefault *impl,
2019 GtkTreeSelection *selection;
2020 GtkTreeIter parent_iter;
2022 if (!impl->browse_shortcuts_tree_view)
2025 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2027 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2030 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2036 /* Removes the selected bookmarks */
2038 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2047 if (!shortcuts_get_selected (impl, &iter))
2050 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2051 SHORTCUTS_COL_DATA, &col_data,
2052 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2053 SHORTCUTS_COL_REMOVABLE, &removable,
2055 g_assert (col_data != NULL);
2056 g_assert (!is_volume);
2064 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2065 error_removing_bookmark_dialog (impl, path, error);
2068 /* Callback used when the "Remove bookmark" button is clicked */
2070 remove_bookmark_button_clicked_cb (GtkButton *button,
2071 GtkFileChooserDefault *impl)
2073 remove_selected_bookmarks (impl);
2076 struct selection_check_closure {
2077 GtkFileChooserDefault *impl;
2080 gboolean all_folders;
2083 /* Used from gtk_tree_selection_selected_foreach() */
2085 selection_check_foreach_cb (GtkTreeModel *model,
2090 struct selection_check_closure *closure;
2091 GtkTreeIter child_iter;
2092 const GtkFileInfo *info;
2096 closure->num_selected++;
2098 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2100 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2101 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2103 closure->all_folders = closure->all_folders && is_folder;
2104 closure->all_files = closure->all_files && !is_folder;
2107 /* Checks whether the selected items in the file list are all files or all folders */
2109 selection_check (GtkFileChooserDefault *impl,
2111 gboolean *all_files,
2112 gboolean *all_folders)
2114 struct selection_check_closure closure;
2115 GtkTreeSelection *selection;
2117 closure.impl = impl;
2118 closure.num_selected = 0;
2119 closure.all_files = TRUE;
2120 closure.all_folders = TRUE;
2122 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2123 gtk_tree_selection_selected_foreach (selection,
2124 selection_check_foreach_cb,
2127 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2130 *num_selected = closure.num_selected;
2133 *all_files = closure.all_files;
2136 *all_folders = closure.all_folders;
2139 struct get_selected_path_closure {
2140 GtkFileChooserDefault *impl;
2141 const GtkFilePath *path;
2145 get_selected_path_foreach_cb (GtkTreeModel *model,
2150 struct get_selected_path_closure *closure;
2151 GtkTreeIter child_iter;
2155 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2156 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2159 /* Returns a selected path from the file list */
2160 static const GtkFilePath *
2161 get_selected_path (GtkFileChooserDefault *impl)
2163 struct get_selected_path_closure closure;
2164 GtkTreeSelection *selection;
2166 closure.impl = impl;
2167 closure.path = NULL;
2169 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2170 gtk_tree_selection_selected_foreach (selection,
2171 get_selected_path_foreach_cb,
2174 return closure.path;
2178 GtkFileChooserDefault *impl;
2180 } UpdateTooltipData;
2183 update_tooltip (GtkTreeModel *model,
2188 UpdateTooltipData *udata = data;
2189 GtkTreeIter child_iter;
2190 const GtkFileInfo *info;
2192 if (udata->tip == NULL)
2194 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2198 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2199 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2200 gtk_file_info_get_display_name (info));
2205 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2206 * if there are no selected items *and* the current folder is not in the
2207 * bookmarks list. De-sensitize the button otherwise.
2210 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2213 gboolean all_folders;
2217 selection_check (impl, &num_selected, NULL, &all_folders);
2219 if (num_selected == 0)
2220 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2221 else if (num_selected == 1)
2223 const GtkFilePath *path;
2225 path = get_selected_path (impl);
2226 active = all_folders && (shortcut_find_position (impl, path) == -1);
2229 active = all_folders;
2231 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2233 if (impl->browse_files_popup_menu_add_shortcut_item)
2234 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2235 (num_selected == 0) ? FALSE : active);
2239 if (num_selected == 0)
2240 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2241 else if (num_selected > 1)
2242 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2245 GtkTreeSelection *selection;
2246 UpdateTooltipData data;
2248 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2251 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2255 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2260 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2261 * bookmark row is selected in the shortcuts tree.
2264 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2267 gboolean removable = FALSE;
2270 if (shortcuts_get_selected (impl, &iter))
2271 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2272 SHORTCUTS_COL_REMOVABLE, &removable,
2273 SHORTCUTS_COL_NAME, &name,
2276 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2282 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2283 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2291 /* GtkWidget::drag-begin handler for the shortcuts list. */
2293 shortcuts_drag_begin_cb (GtkWidget *widget,
2294 GdkDragContext *context,
2295 GtkFileChooserDefault *impl)
2298 impl->shortcuts_drag_context = g_object_ref (context);
2303 /* Removes the idle handler for outside drags */
2305 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2307 if (!impl->shortcuts_drag_outside_idle)
2310 g_source_destroy (impl->shortcuts_drag_outside_idle);
2311 impl->shortcuts_drag_outside_idle = NULL;
2315 /* GtkWidget::drag-end handler for the shortcuts list. */
2317 shortcuts_drag_end_cb (GtkWidget *widget,
2318 GdkDragContext *context,
2319 GtkFileChooserDefault *impl)
2322 g_object_unref (impl->shortcuts_drag_context);
2324 shortcuts_cancel_drag_outside_idle (impl);
2326 if (!impl->shortcuts_drag_outside)
2329 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2331 impl->shortcuts_drag_outside = FALSE;
2335 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2337 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2338 GdkDragContext *context,
2339 GtkFileChooserDefault *impl)
2341 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2345 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2349 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2352 GtkTreeView *tree_view;
2355 GdkPixmap *row_pixmap;
2360 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2362 /* Find the selected path and get its drag pixmap */
2364 if (!shortcuts_get_selected (impl, &iter))
2365 g_assert_not_reached ();
2367 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2369 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2370 gtk_tree_path_free (path);
2379 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2385 GdkPixmap *composite;
2386 int row_pixmap_width, row_pixmap_height;
2387 int pixbuf_width, pixbuf_height;
2388 int composite_width, composite_height;
2389 int pixbuf_x, pixbuf_y;
2390 GdkGC *gc, *mask_gc;
2392 GdkBitmap *pixbuf_mask;
2394 /* Create pixmap and mask for composite image */
2396 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2397 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2398 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2400 composite_width = MAX (row_pixmap_width, pixbuf_width);
2401 composite_height = MAX (row_pixmap_height, pixbuf_height);
2403 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2405 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2408 pixbuf_x = composite_width - pixbuf_width;
2410 pixbuf_y = (composite_height - pixbuf_height) / 2;
2412 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2413 gc = gdk_gc_new (composite);
2415 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2416 mask_gc = gdk_gc_new (mask);
2418 gdk_gc_set_foreground (mask_gc, &color);
2419 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2422 color.green = 0xffff;
2423 color.blue = 0xffff;
2424 gdk_gc_set_rgb_fg_color (gc, &color);
2425 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2427 /* Composite the row pixmap and the pixbuf */
2429 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2431 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2432 NULL, &pixbuf_mask, 128);
2433 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2436 pixbuf_width, pixbuf_height);
2437 g_object_unref (pixbuf_mask);
2439 gdk_draw_drawable (composite, gc, row_pixmap,
2442 row_pixmap_width, row_pixmap_height);
2444 gdk_gc_set_foreground (mask_gc, &color);
2445 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2447 gdk_draw_pixbuf (composite, gc, pixbuf,
2450 pixbuf_width, pixbuf_height,
2454 g_object_unref (pixbuf);
2455 g_object_unref (row_pixmap);
2457 row_pixmap = composite;
2461 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2463 gtk_tree_view_get_path_at_pos (tree_view,
2464 tree_view->priv->press_start_x,
2465 tree_view->priv->press_start_y,
2471 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2472 gdk_drawable_get_colormap (row_pixmap),
2475 tree_view->priv->press_start_x + 1,
2476 row_pixmap_y + cell_y + 1);
2478 g_object_unref (row_pixmap);
2480 g_object_unref (mask);
2483 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2484 * handler so that we can tell apart the drag_leave event that comes right
2485 * before a drag_drop, from a normal drag_leave. We don't want to set the
2486 * cursor nor the flag in the latter case.
2489 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2491 GDK_THREADS_ENTER ();
2493 shortcuts_drag_set_delete_cursor (impl, TRUE);
2494 impl->shortcuts_drag_outside = TRUE;
2496 shortcuts_cancel_drag_outside_idle (impl);
2498 GDK_THREADS_LEAVE ();
2504 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2508 shortcuts_drag_leave_cb (GtkWidget *widget,
2509 GdkDragContext *context,
2511 GtkFileChooserDefault *impl)
2514 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2516 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2517 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2518 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2520 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2524 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2526 GTK_TREE_VIEW_DROP_BEFORE);
2528 g_signal_stop_emission_by_name (widget, "drag-leave");
2531 /* Computes the appropriate row and position for dropping */
2533 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2537 GtkTreeViewDropPosition *pos)
2539 GtkTreeView *tree_view;
2540 GtkTreeViewColumn *column;
2544 int bookmarks_index;
2546 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2548 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2550 if (!gtk_tree_view_get_path_at_pos (tree_view,
2552 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2558 row = bookmarks_index + impl->num_bookmarks - 1;
2559 *path = gtk_tree_path_new_from_indices (row, -1);
2560 *pos = GTK_TREE_VIEW_DROP_AFTER;
2564 row = *gtk_tree_path_get_indices (*path);
2565 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2566 gtk_tree_path_free (*path);
2568 if (row < bookmarks_index)
2570 row = bookmarks_index;
2571 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2573 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2575 row = bookmarks_index + impl->num_bookmarks - 1;
2576 *pos = GTK_TREE_VIEW_DROP_AFTER;
2580 if (cell_y < cell.height / 2)
2581 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2583 *pos = GTK_TREE_VIEW_DROP_AFTER;
2586 *path = gtk_tree_path_new_from_indices (row, -1);
2589 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2590 * implement the destination side of DnD by hand, due to limitations in
2591 * GtkTreeView's DnD API.
2594 shortcuts_drag_motion_cb (GtkWidget *widget,
2595 GdkDragContext *context,
2599 GtkFileChooserDefault *impl)
2602 GtkTreeViewDropPosition pos;
2603 GdkDragAction action;
2606 if (gtk_drag_get_source_widget (context) == widget)
2608 shortcuts_cancel_drag_outside_idle (impl);
2610 if (impl->shortcuts_drag_outside)
2612 shortcuts_drag_set_delete_cursor (impl, FALSE);
2613 impl->shortcuts_drag_outside = FALSE;
2618 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2619 action = GDK_ACTION_COPY;
2620 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2621 action = GDK_ACTION_MOVE;
2628 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2629 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2630 gtk_tree_path_free (path);
2634 g_signal_stop_emission_by_name (widget, "drag-motion");
2638 gdk_drag_status (context, action, time_);
2645 /* GtkWidget::drag-drop handler for the shortcuts list. */
2647 shortcuts_drag_drop_cb (GtkWidget *widget,
2648 GdkDragContext *context,
2652 GtkFileChooserDefault *impl)
2655 shortcuts_cancel_drag_outside_idle (impl);
2658 g_signal_stop_emission_by_name (widget, "drag-drop");
2662 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2664 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2671 uris = g_uri_list_extract_uris (data);
2673 for (i = 0; uris[i]; i++)
2679 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2683 if (shortcuts_add_bookmark_from_path (impl, path, position))
2686 gtk_file_path_free (path);
2692 g_set_error (&error,
2693 GTK_FILE_CHOOSER_ERROR,
2694 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
2695 _("Could not add a bookmark for '%s' "
2696 "because it is an invalid path name."),
2698 error_adding_bookmark_dialog (impl, path, error);
2705 /* Reorders the selected bookmark to the specified position */
2707 shortcuts_reorder (GtkFileChooserDefault *impl,
2715 int bookmarks_index;
2716 const GtkFilePath *file_path;
2717 GtkFilePath *file_path_copy;
2720 /* Get the selected path */
2722 if (!shortcuts_get_selected (impl, &iter))
2723 g_assert_not_reached ();
2725 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2726 old_position = *gtk_tree_path_get_indices (path);
2727 gtk_tree_path_free (path);
2729 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2730 old_position -= bookmarks_index;
2731 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2733 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2734 SHORTCUTS_COL_DATA, &col_data,
2735 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2737 g_assert (col_data != NULL);
2738 g_assert (!is_volume);
2740 file_path = col_data;
2741 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2743 /* Remove the path from the old position and insert it in the new one */
2745 if (new_position > old_position)
2748 if (old_position == new_position)
2752 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2753 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2755 error_adding_bookmark_dialog (impl, file_path_copy, error);
2759 gtk_file_path_free (file_path_copy);
2762 /* Callback used when we get the drag data for the bookmarks list. We add the
2763 * received URIs as bookmarks if they are folders.
2766 shortcuts_drag_data_received_cb (GtkWidget *widget,
2767 GdkDragContext *context,
2770 GtkSelectionData *selection_data,
2775 GtkFileChooserDefault *impl;
2776 GtkTreePath *tree_path;
2777 GtkTreeViewDropPosition tree_pos;
2779 int bookmarks_index;
2781 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2783 /* Compute position */
2785 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2787 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2788 position = *gtk_tree_path_get_indices (tree_path);
2789 gtk_tree_path_free (tree_path);
2791 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2794 g_assert (position >= bookmarks_index);
2795 position -= bookmarks_index;
2797 if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2798 shortcuts_drop_uris (impl, selection_data->data, position);
2799 else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2800 shortcuts_reorder (impl, position);
2802 g_signal_stop_emission_by_name (widget, "drag-data-received");
2805 /* Callback used when the selection in the shortcuts tree changes */
2807 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
2808 GtkFileChooserDefault *impl)
2810 bookmarks_check_remove_sensitivity (impl);
2814 shortcuts_row_separator_func (GtkTreeModel *model,
2818 gint column = GPOINTER_TO_INT (data);
2821 gtk_tree_model_get (model, iter, column, &text, -1);
2831 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
2832 * keypresses before the TreeView gets them.
2835 tree_view_keybinding_cb (GtkWidget *tree_view,
2837 GtkFileChooserDefault *impl)
2839 if (event->keyval == GDK_slash &&
2840 ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
2842 location_popup_handler (impl, "/");
2850 /* Creates the widgets for the shortcuts and bookmarks tree */
2852 shortcuts_list_create (GtkFileChooserDefault *impl)
2855 GtkTreeSelection *selection;
2856 GtkTreeViewColumn *column;
2857 GtkCellRenderer *renderer;
2859 /* Scrolled window */
2861 swin = gtk_scrolled_window_new (NULL, NULL);
2862 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2863 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2864 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2866 gtk_widget_show (swin);
2870 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
2871 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2872 G_CALLBACK (tree_view_keybinding_cb), impl);
2873 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Shortcuts"));
2874 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
2876 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
2878 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2880 shortcuts_source_targets,
2881 num_shortcuts_source_targets,
2884 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
2885 GTK_DEST_DEFAULT_ALL,
2886 shortcuts_dest_targets,
2887 num_shortcuts_dest_targets,
2888 GDK_ACTION_COPY | GDK_ACTION_MOVE);
2890 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2891 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
2892 gtk_tree_selection_set_select_function (selection,
2893 shortcuts_select_func,
2896 g_signal_connect (selection, "changed",
2897 G_CALLBACK (shortcuts_selection_changed_cb), impl);
2899 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
2900 G_CALLBACK (shortcuts_row_activated_cb), impl);
2902 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2903 G_CALLBACK (shortcuts_key_press_event_cb), impl);
2905 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
2906 G_CALLBACK (shortcuts_drag_begin_cb), impl);
2907 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
2908 G_CALLBACK (shortcuts_drag_end_cb), impl);
2909 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
2910 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
2912 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
2913 G_CALLBACK (shortcuts_drag_leave_cb), impl);
2914 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
2915 G_CALLBACK (shortcuts_drag_motion_cb), impl);
2916 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
2917 G_CALLBACK (shortcuts_drag_drop_cb), impl);
2918 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
2919 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
2921 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
2922 gtk_widget_show (impl->browse_shortcuts_tree_view);
2926 column = gtk_tree_view_column_new ();
2927 gtk_tree_view_column_set_title (column, _("Folder"));
2929 renderer = gtk_cell_renderer_pixbuf_new ();
2930 gtk_tree_view_column_pack_start (column, renderer, FALSE);
2931 gtk_tree_view_column_set_attributes (column, renderer,
2932 "pixbuf", SHORTCUTS_COL_PIXBUF,
2933 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2936 renderer = gtk_cell_renderer_text_new ();
2937 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2938 gtk_tree_view_column_set_attributes (column, renderer,
2939 "text", SHORTCUTS_COL_NAME,
2942 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2943 shortcuts_row_separator_func,
2944 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
2947 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
2952 /* Creates the widgets for the shortcuts/bookmarks pane */
2954 shortcuts_pane_create (GtkFileChooserDefault *impl,
2955 GtkSizeGroup *size_group)
2961 vbox = gtk_vbox_new (FALSE, 6);
2962 gtk_widget_show (vbox);
2964 /* Shortcuts tree */
2966 widget = shortcuts_list_create (impl);
2967 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
2969 /* Box for buttons */
2971 hbox = gtk_hbox_new (TRUE, 6);
2972 gtk_size_group_add_widget (size_group, hbox);
2973 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2974 gtk_widget_show (hbox);
2976 /* Add bookmark button */
2978 impl->browse_shortcuts_add_button = button_new (impl,
2983 G_CALLBACK (add_bookmark_button_clicked_cb));
2984 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
2985 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
2986 _("Add the selected folder to the bookmarks"), NULL);
2988 /* Remove bookmark button */
2990 impl->browse_shortcuts_remove_button = button_new (impl,
2995 G_CALLBACK (remove_bookmark_button_clicked_cb));
2996 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
2997 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2998 _("Remove the selected bookmark"), NULL);
3003 /* Handles key press events on the file list, so that we can trap Enter to
3004 * activate the default button on our own. Also, checks to see if '/' has been
3005 * pressed. See comment by tree_view_keybinding_cb() for more details.
3008 trap_activate_cb (GtkWidget *widget,
3012 GtkFileChooserDefault *impl;
3014 impl = (GtkFileChooserDefault *) data;
3016 if (event->keyval == GDK_slash &&
3017 ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3019 location_popup_handler (impl, "/");
3023 if ((event->keyval == GDK_Return
3024 || event->keyval == GDK_ISO_Enter
3025 || event->keyval == GDK_KP_Enter
3026 || event->keyval == GDK_space)
3027 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3028 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3032 window = get_toplevel (widget);
3034 && widget != window->default_widget
3035 && !(widget == window->focus_widget &&
3036 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3038 gtk_window_activate_default (window);
3046 /* Callback used when the file list's popup menu is detached */
3048 popup_menu_detach_cb (GtkWidget *attach_widget,
3051 GtkFileChooserDefault *impl;
3053 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3054 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3056 impl->browse_files_popup_menu = NULL;
3057 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3058 impl->browse_files_popup_menu_hidden_files_item = NULL;
3061 /* Callback used when the "Add to Shortcuts" menu item is activated */
3063 add_to_shortcuts_cb (GtkMenuItem *item,
3064 GtkFileChooserDefault *impl)
3066 bookmarks_add_selected_folder (impl);
3069 /* Callback used when the "Open Location" menu item is activated */
3071 open_location_cb (GtkMenuItem *item,
3072 GtkFileChooserDefault *impl)
3074 location_popup_handler (impl, "");
3077 /* Callback used when the "Show Hidden Files" menu item is toggled */
3079 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3080 GtkFileChooserDefault *impl)
3083 "show-hidden", gtk_check_menu_item_get_active (item),
3087 /* Constructs the popup menu for the file list if needed */
3089 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3093 if (impl->browse_files_popup_menu)
3096 impl->browse_files_popup_menu = gtk_menu_new ();
3097 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3098 impl->browse_files_tree_view,
3099 popup_menu_detach_cb);
3101 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Shortcuts"));
3102 impl->browse_files_popup_menu_add_shortcut_item = item;
3103 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3104 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3105 gtk_widget_set_sensitive (item, FALSE);
3106 g_signal_connect (item, "activate",
3107 G_CALLBACK (add_to_shortcuts_cb), impl);
3108 gtk_widget_show (item);
3109 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3111 item = gtk_image_menu_item_new_with_mnemonic (_("Open _Location"));
3112 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3113 gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU));
3114 g_signal_connect (item, "activate",
3115 G_CALLBACK (open_location_cb), impl);
3116 gtk_widget_show (item);
3117 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3119 item = gtk_separator_menu_item_new ();
3120 gtk_widget_show (item);
3121 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3123 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3124 impl->browse_files_popup_menu_hidden_files_item = item;
3125 g_signal_connect (item, "toggled",
3126 G_CALLBACK (show_hidden_toggled_cb), impl);
3127 gtk_widget_show (item);
3128 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3131 /* Updates the popup menu for the file list, creating it if necessary */
3133 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3135 file_list_build_popup_menu (impl);
3137 /* The sensitivity of the Add to Shortcuts item is set in
3138 * bookmarks_check_add_sensitivity()
3141 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3142 G_CALLBACK (show_hidden_toggled_cb), impl);
3143 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3145 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3146 G_CALLBACK (show_hidden_toggled_cb), impl);
3150 popup_position_func (GtkMenu *menu,
3156 GtkWidget *widget = GTK_WIDGET (user_data);
3157 GdkScreen *screen = gtk_widget_get_screen (widget);
3160 GdkRectangle monitor;
3162 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3164 gdk_window_get_origin (widget->window, x, y);
3166 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3168 *x += (widget->allocation.width - req.width) / 2;
3169 *y += (widget->allocation.height - req.height) / 2;
3171 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3172 gtk_menu_set_monitor (menu, monitor_num);
3173 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3175 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3176 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3182 file_list_popup_menu (GtkFileChooserDefault *impl,
3183 GdkEventButton *event)
3185 file_list_update_popup_menu (impl);
3187 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3188 NULL, NULL, NULL, NULL,
3189 event->button, event->time);
3192 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3194 popup_position_func, impl->browse_files_tree_view,
3195 0, GDK_CURRENT_TIME);
3196 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3202 /* Callback used for the GtkWidget::popup-menu signal of the file list */
3204 list_popup_menu_cb (GtkWidget *widget,
3205 GtkFileChooserDefault *impl)
3207 file_list_popup_menu (impl, NULL);
3211 /* Callback used when a button is pressed on the file list. We trap button 3 to
3212 * bring up a popup menu.
3215 list_button_press_event_cb (GtkWidget *widget,
3216 GdkEventButton *event,
3217 GtkFileChooserDefault *impl)
3219 if (event->button != 3)
3222 file_list_popup_menu (impl, event);
3226 /* Creates the widgets for the file list */
3228 create_file_list (GtkFileChooserDefault *impl)
3231 GtkTreeSelection *selection;
3232 GtkTreeViewColumn *column;
3233 GtkCellRenderer *renderer;
3235 /* Scrolled window */
3237 swin = gtk_scrolled_window_new (NULL, NULL);
3238 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3239 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3240 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3243 /* Tree/list view */
3245 impl->browse_files_tree_view = gtk_tree_view_new ();
3246 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "GtkFileChooserDefault", impl);
3247 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
3249 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
3250 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
3251 g_signal_connect (impl->browse_files_tree_view, "row-activated",
3252 G_CALLBACK (list_row_activated), impl);
3253 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
3254 G_CALLBACK (trap_activate_cb), impl);
3255 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
3256 G_CALLBACK (list_popup_menu_cb), impl);
3257 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
3258 G_CALLBACK (list_button_press_event_cb), impl);
3260 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3261 gtk_tree_selection_set_select_function (selection,
3264 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
3266 file_list_source_targets,
3267 num_file_list_source_targets,
3270 g_signal_connect (selection, "changed",
3271 G_CALLBACK (list_selection_changed), impl);
3273 /* Filename column */
3275 impl->list_name_column = gtk_tree_view_column_new ();
3276 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
3277 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
3278 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
3279 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
3281 renderer = gtk_cell_renderer_pixbuf_new ();
3282 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
3283 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
3284 list_icon_data_func, impl, NULL);
3286 impl->list_name_renderer = gtk_cell_renderer_text_new ();
3287 g_object_set (impl->list_name_renderer,
3288 "ellipsize", PANGO_ELLIPSIZE_END,
3290 g_signal_connect (impl->list_name_renderer, "edited",
3291 G_CALLBACK (renderer_edited_cb), impl);
3292 g_signal_connect (impl->list_name_renderer, "editing-canceled",
3293 G_CALLBACK (renderer_editing_canceled_cb), impl);
3294 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
3295 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
3296 list_name_data_func, impl, NULL);
3298 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
3302 column = gtk_tree_view_column_new ();
3303 gtk_tree_view_column_set_title (column, _("Size"));
3305 renderer = gtk_cell_renderer_text_new ();
3306 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3307 gtk_tree_view_column_set_cell_data_func (column, renderer,
3308 list_size_data_func, impl, NULL);
3309 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
3310 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3312 /* Modification time column */
3314 column = gtk_tree_view_column_new ();
3315 gtk_tree_view_column_set_resizable (column, TRUE);
3316 gtk_tree_view_column_set_title (column, _("Modified"));
3318 renderer = gtk_cell_renderer_text_new ();
3319 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3320 gtk_tree_view_column_set_cell_data_func (column, renderer,
3321 list_mtime_data_func, impl, NULL);
3322 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
3323 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3324 gtk_widget_show_all (swin);
3330 create_path_bar (GtkFileChooserDefault *impl)
3332 GtkWidget *path_bar;
3334 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
3335 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
3341 set_filter_tooltip (GtkWidget *widget,
3344 GtkTooltips *tooltips = (GtkTooltips *)data;
3346 if (GTK_IS_BUTTON (widget))
3347 gtk_tooltips_set_tip (tooltips, widget,
3348 _("Select which types of files are shown"),
3353 realize_filter_combo (GtkWidget *combo,
3356 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
3358 gtk_container_forall (GTK_CONTAINER (combo),
3363 /* Creates the widgets for the files/folders pane */
3365 file_pane_create (GtkFileChooserDefault *impl,
3366 GtkSizeGroup *size_group)
3372 vbox = gtk_vbox_new (FALSE, 6);
3373 gtk_widget_show (vbox);
3375 /* The path bar and 'Create Folder' button */
3376 hbox = gtk_hbox_new (FALSE, 12);
3377 gtk_widget_show (hbox);
3378 impl->browse_path_bar = create_path_bar (impl);
3379 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
3380 gtk_widget_show_all (impl->browse_path_bar);
3381 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
3384 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
3385 g_signal_connect (impl->browse_new_folder_button, "clicked",
3386 G_CALLBACK (new_folder_button_clicked), impl);
3387 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
3388 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3390 /* Box for lists and preview */
3392 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
3393 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
3394 gtk_widget_show (hbox);
3398 widget = create_file_list (impl);
3399 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
3403 impl->preview_box = gtk_vbox_new (FALSE, 12);
3404 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
3405 /* Don't show preview box initially */
3409 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
3411 widget = filter_create (impl);
3413 g_signal_connect (widget, "realize",
3414 G_CALLBACK (realize_filter_combo), impl);
3416 gtk_widget_show (widget);
3417 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
3419 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
3420 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
3424 /* Callback used when the "Browse for more folders" expander is toggled */
3426 expander_changed_cb (GtkExpander *expander,
3428 GtkFileChooserDefault *impl)
3430 update_appearance (impl);
3433 /* Callback used when the selection changes in the save folder combo box */
3435 save_folder_combo_changed_cb (GtkComboBox *combo,
3436 GtkFileChooserDefault *impl)
3440 if (impl->changing_folder)
3443 if (gtk_combo_box_get_active_iter (combo, &iter))
3444 shortcuts_activate_iter (impl, &iter);
3447 /* Creates the combo box with the save folders */
3449 save_folder_combo_create (GtkFileChooserDefault *impl)
3452 GtkCellRenderer *cell;
3454 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
3455 gtk_widget_show (combo);
3457 cell = gtk_cell_renderer_pixbuf_new ();
3458 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3459 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3460 "pixbuf", SHORTCUTS_COL_PIXBUF,
3461 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3462 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3465 cell = gtk_cell_renderer_text_new ();
3466 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3467 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3468 "text", SHORTCUTS_COL_NAME,
3469 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3472 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
3473 shortcuts_row_separator_func,
3474 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3477 g_signal_connect (combo, "changed",
3478 G_CALLBACK (save_folder_combo_changed_cb), impl);
3483 /* Creates the widgets specific to Save mode */
3485 save_widgets_create (GtkFileChooserDefault *impl)
3490 GtkWidget *alignment;
3492 vbox = gtk_vbox_new (FALSE, 12);
3494 table = gtk_table_new (2, 2, FALSE);
3495 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3496 gtk_widget_show (table);
3497 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3498 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3502 widget = gtk_label_new_with_mnemonic (_("_Name:"));
3503 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3504 gtk_table_attach (GTK_TABLE (table), widget,
3508 gtk_widget_show (widget);
3510 impl->save_file_name_entry = _gtk_file_chooser_entry_new (TRUE);
3511 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3513 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3514 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3515 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3517 GTK_EXPAND | GTK_FILL, 0,
3519 gtk_widget_show (impl->save_file_name_entry);
3520 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
3523 impl->save_folder_label = gtk_label_new (NULL);
3524 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
3525 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
3529 gtk_widget_show (impl->save_folder_label);
3531 impl->save_folder_combo = save_folder_combo_create (impl);
3532 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
3534 GTK_EXPAND | GTK_FILL, GTK_FILL,
3536 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
3539 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3540 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
3542 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
3543 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
3544 g_signal_connect (impl->save_expander, "notify::expanded",
3545 G_CALLBACK (expander_changed_cb),
3547 gtk_widget_show_all (alignment);
3552 /* Creates the main hpaned with the widgets shared by Open and Save mode */
3554 browse_widgets_create (GtkFileChooserDefault *impl)
3559 GtkSizeGroup *size_group;
3561 /* size group is used by the [+][-] buttons and the filter combo */
3562 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
3563 vbox = gtk_vbox_new (FALSE, 12);
3566 hpaned = gtk_hpaned_new ();
3567 gtk_widget_show (hpaned);
3568 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
3569 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
3571 widget = shortcuts_pane_create (impl, size_group);
3572 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
3573 widget = file_pane_create (impl, size_group);
3574 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
3576 g_object_unref (size_group);
3582 gtk_file_chooser_default_constructor (GType type,
3583 guint n_construct_properties,
3584 GObjectConstructParam *construct_params)
3586 GtkFileChooserDefault *impl;
3589 object = parent_class->constructor (type,
3590 n_construct_properties,
3592 impl = GTK_FILE_CHOOSER_DEFAULT (object);
3594 g_assert (impl->file_system);
3596 gtk_widget_push_composite_child ();
3598 /* Shortcuts model */
3600 shortcuts_model_create (impl);
3602 /* Widgets for Save mode */
3603 impl->save_widgets = save_widgets_create (impl);
3604 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
3606 /* The browse widgets */
3607 impl->browse_widgets = browse_widgets_create (impl);
3608 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
3610 /* Alignment to hold extra widget */
3611 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3612 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
3614 gtk_widget_pop_composite_child ();
3615 update_appearance (impl);
3620 /* Sets the extra_widget by packing it in the appropriate place */
3622 set_extra_widget (GtkFileChooserDefault *impl,
3623 GtkWidget *extra_widget)
3627 g_object_ref (extra_widget);
3628 /* FIXME: is this right ? */
3629 gtk_widget_show (extra_widget);
3632 if (impl->extra_widget)
3634 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3635 g_object_unref (impl->extra_widget);
3638 impl->extra_widget = extra_widget;
3639 if (impl->extra_widget)
3641 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3642 gtk_widget_show (impl->extra_align);
3645 gtk_widget_hide (impl->extra_align);
3649 set_local_only (GtkFileChooserDefault *impl,
3650 gboolean local_only)
3652 if (local_only != impl->local_only)
3654 impl->local_only = local_only;
3656 if (impl->shortcuts_model && impl->file_system)
3658 shortcuts_add_volumes (impl);
3659 shortcuts_add_bookmarks (impl);
3663 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
3665 /* If we are pointing to a non-local folder, make an effort to change
3666 * back to a local folder, but it's really up to the app to not cause
3667 * such a situation, so we ignore errors.
3669 const gchar *home = g_get_home_dir ();
3670 GtkFilePath *home_path;
3675 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
3677 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
3679 gtk_file_path_free (home_path);
3685 volumes_changed_cb (GtkFileSystem *file_system,
3686 GtkFileChooserDefault *impl)
3688 shortcuts_add_volumes (impl);
3691 /* Callback used when the set of bookmarks changes in the file system */
3693 bookmarks_changed_cb (GtkFileSystem *file_system,
3694 GtkFileChooserDefault *impl)
3696 shortcuts_add_bookmarks (impl);
3698 bookmarks_check_add_sensitivity (impl);
3699 bookmarks_check_remove_sensitivity (impl);
3702 /* Sets the file chooser to multiple selection mode */
3704 set_select_multiple (GtkFileChooserDefault *impl,
3705 gboolean select_multiple,
3706 gboolean property_notify)
3708 GtkTreeSelection *selection;
3709 GtkSelectionMode mode;
3711 if (select_multiple == impl->select_multiple)
3714 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3716 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3717 gtk_tree_selection_set_mode (selection, mode);
3719 impl->select_multiple = select_multiple;
3720 g_object_notify (G_OBJECT (impl), "select-multiple");
3722 check_preview_change (impl);
3726 set_file_system_backend (GtkFileChooserDefault *impl,
3727 const char *backend)
3729 if (impl->file_system)
3731 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3732 impl->volumes_changed_id = 0;
3733 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3734 impl->bookmarks_changed_id = 0;
3735 g_object_unref (impl->file_system);
3738 impl->file_system = NULL;
3740 impl->file_system = _gtk_file_system_create (backend);
3743 GtkSettings *settings = gtk_settings_get_default ();
3744 gchar *default_backend = NULL;
3746 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3747 if (default_backend)
3749 impl->file_system = _gtk_file_system_create (default_backend);
3750 g_free (default_backend);
3754 if (!impl->file_system)
3756 #if defined (G_OS_UNIX)
3757 impl->file_system = gtk_file_system_unix_new ();
3758 #elif defined (G_OS_WIN32)
3759 impl->file_system = gtk_file_system_win32_new ();
3761 #error "No default filesystem implementation on the platform"
3765 if (impl->file_system)
3767 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3768 G_CALLBACK (volumes_changed_cb),
3770 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3771 G_CALLBACK (bookmarks_changed_cb),
3776 /* This function is basically a do_all function.
3778 * It sets the visibility on all the widgets based on the current state, and
3779 * moves the custom_widget if needed.
3782 update_appearance (GtkFileChooserDefault *impl)
3784 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3785 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3789 gtk_widget_show (impl->save_widgets);
3791 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3792 text = _("Save in _folder:");
3794 text = _("Create in _folder:");
3796 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
3798 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3800 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
3801 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
3802 gtk_widget_show (impl->browse_widgets);
3806 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
3807 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
3808 gtk_widget_hide (impl->browse_widgets);
3811 gtk_widget_show (impl->browse_new_folder_button);
3813 if (impl->select_multiple)
3815 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
3816 "Re-setting to single selection mode.");
3817 set_select_multiple (impl, FALSE, TRUE);
3820 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3821 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
3823 gtk_widget_hide (impl->save_widgets);
3824 gtk_widget_show (impl->browse_widgets);
3827 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
3828 gtk_widget_hide (impl->browse_new_folder_button);
3830 gtk_widget_show (impl->browse_new_folder_button);
3832 gtk_widget_queue_draw (impl->browse_files_tree_view);
3834 g_signal_emit_by_name (impl, "default-size-changed");
3838 gtk_file_chooser_default_set_property (GObject *object,
3840 const GValue *value,
3844 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3848 case GTK_FILE_CHOOSER_PROP_ACTION:
3850 GtkFileChooserAction action = g_value_get_enum (value);
3852 if (action != impl->action)
3854 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
3856 if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
3858 g_warning ("Multiple selection mode is not allowed in Save mode");
3859 set_select_multiple (impl, FALSE, TRUE);
3861 impl->action = action;
3862 update_appearance (impl);
3865 if (impl->save_file_name_entry)
3866 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3870 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
3871 set_file_system_backend (impl, g_value_get_string (value));
3873 case GTK_FILE_CHOOSER_PROP_FILTER:
3874 set_current_filter (impl, g_value_get_object (value));
3876 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3877 set_local_only (impl, g_value_get_boolean (value));
3879 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3880 set_preview_widget (impl, g_value_get_object (value));
3882 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3883 impl->preview_widget_active = g_value_get_boolean (value);
3884 update_preview_widget_visibility (impl);
3886 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3887 impl->use_preview_label = g_value_get_boolean (value);
3888 update_preview_widget_visibility (impl);
3890 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3891 set_extra_widget (impl, g_value_get_object (value));
3893 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3895 gboolean select_multiple = g_value_get_boolean (value);
3896 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
3898 g_warning ("Multiple selection mode is not allowed in Save mode");
3902 set_select_multiple (impl, select_multiple, FALSE);
3905 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3907 gboolean show_hidden = g_value_get_boolean (value);
3908 if (show_hidden != impl->show_hidden)
3910 impl->show_hidden = show_hidden;
3912 if (impl->browse_files_model)
3913 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
3918 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3924 gtk_file_chooser_default_get_property (GObject *object,
3929 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3933 case GTK_FILE_CHOOSER_PROP_ACTION:
3934 g_value_set_enum (value, impl->action);
3936 case GTK_FILE_CHOOSER_PROP_FILTER:
3937 g_value_set_object (value, impl->current_filter);
3939 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3940 g_value_set_boolean (value, impl->local_only);
3942 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3943 g_value_set_object (value, impl->preview_widget);
3945 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3946 g_value_set_boolean (value, impl->preview_widget_active);
3948 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3949 g_value_set_boolean (value, impl->use_preview_label);
3951 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3952 g_value_set_object (value, impl->extra_widget);
3954 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3955 g_value_set_boolean (value, impl->select_multiple);
3957 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3958 g_value_set_boolean (value, impl->show_hidden);
3961 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3966 /* Removes the settings signal handler. It's safe to call multiple times */
3968 remove_settings_signal (GtkFileChooserDefault *impl,
3971 if (impl->settings_signal_id)
3973 GtkSettings *settings;
3975 settings = gtk_settings_get_for_screen (screen);
3976 g_signal_handler_disconnect (settings,
3977 impl->settings_signal_id);
3978 impl->settings_signal_id = 0;
3983 gtk_file_chooser_default_dispose (GObject *object)
3985 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
3987 if (impl->extra_widget)
3989 g_object_unref (impl->extra_widget);
3990 impl->extra_widget = NULL;
3993 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
3995 G_OBJECT_CLASS (parent_class)->dispose (object);
3998 /* We override show-all since we have internal widgets that
3999 * shouldn't be shown when you call show_all(), like the filter
4003 gtk_file_chooser_default_show_all (GtkWidget *widget)
4005 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
4007 gtk_widget_show (widget);
4009 if (impl->extra_widget)
4010 gtk_widget_show_all (impl->extra_widget);
4013 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
4014 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
4017 toplevel_set_focus_cb (GtkWindow *window,
4019 GtkFileChooserDefault *impl)
4021 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
4024 /* We monitor the focus widget on our toplevel to be able to know which widget
4025 * was last focused at the time our "should_respond" method gets called.
4028 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
4029 GtkWidget *previous_toplevel)
4031 GtkFileChooserDefault *impl;
4032 GtkWidget *toplevel;
4034 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4036 if (previous_toplevel)
4038 g_assert (impl->toplevel_set_focus_id != 0);
4039 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
4040 impl->toplevel_set_focus_id = 0;
4041 impl->toplevel_last_focus_widget = NULL;
4044 g_assert (impl->toplevel_set_focus_id == 0);
4046 toplevel = gtk_widget_get_toplevel (widget);
4047 if (GTK_IS_WINDOW (toplevel))
4049 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
4050 G_CALLBACK (toplevel_set_focus_cb), impl);
4051 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
4055 /* Changes the icons wherever it is needed */
4057 change_icon_theme (GtkFileChooserDefault *impl)
4059 GtkSettings *settings;
4062 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4064 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
4065 impl->icon_size = MAX (width, height);
4067 impl->icon_size = FALLBACK_ICON_SIZE;
4069 shortcuts_reload_icons (impl);
4070 gtk_widget_queue_resize (impl->browse_files_tree_view);
4073 /* Callback used when a GtkSettings value changes */
4075 settings_notify_cb (GObject *object,
4077 GtkFileChooserDefault *impl)
4081 name = g_param_spec_get_name (pspec);
4083 if (strcmp (name, "gtk-icon-theme-name") == 0
4084 || strcmp (name, "gtk-icon-sizes") == 0)
4085 change_icon_theme (impl);
4088 /* Installs a signal handler for GtkSettings so that we can monitor changes in
4092 check_icon_theme (GtkFileChooserDefault *impl)
4094 GtkSettings *settings;
4096 if (impl->settings_signal_id)
4099 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4101 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4102 impl->settings_signal_id = g_signal_connect (settings, "notify",
4103 G_CALLBACK (settings_notify_cb), impl);
4105 change_icon_theme (impl);
4110 gtk_file_chooser_default_style_set (GtkWidget *widget,
4111 GtkStyle *previous_style)
4113 GtkFileChooserDefault *impl;
4115 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4117 if (GTK_WIDGET_CLASS (parent_class)->style_set)
4118 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
4120 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4121 change_icon_theme (impl);
4123 g_signal_emit_by_name (widget, "default-size-changed");
4127 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
4128 GdkScreen *previous_screen)
4130 GtkFileChooserDefault *impl;
4132 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4134 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
4135 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
4137 remove_settings_signal (impl, previous_screen);
4138 check_icon_theme (impl);
4140 g_signal_emit_by_name (widget, "default-size-changed");
4144 get_is_file_filtered (GtkFileChooserDefault *impl,
4145 const GtkFilePath *path,
4146 GtkFileInfo *file_info)
4148 GtkFileFilterInfo filter_info;
4149 GtkFileFilterFlags needed;
4152 if (!impl->current_filter)
4155 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
4157 needed = gtk_file_filter_get_needed (impl->current_filter);
4159 filter_info.display_name = gtk_file_info_get_display_name (file_info);
4160 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
4162 if (needed & GTK_FILE_FILTER_FILENAME)
4164 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
4165 if (filter_info.filename)
4166 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
4169 filter_info.filename = NULL;
4171 if (needed & GTK_FILE_FILTER_URI)
4173 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
4174 if (filter_info.uri)
4175 filter_info.contains |= GTK_FILE_FILTER_URI;
4178 filter_info.uri = NULL;
4180 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
4182 if (filter_info.filename)
4183 g_free ((gchar *)filter_info.filename);
4184 if (filter_info.uri)
4185 g_free ((gchar *)filter_info.uri);
4190 /* GtkWidget::map method */
4192 gtk_file_chooser_default_map (GtkWidget *widget)
4194 GtkFileChooserDefault *impl;
4196 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4198 GTK_WIDGET_CLASS (parent_class)->map (widget);
4200 if (impl->current_folder)
4202 pending_select_paths_store_selection (impl);
4203 change_folder_and_display_error (impl, impl->current_folder);
4206 bookmarks_changed_cb (impl->file_system, impl);
4210 list_model_filter_func (GtkFileSystemModel *model,
4212 const GtkFileInfo *file_info,
4215 GtkFileChooserDefault *impl = user_data;
4217 if (!impl->current_filter)
4220 if (gtk_file_info_get_is_folder (file_info))
4223 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
4227 install_list_model_filter (GtkFileChooserDefault *impl)
4229 GtkFileSystemModelFilter filter;
4232 g_assert (impl->browse_files_model != NULL);
4234 if (impl->current_filter)
4236 filter = list_model_filter_func;
4245 _gtk_file_system_model_set_filter (impl->browse_files_model,
4250 #define COMPARE_DIRECTORIES \
4251 GtkFileChooserDefault *impl = user_data; \
4252 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
4253 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
4254 gboolean dir_a, dir_b; \
4257 dir_a = gtk_file_info_get_is_folder (info_a); \
4259 return impl->list_sort_ascending ? -1 : 1; \
4262 dir_b = gtk_file_info_get_is_folder (info_b); \
4264 return impl->list_sort_ascending ? 1 : -1; \
4266 if (dir_a != dir_b) \
4267 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
4269 /* Sort callback for the filename column */
4271 name_sort_func (GtkTreeModel *model,
4276 COMPARE_DIRECTORIES;
4278 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
4281 /* Sort callback for the size column */
4283 size_sort_func (GtkTreeModel *model,
4288 COMPARE_DIRECTORIES;
4291 gint64 size_a = gtk_file_info_get_size (info_a);
4292 gint64 size_b = gtk_file_info_get_size (info_b);
4294 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
4298 /* Sort callback for the mtime column */
4300 mtime_sort_func (GtkTreeModel *model,
4305 COMPARE_DIRECTORIES;
4308 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
4309 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
4311 return ta > tb ? -1 : (ta == tb ? 0 : 1);
4315 /* Callback used when the sort column changes. We cache the sort order for use
4316 * in name_sort_func().
4319 list_sort_column_changed_cb (GtkTreeSortable *sortable,
4320 GtkFileChooserDefault *impl)
4322 GtkSortType sort_type;
4324 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
4325 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
4329 set_busy_cursor (GtkFileChooserDefault *impl,
4332 GtkWindow *toplevel;
4333 GdkDisplay *display;
4336 toplevel = get_toplevel (GTK_WIDGET (impl));
4337 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
4340 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
4343 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
4347 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
4348 gdk_display_flush (display);
4351 gdk_cursor_unref (cursor);
4354 /* Creates a sort model to wrap the file system model and sets it on the tree view */
4356 load_set_model (GtkFileChooserDefault *impl)
4358 g_assert (impl->browse_files_model != NULL);
4359 g_assert (impl->sort_model == NULL);
4361 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4362 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4363 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4364 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4365 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4366 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4367 impl->list_sort_ascending = TRUE;
4369 g_signal_connect (impl->sort_model, "sort-column-changed",
4370 G_CALLBACK (list_sort_column_changed_cb), impl);
4372 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4373 GTK_TREE_MODEL (impl->sort_model));
4374 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4375 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4376 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4379 /* Timeout callback used when the loading timer expires */
4381 load_timeout_cb (gpointer data)
4383 GtkFileChooserDefault *impl;
4385 GDK_THREADS_ENTER ();
4387 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4388 g_assert (impl->load_state == LOAD_PRELOAD);
4389 g_assert (impl->load_timeout_id != 0);
4390 g_assert (impl->browse_files_model != NULL);
4392 impl->load_timeout_id = 0;
4393 impl->load_state = LOAD_LOADING;
4395 load_set_model (impl);
4397 GDK_THREADS_LEAVE ();
4402 /* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
4404 load_setup_timer (GtkFileChooserDefault *impl)
4406 g_assert (impl->load_timeout_id == 0);
4407 g_assert (impl->load_state != LOAD_PRELOAD);
4409 impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
4410 impl->load_state = LOAD_PRELOAD;
4413 /* Removes the load timeout and switches to the LOAD_FINISHED state */
4415 load_remove_timer (GtkFileChooserDefault *impl)
4417 if (impl->load_timeout_id != 0)
4419 g_assert (impl->load_state == LOAD_PRELOAD);
4421 g_source_remove (impl->load_timeout_id);
4422 impl->load_timeout_id = 0;
4423 impl->load_state = LOAD_EMPTY;
4426 g_assert (impl->load_state == LOAD_EMPTY ||
4427 impl->load_state == LOAD_LOADING ||
4428 impl->load_state == LOAD_FINISHED);
4431 /* Selects the first row in the file list */
4433 browse_files_select_first_row (GtkFileChooserDefault *impl)
4437 if (!impl->sort_model)
4440 path = gtk_tree_path_new_from_indices (0, -1);
4441 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4442 gtk_tree_path_free (path);
4445 struct center_selected_row_closure {
4446 GtkFileChooserDefault *impl;
4447 gboolean already_centered;
4450 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
4451 * selected row in the tree view.
4454 center_selected_row_foreach_cb (GtkTreeModel *model,
4459 struct center_selected_row_closure *closure;
4462 if (closure->already_centered)
4465 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
4466 closure->already_centered = TRUE;
4469 /* Centers the selected row in the tree view */
4471 browse_files_center_selected_row (GtkFileChooserDefault *impl)
4473 struct center_selected_row_closure closure;
4474 GtkTreeSelection *selection;
4476 closure.impl = impl;
4477 closure.already_centered = FALSE;
4479 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4480 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
4484 show_and_select_paths (GtkFileChooserDefault *impl,
4485 const GtkFilePath *parent_path,
4486 const GtkFilePath *only_one_path,
4490 GtkFileFolder *folder;
4492 gboolean have_hidden;
4493 gboolean have_filtered;
4495 if (!only_one_path && !paths)
4498 folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
4503 have_hidden = FALSE;
4504 have_filtered = FALSE;
4510 info = gtk_file_folder_get_info (folder, only_one_path, error);
4514 have_hidden = gtk_file_info_get_is_hidden (info);
4515 have_filtered = get_is_file_filtered (impl, only_one_path, info);
4516 gtk_file_info_free (info);
4523 for (l = paths; l; l = l->next)
4525 const GtkFilePath *path;
4531 info = gtk_file_folder_get_info (folder, path, NULL);
4535 have_hidden = gtk_file_info_get_is_hidden (info);
4538 have_filtered = get_is_file_filtered (impl, path, info);
4540 gtk_file_info_free (info);
4542 if (have_hidden && have_filtered)
4543 break; /* we now have all the information we need */
4550 g_object_unref (folder);
4556 g_object_set (impl, "show-hidden", TRUE, NULL);
4559 set_current_filter (impl, NULL);
4562 _gtk_file_system_model_path_do (impl->browse_files_model, only_one_path, select_func, impl);
4567 for (l = paths; l; l = l->next)
4569 const GtkFilePath *path;
4572 _gtk_file_system_model_path_do (impl->browse_files_model, path, select_func, impl);
4579 /* Processes the pending operation when a folder is finished loading */
4581 pending_select_paths_process (GtkFileChooserDefault *impl)
4583 g_assert (impl->load_state == LOAD_FINISHED);
4584 g_assert (impl->browse_files_model != NULL);
4585 g_assert (impl->sort_model != NULL);
4587 if (impl->pending_select_paths)
4590 show_and_select_paths (impl, impl->current_folder, NULL, impl->pending_select_paths, NULL);
4591 pending_select_paths_free (impl);
4592 browse_files_center_selected_row (impl);
4595 browse_files_select_first_row (impl);
4597 g_assert (impl->pending_select_paths == NULL);
4600 /* Callback used when the file system model finishes loading */
4602 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
4603 GtkFileChooserDefault *impl)
4605 if (impl->load_state == LOAD_PRELOAD)
4607 load_remove_timer (impl);
4608 load_set_model (impl);
4610 else if (impl->load_state == LOAD_LOADING)
4616 /* We can't g_assert_not_reached(), as something other than us may have
4617 * initiated a folder reload. See #165556.
4622 g_assert (impl->load_timeout_id == 0);
4624 impl->load_state = LOAD_FINISHED;
4626 pending_select_paths_process (impl);
4627 set_busy_cursor (impl, FALSE);
4630 /* Gets rid of the old list model and creates a new one for the current folder */
4632 set_list_model (GtkFileChooserDefault *impl,
4635 g_assert (impl->current_folder != NULL);
4637 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
4639 if (impl->browse_files_model)
4641 g_object_unref (impl->browse_files_model);
4642 impl->browse_files_model = NULL;
4645 if (impl->sort_model)
4647 g_object_unref (impl->sort_model);
4648 impl->sort_model = NULL;
4651 set_busy_cursor (impl, TRUE);
4652 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
4654 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
4655 impl->current_folder, 0,
4658 if (!impl->browse_files_model)
4660 set_busy_cursor (impl, FALSE);
4664 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
4666 g_signal_connect (impl->browse_files_model, "finished-loading",
4667 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
4669 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
4671 install_list_model_filter (impl);
4677 update_chooser_entry (GtkFileChooserDefault *impl)
4679 GtkTreeSelection *selection;
4680 const GtkFileInfo *info;
4682 GtkTreeIter child_iter;
4684 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
4687 g_assert (!impl->select_multiple);
4688 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4690 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4693 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4697 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4699 if (!gtk_file_info_get_is_folder (info))
4700 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4701 gtk_file_info_get_display_name (info));
4705 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
4706 const GtkFilePath *path,
4709 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4712 g_assert (path != NULL);
4714 if (impl->local_only &&
4715 !gtk_file_system_path_is_local (impl->file_system, path))
4718 GTK_FILE_CHOOSER_ERROR,
4719 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
4720 _("Cannot change to folder because it is not local"));
4725 /* Test validity of path here. */
4726 if (!check_is_folder (impl->file_system, path, error))
4729 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
4732 if (impl->current_folder != path)
4734 if (impl->current_folder)
4735 gtk_file_path_free (impl->current_folder);
4737 impl->current_folder = gtk_file_path_copy (path);
4740 /* Update the widgets that may trigger a folder change themselves. */
4742 if (!impl->changing_folder)
4744 impl->changing_folder = TRUE;
4746 shortcuts_update_current_folder (impl);
4748 impl->changing_folder = FALSE;
4751 /* Set the folder on the save entry */
4753 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4754 impl->current_folder);
4756 /* Create a new list model. This is slightly evil; we store the result value
4757 * but perform more actions rather than returning immediately even if it
4758 * generates an error.
4760 result = set_list_model (impl, error);
4762 /* Refresh controls */
4764 shortcuts_find_current_folder (impl);
4766 g_signal_emit_by_name (impl, "current-folder-changed", 0);
4768 check_preview_change (impl);
4769 bookmarks_check_add_sensitivity (impl);
4771 g_signal_emit_by_name (impl, "selection-changed", 0);
4776 static GtkFilePath *
4777 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
4779 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4781 return gtk_file_path_copy (impl->current_folder);
4785 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
4788 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4790 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4791 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4793 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), name);
4797 select_func (GtkFileSystemModel *model,
4802 GtkFileChooserDefault *impl = user_data;
4803 GtkTreeSelection *selection;
4804 GtkTreeIter sorted_iter;
4806 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4808 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
4809 gtk_tree_selection_select_iter (selection, &sorted_iter);
4813 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
4814 const GtkFilePath *path,
4817 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4818 GtkFilePath *parent_path;
4821 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
4825 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
4827 if (impl->load_state == LOAD_EMPTY)
4831 g_assert (impl->current_folder != NULL);
4833 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
4836 if (same_path && impl->load_state == LOAD_FINISHED)
4840 result = show_and_select_paths (impl, parent_path, path, NULL, error);
4841 gtk_file_path_free (parent_path);
4845 pending_select_paths_add (impl, path);
4851 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
4852 gtk_file_path_free (parent_path);
4860 unselect_func (GtkFileSystemModel *model,
4865 GtkFileChooserDefault *impl = user_data;
4866 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4867 GtkTreePath *sorted_path;
4869 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
4871 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
4873 gtk_tree_path_free (sorted_path);
4877 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
4878 const GtkFilePath *path)
4880 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4882 if (!impl->browse_files_model)
4885 _gtk_file_system_model_path_do (impl->browse_files_model, path,
4886 unselect_func, impl);
4890 maybe_select (GtkTreeModel *model,
4895 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
4896 GtkTreeSelection *selection;
4897 const GtkFileInfo *info;
4900 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4902 info = get_list_file_info (impl, iter);
4903 is_folder = gtk_file_info_get_is_folder (info);
4905 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
4906 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
4907 gtk_tree_selection_select_iter (selection, iter);
4909 gtk_tree_selection_unselect_iter (selection, iter);
4915 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
4917 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4918 if (impl->select_multiple)
4919 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
4920 maybe_select, impl);
4924 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
4926 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4927 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4929 gtk_tree_selection_unselect_all (selection);
4932 /* Checks whether the filename entry for the Save modes contains a valid filename */
4933 static GtkFilePath *
4934 check_save_entry (GtkFileChooserDefault *impl,
4938 GtkFileChooserEntry *chooser_entry;
4939 const GtkFilePath *current_folder;
4940 const char *file_part;
4944 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4945 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4947 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
4949 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
4950 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
4952 if (!file_part || file_part[0] == '\0')
4962 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
4966 error_building_filename_dialog (impl, current_folder, file_part, error);
4975 struct get_paths_closure {
4976 GtkFileChooserDefault *impl;
4978 GtkFilePath *path_from_entry;
4982 get_paths_foreach (GtkTreeModel *model,
4987 struct get_paths_closure *info;
4988 const GtkFilePath *file_path;
4989 GtkFileSystemModel *fs_model;
4990 GtkTreeIter sel_iter;
4993 fs_model = info->impl->browse_files_model;
4994 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
4996 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
4998 return; /* We are on the editable row */
5000 if (!info->path_from_entry
5001 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
5002 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
5006 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
5008 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5009 struct get_paths_closure info;
5013 info.path_from_entry = NULL;
5015 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5016 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5018 gboolean is_valid, is_empty;
5020 info.path_from_entry = check_save_entry (impl, &is_valid, &is_empty);
5021 if (!is_valid && !is_empty)
5025 if (!info.path_from_entry || impl->select_multiple)
5027 GtkTreeSelection *selection;
5029 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5030 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
5033 if (info.path_from_entry)
5034 info.result = g_slist_prepend (info.result, info.path_from_entry);
5036 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
5037 * fall back to the current directory */
5038 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
5039 info.result == NULL)
5041 info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
5044 return g_slist_reverse (info.result);
5047 static GtkFilePath *
5048 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
5050 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5052 if (impl->preview_path)
5053 return gtk_file_path_copy (impl->preview_path);
5058 static GtkFileSystem *
5059 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
5061 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5063 return impl->file_system;
5066 /* Shows or hides the filter widgets */
5068 show_filters (GtkFileChooserDefault *impl,
5072 gtk_widget_show (impl->filter_combo_hbox);
5074 gtk_widget_hide (impl->filter_combo_hbox);
5078 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
5079 GtkFileFilter *filter)
5081 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5084 if (g_slist_find (impl->filters, filter))
5086 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
5090 g_object_ref (filter);
5091 gtk_object_sink (GTK_OBJECT (filter));
5092 impl->filters = g_slist_append (impl->filters, filter);
5094 name = gtk_file_filter_get_name (filter);
5096 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
5098 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
5100 if (!g_slist_find (impl->filters, impl->current_filter))
5101 set_current_filter (impl, filter);
5103 show_filters (impl, TRUE);
5107 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
5108 GtkFileFilter *filter)
5110 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5111 GtkTreeModel *model;
5115 filter_index = g_slist_index (impl->filters, filter);
5117 if (filter_index < 0)
5119 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
5123 impl->filters = g_slist_remove (impl->filters, filter);
5125 if (filter == impl->current_filter)
5128 set_current_filter (impl, impl->filters->data);
5130 set_current_filter (impl, NULL);
5133 /* Remove row from the combo box */
5134 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
5135 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
5136 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
5138 g_object_unref (filter);
5141 show_filters (impl, FALSE);
5145 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
5147 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5149 return g_slist_copy (impl->filters);
5152 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
5154 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
5157 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
5161 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
5162 const GtkFilePath *path,
5165 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5169 /* Test validity of path here. */
5170 if (!check_is_folder (impl->file_system, path, error))
5173 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
5175 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
5178 impl->num_shortcuts++;
5180 if (impl->shortcuts_filter_model)
5181 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
5187 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
5188 const GtkFilePath *path,
5191 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5197 if (impl->num_shortcuts == 0)
5200 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5201 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5202 g_assert_not_reached ();
5204 for (i = 0; i < impl->num_shortcuts; i++)
5208 GtkFilePath *shortcut;
5210 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5211 SHORTCUTS_COL_DATA, &col_data,
5212 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5214 g_assert (col_data != NULL);
5215 g_assert (!is_volume);
5217 shortcut = col_data;
5218 if (gtk_file_path_compare (shortcut, path) == 0)
5220 shortcuts_remove_rows (impl, pos + i, 1);
5221 impl->num_shortcuts--;
5225 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5226 g_assert_not_reached ();
5231 uri = gtk_file_system_path_to_uri (impl->file_system, path);
5233 GTK_FILE_CHOOSER_ERROR,
5234 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
5235 _("Shortcut %s does not exist"),
5243 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
5245 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5251 if (impl->num_shortcuts == 0)
5254 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5255 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5256 g_assert_not_reached ();
5260 for (i = 0; i < impl->num_shortcuts; i++)
5264 GtkFilePath *shortcut;
5266 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5267 SHORTCUTS_COL_DATA, &col_data,
5268 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5270 g_assert (col_data != NULL);
5271 g_assert (!is_volume);
5273 shortcut = col_data;
5274 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
5276 if (i != impl->num_shortcuts - 1)
5278 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5279 g_assert_not_reached ();
5283 return g_slist_reverse (list);
5286 /* Guesses a size based upon font sizes */
5288 find_good_size_from_style (GtkWidget *widget,
5292 GtkFileChooserDefault *impl;
5293 gint default_width, default_height;
5296 GtkRequisition preview_req;
5298 g_assert (widget->style != NULL);
5299 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5301 font_size = pango_font_description_get_size (widget->style->font_desc);
5302 font_size = PANGO_PIXELS (font_size);
5304 default_width = font_size * NUM_CHARS;
5305 default_height = font_size * NUM_LINES;
5307 /* Use at least the requisition size not including the preview widget */
5308 gtk_widget_size_request (widget, &req);
5310 if (impl->preview_widget_active && impl->preview_widget)
5311 gtk_widget_size_request (impl->preview_box, &preview_req);
5313 preview_req.width = 0;
5315 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
5316 default_height = MAX (default_height, req.height);
5318 *width = default_width;
5319 *height = default_height;
5323 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
5324 gint *default_width,
5325 gint *default_height)
5327 GtkFileChooserDefault *impl;
5329 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5331 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
5333 if (impl->preview_widget_active && impl->preview_widget)
5334 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
5338 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
5339 gboolean *resize_horizontally,
5340 gboolean *resize_vertically)
5342 GtkFileChooserDefault *impl;
5344 g_return_if_fail (resize_horizontally != NULL);
5345 g_return_if_fail (resize_vertically != NULL);
5347 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5349 *resize_horizontally = TRUE;
5350 *resize_vertically = TRUE;
5352 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5353 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5355 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5357 *resize_horizontally = FALSE;
5358 *resize_vertically = FALSE;
5363 struct switch_folder_closure {
5364 GtkFileChooserDefault *impl;
5365 const GtkFilePath *path;
5369 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
5371 switch_folder_foreach_cb (GtkTreeModel *model,
5376 struct switch_folder_closure *closure;
5377 GtkTreeIter child_iter;
5381 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
5383 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
5384 closure->num_selected++;
5387 /* Changes to the selected folder in the list view */
5389 switch_to_selected_folder (GtkFileChooserDefault *impl)
5391 GtkTreeSelection *selection;
5392 struct switch_folder_closure closure;
5394 /* We do this with foreach() rather than get_selected() as we may be in
5395 * multiple selection mode
5398 closure.impl = impl;
5399 closure.path = NULL;
5400 closure.num_selected = 0;
5402 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5403 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
5405 g_assert (closure.path && closure.num_selected == 1);
5407 change_folder_and_display_error (impl, closure.path);
5410 /* Implementation for GtkFileChooserEmbed::should_respond() */
5412 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
5414 GtkFileChooserDefault *impl;
5415 GtkWidget *toplevel;
5416 GtkWidget *current_focus;
5418 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5420 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
5421 g_assert (GTK_IS_WINDOW (toplevel));
5423 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
5425 if (current_focus == impl->browse_files_tree_view)
5428 gboolean all_files, all_folders;
5432 selection_check (impl, &num_selected, &all_files, &all_folders);
5434 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5436 if (num_selected != 1)
5437 return TRUE; /* zero means current folder; more than one means use the whole selection */
5438 else if (current_focus != impl->browse_files_tree_view)
5440 /* a single folder is selected and a button was clicked */
5441 switch_to_selected_folder (impl);
5446 if (num_selected == 0)
5448 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5449 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5450 goto save_entry; /* it makes sense to use the typed name */
5455 if (num_selected == 1 && all_folders)
5457 switch_to_selected_folder (impl);
5463 else if (current_focus == impl->save_file_name_entry)
5466 gboolean is_valid, is_empty;
5469 GtkFileChooserEntry *entry;
5473 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5474 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5476 entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
5477 path = check_save_entry (impl, &is_valid, &is_empty);
5479 if (!is_empty && !is_valid)
5483 path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5485 is_folder = check_is_folder (impl->file_system, path, NULL);
5488 _gtk_file_chooser_entry_set_file_part (entry, "");
5489 change_folder_and_display_error (impl, path);
5494 /* check that everything up to the last component exists */
5495 gtk_file_path_free (path);
5496 path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5497 is_folder = check_is_folder (impl->file_system, path, NULL);
5500 change_folder_and_display_error (impl, path);
5507 gtk_file_path_free (path);
5510 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
5512 /* The focus is on a dialog's action area button, *and* the widget that
5513 * was focused immediately before it is the shortcuts list. Switch to the
5514 * selected shortcut and tell the caller not to respond.
5518 if (shortcuts_get_selected (impl, &iter))
5520 shortcuts_activate_iter (impl, &iter);
5522 gtk_widget_grab_focus (impl->browse_files_tree_view);
5529 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
5531 /* The focus is on a dialog's action area button, *and* the widget that
5532 * was focused immediately before it is the file list.
5537 /* The focus is on a dialog's action area button or something else */
5538 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5539 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5544 g_assert_not_reached ();
5548 /* Implementation for GtkFileChooserEmbed::initial_focus() */
5550 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
5552 GtkFileChooserDefault *impl;
5555 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5557 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5558 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5559 widget = impl->browse_files_tree_view;
5560 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5561 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5562 widget = impl->save_file_name_entry;
5565 g_assert_not_reached ();
5569 gtk_widget_grab_focus (widget);
5573 set_current_filter (GtkFileChooserDefault *impl,
5574 GtkFileFilter *filter)
5576 if (impl->current_filter != filter)
5580 /* NULL filters are allowed to reset to non-filtered status
5582 filter_index = g_slist_index (impl->filters, filter);
5583 if (impl->filters && filter && filter_index < 0)
5586 if (impl->current_filter)
5587 g_object_unref (impl->current_filter);
5588 impl->current_filter = filter;
5589 if (impl->current_filter)
5591 g_object_ref (impl->current_filter);
5592 gtk_object_sink (GTK_OBJECT (filter));
5596 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
5599 if (impl->browse_files_model)
5600 install_list_model_filter (impl);
5602 g_object_notify (G_OBJECT (impl), "filter");
5607 filter_combo_changed (GtkComboBox *combo_box,
5608 GtkFileChooserDefault *impl)
5610 gint new_index = gtk_combo_box_get_active (combo_box);
5611 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
5613 set_current_filter (impl, new_filter);
5617 check_preview_change (GtkFileChooserDefault *impl)
5619 GtkTreePath *cursor_path;
5620 const GtkFilePath *new_path;
5621 const GtkFileInfo *new_info;
5623 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
5624 if (cursor_path && impl->sort_model)
5627 GtkTreeIter child_iter;
5629 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
5630 gtk_tree_path_free (cursor_path);
5632 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5634 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5635 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5643 if (new_path != impl->preview_path &&
5644 !(new_path && impl->preview_path &&
5645 gtk_file_path_compare (new_path, impl->preview_path) == 0))
5647 if (impl->preview_path)
5649 gtk_file_path_free (impl->preview_path);
5650 g_free (impl->preview_display_name);
5655 impl->preview_path = gtk_file_path_copy (new_path);
5656 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
5660 impl->preview_path = NULL;
5661 impl->preview_display_name = NULL;
5664 if (impl->use_preview_label && impl->preview_label)
5665 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
5667 g_signal_emit_by_name (impl, "update-preview");
5671 /* Activates a volume by mounting it if necessary and then switching to its
5675 shortcuts_activate_volume (GtkFileChooserDefault *impl,
5676 GtkFileSystemVolume *volume)
5680 /* We ref the file chooser since volume_mount() may run a main loop, and the
5681 * user could close the file chooser window in the meantime.
5683 g_object_ref (impl);
5685 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
5690 set_busy_cursor (impl, TRUE);
5693 result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
5699 msg = g_strdup_printf (_("Could not mount %s"),
5700 gtk_file_system_volume_get_display_name (impl->file_system, volume));
5701 error_message (impl, msg, error->message);
5703 g_error_free (error);
5706 set_busy_cursor (impl, FALSE);
5712 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
5713 change_folder_and_display_error (impl, path);
5714 gtk_file_path_free (path);
5718 g_object_unref (impl);
5721 /* Opens the folder or volume at the specified iter in the shortcuts model */
5723 shortcuts_activate_iter (GtkFileChooserDefault *impl,
5729 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
5730 SHORTCUTS_COL_DATA, &col_data,
5731 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5735 return; /* We are on a separator */
5739 GtkFileSystemVolume *volume;
5743 shortcuts_activate_volume (impl, volume);
5747 const GtkFilePath *file_path;
5749 file_path = col_data;
5750 change_folder_and_display_error (impl, file_path);
5754 /* Callback used when a row in the shortcuts list is activated */
5756 shortcuts_row_activated_cb (GtkTreeView *tree_view,
5758 GtkTreeViewColumn *column,
5759 GtkFileChooserDefault *impl)
5762 GtkTreeIter child_iter;
5764 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
5767 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
5770 shortcuts_activate_iter (impl, &child_iter);
5772 gtk_widget_grab_focus (impl->browse_files_tree_view);
5775 /* Handler for GtkWidget::key-press-event on the shortcuts list */
5777 shortcuts_key_press_event_cb (GtkWidget *widget,
5779 GtkFileChooserDefault *impl)
5783 modifiers = gtk_accelerator_get_default_mod_mask ();
5785 if ((event->keyval == GDK_BackSpace
5786 || event->keyval == GDK_Delete
5787 || event->keyval == GDK_KP_Delete)
5788 && (event->state & modifiers) == 0)
5790 remove_selected_bookmarks (impl);
5798 shortcuts_select_func (GtkTreeSelection *selection,
5799 GtkTreeModel *model,
5801 gboolean path_currently_selected,
5804 GtkFileChooserDefault *impl = data;
5806 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
5810 list_select_func (GtkTreeSelection *selection,
5811 GtkTreeModel *model,
5813 gboolean path_currently_selected,
5816 GtkFileChooserDefault *impl = data;
5818 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5819 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5821 GtkTreeIter iter, child_iter;
5822 const GtkFileInfo *info;
5824 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5827 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5829 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5831 if (info && !gtk_file_info_get_is_folder (info))
5839 list_selection_changed (GtkTreeSelection *selection,
5840 GtkFileChooserDefault *impl)
5842 /* See if we are in the new folder editable row for Save mode */
5843 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5845 GtkTreeSelection *selection;
5846 GtkTreeIter iter, child_iter;
5847 const GtkFileInfo *info;
5849 g_assert (!impl->select_multiple);
5850 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5851 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
5854 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5858 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5860 return; /* We are on the editable row for New Folder */
5863 update_chooser_entry (impl);
5864 check_preview_change (impl);
5865 bookmarks_check_add_sensitivity (impl);
5867 g_signal_emit_by_name (impl, "selection-changed", 0);
5870 /* Callback used when a row in the file list is activated */
5872 list_row_activated (GtkTreeView *tree_view,
5874 GtkTreeViewColumn *column,
5875 GtkFileChooserDefault *impl)
5877 GtkTreeIter iter, child_iter;
5878 const GtkFileInfo *info;
5880 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5883 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5885 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5887 if (gtk_file_info_get_is_folder (info))
5889 const GtkFilePath *file_path;
5891 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5892 change_folder_and_display_error (impl, file_path);
5897 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5898 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5899 g_signal_emit_by_name (impl, "file-activated");
5903 path_bar_clicked (GtkPathBar *path_bar,
5904 GtkFilePath *file_path,
5905 gboolean child_is_hidden,
5906 GtkFileChooserDefault *impl)
5908 if (!change_folder_and_display_error (impl, file_path))
5911 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
5912 * show hidden files so that ".baz" appears in the file list, as it will still
5913 * be shown in the path bar: "/foo/[bar]/.baz"
5915 if (child_is_hidden)
5916 g_object_set (impl, "show-hidden", TRUE, NULL);
5919 static const GtkFileInfo *
5920 get_list_file_info (GtkFileChooserDefault *impl,
5923 GtkTreeIter child_iter;
5925 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5929 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5933 list_icon_data_func (GtkTreeViewColumn *tree_column,
5934 GtkCellRenderer *cell,
5935 GtkTreeModel *tree_model,
5939 GtkFileChooserDefault *impl = data;
5940 GtkTreeIter child_iter;
5941 const GtkFilePath *path;
5943 const GtkFileInfo *info;
5944 gboolean sensitive = TRUE;
5946 info = get_list_file_info (impl, iter);
5948 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5951 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5955 /* FIXME: NULL GError */
5956 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
5957 impl->icon_size, NULL);
5961 /* We are on the editable row */
5965 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5966 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
5967 sensitive = gtk_file_info_get_is_folder (info);
5971 "sensitive", sensitive,
5975 g_object_unref (pixbuf);
5979 list_name_data_func (GtkTreeViewColumn *tree_column,
5980 GtkCellRenderer *cell,
5981 GtkTreeModel *tree_model,
5985 GtkFileChooserDefault *impl = data;
5986 const GtkFileInfo *info = get_list_file_info (impl, iter);
5987 gboolean sensitive = TRUE;
5992 "text", _("Type name of new folder"),
5999 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6000 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6002 sensitive = gtk_file_info_get_is_folder (info);
6006 "text", gtk_file_info_get_display_name (info),
6007 "sensitive", sensitive,
6013 list_size_data_func (GtkTreeViewColumn *tree_column,
6014 GtkCellRenderer *cell,
6015 GtkTreeModel *tree_model,
6019 GtkFileChooserDefault *impl = data;
6020 const GtkFileInfo *info = get_list_file_info (impl, iter);
6023 gboolean sensitive = TRUE;
6025 if (!info || gtk_file_info_get_is_folder (info))
6027 g_object_set (cell,"sensitive", sensitive, NULL);
6031 size = gtk_file_info_get_size (info);
6033 if (size < (gint64)1024)
6034 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
6035 else if (size < (gint64)1024*1024)
6036 str = g_strdup_printf (_("%.1f K"), size / (1024.));
6037 else if (size < (gint64)1024*1024*1024)
6038 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
6040 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
6042 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6043 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6048 "sensitive", sensitive,
6055 /* Tree column data callback for the file list; fetches the mtime of a file */
6057 list_mtime_data_func (GtkTreeViewColumn *tree_column,
6058 GtkCellRenderer *cell,
6059 GtkTreeModel *tree_model,
6063 GtkFileChooserDefault *impl;
6064 const GtkFileInfo *info;
6065 GtkFileTime time_mtime, time_now;
6069 gboolean sensitive = TRUE;
6073 info = get_list_file_info (impl, iter);
6083 time_mtime = gtk_file_info_get_modification_time (info);
6084 g_date_set_time (&mtime, (GTime) time_mtime);
6086 time_now = (GTime ) time (NULL);
6087 g_date_set_time (&now, (GTime) time_now);
6089 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6092 strcpy (buf, _("Today"));
6093 else if (days_diff == 1)
6094 strcpy (buf, _("Yesterday"));
6099 if (days_diff > 1 && days_diff < 7)
6100 format = "%A"; /* Days from last week */
6102 format = "%x"; /* Any other date */
6104 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
6105 strcpy (buf, _("Unknown"));
6108 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6109 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6110 sensitive = gtk_file_info_get_is_folder (info);
6114 "sensitive", sensitive,
6119 _gtk_file_chooser_default_new (const char *file_system)
6121 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
6122 "file-system-backend", file_system,
6127 location_entry_create (GtkFileChooserDefault *impl,
6132 entry = _gtk_file_chooser_entry_new (TRUE);
6133 /* Pick a good width for the entry */
6134 gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
6135 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
6136 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
6137 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (entry), impl->action);
6140 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry),
6141 gtk_file_path_new_steal ((gchar *)path));
6142 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), path);
6146 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
6147 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6148 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6149 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), "");
6150 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6151 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6152 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry),
6153 gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
6155 g_assert_not_reached ();
6158 return GTK_WIDGET (entry);
6162 update_from_entry (GtkFileChooserDefault *impl,
6164 GtkFileChooserEntry *chooser_entry)
6166 const GtkFilePath *folder_path;
6167 const char *file_part;
6169 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6170 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6172 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
6174 error_message_with_parent (parent,
6175 _("Cannot change folder"),
6176 _("The folder you specified is an invalid path."));
6180 if (file_part[0] == '\0')
6181 return change_folder_and_display_error (impl, folder_path);
6184 GtkFileFolder *folder = NULL;
6185 GtkFilePath *subfolder_path = NULL;
6186 GtkFileInfo *info = NULL;
6192 /* If the file part is non-empty, we need to figure out if it refers to a
6193 * folder within folder. We could optimize the case here where the folder
6194 * is already loaded for one of our tree models.
6198 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
6202 error_getting_info_dialog (impl, folder_path, error);
6207 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
6209 if (!subfolder_path)
6214 uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
6215 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s'"),
6217 error_message (impl, msg, error->message);
6224 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
6228 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6229 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6231 if (!change_folder_and_display_error (impl, folder_path))
6234 gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
6237 error_getting_info_dialog (impl, subfolder_path, error);
6242 if (gtk_file_info_get_is_folder (info))
6243 result = change_folder_and_display_error (impl, subfolder_path);
6249 result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
6251 error_dialog (impl, _("Could not select item"),
6252 subfolder_path, error);
6258 g_object_unref (folder);
6260 gtk_file_path_free (subfolder_path);
6263 gtk_file_info_free (info);
6268 g_assert_not_reached ();
6272 location_popup_handler (GtkFileChooserDefault *impl,
6276 GtkWindow *toplevel;
6282 const char *accept_stock;
6286 toplevel = get_toplevel (GTK_WIDGET (impl));
6288 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6289 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6291 title = _("Open Location");
6292 accept_stock = GTK_STOCK_OPEN;
6296 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6297 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6298 title = _("Save in Location");
6299 accept_stock = GTK_STOCK_SAVE;
6302 dialog = gtk_dialog_new_with_buttons (title,
6304 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
6305 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
6306 accept_stock, GTK_RESPONSE_ACCEPT,
6308 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
6309 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
6310 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
6311 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
6313 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
6314 GTK_RESPONSE_ACCEPT,
6315 GTK_RESPONSE_CANCEL,
6318 hbox = gtk_hbox_new (FALSE, 12);
6319 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
6320 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
6322 label = gtk_label_new_with_mnemonic (_("_Location:"));
6323 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
6325 entry = location_entry_create (impl, path);
6327 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
6328 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
6332 gtk_widget_show_all (dialog);
6333 /* If the dialog is brought up by typing the first characters
6334 * of a path, unselect the text in the entry, so that you can
6335 * just type on without erasing the initial part.
6338 gtk_editable_select_region (GTK_EDITABLE (entry), -1, -1);
6342 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
6344 if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
6346 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6347 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6349 gtk_widget_grab_focus (impl->browse_files_tree_view);
6353 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6354 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6355 gtk_widget_grab_focus (impl->save_file_name_entry);
6363 GtkWindow *toplevel;
6365 toplevel = get_toplevel (GTK_WIDGET (impl));
6366 if (toplevel && toplevel->focus_widget)
6367 gtk_widget_grab_focus (toplevel->focus_widget);
6370 gtk_widget_destroy (dialog);
6373 /* Handler for the "up-folder" keybinding signal */
6375 up_folder_handler (GtkFileChooserDefault *impl)
6377 pending_select_paths_add (impl, impl->current_folder);
6378 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
6381 /* Handler for the "down-folder" keybinding signal */
6383 down_folder_handler (GtkFileChooserDefault *impl)
6385 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
6388 /* Handler for the "home-folder" keybinding signal */
6390 home_folder_handler (GtkFileChooserDefault *impl)
6395 if (!impl->has_home)
6396 return; /* Should we put up an error dialog? */
6398 pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
6399 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
6400 g_assert_not_reached ();
6402 shortcuts_activate_iter (impl, &iter);
6407 /* Drag and drop interfaces */
6410 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
6415 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
6420 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
6422 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
6425 ShortcutsModelFilter *model;
6429 model = SHORTCUTS_MODEL_FILTER (drag_source);
6431 pos = *gtk_tree_path_get_indices (path);
6432 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
6434 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
6437 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
6439 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
6441 GtkSelectionData *selection_data)
6443 ShortcutsModelFilter *model;
6445 model = SHORTCUTS_MODEL_FILTER (drag_source);
6452 /* Fill the GtkTreeDragSourceIface vtable */
6454 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
6456 iface->row_draggable = shortcuts_model_filter_row_draggable;
6457 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
6461 /* Fill the GtkTreeDragDestIface vtable */
6463 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
6465 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
6466 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
6470 static GtkTreeModel *
6471 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
6472 GtkTreeModel *child_model,
6475 ShortcutsModelFilter *model;
6477 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
6478 "child_model", child_model,
6479 "virtual_root", root,
6484 return GTK_TREE_MODEL (model);