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"
23 #include "gtkalignment.h"
24 #include "gtkbindings.h"
25 #include "gtkbutton.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrendererpixbuf.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkcombobox.h"
33 #include "gtkeventbox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserdefault.h"
36 #include "gtkfilechooserembed.h"
37 #include "gtkfilechooserentry.h"
38 #include "gtkfilechooserutils.h"
39 #include "gtkfilechooser.h"
40 #include "gtkfilesystemmodel.h"
43 #include "gtkhpaned.h"
44 #include "gtkiconfactory.h"
45 #include "gtkicontheme.h"
47 #include "gtkimagemenuitem.h"
50 #include "gtkmarshalers.h"
51 #include "gtkmenuitem.h"
52 #include "gtkmessagedialog.h"
53 #include "gtkpathbar.h"
54 #include "gtkprivate.h"
55 #include "gtkscrolledwindow.h"
56 #include "gtkseparatormenuitem.h"
57 #include "gtksizegroup.h"
60 #include "gtktreednd.h"
61 #include "gtktreeprivate.h"
62 #include "gtktreeview.h"
63 #include "gtktreemodelsort.h"
64 #include "gtktreeselection.h"
65 #include "gtktreestore.h"
66 #include "gtktooltips.h"
67 #include "gtktypebuiltins.h"
70 #if defined (G_OS_UNIX)
71 #include "gtkfilesystemunix.h"
72 #elif defined (G_OS_WIN32)
73 #include "gtkfilesystemwin32.h"
82 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
84 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
85 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
86 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
89 LOAD_EMPTY, /* There is no model */
90 LOAD_PRELOAD, /* Model is loading and a timer is running; model isn't inserted into the tree yet */
91 LOAD_LOADING, /* Timeout expired, model is inserted into the tree, but not fully loaded yet */
92 LOAD_FINISHED /* Model is fully loaded and inserted into the tree */
95 #define MAX_LOADING_TIME 500
97 struct _GtkFileChooserDefaultClass
99 GtkVBoxClass parent_class;
102 struct _GtkFileChooserDefault
104 GtkVBox parent_instance;
106 GtkFileChooserAction action;
108 GtkFileSystem *file_system;
110 /* Save mode widgets */
111 GtkWidget *save_widgets;
113 GtkWidget *save_file_name_entry;
114 GtkWidget *save_folder_label;
115 GtkWidget *save_folder_combo;
116 GtkWidget *save_expander;
118 /* The file browsing widgets */
119 GtkWidget *browse_widgets;
120 GtkWidget *browse_shortcuts_tree_view;
121 GtkWidget *browse_shortcuts_add_button;
122 GtkWidget *browse_shortcuts_remove_button;
123 GtkWidget *browse_shortcuts_popup_menu;
124 GtkWidget *browse_shortcuts_popup_menu_remove_item;
125 GtkWidget *browse_shortcuts_popup_menu_rename_item;
126 GtkWidget *browse_files_tree_view;
127 GtkWidget *browse_files_popup_menu;
128 GtkWidget *browse_files_popup_menu_add_shortcut_item;
129 GtkWidget *browse_files_popup_menu_hidden_files_item;
130 GtkWidget *browse_new_folder_button;
131 GtkWidget *browse_path_bar;
133 GtkFileSystemModel *browse_files_model;
135 GtkWidget *filter_combo_hbox;
136 GtkWidget *filter_combo;
137 GtkWidget *preview_box;
138 GtkWidget *preview_label;
139 GtkWidget *preview_widget;
140 GtkWidget *extra_align;
141 GtkWidget *extra_widget;
143 GtkListStore *shortcuts_model;
144 GtkTreeModel *shortcuts_filter_model;
146 GtkTreeModelSort *sort_model;
148 LoadState load_state;
149 guint load_timeout_id;
151 GSList *pending_select_paths;
153 GtkFileFilter *current_filter;
156 GtkTooltips *tooltips;
159 gboolean has_desktop;
165 gulong volumes_changed_id;
166 gulong bookmarks_changed_id;
168 GtkFilePath *current_volume_path;
169 GtkFilePath *current_folder;
170 GtkFilePath *preview_path;
171 char *preview_display_name;
173 GtkTreeViewColumn *list_name_column;
174 GtkCellRenderer *list_name_renderer;
176 GSource *edited_idle;
177 char *edited_new_text;
179 gulong settings_signal_id;
182 gulong toplevel_set_focus_id;
183 GtkWidget *toplevel_last_focus_widget;
186 GdkDragContext *shortcuts_drag_context;
187 GSource *shortcuts_drag_outside_idle;
192 guint local_only : 1;
193 guint preview_widget_active : 1;
194 guint use_preview_label : 1;
195 guint select_multiple : 1;
196 guint show_hidden : 1;
197 guint list_sort_ascending : 1;
198 guint changing_folder : 1;
199 guint shortcuts_current_folder_active : 1;
202 guint shortcuts_drag_outside : 1;
215 static guint signals[LAST_SIGNAL] = { 0 };
217 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
219 SHORTCUTS_COL_PIXBUF,
222 SHORTCUTS_COL_IS_VOLUME,
223 SHORTCUTS_COL_REMOVABLE,
224 SHORTCUTS_COL_PIXBUF_VISIBLE,
225 SHORTCUTS_COL_NUM_COLUMNS
228 /* Column numbers for the file list */
233 FILE_LIST_COL_NUM_COLUMNS
236 /* Identifiers for target types */
242 /* Target types for dragging from the shortcuts list */
243 static const GtkTargetEntry shortcuts_source_targets[] = {
244 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
247 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
248 / sizeof (shortcuts_source_targets[0]));
250 /* Target types for dropping into the shortcuts list */
251 static const GtkTargetEntry shortcuts_dest_targets[] = {
252 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
253 { "text/uri-list", 0, TEXT_URI_LIST }
256 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
257 / sizeof (shortcuts_dest_targets[0]));
259 /* Target types for DnD from the file list */
260 static const GtkTargetEntry file_list_source_targets[] = {
261 { "text/uri-list", 0, TEXT_URI_LIST }
264 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
265 / sizeof (file_list_source_targets[0]));
267 /* Interesting places in the shortcuts bar */
273 SHORTCUTS_BOOKMARKS_SEPARATOR,
275 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
276 SHORTCUTS_CURRENT_FOLDER
279 /* Icon size for if we can't get it from the theme */
280 #define FALLBACK_ICON_SIZE 16
282 #define PREVIEW_HBOX_SPACING 12
286 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
287 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
288 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
289 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
291 static GObject* gtk_file_chooser_default_constructor (GType type,
292 guint n_construct_properties,
293 GObjectConstructParam *construct_params);
294 static void gtk_file_chooser_default_finalize (GObject *object);
295 static void gtk_file_chooser_default_set_property (GObject *object,
299 static void gtk_file_chooser_default_get_property (GObject *object,
303 static void gtk_file_chooser_default_dispose (GObject *object);
304 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
305 static void gtk_file_chooser_default_map (GtkWidget *widget);
306 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
307 GtkWidget *previous_toplevel);
308 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
309 GtkStyle *previous_style);
310 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
311 GdkScreen *previous_screen);
313 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
314 const GtkFilePath *path,
316 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
317 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
319 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
320 const GtkFilePath *path,
322 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
323 const GtkFilePath *path);
324 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
325 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
326 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
327 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
328 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
329 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
330 GtkFileFilter *filter);
331 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
332 GtkFileFilter *filter);
333 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
334 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
335 const GtkFilePath *path,
337 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
338 const GtkFilePath *path,
340 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
342 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
344 gint *default_height);
345 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
346 gboolean *resize_horizontally,
347 gboolean *resize_vertically);
348 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
349 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
351 static void location_popup_handler (GtkFileChooserDefault *impl,
353 static void up_folder_handler (GtkFileChooserDefault *impl);
354 static void down_folder_handler (GtkFileChooserDefault *impl);
355 static void home_folder_handler (GtkFileChooserDefault *impl);
356 static void update_appearance (GtkFileChooserDefault *impl);
358 static void set_current_filter (GtkFileChooserDefault *impl,
359 GtkFileFilter *filter);
360 static void check_preview_change (GtkFileChooserDefault *impl);
362 static void filter_combo_changed (GtkComboBox *combo_box,
363 GtkFileChooserDefault *impl);
364 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
366 GtkTreeViewColumn *column,
367 GtkFileChooserDefault *impl);
369 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
371 GtkFileChooserDefault *impl);
373 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
376 gboolean path_currently_selected,
378 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
380 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
382 static int shortcuts_get_index (GtkFileChooserDefault *impl,
383 ShortcutsIndex where);
384 static int shortcut_find_position (GtkFileChooserDefault *impl,
385 const GtkFilePath *path);
387 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
389 static gboolean list_select_func (GtkTreeSelection *selection,
392 gboolean path_currently_selected,
395 static void list_selection_changed (GtkTreeSelection *tree_selection,
396 GtkFileChooserDefault *impl);
397 static void list_row_activated (GtkTreeView *tree_view,
399 GtkTreeViewColumn *column,
400 GtkFileChooserDefault *impl);
402 static void select_func (GtkFileSystemModel *model,
407 static void path_bar_clicked (GtkPathBar *path_bar,
408 GtkFilePath *file_path,
409 gboolean child_is_hidden,
410 GtkFileChooserDefault *impl);
412 static void add_bookmark_button_clicked_cb (GtkButton *button,
413 GtkFileChooserDefault *impl);
414 static void remove_bookmark_button_clicked_cb (GtkButton *button,
415 GtkFileChooserDefault *impl);
417 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
418 GtkCellRenderer *cell,
419 GtkTreeModel *tree_model,
422 static void list_name_data_func (GtkTreeViewColumn *tree_column,
423 GtkCellRenderer *cell,
424 GtkTreeModel *tree_model,
428 static void list_size_data_func (GtkTreeViewColumn *tree_column,
429 GtkCellRenderer *cell,
430 GtkTreeModel *tree_model,
434 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
435 GtkCellRenderer *cell,
436 GtkTreeModel *tree_model,
440 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
443 static void load_remove_timer (GtkFileChooserDefault *impl);
445 static GObjectClass *parent_class;
449 /* Drag and drop interface declarations */
452 GtkTreeModelFilter parent;
454 GtkFileChooserDefault *impl;
455 } ShortcutsModelFilter;
458 GtkTreeModelFilterClass parent_class;
459 } ShortcutsModelFilterClass;
461 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
462 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
464 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
466 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
467 _shortcuts_model_filter,
468 GTK_TYPE_TREE_MODEL_FILTER,
469 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
470 shortcuts_model_filter_drag_source_iface_init));
472 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
473 GtkTreeModel *child_model,
479 _gtk_file_chooser_default_get_type (void)
481 static GType file_chooser_default_type = 0;
483 if (!file_chooser_default_type)
485 static const GTypeInfo file_chooser_default_info =
487 sizeof (GtkFileChooserDefaultClass),
488 NULL, /* base_init */
489 NULL, /* base_finalize */
490 (GClassInitFunc) gtk_file_chooser_default_class_init,
491 NULL, /* class_finalize */
492 NULL, /* class_data */
493 sizeof (GtkFileChooserDefault),
495 (GInstanceInitFunc) gtk_file_chooser_default_init,
498 static const GInterfaceInfo file_chooser_info =
500 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
501 NULL, /* interface_finalize */
502 NULL /* interface_data */
505 static const GInterfaceInfo file_chooser_embed_info =
507 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
508 NULL, /* interface_finalize */
509 NULL /* interface_data */
512 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
513 &file_chooser_default_info, 0);
515 g_type_add_interface_static (file_chooser_default_type,
516 GTK_TYPE_FILE_CHOOSER,
518 g_type_add_interface_static (file_chooser_default_type,
519 GTK_TYPE_FILE_CHOOSER_EMBED,
520 &file_chooser_embed_info);
523 return file_chooser_default_type;
527 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
529 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
530 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
531 GtkBindingSet *binding_set;
533 parent_class = g_type_class_peek_parent (class);
535 gobject_class->finalize = gtk_file_chooser_default_finalize;
536 gobject_class->constructor = gtk_file_chooser_default_constructor;
537 gobject_class->set_property = gtk_file_chooser_default_set_property;
538 gobject_class->get_property = gtk_file_chooser_default_get_property;
539 gobject_class->dispose = gtk_file_chooser_default_dispose;
541 widget_class->show_all = gtk_file_chooser_default_show_all;
542 widget_class->map = gtk_file_chooser_default_map;
543 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
544 widget_class->style_set = gtk_file_chooser_default_style_set;
545 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
547 signals[LOCATION_POPUP] =
548 _gtk_binding_signal_new ("location-popup",
549 G_OBJECT_CLASS_TYPE (class),
550 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
551 G_CALLBACK (location_popup_handler),
553 _gtk_marshal_VOID__STRING,
554 G_TYPE_NONE, 1, G_TYPE_STRING);
556 _gtk_binding_signal_new ("up-folder",
557 G_OBJECT_CLASS_TYPE (class),
558 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
559 G_CALLBACK (up_folder_handler),
561 _gtk_marshal_VOID__VOID,
563 signals[DOWN_FOLDER] =
564 _gtk_binding_signal_new ("down-folder",
565 G_OBJECT_CLASS_TYPE (class),
566 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
567 G_CALLBACK (down_folder_handler),
569 _gtk_marshal_VOID__VOID,
571 signals[HOME_FOLDER] =
572 _gtk_binding_signal_new ("home-folder",
573 G_OBJECT_CLASS_TYPE (class),
574 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
575 G_CALLBACK (home_folder_handler),
577 _gtk_marshal_VOID__VOID,
580 binding_set = gtk_binding_set_by_class (class);
582 gtk_binding_entry_add_signal (binding_set,
583 GDK_l, GDK_CONTROL_MASK,
585 1, G_TYPE_STRING, "");
587 gtk_binding_entry_add_signal (binding_set,
590 1, G_TYPE_STRING, "/");
592 gtk_binding_entry_add_signal (binding_set,
593 GDK_Up, GDK_MOD1_MASK,
596 gtk_binding_entry_add_signal (binding_set,
600 gtk_binding_entry_add_signal (binding_set,
601 GDK_KP_Up, GDK_MOD1_MASK,
605 gtk_binding_entry_add_signal (binding_set,
606 GDK_Down, GDK_MOD1_MASK,
609 gtk_binding_entry_add_signal (binding_set,
610 GDK_KP_Down, GDK_MOD1_MASK,
614 gtk_binding_entry_add_signal (binding_set,
615 GDK_Home, GDK_MOD1_MASK,
618 gtk_binding_entry_add_signal (binding_set,
619 GDK_KP_Home, GDK_MOD1_MASK,
623 _gtk_file_chooser_install_properties (gobject_class);
625 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
626 P_("Default file chooser backend"),
627 P_("Name of the GtkFileChooser backend to use by default"),
629 GTK_PARAM_READWRITE));
633 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
635 iface->select_path = gtk_file_chooser_default_select_path;
636 iface->unselect_path = gtk_file_chooser_default_unselect_path;
637 iface->select_all = gtk_file_chooser_default_select_all;
638 iface->unselect_all = gtk_file_chooser_default_unselect_all;
639 iface->get_paths = gtk_file_chooser_default_get_paths;
640 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
641 iface->get_file_system = gtk_file_chooser_default_get_file_system;
642 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
643 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
644 iface->set_current_name = gtk_file_chooser_default_set_current_name;
645 iface->add_filter = gtk_file_chooser_default_add_filter;
646 iface->remove_filter = gtk_file_chooser_default_remove_filter;
647 iface->list_filters = gtk_file_chooser_default_list_filters;
648 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
649 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
650 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
654 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
656 iface->get_default_size = gtk_file_chooser_default_get_default_size;
657 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
658 iface->should_respond = gtk_file_chooser_default_should_respond;
659 iface->initial_focus = gtk_file_chooser_default_initial_focus;
662 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
664 impl->local_only = TRUE;
665 impl->preview_widget_active = TRUE;
666 impl->use_preview_label = TRUE;
667 impl->select_multiple = FALSE;
668 impl->show_hidden = FALSE;
669 impl->icon_size = FALLBACK_ICON_SIZE;
670 impl->load_state = LOAD_EMPTY;
671 impl->pending_select_paths = NULL;
673 gtk_box_set_spacing (GTK_BOX (impl), 12);
675 impl->tooltips = gtk_tooltips_new ();
676 g_object_ref (impl->tooltips);
677 gtk_object_sink (GTK_OBJECT (impl->tooltips));
680 /* Frees the data columns for the specified iter in the shortcuts model*/
682 shortcuts_free_row_data (GtkFileChooserDefault *impl,
688 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
689 SHORTCUTS_COL_DATA, &col_data,
690 SHORTCUTS_COL_IS_VOLUME, &is_volume,
697 GtkFileSystemVolume *volume;
700 gtk_file_system_volume_free (impl->file_system, volume);
707 gtk_file_path_free (path);
711 /* Frees all the data columns in the shortcuts model */
713 shortcuts_free (GtkFileChooserDefault *impl)
717 if (!impl->shortcuts_model)
720 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
723 shortcuts_free_row_data (impl, &iter);
725 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
727 g_object_unref (impl->shortcuts_model);
728 impl->shortcuts_model = NULL;
732 pending_select_paths_free (GtkFileChooserDefault *impl)
736 for (l = impl->pending_select_paths; l; l = l->next)
741 gtk_file_path_free (path);
744 g_slist_free (impl->pending_select_paths);
745 impl->pending_select_paths = NULL;
749 pending_select_paths_add (GtkFileChooserDefault *impl,
750 const GtkFilePath *path)
752 impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
755 /* Used from gtk_tree_selection_selected_foreach() */
757 store_selection_foreach (GtkTreeModel *model,
762 GtkFileChooserDefault *impl;
763 GtkTreeIter child_iter;
764 const GtkFilePath *file_path;
766 impl = GTK_FILE_CHOOSER_DEFAULT (data);
768 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
770 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
771 pending_select_paths_add (impl, file_path);
774 /* Stores the current selection in the list of paths to select; this is used to
775 * preserve the selection when reloading the current folder.
778 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
780 GtkTreeSelection *selection;
782 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
783 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
787 gtk_file_chooser_default_finalize (GObject *object)
789 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
792 if (impl->shortcuts_filter_model)
793 g_object_unref (impl->shortcuts_filter_model);
795 shortcuts_free (impl);
797 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
798 impl->volumes_changed_id = 0;
799 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
800 impl->bookmarks_changed_id = 0;
801 g_object_unref (impl->file_system);
803 for (l = impl->filters; l; l = l->next)
805 GtkFileFilter *filter;
807 filter = GTK_FILE_FILTER (l->data);
808 g_object_unref (filter);
810 g_slist_free (impl->filters);
812 if (impl->current_filter)
813 g_object_unref (impl->current_filter);
815 if (impl->current_volume_path)
816 gtk_file_path_free (impl->current_volume_path);
818 if (impl->current_folder)
819 gtk_file_path_free (impl->current_folder);
821 if (impl->preview_path)
822 gtk_file_path_free (impl->preview_path);
824 pending_select_paths_free (impl);
826 load_remove_timer (impl);
828 /* Free all the Models we have */
829 if (impl->browse_files_model)
830 g_object_unref (impl->browse_files_model);
832 if (impl->sort_model)
833 g_object_unref (impl->sort_model);
835 g_free (impl->preview_display_name);
837 g_free (impl->edited_new_text);
839 g_object_unref (impl->tooltips);
841 G_OBJECT_CLASS (parent_class)->finalize (object);
844 /* Shows an error dialog set as transient for the specified window */
846 error_message_with_parent (GtkWindow *parent,
852 dialog = gtk_message_dialog_new (parent,
853 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
858 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
860 gtk_dialog_run (GTK_DIALOG (dialog));
861 gtk_widget_destroy (dialog);
864 /* Returns a toplevel GtkWindow, or NULL if none */
866 get_toplevel (GtkWidget *widget)
870 toplevel = gtk_widget_get_toplevel (widget);
871 if (!GTK_WIDGET_TOPLEVEL (toplevel))
874 return GTK_WINDOW (toplevel);
877 /* Shows an error dialog for the file chooser */
879 error_message (GtkFileChooserDefault *impl,
883 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
886 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
888 error_dialog (GtkFileChooserDefault *impl,
890 const GtkFilePath *path,
899 uri = gtk_file_system_path_to_uri (impl->file_system, path);
900 text = g_strdup_printf (msg, uri);
901 error_message (impl, text, error->message);
904 g_error_free (error);
908 /* Displays an error message about not being able to get information for a file.
909 * Frees the GError as well.
912 error_getting_info_dialog (GtkFileChooserDefault *impl,
913 const GtkFilePath *path,
917 _("Could not retrieve information about the file"),
921 /* Shows an error dialog about not being able to add a bookmark */
923 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
924 const GtkFilePath *path,
928 _("Could not add a bookmark"),
932 /* Shows an error dialog about not being able to remove a bookmark */
934 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
935 const GtkFilePath *path,
939 _("Could not remove bookmark"),
943 /* Shows an error dialog about not being able to create a folder */
945 error_creating_folder_dialog (GtkFileChooserDefault *impl,
946 const GtkFilePath *path,
950 _("The folder could not be created"),
954 /* Shows an error about not being able to create a folder because a file with
955 * the same name is already there.
958 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
959 const GtkFilePath *path,
963 _("The folder could not be created, as a file with the same name "
964 "already exists. Try using a different name for the folder, "
965 "or rename the file first."),
969 /* Shows an error dialog about not being able to create a filename */
971 error_building_filename_dialog (GtkFileChooserDefault *impl,
972 const GtkFilePath *folder_part,
973 const char *file_part,
976 error_dialog (impl, _("Invalid file name"),
980 /* Shows an error dialog when we cannot switch to a folder */
982 error_changing_folder_dialog (GtkFileChooserDefault *impl,
983 const GtkFilePath *path,
986 error_dialog (impl, _("The folder contents could not be displayed"),
990 /* Changes folders, displaying an error dialog if this fails */
992 change_folder_and_display_error (GtkFileChooserDefault *impl,
993 const GtkFilePath *path)
997 GtkFilePath *path_copy;
999 /* We copy the path because of this case:
1001 * list_row_activated()
1002 * fetches path from model; path belongs to the model (*)
1003 * calls change_folder_and_display_error()
1004 * calls _gtk_file_chooser_set_current_folder_path()
1005 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1008 path_copy = gtk_file_path_copy (path);
1011 result = _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path_copy, &error);
1014 error_changing_folder_dialog (impl, path_copy, error);
1016 gtk_file_path_free (path_copy);
1022 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1024 if (impl->use_preview_label)
1026 if (!impl->preview_label)
1028 impl->preview_label = gtk_label_new (impl->preview_display_name);
1029 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1030 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1031 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1032 gtk_widget_show (impl->preview_label);
1037 if (impl->preview_label)
1039 gtk_widget_destroy (impl->preview_label);
1040 impl->preview_label = NULL;
1044 if (impl->preview_widget_active && impl->preview_widget)
1045 gtk_widget_show (impl->preview_box);
1047 gtk_widget_hide (impl->preview_box);
1049 g_signal_emit_by_name (impl, "default-size-changed");
1053 set_preview_widget (GtkFileChooserDefault *impl,
1054 GtkWidget *preview_widget)
1056 if (preview_widget == impl->preview_widget)
1059 if (impl->preview_widget)
1060 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1061 impl->preview_widget);
1063 impl->preview_widget = preview_widget;
1064 if (impl->preview_widget)
1066 gtk_widget_show (impl->preview_widget);
1067 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1068 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1069 impl->preview_widget,
1070 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1073 update_preview_widget_visibility (impl);
1076 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1078 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1082 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1088 gboolean pixbuf_visible;
1091 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1092 SHORTCUTS_COL_DATA, &data,
1093 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1094 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1097 if (pixbuf_visible && data)
1101 GtkFileSystemVolume *volume;
1104 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1105 impl->icon_size, NULL);
1109 const GtkFilePath *path;
1112 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1113 impl->icon_size, NULL);
1116 gtk_list_store_set (impl->shortcuts_model, &iter,
1117 SHORTCUTS_COL_PIXBUF, pixbuf,
1120 g_object_unref (pixbuf);
1122 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1126 shortcuts_find_folder (GtkFileChooserDefault *impl,
1127 GtkFilePath *folder)
1129 GtkTreeSelection *selection;
1133 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1135 g_assert (folder != NULL);
1136 pos = shortcut_find_position (impl, folder);
1139 gtk_tree_selection_unselect_all (selection);
1143 path = gtk_tree_path_new_from_indices (pos, -1);
1144 gtk_tree_selection_select_path (selection, path);
1145 gtk_tree_path_free (path);
1148 /* If a shortcut corresponds to the current folder, selects it */
1150 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1152 shortcuts_find_folder (impl, impl->current_folder);
1155 /* Convenience function to get the display name and icon info for a path */
1156 static GtkFileInfo *
1157 get_file_info (GtkFileSystem *file_system,
1158 const GtkFilePath *path,
1162 GtkFilePath *parent_path;
1163 GtkFileFolder *parent_folder;
1170 if (!gtk_file_system_get_parent (file_system, path, &parent_path, &tmp))
1173 parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
1174 GTK_FILE_INFO_DISPLAY_NAME
1175 | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
1180 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, &tmp);
1181 g_object_unref (parent_folder);
1185 gtk_file_path_free (parent_path);
1190 GTK_FILE_CHOOSER_ERROR,
1191 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
1192 _("Could not get information about '%s': %s"),
1193 gtk_file_path_get_string (path),
1201 /* Returns whether a path is a folder */
1203 check_is_folder (GtkFileSystem *file_system,
1204 const GtkFilePath *path,
1207 GtkFileFolder *folder;
1209 folder = gtk_file_system_get_folder (file_system, path, 0, error);
1213 g_object_unref (folder);
1217 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1218 * inserts a volume. A position of -1 indicates the end of the tree.
1221 shortcuts_insert_path (GtkFileChooserDefault *impl,
1224 GtkFileSystemVolume *volume,
1225 const GtkFilePath *path,
1238 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1239 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1240 impl->icon_size, NULL);
1244 if (!check_is_folder (impl->file_system, path, error))
1248 label_copy = g_strdup (label);
1251 GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
1256 label_copy = g_strdup (gtk_file_info_get_display_name (info));
1257 gtk_file_info_free (info);
1260 data = gtk_file_path_copy (path);
1261 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1262 impl->icon_size, NULL);
1266 gtk_list_store_append (impl->shortcuts_model, &iter);
1268 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1270 gtk_list_store_set (impl->shortcuts_model, &iter,
1271 SHORTCUTS_COL_PIXBUF, pixbuf,
1272 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1273 SHORTCUTS_COL_NAME, label_copy,
1274 SHORTCUTS_COL_DATA, data,
1275 SHORTCUTS_COL_IS_VOLUME, is_volume,
1276 SHORTCUTS_COL_REMOVABLE, removable,
1279 g_free (label_copy);
1282 g_object_unref (pixbuf);
1287 /* Appends an item for the user's home directory to the shortcuts model */
1289 shortcuts_append_home (GtkFileChooserDefault *impl)
1292 GtkFilePath *home_path;
1295 home = g_get_home_dir ();
1299 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1302 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1303 if (!impl->has_home)
1304 error_getting_info_dialog (impl, home_path, error);
1306 gtk_file_path_free (home_path);
1309 /* Appends the ~/Desktop directory to the shortcuts model */
1311 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1317 name = _gtk_file_system_win32_get_desktop ();
1319 const char *home = g_get_home_dir ();
1323 name = g_build_filename (home, "Desktop", NULL);
1326 path = gtk_file_system_filename_to_path (impl->file_system, name);
1329 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1330 /* We do not actually pop up an error dialog if there is no desktop directory
1331 * because some people may really not want to have one.
1334 gtk_file_path_free (path);
1337 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1339 shortcuts_append_paths (GtkFileChooserDefault *impl,
1346 /* As there is no separator now, we want to start there.
1348 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1351 for (; paths; paths = paths->next)
1359 if (impl->local_only &&
1360 !gtk_file_system_path_is_local (impl->file_system, path))
1363 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
1365 /* NULL GError, but we don't really want to show error boxes here */
1366 if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, label, TRUE, NULL))
1372 return num_inserted;
1375 /* Returns the index for the corresponding item in the shortcuts bar */
1377 shortcuts_get_index (GtkFileChooserDefault *impl,
1378 ShortcutsIndex where)
1384 if (where == SHORTCUTS_HOME)
1387 n += impl->has_home ? 1 : 0;
1389 if (where == SHORTCUTS_DESKTOP)
1392 n += impl->has_desktop ? 1 : 0;
1394 if (where == SHORTCUTS_VOLUMES)
1397 n += impl->num_volumes;
1399 if (where == SHORTCUTS_SHORTCUTS)
1402 n += impl->num_shortcuts;
1404 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1407 /* If there are no bookmarks there won't be a separator */
1408 n += (impl->num_bookmarks > 0) ? 1 : 0;
1410 if (where == SHORTCUTS_BOOKMARKS)
1413 n += impl->num_bookmarks;
1415 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1420 if (where == SHORTCUTS_CURRENT_FOLDER)
1423 g_assert_not_reached ();
1430 /* Removes the specified number of rows from the shortcuts list */
1432 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1438 path = gtk_tree_path_new_from_indices (start_row, -1);
1440 for (; n_rows; n_rows--)
1444 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1445 g_assert_not_reached ();
1447 shortcuts_free_row_data (impl, &iter);
1448 gtk_list_store_remove (impl->shortcuts_model, &iter);
1451 gtk_tree_path_free (path);
1454 /* Adds all the file system volumes to the shortcuts model */
1456 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1461 gboolean old_changing_folders;
1463 old_changing_folders = impl->changing_folder;
1464 impl->changing_folder = TRUE;
1466 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1467 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1468 impl->num_volumes = 0;
1470 list = gtk_file_system_list_volumes (impl->file_system);
1474 for (l = list; l; l = l->next)
1476 GtkFileSystemVolume *volume;
1480 if (impl->local_only)
1482 GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1483 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1484 gtk_file_path_free (base_path);
1488 gtk_file_system_volume_free (impl->file_system, volume);
1493 if (shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL))
1496 gtk_file_system_volume_free (impl->file_system, volume);
1499 impl->num_volumes = n;
1500 g_slist_free (list);
1502 if (impl->shortcuts_filter_model)
1503 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1505 impl->changing_folder = old_changing_folders;
1508 /* Inserts a separator node in the shortcuts list */
1510 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1511 ShortcutsIndex where)
1515 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1517 gtk_list_store_insert (impl->shortcuts_model, &iter,
1518 shortcuts_get_index (impl, where));
1519 gtk_list_store_set (impl->shortcuts_model, &iter,
1520 SHORTCUTS_COL_PIXBUF, NULL,
1521 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1522 SHORTCUTS_COL_NAME, NULL,
1523 SHORTCUTS_COL_DATA, NULL,
1527 /* Updates the list of bookmarks */
1529 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1532 gboolean old_changing_folders;
1534 GtkFilePath *list_selected = NULL;
1535 GtkFilePath *combo_selected = NULL;
1539 old_changing_folders = impl->changing_folder;
1540 impl->changing_folder = TRUE;
1542 if (shortcuts_get_selected (impl, &iter))
1544 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1546 SHORTCUTS_COL_DATA, &col_data,
1547 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1550 if (col_data && !is_volume)
1551 list_selected = gtk_file_path_copy (col_data);
1554 if (impl->save_folder_combo &&
1555 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
1558 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1560 SHORTCUTS_COL_DATA, &col_data,
1561 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1564 if (col_data && !is_volume)
1565 combo_selected = gtk_file_path_copy (col_data);
1568 if (impl->num_bookmarks > 0)
1569 shortcuts_remove_rows (impl,
1570 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1571 impl->num_bookmarks + 1);
1573 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1574 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1575 gtk_file_paths_free (bookmarks);
1577 if (impl->num_bookmarks > 0)
1578 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1580 if (impl->shortcuts_filter_model)
1581 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1585 shortcuts_find_folder (impl, list_selected);
1586 gtk_file_path_free (list_selected);
1593 pos = shortcut_find_position (impl, combo_selected);
1595 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
1597 gtk_file_path_free (combo_selected);
1600 impl->changing_folder = old_changing_folders;
1603 /* Appends a separator and a row to the shortcuts list for the current folder */
1605 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1610 g_assert (!impl->shortcuts_current_folder_active);
1614 g_assert (impl->current_folder != NULL);
1616 pos = shortcut_find_position (impl, impl->current_folder);
1619 GtkFileSystemVolume *volume;
1620 GtkFilePath *base_path;
1624 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1628 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1630 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1632 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1637 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1639 success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1644 success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1647 gtk_file_system_volume_free (impl->file_system, volume);
1650 gtk_file_path_free (base_path);
1653 shortcuts_remove_rows (impl, pos - 1, 1); /* remove the separator */
1655 impl->shortcuts_current_folder_active = success;
1659 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1662 /* Updates the current folder row in the shortcuts model */
1664 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1668 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1670 if (impl->shortcuts_current_folder_active)
1672 shortcuts_remove_rows (impl, pos, 2);
1673 impl->shortcuts_current_folder_active = FALSE;
1676 shortcuts_add_current_folder (impl);
1679 /* Filter function used for the shortcuts filter model */
1681 shortcuts_filter_cb (GtkTreeModel *model,
1685 GtkFileChooserDefault *impl;
1689 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1691 path = gtk_tree_model_get_path (model, iter);
1695 pos = *gtk_tree_path_get_indices (path);
1696 gtk_tree_path_free (path);
1698 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1701 /* Creates the list model for shortcuts */
1703 shortcuts_model_create (GtkFileChooserDefault *impl)
1705 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1706 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1707 GDK_TYPE_PIXBUF, /* pixbuf */
1708 G_TYPE_STRING, /* name */
1709 G_TYPE_POINTER, /* path or volume */
1710 G_TYPE_BOOLEAN, /* is the previous column a volume? */
1711 G_TYPE_BOOLEAN, /* removable */
1712 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1714 if (impl->file_system)
1716 shortcuts_append_home (impl);
1717 shortcuts_append_desktop (impl);
1718 shortcuts_add_volumes (impl);
1719 shortcuts_add_bookmarks (impl);
1722 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1723 GTK_TREE_MODEL (impl->shortcuts_model),
1726 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1727 shortcuts_filter_cb,
1732 /* Callback used when the "New Folder" button is clicked */
1734 new_folder_button_clicked (GtkButton *button,
1735 GtkFileChooserDefault *impl)
1740 if (!impl->browse_files_model)
1741 return; /* FIXME: this sucks. Disable the New Folder button or something. */
1743 /* Prevent button from being clicked twice */
1744 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
1746 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1748 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1749 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1750 path, impl->list_name_column,
1753 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1754 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1756 impl->list_name_column,
1759 gtk_tree_path_free (path);
1762 /* Idle handler for creating a new folder after editing its name cell, or for
1763 * canceling the editing.
1766 edited_idle_cb (GtkFileChooserDefault *impl)
1768 GDK_THREADS_ENTER ();
1770 g_source_destroy (impl->edited_idle);
1771 impl->edited_idle = NULL;
1773 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1774 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1776 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
1778 if (impl->edited_new_text) /* not cancelled? */
1781 GtkFilePath *file_path;
1784 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, impl->edited_new_text,
1789 if (gtk_file_system_create_folder (impl->file_system, file_path, &error))
1790 change_folder_and_display_error (impl, file_path);
1792 error_creating_folder_dialog (impl, file_path, error);
1794 gtk_file_path_free (file_path);
1797 error_creating_folder_dialog (impl, file_path, error);
1799 g_free (impl->edited_new_text);
1800 impl->edited_new_text = NULL;
1803 GDK_THREADS_LEAVE ();
1809 queue_edited_idle (GtkFileChooserDefault *impl,
1810 const gchar *new_text)
1812 /* We create the folder in an idle handler so that we don't modify the tree
1816 g_assert (!impl->edited_idle);
1817 g_assert (!impl->edited_new_text);
1819 impl->edited_idle = g_idle_source_new ();
1820 g_source_set_closure (impl->edited_idle,
1821 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
1823 g_source_attach (impl->edited_idle, NULL);
1826 impl->edited_new_text = g_strdup (new_text);
1829 /* Callback used from the text cell renderer when the new folder is named */
1831 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1833 const gchar *new_text,
1834 GtkFileChooserDefault *impl)
1836 /* work around bug #154921 */
1837 g_object_set (cell_renderer_text,
1838 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1839 queue_edited_idle (impl, new_text);
1842 /* Callback used from the text cell renderer when the new folder edition gets
1846 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1847 GtkFileChooserDefault *impl)
1849 /* work around bug #154921 */
1850 g_object_set (cell_renderer_text,
1851 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1852 queue_edited_idle (impl, NULL);
1855 /* Creates the widgets for the filter combo box */
1857 filter_create (GtkFileChooserDefault *impl)
1859 impl->filter_combo = gtk_combo_box_new_text ();
1860 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
1862 g_signal_connect (impl->filter_combo, "changed",
1863 G_CALLBACK (filter_combo_changed), impl);
1865 return impl->filter_combo;
1869 button_new (GtkFileChooserDefault *impl,
1871 const char *stock_id,
1879 button = gtk_button_new_with_mnemonic (text);
1880 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1881 gtk_button_set_image (GTK_BUTTON (button), GTK_IMAGE (image));
1883 gtk_widget_set_sensitive (button, sensitive);
1884 g_signal_connect (button, "clicked", callback, impl);
1887 gtk_widget_show (button);
1892 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1894 shortcut_find_position (GtkFileChooserDefault *impl,
1895 const GtkFilePath *path)
1899 int current_folder_separator_idx;
1901 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1904 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1906 for (i = 0; i < current_folder_separator_idx; i++)
1911 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1912 SHORTCUTS_COL_DATA, &col_data,
1913 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1920 GtkFileSystemVolume *volume;
1921 GtkFilePath *base_path;
1925 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1927 exists = strcmp (gtk_file_path_get_string (path),
1928 gtk_file_path_get_string (base_path)) == 0;
1936 GtkFilePath *model_path;
1938 model_path = col_data;
1940 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1945 gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1951 /* Tries to add a bookmark from a path name */
1953 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1954 const GtkFilePath *path,
1959 if (shortcut_find_position (impl, path) != -1)
1962 /* FIXME: this check really belongs in gtk_file_system_insert_bookmark. */
1964 if (!check_is_folder (impl->file_system, path, &error))
1966 error_adding_bookmark_dialog (impl, path, error);
1971 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
1973 error_adding_bookmark_dialog (impl, path, error);
1981 add_bookmark_foreach_cb (GtkTreeModel *model,
1986 GtkFileChooserDefault *impl;
1987 GtkFileSystemModel *fs_model;
1988 GtkTreeIter child_iter;
1989 const GtkFilePath *file_path;
1991 impl = (GtkFileChooserDefault *) data;
1993 fs_model = impl->browse_files_model;
1994 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1996 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
1997 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2000 /* Adds a bookmark from the currently selected item in the file list */
2002 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2004 GtkTreeSelection *selection;
2006 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2008 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2009 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2011 gtk_tree_selection_selected_foreach (selection,
2012 add_bookmark_foreach_cb,
2016 /* Callback used when the "Add bookmark" button is clicked */
2018 add_bookmark_button_clicked_cb (GtkButton *button,
2019 GtkFileChooserDefault *impl)
2021 bookmarks_add_selected_folder (impl);
2024 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2025 * returns FALSE if no shortcut is selected.
2028 shortcuts_get_selected (GtkFileChooserDefault *impl,
2031 GtkTreeSelection *selection;
2032 GtkTreeIter parent_iter;
2034 if (!impl->browse_shortcuts_tree_view)
2037 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2039 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2042 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2048 /* Removes the selected bookmarks */
2050 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2058 if (!shortcuts_get_selected (impl, &iter))
2061 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2062 SHORTCUTS_COL_DATA, &col_data,
2063 SHORTCUTS_COL_REMOVABLE, &removable,
2065 g_assert (col_data != NULL);
2073 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2074 error_removing_bookmark_dialog (impl, path, error);
2077 /* Callback used when the "Remove bookmark" button is clicked */
2079 remove_bookmark_button_clicked_cb (GtkButton *button,
2080 GtkFileChooserDefault *impl)
2082 remove_selected_bookmarks (impl);
2085 struct selection_check_closure {
2086 GtkFileChooserDefault *impl;
2089 gboolean all_folders;
2092 /* Used from gtk_tree_selection_selected_foreach() */
2094 selection_check_foreach_cb (GtkTreeModel *model,
2099 struct selection_check_closure *closure;
2100 GtkTreeIter child_iter;
2101 const GtkFileInfo *info;
2105 closure->num_selected++;
2107 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2109 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2110 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2112 closure->all_folders = closure->all_folders && is_folder;
2113 closure->all_files = closure->all_files && !is_folder;
2116 /* Checks whether the selected items in the file list are all files or all folders */
2118 selection_check (GtkFileChooserDefault *impl,
2120 gboolean *all_files,
2121 gboolean *all_folders)
2123 struct selection_check_closure closure;
2124 GtkTreeSelection *selection;
2126 closure.impl = impl;
2127 closure.num_selected = 0;
2128 closure.all_files = TRUE;
2129 closure.all_folders = TRUE;
2131 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2132 gtk_tree_selection_selected_foreach (selection,
2133 selection_check_foreach_cb,
2136 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2139 *num_selected = closure.num_selected;
2142 *all_files = closure.all_files;
2145 *all_folders = closure.all_folders;
2148 struct get_selected_path_closure {
2149 GtkFileChooserDefault *impl;
2150 const GtkFilePath *path;
2154 get_selected_path_foreach_cb (GtkTreeModel *model,
2159 struct get_selected_path_closure *closure;
2160 GtkTreeIter child_iter;
2164 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2165 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2168 /* Returns a selected path from the file list */
2169 static const GtkFilePath *
2170 get_selected_path (GtkFileChooserDefault *impl)
2172 struct get_selected_path_closure closure;
2173 GtkTreeSelection *selection;
2175 closure.impl = impl;
2176 closure.path = NULL;
2178 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2179 gtk_tree_selection_selected_foreach (selection,
2180 get_selected_path_foreach_cb,
2183 return closure.path;
2187 GtkFileChooserDefault *impl;
2189 } UpdateTooltipData;
2192 update_tooltip (GtkTreeModel *model,
2197 UpdateTooltipData *udata = data;
2198 GtkTreeIter child_iter;
2199 const GtkFileInfo *info;
2201 if (udata->tip == NULL)
2203 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2207 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2208 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2209 gtk_file_info_get_display_name (info));
2214 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2215 * if there are no selected items *and* the current folder is not in the
2216 * bookmarks list. De-sensitize the button otherwise.
2219 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2222 gboolean all_folders;
2226 selection_check (impl, &num_selected, NULL, &all_folders);
2228 if (num_selected == 0)
2229 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2230 else if (num_selected == 1)
2232 const GtkFilePath *path;
2234 path = get_selected_path (impl);
2235 active = all_folders && (shortcut_find_position (impl, path) == -1);
2238 active = all_folders;
2240 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2242 if (impl->browse_files_popup_menu_add_shortcut_item)
2243 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2244 (num_selected == 0) ? FALSE : active);
2248 if (num_selected == 0)
2249 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2250 else if (num_selected > 1)
2251 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2254 GtkTreeSelection *selection;
2255 UpdateTooltipData data;
2257 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2260 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2264 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2269 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2270 * bookmark row is selected in the shortcuts tree.
2273 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2276 gboolean removable = FALSE;
2279 if (shortcuts_get_selected (impl, &iter))
2280 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2281 SHORTCUTS_COL_REMOVABLE, &removable,
2282 SHORTCUTS_COL_NAME, &name,
2285 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2291 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2292 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2301 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2304 gboolean removable = FALSE;
2306 if (impl->browse_shortcuts_popup_menu == NULL)
2309 if (shortcuts_get_selected (impl, &iter))
2310 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2311 SHORTCUTS_COL_REMOVABLE, &removable,
2314 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2315 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2318 /* GtkWidget::drag-begin handler for the shortcuts list. */
2320 shortcuts_drag_begin_cb (GtkWidget *widget,
2321 GdkDragContext *context,
2322 GtkFileChooserDefault *impl)
2325 impl->shortcuts_drag_context = g_object_ref (context);
2330 /* Removes the idle handler for outside drags */
2332 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2334 if (!impl->shortcuts_drag_outside_idle)
2337 g_source_destroy (impl->shortcuts_drag_outside_idle);
2338 impl->shortcuts_drag_outside_idle = NULL;
2342 /* GtkWidget::drag-end handler for the shortcuts list. */
2344 shortcuts_drag_end_cb (GtkWidget *widget,
2345 GdkDragContext *context,
2346 GtkFileChooserDefault *impl)
2349 g_object_unref (impl->shortcuts_drag_context);
2351 shortcuts_cancel_drag_outside_idle (impl);
2353 if (!impl->shortcuts_drag_outside)
2356 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2358 impl->shortcuts_drag_outside = FALSE;
2362 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2364 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2365 GdkDragContext *context,
2366 GtkFileChooserDefault *impl)
2368 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2372 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2376 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2379 GtkTreeView *tree_view;
2382 GdkPixmap *row_pixmap;
2387 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2389 /* Find the selected path and get its drag pixmap */
2391 if (!shortcuts_get_selected (impl, &iter))
2392 g_assert_not_reached ();
2394 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2396 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2397 gtk_tree_path_free (path);
2406 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2412 GdkPixmap *composite;
2413 int row_pixmap_width, row_pixmap_height;
2414 int pixbuf_width, pixbuf_height;
2415 int composite_width, composite_height;
2416 int pixbuf_x, pixbuf_y;
2417 GdkGC *gc, *mask_gc;
2419 GdkBitmap *pixbuf_mask;
2421 /* Create pixmap and mask for composite image */
2423 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2424 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2425 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2427 composite_width = MAX (row_pixmap_width, pixbuf_width);
2428 composite_height = MAX (row_pixmap_height, pixbuf_height);
2430 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2432 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2435 pixbuf_x = composite_width - pixbuf_width;
2437 pixbuf_y = (composite_height - pixbuf_height) / 2;
2439 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2440 gc = gdk_gc_new (composite);
2442 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2443 mask_gc = gdk_gc_new (mask);
2445 gdk_gc_set_foreground (mask_gc, &color);
2446 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2449 color.green = 0xffff;
2450 color.blue = 0xffff;
2451 gdk_gc_set_rgb_fg_color (gc, &color);
2452 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2454 /* Composite the row pixmap and the pixbuf */
2456 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2458 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2459 NULL, &pixbuf_mask, 128);
2460 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2463 pixbuf_width, pixbuf_height);
2464 g_object_unref (pixbuf_mask);
2466 gdk_draw_drawable (composite, gc, row_pixmap,
2469 row_pixmap_width, row_pixmap_height);
2471 gdk_gc_set_foreground (mask_gc, &color);
2472 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2474 gdk_draw_pixbuf (composite, gc, pixbuf,
2477 pixbuf_width, pixbuf_height,
2481 g_object_unref (pixbuf);
2482 g_object_unref (row_pixmap);
2484 row_pixmap = composite;
2488 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2490 gtk_tree_view_get_path_at_pos (tree_view,
2491 tree_view->priv->press_start_x,
2492 tree_view->priv->press_start_y,
2498 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2499 gdk_drawable_get_colormap (row_pixmap),
2502 tree_view->priv->press_start_x + 1,
2503 row_pixmap_y + cell_y + 1);
2505 g_object_unref (row_pixmap);
2507 g_object_unref (mask);
2510 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2511 * handler so that we can tell apart the drag_leave event that comes right
2512 * before a drag_drop, from a normal drag_leave. We don't want to set the
2513 * cursor nor the flag in the latter case.
2516 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2518 GDK_THREADS_ENTER ();
2520 shortcuts_drag_set_delete_cursor (impl, TRUE);
2521 impl->shortcuts_drag_outside = TRUE;
2523 shortcuts_cancel_drag_outside_idle (impl);
2525 GDK_THREADS_LEAVE ();
2531 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2535 shortcuts_drag_leave_cb (GtkWidget *widget,
2536 GdkDragContext *context,
2538 GtkFileChooserDefault *impl)
2541 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2543 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2544 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2545 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2547 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2551 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2553 GTK_TREE_VIEW_DROP_BEFORE);
2555 g_signal_stop_emission_by_name (widget, "drag-leave");
2558 /* Computes the appropriate row and position for dropping */
2560 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2564 GtkTreeViewDropPosition *pos)
2566 GtkTreeView *tree_view;
2567 GtkTreeViewColumn *column;
2571 int bookmarks_index;
2573 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2575 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2577 if (!gtk_tree_view_get_path_at_pos (tree_view,
2579 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2585 row = bookmarks_index + impl->num_bookmarks - 1;
2586 *path = gtk_tree_path_new_from_indices (row, -1);
2587 *pos = GTK_TREE_VIEW_DROP_AFTER;
2591 row = *gtk_tree_path_get_indices (*path);
2592 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2593 gtk_tree_path_free (*path);
2595 if (row < bookmarks_index)
2597 row = bookmarks_index;
2598 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2600 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2602 row = bookmarks_index + impl->num_bookmarks - 1;
2603 *pos = GTK_TREE_VIEW_DROP_AFTER;
2607 if (cell_y < cell.height / 2)
2608 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2610 *pos = GTK_TREE_VIEW_DROP_AFTER;
2613 *path = gtk_tree_path_new_from_indices (row, -1);
2616 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2617 * implement the destination side of DnD by hand, due to limitations in
2618 * GtkTreeView's DnD API.
2621 shortcuts_drag_motion_cb (GtkWidget *widget,
2622 GdkDragContext *context,
2626 GtkFileChooserDefault *impl)
2629 GtkTreeViewDropPosition pos;
2630 GdkDragAction action;
2633 if (gtk_drag_get_source_widget (context) == widget)
2635 shortcuts_cancel_drag_outside_idle (impl);
2637 if (impl->shortcuts_drag_outside)
2639 shortcuts_drag_set_delete_cursor (impl, FALSE);
2640 impl->shortcuts_drag_outside = FALSE;
2645 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2646 action = GDK_ACTION_COPY;
2647 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2648 action = GDK_ACTION_MOVE;
2655 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2656 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2657 gtk_tree_path_free (path);
2661 g_signal_stop_emission_by_name (widget, "drag-motion");
2665 gdk_drag_status (context, action, time_);
2672 /* GtkWidget::drag-drop handler for the shortcuts list. */
2674 shortcuts_drag_drop_cb (GtkWidget *widget,
2675 GdkDragContext *context,
2679 GtkFileChooserDefault *impl)
2682 shortcuts_cancel_drag_outside_idle (impl);
2685 g_signal_stop_emission_by_name (widget, "drag-drop");
2689 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2691 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2698 uris = g_uri_list_extract_uris (data);
2700 for (i = 0; uris[i]; i++)
2706 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2710 if (shortcuts_add_bookmark_from_path (impl, path, position))
2713 gtk_file_path_free (path);
2719 g_set_error (&error,
2720 GTK_FILE_CHOOSER_ERROR,
2721 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
2722 _("Could not add a bookmark for '%s' "
2723 "because it is an invalid path name."),
2725 error_adding_bookmark_dialog (impl, path, error);
2732 /* Reorders the selected bookmark to the specified position */
2734 shortcuts_reorder (GtkFileChooserDefault *impl,
2742 int bookmarks_index;
2743 const GtkFilePath *file_path;
2744 GtkFilePath *file_path_copy;
2747 /* Get the selected path */
2749 if (!shortcuts_get_selected (impl, &iter))
2750 g_assert_not_reached ();
2752 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2753 old_position = *gtk_tree_path_get_indices (path);
2754 gtk_tree_path_free (path);
2756 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2757 old_position -= bookmarks_index;
2758 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2760 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2761 SHORTCUTS_COL_DATA, &col_data,
2762 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2764 g_assert (col_data != NULL);
2765 g_assert (!is_volume);
2767 file_path = col_data;
2768 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2770 /* Remove the path from the old position and insert it in the new one */
2772 if (new_position > old_position)
2775 if (old_position == new_position)
2779 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2780 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2782 error_adding_bookmark_dialog (impl, file_path_copy, error);
2786 gtk_file_path_free (file_path_copy);
2789 /* Callback used when we get the drag data for the bookmarks list. We add the
2790 * received URIs as bookmarks if they are folders.
2793 shortcuts_drag_data_received_cb (GtkWidget *widget,
2794 GdkDragContext *context,
2797 GtkSelectionData *selection_data,
2802 GtkFileChooserDefault *impl;
2803 GtkTreePath *tree_path;
2804 GtkTreeViewDropPosition tree_pos;
2806 int bookmarks_index;
2808 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2810 /* Compute position */
2812 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2814 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2815 position = *gtk_tree_path_get_indices (tree_path);
2816 gtk_tree_path_free (tree_path);
2818 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2821 g_assert (position >= bookmarks_index);
2822 position -= bookmarks_index;
2824 if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2825 shortcuts_drop_uris (impl, selection_data->data, position);
2826 else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2827 shortcuts_reorder (impl, position);
2829 g_signal_stop_emission_by_name (widget, "drag-data-received");
2832 /* Callback used when the selection in the shortcuts tree changes */
2834 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
2835 GtkFileChooserDefault *impl)
2837 bookmarks_check_remove_sensitivity (impl);
2838 shortcuts_check_popup_sensitivity (impl);
2842 shortcuts_row_separator_func (GtkTreeModel *model,
2846 gint column = GPOINTER_TO_INT (data);
2849 gtk_tree_model_get (model, iter, column, &text, -1);
2859 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
2860 * keypresses before the TreeView gets them.
2863 tree_view_keybinding_cb (GtkWidget *tree_view,
2865 GtkFileChooserDefault *impl)
2867 if (event->keyval == GDK_slash &&
2868 ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
2870 location_popup_handler (impl, "/");
2877 /* Callback used when the file list's popup menu is detached */
2879 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
2882 GtkFileChooserDefault *impl;
2884 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
2885 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
2887 impl->browse_shortcuts_popup_menu = NULL;
2888 impl->browse_shortcuts_popup_menu_remove_item = NULL;
2889 impl->browse_shortcuts_popup_menu_rename_item = NULL;
2893 remove_shortcut_cb (GtkMenuItem *item,
2894 GtkFileChooserDefault *impl)
2896 remove_selected_bookmarks (impl);
2900 rename_shortcut_cb (GtkMenuItem *item,
2901 GtkFileChooserDefault *impl)
2905 GtkTreeViewColumn *column;
2906 GtkCellRenderer *cell;
2909 if (shortcuts_get_selected (impl, &iter))
2911 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2912 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
2913 renderers = gtk_tree_view_column_get_cell_renderers (column);
2914 cell = g_list_nth_data (renderers, 1);
2915 g_list_free (renderers);
2916 g_object_set (cell, "editable", TRUE, NULL);
2917 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2918 path, column, cell, TRUE);
2919 gtk_tree_path_free (path);
2923 /* Constructs the popup menu for the file list if needed */
2925 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
2929 if (impl->browse_shortcuts_popup_menu)
2932 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
2933 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
2934 impl->browse_shortcuts_tree_view,
2935 shortcuts_popup_menu_detach_cb);
2937 item = gtk_image_menu_item_new_with_label (_("Remove"));
2938 impl->browse_shortcuts_popup_menu_remove_item = item;
2939 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2940 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
2941 g_signal_connect (item, "activate",
2942 G_CALLBACK (remove_shortcut_cb), impl);
2943 gtk_widget_show (item);
2944 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
2946 item = gtk_menu_item_new_with_label (_("Rename..."));
2947 impl->browse_shortcuts_popup_menu_rename_item = item;
2948 g_signal_connect (item, "activate",
2949 G_CALLBACK (rename_shortcut_cb), impl);
2950 gtk_widget_show (item);
2951 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
2953 shortcuts_check_popup_sensitivity (impl);
2957 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
2959 shortcuts_build_popup_menu (impl);
2963 popup_position_func (GtkMenu *menu,
2967 gpointer user_data);
2970 shortcuts_popup_menu (GtkFileChooserDefault *impl,
2971 GdkEventButton *event)
2973 shortcuts_update_popup_menu (impl);
2975 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
2976 NULL, NULL, NULL, NULL,
2977 event->button, event->time);
2980 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
2982 popup_position_func, impl->browse_shortcuts_tree_view,
2983 0, GDK_CURRENT_TIME);
2984 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
2989 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
2991 shortcuts_popup_menu_cb (GtkWidget *widget,
2992 GtkFileChooserDefault *impl)
2994 shortcuts_popup_menu (impl, NULL);
2998 /* Callback used when a button is pressed on the shortcuts list.
2999 * We trap button 3 to bring up a popup menu.
3002 shortcuts_button_press_event_cb (GtkWidget *widget,
3003 GdkEventButton *event,
3004 GtkFileChooserDefault *impl)
3006 if (event->button != 3)
3009 shortcuts_popup_menu (impl, event);
3014 shortcuts_edited (GtkCellRenderer *cell,
3017 GtkFileChooserDefault *impl)
3021 GtkFilePath *shortcut;
3023 g_object_set (cell, "editable", FALSE, NULL);
3025 path = gtk_tree_path_new_from_string (path_string);
3026 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path);
3027 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3028 SHORTCUTS_COL_DATA, &shortcut,
3030 gtk_tree_path_free (path);
3032 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3036 shortcuts_editing_canceled (GtkCellRenderer *cell,
3037 GtkFileChooserDefault *impl)
3039 g_object_set (cell, "editable", FALSE, NULL);
3042 /* Creates the widgets for the shortcuts and bookmarks tree */
3044 shortcuts_list_create (GtkFileChooserDefault *impl)
3047 GtkTreeSelection *selection;
3048 GtkTreeViewColumn *column;
3049 GtkCellRenderer *renderer;
3051 /* Scrolled window */
3053 swin = gtk_scrolled_window_new (NULL, NULL);
3054 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3055 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3056 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3058 gtk_widget_show (swin);
3062 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3063 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3064 G_CALLBACK (tree_view_keybinding_cb), impl);
3065 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3066 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3067 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3068 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3069 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Shortcuts"));
3070 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3072 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
3074 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3076 shortcuts_source_targets,
3077 num_shortcuts_source_targets,
3080 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3081 GTK_DEST_DEFAULT_ALL,
3082 shortcuts_dest_targets,
3083 num_shortcuts_dest_targets,
3084 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3086 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3087 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
3088 gtk_tree_selection_set_select_function (selection,
3089 shortcuts_select_func,
3092 g_signal_connect (selection, "changed",
3093 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3095 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
3096 G_CALLBACK (shortcuts_row_activated_cb), impl);
3098 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3099 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3101 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3102 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3103 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3104 G_CALLBACK (shortcuts_drag_end_cb), impl);
3105 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3106 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3108 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3109 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3110 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3111 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3112 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3113 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3114 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3115 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3117 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3118 gtk_widget_show (impl->browse_shortcuts_tree_view);
3122 column = gtk_tree_view_column_new ();
3123 gtk_tree_view_column_set_title (column, _("Folder"));
3125 renderer = gtk_cell_renderer_pixbuf_new ();
3126 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3127 gtk_tree_view_column_set_attributes (column, renderer,
3128 "pixbuf", SHORTCUTS_COL_PIXBUF,
3129 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3132 renderer = gtk_cell_renderer_text_new ();
3133 g_signal_connect (renderer, "edited",
3134 G_CALLBACK (shortcuts_edited), impl);
3135 g_signal_connect (renderer, "editing-canceled",
3136 G_CALLBACK (shortcuts_editing_canceled), impl);
3137 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3138 gtk_tree_view_column_set_attributes (column, renderer,
3139 "text", SHORTCUTS_COL_NAME,
3142 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3143 shortcuts_row_separator_func,
3144 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3147 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3152 /* Creates the widgets for the shortcuts/bookmarks pane */
3154 shortcuts_pane_create (GtkFileChooserDefault *impl,
3155 GtkSizeGroup *size_group)
3161 vbox = gtk_vbox_new (FALSE, 6);
3162 gtk_widget_show (vbox);
3164 /* Shortcuts tree */
3166 widget = shortcuts_list_create (impl);
3167 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3169 /* Box for buttons */
3171 hbox = gtk_hbox_new (TRUE, 6);
3172 gtk_size_group_add_widget (size_group, hbox);
3173 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3174 gtk_widget_show (hbox);
3176 /* Add bookmark button */
3178 impl->browse_shortcuts_add_button = button_new (impl,
3183 G_CALLBACK (add_bookmark_button_clicked_cb));
3184 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3185 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
3186 _("Add the selected folder to the Bookmarks"), NULL);
3188 /* Remove bookmark button */
3190 impl->browse_shortcuts_remove_button = button_new (impl,
3195 G_CALLBACK (remove_bookmark_button_clicked_cb));
3196 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3197 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
3198 _("Remove the selected bookmark"), NULL);
3203 /* Handles key press events on the file list, so that we can trap Enter to
3204 * activate the default button on our own. Also, checks to see if '/' has been
3205 * pressed. See comment by tree_view_keybinding_cb() for more details.
3208 trap_activate_cb (GtkWidget *widget,
3212 GtkFileChooserDefault *impl;
3215 impl = (GtkFileChooserDefault *) data;
3217 modifiers = gtk_accelerator_get_default_mod_mask ();
3219 if (event->keyval == GDK_slash &&
3220 ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3222 location_popup_handler (impl, "/");
3226 if ((event->keyval == GDK_Return
3227 || event->keyval == GDK_ISO_Enter
3228 || event->keyval == GDK_KP_Enter
3229 || event->keyval == GDK_space)
3230 && ((event->state && modifiers) == 0)
3231 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3232 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3236 window = get_toplevel (widget);
3238 && widget != window->default_widget
3239 && !(widget == window->focus_widget &&
3240 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3242 gtk_window_activate_default (window);
3250 /* Callback used when the file list's popup menu is detached */
3252 popup_menu_detach_cb (GtkWidget *attach_widget,
3255 GtkFileChooserDefault *impl;
3257 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3258 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3260 impl->browse_files_popup_menu = NULL;
3261 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3262 impl->browse_files_popup_menu_hidden_files_item = NULL;
3265 /* Callback used when the "Add to Bookmarks" menu item is activated */
3267 add_to_shortcuts_cb (GtkMenuItem *item,
3268 GtkFileChooserDefault *impl)
3270 bookmarks_add_selected_folder (impl);
3273 /* Callback used when the "Open Location" menu item is activated */
3275 open_location_cb (GtkMenuItem *item,
3276 GtkFileChooserDefault *impl)
3278 location_popup_handler (impl, "");
3281 /* Callback used when the "Show Hidden Files" menu item is toggled */
3283 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3284 GtkFileChooserDefault *impl)
3287 "show-hidden", gtk_check_menu_item_get_active (item),
3291 /* Constructs the popup menu for the file list if needed */
3293 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3297 if (impl->browse_files_popup_menu)
3300 impl->browse_files_popup_menu = gtk_menu_new ();
3301 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3302 impl->browse_files_tree_view,
3303 popup_menu_detach_cb);
3305 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
3306 impl->browse_files_popup_menu_add_shortcut_item = item;
3307 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3308 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3309 gtk_widget_set_sensitive (item, FALSE);
3310 g_signal_connect (item, "activate",
3311 G_CALLBACK (add_to_shortcuts_cb), impl);
3312 gtk_widget_show (item);
3313 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3315 item = gtk_image_menu_item_new_with_mnemonic (_("Open _Location"));
3316 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3317 gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU));
3318 g_signal_connect (item, "activate",
3319 G_CALLBACK (open_location_cb), impl);
3320 gtk_widget_show (item);
3321 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3323 item = gtk_separator_menu_item_new ();
3324 gtk_widget_show (item);
3325 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3327 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3328 impl->browse_files_popup_menu_hidden_files_item = item;
3329 g_signal_connect (item, "toggled",
3330 G_CALLBACK (show_hidden_toggled_cb), impl);
3331 gtk_widget_show (item);
3332 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3335 /* Updates the popup menu for the file list, creating it if necessary */
3337 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3339 file_list_build_popup_menu (impl);
3341 /* The sensitivity of the Add to Bookmarks item is set in
3342 * bookmarks_check_add_sensitivity()
3345 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3346 G_CALLBACK (show_hidden_toggled_cb), impl);
3347 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3349 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3350 G_CALLBACK (show_hidden_toggled_cb), impl);
3354 popup_position_func (GtkMenu *menu,
3360 GtkWidget *widget = GTK_WIDGET (user_data);
3361 GdkScreen *screen = gtk_widget_get_screen (widget);
3364 GdkRectangle monitor;
3366 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3368 gdk_window_get_origin (widget->window, x, y);
3370 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3372 *x += (widget->allocation.width - req.width) / 2;
3373 *y += (widget->allocation.height - req.height) / 2;
3375 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3376 gtk_menu_set_monitor (menu, monitor_num);
3377 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3379 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3380 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3386 file_list_popup_menu (GtkFileChooserDefault *impl,
3387 GdkEventButton *event)
3389 file_list_update_popup_menu (impl);
3391 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3392 NULL, NULL, NULL, NULL,
3393 event->button, event->time);
3396 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3398 popup_position_func, impl->browse_files_tree_view,
3399 0, GDK_CURRENT_TIME);
3400 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3406 /* Callback used for the GtkWidget::popup-menu signal of the file list */
3408 list_popup_menu_cb (GtkWidget *widget,
3409 GtkFileChooserDefault *impl)
3411 file_list_popup_menu (impl, NULL);
3415 /* Callback used when a button is pressed on the file list. We trap button 3 to
3416 * bring up a popup menu.
3419 list_button_press_event_cb (GtkWidget *widget,
3420 GdkEventButton *event,
3421 GtkFileChooserDefault *impl)
3423 if (event->button != 3)
3426 file_list_popup_menu (impl, event);
3430 /* Creates the widgets for the file list */
3432 create_file_list (GtkFileChooserDefault *impl)
3435 GtkTreeSelection *selection;
3436 GtkTreeViewColumn *column;
3437 GtkCellRenderer *renderer;
3439 /* Scrolled window */
3441 swin = gtk_scrolled_window_new (NULL, NULL);
3442 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3443 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3444 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3447 /* Tree/list view */
3449 impl->browse_files_tree_view = gtk_tree_view_new ();
3450 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "GtkFileChooserDefault", impl);
3451 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
3453 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
3454 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
3455 g_signal_connect (impl->browse_files_tree_view, "row-activated",
3456 G_CALLBACK (list_row_activated), impl);
3457 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
3458 G_CALLBACK (trap_activate_cb), impl);
3459 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
3460 G_CALLBACK (list_popup_menu_cb), impl);
3461 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
3462 G_CALLBACK (list_button_press_event_cb), impl);
3464 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3465 gtk_tree_selection_set_select_function (selection,
3468 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
3470 file_list_source_targets,
3471 num_file_list_source_targets,
3474 g_signal_connect (selection, "changed",
3475 G_CALLBACK (list_selection_changed), impl);
3477 /* Filename column */
3479 impl->list_name_column = gtk_tree_view_column_new ();
3480 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
3481 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
3482 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
3483 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
3485 renderer = gtk_cell_renderer_pixbuf_new ();
3486 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
3487 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
3488 list_icon_data_func, impl, NULL);
3490 impl->list_name_renderer = gtk_cell_renderer_text_new ();
3491 g_object_set (impl->list_name_renderer,
3492 "ellipsize", PANGO_ELLIPSIZE_END,
3494 g_signal_connect (impl->list_name_renderer, "edited",
3495 G_CALLBACK (renderer_edited_cb), impl);
3496 g_signal_connect (impl->list_name_renderer, "editing-canceled",
3497 G_CALLBACK (renderer_editing_canceled_cb), impl);
3498 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
3499 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
3500 list_name_data_func, impl, NULL);
3502 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
3506 column = gtk_tree_view_column_new ();
3507 gtk_tree_view_column_set_title (column, _("Size"));
3509 renderer = gtk_cell_renderer_text_new ();
3510 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3511 gtk_tree_view_column_set_cell_data_func (column, renderer,
3512 list_size_data_func, impl, NULL);
3513 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
3514 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3516 /* Modification time column */
3518 column = gtk_tree_view_column_new ();
3519 gtk_tree_view_column_set_resizable (column, TRUE);
3520 gtk_tree_view_column_set_title (column, _("Modified"));
3522 renderer = gtk_cell_renderer_text_new ();
3523 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3524 gtk_tree_view_column_set_cell_data_func (column, renderer,
3525 list_mtime_data_func, impl, NULL);
3526 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
3527 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3528 gtk_widget_show_all (swin);
3534 create_path_bar (GtkFileChooserDefault *impl)
3536 GtkWidget *path_bar;
3538 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
3539 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
3545 set_filter_tooltip (GtkWidget *widget,
3548 GtkTooltips *tooltips = (GtkTooltips *)data;
3550 if (GTK_IS_BUTTON (widget))
3551 gtk_tooltips_set_tip (tooltips, widget,
3552 _("Select which types of files are shown"),
3557 realize_filter_combo (GtkWidget *combo,
3560 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
3562 gtk_container_forall (GTK_CONTAINER (combo),
3567 /* Creates the widgets for the files/folders pane */
3569 file_pane_create (GtkFileChooserDefault *impl,
3570 GtkSizeGroup *size_group)
3576 vbox = gtk_vbox_new (FALSE, 6);
3577 gtk_widget_show (vbox);
3579 /* The path bar and 'Create Folder' button */
3580 hbox = gtk_hbox_new (FALSE, 12);
3581 gtk_widget_show (hbox);
3582 impl->browse_path_bar = create_path_bar (impl);
3583 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
3584 gtk_widget_show_all (impl->browse_path_bar);
3585 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
3588 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
3589 g_signal_connect (impl->browse_new_folder_button, "clicked",
3590 G_CALLBACK (new_folder_button_clicked), impl);
3591 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
3592 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3594 /* Box for lists and preview */
3596 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
3597 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
3598 gtk_widget_show (hbox);
3602 widget = create_file_list (impl);
3603 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
3607 impl->preview_box = gtk_vbox_new (FALSE, 12);
3608 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
3609 /* Don't show preview box initially */
3613 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
3615 widget = filter_create (impl);
3617 g_signal_connect (widget, "realize",
3618 G_CALLBACK (realize_filter_combo), impl);
3620 gtk_widget_show (widget);
3621 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
3623 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
3624 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
3628 /* Callback used when the "Browse for more folders" expander is toggled */
3630 expander_changed_cb (GtkExpander *expander,
3632 GtkFileChooserDefault *impl)
3634 update_appearance (impl);
3637 /* Callback used when the selection changes in the save folder combo box */
3639 save_folder_combo_changed_cb (GtkComboBox *combo,
3640 GtkFileChooserDefault *impl)
3644 if (impl->changing_folder)
3647 if (gtk_combo_box_get_active_iter (combo, &iter))
3648 shortcuts_activate_iter (impl, &iter);
3651 /* Creates the combo box with the save folders */
3653 save_folder_combo_create (GtkFileChooserDefault *impl)
3656 GtkCellRenderer *cell;
3658 combo = g_object_new (GTK_TYPE_COMBO_BOX,
3659 "model", impl->shortcuts_model,
3660 "focus-on-click", FALSE,
3662 gtk_widget_show (combo);
3664 cell = gtk_cell_renderer_pixbuf_new ();
3665 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3666 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3667 "pixbuf", SHORTCUTS_COL_PIXBUF,
3668 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3669 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3672 cell = gtk_cell_renderer_text_new ();
3673 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3674 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3675 "text", SHORTCUTS_COL_NAME,
3676 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3679 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
3680 shortcuts_row_separator_func,
3681 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3684 g_signal_connect (combo, "changed",
3685 G_CALLBACK (save_folder_combo_changed_cb), impl);
3690 /* Creates the widgets specific to Save mode */
3692 save_widgets_create (GtkFileChooserDefault *impl)
3697 GtkWidget *alignment;
3699 vbox = gtk_vbox_new (FALSE, 12);
3701 table = gtk_table_new (2, 2, FALSE);
3702 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3703 gtk_widget_show (table);
3704 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3705 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3709 widget = gtk_label_new_with_mnemonic (_("_Name:"));
3710 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3711 gtk_table_attach (GTK_TABLE (table), widget,
3715 gtk_widget_show (widget);
3717 impl->save_file_name_entry = _gtk_file_chooser_entry_new (TRUE);
3718 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3720 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3721 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3722 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3724 GTK_EXPAND | GTK_FILL, 0,
3726 gtk_widget_show (impl->save_file_name_entry);
3727 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
3730 impl->save_folder_label = gtk_label_new (NULL);
3731 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
3732 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
3736 gtk_widget_show (impl->save_folder_label);
3738 impl->save_folder_combo = save_folder_combo_create (impl);
3739 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
3741 GTK_EXPAND | GTK_FILL, GTK_FILL,
3743 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
3746 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3747 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
3749 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
3750 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
3751 g_signal_connect (impl->save_expander, "notify::expanded",
3752 G_CALLBACK (expander_changed_cb),
3754 gtk_widget_show_all (alignment);
3759 /* Creates the main hpaned with the widgets shared by Open and Save mode */
3761 browse_widgets_create (GtkFileChooserDefault *impl)
3766 GtkSizeGroup *size_group;
3768 /* size group is used by the [+][-] buttons and the filter combo */
3769 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
3770 vbox = gtk_vbox_new (FALSE, 12);
3773 hpaned = gtk_hpaned_new ();
3774 gtk_widget_show (hpaned);
3775 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
3776 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
3778 widget = shortcuts_pane_create (impl, size_group);
3779 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
3780 widget = file_pane_create (impl, size_group);
3781 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
3783 g_object_unref (size_group);
3789 gtk_file_chooser_default_constructor (GType type,
3790 guint n_construct_properties,
3791 GObjectConstructParam *construct_params)
3793 GtkFileChooserDefault *impl;
3796 object = parent_class->constructor (type,
3797 n_construct_properties,
3799 impl = GTK_FILE_CHOOSER_DEFAULT (object);
3801 g_assert (impl->file_system);
3803 gtk_widget_push_composite_child ();
3805 /* Shortcuts model */
3807 shortcuts_model_create (impl);
3809 /* Widgets for Save mode */
3810 impl->save_widgets = save_widgets_create (impl);
3811 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
3813 /* The browse widgets */
3814 impl->browse_widgets = browse_widgets_create (impl);
3815 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
3817 /* Alignment to hold extra widget */
3818 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3819 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
3821 gtk_widget_pop_composite_child ();
3822 update_appearance (impl);
3827 /* Sets the extra_widget by packing it in the appropriate place */
3829 set_extra_widget (GtkFileChooserDefault *impl,
3830 GtkWidget *extra_widget)
3834 g_object_ref (extra_widget);
3835 /* FIXME: is this right ? */
3836 gtk_widget_show (extra_widget);
3839 if (impl->extra_widget)
3841 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3842 g_object_unref (impl->extra_widget);
3845 impl->extra_widget = extra_widget;
3846 if (impl->extra_widget)
3848 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3849 gtk_widget_show (impl->extra_align);
3852 gtk_widget_hide (impl->extra_align);
3856 set_local_only (GtkFileChooserDefault *impl,
3857 gboolean local_only)
3859 if (local_only != impl->local_only)
3861 impl->local_only = local_only;
3863 if (impl->shortcuts_model && impl->file_system)
3865 shortcuts_add_volumes (impl);
3866 shortcuts_add_bookmarks (impl);
3870 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
3872 /* If we are pointing to a non-local folder, make an effort to change
3873 * back to a local folder, but it's really up to the app to not cause
3874 * such a situation, so we ignore errors.
3876 const gchar *home = g_get_home_dir ();
3877 GtkFilePath *home_path;
3882 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
3884 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
3886 gtk_file_path_free (home_path);
3892 volumes_changed_cb (GtkFileSystem *file_system,
3893 GtkFileChooserDefault *impl)
3895 shortcuts_add_volumes (impl);
3898 /* Callback used when the set of bookmarks changes in the file system */
3900 bookmarks_changed_cb (GtkFileSystem *file_system,
3901 GtkFileChooserDefault *impl)
3903 shortcuts_add_bookmarks (impl);
3905 bookmarks_check_add_sensitivity (impl);
3906 bookmarks_check_remove_sensitivity (impl);
3907 shortcuts_check_popup_sensitivity (impl);
3910 /* Sets the file chooser to multiple selection mode */
3912 set_select_multiple (GtkFileChooserDefault *impl,
3913 gboolean select_multiple,
3914 gboolean property_notify)
3916 GtkTreeSelection *selection;
3917 GtkSelectionMode mode;
3919 if (select_multiple == impl->select_multiple)
3922 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3924 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3925 gtk_tree_selection_set_mode (selection, mode);
3927 impl->select_multiple = select_multiple;
3928 g_object_notify (G_OBJECT (impl), "select-multiple");
3930 check_preview_change (impl);
3934 set_file_system_backend (GtkFileChooserDefault *impl,
3935 const char *backend)
3937 if (impl->file_system)
3939 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3940 impl->volumes_changed_id = 0;
3941 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3942 impl->bookmarks_changed_id = 0;
3943 g_object_unref (impl->file_system);
3946 impl->file_system = NULL;
3948 impl->file_system = _gtk_file_system_create (backend);
3951 GtkSettings *settings = gtk_settings_get_default ();
3952 gchar *default_backend = NULL;
3954 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3955 if (default_backend)
3957 impl->file_system = _gtk_file_system_create (default_backend);
3958 g_free (default_backend);
3962 if (!impl->file_system)
3964 #if defined (G_OS_UNIX)
3965 impl->file_system = gtk_file_system_unix_new ();
3966 #elif defined (G_OS_WIN32)
3967 impl->file_system = gtk_file_system_win32_new ();
3969 #error "No default filesystem implementation on the platform"
3973 if (impl->file_system)
3975 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3976 G_CALLBACK (volumes_changed_cb),
3978 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3979 G_CALLBACK (bookmarks_changed_cb),
3984 /* This function is basically a do_all function.
3986 * It sets the visibility on all the widgets based on the current state, and
3987 * moves the custom_widget if needed.
3990 update_appearance (GtkFileChooserDefault *impl)
3992 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3993 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3997 gtk_widget_show (impl->save_widgets);
3999 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4000 text = _("Save in _folder:");
4002 text = _("Create in _folder:");
4004 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
4006 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4008 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
4009 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
4010 gtk_widget_show (impl->browse_widgets);
4014 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
4015 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
4016 gtk_widget_hide (impl->browse_widgets);
4019 gtk_widget_show (impl->browse_new_folder_button);
4021 if (impl->select_multiple)
4023 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
4024 "Re-setting to single selection mode.");
4025 set_select_multiple (impl, FALSE, TRUE);
4028 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4029 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4031 gtk_widget_hide (impl->save_widgets);
4032 gtk_widget_show (impl->browse_widgets);
4035 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
4036 gtk_widget_hide (impl->browse_new_folder_button);
4038 gtk_widget_show (impl->browse_new_folder_button);
4040 gtk_widget_queue_draw (impl->browse_files_tree_view);
4042 g_signal_emit_by_name (impl, "default-size-changed");
4046 gtk_file_chooser_default_set_property (GObject *object,
4048 const GValue *value,
4052 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4056 case GTK_FILE_CHOOSER_PROP_ACTION:
4058 GtkFileChooserAction action = g_value_get_enum (value);
4060 if (action != impl->action)
4062 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
4064 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4065 && impl->select_multiple)
4067 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
4068 "this is not allowed in multiple selection mode. Resetting the file chooser "
4069 "to single selection mode.");
4070 set_select_multiple (impl, FALSE, TRUE);
4072 impl->action = action;
4073 update_appearance (impl);
4076 if (impl->save_file_name_entry)
4077 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4081 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
4082 set_file_system_backend (impl, g_value_get_string (value));
4084 case GTK_FILE_CHOOSER_PROP_FILTER:
4085 set_current_filter (impl, g_value_get_object (value));
4087 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4088 set_local_only (impl, g_value_get_boolean (value));
4090 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4091 set_preview_widget (impl, g_value_get_object (value));
4093 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4094 impl->preview_widget_active = g_value_get_boolean (value);
4095 update_preview_widget_visibility (impl);
4097 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4098 impl->use_preview_label = g_value_get_boolean (value);
4099 update_preview_widget_visibility (impl);
4101 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4102 set_extra_widget (impl, g_value_get_object (value));
4104 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4106 gboolean select_multiple = g_value_get_boolean (value);
4107 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4110 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
4111 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
4112 "leaving the file chooser in single selection mode.");
4116 set_select_multiple (impl, select_multiple, FALSE);
4119 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
4121 gboolean show_hidden = g_value_get_boolean (value);
4122 if (show_hidden != impl->show_hidden)
4124 impl->show_hidden = show_hidden;
4126 if (impl->browse_files_model)
4127 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
4132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4138 gtk_file_chooser_default_get_property (GObject *object,
4143 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4147 case GTK_FILE_CHOOSER_PROP_ACTION:
4148 g_value_set_enum (value, impl->action);
4150 case GTK_FILE_CHOOSER_PROP_FILTER:
4151 g_value_set_object (value, impl->current_filter);
4153 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4154 g_value_set_boolean (value, impl->local_only);
4156 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4157 g_value_set_object (value, impl->preview_widget);
4159 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4160 g_value_set_boolean (value, impl->preview_widget_active);
4162 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4163 g_value_set_boolean (value, impl->use_preview_label);
4165 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4166 g_value_set_object (value, impl->extra_widget);
4168 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4169 g_value_set_boolean (value, impl->select_multiple);
4171 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
4172 g_value_set_boolean (value, impl->show_hidden);
4175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4180 /* Removes the settings signal handler. It's safe to call multiple times */
4182 remove_settings_signal (GtkFileChooserDefault *impl,
4185 if (impl->settings_signal_id)
4187 GtkSettings *settings;
4189 settings = gtk_settings_get_for_screen (screen);
4190 g_signal_handler_disconnect (settings,
4191 impl->settings_signal_id);
4192 impl->settings_signal_id = 0;
4197 gtk_file_chooser_default_dispose (GObject *object)
4199 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
4201 if (impl->extra_widget)
4203 g_object_unref (impl->extra_widget);
4204 impl->extra_widget = NULL;
4207 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
4209 G_OBJECT_CLASS (parent_class)->dispose (object);
4212 /* We override show-all since we have internal widgets that
4213 * shouldn't be shown when you call show_all(), like the filter
4217 gtk_file_chooser_default_show_all (GtkWidget *widget)
4219 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
4221 gtk_widget_show (widget);
4223 if (impl->extra_widget)
4224 gtk_widget_show_all (impl->extra_widget);
4227 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
4228 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
4231 toplevel_set_focus_cb (GtkWindow *window,
4233 GtkFileChooserDefault *impl)
4235 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
4238 /* We monitor the focus widget on our toplevel to be able to know which widget
4239 * was last focused at the time our "should_respond" method gets called.
4242 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
4243 GtkWidget *previous_toplevel)
4245 GtkFileChooserDefault *impl;
4246 GtkWidget *toplevel;
4248 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4250 if (previous_toplevel)
4252 g_assert (impl->toplevel_set_focus_id != 0);
4253 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
4254 impl->toplevel_set_focus_id = 0;
4255 impl->toplevel_last_focus_widget = NULL;
4258 g_assert (impl->toplevel_set_focus_id == 0);
4260 toplevel = gtk_widget_get_toplevel (widget);
4261 if (GTK_IS_WINDOW (toplevel))
4263 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
4264 G_CALLBACK (toplevel_set_focus_cb), impl);
4265 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
4269 /* Changes the icons wherever it is needed */
4271 change_icon_theme (GtkFileChooserDefault *impl)
4273 GtkSettings *settings;
4276 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4278 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
4279 impl->icon_size = MAX (width, height);
4281 impl->icon_size = FALLBACK_ICON_SIZE;
4283 shortcuts_reload_icons (impl);
4284 gtk_widget_queue_resize (impl->browse_files_tree_view);
4287 /* Callback used when a GtkSettings value changes */
4289 settings_notify_cb (GObject *object,
4291 GtkFileChooserDefault *impl)
4295 name = g_param_spec_get_name (pspec);
4297 if (strcmp (name, "gtk-icon-theme-name") == 0
4298 || strcmp (name, "gtk-icon-sizes") == 0)
4299 change_icon_theme (impl);
4302 /* Installs a signal handler for GtkSettings so that we can monitor changes in
4306 check_icon_theme (GtkFileChooserDefault *impl)
4308 GtkSettings *settings;
4310 if (impl->settings_signal_id)
4313 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4315 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4316 impl->settings_signal_id = g_signal_connect (settings, "notify",
4317 G_CALLBACK (settings_notify_cb), impl);
4319 change_icon_theme (impl);
4324 gtk_file_chooser_default_style_set (GtkWidget *widget,
4325 GtkStyle *previous_style)
4327 GtkFileChooserDefault *impl;
4329 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4331 if (GTK_WIDGET_CLASS (parent_class)->style_set)
4332 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
4334 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4335 change_icon_theme (impl);
4337 g_signal_emit_by_name (widget, "default-size-changed");
4341 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
4342 GdkScreen *previous_screen)
4344 GtkFileChooserDefault *impl;
4346 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4348 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
4349 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
4351 remove_settings_signal (impl, previous_screen);
4352 check_icon_theme (impl);
4354 g_signal_emit_by_name (widget, "default-size-changed");
4358 get_is_file_filtered (GtkFileChooserDefault *impl,
4359 const GtkFilePath *path,
4360 GtkFileInfo *file_info)
4362 GtkFileFilterInfo filter_info;
4363 GtkFileFilterFlags needed;
4366 if (!impl->current_filter)
4369 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
4371 needed = gtk_file_filter_get_needed (impl->current_filter);
4373 filter_info.display_name = gtk_file_info_get_display_name (file_info);
4374 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
4376 if (needed & GTK_FILE_FILTER_FILENAME)
4378 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
4379 if (filter_info.filename)
4380 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
4383 filter_info.filename = NULL;
4385 if (needed & GTK_FILE_FILTER_URI)
4387 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
4388 if (filter_info.uri)
4389 filter_info.contains |= GTK_FILE_FILTER_URI;
4392 filter_info.uri = NULL;
4394 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
4396 if (filter_info.filename)
4397 g_free ((gchar *)filter_info.filename);
4398 if (filter_info.uri)
4399 g_free ((gchar *)filter_info.uri);
4404 /* GtkWidget::map method */
4406 gtk_file_chooser_default_map (GtkWidget *widget)
4408 GtkFileChooserDefault *impl;
4410 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4412 GTK_WIDGET_CLASS (parent_class)->map (widget);
4414 if (impl->current_folder)
4416 pending_select_paths_store_selection (impl);
4417 change_folder_and_display_error (impl, impl->current_folder);
4420 bookmarks_changed_cb (impl->file_system, impl);
4424 list_model_filter_func (GtkFileSystemModel *model,
4426 const GtkFileInfo *file_info,
4429 GtkFileChooserDefault *impl = user_data;
4431 if (!impl->current_filter)
4434 if (gtk_file_info_get_is_folder (file_info))
4437 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
4441 install_list_model_filter (GtkFileChooserDefault *impl)
4443 GtkFileSystemModelFilter filter;
4446 g_assert (impl->browse_files_model != NULL);
4448 if (impl->current_filter)
4450 filter = list_model_filter_func;
4459 _gtk_file_system_model_set_filter (impl->browse_files_model,
4464 #define COMPARE_DIRECTORIES \
4465 GtkFileChooserDefault *impl = user_data; \
4466 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
4467 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
4468 gboolean dir_a, dir_b; \
4471 dir_a = gtk_file_info_get_is_folder (info_a); \
4473 return impl->list_sort_ascending ? -1 : 1; \
4476 dir_b = gtk_file_info_get_is_folder (info_b); \
4478 return impl->list_sort_ascending ? 1 : -1; \
4480 if (dir_a != dir_b) \
4481 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
4483 /* Sort callback for the filename column */
4485 name_sort_func (GtkTreeModel *model,
4490 COMPARE_DIRECTORIES;
4492 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
4495 /* Sort callback for the size column */
4497 size_sort_func (GtkTreeModel *model,
4502 COMPARE_DIRECTORIES;
4505 gint64 size_a = gtk_file_info_get_size (info_a);
4506 gint64 size_b = gtk_file_info_get_size (info_b);
4508 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
4512 /* Sort callback for the mtime column */
4514 mtime_sort_func (GtkTreeModel *model,
4519 COMPARE_DIRECTORIES;
4522 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
4523 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
4525 return ta > tb ? -1 : (ta == tb ? 0 : 1);
4529 /* Callback used when the sort column changes. We cache the sort order for use
4530 * in name_sort_func().
4533 list_sort_column_changed_cb (GtkTreeSortable *sortable,
4534 GtkFileChooserDefault *impl)
4536 GtkSortType sort_type;
4538 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
4539 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
4543 set_busy_cursor (GtkFileChooserDefault *impl,
4546 GtkWindow *toplevel;
4547 GdkDisplay *display;
4550 toplevel = get_toplevel (GTK_WIDGET (impl));
4551 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
4554 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
4557 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
4561 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
4562 gdk_display_flush (display);
4565 gdk_cursor_unref (cursor);
4568 /* Creates a sort model to wrap the file system model and sets it on the tree view */
4570 load_set_model (GtkFileChooserDefault *impl)
4572 g_assert (impl->browse_files_model != NULL);
4573 g_assert (impl->sort_model == NULL);
4575 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4576 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4577 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4578 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4579 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4580 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4581 impl->list_sort_ascending = TRUE;
4583 g_signal_connect (impl->sort_model, "sort-column-changed",
4584 G_CALLBACK (list_sort_column_changed_cb), impl);
4586 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4587 GTK_TREE_MODEL (impl->sort_model));
4588 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4589 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4590 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4593 /* Timeout callback used when the loading timer expires */
4595 load_timeout_cb (gpointer data)
4597 GtkFileChooserDefault *impl;
4599 GDK_THREADS_ENTER ();
4601 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4602 g_assert (impl->load_state == LOAD_PRELOAD);
4603 g_assert (impl->load_timeout_id != 0);
4604 g_assert (impl->browse_files_model != NULL);
4606 impl->load_timeout_id = 0;
4607 impl->load_state = LOAD_LOADING;
4609 load_set_model (impl);
4611 GDK_THREADS_LEAVE ();
4616 /* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
4618 load_setup_timer (GtkFileChooserDefault *impl)
4620 g_assert (impl->load_timeout_id == 0);
4621 g_assert (impl->load_state != LOAD_PRELOAD);
4623 impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
4624 impl->load_state = LOAD_PRELOAD;
4627 /* Removes the load timeout and switches to the LOAD_FINISHED state */
4629 load_remove_timer (GtkFileChooserDefault *impl)
4631 if (impl->load_timeout_id != 0)
4633 g_assert (impl->load_state == LOAD_PRELOAD);
4635 g_source_remove (impl->load_timeout_id);
4636 impl->load_timeout_id = 0;
4637 impl->load_state = LOAD_EMPTY;
4640 g_assert (impl->load_state == LOAD_EMPTY ||
4641 impl->load_state == LOAD_LOADING ||
4642 impl->load_state == LOAD_FINISHED);
4645 /* Selects the first row in the file list */
4647 browse_files_select_first_row (GtkFileChooserDefault *impl)
4651 if (!impl->sort_model)
4654 path = gtk_tree_path_new_from_indices (0, -1);
4655 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4656 gtk_tree_path_free (path);
4659 struct center_selected_row_closure {
4660 GtkFileChooserDefault *impl;
4661 gboolean already_centered;
4664 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
4665 * selected row in the tree view.
4668 center_selected_row_foreach_cb (GtkTreeModel *model,
4673 struct center_selected_row_closure *closure;
4676 if (closure->already_centered)
4679 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
4680 closure->already_centered = TRUE;
4683 /* Centers the selected row in the tree view */
4685 browse_files_center_selected_row (GtkFileChooserDefault *impl)
4687 struct center_selected_row_closure closure;
4688 GtkTreeSelection *selection;
4690 closure.impl = impl;
4691 closure.already_centered = FALSE;
4693 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4694 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
4698 show_and_select_paths (GtkFileChooserDefault *impl,
4699 const GtkFilePath *parent_path,
4700 const GtkFilePath *only_one_path,
4704 GtkFileFolder *folder;
4706 gboolean have_hidden;
4707 gboolean have_filtered;
4709 if (!only_one_path && !paths)
4712 folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
4717 have_hidden = FALSE;
4718 have_filtered = FALSE;
4724 info = gtk_file_folder_get_info (folder, only_one_path, error);
4728 have_hidden = gtk_file_info_get_is_hidden (info);
4729 have_filtered = get_is_file_filtered (impl, only_one_path, info);
4730 gtk_file_info_free (info);
4737 for (l = paths; l; l = l->next)
4739 const GtkFilePath *path;
4745 info = gtk_file_folder_get_info (folder, path, NULL);
4749 have_hidden = gtk_file_info_get_is_hidden (info);
4752 have_filtered = get_is_file_filtered (impl, path, info);
4754 gtk_file_info_free (info);
4756 if (have_hidden && have_filtered)
4757 break; /* we now have all the information we need */
4764 g_object_unref (folder);
4770 g_object_set (impl, "show-hidden", TRUE, NULL);
4773 set_current_filter (impl, NULL);
4776 _gtk_file_system_model_path_do (impl->browse_files_model, only_one_path, select_func, impl);
4781 for (l = paths; l; l = l->next)
4783 const GtkFilePath *path;
4786 _gtk_file_system_model_path_do (impl->browse_files_model, path, select_func, impl);
4793 /* Processes the pending operation when a folder is finished loading */
4795 pending_select_paths_process (GtkFileChooserDefault *impl)
4797 g_assert (impl->load_state == LOAD_FINISHED);
4798 g_assert (impl->browse_files_model != NULL);
4799 g_assert (impl->sort_model != NULL);
4801 if (impl->pending_select_paths)
4804 show_and_select_paths (impl, impl->current_folder, NULL, impl->pending_select_paths, NULL);
4805 pending_select_paths_free (impl);
4806 browse_files_center_selected_row (impl);
4810 /* We only select the first row if the chooser is actually mapped ---
4811 * selecting the first row is to help the user when he is interacting with
4812 * the chooser, but sometimes a chooser works not on behalf of the user,
4813 * but rather on behalf of something else like GtkFileChooserButton. In
4814 * that case, the chooser's selection should be what the caller expects,
4815 * as the user can't see that something else got selected. See bug #165264.
4817 * Also, we don't select the first file if we are not in OPEN mode. Doing
4818 * so would change the contents of the filename entry for SAVE or
4819 * CREATE_FOLDER, which is undesired; in SELECT_FOLDER, we don't want to
4820 * select a *different* folder from the one into which the user just
4823 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
4824 browse_files_select_first_row (impl);
4827 g_assert (impl->pending_select_paths == NULL);
4830 /* Callback used when the file system model finishes loading */
4832 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
4833 GtkFileChooserDefault *impl)
4835 if (impl->load_state == LOAD_PRELOAD)
4837 load_remove_timer (impl);
4838 load_set_model (impl);
4840 else if (impl->load_state == LOAD_LOADING)
4846 /* We can't g_assert_not_reached(), as something other than us may have
4847 * initiated a folder reload. See #165556.
4852 g_assert (impl->load_timeout_id == 0);
4854 impl->load_state = LOAD_FINISHED;
4856 pending_select_paths_process (impl);
4857 set_busy_cursor (impl, FALSE);
4860 /* Gets rid of the old list model and creates a new one for the current folder */
4862 set_list_model (GtkFileChooserDefault *impl,
4865 g_assert (impl->current_folder != NULL);
4867 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
4869 if (impl->browse_files_model)
4871 g_object_unref (impl->browse_files_model);
4872 impl->browse_files_model = NULL;
4875 if (impl->sort_model)
4877 g_object_unref (impl->sort_model);
4878 impl->sort_model = NULL;
4881 set_busy_cursor (impl, TRUE);
4882 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
4884 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
4885 impl->current_folder, 0,
4888 if (!impl->browse_files_model)
4890 set_busy_cursor (impl, FALSE);
4894 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
4896 g_signal_connect (impl->browse_files_model, "finished-loading",
4897 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
4899 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
4901 install_list_model_filter (impl);
4907 update_chooser_entry (GtkFileChooserDefault *impl)
4909 GtkTreeSelection *selection;
4910 const GtkFileInfo *info;
4912 GtkTreeIter child_iter;
4913 gboolean change_entry;
4915 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
4918 g_assert (!impl->select_multiple);
4919 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4921 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4923 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), "");
4927 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4931 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4933 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4934 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
4936 change_entry = TRUE; /* ... unless we are in CREATE_FOLDER mode */
4939 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4940 gtk_file_info_get_display_name (info));
4944 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
4945 const GtkFilePath *path,
4948 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4951 g_assert (path != NULL);
4953 if (impl->local_only &&
4954 !gtk_file_system_path_is_local (impl->file_system, path))
4957 GTK_FILE_CHOOSER_ERROR,
4958 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
4959 _("Cannot change to folder because it is not local"));
4964 /* Test validity of path here. */
4965 if (!check_is_folder (impl->file_system, path, error))
4968 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
4971 if (impl->current_folder != path)
4973 if (impl->current_folder)
4974 gtk_file_path_free (impl->current_folder);
4976 impl->current_folder = gtk_file_path_copy (path);
4979 /* Update the widgets that may trigger a folder change themselves. */
4981 if (!impl->changing_folder)
4983 impl->changing_folder = TRUE;
4985 shortcuts_update_current_folder (impl);
4987 impl->changing_folder = FALSE;
4990 /* Set the folder on the save entry */
4992 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4993 impl->current_folder);
4995 /* Create a new list model. This is slightly evil; we store the result value
4996 * but perform more actions rather than returning immediately even if it
4997 * generates an error.
4999 result = set_list_model (impl, error);
5001 /* Refresh controls */
5003 shortcuts_find_current_folder (impl);
5005 g_signal_emit_by_name (impl, "current-folder-changed", 0);
5007 check_preview_change (impl);
5008 bookmarks_check_add_sensitivity (impl);
5010 g_signal_emit_by_name (impl, "selection-changed", 0);
5015 static GtkFilePath *
5016 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
5018 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5020 return gtk_file_path_copy (impl->current_folder);
5024 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
5027 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5029 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5030 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5032 pending_select_paths_free (impl);
5033 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), name);
5037 select_func (GtkFileSystemModel *model,
5042 GtkFileChooserDefault *impl = user_data;
5043 GtkTreeSelection *selection;
5044 GtkTreeIter sorted_iter;
5046 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5048 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
5049 gtk_tree_selection_select_iter (selection, &sorted_iter);
5053 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
5054 const GtkFilePath *path,
5057 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5058 GtkFilePath *parent_path;
5061 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
5065 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
5067 if (impl->load_state == LOAD_EMPTY)
5071 g_assert (impl->current_folder != NULL);
5073 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
5076 if (same_path && impl->load_state == LOAD_FINISHED)
5080 result = show_and_select_paths (impl, parent_path, path, NULL, error);
5081 gtk_file_path_free (parent_path);
5085 pending_select_paths_add (impl, path);
5091 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
5092 gtk_file_path_free (parent_path);
5096 gtk_file_path_free (parent_path);
5101 unselect_func (GtkFileSystemModel *model,
5106 GtkFileChooserDefault *impl = user_data;
5107 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
5108 GtkTreePath *sorted_path;
5110 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
5112 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
5114 gtk_tree_path_free (sorted_path);
5118 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
5119 const GtkFilePath *path)
5121 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5123 if (!impl->browse_files_model)
5126 _gtk_file_system_model_path_do (impl->browse_files_model, path,
5127 unselect_func, impl);
5131 maybe_select (GtkTreeModel *model,
5136 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
5137 GtkTreeSelection *selection;
5138 const GtkFileInfo *info;
5141 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5143 info = get_list_file_info (impl, iter);
5144 is_folder = gtk_file_info_get_is_folder (info);
5146 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
5147 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
5148 gtk_tree_selection_select_iter (selection, iter);
5150 gtk_tree_selection_unselect_iter (selection, iter);
5156 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
5158 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5159 if (impl->select_multiple)
5160 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
5161 maybe_select, impl);
5165 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
5167 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5168 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5170 gtk_tree_selection_unselect_all (selection);
5171 pending_select_paths_free (impl);
5174 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
5176 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
5178 * is_empty_ret - whether the file entry is totally empty
5180 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
5181 * the path will be "$cwd/foobar")
5184 check_save_entry (GtkFileChooserDefault *impl,
5185 GtkFilePath **path_ret,
5186 gboolean *is_well_formed_ret,
5187 gboolean *is_empty_ret,
5188 gboolean *is_file_part_empty_ret)
5190 GtkFileChooserEntry *chooser_entry;
5191 const GtkFilePath *current_folder;
5192 const char *file_part;
5196 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5197 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5199 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
5201 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
5204 *is_well_formed_ret = TRUE;
5205 *is_empty_ret = TRUE;
5206 *is_file_part_empty_ret = TRUE;
5211 *is_empty_ret = FALSE;
5213 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
5214 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
5216 if (!file_part || file_part[0] == '\0')
5218 *path_ret = gtk_file_path_copy (current_folder);
5219 *is_well_formed_ret = TRUE;
5220 *is_file_part_empty_ret = TRUE;
5225 *is_file_part_empty_ret = FALSE;
5228 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
5232 error_building_filename_dialog (impl, current_folder, file_part, error);
5234 *is_well_formed_ret = FALSE;
5240 *is_well_formed_ret = TRUE;
5243 struct get_paths_closure {
5244 GtkFileChooserDefault *impl;
5246 GtkFilePath *path_from_entry;
5250 get_paths_foreach (GtkTreeModel *model,
5255 struct get_paths_closure *info;
5256 const GtkFilePath *file_path;
5257 GtkFileSystemModel *fs_model;
5258 GtkTreeIter sel_iter;
5261 fs_model = info->impl->browse_files_model;
5262 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
5264 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
5266 return; /* We are on the editable row */
5268 if (!info->path_from_entry
5269 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
5270 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
5274 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
5276 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5277 struct get_paths_closure info;
5281 info.path_from_entry = NULL;
5283 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5284 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5286 gboolean is_well_formed, is_empty, is_file_part_empty;
5288 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty);
5290 if (!is_well_formed)
5295 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5297 gtk_file_path_free (info.path_from_entry);
5303 if (!info.path_from_entry || impl->select_multiple)
5305 GtkTreeSelection *selection;
5307 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5308 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
5311 if (info.path_from_entry)
5312 info.result = g_slist_prepend (info.result, info.path_from_entry);
5314 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
5315 * fall back to the current directory */
5316 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
5317 info.result == NULL)
5319 info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
5322 return g_slist_reverse (info.result);
5325 static GtkFilePath *
5326 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
5328 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5330 if (impl->preview_path)
5331 return gtk_file_path_copy (impl->preview_path);
5336 static GtkFileSystem *
5337 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
5339 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5341 return impl->file_system;
5344 /* Shows or hides the filter widgets */
5346 show_filters (GtkFileChooserDefault *impl,
5350 gtk_widget_show (impl->filter_combo_hbox);
5352 gtk_widget_hide (impl->filter_combo_hbox);
5356 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
5357 GtkFileFilter *filter)
5359 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5362 if (g_slist_find (impl->filters, filter))
5364 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
5368 g_object_ref (filter);
5369 gtk_object_sink (GTK_OBJECT (filter));
5370 impl->filters = g_slist_append (impl->filters, filter);
5372 name = gtk_file_filter_get_name (filter);
5374 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
5376 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
5378 if (!g_slist_find (impl->filters, impl->current_filter))
5379 set_current_filter (impl, filter);
5381 show_filters (impl, TRUE);
5385 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
5386 GtkFileFilter *filter)
5388 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5389 GtkTreeModel *model;
5393 filter_index = g_slist_index (impl->filters, filter);
5395 if (filter_index < 0)
5397 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
5401 impl->filters = g_slist_remove (impl->filters, filter);
5403 if (filter == impl->current_filter)
5406 set_current_filter (impl, impl->filters->data);
5408 set_current_filter (impl, NULL);
5411 /* Remove row from the combo box */
5412 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
5413 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
5414 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
5416 g_object_unref (filter);
5419 show_filters (impl, FALSE);
5423 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
5425 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5427 return g_slist_copy (impl->filters);
5430 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
5432 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
5435 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
5439 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
5440 const GtkFilePath *path,
5443 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5447 /* Test validity of path here. */
5448 if (!check_is_folder (impl->file_system, path, error))
5451 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
5453 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
5456 impl->num_shortcuts++;
5458 if (impl->shortcuts_filter_model)
5459 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
5465 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
5466 const GtkFilePath *path,
5469 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5475 if (impl->num_shortcuts == 0)
5478 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5479 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5480 g_assert_not_reached ();
5482 for (i = 0; i < impl->num_shortcuts; i++)
5486 GtkFilePath *shortcut;
5488 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5489 SHORTCUTS_COL_DATA, &col_data,
5490 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5492 g_assert (col_data != NULL);
5493 g_assert (!is_volume);
5495 shortcut = col_data;
5496 if (gtk_file_path_compare (shortcut, path) == 0)
5498 shortcuts_remove_rows (impl, pos + i, 1);
5499 impl->num_shortcuts--;
5503 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5504 g_assert_not_reached ();
5509 uri = gtk_file_system_path_to_uri (impl->file_system, path);
5511 GTK_FILE_CHOOSER_ERROR,
5512 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
5513 _("Shortcut %s does not exist"),
5521 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
5523 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5529 if (impl->num_shortcuts == 0)
5532 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5533 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5534 g_assert_not_reached ();
5538 for (i = 0; i < impl->num_shortcuts; i++)
5542 GtkFilePath *shortcut;
5544 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5545 SHORTCUTS_COL_DATA, &col_data,
5546 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5548 g_assert (col_data != NULL);
5549 g_assert (!is_volume);
5551 shortcut = col_data;
5552 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
5554 if (i != impl->num_shortcuts - 1)
5556 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5557 g_assert_not_reached ();
5561 return g_slist_reverse (list);
5564 /* Guesses a size based upon font sizes */
5566 find_good_size_from_style (GtkWidget *widget,
5570 GtkFileChooserDefault *impl;
5571 gint default_width, default_height;
5574 GtkRequisition preview_req;
5576 g_assert (widget->style != NULL);
5577 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5579 font_size = pango_font_description_get_size (widget->style->font_desc);
5580 font_size = PANGO_PIXELS (font_size);
5582 default_width = font_size * NUM_CHARS;
5583 default_height = font_size * NUM_LINES;
5585 /* Use at least the requisition size not including the preview widget */
5586 gtk_widget_size_request (widget, &req);
5588 if (impl->preview_widget_active && impl->preview_widget)
5589 gtk_widget_size_request (impl->preview_box, &preview_req);
5591 preview_req.width = 0;
5593 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
5594 default_height = MAX (default_height, req.height);
5596 *width = default_width;
5597 *height = default_height;
5601 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
5602 gint *default_width,
5603 gint *default_height)
5605 GtkFileChooserDefault *impl;
5607 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5609 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
5611 if (impl->preview_widget_active && impl->preview_widget)
5612 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
5616 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
5617 gboolean *resize_horizontally,
5618 gboolean *resize_vertically)
5620 GtkFileChooserDefault *impl;
5622 g_return_if_fail (resize_horizontally != NULL);
5623 g_return_if_fail (resize_vertically != NULL);
5625 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5627 *resize_horizontally = TRUE;
5628 *resize_vertically = TRUE;
5630 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5631 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5633 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5635 *resize_horizontally = FALSE;
5636 *resize_vertically = FALSE;
5641 struct switch_folder_closure {
5642 GtkFileChooserDefault *impl;
5643 const GtkFilePath *path;
5647 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
5649 switch_folder_foreach_cb (GtkTreeModel *model,
5654 struct switch_folder_closure *closure;
5655 GtkTreeIter child_iter;
5659 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
5661 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
5662 closure->num_selected++;
5665 /* Changes to the selected folder in the list view */
5667 switch_to_selected_folder (GtkFileChooserDefault *impl)
5669 GtkTreeSelection *selection;
5670 struct switch_folder_closure closure;
5672 /* We do this with foreach() rather than get_selected() as we may be in
5673 * multiple selection mode
5676 closure.impl = impl;
5677 closure.path = NULL;
5678 closure.num_selected = 0;
5680 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5681 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
5683 g_assert (closure.path && closure.num_selected == 1);
5685 change_folder_and_display_error (impl, closure.path);
5688 /* Implementation for GtkFileChooserEmbed::should_respond() */
5690 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
5692 GtkFileChooserDefault *impl;
5693 GtkWidget *toplevel;
5694 GtkWidget *current_focus;
5696 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5698 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
5699 g_assert (GTK_IS_WINDOW (toplevel));
5701 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
5703 if (current_focus == impl->browse_files_tree_view)
5705 /* The following array encodes what we do based on the impl->action and the
5706 * number of files selected.
5709 NOOP, /* Do nothing (don't respond) */
5710 RESPOND, /* Respond immediately */
5711 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
5712 ALL_FILES, /* Respond only if everything selected is a file */
5713 ALL_FOLDERS, /* Respond only if everything selected is a folder */
5714 SAVE_ENTRY, /* Go to the code for handling the save entry */
5715 NOT_REACHED /* Sanity check */
5717 static const ActionToTake what_to_do[4][3] = {
5718 /* 0 selected 1 selected many selected */
5719 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
5720 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
5721 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
5722 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
5726 gboolean all_files, all_folders;
5728 ActionToTake action;
5732 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5734 selection_check (impl, &num_selected, &all_files, &all_folders);
5736 if (num_selected > 2)
5741 action = what_to_do [impl->action] [k];
5751 case RESPOND_OR_SWITCH:
5752 g_assert (num_selected == 1);
5756 switch_to_selected_folder (impl);
5772 g_assert_not_reached ();
5775 else if (current_focus == impl->save_file_name_entry)
5778 gboolean is_well_formed, is_empty, is_file_part_empty;
5781 GtkFileChooserEntry *entry;
5786 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5787 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5789 entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
5790 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty);
5792 if (is_empty || !is_well_formed)
5795 g_assert (path != NULL);
5798 is_folder = check_is_folder (impl->file_system, path, &error);
5801 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5803 _gtk_file_chooser_entry_set_file_part (entry, "");
5804 change_folder_and_display_error (impl, path);
5807 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
5809 /* The folder already exists, so we do not need to create it.
5810 * Just respond to terminate the dialog.
5817 if (impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
5818 && g_error_matches (error, GTK_FILE_SYSTEM_ERROR, GTK_FILE_SYSTEM_ERROR_NOT_FOLDER))
5820 /* Oops, the user typed the name of an existing path which is not a folder */
5821 error_creating_folder_over_existing_file_dialog (impl, path, error);
5822 error = NULL; /* as it will be freed below for the general case */
5827 GtkFilePath *parent_path;
5829 /* check that everything up to the last component exists */
5831 parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5832 is_folder = check_is_folder (impl->file_system, parent_path, NULL);
5835 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5837 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
5839 GError *create_error;
5841 create_error = NULL;
5842 if (gtk_file_system_create_folder (impl->file_system, path, &create_error))
5846 error_creating_folder_dialog (impl, path, create_error);
5853 /* This will display an error, which is what we want */
5854 change_folder_and_display_error (impl, parent_path);
5858 gtk_file_path_free (parent_path);
5862 g_error_free (error);
5865 gtk_file_path_free (path);
5868 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
5870 /* The focus is on a dialog's action area button, *and* the widget that
5871 * was focused immediately before it is the shortcuts list. Switch to the
5872 * selected shortcut and tell the caller not to respond.
5876 if (shortcuts_get_selected (impl, &iter))
5878 shortcuts_activate_iter (impl, &iter);
5880 gtk_widget_grab_focus (impl->browse_files_tree_view);
5887 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
5889 /* The focus is on a dialog's action area button, *and* the widget that
5890 * was focused immediately before it is the file list.
5895 /* The focus is on a dialog's action area button or something else */
5896 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5897 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5902 g_assert_not_reached ();
5906 /* Implementation for GtkFileChooserEmbed::initial_focus() */
5908 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
5910 GtkFileChooserDefault *impl;
5913 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5915 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5916 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5917 widget = impl->browse_files_tree_view;
5918 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5919 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5920 widget = impl->save_file_name_entry;
5923 g_assert_not_reached ();
5927 gtk_widget_grab_focus (widget);
5931 set_current_filter (GtkFileChooserDefault *impl,
5932 GtkFileFilter *filter)
5934 if (impl->current_filter != filter)
5938 /* NULL filters are allowed to reset to non-filtered status
5940 filter_index = g_slist_index (impl->filters, filter);
5941 if (impl->filters && filter && filter_index < 0)
5944 if (impl->current_filter)
5945 g_object_unref (impl->current_filter);
5946 impl->current_filter = filter;
5947 if (impl->current_filter)
5949 g_object_ref (impl->current_filter);
5950 gtk_object_sink (GTK_OBJECT (filter));
5954 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
5957 if (impl->browse_files_model)
5958 install_list_model_filter (impl);
5960 g_object_notify (G_OBJECT (impl), "filter");
5965 filter_combo_changed (GtkComboBox *combo_box,
5966 GtkFileChooserDefault *impl)
5968 gint new_index = gtk_combo_box_get_active (combo_box);
5969 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
5971 set_current_filter (impl, new_filter);
5975 check_preview_change (GtkFileChooserDefault *impl)
5977 GtkTreePath *cursor_path;
5978 const GtkFilePath *new_path;
5979 const GtkFileInfo *new_info;
5981 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
5982 if (cursor_path && impl->sort_model)
5985 GtkTreeIter child_iter;
5987 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
5988 gtk_tree_path_free (cursor_path);
5990 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5992 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5993 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6001 if (new_path != impl->preview_path &&
6002 !(new_path && impl->preview_path &&
6003 gtk_file_path_compare (new_path, impl->preview_path) == 0))
6005 if (impl->preview_path)
6007 gtk_file_path_free (impl->preview_path);
6008 g_free (impl->preview_display_name);
6013 impl->preview_path = gtk_file_path_copy (new_path);
6014 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
6018 impl->preview_path = NULL;
6019 impl->preview_display_name = NULL;
6022 if (impl->use_preview_label && impl->preview_label)
6023 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
6025 g_signal_emit_by_name (impl, "update-preview");
6029 /* Activates a volume by mounting it if necessary and then switching to its
6033 shortcuts_activate_volume (GtkFileChooserDefault *impl,
6034 GtkFileSystemVolume *volume)
6038 /* We ref the file chooser since volume_mount() may run a main loop, and the
6039 * user could close the file chooser window in the meantime.
6041 g_object_ref (impl);
6043 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
6048 set_busy_cursor (impl, TRUE);
6051 result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
6057 msg = g_strdup_printf (_("Could not mount %s"),
6058 gtk_file_system_volume_get_display_name (impl->file_system, volume));
6059 error_message (impl, msg, error->message);
6061 g_error_free (error);
6064 set_busy_cursor (impl, FALSE);
6070 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
6071 change_folder_and_display_error (impl, path);
6072 gtk_file_path_free (path);
6076 g_object_unref (impl);
6079 /* Opens the folder or volume at the specified iter in the shortcuts model */
6081 shortcuts_activate_iter (GtkFileChooserDefault *impl,
6087 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
6088 SHORTCUTS_COL_DATA, &col_data,
6089 SHORTCUTS_COL_IS_VOLUME, &is_volume,
6093 return; /* We are on a separator */
6097 GtkFileSystemVolume *volume;
6101 shortcuts_activate_volume (impl, volume);
6105 const GtkFilePath *file_path;
6107 file_path = col_data;
6108 change_folder_and_display_error (impl, file_path);
6112 /* Callback used when a row in the shortcuts list is activated */
6114 shortcuts_row_activated_cb (GtkTreeView *tree_view,
6116 GtkTreeViewColumn *column,
6117 GtkFileChooserDefault *impl)
6120 GtkTreeIter child_iter;
6122 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
6125 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
6128 shortcuts_activate_iter (impl, &child_iter);
6130 gtk_widget_grab_focus (impl->browse_files_tree_view);
6133 /* Handler for GtkWidget::key-press-event on the shortcuts list */
6135 shortcuts_key_press_event_cb (GtkWidget *widget,
6137 GtkFileChooserDefault *impl)
6141 modifiers = gtk_accelerator_get_default_mod_mask ();
6143 if ((event->keyval == GDK_BackSpace
6144 || event->keyval == GDK_Delete
6145 || event->keyval == GDK_KP_Delete)
6146 && (event->state & modifiers) == 0)
6148 remove_selected_bookmarks (impl);
6156 shortcuts_select_func (GtkTreeSelection *selection,
6157 GtkTreeModel *model,
6159 gboolean path_currently_selected,
6162 GtkFileChooserDefault *impl = data;
6164 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
6168 list_select_func (GtkTreeSelection *selection,
6169 GtkTreeModel *model,
6171 gboolean path_currently_selected,
6174 GtkFileChooserDefault *impl = data;
6176 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6177 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6179 GtkTreeIter iter, child_iter;
6180 const GtkFileInfo *info;
6182 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
6185 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
6187 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6189 if (info && !gtk_file_info_get_is_folder (info))
6197 list_selection_changed (GtkTreeSelection *selection,
6198 GtkFileChooserDefault *impl)
6200 /* See if we are in the new folder editable row for Save mode */
6201 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6203 GtkTreeSelection *selection;
6204 GtkTreeIter iter, child_iter;
6205 const GtkFileInfo *info;
6207 g_assert (!impl->select_multiple);
6208 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6209 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
6210 goto out; /* normal processing */
6212 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6216 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6218 return; /* We are on the editable row for New Folder */
6223 update_chooser_entry (impl);
6224 check_preview_change (impl);
6225 bookmarks_check_add_sensitivity (impl);
6227 g_signal_emit_by_name (impl, "selection-changed", 0);
6230 /* Callback used when a row in the file list is activated */
6232 list_row_activated (GtkTreeView *tree_view,
6234 GtkTreeViewColumn *column,
6235 GtkFileChooserDefault *impl)
6237 GtkTreeIter iter, child_iter;
6238 const GtkFileInfo *info;
6240 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
6243 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
6245 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6247 if (gtk_file_info_get_is_folder (info))
6249 const GtkFilePath *file_path;
6251 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
6252 change_folder_and_display_error (impl, file_path);
6257 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6258 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6259 g_signal_emit_by_name (impl, "file-activated");
6263 path_bar_clicked (GtkPathBar *path_bar,
6264 GtkFilePath *file_path,
6265 gboolean child_is_hidden,
6266 GtkFileChooserDefault *impl)
6268 if (!change_folder_and_display_error (impl, file_path))
6271 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
6272 * show hidden files so that ".baz" appears in the file list, as it will still
6273 * be shown in the path bar: "/foo/[bar]/.baz"
6275 if (child_is_hidden)
6276 g_object_set (impl, "show-hidden", TRUE, NULL);
6279 static const GtkFileInfo *
6280 get_list_file_info (GtkFileChooserDefault *impl,
6283 GtkTreeIter child_iter;
6285 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6289 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6293 list_icon_data_func (GtkTreeViewColumn *tree_column,
6294 GtkCellRenderer *cell,
6295 GtkTreeModel *tree_model,
6299 GtkFileChooserDefault *impl = data;
6300 GtkTreeIter child_iter;
6301 const GtkFilePath *path;
6303 const GtkFileInfo *info;
6304 gboolean sensitive = TRUE;
6306 info = get_list_file_info (impl, iter);
6308 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6311 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
6315 /* FIXME: NULL GError */
6316 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
6317 impl->icon_size, NULL);
6321 /* We are on the editable row */
6325 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6326 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
6327 sensitive = gtk_file_info_get_is_folder (info);
6331 "sensitive", sensitive,
6335 g_object_unref (pixbuf);
6339 list_name_data_func (GtkTreeViewColumn *tree_column,
6340 GtkCellRenderer *cell,
6341 GtkTreeModel *tree_model,
6345 GtkFileChooserDefault *impl = data;
6346 const GtkFileInfo *info = get_list_file_info (impl, iter);
6347 gboolean sensitive = TRUE;
6352 "text", _("Type name of new folder"),
6359 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6360 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6362 sensitive = gtk_file_info_get_is_folder (info);
6366 "text", gtk_file_info_get_display_name (info),
6367 "sensitive", sensitive,
6373 list_size_data_func (GtkTreeViewColumn *tree_column,
6374 GtkCellRenderer *cell,
6375 GtkTreeModel *tree_model,
6379 GtkFileChooserDefault *impl = data;
6380 const GtkFileInfo *info = get_list_file_info (impl, iter);
6383 gboolean sensitive = TRUE;
6385 if (!info || gtk_file_info_get_is_folder (info))
6387 g_object_set (cell,"sensitive", sensitive, NULL);
6391 size = gtk_file_info_get_size (info);
6393 if (size < (gint64)1024)
6394 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
6395 else if (size < (gint64)1024*1024)
6396 str = g_strdup_printf (_("%.1f K"), size / (1024.));
6397 else if (size < (gint64)1024*1024*1024)
6398 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
6400 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
6402 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6403 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6408 "sensitive", sensitive,
6415 /* Tree column data callback for the file list; fetches the mtime of a file */
6417 list_mtime_data_func (GtkTreeViewColumn *tree_column,
6418 GtkCellRenderer *cell,
6419 GtkTreeModel *tree_model,
6423 GtkFileChooserDefault *impl;
6424 const GtkFileInfo *info;
6425 GtkFileTime time_mtime, time_now;
6429 gboolean sensitive = TRUE;
6433 info = get_list_file_info (impl, iter);
6443 time_mtime = gtk_file_info_get_modification_time (info);
6445 if (time_mtime == 0)
6446 strcpy (buf, _("Unknown"));
6449 g_date_set_time (&mtime, (GTime) time_mtime);
6451 time_now = (GTime ) time (NULL);
6452 g_date_set_time (&now, (GTime) time_now);
6454 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
6457 strcpy (buf, _("Today"));
6458 else if (days_diff == 1)
6459 strcpy (buf, _("Yesterday"));
6464 if (days_diff > 1 && days_diff < 7)
6465 format = "%A"; /* Days from last week */
6467 format = "%x"; /* Any other date */
6469 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
6470 strcpy (buf, _("Unknown"));
6474 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6475 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6476 sensitive = gtk_file_info_get_is_folder (info);
6480 "sensitive", sensitive,
6485 _gtk_file_chooser_default_new (const char *file_system)
6487 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
6488 "file-system-backend", file_system,
6493 location_entry_create (GtkFileChooserDefault *impl,
6498 entry = _gtk_file_chooser_entry_new (TRUE);
6499 /* Pick a good width for the entry */
6500 gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
6501 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
6502 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
6503 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (entry), impl->action);
6506 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry),
6507 gtk_file_path_new_steal ((gchar *)path));
6508 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), path);
6512 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
6513 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6514 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6515 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), "");
6516 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6517 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6518 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry),
6519 gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
6521 g_assert_not_reached ();
6524 return GTK_WIDGET (entry);
6528 update_from_entry (GtkFileChooserDefault *impl,
6530 GtkFileChooserEntry *chooser_entry)
6532 const GtkFilePath *folder_path;
6533 const char *file_part;
6535 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6536 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6538 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
6540 error_message_with_parent (parent,
6541 _("Cannot change folder"),
6542 _("The folder you specified is an invalid path."));
6546 if (file_part[0] == '\0')
6547 return change_folder_and_display_error (impl, folder_path);
6550 GtkFileFolder *folder = NULL;
6551 GtkFilePath *subfolder_path = NULL;
6552 GtkFileInfo *info = NULL;
6558 /* If the file part is non-empty, we need to figure out if it refers to a
6559 * folder within folder. We could optimize the case here where the folder
6560 * is already loaded for one of our tree models.
6564 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
6568 error_getting_info_dialog (impl, folder_path, error);
6573 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
6575 if (!subfolder_path)
6580 uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
6581 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s'"),
6583 error_message (impl, msg, error->message);
6590 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
6594 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6595 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6597 if (!change_folder_and_display_error (impl, folder_path))
6600 gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
6603 error_getting_info_dialog (impl, subfolder_path, error);
6608 if (gtk_file_info_get_is_folder (info))
6609 result = change_folder_and_display_error (impl, subfolder_path);
6615 result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
6617 error_dialog (impl, _("Could not select item"),
6618 subfolder_path, error);
6624 g_object_unref (folder);
6626 gtk_file_path_free (subfolder_path);
6629 gtk_file_info_free (info);
6634 g_assert_not_reached ();
6638 location_popup_handler (GtkFileChooserDefault *impl,
6642 GtkWindow *toplevel;
6648 const char *accept_stock;
6652 toplevel = get_toplevel (GTK_WIDGET (impl));
6654 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6655 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6657 title = _("Open Location");
6658 accept_stock = GTK_STOCK_OPEN;
6662 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6663 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6664 title = _("Save in Location");
6665 accept_stock = GTK_STOCK_SAVE;
6668 dialog = gtk_dialog_new_with_buttons (title,
6670 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
6671 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
6672 accept_stock, GTK_RESPONSE_ACCEPT,
6674 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
6675 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
6676 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
6677 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
6679 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
6680 GTK_RESPONSE_ACCEPT,
6681 GTK_RESPONSE_CANCEL,
6684 hbox = gtk_hbox_new (FALSE, 12);
6685 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
6686 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
6688 label = gtk_label_new_with_mnemonic (_("_Location:"));
6689 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
6691 entry = location_entry_create (impl, path);
6693 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
6694 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
6698 gtk_widget_show_all (dialog);
6699 /* If the dialog is brought up by typing the first characters
6700 * of a path, unselect the text in the entry, so that you can
6701 * just type on without erasing the initial part.
6704 gtk_editable_select_region (GTK_EDITABLE (entry), -1, -1);
6708 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
6710 if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
6712 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6713 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6715 gtk_widget_grab_focus (impl->browse_files_tree_view);
6719 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6720 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6721 gtk_widget_grab_focus (impl->save_file_name_entry);
6729 GtkWindow *toplevel;
6731 toplevel = get_toplevel (GTK_WIDGET (impl));
6732 if (toplevel && toplevel->focus_widget)
6733 gtk_widget_grab_focus (toplevel->focus_widget);
6736 gtk_widget_destroy (dialog);
6739 /* Handler for the "up-folder" keybinding signal */
6741 up_folder_handler (GtkFileChooserDefault *impl)
6743 pending_select_paths_add (impl, impl->current_folder);
6744 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
6747 /* Handler for the "down-folder" keybinding signal */
6749 down_folder_handler (GtkFileChooserDefault *impl)
6751 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
6754 /* Handler for the "home-folder" keybinding signal */
6756 home_folder_handler (GtkFileChooserDefault *impl)
6761 if (!impl->has_home)
6762 return; /* Should we put up an error dialog? */
6764 pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
6765 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
6766 g_assert_not_reached ();
6768 shortcuts_activate_iter (impl, &iter);
6773 /* Drag and drop interfaces */
6776 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
6781 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
6786 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
6788 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
6791 ShortcutsModelFilter *model;
6795 model = SHORTCUTS_MODEL_FILTER (drag_source);
6797 pos = *gtk_tree_path_get_indices (path);
6798 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
6800 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
6803 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
6805 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
6807 GtkSelectionData *selection_data)
6809 ShortcutsModelFilter *model;
6811 model = SHORTCUTS_MODEL_FILTER (drag_source);
6818 /* Fill the GtkTreeDragSourceIface vtable */
6820 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
6822 iface->row_draggable = shortcuts_model_filter_row_draggable;
6823 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
6827 /* Fill the GtkTreeDragDestIface vtable */
6829 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
6831 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
6832 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
6836 static GtkTreeModel *
6837 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
6838 GtkTreeModel *child_model,
6841 ShortcutsModelFilter *model;
6843 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
6844 "child-model", child_model,
6845 "virtual-root", root,
6850 return GTK_TREE_MODEL (model);
6853 #define __GTK_FILE_CHOOSER_DEFAULT_C__
6854 #include "gtkaliasdef.c"