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 "gtkclipboard.h"
32 #include "gtkcombobox.h"
34 #include "gtkeventbox.h"
35 #include "gtkexpander.h"
36 #include "gtkfilechooserprivate.h"
37 #include "gtkfilechooserdefault.h"
38 #include "gtkfilechooserembed.h"
39 #include "gtkfilechooserentry.h"
40 #include "gtkfilechoosersettings.h"
41 #include "gtkfilechooserutils.h"
42 #include "gtkfilechooser.h"
43 #include "gtkfilesystemmodel.h"
46 #include "gtkhpaned.h"
47 #include "gtkiconfactory.h"
48 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
53 #include "gtkmarshalers.h"
54 #include "gtkmenuitem.h"
55 #include "gtkmessagedialog.h"
56 #include "gtkpathbar.h"
57 #include "gtkprivate.h"
58 #include "gtkradiobutton.h"
59 #include "gtkscrolledwindow.h"
60 #include "gtkseparatormenuitem.h"
61 #include "gtksizegroup.h"
64 #include "gtktreednd.h"
65 #include "gtktreeprivate.h"
66 #include "gtktreeselection.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"
92 #undef PROFILE_FILE_CHOOSER
93 #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 ? func : "", msg1 ? msg1 : "", msg2 ? msg2 : "");
122 str = g_strdup_printf ("MARK: %*c %s %s %s", profile_indent - 1, ' ', func ? 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))
148 #define MAX_LOADING_TIME 500
150 struct _GtkFileChooserDefaultClass
152 GtkVBoxClass parent_class;
158 LOCATION_POPUP_ON_PASTE,
167 static guint signals[LAST_SIGNAL] = { 0 };
169 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
171 SHORTCUTS_COL_PIXBUF,
174 SHORTCUTS_COL_IS_VOLUME,
175 SHORTCUTS_COL_REMOVABLE,
176 SHORTCUTS_COL_PIXBUF_VISIBLE,
177 SHORTCUTS_COL_HANDLE,
178 SHORTCUTS_COL_NUM_COLUMNS
181 /* Column numbers for the file list */
186 FILE_LIST_COL_NUM_COLUMNS
189 /* Identifiers for target types */
195 /* Target types for dragging from the shortcuts list */
196 static const GtkTargetEntry shortcuts_source_targets[] = {
197 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
200 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
201 / sizeof (shortcuts_source_targets[0]));
203 /* Target types for dropping into the shortcuts list */
204 static const GtkTargetEntry shortcuts_dest_targets[] = {
205 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
206 { "text/uri-list", 0, TEXT_URI_LIST }
209 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
210 / sizeof (shortcuts_dest_targets[0]));
212 /* Target types for DnD from the file list */
213 static const GtkTargetEntry file_list_source_targets[] = {
214 { "text/uri-list", 0, TEXT_URI_LIST }
217 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
218 / sizeof (file_list_source_targets[0]));
220 /* Target types for dropping into the file list */
221 static const GtkTargetEntry file_list_dest_targets[] = {
222 { "text/uri-list", 0, TEXT_URI_LIST }
225 static const int num_file_list_dest_targets = (sizeof (file_list_dest_targets)
226 / sizeof (file_list_dest_targets[0]));
229 /* Interesting places in the shortcuts bar */
235 SHORTCUTS_BOOKMARKS_SEPARATOR,
237 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
238 SHORTCUTS_CURRENT_FOLDER
241 /* Icon size for if we can't get it from the theme */
242 #define FALLBACK_ICON_SIZE 16
244 #define PREVIEW_HBOX_SPACING 12
248 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
249 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
251 static GObject* gtk_file_chooser_default_constructor (GType type,
252 guint n_construct_properties,
253 GObjectConstructParam *construct_params);
254 static void gtk_file_chooser_default_finalize (GObject *object);
255 static void gtk_file_chooser_default_set_property (GObject *object,
259 static void gtk_file_chooser_default_get_property (GObject *object,
263 static void gtk_file_chooser_default_dispose (GObject *object);
264 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
265 static void gtk_file_chooser_default_map (GtkWidget *widget);
266 static void gtk_file_chooser_default_unmap (GtkWidget *widget);
267 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
268 GtkWidget *previous_toplevel);
269 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
270 GtkStyle *previous_style);
271 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
272 GdkScreen *previous_screen);
274 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
275 const GtkFilePath *path,
277 static gboolean gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
278 const GtkFilePath *path,
281 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
282 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
284 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
285 const GtkFilePath *path,
287 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
288 const GtkFilePath *path);
289 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
290 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
291 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
292 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
293 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
294 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
295 GtkFileFilter *filter);
296 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
297 GtkFileFilter *filter);
298 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
299 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
300 const GtkFilePath *path,
302 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
303 const GtkFilePath *path,
305 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
307 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
309 gint *default_height);
310 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
311 gboolean *resize_horizontally,
312 gboolean *resize_vertically);
313 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
314 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
316 static void location_popup_handler (GtkFileChooserDefault *impl,
318 static void location_popup_on_paste_handler (GtkFileChooserDefault *impl);
319 static void up_folder_handler (GtkFileChooserDefault *impl);
320 static void down_folder_handler (GtkFileChooserDefault *impl);
321 static void home_folder_handler (GtkFileChooserDefault *impl);
322 static void desktop_folder_handler (GtkFileChooserDefault *impl);
323 static void quick_bookmark_handler (GtkFileChooserDefault *impl,
324 gint bookmark_index);
325 static void update_appearance (GtkFileChooserDefault *impl);
327 static void set_current_filter (GtkFileChooserDefault *impl,
328 GtkFileFilter *filter);
329 static void check_preview_change (GtkFileChooserDefault *impl);
331 static void filter_combo_changed (GtkComboBox *combo_box,
332 GtkFileChooserDefault *impl);
333 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
335 GtkTreeViewColumn *column,
336 GtkFileChooserDefault *impl);
338 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
340 GtkFileChooserDefault *impl);
342 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
345 gboolean path_currently_selected,
347 static gboolean shortcuts_get_selected (GtkFileChooserDefault *impl,
349 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
351 static int shortcuts_get_index (GtkFileChooserDefault *impl,
352 ShortcutsIndex where);
353 static int shortcut_find_position (GtkFileChooserDefault *impl,
354 const GtkFilePath *path);
356 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
358 static gboolean list_select_func (GtkTreeSelection *selection,
361 gboolean path_currently_selected,
364 static void list_selection_changed (GtkTreeSelection *tree_selection,
365 GtkFileChooserDefault *impl);
366 static void list_row_activated (GtkTreeView *tree_view,
368 GtkTreeViewColumn *column,
369 GtkFileChooserDefault *impl);
371 static void select_func (GtkFileSystemModel *model,
376 static void path_bar_clicked (GtkPathBar *path_bar,
377 GtkFilePath *file_path,
378 GtkFilePath *child_path,
379 gboolean child_is_hidden,
380 GtkFileChooserDefault *impl);
382 static void add_bookmark_button_clicked_cb (GtkButton *button,
383 GtkFileChooserDefault *impl);
384 static void remove_bookmark_button_clicked_cb (GtkButton *button,
385 GtkFileChooserDefault *impl);
386 static void save_folder_combo_changed_cb (GtkComboBox *combo,
387 GtkFileChooserDefault *impl);
389 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
390 GtkCellRenderer *cell,
391 GtkTreeModel *tree_model,
394 static void list_name_data_func (GtkTreeViewColumn *tree_column,
395 GtkCellRenderer *cell,
396 GtkTreeModel *tree_model,
400 static void list_size_data_func (GtkTreeViewColumn *tree_column,
401 GtkCellRenderer *cell,
402 GtkTreeModel *tree_model,
406 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
407 GtkCellRenderer *cell,
408 GtkTreeModel *tree_model,
412 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
415 static void load_remove_timer (GtkFileChooserDefault *impl);
416 static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
418 static void location_button_toggled_cb (GtkToggleButton *toggle,
419 GtkFileChooserDefault *impl);
420 static void location_switch_to_path_bar (GtkFileChooserDefault *impl);
425 /* Drag and drop interface declarations */
428 GtkTreeModelFilter parent;
430 GtkFileChooserDefault *impl;
431 } ShortcutsModelFilter;
434 GtkTreeModelFilterClass parent_class;
435 } ShortcutsModelFilterClass;
437 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
438 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
440 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
442 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
443 _shortcuts_model_filter,
444 GTK_TYPE_TREE_MODEL_FILTER,
445 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
446 shortcuts_model_filter_drag_source_iface_init))
448 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
449 GtkTreeModel *child_model,
454 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDefault, _gtk_file_chooser_default, GTK_TYPE_VBOX,
455 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
456 gtk_file_chooser_default_iface_init)
457 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER_EMBED,
458 gtk_file_chooser_embed_default_iface_init));
461 _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
463 static const guint quick_bookmark_keyvals[10] = {
464 GDK_1, GDK_2, GDK_3, GDK_4, GDK_5, GDK_6, GDK_7, GDK_8, GDK_9, GDK_0
466 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
467 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
468 GtkBindingSet *binding_set;
471 gobject_class->finalize = gtk_file_chooser_default_finalize;
472 gobject_class->constructor = gtk_file_chooser_default_constructor;
473 gobject_class->set_property = gtk_file_chooser_default_set_property;
474 gobject_class->get_property = gtk_file_chooser_default_get_property;
475 gobject_class->dispose = gtk_file_chooser_default_dispose;
477 widget_class->show_all = gtk_file_chooser_default_show_all;
478 widget_class->map = gtk_file_chooser_default_map;
479 widget_class->unmap = gtk_file_chooser_default_unmap;
480 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
481 widget_class->style_set = gtk_file_chooser_default_style_set;
482 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
484 signals[LOCATION_POPUP] =
485 _gtk_binding_signal_new (I_("location-popup"),
486 G_OBJECT_CLASS_TYPE (class),
487 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
488 G_CALLBACK (location_popup_handler),
490 _gtk_marshal_VOID__STRING,
491 G_TYPE_NONE, 1, G_TYPE_STRING);
492 signals[LOCATION_POPUP_ON_PASTE] =
493 _gtk_binding_signal_new ("location-popup-on-paste",
494 G_OBJECT_CLASS_TYPE (class),
495 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
496 G_CALLBACK (location_popup_on_paste_handler),
498 _gtk_marshal_VOID__VOID,
501 _gtk_binding_signal_new (I_("up-folder"),
502 G_OBJECT_CLASS_TYPE (class),
503 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
504 G_CALLBACK (up_folder_handler),
506 _gtk_marshal_VOID__VOID,
508 signals[DOWN_FOLDER] =
509 _gtk_binding_signal_new (I_("down-folder"),
510 G_OBJECT_CLASS_TYPE (class),
511 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
512 G_CALLBACK (down_folder_handler),
514 _gtk_marshal_VOID__VOID,
516 signals[HOME_FOLDER] =
517 _gtk_binding_signal_new (I_("home-folder"),
518 G_OBJECT_CLASS_TYPE (class),
519 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
520 G_CALLBACK (home_folder_handler),
522 _gtk_marshal_VOID__VOID,
524 signals[DESKTOP_FOLDER] =
525 _gtk_binding_signal_new (I_("desktop-folder"),
526 G_OBJECT_CLASS_TYPE (class),
527 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
528 G_CALLBACK (desktop_folder_handler),
530 _gtk_marshal_VOID__VOID,
532 signals[QUICK_BOOKMARK] =
533 _gtk_binding_signal_new (I_("quick-bookmark"),
534 G_OBJECT_CLASS_TYPE (class),
535 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
536 G_CALLBACK (quick_bookmark_handler),
538 _gtk_marshal_VOID__INT,
539 G_TYPE_NONE, 1, G_TYPE_INT);
541 binding_set = gtk_binding_set_by_class (class);
543 gtk_binding_entry_add_signal (binding_set,
544 GDK_l, GDK_CONTROL_MASK,
546 1, G_TYPE_STRING, "");
547 /* FMQ: remove this? */
549 gtk_binding_entry_add_signal (binding_set,
552 1, G_TYPE_STRING, "/");
553 gtk_binding_entry_add_signal (binding_set,
556 1, G_TYPE_STRING, "/");
559 gtk_binding_entry_add_signal (binding_set,
562 1, G_TYPE_STRING, "~");
566 gtk_binding_entry_add_signal (binding_set,
567 GDK_v, GDK_CONTROL_MASK,
568 "location-popup-on-paste",
571 gtk_binding_entry_add_signal (binding_set,
572 GDK_Up, GDK_MOD1_MASK,
575 gtk_binding_entry_add_signal (binding_set,
579 gtk_binding_entry_add_signal (binding_set,
580 GDK_KP_Up, GDK_MOD1_MASK,
584 gtk_binding_entry_add_signal (binding_set,
585 GDK_Down, GDK_MOD1_MASK,
588 gtk_binding_entry_add_signal (binding_set,
589 GDK_KP_Down, GDK_MOD1_MASK,
593 gtk_binding_entry_add_signal (binding_set,
594 GDK_Home, GDK_MOD1_MASK,
597 gtk_binding_entry_add_signal (binding_set,
598 GDK_KP_Home, GDK_MOD1_MASK,
601 gtk_binding_entry_add_signal (binding_set,
602 GDK_d, GDK_MOD1_MASK,
606 for (i = 0; i < 10; i++)
607 gtk_binding_entry_add_signal (binding_set,
608 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
612 _gtk_file_chooser_install_properties (gobject_class);
614 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
615 P_("Default file chooser backend"),
616 P_("Name of the GtkFileChooser backend to use by default"),
618 GTK_PARAM_READWRITE));
622 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
624 iface->select_path = gtk_file_chooser_default_select_path;
625 iface->unselect_path = gtk_file_chooser_default_unselect_path;
626 iface->select_all = gtk_file_chooser_default_select_all;
627 iface->unselect_all = gtk_file_chooser_default_unselect_all;
628 iface->get_paths = gtk_file_chooser_default_get_paths;
629 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
630 iface->get_file_system = gtk_file_chooser_default_get_file_system;
631 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
632 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
633 iface->set_current_name = gtk_file_chooser_default_set_current_name;
634 iface->add_filter = gtk_file_chooser_default_add_filter;
635 iface->remove_filter = gtk_file_chooser_default_remove_filter;
636 iface->list_filters = gtk_file_chooser_default_list_filters;
637 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
638 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
639 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
643 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
645 iface->get_default_size = gtk_file_chooser_default_get_default_size;
646 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
647 iface->should_respond = gtk_file_chooser_default_should_respond;
648 iface->initial_focus = gtk_file_chooser_default_initial_focus;
652 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
654 profile_start ("start", NULL);
655 #ifdef PROFILE_FILE_CHOOSER
656 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
658 impl->local_only = TRUE;
659 impl->preview_widget_active = TRUE;
660 impl->use_preview_label = TRUE;
661 impl->select_multiple = FALSE;
662 impl->show_hidden = FALSE;
663 impl->icon_size = FALLBACK_ICON_SIZE;
664 impl->load_state = LOAD_EMPTY;
665 impl->reload_state = RELOAD_EMPTY;
666 impl->pending_select_paths = NULL;
667 impl->location_mode = LOCATION_MODE_PATH_BAR;
669 gtk_box_set_spacing (GTK_BOX (impl), 12);
671 impl->tooltips = gtk_tooltips_new ();
672 g_object_ref_sink (impl->tooltips);
674 profile_end ("end", NULL);
677 /* Frees the data columns for the specified iter in the shortcuts model*/
679 shortcuts_free_row_data (GtkFileChooserDefault *impl,
684 GtkFileSystemHandle *handle;
686 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
687 SHORTCUTS_COL_DATA, &col_data,
688 SHORTCUTS_COL_IS_VOLUME, &is_volume,
689 SHORTCUTS_COL_HANDLE, &handle,
693 gtk_file_system_cancel_operation (handle);
700 GtkFileSystemVolume *volume;
703 gtk_file_system_volume_free (impl->file_system, volume);
710 gtk_file_path_free (path);
714 /* Frees all the data columns in the shortcuts model */
716 shortcuts_free (GtkFileChooserDefault *impl)
720 if (!impl->shortcuts_model)
723 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
726 shortcuts_free_row_data (impl, &iter);
728 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
730 g_object_unref (impl->shortcuts_model);
731 impl->shortcuts_model = NULL;
735 pending_select_paths_free (GtkFileChooserDefault *impl)
739 for (l = impl->pending_select_paths; l; l = l->next)
744 gtk_file_path_free (path);
747 g_slist_free (impl->pending_select_paths);
748 impl->pending_select_paths = NULL;
752 pending_select_paths_add (GtkFileChooserDefault *impl,
753 const GtkFilePath *path)
755 impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
758 /* Used from gtk_tree_selection_selected_foreach() */
760 store_selection_foreach (GtkTreeModel *model,
765 GtkFileChooserDefault *impl;
766 GtkTreeIter child_iter;
767 const GtkFilePath *file_path;
769 impl = GTK_FILE_CHOOSER_DEFAULT (data);
771 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
773 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
774 pending_select_paths_add (impl, file_path);
777 /* Stores the current selection in the list of paths to select; this is used to
778 * preserve the selection when reloading the current folder.
781 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
783 GtkTreeSelection *selection;
785 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
786 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
790 gtk_file_chooser_default_finalize (GObject *object)
792 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
795 if (impl->shortcuts_filter_model)
796 g_object_unref (impl->shortcuts_filter_model);
798 shortcuts_free (impl);
800 g_object_unref (impl->file_system);
802 for (l = impl->filters; l; l = l->next)
804 GtkFileFilter *filter;
806 filter = GTK_FILE_FILTER (l->data);
807 g_object_unref (filter);
809 g_slist_free (impl->filters);
811 if (impl->current_filter)
812 g_object_unref (impl->current_filter);
814 if (impl->current_volume_path)
815 gtk_file_path_free (impl->current_volume_path);
817 if (impl->current_folder)
818 gtk_file_path_free (impl->current_folder);
820 if (impl->preview_path)
821 gtk_file_path_free (impl->preview_path);
823 load_remove_timer (impl);
825 /* Free all the Models we have */
826 if (impl->browse_files_model)
827 g_object_unref (impl->browse_files_model);
829 if (impl->sort_model)
830 g_object_unref (impl->sort_model);
832 g_free (impl->preview_display_name);
834 g_free (impl->edited_new_text);
836 g_object_unref (impl->tooltips);
838 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
841 /* Shows an error dialog set as transient for the specified window */
843 error_message_with_parent (GtkWindow *parent,
849 dialog = gtk_message_dialog_new (parent,
850 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
855 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
859 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
861 gtk_dialog_run (GTK_DIALOG (dialog));
862 gtk_widget_destroy (dialog);
865 /* Returns a toplevel GtkWindow, or NULL if none */
867 get_toplevel (GtkWidget *widget)
871 toplevel = gtk_widget_get_toplevel (widget);
872 if (!GTK_WIDGET_TOPLEVEL (toplevel))
875 return GTK_WINDOW (toplevel);
878 /* Shows an error dialog for the file chooser */
880 error_message (GtkFileChooserDefault *impl,
884 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
887 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
889 error_dialog (GtkFileChooserDefault *impl,
891 const GtkFilePath *path,
900 uri = gtk_file_system_path_to_uri (impl->file_system, path);
901 text = g_strdup_printf (msg, uri);
902 error_message (impl, text, error->message);
905 g_error_free (error);
909 /* Displays an error message about not being able to get information for a file.
910 * Frees the GError as well.
913 error_getting_info_dialog (GtkFileChooserDefault *impl,
914 const GtkFilePath *path,
918 _("Could not retrieve information about the file"),
922 /* Shows an error dialog about not being able to add a bookmark */
924 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
925 const GtkFilePath *path,
929 _("Could not add a bookmark"),
933 /* Shows an error dialog about not being able to remove a bookmark */
935 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
936 const GtkFilePath *path,
940 _("Could not remove bookmark"),
944 /* Shows an error dialog about not being able to create a folder */
946 error_creating_folder_dialog (GtkFileChooserDefault *impl,
947 const GtkFilePath *path,
951 _("The folder could not be created"),
955 /* Shows an error about not being able to create a folder because a file with
956 * the same name is already there.
959 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
960 const GtkFilePath *path,
964 _("The folder could not be created, as a file with the same name "
965 "already exists. Try using a different name for the folder, "
966 "or rename the file first."),
970 /* Shows an error dialog about not being able to create a filename */
972 error_building_filename_dialog (GtkFileChooserDefault *impl,
973 const GtkFilePath *folder_part,
974 const char *file_part,
977 error_dialog (impl, _("Invalid file name"),
981 /* Shows an error dialog when we cannot switch to a folder */
983 error_changing_folder_dialog (GtkFileChooserDefault *impl,
984 const GtkFilePath *path,
987 error_dialog (impl, _("The folder contents could not be displayed"),
991 /* Changes folders, displaying an error dialog if this fails */
993 change_folder_and_display_error (GtkFileChooserDefault *impl,
994 const GtkFilePath *path)
998 GtkFilePath *path_copy;
1000 g_return_val_if_fail (path != NULL, FALSE);
1002 profile_start ("start", (char *) path);
1004 /* We copy the path because of this case:
1006 * list_row_activated()
1007 * fetches path from model; path belongs to the model (*)
1008 * calls change_folder_and_display_error()
1009 * calls _gtk_file_chooser_set_current_folder_path()
1010 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1013 path_copy = gtk_file_path_copy (path);
1016 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, &error);
1019 error_changing_folder_dialog (impl, path_copy, error);
1021 gtk_file_path_free (path_copy);
1023 profile_end ("end", (char *) path);
1029 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1031 if (impl->use_preview_label)
1033 if (!impl->preview_label)
1035 impl->preview_label = gtk_label_new (impl->preview_display_name);
1036 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1037 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1038 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1039 gtk_widget_show (impl->preview_label);
1044 if (impl->preview_label)
1046 gtk_widget_destroy (impl->preview_label);
1047 impl->preview_label = NULL;
1051 if (impl->preview_widget_active && impl->preview_widget)
1052 gtk_widget_show (impl->preview_box);
1054 gtk_widget_hide (impl->preview_box);
1056 g_signal_emit_by_name (impl, "default-size-changed");
1060 set_preview_widget (GtkFileChooserDefault *impl,
1061 GtkWidget *preview_widget)
1063 if (preview_widget == impl->preview_widget)
1066 if (impl->preview_widget)
1067 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1068 impl->preview_widget);
1070 impl->preview_widget = preview_widget;
1071 if (impl->preview_widget)
1073 gtk_widget_show (impl->preview_widget);
1074 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1075 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1076 impl->preview_widget,
1077 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1080 update_preview_widget_visibility (impl);
1083 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1084 struct ReloadIconsData
1086 GtkFileChooserDefault *impl;
1087 GtkTreeRowReference *row_ref;
1091 shortcuts_reload_icons_get_info_cb (GtkFileSystemHandle *handle,
1092 const GtkFileInfo *info,
1093 const GError *error,
1099 gboolean cancelled = handle->cancelled;
1100 struct ReloadIconsData *data = user_data;
1102 if (!g_slist_find (data->impl->reload_icon_handles, handle))
1105 data->impl->reload_icon_handles = g_slist_remove (data->impl->reload_icon_handles, handle);
1107 if (cancelled || error)
1110 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl),
1111 data->impl->icon_size, NULL);
1113 path = gtk_tree_row_reference_get_path (data->row_ref);
1114 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1115 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1116 SHORTCUTS_COL_PIXBUF, pixbuf,
1118 gtk_tree_path_free (path);
1121 g_object_unref (pixbuf);
1124 gtk_tree_row_reference_free (data->row_ref);
1125 g_object_unref (data->impl);
1128 g_object_unref (handle);
1132 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1137 profile_start ("start", NULL);
1139 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1142 for (l = impl->reload_icon_handles; l; l = l->next)
1144 GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1145 gtk_file_system_cancel_operation (handle);
1147 g_slist_free (impl->reload_icon_handles);
1148 impl->reload_icon_handles = NULL;
1154 gboolean pixbuf_visible;
1157 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1158 SHORTCUTS_COL_DATA, &data,
1159 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1160 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1163 if (pixbuf_visible && data)
1167 GtkFileSystemVolume *volume;
1170 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1171 impl->icon_size, NULL);
1173 gtk_list_store_set (impl->shortcuts_model, &iter,
1174 SHORTCUTS_COL_PIXBUF, pixbuf,
1178 g_object_unref (pixbuf);
1182 const GtkFilePath *path;
1183 struct ReloadIconsData *info;
1184 GtkTreePath *tree_path;
1185 GtkFileSystemHandle *handle;
1189 info = g_new0 (struct ReloadIconsData, 1);
1190 info->impl = g_object_ref (impl);
1191 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1192 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1193 gtk_tree_path_free (tree_path);
1195 handle = gtk_file_system_get_info (impl->file_system, path,
1197 shortcuts_reload_icons_get_info_cb,
1199 impl->reload_icon_handles = g_slist_append (impl->reload_icon_handles, handle);
1203 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1207 profile_end ("end", NULL);
1211 shortcuts_find_folder (GtkFileChooserDefault *impl,
1212 GtkFilePath *folder)
1214 GtkTreeSelection *selection;
1218 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1220 g_assert (folder != NULL);
1221 pos = shortcut_find_position (impl, folder);
1224 gtk_tree_selection_unselect_all (selection);
1228 path = gtk_tree_path_new_from_indices (pos, -1);
1229 gtk_tree_selection_select_path (selection, path);
1230 gtk_tree_path_free (path);
1233 /* If a shortcut corresponds to the current folder, selects it */
1235 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1237 shortcuts_find_folder (impl, impl->current_folder);
1240 /* Removes the specified number of rows from the shortcuts list */
1242 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1248 path = gtk_tree_path_new_from_indices (start_row, -1);
1250 for (; n_rows; n_rows--)
1254 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1255 g_assert_not_reached ();
1257 shortcuts_free_row_data (impl, &iter);
1258 gtk_list_store_remove (impl->shortcuts_model, &iter);
1261 gtk_tree_path_free (path);
1265 shortcuts_update_count (GtkFileChooserDefault *impl,
1266 ShortcutsIndex type,
1271 case SHORTCUTS_HOME:
1273 impl->has_home = FALSE;
1275 impl->has_home = TRUE;
1278 case SHORTCUTS_DESKTOP:
1280 impl->has_desktop = FALSE;
1282 impl->has_desktop = TRUE;
1285 case SHORTCUTS_VOLUMES:
1286 impl->num_volumes += value;
1289 case SHORTCUTS_SHORTCUTS:
1290 impl->num_shortcuts += value;
1293 case SHORTCUTS_BOOKMARKS:
1294 impl->num_bookmarks += value;
1297 case SHORTCUTS_CURRENT_FOLDER:
1299 impl->shortcuts_current_folder_active = FALSE;
1301 impl->shortcuts_current_folder_active = TRUE;
1310 struct ShortcutsInsertRequest
1312 GtkFileChooserDefault *impl;
1313 GtkFilePath *parent_path;
1317 GtkTreeRowReference *row_ref;
1318 ShortcutsIndex type;
1324 get_file_info_finished (GtkFileSystemHandle *handle,
1325 const GtkFileInfo *info,
1326 const GError *error,
1330 gboolean cancelled = handle->cancelled;
1331 gboolean is_volume = FALSE;
1335 GtkFileSystemHandle *model_handle;
1336 struct ShortcutsInsertRequest *request = data;
1338 path = gtk_tree_row_reference_get_path (request->row_ref);
1340 /* Handle doesn't exist anymore in the model */
1343 pos = gtk_tree_path_get_indices (path)[0];
1344 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1346 gtk_tree_path_free (path);
1348 /* validate handle, else goto out */
1349 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1350 SHORTCUTS_COL_HANDLE, &model_handle,
1352 if (handle != model_handle)
1355 /* set the handle to NULL in the model (we unref later on) */
1356 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1357 SHORTCUTS_COL_HANDLE, NULL,
1365 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1366 shortcuts_update_count (request->impl, request->type, -1);
1368 if (request->type == SHORTCUTS_HOME)
1370 const char *home = g_get_home_dir ();
1371 GtkFilePath *home_path;
1373 home_path = gtk_file_system_filename_to_path (request->impl->file_system, home);
1374 error_getting_info_dialog (request->impl, home_path, g_error_copy (error));
1375 gtk_file_path_free (home_path);
1377 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1379 /* Remove the current folder separator */
1380 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1381 shortcuts_remove_rows (request->impl, separator_pos, 1);
1387 if (!request->label_copy)
1388 request->label_copy = g_strdup (gtk_file_info_get_display_name (info));
1389 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1390 request->impl->icon_size, NULL);
1392 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1393 SHORTCUTS_COL_PIXBUF, pixbuf,
1394 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1395 SHORTCUTS_COL_NAME, request->label_copy,
1396 SHORTCUTS_COL_IS_VOLUME, is_volume,
1397 SHORTCUTS_COL_REMOVABLE, request->removable,
1400 if (request->impl->shortcuts_filter_model)
1401 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_filter_model));
1403 if (request->type == SHORTCUTS_CURRENT_FOLDER
1404 && request->impl->save_folder_combo != NULL)
1406 /* The current folder is updated via _activate_iter(), don't
1407 * have save_folder_combo_changed_cb() call _activate_iter()
1410 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1411 G_CALLBACK (save_folder_combo_changed_cb),
1413 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1414 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1415 G_CALLBACK (save_folder_combo_changed_cb),
1420 g_object_unref (pixbuf);
1423 g_object_unref (request->impl);
1424 gtk_file_path_free (request->parent_path);
1425 gtk_file_path_free (request->path);
1426 gtk_tree_row_reference_free (request->row_ref);
1427 g_free (request->label_copy);
1430 g_object_unref (handle);
1433 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1434 * inserts a volume. A position of -1 indicates the end of the tree.
1437 shortcuts_insert_path (GtkFileChooserDefault *impl,
1440 GtkFileSystemVolume *volume,
1441 const GtkFilePath *path,
1444 ShortcutsIndex type)
1447 GdkPixbuf *pixbuf = NULL;
1448 gpointer data = NULL;
1451 profile_start ("start", is_volume ? "volume" : (char *) path);
1456 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1457 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1458 impl->icon_size, NULL);
1462 struct ShortcutsInsertRequest *request;
1463 GtkFileSystemHandle *handle;
1466 request = g_new0 (struct ShortcutsInsertRequest, 1);
1467 request->impl = g_object_ref (impl);
1468 request->path = gtk_file_path_copy (path);
1469 request->name_only = TRUE;
1470 request->removable = removable;
1472 request->type = type;
1474 request->label_copy = g_strdup (label);
1477 gtk_list_store_append (impl->shortcuts_model, &iter);
1479 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1481 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1482 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1483 gtk_tree_path_free (p);
1485 handle = gtk_file_system_get_info (request->impl->file_system, request->path,
1486 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN | GTK_FILE_INFO_ICON,
1487 get_file_info_finished, request);
1489 gtk_list_store_set (impl->shortcuts_model, &iter,
1490 SHORTCUTS_COL_DATA, gtk_file_path_copy (path),
1491 SHORTCUTS_COL_IS_VOLUME, is_volume,
1492 SHORTCUTS_COL_HANDLE, handle,
1495 shortcuts_update_count (impl, type, 1);
1501 data = gtk_file_path_copy (path);
1504 gtk_list_store_append (impl->shortcuts_model, &iter);
1506 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1508 shortcuts_update_count (impl, type, 1);
1510 gtk_list_store_set (impl->shortcuts_model, &iter,
1511 SHORTCUTS_COL_PIXBUF, pixbuf,
1512 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1513 SHORTCUTS_COL_NAME, label_copy,
1514 SHORTCUTS_COL_DATA, data,
1515 SHORTCUTS_COL_IS_VOLUME, is_volume,
1516 SHORTCUTS_COL_REMOVABLE, removable,
1517 SHORTCUTS_COL_HANDLE, NULL,
1520 if (impl->shortcuts_filter_model)
1521 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1523 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1525 /* The current folder is updated via _activate_iter(), don't
1526 * have save_folder_combo_changed_cb() call _activate_iter()
1529 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1530 g_signal_handlers_block_by_func (impl->save_folder_combo,
1531 G_CALLBACK (save_folder_combo_changed_cb),
1533 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1534 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1535 G_CALLBACK (save_folder_combo_changed_cb),
1539 g_free (label_copy);
1542 g_object_unref (pixbuf);
1544 profile_end ("end", NULL);
1547 /* Appends an item for the user's home directory to the shortcuts model */
1549 shortcuts_append_home (GtkFileChooserDefault *impl)
1552 GtkFilePath *home_path;
1554 profile_start ("start", NULL);
1556 home = g_get_home_dir ();
1559 profile_end ("end - no home directory!?", NULL);
1563 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1565 shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, NULL, FALSE, SHORTCUTS_HOME);
1567 gtk_file_path_free (home_path);
1569 profile_end ("end", NULL);
1572 /* Appends the ~/Desktop directory to the shortcuts model */
1574 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1580 profile_start ("start", NULL);
1583 name = _gtk_file_system_win32_get_desktop ();
1585 home = g_get_home_dir ();
1588 profile_end ("end - no home directory!?", NULL);
1592 name = g_build_filename (home, "Desktop", NULL);
1595 path = gtk_file_system_filename_to_path (impl->file_system, name);
1598 shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1599 /* We do not actually pop up an error dialog if there is no desktop directory
1600 * because some people may really not want to have one.
1603 gtk_file_path_free (path);
1605 profile_end ("end", NULL);
1608 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1610 shortcuts_append_paths (GtkFileChooserDefault *impl,
1617 profile_start ("start", NULL);
1619 /* As there is no separator now, we want to start there.
1621 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1624 for (; paths; paths = paths->next)
1630 if (impl->local_only &&
1631 !gtk_file_system_path_is_local (impl->file_system, path))
1634 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
1636 /* NULL GError, but we don't really want to show error boxes here */
1637 shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, label, TRUE, SHORTCUTS_BOOKMARKS);
1643 profile_end ("end", NULL);
1645 return num_inserted;
1648 /* Returns the index for the corresponding item in the shortcuts bar */
1650 shortcuts_get_index (GtkFileChooserDefault *impl,
1651 ShortcutsIndex where)
1657 if (where == SHORTCUTS_HOME)
1660 n += impl->has_home ? 1 : 0;
1662 if (where == SHORTCUTS_DESKTOP)
1665 n += impl->has_desktop ? 1 : 0;
1667 if (where == SHORTCUTS_VOLUMES)
1670 n += impl->num_volumes;
1672 if (where == SHORTCUTS_SHORTCUTS)
1675 n += impl->num_shortcuts;
1677 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1680 /* If there are no bookmarks there won't be a separator */
1681 n += (impl->num_bookmarks > 0) ? 1 : 0;
1683 if (where == SHORTCUTS_BOOKMARKS)
1686 n += impl->num_bookmarks;
1688 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1693 if (where == SHORTCUTS_CURRENT_FOLDER)
1696 g_assert_not_reached ();
1703 /* Adds all the file system volumes to the shortcuts model */
1705 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1710 gboolean old_changing_folders;
1712 profile_start ("start", NULL);
1714 old_changing_folders = impl->changing_folder;
1715 impl->changing_folder = TRUE;
1717 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1718 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1719 impl->num_volumes = 0;
1721 list = gtk_file_system_list_volumes (impl->file_system);
1725 for (l = list; l; l = l->next)
1727 GtkFileSystemVolume *volume;
1731 if (impl->local_only)
1733 if (gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
1735 GtkFilePath *base_path;
1737 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1738 if (base_path != NULL)
1740 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1741 gtk_file_path_free (base_path);
1745 gtk_file_system_volume_free (impl->file_system, volume);
1752 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
1756 impl->num_volumes = n;
1757 g_slist_free (list);
1759 if (impl->shortcuts_filter_model)
1760 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1762 impl->changing_folder = old_changing_folders;
1764 profile_end ("end", NULL);
1767 /* Inserts a separator node in the shortcuts list */
1769 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1770 ShortcutsIndex where)
1774 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1776 gtk_list_store_insert (impl->shortcuts_model, &iter,
1777 shortcuts_get_index (impl, where));
1778 gtk_list_store_set (impl->shortcuts_model, &iter,
1779 SHORTCUTS_COL_PIXBUF, NULL,
1780 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1781 SHORTCUTS_COL_NAME, NULL,
1782 SHORTCUTS_COL_DATA, NULL,
1786 /* Updates the list of bookmarks */
1788 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1791 gboolean old_changing_folders;
1793 GtkFilePath *list_selected = NULL;
1794 GtkFilePath *combo_selected = NULL;
1798 profile_start ("start", NULL);
1801 old_changing_folders = impl->changing_folder;
1802 impl->changing_folder = TRUE;
1804 if (shortcuts_get_selected (impl, &iter))
1806 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1808 SHORTCUTS_COL_DATA, &col_data,
1809 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1812 if (col_data && !is_volume)
1813 list_selected = gtk_file_path_copy (col_data);
1816 if (impl->save_folder_combo &&
1817 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
1820 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1822 SHORTCUTS_COL_DATA, &col_data,
1823 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1826 if (col_data && !is_volume)
1827 combo_selected = gtk_file_path_copy (col_data);
1830 if (impl->num_bookmarks > 0)
1831 shortcuts_remove_rows (impl,
1832 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1833 impl->num_bookmarks + 1);
1835 impl->num_bookmarks = 0;
1837 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1838 shortcuts_append_paths (impl, bookmarks);
1839 gtk_file_paths_free (bookmarks);
1841 if (impl->num_bookmarks > 0)
1842 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1844 if (impl->shortcuts_filter_model)
1845 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1849 shortcuts_find_folder (impl, list_selected);
1850 gtk_file_path_free (list_selected);
1857 pos = shortcut_find_position (impl, combo_selected);
1859 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
1861 gtk_file_path_free (combo_selected);
1864 impl->changing_folder = old_changing_folders;
1866 profile_end ("end", NULL);
1869 /* Appends a separator and a row to the shortcuts list for the current folder */
1871 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1876 g_assert (!impl->shortcuts_current_folder_active);
1880 g_assert (impl->current_folder != NULL);
1882 pos = shortcut_find_position (impl, impl->current_folder);
1885 GtkFileSystemVolume *volume;
1886 GtkFilePath *base_path;
1890 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1894 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1896 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1898 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1903 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1905 shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
1908 shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
1911 gtk_file_path_free (base_path);
1913 else if (impl->save_folder_combo != NULL)
1914 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1917 /* Updates the current folder row in the shortcuts model */
1919 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1923 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1925 if (impl->shortcuts_current_folder_active)
1927 shortcuts_remove_rows (impl, pos, 2);
1928 impl->shortcuts_current_folder_active = FALSE;
1931 shortcuts_add_current_folder (impl);
1934 /* Filter function used for the shortcuts filter model */
1936 shortcuts_filter_cb (GtkTreeModel *model,
1940 GtkFileChooserDefault *impl;
1944 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1946 path = gtk_tree_model_get_path (model, iter);
1950 pos = *gtk_tree_path_get_indices (path);
1951 gtk_tree_path_free (path);
1953 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1956 /* Creates the list model for shortcuts */
1958 shortcuts_model_create (GtkFileChooserDefault *impl)
1960 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1961 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1962 GDK_TYPE_PIXBUF, /* pixbuf */
1963 G_TYPE_STRING, /* name */
1964 G_TYPE_POINTER, /* path or volume */
1965 G_TYPE_BOOLEAN, /* is the previous column a volume? */
1966 G_TYPE_BOOLEAN, /* removable */
1967 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
1968 G_TYPE_OBJECT); /* GtkFileSystemHandle */
1970 if (impl->file_system)
1972 shortcuts_append_home (impl);
1973 shortcuts_append_desktop (impl);
1974 shortcuts_add_volumes (impl);
1977 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1978 GTK_TREE_MODEL (impl->shortcuts_model),
1981 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1982 shortcuts_filter_cb,
1987 /* Callback used when the "New Folder" button is clicked */
1989 new_folder_button_clicked (GtkButton *button,
1990 GtkFileChooserDefault *impl)
1995 if (!impl->browse_files_model)
1996 return; /* FIXME: this sucks. Disable the New Folder button or something. */
1998 /* Prevent button from being clicked twice */
1999 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2001 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2003 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2004 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2005 path, impl->list_name_column,
2008 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2009 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2011 impl->list_name_column,
2014 gtk_tree_path_free (path);
2018 edited_idle_create_folder_cb (GtkFileSystemHandle *handle,
2019 const GtkFilePath *path,
2020 const GError *error,
2023 gboolean cancelled = handle->cancelled;
2024 GtkFileChooserDefault *impl = data;
2026 if (!g_slist_find (impl->pending_handles, handle))
2029 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
2035 change_folder_and_display_error (impl, path);
2037 error_creating_folder_dialog (impl, path, g_error_copy (error));
2040 g_object_unref (impl);
2041 g_object_unref (handle);
2044 /* Idle handler for creating a new folder after editing its name cell, or for
2045 * canceling the editing.
2048 edited_idle_cb (GtkFileChooserDefault *impl)
2050 GDK_THREADS_ENTER ();
2052 g_source_destroy (impl->edited_idle);
2053 impl->edited_idle = NULL;
2055 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2056 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2058 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2060 if (impl->edited_new_text) /* not cancelled? */
2063 GtkFilePath *file_path;
2066 file_path = gtk_file_system_make_path (impl->file_system,
2067 impl->current_folder,
2068 impl->edited_new_text,
2072 GtkFileSystemHandle *handle;
2074 handle = gtk_file_system_create_folder (impl->file_system, file_path,
2075 edited_idle_create_folder_cb,
2076 g_object_ref (impl));
2077 impl->pending_handles = g_slist_append (impl->pending_handles, handle);
2079 gtk_file_path_free (file_path);
2082 error_creating_folder_dialog (impl, file_path, error);
2084 g_free (impl->edited_new_text);
2085 impl->edited_new_text = NULL;
2088 GDK_THREADS_LEAVE ();
2094 queue_edited_idle (GtkFileChooserDefault *impl,
2095 const gchar *new_text)
2097 /* We create the folder in an idle handler so that we don't modify the tree
2101 if (!impl->edited_idle)
2103 impl->edited_idle = g_idle_source_new ();
2104 g_source_set_closure (impl->edited_idle,
2105 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2107 g_source_attach (impl->edited_idle, NULL);
2110 g_free (impl->edited_new_text);
2111 impl->edited_new_text = g_strdup (new_text);
2114 /* Callback used from the text cell renderer when the new folder is named */
2116 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2118 const gchar *new_text,
2119 GtkFileChooserDefault *impl)
2121 /* work around bug #154921 */
2122 g_object_set (cell_renderer_text,
2123 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2124 queue_edited_idle (impl, new_text);
2127 /* Callback used from the text cell renderer when the new folder edition gets
2131 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2132 GtkFileChooserDefault *impl)
2134 /* work around bug #154921 */
2135 g_object_set (cell_renderer_text,
2136 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2137 queue_edited_idle (impl, NULL);
2140 /* Creates the widgets for the filter combo box */
2142 filter_create (GtkFileChooserDefault *impl)
2144 impl->filter_combo = gtk_combo_box_new_text ();
2145 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2147 g_signal_connect (impl->filter_combo, "changed",
2148 G_CALLBACK (filter_combo_changed), impl);
2150 return impl->filter_combo;
2154 button_new (GtkFileChooserDefault *impl,
2156 const char *stock_id,
2164 button = gtk_button_new_with_mnemonic (text);
2165 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2166 gtk_button_set_image (GTK_BUTTON (button), image);
2168 gtk_widget_set_sensitive (button, sensitive);
2169 g_signal_connect (button, "clicked", callback, impl);
2172 gtk_widget_show (button);
2177 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2179 shortcut_find_position (GtkFileChooserDefault *impl,
2180 const GtkFilePath *path)
2184 int current_folder_separator_idx;
2186 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2189 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2192 /* FIXME: is this still needed? */
2193 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2197 for (i = 0; i < current_folder_separator_idx; i++)
2202 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2203 SHORTCUTS_COL_DATA, &col_data,
2204 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2211 GtkFileSystemVolume *volume;
2212 GtkFilePath *base_path;
2216 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2218 exists = base_path && strcmp (gtk_file_path_get_string (path),
2219 gtk_file_path_get_string (base_path)) == 0;
2227 GtkFilePath *model_path;
2229 model_path = col_data;
2231 if (model_path && gtk_file_path_compare (model_path, path) == 0)
2236 if (i < current_folder_separator_idx - 1)
2238 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2239 g_assert_not_reached ();
2246 /* Tries to add a bookmark from a path name */
2248 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2249 const GtkFilePath *path,
2254 g_return_val_if_fail (path != NULL, FALSE);
2256 if (shortcut_find_position (impl, path) != -1)
2260 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2262 error_adding_bookmark_dialog (impl, path, error);
2270 add_bookmark_foreach_cb (GtkTreeModel *model,
2275 GtkFileChooserDefault *impl;
2276 GtkFileSystemModel *fs_model;
2277 GtkTreeIter child_iter;
2278 const GtkFilePath *file_path;
2280 impl = (GtkFileChooserDefault *) data;
2282 fs_model = impl->browse_files_model;
2283 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2285 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2286 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2289 /* Adds a bookmark from the currently selected item in the file list */
2291 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2293 GtkTreeSelection *selection;
2295 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2297 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2298 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2300 gtk_tree_selection_selected_foreach (selection,
2301 add_bookmark_foreach_cb,
2305 /* Callback used when the "Add bookmark" button is clicked */
2307 add_bookmark_button_clicked_cb (GtkButton *button,
2308 GtkFileChooserDefault *impl)
2310 bookmarks_add_selected_folder (impl);
2313 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2314 * returns FALSE if no shortcut is selected.
2317 shortcuts_get_selected (GtkFileChooserDefault *impl,
2320 GtkTreeSelection *selection;
2321 GtkTreeIter parent_iter;
2323 if (!impl->browse_shortcuts_tree_view)
2326 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2328 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2331 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2337 /* Removes the selected bookmarks */
2339 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2347 if (!shortcuts_get_selected (impl, &iter))
2350 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2351 SHORTCUTS_COL_DATA, &col_data,
2352 SHORTCUTS_COL_REMOVABLE, &removable,
2354 g_assert (col_data != NULL);
2362 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2363 error_removing_bookmark_dialog (impl, path, error);
2366 /* Callback used when the "Remove bookmark" button is clicked */
2368 remove_bookmark_button_clicked_cb (GtkButton *button,
2369 GtkFileChooserDefault *impl)
2371 remove_selected_bookmarks (impl);
2374 struct selection_check_closure {
2375 GtkFileChooserDefault *impl;
2378 gboolean all_folders;
2381 /* Used from gtk_tree_selection_selected_foreach() */
2383 selection_check_foreach_cb (GtkTreeModel *model,
2388 struct selection_check_closure *closure;
2389 GtkTreeIter child_iter;
2390 const GtkFileInfo *info;
2394 closure->num_selected++;
2396 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2398 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2399 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2401 closure->all_folders = closure->all_folders && is_folder;
2402 closure->all_files = closure->all_files && !is_folder;
2405 /* Checks whether the selected items in the file list are all files or all folders */
2407 selection_check (GtkFileChooserDefault *impl,
2409 gboolean *all_files,
2410 gboolean *all_folders)
2412 struct selection_check_closure closure;
2413 GtkTreeSelection *selection;
2415 closure.impl = impl;
2416 closure.num_selected = 0;
2417 closure.all_files = TRUE;
2418 closure.all_folders = TRUE;
2420 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2421 gtk_tree_selection_selected_foreach (selection,
2422 selection_check_foreach_cb,
2425 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2428 *num_selected = closure.num_selected;
2431 *all_files = closure.all_files;
2434 *all_folders = closure.all_folders;
2437 struct get_selected_path_closure {
2438 GtkFileChooserDefault *impl;
2439 const GtkFilePath *path;
2443 get_selected_path_foreach_cb (GtkTreeModel *model,
2448 struct get_selected_path_closure *closure;
2449 GtkTreeIter child_iter;
2453 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2454 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2457 /* Returns a selected path from the file list */
2458 static const GtkFilePath *
2459 get_selected_path (GtkFileChooserDefault *impl)
2461 struct get_selected_path_closure closure;
2462 GtkTreeSelection *selection;
2464 closure.impl = impl;
2465 closure.path = NULL;
2467 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2468 gtk_tree_selection_selected_foreach (selection,
2469 get_selected_path_foreach_cb,
2472 return closure.path;
2476 GtkFileChooserDefault *impl;
2478 } UpdateTooltipData;
2481 update_tooltip (GtkTreeModel *model,
2486 UpdateTooltipData *udata = data;
2487 GtkTreeIter child_iter;
2488 const GtkFileInfo *info;
2490 if (udata->tip == NULL)
2492 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2496 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2497 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2498 gtk_file_info_get_display_name (info));
2503 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2504 * if there are no selected items *and* the current folder is not in the
2505 * bookmarks list. De-sensitize the button otherwise.
2508 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2511 gboolean all_folders;
2515 selection_check (impl, &num_selected, NULL, &all_folders);
2517 if (num_selected == 0)
2518 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2519 else if (num_selected == 1)
2521 const GtkFilePath *path;
2523 path = get_selected_path (impl);
2524 active = all_folders && (shortcut_find_position (impl, path) == -1);
2527 active = all_folders;
2529 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2531 if (impl->browse_files_popup_menu_add_shortcut_item)
2532 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2533 (num_selected == 0) ? FALSE : active);
2537 if (num_selected == 0)
2538 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2539 else if (num_selected > 1)
2540 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2543 GtkTreeSelection *selection;
2544 UpdateTooltipData data;
2546 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2549 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2553 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2558 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2559 * bookmark row is selected in the shortcuts tree.
2562 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2565 gboolean removable = FALSE;
2568 if (shortcuts_get_selected (impl, &iter))
2569 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2570 SHORTCUTS_COL_REMOVABLE, &removable,
2571 SHORTCUTS_COL_NAME, &name,
2574 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2580 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2581 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2590 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2593 gboolean removable = FALSE;
2595 if (impl->browse_shortcuts_popup_menu == NULL)
2598 if (shortcuts_get_selected (impl, &iter))
2599 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2600 SHORTCUTS_COL_REMOVABLE, &removable,
2603 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2604 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2607 /* GtkWidget::drag-begin handler for the shortcuts list. */
2609 shortcuts_drag_begin_cb (GtkWidget *widget,
2610 GdkDragContext *context,
2611 GtkFileChooserDefault *impl)
2614 impl->shortcuts_drag_context = g_object_ref (context);
2619 /* Removes the idle handler for outside drags */
2621 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2623 if (!impl->shortcuts_drag_outside_idle)
2626 g_source_destroy (impl->shortcuts_drag_outside_idle);
2627 impl->shortcuts_drag_outside_idle = NULL;
2631 /* GtkWidget::drag-end handler for the shortcuts list. */
2633 shortcuts_drag_end_cb (GtkWidget *widget,
2634 GdkDragContext *context,
2635 GtkFileChooserDefault *impl)
2638 g_object_unref (impl->shortcuts_drag_context);
2640 shortcuts_cancel_drag_outside_idle (impl);
2642 if (!impl->shortcuts_drag_outside)
2645 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2647 impl->shortcuts_drag_outside = FALSE;
2651 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2653 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2654 GdkDragContext *context,
2655 GtkFileChooserDefault *impl)
2657 g_signal_stop_emission_by_name (widget, "drag_data_delete");
2661 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2665 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2668 GtkTreeView *tree_view;
2671 GdkPixmap *row_pixmap;
2676 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2678 /* Find the selected path and get its drag pixmap */
2680 if (!shortcuts_get_selected (impl, &iter))
2681 g_assert_not_reached ();
2683 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2685 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2686 gtk_tree_path_free (path);
2695 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2701 GdkPixmap *composite;
2702 int row_pixmap_width, row_pixmap_height;
2703 int pixbuf_width, pixbuf_height;
2704 int composite_width, composite_height;
2705 int pixbuf_x, pixbuf_y;
2706 GdkGC *gc, *mask_gc;
2708 GdkBitmap *pixbuf_mask;
2710 /* Create pixmap and mask for composite image */
2712 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2713 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2714 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2716 composite_width = MAX (row_pixmap_width, pixbuf_width);
2717 composite_height = MAX (row_pixmap_height, pixbuf_height);
2719 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2721 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2724 pixbuf_x = composite_width - pixbuf_width;
2726 pixbuf_y = (composite_height - pixbuf_height) / 2;
2728 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2729 gc = gdk_gc_new (composite);
2731 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2732 mask_gc = gdk_gc_new (mask);
2734 gdk_gc_set_foreground (mask_gc, &color);
2735 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2738 color.green = 0xffff;
2739 color.blue = 0xffff;
2740 gdk_gc_set_rgb_fg_color (gc, &color);
2741 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2743 /* Composite the row pixmap and the pixbuf */
2745 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2747 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2748 NULL, &pixbuf_mask, 128);
2749 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2752 pixbuf_width, pixbuf_height);
2753 g_object_unref (pixbuf_mask);
2755 gdk_draw_drawable (composite, gc, row_pixmap,
2758 row_pixmap_width, row_pixmap_height);
2760 gdk_gc_set_foreground (mask_gc, &color);
2761 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2763 gdk_draw_pixbuf (composite, gc, pixbuf,
2766 pixbuf_width, pixbuf_height,
2770 g_object_unref (pixbuf);
2771 g_object_unref (row_pixmap);
2773 row_pixmap = composite;
2777 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2779 gtk_tree_view_get_path_at_pos (tree_view,
2780 tree_view->priv->press_start_x,
2781 tree_view->priv->press_start_y,
2787 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2788 gdk_drawable_get_colormap (row_pixmap),
2791 tree_view->priv->press_start_x + 1,
2792 row_pixmap_y + cell_y + 1);
2794 g_object_unref (row_pixmap);
2796 g_object_unref (mask);
2799 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2800 * handler so that we can tell apart the drag_leave event that comes right
2801 * before a drag_drop, from a normal drag_leave. We don't want to set the
2802 * cursor nor the flag in the latter case.
2805 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2807 GDK_THREADS_ENTER ();
2809 shortcuts_drag_set_delete_cursor (impl, TRUE);
2810 impl->shortcuts_drag_outside = TRUE;
2812 shortcuts_cancel_drag_outside_idle (impl);
2814 GDK_THREADS_LEAVE ();
2820 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2824 shortcuts_drag_leave_cb (GtkWidget *widget,
2825 GdkDragContext *context,
2827 GtkFileChooserDefault *impl)
2830 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2832 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2833 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2834 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2836 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2840 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2842 GTK_TREE_VIEW_DROP_BEFORE);
2844 g_signal_stop_emission_by_name (widget, "drag_leave");
2847 /* Computes the appropriate row and position for dropping */
2849 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2853 GtkTreeViewDropPosition *pos)
2855 GtkTreeView *tree_view;
2856 GtkTreeViewColumn *column;
2860 int bookmarks_index;
2862 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2864 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2866 if (!gtk_tree_view_get_path_at_pos (tree_view,
2868 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2874 row = bookmarks_index + impl->num_bookmarks - 1;
2875 *path = gtk_tree_path_new_from_indices (row, -1);
2876 *pos = GTK_TREE_VIEW_DROP_AFTER;
2880 row = *gtk_tree_path_get_indices (*path);
2881 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2882 gtk_tree_path_free (*path);
2884 if (row < bookmarks_index)
2886 row = bookmarks_index;
2887 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2889 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2891 row = bookmarks_index + impl->num_bookmarks - 1;
2892 *pos = GTK_TREE_VIEW_DROP_AFTER;
2896 if (cell_y < cell.height / 2)
2897 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2899 *pos = GTK_TREE_VIEW_DROP_AFTER;
2902 *path = gtk_tree_path_new_from_indices (row, -1);
2905 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2906 * implement the destination side of DnD by hand, due to limitations in
2907 * GtkTreeView's DnD API.
2910 shortcuts_drag_motion_cb (GtkWidget *widget,
2911 GdkDragContext *context,
2915 GtkFileChooserDefault *impl)
2918 GtkTreeViewDropPosition pos;
2919 GdkDragAction action;
2922 if (gtk_drag_get_source_widget (context) == widget)
2924 shortcuts_cancel_drag_outside_idle (impl);
2926 if (impl->shortcuts_drag_outside)
2928 shortcuts_drag_set_delete_cursor (impl, FALSE);
2929 impl->shortcuts_drag_outside = FALSE;
2934 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2935 action = GDK_ACTION_COPY;
2936 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2937 action = GDK_ACTION_MOVE;
2944 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2945 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2946 gtk_tree_path_free (path);
2950 g_signal_stop_emission_by_name (widget, "drag_motion");
2954 gdk_drag_status (context, action, time_);
2961 /* GtkWidget::drag-drop handler for the shortcuts list. */
2963 shortcuts_drag_drop_cb (GtkWidget *widget,
2964 GdkDragContext *context,
2968 GtkFileChooserDefault *impl)
2971 shortcuts_cancel_drag_outside_idle (impl);
2974 g_signal_stop_emission_by_name (widget, "drag_drop");
2978 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2980 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2987 uris = g_uri_list_extract_uris (data);
2989 for (i = 0; uris[i]; i++)
2995 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2999 if (shortcuts_add_bookmark_from_path (impl, path, position))
3002 gtk_file_path_free (path);
3008 g_set_error (&error,
3009 GTK_FILE_CHOOSER_ERROR,
3010 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3011 _("Could not add a bookmark for '%s' "
3012 "because it is an invalid path name."),
3014 error_adding_bookmark_dialog (impl, path, error);
3021 /* Reorders the selected bookmark to the specified position */
3023 shortcuts_reorder (GtkFileChooserDefault *impl,
3031 int bookmarks_index;
3032 const GtkFilePath *file_path;
3033 GtkFilePath *file_path_copy;
3037 /* Get the selected path */
3039 if (!shortcuts_get_selected (impl, &iter))
3040 g_assert_not_reached ();
3042 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3043 old_position = *gtk_tree_path_get_indices (path);
3044 gtk_tree_path_free (path);
3046 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3047 old_position -= bookmarks_index;
3048 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3050 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3051 SHORTCUTS_COL_NAME, &name,
3052 SHORTCUTS_COL_DATA, &col_data,
3053 SHORTCUTS_COL_IS_VOLUME, &is_volume,
3055 g_assert (col_data != NULL);
3056 g_assert (!is_volume);
3058 file_path = col_data;
3059 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
3061 /* Remove the path from the old position and insert it in the new one */
3063 if (new_position > old_position)
3066 if (old_position == new_position)
3070 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
3072 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
3073 gtk_file_system_set_bookmark_label (impl->file_system, file_path_copy, name);
3076 error_adding_bookmark_dialog (impl, file_path_copy, error);
3080 gtk_file_path_free (file_path_copy);
3083 /* Callback used when we get the drag data for the bookmarks list. We add the
3084 * received URIs as bookmarks if they are folders.
3087 shortcuts_drag_data_received_cb (GtkWidget *widget,
3088 GdkDragContext *context,
3091 GtkSelectionData *selection_data,
3096 GtkFileChooserDefault *impl;
3097 GtkTreePath *tree_path;
3098 GtkTreeViewDropPosition tree_pos;
3100 int bookmarks_index;
3102 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3104 /* Compute position */
3106 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3108 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3109 position = *gtk_tree_path_get_indices (tree_path);
3110 gtk_tree_path_free (tree_path);
3112 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3115 g_assert (position >= bookmarks_index);
3116 position -= bookmarks_index;
3118 if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3119 shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3120 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3121 shortcuts_reorder (impl, position);
3123 g_signal_stop_emission_by_name (widget, "drag_data_received");
3126 /* Callback used when the selection in the shortcuts tree changes */
3128 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3129 GtkFileChooserDefault *impl)
3131 bookmarks_check_remove_sensitivity (impl);
3132 shortcuts_check_popup_sensitivity (impl);
3136 shortcuts_row_separator_func (GtkTreeModel *model,
3140 gint column = GPOINTER_TO_INT (data);
3143 gtk_tree_model_get (model, iter, column, &text, -1);
3153 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3154 * keypresses before the TreeView gets them.
3157 tree_view_keybinding_cb (GtkWidget *tree_view,
3159 GtkFileChooserDefault *impl)
3161 /* FMQ: remove this? */
3163 if ((event->keyval == GDK_slash
3164 || event->keyval == GDK_KP_Divide
3166 || event->keyval == GDK_asciitilde
3168 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3170 location_popup_handler (impl, event->string);
3177 /* Callback used when the file list's popup menu is detached */
3179 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3182 GtkFileChooserDefault *impl;
3184 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3185 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3187 impl->browse_shortcuts_popup_menu = NULL;
3188 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3189 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3193 remove_shortcut_cb (GtkMenuItem *item,
3194 GtkFileChooserDefault *impl)
3196 remove_selected_bookmarks (impl);
3199 /* Rename the selected bookmark */
3201 rename_selected_bookmark (GtkFileChooserDefault *impl)
3205 GtkTreeViewColumn *column;
3206 GtkCellRenderer *cell;
3209 if (shortcuts_get_selected (impl, &iter))
3211 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3212 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3213 renderers = gtk_tree_view_column_get_cell_renderers (column);
3214 cell = g_list_nth_data (renderers, 1);
3215 g_list_free (renderers);
3216 g_object_set (cell, "editable", TRUE, NULL);
3217 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3218 path, column, cell, TRUE);
3219 gtk_tree_path_free (path);
3224 rename_shortcut_cb (GtkMenuItem *item,
3225 GtkFileChooserDefault *impl)
3227 rename_selected_bookmark (impl);
3230 /* Constructs the popup menu for the file list if needed */
3232 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3236 if (impl->browse_shortcuts_popup_menu)
3239 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3240 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3241 impl->browse_shortcuts_tree_view,
3242 shortcuts_popup_menu_detach_cb);
3244 item = gtk_image_menu_item_new_with_label (_("Remove"));
3245 impl->browse_shortcuts_popup_menu_remove_item = item;
3246 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3247 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3248 g_signal_connect (item, "activate",
3249 G_CALLBACK (remove_shortcut_cb), impl);
3250 gtk_widget_show (item);
3251 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3253 item = gtk_menu_item_new_with_label (_("Rename..."));
3254 impl->browse_shortcuts_popup_menu_rename_item = item;
3255 g_signal_connect (item, "activate",
3256 G_CALLBACK (rename_shortcut_cb), impl);
3257 gtk_widget_show (item);
3258 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3260 shortcuts_check_popup_sensitivity (impl);
3264 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3266 shortcuts_build_popup_menu (impl);
3270 popup_position_func (GtkMenu *menu,
3274 gpointer user_data);
3277 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3278 GdkEventButton *event)
3280 shortcuts_update_popup_menu (impl);
3282 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3283 NULL, NULL, NULL, NULL,
3284 event->button, event->time);
3287 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3289 popup_position_func, impl->browse_shortcuts_tree_view,
3290 0, GDK_CURRENT_TIME);
3291 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3296 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3298 shortcuts_popup_menu_cb (GtkWidget *widget,
3299 GtkFileChooserDefault *impl)
3301 shortcuts_popup_menu (impl, NULL);
3305 /* Callback used when a button is pressed on the shortcuts list.
3306 * We trap button 3 to bring up a popup menu.
3309 shortcuts_button_press_event_cb (GtkWidget *widget,
3310 GdkEventButton *event,
3311 GtkFileChooserDefault *impl)
3313 static gboolean in_press = FALSE;
3319 if (event->button != 3)
3323 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3329 shortcuts_popup_menu (impl, event);
3334 shortcuts_edited (GtkCellRenderer *cell,
3337 GtkFileChooserDefault *impl)
3341 GtkFilePath *shortcut;
3343 g_object_set (cell, "editable", FALSE, NULL);
3345 path = gtk_tree_path_new_from_string (path_string);
3346 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3347 g_assert_not_reached ();
3349 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3350 SHORTCUTS_COL_DATA, &shortcut,
3352 gtk_tree_path_free (path);
3354 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3358 shortcuts_editing_canceled (GtkCellRenderer *cell,
3359 GtkFileChooserDefault *impl)
3361 g_object_set (cell, "editable", FALSE, NULL);
3364 /* Creates the widgets for the shortcuts and bookmarks tree */
3366 shortcuts_list_create (GtkFileChooserDefault *impl)
3369 GtkTreeSelection *selection;
3370 GtkTreeViewColumn *column;
3371 GtkCellRenderer *renderer;
3373 /* Scrolled window */
3375 swin = gtk_scrolled_window_new (NULL, NULL);
3376 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3377 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3378 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3380 gtk_widget_show (swin);
3384 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3385 #ifdef PROFILE_FILE_CHOOSER
3386 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3388 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3389 G_CALLBACK (tree_view_keybinding_cb), impl);
3390 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3391 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3392 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3393 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3394 /* Accessible object name for the file chooser's shortcuts pane */
3395 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3397 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
3399 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3401 shortcuts_source_targets,
3402 num_shortcuts_source_targets,
3405 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3406 GTK_DEST_DEFAULT_ALL,
3407 shortcuts_dest_targets,
3408 num_shortcuts_dest_targets,
3409 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3411 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3412 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
3413 gtk_tree_selection_set_select_function (selection,
3414 shortcuts_select_func,
3417 g_signal_connect (selection, "changed",
3418 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3420 g_signal_connect (impl->browse_shortcuts_tree_view, "row_activated",
3421 G_CALLBACK (shortcuts_row_activated_cb), impl);
3423 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3424 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3426 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3427 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3428 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3429 G_CALLBACK (shortcuts_drag_end_cb), impl);
3430 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3431 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3433 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3434 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3435 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3436 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3437 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3438 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3439 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3440 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3442 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3443 gtk_widget_show (impl->browse_shortcuts_tree_view);
3447 column = gtk_tree_view_column_new ();
3448 /* Column header for the file chooser's shortcuts pane */
3449 gtk_tree_view_column_set_title (column, _("_Places"));
3451 renderer = gtk_cell_renderer_pixbuf_new ();
3452 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3453 gtk_tree_view_column_set_attributes (column, renderer,
3454 "pixbuf", SHORTCUTS_COL_PIXBUF,
3455 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3458 renderer = gtk_cell_renderer_text_new ();
3459 g_signal_connect (renderer, "edited",
3460 G_CALLBACK (shortcuts_edited), impl);
3461 g_signal_connect (renderer, "editing-canceled",
3462 G_CALLBACK (shortcuts_editing_canceled), impl);
3463 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3464 gtk_tree_view_column_set_attributes (column, renderer,
3465 "text", SHORTCUTS_COL_NAME,
3468 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3469 shortcuts_row_separator_func,
3470 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3473 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3478 /* Creates the widgets for the shortcuts/bookmarks pane */
3480 shortcuts_pane_create (GtkFileChooserDefault *impl,
3481 GtkSizeGroup *size_group)
3487 vbox = gtk_vbox_new (FALSE, 6);
3488 gtk_widget_show (vbox);
3490 /* Shortcuts tree */
3492 widget = shortcuts_list_create (impl);
3493 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3495 /* Box for buttons */
3497 hbox = gtk_hbox_new (TRUE, 6);
3498 gtk_size_group_add_widget (size_group, hbox);
3499 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3500 gtk_widget_show (hbox);
3502 /* Add bookmark button */
3504 impl->browse_shortcuts_add_button = button_new (impl,
3509 G_CALLBACK (add_bookmark_button_clicked_cb));
3510 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3511 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
3512 _("Add the selected folder to the Bookmarks"), NULL);
3514 /* Remove bookmark button */
3516 impl->browse_shortcuts_remove_button = button_new (impl,
3521 G_CALLBACK (remove_bookmark_button_clicked_cb));
3522 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3523 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
3524 _("Remove the selected bookmark"), NULL);
3529 /* Handles key press events on the file list, so that we can trap Enter to
3530 * activate the default button on our own. Also, checks to see if '/' has been
3531 * pressed. See comment by tree_view_keybinding_cb() for more details.
3534 trap_activate_cb (GtkWidget *widget,
3538 GtkFileChooserDefault *impl;
3541 impl = (GtkFileChooserDefault *) data;
3543 modifiers = gtk_accelerator_get_default_mod_mask ();
3545 /* FMQ: remove this? */
3547 if ((event->keyval == GDK_slash
3548 || event->keyval == GDK_KP_Divide
3550 || event->keyval == GDK_asciitilde
3552 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3554 location_popup_handler (impl, event->string);
3559 if ((event->keyval == GDK_Return
3560 || event->keyval == GDK_ISO_Enter
3561 || event->keyval == GDK_KP_Enter
3562 || event->keyval == GDK_space)
3563 && ((event->state & modifiers) == 0)
3564 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3565 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3569 window = get_toplevel (widget);
3571 && widget != window->default_widget
3572 && !(widget == window->focus_widget &&
3573 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3575 gtk_window_activate_default (window);
3583 /* Callback used when the file list's popup menu is detached */
3585 popup_menu_detach_cb (GtkWidget *attach_widget,
3588 GtkFileChooserDefault *impl;
3590 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3591 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3593 impl->browse_files_popup_menu = NULL;
3594 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3595 impl->browse_files_popup_menu_hidden_files_item = NULL;
3598 /* Callback used when the "Add to Bookmarks" menu item is activated */
3600 add_to_shortcuts_cb (GtkMenuItem *item,
3601 GtkFileChooserDefault *impl)
3603 bookmarks_add_selected_folder (impl);
3606 /* Callback used when the "Show Hidden Files" menu item is toggled */
3608 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3609 GtkFileChooserDefault *impl)
3612 "show-hidden", gtk_check_menu_item_get_active (item),
3616 /* Shows an error dialog about not being able to select a dragged file */
3618 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3619 const GtkFilePath *path,
3623 _("Could not select file"),
3628 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3633 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3635 for (i = 1; uris[i]; i++)
3640 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3644 GError *error = NULL;
3646 gtk_file_chooser_default_select_path (chooser, path, &error);
3648 error_selecting_dragged_file_dialog (impl, path, error);
3650 gtk_file_path_free (path);
3655 struct FileListDragData
3657 GtkFileChooserDefault *impl;
3663 file_list_drag_data_received_get_info_cb (GtkFileSystemHandle *handle,
3664 const GtkFileInfo *info,
3665 const GError *error,
3668 gboolean cancelled = handle->cancelled;
3669 struct FileListDragData *data = user_data;
3670 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3672 if (handle != data->impl->file_list_drag_data_received_handle)
3675 data->impl->file_list_drag_data_received_handle = NULL;
3677 if (cancelled || error)
3680 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3681 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3682 data->uris[1] == 0 && !error &&
3683 gtk_file_info_get_is_folder (info))
3684 change_folder_and_display_error (data->impl, data->path);
3687 GError *error = NULL;
3689 gtk_file_chooser_default_unselect_all (chooser);
3690 gtk_file_chooser_default_select_path (chooser, data->path, &error);
3692 error_selecting_dragged_file_dialog (data->impl, data->path, error);
3694 browse_files_center_selected_row (data->impl);
3697 if (data->impl->select_multiple)
3698 file_list_drag_data_select_uris (data->impl, data->uris);
3701 g_object_unref (data->impl);
3702 g_strfreev (data->uris);
3703 gtk_file_path_free (data->path);
3706 g_object_unref (handle);
3710 file_list_drag_data_received_cb (GtkWidget *widget,
3711 GdkDragContext *context,
3714 GtkSelectionData *selection_data,
3719 GtkFileChooserDefault *impl;
3720 GtkFileChooser *chooser;
3724 GError *error = NULL;
3726 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3727 chooser = GTK_FILE_CHOOSER (data);
3729 /* Parse the text/uri-list string, navigate to the first one */
3730 uris = g_uri_list_extract_uris ((const char *) selection_data->data);
3734 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3738 struct FileListDragData *data;
3740 data = g_new0 (struct FileListDragData, 1);
3741 data->impl = g_object_ref (impl);
3745 if (impl->file_list_drag_data_received_handle)
3746 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
3748 impl->file_list_drag_data_received_handle =
3749 gtk_file_system_get_info (impl->file_system, path,
3750 GTK_FILE_INFO_IS_FOLDER,
3751 file_list_drag_data_received_get_info_cb,
3757 g_set_error (&error,
3758 GTK_FILE_CHOOSER_ERROR,
3759 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3760 _("Could not select file '%s' "
3761 "because it is an invalid path name."),
3763 error_selecting_dragged_file_dialog (impl, NULL, error);
3766 if (impl->select_multiple)
3767 file_list_drag_data_select_uris (impl, uris);
3773 g_signal_stop_emission_by_name (widget, "drag_data_received");
3776 /* Don't do anything with the drag_drop signal */
3778 file_list_drag_drop_cb (GtkWidget *widget,
3779 GdkDragContext *context,
3783 GtkFileChooserDefault *impl)
3785 g_signal_stop_emission_by_name (widget, "drag_drop");
3789 /* Disable the normal tree drag motion handler, it makes it look like you're
3790 dropping the dragged item onto a tree item */
3792 file_list_drag_motion_cb (GtkWidget *widget,
3793 GdkDragContext *context,
3797 GtkFileChooserDefault *impl)
3799 g_signal_stop_emission_by_name (widget, "drag_motion");
3803 /* Constructs the popup menu for the file list if needed */
3805 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3809 if (impl->browse_files_popup_menu)
3812 impl->browse_files_popup_menu = gtk_menu_new ();
3813 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3814 impl->browse_files_tree_view,
3815 popup_menu_detach_cb);
3817 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
3818 impl->browse_files_popup_menu_add_shortcut_item = item;
3819 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3820 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3821 gtk_widget_set_sensitive (item, FALSE);
3822 g_signal_connect (item, "activate",
3823 G_CALLBACK (add_to_shortcuts_cb), impl);
3824 gtk_widget_show (item);
3825 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3827 item = gtk_separator_menu_item_new ();
3828 gtk_widget_show (item);
3829 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3831 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3832 impl->browse_files_popup_menu_hidden_files_item = item;
3833 g_signal_connect (item, "toggled",
3834 G_CALLBACK (show_hidden_toggled_cb), impl);
3835 gtk_widget_show (item);
3836 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3839 /* Updates the popup menu for the file list, creating it if necessary */
3841 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3843 file_list_build_popup_menu (impl);
3845 /* The sensitivity of the Add to Bookmarks item is set in
3846 * bookmarks_check_add_sensitivity()
3849 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3850 G_CALLBACK (show_hidden_toggled_cb), impl);
3851 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3853 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3854 G_CALLBACK (show_hidden_toggled_cb), impl);
3858 popup_position_func (GtkMenu *menu,
3864 GtkWidget *widget = GTK_WIDGET (user_data);
3865 GdkScreen *screen = gtk_widget_get_screen (widget);
3868 GdkRectangle monitor;
3870 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3872 gdk_window_get_origin (widget->window, x, y);
3874 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3876 *x += (widget->allocation.width - req.width) / 2;
3877 *y += (widget->allocation.height - req.height) / 2;
3879 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3880 gtk_menu_set_monitor (menu, monitor_num);
3881 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3883 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3884 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3890 file_list_popup_menu (GtkFileChooserDefault *impl,
3891 GdkEventButton *event)
3893 file_list_update_popup_menu (impl);
3895 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3896 NULL, NULL, NULL, NULL,
3897 event->button, event->time);
3900 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3902 popup_position_func, impl->browse_files_tree_view,
3903 0, GDK_CURRENT_TIME);
3904 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3910 /* Callback used for the GtkWidget::popup-menu signal of the file list */
3912 list_popup_menu_cb (GtkWidget *widget,
3913 GtkFileChooserDefault *impl)
3915 file_list_popup_menu (impl, NULL);
3919 /* Callback used when a button is pressed on the file list. We trap button 3 to
3920 * bring up a popup menu.
3923 list_button_press_event_cb (GtkWidget *widget,
3924 GdkEventButton *event,
3925 GtkFileChooserDefault *impl)
3927 static gboolean in_press = FALSE;
3933 if (event->button != 3)
3937 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
3940 file_list_popup_menu (impl, event);
3944 /* Creates the widgets for the file list */
3946 create_file_list (GtkFileChooserDefault *impl)
3949 GtkTreeSelection *selection;
3950 GtkTreeViewColumn *column;
3951 GtkCellRenderer *renderer;
3953 /* Scrolled window */
3955 swin = gtk_scrolled_window_new (NULL, NULL);
3956 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3957 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3958 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3961 /* Tree/list view */
3963 impl->browse_files_tree_view = gtk_tree_view_new ();
3964 #ifdef PROFILE_FILE_CHOOSER
3965 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
3967 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
3968 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
3970 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
3971 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
3973 gtk_drag_dest_set (impl->browse_files_tree_view,
3974 GTK_DEST_DEFAULT_ALL,
3975 file_list_dest_targets,
3976 num_file_list_dest_targets,
3977 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3979 g_signal_connect (impl->browse_files_tree_view, "row_activated",
3980 G_CALLBACK (list_row_activated), impl);
3981 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
3982 G_CALLBACK (trap_activate_cb), impl);
3983 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
3984 G_CALLBACK (list_popup_menu_cb), impl);
3985 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
3986 G_CALLBACK (list_button_press_event_cb), impl);
3988 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
3989 G_CALLBACK (file_list_drag_data_received_cb), impl);
3990 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
3991 G_CALLBACK (file_list_drag_drop_cb), impl);
3992 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
3993 G_CALLBACK (file_list_drag_motion_cb), impl);
3995 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3996 gtk_tree_selection_set_select_function (selection,
3999 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4001 file_list_source_targets,
4002 num_file_list_source_targets,
4005 g_signal_connect (selection, "changed",
4006 G_CALLBACK (list_selection_changed), impl);
4008 /* Filename column */
4010 impl->list_name_column = gtk_tree_view_column_new ();
4011 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4012 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4013 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4014 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4016 renderer = gtk_cell_renderer_pixbuf_new ();
4017 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4018 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4019 list_icon_data_func, impl, NULL);
4021 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4022 g_object_set (impl->list_name_renderer,
4023 "ellipsize", PANGO_ELLIPSIZE_END,
4025 g_signal_connect (impl->list_name_renderer, "edited",
4026 G_CALLBACK (renderer_edited_cb), impl);
4027 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4028 G_CALLBACK (renderer_editing_canceled_cb), impl);
4029 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4030 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4031 list_name_data_func, impl, NULL);
4033 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4037 column = gtk_tree_view_column_new ();
4038 gtk_tree_view_column_set_title (column, _("Size"));
4040 renderer = gtk_cell_renderer_text_new ();
4041 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4042 gtk_tree_view_column_set_cell_data_func (column, renderer,
4043 list_size_data_func, impl, NULL);
4044 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4045 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4047 /* Modification time column */
4049 column = gtk_tree_view_column_new ();
4050 gtk_tree_view_column_set_resizable (column, TRUE);
4051 gtk_tree_view_column_set_title (column, _("Modified"));
4053 renderer = gtk_cell_renderer_text_new ();
4054 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4055 gtk_tree_view_column_set_cell_data_func (column, renderer,
4056 list_mtime_data_func, impl, NULL);
4057 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
4058 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4059 gtk_widget_show_all (swin);
4065 create_path_bar (GtkFileChooserDefault *impl)
4067 GtkWidget *path_bar;
4069 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4070 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4076 set_filter_tooltip (GtkWidget *widget,
4079 GtkTooltips *tooltips = (GtkTooltips *)data;
4081 if (GTK_IS_BUTTON (widget))
4082 gtk_tooltips_set_tip (tooltips, widget,
4083 _("Select which types of files are shown"),
4088 realize_filter_combo (GtkWidget *combo,
4091 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
4093 gtk_container_forall (GTK_CONTAINER (combo),
4098 /* Creates the widgets for the files/folders pane */
4100 file_pane_create (GtkFileChooserDefault *impl,
4101 GtkSizeGroup *size_group)
4107 vbox = gtk_vbox_new (FALSE, 6);
4108 gtk_widget_show (vbox);
4110 /* Box for lists and preview */
4112 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4113 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4114 gtk_widget_show (hbox);
4118 widget = create_file_list (impl);
4119 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4123 impl->preview_box = gtk_vbox_new (FALSE, 12);
4124 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4125 /* Don't show preview box initially */
4129 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4131 widget = filter_create (impl);
4133 g_signal_connect (widget, "realize",
4134 G_CALLBACK (realize_filter_combo), impl);
4136 gtk_widget_show (widget);
4137 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4139 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4140 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4145 /* Callback used when the "Browse for more folders" expander is toggled */
4147 expander_changed_cb (GtkExpander *expander,
4149 GtkFileChooserDefault *impl)
4151 update_appearance (impl);
4154 /* Callback used when the selection changes in the save folder combo box */
4156 save_folder_combo_changed_cb (GtkComboBox *combo,
4157 GtkFileChooserDefault *impl)
4161 if (impl->changing_folder)
4164 if (gtk_combo_box_get_active_iter (combo, &iter))
4165 shortcuts_activate_iter (impl, &iter);
4168 /* Creates the combo box with the save folders */
4170 save_folder_combo_create (GtkFileChooserDefault *impl)
4173 GtkCellRenderer *cell;
4175 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4176 "model", impl->shortcuts_model,
4177 "focus-on-click", FALSE,
4179 gtk_widget_show (combo);
4181 cell = gtk_cell_renderer_pixbuf_new ();
4182 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4183 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4184 "pixbuf", SHORTCUTS_COL_PIXBUF,
4185 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4186 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4189 cell = gtk_cell_renderer_text_new ();
4190 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4191 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4192 "text", SHORTCUTS_COL_NAME,
4193 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4196 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4197 shortcuts_row_separator_func,
4198 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
4201 g_signal_connect (combo, "changed",
4202 G_CALLBACK (save_folder_combo_changed_cb), impl);
4207 /* Creates the widgets specific to Save mode */
4209 save_widgets_create (GtkFileChooserDefault *impl)
4214 GtkWidget *alignment;
4216 if (impl->save_widgets != NULL)
4219 location_switch_to_path_bar (impl);
4221 vbox = gtk_vbox_new (FALSE, 12);
4223 table = gtk_table_new (2, 2, FALSE);
4224 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4225 gtk_widget_show (table);
4226 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4227 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4231 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4232 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4233 gtk_table_attach (GTK_TABLE (table), widget,
4237 gtk_widget_show (widget);
4239 /* Location entry */
4241 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4242 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4244 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4245 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4246 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4248 GTK_EXPAND | GTK_FILL, 0,
4250 gtk_widget_show (impl->location_entry);
4251 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4254 impl->save_folder_label = gtk_label_new (NULL);
4255 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4256 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4260 gtk_widget_show (impl->save_folder_label);
4262 impl->save_folder_combo = save_folder_combo_create (impl);
4263 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4265 GTK_EXPAND | GTK_FILL, GTK_FILL,
4267 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4270 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4271 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4273 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4274 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4275 g_signal_connect (impl->save_expander, "notify::expanded",
4276 G_CALLBACK (expander_changed_cb),
4278 gtk_widget_show_all (alignment);
4280 impl->save_widgets = vbox;
4281 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4282 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4283 gtk_widget_show (impl->save_widgets);
4286 /* Destroys the widgets specific to Save mode */
4288 save_widgets_destroy (GtkFileChooserDefault *impl)
4290 if (impl->save_widgets == NULL)
4293 gtk_widget_destroy (impl->save_widgets);
4294 impl->save_widgets = NULL;
4295 impl->location_entry = NULL;
4296 impl->save_folder_label = NULL;
4297 impl->save_folder_combo = NULL;
4298 impl->save_expander = NULL;
4301 /* Turns on the path bar widget. Can be called even if we are already in that
4305 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4307 if (impl->location_entry)
4309 gtk_widget_destroy (impl->location_entry);
4310 impl->location_entry = NULL;
4313 gtk_widget_hide (impl->location_entry_box);
4316 /* Sets the full path of the current folder as the text in the location entry. */
4318 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4322 if (!impl->current_folder)
4325 if (gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4329 filename = gtk_file_system_path_to_filename (impl->file_system, impl->current_folder);
4332 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4339 text = gtk_file_system_path_to_uri (impl->file_system, impl->current_folder);
4343 gboolean need_slash;
4346 len = strlen (text);
4347 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4353 slash_text = g_new (char, len + 2);
4354 strcpy (slash_text, text);
4355 slash_text[len] = G_DIR_SEPARATOR;
4356 slash_text[len + 1] = 0;
4362 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4367 /* Turns on the location entry. Can be called even if we are already in that
4371 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4373 if (impl->location_entry)
4374 gtk_widget_destroy (impl->location_entry);
4378 gtk_widget_show (impl->location_entry_box);
4382 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4383 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4385 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4386 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4388 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4389 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4391 /* Configure the entry */
4393 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4394 location_entry_set_initial_text (impl);
4398 gtk_widget_show (impl->location_entry);
4399 gtk_widget_grab_focus (impl->location_entry);
4402 /* Sets a new location mode. set_buttons determines whether the toggle button
4403 * for the mode will also be changed.
4406 location_mode_set (GtkFileChooserDefault *impl,
4407 LocationMode new_mode,
4408 gboolean set_button)
4410 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4411 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4413 GtkWindow *toplevel;
4414 GtkWidget *current_focus;
4415 gboolean button_active;
4416 gboolean switch_to_file_list;
4420 case LOCATION_MODE_PATH_BAR:
4421 button_active = FALSE;
4423 /* The location_entry will disappear when we switch to path bar mode. So,
4424 * we'll focus the file list in that case, to avoid having a window with
4425 * no focused widget.
4427 toplevel = get_toplevel (GTK_WIDGET (impl));
4428 switch_to_file_list = FALSE;
4431 current_focus = gtk_window_get_focus (toplevel);
4432 if (!current_focus || current_focus == impl->location_entry)
4433 switch_to_file_list = TRUE;
4436 location_switch_to_path_bar (impl);
4438 if (switch_to_file_list)
4439 gtk_widget_grab_focus (impl->browse_files_tree_view);
4443 case LOCATION_MODE_FILENAME_ENTRY:
4444 button_active = TRUE;
4445 location_switch_to_filename_entry (impl);
4449 g_assert_not_reached ();
4455 g_signal_handlers_block_by_func (impl->location_button,
4456 G_CALLBACK (location_button_toggled_cb), impl);
4458 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4460 g_signal_handlers_unblock_by_func (impl->location_button,
4461 G_CALLBACK (location_button_toggled_cb), impl);
4465 impl->location_mode = new_mode;
4468 /* Callback used when one of the location mode buttons is toggled */
4470 location_button_toggled_cb (GtkToggleButton *toggle,
4471 GtkFileChooserDefault *impl)
4474 LocationMode new_mode;
4476 is_active = gtk_toggle_button_get_active (toggle);
4480 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4481 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4485 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4486 new_mode = LOCATION_MODE_PATH_BAR;
4489 location_mode_set (impl, new_mode, FALSE);
4492 /* Creates a toggle button for the location entry. */
4494 location_button_create (GtkFileChooserDefault *impl)
4499 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4500 gtk_widget_show (image);
4502 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4506 g_signal_connect (impl->location_button, "toggled",
4507 G_CALLBACK (location_button_toggled_cb), impl);
4509 str = _("Type a file name");
4511 gtk_tooltips_set_tip (impl->tooltips, impl->location_button, str, NULL);
4512 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4515 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4517 browse_widgets_create (GtkFileChooserDefault *impl)
4523 GtkSizeGroup *size_group;
4526 /* size group is used by the [+][-] buttons and the filter combo */
4527 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4528 vbox = gtk_vbox_new (FALSE, 12);
4530 /* Location widgets */
4531 hbox = gtk_hbox_new (FALSE, 12);
4532 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4533 gtk_widget_show (hbox);
4535 location_button_create (impl);
4536 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
4540 impl->browse_path_bar = create_path_bar (impl);
4541 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4542 gtk_widget_show_all (impl->browse_path_bar);
4543 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4546 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4547 g_signal_connect (impl->browse_new_folder_button, "clicked",
4548 G_CALLBACK (new_folder_button_clicked), impl);
4549 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4551 /* Box for the location label and entry */
4553 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
4554 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
4556 text = g_strconcat ("<b>", _("_Location:"), "</b>", NULL);
4557 impl->location_label = gtk_label_new_with_mnemonic (text);
4559 gtk_label_set_use_markup (GTK_LABEL (impl->location_label), TRUE);
4560 gtk_widget_show (impl->location_label);
4561 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
4564 hpaned = gtk_hpaned_new ();
4565 gtk_widget_show (hpaned);
4566 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
4567 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
4569 widget = shortcuts_pane_create (impl, size_group);
4570 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4571 widget = file_pane_create (impl, size_group);
4572 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4574 g_object_unref (size_group);
4580 gtk_file_chooser_default_constructor (GType type,
4581 guint n_construct_properties,
4582 GObjectConstructParam *construct_params)
4584 GtkFileChooserDefault *impl;
4587 profile_start ("start", NULL);
4589 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
4590 n_construct_properties,
4592 impl = GTK_FILE_CHOOSER_DEFAULT (object);
4594 g_assert (impl->file_system);
4596 gtk_widget_push_composite_child ();
4598 /* Shortcuts model */
4600 shortcuts_model_create (impl);
4602 /* The browse widgets */
4603 impl->browse_widgets = browse_widgets_create (impl);
4604 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
4606 /* Alignment to hold extra widget */
4607 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4608 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4610 gtk_widget_pop_composite_child ();
4611 update_appearance (impl);
4613 profile_end ("end", NULL);
4618 /* Sets the extra_widget by packing it in the appropriate place */
4620 set_extra_widget (GtkFileChooserDefault *impl,
4621 GtkWidget *extra_widget)
4625 g_object_ref (extra_widget);
4626 /* FIXME: is this right ? */
4627 gtk_widget_show (extra_widget);
4630 if (impl->extra_widget)
4632 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4633 g_object_unref (impl->extra_widget);
4636 impl->extra_widget = extra_widget;
4637 if (impl->extra_widget)
4639 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4640 gtk_widget_show (impl->extra_align);
4643 gtk_widget_hide (impl->extra_align);
4647 set_local_only (GtkFileChooserDefault *impl,
4648 gboolean local_only)
4650 if (local_only != impl->local_only)
4652 impl->local_only = local_only;
4654 if (impl->shortcuts_model && impl->file_system)
4656 shortcuts_add_volumes (impl);
4657 shortcuts_add_bookmarks (impl);
4661 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4663 /* If we are pointing to a non-local folder, make an effort to change
4664 * back to a local folder, but it's really up to the app to not cause
4665 * such a situation, so we ignore errors.
4667 const gchar *home = g_get_home_dir ();
4668 GtkFilePath *home_path;
4673 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
4675 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
4677 gtk_file_path_free (home_path);
4683 volumes_changed_cb (GtkFileSystem *file_system,
4684 GtkFileChooserDefault *impl)
4686 shortcuts_add_volumes (impl);
4689 /* Callback used when the set of bookmarks changes in the file system */
4691 bookmarks_changed_cb (GtkFileSystem *file_system,
4692 GtkFileChooserDefault *impl)
4694 shortcuts_add_bookmarks (impl);
4696 bookmarks_check_add_sensitivity (impl);
4697 bookmarks_check_remove_sensitivity (impl);
4698 shortcuts_check_popup_sensitivity (impl);
4701 /* Sets the file chooser to multiple selection mode */
4703 set_select_multiple (GtkFileChooserDefault *impl,
4704 gboolean select_multiple,
4705 gboolean property_notify)
4707 GtkTreeSelection *selection;
4708 GtkSelectionMode mode;
4710 if (select_multiple == impl->select_multiple)
4713 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
4715 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4716 gtk_tree_selection_set_mode (selection, mode);
4718 impl->select_multiple = select_multiple;
4719 g_object_notify (G_OBJECT (impl), "select-multiple");
4721 check_preview_change (impl);
4725 set_file_system_backend (GtkFileChooserDefault *impl,
4726 const char *backend)
4728 profile_start ("start for backend", backend ? backend : "default");
4730 if (impl->file_system)
4732 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
4733 impl->volumes_changed_id = 0;
4734 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
4735 impl->bookmarks_changed_id = 0;
4736 g_object_unref (impl->file_system);
4739 impl->file_system = NULL;
4741 impl->file_system = _gtk_file_system_create (backend);
4744 GtkSettings *settings = gtk_settings_get_default ();
4745 gchar *default_backend = NULL;
4747 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
4748 if (default_backend)
4750 impl->file_system = _gtk_file_system_create (default_backend);
4751 g_free (default_backend);
4755 if (!impl->file_system)
4757 #if defined (G_OS_UNIX)
4758 impl->file_system = gtk_file_system_unix_new ();
4759 #elif defined (G_OS_WIN32)
4760 impl->file_system = gtk_file_system_win32_new ();
4762 #error "No default filesystem implementation on the platform"
4766 if (impl->file_system)
4768 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
4769 G_CALLBACK (volumes_changed_cb),
4771 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
4772 G_CALLBACK (bookmarks_changed_cb),
4776 profile_end ("end", NULL);
4779 /* This function is basically a do_all function.
4781 * It sets the visibility on all the widgets based on the current state, and
4782 * moves the custom_widget if needed.
4785 update_appearance (GtkFileChooserDefault *impl)
4787 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4788 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4792 gtk_widget_hide (impl->location_button);
4793 save_widgets_create (impl);
4795 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4796 text = _("Save in _folder:");
4798 text = _("Create in _folder:");
4800 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
4802 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4804 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
4805 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
4806 gtk_widget_show (impl->browse_widgets);
4810 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
4811 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
4812 gtk_widget_hide (impl->browse_widgets);
4815 gtk_widget_show (impl->browse_new_folder_button);
4817 if (impl->select_multiple)
4819 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
4820 "Re-setting to single selection mode.");
4821 set_select_multiple (impl, FALSE, TRUE);
4824 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4825 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4827 gtk_widget_show (impl->location_button);
4828 save_widgets_destroy (impl);
4829 gtk_widget_show (impl->browse_widgets);
4830 location_mode_set (impl, impl->location_mode, TRUE);
4833 if (impl->location_entry)
4834 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4836 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
4837 gtk_widget_hide (impl->browse_new_folder_button);
4839 gtk_widget_show (impl->browse_new_folder_button);
4841 /* This *is* needed; we need to redraw the file list because the "sensitivity"
4842 * of files may change depending whether we are in a file or folder-only mode.
4844 gtk_widget_queue_draw (impl->browse_files_tree_view);
4846 g_signal_emit_by_name (impl, "default-size-changed");
4850 gtk_file_chooser_default_set_property (GObject *object,
4852 const GValue *value,
4856 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4860 case GTK_FILE_CHOOSER_PROP_ACTION:
4862 GtkFileChooserAction action = g_value_get_enum (value);
4864 if (action != impl->action)
4866 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
4868 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4869 && impl->select_multiple)
4871 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
4872 "this is not allowed in multiple selection mode. Resetting the file chooser "
4873 "to single selection mode.");
4874 set_select_multiple (impl, FALSE, TRUE);
4876 impl->action = action;
4877 update_appearance (impl);
4882 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
4883 set_file_system_backend (impl, g_value_get_string (value));
4886 case GTK_FILE_CHOOSER_PROP_FILTER:
4887 set_current_filter (impl, g_value_get_object (value));
4890 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4891 set_local_only (impl, g_value_get_boolean (value));
4894 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4895 set_preview_widget (impl, g_value_get_object (value));
4898 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4899 impl->preview_widget_active = g_value_get_boolean (value);
4900 update_preview_widget_visibility (impl);
4903 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4904 impl->use_preview_label = g_value_get_boolean (value);
4905 update_preview_widget_visibility (impl);
4908 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4909 set_extra_widget (impl, g_value_get_object (value));
4912 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4914 gboolean select_multiple = g_value_get_boolean (value);
4915 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4918 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
4919 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
4920 "leaving the file chooser in single selection mode.");
4924 set_select_multiple (impl, select_multiple, FALSE);
4928 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
4930 gboolean show_hidden = g_value_get_boolean (value);
4931 if (show_hidden != impl->show_hidden)
4933 impl->show_hidden = show_hidden;
4935 if (impl->browse_files_model)
4936 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
4941 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
4943 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
4944 impl->do_overwrite_confirmation = do_overwrite_confirmation;
4949 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4955 gtk_file_chooser_default_get_property (GObject *object,
4960 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4964 case GTK_FILE_CHOOSER_PROP_ACTION:
4965 g_value_set_enum (value, impl->action);
4968 case GTK_FILE_CHOOSER_PROP_FILTER:
4969 g_value_set_object (value, impl->current_filter);
4972 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4973 g_value_set_boolean (value, impl->local_only);
4976 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4977 g_value_set_object (value, impl->preview_widget);
4980 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4981 g_value_set_boolean (value, impl->preview_widget_active);
4984 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4985 g_value_set_boolean (value, impl->use_preview_label);
4988 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4989 g_value_set_object (value, impl->extra_widget);
4992 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4993 g_value_set_boolean (value, impl->select_multiple);
4996 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
4997 g_value_set_boolean (value, impl->show_hidden);
5000 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5001 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5005 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5010 /* Removes the settings signal handler. It's safe to call multiple times */
5012 remove_settings_signal (GtkFileChooserDefault *impl,
5015 if (impl->settings_signal_id)
5017 GtkSettings *settings;
5019 settings = gtk_settings_get_for_screen (screen);
5020 g_signal_handler_disconnect (settings,
5021 impl->settings_signal_id);
5022 impl->settings_signal_id = 0;
5027 gtk_file_chooser_default_dispose (GObject *object)
5030 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5032 if (impl->extra_widget)
5034 g_object_unref (impl->extra_widget);
5035 impl->extra_widget = NULL;
5038 if (impl->volumes_changed_id > 0)
5040 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5041 impl->volumes_changed_id = 0;
5044 if (impl->bookmarks_changed_id > 0)
5046 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5047 impl->bookmarks_changed_id = 0;
5050 pending_select_paths_free (impl);
5052 /* cancel all pending operations */
5053 if (impl->pending_handles)
5055 for (l = impl->pending_handles; l; l = l->next)
5057 GtkFileSystemHandle *handle =l->data;
5058 gtk_file_system_cancel_operation (handle);
5060 g_slist_free (impl->pending_handles);
5061 impl->pending_handles = NULL;
5064 if (impl->reload_icon_handles)
5066 for (l = impl->reload_icon_handles; l; l = l->next)
5068 GtkFileSystemHandle *handle =l->data;
5069 gtk_file_system_cancel_operation (handle);
5071 g_slist_free (impl->reload_icon_handles);
5072 impl->reload_icon_handles = NULL;
5075 if (impl->loading_shortcuts)
5077 for (l = impl->loading_shortcuts; l; l = l->next)
5079 GtkFileSystemHandle *handle =l->data;
5080 gtk_file_system_cancel_operation (handle);
5082 g_slist_free (impl->loading_shortcuts);
5083 impl->loading_shortcuts = NULL;
5086 if (impl->file_list_drag_data_received_handle)
5088 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
5089 impl->file_list_drag_data_received_handle = NULL;
5092 if (impl->update_current_folder_handle)
5094 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
5095 impl->update_current_folder_handle = NULL;
5098 if (impl->show_and_select_paths_handle)
5100 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5101 impl->show_and_select_paths_handle = NULL;
5104 if (impl->should_respond_get_info_handle)
5106 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
5107 impl->should_respond_get_info_handle = NULL;
5110 if (impl->update_from_entry_handle)
5112 gtk_file_system_cancel_operation (impl->update_from_entry_handle);
5113 impl->update_from_entry_handle = NULL;
5116 if (impl->shortcuts_activate_iter_handle)
5118 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
5119 impl->shortcuts_activate_iter_handle = NULL;
5122 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5124 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5127 /* We override show-all since we have internal widgets that
5128 * shouldn't be shown when you call show_all(), like the filter
5132 gtk_file_chooser_default_show_all (GtkWidget *widget)
5134 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5136 gtk_widget_show (widget);
5138 if (impl->extra_widget)
5139 gtk_widget_show_all (impl->extra_widget);
5142 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5143 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5146 toplevel_set_focus_cb (GtkWindow *window,
5148 GtkFileChooserDefault *impl)
5150 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5153 /* We monitor the focus widget on our toplevel to be able to know which widget
5154 * was last focused at the time our "should_respond" method gets called.
5157 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5158 GtkWidget *previous_toplevel)
5160 GtkFileChooserDefault *impl;
5161 GtkWidget *toplevel;
5163 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5165 if (previous_toplevel)
5167 g_assert (impl->toplevel_set_focus_id != 0);
5168 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5169 impl->toplevel_set_focus_id = 0;
5170 impl->toplevel_last_focus_widget = NULL;
5173 g_assert (impl->toplevel_set_focus_id == 0);
5175 toplevel = gtk_widget_get_toplevel (widget);
5176 if (GTK_IS_WINDOW (toplevel))
5178 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5179 G_CALLBACK (toplevel_set_focus_cb), impl);
5180 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5184 /* Changes the icons wherever it is needed */
5186 change_icon_theme (GtkFileChooserDefault *impl)
5188 GtkSettings *settings;
5191 profile_start ("start", NULL);
5193 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5195 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5196 impl->icon_size = MAX (width, height);
5198 impl->icon_size = FALLBACK_ICON_SIZE;
5200 shortcuts_reload_icons (impl);
5201 gtk_widget_queue_resize (impl->browse_files_tree_view);
5203 profile_end ("end", NULL);
5206 /* Callback used when a GtkSettings value changes */
5208 settings_notify_cb (GObject *object,
5210 GtkFileChooserDefault *impl)
5214 profile_start ("start", NULL);
5216 name = g_param_spec_get_name (pspec);
5218 if (strcmp (name, "gtk-icon-theme-name") == 0
5219 || strcmp (name, "gtk-icon-sizes") == 0)
5220 change_icon_theme (impl);
5222 profile_end ("end", NULL);
5225 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5229 check_icon_theme (GtkFileChooserDefault *impl)
5231 GtkSettings *settings;
5233 profile_start ("start", NULL);
5235 if (impl->settings_signal_id)
5237 profile_end ("end", NULL);
5241 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5243 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5244 impl->settings_signal_id = g_signal_connect (settings, "notify",
5245 G_CALLBACK (settings_notify_cb), impl);
5247 change_icon_theme (impl);
5250 profile_end ("end", NULL);
5254 gtk_file_chooser_default_style_set (GtkWidget *widget,
5255 GtkStyle *previous_style)
5257 GtkFileChooserDefault *impl;
5259 profile_start ("start", NULL);
5261 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5263 profile_msg (" parent class style_set start", NULL);
5264 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5265 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5266 profile_msg (" parent class style_set end", NULL);
5268 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5269 change_icon_theme (impl);
5271 profile_msg (" emit default-size-changed start", NULL);
5272 g_signal_emit_by_name (widget, "default-size-changed");
5273 profile_msg (" emit default-size-changed end", NULL);
5275 profile_end ("end", NULL);
5279 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5280 GdkScreen *previous_screen)
5282 GtkFileChooserDefault *impl;
5284 profile_start ("start", NULL);
5286 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5288 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5289 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5291 remove_settings_signal (impl, previous_screen);
5292 check_icon_theme (impl);
5294 g_signal_emit_by_name (widget, "default-size-changed");
5296 profile_end ("end", NULL);
5300 get_is_file_filtered (GtkFileChooserDefault *impl,
5301 const GtkFilePath *path,
5302 GtkFileInfo *file_info)
5304 GtkFileFilterInfo filter_info;
5305 GtkFileFilterFlags needed;
5308 if (!impl->current_filter)
5311 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5313 needed = gtk_file_filter_get_needed (impl->current_filter);
5315 filter_info.display_name = gtk_file_info_get_display_name (file_info);
5316 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
5318 if (needed & GTK_FILE_FILTER_FILENAME)
5320 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
5321 if (filter_info.filename)
5322 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5325 filter_info.filename = NULL;
5327 if (needed & GTK_FILE_FILTER_URI)
5329 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
5330 if (filter_info.uri)
5331 filter_info.contains |= GTK_FILE_FILTER_URI;
5334 filter_info.uri = NULL;
5336 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5338 if (filter_info.filename)
5339 g_free ((gchar *)filter_info.filename);
5340 if (filter_info.uri)
5341 g_free ((gchar *)filter_info.uri);
5347 settings_load (GtkFileChooserDefault *impl)
5349 GtkFileChooserSettings *settings;
5350 LocationMode location_mode;
5351 gboolean show_hidden;
5353 settings = _gtk_file_chooser_settings_new ();
5355 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5356 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5358 g_object_unref (settings);
5360 location_mode_set (impl, location_mode, TRUE);
5361 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5365 settings_save (GtkFileChooserDefault *impl)
5367 GtkFileChooserSettings *settings;
5369 settings = _gtk_file_chooser_settings_new ();
5371 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5372 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5375 _gtk_file_chooser_settings_save (settings, NULL);
5377 g_object_unref (settings);
5380 /* GtkWidget::map method */
5382 gtk_file_chooser_default_map (GtkWidget *widget)
5384 GtkFileChooserDefault *impl;
5385 char *current_working_dir;
5387 profile_start ("start", NULL);
5389 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5391 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5393 switch (impl->reload_state)
5396 /* The user didn't explicitly give us a folder to display, so we'll use the cwd */
5397 current_working_dir = g_get_current_dir ();
5398 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), current_working_dir);
5399 g_free (current_working_dir);
5402 case RELOAD_HAS_FOLDER:
5403 /* Nothing; we are already loading or loaded, so we don't need to reload */
5406 case RELOAD_WAS_UNMAPPED:
5407 /* Just reload the current folder; else continue the pending load. */
5408 if (impl->current_folder)
5410 pending_select_paths_store_selection (impl);
5411 change_folder_and_display_error (impl, impl->current_folder);
5416 g_assert_not_reached ();
5419 bookmarks_changed_cb (impl->file_system, impl);
5421 settings_load (impl);
5423 profile_end ("end", NULL);
5426 /* GtkWidget::unmap method */
5428 gtk_file_chooser_default_unmap (GtkWidget *widget)
5430 GtkFileChooserDefault *impl;
5432 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5434 settings_save (impl);
5436 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5438 impl->reload_state = RELOAD_WAS_UNMAPPED;
5442 list_model_filter_func (GtkFileSystemModel *model,
5444 const GtkFileInfo *file_info,
5447 GtkFileChooserDefault *impl = user_data;
5449 if (!impl->current_filter)
5452 if (gtk_file_info_get_is_folder (file_info))
5455 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
5459 install_list_model_filter (GtkFileChooserDefault *impl)
5461 GtkFileSystemModelFilter filter;
5464 g_assert (impl->browse_files_model != NULL);
5466 if (impl->current_filter)
5468 filter = list_model_filter_func;
5477 _gtk_file_system_model_set_filter (impl->browse_files_model,
5482 #define COMPARE_DIRECTORIES \
5483 GtkFileChooserDefault *impl = user_data; \
5484 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
5485 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
5486 gboolean dir_a, dir_b; \
5489 dir_a = gtk_file_info_get_is_folder (info_a); \
5491 return impl->list_sort_ascending ? -1 : 1; \
5494 dir_b = gtk_file_info_get_is_folder (info_b); \
5496 return impl->list_sort_ascending ? 1 : -1; \
5498 if (dir_a != dir_b) \
5499 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5501 /* Sort callback for the filename column */
5503 name_sort_func (GtkTreeModel *model,
5508 COMPARE_DIRECTORIES;
5510 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
5513 /* Sort callback for the size column */
5515 size_sort_func (GtkTreeModel *model,
5520 COMPARE_DIRECTORIES;
5523 gint64 size_a = gtk_file_info_get_size (info_a);
5524 gint64 size_b = gtk_file_info_get_size (info_b);
5526 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
5530 /* Sort callback for the mtime column */
5532 mtime_sort_func (GtkTreeModel *model,
5537 COMPARE_DIRECTORIES;
5540 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
5541 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
5543 return ta > tb ? -1 : (ta == tb ? 0 : 1);
5547 /* Callback used when the sort column changes. We cache the sort order for use
5548 * in name_sort_func().
5551 list_sort_column_changed_cb (GtkTreeSortable *sortable,
5552 GtkFileChooserDefault *impl)
5554 GtkSortType sort_type;
5556 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
5557 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
5561 set_busy_cursor (GtkFileChooserDefault *impl,
5564 GtkWindow *toplevel;
5565 GdkDisplay *display;
5568 toplevel = get_toplevel (GTK_WIDGET (impl));
5569 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
5572 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
5575 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
5579 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
5580 gdk_display_flush (display);
5583 gdk_cursor_unref (cursor);
5586 /* Creates a sort model to wrap the file system model and sets it on the tree view */
5588 load_set_model (GtkFileChooserDefault *impl)
5590 profile_start ("start", NULL);
5592 g_assert (impl->browse_files_model != NULL);
5593 g_assert (impl->sort_model == NULL);
5595 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
5596 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
5597 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
5598 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
5599 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
5600 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
5601 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
5602 impl->list_sort_ascending = TRUE;
5603 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
5605 g_signal_connect (impl->sort_model, "sort_column_changed",
5606 G_CALLBACK (list_sort_column_changed_cb), impl);
5608 profile_msg (" gtk_tree_view_set_model start", NULL);
5609 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
5610 GTK_TREE_MODEL (impl->sort_model));
5611 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
5612 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
5613 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
5614 profile_msg (" gtk_tree_view_set_model end", NULL);
5616 profile_end ("end", NULL);
5619 /* Timeout callback used when the loading timer expires */
5621 load_timeout_cb (gpointer data)
5623 GtkFileChooserDefault *impl;
5625 profile_start ("start", NULL);
5627 GDK_THREADS_ENTER ();
5629 impl = GTK_FILE_CHOOSER_DEFAULT (data);
5630 g_assert (impl->load_state == LOAD_PRELOAD);
5631 g_assert (impl->load_timeout_id != 0);
5632 g_assert (impl->browse_files_model != NULL);
5634 impl->load_timeout_id = 0;
5635 impl->load_state = LOAD_LOADING;
5637 load_set_model (impl);
5639 GDK_THREADS_LEAVE ();
5641 profile_end ("end", NULL);
5646 /* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
5648 load_setup_timer (GtkFileChooserDefault *impl)
5650 g_assert (impl->load_timeout_id == 0);
5651 g_assert (impl->load_state != LOAD_PRELOAD);
5653 impl->load_timeout_id = g_timeout_add (MAX_LOADING_TIME, load_timeout_cb, impl);
5654 impl->load_state = LOAD_PRELOAD;
5657 /* Removes the load timeout and switches to the LOAD_FINISHED state */
5659 load_remove_timer (GtkFileChooserDefault *impl)
5661 if (impl->load_timeout_id != 0)
5663 g_assert (impl->load_state == LOAD_PRELOAD);
5665 g_source_remove (impl->load_timeout_id);
5666 impl->load_timeout_id = 0;
5667 impl->load_state = LOAD_EMPTY;
5670 g_assert (impl->load_state == LOAD_EMPTY ||
5671 impl->load_state == LOAD_LOADING ||
5672 impl->load_state == LOAD_FINISHED);
5675 /* Selects the first row in the file list */
5677 browse_files_select_first_row (GtkFileChooserDefault *impl)
5681 if (!impl->sort_model)
5684 path = gtk_tree_path_new_from_indices (0, -1);
5685 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
5686 gtk_tree_path_free (path);
5689 struct center_selected_row_closure {
5690 GtkFileChooserDefault *impl;
5691 gboolean already_centered;
5694 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
5695 * selected row in the tree view.
5698 center_selected_row_foreach_cb (GtkTreeModel *model,
5703 struct center_selected_row_closure *closure;
5706 if (closure->already_centered)
5709 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
5710 closure->already_centered = TRUE;
5713 /* Centers the selected row in the tree view */
5715 browse_files_center_selected_row (GtkFileChooserDefault *impl)
5717 struct center_selected_row_closure closure;
5718 GtkTreeSelection *selection;
5720 closure.impl = impl;
5721 closure.already_centered = FALSE;
5723 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5724 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
5727 struct ShowAndSelectPathsData
5729 GtkFileChooserDefault *impl;
5734 show_and_select_paths_finished_loading (GtkFileFolder *folder,
5737 gboolean have_hidden;
5738 gboolean have_filtered;
5740 struct ShowAndSelectPathsData *data = user_data;
5742 have_hidden = FALSE;
5743 have_filtered = FALSE;
5745 for (l = data->paths; l; l = l->next)
5747 const GtkFilePath *path;
5753 info = gtk_file_folder_get_info (folder, path, NULL);
5757 have_hidden = gtk_file_info_get_is_hidden (info);
5760 have_filtered = !gtk_file_info_get_is_folder (info) && get_is_file_filtered (data->impl, path, info);
5762 gtk_file_info_free (info);
5764 if (have_hidden && have_filtered)
5765 break; /* we now have all the information we need */
5769 g_signal_handlers_disconnect_by_func (folder,
5770 show_and_select_paths_finished_loading,
5773 g_object_unref (folder);
5776 g_object_set (data->impl, "show-hidden", TRUE, NULL);
5779 set_current_filter (data->impl, NULL);
5781 for (l = data->paths; l; l = l->next)
5783 const GtkFilePath *path;
5786 _gtk_file_system_model_path_do (data->impl->browse_files_model, path,
5787 select_func, data->impl);
5790 browse_files_center_selected_row (data->impl);
5792 gtk_file_paths_free (data->paths);
5797 show_and_select_paths_get_folder_cb (GtkFileSystemHandle *handle,
5798 GtkFileFolder *folder,
5799 const GError *error,
5802 gboolean cancelled = handle->cancelled;
5803 struct ShowAndSelectPathsData *data = user_data;
5805 if (data->impl->show_and_select_paths_handle != handle)
5808 data->impl->show_and_select_paths_handle = NULL;
5810 if (cancelled || error)
5813 g_object_unref (handle);
5815 if (gtk_file_folder_is_finished_loading (folder))
5816 show_and_select_paths_finished_loading (folder, user_data);
5818 g_signal_connect (folder, "finished-loading",
5819 G_CALLBACK (show_and_select_paths_finished_loading),
5825 g_object_unref (data->impl);
5826 gtk_file_paths_free (data->paths);
5829 g_object_unref (handle);
5833 show_and_select_paths (GtkFileChooserDefault *impl,
5834 const GtkFilePath *parent_path,
5838 struct ShowAndSelectPathsData *info;
5840 profile_start ("start", NULL);
5844 profile_end ("end", NULL);
5848 info = g_new (struct ShowAndSelectPathsData, 1);
5849 info->impl = g_object_ref (impl);
5850 info->paths = gtk_file_paths_copy (paths);
5852 if (impl->show_and_select_paths_handle)
5853 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5855 impl->show_and_select_paths_handle =
5856 gtk_file_system_get_folder (impl->file_system, parent_path,
5857 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN,
5858 show_and_select_paths_get_folder_cb, info);
5860 profile_end ("end", NULL);
5864 /* Processes the pending operation when a folder is finished loading */
5866 pending_select_paths_process (GtkFileChooserDefault *impl)
5868 g_assert (impl->load_state == LOAD_FINISHED);
5869 g_assert (impl->browse_files_model != NULL);
5870 g_assert (impl->sort_model != NULL);
5872 impl->processing_pending_selections = TRUE;
5874 if (impl->pending_select_paths)
5877 show_and_select_paths (impl, impl->current_folder, impl->pending_select_paths, NULL);
5878 pending_select_paths_free (impl);
5879 browse_files_center_selected_row (impl);
5883 /* We only select the first row if the chooser is actually mapped ---
5884 * selecting the first row is to help the user when he is interacting with
5885 * the chooser, but sometimes a chooser works not on behalf of the user,
5886 * but rather on behalf of something else like GtkFileChooserButton. In
5887 * that case, the chooser's selection should be what the caller expects,
5888 * as the user can't see that something else got selected. See bug #165264.
5890 * Also, we don't select the first file if we are not in OPEN mode. Doing
5891 * so would change the contents of the filename entry for SAVE or
5892 * CREATE_FOLDER, which is undesired; in SELECT_FOLDER, we don't want to
5893 * select a *different* folder from the one into which the user just
5896 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5897 browse_files_select_first_row (impl);
5900 impl->processing_pending_selections = FALSE;
5902 g_assert (impl->pending_select_paths == NULL);
5905 /* Callback used when the file system model finishes loading */
5907 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
5908 GtkFileChooserDefault *impl)
5910 profile_start ("start", NULL);
5912 if (impl->load_state == LOAD_PRELOAD)
5914 load_remove_timer (impl);
5915 load_set_model (impl);
5917 else if (impl->load_state == LOAD_LOADING)
5923 /* We can't g_assert_not_reached(), as something other than us may have
5924 * initiated a folder reload. See #165556.
5926 profile_end ("end", NULL);
5930 g_assert (impl->load_timeout_id == 0);
5932 impl->load_state = LOAD_FINISHED;
5934 pending_select_paths_process (impl);
5935 set_busy_cursor (impl, FALSE);
5936 #ifdef PROFILE_FILE_CHOOSER
5937 access ("MARK: *** FINISHED LOADING", F_OK);
5940 profile_end ("end", NULL);
5943 /* Gets rid of the old list model and creates a new one for the current folder */
5945 set_list_model (GtkFileChooserDefault *impl,
5948 g_assert (impl->current_folder != NULL);
5950 profile_start ("start", NULL);
5952 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
5954 if (impl->browse_files_model)
5956 g_object_unref (impl->browse_files_model);
5957 impl->browse_files_model = NULL;
5960 if (impl->sort_model)
5962 g_object_unref (impl->sort_model);
5963 impl->sort_model = NULL;
5966 set_busy_cursor (impl, TRUE);
5967 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
5969 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
5970 impl->current_folder, 0,
5973 if (!impl->browse_files_model)
5975 set_busy_cursor (impl, FALSE);
5976 profile_end ("end", NULL);
5980 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
5982 g_signal_connect (impl->browse_files_model, "finished-loading",
5983 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
5985 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
5987 install_list_model_filter (impl);
5989 profile_end ("end", NULL);
5994 struct update_chooser_entry_selected_foreach_closure {
5996 GtkTreeIter first_selected_iter;
6000 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6005 struct update_chooser_entry_selected_foreach_closure *closure;
6008 closure->num_selected++;
6010 if (closure->num_selected == 1)
6011 closure->first_selected_iter = *iter;
6015 update_chooser_entry (GtkFileChooserDefault *impl)
6017 GtkTreeSelection *selection;
6018 struct update_chooser_entry_selected_foreach_closure closure;
6019 const char *file_part;
6021 if (impl->processing_pending_selections)
6024 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6025 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6026 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6027 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6028 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6031 g_assert (impl->location_entry != NULL);
6033 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6035 closure.num_selected = 0;
6036 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6040 if (closure.num_selected == 0)
6042 /* If nothing is selected, we only reset the file name entry if we are in
6043 * CREATE_FOLDER mode. In SAVE mode, nothing will be selected when the
6044 * user starts typeahead in the treeview, and we don't want to clear the
6045 * file name entry in that case --- the user could be typing-ahead to look
6046 * for a folder name. See http://bugzilla.gnome.org/show_bug.cgi?id=308332
6048 if (impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6051 else if (closure.num_selected == 1)
6053 GtkTreeIter child_iter;
6054 const GtkFileInfo *info;
6055 gboolean change_entry;
6057 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6059 &closure.first_selected_iter);
6061 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6063 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6064 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6065 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6066 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
6068 change_entry = TRUE; /* ... unless we are in CREATE_FOLDER mode */
6071 file_part = gtk_file_info_get_display_name (info);
6075 /* Multiple selection, so just clear the entry. */
6080 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), file_part);
6084 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6085 const GtkFilePath *path,
6088 return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, error);
6092 struct UpdateCurrentFolderData
6094 GtkFileChooserDefault *impl;
6096 gboolean keep_trail;
6100 update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
6101 const GtkFileInfo *info,
6102 const GError *error,
6105 gboolean cancelled = handle->cancelled;
6106 struct UpdateCurrentFolderData *data = user_data;
6107 GtkFileChooserDefault *impl = data->impl;
6109 if (handle != impl->update_current_folder_handle)
6112 impl->update_current_folder_handle = NULL;
6113 impl->reload_state = RELOAD_EMPTY;
6115 set_busy_cursor (impl, FALSE);
6122 error_changing_folder_dialog (impl, data->path, g_error_copy (error));
6126 if (!gtk_file_info_get_is_folder (info))
6129 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), data->path, data->keep_trail, NULL))
6132 if (impl->current_folder != data->path)
6134 if (impl->current_folder)
6135 gtk_file_path_free (impl->current_folder);
6137 impl->current_folder = gtk_file_path_copy (data->path);
6139 impl->reload_state = RELOAD_HAS_FOLDER;
6142 /* Update the widgets that may trigger a folder change themselves. */
6144 if (!impl->changing_folder)
6146 impl->changing_folder = TRUE;
6148 shortcuts_update_current_folder (impl);
6150 impl->changing_folder = FALSE;
6153 /* Set the folder on the save entry */
6155 if (impl->location_entry)
6156 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6157 impl->current_folder);
6159 /* Create a new list model. This is slightly evil; we store the result value
6160 * but perform more actions rather than returning immediately even if it
6161 * generates an error.
6163 set_list_model (impl, NULL);
6165 /* Refresh controls */
6167 shortcuts_find_current_folder (impl);
6169 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6171 check_preview_change (impl);
6172 bookmarks_check_add_sensitivity (impl);
6174 g_signal_emit_by_name (impl, "selection-changed", 0);
6177 gtk_file_path_free (data->path);
6180 g_object_unref (handle);
6184 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6185 const GtkFilePath *path,
6186 gboolean keep_trail,
6189 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6190 struct UpdateCurrentFolderData *data;
6192 profile_start ("start", (char *) path);
6194 g_assert (path != NULL);
6196 if (impl->local_only &&
6197 !gtk_file_system_path_is_local (impl->file_system, path))
6200 GTK_FILE_CHOOSER_ERROR,
6201 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6202 _("Cannot change to folder because it is not local"));
6204 profile_end ("end - not local", (char *) path);
6208 if (impl->update_current_folder_handle)
6209 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
6211 /* Test validity of path here. */
6212 data = g_new0 (struct UpdateCurrentFolderData, 1);
6214 data->path = gtk_file_path_copy (path);
6215 data->keep_trail = keep_trail;
6217 impl->reload_state = RELOAD_HAS_FOLDER;
6219 impl->update_current_folder_handle =
6220 gtk_file_system_get_info (impl->file_system, path, GTK_FILE_INFO_IS_FOLDER,
6221 update_current_folder_get_info_cb,
6224 set_busy_cursor (impl, TRUE);
6226 profile_end ("end", NULL);
6230 static GtkFilePath *
6231 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6233 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6235 if (impl->reload_state == RELOAD_EMPTY)
6237 char *current_working_dir;
6240 /* We are unmapped, or we had an error while loading the last folder. We'll return
6241 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
6242 * explicitly calls set_current_folder() on us.
6244 current_working_dir = g_get_current_dir ();
6245 path = gtk_file_system_filename_to_path (impl->file_system, current_working_dir);
6246 g_free (current_working_dir);
6250 return gtk_file_path_copy (impl->current_folder);
6254 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
6257 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6259 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6260 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6262 pending_select_paths_free (impl);
6263 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
6267 select_func (GtkFileSystemModel *model,
6272 GtkFileChooserDefault *impl = user_data;
6273 GtkTreeSelection *selection;
6274 GtkTreeIter sorted_iter;
6276 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6278 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
6279 gtk_tree_selection_select_iter (selection, &sorted_iter);
6283 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
6284 const GtkFilePath *path,
6287 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6288 GtkFilePath *parent_path;
6291 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
6295 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
6297 if (impl->load_state == LOAD_EMPTY)
6301 g_assert (impl->current_folder != NULL);
6303 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
6306 if (same_path && impl->load_state == LOAD_FINISHED)
6311 paths.data = (gpointer) path;
6314 result = show_and_select_paths (impl, parent_path, &paths, error);
6315 gtk_file_path_free (parent_path);
6319 pending_select_paths_add (impl, path);
6325 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
6326 gtk_file_path_free (parent_path);
6330 gtk_file_path_free (parent_path);
6335 unselect_func (GtkFileSystemModel *model,
6340 GtkFileChooserDefault *impl = user_data;
6341 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
6342 GtkTreePath *sorted_path;
6344 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
6346 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
6348 gtk_tree_path_free (sorted_path);
6352 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
6353 const GtkFilePath *path)
6355 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6357 if (!impl->browse_files_model)
6360 _gtk_file_system_model_path_do (impl->browse_files_model, path,
6361 unselect_func, impl);
6365 maybe_select (GtkTreeModel *model,
6370 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
6371 GtkTreeSelection *selection;
6372 const GtkFileInfo *info;
6375 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6377 info = get_list_file_info (impl, iter);
6378 is_folder = gtk_file_info_get_is_folder (info);
6380 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
6381 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
6382 gtk_tree_selection_select_iter (selection, iter);
6384 gtk_tree_selection_unselect_iter (selection, iter);
6390 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
6392 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6393 if (impl->select_multiple)
6394 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
6395 maybe_select, impl);
6399 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
6401 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6402 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6404 gtk_tree_selection_unselect_all (selection);
6405 pending_select_paths_free (impl);
6408 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
6410 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
6412 * is_empty_ret - whether the file entry is totally empty
6414 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
6415 * the path will be "$cwd/foobar")
6418 check_save_entry (GtkFileChooserDefault *impl,
6419 GtkFilePath **path_ret,
6420 gboolean *is_well_formed_ret,
6421 gboolean *is_empty_ret,
6422 gboolean *is_file_part_empty_ret,
6423 gboolean *is_folder)
6425 GtkFileChooserEntry *chooser_entry;
6426 const GtkFilePath *current_folder;
6427 const char *file_part;
6431 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6432 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6433 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6434 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6435 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
6437 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
6439 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
6442 *is_well_formed_ret = TRUE;
6443 *is_empty_ret = TRUE;
6444 *is_file_part_empty_ret = TRUE;
6450 *is_empty_ret = FALSE;
6452 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6453 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6455 if (!file_part || file_part[0] == '\0')
6457 *path_ret = gtk_file_path_copy (current_folder);
6458 *is_well_formed_ret = TRUE;
6459 *is_file_part_empty_ret = TRUE;
6465 *is_file_part_empty_ret = FALSE;
6468 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
6472 error_building_filename_dialog (impl, current_folder, file_part, error);
6474 *is_well_formed_ret = FALSE;
6481 *is_well_formed_ret = TRUE;
6482 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, path);
6485 struct get_paths_closure {
6486 GtkFileChooserDefault *impl;
6488 GtkFilePath *path_from_entry;
6492 get_paths_foreach (GtkTreeModel *model,
6497 struct get_paths_closure *info;
6498 const GtkFilePath *file_path;
6499 GtkFileSystemModel *fs_model;
6500 GtkTreeIter sel_iter;
6503 fs_model = info->impl->browse_files_model;
6504 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
6506 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
6508 return; /* We are on the editable row */
6510 if (!info->path_from_entry
6511 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
6512 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
6516 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
6518 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6519 struct get_paths_closure info;
6520 GtkWindow *toplevel;
6521 GtkWidget *current_focus;
6525 info.path_from_entry = NULL;
6527 toplevel = get_toplevel (GTK_WIDGET (impl));
6529 current_focus = gtk_window_get_focus (toplevel);
6531 current_focus = NULL;
6533 if (current_focus == impl->browse_files_tree_view)
6535 GtkTreeSelection *selection;
6539 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6540 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
6542 /* If there is no selection in the file list, we probably have this situation:
6544 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
6545 * 2. He then double-clicked on a folder ("bar") in the file list
6547 * So we want the selection to be "bar/foo.txt". Jump to the case for the
6548 * filename entry to see if that is the case.
6550 if (info.result == NULL && impl->location_entry)
6553 else if (impl->location_entry && current_focus == impl->location_entry)
6555 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
6559 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
6561 if (!is_well_formed)
6566 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6568 gtk_file_path_free (info.path_from_entry);
6573 info.result = g_slist_prepend (info.result, info.path_from_entry);
6575 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
6577 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
6581 /* The focus is on a dialog's action area button or something else */
6582 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6583 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6589 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
6590 * fall back to the current directory */
6591 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
6592 info.result == NULL)
6594 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_path (chooser));
6597 return g_slist_reverse (info.result);
6600 static GtkFilePath *
6601 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
6603 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6605 if (impl->preview_path)
6606 return gtk_file_path_copy (impl->preview_path);
6611 static GtkFileSystem *
6612 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
6614 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6616 return impl->file_system;
6619 /* Shows or hides the filter widgets */
6621 show_filters (GtkFileChooserDefault *impl,
6625 gtk_widget_show (impl->filter_combo_hbox);
6627 gtk_widget_hide (impl->filter_combo_hbox);
6631 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
6632 GtkFileFilter *filter)
6634 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6637 if (g_slist_find (impl->filters, filter))
6639 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
6643 g_object_ref_sink (filter);
6644 impl->filters = g_slist_append (impl->filters, filter);
6646 name = gtk_file_filter_get_name (filter);
6648 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
6650 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
6652 if (!g_slist_find (impl->filters, impl->current_filter))
6653 set_current_filter (impl, filter);
6655 show_filters (impl, TRUE);
6659 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
6660 GtkFileFilter *filter)
6662 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6663 GtkTreeModel *model;
6667 filter_index = g_slist_index (impl->filters, filter);
6669 if (filter_index < 0)
6671 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
6675 impl->filters = g_slist_remove (impl->filters, filter);
6677 if (filter == impl->current_filter)
6680 set_current_filter (impl, impl->filters->data);
6682 set_current_filter (impl, NULL);
6685 /* Remove row from the combo box */
6686 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
6687 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
6688 g_assert_not_reached ();
6690 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
6692 g_object_unref (filter);
6695 show_filters (impl, FALSE);
6699 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
6701 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6703 return g_slist_copy (impl->filters);
6706 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
6708 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
6711 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
6714 struct AddShortcutData
6716 GtkFileChooserDefault *impl;
6721 add_shortcut_get_info_cb (GtkFileSystemHandle *handle,
6722 const GtkFileInfo *info,
6723 const GError *error,
6727 gboolean cancelled = handle->cancelled;
6728 struct AddShortcutData *data = user_data;
6730 if (!g_slist_find (data->impl->loading_shortcuts, handle))
6733 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, handle);
6735 if (cancelled || error || !gtk_file_info_get_is_folder (info))
6738 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
6740 shortcuts_insert_path (data->impl, pos, FALSE, NULL, data->path, NULL, FALSE, SHORTCUTS_SHORTCUTS);
6743 g_object_unref (data->impl);
6744 gtk_file_path_free (data->path);
6747 g_object_unref (handle);
6751 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
6752 const GtkFilePath *path,
6755 GtkFileSystemHandle *handle;
6756 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6757 struct AddShortcutData *data;
6761 /* Avoid adding duplicates */
6762 pos = shortcut_find_position (impl, path);
6763 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
6767 uri = gtk_file_system_path_to_uri (impl->file_system, path);
6768 /* translators, "Shortcut" means "Bookmark" here */
6770 GTK_FILE_CHOOSER_ERROR,
6771 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6772 _("Shortcut %s already exists"),
6779 for (l = impl->loading_shortcuts; l; l = l->next)
6781 GtkFileSystemHandle *h = l->data;
6784 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
6785 if (p && !gtk_file_path_compare (path, p))
6789 uri = gtk_file_system_path_to_uri (impl->file_system, path);
6791 GTK_FILE_CHOOSER_ERROR,
6792 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6793 _("Shortcut %s already exists"),
6801 data = g_new0 (struct AddShortcutData, 1);
6802 data->impl = g_object_ref (impl);
6803 data->path = gtk_file_path_copy (path);
6805 handle = gtk_file_system_get_info (impl->file_system, path,
6806 GTK_FILE_INFO_IS_FOLDER,
6807 add_shortcut_get_info_cb, data);
6812 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, handle);
6813 g_object_set_data (G_OBJECT (handle), "add-shortcut-path-key", data->path);
6819 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
6820 const GtkFilePath *path,
6823 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6830 for (l = impl->loading_shortcuts; l; l = l->next)
6832 GtkFileSystemHandle *h = l->data;
6835 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
6836 if (p && !gtk_file_path_compare (path, p))
6838 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, h);
6839 gtk_file_system_cancel_operation (h);
6844 if (impl->num_shortcuts == 0)
6847 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
6848 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
6849 g_assert_not_reached ();
6851 for (i = 0; i < impl->num_shortcuts; i++)
6855 GtkFilePath *shortcut;
6857 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
6858 SHORTCUTS_COL_DATA, &col_data,
6859 SHORTCUTS_COL_IS_VOLUME, &is_volume,
6861 g_assert (col_data != NULL);
6862 g_assert (!is_volume);
6864 shortcut = col_data;
6865 if (gtk_file_path_compare (shortcut, path) == 0)
6867 shortcuts_remove_rows (impl, pos + i, 1);
6868 impl->num_shortcuts--;
6872 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
6873 g_assert_not_reached ();
6878 uri = gtk_file_system_path_to_uri (impl->file_system, path);
6879 /* translators, "Shortcut" means "Bookmark" here */
6881 GTK_FILE_CHOOSER_ERROR,
6882 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
6883 _("Shortcut %s does not exist"),
6891 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
6893 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6899 if (impl->num_shortcuts == 0)
6902 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
6903 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
6904 g_assert_not_reached ();
6908 for (i = 0; i < impl->num_shortcuts; i++)
6912 GtkFilePath *shortcut;
6914 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
6915 SHORTCUTS_COL_DATA, &col_data,
6916 SHORTCUTS_COL_IS_VOLUME, &is_volume,
6918 g_assert (col_data != NULL);
6919 g_assert (!is_volume);
6921 shortcut = col_data;
6922 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
6924 if (i != impl->num_shortcuts - 1)
6926 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
6927 g_assert_not_reached ();
6931 return g_slist_reverse (list);
6934 /* Guesses a size based upon font sizes */
6936 find_good_size_from_style (GtkWidget *widget,
6940 GtkFileChooserDefault *impl;
6941 gint default_width, default_height;
6944 GtkRequisition preview_req;
6946 g_assert (widget->style != NULL);
6947 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
6949 font_size = pango_font_description_get_size (widget->style->font_desc);
6950 font_size = PANGO_PIXELS (font_size);
6952 default_width = font_size * NUM_CHARS;
6953 default_height = font_size * NUM_LINES;
6955 /* Use at least the requisition size not including the preview widget */
6956 gtk_widget_size_request (widget, &req);
6958 if (impl->preview_widget_active && impl->preview_widget)
6959 gtk_widget_size_request (impl->preview_box, &preview_req);
6961 preview_req.width = 0;
6963 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
6964 default_height = MAX (default_height, req.height);
6966 *width = default_width;
6967 *height = default_height;
6971 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
6972 gint *default_width,
6973 gint *default_height)
6975 GtkFileChooserDefault *impl;
6977 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
6979 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
6981 if (impl->preview_widget_active && impl->preview_widget)
6982 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
6986 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
6987 gboolean *resize_horizontally,
6988 gboolean *resize_vertically)
6990 GtkFileChooserDefault *impl;
6992 g_return_if_fail (resize_horizontally != NULL);
6993 g_return_if_fail (resize_vertically != NULL);
6995 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
6997 *resize_horizontally = TRUE;
6998 *resize_vertically = TRUE;
7000 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7001 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7003 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
7005 *resize_horizontally = FALSE;
7006 *resize_vertically = FALSE;
7011 struct switch_folder_closure {
7012 GtkFileChooserDefault *impl;
7013 const GtkFilePath *path;
7017 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7019 switch_folder_foreach_cb (GtkTreeModel *model,
7024 struct switch_folder_closure *closure;
7025 GtkTreeIter child_iter;
7029 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7031 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
7032 closure->num_selected++;
7035 /* Changes to the selected folder in the list view */
7037 switch_to_selected_folder (GtkFileChooserDefault *impl)
7039 GtkTreeSelection *selection;
7040 struct switch_folder_closure closure;
7042 /* We do this with foreach() rather than get_selected() as we may be in
7043 * multiple selection mode
7046 closure.impl = impl;
7047 closure.path = NULL;
7048 closure.num_selected = 0;
7050 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7051 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7053 g_assert (closure.path && closure.num_selected == 1);
7055 change_folder_and_display_error (impl, closure.path);
7058 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
7061 static const GtkFileInfo *
7062 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7063 gboolean *had_selection)
7065 GtkTreeSelection *selection;
7066 GtkTreeIter iter, child_iter;
7067 const GtkFileInfo *info;
7069 g_assert (!impl->select_multiple);
7070 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7071 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7073 *had_selection = FALSE;
7077 *had_selection = TRUE;
7079 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7083 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7087 /* Gets the display name of the selected file in the file list; assumes single
7088 * selection mode and that something is selected.
7090 static const gchar *
7091 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7093 const GtkFileInfo *info;
7094 gboolean had_selection;
7096 info = get_selected_file_info_from_file_list (impl, &had_selection);
7097 g_assert (had_selection);
7098 g_assert (info != NULL);
7100 return gtk_file_info_get_display_name (info);
7104 add_custom_button_to_dialog (GtkDialog *dialog,
7105 const gchar *mnemonic_label,
7106 const gchar *stock_id,
7111 button = gtk_button_new_with_mnemonic (mnemonic_label);
7112 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7113 gtk_button_set_image (GTK_BUTTON (button),
7114 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7115 gtk_widget_show (button);
7117 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7120 /* Presents an overwrite confirmation dialog; returns whether we should accept
7124 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7125 const gchar *file_part,
7126 const gchar *folder_display_name)
7128 GtkWindow *toplevel;
7132 toplevel = get_toplevel (GTK_WIDGET (impl));
7134 dialog = gtk_message_dialog_new (toplevel,
7135 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7136 GTK_MESSAGE_QUESTION,
7138 _("A file named \"%s\" already exists. Do you want to replace it?"),
7140 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7141 _("The file already exists in \"%s\". Replacing it will "
7142 "overwrite its contents."),
7143 folder_display_name);
7145 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7146 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"), GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7147 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7149 if (toplevel->group)
7150 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7152 response = gtk_dialog_run (GTK_DIALOG (dialog));
7154 gtk_widget_destroy (dialog);
7156 return (response == GTK_RESPONSE_ACCEPT);
7159 struct GetDisplayNameData
7161 GtkFileChooserDefault *impl;
7166 confirmation_confirm_get_info_cb (GtkFileSystemHandle *handle,
7167 const GtkFileInfo *info,
7168 const GError *error,
7171 gboolean cancelled = handle->cancelled;
7172 gboolean should_respond = FALSE;
7173 struct GetDisplayNameData *data = user_data;
7175 if (handle != data->impl->should_respond_get_info_handle)
7178 data->impl->should_respond_get_info_handle = NULL;
7184 /* Huh? Did the folder disappear? Let the caller deal with it */
7185 should_respond = TRUE;
7187 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, gtk_file_info_get_display_name (info));
7189 set_busy_cursor (data->impl, FALSE);
7191 g_signal_emit_by_name (data->impl, "response-requested");
7194 g_object_unref (data->impl);
7195 g_free (data->file_part);
7198 g_object_unref (handle);
7201 /* Does overwrite confirmation if appropriate, and returns whether the dialog
7202 * should respond. Can get the file part from the file list or the save entry.
7205 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
7206 const gchar *file_part,
7207 const GtkFilePath *parent_path)
7209 GtkFileChooserConfirmation conf;
7211 if (!impl->do_overwrite_confirmation)
7214 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
7216 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
7220 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
7222 struct GetDisplayNameData *data;
7224 g_assert (file_part != NULL);
7226 data = g_new0 (struct GetDisplayNameData, 1);
7227 data->impl = g_object_ref (impl);
7228 data->file_part = g_strdup (file_part);
7230 if (impl->should_respond_get_info_handle)
7231 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
7233 impl->should_respond_get_info_handle =
7234 gtk_file_system_get_info (impl->file_system, parent_path,
7235 GTK_FILE_INFO_DISPLAY_NAME,
7236 confirmation_confirm_get_info_cb,
7238 set_busy_cursor (data->impl, TRUE);
7242 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
7245 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
7249 g_assert_not_reached ();
7255 action_create_folder_cb (GtkFileSystemHandle *handle,
7256 const GtkFilePath *path,
7257 const GError *error,
7260 gboolean cancelled = handle->cancelled;
7261 GtkFileChooserDefault *impl = user_data;
7263 if (!g_slist_find (impl->pending_handles, handle))
7266 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
7268 set_busy_cursor (impl, FALSE);
7274 error_creating_folder_dialog (impl, path, g_error_copy (error));
7276 g_signal_emit_by_name (impl, "response-requested");
7279 g_object_unref (impl);
7280 g_object_unref (handle);
7283 struct SaveEntryData
7285 GtkFileChooserDefault *impl;
7286 gboolean file_exists_and_is_not_folder;
7287 GtkFilePath *parent_path;
7292 save_entry_get_info_cb (GtkFileSystemHandle *handle,
7293 const GtkFileInfo *info,
7294 const GError *error,
7297 gboolean parent_is_folder;
7298 gboolean cancelled = handle->cancelled;
7299 struct SaveEntryData *data = user_data;
7301 if (handle != data->impl->should_respond_get_info_handle)
7304 data->impl->should_respond_get_info_handle = NULL;
7306 set_busy_cursor (data->impl, FALSE);
7312 parent_is_folder = FALSE;
7314 parent_is_folder = gtk_file_info_get_is_folder (info);
7316 if (parent_is_folder)
7318 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7320 if (data->file_exists_and_is_not_folder)
7323 const char *file_part;
7325 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
7326 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_path);
7329 g_signal_emit_by_name (data->impl, "response-requested");
7332 g_signal_emit_by_name (data->impl, "response-requested");
7334 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
7336 GtkFileSystemHandle *handle;
7338 g_object_ref (data->impl);
7339 handle = gtk_file_system_create_folder (data->impl->file_system,
7341 action_create_folder_cb,
7343 data->impl->pending_handles = g_slist_append (data->impl->pending_handles, handle);
7344 set_busy_cursor (data->impl, TRUE);
7349 /* This will display an error, which is what we want */
7350 change_folder_and_display_error (data->impl, data->parent_path);
7354 g_object_unref (data->impl);
7355 gtk_file_path_free (data->path);
7356 gtk_file_path_free (data->parent_path);
7359 g_object_unref (handle);
7363 paste_text_received (GtkClipboard *clipboard,
7365 GtkFileChooserDefault *impl)
7372 path = gtk_file_system_uri_to_path (impl->file_system, text);
7375 if (!g_path_is_absolute (text))
7377 location_popup_handler (impl, text);
7381 path = gtk_file_system_filename_to_path (impl->file_system, text);
7384 location_popup_handler (impl, text);
7389 if (!gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (impl), path, NULL))
7390 location_popup_handler (impl, text);
7392 gtk_file_path_free (path);
7395 /* Handler for the "location-popup-on-paste" keybinding signal */
7397 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
7399 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
7400 GDK_SELECTION_CLIPBOARD);
7401 gtk_clipboard_request_text (clipboard,
7402 (GtkClipboardTextReceivedFunc) paste_text_received,
7407 /* Implementation for GtkFileChooserEmbed::should_respond() */
7409 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
7411 GtkFileChooserDefault *impl;
7412 GtkWidget *toplevel;
7413 GtkWidget *current_focus;
7415 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7417 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7418 g_assert (GTK_IS_WINDOW (toplevel));
7420 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
7422 if (current_focus == impl->browse_files_tree_view)
7424 /* The following array encodes what we do based on the impl->action and the
7425 * number of files selected.
7428 NOOP, /* Do nothing (don't respond) */
7429 RESPOND, /* Respond immediately */
7430 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
7431 ALL_FILES, /* Respond only if everything selected is a file */
7432 ALL_FOLDERS, /* Respond only if everything selected is a folder */
7433 SAVE_ENTRY, /* Go to the code for handling the save entry */
7434 NOT_REACHED /* Sanity check */
7436 static const ActionToTake what_to_do[4][3] = {
7437 /* 0 selected 1 selected many selected */
7438 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
7439 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
7440 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
7441 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
7445 gboolean all_files, all_folders;
7447 ActionToTake action;
7451 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7453 selection_check (impl, &num_selected, &all_files, &all_folders);
7455 if (num_selected > 2)
7460 action = what_to_do [impl->action] [k];
7470 case RESPOND_OR_SWITCH:
7471 g_assert (num_selected == 1);
7475 switch_to_selected_folder (impl);
7478 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7479 return should_respond_after_confirm_overwrite (impl,
7480 get_display_name_from_file_list (impl),
7481 impl->current_folder);
7495 g_assert_not_reached ();
7498 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
7501 gboolean is_well_formed, is_empty, is_file_part_empty;
7504 GtkFileChooserEntry *entry;
7509 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7510 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7511 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7512 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7513 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7515 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7516 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7518 if (is_empty || !is_well_formed)
7521 g_assert (path != NULL);
7526 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7527 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7529 _gtk_file_chooser_entry_set_file_part (entry, "");
7530 change_folder_and_display_error (impl, path);
7533 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7534 || GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7536 /* The folder already exists, so we do not need to create it.
7537 * Just respond to terminate the dialog.
7543 g_assert_not_reached ();
7549 gboolean file_exists_and_is_not_folder;
7551 file_exists_and_is_not_folder = g_error_matches (error, GTK_FILE_SYSTEM_ERROR, GTK_FILE_SYSTEM_ERROR_NOT_FOLDER);
7553 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
7554 retval = TRUE; /* user typed a filename; we are done */
7555 else if (impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER && file_exists_and_is_not_folder)
7557 /* Oops, the user typed the name of an existing path which is not a folder */
7558 error_creating_folder_over_existing_file_dialog (impl, path, error);
7559 error = NULL; /* as it will be freed below for the general case */
7564 GtkFilePath *parent_path;
7565 struct SaveEntryData *data;
7567 /* check that everything up to the last component exists */
7569 parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
7571 data = g_new0 (struct SaveEntryData, 1);
7572 data->impl = g_object_ref (impl);
7573 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
7574 data->parent_path = parent_path; /* Takes ownership */
7575 data->path = gtk_file_path_copy (path);
7577 if (impl->should_respond_get_info_handle)
7578 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
7580 impl->should_respond_get_info_handle =
7581 gtk_file_system_get_info (impl->file_system, parent_path,
7582 GTK_FILE_INFO_IS_FOLDER,
7583 save_entry_get_info_cb,
7585 set_busy_cursor (impl, TRUE);
7591 g_error_free (error);
7594 gtk_file_path_free (path);
7597 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
7599 /* The focus is on a dialog's action area button, *and* the widget that
7600 * was focused immediately before it is the shortcuts list. Switch to the
7601 * selected shortcut and tell the caller not to respond.
7605 if (shortcuts_get_selected (impl, &iter))
7607 shortcuts_activate_iter (impl, &iter);
7609 gtk_widget_grab_focus (impl->browse_files_tree_view);
7616 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7618 /* The focus is on a dialog's action area button, *and* the widget that
7619 * was focused immediately before it is the file list.
7623 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7625 /* The focus is on a dialog's action area button, *and* the widget that
7626 * was focused immediately before it is the location entry.
7631 /* The focus is on a dialog's action area button or something else */
7632 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7633 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7638 g_assert_not_reached ();
7642 /* Implementation for GtkFileChooserEmbed::initial_focus() */
7644 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
7646 GtkFileChooserDefault *impl;
7649 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7651 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7652 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7654 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
7655 widget = impl->browse_files_tree_view;
7657 widget = impl->location_entry;
7659 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7660 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7661 widget = impl->location_entry;
7664 g_assert_not_reached ();
7668 g_assert (widget != NULL);
7669 gtk_widget_grab_focus (widget);
7673 set_current_filter (GtkFileChooserDefault *impl,
7674 GtkFileFilter *filter)
7676 if (impl->current_filter != filter)
7680 /* NULL filters are allowed to reset to non-filtered status
7682 filter_index = g_slist_index (impl->filters, filter);
7683 if (impl->filters && filter && filter_index < 0)
7686 if (impl->current_filter)
7687 g_object_unref (impl->current_filter);
7688 impl->current_filter = filter;
7689 if (impl->current_filter)
7691 g_object_ref_sink (impl->current_filter);
7695 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
7698 if (impl->browse_files_model)
7699 install_list_model_filter (impl);
7701 g_object_notify (G_OBJECT (impl), "filter");
7706 filter_combo_changed (GtkComboBox *combo_box,
7707 GtkFileChooserDefault *impl)
7709 gint new_index = gtk_combo_box_get_active (combo_box);
7710 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
7712 set_current_filter (impl, new_filter);
7716 check_preview_change (GtkFileChooserDefault *impl)
7718 GtkTreePath *cursor_path;
7719 const GtkFilePath *new_path;
7720 const GtkFileInfo *new_info;
7722 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
7723 if (cursor_path && impl->sort_model)
7726 GtkTreeIter child_iter;
7728 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path))
7729 g_assert_not_reached ();
7731 gtk_tree_path_free (cursor_path);
7733 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
7735 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
7736 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7744 if (new_path != impl->preview_path &&
7745 !(new_path && impl->preview_path &&
7746 gtk_file_path_compare (new_path, impl->preview_path) == 0))
7748 if (impl->preview_path)
7750 gtk_file_path_free (impl->preview_path);
7751 g_free (impl->preview_display_name);
7756 impl->preview_path = gtk_file_path_copy (new_path);
7757 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
7761 impl->preview_path = NULL;
7762 impl->preview_display_name = NULL;
7765 if (impl->use_preview_label && impl->preview_label)
7766 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
7768 g_signal_emit_by_name (impl, "update-preview");
7773 shortcuts_activate_volume_mount_cb (GtkFileSystemHandle *handle,
7774 GtkFileSystemVolume *volume,
7775 const GError *error,
7779 gboolean cancelled = handle->cancelled;
7780 GtkFileChooserDefault *impl = data;
7782 if (handle != impl->shortcuts_activate_iter_handle)
7785 impl->shortcuts_activate_iter_handle = NULL;
7787 set_busy_cursor (impl, FALSE);
7796 msg = g_strdup_printf (_("Could not mount %s"),
7797 gtk_file_system_volume_get_display_name (impl->file_system, volume));
7798 error_message (impl, msg, error->message);
7804 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
7807 change_folder_and_display_error (impl, path);
7808 gtk_file_path_free (path);
7812 g_object_unref (impl);
7813 g_object_unref (handle);
7817 /* Activates a volume by mounting it if necessary and then switching to its
7821 shortcuts_activate_volume (GtkFileChooserDefault *impl,
7822 GtkFileSystemVolume *volume)
7826 /* We ref the file chooser since volume_mount() may run a main loop, and the
7827 * user could close the file chooser window in the meantime.
7829 g_object_ref (impl);
7831 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
7833 set_busy_cursor (impl, TRUE);
7835 impl->shortcuts_activate_iter_handle =
7836 gtk_file_system_volume_mount (impl->file_system, volume,
7837 shortcuts_activate_volume_mount_cb,
7838 g_object_ref (impl));
7842 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
7845 change_folder_and_display_error (impl, path);
7846 gtk_file_path_free (path);
7850 g_object_unref (impl);
7853 /* Opens the folder or volume at the specified iter in the shortcuts model */
7854 struct ShortcutsActivateData
7856 GtkFileChooserDefault *impl;
7861 shortcuts_activate_get_info_cb (GtkFileSystemHandle *handle,
7862 const GtkFileInfo *info,
7863 const GError *error,
7866 gboolean cancelled = handle->cancelled;
7867 struct ShortcutsActivateData *data = user_data;
7869 if (handle != data->impl->shortcuts_activate_iter_handle)
7872 data->impl->shortcuts_activate_iter_handle = NULL;
7877 if (!error && gtk_file_info_get_is_folder (info))
7878 change_folder_and_display_error (data->impl, data->path);
7880 gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (data->impl), data->path, NULL);
7883 g_object_unref (data->impl);
7884 gtk_file_path_free (data->path);
7887 g_object_unref (handle);
7891 shortcuts_activate_iter (GtkFileChooserDefault *impl,
7897 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
7898 SHORTCUTS_COL_DATA, &col_data,
7899 SHORTCUTS_COL_IS_VOLUME, &is_volume,
7903 return; /* We are on a separator */
7905 if (impl->shortcuts_activate_iter_handle)
7907 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
7908 impl->shortcuts_activate_iter_handle = NULL;
7913 GtkFileSystemVolume *volume;
7917 shortcuts_activate_volume (impl, volume);
7921 struct ShortcutsActivateData *data;
7923 data = g_new0 (struct ShortcutsActivateData, 1);
7924 data->impl = g_object_ref (impl);
7925 data->path = gtk_file_path_copy (col_data);
7927 impl->shortcuts_activate_iter_handle =
7928 gtk_file_system_get_info (impl->file_system, data->path,
7929 GTK_FILE_INFO_IS_FOLDER,
7930 shortcuts_activate_get_info_cb, data);
7934 /* Callback used when a row in the shortcuts list is activated */
7936 shortcuts_row_activated_cb (GtkTreeView *tree_view,
7938 GtkTreeViewColumn *column,
7939 GtkFileChooserDefault *impl)
7942 GtkTreeIter child_iter;
7944 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
7947 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
7950 shortcuts_activate_iter (impl, &child_iter);
7952 gtk_widget_grab_focus (impl->browse_files_tree_view);
7955 /* Handler for GtkWidget::key-press-event on the shortcuts list */
7957 shortcuts_key_press_event_cb (GtkWidget *widget,
7959 GtkFileChooserDefault *impl)
7963 modifiers = gtk_accelerator_get_default_mod_mask ();
7965 if ((event->keyval == GDK_BackSpace
7966 || event->keyval == GDK_Delete
7967 || event->keyval == GDK_KP_Delete)
7968 && (event->state & modifiers) == 0)
7970 remove_selected_bookmarks (impl);
7974 if ((event->keyval == GDK_F2)
7975 && (event->state & modifiers) == 0)
7977 rename_selected_bookmark (impl);
7985 shortcuts_select_func (GtkTreeSelection *selection,
7986 GtkTreeModel *model,
7988 gboolean path_currently_selected,
7991 GtkFileChooserDefault *impl = data;
7993 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
7997 list_select_func (GtkTreeSelection *selection,
7998 GtkTreeModel *model,
8000 gboolean path_currently_selected,
8003 GtkFileChooserDefault *impl = data;
8005 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8006 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8008 GtkTreeIter iter, child_iter;
8009 const GtkFileInfo *info;
8011 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8014 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8016 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8018 if (info && !gtk_file_info_get_is_folder (info))
8026 list_selection_changed (GtkTreeSelection *selection,
8027 GtkFileChooserDefault *impl)
8029 /* See if we are in the new folder editable row for Save mode */
8030 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8032 const GtkFileInfo *info;
8033 gboolean had_selection;
8035 info = get_selected_file_info_from_file_list (impl, &had_selection);
8037 goto out; /* normal processing */
8040 return; /* We are on the editable row for New Folder */
8045 update_chooser_entry (impl);
8046 check_preview_change (impl);
8047 bookmarks_check_add_sensitivity (impl);
8049 g_signal_emit_by_name (impl, "selection-changed", 0);
8052 /* Callback used when a row in the file list is activated */
8054 list_row_activated (GtkTreeView *tree_view,
8056 GtkTreeViewColumn *column,
8057 GtkFileChooserDefault *impl)
8059 GtkTreeIter iter, child_iter;
8060 const GtkFileInfo *info;
8062 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8065 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8067 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8069 if (gtk_file_info_get_is_folder (info))
8071 const GtkFilePath *file_path;
8073 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8074 change_folder_and_display_error (impl, file_path);
8079 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8080 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8081 g_signal_emit_by_name (impl, "file-activated");
8085 path_bar_clicked (GtkPathBar *path_bar,
8086 GtkFilePath *file_path,
8087 GtkFilePath *child_path,
8088 gboolean child_is_hidden,
8089 GtkFileChooserDefault *impl)
8092 pending_select_paths_add (impl, child_path);
8094 if (!change_folder_and_display_error (impl, file_path))
8097 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
8098 * show hidden files so that ".baz" appears in the file list, as it will still
8099 * be shown in the path bar: "/foo/[bar]/.baz"
8101 if (child_is_hidden)
8102 g_object_set (impl, "show-hidden", TRUE, NULL);
8105 static const GtkFileInfo *
8106 get_list_file_info (GtkFileChooserDefault *impl,
8109 GtkTreeIter child_iter;
8111 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8115 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8119 list_icon_data_func (GtkTreeViewColumn *tree_column,
8120 GtkCellRenderer *cell,
8121 GtkTreeModel *tree_model,
8125 GtkFileChooserDefault *impl = data;
8126 GtkTreeIter child_iter;
8127 const GtkFilePath *path;
8129 const GtkFileInfo *info;
8130 gboolean sensitive = TRUE;
8132 profile_start ("start", NULL);
8134 info = get_list_file_info (impl, iter);
8136 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8139 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8147 /* FIXME: NULL GError */
8148 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl),
8149 impl->icon_size, NULL);
8154 /* We are on the editable row */
8158 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8159 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
8160 sensitive = gtk_file_info_get_is_folder (info);
8164 "sensitive", sensitive,
8168 g_object_unref (pixbuf);
8170 profile_end ("end", NULL);
8174 list_name_data_func (GtkTreeViewColumn *tree_column,
8175 GtkCellRenderer *cell,
8176 GtkTreeModel *tree_model,
8180 GtkFileChooserDefault *impl = data;
8181 const GtkFileInfo *info = get_list_file_info (impl, iter);
8182 gboolean sensitive = TRUE;
8187 "text", _("Type name of new folder"),
8194 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8195 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8197 sensitive = gtk_file_info_get_is_folder (info);
8201 "text", gtk_file_info_get_display_name (info),
8202 "sensitive", sensitive,
8208 list_size_data_func (GtkTreeViewColumn *tree_column,
8209 GtkCellRenderer *cell,
8210 GtkTreeModel *tree_model,
8214 GtkFileChooserDefault *impl = data;
8215 const GtkFileInfo *info = get_list_file_info (impl, iter);
8218 gboolean sensitive = TRUE;
8220 if (!info || gtk_file_info_get_is_folder (info))
8224 "sensitive", sensitive,
8229 size = gtk_file_info_get_size (info);
8231 if (size < (gint64)1024)
8232 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
8233 else if (size < (gint64)1024*1024)
8234 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
8235 else if (size < (gint64)1024*1024*1024)
8236 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
8238 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
8240 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
8241 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8242 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8247 "sensitive", sensitive,
8248 "alignment", PANGO_ALIGN_RIGHT,
8255 /* Tree column data callback for the file list; fetches the mtime of a file */
8257 list_mtime_data_func (GtkTreeViewColumn *tree_column,
8258 GtkCellRenderer *cell,
8259 GtkTreeModel *tree_model,
8263 GtkFileChooserDefault *impl;
8264 const GtkFileInfo *info;
8265 GtkFileTime time_mtime;
8269 gboolean sensitive = TRUE;
8273 info = get_list_file_info (impl, iter);
8283 time_mtime = gtk_file_info_get_modification_time (info);
8285 if (time_mtime == 0)
8286 strcpy (buf, _("Unknown"));
8290 g_date_set_time_t (&mtime, time_mtime);
8291 time_now = time (NULL);
8292 g_date_set_time_t (&now, time_now);
8294 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
8297 strcpy (buf, _("Today"));
8298 else if (days_diff == 1)
8299 strcpy (buf, _("Yesterday"));
8304 if (days_diff > 1 && days_diff < 7)
8305 format = "%A"; /* Days from last week */
8307 format = "%x"; /* Any other date */
8309 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
8310 strcpy (buf, _("Unknown"));
8314 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8315 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8316 sensitive = gtk_file_info_get_is_folder (info);
8320 "sensitive", sensitive,
8325 _gtk_file_chooser_default_new (const char *file_system)
8327 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
8328 "file-system-backend", file_system,
8333 location_popup_handler (GtkFileChooserDefault *impl,
8336 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8337 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8339 LocationMode new_mode;
8341 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8342 new_mode = LOCATION_MODE_FILENAME_ENTRY;
8343 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8344 new_mode = LOCATION_MODE_PATH_BAR;
8347 g_assert_not_reached ();
8351 location_mode_set (impl, new_mode, TRUE);
8353 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8354 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8355 gtk_widget_grab_focus (impl->location_entry);
8357 g_assert_not_reached ();
8360 /* Handler for the "up-folder" keybinding signal */
8362 up_folder_handler (GtkFileChooserDefault *impl)
8364 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
8367 /* Handler for the "down-folder" keybinding signal */
8369 down_folder_handler (GtkFileChooserDefault *impl)
8371 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
8374 /* Switches to the shortcut in the specified index */
8376 switch_to_shortcut (GtkFileChooserDefault *impl,
8381 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8382 g_assert_not_reached ();
8384 shortcuts_activate_iter (impl, &iter);
8387 /* Handler for the "home-folder" keybinding signal */
8389 home_folder_handler (GtkFileChooserDefault *impl)
8392 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
8395 /* Handler for the "desktop-folder" keybinding signal */
8397 desktop_folder_handler (GtkFileChooserDefault *impl)
8399 if (impl->has_desktop)
8400 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
8404 quick_bookmark_handler (GtkFileChooserDefault *impl,
8405 gint bookmark_index)
8410 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
8413 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
8415 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
8416 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
8419 gtk_tree_path_free (path);
8421 switch_to_shortcut (impl, bookmark_pos);
8426 /* Drag and drop interfaces */
8429 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
8434 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
8439 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
8441 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
8444 ShortcutsModelFilter *model;
8448 model = SHORTCUTS_MODEL_FILTER (drag_source);
8450 pos = *gtk_tree_path_get_indices (path);
8451 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
8453 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
8456 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
8458 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
8460 GtkSelectionData *selection_data)
8462 ShortcutsModelFilter *model;
8464 model = SHORTCUTS_MODEL_FILTER (drag_source);
8471 /* Fill the GtkTreeDragSourceIface vtable */
8473 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
8475 iface->row_draggable = shortcuts_model_filter_row_draggable;
8476 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
8480 /* Fill the GtkTreeDragDestIface vtable */
8482 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
8484 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
8485 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
8489 static GtkTreeModel *
8490 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
8491 GtkTreeModel *child_model,
8494 ShortcutsModelFilter *model;
8496 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
8497 "child-model", child_model,
8498 "virtual-root", root,
8503 return GTK_TREE_MODEL (model);
8506 #define __GTK_FILE_CHOOSER_DEFAULT_C__
8507 #include "gtkaliasdef.c"