1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "gdk/gdkkeysyms.h"
23 #include "gtkalignment.h"
24 #include "gtkbindings.h"
25 #include "gtkbutton.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrendererpixbuf.h"
28 #include "gtkcellrendererseptext.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkcombobox.h"
33 #include "gtkexpander.h"
34 #include "gtkfilechooserdefault.h"
35 #include "gtkfilechooserembed.h"
36 #include "gtkfilechooserentry.h"
37 #include "gtkfilechooserutils.h"
38 #include "gtkfilechooser.h"
39 #include "gtkfilesystemmodel.h"
42 #include "gtkhpaned.h"
43 #include "gtkiconfactory.h"
44 #include "gtkicontheme.h"
46 #include "gtkimagemenuitem.h"
49 #include "gtkmarshalers.h"
50 #include "gtkmenuitem.h"
51 #include "gtkmessagedialog.h"
52 #include "gtkpathbar.h"
53 #include "gtkprivate.h"
54 #include "gtkscrolledwindow.h"
55 #include "gtkseparatormenuitem.h"
56 #include "gtksizegroup.h"
59 #include "gtktreednd.h"
60 #include "gtktreeprivate.h"
61 #include "gtktreeview.h"
62 #include "gtktreemodelsort.h"
63 #include "gtktreeselection.h"
64 #include "gtktreestore.h"
65 #include "gtktypebuiltins.h"
68 #if defined (G_OS_UNIX)
69 #include "gtkfilesystemunix.h"
70 #elif defined (G_OS_WIN32)
71 #include "gtkfilesystemwin32.h"
77 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
79 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
80 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
81 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
84 struct _GtkFileChooserDefaultClass
86 GtkVBoxClass parent_class;
89 struct _GtkFileChooserDefault
91 GtkVBox parent_instance;
93 GtkFileChooserAction action;
95 GtkFileSystem *file_system;
97 /* Save mode widgets */
98 GtkWidget *save_widgets;
100 GtkWidget *save_file_name_entry;
101 GtkWidget *save_folder_label;
102 GtkWidget *save_folder_combo;
103 GtkWidget *save_extra_align;
104 GtkWidget *save_expander;
106 /* The file browsing widgets */
107 GtkWidget *browse_widgets;
108 GtkWidget *browse_shortcuts_tree_view;
109 GtkWidget *browse_shortcuts_add_button;
110 GtkWidget *browse_shortcuts_remove_button;
111 GtkWidget *browse_files_tree_view;
112 GtkWidget *browse_files_popup_menu;
113 GtkWidget *browse_files_popup_menu_add_shortcut_item;
114 GtkWidget *browse_files_popup_menu_hidden_files_item;
115 GtkWidget *browse_new_folder_button;
116 GtkWidget *browse_path_bar;
117 GtkWidget *browse_extra_align;
119 GtkFileSystemModel *browse_files_model;
121 GtkWidget *filter_combo_hbox;
122 GtkWidget *filter_combo;
123 GtkWidget *preview_box;
124 GtkWidget *preview_label;
125 GtkWidget *preview_widget;
126 GtkWidget *extra_widget;
128 GtkListStore *shortcuts_model;
129 GtkTreeModel *shortcuts_filter_model;
131 GtkTreeModelSort *sort_model;
133 GtkFileFilter *current_filter;
137 gboolean has_desktop;
143 gulong volumes_changed_id;
144 gulong bookmarks_changed_id;
146 GtkFilePath *current_volume_path;
147 GtkFilePath *current_folder;
148 GtkFilePath *preview_path;
149 char *preview_display_name;
151 GtkTreeViewColumn *list_name_column;
152 GtkCellRenderer *list_name_renderer;
154 GSource *edited_idle;
155 char *edited_new_text;
157 gulong settings_signal_id;
160 gulong toplevel_set_focus_id;
161 GtkWidget *toplevel_last_focus_widget;
164 GdkDragContext *shortcuts_drag_context;
165 GSource *shortcuts_drag_outside_idle;
170 guint local_only : 1;
171 guint preview_widget_active : 1;
172 guint use_preview_label : 1;
173 guint select_multiple : 1;
174 guint show_hidden : 1;
175 guint list_sort_ascending : 1;
176 guint changing_folder : 1;
177 guint shortcuts_current_folder_active : 1;
178 guint shortcuts_current_folder_is_volume : 1;
181 guint shortcuts_drag_outside : 1;
194 static guint signals[LAST_SIGNAL] = { 0 };
196 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
198 SHORTCUTS_COL_PIXBUF,
201 SHORTCUTS_COL_REMOVABLE,
202 SHORTCUTS_COL_PIXBUF_VISIBLE,
203 SHORTCUTS_COL_NUM_COLUMNS
206 /* Column numbers for the file list */
211 FILE_LIST_COL_NUM_COLUMNS
214 /* Identifiers for target types */
220 /* Target types for dragging from the shortcuts list */
221 static GtkTargetEntry shortcuts_source_targets[] = {
222 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
225 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
226 / sizeof (shortcuts_source_targets[0]));
228 /* Target types for dropping into the shortcuts list */
229 static GtkTargetEntry shortcuts_dest_targets[] = {
230 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
231 { "text/uri-list", 0, TEXT_URI_LIST }
234 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
235 / sizeof (shortcuts_dest_targets[0]));
237 /* Target types for DnD from the file list */
238 static GtkTargetEntry file_list_source_targets[] = {
239 { "text/uri-list", 0, TEXT_URI_LIST }
242 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
243 / sizeof (file_list_source_targets[0]));
245 /* Interesting places in the shortcuts bar */
251 SHORTCUTS_BOOKMARKS_SEPARATOR,
253 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
254 SHORTCUTS_CURRENT_FOLDER
257 /* Icon size for if we can't get it from the theme */
258 #define FALLBACK_ICON_SIZE 20
260 #define PREVIEW_HBOX_SPACING 12
264 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
265 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
266 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
267 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
269 static GObject* gtk_file_chooser_default_constructor (GType type,
270 guint n_construct_properties,
271 GObjectConstructParam *construct_params);
272 static void gtk_file_chooser_default_finalize (GObject *object);
273 static void gtk_file_chooser_default_set_property (GObject *object,
277 static void gtk_file_chooser_default_get_property (GObject *object,
281 static void gtk_file_chooser_default_dispose (GObject *object);
282 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
283 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
284 GtkWidget *previous_toplevel);
285 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
286 GtkStyle *previous_style);
287 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
288 GdkScreen *previous_screen);
290 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
291 const GtkFilePath *path,
293 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
294 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
296 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
297 const GtkFilePath *path,
299 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
300 const GtkFilePath *path);
301 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
302 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
303 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
304 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
305 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
306 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
307 GtkFileFilter *filter);
308 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
309 GtkFileFilter *filter);
310 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
311 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
312 const GtkFilePath *path,
314 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
315 const GtkFilePath *path,
317 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
319 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
321 gint *default_height);
322 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
323 gboolean *resize_horizontally,
324 gboolean *resize_vertically);
325 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
326 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
328 static void location_popup_handler (GtkFileChooserDefault *impl);
329 static void up_folder_handler (GtkFileChooserDefault *impl);
330 static void down_folder_handler (GtkFileChooserDefault *impl);
331 static void home_folder_handler (GtkFileChooserDefault *impl);
332 static void update_appearance (GtkFileChooserDefault *impl);
334 static void set_current_filter (GtkFileChooserDefault *impl,
335 GtkFileFilter *filter);
336 static void check_preview_change (GtkFileChooserDefault *impl);
338 static void filter_combo_changed (GtkComboBox *combo_box,
339 GtkFileChooserDefault *impl);
340 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
342 GtkTreeViewColumn *column,
343 GtkFileChooserDefault *impl);
345 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
347 GtkFileChooserDefault *impl);
349 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
352 gboolean path_currently_selected,
354 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
356 static int shortcuts_get_index (GtkFileChooserDefault *impl,
357 ShortcutsIndex where);
358 static int shortcut_find_position (GtkFileChooserDefault *impl,
359 const GtkFilePath *path);
361 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
363 static void list_selection_changed (GtkTreeSelection *tree_selection,
364 GtkFileChooserDefault *impl);
365 static void list_row_activated (GtkTreeView *tree_view,
367 GtkTreeViewColumn *column,
368 GtkFileChooserDefault *impl);
370 static void path_bar_clicked (GtkPathBar *path_bar,
371 GtkFilePath *file_path,
372 gboolean child_is_hidden,
373 GtkFileChooserDefault *impl);
375 static void add_bookmark_button_clicked_cb (GtkButton *button,
376 GtkFileChooserDefault *impl);
377 static void remove_bookmark_button_clicked_cb (GtkButton *button,
378 GtkFileChooserDefault *impl);
380 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
381 GtkCellRenderer *cell,
382 GtkTreeModel *tree_model,
385 static void list_name_data_func (GtkTreeViewColumn *tree_column,
386 GtkCellRenderer *cell,
387 GtkTreeModel *tree_model,
391 static void list_size_data_func (GtkTreeViewColumn *tree_column,
392 GtkCellRenderer *cell,
393 GtkTreeModel *tree_model,
397 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
398 GtkCellRenderer *cell,
399 GtkTreeModel *tree_model,
403 static GObjectClass *parent_class;
407 /* Drag and drop interface declarations */
410 GtkTreeModelFilter parent;
412 GtkFileChooserDefault *impl;
413 } ShortcutsModelFilter;
416 GtkTreeModelFilterClass parent_class;
417 } ShortcutsModelFilterClass;
419 #define SHORTCUTS_MODEL_FILTER_TYPE (shortcuts_model_filter_get_type ())
420 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
422 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
424 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
425 shortcuts_model_filter,
426 GTK_TYPE_TREE_MODEL_FILTER,
427 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
428 shortcuts_model_filter_drag_source_iface_init));
430 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
431 GtkTreeModel *child_model,
437 _gtk_file_chooser_default_get_type (void)
439 static GType file_chooser_default_type = 0;
441 if (!file_chooser_default_type)
443 static const GTypeInfo file_chooser_default_info =
445 sizeof (GtkFileChooserDefaultClass),
446 NULL, /* base_init */
447 NULL, /* base_finalize */
448 (GClassInitFunc) gtk_file_chooser_default_class_init,
449 NULL, /* class_finalize */
450 NULL, /* class_data */
451 sizeof (GtkFileChooserDefault),
453 (GInstanceInitFunc) gtk_file_chooser_default_init,
456 static const GInterfaceInfo file_chooser_info =
458 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
459 NULL, /* interface_finalize */
460 NULL /* interface_data */
463 static const GInterfaceInfo file_chooser_embed_info =
465 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
466 NULL, /* interface_finalize */
467 NULL /* interface_data */
470 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
471 &file_chooser_default_info, 0);
473 g_type_add_interface_static (file_chooser_default_type,
474 GTK_TYPE_FILE_CHOOSER,
476 g_type_add_interface_static (file_chooser_default_type,
477 GTK_TYPE_FILE_CHOOSER_EMBED,
478 &file_chooser_embed_info);
481 return file_chooser_default_type;
485 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
487 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
488 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
489 GtkBindingSet *binding_set;
491 parent_class = g_type_class_peek_parent (class);
493 gobject_class->finalize = gtk_file_chooser_default_finalize;
494 gobject_class->constructor = gtk_file_chooser_default_constructor;
495 gobject_class->set_property = gtk_file_chooser_default_set_property;
496 gobject_class->get_property = gtk_file_chooser_default_get_property;
497 gobject_class->dispose = gtk_file_chooser_default_dispose;
499 widget_class->show_all = gtk_file_chooser_default_show_all;
500 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
501 widget_class->style_set = gtk_file_chooser_default_style_set;
502 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
504 signals[LOCATION_POPUP] =
505 _gtk_binding_signal_new ("location-popup",
506 G_OBJECT_CLASS_TYPE (class),
507 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
508 G_CALLBACK (location_popup_handler),
510 _gtk_marshal_VOID__VOID,
513 _gtk_binding_signal_new ("up-folder",
514 G_OBJECT_CLASS_TYPE (class),
515 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
516 G_CALLBACK (up_folder_handler),
518 _gtk_marshal_VOID__VOID,
520 signals[DOWN_FOLDER] =
521 _gtk_binding_signal_new ("down-folder",
522 G_OBJECT_CLASS_TYPE (class),
523 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
524 G_CALLBACK (down_folder_handler),
526 _gtk_marshal_VOID__VOID,
528 signals[HOME_FOLDER] =
529 _gtk_binding_signal_new ("home-folder",
530 G_OBJECT_CLASS_TYPE (class),
531 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
532 G_CALLBACK (home_folder_handler),
534 _gtk_marshal_VOID__VOID,
537 binding_set = gtk_binding_set_by_class (class);
539 gtk_binding_entry_add_signal (binding_set,
540 GDK_l, GDK_CONTROL_MASK,
544 gtk_binding_entry_add_signal (binding_set,
545 GDK_Up, GDK_MOD1_MASK,
548 gtk_binding_entry_add_signal (binding_set,
549 GDK_KP_Up, GDK_MOD1_MASK,
553 gtk_binding_entry_add_signal (binding_set,
554 GDK_Down, GDK_MOD1_MASK,
557 gtk_binding_entry_add_signal (binding_set,
558 GDK_KP_Down, GDK_MOD1_MASK,
562 gtk_binding_entry_add_signal (binding_set,
563 GDK_Home, GDK_MOD1_MASK,
566 gtk_binding_entry_add_signal (binding_set,
567 GDK_KP_Home, GDK_MOD1_MASK,
571 _gtk_file_chooser_install_properties (gobject_class);
573 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
574 P_("Default file chooser backend"),
575 P_("Name of the GtkFileChooser backend to use by default"),
581 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
583 iface->select_path = gtk_file_chooser_default_select_path;
584 iface->unselect_path = gtk_file_chooser_default_unselect_path;
585 iface->select_all = gtk_file_chooser_default_select_all;
586 iface->unselect_all = gtk_file_chooser_default_unselect_all;
587 iface->get_paths = gtk_file_chooser_default_get_paths;
588 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
589 iface->get_file_system = gtk_file_chooser_default_get_file_system;
590 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
591 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
592 iface->set_current_name = gtk_file_chooser_default_set_current_name;
593 iface->add_filter = gtk_file_chooser_default_add_filter;
594 iface->remove_filter = gtk_file_chooser_default_remove_filter;
595 iface->list_filters = gtk_file_chooser_default_list_filters;
596 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
597 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
598 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
602 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
604 iface->get_default_size = gtk_file_chooser_default_get_default_size;
605 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
606 iface->should_respond = gtk_file_chooser_default_should_respond;
607 iface->initial_focus = gtk_file_chooser_default_initial_focus;
610 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
612 impl->local_only = TRUE;
613 impl->preview_widget_active = TRUE;
614 impl->use_preview_label = TRUE;
615 impl->select_multiple = FALSE;
616 impl->show_hidden = FALSE;
617 impl->icon_size = FALLBACK_ICON_SIZE;
619 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (impl), TRUE);
620 gtk_box_set_spacing (GTK_BOX (impl), 12);
624 gtk_file_chooser_default_finalize (GObject *object)
626 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
629 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
630 impl->volumes_changed_id = 0;
631 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
632 impl->bookmarks_changed_id = 0;
633 g_object_unref (impl->file_system);
635 for (l = impl->filters; l; l = l->next)
637 GtkFileFilter *filter;
639 filter = GTK_FILE_FILTER (l->data);
640 g_object_unref (filter);
642 g_slist_free (impl->filters);
644 if (impl->current_filter)
645 g_object_unref (impl->current_filter);
647 if (impl->current_volume_path)
648 gtk_file_path_free (impl->current_volume_path);
650 if (impl->current_folder)
651 gtk_file_path_free (impl->current_folder);
653 if (impl->preview_path)
654 gtk_file_path_free (impl->preview_path);
656 /* Free all the Models we have */
657 if (impl->browse_files_model)
658 g_object_unref (impl->browse_files_model);
660 if (impl->shortcuts_model)
661 g_object_unref (impl->shortcuts_model);
663 if (impl->shortcuts_filter_model)
664 g_object_unref (impl->shortcuts_filter_model);
666 if (impl->sort_model)
667 g_object_unref (impl->sort_model);
669 g_free (impl->preview_display_name);
671 g_free (impl->edited_new_text);
673 G_OBJECT_CLASS (parent_class)->finalize (object);
676 /* Shows an error dialog set as transient for the specified window */
678 error_message_with_parent (GtkWindow *parent,
683 dialog = gtk_message_dialog_new (parent,
684 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
689 gtk_dialog_run (GTK_DIALOG (dialog));
690 gtk_widget_destroy (dialog);
693 /* Returns a toplevel GtkWindow, or NULL if none */
695 get_toplevel (GtkWidget *widget)
699 toplevel = gtk_widget_get_toplevel (widget);
700 if (!GTK_WIDGET_TOPLEVEL (toplevel))
703 return GTK_WINDOW (toplevel);
706 /* Shows an error dialog for the file chooser */
708 error_message (GtkFileChooserDefault *impl,
711 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg);
714 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
716 error_dialog (GtkFileChooserDefault *impl,
718 const GtkFilePath *path,
721 g_return_if_fail (path != NULL);
725 char *uri = gtk_file_system_path_to_uri (impl->file_system, path);
726 char *text = g_strdup_printf (msg,
729 error_message (impl, text);
732 g_error_free (error);
736 /* Displays an error message about not being able to get information for a file.
737 * Frees the GError as well.
740 error_getting_info_dialog (GtkFileChooserDefault *impl,
741 const GtkFilePath *path,
745 _("Could not retrieve information about %s:\n%s"),
749 /* Shows an error dialog about not being able to add a bookmark */
751 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
752 const GtkFilePath *path,
756 _("Could not add a bookmark for %s:\n%s"),
760 /* Shows an error dialog about not being able to compose a filename */
762 error_building_filename_dialog (GtkFileChooserDefault *impl,
763 const GtkFilePath *base_path,
764 const char *file_part,
770 uri = gtk_file_system_path_to_uri (impl->file_system, base_path);
771 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
774 error_message (impl, msg);
777 g_error_free (error);
780 /* Shows an error dialog when we cannot switch to a folder */
782 error_changing_folder_dialog (GtkFileChooserDefault *impl,
783 const GtkFilePath *path,
787 _("Could not change the current folder to %s:\n%s"),
792 /* Changes folders, displaying an error dialog if this fails */
794 change_folder_and_display_error (GtkFileChooserDefault *impl,
795 const GtkFilePath *path)
799 GtkFilePath *path_copy;
801 /* We copy the path because of this case:
803 * list_row_activated()
804 * fetches path from model; path belongs to the model (*)
805 * calls change_folder_and_display_error()
806 * calls _gtk_file_chooser_set_current_folder_path()
807 * changing folders fails, sets model to NULL, thus freeing the path in (*)
810 path_copy = gtk_file_path_copy (path);
813 result = _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path_copy, &error);
816 error_changing_folder_dialog (impl, path_copy, error);
818 gtk_file_path_free (path_copy);
824 update_preview_widget_visibility (GtkFileChooserDefault *impl)
826 if (impl->use_preview_label)
828 if (!impl->preview_label)
830 impl->preview_label = gtk_label_new (impl->preview_display_name);
831 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
832 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
833 gtk_widget_show (impl->preview_label);
838 if (impl->preview_label)
840 gtk_widget_destroy (impl->preview_label);
841 impl->preview_label = NULL;
845 if (impl->preview_widget_active && impl->preview_widget)
846 gtk_widget_show (impl->preview_box);
848 gtk_widget_hide (impl->preview_box);
850 g_signal_emit_by_name (impl, "default-size-changed");
854 set_preview_widget (GtkFileChooserDefault *impl,
855 GtkWidget *preview_widget)
857 if (preview_widget == impl->preview_widget)
860 if (impl->preview_widget)
861 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
862 impl->preview_widget);
864 impl->preview_widget = preview_widget;
865 if (impl->preview_widget)
867 gtk_widget_show (impl->preview_widget);
868 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
869 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
870 impl->preview_widget,
871 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
874 update_preview_widget_visibility (impl);
877 /* Re-reads all the icons for the shortcuts, used when the theme changes */
879 shortcuts_reload_icons (GtkFileChooserDefault *impl)
883 int bookmarks_separator_idx;
884 int current_folder_separator_idx;
887 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
890 bookmarks_separator_idx = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
891 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
892 volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
898 gboolean pixbuf_visible;
901 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
902 SHORTCUTS_COL_PATH, &data,
903 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
906 if (!pixbuf_visible || !data)
909 if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
911 GtkFileSystemVolume *volume;
914 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
915 impl->icon_size, NULL);
919 const GtkFilePath *path;
922 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
923 impl->icon_size, NULL);
926 gtk_list_store_set (impl->shortcuts_model, &iter,
927 SHORTCUTS_COL_PIXBUF, pixbuf,
932 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
935 /* If a shortcut corresponds to the current folder, selects it */
937 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
939 GtkTreeSelection *selection;
943 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
945 pos = shortcut_find_position (impl, impl->current_folder);
948 gtk_tree_selection_unselect_all (selection);
952 path = gtk_tree_path_new_from_indices (pos, -1);
953 gtk_tree_selection_select_path (selection, path);
954 gtk_tree_path_free (path);
957 /* Returns whether a path is a folder */
959 check_is_folder (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
961 GtkFileFolder *folder;
963 folder = gtk_file_system_get_folder (file_system, path,
964 GTK_FILE_INFO_DISPLAY_NAME,
969 g_object_unref (folder);
973 /* Convenience function to get the display name and icon info for a path */
975 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, gboolean name_only, GError **error)
977 GtkFilePath *parent_path;
978 GtkFileFolder *parent_folder;
983 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
986 parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
987 GTK_FILE_INFO_DISPLAY_NAME
988 | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
993 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, error);
994 g_object_unref (parent_folder);
998 gtk_file_path_free (parent_path);
1002 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1003 * inserts a volume. A position of -1 indicates the end of the tree.
1006 shortcuts_insert_path (GtkFileChooserDefault *impl,
1009 GtkFileSystemVolume *volume,
1010 const GtkFilePath *path,
1023 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1024 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1025 impl->icon_size, NULL);
1029 if (!check_is_folder (impl->file_system, path, error))
1033 label_copy = g_strdup (label);
1036 GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
1041 label_copy = g_strdup (gtk_file_info_get_display_name (info));
1042 gtk_file_info_free (info);
1045 data = gtk_file_path_copy (path);
1046 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1047 impl->icon_size, NULL);
1051 gtk_list_store_append (impl->shortcuts_model, &iter);
1053 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1055 gtk_list_store_set (impl->shortcuts_model, &iter,
1056 SHORTCUTS_COL_PIXBUF, pixbuf,
1057 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1058 SHORTCUTS_COL_NAME, label_copy,
1059 SHORTCUTS_COL_PATH, data,
1060 SHORTCUTS_COL_REMOVABLE, removable,
1063 g_free (label_copy);
1066 g_object_unref (pixbuf);
1071 /* Appends an item for the user's home directory to the shortcuts model */
1073 shortcuts_append_home (GtkFileChooserDefault *impl)
1076 GtkFilePath *home_path;
1079 home = g_get_home_dir ();
1080 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1083 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1084 if (!impl->has_home)
1085 error_getting_info_dialog (impl, home_path, error);
1087 gtk_file_path_free (home_path);
1090 /* Appends the ~/Desktop directory to the shortcuts model */
1092 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1097 name = g_build_filename (g_get_home_dir (), "Desktop", NULL);
1098 path = gtk_file_system_filename_to_path (impl->file_system, name);
1101 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1102 /* We do not actually pop up an error dialog if there is no desktop directory
1103 * because some people may really not want to have one.
1106 gtk_file_path_free (path);
1109 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1111 shortcuts_append_paths (GtkFileChooserDefault *impl,
1117 /* As there is no separator now, we want to start there.
1119 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1122 for (; paths; paths = paths->next)
1130 if (impl->local_only &&
1131 !gtk_file_system_path_is_local (impl->file_system, path))
1134 /* NULL GError, but we don't really want to show error boxes here */
1135 if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, NULL, TRUE, NULL))
1139 return num_inserted;
1142 /* Returns the index for the corresponding item in the shortcuts bar */
1144 shortcuts_get_index (GtkFileChooserDefault *impl,
1145 ShortcutsIndex where)
1151 if (where == SHORTCUTS_HOME)
1154 n += impl->has_home ? 1 : 0;
1156 if (where == SHORTCUTS_DESKTOP)
1159 n += impl->has_desktop ? 1 : 0;
1161 if (where == SHORTCUTS_VOLUMES)
1164 n += impl->num_volumes;
1166 if (where == SHORTCUTS_SHORTCUTS)
1169 n += impl->num_shortcuts;
1171 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1174 /* If there are no bookmarks there won't be a separator */
1175 n += (impl->num_bookmarks > 0) ? 1 : 0;
1177 if (where == SHORTCUTS_BOOKMARKS)
1180 n += impl->num_bookmarks;
1182 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1187 if (where == SHORTCUTS_CURRENT_FOLDER)
1190 g_assert_not_reached ();
1197 typedef void (* RemoveFunc) (GtkFileChooserDefault *impl, gpointer data);
1199 /* Removes the specified number of rows from the shortcuts list */
1201 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1204 RemoveFunc remove_fn)
1208 path = gtk_tree_path_new_from_indices (start_row, -1);
1210 for (; n_rows; n_rows--)
1215 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1216 g_assert_not_reached ();
1220 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1221 (* remove_fn) (impl, data);
1224 gtk_list_store_remove (impl->shortcuts_model, &iter);
1227 gtk_tree_path_free (path);
1230 /* Used from shortcuts_remove_rows() in shortcuts_add_volumes() */
1232 volume_remove_cb (GtkFileChooserDefault *impl, gpointer data)
1234 GtkFileSystemVolume *volume;
1237 gtk_file_system_volume_free (impl->file_system, volume);
1240 /* Adds all the file system volumes to the shortcuts model */
1242 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1247 gboolean old_changing_folders;
1249 old_changing_folders = impl->changing_folder;
1250 impl->changing_folder = TRUE;
1252 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1253 shortcuts_remove_rows (impl, start_row, impl->num_volumes, volume_remove_cb);
1254 impl->num_volumes = 0;
1256 list = gtk_file_system_list_volumes (impl->file_system);
1260 for (l = list; l; l = l->next)
1262 GtkFileSystemVolume *volume;
1266 if (impl->local_only)
1268 GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1269 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1270 gtk_file_path_free (base_path);
1274 gtk_file_system_volume_free (impl->file_system, volume);
1279 if (shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL))
1282 gtk_file_system_volume_free (impl->file_system, volume);
1285 impl->num_volumes = n;
1286 g_slist_free (list);
1288 if (impl->shortcuts_filter_model)
1289 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1291 impl->changing_folder = old_changing_folders;
1294 /* Used from shortcuts_remove_rows() */
1296 remove_bookmark_cb (GtkFileChooserDefault *impl, gpointer data)
1301 gtk_file_path_free (path);
1304 /* Inserts a separator node in the shortcuts list */
1306 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1307 ShortcutsIndex where)
1311 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1313 gtk_list_store_insert (impl->shortcuts_model, &iter,
1314 shortcuts_get_index (impl, where));
1315 gtk_list_store_set (impl->shortcuts_model, &iter,
1316 SHORTCUTS_COL_PIXBUF, NULL,
1317 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1318 SHORTCUTS_COL_NAME, NULL,
1319 SHORTCUTS_COL_PATH, NULL,
1323 /* Updates the list of bookmarks */
1325 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1328 gboolean old_changing_folders;
1330 old_changing_folders = impl->changing_folder;
1331 impl->changing_folder = TRUE;
1333 if (impl->num_bookmarks > 0)
1335 shortcuts_remove_rows (impl,
1336 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1337 impl->num_bookmarks + 1,
1338 remove_bookmark_cb);
1342 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1343 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1344 gtk_file_paths_free (bookmarks);
1346 if (impl->num_bookmarks > 0)
1348 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1350 if (impl->shortcuts_filter_model)
1351 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1353 impl->changing_folder = old_changing_folders;
1356 /* Appends a separator and a row to the shortcuts list for the current folder */
1358 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1363 g_assert (!impl->shortcuts_current_folder_active);
1367 pos = shortcut_find_position (impl, impl->current_folder);
1370 GtkFileSystemVolume *volume;
1371 GtkFilePath *base_path;
1375 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1379 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1381 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1383 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1388 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1390 success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1391 impl->shortcuts_current_folder_is_volume = TRUE;
1395 success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1396 impl->shortcuts_current_folder_is_volume = FALSE;
1400 gtk_file_system_volume_free (impl->file_system, volume);
1401 gtk_file_path_free (base_path);
1404 shortcuts_remove_rows (impl, pos - 1, 1, NULL); /* remove the separator */
1406 impl->shortcuts_current_folder_active = success;
1410 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1413 /* Used from shortcuts_remove_rows() in shortcuts_update_current_folder() */
1415 remove_current_folder_cb (GtkFileChooserDefault *impl,
1418 if (impl->shortcuts_current_folder_is_volume)
1419 gtk_file_system_volume_free (impl->file_system, data);
1421 gtk_file_path_free (data);
1424 /* Updates the current folder row in the shortcuts model */
1426 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1430 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1432 if (impl->shortcuts_current_folder_active)
1434 shortcuts_remove_rows (impl, pos, 2, remove_current_folder_cb);
1435 impl->shortcuts_current_folder_active = FALSE;
1438 shortcuts_add_current_folder (impl);
1441 /* Filter function used for the shortcuts filter model */
1443 shortcuts_filter_cb (GtkTreeModel *model,
1447 GtkFileChooserDefault *impl;
1451 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1453 path = gtk_tree_model_get_path (model, iter);
1457 pos = *gtk_tree_path_get_indices (path);
1458 gtk_tree_path_free (path);
1460 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1463 /* Creates the list model for shortcuts */
1465 shortcuts_model_create (GtkFileChooserDefault *impl)
1467 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1468 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1469 GDK_TYPE_PIXBUF, /* pixbuf */
1470 G_TYPE_STRING, /* name */
1471 G_TYPE_POINTER, /* path or volume */
1472 G_TYPE_BOOLEAN, /* removable */
1473 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1475 if (impl->file_system)
1477 shortcuts_append_home (impl);
1478 shortcuts_append_desktop (impl);
1479 shortcuts_add_volumes (impl);
1480 shortcuts_add_bookmarks (impl);
1483 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1484 GTK_TREE_MODEL (impl->shortcuts_model),
1487 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1488 shortcuts_filter_cb,
1493 /* Callback used when the "New Folder" button is clicked */
1495 new_folder_button_clicked (GtkButton *button,
1496 GtkFileChooserDefault *impl)
1501 if (!impl->browse_files_model)
1502 return; /* FIXME: this sucks. Disable the New Folder button or something. */
1504 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1506 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1507 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1508 path, impl->list_name_column,
1511 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1512 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1514 impl->list_name_column,
1518 /* Idle handler for creating a new folder after editing its name cell, or for
1519 * canceling the editing.
1522 edited_idle_cb (GtkFileChooserDefault *impl)
1524 g_source_destroy (impl->edited_idle);
1525 impl->edited_idle = NULL;
1527 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1528 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1530 if (impl->edited_new_text) /* not cancelled? */
1533 GtkFilePath *file_path;
1536 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, impl->edited_new_text,
1541 if (gtk_file_system_create_folder (impl->file_system, file_path, &error))
1542 change_folder_and_display_error (impl, file_path);
1545 _("Could not create folder %s:\n%s"),
1548 gtk_file_path_free (file_path);
1551 error_building_filename_dialog (impl, impl->current_folder, impl->edited_new_text, error);
1553 g_free (impl->edited_new_text);
1554 impl->edited_new_text = NULL;
1561 queue_edited_idle (GtkFileChooserDefault *impl,
1562 const gchar *new_text)
1564 /* We create the folder in an idle handler so that we don't modify the tree
1568 g_assert (!impl->edited_idle);
1569 g_assert (!impl->edited_new_text);
1571 impl->edited_idle = g_idle_source_new ();
1572 g_source_set_closure (impl->edited_idle,
1573 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
1575 g_source_attach (impl->edited_idle, NULL);
1578 impl->edited_new_text = g_strdup (new_text);
1581 /* Callback used from the text cell renderer when the new folder is named */
1583 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1585 const gchar *new_text,
1586 GtkFileChooserDefault *impl)
1588 queue_edited_idle (impl, new_text);
1591 /* Callback used from the text cell renderer when the new folder edition gets
1595 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1596 GtkFileChooserDefault *impl)
1598 queue_edited_idle (impl, NULL);
1601 /* Creates the widgets for the filter combo box */
1603 filter_create (GtkFileChooserDefault *impl)
1605 impl->filter_combo = gtk_combo_box_new_text ();
1606 g_signal_connect (impl->filter_combo, "changed",
1607 G_CALLBACK (filter_combo_changed), impl);
1609 return impl->filter_combo;
1613 button_new (GtkFileChooserDefault *impl,
1615 const char *stock_id,
1625 button = gtk_button_new ();
1626 hbox = gtk_hbox_new (FALSE, 2);
1627 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1629 gtk_container_add (GTK_CONTAINER (button), align);
1630 gtk_container_add (GTK_CONTAINER (align), hbox);
1631 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1633 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1635 widget = gtk_label_new_with_mnemonic (text);
1636 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1637 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1639 gtk_widget_set_sensitive (button, sensitive);
1640 g_signal_connect (button, "clicked", callback, impl);
1642 gtk_widget_show_all (align);
1645 gtk_widget_show (button);
1650 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1652 shortcut_find_position (GtkFileChooserDefault *impl,
1653 const GtkFilePath *path)
1657 int bookmarks_separator_idx;
1658 int current_folder_separator_idx;
1661 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1664 bookmarks_separator_idx = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1665 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1666 volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1670 for (i = 0; i < current_folder_separator_idx; i++)
1674 if (i == bookmarks_separator_idx)
1677 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1679 if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
1681 GtkFileSystemVolume *volume;
1682 GtkFilePath *base_path;
1686 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1688 exists = strcmp (gtk_file_path_get_string (path),
1689 gtk_file_path_get_string (base_path)) == 0;
1697 GtkFilePath *model_path;
1701 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1706 gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1712 /* Tries to add a bookmark from a path name */
1714 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1715 const GtkFilePath *path,
1720 if (shortcut_find_position (impl, path) != -1)
1723 /* FIXME: this check really belongs in gtk_file_system_insert_bookmark. */
1725 if (!check_is_folder (impl->file_system, path, &error))
1728 _("Could not add bookmark for %s because it is not a folder."),
1735 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
1737 error_could_not_add_bookmark_dialog (impl, path, error);
1745 add_bookmark_foreach_cb (GtkTreeModel *model,
1750 GtkFileChooserDefault *impl;
1751 GtkFileSystemModel *fs_model;
1752 GtkTreeIter child_iter;
1753 const GtkFilePath *file_path;
1755 impl = (GtkFileChooserDefault *) data;
1757 fs_model = impl->browse_files_model;
1758 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1760 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
1761 shortcuts_add_bookmark_from_path (impl, file_path, -1);
1764 /* Adds a bookmark from the currently selected item in the file list */
1766 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
1768 GtkTreeSelection *selection;
1770 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1772 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1773 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
1775 gtk_tree_selection_selected_foreach (selection,
1776 add_bookmark_foreach_cb,
1780 /* Callback used when the "Add bookmark" button is clicked */
1782 add_bookmark_button_clicked_cb (GtkButton *button,
1783 GtkFileChooserDefault *impl)
1785 bookmarks_add_selected_folder (impl);
1788 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
1789 * returns FALSE if no shortcut is selected.
1792 shortcuts_get_selected (GtkFileChooserDefault *impl,
1795 GtkTreeSelection *selection;
1796 GtkTreeIter parent_iter;
1798 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1800 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
1803 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1809 /* Removes the selected bookmarks */
1811 remove_selected_bookmarks (GtkFileChooserDefault *impl)
1818 if (!shortcuts_get_selected (impl, &iter))
1821 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1822 SHORTCUTS_COL_PATH, &path,
1823 SHORTCUTS_COL_REMOVABLE, &removable, -1);
1828 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1831 _("Could not remove bookmark for %s:\n%s"),
1837 /* Callback used when the "Remove bookmark" button is clicked */
1839 remove_bookmark_button_clicked_cb (GtkButton *button,
1840 GtkFileChooserDefault *impl)
1842 remove_selected_bookmarks (impl);
1845 struct selection_check_closure {
1846 GtkFileChooserDefault *impl;
1849 gboolean all_folders;
1852 /* Used from gtk_tree_selection_selected_foreach() */
1854 selection_check_foreach_cb (GtkTreeModel *model,
1859 struct selection_check_closure *closure;
1860 GtkTreeIter child_iter;
1861 const GtkFileInfo *info;
1865 closure->num_selected++;
1867 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1869 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
1870 is_folder = gtk_file_info_get_is_folder (info);
1872 closure->all_folders &= is_folder;
1873 closure->all_files &= !is_folder;
1876 /* Checks whether the selected items in the file list are all files or all folders */
1878 selection_check (GtkFileChooserDefault *impl,
1880 gboolean *all_files,
1881 gboolean *all_folders)
1883 struct selection_check_closure closure;
1884 GtkTreeSelection *selection;
1886 closure.impl = impl;
1887 closure.num_selected = 0;
1888 closure.all_files = TRUE;
1889 closure.all_folders = TRUE;
1891 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1892 gtk_tree_selection_selected_foreach (selection,
1893 selection_check_foreach_cb,
1896 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
1899 *num_selected = closure.num_selected;
1902 *all_files = closure.all_files;
1905 *all_folders = closure.all_folders;
1908 struct get_selected_path_closure {
1909 GtkFileChooserDefault *impl;
1910 const GtkFilePath *path;
1914 get_selected_path_foreach_cb (GtkTreeModel *model,
1919 struct get_selected_path_closure *closure;
1920 GtkTreeIter child_iter;
1924 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1925 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
1928 /* Returns a selected path from the file list */
1929 static const GtkFilePath *
1930 get_selected_path (GtkFileChooserDefault *impl)
1932 struct get_selected_path_closure closure;
1933 GtkTreeSelection *selection;
1935 closure.impl = impl;
1936 closure.path = NULL;
1938 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1939 gtk_tree_selection_selected_foreach (selection,
1940 get_selected_path_foreach_cb,
1943 return closure.path;
1946 /* Sensitize the "add bookmark" button if all the selected items are folders, or
1947 * if there are no selected items *and* the current folder is not in the
1948 * bookmarks list. De-sensitize the button otherwise.
1951 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1954 gboolean all_folders;
1957 selection_check (impl, &num_selected, NULL, &all_folders);
1959 if (num_selected == 0)
1960 active = (shortcut_find_position (impl, impl->current_folder) == -1);
1961 else if (num_selected == 1)
1963 const GtkFilePath *path;
1965 path = get_selected_path (impl);
1966 active = all_folders && (shortcut_find_position (impl, path) == -1);
1969 active = all_folders;
1971 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
1973 if (impl->browse_files_popup_menu_add_shortcut_item)
1974 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
1975 (num_selected == 0) ? FALSE : active);
1978 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
1979 * bookmark row is selected in the shortcuts tree.
1982 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
1985 gboolean removable = FALSE;
1987 if (shortcuts_get_selected (impl, &iter))
1988 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1989 SHORTCUTS_COL_REMOVABLE, &removable,
1992 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
1995 /* GtkWidget::drag-begin handler for the shortcuts list. */
1997 shortcuts_drag_begin_cb (GtkWidget *widget,
1998 GdkDragContext *context,
1999 GtkFileChooserDefault *impl)
2002 impl->shortcuts_drag_context = g_object_ref (context);
2007 /* Removes the idle handler for outside drags */
2009 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2011 if (!impl->shortcuts_drag_outside_idle)
2014 g_source_destroy (impl->shortcuts_drag_outside_idle);
2015 impl->shortcuts_drag_outside_idle = NULL;
2019 /* GtkWidget::drag-end handler for the shortcuts list. */
2021 shortcuts_drag_end_cb (GtkWidget *widget,
2022 GdkDragContext *context,
2023 GtkFileChooserDefault *impl)
2026 g_object_unref (impl->shortcuts_drag_context);
2028 shortcuts_cancel_drag_outside_idle (impl);
2030 if (!impl->shortcuts_drag_outside)
2033 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2035 impl->shortcuts_drag_outside = FALSE;
2039 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2041 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2042 GdkDragContext *context,
2043 GtkFileChooserDefault *impl)
2045 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2049 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2053 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2056 GtkTreeView *tree_view;
2059 GdkPixmap *row_pixmap;
2064 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2066 /* Find the selected path and get its drag pixmap */
2068 if (!shortcuts_get_selected (impl, &iter))
2069 g_assert_not_reached ();
2071 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2073 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2074 gtk_tree_path_free (path);
2083 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2089 GdkPixmap *composite;
2090 int row_pixmap_width, row_pixmap_height;
2091 int pixbuf_width, pixbuf_height;
2092 int composite_width, composite_height;
2093 int pixbuf_x, pixbuf_y;
2094 GdkGC *gc, *mask_gc;
2096 GdkBitmap *pixbuf_mask;
2098 /* Create pixmap and mask for composite image */
2100 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2101 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2102 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2104 composite_width = MAX (row_pixmap_width, pixbuf_width);
2105 composite_height = MAX (row_pixmap_height, pixbuf_height);
2107 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2109 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2112 pixbuf_x = composite_width - pixbuf_width;
2114 pixbuf_y = (composite_height - pixbuf_height) / 2;
2116 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2117 gc = gdk_gc_new (composite);
2119 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2120 mask_gc = gdk_gc_new (mask);
2122 gdk_gc_set_foreground (mask_gc, &color);
2123 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2126 color.green = 0xffff;
2127 color.blue = 0xffff;
2128 gdk_gc_set_rgb_fg_color (gc, &color);
2129 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2131 /* Composite the row pixmap and the pixbuf */
2133 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2135 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2136 NULL, &pixbuf_mask, 128);
2137 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2140 pixbuf_width, pixbuf_height);
2141 g_object_unref (pixbuf_mask);
2143 gdk_draw_drawable (composite, gc, row_pixmap,
2146 row_pixmap_width, row_pixmap_height);
2148 gdk_gc_set_foreground (mask_gc, &color);
2149 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2151 gdk_draw_pixbuf (composite, gc, pixbuf,
2154 pixbuf_width, pixbuf_height,
2158 g_object_unref (pixbuf);
2159 g_object_unref (row_pixmap);
2161 row_pixmap = composite;
2165 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2167 gtk_tree_view_get_path_at_pos (tree_view,
2168 tree_view->priv->press_start_x,
2169 tree_view->priv->press_start_y,
2175 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2176 gdk_drawable_get_colormap (row_pixmap),
2179 tree_view->priv->press_start_x + 1,
2180 row_pixmap_y + cell_y + 1);
2182 g_object_unref (row_pixmap);
2184 g_object_unref (mask);
2187 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2188 * handler so that we can tell apart the drag_leave event that comes right
2189 * before a drag_drop, from a normal drag_leave. We don't want to set the
2190 * cursor nor the flag in the latter case.
2193 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2195 shortcuts_drag_set_delete_cursor (impl, TRUE);
2196 impl->shortcuts_drag_outside = TRUE;
2198 shortcuts_cancel_drag_outside_idle (impl);
2203 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2207 shortcuts_drag_leave_cb (GtkWidget *widget,
2208 GdkDragContext *context,
2210 GtkFileChooserDefault *impl)
2213 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2215 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2216 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2217 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2219 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2223 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2225 GTK_TREE_VIEW_DROP_BEFORE);
2227 g_signal_stop_emission_by_name (widget, "drag-leave");
2230 /* Computes the appropriate row and position for dropping */
2232 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2236 GtkTreeViewDropPosition *pos)
2238 GtkTreeView *tree_view;
2239 GtkTreeViewColumn *column;
2243 int bookmarks_index;
2245 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2247 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2249 if (!gtk_tree_view_get_path_at_pos (tree_view,
2251 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2257 row = bookmarks_index + impl->num_bookmarks - 1;
2258 *path = gtk_tree_path_new_from_indices (row, -1);
2259 *pos = GTK_TREE_VIEW_DROP_AFTER;
2263 row = *gtk_tree_path_get_indices (*path);
2264 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2265 gtk_tree_path_free (*path);
2267 if (row < bookmarks_index)
2269 row = bookmarks_index;
2270 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2272 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2274 row = bookmarks_index + impl->num_bookmarks - 1;
2275 *pos = GTK_TREE_VIEW_DROP_AFTER;
2279 if (cell_y < cell.height / 2)
2280 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2282 *pos = GTK_TREE_VIEW_DROP_AFTER;
2285 *path = gtk_tree_path_new_from_indices (row, -1);
2288 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2289 * implement the destination side of DnD by hand, due to limitations in
2290 * GtkTreeView's DnD API.
2293 shortcuts_drag_motion_cb (GtkWidget *widget,
2294 GdkDragContext *context,
2298 GtkFileChooserDefault *impl)
2301 GtkTreeViewDropPosition pos;
2302 GdkDragAction action;
2305 if (gtk_drag_get_source_widget (context) == widget)
2307 shortcuts_cancel_drag_outside_idle (impl);
2309 if (impl->shortcuts_drag_outside)
2311 shortcuts_drag_set_delete_cursor (impl, FALSE);
2312 impl->shortcuts_drag_outside = FALSE;
2317 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2318 action = GDK_ACTION_COPY;
2319 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2320 action = GDK_ACTION_MOVE;
2327 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2328 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2329 gtk_tree_path_free (path);
2333 g_signal_stop_emission_by_name (widget, "drag-motion");
2337 gdk_drag_status (context, action, time_);
2344 /* GtkWidget::drag-drop handler for the shortcuts list. */
2346 shortcuts_drag_drop_cb (GtkWidget *widget,
2347 GdkDragContext *context,
2351 GtkFileChooserDefault *impl)
2354 shortcuts_cancel_drag_outside_idle (impl);
2357 g_signal_stop_emission_by_name (widget, "drag-drop");
2361 /* Converts raw selection data from text/uri-list to a list of strings. */
2363 split_uris (const char *data)
2372 /* We don't actually try to validate the URI according to RFC
2373 * 2396, or even check for allowed characters - we just ignore
2374 * comments and trim whitespace off the ends. We also
2375 * allow LF delimination as well as the specified CRLF.
2377 * We do allow comments like specified in RFC 2483.
2383 while (g_ascii_isspace (*p))
2387 while (*q && (*q != '\n') && (*q != '\r'))
2393 while (q > p && g_ascii_isspace (*q))
2397 uris = g_slist_prepend (uris, g_strndup (p, q - p + 1));
2400 p = strchr (p, '\n');
2405 uris = g_slist_reverse (uris);
2409 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2411 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2417 uris = split_uris (data);
2419 for (l = uris; l; l = l->next)
2425 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2429 if (shortcuts_add_bookmark_from_path (impl, path, position))
2432 gtk_file_path_free (path);
2438 msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
2440 error_message (impl, msg);
2447 g_slist_free (uris);
2450 /* Reorders the selected bookmark to the specified position */
2452 shortcuts_reorder (GtkFileChooserDefault *impl,
2458 int bookmarks_index;
2459 const GtkFilePath *file_path;
2460 GtkFilePath *file_path_copy;
2463 /* Get the selected path */
2465 if (!shortcuts_get_selected (impl, &iter))
2466 g_assert_not_reached ();
2468 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2469 old_position = *gtk_tree_path_get_indices (path);
2470 gtk_tree_path_free (path);
2472 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2473 old_position -= bookmarks_index;
2474 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2476 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2477 SHORTCUTS_COL_PATH, &file_path,
2479 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2481 /* Remove the path from the old position and insert it in the new one */
2483 if (new_position > old_position)
2486 if (old_position == new_position)
2490 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2491 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2493 error_could_not_add_bookmark_dialog (impl, file_path_copy, error);
2495 gtk_file_path_free (file_path_copy);
2498 /* Callback used when we get the drag data for the bookmarks list. We add the
2499 * received URIs as bookmarks if they are folders.
2502 shortcuts_drag_data_received_cb (GtkWidget *widget,
2503 GdkDragContext *context,
2506 GtkSelectionData *selection_data,
2511 GtkFileChooserDefault *impl;
2512 GtkTreePath *tree_path;
2513 GtkTreeViewDropPosition tree_pos;
2515 int bookmarks_index;
2517 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2519 /* Compute position */
2521 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2523 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2524 position = *gtk_tree_path_get_indices (tree_path);
2525 gtk_tree_path_free (tree_path);
2527 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2530 g_assert (position >= bookmarks_index);
2531 position -= bookmarks_index;
2533 if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2534 shortcuts_drop_uris (impl, selection_data->data, position);
2535 else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2536 shortcuts_reorder (impl, position);
2538 g_signal_stop_emission_by_name (widget, "drag-data-received");
2541 /* Callback used when the selection in the shortcuts tree changes */
2543 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
2544 GtkFileChooserDefault *impl)
2546 bookmarks_check_remove_sensitivity (impl);
2549 /* Creates the widgets for the shortcuts and bookmarks tree */
2551 shortcuts_list_create (GtkFileChooserDefault *impl)
2554 GtkTreeSelection *selection;
2555 GtkTreeViewColumn *column;
2556 GtkCellRenderer *renderer;
2558 /* Scrolled window */
2560 swin = gtk_scrolled_window_new (NULL, NULL);
2561 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2562 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2563 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2565 gtk_widget_show (swin);
2569 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
2570 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Shortcuts"));
2571 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
2573 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
2575 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2577 shortcuts_source_targets,
2578 num_shortcuts_source_targets,
2581 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
2582 GTK_DEST_DEFAULT_ALL,
2583 shortcuts_dest_targets,
2584 num_shortcuts_dest_targets,
2585 GDK_ACTION_COPY | GDK_ACTION_MOVE);
2587 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2588 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
2589 gtk_tree_selection_set_select_function (selection,
2590 shortcuts_select_func,
2593 g_signal_connect (selection, "changed",
2594 G_CALLBACK (shortcuts_selection_changed_cb), impl);
2596 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
2597 G_CALLBACK (shortcuts_row_activated_cb), impl);
2599 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2600 G_CALLBACK (shortcuts_key_press_event_cb), impl);
2602 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
2603 G_CALLBACK (shortcuts_drag_begin_cb), impl);
2604 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
2605 G_CALLBACK (shortcuts_drag_end_cb), impl);
2606 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
2607 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
2609 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
2610 G_CALLBACK (shortcuts_drag_leave_cb), impl);
2611 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
2612 G_CALLBACK (shortcuts_drag_motion_cb), impl);
2613 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
2614 G_CALLBACK (shortcuts_drag_drop_cb), impl);
2615 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
2616 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
2618 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
2619 gtk_widget_show (impl->browse_shortcuts_tree_view);
2623 column = gtk_tree_view_column_new ();
2624 gtk_tree_view_column_set_title (column, _("Folder"));
2626 renderer = gtk_cell_renderer_pixbuf_new ();
2627 gtk_tree_view_column_pack_start (column, renderer, FALSE);
2628 gtk_tree_view_column_set_attributes (column, renderer,
2629 "pixbuf", SHORTCUTS_COL_PIXBUF,
2630 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2633 renderer = _gtk_cell_renderer_sep_text_new ();
2634 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2635 gtk_tree_view_column_set_attributes (column, renderer,
2636 "text", SHORTCUTS_COL_NAME,
2639 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
2644 /* Creates the widgets for the shortcuts/bookmarks pane */
2646 shortcuts_pane_create (GtkFileChooserDefault *impl,
2647 GtkSizeGroup *size_group)
2653 vbox = gtk_vbox_new (FALSE, 6);
2654 gtk_widget_show (vbox);
2656 /* Shortcuts tree */
2658 widget = shortcuts_list_create (impl);
2659 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
2661 /* Box for buttons */
2663 hbox = gtk_hbox_new (TRUE, 6);
2664 gtk_size_group_add_widget (size_group, hbox);
2665 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2666 gtk_widget_show (hbox);
2668 /* Add bookmark button */
2670 impl->browse_shortcuts_add_button = button_new (impl,
2675 G_CALLBACK (add_bookmark_button_clicked_cb));
2676 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
2678 /* Remove bookmark button */
2680 impl->browse_shortcuts_remove_button = button_new (impl,
2685 G_CALLBACK (remove_bookmark_button_clicked_cb));
2686 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
2691 /* Handles key press events on the file list, so that we can trap Enter to
2692 * activate the default button on our own.
2695 trap_activate_cb (GtkWidget *widget,
2699 GtkFileChooserDefault *impl;
2701 impl = (GtkFileChooserDefault *) data;
2703 if (event->keyval == GDK_Return
2704 || event->keyval == GDK_ISO_Enter
2705 || event->keyval == GDK_KP_Enter
2706 || event->keyval == GDK_space)
2710 window = get_toplevel (widget);
2712 && widget != window->default_widget
2713 && !(widget == window->focus_widget &&
2714 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
2716 gtk_window_activate_default (window);
2724 /* Callback used when the file list's popup menu is detached */
2726 popup_menu_detach_cb (GtkWidget *attach_widget,
2729 GtkFileChooserDefault *impl;
2731 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
2732 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
2734 impl->browse_files_popup_menu = NULL;
2735 impl->browse_files_popup_menu_add_shortcut_item = NULL;
2736 impl->browse_files_popup_menu_hidden_files_item = NULL;
2739 /* Callback used when the "Add to Shortcuts" menu item is activated */
2741 add_to_shortcuts_cb (GtkMenuItem *item,
2742 GtkFileChooserDefault *impl)
2744 bookmarks_add_selected_folder (impl);
2747 /* Callback used when the "Show Hidden Files" menu item is toggled */
2749 show_hidden_toggled_cb (GtkCheckMenuItem *item,
2750 GtkFileChooserDefault *impl)
2753 "show-hidden", gtk_check_menu_item_get_active (item),
2757 /* Constructs the popup menu for the file list if needed */
2759 file_list_build_popup_menu (GtkFileChooserDefault *impl)
2763 if (impl->browse_files_popup_menu)
2766 impl->browse_files_popup_menu = gtk_menu_new ();
2767 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
2768 impl->browse_files_tree_view,
2769 popup_menu_detach_cb);
2771 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Shortcuts"));
2772 impl->browse_files_popup_menu_add_shortcut_item = item;
2773 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2774 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
2775 gtk_widget_set_sensitive (item, FALSE);
2776 g_signal_connect (item, "activate",
2777 G_CALLBACK (add_to_shortcuts_cb), impl);
2778 gtk_widget_show (item);
2779 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2781 item = gtk_separator_menu_item_new ();
2782 gtk_widget_show (item);
2783 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2785 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
2786 impl->browse_files_popup_menu_hidden_files_item = item;
2787 g_signal_connect (item, "toggled",
2788 G_CALLBACK (show_hidden_toggled_cb), impl);
2789 gtk_widget_show (item);
2790 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2793 /* Updates the popup menu for the file list, creating it if necessary */
2795 file_list_update_popup_menu (GtkFileChooserDefault *impl)
2797 file_list_build_popup_menu (impl);
2799 /* The sensitivity of the Add to Shortcuts item is set in
2800 * bookmarks_check_add_sensitivity()
2803 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
2804 G_CALLBACK (show_hidden_toggled_cb), impl);
2805 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
2807 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
2808 G_CALLBACK (show_hidden_toggled_cb), impl);
2812 popup_position_func (GtkMenu *menu,
2818 GtkWidget *widget = GTK_WIDGET (user_data);
2819 GdkScreen *screen = gtk_widget_get_screen (widget);
2822 GdkRectangle monitor;
2824 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
2826 gdk_window_get_origin (widget->window, x, y);
2828 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2830 *x += (widget->allocation.width - req.width) / 2;
2831 *y += (widget->allocation.height - req.height) / 2;
2833 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
2834 gtk_menu_set_monitor (menu, monitor_num);
2835 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2837 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
2838 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
2844 file_list_popup_menu (GtkFileChooserDefault *impl,
2845 GdkEventButton *event)
2847 file_list_update_popup_menu (impl);
2849 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
2850 NULL, NULL, NULL, NULL,
2851 event->button, event->time);
2854 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
2856 popup_position_func, impl->browse_files_tree_view,
2857 0, GDK_CURRENT_TIME);
2858 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
2864 /* Callback used for the GtkWidget::popup-menu signal of the file list */
2866 list_popup_menu_cb (GtkWidget *widget,
2867 GtkFileChooserDefault *impl)
2869 file_list_popup_menu (impl, NULL);
2873 /* Callback used when a button is pressed on the file list. We trap button 3 to
2874 * bring up a popup menu.
2877 list_button_press_event_cb (GtkWidget *widget,
2878 GdkEventButton *event,
2879 GtkFileChooserDefault *impl)
2881 if (event->button != 3)
2884 file_list_popup_menu (impl, event);
2888 /* Creates the widgets for the file list */
2890 create_file_list (GtkFileChooserDefault *impl)
2893 GtkTreeSelection *selection;
2894 GtkTreeViewColumn *column;
2895 GtkCellRenderer *renderer;
2897 /* Scrolled window */
2899 swin = gtk_scrolled_window_new (NULL, NULL);
2900 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2901 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2902 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2905 /* Tree/list view */
2907 impl->browse_files_tree_view = gtk_tree_view_new ();
2908 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "GtkFileChooserDefault", impl);
2909 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
2911 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
2912 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
2913 g_signal_connect (impl->browse_files_tree_view, "row-activated",
2914 G_CALLBACK (list_row_activated), impl);
2915 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
2916 G_CALLBACK (trap_activate_cb), impl);
2917 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
2918 G_CALLBACK (list_popup_menu_cb), impl);
2919 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
2920 G_CALLBACK (list_button_press_event_cb), impl);
2922 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2923 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
2925 file_list_source_targets,
2926 num_file_list_source_targets,
2929 g_signal_connect (selection, "changed",
2930 G_CALLBACK (list_selection_changed), impl);
2932 /* Filename column */
2934 impl->list_name_column = gtk_tree_view_column_new ();
2935 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
2936 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
2937 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
2938 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
2940 renderer = gtk_cell_renderer_pixbuf_new ();
2941 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
2942 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
2943 list_icon_data_func, impl, NULL);
2945 impl->list_name_renderer = gtk_cell_renderer_text_new ();
2946 g_object_set (impl->list_name_renderer,
2947 "ellipsize", PANGO_ELLIPSIZE_END,
2949 g_signal_connect (impl->list_name_renderer, "edited",
2950 G_CALLBACK (renderer_edited_cb), impl);
2951 g_signal_connect (impl->list_name_renderer, "editing-canceled",
2952 G_CALLBACK (renderer_editing_canceled_cb), impl);
2953 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
2954 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
2955 list_name_data_func, impl, NULL);
2957 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
2961 column = gtk_tree_view_column_new ();
2962 gtk_tree_view_column_set_title (column, _("Size"));
2964 renderer = gtk_cell_renderer_text_new ();
2965 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2966 gtk_tree_view_column_set_cell_data_func (column, renderer,
2967 list_size_data_func, impl, NULL);
2968 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
2969 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
2971 /* Modification time column */
2973 column = gtk_tree_view_column_new ();
2974 gtk_tree_view_column_set_resizable (column, TRUE);
2975 gtk_tree_view_column_set_title (column, _("Modified"));
2977 renderer = gtk_cell_renderer_text_new ();
2978 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2979 gtk_tree_view_column_set_cell_data_func (column, renderer,
2980 list_mtime_data_func, impl, NULL);
2981 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
2982 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
2983 gtk_widget_show_all (swin);
2989 create_path_bar (GtkFileChooserDefault *impl)
2991 GtkWidget *path_bar;
2993 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
2994 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
2999 /* Creates the widgets for the files/folders pane */
3001 file_pane_create (GtkFileChooserDefault *impl,
3002 GtkSizeGroup *size_group)
3008 vbox = gtk_vbox_new (FALSE, 6);
3009 gtk_widget_show (vbox);
3011 /* The path bar and 'Create Folder' button */
3012 hbox = gtk_hbox_new (FALSE, 12);
3013 gtk_widget_show (hbox);
3014 impl->browse_path_bar = create_path_bar (impl);
3015 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
3016 gtk_widget_show_all (impl->browse_path_bar);
3017 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
3020 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
3021 g_signal_connect (impl->browse_new_folder_button, "clicked",
3022 G_CALLBACK (new_folder_button_clicked), impl);
3023 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
3024 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3026 /* Box for lists and preview */
3028 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
3029 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
3030 gtk_widget_show (hbox);
3034 widget = create_file_list (impl);
3035 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
3039 impl->preview_box = gtk_vbox_new (FALSE, 12);
3040 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
3041 /* Don't show preview box initially */
3045 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
3047 widget = filter_create (impl);
3048 gtk_widget_show (widget);
3049 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
3051 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
3052 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
3056 /* Callback used when the "Browse for more folders" expander is toggled */
3058 expander_changed_cb (GtkExpander *expander,
3060 GtkFileChooserDefault *impl)
3062 update_appearance (impl);
3065 /* Callback used when the selection changes in the save folder combo box */
3067 save_folder_combo_changed_cb (GtkComboBox *combo,
3068 GtkFileChooserDefault *impl)
3072 if (impl->changing_folder)
3075 if (gtk_combo_box_get_active_iter (combo, &iter))
3076 shortcuts_activate_iter (impl, &iter);
3079 /* Creates the combo box with the save folders */
3081 save_folder_combo_create (GtkFileChooserDefault *impl)
3084 GtkCellRenderer *cell;
3086 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
3087 gtk_widget_show (combo);
3089 cell = gtk_cell_renderer_pixbuf_new ();
3090 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3091 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3092 "pixbuf", SHORTCUTS_COL_PIXBUF,
3093 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3094 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3097 cell = _gtk_cell_renderer_sep_text_new ();
3098 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3099 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3100 "text", SHORTCUTS_COL_NAME,
3101 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3104 g_signal_connect (combo, "changed",
3105 G_CALLBACK (save_folder_combo_changed_cb), impl);
3110 /* Creates the widgets specific to Save mode */
3112 save_widgets_create (GtkFileChooserDefault *impl)
3117 GtkWidget *alignment;
3119 vbox = gtk_vbox_new (FALSE, 12);
3121 table = gtk_table_new (2, 2, FALSE);
3122 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3123 gtk_widget_show (table);
3124 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3125 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3129 widget = gtk_label_new_with_mnemonic (_("_Name:"));
3130 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3131 gtk_table_attach (GTK_TABLE (table), widget,
3135 gtk_widget_show (widget);
3137 impl->save_file_name_entry = gtk_entry_new ();
3138 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3139 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3140 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3142 GTK_EXPAND | GTK_FILL, 0,
3144 gtk_widget_show (impl->save_file_name_entry);
3145 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
3148 impl->save_folder_label = gtk_label_new (NULL);
3149 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
3150 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
3154 gtk_widget_show (impl->save_folder_label);
3156 impl->save_folder_combo = save_folder_combo_create (impl);
3157 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
3159 GTK_EXPAND | GTK_FILL, GTK_FILL,
3161 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
3164 impl->save_extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3165 gtk_box_pack_start (GTK_BOX (vbox), impl->save_extra_align, FALSE, FALSE, 0);
3168 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3169 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
3171 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
3172 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
3173 g_signal_connect (impl->save_expander, "notify::expanded",
3174 G_CALLBACK (expander_changed_cb),
3176 gtk_widget_show_all (alignment);
3181 /* Creates the main hpaned with the widgets shared by Open and Save mode */
3183 browse_widgets_create (GtkFileChooserDefault *impl)
3188 GtkSizeGroup *size_group;
3190 /* size group is used by the [+][-] buttons and the filter combo */
3191 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
3192 vbox = gtk_vbox_new (FALSE, 12);
3195 hpaned = gtk_hpaned_new ();
3196 gtk_widget_show (hpaned);
3197 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
3198 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
3200 widget = shortcuts_pane_create (impl, size_group);
3201 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
3202 widget = file_pane_create (impl, size_group);
3203 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
3205 g_object_unref (size_group);
3207 /* Alignment to hold custom widget */
3208 impl->browse_extra_align = gtk_alignment_new (0.0, .5, 1.0, 1.0);
3209 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_extra_align, FALSE, FALSE, 0);
3215 gtk_file_chooser_default_constructor (GType type,
3216 guint n_construct_properties,
3217 GObjectConstructParam *construct_params)
3219 GtkFileChooserDefault *impl;
3222 object = parent_class->constructor (type,
3223 n_construct_properties,
3225 impl = GTK_FILE_CHOOSER_DEFAULT (object);
3227 g_assert (impl->file_system);
3229 gtk_widget_push_composite_child ();
3231 /* Shortcuts model */
3233 shortcuts_model_create (impl);
3235 /* Widgets for Save mode */
3236 impl->save_widgets = save_widgets_create (impl);
3237 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
3239 /* The browse widgets */
3240 impl->browse_widgets = browse_widgets_create (impl);
3241 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
3243 gtk_widget_pop_composite_child ();
3244 update_appearance (impl);
3249 /* Sets the extra_widget by packing it in the appropriate place */
3251 set_extra_widget (GtkFileChooserDefault *impl,
3252 GtkWidget *extra_widget)
3256 g_object_ref (extra_widget);
3257 /* FIXME: is this right ? */
3258 gtk_widget_show (extra_widget);
3261 if (impl->extra_widget)
3262 g_object_unref (impl->extra_widget);
3264 impl->extra_widget = extra_widget;
3268 set_local_only (GtkFileChooserDefault *impl,
3269 gboolean local_only)
3271 if (local_only != impl->local_only)
3273 impl->local_only = local_only;
3275 if (impl->shortcuts_model && impl->file_system)
3277 shortcuts_add_volumes (impl);
3278 shortcuts_add_bookmarks (impl);
3282 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
3284 /* If we are pointing to a non-local folder, make an effort to change
3285 * back to a local folder, but it's really up to the app to not cause
3286 * such a situation, so we ignore errors.
3288 const gchar *home = g_get_home_dir ();
3289 GtkFilePath *home_path = gtk_file_system_filename_to_path (impl->file_system, home);
3291 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
3293 gtk_file_path_free (home_path);
3299 volumes_changed_cb (GtkFileSystem *file_system,
3300 GtkFileChooserDefault *impl)
3302 shortcuts_add_volumes (impl);
3305 /* Callback used when the set of bookmarks changes in the file system */
3307 bookmarks_changed_cb (GtkFileSystem *file_system,
3308 GtkFileChooserDefault *impl)
3310 shortcuts_add_bookmarks (impl);
3312 bookmarks_check_add_sensitivity (impl);
3313 bookmarks_check_remove_sensitivity (impl);
3316 /* Sets the file chooser to multiple selection mode */
3318 set_select_multiple (GtkFileChooserDefault *impl,
3319 gboolean select_multiple,
3320 gboolean property_notify)
3322 GtkTreeSelection *selection;
3323 GtkSelectionMode mode;
3325 if (select_multiple == impl->select_multiple)
3328 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3330 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3331 gtk_tree_selection_set_mode (selection, mode);
3333 impl->select_multiple = select_multiple;
3334 g_object_notify (G_OBJECT (impl), "select-multiple");
3336 check_preview_change (impl);
3340 set_file_system_backend (GtkFileChooserDefault *impl,
3341 const char *backend)
3343 if (impl->file_system)
3345 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3346 impl->volumes_changed_id = 0;
3347 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3348 impl->bookmarks_changed_id = 0;
3349 g_object_unref (impl->file_system);
3352 impl->file_system = NULL;
3354 impl->file_system = _gtk_file_system_create (backend);
3357 GtkSettings *settings = gtk_settings_get_default ();
3358 gchar *default_backend = NULL;
3360 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3361 if (default_backend)
3363 impl->file_system = _gtk_file_system_create (default_backend);
3364 g_free (default_backend);
3368 if (!impl->file_system)
3370 #if defined (G_OS_UNIX)
3371 impl->file_system = gtk_file_system_unix_new ();
3372 #elif defined (G_OS_WIN32)
3373 impl->file_system = gtk_file_system_win32_new ();
3375 #error "No default filesystem implementation on the platform"
3379 if (impl->file_system)
3381 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3382 G_CALLBACK (volumes_changed_cb),
3384 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3385 G_CALLBACK (bookmarks_changed_cb),
3390 /* This function is basically a do_all function.
3392 * It sets the visibility on all the widgets based on the current state, and
3393 * moves the custom_widget if needed.
3396 update_appearance (GtkFileChooserDefault *impl)
3400 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3401 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3405 gtk_widget_show (impl->save_widgets);
3407 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3408 text = _("Save in _folder:");
3410 text = _("Create in _folder:");
3412 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
3414 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3416 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
3417 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
3418 gtk_widget_show (impl->browse_widgets);
3422 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
3423 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
3424 gtk_widget_hide (impl->browse_widgets);
3427 gtk_widget_show (impl->browse_new_folder_button);
3429 if (impl->select_multiple)
3431 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
3432 "Re-setting to single selection mode.");
3433 set_select_multiple (impl, FALSE, TRUE);
3436 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3437 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
3439 gtk_widget_hide (impl->save_widgets);
3440 gtk_widget_show (impl->browse_widgets);
3443 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3444 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3446 if (impl->browse_files_model)
3447 _gtk_file_system_model_set_show_files (impl->browse_files_model, FALSE);
3451 if (impl->browse_files_model)
3452 _gtk_file_system_model_set_show_files (impl->browse_files_model, TRUE);
3455 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
3456 gtk_widget_hide (impl->browse_new_folder_button);
3458 gtk_widget_show (impl->browse_new_folder_button);
3460 if (impl->extra_widget)
3463 GtkWidget *unused_align;
3465 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
3466 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3468 align = impl->save_extra_align;
3469 unused_align = impl->browse_extra_align;
3473 align = impl->browse_extra_align;
3474 unused_align = impl->save_extra_align;
3477 /* We own a ref on extra_widget, so it's safe to do this */
3478 child = GTK_BIN (unused_align)->child;
3480 gtk_container_remove (GTK_CONTAINER (unused_align), child);
3482 child = GTK_BIN (align)->child;
3483 if (child && child != impl->extra_widget)
3485 gtk_container_remove (GTK_CONTAINER (align), child);
3486 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
3488 else if (child == NULL)
3490 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
3493 gtk_widget_show (align);
3494 gtk_widget_hide (unused_align);
3498 child = GTK_BIN (impl->browse_extra_align)->child;
3500 gtk_container_remove (GTK_CONTAINER (impl->browse_extra_align), child);
3502 child = GTK_BIN (impl->save_extra_align)->child;
3504 gtk_container_remove (GTK_CONTAINER (impl->save_extra_align), child);
3506 gtk_widget_hide (impl->save_extra_align);
3507 gtk_widget_hide (impl->browse_extra_align);
3510 g_signal_emit_by_name (impl, "default-size-changed");
3514 gtk_file_chooser_default_set_property (GObject *object,
3516 const GValue *value,
3520 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3524 case GTK_FILE_CHOOSER_PROP_ACTION:
3526 GtkFileChooserAction action = g_value_get_enum (value);
3528 if (action != impl->action)
3530 if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
3532 g_warning ("Multiple selection mode is not allowed in Save mode");
3533 set_select_multiple (impl, FALSE, TRUE);
3535 impl->action = action;
3536 update_appearance (impl);
3540 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
3541 set_file_system_backend (impl, g_value_get_string (value));
3543 case GTK_FILE_CHOOSER_PROP_FILTER:
3544 set_current_filter (impl, g_value_get_object (value));
3546 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3547 set_local_only (impl, g_value_get_boolean (value));
3549 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3550 set_preview_widget (impl, g_value_get_object (value));
3552 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3553 impl->preview_widget_active = g_value_get_boolean (value);
3554 update_preview_widget_visibility (impl);
3556 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3557 impl->use_preview_label = g_value_get_boolean (value);
3558 update_preview_widget_visibility (impl);
3560 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3561 set_extra_widget (impl, g_value_get_object (value));
3562 update_appearance (impl);
3564 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3566 gboolean select_multiple = g_value_get_boolean (value);
3567 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
3569 g_warning ("Multiple selection mode is not allowed in Save mode");
3573 set_select_multiple (impl, select_multiple, FALSE);
3576 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3578 gboolean show_hidden = g_value_get_boolean (value);
3579 if (show_hidden != impl->show_hidden)
3581 impl->show_hidden = show_hidden;
3583 if (impl->browse_files_model)
3584 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
3589 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3595 gtk_file_chooser_default_get_property (GObject *object,
3600 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3604 case GTK_FILE_CHOOSER_PROP_ACTION:
3605 g_value_set_enum (value, impl->action);
3607 case GTK_FILE_CHOOSER_PROP_FILTER:
3608 g_value_set_object (value, impl->current_filter);
3610 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3611 g_value_set_boolean (value, impl->local_only);
3613 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3614 g_value_set_object (value, impl->preview_widget);
3616 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3617 g_value_set_boolean (value, impl->preview_widget_active);
3619 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3620 g_value_set_boolean (value, impl->use_preview_label);
3622 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3623 g_value_set_object (value, impl->extra_widget);
3625 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3626 g_value_set_boolean (value, impl->select_multiple);
3628 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3629 g_value_set_boolean (value, impl->show_hidden);
3632 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3637 /* Removes the settings signal handler. It's safe to call multiple times */
3639 remove_settings_signal (GtkFileChooserDefault *impl,
3642 if (impl->settings_signal_id)
3644 GtkSettings *settings;
3646 settings = gtk_settings_get_for_screen (screen);
3647 g_signal_handler_disconnect (settings,
3648 impl->settings_signal_id);
3649 impl->settings_signal_id = 0;
3654 gtk_file_chooser_default_dispose (GObject *object)
3656 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
3658 if (impl->extra_widget)
3660 g_object_unref (impl->extra_widget);
3661 impl->extra_widget = NULL;
3664 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
3666 G_OBJECT_CLASS (parent_class)->dispose (object);
3669 /* We override show-all since we have internal widgets that
3670 * shouldn't be shown when you call show_all(), like the filter
3674 gtk_file_chooser_default_show_all (GtkWidget *widget)
3676 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
3678 gtk_widget_show (widget);
3680 if (impl->extra_widget)
3681 gtk_widget_show_all (impl->extra_widget);
3684 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
3685 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
3688 toplevel_set_focus_cb (GtkWindow *window,
3690 GtkFileChooserDefault *impl)
3692 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
3695 /* We monitor the focus widget on our toplevel to be able to know which widget
3696 * was last focused at the time our "should_respond" method gets called.
3699 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
3700 GtkWidget *previous_toplevel)
3702 GtkFileChooserDefault *impl;
3703 GtkWidget *toplevel;
3705 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3707 if (previous_toplevel)
3709 g_assert (impl->toplevel_set_focus_id != 0);
3710 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
3711 impl->toplevel_set_focus_id = 0;
3712 impl->toplevel_last_focus_widget = NULL;
3715 g_assert (impl->toplevel_set_focus_id == 0);
3717 toplevel = gtk_widget_get_toplevel (widget);
3718 if (GTK_IS_WINDOW (toplevel))
3720 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
3721 G_CALLBACK (toplevel_set_focus_cb), impl);
3722 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
3726 /* Changes the icons wherever it is needed */
3728 change_icon_theme (GtkFileChooserDefault *impl)
3730 GtkSettings *settings;
3733 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3735 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_SMALL_TOOLBAR, &width, &height))
3736 impl->icon_size = MAX (width, height);
3738 impl->icon_size = FALLBACK_ICON_SIZE;
3740 shortcuts_reload_icons (impl);
3741 gtk_widget_queue_resize (impl->browse_files_tree_view);
3744 /* Callback used when a GtkSettings value changes */
3746 settings_notify_cb (GObject *object,
3748 GtkFileChooserDefault *impl)
3752 name = g_param_spec_get_name (pspec);
3754 if (strcmp (name, "gtk-icon-theme-name") == 0
3755 || strcmp (name, "gtk-icon-sizes") == 0)
3756 change_icon_theme (impl);
3759 /* Installs a signal handler for GtkSettings so that we can monitor changes in
3763 check_icon_theme (GtkFileChooserDefault *impl)
3765 GtkSettings *settings;
3767 if (impl->settings_signal_id)
3770 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
3772 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3773 impl->settings_signal_id = g_signal_connect (settings, "notify",
3774 G_CALLBACK (settings_notify_cb), impl);
3776 change_icon_theme (impl);
3781 gtk_file_chooser_default_style_set (GtkWidget *widget,
3782 GtkStyle *previous_style)
3784 GtkFileChooserDefault *impl;
3786 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3788 if (GTK_WIDGET_CLASS (parent_class)->style_set)
3789 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
3791 check_icon_theme (impl);
3793 g_signal_emit_by_name (widget, "default-size-changed");
3797 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
3798 GdkScreen *previous_screen)
3800 GtkFileChooserDefault *impl;
3802 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3804 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
3805 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
3807 remove_settings_signal (impl, previous_screen);
3808 check_icon_theme (impl);
3810 g_signal_emit_by_name (widget, "default-size-changed");
3814 list_model_filter_func (GtkFileSystemModel *model,
3816 const GtkFileInfo *file_info,
3819 GtkFileChooserDefault *impl = user_data;
3820 GtkFileFilterInfo filter_info;
3821 GtkFileFilterFlags needed;
3824 if (!impl->current_filter)
3827 if (gtk_file_info_get_is_folder (file_info))
3830 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
3832 needed = gtk_file_filter_get_needed (impl->current_filter);
3834 filter_info.display_name = gtk_file_info_get_display_name (file_info);
3835 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
3837 if (needed & GTK_FILE_FILTER_FILENAME)
3839 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
3840 if (filter_info.filename)
3841 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
3844 filter_info.filename = NULL;
3846 if (needed & GTK_FILE_FILTER_URI)
3848 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
3849 if (filter_info.uri)
3850 filter_info.contains |= GTK_FILE_FILTER_URI;
3853 filter_info.uri = NULL;
3855 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
3857 if (filter_info.filename)
3858 g_free ((gchar *)filter_info.filename);
3859 if (filter_info.uri)
3860 g_free ((gchar *)filter_info.uri);
3866 install_list_model_filter (GtkFileChooserDefault *impl)
3868 g_assert (impl->browse_files_model != NULL);
3870 if (impl->current_filter)
3871 _gtk_file_system_model_set_filter (impl->browse_files_model,
3872 list_model_filter_func,
3876 #define COMPARE_DIRECTORIES \
3877 GtkFileChooserDefault *impl = user_data; \
3878 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
3879 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
3880 gboolean dir_a, dir_b; \
3883 dir_a = gtk_file_info_get_is_folder (info_a); \
3885 return impl->list_sort_ascending ? -1 : 1; \
3888 dir_b = gtk_file_info_get_is_folder (info_b); \
3890 return impl->list_sort_ascending ? 1 : -1; \
3892 if (dir_a != dir_b) \
3893 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
3895 /* Sort callback for the filename column */
3897 name_sort_func (GtkTreeModel *model,
3902 COMPARE_DIRECTORIES;
3904 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
3907 /* Sort callback for the size column */
3909 size_sort_func (GtkTreeModel *model,
3914 COMPARE_DIRECTORIES;
3917 gint64 size_a = gtk_file_info_get_size (info_a);
3918 gint64 size_b = gtk_file_info_get_size (info_b);
3920 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
3924 /* Sort callback for the mtime column */
3926 mtime_sort_func (GtkTreeModel *model,
3931 COMPARE_DIRECTORIES;
3934 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
3935 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
3937 return ta > tb ? -1 : (ta == tb ? 0 : 1);
3941 /* Callback used when the sort column changes. We cache the sort order for use
3942 * in name_sort_func().
3945 list_sort_column_changed_cb (GtkTreeSortable *sortable,
3946 GtkFileChooserDefault *impl)
3948 GtkSortType sort_type;
3950 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
3951 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
3955 set_busy_cursor (GtkFileChooserDefault *impl,
3958 GtkWindow *toplevel;
3959 GdkDisplay *display;
3962 toplevel = get_toplevel (GTK_WIDGET (impl));
3963 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
3966 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
3969 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
3973 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
3974 gdk_display_flush (display);
3977 gdk_cursor_unref (cursor);
3980 /* Callback used when the file system model finishes loading */
3982 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
3983 GtkFileChooserDefault *impl)
3985 set_busy_cursor (impl, FALSE);
3988 /* Gets rid of the old list model and creates a new one for the current folder */
3990 set_list_model (GtkFileChooserDefault *impl,
3993 if (impl->browse_files_model)
3995 g_object_unref (impl->browse_files_model);
3996 impl->browse_files_model = NULL;
3998 g_object_unref (impl->sort_model);
3999 impl->sort_model = NULL;
4002 set_busy_cursor (impl, TRUE);
4004 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
4005 impl->current_folder, 0,
4008 if (!impl->browse_files_model)
4010 set_busy_cursor (impl, FALSE);
4011 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
4015 g_signal_connect (impl->browse_files_model, "finished-loading",
4016 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
4018 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
4019 switch (impl->action)
4021 case GTK_FILE_CHOOSER_ACTION_OPEN:
4022 case GTK_FILE_CHOOSER_ACTION_SAVE:
4023 _gtk_file_system_model_set_show_files (impl->browse_files_model, TRUE);
4025 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
4026 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
4027 _gtk_file_system_model_set_show_files (impl->browse_files_model, FALSE);
4030 g_assert_not_reached ();
4032 install_list_model_filter (impl);
4034 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4035 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4036 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4037 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4038 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4039 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4040 impl->list_sort_ascending = TRUE;
4042 g_signal_connect (impl->sort_model, "sort-column-changed",
4043 G_CALLBACK (list_sort_column_changed_cb), impl);
4045 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4046 GTK_TREE_MODEL (impl->sort_model));
4047 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4048 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4049 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4055 update_chooser_entry (GtkFileChooserDefault *impl)
4057 GtkTreeSelection *selection;
4058 const GtkFileInfo *info;
4060 GtkTreeIter child_iter;
4062 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
4065 g_assert (!impl->select_multiple);
4066 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4068 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4071 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4075 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4077 if (!gtk_file_info_get_is_folder (info))
4078 gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry),
4079 gtk_file_info_get_display_name (info));
4083 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
4084 const GtkFilePath *path,
4087 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4090 if (impl->local_only &&
4091 !gtk_file_system_path_is_local (impl->file_system, path))
4094 GTK_FILE_SYSTEM_ERROR,
4095 GTK_FILE_SYSTEM_ERROR_FAILED,
4096 _("Cannot change to folder because it is not local"));
4101 /* Test validity of path here. */
4102 if (!check_is_folder (impl->file_system, path, error))
4105 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
4108 if (impl->current_folder != path)
4110 if (impl->current_folder)
4111 gtk_file_path_free (impl->current_folder);
4113 impl->current_folder = gtk_file_path_copy (path);
4116 /* Update the widgets that may trigger a folder change themselves. */
4118 if (!impl->changing_folder)
4120 impl->changing_folder = TRUE;
4122 shortcuts_update_current_folder (impl);
4124 impl->changing_folder = FALSE;
4127 /* Create a new list model. This is slightly evil; we store the result value
4128 * but perform more actions rather than returning immediately even if it
4129 * generates an error.
4131 result = set_list_model (impl, error);
4133 /* Refresh controls */
4135 shortcuts_find_current_folder (impl);
4137 g_signal_emit_by_name (impl, "current-folder-changed", 0);
4139 check_preview_change (impl);
4140 bookmarks_check_add_sensitivity (impl);
4142 g_signal_emit_by_name (impl, "selection-changed", 0);
4147 static GtkFilePath *
4148 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
4150 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4152 return gtk_file_path_copy (impl->current_folder);
4156 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
4159 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4161 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4162 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4164 gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry), name);
4168 select_func (GtkFileSystemModel *model,
4173 GtkFileChooserDefault *impl = user_data;
4174 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4175 GtkTreePath *sorted_path;
4177 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
4178 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
4179 gtk_tree_path_free (sorted_path);
4183 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
4184 const GtkFilePath *path,
4187 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4188 GtkFilePath *parent_path;
4190 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
4194 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
4198 GtkFileFolder *folder;
4202 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
4206 gtk_file_path_free (parent_path);
4210 folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
4211 gtk_file_path_free (parent_path);
4216 info = gtk_file_folder_get_info (folder, path, error);
4217 g_object_unref (folder);
4222 is_hidden = gtk_file_info_get_is_hidden (info);
4223 gtk_file_info_free (info);
4226 g_object_set (impl, "show-hidden", TRUE, NULL);
4228 result = _gtk_file_system_model_path_do (impl->browse_files_model, path,
4232 GTK_FILE_CHOOSER_ERROR,
4233 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
4234 _("Could not find the path"));
4239 g_assert_not_reached ();
4243 unselect_func (GtkFileSystemModel *model,
4248 GtkFileChooserDefault *impl = user_data;
4249 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4250 GtkTreePath *sorted_path;
4252 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
4254 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
4256 gtk_tree_path_free (sorted_path);
4260 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
4261 const GtkFilePath *path)
4263 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4265 if (!impl->browse_files_model)
4268 _gtk_file_system_model_path_do (impl->browse_files_model, path,
4269 unselect_func, impl);
4273 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
4275 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4276 if (impl->select_multiple)
4278 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4279 gtk_tree_selection_select_all (selection);
4284 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
4286 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4287 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4289 gtk_tree_selection_unselect_all (selection);
4292 /* Checks whether the filename entry for the Save modes contains a valid filename */
4293 static GtkFilePath *
4294 check_save_entry (GtkFileChooserDefault *impl,
4298 const char *filename;
4302 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4303 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4305 filename = gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry));
4307 if (!filename || filename[0] == '\0')
4317 path = gtk_file_system_make_path (impl->file_system, impl->current_folder, filename, &error);
4321 error_building_filename_dialog (impl, impl->current_folder, filename, error);
4330 struct get_paths_closure {
4331 GtkFileChooserDefault *impl;
4333 GtkFilePath *path_from_entry;
4337 get_paths_foreach (GtkTreeModel *model,
4342 struct get_paths_closure *info;
4343 const GtkFilePath *file_path;
4344 GtkFileSystemModel *fs_model;
4345 GtkTreeIter sel_iter;
4348 fs_model = info->impl->browse_files_model;
4349 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
4351 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
4353 return; /* We are on the editable row */
4355 if (!info->path_from_entry
4356 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
4357 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
4361 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
4363 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4364 struct get_paths_closure info;
4368 info.path_from_entry = NULL;
4370 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4371 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4373 gboolean is_valid, is_empty;
4375 info.path_from_entry = check_save_entry (impl, &is_valid, &is_empty);
4376 if (!is_valid && !is_empty)
4380 if (!info.path_from_entry || impl->select_multiple)
4382 GtkTreeSelection *selection;
4384 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4385 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
4388 if (info.path_from_entry)
4389 info.result = g_slist_prepend (info.result, info.path_from_entry);
4391 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
4392 * fall back to the current directory */
4393 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
4394 info.result == NULL)
4396 info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
4399 return g_slist_reverse (info.result);
4402 static GtkFilePath *
4403 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
4405 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4407 if (impl->preview_path)
4408 return gtk_file_path_copy (impl->preview_path);
4413 static GtkFileSystem *
4414 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
4416 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4418 return impl->file_system;
4421 /* Shows or hides the filter widgets */
4423 show_filters (GtkFileChooserDefault *impl,
4427 gtk_widget_show (impl->filter_combo_hbox);
4429 gtk_widget_hide (impl->filter_combo_hbox);
4433 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
4434 GtkFileFilter *filter)
4436 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4439 if (g_slist_find (impl->filters, filter))
4441 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
4445 g_object_ref (filter);
4446 gtk_object_sink (GTK_OBJECT (filter));
4447 impl->filters = g_slist_append (impl->filters, filter);
4449 name = gtk_file_filter_get_name (filter);
4451 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
4453 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
4455 if (!g_slist_find (impl->filters, impl->current_filter))
4456 set_current_filter (impl, filter);
4458 show_filters (impl, TRUE);
4462 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
4463 GtkFileFilter *filter)
4465 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4466 GtkTreeModel *model;
4470 filter_index = g_slist_index (impl->filters, filter);
4472 if (filter_index < 0)
4474 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
4478 impl->filters = g_slist_remove (impl->filters, filter);
4480 if (filter == impl->current_filter)
4483 set_current_filter (impl, impl->filters->data);
4485 set_current_filter (impl, NULL);
4488 /* Remove row from the combo box */
4489 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
4490 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
4491 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
4493 g_object_unref (filter);
4496 show_filters (impl, FALSE);
4500 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
4502 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4504 return g_slist_copy (impl->filters);
4507 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
4509 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
4512 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
4516 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
4517 const GtkFilePath *path,
4520 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4524 /* Test validity of path here. */
4525 if (!check_is_folder (impl->file_system, path, error))
4528 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
4530 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
4533 impl->num_shortcuts++;
4535 if (impl->shortcuts_filter_model)
4536 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
4542 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
4543 const GtkFilePath *path,
4546 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4552 if (impl->num_shortcuts == 0)
4555 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
4556 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
4557 g_assert_not_reached ();
4559 for (i = 0; i < impl->num_shortcuts; i++)
4561 GtkFilePath *shortcut;
4563 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
4564 g_assert (shortcut != NULL);
4566 if (gtk_file_path_compare (shortcut, path) == 0)
4568 /* The other columns are freed by the GtkTreeStore */
4569 gtk_file_path_free (shortcut);
4570 gtk_list_store_remove (impl->shortcuts_model, &iter);
4571 impl->num_shortcuts--;
4575 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
4576 g_assert_not_reached ();
4581 uri = gtk_file_system_path_to_uri (impl->file_system, path);
4583 GTK_FILE_CHOOSER_ERROR,
4584 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
4585 _("shortcut %s does not exist"),
4593 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
4595 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4601 if (impl->num_shortcuts == 0)
4604 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
4605 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
4606 g_assert_not_reached ();
4610 for (i = 0; i < impl->num_shortcuts; i++)
4612 GtkFilePath *shortcut;
4614 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
4615 g_assert (shortcut != NULL);
4617 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
4619 if (i != impl->num_shortcuts - 1)
4621 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
4622 g_assert_not_reached ();
4626 return g_slist_reverse (list);
4629 /* Guesses a size based upon font sizes */
4631 find_good_size_from_style (GtkWidget *widget,
4635 GtkFileChooserDefault *impl;
4636 gint default_width, default_height;
4639 GtkRequisition preview_req;
4641 g_assert (widget->style != NULL);
4642 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4644 font_size = pango_font_description_get_size (widget->style->font_desc);
4645 font_size = PANGO_PIXELS (font_size);
4647 default_width = font_size * NUM_CHARS;
4648 default_height = font_size * NUM_LINES;
4650 /* Use at least the requisition size not including the preview widget */
4651 gtk_widget_size_request (widget, &req);
4653 if (impl->preview_widget_active && impl->preview_widget)
4654 gtk_widget_size_request (impl->preview_box, &preview_req);
4656 preview_req.width = 0;
4658 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
4659 default_height = MAX (default_height, req.height);
4661 *width = default_width;
4662 *height = default_height;
4666 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
4667 gint *default_width,
4668 gint *default_height)
4670 GtkFileChooserDefault *impl;
4672 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4674 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
4676 if (impl->preview_widget_active && impl->preview_widget)
4677 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
4681 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
4682 gboolean *resize_horizontally,
4683 gboolean *resize_vertically)
4685 GtkFileChooserDefault *impl;
4687 g_return_if_fail (resize_horizontally != NULL);
4688 g_return_if_fail (resize_vertically != NULL);
4690 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4692 *resize_horizontally = TRUE;
4693 *resize_vertically = TRUE;
4695 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4696 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4698 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4700 *resize_horizontally = FALSE;
4701 *resize_vertically = FALSE;
4706 struct switch_folder_closure {
4707 GtkFileChooserDefault *impl;
4708 const GtkFilePath *path;
4712 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
4714 switch_folder_foreach_cb (GtkTreeModel *model,
4719 struct switch_folder_closure *closure;
4720 GtkTreeIter child_iter;
4724 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
4726 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
4727 closure->num_selected++;
4730 /* Changes to the selected folder in the list view */
4732 switch_to_selected_folder (GtkFileChooserDefault *impl)
4734 GtkTreeSelection *selection;
4735 struct switch_folder_closure closure;
4737 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4738 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE);
4740 /* We do this with foreach() rather than get_selected() as we may be in
4741 * multiple selection mode
4744 closure.impl = impl;
4745 closure.path = NULL;
4746 closure.num_selected = 0;
4748 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4749 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
4751 g_assert (closure.path && closure.num_selected == 1);
4753 change_folder_and_display_error (impl, closure.path);
4756 /* Implementation for GtkFileChooserEmbed::should_respond() */
4758 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
4760 GtkFileChooserDefault *impl;
4763 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4765 /* First, if the shortcuts list had the focus, we should switch folders rather
4768 if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
4772 if (shortcuts_get_selected (impl, &iter))
4773 shortcuts_activate_iter (impl, &iter);
4778 /* Second, check the save entry. If it has a valid name, we are done. */
4780 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4781 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4784 gboolean is_valid, is_empty;
4786 path = check_save_entry (impl, &is_valid, &is_empty);
4790 gtk_file_path_free (path);
4797 /* Third, do we have an empty selection? */
4798 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4799 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4801 GtkTreeSelection *selection;
4803 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4804 num_selected = gtk_tree_selection_count_selected_rows (selection);
4805 if (num_selected == 0)
4809 /* Fourth, should we return file names or folder names? */
4811 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4812 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4814 gboolean all_files, all_folders;
4816 selection_check (impl, NULL, &all_files, &all_folders);
4818 if (num_selected == 1)
4822 switch_to_selected_folder (impl);
4831 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
4832 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4833 /* There can be no files selected in folder mode since we don't show them,
4838 g_assert_not_reached ();
4842 /* Implementation for GtkFileChooserEmbed::initial_focus() */
4844 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
4846 GtkFileChooserDefault *impl;
4849 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4851 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4852 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4856 path = gtk_tree_path_new_from_indices (0, -1);
4857 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4858 gtk_tree_path_free (path);
4860 widget = impl->browse_files_tree_view;
4862 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4863 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4864 widget = impl->save_file_name_entry;
4867 g_assert_not_reached ();
4871 gtk_widget_grab_focus (widget);
4875 set_current_filter (GtkFileChooserDefault *impl,
4876 GtkFileFilter *filter)
4878 if (impl->current_filter != filter)
4882 /* If we have filters, new filter must be one of them
4884 filter_index = g_slist_index (impl->filters, filter);
4885 if (impl->filters && filter_index < 0)
4888 if (impl->current_filter)
4889 g_object_unref (impl->current_filter);
4890 impl->current_filter = filter;
4891 if (impl->current_filter)
4893 g_object_ref (impl->current_filter);
4894 gtk_object_sink (GTK_OBJECT (filter));
4898 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
4901 if (impl->browse_files_model)
4902 install_list_model_filter (impl);
4904 g_object_notify (G_OBJECT (impl), "filter");
4909 filter_combo_changed (GtkComboBox *combo_box,
4910 GtkFileChooserDefault *impl)
4912 gint new_index = gtk_combo_box_get_active (combo_box);
4913 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
4915 set_current_filter (impl, new_filter);
4919 check_preview_change (GtkFileChooserDefault *impl)
4921 GtkTreePath *cursor_path;
4922 const GtkFilePath *new_path;
4923 const GtkFileInfo *new_info;
4925 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
4926 if (cursor_path && impl->sort_model)
4929 GtkTreeIter child_iter;
4931 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
4932 gtk_tree_path_free (cursor_path);
4934 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
4936 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
4937 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4945 if (new_path != impl->preview_path &&
4946 !(new_path && impl->preview_path &&
4947 gtk_file_path_compare (new_path, impl->preview_path) == 0))
4949 if (impl->preview_path)
4951 gtk_file_path_free (impl->preview_path);
4952 g_free (impl->preview_display_name);
4957 impl->preview_path = gtk_file_path_copy (new_path);
4958 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
4962 impl->preview_path = NULL;
4963 impl->preview_display_name = NULL;
4966 if (impl->use_preview_label && impl->preview_label)
4967 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
4969 g_signal_emit_by_name (impl, "update-preview");
4973 /* Activates a volume by mounting it if necessary and then switching to its
4977 shortcuts_activate_volume (GtkFileChooserDefault *impl,
4978 GtkFileSystemVolume *volume)
4982 /* We ref the file chooser since volume_mount() may run a main loop, and the
4983 * user could close the file chooser window in the meantime.
4985 g_object_ref (impl);
4987 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
4992 set_busy_cursor (impl, TRUE);
4995 result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
5001 msg = g_strdup_printf ("Could not mount %s:\n%s",
5002 gtk_file_system_volume_get_display_name (impl->file_system, volume),
5004 error_message (impl, msg);
5006 g_error_free (error);
5009 set_busy_cursor (impl, FALSE);
5015 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
5016 change_folder_and_display_error (impl, path);
5017 gtk_file_path_free (path);
5021 g_object_unref (impl);
5024 /* Opens the folder or volume at the specified iter in the shortcuts model */
5026 shortcuts_activate_iter (GtkFileChooserDefault *impl,
5034 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), iter);
5035 g_assert (path != NULL);
5037 item_num = *gtk_tree_path_get_indices (path);
5038 gtk_tree_path_free (path);
5040 if (item_num == shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR)
5041 || item_num == shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR))
5044 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter, SHORTCUTS_COL_PATH, &data, -1);
5046 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
5047 if ((item_num >= start_row && item_num < start_row + impl->num_volumes)
5048 || (item_num == shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER) && impl->shortcuts_current_folder_is_volume))
5050 GtkFileSystemVolume *volume;
5053 shortcuts_activate_volume (impl, volume);
5057 const GtkFilePath *file_path;
5060 change_folder_and_display_error (impl, file_path);
5064 /* Callback used when a row in the shortcuts list is activated */
5066 shortcuts_row_activated_cb (GtkTreeView *tree_view,
5068 GtkTreeViewColumn *column,
5069 GtkFileChooserDefault *impl)
5072 GtkTreeIter child_iter;
5074 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
5077 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
5080 shortcuts_activate_iter (impl, &child_iter);
5083 /* Handler for GtkWidget::key-press-event on the shortcuts list */
5085 shortcuts_key_press_event_cb (GtkWidget *widget,
5087 GtkFileChooserDefault *impl)
5091 modifiers = gtk_accelerator_get_default_mod_mask ();
5093 if ((event->keyval == GDK_BackSpace
5094 || event->keyval == GDK_Delete
5095 || event->keyval == GDK_KP_Delete)
5096 && (event->state & modifiers) == 0)
5098 remove_selected_bookmarks (impl);
5106 shortcuts_select_func (GtkTreeSelection *selection,
5107 GtkTreeModel *model,
5109 gboolean path_currently_selected,
5112 GtkFileChooserDefault *impl = data;
5114 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
5118 list_selection_changed (GtkTreeSelection *selection,
5119 GtkFileChooserDefault *impl)
5121 /* See if we are in the new folder editable row for Save mode */
5122 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5124 GtkTreeSelection *selection;
5125 GtkTreeIter iter, child_iter;
5126 const GtkFileInfo *info;
5128 g_assert (!impl->select_multiple);
5129 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5130 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
5133 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5137 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5139 return; /* We are on the editable row for New Folder */
5142 update_chooser_entry (impl);
5143 check_preview_change (impl);
5144 bookmarks_check_add_sensitivity (impl);
5146 g_signal_emit_by_name (impl, "selection-changed", 0);
5149 /* Callback used when a row in the file list is activated */
5151 list_row_activated (GtkTreeView *tree_view,
5153 GtkTreeViewColumn *column,
5154 GtkFileChooserDefault *impl)
5156 GtkTreeIter iter, child_iter;
5157 const GtkFileInfo *info;
5159 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5162 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5164 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5166 if (gtk_file_info_get_is_folder (info))
5168 const GtkFilePath *file_path;
5170 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5171 change_folder_and_display_error (impl, file_path);
5176 g_signal_emit_by_name (impl, "file-activated");
5180 path_bar_clicked (GtkPathBar *path_bar,
5181 GtkFilePath *file_path,
5182 gboolean child_is_hidden,
5183 GtkFileChooserDefault *impl)
5185 if (!change_folder_and_display_error (impl, file_path))
5188 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
5189 * show hidden files so that ".baz" appears in the file list, as it will still
5190 * be shown in the path bar: "/foo/[bar]/.baz"
5192 if (child_is_hidden)
5193 g_object_set (impl, "show-hidden", TRUE, NULL);
5196 static const GtkFileInfo *
5197 get_list_file_info (GtkFileChooserDefault *impl,
5200 GtkTreeIter child_iter;
5202 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5206 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5210 list_icon_data_func (GtkTreeViewColumn *tree_column,
5211 GtkCellRenderer *cell,
5212 GtkTreeModel *tree_model,
5216 GtkFileChooserDefault *impl = data;
5217 GtkTreeIter child_iter;
5218 const GtkFilePath *path;
5221 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5224 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5228 /* FIXME: NULL GError */
5229 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
5230 impl->icon_size, NULL);
5236 g_object_unref (pixbuf);
5240 list_name_data_func (GtkTreeViewColumn *tree_column,
5241 GtkCellRenderer *cell,
5242 GtkTreeModel *tree_model,
5246 GtkFileChooserDefault *impl = data;
5247 const GtkFileInfo *info = get_list_file_info (impl, iter);
5252 "text", _("Type name of new folder"),
5258 "text", gtk_file_info_get_display_name (info),
5264 list_size_data_func (GtkTreeViewColumn *tree_column,
5265 GtkCellRenderer *cell,
5266 GtkTreeModel *tree_model,
5270 GtkFileChooserDefault *impl = data;
5271 const GtkFileInfo *info = get_list_file_info (impl, iter);
5275 if (!info || gtk_file_info_get_is_folder (info))
5278 size = gtk_file_info_get_size (info);
5280 if (size < (gint64)1024)
5281 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
5282 else if (size < (gint64)1024*1024)
5283 str = g_strdup_printf (_("%.1f K"), size / (1024.));
5284 else if (size < (gint64)1024*1024*1024)
5285 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
5287 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
5297 /* Tree column data callback for the file list; fetches the mtime of a file */
5299 list_mtime_data_func (GtkTreeViewColumn *tree_column,
5300 GtkCellRenderer *cell,
5301 GtkTreeModel *tree_model,
5305 GtkFileChooserDefault *impl;
5306 const GtkFileInfo *info;
5307 GtkFileTime time_mtime, time_now;
5314 info = get_list_file_info (impl, iter);
5323 time_mtime = gtk_file_info_get_modification_time (info);
5324 g_date_set_time (&mtime, (GTime) time_mtime);
5326 time_now = (GTime ) time (NULL);
5327 g_date_set_time (&now, (GTime) time_now);
5329 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
5332 strcpy (buf, _("Today"));
5333 else if (days_diff == 1)
5334 strcpy (buf, _("Yesterday"));
5339 if (days_diff > 1 && days_diff < 7)
5340 format = "%A"; /* Days from last week */
5342 format = "%x"; /* Any other date */
5344 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
5345 strcpy (buf, _("Unknown"));
5354 _gtk_file_chooser_default_new (const char *file_system)
5356 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
5357 "file-system-backend", file_system,
5361 /* Sets the file part of a file chooser entry from the file under the cursor */
5363 location_entry_set_from_list (GtkFileChooserDefault *impl,
5364 GtkFileChooserEntry *entry)
5366 GtkTreePath *tree_path;
5367 GtkTreeIter iter, child_iter;
5368 const GtkFileInfo *info;
5371 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5372 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
5374 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &tree_path, NULL);
5378 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, tree_path);
5379 gtk_tree_path_free (tree_path);
5381 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5383 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5387 name = gtk_file_info_get_display_name (info);
5388 _gtk_file_chooser_entry_set_file_part (entry, name);
5391 /* Sets the file part of a file chooser entry from the Name entry in save mode */
5393 location_entry_set_from_save_name (GtkFileChooserDefault *impl,
5394 GtkFileChooserEntry *entry)
5396 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5397 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5399 _gtk_file_chooser_entry_set_file_part (entry, gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
5403 location_entry_create (GtkFileChooserDefault *impl)
5407 entry = _gtk_file_chooser_entry_new ();
5408 /* Pick a good width for the entry */
5409 gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
5410 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
5411 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
5412 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
5414 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5415 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5416 location_entry_set_from_list (impl, GTK_FILE_CHOOSER_ENTRY (entry));
5417 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5418 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5419 location_entry_set_from_save_name (impl, GTK_FILE_CHOOSER_ENTRY (entry));
5421 g_assert_not_reached ();
5423 return GTK_WIDGET (entry);
5427 update_from_entry (GtkFileChooserDefault *impl,
5429 GtkFileChooserEntry *chooser_entry)
5431 const GtkFilePath *folder_path;
5432 const char *file_part;
5434 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
5435 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
5437 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
5439 error_message_with_parent (parent,
5440 _("Cannot change to the folder you specified as it is an invalid path."));
5444 if (file_part[0] == '\0')
5445 return change_folder_and_display_error (impl, folder_path);
5448 GtkFileFolder *folder = NULL;
5449 GtkFilePath *subfolder_path = NULL;
5450 GtkFileInfo *info = NULL;
5456 /* If the file part is non-empty, we need to figure out if it refers to a
5457 * folder within folder. We could optimize the case here where the folder
5458 * is already loaded for one of our tree models.
5462 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
5466 error_getting_info_dialog (impl, folder_path, error);
5471 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
5473 if (!subfolder_path)
5478 uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
5479 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
5482 error_message (impl, msg);
5489 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
5493 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5494 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5496 if (!change_folder_and_display_error (impl, folder_path))
5499 gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
5502 error_getting_info_dialog (impl, subfolder_path, error);
5507 if (gtk_file_info_get_is_folder (info))
5508 result = change_folder_and_display_error (impl, subfolder_path);
5514 result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
5517 _("Could not select %s:\n%s"),
5518 subfolder_path, error);
5524 g_object_unref (folder);
5526 gtk_file_path_free (subfolder_path);
5529 gtk_file_info_free (info);
5534 g_assert_not_reached ();
5538 location_popup_handler (GtkFileChooserDefault *impl)
5541 GtkWindow *toplevel;
5547 const char *accept_stock;
5551 toplevel = get_toplevel (GTK_WIDGET (impl));
5553 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5554 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5556 title = _("Open Location");
5557 accept_stock = GTK_STOCK_OPEN;
5561 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5562 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5563 title = _("Save in Location");
5564 accept_stock = GTK_STOCK_SAVE;
5567 dialog = gtk_dialog_new_with_buttons (title,
5569 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
5570 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
5571 accept_stock, GTK_RESPONSE_ACCEPT,
5573 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
5574 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
5575 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
5576 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
5578 hbox = gtk_hbox_new (FALSE, 12);
5579 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
5580 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
5582 label = gtk_label_new_with_mnemonic (_("_Location:"));
5583 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
5585 entry = location_entry_create (impl);
5586 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
5587 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
5591 gtk_widget_show_all (dialog);
5595 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
5597 if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
5599 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5600 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5602 gtk_widget_grab_focus (impl->browse_files_tree_view);
5606 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5607 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5608 gtk_widget_grab_focus (impl->save_file_name_entry);
5616 GtkWindow *toplevel;
5618 toplevel = get_toplevel (GTK_WIDGET (impl));
5619 if (toplevel && toplevel->focus_widget)
5620 gtk_widget_grab_focus (toplevel->focus_widget);
5623 gtk_widget_destroy (dialog);
5626 /* Handler for the "up-folder" keybinding signal */
5628 up_folder_handler (GtkFileChooserDefault *impl)
5630 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
5633 /* Handler for the "down-folder" keybinding signal */
5635 down_folder_handler (GtkFileChooserDefault *impl)
5637 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
5640 /* Handler for the "home-folder" keybinding signal */
5642 home_folder_handler (GtkFileChooserDefault *impl)
5648 if (!impl->has_home)
5649 return; /* Should we put up an error dialog? */
5651 pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
5652 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5653 g_assert_not_reached ();
5655 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &path, -1);
5656 g_assert (path != NULL);
5658 change_folder_and_display_error (impl, path);
5663 /* Drag and drop interfaces */
5666 shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
5671 shortcuts_model_filter_init (ShortcutsModelFilter *model)
5676 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
5678 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
5681 ShortcutsModelFilter *model;
5685 model = SHORTCUTS_MODEL_FILTER (drag_source);
5687 pos = *gtk_tree_path_get_indices (path);
5688 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
5690 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
5693 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
5695 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
5697 GtkSelectionData *selection_data)
5699 ShortcutsModelFilter *model;
5701 model = SHORTCUTS_MODEL_FILTER (drag_source);
5708 /* Fill the GtkTreeDragSourceIface vtable */
5710 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
5712 iface->row_draggable = shortcuts_model_filter_row_draggable;
5713 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
5717 /* Fill the GtkTreeDragDestIface vtable */
5719 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
5721 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
5722 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
5726 static GtkTreeModel *
5727 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
5728 GtkTreeModel *child_model,
5731 ShortcutsModelFilter *model;
5733 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
5734 "child_model", child_model,
5735 "virtual_root", root,
5740 return GTK_TREE_MODEL (model);