1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "gdk/gdkkeysyms.h"
23 #include "gtkalignment.h"
24 #include "gtkbindings.h"
25 #include "gtkbutton.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrendererpixbuf.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcheckmenuitem.h"
31 #include "gtkcombobox.h"
33 #include "gtkeventbox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserdefault.h"
36 #include "gtkfilechooserembed.h"
37 #include "gtkfilechooserentry.h"
38 #include "gtkfilechooserutils.h"
39 #include "gtkfilechooser.h"
40 #include "gtkfilesystemmodel.h"
43 #include "gtkhpaned.h"
44 #include "gtkiconfactory.h"
45 #include "gtkicontheme.h"
47 #include "gtkimagemenuitem.h"
50 #include "gtkmarshalers.h"
51 #include "gtkmenuitem.h"
52 #include "gtkmessagedialog.h"
53 #include "gtkpathbar.h"
54 #include "gtkprivate.h"
55 #include "gtkscrolledwindow.h"
56 #include "gtkseparatormenuitem.h"
57 #include "gtksizegroup.h"
60 #include "gtktreednd.h"
61 #include "gtktreeprivate.h"
62 #include "gtktreeview.h"
63 #include "gtktreemodelsort.h"
64 #include "gtktreeselection.h"
65 #include "gtktreestore.h"
66 #include "gtktooltips.h"
67 #include "gtktypebuiltins.h"
70 #if defined (G_OS_UNIX)
71 #include "gtkfilesystemunix.h"
72 #elif defined (G_OS_WIN32)
73 #include "gtkfilesystemwin32.h"
86 #define PROFILE_FILE_CHOOSER
87 #ifdef PROFILE_FILE_CHOOSER
100 #define PROFILE_INDENT 4
101 static int profile_indent;
104 profile_add_indent (int indent)
106 profile_indent += indent;
107 if (profile_indent < 0)
108 g_error ("You screwed up your indentation");
112 _gtk_file_chooser_profile_log (const char *func, int indent, const char *msg1, const char *msg2)
117 profile_add_indent (indent);
119 if (profile_indent == 0)
120 str = g_strdup_printf ("MARK: %s %s %s", func, msg1 ? msg1 : "", msg2 ? msg2 : "");
122 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func, msg1 ? msg1 : "", msg2 ? msg2 : "");
128 profile_add_indent (indent);
131 #define profile_start(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, PROFILE_INDENT, x, y)
132 #define profile_end(x, y) _gtk_file_chooser_profile_log (G_STRFUNC, -PROFILE_INDENT, x, y)
133 #define profile_msg(x, y) _gtk_file_chooser_profile_log (NULL, 0, x, y)
135 #define profile_start(x, y)
136 #define profile_end(x, y)
137 #define profile_msg(x, y)
142 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
144 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
145 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
146 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
149 LOAD_EMPTY, /* There is no model */
150 LOAD_PRELOAD, /* Model is loading and a timer is running; model isn't inserted into the tree yet */
151 LOAD_LOADING, /* Timeout expired, model is inserted into the tree, but not fully loaded yet */
152 LOAD_FINISHED /* Model is fully loaded and inserted into the tree */
155 #define MAX_LOADING_TIME 500
157 struct _GtkFileChooserDefaultClass
159 GtkVBoxClass parent_class;
162 struct _GtkFileChooserDefault
164 GtkVBox parent_instance;
166 GtkFileChooserAction action;
168 GtkFileSystem *file_system;
170 /* Save mode widgets */
171 GtkWidget *save_widgets;
173 GtkWidget *save_file_name_entry;
174 GtkWidget *save_folder_label;
175 GtkWidget *save_folder_combo;
176 GtkWidget *save_expander;
178 /* The file browsing widgets */
179 GtkWidget *browse_widgets;
180 GtkWidget *browse_shortcuts_tree_view;
181 GtkWidget *browse_shortcuts_add_button;
182 GtkWidget *browse_shortcuts_remove_button;
183 GtkWidget *browse_shortcuts_popup_menu;
184 GtkWidget *browse_shortcuts_popup_menu_remove_item;
185 GtkWidget *browse_shortcuts_popup_menu_rename_item;
186 GtkWidget *browse_files_tree_view;
187 GtkWidget *browse_files_popup_menu;
188 GtkWidget *browse_files_popup_menu_add_shortcut_item;
189 GtkWidget *browse_files_popup_menu_hidden_files_item;
190 GtkWidget *browse_new_folder_button;
191 GtkWidget *browse_path_bar;
193 GtkFileSystemModel *browse_files_model;
195 GtkWidget *filter_combo_hbox;
196 GtkWidget *filter_combo;
197 GtkWidget *preview_box;
198 GtkWidget *preview_label;
199 GtkWidget *preview_widget;
200 GtkWidget *extra_align;
201 GtkWidget *extra_widget;
203 GtkListStore *shortcuts_model;
204 GtkTreeModel *shortcuts_filter_model;
206 GtkTreeModelSort *sort_model;
208 LoadState load_state;
209 guint load_timeout_id;
211 GSList *pending_select_paths;
213 GtkFileFilter *current_filter;
216 GtkTooltips *tooltips;
219 gboolean has_desktop;
225 gulong volumes_changed_id;
226 gulong bookmarks_changed_id;
228 GtkFilePath *current_volume_path;
229 GtkFilePath *current_folder;
230 GtkFilePath *preview_path;
231 char *preview_display_name;
233 GtkTreeViewColumn *list_name_column;
234 GtkCellRenderer *list_name_renderer;
236 GSource *edited_idle;
237 char *edited_new_text;
239 gulong settings_signal_id;
242 gulong toplevel_set_focus_id;
243 GtkWidget *toplevel_last_focus_widget;
246 GdkDragContext *shortcuts_drag_context;
247 GSource *shortcuts_drag_outside_idle;
252 guint local_only : 1;
253 guint preview_widget_active : 1;
254 guint use_preview_label : 1;
255 guint select_multiple : 1;
256 guint show_hidden : 1;
257 guint do_overwrite_confirmation : 1;
258 guint list_sort_ascending : 1;
259 guint changing_folder : 1;
260 guint shortcuts_current_folder_active : 1;
263 guint shortcuts_drag_outside : 1;
276 static guint signals[LAST_SIGNAL] = { 0 };
278 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
280 SHORTCUTS_COL_PIXBUF,
283 SHORTCUTS_COL_IS_VOLUME,
284 SHORTCUTS_COL_REMOVABLE,
285 SHORTCUTS_COL_PIXBUF_VISIBLE,
286 SHORTCUTS_COL_NUM_COLUMNS
289 /* Column numbers for the file list */
294 FILE_LIST_COL_NUM_COLUMNS
297 /* Identifiers for target types */
303 /* Target types for dragging from the shortcuts list */
304 static const GtkTargetEntry shortcuts_source_targets[] = {
305 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
308 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
309 / sizeof (shortcuts_source_targets[0]));
311 /* Target types for dropping into the shortcuts list */
312 static const GtkTargetEntry shortcuts_dest_targets[] = {
313 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
314 { "text/uri-list", 0, TEXT_URI_LIST }
317 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
318 / sizeof (shortcuts_dest_targets[0]));
320 /* Target types for DnD from the file list */
321 static const GtkTargetEntry file_list_source_targets[] = {
322 { "text/uri-list", 0, TEXT_URI_LIST }
325 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
326 / sizeof (file_list_source_targets[0]));
328 /* Target types for dropping into the file list */
329 static const GtkTargetEntry file_list_dest_targets[] = {
330 { "text/uri-list", 0, TEXT_URI_LIST }
333 static const int num_file_list_dest_targets = (sizeof (file_list_dest_targets)
334 / sizeof (file_list_dest_targets[0]));
337 /* Interesting places in the shortcuts bar */
343 SHORTCUTS_BOOKMARKS_SEPARATOR,
345 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
346 SHORTCUTS_CURRENT_FOLDER
349 /* Icon size for if we can't get it from the theme */
350 #define FALLBACK_ICON_SIZE 16
352 #define PREVIEW_HBOX_SPACING 12
356 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
357 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
358 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
359 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
361 static GObject* gtk_file_chooser_default_constructor (GType type,
362 guint n_construct_properties,
363 GObjectConstructParam *construct_params);
364 static void gtk_file_chooser_default_finalize (GObject *object);
365 static void gtk_file_chooser_default_set_property (GObject *object,
369 static void gtk_file_chooser_default_get_property (GObject *object,
373 static void gtk_file_chooser_default_dispose (GObject *object);
374 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
375 static void gtk_file_chooser_default_map (GtkWidget *widget);
376 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
377 GtkWidget *previous_toplevel);
378 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
379 GtkStyle *previous_style);
380 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
381 GdkScreen *previous_screen);
383 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
384 const GtkFilePath *path,
386 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
387 const GtkFilePath *path,
390 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
391 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
393 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
394 const GtkFilePath *path,
396 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
397 const GtkFilePath *path);
398 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
399 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
400 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
401 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
402 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
403 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
404 GtkFileFilter *filter);
405 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
406 GtkFileFilter *filter);
407 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
408 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
409 const GtkFilePath *path,
411 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
412 const GtkFilePath *path,
414 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
416 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
418 gint *default_height);
419 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
420 gboolean *resize_horizontally,
421 gboolean *resize_vertically);
422 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
423 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
425 static void location_popup_handler (GtkFileChooserDefault *impl,
427 static void up_folder_handler (GtkFileChooserDefault *impl);
428 static void down_folder_handler (GtkFileChooserDefault *impl);
429 static void home_folder_handler (GtkFileChooserDefault *impl);
430 static void update_appearance (GtkFileChooserDefault *impl);
432 static void set_current_filter (GtkFileChooserDefault *impl,
433 GtkFileFilter *filter);
434 static void check_preview_change (GtkFileChooserDefault *impl);
436 static void filter_combo_changed (GtkComboBox *combo_box,
437 GtkFileChooserDefault *impl);
438 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
440 GtkTreeViewColumn *column,
441 GtkFileChooserDefault *impl);
443 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
445 GtkFileChooserDefault *impl);
447 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
450 gboolean path_currently_selected,
452 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
454 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
456 static int shortcuts_get_index (GtkFileChooserDefault *impl,
457 ShortcutsIndex where);
458 static int shortcut_find_position (GtkFileChooserDefault *impl,
459 const GtkFilePath *path);
461 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
463 static gboolean list_select_func (GtkTreeSelection *selection,
466 gboolean path_currently_selected,
469 static void list_selection_changed (GtkTreeSelection *tree_selection,
470 GtkFileChooserDefault *impl);
471 static void list_row_activated (GtkTreeView *tree_view,
473 GtkTreeViewColumn *column,
474 GtkFileChooserDefault *impl);
476 static void select_func (GtkFileSystemModel *model,
481 static void path_bar_clicked (GtkPathBar *path_bar,
482 GtkFilePath *file_path,
483 gboolean child_is_hidden,
484 GtkFileChooserDefault *impl);
486 static void add_bookmark_button_clicked_cb (GtkButton *button,
487 GtkFileChooserDefault *impl);
488 static void remove_bookmark_button_clicked_cb (GtkButton *button,
489 GtkFileChooserDefault *impl);
491 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
492 GtkCellRenderer *cell,
493 GtkTreeModel *tree_model,
496 static void list_name_data_func (GtkTreeViewColumn *tree_column,
497 GtkCellRenderer *cell,
498 GtkTreeModel *tree_model,
502 static void list_size_data_func (GtkTreeViewColumn *tree_column,
503 GtkCellRenderer *cell,
504 GtkTreeModel *tree_model,
508 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
509 GtkCellRenderer *cell,
510 GtkTreeModel *tree_model,
514 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
517 static void load_remove_timer (GtkFileChooserDefault *impl);
518 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
520 static GObjectClass *parent_class;
524 /* Drag and drop interface declarations */
527 GtkTreeModelFilter parent;
529 GtkFileChooserDefault *impl;
530 } ShortcutsModelFilter;
533 GtkTreeModelFilterClass parent_class;
534 } ShortcutsModelFilterClass;
536 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
537 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
539 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
541 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
542 _shortcuts_model_filter,
543 GTK_TYPE_TREE_MODEL_FILTER,
544 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
545 shortcuts_model_filter_drag_source_iface_init));
547 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
548 GtkTreeModel *child_model,
554 _gtk_file_chooser_default_get_type (void)
556 static GType file_chooser_default_type = 0;
558 if (!file_chooser_default_type)
560 static const GTypeInfo file_chooser_default_info =
562 sizeof (GtkFileChooserDefaultClass),
563 NULL, /* base_init */
564 NULL, /* base_finalize */
565 (GClassInitFunc) gtk_file_chooser_default_class_init,
566 NULL, /* class_finalize */
567 NULL, /* class_data */
568 sizeof (GtkFileChooserDefault),
570 (GInstanceInitFunc) gtk_file_chooser_default_init,
573 static const GInterfaceInfo file_chooser_info =
575 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
576 NULL, /* interface_finalize */
577 NULL /* interface_data */
580 static const GInterfaceInfo file_chooser_embed_info =
582 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
583 NULL, /* interface_finalize */
584 NULL /* interface_data */
587 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
588 &file_chooser_default_info, 0);
590 g_type_add_interface_static (file_chooser_default_type,
591 GTK_TYPE_FILE_CHOOSER,
593 g_type_add_interface_static (file_chooser_default_type,
594 GTK_TYPE_FILE_CHOOSER_EMBED,
595 &file_chooser_embed_info);
598 return file_chooser_default_type;
602 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
604 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
605 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
606 GtkBindingSet *binding_set;
608 parent_class = g_type_class_peek_parent (class);
610 gobject_class->finalize = gtk_file_chooser_default_finalize;
611 gobject_class->constructor = gtk_file_chooser_default_constructor;
612 gobject_class->set_property = gtk_file_chooser_default_set_property;
613 gobject_class->get_property = gtk_file_chooser_default_get_property;
614 gobject_class->dispose = gtk_file_chooser_default_dispose;
616 widget_class->show_all = gtk_file_chooser_default_show_all;
617 widget_class->map = gtk_file_chooser_default_map;
618 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
619 widget_class->style_set = gtk_file_chooser_default_style_set;
620 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
622 signals[LOCATION_POPUP] =
623 _gtk_binding_signal_new ("location-popup",
624 G_OBJECT_CLASS_TYPE (class),
625 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
626 G_CALLBACK (location_popup_handler),
628 _gtk_marshal_VOID__STRING,
629 G_TYPE_NONE, 1, G_TYPE_STRING);
631 _gtk_binding_signal_new ("up-folder",
632 G_OBJECT_CLASS_TYPE (class),
633 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
634 G_CALLBACK (up_folder_handler),
636 _gtk_marshal_VOID__VOID,
638 signals[DOWN_FOLDER] =
639 _gtk_binding_signal_new ("down-folder",
640 G_OBJECT_CLASS_TYPE (class),
641 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
642 G_CALLBACK (down_folder_handler),
644 _gtk_marshal_VOID__VOID,
646 signals[HOME_FOLDER] =
647 _gtk_binding_signal_new ("home-folder",
648 G_OBJECT_CLASS_TYPE (class),
649 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
650 G_CALLBACK (home_folder_handler),
652 _gtk_marshal_VOID__VOID,
655 binding_set = gtk_binding_set_by_class (class);
657 gtk_binding_entry_add_signal (binding_set,
658 GDK_l, GDK_CONTROL_MASK,
660 1, G_TYPE_STRING, "");
662 gtk_binding_entry_add_signal (binding_set,
665 1, G_TYPE_STRING, "/");
667 gtk_binding_entry_add_signal (binding_set,
668 GDK_Up, GDK_MOD1_MASK,
671 gtk_binding_entry_add_signal (binding_set,
675 gtk_binding_entry_add_signal (binding_set,
676 GDK_KP_Up, GDK_MOD1_MASK,
680 gtk_binding_entry_add_signal (binding_set,
681 GDK_Down, GDK_MOD1_MASK,
684 gtk_binding_entry_add_signal (binding_set,
685 GDK_KP_Down, GDK_MOD1_MASK,
689 gtk_binding_entry_add_signal (binding_set,
690 GDK_Home, GDK_MOD1_MASK,
693 gtk_binding_entry_add_signal (binding_set,
694 GDK_KP_Home, GDK_MOD1_MASK,
698 _gtk_file_chooser_install_properties (gobject_class);
700 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
701 P_("Default file chooser backend"),
702 P_("Name of the GtkFileChooser backend to use by default"),
704 GTK_PARAM_READWRITE));
708 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
710 iface->select_path = gtk_file_chooser_default_select_path;
711 iface->unselect_path = gtk_file_chooser_default_unselect_path;
712 iface->select_all = gtk_file_chooser_default_select_all;
713 iface->unselect_all = gtk_file_chooser_default_unselect_all;
714 iface->get_paths = gtk_file_chooser_default_get_paths;
715 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
716 iface->get_file_system = gtk_file_chooser_default_get_file_system;
717 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
718 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
719 iface->set_current_name = gtk_file_chooser_default_set_current_name;
720 iface->add_filter = gtk_file_chooser_default_add_filter;
721 iface->remove_filter = gtk_file_chooser_default_remove_filter;
722 iface->list_filters = gtk_file_chooser_default_list_filters;
723 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
724 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
725 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
729 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
731 iface->get_default_size = gtk_file_chooser_default_get_default_size;
732 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
733 iface->should_respond = gtk_file_chooser_default_should_respond;
734 iface->initial_focus = gtk_file_chooser_default_initial_focus;
737 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
739 profile_start ("start", NULL);
741 impl->local_only = TRUE;
742 impl->preview_widget_active = TRUE;
743 impl->use_preview_label = TRUE;
744 impl->select_multiple = FALSE;
745 impl->show_hidden = FALSE;
746 impl->icon_size = FALLBACK_ICON_SIZE;
747 impl->load_state = LOAD_EMPTY;
748 impl->pending_select_paths = NULL;
750 gtk_box_set_spacing (GTK_BOX (impl), 12);
752 impl->tooltips = gtk_tooltips_new ();
753 g_object_ref (impl->tooltips);
754 gtk_object_sink (GTK_OBJECT (impl->tooltips));
756 profile_end ("end", NULL);
759 /* Frees the data columns for the specified iter in the shortcuts model*/
761 shortcuts_free_row_data (GtkFileChooserDefault *impl,
767 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
768 SHORTCUTS_COL_DATA, &col_data,
769 SHORTCUTS_COL_IS_VOLUME, &is_volume,
776 GtkFileSystemVolume *volume;
779 gtk_file_system_volume_free (impl->file_system, volume);
786 gtk_file_path_free (path);
790 /* Frees all the data columns in the shortcuts model */
792 shortcuts_free (GtkFileChooserDefault *impl)
796 if (!impl->shortcuts_model)
799 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
802 shortcuts_free_row_data (impl, &iter);
804 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
806 g_object_unref (impl->shortcuts_model);
807 impl->shortcuts_model = NULL;
811 pending_select_paths_free (GtkFileChooserDefault *impl)
815 for (l = impl->pending_select_paths; l; l = l->next)
820 gtk_file_path_free (path);
823 g_slist_free (impl->pending_select_paths);
824 impl->pending_select_paths = NULL;
828 pending_select_paths_add (GtkFileChooserDefault *impl,
829 const GtkFilePath *path)
831 impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
834 /* Used from gtk_tree_selection_selected_foreach() */
836 store_selection_foreach (GtkTreeModel *model,
841 GtkFileChooserDefault *impl;
842 GtkTreeIter child_iter;
843 const GtkFilePath *file_path;
845 impl = GTK_FILE_CHOOSER_DEFAULT (data);
847 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
849 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
850 pending_select_paths_add (impl, file_path);
853 /* Stores the current selection in the list of paths to select; this is used to
854 * preserve the selection when reloading the current folder.
857 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
859 GtkTreeSelection *selection;
861 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
862 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
866 gtk_file_chooser_default_finalize (GObject *object)
868 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
871 if (impl->shortcuts_filter_model)
872 g_object_unref (impl->shortcuts_filter_model);
874 shortcuts_free (impl);
876 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
877 impl->volumes_changed_id = 0;
878 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
879 impl->bookmarks_changed_id = 0;
880 g_object_unref (impl->file_system);
882 for (l = impl->filters; l; l = l->next)
884 GtkFileFilter *filter;
886 filter = GTK_FILE_FILTER (l->data);
887 g_object_unref (filter);
889 g_slist_free (impl->filters);
891 if (impl->current_filter)
892 g_object_unref (impl->current_filter);
894 if (impl->current_volume_path)
895 gtk_file_path_free (impl->current_volume_path);
897 if (impl->current_folder)
898 gtk_file_path_free (impl->current_folder);
900 if (impl->preview_path)
901 gtk_file_path_free (impl->preview_path);
903 pending_select_paths_free (impl);
905 load_remove_timer (impl);
907 /* Free all the Models we have */
908 if (impl->browse_files_model)
909 g_object_unref (impl->browse_files_model);
911 if (impl->sort_model)
912 g_object_unref (impl->sort_model);
914 g_free (impl->preview_display_name);
916 g_free (impl->edited_new_text);
918 g_object_unref (impl->tooltips);
920 G_OBJECT_CLASS (parent_class)->finalize (object);
923 /* Shows an error dialog set as transient for the specified window */
925 error_message_with_parent (GtkWindow *parent,
931 dialog = gtk_message_dialog_new (parent,
932 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
937 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
939 gtk_dialog_run (GTK_DIALOG (dialog));
940 gtk_widget_destroy (dialog);
943 /* Returns a toplevel GtkWindow, or NULL if none */
945 get_toplevel (GtkWidget *widget)
949 toplevel = gtk_widget_get_toplevel (widget);
950 if (!GTK_WIDGET_TOPLEVEL (toplevel))
953 return GTK_WINDOW (toplevel);
956 /* Shows an error dialog for the file chooser */
958 error_message (GtkFileChooserDefault *impl,
962 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
965 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
967 error_dialog (GtkFileChooserDefault *impl,
969 const GtkFilePath *path,
978 uri = gtk_file_system_path_to_uri (impl->file_system, path);
979 text = g_strdup_printf (msg, uri);
980 error_message (impl, text, error->message);
983 g_error_free (error);
987 /* Displays an error message about not being able to get information for a file.
988 * Frees the GError as well.
991 error_getting_info_dialog (GtkFileChooserDefault *impl,
992 const GtkFilePath *path,
996 _("Could not retrieve information about the file"),
1000 /* Shows an error dialog about not being able to add a bookmark */
1002 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
1003 const GtkFilePath *path,
1007 _("Could not add a bookmark"),
1011 /* Shows an error dialog about not being able to remove a bookmark */
1013 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
1014 const GtkFilePath *path,
1018 _("Could not remove bookmark"),
1022 /* Shows an error dialog about not being able to create a folder */
1024 error_creating_folder_dialog (GtkFileChooserDefault *impl,
1025 const GtkFilePath *path,
1029 _("The folder could not be created"),
1033 /* Shows an error about not being able to create a folder because a file with
1034 * the same name is already there.
1037 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
1038 const GtkFilePath *path,
1042 _("The folder could not be created, as a file with the same name "
1043 "already exists. Try using a different name for the folder, "
1044 "or rename the file first."),
1048 /* Shows an error dialog about not being able to create a filename */
1050 error_building_filename_dialog (GtkFileChooserDefault *impl,
1051 const GtkFilePath *folder_part,
1052 const char *file_part,
1055 error_dialog (impl, _("Invalid file name"),
1059 /* Shows an error dialog when we cannot switch to a folder */
1061 error_changing_folder_dialog (GtkFileChooserDefault *impl,
1062 const GtkFilePath *path,
1065 error_dialog (impl, _("The folder contents could not be displayed"),
1069 /* Changes folders, displaying an error dialog if this fails */
1071 change_folder_and_display_error (GtkFileChooserDefault *impl,
1072 const GtkFilePath *path)
1076 GtkFilePath *path_copy;
1078 g_return_val_if_fail (path != NULL, FALSE);
1080 profile_start ("start", (char *) path);
1082 /* We copy the path because of this case:
1084 * list_row_activated()
1085 * fetches path from model; path belongs to the model (*)
1086 * calls change_folder_and_display_error()
1087 * calls _gtk_file_chooser_set_current_folder_path()
1088 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1091 path_copy = gtk_file_path_copy (path);
1094 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, &error);
1097 error_changing_folder_dialog (impl, path_copy, error);
1099 gtk_file_path_free (path_copy);
1101 profile_end ("end", (char *) path);
1107 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1109 if (impl->use_preview_label)
1111 if (!impl->preview_label)
1113 impl->preview_label = gtk_label_new (impl->preview_display_name);
1114 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1115 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1116 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1117 gtk_widget_show (impl->preview_label);
1122 if (impl->preview_label)
1124 gtk_widget_destroy (impl->preview_label);
1125 impl->preview_label = NULL;
1129 if (impl->preview_widget_active && impl->preview_widget)
1130 gtk_widget_show (impl->preview_box);
1132 gtk_widget_hide (impl->preview_box);
1134 g_signal_emit_by_name (impl, "default-size-changed");
1138 set_preview_widget (GtkFileChooserDefault *impl,
1139 GtkWidget *preview_widget)
1141 if (preview_widget == impl->preview_widget)
1144 if (impl->preview_widget)
1145 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1146 impl->preview_widget);
1148 impl->preview_widget = preview_widget;
1149 if (impl->preview_widget)
1151 gtk_widget_show (impl->preview_widget);
1152 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1153 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1154 impl->preview_widget,
1155 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1158 update_preview_widget_visibility (impl);
1161 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1163 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1167 profile_start ("start", NULL);
1169 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1175 gboolean pixbuf_visible;
1178 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1179 SHORTCUTS_COL_DATA, &data,
1180 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1181 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1184 if (pixbuf_visible && data)
1188 GtkFileSystemVolume *volume;
1191 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1192 impl->icon_size, NULL);
1196 const GtkFilePath *path;
1199 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1200 impl->icon_size, NULL);
1203 gtk_list_store_set (impl->shortcuts_model, &iter,
1204 SHORTCUTS_COL_PIXBUF, pixbuf,
1207 g_object_unref (pixbuf);
1209 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1213 profile_end ("end", NULL);
1217 shortcuts_find_folder (GtkFileChooserDefault *impl,
1218 GtkFilePath *folder)
1220 GtkTreeSelection *selection;
1224 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1226 g_assert (folder != NULL);
1227 pos = shortcut_find_position (impl, folder);
1230 gtk_tree_selection_unselect_all (selection);
1234 path = gtk_tree_path_new_from_indices (pos, -1);
1235 gtk_tree_selection_select_path (selection, path);
1236 gtk_tree_path_free (path);
1239 /* If a shortcut corresponds to the current folder, selects it */
1241 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1243 shortcuts_find_folder (impl, impl->current_folder);
1246 /* Convenience function to get the display name and icon info for a path */
1247 static GtkFileInfo *
1248 get_file_info (GtkFileSystem *file_system,
1249 const GtkFilePath *path,
1253 GtkFilePath *parent_path;
1254 GtkFileFolder *parent_folder;
1258 profile_start ("start", (char *) path);
1263 if (!gtk_file_system_get_parent (file_system, path, &parent_path, &tmp))
1266 parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
1267 GTK_FILE_INFO_DISPLAY_NAME
1268 | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
1273 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, &tmp);
1274 g_object_unref (parent_folder);
1278 gtk_file_path_free (parent_path);
1283 GTK_FILE_CHOOSER_ERROR,
1284 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
1285 _("Could not get information about '%s': %s"),
1286 gtk_file_path_get_string (path),
1291 profile_end ("end", (char *) path);
1296 /* Returns whether a path is a folder */
1298 check_is_folder (GtkFileSystem *file_system,
1299 const GtkFilePath *path,
1302 GtkFileFolder *folder;
1304 profile_start ("start", (char *) path);
1306 folder = gtk_file_system_get_folder (file_system, path, 0, error);
1309 profile_end ("end - is not folder", (char *) path);
1313 g_object_unref (folder);
1315 profile_end ("end", (char *) path);
1319 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1320 * inserts a volume. A position of -1 indicates the end of the tree.
1323 shortcuts_insert_path (GtkFileChooserDefault *impl,
1326 GtkFileSystemVolume *volume,
1327 const GtkFilePath *path,
1337 profile_start ("start", is_volume ? "volume" : (char *) path);
1342 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1343 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1344 impl->icon_size, NULL);
1348 if (!check_is_folder (impl->file_system, path, error))
1350 profile_end ("end - is not folder", NULL);
1355 label_copy = g_strdup (label);
1358 GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
1362 profile_end ("end - could not get info", (char *) path);
1366 label_copy = g_strdup (gtk_file_info_get_display_name (info));
1367 gtk_file_info_free (info);
1370 data = gtk_file_path_copy (path);
1371 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1372 impl->icon_size, NULL);
1376 gtk_list_store_append (impl->shortcuts_model, &iter);
1378 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1380 gtk_list_store_set (impl->shortcuts_model, &iter,
1381 SHORTCUTS_COL_PIXBUF, pixbuf,
1382 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1383 SHORTCUTS_COL_NAME, label_copy,
1384 SHORTCUTS_COL_DATA, data,
1385 SHORTCUTS_COL_IS_VOLUME, is_volume,
1386 SHORTCUTS_COL_REMOVABLE, removable,
1389 g_free (label_copy);
1392 g_object_unref (pixbuf);
1394 profile_end ("end", NULL);
1399 /* Appends an item for the user's home directory to the shortcuts model */
1401 shortcuts_append_home (GtkFileChooserDefault *impl)
1404 GtkFilePath *home_path;
1407 profile_start ("start", NULL);
1409 home = g_get_home_dir ();
1412 profile_end ("end - no home directory!?", NULL);
1416 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1419 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1420 if (!impl->has_home)
1421 error_getting_info_dialog (impl, home_path, error);
1423 gtk_file_path_free (home_path);
1425 profile_end ("end", NULL);
1428 /* Appends the ~/Desktop directory to the shortcuts model */
1430 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1435 profile_start ("start", NULL);
1438 name = _gtk_file_system_win32_get_desktop ();
1440 const char *home = g_get_home_dir ();
1443 profile_end ("end - no home directory!?", NULL);
1447 name = g_build_filename (home, "Desktop", NULL);
1450 path = gtk_file_system_filename_to_path (impl->file_system, name);
1453 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1454 /* We do not actually pop up an error dialog if there is no desktop directory
1455 * because some people may really not want to have one.
1458 gtk_file_path_free (path);
1460 profile_end ("end", NULL);
1463 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1465 shortcuts_append_paths (GtkFileChooserDefault *impl,
1472 profile_start ("start", NULL);
1474 /* As there is no separator now, we want to start there.
1476 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1479 for (; paths; paths = paths->next)
1487 if (impl->local_only &&
1488 !gtk_file_system_path_is_local (impl->file_system, path))
1491 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
1493 /* NULL GError, but we don't really want to show error boxes here */
1494 if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, label, TRUE, NULL))
1500 profile_end ("end", NULL);
1502 return num_inserted;
1505 /* Returns the index for the corresponding item in the shortcuts bar */
1507 shortcuts_get_index (GtkFileChooserDefault *impl,
1508 ShortcutsIndex where)
1514 if (where == SHORTCUTS_HOME)
1517 n += impl->has_home ? 1 : 0;
1519 if (where == SHORTCUTS_DESKTOP)
1522 n += impl->has_desktop ? 1 : 0;
1524 if (where == SHORTCUTS_VOLUMES)
1527 n += impl->num_volumes;
1529 if (where == SHORTCUTS_SHORTCUTS)
1532 n += impl->num_shortcuts;
1534 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1537 /* If there are no bookmarks there won't be a separator */
1538 n += (impl->num_bookmarks > 0) ? 1 : 0;
1540 if (where == SHORTCUTS_BOOKMARKS)
1543 n += impl->num_bookmarks;
1545 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1550 if (where == SHORTCUTS_CURRENT_FOLDER)
1553 g_assert_not_reached ();
1560 /* Removes the specified number of rows from the shortcuts list */
1562 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1568 path = gtk_tree_path_new_from_indices (start_row, -1);
1570 for (; n_rows; n_rows--)
1574 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1575 g_assert_not_reached ();
1577 shortcuts_free_row_data (impl, &iter);
1578 gtk_list_store_remove (impl->shortcuts_model, &iter);
1581 gtk_tree_path_free (path);
1584 /* Adds all the file system volumes to the shortcuts model */
1586 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1591 gboolean old_changing_folders;
1593 profile_start ("start", NULL);
1595 old_changing_folders = impl->changing_folder;
1596 impl->changing_folder = TRUE;
1598 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1599 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1600 impl->num_volumes = 0;
1602 list = gtk_file_system_list_volumes (impl->file_system);
1606 for (l = list; l; l = l->next)
1608 GtkFileSystemVolume *volume;
1612 if (impl->local_only)
1614 GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1615 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1616 gtk_file_path_free (base_path);
1620 gtk_file_system_volume_free (impl->file_system, volume);
1625 if (shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL))
1628 gtk_file_system_volume_free (impl->file_system, volume);
1631 impl->num_volumes = n;
1632 g_slist_free (list);
1634 if (impl->shortcuts_filter_model)
1635 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1637 impl->changing_folder = old_changing_folders;
1639 profile_end ("end", NULL);
1642 /* Inserts a separator node in the shortcuts list */
1644 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1645 ShortcutsIndex where)
1649 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1651 gtk_list_store_insert (impl->shortcuts_model, &iter,
1652 shortcuts_get_index (impl, where));
1653 gtk_list_store_set (impl->shortcuts_model, &iter,
1654 SHORTCUTS_COL_PIXBUF, NULL,
1655 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1656 SHORTCUTS_COL_NAME, NULL,
1657 SHORTCUTS_COL_DATA, NULL,
1661 /* Updates the list of bookmarks */
1663 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1666 gboolean old_changing_folders;
1668 GtkFilePath *list_selected = NULL;
1669 GtkFilePath *combo_selected = NULL;
1673 profile_start ("start", NULL);
1675 old_changing_folders = impl->changing_folder;
1676 impl->changing_folder = TRUE;
1678 if (shortcuts_get_selected (impl, &iter))
1680 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1682 SHORTCUTS_COL_DATA, &col_data,
1683 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1686 if (col_data && !is_volume)
1687 list_selected = gtk_file_path_copy (col_data);
1690 if (impl->save_folder_combo &&
1691 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
1694 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1696 SHORTCUTS_COL_DATA, &col_data,
1697 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1700 if (col_data && !is_volume)
1701 combo_selected = gtk_file_path_copy (col_data);
1704 if (impl->num_bookmarks > 0)
1705 shortcuts_remove_rows (impl,
1706 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1707 impl->num_bookmarks + 1);
1709 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1710 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1711 gtk_file_paths_free (bookmarks);
1713 if (impl->num_bookmarks > 0)
1714 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1716 if (impl->shortcuts_filter_model)
1717 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1721 shortcuts_find_folder (impl, list_selected);
1722 gtk_file_path_free (list_selected);
1729 pos = shortcut_find_position (impl, combo_selected);
1731 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
1733 gtk_file_path_free (combo_selected);
1736 impl->changing_folder = old_changing_folders;
1738 profile_end ("end", NULL);
1741 /* Appends a separator and a row to the shortcuts list for the current folder */
1743 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1748 g_assert (!impl->shortcuts_current_folder_active);
1752 g_assert (impl->current_folder != NULL);
1754 pos = shortcut_find_position (impl, impl->current_folder);
1757 GtkFileSystemVolume *volume;
1758 GtkFilePath *base_path;
1762 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1766 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1768 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1770 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1775 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1777 success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1782 success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1785 gtk_file_system_volume_free (impl->file_system, volume);
1788 gtk_file_path_free (base_path);
1791 shortcuts_remove_rows (impl, pos - 1, 1); /* remove the separator */
1793 impl->shortcuts_current_folder_active = success;
1797 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1800 /* Updates the current folder row in the shortcuts model */
1802 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1806 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1808 if (impl->shortcuts_current_folder_active)
1810 shortcuts_remove_rows (impl, pos, 2);
1811 impl->shortcuts_current_folder_active = FALSE;
1814 shortcuts_add_current_folder (impl);
1817 /* Filter function used for the shortcuts filter model */
1819 shortcuts_filter_cb (GtkTreeModel *model,
1823 GtkFileChooserDefault *impl;
1827 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1829 path = gtk_tree_model_get_path (model, iter);
1833 pos = *gtk_tree_path_get_indices (path);
1834 gtk_tree_path_free (path);
1836 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1839 /* Creates the list model for shortcuts */
1841 shortcuts_model_create (GtkFileChooserDefault *impl)
1843 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1844 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1845 GDK_TYPE_PIXBUF, /* pixbuf */
1846 G_TYPE_STRING, /* name */
1847 G_TYPE_POINTER, /* path or volume */
1848 G_TYPE_BOOLEAN, /* is the previous column a volume? */
1849 G_TYPE_BOOLEAN, /* removable */
1850 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1852 if (impl->file_system)
1854 shortcuts_append_home (impl);
1855 shortcuts_append_desktop (impl);
1856 shortcuts_add_volumes (impl);
1857 shortcuts_add_bookmarks (impl);
1860 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1861 GTK_TREE_MODEL (impl->shortcuts_model),
1864 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1865 shortcuts_filter_cb,
1870 /* Callback used when the "New Folder" button is clicked */
1872 new_folder_button_clicked (GtkButton *button,
1873 GtkFileChooserDefault *impl)
1878 if (!impl->browse_files_model)
1879 return; /* FIXME: this sucks. Disable the New Folder button or something. */
1881 /* Prevent button from being clicked twice */
1882 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
1884 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1886 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1887 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1888 path, impl->list_name_column,
1891 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1892 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1894 impl->list_name_column,
1897 gtk_tree_path_free (path);
1900 /* Idle handler for creating a new folder after editing its name cell, or for
1901 * canceling the editing.
1904 edited_idle_cb (GtkFileChooserDefault *impl)
1906 GDK_THREADS_ENTER ();
1908 g_source_destroy (impl->edited_idle);
1909 impl->edited_idle = NULL;
1911 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1912 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1914 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
1916 if (impl->edited_new_text) /* not cancelled? */
1919 GtkFilePath *file_path;
1922 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, impl->edited_new_text,
1927 if (gtk_file_system_create_folder (impl->file_system, file_path, &error))
1928 change_folder_and_display_error (impl, file_path);
1930 error_creating_folder_dialog (impl, file_path, error);
1932 gtk_file_path_free (file_path);
1935 error_creating_folder_dialog (impl, file_path, error);
1937 g_free (impl->edited_new_text);
1938 impl->edited_new_text = NULL;
1941 GDK_THREADS_LEAVE ();
1947 queue_edited_idle (GtkFileChooserDefault *impl,
1948 const gchar *new_text)
1950 /* We create the folder in an idle handler so that we don't modify the tree
1954 g_assert (!impl->edited_idle);
1955 g_assert (!impl->edited_new_text);
1957 impl->edited_idle = g_idle_source_new ();
1958 g_source_set_closure (impl->edited_idle,
1959 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
1961 g_source_attach (impl->edited_idle, NULL);
1964 impl->edited_new_text = g_strdup (new_text);
1967 /* Callback used from the text cell renderer when the new folder is named */
1969 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1971 const gchar *new_text,
1972 GtkFileChooserDefault *impl)
1974 /* work around bug #154921 */
1975 g_object_set (cell_renderer_text,
1976 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1977 queue_edited_idle (impl, new_text);
1980 /* Callback used from the text cell renderer when the new folder edition gets
1984 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1985 GtkFileChooserDefault *impl)
1987 /* work around bug #154921 */
1988 g_object_set (cell_renderer_text,
1989 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
1990 queue_edited_idle (impl, NULL);
1993 /* Creates the widgets for the filter combo box */
1995 filter_create (GtkFileChooserDefault *impl)
1997 impl->filter_combo = gtk_combo_box_new_text ();
1998 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2000 g_signal_connect (impl->filter_combo, "changed",
2001 G_CALLBACK (filter_combo_changed), impl);
2003 return impl->filter_combo;
2007 button_new (GtkFileChooserDefault *impl,
2009 const char *stock_id,
2017 button = gtk_button_new_with_mnemonic (text);
2018 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2019 gtk_button_set_image (GTK_BUTTON (button), image);
2021 gtk_widget_set_sensitive (button, sensitive);
2022 g_signal_connect (button, "clicked", callback, impl);
2025 gtk_widget_show (button);
2030 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2032 shortcut_find_position (GtkFileChooserDefault *impl,
2033 const GtkFilePath *path)
2037 int current_folder_separator_idx;
2039 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2042 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2044 for (i = 0; i < current_folder_separator_idx; i++)
2049 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2050 SHORTCUTS_COL_DATA, &col_data,
2051 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2058 GtkFileSystemVolume *volume;
2059 GtkFilePath *base_path;
2063 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2065 exists = base_path && strcmp (gtk_file_path_get_string (path),
2066 gtk_file_path_get_string (base_path)) == 0;
2074 GtkFilePath *model_path;
2076 model_path = col_data;
2078 if (model_path && gtk_file_path_compare (model_path, path) == 0)
2083 gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2089 /* Tries to add a bookmark from a path name */
2091 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2092 const GtkFilePath *path,
2097 g_return_val_if_fail (path != NULL, FALSE);
2099 if (shortcut_find_position (impl, path) != -1)
2102 /* FIXME: this check really belongs in gtk_file_system_insert_bookmark. */
2104 if (!check_is_folder (impl->file_system, path, &error))
2106 error_adding_bookmark_dialog (impl, path, error);
2111 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2113 error_adding_bookmark_dialog (impl, path, error);
2121 add_bookmark_foreach_cb (GtkTreeModel *model,
2126 GtkFileChooserDefault *impl;
2127 GtkFileSystemModel *fs_model;
2128 GtkTreeIter child_iter;
2129 const GtkFilePath *file_path;
2131 impl = (GtkFileChooserDefault *) data;
2133 fs_model = impl->browse_files_model;
2134 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2136 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2137 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2140 /* Adds a bookmark from the currently selected item in the file list */
2142 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2144 GtkTreeSelection *selection;
2146 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2148 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2149 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2151 gtk_tree_selection_selected_foreach (selection,
2152 add_bookmark_foreach_cb,
2156 /* Callback used when the "Add bookmark" button is clicked */
2158 add_bookmark_button_clicked_cb (GtkButton *button,
2159 GtkFileChooserDefault *impl)
2161 bookmarks_add_selected_folder (impl);
2164 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2165 * returns FALSE if no shortcut is selected.
2168 shortcuts_get_selected (GtkFileChooserDefault *impl,
2171 GtkTreeSelection *selection;
2172 GtkTreeIter parent_iter;
2174 if (!impl->browse_shortcuts_tree_view)
2177 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2179 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2182 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2188 /* Removes the selected bookmarks */
2190 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2198 if (!shortcuts_get_selected (impl, &iter))
2201 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2202 SHORTCUTS_COL_DATA, &col_data,
2203 SHORTCUTS_COL_REMOVABLE, &removable,
2205 g_assert (col_data != NULL);
2213 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2214 error_removing_bookmark_dialog (impl, path, error);
2217 /* Callback used when the "Remove bookmark" button is clicked */
2219 remove_bookmark_button_clicked_cb (GtkButton *button,
2220 GtkFileChooserDefault *impl)
2222 remove_selected_bookmarks (impl);
2225 struct selection_check_closure {
2226 GtkFileChooserDefault *impl;
2229 gboolean all_folders;
2232 /* Used from gtk_tree_selection_selected_foreach() */
2234 selection_check_foreach_cb (GtkTreeModel *model,
2239 struct selection_check_closure *closure;
2240 GtkTreeIter child_iter;
2241 const GtkFileInfo *info;
2245 closure->num_selected++;
2247 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2249 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2250 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2252 closure->all_folders = closure->all_folders && is_folder;
2253 closure->all_files = closure->all_files && !is_folder;
2256 /* Checks whether the selected items in the file list are all files or all folders */
2258 selection_check (GtkFileChooserDefault *impl,
2260 gboolean *all_files,
2261 gboolean *all_folders)
2263 struct selection_check_closure closure;
2264 GtkTreeSelection *selection;
2266 closure.impl = impl;
2267 closure.num_selected = 0;
2268 closure.all_files = TRUE;
2269 closure.all_folders = TRUE;
2271 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2272 gtk_tree_selection_selected_foreach (selection,
2273 selection_check_foreach_cb,
2276 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2279 *num_selected = closure.num_selected;
2282 *all_files = closure.all_files;
2285 *all_folders = closure.all_folders;
2288 struct get_selected_path_closure {
2289 GtkFileChooserDefault *impl;
2290 const GtkFilePath *path;
2294 get_selected_path_foreach_cb (GtkTreeModel *model,
2299 struct get_selected_path_closure *closure;
2300 GtkTreeIter child_iter;
2304 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2305 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2308 /* Returns a selected path from the file list */
2309 static const GtkFilePath *
2310 get_selected_path (GtkFileChooserDefault *impl)
2312 struct get_selected_path_closure closure;
2313 GtkTreeSelection *selection;
2315 closure.impl = impl;
2316 closure.path = NULL;
2318 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2319 gtk_tree_selection_selected_foreach (selection,
2320 get_selected_path_foreach_cb,
2323 return closure.path;
2327 GtkFileChooserDefault *impl;
2329 } UpdateTooltipData;
2332 update_tooltip (GtkTreeModel *model,
2337 UpdateTooltipData *udata = data;
2338 GtkTreeIter child_iter;
2339 const GtkFileInfo *info;
2341 if (udata->tip == NULL)
2343 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2347 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2348 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2349 gtk_file_info_get_display_name (info));
2354 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2355 * if there are no selected items *and* the current folder is not in the
2356 * bookmarks list. De-sensitize the button otherwise.
2359 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2362 gboolean all_folders;
2366 selection_check (impl, &num_selected, NULL, &all_folders);
2368 if (num_selected == 0)
2369 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2370 else if (num_selected == 1)
2372 const GtkFilePath *path;
2374 path = get_selected_path (impl);
2375 active = all_folders && (shortcut_find_position (impl, path) == -1);
2378 active = all_folders;
2380 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2382 if (impl->browse_files_popup_menu_add_shortcut_item)
2383 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2384 (num_selected == 0) ? FALSE : active);
2388 if (num_selected == 0)
2389 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2390 else if (num_selected > 1)
2391 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2394 GtkTreeSelection *selection;
2395 UpdateTooltipData data;
2397 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2400 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2404 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2409 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2410 * bookmark row is selected in the shortcuts tree.
2413 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2416 gboolean removable = FALSE;
2419 if (shortcuts_get_selected (impl, &iter))
2420 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2421 SHORTCUTS_COL_REMOVABLE, &removable,
2422 SHORTCUTS_COL_NAME, &name,
2425 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2431 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2432 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2441 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2444 gboolean removable = FALSE;
2446 if (impl->browse_shortcuts_popup_menu == NULL)
2449 if (shortcuts_get_selected (impl, &iter))
2450 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2451 SHORTCUTS_COL_REMOVABLE, &removable,
2454 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2455 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2458 /* GtkWidget::drag-begin handler for the shortcuts list. */
2460 shortcuts_drag_begin_cb (GtkWidget *widget,
2461 GdkDragContext *context,
2462 GtkFileChooserDefault *impl)
2465 impl->shortcuts_drag_context = g_object_ref (context);
2470 /* Removes the idle handler for outside drags */
2472 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2474 if (!impl->shortcuts_drag_outside_idle)
2477 g_source_destroy (impl->shortcuts_drag_outside_idle);
2478 impl->shortcuts_drag_outside_idle = NULL;
2482 /* GtkWidget::drag-end handler for the shortcuts list. */
2484 shortcuts_drag_end_cb (GtkWidget *widget,
2485 GdkDragContext *context,
2486 GtkFileChooserDefault *impl)
2489 g_object_unref (impl->shortcuts_drag_context);
2491 shortcuts_cancel_drag_outside_idle (impl);
2493 if (!impl->shortcuts_drag_outside)
2496 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2498 impl->shortcuts_drag_outside = FALSE;
2502 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2504 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2505 GdkDragContext *context,
2506 GtkFileChooserDefault *impl)
2508 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2512 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2516 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2519 GtkTreeView *tree_view;
2522 GdkPixmap *row_pixmap;
2527 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2529 /* Find the selected path and get its drag pixmap */
2531 if (!shortcuts_get_selected (impl, &iter))
2532 g_assert_not_reached ();
2534 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2536 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2537 gtk_tree_path_free (path);
2546 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2552 GdkPixmap *composite;
2553 int row_pixmap_width, row_pixmap_height;
2554 int pixbuf_width, pixbuf_height;
2555 int composite_width, composite_height;
2556 int pixbuf_x, pixbuf_y;
2557 GdkGC *gc, *mask_gc;
2559 GdkBitmap *pixbuf_mask;
2561 /* Create pixmap and mask for composite image */
2563 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2564 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2565 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2567 composite_width = MAX (row_pixmap_width, pixbuf_width);
2568 composite_height = MAX (row_pixmap_height, pixbuf_height);
2570 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2572 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2575 pixbuf_x = composite_width - pixbuf_width;
2577 pixbuf_y = (composite_height - pixbuf_height) / 2;
2579 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2580 gc = gdk_gc_new (composite);
2582 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2583 mask_gc = gdk_gc_new (mask);
2585 gdk_gc_set_foreground (mask_gc, &color);
2586 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2589 color.green = 0xffff;
2590 color.blue = 0xffff;
2591 gdk_gc_set_rgb_fg_color (gc, &color);
2592 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2594 /* Composite the row pixmap and the pixbuf */
2596 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2598 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2599 NULL, &pixbuf_mask, 128);
2600 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2603 pixbuf_width, pixbuf_height);
2604 g_object_unref (pixbuf_mask);
2606 gdk_draw_drawable (composite, gc, row_pixmap,
2609 row_pixmap_width, row_pixmap_height);
2611 gdk_gc_set_foreground (mask_gc, &color);
2612 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2614 gdk_draw_pixbuf (composite, gc, pixbuf,
2617 pixbuf_width, pixbuf_height,
2621 g_object_unref (pixbuf);
2622 g_object_unref (row_pixmap);
2624 row_pixmap = composite;
2628 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2630 gtk_tree_view_get_path_at_pos (tree_view,
2631 tree_view->priv->press_start_x,
2632 tree_view->priv->press_start_y,
2638 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2639 gdk_drawable_get_colormap (row_pixmap),
2642 tree_view->priv->press_start_x + 1,
2643 row_pixmap_y + cell_y + 1);
2645 g_object_unref (row_pixmap);
2647 g_object_unref (mask);
2650 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2651 * handler so that we can tell apart the drag_leave event that comes right
2652 * before a drag_drop, from a normal drag_leave. We don't want to set the
2653 * cursor nor the flag in the latter case.
2656 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2658 GDK_THREADS_ENTER ();
2660 shortcuts_drag_set_delete_cursor (impl, TRUE);
2661 impl->shortcuts_drag_outside = TRUE;
2663 shortcuts_cancel_drag_outside_idle (impl);
2665 GDK_THREADS_LEAVE ();
2671 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2675 shortcuts_drag_leave_cb (GtkWidget *widget,
2676 GdkDragContext *context,
2678 GtkFileChooserDefault *impl)
2681 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2683 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2684 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2685 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2687 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2691 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2693 GTK_TREE_VIEW_DROP_BEFORE);
2695 g_signal_stop_emission_by_name (widget, "drag-leave");
2698 /* Computes the appropriate row and position for dropping */
2700 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2704 GtkTreeViewDropPosition *pos)
2706 GtkTreeView *tree_view;
2707 GtkTreeViewColumn *column;
2711 int bookmarks_index;
2713 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2715 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2717 if (!gtk_tree_view_get_path_at_pos (tree_view,
2719 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2725 row = bookmarks_index + impl->num_bookmarks - 1;
2726 *path = gtk_tree_path_new_from_indices (row, -1);
2727 *pos = GTK_TREE_VIEW_DROP_AFTER;
2731 row = *gtk_tree_path_get_indices (*path);
2732 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2733 gtk_tree_path_free (*path);
2735 if (row < bookmarks_index)
2737 row = bookmarks_index;
2738 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2740 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2742 row = bookmarks_index + impl->num_bookmarks - 1;
2743 *pos = GTK_TREE_VIEW_DROP_AFTER;
2747 if (cell_y < cell.height / 2)
2748 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2750 *pos = GTK_TREE_VIEW_DROP_AFTER;
2753 *path = gtk_tree_path_new_from_indices (row, -1);
2756 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2757 * implement the destination side of DnD by hand, due to limitations in
2758 * GtkTreeView's DnD API.
2761 shortcuts_drag_motion_cb (GtkWidget *widget,
2762 GdkDragContext *context,
2766 GtkFileChooserDefault *impl)
2769 GtkTreeViewDropPosition pos;
2770 GdkDragAction action;
2773 if (gtk_drag_get_source_widget (context) == widget)
2775 shortcuts_cancel_drag_outside_idle (impl);
2777 if (impl->shortcuts_drag_outside)
2779 shortcuts_drag_set_delete_cursor (impl, FALSE);
2780 impl->shortcuts_drag_outside = FALSE;
2785 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2786 action = GDK_ACTION_COPY;
2787 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2788 action = GDK_ACTION_MOVE;
2795 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2796 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2797 gtk_tree_path_free (path);
2801 g_signal_stop_emission_by_name (widget, "drag-motion");
2805 gdk_drag_status (context, action, time_);
2812 /* GtkWidget::drag-drop handler for the shortcuts list. */
2814 shortcuts_drag_drop_cb (GtkWidget *widget,
2815 GdkDragContext *context,
2819 GtkFileChooserDefault *impl)
2822 shortcuts_cancel_drag_outside_idle (impl);
2825 g_signal_stop_emission_by_name (widget, "drag-drop");
2829 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2831 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2838 uris = g_uri_list_extract_uris (data);
2840 for (i = 0; uris[i]; i++)
2846 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2850 if (shortcuts_add_bookmark_from_path (impl, path, position))
2853 gtk_file_path_free (path);
2859 g_set_error (&error,
2860 GTK_FILE_CHOOSER_ERROR,
2861 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
2862 _("Could not add a bookmark for '%s' "
2863 "because it is an invalid path name."),
2865 error_adding_bookmark_dialog (impl, path, error);
2872 /* Reorders the selected bookmark to the specified position */
2874 shortcuts_reorder (GtkFileChooserDefault *impl,
2882 int bookmarks_index;
2883 const GtkFilePath *file_path;
2884 GtkFilePath *file_path_copy;
2887 /* Get the selected path */
2889 if (!shortcuts_get_selected (impl, &iter))
2890 g_assert_not_reached ();
2892 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2893 old_position = *gtk_tree_path_get_indices (path);
2894 gtk_tree_path_free (path);
2896 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2897 old_position -= bookmarks_index;
2898 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2900 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2901 SHORTCUTS_COL_DATA, &col_data,
2902 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2904 g_assert (col_data != NULL);
2905 g_assert (!is_volume);
2907 file_path = col_data;
2908 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2910 /* Remove the path from the old position and insert it in the new one */
2912 if (new_position > old_position)
2915 if (old_position == new_position)
2919 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2920 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2922 error_adding_bookmark_dialog (impl, file_path_copy, error);
2926 gtk_file_path_free (file_path_copy);
2929 /* Callback used when we get the drag data for the bookmarks list. We add the
2930 * received URIs as bookmarks if they are folders.
2933 shortcuts_drag_data_received_cb (GtkWidget *widget,
2934 GdkDragContext *context,
2937 GtkSelectionData *selection_data,
2942 GtkFileChooserDefault *impl;
2943 GtkTreePath *tree_path;
2944 GtkTreeViewDropPosition tree_pos;
2946 int bookmarks_index;
2948 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2950 /* Compute position */
2952 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2954 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2955 position = *gtk_tree_path_get_indices (tree_path);
2956 gtk_tree_path_free (tree_path);
2958 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2961 g_assert (position >= bookmarks_index);
2962 position -= bookmarks_index;
2964 if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2965 shortcuts_drop_uris (impl, selection_data->data, position);
2966 else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2967 shortcuts_reorder (impl, position);
2969 g_signal_stop_emission_by_name (widget, "drag-data-received");
2972 /* Callback used when the selection in the shortcuts tree changes */
2974 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
2975 GtkFileChooserDefault *impl)
2977 bookmarks_check_remove_sensitivity (impl);
2978 shortcuts_check_popup_sensitivity (impl);
2982 shortcuts_row_separator_func (GtkTreeModel *model,
2986 gint column = GPOINTER_TO_INT (data);
2989 gtk_tree_model_get (model, iter, column, &text, -1);
2999 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3000 * keypresses before the TreeView gets them.
3003 tree_view_keybinding_cb (GtkWidget *tree_view,
3005 GtkFileChooserDefault *impl)
3007 if (event->keyval == GDK_slash &&
3008 ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3010 location_popup_handler (impl, "/");
3017 /* Callback used when the file list's popup menu is detached */
3019 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3022 GtkFileChooserDefault *impl;
3024 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3025 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3027 impl->browse_shortcuts_popup_menu = NULL;
3028 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3029 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3033 remove_shortcut_cb (GtkMenuItem *item,
3034 GtkFileChooserDefault *impl)
3036 remove_selected_bookmarks (impl);
3040 rename_shortcut_cb (GtkMenuItem *item,
3041 GtkFileChooserDefault *impl)
3045 GtkTreeViewColumn *column;
3046 GtkCellRenderer *cell;
3049 if (shortcuts_get_selected (impl, &iter))
3051 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3052 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3053 renderers = gtk_tree_view_column_get_cell_renderers (column);
3054 cell = g_list_nth_data (renderers, 1);
3055 g_list_free (renderers);
3056 g_object_set (cell, "editable", TRUE, NULL);
3057 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3058 path, column, cell, TRUE);
3059 gtk_tree_path_free (path);
3063 /* Constructs the popup menu for the file list if needed */
3065 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3069 if (impl->browse_shortcuts_popup_menu)
3072 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3073 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3074 impl->browse_shortcuts_tree_view,
3075 shortcuts_popup_menu_detach_cb);
3077 item = gtk_image_menu_item_new_with_label (_("Remove"));
3078 impl->browse_shortcuts_popup_menu_remove_item = item;
3079 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3080 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3081 g_signal_connect (item, "activate",
3082 G_CALLBACK (remove_shortcut_cb), impl);
3083 gtk_widget_show (item);
3084 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3086 item = gtk_menu_item_new_with_label (_("Rename..."));
3087 impl->browse_shortcuts_popup_menu_rename_item = item;
3088 g_signal_connect (item, "activate",
3089 G_CALLBACK (rename_shortcut_cb), impl);
3090 gtk_widget_show (item);
3091 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3093 shortcuts_check_popup_sensitivity (impl);
3097 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3099 shortcuts_build_popup_menu (impl);
3103 popup_position_func (GtkMenu *menu,
3107 gpointer user_data);
3110 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3111 GdkEventButton *event)
3113 shortcuts_update_popup_menu (impl);
3115 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3116 NULL, NULL, NULL, NULL,
3117 event->button, event->time);
3120 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3122 popup_position_func, impl->browse_shortcuts_tree_view,
3123 0, GDK_CURRENT_TIME);
3124 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3129 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3131 shortcuts_popup_menu_cb (GtkWidget *widget,
3132 GtkFileChooserDefault *impl)
3134 shortcuts_popup_menu (impl, NULL);
3138 /* Callback used when a button is pressed on the shortcuts list.
3139 * We trap button 3 to bring up a popup menu.
3142 shortcuts_button_press_event_cb (GtkWidget *widget,
3143 GdkEventButton *event,
3144 GtkFileChooserDefault *impl)
3146 if (event->button != 3)
3149 shortcuts_popup_menu (impl, event);
3154 shortcuts_edited (GtkCellRenderer *cell,
3157 GtkFileChooserDefault *impl)
3161 GtkFilePath *shortcut;
3163 g_object_set (cell, "editable", FALSE, NULL);
3165 path = gtk_tree_path_new_from_string (path_string);
3166 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path);
3167 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3168 SHORTCUTS_COL_DATA, &shortcut,
3170 gtk_tree_path_free (path);
3172 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3176 shortcuts_editing_canceled (GtkCellRenderer *cell,
3177 GtkFileChooserDefault *impl)
3179 g_object_set (cell, "editable", FALSE, NULL);
3182 /* Creates the widgets for the shortcuts and bookmarks tree */
3184 shortcuts_list_create (GtkFileChooserDefault *impl)
3187 GtkTreeSelection *selection;
3188 GtkTreeViewColumn *column;
3189 GtkCellRenderer *renderer;
3191 /* Scrolled window */
3193 swin = gtk_scrolled_window_new (NULL, NULL);
3194 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3195 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3196 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3198 gtk_widget_show (swin);
3202 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3203 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3204 G_CALLBACK (tree_view_keybinding_cb), impl);
3205 g_signal_connect (impl->browse_shortcuts_tree_view, "popup-menu",
3206 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3207 g_signal_connect (impl->browse_shortcuts_tree_view, "button-press-event",
3208 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3209 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Shortcuts"));
3210 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
3212 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
3214 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3216 shortcuts_source_targets,
3217 num_shortcuts_source_targets,
3220 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3221 GTK_DEST_DEFAULT_ALL,
3222 shortcuts_dest_targets,
3223 num_shortcuts_dest_targets,
3224 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3226 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3227 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
3228 gtk_tree_selection_set_select_function (selection,
3229 shortcuts_select_func,
3232 g_signal_connect (selection, "changed",
3233 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3235 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
3236 G_CALLBACK (shortcuts_row_activated_cb), impl);
3238 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
3239 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3241 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
3242 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3243 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
3244 G_CALLBACK (shortcuts_drag_end_cb), impl);
3245 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
3246 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3248 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
3249 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3250 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
3251 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3252 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
3253 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3254 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
3255 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3257 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3258 gtk_widget_show (impl->browse_shortcuts_tree_view);
3262 column = gtk_tree_view_column_new ();
3263 gtk_tree_view_column_set_title (column, _("Folder"));
3265 renderer = gtk_cell_renderer_pixbuf_new ();
3266 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3267 gtk_tree_view_column_set_attributes (column, renderer,
3268 "pixbuf", SHORTCUTS_COL_PIXBUF,
3269 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3272 renderer = gtk_cell_renderer_text_new ();
3273 g_signal_connect (renderer, "edited",
3274 G_CALLBACK (shortcuts_edited), impl);
3275 g_signal_connect (renderer, "editing-canceled",
3276 G_CALLBACK (shortcuts_editing_canceled), impl);
3277 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3278 gtk_tree_view_column_set_attributes (column, renderer,
3279 "text", SHORTCUTS_COL_NAME,
3282 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3283 shortcuts_row_separator_func,
3284 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3287 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3292 /* Creates the widgets for the shortcuts/bookmarks pane */
3294 shortcuts_pane_create (GtkFileChooserDefault *impl,
3295 GtkSizeGroup *size_group)
3301 vbox = gtk_vbox_new (FALSE, 6);
3302 gtk_widget_show (vbox);
3304 /* Shortcuts tree */
3306 widget = shortcuts_list_create (impl);
3307 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3309 /* Box for buttons */
3311 hbox = gtk_hbox_new (TRUE, 6);
3312 gtk_size_group_add_widget (size_group, hbox);
3313 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3314 gtk_widget_show (hbox);
3316 /* Add bookmark button */
3318 impl->browse_shortcuts_add_button = button_new (impl,
3323 G_CALLBACK (add_bookmark_button_clicked_cb));
3324 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3325 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
3326 _("Add the selected folder to the Bookmarks"), NULL);
3328 /* Remove bookmark button */
3330 impl->browse_shortcuts_remove_button = button_new (impl,
3335 G_CALLBACK (remove_bookmark_button_clicked_cb));
3336 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3337 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
3338 _("Remove the selected bookmark"), NULL);
3343 /* Handles key press events on the file list, so that we can trap Enter to
3344 * activate the default button on our own. Also, checks to see if '/' has been
3345 * pressed. See comment by tree_view_keybinding_cb() for more details.
3348 trap_activate_cb (GtkWidget *widget,
3352 GtkFileChooserDefault *impl;
3355 impl = (GtkFileChooserDefault *) data;
3357 modifiers = gtk_accelerator_get_default_mod_mask ();
3359 if (event->keyval == GDK_slash &&
3360 ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3362 location_popup_handler (impl, "/");
3366 if ((event->keyval == GDK_Return
3367 || event->keyval == GDK_ISO_Enter
3368 || event->keyval == GDK_KP_Enter
3369 || event->keyval == GDK_space)
3370 && ((event->state && modifiers) == 0)
3371 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3372 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3376 window = get_toplevel (widget);
3378 && widget != window->default_widget
3379 && !(widget == window->focus_widget &&
3380 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3382 gtk_window_activate_default (window);
3390 /* Callback used when the file list's popup menu is detached */
3392 popup_menu_detach_cb (GtkWidget *attach_widget,
3395 GtkFileChooserDefault *impl;
3397 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3398 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3400 impl->browse_files_popup_menu = NULL;
3401 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3402 impl->browse_files_popup_menu_hidden_files_item = NULL;
3405 /* Callback used when the "Add to Bookmarks" menu item is activated */
3407 add_to_shortcuts_cb (GtkMenuItem *item,
3408 GtkFileChooserDefault *impl)
3410 bookmarks_add_selected_folder (impl);
3413 /* Callback used when the "Open Location" menu item is activated */
3415 open_location_cb (GtkMenuItem *item,
3416 GtkFileChooserDefault *impl)
3418 location_popup_handler (impl, "");
3421 /* Callback used when the "Show Hidden Files" menu item is toggled */
3423 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3424 GtkFileChooserDefault *impl)
3427 "show-hidden", gtk_check_menu_item_get_active (item),
3431 /* Shows an error dialog about not being able to select a dragged file */
3433 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3434 const GtkFilePath *path,
3438 _("Could not select file"),
3443 file_list_drag_data_received_cb (GtkWidget *widget,
3444 GdkDragContext *context,
3447 GtkSelectionData *selection_data,
3452 GtkFileChooserDefault *impl;
3453 GtkFileChooser *chooser;
3457 GError *error = NULL;
3460 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3461 chooser = GTK_FILE_CHOOSER (data);
3463 /* Parse the text/uri-list string, navigate to the first one */
3464 uris = g_uri_list_extract_uris (selection_data->data);
3468 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3472 if ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3473 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3475 check_is_folder (impl->file_system, path, NULL))
3476 change_folder_and_display_error (impl, path);
3479 gtk_file_chooser_default_unselect_all (chooser);
3480 gtk_file_chooser_default_select_path (chooser, path, &error);
3482 error_selecting_dragged_file_dialog (impl, path, error);
3484 browse_files_center_selected_row (impl);
3487 gtk_file_path_free (path);
3491 g_set_error (&error,
3492 GTK_FILE_CHOOSER_ERROR,
3493 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3494 _("Could not select file '%s' "
3495 "because it is an invalid path name."),
3497 error_selecting_dragged_file_dialog (impl, NULL, error);
3501 if (impl->select_multiple)
3503 for (i = 1; uris[i]; i++)
3506 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3510 gtk_file_chooser_default_select_path (chooser, path, &error);
3512 error_selecting_dragged_file_dialog (impl, path, error);
3514 gtk_file_path_free (path);
3522 g_signal_stop_emission_by_name (widget, "drag-data-received");
3525 /* Don't do anything with the drag_drop signal */
3527 file_list_drag_drop_cb (GtkWidget *widget,
3528 GdkDragContext *context,
3532 GtkFileChooserDefault *impl)
3534 g_signal_stop_emission_by_name (widget, "drag-drop");
3538 /* Disable the normal tree drag motion handler, it makes it look like you're
3539 dropping the dragged item onto a tree item */
3541 file_list_drag_motion_cb (GtkWidget *widget,
3542 GdkDragContext *context,
3546 GtkFileChooserDefault *impl)
3548 g_signal_stop_emission_by_name (widget, "drag-motion");
3552 /* Constructs the popup menu for the file list if needed */
3554 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3558 if (impl->browse_files_popup_menu)
3561 impl->browse_files_popup_menu = gtk_menu_new ();
3562 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3563 impl->browse_files_tree_view,
3564 popup_menu_detach_cb);
3566 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
3567 impl->browse_files_popup_menu_add_shortcut_item = item;
3568 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3569 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3570 gtk_widget_set_sensitive (item, FALSE);
3571 g_signal_connect (item, "activate",
3572 G_CALLBACK (add_to_shortcuts_cb), impl);
3573 gtk_widget_show (item);
3574 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3576 item = gtk_image_menu_item_new_with_mnemonic (_("Open _Location"));
3577 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3578 gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU));
3579 g_signal_connect (item, "activate",
3580 G_CALLBACK (open_location_cb), impl);
3581 gtk_widget_show (item);
3582 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3584 item = gtk_separator_menu_item_new ();
3585 gtk_widget_show (item);
3586 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3588 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3589 impl->browse_files_popup_menu_hidden_files_item = item;
3590 g_signal_connect (item, "toggled",
3591 G_CALLBACK (show_hidden_toggled_cb), impl);
3592 gtk_widget_show (item);
3593 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3596 /* Updates the popup menu for the file list, creating it if necessary */
3598 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3600 file_list_build_popup_menu (impl);
3602 /* The sensitivity of the Add to Bookmarks item is set in
3603 * bookmarks_check_add_sensitivity()
3606 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3607 G_CALLBACK (show_hidden_toggled_cb), impl);
3608 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3610 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3611 G_CALLBACK (show_hidden_toggled_cb), impl);
3615 popup_position_func (GtkMenu *menu,
3621 GtkWidget *widget = GTK_WIDGET (user_data);
3622 GdkScreen *screen = gtk_widget_get_screen (widget);
3625 GdkRectangle monitor;
3627 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3629 gdk_window_get_origin (widget->window, x, y);
3631 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3633 *x += (widget->allocation.width - req.width) / 2;
3634 *y += (widget->allocation.height - req.height) / 2;
3636 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3637 gtk_menu_set_monitor (menu, monitor_num);
3638 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3640 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3641 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3647 file_list_popup_menu (GtkFileChooserDefault *impl,
3648 GdkEventButton *event)
3650 file_list_update_popup_menu (impl);
3652 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3653 NULL, NULL, NULL, NULL,
3654 event->button, event->time);
3657 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3659 popup_position_func, impl->browse_files_tree_view,
3660 0, GDK_CURRENT_TIME);
3661 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3667 /* Callback used for the GtkWidget::popup-menu signal of the file list */
3669 list_popup_menu_cb (GtkWidget *widget,
3670 GtkFileChooserDefault *impl)
3672 file_list_popup_menu (impl, NULL);
3676 /* Callback used when a button is pressed on the file list. We trap button 3 to
3677 * bring up a popup menu.
3680 list_button_press_event_cb (GtkWidget *widget,
3681 GdkEventButton *event,
3682 GtkFileChooserDefault *impl)
3684 if (event->button != 3)
3687 file_list_popup_menu (impl, event);
3691 /* Creates the widgets for the file list */
3693 create_file_list (GtkFileChooserDefault *impl)
3696 GtkTreeSelection *selection;
3697 GtkTreeViewColumn *column;
3698 GtkCellRenderer *renderer;
3700 /* Scrolled window */
3702 swin = gtk_scrolled_window_new (NULL, NULL);
3703 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3704 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3705 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3708 /* Tree/list view */
3710 impl->browse_files_tree_view = gtk_tree_view_new ();
3711 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "GtkFileChooserDefault", impl);
3712 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
3714 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
3715 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
3717 gtk_drag_dest_set (impl->browse_files_tree_view,
3718 GTK_DEST_DEFAULT_ALL,
3719 file_list_dest_targets,
3720 num_file_list_dest_targets,
3721 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3723 g_signal_connect (impl->browse_files_tree_view, "row-activated",
3724 G_CALLBACK (list_row_activated), impl);
3725 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
3726 G_CALLBACK (trap_activate_cb), impl);
3727 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
3728 G_CALLBACK (list_popup_menu_cb), impl);
3729 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
3730 G_CALLBACK (list_button_press_event_cb), impl);
3732 g_signal_connect (impl->browse_files_tree_view, "drag-data-received",
3733 G_CALLBACK (file_list_drag_data_received_cb), impl);
3734 g_signal_connect (impl->browse_files_tree_view, "drag-drop",
3735 G_CALLBACK (file_list_drag_drop_cb), impl);
3736 g_signal_connect (impl->browse_files_tree_view, "drag-motion",
3737 G_CALLBACK (file_list_drag_motion_cb), impl);
3739 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3740 gtk_tree_selection_set_select_function (selection,
3743 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
3745 file_list_source_targets,
3746 num_file_list_source_targets,
3749 g_signal_connect (selection, "changed",
3750 G_CALLBACK (list_selection_changed), impl);
3752 /* Filename column */
3754 impl->list_name_column = gtk_tree_view_column_new ();
3755 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
3756 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
3757 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
3758 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
3760 renderer = gtk_cell_renderer_pixbuf_new ();
3761 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
3762 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
3763 list_icon_data_func, impl, NULL);
3765 impl->list_name_renderer = gtk_cell_renderer_text_new ();
3766 g_object_set (impl->list_name_renderer,
3767 "ellipsize", PANGO_ELLIPSIZE_END,
3769 g_signal_connect (impl->list_name_renderer, "edited",
3770 G_CALLBACK (renderer_edited_cb), impl);
3771 g_signal_connect (impl->list_name_renderer, "editing-canceled",
3772 G_CALLBACK (renderer_editing_canceled_cb), impl);
3773 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
3774 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
3775 list_name_data_func, impl, NULL);
3777 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
3781 column = gtk_tree_view_column_new ();
3782 gtk_tree_view_column_set_title (column, _("Size"));
3784 renderer = gtk_cell_renderer_text_new ();
3785 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3786 gtk_tree_view_column_set_cell_data_func (column, renderer,
3787 list_size_data_func, impl, NULL);
3788 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
3789 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3791 /* Modification time column */
3793 column = gtk_tree_view_column_new ();
3794 gtk_tree_view_column_set_resizable (column, TRUE);
3795 gtk_tree_view_column_set_title (column, _("Modified"));
3797 renderer = gtk_cell_renderer_text_new ();
3798 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3799 gtk_tree_view_column_set_cell_data_func (column, renderer,
3800 list_mtime_data_func, impl, NULL);
3801 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
3802 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3803 gtk_widget_show_all (swin);
3809 create_path_bar (GtkFileChooserDefault *impl)
3811 GtkWidget *path_bar;
3813 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
3814 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
3820 set_filter_tooltip (GtkWidget *widget,
3823 GtkTooltips *tooltips = (GtkTooltips *)data;
3825 if (GTK_IS_BUTTON (widget))
3826 gtk_tooltips_set_tip (tooltips, widget,
3827 _("Select which types of files are shown"),
3832 realize_filter_combo (GtkWidget *combo,
3835 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
3837 gtk_container_forall (GTK_CONTAINER (combo),
3842 /* Creates the widgets for the files/folders pane */
3844 file_pane_create (GtkFileChooserDefault *impl,
3845 GtkSizeGroup *size_group)
3851 vbox = gtk_vbox_new (FALSE, 6);
3852 gtk_widget_show (vbox);
3854 /* The path bar and 'Create Folder' button */
3855 hbox = gtk_hbox_new (FALSE, 12);
3856 gtk_widget_show (hbox);
3857 impl->browse_path_bar = create_path_bar (impl);
3858 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
3859 gtk_widget_show_all (impl->browse_path_bar);
3860 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
3863 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
3864 g_signal_connect (impl->browse_new_folder_button, "clicked",
3865 G_CALLBACK (new_folder_button_clicked), impl);
3866 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
3867 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3869 /* Box for lists and preview */
3871 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
3872 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
3873 gtk_widget_show (hbox);
3877 widget = create_file_list (impl);
3878 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
3882 impl->preview_box = gtk_vbox_new (FALSE, 12);
3883 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
3884 /* Don't show preview box initially */
3888 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
3890 widget = filter_create (impl);
3892 g_signal_connect (widget, "realize",
3893 G_CALLBACK (realize_filter_combo), impl);
3895 gtk_widget_show (widget);
3896 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
3898 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
3899 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
3903 /* Callback used when the "Browse for more folders" expander is toggled */
3905 expander_changed_cb (GtkExpander *expander,
3907 GtkFileChooserDefault *impl)
3909 update_appearance (impl);
3912 /* Callback used when the selection changes in the save folder combo box */
3914 save_folder_combo_changed_cb (GtkComboBox *combo,
3915 GtkFileChooserDefault *impl)
3919 if (impl->changing_folder)
3922 if (gtk_combo_box_get_active_iter (combo, &iter))
3923 shortcuts_activate_iter (impl, &iter);
3926 /* Creates the combo box with the save folders */
3928 save_folder_combo_create (GtkFileChooserDefault *impl)
3931 GtkCellRenderer *cell;
3933 combo = g_object_new (GTK_TYPE_COMBO_BOX,
3934 "model", impl->shortcuts_model,
3935 "focus-on-click", FALSE,
3937 gtk_widget_show (combo);
3939 cell = gtk_cell_renderer_pixbuf_new ();
3940 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3941 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3942 "pixbuf", SHORTCUTS_COL_PIXBUF,
3943 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3944 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3947 cell = gtk_cell_renderer_text_new ();
3948 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3949 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3950 "text", SHORTCUTS_COL_NAME,
3951 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3954 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
3955 shortcuts_row_separator_func,
3956 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3959 g_signal_connect (combo, "changed",
3960 G_CALLBACK (save_folder_combo_changed_cb), impl);
3965 /* Creates the widgets specific to Save mode */
3967 save_widgets_create (GtkFileChooserDefault *impl)
3972 GtkWidget *alignment;
3974 vbox = gtk_vbox_new (FALSE, 12);
3976 table = gtk_table_new (2, 2, FALSE);
3977 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3978 gtk_widget_show (table);
3979 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3980 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3984 widget = gtk_label_new_with_mnemonic (_("_Name:"));
3985 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3986 gtk_table_attach (GTK_TABLE (table), widget,
3990 gtk_widget_show (widget);
3992 impl->save_file_name_entry = _gtk_file_chooser_entry_new (TRUE);
3993 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3995 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3996 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3997 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3999 GTK_EXPAND | GTK_FILL, 0,
4001 gtk_widget_show (impl->save_file_name_entry);
4002 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
4005 impl->save_folder_label = gtk_label_new (NULL);
4006 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4007 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4011 gtk_widget_show (impl->save_folder_label);
4013 impl->save_folder_combo = save_folder_combo_create (impl);
4014 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4016 GTK_EXPAND | GTK_FILL, GTK_FILL,
4018 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4021 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4022 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4024 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4025 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4026 g_signal_connect (impl->save_expander, "notify::expanded",
4027 G_CALLBACK (expander_changed_cb),
4029 gtk_widget_show_all (alignment);
4034 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4036 browse_widgets_create (GtkFileChooserDefault *impl)
4041 GtkSizeGroup *size_group;
4043 /* size group is used by the [+][-] buttons and the filter combo */
4044 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4045 vbox = gtk_vbox_new (FALSE, 12);
4048 hpaned = gtk_hpaned_new ();
4049 gtk_widget_show (hpaned);
4050 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
4051 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
4053 widget = shortcuts_pane_create (impl, size_group);
4054 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4055 widget = file_pane_create (impl, size_group);
4056 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4058 g_object_unref (size_group);
4064 gtk_file_chooser_default_constructor (GType type,
4065 guint n_construct_properties,
4066 GObjectConstructParam *construct_params)
4068 GtkFileChooserDefault *impl;
4071 object = parent_class->constructor (type,
4072 n_construct_properties,
4074 impl = GTK_FILE_CHOOSER_DEFAULT (object);
4076 g_assert (impl->file_system);
4078 gtk_widget_push_composite_child ();
4080 /* Shortcuts model */
4082 shortcuts_model_create (impl);
4084 /* Widgets for Save mode */
4085 impl->save_widgets = save_widgets_create (impl);
4086 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4088 /* The browse widgets */
4089 impl->browse_widgets = browse_widgets_create (impl);
4090 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
4092 /* Alignment to hold extra widget */
4093 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4094 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4096 gtk_widget_pop_composite_child ();
4097 update_appearance (impl);
4102 /* Sets the extra_widget by packing it in the appropriate place */
4104 set_extra_widget (GtkFileChooserDefault *impl,
4105 GtkWidget *extra_widget)
4109 g_object_ref (extra_widget);
4110 /* FIXME: is this right ? */
4111 gtk_widget_show (extra_widget);
4114 if (impl->extra_widget)
4116 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4117 g_object_unref (impl->extra_widget);
4120 impl->extra_widget = extra_widget;
4121 if (impl->extra_widget)
4123 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4124 gtk_widget_show (impl->extra_align);
4127 gtk_widget_hide (impl->extra_align);
4131 set_local_only (GtkFileChooserDefault *impl,
4132 gboolean local_only)
4134 if (local_only != impl->local_only)
4136 impl->local_only = local_only;
4138 if (impl->shortcuts_model && impl->file_system)
4140 shortcuts_add_volumes (impl);
4141 shortcuts_add_bookmarks (impl);
4145 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4147 /* If we are pointing to a non-local folder, make an effort to change
4148 * back to a local folder, but it's really up to the app to not cause
4149 * such a situation, so we ignore errors.
4151 const gchar *home = g_get_home_dir ();
4152 GtkFilePath *home_path;
4157 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
4159 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
4161 gtk_file_path_free (home_path);
4167 volumes_changed_cb (GtkFileSystem *file_system,
4168 GtkFileChooserDefault *impl)
4170 shortcuts_add_volumes (impl);
4173 /* Callback used when the set of bookmarks changes in the file system */
4175 bookmarks_changed_cb (GtkFileSystem *file_system,
4176 GtkFileChooserDefault *impl)
4178 shortcuts_add_bookmarks (impl);
4180 bookmarks_check_add_sensitivity (impl);
4181 bookmarks_check_remove_sensitivity (impl);
4182 shortcuts_check_popup_sensitivity (impl);
4185 /* Sets the file chooser to multiple selection mode */
4187 set_select_multiple (GtkFileChooserDefault *impl,
4188 gboolean select_multiple,
4189 gboolean property_notify)
4191 GtkTreeSelection *selection;
4192 GtkSelectionMode mode;
4194 if (select_multiple == impl->select_multiple)
4197 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
4199 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4200 gtk_tree_selection_set_mode (selection, mode);
4202 impl->select_multiple = select_multiple;
4203 g_object_notify (G_OBJECT (impl), "select-multiple");
4205 check_preview_change (impl);
4209 set_file_system_backend (GtkFileChooserDefault *impl,
4210 const char *backend)
4212 profile_start ("start for backend", backend ? backend : "default");
4214 if (impl->file_system)
4216 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
4217 impl->volumes_changed_id = 0;
4218 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
4219 impl->bookmarks_changed_id = 0;
4220 g_object_unref (impl->file_system);
4223 impl->file_system = NULL;
4225 impl->file_system = _gtk_file_system_create (backend);
4228 GtkSettings *settings = gtk_settings_get_default ();
4229 gchar *default_backend = NULL;
4231 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
4232 if (default_backend)
4234 impl->file_system = _gtk_file_system_create (default_backend);
4235 g_free (default_backend);
4239 if (!impl->file_system)
4241 #if defined (G_OS_UNIX)
4242 impl->file_system = gtk_file_system_unix_new ();
4243 #elif defined (G_OS_WIN32)
4244 impl->file_system = gtk_file_system_win32_new ();
4246 #error "No default filesystem implementation on the platform"
4250 if (impl->file_system)
4252 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
4253 G_CALLBACK (volumes_changed_cb),
4255 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
4256 G_CALLBACK (bookmarks_changed_cb),
4260 profile_end ("end", NULL);
4263 /* This function is basically a do_all function.
4265 * It sets the visibility on all the widgets based on the current state, and
4266 * moves the custom_widget if needed.
4269 update_appearance (GtkFileChooserDefault *impl)
4271 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4272 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4276 gtk_widget_show (impl->save_widgets);
4278 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4279 text = _("Save in _folder:");
4281 text = _("Create in _folder:");
4283 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
4285 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4287 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
4288 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
4289 gtk_widget_show (impl->browse_widgets);
4293 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
4294 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
4295 gtk_widget_hide (impl->browse_widgets);
4298 gtk_widget_show (impl->browse_new_folder_button);
4300 if (impl->select_multiple)
4302 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
4303 "Re-setting to single selection mode.");
4304 set_select_multiple (impl, FALSE, TRUE);
4307 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4308 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4310 gtk_widget_hide (impl->save_widgets);
4311 gtk_widget_show (impl->browse_widgets);
4314 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
4315 gtk_widget_hide (impl->browse_new_folder_button);
4317 gtk_widget_show (impl->browse_new_folder_button);
4319 gtk_widget_queue_draw (impl->browse_files_tree_view);
4321 g_signal_emit_by_name (impl, "default-size-changed");
4325 gtk_file_chooser_default_set_property (GObject *object,
4327 const GValue *value,
4331 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4335 case GTK_FILE_CHOOSER_PROP_ACTION:
4337 GtkFileChooserAction action = g_value_get_enum (value);
4339 if (action != impl->action)
4341 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
4343 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4344 && impl->select_multiple)
4346 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
4347 "this is not allowed in multiple selection mode. Resetting the file chooser "
4348 "to single selection mode.");
4349 set_select_multiple (impl, FALSE, TRUE);
4351 impl->action = action;
4352 update_appearance (impl);
4355 if (impl->save_file_name_entry)
4356 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4361 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
4362 set_file_system_backend (impl, g_value_get_string (value));
4365 case GTK_FILE_CHOOSER_PROP_FILTER:
4366 set_current_filter (impl, g_value_get_object (value));
4369 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4370 set_local_only (impl, g_value_get_boolean (value));
4373 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4374 set_preview_widget (impl, g_value_get_object (value));
4377 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4378 impl->preview_widget_active = g_value_get_boolean (value);
4379 update_preview_widget_visibility (impl);
4382 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4383 impl->use_preview_label = g_value_get_boolean (value);
4384 update_preview_widget_visibility (impl);
4387 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4388 set_extra_widget (impl, g_value_get_object (value));
4391 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4393 gboolean select_multiple = g_value_get_boolean (value);
4394 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4397 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
4398 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
4399 "leaving the file chooser in single selection mode.");
4403 set_select_multiple (impl, select_multiple, FALSE);
4407 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
4409 gboolean show_hidden = g_value_get_boolean (value);
4410 if (show_hidden != impl->show_hidden)
4412 impl->show_hidden = show_hidden;
4414 if (impl->browse_files_model)
4415 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
4420 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
4422 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
4423 impl->do_overwrite_confirmation = do_overwrite_confirmation;
4428 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4434 gtk_file_chooser_default_get_property (GObject *object,
4439 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4443 case GTK_FILE_CHOOSER_PROP_ACTION:
4444 g_value_set_enum (value, impl->action);
4447 case GTK_FILE_CHOOSER_PROP_FILTER:
4448 g_value_set_object (value, impl->current_filter);
4451 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4452 g_value_set_boolean (value, impl->local_only);
4455 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4456 g_value_set_object (value, impl->preview_widget);
4459 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4460 g_value_set_boolean (value, impl->preview_widget_active);
4463 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4464 g_value_set_boolean (value, impl->use_preview_label);
4467 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4468 g_value_set_object (value, impl->extra_widget);
4471 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4472 g_value_set_boolean (value, impl->select_multiple);
4475 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
4476 g_value_set_boolean (value, impl->show_hidden);
4479 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
4480 g_value_set_boolean (value, impl->do_overwrite_confirmation);
4484 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4489 /* Removes the settings signal handler. It's safe to call multiple times */
4491 remove_settings_signal (GtkFileChooserDefault *impl,
4494 if (impl->settings_signal_id)
4496 GtkSettings *settings;
4498 settings = gtk_settings_get_for_screen (screen);
4499 g_signal_handler_disconnect (settings,
4500 impl->settings_signal_id);
4501 impl->settings_signal_id = 0;
4506 gtk_file_chooser_default_dispose (GObject *object)
4508 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
4510 if (impl->extra_widget)
4512 g_object_unref (impl->extra_widget);
4513 impl->extra_widget = NULL;
4516 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
4518 G_OBJECT_CLASS (parent_class)->dispose (object);
4521 /* We override show-all since we have internal widgets that
4522 * shouldn't be shown when you call show_all(), like the filter
4526 gtk_file_chooser_default_show_all (GtkWidget *widget)
4528 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
4530 gtk_widget_show (widget);
4532 if (impl->extra_widget)
4533 gtk_widget_show_all (impl->extra_widget);
4536 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
4537 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
4540 toplevel_set_focus_cb (GtkWindow *window,
4542 GtkFileChooserDefault *impl)
4544 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
4547 /* We monitor the focus widget on our toplevel to be able to know which widget
4548 * was last focused at the time our "should_respond" method gets called.
4551 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
4552 GtkWidget *previous_toplevel)
4554 GtkFileChooserDefault *impl;
4555 GtkWidget *toplevel;
4557 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4559 if (previous_toplevel)
4561 g_assert (impl->toplevel_set_focus_id != 0);
4562 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
4563 impl->toplevel_set_focus_id = 0;
4564 impl->toplevel_last_focus_widget = NULL;
4567 g_assert (impl->toplevel_set_focus_id == 0);
4569 toplevel = gtk_widget_get_toplevel (widget);
4570 if (GTK_IS_WINDOW (toplevel))
4572 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
4573 G_CALLBACK (toplevel_set_focus_cb), impl);
4574 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
4578 /* Changes the icons wherever it is needed */
4580 change_icon_theme (GtkFileChooserDefault *impl)
4582 GtkSettings *settings;
4585 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4587 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
4588 impl->icon_size = MAX (width, height);
4590 impl->icon_size = FALLBACK_ICON_SIZE;
4592 shortcuts_reload_icons (impl);
4593 gtk_widget_queue_resize (impl->browse_files_tree_view);
4596 /* Callback used when a GtkSettings value changes */
4598 settings_notify_cb (GObject *object,
4600 GtkFileChooserDefault *impl)
4604 name = g_param_spec_get_name (pspec);
4606 if (strcmp (name, "gtk-icon-theme-name") == 0
4607 || strcmp (name, "gtk-icon-sizes") == 0)
4608 change_icon_theme (impl);
4611 /* Installs a signal handler for GtkSettings so that we can monitor changes in
4615 check_icon_theme (GtkFileChooserDefault *impl)
4617 GtkSettings *settings;
4619 if (impl->settings_signal_id)
4622 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4624 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
4625 impl->settings_signal_id = g_signal_connect (settings, "notify",
4626 G_CALLBACK (settings_notify_cb), impl);
4628 change_icon_theme (impl);
4633 gtk_file_chooser_default_style_set (GtkWidget *widget,
4634 GtkStyle *previous_style)
4636 GtkFileChooserDefault *impl;
4638 profile_start ("start", NULL);
4640 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4642 if (GTK_WIDGET_CLASS (parent_class)->style_set)
4643 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
4645 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
4646 change_icon_theme (impl);
4648 g_signal_emit_by_name (widget, "default-size-changed");
4650 profile_end ("end", NULL);
4654 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
4655 GdkScreen *previous_screen)
4657 GtkFileChooserDefault *impl;
4659 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4661 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
4662 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
4664 remove_settings_signal (impl, previous_screen);
4665 check_icon_theme (impl);
4667 g_signal_emit_by_name (widget, "default-size-changed");
4671 get_is_file_filtered (GtkFileChooserDefault *impl,
4672 const GtkFilePath *path,
4673 GtkFileInfo *file_info)
4675 GtkFileFilterInfo filter_info;
4676 GtkFileFilterFlags needed;
4679 if (!impl->current_filter)
4682 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
4684 needed = gtk_file_filter_get_needed (impl->current_filter);
4686 filter_info.display_name = gtk_file_info_get_display_name (file_info);
4687 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
4689 if (needed & GTK_FILE_FILTER_FILENAME)
4691 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
4692 if (filter_info.filename)
4693 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
4696 filter_info.filename = NULL;
4698 if (needed & GTK_FILE_FILTER_URI)
4700 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
4701 if (filter_info.uri)
4702 filter_info.contains |= GTK_FILE_FILTER_URI;
4705 filter_info.uri = NULL;
4707 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
4709 if (filter_info.filename)
4710 g_free ((gchar *)filter_info.filename);
4711 if (filter_info.uri)
4712 g_free ((gchar *)filter_info.uri);
4717 /* GtkWidget::map method */
4719 gtk_file_chooser_default_map (GtkWidget *widget)
4721 GtkFileChooserDefault *impl;
4723 profile_start ("start", NULL);
4725 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4727 GTK_WIDGET_CLASS (parent_class)->map (widget);
4729 if (impl->current_folder)
4731 pending_select_paths_store_selection (impl);
4732 change_folder_and_display_error (impl, impl->current_folder);
4735 bookmarks_changed_cb (impl->file_system, impl);
4737 profile_end ("end", NULL);
4741 list_model_filter_func (GtkFileSystemModel *model,
4743 const GtkFileInfo *file_info,
4746 GtkFileChooserDefault *impl = user_data;
4748 if (!impl->current_filter)
4751 if (gtk_file_info_get_is_folder (file_info))
4754 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
4758 install_list_model_filter (GtkFileChooserDefault *impl)
4760 GtkFileSystemModelFilter filter;
4763 g_assert (impl->browse_files_model != NULL);
4765 if (impl->current_filter)
4767 filter = list_model_filter_func;
4776 _gtk_file_system_model_set_filter (impl->browse_files_model,
4781 #define COMPARE_DIRECTORIES \
4782 GtkFileChooserDefault *impl = user_data; \
4783 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
4784 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
4785 gboolean dir_a, dir_b; \
4788 dir_a = gtk_file_info_get_is_folder (info_a); \
4790 return impl->list_sort_ascending ? -1 : 1; \
4793 dir_b = gtk_file_info_get_is_folder (info_b); \
4795 return impl->list_sort_ascending ? 1 : -1; \
4797 if (dir_a != dir_b) \
4798 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
4800 /* Sort callback for the filename column */
4802 name_sort_func (GtkTreeModel *model,
4807 COMPARE_DIRECTORIES;
4809 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
4812 /* Sort callback for the size column */
4814 size_sort_func (GtkTreeModel *model,
4819 COMPARE_DIRECTORIES;
4822 gint64 size_a = gtk_file_info_get_size (info_a);
4823 gint64 size_b = gtk_file_info_get_size (info_b);
4825 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
4829 /* Sort callback for the mtime column */
4831 mtime_sort_func (GtkTreeModel *model,
4836 COMPARE_DIRECTORIES;
4839 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
4840 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
4842 return ta > tb ? -1 : (ta == tb ? 0 : 1);
4846 /* Callback used when the sort column changes. We cache the sort order for use
4847 * in name_sort_func().
4850 list_sort_column_changed_cb (GtkTreeSortable *sortable,
4851 GtkFileChooserDefault *impl)
4853 GtkSortType sort_type;
4855 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
4856 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
4860 set_busy_cursor (GtkFileChooserDefault *impl,
4863 GtkWindow *toplevel;
4864 GdkDisplay *display;
4867 toplevel = get_toplevel (GTK_WIDGET (impl));
4868 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
4871 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
4874 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
4878 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
4879 gdk_display_flush (display);
4882 gdk_cursor_unref (cursor);
4885 /* Creates a sort model to wrap the file system model and sets it on the tree view */
4887 load_set_model (GtkFileChooserDefault *impl)
4889 g_assert (impl->browse_files_model != NULL);
4890 g_assert (impl->sort_model == NULL);
4892 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4893 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4894 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4895 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4896 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4897 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4898 impl->list_sort_ascending = TRUE;
4900 g_signal_connect (impl->sort_model, "sort-column-changed",
4901 G_CALLBACK (list_sort_column_changed_cb), impl);
4903 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4904 GTK_TREE_MODEL (impl->sort_model));
4905 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4906 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4907 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4910 /* Timeout callback used when the loading timer expires */
4912 load_timeout_cb (gpointer data)
4914 GtkFileChooserDefault *impl;
4916 profile_start ("start", NULL);
4918 GDK_THREADS_ENTER ();
4920 impl = GTK_FILE_CHOOSER_DEFAULT (data);
4921 g_assert (impl->load_state == LOAD_PRELOAD);
4922 g_assert (impl->load_timeout_id != 0);
4923 g_assert (impl->browse_files_model != NULL);
4925 impl->load_timeout_id = 0;
4926 impl->load_state = LOAD_LOADING;
4928 load_set_model (impl);
4930 GDK_THREADS_LEAVE ();
4932 profile_end ("end", NULL);
4937 /* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
4939 load_setup_timer (GtkFileChooserDefault *impl)
4941 g_assert (impl->load_timeout_id == 0);
4942 g_assert (impl->load_state != LOAD_PRELOAD);
4944 impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
4945 impl->load_state = LOAD_PRELOAD;
4948 /* Removes the load timeout and switches to the LOAD_FINISHED state */
4950 load_remove_timer (GtkFileChooserDefault *impl)
4952 if (impl->load_timeout_id != 0)
4954 g_assert (impl->load_state == LOAD_PRELOAD);
4956 g_source_remove (impl->load_timeout_id);
4957 impl->load_timeout_id = 0;
4958 impl->load_state = LOAD_EMPTY;
4961 g_assert (impl->load_state == LOAD_EMPTY ||
4962 impl->load_state == LOAD_LOADING ||
4963 impl->load_state == LOAD_FINISHED);
4966 /* Selects the first row in the file list */
4968 browse_files_select_first_row (GtkFileChooserDefault *impl)
4972 if (!impl->sort_model)
4975 path = gtk_tree_path_new_from_indices (0, -1);
4976 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4977 gtk_tree_path_free (path);
4980 struct center_selected_row_closure {
4981 GtkFileChooserDefault *impl;
4982 gboolean already_centered;
4985 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
4986 * selected row in the tree view.
4989 center_selected_row_foreach_cb (GtkTreeModel *model,
4994 struct center_selected_row_closure *closure;
4997 if (closure->already_centered)
5000 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
5001 closure->already_centered = TRUE;
5004 /* Centers the selected row in the tree view */
5006 browse_files_center_selected_row (GtkFileChooserDefault *impl)
5008 struct center_selected_row_closure closure;
5009 GtkTreeSelection *selection;
5011 closure.impl = impl;
5012 closure.already_centered = FALSE;
5014 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5015 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
5019 show_and_select_paths (GtkFileChooserDefault *impl,
5020 const GtkFilePath *parent_path,
5021 const GtkFilePath *only_one_path,
5025 GtkFileFolder *folder;
5027 gboolean have_hidden;
5028 gboolean have_filtered;
5030 profile_start ("start", NULL);
5032 if (!only_one_path && !paths)
5034 profile_end ("end", NULL);
5038 folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
5041 profile_end ("end", NULL);
5046 have_hidden = FALSE;
5047 have_filtered = FALSE;
5053 info = gtk_file_folder_get_info (folder, only_one_path, error);
5057 have_hidden = gtk_file_info_get_is_hidden (info);
5058 have_filtered = get_is_file_filtered (impl, only_one_path, info);
5059 gtk_file_info_free (info);
5066 for (l = paths; l; l = l->next)
5068 const GtkFilePath *path;
5074 info = gtk_file_folder_get_info (folder, path, NULL);
5078 have_hidden = gtk_file_info_get_is_hidden (info);
5081 have_filtered = get_is_file_filtered (impl, path, info);
5083 gtk_file_info_free (info);
5085 if (have_hidden && have_filtered)
5086 break; /* we now have all the information we need */
5093 g_object_unref (folder);
5097 profile_end ("end", NULL);
5102 g_object_set (impl, "show-hidden", TRUE, NULL);
5105 set_current_filter (impl, NULL);
5108 _gtk_file_system_model_path_do (impl->browse_files_model, only_one_path, select_func, impl);
5113 for (l = paths; l; l = l->next)
5115 const GtkFilePath *path;
5118 _gtk_file_system_model_path_do (impl->browse_files_model, path, select_func, impl);
5122 profile_end ("end", NULL);
5126 /* Processes the pending operation when a folder is finished loading */
5128 pending_select_paths_process (GtkFileChooserDefault *impl)
5130 g_assert (impl->load_state == LOAD_FINISHED);
5131 g_assert (impl->browse_files_model != NULL);
5132 g_assert (impl->sort_model != NULL);
5134 if (impl->pending_select_paths)
5137 show_and_select_paths (impl, impl->current_folder, NULL, impl->pending_select_paths, NULL);
5138 pending_select_paths_free (impl);
5139 browse_files_center_selected_row (impl);
5143 /* We only select the first row if the chooser is actually mapped ---
5144 * selecting the first row is to help the user when he is interacting with
5145 * the chooser, but sometimes a chooser works not on behalf of the user,
5146 * but rather on behalf of something else like GtkFileChooserButton. In
5147 * that case, the chooser's selection should be what the caller expects,
5148 * as the user can't see that something else got selected. See bug #165264.
5150 * Also, we don't select the first file if we are not in OPEN mode. Doing
5151 * so would change the contents of the filename entry for SAVE or
5152 * CREATE_FOLDER, which is undesired; in SELECT_FOLDER, we don't want to
5153 * select a *different* folder from the one into which the user just
5156 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5157 browse_files_select_first_row (impl);
5160 g_assert (impl->pending_select_paths == NULL);
5163 /* Callback used when the file system model finishes loading */
5165 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
5166 GtkFileChooserDefault *impl)
5168 profile_start ("start", NULL);
5170 if (impl->load_state == LOAD_PRELOAD)
5172 load_remove_timer (impl);
5173 load_set_model (impl);
5175 else if (impl->load_state == LOAD_LOADING)
5181 /* We can't g_assert_not_reached(), as something other than us may have
5182 * initiated a folder reload. See #165556.
5184 profile_end ("end", NULL);
5188 g_assert (impl->load_timeout_id == 0);
5190 impl->load_state = LOAD_FINISHED;
5192 pending_select_paths_process (impl);
5193 set_busy_cursor (impl, FALSE);
5195 profile_end ("end", NULL);
5198 /* Gets rid of the old list model and creates a new one for the current folder */
5200 set_list_model (GtkFileChooserDefault *impl,
5203 g_assert (impl->current_folder != NULL);
5205 profile_start ("start", NULL);
5207 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
5209 if (impl->browse_files_model)
5211 g_object_unref (impl->browse_files_model);
5212 impl->browse_files_model = NULL;
5215 if (impl->sort_model)
5217 g_object_unref (impl->sort_model);
5218 impl->sort_model = NULL;
5221 set_busy_cursor (impl, TRUE);
5222 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
5224 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
5225 impl->current_folder, 0,
5228 if (!impl->browse_files_model)
5230 set_busy_cursor (impl, FALSE);
5231 profile_end ("end", NULL);
5235 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
5237 g_signal_connect (impl->browse_files_model, "finished-loading",
5238 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
5240 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
5242 install_list_model_filter (impl);
5244 profile_end ("end", NULL);
5250 update_chooser_entry (GtkFileChooserDefault *impl)
5252 GtkTreeSelection *selection;
5253 const GtkFileInfo *info;
5255 GtkTreeIter child_iter;
5256 gboolean change_entry;
5258 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
5261 g_assert (!impl->select_multiple);
5262 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5264 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
5266 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), "");
5270 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5274 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5276 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5277 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
5279 change_entry = TRUE; /* ... unless we are in CREATE_FOLDER mode */
5282 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
5283 gtk_file_info_get_display_name (info));
5287 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
5288 const GtkFilePath *path,
5291 return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, error);
5295 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
5296 const GtkFilePath *path,
5297 gboolean keep_trail,
5300 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5303 profile_start ("start", (char *) path);
5305 g_assert (path != NULL);
5307 if (impl->local_only &&
5308 !gtk_file_system_path_is_local (impl->file_system, path))
5311 GTK_FILE_CHOOSER_ERROR,
5312 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
5313 _("Cannot change to folder because it is not local"));
5315 profile_end ("end - not local", (char *) path);
5319 /* Test validity of path here. */
5320 if (!check_is_folder (impl->file_system, path, error))
5322 profile_end ("end - not a folder", (char *) path);
5326 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, keep_trail, error))
5328 profile_end ("end - could not set path bar", (char *) path);
5332 if (impl->current_folder != path)
5334 if (impl->current_folder)
5335 gtk_file_path_free (impl->current_folder);
5337 impl->current_folder = gtk_file_path_copy (path);
5340 /* Update the widgets that may trigger a folder change themselves. */
5342 if (!impl->changing_folder)
5344 impl->changing_folder = TRUE;
5346 shortcuts_update_current_folder (impl);
5348 impl->changing_folder = FALSE;
5351 /* Set the folder on the save entry */
5353 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
5354 impl->current_folder);
5356 /* Create a new list model. This is slightly evil; we store the result value
5357 * but perform more actions rather than returning immediately even if it
5358 * generates an error.
5360 result = set_list_model (impl, error);
5362 /* Refresh controls */
5364 shortcuts_find_current_folder (impl);
5366 g_signal_emit_by_name (impl, "current-folder-changed", 0);
5368 check_preview_change (impl);
5369 bookmarks_check_add_sensitivity (impl);
5371 g_signal_emit_by_name (impl, "selection-changed", 0);
5373 profile_end ("end", NULL);
5377 static GtkFilePath *
5378 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
5380 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5382 return gtk_file_path_copy (impl->current_folder);
5386 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
5389 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5391 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5392 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5394 pending_select_paths_free (impl);
5395 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), name);
5399 select_func (GtkFileSystemModel *model,
5404 GtkFileChooserDefault *impl = user_data;
5405 GtkTreeSelection *selection;
5406 GtkTreeIter sorted_iter;
5408 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5410 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
5411 gtk_tree_selection_select_iter (selection, &sorted_iter);
5415 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
5416 const GtkFilePath *path,
5419 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5420 GtkFilePath *parent_path;
5423 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
5427 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
5429 if (impl->load_state == LOAD_EMPTY)
5433 g_assert (impl->current_folder != NULL);
5435 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
5438 if (same_path && impl->load_state == LOAD_FINISHED)
5442 result = show_and_select_paths (impl, parent_path, path, NULL, error);
5443 gtk_file_path_free (parent_path);
5447 pending_select_paths_add (impl, path);
5453 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
5454 gtk_file_path_free (parent_path);
5458 gtk_file_path_free (parent_path);
5463 unselect_func (GtkFileSystemModel *model,
5468 GtkFileChooserDefault *impl = user_data;
5469 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
5470 GtkTreePath *sorted_path;
5472 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
5474 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
5476 gtk_tree_path_free (sorted_path);
5480 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
5481 const GtkFilePath *path)
5483 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5485 if (!impl->browse_files_model)
5488 _gtk_file_system_model_path_do (impl->browse_files_model, path,
5489 unselect_func, impl);
5493 maybe_select (GtkTreeModel *model,
5498 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
5499 GtkTreeSelection *selection;
5500 const GtkFileInfo *info;
5503 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5505 info = get_list_file_info (impl, iter);
5506 is_folder = gtk_file_info_get_is_folder (info);
5508 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
5509 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
5510 gtk_tree_selection_select_iter (selection, iter);
5512 gtk_tree_selection_unselect_iter (selection, iter);
5518 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
5520 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5521 if (impl->select_multiple)
5522 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
5523 maybe_select, impl);
5527 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
5529 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5530 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5532 gtk_tree_selection_unselect_all (selection);
5533 pending_select_paths_free (impl);
5536 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
5538 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
5540 * is_empty_ret - whether the file entry is totally empty
5542 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
5543 * the path will be "$cwd/foobar")
5546 check_save_entry (GtkFileChooserDefault *impl,
5547 GtkFilePath **path_ret,
5548 gboolean *is_well_formed_ret,
5549 gboolean *is_empty_ret,
5550 gboolean *is_file_part_empty_ret)
5552 GtkFileChooserEntry *chooser_entry;
5553 const GtkFilePath *current_folder;
5554 const char *file_part;
5558 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5559 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5561 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
5563 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
5566 *is_well_formed_ret = TRUE;
5567 *is_empty_ret = TRUE;
5568 *is_file_part_empty_ret = TRUE;
5573 *is_empty_ret = FALSE;
5575 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
5576 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
5578 if (!file_part || file_part[0] == '\0')
5580 *path_ret = gtk_file_path_copy (current_folder);
5581 *is_well_formed_ret = TRUE;
5582 *is_file_part_empty_ret = TRUE;
5587 *is_file_part_empty_ret = FALSE;
5590 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
5594 error_building_filename_dialog (impl, current_folder, file_part, error);
5596 *is_well_formed_ret = FALSE;
5602 *is_well_formed_ret = TRUE;
5605 struct get_paths_closure {
5606 GtkFileChooserDefault *impl;
5608 GtkFilePath *path_from_entry;
5612 get_paths_foreach (GtkTreeModel *model,
5617 struct get_paths_closure *info;
5618 const GtkFilePath *file_path;
5619 GtkFileSystemModel *fs_model;
5620 GtkTreeIter sel_iter;
5623 fs_model = info->impl->browse_files_model;
5624 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
5626 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
5628 return; /* We are on the editable row */
5630 if (!info->path_from_entry
5631 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
5632 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
5636 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
5638 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5639 struct get_paths_closure info;
5643 info.path_from_entry = NULL;
5645 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5646 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5648 gboolean is_well_formed, is_empty, is_file_part_empty;
5650 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty);
5652 if (!is_well_formed)
5657 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5659 gtk_file_path_free (info.path_from_entry);
5665 if (!info.path_from_entry || impl->select_multiple)
5667 GtkTreeSelection *selection;
5669 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5670 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
5673 if (info.path_from_entry)
5674 info.result = g_slist_prepend (info.result, info.path_from_entry);
5676 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
5677 * fall back to the current directory */
5678 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
5679 info.result == NULL)
5681 info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
5684 return g_slist_reverse (info.result);
5687 static GtkFilePath *
5688 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
5690 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5692 if (impl->preview_path)
5693 return gtk_file_path_copy (impl->preview_path);
5698 static GtkFileSystem *
5699 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
5701 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5703 return impl->file_system;
5706 /* Shows or hides the filter widgets */
5708 show_filters (GtkFileChooserDefault *impl,
5712 gtk_widget_show (impl->filter_combo_hbox);
5714 gtk_widget_hide (impl->filter_combo_hbox);
5718 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
5719 GtkFileFilter *filter)
5721 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5724 if (g_slist_find (impl->filters, filter))
5726 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
5730 g_object_ref (filter);
5731 gtk_object_sink (GTK_OBJECT (filter));
5732 impl->filters = g_slist_append (impl->filters, filter);
5734 name = gtk_file_filter_get_name (filter);
5736 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
5738 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
5740 if (!g_slist_find (impl->filters, impl->current_filter))
5741 set_current_filter (impl, filter);
5743 show_filters (impl, TRUE);
5747 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
5748 GtkFileFilter *filter)
5750 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5751 GtkTreeModel *model;
5755 filter_index = g_slist_index (impl->filters, filter);
5757 if (filter_index < 0)
5759 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
5763 impl->filters = g_slist_remove (impl->filters, filter);
5765 if (filter == impl->current_filter)
5768 set_current_filter (impl, impl->filters->data);
5770 set_current_filter (impl, NULL);
5773 /* Remove row from the combo box */
5774 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
5775 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
5776 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
5778 g_object_unref (filter);
5781 show_filters (impl, FALSE);
5785 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
5787 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5789 return g_slist_copy (impl->filters);
5792 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
5794 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
5797 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
5801 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
5802 const GtkFilePath *path,
5805 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5809 /* Test validity of path here. */
5810 if (!check_is_folder (impl->file_system, path, error))
5813 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
5815 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
5818 impl->num_shortcuts++;
5820 if (impl->shortcuts_filter_model)
5821 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
5827 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
5828 const GtkFilePath *path,
5831 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5837 if (impl->num_shortcuts == 0)
5840 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5841 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5842 g_assert_not_reached ();
5844 for (i = 0; i < impl->num_shortcuts; i++)
5848 GtkFilePath *shortcut;
5850 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5851 SHORTCUTS_COL_DATA, &col_data,
5852 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5854 g_assert (col_data != NULL);
5855 g_assert (!is_volume);
5857 shortcut = col_data;
5858 if (gtk_file_path_compare (shortcut, path) == 0)
5860 shortcuts_remove_rows (impl, pos + i, 1);
5861 impl->num_shortcuts--;
5865 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5866 g_assert_not_reached ();
5871 uri = gtk_file_system_path_to_uri (impl->file_system, path);
5873 GTK_FILE_CHOOSER_ERROR,
5874 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
5875 _("Shortcut %s does not exist"),
5883 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
5885 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
5891 if (impl->num_shortcuts == 0)
5894 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
5895 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5896 g_assert_not_reached ();
5900 for (i = 0; i < impl->num_shortcuts; i++)
5904 GtkFilePath *shortcut;
5906 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
5907 SHORTCUTS_COL_DATA, &col_data,
5908 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5910 g_assert (col_data != NULL);
5911 g_assert (!is_volume);
5913 shortcut = col_data;
5914 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
5916 if (i != impl->num_shortcuts - 1)
5918 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
5919 g_assert_not_reached ();
5923 return g_slist_reverse (list);
5926 /* Guesses a size based upon font sizes */
5928 find_good_size_from_style (GtkWidget *widget,
5932 GtkFileChooserDefault *impl;
5933 gint default_width, default_height;
5936 GtkRequisition preview_req;
5938 g_assert (widget->style != NULL);
5939 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5941 font_size = pango_font_description_get_size (widget->style->font_desc);
5942 font_size = PANGO_PIXELS (font_size);
5944 default_width = font_size * NUM_CHARS;
5945 default_height = font_size * NUM_LINES;
5947 /* Use at least the requisition size not including the preview widget */
5948 gtk_widget_size_request (widget, &req);
5950 if (impl->preview_widget_active && impl->preview_widget)
5951 gtk_widget_size_request (impl->preview_box, &preview_req);
5953 preview_req.width = 0;
5955 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
5956 default_height = MAX (default_height, req.height);
5958 *width = default_width;
5959 *height = default_height;
5963 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
5964 gint *default_width,
5965 gint *default_height)
5967 GtkFileChooserDefault *impl;
5969 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5971 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
5973 if (impl->preview_widget_active && impl->preview_widget)
5974 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
5978 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
5979 gboolean *resize_horizontally,
5980 gboolean *resize_vertically)
5982 GtkFileChooserDefault *impl;
5984 g_return_if_fail (resize_horizontally != NULL);
5985 g_return_if_fail (resize_vertically != NULL);
5987 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5989 *resize_horizontally = TRUE;
5990 *resize_vertically = TRUE;
5992 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
5993 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5995 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
5997 *resize_horizontally = FALSE;
5998 *resize_vertically = FALSE;
6003 struct switch_folder_closure {
6004 GtkFileChooserDefault *impl;
6005 const GtkFilePath *path;
6009 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
6011 switch_folder_foreach_cb (GtkTreeModel *model,
6016 struct switch_folder_closure *closure;
6017 GtkTreeIter child_iter;
6021 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
6023 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
6024 closure->num_selected++;
6027 /* Changes to the selected folder in the list view */
6029 switch_to_selected_folder (GtkFileChooserDefault *impl)
6031 GtkTreeSelection *selection;
6032 struct switch_folder_closure closure;
6034 /* We do this with foreach() rather than get_selected() as we may be in
6035 * multiple selection mode
6038 closure.impl = impl;
6039 closure.path = NULL;
6040 closure.num_selected = 0;
6042 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6043 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
6045 g_assert (closure.path && closure.num_selected == 1);
6047 change_folder_and_display_error (impl, closure.path);
6050 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
6053 static const GtkFileInfo *
6054 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
6055 gboolean *had_selection)
6057 GtkTreeSelection *selection;
6058 GtkTreeIter iter, child_iter;
6059 const GtkFileInfo *info;
6061 g_assert (!impl->select_multiple);
6062 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6063 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
6065 *had_selection = FALSE;
6069 *had_selection = TRUE;
6071 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6075 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6079 /* Gets the display name of the selected file in the file list; assumes single
6080 * selection mode and that something is selected.
6082 static const gchar *
6083 get_display_name_from_file_list (GtkFileChooserDefault *impl)
6085 const GtkFileInfo *info;
6086 gboolean had_selection;
6088 info = get_selected_file_info_from_file_list (impl, &had_selection);
6089 g_assert (had_selection);
6090 g_assert (info != NULL);
6092 return gtk_file_info_get_display_name (info);
6096 add_custom_button_to_dialog (GtkDialog *dialog,
6097 const gchar *mnemonic_label,
6098 const gchar *stock_id,
6103 button = gtk_button_new_with_mnemonic (mnemonic_label);
6104 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
6105 gtk_button_set_image (GTK_BUTTON (button),
6106 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
6107 gtk_widget_show (button);
6109 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
6112 /* Presents an overwrite confirmation dialog; returns whether we should accept
6116 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
6117 const gchar *file_part,
6118 const gchar *folder_display_name)
6120 GtkWindow *toplevel;
6124 toplevel = get_toplevel (GTK_WIDGET (impl));
6126 dialog = gtk_message_dialog_new (toplevel,
6127 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
6128 GTK_MESSAGE_QUESTION,
6130 _("A file named \"%s\" already exists. Do you want to replace it?"),
6132 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
6133 _("The file already exists in \"%s\". Replacing it will "
6134 "overwrite its contents."),
6135 folder_display_name);
6137 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
6138 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"), GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
6139 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
6141 response = gtk_dialog_run (GTK_DIALOG (dialog));
6143 gtk_widget_destroy (dialog);
6145 return (response == GTK_RESPONSE_ACCEPT);
6149 get_display_name_for_folder (GtkFileChooserDefault *impl,
6150 const GtkFilePath *path)
6153 GtkFilePath *parent_path;
6154 GtkFileFolder *parent_folder;
6157 display_name = NULL;
6159 parent_folder = NULL;
6162 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, NULL))
6165 parent_folder = gtk_file_system_get_folder (impl->file_system,
6166 parent_path ? parent_path : path,
6167 GTK_FILE_INFO_DISPLAY_NAME,
6172 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, NULL);
6176 display_name = g_strdup (gtk_file_info_get_display_name (info));
6181 gtk_file_path_free (parent_path);
6184 g_object_unref (parent_folder);
6187 gtk_file_info_free (info);
6189 return display_name;
6192 /* Does overwrite confirmation if appropriate, and returns whether the dialog
6193 * should respond. Can get the file part from the file list or the save entry.
6196 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
6197 const gchar *file_part,
6198 const GtkFilePath *parent_path)
6200 GtkFileChooserConfirmation conf;
6202 if (!impl->do_overwrite_confirmation)
6205 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
6207 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
6211 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
6213 char *parent_display_name;
6216 g_assert (file_part != NULL);
6218 parent_display_name = get_display_name_for_folder (impl, parent_path);
6219 if (!parent_display_name)
6220 return TRUE; /* Huh? Did the folder disappear? Let the caller deal with it */
6222 retval = confirm_dialog_should_accept_filename (impl, file_part, parent_display_name);
6223 g_free (parent_display_name);
6227 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
6230 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
6234 g_assert_not_reached ();
6239 /* Implementation for GtkFileChooserEmbed::should_respond() */
6241 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
6243 GtkFileChooserDefault *impl;
6244 GtkWidget *toplevel;
6245 GtkWidget *current_focus;
6247 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
6249 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
6250 g_assert (GTK_IS_WINDOW (toplevel));
6252 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
6254 if (current_focus == impl->browse_files_tree_view)
6256 /* The following array encodes what we do based on the impl->action and the
6257 * number of files selected.
6260 NOOP, /* Do nothing (don't respond) */
6261 RESPOND, /* Respond immediately */
6262 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
6263 ALL_FILES, /* Respond only if everything selected is a file */
6264 ALL_FOLDERS, /* Respond only if everything selected is a folder */
6265 SAVE_ENTRY, /* Go to the code for handling the save entry */
6266 NOT_REACHED /* Sanity check */
6268 static const ActionToTake what_to_do[4][3] = {
6269 /* 0 selected 1 selected many selected */
6270 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
6271 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
6272 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
6273 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
6277 gboolean all_files, all_folders;
6279 ActionToTake action;
6283 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6285 selection_check (impl, &num_selected, &all_files, &all_folders);
6287 if (num_selected > 2)
6292 action = what_to_do [impl->action] [k];
6302 case RESPOND_OR_SWITCH:
6303 g_assert (num_selected == 1);
6307 switch_to_selected_folder (impl);
6310 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6311 return should_respond_after_confirm_overwrite (impl,
6312 get_display_name_from_file_list (impl),
6313 impl->current_folder);
6327 g_assert_not_reached ();
6330 else if (current_focus == impl->save_file_name_entry)
6333 gboolean is_well_formed, is_empty, is_file_part_empty;
6336 GtkFileChooserEntry *entry;
6341 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6342 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6344 entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
6345 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty);
6347 if (is_empty || !is_well_formed)
6350 g_assert (path != NULL);
6353 is_folder = check_is_folder (impl->file_system, path, &error);
6356 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6358 _gtk_file_chooser_entry_set_file_part (entry, "");
6359 change_folder_and_display_error (impl, path);
6362 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
6364 /* The folder already exists, so we do not need to create it.
6365 * Just respond to terminate the dialog.
6372 gboolean file_exists_and_is_not_folder;
6374 file_exists_and_is_not_folder = g_error_matches (error, GTK_FILE_SYSTEM_ERROR, GTK_FILE_SYSTEM_ERROR_NOT_FOLDER);
6376 if (impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER && file_exists_and_is_not_folder)
6378 /* Oops, the user typed the name of an existing path which is not a folder */
6379 error_creating_folder_over_existing_file_dialog (impl, path, error);
6380 error = NULL; /* as it will be freed below for the general case */
6385 GtkFilePath *parent_path;
6386 gboolean parent_is_folder;
6388 /* check that everything up to the last component exists */
6390 parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
6391 parent_is_folder = check_is_folder (impl->file_system, parent_path, NULL);
6392 if (parent_is_folder)
6394 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6396 if (file_exists_and_is_not_folder)
6398 const char *file_part;
6400 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry));
6401 retval = should_respond_after_confirm_overwrite (impl, file_part, parent_path);
6406 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
6408 GError *create_error;
6410 create_error = NULL;
6411 if (gtk_file_system_create_folder (impl->file_system, path, &create_error))
6415 error_creating_folder_dialog (impl, path, create_error);
6422 /* This will display an error, which is what we want */
6423 change_folder_and_display_error (impl, parent_path);
6427 gtk_file_path_free (parent_path);
6431 g_error_free (error);
6434 gtk_file_path_free (path);
6437 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
6439 /* The focus is on a dialog's action area button, *and* the widget that
6440 * was focused immediately before it is the shortcuts list. Switch to the
6441 * selected shortcut and tell the caller not to respond.
6445 if (shortcuts_get_selected (impl, &iter))
6447 shortcuts_activate_iter (impl, &iter);
6449 gtk_widget_grab_focus (impl->browse_files_tree_view);
6456 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
6458 /* The focus is on a dialog's action area button, *and* the widget that
6459 * was focused immediately before it is the file list.
6464 /* The focus is on a dialog's action area button or something else */
6465 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6466 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6471 g_assert_not_reached ();
6475 /* Implementation for GtkFileChooserEmbed::initial_focus() */
6477 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
6479 GtkFileChooserDefault *impl;
6482 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
6484 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6485 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6486 widget = impl->browse_files_tree_view;
6487 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6488 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6489 widget = impl->save_file_name_entry;
6492 g_assert_not_reached ();
6496 gtk_widget_grab_focus (widget);
6500 set_current_filter (GtkFileChooserDefault *impl,
6501 GtkFileFilter *filter)
6503 if (impl->current_filter != filter)
6507 /* NULL filters are allowed to reset to non-filtered status
6509 filter_index = g_slist_index (impl->filters, filter);
6510 if (impl->filters && filter && filter_index < 0)
6513 if (impl->current_filter)
6514 g_object_unref (impl->current_filter);
6515 impl->current_filter = filter;
6516 if (impl->current_filter)
6518 g_object_ref (impl->current_filter);
6519 gtk_object_sink (GTK_OBJECT (filter));
6523 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
6526 if (impl->browse_files_model)
6527 install_list_model_filter (impl);
6529 g_object_notify (G_OBJECT (impl), "filter");
6534 filter_combo_changed (GtkComboBox *combo_box,
6535 GtkFileChooserDefault *impl)
6537 gint new_index = gtk_combo_box_get_active (combo_box);
6538 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
6540 set_current_filter (impl, new_filter);
6544 check_preview_change (GtkFileChooserDefault *impl)
6546 GtkTreePath *cursor_path;
6547 const GtkFilePath *new_path;
6548 const GtkFileInfo *new_info;
6550 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
6551 if (cursor_path && impl->sort_model)
6554 GtkTreeIter child_iter;
6556 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
6557 gtk_tree_path_free (cursor_path);
6559 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
6561 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
6562 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6570 if (new_path != impl->preview_path &&
6571 !(new_path && impl->preview_path &&
6572 gtk_file_path_compare (new_path, impl->preview_path) == 0))
6574 if (impl->preview_path)
6576 gtk_file_path_free (impl->preview_path);
6577 g_free (impl->preview_display_name);
6582 impl->preview_path = gtk_file_path_copy (new_path);
6583 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
6587 impl->preview_path = NULL;
6588 impl->preview_display_name = NULL;
6591 if (impl->use_preview_label && impl->preview_label)
6592 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
6594 g_signal_emit_by_name (impl, "update-preview");
6598 /* Activates a volume by mounting it if necessary and then switching to its
6602 shortcuts_activate_volume (GtkFileChooserDefault *impl,
6603 GtkFileSystemVolume *volume)
6607 /* We ref the file chooser since volume_mount() may run a main loop, and the
6608 * user could close the file chooser window in the meantime.
6610 g_object_ref (impl);
6612 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
6617 set_busy_cursor (impl, TRUE);
6620 result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
6626 msg = g_strdup_printf (_("Could not mount %s"),
6627 gtk_file_system_volume_get_display_name (impl->file_system, volume));
6628 error_message (impl, msg, error->message);
6630 g_error_free (error);
6633 set_busy_cursor (impl, FALSE);
6639 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
6640 change_folder_and_display_error (impl, path);
6641 gtk_file_path_free (path);
6645 g_object_unref (impl);
6648 /* Opens the folder or volume at the specified iter in the shortcuts model */
6650 shortcuts_activate_iter (GtkFileChooserDefault *impl,
6656 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
6657 SHORTCUTS_COL_DATA, &col_data,
6658 SHORTCUTS_COL_IS_VOLUME, &is_volume,
6662 return; /* We are on a separator */
6666 GtkFileSystemVolume *volume;
6670 shortcuts_activate_volume (impl, volume);
6674 const GtkFilePath *file_path;
6676 file_path = col_data;
6677 change_folder_and_display_error (impl, file_path);
6681 /* Callback used when a row in the shortcuts list is activated */
6683 shortcuts_row_activated_cb (GtkTreeView *tree_view,
6685 GtkTreeViewColumn *column,
6686 GtkFileChooserDefault *impl)
6689 GtkTreeIter child_iter;
6691 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
6694 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
6697 shortcuts_activate_iter (impl, &child_iter);
6699 gtk_widget_grab_focus (impl->browse_files_tree_view);
6702 /* Handler for GtkWidget::key-press-event on the shortcuts list */
6704 shortcuts_key_press_event_cb (GtkWidget *widget,
6706 GtkFileChooserDefault *impl)
6710 modifiers = gtk_accelerator_get_default_mod_mask ();
6712 if ((event->keyval == GDK_BackSpace
6713 || event->keyval == GDK_Delete
6714 || event->keyval == GDK_KP_Delete)
6715 && (event->state & modifiers) == 0)
6717 remove_selected_bookmarks (impl);
6725 shortcuts_select_func (GtkTreeSelection *selection,
6726 GtkTreeModel *model,
6728 gboolean path_currently_selected,
6731 GtkFileChooserDefault *impl = data;
6733 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
6737 list_select_func (GtkTreeSelection *selection,
6738 GtkTreeModel *model,
6740 gboolean path_currently_selected,
6743 GtkFileChooserDefault *impl = data;
6745 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6746 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6748 GtkTreeIter iter, child_iter;
6749 const GtkFileInfo *info;
6751 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
6754 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
6756 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6758 if (info && !gtk_file_info_get_is_folder (info))
6766 list_selection_changed (GtkTreeSelection *selection,
6767 GtkFileChooserDefault *impl)
6769 /* See if we are in the new folder editable row for Save mode */
6770 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6772 const GtkFileInfo *info;
6773 gboolean had_selection;
6775 info = get_selected_file_info_from_file_list (impl, &had_selection);
6777 goto out; /* normal processing */
6780 return; /* We are on the editable row for New Folder */
6785 update_chooser_entry (impl);
6786 check_preview_change (impl);
6787 bookmarks_check_add_sensitivity (impl);
6789 g_signal_emit_by_name (impl, "selection-changed", 0);
6792 /* Callback used when a row in the file list is activated */
6794 list_row_activated (GtkTreeView *tree_view,
6796 GtkTreeViewColumn *column,
6797 GtkFileChooserDefault *impl)
6799 GtkTreeIter iter, child_iter;
6800 const GtkFileInfo *info;
6802 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
6805 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
6807 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6809 if (gtk_file_info_get_is_folder (info))
6811 const GtkFilePath *file_path;
6813 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
6814 change_folder_and_display_error (impl, file_path);
6819 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
6820 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6821 g_signal_emit_by_name (impl, "file-activated");
6825 path_bar_clicked (GtkPathBar *path_bar,
6826 GtkFilePath *file_path,
6827 gboolean child_is_hidden,
6828 GtkFileChooserDefault *impl)
6830 if (!change_folder_and_display_error (impl, file_path))
6833 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
6834 * show hidden files so that ".baz" appears in the file list, as it will still
6835 * be shown in the path bar: "/foo/[bar]/.baz"
6837 if (child_is_hidden)
6838 g_object_set (impl, "show-hidden", TRUE, NULL);
6841 static const GtkFileInfo *
6842 get_list_file_info (GtkFileChooserDefault *impl,
6845 GtkTreeIter child_iter;
6847 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6851 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6855 list_icon_data_func (GtkTreeViewColumn *tree_column,
6856 GtkCellRenderer *cell,
6857 GtkTreeModel *tree_model,
6861 GtkFileChooserDefault *impl = data;
6862 GtkTreeIter child_iter;
6863 const GtkFilePath *path;
6865 const GtkFileInfo *info;
6866 gboolean sensitive = TRUE;
6868 profile_start ("start", NULL);
6870 info = get_list_file_info (impl, iter);
6872 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6875 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
6879 /* FIXME: NULL GError */
6880 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
6881 impl->icon_size, NULL);
6885 /* We are on the editable row */
6889 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6890 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
6891 sensitive = gtk_file_info_get_is_folder (info);
6895 "sensitive", sensitive,
6899 g_object_unref (pixbuf);
6901 profile_end ("end", NULL);
6905 list_name_data_func (GtkTreeViewColumn *tree_column,
6906 GtkCellRenderer *cell,
6907 GtkTreeModel *tree_model,
6911 GtkFileChooserDefault *impl = data;
6912 const GtkFileInfo *info = get_list_file_info (impl, iter);
6913 gboolean sensitive = TRUE;
6918 "text", _("Type name of new folder"),
6925 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
6926 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6928 sensitive = gtk_file_info_get_is_folder (info);
6932 "text", gtk_file_info_get_display_name (info),
6933 "sensitive", sensitive,
6939 list_size_data_func (GtkTreeViewColumn *tree_column,
6940 GtkCellRenderer *cell,
6941 GtkTreeModel *tree_model,
6945 GtkFileChooserDefault *impl = data;
6946 const GtkFileInfo *info = get_list_file_info (impl, iter);
6949 gboolean sensitive = TRUE;
6951 if (!info || gtk_file_info_get_is_folder (info))
6953 g_object_set (cell,"sensitive", sensitive, NULL);
6957 size = gtk_file_info_get_size (info);
6959 if (size < (gint64)1024)
6960 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
6961 else if (size < (gint64)1024*1024)
6962 str = g_strdup_printf (_("%.1f K"), size / (1024.));
6963 else if (size < (gint64)1024*1024*1024)
6964 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
6966 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
6968 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
6969 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6974 "sensitive", sensitive,
6981 /* Tree column data callback for the file list; fetches the mtime of a file */
6983 list_mtime_data_func (GtkTreeViewColumn *tree_column,
6984 GtkCellRenderer *cell,
6985 GtkTreeModel *tree_model,
6989 GtkFileChooserDefault *impl;
6990 const GtkFileInfo *info;
6991 GtkFileTime time_mtime, time_now;
6995 gboolean sensitive = TRUE;
6999 info = get_list_file_info (impl, iter);
7009 time_mtime = gtk_file_info_get_modification_time (info);
7011 if (time_mtime == 0)
7012 strcpy (buf, _("Unknown"));
7015 g_date_set_time (&mtime, (GTime) time_mtime);
7017 time_now = (GTime ) time (NULL);
7018 g_date_set_time (&now, (GTime) time_now);
7020 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
7023 strcpy (buf, _("Today"));
7024 else if (days_diff == 1)
7025 strcpy (buf, _("Yesterday"));
7030 if (days_diff > 1 && days_diff < 7)
7031 format = "%A"; /* Days from last week */
7033 format = "%x"; /* Any other date */
7035 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
7036 strcpy (buf, _("Unknown"));
7040 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
7041 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7042 sensitive = gtk_file_info_get_is_folder (info);
7046 "sensitive", sensitive,
7051 _gtk_file_chooser_default_new (const char *file_system)
7053 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
7054 "file-system-backend", file_system,
7059 location_entry_create (GtkFileChooserDefault *impl,
7064 entry = _gtk_file_chooser_entry_new (TRUE);
7065 /* Pick a good width for the entry */
7066 gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
7067 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
7068 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
7069 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (entry), impl->action);
7072 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry),
7073 gtk_file_path_new_steal ((gchar *)path));
7074 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), path);
7078 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
7079 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7080 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7081 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), "");
7082 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7083 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7084 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry),
7085 gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
7087 g_assert_not_reached ();
7090 return GTK_WIDGET (entry);
7094 update_from_entry (GtkFileChooserDefault *impl,
7096 GtkFileChooserEntry *chooser_entry)
7098 const GtkFilePath *folder_path;
7099 const char *file_part;
7101 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
7102 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
7104 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
7106 error_message_with_parent (parent,
7107 _("Cannot change folder"),
7108 _("The folder you specified is an invalid path."));
7112 if (file_part[0] == '\0')
7113 return change_folder_and_display_error (impl, folder_path);
7116 GtkFileFolder *folder = NULL;
7117 GtkFilePath *subfolder_path = NULL;
7118 GtkFileInfo *info = NULL;
7124 /* If the file part is non-empty, we need to figure out if it refers to a
7125 * folder within folder. We could optimize the case here where the folder
7126 * is already loaded for one of our tree models.
7130 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
7134 error_getting_info_dialog (impl, folder_path, error);
7139 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
7141 if (!subfolder_path)
7146 uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
7147 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s'"),
7149 error_message (impl, msg, error->message);
7156 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
7160 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7161 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7163 if (!change_folder_and_display_error (impl, folder_path))
7166 gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
7169 error_getting_info_dialog (impl, subfolder_path, error);
7174 if (gtk_file_info_get_is_folder (info))
7175 result = change_folder_and_display_error (impl, subfolder_path);
7181 result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
7183 error_dialog (impl, _("Could not select item"),
7184 subfolder_path, error);
7190 g_object_unref (folder);
7192 gtk_file_path_free (subfolder_path);
7195 gtk_file_info_free (info);
7200 g_assert_not_reached ();
7204 location_popup_handler (GtkFileChooserDefault *impl,
7208 GtkWindow *toplevel;
7214 const char *accept_stock;
7218 toplevel = get_toplevel (GTK_WIDGET (impl));
7220 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7221 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7223 title = _("Open Location");
7224 accept_stock = GTK_STOCK_OPEN;
7228 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7229 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7230 title = _("Save in Location");
7231 accept_stock = GTK_STOCK_SAVE;
7234 dialog = gtk_dialog_new_with_buttons (title,
7236 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
7237 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
7238 accept_stock, GTK_RESPONSE_ACCEPT,
7240 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
7241 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
7242 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
7243 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7245 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
7246 GTK_RESPONSE_ACCEPT,
7247 GTK_RESPONSE_CANCEL,
7250 hbox = gtk_hbox_new (FALSE, 12);
7251 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
7252 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
7254 label = gtk_label_new_with_mnemonic (_("_Location:"));
7255 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
7257 entry = location_entry_create (impl, path);
7259 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
7260 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
7264 gtk_widget_show_all (dialog);
7265 /* If the dialog is brought up by typing the first characters
7266 * of a path, unselect the text in the entry, so that you can
7267 * just type on without erasing the initial part.
7270 gtk_editable_select_region (GTK_EDITABLE (entry), -1, -1);
7274 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
7276 if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
7278 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7279 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7281 gtk_widget_grab_focus (impl->browse_files_tree_view);
7285 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7286 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7287 gtk_widget_grab_focus (impl->save_file_name_entry);
7295 GtkWindow *toplevel;
7297 toplevel = get_toplevel (GTK_WIDGET (impl));
7298 if (toplevel && toplevel->focus_widget)
7299 gtk_widget_grab_focus (toplevel->focus_widget);
7302 gtk_widget_destroy (dialog);
7305 /* Handler for the "up-folder" keybinding signal */
7307 up_folder_handler (GtkFileChooserDefault *impl)
7309 pending_select_paths_add (impl, impl->current_folder);
7310 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
7313 /* Handler for the "down-folder" keybinding signal */
7315 down_folder_handler (GtkFileChooserDefault *impl)
7317 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
7320 /* Handler for the "home-folder" keybinding signal */
7322 home_folder_handler (GtkFileChooserDefault *impl)
7327 if (!impl->has_home)
7328 return; /* Should we put up an error dialog? */
7330 pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
7331 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7332 g_assert_not_reached ();
7334 shortcuts_activate_iter (impl, &iter);
7339 /* Drag and drop interfaces */
7342 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
7347 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
7352 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
7354 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
7357 ShortcutsModelFilter *model;
7361 model = SHORTCUTS_MODEL_FILTER (drag_source);
7363 pos = *gtk_tree_path_get_indices (path);
7364 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
7366 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
7369 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
7371 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
7373 GtkSelectionData *selection_data)
7375 ShortcutsModelFilter *model;
7377 model = SHORTCUTS_MODEL_FILTER (drag_source);
7384 /* Fill the GtkTreeDragSourceIface vtable */
7386 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
7388 iface->row_draggable = shortcuts_model_filter_row_draggable;
7389 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
7393 /* Fill the GtkTreeDragDestIface vtable */
7395 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
7397 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
7398 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
7402 static GtkTreeModel *
7403 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
7404 GtkTreeModel *child_model,
7407 ShortcutsModelFilter *model;
7409 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
7410 "child-model", child_model,
7411 "virtual-root", root,
7416 return GTK_TREE_MODEL (model);
7419 #define __GTK_FILE_CHOOSER_DEFAULT_C__
7420 #include "gtkaliasdef.c"