1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserdefault.c: Default implementation of GtkFileChooser
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "gdk/gdkkeysyms.h"
23 #include "gtkalignment.h"
24 #include "gtkbindings.h"
25 #include "gtkbutton.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrendererpixbuf.h"
28 #include "gtkcellrendererseptext.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcombobox.h"
32 #include "gtkexpander.h"
33 #include "gtkfilechooserdefault.h"
34 #include "gtkfilechooserembed.h"
35 #include "gtkfilechooserentry.h"
36 #include "gtkfilechooserutils.h"
37 #include "gtkfilechooser.h"
38 #include "gtkfilesystemmodel.h"
41 #include "gtkhpaned.h"
42 #include "gtkiconfactory.h"
43 #include "gtkicontheme.h"
47 #include "gtkmarshalers.h"
48 #include "gtkmenuitem.h"
49 #include "gtkmessagedialog.h"
50 #include "gtkpathbar.h"
51 #include "gtkprivate.h"
52 #include "gtkscrolledwindow.h"
53 #include "gtksizegroup.h"
56 #include "gtktreednd.h"
57 #include "gtktreeprivate.h"
58 #include "gtktreeview.h"
59 #include "gtktreemodelsort.h"
60 #include "gtktreeselection.h"
61 #include "gtktreestore.h"
62 #include "gtktypebuiltins.h"
65 #if defined (G_OS_UNIX)
66 #include "gtkfilesystemunix.h"
67 #elif defined (G_OS_WIN32)
68 #include "gtkfilesystemwin32.h"
74 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
76 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
77 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
78 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
81 struct _GtkFileChooserDefaultClass
83 GtkVBoxClass parent_class;
86 struct _GtkFileChooserDefault
88 GtkVBox parent_instance;
90 GtkFileChooserAction action;
92 GtkFileSystem *file_system;
94 /* Save mode widgets */
95 GtkWidget *save_widgets;
97 GtkWidget *save_file_name_entry;
98 GtkWidget *save_folder_label;
99 GtkWidget *save_folder_combo;
100 GtkWidget *save_extra_align;
101 GtkWidget *save_expander;
103 /* The file browsing widgets */
104 GtkWidget *browse_widgets;
105 GtkWidget *browse_shortcuts_tree_view;
106 GtkWidget *browse_shortcuts_add_button;
107 GtkWidget *browse_shortcuts_remove_button;
108 GtkWidget *browse_files_tree_view;
109 GtkWidget *browse_new_folder_button;
110 GtkWidget *browse_path_bar;
111 GtkWidget *browse_extra_align;
113 GtkFileSystemModel *browse_files_model;
115 GtkWidget *filter_combo;
116 GtkWidget *preview_box;
117 GtkWidget *preview_label;
118 GtkWidget *preview_widget;
119 GtkWidget *extra_widget;
121 GtkListStore *shortcuts_model;
122 GtkTreeModel *shortcuts_filter_model;
124 GtkTreeModelSort *sort_model;
126 GtkFileFilter *current_filter;
130 gboolean has_desktop;
136 guint volumes_changed_id;
137 guint bookmarks_changed_id;
139 GtkFilePath *current_volume_path;
140 GtkFilePath *current_folder;
141 GtkFilePath *preview_path;
142 char *preview_display_name;
144 GtkTreeViewColumn *list_name_column;
145 GtkCellRenderer *list_name_renderer;
147 guint settings_signal_id;
151 GdkDragContext *shortcuts_drag_context;
152 GSource *shortcuts_drag_outside_idle;
157 guint local_only : 1;
158 guint preview_widget_active : 1;
159 guint use_preview_label : 1;
160 guint select_multiple : 1;
161 guint show_hidden : 1;
162 guint list_sort_ascending : 1;
163 guint changing_folder : 1;
164 guint shortcuts_current_folder_active : 1;
165 guint shortcuts_current_folder_is_volume : 1;
168 guint shortcuts_drag_outside : 1;
181 static guint signals[LAST_SIGNAL] = { 0 };
183 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
185 SHORTCUTS_COL_PIXBUF,
188 SHORTCUTS_COL_REMOVABLE,
189 SHORTCUTS_COL_PIXBUF_VISIBLE,
190 SHORTCUTS_COL_NUM_COLUMNS
193 /* Column numbers for the file list */
198 FILE_LIST_COL_NUM_COLUMNS
201 /* Identifiers for target types */
207 /* Target types for dragging from the shortcuts list */
208 static GtkTargetEntry shortcuts_source_targets[] = {
209 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
212 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
213 / sizeof (shortcuts_source_targets[0]));
215 /* Target types for dropping into the shortcuts list */
216 static GtkTargetEntry shortcuts_dest_targets[] = {
217 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
218 { "text/uri-list", 0, TEXT_URI_LIST }
221 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
222 / sizeof (shortcuts_dest_targets[0]));
224 /* Target types for DnD from the file list */
225 static GtkTargetEntry file_list_source_targets[] = {
226 { "text/uri-list", 0, TEXT_URI_LIST }
229 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
230 / sizeof (file_list_source_targets[0]));
232 /* Interesting places in the shortcuts bar */
238 SHORTCUTS_BOOKMARKS_SEPARATOR,
240 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
241 SHORTCUTS_CURRENT_FOLDER
244 /* Icon size for if we can't get it from the theme */
245 #define FALLBACK_ICON_SIZE 20
247 #define PREVIEW_HBOX_SPACING 12
251 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
252 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
253 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
254 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
256 static GObject* gtk_file_chooser_default_constructor (GType type,
257 guint n_construct_properties,
258 GObjectConstructParam *construct_params);
259 static void gtk_file_chooser_default_finalize (GObject *object);
260 static void gtk_file_chooser_default_set_property (GObject *object,
264 static void gtk_file_chooser_default_get_property (GObject *object,
268 static void gtk_file_chooser_default_dispose (GObject *object);
269 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
270 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
271 GtkStyle *previous_style);
272 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
273 GdkScreen *previous_screen);
275 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
276 const GtkFilePath *path,
278 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
279 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
281 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
282 const GtkFilePath *path,
284 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
285 const GtkFilePath *path);
286 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
287 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
288 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
289 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
290 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
291 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
292 GtkFileFilter *filter);
293 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
294 GtkFileFilter *filter);
295 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
296 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
297 const GtkFilePath *path,
299 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
300 const GtkFilePath *path,
302 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
304 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
306 gint *default_height);
307 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
308 gboolean *resize_horizontally,
309 gboolean *resize_vertically);
310 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
311 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
313 static void location_popup_handler (GtkFileChooserDefault *impl);
314 static void up_folder_handler (GtkFileChooserDefault *impl);
315 static void down_folder_handler (GtkFileChooserDefault *impl);
316 static void home_folder_handler (GtkFileChooserDefault *impl);
317 static void update_appearance (GtkFileChooserDefault *impl);
319 static void set_current_filter (GtkFileChooserDefault *impl,
320 GtkFileFilter *filter);
321 static void check_preview_change (GtkFileChooserDefault *impl);
323 static void filter_combo_changed (GtkComboBox *combo_box,
324 GtkFileChooserDefault *impl);
325 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
327 GtkTreeViewColumn *column,
328 GtkFileChooserDefault *impl);
329 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
332 gboolean path_currently_selected,
334 static void shortcuts_activate_item (GtkFileChooserDefault *impl,
336 static int shortcuts_get_index (GtkFileChooserDefault *impl,
337 ShortcutsIndex where);
338 static int shortcut_find_position (GtkFileChooserDefault *impl,
339 const GtkFilePath *path);
341 static void list_selection_changed (GtkTreeSelection *tree_selection,
342 GtkFileChooserDefault *impl);
343 static void list_row_activated (GtkTreeView *tree_view,
345 GtkTreeViewColumn *column,
346 GtkFileChooserDefault *impl);
348 static void path_bar_clicked (GtkPathBar *path_bar,
349 GtkFilePath *file_path,
350 GtkFileChooserDefault *impl);
352 static void add_bookmark_button_clicked_cb (GtkButton *button,
353 GtkFileChooserDefault *impl);
354 static void remove_bookmark_button_clicked_cb (GtkButton *button,
355 GtkFileChooserDefault *impl);
357 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
358 GtkCellRenderer *cell,
359 GtkTreeModel *tree_model,
362 static void list_name_data_func (GtkTreeViewColumn *tree_column,
363 GtkCellRenderer *cell,
364 GtkTreeModel *tree_model,
368 static void list_size_data_func (GtkTreeViewColumn *tree_column,
369 GtkCellRenderer *cell,
370 GtkTreeModel *tree_model,
374 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
375 GtkCellRenderer *cell,
376 GtkTreeModel *tree_model,
380 static GObjectClass *parent_class;
384 /* Drag and drop interface declarations */
387 GtkTreeModelFilter parent;
389 GtkFileChooserDefault *impl;
390 } ShortcutsModelFilter;
393 GtkTreeModelFilterClass parent_class;
394 } ShortcutsModelFilterClass;
396 #define SHORTCUTS_MODEL_FILTER_TYPE (shortcuts_model_filter_get_type ())
397 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
399 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
401 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
402 shortcuts_model_filter,
403 GTK_TYPE_TREE_MODEL_FILTER,
404 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
405 shortcuts_model_filter_drag_source_iface_init));
407 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
408 GtkTreeModel *child_model,
414 _gtk_file_chooser_default_get_type (void)
416 static GType file_chooser_default_type = 0;
418 if (!file_chooser_default_type)
420 static const GTypeInfo file_chooser_default_info =
422 sizeof (GtkFileChooserDefaultClass),
423 NULL, /* base_init */
424 NULL, /* base_finalize */
425 (GClassInitFunc) gtk_file_chooser_default_class_init,
426 NULL, /* class_finalize */
427 NULL, /* class_data */
428 sizeof (GtkFileChooserDefault),
430 (GInstanceInitFunc) gtk_file_chooser_default_init,
433 static const GInterfaceInfo file_chooser_info =
435 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
436 NULL, /* interface_finalize */
437 NULL /* interface_data */
440 static const GInterfaceInfo file_chooser_embed_info =
442 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
443 NULL, /* interface_finalize */
444 NULL /* interface_data */
447 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
448 &file_chooser_default_info, 0);
450 g_type_add_interface_static (file_chooser_default_type,
451 GTK_TYPE_FILE_CHOOSER,
453 g_type_add_interface_static (file_chooser_default_type,
454 GTK_TYPE_FILE_CHOOSER_EMBED,
455 &file_chooser_embed_info);
458 return file_chooser_default_type;
462 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
464 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
465 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
466 GtkBindingSet *binding_set;
468 parent_class = g_type_class_peek_parent (class);
470 gobject_class->finalize = gtk_file_chooser_default_finalize;
471 gobject_class->constructor = gtk_file_chooser_default_constructor;
472 gobject_class->set_property = gtk_file_chooser_default_set_property;
473 gobject_class->get_property = gtk_file_chooser_default_get_property;
474 gobject_class->dispose = gtk_file_chooser_default_dispose;
476 widget_class->show_all = gtk_file_chooser_default_show_all;
477 widget_class->style_set = gtk_file_chooser_default_style_set;
478 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
480 signals[LOCATION_POPUP] =
481 _gtk_binding_signal_new ("location-popup",
482 G_OBJECT_CLASS_TYPE (class),
483 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
484 G_CALLBACK (location_popup_handler),
486 _gtk_marshal_VOID__VOID,
489 _gtk_binding_signal_new ("up-folder",
490 G_OBJECT_CLASS_TYPE (class),
491 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
492 G_CALLBACK (up_folder_handler),
494 _gtk_marshal_VOID__VOID,
496 signals[DOWN_FOLDER] =
497 _gtk_binding_signal_new ("down-folder",
498 G_OBJECT_CLASS_TYPE (class),
499 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
500 G_CALLBACK (down_folder_handler),
502 _gtk_marshal_VOID__VOID,
504 signals[HOME_FOLDER] =
505 _gtk_binding_signal_new ("home-folder",
506 G_OBJECT_CLASS_TYPE (class),
507 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
508 G_CALLBACK (home_folder_handler),
510 _gtk_marshal_VOID__VOID,
513 binding_set = gtk_binding_set_by_class (class);
515 gtk_binding_entry_add_signal (binding_set,
516 GDK_l, GDK_CONTROL_MASK,
520 gtk_binding_entry_add_signal (binding_set,
521 GDK_Up, GDK_MOD1_MASK,
524 gtk_binding_entry_add_signal (binding_set,
525 GDK_KP_Up, GDK_MOD1_MASK,
529 gtk_binding_entry_add_signal (binding_set,
530 GDK_Down, GDK_MOD1_MASK,
533 gtk_binding_entry_add_signal (binding_set,
534 GDK_KP_Down, GDK_MOD1_MASK,
538 gtk_binding_entry_add_signal (binding_set,
539 GDK_Home, GDK_MOD1_MASK,
542 gtk_binding_entry_add_signal (binding_set,
543 GDK_KP_Home, GDK_MOD1_MASK,
547 _gtk_file_chooser_install_properties (gobject_class);
549 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
550 P_("Default file chooser backend"),
551 P_("Name of the GtkFileChooser backend to use by default"),
557 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
559 iface->select_path = gtk_file_chooser_default_select_path;
560 iface->unselect_path = gtk_file_chooser_default_unselect_path;
561 iface->select_all = gtk_file_chooser_default_select_all;
562 iface->unselect_all = gtk_file_chooser_default_unselect_all;
563 iface->get_paths = gtk_file_chooser_default_get_paths;
564 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
565 iface->get_file_system = gtk_file_chooser_default_get_file_system;
566 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
567 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
568 iface->set_current_name = gtk_file_chooser_default_set_current_name;
569 iface->add_filter = gtk_file_chooser_default_add_filter;
570 iface->remove_filter = gtk_file_chooser_default_remove_filter;
571 iface->list_filters = gtk_file_chooser_default_list_filters;
572 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
573 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
574 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
578 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
580 iface->get_default_size = gtk_file_chooser_default_get_default_size;
581 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
582 iface->should_respond = gtk_file_chooser_default_should_respond;
583 iface->initial_focus = gtk_file_chooser_default_initial_focus;
586 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
588 impl->local_only = TRUE;
589 impl->preview_widget_active = TRUE;
590 impl->use_preview_label = TRUE;
591 impl->select_multiple = FALSE;
592 impl->show_hidden = FALSE;
593 impl->icon_size = FALLBACK_ICON_SIZE;
595 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (impl), TRUE);
596 gtk_box_set_spacing (GTK_BOX (impl), 12);
600 gtk_file_chooser_default_finalize (GObject *object)
602 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
605 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
606 impl->volumes_changed_id = 0;
607 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
608 impl->bookmarks_changed_id = 0;
609 g_object_unref (impl->file_system);
611 for (l = impl->filters; l; l = l->next)
613 GtkFileFilter *filter;
615 filter = GTK_FILE_FILTER (l->data);
616 g_object_unref (filter);
618 g_slist_free (impl->filters);
620 if (impl->current_filter)
621 g_object_unref (impl->current_filter);
623 if (impl->current_volume_path)
624 gtk_file_path_free (impl->current_volume_path);
626 if (impl->current_folder)
627 gtk_file_path_free (impl->current_folder);
629 if (impl->preview_path)
630 gtk_file_path_free (impl->preview_path);
632 /* Free all the Models we have */
633 if (impl->browse_files_model)
634 g_object_unref (impl->browse_files_model);
636 if (impl->shortcuts_model)
637 g_object_unref (impl->shortcuts_model);
639 if (impl->shortcuts_filter_model)
640 g_object_unref (impl->shortcuts_filter_model);
642 if (impl->sort_model)
643 g_object_unref (impl->sort_model);
645 g_free (impl->preview_display_name);
647 G_OBJECT_CLASS (parent_class)->finalize (object);
650 /* Shows an error dialog set as transient for the specified window */
652 error_message_with_parent (GtkWindow *parent,
657 dialog = gtk_message_dialog_new (parent,
658 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
663 gtk_dialog_run (GTK_DIALOG (dialog));
664 gtk_widget_destroy (dialog);
667 /* Shows an error dialog for the file chooser */
669 error_message (GtkFileChooserDefault *impl,
674 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
675 if (!GTK_WIDGET_TOPLEVEL (toplevel))
678 error_message_with_parent (toplevel ? GTK_WINDOW (toplevel) : NULL,
682 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
684 error_dialog (GtkFileChooserDefault *impl,
686 const GtkFilePath *path,
689 g_return_if_fail (path != NULL);
693 char *uri = gtk_file_system_path_to_uri (impl->file_system, path);
694 char *text = g_strdup_printf (msg,
697 error_message (impl, text);
700 g_error_free (error);
704 /* Displays an error message about not being able to get information for a file.
705 * Frees the GError as well.
708 error_getting_info_dialog (GtkFileChooserDefault *impl,
709 const GtkFilePath *path,
713 _("Could not retrieve information about %s:\n%s"),
717 /* Shows an error dialog about not being able to add a bookmark */
719 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
720 const GtkFilePath *path,
724 _("Could not add a bookmark for %s:\n%s"),
728 /* Shows an error dialog about not being able to compose a filename */
730 error_building_filename_dialog (GtkFileChooserDefault *impl,
731 const GtkFilePath *base_path,
732 const char *file_part,
738 uri = gtk_file_system_path_to_uri (impl->file_system, base_path);
739 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
742 error_message (impl, msg);
745 g_error_free (error);
748 /* Shows an error dialog when we cannot switch to a folder */
750 error_changing_folder_dialog (GtkFileChooserDefault *impl,
751 const GtkFilePath *path,
755 _("Could not change the current folder to %s:\n%s"),
760 /* Changes folders, displaying an error dialog if this fails */
762 change_folder_and_display_error (GtkFileChooserDefault *impl,
763 const GtkFilePath *path)
769 result = _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path, &error);
772 error_changing_folder_dialog (impl, path, error);
778 update_preview_widget_visibility (GtkFileChooserDefault *impl)
780 if (impl->use_preview_label)
782 if (!impl->preview_label)
784 impl->preview_label = gtk_label_new (impl->preview_display_name);
785 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
786 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
787 gtk_widget_show (impl->preview_label);
792 if (impl->preview_label)
794 gtk_widget_destroy (impl->preview_label);
795 impl->preview_label = NULL;
799 if (impl->preview_widget_active && impl->preview_widget)
800 gtk_widget_show (impl->preview_box);
802 gtk_widget_hide (impl->preview_box);
804 g_signal_emit_by_name (impl, "default-size-changed");
808 set_preview_widget (GtkFileChooserDefault *impl,
809 GtkWidget *preview_widget)
811 if (preview_widget == impl->preview_widget)
814 if (impl->preview_widget)
815 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
816 impl->preview_widget);
818 impl->preview_widget = preview_widget;
819 if (impl->preview_widget)
821 gtk_widget_show (impl->preview_widget);
822 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
823 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
824 impl->preview_widget,
825 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
828 update_preview_widget_visibility (impl);
831 /* Re-reads all the icons for the shortcuts, used when the theme changes */
833 shortcuts_reload_icons (GtkFileChooserDefault *impl)
837 int bookmarks_separator_idx;
838 int current_folder_separator_idx;
841 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
844 bookmarks_separator_idx = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
845 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
846 volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
852 gboolean pixbuf_visible;
855 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
856 SHORTCUTS_COL_PATH, &data,
857 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
860 if (!pixbuf_visible || !data)
863 if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
865 GtkFileSystemVolume *volume;
868 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
869 impl->icon_size, NULL);
873 const GtkFilePath *path;
876 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
877 impl->icon_size, NULL);
880 gtk_list_store_set (impl->shortcuts_model, &iter,
881 SHORTCUTS_COL_PIXBUF, pixbuf,
886 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
889 /* If a shortcut corresponds to the current folder, selects it */
891 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
893 GtkTreeSelection *selection;
897 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
899 pos = shortcut_find_position (impl, impl->current_folder);
902 gtk_tree_selection_unselect_all (selection);
906 path = gtk_tree_path_new_from_indices (pos, -1);
907 gtk_tree_selection_select_path (selection, path);
908 gtk_tree_path_free (path);
911 /* Returns whether a path is a folder */
913 check_is_folder (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
915 GtkFileFolder *folder;
917 folder = gtk_file_system_get_folder (file_system, path,
918 GTK_FILE_INFO_DISPLAY_NAME,
923 g_object_unref (folder);
927 /* Convenience function to get the display name and icon info for a path */
929 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, gboolean name_only, GError **error)
931 GtkFilePath *parent_path;
932 GtkFileFolder *parent_folder;
937 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
940 parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
941 GTK_FILE_INFO_DISPLAY_NAME
942 | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
947 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, error);
948 g_object_unref (parent_folder);
952 gtk_file_path_free (parent_path);
956 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
957 * inserts a volume. A position of -1 indicates the end of the tree.
960 shortcuts_insert_path (GtkFileChooserDefault *impl,
963 GtkFileSystemVolume *volume,
964 const GtkFilePath *path,
977 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
978 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
979 impl->icon_size, NULL);
983 if (!check_is_folder (impl->file_system, path, error))
987 label_copy = g_strdup (label);
990 GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
995 label_copy = g_strdup (gtk_file_info_get_display_name (info));
996 gtk_file_info_free (info);
999 data = gtk_file_path_copy (path);
1000 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1001 impl->icon_size, NULL);
1005 gtk_list_store_append (impl->shortcuts_model, &iter);
1007 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1009 gtk_list_store_set (impl->shortcuts_model, &iter,
1010 SHORTCUTS_COL_PIXBUF, pixbuf,
1011 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1012 SHORTCUTS_COL_NAME, label_copy,
1013 SHORTCUTS_COL_PATH, data,
1014 SHORTCUTS_COL_REMOVABLE, removable,
1017 g_free (label_copy);
1020 g_object_unref (pixbuf);
1025 /* Appends an item for the user's home directory to the shortcuts model */
1027 shortcuts_append_home (GtkFileChooserDefault *impl)
1030 GtkFilePath *home_path;
1033 home = g_get_home_dir ();
1034 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1037 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1038 if (!impl->has_home)
1039 error_getting_info_dialog (impl, home_path, error);
1041 gtk_file_path_free (home_path);
1044 /* Appends the ~/Desktop directory to the shortcuts model */
1046 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1051 name = g_build_filename (g_get_home_dir (), "Desktop", NULL);
1052 path = gtk_file_system_filename_to_path (impl->file_system, name);
1055 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1056 /* We do not actually pop up an error dialog if there is no desktop directory
1057 * because some people may really not want to have one.
1060 gtk_file_path_free (path);
1063 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1065 shortcuts_append_paths (GtkFileChooserDefault *impl,
1071 /* As there is no separator now, we want to start there.
1073 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1076 for (; paths; paths = paths->next)
1084 if (impl->local_only &&
1085 !gtk_file_system_path_is_local (impl->file_system, path))
1088 /* NULL GError, but we don't really want to show error boxes here */
1089 if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, NULL, TRUE, NULL))
1093 return num_inserted;
1096 /* Returns the index for the corresponding item in the shortcuts bar */
1098 shortcuts_get_index (GtkFileChooserDefault *impl,
1099 ShortcutsIndex where)
1105 if (where == SHORTCUTS_HOME)
1108 n += impl->has_home ? 1 : 0;
1110 if (where == SHORTCUTS_DESKTOP)
1113 n += impl->has_desktop ? 1 : 0;
1115 if (where == SHORTCUTS_VOLUMES)
1118 n += impl->num_volumes;
1120 if (where == SHORTCUTS_SHORTCUTS)
1123 n += impl->num_shortcuts;
1125 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1128 /* If there are no bookmarks there won't be a separator */
1129 n += (impl->num_bookmarks > 0) ? 1 : 0;
1131 if (where == SHORTCUTS_BOOKMARKS)
1134 n += impl->num_bookmarks;
1136 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1141 if (where == SHORTCUTS_CURRENT_FOLDER)
1144 g_assert_not_reached ();
1151 typedef void (* RemoveFunc) (GtkFileChooserDefault *impl, gpointer data);
1153 /* Removes the specified number of rows from the shortcuts list */
1155 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1158 RemoveFunc remove_fn)
1162 path = gtk_tree_path_new_from_indices (start_row, -1);
1164 for (; n_rows; n_rows--)
1169 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1170 g_assert_not_reached ();
1174 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1175 (* remove_fn) (impl, data);
1178 gtk_list_store_remove (impl->shortcuts_model, &iter);
1181 gtk_tree_path_free (path);
1184 /* Used from shortcuts_remove_rows() in shortcuts_add_volumes() */
1186 volume_remove_cb (GtkFileChooserDefault *impl, gpointer data)
1188 GtkFileSystemVolume *volume;
1191 gtk_file_system_volume_free (impl->file_system, volume);
1194 /* Adds all the file system volumes to the shortcuts model */
1196 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1201 gboolean old_changing_folders;
1203 old_changing_folders = impl->changing_folder;
1204 impl->changing_folder = TRUE;
1206 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1207 shortcuts_remove_rows (impl, start_row, impl->num_volumes, volume_remove_cb);
1208 impl->num_volumes = 0;
1210 list = gtk_file_system_list_volumes (impl->file_system);
1214 for (l = list; l; l = l->next)
1216 GtkFileSystemVolume *volume;
1220 if (impl->local_only)
1222 GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1223 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1224 gtk_file_path_free (base_path);
1230 shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL);
1234 impl->num_volumes = n;
1235 g_slist_free (list);
1237 if (impl->shortcuts_filter_model)
1238 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1240 impl->changing_folder = old_changing_folders;
1243 /* Used from shortcuts_remove_rows() */
1245 remove_bookmark_cb (GtkFileChooserDefault *impl, gpointer data)
1250 gtk_file_path_free (path);
1253 /* Inserts a separator node in the shortcuts list */
1255 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1256 ShortcutsIndex where)
1260 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1262 gtk_list_store_insert (impl->shortcuts_model, &iter,
1263 shortcuts_get_index (impl, where));
1264 gtk_list_store_set (impl->shortcuts_model, &iter,
1265 SHORTCUTS_COL_PIXBUF, NULL,
1266 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1267 SHORTCUTS_COL_NAME, NULL,
1268 SHORTCUTS_COL_PATH, NULL,
1272 /* Updates the list of bookmarks */
1274 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1277 gboolean old_changing_folders;
1279 old_changing_folders = impl->changing_folder;
1280 impl->changing_folder = TRUE;
1282 if (impl->num_bookmarks > 0)
1284 shortcuts_remove_rows (impl,
1285 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1286 impl->num_bookmarks + 1,
1287 remove_bookmark_cb);
1291 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1292 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1293 gtk_file_paths_free (bookmarks);
1295 if (impl->num_bookmarks > 0)
1297 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1299 if (impl->shortcuts_filter_model)
1300 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1302 impl->changing_folder = old_changing_folders;
1305 /* Appends a separator and a row to the shortcuts list for the current folder */
1307 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1312 g_assert (!impl->shortcuts_current_folder_active);
1316 pos = shortcut_find_position (impl, impl->current_folder);
1319 GtkFileSystemVolume *volume;
1320 GtkFilePath *base_path;
1324 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1328 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1330 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1332 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1337 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1339 success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1340 impl->shortcuts_current_folder_is_volume = TRUE;
1344 success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1345 impl->shortcuts_current_folder_is_volume = FALSE;
1349 gtk_file_system_volume_free (impl->file_system, volume);
1350 gtk_file_path_free (base_path);
1353 shortcuts_remove_rows (impl, pos - 1, 1, NULL); /* remove the separator */
1355 impl->shortcuts_current_folder_active = success;
1359 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1362 /* Used from shortcuts_remove_rows() in shortcuts_update_current_folder() */
1364 remove_current_folder_cb (GtkFileChooserDefault *impl,
1367 if (impl->shortcuts_current_folder_is_volume)
1368 gtk_file_system_volume_free (impl->file_system, data);
1370 gtk_file_path_free (data);
1373 /* Updates the current folder row in the shortcuts model */
1375 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1379 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1381 if (impl->shortcuts_current_folder_active)
1383 shortcuts_remove_rows (impl, pos, 2, remove_current_folder_cb);
1384 impl->shortcuts_current_folder_active = FALSE;
1387 shortcuts_add_current_folder (impl);
1390 /* Filter function used for the shortcuts filter model */
1392 shortcuts_filter_cb (GtkTreeModel *model,
1396 GtkFileChooserDefault *impl;
1400 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1402 path = gtk_tree_model_get_path (model, iter);
1406 pos = *gtk_tree_path_get_indices (path);
1407 gtk_tree_path_free (path);
1409 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1412 /* Creates the list model for shortcuts */
1414 shortcuts_model_create (GtkFileChooserDefault *impl)
1416 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1417 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1418 GDK_TYPE_PIXBUF, /* pixbuf */
1419 G_TYPE_STRING, /* name */
1420 G_TYPE_POINTER, /* path or volume */
1421 G_TYPE_BOOLEAN, /* removable */
1422 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1424 if (impl->file_system)
1426 shortcuts_append_home (impl);
1427 shortcuts_append_desktop (impl);
1428 shortcuts_add_volumes (impl);
1429 shortcuts_add_bookmarks (impl);
1432 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1433 GTK_TREE_MODEL (impl->shortcuts_model),
1436 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1437 shortcuts_filter_cb,
1442 /* Callback used when the "New Folder" toolbar button is clicked */
1444 new_folder_button_clicked (GtkButton *button,
1445 GtkFileChooserDefault *impl)
1450 /* FIXME: this doesn't work for folder mode, just for file mode */
1452 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1454 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1455 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1456 path, impl->list_name_column,
1459 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1460 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1462 impl->list_name_column,
1466 /* Callback used from the text cell renderer when the new folder is named */
1468 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1470 const gchar *new_text,
1471 GtkFileChooserDefault *impl)
1474 GtkFilePath *file_path;
1476 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1477 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1480 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, new_text, &error);
1483 error_building_filename_dialog (impl, impl->current_folder, new_text, error);
1488 if (!gtk_file_system_create_folder (impl->file_system, file_path, &error))
1491 _("Could not create folder %s:\n%s"),
1495 gtk_file_path_free (file_path);
1497 /* FIXME: scroll to the new folder and select it */
1500 /* Callback used from the text cell renderer when the new folder edition gets
1504 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1505 GtkFileChooserDefault *impl)
1507 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1508 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1511 /* Creates the widgets for the filter combo box */
1513 filter_create (GtkFileChooserDefault *impl)
1515 impl->filter_combo = gtk_combo_box_new_text ();
1516 g_signal_connect (impl->filter_combo, "changed",
1517 G_CALLBACK (filter_combo_changed), impl);
1519 return impl->filter_combo;
1523 button_new (GtkFileChooserDefault *impl,
1525 const char *stock_id,
1535 button = gtk_button_new ();
1536 hbox = gtk_hbox_new (FALSE, 2);
1537 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1539 gtk_container_add (GTK_CONTAINER (button), align);
1540 gtk_container_add (GTK_CONTAINER (align), hbox);
1541 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1543 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1545 widget = gtk_label_new_with_mnemonic (text);
1546 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1547 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1549 gtk_widget_set_sensitive (button, sensitive);
1550 g_signal_connect (button, "clicked", callback, impl);
1552 gtk_widget_show_all (align);
1555 gtk_widget_show (button);
1560 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1562 shortcut_find_position (GtkFileChooserDefault *impl,
1563 const GtkFilePath *path)
1567 int bookmarks_separator_idx;
1568 int current_folder_separator_idx;
1571 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1574 bookmarks_separator_idx = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1575 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1576 volumes_idx = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1580 for (i = 0; i < current_folder_separator_idx; i++)
1584 if (i == bookmarks_separator_idx)
1587 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
1589 if (i >= volumes_idx && i < volumes_idx + impl->num_volumes)
1591 GtkFileSystemVolume *volume;
1592 GtkFilePath *base_path;
1596 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1598 exists = strcmp (gtk_file_path_get_string (path),
1599 gtk_file_path_get_string (base_path)) == 0;
1607 GtkFilePath *model_path;
1611 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1616 gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1622 /* Tries to add a bookmark from a path name */
1624 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1625 const GtkFilePath *path,
1632 if (shortcut_find_position (impl, path) != -1)
1638 info = get_file_info (impl->file_system, path, FALSE, &error);
1641 error_getting_info_dialog (impl, path, error);
1642 else if (!gtk_file_info_get_is_folder (info))
1647 uri = gtk_file_system_path_to_uri (impl->file_system, path);
1648 msg = g_strdup_printf (_("Could not add bookmark for %s because it is not a folder."),
1650 error_message (impl, msg);
1657 if (gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
1660 error_could_not_add_bookmark_dialog (impl, path, error);
1667 add_bookmark_foreach_cb (GtkTreeModel *model,
1672 GtkFileChooserDefault *impl;
1673 GtkFileSystemModel *fs_model;
1674 GtkTreeIter child_iter;
1675 const GtkFilePath *file_path;
1677 impl = (GtkFileChooserDefault *) data;
1679 fs_model = impl->browse_files_model;
1680 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1682 file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &child_iter);
1683 shortcuts_add_bookmark_from_path (impl, file_path, -1);
1686 /* Callback used when the "Add bookmark" button is clicked */
1688 add_bookmark_button_clicked_cb (GtkButton *button,
1689 GtkFileChooserDefault *impl)
1691 GtkTreeSelection *selection;
1693 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1695 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1696 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
1698 gtk_tree_selection_selected_foreach (selection,
1699 add_bookmark_foreach_cb,
1703 /* Callback used when the "Remove bookmark" button is clicked */
1705 remove_bookmark_button_clicked_cb (GtkButton *button,
1706 GtkFileChooserDefault *impl)
1708 GtkTreeSelection *selection;
1714 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1716 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1718 gtk_tree_model_get (impl->shortcuts_filter_model, &iter,
1719 SHORTCUTS_COL_PATH, &path,
1720 SHORTCUTS_COL_REMOVABLE, &removable, -1);
1723 g_assert_not_reached ();
1728 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1731 _("Could not remove bookmark for %s:\n%s"),
1738 struct selection_check_closure {
1739 GtkFileChooserDefault *impl;
1742 gboolean all_folders;
1745 /* Used from gtk_tree_selection_selected_foreach() */
1747 selection_check_foreach_cb (GtkTreeModel *model,
1752 struct selection_check_closure *closure;
1753 GtkTreeIter child_iter;
1754 const GtkFileInfo *info;
1758 closure->empty = FALSE;
1760 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1762 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
1763 is_folder = gtk_file_info_get_is_folder (info);
1765 closure->all_folders &= is_folder;
1766 closure->all_files &= !is_folder;
1769 /* Checks whether the selected items in the file list are all files or all folders */
1771 selection_check (GtkFileChooserDefault *impl,
1773 gboolean *all_files,
1774 gboolean *all_folders)
1776 struct selection_check_closure closure;
1777 GtkTreeSelection *selection;
1779 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1781 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
1782 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
1784 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1785 closure.empty = TRUE;
1788 closure.empty = FALSE;
1789 closure.all_files = FALSE;
1790 closure.all_folders = TRUE;
1795 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
1796 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE);
1798 closure.impl = impl;
1799 closure.empty = TRUE;
1800 closure.all_files = TRUE;
1801 closure.all_folders = TRUE;
1803 gtk_tree_selection_selected_foreach (selection,
1804 selection_check_foreach_cb,
1808 g_assert (closure.empty || !(closure.all_files && closure.all_folders));
1811 *empty = closure.empty;
1814 *all_files = closure.all_files;
1817 *all_folders = closure.all_folders;
1820 /* Sensitize the "add bookmark" button if all the selected items are folders, or
1821 * if there are no selected items *and* the current folder is not in the
1822 * bookmarks list. De-sensitize the button otherwise.
1825 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1827 GtkTreeSelection *selection;
1830 /* Check selection */
1832 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1834 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1835 active = (shortcut_find_position (impl, impl->current_folder) == -1);
1838 gboolean all_folders;
1840 selection_check (impl, NULL, NULL, &all_folders);
1841 active = (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
1842 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER ||
1846 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
1849 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
1850 * bookmark row is selected in the shortcuts tree.
1853 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
1855 GtkTreeSelection *selection;
1857 gboolean removable = FALSE;
1859 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1861 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1862 gtk_tree_model_get (impl->shortcuts_filter_model, &iter,
1863 SHORTCUTS_COL_REMOVABLE, &removable,
1866 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
1869 /* GtkWidget::drag-begin handler for the shortcuts list. */
1871 shortcuts_drag_begin_cb (GtkWidget *widget,
1872 GdkDragContext *context,
1873 GtkFileChooserDefault *impl)
1876 impl->shortcuts_drag_context = g_object_ref (context);
1881 /* Removes the idle handler for outside drags */
1883 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
1885 if (!impl->shortcuts_drag_outside_idle)
1888 g_source_destroy (impl->shortcuts_drag_outside_idle);
1889 impl->shortcuts_drag_outside_idle = NULL;
1893 /* GtkWidget::drag-end handler for the shortcuts list. */
1895 shortcuts_drag_end_cb (GtkWidget *widget,
1896 GdkDragContext *context,
1897 GtkFileChooserDefault *impl)
1900 g_object_unref (impl->shortcuts_drag_context);
1902 shortcuts_cancel_drag_outside_idle (impl);
1904 if (!impl->shortcuts_drag_outside)
1907 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
1909 impl->shortcuts_drag_outside = FALSE;
1913 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
1915 shortcuts_drag_data_delete_cb (GtkWidget *widget,
1916 GdkDragContext *context,
1917 GtkFileChooserDefault *impl)
1919 g_signal_stop_emission_by_name (widget, "drag-data-delete");
1923 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
1927 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
1930 GtkTreeView *tree_view;
1931 GtkTreeSelection *selection;
1932 GtkTreeIter iter, child_iter;
1934 GdkPixmap *row_pixmap;
1939 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
1941 /* Find the selected path and get its drag pixmap */
1943 selection = gtk_tree_view_get_selection (tree_view);
1944 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
1945 g_assert_not_reached ();
1947 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1951 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter);
1953 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
1954 gtk_tree_path_free (path);
1963 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
1969 GdkPixmap *composite;
1970 int row_pixmap_width, row_pixmap_height;
1971 int pixbuf_width, pixbuf_height;
1972 int composite_width, composite_height;
1973 int pixbuf_x, pixbuf_y;
1974 GdkGC *gc, *mask_gc;
1976 GdkBitmap *pixbuf_mask;
1978 /* Create pixmap and mask for composite image */
1980 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
1981 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
1982 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
1984 composite_width = MAX (row_pixmap_width, pixbuf_width);
1985 composite_height = MAX (row_pixmap_height, pixbuf_height);
1987 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
1989 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
1992 pixbuf_x = composite_width - pixbuf_width;
1994 pixbuf_y = (composite_height - pixbuf_height) / 2;
1996 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
1997 gc = gdk_gc_new (composite);
1999 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2000 mask_gc = gdk_gc_new (mask);
2002 gdk_gc_set_foreground (mask_gc, &color);
2003 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2006 color.green = 0xffff;
2007 color.blue = 0xffff;
2008 gdk_gc_set_rgb_fg_color (gc, &color);
2009 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2011 /* Composite the row pixmap and the pixbuf */
2013 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2015 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2016 NULL, &pixbuf_mask, 128);
2017 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2020 pixbuf_width, pixbuf_height);
2021 g_object_unref (pixbuf_mask);
2023 gdk_draw_drawable (composite, gc, row_pixmap,
2026 row_pixmap_width, row_pixmap_height);
2028 gdk_gc_set_foreground (mask_gc, &color);
2029 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2031 gdk_draw_pixbuf (composite, gc, pixbuf,
2034 pixbuf_width, pixbuf_height,
2038 g_object_unref (pixbuf);
2039 g_object_unref (row_pixmap);
2041 row_pixmap = composite;
2045 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2047 gtk_tree_view_get_path_at_pos (tree_view,
2048 tree_view->priv->press_start_x,
2049 tree_view->priv->press_start_y,
2055 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2056 gdk_drawable_get_colormap (row_pixmap),
2059 tree_view->priv->press_start_x + 1,
2060 row_pixmap_y + cell_y + 1);
2062 g_object_unref (row_pixmap);
2064 g_object_unref (mask);
2067 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2068 * handler so that we can tell apart the drag_leave event that comes right
2069 * before a drag_drop, from a normal drag_leave. We don't want to set the
2070 * cursor nor the flag in the latter case.
2073 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2075 shortcuts_drag_set_delete_cursor (impl, TRUE);
2076 impl->shortcuts_drag_outside = TRUE;
2078 shortcuts_cancel_drag_outside_idle (impl);
2083 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2087 shortcuts_drag_leave_cb (GtkWidget *widget,
2088 GdkDragContext *context,
2090 GtkFileChooserDefault *impl)
2093 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2095 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2096 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2097 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2099 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2103 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2105 GTK_TREE_VIEW_DROP_BEFORE);
2107 g_signal_stop_emission_by_name (widget, "drag-leave");
2110 /* Computes the appropriate row and position for dropping */
2112 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2116 GtkTreeViewDropPosition *pos)
2118 GtkTreeView *tree_view;
2119 GtkTreeViewColumn *column;
2123 int bookmarks_index;
2125 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2127 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2129 if (!gtk_tree_view_get_path_at_pos (tree_view,
2131 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2137 row = bookmarks_index + impl->num_bookmarks - 1;
2138 *path = gtk_tree_path_new_from_indices (row, -1);
2139 *pos = GTK_TREE_VIEW_DROP_AFTER;
2143 row = *gtk_tree_path_get_indices (*path);
2144 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2145 gtk_tree_path_free (*path);
2147 if (row < bookmarks_index)
2149 row = bookmarks_index;
2150 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2152 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2154 row = bookmarks_index + impl->num_bookmarks - 1;
2155 *pos = GTK_TREE_VIEW_DROP_AFTER;
2159 if (cell_y < cell.height / 2)
2160 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2162 *pos = GTK_TREE_VIEW_DROP_AFTER;
2165 *path = gtk_tree_path_new_from_indices (row, -1);
2168 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2169 * implement the destination side of DnD by hand, due to limitations in
2170 * GtkTreeView's DnD API.
2173 shortcuts_drag_motion_cb (GtkWidget *widget,
2174 GdkDragContext *context,
2178 GtkFileChooserDefault *impl)
2181 GtkTreeViewDropPosition pos;
2182 GdkDragAction action;
2185 if (gtk_drag_get_source_widget (context) == widget)
2187 shortcuts_cancel_drag_outside_idle (impl);
2189 if (impl->shortcuts_drag_outside)
2191 shortcuts_drag_set_delete_cursor (impl, FALSE);
2192 impl->shortcuts_drag_outside = FALSE;
2197 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2198 action = GDK_ACTION_COPY;
2199 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2200 action = GDK_ACTION_MOVE;
2207 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2208 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2209 gtk_tree_path_free (path);
2213 g_signal_stop_emission_by_name (widget, "drag-motion");
2217 gdk_drag_status (context, action, time_);
2224 /* GtkWidget::drag-drop handler for the shortcuts list. */
2226 shortcuts_drag_drop_cb (GtkWidget *widget,
2227 GdkDragContext *context,
2231 GtkFileChooserDefault *impl)
2234 shortcuts_cancel_drag_outside_idle (impl);
2237 g_signal_stop_emission_by_name (widget, "drag-drop");
2241 /* Converts raw selection data from text/uri-list to a list of strings */
2243 split_uris (const char *data)
2246 const char *p, *start;
2252 for (p = start; *p != 0; p++)
2253 if (*p == '\r' && *(p + 1) == '\n')
2257 name = g_strndup (start, p - start);
2258 uris = g_slist_prepend (uris, name);
2264 uris = g_slist_reverse (uris);
2268 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2270 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2276 uris = split_uris (data);
2278 for (l = uris; l; l = l->next)
2284 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2288 if (shortcuts_add_bookmark_from_path (impl, path, position))
2291 gtk_file_path_free (path);
2297 msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
2299 error_message (impl, msg);
2306 g_slist_free (uris);
2309 /* Reorders the selected bookmark to the specified position */
2311 shortcuts_reorder (GtkFileChooserDefault *impl,
2314 GtkTreeSelection *selection;
2315 GtkTreeIter iter, child_iter;
2318 int bookmarks_index;
2319 const GtkFilePath *file_path;
2320 GtkFilePath *file_path_copy;
2323 /* Get the selected path */
2325 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2326 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2327 g_assert_not_reached ();
2329 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
2333 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter);
2334 old_position = *gtk_tree_path_get_indices (path);
2335 gtk_tree_path_free (path);
2337 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2338 old_position -= bookmarks_index;
2339 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2341 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter,
2342 SHORTCUTS_COL_PATH, &file_path,
2344 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2346 /* Remove the path from the old position and insert it in the new one */
2348 if (new_position > old_position)
2351 if (old_position == new_position)
2355 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2356 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2358 error_could_not_add_bookmark_dialog (impl, file_path_copy, error);
2360 gtk_file_path_free (file_path_copy);
2363 /* Callback used when we get the drag data for the bookmarks list. We add the
2364 * received URIs as bookmarks if they are folders.
2367 shortcuts_drag_data_received_cb (GtkWidget *widget,
2368 GdkDragContext *context,
2371 GtkSelectionData *selection_data,
2376 GtkFileChooserDefault *impl;
2377 GtkTreePath *tree_path;
2378 GtkTreeViewDropPosition tree_pos;
2380 int bookmarks_index;
2382 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2384 /* Compute position */
2386 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2388 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2389 position = *gtk_tree_path_get_indices (tree_path);
2390 gtk_tree_path_free (tree_path);
2392 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2395 g_assert (position >= bookmarks_index);
2396 position -= bookmarks_index;
2398 if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2399 shortcuts_drop_uris (impl, selection_data->data, position);
2400 else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2401 shortcuts_reorder (impl, position);
2403 g_signal_stop_emission_by_name (widget, "drag-data-received");
2406 /* Callback used when the selection in the shortcuts tree changes */
2408 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
2409 GtkFileChooserDefault *impl)
2411 bookmarks_check_remove_sensitivity (impl);
2414 /* Creates the widgets for the shortcuts and bookmarks tree */
2416 shortcuts_list_create (GtkFileChooserDefault *impl)
2419 GtkTreeSelection *selection;
2420 GtkTreeViewColumn *column;
2421 GtkCellRenderer *renderer;
2423 /* Scrolled window */
2425 swin = gtk_scrolled_window_new (NULL, NULL);
2426 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2427 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2428 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2430 gtk_widget_show (swin);
2434 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
2435 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
2437 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
2439 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2441 shortcuts_source_targets,
2442 num_shortcuts_source_targets,
2445 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
2446 GTK_DEST_DEFAULT_ALL,
2447 shortcuts_dest_targets,
2448 num_shortcuts_dest_targets,
2449 GDK_ACTION_COPY | GDK_ACTION_MOVE);
2451 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2452 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
2453 gtk_tree_selection_set_select_function (selection,
2454 shortcuts_select_func,
2457 g_signal_connect (selection, "changed",
2458 G_CALLBACK (shortcuts_selection_changed_cb), impl);
2460 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
2461 G_CALLBACK (shortcuts_row_activated_cb), impl);
2463 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
2464 G_CALLBACK (shortcuts_drag_begin_cb), impl);
2465 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
2466 G_CALLBACK (shortcuts_drag_end_cb), impl);
2467 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
2468 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
2470 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
2471 G_CALLBACK (shortcuts_drag_leave_cb), impl);
2472 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
2473 G_CALLBACK (shortcuts_drag_motion_cb), impl);
2474 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
2475 G_CALLBACK (shortcuts_drag_drop_cb), impl);
2476 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
2477 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
2479 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
2480 gtk_widget_show (impl->browse_shortcuts_tree_view);
2484 column = gtk_tree_view_column_new ();
2485 gtk_tree_view_column_set_title (column, _("Folder"));
2487 renderer = gtk_cell_renderer_pixbuf_new ();
2488 gtk_tree_view_column_pack_start (column, renderer, FALSE);
2489 gtk_tree_view_column_set_attributes (column, renderer,
2490 "pixbuf", SHORTCUTS_COL_PIXBUF,
2491 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2494 renderer = _gtk_cell_renderer_sep_text_new ();
2495 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2496 gtk_tree_view_column_set_attributes (column, renderer,
2497 "text", SHORTCUTS_COL_NAME,
2500 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
2505 /* Creates the widgets for the shortcuts/bookmarks pane */
2507 shortcuts_pane_create (GtkFileChooserDefault *impl,
2508 GtkSizeGroup *size_group)
2514 vbox = gtk_vbox_new (FALSE, 6);
2515 gtk_widget_show (vbox);
2517 /* Shortcuts tree */
2519 widget = shortcuts_list_create (impl);
2520 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
2522 /* Box for buttons */
2524 hbox = gtk_hbox_new (TRUE, 6);
2525 gtk_size_group_add_widget (size_group, hbox);
2526 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2527 gtk_widget_show (hbox);
2529 /* Add bookmark button */
2531 impl->browse_shortcuts_add_button = button_new (impl,
2536 G_CALLBACK (add_bookmark_button_clicked_cb));
2537 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
2539 /* Remove bookmark button */
2541 impl->browse_shortcuts_remove_button = button_new (impl,
2546 G_CALLBACK (remove_bookmark_button_clicked_cb));
2547 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
2553 trap_activate_cb (GtkWidget *widget,
2557 GtkFileChooserDefault *impl;
2559 impl = (GtkFileChooserDefault *) data;
2561 if (event->keyval == GDK_Return
2562 || event->keyval == GDK_ISO_Enter
2563 || event->keyval == GDK_KP_Enter
2564 || event->keyval == GDK_space)
2566 GtkWidget *toplevel;
2568 toplevel = gtk_widget_get_toplevel (widget);
2569 if (GTK_IS_WINDOW (toplevel))
2573 window = GTK_WINDOW (toplevel);
2576 widget != window->default_widget &&
2577 !(widget == window->focus_widget &&
2578 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
2579 gtk_window_activate_default (window);
2587 /* Creates the widgets for the file list */
2589 create_file_list (GtkFileChooserDefault *impl)
2592 GtkTreeSelection *selection;
2593 GtkTreeViewColumn *column;
2594 GtkCellRenderer *renderer;
2596 /* Scrolled window */
2598 swin = gtk_scrolled_window_new (NULL, NULL);
2599 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2600 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2601 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2604 /* Tree/list view */
2606 impl->browse_files_tree_view = gtk_tree_view_new ();
2607 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
2608 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
2609 g_signal_connect (impl->browse_files_tree_view, "row-activated",
2610 G_CALLBACK (list_row_activated), impl);
2611 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
2612 G_CALLBACK (trap_activate_cb), impl);
2614 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
2615 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
2617 file_list_source_targets,
2618 num_file_list_source_targets,
2621 g_signal_connect (selection, "changed",
2622 G_CALLBACK (list_selection_changed), impl);
2624 /* Filename column */
2626 impl->list_name_column = gtk_tree_view_column_new ();
2627 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
2628 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
2629 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
2631 renderer = gtk_cell_renderer_pixbuf_new ();
2632 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
2633 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
2634 list_icon_data_func, impl, NULL);
2636 impl->list_name_renderer = gtk_cell_renderer_text_new ();
2637 g_signal_connect (impl->list_name_renderer, "edited",
2638 G_CALLBACK (renderer_edited_cb), impl);
2639 g_signal_connect (impl->list_name_renderer, "editing-canceled",
2640 G_CALLBACK (renderer_editing_canceled_cb), impl);
2641 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
2642 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
2643 list_name_data_func, impl, NULL);
2645 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
2649 column = gtk_tree_view_column_new ();
2650 gtk_tree_view_column_set_title (column, _("Size"));
2652 renderer = gtk_cell_renderer_text_new ();
2653 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2654 gtk_tree_view_column_set_cell_data_func (column, renderer,
2655 list_size_data_func, impl, NULL);
2656 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
2657 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
2659 /* Modification time column */
2661 column = gtk_tree_view_column_new ();
2662 gtk_tree_view_column_set_title (column, _("Modified"));
2664 renderer = gtk_cell_renderer_text_new ();
2665 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2666 gtk_tree_view_column_set_cell_data_func (column, renderer,
2667 list_mtime_data_func, impl, NULL);
2668 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
2669 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
2670 gtk_widget_show_all (swin);
2676 create_filename_entry_and_filter_combo (GtkFileChooserDefault *impl)
2681 hbox = gtk_hbox_new (FALSE, 12);
2682 gtk_widget_show (hbox);
2686 widget = filter_create (impl);
2687 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
2693 create_path_bar (GtkFileChooserDefault *impl)
2695 GtkWidget *path_bar;
2697 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
2698 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
2703 /* Creates the widgets for the files/folders pane */
2705 file_pane_create (GtkFileChooserDefault *impl,
2706 GtkSizeGroup *size_group)
2712 vbox = gtk_vbox_new (FALSE, 6);
2713 gtk_widget_show (vbox);
2715 /* The path bar and 'Create Folder' button */
2716 hbox = gtk_hbox_new (FALSE, 12);
2717 gtk_widget_show (hbox);
2718 impl->browse_path_bar = create_path_bar (impl);
2719 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
2720 gtk_widget_show_all (impl->browse_path_bar);
2721 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
2724 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create _Folder"));
2725 g_signal_connect (impl->browse_new_folder_button, "clicked",
2726 G_CALLBACK (new_folder_button_clicked), impl);
2727 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
2728 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2730 /* Box for lists and preview */
2732 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
2733 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
2734 gtk_widget_show (hbox);
2738 widget = create_file_list (impl);
2739 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
2743 impl->preview_box = gtk_vbox_new (FALSE, 12);
2744 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
2745 /* Don't show preview box initially */
2747 /* Filename entry and filter combo */
2748 hbox = gtk_hbox_new (FALSE, 0);
2749 gtk_size_group_add_widget (size_group, hbox);
2750 widget = create_filename_entry_and_filter_combo (impl);
2751 gtk_box_pack_end (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
2752 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2753 gtk_widget_show (hbox);
2757 /* Callback used when the "Browse for more folders" expander is toggled */
2759 expander_changed_cb (GtkExpander *expander,
2761 GtkFileChooserDefault *impl)
2763 update_appearance (impl);
2766 /* Callback used when the selection changes in the save folder combo box */
2768 save_folder_combo_changed_cb (GtkComboBox *combo,
2769 GtkFileChooserDefault *impl)
2773 if (impl->changing_folder)
2776 active = gtk_combo_box_get_active (combo);
2780 shortcuts_activate_item (impl, active);
2783 /* Creates the combo box with the save folders */
2785 save_folder_combo_create (GtkFileChooserDefault *impl)
2788 GtkCellRenderer *cell;
2790 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
2791 gtk_widget_show (combo);
2793 cell = gtk_cell_renderer_pixbuf_new ();
2794 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
2795 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
2796 "pixbuf", SHORTCUTS_COL_PIXBUF,
2797 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2800 cell = _gtk_cell_renderer_sep_text_new ();
2801 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
2802 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
2803 "text", SHORTCUTS_COL_NAME,
2806 g_signal_connect (combo, "changed",
2807 G_CALLBACK (save_folder_combo_changed_cb), impl);
2812 /* Creates the widgets specific to Save mode */
2814 save_widgets_create (GtkFileChooserDefault *impl)
2819 GtkWidget *alignment;
2821 vbox = gtk_vbox_new (FALSE, 12);
2823 table = gtk_table_new (2, 2, FALSE);
2824 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
2825 gtk_widget_show (table);
2826 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
2827 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2831 widget = gtk_label_new_with_mnemonic (_("_Name:"));
2832 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
2833 gtk_table_attach (GTK_TABLE (table), widget,
2837 gtk_widget_show (widget);
2839 impl->save_file_name_entry = gtk_entry_new ();
2840 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
2841 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
2842 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
2844 GTK_EXPAND | GTK_FILL, 0,
2846 gtk_widget_show (impl->save_file_name_entry);
2847 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
2850 impl->save_folder_label = gtk_label_new (NULL);
2851 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
2852 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
2856 gtk_widget_show (impl->save_folder_label);
2858 impl->save_folder_combo = save_folder_combo_create (impl);
2859 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
2861 GTK_EXPAND | GTK_FILL, GTK_FILL,
2863 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
2866 impl->save_extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
2867 gtk_box_pack_start (GTK_BOX (vbox), impl->save_extra_align, FALSE, FALSE, 0);
2870 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
2871 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
2873 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
2874 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
2875 g_signal_connect (impl->save_expander, "notify::expanded",
2876 G_CALLBACK (expander_changed_cb),
2878 gtk_widget_show_all (alignment);
2883 /* Creates the main hpaned with the widgets shared by Open and Save mode */
2885 browse_widgets_create (GtkFileChooserDefault *impl)
2890 GtkSizeGroup *size_group;
2892 /* size group is used by the [+][-] buttons and the filter combo */
2893 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
2894 vbox = gtk_vbox_new (FALSE, 12);
2897 hpaned = gtk_hpaned_new ();
2898 gtk_widget_show (hpaned);
2899 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
2900 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
2902 widget = shortcuts_pane_create (impl, size_group);
2903 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
2904 widget = file_pane_create (impl, size_group);
2905 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
2907 g_object_unref (size_group);
2909 /* Alignment to hold custom widget */
2910 impl->browse_extra_align = gtk_alignment_new (0.0, .5, 1.0, 1.0);
2911 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_extra_align, FALSE, FALSE, 0);
2917 gtk_file_chooser_default_constructor (GType type,
2918 guint n_construct_properties,
2919 GObjectConstructParam *construct_params)
2921 GtkFileChooserDefault *impl;
2924 object = parent_class->constructor (type,
2925 n_construct_properties,
2927 impl = GTK_FILE_CHOOSER_DEFAULT (object);
2929 g_assert (impl->file_system);
2931 gtk_widget_push_composite_child ();
2933 /* Shortcuts model */
2935 shortcuts_model_create (impl);
2937 /* Widgets for Save mode */
2938 impl->save_widgets = save_widgets_create (impl);
2939 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
2941 /* The browse widgets */
2942 impl->browse_widgets = browse_widgets_create (impl);
2943 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
2945 gtk_widget_pop_composite_child ();
2946 update_appearance (impl);
2951 /* Sets the extra_widget by packing it in the appropriate place */
2953 set_extra_widget (GtkFileChooserDefault *impl,
2954 GtkWidget *extra_widget)
2958 g_object_ref (extra_widget);
2959 /* FIXME: is this right ? */
2960 gtk_widget_show (extra_widget);
2963 if (impl->extra_widget)
2964 g_object_unref (impl->extra_widget);
2966 impl->extra_widget = extra_widget;
2970 set_local_only (GtkFileChooserDefault *impl,
2971 gboolean local_only)
2973 if (local_only != impl->local_only)
2975 impl->local_only = local_only;
2977 if (impl->shortcuts_model && impl->file_system)
2979 shortcuts_add_volumes (impl);
2980 shortcuts_add_bookmarks (impl);
2984 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
2986 /* If we are pointing to a non-local folder, make an effort to change
2987 * back to a local folder, but it's really up to the app to not cause
2988 * such a situation, so we ignore errors.
2990 const gchar *home = g_get_home_dir ();
2991 GtkFilePath *home_path = gtk_file_system_filename_to_path (impl->file_system, home);
2993 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
2995 gtk_file_path_free (home_path);
3001 volumes_changed_cb (GtkFileSystem *file_system,
3002 GtkFileChooserDefault *impl)
3004 shortcuts_add_volumes (impl);
3007 /* Callback used when the set of bookmarks changes in the file system */
3009 bookmarks_changed_cb (GtkFileSystem *file_system,
3010 GtkFileChooserDefault *impl)
3012 shortcuts_add_bookmarks (impl);
3014 bookmarks_check_add_sensitivity (impl);
3015 bookmarks_check_remove_sensitivity (impl);
3018 /* Sets the file chooser to multiple selection mode */
3020 set_select_multiple (GtkFileChooserDefault *impl,
3021 gboolean select_multiple,
3022 gboolean property_notify)
3024 GtkTreeSelection *selection;
3025 GtkSelectionMode mode;
3027 if (select_multiple == impl->select_multiple)
3030 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3032 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3033 gtk_tree_selection_set_mode (selection, mode);
3035 impl->select_multiple = select_multiple;
3036 g_object_notify (G_OBJECT (impl), "select-multiple");
3038 /* FIXME #132255: See note in check_preview_change() */
3039 check_preview_change (impl);
3043 set_file_system_backend (GtkFileChooserDefault *impl,
3044 const char *backend)
3046 if (impl->file_system)
3048 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3049 impl->volumes_changed_id = 0;
3050 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3051 impl->bookmarks_changed_id = 0;
3052 g_object_unref (impl->file_system);
3055 impl->file_system = NULL;
3057 impl->file_system = _gtk_file_system_create (backend);
3060 GtkSettings *settings = gtk_settings_get_default ();
3061 gchar *default_backend = NULL;
3063 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3064 if (default_backend)
3066 impl->file_system = _gtk_file_system_create (default_backend);
3067 g_free (default_backend);
3071 if (!impl->file_system)
3073 #if defined (G_OS_UNIX)
3074 impl->file_system = gtk_file_system_unix_new ();
3075 #elif defined (G_OS_WIN32)
3076 impl->file_system = gtk_file_system_win32_new ();
3078 #error "No default filesystem implementation on the platform"
3082 if (impl->file_system)
3084 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3085 G_CALLBACK (volumes_changed_cb),
3087 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3088 G_CALLBACK (bookmarks_changed_cb),
3093 /* This function is basically a do_all function.
3095 * It sets the visibility on all the widgets based on the current state, and
3096 * moves the custom_widget if needed.
3099 update_appearance (GtkFileChooserDefault *impl)
3103 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3104 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3108 gtk_widget_show (impl->save_widgets);
3110 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3111 text = _("Save in _folder:");
3113 text = _("Create in _folder:");
3115 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
3117 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3119 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
3120 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
3121 gtk_widget_show (impl->browse_widgets);
3125 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
3126 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
3127 gtk_widget_hide (impl->browse_widgets);
3130 gtk_widget_show (impl->browse_new_folder_button);
3132 if (impl->select_multiple)
3134 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
3135 "Re-setting to single selection mode.");
3136 set_select_multiple (impl, FALSE, TRUE);
3139 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3140 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
3142 gtk_widget_hide (impl->save_widgets);
3143 gtk_widget_show (impl->browse_widgets);
3146 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3147 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3149 if (impl->browse_files_model)
3150 _gtk_file_system_model_set_show_files (impl->browse_files_model, FALSE);
3154 if (impl->browse_files_model)
3155 _gtk_file_system_model_set_show_files (impl->browse_files_model, TRUE);
3158 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
3159 gtk_widget_hide (impl->browse_new_folder_button);
3161 gtk_widget_show (impl->browse_new_folder_button);
3163 if (impl->extra_widget)
3166 GtkWidget *unused_align;
3168 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
3169 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3171 align = impl->save_extra_align;
3172 unused_align = impl->browse_extra_align;
3176 align = impl->browse_extra_align;
3177 unused_align = impl->save_extra_align;
3180 /* We own a ref on extra_widget, so it's safe to do this */
3181 child = GTK_BIN (unused_align)->child;
3183 gtk_container_remove (GTK_CONTAINER (unused_align), child);
3185 child = GTK_BIN (align)->child;
3186 if (child && child != impl->extra_widget)
3188 gtk_container_remove (GTK_CONTAINER (align), child);
3189 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
3191 else if (child == NULL)
3193 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
3196 gtk_widget_show (align);
3197 gtk_widget_hide (unused_align);
3201 child = GTK_BIN (impl->browse_extra_align)->child;
3203 gtk_container_remove (GTK_CONTAINER (impl->browse_extra_align), child);
3205 child = GTK_BIN (impl->save_extra_align)->child;
3207 gtk_container_remove (GTK_CONTAINER (impl->save_extra_align), child);
3209 gtk_widget_hide (impl->save_extra_align);
3210 gtk_widget_hide (impl->browse_extra_align);
3213 g_signal_emit_by_name (impl, "default-size-changed");
3217 gtk_file_chooser_default_set_property (GObject *object,
3219 const GValue *value,
3223 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3227 case GTK_FILE_CHOOSER_PROP_ACTION:
3229 GtkFileChooserAction action = g_value_get_enum (value);
3231 if (action != impl->action)
3233 if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
3235 g_warning ("Multiple selection mode is not allowed in Save mode");
3236 set_select_multiple (impl, FALSE, TRUE);
3238 impl->action = action;
3239 update_appearance (impl);
3243 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
3244 set_file_system_backend (impl, g_value_get_string (value));
3246 case GTK_FILE_CHOOSER_PROP_FILTER:
3247 set_current_filter (impl, g_value_get_object (value));
3249 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3250 set_local_only (impl, g_value_get_boolean (value));
3252 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3253 set_preview_widget (impl, g_value_get_object (value));
3255 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3256 impl->preview_widget_active = g_value_get_boolean (value);
3257 update_preview_widget_visibility (impl);
3259 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3260 impl->use_preview_label = g_value_get_boolean (value);
3261 update_preview_widget_visibility (impl);
3263 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3264 set_extra_widget (impl, g_value_get_object (value));
3265 update_appearance (impl);
3267 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3269 gboolean select_multiple = g_value_get_boolean (value);
3270 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
3272 g_warning ("Multiple selection mode is not allowed in Save mode");
3276 set_select_multiple (impl, select_multiple, FALSE);
3279 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3281 gboolean show_hidden = g_value_get_boolean (value);
3282 if (show_hidden != impl->show_hidden)
3284 impl->show_hidden = show_hidden;
3285 _gtk_file_system_model_set_show_hidden (GTK_FILE_SYSTEM_MODEL (impl->browse_files_model),
3291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3297 gtk_file_chooser_default_get_property (GObject *object,
3302 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3306 case GTK_FILE_CHOOSER_PROP_ACTION:
3307 g_value_set_enum (value, impl->action);
3309 case GTK_FILE_CHOOSER_PROP_FILTER:
3310 g_value_set_object (value, impl->current_filter);
3312 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3313 g_value_set_boolean (value, impl->local_only);
3315 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3316 g_value_set_object (value, impl->preview_widget);
3318 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3319 g_value_set_boolean (value, impl->preview_widget_active);
3321 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3322 g_value_set_boolean (value, impl->use_preview_label);
3324 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3325 g_value_set_object (value, impl->extra_widget);
3327 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3328 g_value_set_boolean (value, impl->select_multiple);
3330 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3331 g_value_set_boolean (value, impl->show_hidden);
3334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3339 /* Removes the settings signal handler. It's safe to call multiple times */
3341 remove_settings_signal (GtkFileChooserDefault *impl,
3344 if (impl->settings_signal_id)
3346 GtkSettings *settings;
3348 settings = gtk_settings_get_for_screen (screen);
3349 g_signal_handler_disconnect (settings,
3350 impl->settings_signal_id);
3351 impl->settings_signal_id = 0;
3356 gtk_file_chooser_default_dispose (GObject *object)
3358 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
3360 if (impl->extra_widget)
3362 g_object_unref (impl->extra_widget);
3363 impl->extra_widget = NULL;
3366 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
3368 G_OBJECT_CLASS (parent_class)->dispose (object);
3371 /* We override show-all since we have internal widgets that
3372 * shouldn't be shown when you call show_all(), like the filter
3376 gtk_file_chooser_default_show_all (GtkWidget *widget)
3378 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
3380 gtk_widget_show (widget);
3382 if (impl->extra_widget)
3383 gtk_widget_show_all (impl->extra_widget);
3386 /* Changes the icons wherever it is needed */
3388 change_icon_theme (GtkFileChooserDefault *impl)
3390 GtkSettings *settings;
3393 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3395 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_SMALL_TOOLBAR, &width, &height))
3396 impl->icon_size = MAX (width, height);
3398 impl->icon_size = FALLBACK_ICON_SIZE;
3400 shortcuts_reload_icons (impl);
3401 gtk_widget_queue_resize (impl->browse_files_tree_view);
3404 /* Callback used when a GtkSettings value changes */
3406 settings_notify_cb (GObject *object,
3408 GtkFileChooserDefault *impl)
3412 name = g_param_spec_get_name (pspec);
3414 if (strcmp (name, "gtk-icon-theme-name") == 0
3415 || strcmp (name, "gtk-icon-sizes") == 0)
3416 change_icon_theme (impl);
3419 /* Installs a signal handler for GtkSettings so that we can monitor changes in
3423 check_icon_theme (GtkFileChooserDefault *impl)
3425 GtkSettings *settings;
3427 if (impl->settings_signal_id)
3430 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3431 impl->settings_signal_id = g_signal_connect (settings, "notify",
3432 G_CALLBACK (settings_notify_cb), impl);
3434 change_icon_theme (impl);
3438 gtk_file_chooser_default_style_set (GtkWidget *widget,
3439 GtkStyle *previous_style)
3441 GtkFileChooserDefault *impl;
3443 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3445 if (GTK_WIDGET_CLASS (parent_class)->style_set)
3446 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
3448 check_icon_theme (impl);
3450 g_signal_emit_by_name (widget, "default-size-changed");
3454 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
3455 GdkScreen *previous_screen)
3457 GtkFileChooserDefault *impl;
3459 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3461 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
3462 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
3464 remove_settings_signal (impl, previous_screen);
3465 check_icon_theme (impl);
3467 g_signal_emit_by_name (widget, "default-size-changed");
3471 list_model_filter_func (GtkFileSystemModel *model,
3473 const GtkFileInfo *file_info,
3476 GtkFileChooserDefault *impl = user_data;
3477 GtkFileFilterInfo filter_info;
3478 GtkFileFilterFlags needed;
3481 if (!impl->current_filter)
3484 if (gtk_file_info_get_is_folder (file_info))
3487 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
3489 needed = gtk_file_filter_get_needed (impl->current_filter);
3491 filter_info.display_name = gtk_file_info_get_display_name (file_info);
3492 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
3494 if (needed & GTK_FILE_FILTER_FILENAME)
3496 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
3497 if (filter_info.filename)
3498 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
3501 filter_info.filename = NULL;
3503 if (needed & GTK_FILE_FILTER_URI)
3505 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
3506 if (filter_info.uri)
3507 filter_info.contains |= GTK_FILE_FILTER_URI;
3510 filter_info.uri = NULL;
3512 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
3514 if (filter_info.filename)
3515 g_free ((gchar *)filter_info.filename);
3516 if (filter_info.uri)
3517 g_free ((gchar *)filter_info.uri);
3523 install_list_model_filter (GtkFileChooserDefault *impl)
3525 if (impl->current_filter)
3526 _gtk_file_system_model_set_filter (impl->browse_files_model,
3527 list_model_filter_func,
3531 #define COMPARE_DIRECTORIES \
3532 GtkFileChooserDefault *impl = user_data; \
3533 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
3534 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
3535 gboolean dir_a, dir_b; \
3538 dir_a = gtk_file_info_get_is_folder (info_a); \
3540 return impl->list_sort_ascending ? -1 : 1; \
3543 dir_b = gtk_file_info_get_is_folder (info_b); \
3545 return impl->list_sort_ascending ? 1 : -1; \
3547 if (dir_a != dir_b) \
3548 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
3550 /* Sort callback for the filename column */
3552 name_sort_func (GtkTreeModel *model,
3557 COMPARE_DIRECTORIES;
3559 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
3562 /* Sort callback for the size column */
3564 size_sort_func (GtkTreeModel *model,
3569 COMPARE_DIRECTORIES;
3572 gint64 size_a = gtk_file_info_get_size (info_a);
3573 gint64 size_b = gtk_file_info_get_size (info_b);
3575 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
3579 /* Sort callback for the mtime column */
3581 mtime_sort_func (GtkTreeModel *model,
3586 COMPARE_DIRECTORIES;
3589 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
3590 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
3592 return ta > tb ? -1 : (ta == tb ? 0 : 1);
3596 /* Callback used when the sort column changes. We cache the sort order for use
3597 * in name_sort_func().
3600 list_sort_column_changed_cb (GtkTreeSortable *sortable,
3601 GtkFileChooserDefault *impl)
3603 GtkSortType sort_type;
3605 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
3606 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
3609 /* Gets rid of the old list model and creates a new one for the current folder */
3611 set_list_model (GtkFileChooserDefault *impl)
3613 if (impl->browse_files_model)
3615 g_object_unref (impl->browse_files_model);
3616 g_object_unref (impl->sort_model);
3619 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
3620 impl->current_folder, 0,
3622 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
3623 switch (impl->action)
3625 case GTK_FILE_CHOOSER_ACTION_OPEN:
3626 case GTK_FILE_CHOOSER_ACTION_SAVE:
3627 _gtk_file_system_model_set_show_files (impl->browse_files_model, TRUE);
3629 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
3630 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
3631 _gtk_file_system_model_set_show_files (impl->browse_files_model, FALSE);
3634 g_assert_not_reached ();
3636 install_list_model_filter (impl);
3638 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
3639 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
3640 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
3641 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
3642 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
3643 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
3644 impl->list_sort_ascending = TRUE;
3646 g_signal_connect (impl->sort_model, "sort-column-changed",
3647 G_CALLBACK (list_sort_column_changed_cb), impl);
3649 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
3650 GTK_TREE_MODEL (impl->sort_model));
3651 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
3652 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
3653 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
3657 update_chooser_entry (GtkFileChooserDefault *impl)
3659 GtkTreeSelection *selection;
3660 const GtkFileInfo *info;
3662 GtkTreeIter child_iter;
3664 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
3667 g_assert (!impl->select_multiple);
3668 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3670 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
3673 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
3677 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
3679 if (!gtk_file_info_get_is_folder (info))
3680 gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry),
3681 gtk_file_info_get_display_name (info));
3685 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
3686 const GtkFilePath *path,
3689 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3691 /* Test validity of path here. */
3692 if (!check_is_folder (impl->file_system, path, error))
3695 if (impl->local_only &&
3696 !gtk_file_system_path_is_local (impl->file_system, path))
3699 GTK_FILE_SYSTEM_ERROR,
3700 GTK_FILE_SYSTEM_ERROR_FAILED,
3701 _("Can't change to folder because it isn't local"));
3706 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
3709 if (impl->current_folder != path)
3711 if (impl->current_folder)
3712 gtk_file_path_free (impl->current_folder);
3714 impl->current_folder = gtk_file_path_copy (path);
3717 /* Update the widgets that may trigger a folder change themselves. */
3719 if (!impl->changing_folder)
3721 impl->changing_folder = TRUE;
3723 shortcuts_update_current_folder (impl);
3725 impl->changing_folder = FALSE;
3728 /* Create a new list model */
3729 set_list_model (impl);
3731 /* Refresh controls */
3733 shortcuts_find_current_folder (impl);
3735 g_signal_emit_by_name (impl, "current-folder-changed", 0);
3737 check_preview_change (impl);
3738 bookmarks_check_add_sensitivity (impl);
3740 g_signal_emit_by_name (impl, "selection-changed", 0);
3745 static GtkFilePath *
3746 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
3748 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3750 return gtk_file_path_copy (impl->current_folder);
3754 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
3757 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3759 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
3760 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
3762 gtk_entry_set_text (GTK_ENTRY (impl->save_file_name_entry), name);
3766 select_func (GtkFileSystemModel *model,
3771 GtkFileChooserDefault *impl = user_data;
3772 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
3773 GtkTreePath *sorted_path;
3775 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
3776 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
3777 gtk_tree_path_free (sorted_path);
3781 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
3782 const GtkFilePath *path,
3785 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3786 GtkFilePath *parent_path;
3788 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
3792 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
3797 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
3798 gtk_file_path_free (parent_path);
3799 _gtk_file_system_model_path_do (impl->browse_files_model, path,
3805 g_assert_not_reached ();
3809 unselect_func (GtkFileSystemModel *model,
3814 GtkFileChooserDefault *impl = user_data;
3815 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
3816 GtkTreePath *sorted_path;
3818 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
3820 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
3822 gtk_tree_path_free (sorted_path);
3826 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
3827 const GtkFilePath *path)
3829 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3831 _gtk_file_system_model_path_do (impl->browse_files_model, path,
3832 unselect_func, impl);
3836 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
3838 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3839 if (impl->select_multiple)
3841 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3842 gtk_tree_selection_select_all (selection);
3847 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
3849 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3850 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3852 gtk_tree_selection_unselect_all (selection);
3855 /* Checks whether the filename entry for the Save modes contains a valid filename */
3856 static GtkFilePath *
3857 check_save_entry (GtkFileChooserDefault *impl,
3861 const char *filename;
3865 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
3866 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
3868 filename = gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry));
3870 if (!filename || filename[0] == '\0')
3880 path = gtk_file_system_make_path (impl->file_system, impl->current_folder, filename, &error);
3884 error_building_filename_dialog (impl, impl->current_folder, filename, error);
3893 struct get_paths_closure {
3894 GtkFileChooserDefault *impl;
3896 GtkFilePath *path_from_entry;
3900 get_paths_foreach (GtkTreeModel *model,
3905 struct get_paths_closure *info;
3906 const GtkFilePath *file_path;
3907 GtkFileSystemModel *fs_model;
3908 GtkTreeIter sel_iter;
3911 fs_model = info->impl->browse_files_model;
3912 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
3914 file_path = _gtk_file_system_model_get_path (GTK_FILE_SYSTEM_MODEL (fs_model), &sel_iter);
3916 return; /* We are on the editable row */
3918 if (!info->path_from_entry
3919 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
3920 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
3924 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
3926 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3927 struct get_paths_closure info;
3931 info.path_from_entry = NULL;
3933 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
3934 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3936 gboolean is_valid, is_empty;
3938 info.path_from_entry = check_save_entry (impl, &is_valid, &is_empty);
3939 if (!is_valid && !is_empty)
3943 if (!info.path_from_entry || impl->select_multiple)
3945 GtkTreeSelection *selection;
3947 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3948 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
3951 if (info.path_from_entry)
3952 info.result = g_slist_prepend (info.result, info.path_from_entry);
3954 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
3955 * fall back to the current directory */
3956 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
3957 info.result == NULL)
3959 info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
3962 return g_slist_reverse (info.result);
3965 static GtkFilePath *
3966 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
3968 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3970 if (impl->preview_path)
3971 return gtk_file_path_copy (impl->preview_path);
3976 static GtkFileSystem *
3977 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
3979 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
3981 return impl->file_system;
3984 /* Shows or hides the filter widgets */
3986 toolbar_show_filters (GtkFileChooserDefault *impl,
3990 gtk_widget_show (impl->filter_combo);
3992 gtk_widget_hide (impl->filter_combo);
3996 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
3997 GtkFileFilter *filter)
3999 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4002 if (g_slist_find (impl->filters, filter))
4004 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
4008 g_object_ref (filter);
4009 gtk_object_sink (GTK_OBJECT (filter));
4010 impl->filters = g_slist_append (impl->filters, filter);
4012 name = gtk_file_filter_get_name (filter);
4014 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
4016 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
4018 if (!g_slist_find (impl->filters, impl->current_filter))
4019 set_current_filter (impl, filter);
4021 toolbar_show_filters (impl, TRUE);
4025 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
4026 GtkFileFilter *filter)
4028 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4029 GtkTreeModel *model;
4033 filter_index = g_slist_index (impl->filters, filter);
4035 if (filter_index < 0)
4037 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
4041 impl->filters = g_slist_remove (impl->filters, filter);
4043 if (filter == impl->current_filter)
4046 set_current_filter (impl, impl->filters->data);
4048 set_current_filter (impl, NULL);
4051 /* Remove row from the combo box */
4052 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
4053 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
4054 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
4056 g_object_unref (filter);
4059 toolbar_show_filters (impl, FALSE);
4063 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
4065 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4067 return g_slist_copy (impl->filters);
4070 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
4072 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
4075 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
4079 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
4080 const GtkFilePath *path,
4083 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4087 /* Test validity of path here. */
4088 if (!check_is_folder (impl->file_system, path, error))
4091 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
4093 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
4096 impl->num_shortcuts++;
4098 if (impl->shortcuts_filter_model)
4099 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
4105 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
4106 const GtkFilePath *path,
4109 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4115 if (impl->num_shortcuts == 0)
4118 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
4119 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
4120 g_assert_not_reached ();
4122 for (i = 0; i < impl->num_shortcuts; i++)
4124 GtkFilePath *shortcut;
4126 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
4127 g_assert (shortcut != NULL);
4129 if (gtk_file_path_compare (shortcut, path) == 0)
4131 /* The other columns are freed by the GtkTreeStore */
4132 gtk_file_path_free (shortcut);
4133 gtk_list_store_remove (impl->shortcuts_model, &iter);
4134 impl->num_shortcuts--;
4138 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
4139 g_assert_not_reached ();
4144 uri = gtk_file_system_path_to_uri (impl->file_system, path);
4146 GTK_FILE_CHOOSER_ERROR,
4147 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
4148 _("shortcut %s does not exist"),
4156 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
4158 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4164 if (impl->num_shortcuts == 0)
4167 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
4168 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
4169 g_assert_not_reached ();
4173 for (i = 0; i < impl->num_shortcuts; i++)
4175 GtkFilePath *shortcut;
4177 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
4178 g_assert (shortcut != NULL);
4180 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
4182 if (i != impl->num_shortcuts - 1)
4184 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
4185 g_assert_not_reached ();
4189 return g_slist_reverse (list);
4192 /* Guesses a size based upon font sizes */
4194 find_good_size_from_style (GtkWidget *widget,
4198 GtkFileChooserDefault *impl;
4199 gint default_width, default_height;
4202 GtkRequisition preview_req;
4204 g_assert (widget->style != NULL);
4205 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4207 font_size = pango_font_description_get_size (widget->style->font_desc);
4208 font_size = PANGO_PIXELS (font_size);
4210 default_width = font_size * NUM_CHARS;
4211 default_height = font_size * NUM_LINES;
4213 /* Use at least the requisition size not including the preview widget */
4214 gtk_widget_size_request (widget, &req);
4216 if (impl->preview_widget_active && impl->preview_widget)
4217 gtk_widget_size_request (impl->preview_box, &preview_req);
4219 preview_req.width = 0;
4221 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
4222 default_height = MAX (default_height, req.height);
4224 *width = default_width;
4225 *height = default_height;
4229 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
4230 gint *default_width,
4231 gint *default_height)
4233 GtkFileChooserDefault *impl;
4235 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4237 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
4239 if (impl->preview_widget_active && impl->preview_widget)
4240 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
4244 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
4245 gboolean *resize_horizontally,
4246 gboolean *resize_vertically)
4248 GtkFileChooserDefault *impl;
4250 g_return_if_fail (resize_horizontally != NULL);
4251 g_return_if_fail (resize_vertically != NULL);
4253 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4255 *resize_horizontally = TRUE;
4256 *resize_vertically = TRUE;
4258 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4259 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4261 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4263 *resize_horizontally = FALSE;
4264 *resize_vertically = FALSE;
4269 struct switch_folder_closure {
4270 GtkFileChooserDefault *impl;
4271 const GtkFilePath *path;
4275 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
4277 switch_folder_foreach_cb (GtkTreeModel *model,
4282 struct switch_folder_closure *closure;
4283 GtkTreeIter child_iter;
4287 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
4289 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
4290 closure->num_selected++;
4293 /* Changes to the selected folder in the list view */
4295 switch_to_selected_folder (GtkFileChooserDefault *impl)
4297 GtkTreeSelection *selection;
4298 struct switch_folder_closure closure;
4300 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4301 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE);
4303 /* We do this with foreach() rather than get_selected() as we may be in
4304 * multiple selection mode
4307 closure.impl = impl;
4308 closure.path = NULL;
4309 closure.num_selected = 0;
4311 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4312 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
4314 g_assert (closure.path && closure.num_selected == 1);
4316 change_folder_and_display_error (impl, closure.path);
4319 /* Implementation for GtkFileChooserEmbed::should_respond() */
4321 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
4323 GtkFileChooserDefault *impl;
4324 GtkTreeSelection *selection;
4327 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4329 /* First, check the save entry. If it has a valid name, we are done */
4331 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4332 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4335 gboolean is_valid, is_empty;
4337 path = check_save_entry (impl, &is_valid, &is_empty);
4341 gtk_file_path_free (path);
4348 /* Second, do we have an empty selection */
4349 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4350 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4352 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4353 num_selected = gtk_tree_selection_count_selected_rows (selection);
4354 if (num_selected == 0)
4358 /* Third, should we return file names or folder names? */
4360 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4361 || impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4363 gboolean all_files, all_folders;
4365 selection_check (impl, NULL, &all_files, &all_folders);
4367 if (num_selected == 1)
4371 switch_to_selected_folder (impl);
4380 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
4381 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4382 /* There can be no files selected in folder mode since we don't show them,
4387 g_assert_not_reached ();
4391 /* Implementation for GtkFileChooserEmbed::initial_focus() */
4393 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
4395 GtkFileChooserDefault *impl;
4398 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4400 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
4401 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
4405 path = gtk_tree_path_new_from_indices (0, -1);
4406 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
4407 gtk_tree_path_free (path);
4409 widget = impl->browse_files_tree_view;
4411 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4412 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4413 widget = impl->save_file_name_entry;
4416 g_assert_not_reached ();
4420 gtk_widget_grab_focus (widget);
4424 set_current_filter (GtkFileChooserDefault *impl,
4425 GtkFileFilter *filter)
4427 if (impl->current_filter != filter)
4431 /* If we have filters, new filter must be one of them
4433 filter_index = g_slist_index (impl->filters, filter);
4434 if (impl->filters && filter_index < 0)
4437 if (impl->current_filter)
4438 g_object_unref (impl->current_filter);
4439 impl->current_filter = filter;
4440 if (impl->current_filter)
4442 g_object_ref (impl->current_filter);
4443 gtk_object_sink (GTK_OBJECT (filter));
4447 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
4450 install_list_model_filter (impl);
4452 g_object_notify (G_OBJECT (impl), "filter");
4457 filter_combo_changed (GtkComboBox *combo_box,
4458 GtkFileChooserDefault *impl)
4460 gint new_index = gtk_combo_box_get_active (combo_box);
4461 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
4463 set_current_filter (impl, new_filter);
4467 check_preview_change (GtkFileChooserDefault *impl)
4469 const GtkFilePath *new_path = NULL;
4470 const GtkFileInfo *new_info = NULL;
4472 /* FIXME #132255: Fixing preview for multiple selection involves getting the
4473 * full selection and diffing to find out what the most recently selected file
4474 * is; there is logic in GtkFileSelection that probably can be
4477 if (impl->sort_model && !impl->select_multiple)
4479 GtkTreeSelection *selection;
4482 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4483 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
4485 GtkTreeIter child_iter;
4487 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4488 &child_iter, &iter);
4490 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
4491 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4495 if (new_path != impl->preview_path &&
4496 !(new_path && impl->preview_path &&
4497 gtk_file_path_compare (new_path, impl->preview_path) == 0))
4499 if (impl->preview_path)
4501 gtk_file_path_free (impl->preview_path);
4502 g_free (impl->preview_display_name);
4507 impl->preview_path = gtk_file_path_copy (new_path);
4508 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
4512 impl->preview_path = NULL;
4513 impl->preview_display_name = NULL;
4516 if (impl->use_preview_label && impl->preview_label)
4517 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
4519 g_signal_emit_by_name (impl, "update-preview");
4523 /* Activates a volume by mounting it if necessary and then switching to its
4527 shortcuts_activate_volume (GtkFileChooserDefault *impl,
4528 GtkFileSystemVolume *volume)
4532 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
4537 if (!gtk_file_system_volume_mount (impl->file_system, volume, &error))
4541 msg = g_strdup_printf ("Could not mount %s:\n%s",
4542 gtk_file_system_volume_get_display_name (impl->file_system, volume),
4544 error_message (impl, msg);
4546 g_error_free (error);
4552 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
4553 change_folder_and_display_error (impl, path);
4554 gtk_file_path_free (path);
4557 /* Opens the folder or volume at the specified index in the shortcuts list */
4559 shortcuts_activate_item (GtkFileChooserDefault *impl,
4568 if (item_num == shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR)
4569 || item_num == shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR))
4572 path = gtk_tree_path_new_from_indices (item_num, -1);
4573 result = gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path);
4574 gtk_tree_path_free (path);
4579 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &data, -1);
4581 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
4582 if ((item_num >= start_row && item_num < start_row + impl->num_volumes)
4583 || (item_num == shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER) && impl->shortcuts_current_folder_is_volume))
4585 GtkFileSystemVolume *volume;
4588 shortcuts_activate_volume (impl, volume);
4592 const GtkFilePath *file_path;
4595 change_folder_and_display_error (impl, file_path);
4599 /* Callback used when a row in the shortcuts list is activated */
4601 shortcuts_row_activated_cb (GtkTreeView *tree_view,
4603 GtkTreeViewColumn *column,
4604 GtkFileChooserDefault *impl)
4608 GtkTreeIter child_iter;
4609 GtkTreePath *child_path;
4611 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
4614 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
4617 child_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &child_iter);
4621 selected = *gtk_tree_path_get_indices (child_path);
4622 gtk_tree_path_free (child_path);
4624 shortcuts_activate_item (impl, selected);
4628 shortcuts_select_func (GtkTreeSelection *selection,
4629 GtkTreeModel *model,
4631 gboolean path_currently_selected,
4634 GtkFileChooserDefault *impl = data;
4636 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
4640 list_selection_changed (GtkTreeSelection *selection,
4641 GtkFileChooserDefault *impl)
4643 /* See if we are in the new folder editable row for Save mode */
4644 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
4646 GtkTreeSelection *selection;
4647 GtkTreeIter iter, child_iter;
4648 const GtkFileInfo *info;
4650 g_assert (!impl->select_multiple);
4651 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4652 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4655 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4659 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4661 return; /* We are on the editable row for New Folder */
4664 update_chooser_entry (impl);
4665 check_preview_change (impl);
4666 bookmarks_check_add_sensitivity (impl);
4668 g_signal_emit_by_name (impl, "selection-changed", 0);
4671 /* Callback used when a row in the file list is activated */
4673 list_row_activated (GtkTreeView *tree_view,
4675 GtkTreeViewColumn *column,
4676 GtkFileChooserDefault *impl)
4678 GtkTreeIter iter, child_iter;
4679 const GtkFileInfo *info;
4681 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
4684 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
4686 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4688 if (gtk_file_info_get_is_folder (info))
4690 const GtkFilePath *file_path;
4692 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
4693 change_folder_and_display_error (impl, file_path);
4698 g_signal_emit_by_name (impl, "file-activated");
4702 path_bar_clicked (GtkPathBar *path_bar,
4703 GtkFilePath *file_path,
4704 GtkFileChooserDefault *impl)
4706 change_folder_and_display_error (impl, file_path);
4709 static const GtkFileInfo *
4710 get_list_file_info (GtkFileChooserDefault *impl,
4713 GtkTreeIter child_iter;
4715 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4719 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4723 list_icon_data_func (GtkTreeViewColumn *tree_column,
4724 GtkCellRenderer *cell,
4725 GtkTreeModel *tree_model,
4729 GtkFileChooserDefault *impl = data;
4730 GtkTreeIter child_iter;
4731 const GtkFilePath *path;
4734 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4737 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
4741 /* FIXME: NULL GError */
4742 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
4743 impl->icon_size, NULL);
4749 g_object_unref (pixbuf);
4753 list_name_data_func (GtkTreeViewColumn *tree_column,
4754 GtkCellRenderer *cell,
4755 GtkTreeModel *tree_model,
4759 GtkFileChooserDefault *impl = data;
4760 const GtkFileInfo *info = get_list_file_info (impl, iter);
4765 "text", _("Type name of new folder"),
4771 "text", gtk_file_info_get_display_name (info),
4777 list_size_data_func (GtkTreeViewColumn *tree_column,
4778 GtkCellRenderer *cell,
4779 GtkTreeModel *tree_model,
4783 GtkFileChooserDefault *impl = data;
4784 const GtkFileInfo *info = get_list_file_info (impl, iter);
4788 if (!info || gtk_file_info_get_is_folder (info))
4791 size = gtk_file_info_get_size (info);
4793 if (size < (gint64)1024)
4794 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
4795 else if (size < (gint64)1024*1024)
4796 str = g_strdup_printf (_("%.1f K"), size / (1024.));
4797 else if (size < (gint64)1024*1024*1024)
4798 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
4800 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
4810 /* Tree column data callback for the file list; fetches the mtime of a file */
4812 list_mtime_data_func (GtkTreeViewColumn *tree_column,
4813 GtkCellRenderer *cell,
4814 GtkTreeModel *tree_model,
4818 GtkFileChooserDefault *impl;
4819 const GtkFileInfo *info;
4820 GtkFileTime time_mtime, time_now;
4827 info = get_list_file_info (impl, iter);
4836 time_mtime = gtk_file_info_get_modification_time (info);
4837 g_date_set_time (&mtime, (GTime) time_mtime);
4839 time_now = (GTime ) time (NULL);
4840 g_date_set_time (&now, (GTime) time_now);
4842 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
4845 strcpy (buf, _("Today"));
4846 else if (days_diff == 1)
4847 strcpy (buf, _("Yesterday"));
4852 if (days_diff > 1 && days_diff < 7)
4853 format = "%A"; /* Days from last week */
4855 format = "%x"; /* Any other date */
4857 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
4858 strcpy (buf, _("Unknown"));
4867 _gtk_file_chooser_default_new (const char *file_system)
4869 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
4870 "file-system-backend", file_system,
4875 location_entry_create (GtkFileChooserDefault *impl)
4879 entry = _gtk_file_chooser_entry_new ();
4880 /* Pick a good width for the entry */
4881 gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
4882 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
4883 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
4884 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
4886 return GTK_WIDGET (entry);
4890 update_from_entry (GtkFileChooserDefault *impl,
4892 GtkFileChooserEntry *chooser_entry)
4894 const GtkFilePath *folder_path;
4895 const char *file_part;
4897 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
4898 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
4900 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
4902 error_message_with_parent (parent,
4903 _("Cannot change to the folder you specified as it is an invalid path."));
4907 if (file_part[0] == '\0')
4908 return change_folder_and_display_error (impl, folder_path);
4911 GtkFileFolder *folder = NULL;
4912 GtkFilePath *subfolder_path = NULL;
4913 GtkFileInfo *info = NULL;
4919 /* If the file part is non-empty, we need to figure out if it refers to a
4920 * folder within folder. We could optimize the case here where the folder
4921 * is already loaded for one of our tree models.
4925 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
4929 error_getting_info_dialog (impl, folder_path, error);
4934 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
4936 if (!subfolder_path)
4941 uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
4942 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
4945 error_message (impl, msg);
4952 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
4956 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4957 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4959 if (!change_folder_and_display_error (impl, folder_path))
4962 gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
4965 error_getting_info_dialog (impl, subfolder_path, error);
4970 if (gtk_file_info_get_is_folder (info))
4971 result = change_folder_and_display_error (impl, subfolder_path);
4977 result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
4980 _("Could not select %s:\n%s"),
4981 subfolder_path, error);
4987 g_object_unref (folder);
4989 gtk_file_path_free (subfolder_path);
4992 gtk_file_info_free (info);
4997 g_assert_not_reached ();
5001 location_popup_handler (GtkFileChooserDefault *impl)
5004 GtkWidget *toplevel;
5013 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
5014 if (!GTK_WIDGET_TOPLEVEL (toplevel))
5017 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5018 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5020 title = _("Open Location");
5024 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5025 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5026 title = ""; /* FIXME: #137272, fix for 2.4.1 */
5029 dialog = gtk_dialog_new_with_buttons (title,
5030 GTK_WINDOW (toplevel),
5031 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
5032 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
5033 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
5035 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
5036 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
5037 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
5038 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
5040 hbox = gtk_hbox_new (FALSE, 12);
5041 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
5042 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
5044 label = gtk_label_new_with_mnemonic (_("_Location:"));
5045 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
5047 entry = location_entry_create (impl);
5048 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
5049 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
5053 gtk_widget_show_all (dialog);
5057 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
5059 if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
5061 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5062 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5064 gtk_widget_grab_focus (impl->browse_files_tree_view);
5068 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5069 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5070 gtk_widget_grab_focus (impl->save_file_name_entry);
5078 GtkWidget *toplevel;
5080 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
5081 if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_WINDOW (toplevel)->focus_widget)
5082 gtk_widget_grab_focus (GTK_WINDOW (toplevel)->focus_widget);
5085 gtk_widget_destroy (dialog);
5088 /* Handler for the "up-folder" keybinding signal */
5090 up_folder_handler (GtkFileChooserDefault *impl)
5092 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
5095 /* Handler for the "down-folder" keybinding signal */
5097 down_folder_handler (GtkFileChooserDefault *impl)
5099 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
5102 /* Handler for the "home-folder" keybinding signal */
5104 home_folder_handler (GtkFileChooserDefault *impl)
5110 if (!impl->has_home)
5111 return; /* Should we put up an error dialog? */
5113 pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
5114 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5115 g_assert_not_reached ();
5117 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &path, -1);
5118 g_assert (path != NULL);
5120 change_folder_and_display_error (impl, path);
5125 /* Drag and drop interfaces */
5128 shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
5133 shortcuts_model_filter_init (ShortcutsModelFilter *model)
5138 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
5140 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
5143 ShortcutsModelFilter *model;
5147 model = SHORTCUTS_MODEL_FILTER (drag_source);
5149 pos = *gtk_tree_path_get_indices (path);
5150 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
5152 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
5155 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
5157 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
5159 GtkSelectionData *selection_data)
5161 ShortcutsModelFilter *model;
5163 model = SHORTCUTS_MODEL_FILTER (drag_source);
5170 /* Fill the GtkTreeDragSourceIface vtable */
5172 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
5174 iface->row_draggable = shortcuts_model_filter_row_draggable;
5175 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
5179 /* Fill the GtkTreeDragDestIface vtable */
5181 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
5183 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
5184 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
5188 static GtkTreeModel *
5189 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
5190 GtkTreeModel *child_model,
5193 ShortcutsModelFilter *model;
5195 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
5196 "child_model", child_model,
5197 "virtual_root", root,
5202 return GTK_TREE_MODEL (model);