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;
225 /* ********************* *
226 * Function Prototypes *
227 * ********************* */
229 /* GtkFileChooserIface Functions */
230 static void gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
231 static gboolean gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
234 static GFile *gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser);
235 static gboolean gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
238 static void gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
240 static void gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser);
241 static GSList *gtk_file_chooser_button_get_files (GtkFileChooser *chooser);
242 static gboolean gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
245 static gboolean gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
249 /* GObject Functions */
250 static GObject *gtk_file_chooser_button_constructor (GType type,
252 GObjectConstructParam *params);
253 static void gtk_file_chooser_button_set_property (GObject *object,
257 static void gtk_file_chooser_button_get_property (GObject *object,
261 static void gtk_file_chooser_button_finalize (GObject *object);
263 /* GtkWidget Functions */
264 static void gtk_file_chooser_button_destroy (GtkWidget *widget);
265 static void gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
266 GdkDragContext *context,
269 GtkSelectionData *data,
272 static void gtk_file_chooser_button_show_all (GtkWidget *widget);
273 static void gtk_file_chooser_button_show (GtkWidget *widget);
274 static void gtk_file_chooser_button_hide (GtkWidget *widget);
275 static void gtk_file_chooser_button_map (GtkWidget *widget);
276 static gboolean gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
277 gboolean group_cycling);
278 static void gtk_file_chooser_button_style_updated (GtkWidget *widget);
279 static void gtk_file_chooser_button_screen_changed (GtkWidget *widget,
280 GdkScreen *old_screen);
282 /* Utility Functions */
283 static GtkIconTheme *get_icon_theme (GtkWidget *widget);
284 static void set_info_for_file_at_iter (GtkFileChooserButton *fs,
288 static gint model_get_type_position (GtkFileChooserButton *button,
290 static void model_free_row_data (GtkFileChooserButton *button,
292 static void model_add_special (GtkFileChooserButton *button);
293 static void model_add_other (GtkFileChooserButton *button);
294 static void model_add_empty_selection (GtkFileChooserButton *button);
295 static void model_add_volumes (GtkFileChooserButton *button,
297 static void model_add_bookmarks (GtkFileChooserButton *button,
299 static void model_update_current_folder (GtkFileChooserButton *button,
301 static void model_remove_rows (GtkFileChooserButton *button,
305 static gboolean filter_model_visible_func (GtkTreeModel *model,
309 static gboolean combo_box_row_separator_func (GtkTreeModel *model,
312 static void name_cell_data_func (GtkCellLayout *layout,
313 GtkCellRenderer *cell,
317 static void open_dialog (GtkFileChooserButton *button);
318 static void update_combo_box (GtkFileChooserButton *button);
319 static void update_label_and_image (GtkFileChooserButton *button);
321 /* Child Object Callbacks */
322 static void fs_volumes_changed_cb (GtkFileSystem *fs,
324 static void fs_bookmarks_changed_cb (GtkFileSystem *fs,
327 static void combo_box_changed_cb (GtkComboBox *combo_box,
329 static void combo_box_notify_popup_shown_cb (GObject *object,
333 static void button_clicked_cb (GtkButton *real_button,
336 static void dialog_update_preview_cb (GtkFileChooser *dialog,
338 static void dialog_selection_changed_cb (GtkFileChooser *dialog,
340 static void dialog_file_activated_cb (GtkFileChooser *dialog,
342 static void dialog_current_folder_changed_cb (GtkFileChooser *dialog,
344 static void dialog_notify_cb (GObject *dialog,
347 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
350 static void dialog_response_cb (GtkDialog *dialog,
354 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
356 /* ******************* *
357 * GType Declaration *
358 * ******************* */
360 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
361 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
365 /* ***************** *
367 * ***************** */
370 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
372 GObjectClass *gobject_class;
373 GtkWidgetClass *widget_class;
375 gobject_class = G_OBJECT_CLASS (class);
376 widget_class = GTK_WIDGET_CLASS (class);
378 gobject_class->constructor = gtk_file_chooser_button_constructor;
379 gobject_class->set_property = gtk_file_chooser_button_set_property;
380 gobject_class->get_property = gtk_file_chooser_button_get_property;
381 gobject_class->finalize = gtk_file_chooser_button_finalize;
383 widget_class->destroy = gtk_file_chooser_button_destroy;
384 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
385 widget_class->show_all = gtk_file_chooser_button_show_all;
386 widget_class->show = gtk_file_chooser_button_show;
387 widget_class->hide = gtk_file_chooser_button_hide;
388 widget_class->map = gtk_file_chooser_button_map;
389 widget_class->style_updated = gtk_file_chooser_button_style_updated;
390 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
391 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
394 * GtkFileChooserButton::file-set:
395 * @widget: the object which received the signal.
397 * The ::file-set signal is emitted when the user selects a file.
399 * Note that this signal is only emitted when the <emphasis>user</emphasis>
404 file_chooser_button_signals[FILE_SET] =
405 g_signal_new (I_("file-set"),
406 G_TYPE_FROM_CLASS (gobject_class),
408 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
410 _gtk_marshal_VOID__VOID,
414 * GtkFileChooserButton:dialog:
416 * Instance of the #GtkFileChooserDialog associated with the button.
420 g_object_class_install_property (gobject_class, PROP_DIALOG,
421 g_param_spec_object ("dialog",
423 P_("The file chooser dialog to use."),
424 GTK_TYPE_FILE_CHOOSER,
425 (GTK_PARAM_WRITABLE |
426 G_PARAM_CONSTRUCT_ONLY)));
429 * GtkFileChooserButton:focus-on-click:
431 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
436 g_object_class_install_property (gobject_class,
438 g_param_spec_boolean ("focus-on-click",
439 P_("Focus on click"),
440 P_("Whether the button grabs focus when it is clicked with the mouse"),
442 GTK_PARAM_READWRITE));
445 * GtkFileChooserButton:title:
447 * Title to put on the #GtkFileChooserDialog associated with the button.
451 g_object_class_install_property (gobject_class, PROP_TITLE,
452 g_param_spec_string ("title",
454 P_("The title of the file chooser dialog."),
456 GTK_PARAM_READWRITE));
459 * GtkFileChooserButton:width-chars:
461 * The width of the entry and label inside the button, in characters.
465 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
466 g_param_spec_int ("width-chars",
467 P_("Width In Characters"),
468 P_("The desired width of the button widget, in characters."),
470 GTK_PARAM_READWRITE));
472 _gtk_file_chooser_install_properties (gobject_class);
474 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
478 gtk_file_chooser_button_init (GtkFileChooserButton *button)
480 GtkFileChooserButtonPrivate *priv;
481 GtkWidget *box, *image, *sep;
482 GtkTargetList *target_list;
484 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
485 GTK_TYPE_FILE_CHOOSER_BUTTON,
486 GtkFileChooserButtonPrivate);
488 priv->icon_size = FALLBACK_ICON_SIZE;
489 priv->focus_on_click = TRUE;
491 gtk_widget_push_composite_child ();
494 priv->button = gtk_button_new ();
495 g_signal_connect (priv->button, "clicked",
496 G_CALLBACK (button_clicked_cb), button);
497 gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
498 gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
499 gtk_widget_show (priv->button);
501 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
502 gtk_container_add (GTK_CONTAINER (priv->button), box);
503 gtk_widget_show (box);
505 priv->image = gtk_image_new ();
506 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
507 gtk_widget_show (priv->image);
509 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
510 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
511 gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
512 gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
513 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
514 //gtk_container_add (GTK_CONTAINER (box), priv->label);
515 gtk_widget_show (priv->label);
517 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
518 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
519 gtk_widget_show (sep);
521 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
522 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
523 gtk_widget_show (image);
526 /* Keep in sync with columns enum, line 88 */
528 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
529 GDK_TYPE_PIXBUF, /* ICON_COLUMN */
530 G_TYPE_STRING, /* DISPLAY_NAME_COLUMN */
531 G_TYPE_CHAR, /* TYPE_COLUMN */
532 G_TYPE_POINTER /* DATA_COLUMN (Volume || Path) */,
533 G_TYPE_BOOLEAN /* IS_FOLDER_COLUMN */,
534 G_TYPE_POINTER /* CANCELLABLE_COLUMN */));
536 priv->combo_box = gtk_combo_box_new ();
537 priv->combo_box_changed_id = g_signal_connect (priv->combo_box, "changed",
538 G_CALLBACK (combo_box_changed_cb), button);
540 g_signal_connect (priv->combo_box, "notify::popup-shown",
541 G_CALLBACK (combo_box_notify_popup_shown_cb), button);
543 gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
544 gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
546 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
547 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
548 priv->icon_cell, FALSE);
549 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
550 priv->icon_cell, "pixbuf", ICON_COLUMN);
552 priv->name_cell = gtk_cell_renderer_text_new ();
553 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
554 priv->name_cell, TRUE);
555 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
556 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
557 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
558 priv->name_cell, name_cell_data_func,
561 gtk_widget_pop_composite_child ();
564 gtk_drag_dest_set (GTK_WIDGET (button),
565 (GTK_DEST_DEFAULT_ALL),
568 target_list = gtk_target_list_new (NULL, 0);
569 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
570 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
571 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
572 gtk_target_list_unref (target_list);
576 /* ******************************* *
577 * GtkFileChooserIface Functions *
578 * ******************************* */
580 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
582 _gtk_file_chooser_delegate_iface_init (iface);
584 iface->set_current_folder = gtk_file_chooser_button_set_current_folder;
585 iface->get_current_folder = gtk_file_chooser_button_get_current_folder;
586 iface->select_file = gtk_file_chooser_button_select_file;
587 iface->unselect_file = gtk_file_chooser_button_unselect_file;
588 iface->unselect_all = gtk_file_chooser_button_unselect_all;
589 iface->get_files = gtk_file_chooser_button_get_files;
590 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
591 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
595 gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
599 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
600 GtkFileChooserButtonPrivate *priv = button->priv;
601 GtkFileChooser *delegate;
603 delegate = g_object_get_qdata (G_OBJECT (chooser),
604 GTK_FILE_CHOOSER_DELEGATE_QUARK);
607 return gtk_file_chooser_set_current_folder_file (delegate, file, error);
610 if (priv->current_folder_while_inactive)
611 g_object_unref (priv->current_folder_while_inactive);
613 priv->current_folder_while_inactive = g_object_ref (file);
615 update_combo_box (button);
617 g_signal_emit_by_name (button, "current-folder-changed");
624 gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser)
626 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
627 GtkFileChooserButtonPrivate *priv = button->priv;
628 GtkFileChooser *delegate;
630 delegate = g_object_get_qdata (G_OBJECT (chooser),
631 GTK_FILE_CHOOSER_DELEGATE_QUARK);
634 return gtk_file_chooser_get_current_folder_file (delegate);
637 if (priv->current_folder_while_inactive)
638 return g_object_ref (priv->current_folder_while_inactive);
645 gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
649 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
650 GtkFileChooserButtonPrivate *priv = button->priv;
651 GtkFileChooser *delegate;
653 delegate = g_object_get_qdata (G_OBJECT (chooser),
654 GTK_FILE_CHOOSER_DELEGATE_QUARK);
657 return gtk_file_chooser_select_file (delegate, file, error);
660 if (priv->selection_while_inactive)
661 g_object_unref (priv->selection_while_inactive);
663 priv->selection_while_inactive = g_object_ref (file);
665 update_label_and_image (button);
666 update_combo_box (button);
673 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
676 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
677 GtkFileChooserButtonPrivate *priv = button->priv;
678 GtkFileChooser *delegate;
680 delegate = g_object_get_qdata (G_OBJECT (chooser),
681 GTK_FILE_CHOOSER_DELEGATE_QUARK);
684 gtk_file_chooser_unselect_file (delegate, file);
687 if (g_file_equal (priv->selection_while_inactive, file))
689 if (priv->selection_while_inactive)
691 g_object_unref (priv->selection_while_inactive);
692 priv->selection_while_inactive = NULL;
695 update_label_and_image (button);
696 update_combo_box (button);
702 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
704 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
705 GtkFileChooserButtonPrivate *priv = button->priv;
706 GtkFileChooser *delegate;
708 delegate = g_object_get_qdata (G_OBJECT (chooser),
709 GTK_FILE_CHOOSER_DELEGATE_QUARK);
712 gtk_file_chooser_unselect_all (delegate);
715 if (priv->selection_while_inactive)
717 g_object_unref (priv->selection_while_inactive);
718 priv->selection_while_inactive = NULL;
720 update_label_and_image (button);
721 update_combo_box (button);
727 get_selected_file (GtkFileChooserButton *button)
729 GtkFileChooserButtonPrivate *priv = button->priv;
732 return gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
735 if (priv->selection_while_inactive)
736 return g_object_ref (priv->selection_while_inactive);
737 else if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
739 /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
740 * the current folder, since that is what GtkFileChooserDefault would do.
742 if (priv->current_folder_while_inactive)
743 return g_object_ref (priv->current_folder_while_inactive);
751 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
753 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
755 return g_slist_prepend (NULL, get_selected_file (button));
759 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
763 GtkFileChooser *delegate;
766 delegate = g_object_get_qdata (G_OBJECT (chooser),
767 GTK_FILE_CHOOSER_DELEGATE_QUARK);
768 retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
772 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
773 GtkFileChooserButtonPrivate *priv = button->priv;
777 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
778 pos += priv->n_shortcuts;
780 gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
781 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
783 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
784 TYPE_COLUMN, ROW_TYPE_SHORTCUT,
785 DATA_COLUMN, g_object_ref (file),
786 IS_FOLDER_COLUMN, FALSE,
788 set_info_for_file_at_iter (button, file, &iter);
791 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
798 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
802 GtkFileChooser *delegate;
805 delegate = g_object_get_qdata (G_OBJECT (chooser),
806 GTK_FILE_CHOOSER_DELEGATE_QUARK);
808 retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
812 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
813 GtkFileChooserButtonPrivate *priv = button->priv;
818 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
819 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
825 gtk_tree_model_get (priv->model, &iter,
830 if (type == ROW_TYPE_SHORTCUT &&
831 data && g_file_equal (data, file))
833 model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
834 gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
836 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
837 update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
841 while (type == ROW_TYPE_SHORTCUT &&
842 gtk_tree_model_iter_next (priv->model, &iter));
849 /* ******************* *
850 * GObject Functions *
851 * ******************* */
854 gtk_file_chooser_button_constructor (GType type,
856 GObjectConstructParam *params)
859 GtkFileChooserButton *button;
860 GtkFileChooserButtonPrivate *priv;
863 object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
866 button = GTK_FILE_CHOOSER_BUTTON (object);
871 priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
872 GTK_FILE_CHOOSER_ACTION_OPEN,
879 gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
880 GTK_RESPONSE_ACCEPT);
881 gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
886 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
888 else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
890 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
893 g_signal_connect (priv->dialog, "delete-event",
894 G_CALLBACK (dialog_delete_event_cb), object);
895 g_signal_connect (priv->dialog, "response",
896 G_CALLBACK (dialog_response_cb), object);
898 /* This is used, instead of the standard delegate, to ensure that signals are only
899 * delegated when the OK button is pressed. */
900 g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
901 priv->dialog_folder_changed_id =
902 g_signal_connect (priv->dialog, "current-folder-changed",
903 G_CALLBACK (dialog_current_folder_changed_cb), object);
904 priv->dialog_file_activated_id =
905 g_signal_connect (priv->dialog, "file-activated",
906 G_CALLBACK (dialog_file_activated_cb), object);
907 priv->dialog_selection_changed_id =
908 g_signal_connect (priv->dialog, "selection-changed",
909 G_CALLBACK (dialog_selection_changed_cb), object);
910 g_signal_connect (priv->dialog, "update-preview",
911 G_CALLBACK (dialog_update_preview_cb), object);
912 g_signal_connect (priv->dialog, "notify",
913 G_CALLBACK (dialog_notify_cb), object);
914 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
915 (gpointer) (&priv->dialog));
918 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
920 model_add_special (button);
922 list = _gtk_file_system_list_volumes (priv->fs);
923 model_add_volumes (button, list);
926 list = _gtk_file_system_list_bookmarks (priv->fs);
927 model_add_bookmarks (button, list);
928 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
931 model_add_other (button);
933 model_add_empty_selection (button);
935 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
936 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
937 filter_model_visible_func,
940 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
941 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
942 combo_box_row_separator_func,
945 /* set up the action for a user-provided dialog, this also updates
946 * the label, image and combobox
948 g_object_set (object,
949 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
952 priv->fs_volumes_changed_id =
953 g_signal_connect (priv->fs, "volumes-changed",
954 G_CALLBACK (fs_volumes_changed_cb), object);
955 priv->fs_bookmarks_changed_id =
956 g_signal_connect (priv->fs, "bookmarks-changed",
957 G_CALLBACK (fs_bookmarks_changed_cb), object);
959 update_label_and_image (button);
960 update_combo_box (button);
966 gtk_file_chooser_button_set_property (GObject *object,
971 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
972 GtkFileChooserButtonPrivate *priv = button->priv;
978 priv->dialog = g_value_get_object (value);
980 case PROP_FOCUS_ON_CLICK:
981 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
983 case PROP_WIDTH_CHARS:
984 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
985 g_value_get_int (value));
987 case GTK_FILE_CHOOSER_PROP_ACTION:
988 switch (g_value_get_enum (value))
990 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
991 case GTK_FILE_CHOOSER_ACTION_SAVE:
996 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
997 eval = g_enum_get_value (eclass, g_value_get_enum (value));
998 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
999 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
1001 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
1006 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1007 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
1008 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
1010 switch (g_value_get_enum (value))
1012 case GTK_FILE_CHOOSER_ACTION_OPEN:
1013 gtk_widget_hide (priv->combo_box);
1014 gtk_widget_show (priv->button);
1016 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1017 gtk_widget_hide (priv->button);
1018 gtk_widget_show (priv->combo_box);
1021 g_assert_not_reached ();
1027 case GTK_FILE_CHOOSER_PROP_FILTER:
1028 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1029 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1030 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1031 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1032 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1033 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1034 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1035 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1038 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1039 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1040 fs_volumes_changed_cb (priv->fs, button);
1041 fs_bookmarks_changed_cb (priv->fs, button);
1044 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1045 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
1046 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
1049 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1055 gtk_file_chooser_button_get_property (GObject *object,
1060 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1061 GtkFileChooserButtonPrivate *priv = button->priv;
1065 case PROP_WIDTH_CHARS:
1066 g_value_set_int (value,
1067 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
1069 case PROP_FOCUS_ON_CLICK:
1070 g_value_set_boolean (value,
1071 gtk_file_chooser_button_get_focus_on_click (button));
1075 case GTK_FILE_CHOOSER_PROP_ACTION:
1076 case GTK_FILE_CHOOSER_PROP_FILTER:
1077 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1078 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1079 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1080 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1081 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1082 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1083 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1084 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1085 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1086 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1090 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1096 gtk_file_chooser_button_finalize (GObject *object)
1098 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1099 GtkFileChooserButtonPrivate *priv = button->priv;
1101 if (priv->selection_while_inactive)
1102 g_object_unref (priv->selection_while_inactive);
1104 if (priv->current_folder_while_inactive)
1105 g_object_unref (priv->current_folder_while_inactive);
1107 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1110 /* ********************* *
1111 * GtkWidget Functions *
1112 * ********************* */
1115 gtk_file_chooser_button_destroy (GtkWidget *widget)
1117 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1118 GtkFileChooserButtonPrivate *priv = button->priv;
1122 if (priv->dialog != NULL)
1124 gtk_widget_destroy (priv->dialog);
1125 priv->dialog = NULL;
1128 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1130 model_free_row_data (button, &iter);
1132 while (gtk_tree_model_iter_next (priv->model, &iter));
1134 if (priv->dnd_select_folder_cancellable)
1136 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1137 priv->dnd_select_folder_cancellable = NULL;
1140 if (priv->update_button_cancellable)
1142 g_cancellable_cancel (priv->update_button_cancellable);
1143 priv->update_button_cancellable = NULL;
1146 if (priv->change_icon_theme_cancellables)
1148 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1150 GCancellable *cancellable = G_CANCELLABLE (l->data);
1151 g_cancellable_cancel (cancellable);
1153 g_slist_free (priv->change_icon_theme_cancellables);
1154 priv->change_icon_theme_cancellables = NULL;
1159 g_object_unref (priv->model);
1163 if (priv->filter_model)
1165 g_object_unref (priv->filter_model);
1166 priv->filter_model = NULL;
1171 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1172 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1173 g_object_unref (priv->fs);
1177 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1180 struct DndSelectFolderData
1182 GtkFileSystem *file_system;
1183 GtkFileChooserButton *button;
1184 GtkFileChooserAction action;
1192 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1194 const GError *error,
1197 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1198 struct DndSelectFolderData *data = user_data;
1200 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1202 g_object_unref (data->button);
1203 g_object_unref (data->file);
1204 g_strfreev (data->uris);
1207 g_object_unref (cancellable);
1211 data->button->priv->dnd_select_folder_cancellable = NULL;
1213 if (!cancelled && !error && info != NULL)
1217 is_folder = _gtk_file_info_consider_as_directory (info);
1220 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1221 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1222 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1226 data->selected = FALSE;
1228 if (data->selected || data->uris[++data->i] == NULL)
1230 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1232 g_object_unref (data->button);
1233 g_object_unref (data->file);
1234 g_strfreev (data->uris);
1237 g_object_unref (cancellable);
1242 g_object_unref (data->file);
1244 data->file = g_file_new_for_uri (data->uris[data->i]);
1246 data->button->priv->dnd_select_folder_cancellable =
1247 _gtk_file_system_get_info (data->file_system, data->file,
1249 dnd_select_folder_get_info_cb, user_data);
1251 g_object_unref (cancellable);
1255 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1256 GdkDragContext *context,
1259 GtkSelectionData *data,
1263 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1264 GtkFileChooserButtonPrivate *priv = button->priv;
1268 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1269 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1275 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1283 struct DndSelectFolderData *info;
1285 uris = gtk_selection_data_get_uris (data);
1290 info = g_new0 (struct DndSelectFolderData, 1);
1291 info->button = g_object_ref (button);
1294 info->selected = FALSE;
1295 info->file_system = priv->fs;
1296 g_object_get (priv->dialog, "action", &info->action, NULL);
1298 info->file = g_file_new_for_uri (info->uris[info->i]);
1300 if (priv->dnd_select_folder_cancellable)
1301 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1303 priv->dnd_select_folder_cancellable =
1304 _gtk_file_system_get_info (priv->fs, info->file,
1306 dnd_select_folder_get_info_cb, info);
1311 text = (char*) gtk_selection_data_get_text (data);
1312 file = g_file_new_for_uri (text);
1313 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1315 g_object_unref (file);
1317 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1324 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1328 gtk_file_chooser_button_show_all (GtkWidget *widget)
1330 gtk_widget_show (widget);
1334 gtk_file_chooser_button_show (GtkWidget *widget)
1336 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1337 GtkFileChooserButtonPrivate *priv = button->priv;
1339 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1340 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1343 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1347 gtk_file_chooser_button_hide (GtkWidget *widget)
1349 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1350 GtkFileChooserButtonPrivate *priv = button->priv;
1352 gtk_widget_hide (priv->dialog);
1354 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1355 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1359 gtk_file_chooser_button_map (GtkWidget *widget)
1361 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1365 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1366 gboolean group_cycling)
1368 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1369 GtkFileChooserButtonPrivate *priv = button->priv;
1371 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1373 case GTK_FILE_CHOOSER_ACTION_OPEN:
1374 gtk_widget_grab_focus (priv->button);
1376 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1377 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1380 g_assert_not_reached ();
1387 /* Changes the icons wherever it is needed */
1388 struct ChangeIconThemeData
1390 GtkFileChooserButton *button;
1391 GtkTreeRowReference *row_ref;
1395 change_icon_theme_get_info_cb (GCancellable *cancellable,
1397 const GError *error,
1400 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1402 struct ChangeIconThemeData *data = user_data;
1404 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1407 data->button->priv->change_icon_theme_cancellables =
1408 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1410 if (cancelled || error)
1413 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1421 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1423 path = gtk_tree_row_reference_get_path (data->row_ref);
1426 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1427 gtk_tree_path_free (path);
1429 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1430 ICON_COLUMN, pixbuf,
1433 g_object_set (data->button->priv->icon_cell,
1437 g_object_unref (pixbuf);
1441 g_object_unref (data->button);
1442 gtk_tree_row_reference_free (data->row_ref);
1445 g_object_unref (cancellable);
1449 change_icon_theme (GtkFileChooserButton *button)
1451 GtkFileChooserButtonPrivate *priv = button->priv;
1452 GtkSettings *settings;
1453 GtkIconTheme *theme;
1456 gint width = 0, height = 0;
1458 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1460 GCancellable *cancellable = G_CANCELLABLE (l->data);
1461 g_cancellable_cancel (cancellable);
1463 g_slist_free (button->priv->change_icon_theme_cancellables);
1464 button->priv->change_icon_theme_cancellables = NULL;
1466 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1468 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1470 priv->icon_size = MAX (width, height);
1472 priv->icon_size = FALLBACK_ICON_SIZE;
1474 update_label_and_image (button);
1476 gtk_tree_model_get_iter_first (priv->model, &iter);
1478 theme = get_icon_theme (GTK_WIDGET (button));
1486 type = ROW_TYPE_INVALID;
1487 gtk_tree_model_get (priv->model, &iter,
1494 case ROW_TYPE_SPECIAL:
1495 case ROW_TYPE_SHORTCUT:
1496 case ROW_TYPE_BOOKMARK:
1497 case ROW_TYPE_CURRENT_FOLDER:
1500 if (g_file_is_native (G_FILE (data)))
1503 GCancellable *cancellable;
1504 struct ChangeIconThemeData *info;
1506 info = g_new0 (struct ChangeIconThemeData, 1);
1507 info->button = g_object_ref (button);
1508 path = gtk_tree_model_get_path (priv->model, &iter);
1509 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1510 gtk_tree_path_free (path);
1513 _gtk_file_system_get_info (priv->fs, data,
1515 change_icon_theme_get_info_cb,
1517 button->priv->change_icon_theme_cancellables =
1518 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1522 /* Don't call get_info for remote paths to avoid latency and
1524 * If we switch to a better bookmarks file format (XBEL), we
1525 * should use mime info to get a better icon.
1527 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1528 priv->icon_size, 0, NULL);
1531 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1532 priv->icon_size, 0, NULL);
1534 case ROW_TYPE_VOLUME:
1536 pixbuf = _gtk_file_system_volume_render_icon (data,
1537 GTK_WIDGET (button),
1541 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1542 priv->icon_size, 0, NULL);
1550 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1552 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1553 ICON_COLUMN, pixbuf,
1557 g_object_unref (pixbuf);
1559 while (gtk_tree_model_iter_next (priv->model, &iter));
1561 g_object_set (button->priv->icon_cell,
1567 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1569 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1571 if (gtk_widget_has_screen (widget))
1572 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1576 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1577 GdkScreen *old_screen)
1579 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1580 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1583 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1587 /* ******************* *
1588 * Utility Functions *
1589 * ******************* */
1592 static GtkIconTheme *
1593 get_icon_theme (GtkWidget *widget)
1595 if (gtk_widget_has_screen (widget))
1596 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1598 return gtk_icon_theme_get_default ();
1602 struct SetDisplayNameData
1604 GtkFileChooserButton *button;
1606 GtkTreeRowReference *row_ref;
1610 set_info_get_info_cb (GCancellable *cancellable,
1612 const GError *error,
1613 gpointer callback_data)
1615 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1619 GCancellable *model_cancellable = NULL;
1620 struct SetDisplayNameData *data = callback_data;
1623 if (!data->button->priv->model)
1624 /* button got destroyed */
1627 path = gtk_tree_row_reference_get_path (data->row_ref);
1629 /* Cancellable doesn't exist anymore in the model */
1632 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1633 gtk_tree_path_free (path);
1635 /* Validate the cancellable */
1636 gtk_tree_model_get (data->button->priv->model, &iter,
1637 CANCELLABLE_COLUMN, &model_cancellable,
1639 if (cancellable != model_cancellable)
1642 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1643 CANCELLABLE_COLUMN, NULL,
1646 if (cancelled || error)
1647 /* There was an error, leave the fallback name in there */
1650 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1653 data->label = g_strdup (g_file_info_get_display_name (info));
1655 is_folder = _gtk_file_info_consider_as_directory (info);
1657 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1658 ICON_COLUMN, pixbuf,
1659 DISPLAY_NAME_COLUMN, data->label,
1660 IS_FOLDER_COLUMN, is_folder,
1664 g_object_unref (pixbuf);
1667 g_object_unref (data->button);
1668 g_free (data->label);
1669 gtk_tree_row_reference_free (data->row_ref);
1672 if (model_cancellable)
1673 g_object_unref (model_cancellable);
1677 set_info_for_file_at_iter (GtkFileChooserButton *button,
1681 struct SetDisplayNameData *data;
1682 GtkTreePath *tree_path;
1683 GCancellable *cancellable;
1685 data = g_new0 (struct SetDisplayNameData, 1);
1686 data->button = g_object_ref (button);
1687 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1689 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1690 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1691 gtk_tree_path_free (tree_path);
1693 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1694 "standard::type,standard::icon,standard::display-name",
1695 set_info_get_info_cb, data);
1697 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1698 CANCELLABLE_COLUMN, cancellable,
1702 /* Shortcuts Model */
1704 model_get_type_position (GtkFileChooserButton *button,
1709 if (row_type == ROW_TYPE_SPECIAL)
1712 retval += button->priv->n_special;
1714 if (row_type == ROW_TYPE_VOLUME)
1717 retval += button->priv->n_volumes;
1719 if (row_type == ROW_TYPE_SHORTCUT)
1722 retval += button->priv->n_shortcuts;
1724 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1727 retval += button->priv->has_bookmark_separator;
1729 if (row_type == ROW_TYPE_BOOKMARK)
1732 retval += button->priv->n_bookmarks;
1734 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1737 retval += button->priv->has_current_folder_separator;
1739 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1742 retval += button->priv->has_current_folder;
1744 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1747 retval += button->priv->has_other_separator;
1749 if (row_type == ROW_TYPE_OTHER)
1754 if (row_type == ROW_TYPE_EMPTY_SELECTION)
1757 g_assert_not_reached ();
1762 model_free_row_data (GtkFileChooserButton *button,
1767 GCancellable *cancellable;
1769 gtk_tree_model_get (button->priv->model, iter,
1772 CANCELLABLE_COLUMN, &cancellable,
1777 g_cancellable_cancel (cancellable);
1778 g_object_unref (cancellable);
1783 case ROW_TYPE_SPECIAL:
1784 case ROW_TYPE_SHORTCUT:
1785 case ROW_TYPE_BOOKMARK:
1786 case ROW_TYPE_CURRENT_FOLDER:
1787 g_object_unref (data);
1789 case ROW_TYPE_VOLUME:
1790 _gtk_file_system_volume_unref (data);
1798 model_add_special_get_info_cb (GCancellable *cancellable,
1800 const GError *error,
1803 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1807 GCancellable *model_cancellable = NULL;
1808 struct ChangeIconThemeData *data = user_data;
1811 if (!data->button->priv->model)
1812 /* button got destroyed */
1815 path = gtk_tree_row_reference_get_path (data->row_ref);
1817 /* Cancellable doesn't exist anymore in the model */
1820 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1821 gtk_tree_path_free (path);
1823 gtk_tree_model_get (data->button->priv->model, &iter,
1824 CANCELLABLE_COLUMN, &model_cancellable,
1826 if (cancellable != model_cancellable)
1829 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1830 CANCELLABLE_COLUMN, NULL,
1833 if (cancelled || error)
1836 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1840 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1841 ICON_COLUMN, pixbuf,
1843 g_object_unref (pixbuf);
1846 gtk_tree_model_get (data->button->priv->model, &iter,
1847 DISPLAY_NAME_COLUMN, &name,
1850 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1851 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1856 g_object_unref (data->button);
1857 gtk_tree_row_reference_free (data->row_ref);
1860 if (model_cancellable)
1861 g_object_unref (model_cancellable);
1865 model_add_special (GtkFileChooserButton *button)
1867 const gchar *homedir;
1868 const gchar *desktopdir;
1869 GtkListStore *store;
1874 store = GTK_LIST_STORE (button->priv->model);
1875 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1877 homedir = g_get_home_dir ();
1881 GtkTreePath *tree_path;
1882 GCancellable *cancellable;
1883 struct ChangeIconThemeData *info;
1885 file = g_file_new_for_path (homedir);
1886 gtk_list_store_insert (store, &iter, pos);
1889 info = g_new0 (struct ChangeIconThemeData, 1);
1890 info->button = g_object_ref (button);
1891 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1892 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1894 gtk_tree_path_free (tree_path);
1896 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1897 "standard::icon,standard::display-name",
1898 model_add_special_get_info_cb, info);
1900 gtk_list_store_set (store, &iter,
1902 DISPLAY_NAME_COLUMN, NULL,
1903 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1905 IS_FOLDER_COLUMN, TRUE,
1906 CANCELLABLE_COLUMN, cancellable,
1909 button->priv->n_special++;
1912 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1914 /* "To disable a directory, point it to the homedir."
1915 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1917 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1919 GtkTreePath *tree_path;
1920 GCancellable *cancellable;
1921 struct ChangeIconThemeData *info;
1923 file = g_file_new_for_path (desktopdir);
1924 gtk_list_store_insert (store, &iter, pos);
1927 info = g_new0 (struct ChangeIconThemeData, 1);
1928 info->button = g_object_ref (button);
1929 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1930 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1932 gtk_tree_path_free (tree_path);
1934 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1935 "standard::icon,standard::display-name",
1936 model_add_special_get_info_cb, info);
1938 gtk_list_store_set (store, &iter,
1939 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1941 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1943 IS_FOLDER_COLUMN, TRUE,
1944 CANCELLABLE_COLUMN, cancellable,
1947 button->priv->n_special++;
1952 model_add_volumes (GtkFileChooserButton *button,
1955 GtkListStore *store;
1957 gboolean local_only;
1963 store = GTK_LIST_STORE (button->priv->model);
1964 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1965 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1967 for (l = volumes; l; l = l->next)
1969 GtkFileSystemVolume *volume;
1972 gchar *display_name;
1978 if (_gtk_file_system_volume_is_mounted (volume))
1982 base_file = _gtk_file_system_volume_get_root (volume);
1983 if (base_file != NULL)
1985 if (!_gtk_file_has_native_path (base_file))
1987 g_object_unref (base_file);
1991 g_object_unref (base_file);
1996 pixbuf = _gtk_file_system_volume_render_icon (volume,
1997 GTK_WIDGET (button),
1998 button->priv->icon_size,
2000 display_name = _gtk_file_system_volume_get_display_name (volume);
2002 gtk_list_store_insert (store, &iter, pos);
2003 gtk_list_store_set (store, &iter,
2004 ICON_COLUMN, pixbuf,
2005 DISPLAY_NAME_COLUMN, display_name,
2006 TYPE_COLUMN, ROW_TYPE_VOLUME,
2007 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
2008 IS_FOLDER_COLUMN, TRUE,
2012 g_object_unref (pixbuf);
2013 g_free (display_name);
2015 button->priv->n_volumes++;
2020 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
2023 model_add_bookmarks (GtkFileChooserButton *button,
2026 GtkListStore *store;
2029 gboolean local_only;
2035 store = GTK_LIST_STORE (button->priv->model);
2036 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
2037 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
2039 for (l = bookmarks; l; l = l->next)
2045 if (_gtk_file_has_native_path (file))
2047 gtk_list_store_insert (store, &iter, pos);
2048 gtk_list_store_set (store, &iter,
2050 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2051 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2052 DATA_COLUMN, g_object_ref (file),
2053 IS_FOLDER_COLUMN, FALSE,
2055 set_info_for_file_at_iter (button, file, &iter);
2060 GtkIconTheme *icon_theme;
2066 /* Don't call get_info for remote paths to avoid latency and
2068 * If we switch to a better bookmarks file format (XBEL), we
2069 * should use mime info to get a better icon.
2071 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2073 label = _gtk_file_chooser_label_for_file (file);
2075 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2076 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2077 button->priv->icon_size, 0, NULL);
2079 gtk_list_store_insert (store, &iter, pos);
2080 gtk_list_store_set (store, &iter,
2081 ICON_COLUMN, pixbuf,
2082 DISPLAY_NAME_COLUMN, label,
2083 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2084 DATA_COLUMN, g_object_ref (file),
2085 IS_FOLDER_COLUMN, TRUE,
2089 g_object_unref (pixbuf);
2092 button->priv->n_bookmarks++;
2096 if (button->priv->n_bookmarks > 0 &&
2097 !button->priv->has_bookmark_separator)
2099 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2101 gtk_list_store_insert (store, &iter, pos);
2102 gtk_list_store_set (store, &iter,
2104 DISPLAY_NAME_COLUMN, NULL,
2105 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2107 IS_FOLDER_COLUMN, FALSE,
2109 button->priv->has_bookmark_separator = TRUE;
2114 model_update_current_folder (GtkFileChooserButton *button,
2117 GtkListStore *store;
2124 store = GTK_LIST_STORE (button->priv->model);
2126 if (!button->priv->has_current_folder_separator)
2128 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
2129 gtk_list_store_insert (store, &iter, pos);
2130 gtk_list_store_set (store, &iter,
2132 DISPLAY_NAME_COLUMN, NULL,
2133 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
2135 IS_FOLDER_COLUMN, FALSE,
2137 button->priv->has_current_folder_separator = TRUE;
2140 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2141 if (!button->priv->has_current_folder)
2143 gtk_list_store_insert (store, &iter, pos);
2144 button->priv->has_current_folder = TRUE;
2148 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2149 model_free_row_data (button, &iter);
2152 if (g_file_is_native (file))
2154 gtk_list_store_set (store, &iter,
2156 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2157 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2158 DATA_COLUMN, g_object_ref (file),
2159 IS_FOLDER_COLUMN, FALSE,
2161 set_info_for_file_at_iter (button, file, &iter);
2166 GtkIconTheme *icon_theme;
2169 /* Don't call get_info for remote paths to avoid latency and
2171 * If we switch to a better bookmarks file format (XBEL), we
2172 * should use mime info to get a better icon.
2174 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2176 label = _gtk_file_chooser_label_for_file (file);
2178 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2180 if (g_file_is_native (file))
2181 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2182 button->priv->icon_size, 0, NULL);
2184 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2185 button->priv->icon_size, 0, NULL);
2187 gtk_list_store_set (store, &iter,
2188 ICON_COLUMN, pixbuf,
2189 DISPLAY_NAME_COLUMN, label,
2190 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2191 DATA_COLUMN, g_object_ref (file),
2192 IS_FOLDER_COLUMN, TRUE,
2196 g_object_unref (pixbuf);
2201 model_add_other (GtkFileChooserButton *button)
2203 GtkListStore *store;
2207 store = GTK_LIST_STORE (button->priv->model);
2208 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2210 gtk_list_store_insert (store, &iter, pos);
2211 gtk_list_store_set (store, &iter,
2213 DISPLAY_NAME_COLUMN, NULL,
2214 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2216 IS_FOLDER_COLUMN, FALSE,
2218 button->priv->has_other_separator = TRUE;
2221 gtk_list_store_insert (store, &iter, pos);
2222 gtk_list_store_set (store, &iter,
2224 DISPLAY_NAME_COLUMN, _("Other…"),
2225 TYPE_COLUMN, ROW_TYPE_OTHER,
2227 IS_FOLDER_COLUMN, FALSE,
2232 model_add_empty_selection (GtkFileChooserButton *button)
2234 GtkListStore *store;
2238 store = GTK_LIST_STORE (button->priv->model);
2239 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2241 gtk_list_store_insert (store, &iter, pos);
2242 gtk_list_store_set (store, &iter,
2244 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2245 TYPE_COLUMN, ROW_TYPE_EMPTY_SELECTION,
2247 IS_FOLDER_COLUMN, FALSE,
2252 model_remove_rows (GtkFileChooserButton *button,
2256 GtkListStore *store;
2261 store = GTK_LIST_STORE (button->priv->model);
2267 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2268 g_assert_not_reached ();
2270 model_free_row_data (button, &iter);
2271 gtk_list_store_remove (store, &iter);
2279 test_if_file_is_visible (GtkFileSystem *fs,
2281 gboolean local_only,
2287 if (local_only && !_gtk_file_has_native_path (file))
2297 filter_model_visible_func (GtkTreeModel *model,
2301 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2302 GtkFileChooserButtonPrivate *priv = button->priv;
2305 gboolean local_only, retval, is_folder;
2307 type = ROW_TYPE_INVALID;
2309 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2311 gtk_tree_model_get (model, iter,
2314 IS_FOLDER_COLUMN, &is_folder,
2319 case ROW_TYPE_CURRENT_FOLDER:
2322 case ROW_TYPE_SPECIAL:
2323 case ROW_TYPE_SHORTCUT:
2324 case ROW_TYPE_BOOKMARK:
2325 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2327 case ROW_TYPE_VOLUME:
2332 if (_gtk_file_system_volume_is_mounted (data))
2336 base_file = _gtk_file_system_volume_get_root (data);
2340 if (!_gtk_file_has_native_path (base_file))
2342 g_object_unref (base_file);
2350 case ROW_TYPE_EMPTY_SELECTION:
2352 gboolean popup_shown;
2354 g_object_get (priv->combo_box,
2355 "popup-shown", &popup_shown,
2364 /* When the combo box is not popped up... */
2366 selected = get_selected_file (button);
2368 retval = FALSE; /* ... nonempty selection means the ROW_TYPE_EMPTY_SELECTION is *not* visible... */
2370 retval = TRUE; /* ... and empty selection means the ROW_TYPE_EMPTY_SELECTION *is* visible */
2373 g_object_unref (selected);
2388 name_cell_data_func (GtkCellLayout *layout,
2389 GtkCellRenderer *cell,
2390 GtkTreeModel *model,
2397 gtk_tree_model_get (model, iter,
2401 if (type == ROW_TYPE_CURRENT_FOLDER)
2402 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2404 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2408 combo_box_row_separator_func (GtkTreeModel *model,
2412 gchar type = ROW_TYPE_INVALID;
2414 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2416 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2417 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2418 type == ROW_TYPE_OTHER_SEPARATOR);
2422 select_combo_box_row_no_notify (GtkFileChooserButton *button, int pos)
2424 GtkFileChooserButtonPrivate *priv = button->priv;
2425 GtkTreeIter iter, filter_iter;
2427 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2428 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2429 &filter_iter, &iter);
2431 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2432 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2433 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2437 update_combo_box (GtkFileChooserButton *button)
2439 GtkFileChooserButtonPrivate *priv = button->priv;
2444 file = get_selected_file (button);
2448 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2455 type = ROW_TYPE_INVALID;
2458 gtk_tree_model_get (priv->filter_model, &iter,
2465 case ROW_TYPE_SPECIAL:
2466 case ROW_TYPE_SHORTCUT:
2467 case ROW_TYPE_BOOKMARK:
2468 case ROW_TYPE_CURRENT_FOLDER:
2469 row_found = (file && g_file_equal (data, file));
2471 case ROW_TYPE_VOLUME:
2475 base_file = _gtk_file_system_volume_get_root (data);
2478 row_found = (file && g_file_equal (base_file, file));
2479 g_object_unref (base_file);
2490 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2491 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2493 g_signal_handler_unblock (priv->combo_box,
2494 priv->combo_box_changed_id);
2497 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2503 /* If it hasn't been found already, update & select the current-folder row. */
2506 model_update_current_folder (button, file);
2507 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2511 /* No selection; switch to that row */
2513 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2516 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2518 select_combo_box_row_no_notify (button, pos);
2522 g_object_unref (file);
2527 update_label_get_info_cb (GCancellable *cancellable,
2529 const GError *error,
2532 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2534 GtkFileChooserButton *button = data;
2535 GtkFileChooserButtonPrivate *priv = button->priv;
2537 if (cancellable != priv->update_button_cancellable)
2540 priv->update_button_cancellable = NULL;
2542 if (cancelled || error)
2545 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2547 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2550 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2552 priv->icon_size, 0, NULL);
2554 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2556 g_object_unref (pixbuf);
2559 g_object_unref (button);
2560 g_object_unref (cancellable);
2564 update_label_and_image (GtkFileChooserButton *button)
2566 GtkFileChooserButtonPrivate *priv = button->priv;
2570 file = get_selected_file (button);
2574 if (priv->update_button_cancellable)
2576 g_cancellable_cancel (priv->update_button_cancellable);
2577 priv->update_button_cancellable = NULL;
2582 GtkFileSystemVolume *volume = NULL;
2584 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2589 base_file = _gtk_file_system_volume_get_root (volume);
2590 if (base_file && g_file_equal (base_file, file))
2594 label_text = _gtk_file_system_volume_get_display_name (volume);
2595 pixbuf = _gtk_file_system_volume_render_icon (volume,
2596 GTK_WIDGET (button),
2599 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2601 g_object_unref (pixbuf);
2605 g_object_unref (base_file);
2607 _gtk_file_system_volume_unref (volume);
2613 if (g_file_is_native (file))
2615 priv->update_button_cancellable =
2616 _gtk_file_system_get_info (priv->fs, file,
2617 "standard::icon,standard::display-name",
2618 update_label_get_info_cb,
2619 g_object_ref (button));
2625 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2626 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2628 priv->icon_size, 0, NULL);
2629 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2631 g_object_unref (pixbuf);
2637 g_object_unref (file);
2641 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2642 g_free (label_text);
2646 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2647 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2652 /* ************************ *
2653 * Child Object Callbacks *
2654 * ************************ */
2658 fs_volumes_changed_cb (GtkFileSystem *fs,
2661 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2662 GtkFileChooserButtonPrivate *priv = button->priv;
2665 model_remove_rows (user_data,
2666 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2669 priv->n_volumes = 0;
2671 volumes = _gtk_file_system_list_volumes (fs);
2672 model_add_volumes (user_data, volumes);
2673 g_slist_free (volumes);
2675 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2677 update_label_and_image (user_data);
2678 update_combo_box (user_data);
2682 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2685 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2686 GtkFileChooserButtonPrivate *priv = button->priv;
2689 bookmarks = _gtk_file_system_list_bookmarks (fs);
2690 model_remove_rows (user_data,
2691 model_get_type_position (user_data,
2692 ROW_TYPE_BOOKMARK_SEPARATOR),
2693 (priv->n_bookmarks + priv->has_bookmark_separator));
2694 priv->has_bookmark_separator = FALSE;
2695 priv->n_bookmarks = 0;
2696 model_add_bookmarks (user_data, bookmarks);
2697 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2698 g_slist_free (bookmarks);
2700 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2702 update_label_and_image (user_data);
2703 update_combo_box (user_data);
2707 save_inactive_state (GtkFileChooserButton *button)
2709 GtkFileChooserButtonPrivate *priv = button->priv;
2711 if (priv->current_folder_while_inactive)
2712 g_object_unref (priv->current_folder_while_inactive);
2714 if (priv->selection_while_inactive)
2715 g_object_unref (priv->selection_while_inactive);
2717 priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->dialog));
2718 priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2722 restore_inactive_state (GtkFileChooserButton *button)
2724 GtkFileChooserButtonPrivate *priv = button->priv;
2726 if (priv->current_folder_while_inactive)
2727 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), priv->current_folder_while_inactive, NULL);
2729 if (priv->selection_while_inactive)
2730 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2732 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2737 open_dialog (GtkFileChooserButton *button)
2739 GtkFileChooserButtonPrivate *priv = button->priv;
2741 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2743 if (!gtk_widget_get_visible (priv->dialog))
2745 GtkWidget *toplevel;
2747 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2749 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2751 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2752 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2753 GTK_WINDOW (toplevel));
2755 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2756 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2762 g_signal_handler_block (priv->dialog,
2763 priv->dialog_folder_changed_id);
2764 g_signal_handler_block (priv->dialog,
2765 priv->dialog_file_activated_id);
2766 g_signal_handler_block (priv->dialog,
2767 priv->dialog_selection_changed_id);
2769 restore_inactive_state (button);
2771 priv->active = TRUE;
2774 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2775 gtk_window_present (GTK_WINDOW (priv->dialog));
2780 combo_box_changed_cb (GtkComboBox *combo_box,
2785 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2787 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2788 GtkFileChooserButtonPrivate *priv = button->priv;
2792 type = ROW_TYPE_INVALID;
2795 gtk_tree_model_get (priv->filter_model, &iter,
2802 case ROW_TYPE_SPECIAL:
2803 case ROW_TYPE_SHORTCUT:
2804 case ROW_TYPE_BOOKMARK:
2805 case ROW_TYPE_CURRENT_FOLDER:
2807 gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), data, NULL);
2809 case ROW_TYPE_VOLUME:
2813 base_file = _gtk_file_system_volume_get_root (data);
2816 gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), base_file, NULL);
2817 g_object_unref (base_file);
2821 case ROW_TYPE_OTHER:
2822 open_dialog (user_data);
2830 /* Calback for the "notify::popup-shown" signal on the combo box.
2831 * When the combo is popped up, we don't want the ROW_TYPE_EMPTY_SELECTION to be visible
2832 * at all; otherwise we would be showing a "(None)" item in the combo box's popup.
2834 * However, when the combo box is *not* popped up, we want the empty-selection row
2835 * to be visible depending on the selection.
2837 * Since all that is done through the filter_model_visible_func(), this means
2838 * that we need to refilter the model when the combo box pops up - hence the
2839 * present signal handler.
2842 combo_box_notify_popup_shown_cb (GObject *object,
2846 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2847 GtkFileChooserButtonPrivate *priv = button->priv;
2848 gboolean popup_shown;
2850 g_object_get (priv->combo_box,
2851 "popup-shown", &popup_shown,
2854 /* Indicate that the ROW_TYPE_EMPTY_SELECTION will change visibility... */
2855 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2857 /* If the combo box popup got dismissed, go back to showing the ROW_TYPE_EMPTY_SELECTION if needed */
2860 GFile *selected = get_selected_file (button);
2866 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2867 select_combo_box_row_no_notify (button, pos);
2870 g_object_unref (selected);
2876 button_clicked_cb (GtkButton *real_button,
2879 open_dialog (user_data);
2884 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2887 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2889 g_signal_emit_by_name (button, "current-folder-changed");
2893 dialog_file_activated_cb (GtkFileChooser *dialog,
2896 g_signal_emit_by_name (user_data, "file-activated");
2900 dialog_selection_changed_cb (GtkFileChooser *dialog,
2903 update_label_and_image (user_data);
2904 update_combo_box (user_data);
2905 g_signal_emit_by_name (user_data, "selection-changed");
2909 dialog_update_preview_cb (GtkFileChooser *dialog,
2912 g_signal_emit_by_name (user_data, "update-preview");
2916 dialog_notify_cb (GObject *dialog,
2922 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2923 GTK_TYPE_FILE_CHOOSER);
2924 if (g_object_interface_find_property (iface, pspec->name))
2925 g_object_notify (user_data, pspec->name);
2927 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2929 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2930 GtkFileChooserButtonPrivate *priv = button->priv;
2932 if (priv->has_current_folder)
2938 pos = model_get_type_position (user_data,
2939 ROW_TYPE_CURRENT_FOLDER);
2940 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2943 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2945 /* If the path isn't local but we're in local-only mode now, remove
2946 * the custom-folder row */
2947 if (data && _gtk_file_has_native_path (G_FILE (data)) &&
2948 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2951 model_remove_rows (user_data, pos, 2);
2955 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2956 update_combo_box (user_data);
2961 dialog_delete_event_cb (GtkWidget *dialog,
2965 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2971 dialog_response_cb (GtkDialog *dialog,
2975 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2976 GtkFileChooserButtonPrivate *priv = button->priv;
2978 if (response == GTK_RESPONSE_ACCEPT ||
2979 response == GTK_RESPONSE_OK)
2981 save_inactive_state (button);
2983 g_signal_emit_by_name (button, "current-folder-changed");
2984 g_signal_emit_by_name (button, "selection-changed");
2988 restore_inactive_state (button);
2993 g_signal_handler_unblock (priv->dialog,
2994 priv->dialog_folder_changed_id);
2995 g_signal_handler_unblock (priv->dialog,
2996 priv->dialog_file_activated_id);
2997 g_signal_handler_unblock (priv->dialog,
2998 priv->dialog_selection_changed_id);
2999 priv->active = FALSE;
3002 update_label_and_image (button);
3003 update_combo_box (button);
3005 gtk_widget_set_sensitive (priv->combo_box, TRUE);
3006 gtk_widget_hide (priv->dialog);
3008 if (response == GTK_RESPONSE_ACCEPT ||
3009 response == GTK_RESPONSE_OK)
3010 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
3014 /* ************************************************************************** *
3016 * ************************************************************************** */
3019 * gtk_file_chooser_button_new:
3020 * @title: the title of the browse dialog.
3021 * @action: the open mode for the widget.
3023 * Creates a new file-selecting button widget.
3025 * Returns: a new button widget.
3030 gtk_file_chooser_button_new (const gchar *title,
3031 GtkFileChooserAction action)
3033 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
3034 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
3036 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
3038 "title", (title ? title : _(DEFAULT_TITLE)),
3043 * gtk_file_chooser_button_new_with_dialog:
3044 * @dialog: the widget to use as dialog
3046 * Creates a #GtkFileChooserButton widget which uses @dialog as its
3047 * file-picking window.
3049 * Note that @dialog must be a #GtkDialog (or subclass) which
3050 * implements the #GtkFileChooser interface and must not have
3051 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
3053 * Also note that the dialog needs to have its confirmative button
3054 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
3055 * order for the button to take over the file selected in the dialog.
3057 * Returns: a new button widget.
3062 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
3064 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
3066 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
3072 * gtk_file_chooser_button_set_title:
3073 * @button: the button widget to modify.
3074 * @title: the new browse dialog title.
3076 * Modifies the @title of the browse dialog used by @button.
3081 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
3084 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3086 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
3087 g_object_notify (G_OBJECT (button), "title");
3091 * gtk_file_chooser_button_get_title:
3092 * @button: the button widget to examine.
3094 * Retrieves the title of the browse dialog used by @button. The returned value
3095 * should not be modified or freed.
3097 * Returns: a pointer to the browse dialog's title.
3102 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
3104 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
3106 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
3110 * gtk_file_chooser_button_get_width_chars:
3111 * @button: the button widget to examine.
3113 * Retrieves the width in characters of the @button widget's entry and/or label.
3115 * Returns: an integer width (in characters) that the button will use to size itself.
3120 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
3122 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
3124 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
3128 * gtk_file_chooser_button_set_width_chars:
3129 * @button: the button widget to examine.
3130 * @n_chars: the new width, in characters.
3132 * Sets the width (in characters) that @button will use to @n_chars.
3137 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
3140 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3142 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
3143 g_object_notify (G_OBJECT (button), "width-chars");
3147 * gtk_file_chooser_button_set_focus_on_click:
3148 * @button: a #GtkFileChooserButton
3149 * @focus_on_click: whether the button grabs focus when clicked with the mouse
3151 * Sets whether the button will grab focus when it is clicked with the mouse.
3152 * Making mouse clicks not grab focus is useful in places like toolbars where
3153 * you don't want the keyboard focus removed from the main area of the
3159 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
3160 gboolean focus_on_click)
3162 GtkFileChooserButtonPrivate *priv;
3164 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3166 priv = button->priv;
3168 focus_on_click = focus_on_click != FALSE;
3170 if (priv->focus_on_click != focus_on_click)
3172 priv->focus_on_click = focus_on_click;
3173 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
3174 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
3176 g_object_notify (G_OBJECT (button), "focus-on-click");
3181 * gtk_file_chooser_button_get_focus_on_click:
3182 * @button: a #GtkFileChooserButton
3184 * Returns whether the button grabs focus when it is clicked with the mouse.
3185 * See gtk_file_chooser_button_set_focus_on_click().
3187 * Return value: %TRUE if the button grabs focus when it is clicked with
3193 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
3195 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
3197 return button->priv->focus_on_click;