1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserimpldefault.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.
21 #include "gtkcellrendererseptext.h"
22 #include "gtkfilechooserimpldefault.h"
23 #include "gtkfilechooserentry.h"
24 #include "gtkfilechooserenums.h"
25 #include "gtkfilechooserutils.h"
26 #include "gtkfilechooser.h"
27 #include "gtkfilesystemmodel.h"
29 #include <gtk/gtkalignment.h>
30 #include <gtk/gtkcellrendererpixbuf.h>
31 #include <gtk/gtkcellrenderertext.h>
32 #include <gtk/gtkentry.h>
33 #include <gtk/gtkframe.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtkhpaned.h>
36 #include <gtk/gtkicontheme.h>
37 #include <gtk/gtkimage.h>
38 #include <gtk/gtklabel.h>
39 #include <gtk/gtkmenuitem.h>
40 #include <gtk/gtkoptionmenu.h>
41 #include <gtk/gtkscrolledwindow.h>
42 #include <gtk/gtkstock.h>
43 #include <gtk/gtktable.h>
44 #include <gtk/gtktreeview.h>
45 #include <gtk/gtktreemodelsort.h>
46 #include <gtk/gtktreeselection.h>
47 #include <gtk/gtktreestore.h>
48 #include <gtk/gtkvbox.h>
53 typedef struct _GtkFileChooserImplDefaultClass GtkFileChooserImplDefaultClass;
55 #define GTK_FILE_CHOOSER_IMPL_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_IMPL_DEFAULT, GtkFileChooserImplDefaultClass))
56 #define GTK_IS_FILE_CHOOSER_IMPL_DEFAULT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_IMPL_DEFAULT))
57 #define GTK_FILE_CHOOSER_IMPL_DEFAULT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_IMPL_DEFAULT, GtkFileChooserImplDefaultClass))
59 struct _GtkFileChooserImplDefaultClass
61 GtkVBoxClass parent_class;
64 struct _GtkFileChooserImplDefault
66 GtkVBox parent_instance;
68 GtkFileSystem *file_system;
69 GtkFileSystemModel *tree_model;
70 GtkTreeStore *shortcuts_model;
71 GtkFileSystemModel *list_model;
72 GtkTreeModelSort *sort_model;
74 GtkFileChooserAction action;
76 GtkFileFilter *current_filter;
85 guint bookmarks_changed_id;
86 GtkTreeIter bookmarks_iter;
88 GtkFilePath *current_folder;
89 GtkFilePath *preview_path;
91 GtkWidget *preview_frame;
93 GtkWidget *filter_alignment;
94 GtkWidget *filter_option_menu;
95 GtkWidget *tree_scrollwin;
97 GtkWidget *shortcuts_scrollwin;
98 GtkWidget *shortcuts_tree;
99 GtkWidget *add_bookmark_button;
100 GtkWidget *remove_bookmark_button;
101 GtkWidget *list_scrollwin;
104 GtkWidget *preview_widget;
105 GtkWidget *extra_widget;
107 guint folder_mode : 1;
108 guint local_only : 1;
109 guint preview_widget_active : 1;
110 guint select_multiple : 1;
111 guint show_hidden : 1;
112 guint changing_folder : 1;
113 guint list_sort_ascending : 1;
114 guint bookmarks_set : 1;
117 /* Column numbers for the shortcuts tree. Keep these in sync with create_shortcuts_model() */
119 SHORTCUTS_COL_PIXBUF,
122 SHORTCUTS_COL_NUM_COLUMNS
125 /* Column numbers for the file list */
130 FILE_LIST_COL_NUM_COLUMNS
133 /* Standard icon size */
136 static void gtk_file_chooser_impl_default_class_init (GtkFileChooserImplDefaultClass *class);
137 static void gtk_file_chooser_impl_default_iface_init (GtkFileChooserIface *iface);
138 static void gtk_file_chooser_impl_default_init (GtkFileChooserImplDefault *impl);
140 static GObject* gtk_file_chooser_impl_default_constructor (GType type,
141 guint n_construct_properties,
142 GObjectConstructParam *construct_params);
143 static void gtk_file_chooser_impl_default_finalize (GObject *object);
144 static void gtk_file_chooser_impl_default_set_property (GObject *object,
148 static void gtk_file_chooser_impl_default_get_property (GObject *object,
152 static void gtk_file_chooser_impl_default_show_all (GtkWidget *widget);
154 static void gtk_file_chooser_impl_default_set_current_folder (GtkFileChooser *chooser,
155 const GtkFilePath *path);
156 static GtkFilePath * gtk_file_chooser_impl_default_get_current_folder (GtkFileChooser *chooser);
157 static void gtk_file_chooser_impl_default_set_current_name (GtkFileChooser *chooser,
159 static void gtk_file_chooser_impl_default_select_path (GtkFileChooser *chooser,
160 const GtkFilePath *path);
161 static void gtk_file_chooser_impl_default_unselect_path (GtkFileChooser *chooser,
162 const GtkFilePath *path);
163 static void gtk_file_chooser_impl_default_select_all (GtkFileChooser *chooser);
164 static void gtk_file_chooser_impl_default_unselect_all (GtkFileChooser *chooser);
165 static GSList * gtk_file_chooser_impl_default_get_paths (GtkFileChooser *chooser);
166 static GtkFilePath * gtk_file_chooser_impl_default_get_preview_path (GtkFileChooser *chooser);
167 static GtkFileSystem *gtk_file_chooser_impl_default_get_file_system (GtkFileChooser *chooser);
168 static void gtk_file_chooser_impl_default_add_filter (GtkFileChooser *chooser,
169 GtkFileFilter *filter);
170 static void gtk_file_chooser_impl_default_remove_filter (GtkFileChooser *chooser,
171 GtkFileFilter *filter);
172 static GSList * gtk_file_chooser_impl_default_list_filters (GtkFileChooser *chooser);
173 static gboolean gtk_file_chooser_impl_default_add_shortcut_folder (GtkFileChooser *chooser,
174 const GtkFilePath *path,
176 static gboolean gtk_file_chooser_impl_default_remove_shortcut_folder (GtkFileChooser *chooser,
177 const GtkFilePath *path,
179 static GSList * gtk_file_chooser_impl_default_list_shortcut_folders (GtkFileChooser *chooser);
181 static void set_current_filter (GtkFileChooserImplDefault *impl,
182 GtkFileFilter *filter);
183 static void check_preview_change (GtkFileChooserImplDefault *impl);
185 static void filter_option_menu_changed (GtkOptionMenu *option_menu,
186 GtkFileChooserImplDefault *impl);
187 static void tree_selection_changed (GtkTreeSelection *tree_selection,
188 GtkFileChooserImplDefault *impl);
189 static void shortcuts_selection_changed (GtkTreeSelection *tree_selection,
190 GtkFileChooserImplDefault *impl);
191 static void list_selection_changed (GtkTreeSelection *tree_selection,
192 GtkFileChooserImplDefault *impl);
193 static void list_row_activated (GtkTreeView *tree_view,
195 GtkTreeViewColumn *column,
196 GtkFileChooserImplDefault *impl);
197 static void entry_activate (GtkEntry *entry,
198 GtkFileChooserImplDefault *impl);
200 static void tree_name_data_func (GtkTreeViewColumn *tree_column,
201 GtkCellRenderer *cell,
202 GtkTreeModel *tree_model,
205 static void list_icon_data_func (GtkTreeViewColumn *tree_column,
206 GtkCellRenderer *cell,
207 GtkTreeModel *tree_model,
210 static void list_name_data_func (GtkTreeViewColumn *tree_column,
211 GtkCellRenderer *cell,
212 GtkTreeModel *tree_model,
216 static void list_size_data_func (GtkTreeViewColumn *tree_column,
217 GtkCellRenderer *cell,
218 GtkTreeModel *tree_model,
222 static void list_mtime_data_func (GtkTreeViewColumn *tree_column,
223 GtkCellRenderer *cell,
224 GtkTreeModel *tree_model,
228 static GObjectClass *parent_class;
231 _gtk_file_chooser_impl_default_get_type (void)
233 static GType file_chooser_impl_default_type = 0;
235 if (!file_chooser_impl_default_type)
237 static const GTypeInfo file_chooser_impl_default_info =
239 sizeof (GtkFileChooserImplDefaultClass),
240 NULL, /* base_init */
241 NULL, /* base_finalize */
242 (GClassInitFunc) gtk_file_chooser_impl_default_class_init,
243 NULL, /* class_finalize */
244 NULL, /* class_data */
245 sizeof (GtkFileChooserImplDefault),
247 (GInstanceInitFunc) gtk_file_chooser_impl_default_init,
250 static const GInterfaceInfo file_chooser_info =
252 (GInterfaceInitFunc) gtk_file_chooser_impl_default_iface_init, /* interface_init */
253 NULL, /* interface_finalize */
254 NULL /* interface_data */
257 file_chooser_impl_default_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserImplDefault",
258 &file_chooser_impl_default_info, 0);
259 g_type_add_interface_static (file_chooser_impl_default_type,
260 GTK_TYPE_FILE_CHOOSER,
264 return file_chooser_impl_default_type;
268 gtk_file_chooser_impl_default_class_init (GtkFileChooserImplDefaultClass *class)
270 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
271 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
273 parent_class = g_type_class_peek_parent (class);
275 gobject_class->finalize = gtk_file_chooser_impl_default_finalize;
276 gobject_class->constructor = gtk_file_chooser_impl_default_constructor;
277 gobject_class->set_property = gtk_file_chooser_impl_default_set_property;
278 gobject_class->get_property = gtk_file_chooser_impl_default_get_property;
280 widget_class->show_all = gtk_file_chooser_impl_default_show_all;
282 _gtk_file_chooser_install_properties (gobject_class);
286 gtk_file_chooser_impl_default_iface_init (GtkFileChooserIface *iface)
288 iface->select_path = gtk_file_chooser_impl_default_select_path;
289 iface->unselect_path = gtk_file_chooser_impl_default_unselect_path;
290 iface->select_all = gtk_file_chooser_impl_default_select_all;
291 iface->unselect_all = gtk_file_chooser_impl_default_unselect_all;
292 iface->get_paths = gtk_file_chooser_impl_default_get_paths;
293 iface->get_preview_path = gtk_file_chooser_impl_default_get_preview_path;
294 iface->get_file_system = gtk_file_chooser_impl_default_get_file_system;
295 iface->set_current_folder = gtk_file_chooser_impl_default_set_current_folder;
296 iface->get_current_folder = gtk_file_chooser_impl_default_get_current_folder;
297 iface->set_current_name = gtk_file_chooser_impl_default_set_current_name;
298 iface->add_filter = gtk_file_chooser_impl_default_add_filter;
299 iface->remove_filter = gtk_file_chooser_impl_default_remove_filter;
300 iface->list_filters = gtk_file_chooser_impl_default_list_filters;
301 iface->add_shortcut_folder = gtk_file_chooser_impl_default_add_shortcut_folder;
302 iface->remove_shortcut_folder = gtk_file_chooser_impl_default_remove_shortcut_folder;
303 iface->list_shortcut_folders = gtk_file_chooser_impl_default_list_shortcut_folders;
307 gtk_file_chooser_impl_default_init (GtkFileChooserImplDefault *impl)
309 impl->folder_mode = FALSE;
310 impl->local_only = TRUE;
311 impl->preview_widget_active = TRUE;
312 impl->select_multiple = FALSE;
313 impl->show_hidden = FALSE;
315 gtk_container_set_border_width (GTK_CONTAINER (impl), 5);
319 gtk_file_chooser_impl_default_finalize (GObject *object)
321 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (object);
323 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
324 impl->bookmarks_changed_id = 0;
325 g_object_unref (impl->file_system);
327 G_OBJECT_CLASS (parent_class)->finalize (object);
331 update_preview_widget_visibility (GtkFileChooserImplDefault *impl)
333 if (impl->preview_widget_active && impl->preview_widget)
334 gtk_widget_show (impl->preview_frame);
336 gtk_widget_hide (impl->preview_frame);
340 set_preview_widget (GtkFileChooserImplDefault *impl,
341 GtkWidget *preview_widget)
343 if (preview_widget == impl->preview_widget)
346 if (impl->preview_widget)
347 gtk_container_remove (GTK_CONTAINER (impl->preview_frame),
348 impl->preview_widget);
350 impl->preview_widget = preview_widget;
351 if (impl->preview_widget)
353 gtk_widget_show (impl->preview_widget);
354 gtk_container_add (GTK_CONTAINER (impl->preview_frame),
355 impl->preview_widget);
358 update_preview_widget_visibility (impl);
361 /* Used from gtk_tree_model_foreach(); selects the item that corresponds to the
364 set_current_shortcut_foreach_cb (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
366 GtkFileChooserImplDefault *impl;
367 GtkFilePath *model_path;
368 GtkTreeSelection *selection;
370 impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (data);
372 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
374 gtk_tree_model_get (model, iter, SHORTCUTS_COL_PATH, &model_path, -1);
376 if (model_path && impl->current_folder && gtk_file_path_compare (model_path, impl->current_folder) == 0)
378 gtk_tree_selection_select_path (selection, path);
382 gtk_tree_selection_unselect_path (selection, path);
387 /* Selects the appropriate node in the shortcuts tree based on the current folder */
389 shortcuts_select_folder (GtkFileChooserImplDefault *impl)
391 gtk_tree_model_foreach (GTK_TREE_MODEL (impl->shortcuts_model),
392 set_current_shortcut_foreach_cb,
396 /* Convenience function to get the display name and icon info for a path */
398 get_file_info (GtkFileSystem *file_system, const GtkFilePath *path, GError **error)
400 GtkFilePath *parent_path;
401 GtkFileFolder *parent_folder;
404 if (!gtk_file_system_get_parent (file_system, path, &parent_path, error))
407 parent_folder = gtk_file_system_get_folder (file_system, parent_path,
408 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
410 gtk_file_path_free (parent_path);
415 info = gtk_file_folder_get_info (parent_folder, path, error);
416 g_object_unref (parent_folder);
421 /* Inserts a path in the shortcuts tree, making a copy of it. A position of -1
422 * indicates the end of the tree. If the label is NULL, then the display name
423 * of a GtkFileInfo is used.
426 shortcuts_insert_path (GtkFileChooserImplDefault *impl,
428 const GtkFilePath *path,
434 GtkFilePath *path_copy;
438 /* FIXME: what if someone adds a shortcut to a root? get_file_info() will not
439 * work in that case, I think...
443 info = gtk_file_system_get_root_info (impl->file_system,
445 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
448 info = get_file_info (impl->file_system, path, error);
453 pixbuf = gtk_file_info_render_icon (info, impl->shortcuts_tree, ICON_SIZE);
455 gtk_tree_store_insert (impl->shortcuts_model, &iter, NULL, pos);
456 path_copy = gtk_file_path_copy (path);
459 label = gtk_file_info_get_display_name (info);
461 gtk_tree_store_set (impl->shortcuts_model, &iter,
462 SHORTCUTS_COL_PIXBUF, pixbuf,
463 SHORTCUTS_COL_NAME, label,
464 SHORTCUTS_COL_PATH, path_copy,
468 g_object_unref (pixbuf);
473 /* Appends an item for the user's home directory to the shortcuts model */
475 shortcuts_append_home (GtkFileChooserImplDefault *impl)
479 GtkFilePath *home_path;
482 name = g_get_user_name ();
483 label = g_strdup_printf ("%s's Home", name);
485 home = g_get_home_dir ();
486 home_path = gtk_file_system_filename_to_path (impl->file_system, home);
488 impl->has_home = shortcuts_insert_path (impl, -1, home_path, FALSE, label, NULL); /* FIXME: use GError? */
491 gtk_file_path_free (home_path);
494 /* Appends the ~/Desktop directory to the shortcuts model */
496 shortcuts_append_desktop (GtkFileChooserImplDefault *impl)
501 /* FIXME: What is the Right Way of finding the desktop directory? */
503 name = g_build_filename (g_get_home_dir (), "Desktop", NULL);
504 path = gtk_file_system_filename_to_path (impl->file_system, name);
507 impl->has_desktop = shortcuts_insert_path (impl, -1, path, FALSE, NULL, NULL); /* FIXME: use GError? */
508 gtk_file_path_free (path);
511 /* Appends all the file system roots to the shortcuts model */
513 shortcuts_append_file_system_roots (GtkFileChooserImplDefault *impl)
517 roots = gtk_file_system_list_roots (impl->file_system);
518 /* FIXME: handle the roots-changed signal on the file system */
522 for (l = roots; l; l = l->next)
527 if (shortcuts_insert_path (impl, -1, path, TRUE, NULL, NULL)) /* FIXME: use GError? */
531 gtk_file_paths_free (roots);
534 /* Removes the bookmarks separator node and all the bookmarks from the tree
538 remove_bookmark_rows (GtkFileChooserImplDefault *impl)
543 if (!impl->bookmarks_set)
546 /* Ugh. Is there a better way to do this? */
548 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &impl->bookmarks_iter);
550 while (gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->shortcuts_model), &iter, path))
551 gtk_tree_store_remove (impl->shortcuts_model, &impl->bookmarks_iter);
553 impl->bookmarks_set = FALSE;
556 /* Appends the bookmarks separator node and the bookmarks from the file system. */
558 shortcuts_append_bookmarks (GtkFileChooserImplDefault *impl)
560 GSList *bookmarks, *l;
562 remove_bookmark_rows (impl);
564 gtk_tree_store_append (impl->shortcuts_model, &impl->bookmarks_iter, NULL);
565 gtk_tree_store_set (impl->shortcuts_model, &impl->bookmarks_iter,
566 SHORTCUTS_COL_PIXBUF, NULL,
567 SHORTCUTS_COL_NAME, NULL,
568 SHORTCUTS_COL_PATH, NULL,
570 impl->bookmarks_set = TRUE;
571 impl->num_bookmarks = 0;
573 bookmarks = gtk_file_system_list_bookmarks (impl->file_system);
575 for (l = bookmarks; l; l = l->next)
580 if (shortcuts_insert_path (impl, -1, path, FALSE, NULL, NULL)) /* FIXME: use GError? */
581 impl->num_bookmarks++;
585 /* Creates the GtkTreeStore used as the shortcuts model */
587 create_shortcuts_model (GtkFileChooserImplDefault *impl)
589 if (impl->shortcuts_model)
590 g_object_unref (impl->shortcuts_model);
592 /* Keep this order in sync with the SHORCUTS_COL_* enum values */
593 impl->shortcuts_model = gtk_tree_store_new (SHORTCUTS_COL_NUM_COLUMNS,
594 GDK_TYPE_PIXBUF, /* pixbuf */
595 G_TYPE_STRING, /* name */
596 G_TYPE_POINTER); /* path */
598 if (impl->file_system)
600 shortcuts_append_home (impl);
601 shortcuts_append_desktop (impl);
602 shortcuts_append_file_system_roots (impl);
603 shortcuts_append_bookmarks (impl);
606 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->shortcuts_tree), GTK_TREE_MODEL (impl->shortcuts_model));
609 /* Creates the widgets for the filter option menu */
611 create_filter (GtkFileChooserImplDefault *impl)
616 impl->filter_alignment = gtk_alignment_new (0.0, 0.5, 0.0, 1.0);
617 gtk_alignment_set_padding (GTK_ALIGNMENT (impl->filter_alignment), 0, 6, 0, 0);
618 /* Don't show filter initially -- don't gtk_widget_show() the filter_alignment here */
620 hbox = gtk_hbox_new (FALSE, 6);
621 gtk_container_add (GTK_CONTAINER (impl->filter_alignment), hbox);
622 gtk_widget_show (hbox);
624 label = gtk_label_new_with_mnemonic ("Files of _type:");
625 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
626 gtk_widget_show (label);
628 impl->filter_option_menu = gtk_option_menu_new ();
629 gtk_option_menu_set_menu (GTK_OPTION_MENU (impl->filter_option_menu),
631 gtk_box_pack_start (GTK_BOX (hbox), impl->filter_option_menu, FALSE, FALSE, 0);
632 gtk_widget_show (impl->filter_option_menu);
634 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->filter_option_menu);
636 g_signal_connect (impl->filter_option_menu, "changed",
637 G_CALLBACK (filter_option_menu_changed), impl);
639 return impl->filter_alignment;
642 /* Creates the widgets for the folder tree */
644 create_folder_tree (GtkFileChooserImplDefault *impl)
646 GtkTreeSelection *selection;
648 /* Scrolled window */
650 impl->tree_scrollwin = gtk_scrolled_window_new (NULL, NULL);
651 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->tree_scrollwin),
652 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
653 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->tree_scrollwin),
655 if (impl->folder_mode)
656 gtk_widget_show (impl->tree_scrollwin);
660 impl->tree = gtk_tree_view_new ();
661 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->tree), FALSE);
663 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->tree));
664 g_signal_connect (selection, "changed",
665 G_CALLBACK (tree_selection_changed), impl);
667 gtk_container_add (GTK_CONTAINER (impl->tree_scrollwin), impl->tree);
668 gtk_widget_show (impl->tree);
672 impl->tree_model = _gtk_file_system_model_new (impl->file_system, NULL, -1,
673 GTK_FILE_INFO_DISPLAY_NAME);
674 _gtk_file_system_model_set_show_files (impl->tree_model, FALSE);
676 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->tree),
677 GTK_TREE_MODEL (impl->tree_model));
681 gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (impl->tree), 0,
683 gtk_cell_renderer_text_new (),
684 tree_name_data_func, impl, NULL);
685 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->tree),
686 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
688 return impl->tree_scrollwin;
691 /* Callback used when the "Add bookmark" button is clicked */
693 add_bookmark_button_clicked_cb (GtkButton *button,
694 GtkFileChooserImplDefault *impl)
696 gtk_file_system_add_bookmark (impl->file_system, impl->current_folder, NULL); /* FIXME: use GError */
699 /* Callback used when the "Remove bookmark" button is clicked */
701 remove_bookmark_button_clicked_cb (GtkButton *button,
702 GtkFileChooserImplDefault *impl)
704 GtkTreeSelection *selection;
708 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
710 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
713 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &path, -1);
715 gtk_file_system_remove_bookmark (impl->file_system, path, NULL); /* FIXME: use GError */
718 /* Sensitize the "add bookmark" button if the current folder is not in the
719 * bookmarks list, or de-sensitize it otherwise.
722 bookmarks_check_add_sensitivity (GtkFileChooserImplDefault *impl)
729 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
734 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &path, -1);
736 if (path && gtk_file_path_compare (path, impl->current_folder) == 0)
742 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter));
744 gtk_widget_set_sensitive (impl->add_bookmark_button, !exists);
747 /* Sets the sensitivity of the "remove bookmark" button depending on whether a
748 * bookmark row is selected in the shortcuts tree.
751 bookmarks_check_remove_sensitivity (GtkFileChooserImplDefault *impl)
753 GtkTreeSelection *selection;
755 gboolean is_bookmark;
757 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
759 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
761 GtkTreePath *bookmarks_path;
762 GtkTreePath *sel_path;
764 bookmarks_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model),
765 &impl->bookmarks_iter);
766 sel_path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->shortcuts_model), &iter);
768 is_bookmark = (gtk_tree_path_compare (bookmarks_path, sel_path) < 0);
770 gtk_tree_path_free (bookmarks_path);
771 gtk_tree_path_free (sel_path);
776 gtk_widget_set_sensitive (impl->remove_bookmark_button, is_bookmark);
779 /* Creates the widgets for the shortcuts and bookmarks tree */
781 create_shortcuts_tree (GtkFileChooserImplDefault *impl)
785 GtkTreeSelection *selection;
786 GtkTreeViewColumn *column;
787 GtkCellRenderer *renderer;
790 vbox = gtk_vbox_new (FALSE, 12);
791 gtk_widget_show (vbox);
793 /* Scrolled window */
795 impl->shortcuts_scrollwin = gtk_scrolled_window_new (NULL, NULL);
796 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->shortcuts_scrollwin),
797 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
798 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->shortcuts_scrollwin),
800 gtk_box_pack_start (GTK_BOX (vbox), impl->shortcuts_scrollwin, TRUE, TRUE, 0);
801 gtk_widget_show (impl->shortcuts_scrollwin);
805 impl->shortcuts_tree = gtk_tree_view_new ();
806 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (impl->shortcuts_tree), FALSE);
808 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->shortcuts_tree));
809 g_signal_connect (selection, "changed",
810 G_CALLBACK (shortcuts_selection_changed), impl);
812 gtk_container_add (GTK_CONTAINER (impl->shortcuts_scrollwin), impl->shortcuts_tree);
813 gtk_widget_show (impl->shortcuts_tree);
817 create_shortcuts_model (impl);
821 column = gtk_tree_view_column_new ();
822 gtk_tree_view_column_set_title (column, "Folder");
824 renderer = gtk_cell_renderer_pixbuf_new ();
825 gtk_tree_view_column_pack_start (column, renderer, FALSE);
826 gtk_tree_view_column_set_attributes (column, renderer,
827 "pixbuf", SHORTCUTS_COL_PIXBUF,
830 renderer = _gtk_cell_renderer_sep_text_new ();
831 gtk_tree_view_column_pack_start (column, renderer, TRUE);
832 gtk_tree_view_column_set_attributes (column, renderer,
833 "text", SHORTCUTS_COL_NAME,
836 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->shortcuts_tree), column);
838 /* Bookmark buttons */
840 hbox = gtk_hbox_new (FALSE, 12);
841 gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
842 gtk_widget_show (hbox);
844 impl->add_bookmark_button = gtk_button_new_with_label ("Add bookmark");
845 g_signal_connect (impl->add_bookmark_button, "clicked",
846 G_CALLBACK (add_bookmark_button_clicked_cb), impl);
847 gtk_box_pack_start (GTK_BOX (hbox), impl->add_bookmark_button, TRUE, TRUE, 0);
848 gtk_widget_set_sensitive (impl->add_bookmark_button, FALSE);
849 gtk_widget_show (impl->add_bookmark_button);
851 impl->remove_bookmark_button = gtk_button_new ();
852 g_signal_connect (impl->remove_bookmark_button, "clicked",
853 G_CALLBACK (remove_bookmark_button_clicked_cb), impl);
854 image = gtk_image_new_from_stock (GTK_STOCK_DELETE, GTK_ICON_SIZE_BUTTON);
855 gtk_container_add (GTK_CONTAINER (impl->remove_bookmark_button), image);
856 gtk_widget_show (image);
857 gtk_widget_set_sensitive (impl->remove_bookmark_button, FALSE);
858 gtk_box_pack_start (GTK_BOX (hbox), impl->remove_bookmark_button, FALSE, FALSE, 0);
859 gtk_widget_show (impl->remove_bookmark_button);
861 shortcuts_select_folder (impl);
866 /* Creates the widgets for the file list */
868 create_file_list (GtkFileChooserImplDefault *impl)
870 GtkTreeSelection *selection;
871 GtkTreeViewColumn *column;
872 GtkCellRenderer *renderer;
874 /* Scrolled window */
876 impl->list_scrollwin = gtk_scrolled_window_new (NULL, NULL);
877 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->list_scrollwin),
878 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
879 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->list_scrollwin),
881 if (!impl->folder_mode)
882 gtk_widget_show (impl->list_scrollwin);
886 impl->list = gtk_tree_view_new ();
887 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (impl->list), TRUE);
888 gtk_container_add (GTK_CONTAINER (impl->list_scrollwin), impl->list);
889 g_signal_connect (impl->list, "row_activated",
890 G_CALLBACK (list_row_activated), impl);
891 gtk_widget_show (impl->list);
893 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
894 g_signal_connect (selection, "changed",
895 G_CALLBACK (list_selection_changed), impl);
897 /* Filename column */
899 column = gtk_tree_view_column_new ();
900 gtk_tree_view_column_set_title (column, "File name");
901 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_NAME);
903 renderer = gtk_cell_renderer_pixbuf_new ();
904 gtk_tree_view_column_pack_start (column, renderer, TRUE);
905 gtk_tree_view_column_set_cell_data_func (column, renderer,
906 list_icon_data_func, impl, NULL);
908 renderer = gtk_cell_renderer_text_new ();
909 gtk_tree_view_column_pack_start (column, renderer, TRUE);
910 gtk_tree_view_column_set_cell_data_func (column, renderer,
911 list_name_data_func, impl, NULL);
913 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), column);
917 column = gtk_tree_view_column_new ();
918 gtk_tree_view_column_set_title (column, "Size");
920 renderer = gtk_cell_renderer_text_new ();
921 gtk_tree_view_column_pack_start (column, renderer, TRUE);
922 gtk_tree_view_column_set_cell_data_func (column, renderer,
923 list_size_data_func, impl, NULL);
924 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_SIZE);
925 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), column);
927 /* Modification time column */
929 column = gtk_tree_view_column_new ();
930 gtk_tree_view_column_set_title (column, "Modified");
932 renderer = gtk_cell_renderer_text_new ();
933 gtk_tree_view_column_pack_start (column, renderer, TRUE);
934 gtk_tree_view_column_set_cell_data_func (column, renderer,
935 list_mtime_data_func, impl, NULL);
936 gtk_tree_view_column_set_sort_column_id (column, FILE_LIST_COL_MTIME);
937 gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), column);
939 return impl->list_scrollwin;
943 create_filename_entry (GtkFileChooserImplDefault *impl)
948 hbox = gtk_hbox_new (FALSE, 6);
949 gtk_widget_show (hbox);
951 label = gtk_label_new_with_mnemonic ("_Location:");
952 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
953 gtk_widget_show (label);
955 impl->entry = _gtk_file_chooser_entry_new ();
956 gtk_entry_set_activates_default (GTK_ENTRY (impl->entry), TRUE);
957 g_signal_connect (impl->entry, "activate",
958 G_CALLBACK (entry_activate), impl);
959 _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->entry),
962 gtk_box_pack_start (GTK_BOX (hbox), impl->entry, TRUE, TRUE, 0);
963 gtk_widget_show (impl->entry);
965 gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->entry);
971 gtk_file_chooser_impl_default_constructor (GType type,
972 guint n_construct_properties,
973 GObjectConstructParam *construct_params)
975 GtkFileChooserImplDefault *impl;
985 object = parent_class->constructor (type,
986 n_construct_properties,
988 impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (object);
990 g_assert (impl->file_system);
992 gtk_widget_push_composite_child ();
996 table = gtk_table_new (3, 2, FALSE);
997 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
998 gtk_box_pack_start (GTK_BOX (impl), table, TRUE, TRUE, 0);
999 gtk_widget_show (table);
1003 widget = create_filter (impl);
1004 gtk_table_attach (GTK_TABLE (table), widget,
1006 GTK_EXPAND | GTK_FILL, 0,
1011 hpaned = gtk_hpaned_new ();
1012 gtk_table_attach (GTK_TABLE (table), hpaned,
1014 GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
1016 gtk_paned_set_position (GTK_PANED (hpaned), 200); /* FIXME: this sucks */
1017 gtk_widget_show (hpaned);
1019 /* Shortcuts list */
1021 widget = create_shortcuts_tree (impl);
1022 gtk_paned_add1 (GTK_PANED (hpaned), widget);
1026 hbox = gtk_hbox_new (FALSE, 12);
1027 gtk_paned_add2 (GTK_PANED (hpaned), hbox);
1028 gtk_widget_show (hbox);
1030 widget = create_folder_tree (impl);
1031 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1035 widget = create_file_list (impl);
1036 gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
1038 /* Location/filename entry */
1040 widget = create_filename_entry (impl);
1041 gtk_table_attach (GTK_TABLE (table), widget,
1043 GTK_EXPAND | GTK_FILL, 0,
1048 impl->preview_frame = gtk_frame_new ("Preview");
1049 gtk_table_attach (GTK_TABLE (table), impl->preview_frame,
1051 0, GTK_EXPAND | GTK_FILL,
1053 /* Don't show preview frame initially */
1056 focus_chain = g_list_append (NULL, impl->entry);
1057 focus_chain = g_list_append (focus_chain, impl->tree);
1058 focus_chain = g_list_append (focus_chain, impl->list);
1059 gtk_container_set_focus_chain (GTK_CONTAINER (impl), focus_chain);
1060 g_list_free (focus_chain);
1063 gtk_widget_pop_composite_child ();
1068 /* Sets the extra_widget by packing it in the appropriate place */
1070 set_extra_widget (GtkFileChooserImplDefault *impl,
1071 GtkWidget *extra_widget)
1073 if (extra_widget == impl->extra_widget)
1076 if (impl->extra_widget)
1077 gtk_container_remove (GTK_CONTAINER (impl), impl->extra_widget);
1079 impl->extra_widget = extra_widget;
1080 if (impl->extra_widget)
1082 gtk_widget_show (impl->extra_widget);
1083 gtk_box_pack_end (GTK_BOX (impl), impl->extra_widget, FALSE, FALSE, 0);
1087 /* Callback used when the set of bookmarks changes in the file system */
1089 bookmarks_changed_cb (GtkFileSystem *file_system,
1090 GtkFileChooserImplDefault *impl)
1092 shortcuts_append_bookmarks (impl);
1093 shortcuts_select_folder (impl);
1095 bookmarks_check_add_sensitivity (impl);
1096 bookmarks_check_remove_sensitivity (impl);
1100 gtk_file_chooser_impl_default_set_property (GObject *object,
1102 const GValue *value,
1106 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (object);
1110 case GTK_FILE_CHOOSER_PROP_ACTION:
1111 impl->action = g_value_get_enum (value);
1113 case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM:
1115 GtkFileSystem *file_system = g_value_get_object (value);
1116 if (impl->file_system != file_system)
1118 if (impl->file_system)
1120 g_signal_handler_disconnect (impl->file_system, impl->bookmarks_changed_id);
1121 impl->bookmarks_changed_id = 0;
1122 g_object_unref (impl->file_system);
1124 impl->file_system = file_system;
1125 if (impl->file_system)
1127 g_object_ref (impl->file_system);
1128 impl->bookmarks_changed_id = g_signal_connect (impl->file_system, "bookmarks-changed",
1129 G_CALLBACK (bookmarks_changed_cb),
1135 case GTK_FILE_CHOOSER_PROP_FILTER:
1136 set_current_filter (impl, g_value_get_object (value));
1138 case GTK_FILE_CHOOSER_PROP_FOLDER_MODE:
1140 gboolean folder_mode = g_value_get_boolean (value);
1141 if (folder_mode != impl->folder_mode)
1143 impl->folder_mode = folder_mode;
1144 if (impl->folder_mode)
1146 gtk_widget_hide (impl->list_scrollwin);
1147 gtk_widget_show (impl->tree_scrollwin);
1151 gtk_widget_hide (impl->tree_scrollwin);
1152 gtk_widget_show (impl->list_scrollwin);
1157 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1158 impl->local_only = g_value_get_boolean (value);
1160 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1161 set_preview_widget (impl, g_value_get_object (value));
1163 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1164 impl->preview_widget_active = g_value_get_boolean (value);
1165 update_preview_widget_visibility (impl);
1167 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1168 set_extra_widget (impl, g_value_get_object (value));
1170 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1172 gboolean select_multiple = g_value_get_boolean (value);
1173 if (select_multiple != impl->select_multiple)
1175 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1177 impl->select_multiple = select_multiple;
1178 gtk_tree_selection_set_mode (selection,
1180 GTK_SELECTION_MULTIPLE : GTK_SELECTION_BROWSE));
1181 /* FIXME: See note in check_preview_change() */
1182 check_preview_change (impl);
1186 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1188 gboolean show_hidden = g_value_get_boolean (value);
1189 if (show_hidden != impl->show_hidden)
1191 impl->show_hidden = show_hidden;
1192 _gtk_file_system_model_set_show_hidden (impl->tree_model, show_hidden);
1193 _gtk_file_system_model_set_show_hidden (impl->list_model, show_hidden);
1198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1204 gtk_file_chooser_impl_default_get_property (GObject *object,
1209 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (object);
1213 case GTK_FILE_CHOOSER_PROP_ACTION:
1214 g_value_set_enum (value, impl->action);
1216 case GTK_FILE_CHOOSER_PROP_FILTER:
1217 g_value_set_object (value, impl->current_filter);
1219 case GTK_FILE_CHOOSER_PROP_FOLDER_MODE:
1220 g_value_set_boolean (value, impl->folder_mode);
1222 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1223 g_value_set_boolean (value, impl->local_only);
1225 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1226 g_value_set_object (value, impl->preview_widget);
1228 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1229 g_value_set_boolean (value, impl->preview_widget_active);
1231 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1232 g_value_set_object (value, impl->extra_widget);
1234 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1235 g_value_set_boolean (value, impl->select_multiple);
1237 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1238 g_value_set_boolean (value, impl->show_hidden);
1241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1246 /* We override show-all since we have internal widgets that
1247 * shouldn't be shown when you call show_all(), like the filter
1251 gtk_file_chooser_impl_default_show_all (GtkWidget *widget)
1253 gtk_widget_show (widget);
1257 expand_and_select_func (GtkFileSystemModel *model,
1262 GtkFileChooserImplDefault *impl = user_data;
1263 GtkTreeView *tree_view;
1265 if (model == impl->tree_model)
1266 tree_view = GTK_TREE_VIEW (impl->tree);
1268 tree_view = GTK_TREE_VIEW (impl->list);
1270 gtk_tree_view_expand_to_path (tree_view, path);
1271 gtk_tree_view_expand_row (tree_view, path, FALSE);
1272 gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE);
1273 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->tree), path, NULL, TRUE, 0.3, 0.5);
1277 list_model_filter_func (GtkFileSystemModel *model,
1279 const GtkFileInfo *file_info,
1282 GtkFileChooserImplDefault *impl = user_data;
1283 GtkFileFilterInfo filter_info;
1284 GtkFileFilterFlags needed;
1287 if (!impl->current_filter)
1290 filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE;
1292 needed = gtk_file_filter_get_needed (impl->current_filter);
1294 filter_info.display_name = gtk_file_info_get_display_name (file_info);
1295 filter_info.mime_type = gtk_file_info_get_mime_type (file_info);
1297 if (needed & GTK_FILE_FILTER_FILENAME)
1299 filter_info.filename = gtk_file_system_path_to_filename (impl->file_system, path);
1300 if (filter_info.filename)
1301 filter_info.contains |= GTK_FILE_FILTER_FILENAME;
1304 filter_info.filename = NULL;
1306 if (needed & GTK_FILE_FILTER_URI)
1308 filter_info.uri = gtk_file_system_path_to_uri (impl->file_system, path);
1309 if (filter_info.filename)
1310 filter_info.contains |= GTK_FILE_FILTER_URI;
1313 filter_info.uri = NULL;
1315 result = gtk_file_filter_filter (impl->current_filter, &filter_info);
1317 if (filter_info.filename)
1318 g_free ((gchar *)filter_info.filename);
1319 if (filter_info.uri)
1320 g_free ((gchar *)filter_info.uri);
1326 install_list_model_filter (GtkFileChooserImplDefault *impl)
1328 if (impl->current_filter)
1329 _gtk_file_system_model_set_filter (impl->list_model,
1330 list_model_filter_func,
1334 #define COMPARE_DIRECTORIES \
1335 GtkFileChooserImplDefault *impl = user_data; \
1336 const GtkFileInfo *info_a = _gtk_file_system_model_get_info (impl->tree_model, a); \
1337 const GtkFileInfo *info_b = _gtk_file_system_model_get_info (impl->tree_model, b); \
1338 gboolean dir_a = gtk_file_info_get_is_folder (info_a); \
1339 gboolean dir_b = gtk_file_info_get_is_folder (info_b); \
1341 if (dir_a != dir_b) \
1342 return impl->list_sort_ascending ? (dir_a ? -1 : 1) : (dir_a ? 1 : -1) /* Directories *always* go first */
1344 /* Sort callback for the filename column */
1346 name_sort_func (GtkTreeModel *model,
1351 COMPARE_DIRECTORIES;
1353 return strcmp (gtk_file_info_get_display_key (info_a), gtk_file_info_get_display_key (info_b));
1356 /* Sort callback for the size column */
1358 size_sort_func (GtkTreeModel *model,
1363 COMPARE_DIRECTORIES;
1366 gint64 size_a = gtk_file_info_get_size (info_a);
1367 gint64 size_b = gtk_file_info_get_size (info_b);
1369 return size_a > size_b ? -1 : (size_a == size_b ? 0 : 1);
1373 /* Sort callback for the mtime column */
1375 mtime_sort_func (GtkTreeModel *model,
1380 COMPARE_DIRECTORIES;
1383 GtkFileTime ta = gtk_file_info_get_modification_time (info_a);
1384 GtkFileTime tb = gtk_file_info_get_modification_time (info_b);
1386 return ta > tb ? -1 : (ta == tb ? 0 : 1);
1390 /* Callback used when the sort column changes. We cache the sort order for use
1391 * in name_sort_func().
1394 list_sort_column_changed_cb (GtkTreeSortable *sortable,
1395 GtkFileChooserImplDefault *impl)
1397 GtkSortType sort_type;
1399 if (gtk_tree_sortable_get_sort_column_id (sortable, NULL, &sort_type))
1400 impl->list_sort_ascending = (sort_type == GTK_SORT_ASCENDING);
1403 /* Gets rid of the old list model and creates a new one for the current folder */
1405 set_list_model (GtkFileChooserImplDefault *impl)
1407 if (impl->list_model)
1409 g_object_unref (impl->list_model);
1410 impl->list_model = NULL;
1412 g_object_unref (impl->sort_model);
1413 impl->sort_model = NULL;
1416 impl->list_model = _gtk_file_system_model_new (impl->file_system,
1417 impl->current_folder, 0,
1418 GTK_FILE_INFO_ICON |
1419 GTK_FILE_INFO_DISPLAY_NAME |
1420 GTK_FILE_INFO_IS_FOLDER |
1421 GTK_FILE_INFO_SIZE |
1422 GTK_FILE_INFO_MODIFICATION_TIME);
1423 install_list_model_filter (impl);
1425 impl->sort_model = (GtkTreeModelSort *)gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (impl->list_model));
1426 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, name_sort_func, impl, NULL);
1427 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_SIZE, size_sort_func, impl, NULL);
1428 gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_MTIME, mtime_sort_func, impl, NULL);
1429 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (impl->sort_model), NULL, NULL, NULL);
1430 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (impl->sort_model), FILE_LIST_COL_NAME, GTK_SORT_ASCENDING);
1431 impl->list_sort_ascending = TRUE;
1433 g_signal_connect (impl->sort_model, "sort_column_changed",
1434 G_CALLBACK (list_sort_column_changed_cb), impl);
1436 gtk_tree_view_set_model (GTK_TREE_VIEW (impl->list),
1437 GTK_TREE_MODEL (impl->sort_model));
1438 gtk_tree_view_columns_autosize (GTK_TREE_VIEW (impl->list));
1439 gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->list),
1440 GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME);
1444 update_chooser_entry (GtkFileChooserImplDefault *impl)
1446 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1447 const GtkFileInfo *info;
1449 GtkTreeIter child_iter;
1451 /* Fixing this for multiple selection involves getting the full
1452 * selection and diffing to find out what the most recently selected
1453 * file is; there is logic in GtkFileSelection that probably can
1454 * be copied; check_preview_change() is similar.
1456 if (impl->select_multiple ||
1457 !gtk_tree_selection_get_selected (selection, NULL, &iter))
1460 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
1464 info = _gtk_file_system_model_get_info (impl->list_model, &child_iter);
1466 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->entry),
1467 gtk_file_info_get_display_name (info));
1471 gtk_file_chooser_impl_default_set_current_folder (GtkFileChooser *chooser,
1472 const GtkFilePath *path)
1474 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1476 _gtk_file_system_model_path_do (impl->tree_model, path,
1477 expand_and_select_func, impl);
1480 static GtkFilePath *
1481 gtk_file_chooser_impl_default_get_current_folder (GtkFileChooser *chooser)
1483 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1485 return gtk_file_path_copy (impl->current_folder);
1489 gtk_file_chooser_impl_default_set_current_name (GtkFileChooser *chooser,
1492 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1494 _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->entry), name);
1498 select_func (GtkFileSystemModel *model,
1503 GtkFileChooserImplDefault *impl = user_data;
1504 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->list);
1505 GtkTreePath *sorted_path;
1507 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model, path);
1508 gtk_tree_view_set_cursor (tree_view, sorted_path, NULL, FALSE);
1509 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->tree), sorted_path, NULL, TRUE, 0.3, 0.0);
1510 gtk_tree_path_free (sorted_path);
1514 gtk_file_chooser_impl_default_select_path (GtkFileChooser *chooser,
1515 const GtkFilePath *path)
1517 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1518 GtkFilePath *parent_path;
1520 if (!gtk_file_system_get_parent (impl->file_system, path, &parent_path, NULL)) /* NULL-GError */
1525 _gtk_file_chooser_set_current_folder_path (chooser, path);
1529 _gtk_file_chooser_set_current_folder_path (chooser, parent_path);
1530 gtk_file_path_free (parent_path);
1531 _gtk_file_system_model_path_do (impl->list_model, path,
1537 unselect_func (GtkFileSystemModel *model,
1542 GtkFileChooserImplDefault *impl = user_data;
1543 GtkTreeView *tree_view = GTK_TREE_VIEW (impl->list);
1544 GtkTreePath *sorted_path;
1546 sorted_path = gtk_tree_model_sort_convert_child_path_to_path (impl->sort_model,
1548 gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (tree_view),
1550 gtk_tree_path_free (sorted_path);
1554 gtk_file_chooser_impl_default_unselect_path (GtkFileChooser *chooser,
1555 const GtkFilePath *path)
1557 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1559 _gtk_file_system_model_path_do (impl->list_model, path,
1560 unselect_func, impl);
1564 gtk_file_chooser_impl_default_select_all (GtkFileChooser *chooser)
1566 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1567 if (impl->select_multiple)
1569 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1570 gtk_tree_selection_select_all (selection);
1575 gtk_file_chooser_impl_default_unselect_all (GtkFileChooser *chooser)
1577 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1578 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1580 gtk_tree_selection_unselect_all (selection);
1584 get_paths_foreach (GtkTreeModel *model,
1589 GtkTreePath *child_path;
1590 GtkTreeIter child_iter;
1591 const GtkFilePath *file_path;
1595 GtkFileChooserImplDefault *impl;
1598 child_path = gtk_tree_model_sort_convert_path_to_child_path (info->impl->sort_model, path);
1599 gtk_tree_model_get_iter (GTK_TREE_MODEL (info->impl->list_model), &child_iter, child_path);
1600 gtk_tree_path_free (child_path);
1602 file_path = _gtk_file_system_model_get_path (info->impl->list_model, &child_iter);
1603 info->result = g_slist_prepend (info->result, gtk_file_path_copy (file_path));
1607 gtk_file_chooser_impl_default_get_paths (GtkFileChooser *chooser)
1609 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1610 GtkTreeSelection *selection;
1614 GtkFileChooserImplDefault *impl;
1617 if (!impl->sort_model)
1620 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1623 gtk_tree_selection_selected_foreach (selection,
1624 get_paths_foreach, &info);
1625 return g_slist_reverse (info.result);
1628 static GtkFilePath *
1629 gtk_file_chooser_impl_default_get_preview_path (GtkFileChooser *chooser)
1631 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1633 if (impl->preview_path)
1634 return gtk_file_path_copy (impl->preview_path);
1639 static GtkFileSystem *
1640 gtk_file_chooser_impl_default_get_file_system (GtkFileChooser *chooser)
1642 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1644 return impl->file_system;
1648 find_filter_menu_item (GtkFileChooserImplDefault *impl,
1649 GtkFileFilter *filter,
1652 GtkWidget *menu = gtk_option_menu_get_menu (GTK_OPTION_MENU (impl->filter_option_menu));
1653 GList *children = gtk_container_get_children (GTK_CONTAINER (menu));
1660 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
1662 if (g_object_get_data (tmp_list->data, "gtk-file-filter") == filter)
1665 *index_return = index;
1666 return tmp_list->data;
1671 g_list_free (children);
1677 gtk_file_chooser_impl_default_add_filter (GtkFileChooser *chooser,
1678 GtkFileFilter *filter)
1680 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1682 GtkWidget *menu_item;
1685 if (g_slist_find (impl->filters, filter))
1687 g_warning ("gtk_file_chooser_add_filter() called on filter already in list\n");
1691 g_object_ref (filter);
1692 gtk_object_sink (GTK_OBJECT (filter));
1693 impl->filters = g_slist_append (impl->filters, filter);
1695 name = gtk_file_filter_get_name (filter);
1697 name = "Untitled filter"; /* Place-holder, doesn't need to be marked for translation */
1699 menu_item = gtk_menu_item_new_with_label (name);
1700 g_object_set_data (G_OBJECT (menu_item), "gtk-file-filter", filter);
1701 gtk_widget_show (menu_item);
1703 menu = gtk_option_menu_get_menu (GTK_OPTION_MENU (impl->filter_option_menu));
1704 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
1705 /* Option menus don't react to menu size changes properly */
1706 gtk_widget_size_request (menu, NULL);
1708 if (!g_slist_find (impl->filters, impl->current_filter))
1709 set_current_filter (impl, filter);
1711 gtk_widget_show (impl->filter_alignment);
1715 gtk_file_chooser_impl_default_remove_filter (GtkFileChooser *chooser,
1716 GtkFileFilter *filter)
1718 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1720 GtkWidget *menu_item;
1722 if (!g_slist_find (impl->filters, filter))
1724 g_warning ("gtk_file_chooser_remove_filter() called on filter not in list\n");
1728 impl->filters = g_slist_remove (impl->filters, filter);
1730 if (filter == impl->current_filter)
1733 set_current_filter (impl, impl->filters->data);
1735 set_current_filter (impl, NULL);
1738 menu = gtk_option_menu_get_menu (GTK_OPTION_MENU (impl->filter_option_menu));
1739 menu_item = find_filter_menu_item (impl, filter, NULL);
1740 g_assert (menu_item);
1741 gtk_widget_destroy (menu_item);
1742 /* Option menus don't react to menu size changes properly */
1743 gtk_widget_size_request (menu, NULL);
1745 g_object_unref (filter);
1748 gtk_widget_hide (impl->filter_alignment);
1752 gtk_file_chooser_impl_default_list_filters (GtkFileChooser *chooser)
1754 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1756 return g_slist_copy (impl->filters);
1759 /* Returns the position in the shortcuts tree where the nth specified shortcut would appear */
1761 shortcuts_get_pos_for_shortcut_folder (GtkFileChooserImplDefault *impl,
1764 return pos + ((impl->has_home ? 1 : 0)
1765 + (impl->has_desktop ? 1 : 0)
1770 gtk_file_chooser_impl_default_add_shortcut_folder (GtkFileChooser *chooser,
1771 const GtkFilePath *path,
1774 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1778 pos = shortcuts_get_pos_for_shortcut_folder (impl, impl->num_shortcuts);
1780 /* FIXME: how do we know if the path is a file system root? */
1781 result = shortcuts_insert_path (impl, pos, path, FALSE, NULL, error);
1784 impl->num_shortcuts++;
1790 gtk_file_chooser_impl_default_remove_shortcut_folder (GtkFileChooser *chooser,
1791 const GtkFilePath *path,
1794 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1799 if (impl->num_shortcuts == 0)
1802 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
1803 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
1804 g_assert_not_reached ();
1806 for (i = 0; i < impl->num_shortcuts; i++)
1808 GtkFilePath *shortcut;
1810 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
1811 g_assert (shortcut != NULL);
1813 if (gtk_file_path_compare (shortcut, path) == 0)
1815 /* The other columns are freed by the GtkTreeStore */
1816 gtk_file_path_free (shortcut);
1817 gtk_tree_store_remove (impl->shortcuts_model, &iter);
1818 impl->num_shortcuts--;
1822 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1823 g_assert_not_reached ();
1829 GTK_FILE_CHOOSER_ERROR,
1830 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
1831 "shortcut %s does not exist",
1832 gtk_file_path_get_string (path));
1838 gtk_file_chooser_impl_default_list_shortcut_folders (GtkFileChooser *chooser)
1840 GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser);
1846 pos = shortcuts_get_pos_for_shortcut_folder (impl, 0);
1847 if (!gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (impl->shortcuts_model), &iter, NULL, pos))
1848 g_assert_not_reached ();
1852 for (i = 0; i < impl->num_shortcuts; i++)
1854 GtkFilePath *shortcut;
1856 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &shortcut, -1);
1857 g_assert (shortcut != NULL);
1859 list = g_slist_prepend (list, gtk_file_path_copy (shortcut));
1861 if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (impl->shortcuts_model), &iter))
1862 g_assert_not_reached ();
1865 return g_slist_reverse (list);
1869 set_current_filter (GtkFileChooserImplDefault *impl,
1870 GtkFileFilter *filter)
1872 if (impl->current_filter != filter)
1874 int menu_item_index;
1876 /* If we have filters, new filter must be one of them
1878 find_filter_menu_item (impl, filter, &menu_item_index);
1879 if (impl->filters && menu_item_index < 0)
1882 if (impl->current_filter)
1883 g_object_unref (impl->current_filter);
1884 impl->current_filter = filter;
1885 if (impl->current_filter)
1887 g_object_ref (impl->current_filter);
1888 gtk_object_sink (GTK_OBJECT (filter));
1892 gtk_option_menu_set_history (GTK_OPTION_MENU (impl->filter_option_menu),
1895 install_list_model_filter (impl);
1897 g_object_notify (G_OBJECT (impl), "filter");
1902 open_and_close (GtkTreeView *tree_view,
1903 GtkTreePath *target_path)
1905 GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
1909 path = gtk_tree_path_new ();
1910 gtk_tree_path_append_index (path, 0);
1912 gtk_tree_model_get_iter (model, &iter, path);
1916 if (gtk_tree_path_is_ancestor (path, target_path) ||
1917 gtk_tree_path_compare (path, target_path) == 0)
1919 GtkTreeIter child_iter;
1920 gtk_tree_view_expand_row (tree_view, path, FALSE);
1921 if (gtk_tree_model_iter_children (model, &child_iter, &iter))
1924 gtk_tree_path_down (path);
1929 gtk_tree_view_collapse_row (tree_view, path);
1933 GtkTreeIter parent_iter;
1934 GtkTreeIter next_iter;
1937 if (gtk_tree_model_iter_next (model, &next_iter))
1940 gtk_tree_path_next (path);
1944 if (!gtk_tree_model_iter_parent (model, &parent_iter, &iter))
1948 gtk_tree_path_up (path);
1955 gtk_tree_path_free (path);
1959 filter_option_menu_changed (GtkOptionMenu *option_menu,
1960 GtkFileChooserImplDefault *impl)
1962 gint new_index = gtk_option_menu_get_history (GTK_OPTION_MENU (option_menu));
1963 GtkFileFilter *new_filter = g_slist_nth_data (impl->filters, new_index);
1965 set_current_filter (impl, new_filter);
1969 check_preview_change (GtkFileChooserImplDefault *impl)
1971 const GtkFilePath *new_path = NULL;
1973 /* Fixing preview for multiple selection involves getting the full
1974 * selection and diffing to find out what the most recently selected
1975 * file is; there is logic in GtkFileSelection that probably can
1976 * be copied. update_chooser_entry() is similar.
1978 if (impl->sort_model && !impl->select_multiple)
1980 GtkTreeSelection *selection;
1983 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list));
1984 if (gtk_tree_selection_get_selected (selection, NULL, &iter))
1986 GtkTreeIter child_iter;
1988 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
1989 &child_iter, &iter);
1991 new_path = _gtk_file_system_model_get_path (impl->list_model, &child_iter);
1995 if (new_path != impl->preview_path &&
1996 !(new_path && impl->preview_path &&
1997 gtk_file_path_compare (new_path, impl->preview_path) == 0))
1999 if (impl->preview_path)
2000 gtk_file_path_free (impl->preview_path);
2003 impl->preview_path = gtk_file_path_copy (new_path);
2005 impl->preview_path = NULL;
2007 g_signal_emit_by_name (impl, "update-preview");
2012 tree_selection_changed (GtkTreeSelection *selection,
2013 GtkFileChooserImplDefault *impl)
2016 const GtkFilePath *file_path;
2019 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2022 file_path = _gtk_file_system_model_get_path (impl->tree_model, &iter);
2023 if (impl->current_folder && gtk_file_path_compare (file_path, impl->current_folder) == 0)
2026 if (impl->current_folder)
2027 gtk_file_path_free (impl->current_folder);
2028 impl->current_folder = gtk_file_path_copy (file_path);
2029 _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->entry), file_path);
2031 /* Close the tree up to only the parents of the newly selected
2032 * node and it's immediate children are visible.
2034 path = gtk_tree_model_get_path (GTK_TREE_MODEL (impl->tree_model), &iter);
2035 open_and_close (GTK_TREE_VIEW (impl->tree), path);
2036 gtk_tree_path_free (path);
2038 /* Create the new list model */
2039 set_list_model (impl);
2041 shortcuts_select_folder (impl);
2043 g_signal_emit_by_name (impl, "current-folder-changed", 0);
2045 update_chooser_entry (impl);
2046 check_preview_change (impl);
2047 bookmarks_check_add_sensitivity (impl);
2049 g_signal_emit_by_name (impl, "selection-changed", 0);
2052 /* Callback used when the selection in the shortcuts list changes */
2054 shortcuts_selection_changed (GtkTreeSelection *selection,
2055 GtkFileChooserImplDefault *impl)
2060 if (impl->changing_folder)
2063 bookmarks_check_remove_sensitivity (impl);
2065 /* Set the current folder */
2067 if (!gtk_tree_selection_get_selected (selection, NULL, &iter))
2070 gtk_tree_model_get (GTK_TREE_MODEL (impl->shortcuts_model), &iter, SHORTCUTS_COL_PATH, &path, -1);
2074 /* We are on the bookmarks separator node, so unselect it */
2075 shortcuts_select_folder (impl);
2076 /* FIXME: how to make this row unselectable? */
2080 impl->changing_folder = TRUE;
2081 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), path);
2082 impl->changing_folder = FALSE;
2086 list_selection_changed (GtkTreeSelection *selection,
2087 GtkFileChooserImplDefault *impl)
2089 update_chooser_entry (impl);
2090 check_preview_change (impl);
2092 g_signal_emit_by_name (impl, "selection-changed", 0);
2095 /* Callback used when a row in the file list is activated */
2097 list_row_activated (GtkTreeView *tree_view,
2099 GtkTreeViewColumn *column,
2100 GtkFileChooserImplDefault *impl)
2102 GtkTreeIter iter, child_iter;
2103 const GtkFileInfo *info;
2105 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (impl->sort_model), &iter, path))
2108 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, &child_iter, &iter);
2110 info = _gtk_file_system_model_get_info (impl->list_model, &child_iter);
2112 if (gtk_file_info_get_is_folder (info))
2114 const GtkFilePath *file_path;
2116 file_path = _gtk_file_system_model_get_path (impl->list_model, &child_iter);
2117 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), file_path);
2122 g_signal_emit_by_name (impl, "file-activated");
2126 entry_activate (GtkEntry *entry,
2127 GtkFileChooserImplDefault *impl)
2129 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
2130 const GtkFilePath *folder_path = _gtk_file_chooser_entry_get_current_folder (chooser_entry);
2131 const gchar *file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry);
2132 GtkFilePath *new_folder = NULL;
2134 /* If the file part is non-empty, we need to figure out if it
2135 * refers to a folder within folder. We could optimize the case
2136 * here where the folder is already loaded for one of our tree models.
2138 if (file_part[0] == '\0' && gtk_file_path_compare (impl->current_folder, folder_path) != 0)
2139 new_folder = gtk_file_path_copy (folder_path);
2142 GtkFileFolder *folder = NULL;
2143 GtkFilePath *subfolder_path = NULL;
2144 GtkFileInfo *info = NULL;
2146 folder = gtk_file_system_get_folder (impl->file_system,
2148 GTK_FILE_INFO_IS_FOLDER,
2149 NULL); /* NULL-GError */
2152 subfolder_path = gtk_file_system_make_path (impl->file_system,
2155 NULL); /* NULL-GError */
2158 info = gtk_file_folder_get_info (folder,
2160 NULL); /* NULL-GError */
2162 if (info && gtk_file_info_get_is_folder (info))
2163 new_folder = gtk_file_path_copy (subfolder_path);
2166 g_object_unref (folder);
2169 gtk_file_path_free (subfolder_path);
2172 gtk_file_info_free (info);
2177 g_signal_stop_emission_by_name (entry, "activate");
2179 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (impl), new_folder);
2180 _gtk_file_chooser_entry_set_file_part (chooser_entry, "");
2182 gtk_file_path_free (new_folder);
2186 static const GtkFileInfo *
2187 get_list_file_info (GtkFileChooserImplDefault *impl,
2190 GtkTreeIter child_iter;
2192 gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model,
2196 return _gtk_file_system_model_get_info (impl->tree_model, &child_iter);
2200 tree_name_data_func (GtkTreeViewColumn *tree_column,
2201 GtkCellRenderer *cell,
2202 GtkTreeModel *tree_model,
2206 GtkFileChooserImplDefault *impl = data;
2207 const GtkFileInfo *info = _gtk_file_system_model_get_info (impl->tree_model, iter);
2212 "text", gtk_file_info_get_display_name (info),
2218 list_icon_data_func (GtkTreeViewColumn *tree_column,
2219 GtkCellRenderer *cell,
2220 GtkTreeModel *tree_model,
2224 GtkFileChooserImplDefault *impl = data;
2225 const GtkFileInfo *info = get_list_file_info (impl, iter);
2229 GtkWidget *widget = GTK_TREE_VIEW_COLUMN (tree_column)->tree_view;
2230 GdkPixbuf *pixbuf = gtk_file_info_render_icon (info, widget, ICON_SIZE);
2237 g_object_unref (pixbuf);
2241 /* Sets a cellrenderer's text, making it bold if the GtkFileInfo is a folder */
2243 set_cell_text_bold_if_folder (const GtkFileInfo *info, GtkCellRenderer *cell, const char *text)
2247 "weight", gtk_file_info_get_is_folder (info) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
2252 list_name_data_func (GtkTreeViewColumn *tree_column,
2253 GtkCellRenderer *cell,
2254 GtkTreeModel *tree_model,
2258 GtkFileChooserImplDefault *impl = data;
2259 const GtkFileInfo *info = get_list_file_info (impl, iter);
2264 set_cell_text_bold_if_folder (info, cell, gtk_file_info_get_display_name (info));
2269 list_size_data_func (GtkTreeViewColumn *tree_column,
2270 GtkCellRenderer *cell,
2271 GtkTreeModel *tree_model,
2275 GtkFileChooserImplDefault *impl = data;
2276 const GtkFileInfo *info = get_list_file_info (impl, iter);
2277 gint64 size = gtk_file_info_get_size (info);
2280 if (!info || gtk_file_info_get_is_folder (info))
2283 if (size < (gint64)1024)
2284 str = g_strdup_printf ("%d bytes", (gint)size);
2285 else if (size < (gint64)1024*1024)
2286 str = g_strdup_printf ("%.1f K", size / (1024.));
2287 else if (size < (gint64)1024*1024*1024)
2288 str = g_strdup_printf ("%.1f M", size / (1024.*1024.));
2290 str = g_strdup_printf ("%.1f G", size / (1024.*1024.*1024.));
2300 /* Tree column data callback for the file list; fetches the mtime of a file */
2302 list_mtime_data_func (GtkTreeViewColumn *tree_column,
2303 GtkCellRenderer *cell,
2304 GtkTreeModel *tree_model,
2308 GtkFileChooserImplDefault *impl;
2309 const GtkFileInfo *info;
2311 struct tm tm, now_tm;
2316 info = get_list_file_info (impl, iter);
2320 mtime = (time_t) gtk_file_info_get_modification_time (info);
2321 tm = *localtime (&mtime);
2324 now_tm = *localtime (&now);
2327 if (tm.tm_mday == now_tm.tm_mday
2328 && tm.tm_mon == now_tm.tm_mon
2329 && tm.tm_year == now_tm.tm_year)
2330 strcpy (buf, "Today");
2335 /* Days from last week */
2337 for (i = 1; i < 7; i++)
2342 then = now - i * 60 * 60 * 24;
2343 then_tm = *localtime (&then);
2345 if (tm.tm_mday == then_tm.tm_mday
2346 && tm.tm_mon == then_tm.tm_mon
2347 && tm.tm_year == then_tm.tm_year)
2350 strcpy (buf, "Yesterday");
2352 if (strftime (buf, sizeof (buf), "%A", &tm) == 0)
2353 strcpy (buf, "Unknown");
2359 /* Any other date */
2363 if (strftime (buf, sizeof (buf), "%d/%b/%Y", &tm) == 0)
2364 strcpy (buf, "Unknown");
2368 set_cell_text_bold_if_folder (info, cell, buf);
2372 _gtk_file_chooser_impl_default_new (GtkFileSystem *file_system)
2374 return g_object_new (GTK_TYPE_FILE_CHOOSER_IMPL_DEFAULT,
2375 "file-system", file_system,