1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
3 /* GTK+: gtkfilechooserbutton.c
5 * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
32 #include "gtkbutton.h"
33 #include "gtkcelllayout.h"
34 #include "gtkcellrenderertext.h"
35 #include "gtkcellrendererpixbuf.h"
36 #include "gtkcombobox.h"
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
42 #include "gtkliststore.h"
44 #include "gtktreemodelfilter.h"
45 #include "gtkseparator.h"
46 #include "gtkfilechooserdialog.h"
47 #include "gtkfilechooserprivate.h"
48 #include "gtkfilechooserutils.h"
49 #include "gtkmarshalers.h"
51 #include "gtkfilechooserbutton.h"
53 #include "gtkorientable.h"
55 #include "gtktypebuiltins.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
61 * SECTION:gtkfilechooserbutton
62 * @Short_description: A button to launch a file selection dialog
63 * @Title: GtkFileChooserButton
64 * @See_also:#GtkFileChooserDialog
66 * The #GtkFileChooserButton is a widget that lets the user select a
67 * file. It implements the #GtkFileChooser interface. Visually, it is a
68 * file name with a button to bring up a #GtkFileChooserDialog.
69 * The user can then use that dialog to change the file associated with
70 * that button. This widget does not support setting the
71 * #GtkFileChooser:select-multiple property to %TRUE.
74 * <title>Create a button to let the user select a file in /etc</title>
79 * button = gtk_file_chooser_button_new (_("Select a file"),
80 * GTK_FILE_CHOOSER_ACTION_OPEN);
81 * gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
87 * The #GtkFileChooserButton supports the #GtkFileChooserAction<!-- -->s
88 * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
91 * The #GtkFileChooserButton will ellipsize the label,
92 * and thus will thus request little horizontal space. To give the button
93 * more space, you should call gtk_widget_get_preferred_size(),
94 * gtk_file_chooser_button_set_width_chars(), or pack the button in
95 * such a way that other interface elements give space to the widget.
100 /* **************** *
102 * **************** */
104 #define DEFAULT_TITLE N_("Select a File")
105 #define DESKTOP_DISPLAY_NAME N_("Desktop")
106 #define FALLBACK_DISPLAY_NAME N_("(None)")
107 #define FALLBACK_ICON_NAME "stock_unknown"
108 #define FALLBACK_ICON_SIZE 16
111 /* ********************** *
112 * Private Enumerations *
113 * ********************** */
133 /* TreeModel Columns */
145 /* TreeModel Row Types */
151 ROW_TYPE_BOOKMARK_SEPARATOR,
153 ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
154 ROW_TYPE_CURRENT_FOLDER,
155 ROW_TYPE_OTHER_SEPARATOR,
158 ROW_TYPE_INVALID = -1
163 /* ******************** *
164 * Private Structures *
165 * ******************** */
167 struct _GtkFileChooserButtonPrivate
173 GtkWidget *combo_box;
174 GtkCellRenderer *icon_cell;
175 GtkCellRenderer *name_cell;
178 GtkTreeModel *filter_model;
181 GFile *selection_while_inactive;
182 GFile *current_folder_while_inactive;
184 gulong combo_box_changed_id;
185 gulong dialog_file_activated_id;
186 gulong dialog_folder_changed_id;
187 gulong dialog_selection_changed_id;
188 gulong fs_volumes_changed_id;
189 gulong fs_bookmarks_changed_id;
191 GCancellable *dnd_select_folder_cancellable;
192 GCancellable *update_button_cancellable;
193 GSList *change_icon_theme_cancellables;
201 guint has_bookmark_separator : 1;
202 guint has_current_folder_separator : 1;
203 guint has_current_folder : 1;
204 guint has_other_separator : 1;
206 /* Used for hiding/showing the dialog when the button is hidden */
209 guint focus_on_click : 1;
224 /* ********************* *
225 * Function Prototypes *
226 * ********************* */
228 /* GtkFileChooserIface Functions */
229 static void gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
230 static gboolean gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
233 static GFile *gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser);
234 static gboolean gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
237 static void gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
239 static void gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser);
240 static GSList *gtk_file_chooser_button_get_files (GtkFileChooser *chooser);
241 static gboolean gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
244 static gboolean gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
248 /* GObject Functions */
249 static GObject *gtk_file_chooser_button_constructor (GType type,
251 GObjectConstructParam *params);
252 static void gtk_file_chooser_button_set_property (GObject *object,
256 static void gtk_file_chooser_button_get_property (GObject *object,
260 static void gtk_file_chooser_button_finalize (GObject *object);
262 /* GtkWidget Functions */
263 static void gtk_file_chooser_button_destroy (GtkWidget *widget);
264 static void gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
265 GdkDragContext *context,
268 GtkSelectionData *data,
271 static void gtk_file_chooser_button_show_all (GtkWidget *widget);
272 static void gtk_file_chooser_button_show (GtkWidget *widget);
273 static void gtk_file_chooser_button_hide (GtkWidget *widget);
274 static void gtk_file_chooser_button_map (GtkWidget *widget);
275 static gboolean gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
276 gboolean group_cycling);
277 static void gtk_file_chooser_button_style_updated (GtkWidget *widget);
278 static void gtk_file_chooser_button_screen_changed (GtkWidget *widget,
279 GdkScreen *old_screen);
281 /* Utility Functions */
282 static GtkIconTheme *get_icon_theme (GtkWidget *widget);
283 static void set_info_for_file_at_iter (GtkFileChooserButton *fs,
287 static gint model_get_type_position (GtkFileChooserButton *button,
289 static void model_free_row_data (GtkFileChooserButton *button,
291 static inline void model_add_special (GtkFileChooserButton *button);
292 static inline void model_add_other (GtkFileChooserButton *button);
293 static void model_add_volumes (GtkFileChooserButton *button,
295 static void model_add_bookmarks (GtkFileChooserButton *button,
297 static void model_update_current_folder (GtkFileChooserButton *button,
299 static void model_remove_rows (GtkFileChooserButton *button,
303 static gboolean filter_model_visible_func (GtkTreeModel *model,
307 static gboolean combo_box_row_separator_func (GtkTreeModel *model,
310 static void name_cell_data_func (GtkCellLayout *layout,
311 GtkCellRenderer *cell,
315 static void open_dialog (GtkFileChooserButton *button);
316 static void update_combo_box (GtkFileChooserButton *button);
317 static void update_label_and_image (GtkFileChooserButton *button);
319 /* Child Object Callbacks */
320 static void fs_volumes_changed_cb (GtkFileSystem *fs,
322 static void fs_bookmarks_changed_cb (GtkFileSystem *fs,
325 static void combo_box_changed_cb (GtkComboBox *combo_box,
328 static void button_clicked_cb (GtkButton *real_button,
331 static void dialog_update_preview_cb (GtkFileChooser *dialog,
333 static void dialog_selection_changed_cb (GtkFileChooser *dialog,
335 static void dialog_file_activated_cb (GtkFileChooser *dialog,
337 static void dialog_current_folder_changed_cb (GtkFileChooser *dialog,
339 static void dialog_notify_cb (GObject *dialog,
342 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
345 static void dialog_response_cb (GtkDialog *dialog,
349 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
351 /* ******************* *
352 * GType Declaration *
353 * ******************* */
355 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
356 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
360 /* ***************** *
362 * ***************** */
365 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
367 GObjectClass *gobject_class;
368 GtkWidgetClass *widget_class;
370 gobject_class = G_OBJECT_CLASS (class);
371 widget_class = GTK_WIDGET_CLASS (class);
373 gobject_class->constructor = gtk_file_chooser_button_constructor;
374 gobject_class->set_property = gtk_file_chooser_button_set_property;
375 gobject_class->get_property = gtk_file_chooser_button_get_property;
376 gobject_class->finalize = gtk_file_chooser_button_finalize;
378 widget_class->destroy = gtk_file_chooser_button_destroy;
379 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
380 widget_class->show_all = gtk_file_chooser_button_show_all;
381 widget_class->show = gtk_file_chooser_button_show;
382 widget_class->hide = gtk_file_chooser_button_hide;
383 widget_class->map = gtk_file_chooser_button_map;
384 widget_class->style_updated = gtk_file_chooser_button_style_updated;
385 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
386 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
389 * GtkFileChooserButton::file-set:
390 * @widget: the object which received the signal.
392 * The ::file-set signal is emitted when the user selects a file.
394 * Note that this signal is only emitted when the <emphasis>user</emphasis>
399 file_chooser_button_signals[FILE_SET] =
400 g_signal_new (I_("file-set"),
401 G_TYPE_FROM_CLASS (gobject_class),
403 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
405 _gtk_marshal_VOID__VOID,
409 * GtkFileChooserButton:dialog:
411 * Instance of the #GtkFileChooserDialog associated with the button.
415 g_object_class_install_property (gobject_class, PROP_DIALOG,
416 g_param_spec_object ("dialog",
418 P_("The file chooser dialog to use."),
419 GTK_TYPE_FILE_CHOOSER,
420 (GTK_PARAM_WRITABLE |
421 G_PARAM_CONSTRUCT_ONLY)));
424 * GtkFileChooserButton:focus-on-click:
426 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
431 g_object_class_install_property (gobject_class,
433 g_param_spec_boolean ("focus-on-click",
434 P_("Focus on click"),
435 P_("Whether the button grabs focus when it is clicked with the mouse"),
437 GTK_PARAM_READWRITE));
440 * GtkFileChooserButton:title:
442 * Title to put on the #GtkFileChooserDialog associated with the button.
446 g_object_class_install_property (gobject_class, PROP_TITLE,
447 g_param_spec_string ("title",
449 P_("The title of the file chooser dialog."),
451 GTK_PARAM_READWRITE));
454 * GtkFileChooserButton:width-chars:
456 * The width of the entry and label inside the button, in characters.
460 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
461 g_param_spec_int ("width-chars",
462 P_("Width In Characters"),
463 P_("The desired width of the button widget, in characters."),
465 GTK_PARAM_READWRITE));
467 _gtk_file_chooser_install_properties (gobject_class);
469 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
473 gtk_file_chooser_button_init (GtkFileChooserButton *button)
475 GtkFileChooserButtonPrivate *priv;
476 GtkWidget *box, *image, *sep;
477 GtkTargetList *target_list;
479 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
480 GTK_TYPE_FILE_CHOOSER_BUTTON,
481 GtkFileChooserButtonPrivate);
483 priv->icon_size = FALLBACK_ICON_SIZE;
484 priv->focus_on_click = TRUE;
486 gtk_widget_push_composite_child ();
489 priv->button = gtk_button_new ();
490 g_signal_connect (priv->button, "clicked",
491 G_CALLBACK (button_clicked_cb), button);
492 gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
493 gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
494 gtk_widget_show (priv->button);
496 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
497 gtk_container_add (GTK_CONTAINER (priv->button), box);
498 gtk_widget_show (box);
500 priv->image = gtk_image_new ();
501 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
502 gtk_widget_show (priv->image);
504 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
505 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
506 gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
507 gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
508 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
509 //gtk_container_add (GTK_CONTAINER (box), priv->label);
510 gtk_widget_show (priv->label);
512 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
513 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
514 gtk_widget_show (sep);
516 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
517 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
518 gtk_widget_show (image);
521 /* Keep in sync with columns enum, line 88 */
523 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
524 GDK_TYPE_PIXBUF, /* Icon */
525 G_TYPE_STRING, /* Display Name */
526 G_TYPE_CHAR, /* Row Type */
527 G_TYPE_POINTER /* Volume || Path */,
528 G_TYPE_BOOLEAN /* Is Folder? */,
529 G_TYPE_POINTER /* cancellable */));
531 priv->combo_box = gtk_combo_box_new ();
532 priv->combo_box_changed_id =
533 g_signal_connect (priv->combo_box, "changed",
534 G_CALLBACK (combo_box_changed_cb), button);
535 gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
536 gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
538 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
539 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
540 priv->icon_cell, FALSE);
541 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
542 priv->icon_cell, "pixbuf", ICON_COLUMN);
544 priv->name_cell = gtk_cell_renderer_text_new ();
545 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
546 priv->name_cell, TRUE);
547 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
548 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
549 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
550 priv->name_cell, name_cell_data_func,
553 gtk_widget_pop_composite_child ();
556 gtk_drag_dest_set (GTK_WIDGET (button),
557 (GTK_DEST_DEFAULT_ALL),
560 target_list = gtk_target_list_new (NULL, 0);
561 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
562 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
563 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
564 gtk_target_list_unref (target_list);
568 /* ******************************* *
569 * GtkFileChooserIface Functions *
570 * ******************************* */
572 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
574 _gtk_file_chooser_delegate_iface_init (iface);
576 iface->set_current_folder = gtk_file_chooser_button_set_current_folder;
577 iface->get_current_folder = gtk_file_chooser_button_get_current_folder;
578 iface->select_file = gtk_file_chooser_button_select_file;
579 iface->unselect_file = gtk_file_chooser_button_unselect_file;
580 iface->unselect_all = gtk_file_chooser_button_unselect_all;
581 iface->get_files = gtk_file_chooser_button_get_files;
582 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
583 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
587 gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
591 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
592 GtkFileChooserButtonPrivate *priv = button->priv;
593 GtkFileChooser *delegate;
595 delegate = g_object_get_qdata (G_OBJECT (chooser),
596 GTK_FILE_CHOOSER_DELEGATE_QUARK);
599 return gtk_file_chooser_set_current_folder_file (delegate, file, error);
602 if (priv->current_folder_while_inactive)
603 g_object_unref (priv->current_folder_while_inactive);
605 priv->current_folder_while_inactive = g_object_ref (file);
607 g_signal_emit_by_name (button, "current-folder-changed");
614 gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser)
616 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
617 GtkFileChooserButtonPrivate *priv = button->priv;
618 GtkFileChooser *delegate;
620 delegate = g_object_get_qdata (G_OBJECT (chooser),
621 GTK_FILE_CHOOSER_DELEGATE_QUARK);
624 return gtk_file_chooser_get_current_folder_file (delegate);
627 if (priv->current_folder_while_inactive)
628 return g_object_ref (priv->current_folder_while_inactive);
635 gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
639 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
640 GtkFileChooserButtonPrivate *priv = button->priv;
641 GtkFileChooser *delegate;
643 delegate = g_object_get_qdata (G_OBJECT (chooser),
644 GTK_FILE_CHOOSER_DELEGATE_QUARK);
647 return gtk_file_chooser_select_file (delegate, file, error);
650 if (priv->selection_while_inactive)
651 g_object_unref (priv->selection_while_inactive);
653 priv->selection_while_inactive = g_object_ref (file);
655 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
662 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
665 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
666 GtkFileChooserButtonPrivate *priv = button->priv;
667 GtkFileChooser *delegate;
669 delegate = g_object_get_qdata (G_OBJECT (chooser),
670 GTK_FILE_CHOOSER_DELEGATE_QUARK);
673 gtk_file_chooser_unselect_file (delegate, file);
676 if (g_file_equal (priv->selection_while_inactive, file))
678 if (priv->selection_while_inactive)
680 g_object_unref (priv->selection_while_inactive);
681 priv->selection_while_inactive = NULL;
684 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
690 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
692 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
693 GtkFileChooserButtonPrivate *priv = button->priv;
694 GtkFileChooser *delegate;
696 delegate = g_object_get_qdata (G_OBJECT (chooser),
697 GTK_FILE_CHOOSER_DELEGATE_QUARK);
700 gtk_file_chooser_unselect_all (delegate);
703 if (priv->selection_while_inactive)
705 g_object_unref (priv->selection_while_inactive);
706 priv->selection_while_inactive = NULL;
709 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
714 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
716 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
717 GtkFileChooserButtonPrivate *priv = button->priv;
718 GtkFileChooser *delegate;
720 delegate = g_object_get_qdata (G_OBJECT (chooser),
721 GTK_FILE_CHOOSER_DELEGATE_QUARK);
724 return gtk_file_chooser_get_files (delegate);
731 if (priv->selection_while_inactive)
732 result = g_slist_prepend (NULL, g_object_ref (priv->selection_while_inactive));
733 else if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
735 /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
736 * the current folder, since that is what GtkFileChooserDefault would do.
738 if (priv->current_folder_while_inactive)
739 result = g_slist_prepend (NULL, g_object_ref (priv->current_folder_while_inactive));
747 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
751 GtkFileChooser *delegate;
754 delegate = g_object_get_qdata (G_OBJECT (chooser),
755 GTK_FILE_CHOOSER_DELEGATE_QUARK);
756 retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
760 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
761 GtkFileChooserButtonPrivate *priv = button->priv;
765 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
766 pos += priv->n_shortcuts;
768 gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
769 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
771 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
772 TYPE_COLUMN, ROW_TYPE_SHORTCUT,
773 DATA_COLUMN, g_object_ref (file),
774 IS_FOLDER_COLUMN, FALSE,
776 set_info_for_file_at_iter (button, file, &iter);
779 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
786 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
790 GtkFileChooser *delegate;
793 delegate = g_object_get_qdata (G_OBJECT (chooser),
794 GTK_FILE_CHOOSER_DELEGATE_QUARK);
796 retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
800 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
801 GtkFileChooserButtonPrivate *priv = button->priv;
806 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
807 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
813 gtk_tree_model_get (priv->model, &iter,
818 if (type == ROW_TYPE_SHORTCUT &&
819 data && g_file_equal (data, file))
821 model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
822 gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
824 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
825 update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
829 while (type == ROW_TYPE_SHORTCUT &&
830 gtk_tree_model_iter_next (priv->model, &iter));
837 /* ******************* *
838 * GObject Functions *
839 * ******************* */
842 gtk_file_chooser_button_constructor (GType type,
844 GObjectConstructParam *params)
847 GtkFileChooserButton *button;
848 GtkFileChooserButtonPrivate *priv;
851 object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
854 button = GTK_FILE_CHOOSER_BUTTON (object);
859 priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
860 GTK_FILE_CHOOSER_ACTION_OPEN,
867 gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
868 GTK_RESPONSE_ACCEPT);
869 gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
874 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
876 else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
878 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
881 g_signal_connect (priv->dialog, "delete-event",
882 G_CALLBACK (dialog_delete_event_cb), object);
883 g_signal_connect (priv->dialog, "response",
884 G_CALLBACK (dialog_response_cb), object);
886 /* This is used, instead of the standard delegate, to ensure that signals are only
887 * delegated when the OK button is pressed. */
888 g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
889 priv->dialog_folder_changed_id =
890 g_signal_connect (priv->dialog, "current-folder-changed",
891 G_CALLBACK (dialog_current_folder_changed_cb), object);
892 priv->dialog_file_activated_id =
893 g_signal_connect (priv->dialog, "file-activated",
894 G_CALLBACK (dialog_file_activated_cb), object);
895 priv->dialog_selection_changed_id =
896 g_signal_connect (priv->dialog, "selection-changed",
897 G_CALLBACK (dialog_selection_changed_cb), object);
898 g_signal_connect (priv->dialog, "update-preview",
899 G_CALLBACK (dialog_update_preview_cb), object);
900 g_signal_connect (priv->dialog, "notify",
901 G_CALLBACK (dialog_notify_cb), object);
902 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
903 (gpointer) (&priv->dialog));
906 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
908 model_add_special (button);
910 list = _gtk_file_system_list_volumes (priv->fs);
911 model_add_volumes (button, list);
914 list = _gtk_file_system_list_bookmarks (priv->fs);
915 model_add_bookmarks (button, list);
916 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
919 model_add_other (button);
921 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
922 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
923 filter_model_visible_func,
926 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
927 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
928 combo_box_row_separator_func,
931 /* set up the action for a user-provided dialog, this also updates
932 * the label, image and combobox
934 g_object_set (object,
935 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
938 priv->fs_volumes_changed_id =
939 g_signal_connect (priv->fs, "volumes-changed",
940 G_CALLBACK (fs_volumes_changed_cb), object);
941 priv->fs_bookmarks_changed_id =
942 g_signal_connect (priv->fs, "bookmarks-changed",
943 G_CALLBACK (fs_bookmarks_changed_cb), object);
949 gtk_file_chooser_button_set_property (GObject *object,
954 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
955 GtkFileChooserButtonPrivate *priv = button->priv;
961 priv->dialog = g_value_get_object (value);
963 case PROP_FOCUS_ON_CLICK:
964 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
966 case PROP_WIDTH_CHARS:
967 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
968 g_value_get_int (value));
970 case GTK_FILE_CHOOSER_PROP_ACTION:
971 switch (g_value_get_enum (value))
973 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
974 case GTK_FILE_CHOOSER_ACTION_SAVE:
979 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
980 eval = g_enum_get_value (eclass, g_value_get_enum (value));
981 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
982 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
984 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
989 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
990 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
991 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
993 switch (g_value_get_enum (value))
995 case GTK_FILE_CHOOSER_ACTION_OPEN:
996 gtk_widget_hide (priv->combo_box);
997 gtk_widget_show (priv->button);
999 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1000 gtk_widget_hide (priv->button);
1001 gtk_widget_show (priv->combo_box);
1004 g_assert_not_reached ();
1010 case GTK_FILE_CHOOSER_PROP_FILTER:
1011 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1012 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1013 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1014 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1015 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1016 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1017 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1018 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1021 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1022 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1023 fs_volumes_changed_cb (priv->fs, button);
1024 fs_bookmarks_changed_cb (priv->fs, button);
1027 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1028 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
1029 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
1032 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1038 gtk_file_chooser_button_get_property (GObject *object,
1043 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1044 GtkFileChooserButtonPrivate *priv = button->priv;
1048 case PROP_WIDTH_CHARS:
1049 g_value_set_int (value,
1050 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
1052 case PROP_FOCUS_ON_CLICK:
1053 g_value_set_boolean (value,
1054 gtk_file_chooser_button_get_focus_on_click (button));
1058 case GTK_FILE_CHOOSER_PROP_ACTION:
1059 case GTK_FILE_CHOOSER_PROP_FILTER:
1060 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1061 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1062 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1063 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1064 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1065 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1066 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1067 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1068 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1069 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1073 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1079 gtk_file_chooser_button_finalize (GObject *object)
1081 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1082 GtkFileChooserButtonPrivate *priv = button->priv;
1084 if (priv->selection_while_inactive)
1085 g_object_unref (priv->selection_while_inactive);
1087 if (priv->current_folder_while_inactive)
1088 g_object_unref (priv->current_folder_while_inactive);
1090 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1093 /* ********************* *
1094 * GtkWidget Functions *
1095 * ********************* */
1098 gtk_file_chooser_button_destroy (GtkWidget *widget)
1100 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1101 GtkFileChooserButtonPrivate *priv = button->priv;
1105 if (priv->dialog != NULL)
1107 gtk_widget_destroy (priv->dialog);
1108 priv->dialog = NULL;
1111 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1113 model_free_row_data (button, &iter);
1115 while (gtk_tree_model_iter_next (priv->model, &iter));
1117 if (priv->dnd_select_folder_cancellable)
1119 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1120 priv->dnd_select_folder_cancellable = NULL;
1123 if (priv->update_button_cancellable)
1125 g_cancellable_cancel (priv->update_button_cancellable);
1126 priv->update_button_cancellable = NULL;
1129 if (priv->change_icon_theme_cancellables)
1131 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1133 GCancellable *cancellable = G_CANCELLABLE (l->data);
1134 g_cancellable_cancel (cancellable);
1136 g_slist_free (priv->change_icon_theme_cancellables);
1137 priv->change_icon_theme_cancellables = NULL;
1142 g_object_unref (priv->model);
1146 if (priv->filter_model)
1148 g_object_unref (priv->filter_model);
1149 priv->filter_model = NULL;
1154 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1155 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1156 g_object_unref (priv->fs);
1160 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1163 struct DndSelectFolderData
1165 GtkFileSystem *file_system;
1166 GtkFileChooserButton *button;
1167 GtkFileChooserAction action;
1175 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1177 const GError *error,
1180 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1181 struct DndSelectFolderData *data = user_data;
1183 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1185 g_object_unref (data->button);
1186 g_object_unref (data->file);
1187 g_strfreev (data->uris);
1190 g_object_unref (cancellable);
1194 data->button->priv->dnd_select_folder_cancellable = NULL;
1196 if (!cancelled && !error && info != NULL)
1200 is_folder = _gtk_file_info_consider_as_directory (info);
1203 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1204 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1205 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1209 data->selected = FALSE;
1211 if (data->selected || data->uris[++data->i] == NULL)
1213 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1215 g_object_unref (data->button);
1216 g_object_unref (data->file);
1217 g_strfreev (data->uris);
1220 g_object_unref (cancellable);
1225 g_object_unref (data->file);
1227 data->file = g_file_new_for_uri (data->uris[data->i]);
1229 data->button->priv->dnd_select_folder_cancellable =
1230 _gtk_file_system_get_info (data->file_system, data->file,
1232 dnd_select_folder_get_info_cb, user_data);
1234 g_object_unref (cancellable);
1238 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1239 GdkDragContext *context,
1242 GtkSelectionData *data,
1246 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1247 GtkFileChooserButtonPrivate *priv = button->priv;
1251 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1252 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1258 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1266 struct DndSelectFolderData *info;
1268 uris = gtk_selection_data_get_uris (data);
1273 info = g_new0 (struct DndSelectFolderData, 1);
1274 info->button = g_object_ref (button);
1277 info->selected = FALSE;
1278 info->file_system = priv->fs;
1279 g_object_get (priv->dialog, "action", &info->action, NULL);
1281 info->file = g_file_new_for_uri (info->uris[info->i]);
1283 if (priv->dnd_select_folder_cancellable)
1284 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1286 priv->dnd_select_folder_cancellable =
1287 _gtk_file_system_get_info (priv->fs, info->file,
1289 dnd_select_folder_get_info_cb, info);
1294 text = (char*) gtk_selection_data_get_text (data);
1295 file = g_file_new_for_uri (text);
1296 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1298 g_object_unref (file);
1300 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1307 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1311 gtk_file_chooser_button_show_all (GtkWidget *widget)
1313 gtk_widget_show (widget);
1317 gtk_file_chooser_button_show (GtkWidget *widget)
1319 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1320 GtkFileChooserButtonPrivate *priv = button->priv;
1322 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1323 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1326 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1330 gtk_file_chooser_button_hide (GtkWidget *widget)
1332 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1333 GtkFileChooserButtonPrivate *priv = button->priv;
1335 gtk_widget_hide (priv->dialog);
1337 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1338 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1342 gtk_file_chooser_button_map (GtkWidget *widget)
1344 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1348 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1349 gboolean group_cycling)
1351 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1352 GtkFileChooserButtonPrivate *priv = button->priv;
1354 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1356 case GTK_FILE_CHOOSER_ACTION_OPEN:
1357 gtk_widget_grab_focus (priv->button);
1359 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1360 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1363 g_assert_not_reached ();
1370 /* Changes the icons wherever it is needed */
1371 struct ChangeIconThemeData
1373 GtkFileChooserButton *button;
1374 GtkTreeRowReference *row_ref;
1378 change_icon_theme_get_info_cb (GCancellable *cancellable,
1380 const GError *error,
1383 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1385 struct ChangeIconThemeData *data = user_data;
1387 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1390 data->button->priv->change_icon_theme_cancellables =
1391 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1393 if (cancelled || error)
1396 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1404 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1406 path = gtk_tree_row_reference_get_path (data->row_ref);
1409 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1410 gtk_tree_path_free (path);
1412 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1413 ICON_COLUMN, pixbuf,
1416 g_object_set (data->button->priv->icon_cell,
1420 g_object_unref (pixbuf);
1424 g_object_unref (data->button);
1425 gtk_tree_row_reference_free (data->row_ref);
1428 g_object_unref (cancellable);
1432 change_icon_theme (GtkFileChooserButton *button)
1434 GtkFileChooserButtonPrivate *priv = button->priv;
1435 GtkSettings *settings;
1436 GtkIconTheme *theme;
1439 gint width = 0, height = 0;
1441 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1443 GCancellable *cancellable = G_CANCELLABLE (l->data);
1444 g_cancellable_cancel (cancellable);
1446 g_slist_free (button->priv->change_icon_theme_cancellables);
1447 button->priv->change_icon_theme_cancellables = NULL;
1449 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1451 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1453 priv->icon_size = MAX (width, height);
1455 priv->icon_size = FALLBACK_ICON_SIZE;
1457 update_label_and_image (button);
1459 gtk_tree_model_get_iter_first (priv->model, &iter);
1461 theme = get_icon_theme (GTK_WIDGET (button));
1469 type = ROW_TYPE_INVALID;
1470 gtk_tree_model_get (priv->model, &iter,
1477 case ROW_TYPE_SPECIAL:
1478 case ROW_TYPE_SHORTCUT:
1479 case ROW_TYPE_BOOKMARK:
1480 case ROW_TYPE_CURRENT_FOLDER:
1483 if (g_file_is_native (G_FILE (data)))
1486 GCancellable *cancellable;
1487 struct ChangeIconThemeData *info;
1489 info = g_new0 (struct ChangeIconThemeData, 1);
1490 info->button = g_object_ref (button);
1491 path = gtk_tree_model_get_path (priv->model, &iter);
1492 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1493 gtk_tree_path_free (path);
1496 _gtk_file_system_get_info (priv->fs, data,
1498 change_icon_theme_get_info_cb,
1500 button->priv->change_icon_theme_cancellables =
1501 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1505 /* Don't call get_info for remote paths to avoid latency and
1507 * If we switch to a better bookmarks file format (XBEL), we
1508 * should use mime info to get a better icon.
1510 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1511 priv->icon_size, 0, NULL);
1514 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1515 priv->icon_size, 0, NULL);
1517 case ROW_TYPE_VOLUME:
1519 pixbuf = _gtk_file_system_volume_render_icon (data,
1520 GTK_WIDGET (button),
1524 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1525 priv->icon_size, 0, NULL);
1533 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1535 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1536 ICON_COLUMN, pixbuf,
1540 g_object_unref (pixbuf);
1542 while (gtk_tree_model_iter_next (priv->model, &iter));
1544 g_object_set (button->priv->icon_cell,
1550 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1552 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1554 if (gtk_widget_has_screen (widget))
1555 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1559 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1560 GdkScreen *old_screen)
1562 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1563 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1566 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1570 /* ******************* *
1571 * Utility Functions *
1572 * ******************* */
1575 static GtkIconTheme *
1576 get_icon_theme (GtkWidget *widget)
1578 if (gtk_widget_has_screen (widget))
1579 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1581 return gtk_icon_theme_get_default ();
1585 struct SetDisplayNameData
1587 GtkFileChooserButton *button;
1589 GtkTreeRowReference *row_ref;
1593 set_info_get_info_cb (GCancellable *cancellable,
1595 const GError *error,
1596 gpointer callback_data)
1598 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1602 GCancellable *model_cancellable = NULL;
1603 struct SetDisplayNameData *data = callback_data;
1606 if (!data->button->priv->model)
1607 /* button got destroyed */
1610 path = gtk_tree_row_reference_get_path (data->row_ref);
1612 /* Cancellable doesn't exist anymore in the model */
1615 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1616 gtk_tree_path_free (path);
1618 /* Validate the cancellable */
1619 gtk_tree_model_get (data->button->priv->model, &iter,
1620 CANCELLABLE_COLUMN, &model_cancellable,
1622 if (cancellable != model_cancellable)
1625 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1626 CANCELLABLE_COLUMN, NULL,
1629 if (cancelled || error)
1630 /* There was an error, leave the fallback name in there */
1633 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1636 data->label = g_strdup (g_file_info_get_display_name (info));
1638 is_folder = _gtk_file_info_consider_as_directory (info);
1640 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1641 ICON_COLUMN, pixbuf,
1642 DISPLAY_NAME_COLUMN, data->label,
1643 IS_FOLDER_COLUMN, is_folder,
1647 g_object_unref (pixbuf);
1650 g_object_unref (data->button);
1651 g_free (data->label);
1652 gtk_tree_row_reference_free (data->row_ref);
1655 if (model_cancellable)
1656 g_object_unref (model_cancellable);
1660 set_info_for_file_at_iter (GtkFileChooserButton *button,
1664 struct SetDisplayNameData *data;
1665 GtkTreePath *tree_path;
1666 GCancellable *cancellable;
1668 data = g_new0 (struct SetDisplayNameData, 1);
1669 data->button = g_object_ref (button);
1670 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1672 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1673 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1674 gtk_tree_path_free (tree_path);
1676 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1677 "standard::type,standard::icon,standard::display-name",
1678 set_info_get_info_cb, data);
1680 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1681 CANCELLABLE_COLUMN, cancellable,
1685 /* Shortcuts Model */
1687 model_get_type_position (GtkFileChooserButton *button,
1692 if (row_type == ROW_TYPE_SPECIAL)
1695 retval += button->priv->n_special;
1697 if (row_type == ROW_TYPE_VOLUME)
1700 retval += button->priv->n_volumes;
1702 if (row_type == ROW_TYPE_SHORTCUT)
1705 retval += button->priv->n_shortcuts;
1707 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1710 retval += button->priv->has_bookmark_separator;
1712 if (row_type == ROW_TYPE_BOOKMARK)
1715 retval += button->priv->n_bookmarks;
1717 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1720 retval += button->priv->has_current_folder_separator;
1722 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1725 retval += button->priv->has_current_folder;
1727 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1730 retval += button->priv->has_other_separator;
1732 if (row_type == ROW_TYPE_OTHER)
1735 g_assert_not_reached ();
1740 model_free_row_data (GtkFileChooserButton *button,
1745 GCancellable *cancellable;
1747 gtk_tree_model_get (button->priv->model, iter,
1750 CANCELLABLE_COLUMN, &cancellable,
1755 g_cancellable_cancel (cancellable);
1756 g_object_unref (cancellable);
1761 case ROW_TYPE_SPECIAL:
1762 case ROW_TYPE_SHORTCUT:
1763 case ROW_TYPE_BOOKMARK:
1764 case ROW_TYPE_CURRENT_FOLDER:
1765 g_object_unref (data);
1767 case ROW_TYPE_VOLUME:
1768 _gtk_file_system_volume_unref (data);
1776 model_add_special_get_info_cb (GCancellable *cancellable,
1778 const GError *error,
1781 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1785 GCancellable *model_cancellable = NULL;
1786 struct ChangeIconThemeData *data = user_data;
1789 if (!data->button->priv->model)
1790 /* button got destroyed */
1793 path = gtk_tree_row_reference_get_path (data->row_ref);
1795 /* Cancellable doesn't exist anymore in the model */
1798 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1799 gtk_tree_path_free (path);
1801 gtk_tree_model_get (data->button->priv->model, &iter,
1802 CANCELLABLE_COLUMN, &model_cancellable,
1804 if (cancellable != model_cancellable)
1807 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1808 CANCELLABLE_COLUMN, NULL,
1811 if (cancelled || error)
1814 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1818 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1819 ICON_COLUMN, pixbuf,
1821 g_object_unref (pixbuf);
1824 gtk_tree_model_get (data->button->priv->model, &iter,
1825 DISPLAY_NAME_COLUMN, &name,
1828 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1829 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1834 g_object_unref (data->button);
1835 gtk_tree_row_reference_free (data->row_ref);
1838 if (model_cancellable)
1839 g_object_unref (model_cancellable);
1843 model_add_special (GtkFileChooserButton *button)
1845 const gchar *homedir;
1846 const gchar *desktopdir;
1847 GtkListStore *store;
1852 store = GTK_LIST_STORE (button->priv->model);
1853 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1855 homedir = g_get_home_dir ();
1859 GtkTreePath *tree_path;
1860 GCancellable *cancellable;
1861 struct ChangeIconThemeData *info;
1863 file = g_file_new_for_path (homedir);
1864 gtk_list_store_insert (store, &iter, pos);
1867 info = g_new0 (struct ChangeIconThemeData, 1);
1868 info->button = g_object_ref (button);
1869 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1870 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1872 gtk_tree_path_free (tree_path);
1874 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1875 "standard::icon,standard::display-name",
1876 model_add_special_get_info_cb, info);
1878 gtk_list_store_set (store, &iter,
1880 DISPLAY_NAME_COLUMN, NULL,
1881 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1883 IS_FOLDER_COLUMN, TRUE,
1884 CANCELLABLE_COLUMN, cancellable,
1887 button->priv->n_special++;
1890 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1892 /* "To disable a directory, point it to the homedir."
1893 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1895 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1897 GtkTreePath *tree_path;
1898 GCancellable *cancellable;
1899 struct ChangeIconThemeData *info;
1901 file = g_file_new_for_path (desktopdir);
1902 gtk_list_store_insert (store, &iter, pos);
1905 info = g_new0 (struct ChangeIconThemeData, 1);
1906 info->button = g_object_ref (button);
1907 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1908 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1910 gtk_tree_path_free (tree_path);
1912 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1913 "standard::icon,standard::display-name",
1914 model_add_special_get_info_cb, info);
1916 gtk_list_store_set (store, &iter,
1917 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1919 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1921 IS_FOLDER_COLUMN, TRUE,
1922 CANCELLABLE_COLUMN, cancellable,
1925 button->priv->n_special++;
1930 model_add_volumes (GtkFileChooserButton *button,
1933 GtkListStore *store;
1935 gboolean local_only;
1941 store = GTK_LIST_STORE (button->priv->model);
1942 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1943 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1945 for (l = volumes; l; l = l->next)
1947 GtkFileSystemVolume *volume;
1950 gchar *display_name;
1956 if (_gtk_file_system_volume_is_mounted (volume))
1960 base_file = _gtk_file_system_volume_get_root (volume);
1961 if (base_file != NULL)
1963 if (!g_file_is_native (base_file))
1965 g_object_unref (base_file);
1969 g_object_unref (base_file);
1974 pixbuf = _gtk_file_system_volume_render_icon (volume,
1975 GTK_WIDGET (button),
1976 button->priv->icon_size,
1978 display_name = _gtk_file_system_volume_get_display_name (volume);
1980 gtk_list_store_insert (store, &iter, pos);
1981 gtk_list_store_set (store, &iter,
1982 ICON_COLUMN, pixbuf,
1983 DISPLAY_NAME_COLUMN, display_name,
1984 TYPE_COLUMN, ROW_TYPE_VOLUME,
1985 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
1986 IS_FOLDER_COLUMN, TRUE,
1990 g_object_unref (pixbuf);
1991 g_free (display_name);
1993 button->priv->n_volumes++;
1998 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
2001 model_add_bookmarks (GtkFileChooserButton *button,
2004 GtkListStore *store;
2007 gboolean local_only;
2013 store = GTK_LIST_STORE (button->priv->model);
2014 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
2015 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
2017 for (l = bookmarks; l; l = l->next)
2023 if (g_file_is_native (file))
2025 gtk_list_store_insert (store, &iter, pos);
2026 gtk_list_store_set (store, &iter,
2028 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2029 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2030 DATA_COLUMN, g_object_ref (file),
2031 IS_FOLDER_COLUMN, FALSE,
2033 set_info_for_file_at_iter (button, file, &iter);
2038 GtkIconTheme *icon_theme;
2044 /* Don't call get_info for remote paths to avoid latency and
2046 * If we switch to a better bookmarks file format (XBEL), we
2047 * should use mime info to get a better icon.
2049 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2051 label = _gtk_file_chooser_label_for_file (file);
2053 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2054 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2055 button->priv->icon_size, 0, NULL);
2057 gtk_list_store_insert (store, &iter, pos);
2058 gtk_list_store_set (store, &iter,
2059 ICON_COLUMN, pixbuf,
2060 DISPLAY_NAME_COLUMN, label,
2061 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2062 DATA_COLUMN, g_object_ref (file),
2063 IS_FOLDER_COLUMN, TRUE,
2067 g_object_unref (pixbuf);
2070 button->priv->n_bookmarks++;
2074 if (button->priv->n_bookmarks > 0 &&
2075 !button->priv->has_bookmark_separator)
2077 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2079 gtk_list_store_insert (store, &iter, pos);
2080 gtk_list_store_set (store, &iter,
2082 DISPLAY_NAME_COLUMN, NULL,
2083 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2085 IS_FOLDER_COLUMN, FALSE,
2087 button->priv->has_bookmark_separator = TRUE;
2092 model_update_current_folder (GtkFileChooserButton *button,
2095 GtkListStore *store;
2102 store = GTK_LIST_STORE (button->priv->model);
2104 if (!button->priv->has_current_folder_separator)
2106 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
2107 gtk_list_store_insert (store, &iter, pos);
2108 gtk_list_store_set (store, &iter,
2110 DISPLAY_NAME_COLUMN, NULL,
2111 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
2113 IS_FOLDER_COLUMN, FALSE,
2115 button->priv->has_current_folder_separator = TRUE;
2118 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2119 if (!button->priv->has_current_folder)
2121 gtk_list_store_insert (store, &iter, pos);
2122 button->priv->has_current_folder = TRUE;
2126 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2127 model_free_row_data (button, &iter);
2130 if (g_file_is_native (file))
2132 gtk_list_store_set (store, &iter,
2134 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2135 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2136 DATA_COLUMN, g_object_ref (file),
2137 IS_FOLDER_COLUMN, FALSE,
2139 set_info_for_file_at_iter (button, file, &iter);
2144 GtkIconTheme *icon_theme;
2147 /* Don't call get_info for remote paths to avoid latency and
2149 * If we switch to a better bookmarks file format (XBEL), we
2150 * should use mime info to get a better icon.
2152 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2154 label = _gtk_file_chooser_label_for_file (file);
2156 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2158 if (g_file_is_native (file))
2159 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2160 button->priv->icon_size, 0, NULL);
2162 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2163 button->priv->icon_size, 0, NULL);
2165 gtk_list_store_set (store, &iter,
2166 ICON_COLUMN, pixbuf,
2167 DISPLAY_NAME_COLUMN, label,
2168 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2169 DATA_COLUMN, g_object_ref (file),
2170 IS_FOLDER_COLUMN, TRUE,
2174 g_object_unref (pixbuf);
2179 model_add_other (GtkFileChooserButton *button)
2181 GtkListStore *store;
2185 store = GTK_LIST_STORE (button->priv->model);
2186 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2188 gtk_list_store_insert (store, &iter, pos);
2189 gtk_list_store_set (store, &iter,
2191 DISPLAY_NAME_COLUMN, NULL,
2192 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2194 IS_FOLDER_COLUMN, FALSE,
2196 button->priv->has_other_separator = TRUE;
2199 gtk_list_store_insert (store, &iter, pos);
2200 gtk_list_store_set (store, &iter,
2202 DISPLAY_NAME_COLUMN, _("Other…"),
2203 TYPE_COLUMN, ROW_TYPE_OTHER,
2205 IS_FOLDER_COLUMN, FALSE,
2210 model_remove_rows (GtkFileChooserButton *button,
2214 GtkListStore *store;
2219 store = GTK_LIST_STORE (button->priv->model);
2225 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2226 g_assert_not_reached ();
2228 model_free_row_data (button, &iter);
2229 gtk_list_store_remove (store, &iter);
2236 static inline gboolean
2237 test_if_file_is_visible (GtkFileSystem *fs,
2239 gboolean local_only,
2245 if (local_only && !g_file_is_native (file))
2255 filter_model_visible_func (GtkTreeModel *model,
2259 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2260 GtkFileChooserButtonPrivate *priv = button->priv;
2263 gboolean local_only, retval, is_folder;
2265 type = ROW_TYPE_INVALID;
2267 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2269 gtk_tree_model_get (model, iter,
2272 IS_FOLDER_COLUMN, &is_folder,
2277 case ROW_TYPE_CURRENT_FOLDER:
2280 case ROW_TYPE_SPECIAL:
2281 case ROW_TYPE_SHORTCUT:
2282 case ROW_TYPE_BOOKMARK:
2283 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2285 case ROW_TYPE_VOLUME:
2290 if (_gtk_file_system_volume_is_mounted (data))
2294 base_file = _gtk_file_system_volume_get_root (data);
2298 if (!g_file_is_native (base_file))
2300 g_object_unref (base_file);
2318 name_cell_data_func (GtkCellLayout *layout,
2319 GtkCellRenderer *cell,
2320 GtkTreeModel *model,
2327 gtk_tree_model_get (model, iter,
2331 if (type == ROW_TYPE_CURRENT_FOLDER)
2332 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2334 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2338 combo_box_row_separator_func (GtkTreeModel *model,
2342 gchar type = ROW_TYPE_INVALID;
2344 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2346 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2347 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2348 type == ROW_TYPE_OTHER_SEPARATOR);
2352 update_combo_box (GtkFileChooserButton *button)
2354 GtkFileChooserButtonPrivate *priv = button->priv;
2359 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2361 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2370 type = ROW_TYPE_INVALID;
2373 gtk_tree_model_get (priv->filter_model, &iter,
2380 case ROW_TYPE_SPECIAL:
2381 case ROW_TYPE_SHORTCUT:
2382 case ROW_TYPE_BOOKMARK:
2383 case ROW_TYPE_CURRENT_FOLDER:
2384 row_found = (files &&
2386 g_file_equal (data, files->data));
2388 case ROW_TYPE_VOLUME:
2392 base_file = _gtk_file_system_volume_get_root (data);
2395 row_found = (files &&
2397 g_file_equal (base_file, files->data));
2398 g_object_unref (base_file);
2409 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2410 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2412 g_signal_handler_unblock (priv->combo_box,
2413 priv->combo_box_changed_id);
2416 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2418 /* If it hasn't been found already, update & select the current-folder row. */
2419 if (!row_found && files && files->data)
2421 GtkTreeIter filter_iter;
2424 model_update_current_folder (button, files->data);
2425 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2427 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2428 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2430 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2431 &filter_iter, &iter);
2433 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2434 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2435 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2438 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2439 g_slist_free (files);
2444 update_label_get_info_cb (GCancellable *cancellable,
2446 const GError *error,
2449 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2451 GtkFileChooserButton *button = data;
2452 GtkFileChooserButtonPrivate *priv = button->priv;
2454 if (cancellable != priv->update_button_cancellable)
2457 priv->update_button_cancellable = NULL;
2459 if (cancelled || error)
2462 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2464 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2467 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2469 priv->icon_size, 0, NULL);
2471 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2473 g_object_unref (pixbuf);
2476 g_object_unref (button);
2477 g_object_unref (cancellable);
2481 update_label_and_image (GtkFileChooserButton *button)
2483 GtkFileChooserButtonPrivate *priv = button->priv;
2487 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2490 if (priv->update_button_cancellable)
2492 g_cancellable_cancel (priv->update_button_cancellable);
2493 priv->update_button_cancellable = NULL;
2496 if (files && files->data)
2499 GtkFileSystemVolume *volume = NULL;
2503 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2508 base_file = _gtk_file_system_volume_get_root (volume);
2509 if (base_file && g_file_equal (base_file, file))
2513 label_text = _gtk_file_system_volume_get_display_name (volume);
2514 pixbuf = _gtk_file_system_volume_render_icon (volume,
2515 GTK_WIDGET (button),
2518 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2520 g_object_unref (pixbuf);
2524 g_object_unref (base_file);
2526 _gtk_file_system_volume_unref (volume);
2532 if (g_file_is_native (file))
2534 priv->update_button_cancellable =
2535 _gtk_file_system_get_info (priv->fs, file,
2536 "standard::icon,standard::display-name",
2537 update_label_get_info_cb,
2538 g_object_ref (button));
2544 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2545 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2547 priv->icon_size, 0, NULL);
2548 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2550 g_object_unref (pixbuf);
2554 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2555 g_slist_free (files);
2559 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2560 g_free (label_text);
2564 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2565 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2570 /* ************************ *
2571 * Child Object Callbacks *
2572 * ************************ */
2576 fs_volumes_changed_cb (GtkFileSystem *fs,
2579 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2580 GtkFileChooserButtonPrivate *priv = button->priv;
2583 model_remove_rows (user_data,
2584 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2587 priv->n_volumes = 0;
2589 volumes = _gtk_file_system_list_volumes (fs);
2590 model_add_volumes (user_data, volumes);
2591 g_slist_free (volumes);
2593 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2595 update_label_and_image (user_data);
2596 update_combo_box (user_data);
2600 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2603 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2604 GtkFileChooserButtonPrivate *priv = button->priv;
2607 bookmarks = _gtk_file_system_list_bookmarks (fs);
2608 model_remove_rows (user_data,
2609 model_get_type_position (user_data,
2610 ROW_TYPE_BOOKMARK_SEPARATOR),
2611 (priv->n_bookmarks + priv->has_bookmark_separator));
2612 priv->has_bookmark_separator = FALSE;
2613 priv->n_bookmarks = 0;
2614 model_add_bookmarks (user_data, bookmarks);
2615 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2616 g_slist_free (bookmarks);
2618 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2620 update_label_and_image (user_data);
2621 update_combo_box (user_data);
2625 save_inactive_state (GtkFileChooserButton *button)
2627 GtkFileChooserButtonPrivate *priv = button->priv;
2629 if (priv->current_folder_while_inactive)
2630 g_object_unref (priv->current_folder_while_inactive);
2632 if (priv->selection_while_inactive)
2633 g_object_unref (priv->selection_while_inactive);
2635 priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->dialog));
2636 priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2640 restore_inactive_state (GtkFileChooserButton *button)
2642 GtkFileChooserButtonPrivate *priv = button->priv;
2644 if (priv->current_folder_while_inactive)
2645 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), priv->current_folder_while_inactive, NULL);
2647 if (priv->selection_while_inactive)
2648 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2650 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2655 open_dialog (GtkFileChooserButton *button)
2657 GtkFileChooserButtonPrivate *priv = button->priv;
2659 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2661 if (!gtk_widget_get_visible (priv->dialog))
2663 GtkWidget *toplevel;
2665 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2667 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2669 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2670 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2671 GTK_WINDOW (toplevel));
2673 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2674 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2680 g_signal_handler_block (priv->dialog,
2681 priv->dialog_folder_changed_id);
2682 g_signal_handler_block (priv->dialog,
2683 priv->dialog_file_activated_id);
2684 g_signal_handler_block (priv->dialog,
2685 priv->dialog_selection_changed_id);
2687 restore_inactive_state (button);
2689 priv->active = TRUE;
2692 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2693 gtk_window_present (GTK_WINDOW (priv->dialog));
2698 combo_box_changed_cb (GtkComboBox *combo_box,
2703 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2705 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2706 GtkFileChooserButtonPrivate *priv = button->priv;
2710 type = ROW_TYPE_INVALID;
2713 gtk_tree_model_get (priv->filter_model, &iter,
2720 case ROW_TYPE_SPECIAL:
2721 case ROW_TYPE_SHORTCUT:
2722 case ROW_TYPE_BOOKMARK:
2723 case ROW_TYPE_CURRENT_FOLDER:
2724 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2726 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2729 case ROW_TYPE_VOLUME:
2733 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2734 base_file = _gtk_file_system_volume_get_root (data);
2737 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2739 g_object_unref (base_file);
2743 case ROW_TYPE_OTHER:
2744 open_dialog (user_data);
2754 button_clicked_cb (GtkButton *real_button,
2757 open_dialog (user_data);
2762 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2765 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2767 g_signal_emit_by_name (button, "current-folder-changed");
2771 dialog_file_activated_cb (GtkFileChooser *dialog,
2774 g_signal_emit_by_name (user_data, "file-activated");
2778 dialog_selection_changed_cb (GtkFileChooser *dialog,
2781 update_label_and_image (user_data);
2782 update_combo_box (user_data);
2783 g_signal_emit_by_name (user_data, "selection-changed");
2787 dialog_update_preview_cb (GtkFileChooser *dialog,
2790 g_signal_emit_by_name (user_data, "update-preview");
2794 dialog_notify_cb (GObject *dialog,
2800 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2801 GTK_TYPE_FILE_CHOOSER);
2802 if (g_object_interface_find_property (iface, pspec->name))
2803 g_object_notify (user_data, pspec->name);
2805 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2807 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2808 GtkFileChooserButtonPrivate *priv = button->priv;
2810 if (priv->has_current_folder)
2816 pos = model_get_type_position (user_data,
2817 ROW_TYPE_CURRENT_FOLDER);
2818 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2821 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2823 /* If the path isn't local but we're in local-only mode now, remove
2824 * the custom-folder row */
2825 if (data && g_file_is_native (G_FILE (data)) &&
2826 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2829 model_remove_rows (user_data, pos, 2);
2833 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2834 update_combo_box (user_data);
2839 dialog_delete_event_cb (GtkWidget *dialog,
2843 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2849 dialog_response_cb (GtkDialog *dialog,
2853 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2854 GtkFileChooserButtonPrivate *priv = button->priv;
2856 if (response == GTK_RESPONSE_ACCEPT ||
2857 response == GTK_RESPONSE_OK)
2859 save_inactive_state (button);
2861 g_signal_emit_by_name (button, "current-folder-changed");
2862 g_signal_emit_by_name (button, "selection-changed");
2864 update_label_and_image (button);
2865 update_combo_box (button);
2869 restore_inactive_state (button);
2874 g_signal_handler_unblock (priv->dialog,
2875 priv->dialog_folder_changed_id);
2876 g_signal_handler_unblock (priv->dialog,
2877 priv->dialog_file_activated_id);
2878 g_signal_handler_unblock (priv->dialog,
2879 priv->dialog_selection_changed_id);
2880 priv->active = FALSE;
2883 gtk_widget_set_sensitive (priv->combo_box, TRUE);
2884 gtk_widget_hide (priv->dialog);
2886 if (response == GTK_RESPONSE_ACCEPT ||
2887 response == GTK_RESPONSE_OK)
2888 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
2892 /* ************************************************************************** *
2894 * ************************************************************************** */
2897 * gtk_file_chooser_button_new:
2898 * @title: the title of the browse dialog.
2899 * @action: the open mode for the widget.
2901 * Creates a new file-selecting button widget.
2903 * Returns: a new button widget.
2908 gtk_file_chooser_button_new (const gchar *title,
2909 GtkFileChooserAction action)
2911 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2912 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2914 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2916 "title", (title ? title : _(DEFAULT_TITLE)),
2921 * gtk_file_chooser_button_new_with_dialog:
2922 * @dialog: the widget to use as dialog
2924 * Creates a #GtkFileChooserButton widget which uses @dialog as its
2925 * file-picking window.
2927 * Note that @dialog must be a #GtkDialog (or subclass) which
2928 * implements the #GtkFileChooser interface and must not have
2929 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2931 * Also note that the dialog needs to have its confirmative button
2932 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2933 * order for the button to take over the file selected in the dialog.
2935 * Returns: a new button widget.
2940 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2942 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2944 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2950 * gtk_file_chooser_button_set_title:
2951 * @button: the button widget to modify.
2952 * @title: the new browse dialog title.
2954 * Modifies the @title of the browse dialog used by @button.
2959 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2962 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2964 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2965 g_object_notify (G_OBJECT (button), "title");
2969 * gtk_file_chooser_button_get_title:
2970 * @button: the button widget to examine.
2972 * Retrieves the title of the browse dialog used by @button. The returned value
2973 * should not be modified or freed.
2975 * Returns: a pointer to the browse dialog's title.
2980 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2982 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2984 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2988 * gtk_file_chooser_button_get_width_chars:
2989 * @button: the button widget to examine.
2991 * Retrieves the width in characters of the @button widget's entry and/or label.
2993 * Returns: an integer width (in characters) that the button will use to size itself.
2998 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
3000 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
3002 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
3006 * gtk_file_chooser_button_set_width_chars:
3007 * @button: the button widget to examine.
3008 * @n_chars: the new width, in characters.
3010 * Sets the width (in characters) that @button will use to @n_chars.
3015 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
3018 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3020 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
3021 g_object_notify (G_OBJECT (button), "width-chars");
3025 * gtk_file_chooser_button_set_focus_on_click:
3026 * @button: a #GtkFileChooserButton
3027 * @focus_on_click: whether the button grabs focus when clicked with the mouse
3029 * Sets whether the button will grab focus when it is clicked with the mouse.
3030 * Making mouse clicks not grab focus is useful in places like toolbars where
3031 * you don't want the keyboard focus removed from the main area of the
3037 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
3038 gboolean focus_on_click)
3040 GtkFileChooserButtonPrivate *priv;
3042 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3044 priv = button->priv;
3046 focus_on_click = focus_on_click != FALSE;
3048 if (priv->focus_on_click != focus_on_click)
3050 priv->focus_on_click = focus_on_click;
3051 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
3052 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
3054 g_object_notify (G_OBJECT (button), "focus-on-click");
3059 * gtk_file_chooser_button_get_focus_on_click:
3060 * @button: a #GtkFileChooserButton
3062 * Returns whether the button grabs focus when it is clicked with the mouse.
3063 * See gtk_file_chooser_button_set_focus_on_click().
3065 * Return value: %TRUE if the button grabs focus when it is clicked with
3071 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
3073 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
3075 return button->priv->focus_on_click;