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"
24 #include "gtkalignment.h"
25 #include "gtkbindings.h"
26 #include "gtkbutton.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrendererpixbuf.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkcellrenderertext.h"
31 #include "gtkcheckmenuitem.h"
32 #include "gtkcombobox.h"
34 #include "gtkexpander.h"
35 #include "gtkfilechooserdefault.h"
36 #include "gtkfilechooserembed.h"
37 #include "gtkfilechooserentry.h"
38 #include "gtkfilechooserutils.h"
39 #include "gtkfilechooser.h"
40 #include "gtkfilesystemmodel.h"
43 #include "gtkhpaned.h"
44 #include "gtkiconfactory.h"
45 #include "gtkicontheme.h"
47 #include "gtkimagemenuitem.h"
50 #include "gtkmarshalers.h"
51 #include "gtkmenuitem.h"
52 #include "gtkmessagedialog.h"
53 #include "gtkpathbar.h"
54 #include "gtkprivate.h"
55 #include "gtkscrolledwindow.h"
56 #include "gtkseparatormenuitem.h"
57 #include "gtksizegroup.h"
60 #include "gtktreednd.h"
61 #include "gtktreeprivate.h"
62 #include "gtktreeview.h"
63 #include "gtktreemodelsort.h"
64 #include "gtktreeselection.h"
65 #include "gtktreestore.h"
66 #include "gtktypebuiltins.h"
69 #if defined (G_OS_UNIX)
70 #include "gtkfilesystemunix.h"
71 #elif defined (G_OS_WIN32)
72 #include "gtkfilesystemwin32.h"
79 typedef struct _GtkFileChooserDefaultClass GtkFileChooserDefaultClass;
81 #define GTK_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
82 #define GTK_IS_FILE_CHOOSER_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_DEFAULT))
83 #define GTK_FILE_CHOOSER_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_DEFAULT, GtkFileChooserDefaultClass))
86 struct _GtkFileChooserDefaultClass
88 GtkVBoxClass parent_class;
91 struct _GtkFileChooserDefault
93 GtkVBox parent_instance;
95 GtkFileChooserAction action;
97 GtkFileSystem *file_system;
99 /* Save mode widgets */
100 GtkWidget *save_widgets;
102 GtkWidget *save_file_name_entry;
103 GtkWidget *save_folder_label;
104 GtkWidget *save_folder_combo;
105 GtkWidget *save_extra_align;
106 GtkWidget *save_expander;
108 /* The file browsing widgets */
109 GtkWidget *browse_widgets;
110 GtkWidget *browse_shortcuts_tree_view;
111 GtkWidget *browse_shortcuts_add_button;
112 GtkWidget *browse_shortcuts_remove_button;
113 GtkWidget *browse_files_tree_view;
114 GtkWidget *browse_files_popup_menu;
115 GtkWidget *browse_files_popup_menu_add_shortcut_item;
116 GtkWidget *browse_files_popup_menu_hidden_files_item;
117 GtkWidget *browse_new_folder_button;
118 GtkWidget *browse_path_bar;
119 GtkWidget *browse_extra_align;
121 GtkFileSystemModel *browse_files_model;
123 GtkWidget *filter_combo_hbox;
124 GtkWidget *filter_combo;
125 GtkWidget *preview_box;
126 GtkWidget *preview_label;
127 GtkWidget *preview_widget;
128 GtkWidget *extra_widget;
130 GtkListStore *shortcuts_model;
131 GtkTreeModel *shortcuts_filter_model;
133 GtkTreeModelSort *sort_model;
135 GtkFileFilter *current_filter;
139 gboolean has_desktop;
145 gulong volumes_changed_id;
146 gulong bookmarks_changed_id;
148 GtkFilePath *current_volume_path;
149 GtkFilePath *current_folder;
150 GtkFilePath *preview_path;
151 char *preview_display_name;
153 GtkTreeViewColumn *list_name_column;
154 GtkCellRenderer *list_name_renderer;
156 GSource *edited_idle;
157 char *edited_new_text;
159 gulong settings_signal_id;
162 gulong toplevel_set_focus_id;
163 GtkWidget *toplevel_last_focus_widget;
166 GdkDragContext *shortcuts_drag_context;
167 GSource *shortcuts_drag_outside_idle;
172 guint local_only : 1;
173 guint preview_widget_active : 1;
174 guint use_preview_label : 1;
175 guint select_multiple : 1;
176 guint show_hidden : 1;
177 guint list_sort_ascending : 1;
178 guint changing_folder : 1;
179 guint shortcuts_current_folder_active : 1;
182 guint shortcuts_drag_outside : 1;
195 static guint signals[LAST_SIGNAL] = { 0 };
197 /* Column numbers for the shortcuts tree. Keep these in sync with shortcuts_model_create() */
199 SHORTCUTS_COL_PIXBUF,
202 SHORTCUTS_COL_IS_VOLUME,
203 SHORTCUTS_COL_REMOVABLE,
204 SHORTCUTS_COL_PIXBUF_VISIBLE,
205 SHORTCUTS_COL_NUM_COLUMNS
208 /* Column numbers for the file list */
213 FILE_LIST_COL_NUM_COLUMNS
216 /* Identifiers for target types */
222 /* Target types for dragging from the shortcuts list */
223 static GtkTargetEntry shortcuts_source_targets[] = {
224 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW }
227 static const int num_shortcuts_source_targets = (sizeof (shortcuts_source_targets)
228 / sizeof (shortcuts_source_targets[0]));
230 /* Target types for dropping into the shortcuts list */
231 static GtkTargetEntry shortcuts_dest_targets[] = {
232 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, GTK_TREE_MODEL_ROW },
233 { "text/uri-list", 0, TEXT_URI_LIST }
236 static const int num_shortcuts_dest_targets = (sizeof (shortcuts_dest_targets)
237 / sizeof (shortcuts_dest_targets[0]));
239 /* Target types for DnD from the file list */
240 static GtkTargetEntry file_list_source_targets[] = {
241 { "text/uri-list", 0, TEXT_URI_LIST }
244 static const int num_file_list_source_targets = (sizeof (file_list_source_targets)
245 / sizeof (file_list_source_targets[0]));
247 /* Interesting places in the shortcuts bar */
253 SHORTCUTS_BOOKMARKS_SEPARATOR,
255 SHORTCUTS_CURRENT_FOLDER_SEPARATOR,
256 SHORTCUTS_CURRENT_FOLDER
259 /* Icon size for if we can't get it from the theme */
260 #define FALLBACK_ICON_SIZE 20
262 #define PREVIEW_HBOX_SPACING 12
266 static void gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class);
267 static void gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface);
268 static void gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface);
269 static void gtk_file_chooser_default_init (GtkFileChooserDefault *impl);
271 static GObject* gtk_file_chooser_default_constructor (GType type,
272 guint n_construct_properties,
273 GObjectConstructParam *construct_params);
274 static void gtk_file_chooser_default_finalize (GObject *object);
275 static void gtk_file_chooser_default_set_property (GObject *object,
279 static void gtk_file_chooser_default_get_property (GObject *object,
283 static void gtk_file_chooser_default_dispose (GObject *object);
284 static void gtk_file_chooser_default_show_all (GtkWidget *widget);
285 static void gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
286 GtkWidget *previous_toplevel);
287 static void gtk_file_chooser_default_style_set (GtkWidget *widget,
288 GtkStyle *previous_style);
289 static void gtk_file_chooser_default_screen_changed (GtkWidget *widget,
290 GdkScreen *previous_screen);
292 static gboolean gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
293 const GtkFilePath *path,
295 static GtkFilePath * gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser);
296 static void gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
298 static gboolean gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
299 const GtkFilePath *path,
301 static void gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
302 const GtkFilePath *path);
303 static void gtk_file_chooser_default_select_all (GtkFileChooser *chooser);
304 static void gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser);
305 static GSList * gtk_file_chooser_default_get_paths (GtkFileChooser *chooser);
306 static GtkFilePath * gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser);
307 static GtkFileSystem *gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser);
308 static void gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
309 GtkFileFilter *filter);
310 static void gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
311 GtkFileFilter *filter);
312 static GSList * gtk_file_chooser_default_list_filters (GtkFileChooser *chooser);
313 static gboolean gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
314 const GtkFilePath *path,
316 static gboolean gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
317 const GtkFilePath *path,
319 static GSList * gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser);
321 static void gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
323 gint *default_height);
324 static void gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
325 gboolean *resize_horizontally,
326 gboolean *resize_vertically);
327 static gboolean gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed);
328 static void gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed);
330 static void location_popup_handler (GtkFileChooserDefault *impl);
331 static void up_folder_handler (GtkFileChooserDefault *impl);
332 static void down_folder_handler (GtkFileChooserDefault *impl);
333 static void home_folder_handler (GtkFileChooserDefault *impl);
334 static void update_appearance (GtkFileChooserDefault *impl);
336 static void set_current_filter (GtkFileChooserDefault *impl,
337 GtkFileFilter *filter);
338 static void check_preview_change (GtkFileChooserDefault *impl);
340 static void filter_combo_changed (GtkComboBox *combo_box,
341 GtkFileChooserDefault *impl);
342 static void shortcuts_row_activated_cb (GtkTreeView *tree_view,
344 GtkTreeViewColumn *column,
345 GtkFileChooserDefault *impl);
347 static gboolean shortcuts_key_press_event_cb (GtkWidget *widget,
349 GtkFileChooserDefault *impl);
351 static gboolean shortcuts_select_func (GtkTreeSelection *selection,
354 gboolean path_currently_selected,
356 static void shortcuts_activate_iter (GtkFileChooserDefault *impl,
358 static int shortcuts_get_index (GtkFileChooserDefault *impl,
359 ShortcutsIndex where);
360 static int shortcut_find_position (GtkFileChooserDefault *impl,
361 const GtkFilePath *path);
363 static void bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl);
365 static void list_selection_changed (GtkTreeSelection *tree_selection,
366 GtkFileChooserDefault *impl);
367 static void list_row_activated (GtkTreeView *tree_view,
369 GtkTreeViewColumn *column,
370 GtkFileChooserDefault *impl);
372 static void path_bar_clicked (GtkPathBar *path_bar,
373 GtkFilePath *file_path,
374 gboolean child_is_hidden,
375 GtkFileChooserDefault *impl);
377 static void add_bookmark_button_clicked_cb (GtkButton *button,
378 GtkFileChooserDefault *impl);
379 static void remove_bookmark_button_clicked_cb (GtkButton *button,
380 GtkFileChooserDefault *impl);
382 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
383 GtkCellRenderer *cell,
384 GtkTreeModel *tree_model,
387 static void list_name_data_func (GtkTreeViewColumn *tree_column,
388 GtkCellRenderer *cell,
389 GtkTreeModel *tree_model,
393 static void list_size_data_func (GtkTreeViewColumn *tree_column,
394 GtkCellRenderer *cell,
395 GtkTreeModel *tree_model,
399 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
400 GtkCellRenderer *cell,
401 GtkTreeModel *tree_model,
405 static const GtkFileInfo *get_list_file_info (GtkFileChooserDefault *impl,
408 static GObjectClass *parent_class;
412 /* Drag and drop interface declarations */
415 GtkTreeModelFilter parent;
417 GtkFileChooserDefault *impl;
418 } ShortcutsModelFilter;
421 GtkTreeModelFilterClass parent_class;
422 } ShortcutsModelFilterClass;
424 #define SHORTCUTS_MODEL_FILTER_TYPE (_shortcuts_model_filter_get_type ())
425 #define SHORTCUTS_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SHORTCUTS_MODEL_FILTER_TYPE, ShortcutsModelFilter))
427 static void shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface);
429 G_DEFINE_TYPE_WITH_CODE (ShortcutsModelFilter,
430 _shortcuts_model_filter,
431 GTK_TYPE_TREE_MODEL_FILTER,
432 G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
433 shortcuts_model_filter_drag_source_iface_init));
435 static GtkTreeModel *shortcuts_model_filter_new (GtkFileChooserDefault *impl,
436 GtkTreeModel *child_model,
442 _gtk_file_chooser_default_get_type (void)
444 static GType file_chooser_default_type = 0;
446 if (!file_chooser_default_type)
448 static const GTypeInfo file_chooser_default_info =
450 sizeof (GtkFileChooserDefaultClass),
451 NULL, /* base_init */
452 NULL, /* base_finalize */
453 (GClassInitFunc) gtk_file_chooser_default_class_init,
454 NULL, /* class_finalize */
455 NULL, /* class_data */
456 sizeof (GtkFileChooserDefault),
458 (GInstanceInitFunc) gtk_file_chooser_default_init,
461 static const GInterfaceInfo file_chooser_info =
463 (GInterfaceInitFunc) gtk_file_chooser_default_iface_init, /* interface_init */
464 NULL, /* interface_finalize */
465 NULL /* interface_data */
468 static const GInterfaceInfo file_chooser_embed_info =
470 (GInterfaceInitFunc) gtk_file_chooser_embed_default_iface_init, /* interface_init */
471 NULL, /* interface_finalize */
472 NULL /* interface_data */
475 file_chooser_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserDefault",
476 &file_chooser_default_info, 0);
478 g_type_add_interface_static (file_chooser_default_type,
479 GTK_TYPE_FILE_CHOOSER,
481 g_type_add_interface_static (file_chooser_default_type,
482 GTK_TYPE_FILE_CHOOSER_EMBED,
483 &file_chooser_embed_info);
486 return file_chooser_default_type;
490 gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
492 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
493 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
494 GtkBindingSet *binding_set;
496 parent_class = g_type_class_peek_parent (class);
498 gobject_class->finalize = gtk_file_chooser_default_finalize;
499 gobject_class->constructor = gtk_file_chooser_default_constructor;
500 gobject_class->set_property = gtk_file_chooser_default_set_property;
501 gobject_class->get_property = gtk_file_chooser_default_get_property;
502 gobject_class->dispose = gtk_file_chooser_default_dispose;
504 widget_class->show_all = gtk_file_chooser_default_show_all;
505 widget_class->hierarchy_changed = gtk_file_chooser_default_hierarchy_changed;
506 widget_class->style_set = gtk_file_chooser_default_style_set;
507 widget_class->screen_changed = gtk_file_chooser_default_screen_changed;
509 signals[LOCATION_POPUP] =
510 _gtk_binding_signal_new ("location-popup",
511 G_OBJECT_CLASS_TYPE (class),
512 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
513 G_CALLBACK (location_popup_handler),
515 _gtk_marshal_VOID__VOID,
518 _gtk_binding_signal_new ("up-folder",
519 G_OBJECT_CLASS_TYPE (class),
520 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
521 G_CALLBACK (up_folder_handler),
523 _gtk_marshal_VOID__VOID,
525 signals[DOWN_FOLDER] =
526 _gtk_binding_signal_new ("down-folder",
527 G_OBJECT_CLASS_TYPE (class),
528 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
529 G_CALLBACK (down_folder_handler),
531 _gtk_marshal_VOID__VOID,
533 signals[HOME_FOLDER] =
534 _gtk_binding_signal_new ("home-folder",
535 G_OBJECT_CLASS_TYPE (class),
536 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
537 G_CALLBACK (home_folder_handler),
539 _gtk_marshal_VOID__VOID,
542 binding_set = gtk_binding_set_by_class (class);
544 gtk_binding_entry_add_signal (binding_set,
545 GDK_l, GDK_CONTROL_MASK,
549 gtk_binding_entry_add_signal (binding_set,
554 gtk_binding_entry_add_signal (binding_set,
555 GDK_Up, GDK_MOD1_MASK,
558 gtk_binding_entry_add_signal (binding_set,
559 GDK_KP_Up, GDK_MOD1_MASK,
563 gtk_binding_entry_add_signal (binding_set,
564 GDK_Down, GDK_MOD1_MASK,
567 gtk_binding_entry_add_signal (binding_set,
568 GDK_KP_Down, GDK_MOD1_MASK,
572 gtk_binding_entry_add_signal (binding_set,
573 GDK_Home, GDK_MOD1_MASK,
576 gtk_binding_entry_add_signal (binding_set,
577 GDK_KP_Home, GDK_MOD1_MASK,
581 _gtk_file_chooser_install_properties (gobject_class);
583 gtk_settings_install_property (g_param_spec_string ("gtk-file-chooser-backend",
584 P_("Default file chooser backend"),
585 P_("Name of the GtkFileChooser backend to use by default"),
591 gtk_file_chooser_default_iface_init (GtkFileChooserIface *iface)
593 iface->select_path = gtk_file_chooser_default_select_path;
594 iface->unselect_path = gtk_file_chooser_default_unselect_path;
595 iface->select_all = gtk_file_chooser_default_select_all;
596 iface->unselect_all = gtk_file_chooser_default_unselect_all;
597 iface->get_paths = gtk_file_chooser_default_get_paths;
598 iface->get_preview_path = gtk_file_chooser_default_get_preview_path;
599 iface->get_file_system = gtk_file_chooser_default_get_file_system;
600 iface->set_current_folder = gtk_file_chooser_default_set_current_folder;
601 iface->get_current_folder = gtk_file_chooser_default_get_current_folder;
602 iface->set_current_name = gtk_file_chooser_default_set_current_name;
603 iface->add_filter = gtk_file_chooser_default_add_filter;
604 iface->remove_filter = gtk_file_chooser_default_remove_filter;
605 iface->list_filters = gtk_file_chooser_default_list_filters;
606 iface->add_shortcut_folder = gtk_file_chooser_default_add_shortcut_folder;
607 iface->remove_shortcut_folder = gtk_file_chooser_default_remove_shortcut_folder;
608 iface->list_shortcut_folders = gtk_file_chooser_default_list_shortcut_folders;
612 gtk_file_chooser_embed_default_iface_init (GtkFileChooserEmbedIface *iface)
614 iface->get_default_size = gtk_file_chooser_default_get_default_size;
615 iface->get_resizable_hints = gtk_file_chooser_default_get_resizable_hints;
616 iface->should_respond = gtk_file_chooser_default_should_respond;
617 iface->initial_focus = gtk_file_chooser_default_initial_focus;
620 gtk_file_chooser_default_init (GtkFileChooserDefault *impl)
622 impl->local_only = TRUE;
623 impl->preview_widget_active = TRUE;
624 impl->use_preview_label = TRUE;
625 impl->select_multiple = FALSE;
626 impl->show_hidden = FALSE;
627 impl->icon_size = FALLBACK_ICON_SIZE;
629 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (impl), TRUE);
630 gtk_box_set_spacing (GTK_BOX (impl), 12);
633 /* Frees the data columns for the specified iter in the shortcuts model*/
635 shortcuts_free_row_data (GtkFileChooserDefault *impl,
641 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
642 SHORTCUTS_COL_DATA, &col_data,
643 SHORTCUTS_COL_IS_VOLUME, &is_volume,
650 GtkFileSystemVolume *volume;
653 gtk_file_system_volume_free (impl->file_system, volume);
660 gtk_file_path_free (path);
664 /* Frees all the data columns in the shortcuts model */
666 shortcuts_free (GtkFileChooserDefault *impl)
670 if (!impl->shortcuts_model)
673 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
676 shortcuts_free_row_data (impl, &iter);
678 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
680 g_object_unref (impl->shortcuts_model);
681 impl->shortcuts_model = NULL;
685 gtk_file_chooser_default_finalize (GObject *object)
687 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
690 if (impl->shortcuts_filter_model)
691 g_object_unref (impl->shortcuts_filter_model);
693 shortcuts_free (impl);
695 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
696 impl->volumes_changed_id = 0;
697 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
698 impl->bookmarks_changed_id = 0;
699 g_object_unref (impl->file_system);
701 for (l = impl->filters; l; l = l->next)
703 GtkFileFilter *filter;
705 filter = GTK_FILE_FILTER (l->data);
706 g_object_unref (filter);
708 g_slist_free (impl->filters);
710 if (impl->current_filter)
711 g_object_unref (impl->current_filter);
713 if (impl->current_volume_path)
714 gtk_file_path_free (impl->current_volume_path);
716 if (impl->current_folder)
717 gtk_file_path_free (impl->current_folder);
719 if (impl->preview_path)
720 gtk_file_path_free (impl->preview_path);
722 /* Free all the Models we have */
723 if (impl->browse_files_model)
724 g_object_unref (impl->browse_files_model);
726 if (impl->sort_model)
727 g_object_unref (impl->sort_model);
729 g_free (impl->preview_display_name);
731 g_free (impl->edited_new_text);
733 G_OBJECT_CLASS (parent_class)->finalize (object);
736 /* Shows an error dialog set as transient for the specified window */
738 error_message_with_parent (GtkWindow *parent,
743 dialog = gtk_message_dialog_new (parent,
744 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
749 gtk_dialog_run (GTK_DIALOG (dialog));
750 gtk_widget_destroy (dialog);
753 /* Returns a toplevel GtkWindow, or NULL if none */
755 get_toplevel (GtkWidget *widget)
759 toplevel = gtk_widget_get_toplevel (widget);
760 if (!GTK_WIDGET_TOPLEVEL (toplevel))
763 return GTK_WINDOW (toplevel);
766 /* Shows an error dialog for the file chooser */
768 error_message (GtkFileChooserDefault *impl,
771 error_message_with_parent (get_toplevel (GTK_WIDGET (impl)), msg);
774 /* Shows a simple error dialog relative to a path. Frees the GError as well. */
776 error_dialog (GtkFileChooserDefault *impl,
778 const GtkFilePath *path,
781 g_return_if_fail (path != NULL);
785 char *uri = gtk_file_system_path_to_uri (impl->file_system, path);
786 char *text = g_strdup_printf (msg,
789 error_message (impl, text);
792 g_error_free (error);
796 /* Displays an error message about not being able to get information for a file.
797 * Frees the GError as well.
800 error_getting_info_dialog (GtkFileChooserDefault *impl,
801 const GtkFilePath *path,
805 _("Could not retrieve information about %s:\n%s"),
809 /* Shows an error dialog about not being able to add a bookmark */
811 error_could_not_add_bookmark_dialog (GtkFileChooserDefault *impl,
812 const GtkFilePath *path,
816 _("Could not add a bookmark for %s:\n%s"),
820 /* Shows an error dialog about not being able to compose a filename */
822 error_building_filename_dialog (GtkFileChooserDefault *impl,
823 const GtkFilePath *base_path,
824 const char *file_part,
830 uri = gtk_file_system_path_to_uri (impl->file_system, base_path);
831 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
834 error_message (impl, msg);
837 g_error_free (error);
840 /* Shows an error dialog when we cannot switch to a folder */
842 error_changing_folder_dialog (GtkFileChooserDefault *impl,
843 const GtkFilePath *path,
847 _("Could not change the current folder to %s:\n%s"),
852 /* Changes folders, displaying an error dialog if this fails */
854 change_folder_and_display_error (GtkFileChooserDefault *impl,
855 const GtkFilePath *path)
859 GtkFilePath *path_copy;
861 /* We copy the path because of this case:
863 * list_row_activated()
864 * fetches path from model; path belongs to the model (*)
865 * calls change_folder_and_display_error()
866 * calls _gtk_file_chooser_set_current_folder_path()
867 * changing folders fails, sets model to NULL, thus freeing the path in (*)
870 path_copy = gtk_file_path_copy (path);
873 result = _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path_copy, &error);
876 error_changing_folder_dialog (impl, path_copy, error);
878 gtk_file_path_free (path_copy);
884 update_preview_widget_visibility (GtkFileChooserDefault *impl)
886 if (impl->use_preview_label)
888 if (!impl->preview_label)
890 impl->preview_label = gtk_label_new (impl->preview_display_name);
891 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_label, FALSE, FALSE, 0);
892 gtk_box_reorder_child (GTK_BOX (impl->preview_box), impl->preview_label, 0);
893 gtk_widget_show (impl->preview_label);
898 if (impl->preview_label)
900 gtk_widget_destroy (impl->preview_label);
901 impl->preview_label = NULL;
905 if (impl->preview_widget_active && impl->preview_widget)
906 gtk_widget_show (impl->preview_box);
908 gtk_widget_hide (impl->preview_box);
910 g_signal_emit_by_name (impl, "default-size-changed");
914 set_preview_widget (GtkFileChooserDefault *impl,
915 GtkWidget *preview_widget)
917 if (preview_widget == impl->preview_widget)
920 if (impl->preview_widget)
921 gtk_container_remove (GTK_CONTAINER (impl->preview_box),
922 impl->preview_widget);
924 impl->preview_widget = preview_widget;
925 if (impl->preview_widget)
927 gtk_widget_show (impl->preview_widget);
928 gtk_box_pack_start (GTK_BOX (impl->preview_box), impl->preview_widget, TRUE, TRUE, 0);
929 gtk_box_reorder_child (GTK_BOX (impl->preview_box),
930 impl->preview_widget,
931 (impl->use_preview_label && impl->preview_label) ? 1 : 0);
934 update_preview_widget_visibility (impl);
937 /* Re-reads all the icons for the shortcuts, used when the theme changes */
939 shortcuts_reload_icons (GtkFileChooserDefault *impl)
943 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
949 gboolean pixbuf_visible;
952 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
953 SHORTCUTS_COL_DATA, &data,
954 SHORTCUTS_COL_IS_VOLUME, &is_volume,
955 SHORTCUTS_COL_PIXBUF_VISIBLE, &pixbuf_visible,
958 if (pixbuf_visible && data)
962 GtkFileSystemVolume *volume;
965 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
966 impl->icon_size, NULL);
970 const GtkFilePath *path;
973 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
974 impl->icon_size, NULL);
977 gtk_list_store_set (impl->shortcuts_model, &iter,
978 SHORTCUTS_COL_PIXBUF, pixbuf,
981 g_object_unref (pixbuf);
983 } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model),&iter));
986 /* If a shortcut corresponds to the current folder, selects it */
988 shortcuts_find_current_folder (GtkFileChooserDefault *impl)
990 GtkTreeSelection *selection;
994 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
996 pos = shortcut_find_position (impl, impl->current_folder);
999 gtk_tree_selection_unselect_all (selection);
1003 path = gtk_tree_path_new_from_indices (pos, -1);
1004 gtk_tree_selection_select_path (selection, path);
1005 gtk_tree_path_free (path);
1008 /* Convenience function to get the display name and icon info for a path */
1009 static GtkFileInfo *
1010 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, gboolean name_only, GError **error)
1012 GtkFilePath *parent_path;
1013 GtkFileFolder *parent_folder;
1018 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
1021 parent_folder = gtk_file_system_get_folder (file_system, parent_path ? parent_path : path,
1022 GTK_FILE_INFO_DISPLAY_NAME
1023 | (name_only ? 0 : GTK_FILE_INFO_IS_FOLDER),
1028 info = gtk_file_folder_get_info (parent_folder, parent_path ? path : NULL, error);
1029 g_object_unref (parent_folder);
1033 gtk_file_path_free (parent_path);
1037 /* Returns whether a path is a folder */
1039 check_is_folder (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
1044 /* Use get_file_info() rather than trying get_folder() and checking
1045 * for an error directly because older versions of the gnome-vfs
1046 * backend don't return an error immediately. This way is also
1047 * more efficient if we already have the parent folder.
1049 info = get_file_info (file_system, path, FALSE, error);
1054 is_folder = gtk_file_info_get_is_folder (info);
1055 gtk_file_info_free (info);
1060 GTK_FILE_SYSTEM_ERROR,
1061 GTK_FILE_SYSTEM_ERROR_NOT_FOLDER,
1063 gtk_file_info_get_display_name (info),
1064 g_strerror (ENOTDIR));
1072 /* Inserts a path in the shortcuts tree, making a copy of it; alternatively,
1073 * inserts a volume. A position of -1 indicates the end of the tree.
1076 shortcuts_insert_path (GtkFileChooserDefault *impl,
1079 GtkFileSystemVolume *volume,
1080 const GtkFilePath *path,
1093 label_copy = gtk_file_system_volume_get_display_name (impl->file_system, volume);
1094 pixbuf = gtk_file_system_volume_render_icon (impl->file_system, volume, GTK_WIDGET (impl),
1095 impl->icon_size, NULL);
1099 if (!check_is_folder (impl->file_system, path, error))
1103 label_copy = g_strdup (label);
1106 GtkFileInfo *info = get_file_info (impl->file_system, path, TRUE, error);
1111 label_copy = g_strdup (gtk_file_info_get_display_name (info));
1112 gtk_file_info_free (info);
1115 data = gtk_file_path_copy (path);
1116 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
1117 impl->icon_size, NULL);
1121 gtk_list_store_append (impl->shortcuts_model, &iter);
1123 gtk_list_store_insert (impl->shortcuts_model, &iter, pos);
1125 gtk_list_store_set (impl->shortcuts_model, &iter,
1126 SHORTCUTS_COL_PIXBUF, pixbuf,
1127 SHORTCUTS_COL_PIXBUF_VISIBLE, TRUE,
1128 SHORTCUTS_COL_NAME, label_copy,
1129 SHORTCUTS_COL_DATA, data,
1130 SHORTCUTS_COL_IS_VOLUME, is_volume,
1131 SHORTCUTS_COL_REMOVABLE, removable,
1134 g_free (label_copy);
1137 g_object_unref (pixbuf);
1142 /* Appends an item for the user's home directory to the shortcuts model */
1144 shortcuts_append_home (GtkFileChooserDefault *impl)
1147 GtkFilePath *home_path;
1150 home = g_get_home_dir ();
1154 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
1157 impl->has_home = shortcuts_insert_path (impl, -1, FALSE, NULL, home_path, _("Home"), FALSE, &error);
1158 if (!impl->has_home)
1159 error_getting_info_dialog (impl, home_path, error);
1161 gtk_file_path_free (home_path);
1164 /* Appends the ~/Desktop directory to the shortcuts model */
1166 shortcuts_append_desktop (GtkFileChooserDefault *impl)
1172 home = g_get_home_dir ();
1176 name = g_build_filename (home, "Desktop", NULL);
1177 path = gtk_file_system_filename_to_path (impl->file_system, name);
1180 impl->has_desktop = shortcuts_insert_path (impl, -1, FALSE, NULL, path, _("Desktop"), FALSE, NULL);
1181 /* We do not actually pop up an error dialog if there is no desktop directory
1182 * because some people may really not want to have one.
1185 gtk_file_path_free (path);
1188 /* Appends a list of GtkFilePath to the shortcuts model; returns how many were inserted */
1190 shortcuts_append_paths (GtkFileChooserDefault *impl,
1196 /* As there is no separator now, we want to start there.
1198 start_row = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1201 for (; paths; paths = paths->next)
1209 if (impl->local_only &&
1210 !gtk_file_system_path_is_local (impl->file_system, path))
1213 /* NULL GError, but we don't really want to show error boxes here */
1214 if (shortcuts_insert_path (impl, start_row + num_inserted, FALSE, NULL, path, NULL, TRUE, NULL))
1218 return num_inserted;
1221 /* Returns the index for the corresponding item in the shortcuts bar */
1223 shortcuts_get_index (GtkFileChooserDefault *impl,
1224 ShortcutsIndex where)
1230 if (where == SHORTCUTS_HOME)
1233 n += impl->has_home ? 1 : 0;
1235 if (where == SHORTCUTS_DESKTOP)
1238 n += impl->has_desktop ? 1 : 0;
1240 if (where == SHORTCUTS_VOLUMES)
1243 n += impl->num_volumes;
1245 if (where == SHORTCUTS_SHORTCUTS)
1248 n += impl->num_shortcuts;
1250 if (where == SHORTCUTS_BOOKMARKS_SEPARATOR)
1253 /* If there are no bookmarks there won't be a separator */
1254 n += (impl->num_bookmarks > 0) ? 1 : 0;
1256 if (where == SHORTCUTS_BOOKMARKS)
1259 n += impl->num_bookmarks;
1261 if (where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR)
1266 if (where == SHORTCUTS_CURRENT_FOLDER)
1269 g_assert_not_reached ();
1276 /* Removes the specified number of rows from the shortcuts list */
1278 shortcuts_remove_rows (GtkFileChooserDefault *impl,
1285 path = gtk_tree_path_new_from_indices (start_row, -1);
1287 for (; n_rows; n_rows--)
1291 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
1292 g_assert_not_reached ();
1294 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1295 SHORTCUTS_COL_NAME, &text, -1);
1296 g_print ("removing shortcut %s\n", text);
1298 shortcuts_free_row_data (impl, &iter);
1299 gtk_list_store_remove (impl->shortcuts_model, &iter);
1302 gtk_tree_path_free (path);
1305 /* Adds all the file system volumes to the shortcuts model */
1307 shortcuts_add_volumes (GtkFileChooserDefault *impl)
1312 gboolean old_changing_folders;
1314 old_changing_folders = impl->changing_folder;
1315 impl->changing_folder = TRUE;
1317 start_row = shortcuts_get_index (impl, SHORTCUTS_VOLUMES);
1318 shortcuts_remove_rows (impl, start_row, impl->num_volumes);
1319 impl->num_volumes = 0;
1321 list = gtk_file_system_list_volumes (impl->file_system);
1325 for (l = list; l; l = l->next)
1327 GtkFileSystemVolume *volume;
1331 if (impl->local_only)
1333 GtkFilePath *base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1334 gboolean is_local = gtk_file_system_path_is_local (impl->file_system, base_path);
1335 gtk_file_path_free (base_path);
1339 gtk_file_system_volume_free (impl->file_system, volume);
1344 if (shortcuts_insert_path (impl, start_row + n, TRUE, volume, NULL, NULL, FALSE, NULL))
1347 gtk_file_system_volume_free (impl->file_system, volume);
1350 impl->num_volumes = n;
1351 g_slist_free (list);
1353 if (impl->shortcuts_filter_model)
1354 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1356 impl->changing_folder = old_changing_folders;
1359 /* Inserts a separator node in the shortcuts list */
1361 shortcuts_insert_separator (GtkFileChooserDefault *impl,
1362 ShortcutsIndex where)
1366 g_assert (where == SHORTCUTS_BOOKMARKS_SEPARATOR || where == SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1368 gtk_list_store_insert (impl->shortcuts_model, &iter,
1369 shortcuts_get_index (impl, where));
1370 gtk_list_store_set (impl->shortcuts_model, &iter,
1371 SHORTCUTS_COL_PIXBUF, NULL,
1372 SHORTCUTS_COL_PIXBUF_VISIBLE, FALSE,
1373 SHORTCUTS_COL_NAME, NULL,
1374 SHORTCUTS_COL_DATA, NULL,
1378 /* Updates the list of bookmarks */
1380 shortcuts_add_bookmarks (GtkFileChooserDefault *impl)
1383 gboolean old_changing_folders;
1385 old_changing_folders = impl->changing_folder;
1386 impl->changing_folder = TRUE;
1388 if (impl->num_bookmarks > 0)
1389 shortcuts_remove_rows (impl,
1390 shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR),
1391 impl->num_bookmarks + 1);
1393 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
1394 impl->num_bookmarks = shortcuts_append_paths (impl, bookmarks);
1395 gtk_file_paths_free (bookmarks);
1397 if (impl->num_bookmarks > 0)
1399 shortcuts_insert_separator (impl, SHORTCUTS_BOOKMARKS_SEPARATOR);
1401 if (impl->shortcuts_filter_model)
1402 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
1404 impl->changing_folder = old_changing_folders;
1407 /* Appends a separator and a row to the shortcuts list for the current folder */
1409 shortcuts_add_current_folder (GtkFileChooserDefault *impl)
1414 g_assert (!impl->shortcuts_current_folder_active);
1418 pos = shortcut_find_position (impl, impl->current_folder);
1421 GtkFileSystemVolume *volume;
1422 GtkFilePath *base_path;
1426 shortcuts_insert_separator (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1430 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER);
1432 volume = gtk_file_system_get_volume_for_path (impl->file_system, impl->current_folder);
1434 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1439 strcmp (gtk_file_path_get_string (base_path), gtk_file_path_get_string (impl->current_folder)) == 0)
1441 success = shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, NULL);
1443 gtk_file_system_volume_free (impl->file_system, volume);
1446 success = shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, NULL);
1449 gtk_file_path_free (base_path);
1452 shortcuts_remove_rows (impl, pos - 1, 1); /* remove the separator */
1454 impl->shortcuts_current_folder_active = success;
1458 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->save_folder_combo), pos);
1461 /* Updates the current folder row in the shortcuts model */
1463 shortcuts_update_current_folder (GtkFileChooserDefault *impl)
1467 pos = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1469 if (impl->shortcuts_current_folder_active)
1471 shortcuts_remove_rows (impl, pos, 2);
1472 impl->shortcuts_current_folder_active = FALSE;
1475 shortcuts_add_current_folder (impl);
1478 /* Filter function used for the shortcuts filter model */
1480 shortcuts_filter_cb (GtkTreeModel *model,
1484 GtkFileChooserDefault *impl;
1488 impl = GTK_FILE_CHOOSER_DEFAULT (data);
1490 path = gtk_tree_model_get_path (model, iter);
1494 pos = *gtk_tree_path_get_indices (path);
1495 gtk_tree_path_free (path);
1497 return (pos < shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR));
1500 /* Creates the list model for shortcuts */
1502 shortcuts_model_create (GtkFileChooserDefault *impl)
1504 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
1505 impl->shortcuts_model = gtk_list_store_new (SHORTCUTS_COL_NUM_COLUMNS,
1506 GDK_TYPE_PIXBUF, /* pixbuf */
1507 G_TYPE_STRING, /* name */
1508 G_TYPE_POINTER, /* path or volume */
1509 G_TYPE_BOOLEAN, /* is the previous column a volume? */
1510 G_TYPE_BOOLEAN, /* removable */
1511 G_TYPE_BOOLEAN); /* pixbuf cell visibility */
1513 if (impl->file_system)
1515 shortcuts_append_home (impl);
1516 shortcuts_append_desktop (impl);
1517 shortcuts_add_volumes (impl);
1518 shortcuts_add_bookmarks (impl);
1521 impl->shortcuts_filter_model = shortcuts_model_filter_new (impl,
1522 GTK_TREE_MODEL (impl->shortcuts_model),
1525 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1526 shortcuts_filter_cb,
1531 /* Callback used when the "New Folder" button is clicked */
1533 new_folder_button_clicked (GtkButton *button,
1534 GtkFileChooserDefault *impl)
1539 if (!impl->browse_files_model)
1540 return; /* FIXME: this sucks. Disable the New Folder button or something. */
1542 _gtk_file_system_model_add_editable (impl->browse_files_model, &iter);
1544 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->browse_files_model), &iter);
1545 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->browse_files_tree_view),
1546 path, impl->list_name_column,
1549 g_object_set (impl->list_name_renderer, "editable", TRUE, NULL);
1550 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view),
1552 impl->list_name_column,
1556 /* Idle handler for creating a new folder after editing its name cell, or for
1557 * canceling the editing.
1560 edited_idle_cb (GtkFileChooserDefault *impl)
1562 g_source_destroy (impl->edited_idle);
1563 impl->edited_idle = NULL;
1565 _gtk_file_system_model_remove_editable (impl->browse_files_model);
1566 g_object_set (impl->list_name_renderer, "editable", FALSE, NULL);
1568 if (impl->edited_new_text) /* not cancelled? */
1571 GtkFilePath *file_path;
1574 file_path = gtk_file_system_make_path (impl->file_system, impl->current_folder, impl->edited_new_text,
1579 if (gtk_file_system_create_folder (impl->file_system, file_path, &error))
1580 change_folder_and_display_error (impl, file_path);
1583 _("Could not create folder %s:\n%s"),
1586 gtk_file_path_free (file_path);
1589 error_building_filename_dialog (impl, impl->current_folder, impl->edited_new_text, error);
1591 g_free (impl->edited_new_text);
1592 impl->edited_new_text = NULL;
1599 queue_edited_idle (GtkFileChooserDefault *impl,
1600 const gchar *new_text)
1602 /* We create the folder in an idle handler so that we don't modify the tree
1606 g_assert (!impl->edited_idle);
1607 g_assert (!impl->edited_new_text);
1609 impl->edited_idle = g_idle_source_new ();
1610 g_source_set_closure (impl->edited_idle,
1611 g_cclosure_new_object (G_CALLBACK (edited_idle_cb),
1613 g_source_attach (impl->edited_idle, NULL);
1616 impl->edited_new_text = g_strdup (new_text);
1619 /* Callback used from the text cell renderer when the new folder is named */
1621 renderer_edited_cb (GtkCellRendererText *cell_renderer_text,
1623 const gchar *new_text,
1624 GtkFileChooserDefault *impl)
1626 queue_edited_idle (impl, new_text);
1629 /* Callback used from the text cell renderer when the new folder edition gets
1633 renderer_editing_canceled_cb (GtkCellRendererText *cell_renderer_text,
1634 GtkFileChooserDefault *impl)
1636 queue_edited_idle (impl, NULL);
1639 /* Creates the widgets for the filter combo box */
1641 filter_create (GtkFileChooserDefault *impl)
1643 impl->filter_combo = gtk_combo_box_new_text ();
1644 g_signal_connect (impl->filter_combo, "changed",
1645 G_CALLBACK (filter_combo_changed), impl);
1647 return impl->filter_combo;
1651 button_new (GtkFileChooserDefault *impl,
1653 const char *stock_id,
1663 button = gtk_button_new ();
1664 hbox = gtk_hbox_new (FALSE, 2);
1665 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1667 gtk_container_add (GTK_CONTAINER (button), align);
1668 gtk_container_add (GTK_CONTAINER (align), hbox);
1669 widget = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
1671 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1673 widget = gtk_label_new_with_mnemonic (text);
1674 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), GTK_WIDGET (button));
1675 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1677 gtk_widget_set_sensitive (button, sensitive);
1678 g_signal_connect (button, "clicked", callback, impl);
1680 gtk_widget_show_all (align);
1683 gtk_widget_show (button);
1688 /* Looks for a path among the shortcuts; returns its index or -1 if it doesn't exist */
1690 shortcut_find_position (GtkFileChooserDefault *impl,
1691 const GtkFilePath *path)
1695 int current_folder_separator_idx;
1697 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1700 current_folder_separator_idx = shortcuts_get_index (impl, SHORTCUTS_CURRENT_FOLDER_SEPARATOR);
1702 for (i = 0; i < current_folder_separator_idx; i++)
1707 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1708 SHORTCUTS_COL_DATA, &col_data,
1709 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1716 GtkFileSystemVolume *volume;
1717 GtkFilePath *base_path;
1721 base_path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
1723 exists = strcmp (gtk_file_path_get_string (path),
1724 gtk_file_path_get_string (base_path)) == 0;
1732 GtkFilePath *model_path;
1734 model_path = col_data;
1736 if (model_path && gtk_file_path_compare (model_path, path) == 0)
1741 gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
1747 /* Tries to add a bookmark from a path name */
1749 shortcuts_add_bookmark_from_path (GtkFileChooserDefault *impl,
1750 const GtkFilePath *path,
1755 if (shortcut_find_position (impl, path) != -1)
1758 /* FIXME: this check really belongs in gtk_file_system_insert_bookmark. */
1760 if (!check_is_folder (impl->file_system, path, &error))
1763 _("Could not add bookmark for %s because it is not a folder."),
1770 if (!gtk_file_system_insert_bookmark (impl->file_system, path, pos, &error))
1772 error_could_not_add_bookmark_dialog (impl, path, error);
1780 add_bookmark_foreach_cb (GtkTreeModel *model,
1785 GtkFileChooserDefault *impl;
1786 GtkFileSystemModel *fs_model;
1787 GtkTreeIter child_iter;
1788 const GtkFilePath *file_path;
1790 impl = (GtkFileChooserDefault *) data;
1792 fs_model = impl->browse_files_model;
1793 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, iter);
1795 file_path = _gtk_file_system_model_get_path (fs_model, &child_iter);
1796 shortcuts_add_bookmark_from_path (impl, file_path, -1);
1799 /* Adds a bookmark from the currently selected item in the file list */
1801 bookmarks_add_selected_folder (GtkFileChooserDefault *impl)
1803 GtkTreeSelection *selection;
1805 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1807 if (gtk_tree_selection_count_selected_rows (selection) == 0)
1808 shortcuts_add_bookmark_from_path (impl, impl->current_folder, -1);
1810 gtk_tree_selection_selected_foreach (selection,
1811 add_bookmark_foreach_cb,
1815 /* Callback used when the "Add bookmark" button is clicked */
1817 add_bookmark_button_clicked_cb (GtkButton *button,
1818 GtkFileChooserDefault *impl)
1820 bookmarks_add_selected_folder (impl);
1823 /* Returns TRUE plus an iter in the shortcuts_model if a row is selected;
1824 * returns FALSE if no shortcut is selected.
1827 shortcuts_get_selected (GtkFileChooserDefault *impl,
1830 GtkTreeSelection *selection;
1831 GtkTreeIter parent_iter;
1833 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
1835 if (!gtk_tree_selection_get_selected (selection, NULL, &parent_iter))
1838 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
1844 /* Removes the selected bookmarks */
1846 remove_selected_bookmarks (GtkFileChooserDefault *impl)
1855 if (!shortcuts_get_selected (impl, &iter))
1858 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
1859 SHORTCUTS_COL_DATA, &col_data,
1860 SHORTCUTS_COL_IS_VOLUME, &is_volume,
1861 SHORTCUTS_COL_REMOVABLE, &removable,
1863 g_assert (col_data != NULL);
1864 g_assert (!is_volume);
1872 if (!gtk_file_system_remove_bookmark (impl->file_system, path, &error))
1875 _("Could not remove bookmark for %s:\n%s"),
1881 /* Callback used when the "Remove bookmark" button is clicked */
1883 remove_bookmark_button_clicked_cb (GtkButton *button,
1884 GtkFileChooserDefault *impl)
1886 remove_selected_bookmarks (impl);
1889 struct selection_check_closure {
1890 GtkFileChooserDefault *impl;
1893 gboolean all_folders;
1896 /* Used from gtk_tree_selection_selected_foreach() */
1898 selection_check_foreach_cb (GtkTreeModel *model,
1903 struct selection_check_closure *closure;
1904 GtkTreeIter child_iter;
1905 const GtkFileInfo *info;
1909 closure->num_selected++;
1911 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1913 info = _gtk_file_system_model_get_info (closure->impl->browse_files_model, &child_iter);
1914 is_folder = gtk_file_info_get_is_folder (info);
1916 closure->all_folders &= is_folder;
1917 closure->all_files &= !is_folder;
1920 /* Checks whether the selected items in the file list are all files or all folders */
1922 selection_check (GtkFileChooserDefault *impl,
1924 gboolean *all_files,
1925 gboolean *all_folders)
1927 struct selection_check_closure closure;
1928 GtkTreeSelection *selection;
1930 closure.impl = impl;
1931 closure.num_selected = 0;
1932 closure.all_files = TRUE;
1933 closure.all_folders = TRUE;
1935 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1936 gtk_tree_selection_selected_foreach (selection,
1937 selection_check_foreach_cb,
1940 g_assert (closure.num_selected == 0 || !(closure.all_files && closure.all_folders));
1943 *num_selected = closure.num_selected;
1946 *all_files = closure.all_files;
1949 *all_folders = closure.all_folders;
1952 struct get_selected_path_closure {
1953 GtkFileChooserDefault *impl;
1954 const GtkFilePath *path;
1958 get_selected_path_foreach_cb (GtkTreeModel *model,
1963 struct get_selected_path_closure *closure;
1964 GtkTreeIter child_iter;
1968 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
1969 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
1972 /* Returns a selected path from the file list */
1973 static const GtkFilePath *
1974 get_selected_path (GtkFileChooserDefault *impl)
1976 struct get_selected_path_closure closure;
1977 GtkTreeSelection *selection;
1979 closure.impl = impl;
1980 closure.path = NULL;
1982 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
1983 gtk_tree_selection_selected_foreach (selection,
1984 get_selected_path_foreach_cb,
1987 return closure.path;
1990 /* Sensitize the "add bookmark" button if all the selected items are folders, or
1991 * if there are no selected items *and* the current folder is not in the
1992 * bookmarks list. De-sensitize the button otherwise.
1995 bookmarks_check_add_sensitivity (GtkFileChooserDefault *impl)
1998 gboolean all_folders;
2001 selection_check (impl, &num_selected, NULL, &all_folders);
2003 if (num_selected == 0)
2004 active = (shortcut_find_position (impl, impl->current_folder) == -1);
2005 else if (num_selected == 1)
2007 const GtkFilePath *path;
2009 path = get_selected_path (impl);
2010 active = all_folders && (shortcut_find_position (impl, path) == -1);
2013 active = all_folders;
2015 gtk_widget_set_sensitive (impl->browse_shortcuts_add_button, active);
2017 if (impl->browse_files_popup_menu_add_shortcut_item)
2018 gtk_widget_set_sensitive (impl->browse_files_popup_menu_add_shortcut_item,
2019 (num_selected == 0) ? FALSE : active);
2022 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
2023 * bookmark row is selected in the shortcuts tree.
2026 bookmarks_check_remove_sensitivity (GtkFileChooserDefault *impl)
2029 gboolean removable = FALSE;
2031 if (shortcuts_get_selected (impl, &iter))
2032 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2033 SHORTCUTS_COL_REMOVABLE, &removable,
2036 gtk_widget_set_sensitive (impl->browse_shortcuts_remove_button, removable);
2039 /* GtkWidget::drag-begin handler for the shortcuts list. */
2041 shortcuts_drag_begin_cb (GtkWidget *widget,
2042 GdkDragContext *context,
2043 GtkFileChooserDefault *impl)
2046 impl->shortcuts_drag_context = g_object_ref (context);
2051 /* Removes the idle handler for outside drags */
2053 shortcuts_cancel_drag_outside_idle (GtkFileChooserDefault *impl)
2055 if (!impl->shortcuts_drag_outside_idle)
2058 g_source_destroy (impl->shortcuts_drag_outside_idle);
2059 impl->shortcuts_drag_outside_idle = NULL;
2063 /* GtkWidget::drag-end handler for the shortcuts list. */
2065 shortcuts_drag_end_cb (GtkWidget *widget,
2066 GdkDragContext *context,
2067 GtkFileChooserDefault *impl)
2070 g_object_unref (impl->shortcuts_drag_context);
2072 shortcuts_cancel_drag_outside_idle (impl);
2074 if (!impl->shortcuts_drag_outside)
2077 gtk_button_clicked (GTK_BUTTON (impl->browse_shortcuts_remove_button));
2079 impl->shortcuts_drag_outside = FALSE;
2083 /* GtkWidget::drag-data-delete handler for the shortcuts list. */
2085 shortcuts_drag_data_delete_cb (GtkWidget *widget,
2086 GdkDragContext *context,
2087 GtkFileChooserDefault *impl)
2089 g_signal_stop_emission_by_name (widget, "drag-data-delete");
2093 /* Creates a suitable drag cursor to indicate that the selected bookmark will be
2097 shortcuts_drag_set_delete_cursor (GtkFileChooserDefault *impl,
2100 GtkTreeView *tree_view;
2103 GdkPixmap *row_pixmap;
2108 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2110 /* Find the selected path and get its drag pixmap */
2112 if (!shortcuts_get_selected (impl, &iter))
2113 g_assert_not_reached ();
2115 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2117 row_pixmap = gtk_tree_view_create_row_drag_icon (tree_view, path);
2118 gtk_tree_path_free (path);
2127 pixbuf = gtk_widget_render_icon (impl->browse_shortcuts_tree_view,
2133 GdkPixmap *composite;
2134 int row_pixmap_width, row_pixmap_height;
2135 int pixbuf_width, pixbuf_height;
2136 int composite_width, composite_height;
2137 int pixbuf_x, pixbuf_y;
2138 GdkGC *gc, *mask_gc;
2140 GdkBitmap *pixbuf_mask;
2142 /* Create pixmap and mask for composite image */
2144 gdk_drawable_get_size (row_pixmap, &row_pixmap_width, &row_pixmap_height);
2145 pixbuf_width = gdk_pixbuf_get_width (pixbuf);
2146 pixbuf_height = gdk_pixbuf_get_height (pixbuf);
2148 composite_width = MAX (row_pixmap_width, pixbuf_width);
2149 composite_height = MAX (row_pixmap_height, pixbuf_height);
2151 row_pixmap_y = (composite_height - row_pixmap_height) / 2;
2153 if (gtk_widget_get_direction (impl->browse_shortcuts_tree_view) == GTK_TEXT_DIR_RTL)
2156 pixbuf_x = composite_width - pixbuf_width;
2158 pixbuf_y = (composite_height - pixbuf_height) / 2;
2160 composite = gdk_pixmap_new (row_pixmap, composite_width, composite_height, -1);
2161 gc = gdk_gc_new (composite);
2163 mask = gdk_pixmap_new (row_pixmap, composite_width, composite_height, 1);
2164 mask_gc = gdk_gc_new (mask);
2166 gdk_gc_set_foreground (mask_gc, &color);
2167 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, 0, composite_width, composite_height);
2170 color.green = 0xffff;
2171 color.blue = 0xffff;
2172 gdk_gc_set_rgb_fg_color (gc, &color);
2173 gdk_draw_rectangle (composite, gc, TRUE, 0, 0, composite_width, composite_height);
2175 /* Composite the row pixmap and the pixbuf */
2177 gdk_pixbuf_render_pixmap_and_mask_for_colormap
2179 gtk_widget_get_colormap (impl->browse_shortcuts_tree_view),
2180 NULL, &pixbuf_mask, 128);
2181 gdk_draw_drawable (mask, mask_gc, pixbuf_mask,
2184 pixbuf_width, pixbuf_height);
2185 g_object_unref (pixbuf_mask);
2187 gdk_draw_drawable (composite, gc, row_pixmap,
2190 row_pixmap_width, row_pixmap_height);
2192 gdk_gc_set_foreground (mask_gc, &color);
2193 gdk_draw_rectangle (mask, mask_gc, TRUE, 0, row_pixmap_y, row_pixmap_width, row_pixmap_height);
2195 gdk_draw_pixbuf (composite, gc, pixbuf,
2198 pixbuf_width, pixbuf_height,
2202 g_object_unref (pixbuf);
2203 g_object_unref (row_pixmap);
2205 row_pixmap = composite;
2209 /* The hotspot offsets here are copied from gtk_tree_view_drag_begin(), ugh */
2211 gtk_tree_view_get_path_at_pos (tree_view,
2212 tree_view->priv->press_start_x,
2213 tree_view->priv->press_start_y,
2219 gtk_drag_set_icon_pixmap (impl->shortcuts_drag_context,
2220 gdk_drawable_get_colormap (row_pixmap),
2223 tree_view->priv->press_start_x + 1,
2224 row_pixmap_y + cell_y + 1);
2226 g_object_unref (row_pixmap);
2228 g_object_unref (mask);
2231 /* We set the delete cursor and the shortcuts_drag_outside flag in an idle
2232 * handler so that we can tell apart the drag_leave event that comes right
2233 * before a drag_drop, from a normal drag_leave. We don't want to set the
2234 * cursor nor the flag in the latter case.
2237 shortcuts_drag_outside_idle_cb (GtkFileChooserDefault *impl)
2239 shortcuts_drag_set_delete_cursor (impl, TRUE);
2240 impl->shortcuts_drag_outside = TRUE;
2242 shortcuts_cancel_drag_outside_idle (impl);
2247 /* GtkWidget::drag-leave handler for the shortcuts list. We unhighlight the
2251 shortcuts_drag_leave_cb (GtkWidget *widget,
2252 GdkDragContext *context,
2254 GtkFileChooserDefault *impl)
2257 if (gtk_drag_get_source_widget (context) == widget && !impl->shortcuts_drag_outside_idle)
2259 impl->shortcuts_drag_outside_idle = g_idle_source_new ();
2260 g_source_set_closure (impl->shortcuts_drag_outside_idle,
2261 g_cclosure_new_object (G_CALLBACK (shortcuts_drag_outside_idle_cb),
2263 g_source_attach (impl->shortcuts_drag_outside_idle, NULL);
2267 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2269 GTK_TREE_VIEW_DROP_BEFORE);
2271 g_signal_stop_emission_by_name (widget, "drag-leave");
2274 /* Computes the appropriate row and position for dropping */
2276 shortcuts_compute_drop_position (GtkFileChooserDefault *impl,
2280 GtkTreeViewDropPosition *pos)
2282 GtkTreeView *tree_view;
2283 GtkTreeViewColumn *column;
2287 int bookmarks_index;
2289 tree_view = GTK_TREE_VIEW (impl->browse_shortcuts_tree_view);
2291 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2293 if (!gtk_tree_view_get_path_at_pos (tree_view,
2295 y - TREE_VIEW_HEADER_HEIGHT (tree_view),
2301 row = bookmarks_index + impl->num_bookmarks - 1;
2302 *path = gtk_tree_path_new_from_indices (row, -1);
2303 *pos = GTK_TREE_VIEW_DROP_AFTER;
2307 row = *gtk_tree_path_get_indices (*path);
2308 gtk_tree_view_get_background_area (tree_view, *path, column, &cell);
2309 gtk_tree_path_free (*path);
2311 if (row < bookmarks_index)
2313 row = bookmarks_index;
2314 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2316 else if (row > bookmarks_index + impl->num_bookmarks - 1)
2318 row = bookmarks_index + impl->num_bookmarks - 1;
2319 *pos = GTK_TREE_VIEW_DROP_AFTER;
2323 if (cell_y < cell.height / 2)
2324 *pos = GTK_TREE_VIEW_DROP_BEFORE;
2326 *pos = GTK_TREE_VIEW_DROP_AFTER;
2329 *path = gtk_tree_path_new_from_indices (row, -1);
2332 /* GtkWidget::drag-motion handler for the shortcuts list. We basically
2333 * implement the destination side of DnD by hand, due to limitations in
2334 * GtkTreeView's DnD API.
2337 shortcuts_drag_motion_cb (GtkWidget *widget,
2338 GdkDragContext *context,
2342 GtkFileChooserDefault *impl)
2345 GtkTreeViewDropPosition pos;
2346 GdkDragAction action;
2349 if (gtk_drag_get_source_widget (context) == widget)
2351 shortcuts_cancel_drag_outside_idle (impl);
2353 if (impl->shortcuts_drag_outside)
2355 shortcuts_drag_set_delete_cursor (impl, FALSE);
2356 impl->shortcuts_drag_outside = FALSE;
2361 if (context->suggested_action == GDK_ACTION_COPY || (context->actions & GDK_ACTION_COPY) != 0)
2362 action = GDK_ACTION_COPY;
2363 else if (context->suggested_action == GDK_ACTION_MOVE || (context->actions & GDK_ACTION_MOVE) != 0)
2364 action = GDK_ACTION_MOVE;
2371 shortcuts_compute_drop_position (impl, x, y, &path, &pos);
2372 gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), path, pos);
2373 gtk_tree_path_free (path);
2377 g_signal_stop_emission_by_name (widget, "drag-motion");
2381 gdk_drag_status (context, action, time_);
2388 /* GtkWidget::drag-drop handler for the shortcuts list. */
2390 shortcuts_drag_drop_cb (GtkWidget *widget,
2391 GdkDragContext *context,
2395 GtkFileChooserDefault *impl)
2398 shortcuts_cancel_drag_outside_idle (impl);
2401 g_signal_stop_emission_by_name (widget, "drag-drop");
2405 /* Converts raw selection data from text/uri-list to a list of strings. */
2407 split_uris (const char *data)
2416 /* We don't actually try to validate the URI according to RFC
2417 * 2396, or even check for allowed characters - we just ignore
2418 * comments and trim whitespace off the ends. We also
2419 * allow LF delimination as well as the specified CRLF.
2421 * We do allow comments like specified in RFC 2483.
2427 while (g_ascii_isspace (*p))
2431 while (*q && (*q != '\n') && (*q != '\r'))
2437 while (q > p && g_ascii_isspace (*q))
2441 uris = g_slist_prepend (uris, g_strndup (p, q - p + 1));
2444 p = strchr (p, '\n');
2449 uris = g_slist_reverse (uris);
2453 /* Parses a "text/uri-list" string and inserts its URIs as bookmarks */
2455 shortcuts_drop_uris (GtkFileChooserDefault *impl,
2461 uris = split_uris (data);
2463 for (l = uris; l; l = l->next)
2469 path = gtk_file_system_uri_to_path (impl->file_system, uri);
2473 if (shortcuts_add_bookmark_from_path (impl, path, position))
2476 gtk_file_path_free (path);
2482 msg = g_strdup_printf (_("Could not add a bookmark for %s because it is an invalid path name."),
2484 error_message (impl, msg);
2491 g_slist_free (uris);
2494 /* Reorders the selected bookmark to the specified position */
2496 shortcuts_reorder (GtkFileChooserDefault *impl,
2504 int bookmarks_index;
2505 const GtkFilePath *file_path;
2506 GtkFilePath *file_path_copy;
2509 /* Get the selected path */
2511 if (!shortcuts_get_selected (impl, &iter))
2512 g_assert_not_reached ();
2514 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
2515 old_position = *gtk_tree_path_get_indices (path);
2516 gtk_tree_path_free (path);
2518 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2519 old_position -= bookmarks_index;
2520 g_assert (old_position >= 0 && old_position < impl->num_bookmarks);
2522 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
2523 SHORTCUTS_COL_DATA, &col_data,
2524 SHORTCUTS_COL_IS_VOLUME, &is_volume,
2526 g_assert (col_data != NULL);
2527 g_assert (!is_volume);
2529 file_path = col_data;
2530 file_path_copy = gtk_file_path_copy (file_path); /* removal below will free file_path, so we need a copy */
2532 /* Remove the path from the old position and insert it in the new one */
2534 if (new_position > old_position)
2537 if (old_position == new_position)
2541 if (gtk_file_system_remove_bookmark (impl->file_system, file_path_copy, &error))
2542 shortcuts_add_bookmark_from_path (impl, file_path_copy, new_position);
2544 error_could_not_add_bookmark_dialog (impl, file_path_copy, error);
2548 gtk_file_path_free (file_path_copy);
2551 /* Callback used when we get the drag data for the bookmarks list. We add the
2552 * received URIs as bookmarks if they are folders.
2555 shortcuts_drag_data_received_cb (GtkWidget *widget,
2556 GdkDragContext *context,
2559 GtkSelectionData *selection_data,
2564 GtkFileChooserDefault *impl;
2565 GtkTreePath *tree_path;
2566 GtkTreeViewDropPosition tree_pos;
2568 int bookmarks_index;
2570 impl = GTK_FILE_CHOOSER_DEFAULT (data);
2572 /* Compute position */
2574 bookmarks_index = shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS);
2576 shortcuts_compute_drop_position (impl, x, y, &tree_path, &tree_pos);
2577 position = *gtk_tree_path_get_indices (tree_path);
2578 gtk_tree_path_free (tree_path);
2580 if (tree_pos == GTK_TREE_VIEW_DROP_AFTER)
2583 g_assert (position >= bookmarks_index);
2584 position -= bookmarks_index;
2586 if (selection_data->target == gdk_atom_intern ("text/uri-list", FALSE))
2587 shortcuts_drop_uris (impl, selection_data->data, position);
2588 else if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
2589 shortcuts_reorder (impl, position);
2591 g_signal_stop_emission_by_name (widget, "drag-data-received");
2594 /* Callback used when the selection in the shortcuts tree changes */
2596 shortcuts_selection_changed_cb (GtkTreeSelection *selection,
2597 GtkFileChooserDefault *impl)
2599 bookmarks_check_remove_sensitivity (impl);
2603 shortcuts_row_separator_func (GtkTreeModel *model,
2607 gint column = GPOINTER_TO_INT (data);
2610 gtk_tree_model_get (model, iter, column, &text, -1);
2620 /* Since GtkTreeView has a keybinding attached to '/', we need to catch
2621 * keypresses before the TreeView gets them.
2624 tree_view_keybinding_cb (GtkWidget *tree_view,
2626 GtkFileChooserDefault *impl)
2628 if (event->keyval == GDK_slash &&
2629 ! (event->state & gtk_accelerator_get_default_mod_mask ()))
2631 location_popup_handler (impl);
2639 /* Creates the widgets for the shortcuts and bookmarks tree */
2641 shortcuts_list_create (GtkFileChooserDefault *impl)
2644 GtkTreeSelection *selection;
2645 GtkTreeViewColumn *column;
2646 GtkCellRenderer *renderer;
2648 /* Scrolled window */
2650 swin = gtk_scrolled_window_new (NULL, NULL);
2651 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
2652 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2653 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
2655 gtk_widget_show (swin);
2659 impl->browse_shortcuts_tree_view = gtk_tree_view_new ();
2660 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2661 G_CALLBACK (tree_view_keybinding_cb), impl);
2662 atk_object_set_name (gtk_widget_get_accessible (impl->browse_shortcuts_tree_view), _("Shortcuts"));
2663 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), FALSE);
2665 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), impl->shortcuts_filter_model);
2667 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2669 shortcuts_source_targets,
2670 num_shortcuts_source_targets,
2673 gtk_drag_dest_set (impl->browse_shortcuts_tree_view,
2674 GTK_DEST_DEFAULT_ALL,
2675 shortcuts_dest_targets,
2676 num_shortcuts_dest_targets,
2677 GDK_ACTION_COPY | GDK_ACTION_MOVE);
2679 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view));
2680 gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
2681 gtk_tree_selection_set_select_function (selection,
2682 shortcuts_select_func,
2685 g_signal_connect (selection, "changed",
2686 G_CALLBACK (shortcuts_selection_changed_cb), impl);
2688 g_signal_connect (impl->browse_shortcuts_tree_view, "row-activated",
2689 G_CALLBACK (shortcuts_row_activated_cb), impl);
2691 g_signal_connect (impl->browse_shortcuts_tree_view, "key-press-event",
2692 G_CALLBACK (shortcuts_key_press_event_cb), impl);
2694 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-begin",
2695 G_CALLBACK (shortcuts_drag_begin_cb), impl);
2696 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-end",
2697 G_CALLBACK (shortcuts_drag_end_cb), impl);
2698 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-delete",
2699 G_CALLBACK (shortcuts_drag_data_delete_cb), impl);
2701 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-leave",
2702 G_CALLBACK (shortcuts_drag_leave_cb), impl);
2703 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-motion",
2704 G_CALLBACK (shortcuts_drag_motion_cb), impl);
2705 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-drop",
2706 G_CALLBACK (shortcuts_drag_drop_cb), impl);
2707 g_signal_connect (impl->browse_shortcuts_tree_view, "drag-data-received",
2708 G_CALLBACK (shortcuts_drag_data_received_cb), impl);
2710 gtk_container_add (GTK_CONTAINER (swin), impl->browse_shortcuts_tree_view);
2711 gtk_widget_show (impl->browse_shortcuts_tree_view);
2715 column = gtk_tree_view_column_new ();
2716 gtk_tree_view_column_set_title (column, _("Folder"));
2718 renderer = gtk_cell_renderer_pixbuf_new ();
2719 gtk_tree_view_column_pack_start (column, renderer, FALSE);
2720 gtk_tree_view_column_set_attributes (column, renderer,
2721 "pixbuf", SHORTCUTS_COL_PIXBUF,
2722 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
2725 renderer = gtk_cell_renderer_text_new ();
2726 gtk_tree_view_column_pack_start (column, renderer, TRUE);
2727 gtk_tree_view_column_set_attributes (column, renderer,
2728 "text", SHORTCUTS_COL_NAME,
2731 gtk_tree_view_set_row_separator_func (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view),
2732 shortcuts_row_separator_func,
2733 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
2736 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_shortcuts_tree_view), column);
2741 /* Creates the widgets for the shortcuts/bookmarks pane */
2743 shortcuts_pane_create (GtkFileChooserDefault *impl,
2744 GtkSizeGroup *size_group)
2750 vbox = gtk_vbox_new (FALSE, 6);
2751 gtk_widget_show (vbox);
2753 /* Shortcuts tree */
2755 widget = shortcuts_list_create (impl);
2756 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
2758 /* Box for buttons */
2760 hbox = gtk_hbox_new (TRUE, 6);
2761 gtk_size_group_add_widget (size_group, hbox);
2762 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
2763 gtk_widget_show (hbox);
2765 /* Add bookmark button */
2767 impl->browse_shortcuts_add_button = button_new (impl,
2772 G_CALLBACK (add_bookmark_button_clicked_cb));
2773 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_add_button, TRUE, TRUE, 0);
2775 /* Remove bookmark button */
2777 impl->browse_shortcuts_remove_button = button_new (impl,
2782 G_CALLBACK (remove_bookmark_button_clicked_cb));
2783 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_shortcuts_remove_button, TRUE, TRUE, 0);
2788 /* Handles key press events on the file list, so that we can trap Enter to
2789 * activate the default button on our own. Also, checks to see if '/' has been
2790 * pressed. See comment by tree_view_keybinding_cb() for more details.
2793 trap_activate_cb (GtkWidget *widget,
2797 GtkFileChooserDefault *impl;
2799 impl = (GtkFileChooserDefault *) data;
2801 if (event->keyval == GDK_slash &&
2802 ! (event->state & gtk_accelerator_get_default_mod_mask ()))
2804 location_popup_handler (impl);
2808 if (event->keyval == GDK_Return
2809 || event->keyval == GDK_ISO_Enter
2810 || event->keyval == GDK_KP_Enter
2811 || event->keyval == GDK_space)
2815 window = get_toplevel (widget);
2817 && widget != window->default_widget
2818 && !(widget == window->focus_widget &&
2819 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
2821 gtk_window_activate_default (window);
2829 /* Callback used when the file list's popup menu is detached */
2831 popup_menu_detach_cb (GtkWidget *attach_widget,
2834 GtkFileChooserDefault *impl;
2836 impl = g_object_get_data (G_OBJECT (attach_widget), "GtkFileChooserDefault");
2837 g_assert (GTK_IS_FILE_CHOOSER_DEFAULT (impl));
2839 impl->browse_files_popup_menu = NULL;
2840 impl->browse_files_popup_menu_add_shortcut_item = NULL;
2841 impl->browse_files_popup_menu_hidden_files_item = NULL;
2844 /* Callback used when the "Add to Shortcuts" menu item is activated */
2846 add_to_shortcuts_cb (GtkMenuItem *item,
2847 GtkFileChooserDefault *impl)
2849 bookmarks_add_selected_folder (impl);
2852 /* Callback used when the "Show Hidden Files" menu item is toggled */
2854 show_hidden_toggled_cb (GtkCheckMenuItem *item,
2855 GtkFileChooserDefault *impl)
2858 "show-hidden", gtk_check_menu_item_get_active (item),
2862 /* Constructs the popup menu for the file list if needed */
2864 file_list_build_popup_menu (GtkFileChooserDefault *impl)
2868 if (impl->browse_files_popup_menu)
2871 impl->browse_files_popup_menu = gtk_menu_new ();
2872 gtk_menu_attach_to_widget (GTK_MENU (impl->browse_files_popup_menu),
2873 impl->browse_files_tree_view,
2874 popup_menu_detach_cb);
2876 item = gtk_image_menu_item_new_with_mnemonic (_("_Add to Shortcuts"));
2877 impl->browse_files_popup_menu_add_shortcut_item = item;
2878 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
2879 gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
2880 gtk_widget_set_sensitive (item, FALSE);
2881 g_signal_connect (item, "activate",
2882 G_CALLBACK (add_to_shortcuts_cb), impl);
2883 gtk_widget_show (item);
2884 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2886 item = gtk_separator_menu_item_new ();
2887 gtk_widget_show (item);
2888 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2890 item = gtk_check_menu_item_new_with_mnemonic (_("Show _Hidden Files"));
2891 impl->browse_files_popup_menu_hidden_files_item = item;
2892 g_signal_connect (item, "toggled",
2893 G_CALLBACK (show_hidden_toggled_cb), impl);
2894 gtk_widget_show (item);
2895 gtk_menu_shell_append (GTK_MENU_SHELL (impl->browse_files_popup_menu), item);
2898 /* Updates the popup menu for the file list, creating it if necessary */
2900 file_list_update_popup_menu (GtkFileChooserDefault *impl)
2902 file_list_build_popup_menu (impl);
2904 /* The sensitivity of the Add to Shortcuts item is set in
2905 * bookmarks_check_add_sensitivity()
2908 g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
2909 G_CALLBACK (show_hidden_toggled_cb), impl);
2910 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (impl->browse_files_popup_menu_hidden_files_item),
2912 g_signal_handlers_unblock_by_func (impl->browse_files_popup_menu_hidden_files_item,
2913 G_CALLBACK (show_hidden_toggled_cb), impl);
2917 popup_position_func (GtkMenu *menu,
2923 GtkWidget *widget = GTK_WIDGET (user_data);
2924 GdkScreen *screen = gtk_widget_get_screen (widget);
2927 GdkRectangle monitor;
2929 g_return_if_fail (GTK_WIDGET_REALIZED (widget));
2931 gdk_window_get_origin (widget->window, x, y);
2933 gtk_widget_size_request (GTK_WIDGET (menu), &req);
2935 *x += (widget->allocation.width - req.width) / 2;
2936 *y += (widget->allocation.height - req.height) / 2;
2938 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
2939 gtk_menu_set_monitor (menu, monitor_num);
2940 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2942 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
2943 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
2949 file_list_popup_menu (GtkFileChooserDefault *impl,
2950 GdkEventButton *event)
2952 file_list_update_popup_menu (impl);
2954 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
2955 NULL, NULL, NULL, NULL,
2956 event->button, event->time);
2959 gtk_menu_popup (GTK_MENU (impl->browse_files_popup_menu),
2961 popup_position_func, impl->browse_files_tree_view,
2962 0, GDK_CURRENT_TIME);
2963 gtk_menu_shell_select_first (GTK_MENU_SHELL (impl->browse_files_popup_menu),
2969 /* Callback used for the GtkWidget::popup-menu signal of the file list */
2971 list_popup_menu_cb (GtkWidget *widget,
2972 GtkFileChooserDefault *impl)
2974 file_list_popup_menu (impl, NULL);
2978 /* Callback used when a button is pressed on the file list. We trap button 3 to
2979 * bring up a popup menu.
2982 list_button_press_event_cb (GtkWidget *widget,
2983 GdkEventButton *event,
2984 GtkFileChooserDefault *impl)
2986 if (event->button != 3)
2989 file_list_popup_menu (impl, event);
2993 /* Creates the widgets for the file list */
2995 create_file_list (GtkFileChooserDefault *impl)
2998 GtkTreeSelection *selection;
2999 GtkTreeViewColumn *column;
3000 GtkCellRenderer *renderer;
3002 /* Scrolled window */
3004 swin = gtk_scrolled_window_new (NULL, NULL);
3005 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
3006 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
3007 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin),
3010 /* Tree/list view */
3012 impl->browse_files_tree_view = gtk_tree_view_new ();
3013 g_object_set_data (G_OBJECT (impl->browse_files_tree_view), "GtkFileChooserDefault", impl);
3014 atk_object_set_name (gtk_widget_get_accessible (impl->browse_files_tree_view), _("Files"));
3016 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->browse_files_tree_view), TRUE);
3017 gtk_container_add (GTK_CONTAINER (swin), impl->browse_files_tree_view);
3018 g_signal_connect (impl->browse_files_tree_view, "row-activated",
3019 G_CALLBACK (list_row_activated), impl);
3020 g_signal_connect (impl->browse_files_tree_view, "key-press-event",
3021 G_CALLBACK (trap_activate_cb), impl);
3022 g_signal_connect (impl->browse_files_tree_view, "popup-menu",
3023 G_CALLBACK (list_popup_menu_cb), impl);
3024 g_signal_connect (impl->browse_files_tree_view, "button-press-event",
3025 G_CALLBACK (list_button_press_event_cb), impl);
3027 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3028 gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (impl->browse_files_tree_view),
3030 file_list_source_targets,
3031 num_file_list_source_targets,
3034 g_signal_connect (selection, "changed",
3035 G_CALLBACK (list_selection_changed), impl);
3037 /* Filename column */
3039 impl->list_name_column = gtk_tree_view_column_new ();
3040 gtk_tree_view_column_set_expand (impl->list_name_column, TRUE);
3041 gtk_tree_view_column_set_resizable (impl->list_name_column, TRUE);
3042 gtk_tree_view_column_set_title (impl->list_name_column, _("Name"));
3043 gtk_tree_view_column_set_sort_column_id (impl->list_name_column, FILE_LIST_COL_NAME);
3045 renderer = gtk_cell_renderer_pixbuf_new ();
3046 gtk_tree_view_column_pack_start (impl->list_name_column, renderer, FALSE);
3047 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, renderer,
3048 list_icon_data_func, impl, NULL);
3050 impl->list_name_renderer = gtk_cell_renderer_text_new ();
3051 g_object_set (impl->list_name_renderer,
3052 "ellipsize", PANGO_ELLIPSIZE_END,
3054 g_signal_connect (impl->list_name_renderer, "edited",
3055 G_CALLBACK (renderer_edited_cb), impl);
3056 g_signal_connect (impl->list_name_renderer, "editing-canceled",
3057 G_CALLBACK (renderer_editing_canceled_cb), impl);
3058 gtk_tree_view_column_pack_start (impl->list_name_column, impl->list_name_renderer, TRUE);
3059 gtk_tree_view_column_set_cell_data_func (impl->list_name_column, impl->list_name_renderer,
3060 list_name_data_func, impl, NULL);
3062 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), impl->list_name_column);
3066 column = gtk_tree_view_column_new ();
3067 gtk_tree_view_column_set_title (column, _("Size"));
3069 renderer = gtk_cell_renderer_text_new ();
3070 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3071 gtk_tree_view_column_set_cell_data_func (column, renderer,
3072 list_size_data_func, impl, NULL);
3073 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
3074 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3076 /* Modification time column */
3078 column = gtk_tree_view_column_new ();
3079 gtk_tree_view_column_set_resizable (column, TRUE);
3080 gtk_tree_view_column_set_title (column, _("Modified"));
3082 renderer = gtk_cell_renderer_text_new ();
3083 gtk_tree_view_column_pack_start (column, renderer, TRUE);
3084 gtk_tree_view_column_set_cell_data_func (column, renderer,
3085 list_mtime_data_func, impl, NULL);
3086 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
3087 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->browse_files_tree_view), column);
3088 gtk_widget_show_all (swin);
3094 create_path_bar (GtkFileChooserDefault *impl)
3096 GtkWidget *path_bar;
3098 path_bar = g_object_new (GTK_TYPE_PATH_BAR, NULL);
3099 _gtk_path_bar_set_file_system (GTK_PATH_BAR (path_bar), impl->file_system);
3104 /* Creates the widgets for the files/folders pane */
3106 file_pane_create (GtkFileChooserDefault *impl,
3107 GtkSizeGroup *size_group)
3113 vbox = gtk_vbox_new (FALSE, 6);
3114 gtk_widget_show (vbox);
3116 /* The path bar and 'Create Folder' button */
3117 hbox = gtk_hbox_new (FALSE, 12);
3118 gtk_widget_show (hbox);
3119 impl->browse_path_bar = create_path_bar (impl);
3120 g_signal_connect (impl->browse_path_bar, "path-clicked", G_CALLBACK (path_bar_clicked), impl);
3121 gtk_widget_show_all (impl->browse_path_bar);
3122 gtk_box_pack_start (GTK_BOX (hbox), impl->browse_path_bar, TRUE, TRUE, 0);
3125 impl->browse_new_folder_button = gtk_button_new_with_mnemonic (_("Create Fo_lder"));
3126 g_signal_connect (impl->browse_new_folder_button, "clicked",
3127 G_CALLBACK (new_folder_button_clicked), impl);
3128 gtk_box_pack_end (GTK_BOX (hbox), impl->browse_new_folder_button, FALSE, FALSE, 0);
3129 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
3131 /* Box for lists and preview */
3133 hbox = gtk_hbox_new (FALSE, PREVIEW_HBOX_SPACING);
3134 gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
3135 gtk_widget_show (hbox);
3139 widget = create_file_list (impl);
3140 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
3144 impl->preview_box = gtk_vbox_new (FALSE, 12);
3145 gtk_box_pack_start (GTK_BOX (hbox), impl->preview_box, FALSE, FALSE, 0);
3146 /* Don't show preview box initially */
3150 impl->filter_combo_hbox = gtk_hbox_new (FALSE, 12);
3152 widget = filter_create (impl);
3153 gtk_widget_show (widget);
3154 gtk_box_pack_end (GTK_BOX (impl->filter_combo_hbox), widget, FALSE, FALSE, 0);
3156 gtk_size_group_add_widget (size_group, impl->filter_combo_hbox);
3157 gtk_box_pack_end (GTK_BOX (vbox), impl->filter_combo_hbox, FALSE, FALSE, 0);
3161 /* Callback used when the "Browse for more folders" expander is toggled */
3163 expander_changed_cb (GtkExpander *expander,
3165 GtkFileChooserDefault *impl)
3167 update_appearance (impl);
3170 /* Callback used when the selection changes in the save folder combo box */
3172 save_folder_combo_changed_cb (GtkComboBox *combo,
3173 GtkFileChooserDefault *impl)
3177 if (impl->changing_folder)
3180 if (gtk_combo_box_get_active_iter (combo, &iter))
3181 shortcuts_activate_iter (impl, &iter);
3184 /* Creates the combo box with the save folders */
3186 save_folder_combo_create (GtkFileChooserDefault *impl)
3189 GtkCellRenderer *cell;
3191 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (impl->shortcuts_model));
3192 gtk_widget_show (combo);
3194 cell = gtk_cell_renderer_pixbuf_new ();
3195 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
3196 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3197 "pixbuf", SHORTCUTS_COL_PIXBUF,
3198 "visible", SHORTCUTS_COL_PIXBUF_VISIBLE,
3199 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3202 cell = gtk_cell_renderer_text_new ();
3203 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3204 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
3205 "text", SHORTCUTS_COL_NAME,
3206 "sensitive", SHORTCUTS_COL_PIXBUF_VISIBLE,
3209 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
3210 shortcuts_row_separator_func,
3211 GINT_TO_POINTER (SHORTCUTS_COL_NAME),
3214 g_signal_connect (combo, "changed",
3215 G_CALLBACK (save_folder_combo_changed_cb), impl);
3220 /* Creates the widgets specific to Save mode */
3222 save_widgets_create (GtkFileChooserDefault *impl)
3227 GtkWidget *alignment;
3229 vbox = gtk_vbox_new (FALSE, 12);
3231 table = gtk_table_new (2, 2, FALSE);
3232 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
3233 gtk_widget_show (table);
3234 gtk_table_set_row_spacings (GTK_TABLE (table), 12);
3235 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3239 widget = gtk_label_new_with_mnemonic (_("_Name:"));
3240 gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
3241 gtk_table_attach (GTK_TABLE (table), widget,
3245 gtk_widget_show (widget);
3247 impl->save_file_name_entry = _gtk_file_chooser_entry_new (TRUE);
3248 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3250 gtk_entry_set_width_chars (GTK_ENTRY (impl->save_file_name_entry), 45);
3251 gtk_entry_set_activates_default (GTK_ENTRY (impl->save_file_name_entry), TRUE);
3252 gtk_table_attach (GTK_TABLE (table), impl->save_file_name_entry,
3254 GTK_EXPAND | GTK_FILL, 0,
3256 gtk_widget_show (impl->save_file_name_entry);
3257 gtk_label_set_mnemonic_widget (GTK_LABEL (widget), impl->save_file_name_entry);
3260 impl->save_folder_label = gtk_label_new (NULL);
3261 gtk_misc_set_alignment (GTK_MISC (impl->save_folder_label), 0.0, 0.5);
3262 gtk_table_attach (GTK_TABLE (table), impl->save_folder_label,
3266 gtk_widget_show (impl->save_folder_label);
3268 impl->save_folder_combo = save_folder_combo_create (impl);
3269 gtk_table_attach (GTK_TABLE (table), impl->save_folder_combo,
3271 GTK_EXPAND | GTK_FILL, GTK_FILL,
3273 gtk_label_set_mnemonic_widget (GTK_LABEL (impl->save_folder_label), impl->save_folder_combo);
3276 impl->save_extra_align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3277 gtk_box_pack_start (GTK_BOX (vbox), impl->save_extra_align, FALSE, FALSE, 0);
3280 alignment = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
3281 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
3283 impl->save_expander = gtk_expander_new_with_mnemonic (_("_Browse for other folders"));
3284 gtk_container_add (GTK_CONTAINER (alignment), impl->save_expander);
3285 g_signal_connect (impl->save_expander, "notify::expanded",
3286 G_CALLBACK (expander_changed_cb),
3288 gtk_widget_show_all (alignment);
3293 /* Creates the main hpaned with the widgets shared by Open and Save mode */
3295 browse_widgets_create (GtkFileChooserDefault *impl)
3300 GtkSizeGroup *size_group;
3302 /* size group is used by the [+][-] buttons and the filter combo */
3303 size_group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
3304 vbox = gtk_vbox_new (FALSE, 12);
3307 hpaned = gtk_hpaned_new ();
3308 gtk_widget_show (hpaned);
3309 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
3310 gtk_box_pack_start (GTK_BOX (vbox), hpaned, TRUE, TRUE, 0);
3312 widget = shortcuts_pane_create (impl, size_group);
3313 gtk_paned_pack1 (GTK_PANED (hpaned), widget, FALSE, FALSE);
3314 widget = file_pane_create (impl, size_group);
3315 gtk_paned_pack2 (GTK_PANED (hpaned), widget, TRUE, FALSE);
3317 g_object_unref (size_group);
3319 /* Alignment to hold custom widget */
3320 impl->browse_extra_align = gtk_alignment_new (0.0, .5, 1.0, 1.0);
3321 gtk_box_pack_start (GTK_BOX (vbox), impl->browse_extra_align, FALSE, FALSE, 0);
3327 gtk_file_chooser_default_constructor (GType type,
3328 guint n_construct_properties,
3329 GObjectConstructParam *construct_params)
3331 GtkFileChooserDefault *impl;
3334 object = parent_class->constructor (type,
3335 n_construct_properties,
3337 impl = GTK_FILE_CHOOSER_DEFAULT (object);
3339 g_assert (impl->file_system);
3341 gtk_widget_push_composite_child ();
3343 /* Shortcuts model */
3345 shortcuts_model_create (impl);
3347 /* Widgets for Save mode */
3348 impl->save_widgets = save_widgets_create (impl);
3349 gtk_box_pack_start (GTK_BOX (impl), impl->save_widgets, FALSE, FALSE, 0);
3351 /* The browse widgets */
3352 impl->browse_widgets = browse_widgets_create (impl);
3353 gtk_box_pack_start (GTK_BOX (impl), impl->browse_widgets, TRUE, TRUE, 0);
3355 gtk_widget_pop_composite_child ();
3356 update_appearance (impl);
3361 /* Sets the extra_widget by packing it in the appropriate place */
3363 set_extra_widget (GtkFileChooserDefault *impl,
3364 GtkWidget *extra_widget)
3368 g_object_ref (extra_widget);
3369 /* FIXME: is this right ? */
3370 gtk_widget_show (extra_widget);
3373 if (impl->extra_widget)
3374 g_object_unref (impl->extra_widget);
3376 impl->extra_widget = extra_widget;
3380 set_local_only (GtkFileChooserDefault *impl,
3381 gboolean local_only)
3383 if (local_only != impl->local_only)
3385 impl->local_only = local_only;
3387 if (impl->shortcuts_model && impl->file_system)
3389 shortcuts_add_volumes (impl);
3390 shortcuts_add_bookmarks (impl);
3394 !gtk_file_system_path_is_local (impl->file_system, impl->current_folder))
3396 /* If we are pointing to a non-local folder, make an effort to change
3397 * back to a local folder, but it's really up to the app to not cause
3398 * such a situation, so we ignore errors.
3400 const gchar *home = g_get_home_dir ();
3401 GtkFilePath *home_path;
3406 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
3408 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), home_path, NULL);
3410 gtk_file_path_free (home_path);
3416 volumes_changed_cb (GtkFileSystem *file_system,
3417 GtkFileChooserDefault *impl)
3419 shortcuts_add_volumes (impl);
3422 /* Callback used when the set of bookmarks changes in the file system */
3424 bookmarks_changed_cb (GtkFileSystem *file_system,
3425 GtkFileChooserDefault *impl)
3427 shortcuts_add_bookmarks (impl);
3429 bookmarks_check_add_sensitivity (impl);
3430 bookmarks_check_remove_sensitivity (impl);
3433 /* Sets the file chooser to multiple selection mode */
3435 set_select_multiple (GtkFileChooserDefault *impl,
3436 gboolean select_multiple,
3437 gboolean property_notify)
3439 GtkTreeSelection *selection;
3440 GtkSelectionMode mode;
3442 if (select_multiple == impl->select_multiple)
3445 mode = select_multiple ? GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE;
3447 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
3448 gtk_tree_selection_set_mode (selection, mode);
3450 impl->select_multiple = select_multiple;
3451 g_object_notify (G_OBJECT (impl), "select-multiple");
3453 check_preview_change (impl);
3457 set_file_system_backend (GtkFileChooserDefault *impl,
3458 const char *backend)
3460 if (impl->file_system)
3462 g_signal_handler_disconnect (impl->file_system, impl->volumes_changed_id);
3463 impl->volumes_changed_id = 0;
3464 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
3465 impl->bookmarks_changed_id = 0;
3466 g_object_unref (impl->file_system);
3469 impl->file_system = NULL;
3471 impl->file_system = _gtk_file_system_create (backend);
3474 GtkSettings *settings = gtk_settings_get_default ();
3475 gchar *default_backend = NULL;
3477 g_object_get (settings, "gtk-file-chooser-backend", &default_backend, NULL);
3478 if (default_backend)
3480 impl->file_system = _gtk_file_system_create (default_backend);
3481 g_free (default_backend);
3485 if (!impl->file_system)
3487 #if defined (G_OS_UNIX)
3488 impl->file_system = gtk_file_system_unix_new ();
3489 #elif defined (G_OS_WIN32)
3490 impl->file_system = gtk_file_system_win32_new ();
3492 #error "No default filesystem implementation on the platform"
3496 if (impl->file_system)
3498 impl->volumes_changed_id = g_signal_connect (impl->file_system, "volumes-changed",
3499 G_CALLBACK (volumes_changed_cb),
3501 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
3502 G_CALLBACK (bookmarks_changed_cb),
3507 /* This function is basically a do_all function.
3509 * It sets the visibility on all the widgets based on the current state, and
3510 * moves the custom_widget if needed.
3513 update_appearance (GtkFileChooserDefault *impl)
3517 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
3518 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3522 gtk_widget_show (impl->save_widgets);
3524 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
3525 text = _("Save in _folder:");
3527 text = _("Create in _folder:");
3529 gtk_label_set_text_with_mnemonic (GTK_LABEL (impl->save_folder_label), text);
3531 if (gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
3533 gtk_widget_set_sensitive (impl->save_folder_label, FALSE);
3534 gtk_widget_set_sensitive (impl->save_folder_combo, FALSE);
3535 gtk_widget_show (impl->browse_widgets);
3539 gtk_widget_set_sensitive (impl->save_folder_label, TRUE);
3540 gtk_widget_set_sensitive (impl->save_folder_combo, TRUE);
3541 gtk_widget_hide (impl->browse_widgets);
3544 gtk_widget_show (impl->browse_new_folder_button);
3546 if (impl->select_multiple)
3548 g_warning ("Save mode cannot be set in conjunction with multiple selection mode. "
3549 "Re-setting to single selection mode.");
3550 set_select_multiple (impl, FALSE, TRUE);
3553 else if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3554 impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
3556 gtk_widget_hide (impl->save_widgets);
3557 gtk_widget_show (impl->browse_widgets);
3560 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
3561 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3563 if (impl->browse_files_model)
3564 _gtk_file_system_model_set_show_files (impl->browse_files_model, FALSE);
3568 if (impl->browse_files_model)
3569 _gtk_file_system_model_set_show_files (impl->browse_files_model, TRUE);
3572 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
3573 gtk_widget_hide (impl->browse_new_folder_button);
3575 gtk_widget_show (impl->browse_new_folder_button);
3577 if (impl->extra_widget)
3580 GtkWidget *unused_align;
3582 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
3583 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
3585 align = impl->save_extra_align;
3586 unused_align = impl->browse_extra_align;
3590 align = impl->browse_extra_align;
3591 unused_align = impl->save_extra_align;
3594 /* We own a ref on extra_widget, so it's safe to do this */
3595 child = GTK_BIN (unused_align)->child;
3597 gtk_container_remove (GTK_CONTAINER (unused_align), child);
3599 child = GTK_BIN (align)->child;
3600 if (child && child != impl->extra_widget)
3602 gtk_container_remove (GTK_CONTAINER (align), child);
3603 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
3605 else if (child == NULL)
3607 gtk_container_add (GTK_CONTAINER (align), impl->extra_widget);
3610 gtk_widget_show (align);
3611 gtk_widget_hide (unused_align);
3615 child = GTK_BIN (impl->browse_extra_align)->child;
3617 gtk_container_remove (GTK_CONTAINER (impl->browse_extra_align), child);
3619 child = GTK_BIN (impl->save_extra_align)->child;
3621 gtk_container_remove (GTK_CONTAINER (impl->save_extra_align), child);
3623 gtk_widget_hide (impl->save_extra_align);
3624 gtk_widget_hide (impl->browse_extra_align);
3627 g_signal_emit_by_name (impl, "default-size-changed");
3631 gtk_file_chooser_default_set_property (GObject *object,
3633 const GValue *value,
3637 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3641 case GTK_FILE_CHOOSER_PROP_ACTION:
3643 GtkFileChooserAction action = g_value_get_enum (value);
3645 if (action != impl->action)
3647 if (action == GTK_FILE_CHOOSER_ACTION_SAVE && impl->select_multiple)
3649 g_warning ("Multiple selection mode is not allowed in Save mode");
3650 set_select_multiple (impl, FALSE, TRUE);
3652 impl->action = action;
3653 update_appearance (impl);
3656 if (impl->save_file_name_entry)
3657 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
3661 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
3662 set_file_system_backend (impl, g_value_get_string (value));
3664 case GTK_FILE_CHOOSER_PROP_FILTER:
3665 set_current_filter (impl, g_value_get_object (value));
3667 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3668 set_local_only (impl, g_value_get_boolean (value));
3670 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3671 set_preview_widget (impl, g_value_get_object (value));
3673 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3674 impl->preview_widget_active = g_value_get_boolean (value);
3675 update_preview_widget_visibility (impl);
3677 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3678 impl->use_preview_label = g_value_get_boolean (value);
3679 update_preview_widget_visibility (impl);
3681 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3682 set_extra_widget (impl, g_value_get_object (value));
3683 update_appearance (impl);
3685 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3687 gboolean select_multiple = g_value_get_boolean (value);
3688 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE && select_multiple)
3690 g_warning ("Multiple selection mode is not allowed in Save mode");
3694 set_select_multiple (impl, select_multiple, FALSE);
3697 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3699 gboolean show_hidden = g_value_get_boolean (value);
3700 if (show_hidden != impl->show_hidden)
3702 impl->show_hidden = show_hidden;
3704 if (impl->browse_files_model)
3705 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, show_hidden);
3710 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3716 gtk_file_chooser_default_get_property (GObject *object,
3721 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (object);
3725 case GTK_FILE_CHOOSER_PROP_ACTION:
3726 g_value_set_enum (value, impl->action);
3728 case GTK_FILE_CHOOSER_PROP_FILTER:
3729 g_value_set_object (value, impl->current_filter);
3731 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
3732 g_value_set_boolean (value, impl->local_only);
3734 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
3735 g_value_set_object (value, impl->preview_widget);
3737 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
3738 g_value_set_boolean (value, impl->preview_widget_active);
3740 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
3741 g_value_set_boolean (value, impl->use_preview_label);
3743 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
3744 g_value_set_object (value, impl->extra_widget);
3746 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
3747 g_value_set_boolean (value, impl->select_multiple);
3749 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
3750 g_value_set_boolean (value, impl->show_hidden);
3753 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3758 /* Removes the settings signal handler. It's safe to call multiple times */
3760 remove_settings_signal (GtkFileChooserDefault *impl,
3763 if (impl->settings_signal_id)
3765 GtkSettings *settings;
3767 settings = gtk_settings_get_for_screen (screen);
3768 g_signal_handler_disconnect (settings,
3769 impl->settings_signal_id);
3770 impl->settings_signal_id = 0;
3775 gtk_file_chooser_default_dispose (GObject *object)
3777 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) object;
3779 if (impl->extra_widget)
3781 g_object_unref (impl->extra_widget);
3782 impl->extra_widget = NULL;
3785 remove_settings_signal (impl, gtk_widget_get_screen (GTK_WIDGET (impl)));
3787 G_OBJECT_CLASS (parent_class)->dispose (object);
3790 /* We override show-all since we have internal widgets that
3791 * shouldn't be shown when you call show_all(), like the filter
3795 gtk_file_chooser_default_show_all (GtkWidget *widget)
3797 GtkFileChooserDefault *impl = (GtkFileChooserDefault *) widget;
3799 gtk_widget_show (widget);
3801 if (impl->extra_widget)
3802 gtk_widget_show_all (impl->extra_widget);
3805 /* Handler for GtkWindow::set-focus; this is where we save the last-focused
3806 * widget on our toplevel. See gtk_file_chooser_default_hierarchy_changed()
3809 toplevel_set_focus_cb (GtkWindow *window,
3811 GtkFileChooserDefault *impl)
3813 impl->toplevel_last_focus_widget = gtk_window_get_focus (window);
3816 /* We monitor the focus widget on our toplevel to be able to know which widget
3817 * was last focused at the time our "should_respond" method gets called.
3820 gtk_file_chooser_default_hierarchy_changed (GtkWidget *widget,
3821 GtkWidget *previous_toplevel)
3823 GtkFileChooserDefault *impl;
3824 GtkWidget *toplevel;
3826 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3828 if (previous_toplevel)
3830 g_assert (impl->toplevel_set_focus_id != 0);
3831 g_signal_handler_disconnect (previous_toplevel, impl->toplevel_set_focus_id);
3832 impl->toplevel_set_focus_id = 0;
3833 impl->toplevel_last_focus_widget = NULL;
3836 g_assert (impl->toplevel_set_focus_id == 0);
3838 toplevel = gtk_widget_get_toplevel (widget);
3839 if (GTK_IS_WINDOW (toplevel))
3841 impl->toplevel_set_focus_id = g_signal_connect (toplevel, "set-focus",
3842 G_CALLBACK (toplevel_set_focus_cb), impl);
3843 impl->toplevel_last_focus_widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
3847 /* Changes the icons wherever it is needed */
3849 change_icon_theme (GtkFileChooserDefault *impl)
3851 GtkSettings *settings;
3854 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3856 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_SMALL_TOOLBAR, &width, &height))
3857 impl->icon_size = MAX (width, height);
3859 impl->icon_size = FALLBACK_ICON_SIZE;
3861 shortcuts_reload_icons (impl);
3862 gtk_widget_queue_resize (impl->browse_files_tree_view);
3865 /* Callback used when a GtkSettings value changes */
3867 settings_notify_cb (GObject *object,
3869 GtkFileChooserDefault *impl)
3873 name = g_param_spec_get_name (pspec);
3875 if (strcmp (name, "gtk-icon-theme-name") == 0
3876 || strcmp (name, "gtk-icon-sizes") == 0)
3877 change_icon_theme (impl);
3880 /* Installs a signal handler for GtkSettings so that we can monitor changes in
3884 check_icon_theme (GtkFileChooserDefault *impl)
3886 GtkSettings *settings;
3888 if (impl->settings_signal_id)
3891 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
3893 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (impl)));
3894 impl->settings_signal_id = g_signal_connect (settings, "notify",
3895 G_CALLBACK (settings_notify_cb), impl);
3897 change_icon_theme (impl);
3902 gtk_file_chooser_default_style_set (GtkWidget *widget,
3903 GtkStyle *previous_style)
3905 GtkFileChooserDefault *impl;
3907 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3909 if (GTK_WIDGET_CLASS (parent_class)->style_set)
3910 GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
3912 if (gtk_widget_has_screen (GTK_WIDGET (impl)))
3913 change_icon_theme (impl);
3915 g_signal_emit_by_name (widget, "default-size-changed");
3919 gtk_file_chooser_default_screen_changed (GtkWidget *widget,
3920 GdkScreen *previous_screen)
3922 GtkFileChooserDefault *impl;
3924 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
3926 if (GTK_WIDGET_CLASS (parent_class)->screen_changed)
3927 GTK_WIDGET_CLASS (parent_class)->screen_changed (widget, previous_screen);
3929 remove_settings_signal (impl, previous_screen);
3930 check_icon_theme (impl);
3932 g_signal_emit_by_name (widget, "default-size-changed");
3936 list_model_filter_func (GtkFileSystemModel *model,
3938 const GtkFileInfo *file_info,
3941 GtkFileChooserDefault *impl = user_data;
3942 GtkFileFilterInfo filter_info;
3943 GtkFileFilterFlags needed;
3946 if (!impl->current_filter)
3949 if (gtk_file_info_get_is_folder (file_info))
3952 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
3954 needed = gtk_file_filter_get_needed (impl->current_filter);
3956 filter_info.display_name = gtk_file_info_get_display_name (file_info);
3957 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
3959 if (needed & GTK_FILE_FILTER_FILENAME)
3961 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
3962 if (filter_info.filename)
3963 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
3966 filter_info.filename = NULL;
3968 if (needed & GTK_FILE_FILTER_URI)
3970 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
3971 if (filter_info.uri)
3972 filter_info.contains |= GTK_FILE_FILTER_URI;
3975 filter_info.uri = NULL;
3977 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
3979 if (filter_info.filename)
3980 g_free ((gchar *)filter_info.filename);
3981 if (filter_info.uri)
3982 g_free ((gchar *)filter_info.uri);
3988 install_list_model_filter (GtkFileChooserDefault *impl)
3990 g_assert (impl->browse_files_model != NULL);
3992 if (impl->current_filter)
3993 _gtk_file_system_model_set_filter (impl->browse_files_model,
3994 list_model_filter_func,
3998 #define COMPARE_DIRECTORIES \
3999 GtkFileChooserDefault *impl = user_data; \
4000 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->browse_files_model, a); \
4001 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->browse_files_model, b); \
4002 gboolean dir_a, dir_b; \
4005 dir_a = gtk_file_info_get_is_folder (info_a); \
4007 return impl->list_sort_ascending ? -1 : 1; \
4010 dir_b = gtk_file_info_get_is_folder (info_b); \
4012 return impl->list_sort_ascending ? 1 : -1; \
4014 if (dir_a != dir_b) \
4015 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
4017 /* Sort callback for the filename column */
4019 name_sort_func (GtkTreeModel *model,
4024 COMPARE_DIRECTORIES;
4026 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
4029 /* Sort callback for the size column */
4031 size_sort_func (GtkTreeModel *model,
4036 COMPARE_DIRECTORIES;
4039 gint64 size_a = gtk_file_info_get_size (info_a);
4040 gint64 size_b = gtk_file_info_get_size (info_b);
4042 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
4046 /* Sort callback for the mtime column */
4048 mtime_sort_func (GtkTreeModel *model,
4053 COMPARE_DIRECTORIES;
4056 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
4057 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
4059 return ta > tb ? -1 : (ta == tb ? 0 : 1);
4063 /* Callback used when the sort column changes. We cache the sort order for use
4064 * in name_sort_func().
4067 list_sort_column_changed_cb (GtkTreeSortable *sortable,
4068 GtkFileChooserDefault *impl)
4070 GtkSortType sort_type;
4072 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
4073 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
4077 set_busy_cursor (GtkFileChooserDefault *impl,
4080 GtkWindow *toplevel;
4081 GdkDisplay *display;
4084 toplevel = get_toplevel (GTK_WIDGET (impl));
4085 if (!toplevel || !GTK_WIDGET_REALIZED (toplevel))
4088 display = gtk_widget_get_display (GTK_WIDGET (toplevel));
4091 cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
4095 gdk_window_set_cursor (GTK_WIDGET (toplevel)->window, cursor);
4096 gdk_display_flush (display);
4099 gdk_cursor_unref (cursor);
4102 /* Callback used when the file system model finishes loading */
4104 browse_files_model_finished_loading_cb (GtkFileSystemModel *model,
4105 GtkFileChooserDefault *impl)
4107 set_busy_cursor (impl, FALSE);
4110 /* Gets rid of the old list model and creates a new one for the current folder */
4112 set_list_model (GtkFileChooserDefault *impl,
4115 if (impl->browse_files_model)
4117 g_object_unref (impl->browse_files_model);
4118 impl->browse_files_model = NULL;
4120 g_object_unref (impl->sort_model);
4121 impl->sort_model = NULL;
4124 set_busy_cursor (impl, TRUE);
4126 impl->browse_files_model = _gtk_file_system_model_new (impl->file_system,
4127 impl->current_folder, 0,
4130 if (!impl->browse_files_model)
4132 set_busy_cursor (impl, FALSE);
4133 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view), NULL);
4137 g_signal_connect (impl->browse_files_model, "finished-loading",
4138 G_CALLBACK (browse_files_model_finished_loading_cb), impl);
4140 _gtk_file_system_model_set_show_hidden (impl->browse_files_model, impl->show_hidden);
4141 switch (impl->action)
4143 case GTK_FILE_CHOOSER_ACTION_OPEN:
4144 case GTK_FILE_CHOOSER_ACTION_SAVE:
4145 _gtk_file_system_model_set_show_files (impl->browse_files_model, TRUE);
4147 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
4148 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
4149 _gtk_file_system_model_set_show_files (impl->browse_files_model, FALSE);
4152 g_assert_not_reached ();
4154 install_list_model_filter (impl);
4156 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->browse_files_model));
4157 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
4158 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
4159 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
4160 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
4161 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
4162 impl->list_sort_ascending = TRUE;
4164 g_signal_connect (impl->sort_model, "sort-column-changed",
4165 G_CALLBACK (list_sort_column_changed_cb), impl);
4167 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->browse_files_tree_view),
4168 GTK_TREE_MODEL (impl->sort_model));
4169 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->browse_files_tree_view));
4170 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->browse_files_tree_view),
4171 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
4177 update_chooser_entry (GtkFileChooserDefault *impl)
4179 GtkTreeSelection *selection;
4180 const GtkFileInfo *info;
4182 GtkTreeIter child_iter;
4184 if (impl->action != GTK_FILE_CHOOSER_ACTION_SAVE)
4187 g_assert (!impl->select_multiple);
4188 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4190 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
4193 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
4197 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
4199 if (!gtk_file_info_get_is_folder (info))
4200 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4201 gtk_file_info_get_display_name (info));
4205 gtk_file_chooser_default_set_current_folder (GtkFileChooser *chooser,
4206 const GtkFilePath *path,
4209 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4212 if (impl->local_only &&
4213 !gtk_file_system_path_is_local (impl->file_system, path))
4216 GTK_FILE_SYSTEM_ERROR,
4217 GTK_FILE_SYSTEM_ERROR_FAILED,
4218 _("Cannot change to folder because it is not local"));
4223 /* Test validity of path here. */
4224 if (!check_is_folder (impl->file_system, path, error))
4227 if (!_gtk_path_bar_set_path (GTK_PATH_BAR (impl->browse_path_bar), path, error))
4230 if (impl->current_folder != path)
4232 if (impl->current_folder)
4233 gtk_file_path_free (impl->current_folder);
4235 impl->current_folder = gtk_file_path_copy (path);
4238 /* Update the widgets that may trigger a folder change themselves. */
4240 if (!impl->changing_folder)
4242 impl->changing_folder = TRUE;
4244 shortcuts_update_current_folder (impl);
4246 impl->changing_folder = FALSE;
4249 /* Set the folder on the save entry */
4251 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry),
4252 impl->current_folder);
4254 /* Create a new list model. This is slightly evil; we store the result value
4255 * but perform more actions rather than returning immediately even if it
4256 * generates an error.
4258 result = set_list_model (impl, error);
4260 /* Refresh controls */
4262 shortcuts_find_current_folder (impl);
4264 g_signal_emit_by_name (impl, "current-folder-changed", 0);
4266 check_preview_change (impl);
4267 bookmarks_check_add_sensitivity (impl);
4269 g_signal_emit_by_name (impl, "selection-changed", 0);
4274 static GtkFilePath *
4275 gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
4277 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4279 return gtk_file_path_copy (impl->current_folder);
4283 gtk_file_chooser_default_set_current_name (GtkFileChooser *chooser,
4286 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4288 g_return_if_fail (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4289 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4291 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry), name);
4295 select_func (GtkFileSystemModel *model,
4300 GtkFileChooserDefault *impl = user_data;
4301 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4302 GtkTreePath *sorted_path;
4304 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
4305 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
4306 gtk_tree_view_scroll_to_cell (tree_view, sorted_path, NULL, FALSE, 0.0, 0.0);
4307 gtk_tree_path_free (sorted_path);
4311 gtk_file_chooser_default_select_path (GtkFileChooser *chooser,
4312 const GtkFilePath *path,
4315 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4316 GtkFilePath *parent_path;
4318 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, error))
4322 return _gtk_file_chooser_set_current_folder_path (chooser, path, error);
4326 GtkFileFolder *folder;
4330 result = _gtk_file_chooser_set_current_folder_path (chooser, parent_path, error);
4334 gtk_file_path_free (parent_path);
4338 folder = gtk_file_system_get_folder (impl->file_system, parent_path, GTK_FILE_INFO_IS_HIDDEN, error);
4339 gtk_file_path_free (parent_path);
4344 info = gtk_file_folder_get_info (folder, path, error);
4345 g_object_unref (folder);
4350 is_hidden = gtk_file_info_get_is_hidden (info);
4351 gtk_file_info_free (info);
4354 g_object_set (impl, "show-hidden", TRUE, NULL);
4356 result = _gtk_file_system_model_path_do (impl->browse_files_model, path,
4360 GTK_FILE_CHOOSER_ERROR,
4361 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
4362 _("Could not find the path"));
4367 g_assert_not_reached ();
4371 unselect_func (GtkFileSystemModel *model,
4376 GtkFileChooserDefault *impl = user_data;
4377 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->browse_files_tree_view);
4378 GtkTreePath *sorted_path;
4380 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
4382 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
4384 gtk_tree_path_free (sorted_path);
4388 gtk_file_chooser_default_unselect_path (GtkFileChooser *chooser,
4389 const GtkFilePath *path)
4391 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4393 if (!impl->browse_files_model)
4396 _gtk_file_system_model_path_do (impl->browse_files_model, path,
4397 unselect_func, impl);
4401 maybe_select (GtkTreeModel *model,
4406 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (data);
4407 GtkTreeSelection *selection;
4408 const GtkFileInfo *info;
4411 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4413 info = get_list_file_info (impl, iter);
4414 is_folder = gtk_file_info_get_is_folder (info);
4416 if ((is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) ||
4417 (!is_folder && impl->action == GTK_FILE_CHOOSER_ACTION_OPEN))
4418 gtk_tree_selection_select_iter (selection, iter);
4420 gtk_tree_selection_unselect_iter (selection, iter);
4426 gtk_file_chooser_default_select_all (GtkFileChooser *chooser)
4428 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4429 if (impl->select_multiple)
4430 gtk_tree_model_foreach (impl->sort_model, maybe_select, impl);
4434 gtk_file_chooser_default_unselect_all (GtkFileChooser *chooser)
4436 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4437 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4439 gtk_tree_selection_unselect_all (selection);
4442 /* Checks whether the filename entry for the Save modes contains a valid filename */
4443 static GtkFilePath *
4444 check_save_entry (GtkFileChooserDefault *impl,
4448 GtkFileChooserEntry *chooser_entry;
4449 const GtkFilePath *current_folder;
4450 const char *file_part;
4454 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4455 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4457 chooser_entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
4459 current_folder = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
4460 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
4462 if (!file_part || file_part[0] == '\0')
4472 path = gtk_file_system_make_path (impl->file_system, current_folder, file_part, &error);
4476 error_building_filename_dialog (impl, current_folder, file_part, error);
4485 struct get_paths_closure {
4486 GtkFileChooserDefault *impl;
4488 GtkFilePath *path_from_entry;
4492 get_paths_foreach (GtkTreeModel *model,
4497 struct get_paths_closure *info;
4498 const GtkFilePath *file_path;
4499 GtkFileSystemModel *fs_model;
4500 GtkTreeIter sel_iter;
4503 fs_model = info->impl->browse_files_model;
4504 gtk_tree_model_sort_convert_iter_to_child_iter (info->impl->sort_model, &sel_iter, iter);
4506 file_path = _gtk_file_system_model_get_path (fs_model, &sel_iter);
4508 return; /* We are on the editable row */
4510 if (!info->path_from_entry
4511 || gtk_file_path_compare (info->path_from_entry, file_path) != 0)
4512 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
4516 gtk_file_chooser_default_get_paths (GtkFileChooser *chooser)
4518 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4519 struct get_paths_closure info;
4523 info.path_from_entry = NULL;
4525 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4526 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4528 gboolean is_valid, is_empty;
4530 info.path_from_entry = check_save_entry (impl, &is_valid, &is_empty);
4531 if (!is_valid && !is_empty)
4535 if (!info.path_from_entry || impl->select_multiple)
4537 GtkTreeSelection *selection;
4539 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4540 gtk_tree_selection_selected_foreach (selection, get_paths_foreach, &info);
4543 if (info.path_from_entry)
4544 info.result = g_slist_prepend (info.result, info.path_from_entry);
4546 /* If there's no folder selected, and we're in SELECT_FOLDER mode, then we
4547 * fall back to the current directory */
4548 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
4549 info.result == NULL)
4551 info.result = g_slist_prepend (info.result, gtk_file_path_copy (impl->current_folder));
4554 return g_slist_reverse (info.result);
4557 static GtkFilePath *
4558 gtk_file_chooser_default_get_preview_path (GtkFileChooser *chooser)
4560 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4562 if (impl->preview_path)
4563 return gtk_file_path_copy (impl->preview_path);
4568 static GtkFileSystem *
4569 gtk_file_chooser_default_get_file_system (GtkFileChooser *chooser)
4571 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4573 return impl->file_system;
4576 /* Shows or hides the filter widgets */
4578 show_filters (GtkFileChooserDefault *impl,
4582 gtk_widget_show (impl->filter_combo_hbox);
4584 gtk_widget_hide (impl->filter_combo_hbox);
4588 gtk_file_chooser_default_add_filter (GtkFileChooser *chooser,
4589 GtkFileFilter *filter)
4591 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4594 if (g_slist_find (impl->filters, filter))
4596 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
4600 g_object_ref (filter);
4601 gtk_object_sink (GTK_OBJECT (filter));
4602 impl->filters = g_slist_append (impl->filters, filter);
4604 name = gtk_file_filter_get_name (filter);
4606 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
4608 gtk_combo_box_append_text (GTK_COMBO_BOX (impl->filter_combo), name);
4610 if (!g_slist_find (impl->filters, impl->current_filter))
4611 set_current_filter (impl, filter);
4613 show_filters (impl, TRUE);
4617 gtk_file_chooser_default_remove_filter (GtkFileChooser *chooser,
4618 GtkFileFilter *filter)
4620 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4621 GtkTreeModel *model;
4625 filter_index = g_slist_index (impl->filters, filter);
4627 if (filter_index < 0)
4629 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
4633 impl->filters = g_slist_remove (impl->filters, filter);
4635 if (filter == impl->current_filter)
4638 set_current_filter (impl, impl->filters->data);
4640 set_current_filter (impl, NULL);
4643 /* Remove row from the combo box */
4644 model = gtk_combo_box_get_model (GTK_COMBO_BOX (impl->filter_combo));
4645 gtk_tree_model_iter_nth_child (model, &iter, NULL, filter_index);
4646 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
4648 g_object_unref (filter);
4651 show_filters (impl, FALSE);
4655 gtk_file_chooser_default_list_filters (GtkFileChooser *chooser)
4657 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4659 return g_slist_copy (impl->filters);
4662 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
4664 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserDefault *impl,
4667 return pos + shortcuts_get_index (impl, SHORTCUTS_SHORTCUTS);
4671 gtk_file_chooser_default_add_shortcut_folder (GtkFileChooser *chooser,
4672 const GtkFilePath *path,
4675 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4679 /* Test validity of path here. */
4680 if (!check_is_folder (impl->file_system, path, error))
4683 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
4685 result = shortcuts_insert_path (impl, pos, FALSE, NULL, path, NULL, FALSE, error);
4688 impl->num_shortcuts++;
4690 if (impl->shortcuts_filter_model)
4691 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model));
4697 gtk_file_chooser_default_remove_shortcut_folder (GtkFileChooser *chooser,
4698 const GtkFilePath *path,
4701 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4707 if (impl->num_shortcuts == 0)
4710 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
4711 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
4712 g_assert_not_reached ();
4714 for (i = 0; i < impl->num_shortcuts; i++)
4718 GtkFilePath *shortcut;
4720 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
4721 SHORTCUTS_COL_DATA, &col_data,
4722 SHORTCUTS_COL_IS_VOLUME, &is_volume,
4724 g_assert (col_data != NULL);
4725 g_assert (!is_volume);
4727 shortcut = col_data;
4728 if (gtk_file_path_compare (shortcut, path) == 0)
4730 shortcuts_remove_rows (impl, pos + i, 1);
4731 impl->num_shortcuts--;
4735 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
4736 g_assert_not_reached ();
4741 uri = gtk_file_system_path_to_uri (impl->file_system, path);
4743 GTK_FILE_CHOOSER_ERROR,
4744 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
4745 _("shortcut %s does not exist"),
4753 gtk_file_chooser_default_list_shortcut_folders (GtkFileChooser *chooser)
4755 GtkFileChooserDefault *impl = GTK_FILE_CHOOSER_DEFAULT (chooser);
4761 if (impl->num_shortcuts == 0)
4764 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
4765 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
4766 g_assert_not_reached ();
4770 for (i = 0; i < impl->num_shortcuts; i++)
4774 GtkFilePath *shortcut;
4776 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter,
4777 SHORTCUTS_COL_DATA, &col_data,
4778 SHORTCUTS_COL_IS_VOLUME, &is_volume,
4780 g_assert (col_data != NULL);
4781 g_assert (!is_volume);
4783 shortcut = col_data;
4784 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
4786 if (i != impl->num_shortcuts - 1)
4788 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
4789 g_assert_not_reached ();
4793 return g_slist_reverse (list);
4796 /* Guesses a size based upon font sizes */
4798 find_good_size_from_style (GtkWidget *widget,
4802 GtkFileChooserDefault *impl;
4803 gint default_width, default_height;
4806 GtkRequisition preview_req;
4808 g_assert (widget->style != NULL);
4809 impl = GTK_FILE_CHOOSER_DEFAULT (widget);
4811 font_size = pango_font_description_get_size (widget->style->font_desc);
4812 font_size = PANGO_PIXELS (font_size);
4814 default_width = font_size * NUM_CHARS;
4815 default_height = font_size * NUM_LINES;
4817 /* Use at least the requisition size not including the preview widget */
4818 gtk_widget_size_request (widget, &req);
4820 if (impl->preview_widget_active && impl->preview_widget)
4821 gtk_widget_size_request (impl->preview_box, &preview_req);
4823 preview_req.width = 0;
4825 default_width = MAX (default_width, (req.width - (preview_req.width + PREVIEW_HBOX_SPACING)));
4826 default_height = MAX (default_height, req.height);
4828 *width = default_width;
4829 *height = default_height;
4833 gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
4834 gint *default_width,
4835 gint *default_height)
4837 GtkFileChooserDefault *impl;
4839 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4841 find_good_size_from_style (GTK_WIDGET (chooser_embed), default_width, default_height);
4843 if (impl->preview_widget_active && impl->preview_widget)
4844 *default_width += impl->preview_box->requisition.width + PREVIEW_HBOX_SPACING;
4848 gtk_file_chooser_default_get_resizable_hints (GtkFileChooserEmbed *chooser_embed,
4849 gboolean *resize_horizontally,
4850 gboolean *resize_vertically)
4852 GtkFileChooserDefault *impl;
4854 g_return_if_fail (resize_horizontally != NULL);
4855 g_return_if_fail (resize_vertically != NULL);
4857 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4859 *resize_horizontally = TRUE;
4860 *resize_vertically = TRUE;
4862 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE ||
4863 impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4865 if (! gtk_expander_get_expanded (GTK_EXPANDER (impl->save_expander)))
4867 *resize_horizontally = FALSE;
4868 *resize_vertically = FALSE;
4873 struct switch_folder_closure {
4874 GtkFileChooserDefault *impl;
4875 const GtkFilePath *path;
4879 /* Used from gtk_tree_selection_selected_foreach() in switch_to_selected_folder() */
4881 switch_folder_foreach_cb (GtkTreeModel *model,
4886 struct switch_folder_closure *closure;
4887 GtkTreeIter child_iter;
4891 gtk_tree_model_sort_convert_iter_to_child_iter (closure->impl->sort_model, &child_iter, iter);
4893 closure->path = _gtk_file_system_model_get_path (closure->impl->browse_files_model, &child_iter);
4894 closure->num_selected++;
4897 /* Changes to the selected folder in the list view */
4899 switch_to_selected_folder (GtkFileChooserDefault *impl)
4901 GtkTreeSelection *selection;
4902 struct switch_folder_closure closure;
4904 /* We do this with foreach() rather than get_selected() as we may be in
4905 * multiple selection mode
4908 closure.impl = impl;
4909 closure.path = NULL;
4910 closure.num_selected = 0;
4912 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
4913 gtk_tree_selection_selected_foreach (selection, switch_folder_foreach_cb, &closure);
4915 g_assert (closure.path && closure.num_selected == 1);
4917 change_folder_and_display_error (impl, closure.path);
4920 /* Implementation for GtkFileChooserEmbed::should_respond() */
4922 gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
4924 GtkFileChooserDefault *impl;
4925 GtkWidget *toplevel;
4926 GtkWidget *current_focus;
4928 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
4930 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (impl));
4931 g_assert (GTK_IS_WINDOW (toplevel));
4933 current_focus = gtk_window_get_focus (GTK_WINDOW (toplevel));
4935 if (current_focus == NULL)
4937 else if (current_focus == impl->browse_files_tree_view)
4940 gboolean all_files, all_folders;
4944 selection_check (impl, &num_selected, &all_files, &all_folders);
4946 if (impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && num_selected != 1)
4947 return TRUE; /* zero means current folder; more than one means use the whole selection */
4949 if (num_selected == 0)
4951 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4952 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
4953 goto save_entry; /* it makes sense to use the typed name */
4958 if (num_selected == 1 && all_folders)
4960 switch_to_selected_folder (impl);
4966 else if (current_focus == impl->save_file_name_entry)
4969 gboolean is_valid, is_empty;
4972 GtkFileChooserEntry *entry;
4976 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
4977 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
4979 entry = GTK_FILE_CHOOSER_ENTRY (impl->save_file_name_entry);
4980 path = check_save_entry (impl, &is_valid, &is_empty);
4982 if (!is_empty && !is_valid)
4986 path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
4988 is_folder = check_is_folder (impl->file_system, path, NULL);
4991 _gtk_file_chooser_entry_set_file_part (entry, "");
4992 change_folder_and_display_error (impl, path);
4998 gtk_file_path_free (path);
5001 else if (impl->toplevel_last_focus_widget == impl->browse_shortcuts_tree_view)
5003 /* The focus is on a dialog's action area button, *and* the widget that
5004 * was focused immediately before it is the shortcuts list. Switch to the
5005 * selected shortcut and tell the caller not to respond.
5009 if (shortcuts_get_selected (impl, &iter))
5011 shortcuts_activate_iter (impl, &iter);
5013 gtk_widget_grab_focus (impl->browse_files_tree_view);
5020 else if (impl->toplevel_last_focus_widget == impl->browse_files_tree_view)
5022 /* The focus is on a dialog's action area button, *and* the widget that
5023 * was focused immediately before it is the file list.
5028 /* The focus is on a dialog's action area button or something else */
5029 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5030 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5035 g_assert_not_reached ();
5039 /* Implementation for GtkFileChooserEmbed::initial_focus() */
5041 gtk_file_chooser_default_initial_focus (GtkFileChooserEmbed *chooser_embed)
5043 GtkFileChooserDefault *impl;
5046 impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
5048 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5049 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5053 path = gtk_tree_path_new_from_indices (0, -1);
5054 gtk_tree_view_set_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), path, NULL, FALSE);
5055 gtk_tree_path_free (path);
5057 widget = impl->browse_files_tree_view;
5059 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5060 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5061 widget = impl->save_file_name_entry;
5064 g_assert_not_reached ();
5068 gtk_widget_grab_focus (widget);
5072 set_current_filter (GtkFileChooserDefault *impl,
5073 GtkFileFilter *filter)
5075 if (impl->current_filter != filter)
5079 /* If we have filters, new filter must be one of them
5081 filter_index = g_slist_index (impl->filters, filter);
5082 if (impl->filters && filter_index < 0)
5085 if (impl->current_filter)
5086 g_object_unref (impl->current_filter);
5087 impl->current_filter = filter;
5088 if (impl->current_filter)
5090 g_object_ref (impl->current_filter);
5091 gtk_object_sink (GTK_OBJECT (filter));
5095 gtk_combo_box_set_active (GTK_COMBO_BOX (impl->filter_combo),
5098 if (impl->browse_files_model)
5099 install_list_model_filter (impl);
5101 g_object_notify (G_OBJECT (impl), "filter");
5106 filter_combo_changed (GtkComboBox *combo_box,
5107 GtkFileChooserDefault *impl)
5109 gint new_index = gtk_combo_box_get_active (combo_box);
5110 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
5112 set_current_filter (impl, new_filter);
5116 check_preview_change (GtkFileChooserDefault *impl)
5118 GtkTreePath *cursor_path;
5119 const GtkFilePath *new_path;
5120 const GtkFileInfo *new_info;
5122 gtk_tree_view_get_cursor (GTK_TREE_VIEW (impl->browse_files_tree_view), &cursor_path, NULL);
5123 if (cursor_path && impl->sort_model)
5126 GtkTreeIter child_iter;
5128 gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, cursor_path);
5129 gtk_tree_path_free (cursor_path);
5131 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5133 new_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5134 new_info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5142 if (new_path != impl->preview_path &&
5143 !(new_path && impl->preview_path &&
5144 gtk_file_path_compare (new_path, impl->preview_path) == 0))
5146 if (impl->preview_path)
5148 gtk_file_path_free (impl->preview_path);
5149 g_free (impl->preview_display_name);
5154 impl->preview_path = gtk_file_path_copy (new_path);
5155 impl->preview_display_name = g_strdup (gtk_file_info_get_display_name (new_info));
5159 impl->preview_path = NULL;
5160 impl->preview_display_name = NULL;
5163 if (impl->use_preview_label && impl->preview_label)
5164 gtk_label_set_text (GTK_LABEL (impl->preview_label), impl->preview_display_name);
5166 g_signal_emit_by_name (impl, "update-preview");
5170 /* Activates a volume by mounting it if necessary and then switching to its
5174 shortcuts_activate_volume (GtkFileChooserDefault *impl,
5175 GtkFileSystemVolume *volume)
5179 /* We ref the file chooser since volume_mount() may run a main loop, and the
5180 * user could close the file chooser window in the meantime.
5182 g_object_ref (impl);
5184 if (!gtk_file_system_volume_get_is_mounted (impl->file_system, volume))
5189 set_busy_cursor (impl, TRUE);
5192 result = gtk_file_system_volume_mount (impl->file_system, volume, &error);
5198 msg = g_strdup_printf ("Could not mount %s:\n%s",
5199 gtk_file_system_volume_get_display_name (impl->file_system, volume),
5201 error_message (impl, msg);
5203 g_error_free (error);
5206 set_busy_cursor (impl, FALSE);
5212 path = gtk_file_system_volume_get_base_path (impl->file_system, volume);
5213 change_folder_and_display_error (impl, path);
5214 gtk_file_path_free (path);
5218 g_object_unref (impl);
5221 /* Opens the folder or volume at the specified iter in the shortcuts model */
5223 shortcuts_activate_iter (GtkFileChooserDefault *impl,
5229 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), iter,
5230 SHORTCUTS_COL_DATA, &col_data,
5231 SHORTCUTS_COL_IS_VOLUME, &is_volume,
5235 return; /* We are on a separator */
5239 GtkFileSystemVolume *volume;
5243 shortcuts_activate_volume (impl, volume);
5247 const GtkFilePath *file_path;
5249 file_path = col_data;
5250 change_folder_and_display_error (impl, file_path);
5254 /* Callback used when a row in the shortcuts list is activated */
5256 shortcuts_row_activated_cb (GtkTreeView *tree_view,
5258 GtkTreeViewColumn *column,
5259 GtkFileChooserDefault *impl)
5262 GtkTreeIter child_iter;
5264 if (!gtk_tree_model_get_iter (impl->shortcuts_filter_model, &iter, path))
5267 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (impl->shortcuts_filter_model),
5270 shortcuts_activate_iter (impl, &child_iter);
5272 gtk_widget_grab_focus (impl->browse_files_tree_view);
5275 /* Handler for GtkWidget::key-press-event on the shortcuts list */
5277 shortcuts_key_press_event_cb (GtkWidget *widget,
5279 GtkFileChooserDefault *impl)
5283 modifiers = gtk_accelerator_get_default_mod_mask ();
5285 if ((event->keyval == GDK_BackSpace
5286 || event->keyval == GDK_Delete
5287 || event->keyval == GDK_KP_Delete)
5288 && (event->state & modifiers) == 0)
5290 remove_selected_bookmarks (impl);
5298 shortcuts_select_func (GtkTreeSelection *selection,
5299 GtkTreeModel *model,
5301 gboolean path_currently_selected,
5304 GtkFileChooserDefault *impl = data;
5306 return (*gtk_tree_path_get_indices (path) != shortcuts_get_index (impl, SHORTCUTS_BOOKMARKS_SEPARATOR));
5310 list_selection_changed (GtkTreeSelection *selection,
5311 GtkFileChooserDefault *impl)
5313 /* See if we are in the new folder editable row for Save mode */
5314 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE)
5316 GtkTreeSelection *selection;
5317 GtkTreeIter iter, child_iter;
5318 const GtkFileInfo *info;
5320 g_assert (!impl->select_multiple);
5321 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->browse_files_tree_view));
5322 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
5325 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5329 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5331 return; /* We are on the editable row for New Folder */
5334 update_chooser_entry (impl);
5335 check_preview_change (impl);
5336 bookmarks_check_add_sensitivity (impl);
5338 g_signal_emit_by_name (impl, "selection-changed", 0);
5341 /* Callback used when a row in the file list is activated */
5343 list_row_activated (GtkTreeView *tree_view,
5345 GtkTreeViewColumn *column,
5346 GtkFileChooserDefault *impl)
5348 GtkTreeIter iter, child_iter;
5349 const GtkFileInfo *info;
5351 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
5354 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
5356 info = _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5358 if (gtk_file_info_get_is_folder (info))
5360 const GtkFilePath *file_path;
5362 file_path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5363 change_folder_and_display_error (impl, file_path);
5368 g_signal_emit_by_name (impl, "file-activated");
5372 path_bar_clicked (GtkPathBar *path_bar,
5373 GtkFilePath *file_path,
5374 gboolean child_is_hidden,
5375 GtkFileChooserDefault *impl)
5377 if (!change_folder_and_display_error (impl, file_path))
5380 /* Say we have "/foo/bar/[.baz]" and the user clicks on "bar". We should then
5381 * show hidden files so that ".baz" appears in the file list, as it will still
5382 * be shown in the path bar: "/foo/[bar]/.baz"
5384 if (child_is_hidden)
5385 g_object_set (impl, "show-hidden", TRUE, NULL);
5388 static const GtkFileInfo *
5389 get_list_file_info (GtkFileChooserDefault *impl,
5392 GtkTreeIter child_iter;
5394 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5398 return _gtk_file_system_model_get_info (impl->browse_files_model, &child_iter);
5402 list_icon_data_func (GtkTreeViewColumn *tree_column,
5403 GtkCellRenderer *cell,
5404 GtkTreeModel *tree_model,
5408 GtkFileChooserDefault *impl = data;
5409 GtkTreeIter child_iter;
5410 const GtkFilePath *path;
5413 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
5416 path = _gtk_file_system_model_get_path (impl->browse_files_model, &child_iter);
5420 /* FIXME: NULL GError */
5421 pixbuf = gtk_file_system_render_icon (impl->file_system, path, GTK_WIDGET (impl),
5422 impl->icon_size, NULL);
5428 g_object_unref (pixbuf);
5432 list_name_data_func (GtkTreeViewColumn *tree_column,
5433 GtkCellRenderer *cell,
5434 GtkTreeModel *tree_model,
5438 GtkFileChooserDefault *impl = data;
5439 const GtkFileInfo *info = get_list_file_info (impl, iter);
5444 "text", _("Type name of new folder"),
5450 "text", gtk_file_info_get_display_name (info),
5456 list_size_data_func (GtkTreeViewColumn *tree_column,
5457 GtkCellRenderer *cell,
5458 GtkTreeModel *tree_model,
5462 GtkFileChooserDefault *impl = data;
5463 const GtkFileInfo *info = get_list_file_info (impl, iter);
5467 if (!info || gtk_file_info_get_is_folder (info))
5470 size = gtk_file_info_get_size (info);
5472 if (size < (gint64)1024)
5473 str = g_strdup_printf (ngettext ("%d byte", "%d bytes", (gint)size), (gint)size);
5474 else if (size < (gint64)1024*1024)
5475 str = g_strdup_printf (_("%.1f K"), size / (1024.));
5476 else if (size < (gint64)1024*1024*1024)
5477 str = g_strdup_printf (_("%.1f M"), size / (1024.*1024.));
5479 str = g_strdup_printf (_("%.1f G"), size / (1024.*1024.*1024.));
5489 /* Tree column data callback for the file list; fetches the mtime of a file */
5491 list_mtime_data_func (GtkTreeViewColumn *tree_column,
5492 GtkCellRenderer *cell,
5493 GtkTreeModel *tree_model,
5497 GtkFileChooserDefault *impl;
5498 const GtkFileInfo *info;
5499 GtkFileTime time_mtime, time_now;
5506 info = get_list_file_info (impl, iter);
5515 time_mtime = gtk_file_info_get_modification_time (info);
5516 g_date_set_time (&mtime, (GTime) time_mtime);
5518 time_now = (GTime ) time (NULL);
5519 g_date_set_time (&now, (GTime) time_now);
5521 days_diff = g_date_get_julian (&now) - g_date_get_julian (&mtime);
5524 strcpy (buf, _("Today"));
5525 else if (days_diff == 1)
5526 strcpy (buf, _("Yesterday"));
5531 if (days_diff > 1 && days_diff < 7)
5532 format = "%A"; /* Days from last week */
5534 format = "%x"; /* Any other date */
5536 if (g_date_strftime (buf, sizeof (buf), format, &mtime) == 0)
5537 strcpy (buf, _("Unknown"));
5546 _gtk_file_chooser_default_new (const char *file_system)
5548 return g_object_new (GTK_TYPE_FILE_CHOOSER_DEFAULT,
5549 "file-system-backend", file_system,
5554 location_entry_create (GtkFileChooserDefault *impl)
5558 entry = _gtk_file_chooser_entry_new (TRUE);
5559 /* Pick a good width for the entry */
5560 gtk_entry_set_width_chars (GTK_ENTRY (entry), 30);
5561 gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
5562 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (entry), impl->file_system);
5563 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (entry), impl->current_folder);
5564 _gtk_file_chooser_entry_set_action (GTK_FILE_CHOOSER_ENTRY (entry), impl->action);
5565 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5566 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5567 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry), "");
5568 else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5569 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5570 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (entry),
5571 gtk_entry_get_text (GTK_ENTRY (impl->save_file_name_entry)));
5573 g_assert_not_reached ();
5575 return GTK_WIDGET (entry);
5579 update_from_entry (GtkFileChooserDefault *impl,
5581 GtkFileChooserEntry *chooser_entry)
5583 const GtkFilePath *folder_path;
5584 const char *file_part;
5586 folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
5587 file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
5589 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN && !folder_path)
5591 error_message_with_parent (parent,
5592 _("Cannot change to the folder you specified as it is an invalid path."));
5596 if (file_part[0] == '\0')
5597 return change_folder_and_display_error (impl, folder_path);
5600 GtkFileFolder *folder = NULL;
5601 GtkFilePath *subfolder_path = NULL;
5602 GtkFileInfo *info = NULL;
5608 /* If the file part is non-empty, we need to figure out if it refers to a
5609 * folder within folder. We could optimize the case here where the folder
5610 * is already loaded for one of our tree models.
5614 folder = gtk_file_system_get_folder (impl->file_system, folder_path, GTK_FILE_INFO_IS_FOLDER, &error);
5618 error_getting_info_dialog (impl, folder_path, error);
5623 subfolder_path = gtk_file_system_make_path (impl->file_system, folder_path, file_part, &error);
5625 if (!subfolder_path)
5630 uri = gtk_file_system_path_to_uri (impl->file_system, folder_path);
5631 msg = g_strdup_printf (_("Could not build file name from '%s' and '%s':\n%s"),
5634 error_message (impl, msg);
5641 info = gtk_file_folder_get_info (folder, subfolder_path, &error);
5645 if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5646 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
5648 if (!change_folder_and_display_error (impl, folder_path))
5651 gtk_file_chooser_default_set_current_name (GTK_FILE_CHOOSER (impl), file_part);
5654 error_getting_info_dialog (impl, subfolder_path, error);
5659 if (gtk_file_info_get_is_folder (info))
5660 result = change_folder_and_display_error (impl, subfolder_path);
5666 result = _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (impl), subfolder_path, &error);
5669 _("Could not select %s:\n%s"),
5670 subfolder_path, error);
5676 g_object_unref (folder);
5678 gtk_file_path_free (subfolder_path);
5681 gtk_file_info_free (info);
5686 g_assert_not_reached ();
5690 location_popup_handler (GtkFileChooserDefault *impl)
5693 GtkWindow *toplevel;
5699 const char *accept_stock;
5703 toplevel = get_toplevel (GTK_WIDGET (impl));
5705 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5706 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5708 title = _("Open Location");
5709 accept_stock = GTK_STOCK_OPEN;
5713 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5714 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5715 title = _("Save in Location");
5716 accept_stock = GTK_STOCK_SAVE;
5719 dialog = gtk_dialog_new_with_buttons (title,
5721 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
5722 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
5723 accept_stock, GTK_RESPONSE_ACCEPT,
5725 gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
5726 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
5727 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
5728 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
5730 hbox = gtk_hbox_new (FALSE, 12);
5731 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox, FALSE, FALSE, 0);
5732 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
5734 label = gtk_label_new_with_mnemonic (_("_Location:"));
5735 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
5737 entry = location_entry_create (impl);
5738 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
5739 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
5743 gtk_widget_show_all (dialog);
5747 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
5749 if (update_from_entry (impl, GTK_WINDOW (dialog), GTK_FILE_CHOOSER_ENTRY (entry)))
5751 if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN
5752 || impl->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
5754 gtk_widget_grab_focus (impl->browse_files_tree_view);
5758 g_assert (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
5759 || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER);
5760 gtk_widget_grab_focus (impl->save_file_name_entry);
5768 GtkWindow *toplevel;
5770 toplevel = get_toplevel (GTK_WIDGET (impl));
5771 if (toplevel && toplevel->focus_widget)
5772 gtk_widget_grab_focus (toplevel->focus_widget);
5775 gtk_widget_destroy (dialog);
5778 /* Handler for the "up-folder" keybinding signal */
5780 up_folder_handler (GtkFileChooserDefault *impl)
5782 _gtk_path_bar_up (GTK_PATH_BAR (impl->browse_path_bar));
5785 /* Handler for the "down-folder" keybinding signal */
5787 down_folder_handler (GtkFileChooserDefault *impl)
5789 _gtk_path_bar_down (GTK_PATH_BAR (impl->browse_path_bar));
5792 /* Handler for the "home-folder" keybinding signal */
5794 home_folder_handler (GtkFileChooserDefault *impl)
5799 if (!impl->has_home)
5800 return; /* Should we put up an error dialog? */
5802 pos = shortcuts_get_index (impl, SHORTCUTS_HOME);
5803 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
5804 g_assert_not_reached ();
5806 shortcuts_activate_iter (impl, &iter);
5811 /* Drag and drop interfaces */
5814 _shortcuts_model_filter_class_init (ShortcutsModelFilterClass *class)
5819 _shortcuts_model_filter_init (ShortcutsModelFilter *model)
5824 /* GtkTreeDragSource::row_draggable implementation for the shortcuts filter model */
5826 shortcuts_model_filter_row_draggable (GtkTreeDragSource *drag_source,
5829 ShortcutsModelFilter *model;
5833 model = SHORTCUTS_MODEL_FILTER (drag_source);
5835 pos = *gtk_tree_path_get_indices (path);
5836 bookmarks_pos = shortcuts_get_index (model->impl, SHORTCUTS_BOOKMARKS);
5838 return (pos >= bookmarks_pos && pos < bookmarks_pos + model->impl->num_bookmarks);
5841 /* GtkTreeDragSource::drag_data_get implementation for the shortcuts filter model */
5843 shortcuts_model_filter_drag_data_get (GtkTreeDragSource *drag_source,
5845 GtkSelectionData *selection_data)
5847 ShortcutsModelFilter *model;
5849 model = SHORTCUTS_MODEL_FILTER (drag_source);
5856 /* Fill the GtkTreeDragSourceIface vtable */
5858 shortcuts_model_filter_drag_source_iface_init (GtkTreeDragSourceIface *iface)
5860 iface->row_draggable = shortcuts_model_filter_row_draggable;
5861 iface->drag_data_get = shortcuts_model_filter_drag_data_get;
5865 /* Fill the GtkTreeDragDestIface vtable */
5867 shortcuts_model_filter_drag_dest_iface_init (GtkTreeDragDestIface *iface)
5869 iface->drag_data_received = shortcuts_model_filter_drag_data_received;
5870 iface->row_drop_possible = shortcuts_model_filter_row_drop_possible;
5874 static GtkTreeModel *
5875 shortcuts_model_filter_new (GtkFileChooserDefault *impl,
5876 GtkTreeModel *child_model,
5879 ShortcutsModelFilter *model;
5881 model = g_object_new (SHORTCUTS_MODEL_FILTER_TYPE,
5882 "child_model", child_model,
5883 "virtual_root", root,
5888 return GTK_TREE_MODEL (model);