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_map (GtkWidget *widget);
307 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
308 GtkWidget *previous_toplevel);
309 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
310 GtkStyle *previous_style);
311 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
312 GdkScreen *previous_screen);
314 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
315 const GtkFilePath *path,
317 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
318 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
320 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
321 const GtkFilePath *path,
323 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
324 const GtkFilePath *path);
325 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
326 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
327 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
328 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
329 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
330 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
331 GtkFileFilter *filter);
332 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
333 GtkFileFilter *filter);
334 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
335 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
336 const GtkFilePath *path,
338 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
339 const GtkFilePath *path,
341 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
343 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
345 gint *default_height);
346 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
347 gboolean *resize_horizontally,
348 gboolean *resize_vertically);
349 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
350 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
352 static void location_popup_handler (GtkFileChooserDefault *impl,
354 static void up_folder_handler (GtkFileChooserDefault *impl);
355 static void down_folder_handler (GtkFileChooserDefault *impl);
356 static void home_folder_handler (GtkFileChooserDefault *impl);
357 static void update_appearance (GtkFileChooserDefault *impl);
359 static void set_current_filter (GtkFileChooserDefault *impl,
360 GtkFileFilter *filter);
361 static void check_preview_change (GtkFileChooserDefault *impl);
363 static void filter_combo_changed (GtkComboBox *combo_box,
364 GtkFileChooserDefault *impl);
365 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
367 GtkTreeViewColumn *column,
368 GtkFileChooserDefault *impl);
370 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
372 GtkFileChooserDefault *impl);
374 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
377 gboolean path_currently_selected,
379 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
381 static int shortcuts_get_index (GtkFileChooserDefault *impl,
382 ShortcutsIndex where);
383 static int shortcut_find_position (GtkFileChooserDefault *impl,
384 const GtkFilePath *path);
386 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
388 static gboolean list_select_func (GtkTreeSelection *selection,
391 gboolean path_currently_selected,
394 static void list_selection_changed (GtkTreeSelection *tree_selection,
395 GtkFileChooserDefault *impl);
396 static void list_row_activated (GtkTreeView *tree_view,
398 GtkTreeViewColumn *column,
399 GtkFileChooserDefault *impl);
401 static void select_func (GtkFileSystemModel *model,
406 static void path_bar_clicked (GtkPathBar *path_bar,
407 GtkFilePath *file_path,
408 gboolean child_is_hidden,
409 GtkFileChooserDefault *impl);
411 static void add_bookmark_button_clicked_cb (GtkButton *button,
412 GtkFileChooserDefault *impl);
413 static void remove_bookmark_button_clicked_cb (GtkButton *button,
414 GtkFileChooserDefault *impl);
416 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
417 GtkCellRenderer *cell,
418 GtkTreeModel *tree_model,
421 static void list_name_data_func (GtkTreeViewColumn *tree_column,
422 GtkCellRenderer *cell,
423 GtkTreeModel *tree_model,
427 static void list_size_data_func (GtkTreeViewColumn *tree_column,
428 GtkCellRenderer *cell,
429 GtkTreeModel *tree_model,
433 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
434 GtkCellRenderer *cell,
435 GtkTreeModel *tree_model,
439 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
442 static void load_remove_timer (GtkFileChooserDefault *impl);
444 static GObjectClass *parent_class;
448 /* Drag and drop interface declarations */
451 GtkTreeModelFilter parent;
453 GtkFileChooserDefault *impl;
454 } ShortcutsModelFilter;
457 GtkTreeModelFilterClass parent_class;
458 } ShortcutsModelFilterClass;
460 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
461 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
463 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
465 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
466 _shortcuts_model_filter,
467 GTK_TYPE_TREE_MODEL_FILTER,
468 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
469 shortcuts_model_filter_drag_source_iface_init));
471 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
472 GtkTreeModel *child_model,
478 _gtk_file_chooser_default_get_type (void)
480 static GType file_chooser_default_type = 0;
482 if (!file_chooser_default_type)
484 static const GTypeInfo file_chooser_default_info =
486 sizeof (GtkFileChooserDefaultClass),
487 NULL, /* base_init */
488 NULL, /* base_finalize */
489 (GClassInitFunc) gtk_file_chooser_default_class_init,
490 NULL, /* class_finalize */
491 NULL, /* class_data */
492 sizeof (GtkFileChooserDefault),
494 (GInstanceInitFunc) gtk_file_chooser_default_init,
497 static const GInterfaceInfo file_chooser_info =
499 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
500 NULL, /* interface_finalize */
501 NULL /* interface_data */
504 static const GInterfaceInfo file_chooser_embed_info =
506 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
507 NULL, /* interface_finalize */
508 NULL /* interface_data */
511 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
512 &file_chooser_default_info, 0);
514 g_type_add_interface_static (file_chooser_default_type,
515 GTK_TYPE_FILE_CHOOSER,
517 g_type_add_interface_static (file_chooser_default_type,
518 GTK_TYPE_FILE_CHOOSER_EMBED,
519 &file_chooser_embed_info);
522 return file_chooser_default_type;
526 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
528 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
529 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
530 GtkBindingSet *binding_set;
532 parent_class = g_type_class_peek_parent (class);
534 gobject_class->finalize = gtk_file_chooser_default_finalize;
535 gobject_class->constructor = gtk_file_chooser_default_constructor;
536 gobject_class->set_property = gtk_file_chooser_default_set_property;
537 gobject_class->get_property = gtk_file_chooser_default_get_property;
538 gobject_class->dispose = gtk_file_chooser_default_dispose;
540 widget_class->show_all = gtk_file_chooser_default_show_all;
541 widget_class->map = gtk_file_chooser_default_map;
542 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
543 widget_class->style_set = gtk_file_chooser_default_style_set;
544 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
546 signals[LOCATION_POPUP] =
547 _gtk_binding_signal_new ("location-popup",
548 G_OBJECT_CLASS_TYPE (class),
549 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
550 G_CALLBACK (location_popup_handler),
552 _gtk_marshal_VOID__STRING,
553 G_TYPE_NONE, 1, G_TYPE_STRING);
555 _gtk_binding_signal_new ("up-folder",
556 G_OBJECT_CLASS_TYPE (class),
557 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
558 G_CALLBACK (up_folder_handler),
560 _gtk_marshal_VOID__VOID,
562 signals[DOWN_FOLDER] =
563 _gtk_binding_signal_new ("down-folder",
564 G_OBJECT_CLASS_TYPE (class),
565 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
566 G_CALLBACK (down_folder_handler),
568 _gtk_marshal_VOID__VOID,
570 signals[HOME_FOLDER] =
571 _gtk_binding_signal_new ("home-folder",
572 G_OBJECT_CLASS_TYPE (class),
573 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
574 G_CALLBACK (home_folder_handler),
576 _gtk_marshal_VOID__VOID,
579 binding_set = gtk_binding_set_by_class (class);
581 gtk_binding_entry_add_signal (binding_set,
582 GDK_l, GDK_CONTROL_MASK,
584 1, G_TYPE_STRING, "");
586 gtk_binding_entry_add_signal (binding_set,
589 1, G_TYPE_STRING, "/");
591 gtk_binding_entry_add_signal (binding_set,
592 GDK_Up, GDK_MOD1_MASK,
595 gtk_binding_entry_add_signal (binding_set,
599 gtk_binding_entry_add_signal (binding_set,
600 GDK_KP_Up, GDK_MOD1_MASK,
604 gtk_binding_entry_add_signal (binding_set,
605 GDK_Down, GDK_MOD1_MASK,
608 gtk_binding_entry_add_signal (binding_set,
609 GDK_KP_Down, GDK_MOD1_MASK,
613 gtk_binding_entry_add_signal (binding_set,
614 GDK_Home, GDK_MOD1_MASK,
617 gtk_binding_entry_add_signal (binding_set,
618 GDK_KP_Home, GDK_MOD1_MASK,
622 _gtk_file_chooser_install_properties (gobject_class);
624 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
625 P_("Default file chooser backend"),
626 P_("Name of the GtkFileChooser backend to use by default"),
632 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
634 iface->select_path = gtk_file_chooser_default_select_path;
635 iface->unselect_path = gtk_file_chooser_default_unselect_path;
636 iface->select_all = gtk_file_chooser_default_select_all;
637 iface->unselect_all = gtk_file_chooser_default_unselect_all;
638 iface->get_paths = gtk_file_chooser_default_get_paths;
639 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
640 iface->get_file_system = gtk_file_chooser_default_get_file_system;
641 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
642 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
643 iface->set_current_name = gtk_file_chooser_default_set_current_name;
644 iface->add_filter = gtk_file_chooser_default_add_filter;
645 iface->remove_filter = gtk_file_chooser_default_remove_filter;
646 iface->list_filters = gtk_file_chooser_default_list_filters;
647 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
648 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
649 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
653 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
655 iface->get_default_size = gtk_file_chooser_default_get_default_size;
656 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
657 iface->should_respond = gtk_file_chooser_default_should_respond;
658 iface->initial_focus = gtk_file_chooser_default_initial_focus;
661 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
663 impl->local_only = TRUE;
664 impl->preview_widget_active = TRUE;
665 impl->use_preview_label = TRUE;
666 impl->select_multiple = FALSE;
667 impl->show_hidden = FALSE;
668 impl->icon_size = FALLBACK_ICON_SIZE;
669 impl->load_state = LOAD_FINISHED;
670 impl->pending_op = PENDING_OP_NONE;
671 impl->pending_select_path = NULL;
673 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (impl), TRUE);
674 gtk_box_set_spacing (GTK_BOX (impl), 12);
676 impl->tooltips = gtk_tooltips_new ();
677 g_object_ref (impl->tooltips);
678 gtk_object_sink (GTK_OBJECT (impl->tooltips));
681 /* Frees the data columns for the specified iter in the shortcuts model*/
683 shortcuts_free_row_data (GtkFileChooserDefault *impl,
689 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
690 SHORTCUTS_COL_DATA, &col_data,
691 SHORTCUTS_COL_IS_VOLUME, &is_volume,
698 GtkFileSystemVolume *volume;
701 gtk_file_system_volume_free (impl->file_system, volume);
708 gtk_file_path_free (path);
712 /* Frees all the data columns in the shortcuts model */
714 shortcuts_free (GtkFileChooserDefault *impl)
718 if (!impl->shortcuts_model)
721 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
724 shortcuts_free_row_data (impl, &iter);
726 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
728 g_object_unref (impl->shortcuts_model);
729 impl->shortcuts_model = NULL;
733 gtk_file_chooser_default_finalize (GObject *object)
735 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
738 if (impl->shortcuts_filter_model)
739 g_object_unref (impl->shortcuts_filter_model);
741 shortcuts_free (impl);
743 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
744 impl->volumes_changed_id = 0;
745 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
746 impl->bookmarks_changed_id = 0;
747 g_object_unref (impl->file_system);
749 for (l = impl->filters; l; l = l->next)
751 GtkFileFilter *filter;
753 filter = GTK_FILE_FILTER (l->data);
754 g_object_unref (filter);
756 g_slist_free (impl->filters);
758 if (impl->current_filter)
759 g_object_unref (impl->current_filter);
761 if (impl->current_volume_path)
762 gtk_file_path_free (impl->current_volume_path);
764 if (impl->current_folder)
765 gtk_file_path_free (impl->current_folder);
767 if (impl->preview_path)
768 gtk_file_path_free (impl->preview_path);
770 if (impl->pending_op == PENDING_OP_SELECT_PATH)
772 g_assert (impl->pending_select_path != NULL);
773 gtk_file_path_free (impl->pending_select_path);
776 load_remove_timer (impl);
778 /* Free all the Models we have */
779 if (impl->browse_files_model)
780 g_object_unref (impl->browse_files_model);
782 if (impl->sort_model)
783 g_object_unref (impl->sort_model);
785 g_free (impl->preview_display_name);
787 g_free (impl->edited_new_text);
789 g_object_unref (impl->tooltips);
791 G_OBJECT_CLASS (parent_class)->finalize (object);
794 /* Shows an error dialog set as transient for the specified window */
796 error_message_with_parent (GtkWindow *parent,
802 dialog = gtk_message_dialog_new (parent,
803 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
808 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
810 gtk_dialog_run (GTK_DIALOG (dialog));
811 gtk_widget_destroy (dialog);
814 /* Returns a toplevel GtkWindow, or NULL if none */
816 get_toplevel (GtkWidget *widget)
820 toplevel = gtk_widget_get_toplevel (widget);
821 if (!GTK_WIDGET_TOPLEVEL (toplevel))
824 return GTK_WINDOW (toplevel);
827 /* Shows an error dialog for the file chooser */
829 error_message (GtkFileChooserDefault *impl,
833 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
836 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
838 error_dialog (GtkFileChooserDefault *impl,
840 const GtkFilePath *path,
849 uri = gtk_file_system_path_to_uri (impl->file_system, path);
850 text = g_strdup_printf (msg, uri);
851 error_message (impl, text, error->message);
854 g_error_free (error);
858 /* Displays an error message about not being able to get information for a file.
859 * Frees the GError as well.
862 error_getting_info_dialog (GtkFileChooserDefault *impl,
863 const GtkFilePath *path,
867 _("Could not retrieve information about the file"),
871 /* Shows an error dialog about not being able to add a bookmark */
873 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
874 const GtkFilePath *path,
878 _("Could not add a bookmark"),
882 /* Shows an error dialog about not being able to remove a bookmark */
884 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
885 const GtkFilePath *path,
889 _("Could not remove bookmark"),
893 /* Shows an error dialog about not being able to create a folder */
895 error_creating_folder_dialog (GtkFileChooserDefault *impl,
896 const GtkFilePath *path,
900 _("The folder could not be created"),
904 /* Shows an error dialog about not being able to create a filename */
906 error_building_filename_dialog (GtkFileChooserDefault *impl,
907 const GtkFilePath *folder_part,
908 const char *file_part,
911 error_dialog (impl, _("Invalid file name"),
915 /* Shows an error dialog when we cannot switch to a folder */
917 error_changing_folder_dialog (GtkFileChooserDefault *impl,
918 const GtkFilePath *path,
921 error_dialog (impl, _("The folder contents could not be displayed"),
925 /* Changes folders, displaying an error dialog if this fails */
927 change_folder_and_display_error (GtkFileChooserDefault *impl,
928 const GtkFilePath *path)
932 GtkFilePath *path_copy;
934 /* We copy the path because of this case:
936 * list_row_activated()
937 * fetches path from model; path belongs to the model (*)
938 * calls change_folder_and_display_error()
939 * calls _gtk_file_chooser_set_current_folder_path()
940 * changing folders fails, sets model to NULL, thus freeing the path in (*)
943 path_copy = gtk_file_path_copy (path);
946 result = _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path_copy, &error);
949 error_changing_folder_dialog (impl, path_copy, error);
951 gtk_file_path_free (path_copy);
957 update_preview_widget_visibility (GtkFileChooserDefault *impl)
959 if (impl->use_preview_label)
961 if (!impl->preview_label)
963 impl->preview_label = gtk_label_new (impl->preview_display_name);
964 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
965 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
966 gtk_widget_show (impl->preview_label);
971 if (impl->preview_label)
973 gtk_widget_destroy (impl->preview_label);
974 impl->preview_label = NULL;
978 if (impl->preview_widget_active && impl->preview_widget)
979 gtk_widget_show (impl->preview_box);
981 gtk_widget_hide (impl->preview_box);
983 g_signal_emit_by_name (impl, "default-size-changed");
987 set_preview_widget (GtkFileChooserDefault *impl,
988 GtkWidget *preview_widget)
990 if (preview_widget == impl->preview_widget)
993 if (impl->preview_widget)
994 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
995 impl->preview_widget);
997 impl->preview_widget = preview_widget;
998 if (impl->preview_widget)
1000 gtk_widget_show (impl->preview_widget);
1001 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1002 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1003 impl->preview_widget,
1004 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1007 update_preview_widget_visibility (impl);
1010 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1012 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1016 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1022 gboolean pixbuf_visible;
1025 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1026 SHORTCUTS_COL_DATA, &data,
1027 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1028 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1031 if (pixbuf_visible && data)
1035 GtkFileSystemVolume *volume;
1038 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1039 impl->icon_size, NULL);
1043 const GtkFilePath *path;
1046 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1047 impl->icon_size, NULL);
1050 gtk_list_store_set (impl->shortcuts_model, &iter,
1051 SHORTCUTS_COL_PIXBUF, pixbuf,
1054 g_object_unref (pixbuf);
1056 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1059 /* If a shortcut corresponds to the current folder, selects it */
1061 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1063 GtkTreeSelection *selection;
1067 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1069 pos = shortcut_find_position (impl, impl->current_folder);
1072 gtk_tree_selection_unselect_all (selection);
1076 path = gtk_tree_path_new_from_indices (pos, -1);
1077 gtk_tree_selection_select_path (selection, path);
1078 gtk_tree_path_free (path);
1081 /* Convenience function to get the display name and icon info for a path */
1082 static GtkFileInfo *
1083 get_file_info (GtkFileSystem *file_system,
1084 const GtkFilePath *path,
1088 GtkFilePath *parent_path;
1089 GtkFileFolder *parent_folder;
1096 if (!gtk_file_system_get_parent (file_system, path, &parent_path, &tmp))
1099 parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
1100 GTK_FILE_INFO_DISPLAY_NAME
1101 | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
1106 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, &tmp);
1107 g_object_unref (parent_folder);
1111 gtk_file_path_free (parent_path);
1116 GTK_FILE_CHOOSER_ERROR,
1117 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
1118 _("Could not get information about '%s': %s"),
1119 gtk_file_path_get_string (path),
1127 /* Returns whether a path is a folder */
1129 check_is_folder (GtkFileSystem *file_system,
1130 const GtkFilePath *path,
1136 /* Use get_file_info() rather than trying get_folder() and checking
1137 * for an error directly because older versions of the gnome-vfs
1138 * backend don't return an error immediately. This way is also
1139 * more efficient if we already have the parent folder.
1141 info = get_file_info (file_system, path, FALSE, error);
1146 is_folder = gtk_file_info_get_is_folder (info);
1150 GTK_FILE_CHOOSER_ERROR,
1151 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
1153 gtk_file_info_get_display_name (info),
1154 g_strerror (ENOTDIR));
1156 gtk_file_info_free (info);
1161 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1162 * inserts a volume. A position of -1 indicates the end of the tree.
1165 shortcuts_insert_path (GtkFileChooserDefault *impl,
1168 GtkFileSystemVolume *volume,
1169 const GtkFilePath *path,
1182 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1183 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1184 impl->icon_size, NULL);
1188 if (!check_is_folder (impl->file_system, path, error))
1192 label_copy = g_strdup (label);
1195 GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
1200 label_copy = g_strdup (gtk_file_info_get_display_name (info));
1201 gtk_file_info_free (info);
1204 data = gtk_file_path_copy (path);
1205 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1206 impl->icon_size, NULL);
1210 gtk_list_store_append (impl->shortcuts_model, &iter);
1212 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1214 gtk_list_store_set (impl->shortcuts_model, &iter,
1215 SHORTCUTS_COL_PIXBUF, pixbuf,
1216 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1217 SHORTCUTS_COL_NAME, label_copy,
1218 SHORTCUTS_COL_DATA, data,
1219 SHORTCUTS_COL_IS_VOLUME, is_volume,
1220 SHORTCUTS_COL_REMOVABLE, removable,
1223 g_free (label_copy);
1226 g_object_unref (pixbuf);
1231 /* Appends an item for the user's home directory to the shortcuts model */
1233 shortcuts_append_home (GtkFileChooserDefault *impl)
1236 GtkFilePath *home_path;
1239 home = g_get_home_dir ();
1243 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1246 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1247 if (!impl->has_home)
1248 error_getting_info_dialog (impl, home_path, error);
1250 gtk_file_path_free (home_path);
1253 /* Appends the ~/Desktop directory to the shortcuts model */
1255 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1261 name = _gtk_file_system_win32_get_desktop ();
1263 const char *home = g_get_home_dir ();
1267 name = g_build_filename (home, "Desktop", NULL);
1270 path = gtk_file_system_filename_to_path (impl->file_system, name);
1273 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1274 /* We do not actually pop up an error dialog if there is no desktop directory
1275 * because some people may really not want to have one.
1278 gtk_file_path_free (path);
1281 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1283 shortcuts_append_paths (GtkFileChooserDefault *impl,
1289 /* As there is no separator now, we want to start there.
1291 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1294 for (; paths; paths = paths->next)
1302 if (impl->local_only &&
1303 !gtk_file_system_path_is_local (impl->file_system, path))
1306 /* NULL GError, but we don't really want to show error boxes here */
1307 if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, NULL, TRUE, NULL))
1311 return num_inserted;
1314 /* Returns the index for the corresponding item in the shortcuts bar */
1316 shortcuts_get_index (GtkFileChooserDefault *impl,
1317 ShortcutsIndex where)
1323 if (where == SHORTCUTS_HOME)
1326 n += impl->has_home ? 1 : 0;
1328 if (where == SHORTCUTS_DESKTOP)
1331 n += impl->has_desktop ? 1 : 0;
1333 if (where == SHORTCUTS_VOLUMES)
1336 n += impl->num_volumes;
1338 if (where == SHORTCUTS_SHORTCUTS)
1341 n += impl->num_shortcuts;
1343 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1346 /* If there are no bookmarks there won't be a separator */
1347 n += (impl->num_bookmarks > 0) ? 1 : 0;
1349 if (where == SHORTCUTS_BOOKMARKS)
1352 n += impl->num_bookmarks;
1354 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1359 if (where == SHORTCUTS_CURRENT_FOLDER)
1362 g_assert_not_reached ();
1369 /* Removes the specified number of rows from the shortcuts list */
1371 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1377 path = gtk_tree_path_new_from_indices (start_row, -1);
1379 for (; n_rows; n_rows--)
1383 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1384 g_assert_not_reached ();
1386 shortcuts_free_row_data (impl, &iter);
1387 gtk_list_store_remove (impl->shortcuts_model, &iter);
1390 gtk_tree_path_free (path);
1393 /* Adds all the file system volumes to the shortcuts model */
1395 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1400 gboolean old_changing_folders;
1402 old_changing_folders = impl->changing_folder;
1403 impl->changing_folder = TRUE;
1405 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1406 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1407 impl->num_volumes = 0;
1409 list = gtk_file_system_list_volumes (impl->file_system);
1413 for (l = list; l; l = l->next)
1415 GtkFileSystemVolume *volume;
1419 if (impl->local_only)
1421 GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1422 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1423 gtk_file_path_free (base_path);
1427 gtk_file_system_volume_free (impl->file_system, volume);
1432 if (shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL))
1435 gtk_file_system_volume_free (impl->file_system, volume);
1438 impl->num_volumes = n;
1439 g_slist_free (list);
1441 if (impl->shortcuts_filter_model)
1442 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1444 impl->changing_folder = old_changing_folders;
1447 /* Inserts a separator node in the shortcuts list */
1449 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1450 ShortcutsIndex where)
1454 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1456 gtk_list_store_insert (impl->shortcuts_model, &iter,
1457 shortcuts_get_index (impl, where));
1458 gtk_list_store_set (impl->shortcuts_model, &iter,
1459 SHORTCUTS_COL_PIXBUF, NULL,
1460 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1461 SHORTCUTS_COL_NAME, NULL,
1462 SHORTCUTS_COL_DATA, NULL,
1466 /* Updates the list of bookmarks */
1468 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1471 gboolean old_changing_folders;
1473 old_changing_folders = impl->changing_folder;
1474 impl->changing_folder = TRUE;
1476 if (impl->num_bookmarks > 0)
1477 shortcuts_remove_rows (impl,
1478 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1479 impl->num_bookmarks + 1);
1481 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1482 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1483 gtk_file_paths_free (bookmarks);
1485 if (impl->num_bookmarks > 0)
1487 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1489 if (impl->shortcuts_filter_model)
1490 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1492 impl->changing_folder = old_changing_folders;
1495 /* Appends a separator and a row to the shortcuts list for the current folder */
1497 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1502 g_assert (!impl->shortcuts_current_folder_active);
1506 pos = shortcut_find_position (impl, impl->current_folder);
1509 GtkFileSystemVolume *volume;
1510 GtkFilePath *base_path;
1514 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1518 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1520 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1522 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1527 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1529 success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1534 success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1537 gtk_file_system_volume_free (impl->file_system, volume);
1540 gtk_file_path_free (base_path);
1543 shortcuts_remove_rows (impl, pos - 1, 1); /* remove the separator */
1545 impl->shortcuts_current_folder_active = success;
1549 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1552 /* Updates the current folder row in the shortcuts model */
1554 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1558 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1560 if (impl->shortcuts_current_folder_active)
1562 shortcuts_remove_rows (impl, pos, 2);
1563 impl->shortcuts_current_folder_active = FALSE;
1566 shortcuts_add_current_folder (impl);
1569 /* Filter function used for the shortcuts filter model */
1571 shortcuts_filter_cb (GtkTreeModel *model,
1575 GtkFileChooserDefault *impl;
1579 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1581 path = gtk_tree_model_get_path (model, iter);
1585 pos = *gtk_tree_path_get_indices (path);
1586 gtk_tree_path_free (path);
1588 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1591 /* Creates the list model for shortcuts */
1593 shortcuts_model_create (GtkFileChooserDefault *impl)
1595 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1596 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1597 GDK_TYPE_PIXBUF, /* pixbuf */
1598 G_TYPE_STRING, /* name */
1599 G_TYPE_POINTER, /* path or volume */
1600 G_TYPE_BOOLEAN, /* is the previous column a volume? */
1601 G_TYPE_BOOLEAN, /* removable */
1602 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1604 if (impl->file_system)
1606 shortcuts_append_home (impl);
1607 shortcuts_append_desktop (impl);
1608 shortcuts_add_volumes (impl);
1609 shortcuts_add_bookmarks (impl);
1612 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1613 GTK_TREE_MODEL (impl->shortcuts_model),
1616 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1617 shortcuts_filter_cb,
1622 /* Callback used when the "New Folder" button is clicked */
1624 new_folder_button_clicked (GtkButton *button,
1625 GtkFileChooserDefault *impl)
1630 if (!impl->browse_files_model)
1631 return; /* FIXME: this sucks. Disable the New Folder button or something. */
1633 /* Prevent button from being clicked twice */
1634 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
1636 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1638 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1639 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1640 path, impl->list_name_column,
1643 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1644 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1646 impl->list_name_column,
1649 gtk_tree_path_free (path);
1652 /* Idle handler for creating a new folder after editing its name cell, or for
1653 * canceling the editing.
1656 edited_idle_cb (GtkFileChooserDefault *impl)
1658 GDK_THREADS_ENTER ();
1660 g_source_destroy (impl->edited_idle);
1661 impl->edited_idle = NULL;
1663 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1664 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1666 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
1668 if (impl->edited_new_text) /* not cancelled? */
1671 GtkFilePath *file_path;
1674 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, impl->edited_new_text,
1679 if (gtk_file_system_create_folder (impl->file_system, file_path, &error))
1680 change_folder_and_display_error (impl, file_path);
1682 error_creating_folder_dialog (impl, file_path, error);
1684 gtk_file_path_free (file_path);
1687 error_creating_folder_dialog (impl, file_path, error);
1689 g_free (impl->edited_new_text);
1690 impl->edited_new_text = NULL;
1693 GDK_THREADS_LEAVE ();
1699 queue_edited_idle (GtkFileChooserDefault *impl,
1700 const gchar *new_text)
1702 /* We create the folder in an idle handler so that we don't modify the tree
1706 g_assert (!impl->edited_idle);
1707 g_assert (!impl->edited_new_text);
1709 impl->edited_idle = g_idle_source_new ();
1710 g_source_set_closure (impl->edited_idle,
1711 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
1713 g_source_attach (impl->edited_idle, NULL);
1716 impl->edited_new_text = g_strdup (new_text);
1719 /* Callback used from the text cell renderer when the new folder is named */
1721 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1723 const gchar *new_text,
1724 GtkFileChooserDefault *impl)
1726 /* work around bug #154921 */
1727 g_object_set (cell_renderer_text,
1728 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1729 queue_edited_idle (impl, new_text);
1732 /* Callback used from the text cell renderer when the new folder edition gets
1736 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1737 GtkFileChooserDefault *impl)
1739 /* work around bug #154921 */
1740 g_object_set (cell_renderer_text,
1741 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1742 queue_edited_idle (impl, NULL);
1745 /* Creates the widgets for the filter combo box */
1747 filter_create (GtkFileChooserDefault *impl)
1749 impl->filter_combo = gtk_combo_box_new_text ();
1750 g_signal_connect (impl->filter_combo, "changed",
1751 G_CALLBACK (filter_combo_changed), impl);
1753 return impl->filter_combo;
1757 button_new (GtkFileChooserDefault *impl,
1759 const char *stock_id,
1769 button = gtk_button_new ();
1770 hbox = gtk_hbox_new (FALSE, 2);
1771 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1773 gtk_container_add (GTK_CONTAINER (button), align);
1774 gtk_container_add (GTK_CONTAINER (align), hbox);
1775 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1777 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1779 widget = gtk_label_new_with_mnemonic (text);
1780 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1781 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1783 gtk_widget_set_sensitive (button, sensitive);
1784 g_signal_connect (button, "clicked", callback, impl);
1786 gtk_widget_show_all (align);
1789 gtk_widget_show (button);
1794 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1796 shortcut_find_position (GtkFileChooserDefault *impl,
1797 const GtkFilePath *path)
1801 int current_folder_separator_idx;
1803 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1806 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1808 for (i = 0; i < current_folder_separator_idx; i++)
1813 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1814 SHORTCUTS_COL_DATA, &col_data,
1815 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1822 GtkFileSystemVolume *volume;
1823 GtkFilePath *base_path;
1827 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1829 exists = strcmp (gtk_file_path_get_string (path),
1830 gtk_file_path_get_string (base_path)) == 0;
1838 GtkFilePath *model_path;
1840 model_path = col_data;
1842 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1847 gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1853 /* Tries to add a bookmark from a path name */
1855 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1856 const GtkFilePath *path,
1861 if (shortcut_find_position (impl, path) != -1)
1864 /* FIXME: this check really belongs in gtk_file_system_insert_bookmark. */
1866 if (!check_is_folder (impl->file_system, path, &error))
1868 error_adding_bookmark_dialog (impl, path, error);
1873 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
1875 error_adding_bookmark_dialog (impl, path, error);
1883 add_bookmark_foreach_cb (GtkTreeModel *model,
1888 GtkFileChooserDefault *impl;
1889 GtkFileSystemModel *fs_model;
1890 GtkTreeIter child_iter;
1891 const GtkFilePath *file_path;
1893 impl = (GtkFileChooserDefault *) data;
1895 fs_model = impl->browse_files_model;
1896 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1898 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
1899 shortcuts_add_bookmark_from_path (impl, file_path, -1);
1902 /* Adds a bookmark from the currently selected item in the file list */
1904 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
1906 GtkTreeSelection *selection;
1908 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1910 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1911 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
1913 gtk_tree_selection_selected_foreach (selection,
1914 add_bookmark_foreach_cb,
1918 /* Callback used when the "Add bookmark" button is clicked */
1920 add_bookmark_button_clicked_cb (GtkButton *button,
1921 GtkFileChooserDefault *impl)
1923 bookmarks_add_selected_folder (impl);
1926 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
1927 * returns FALSE if no shortcut is selected.
1930 shortcuts_get_selected (GtkFileChooserDefault *impl,
1933 GtkTreeSelection *selection;
1934 GtkTreeIter parent_iter;
1936 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1938 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
1941 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1947 /* Removes the selected bookmarks */
1949 remove_selected_bookmarks (GtkFileChooserDefault *impl)
1958 if (!shortcuts_get_selected (impl, &iter))
1961 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1962 SHORTCUTS_COL_DATA, &col_data,
1963 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1964 SHORTCUTS_COL_REMOVABLE, &removable,
1966 g_assert (col_data != NULL);
1967 g_assert (!is_volume);
1975 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1976 error_removing_bookmark_dialog (impl, path, error);
1979 /* Callback used when the "Remove bookmark" button is clicked */
1981 remove_bookmark_button_clicked_cb (GtkButton *button,
1982 GtkFileChooserDefault *impl)
1984 remove_selected_bookmarks (impl);
1987 struct selection_check_closure {
1988 GtkFileChooserDefault *impl;
1991 gboolean all_folders;
1994 /* Used from gtk_tree_selection_selected_foreach() */
1996 selection_check_foreach_cb (GtkTreeModel *model,
2001 struct selection_check_closure *closure;
2002 GtkTreeIter child_iter;
2003 const GtkFileInfo *info;
2007 closure->num_selected++;
2009 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2011 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2012 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2014 closure->all_folders = closure->all_folders && is_folder;
2015 closure->all_files = closure->all_files && !is_folder;
2018 /* Checks whether the selected items in the file list are all files or all folders */
2020 selection_check (GtkFileChooserDefault *impl,
2022 gboolean *all_files,
2023 gboolean *all_folders)
2025 struct selection_check_closure closure;
2026 GtkTreeSelection *selection;
2028 closure.impl = impl;
2029 closure.num_selected = 0;
2030 closure.all_files = TRUE;
2031 closure.all_folders = TRUE;
2033 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2034 gtk_tree_selection_selected_foreach (selection,
2035 selection_check_foreach_cb,
2038 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2041 *num_selected = closure.num_selected;
2044 *all_files = closure.all_files;
2047 *all_folders = closure.all_folders;
2050 struct get_selected_path_closure {
2051 GtkFileChooserDefault *impl;
2052 const GtkFilePath *path;
2056 get_selected_path_foreach_cb (GtkTreeModel *model,
2061 struct get_selected_path_closure *closure;
2062 GtkTreeIter child_iter;
2066 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2067 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2070 /* Returns a selected path from the file list */
2071 static const GtkFilePath *
2072 get_selected_path (GtkFileChooserDefault *impl)
2074 struct get_selected_path_closure closure;
2075 GtkTreeSelection *selection;
2077 closure.impl = impl;
2078 closure.path = NULL;
2080 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2081 gtk_tree_selection_selected_foreach (selection,
2082 get_selected_path_foreach_cb,
2085 return closure.path;
2089 GtkFileChooserDefault *impl;
2091 } UpdateTooltipData;
2094 update_tooltip (GtkTreeModel *model,
2099 UpdateTooltipData *udata = data;
2100 GtkTreeIter child_iter;
2101 const GtkFileInfo *info;
2103 if (udata->tip == NULL)
2105 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2109 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2110 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2111 gtk_file_info_get_display_name (info));
2116 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2117 * if there are no selected items *and* the current folder is not in the
2118 * bookmarks list. De-sensitize the button otherwise.
2121 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2124 gboolean all_folders;
2128 selection_check (impl, &num_selected, NULL, &all_folders);
2130 if (num_selected == 0)
2131 active = (shortcut_find_position (impl, impl->current_folder) == -1);
2132 else if (num_selected == 1)
2134 const GtkFilePath *path;
2136 path = get_selected_path (impl);
2137 active = all_folders && (shortcut_find_position (impl, path) == -1);
2140 active = all_folders;
2142 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2144 if (impl->browse_files_popup_menu_add_shortcut_item)
2145 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2146 (num_selected == 0) ? FALSE : active);
2150 if (num_selected == 0)
2151 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2152 else if (num_selected > 1)
2153 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2156 GtkTreeSelection *selection;
2157 UpdateTooltipData data;
2159 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2162 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2166 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2171 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2172 * bookmark row is selected in the shortcuts tree.
2175 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2178 gboolean removable = FALSE;
2181 if (shortcuts_get_selected (impl, &iter))
2182 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2183 SHORTCUTS_COL_REMOVABLE, &removable,
2184 SHORTCUTS_COL_NAME, &name,
2187 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2193 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2194 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2202 /* GtkWidget::drag-begin handler for the shortcuts list. */
2204 shortcuts_drag_begin_cb (GtkWidget *widget,
2205 GdkDragContext *context,
2206 GtkFileChooserDefault *impl)
2209 impl->shortcuts_drag_context = g_object_ref (context);
2214 /* Removes the idle handler for outside drags */
2216 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2218 if (!impl->shortcuts_drag_outside_idle)
2221 g_source_destroy (impl->shortcuts_drag_outside_idle);
2222 impl->shortcuts_drag_outside_idle = NULL;
2226 /* GtkWidget::drag-end handler for the shortcuts list. */
2228 shortcuts_drag_end_cb (GtkWidget *widget,
2229 GdkDragContext *context,
2230 GtkFileChooserDefault *impl)
2233 g_object_unref (impl->shortcuts_drag_context);
2235 shortcuts_cancel_drag_outside_idle (impl);
2237 if (!impl->shortcuts_drag_outside)
2240 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2242 impl->shortcuts_drag_outside = FALSE;
2246 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2248 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2249 GdkDragContext *context,
2250 GtkFileChooserDefault *impl)
2252 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2256 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2260 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2263 GtkTreeView *tree_view;
2266 GdkPixmap *row_pixmap;
2271 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2273 /* Find the selected path and get its drag pixmap */
2275 if (!shortcuts_get_selected (impl, &iter))
2276 g_assert_not_reached ();
2278 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2280 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2281 gtk_tree_path_free (path);
2290 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2296 GdkPixmap *composite;
2297 int row_pixmap_width, row_pixmap_height;
2298 int pixbuf_width, pixbuf_height;
2299 int composite_width, composite_height;
2300 int pixbuf_x, pixbuf_y;
2301 GdkGC *gc, *mask_gc;
2303 GdkBitmap *pixbuf_mask;
2305 /* Create pixmap and mask for composite image */
2307 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2308 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2309 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2311 composite_width = MAX (row_pixmap_width, pixbuf_width);
2312 composite_height = MAX (row_pixmap_height, pixbuf_height);
2314 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2316 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2319 pixbuf_x = composite_width - pixbuf_width;
2321 pixbuf_y = (composite_height - pixbuf_height) / 2;
2323 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2324 gc = gdk_gc_new (composite);
2326 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2327 mask_gc = gdk_gc_new (mask);
2329 gdk_gc_set_foreground (mask_gc, &color);
2330 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2333 color.green = 0xffff;
2334 color.blue = 0xffff;
2335 gdk_gc_set_rgb_fg_color (gc, &color);
2336 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2338 /* Composite the row pixmap and the pixbuf */
2340 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2342 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2343 NULL, &pixbuf_mask, 128);
2344 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2347 pixbuf_width, pixbuf_height);
2348 g_object_unref (pixbuf_mask);
2350 gdk_draw_drawable (composite, gc, row_pixmap,
2353 row_pixmap_width, row_pixmap_height);
2355 gdk_gc_set_foreground (mask_gc, &color);
2356 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2358 gdk_draw_pixbuf (composite, gc, pixbuf,
2361 pixbuf_width, pixbuf_height,
2365 g_object_unref (pixbuf);
2366 g_object_unref (row_pixmap);
2368 row_pixmap = composite;
2372 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2374 gtk_tree_view_get_path_at_pos (tree_view,
2375 tree_view->priv->press_start_x,
2376 tree_view->priv->press_start_y,
2382 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2383 gdk_drawable_get_colormap (row_pixmap),
2386 tree_view->priv->press_start_x + 1,
2387 row_pixmap_y + cell_y + 1);
2389 g_object_unref (row_pixmap);
2391 g_object_unref (mask);
2394 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2395 * handler so that we can tell apart the drag_leave event that comes right
2396 * before a drag_drop, from a normal drag_leave. We don't want to set the
2397 * cursor nor the flag in the latter case.
2400 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2402 GDK_THREADS_ENTER ();
2404 shortcuts_drag_set_delete_cursor (impl, TRUE);
2405 impl->shortcuts_drag_outside = TRUE;
2407 shortcuts_cancel_drag_outside_idle (impl);
2409 GDK_THREADS_LEAVE ();
2415 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2419 shortcuts_drag_leave_cb (GtkWidget *widget,
2420 GdkDragContext *context,
2422 GtkFileChooserDefault *impl)
2425 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2427 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2428 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2429 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2431 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2435 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2437 GTK_TREE_VIEW_DROP_BEFORE);
2439 g_signal_stop_emission_by_name (widget, "drag-leave");
2442 /* Computes the appropriate row and position for dropping */
2444 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2448 GtkTreeViewDropPosition *pos)
2450 GtkTreeView *tree_view;
2451 GtkTreeViewColumn *column;
2455 int bookmarks_index;
2457 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2459 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2461 if (!gtk_tree_view_get_path_at_pos (tree_view,
2463 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2469 row = bookmarks_index + impl->num_bookmarks - 1;
2470 *path = gtk_tree_path_new_from_indices (row, -1);
2471 *pos = GTK_TREE_VIEW_DROP_AFTER;
2475 row = *gtk_tree_path_get_indices (*path);
2476 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2477 gtk_tree_path_free (*path);
2479 if (row < bookmarks_index)
2481 row = bookmarks_index;
2482 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2484 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2486 row = bookmarks_index + impl->num_bookmarks - 1;
2487 *pos = GTK_TREE_VIEW_DROP_AFTER;
2491 if (cell_y < cell.height / 2)
2492 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2494 *pos = GTK_TREE_VIEW_DROP_AFTER;
2497 *path = gtk_tree_path_new_from_indices (row, -1);
2500 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2501 * implement the destination side of DnD by hand, due to limitations in
2502 * GtkTreeView's DnD API.
2505 shortcuts_drag_motion_cb (GtkWidget *widget,
2506 GdkDragContext *context,
2510 GtkFileChooserDefault *impl)
2513 GtkTreeViewDropPosition pos;
2514 GdkDragAction action;
2517 if (gtk_drag_get_source_widget (context) == widget)
2519 shortcuts_cancel_drag_outside_idle (impl);
2521 if (impl->shortcuts_drag_outside)
2523 shortcuts_drag_set_delete_cursor (impl, FALSE);
2524 impl->shortcuts_drag_outside = FALSE;
2529 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2530 action = GDK_ACTION_COPY;
2531 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2532 action = GDK_ACTION_MOVE;
2539 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2540 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2541 gtk_tree_path_free (path);
2545 g_signal_stop_emission_by_name (widget, "drag-motion");
2549 gdk_drag_status (context, action, time_);
2556 /* GtkWidget::drag-drop handler for the shortcuts list. */
2558 shortcuts_drag_drop_cb (GtkWidget *widget,
2559 GdkDragContext *context,
2563 GtkFileChooserDefault *impl)
2566 shortcuts_cancel_drag_outside_idle (impl);
2569 g_signal_stop_emission_by_name (widget, "drag-drop");
2573 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2575 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2582 uris = g_uri_list_extract_uris (data);
2584 for (i = 0; uris[i]; i++)
2590 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2594 if (shortcuts_add_bookmark_from_path (impl, path, position))
2597 gtk_file_path_free (path);
2603 g_set_error (&error,
2604 GTK_FILE_CHOOSER_ERROR,
2605 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
2606 _("Could not add a bookmark for '%s' "
2607 "because it is an invalid path name."),
2609 error_adding_bookmark_dialog (impl, path, error);
2616 /* Reorders the selected bookmark to the specified position */
2618 shortcuts_reorder (GtkFileChooserDefault *impl,
2626 int bookmarks_index;
2627 const GtkFilePath *file_path;
2628 GtkFilePath *file_path_copy;
2631 /* Get the selected path */
2633 if (!shortcuts_get_selected (impl, &iter))
2634 g_assert_not_reached ();
2636 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2637 old_position = *gtk_tree_path_get_indices (path);
2638 gtk_tree_path_free (path);
2640 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2641 old_position -= bookmarks_index;
2642 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2644 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2645 SHORTCUTS_COL_DATA, &col_data,
2646 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2648 g_assert (col_data != NULL);
2649 g_assert (!is_volume);
2651 file_path = col_data;
2652 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2654 /* Remove the path from the old position and insert it in the new one */
2656 if (new_position > old_position)
2659 if (old_position == new_position)
2663 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2664 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2666 error_adding_bookmark_dialog (impl, file_path_copy, error);
2670 gtk_file_path_free (file_path_copy);
2673 /* Callback used when we get the drag data for the bookmarks list. We add the
2674 * received URIs as bookmarks if they are folders.
2677 shortcuts_drag_data_received_cb (GtkWidget *widget,
2678 GdkDragContext *context,
2681 GtkSelectionData *selection_data,
2686 GtkFileChooserDefault *impl;
2687 GtkTreePath *tree_path;
2688 GtkTreeViewDropPosition tree_pos;
2690 int bookmarks_index;
2692 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2694 /* Compute position */
2696 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2698 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2699 position = *gtk_tree_path_get_indices (tree_path);
2700 gtk_tree_path_free (tree_path);
2702 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2705 g_assert (position >= bookmarks_index);
2706 position -= bookmarks_index;
2708 if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2709 shortcuts_drop_uris (impl, selection_data->data, position);
2710 else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2711 shortcuts_reorder (impl, position);
2713 g_signal_stop_emission_by_name (widget, "drag-data-received");
2716 /* Callback used when the selection in the shortcuts tree changes */
2718 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
2719 GtkFileChooserDefault *impl)
2721 bookmarks_check_remove_sensitivity (impl);
2725 shortcuts_row_separator_func (GtkTreeModel *model,
2729 gint column = GPOINTER_TO_INT (data);
2732 gtk_tree_model_get (model, iter, column, &text, -1);
2742 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
2743 * keypresses before the TreeView gets them.
2746 tree_view_keybinding_cb (GtkWidget *tree_view,
2748 GtkFileChooserDefault *impl)
2750 if (event->keyval == GDK_slash &&
2751 ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
2753 location_popup_handler (impl, "/");
2761 /* Creates the widgets for the shortcuts and bookmarks tree */
2763 shortcuts_list_create (GtkFileChooserDefault *impl)
2766 GtkTreeSelection *selection;
2767 GtkTreeViewColumn *column;
2768 GtkCellRenderer *renderer;
2770 /* Scrolled window */
2772 swin = gtk_scrolled_window_new (NULL, NULL);
2773 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2774 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2775 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2777 gtk_widget_show (swin);
2781 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
2782 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2783 G_CALLBACK (tree_view_keybinding_cb), impl);
2784 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Shortcuts"));
2785 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
2787 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
2789 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2791 shortcuts_source_targets,
2792 num_shortcuts_source_targets,
2795 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
2796 GTK_DEST_DEFAULT_ALL,
2797 shortcuts_dest_targets,
2798 num_shortcuts_dest_targets,
2799 GDK_ACTION_COPY | GDK_ACTION_MOVE);
2801 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2802 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
2803 gtk_tree_selection_set_select_function (selection,
2804 shortcuts_select_func,
2807 g_signal_connect (selection, "changed",
2808 G_CALLBACK (shortcuts_selection_changed_cb), impl);
2810 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
2811 G_CALLBACK (shortcuts_row_activated_cb), impl);
2813 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2814 G_CALLBACK (shortcuts_key_press_event_cb), impl);
2816 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
2817 G_CALLBACK (shortcuts_drag_begin_cb), impl);
2818 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
2819 G_CALLBACK (shortcuts_drag_end_cb), impl);
2820 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
2821 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
2823 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
2824 G_CALLBACK (shortcuts_drag_leave_cb), impl);
2825 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
2826 G_CALLBACK (shortcuts_drag_motion_cb), impl);
2827 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
2828 G_CALLBACK (shortcuts_drag_drop_cb), impl);
2829 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
2830 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
2832 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
2833 gtk_widget_show (impl->browse_shortcuts_tree_view);
2837 column = gtk_tree_view_column_new ();
2838 gtk_tree_view_column_set_title (column, _("Folder"));
2840 renderer = gtk_cell_renderer_pixbuf_new ();
2841 gtk_tree_view_column_pack_start (column, renderer, FALSE);
2842 gtk_tree_view_column_set_attributes (column, renderer,
2843 "pixbuf", SHORTCUTS_COL_PIXBUF,
2844 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2847 renderer = gtk_cell_renderer_text_new ();
2848 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2849 gtk_tree_view_column_set_attributes (column, renderer,
2850 "text", SHORTCUTS_COL_NAME,
2853 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2854 shortcuts_row_separator_func,
2855 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
2858 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
2863 /* Creates the widgets for the shortcuts/bookmarks pane */
2865 shortcuts_pane_create (GtkFileChooserDefault *impl,
2866 GtkSizeGroup *size_group)
2872 vbox = gtk_vbox_new (FALSE, 6);
2873 gtk_widget_show (vbox);
2875 /* Shortcuts tree */
2877 widget = shortcuts_list_create (impl);
2878 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
2880 /* Box for buttons */
2882 hbox = gtk_hbox_new (TRUE, 6);
2883 gtk_size_group_add_widget (size_group, hbox);
2884 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2885 gtk_widget_show (hbox);
2887 /* Add bookmark button */
2889 impl->browse_shortcuts_add_button = button_new (impl,
2894 G_CALLBACK (add_bookmark_button_clicked_cb));
2895 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
2896 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
2897 _("Add the selected folder to the bookmarks"), NULL);
2899 /* Remove bookmark button */
2901 impl->browse_shortcuts_remove_button = button_new (impl,
2906 G_CALLBACK (remove_bookmark_button_clicked_cb));
2907 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
2908 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2909 _("Remove the selected bookmark"), NULL);
2914 /* Handles key press events on the file list, so that we can trap Enter to
2915 * activate the default button on our own. Also, checks to see if '/' has been
2916 * pressed. See comment by tree_view_keybinding_cb() for more details.
2919 trap_activate_cb (GtkWidget *widget,
2923 GtkFileChooserDefault *impl;
2925 impl = (GtkFileChooserDefault *) data;
2927 if (event->keyval == GDK_slash &&
2928 ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
2930 location_popup_handler (impl, "/");
2934 if ((event->keyval == GDK_Return
2935 || event->keyval == GDK_ISO_Enter
2936 || event->keyval == GDK_KP_Enter
2937 || event->keyval == GDK_space)
2938 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
2939 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
2943 window = get_toplevel (widget);
2945 && widget != window->default_widget
2946 && !(widget == window->focus_widget &&
2947 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
2949 gtk_window_activate_default (window);
2957 /* Callback used when the file list's popup menu is detached */
2959 popup_menu_detach_cb (GtkWidget *attach_widget,
2962 GtkFileChooserDefault *impl;
2964 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
2965 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
2967 impl->browse_files_popup_menu = NULL;
2968 impl->browse_files_popup_menu_add_shortcut_item = NULL;
2969 impl->browse_files_popup_menu_hidden_files_item = NULL;
2972 /* Callback used when the "Add to Shortcuts" menu item is activated */
2974 add_to_shortcuts_cb (GtkMenuItem *item,
2975 GtkFileChooserDefault *impl)
2977 bookmarks_add_selected_folder (impl);
2980 /* Callback used when the "Show Hidden Files" menu item is toggled */
2982 show_hidden_toggled_cb (GtkCheckMenuItem *item,
2983 GtkFileChooserDefault *impl)
2986 "show-hidden", gtk_check_menu_item_get_active (item),
2990 /* Constructs the popup menu for the file list if needed */
2992 file_list_build_popup_menu (GtkFileChooserDefault *impl)
2996 if (impl->browse_files_popup_menu)
2999 impl->browse_files_popup_menu = gtk_menu_new ();
3000 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3001 impl->browse_files_tree_view,
3002 popup_menu_detach_cb);
3004 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Shortcuts"));
3005 impl->browse_files_popup_menu_add_shortcut_item = item;
3006 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3007 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3008 gtk_widget_set_sensitive (item, FALSE);
3009 g_signal_connect (item, "activate",
3010 G_CALLBACK (add_to_shortcuts_cb), impl);
3011 gtk_widget_show (item);
3012 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3014 item = gtk_separator_menu_item_new ();
3015 gtk_widget_show (item);
3016 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3018 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3019 impl->browse_files_popup_menu_hidden_files_item = item;
3020 g_signal_connect (item, "toggled",
3021 G_CALLBACK (show_hidden_toggled_cb), impl);
3022 gtk_widget_show (item);
3023 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3026 /* Updates the popup menu for the file list, creating it if necessary */
3028 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3030 file_list_build_popup_menu (impl);
3032 /* The sensitivity of the Add to Shortcuts item is set in
3033 * bookmarks_check_add_sensitivity()
3036 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3037 G_CALLBACK (show_hidden_toggled_cb), impl);
3038 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3040 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3041 G_CALLBACK (show_hidden_toggled_cb), impl);
3045 popup_position_func (GtkMenu *menu,
3051 GtkWidget *widget = GTK_WIDGET (user_data);
3052 GdkScreen *screen = gtk_widget_get_screen (widget);
3055 GdkRectangle monitor;
3057 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3059 gdk_window_get_origin (widget->window, x, y);
3061 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3063 *x += (widget->allocation.width - req.width) / 2;
3064 *y += (widget->allocation.height - req.height) / 2;
3066 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3067 gtk_menu_set_monitor (menu, monitor_num);
3068 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3070 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3071 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3077 file_list_popup_menu (GtkFileChooserDefault *impl,
3078 GdkEventButton *event)
3080 file_list_update_popup_menu (impl);
3082 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3083 NULL, NULL, NULL, NULL,
3084 event->button, event->time);
3087 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3089 popup_position_func, impl->browse_files_tree_view,
3090 0, GDK_CURRENT_TIME);
3091 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3097 /* Callback used for the GtkWidget::popup-menu signal of the file list */
3099 list_popup_menu_cb (GtkWidget *widget,
3100 GtkFileChooserDefault *impl)
3102 file_list_popup_menu (impl, NULL);
3106 /* Callback used when a button is pressed on the file list. We trap button 3 to
3107 * bring up a popup menu.
3110 list_button_press_event_cb (GtkWidget *widget,
3111 GdkEventButton *event,
3112 GtkFileChooserDefault *impl)
3114 if (event->button != 3)
3117 file_list_popup_menu (impl, event);
3121 /* Creates the widgets for the file list */
3123 create_file_list (GtkFileChooserDefault *impl)
3126 GtkTreeSelection *selection;
3127 GtkTreeViewColumn *column;
3128 GtkCellRenderer *renderer;
3130 /* Scrolled window */
3132 swin = gtk_scrolled_window_new (NULL, NULL);
3133 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3134 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3135 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3138 /* Tree/list view */
3140 impl->browse_files_tree_view = gtk_tree_view_new ();
3141 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "GtkFileChooserDefault", impl);
3142 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
3144 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
3145 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
3146 g_signal_connect (impl->browse_files_tree_view, "row-activated",
3147 G_CALLBACK (list_row_activated), impl);
3148 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
3149 G_CALLBACK (trap_activate_cb), impl);
3150 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
3151 G_CALLBACK (list_popup_menu_cb), impl);
3152 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
3153 G_CALLBACK (list_button_press_event_cb), impl);
3155 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3156 gtk_tree_selection_set_select_function (selection,
3159 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
3161 file_list_source_targets,
3162 num_file_list_source_targets,
3165 g_signal_connect (selection, "changed",
3166 G_CALLBACK (list_selection_changed), impl);
3168 /* Filename column */
3170 impl->list_name_column = gtk_tree_view_column_new ();
3171 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
3172 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
3173 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
3174 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
3176 renderer = gtk_cell_renderer_pixbuf_new ();
3177 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
3178 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
3179 list_icon_data_func, impl, NULL);
3181 impl->list_name_renderer = gtk_cell_renderer_text_new ();
3182 g_object_set (impl->list_name_renderer,
3183 "ellipsize", PANGO_ELLIPSIZE_END,
3185 g_signal_connect (impl->list_name_renderer, "edited",
3186 G_CALLBACK (renderer_edited_cb), impl);
3187 g_signal_connect (impl->list_name_renderer, "editing-canceled",
3188 G_CALLBACK (renderer_editing_canceled_cb), impl);
3189 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
3190 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
3191 list_name_data_func, impl, NULL);
3193 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
3197 column = gtk_tree_view_column_new ();
3198 gtk_tree_view_column_set_title (column, _("Size"));
3200 renderer = gtk_cell_renderer_text_new ();
3201 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3202 gtk_tree_view_column_set_cell_data_func (column, renderer,
3203 list_size_data_func, impl, NULL);
3204 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
3205 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3207 /* Modification time column */
3209 column = gtk_tree_view_column_new ();
3210 gtk_tree_view_column_set_resizable (column, TRUE);
3211 gtk_tree_view_column_set_title (column, _("Modified"));
3213 renderer = gtk_cell_renderer_text_new ();
3214 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3215 gtk_tree_view_column_set_cell_data_func (column, renderer,
3216 list_mtime_data_func, impl, NULL);
3217 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
3218 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3219 gtk_widget_show_all (swin);
3225 create_path_bar (GtkFileChooserDefault *impl)
3227 GtkWidget *path_bar;
3229 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
3230 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
3236 set_filter_tooltip (GtkWidget *widget,
3239 GtkTooltips *tooltips = (GtkTooltips *)data;
3241 if (GTK_IS_BUTTON (widget))
3242 gtk_tooltips_set_tip (tooltips, widget,
3243 _("Select which types of files are shown"),
3248 realize_filter_combo (GtkWidget *combo,
3251 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
3253 gtk_container_forall (GTK_CONTAINER (combo),
3258 /* Creates the widgets for the files/folders pane */
3260 file_pane_create (GtkFileChooserDefault *impl,
3261 GtkSizeGroup *size_group)
3267 vbox = gtk_vbox_new (FALSE, 6);
3268 gtk_widget_show (vbox);
3270 /* The path bar and 'Create Folder' button */
3271 hbox = gtk_hbox_new (FALSE, 12);
3272 gtk_widget_show (hbox);
3273 impl->browse_path_bar = create_path_bar (impl);
3274 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
3275 gtk_widget_show_all (impl->browse_path_bar);
3276 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
3279 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
3280 g_signal_connect (impl->browse_new_folder_button, "clicked",
3281 G_CALLBACK (new_folder_button_clicked), impl);
3282 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
3283 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3285 /* Box for lists and preview */
3287 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
3288 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
3289 gtk_widget_show (hbox);
3293 widget = create_file_list (impl);
3294 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
3298 impl->preview_box = gtk_vbox_new (FALSE, 12);
3299 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
3300 /* Don't show preview box initially */
3304 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
3306 widget = filter_create (impl);
3308 g_signal_connect (widget, "realize",
3309 G_CALLBACK (realize_filter_combo), impl);
3311 gtk_widget_show (widget);
3312 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
3314 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
3315 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
3319 /* Callback used when the "Browse for more folders" expander is toggled */
3321 expander_changed_cb (GtkExpander *expander,
3323 GtkFileChooserDefault *impl)
3325 update_appearance (impl);
3328 /* Callback used when the selection changes in the save folder combo box */
3330 save_folder_combo_changed_cb (GtkComboBox *combo,
3331 GtkFileChooserDefault *impl)
3335 if (impl->changing_folder)
3338 if (gtk_combo_box_get_active_iter (combo, &iter))
3339 shortcuts_activate_iter (impl, &iter);
3342 /* Creates the combo box with the save folders */
3344 save_folder_combo_create (GtkFileChooserDefault *impl)
3347 GtkCellRenderer *cell;
3349 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
3350 gtk_widget_show (combo);
3352 cell = gtk_cell_renderer_pixbuf_new ();
3353 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3354 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3355 "pixbuf", SHORTCUTS_COL_PIXBUF,
3356 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3357 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3360 cell = gtk_cell_renderer_text_new ();
3361 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3362 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3363 "text", SHORTCUTS_COL_NAME,
3364 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3367 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
3368 shortcuts_row_separator_func,
3369 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3372 g_signal_connect (combo, "changed",
3373 G_CALLBACK (save_folder_combo_changed_cb), impl);
3378 /* Creates the widgets specific to Save mode */
3380 save_widgets_create (GtkFileChooserDefault *impl)
3385 GtkWidget *alignment;
3387 vbox = gtk_vbox_new (FALSE, 12);
3389 table = gtk_table_new (2, 2, FALSE);
3390 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3391 gtk_widget_show (table);
3392 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3393 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3397 widget = gtk_label_new_with_mnemonic (_("_Name:"));
3398 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3399 gtk_table_attach (GTK_TABLE (table), widget,
3403 gtk_widget_show (widget);
3405 impl->save_file_name_entry = _gtk_file_chooser_entry_new (TRUE);
3406 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3408 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3409 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3410 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3412 GTK_EXPAND | GTK_FILL, 0,
3414 gtk_widget_show (impl->save_file_name_entry);
3415 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
3418 impl->save_folder_label = gtk_label_new (NULL);
3419 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
3420 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
3424 gtk_widget_show (impl->save_folder_label);
3426 impl->save_folder_combo = save_folder_combo_create (impl);
3427 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
3429 GTK_EXPAND | GTK_FILL, GTK_FILL,
3431 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
3434 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3435 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
3437 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
3438 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
3439 g_signal_connect (impl->save_expander, "notify::expanded",
3440 G_CALLBACK (expander_changed_cb),
3442 gtk_widget_show_all (alignment);
3447 /* Creates the main hpaned with the widgets shared by Open and Save mode */
3449 browse_widgets_create (GtkFileChooserDefault *impl)
3454 GtkSizeGroup *size_group;
3456 /* size group is used by the [+][-] buttons and the filter combo */
3457 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
3458 vbox = gtk_vbox_new (FALSE, 12);
3461 hpaned = gtk_hpaned_new ();
3462 gtk_widget_show (hpaned);
3463 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
3464 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
3466 widget = shortcuts_pane_create (impl, size_group);
3467 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
3468 widget = file_pane_create (impl, size_group);
3469 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
3471 g_object_unref (size_group);
3477 gtk_file_chooser_default_constructor (GType type,
3478 guint n_construct_properties,
3479 GObjectConstructParam *construct_params)
3481 GtkFileChooserDefault *impl;
3484 object = parent_class->constructor (type,
3485 n_construct_properties,
3487 impl = GTK_FILE_CHOOSER_DEFAULT (object);
3489 g_assert (impl->file_system);
3491 gtk_widget_push_composite_child ();
3493 /* Shortcuts model */
3495 shortcuts_model_create (impl);
3497 /* Widgets for Save mode */
3498 impl->save_widgets = save_widgets_create (impl);
3499 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
3501 /* The browse widgets */
3502 impl->browse_widgets = browse_widgets_create (impl);
3503 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
3505 /* Alignment to hold extra widget */
3506 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3507 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
3509 gtk_widget_pop_composite_child ();
3510 update_appearance (impl);
3515 /* Sets the extra_widget by packing it in the appropriate place */
3517 set_extra_widget (GtkFileChooserDefault *impl,
3518 GtkWidget *extra_widget)
3522 g_object_ref (extra_widget);
3523 /* FIXME: is this right ? */
3524 gtk_widget_show (extra_widget);
3527 if (impl->extra_widget)
3529 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3530 g_object_unref (impl->extra_widget);
3533 impl->extra_widget = extra_widget;
3534 if (impl->extra_widget)
3536 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
3537 gtk_widget_show (impl->extra_align);
3540 gtk_widget_hide (impl->extra_align);
3544 set_local_only (GtkFileChooserDefault *impl,
3545 gboolean local_only)
3547 if (local_only != impl->local_only)
3549 impl->local_only = local_only;
3551 if (impl->shortcuts_model && impl->file_system)
3553 shortcuts_add_volumes (impl);
3554 shortcuts_add_bookmarks (impl);
3558 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
3560 /* If we are pointing to a non-local folder, make an effort to change
3561 * back to a local folder, but it's really up to the app to not cause
3562 * such a situation, so we ignore errors.
3564 const gchar *home = g_get_home_dir ();
3565 GtkFilePath *home_path;
3570 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
3572 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
3574 gtk_file_path_free (home_path);
3580 volumes_changed_cb (GtkFileSystem *file_system,
3581 GtkFileChooserDefault *impl)
3583 shortcuts_add_volumes (impl);
3586 /* Callback used when the set of bookmarks changes in the file system */
3588 bookmarks_changed_cb (GtkFileSystem *file_system,
3589 GtkFileChooserDefault *impl)
3591 shortcuts_add_bookmarks (impl);
3593 bookmarks_check_add_sensitivity (impl);
3594 bookmarks_check_remove_sensitivity (impl);
3597 /* Sets the file chooser to multiple selection mode */
3599 set_select_multiple (GtkFileChooserDefault *impl,
3600 gboolean select_multiple,
3601 gboolean property_notify)
3603 GtkTreeSelection *selection;
3604 GtkSelectionMode mode;
3606 if (select_multiple == impl->select_multiple)
3609 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3611 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3612 gtk_tree_selection_set_mode (selection, mode);
3614 impl->select_multiple = select_multiple;
3615 g_object_notify (G_OBJECT (impl), "select-multiple");
3617 check_preview_change (impl);
3621 set_file_system_backend (GtkFileChooserDefault *impl,
3622 const char *backend)
3624 if (impl->file_system)
3626 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3627 impl->volumes_changed_id = 0;
3628 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3629 impl->bookmarks_changed_id = 0;
3630 g_object_unref (impl->file_system);
3633 impl->file_system = NULL;
3635 impl->file_system = _gtk_file_system_create (backend);
3638 GtkSettings *settings = gtk_settings_get_default ();
3639 gchar *default_backend = NULL;
3641 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3642 if (default_backend)
3644 impl->file_system = _gtk_file_system_create (default_backend);
3645 g_free (default_backend);
3649 if (!impl->file_system)
3651 #if defined (G_OS_UNIX)
3652 impl->file_system = gtk_file_system_unix_new ();
3653 #elif defined (G_OS_WIN32)
3654 impl->file_system = gtk_file_system_win32_new ();
3656 #error "No default filesystem implementation on the platform"
3660 if (impl->file_system)
3662 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3663 G_CALLBACK (volumes_changed_cb),
3665 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3666 G_CALLBACK (bookmarks_changed_cb),
3671 /* This function is basically a do_all function.
3673 * It sets the visibility on all the widgets based on the current state, and
3674 * moves the custom_widget if needed.
3677 update_appearance (GtkFileChooserDefault *impl)
3679 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3680 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3684 gtk_widget_show (impl->save_widgets);
3686 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3687 text = _("Save in _folder:");
3689 text = _("Create in _folder:");
3691 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
3693 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3695 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
3696 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
3697 gtk_widget_show (impl->browse_widgets);
3701 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
3702 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
3703 gtk_widget_hide (impl->browse_widgets);
3706 gtk_widget_show (impl->browse_new_folder_button);
3708 if (impl->select_multiple)
3710 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
3711 "Re-setting to single selection mode.");
3712 set_select_multiple (impl, FALSE, TRUE);
3715 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3716 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
3718 gtk_widget_hide (impl->save_widgets);
3719 gtk_widget_show (impl->browse_widgets);
3722 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
3723 gtk_widget_hide (impl->browse_new_folder_button);
3725 gtk_widget_show (impl->browse_new_folder_button);
3727 gtk_widget_queue_draw (impl->browse_files_tree_view);
3729 g_signal_emit_by_name (impl, "default-size-changed");
3733 gtk_file_chooser_default_set_property (GObject *object,
3735 const GValue *value,
3739 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3743 case GTK_FILE_CHOOSER_PROP_ACTION:
3745 GtkFileChooserAction action = g_value_get_enum (value);
3747 if (action != impl->action)
3749 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
3751 if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
3753 g_warning ("Multiple selection mode is not allowed in Save mode");
3754 set_select_multiple (impl, FALSE, TRUE);
3756 impl->action = action;
3757 update_appearance (impl);
3760 if (impl->save_file_name_entry)
3761 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3765 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
3766 set_file_system_backend (impl, g_value_get_string (value));
3768 case GTK_FILE_CHOOSER_PROP_FILTER:
3769 set_current_filter (impl, g_value_get_object (value));
3771 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3772 set_local_only (impl, g_value_get_boolean (value));
3774 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3775 set_preview_widget (impl, g_value_get_object (value));
3777 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3778 impl->preview_widget_active = g_value_get_boolean (value);
3779 update_preview_widget_visibility (impl);
3781 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3782 impl->use_preview_label = g_value_get_boolean (value);
3783 update_preview_widget_visibility (impl);
3785 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3786 set_extra_widget (impl, g_value_get_object (value));
3788 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3790 gboolean select_multiple = g_value_get_boolean (value);
3791 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
3793 g_warning ("Multiple selection mode is not allowed in Save mode");
3797 set_select_multiple (impl, select_multiple, FALSE);
3800 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3802 gboolean show_hidden = g_value_get_boolean (value);
3803 if (show_hidden != impl->show_hidden)
3805 impl->show_hidden = show_hidden;
3807 if (impl->browse_files_model)
3808 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
3813 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3819 gtk_file_chooser_default_get_property (GObject *object,
3824 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3828 case GTK_FILE_CHOOSER_PROP_ACTION:
3829 g_value_set_enum (value, impl->action);
3831 case GTK_FILE_CHOOSER_PROP_FILTER:
3832 g_value_set_object (value, impl->current_filter);
3834 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3835 g_value_set_boolean (value, impl->local_only);
3837 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3838 g_value_set_object (value, impl->preview_widget);
3840 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3841 g_value_set_boolean (value, impl->preview_widget_active);
3843 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3844 g_value_set_boolean (value, impl->use_preview_label);
3846 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3847 g_value_set_object (value, impl->extra_widget);
3849 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3850 g_value_set_boolean (value, impl->select_multiple);
3852 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3853 g_value_set_boolean (value, impl->show_hidden);
3856 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3861 /* Removes the settings signal handler. It's safe to call multiple times */
3863 remove_settings_signal (GtkFileChooserDefault *impl,
3866 if (impl->settings_signal_id)
3868 GtkSettings *settings;
3870 settings = gtk_settings_get_for_screen (screen);
3871 g_signal_handler_disconnect (settings,
3872 impl->settings_signal_id);
3873 impl->settings_signal_id = 0;
3878 gtk_file_chooser_default_dispose (GObject *object)
3880 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
3882 if (impl->extra_widget)
3884 g_object_unref (impl->extra_widget);
3885 impl->extra_widget = NULL;
3888 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
3890 G_OBJECT_CLASS (parent_class)->dispose (object);
3893 /* We override show-all since we have internal widgets that
3894 * shouldn't be shown when you call show_all(), like the filter
3898 gtk_file_chooser_default_show_all (GtkWidget *widget)
3900 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
3902 gtk_widget_show (widget);
3904 if (impl->extra_widget)
3905 gtk_widget_show_all (impl->extra_widget);
3908 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
3909 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
3912 toplevel_set_focus_cb (GtkWindow *window,
3914 GtkFileChooserDefault *impl)
3916 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
3919 /* We monitor the focus widget on our toplevel to be able to know which widget
3920 * was last focused at the time our "should_respond" method gets called.
3923 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
3924 GtkWidget *previous_toplevel)
3926 GtkFileChooserDefault *impl;
3927 GtkWidget *toplevel;
3929 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3931 if (previous_toplevel)
3933 g_assert (impl->toplevel_set_focus_id != 0);
3934 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
3935 impl->toplevel_set_focus_id = 0;
3936 impl->toplevel_last_focus_widget = NULL;
3939 g_assert (impl->toplevel_set_focus_id == 0);
3941 toplevel = gtk_widget_get_toplevel (widget);
3942 if (GTK_IS_WINDOW (toplevel))
3944 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
3945 G_CALLBACK (toplevel_set_focus_cb), impl);
3946 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
3950 /* Changes the icons wherever it is needed */
3952 change_icon_theme (GtkFileChooserDefault *impl)
3954 GtkSettings *settings;
3957 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3959 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_SMALL_TOOLBAR, &width, &height))
3960 impl->icon_size = MAX (width, height);
3962 impl->icon_size = FALLBACK_ICON_SIZE;
3964 shortcuts_reload_icons (impl);
3965 gtk_widget_queue_resize (impl->browse_files_tree_view);
3968 /* Callback used when a GtkSettings value changes */
3970 settings_notify_cb (GObject *object,
3972 GtkFileChooserDefault *impl)
3976 name = g_param_spec_get_name (pspec);
3978 if (strcmp (name, "gtk-icon-theme-name") == 0
3979 || strcmp (name, "gtk-icon-sizes") == 0)
3980 change_icon_theme (impl);
3983 /* Installs a signal handler for GtkSettings so that we can monitor changes in
3987 check_icon_theme (GtkFileChooserDefault *impl)
3989 GtkSettings *settings;
3991 if (impl->settings_signal_id)
3994 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
3996 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3997 impl->settings_signal_id = g_signal_connect (settings, "notify",
3998 G_CALLBACK (settings_notify_cb), impl);
4000 change_icon_theme (impl);
4005 gtk_file_chooser_default_style_set (GtkWidget *widget,
4006 GtkStyle *previous_style)
4008 GtkFileChooserDefault *impl;
4010 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4012 if (GTK_WIDGET_CLASS (parent_class)->style_set)
4013 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
4015 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4016 change_icon_theme (impl);
4018 g_signal_emit_by_name (widget, "default-size-changed");
4022 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
4023 GdkScreen *previous_screen)
4025 GtkFileChooserDefault *impl;
4027 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4029 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
4030 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
4032 remove_settings_signal (impl, previous_screen);
4033 check_icon_theme (impl);
4035 g_signal_emit_by_name (widget, "default-size-changed");
4039 get_is_file_filtered (GtkFileChooserDefault *impl,
4041 GtkFileInfo *file_info)
4043 GtkFileFilterInfo filter_info;
4044 GtkFileFilterFlags needed;
4047 if (!impl->current_filter)
4050 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
4052 needed = gtk_file_filter_get_needed (impl->current_filter);
4054 filter_info.display_name = gtk_file_info_get_display_name (file_info);
4055 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
4057 if (needed & GTK_FILE_FILTER_FILENAME)
4059 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
4060 if (filter_info.filename)
4061 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
4064 filter_info.filename = NULL;
4066 if (needed & GTK_FILE_FILTER_URI)
4068 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
4069 if (filter_info.uri)
4070 filter_info.contains |= GTK_FILE_FILTER_URI;
4073 filter_info.uri = NULL;
4075 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
4077 if (filter_info.filename)
4078 g_free ((gchar *)filter_info.filename);
4079 if (filter_info.uri)
4080 g_free ((gchar *)filter_info.uri);
4085 /* GtkWidget::map method */
4087 gtk_file_chooser_default_map (GtkWidget *widget)
4089 GtkFileChooserDefault *impl;
4091 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4093 GTK_WIDGET_CLASS (parent_class)->map (widget);
4095 change_folder_and_display_error (impl, impl->current_folder);
4096 bookmarks_changed_cb (impl->file_system, impl);
4100 list_model_filter_func (GtkFileSystemModel *model,
4102 const GtkFileInfo *file_info,
4105 GtkFileChooserDefault *impl = user_data;
4107 if (!impl->current_filter)
4110 if (gtk_file_info_get_is_folder (file_info))
4113 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
4117 install_list_model_filter (GtkFileChooserDefault *impl)
4119 GtkFileSystemModelFilter filter;
4122 g_assert (impl->browse_files_model != NULL);
4124 if (impl->current_filter)
4126 filter = list_model_filter_func;
4135 _gtk_file_system_model_set_filter (impl->browse_files_model,
4140 #define COMPARE_DIRECTORIES \
4141 GtkFileChooserDefault *impl = user_data; \
4142 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
4143 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
4144 gboolean dir_a, dir_b; \
4147 dir_a = gtk_file_info_get_is_folder (info_a); \
4149 return impl->list_sort_ascending ? -1 : 1; \
4152 dir_b = gtk_file_info_get_is_folder (info_b); \
4154 return impl->list_sort_ascending ? 1 : -1; \
4156 if (dir_a != dir_b) \
4157 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
4159 /* Sort callback for the filename column */
4161 name_sort_func (GtkTreeModel *model,
4166 COMPARE_DIRECTORIES;
4168 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
4171 /* Sort callback for the size column */
4173 size_sort_func (GtkTreeModel *model,
4178 COMPARE_DIRECTORIES;
4181 gint64 size_a = gtk_file_info_get_size (info_a);
4182 gint64 size_b = gtk_file_info_get_size (info_b);
4184 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
4188 /* Sort callback for the mtime column */
4190 mtime_sort_func (GtkTreeModel *model,
4195 COMPARE_DIRECTORIES;
4198 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
4199 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
4201 return ta > tb ? -1 : (ta == tb ? 0 : 1);
4205 /* Callback used when the sort column changes. We cache the sort order for use
4206 * in name_sort_func().
4209 list_sort_column_changed_cb (GtkTreeSortable *sortable,
4210 GtkFileChooserDefault *impl)
4212 GtkSortType sort_type;
4214 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
4215 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
4219 set_busy_cursor (GtkFileChooserDefault *impl,
4222 GtkWindow *toplevel;
4223 GdkDisplay *display;
4226 toplevel = get_toplevel (GTK_WIDGET (impl));
4227 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
4230 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
4233 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
4237 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
4238 gdk_display_flush (display);
4241 gdk_cursor_unref (cursor);
4244 /* Creates a sort model to wrap the file system model and sets it on the tree view */
4246 load_set_model (GtkFileChooserDefault *impl)
4248 g_assert (impl->browse_files_model != NULL);
4249 g_assert (impl->sort_model == NULL);
4251 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4252 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4253 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4254 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4255 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4256 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4257 impl->list_sort_ascending = TRUE;
4259 g_signal_connect (impl->sort_model, "sort-column-changed",
4260 G_CALLBACK (list_sort_column_changed_cb), impl);
4262 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4263 GTK_TREE_MODEL (impl->sort_model));
4264 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4265 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4266 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4269 /* Timeout callback used when the loading timer expires */
4271 load_timeout_cb (gpointer data)
4273 GtkFileChooserDefault *impl;
4275 GDK_THREADS_ENTER ();
4277 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4278 g_assert (impl->load_state == LOAD_LOADING);
4279 g_assert (impl->load_timeout_id != 0);
4280 g_assert (impl->browse_files_model != NULL);
4282 impl->load_timeout_id = 0;
4283 impl->load_state = LOAD_FINISHED;
4285 load_set_model (impl);
4287 GDK_THREADS_LEAVE ();
4292 /* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
4294 load_setup_timer (GtkFileChooserDefault *impl)
4296 g_assert (impl->load_timeout_id == 0);
4297 g_assert (impl->load_state == LOAD_FINISHED);
4299 impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
4300 impl->load_state = LOAD_LOADING;
4303 /* Removes the load timeout and switches to the LOAD_FINISHED state */
4305 load_remove_timer (GtkFileChooserDefault *impl)
4307 if (impl->load_timeout_id != 0)
4309 g_assert (impl->load_state == LOAD_LOADING);
4311 g_source_remove (impl->load_timeout_id);
4312 impl->load_timeout_id = 0;
4313 impl->load_state = LOAD_FINISHED;
4316 g_assert (impl->load_state == LOAD_FINISHED);
4319 /* Queues a pending operation relative to selecting a file when the current
4320 * folder finishes loading.
4323 pending_op_queue (GtkFileChooserDefault *impl, PendingOp op, const GtkFilePath *path)
4325 if (impl->pending_op == PENDING_OP_SELECT_PATH)
4327 g_assert (impl->pending_select_path != NULL);
4328 gtk_file_path_free (impl->pending_select_path);
4330 impl->pending_select_path = NULL;
4333 impl->pending_op = op;
4335 if (impl->pending_op == PENDING_OP_SELECT_PATH)
4337 g_assert (path != NULL);
4338 impl->pending_select_path = gtk_file_path_copy (path);
4342 /* Selects the first row in the file list */
4344 browse_files_select_first_row (GtkFileChooserDefault *impl)
4348 path = gtk_tree_path_new_from_indices (0, -1);
4349 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4350 gtk_tree_path_free (path);
4353 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
4354 * selected row in the tree view.
4357 center_selected_row_foreach_cb (GtkTreeModel *model,
4362 GtkFileChooserDefault *impl;
4364 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4365 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
4368 /* Centers the selected row in the tree view */
4370 browse_files_center_selected_row (GtkFileChooserDefault *impl)
4372 GtkTreeSelection *selection;
4374 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4375 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, impl);
4378 /* Processes the pending operation when a folder is finished loading */
4380 pending_op_process (GtkFileChooserDefault *impl)
4382 switch (impl->pending_op)
4384 case PENDING_OP_NONE:
4387 case PENDING_OP_SELECT_PATH:
4388 g_assert (impl->pending_select_path != NULL);
4390 _gtk_file_system_model_path_do (impl->browse_files_model, impl->pending_select_path, select_func, impl);
4391 gtk_file_path_free (impl->pending_select_path);
4392 impl->pending_select_path = NULL;
4393 impl->pending_op = PENDING_OP_NONE;
4394 browse_files_center_selected_row (impl);
4397 case PENDING_OP_SELECT_FIRST:
4398 browse_files_select_first_row (impl);
4399 impl->pending_op = PENDING_OP_NONE;
4403 g_assert_not_reached ();
4406 g_assert (impl->pending_op == PENDING_OP_NONE);
4407 g_assert (impl->pending_select_path == NULL);
4410 /* Callback used when the file system model finishes loading */
4412 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
4413 GtkFileChooserDefault *impl)
4415 if (impl->load_state == LOAD_LOADING)
4417 load_remove_timer (impl);
4418 load_set_model (impl);
4421 g_assert (impl->load_state == LOAD_FINISHED);
4423 pending_op_process (impl);
4424 set_busy_cursor (impl, FALSE);
4427 /* Gets rid of the old list model and creates a new one for the current folder */
4429 set_list_model (GtkFileChooserDefault *impl,
4432 load_remove_timer (impl);
4434 if (impl->browse_files_model)
4436 g_object_unref (impl->browse_files_model);
4437 impl->browse_files_model = NULL;
4440 if (impl->sort_model)
4442 g_object_unref (impl->sort_model);
4443 impl->sort_model = NULL;
4446 set_busy_cursor (impl, TRUE);
4447 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
4449 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
4450 impl->current_folder, 0,
4453 if (!impl->browse_files_model)
4455 set_busy_cursor (impl, FALSE);
4459 load_setup_timer (impl);
4461 g_signal_connect (impl->browse_files_model, "finished-loading",
4462 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
4464 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
4466 install_list_model_filter (impl);
4472 update_chooser_entry (GtkFileChooserDefault *impl)
4474 GtkTreeSelection *selection;
4475 const GtkFileInfo *info;
4477 GtkTreeIter child_iter;
4479 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
4482 g_assert (!impl->select_multiple);
4483 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4485 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4488 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4492 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4494 if (!gtk_file_info_get_is_folder (info))
4495 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4496 gtk_file_info_get_display_name (info));
4500 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
4501 const GtkFilePath *path,
4504 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4507 if (impl->local_only &&
4508 !gtk_file_system_path_is_local (impl->file_system, path))
4511 GTK_FILE_CHOOSER_ERROR,
4512 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
4513 _("Cannot change to folder because it is not local"));
4518 /* Test validity of path here. */
4519 if (!check_is_folder (impl->file_system, path, error))
4522 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
4525 if (impl->current_folder != path)
4527 if (impl->current_folder)
4528 gtk_file_path_free (impl->current_folder);
4530 impl->current_folder = gtk_file_path_copy (path);
4533 /* Update the widgets that may trigger a folder change themselves. */
4535 if (!impl->changing_folder)
4537 impl->changing_folder = TRUE;
4539 shortcuts_update_current_folder (impl);
4541 impl->changing_folder = FALSE;
4544 /* Set the folder on the save entry */
4546 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4547 impl->current_folder);
4549 /* Create a new list model. This is slightly evil; we store the result value
4550 * but perform more actions rather than returning immediately even if it
4551 * generates an error.
4553 result = set_list_model (impl, error);
4555 /* Refresh controls */
4557 shortcuts_find_current_folder (impl);
4559 g_signal_emit_by_name (impl, "current-folder-changed", 0);
4561 check_preview_change (impl);
4562 bookmarks_check_add_sensitivity (impl);
4564 g_signal_emit_by_name (impl, "selection-changed", 0);
4569 static GtkFilePath *
4570 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
4572 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4574 return gtk_file_path_copy (impl->current_folder);
4578 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
4581 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4583 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4584 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4586 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), name);
4590 select_func (GtkFileSystemModel *model,
4595 GtkFileChooserDefault *impl = user_data;
4596 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4597 GtkTreePath *sorted_path;
4599 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
4600 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
4601 gtk_tree_view_scroll_to_cell (tree_view, sorted_path, NULL, FALSE, 0.0, 0.0);
4602 gtk_tree_path_free (sorted_path);
4606 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
4607 const GtkFilePath *path,
4610 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4611 GtkFilePath *parent_path;
4613 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
4617 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
4621 GtkFileFolder *folder;
4623 gboolean is_hidden, is_filtered;
4625 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
4629 gtk_file_path_free (parent_path);
4633 folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
4634 gtk_file_path_free (parent_path);
4639 info = gtk_file_folder_get_info (folder, path, error);
4640 g_object_unref (folder);
4645 is_hidden = gtk_file_info_get_is_hidden (info);
4646 is_filtered = get_is_file_filtered (impl, (GtkFilePath *) path, info);
4647 gtk_file_info_free (info);
4650 g_object_set (impl, "show-hidden", TRUE, NULL);
4653 set_current_filter (impl, NULL);
4655 pending_op_queue (impl, PENDING_OP_SELECT_PATH, path);
4659 g_assert_not_reached ();
4663 unselect_func (GtkFileSystemModel *model,
4668 GtkFileChooserDefault *impl = user_data;
4669 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4670 GtkTreePath *sorted_path;
4672 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
4674 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
4676 gtk_tree_path_free (sorted_path);
4680 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
4681 const GtkFilePath *path)
4683 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4685 if (!impl->browse_files_model)
4688 _gtk_file_system_model_path_do (impl->browse_files_model, path,
4689 unselect_func, impl);
4693 maybe_select (GtkTreeModel *model,
4698 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
4699 GtkTreeSelection *selection;
4700 const GtkFileInfo *info;
4703 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4705 info = get_list_file_info (impl, iter);
4706 is_folder = gtk_file_info_get_is_folder (info);
4708 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
4709 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
4710 gtk_tree_selection_select_iter (selection, iter);
4712 gtk_tree_selection_unselect_iter (selection, iter);
4718 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
4720 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4721 if (impl->select_multiple)
4722 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
4723 maybe_select, impl);
4727 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
4729 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4730 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4732 gtk_tree_selection_unselect_all (selection);
4735 /* Checks whether the filename entry for the Save modes contains a valid filename */
4736 static GtkFilePath *
4737 check_save_entry (GtkFileChooserDefault *impl,
4741 GtkFileChooserEntry *chooser_entry;
4742 const GtkFilePath *current_folder;
4743 const char *file_part;
4747 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4748 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4750 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
4752 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
4753 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
4755 if (!file_part || file_part[0] == '\0')
4765 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
4769 error_building_filename_dialog (impl, current_folder, file_part, error);
4778 struct get_paths_closure {
4779 GtkFileChooserDefault *impl;
4781 GtkFilePath *path_from_entry;
4785 get_paths_foreach (GtkTreeModel *model,
4790 struct get_paths_closure *info;
4791 const GtkFilePath *file_path;
4792 GtkFileSystemModel *fs_model;
4793 GtkTreeIter sel_iter;
4796 fs_model = info->impl->browse_files_model;
4797 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
4799 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
4801 return; /* We are on the editable row */
4803 if (!info->path_from_entry
4804 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
4805 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
4809 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
4811 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4812 struct get_paths_closure info;
4816 info.path_from_entry = NULL;
4818 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4819 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4821 gboolean is_valid, is_empty;
4823 info.path_from_entry = check_save_entry (impl, &is_valid, &is_empty);
4824 if (!is_valid && !is_empty)
4828 if (!info.path_from_entry || impl->select_multiple)
4830 GtkTreeSelection *selection;
4832 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4833 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
4836 if (info.path_from_entry)
4837 info.result = g_slist_prepend (info.result, info.path_from_entry);
4839 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
4840 * fall back to the current directory */
4841 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
4842 info.result == NULL)
4844 info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
4847 return g_slist_reverse (info.result);
4850 static GtkFilePath *
4851 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
4853 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4855 if (impl->preview_path)
4856 return gtk_file_path_copy (impl->preview_path);
4861 static GtkFileSystem *
4862 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
4864 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4866 return impl->file_system;
4869 /* Shows or hides the filter widgets */
4871 show_filters (GtkFileChooserDefault *impl,
4875 gtk_widget_show (impl->filter_combo_hbox);
4877 gtk_widget_hide (impl->filter_combo_hbox);
4881 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
4882 GtkFileFilter *filter)
4884 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4887 if (g_slist_find (impl->filters, filter))
4889 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
4893 g_object_ref (filter);
4894 gtk_object_sink (GTK_OBJECT (filter));
4895 impl->filters = g_slist_append (impl->filters, filter);
4897 name = gtk_file_filter_get_name (filter);
4899 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
4901 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
4903 if (!g_slist_find (impl->filters, impl->current_filter))
4904 set_current_filter (impl, filter);
4906 show_filters (impl, TRUE);
4910 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
4911 GtkFileFilter *filter)
4913 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4914 GtkTreeModel *model;
4918 filter_index = g_slist_index (impl->filters, filter);
4920 if (filter_index < 0)
4922 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
4926 impl->filters = g_slist_remove (impl->filters, filter);
4928 if (filter == impl->current_filter)
4931 set_current_filter (impl, impl->filters->data);
4933 set_current_filter (impl, NULL);
4936 /* Remove row from the combo box */
4937 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
4938 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
4939 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
4941 g_object_unref (filter);
4944 show_filters (impl, FALSE);
4948 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
4950 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4952 return g_slist_copy (impl->filters);
4955 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
4957 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
4960 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
4964 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
4965 const GtkFilePath *path,
4968 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4972 /* Test validity of path here. */
4973 if (!check_is_folder (impl->file_system, path, error))
4976 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
4978 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
4981 impl->num_shortcuts++;
4983 if (impl->shortcuts_filter_model)
4984 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
4990 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
4991 const GtkFilePath *path,
4994 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5000 if (impl->num_shortcuts == 0)
5003 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5004 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5005 g_assert_not_reached ();
5007 for (i = 0; i < impl->num_shortcuts; i++)
5011 GtkFilePath *shortcut;
5013 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5014 SHORTCUTS_COL_DATA, &col_data,
5015 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5017 g_assert (col_data != NULL);
5018 g_assert (!is_volume);
5020 shortcut = col_data;
5021 if (gtk_file_path_compare (shortcut, path) == 0)
5023 shortcuts_remove_rows (impl, pos + i, 1);
5024 impl->num_shortcuts--;
5028 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5029 g_assert_not_reached ();
5034 uri = gtk_file_system_path_to_uri (impl->file_system, path);
5036 GTK_FILE_CHOOSER_ERROR,
5037 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
5038 _("Shortcut %s does not exist"),
5046 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
5048 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5054 if (impl->num_shortcuts == 0)
5057 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5058 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5059 g_assert_not_reached ();
5063 for (i = 0; i < impl->num_shortcuts; i++)
5067 GtkFilePath *shortcut;
5069 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5070 SHORTCUTS_COL_DATA, &col_data,
5071 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5073 g_assert (col_data != NULL);
5074 g_assert (!is_volume);
5076 shortcut = col_data;
5077 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
5079 if (i != impl->num_shortcuts - 1)
5081 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5082 g_assert_not_reached ();
5086 return g_slist_reverse (list);
5089 /* Guesses a size based upon font sizes */
5091 find_good_size_from_style (GtkWidget *widget,
5095 GtkFileChooserDefault *impl;
5096 gint default_width, default_height;
5099 GtkRequisition preview_req;
5101 g_assert (widget->style != NULL);
5102 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5104 font_size = pango_font_description_get_size (widget->style->font_desc);
5105 font_size = PANGO_PIXELS (font_size);
5107 default_width = font_size * NUM_CHARS;
5108 default_height = font_size * NUM_LINES;
5110 /* Use at least the requisition size not including the preview widget */
5111 gtk_widget_size_request (widget, &req);
5113 if (impl->preview_widget_active && impl->preview_widget)
5114 gtk_widget_size_request (impl->preview_box, &preview_req);
5116 preview_req.width = 0;
5118 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
5119 default_height = MAX (default_height, req.height);
5121 *width = default_width;
5122 *height = default_height;
5126 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
5127 gint *default_width,
5128 gint *default_height)
5130 GtkFileChooserDefault *impl;
5132 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5134 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
5136 if (impl->preview_widget_active && impl->preview_widget)
5137 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
5141 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
5142 gboolean *resize_horizontally,
5143 gboolean *resize_vertically)
5145 GtkFileChooserDefault *impl;
5147 g_return_if_fail (resize_horizontally != NULL);
5148 g_return_if_fail (resize_vertically != NULL);
5150 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5152 *resize_horizontally = TRUE;
5153 *resize_vertically = TRUE;
5155 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5156 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5158 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5160 *resize_horizontally = FALSE;
5161 *resize_vertically = FALSE;
5166 struct switch_folder_closure {
5167 GtkFileChooserDefault *impl;
5168 const GtkFilePath *path;
5172 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
5174 switch_folder_foreach_cb (GtkTreeModel *model,
5179 struct switch_folder_closure *closure;
5180 GtkTreeIter child_iter;
5184 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
5186 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
5187 closure->num_selected++;
5190 /* Changes to the selected folder in the list view */
5192 switch_to_selected_folder (GtkFileChooserDefault *impl)
5194 GtkTreeSelection *selection;
5195 struct switch_folder_closure closure;
5197 /* We do this with foreach() rather than get_selected() as we may be in
5198 * multiple selection mode
5201 closure.impl = impl;
5202 closure.path = NULL;
5203 closure.num_selected = 0;
5205 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5206 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
5208 g_assert (closure.path && closure.num_selected == 1);
5210 change_folder_and_display_error (impl, closure.path);
5213 /* Implementation for GtkFileChooserEmbed::should_respond() */
5215 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
5217 GtkFileChooserDefault *impl;
5218 GtkWidget *toplevel;
5219 GtkWidget *current_focus;
5221 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5223 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
5224 g_assert (GTK_IS_WINDOW (toplevel));
5226 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
5228 if (current_focus == impl->browse_files_tree_view)
5231 gboolean all_files, all_folders;
5235 selection_check (impl, &num_selected, &all_files, &all_folders);
5237 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5239 if (num_selected != 1)
5240 return TRUE; /* zero means current folder; more than one means use the whole selection */
5241 else if (current_focus != impl->browse_files_tree_view)
5243 /* a single folder is selected and a button was clicked */
5244 switch_to_selected_folder (impl);
5249 if (num_selected == 0)
5251 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5252 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5253 goto save_entry; /* it makes sense to use the typed name */
5258 if (num_selected == 1 && all_folders)
5260 switch_to_selected_folder (impl);
5261 pending_op_queue (impl, PENDING_OP_SELECT_FIRST, NULL);
5267 else if (current_focus == impl->save_file_name_entry)
5270 gboolean is_valid, is_empty;
5273 GtkFileChooserEntry *entry;
5277 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5278 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5280 entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
5281 path = check_save_entry (impl, &is_valid, &is_empty);
5283 if (!is_empty && !is_valid)
5287 path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
5289 is_folder = check_is_folder (impl->file_system, path, NULL);
5292 _gtk_file_chooser_entry_set_file_part (entry, "");
5293 change_folder_and_display_error (impl, path);
5299 gtk_file_path_free (path);
5302 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
5304 /* The focus is on a dialog's action area button, *and* the widget that
5305 * was focused immediately before it is the shortcuts list. Switch to the
5306 * selected shortcut and tell the caller not to respond.
5310 if (shortcuts_get_selected (impl, &iter))
5312 shortcuts_activate_iter (impl, &iter);
5314 gtk_widget_grab_focus (impl->browse_files_tree_view);
5321 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
5323 /* The focus is on a dialog's action area button, *and* the widget that
5324 * was focused immediately before it is the file list.
5329 /* The focus is on a dialog's action area button or something else */
5330 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5331 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5336 g_assert_not_reached ();
5340 /* Implementation for GtkFileChooserEmbed::initial_focus() */
5342 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
5344 GtkFileChooserDefault *impl;
5347 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5349 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5350 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5352 if (impl->load_state == LOAD_FINISHED)
5353 browse_files_select_first_row (impl);
5355 pending_op_queue (impl, PENDING_OP_SELECT_FIRST, NULL);
5357 widget = impl->browse_files_tree_view;
5359 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5360 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5361 widget = impl->save_file_name_entry;
5364 g_assert_not_reached ();
5368 gtk_widget_grab_focus (widget);
5372 set_current_filter (GtkFileChooserDefault *impl,
5373 GtkFileFilter *filter)
5375 if (impl->current_filter != filter)
5379 /* NULL filters are allowed to reset to non-filtered status
5381 filter_index = g_slist_index (impl->filters, filter);
5382 if (impl->filters && filter && filter_index < 0)
5385 if (impl->current_filter)
5386 g_object_unref (impl->current_filter);
5387 impl->current_filter = filter;
5388 if (impl->current_filter)
5390 g_object_ref (impl->current_filter);
5391 gtk_object_sink (GTK_OBJECT (filter));
5395 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
5398 if (impl->browse_files_model)
5399 install_list_model_filter (impl);
5401 g_object_notify (G_OBJECT (impl), "filter");
5406 filter_combo_changed (GtkComboBox *combo_box,
5407 GtkFileChooserDefault *impl)
5409 gint new_index = gtk_combo_box_get_active (combo_box);
5410 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
5412 set_current_filter (impl, new_filter);
5416 check_preview_change (GtkFileChooserDefault *impl)
5418 GtkTreePath *cursor_path;
5419 const GtkFilePath *new_path;
5420 const GtkFileInfo *new_info;
5422 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
5423 if (cursor_path && impl->sort_model)
5426 GtkTreeIter child_iter;
5428 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
5429 gtk_tree_path_free (cursor_path);
5431 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5433 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5434 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5442 if (new_path != impl->preview_path &&
5443 !(new_path && impl->preview_path &&
5444 gtk_file_path_compare (new_path, impl->preview_path) == 0))
5446 if (impl->preview_path)
5448 gtk_file_path_free (impl->preview_path);
5449 g_free (impl->preview_display_name);
5454 impl->preview_path = gtk_file_path_copy (new_path);
5455 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
5459 impl->preview_path = NULL;
5460 impl->preview_display_name = NULL;
5463 if (impl->use_preview_label && impl->preview_label)
5464 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
5466 g_signal_emit_by_name (impl, "update-preview");
5470 /* Activates a volume by mounting it if necessary and then switching to its
5474 shortcuts_activate_volume (GtkFileChooserDefault *impl,
5475 GtkFileSystemVolume *volume)
5479 /* We ref the file chooser since volume_mount() may run a main loop, and the
5480 * user could close the file chooser window in the meantime.
5482 g_object_ref (impl);
5484 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
5489 set_busy_cursor (impl, TRUE);
5492 result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
5498 msg = g_strdup_printf (_("Could not mount %s"),
5499 gtk_file_system_volume_get_display_name (impl->file_system, volume));
5500 error_message (impl, msg, error->message);
5502 g_error_free (error);
5505 set_busy_cursor (impl, FALSE);
5511 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
5512 change_folder_and_display_error (impl, path);
5513 gtk_file_path_free (path);
5517 g_object_unref (impl);
5520 /* Opens the folder or volume at the specified iter in the shortcuts model */
5522 shortcuts_activate_iter (GtkFileChooserDefault *impl,
5528 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
5529 SHORTCUTS_COL_DATA, &col_data,
5530 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5534 return; /* We are on a separator */
5538 GtkFileSystemVolume *volume;
5542 shortcuts_activate_volume (impl, volume);
5546 const GtkFilePath *file_path;
5548 file_path = col_data;
5549 change_folder_and_display_error (impl, file_path);
5553 /* Callback used when a row in the shortcuts list is activated */
5555 shortcuts_row_activated_cb (GtkTreeView *tree_view,
5557 GtkTreeViewColumn *column,
5558 GtkFileChooserDefault *impl)
5561 GtkTreeIter child_iter;
5563 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
5566 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
5569 shortcuts_activate_iter (impl, &child_iter);
5571 gtk_widget_grab_focus (impl->browse_files_tree_view);
5574 /* Handler for GtkWidget::key-press-event on the shortcuts list */
5576 shortcuts_key_press_event_cb (GtkWidget *widget,
5578 GtkFileChooserDefault *impl)
5582 modifiers = gtk_accelerator_get_default_mod_mask ();
5584 if ((event->keyval == GDK_BackSpace
5585 || event->keyval == GDK_Delete
5586 || event->keyval == GDK_KP_Delete)
5587 && (event->state & modifiers) == 0)
5589 remove_selected_bookmarks (impl);
5597 shortcuts_select_func (GtkTreeSelection *selection,
5598 GtkTreeModel *model,
5600 gboolean path_currently_selected,
5603 GtkFileChooserDefault *impl = data;
5605 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
5609 list_select_func (GtkTreeSelection *selection,
5610 GtkTreeModel *model,
5612 gboolean path_currently_selected,
5615 GtkFileChooserDefault *impl = data;
5617 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5618 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5620 GtkTreeIter iter, child_iter;
5621 const GtkFileInfo *info;
5623 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5626 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5628 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5630 if (info && !gtk_file_info_get_is_folder (info))
5638 list_selection_changed (GtkTreeSelection *selection,
5639 GtkFileChooserDefault *impl)
5641 /* See if we are in the new folder editable row for Save mode */
5642 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5644 GtkTreeSelection *selection;
5645 GtkTreeIter iter, child_iter;
5646 const GtkFileInfo *info;
5648 g_assert (!impl->select_multiple);
5649 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5650 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
5653 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5657 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5659 return; /* We are on the editable row for New Folder */
5662 update_chooser_entry (impl);
5663 check_preview_change (impl);
5664 bookmarks_check_add_sensitivity (impl);
5666 g_signal_emit_by_name (impl, "selection-changed", 0);
5669 /* Callback used when a row in the file list is activated */
5671 list_row_activated (GtkTreeView *tree_view,
5673 GtkTreeViewColumn *column,
5674 GtkFileChooserDefault *impl)
5676 GtkTreeIter iter, child_iter;
5677 const GtkFileInfo *info;
5679 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5682 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5684 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5686 if (gtk_file_info_get_is_folder (info))
5688 const GtkFilePath *file_path;
5690 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5691 change_folder_and_display_error (impl, file_path);
5696 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
5697 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5698 g_signal_emit_by_name (impl, "file-activated");
5702 path_bar_clicked (GtkPathBar *path_bar,
5703 GtkFilePath *file_path,
5704 gboolean child_is_hidden,
5705 GtkFileChooserDefault *impl)
5707 if (!change_folder_and_display_error (impl, file_path))
5710 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
5711 * show hidden files so that ".baz" appears in the file list, as it will still
5712 * be shown in the path bar: "/foo/[bar]/.baz"
5714 if (child_is_hidden)
5715 g_object_set (impl, "show-hidden", TRUE, NULL);
5718 static const GtkFileInfo *
5719 get_list_file_info (GtkFileChooserDefault *impl,
5722 GtkTreeIter child_iter;
5724 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5728 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5732 list_icon_data_func (GtkTreeViewColumn *tree_column,
5733 GtkCellRenderer *cell,
5734 GtkTreeModel *tree_model,
5738 GtkFileChooserDefault *impl = data;
5739 GtkTreeIter child_iter;
5740 const GtkFilePath *path;
5742 const GtkFileInfo *info;
5743 gboolean sensitive = TRUE;
5745 info = get_list_file_info (impl, iter);
5747 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5750 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5754 /* FIXME: NULL GError */
5755 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
5756 impl->icon_size, NULL);
5760 /* We are on the editable row */
5764 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5765 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
5766 sensitive = gtk_file_info_get_is_folder (info);
5770 "sensitive", sensitive,
5774 g_object_unref (pixbuf);
5778 list_name_data_func (GtkTreeViewColumn *tree_column,
5779 GtkCellRenderer *cell,
5780 GtkTreeModel *tree_model,
5784 GtkFileChooserDefault *impl = data;
5785 const GtkFileInfo *info = get_list_file_info (impl, iter);
5786 gboolean sensitive = TRUE;
5791 "text", _("Type name of new folder"),
5798 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
5799 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5801 sensitive = gtk_file_info_get_is_folder (info);
5805 "text", gtk_file_info_get_display_name (info),
5806 "sensitive", sensitive,
5812 list_size_data_func (GtkTreeViewColumn *tree_column,
5813 GtkCellRenderer *cell,
5814 GtkTreeModel *tree_model,
5818 GtkFileChooserDefault *impl = data;
5819 const GtkFileInfo *info = get_list_file_info (impl, iter);
5822 gboolean sensitive = TRUE;
5824 if (!info || gtk_file_info_get_is_folder (info))
5826 g_object_set (cell,"sensitive", sensitive, NULL);
5830 size = gtk_file_info_get_size (info);
5832 if (size < (gint64)1024)
5833 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
5834 else if (size < (gint64)1024*1024)
5835 str = g_strdup_printf (_("%.1f K"), size / (1024.));
5836 else if (size < (gint64)1024*1024*1024)
5837 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
5839 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
5841 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5842 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5847 "sensitive", sensitive,
5854 /* Tree column data callback for the file list; fetches the mtime of a file */
5856 list_mtime_data_func (GtkTreeViewColumn *tree_column,
5857 GtkCellRenderer *cell,
5858 GtkTreeModel *tree_model,
5862 GtkFileChooserDefault *impl;
5863 const GtkFileInfo *info;
5864 GtkFileTime time_mtime, time_now;
5868 gboolean sensitive = TRUE;
5872 info = get_list_file_info (impl, iter);
5882 time_mtime = gtk_file_info_get_modification_time (info);
5883 g_date_set_time (&mtime, (GTime) time_mtime);
5885 time_now = (GTime ) time (NULL);
5886 g_date_set_time (&now, (GTime) time_now);
5888 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
5891 strcpy (buf, _("Today"));
5892 else if (days_diff == 1)
5893 strcpy (buf, _("Yesterday"));
5898 if (days_diff > 1 && days_diff < 7)
5899 format = "%A"; /* Days from last week */
5901 format = "%x"; /* Any other date */
5903 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
5904 strcpy (buf, _("Unknown"));
5907 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
5908 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5909 sensitive = gtk_file_info_get_is_folder (info);
5913 "sensitive", sensitive,
5918 _gtk_file_chooser_default_new (const char *file_system)
5920 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
5921 "file-system-backend", file_system,
5926 location_entry_create (GtkFileChooserDefault *impl,
5931 entry = _gtk_file_chooser_entry_new (TRUE);
5932 /* Pick a good width for the entry */
5933 gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
5934 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
5935 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
5936 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (entry), impl->action);
5939 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry),
5940 gtk_file_path_new_steal ((gchar *)path));
5941 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), path);
5945 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
5946 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5947 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5948 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), "");
5949 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5950 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5951 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry),
5952 gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
5954 g_assert_not_reached ();
5957 return GTK_WIDGET (entry);
5961 update_from_entry (GtkFileChooserDefault *impl,
5963 GtkFileChooserEntry *chooser_entry)
5965 const GtkFilePath *folder_path;
5966 const char *file_part;
5968 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
5969 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
5971 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
5973 error_message_with_parent (parent,
5974 _("Cannot change folder"),
5975 _("The folder you specified is an invalid path."));
5979 if (file_part[0] == '\0')
5980 return change_folder_and_display_error (impl, folder_path);
5983 GtkFileFolder *folder = NULL;
5984 GtkFilePath *subfolder_path = NULL;
5985 GtkFileInfo *info = NULL;
5991 /* If the file part is non-empty, we need to figure out if it refers to a
5992 * folder within folder. We could optimize the case here where the folder
5993 * is already loaded for one of our tree models.
5997 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
6001 error_getting_info_dialog (impl, folder_path, error);
6006 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
6008 if (!subfolder_path)
6013 uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
6014 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s'"),
6016 error_message (impl, msg, error->message);
6023 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
6027 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6028 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6030 if (!change_folder_and_display_error (impl, folder_path))
6033 gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
6036 error_getting_info_dialog (impl, subfolder_path, error);
6041 if (gtk_file_info_get_is_folder (info))
6042 result = change_folder_and_display_error (impl, subfolder_path);
6048 result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
6050 error_dialog (impl, _("Could not select item"),
6051 subfolder_path, error);
6057 g_object_unref (folder);
6059 gtk_file_path_free (subfolder_path);
6062 gtk_file_info_free (info);
6067 g_assert_not_reached ();
6071 location_popup_handler (GtkFileChooserDefault *impl,
6075 GtkWindow *toplevel;
6081 const char *accept_stock;
6085 toplevel = get_toplevel (GTK_WIDGET (impl));
6087 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6088 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6090 title = _("Open Location");
6091 accept_stock = GTK_STOCK_OPEN;
6095 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6096 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6097 title = _("Save in Location");
6098 accept_stock = GTK_STOCK_SAVE;
6101 dialog = gtk_dialog_new_with_buttons (title,
6103 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
6104 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
6105 accept_stock, GTK_RESPONSE_ACCEPT,
6107 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
6108 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
6109 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
6110 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
6112 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
6113 GTK_RESPONSE_ACCEPT,
6114 GTK_RESPONSE_CANCEL,
6117 hbox = gtk_hbox_new (FALSE, 12);
6118 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
6119 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
6121 label = gtk_label_new_with_mnemonic (_("_Location:"));
6122 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
6124 entry = location_entry_create (impl, path);
6126 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
6127 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
6131 gtk_widget_show_all (dialog);
6132 /* If the dialog is brought up by typing the first characters
6133 * of a path, unselect the text in the entry, so that you can
6134 * just type on without erasing the initial part.
6137 gtk_editable_select_region (GTK_EDITABLE (entry), -1, -1);
6141 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
6143 if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
6145 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6146 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6148 gtk_widget_grab_focus (impl->browse_files_tree_view);
6152 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6153 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6154 gtk_widget_grab_focus (impl->save_file_name_entry);
6162 GtkWindow *toplevel;
6164 toplevel = get_toplevel (GTK_WIDGET (impl));
6165 if (toplevel && toplevel->focus_widget)
6166 gtk_widget_grab_focus (toplevel->focus_widget);
6169 gtk_widget_destroy (dialog);
6172 /* Handler for the "up-folder" keybinding signal */
6174 up_folder_handler (GtkFileChooserDefault *impl)
6176 pending_op_queue (impl, PENDING_OP_SELECT_PATH, impl->current_folder);
6177 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
6180 /* Handler for the "down-folder" keybinding signal */
6182 down_folder_handler (GtkFileChooserDefault *impl)
6184 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
6187 /* Handler for the "home-folder" keybinding signal */
6189 home_folder_handler (GtkFileChooserDefault *impl)
6194 if (!impl->has_home)
6195 return; /* Should we put up an error dialog? */
6197 pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
6198 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
6199 g_assert_not_reached ();
6201 shortcuts_activate_iter (impl, &iter);
6206 /* Drag and drop interfaces */
6209 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
6214 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
6219 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
6221 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
6224 ShortcutsModelFilter *model;
6228 model = SHORTCUTS_MODEL_FILTER (drag_source);
6230 pos = *gtk_tree_path_get_indices (path);
6231 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
6233 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
6236 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
6238 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
6240 GtkSelectionData *selection_data)
6242 ShortcutsModelFilter *model;
6244 model = SHORTCUTS_MODEL_FILTER (drag_source);
6251 /* Fill the GtkTreeDragSourceIface vtable */
6253 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
6255 iface->row_draggable = shortcuts_model_filter_row_draggable;
6256 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
6260 /* Fill the GtkTreeDragDestIface vtable */
6262 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
6264 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
6265 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
6269 static GtkTreeModel *
6270 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
6271 GtkTreeModel *child_model,
6274 ShortcutsModelFilter *model;
6276 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
6277 "child_model", child_model,
6278 "virtual_root", root,
6283 return GTK_TREE_MODEL (model);