1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "gdk/gdkkeysyms.h"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "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, "");
548 gtk_binding_entry_add_signal (binding_set,
551 1, G_TYPE_STRING, "/");
552 gtk_binding_entry_add_signal (binding_set,
555 1, G_TYPE_STRING, "/");
558 gtk_binding_entry_add_signal (binding_set,
561 1, G_TYPE_STRING, "~");
564 gtk_binding_entry_add_signal (binding_set,
565 GDK_v, GDK_CONTROL_MASK,
566 "location-popup-on-paste",
569 gtk_binding_entry_add_signal (binding_set,
570 GDK_Up, GDK_MOD1_MASK,
573 gtk_binding_entry_add_signal (binding_set,
577 gtk_binding_entry_add_signal (binding_set,
578 GDK_KP_Up, GDK_MOD1_MASK,
582 gtk_binding_entry_add_signal (binding_set,
583 GDK_Down, GDK_MOD1_MASK,
586 gtk_binding_entry_add_signal (binding_set,
587 GDK_KP_Down, GDK_MOD1_MASK,
591 gtk_binding_entry_add_signal (binding_set,
592 GDK_Home, GDK_MOD1_MASK,
595 gtk_binding_entry_add_signal (binding_set,
596 GDK_KP_Home, GDK_MOD1_MASK,
599 gtk_binding_entry_add_signal (binding_set,
600 GDK_d, GDK_MOD1_MASK,
604 for (i = 0; i < 10; i++)
605 gtk_binding_entry_add_signal (binding_set,
606 quick_bookmark_keyvals[i], GDK_MOD1_MASK,
610 _gtk_file_chooser_install_properties (gobject_class);
614 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
616 iface->select_path = gtk_file_chooser_default_select_path;
617 iface->unselect_path = gtk_file_chooser_default_unselect_path;
618 iface->select_all = gtk_file_chooser_default_select_all;
619 iface->unselect_all = gtk_file_chooser_default_unselect_all;
620 iface->get_paths = gtk_file_chooser_default_get_paths;
621 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
622 iface->get_file_system = gtk_file_chooser_default_get_file_system;
623 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
624 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
625 iface->set_current_name = gtk_file_chooser_default_set_current_name;
626 iface->add_filter = gtk_file_chooser_default_add_filter;
627 iface->remove_filter = gtk_file_chooser_default_remove_filter;
628 iface->list_filters = gtk_file_chooser_default_list_filters;
629 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
630 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
631 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
635 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
637 iface->get_default_size = gtk_file_chooser_default_get_default_size;
638 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
639 iface->should_respond = gtk_file_chooser_default_should_respond;
640 iface->initial_focus = gtk_file_chooser_default_initial_focus;
644 _gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
646 profile_start ("start", NULL);
647 #ifdef PROFILE_FILE_CHOOSER
648 access ("MARK: *** CREATE FILE CHOOSER", F_OK);
650 impl->local_only = TRUE;
651 impl->preview_widget_active = TRUE;
652 impl->use_preview_label = TRUE;
653 impl->select_multiple = FALSE;
654 impl->show_hidden = FALSE;
655 impl->icon_size = FALLBACK_ICON_SIZE;
656 impl->load_state = LOAD_EMPTY;
657 impl->reload_state = RELOAD_EMPTY;
658 impl->pending_select_paths = NULL;
659 impl->location_mode = LOCATION_MODE_PATH_BAR;
661 gtk_box_set_spacing (GTK_BOX (impl), 12);
663 impl->tooltips = gtk_tooltips_new ();
664 g_object_ref_sink (impl->tooltips);
666 profile_end ("end", NULL);
669 /* Frees the data columns for the specified iter in the shortcuts model*/
671 shortcuts_free_row_data (GtkFileChooserDefault *impl,
676 GtkFileSystemHandle *handle;
678 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
679 SHORTCUTS_COL_DATA, &col_data,
680 SHORTCUTS_COL_IS_VOLUME, &is_volume,
681 SHORTCUTS_COL_HANDLE, &handle,
685 gtk_file_system_cancel_operation (handle);
692 GtkFileSystemVolume *volume;
695 gtk_file_system_volume_free (impl->file_system, volume);
702 gtk_file_path_free (path);
706 /* Frees all the data columns in the shortcuts model */
708 shortcuts_free (GtkFileChooserDefault *impl)
712 if (!impl->shortcuts_model)
715 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
718 shortcuts_free_row_data (impl, &iter);
720 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
722 g_object_unref (impl->shortcuts_model);
723 impl->shortcuts_model = NULL;
727 pending_select_paths_free (GtkFileChooserDefault *impl)
731 for (l = impl->pending_select_paths; l; l = l->next)
736 gtk_file_path_free (path);
739 g_slist_free (impl->pending_select_paths);
740 impl->pending_select_paths = NULL;
744 pending_select_paths_add (GtkFileChooserDefault *impl,
745 const GtkFilePath *path)
747 impl->pending_select_paths = g_slist_prepend (impl->pending_select_paths, gtk_file_path_copy (path));
750 /* Used from gtk_tree_selection_selected_foreach() */
752 store_selection_foreach (GtkTreeModel *model,
757 GtkFileChooserDefault *impl;
758 GtkTreeIter child_iter;
759 const GtkFilePath *file_path;
761 impl = GTK_FILE_CHOOSER_DEFAULT (data);
763 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
765 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
766 pending_select_paths_add (impl, file_path);
769 /* Stores the current selection in the list of paths to select; this is used to
770 * preserve the selection when reloading the current folder.
773 pending_select_paths_store_selection (GtkFileChooserDefault *impl)
775 GtkTreeSelection *selection;
777 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
778 gtk_tree_selection_selected_foreach (selection, store_selection_foreach, impl);
782 gtk_file_chooser_default_finalize (GObject *object)
784 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
787 if (impl->shortcuts_filter_model)
788 g_object_unref (impl->shortcuts_filter_model);
790 shortcuts_free (impl);
792 g_object_unref (impl->file_system);
794 g_free (impl->browse_files_last_selected_name);
796 for (l = impl->filters; l; l = l->next)
798 GtkFileFilter *filter;
800 filter = GTK_FILE_FILTER (l->data);
801 g_object_unref (filter);
803 g_slist_free (impl->filters);
805 if (impl->current_filter)
806 g_object_unref (impl->current_filter);
808 if (impl->current_volume_path)
809 gtk_file_path_free (impl->current_volume_path);
811 if (impl->current_folder)
812 gtk_file_path_free (impl->current_folder);
814 if (impl->preview_path)
815 gtk_file_path_free (impl->preview_path);
817 load_remove_timer (impl);
819 /* Free all the Models we have */
820 if (impl->browse_files_model)
821 g_object_unref (impl->browse_files_model);
823 if (impl->sort_model)
824 g_object_unref (impl->sort_model);
826 g_free (impl->preview_display_name);
828 g_free (impl->edited_new_text);
830 g_object_unref (impl->tooltips);
832 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->finalize (object);
835 /* Shows an error dialog set as transient for the specified window */
837 error_message_with_parent (GtkWindow *parent,
843 dialog = gtk_message_dialog_new (parent,
844 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
849 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
853 gtk_window_group_add_window (parent->group, GTK_WINDOW (dialog));
855 gtk_dialog_run (GTK_DIALOG (dialog));
856 gtk_widget_destroy (dialog);
859 /* Returns a toplevel GtkWindow, or NULL if none */
861 get_toplevel (GtkWidget *widget)
865 toplevel = gtk_widget_get_toplevel (widget);
866 if (!GTK_WIDGET_TOPLEVEL (toplevel))
869 return GTK_WINDOW (toplevel);
872 /* Shows an error dialog for the file chooser */
874 error_message (GtkFileChooserDefault *impl,
878 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg, detail);
881 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
883 error_dialog (GtkFileChooserDefault *impl,
885 const GtkFilePath *path,
894 uri = gtk_file_system_path_to_uri (impl->file_system, path);
895 text = g_strdup_printf (msg, uri);
896 error_message (impl, text, error->message);
899 g_error_free (error);
903 /* Displays an error message about not being able to get information for a file.
904 * Frees the GError as well.
907 error_getting_info_dialog (GtkFileChooserDefault *impl,
908 const GtkFilePath *path,
912 _("Could not retrieve information about the file"),
916 /* Shows an error dialog about not being able to add a bookmark */
918 error_adding_bookmark_dialog (GtkFileChooserDefault *impl,
919 const GtkFilePath *path,
923 _("Could not add a bookmark"),
927 /* Shows an error dialog about not being able to remove a bookmark */
929 error_removing_bookmark_dialog (GtkFileChooserDefault *impl,
930 const GtkFilePath *path,
934 _("Could not remove bookmark"),
938 /* Shows an error dialog about not being able to create a folder */
940 error_creating_folder_dialog (GtkFileChooserDefault *impl,
941 const GtkFilePath *path,
945 _("The folder could not be created"),
949 /* Shows an error about not being able to create a folder because a file with
950 * the same name is already there.
953 error_creating_folder_over_existing_file_dialog (GtkFileChooserDefault *impl,
954 const GtkFilePath *path,
958 _("The folder could not be created, as a file with the same name "
959 "already exists. Try using a different name for the folder, "
960 "or rename the file first."),
964 /* Shows an error dialog about not being able to create a filename */
966 error_building_filename_dialog (GtkFileChooserDefault *impl,
967 const GtkFilePath *folder_part,
968 const char *file_part,
971 error_dialog (impl, _("Invalid file name"),
975 /* Shows an error dialog when we cannot switch to a folder */
977 error_changing_folder_dialog (GtkFileChooserDefault *impl,
978 const GtkFilePath *path,
981 error_dialog (impl, _("The folder contents could not be displayed"),
985 /* Changes folders, displaying an error dialog if this fails */
987 change_folder_and_display_error (GtkFileChooserDefault *impl,
988 const GtkFilePath *path)
992 GtkFilePath *path_copy;
994 g_return_val_if_fail (path != NULL, FALSE);
996 profile_start ("start", (char *) path);
998 /* We copy the path because of this case:
1000 * list_row_activated()
1001 * fetches path from model; path belongs to the model (*)
1002 * calls change_folder_and_display_error()
1003 * calls _gtk_file_chooser_set_current_folder_path()
1004 * changing folders fails, sets model to NULL, thus freeing the path in (*)
1007 path_copy = gtk_file_path_copy (path);
1010 result = gtk_file_chooser_default_update_current_folder (GTK_FILE_CHOOSER (impl), path_copy, TRUE, &error);
1013 error_changing_folder_dialog (impl, path_copy, error);
1015 gtk_file_path_free (path_copy);
1017 profile_end ("end", (char *) path);
1023 update_preview_widget_visibility (GtkFileChooserDefault *impl)
1025 if (impl->use_preview_label)
1027 if (!impl->preview_label)
1029 impl->preview_label = gtk_label_new (impl->preview_display_name);
1030 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
1031 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
1032 gtk_label_set_ellipsize (GTK_LABEL (impl->preview_label), PANGO_ELLIPSIZE_MIDDLE);
1033 gtk_widget_show (impl->preview_label);
1038 if (impl->preview_label)
1040 gtk_widget_destroy (impl->preview_label);
1041 impl->preview_label = NULL;
1045 if (impl->preview_widget_active && impl->preview_widget)
1046 gtk_widget_show (impl->preview_box);
1048 gtk_widget_hide (impl->preview_box);
1050 g_signal_emit_by_name (impl, "default-size-changed");
1054 set_preview_widget (GtkFileChooserDefault *impl,
1055 GtkWidget *preview_widget)
1057 if (preview_widget == impl->preview_widget)
1060 if (impl->preview_widget)
1061 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
1062 impl->preview_widget);
1064 impl->preview_widget = preview_widget;
1065 if (impl->preview_widget)
1067 gtk_widget_show (impl->preview_widget);
1068 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
1069 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
1070 impl->preview_widget,
1071 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
1074 update_preview_widget_visibility (impl);
1077 /* Re-reads all the icons for the shortcuts, used when the theme changes */
1078 struct ReloadIconsData
1080 GtkFileChooserDefault *impl;
1081 GtkTreeRowReference *row_ref;
1085 shortcuts_reload_icons_get_info_cb (GtkFileSystemHandle *handle,
1086 const GtkFileInfo *info,
1087 const GError *error,
1093 gboolean cancelled = handle->cancelled;
1094 struct ReloadIconsData *data = user_data;
1096 if (!g_slist_find (data->impl->reload_icon_handles, handle))
1099 data->impl->reload_icon_handles = g_slist_remove (data->impl->reload_icon_handles, handle);
1101 if (cancelled || error)
1104 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->impl),
1105 data->impl->icon_size, NULL);
1107 path = gtk_tree_row_reference_get_path (data->row_ref);
1108 gtk_tree_model_get_iter (GTK_TREE_MODEL (data->impl->shortcuts_model), &iter, path);
1109 gtk_list_store_set (data->impl->shortcuts_model, &iter,
1110 SHORTCUTS_COL_PIXBUF, pixbuf,
1112 gtk_tree_path_free (path);
1115 g_object_unref (pixbuf);
1118 gtk_tree_row_reference_free (data->row_ref);
1119 g_object_unref (data->impl);
1122 g_object_unref (handle);
1126 shortcuts_reload_icons (GtkFileChooserDefault *impl)
1131 profile_start ("start", NULL);
1133 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1136 for (l = impl->reload_icon_handles; l; l = l->next)
1138 GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1139 gtk_file_system_cancel_operation (handle);
1141 g_slist_free (impl->reload_icon_handles);
1142 impl->reload_icon_handles = NULL;
1148 gboolean pixbuf_visible;
1151 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1152 SHORTCUTS_COL_DATA, &data,
1153 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1154 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
1157 if (pixbuf_visible && data)
1161 GtkFileSystemVolume *volume;
1164 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1165 impl->icon_size, NULL);
1167 gtk_list_store_set (impl->shortcuts_model, &iter,
1168 SHORTCUTS_COL_PIXBUF, pixbuf,
1172 g_object_unref (pixbuf);
1174 else if (gtk_file_system_path_is_local (impl->file_system, (GtkFilePath *)data))
1176 const GtkFilePath *path;
1177 struct ReloadIconsData *info;
1178 GtkTreePath *tree_path;
1179 GtkFileSystemHandle *handle;
1183 info = g_new0 (struct ReloadIconsData, 1);
1184 info->impl = g_object_ref (impl);
1185 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1186 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), tree_path);
1187 gtk_tree_path_free (tree_path);
1189 handle = gtk_file_system_get_info (impl->file_system, path,
1191 shortcuts_reload_icons_get_info_cb,
1193 impl->reload_icon_handles = g_slist_append (impl->reload_icon_handles, handle);
1197 GtkIconTheme *icon_theme;
1199 /* Don't call get_info for remote paths to avoid latency and
1201 * If we switch to a better bookmarks file format (XBEL), we
1202 * should use mime info to get a better icon.
1204 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1205 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory",
1206 impl->icon_size, 0, NULL);
1208 gtk_list_store_set (impl->shortcuts_model, &iter,
1209 SHORTCUTS_COL_PIXBUF, pixbuf,
1213 g_object_unref (pixbuf);
1217 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
1221 profile_end ("end", NULL);
1225 shortcuts_find_folder (GtkFileChooserDefault *impl,
1226 GtkFilePath *folder)
1228 GtkTreeSelection *selection;
1232 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1234 g_assert (folder != NULL);
1235 pos = shortcut_find_position (impl, folder);
1238 gtk_tree_selection_unselect_all (selection);
1242 path = gtk_tree_path_new_from_indices (pos, -1);
1243 gtk_tree_selection_select_path (selection, path);
1244 gtk_tree_path_free (path);
1247 /* If a shortcut corresponds to the current folder, selects it */
1249 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
1251 shortcuts_find_folder (impl, impl->current_folder);
1254 /* Removes the specified number of rows from the shortcuts list */
1256 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1262 path = gtk_tree_path_new_from_indices (start_row, -1);
1264 for (; n_rows; n_rows--)
1268 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1269 g_assert_not_reached ();
1271 shortcuts_free_row_data (impl, &iter);
1272 gtk_list_store_remove (impl->shortcuts_model, &iter);
1275 gtk_tree_path_free (path);
1279 shortcuts_update_count (GtkFileChooserDefault *impl,
1280 ShortcutsIndex type,
1285 case SHORTCUTS_HOME:
1287 impl->has_home = FALSE;
1289 impl->has_home = TRUE;
1292 case SHORTCUTS_DESKTOP:
1294 impl->has_desktop = FALSE;
1296 impl->has_desktop = TRUE;
1299 case SHORTCUTS_VOLUMES:
1300 impl->num_volumes += value;
1303 case SHORTCUTS_SHORTCUTS:
1304 impl->num_shortcuts += value;
1307 case SHORTCUTS_BOOKMARKS:
1308 impl->num_bookmarks += value;
1311 case SHORTCUTS_CURRENT_FOLDER:
1313 impl->shortcuts_current_folder_active = FALSE;
1315 impl->shortcuts_current_folder_active = TRUE;
1324 struct ShortcutsInsertRequest
1326 GtkFileChooserDefault *impl;
1327 GtkFilePath *parent_path;
1331 GtkTreeRowReference *row_ref;
1332 ShortcutsIndex type;
1338 get_file_info_finished (GtkFileSystemHandle *handle,
1339 const GtkFileInfo *info,
1340 const GError *error,
1344 gboolean cancelled = handle->cancelled;
1345 gboolean is_volume = FALSE;
1349 GtkFileSystemHandle *model_handle;
1350 struct ShortcutsInsertRequest *request = data;
1352 path = gtk_tree_row_reference_get_path (request->row_ref);
1354 /* Handle doesn't exist anymore in the model */
1357 pos = gtk_tree_path_get_indices (path)[0];
1358 gtk_tree_model_get_iter (GTK_TREE_MODEL (request->impl->shortcuts_model),
1360 gtk_tree_path_free (path);
1362 /* validate handle, else goto out */
1363 gtk_tree_model_get (GTK_TREE_MODEL (request->impl->shortcuts_model), &iter,
1364 SHORTCUTS_COL_HANDLE, &model_handle,
1366 if (handle != model_handle)
1369 /* set the handle to NULL in the model (we unref later on) */
1370 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1371 SHORTCUTS_COL_HANDLE, NULL,
1379 gtk_list_store_remove (request->impl->shortcuts_model, &iter);
1380 shortcuts_update_count (request->impl, request->type, -1);
1382 if (request->type == SHORTCUTS_HOME)
1384 const char *home = g_get_home_dir ();
1385 GtkFilePath *home_path;
1387 home_path = gtk_file_system_filename_to_path (request->impl->file_system, home);
1388 error_getting_info_dialog (request->impl, home_path, g_error_copy (error));
1389 gtk_file_path_free (home_path);
1391 else if (request->type == SHORTCUTS_CURRENT_FOLDER)
1393 /* Remove the current folder separator */
1394 gint separator_pos = shortcuts_get_index (request->impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1395 shortcuts_remove_rows (request->impl, separator_pos, 1);
1401 if (!request->label_copy)
1402 request->label_copy = g_strdup (gtk_file_info_get_display_name (info));
1403 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (request->impl),
1404 request->impl->icon_size, NULL);
1406 gtk_list_store_set (request->impl->shortcuts_model, &iter,
1407 SHORTCUTS_COL_PIXBUF, pixbuf,
1408 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1409 SHORTCUTS_COL_NAME, request->label_copy,
1410 SHORTCUTS_COL_IS_VOLUME, is_volume,
1411 SHORTCUTS_COL_REMOVABLE, request->removable,
1414 if (request->impl->shortcuts_filter_model)
1415 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (request->impl->shortcuts_filter_model));
1417 if (request->type == SHORTCUTS_CURRENT_FOLDER
1418 && request->impl->save_folder_combo != NULL)
1420 /* The current folder is updated via _activate_iter(), don't
1421 * have save_folder_combo_changed_cb() call _activate_iter()
1424 g_signal_handlers_block_by_func (request->impl->save_folder_combo,
1425 G_CALLBACK (save_folder_combo_changed_cb),
1427 gtk_combo_box_set_active (GTK_COMBO_BOX (request->impl->save_folder_combo), pos);
1428 g_signal_handlers_unblock_by_func (request->impl->save_folder_combo,
1429 G_CALLBACK (save_folder_combo_changed_cb),
1434 g_object_unref (pixbuf);
1437 g_object_unref (request->impl);
1438 gtk_file_path_free (request->parent_path);
1439 gtk_file_path_free (request->path);
1440 gtk_tree_row_reference_free (request->row_ref);
1441 g_free (request->label_copy);
1444 g_object_unref (handle);
1447 /* FIXME: GtkFileSystem needs a function to split a remote path
1448 * into hostname and path components, or maybe just have a
1449 * gtk_file_system_path_get_display_name().
1451 * This function is also used in gtkfilechooserbutton.c
1454 _gtk_file_chooser_label_for_uri (const gchar *uri)
1456 const gchar *path, *start, *end, *p;
1457 gchar *host, *label;
1459 start = strstr (uri, "://");
1461 path = strchr (start, '/');
1467 end = uri + strlen (uri);
1471 /* strip username */
1472 p = strchr (start, '@');
1478 p = strchr (start, ':');
1482 host = g_strndup (start, end - start);
1484 /* Translators: the first string is a path and the second string
1485 * is a hostname. Nautilus and the panel contain the same string
1488 label = g_strdup_printf (_("%1$s on %2$s"), path, host);
1495 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1496 * inserts a volume. A position of -1 indicates the end of the tree.
1499 shortcuts_insert_path (GtkFileChooserDefault *impl,
1502 GtkFileSystemVolume *volume,
1503 const GtkFilePath *path,
1506 ShortcutsIndex type)
1509 GdkPixbuf *pixbuf = NULL;
1510 gpointer data = NULL;
1512 GtkIconTheme *icon_theme;
1514 profile_start ("start", is_volume ? "volume" : (char *) path);
1519 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1520 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1521 impl->icon_size, NULL);
1523 else if (gtk_file_system_path_is_local (impl->file_system, path))
1525 struct ShortcutsInsertRequest *request;
1526 GtkFileSystemHandle *handle;
1529 request = g_new0 (struct ShortcutsInsertRequest, 1);
1530 request->impl = g_object_ref (impl);
1531 request->path = gtk_file_path_copy (path);
1532 request->name_only = TRUE;
1533 request->removable = removable;
1535 request->type = type;
1537 request->label_copy = g_strdup (label);
1540 gtk_list_store_append (impl->shortcuts_model, &iter);
1542 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1544 p = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1545 request->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (impl->shortcuts_model), p);
1546 gtk_tree_path_free (p);
1548 handle = gtk_file_system_get_info (request->impl->file_system, request->path,
1549 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN | GTK_FILE_INFO_ICON,
1550 get_file_info_finished, request);
1552 gtk_list_store_set (impl->shortcuts_model, &iter,
1553 SHORTCUTS_COL_DATA, gtk_file_path_copy (path),
1554 SHORTCUTS_COL_IS_VOLUME, is_volume,
1555 SHORTCUTS_COL_HANDLE, handle,
1558 shortcuts_update_count (impl, type, 1);
1564 /* Don't call get_info for remote paths to avoid latency and
1567 data = gtk_file_path_copy (path);
1569 label_copy = g_strdup (label);
1574 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1576 label_copy = _gtk_file_chooser_label_for_uri (uri);
1581 /* If we switch to a better bookmarks file format (XBEL), we
1582 * should use mime info to get a better icon.
1584 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
1585 pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory",
1586 impl->icon_size, 0, NULL);
1590 gtk_list_store_append (impl->shortcuts_model, &iter);
1592 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1594 shortcuts_update_count (impl, type, 1);
1596 gtk_list_store_set (impl->shortcuts_model, &iter,
1597 SHORTCUTS_COL_PIXBUF, pixbuf,
1598 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1599 SHORTCUTS_COL_NAME, label_copy,
1600 SHORTCUTS_COL_DATA, data,
1601 SHORTCUTS_COL_IS_VOLUME, is_volume,
1602 SHORTCUTS_COL_REMOVABLE, removable,
1603 SHORTCUTS_COL_HANDLE, NULL,
1606 if (impl->shortcuts_filter_model)
1607 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1609 if (type == SHORTCUTS_CURRENT_FOLDER && impl->save_folder_combo != NULL)
1611 /* The current folder is updated via _activate_iter(), don't
1612 * have save_folder_combo_changed_cb() call _activate_iter()
1615 gint combo_pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1616 g_signal_handlers_block_by_func (impl->save_folder_combo,
1617 G_CALLBACK (save_folder_combo_changed_cb),
1619 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), combo_pos);
1620 g_signal_handlers_unblock_by_func (impl->save_folder_combo,
1621 G_CALLBACK (save_folder_combo_changed_cb),
1625 g_free (label_copy);
1628 g_object_unref (pixbuf);
1630 profile_end ("end", NULL);
1633 /* Appends an item for the user's home directory to the shortcuts model */
1635 shortcuts_append_home (GtkFileChooserDefault *impl)
1638 GtkFilePath *home_path;
1640 profile_start ("start", NULL);
1642 home = g_get_home_dir ();
1645 profile_end ("end - no home directory!?", NULL);
1649 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1651 shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, NULL, FALSE, SHORTCUTS_HOME);
1653 gtk_file_path_free (home_path);
1655 profile_end ("end", NULL);
1658 /* Appends the ~/Desktop directory to the shortcuts model */
1660 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1666 profile_start ("start", NULL);
1669 name = _gtk_file_system_win32_get_desktop ();
1671 home = g_get_home_dir ();
1674 profile_end ("end - no home directory!?", NULL);
1678 name = g_build_filename (home, "Desktop", NULL);
1681 path = gtk_file_system_filename_to_path (impl->file_system, name);
1684 shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, SHORTCUTS_DESKTOP);
1685 /* We do not actually pop up an error dialog if there is no desktop directory
1686 * because some people may really not want to have one.
1689 gtk_file_path_free (path);
1691 profile_end ("end", NULL);
1694 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1696 shortcuts_append_paths (GtkFileChooserDefault *impl,
1703 profile_start ("start", NULL);
1705 /* As there is no separator now, we want to start there.
1707 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1710 for (; paths; paths = paths->next)
1716 if (impl->local_only &&
1717 !gtk_file_system_path_is_local (impl->file_system, path))
1720 label = gtk_file_system_get_bookmark_label (impl->file_system, path);
1722 /* NULL GError, but we don't really want to show error boxes here */
1723 shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, label, TRUE, SHORTCUTS_BOOKMARKS);
1729 profile_end ("end", NULL);
1731 return num_inserted;
1734 /* Returns the index for the corresponding item in the shortcuts bar */
1736 shortcuts_get_index (GtkFileChooserDefault *impl,
1737 ShortcutsIndex where)
1743 if (where == SHORTCUTS_HOME)
1746 n += impl->has_home ? 1 : 0;
1748 if (where == SHORTCUTS_DESKTOP)
1751 n += impl->has_desktop ? 1 : 0;
1753 if (where == SHORTCUTS_VOLUMES)
1756 n += impl->num_volumes;
1758 if (where == SHORTCUTS_SHORTCUTS)
1761 n += impl->num_shortcuts;
1763 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1766 /* If there are no bookmarks there won't be a separator */
1767 n += (impl->num_bookmarks > 0) ? 1 : 0;
1769 if (where == SHORTCUTS_BOOKMARKS)
1772 n += impl->num_bookmarks;
1774 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1779 if (where == SHORTCUTS_CURRENT_FOLDER)
1782 g_assert_not_reached ();
1789 /* Adds all the file system volumes to the shortcuts model */
1791 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1796 gboolean old_changing_folders;
1798 profile_start ("start", NULL);
1801 old_changing_folders = impl->changing_folder;
1802 impl->changing_folder = TRUE;
1804 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1805 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1806 impl->num_volumes = 0;
1808 list = gtk_file_system_list_volumes (impl->file_system);
1812 for (l = list; l; l = l->next)
1814 GtkFileSystemVolume *volume;
1818 if (impl->local_only)
1820 if (gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
1822 GtkFilePath *base_path;
1824 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1825 if (base_path != NULL)
1827 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1828 gtk_file_path_free (base_path);
1832 gtk_file_system_volume_free (impl->file_system, volume);
1839 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_VOLUMES);
1843 impl->num_volumes = n;
1844 g_slist_free (list);
1846 if (impl->shortcuts_filter_model)
1847 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1849 impl->changing_folder = old_changing_folders;
1851 profile_end ("end", NULL);
1854 /* Inserts a separator node in the shortcuts list */
1856 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1857 ShortcutsIndex where)
1861 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1863 gtk_list_store_insert (impl->shortcuts_model, &iter,
1864 shortcuts_get_index (impl, where));
1865 gtk_list_store_set (impl->shortcuts_model, &iter,
1866 SHORTCUTS_COL_PIXBUF, NULL,
1867 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1868 SHORTCUTS_COL_NAME, NULL,
1869 SHORTCUTS_COL_DATA, NULL,
1873 /* Updates the list of bookmarks */
1875 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1878 gboolean old_changing_folders;
1880 GtkFilePath *list_selected = NULL;
1881 GtkFilePath *combo_selected = NULL;
1885 profile_start ("start", NULL);
1887 old_changing_folders = impl->changing_folder;
1888 impl->changing_folder = TRUE;
1890 if (shortcuts_get_selected (impl, &iter))
1892 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1894 SHORTCUTS_COL_DATA, &col_data,
1895 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1898 if (col_data && !is_volume)
1899 list_selected = gtk_file_path_copy (col_data);
1902 if (impl->save_folder_combo &&
1903 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (impl->save_folder_combo),
1906 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model),
1908 SHORTCUTS_COL_DATA, &col_data,
1909 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1912 if (col_data && !is_volume)
1913 combo_selected = gtk_file_path_copy (col_data);
1916 if (impl->num_bookmarks > 0)
1917 shortcuts_remove_rows (impl,
1918 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1919 impl->num_bookmarks + 1);
1921 impl->num_bookmarks = 0;
1923 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1924 shortcuts_append_paths (impl, bookmarks);
1925 gtk_file_paths_free (bookmarks);
1927 if (impl->num_bookmarks > 0)
1928 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1930 if (impl->shortcuts_filter_model)
1931 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1935 shortcuts_find_folder (impl, list_selected);
1936 gtk_file_path_free (list_selected);
1943 pos = shortcut_find_position (impl, combo_selected);
1945 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo),
1947 gtk_file_path_free (combo_selected);
1950 impl->changing_folder = old_changing_folders;
1952 profile_end ("end", NULL);
1955 /* Appends a separator and a row to the shortcuts list for the current folder */
1957 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1962 g_assert (!impl->shortcuts_current_folder_active);
1966 g_assert (impl->current_folder != NULL);
1968 pos = shortcut_find_position (impl, impl->current_folder);
1971 GtkFileSystemVolume *volume;
1972 GtkFilePath *base_path;
1976 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1980 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1982 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1984 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1989 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1991 shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
1995 shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
1997 gtk_file_system_volume_free (impl->file_system, volume);
2001 gtk_file_path_free (base_path);
2003 else if (impl->save_folder_combo != NULL)
2004 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
2007 /* Updates the current folder row in the shortcuts model */
2009 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
2013 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2015 if (impl->shortcuts_current_folder_active)
2017 shortcuts_remove_rows (impl, pos, 2);
2018 impl->shortcuts_current_folder_active = FALSE;
2021 shortcuts_add_current_folder (impl);
2024 /* Filter function used for the shortcuts filter model */
2026 shortcuts_filter_cb (GtkTreeModel *model,
2030 GtkFileChooserDefault *impl;
2034 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2036 path = gtk_tree_model_get_path (model, iter);
2040 pos = *gtk_tree_path_get_indices (path);
2041 gtk_tree_path_free (path);
2043 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
2046 /* Creates the list model for shortcuts */
2048 shortcuts_model_create (GtkFileChooserDefault *impl)
2050 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
2051 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
2052 GDK_TYPE_PIXBUF, /* pixbuf */
2053 G_TYPE_STRING, /* name */
2054 G_TYPE_POINTER, /* path or volume */
2055 G_TYPE_BOOLEAN, /* is the previous column a volume? */
2056 G_TYPE_BOOLEAN, /* removable */
2057 G_TYPE_BOOLEAN, /* pixbuf cell visibility */
2058 G_TYPE_POINTER); /* GtkFileSystemHandle */
2060 if (impl->file_system)
2062 shortcuts_append_home (impl);
2063 shortcuts_append_desktop (impl);
2064 shortcuts_add_volumes (impl);
2067 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
2068 GTK_TREE_MODEL (impl->shortcuts_model),
2071 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2072 shortcuts_filter_cb,
2077 /* Callback used when the "New Folder" button is clicked */
2079 new_folder_button_clicked (GtkButton *button,
2080 GtkFileChooserDefault *impl)
2085 if (!impl->browse_files_model)
2086 return; /* FIXME: this sucks. Disable the New Folder button or something. */
2088 /* Prevent button from being clicked twice */
2089 gtk_widget_set_sensitive (impl->browse_new_folder_button, FALSE);
2091 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
2093 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
2094 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
2095 path, impl->list_name_column,
2098 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
2099 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
2101 impl->list_name_column,
2104 gtk_tree_path_free (path);
2108 edited_idle_create_folder_cb (GtkFileSystemHandle *handle,
2109 const GtkFilePath *path,
2110 const GError *error,
2113 gboolean cancelled = handle->cancelled;
2114 GtkFileChooserDefault *impl = data;
2116 if (!g_slist_find (impl->pending_handles, handle))
2119 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
2125 change_folder_and_display_error (impl, path);
2127 error_creating_folder_dialog (impl, path, g_error_copy (error));
2130 g_object_unref (impl);
2131 g_object_unref (handle);
2134 /* Idle handler for creating a new folder after editing its name cell, or for
2135 * canceling the editing.
2138 edited_idle_cb (GtkFileChooserDefault *impl)
2140 GDK_THREADS_ENTER ();
2142 g_source_destroy (impl->edited_idle);
2143 impl->edited_idle = NULL;
2145 _gtk_file_system_model_remove_editable (impl->browse_files_model);
2146 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
2148 gtk_widget_set_sensitive (impl->browse_new_folder_button, TRUE);
2150 if (impl->edited_new_text) /* not cancelled? */
2153 GtkFilePath *file_path;
2156 file_path = gtk_file_system_make_path (impl->file_system,
2157 impl->current_folder,
2158 impl->edited_new_text,
2162 GtkFileSystemHandle *handle;
2164 handle = gtk_file_system_create_folder (impl->file_system, file_path,
2165 edited_idle_create_folder_cb,
2166 g_object_ref (impl));
2167 impl->pending_handles = g_slist_append (impl->pending_handles, handle);
2169 gtk_file_path_free (file_path);
2172 error_creating_folder_dialog (impl, file_path, error);
2174 g_free (impl->edited_new_text);
2175 impl->edited_new_text = NULL;
2178 GDK_THREADS_LEAVE ();
2184 queue_edited_idle (GtkFileChooserDefault *impl,
2185 const gchar *new_text)
2187 /* We create the folder in an idle handler so that we don't modify the tree
2191 if (!impl->edited_idle)
2193 impl->edited_idle = g_idle_source_new ();
2194 g_source_set_closure (impl->edited_idle,
2195 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
2197 g_source_attach (impl->edited_idle, NULL);
2200 g_free (impl->edited_new_text);
2201 impl->edited_new_text = g_strdup (new_text);
2204 /* Callback used from the text cell renderer when the new folder is named */
2206 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
2208 const gchar *new_text,
2209 GtkFileChooserDefault *impl)
2211 /* work around bug #154921 */
2212 g_object_set (cell_renderer_text,
2213 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2214 queue_edited_idle (impl, new_text);
2217 /* Callback used from the text cell renderer when the new folder edition gets
2221 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
2222 GtkFileChooserDefault *impl)
2224 /* work around bug #154921 */
2225 g_object_set (cell_renderer_text,
2226 "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
2227 queue_edited_idle (impl, NULL);
2230 /* Creates the widgets for the filter combo box */
2232 filter_create (GtkFileChooserDefault *impl)
2234 impl->filter_combo = gtk_combo_box_new_text ();
2235 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (impl->filter_combo), FALSE);
2237 g_signal_connect (impl->filter_combo, "changed",
2238 G_CALLBACK (filter_combo_changed), impl);
2240 return impl->filter_combo;
2244 button_new (GtkFileChooserDefault *impl,
2246 const char *stock_id,
2254 button = gtk_button_new_with_mnemonic (text);
2255 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
2256 gtk_button_set_image (GTK_BUTTON (button), image);
2258 gtk_widget_set_sensitive (button, sensitive);
2259 g_signal_connect (button, "clicked", callback, impl);
2262 gtk_widget_show (button);
2267 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
2269 shortcut_find_position (GtkFileChooserDefault *impl,
2270 const GtkFilePath *path)
2274 int current_folder_separator_idx;
2276 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2279 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
2282 /* FIXME: is this still needed? */
2283 if (current_folder_separator_idx >= impl->shortcuts_model->length)
2287 for (i = 0; i < current_folder_separator_idx; i++)
2292 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2293 SHORTCUTS_COL_DATA, &col_data,
2294 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2301 GtkFileSystemVolume *volume;
2302 GtkFilePath *base_path;
2306 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
2308 exists = base_path && strcmp (gtk_file_path_get_string (path),
2309 gtk_file_path_get_string (base_path)) == 0;
2317 GtkFilePath *model_path;
2319 model_path = col_data;
2321 if (model_path && gtk_file_path_compare (model_path, path) == 0)
2326 if (i < current_folder_separator_idx - 1)
2328 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
2329 g_assert_not_reached ();
2336 /* Tries to add a bookmark from a path name */
2338 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
2339 const GtkFilePath *path,
2344 g_return_val_if_fail (path != NULL, FALSE);
2346 if (shortcut_find_position (impl, path) != -1)
2350 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
2352 error_adding_bookmark_dialog (impl, path, error);
2360 add_bookmark_foreach_cb (GtkTreeModel *model,
2365 GtkFileChooserDefault *impl;
2366 GtkFileSystemModel *fs_model;
2367 GtkTreeIter child_iter;
2368 const GtkFilePath *file_path;
2370 impl = (GtkFileChooserDefault *) data;
2372 fs_model = impl->browse_files_model;
2373 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
2375 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
2376 shortcuts_add_bookmark_from_path (impl, file_path, -1);
2379 /* Adds a bookmark from the currently selected item in the file list */
2381 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
2383 GtkTreeSelection *selection;
2385 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2387 if (gtk_tree_selection_count_selected_rows (selection) == 0)
2388 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
2390 gtk_tree_selection_selected_foreach (selection,
2391 add_bookmark_foreach_cb,
2395 /* Callback used when the "Add bookmark" button is clicked */
2397 add_bookmark_button_clicked_cb (GtkButton *button,
2398 GtkFileChooserDefault *impl)
2400 bookmarks_add_selected_folder (impl);
2403 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
2404 * returns FALSE if no shortcut is selected.
2407 shortcuts_get_selected (GtkFileChooserDefault *impl,
2410 GtkTreeSelection *selection;
2411 GtkTreeIter parent_iter;
2413 if (!impl->browse_shortcuts_tree_view)
2416 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2418 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
2421 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2427 /* Removes the selected bookmarks */
2429 remove_selected_bookmarks (GtkFileChooserDefault *impl)
2437 if (!shortcuts_get_selected (impl, &iter))
2440 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2441 SHORTCUTS_COL_DATA, &col_data,
2442 SHORTCUTS_COL_REMOVABLE, &removable,
2444 g_assert (col_data != NULL);
2452 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
2453 error_removing_bookmark_dialog (impl, path, error);
2456 /* Callback used when the "Remove bookmark" button is clicked */
2458 remove_bookmark_button_clicked_cb (GtkButton *button,
2459 GtkFileChooserDefault *impl)
2461 remove_selected_bookmarks (impl);
2464 struct selection_check_closure {
2465 GtkFileChooserDefault *impl;
2468 gboolean all_folders;
2471 /* Used from gtk_tree_selection_selected_foreach() */
2473 selection_check_foreach_cb (GtkTreeModel *model,
2478 struct selection_check_closure *closure;
2479 GtkTreeIter child_iter;
2480 const GtkFileInfo *info;
2484 closure->num_selected++;
2486 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2488 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
2489 is_folder = info ? gtk_file_info_get_is_folder (info) : FALSE;
2491 closure->all_folders = closure->all_folders && is_folder;
2492 closure->all_files = closure->all_files && !is_folder;
2495 /* Checks whether the selected items in the file list are all files or all folders */
2497 selection_check (GtkFileChooserDefault *impl,
2499 gboolean *all_files,
2500 gboolean *all_folders)
2502 struct selection_check_closure closure;
2503 GtkTreeSelection *selection;
2505 closure.impl = impl;
2506 closure.num_selected = 0;
2507 closure.all_files = TRUE;
2508 closure.all_folders = TRUE;
2510 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2511 gtk_tree_selection_selected_foreach (selection,
2512 selection_check_foreach_cb,
2515 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
2518 *num_selected = closure.num_selected;
2521 *all_files = closure.all_files;
2524 *all_folders = closure.all_folders;
2527 struct get_selected_path_closure {
2528 GtkFileChooserDefault *impl;
2529 const GtkFilePath *path;
2533 get_selected_path_foreach_cb (GtkTreeModel *model,
2538 struct get_selected_path_closure *closure;
2539 GtkTreeIter child_iter;
2543 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
2544 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
2547 /* Returns a selected path from the file list */
2548 static const GtkFilePath *
2549 get_selected_path (GtkFileChooserDefault *impl)
2551 struct get_selected_path_closure closure;
2552 GtkTreeSelection *selection;
2554 closure.impl = impl;
2555 closure.path = NULL;
2557 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2558 gtk_tree_selection_selected_foreach (selection,
2559 get_selected_path_foreach_cb,
2562 return closure.path;
2566 GtkFileChooserDefault *impl;
2568 } UpdateTooltipData;
2571 update_tooltip (GtkTreeModel *model,
2576 UpdateTooltipData *udata = data;
2577 GtkTreeIter child_iter;
2578 const GtkFileInfo *info;
2580 if (udata->tip == NULL)
2582 gtk_tree_model_sort_convert_iter_to_child_iter (udata->impl->sort_model,
2586 info = _gtk_file_system_model_get_info (udata->impl->browse_files_model, &child_iter);
2587 udata->tip = g_strdup_printf (_("Add the folder '%s' to the bookmarks"),
2588 gtk_file_info_get_display_name (info));
2593 /* Sensitize the "add bookmark" button if all the selected items are folders, or
2594 * if there are no selected items *and* the current folder is not in the
2595 * bookmarks list. De-sensitize the button otherwise.
2598 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
2601 gboolean all_folders;
2605 selection_check (impl, &num_selected, NULL, &all_folders);
2607 if (num_selected == 0)
2608 active = (impl->current_folder != NULL) && (shortcut_find_position (impl, impl->current_folder) == -1);
2609 else if (num_selected == 1)
2611 const GtkFilePath *path;
2613 path = get_selected_path (impl);
2614 active = all_folders && (shortcut_find_position (impl, path) == -1);
2617 active = all_folders;
2619 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2621 if (impl->browse_files_popup_menu_add_shortcut_item)
2622 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2623 (num_selected == 0) ? FALSE : active);
2627 if (num_selected == 0)
2628 tip = g_strdup_printf (_("Add the current folder to the bookmarks"));
2629 else if (num_selected > 1)
2630 tip = g_strdup_printf (_("Add the selected folders to the bookmarks"));
2633 GtkTreeSelection *selection;
2634 UpdateTooltipData data;
2636 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2639 gtk_tree_selection_selected_foreach (selection, update_tooltip, &data);
2643 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button, tip, NULL);
2648 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2649 * bookmark row is selected in the shortcuts tree.
2652 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2655 gboolean removable = FALSE;
2658 if (shortcuts_get_selected (impl, &iter))
2659 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2660 SHORTCUTS_COL_REMOVABLE, &removable,
2661 SHORTCUTS_COL_NAME, &name,
2664 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2670 tip = g_strdup_printf (_("Remove the bookmark '%s'"), name);
2671 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
2680 shortcuts_check_popup_sensitivity (GtkFileChooserDefault *impl)
2683 gboolean removable = FALSE;
2685 if (impl->browse_shortcuts_popup_menu == NULL)
2688 if (shortcuts_get_selected (impl, &iter))
2689 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2690 SHORTCUTS_COL_REMOVABLE, &removable,
2693 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_remove_item, removable);
2694 gtk_widget_set_sensitive (impl->browse_shortcuts_popup_menu_rename_item, removable);
2697 /* GtkWidget::drag-begin handler for the shortcuts list. */
2699 shortcuts_drag_begin_cb (GtkWidget *widget,
2700 GdkDragContext *context,
2701 GtkFileChooserDefault *impl)
2704 impl->shortcuts_drag_context = g_object_ref (context);
2709 /* Removes the idle handler for outside drags */
2711 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2713 if (!impl->shortcuts_drag_outside_idle)
2716 g_source_destroy (impl->shortcuts_drag_outside_idle);
2717 impl->shortcuts_drag_outside_idle = NULL;
2721 /* GtkWidget::drag-end handler for the shortcuts list. */
2723 shortcuts_drag_end_cb (GtkWidget *widget,
2724 GdkDragContext *context,
2725 GtkFileChooserDefault *impl)
2728 g_object_unref (impl->shortcuts_drag_context);
2730 shortcuts_cancel_drag_outside_idle (impl);
2732 if (!impl->shortcuts_drag_outside)
2735 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2737 impl->shortcuts_drag_outside = FALSE;
2741 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2743 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2744 GdkDragContext *context,
2745 GtkFileChooserDefault *impl)
2747 g_signal_stop_emission_by_name (widget, "drag_data_delete");
2751 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2755 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2758 GtkTreeView *tree_view;
2761 GdkPixmap *row_pixmap;
2766 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2768 /* Find the selected path and get its drag pixmap */
2770 if (!shortcuts_get_selected (impl, &iter))
2771 g_assert_not_reached ();
2773 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2775 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2776 gtk_tree_path_free (path);
2785 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2791 GdkPixmap *composite;
2792 int row_pixmap_width, row_pixmap_height;
2793 int pixbuf_width, pixbuf_height;
2794 int composite_width, composite_height;
2795 int pixbuf_x, pixbuf_y;
2796 GdkGC *gc, *mask_gc;
2798 GdkBitmap *pixbuf_mask;
2800 /* Create pixmap and mask for composite image */
2802 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2803 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2804 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2806 composite_width = MAX (row_pixmap_width, pixbuf_width);
2807 composite_height = MAX (row_pixmap_height, pixbuf_height);
2809 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2811 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2814 pixbuf_x = composite_width - pixbuf_width;
2816 pixbuf_y = (composite_height - pixbuf_height) / 2;
2818 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2819 gc = gdk_gc_new (composite);
2821 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2822 mask_gc = gdk_gc_new (mask);
2824 gdk_gc_set_foreground (mask_gc, &color);
2825 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2828 color.green = 0xffff;
2829 color.blue = 0xffff;
2830 gdk_gc_set_rgb_fg_color (gc, &color);
2831 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2833 /* Composite the row pixmap and the pixbuf */
2835 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2837 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2838 NULL, &pixbuf_mask, 128);
2839 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2842 pixbuf_width, pixbuf_height);
2843 g_object_unref (pixbuf_mask);
2845 gdk_draw_drawable (composite, gc, row_pixmap,
2848 row_pixmap_width, row_pixmap_height);
2850 gdk_gc_set_foreground (mask_gc, &color);
2851 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2853 gdk_draw_pixbuf (composite, gc, pixbuf,
2856 pixbuf_width, pixbuf_height,
2860 g_object_unref (pixbuf);
2861 g_object_unref (row_pixmap);
2863 row_pixmap = composite;
2867 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2869 gtk_tree_view_get_path_at_pos (tree_view,
2870 tree_view->priv->press_start_x,
2871 tree_view->priv->press_start_y,
2877 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2878 gdk_drawable_get_colormap (row_pixmap),
2881 tree_view->priv->press_start_x + 1,
2882 row_pixmap_y + cell_y + 1);
2884 g_object_unref (row_pixmap);
2886 g_object_unref (mask);
2889 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2890 * handler so that we can tell apart the drag_leave event that comes right
2891 * before a drag_drop, from a normal drag_leave. We don't want to set the
2892 * cursor nor the flag in the latter case.
2895 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2897 GDK_THREADS_ENTER ();
2899 shortcuts_drag_set_delete_cursor (impl, TRUE);
2900 impl->shortcuts_drag_outside = TRUE;
2902 shortcuts_cancel_drag_outside_idle (impl);
2904 GDK_THREADS_LEAVE ();
2910 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2914 shortcuts_drag_leave_cb (GtkWidget *widget,
2915 GdkDragContext *context,
2917 GtkFileChooserDefault *impl)
2920 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2922 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2923 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2924 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2926 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2930 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2932 GTK_TREE_VIEW_DROP_BEFORE);
2934 g_signal_stop_emission_by_name (widget, "drag_leave");
2937 /* Computes the appropriate row and position for dropping */
2939 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2943 GtkTreeViewDropPosition *pos)
2945 GtkTreeView *tree_view;
2946 GtkTreeViewColumn *column;
2950 int bookmarks_index;
2952 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2954 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2956 if (!gtk_tree_view_get_path_at_pos (tree_view,
2958 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2964 row = bookmarks_index + impl->num_bookmarks - 1;
2965 *path = gtk_tree_path_new_from_indices (row, -1);
2966 *pos = GTK_TREE_VIEW_DROP_AFTER;
2970 row = *gtk_tree_path_get_indices (*path);
2971 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2972 gtk_tree_path_free (*path);
2974 if (row < bookmarks_index)
2976 row = bookmarks_index;
2977 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2979 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2981 row = bookmarks_index + impl->num_bookmarks - 1;
2982 *pos = GTK_TREE_VIEW_DROP_AFTER;
2986 if (cell_y < cell.height / 2)
2987 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2989 *pos = GTK_TREE_VIEW_DROP_AFTER;
2992 *path = gtk_tree_path_new_from_indices (row, -1);
2995 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2996 * implement the destination side of DnD by hand, due to limitations in
2997 * GtkTreeView's DnD API.
3000 shortcuts_drag_motion_cb (GtkWidget *widget,
3001 GdkDragContext *context,
3005 GtkFileChooserDefault *impl)
3008 GtkTreeViewDropPosition pos;
3009 GdkDragAction action;
3012 if (gtk_drag_get_source_widget (context) == widget)
3014 shortcuts_cancel_drag_outside_idle (impl);
3016 if (impl->shortcuts_drag_outside)
3018 shortcuts_drag_set_delete_cursor (impl, FALSE);
3019 impl->shortcuts_drag_outside = FALSE;
3024 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
3025 action = GDK_ACTION_COPY;
3026 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
3027 action = GDK_ACTION_MOVE;
3034 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
3035 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
3036 gtk_tree_path_free (path);
3040 g_signal_stop_emission_by_name (widget, "drag_motion");
3044 gdk_drag_status (context, action, time_);
3051 /* GtkWidget::drag-drop handler for the shortcuts list. */
3053 shortcuts_drag_drop_cb (GtkWidget *widget,
3054 GdkDragContext *context,
3058 GtkFileChooserDefault *impl)
3061 shortcuts_cancel_drag_outside_idle (impl);
3064 g_signal_stop_emission_by_name (widget, "drag_drop");
3068 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
3070 shortcuts_drop_uris (GtkFileChooserDefault *impl,
3077 uris = g_uri_list_extract_uris (data);
3079 for (i = 0; uris[i]; i++)
3085 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3089 if (shortcuts_add_bookmark_from_path (impl, path, position))
3092 gtk_file_path_free (path);
3098 g_set_error (&error,
3099 GTK_FILE_CHOOSER_ERROR,
3100 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3101 _("Could not add a bookmark for '%s' "
3102 "because it is an invalid path name."),
3104 error_adding_bookmark_dialog (impl, path, error);
3111 /* Reorders the selected bookmark to the specified position */
3113 shortcuts_reorder (GtkFileChooserDefault *impl,
3121 int bookmarks_index;
3122 const GtkFilePath *file_path;
3123 GtkFilePath *file_path_copy;
3127 /* Get the selected path */
3129 if (!shortcuts_get_selected (impl, &iter))
3130 g_assert_not_reached ();
3132 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3133 old_position = *gtk_tree_path_get_indices (path);
3134 gtk_tree_path_free (path);
3136 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3137 old_position -= bookmarks_index;
3138 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
3140 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3141 SHORTCUTS_COL_NAME, &name,
3142 SHORTCUTS_COL_DATA, &col_data,
3143 SHORTCUTS_COL_IS_VOLUME, &is_volume,
3145 g_assert (col_data != NULL);
3146 g_assert (!is_volume);
3148 file_path = col_data;
3149 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
3151 /* Remove the path from the old position and insert it in the new one */
3153 if (new_position > old_position)
3156 if (old_position == new_position)
3160 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
3162 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
3163 gtk_file_system_set_bookmark_label (impl->file_system, file_path_copy, name);
3166 error_adding_bookmark_dialog (impl, file_path_copy, error);
3170 gtk_file_path_free (file_path_copy);
3173 /* Callback used when we get the drag data for the bookmarks list. We add the
3174 * received URIs as bookmarks if they are folders.
3177 shortcuts_drag_data_received_cb (GtkWidget *widget,
3178 GdkDragContext *context,
3181 GtkSelectionData *selection_data,
3186 GtkFileChooserDefault *impl;
3187 GtkTreePath *tree_path;
3188 GtkTreeViewDropPosition tree_pos;
3190 int bookmarks_index;
3192 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3194 /* Compute position */
3196 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
3198 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
3199 position = *gtk_tree_path_get_indices (tree_path);
3200 gtk_tree_path_free (tree_path);
3202 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
3205 g_assert (position >= bookmarks_index);
3206 position -= bookmarks_index;
3208 if (selection_data->target == gdk_atom_intern_static_string ("text/uri-list"))
3209 shortcuts_drop_uris (impl, (const char *) selection_data->data, position);
3210 else if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
3211 shortcuts_reorder (impl, position);
3213 g_signal_stop_emission_by_name (widget, "drag_data_received");
3216 /* Callback used when the selection in the shortcuts tree changes */
3218 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
3219 GtkFileChooserDefault *impl)
3221 bookmarks_check_remove_sensitivity (impl);
3222 shortcuts_check_popup_sensitivity (impl);
3226 shortcuts_row_separator_func (GtkTreeModel *model,
3230 gint column = GPOINTER_TO_INT (data);
3233 gtk_tree_model_get (model, iter, column, &text, -1);
3243 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
3244 * keypresses before the TreeView gets them.
3247 tree_view_keybinding_cb (GtkWidget *tree_view,
3249 GtkFileChooserDefault *impl)
3251 if ((event->keyval == GDK_slash
3252 || event->keyval == GDK_KP_Divide
3254 || event->keyval == GDK_asciitilde
3256 ) && ! (event->state & (~GDK_SHIFT_MASK & gtk_accelerator_get_default_mod_mask ())))
3258 location_popup_handler (impl, event->string);
3265 /* Callback used when the file list's popup menu is detached */
3267 shortcuts_popup_menu_detach_cb (GtkWidget *attach_widget,
3270 GtkFileChooserDefault *impl;
3272 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3273 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3275 impl->browse_shortcuts_popup_menu = NULL;
3276 impl->browse_shortcuts_popup_menu_remove_item = NULL;
3277 impl->browse_shortcuts_popup_menu_rename_item = NULL;
3281 remove_shortcut_cb (GtkMenuItem *item,
3282 GtkFileChooserDefault *impl)
3284 remove_selected_bookmarks (impl);
3287 /* Rename the selected bookmark */
3289 rename_selected_bookmark (GtkFileChooserDefault *impl)
3293 GtkTreeViewColumn *column;
3294 GtkCellRenderer *cell;
3297 if (shortcuts_get_selected (impl, &iter))
3299 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
3300 column = gtk_tree_view_get_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), 0);
3301 renderers = gtk_tree_view_column_get_cell_renderers (column);
3302 cell = g_list_nth_data (renderers, 1);
3303 g_list_free (renderers);
3304 g_object_set (cell, "editable", TRUE, NULL);
3305 gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3306 path, column, cell, TRUE);
3307 gtk_tree_path_free (path);
3312 rename_shortcut_cb (GtkMenuItem *item,
3313 GtkFileChooserDefault *impl)
3315 rename_selected_bookmark (impl);
3318 /* Constructs the popup menu for the file list if needed */
3320 shortcuts_build_popup_menu (GtkFileChooserDefault *impl)
3324 if (impl->browse_shortcuts_popup_menu)
3327 impl->browse_shortcuts_popup_menu = gtk_menu_new ();
3328 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_shortcuts_popup_menu),
3329 impl->browse_shortcuts_tree_view,
3330 shortcuts_popup_menu_detach_cb);
3332 item = gtk_image_menu_item_new_with_label (_("Remove"));
3333 impl->browse_shortcuts_popup_menu_remove_item = item;
3334 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3335 gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_MENU));
3336 g_signal_connect (item, "activate",
3337 G_CALLBACK (remove_shortcut_cb), impl);
3338 gtk_widget_show (item);
3339 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3341 item = gtk_menu_item_new_with_label (_("Rename..."));
3342 impl->browse_shortcuts_popup_menu_rename_item = item;
3343 g_signal_connect (item, "activate",
3344 G_CALLBACK (rename_shortcut_cb), impl);
3345 gtk_widget_show (item);
3346 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu), item);
3348 shortcuts_check_popup_sensitivity (impl);
3352 shortcuts_update_popup_menu (GtkFileChooserDefault *impl)
3354 shortcuts_build_popup_menu (impl);
3358 popup_position_func (GtkMenu *menu,
3362 gpointer user_data);
3365 shortcuts_popup_menu (GtkFileChooserDefault *impl,
3366 GdkEventButton *event)
3368 shortcuts_update_popup_menu (impl);
3370 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3371 NULL, NULL, NULL, NULL,
3372 event->button, event->time);
3375 gtk_menu_popup (GTK_MENU (impl->browse_shortcuts_popup_menu),
3377 popup_position_func, impl->browse_shortcuts_tree_view,
3378 0, GDK_CURRENT_TIME);
3379 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_shortcuts_popup_menu),
3384 /* Callback used for the GtkWidget::popup-menu signal of the shortcuts list */
3386 shortcuts_popup_menu_cb (GtkWidget *widget,
3387 GtkFileChooserDefault *impl)
3389 shortcuts_popup_menu (impl, NULL);
3393 /* Callback used when a button is pressed on the shortcuts list.
3394 * We trap button 3 to bring up a popup menu.
3397 shortcuts_button_press_event_cb (GtkWidget *widget,
3398 GdkEventButton *event,
3399 GtkFileChooserDefault *impl)
3401 static gboolean in_press = FALSE;
3407 if (event->button != 3)
3411 handled = gtk_widget_event (impl->browse_shortcuts_tree_view, (GdkEvent *) event);
3417 shortcuts_popup_menu (impl, event);
3422 shortcuts_edited (GtkCellRenderer *cell,
3425 GtkFileChooserDefault *impl)
3429 GtkFilePath *shortcut;
3431 g_object_set (cell, "editable", FALSE, NULL);
3433 path = gtk_tree_path_new_from_string (path_string);
3434 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
3435 g_assert_not_reached ();
3437 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
3438 SHORTCUTS_COL_DATA, &shortcut,
3440 gtk_tree_path_free (path);
3442 gtk_file_system_set_bookmark_label (impl->file_system, shortcut, new_text);
3446 shortcuts_editing_canceled (GtkCellRenderer *cell,
3447 GtkFileChooserDefault *impl)
3449 g_object_set (cell, "editable", FALSE, NULL);
3452 /* Creates the widgets for the shortcuts and bookmarks tree */
3454 shortcuts_list_create (GtkFileChooserDefault *impl)
3457 GtkTreeSelection *selection;
3458 GtkTreeViewColumn *column;
3459 GtkCellRenderer *renderer;
3461 /* Scrolled window */
3463 swin = gtk_scrolled_window_new (NULL, NULL);
3464 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3465 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3466 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3468 gtk_widget_show (swin);
3472 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
3473 #ifdef PROFILE_FILE_CHOOSER
3474 g_object_set_data (G_OBJECT (impl->browse_shortcuts_tree_view), "fmq-name", "shortcuts");
3476 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3477 G_CALLBACK (tree_view_keybinding_cb), impl);
3478 g_signal_connect (impl->browse_shortcuts_tree_view, "popup_menu",
3479 G_CALLBACK (shortcuts_popup_menu_cb), impl);
3480 g_signal_connect (impl->browse_shortcuts_tree_view, "button_press_event",
3481 G_CALLBACK (shortcuts_button_press_event_cb), impl);
3482 /* Accessible object name for the file chooser's shortcuts pane */
3483 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Places"));
3485 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
3487 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3489 shortcuts_source_targets,
3490 num_shortcuts_source_targets,
3493 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
3494 GTK_DEST_DEFAULT_ALL,
3495 shortcuts_dest_targets,
3496 num_shortcuts_dest_targets,
3497 GDK_ACTION_COPY | GDK_ACTION_MOVE);
3499 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
3500 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
3501 gtk_tree_selection_set_select_function (selection,
3502 shortcuts_select_func,
3505 g_signal_connect (selection, "changed",
3506 G_CALLBACK (shortcuts_selection_changed_cb), impl);
3508 g_signal_connect (impl->browse_shortcuts_tree_view, "row_activated",
3509 G_CALLBACK (shortcuts_row_activated_cb), impl);
3511 g_signal_connect (impl->browse_shortcuts_tree_view, "key_press_event",
3512 G_CALLBACK (shortcuts_key_press_event_cb), impl);
3514 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_begin",
3515 G_CALLBACK (shortcuts_drag_begin_cb), impl);
3516 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_end",
3517 G_CALLBACK (shortcuts_drag_end_cb), impl);
3518 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_delete",
3519 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
3521 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_leave",
3522 G_CALLBACK (shortcuts_drag_leave_cb), impl);
3523 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_motion",
3524 G_CALLBACK (shortcuts_drag_motion_cb), impl);
3525 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_drop",
3526 G_CALLBACK (shortcuts_drag_drop_cb), impl);
3527 g_signal_connect (impl->browse_shortcuts_tree_view, "drag_data_received",
3528 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
3530 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
3531 gtk_widget_show (impl->browse_shortcuts_tree_view);
3535 column = gtk_tree_view_column_new ();
3536 /* Column header for the file chooser's shortcuts pane */
3537 gtk_tree_view_column_set_title (column, _("_Places"));
3539 renderer = gtk_cell_renderer_pixbuf_new ();
3540 gtk_tree_view_column_pack_start (column, renderer, FALSE);
3541 gtk_tree_view_column_set_attributes (column, renderer,
3542 "pixbuf", SHORTCUTS_COL_PIXBUF,
3543 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3546 renderer = gtk_cell_renderer_text_new ();
3547 g_signal_connect (renderer, "edited",
3548 G_CALLBACK (shortcuts_edited), impl);
3549 g_signal_connect (renderer, "editing-canceled",
3550 G_CALLBACK (shortcuts_editing_canceled), impl);
3551 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3552 gtk_tree_view_column_set_attributes (column, renderer,
3553 "text", SHORTCUTS_COL_NAME,
3556 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
3557 shortcuts_row_separator_func,
3558 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3561 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
3566 /* Creates the widgets for the shortcuts/bookmarks pane */
3568 shortcuts_pane_create (GtkFileChooserDefault *impl,
3569 GtkSizeGroup *size_group)
3575 vbox = gtk_vbox_new (FALSE, 6);
3576 gtk_widget_show (vbox);
3578 /* Shortcuts tree */
3580 widget = shortcuts_list_create (impl);
3581 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
3583 /* Box for buttons */
3585 hbox = gtk_hbox_new (TRUE, 6);
3586 gtk_size_group_add_widget (size_group, hbox);
3587 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3588 gtk_widget_show (hbox);
3590 /* Add bookmark button */
3592 impl->browse_shortcuts_add_button = button_new (impl,
3597 G_CALLBACK (add_bookmark_button_clicked_cb));
3598 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
3599 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_add_button,
3600 _("Add the selected folder to the Bookmarks"), NULL);
3602 /* Remove bookmark button */
3604 impl->browse_shortcuts_remove_button = button_new (impl,
3609 G_CALLBACK (remove_bookmark_button_clicked_cb));
3610 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
3611 gtk_tooltips_set_tip (impl->tooltips, impl->browse_shortcuts_remove_button,
3612 _("Remove the selected bookmark"), NULL);
3617 /* Handles key press events on the file list, so that we can trap Enter to
3618 * activate the default button on our own. Also, checks to see if '/' has been
3619 * pressed. See comment by tree_view_keybinding_cb() for more details.
3622 trap_activate_cb (GtkWidget *widget,
3626 GtkFileChooserDefault *impl;
3629 impl = (GtkFileChooserDefault *) data;
3631 modifiers = gtk_accelerator_get_default_mod_mask ();
3633 if ((event->keyval == GDK_slash
3634 || event->keyval == GDK_KP_Divide
3636 || event->keyval == GDK_asciitilde
3638 ) && ! (event->state & (~GDK_SHIFT_MASK & modifiers)))
3640 location_popup_handler (impl, event->string);
3644 if ((event->keyval == GDK_Return
3645 || event->keyval == GDK_ISO_Enter
3646 || event->keyval == GDK_KP_Enter
3647 || event->keyval == GDK_space)
3648 && ((event->state & modifiers) == 0)
3649 && !(impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3650 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
3654 window = get_toplevel (widget);
3656 && widget != window->default_widget
3657 && !(widget == window->focus_widget &&
3658 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3660 gtk_window_activate_default (window);
3668 /* Callback used when the file list's popup menu is detached */
3670 popup_menu_detach_cb (GtkWidget *attach_widget,
3673 GtkFileChooserDefault *impl;
3675 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
3676 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
3678 impl->browse_files_popup_menu = NULL;
3679 impl->browse_files_popup_menu_add_shortcut_item = NULL;
3680 impl->browse_files_popup_menu_hidden_files_item = NULL;
3683 /* Callback used when the "Add to Bookmarks" menu item is activated */
3685 add_to_shortcuts_cb (GtkMenuItem *item,
3686 GtkFileChooserDefault *impl)
3688 bookmarks_add_selected_folder (impl);
3691 /* Callback used when the "Show Hidden Files" menu item is toggled */
3693 show_hidden_toggled_cb (GtkCheckMenuItem *item,
3694 GtkFileChooserDefault *impl)
3697 "show-hidden", gtk_check_menu_item_get_active (item),
3701 /* Shows an error dialog about not being able to select a dragged file */
3703 error_selecting_dragged_file_dialog (GtkFileChooserDefault *impl,
3704 const GtkFilePath *path,
3708 _("Could not select file"),
3713 file_list_drag_data_select_uris (GtkFileChooserDefault *impl,
3718 GtkFileChooser *chooser = GTK_FILE_CHOOSER (impl);
3720 for (i = 1; uris[i]; i++)
3725 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3729 GError *error = NULL;
3731 gtk_file_chooser_default_select_path (chooser, path, &error);
3733 error_selecting_dragged_file_dialog (impl, path, error);
3735 gtk_file_path_free (path);
3740 struct FileListDragData
3742 GtkFileChooserDefault *impl;
3748 file_list_drag_data_received_get_info_cb (GtkFileSystemHandle *handle,
3749 const GtkFileInfo *info,
3750 const GError *error,
3753 gboolean cancelled = handle->cancelled;
3754 struct FileListDragData *data = user_data;
3755 GtkFileChooser *chooser = GTK_FILE_CHOOSER (data->impl);
3757 if (handle != data->impl->file_list_drag_data_received_handle)
3760 data->impl->file_list_drag_data_received_handle = NULL;
3762 if (cancelled || error)
3765 if ((data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3766 data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE) &&
3767 data->uris[1] == 0 && !error &&
3768 gtk_file_info_get_is_folder (info))
3769 change_folder_and_display_error (data->impl, data->path);
3772 GError *error = NULL;
3774 gtk_file_chooser_default_unselect_all (chooser);
3775 gtk_file_chooser_default_select_path (chooser, data->path, &error);
3777 error_selecting_dragged_file_dialog (data->impl, data->path, error);
3779 browse_files_center_selected_row (data->impl);
3782 if (data->impl->select_multiple)
3783 file_list_drag_data_select_uris (data->impl, data->uris);
3786 g_object_unref (data->impl);
3787 g_strfreev (data->uris);
3788 gtk_file_path_free (data->path);
3791 g_object_unref (handle);
3795 file_list_drag_data_received_cb (GtkWidget *widget,
3796 GdkDragContext *context,
3799 GtkSelectionData *selection_data,
3804 GtkFileChooserDefault *impl;
3805 GtkFileChooser *chooser;
3809 GError *error = NULL;
3811 impl = GTK_FILE_CHOOSER_DEFAULT (data);
3812 chooser = GTK_FILE_CHOOSER (data);
3814 /* Parse the text/uri-list string, navigate to the first one */
3815 uris = g_uri_list_extract_uris ((const char *) selection_data->data);
3819 path = gtk_file_system_uri_to_path (impl->file_system, uri);
3823 struct FileListDragData *data;
3825 data = g_new0 (struct FileListDragData, 1);
3826 data->impl = g_object_ref (impl);
3830 if (impl->file_list_drag_data_received_handle)
3831 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
3833 impl->file_list_drag_data_received_handle =
3834 gtk_file_system_get_info (impl->file_system, path,
3835 GTK_FILE_INFO_IS_FOLDER,
3836 file_list_drag_data_received_get_info_cb,
3842 g_set_error (&error,
3843 GTK_FILE_CHOOSER_ERROR,
3844 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
3845 _("Could not select file '%s' "
3846 "because it is an invalid path name."),
3848 error_selecting_dragged_file_dialog (impl, NULL, error);
3851 if (impl->select_multiple)
3852 file_list_drag_data_select_uris (impl, uris);
3858 g_signal_stop_emission_by_name (widget, "drag_data_received");
3861 /* Don't do anything with the drag_drop signal */
3863 file_list_drag_drop_cb (GtkWidget *widget,
3864 GdkDragContext *context,
3868 GtkFileChooserDefault *impl)
3870 g_signal_stop_emission_by_name (widget, "drag_drop");
3874 /* Disable the normal tree drag motion handler, it makes it look like you're
3875 dropping the dragged item onto a tree item */
3877 file_list_drag_motion_cb (GtkWidget *widget,
3878 GdkDragContext *context,
3882 GtkFileChooserDefault *impl)
3884 g_signal_stop_emission_by_name (widget, "drag_motion");
3888 /* Constructs the popup menu for the file list if needed */
3890 file_list_build_popup_menu (GtkFileChooserDefault *impl)
3894 if (impl->browse_files_popup_menu)
3897 impl->browse_files_popup_menu = gtk_menu_new ();
3898 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
3899 impl->browse_files_tree_view,
3900 popup_menu_detach_cb);
3902 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Bookmarks"));
3903 impl->browse_files_popup_menu_add_shortcut_item = item;
3904 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
3905 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
3906 gtk_widget_set_sensitive (item, FALSE);
3907 g_signal_connect (item, "activate",
3908 G_CALLBACK (add_to_shortcuts_cb), impl);
3909 gtk_widget_show (item);
3910 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3912 item = gtk_separator_menu_item_new ();
3913 gtk_widget_show (item);
3914 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3916 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
3917 impl->browse_files_popup_menu_hidden_files_item = item;
3918 g_signal_connect (item, "toggled",
3919 G_CALLBACK (show_hidden_toggled_cb), impl);
3920 gtk_widget_show (item);
3921 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
3924 /* Updates the popup menu for the file list, creating it if necessary */
3926 file_list_update_popup_menu (GtkFileChooserDefault *impl)
3928 file_list_build_popup_menu (impl);
3930 /* The sensitivity of the Add to Bookmarks item is set in
3931 * bookmarks_check_add_sensitivity()
3934 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
3935 G_CALLBACK (show_hidden_toggled_cb), impl);
3936 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
3938 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
3939 G_CALLBACK (show_hidden_toggled_cb), impl);
3943 popup_position_func (GtkMenu *menu,
3949 GtkWidget *widget = GTK_WIDGET (user_data);
3950 GdkScreen *screen = gtk_widget_get_screen (widget);
3953 GdkRectangle monitor;
3955 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
3957 gdk_window_get_origin (widget->window, x, y);
3959 gtk_widget_size_request (GTK_WIDGET (menu), &req);
3961 *x += (widget->allocation.width - req.width) / 2;
3962 *y += (widget->allocation.height - req.height) / 2;
3964 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
3965 gtk_menu_set_monitor (menu, monitor_num);
3966 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
3968 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
3969 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
3975 file_list_popup_menu (GtkFileChooserDefault *impl,
3976 GdkEventButton *event)
3978 file_list_update_popup_menu (impl);
3980 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3981 NULL, NULL, NULL, NULL,
3982 event->button, event->time);
3985 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
3987 popup_position_func, impl->browse_files_tree_view,
3988 0, GDK_CURRENT_TIME);
3989 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
3995 /* Callback used for the GtkWidget::popup-menu signal of the file list */
3997 list_popup_menu_cb (GtkWidget *widget,
3998 GtkFileChooserDefault *impl)
4000 file_list_popup_menu (impl, NULL);
4004 /* Callback used when a button is pressed on the file list. We trap button 3 to
4005 * bring up a popup menu.
4008 list_button_press_event_cb (GtkWidget *widget,
4009 GdkEventButton *event,
4010 GtkFileChooserDefault *impl)
4012 static gboolean in_press = FALSE;
4018 if (event->button != 3)
4022 handled = gtk_widget_event (impl->browse_files_tree_view, (GdkEvent *) event);
4025 file_list_popup_menu (impl, event);
4029 /* Creates the widgets for the file list */
4031 create_file_list (GtkFileChooserDefault *impl)
4034 GtkTreeSelection *selection;
4035 GtkTreeViewColumn *column;
4036 GtkCellRenderer *renderer;
4038 /* Scrolled window */
4040 swin = gtk_scrolled_window_new (NULL, NULL);
4041 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
4042 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
4043 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
4046 /* Tree/list view */
4048 impl->browse_files_tree_view = gtk_tree_view_new ();
4049 #ifdef PROFILE_FILE_CHOOSER
4050 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "fmq-name", "file_list");
4052 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), I_("GtkFileChooserDefault"), impl);
4053 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
4055 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
4056 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
4058 gtk_drag_dest_set (impl->browse_files_tree_view,
4059 GTK_DEST_DEFAULT_ALL,
4060 file_list_dest_targets,
4061 num_file_list_dest_targets,
4062 GDK_ACTION_COPY | GDK_ACTION_MOVE);
4064 g_signal_connect (impl->browse_files_tree_view, "row_activated",
4065 G_CALLBACK (list_row_activated), impl);
4066 g_signal_connect (impl->browse_files_tree_view, "key_press_event",
4067 G_CALLBACK (trap_activate_cb), impl);
4068 g_signal_connect (impl->browse_files_tree_view, "popup_menu",
4069 G_CALLBACK (list_popup_menu_cb), impl);
4070 g_signal_connect (impl->browse_files_tree_view, "button_press_event",
4071 G_CALLBACK (list_button_press_event_cb), impl);
4073 g_signal_connect (impl->browse_files_tree_view, "drag_data_received",
4074 G_CALLBACK (file_list_drag_data_received_cb), impl);
4075 g_signal_connect (impl->browse_files_tree_view, "drag_drop",
4076 G_CALLBACK (file_list_drag_drop_cb), impl);
4077 g_signal_connect (impl->browse_files_tree_view, "drag_motion",
4078 G_CALLBACK (file_list_drag_motion_cb), impl);
4080 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4081 gtk_tree_selection_set_select_function (selection,
4084 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
4086 file_list_source_targets,
4087 num_file_list_source_targets,
4090 g_signal_connect (selection, "changed",
4091 G_CALLBACK (list_selection_changed), impl);
4093 /* Filename column */
4095 impl->list_name_column = gtk_tree_view_column_new ();
4096 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
4097 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
4098 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
4099 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
4101 renderer = gtk_cell_renderer_pixbuf_new ();
4102 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
4103 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
4104 list_icon_data_func, impl, NULL);
4106 impl->list_name_renderer = gtk_cell_renderer_text_new ();
4107 g_object_set (impl->list_name_renderer,
4108 "ellipsize", PANGO_ELLIPSIZE_END,
4110 g_signal_connect (impl->list_name_renderer, "edited",
4111 G_CALLBACK (renderer_edited_cb), impl);
4112 g_signal_connect (impl->list_name_renderer, "editing_canceled",
4113 G_CALLBACK (renderer_editing_canceled_cb), impl);
4114 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
4115 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
4116 list_name_data_func, impl, NULL);
4118 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
4122 column = gtk_tree_view_column_new ();
4123 gtk_tree_view_column_set_title (column, _("Size"));
4125 renderer = gtk_cell_renderer_text_new ();
4126 gtk_tree_view_column_pack_start (column, renderer, TRUE); /* bug: it doesn't expand */
4127 gtk_tree_view_column_set_cell_data_func (column, renderer,
4128 list_size_data_func, impl, NULL);
4129 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
4130 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4132 /* Modification time column */
4134 column = gtk_tree_view_column_new ();
4135 gtk_tree_view_column_set_resizable (column, TRUE);
4136 gtk_tree_view_column_set_title (column, _("Modified"));
4138 renderer = gtk_cell_renderer_text_new ();
4139 gtk_tree_view_column_pack_start (column, renderer, TRUE);
4140 gtk_tree_view_column_set_cell_data_func (column, renderer,
4141 list_mtime_data_func, impl, NULL);
4142 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
4143 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
4144 gtk_widget_show_all (swin);
4150 create_path_bar (GtkFileChooserDefault *impl)
4152 GtkWidget *path_bar;
4154 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
4155 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
4161 set_filter_tooltip (GtkWidget *widget,
4164 GtkTooltips *tooltips = (GtkTooltips *)data;
4166 if (GTK_IS_BUTTON (widget))
4167 gtk_tooltips_set_tip (tooltips, widget,
4168 _("Select which types of files are shown"),
4173 realize_filter_combo (GtkWidget *combo,
4176 GtkFileChooserDefault *impl = (GtkFileChooserDefault *)data;
4178 gtk_container_forall (GTK_CONTAINER (combo),
4183 /* Creates the widgets for the files/folders pane */
4185 file_pane_create (GtkFileChooserDefault *impl,
4186 GtkSizeGroup *size_group)
4192 vbox = gtk_vbox_new (FALSE, 6);
4193 gtk_widget_show (vbox);
4195 /* Box for lists and preview */
4197 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
4198 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
4199 gtk_widget_show (hbox);
4203 widget = create_file_list (impl);
4204 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
4208 impl->preview_box = gtk_vbox_new (FALSE, 12);
4209 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
4210 /* Don't show preview box initially */
4214 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
4216 widget = filter_create (impl);
4218 g_signal_connect (widget, "realize",
4219 G_CALLBACK (realize_filter_combo), impl);
4221 gtk_widget_show (widget);
4222 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
4224 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
4225 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
4230 /* Callback used when the "Browse for more folders" expander is toggled */
4232 expander_changed_cb (GtkExpander *expander,
4234 GtkFileChooserDefault *impl)
4236 impl->expand_folders = gtk_expander_get_expanded(GTK_EXPANDER (impl->save_expander));
4237 update_appearance (impl);
4240 /* Callback used when the selection changes in the save folder combo box */
4242 save_folder_combo_changed_cb (GtkComboBox *combo,
4243 GtkFileChooserDefault *impl)
4247 if (impl->changing_folder)
4250 if (gtk_combo_box_get_active_iter (combo, &iter))
4251 shortcuts_activate_iter (impl, &iter);
4254 /* Creates the combo box with the save folders */
4256 save_folder_combo_create (GtkFileChooserDefault *impl)
4259 GtkCellRenderer *cell;
4261 combo = g_object_new (GTK_TYPE_COMBO_BOX,
4262 "model", impl->shortcuts_model,
4263 "focus-on-click", FALSE,
4265 gtk_widget_show (combo);
4267 cell = gtk_cell_renderer_pixbuf_new ();
4268 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
4269 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4270 "pixbuf", SHORTCUTS_COL_PIXBUF,
4271 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
4272 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4275 cell = gtk_cell_renderer_text_new ();
4276 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
4277 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
4278 "text", SHORTCUTS_COL_NAME,
4279 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
4282 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
4283 shortcuts_row_separator_func,
4284 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
4287 g_signal_connect (combo, "changed",
4288 G_CALLBACK (save_folder_combo_changed_cb), impl);
4293 /* Creates the widgets specific to Save mode */
4295 save_widgets_create (GtkFileChooserDefault *impl)
4300 GtkWidget *alignment;
4302 if (impl->save_widgets != NULL)
4305 location_switch_to_path_bar (impl);
4307 vbox = gtk_vbox_new (FALSE, 12);
4309 table = gtk_table_new (2, 2, FALSE);
4310 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
4311 gtk_widget_show (table);
4312 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
4313 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
4317 widget = gtk_label_new_with_mnemonic (_("_Name:"));
4318 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
4319 gtk_table_attach (GTK_TABLE (table), widget,
4323 gtk_widget_show (widget);
4325 /* Location entry */
4327 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4328 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4330 gtk_entry_set_width_chars (GTK_ENTRY (impl->location_entry), 45);
4331 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4332 gtk_table_attach (GTK_TABLE (table), impl->location_entry,
4334 GTK_EXPAND | GTK_FILL, 0,
4336 gtk_widget_show (impl->location_entry);
4337 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->location_entry);
4340 impl->save_folder_label = gtk_label_new (NULL);
4341 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
4342 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
4346 gtk_widget_show (impl->save_folder_label);
4348 impl->save_folder_combo = save_folder_combo_create (impl);
4349 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
4351 GTK_EXPAND | GTK_FILL, GTK_FILL,
4353 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
4356 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4357 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
4359 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
4360 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
4361 g_signal_connect (impl->save_expander, "notify::expanded",
4362 G_CALLBACK (expander_changed_cb),
4364 gtk_widget_show_all (alignment);
4366 impl->save_widgets = vbox;
4367 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
4368 gtk_box_reorder_child (GTK_BOX (impl), impl->save_widgets, 0);
4369 gtk_widget_show (impl->save_widgets);
4372 /* Destroys the widgets specific to Save mode */
4374 save_widgets_destroy (GtkFileChooserDefault *impl)
4376 if (impl->save_widgets == NULL)
4379 gtk_widget_destroy (impl->save_widgets);
4380 impl->save_widgets = NULL;
4381 impl->location_entry = NULL;
4382 impl->save_folder_label = NULL;
4383 impl->save_folder_combo = NULL;
4384 impl->save_expander = NULL;
4387 /* Turns on the path bar widget. Can be called even if we are already in that
4391 location_switch_to_path_bar (GtkFileChooserDefault *impl)
4393 if (impl->location_entry)
4395 gtk_widget_destroy (impl->location_entry);
4396 impl->location_entry = NULL;
4399 gtk_widget_hide (impl->location_entry_box);
4402 /* Sets the full path of the current folder as the text in the location entry. */
4404 location_entry_set_initial_text (GtkFileChooserDefault *impl)
4408 if (!impl->current_folder)
4411 if (gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4415 filename = gtk_file_system_path_to_filename (impl->file_system, impl->current_folder);
4418 text = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
4425 text = gtk_file_system_path_to_uri (impl->file_system, impl->current_folder);
4429 gboolean need_slash;
4432 len = strlen (text);
4433 need_slash = (text[len - 1] != G_DIR_SEPARATOR);
4439 slash_text = g_new (char, len + 2);
4440 strcpy (slash_text, text);
4441 slash_text[len] = G_DIR_SEPARATOR;
4442 slash_text[len + 1] = 0;
4448 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), text);
4453 /* Turns on the location entry. Can be called even if we are already in that
4457 location_switch_to_filename_entry (GtkFileChooserDefault *impl)
4459 if (impl->location_entry)
4460 gtk_widget_destroy (impl->location_entry);
4464 gtk_widget_show (impl->location_entry_box);
4468 impl->location_entry = _gtk_file_chooser_entry_new (TRUE);
4469 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
4471 gtk_entry_set_activates_default (GTK_ENTRY (impl->location_entry), TRUE);
4472 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4474 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_entry, TRUE, TRUE, 0);
4475 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->location_label), impl->location_entry);
4477 /* Configure the entry */
4479 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->current_folder);
4483 gtk_widget_show (impl->location_entry);
4484 gtk_widget_grab_focus (impl->location_entry);
4487 /* Sets a new location mode. set_buttons determines whether the toggle button
4488 * for the mode will also be changed.
4491 location_mode_set (GtkFileChooserDefault *impl,
4492 LocationMode new_mode,
4493 gboolean set_button)
4495 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4496 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4498 GtkWindow *toplevel;
4499 GtkWidget *current_focus;
4500 gboolean button_active;
4501 gboolean switch_to_file_list;
4505 case LOCATION_MODE_PATH_BAR:
4506 button_active = FALSE;
4508 /* The location_entry will disappear when we switch to path bar mode. So,
4509 * we'll focus the file list in that case, to avoid having a window with
4510 * no focused widget.
4512 toplevel = get_toplevel (GTK_WIDGET (impl));
4513 switch_to_file_list = FALSE;
4516 current_focus = gtk_window_get_focus (toplevel);
4517 if (!current_focus || current_focus == impl->location_entry)
4518 switch_to_file_list = TRUE;
4521 location_switch_to_path_bar (impl);
4523 if (switch_to_file_list)
4524 gtk_widget_grab_focus (impl->browse_files_tree_view);
4528 case LOCATION_MODE_FILENAME_ENTRY:
4529 button_active = TRUE;
4530 location_switch_to_filename_entry (impl);
4534 g_assert_not_reached ();
4540 g_signal_handlers_block_by_func (impl->location_button,
4541 G_CALLBACK (location_button_toggled_cb), impl);
4543 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impl->location_button), button_active);
4545 g_signal_handlers_unblock_by_func (impl->location_button,
4546 G_CALLBACK (location_button_toggled_cb), impl);
4550 impl->location_mode = new_mode;
4553 /* Callback used when one of the location mode buttons is toggled */
4555 location_button_toggled_cb (GtkToggleButton *toggle,
4556 GtkFileChooserDefault *impl)
4559 LocationMode new_mode;
4561 is_active = gtk_toggle_button_get_active (toggle);
4565 g_assert (impl->location_mode == LOCATION_MODE_PATH_BAR);
4566 new_mode = LOCATION_MODE_FILENAME_ENTRY;
4570 g_assert (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY);
4571 new_mode = LOCATION_MODE_PATH_BAR;
4574 location_mode_set (impl, new_mode, FALSE);
4577 /* Creates a toggle button for the location entry. */
4579 location_button_create (GtkFileChooserDefault *impl)
4584 image = gtk_image_new_from_stock (GTK_STOCK_EDIT, GTK_ICON_SIZE_BUTTON);
4585 gtk_widget_show (image);
4587 impl->location_button = g_object_new (GTK_TYPE_TOGGLE_BUTTON,
4591 g_signal_connect (impl->location_button, "toggled",
4592 G_CALLBACK (location_button_toggled_cb), impl);
4594 str = _("Type a file name");
4596 gtk_tooltips_set_tip (impl->tooltips, impl->location_button, str, NULL);
4597 atk_object_set_name (gtk_widget_get_accessible (impl->location_button), str);
4600 /* Creates the main hpaned with the widgets shared by Open and Save mode */
4602 browse_widgets_create (GtkFileChooserDefault *impl)
4608 GtkSizeGroup *size_group;
4610 /* size group is used by the [+][-] buttons and the filter combo */
4611 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
4612 vbox = gtk_vbox_new (FALSE, 12);
4614 /* Location widgets */
4615 hbox = gtk_hbox_new (FALSE, 12);
4616 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
4617 gtk_widget_show (hbox);
4619 location_button_create (impl);
4620 gtk_box_pack_start (GTK_BOX (hbox), impl->location_button, FALSE, FALSE, 0);
4624 impl->browse_path_bar = create_path_bar (impl);
4625 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
4626 gtk_widget_show_all (impl->browse_path_bar);
4627 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
4630 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
4631 g_signal_connect (impl->browse_new_folder_button, "clicked",
4632 G_CALLBACK (new_folder_button_clicked), impl);
4633 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
4635 /* Box for the location label and entry */
4637 impl->location_entry_box = gtk_hbox_new (FALSE, 12);
4638 gtk_box_pack_start (GTK_BOX (vbox), impl->location_entry_box, FALSE, FALSE, 0);
4640 impl->location_label = gtk_label_new_with_mnemonic (_("_Location:"));
4641 gtk_widget_show (impl->location_label);
4642 gtk_box_pack_start (GTK_BOX (impl->location_entry_box), impl->location_label, FALSE, FALSE, 0);
4645 hpaned = gtk_hpaned_new ();
4646 gtk_widget_show (hpaned);
4647 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
4649 widget = shortcuts_pane_create (impl, size_group);
4650 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
4651 widget = file_pane_create (impl, size_group);
4652 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
4654 g_object_unref (size_group);
4660 gtk_file_chooser_default_constructor (GType type,
4661 guint n_construct_properties,
4662 GObjectConstructParam *construct_params)
4664 GtkFileChooserDefault *impl;
4667 profile_start ("start", NULL);
4669 object = G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->constructor (type,
4670 n_construct_properties,
4672 impl = GTK_FILE_CHOOSER_DEFAULT (object);
4674 g_assert (impl->file_system);
4676 gtk_widget_push_composite_child ();
4678 /* Shortcuts model */
4680 shortcuts_model_create (impl);
4682 /* The browse widgets */
4683 impl->browse_widgets = browse_widgets_create (impl);
4684 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
4686 /* Alignment to hold extra widget */
4687 impl->extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
4688 gtk_box_pack_start (GTK_BOX (impl), impl->extra_align, FALSE, FALSE, 0);
4690 gtk_widget_pop_composite_child ();
4691 update_appearance (impl);
4693 profile_end ("end", NULL);
4698 /* Sets the extra_widget by packing it in the appropriate place */
4700 set_extra_widget (GtkFileChooserDefault *impl,
4701 GtkWidget *extra_widget)
4705 g_object_ref (extra_widget);
4706 /* FIXME: is this right ? */
4707 gtk_widget_show (extra_widget);
4710 if (impl->extra_widget)
4712 gtk_container_remove (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4713 g_object_unref (impl->extra_widget);
4716 impl->extra_widget = extra_widget;
4717 if (impl->extra_widget)
4719 gtk_container_add (GTK_CONTAINER (impl->extra_align), impl->extra_widget);
4720 gtk_widget_show (impl->extra_align);
4723 gtk_widget_hide (impl->extra_align);
4727 set_local_only (GtkFileChooserDefault *impl,
4728 gboolean local_only)
4730 if (local_only != impl->local_only)
4732 impl->local_only = local_only;
4734 if (impl->shortcuts_model && impl->file_system)
4736 shortcuts_add_volumes (impl);
4737 shortcuts_add_bookmarks (impl);
4741 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
4743 /* If we are pointing to a non-local folder, make an effort to change
4744 * back to a local folder, but it's really up to the app to not cause
4745 * such a situation, so we ignore errors.
4747 const gchar *home = g_get_home_dir ();
4748 GtkFilePath *home_path;
4753 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
4755 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
4757 gtk_file_path_free (home_path);
4763 volumes_changed_cb (GtkFileSystem *file_system,
4764 GtkFileChooserDefault *impl)
4766 shortcuts_add_volumes (impl);
4769 /* Callback used when the set of bookmarks changes in the file system */
4771 bookmarks_changed_cb (GtkFileSystem *file_system,
4772 GtkFileChooserDefault *impl)
4774 shortcuts_add_bookmarks (impl);
4776 bookmarks_check_add_sensitivity (impl);
4777 bookmarks_check_remove_sensitivity (impl);
4778 shortcuts_check_popup_sensitivity (impl);
4781 /* Sets the file chooser to multiple selection mode */
4783 set_select_multiple (GtkFileChooserDefault *impl,
4784 gboolean select_multiple,
4785 gboolean property_notify)
4787 GtkTreeSelection *selection;
4788 GtkSelectionMode mode;
4790 if (select_multiple == impl->select_multiple)
4793 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
4795 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4796 gtk_tree_selection_set_mode (selection, mode);
4798 impl->select_multiple = select_multiple;
4799 g_object_notify (G_OBJECT (impl), "select-multiple");
4801 check_preview_change (impl);
4805 set_file_system_backend (GtkFileChooserDefault *impl,
4806 const char *backend)
4808 profile_start ("start for backend", backend ? backend : "default");
4810 if (impl->file_system)
4812 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
4813 impl->volumes_changed_id = 0;
4814 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
4815 impl->bookmarks_changed_id = 0;
4816 g_object_unref (impl->file_system);
4819 impl->file_system = NULL;
4821 impl->file_system = _gtk_file_system_create (backend);
4824 GtkSettings *settings = gtk_settings_get_default ();
4825 gchar *default_backend = NULL;
4827 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
4828 if (default_backend)
4830 impl->file_system = _gtk_file_system_create (default_backend);
4831 g_free (default_backend);
4835 if (!impl->file_system)
4837 #if defined (G_OS_UNIX)
4838 impl->file_system = gtk_file_system_unix_new ();
4839 #elif defined (G_OS_WIN32)
4840 impl->file_system = gtk_file_system_win32_new ();
4842 #error "No default filesystem implementation on the platform"
4846 if (impl->file_system)
4848 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
4849 G_CALLBACK (volumes_changed_cb),
4851 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
4852 G_CALLBACK (bookmarks_changed_cb),
4856 profile_end ("end", NULL);
4859 /* This function is basically a do_all function.
4861 * It sets the visibility on all the widgets based on the current state, and
4862 * moves the custom_widget if needed.
4865 update_appearance (GtkFileChooserDefault *impl)
4867 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4868 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4872 gtk_widget_hide (impl->location_button);
4873 save_widgets_create (impl);
4875 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4876 text = _("Save in _folder:");
4878 text = _("Create in _folder:");
4880 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
4882 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4884 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
4885 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
4886 gtk_widget_show (impl->browse_widgets);
4890 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
4891 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
4892 gtk_widget_hide (impl->browse_widgets);
4895 gtk_widget_show (impl->browse_new_folder_button);
4897 if (impl->select_multiple)
4899 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
4900 "Re-setting to single selection mode.");
4901 set_select_multiple (impl, FALSE, TRUE);
4904 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
4905 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4907 gtk_widget_show (impl->location_button);
4908 save_widgets_destroy (impl);
4909 gtk_widget_show (impl->browse_widgets);
4910 location_mode_set (impl, impl->location_mode, TRUE);
4913 if (impl->location_entry)
4914 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->action);
4916 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
4917 gtk_widget_hide (impl->browse_new_folder_button);
4919 gtk_widget_show (impl->browse_new_folder_button);
4921 /* This *is* needed; we need to redraw the file list because the "sensitivity"
4922 * of files may change depending whether we are in a file or folder-only mode.
4924 gtk_widget_queue_draw (impl->browse_files_tree_view);
4926 g_signal_emit_by_name (impl, "default-size-changed");
4930 gtk_file_chooser_default_set_property (GObject *object,
4932 const GValue *value,
4936 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
4940 case GTK_FILE_CHOOSER_PROP_ACTION:
4942 GtkFileChooserAction action = g_value_get_enum (value);
4944 if (action != impl->action)
4946 gtk_file_chooser_default_unselect_all (GTK_FILE_CHOOSER (impl));
4948 if ((action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4949 && impl->select_multiple)
4951 g_warning ("Tried to change the file chooser action to SAVE or CREATE_FOLDER, but "
4952 "this is not allowed in multiple selection mode. Resetting the file chooser "
4953 "to single selection mode.");
4954 set_select_multiple (impl, FALSE, TRUE);
4956 impl->action = action;
4957 update_appearance (impl);
4962 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
4963 set_file_system_backend (impl, g_value_get_string (value));
4966 case GTK_FILE_CHOOSER_PROP_FILTER:
4967 set_current_filter (impl, g_value_get_object (value));
4970 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
4971 set_local_only (impl, g_value_get_boolean (value));
4974 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
4975 set_preview_widget (impl, g_value_get_object (value));
4978 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
4979 impl->preview_widget_active = g_value_get_boolean (value);
4980 update_preview_widget_visibility (impl);
4983 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
4984 impl->use_preview_label = g_value_get_boolean (value);
4985 update_preview_widget_visibility (impl);
4988 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
4989 set_extra_widget (impl, g_value_get_object (value));
4992 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
4994 gboolean select_multiple = g_value_get_boolean (value);
4995 if ((impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4998 g_warning ("Tried to set the file chooser to multiple selection mode, but this is "
4999 "not allowed in SAVE or CREATE_FOLDER modes. Ignoring the change and "
5000 "leaving the file chooser in single selection mode.");
5004 set_select_multiple (impl, select_multiple, FALSE);
5008 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5010 gboolean show_hidden = g_value_get_boolean (value);
5011 if (show_hidden != impl->show_hidden)
5013 impl->show_hidden = show_hidden;
5015 if (impl->browse_files_model)
5016 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
5021 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5023 gboolean do_overwrite_confirmation = g_value_get_boolean (value);
5024 impl->do_overwrite_confirmation = do_overwrite_confirmation;
5029 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5035 gtk_file_chooser_default_get_property (GObject *object,
5040 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
5044 case GTK_FILE_CHOOSER_PROP_ACTION:
5045 g_value_set_enum (value, impl->action);
5048 case GTK_FILE_CHOOSER_PROP_FILTER:
5049 g_value_set_object (value, impl->current_filter);
5052 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
5053 g_value_set_boolean (value, impl->local_only);
5056 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
5057 g_value_set_object (value, impl->preview_widget);
5060 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
5061 g_value_set_boolean (value, impl->preview_widget_active);
5064 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
5065 g_value_set_boolean (value, impl->use_preview_label);
5068 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
5069 g_value_set_object (value, impl->extra_widget);
5072 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
5073 g_value_set_boolean (value, impl->select_multiple);
5076 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
5077 g_value_set_boolean (value, impl->show_hidden);
5080 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
5081 g_value_set_boolean (value, impl->do_overwrite_confirmation);
5085 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
5090 /* Removes the settings signal handler. It's safe to call multiple times */
5092 remove_settings_signal (GtkFileChooserDefault *impl,
5095 if (impl->settings_signal_id)
5097 GtkSettings *settings;
5099 settings = gtk_settings_get_for_screen (screen);
5100 g_signal_handler_disconnect (settings,
5101 impl->settings_signal_id);
5102 impl->settings_signal_id = 0;
5107 gtk_file_chooser_default_dispose (GObject *object)
5110 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
5112 if (impl->extra_widget)
5114 g_object_unref (impl->extra_widget);
5115 impl->extra_widget = NULL;
5118 if (impl->volumes_changed_id > 0)
5120 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
5121 impl->volumes_changed_id = 0;
5124 if (impl->bookmarks_changed_id > 0)
5126 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
5127 impl->bookmarks_changed_id = 0;
5130 pending_select_paths_free (impl);
5132 /* cancel all pending operations */
5133 if (impl->pending_handles)
5135 for (l = impl->pending_handles; l; l = l->next)
5137 GtkFileSystemHandle *handle =l->data;
5138 gtk_file_system_cancel_operation (handle);
5140 g_slist_free (impl->pending_handles);
5141 impl->pending_handles = NULL;
5144 if (impl->reload_icon_handles)
5146 for (l = impl->reload_icon_handles; l; l = l->next)
5148 GtkFileSystemHandle *handle =l->data;
5149 gtk_file_system_cancel_operation (handle);
5151 g_slist_free (impl->reload_icon_handles);
5152 impl->reload_icon_handles = NULL;
5155 if (impl->loading_shortcuts)
5157 for (l = impl->loading_shortcuts; l; l = l->next)
5159 GtkFileSystemHandle *handle =l->data;
5160 gtk_file_system_cancel_operation (handle);
5162 g_slist_free (impl->loading_shortcuts);
5163 impl->loading_shortcuts = NULL;
5166 if (impl->file_list_drag_data_received_handle)
5168 gtk_file_system_cancel_operation (impl->file_list_drag_data_received_handle);
5169 impl->file_list_drag_data_received_handle = NULL;
5172 if (impl->update_current_folder_handle)
5174 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
5175 impl->update_current_folder_handle = NULL;
5178 if (impl->show_and_select_paths_handle)
5180 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5181 impl->show_and_select_paths_handle = NULL;
5184 if (impl->should_respond_get_info_handle)
5186 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
5187 impl->should_respond_get_info_handle = NULL;
5190 if (impl->update_from_entry_handle)
5192 gtk_file_system_cancel_operation (impl->update_from_entry_handle);
5193 impl->update_from_entry_handle = NULL;
5196 if (impl->shortcuts_activate_iter_handle)
5198 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
5199 impl->shortcuts_activate_iter_handle = NULL;
5202 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
5204 G_OBJECT_CLASS (_gtk_file_chooser_default_parent_class)->dispose (object);
5207 /* We override show-all since we have internal widgets that
5208 * shouldn't be shown when you call show_all(), like the filter
5212 gtk_file_chooser_default_show_all (GtkWidget *widget)
5214 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
5216 gtk_widget_show (widget);
5218 if (impl->extra_widget)
5219 gtk_widget_show_all (impl->extra_widget);
5222 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
5223 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
5226 toplevel_set_focus_cb (GtkWindow *window,
5228 GtkFileChooserDefault *impl)
5230 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
5233 /* We monitor the focus widget on our toplevel to be able to know which widget
5234 * was last focused at the time our "should_respond" method gets called.
5237 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
5238 GtkWidget *previous_toplevel)
5240 GtkFileChooserDefault *impl;
5241 GtkWidget *toplevel;
5243 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5245 if (previous_toplevel)
5247 g_assert (impl->toplevel_set_focus_id != 0);
5248 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
5249 impl->toplevel_set_focus_id = 0;
5250 impl->toplevel_last_focus_widget = NULL;
5253 g_assert (impl->toplevel_set_focus_id == 0);
5255 toplevel = gtk_widget_get_toplevel (widget);
5256 if (GTK_IS_WINDOW (toplevel))
5258 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set_focus",
5259 G_CALLBACK (toplevel_set_focus_cb), impl);
5260 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
5264 /* Changes the icons wherever it is needed */
5266 change_icon_theme (GtkFileChooserDefault *impl)
5268 GtkSettings *settings;
5271 profile_start ("start", NULL);
5273 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5275 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
5276 impl->icon_size = MAX (width, height);
5278 impl->icon_size = FALLBACK_ICON_SIZE;
5280 shortcuts_reload_icons (impl);
5281 gtk_widget_queue_resize (impl->browse_files_tree_view);
5283 profile_end ("end", NULL);
5286 /* Callback used when a GtkSettings value changes */
5288 settings_notify_cb (GObject *object,
5290 GtkFileChooserDefault *impl)
5294 profile_start ("start", NULL);
5296 name = g_param_spec_get_name (pspec);
5298 if (strcmp (name, "gtk-icon-theme-name") == 0
5299 || strcmp (name, "gtk-icon-sizes") == 0)
5300 change_icon_theme (impl);
5302 profile_end ("end", NULL);
5305 /* Installs a signal handler for GtkSettings so that we can monitor changes in
5309 check_icon_theme (GtkFileChooserDefault *impl)
5311 GtkSettings *settings;
5313 profile_start ("start", NULL);
5315 if (impl->settings_signal_id)
5317 profile_end ("end", NULL);
5321 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5323 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
5324 impl->settings_signal_id = g_signal_connect (settings, "notify",
5325 G_CALLBACK (settings_notify_cb), impl);
5327 change_icon_theme (impl);
5330 profile_end ("end", NULL);
5334 gtk_file_chooser_default_style_set (GtkWidget *widget,
5335 GtkStyle *previous_style)
5337 GtkFileChooserDefault *impl;
5339 profile_start ("start", NULL);
5341 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5343 profile_msg (" parent class style_set start", NULL);
5344 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set)
5345 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->style_set (widget, previous_style);
5346 profile_msg (" parent class style_set end", NULL);
5348 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
5349 change_icon_theme (impl);
5351 profile_msg (" emit default-size-changed start", NULL);
5352 g_signal_emit_by_name (widget, "default-size-changed");
5353 profile_msg (" emit default-size-changed end", NULL);
5355 profile_end ("end", NULL);
5359 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
5360 GdkScreen *previous_screen)
5362 GtkFileChooserDefault *impl;
5364 profile_start ("start", NULL);
5366 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5368 if (GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed)
5369 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->screen_changed (widget, previous_screen);
5371 remove_settings_signal (impl, previous_screen);
5372 check_icon_theme (impl);
5374 g_signal_emit_by_name (widget, "default-size-changed");
5376 profile_end ("end", NULL);
5380 get_is_file_filtered (GtkFileChooserDefault *impl,
5381 const GtkFilePath *path,
5382 GtkFileInfo *file_info)
5384 GtkFileFilterInfo filter_info;
5385 GtkFileFilterFlags needed;
5388 if (!impl->current_filter)
5391 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
5393 needed = gtk_file_filter_get_needed (impl->current_filter);
5395 filter_info.display_name = gtk_file_info_get_display_name (file_info);
5396 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
5398 if (needed & GTK_FILE_FILTER_FILENAME)
5400 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
5401 if (filter_info.filename)
5402 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
5405 filter_info.filename = NULL;
5407 if (needed & GTK_FILE_FILTER_URI)
5409 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
5410 if (filter_info.uri)
5411 filter_info.contains |= GTK_FILE_FILTER_URI;
5414 filter_info.uri = NULL;
5416 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
5418 if (filter_info.filename)
5419 g_free ((gchar *)filter_info.filename);
5420 if (filter_info.uri)
5421 g_free ((gchar *)filter_info.uri);
5427 settings_load (GtkFileChooserDefault *impl)
5429 GtkFileChooserSettings *settings;
5430 LocationMode location_mode;
5431 gboolean show_hidden;
5432 gboolean expand_folders;
5434 settings = _gtk_file_chooser_settings_new ();
5436 location_mode = _gtk_file_chooser_settings_get_location_mode (settings);
5437 show_hidden = _gtk_file_chooser_settings_get_show_hidden (settings);
5438 expand_folders = _gtk_file_chooser_settings_get_expand_folders (settings);
5440 g_object_unref (settings);
5442 location_mode_set (impl, location_mode, TRUE);
5443 gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (impl), show_hidden);
5444 impl->expand_folders = expand_folders;
5445 if (impl->save_expander)
5446 gtk_expander_set_expanded (GTK_EXPANDER (impl->save_expander), expand_folders);
5450 settings_save (GtkFileChooserDefault *impl)
5452 GtkFileChooserSettings *settings;
5454 settings = _gtk_file_chooser_settings_new ();
5456 _gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
5457 _gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
5458 _gtk_file_chooser_settings_set_expand_folders (settings, impl->expand_folders);
5461 _gtk_file_chooser_settings_save (settings, NULL);
5463 g_object_unref (settings);
5466 /* GtkWidget::map method */
5468 gtk_file_chooser_default_map (GtkWidget *widget)
5470 GtkFileChooserDefault *impl;
5471 char *current_working_dir;
5473 profile_start ("start", NULL);
5475 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5477 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->map (widget);
5479 switch (impl->reload_state)
5482 /* The user didn't explicitly give us a folder to display, so we'll use the cwd */
5483 current_working_dir = g_get_current_dir ();
5484 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), current_working_dir);
5485 g_free (current_working_dir);
5488 case RELOAD_HAS_FOLDER:
5489 /* Nothing; we are already loading or loaded, so we don't need to reload */
5492 case RELOAD_WAS_UNMAPPED:
5493 /* Just reload the current folder; else continue the pending load. */
5494 if (impl->current_folder)
5496 pending_select_paths_store_selection (impl);
5497 change_folder_and_display_error (impl, impl->current_folder);
5502 g_assert_not_reached ();
5505 bookmarks_changed_cb (impl->file_system, impl);
5507 settings_load (impl);
5509 profile_end ("end", NULL);
5512 /* GtkWidget::unmap method */
5514 gtk_file_chooser_default_unmap (GtkWidget *widget)
5516 GtkFileChooserDefault *impl;
5518 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
5520 settings_save (impl);
5522 GTK_WIDGET_CLASS (_gtk_file_chooser_default_parent_class)->unmap (widget);
5524 impl->reload_state = RELOAD_WAS_UNMAPPED;
5528 list_model_filter_func (GtkFileSystemModel *model,
5530 const GtkFileInfo *file_info,
5533 GtkFileChooserDefault *impl = user_data;
5535 if (!impl->current_filter)
5538 if (gtk_file_info_get_is_folder (file_info))
5541 return !get_is_file_filtered (impl, path, (GtkFileInfo *) file_info);
5545 install_list_model_filter (GtkFileChooserDefault *impl)
5547 GtkFileSystemModelFilter filter;
5550 g_assert (impl->browse_files_model != NULL);
5552 if (impl->current_filter)
5554 filter = list_model_filter_func;
5563 _gtk_file_system_model_set_filter (impl->browse_files_model,
5568 #define COMPARE_DIRECTORIES \
5569 GtkFileChooserDefault *impl = user_data; \
5570 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
5571 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
5572 gboolean dir_a, dir_b; \
5575 dir_a = gtk_file_info_get_is_folder (info_a); \
5577 return impl->list_sort_ascending ? -1 : 1; \
5580 dir_b = gtk_file_info_get_is_folder (info_b); \
5582 return impl->list_sort_ascending ? 1 : -1; \
5584 if (dir_a != dir_b) \
5585 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
5587 /* Sort callback for the filename column */
5589 name_sort_func (GtkTreeModel *model,
5594 COMPARE_DIRECTORIES;
5596 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
5599 /* Sort callback for the size column */
5601 size_sort_func (GtkTreeModel *model,
5606 COMPARE_DIRECTORIES;
5609 gint64 size_a = gtk_file_info_get_size (info_a);
5610 gint64 size_b = gtk_file_info_get_size (info_b);
5612 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
5616 /* Sort callback for the mtime column */
5618 mtime_sort_func (GtkTreeModel *model,
5623 COMPARE_DIRECTORIES;
5626 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
5627 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
5629 return ta > tb ? -1 : (ta == tb ? 0 : 1);
5633 /* Callback used when the sort column changes. We cache the sort order for use
5634 * in name_sort_func().
5637 list_sort_column_changed_cb (GtkTreeSortable *sortable,
5638 GtkFileChooserDefault *impl)
5640 GtkSortType sort_type;
5642 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
5643 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
5647 set_busy_cursor (GtkFileChooserDefault *impl,
5650 GtkWindow *toplevel;
5651 GdkDisplay *display;
5654 toplevel = get_toplevel (GTK_WIDGET (impl));
5655 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
5658 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
5661 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
5665 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
5666 gdk_display_flush (display);
5669 gdk_cursor_unref (cursor);
5672 /* Creates a sort model to wrap the file system model and sets it on the tree view */
5674 load_set_model (GtkFileChooserDefault *impl)
5676 profile_start ("start", NULL);
5678 g_assert (impl->browse_files_model != NULL);
5679 g_assert (impl->sort_model == NULL);
5681 profile_msg (" gtk_tree_model_sort_new_with_model start", NULL);
5682 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
5683 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
5684 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
5685 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
5686 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
5687 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
5688 impl->list_sort_ascending = TRUE;
5689 profile_msg (" gtk_tree_model_sort_new_with_model end", NULL);
5691 g_signal_connect (impl->sort_model, "sort_column_changed",
5692 G_CALLBACK (list_sort_column_changed_cb), impl);
5694 profile_msg (" gtk_tree_view_set_model start", NULL);
5695 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
5696 GTK_TREE_MODEL (impl->sort_model));
5697 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
5698 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
5699 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
5700 profile_msg (" gtk_tree_view_set_model end", NULL);
5702 profile_end ("end", NULL);
5705 /* Timeout callback used when the loading timer expires */
5707 load_timeout_cb (gpointer data)
5709 GtkFileChooserDefault *impl;
5711 profile_start ("start", NULL);
5713 impl = GTK_FILE_CHOOSER_DEFAULT (data);
5714 g_assert (impl->load_state == LOAD_PRELOAD);
5715 g_assert (impl->load_timeout_id != 0);
5716 g_assert (impl->browse_files_model != NULL);
5718 impl->load_timeout_id = 0;
5719 impl->load_state = LOAD_LOADING;
5721 load_set_model (impl);
5723 profile_end ("end", NULL);
5728 /* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
5730 load_setup_timer (GtkFileChooserDefault *impl)
5732 g_assert (impl->load_timeout_id == 0);
5733 g_assert (impl->load_state != LOAD_PRELOAD);
5735 impl->load_timeout_id = gdk_threads_add_timeout (MAX_LOADING_TIME, load_timeout_cb, impl);
5736 impl->load_state = LOAD_PRELOAD;
5739 /* Removes the load timeout and switches to the LOAD_FINISHED state */
5741 load_remove_timer (GtkFileChooserDefault *impl)
5743 if (impl->load_timeout_id != 0)
5745 g_assert (impl->load_state == LOAD_PRELOAD);
5747 g_source_remove (impl->load_timeout_id);
5748 impl->load_timeout_id = 0;
5749 impl->load_state = LOAD_EMPTY;
5752 g_assert (impl->load_state == LOAD_EMPTY ||
5753 impl->load_state == LOAD_LOADING ||
5754 impl->load_state == LOAD_FINISHED);
5757 /* Selects the first row in the file list */
5759 browse_files_select_first_row (GtkFileChooserDefault *impl)
5763 if (!impl->sort_model)
5766 path = gtk_tree_path_new_from_indices (0, -1);
5767 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
5768 gtk_tree_path_free (path);
5771 struct center_selected_row_closure {
5772 GtkFileChooserDefault *impl;
5773 gboolean already_centered;
5776 /* Callback used from gtk_tree_selection_selected_foreach(); centers the
5777 * selected row in the tree view.
5780 center_selected_row_foreach_cb (GtkTreeModel *model,
5785 struct center_selected_row_closure *closure;
5788 if (closure->already_centered)
5791 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (closure->impl->browse_files_tree_view), path, NULL, TRUE, 0.5, 0.0);
5792 closure->already_centered = TRUE;
5795 /* Centers the selected row in the tree view */
5797 browse_files_center_selected_row (GtkFileChooserDefault *impl)
5799 struct center_selected_row_closure closure;
5800 GtkTreeSelection *selection;
5802 closure.impl = impl;
5803 closure.already_centered = FALSE;
5805 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5806 gtk_tree_selection_selected_foreach (selection, center_selected_row_foreach_cb, &closure);
5809 struct ShowAndSelectPathsData
5811 GtkFileChooserDefault *impl;
5816 show_and_select_paths_finished_loading (GtkFileFolder *folder,
5819 gboolean have_hidden;
5820 gboolean have_filtered;
5822 struct ShowAndSelectPathsData *data = user_data;
5824 have_hidden = FALSE;
5825 have_filtered = FALSE;
5827 for (l = data->paths; l; l = l->next)
5829 const GtkFilePath *path;
5835 info = gtk_file_folder_get_info (folder, path, NULL);
5839 have_hidden = gtk_file_info_get_is_hidden (info);
5842 have_filtered = !gtk_file_info_get_is_folder (info) && get_is_file_filtered (data->impl, path, info);
5844 gtk_file_info_free (info);
5846 if (have_hidden && have_filtered)
5847 break; /* we now have all the information we need */
5851 g_signal_handlers_disconnect_by_func (folder,
5852 show_and_select_paths_finished_loading,
5855 g_object_unref (folder);
5858 g_object_set (data->impl, "show-hidden", TRUE, NULL);
5861 set_current_filter (data->impl, NULL);
5863 for (l = data->paths; l; l = l->next)
5865 const GtkFilePath *path;
5868 _gtk_file_system_model_path_do (data->impl->browse_files_model, path,
5869 select_func, data->impl);
5872 browse_files_center_selected_row (data->impl);
5874 g_object_unref (data->impl);
5875 gtk_file_paths_free (data->paths);
5880 show_and_select_paths_get_folder_cb (GtkFileSystemHandle *handle,
5881 GtkFileFolder *folder,
5882 const GError *error,
5885 gboolean cancelled = handle->cancelled;
5886 struct ShowAndSelectPathsData *data = user_data;
5888 if (data->impl->show_and_select_paths_handle != handle)
5891 data->impl->show_and_select_paths_handle = NULL;
5893 if (cancelled || error)
5896 g_object_unref (handle);
5898 if (gtk_file_folder_is_finished_loading (folder))
5899 show_and_select_paths_finished_loading (folder, user_data);
5901 g_signal_connect (folder, "finished-loading",
5902 G_CALLBACK (show_and_select_paths_finished_loading),
5908 g_object_unref (data->impl);
5909 gtk_file_paths_free (data->paths);
5912 g_object_unref (handle);
5916 show_and_select_paths (GtkFileChooserDefault *impl,
5917 const GtkFilePath *parent_path,
5921 struct ShowAndSelectPathsData *info;
5923 profile_start ("start", NULL);
5927 profile_end ("end", NULL);
5931 info = g_new (struct ShowAndSelectPathsData, 1);
5932 info->impl = g_object_ref (impl);
5933 info->paths = gtk_file_paths_copy (paths);
5935 if (impl->show_and_select_paths_handle)
5936 gtk_file_system_cancel_operation (impl->show_and_select_paths_handle);
5938 impl->show_and_select_paths_handle =
5939 gtk_file_system_get_folder (impl->file_system, parent_path,
5940 GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_IS_HIDDEN,
5941 show_and_select_paths_get_folder_cb, info);
5943 profile_end ("end", NULL);
5947 /* Processes the pending operation when a folder is finished loading */
5949 pending_select_paths_process (GtkFileChooserDefault *impl)
5951 g_assert (impl->load_state == LOAD_FINISHED);
5952 g_assert (impl->browse_files_model != NULL);
5953 g_assert (impl->sort_model != NULL);
5955 if (impl->pending_select_paths)
5958 show_and_select_paths (impl, impl->current_folder, impl->pending_select_paths, NULL);
5959 pending_select_paths_free (impl);
5960 browse_files_center_selected_row (impl);
5964 /* We only select the first row if the chooser is actually mapped ---
5965 * selecting the first row is to help the user when he is interacting with
5966 * the chooser, but sometimes a chooser works not on behalf of the user,
5967 * but rather on behalf of something else like GtkFileChooserButton. In
5968 * that case, the chooser's selection should be what the caller expects,
5969 * as the user can't see that something else got selected. See bug #165264.
5971 * Also, we don't select the first file if we are not in OPEN mode. Doing
5972 * so would change the contents of the filename entry for SAVE or
5973 * CREATE_FOLDER, which is undesired; in SELECT_FOLDER, we don't want to
5974 * select a *different* folder from the one into which the user just
5977 if (GTK_WIDGET_MAPPED (impl) && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
5978 browse_files_select_first_row (impl);
5981 g_assert (impl->pending_select_paths == NULL);
5984 /* Callback used when the file system model finishes loading */
5986 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
5987 GtkFileChooserDefault *impl)
5989 profile_start ("start", NULL);
5991 if (impl->load_state == LOAD_PRELOAD)
5993 load_remove_timer (impl);
5994 load_set_model (impl);
5996 else if (impl->load_state == LOAD_LOADING)
6002 /* We can't g_assert_not_reached(), as something other than us may have
6003 * initiated a folder reload. See #165556.
6005 profile_end ("end", NULL);
6009 g_assert (impl->load_timeout_id == 0);
6011 impl->load_state = LOAD_FINISHED;
6013 pending_select_paths_process (impl);
6014 set_busy_cursor (impl, FALSE);
6015 #ifdef PROFILE_FILE_CHOOSER
6016 access ("MARK: *** FINISHED LOADING", F_OK);
6019 profile_end ("end", NULL);
6022 /* Gets rid of the old list model and creates a new one for the current folder */
6024 set_list_model (GtkFileChooserDefault *impl,
6027 g_assert (impl->current_folder != NULL);
6029 profile_start ("start", NULL);
6031 load_remove_timer (impl); /* This changes the state to LOAD_EMPTY */
6033 if (impl->browse_files_model)
6035 g_object_unref (impl->browse_files_model);
6036 impl->browse_files_model = NULL;
6039 if (impl->sort_model)
6041 g_object_unref (impl->sort_model);
6042 impl->sort_model = NULL;
6045 set_busy_cursor (impl, TRUE);
6046 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
6048 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
6049 impl->current_folder, 0,
6052 if (!impl->browse_files_model)
6054 set_busy_cursor (impl, FALSE);
6055 profile_end ("end", NULL);
6059 load_setup_timer (impl); /* This changes the state to LOAD_PRELOAD */
6061 g_signal_connect (impl->browse_files_model, "finished-loading",
6062 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
6064 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
6066 install_list_model_filter (impl);
6068 profile_end ("end", NULL);
6073 struct update_chooser_entry_selected_foreach_closure {
6075 GtkTreeIter first_selected_iter;
6079 compare_utf8_filenames (const gchar *a,
6082 gchar *a_folded, *b_folded;
6085 a_folded = g_utf8_strdown (a, -1);
6086 b_folded = g_utf8_strdown (b, -1);
6088 retval = strcmp (a_folded, b_folded);
6097 update_chooser_entry_selected_foreach (GtkTreeModel *model,
6102 struct update_chooser_entry_selected_foreach_closure *closure;
6105 closure->num_selected++;
6107 if (closure->num_selected == 1)
6108 closure->first_selected_iter = *iter;
6112 update_chooser_entry (GtkFileChooserDefault *impl)
6114 GtkTreeSelection *selection;
6115 struct update_chooser_entry_selected_foreach_closure closure;
6116 const char *file_part;
6118 if (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6119 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6120 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6121 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6122 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)))
6125 g_assert (impl->location_entry != NULL);
6127 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6128 closure.num_selected = 0;
6129 gtk_tree_selection_selected_foreach (selection, update_chooser_entry_selected_foreach, &closure);
6133 if (closure.num_selected == 0)
6135 goto maybe_clear_entry;
6137 else if (closure.num_selected == 1)
6139 GtkTreeIter child_iter;
6140 const GtkFileInfo *info;
6141 gboolean change_entry;
6143 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
6145 &closure.first_selected_iter);
6147 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
6149 /* If the cursor moved to the row of the newly created folder,
6150 * retrieving info will return NULL.
6155 g_free (impl->browse_files_last_selected_name);
6156 impl->browse_files_last_selected_name = g_strdup (gtk_file_info_get_display_name (info));
6158 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6159 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6160 change_entry = !gtk_file_info_get_is_folder (info); /* We don't want the name to change when clicking on a folder... */
6162 change_entry = TRUE; /* ... unless we are in one of the folder modes */
6165 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), impl->browse_files_last_selected_name);
6171 g_assert (!(impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6172 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER));
6174 /* Multiple selection, so just clear the entry. */
6176 g_free (impl->browse_files_last_selected_name);
6177 impl->browse_files_last_selected_name = NULL;
6179 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6185 if (impl->browse_files_last_selected_name)
6187 const char *entry_text;
6189 gboolean clear_entry;
6191 entry_text = gtk_entry_get_text (GTK_ENTRY (impl->location_entry));
6192 len = strlen (entry_text);
6195 /* The file chooser entry may have appended a "/" to its text. So
6196 * take it out, and compare the result to the old selection.
6198 if (entry_text[len - 1] == G_DIR_SEPARATOR)
6202 tmp = g_strndup (entry_text, len - 1);
6203 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, tmp) == 0);
6207 clear_entry = (compare_utf8_filenames (impl->browse_files_last_selected_name, entry_text) == 0);
6210 clear_entry = FALSE;
6213 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
6218 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
6219 const GtkFilePath *path,
6222 return gtk_file_chooser_default_update_current_folder (chooser, path, FALSE, error);
6226 struct UpdateCurrentFolderData
6228 GtkFileChooserDefault *impl;
6230 gboolean keep_trail;
6231 GtkFilePath *original_path;
6232 GError *original_error;
6236 update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
6237 const GtkFileInfo *info,
6238 const GError *error,
6241 gboolean cancelled = handle->cancelled;
6242 struct UpdateCurrentFolderData *data = user_data;
6243 GtkFileChooserDefault *impl = data->impl;
6245 if (handle != impl->update_current_folder_handle)
6248 impl->update_current_folder_handle = NULL;
6249 impl->reload_state = RELOAD_EMPTY;
6251 set_busy_cursor (impl, FALSE);
6258 GtkFilePath *parent_path;
6260 if (!data->original_path)
6262 data->original_path = gtk_file_path_copy (data->path);
6263 data->original_error = g_error_copy (error);
6266 /* get parent path and try to change the folder to that */
6267 if (gtk_file_system_get_parent (impl->file_system, data->path, &parent_path, NULL) &&
6268 parent_path != NULL)
6270 gtk_file_path_free (data->path);
6271 data->path = parent_path;
6273 g_object_unref (handle);
6275 /* restart the update current folder operation */
6276 impl->reload_state = RELOAD_HAS_FOLDER;
6278 impl->update_current_folder_handle =
6279 gtk_file_system_get_info (impl->file_system, data->path,
6280 GTK_FILE_INFO_IS_FOLDER,
6281 update_current_folder_get_info_cb,
6284 set_busy_cursor (impl, TRUE);
6290 /* error and bail out */
6291 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6293 gtk_file_path_free (data->original_path);
6299 if (data->original_path)
6301 error_changing_folder_dialog (impl, data->original_path, data->original_error);
6303 gtk_file_path_free (data->original_path);
6306 if (!gtk_file_info_get_is_folder (info))
6309 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), data->path, data->keep_trail, NULL))
6312 if (impl->current_folder != data->path)
6314 if (impl->current_folder)
6315 gtk_file_path_free (impl->current_folder);
6317 impl->current_folder = gtk_file_path_copy (data->path);
6319 impl->reload_state = RELOAD_HAS_FOLDER;
6322 /* Update the widgets that may trigger a folder change themselves. */
6324 if (!impl->changing_folder)
6326 impl->changing_folder = TRUE;
6328 shortcuts_update_current_folder (impl);
6330 impl->changing_folder = FALSE;
6333 /* Set the folder on the save entry */
6335 if (impl->location_entry)
6336 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->location_entry),
6337 impl->current_folder);
6339 /* Create a new list model. This is slightly evil; we store the result value
6340 * but perform more actions rather than returning immediately even if it
6341 * generates an error.
6343 set_list_model (impl, NULL);
6345 /* Refresh controls */
6347 shortcuts_find_current_folder (impl);
6349 g_signal_emit_by_name (impl, "current-folder-changed", 0);
6351 check_preview_change (impl);
6352 bookmarks_check_add_sensitivity (impl);
6354 g_signal_emit_by_name (impl, "selection-changed", 0);
6357 gtk_file_path_free (data->path);
6360 g_object_unref (handle);
6364 gtk_file_chooser_default_update_current_folder (GtkFileChooser *chooser,
6365 const GtkFilePath *path,
6366 gboolean keep_trail,
6369 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6370 struct UpdateCurrentFolderData *data;
6372 profile_start ("start", (char *) path);
6374 g_assert (path != NULL);
6376 if (impl->local_only &&
6377 !gtk_file_system_path_is_local (impl->file_system, path))
6380 GTK_FILE_CHOOSER_ERROR,
6381 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
6382 _("Cannot change to folder because it is not local"));
6384 profile_end ("end - not local", (char *) path);
6388 if (impl->update_current_folder_handle)
6389 gtk_file_system_cancel_operation (impl->update_current_folder_handle);
6391 /* Test validity of path here. */
6392 data = g_new0 (struct UpdateCurrentFolderData, 1);
6394 data->path = gtk_file_path_copy (path);
6395 data->keep_trail = keep_trail;
6397 impl->reload_state = RELOAD_HAS_FOLDER;
6399 impl->update_current_folder_handle =
6400 gtk_file_system_get_info (impl->file_system, path, GTK_FILE_INFO_IS_FOLDER,
6401 update_current_folder_get_info_cb,
6404 set_busy_cursor (impl, TRUE);
6406 profile_end ("end", NULL);
6410 static GtkFilePath *
6411 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
6413 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6415 if (impl->reload_state == RELOAD_EMPTY)
6417 char *current_working_dir;
6420 /* We are unmapped, or we had an error while loading the last folder. We'll return
6421 * the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
6422 * explicitly calls set_current_folder() on us.
6424 current_working_dir = g_get_current_dir ();
6425 path = gtk_file_system_filename_to_path (impl->file_system, current_working_dir);
6426 g_free (current_working_dir);
6430 return gtk_file_path_copy (impl->current_folder);
6434 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
6437 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6439 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6440 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
6442 pending_select_paths_free (impl);
6443 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), name);
6447 select_func (GtkFileSystemModel *model,
6452 GtkFileChooserDefault *impl = user_data;
6453 GtkTreeSelection *selection;
6454 GtkTreeIter sorted_iter;
6456 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6458 gtk_tree_model_sort_convert_child_iter_to_iter (impl->sort_model, &sorted_iter, iter);
6459 gtk_tree_selection_select_iter (selection, &sorted_iter);
6463 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
6464 const GtkFilePath *path,
6467 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6468 GtkFilePath *parent_path;
6471 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
6475 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
6477 if (impl->load_state == LOAD_EMPTY)
6481 g_assert (impl->current_folder != NULL);
6483 same_path = gtk_file_path_compare (parent_path, impl->current_folder) == 0;
6486 if (same_path && impl->load_state == LOAD_FINISHED)
6491 paths.data = (gpointer) path;
6494 result = show_and_select_paths (impl, parent_path, &paths, error);
6495 gtk_file_path_free (parent_path);
6499 pending_select_paths_add (impl, path);
6505 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
6506 gtk_file_path_free (parent_path);
6510 gtk_file_path_free (parent_path);
6515 unselect_func (GtkFileSystemModel *model,
6520 GtkFileChooserDefault *impl = user_data;
6521 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
6522 GtkTreePath *sorted_path;
6524 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
6526 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
6528 gtk_tree_path_free (sorted_path);
6532 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
6533 const GtkFilePath *path)
6535 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6537 if (!impl->browse_files_model)
6540 _gtk_file_system_model_path_do (impl->browse_files_model, path,
6541 unselect_func, impl);
6545 maybe_select (GtkTreeModel *model,
6550 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
6551 GtkTreeSelection *selection;
6552 const GtkFileInfo *info;
6555 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6557 info = get_list_file_info (impl, iter);
6558 is_folder = gtk_file_info_get_is_folder (info);
6560 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
6561 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
6562 gtk_tree_selection_select_iter (selection, iter);
6564 gtk_tree_selection_unselect_iter (selection, iter);
6570 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
6572 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6573 if (impl->select_multiple)
6574 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->sort_model),
6575 maybe_select, impl);
6579 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
6581 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6582 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6584 gtk_tree_selection_unselect_all (selection);
6585 pending_select_paths_free (impl);
6588 /* Checks whether the filename entry for the Save modes contains a well-formed filename.
6590 * is_well_formed_ret - whether what the user typed passes gkt_file_system_make_path()
6592 * is_empty_ret - whether the file entry is totally empty
6594 * is_file_part_empty_ret - whether the file part is empty (will be if user types "foobar/", and
6595 * the path will be "$cwd/foobar")
6598 check_save_entry (GtkFileChooserDefault *impl,
6599 GtkFilePath **path_ret,
6600 gboolean *is_well_formed_ret,
6601 gboolean *is_empty_ret,
6602 gboolean *is_file_part_empty_ret,
6603 gboolean *is_folder)
6605 GtkFileChooserEntry *chooser_entry;
6606 const GtkFilePath *current_folder;
6607 const char *file_part;
6611 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6612 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
6613 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
6614 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
6615 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
6617 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
6619 if (strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry))) == 0)
6622 *is_well_formed_ret = TRUE;
6623 *is_empty_ret = TRUE;
6624 *is_file_part_empty_ret = TRUE;
6630 *is_empty_ret = FALSE;
6632 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
6633 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
6635 if (!file_part || file_part[0] == '\0')
6637 *path_ret = gtk_file_path_copy (current_folder);
6638 *is_well_formed_ret = TRUE;
6639 *is_file_part_empty_ret = TRUE;
6645 *is_file_part_empty_ret = FALSE;
6648 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
6652 error_building_filename_dialog (impl, current_folder, file_part, error);
6654 *is_well_formed_ret = FALSE;
6661 *is_well_formed_ret = TRUE;
6662 *is_folder = _gtk_file_chooser_entry_get_is_folder (chooser_entry, path);
6665 struct get_paths_closure {
6666 GtkFileChooserDefault *impl;
6668 GtkFilePath *path_from_entry;
6672 get_paths_foreach (GtkTreeModel *model,
6677 struct get_paths_closure *info;
6678 const GtkFilePath *file_path;
6679 GtkFileSystemModel *fs_model;
6680 GtkTreeIter sel_iter;
6683 fs_model = info->impl->browse_files_model;
6684 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
6686 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
6688 return; /* We are on the editable row */
6690 if (!info->path_from_entry
6691 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
6692 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
6696 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
6698 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6699 struct get_paths_closure info;
6700 GtkWindow *toplevel;
6701 GtkWidget *current_focus;
6705 info.path_from_entry = NULL;
6707 toplevel = get_toplevel (GTK_WIDGET (impl));
6709 current_focus = gtk_window_get_focus (toplevel);
6711 current_focus = NULL;
6713 if (current_focus == impl->browse_files_tree_view)
6715 GtkTreeSelection *selection;
6719 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
6720 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
6722 /* If there is no selection in the file list, we probably have this situation:
6724 * 1. The user typed a filename in the SAVE filename entry ("foo.txt").
6725 * 2. He then double-clicked on a folder ("bar") in the file list
6727 * So we want the selection to be "bar/foo.txt". Jump to the case for the
6728 * filename entry to see if that is the case.
6730 if (info.result == NULL && impl->location_entry)
6733 else if (impl->location_entry && current_focus == impl->location_entry)
6735 gboolean is_well_formed, is_empty, is_file_part_empty, is_folder;
6739 check_save_entry (impl, &info.path_from_entry, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
6744 if (!is_well_formed)
6747 if (is_file_part_empty && impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
6749 gtk_file_path_free (info.path_from_entry);
6753 g_assert (info.path_from_entry != NULL);
6754 info.result = g_slist_prepend (info.result, info.path_from_entry);
6756 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
6758 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
6762 /* The focus is on a dialog's action area button or something else */
6763 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
6764 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
6772 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
6773 * fall back to the current directory */
6774 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
6775 info.result == NULL)
6777 info.result = g_slist_prepend (info.result, _gtk_file_chooser_get_current_folder_path (chooser));
6780 return g_slist_reverse (info.result);
6783 static GtkFilePath *
6784 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
6786 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6788 if (impl->preview_path)
6789 return gtk_file_path_copy (impl->preview_path);
6794 static GtkFileSystem *
6795 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
6797 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6799 return impl->file_system;
6802 /* Shows or hides the filter widgets */
6804 show_filters (GtkFileChooserDefault *impl,
6808 gtk_widget_show (impl->filter_combo_hbox);
6810 gtk_widget_hide (impl->filter_combo_hbox);
6814 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
6815 GtkFileFilter *filter)
6817 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6820 if (g_slist_find (impl->filters, filter))
6822 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
6826 g_object_ref_sink (filter);
6827 impl->filters = g_slist_append (impl->filters, filter);
6829 name = gtk_file_filter_get_name (filter);
6831 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
6833 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
6835 if (!g_slist_find (impl->filters, impl->current_filter))
6836 set_current_filter (impl, filter);
6838 show_filters (impl, TRUE);
6842 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
6843 GtkFileFilter *filter)
6845 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6846 GtkTreeModel *model;
6850 filter_index = g_slist_index (impl->filters, filter);
6852 if (filter_index < 0)
6854 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
6858 impl->filters = g_slist_remove (impl->filters, filter);
6860 if (filter == impl->current_filter)
6863 set_current_filter (impl, impl->filters->data);
6865 set_current_filter (impl, NULL);
6868 /* Remove row from the combo box */
6869 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
6870 if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index))
6871 g_assert_not_reached ();
6873 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
6875 g_object_unref (filter);
6878 show_filters (impl, FALSE);
6882 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
6884 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6886 return g_slist_copy (impl->filters);
6889 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
6891 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
6894 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
6897 struct AddShortcutData
6899 GtkFileChooserDefault *impl;
6904 add_shortcut_get_info_cb (GtkFileSystemHandle *handle,
6905 const GtkFileInfo *info,
6906 const GError *error,
6910 gboolean cancelled = handle->cancelled;
6911 struct AddShortcutData *data = user_data;
6913 if (!g_slist_find (data->impl->loading_shortcuts, handle))
6916 data->impl->loading_shortcuts = g_slist_remove (data->impl->loading_shortcuts, handle);
6918 if (cancelled || error || !gtk_file_info_get_is_folder (info))
6921 pos = shortcuts_get_pos_for_shortcut_folder (data->impl, data->impl->num_shortcuts);
6923 shortcuts_insert_path (data->impl, pos, FALSE, NULL, data->path, NULL, FALSE, SHORTCUTS_SHORTCUTS);
6926 g_object_unref (data->impl);
6927 gtk_file_path_free (data->path);
6930 g_object_unref (handle);
6934 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
6935 const GtkFilePath *path,
6938 GtkFileSystemHandle *handle;
6939 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
6940 struct AddShortcutData *data;
6944 /* Avoid adding duplicates */
6945 pos = shortcut_find_position (impl, path);
6946 if (pos >= 0 && pos < shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR))
6950 uri = gtk_file_system_path_to_uri (impl->file_system, path);
6951 /* translators, "Shortcut" means "Bookmark" here */
6953 GTK_FILE_CHOOSER_ERROR,
6954 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6955 _("Shortcut %s already exists"),
6962 for (l = impl->loading_shortcuts; l; l = l->next)
6964 GtkFileSystemHandle *h = l->data;
6967 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
6968 if (p && !gtk_file_path_compare (path, p))
6972 uri = gtk_file_system_path_to_uri (impl->file_system, path);
6974 GTK_FILE_CHOOSER_ERROR,
6975 GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
6976 _("Shortcut %s already exists"),
6984 data = g_new0 (struct AddShortcutData, 1);
6985 data->impl = g_object_ref (impl);
6986 data->path = gtk_file_path_copy (path);
6988 handle = gtk_file_system_get_info (impl->file_system, path,
6989 GTK_FILE_INFO_IS_FOLDER,
6990 add_shortcut_get_info_cb, data);
6995 impl->loading_shortcuts = g_slist_append (impl->loading_shortcuts, handle);
6996 g_object_set_data (G_OBJECT (handle), "add-shortcut-path-key", data->path);
7002 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
7003 const GtkFilePath *path,
7006 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7013 for (l = impl->loading_shortcuts; l; l = l->next)
7015 GtkFileSystemHandle *h = l->data;
7018 p = g_object_get_data (G_OBJECT (h), "add-shortcut-path-key");
7019 if (p && !gtk_file_path_compare (path, p))
7021 impl->loading_shortcuts = g_slist_remove (impl->loading_shortcuts, h);
7022 gtk_file_system_cancel_operation (h);
7027 if (impl->num_shortcuts == 0)
7030 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7031 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7032 g_assert_not_reached ();
7034 for (i = 0; i < impl->num_shortcuts; i++)
7038 GtkFilePath *shortcut;
7040 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7041 SHORTCUTS_COL_DATA, &col_data,
7042 SHORTCUTS_COL_IS_VOLUME, &is_volume,
7044 g_assert (col_data != NULL);
7045 g_assert (!is_volume);
7047 shortcut = col_data;
7048 if (gtk_file_path_compare (shortcut, path) == 0)
7050 shortcuts_remove_rows (impl, pos + i, 1);
7051 impl->num_shortcuts--;
7055 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7056 g_assert_not_reached ();
7061 uri = gtk_file_system_path_to_uri (impl->file_system, path);
7062 /* translators, "Shortcut" means "Bookmark" here */
7064 GTK_FILE_CHOOSER_ERROR,
7065 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
7066 _("Shortcut %s does not exist"),
7074 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
7076 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
7082 if (impl->num_shortcuts == 0)
7085 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
7086 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
7087 g_assert_not_reached ();
7091 for (i = 0; i < impl->num_shortcuts; i++)
7095 GtkFilePath *shortcut;
7097 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
7098 SHORTCUTS_COL_DATA, &col_data,
7099 SHORTCUTS_COL_IS_VOLUME, &is_volume,
7101 g_assert (col_data != NULL);
7102 g_assert (!is_volume);
7104 shortcut = col_data;
7105 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
7107 if (i != impl->num_shortcuts - 1)
7109 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
7110 g_assert_not_reached ();
7114 return g_slist_reverse (list);
7117 /* Guesses a size based upon font sizes */
7119 find_good_size_from_style (GtkWidget *widget,
7123 GtkFileChooserDefault *impl;
7124 gint default_width, default_height;
7128 g_assert (widget->style != NULL);
7129 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
7131 font_size = pango_font_description_get_size (widget->style->font_desc);
7132 font_size = PANGO_PIXELS (font_size);
7134 default_width = font_size * NUM_CHARS;
7135 default_height = font_size * NUM_LINES;
7137 if (impl->preview_widget_active && impl->preview_widget)
7139 gtk_widget_size_request (impl->preview_box, &req);
7140 default_width += PREVIEW_HBOX_SPACING + req.width;
7143 if (impl->extra_widget)
7145 gtk_widget_size_request (impl->extra_align, &req);
7146 default_height += GTK_BOX (widget)->spacing + req.height;
7149 gtk_widget_size_request (widget, &req);
7150 default_width = MAX (default_width, req.width);
7151 default_height = MAX (default_height, req.height);
7153 *width = default_width;
7154 *height = default_height;
7158 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
7159 gint *default_width,
7160 gint *default_height)
7162 GtkFileChooserDefault *impl;
7164 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7165 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
7169 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
7170 gboolean *resize_horizontally,
7171 gboolean *resize_vertically)
7173 GtkFileChooserDefault *impl;
7175 g_return_if_fail (resize_horizontally != NULL);
7176 g_return_if_fail (resize_vertically != NULL);
7178 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7180 *resize_horizontally = TRUE;
7181 *resize_vertically = TRUE;
7183 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
7184 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7186 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
7188 *resize_horizontally = FALSE;
7189 *resize_vertically = FALSE;
7194 struct switch_folder_closure {
7195 GtkFileChooserDefault *impl;
7196 const GtkFilePath *path;
7200 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
7202 switch_folder_foreach_cb (GtkTreeModel *model,
7207 struct switch_folder_closure *closure;
7208 GtkTreeIter child_iter;
7212 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
7214 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
7215 closure->num_selected++;
7218 /* Changes to the selected folder in the list view */
7220 switch_to_selected_folder (GtkFileChooserDefault *impl)
7222 GtkTreeSelection *selection;
7223 struct switch_folder_closure closure;
7225 /* We do this with foreach() rather than get_selected() as we may be in
7226 * multiple selection mode
7229 closure.impl = impl;
7230 closure.path = NULL;
7231 closure.num_selected = 0;
7233 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7234 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
7236 g_assert (closure.path && closure.num_selected == 1);
7238 change_folder_and_display_error (impl, closure.path);
7241 /* Gets the GtkFileInfo for the selected row in the file list; assumes single
7244 static const GtkFileInfo *
7245 get_selected_file_info_from_file_list (GtkFileChooserDefault *impl,
7246 gboolean *had_selection)
7248 GtkTreeSelection *selection;
7249 GtkTreeIter iter, child_iter;
7250 const GtkFileInfo *info;
7252 g_assert (!impl->select_multiple);
7253 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
7254 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
7256 *had_selection = FALSE;
7260 *had_selection = TRUE;
7262 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
7266 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7270 /* Gets the display name of the selected file in the file list; assumes single
7271 * selection mode and that something is selected.
7273 static const gchar *
7274 get_display_name_from_file_list (GtkFileChooserDefault *impl)
7276 const GtkFileInfo *info;
7277 gboolean had_selection;
7279 info = get_selected_file_info_from_file_list (impl, &had_selection);
7280 g_assert (had_selection);
7281 g_assert (info != NULL);
7283 return gtk_file_info_get_display_name (info);
7287 add_custom_button_to_dialog (GtkDialog *dialog,
7288 const gchar *mnemonic_label,
7289 const gchar *stock_id,
7294 button = gtk_button_new_with_mnemonic (mnemonic_label);
7295 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
7296 gtk_button_set_image (GTK_BUTTON (button),
7297 gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON));
7298 gtk_widget_show (button);
7300 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
7303 /* Presents an overwrite confirmation dialog; returns whether we should accept
7307 confirm_dialog_should_accept_filename (GtkFileChooserDefault *impl,
7308 const gchar *file_part,
7309 const gchar *folder_display_name)
7311 GtkWindow *toplevel;
7315 toplevel = get_toplevel (GTK_WIDGET (impl));
7317 dialog = gtk_message_dialog_new (toplevel,
7318 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
7319 GTK_MESSAGE_QUESTION,
7321 _("A file named \"%s\" already exists. Do you want to replace it?"),
7323 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
7324 _("The file already exists in \"%s\". Replacing it will "
7325 "overwrite its contents."),
7326 folder_display_name);
7328 gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
7329 add_custom_button_to_dialog (GTK_DIALOG (dialog), _("_Replace"), GTK_STOCK_SAVE_AS, GTK_RESPONSE_ACCEPT);
7330 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
7332 if (toplevel->group)
7333 gtk_window_group_add_window (toplevel->group, GTK_WINDOW (dialog));
7335 response = gtk_dialog_run (GTK_DIALOG (dialog));
7337 gtk_widget_destroy (dialog);
7339 return (response == GTK_RESPONSE_ACCEPT);
7342 struct GetDisplayNameData
7344 GtkFileChooserDefault *impl;
7349 confirmation_confirm_get_info_cb (GtkFileSystemHandle *handle,
7350 const GtkFileInfo *info,
7351 const GError *error,
7354 gboolean cancelled = handle->cancelled;
7355 gboolean should_respond = FALSE;
7356 struct GetDisplayNameData *data = user_data;
7358 if (handle != data->impl->should_respond_get_info_handle)
7361 data->impl->should_respond_get_info_handle = NULL;
7367 /* Huh? Did the folder disappear? Let the caller deal with it */
7368 should_respond = TRUE;
7370 should_respond = confirm_dialog_should_accept_filename (data->impl, data->file_part, gtk_file_info_get_display_name (info));
7372 set_busy_cursor (data->impl, FALSE);
7374 g_signal_emit_by_name (data->impl, "response-requested");
7377 g_object_unref (data->impl);
7378 g_free (data->file_part);
7381 g_object_unref (handle);
7384 /* Does overwrite confirmation if appropriate, and returns whether the dialog
7385 * should respond. Can get the file part from the file list or the save entry.
7388 should_respond_after_confirm_overwrite (GtkFileChooserDefault *impl,
7389 const gchar *file_part,
7390 const GtkFilePath *parent_path)
7392 GtkFileChooserConfirmation conf;
7394 if (!impl->do_overwrite_confirmation)
7397 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
7399 g_signal_emit_by_name (impl, "confirm-overwrite", &conf);
7403 case GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM:
7405 struct GetDisplayNameData *data;
7407 g_assert (file_part != NULL);
7409 data = g_new0 (struct GetDisplayNameData, 1);
7410 data->impl = g_object_ref (impl);
7411 data->file_part = g_strdup (file_part);
7413 if (impl->should_respond_get_info_handle)
7414 gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
7416 impl->should_respond_get_info_handle =
7417 gtk_file_system_get_info (impl->file_system, parent_path,
7418 GTK_FILE_INFO_DISPLAY_NAME,
7419 confirmation_confirm_get_info_cb,
7421 set_busy_cursor (data->impl, TRUE);
7425 case GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME:
7428 case GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN:
7432 g_assert_not_reached ();
7438 action_create_folder_cb (GtkFileSystemHandle *handle,
7439 const GtkFilePath *path,
7440 const GError *error,
7443 gboolean cancelled = handle->cancelled;
7444 GtkFileChooserDefault *impl = user_data;
7446 if (!g_slist_find (impl->pending_handles, handle))
7449 impl->pending_handles = g_slist_remove (impl->pending_handles, handle);
7451 set_busy_cursor (impl, FALSE);
7457 error_creating_folder_dialog (impl, path, g_error_copy (error));
7459 g_signal_emit_by_name (impl, "response-requested");
7462 g_object_unref (impl);
7463 g_object_unref (handle);
7466 struct FileExistsData
7468 GtkFileChooserDefault *impl;
7469 gboolean file_exists_and_is_not_folder;
7470 GtkFilePath *parent_path;
7475 save_entry_get_info_cb (GtkFileSystemHandle *handle,
7476 const GtkFileInfo *info,
7477 const GError *error,
7480 gboolean parent_is_folder;
7481 gboolean cancelled = handle->cancelled;
7482 struct FileExistsData *data = user_data;
7484 if (handle != data->impl->should_respond_get_info_handle)
7487 data->impl->should_respond_get_info_handle = NULL;
7489 set_busy_cursor (data->impl, FALSE);
7495 parent_is_folder = FALSE;
7497 parent_is_folder = gtk_file_info_get_is_folder (info);
7499 if (parent_is_folder)
7501 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7503 if (data->file_exists_and_is_not_folder)
7506 const char *file_part;
7508 file_part = _gtk_file_chooser_entry_get_file_part (GTK_FILE_CHOOSER_ENTRY (data->impl->location_entry));
7509 retval = should_respond_after_confirm_overwrite (data->impl, file_part, data->parent_path);
7512 g_signal_emit_by_name (data->impl, "response-requested");
7515 g_signal_emit_by_name (data->impl, "response-requested");
7517 else /* GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER */
7519 GtkFileSystemHandle *handle;
7521 g_object_ref (data->impl);
7522 handle = gtk_file_system_create_folder (data->impl->file_system,
7524 action_create_folder_cb,
7526 data->impl->pending_handles = g_slist_append (data->impl->pending_handles, handle);
7527 set_busy_cursor (data->impl, TRUE);
7532 /* This will display an error, which is what we want */
7533 change_folder_and_display_error (data->impl, data->parent_path);
7537 g_object_unref (data->impl);
7538 gtk_file_path_free (data->path);
7539 gtk_file_path_free (data->parent_path);
7542 g_object_unref (handle);
7546 file_exists_get_info_cb (GtkFileSystemHandle *handle,
7547 const GtkFileInfo *info,
7548 const GError *error,
7551 gboolean data_ownership_taken = FALSE;
7552 gboolean cancelled = handle->cancelled;
7553 gboolean file_exists_and_is_not_folder;
7554 struct FileExistsData *data = user_data;
7556 if (handle != data->impl->file_exists_get_info_handle)
7559 data->impl->file_exists_get_info_handle = NULL;
7561 set_busy_cursor (data->impl, FALSE);
7566 file_exists_and_is_not_folder = info && !gtk_file_info_get_is_folder (info);
7568 if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
7569 /* user typed a filename; we are done */
7570 g_signal_emit_by_name (data->impl, "response-requested");
7571 else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7572 && file_exists_and_is_not_folder)
7574 /* Oops, the user typed the name of an existing path which is not
7577 error_creating_folder_over_existing_file_dialog (data->impl, data->path,
7578 g_error_copy (error));
7582 /* check that everything up to the last component exists */
7584 data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
7585 data_ownership_taken = TRUE;
7587 if (data->impl->should_respond_get_info_handle)
7588 gtk_file_system_cancel_operation (data->impl->should_respond_get_info_handle);
7590 data->impl->should_respond_get_info_handle =
7591 gtk_file_system_get_info (data->impl->file_system,
7593 GTK_FILE_INFO_IS_FOLDER,
7594 save_entry_get_info_cb,
7596 set_busy_cursor (data->impl, TRUE);
7600 if (!data_ownership_taken)
7602 g_object_unref (data->impl);
7603 gtk_file_path_free (data->path);
7604 gtk_file_path_free (data->parent_path);
7608 g_object_unref (handle);
7612 paste_text_received (GtkClipboard *clipboard,
7614 GtkFileChooserDefault *impl)
7621 path = gtk_file_system_uri_to_path (impl->file_system, text);
7624 if (!g_path_is_absolute (text))
7626 location_popup_handler (impl, text);
7630 path = gtk_file_system_filename_to_path (impl->file_system, text);
7633 location_popup_handler (impl, text);
7638 if (!gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (impl), path, NULL))
7639 location_popup_handler (impl, text);
7641 gtk_file_path_free (path);
7644 /* Handler for the "location-popup-on-paste" keybinding signal */
7646 location_popup_on_paste_handler (GtkFileChooserDefault *impl)
7648 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (impl),
7649 GDK_SELECTION_CLIPBOARD);
7650 gtk_clipboard_request_text (clipboard,
7651 (GtkClipboardTextReceivedFunc) paste_text_received,
7656 /* Implementation for GtkFileChooserEmbed::should_respond() */
7658 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
7660 GtkFileChooserDefault *impl;
7661 GtkWidget *toplevel;
7662 GtkWidget *current_focus;
7664 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7666 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
7667 g_assert (GTK_IS_WINDOW (toplevel));
7669 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
7671 if (current_focus == impl->browse_files_tree_view)
7673 /* The following array encodes what we do based on the impl->action and the
7674 * number of files selected.
7677 NOOP, /* Do nothing (don't respond) */
7678 RESPOND, /* Respond immediately */
7679 RESPOND_OR_SWITCH, /* Respond immediately if the selected item is a file; switch to it if it is a folder */
7680 ALL_FILES, /* Respond only if everything selected is a file */
7681 ALL_FOLDERS, /* Respond only if everything selected is a folder */
7682 SAVE_ENTRY, /* Go to the code for handling the save entry */
7683 NOT_REACHED /* Sanity check */
7685 static const ActionToTake what_to_do[4][3] = {
7686 /* 0 selected 1 selected many selected */
7687 /* ACTION_OPEN */ { NOOP, RESPOND_OR_SWITCH, ALL_FILES },
7688 /* ACTION_SAVE */ { SAVE_ENTRY, RESPOND_OR_SWITCH, NOT_REACHED },
7689 /* ACTION_SELECT_FOLDER */ { RESPOND, ALL_FOLDERS, ALL_FOLDERS },
7690 /* ACTION_CREATE_FOLDER */ { SAVE_ENTRY, ALL_FOLDERS, NOT_REACHED }
7694 gboolean all_files, all_folders;
7696 ActionToTake action;
7700 g_assert (impl->action >= GTK_FILE_CHOOSER_ACTION_OPEN && impl->action <= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
7702 selection_check (impl, &num_selected, &all_files, &all_folders);
7704 if (num_selected > 2)
7709 action = what_to_do [impl->action] [k];
7719 case RESPOND_OR_SWITCH:
7720 g_assert (num_selected == 1);
7724 switch_to_selected_folder (impl);
7727 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7728 return should_respond_after_confirm_overwrite (impl,
7729 get_display_name_from_file_list (impl),
7730 impl->current_folder);
7744 g_assert_not_reached ();
7747 else if ((impl->location_entry != NULL) && (current_focus == impl->location_entry))
7750 gboolean is_well_formed, is_empty, is_file_part_empty;
7753 GtkFileChooserEntry *entry;
7758 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7759 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
7760 || ((impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7761 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7762 && impl->location_mode == LOCATION_MODE_FILENAME_ENTRY));
7764 entry = GTK_FILE_CHOOSER_ENTRY (impl->location_entry);
7765 check_save_entry (impl, &path, &is_well_formed, &is_empty, &is_file_part_empty, &is_folder);
7767 if (is_empty || !is_well_formed)
7770 g_assert (path != NULL);
7775 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7776 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
7778 _gtk_file_chooser_entry_set_file_part (entry, "");
7779 change_folder_and_display_error (impl, path);
7782 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
7783 || GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7785 /* The folder already exists, so we do not need to create it.
7786 * Just respond to terminate the dialog.
7792 g_assert_not_reached ();
7798 struct FileExistsData *data;
7800 /* We need to check whether path exists and is not a folder */
7802 data = g_new0 (struct FileExistsData, 1);
7803 data->impl = g_object_ref (impl);
7804 data->path = gtk_file_path_copy (path);
7805 data->parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
7807 if (impl->file_exists_get_info_handle)
7808 gtk_file_system_cancel_operation (impl->file_exists_get_info_handle);
7810 impl->file_exists_get_info_handle =
7811 gtk_file_system_get_info (impl->file_system, path,
7812 GTK_FILE_INFO_IS_FOLDER,
7813 file_exists_get_info_cb,
7816 set_busy_cursor (impl, TRUE);
7820 g_error_free (error);
7823 gtk_file_path_free (path);
7826 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
7828 /* The focus is on a dialog's action area button, *and* the widget that
7829 * was focused immediately before it is the shortcuts list. Switch to the
7830 * selected shortcut and tell the caller not to respond.
7834 if (shortcuts_get_selected (impl, &iter))
7836 shortcuts_activate_iter (impl, &iter);
7838 gtk_widget_grab_focus (impl->browse_files_tree_view);
7845 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
7847 /* The focus is on a dialog's action area button, *and* the widget that
7848 * was focused immediately before it is the file list.
7852 else if (impl->location_entry && impl->toplevel_last_focus_widget == impl->location_entry)
7854 /* The focus is on a dialog's action area button, *and* the widget that
7855 * was focused immediately before it is the location entry.
7860 /* The focus is on a dialog's action area button or something else */
7861 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7862 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7867 g_assert_not_reached ();
7871 /* Implementation for GtkFileChooserEmbed::initial_focus() */
7873 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
7875 GtkFileChooserDefault *impl;
7878 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
7880 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
7881 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
7883 if (impl->location_mode == LOCATION_MODE_PATH_BAR)
7884 widget = impl->browse_files_tree_view;
7886 widget = impl->location_entry;
7888 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
7889 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
7890 widget = impl->location_entry;
7893 g_assert_not_reached ();
7897 g_assert (widget != NULL);
7898 gtk_widget_grab_focus (widget);
7902 set_current_filter (GtkFileChooserDefault *impl,
7903 GtkFileFilter *filter)
7905 if (impl->current_filter != filter)
7909 /* NULL filters are allowed to reset to non-filtered status
7911 filter_index = g_slist_index (impl->filters, filter);
7912 if (impl->filters && filter && filter_index < 0)
7915 if (impl->current_filter)
7916 g_object_unref (impl->current_filter);
7917 impl->current_filter = filter;
7918 if (impl->current_filter)
7920 g_object_ref_sink (impl->current_filter);
7924 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
7927 if (impl->browse_files_model)
7928 install_list_model_filter (impl);
7930 g_object_notify (G_OBJECT (impl), "filter");
7935 filter_combo_changed (GtkComboBox *combo_box,
7936 GtkFileChooserDefault *impl)
7938 gint new_index = gtk_combo_box_get_active (combo_box);
7939 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
7941 set_current_filter (impl, new_filter);
7945 check_preview_change (GtkFileChooserDefault *impl)
7947 GtkTreePath *cursor_path;
7948 const GtkFilePath *new_path;
7949 const GtkFileInfo *new_info;
7951 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
7952 if (cursor_path && impl->sort_model)
7955 GtkTreeIter child_iter;
7957 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path))
7958 g_assert_not_reached ();
7960 gtk_tree_path_free (cursor_path);
7962 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
7964 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
7965 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
7973 if (new_path != impl->preview_path &&
7974 !(new_path && impl->preview_path &&
7975 gtk_file_path_compare (new_path, impl->preview_path) == 0))
7977 if (impl->preview_path)
7979 gtk_file_path_free (impl->preview_path);
7980 g_free (impl->preview_display_name);
7985 impl->preview_path = gtk_file_path_copy (new_path);
7986 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
7990 impl->preview_path = NULL;
7991 impl->preview_display_name = NULL;
7994 if (impl->use_preview_label && impl->preview_label)
7995 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
7997 g_signal_emit_by_name (impl, "update-preview");
8002 shortcuts_activate_volume_mount_cb (GtkFileSystemHandle *handle,
8003 GtkFileSystemVolume *volume,
8004 const GError *error,
8008 gboolean cancelled = handle->cancelled;
8009 GtkFileChooserDefault *impl = data;
8011 if (handle != impl->shortcuts_activate_iter_handle)
8014 impl->shortcuts_activate_iter_handle = NULL;
8016 set_busy_cursor (impl, FALSE);
8025 msg = g_strdup_printf (_("Could not mount %s"),
8026 gtk_file_system_volume_get_display_name (impl->file_system, volume));
8027 error_message (impl, msg, error->message);
8033 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
8036 change_folder_and_display_error (impl, path);
8037 gtk_file_path_free (path);
8041 g_object_unref (impl);
8042 g_object_unref (handle);
8046 /* Activates a volume by mounting it if necessary and then switching to its
8050 shortcuts_activate_volume (GtkFileChooserDefault *impl,
8051 GtkFileSystemVolume *volume)
8055 /* We ref the file chooser since volume_mount() may run a main loop, and the
8056 * user could close the file chooser window in the meantime.
8058 g_object_ref (impl);
8060 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
8062 set_busy_cursor (impl, TRUE);
8064 impl->shortcuts_activate_iter_handle =
8065 gtk_file_system_volume_mount (impl->file_system, volume,
8066 shortcuts_activate_volume_mount_cb,
8067 g_object_ref (impl));
8071 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
8074 change_folder_and_display_error (impl, path);
8075 gtk_file_path_free (path);
8079 g_object_unref (impl);
8082 /* Opens the folder or volume at the specified iter in the shortcuts model */
8083 struct ShortcutsActivateData
8085 GtkFileChooserDefault *impl;
8090 shortcuts_activate_get_info_cb (GtkFileSystemHandle *handle,
8091 const GtkFileInfo *info,
8092 const GError *error,
8095 gboolean cancelled = handle->cancelled;
8096 struct ShortcutsActivateData *data = user_data;
8098 if (handle != data->impl->shortcuts_activate_iter_handle)
8101 data->impl->shortcuts_activate_iter_handle = NULL;
8106 if (!error && gtk_file_info_get_is_folder (info))
8107 change_folder_and_display_error (data->impl, data->path);
8109 gtk_file_chooser_default_select_path (GTK_FILE_CHOOSER (data->impl), data->path, NULL);
8112 g_object_unref (data->impl);
8113 gtk_file_path_free (data->path);
8116 g_object_unref (handle);
8120 shortcuts_activate_iter (GtkFileChooserDefault *impl,
8126 if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY && impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
8127 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), "");
8129 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
8130 SHORTCUTS_COL_DATA, &col_data,
8131 SHORTCUTS_COL_IS_VOLUME, &is_volume,
8135 return; /* We are on a separator */
8137 if (impl->shortcuts_activate_iter_handle)
8139 gtk_file_system_cancel_operation (impl->shortcuts_activate_iter_handle);
8140 impl->shortcuts_activate_iter_handle = NULL;
8145 GtkFileSystemVolume *volume;
8149 shortcuts_activate_volume (impl, volume);
8153 struct ShortcutsActivateData *data;
8155 data = g_new0 (struct ShortcutsActivateData, 1);
8156 data->impl = g_object_ref (impl);
8157 data->path = gtk_file_path_copy (col_data);
8159 impl->shortcuts_activate_iter_handle =
8160 gtk_file_system_get_info (impl->file_system, data->path,
8161 GTK_FILE_INFO_IS_FOLDER,
8162 shortcuts_activate_get_info_cb, data);
8166 /* Callback used when a row in the shortcuts list is activated */
8168 shortcuts_row_activated_cb (GtkTreeView *tree_view,
8170 GtkTreeViewColumn *column,
8171 GtkFileChooserDefault *impl)
8174 GtkTreeIter child_iter;
8176 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
8179 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
8182 shortcuts_activate_iter (impl, &child_iter);
8184 gtk_widget_grab_focus (impl->browse_files_tree_view);
8187 /* Handler for GtkWidget::key-press-event on the shortcuts list */
8189 shortcuts_key_press_event_cb (GtkWidget *widget,
8191 GtkFileChooserDefault *impl)
8195 modifiers = gtk_accelerator_get_default_mod_mask ();
8197 if ((event->keyval == GDK_BackSpace
8198 || event->keyval == GDK_Delete
8199 || event->keyval == GDK_KP_Delete)
8200 && (event->state & modifiers) == 0)
8202 remove_selected_bookmarks (impl);
8206 if ((event->keyval == GDK_F2)
8207 && (event->state & modifiers) == 0)
8209 rename_selected_bookmark (impl);
8217 shortcuts_select_func (GtkTreeSelection *selection,
8218 GtkTreeModel *model,
8220 gboolean path_currently_selected,
8223 GtkFileChooserDefault *impl = data;
8225 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
8229 list_select_func (GtkTreeSelection *selection,
8230 GtkTreeModel *model,
8232 gboolean path_currently_selected,
8235 GtkFileChooserDefault *impl = data;
8237 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8238 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8240 GtkTreeIter iter, child_iter;
8241 const GtkFileInfo *info;
8243 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8246 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8248 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8250 if (info && !gtk_file_info_get_is_folder (info))
8258 list_selection_changed (GtkTreeSelection *selection,
8259 GtkFileChooserDefault *impl)
8261 /* See if we are in the new folder editable row for Save mode */
8262 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8264 const GtkFileInfo *info;
8265 gboolean had_selection;
8267 info = get_selected_file_info_from_file_list (impl, &had_selection);
8269 goto out; /* normal processing */
8272 return; /* We are on the editable row for New Folder */
8277 update_chooser_entry (impl);
8278 check_preview_change (impl);
8279 bookmarks_check_add_sensitivity (impl);
8281 g_signal_emit_by_name (impl, "selection-changed", 0);
8284 /* Callback used when a row in the file list is activated */
8286 list_row_activated (GtkTreeView *tree_view,
8288 GtkTreeViewColumn *column,
8289 GtkFileChooserDefault *impl)
8291 GtkTreeIter iter, child_iter;
8292 const GtkFileInfo *info;
8294 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
8297 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
8299 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8301 if (gtk_file_info_get_is_folder (info))
8303 const GtkFilePath *file_path;
8305 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8306 change_folder_and_display_error (impl, file_path);
8311 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
8312 impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
8313 g_signal_emit_by_name (impl, "file-activated");
8317 path_bar_clicked (GtkPathBar *path_bar,
8318 GtkFilePath *file_path,
8319 GtkFilePath *child_path,
8320 gboolean child_is_hidden,
8321 GtkFileChooserDefault *impl)
8324 pending_select_paths_add (impl, child_path);
8326 if (!change_folder_and_display_error (impl, file_path))
8329 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
8330 * show hidden files so that ".baz" appears in the file list, as it will still
8331 * be shown in the path bar: "/foo/[bar]/.baz"
8333 if (child_is_hidden)
8334 g_object_set (impl, "show-hidden", TRUE, NULL);
8337 static const GtkFileInfo *
8338 get_list_file_info (GtkFileChooserDefault *impl,
8341 GtkTreeIter child_iter;
8343 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8347 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
8351 list_icon_data_func (GtkTreeViewColumn *tree_column,
8352 GtkCellRenderer *cell,
8353 GtkTreeModel *tree_model,
8357 GtkFileChooserDefault *impl = data;
8358 GtkTreeIter child_iter;
8359 const GtkFilePath *path;
8361 const GtkFileInfo *info;
8362 gboolean sensitive = TRUE;
8364 profile_start ("start", NULL);
8366 info = get_list_file_info (impl, iter);
8368 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
8371 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
8379 /* FIXME: NULL GError */
8380 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (impl),
8381 impl->icon_size, NULL);
8386 /* We are on the editable row */
8390 if (info && (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8391 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER))
8392 sensitive = gtk_file_info_get_is_folder (info);
8396 "sensitive", sensitive,
8400 g_object_unref (pixbuf);
8402 profile_end ("end", NULL);
8406 list_name_data_func (GtkTreeViewColumn *tree_column,
8407 GtkCellRenderer *cell,
8408 GtkTreeModel *tree_model,
8412 GtkFileChooserDefault *impl = data;
8413 const GtkFileInfo *info = get_list_file_info (impl, iter);
8414 gboolean sensitive = TRUE;
8419 "text", _("Type name of new folder"),
8426 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
8427 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8429 sensitive = gtk_file_info_get_is_folder (info);
8433 "text", gtk_file_info_get_display_name (info),
8434 "sensitive", sensitive,
8440 list_size_data_func (GtkTreeViewColumn *tree_column,
8441 GtkCellRenderer *cell,
8442 GtkTreeModel *tree_model,
8446 GtkFileChooserDefault *impl = data;
8447 const GtkFileInfo *info = get_list_file_info (impl, iter);
8450 gboolean sensitive = TRUE;
8452 if (!info || gtk_file_info_get_is_folder (info))
8456 "sensitive", sensitive,
8461 size = gtk_file_info_get_size (info);
8463 if (size < (gint64)1024)
8464 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
8465 else if (size < (gint64)1024*1024)
8466 str = g_strdup_printf (_("%.1f KB"), size / (1024.));
8467 else if (size < (gint64)1024*1024*1024)
8468 str = g_strdup_printf (_("%.1f MB"), size / (1024.*1024.));
8470 str = g_strdup_printf (_("%.1f GB"), size / (1024.*1024.*1024.));
8472 str = g_strdup_printf ("%" G_GINT64_FORMAT, size);
8473 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8474 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8479 "sensitive", sensitive,
8480 "alignment", PANGO_ALIGN_RIGHT,
8487 /* Tree column data callback for the file list; fetches the mtime of a file */
8489 list_mtime_data_func (GtkTreeViewColumn *tree_column,
8490 GtkCellRenderer *cell,
8491 GtkTreeModel *tree_model,
8495 GtkFileChooserDefault *impl;
8496 const GtkFileInfo *info;
8497 GtkFileTime time_mtime;
8501 gboolean sensitive = TRUE;
8505 info = get_list_file_info (impl, iter);
8515 time_mtime = gtk_file_info_get_modification_time (info);
8517 if (time_mtime == 0)
8518 strcpy (buf, _("Unknown"));
8522 g_date_set_time_t (&mtime, time_mtime);
8523 time_now = time (NULL);
8524 g_date_set_time_t (&now, time_now);
8526 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
8529 strcpy (buf, _("Today"));
8530 else if (days_diff == 1)
8531 strcpy (buf, _("Yesterday"));
8536 if (days_diff > 1 && days_diff < 7)
8537 format = "%A"; /* Days from last week */
8539 format = "%x"; /* Any other date */
8541 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
8542 strcpy (buf, _("Unknown"));
8546 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
8547 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8548 sensitive = gtk_file_info_get_is_folder (info);
8552 "sensitive", sensitive,
8557 _gtk_file_chooser_default_new (const char *file_system)
8559 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
8560 "file-system-backend", file_system,
8565 location_set_user_text (GtkFileChooserDefault *impl,
8568 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
8569 gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
8573 location_popup_handler (GtkFileChooserDefault *impl,
8576 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
8577 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
8579 LocationMode new_mode;
8583 /* since the user typed something, we unconditionally want to turn on the entry */
8584 new_mode = LOCATION_MODE_FILENAME_ENTRY;
8586 else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
8587 new_mode = LOCATION_MODE_FILENAME_ENTRY;
8588 else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
8589 new_mode = LOCATION_MODE_PATH_BAR;
8592 g_assert_not_reached ();
8596 location_mode_set (impl, new_mode, TRUE);
8597 if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
8600 location_set_user_text (impl, path);
8603 location_entry_set_initial_text (impl);
8604 gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
8608 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
8609 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
8611 gtk_widget_grab_focus (impl->location_entry);
8613 location_set_user_text (impl, path);
8616 g_assert_not_reached ();
8619 /* Handler for the "up-folder" keybinding signal */
8621 up_folder_handler (GtkFileChooserDefault *impl)
8623 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
8626 /* Handler for the "down-folder" keybinding signal */
8628 down_folder_handler (GtkFileChooserDefault *impl)
8630 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
8633 /* Switches to the shortcut in the specified index */
8635 switch_to_shortcut (GtkFileChooserDefault *impl,
8640 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
8641 g_assert_not_reached ();
8643 shortcuts_activate_iter (impl, &iter);
8646 /* Handler for the "home-folder" keybinding signal */
8648 home_folder_handler (GtkFileChooserDefault *impl)
8651 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_HOME));
8654 /* Handler for the "desktop-folder" keybinding signal */
8656 desktop_folder_handler (GtkFileChooserDefault *impl)
8658 if (impl->has_desktop)
8659 switch_to_shortcut (impl, shortcuts_get_index (impl, SHORTCUTS_DESKTOP));
8663 quick_bookmark_handler (GtkFileChooserDefault *impl,
8664 gint bookmark_index)
8669 if (bookmark_index < 0 || bookmark_index >= impl->num_bookmarks)
8672 bookmark_pos = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS) + bookmark_index;
8674 path = gtk_tree_path_new_from_indices (bookmark_pos, -1);
8675 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
8678 gtk_tree_path_free (path);
8680 switch_to_shortcut (impl, bookmark_pos);
8685 /* Drag and drop interfaces */
8688 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
8693 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
8698 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
8700 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
8703 ShortcutsModelFilter *model;
8707 model = SHORTCUTS_MODEL_FILTER (drag_source);
8709 pos = *gtk_tree_path_get_indices (path);
8710 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
8712 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
8715 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
8717 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
8719 GtkSelectionData *selection_data)
8721 ShortcutsModelFilter *model;
8723 model = SHORTCUTS_MODEL_FILTER (drag_source);
8730 /* Fill the GtkTreeDragSourceIface vtable */
8732 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
8734 iface->row_draggable = shortcuts_model_filter_row_draggable;
8735 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
8739 /* Fill the GtkTreeDragDestIface vtable */
8741 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
8743 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
8744 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
8748 static GtkTreeModel *
8749 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
8750 GtkTreeModel *child_model,
8753 ShortcutsModelFilter *model;
8755 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
8756 "child-model", child_model,
8757 "virtual-root", root,
8762 return GTK_TREE_MODEL (model);