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)") /* this string is used in gtk+/gtk/tests/filechooser.c - change it there if you change it here */
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,
157 ROW_TYPE_EMPTY_SELECTION,
159 ROW_TYPE_INVALID = -1
164 /* ******************** *
165 * Private Structures *
166 * ******************** */
168 struct _GtkFileChooserButtonPrivate
174 GtkWidget *combo_box;
175 GtkCellRenderer *icon_cell;
176 GtkCellRenderer *name_cell;
179 GtkTreeModel *filter_model;
182 GFile *selection_while_inactive;
183 GFile *current_folder_while_inactive;
185 gulong combo_box_changed_id;
186 gulong dialog_file_activated_id;
187 gulong dialog_folder_changed_id;
188 gulong dialog_selection_changed_id;
189 gulong fs_volumes_changed_id;
190 gulong fs_bookmarks_changed_id;
192 GCancellable *dnd_select_folder_cancellable;
193 GCancellable *update_button_cancellable;
194 GSList *change_icon_theme_cancellables;
202 guint has_bookmark_separator : 1;
203 guint has_current_folder_separator : 1;
204 guint has_current_folder : 1;
205 guint has_other_separator : 1;
207 /* Used for hiding/showing the dialog when the button is hidden */
210 guint focus_on_click : 1;
212 /* Whether the next async callback from GIO should emit the "selection-changed" signal */
213 guint is_changing_selection : 1;
228 /* ********************* *
229 * Function Prototypes *
230 * ********************* */
232 /* GtkFileChooserIface Functions */
233 static void gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
234 static gboolean gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
237 static GFile *gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser);
238 static gboolean gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
241 static void gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
243 static void gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser);
244 static GSList *gtk_file_chooser_button_get_files (GtkFileChooser *chooser);
245 static gboolean gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
248 static gboolean gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
252 /* GObject Functions */
253 static GObject *gtk_file_chooser_button_constructor (GType type,
255 GObjectConstructParam *params);
256 static void gtk_file_chooser_button_set_property (GObject *object,
260 static void gtk_file_chooser_button_get_property (GObject *object,
264 static void gtk_file_chooser_button_finalize (GObject *object);
266 /* GtkWidget Functions */
267 static void gtk_file_chooser_button_destroy (GtkWidget *widget);
268 static void gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
269 GdkDragContext *context,
272 GtkSelectionData *data,
275 static void gtk_file_chooser_button_show_all (GtkWidget *widget);
276 static void gtk_file_chooser_button_show (GtkWidget *widget);
277 static void gtk_file_chooser_button_hide (GtkWidget *widget);
278 static void gtk_file_chooser_button_map (GtkWidget *widget);
279 static gboolean gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
280 gboolean group_cycling);
281 static void gtk_file_chooser_button_style_updated (GtkWidget *widget);
282 static void gtk_file_chooser_button_screen_changed (GtkWidget *widget,
283 GdkScreen *old_screen);
285 /* Utility Functions */
286 static GtkIconTheme *get_icon_theme (GtkWidget *widget);
287 static void set_info_for_file_at_iter (GtkFileChooserButton *fs,
291 static gint model_get_type_position (GtkFileChooserButton *button,
293 static void model_free_row_data (GtkFileChooserButton *button,
295 static void model_add_special (GtkFileChooserButton *button);
296 static void model_add_other (GtkFileChooserButton *button);
297 static void model_add_empty_selection (GtkFileChooserButton *button);
298 static void model_add_volumes (GtkFileChooserButton *button,
300 static void model_add_bookmarks (GtkFileChooserButton *button,
302 static void model_update_current_folder (GtkFileChooserButton *button,
304 static void model_remove_rows (GtkFileChooserButton *button,
308 static gboolean filter_model_visible_func (GtkTreeModel *model,
312 static gboolean combo_box_row_separator_func (GtkTreeModel *model,
315 static void name_cell_data_func (GtkCellLayout *layout,
316 GtkCellRenderer *cell,
320 static void open_dialog (GtkFileChooserButton *button);
321 static void update_combo_box (GtkFileChooserButton *button);
322 static void update_label_and_image (GtkFileChooserButton *button);
324 /* Child Object Callbacks */
325 static void fs_volumes_changed_cb (GtkFileSystem *fs,
327 static void fs_bookmarks_changed_cb (GtkFileSystem *fs,
330 static void combo_box_changed_cb (GtkComboBox *combo_box,
332 static void combo_box_notify_popup_shown_cb (GObject *object,
336 static void button_clicked_cb (GtkButton *real_button,
339 static void dialog_update_preview_cb (GtkFileChooser *dialog,
341 static void dialog_selection_changed_cb (GtkFileChooser *dialog,
343 static void dialog_file_activated_cb (GtkFileChooser *dialog,
345 static void dialog_current_folder_changed_cb (GtkFileChooser *dialog,
347 static void dialog_notify_cb (GObject *dialog,
350 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
353 static void dialog_response_cb (GtkDialog *dialog,
357 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
359 /* ******************* *
360 * GType Declaration *
361 * ******************* */
363 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
364 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
368 /* ***************** *
370 * ***************** */
373 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
375 GObjectClass *gobject_class;
376 GtkWidgetClass *widget_class;
378 gobject_class = G_OBJECT_CLASS (class);
379 widget_class = GTK_WIDGET_CLASS (class);
381 gobject_class->constructor = gtk_file_chooser_button_constructor;
382 gobject_class->set_property = gtk_file_chooser_button_set_property;
383 gobject_class->get_property = gtk_file_chooser_button_get_property;
384 gobject_class->finalize = gtk_file_chooser_button_finalize;
386 widget_class->destroy = gtk_file_chooser_button_destroy;
387 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
388 widget_class->show_all = gtk_file_chooser_button_show_all;
389 widget_class->show = gtk_file_chooser_button_show;
390 widget_class->hide = gtk_file_chooser_button_hide;
391 widget_class->map = gtk_file_chooser_button_map;
392 widget_class->style_updated = gtk_file_chooser_button_style_updated;
393 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
394 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
397 * GtkFileChooserButton::file-set:
398 * @widget: the object which received the signal.
400 * The ::file-set signal is emitted when the user selects a file.
402 * Note that this signal is only emitted when the <emphasis>user</emphasis>
407 file_chooser_button_signals[FILE_SET] =
408 g_signal_new (I_("file-set"),
409 G_TYPE_FROM_CLASS (gobject_class),
411 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
413 _gtk_marshal_VOID__VOID,
417 * GtkFileChooserButton:dialog:
419 * Instance of the #GtkFileChooserDialog associated with the button.
423 g_object_class_install_property (gobject_class, PROP_DIALOG,
424 g_param_spec_object ("dialog",
426 P_("The file chooser dialog to use."),
427 GTK_TYPE_FILE_CHOOSER,
428 (GTK_PARAM_WRITABLE |
429 G_PARAM_CONSTRUCT_ONLY)));
432 * GtkFileChooserButton:focus-on-click:
434 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
439 g_object_class_install_property (gobject_class,
441 g_param_spec_boolean ("focus-on-click",
442 P_("Focus on click"),
443 P_("Whether the button grabs focus when it is clicked with the mouse"),
445 GTK_PARAM_READWRITE));
448 * GtkFileChooserButton:title:
450 * Title to put on the #GtkFileChooserDialog associated with the button.
454 g_object_class_install_property (gobject_class, PROP_TITLE,
455 g_param_spec_string ("title",
457 P_("The title of the file chooser dialog."),
459 GTK_PARAM_READWRITE));
462 * GtkFileChooserButton:width-chars:
464 * The width of the entry and label inside the button, in characters.
468 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
469 g_param_spec_int ("width-chars",
470 P_("Width In Characters"),
471 P_("The desired width of the button widget, in characters."),
473 GTK_PARAM_READWRITE));
475 _gtk_file_chooser_install_properties (gobject_class);
477 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
481 gtk_file_chooser_button_init (GtkFileChooserButton *button)
483 GtkFileChooserButtonPrivate *priv;
484 GtkWidget *box, *image, *sep;
485 GtkTargetList *target_list;
487 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
488 GTK_TYPE_FILE_CHOOSER_BUTTON,
489 GtkFileChooserButtonPrivate);
491 priv->icon_size = FALLBACK_ICON_SIZE;
492 priv->focus_on_click = TRUE;
494 gtk_widget_push_composite_child ();
497 priv->button = gtk_button_new ();
498 g_signal_connect (priv->button, "clicked",
499 G_CALLBACK (button_clicked_cb), button);
500 gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
501 gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
502 gtk_widget_show (priv->button);
504 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
505 gtk_container_add (GTK_CONTAINER (priv->button), box);
506 gtk_widget_show (box);
508 priv->image = gtk_image_new ();
509 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
510 gtk_widget_show (priv->image);
512 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
513 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
514 gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
515 gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
516 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
517 //gtk_container_add (GTK_CONTAINER (box), priv->label);
518 gtk_widget_show (priv->label);
520 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
521 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
522 gtk_widget_show (sep);
524 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
525 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
526 gtk_widget_show (image);
529 /* Keep in sync with columns enum, line 88 */
531 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
532 GDK_TYPE_PIXBUF, /* ICON_COLUMN */
533 G_TYPE_STRING, /* DISPLAY_NAME_COLUMN */
534 G_TYPE_CHAR, /* TYPE_COLUMN */
535 G_TYPE_POINTER /* DATA_COLUMN (Volume || Path) */,
536 G_TYPE_BOOLEAN /* IS_FOLDER_COLUMN */,
537 G_TYPE_POINTER /* CANCELLABLE_COLUMN */));
539 priv->combo_box = gtk_combo_box_new ();
540 priv->combo_box_changed_id = g_signal_connect (priv->combo_box, "changed",
541 G_CALLBACK (combo_box_changed_cb), button);
543 g_signal_connect (priv->combo_box, "notify::popup-shown",
544 G_CALLBACK (combo_box_notify_popup_shown_cb), button);
546 gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
547 gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
549 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
550 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
551 priv->icon_cell, FALSE);
552 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
553 priv->icon_cell, "pixbuf", ICON_COLUMN);
555 priv->name_cell = gtk_cell_renderer_text_new ();
556 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
557 priv->name_cell, TRUE);
558 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
559 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
560 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
561 priv->name_cell, name_cell_data_func,
564 gtk_widget_pop_composite_child ();
567 gtk_drag_dest_set (GTK_WIDGET (button),
568 (GTK_DEST_DEFAULT_ALL),
571 target_list = gtk_target_list_new (NULL, 0);
572 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
573 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
574 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
575 gtk_target_list_unref (target_list);
579 /* ******************************* *
580 * GtkFileChooserIface Functions *
581 * ******************************* */
583 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
585 _gtk_file_chooser_delegate_iface_init (iface);
587 iface->set_current_folder = gtk_file_chooser_button_set_current_folder;
588 iface->get_current_folder = gtk_file_chooser_button_get_current_folder;
589 iface->select_file = gtk_file_chooser_button_select_file;
590 iface->unselect_file = gtk_file_chooser_button_unselect_file;
591 iface->unselect_all = gtk_file_chooser_button_unselect_all;
592 iface->get_files = gtk_file_chooser_button_get_files;
593 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
594 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
598 emit_selection_changed_if_changing_selection (GtkFileChooserButton *button)
600 GtkFileChooserButtonPrivate *priv = button->priv;
602 if (priv->is_changing_selection)
604 priv->is_changing_selection = FALSE;
605 g_signal_emit_by_name (button, "selection-changed");
610 gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
614 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
615 GtkFileChooserButtonPrivate *priv = button->priv;
616 GtkFileChooser *delegate;
618 delegate = g_object_get_qdata (G_OBJECT (chooser),
619 GTK_FILE_CHOOSER_DELEGATE_QUARK);
622 return gtk_file_chooser_set_current_folder_file (delegate, file, error);
625 if (priv->current_folder_while_inactive)
626 g_object_unref (priv->current_folder_while_inactive);
628 priv->current_folder_while_inactive = g_object_ref (file);
630 update_combo_box (button);
632 g_signal_emit_by_name (button, "current-folder-changed");
639 gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser)
641 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
642 GtkFileChooserButtonPrivate *priv = button->priv;
643 GtkFileChooser *delegate;
645 delegate = g_object_get_qdata (G_OBJECT (chooser),
646 GTK_FILE_CHOOSER_DELEGATE_QUARK);
649 return gtk_file_chooser_get_current_folder_file (delegate);
652 if (priv->current_folder_while_inactive)
653 return g_object_ref (priv->current_folder_while_inactive);
660 gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
664 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
665 GtkFileChooserButtonPrivate *priv = button->priv;
666 GtkFileChooser *delegate;
668 delegate = g_object_get_qdata (G_OBJECT (chooser),
669 GTK_FILE_CHOOSER_DELEGATE_QUARK);
672 return gtk_file_chooser_select_file (delegate, file, error);
675 if (priv->selection_while_inactive)
676 g_object_unref (priv->selection_while_inactive);
678 priv->selection_while_inactive = g_object_ref (file);
680 priv->is_changing_selection = TRUE;
682 update_label_and_image (button);
683 update_combo_box (button);
690 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
693 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
694 GtkFileChooserButtonPrivate *priv = button->priv;
695 GtkFileChooser *delegate;
697 delegate = g_object_get_qdata (G_OBJECT (chooser),
698 GTK_FILE_CHOOSER_DELEGATE_QUARK);
701 gtk_file_chooser_unselect_file (delegate, file);
704 if (g_file_equal (priv->selection_while_inactive, file))
706 if (priv->selection_while_inactive)
708 g_object_unref (priv->selection_while_inactive);
709 priv->selection_while_inactive = NULL;
712 priv->is_changing_selection = TRUE;
714 update_label_and_image (button);
715 update_combo_box (button);
721 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
723 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
724 GtkFileChooserButtonPrivate *priv = button->priv;
725 GtkFileChooser *delegate;
727 delegate = g_object_get_qdata (G_OBJECT (chooser),
728 GTK_FILE_CHOOSER_DELEGATE_QUARK);
731 gtk_file_chooser_unselect_all (delegate);
734 if (priv->selection_while_inactive)
736 g_object_unref (priv->selection_while_inactive);
737 priv->selection_while_inactive = NULL;
740 update_label_and_image (button);
741 update_combo_box (button);
746 get_selected_file (GtkFileChooserButton *button)
748 GtkFileChooserButtonPrivate *priv = button->priv;
751 return gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
754 if (priv->selection_while_inactive)
755 return g_object_ref (priv->selection_while_inactive);
756 else if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
758 /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
759 * the current folder, since that is what GtkFileChooserDefault would do.
761 if (priv->current_folder_while_inactive)
762 return g_object_ref (priv->current_folder_while_inactive);
770 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
772 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
774 return g_slist_prepend (NULL, get_selected_file (button));
778 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
782 GtkFileChooser *delegate;
785 delegate = g_object_get_qdata (G_OBJECT (chooser),
786 GTK_FILE_CHOOSER_DELEGATE_QUARK);
787 retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
791 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
792 GtkFileChooserButtonPrivate *priv = button->priv;
796 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
797 pos += priv->n_shortcuts;
799 gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
800 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
802 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
803 TYPE_COLUMN, ROW_TYPE_SHORTCUT,
804 DATA_COLUMN, g_object_ref (file),
805 IS_FOLDER_COLUMN, FALSE,
807 set_info_for_file_at_iter (button, file, &iter);
810 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
817 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
821 GtkFileChooser *delegate;
824 delegate = g_object_get_qdata (G_OBJECT (chooser),
825 GTK_FILE_CHOOSER_DELEGATE_QUARK);
827 retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
831 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
832 GtkFileChooserButtonPrivate *priv = button->priv;
837 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
838 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
844 gtk_tree_model_get (priv->model, &iter,
849 if (type == ROW_TYPE_SHORTCUT &&
850 data && g_file_equal (data, file))
852 model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
853 gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
855 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
856 update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
860 while (type == ROW_TYPE_SHORTCUT &&
861 gtk_tree_model_iter_next (priv->model, &iter));
868 /* ******************* *
869 * GObject Functions *
870 * ******************* */
873 gtk_file_chooser_button_constructor (GType type,
875 GObjectConstructParam *params)
878 GtkFileChooserButton *button;
879 GtkFileChooserButtonPrivate *priv;
882 object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
885 button = GTK_FILE_CHOOSER_BUTTON (object);
890 priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
891 GTK_FILE_CHOOSER_ACTION_OPEN,
898 gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
899 GTK_RESPONSE_ACCEPT);
900 gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
905 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
907 else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
909 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
912 g_signal_connect (priv->dialog, "delete-event",
913 G_CALLBACK (dialog_delete_event_cb), object);
914 g_signal_connect (priv->dialog, "response",
915 G_CALLBACK (dialog_response_cb), object);
917 /* This is used, instead of the standard delegate, to ensure that signals are only
918 * delegated when the OK button is pressed. */
919 g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
920 priv->dialog_folder_changed_id =
921 g_signal_connect (priv->dialog, "current-folder-changed",
922 G_CALLBACK (dialog_current_folder_changed_cb), object);
923 priv->dialog_file_activated_id =
924 g_signal_connect (priv->dialog, "file-activated",
925 G_CALLBACK (dialog_file_activated_cb), object);
926 priv->dialog_selection_changed_id =
927 g_signal_connect (priv->dialog, "selection-changed",
928 G_CALLBACK (dialog_selection_changed_cb), object);
929 g_signal_connect (priv->dialog, "update-preview",
930 G_CALLBACK (dialog_update_preview_cb), object);
931 g_signal_connect (priv->dialog, "notify",
932 G_CALLBACK (dialog_notify_cb), object);
933 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
934 (gpointer) (&priv->dialog));
937 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
939 model_add_special (button);
941 list = _gtk_file_system_list_volumes (priv->fs);
942 model_add_volumes (button, list);
945 list = _gtk_file_system_list_bookmarks (priv->fs);
946 model_add_bookmarks (button, list);
947 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
950 model_add_other (button);
952 model_add_empty_selection (button);
954 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
955 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
956 filter_model_visible_func,
959 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
960 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
961 combo_box_row_separator_func,
964 /* set up the action for a user-provided dialog, this also updates
965 * the label, image and combobox
967 g_object_set (object,
968 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
971 priv->fs_volumes_changed_id =
972 g_signal_connect (priv->fs, "volumes-changed",
973 G_CALLBACK (fs_volumes_changed_cb), object);
974 priv->fs_bookmarks_changed_id =
975 g_signal_connect (priv->fs, "bookmarks-changed",
976 G_CALLBACK (fs_bookmarks_changed_cb), object);
978 update_label_and_image (button);
979 update_combo_box (button);
985 gtk_file_chooser_button_set_property (GObject *object,
990 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
991 GtkFileChooserButtonPrivate *priv = button->priv;
997 priv->dialog = g_value_get_object (value);
999 case PROP_FOCUS_ON_CLICK:
1000 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
1002 case PROP_WIDTH_CHARS:
1003 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
1004 g_value_get_int (value));
1006 case GTK_FILE_CHOOSER_PROP_ACTION:
1007 switch (g_value_get_enum (value))
1009 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1010 case GTK_FILE_CHOOSER_ACTION_SAVE:
1015 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
1016 eval = g_enum_get_value (eclass, g_value_get_enum (value));
1017 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
1018 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
1020 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
1025 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1026 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
1027 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
1029 switch (g_value_get_enum (value))
1031 case GTK_FILE_CHOOSER_ACTION_OPEN:
1032 gtk_widget_hide (priv->combo_box);
1033 gtk_widget_show (priv->button);
1035 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1036 gtk_widget_hide (priv->button);
1037 gtk_widget_show (priv->combo_box);
1040 g_assert_not_reached ();
1046 case GTK_FILE_CHOOSER_PROP_FILTER:
1047 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1048 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1049 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1050 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1051 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1052 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1053 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1054 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1057 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1058 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1059 fs_volumes_changed_cb (priv->fs, button);
1060 fs_bookmarks_changed_cb (priv->fs, button);
1063 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1064 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
1065 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
1068 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1074 gtk_file_chooser_button_get_property (GObject *object,
1079 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1080 GtkFileChooserButtonPrivate *priv = button->priv;
1084 case PROP_WIDTH_CHARS:
1085 g_value_set_int (value,
1086 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
1088 case PROP_FOCUS_ON_CLICK:
1089 g_value_set_boolean (value,
1090 gtk_file_chooser_button_get_focus_on_click (button));
1094 case GTK_FILE_CHOOSER_PROP_ACTION:
1095 case GTK_FILE_CHOOSER_PROP_FILTER:
1096 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1097 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1098 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1099 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1100 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1101 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1102 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1103 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1104 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1105 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1115 gtk_file_chooser_button_finalize (GObject *object)
1117 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1118 GtkFileChooserButtonPrivate *priv = button->priv;
1120 if (priv->selection_while_inactive)
1121 g_object_unref (priv->selection_while_inactive);
1123 if (priv->current_folder_while_inactive)
1124 g_object_unref (priv->current_folder_while_inactive);
1126 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1129 /* ********************* *
1130 * GtkWidget Functions *
1131 * ********************* */
1134 gtk_file_chooser_button_destroy (GtkWidget *widget)
1136 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1137 GtkFileChooserButtonPrivate *priv = button->priv;
1141 if (priv->dialog != NULL)
1143 gtk_widget_destroy (priv->dialog);
1144 priv->dialog = NULL;
1147 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1149 model_free_row_data (button, &iter);
1151 while (gtk_tree_model_iter_next (priv->model, &iter));
1153 if (priv->dnd_select_folder_cancellable)
1155 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1156 priv->dnd_select_folder_cancellable = NULL;
1159 if (priv->update_button_cancellable)
1161 g_cancellable_cancel (priv->update_button_cancellable);
1162 priv->update_button_cancellable = NULL;
1165 if (priv->change_icon_theme_cancellables)
1167 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1169 GCancellable *cancellable = G_CANCELLABLE (l->data);
1170 g_cancellable_cancel (cancellable);
1172 g_slist_free (priv->change_icon_theme_cancellables);
1173 priv->change_icon_theme_cancellables = NULL;
1178 g_object_unref (priv->model);
1182 if (priv->filter_model)
1184 g_object_unref (priv->filter_model);
1185 priv->filter_model = NULL;
1190 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1191 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1192 g_object_unref (priv->fs);
1196 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1199 struct DndSelectFolderData
1201 GtkFileSystem *file_system;
1202 GtkFileChooserButton *button;
1203 GtkFileChooserAction action;
1211 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1213 const GError *error,
1216 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1217 struct DndSelectFolderData *data = user_data;
1219 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1221 g_object_unref (data->button);
1222 g_object_unref (data->file);
1223 g_strfreev (data->uris);
1226 g_object_unref (cancellable);
1230 data->button->priv->dnd_select_folder_cancellable = NULL;
1232 if (!cancelled && !error && info != NULL)
1236 is_folder = _gtk_file_info_consider_as_directory (info);
1239 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1240 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1241 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1245 data->selected = FALSE;
1247 if (data->selected || data->uris[++data->i] == NULL)
1249 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1251 g_object_unref (data->button);
1252 g_object_unref (data->file);
1253 g_strfreev (data->uris);
1256 g_object_unref (cancellable);
1261 g_object_unref (data->file);
1263 data->file = g_file_new_for_uri (data->uris[data->i]);
1265 data->button->priv->dnd_select_folder_cancellable =
1266 _gtk_file_system_get_info (data->file_system, data->file,
1268 dnd_select_folder_get_info_cb, user_data);
1270 g_object_unref (cancellable);
1274 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1275 GdkDragContext *context,
1278 GtkSelectionData *data,
1282 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1283 GtkFileChooserButtonPrivate *priv = button->priv;
1287 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1288 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1294 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1302 struct DndSelectFolderData *info;
1304 uris = gtk_selection_data_get_uris (data);
1309 info = g_new0 (struct DndSelectFolderData, 1);
1310 info->button = g_object_ref (button);
1313 info->selected = FALSE;
1314 info->file_system = priv->fs;
1315 g_object_get (priv->dialog, "action", &info->action, NULL);
1317 info->file = g_file_new_for_uri (info->uris[info->i]);
1319 if (priv->dnd_select_folder_cancellable)
1320 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1322 priv->dnd_select_folder_cancellable =
1323 _gtk_file_system_get_info (priv->fs, info->file,
1325 dnd_select_folder_get_info_cb, info);
1330 text = (char*) gtk_selection_data_get_text (data);
1331 file = g_file_new_for_uri (text);
1332 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1334 g_object_unref (file);
1336 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1343 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1347 gtk_file_chooser_button_show_all (GtkWidget *widget)
1349 gtk_widget_show (widget);
1353 gtk_file_chooser_button_show (GtkWidget *widget)
1355 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1356 GtkFileChooserButtonPrivate *priv = button->priv;
1358 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1359 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1362 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1366 gtk_file_chooser_button_hide (GtkWidget *widget)
1368 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1369 GtkFileChooserButtonPrivate *priv = button->priv;
1371 gtk_widget_hide (priv->dialog);
1373 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1374 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1378 gtk_file_chooser_button_map (GtkWidget *widget)
1380 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1384 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1385 gboolean group_cycling)
1387 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1388 GtkFileChooserButtonPrivate *priv = button->priv;
1390 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1392 case GTK_FILE_CHOOSER_ACTION_OPEN:
1393 gtk_widget_grab_focus (priv->button);
1395 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1396 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1399 g_assert_not_reached ();
1406 /* Changes the icons wherever it is needed */
1407 struct ChangeIconThemeData
1409 GtkFileChooserButton *button;
1410 GtkTreeRowReference *row_ref;
1414 change_icon_theme_get_info_cb (GCancellable *cancellable,
1416 const GError *error,
1419 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1421 struct ChangeIconThemeData *data = user_data;
1423 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1426 data->button->priv->change_icon_theme_cancellables =
1427 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1429 if (cancelled || error)
1432 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1440 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1442 path = gtk_tree_row_reference_get_path (data->row_ref);
1445 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1446 gtk_tree_path_free (path);
1448 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1449 ICON_COLUMN, pixbuf,
1452 g_object_set (data->button->priv->icon_cell,
1456 g_object_unref (pixbuf);
1460 g_object_unref (data->button);
1461 gtk_tree_row_reference_free (data->row_ref);
1464 g_object_unref (cancellable);
1468 change_icon_theme (GtkFileChooserButton *button)
1470 GtkFileChooserButtonPrivate *priv = button->priv;
1471 GtkSettings *settings;
1472 GtkIconTheme *theme;
1475 gint width = 0, height = 0;
1477 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1479 GCancellable *cancellable = G_CANCELLABLE (l->data);
1480 g_cancellable_cancel (cancellable);
1482 g_slist_free (button->priv->change_icon_theme_cancellables);
1483 button->priv->change_icon_theme_cancellables = NULL;
1485 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1487 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1489 priv->icon_size = MAX (width, height);
1491 priv->icon_size = FALLBACK_ICON_SIZE;
1493 update_label_and_image (button);
1495 gtk_tree_model_get_iter_first (priv->model, &iter);
1497 theme = get_icon_theme (GTK_WIDGET (button));
1505 type = ROW_TYPE_INVALID;
1506 gtk_tree_model_get (priv->model, &iter,
1513 case ROW_TYPE_SPECIAL:
1514 case ROW_TYPE_SHORTCUT:
1515 case ROW_TYPE_BOOKMARK:
1516 case ROW_TYPE_CURRENT_FOLDER:
1519 if (g_file_is_native (G_FILE (data)))
1522 GCancellable *cancellable;
1523 struct ChangeIconThemeData *info;
1525 info = g_new0 (struct ChangeIconThemeData, 1);
1526 info->button = g_object_ref (button);
1527 path = gtk_tree_model_get_path (priv->model, &iter);
1528 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1529 gtk_tree_path_free (path);
1532 _gtk_file_system_get_info (priv->fs, data,
1534 change_icon_theme_get_info_cb,
1536 button->priv->change_icon_theme_cancellables =
1537 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1541 /* Don't call get_info for remote paths to avoid latency and
1543 * If we switch to a better bookmarks file format (XBEL), we
1544 * should use mime info to get a better icon.
1546 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1547 priv->icon_size, 0, NULL);
1550 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1551 priv->icon_size, 0, NULL);
1553 case ROW_TYPE_VOLUME:
1555 pixbuf = _gtk_file_system_volume_render_icon (data,
1556 GTK_WIDGET (button),
1560 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1561 priv->icon_size, 0, NULL);
1569 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1571 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1572 ICON_COLUMN, pixbuf,
1576 g_object_unref (pixbuf);
1578 while (gtk_tree_model_iter_next (priv->model, &iter));
1580 g_object_set (button->priv->icon_cell,
1586 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1588 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1590 if (gtk_widget_has_screen (widget))
1591 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1595 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1596 GdkScreen *old_screen)
1598 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1599 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1602 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1606 /* ******************* *
1607 * Utility Functions *
1608 * ******************* */
1611 static GtkIconTheme *
1612 get_icon_theme (GtkWidget *widget)
1614 if (gtk_widget_has_screen (widget))
1615 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1617 return gtk_icon_theme_get_default ();
1621 struct SetDisplayNameData
1623 GtkFileChooserButton *button;
1625 GtkTreeRowReference *row_ref;
1629 set_info_get_info_cb (GCancellable *cancellable,
1631 const GError *error,
1632 gpointer callback_data)
1634 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1638 GCancellable *model_cancellable = NULL;
1639 struct SetDisplayNameData *data = callback_data;
1642 if (!data->button->priv->model)
1643 /* button got destroyed */
1646 path = gtk_tree_row_reference_get_path (data->row_ref);
1648 /* Cancellable doesn't exist anymore in the model */
1651 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1652 gtk_tree_path_free (path);
1654 /* Validate the cancellable */
1655 gtk_tree_model_get (data->button->priv->model, &iter,
1656 CANCELLABLE_COLUMN, &model_cancellable,
1658 if (cancellable != model_cancellable)
1661 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1662 CANCELLABLE_COLUMN, NULL,
1665 if (cancelled || error)
1666 /* There was an error, leave the fallback name in there */
1669 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1672 data->label = g_strdup (g_file_info_get_display_name (info));
1674 is_folder = _gtk_file_info_consider_as_directory (info);
1676 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1677 ICON_COLUMN, pixbuf,
1678 DISPLAY_NAME_COLUMN, data->label,
1679 IS_FOLDER_COLUMN, is_folder,
1683 g_object_unref (pixbuf);
1686 g_object_unref (data->button);
1687 g_free (data->label);
1688 gtk_tree_row_reference_free (data->row_ref);
1691 if (model_cancellable)
1692 g_object_unref (model_cancellable);
1696 set_info_for_file_at_iter (GtkFileChooserButton *button,
1700 struct SetDisplayNameData *data;
1701 GtkTreePath *tree_path;
1702 GCancellable *cancellable;
1704 data = g_new0 (struct SetDisplayNameData, 1);
1705 data->button = g_object_ref (button);
1706 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1708 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1709 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1710 gtk_tree_path_free (tree_path);
1712 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1713 "standard::type,standard::icon,standard::display-name",
1714 set_info_get_info_cb, data);
1716 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1717 CANCELLABLE_COLUMN, cancellable,
1721 /* Shortcuts Model */
1723 model_get_type_position (GtkFileChooserButton *button,
1728 if (row_type == ROW_TYPE_SPECIAL)
1731 retval += button->priv->n_special;
1733 if (row_type == ROW_TYPE_VOLUME)
1736 retval += button->priv->n_volumes;
1738 if (row_type == ROW_TYPE_SHORTCUT)
1741 retval += button->priv->n_shortcuts;
1743 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1746 retval += button->priv->has_bookmark_separator;
1748 if (row_type == ROW_TYPE_BOOKMARK)
1751 retval += button->priv->n_bookmarks;
1753 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1756 retval += button->priv->has_current_folder_separator;
1758 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1761 retval += button->priv->has_current_folder;
1763 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1766 retval += button->priv->has_other_separator;
1768 if (row_type == ROW_TYPE_OTHER)
1773 if (row_type == ROW_TYPE_EMPTY_SELECTION)
1776 g_assert_not_reached ();
1781 model_free_row_data (GtkFileChooserButton *button,
1786 GCancellable *cancellable;
1788 gtk_tree_model_get (button->priv->model, iter,
1791 CANCELLABLE_COLUMN, &cancellable,
1796 g_cancellable_cancel (cancellable);
1797 g_object_unref (cancellable);
1802 case ROW_TYPE_SPECIAL:
1803 case ROW_TYPE_SHORTCUT:
1804 case ROW_TYPE_BOOKMARK:
1805 case ROW_TYPE_CURRENT_FOLDER:
1806 g_object_unref (data);
1808 case ROW_TYPE_VOLUME:
1809 _gtk_file_system_volume_unref (data);
1817 model_add_special_get_info_cb (GCancellable *cancellable,
1819 const GError *error,
1822 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1826 GCancellable *model_cancellable = NULL;
1827 struct ChangeIconThemeData *data = user_data;
1830 if (!data->button->priv->model)
1831 /* button got destroyed */
1834 path = gtk_tree_row_reference_get_path (data->row_ref);
1836 /* Cancellable doesn't exist anymore in the model */
1839 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1840 gtk_tree_path_free (path);
1842 gtk_tree_model_get (data->button->priv->model, &iter,
1843 CANCELLABLE_COLUMN, &model_cancellable,
1845 if (cancellable != model_cancellable)
1848 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1849 CANCELLABLE_COLUMN, NULL,
1852 if (cancelled || error)
1855 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1859 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1860 ICON_COLUMN, pixbuf,
1862 g_object_unref (pixbuf);
1865 gtk_tree_model_get (data->button->priv->model, &iter,
1866 DISPLAY_NAME_COLUMN, &name,
1869 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1870 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1875 g_object_unref (data->button);
1876 gtk_tree_row_reference_free (data->row_ref);
1879 if (model_cancellable)
1880 g_object_unref (model_cancellable);
1884 model_add_special (GtkFileChooserButton *button)
1886 const gchar *homedir;
1887 const gchar *desktopdir;
1888 GtkListStore *store;
1893 store = GTK_LIST_STORE (button->priv->model);
1894 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1896 homedir = g_get_home_dir ();
1900 GtkTreePath *tree_path;
1901 GCancellable *cancellable;
1902 struct ChangeIconThemeData *info;
1904 file = g_file_new_for_path (homedir);
1905 gtk_list_store_insert (store, &iter, pos);
1908 info = g_new0 (struct ChangeIconThemeData, 1);
1909 info->button = g_object_ref (button);
1910 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1911 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1913 gtk_tree_path_free (tree_path);
1915 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1916 "standard::icon,standard::display-name",
1917 model_add_special_get_info_cb, info);
1919 gtk_list_store_set (store, &iter,
1921 DISPLAY_NAME_COLUMN, NULL,
1922 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1924 IS_FOLDER_COLUMN, TRUE,
1925 CANCELLABLE_COLUMN, cancellable,
1928 button->priv->n_special++;
1931 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1933 /* "To disable a directory, point it to the homedir."
1934 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1936 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1938 GtkTreePath *tree_path;
1939 GCancellable *cancellable;
1940 struct ChangeIconThemeData *info;
1942 file = g_file_new_for_path (desktopdir);
1943 gtk_list_store_insert (store, &iter, pos);
1946 info = g_new0 (struct ChangeIconThemeData, 1);
1947 info->button = g_object_ref (button);
1948 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1949 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1951 gtk_tree_path_free (tree_path);
1953 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1954 "standard::icon,standard::display-name",
1955 model_add_special_get_info_cb, info);
1957 gtk_list_store_set (store, &iter,
1958 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1960 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1962 IS_FOLDER_COLUMN, TRUE,
1963 CANCELLABLE_COLUMN, cancellable,
1966 button->priv->n_special++;
1971 model_add_volumes (GtkFileChooserButton *button,
1974 GtkListStore *store;
1976 gboolean local_only;
1982 store = GTK_LIST_STORE (button->priv->model);
1983 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1984 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1986 for (l = volumes; l; l = l->next)
1988 GtkFileSystemVolume *volume;
1991 gchar *display_name;
1997 if (_gtk_file_system_volume_is_mounted (volume))
2001 base_file = _gtk_file_system_volume_get_root (volume);
2002 if (base_file != NULL)
2004 if (!_gtk_file_has_native_path (base_file))
2006 g_object_unref (base_file);
2010 g_object_unref (base_file);
2015 pixbuf = _gtk_file_system_volume_render_icon (volume,
2016 GTK_WIDGET (button),
2017 button->priv->icon_size,
2019 display_name = _gtk_file_system_volume_get_display_name (volume);
2021 gtk_list_store_insert (store, &iter, pos);
2022 gtk_list_store_set (store, &iter,
2023 ICON_COLUMN, pixbuf,
2024 DISPLAY_NAME_COLUMN, display_name,
2025 TYPE_COLUMN, ROW_TYPE_VOLUME,
2026 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
2027 IS_FOLDER_COLUMN, TRUE,
2031 g_object_unref (pixbuf);
2032 g_free (display_name);
2034 button->priv->n_volumes++;
2039 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
2042 model_add_bookmarks (GtkFileChooserButton *button,
2045 GtkListStore *store;
2048 gboolean local_only;
2054 store = GTK_LIST_STORE (button->priv->model);
2055 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
2056 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
2058 for (l = bookmarks; l; l = l->next)
2064 if (_gtk_file_has_native_path (file))
2066 gtk_list_store_insert (store, &iter, pos);
2067 gtk_list_store_set (store, &iter,
2069 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2070 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2071 DATA_COLUMN, g_object_ref (file),
2072 IS_FOLDER_COLUMN, FALSE,
2074 set_info_for_file_at_iter (button, file, &iter);
2079 GtkIconTheme *icon_theme;
2085 /* Don't call get_info for remote paths to avoid latency and
2087 * If we switch to a better bookmarks file format (XBEL), we
2088 * should use mime info to get a better icon.
2090 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2092 label = _gtk_file_chooser_label_for_file (file);
2094 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2095 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2096 button->priv->icon_size, 0, NULL);
2098 gtk_list_store_insert (store, &iter, pos);
2099 gtk_list_store_set (store, &iter,
2100 ICON_COLUMN, pixbuf,
2101 DISPLAY_NAME_COLUMN, label,
2102 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2103 DATA_COLUMN, g_object_ref (file),
2104 IS_FOLDER_COLUMN, TRUE,
2108 g_object_unref (pixbuf);
2111 button->priv->n_bookmarks++;
2115 if (button->priv->n_bookmarks > 0 &&
2116 !button->priv->has_bookmark_separator)
2118 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2120 gtk_list_store_insert (store, &iter, pos);
2121 gtk_list_store_set (store, &iter,
2123 DISPLAY_NAME_COLUMN, NULL,
2124 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2126 IS_FOLDER_COLUMN, FALSE,
2128 button->priv->has_bookmark_separator = TRUE;
2133 model_update_current_folder (GtkFileChooserButton *button,
2136 GtkListStore *store;
2143 store = GTK_LIST_STORE (button->priv->model);
2145 if (!button->priv->has_current_folder_separator)
2147 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
2148 gtk_list_store_insert (store, &iter, pos);
2149 gtk_list_store_set (store, &iter,
2151 DISPLAY_NAME_COLUMN, NULL,
2152 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
2154 IS_FOLDER_COLUMN, FALSE,
2156 button->priv->has_current_folder_separator = TRUE;
2159 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2160 if (!button->priv->has_current_folder)
2162 gtk_list_store_insert (store, &iter, pos);
2163 button->priv->has_current_folder = TRUE;
2167 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2168 model_free_row_data (button, &iter);
2171 if (g_file_is_native (file))
2173 gtk_list_store_set (store, &iter,
2175 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2176 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2177 DATA_COLUMN, g_object_ref (file),
2178 IS_FOLDER_COLUMN, FALSE,
2180 set_info_for_file_at_iter (button, file, &iter);
2185 GtkIconTheme *icon_theme;
2188 /* Don't call get_info for remote paths to avoid latency and
2190 * If we switch to a better bookmarks file format (XBEL), we
2191 * should use mime info to get a better icon.
2193 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2195 label = _gtk_file_chooser_label_for_file (file);
2197 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2199 if (g_file_is_native (file))
2200 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2201 button->priv->icon_size, 0, NULL);
2203 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2204 button->priv->icon_size, 0, NULL);
2206 gtk_list_store_set (store, &iter,
2207 ICON_COLUMN, pixbuf,
2208 DISPLAY_NAME_COLUMN, label,
2209 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2210 DATA_COLUMN, g_object_ref (file),
2211 IS_FOLDER_COLUMN, TRUE,
2215 g_object_unref (pixbuf);
2220 model_add_other (GtkFileChooserButton *button)
2222 GtkListStore *store;
2226 store = GTK_LIST_STORE (button->priv->model);
2227 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2229 gtk_list_store_insert (store, &iter, pos);
2230 gtk_list_store_set (store, &iter,
2232 DISPLAY_NAME_COLUMN, NULL,
2233 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2235 IS_FOLDER_COLUMN, FALSE,
2237 button->priv->has_other_separator = TRUE;
2240 gtk_list_store_insert (store, &iter, pos);
2241 gtk_list_store_set (store, &iter,
2243 DISPLAY_NAME_COLUMN, _("Other…"),
2244 TYPE_COLUMN, ROW_TYPE_OTHER,
2246 IS_FOLDER_COLUMN, FALSE,
2251 model_add_empty_selection (GtkFileChooserButton *button)
2253 GtkListStore *store;
2257 store = GTK_LIST_STORE (button->priv->model);
2258 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2260 gtk_list_store_insert (store, &iter, pos);
2261 gtk_list_store_set (store, &iter,
2263 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2264 TYPE_COLUMN, ROW_TYPE_EMPTY_SELECTION,
2266 IS_FOLDER_COLUMN, FALSE,
2271 model_remove_rows (GtkFileChooserButton *button,
2275 GtkListStore *store;
2280 store = GTK_LIST_STORE (button->priv->model);
2286 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2287 g_assert_not_reached ();
2289 model_free_row_data (button, &iter);
2290 gtk_list_store_remove (store, &iter);
2298 test_if_file_is_visible (GtkFileSystem *fs,
2300 gboolean local_only,
2306 if (local_only && !_gtk_file_has_native_path (file))
2316 filter_model_visible_func (GtkTreeModel *model,
2320 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2321 GtkFileChooserButtonPrivate *priv = button->priv;
2324 gboolean local_only, retval, is_folder;
2326 type = ROW_TYPE_INVALID;
2328 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2330 gtk_tree_model_get (model, iter,
2333 IS_FOLDER_COLUMN, &is_folder,
2338 case ROW_TYPE_CURRENT_FOLDER:
2341 case ROW_TYPE_SPECIAL:
2342 case ROW_TYPE_SHORTCUT:
2343 case ROW_TYPE_BOOKMARK:
2344 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2346 case ROW_TYPE_VOLUME:
2351 if (_gtk_file_system_volume_is_mounted (data))
2355 base_file = _gtk_file_system_volume_get_root (data);
2359 if (!_gtk_file_has_native_path (base_file))
2361 g_object_unref (base_file);
2369 case ROW_TYPE_EMPTY_SELECTION:
2371 gboolean popup_shown;
2373 g_object_get (priv->combo_box,
2374 "popup-shown", &popup_shown,
2383 /* When the combo box is not popped up... */
2385 selected = get_selected_file (button);
2387 retval = FALSE; /* ... nonempty selection means the ROW_TYPE_EMPTY_SELECTION is *not* visible... */
2389 retval = TRUE; /* ... and empty selection means the ROW_TYPE_EMPTY_SELECTION *is* visible */
2392 g_object_unref (selected);
2407 name_cell_data_func (GtkCellLayout *layout,
2408 GtkCellRenderer *cell,
2409 GtkTreeModel *model,
2416 gtk_tree_model_get (model, iter,
2420 if (type == ROW_TYPE_CURRENT_FOLDER)
2421 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2423 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2427 combo_box_row_separator_func (GtkTreeModel *model,
2431 gchar type = ROW_TYPE_INVALID;
2433 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2435 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2436 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2437 type == ROW_TYPE_OTHER_SEPARATOR);
2441 select_combo_box_row_no_notify (GtkFileChooserButton *button, int pos)
2443 GtkFileChooserButtonPrivate *priv = button->priv;
2444 GtkTreeIter iter, filter_iter;
2446 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2447 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2448 &filter_iter, &iter);
2450 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2451 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2452 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2456 update_combo_box (GtkFileChooserButton *button)
2458 GtkFileChooserButtonPrivate *priv = button->priv;
2463 file = get_selected_file (button);
2467 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2474 type = ROW_TYPE_INVALID;
2477 gtk_tree_model_get (priv->filter_model, &iter,
2484 case ROW_TYPE_SPECIAL:
2485 case ROW_TYPE_SHORTCUT:
2486 case ROW_TYPE_BOOKMARK:
2487 case ROW_TYPE_CURRENT_FOLDER:
2488 row_found = (file && g_file_equal (data, file));
2490 case ROW_TYPE_VOLUME:
2494 base_file = _gtk_file_system_volume_get_root (data);
2497 row_found = (file && g_file_equal (base_file, file));
2498 g_object_unref (base_file);
2509 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2510 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2512 g_signal_handler_unblock (priv->combo_box,
2513 priv->combo_box_changed_id);
2516 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2522 /* If it hasn't been found already, update & select the current-folder row. */
2525 model_update_current_folder (button, file);
2526 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2530 /* No selection; switch to that row */
2532 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2535 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2537 select_combo_box_row_no_notify (button, pos);
2541 g_object_unref (file);
2546 update_label_get_info_cb (GCancellable *cancellable,
2548 const GError *error,
2551 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2553 GtkFileChooserButton *button = data;
2554 GtkFileChooserButtonPrivate *priv = button->priv;
2556 if (cancellable != priv->update_button_cancellable)
2559 priv->update_button_cancellable = NULL;
2561 if (cancelled || error)
2564 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2566 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2569 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2571 priv->icon_size, 0, NULL);
2573 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2575 g_object_unref (pixbuf);
2578 emit_selection_changed_if_changing_selection (button);
2580 g_object_unref (button);
2581 g_object_unref (cancellable);
2585 update_label_and_image (GtkFileChooserButton *button)
2587 GtkFileChooserButtonPrivate *priv = button->priv;
2590 gboolean done_changing_selection;
2592 file = get_selected_file (button);
2595 done_changing_selection = FALSE;
2597 if (priv->update_button_cancellable)
2599 g_cancellable_cancel (priv->update_button_cancellable);
2600 priv->update_button_cancellable = NULL;
2605 GtkFileSystemVolume *volume = NULL;
2607 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2612 base_file = _gtk_file_system_volume_get_root (volume);
2613 if (base_file && g_file_equal (base_file, file))
2617 label_text = _gtk_file_system_volume_get_display_name (volume);
2618 pixbuf = _gtk_file_system_volume_render_icon (volume,
2619 GTK_WIDGET (button),
2622 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2624 g_object_unref (pixbuf);
2628 g_object_unref (base_file);
2630 _gtk_file_system_volume_unref (volume);
2634 done_changing_selection = TRUE;
2639 if (g_file_is_native (file))
2641 priv->update_button_cancellable =
2642 _gtk_file_system_get_info (priv->fs, file,
2643 "standard::icon,standard::display-name",
2644 update_label_get_info_cb,
2645 g_object_ref (button));
2651 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2652 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2654 priv->icon_size, 0, NULL);
2655 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2657 g_object_unref (pixbuf);
2659 done_changing_selection = TRUE;
2664 /* We know the selection is empty */
2665 done_changing_selection = TRUE;
2671 g_object_unref (file);
2675 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2676 g_free (label_text);
2680 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2681 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2684 if (done_changing_selection)
2685 emit_selection_changed_if_changing_selection (button);
2689 /* ************************ *
2690 * Child Object Callbacks *
2691 * ************************ */
2695 fs_volumes_changed_cb (GtkFileSystem *fs,
2698 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2699 GtkFileChooserButtonPrivate *priv = button->priv;
2702 model_remove_rows (user_data,
2703 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2706 priv->n_volumes = 0;
2708 volumes = _gtk_file_system_list_volumes (fs);
2709 model_add_volumes (user_data, volumes);
2710 g_slist_free (volumes);
2712 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2714 update_label_and_image (user_data);
2715 update_combo_box (user_data);
2719 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2722 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2723 GtkFileChooserButtonPrivate *priv = button->priv;
2726 bookmarks = _gtk_file_system_list_bookmarks (fs);
2727 model_remove_rows (user_data,
2728 model_get_type_position (user_data,
2729 ROW_TYPE_BOOKMARK_SEPARATOR),
2730 (priv->n_bookmarks + priv->has_bookmark_separator));
2731 priv->has_bookmark_separator = FALSE;
2732 priv->n_bookmarks = 0;
2733 model_add_bookmarks (user_data, bookmarks);
2734 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2735 g_slist_free (bookmarks);
2737 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2739 update_label_and_image (user_data);
2740 update_combo_box (user_data);
2744 save_inactive_state (GtkFileChooserButton *button)
2746 GtkFileChooserButtonPrivate *priv = button->priv;
2748 if (priv->current_folder_while_inactive)
2749 g_object_unref (priv->current_folder_while_inactive);
2751 if (priv->selection_while_inactive)
2752 g_object_unref (priv->selection_while_inactive);
2754 priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->dialog));
2755 priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2759 restore_inactive_state (GtkFileChooserButton *button)
2761 GtkFileChooserButtonPrivate *priv = button->priv;
2763 if (priv->current_folder_while_inactive)
2764 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), priv->current_folder_while_inactive, NULL);
2766 if (priv->selection_while_inactive)
2767 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2769 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2774 open_dialog (GtkFileChooserButton *button)
2776 GtkFileChooserButtonPrivate *priv = button->priv;
2778 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2780 if (!gtk_widget_get_visible (priv->dialog))
2782 GtkWidget *toplevel;
2784 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2786 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2788 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2789 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2790 GTK_WINDOW (toplevel));
2792 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2793 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2799 g_signal_handler_block (priv->dialog,
2800 priv->dialog_folder_changed_id);
2801 g_signal_handler_block (priv->dialog,
2802 priv->dialog_file_activated_id);
2803 g_signal_handler_block (priv->dialog,
2804 priv->dialog_selection_changed_id);
2806 restore_inactive_state (button);
2808 priv->active = TRUE;
2811 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2812 gtk_window_present (GTK_WINDOW (priv->dialog));
2817 combo_box_changed_cb (GtkComboBox *combo_box,
2822 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2824 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2825 GtkFileChooserButtonPrivate *priv = button->priv;
2829 type = ROW_TYPE_INVALID;
2832 gtk_tree_model_get (priv->filter_model, &iter,
2839 case ROW_TYPE_SPECIAL:
2840 case ROW_TYPE_SHORTCUT:
2841 case ROW_TYPE_BOOKMARK:
2842 case ROW_TYPE_CURRENT_FOLDER:
2844 gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), data, NULL);
2846 case ROW_TYPE_VOLUME:
2850 base_file = _gtk_file_system_volume_get_root (data);
2853 gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), base_file, NULL);
2854 g_object_unref (base_file);
2858 case ROW_TYPE_OTHER:
2859 open_dialog (user_data);
2867 /* Calback for the "notify::popup-shown" signal on the combo box.
2868 * When the combo is popped up, we don't want the ROW_TYPE_EMPTY_SELECTION to be visible
2869 * at all; otherwise we would be showing a "(None)" item in the combo box's popup.
2871 * However, when the combo box is *not* popped up, we want the empty-selection row
2872 * to be visible depending on the selection.
2874 * Since all that is done through the filter_model_visible_func(), this means
2875 * that we need to refilter the model when the combo box pops up - hence the
2876 * present signal handler.
2879 combo_box_notify_popup_shown_cb (GObject *object,
2883 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2884 GtkFileChooserButtonPrivate *priv = button->priv;
2885 gboolean popup_shown;
2887 g_object_get (priv->combo_box,
2888 "popup-shown", &popup_shown,
2891 /* Indicate that the ROW_TYPE_EMPTY_SELECTION will change visibility... */
2892 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2894 /* If the combo box popup got dismissed, go back to showing the ROW_TYPE_EMPTY_SELECTION if needed */
2897 GFile *selected = get_selected_file (button);
2903 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2904 select_combo_box_row_no_notify (button, pos);
2907 g_object_unref (selected);
2913 button_clicked_cb (GtkButton *real_button,
2916 open_dialog (user_data);
2921 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2924 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2926 g_signal_emit_by_name (button, "current-folder-changed");
2930 dialog_file_activated_cb (GtkFileChooser *dialog,
2933 g_signal_emit_by_name (user_data, "file-activated");
2937 dialog_selection_changed_cb (GtkFileChooser *dialog,
2940 update_label_and_image (user_data);
2941 update_combo_box (user_data);
2942 g_signal_emit_by_name (user_data, "selection-changed");
2946 dialog_update_preview_cb (GtkFileChooser *dialog,
2949 g_signal_emit_by_name (user_data, "update-preview");
2953 dialog_notify_cb (GObject *dialog,
2959 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2960 GTK_TYPE_FILE_CHOOSER);
2961 if (g_object_interface_find_property (iface, pspec->name))
2962 g_object_notify (user_data, pspec->name);
2964 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2966 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2967 GtkFileChooserButtonPrivate *priv = button->priv;
2969 if (priv->has_current_folder)
2975 pos = model_get_type_position (user_data,
2976 ROW_TYPE_CURRENT_FOLDER);
2977 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2980 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2982 /* If the path isn't local but we're in local-only mode now, remove
2983 * the custom-folder row */
2984 if (data && _gtk_file_has_native_path (G_FILE (data)) &&
2985 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2988 model_remove_rows (user_data, pos, 2);
2992 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2993 update_combo_box (user_data);
2998 dialog_delete_event_cb (GtkWidget *dialog,
3002 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
3008 dialog_response_cb (GtkDialog *dialog,
3012 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
3013 GtkFileChooserButtonPrivate *priv = button->priv;
3015 if (response == GTK_RESPONSE_ACCEPT ||
3016 response == GTK_RESPONSE_OK)
3018 save_inactive_state (button);
3020 g_signal_emit_by_name (button, "current-folder-changed");
3021 g_signal_emit_by_name (button, "selection-changed");
3025 restore_inactive_state (button);
3030 g_signal_handler_unblock (priv->dialog,
3031 priv->dialog_folder_changed_id);
3032 g_signal_handler_unblock (priv->dialog,
3033 priv->dialog_file_activated_id);
3034 g_signal_handler_unblock (priv->dialog,
3035 priv->dialog_selection_changed_id);
3036 priv->active = FALSE;
3039 update_label_and_image (button);
3040 update_combo_box (button);
3042 gtk_widget_set_sensitive (priv->combo_box, TRUE);
3043 gtk_widget_hide (priv->dialog);
3045 if (response == GTK_RESPONSE_ACCEPT ||
3046 response == GTK_RESPONSE_OK)
3047 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
3051 /* ************************************************************************** *
3053 * ************************************************************************** */
3056 * gtk_file_chooser_button_new:
3057 * @title: the title of the browse dialog.
3058 * @action: the open mode for the widget.
3060 * Creates a new file-selecting button widget.
3062 * Returns: a new button widget.
3067 gtk_file_chooser_button_new (const gchar *title,
3068 GtkFileChooserAction action)
3070 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3071 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
3073 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
3075 "title", (title ? title : _(DEFAULT_TITLE)),
3080 * gtk_file_chooser_button_new_with_dialog:
3081 * @dialog: the widget to use as dialog
3083 * Creates a #GtkFileChooserButton widget which uses @dialog as its
3084 * file-picking window.
3086 * Note that @dialog must be a #GtkDialog (or subclass) which
3087 * implements the #GtkFileChooser interface and must not have
3088 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
3090 * Also note that the dialog needs to have its confirmative button
3091 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
3092 * order for the button to take over the file selected in the dialog.
3094 * Returns: a new button widget.
3099 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
3101 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
3103 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
3109 * gtk_file_chooser_button_set_title:
3110 * @button: the button widget to modify.
3111 * @title: the new browse dialog title.
3113 * Modifies the @title of the browse dialog used by @button.
3118 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
3121 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3123 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
3124 g_object_notify (G_OBJECT (button), "title");
3128 * gtk_file_chooser_button_get_title:
3129 * @button: the button widget to examine.
3131 * Retrieves the title of the browse dialog used by @button. The returned value
3132 * should not be modified or freed.
3134 * Returns: a pointer to the browse dialog's title.
3139 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
3141 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
3143 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
3147 * gtk_file_chooser_button_get_width_chars:
3148 * @button: the button widget to examine.
3150 * Retrieves the width in characters of the @button widget's entry and/or label.
3152 * Returns: an integer width (in characters) that the button will use to size itself.
3157 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
3159 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
3161 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
3165 * gtk_file_chooser_button_set_width_chars:
3166 * @button: the button widget to examine.
3167 * @n_chars: the new width, in characters.
3169 * Sets the width (in characters) that @button will use to @n_chars.
3174 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
3177 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3179 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
3180 g_object_notify (G_OBJECT (button), "width-chars");
3184 * gtk_file_chooser_button_set_focus_on_click:
3185 * @button: a #GtkFileChooserButton
3186 * @focus_on_click: whether the button grabs focus when clicked with the mouse
3188 * Sets whether the button will grab focus when it is clicked with the mouse.
3189 * Making mouse clicks not grab focus is useful in places like toolbars where
3190 * you don't want the keyboard focus removed from the main area of the
3196 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
3197 gboolean focus_on_click)
3199 GtkFileChooserButtonPrivate *priv;
3201 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3203 priv = button->priv;
3205 focus_on_click = focus_on_click != FALSE;
3207 if (priv->focus_on_click != focus_on_click)
3209 priv->focus_on_click = focus_on_click;
3210 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
3211 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
3213 g_object_notify (G_OBJECT (button), "focus-on-click");
3218 * gtk_file_chooser_button_get_focus_on_click:
3219 * @button: a #GtkFileChooserButton
3221 * Returns whether the button grabs focus when it is clicked with the mouse.
3222 * See gtk_file_chooser_button_set_focus_on_click().
3224 * Return value: %TRUE if the button grabs focus when it is clicked with
3230 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
3232 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
3234 return button->priv->focus_on_click;