1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "gdk/gdkkeysyms.h"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcellrenderertext.h"
31 #include "gtkcheckmenuitem.h"
32 #include "gtkcombobox.h"
34 #include "gtkeventbox.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserdefault.h"
37 #include "gtkfilechooserembed.h"
38 #include "gtkfilechooserentry.h"
39 #include "gtkfilechooserutils.h"
40 #include "gtkfilechooser.h"
41 #include "gtkfilesystemmodel.h"
44 #include "gtkhpaned.h"
45 #include "gtkiconfactory.h"
46 #include "gtkicontheme.h"
48 #include "gtkimagemenuitem.h"
51 #include "gtkmarshalers.h"
52 #include "gtkmenuitem.h"
53 #include "gtkmessagedialog.h"
54 #include "gtkpathbar.h"
55 #include "gtkprivate.h"
56 #include "gtkscrolledwindow.h"
57 #include "gtkseparatormenuitem.h"
58 #include "gtksizegroup.h"
61 #include "gtktreednd.h"
62 #include "gtktreeprivate.h"
63 #include "gtktreeview.h"
64 #include "gtktreemodelsort.h"
65 #include "gtktreeselection.h"
66 #include "gtktreestore.h"
67 #include "gtktooltips.h"
68 #include "gtktypebuiltins.h"
71 #if defined (G_OS_UNIX)
72 #include "gtkfilesystemunix.h"
73 #elif defined (G_OS_WIN32)
74 #include "gtkfilesystemwin32.h"
81 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
83 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
84 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
85 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
92 #define MAX_LOADING_TIME 500
94 struct _GtkFileChooserDefaultClass
96 GtkVBoxClass parent_class;
101 PENDING_OP_SELECT_PATH,
102 PENDING_OP_SELECT_FIRST
105 struct _GtkFileChooserDefault
107 GtkVBox parent_instance;
109 GtkFileChooserAction action;
111 GtkFileSystem *file_system;
113 /* Save mode widgets */
114 GtkWidget *save_widgets;
116 GtkWidget *save_file_name_entry;
117 GtkWidget *save_folder_label;
118 GtkWidget *save_folder_combo;
119 GtkWidget *save_expander;
121 /* The file browsing widgets */
122 GtkWidget *browse_widgets;
123 GtkWidget *browse_shortcuts_tree_view;
124 GtkWidget *browse_shortcuts_add_button;
125 GtkWidget *browse_shortcuts_remove_button;
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 PendingOp pending_op;
152 GtkFilePath *pending_select_path;
154 GtkFileFilter *current_filter;
157 GtkTooltips *tooltips;
160 gboolean has_desktop;
166 gulong volumes_changed_id;
167 gulong bookmarks_changed_id;
169 GtkFilePath *current_volume_path;
170 GtkFilePath *current_folder;
171 GtkFilePath *preview_path;
172 char *preview_display_name;
174 GtkTreeViewColumn *list_name_column;
175 GtkCellRenderer *list_name_renderer;
177 GSource *edited_idle;
178 char *edited_new_text;
180 gulong settings_signal_id;
183 gulong toplevel_set_focus_id;
184 GtkWidget *toplevel_last_focus_widget;
187 GdkDragContext *shortcuts_drag_context;
188 GSource *shortcuts_drag_outside_idle;
193 guint local_only : 1;
194 guint preview_widget_active : 1;
195 guint use_preview_label : 1;
196 guint select_multiple : 1;
197 guint show_hidden : 1;
198 guint list_sort_ascending : 1;
199 guint changing_folder : 1;
200 guint shortcuts_current_folder_active : 1;
203 guint shortcuts_drag_outside : 1;
216 static guint signals[LAST_SIGNAL] = { 0 };
218 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
220 SHORTCUTS_COL_PIXBUF,
223 SHORTCUTS_COL_IS_VOLUME,
224 SHORTCUTS_COL_REMOVABLE,
225 SHORTCUTS_COL_PIXBUF_VISIBLE,
226 SHORTCUTS_COL_NUM_COLUMNS
229 /* Column numbers for the file list */
234 FILE_LIST_COL_NUM_COLUMNS
237 /* Identifiers for target types */
243 /* Target types for dragging from the shortcuts list */
244 static GtkTargetEntry shortcuts_source_targets[] = {
245 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
248 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
249 / sizeof (shortcuts_source_targets[0]));
251 /* Target types for dropping into the shortcuts list */
252 static GtkTargetEntry shortcuts_dest_targets[] = {
253 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
254 { "text/uri-list", 0, TEXT_URI_LIST }
257 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
258 / sizeof (shortcuts_dest_targets[0]));
260 /* Target types for DnD from the file list */
261 static GtkTargetEntry file_list_source_targets[] = {
262 { "text/uri-list", 0, TEXT_URI_LIST }
265 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
266 / sizeof (file_list_source_targets[0]));
268 /* Interesting places in the shortcuts bar */
274 SHORTCUTS_BOOKMARKS_SEPARATOR,
276 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
277 SHORTCUTS_CURRENT_FOLDER
280 /* Icon size for if we can't get it from the theme */
281 #define FALLBACK_ICON_SIZE 20
283 #define PREVIEW_HBOX_SPACING 12
287 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
288 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
289 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
290 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
292 static GObject* gtk_file_chooser_default_constructor (GType type,
293 guint n_construct_properties,
294 GObjectConstructParam *construct_params);
295 static void gtk_file_chooser_default_finalize (GObject *object);
296 static void gtk_file_chooser_default_set_property (GObject *object,
300 static void gtk_file_chooser_default_get_property (GObject *object,
304 static void gtk_file_chooser_default_dispose (GObject *object);
305 static void gtk_file_chooser_default_show_all (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 void shortcuts_activate_iter (GtkFileChooserDefault *impl,
380 static int shortcuts_get_index (GtkFileChooserDefault *impl,
381 ShortcutsIndex where);
382 static int shortcut_find_position (GtkFileChooserDefault *impl,
383 const GtkFilePath *path);
385 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
387 static gboolean list_select_func (GtkTreeSelection *selection,
390 gboolean path_currently_selected,
393 static void list_selection_changed (GtkTreeSelection *tree_selection,
394 GtkFileChooserDefault *impl);
395 static void list_row_activated (GtkTreeView *tree_view,
397 GtkTreeViewColumn *column,
398 GtkFileChooserDefault *impl);
400 static void select_func (GtkFileSystemModel *model,
405 static void path_bar_clicked (GtkPathBar *path_bar,
406 GtkFilePath *file_path,
407 gboolean child_is_hidden,
408 GtkFileChooserDefault *impl);
410 static void add_bookmark_button_clicked_cb (GtkButton *button,
411 GtkFileChooserDefault *impl);
412 static void remove_bookmark_button_clicked_cb (GtkButton *button,
413 GtkFileChooserDefault *impl);
415 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
416 GtkCellRenderer *cell,
417 GtkTreeModel *tree_model,
420 static void list_name_data_func (GtkTreeViewColumn *tree_column,
421 GtkCellRenderer *cell,
422 GtkTreeModel *tree_model,
426 static void list_size_data_func (GtkTreeViewColumn *tree_column,
427 GtkCellRenderer *cell,
428 GtkTreeModel *tree_model,
432 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
433 GtkCellRenderer *cell,
434 GtkTreeModel *tree_model,
438 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
441 static void load_remove_timer (GtkFileChooserDefault *impl);
443 static GObjectClass *parent_class;
447 /* Drag and drop interface declarations */
450 GtkTreeModelFilter parent;
452 GtkFileChooserDefault *impl;
453 } ShortcutsModelFilter;
456 GtkTreeModelFilterClass parent_class;
457 } ShortcutsModelFilterClass;
459 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
460 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
462 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
464 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
465 _shortcuts_model_filter,
466 GTK_TYPE_TREE_MODEL_FILTER,
467 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
468 shortcuts_model_filter_drag_source_iface_init));
470 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
471 GtkTreeModel *child_model,
477 _gtk_file_chooser_default_get_type (void)
479 static GType file_chooser_default_type = 0;
481 if (!file_chooser_default_type)
483 static const GTypeInfo file_chooser_default_info =
485 sizeof (GtkFileChooserDefaultClass),
486 NULL, /* base_init */
487 NULL, /* base_finalize */
488 (GClassInitFunc) gtk_file_chooser_default_class_init,
489 NULL, /* class_finalize */
490 NULL, /* class_data */
491 sizeof (GtkFileChooserDefault),
493 (GInstanceInitFunc) gtk_file_chooser_default_init,
496 static const GInterfaceInfo file_chooser_info =
498 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
499 NULL, /* interface_finalize */
500 NULL /* interface_data */
503 static const GInterfaceInfo file_chooser_embed_info =
505 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
506 NULL, /* interface_finalize */
507 NULL /* interface_data */
510 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
511 &file_chooser_default_info, 0);
513 g_type_add_interface_static (file_chooser_default_type,
514 GTK_TYPE_FILE_CHOOSER,
516 g_type_add_interface_static (file_chooser_default_type,
517 GTK_TYPE_FILE_CHOOSER_EMBED,
518 &file_chooser_embed_info);
521 return file_chooser_default_type;
525 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
527 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
528 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
529 GtkBindingSet *binding_set;
531 parent_class = g_type_class_peek_parent (class);
533 gobject_class->finalize = gtk_file_chooser_default_finalize;
534 gobject_class->constructor = gtk_file_chooser_default_constructor;
535 gobject_class->set_property = gtk_file_chooser_default_set_property;
536 gobject_class->get_property = gtk_file_chooser_default_get_property;
537 gobject_class->dispose = gtk_file_chooser_default_dispose;
539 widget_class->show_all = gtk_file_chooser_default_show_all;
540 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
541 widget_class->style_set = gtk_file_chooser_default_style_set;
542 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
544 signals[LOCATION_POPUP] =
545 _gtk_binding_signal_new ("location-popup",
546 G_OBJECT_CLASS_TYPE (class),
547 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
548 G_CALLBACK (location_popup_handler),
550 _gtk_marshal_VOID__STRING,
551 G_TYPE_NONE, 1, G_TYPE_STRING);
553 _gtk_binding_signal_new ("up-folder",
554 G_OBJECT_CLASS_TYPE (class),
555 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
556 G_CALLBACK (up_folder_handler),
558 _gtk_marshal_VOID__VOID,
560 signals[DOWN_FOLDER] =
561 _gtk_binding_signal_new ("down-folder",
562 G_OBJECT_CLASS_TYPE (class),
563 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
564 G_CALLBACK (down_folder_handler),
566 _gtk_marshal_VOID__VOID,
568 signals[HOME_FOLDER] =
569 _gtk_binding_signal_new ("home-folder",
570 G_OBJECT_CLASS_TYPE (class),
571 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
572 G_CALLBACK (home_folder_handler),
574 _gtk_marshal_VOID__VOID,
577 binding_set = gtk_binding_set_by_class (class);
579 gtk_binding_entry_add_signal (binding_set,
580 GDK_l, GDK_CONTROL_MASK,
582 1, G_TYPE_STRING, "");
584 gtk_binding_entry_add_signal (binding_set,
587 1, G_TYPE_STRING, "/");
589 gtk_binding_entry_add_signal (binding_set,
590 GDK_Up, GDK_MOD1_MASK,
593 gtk_binding_entry_add_signal (binding_set,
597 gtk_binding_entry_add_signal (binding_set,
598 GDK_KP_Up, GDK_MOD1_MASK,
602 gtk_binding_entry_add_signal (binding_set,
603 GDK_Down, GDK_MOD1_MASK,
606 gtk_binding_entry_add_signal (binding_set,
607 GDK_KP_Down, GDK_MOD1_MASK,
611 gtk_binding_entry_add_signal (binding_set,
612 GDK_Home, GDK_MOD1_MASK,
615 gtk_binding_entry_add_signal (binding_set,
616 GDK_KP_Home, GDK_MOD1_MASK,
620 _gtk_file_chooser_install_properties (gobject_class);
622 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
623 P_("Default file chooser backend"),
624 P_("Name of the GtkFileChooser backend to use by default"),
630 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
632 iface->select_path = gtk_file_chooser_default_select_path;
633 iface->unselect_path = gtk_file_chooser_default_unselect_path;
634 iface->select_all = gtk_file_chooser_default_select_all;
635 iface->unselect_all = gtk_file_chooser_default_unselect_all;
636 iface->get_paths = gtk_file_chooser_default_get_paths;
637 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
638 iface->get_file_system = gtk_file_chooser_default_get_file_system;
639 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
640 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
641 iface->set_current_name = gtk_file_chooser_default_set_current_name;
642 iface->add_filter = gtk_file_chooser_default_add_filter;
643 iface->remove_filter = gtk_file_chooser_default_remove_filter;
644 iface->list_filters = gtk_file_chooser_default_list_filters;
645 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
646 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
647 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
651 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
653 iface->get_default_size = gtk_file_chooser_default_get_default_size;
654 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
655 iface->should_respond = gtk_file_chooser_default_should_respond;
656 iface->initial_focus = gtk_file_chooser_default_initial_focus;
659 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
661 impl->local_only = TRUE;
662 impl->preview_widget_active = TRUE;
663 impl->use_preview_label = TRUE;
664 impl->select_multiple = FALSE;
665 impl->show_hidden = FALSE;
666 impl->icon_size = FALLBACK_ICON_SIZE;
667 impl->load_state = LOAD_FINISHED;
668 impl->pending_op = PENDING_OP_NONE;
669 impl->pending_select_path = NULL;
671 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (impl), TRUE);
672 gtk_box_set_spacing (GTK_BOX (impl), 12);
674 impl->tooltips = gtk_tooltips_new ();
675 g_object_ref (impl->tooltips);
676 gtk_object_sink (GTK_OBJECT (impl->tooltips));
679 /* Frees the data columns for the specified iter in the shortcuts model*/
681 shortcuts_free_row_data (GtkFileChooserDefault *impl,
687 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
688 SHORTCUTS_COL_DATA, &col_data,
689 SHORTCUTS_COL_IS_VOLUME, &is_volume,
696 GtkFileSystemVolume *volume;
699 gtk_file_system_volume_free (impl->file_system, volume);
706 gtk_file_path_free (path);
710 /* Frees all the data columns in the shortcuts model */
712 shortcuts_free (GtkFileChooserDefault *impl)
716 if (!impl->shortcuts_model)
719 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
722 shortcuts_free_row_data (impl, &iter);
724 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
726 g_object_unref (impl->shortcuts_model);
727 impl->shortcuts_model = NULL;
731 gtk_file_chooser_default_finalize (GObject *object)
733 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
736 if (impl->shortcuts_filter_model)
737 g_object_unref (impl->shortcuts_filter_model);
739 shortcuts_free (impl);
741 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
742 impl->volumes_changed_id = 0;
743 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
744 impl->bookmarks_changed_id = 0;
745 g_object_unref (impl->file_system);
747 for (l = impl->filters; l; l = l->next)
749 GtkFileFilter *filter;
751 filter = GTK_FILE_FILTER (l->data);
752 g_object_unref (filter);
754 g_slist_free (impl->filters);
756 if (impl->current_filter)
757 g_object_unref (impl->current_filter);
759 if (impl->current_volume_path)
760 gtk_file_path_free (impl->current_volume_path);
762 if (impl->current_folder)
763 gtk_file_path_free (impl->current_folder);
765 if (impl->preview_path)
766 gtk_file_path_free (impl->preview_path);
768 if (impl->pending_op == PENDING_OP_SELECT_PATH)
770 g_assert (impl->pending_select_path != NULL);
771 gtk_file_path_free (impl->pending_select_path);
774 load_remove_timer (impl);
776 /* Free all the Models we have */
777 if (impl->browse_files_model)
778 g_object_unref (impl->browse_files_model);
780 if (impl->sort_model)
781 g_object_unref (impl->sort_model);
783 g_free (impl->preview_display_name);
785 g_free (impl->edited_new_text);
787 g_object_unref (impl->tooltips);
789 G_OBJECT_CLASS (parent_class)->finalize (object);
792 /* Shows an error dialog set as transient for the specified window */
794 error_message_with_parent (GtkWindow *parent,
799 dialog = gtk_message_dialog_new (parent,
800 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
805 gtk_dialog_run (GTK_DIALOG (dialog));
806 gtk_widget_destroy (dialog);
809 /* Returns a toplevel GtkWindow, or NULL if none */
811 get_toplevel (GtkWidget *widget)
815 toplevel = gtk_widget_get_toplevel (widget);
816 if (!GTK_WIDGET_TOPLEVEL (toplevel))
819 return GTK_WINDOW (toplevel);
822 /* Shows an error dialog for the file chooser */
824 error_message (GtkFileChooserDefault *impl,
827 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg);
830 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
832 error_dialog (GtkFileChooserDefault *impl,
834 const GtkFilePath *path,
837 g_return_if_fail (path != NULL);
841 char *uri = gtk_file_system_path_to_uri (impl->file_system, path);
842 char *text = g_strdup_printf (msg,
845 error_message (impl, text);
848 g_error_free (error);
852 /* Displays an error message about not being able to get information for a file.
853 * Frees the GError as well.
856 error_getting_info_dialog (GtkFileChooserDefault *impl,
857 const GtkFilePath *path,
861 _("Could not retrieve information about %s:\n%s"),
865 /* Shows an error dialog about not being able to add a bookmark */
867 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
868 const GtkFilePath *path,
872 _("Could not add a bookmark for %s:\n%s"),
876 /* Shows an error dialog about not being able to compose a filename */
878 error_building_filename_dialog (GtkFileChooserDefault *impl,
879 const GtkFilePath *base_path,
880 const char *file_part,
886 uri = gtk_file_system_path_to_uri (impl->file_system, base_path);
887 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
890 error_message (impl, msg);
893 g_error_free (error);
896 /* Shows an error dialog when we cannot switch to a folder */
898 error_changing_folder_dialog (GtkFileChooserDefault *impl,
899 const GtkFilePath *path,
903 _("Could not change the current folder to %s:\n%s"),
908 /* Changes folders, displaying an error dialog if this fails */
910 change_folder_and_display_error (GtkFileChooserDefault *impl,
911 const GtkFilePath *path)
915 GtkFilePath *path_copy;
917 /* We copy the path because of this case:
919 * list_row_activated()
920 * fetches path from model; path belongs to the model (*)
921 * calls change_folder_and_display_error()
922 * calls _gtk_file_chooser_set_current_folder_path()
923 * changing folders fails, sets model to NULL, thus freeing the path in (*)
926 path_copy = gtk_file_path_copy (path);
929 result = _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path_copy, &error);
932 error_changing_folder_dialog (impl, path_copy, error);
934 gtk_file_path_free (path_copy);
940 update_preview_widget_visibility (GtkFileChooserDefault *impl)
942 if (impl->use_preview_label)
944 if (!impl->preview_label)
946 impl->preview_label = gtk_label_new (impl->preview_display_name);
947 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
948 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
949 gtk_widget_show (impl->preview_label);
954 if (impl->preview_label)
956 gtk_widget_destroy (impl->preview_label);
957 impl->preview_label = NULL;
961 if (impl->preview_widget_active && impl->preview_widget)
962 gtk_widget_show (impl->preview_box);
964 gtk_widget_hide (impl->preview_box);
966 g_signal_emit_by_name (impl, "default-size-changed");
970 set_preview_widget (GtkFileChooserDefault *impl,
971 GtkWidget *preview_widget)
973 if (preview_widget == impl->preview_widget)
976 if (impl->preview_widget)
977 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
978 impl->preview_widget);
980 impl->preview_widget = preview_widget;
981 if (impl->preview_widget)
983 gtk_widget_show (impl->preview_widget);
984 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
985 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
986 impl->preview_widget,
987 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
990 update_preview_widget_visibility (impl);
993 /* Re-reads all the icons for the shortcuts, used when the theme changes */
995 shortcuts_reload_icons (GtkFileChooserDefault *impl)
999 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1005 gboolean pixbuf_visible;
1008 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1009 SHORTCUTS_COL_DATA, &data,
1010 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1011 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1014 if (pixbuf_visible && data)
1018 GtkFileSystemVolume *volume;
1021 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1022 impl->icon_size, NULL);
1026 const GtkFilePath *path;
1029 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1030 impl->icon_size, NULL);
1033 gtk_list_store_set (impl->shortcuts_model, &iter,
1034 SHORTCUTS_COL_PIXBUF, pixbuf,
1037 g_object_unref (pixbuf);
1039 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1042 /* If a shortcut corresponds to the current folder, selects it */
1044 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1046 GtkTreeSelection *selection;
1050 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1052 pos = shortcut_find_position (impl, impl->current_folder);
1055 gtk_tree_selection_unselect_all (selection);
1059 path = gtk_tree_path_new_from_indices (pos, -1);
1060 gtk_tree_selection_select_path (selection, path);
1061 gtk_tree_path_free (path);
1064 /* Convenience function to get the display name and icon info for a path */
1065 static GtkFileInfo *
1066 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, gboolean name_only, GError **error)
1068 GtkFilePath *parent_path;
1069 GtkFileFolder *parent_folder;
1074 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
1077 parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
1078 GTK_FILE_INFO_DISPLAY_NAME
1079 | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
1084 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, error);
1085 g_object_unref (parent_folder);
1089 gtk_file_path_free (parent_path);
1093 /* Returns whether a path is a folder */
1095 check_is_folder (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
1100 /* Use get_file_info() rather than trying get_folder() and checking
1101 * for an error directly because older versions of the gnome-vfs
1102 * backend don't return an error immediately. This way is also
1103 * more efficient if we already have the parent folder.
1105 info = get_file_info (file_system, path, FALSE, error);
1110 is_folder = gtk_file_info_get_is_folder (info);
1114 GTK_FILE_SYSTEM_ERROR,
1115 GTK_FILE_SYSTEM_ERROR_NOT_FOLDER,
1117 gtk_file_info_get_display_name (info),
1118 g_strerror (ENOTDIR));
1120 gtk_file_info_free (info);
1125 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1126 * inserts a volume. A position of -1 indicates the end of the tree.
1129 shortcuts_insert_path (GtkFileChooserDefault *impl,
1132 GtkFileSystemVolume *volume,
1133 const GtkFilePath *path,
1146 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1147 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1148 impl->icon_size, NULL);
1152 if (!check_is_folder (impl->file_system, path, error))
1156 label_copy = g_strdup (label);
1159 GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
1164 label_copy = g_strdup (gtk_file_info_get_display_name (info));
1165 gtk_file_info_free (info);
1168 data = gtk_file_path_copy (path);
1169 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1170 impl->icon_size, NULL);
1174 gtk_list_store_append (impl->shortcuts_model, &iter);
1176 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1178 gtk_list_store_set (impl->shortcuts_model, &iter,
1179 SHORTCUTS_COL_PIXBUF, pixbuf,
1180 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1181 SHORTCUTS_COL_NAME, label_copy,
1182 SHORTCUTS_COL_DATA, data,
1183 SHORTCUTS_COL_IS_VOLUME, is_volume,
1184 SHORTCUTS_COL_REMOVABLE, removable,
1187 g_free (label_copy);
1190 g_object_unref (pixbuf);
1195 /* Appends an item for the user's home directory to the shortcuts model */
1197 shortcuts_append_home (GtkFileChooserDefault *impl)
1200 GtkFilePath *home_path;
1203 home = g_get_home_dir ();
1207 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1210 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1211 if (!impl->has_home)
1212 error_getting_info_dialog (impl, home_path, error);
1214 gtk_file_path_free (home_path);
1217 /* Appends the ~/Desktop directory to the shortcuts model */
1219 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1225 home = g_get_home_dir ();
1229 name = g_build_filename (home, "Desktop", NULL);
1230 path = gtk_file_system_filename_to_path (impl->file_system, name);
1233 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1234 /* We do not actually pop up an error dialog if there is no desktop directory
1235 * because some people may really not want to have one.
1238 gtk_file_path_free (path);
1241 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1243 shortcuts_append_paths (GtkFileChooserDefault *impl,
1249 /* As there is no separator now, we want to start there.
1251 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1254 for (; paths; paths = paths->next)
1262 if (impl->local_only &&
1263 !gtk_file_system_path_is_local (impl->file_system, path))
1266 /* NULL GError, but we don't really want to show error boxes here */
1267 if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, NULL, TRUE, NULL))
1271 return num_inserted;
1274 /* Returns the index for the corresponding item in the shortcuts bar */
1276 shortcuts_get_index (GtkFileChooserDefault *impl,
1277 ShortcutsIndex where)
1283 if (where == SHORTCUTS_HOME)
1286 n += impl->has_home ? 1 : 0;
1288 if (where == SHORTCUTS_DESKTOP)
1291 n += impl->has_desktop ? 1 : 0;
1293 if (where == SHORTCUTS_VOLUMES)
1296 n += impl->num_volumes;
1298 if (where == SHORTCUTS_SHORTCUTS)
1301 n += impl->num_shortcuts;
1303 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1306 /* If there are no bookmarks there won't be a separator */
1307 n += (impl->num_bookmarks > 0) ? 1 : 0;
1309 if (where == SHORTCUTS_BOOKMARKS)
1312 n += impl->num_bookmarks;
1314 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1319 if (where == SHORTCUTS_CURRENT_FOLDER)
1322 g_assert_not_reached ();
1329 /* Removes the specified number of rows from the shortcuts list */
1331 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1337 path = gtk_tree_path_new_from_indices (start_row, -1);
1339 for (; n_rows; n_rows--)
1343 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1344 g_assert_not_reached ();
1346 shortcuts_free_row_data (impl, &iter);
1347 gtk_list_store_remove (impl->shortcuts_model, &iter);
1350 gtk_tree_path_free (path);
1353 /* Adds all the file system volumes to the shortcuts model */
1355 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1360 gboolean old_changing_folders;
1362 old_changing_folders = impl->changing_folder;
1363 impl->changing_folder = TRUE;
1365 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1366 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1367 impl->num_volumes = 0;
1369 list = gtk_file_system_list_volumes (impl->file_system);
1373 for (l = list; l; l = l->next)
1375 GtkFileSystemVolume *volume;
1379 if (impl->local_only)
1381 GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1382 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1383 gtk_file_path_free (base_path);
1387 gtk_file_system_volume_free (impl->file_system, volume);
1392 if (shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL))
1395 gtk_file_system_volume_free (impl->file_system, volume);
1398 impl->num_volumes = n;
1399 g_slist_free (list);
1401 if (impl->shortcuts_filter_model)
1402 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1404 impl->changing_folder = old_changing_folders;
1407 /* Inserts a separator node in the shortcuts list */
1409 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1410 ShortcutsIndex where)
1414 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1416 gtk_list_store_insert (impl->shortcuts_model, &iter,
1417 shortcuts_get_index (impl, where));
1418 gtk_list_store_set (impl->shortcuts_model, &iter,
1419 SHORTCUTS_COL_PIXBUF, NULL,
1420 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1421 SHORTCUTS_COL_NAME, NULL,
1422 SHORTCUTS_COL_DATA, NULL,
1426 /* Updates the list of bookmarks */
1428 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1431 gboolean old_changing_folders;
1433 old_changing_folders = impl->changing_folder;
1434 impl->changing_folder = TRUE;
1436 if (impl->num_bookmarks > 0)
1437 shortcuts_remove_rows (impl,
1438 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1439 impl->num_bookmarks + 1);
1441 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1442 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1443 gtk_file_paths_free (bookmarks);
1445 if (impl->num_bookmarks > 0)
1447 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1449 if (impl->shortcuts_filter_model)
1450 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1452 impl->changing_folder = old_changing_folders;
1455 /* Appends a separator and a row to the shortcuts list for the current folder */
1457 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1462 g_assert (!impl->shortcuts_current_folder_active);
1466 pos = shortcut_find_position (impl, impl->current_folder);
1469 GtkFileSystemVolume *volume;
1470 GtkFilePath *base_path;
1474 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1478 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1480 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1482 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1487 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1489 success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1494 success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1497 gtk_file_system_volume_free (impl->file_system, volume);
1500 gtk_file_path_free (base_path);
1503 shortcuts_remove_rows (impl, pos - 1, 1); /* remove the separator */
1505 impl->shortcuts_current_folder_active = success;
1509 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1512 /* Updates the current folder row in the shortcuts model */
1514 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1518 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1520 if (impl->shortcuts_current_folder_active)
1522 shortcuts_remove_rows (impl, pos, 2);
1523 impl->shortcuts_current_folder_active = FALSE;
1526 shortcuts_add_current_folder (impl);
1529 /* Filter function used for the shortcuts filter model */
1531 shortcuts_filter_cb (GtkTreeModel *model,
1535 GtkFileChooserDefault *impl;
1539 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1541 path = gtk_tree_model_get_path (model, iter);
1545 pos = *gtk_tree_path_get_indices (path);
1546 gtk_tree_path_free (path);
1548 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1551 /* Creates the list model for shortcuts */
1553 shortcuts_model_create (GtkFileChooserDefault *impl)
1555 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1556 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1557 GDK_TYPE_PIXBUF, /* pixbuf */
1558 G_TYPE_STRING, /* name */
1559 G_TYPE_POINTER, /* path or volume */
1560 G_TYPE_BOOLEAN, /* is the previous column a volume? */
1561 G_TYPE_BOOLEAN, /* removable */
1562 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1564 if (impl->file_system)
1566 shortcuts_append_home (impl);
1567 shortcuts_append_desktop (impl);
1568 shortcuts_add_volumes (impl);
1569 shortcuts_add_bookmarks (impl);
1572 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1573 GTK_TREE_MODEL (impl->shortcuts_model),
1576 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1577 shortcuts_filter_cb,
1582 /* Callback used when the "New Folder" button is clicked */
1584 new_folder_button_clicked (GtkButton *button,
1585 GtkFileChooserDefault *impl)
1590 if (!impl->browse_files_model)
1591 return; /* FIXME: this sucks. Disable the New Folder button or something. */
1593 /* Prevent button from being clicked twice */
1594 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
1596 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1598 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1599 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1600 path, impl->list_name_column,
1603 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1604 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1606 impl->list_name_column,
1609 gtk_tree_path_free (path);
1612 /* Idle handler for creating a new folder after editing its name cell, or for
1613 * canceling the editing.
1616 edited_idle_cb (GtkFileChooserDefault *impl)
1618 g_source_destroy (impl->edited_idle);
1619 impl->edited_idle = NULL;
1621 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1622 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1624 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
1626 if (impl->edited_new_text) /* not cancelled? */
1629 GtkFilePath *file_path;
1632 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, impl->edited_new_text,
1637 if (gtk_file_system_create_folder (impl->file_system, file_path, &error))
1638 change_folder_and_display_error (impl, file_path);
1641 _("Could not create folder %s:\n%s"),
1644 gtk_file_path_free (file_path);
1647 error_building_filename_dialog (impl, impl->current_folder, impl->edited_new_text, error);
1649 g_free (impl->edited_new_text);
1650 impl->edited_new_text = NULL;
1657 queue_edited_idle (GtkFileChooserDefault *impl,
1658 const gchar *new_text)
1660 /* We create the folder in an idle handler so that we don't modify the tree
1664 g_assert (!impl->edited_idle);
1665 g_assert (!impl->edited_new_text);
1667 impl->edited_idle = g_idle_source_new ();
1668 g_source_set_closure (impl->edited_idle,
1669 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
1671 g_source_attach (impl->edited_idle, NULL);
1674 impl->edited_new_text = g_strdup (new_text);
1677 /* Callback used from the text cell renderer when the new folder is named */
1679 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1681 const gchar *new_text,
1682 GtkFileChooserDefault *impl)
1684 /* work around bug #154921 */
1685 g_object_set (cell_renderer_text,
1686 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1687 queue_edited_idle (impl, new_text);
1690 /* Callback used from the text cell renderer when the new folder edition gets
1694 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1695 GtkFileChooserDefault *impl)
1697 /* work around bug #154921 */
1698 g_object_set (cell_renderer_text,
1699 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1700 queue_edited_idle (impl, NULL);
1703 /* Creates the widgets for the filter combo box */
1705 filter_create (GtkFileChooserDefault *impl)
1707 impl->filter_combo = gtk_combo_box_new_text ();
1708 g_signal_connect (impl->filter_combo, "changed",
1709 G_CALLBACK (filter_combo_changed), impl);
1711 return impl->filter_combo;
1715 button_new (GtkFileChooserDefault *impl,
1717 const char *stock_id,
1727 button = gtk_button_new ();
1728 hbox = gtk_hbox_new (FALSE, 2);
1729 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1731 gtk_container_add (GTK_CONTAINER (button), align);
1732 gtk_container_add (GTK_CONTAINER (align), hbox);
1733 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1735 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1737 widget = gtk_label_new_with_mnemonic (text);
1738 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1739 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1741 gtk_widget_set_sensitive (button, sensitive);
1742 g_signal_connect (button, "clicked", callback, impl);
1744 gtk_widget_show_all (align);
1747 gtk_widget_show (button);
1752 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1754 shortcut_find_position (GtkFileChooserDefault *impl,
1755 const GtkFilePath *path)
1759 int current_folder_separator_idx;
1761 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1764 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1766 for (i = 0; i < current_folder_separator_idx; i++)
1771 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1772 SHORTCUTS_COL_DATA, &col_data,
1773 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1780 GtkFileSystemVolume *volume;
1781 GtkFilePath *base_path;
1785 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1787 exists = strcmp (gtk_file_path_get_string (path),
1788 gtk_file_path_get_string (base_path)) == 0;
1796 GtkFilePath *model_path;
1798 model_path = col_data;
1800 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1805 gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1811 /* Tries to add a bookmark from a path name */
1813 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1814 const GtkFilePath *path,
1819 if (shortcut_find_position (impl, path) != -1)
1822 /* FIXME: this check really belongs in gtk_file_system_insert_bookmark. */
1824 if (!check_is_folder (impl->file_system, path, &error))
1827 _("Could not add bookmark for %s because it is not a folder."),
1834 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
1836 error_could_not_add_bookmark_dialog (impl, path, error);
1844 add_bookmark_foreach_cb (GtkTreeModel *model,
1849 GtkFileChooserDefault *impl;
1850 GtkFileSystemModel *fs_model;
1851 GtkTreeIter child_iter;
1852 const GtkFilePath *file_path;
1854 impl = (GtkFileChooserDefault *) data;
1856 fs_model = impl->browse_files_model;
1857 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1859 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
1860 shortcuts_add_bookmark_from_path (impl, file_path, -1);
1863 /* Adds a bookmark from the currently selected item in the file list */
1865 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
1867 GtkTreeSelection *selection;
1869 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1871 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1872 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
1874 gtk_tree_selection_selected_foreach (selection,
1875 add_bookmark_foreach_cb,
1879 /* Callback used when the "Add bookmark" button is clicked */
1881 add_bookmark_button_clicked_cb (GtkButton *button,
1882 GtkFileChooserDefault *impl)
1884 bookmarks_add_selected_folder (impl);
1887 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
1888 * returns FALSE if no shortcut is selected.
1891 shortcuts_get_selected (GtkFileChooserDefault *impl,
1894 GtkTreeSelection *selection;
1895 GtkTreeIter parent_iter;
1897 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1899 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
1902 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1908 /* Removes the selected bookmarks */
1910 remove_selected_bookmarks (GtkFileChooserDefault *impl)
1919 if (!shortcuts_get_selected (impl, &iter))
1922 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1923 SHORTCUTS_COL_DATA, &col_data,
1924 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1925 SHORTCUTS_COL_REMOVABLE, &removable,
1927 g_assert (col_data != NULL);
1928 g_assert (!is_volume);
1936 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1939 _("Could not remove bookmark for %s:\n%s"),
1945 /* Callback used when the "Remove bookmark" button is clicked */
1947 remove_bookmark_button_clicked_cb (GtkButton *button,
1948 GtkFileChooserDefault *impl)
1950 remove_selected_bookmarks (impl);
1953 struct selection_check_closure {
1954 GtkFileChooserDefault *impl;
1957 gboolean all_folders;
1960 /* Used from gtk_tree_selection_selected_foreach() */
1962 selection_check_foreach_cb (GtkTreeModel *model,
1967 struct selection_check_closure *closure;
1968 GtkTreeIter child_iter;
1969 const GtkFileInfo *info;
1973 closure->num_selected++;
1975 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1977 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
1978 is_folder = gtk_file_info_get_is_folder (info);
1980 closure->all_folders = closure->all_folders && is_folder;
1981 closure->all_files = closure->all_files && !is_folder;
1984 /* Checks whether the selected items in the file list are all files or all folders */
1986 selection_check (GtkFileChooserDefault *impl,
1988 gboolean *all_files,
1989 gboolean *all_folders)
1991 struct selection_check_closure closure;
1992 GtkTreeSelection *selection;
1994 closure.impl = impl;
1995 closure.num_selected = 0;
1996 closure.all_files = TRUE;
1997 closure.all_folders = TRUE;
1999 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2000 gtk_tree_selection_selected_foreach (selection,
2001 selection_check_foreach_cb,
2004 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2007 *num_selected = closure.num_selected;
2010 *all_files = closure.all_files;
2013 *all_folders = closure.all_folders;
2016 struct get_selected_path_closure {
2017 GtkFileChooserDefault *impl;
2018 const GtkFilePath *path;
2022 get_selected_path_foreach_cb (GtkTreeModel *model,
2027 struct get_selected_path_closure *closure;
2028 GtkTreeIter child_iter;
2032 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2033 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2036 /* Returns a selected path from the file list */
2037 static const GtkFilePath *
2038 get_selected_path (GtkFileChooserDefault *impl)
2040 struct get_selected_path_closure closure;
2041 GtkTreeSelection *selection;
2043 closure.impl = impl;
2044 closure.path = NULL;
2046 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2047 gtk_tree_selection_selected_foreach (selection,
2048 get_selected_path_foreach_cb,
2051 return closure.path;
2055 GtkFileChooserDefault *impl;
2057 } UpdateTooltipData;
2060 update_tooltip (GtkTreeModel *model,
2065 UpdateTooltipData *udata = data;
2066 GtkTreeIter child_iter;
2067 const GtkFileInfo *info;
2069 if (udata->tip == NULL)
2071 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2075 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2076 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2077 gtk_file_info_get_display_name (info));
2082 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2083 * if there are no selected items *and* the current folder is not in the
2084 * bookmarks list. De-sensitize the button otherwise.
2087 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2090 gboolean all_folders;
2094 selection_check (impl, &num_selected, NULL, &all_folders);
2096 if (num_selected == 0)
2097 active = (shortcut_find_position (impl, impl->current_folder) == -1);
2098 else if (num_selected == 1)
2100 const GtkFilePath *path;
2102 path = get_selected_path (impl);
2103 active = all_folders && (shortcut_find_position (impl, path) == -1);
2106 active = all_folders;
2108 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2110 if (impl->browse_files_popup_menu_add_shortcut_item)
2111 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2112 (num_selected == 0) ? FALSE : active);
2116 if (num_selected == 0)
2117 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2118 else if (num_selected > 1)
2119 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2122 GtkTreeSelection *selection;
2123 UpdateTooltipData data;
2125 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2128 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2132 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2137 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2138 * bookmark row is selected in the shortcuts tree.
2141 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2144 gboolean removable = FALSE;
2147 if (shortcuts_get_selected (impl, &iter))
2148 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2149 SHORTCUTS_COL_REMOVABLE, &removable,
2150 SHORTCUTS_COL_NAME, &name,
2153 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2159 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2160 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2168 /* GtkWidget::drag-begin handler for the shortcuts list. */
2170 shortcuts_drag_begin_cb (GtkWidget *widget,
2171 GdkDragContext *context,
2172 GtkFileChooserDefault *impl)
2175 impl->shortcuts_drag_context = g_object_ref (context);
2180 /* Removes the idle handler for outside drags */
2182 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2184 if (!impl->shortcuts_drag_outside_idle)
2187 g_source_destroy (impl->shortcuts_drag_outside_idle);
2188 impl->shortcuts_drag_outside_idle = NULL;
2192 /* GtkWidget::drag-end handler for the shortcuts list. */
2194 shortcuts_drag_end_cb (GtkWidget *widget,
2195 GdkDragContext *context,
2196 GtkFileChooserDefault *impl)
2199 g_object_unref (impl->shortcuts_drag_context);
2201 shortcuts_cancel_drag_outside_idle (impl);
2203 if (!impl->shortcuts_drag_outside)
2206 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2208 impl->shortcuts_drag_outside = FALSE;
2212 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2214 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2215 GdkDragContext *context,
2216 GtkFileChooserDefault *impl)
2218 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2222 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2226 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2229 GtkTreeView *tree_view;
2232 GdkPixmap *row_pixmap;
2237 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2239 /* Find the selected path and get its drag pixmap */
2241 if (!shortcuts_get_selected (impl, &iter))
2242 g_assert_not_reached ();
2244 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2246 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2247 gtk_tree_path_free (path);
2256 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2262 GdkPixmap *composite;
2263 int row_pixmap_width, row_pixmap_height;
2264 int pixbuf_width, pixbuf_height;
2265 int composite_width, composite_height;
2266 int pixbuf_x, pixbuf_y;
2267 GdkGC *gc, *mask_gc;
2269 GdkBitmap *pixbuf_mask;
2271 /* Create pixmap and mask for composite image */
2273 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2274 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2275 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2277 composite_width = MAX (row_pixmap_width, pixbuf_width);
2278 composite_height = MAX (row_pixmap_height, pixbuf_height);
2280 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2282 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2285 pixbuf_x = composite_width - pixbuf_width;
2287 pixbuf_y = (composite_height - pixbuf_height) / 2;
2289 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2290 gc = gdk_gc_new (composite);
2292 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2293 mask_gc = gdk_gc_new (mask);
2295 gdk_gc_set_foreground (mask_gc, &color);
2296 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2299 color.green = 0xffff;
2300 color.blue = 0xffff;
2301 gdk_gc_set_rgb_fg_color (gc, &color);
2302 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2304 /* Composite the row pixmap and the pixbuf */
2306 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2308 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2309 NULL, &pixbuf_mask, 128);
2310 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2313 pixbuf_width, pixbuf_height);
2314 g_object_unref (pixbuf_mask);
2316 gdk_draw_drawable (composite, gc, row_pixmap,
2319 row_pixmap_width, row_pixmap_height);
2321 gdk_gc_set_foreground (mask_gc, &color);
2322 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2324 gdk_draw_pixbuf (composite, gc, pixbuf,
2327 pixbuf_width, pixbuf_height,
2331 g_object_unref (pixbuf);
2332 g_object_unref (row_pixmap);
2334 row_pixmap = composite;
2338 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2340 gtk_tree_view_get_path_at_pos (tree_view,
2341 tree_view->priv->press_start_x,
2342 tree_view->priv->press_start_y,
2348 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2349 gdk_drawable_get_colormap (row_pixmap),
2352 tree_view->priv->press_start_x + 1,
2353 row_pixmap_y + cell_y + 1);
2355 g_object_unref (row_pixmap);
2357 g_object_unref (mask);
2360 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2361 * handler so that we can tell apart the drag_leave event that comes right
2362 * before a drag_drop, from a normal drag_leave. We don't want to set the
2363 * cursor nor the flag in the latter case.
2366 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2368 shortcuts_drag_set_delete_cursor (impl, TRUE);
2369 impl->shortcuts_drag_outside = TRUE;
2371 shortcuts_cancel_drag_outside_idle (impl);
2376 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2380 shortcuts_drag_leave_cb (GtkWidget *widget,
2381 GdkDragContext *context,
2383 GtkFileChooserDefault *impl)
2386 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2388 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2389 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2390 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2392 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2396 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2398 GTK_TREE_VIEW_DROP_BEFORE);
2400 g_signal_stop_emission_by_name (widget, "drag-leave");
2403 /* Computes the appropriate row and position for dropping */
2405 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2409 GtkTreeViewDropPosition *pos)
2411 GtkTreeView *tree_view;
2412 GtkTreeViewColumn *column;
2416 int bookmarks_index;
2418 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2420 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2422 if (!gtk_tree_view_get_path_at_pos (tree_view,
2424 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2430 row = bookmarks_index + impl->num_bookmarks - 1;
2431 *path = gtk_tree_path_new_from_indices (row, -1);
2432 *pos = GTK_TREE_VIEW_DROP_AFTER;
2436 row = *gtk_tree_path_get_indices (*path);
2437 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2438 gtk_tree_path_free (*path);
2440 if (row < bookmarks_index)
2442 row = bookmarks_index;
2443 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2445 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2447 row = bookmarks_index + impl->num_bookmarks - 1;
2448 *pos = GTK_TREE_VIEW_DROP_AFTER;
2452 if (cell_y < cell.height / 2)
2453 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2455 *pos = GTK_TREE_VIEW_DROP_AFTER;
2458 *path = gtk_tree_path_new_from_indices (row, -1);
2461 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2462 * implement the destination side of DnD by hand, due to limitations in
2463 * GtkTreeView's DnD API.
2466 shortcuts_drag_motion_cb (GtkWidget *widget,
2467 GdkDragContext *context,
2471 GtkFileChooserDefault *impl)
2474 GtkTreeViewDropPosition pos;
2475 GdkDragAction action;
2478 if (gtk_drag_get_source_widget (context) == widget)
2480 shortcuts_cancel_drag_outside_idle (impl);
2482 if (impl->shortcuts_drag_outside)
2484 shortcuts_drag_set_delete_cursor (impl, FALSE);
2485 impl->shortcuts_drag_outside = FALSE;
2490 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2491 action = GDK_ACTION_COPY;
2492 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2493 action = GDK_ACTION_MOVE;
2500 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2501 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2502 gtk_tree_path_free (path);
2506 g_signal_stop_emission_by_name (widget, "drag-motion");
2510 gdk_drag_status (context, action, time_);
2517 /* GtkWidget::drag-drop handler for the shortcuts list. */
2519 shortcuts_drag_drop_cb (GtkWidget *widget,
2520 GdkDragContext *context,
2524 GtkFileChooserDefault *impl)
2527 shortcuts_cancel_drag_outside_idle (impl);
2530 g_signal_stop_emission_by_name (widget, "drag-drop");
2534 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2536 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2543 uris = g_uri_list_extract_uris (data);
2545 for (i = 0; uris[i]; i++)
2551 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2555 if (shortcuts_add_bookmark_from_path (impl, path, position))
2558 gtk_file_path_free (path);
2564 msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
2566 error_message (impl, msg);
2574 /* Reorders the selected bookmark to the specified position */
2576 shortcuts_reorder (GtkFileChooserDefault *impl,
2584 int bookmarks_index;
2585 const GtkFilePath *file_path;
2586 GtkFilePath *file_path_copy;
2589 /* Get the selected path */
2591 if (!shortcuts_get_selected (impl, &iter))
2592 g_assert_not_reached ();
2594 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2595 old_position = *gtk_tree_path_get_indices (path);
2596 gtk_tree_path_free (path);
2598 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2599 old_position -= bookmarks_index;
2600 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2602 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2603 SHORTCUTS_COL_DATA, &col_data,
2604 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2606 g_assert (col_data != NULL);
2607 g_assert (!is_volume);
2609 file_path = col_data;
2610 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2612 /* Remove the path from the old position and insert it in the new one */
2614 if (new_position > old_position)
2617 if (old_position == new_position)
2621 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2622 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2624 error_could_not_add_bookmark_dialog (impl, file_path_copy, error);
2628 gtk_file_path_free (file_path_copy);
2631 /* Callback used when we get the drag data for the bookmarks list. We add the
2632 * received URIs as bookmarks if they are folders.
2635 shortcuts_drag_data_received_cb (GtkWidget *widget,
2636 GdkDragContext *context,
2639 GtkSelectionData *selection_data,
2644 GtkFileChooserDefault *impl;
2645 GtkTreePath *tree_path;
2646 GtkTreeViewDropPosition tree_pos;
2648 int bookmarks_index;
2650 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2652 /* Compute position */
2654 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2656 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2657 position = *gtk_tree_path_get_indices (tree_path);
2658 gtk_tree_path_free (tree_path);
2660 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2663 g_assert (position >= bookmarks_index);
2664 position -= bookmarks_index;
2666 if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2667 shortcuts_drop_uris (impl, selection_data->data, position);
2668 else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2669 shortcuts_reorder (impl, position);
2671 g_signal_stop_emission_by_name (widget, "drag-data-received");
2674 /* Callback used when the selection in the shortcuts tree changes */
2676 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
2677 GtkFileChooserDefault *impl)
2679 bookmarks_check_remove_sensitivity (impl);
2683 shortcuts_row_separator_func (GtkTreeModel *model,
2687 gint column = GPOINTER_TO_INT (data);
2690 gtk_tree_model_get (model, iter, column, &text, -1);
2700 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
2701 * keypresses before the TreeView gets them.
2704 tree_view_keybinding_cb (GtkWidget *tree_view,
2706 GtkFileChooserDefault *impl)
2708 if (event->keyval == GDK_slash &&
2709 ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
2711 location_popup_handler (impl, "/");
2719 /* Creates the widgets for the shortcuts and bookmarks tree */
2721 shortcuts_list_create (GtkFileChooserDefault *impl)
2724 GtkTreeSelection *selection;
2725 GtkTreeViewColumn *column;
2726 GtkCellRenderer *renderer;
2728 /* Scrolled window */
2730 swin = gtk_scrolled_window_new (NULL, NULL);
2731 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2732 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2733 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2735 gtk_widget_show (swin);
2739 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
2740 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2741 G_CALLBACK (tree_view_keybinding_cb), impl);
2742 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Shortcuts"));
2743 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
2745 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
2747 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2749 shortcuts_source_targets,
2750 num_shortcuts_source_targets,
2753 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
2754 GTK_DEST_DEFAULT_ALL,
2755 shortcuts_dest_targets,
2756 num_shortcuts_dest_targets,
2757 GDK_ACTION_COPY | GDK_ACTION_MOVE);
2759 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2760 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
2761 gtk_tree_selection_set_select_function (selection,
2762 shortcuts_select_func,
2765 g_signal_connect (selection, "changed",
2766 G_CALLBACK (shortcuts_selection_changed_cb), impl);
2768 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
2769 G_CALLBACK (shortcuts_row_activated_cb), impl);
2771 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2772 G_CALLBACK (shortcuts_key_press_event_cb), impl);
2774 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
2775 G_CALLBACK (shortcuts_drag_begin_cb), impl);
2776 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
2777 G_CALLBACK (shortcuts_drag_end_cb), impl);
2778 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
2779 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
2781 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
2782 G_CALLBACK (shortcuts_drag_leave_cb), impl);
2783 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
2784 G_CALLBACK (shortcuts_drag_motion_cb), impl);
2785 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
2786 G_CALLBACK (shortcuts_drag_drop_cb), impl);
2787 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
2788 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
2790 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
2791 gtk_widget_show (impl->browse_shortcuts_tree_view);
2795 column = gtk_tree_view_column_new ();
2796 gtk_tree_view_column_set_title (column, _("Folder"));
2798 renderer = gtk_cell_renderer_pixbuf_new ();
2799 gtk_tree_view_column_pack_start (column, renderer, FALSE);
2800 gtk_tree_view_column_set_attributes (column, renderer,
2801 "pixbuf", SHORTCUTS_COL_PIXBUF,
2802 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2805 renderer = gtk_cell_renderer_text_new ();
2806 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2807 gtk_tree_view_column_set_attributes (column, renderer,
2808 "text", SHORTCUTS_COL_NAME,
2811 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2812 shortcuts_row_separator_func,
2813 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
2816 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
2821 /* Creates the widgets for the shortcuts/bookmarks pane */
2823 shortcuts_pane_create (GtkFileChooserDefault *impl,
2824 GtkSizeGroup *size_group)
2830 vbox = gtk_vbox_new (FALSE, 6);
2831 gtk_widget_show (vbox);
2833 /* Shortcuts tree */
2835 widget = shortcuts_list_create (impl);
2836 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
2838 /* Box for buttons */
2840 hbox = gtk_hbox_new (TRUE, 6);
2841 gtk_size_group_add_widget (size_group, hbox);
2842 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2843 gtk_widget_show (hbox);
2845 /* Add bookmark button */
2847 impl->browse_shortcuts_add_button = button_new (impl,
2852 G_CALLBACK (add_bookmark_button_clicked_cb));
2853 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
2854 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
2855 _("Add the selected folder to the bookmarks"), NULL);
2857 /* Remove bookmark button */
2859 impl->browse_shortcuts_remove_button = button_new (impl,
2864 G_CALLBACK (remove_bookmark_button_clicked_cb));
2865 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
2866 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2867 _("Remove the selected bookmark"), NULL);
2872 /* Handles key press events on the file list, so that we can trap Enter to
2873 * activate the default button on our own. Also, checks to see if '/' has been
2874 * pressed. See comment by tree_view_keybinding_cb() for more details.
2877 trap_activate_cb (GtkWidget *widget,
2881 GtkFileChooserDefault *impl;
2883 impl = (GtkFileChooserDefault *) data;
2885 if (event->keyval == GDK_slash &&
2886 ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
2888 location_popup_handler (impl, "/");
2892 if ((event->keyval == GDK_Return
2893 || event->keyval == GDK_ISO_Enter
2894 || event->keyval == GDK_KP_Enter
2895 || event->keyval == GDK_space)
2896 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2897 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
2901 window = get_toplevel (widget);
2903 && widget != window->default_widget
2904 && !(widget == window->focus_widget &&
2905 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
2907 gtk_window_activate_default (window);
2915 /* Callback used when the file list's popup menu is detached */
2917 popup_menu_detach_cb (GtkWidget *attach_widget,
2920 GtkFileChooserDefault *impl;
2922 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
2923 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
2925 impl->browse_files_popup_menu = NULL;
2926 impl->browse_files_popup_menu_add_shortcut_item = NULL;
2927 impl->browse_files_popup_menu_hidden_files_item = NULL;
2930 /* Callback used when the "Add to Shortcuts" menu item is activated */
2932 add_to_shortcuts_cb (GtkMenuItem *item,
2933 GtkFileChooserDefault *impl)
2935 bookmarks_add_selected_folder (impl);
2938 /* Callback used when the "Show Hidden Files" menu item is toggled */
2940 show_hidden_toggled_cb (GtkCheckMenuItem *item,
2941 GtkFileChooserDefault *impl)
2944 "show-hidden", gtk_check_menu_item_get_active (item),
2948 /* Constructs the popup menu for the file list if needed */
2950 file_list_build_popup_menu (GtkFileChooserDefault *impl)
2954 if (impl->browse_files_popup_menu)
2957 impl->browse_files_popup_menu = gtk_menu_new ();
2958 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
2959 impl->browse_files_tree_view,
2960 popup_menu_detach_cb);
2962 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Shortcuts"));
2963 impl->browse_files_popup_menu_add_shortcut_item = item;
2964 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2965 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
2966 gtk_widget_set_sensitive (item, FALSE);
2967 g_signal_connect (item, "activate",
2968 G_CALLBACK (add_to_shortcuts_cb), impl);
2969 gtk_widget_show (item);
2970 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2972 item = gtk_separator_menu_item_new ();
2973 gtk_widget_show (item);
2974 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2976 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
2977 impl->browse_files_popup_menu_hidden_files_item = item;
2978 g_signal_connect (item, "toggled",
2979 G_CALLBACK (show_hidden_toggled_cb), impl);
2980 gtk_widget_show (item);
2981 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2984 /* Updates the popup menu for the file list, creating it if necessary */
2986 file_list_update_popup_menu (GtkFileChooserDefault *impl)
2988 file_list_build_popup_menu (impl);
2990 /* The sensitivity of the Add to Shortcuts item is set in
2991 * bookmarks_check_add_sensitivity()
2994 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
2995 G_CALLBACK (show_hidden_toggled_cb), impl);
2996 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
2998 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
2999 G_CALLBACK (show_hidden_toggled_cb), impl);
3003 popup_position_func (GtkMenu *menu,
3009 GtkWidget *widget = GTK_WIDGET (user_data);
3010 GdkScreen *screen = gtk_widget_get_screen (widget);
3013 GdkRectangle monitor;
3015 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3017 gdk_window_get_origin (widget->window, x, y);
3019 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3021 *x += (widget->allocation.width - req.width) / 2;
3022 *y += (widget->allocation.height - req.height) / 2;
3024 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3025 gtk_menu_set_monitor (menu, monitor_num);
3026 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3028 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3029 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3035 file_list_popup_menu (GtkFileChooserDefault *impl,
3036 GdkEventButton *event)
3038 file_list_update_popup_menu (impl);
3040 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3041 NULL, NULL, NULL, NULL,
3042 event->button, event->time);
3045 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3047 popup_position_func, impl->browse_files_tree_view,
3048 0, GDK_CURRENT_TIME);
3049 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3055 /* Callback used for the GtkWidget::popup-menu signal of the file list */
3057 list_popup_menu_cb (GtkWidget *widget,
3058 GtkFileChooserDefault *impl)
3060 file_list_popup_menu (impl, NULL);
3064 /* Callback used when a button is pressed on the file list. We trap button 3 to
3065 * bring up a popup menu.
3068 list_button_press_event_cb (GtkWidget *widget,
3069 GdkEventButton *event,
3070 GtkFileChooserDefault *impl)
3072 if (event->button != 3)
3075 file_list_popup_menu (impl, event);
3079 /* Creates the widgets for the file list */
3081 create_file_list (GtkFileChooserDefault *impl)
3084 GtkTreeSelection *selection;
3085 GtkTreeViewColumn *column;
3086 GtkCellRenderer *renderer;
3088 /* Scrolled window */
3090 swin = gtk_scrolled_window_new (NULL, NULL);
3091 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3092 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3093 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3096 /* Tree/list view */
3098 impl->browse_files_tree_view = gtk_tree_view_new ();
3099 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "GtkFileChooserDefault", impl);
3100 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
3102 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
3103 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
3104 g_signal_connect (impl->browse_files_tree_view, "row-activated",
3105 G_CALLBACK (list_row_activated), impl);
3106 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
3107 G_CALLBACK (trap_activate_cb), impl);
3108 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
3109 G_CALLBACK (list_popup_menu_cb), impl);
3110 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
3111 G_CALLBACK (list_button_press_event_cb), impl);
3113 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3114 gtk_tree_selection_set_select_function (selection,
3117 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
3119 file_list_source_targets,
3120 num_file_list_source_targets,
3123 g_signal_connect (selection, "changed",
3124 G_CALLBACK (list_selection_changed), impl);
3126 /* Filename column */
3128 impl->list_name_column = gtk_tree_view_column_new ();
3129 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
3130 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
3131 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
3132 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
3134 renderer = gtk_cell_renderer_pixbuf_new ();
3135 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
3136 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
3137 list_icon_data_func, impl, NULL);
3139 impl->list_name_renderer = gtk_cell_renderer_text_new ();
3140 g_object_set (impl->list_name_renderer,
3141 "ellipsize", PANGO_ELLIPSIZE_END,
3143 g_signal_connect (impl->list_name_renderer, "edited",
3144 G_CALLBACK (renderer_edited_cb), impl);
3145 g_signal_connect (impl->list_name_renderer, "editing-canceled",
3146 G_CALLBACK (renderer_editing_canceled_cb), impl);
3147 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
3148 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
3149 list_name_data_func, impl, NULL);
3151 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
3155 column = gtk_tree_view_column_new ();
3156 gtk_tree_view_column_set_title (column, _("Size"));
3158 renderer = gtk_cell_renderer_text_new ();
3159 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3160 gtk_tree_view_column_set_cell_data_func (column, renderer,
3161 list_size_data_func, impl, NULL);
3162 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
3163 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3165 /* Modification time column */
3167 column = gtk_tree_view_column_new ();
3168 gtk_tree_view_column_set_resizable (column, TRUE);
3169 gtk_tree_view_column_set_title (column, _("Modified"));
3171 renderer = gtk_cell_renderer_text_new ();
3172 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3173 gtk_tree_view_column_set_cell_data_func (column, renderer,
3174 list_mtime_data_func, impl, NULL);
3175 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
3176 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3177 gtk_widget_show_all (swin);
3183 create_path_bar (GtkFileChooserDefault *impl)
3185 GtkWidget *path_bar;
3187 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
3188 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
3194 set_filter_tooltip (GtkWidget *widget,
3197 GtkTooltips *tooltips = (GtkTooltips *)data;
3199 if (GTK_IS_BUTTON (widget))
3200 gtk_tooltips_set_tip (tooltips, widget,
3201 _("Select which types of files are shown"),
3206 realize_filter_combo (GtkWidget *combo,
3209 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
3211 gtk_container_forall (GTK_CONTAINER (combo),
3216 /* Creates the widgets for the files/folders pane */
3218 file_pane_create (GtkFileChooserDefault *impl,
3219 GtkSizeGroup *size_group)
3225 vbox = gtk_vbox_new (FALSE, 6);
3226 gtk_widget_show (vbox);
3228 /* The path bar and 'Create Folder' button */
3229 hbox = gtk_hbox_new (FALSE, 12);
3230 gtk_widget_show (hbox);
3231 impl->browse_path_bar = create_path_bar (impl);
3232 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
3233 gtk_widget_show_all (impl->browse_path_bar);
3234 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
3237 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
3238 g_signal_connect (impl->browse_new_folder_button, "clicked",
3239 G_CALLBACK (new_folder_button_clicked), impl);
3240 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
3241 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3243 /* Box for lists and preview */
3245 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
3246 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
3247 gtk_widget_show (hbox);
3251 widget = create_file_list (impl);
3252 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
3256 impl->preview_box = gtk_vbox_new (FALSE, 12);
3257 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
3258 /* Don't show preview box initially */
3262 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
3264 widget = filter_create (impl);
3266 g_signal_connect (widget, "realize",
3267 G_CALLBACK (realize_filter_combo), impl);
3269 gtk_widget_show (widget);
3270 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
3272 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
3273 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
3277 /* Callback used when the "Browse for more folders" expander is toggled */
3279 expander_changed_cb (GtkExpander *expander,
3281 GtkFileChooserDefault *impl)
3283 update_appearance (impl);
3286 /* Callback used when the selection changes in the save folder combo box */
3288 save_folder_combo_changed_cb (GtkComboBox *combo,
3289 GtkFileChooserDefault *impl)
3293 if (impl->changing_folder)
3296 if (gtk_combo_box_get_active_iter (combo, &iter))
3297 shortcuts_activate_iter (impl, &iter);
3300 /* Creates the combo box with the save folders */
3302 save_folder_combo_create (GtkFileChooserDefault *impl)
3305 GtkCellRenderer *cell;
3307 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
3308 gtk_widget_show (combo);
3310 cell = gtk_cell_renderer_pixbuf_new ();
3311 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3312 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3313 "pixbuf", SHORTCUTS_COL_PIXBUF,
3314 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3315 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3318 cell = gtk_cell_renderer_text_new ();
3319 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3320 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3321 "text", SHORTCUTS_COL_NAME,
3322 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3325 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
3326 shortcuts_row_separator_func,
3327 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3330 g_signal_connect (combo, "changed",
3331 G_CALLBACK (save_folder_combo_changed_cb), impl);
3336 /* Creates the widgets specific to Save mode */
3338 save_widgets_create (GtkFileChooserDefault *impl)
3343 GtkWidget *alignment;
3345 vbox = gtk_vbox_new (FALSE, 12);
3347 table = gtk_table_new (2, 2, FALSE);
3348 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3349 gtk_widget_show (table);
3350 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3351 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3355 widget = gtk_label_new_with_mnemonic (_("_Name:"));
3356 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3357 gtk_table_attach (GTK_TABLE (table), widget,
3361 gtk_widget_show (widget);
3363 impl->save_file_name_entry = _gtk_file_chooser_entry_new (TRUE);
3364 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3366 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3367 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3368 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3370 GTK_EXPAND | GTK_FILL, 0,
3372 gtk_widget_show (impl->save_file_name_entry);
3373 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
3376 impl->save_folder_label = gtk_label_new (NULL);
3377 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
3378 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
3382 gtk_widget_show (impl->save_folder_label);
3384 impl->save_folder_combo = save_folder_combo_create (impl);
3385 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
3387 GTK_EXPAND | GTK_FILL, GTK_FILL,
3389 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
3392 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3393 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
3395 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
3396 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
3397 g_signal_connect (impl->save_expander, "notify::expanded",
3398 G_CALLBACK (expander_changed_cb),
3400 gtk_widget_show_all (alignment);
3405 /* Creates the main hpaned with the widgets shared by Open and Save mode */
3407 browse_widgets_create (GtkFileChooserDefault *impl)
3412 GtkSizeGroup *size_group;
3414 /* size group is used by the [+][-] buttons and the filter combo */
3415 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
3416 vbox = gtk_vbox_new (FALSE, 12);
3419 hpaned = gtk_hpaned_new ();
3420 gtk_widget_show (hpaned);
3421 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
3422 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
3424 widget = shortcuts_pane_create (impl, size_group);
3425 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
3426 widget = file_pane_create (impl, size_group);
3427 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
3429 g_object_unref (size_group);
3435 gtk_file_chooser_default_constructor (GType type,
3436 guint n_construct_properties,
3437 GObjectConstructParam *construct_params)
3439 GtkFileChooserDefault *impl;
3442 object = parent_class->constructor (type,
3443 n_construct_properties,
3445 impl = GTK_FILE_CHOOSER_DEFAULT (object);
3447 g_assert (impl->file_system);
3449 gtk_widget_push_composite_child ();
3451 /* Shortcuts model */
3453 shortcuts_model_create (impl);
3455 /* Widgets for Save mode */
3456 impl->save_widgets = save_widgets_create (impl);
3457 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
3459 /* The browse widgets */
3460 impl->browse_widgets = browse_widgets_create (impl);
3461 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
3463 /* Alignment to hold extra widget */
3464 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3465 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
3467 gtk_widget_pop_composite_child ();
3468 update_appearance (impl);
3473 /* Sets the extra_widget by packing it in the appropriate place */
3475 set_extra_widget (GtkFileChooserDefault *impl,
3476 GtkWidget *extra_widget)
3480 g_object_ref (extra_widget);
3481 /* FIXME: is this right ? */
3482 gtk_widget_show (extra_widget);
3485 if (impl->extra_widget)
3487 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3488 g_object_unref (impl->extra_widget);
3491 impl->extra_widget = extra_widget;
3492 if (impl->extra_widget)
3494 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3495 gtk_widget_show (impl->extra_align);
3498 gtk_widget_hide (impl->extra_align);
3502 set_local_only (GtkFileChooserDefault *impl,
3503 gboolean local_only)
3505 if (local_only != impl->local_only)
3507 impl->local_only = local_only;
3509 if (impl->shortcuts_model && impl->file_system)
3511 shortcuts_add_volumes (impl);
3512 shortcuts_add_bookmarks (impl);
3516 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
3518 /* If we are pointing to a non-local folder, make an effort to change
3519 * back to a local folder, but it's really up to the app to not cause
3520 * such a situation, so we ignore errors.
3522 const gchar *home = g_get_home_dir ();
3523 GtkFilePath *home_path;
3528 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
3530 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
3532 gtk_file_path_free (home_path);
3538 volumes_changed_cb (GtkFileSystem *file_system,
3539 GtkFileChooserDefault *impl)
3541 shortcuts_add_volumes (impl);
3544 /* Callback used when the set of bookmarks changes in the file system */
3546 bookmarks_changed_cb (GtkFileSystem *file_system,
3547 GtkFileChooserDefault *impl)
3549 shortcuts_add_bookmarks (impl);
3551 bookmarks_check_add_sensitivity (impl);
3552 bookmarks_check_remove_sensitivity (impl);
3555 /* Sets the file chooser to multiple selection mode */
3557 set_select_multiple (GtkFileChooserDefault *impl,
3558 gboolean select_multiple,
3559 gboolean property_notify)
3561 GtkTreeSelection *selection;
3562 GtkSelectionMode mode;
3564 if (select_multiple == impl->select_multiple)
3567 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3569 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3570 gtk_tree_selection_set_mode (selection, mode);
3572 impl->select_multiple = select_multiple;
3573 g_object_notify (G_OBJECT (impl), "select-multiple");
3575 check_preview_change (impl);
3579 set_file_system_backend (GtkFileChooserDefault *impl,
3580 const char *backend)
3582 if (impl->file_system)
3584 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3585 impl->volumes_changed_id = 0;
3586 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3587 impl->bookmarks_changed_id = 0;
3588 g_object_unref (impl->file_system);
3591 impl->file_system = NULL;
3593 impl->file_system = _gtk_file_system_create (backend);
3596 GtkSettings *settings = gtk_settings_get_default ();
3597 gchar *default_backend = NULL;
3599 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3600 if (default_backend)
3602 impl->file_system = _gtk_file_system_create (default_backend);
3603 g_free (default_backend);
3607 if (!impl->file_system)
3609 #if defined (G_OS_UNIX)
3610 impl->file_system = gtk_file_system_unix_new ();
3611 #elif defined (G_OS_WIN32)
3612 impl->file_system = gtk_file_system_win32_new ();
3614 #error "No default filesystem implementation on the platform"
3618 if (impl->file_system)
3620 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3621 G_CALLBACK (volumes_changed_cb),
3623 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3624 G_CALLBACK (bookmarks_changed_cb),
3629 /* This function is basically a do_all function.
3631 * It sets the visibility on all the widgets based on the current state, and
3632 * moves the custom_widget if needed.
3635 update_appearance (GtkFileChooserDefault *impl)
3637 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3638 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3642 gtk_widget_show (impl->save_widgets);
3644 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3645 text = _("Save in _folder:");
3647 text = _("Create in _folder:");
3649 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
3651 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3653 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
3654 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
3655 gtk_widget_show (impl->browse_widgets);
3659 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
3660 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
3661 gtk_widget_hide (impl->browse_widgets);
3664 gtk_widget_show (impl->browse_new_folder_button);
3666 if (impl->select_multiple)
3668 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
3669 "Re-setting to single selection mode.");
3670 set_select_multiple (impl, FALSE, TRUE);
3673 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3674 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
3676 gtk_widget_hide (impl->save_widgets);
3677 gtk_widget_show (impl->browse_widgets);
3680 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
3681 gtk_widget_hide (impl->browse_new_folder_button);
3683 gtk_widget_show (impl->browse_new_folder_button);
3685 gtk_widget_queue_draw (impl->browse_files_tree_view);
3687 g_signal_emit_by_name (impl, "default-size-changed");
3691 gtk_file_chooser_default_set_property (GObject *object,
3693 const GValue *value,
3697 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3701 case GTK_FILE_CHOOSER_PROP_ACTION:
3703 GtkFileChooserAction action = g_value_get_enum (value);
3705 if (action != impl->action)
3707 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
3709 if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
3711 g_warning ("Multiple selection mode is not allowed in Save mode");
3712 set_select_multiple (impl, FALSE, TRUE);
3714 impl->action = action;
3715 update_appearance (impl);
3718 if (impl->save_file_name_entry)
3719 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3723 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
3724 set_file_system_backend (impl, g_value_get_string (value));
3726 case GTK_FILE_CHOOSER_PROP_FILTER:
3727 set_current_filter (impl, g_value_get_object (value));
3729 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3730 set_local_only (impl, g_value_get_boolean (value));
3732 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3733 set_preview_widget (impl, g_value_get_object (value));
3735 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3736 impl->preview_widget_active = g_value_get_boolean (value);
3737 update_preview_widget_visibility (impl);
3739 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3740 impl->use_preview_label = g_value_get_boolean (value);
3741 update_preview_widget_visibility (impl);
3743 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3744 set_extra_widget (impl, g_value_get_object (value));
3746 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3748 gboolean select_multiple = g_value_get_boolean (value);
3749 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
3751 g_warning ("Multiple selection mode is not allowed in Save mode");
3755 set_select_multiple (impl, select_multiple, FALSE);
3758 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3760 gboolean show_hidden = g_value_get_boolean (value);
3761 if (show_hidden != impl->show_hidden)
3763 impl->show_hidden = show_hidden;
3765 if (impl->browse_files_model)
3766 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
3771 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3777 gtk_file_chooser_default_get_property (GObject *object,
3782 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3786 case GTK_FILE_CHOOSER_PROP_ACTION:
3787 g_value_set_enum (value, impl->action);
3789 case GTK_FILE_CHOOSER_PROP_FILTER:
3790 g_value_set_object (value, impl->current_filter);
3792 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3793 g_value_set_boolean (value, impl->local_only);
3795 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3796 g_value_set_object (value, impl->preview_widget);
3798 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3799 g_value_set_boolean (value, impl->preview_widget_active);
3801 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3802 g_value_set_boolean (value, impl->use_preview_label);
3804 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3805 g_value_set_object (value, impl->extra_widget);
3807 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3808 g_value_set_boolean (value, impl->select_multiple);
3810 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3811 g_value_set_boolean (value, impl->show_hidden);
3814 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3819 /* Removes the settings signal handler. It's safe to call multiple times */
3821 remove_settings_signal (GtkFileChooserDefault *impl,
3824 if (impl->settings_signal_id)
3826 GtkSettings *settings;
3828 settings = gtk_settings_get_for_screen (screen);
3829 g_signal_handler_disconnect (settings,
3830 impl->settings_signal_id);
3831 impl->settings_signal_id = 0;
3836 gtk_file_chooser_default_dispose (GObject *object)
3838 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
3840 if (impl->extra_widget)
3842 g_object_unref (impl->extra_widget);
3843 impl->extra_widget = NULL;
3846 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
3848 G_OBJECT_CLASS (parent_class)->dispose (object);
3851 /* We override show-all since we have internal widgets that
3852 * shouldn't be shown when you call show_all(), like the filter
3856 gtk_file_chooser_default_show_all (GtkWidget *widget)
3858 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
3860 gtk_widget_show (widget);
3862 if (impl->extra_widget)
3863 gtk_widget_show_all (impl->extra_widget);
3866 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
3867 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
3870 toplevel_set_focus_cb (GtkWindow *window,
3872 GtkFileChooserDefault *impl)
3874 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
3877 /* We monitor the focus widget on our toplevel to be able to know which widget
3878 * was last focused at the time our "should_respond" method gets called.
3881 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
3882 GtkWidget *previous_toplevel)
3884 GtkFileChooserDefault *impl;
3885 GtkWidget *toplevel;
3887 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3889 if (previous_toplevel)
3891 g_assert (impl->toplevel_set_focus_id != 0);
3892 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
3893 impl->toplevel_set_focus_id = 0;
3894 impl->toplevel_last_focus_widget = NULL;
3897 g_assert (impl->toplevel_set_focus_id == 0);
3899 toplevel = gtk_widget_get_toplevel (widget);
3900 if (GTK_IS_WINDOW (toplevel))
3902 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
3903 G_CALLBACK (toplevel_set_focus_cb), impl);
3904 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
3908 /* Changes the icons wherever it is needed */
3910 change_icon_theme (GtkFileChooserDefault *impl)
3912 GtkSettings *settings;
3915 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3917 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_SMALL_TOOLBAR, &width, &height))
3918 impl->icon_size = MAX (width, height);
3920 impl->icon_size = FALLBACK_ICON_SIZE;
3922 shortcuts_reload_icons (impl);
3923 gtk_widget_queue_resize (impl->browse_files_tree_view);
3926 /* Callback used when a GtkSettings value changes */
3928 settings_notify_cb (GObject *object,
3930 GtkFileChooserDefault *impl)
3934 name = g_param_spec_get_name (pspec);
3936 if (strcmp (name, "gtk-icon-theme-name") == 0
3937 || strcmp (name, "gtk-icon-sizes") == 0)
3938 change_icon_theme (impl);
3941 /* Installs a signal handler for GtkSettings so that we can monitor changes in
3945 check_icon_theme (GtkFileChooserDefault *impl)
3947 GtkSettings *settings;
3949 if (impl->settings_signal_id)
3952 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
3954 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3955 impl->settings_signal_id = g_signal_connect (settings, "notify",
3956 G_CALLBACK (settings_notify_cb), impl);
3958 change_icon_theme (impl);
3963 gtk_file_chooser_default_style_set (GtkWidget *widget,
3964 GtkStyle *previous_style)
3966 GtkFileChooserDefault *impl;
3968 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3970 if (GTK_WIDGET_CLASS (parent_class)->style_set)
3971 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
3973 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
3974 change_icon_theme (impl);
3976 g_signal_emit_by_name (widget, "default-size-changed");
3980 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
3981 GdkScreen *previous_screen)
3983 GtkFileChooserDefault *impl;
3985 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3987 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
3988 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
3990 remove_settings_signal (impl, previous_screen);
3991 check_icon_theme (impl);
3993 g_signal_emit_by_name (widget, "default-size-changed");
3997 get_is_file_filtered (GtkFileChooserDefault *impl,
3999 GtkFileInfo *file_info)
4001 GtkFileFilterInfo filter_info;
4002 GtkFileFilterFlags needed;
4005 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
4007 needed = gtk_file_filter_get_needed (impl->current_filter);
4009 filter_info.display_name = gtk_file_info_get_display_name (file_info);
4010 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
4012 if (needed & GTK_FILE_FILTER_FILENAME)
4014 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
4015 if (filter_info.filename)
4016 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
4019 filter_info.filename = NULL;
4021 if (needed & GTK_FILE_FILTER_URI)
4023 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
4024 if (filter_info.uri)
4025 filter_info.contains |= GTK_FILE_FILTER_URI;
4028 filter_info.uri = NULL;
4030 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
4032 if (filter_info.filename)
4033 g_free ((gchar *)filter_info.filename);
4034 if (filter_info.uri)
4035 g_free ((gchar *)filter_info.uri);
4041 list_model_filter_func (GtkFileSystemModel *model,
4043 const GtkFileInfo *file_info,
4046 GtkFileChooserDefault *impl = user_data;
4048 if (!impl->current_filter)
4051 if (gtk_file_info_get_is_folder (file_info))
4054 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
4058 install_list_model_filter (GtkFileChooserDefault *impl)
4060 GtkFileSystemModelFilter filter;
4063 g_assert (impl->browse_files_model != NULL);
4065 if (impl->current_filter)
4067 filter = list_model_filter_func;
4076 _gtk_file_system_model_set_filter (impl->browse_files_model,
4081 #define COMPARE_DIRECTORIES \
4082 GtkFileChooserDefault *impl = user_data; \
4083 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
4084 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
4085 gboolean dir_a, dir_b; \
4088 dir_a = gtk_file_info_get_is_folder (info_a); \
4090 return impl->list_sort_ascending ? -1 : 1; \
4093 dir_b = gtk_file_info_get_is_folder (info_b); \
4095 return impl->list_sort_ascending ? 1 : -1; \
4097 if (dir_a != dir_b) \
4098 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
4100 /* Sort callback for the filename column */
4102 name_sort_func (GtkTreeModel *model,
4107 COMPARE_DIRECTORIES;
4109 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
4112 /* Sort callback for the size column */
4114 size_sort_func (GtkTreeModel *model,
4119 COMPARE_DIRECTORIES;
4122 gint64 size_a = gtk_file_info_get_size (info_a);
4123 gint64 size_b = gtk_file_info_get_size (info_b);
4125 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
4129 /* Sort callback for the mtime column */
4131 mtime_sort_func (GtkTreeModel *model,
4136 COMPARE_DIRECTORIES;
4139 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
4140 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
4142 return ta > tb ? -1 : (ta == tb ? 0 : 1);
4146 /* Callback used when the sort column changes. We cache the sort order for use
4147 * in name_sort_func().
4150 list_sort_column_changed_cb (GtkTreeSortable *sortable,
4151 GtkFileChooserDefault *impl)
4153 GtkSortType sort_type;
4155 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
4156 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
4160 set_busy_cursor (GtkFileChooserDefault *impl,
4163 GtkWindow *toplevel;
4164 GdkDisplay *display;
4167 toplevel = get_toplevel (GTK_WIDGET (impl));
4168 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
4171 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
4174 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
4178 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
4179 gdk_display_flush (display);
4182 gdk_cursor_unref (cursor);
4185 /* Creates a sort model to wrap the file system model and sets it on the tree view */
4187 load_set_model (GtkFileChooserDefault *impl)
4189 g_assert (impl->browse_files_model != NULL);
4190 g_assert (impl->sort_model == NULL);
4192 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4193 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4194 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4195 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4196 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4197 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4198 impl->list_sort_ascending = TRUE;
4200 g_signal_connect (impl->sort_model, "sort-column-changed",
4201 G_CALLBACK (list_sort_column_changed_cb), impl);
4203 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4204 GTK_TREE_MODEL (impl->sort_model));
4205 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4206 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4207 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4210 /* Timeout callback used when the loading timer expires */
4212 load_timeout_cb (gpointer data)
4214 GtkFileChooserDefault *impl;
4216 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4217 g_assert (impl->load_state == LOAD_LOADING);
4218 g_assert (impl->load_timeout_id != 0);
4219 g_assert (impl->browse_files_model != NULL);
4221 impl->load_timeout_id = 0;
4222 impl->load_state = LOAD_FINISHED;
4224 load_set_model (impl);
4229 /* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
4231 load_setup_timer (GtkFileChooserDefault *impl)
4233 g_assert (impl->load_timeout_id == 0);
4234 g_assert (impl->load_state == LOAD_FINISHED);
4236 impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
4237 impl->load_state = LOAD_LOADING;
4240 /* Removes the load timeout and switches to the LOAD_FINISHED state */
4242 load_remove_timer (GtkFileChooserDefault *impl)
4244 if (impl->load_timeout_id != 0)
4246 g_assert (impl->load_state == LOAD_LOADING);
4248 g_source_remove (impl->load_timeout_id);
4249 impl->load_timeout_id = 0;
4250 impl->load_state = LOAD_FINISHED;
4253 g_assert (impl->load_state == LOAD_FINISHED);
4256 /* Queues a pending operation relative to selecting a file when the current
4257 * folder finishes loading.
4260 pending_op_queue (GtkFileChooserDefault *impl, PendingOp op, const GtkFilePath *path)
4262 if (impl->pending_op == PENDING_OP_SELECT_PATH)
4264 g_assert (impl->pending_select_path != NULL);
4265 gtk_file_path_free (impl->pending_select_path);
4267 impl->pending_select_path = NULL;
4270 impl->pending_op = op;
4272 if (impl->pending_op == PENDING_OP_SELECT_PATH)
4274 g_assert (path != NULL);
4275 impl->pending_select_path = gtk_file_path_copy (path);
4279 /* Selects the first row in the file list */
4281 browse_files_select_first_row (GtkFileChooserDefault *impl)
4285 path = gtk_tree_path_new_from_indices (0, -1);
4286 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4287 gtk_tree_path_free (path);
4290 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
4291 * selected row in the tree view.
4294 center_selected_row_foreach_cb (GtkTreeModel *model,
4299 GtkFileChooserDefault *impl;
4301 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4302 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
4305 /* Centers the selected row in the tree view */
4307 browse_files_center_selected_row (GtkFileChooserDefault *impl)
4309 GtkTreeSelection *selection;
4311 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4312 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, impl);
4315 /* Processes the pending operation when a folder is finished loading */
4317 pending_op_process (GtkFileChooserDefault *impl)
4319 switch (impl->pending_op)
4321 case PENDING_OP_NONE:
4324 case PENDING_OP_SELECT_PATH:
4325 g_assert (impl->pending_select_path != NULL);
4327 _gtk_file_system_model_path_do (impl->browse_files_model, impl->pending_select_path, select_func, impl);
4328 gtk_file_path_free (impl->pending_select_path);
4329 impl->pending_select_path = NULL;
4330 impl->pending_op = PENDING_OP_NONE;
4331 browse_files_center_selected_row (impl);
4334 case PENDING_OP_SELECT_FIRST:
4335 browse_files_select_first_row (impl);
4336 impl->pending_op = PENDING_OP_NONE;
4340 g_assert_not_reached ();
4343 g_assert (impl->pending_op == PENDING_OP_NONE);
4344 g_assert (impl->pending_select_path == NULL);
4347 /* Callback used when the file system model finishes loading */
4349 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
4350 GtkFileChooserDefault *impl)
4352 if (impl->load_state == LOAD_LOADING)
4354 load_remove_timer (impl);
4355 load_set_model (impl);
4358 g_assert (impl->load_state == LOAD_FINISHED);
4360 pending_op_process (impl);
4361 set_busy_cursor (impl, FALSE);
4364 /* Gets rid of the old list model and creates a new one for the current folder */
4366 set_list_model (GtkFileChooserDefault *impl,
4369 load_remove_timer (impl);
4371 if (impl->browse_files_model)
4373 g_object_unref (impl->browse_files_model);
4374 impl->browse_files_model = NULL;
4377 if (impl->sort_model)
4379 g_object_unref (impl->sort_model);
4380 impl->sort_model = NULL;
4383 set_busy_cursor (impl, TRUE);
4384 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
4386 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
4387 impl->current_folder, 0,
4390 if (!impl->browse_files_model)
4392 set_busy_cursor (impl, FALSE);
4396 load_setup_timer (impl);
4398 g_signal_connect (impl->browse_files_model, "finished-loading",
4399 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
4401 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
4403 install_list_model_filter (impl);
4409 update_chooser_entry (GtkFileChooserDefault *impl)
4411 GtkTreeSelection *selection;
4412 const GtkFileInfo *info;
4414 GtkTreeIter child_iter;
4416 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
4419 g_assert (!impl->select_multiple);
4420 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4422 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4425 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4429 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4431 if (!gtk_file_info_get_is_folder (info))
4432 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4433 gtk_file_info_get_display_name (info));
4437 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
4438 const GtkFilePath *path,
4441 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4444 if (impl->local_only &&
4445 !gtk_file_system_path_is_local (impl->file_system, path))
4448 GTK_FILE_SYSTEM_ERROR,
4449 GTK_FILE_SYSTEM_ERROR_FAILED,
4450 _("Cannot change to folder because it is not local"));
4455 /* Test validity of path here. */
4456 if (!check_is_folder (impl->file_system, path, error))
4459 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
4462 if (impl->current_folder != path)
4464 if (impl->current_folder)
4465 gtk_file_path_free (impl->current_folder);
4467 impl->current_folder = gtk_file_path_copy (path);
4470 /* Update the widgets that may trigger a folder change themselves. */
4472 if (!impl->changing_folder)
4474 impl->changing_folder = TRUE;
4476 shortcuts_update_current_folder (impl);
4478 impl->changing_folder = FALSE;
4481 /* Set the folder on the save entry */
4483 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4484 impl->current_folder);
4486 /* Create a new list model. This is slightly evil; we store the result value
4487 * but perform more actions rather than returning immediately even if it
4488 * generates an error.
4490 result = set_list_model (impl, error);
4492 /* Refresh controls */
4494 shortcuts_find_current_folder (impl);
4496 g_signal_emit_by_name (impl, "current-folder-changed", 0);
4498 check_preview_change (impl);
4499 bookmarks_check_add_sensitivity (impl);
4501 g_signal_emit_by_name (impl, "selection-changed", 0);
4506 static GtkFilePath *
4507 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
4509 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4511 return gtk_file_path_copy (impl->current_folder);
4515 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
4518 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4520 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4521 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4523 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), name);
4527 select_func (GtkFileSystemModel *model,
4532 GtkFileChooserDefault *impl = user_data;
4533 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4534 GtkTreePath *sorted_path;
4536 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
4537 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
4538 gtk_tree_view_scroll_to_cell (tree_view, sorted_path, NULL, FALSE, 0.0, 0.0);
4539 gtk_tree_path_free (sorted_path);
4543 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
4544 const GtkFilePath *path,
4547 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4548 GtkFilePath *parent_path;
4550 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
4554 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
4558 GtkFileFolder *folder;
4560 gboolean is_hidden, is_filtered;
4562 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
4566 gtk_file_path_free (parent_path);
4570 folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
4571 gtk_file_path_free (parent_path);
4576 info = gtk_file_folder_get_info (folder, path, error);
4577 g_object_unref (folder);
4582 is_hidden = gtk_file_info_get_is_hidden (info);
4583 is_filtered = get_is_file_filtered (impl, (GtkFilePath *) path, info);
4584 gtk_file_info_free (info);
4587 g_object_set (impl, "show-hidden", TRUE, NULL);
4590 set_current_filter (impl, NULL);
4592 pending_op_queue (impl, PENDING_OP_SELECT_PATH, path);
4596 g_assert_not_reached ();
4600 unselect_func (GtkFileSystemModel *model,
4605 GtkFileChooserDefault *impl = user_data;
4606 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4607 GtkTreePath *sorted_path;
4609 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
4611 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
4613 gtk_tree_path_free (sorted_path);
4617 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
4618 const GtkFilePath *path)
4620 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4622 if (!impl->browse_files_model)
4625 _gtk_file_system_model_path_do (impl->browse_files_model, path,
4626 unselect_func, impl);
4630 maybe_select (GtkTreeModel *model,
4635 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
4636 GtkTreeSelection *selection;
4637 const GtkFileInfo *info;
4640 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4642 info = get_list_file_info (impl, iter);
4643 is_folder = gtk_file_info_get_is_folder (info);
4645 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
4646 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
4647 gtk_tree_selection_select_iter (selection, iter);
4649 gtk_tree_selection_unselect_iter (selection, iter);
4655 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
4657 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4658 if (impl->select_multiple)
4659 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
4660 maybe_select, impl);
4664 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
4666 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4667 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4669 gtk_tree_selection_unselect_all (selection);
4672 /* Checks whether the filename entry for the Save modes contains a valid filename */
4673 static GtkFilePath *
4674 check_save_entry (GtkFileChooserDefault *impl,
4678 GtkFileChooserEntry *chooser_entry;
4679 const GtkFilePath *current_folder;
4680 const char *file_part;
4684 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4685 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4687 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
4689 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
4690 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
4692 if (!file_part || file_part[0] == '\0')
4702 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
4706 error_building_filename_dialog (impl, current_folder, file_part, error);
4715 struct get_paths_closure {
4716 GtkFileChooserDefault *impl;
4718 GtkFilePath *path_from_entry;
4722 get_paths_foreach (GtkTreeModel *model,
4727 struct get_paths_closure *info;
4728 const GtkFilePath *file_path;
4729 GtkFileSystemModel *fs_model;
4730 GtkTreeIter sel_iter;
4733 fs_model = info->impl->browse_files_model;
4734 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
4736 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
4738 return; /* We are on the editable row */
4740 if (!info->path_from_entry
4741 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
4742 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
4746 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
4748 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4749 struct get_paths_closure info;
4753 info.path_from_entry = NULL;
4755 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4756 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4758 gboolean is_valid, is_empty;
4760 info.path_from_entry = check_save_entry (impl, &is_valid, &is_empty);
4761 if (!is_valid && !is_empty)
4765 if (!info.path_from_entry || impl->select_multiple)
4767 GtkTreeSelection *selection;
4769 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4770 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
4773 if (info.path_from_entry)
4774 info.result = g_slist_prepend (info.result, info.path_from_entry);
4776 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
4777 * fall back to the current directory */
4778 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
4779 info.result == NULL)
4781 info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
4784 return g_slist_reverse (info.result);
4787 static GtkFilePath *
4788 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
4790 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4792 if (impl->preview_path)
4793 return gtk_file_path_copy (impl->preview_path);
4798 static GtkFileSystem *
4799 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
4801 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4803 return impl->file_system;
4806 /* Shows or hides the filter widgets */
4808 show_filters (GtkFileChooserDefault *impl,
4812 gtk_widget_show (impl->filter_combo_hbox);
4814 gtk_widget_hide (impl->filter_combo_hbox);
4818 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
4819 GtkFileFilter *filter)
4821 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4824 if (g_slist_find (impl->filters, filter))
4826 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
4830 g_object_ref (filter);
4831 gtk_object_sink (GTK_OBJECT (filter));
4832 impl->filters = g_slist_append (impl->filters, filter);
4834 name = gtk_file_filter_get_name (filter);
4836 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
4838 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
4840 if (!g_slist_find (impl->filters, impl->current_filter))
4841 set_current_filter (impl, filter);
4843 show_filters (impl, TRUE);
4847 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
4848 GtkFileFilter *filter)
4850 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4851 GtkTreeModel *model;
4855 filter_index = g_slist_index (impl->filters, filter);
4857 if (filter_index < 0)
4859 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
4863 impl->filters = g_slist_remove (impl->filters, filter);
4865 if (filter == impl->current_filter)
4868 set_current_filter (impl, impl->filters->data);
4870 set_current_filter (impl, NULL);
4873 /* Remove row from the combo box */
4874 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
4875 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
4876 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
4878 g_object_unref (filter);
4881 show_filters (impl, FALSE);
4885 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
4887 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4889 return g_slist_copy (impl->filters);
4892 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
4894 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
4897 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
4901 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
4902 const GtkFilePath *path,
4905 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4909 /* Test validity of path here. */
4910 if (!check_is_folder (impl->file_system, path, error))
4913 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
4915 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
4918 impl->num_shortcuts++;
4920 if (impl->shortcuts_filter_model)
4921 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
4927 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
4928 const GtkFilePath *path,
4931 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4937 if (impl->num_shortcuts == 0)
4940 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
4941 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
4942 g_assert_not_reached ();
4944 for (i = 0; i < impl->num_shortcuts; i++)
4948 GtkFilePath *shortcut;
4950 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
4951 SHORTCUTS_COL_DATA, &col_data,
4952 SHORTCUTS_COL_IS_VOLUME, &is_volume,
4954 g_assert (col_data != NULL);
4955 g_assert (!is_volume);
4957 shortcut = col_data;
4958 if (gtk_file_path_compare (shortcut, path) == 0)
4960 shortcuts_remove_rows (impl, pos + i, 1);
4961 impl->num_shortcuts--;
4965 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
4966 g_assert_not_reached ();
4971 uri = gtk_file_system_path_to_uri (impl->file_system, path);
4973 GTK_FILE_CHOOSER_ERROR,
4974 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
4975 _("shortcut %s does not exist"),
4983 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
4985 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4991 if (impl->num_shortcuts == 0)
4994 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
4995 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
4996 g_assert_not_reached ();
5000 for (i = 0; i < impl->num_shortcuts; i++)
5004 GtkFilePath *shortcut;
5006 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5007 SHORTCUTS_COL_DATA, &col_data,
5008 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5010 g_assert (col_data != NULL);
5011 g_assert (!is_volume);
5013 shortcut = col_data;
5014 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
5016 if (i != impl->num_shortcuts - 1)
5018 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5019 g_assert_not_reached ();
5023 return g_slist_reverse (list);
5026 /* Guesses a size based upon font sizes */
5028 find_good_size_from_style (GtkWidget *widget,
5032 GtkFileChooserDefault *impl;
5033 gint default_width, default_height;
5036 GtkRequisition preview_req;
5038 g_assert (widget->style != NULL);
5039 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5041 font_size = pango_font_description_get_size (widget->style->font_desc);
5042 font_size = PANGO_PIXELS (font_size);
5044 default_width = font_size * NUM_CHARS;
5045 default_height = font_size * NUM_LINES;
5047 /* Use at least the requisition size not including the preview widget */
5048 gtk_widget_size_request (widget, &req);
5050 if (impl->preview_widget_active && impl->preview_widget)
5051 gtk_widget_size_request (impl->preview_box, &preview_req);
5053 preview_req.width = 0;
5055 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
5056 default_height = MAX (default_height, req.height);
5058 *width = default_width;
5059 *height = default_height;
5063 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
5064 gint *default_width,
5065 gint *default_height)
5067 GtkFileChooserDefault *impl;
5069 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5071 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
5073 if (impl->preview_widget_active && impl->preview_widget)
5074 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
5078 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
5079 gboolean *resize_horizontally,
5080 gboolean *resize_vertically)
5082 GtkFileChooserDefault *impl;
5084 g_return_if_fail (resize_horizontally != NULL);
5085 g_return_if_fail (resize_vertically != NULL);
5087 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5089 *resize_horizontally = TRUE;
5090 *resize_vertically = TRUE;
5092 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5093 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5095 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5097 *resize_horizontally = FALSE;
5098 *resize_vertically = FALSE;
5103 struct switch_folder_closure {
5104 GtkFileChooserDefault *impl;
5105 const GtkFilePath *path;
5109 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
5111 switch_folder_foreach_cb (GtkTreeModel *model,
5116 struct switch_folder_closure *closure;
5117 GtkTreeIter child_iter;
5121 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
5123 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
5124 closure->num_selected++;
5127 /* Changes to the selected folder in the list view */
5129 switch_to_selected_folder (GtkFileChooserDefault *impl)
5131 GtkTreeSelection *selection;
5132 struct switch_folder_closure closure;
5134 /* We do this with foreach() rather than get_selected() as we may be in
5135 * multiple selection mode
5138 closure.impl = impl;
5139 closure.path = NULL;
5140 closure.num_selected = 0;
5142 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5143 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
5145 g_assert (closure.path && closure.num_selected == 1);
5147 change_folder_and_display_error (impl, closure.path);
5150 /* Implementation for GtkFileChooserEmbed::should_respond() */
5152 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
5154 GtkFileChooserDefault *impl;
5155 GtkWidget *toplevel;
5156 GtkWidget *current_focus;
5158 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5160 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
5161 g_assert (GTK_IS_WINDOW (toplevel));
5163 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
5165 if (current_focus == impl->browse_files_tree_view)
5168 gboolean all_files, all_folders;
5172 selection_check (impl, &num_selected, &all_files, &all_folders);
5174 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5176 if (num_selected != 1)
5177 return TRUE; /* zero means current folder; more than one means use the whole selection */
5178 else if (current_focus != impl->browse_files_tree_view)
5180 /* a single folder is selected and a button was clicked */
5181 switch_to_selected_folder (impl);
5186 if (num_selected == 0)
5188 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5189 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5190 goto save_entry; /* it makes sense to use the typed name */
5195 if (num_selected == 1 && all_folders)
5197 switch_to_selected_folder (impl);
5198 pending_op_queue (impl, PENDING_OP_SELECT_FIRST, NULL);
5204 else if (current_focus == impl->save_file_name_entry)
5207 gboolean is_valid, is_empty;
5210 GtkFileChooserEntry *entry;
5214 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5215 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5217 entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
5218 path = check_save_entry (impl, &is_valid, &is_empty);
5220 if (!is_empty && !is_valid)
5224 path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5226 is_folder = check_is_folder (impl->file_system, path, NULL);
5229 _gtk_file_chooser_entry_set_file_part (entry, "");
5230 change_folder_and_display_error (impl, path);
5236 gtk_file_path_free (path);
5239 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
5241 /* The focus is on a dialog's action area button, *and* the widget that
5242 * was focused immediately before it is the shortcuts list. Switch to the
5243 * selected shortcut and tell the caller not to respond.
5247 if (shortcuts_get_selected (impl, &iter))
5249 shortcuts_activate_iter (impl, &iter);
5251 gtk_widget_grab_focus (impl->browse_files_tree_view);
5258 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
5260 /* The focus is on a dialog's action area button, *and* the widget that
5261 * was focused immediately before it is the file list.
5266 /* The focus is on a dialog's action area button or something else */
5267 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5268 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5273 g_assert_not_reached ();
5277 /* Implementation for GtkFileChooserEmbed::initial_focus() */
5279 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
5281 GtkFileChooserDefault *impl;
5284 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5286 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5287 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5289 if (impl->load_state == LOAD_FINISHED)
5290 browse_files_select_first_row (impl);
5292 widget = impl->browse_files_tree_view;
5294 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5295 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5296 widget = impl->save_file_name_entry;
5299 g_assert_not_reached ();
5303 gtk_widget_grab_focus (widget);
5307 set_current_filter (GtkFileChooserDefault *impl,
5308 GtkFileFilter *filter)
5310 if (impl->current_filter != filter)
5314 /* NULL filters are allowed to reset to non-filtered status
5316 filter_index = g_slist_index (impl->filters, filter);
5317 if (impl->filters && filter && filter_index < 0)
5320 if (impl->current_filter)
5321 g_object_unref (impl->current_filter);
5322 impl->current_filter = filter;
5323 if (impl->current_filter)
5325 g_object_ref (impl->current_filter);
5326 gtk_object_sink (GTK_OBJECT (filter));
5330 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
5333 if (impl->browse_files_model)
5334 install_list_model_filter (impl);
5336 g_object_notify (G_OBJECT (impl), "filter");
5341 filter_combo_changed (GtkComboBox *combo_box,
5342 GtkFileChooserDefault *impl)
5344 gint new_index = gtk_combo_box_get_active (combo_box);
5345 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
5347 set_current_filter (impl, new_filter);
5351 check_preview_change (GtkFileChooserDefault *impl)
5353 GtkTreePath *cursor_path;
5354 const GtkFilePath *new_path;
5355 const GtkFileInfo *new_info;
5357 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
5358 if (cursor_path && impl->sort_model)
5361 GtkTreeIter child_iter;
5363 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
5364 gtk_tree_path_free (cursor_path);
5366 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5368 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5369 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5377 if (new_path != impl->preview_path &&
5378 !(new_path && impl->preview_path &&
5379 gtk_file_path_compare (new_path, impl->preview_path) == 0))
5381 if (impl->preview_path)
5383 gtk_file_path_free (impl->preview_path);
5384 g_free (impl->preview_display_name);
5389 impl->preview_path = gtk_file_path_copy (new_path);
5390 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
5394 impl->preview_path = NULL;
5395 impl->preview_display_name = NULL;
5398 if (impl->use_preview_label && impl->preview_label)
5399 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
5401 g_signal_emit_by_name (impl, "update-preview");
5405 /* Activates a volume by mounting it if necessary and then switching to its
5409 shortcuts_activate_volume (GtkFileChooserDefault *impl,
5410 GtkFileSystemVolume *volume)
5414 /* We ref the file chooser since volume_mount() may run a main loop, and the
5415 * user could close the file chooser window in the meantime.
5417 g_object_ref (impl);
5419 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
5424 set_busy_cursor (impl, TRUE);
5427 result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
5433 msg = g_strdup_printf ("Could not mount %s:\n%s",
5434 gtk_file_system_volume_get_display_name (impl->file_system, volume),
5436 error_message (impl, msg);
5438 g_error_free (error);
5441 set_busy_cursor (impl, FALSE);
5447 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
5448 change_folder_and_display_error (impl, path);
5449 gtk_file_path_free (path);
5453 g_object_unref (impl);
5456 /* Opens the folder or volume at the specified iter in the shortcuts model */
5458 shortcuts_activate_iter (GtkFileChooserDefault *impl,
5464 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
5465 SHORTCUTS_COL_DATA, &col_data,
5466 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5470 return; /* We are on a separator */
5474 GtkFileSystemVolume *volume;
5478 shortcuts_activate_volume (impl, volume);
5482 const GtkFilePath *file_path;
5484 file_path = col_data;
5485 change_folder_and_display_error (impl, file_path);
5489 /* Callback used when a row in the shortcuts list is activated */
5491 shortcuts_row_activated_cb (GtkTreeView *tree_view,
5493 GtkTreeViewColumn *column,
5494 GtkFileChooserDefault *impl)
5497 GtkTreeIter child_iter;
5499 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
5502 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
5505 shortcuts_activate_iter (impl, &child_iter);
5507 gtk_widget_grab_focus (impl->browse_files_tree_view);
5510 /* Handler for GtkWidget::key-press-event on the shortcuts list */
5512 shortcuts_key_press_event_cb (GtkWidget *widget,
5514 GtkFileChooserDefault *impl)
5518 modifiers = gtk_accelerator_get_default_mod_mask ();
5520 if ((event->keyval == GDK_BackSpace
5521 || event->keyval == GDK_Delete
5522 || event->keyval == GDK_KP_Delete)
5523 && (event->state & modifiers) == 0)
5525 remove_selected_bookmarks (impl);
5533 shortcuts_select_func (GtkTreeSelection *selection,
5534 GtkTreeModel *model,
5536 gboolean path_currently_selected,
5539 GtkFileChooserDefault *impl = data;
5541 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
5545 list_select_func (GtkTreeSelection *selection,
5546 GtkTreeModel *model,
5548 gboolean path_currently_selected,
5551 GtkFileChooserDefault *impl = data;
5553 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5554 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5556 GtkTreeIter iter, child_iter;
5557 const GtkFileInfo *info;
5559 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5562 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5564 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5566 if (!gtk_file_info_get_is_folder (info))
5574 list_selection_changed (GtkTreeSelection *selection,
5575 GtkFileChooserDefault *impl)
5577 /* See if we are in the new folder editable row for Save mode */
5578 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5580 GtkTreeSelection *selection;
5581 GtkTreeIter iter, child_iter;
5582 const GtkFileInfo *info;
5584 g_assert (!impl->select_multiple);
5585 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5586 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
5589 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5593 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5595 return; /* We are on the editable row for New Folder */
5598 update_chooser_entry (impl);
5599 check_preview_change (impl);
5600 bookmarks_check_add_sensitivity (impl);
5602 g_signal_emit_by_name (impl, "selection-changed", 0);
5605 /* Callback used when a row in the file list is activated */
5607 list_row_activated (GtkTreeView *tree_view,
5609 GtkTreeViewColumn *column,
5610 GtkFileChooserDefault *impl)
5612 GtkTreeIter iter, child_iter;
5613 const GtkFileInfo *info;
5615 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5618 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5620 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5622 if (gtk_file_info_get_is_folder (info))
5624 const GtkFilePath *file_path;
5626 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5627 change_folder_and_display_error (impl, file_path);
5632 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5633 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5634 g_signal_emit_by_name (impl, "file-activated");
5638 path_bar_clicked (GtkPathBar *path_bar,
5639 GtkFilePath *file_path,
5640 gboolean child_is_hidden,
5641 GtkFileChooserDefault *impl)
5643 if (!change_folder_and_display_error (impl, file_path))
5646 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
5647 * show hidden files so that ".baz" appears in the file list, as it will still
5648 * be shown in the path bar: "/foo/[bar]/.baz"
5650 if (child_is_hidden)
5651 g_object_set (impl, "show-hidden", TRUE, NULL);
5654 static const GtkFileInfo *
5655 get_list_file_info (GtkFileChooserDefault *impl,
5658 GtkTreeIter child_iter;
5660 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5664 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5668 list_icon_data_func (GtkTreeViewColumn *tree_column,
5669 GtkCellRenderer *cell,
5670 GtkTreeModel *tree_model,
5674 GtkFileChooserDefault *impl = data;
5675 GtkTreeIter child_iter;
5676 const GtkFilePath *path;
5678 const GtkFileInfo *info;
5679 gboolean sensitive = TRUE;
5681 info = get_list_file_info (impl, iter);
5683 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5686 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5690 /* FIXME: NULL GError */
5691 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
5692 impl->icon_size, NULL);
5696 /* We are on the editable row */
5700 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5701 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
5702 sensitive = gtk_file_info_get_is_folder (info);
5706 "sensitive", sensitive,
5710 g_object_unref (pixbuf);
5714 list_name_data_func (GtkTreeViewColumn *tree_column,
5715 GtkCellRenderer *cell,
5716 GtkTreeModel *tree_model,
5720 GtkFileChooserDefault *impl = data;
5721 const GtkFileInfo *info = get_list_file_info (impl, iter);
5722 gboolean sensitive = TRUE;
5727 "text", _("Type name of new folder"),
5734 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
5735 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5737 sensitive = gtk_file_info_get_is_folder (info);
5741 "text", gtk_file_info_get_display_name (info),
5742 "sensitive", sensitive,
5748 list_size_data_func (GtkTreeViewColumn *tree_column,
5749 GtkCellRenderer *cell,
5750 GtkTreeModel *tree_model,
5754 GtkFileChooserDefault *impl = data;
5755 const GtkFileInfo *info = get_list_file_info (impl, iter);
5758 gboolean sensitive = TRUE;
5760 if (!info || gtk_file_info_get_is_folder (info))
5762 g_object_set (cell,"sensitive", sensitive, NULL);
5766 size = gtk_file_info_get_size (info);
5768 if (size < (gint64)1024)
5769 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
5770 else if (size < (gint64)1024*1024)
5771 str = g_strdup_printf (_("%.1f K"), size / (1024.));
5772 else if (size < (gint64)1024*1024*1024)
5773 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
5775 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
5777 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5778 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5783 "sensitive", sensitive,
5790 /* Tree column data callback for the file list; fetches the mtime of a file */
5792 list_mtime_data_func (GtkTreeViewColumn *tree_column,
5793 GtkCellRenderer *cell,
5794 GtkTreeModel *tree_model,
5798 GtkFileChooserDefault *impl;
5799 const GtkFileInfo *info;
5800 GtkFileTime time_mtime, time_now;
5804 gboolean sensitive = TRUE;
5808 info = get_list_file_info (impl, iter);
5818 time_mtime = gtk_file_info_get_modification_time (info);
5819 g_date_set_time (&mtime, (GTime) time_mtime);
5821 time_now = (GTime ) time (NULL);
5822 g_date_set_time (&now, (GTime) time_now);
5824 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
5827 strcpy (buf, _("Today"));
5828 else if (days_diff == 1)
5829 strcpy (buf, _("Yesterday"));
5834 if (days_diff > 1 && days_diff < 7)
5835 format = "%A"; /* Days from last week */
5837 format = "%x"; /* Any other date */
5839 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
5840 strcpy (buf, _("Unknown"));
5843 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5844 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5845 sensitive = gtk_file_info_get_is_folder (info);
5849 "sensitive", sensitive,
5854 _gtk_file_chooser_default_new (const char *file_system)
5856 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
5857 "file-system-backend", file_system,
5862 location_entry_create (GtkFileChooserDefault *impl,
5867 entry = _gtk_file_chooser_entry_new (TRUE);
5868 /* Pick a good width for the entry */
5869 gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
5870 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
5871 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
5872 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (entry), impl->action);
5875 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry),
5876 gtk_file_path_new_steal ((gchar *)path));
5877 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), path);
5881 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
5882 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5883 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5884 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), "");
5885 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5886 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5887 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry),
5888 gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
5890 g_assert_not_reached ();
5893 return GTK_WIDGET (entry);
5897 update_from_entry (GtkFileChooserDefault *impl,
5899 GtkFileChooserEntry *chooser_entry)
5901 const GtkFilePath *folder_path;
5902 const char *file_part;
5904 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
5905 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
5907 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
5909 error_message_with_parent (parent,
5910 _("Cannot change to the folder you specified as it is an invalid path."));
5914 if (file_part[0] == '\0')
5915 return change_folder_and_display_error (impl, folder_path);
5918 GtkFileFolder *folder = NULL;
5919 GtkFilePath *subfolder_path = NULL;
5920 GtkFileInfo *info = NULL;
5926 /* If the file part is non-empty, we need to figure out if it refers to a
5927 * folder within folder. We could optimize the case here where the folder
5928 * is already loaded for one of our tree models.
5932 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
5936 error_getting_info_dialog (impl, folder_path, error);
5941 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
5943 if (!subfolder_path)
5948 uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
5949 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
5952 error_message (impl, msg);
5959 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
5963 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5964 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5966 if (!change_folder_and_display_error (impl, folder_path))
5969 gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
5972 error_getting_info_dialog (impl, subfolder_path, error);
5977 if (gtk_file_info_get_is_folder (info))
5978 result = change_folder_and_display_error (impl, subfolder_path);
5984 result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
5987 _("Could not select %s:\n%s"),
5988 subfolder_path, error);
5994 g_object_unref (folder);
5996 gtk_file_path_free (subfolder_path);
5999 gtk_file_info_free (info);
6004 g_assert_not_reached ();
6008 location_popup_handler (GtkFileChooserDefault *impl,
6012 GtkWindow *toplevel;
6018 const char *accept_stock;
6022 toplevel = get_toplevel (GTK_WIDGET (impl));
6024 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6025 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6027 title = _("Open Location");
6028 accept_stock = GTK_STOCK_OPEN;
6032 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6033 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6034 title = _("Save in Location");
6035 accept_stock = GTK_STOCK_SAVE;
6038 dialog = gtk_dialog_new_with_buttons (title,
6040 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
6041 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
6042 accept_stock, GTK_RESPONSE_ACCEPT,
6044 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
6045 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
6046 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
6047 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
6049 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
6050 GTK_RESPONSE_ACCEPT,
6051 GTK_RESPONSE_CANCEL,
6054 hbox = gtk_hbox_new (FALSE, 12);
6055 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
6056 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
6058 label = gtk_label_new_with_mnemonic (_("_Location:"));
6059 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
6061 entry = location_entry_create (impl, path);
6063 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
6064 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
6068 gtk_widget_show_all (dialog);
6069 /* If the dialog is brought up by typing the first characters
6070 * of a path, unselect the text in the entry, so that you can
6071 * just type on without erasing the initial part.
6074 gtk_editable_select_region (GTK_EDITABLE (entry), -1, -1);
6078 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
6080 if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
6082 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6083 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6085 gtk_widget_grab_focus (impl->browse_files_tree_view);
6089 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6090 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6091 gtk_widget_grab_focus (impl->save_file_name_entry);
6099 GtkWindow *toplevel;
6101 toplevel = get_toplevel (GTK_WIDGET (impl));
6102 if (toplevel && toplevel->focus_widget)
6103 gtk_widget_grab_focus (toplevel->focus_widget);
6106 gtk_widget_destroy (dialog);
6109 /* Handler for the "up-folder" keybinding signal */
6111 up_folder_handler (GtkFileChooserDefault *impl)
6113 pending_op_queue (impl, PENDING_OP_SELECT_PATH, impl->current_folder);
6114 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
6117 /* Handler for the "down-folder" keybinding signal */
6119 down_folder_handler (GtkFileChooserDefault *impl)
6121 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
6124 /* Handler for the "home-folder" keybinding signal */
6126 home_folder_handler (GtkFileChooserDefault *impl)
6131 if (!impl->has_home)
6132 return; /* Should we put up an error dialog? */
6134 pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
6135 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
6136 g_assert_not_reached ();
6138 shortcuts_activate_iter (impl, &iter);
6143 /* Drag and drop interfaces */
6146 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
6151 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
6156 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
6158 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
6161 ShortcutsModelFilter *model;
6165 model = SHORTCUTS_MODEL_FILTER (drag_source);
6167 pos = *gtk_tree_path_get_indices (path);
6168 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
6170 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
6173 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
6175 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
6177 GtkSelectionData *selection_data)
6179 ShortcutsModelFilter *model;
6181 model = SHORTCUTS_MODEL_FILTER (drag_source);
6188 /* Fill the GtkTreeDragSourceIface vtable */
6190 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
6192 iface->row_draggable = shortcuts_model_filter_row_draggable;
6193 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
6197 /* Fill the GtkTreeDragDestIface vtable */
6199 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
6201 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
6202 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
6206 static GtkTreeModel *
6207 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
6208 GtkTreeModel *child_model,
6211 ShortcutsModelFilter *model;
6213 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
6214 "child_model", child_model,
6215 "virtual_root", root,
6220 return GTK_TREE_MODEL (model);