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 fs_volumes_changed_id;
187 gulong fs_bookmarks_changed_id;
189 GCancellable *dnd_select_folder_cancellable;
190 GCancellable *update_button_cancellable;
191 GSList *change_icon_theme_cancellables;
199 guint has_bookmark_separator : 1;
200 guint has_current_folder_separator : 1;
201 guint has_current_folder : 1;
202 guint has_other_separator : 1;
204 /* Used for hiding/showing the dialog when the button is hidden */
207 guint focus_on_click : 1;
209 /* Whether the next async callback from GIO should emit the "selection-changed" signal */
210 guint is_changing_selection : 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_notify_cb (GObject *dialog,
341 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
344 static void dialog_response_cb (GtkDialog *dialog,
348 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
350 /* ******************* *
351 * GType Declaration *
352 * ******************* */
354 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
355 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
359 /* ***************** *
361 * ***************** */
364 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
366 GObjectClass *gobject_class;
367 GtkWidgetClass *widget_class;
369 gobject_class = G_OBJECT_CLASS (class);
370 widget_class = GTK_WIDGET_CLASS (class);
372 gobject_class->constructor = gtk_file_chooser_button_constructor;
373 gobject_class->set_property = gtk_file_chooser_button_set_property;
374 gobject_class->get_property = gtk_file_chooser_button_get_property;
375 gobject_class->finalize = gtk_file_chooser_button_finalize;
377 widget_class->destroy = gtk_file_chooser_button_destroy;
378 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
379 widget_class->show_all = gtk_file_chooser_button_show_all;
380 widget_class->show = gtk_file_chooser_button_show;
381 widget_class->hide = gtk_file_chooser_button_hide;
382 widget_class->map = gtk_file_chooser_button_map;
383 widget_class->style_updated = gtk_file_chooser_button_style_updated;
384 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
385 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
388 * GtkFileChooserButton::file-set:
389 * @widget: the object which received the signal.
391 * The ::file-set signal is emitted when the user selects a file.
393 * Note that this signal is only emitted when the <emphasis>user</emphasis>
398 file_chooser_button_signals[FILE_SET] =
399 g_signal_new (I_("file-set"),
400 G_TYPE_FROM_CLASS (gobject_class),
402 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
404 _gtk_marshal_VOID__VOID,
408 * GtkFileChooserButton:dialog:
410 * Instance of the #GtkFileChooserDialog associated with the button.
414 g_object_class_install_property (gobject_class, PROP_DIALOG,
415 g_param_spec_object ("dialog",
417 P_("The file chooser dialog to use."),
418 GTK_TYPE_FILE_CHOOSER,
419 (GTK_PARAM_WRITABLE |
420 G_PARAM_CONSTRUCT_ONLY)));
423 * GtkFileChooserButton:focus-on-click:
425 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
430 g_object_class_install_property (gobject_class,
432 g_param_spec_boolean ("focus-on-click",
433 P_("Focus on click"),
434 P_("Whether the button grabs focus when it is clicked with the mouse"),
436 GTK_PARAM_READWRITE));
439 * GtkFileChooserButton:title:
441 * Title to put on the #GtkFileChooserDialog associated with the button.
445 g_object_class_install_property (gobject_class, PROP_TITLE,
446 g_param_spec_string ("title",
448 P_("The title of the file chooser dialog."),
450 GTK_PARAM_READWRITE));
453 * GtkFileChooserButton:width-chars:
455 * The width of the entry and label inside the button, in characters.
459 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
460 g_param_spec_int ("width-chars",
461 P_("Width In Characters"),
462 P_("The desired width of the button widget, in characters."),
464 GTK_PARAM_READWRITE));
466 _gtk_file_chooser_install_properties (gobject_class);
468 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
472 gtk_file_chooser_button_init (GtkFileChooserButton *button)
474 GtkFileChooserButtonPrivate *priv;
475 GtkWidget *box, *image, *sep;
476 GtkTargetList *target_list;
478 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
479 GTK_TYPE_FILE_CHOOSER_BUTTON,
480 GtkFileChooserButtonPrivate);
482 priv->icon_size = FALLBACK_ICON_SIZE;
483 priv->focus_on_click = TRUE;
485 gtk_widget_push_composite_child ();
488 priv->button = gtk_button_new ();
489 g_signal_connect (priv->button, "clicked",
490 G_CALLBACK (button_clicked_cb), button);
491 gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
492 gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
493 gtk_widget_show (priv->button);
495 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
496 gtk_container_add (GTK_CONTAINER (priv->button), box);
497 gtk_widget_show (box);
499 priv->image = gtk_image_new ();
500 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
501 gtk_widget_show (priv->image);
503 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
504 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
505 gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
506 gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
507 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
508 //gtk_container_add (GTK_CONTAINER (box), priv->label);
509 gtk_widget_show (priv->label);
511 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
512 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
513 gtk_widget_show (sep);
515 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
516 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
517 gtk_widget_show (image);
520 /* Keep in sync with columns enum, line 88 */
522 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
523 GDK_TYPE_PIXBUF, /* ICON_COLUMN */
524 G_TYPE_STRING, /* DISPLAY_NAME_COLUMN */
525 G_TYPE_CHAR, /* TYPE_COLUMN */
526 G_TYPE_POINTER /* DATA_COLUMN (Volume || Path) */,
527 G_TYPE_BOOLEAN /* IS_FOLDER_COLUMN */,
528 G_TYPE_POINTER /* CANCELLABLE_COLUMN */));
530 priv->combo_box = gtk_combo_box_new ();
531 priv->combo_box_changed_id = g_signal_connect (priv->combo_box, "changed",
532 G_CALLBACK (combo_box_changed_cb), button);
534 g_signal_connect (priv->combo_box, "notify::popup-shown",
535 G_CALLBACK (combo_box_notify_popup_shown_cb), button);
537 gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
538 gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
540 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
541 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
542 priv->icon_cell, FALSE);
543 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
544 priv->icon_cell, "pixbuf", ICON_COLUMN);
546 priv->name_cell = gtk_cell_renderer_text_new ();
547 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
548 priv->name_cell, TRUE);
549 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
550 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
551 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
552 priv->name_cell, name_cell_data_func,
555 gtk_widget_pop_composite_child ();
558 gtk_drag_dest_set (GTK_WIDGET (button),
559 (GTK_DEST_DEFAULT_ALL),
562 target_list = gtk_target_list_new (NULL, 0);
563 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
564 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
565 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
566 gtk_target_list_unref (target_list);
570 /* ******************************* *
571 * GtkFileChooserIface Functions *
572 * ******************************* */
574 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
576 _gtk_file_chooser_delegate_iface_init (iface);
578 iface->set_current_folder = gtk_file_chooser_button_set_current_folder;
579 iface->get_current_folder = gtk_file_chooser_button_get_current_folder;
580 iface->select_file = gtk_file_chooser_button_select_file;
581 iface->unselect_file = gtk_file_chooser_button_unselect_file;
582 iface->unselect_all = gtk_file_chooser_button_unselect_all;
583 iface->get_files = gtk_file_chooser_button_get_files;
584 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
585 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
589 emit_selection_changed_if_changing_selection (GtkFileChooserButton *button)
591 GtkFileChooserButtonPrivate *priv = button->priv;
593 if (priv->is_changing_selection)
595 priv->is_changing_selection = FALSE;
596 g_signal_emit_by_name (button, "selection-changed");
601 gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
605 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
606 GtkFileChooserButtonPrivate *priv = button->priv;
608 if (priv->current_folder_while_inactive)
609 g_object_unref (priv->current_folder_while_inactive);
611 priv->current_folder_while_inactive = g_object_ref (file);
613 update_combo_box (button);
615 g_signal_emit_by_name (button, "current-folder-changed");
618 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), file, NULL);
624 gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser)
626 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
627 GtkFileChooserButtonPrivate *priv = button->priv;
629 if (priv->current_folder_while_inactive)
630 return g_object_ref (priv->current_folder_while_inactive);
636 gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
640 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
641 GtkFileChooserButtonPrivate *priv = button->priv;
643 if (priv->selection_while_inactive)
644 g_object_unref (priv->selection_while_inactive);
646 priv->selection_while_inactive = g_object_ref (file);
648 priv->is_changing_selection = TRUE;
650 update_label_and_image (button);
651 update_combo_box (button);
654 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file, NULL);
660 unselect_current_file (GtkFileChooserButton *button)
662 GtkFileChooserButtonPrivate *priv = button->priv;
664 if (priv->selection_while_inactive)
666 g_object_unref (priv->selection_while_inactive);
667 priv->selection_while_inactive = NULL;
670 priv->is_changing_selection = TRUE;
672 update_label_and_image (button);
673 update_combo_box (button);
677 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
680 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
681 GtkFileChooserButtonPrivate *priv = button->priv;
683 if (g_file_equal (priv->selection_while_inactive, file))
684 unselect_current_file (button);
687 gtk_file_chooser_unselect_file (GTK_FILE_CHOOSER (priv->dialog), file);
691 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
693 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
694 GtkFileChooserButtonPrivate *priv = button->priv;
696 unselect_current_file (button);
699 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
703 get_selected_file (GtkFileChooserButton *button)
705 GtkFileChooserButtonPrivate *priv = button->priv;
710 if (priv->selection_while_inactive)
711 retval = priv->selection_while_inactive;
712 else if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
714 /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
715 * the current folder, since that is what GtkFileChooserDefault would do.
717 if (priv->current_folder_while_inactive)
718 retval = priv->current_folder_while_inactive;
722 return g_object_ref (retval);
728 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
730 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
733 file = get_selected_file (button);
735 return g_slist_prepend (NULL, file);
741 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
745 GtkFileChooser *delegate;
748 delegate = g_object_get_qdata (G_OBJECT (chooser),
749 GTK_FILE_CHOOSER_DELEGATE_QUARK);
750 retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
754 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
755 GtkFileChooserButtonPrivate *priv = button->priv;
759 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
760 pos += priv->n_shortcuts;
762 gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
763 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
765 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
766 TYPE_COLUMN, ROW_TYPE_SHORTCUT,
767 DATA_COLUMN, g_object_ref (file),
768 IS_FOLDER_COLUMN, FALSE,
770 set_info_for_file_at_iter (button, file, &iter);
773 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
780 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
784 GtkFileChooser *delegate;
787 delegate = g_object_get_qdata (G_OBJECT (chooser),
788 GTK_FILE_CHOOSER_DELEGATE_QUARK);
790 retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
794 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
795 GtkFileChooserButtonPrivate *priv = button->priv;
800 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
801 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
807 gtk_tree_model_get (priv->model, &iter,
812 if (type == ROW_TYPE_SHORTCUT &&
813 data && g_file_equal (data, file))
815 model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
816 gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
818 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
819 update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
823 while (type == ROW_TYPE_SHORTCUT &&
824 gtk_tree_model_iter_next (priv->model, &iter));
831 /* ******************* *
832 * GObject Functions *
833 * ******************* */
836 gtk_file_chooser_button_constructor (GType type,
838 GObjectConstructParam *params)
841 GtkFileChooserButton *button;
842 GtkFileChooserButtonPrivate *priv;
845 object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
848 button = GTK_FILE_CHOOSER_BUTTON (object);
853 priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
854 GTK_FILE_CHOOSER_ACTION_OPEN,
861 gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
862 GTK_RESPONSE_ACCEPT);
863 gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
868 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
870 else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
872 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
875 g_signal_connect (priv->dialog, "delete-event",
876 G_CALLBACK (dialog_delete_event_cb), object);
877 g_signal_connect (priv->dialog, "response",
878 G_CALLBACK (dialog_response_cb), object);
880 /* This is used, instead of the standard delegate, to ensure that signals are only
881 * delegated when the OK button is pressed. */
882 g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
884 g_signal_connect (priv->dialog, "update-preview",
885 G_CALLBACK (dialog_update_preview_cb), object);
886 g_signal_connect (priv->dialog, "notify",
887 G_CALLBACK (dialog_notify_cb), object);
888 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
889 (gpointer) (&priv->dialog));
892 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
894 model_add_special (button);
896 list = _gtk_file_system_list_volumes (priv->fs);
897 model_add_volumes (button, list);
900 list = _gtk_file_system_list_bookmarks (priv->fs);
901 model_add_bookmarks (button, list);
902 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
905 model_add_other (button);
907 model_add_empty_selection (button);
909 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
910 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
911 filter_model_visible_func,
914 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
915 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
916 combo_box_row_separator_func,
919 /* set up the action for a user-provided dialog, this also updates
920 * the label, image and combobox
922 g_object_set (object,
923 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
926 priv->fs_volumes_changed_id =
927 g_signal_connect (priv->fs, "volumes-changed",
928 G_CALLBACK (fs_volumes_changed_cb), object);
929 priv->fs_bookmarks_changed_id =
930 g_signal_connect (priv->fs, "bookmarks-changed",
931 G_CALLBACK (fs_bookmarks_changed_cb), object);
933 update_label_and_image (button);
934 update_combo_box (button);
940 gtk_file_chooser_button_set_property (GObject *object,
945 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
946 GtkFileChooserButtonPrivate *priv = button->priv;
952 priv->dialog = g_value_get_object (value);
954 case PROP_FOCUS_ON_CLICK:
955 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
957 case PROP_WIDTH_CHARS:
958 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
959 g_value_get_int (value));
961 case GTK_FILE_CHOOSER_PROP_ACTION:
962 switch (g_value_get_enum (value))
964 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
965 case GTK_FILE_CHOOSER_ACTION_SAVE:
970 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
971 eval = g_enum_get_value (eclass, g_value_get_enum (value));
972 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
973 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
975 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
980 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
981 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
982 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
984 switch (g_value_get_enum (value))
986 case GTK_FILE_CHOOSER_ACTION_OPEN:
987 gtk_widget_hide (priv->combo_box);
988 gtk_widget_show (priv->button);
990 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
991 gtk_widget_hide (priv->button);
992 gtk_widget_show (priv->combo_box);
995 g_assert_not_reached ();
1001 case GTK_FILE_CHOOSER_PROP_FILTER:
1002 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1003 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1004 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1005 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1006 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1007 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1008 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1009 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1012 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1013 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1014 fs_volumes_changed_cb (priv->fs, button);
1015 fs_bookmarks_changed_cb (priv->fs, button);
1018 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1019 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
1020 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
1023 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1029 gtk_file_chooser_button_get_property (GObject *object,
1034 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1035 GtkFileChooserButtonPrivate *priv = button->priv;
1039 case PROP_WIDTH_CHARS:
1040 g_value_set_int (value,
1041 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
1043 case PROP_FOCUS_ON_CLICK:
1044 g_value_set_boolean (value,
1045 gtk_file_chooser_button_get_focus_on_click (button));
1049 case GTK_FILE_CHOOSER_PROP_ACTION:
1050 case GTK_FILE_CHOOSER_PROP_FILTER:
1051 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1052 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1053 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1054 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1055 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1056 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1057 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1058 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1059 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1060 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1064 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1070 gtk_file_chooser_button_finalize (GObject *object)
1072 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1073 GtkFileChooserButtonPrivate *priv = button->priv;
1075 if (priv->selection_while_inactive)
1076 g_object_unref (priv->selection_while_inactive);
1078 if (priv->current_folder_while_inactive)
1079 g_object_unref (priv->current_folder_while_inactive);
1081 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1084 /* ********************* *
1085 * GtkWidget Functions *
1086 * ********************* */
1089 gtk_file_chooser_button_destroy (GtkWidget *widget)
1091 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1092 GtkFileChooserButtonPrivate *priv = button->priv;
1096 if (priv->dialog != NULL)
1098 gtk_widget_destroy (priv->dialog);
1099 priv->dialog = NULL;
1102 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1104 model_free_row_data (button, &iter);
1106 while (gtk_tree_model_iter_next (priv->model, &iter));
1108 if (priv->dnd_select_folder_cancellable)
1110 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1111 priv->dnd_select_folder_cancellable = NULL;
1114 if (priv->update_button_cancellable)
1116 g_cancellable_cancel (priv->update_button_cancellable);
1117 priv->update_button_cancellable = NULL;
1120 if (priv->change_icon_theme_cancellables)
1122 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1124 GCancellable *cancellable = G_CANCELLABLE (l->data);
1125 g_cancellable_cancel (cancellable);
1127 g_slist_free (priv->change_icon_theme_cancellables);
1128 priv->change_icon_theme_cancellables = NULL;
1133 g_object_unref (priv->model);
1137 if (priv->filter_model)
1139 g_object_unref (priv->filter_model);
1140 priv->filter_model = NULL;
1145 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1146 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1147 g_object_unref (priv->fs);
1151 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1154 struct DndSelectFolderData
1156 GtkFileSystem *file_system;
1157 GtkFileChooserButton *button;
1158 GtkFileChooserAction action;
1166 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1168 const GError *error,
1171 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1172 struct DndSelectFolderData *data = user_data;
1174 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1176 g_object_unref (data->button);
1177 g_object_unref (data->file);
1178 g_strfreev (data->uris);
1181 g_object_unref (cancellable);
1185 data->button->priv->dnd_select_folder_cancellable = NULL;
1187 if (!cancelled && !error && info != NULL)
1191 is_folder = _gtk_file_info_consider_as_directory (info);
1194 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1195 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1196 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1200 data->selected = FALSE;
1202 if (data->selected || data->uris[++data->i] == NULL)
1204 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1206 g_object_unref (data->button);
1207 g_object_unref (data->file);
1208 g_strfreev (data->uris);
1211 g_object_unref (cancellable);
1216 g_object_unref (data->file);
1218 data->file = g_file_new_for_uri (data->uris[data->i]);
1220 data->button->priv->dnd_select_folder_cancellable =
1221 _gtk_file_system_get_info (data->file_system, data->file,
1223 dnd_select_folder_get_info_cb, user_data);
1225 g_object_unref (cancellable);
1229 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1230 GdkDragContext *context,
1233 GtkSelectionData *data,
1237 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1238 GtkFileChooserButtonPrivate *priv = button->priv;
1242 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1243 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1249 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1257 struct DndSelectFolderData *info;
1259 uris = gtk_selection_data_get_uris (data);
1264 info = g_new0 (struct DndSelectFolderData, 1);
1265 info->button = g_object_ref (button);
1268 info->selected = FALSE;
1269 info->file_system = priv->fs;
1270 g_object_get (priv->dialog, "action", &info->action, NULL);
1272 info->file = g_file_new_for_uri (info->uris[info->i]);
1274 if (priv->dnd_select_folder_cancellable)
1275 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1277 priv->dnd_select_folder_cancellable =
1278 _gtk_file_system_get_info (priv->fs, info->file,
1280 dnd_select_folder_get_info_cb, info);
1285 text = (char*) gtk_selection_data_get_text (data);
1286 file = g_file_new_for_uri (text);
1287 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1289 g_object_unref (file);
1291 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1298 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1302 gtk_file_chooser_button_show_all (GtkWidget *widget)
1304 gtk_widget_show (widget);
1308 gtk_file_chooser_button_show (GtkWidget *widget)
1310 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1311 GtkFileChooserButtonPrivate *priv = button->priv;
1313 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1314 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1317 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1321 gtk_file_chooser_button_hide (GtkWidget *widget)
1323 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1324 GtkFileChooserButtonPrivate *priv = button->priv;
1326 gtk_widget_hide (priv->dialog);
1328 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1329 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1333 gtk_file_chooser_button_map (GtkWidget *widget)
1335 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1339 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1340 gboolean group_cycling)
1342 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1343 GtkFileChooserButtonPrivate *priv = button->priv;
1345 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1347 case GTK_FILE_CHOOSER_ACTION_OPEN:
1348 gtk_widget_grab_focus (priv->button);
1350 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1351 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1354 g_assert_not_reached ();
1361 /* Changes the icons wherever it is needed */
1362 struct ChangeIconThemeData
1364 GtkFileChooserButton *button;
1365 GtkTreeRowReference *row_ref;
1369 change_icon_theme_get_info_cb (GCancellable *cancellable,
1371 const GError *error,
1374 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1376 struct ChangeIconThemeData *data = user_data;
1378 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1381 data->button->priv->change_icon_theme_cancellables =
1382 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1384 if (cancelled || error)
1387 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1395 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1397 path = gtk_tree_row_reference_get_path (data->row_ref);
1400 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1401 gtk_tree_path_free (path);
1403 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1404 ICON_COLUMN, pixbuf,
1407 g_object_set (data->button->priv->icon_cell,
1411 g_object_unref (pixbuf);
1415 g_object_unref (data->button);
1416 gtk_tree_row_reference_free (data->row_ref);
1419 g_object_unref (cancellable);
1423 change_icon_theme (GtkFileChooserButton *button)
1425 GtkFileChooserButtonPrivate *priv = button->priv;
1426 GtkSettings *settings;
1427 GtkIconTheme *theme;
1430 gint width = 0, height = 0;
1432 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1434 GCancellable *cancellable = G_CANCELLABLE (l->data);
1435 g_cancellable_cancel (cancellable);
1437 g_slist_free (button->priv->change_icon_theme_cancellables);
1438 button->priv->change_icon_theme_cancellables = NULL;
1440 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1442 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1444 priv->icon_size = MAX (width, height);
1446 priv->icon_size = FALLBACK_ICON_SIZE;
1448 update_label_and_image (button);
1450 gtk_tree_model_get_iter_first (priv->model, &iter);
1452 theme = get_icon_theme (GTK_WIDGET (button));
1460 type = ROW_TYPE_INVALID;
1461 gtk_tree_model_get (priv->model, &iter,
1468 case ROW_TYPE_SPECIAL:
1469 case ROW_TYPE_SHORTCUT:
1470 case ROW_TYPE_BOOKMARK:
1471 case ROW_TYPE_CURRENT_FOLDER:
1474 if (g_file_is_native (G_FILE (data)))
1477 GCancellable *cancellable;
1478 struct ChangeIconThemeData *info;
1480 info = g_new0 (struct ChangeIconThemeData, 1);
1481 info->button = g_object_ref (button);
1482 path = gtk_tree_model_get_path (priv->model, &iter);
1483 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1484 gtk_tree_path_free (path);
1487 _gtk_file_system_get_info (priv->fs, data,
1489 change_icon_theme_get_info_cb,
1491 button->priv->change_icon_theme_cancellables =
1492 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1496 /* Don't call get_info for remote paths to avoid latency and
1498 * If we switch to a better bookmarks file format (XBEL), we
1499 * should use mime info to get a better icon.
1501 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1502 priv->icon_size, 0, NULL);
1505 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1506 priv->icon_size, 0, NULL);
1508 case ROW_TYPE_VOLUME:
1510 pixbuf = _gtk_file_system_volume_render_icon (data,
1511 GTK_WIDGET (button),
1515 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1516 priv->icon_size, 0, NULL);
1524 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1526 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1527 ICON_COLUMN, pixbuf,
1531 g_object_unref (pixbuf);
1533 while (gtk_tree_model_iter_next (priv->model, &iter));
1535 g_object_set (button->priv->icon_cell,
1541 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1543 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1545 if (gtk_widget_has_screen (widget))
1546 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1550 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1551 GdkScreen *old_screen)
1553 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1554 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1557 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1561 /* ******************* *
1562 * Utility Functions *
1563 * ******************* */
1566 static GtkIconTheme *
1567 get_icon_theme (GtkWidget *widget)
1569 if (gtk_widget_has_screen (widget))
1570 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1572 return gtk_icon_theme_get_default ();
1576 struct SetDisplayNameData
1578 GtkFileChooserButton *button;
1580 GtkTreeRowReference *row_ref;
1584 set_info_get_info_cb (GCancellable *cancellable,
1586 const GError *error,
1587 gpointer callback_data)
1589 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1593 GCancellable *model_cancellable = NULL;
1594 struct SetDisplayNameData *data = callback_data;
1597 if (!data->button->priv->model)
1598 /* button got destroyed */
1601 path = gtk_tree_row_reference_get_path (data->row_ref);
1603 /* Cancellable doesn't exist anymore in the model */
1606 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1607 gtk_tree_path_free (path);
1609 /* Validate the cancellable */
1610 gtk_tree_model_get (data->button->priv->model, &iter,
1611 CANCELLABLE_COLUMN, &model_cancellable,
1613 if (cancellable != model_cancellable)
1616 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1617 CANCELLABLE_COLUMN, NULL,
1620 if (cancelled || error)
1621 /* There was an error, leave the fallback name in there */
1624 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1627 data->label = g_strdup (g_file_info_get_display_name (info));
1629 is_folder = _gtk_file_info_consider_as_directory (info);
1631 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1632 ICON_COLUMN, pixbuf,
1633 DISPLAY_NAME_COLUMN, data->label,
1634 IS_FOLDER_COLUMN, is_folder,
1638 g_object_unref (pixbuf);
1641 g_object_unref (data->button);
1642 g_free (data->label);
1643 gtk_tree_row_reference_free (data->row_ref);
1646 if (model_cancellable)
1647 g_object_unref (model_cancellable);
1651 set_info_for_file_at_iter (GtkFileChooserButton *button,
1655 struct SetDisplayNameData *data;
1656 GtkTreePath *tree_path;
1657 GCancellable *cancellable;
1659 data = g_new0 (struct SetDisplayNameData, 1);
1660 data->button = g_object_ref (button);
1661 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1663 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1664 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1665 gtk_tree_path_free (tree_path);
1667 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1668 "standard::type,standard::icon,standard::display-name",
1669 set_info_get_info_cb, data);
1671 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1672 CANCELLABLE_COLUMN, cancellable,
1676 /* Shortcuts Model */
1678 model_get_type_position (GtkFileChooserButton *button,
1683 if (row_type == ROW_TYPE_SPECIAL)
1686 retval += button->priv->n_special;
1688 if (row_type == ROW_TYPE_VOLUME)
1691 retval += button->priv->n_volumes;
1693 if (row_type == ROW_TYPE_SHORTCUT)
1696 retval += button->priv->n_shortcuts;
1698 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1701 retval += button->priv->has_bookmark_separator;
1703 if (row_type == ROW_TYPE_BOOKMARK)
1706 retval += button->priv->n_bookmarks;
1708 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1711 retval += button->priv->has_current_folder_separator;
1713 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1716 retval += button->priv->has_current_folder;
1718 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1721 retval += button->priv->has_other_separator;
1723 if (row_type == ROW_TYPE_OTHER)
1728 if (row_type == ROW_TYPE_EMPTY_SELECTION)
1731 g_assert_not_reached ();
1736 model_free_row_data (GtkFileChooserButton *button,
1741 GCancellable *cancellable;
1743 gtk_tree_model_get (button->priv->model, iter,
1746 CANCELLABLE_COLUMN, &cancellable,
1751 g_cancellable_cancel (cancellable);
1752 g_object_unref (cancellable);
1757 case ROW_TYPE_SPECIAL:
1758 case ROW_TYPE_SHORTCUT:
1759 case ROW_TYPE_BOOKMARK:
1760 case ROW_TYPE_CURRENT_FOLDER:
1761 g_object_unref (data);
1763 case ROW_TYPE_VOLUME:
1764 _gtk_file_system_volume_unref (data);
1772 model_add_special_get_info_cb (GCancellable *cancellable,
1774 const GError *error,
1777 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1781 GCancellable *model_cancellable = NULL;
1782 struct ChangeIconThemeData *data = user_data;
1785 if (!data->button->priv->model)
1786 /* button got destroyed */
1789 path = gtk_tree_row_reference_get_path (data->row_ref);
1791 /* Cancellable doesn't exist anymore in the model */
1794 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1795 gtk_tree_path_free (path);
1797 gtk_tree_model_get (data->button->priv->model, &iter,
1798 CANCELLABLE_COLUMN, &model_cancellable,
1800 if (cancellable != model_cancellable)
1803 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1804 CANCELLABLE_COLUMN, NULL,
1807 if (cancelled || error)
1810 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1814 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1815 ICON_COLUMN, pixbuf,
1817 g_object_unref (pixbuf);
1820 gtk_tree_model_get (data->button->priv->model, &iter,
1821 DISPLAY_NAME_COLUMN, &name,
1824 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1825 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1830 g_object_unref (data->button);
1831 gtk_tree_row_reference_free (data->row_ref);
1834 if (model_cancellable)
1835 g_object_unref (model_cancellable);
1839 model_add_special (GtkFileChooserButton *button)
1841 const gchar *homedir;
1842 const gchar *desktopdir;
1843 GtkListStore *store;
1848 store = GTK_LIST_STORE (button->priv->model);
1849 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1851 homedir = g_get_home_dir ();
1855 GtkTreePath *tree_path;
1856 GCancellable *cancellable;
1857 struct ChangeIconThemeData *info;
1859 file = g_file_new_for_path (homedir);
1860 gtk_list_store_insert (store, &iter, pos);
1863 info = g_new0 (struct ChangeIconThemeData, 1);
1864 info->button = g_object_ref (button);
1865 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1866 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1868 gtk_tree_path_free (tree_path);
1870 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1871 "standard::icon,standard::display-name",
1872 model_add_special_get_info_cb, info);
1874 gtk_list_store_set (store, &iter,
1876 DISPLAY_NAME_COLUMN, NULL,
1877 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1879 IS_FOLDER_COLUMN, TRUE,
1880 CANCELLABLE_COLUMN, cancellable,
1883 button->priv->n_special++;
1886 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1888 /* "To disable a directory, point it to the homedir."
1889 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1891 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1893 GtkTreePath *tree_path;
1894 GCancellable *cancellable;
1895 struct ChangeIconThemeData *info;
1897 file = g_file_new_for_path (desktopdir);
1898 gtk_list_store_insert (store, &iter, pos);
1901 info = g_new0 (struct ChangeIconThemeData, 1);
1902 info->button = g_object_ref (button);
1903 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1904 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1906 gtk_tree_path_free (tree_path);
1908 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1909 "standard::icon,standard::display-name",
1910 model_add_special_get_info_cb, info);
1912 gtk_list_store_set (store, &iter,
1913 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1915 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1917 IS_FOLDER_COLUMN, TRUE,
1918 CANCELLABLE_COLUMN, cancellable,
1921 button->priv->n_special++;
1926 model_add_volumes (GtkFileChooserButton *button,
1929 GtkListStore *store;
1931 gboolean local_only;
1937 store = GTK_LIST_STORE (button->priv->model);
1938 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1939 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1941 for (l = volumes; l; l = l->next)
1943 GtkFileSystemVolume *volume;
1946 gchar *display_name;
1952 if (_gtk_file_system_volume_is_mounted (volume))
1956 base_file = _gtk_file_system_volume_get_root (volume);
1957 if (base_file != NULL)
1959 if (!_gtk_file_has_native_path (base_file))
1961 g_object_unref (base_file);
1965 g_object_unref (base_file);
1970 pixbuf = _gtk_file_system_volume_render_icon (volume,
1971 GTK_WIDGET (button),
1972 button->priv->icon_size,
1974 display_name = _gtk_file_system_volume_get_display_name (volume);
1976 gtk_list_store_insert (store, &iter, pos);
1977 gtk_list_store_set (store, &iter,
1978 ICON_COLUMN, pixbuf,
1979 DISPLAY_NAME_COLUMN, display_name,
1980 TYPE_COLUMN, ROW_TYPE_VOLUME,
1981 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
1982 IS_FOLDER_COLUMN, TRUE,
1986 g_object_unref (pixbuf);
1987 g_free (display_name);
1989 button->priv->n_volumes++;
1994 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
1997 model_add_bookmarks (GtkFileChooserButton *button,
2000 GtkListStore *store;
2003 gboolean local_only;
2009 store = GTK_LIST_STORE (button->priv->model);
2010 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
2011 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
2013 for (l = bookmarks; l; l = l->next)
2019 if (_gtk_file_has_native_path (file))
2021 gtk_list_store_insert (store, &iter, pos);
2022 gtk_list_store_set (store, &iter,
2024 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2025 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2026 DATA_COLUMN, g_object_ref (file),
2027 IS_FOLDER_COLUMN, FALSE,
2029 set_info_for_file_at_iter (button, file, &iter);
2034 GtkIconTheme *icon_theme;
2040 /* Don't call get_info for remote paths to avoid latency and
2042 * If we switch to a better bookmarks file format (XBEL), we
2043 * should use mime info to get a better icon.
2045 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2047 label = _gtk_file_chooser_label_for_file (file);
2049 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2050 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2051 button->priv->icon_size, 0, NULL);
2053 gtk_list_store_insert (store, &iter, pos);
2054 gtk_list_store_set (store, &iter,
2055 ICON_COLUMN, pixbuf,
2056 DISPLAY_NAME_COLUMN, label,
2057 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2058 DATA_COLUMN, g_object_ref (file),
2059 IS_FOLDER_COLUMN, TRUE,
2063 g_object_unref (pixbuf);
2066 button->priv->n_bookmarks++;
2070 if (button->priv->n_bookmarks > 0 &&
2071 !button->priv->has_bookmark_separator)
2073 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2075 gtk_list_store_insert (store, &iter, pos);
2076 gtk_list_store_set (store, &iter,
2078 DISPLAY_NAME_COLUMN, NULL,
2079 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2081 IS_FOLDER_COLUMN, FALSE,
2083 button->priv->has_bookmark_separator = TRUE;
2088 model_update_current_folder (GtkFileChooserButton *button,
2091 GtkListStore *store;
2098 store = GTK_LIST_STORE (button->priv->model);
2100 if (!button->priv->has_current_folder_separator)
2102 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
2103 gtk_list_store_insert (store, &iter, pos);
2104 gtk_list_store_set (store, &iter,
2106 DISPLAY_NAME_COLUMN, NULL,
2107 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
2109 IS_FOLDER_COLUMN, FALSE,
2111 button->priv->has_current_folder_separator = TRUE;
2114 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2115 if (!button->priv->has_current_folder)
2117 gtk_list_store_insert (store, &iter, pos);
2118 button->priv->has_current_folder = TRUE;
2122 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2123 model_free_row_data (button, &iter);
2126 if (g_file_is_native (file))
2128 gtk_list_store_set (store, &iter,
2130 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2131 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2132 DATA_COLUMN, g_object_ref (file),
2133 IS_FOLDER_COLUMN, FALSE,
2135 set_info_for_file_at_iter (button, file, &iter);
2140 GtkIconTheme *icon_theme;
2143 /* Don't call get_info for remote paths to avoid latency and
2145 * If we switch to a better bookmarks file format (XBEL), we
2146 * should use mime info to get a better icon.
2148 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2150 label = _gtk_file_chooser_label_for_file (file);
2152 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2154 if (g_file_is_native (file))
2155 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2156 button->priv->icon_size, 0, NULL);
2158 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2159 button->priv->icon_size, 0, NULL);
2161 gtk_list_store_set (store, &iter,
2162 ICON_COLUMN, pixbuf,
2163 DISPLAY_NAME_COLUMN, label,
2164 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2165 DATA_COLUMN, g_object_ref (file),
2166 IS_FOLDER_COLUMN, TRUE,
2170 g_object_unref (pixbuf);
2175 model_add_other (GtkFileChooserButton *button)
2177 GtkListStore *store;
2181 store = GTK_LIST_STORE (button->priv->model);
2182 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2184 gtk_list_store_insert (store, &iter, pos);
2185 gtk_list_store_set (store, &iter,
2187 DISPLAY_NAME_COLUMN, NULL,
2188 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2190 IS_FOLDER_COLUMN, FALSE,
2192 button->priv->has_other_separator = TRUE;
2195 gtk_list_store_insert (store, &iter, pos);
2196 gtk_list_store_set (store, &iter,
2198 DISPLAY_NAME_COLUMN, _("Other…"),
2199 TYPE_COLUMN, ROW_TYPE_OTHER,
2201 IS_FOLDER_COLUMN, FALSE,
2206 model_add_empty_selection (GtkFileChooserButton *button)
2208 GtkListStore *store;
2212 store = GTK_LIST_STORE (button->priv->model);
2213 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2215 gtk_list_store_insert (store, &iter, pos);
2216 gtk_list_store_set (store, &iter,
2218 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2219 TYPE_COLUMN, ROW_TYPE_EMPTY_SELECTION,
2221 IS_FOLDER_COLUMN, FALSE,
2226 model_remove_rows (GtkFileChooserButton *button,
2230 GtkListStore *store;
2235 store = GTK_LIST_STORE (button->priv->model);
2241 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2242 g_assert_not_reached ();
2244 model_free_row_data (button, &iter);
2245 gtk_list_store_remove (store, &iter);
2253 test_if_file_is_visible (GtkFileSystem *fs,
2255 gboolean local_only,
2261 if (local_only && !_gtk_file_has_native_path (file))
2271 filter_model_visible_func (GtkTreeModel *model,
2275 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2276 GtkFileChooserButtonPrivate *priv = button->priv;
2279 gboolean local_only, retval, is_folder;
2281 type = ROW_TYPE_INVALID;
2283 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2285 gtk_tree_model_get (model, iter,
2288 IS_FOLDER_COLUMN, &is_folder,
2293 case ROW_TYPE_CURRENT_FOLDER:
2296 case ROW_TYPE_SPECIAL:
2297 case ROW_TYPE_SHORTCUT:
2298 case ROW_TYPE_BOOKMARK:
2299 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2301 case ROW_TYPE_VOLUME:
2306 if (_gtk_file_system_volume_is_mounted (data))
2310 base_file = _gtk_file_system_volume_get_root (data);
2314 if (!_gtk_file_has_native_path (base_file))
2316 g_object_unref (base_file);
2324 case ROW_TYPE_EMPTY_SELECTION:
2326 gboolean popup_shown;
2328 g_object_get (priv->combo_box,
2329 "popup-shown", &popup_shown,
2338 /* When the combo box is not popped up... */
2340 selected = get_selected_file (button);
2342 retval = FALSE; /* ... nonempty selection means the ROW_TYPE_EMPTY_SELECTION is *not* visible... */
2344 retval = TRUE; /* ... and empty selection means the ROW_TYPE_EMPTY_SELECTION *is* visible */
2347 g_object_unref (selected);
2362 name_cell_data_func (GtkCellLayout *layout,
2363 GtkCellRenderer *cell,
2364 GtkTreeModel *model,
2371 gtk_tree_model_get (model, iter,
2375 if (type == ROW_TYPE_CURRENT_FOLDER)
2376 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2378 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2382 combo_box_row_separator_func (GtkTreeModel *model,
2386 gchar type = ROW_TYPE_INVALID;
2388 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2390 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2391 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2392 type == ROW_TYPE_OTHER_SEPARATOR);
2396 select_combo_box_row_no_notify (GtkFileChooserButton *button, int pos)
2398 GtkFileChooserButtonPrivate *priv = button->priv;
2399 GtkTreeIter iter, filter_iter;
2401 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2402 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2403 &filter_iter, &iter);
2405 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2406 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2407 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2411 update_combo_box (GtkFileChooserButton *button)
2413 GtkFileChooserButtonPrivate *priv = button->priv;
2418 file = get_selected_file (button);
2422 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2429 type = ROW_TYPE_INVALID;
2432 gtk_tree_model_get (priv->filter_model, &iter,
2439 case ROW_TYPE_SPECIAL:
2440 case ROW_TYPE_SHORTCUT:
2441 case ROW_TYPE_BOOKMARK:
2442 case ROW_TYPE_CURRENT_FOLDER:
2443 row_found = (file && g_file_equal (data, file));
2445 case ROW_TYPE_VOLUME:
2449 base_file = _gtk_file_system_volume_get_root (data);
2452 row_found = (file && g_file_equal (base_file, file));
2453 g_object_unref (base_file);
2464 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2465 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2467 g_signal_handler_unblock (priv->combo_box,
2468 priv->combo_box_changed_id);
2471 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2477 /* If it hasn't been found already, update & select the current-folder row. */
2480 model_update_current_folder (button, file);
2481 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2485 /* No selection; switch to that row */
2487 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2490 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2492 select_combo_box_row_no_notify (button, pos);
2496 g_object_unref (file);
2501 update_label_get_info_cb (GCancellable *cancellable,
2503 const GError *error,
2506 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2508 GtkFileChooserButton *button = data;
2509 GtkFileChooserButtonPrivate *priv = button->priv;
2511 if (cancellable != priv->update_button_cancellable)
2514 priv->update_button_cancellable = NULL;
2516 if (cancelled || error)
2519 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2521 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2524 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2526 priv->icon_size, 0, NULL);
2528 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2530 g_object_unref (pixbuf);
2533 emit_selection_changed_if_changing_selection (button);
2535 g_object_unref (button);
2536 g_object_unref (cancellable);
2540 update_label_and_image (GtkFileChooserButton *button)
2542 GtkFileChooserButtonPrivate *priv = button->priv;
2545 gboolean done_changing_selection;
2547 file = get_selected_file (button);
2550 done_changing_selection = FALSE;
2552 if (priv->update_button_cancellable)
2554 g_cancellable_cancel (priv->update_button_cancellable);
2555 priv->update_button_cancellable = NULL;
2560 GtkFileSystemVolume *volume = NULL;
2562 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2567 base_file = _gtk_file_system_volume_get_root (volume);
2568 if (base_file && g_file_equal (base_file, file))
2572 label_text = _gtk_file_system_volume_get_display_name (volume);
2573 pixbuf = _gtk_file_system_volume_render_icon (volume,
2574 GTK_WIDGET (button),
2577 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2579 g_object_unref (pixbuf);
2583 g_object_unref (base_file);
2585 _gtk_file_system_volume_unref (volume);
2589 done_changing_selection = TRUE;
2594 if (g_file_is_native (file))
2596 priv->update_button_cancellable =
2597 _gtk_file_system_get_info (priv->fs, file,
2598 "standard::icon,standard::display-name",
2599 update_label_get_info_cb,
2600 g_object_ref (button));
2606 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2607 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2609 priv->icon_size, 0, NULL);
2610 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2612 g_object_unref (pixbuf);
2614 done_changing_selection = TRUE;
2619 /* We know the selection is empty */
2620 done_changing_selection = TRUE;
2626 g_object_unref (file);
2630 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2631 g_free (label_text);
2635 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2636 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2639 if (done_changing_selection)
2640 emit_selection_changed_if_changing_selection (button);
2644 /* ************************ *
2645 * Child Object Callbacks *
2646 * ************************ */
2650 fs_volumes_changed_cb (GtkFileSystem *fs,
2653 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2654 GtkFileChooserButtonPrivate *priv = button->priv;
2657 model_remove_rows (user_data,
2658 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2661 priv->n_volumes = 0;
2663 volumes = _gtk_file_system_list_volumes (fs);
2664 model_add_volumes (user_data, volumes);
2665 g_slist_free (volumes);
2667 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2669 update_label_and_image (user_data);
2670 update_combo_box (user_data);
2674 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2677 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2678 GtkFileChooserButtonPrivate *priv = button->priv;
2681 bookmarks = _gtk_file_system_list_bookmarks (fs);
2682 model_remove_rows (user_data,
2683 model_get_type_position (user_data,
2684 ROW_TYPE_BOOKMARK_SEPARATOR),
2685 (priv->n_bookmarks + priv->has_bookmark_separator));
2686 priv->has_bookmark_separator = FALSE;
2687 priv->n_bookmarks = 0;
2688 model_add_bookmarks (user_data, bookmarks);
2689 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2690 g_slist_free (bookmarks);
2692 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2694 update_label_and_image (user_data);
2695 update_combo_box (user_data);
2699 save_inactive_state (GtkFileChooserButton *button)
2701 GtkFileChooserButtonPrivate *priv = button->priv;
2703 if (priv->current_folder_while_inactive)
2704 g_object_unref (priv->current_folder_while_inactive);
2706 if (priv->selection_while_inactive)
2707 g_object_unref (priv->selection_while_inactive);
2709 priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->dialog));
2710 priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2714 restore_inactive_state (GtkFileChooserButton *button)
2716 GtkFileChooserButtonPrivate *priv = button->priv;
2718 if (priv->current_folder_while_inactive)
2719 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), priv->current_folder_while_inactive, NULL);
2721 if (priv->selection_while_inactive)
2722 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2724 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2729 open_dialog (GtkFileChooserButton *button)
2731 GtkFileChooserButtonPrivate *priv = button->priv;
2733 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2735 if (!gtk_widget_get_visible (priv->dialog))
2737 GtkWidget *toplevel;
2739 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2741 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2743 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2744 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2745 GTK_WINDOW (toplevel));
2747 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2748 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2754 restore_inactive_state (button);
2755 priv->active = TRUE;
2758 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2759 gtk_window_present (GTK_WINDOW (priv->dialog));
2764 combo_box_changed_cb (GtkComboBox *combo_box,
2769 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2771 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2772 GtkFileChooserButtonPrivate *priv = button->priv;
2776 type = ROW_TYPE_INVALID;
2779 gtk_tree_model_get (priv->filter_model, &iter,
2786 case ROW_TYPE_SPECIAL:
2787 case ROW_TYPE_SHORTCUT:
2788 case ROW_TYPE_BOOKMARK:
2789 case ROW_TYPE_CURRENT_FOLDER:
2791 gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), data, NULL);
2793 case ROW_TYPE_VOLUME:
2797 base_file = _gtk_file_system_volume_get_root (data);
2800 gtk_file_chooser_button_select_file (GTK_FILE_CHOOSER (button), base_file, NULL);
2801 g_object_unref (base_file);
2805 case ROW_TYPE_OTHER:
2806 open_dialog (user_data);
2814 /* Calback for the "notify::popup-shown" signal on the combo box.
2815 * When the combo is popped up, we don't want the ROW_TYPE_EMPTY_SELECTION to be visible
2816 * at all; otherwise we would be showing a "(None)" item in the combo box's popup.
2818 * However, when the combo box is *not* popped up, we want the empty-selection row
2819 * to be visible depending on the selection.
2821 * Since all that is done through the filter_model_visible_func(), this means
2822 * that we need to refilter the model when the combo box pops up - hence the
2823 * present signal handler.
2826 combo_box_notify_popup_shown_cb (GObject *object,
2830 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2831 GtkFileChooserButtonPrivate *priv = button->priv;
2832 gboolean popup_shown;
2834 g_object_get (priv->combo_box,
2835 "popup-shown", &popup_shown,
2838 /* Indicate that the ROW_TYPE_EMPTY_SELECTION will change visibility... */
2839 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2841 /* If the combo box popup got dismissed, go back to showing the ROW_TYPE_EMPTY_SELECTION if needed */
2844 GFile *selected = get_selected_file (button);
2850 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2851 select_combo_box_row_no_notify (button, pos);
2854 g_object_unref (selected);
2860 button_clicked_cb (GtkButton *real_button,
2863 open_dialog (user_data);
2869 dialog_update_preview_cb (GtkFileChooser *dialog,
2872 g_signal_emit_by_name (user_data, "update-preview");
2876 dialog_notify_cb (GObject *dialog,
2882 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2883 GTK_TYPE_FILE_CHOOSER);
2884 if (g_object_interface_find_property (iface, pspec->name))
2885 g_object_notify (user_data, pspec->name);
2887 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2889 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2890 GtkFileChooserButtonPrivate *priv = button->priv;
2892 if (priv->has_current_folder)
2898 pos = model_get_type_position (user_data,
2899 ROW_TYPE_CURRENT_FOLDER);
2900 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2903 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2905 /* If the path isn't local but we're in local-only mode now, remove
2906 * the custom-folder row */
2907 if (data && _gtk_file_has_native_path (G_FILE (data)) &&
2908 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2911 model_remove_rows (user_data, pos, 2);
2915 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2916 update_combo_box (user_data);
2921 dialog_delete_event_cb (GtkWidget *dialog,
2925 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2931 dialog_response_cb (GtkDialog *dialog,
2935 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2936 GtkFileChooserButtonPrivate *priv = button->priv;
2938 if (response == GTK_RESPONSE_ACCEPT ||
2939 response == GTK_RESPONSE_OK)
2941 save_inactive_state (button);
2943 g_signal_emit_by_name (button, "current-folder-changed");
2944 g_signal_emit_by_name (button, "selection-changed");
2948 restore_inactive_state (button);
2952 priv->active = FALSE;
2954 update_label_and_image (button);
2955 update_combo_box (button);
2957 gtk_widget_set_sensitive (priv->combo_box, TRUE);
2958 gtk_widget_hide (priv->dialog);
2960 if (response == GTK_RESPONSE_ACCEPT ||
2961 response == GTK_RESPONSE_OK)
2962 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
2966 /* ************************************************************************** *
2968 * ************************************************************************** */
2971 * gtk_file_chooser_button_new:
2972 * @title: the title of the browse dialog.
2973 * @action: the open mode for the widget.
2975 * Creates a new file-selecting button widget.
2977 * Returns: a new button widget.
2982 gtk_file_chooser_button_new (const gchar *title,
2983 GtkFileChooserAction action)
2985 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2986 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2988 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2990 "title", (title ? title : _(DEFAULT_TITLE)),
2995 * gtk_file_chooser_button_new_with_dialog:
2996 * @dialog: the widget to use as dialog
2998 * Creates a #GtkFileChooserButton widget which uses @dialog as its
2999 * file-picking window.
3001 * Note that @dialog must be a #GtkDialog (or subclass) which
3002 * implements the #GtkFileChooser interface and must not have
3003 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
3005 * Also note that the dialog needs to have its confirmative button
3006 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
3007 * order for the button to take over the file selected in the dialog.
3009 * Returns: a new button widget.
3014 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
3016 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
3018 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
3024 * gtk_file_chooser_button_set_title:
3025 * @button: the button widget to modify.
3026 * @title: the new browse dialog title.
3028 * Modifies the @title of the browse dialog used by @button.
3033 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
3036 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3038 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
3039 g_object_notify (G_OBJECT (button), "title");
3043 * gtk_file_chooser_button_get_title:
3044 * @button: the button widget to examine.
3046 * Retrieves the title of the browse dialog used by @button. The returned value
3047 * should not be modified or freed.
3049 * Returns: a pointer to the browse dialog's title.
3054 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
3056 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
3058 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
3062 * gtk_file_chooser_button_get_width_chars:
3063 * @button: the button widget to examine.
3065 * Retrieves the width in characters of the @button widget's entry and/or label.
3067 * Returns: an integer width (in characters) that the button will use to size itself.
3072 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
3074 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
3076 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
3080 * gtk_file_chooser_button_set_width_chars:
3081 * @button: the button widget to examine.
3082 * @n_chars: the new width, in characters.
3084 * Sets the width (in characters) that @button will use to @n_chars.
3089 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
3092 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3094 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
3095 g_object_notify (G_OBJECT (button), "width-chars");
3099 * gtk_file_chooser_button_set_focus_on_click:
3100 * @button: a #GtkFileChooserButton
3101 * @focus_on_click: whether the button grabs focus when clicked with the mouse
3103 * Sets whether the button will grab focus when it is clicked with the mouse.
3104 * Making mouse clicks not grab focus is useful in places like toolbars where
3105 * you don't want the keyboard focus removed from the main area of the
3111 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
3112 gboolean focus_on_click)
3114 GtkFileChooserButtonPrivate *priv;
3116 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3118 priv = button->priv;
3120 focus_on_click = focus_on_click != FALSE;
3122 if (priv->focus_on_click != focus_on_click)
3124 priv->focus_on_click = focus_on_click;
3125 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
3126 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
3128 g_object_notify (G_OBJECT (button), "focus-on-click");
3133 * gtk_file_chooser_button_get_focus_on_click:
3134 * @button: a #GtkFileChooserButton
3136 * Returns whether the button grabs focus when it is clicked with the mouse.
3137 * See gtk_file_chooser_button_set_focus_on_click().
3139 * Return value: %TRUE if the button grabs focus when it is clicked with
3145 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
3147 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
3149 return button->priv->focus_on_click;