1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
3 /* GTK+: gtkfilechooserbutton.c
5 * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
32 #include "gtkbutton.h"
33 #include "gtkcelllayout.h"
34 #include "gtkcellrenderertext.h"
35 #include "gtkcellrendererpixbuf.h"
36 #include "gtkcombobox.h"
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
42 #include "gtkliststore.h"
44 #include "gtktreemodelfilter.h"
45 #include "gtkseparator.h"
46 #include "gtkfilechooserdialog.h"
47 #include "gtkfilechooserprivate.h"
48 #include "gtkfilechooserutils.h"
49 #include "gtkmarshalers.h"
51 #include "gtkfilechooserbutton.h"
53 #include "gtkorientable.h"
55 #include "gtktypebuiltins.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
61 * SECTION:gtkfilechooserbutton
62 * @Short_description: A button to launch a file selection dialog
63 * @Title: GtkFileChooserButton
64 * @See_also:#GtkFileChooserDialog
66 * The #GtkFileChooserButton is a widget that lets the user select a
67 * file. It implements the #GtkFileChooser interface. Visually, it is a
68 * file name with a button to bring up a #GtkFileChooserDialog.
69 * The user can then use that dialog to change the file associated with
70 * that button. This widget does not support setting the
71 * #GtkFileChooser:select-multiple property to %TRUE.
74 * <title>Create a button to let the user select a file in /etc</title>
79 * button = gtk_file_chooser_button_new (_("Select a file"),
80 * GTK_FILE_CHOOSER_ACTION_OPEN);
81 * gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
87 * The #GtkFileChooserButton supports the #GtkFileChooserAction<!-- -->s
88 * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
91 * The #GtkFileChooserButton will ellipsize the label,
92 * and thus will thus request little horizontal space. To give the button
93 * more space, you should call gtk_widget_get_preferred_size(),
94 * gtk_file_chooser_button_set_width_chars(), or pack the button in
95 * such a way that other interface elements give space to the widget.
100 /* **************** *
102 * **************** */
104 #define DEFAULT_TITLE N_("Select a File")
105 #define DESKTOP_DISPLAY_NAME N_("Desktop")
106 #define FALLBACK_DISPLAY_NAME N_("(None)")
107 #define FALLBACK_ICON_NAME "stock_unknown"
108 #define FALLBACK_ICON_SIZE 16
111 /* ********************** *
112 * Private Enumerations *
113 * ********************** */
133 /* TreeModel Columns */
145 /* TreeModel Row Types */
151 ROW_TYPE_BOOKMARK_SEPARATOR,
153 ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
154 ROW_TYPE_CURRENT_FOLDER,
155 ROW_TYPE_OTHER_SEPARATOR,
158 ROW_TYPE_INVALID = -1
163 /* ******************** *
164 * Private Structures *
165 * ******************** */
167 struct _GtkFileChooserButtonPrivate
173 GtkWidget *combo_box;
174 GtkCellRenderer *icon_cell;
175 GtkCellRenderer *name_cell;
178 GtkTreeModel *filter_model;
181 GFile *selection_while_inactive;
182 GFile *current_folder_while_inactive;
184 gulong combo_box_changed_id;
185 gulong dialog_file_activated_id;
186 gulong dialog_folder_changed_id;
187 gulong dialog_selection_changed_id;
188 gulong fs_volumes_changed_id;
189 gulong fs_bookmarks_changed_id;
191 GCancellable *dnd_select_folder_cancellable;
192 GCancellable *update_button_cancellable;
193 GSList *change_icon_theme_cancellables;
201 guint has_bookmark_separator : 1;
202 guint has_current_folder_separator : 1;
203 guint has_current_folder : 1;
204 guint has_other_separator : 1;
206 /* Used for hiding/showing the dialog when the button is hidden */
209 guint focus_on_click : 1;
224 /* ********************* *
225 * Function Prototypes *
226 * ********************* */
228 /* GtkFileChooserIface Functions */
229 static void gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
230 static gboolean gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
233 static GFile *gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser);
234 static gboolean gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
237 static void gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
239 static void gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser);
240 static GSList *gtk_file_chooser_button_get_files (GtkFileChooser *chooser);
241 static gboolean gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
244 static gboolean gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
248 /* GObject Functions */
249 static GObject *gtk_file_chooser_button_constructor (GType type,
251 GObjectConstructParam *params);
252 static void gtk_file_chooser_button_set_property (GObject *object,
256 static void gtk_file_chooser_button_get_property (GObject *object,
260 static void gtk_file_chooser_button_finalize (GObject *object);
262 /* GtkWidget Functions */
263 static void gtk_file_chooser_button_destroy (GtkWidget *widget);
264 static void gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
265 GdkDragContext *context,
268 GtkSelectionData *data,
271 static void gtk_file_chooser_button_show_all (GtkWidget *widget);
272 static void gtk_file_chooser_button_show (GtkWidget *widget);
273 static void gtk_file_chooser_button_hide (GtkWidget *widget);
274 static void gtk_file_chooser_button_map (GtkWidget *widget);
275 static gboolean gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
276 gboolean group_cycling);
277 static void gtk_file_chooser_button_style_updated (GtkWidget *widget);
278 static void gtk_file_chooser_button_screen_changed (GtkWidget *widget,
279 GdkScreen *old_screen);
281 /* Utility Functions */
282 static GtkIconTheme *get_icon_theme (GtkWidget *widget);
283 static void set_info_for_file_at_iter (GtkFileChooserButton *fs,
287 static gint model_get_type_position (GtkFileChooserButton *button,
289 static void model_free_row_data (GtkFileChooserButton *button,
291 static inline void model_add_special (GtkFileChooserButton *button);
292 static inline void model_add_other (GtkFileChooserButton *button);
293 static void model_add_volumes (GtkFileChooserButton *button,
295 static void model_add_bookmarks (GtkFileChooserButton *button,
297 static void model_update_current_folder (GtkFileChooserButton *button,
299 static void model_remove_rows (GtkFileChooserButton *button,
303 static gboolean filter_model_visible_func (GtkTreeModel *model,
307 static gboolean combo_box_row_separator_func (GtkTreeModel *model,
310 static void name_cell_data_func (GtkCellLayout *layout,
311 GtkCellRenderer *cell,
315 static void open_dialog (GtkFileChooserButton *button);
316 static void update_combo_box (GtkFileChooserButton *button);
317 static void update_label_and_image (GtkFileChooserButton *button);
319 /* Child Object Callbacks */
320 static void fs_volumes_changed_cb (GtkFileSystem *fs,
322 static void fs_bookmarks_changed_cb (GtkFileSystem *fs,
325 static void combo_box_changed_cb (GtkComboBox *combo_box,
328 static void button_clicked_cb (GtkButton *real_button,
331 static void dialog_update_preview_cb (GtkFileChooser *dialog,
333 static void dialog_selection_changed_cb (GtkFileChooser *dialog,
335 static void dialog_file_activated_cb (GtkFileChooser *dialog,
337 static void dialog_current_folder_changed_cb (GtkFileChooser *dialog,
339 static void dialog_notify_cb (GObject *dialog,
342 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
345 static void dialog_response_cb (GtkDialog *dialog,
349 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
351 /* ******************* *
352 * GType Declaration *
353 * ******************* */
355 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
356 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
360 /* ***************** *
362 * ***************** */
365 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
367 GObjectClass *gobject_class;
368 GtkWidgetClass *widget_class;
370 gobject_class = G_OBJECT_CLASS (class);
371 widget_class = GTK_WIDGET_CLASS (class);
373 gobject_class->constructor = gtk_file_chooser_button_constructor;
374 gobject_class->set_property = gtk_file_chooser_button_set_property;
375 gobject_class->get_property = gtk_file_chooser_button_get_property;
376 gobject_class->finalize = gtk_file_chooser_button_finalize;
378 widget_class->destroy = gtk_file_chooser_button_destroy;
379 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
380 widget_class->show_all = gtk_file_chooser_button_show_all;
381 widget_class->show = gtk_file_chooser_button_show;
382 widget_class->hide = gtk_file_chooser_button_hide;
383 widget_class->map = gtk_file_chooser_button_map;
384 widget_class->style_updated = gtk_file_chooser_button_style_updated;
385 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
386 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
389 * GtkFileChooserButton::file-set:
390 * @widget: the object which received the signal.
392 * The ::file-set signal is emitted when the user selects a file.
394 * Note that this signal is only emitted when the <emphasis>user</emphasis>
399 file_chooser_button_signals[FILE_SET] =
400 g_signal_new (I_("file-set"),
401 G_TYPE_FROM_CLASS (gobject_class),
403 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
405 _gtk_marshal_VOID__VOID,
409 * GtkFileChooserButton:dialog:
411 * Instance of the #GtkFileChooserDialog associated with the button.
415 g_object_class_install_property (gobject_class, PROP_DIALOG,
416 g_param_spec_object ("dialog",
418 P_("The file chooser dialog to use."),
419 GTK_TYPE_FILE_CHOOSER,
420 (GTK_PARAM_WRITABLE |
421 G_PARAM_CONSTRUCT_ONLY)));
424 * GtkFileChooserButton:focus-on-click:
426 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
431 g_object_class_install_property (gobject_class,
433 g_param_spec_boolean ("focus-on-click",
434 P_("Focus on click"),
435 P_("Whether the button grabs focus when it is clicked with the mouse"),
437 GTK_PARAM_READWRITE));
440 * GtkFileChooserButton:title:
442 * Title to put on the #GtkFileChooserDialog associated with the button.
446 g_object_class_install_property (gobject_class, PROP_TITLE,
447 g_param_spec_string ("title",
449 P_("The title of the file chooser dialog."),
451 GTK_PARAM_READWRITE));
454 * GtkFileChooserButton:width-chars:
456 * The width of the entry and label inside the button, in characters.
460 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
461 g_param_spec_int ("width-chars",
462 P_("Width In Characters"),
463 P_("The desired width of the button widget, in characters."),
465 GTK_PARAM_READWRITE));
467 _gtk_file_chooser_install_properties (gobject_class);
469 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
473 gtk_file_chooser_button_init (GtkFileChooserButton *button)
475 GtkFileChooserButtonPrivate *priv;
476 GtkWidget *box, *image, *sep;
477 GtkTargetList *target_list;
479 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
480 GTK_TYPE_FILE_CHOOSER_BUTTON,
481 GtkFileChooserButtonPrivate);
483 priv->icon_size = FALLBACK_ICON_SIZE;
484 priv->focus_on_click = TRUE;
486 gtk_widget_push_composite_child ();
489 priv->button = gtk_button_new ();
490 g_signal_connect (priv->button, "clicked",
491 G_CALLBACK (button_clicked_cb), button);
492 gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
493 gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
494 gtk_widget_show (priv->button);
496 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
497 gtk_container_add (GTK_CONTAINER (priv->button), box);
498 gtk_widget_show (box);
500 priv->image = gtk_image_new ();
501 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
502 gtk_widget_show (priv->image);
504 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
505 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
506 gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
507 gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
508 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
509 //gtk_container_add (GTK_CONTAINER (box), priv->label);
510 gtk_widget_show (priv->label);
512 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
513 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
514 gtk_widget_show (sep);
516 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
517 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
518 gtk_widget_show (image);
521 /* Keep in sync with columns enum, line 88 */
523 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
524 GDK_TYPE_PIXBUF, /* Icon */
525 G_TYPE_STRING, /* Display Name */
526 G_TYPE_CHAR, /* Row Type */
527 G_TYPE_POINTER /* Volume || Path */,
528 G_TYPE_BOOLEAN /* Is Folder? */,
529 G_TYPE_POINTER /* cancellable */));
531 priv->combo_box = gtk_combo_box_new ();
532 priv->combo_box_changed_id =
533 g_signal_connect (priv->combo_box, "changed",
534 G_CALLBACK (combo_box_changed_cb), button);
535 gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
536 gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
538 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
539 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
540 priv->icon_cell, FALSE);
541 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
542 priv->icon_cell, "pixbuf", ICON_COLUMN);
544 priv->name_cell = gtk_cell_renderer_text_new ();
545 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
546 priv->name_cell, TRUE);
547 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
548 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
549 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
550 priv->name_cell, name_cell_data_func,
553 gtk_widget_pop_composite_child ();
556 gtk_drag_dest_set (GTK_WIDGET (button),
557 (GTK_DEST_DEFAULT_ALL),
560 target_list = gtk_target_list_new (NULL, 0);
561 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
562 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
563 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
564 gtk_target_list_unref (target_list);
568 /* ******************************* *
569 * GtkFileChooserIface Functions *
570 * ******************************* */
572 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
574 _gtk_file_chooser_delegate_iface_init (iface);
576 iface->set_current_folder = gtk_file_chooser_button_set_current_folder;
577 iface->get_current_folder = gtk_file_chooser_button_get_current_folder;
578 iface->select_file = gtk_file_chooser_button_select_file;
579 iface->unselect_file = gtk_file_chooser_button_unselect_file;
580 iface->unselect_all = gtk_file_chooser_button_unselect_all;
581 iface->get_files = gtk_file_chooser_button_get_files;
582 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
583 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
587 gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
591 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
592 GtkFileChooserButtonPrivate *priv = button->priv;
593 GtkFileChooser *delegate;
595 delegate = g_object_get_qdata (G_OBJECT (chooser),
596 GTK_FILE_CHOOSER_DELEGATE_QUARK);
599 return gtk_file_chooser_set_current_folder_file (delegate, file, error);
602 if (priv->current_folder_while_inactive)
603 g_object_unref (priv->current_folder_while_inactive);
605 priv->current_folder_while_inactive = g_object_ref (file);
607 g_signal_emit_by_name (button, "current-folder-changed");
614 gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser)
616 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
617 GtkFileChooserButtonPrivate *priv = button->priv;
618 GtkFileChooser *delegate;
620 delegate = g_object_get_qdata (G_OBJECT (chooser),
621 GTK_FILE_CHOOSER_DELEGATE_QUARK);
624 return gtk_file_chooser_get_current_folder_file (delegate);
627 if (priv->current_folder_while_inactive)
628 return g_object_ref (priv->current_folder_while_inactive);
635 gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
639 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
640 GtkFileChooserButtonPrivate *priv = button->priv;
641 GtkFileChooser *delegate;
643 delegate = g_object_get_qdata (G_OBJECT (chooser),
644 GTK_FILE_CHOOSER_DELEGATE_QUARK);
647 return gtk_file_chooser_select_file (delegate, file, error);
650 if (priv->selection_while_inactive)
651 g_object_unref (priv->selection_while_inactive);
653 priv->selection_while_inactive = g_object_ref (file);
660 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
663 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
664 GtkFileChooserButtonPrivate *priv = button->priv;
665 GtkFileChooser *delegate;
667 delegate = g_object_get_qdata (G_OBJECT (chooser),
668 GTK_FILE_CHOOSER_DELEGATE_QUARK);
671 gtk_file_chooser_unselect_file (delegate, file);
674 if (g_file_equal (priv->selection_while_inactive, file))
676 if (priv->selection_while_inactive)
678 g_object_unref (priv->selection_while_inactive);
679 priv->selection_while_inactive = NULL;
686 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
688 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
689 GtkFileChooserButtonPrivate *priv = button->priv;
690 GtkFileChooser *delegate;
692 delegate = g_object_get_qdata (G_OBJECT (chooser),
693 GTK_FILE_CHOOSER_DELEGATE_QUARK);
696 gtk_file_chooser_unselect_all (delegate);
699 if (priv->selection_while_inactive)
701 g_object_unref (priv->selection_while_inactive);
702 priv->selection_while_inactive = NULL;
708 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
710 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
711 GtkFileChooserButtonPrivate *priv = button->priv;
712 GtkFileChooser *delegate;
714 delegate = g_object_get_qdata (G_OBJECT (chooser),
715 GTK_FILE_CHOOSER_DELEGATE_QUARK);
718 return gtk_file_chooser_get_files (delegate);
725 if (priv->selection_while_inactive)
726 result = g_slist_prepend (NULL, g_object_ref (priv->selection_while_inactive));
727 else if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
729 /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
730 * the current folder, since that is what GtkFileChooserDefault would do.
732 if (priv->current_folder_while_inactive)
733 result = g_slist_prepend (NULL, g_object_ref (priv->current_folder_while_inactive));
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);
883 priv->dialog_folder_changed_id =
884 g_signal_connect (priv->dialog, "current-folder-changed",
885 G_CALLBACK (dialog_current_folder_changed_cb), object);
886 priv->dialog_file_activated_id =
887 g_signal_connect (priv->dialog, "file-activated",
888 G_CALLBACK (dialog_file_activated_cb), object);
889 priv->dialog_selection_changed_id =
890 g_signal_connect (priv->dialog, "selection-changed",
891 G_CALLBACK (dialog_selection_changed_cb), object);
892 g_signal_connect (priv->dialog, "update-preview",
893 G_CALLBACK (dialog_update_preview_cb), object);
894 g_signal_connect (priv->dialog, "notify",
895 G_CALLBACK (dialog_notify_cb), object);
896 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
897 (gpointer) (&priv->dialog));
900 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
902 model_add_special (button);
904 list = _gtk_file_system_list_volumes (priv->fs);
905 model_add_volumes (button, list);
908 list = _gtk_file_system_list_bookmarks (priv->fs);
909 model_add_bookmarks (button, list);
910 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
913 model_add_other (button);
915 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
916 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
917 filter_model_visible_func,
920 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
921 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
922 combo_box_row_separator_func,
925 /* set up the action for a user-provided dialog, this also updates
926 * the label, image and combobox
928 g_object_set (object,
929 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
932 priv->fs_volumes_changed_id =
933 g_signal_connect (priv->fs, "volumes-changed",
934 G_CALLBACK (fs_volumes_changed_cb), object);
935 priv->fs_bookmarks_changed_id =
936 g_signal_connect (priv->fs, "bookmarks-changed",
937 G_CALLBACK (fs_bookmarks_changed_cb), object);
943 gtk_file_chooser_button_set_property (GObject *object,
948 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
949 GtkFileChooserButtonPrivate *priv = button->priv;
955 priv->dialog = g_value_get_object (value);
957 case PROP_FOCUS_ON_CLICK:
958 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
960 case PROP_WIDTH_CHARS:
961 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
962 g_value_get_int (value));
964 case GTK_FILE_CHOOSER_PROP_ACTION:
965 switch (g_value_get_enum (value))
967 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
968 case GTK_FILE_CHOOSER_ACTION_SAVE:
973 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
974 eval = g_enum_get_value (eclass, g_value_get_enum (value));
975 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
976 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
978 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
983 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
984 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
985 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
987 switch (g_value_get_enum (value))
989 case GTK_FILE_CHOOSER_ACTION_OPEN:
990 gtk_widget_hide (priv->combo_box);
991 gtk_widget_show (priv->button);
993 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
994 gtk_widget_hide (priv->button);
995 gtk_widget_show (priv->combo_box);
998 g_assert_not_reached ();
1004 case GTK_FILE_CHOOSER_PROP_FILTER:
1005 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1006 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1007 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1008 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1009 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1010 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1011 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1012 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1015 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1016 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1017 fs_volumes_changed_cb (priv->fs, button);
1018 fs_bookmarks_changed_cb (priv->fs, button);
1021 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1022 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
1023 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
1026 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1032 gtk_file_chooser_button_get_property (GObject *object,
1037 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1038 GtkFileChooserButtonPrivate *priv = button->priv;
1042 case PROP_WIDTH_CHARS:
1043 g_value_set_int (value,
1044 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
1046 case PROP_FOCUS_ON_CLICK:
1047 g_value_set_boolean (value,
1048 gtk_file_chooser_button_get_focus_on_click (button));
1052 case GTK_FILE_CHOOSER_PROP_ACTION:
1053 case GTK_FILE_CHOOSER_PROP_FILTER:
1054 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1055 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1056 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1057 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1058 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1059 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1060 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1061 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1062 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1063 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1067 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1073 gtk_file_chooser_button_finalize (GObject *object)
1075 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1076 GtkFileChooserButtonPrivate *priv = button->priv;
1078 if (priv->selection_while_inactive)
1079 g_object_unref (priv->selection_while_inactive);
1081 if (priv->current_folder_while_inactive)
1082 g_object_unref (priv->current_folder_while_inactive);
1084 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1087 /* ********************* *
1088 * GtkWidget Functions *
1089 * ********************* */
1092 gtk_file_chooser_button_destroy (GtkWidget *widget)
1094 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1095 GtkFileChooserButtonPrivate *priv = button->priv;
1099 if (priv->dialog != NULL)
1101 gtk_widget_destroy (priv->dialog);
1102 priv->dialog = NULL;
1105 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1107 model_free_row_data (button, &iter);
1109 while (gtk_tree_model_iter_next (priv->model, &iter));
1111 if (priv->dnd_select_folder_cancellable)
1113 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1114 priv->dnd_select_folder_cancellable = NULL;
1117 if (priv->update_button_cancellable)
1119 g_cancellable_cancel (priv->update_button_cancellable);
1120 priv->update_button_cancellable = NULL;
1123 if (priv->change_icon_theme_cancellables)
1125 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1127 GCancellable *cancellable = G_CANCELLABLE (l->data);
1128 g_cancellable_cancel (cancellable);
1130 g_slist_free (priv->change_icon_theme_cancellables);
1131 priv->change_icon_theme_cancellables = NULL;
1136 g_object_unref (priv->model);
1140 if (priv->filter_model)
1142 g_object_unref (priv->filter_model);
1143 priv->filter_model = NULL;
1148 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1149 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1150 g_object_unref (priv->fs);
1154 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1157 struct DndSelectFolderData
1159 GtkFileSystem *file_system;
1160 GtkFileChooserButton *button;
1161 GtkFileChooserAction action;
1169 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1171 const GError *error,
1174 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1175 struct DndSelectFolderData *data = user_data;
1177 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1179 g_object_unref (data->button);
1180 g_object_unref (data->file);
1181 g_strfreev (data->uris);
1184 g_object_unref (cancellable);
1188 data->button->priv->dnd_select_folder_cancellable = NULL;
1190 if (!cancelled && !error && info != NULL)
1194 is_folder = _gtk_file_info_consider_as_directory (info);
1197 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1198 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1199 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1203 data->selected = FALSE;
1205 if (data->selected || data->uris[++data->i] == NULL)
1207 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1209 g_object_unref (data->button);
1210 g_object_unref (data->file);
1211 g_strfreev (data->uris);
1214 g_object_unref (cancellable);
1219 g_object_unref (data->file);
1221 data->file = g_file_new_for_uri (data->uris[data->i]);
1223 data->button->priv->dnd_select_folder_cancellable =
1224 _gtk_file_system_get_info (data->file_system, data->file,
1226 dnd_select_folder_get_info_cb, user_data);
1228 g_object_unref (cancellable);
1232 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1233 GdkDragContext *context,
1236 GtkSelectionData *data,
1240 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1241 GtkFileChooserButtonPrivate *priv = button->priv;
1245 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1246 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1252 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1260 struct DndSelectFolderData *info;
1262 uris = gtk_selection_data_get_uris (data);
1267 info = g_new0 (struct DndSelectFolderData, 1);
1268 info->button = g_object_ref (button);
1271 info->selected = FALSE;
1272 info->file_system = priv->fs;
1273 g_object_get (priv->dialog, "action", &info->action, NULL);
1275 info->file = g_file_new_for_uri (info->uris[info->i]);
1277 if (priv->dnd_select_folder_cancellable)
1278 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1280 priv->dnd_select_folder_cancellable =
1281 _gtk_file_system_get_info (priv->fs, info->file,
1283 dnd_select_folder_get_info_cb, info);
1288 text = (char*) gtk_selection_data_get_text (data);
1289 file = g_file_new_for_uri (text);
1290 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1292 g_object_unref (file);
1294 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1301 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1305 gtk_file_chooser_button_show_all (GtkWidget *widget)
1307 gtk_widget_show (widget);
1311 gtk_file_chooser_button_show (GtkWidget *widget)
1313 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1314 GtkFileChooserButtonPrivate *priv = button->priv;
1316 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1317 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1320 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1324 gtk_file_chooser_button_hide (GtkWidget *widget)
1326 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1327 GtkFileChooserButtonPrivate *priv = button->priv;
1329 gtk_widget_hide (priv->dialog);
1331 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1332 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1336 gtk_file_chooser_button_map (GtkWidget *widget)
1338 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1342 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1343 gboolean group_cycling)
1345 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1346 GtkFileChooserButtonPrivate *priv = button->priv;
1348 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1350 case GTK_FILE_CHOOSER_ACTION_OPEN:
1351 gtk_widget_grab_focus (priv->button);
1353 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1354 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1357 g_assert_not_reached ();
1364 /* Changes the icons wherever it is needed */
1365 struct ChangeIconThemeData
1367 GtkFileChooserButton *button;
1368 GtkTreeRowReference *row_ref;
1372 change_icon_theme_get_info_cb (GCancellable *cancellable,
1374 const GError *error,
1377 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1379 struct ChangeIconThemeData *data = user_data;
1381 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1384 data->button->priv->change_icon_theme_cancellables =
1385 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1387 if (cancelled || error)
1390 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1398 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1400 path = gtk_tree_row_reference_get_path (data->row_ref);
1403 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1404 gtk_tree_path_free (path);
1406 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1407 ICON_COLUMN, pixbuf,
1410 g_object_set (data->button->priv->icon_cell,
1414 g_object_unref (pixbuf);
1418 g_object_unref (data->button);
1419 gtk_tree_row_reference_free (data->row_ref);
1422 g_object_unref (cancellable);
1426 change_icon_theme (GtkFileChooserButton *button)
1428 GtkFileChooserButtonPrivate *priv = button->priv;
1429 GtkSettings *settings;
1430 GtkIconTheme *theme;
1433 gint width = 0, height = 0;
1435 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1437 GCancellable *cancellable = G_CANCELLABLE (l->data);
1438 g_cancellable_cancel (cancellable);
1440 g_slist_free (button->priv->change_icon_theme_cancellables);
1441 button->priv->change_icon_theme_cancellables = NULL;
1443 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1445 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1447 priv->icon_size = MAX (width, height);
1449 priv->icon_size = FALLBACK_ICON_SIZE;
1451 update_label_and_image (button);
1453 gtk_tree_model_get_iter_first (priv->model, &iter);
1455 theme = get_icon_theme (GTK_WIDGET (button));
1463 type = ROW_TYPE_INVALID;
1464 gtk_tree_model_get (priv->model, &iter,
1471 case ROW_TYPE_SPECIAL:
1472 case ROW_TYPE_SHORTCUT:
1473 case ROW_TYPE_BOOKMARK:
1474 case ROW_TYPE_CURRENT_FOLDER:
1477 if (g_file_is_native (G_FILE (data)))
1480 GCancellable *cancellable;
1481 struct ChangeIconThemeData *info;
1483 info = g_new0 (struct ChangeIconThemeData, 1);
1484 info->button = g_object_ref (button);
1485 path = gtk_tree_model_get_path (priv->model, &iter);
1486 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1487 gtk_tree_path_free (path);
1490 _gtk_file_system_get_info (priv->fs, data,
1492 change_icon_theme_get_info_cb,
1494 button->priv->change_icon_theme_cancellables =
1495 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1499 /* Don't call get_info for remote paths to avoid latency and
1501 * If we switch to a better bookmarks file format (XBEL), we
1502 * should use mime info to get a better icon.
1504 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1505 priv->icon_size, 0, NULL);
1508 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1509 priv->icon_size, 0, NULL);
1511 case ROW_TYPE_VOLUME:
1513 pixbuf = _gtk_file_system_volume_render_icon (data,
1514 GTK_WIDGET (button),
1518 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1519 priv->icon_size, 0, NULL);
1527 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1529 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1530 ICON_COLUMN, pixbuf,
1534 g_object_unref (pixbuf);
1536 while (gtk_tree_model_iter_next (priv->model, &iter));
1538 g_object_set (button->priv->icon_cell,
1544 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1546 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1548 if (gtk_widget_has_screen (widget))
1549 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1553 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1554 GdkScreen *old_screen)
1556 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1557 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1560 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1564 /* ******************* *
1565 * Utility Functions *
1566 * ******************* */
1569 static GtkIconTheme *
1570 get_icon_theme (GtkWidget *widget)
1572 if (gtk_widget_has_screen (widget))
1573 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1575 return gtk_icon_theme_get_default ();
1579 struct SetDisplayNameData
1581 GtkFileChooserButton *button;
1583 GtkTreeRowReference *row_ref;
1587 set_info_get_info_cb (GCancellable *cancellable,
1589 const GError *error,
1590 gpointer callback_data)
1592 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1596 GCancellable *model_cancellable = NULL;
1597 struct SetDisplayNameData *data = callback_data;
1600 if (!data->button->priv->model)
1601 /* button got destroyed */
1604 path = gtk_tree_row_reference_get_path (data->row_ref);
1606 /* Cancellable doesn't exist anymore in the model */
1609 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1610 gtk_tree_path_free (path);
1612 /* Validate the cancellable */
1613 gtk_tree_model_get (data->button->priv->model, &iter,
1614 CANCELLABLE_COLUMN, &model_cancellable,
1616 if (cancellable != model_cancellable)
1619 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1620 CANCELLABLE_COLUMN, NULL,
1623 if (cancelled || error)
1624 /* There was an error, leave the fallback name in there */
1627 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1630 data->label = g_strdup (g_file_info_get_display_name (info));
1632 is_folder = _gtk_file_info_consider_as_directory (info);
1634 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1635 ICON_COLUMN, pixbuf,
1636 DISPLAY_NAME_COLUMN, data->label,
1637 IS_FOLDER_COLUMN, is_folder,
1641 g_object_unref (pixbuf);
1644 g_object_unref (data->button);
1645 g_free (data->label);
1646 gtk_tree_row_reference_free (data->row_ref);
1649 if (model_cancellable)
1650 g_object_unref (model_cancellable);
1654 set_info_for_file_at_iter (GtkFileChooserButton *button,
1658 struct SetDisplayNameData *data;
1659 GtkTreePath *tree_path;
1660 GCancellable *cancellable;
1662 data = g_new0 (struct SetDisplayNameData, 1);
1663 data->button = g_object_ref (button);
1664 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1666 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1667 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1668 gtk_tree_path_free (tree_path);
1670 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1671 "standard::type,standard::icon,standard::display-name",
1672 set_info_get_info_cb, data);
1674 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1675 CANCELLABLE_COLUMN, cancellable,
1679 /* Shortcuts Model */
1681 model_get_type_position (GtkFileChooserButton *button,
1686 if (row_type == ROW_TYPE_SPECIAL)
1689 retval += button->priv->n_special;
1691 if (row_type == ROW_TYPE_VOLUME)
1694 retval += button->priv->n_volumes;
1696 if (row_type == ROW_TYPE_SHORTCUT)
1699 retval += button->priv->n_shortcuts;
1701 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1704 retval += button->priv->has_bookmark_separator;
1706 if (row_type == ROW_TYPE_BOOKMARK)
1709 retval += button->priv->n_bookmarks;
1711 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1714 retval += button->priv->has_current_folder_separator;
1716 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1719 retval += button->priv->has_current_folder;
1721 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1724 retval += button->priv->has_other_separator;
1726 if (row_type == ROW_TYPE_OTHER)
1729 g_assert_not_reached ();
1734 model_free_row_data (GtkFileChooserButton *button,
1739 GCancellable *cancellable;
1741 gtk_tree_model_get (button->priv->model, iter,
1744 CANCELLABLE_COLUMN, &cancellable,
1749 g_cancellable_cancel (cancellable);
1750 g_object_unref (cancellable);
1755 case ROW_TYPE_SPECIAL:
1756 case ROW_TYPE_SHORTCUT:
1757 case ROW_TYPE_BOOKMARK:
1758 case ROW_TYPE_CURRENT_FOLDER:
1759 g_object_unref (data);
1761 case ROW_TYPE_VOLUME:
1762 _gtk_file_system_volume_unref (data);
1770 model_add_special_get_info_cb (GCancellable *cancellable,
1772 const GError *error,
1775 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1779 GCancellable *model_cancellable = NULL;
1780 struct ChangeIconThemeData *data = user_data;
1783 if (!data->button->priv->model)
1784 /* button got destroyed */
1787 path = gtk_tree_row_reference_get_path (data->row_ref);
1789 /* Cancellable doesn't exist anymore in the model */
1792 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1793 gtk_tree_path_free (path);
1795 gtk_tree_model_get (data->button->priv->model, &iter,
1796 CANCELLABLE_COLUMN, &model_cancellable,
1798 if (cancellable != model_cancellable)
1801 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1802 CANCELLABLE_COLUMN, NULL,
1805 if (cancelled || error)
1808 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1812 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1813 ICON_COLUMN, pixbuf,
1815 g_object_unref (pixbuf);
1818 gtk_tree_model_get (data->button->priv->model, &iter,
1819 DISPLAY_NAME_COLUMN, &name,
1822 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1823 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1828 g_object_unref (data->button);
1829 gtk_tree_row_reference_free (data->row_ref);
1832 if (model_cancellable)
1833 g_object_unref (model_cancellable);
1837 model_add_special (GtkFileChooserButton *button)
1839 const gchar *homedir;
1840 const gchar *desktopdir;
1841 GtkListStore *store;
1846 store = GTK_LIST_STORE (button->priv->model);
1847 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1849 homedir = g_get_home_dir ();
1853 GtkTreePath *tree_path;
1854 GCancellable *cancellable;
1855 struct ChangeIconThemeData *info;
1857 file = g_file_new_for_path (homedir);
1858 gtk_list_store_insert (store, &iter, pos);
1861 info = g_new0 (struct ChangeIconThemeData, 1);
1862 info->button = g_object_ref (button);
1863 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1864 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1866 gtk_tree_path_free (tree_path);
1868 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1869 "standard::icon,standard::display-name",
1870 model_add_special_get_info_cb, info);
1872 gtk_list_store_set (store, &iter,
1874 DISPLAY_NAME_COLUMN, NULL,
1875 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1877 IS_FOLDER_COLUMN, TRUE,
1878 CANCELLABLE_COLUMN, cancellable,
1881 button->priv->n_special++;
1884 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1886 /* "To disable a directory, point it to the homedir."
1887 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1889 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1891 GtkTreePath *tree_path;
1892 GCancellable *cancellable;
1893 struct ChangeIconThemeData *info;
1895 file = g_file_new_for_path (desktopdir);
1896 gtk_list_store_insert (store, &iter, pos);
1899 info = g_new0 (struct ChangeIconThemeData, 1);
1900 info->button = g_object_ref (button);
1901 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1902 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1904 gtk_tree_path_free (tree_path);
1906 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1907 "standard::icon,standard::display-name",
1908 model_add_special_get_info_cb, info);
1910 gtk_list_store_set (store, &iter,
1911 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1913 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1915 IS_FOLDER_COLUMN, TRUE,
1916 CANCELLABLE_COLUMN, cancellable,
1919 button->priv->n_special++;
1924 model_add_volumes (GtkFileChooserButton *button,
1927 GtkListStore *store;
1929 gboolean local_only;
1935 store = GTK_LIST_STORE (button->priv->model);
1936 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1937 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1939 for (l = volumes; l; l = l->next)
1941 GtkFileSystemVolume *volume;
1944 gchar *display_name;
1950 if (_gtk_file_system_volume_is_mounted (volume))
1954 base_file = _gtk_file_system_volume_get_root (volume);
1955 if (base_file != NULL)
1957 if (!g_file_is_native (base_file))
1959 g_object_unref (base_file);
1963 g_object_unref (base_file);
1968 pixbuf = _gtk_file_system_volume_render_icon (volume,
1969 GTK_WIDGET (button),
1970 button->priv->icon_size,
1972 display_name = _gtk_file_system_volume_get_display_name (volume);
1974 gtk_list_store_insert (store, &iter, pos);
1975 gtk_list_store_set (store, &iter,
1976 ICON_COLUMN, pixbuf,
1977 DISPLAY_NAME_COLUMN, display_name,
1978 TYPE_COLUMN, ROW_TYPE_VOLUME,
1979 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
1980 IS_FOLDER_COLUMN, TRUE,
1984 g_object_unref (pixbuf);
1985 g_free (display_name);
1987 button->priv->n_volumes++;
1992 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
1995 model_add_bookmarks (GtkFileChooserButton *button,
1998 GtkListStore *store;
2001 gboolean local_only;
2007 store = GTK_LIST_STORE (button->priv->model);
2008 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
2009 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
2011 for (l = bookmarks; l; l = l->next)
2017 if (g_file_is_native (file))
2019 gtk_list_store_insert (store, &iter, pos);
2020 gtk_list_store_set (store, &iter,
2022 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2023 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2024 DATA_COLUMN, g_object_ref (file),
2025 IS_FOLDER_COLUMN, FALSE,
2027 set_info_for_file_at_iter (button, file, &iter);
2032 GtkIconTheme *icon_theme;
2038 /* Don't call get_info for remote paths to avoid latency and
2040 * If we switch to a better bookmarks file format (XBEL), we
2041 * should use mime info to get a better icon.
2043 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2045 label = _gtk_file_chooser_label_for_file (file);
2047 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2048 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2049 button->priv->icon_size, 0, NULL);
2051 gtk_list_store_insert (store, &iter, pos);
2052 gtk_list_store_set (store, &iter,
2053 ICON_COLUMN, pixbuf,
2054 DISPLAY_NAME_COLUMN, label,
2055 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2056 DATA_COLUMN, g_object_ref (file),
2057 IS_FOLDER_COLUMN, TRUE,
2061 g_object_unref (pixbuf);
2064 button->priv->n_bookmarks++;
2068 if (button->priv->n_bookmarks > 0 &&
2069 !button->priv->has_bookmark_separator)
2071 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2073 gtk_list_store_insert (store, &iter, pos);
2074 gtk_list_store_set (store, &iter,
2076 DISPLAY_NAME_COLUMN, NULL,
2077 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2079 IS_FOLDER_COLUMN, FALSE,
2081 button->priv->has_bookmark_separator = TRUE;
2086 model_update_current_folder (GtkFileChooserButton *button,
2089 GtkListStore *store;
2096 store = GTK_LIST_STORE (button->priv->model);
2098 if (!button->priv->has_current_folder_separator)
2100 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_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_CURRENT_FOLDER_SEPARATOR,
2107 IS_FOLDER_COLUMN, FALSE,
2109 button->priv->has_current_folder_separator = TRUE;
2112 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2113 if (!button->priv->has_current_folder)
2115 gtk_list_store_insert (store, &iter, pos);
2116 button->priv->has_current_folder = TRUE;
2120 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2121 model_free_row_data (button, &iter);
2124 if (g_file_is_native (file))
2126 gtk_list_store_set (store, &iter,
2128 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2129 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2130 DATA_COLUMN, g_object_ref (file),
2131 IS_FOLDER_COLUMN, FALSE,
2133 set_info_for_file_at_iter (button, file, &iter);
2138 GtkIconTheme *icon_theme;
2141 /* Don't call get_info for remote paths to avoid latency and
2143 * If we switch to a better bookmarks file format (XBEL), we
2144 * should use mime info to get a better icon.
2146 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2148 label = _gtk_file_chooser_label_for_file (file);
2150 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2152 if (g_file_is_native (file))
2153 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2154 button->priv->icon_size, 0, NULL);
2156 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2157 button->priv->icon_size, 0, NULL);
2159 gtk_list_store_set (store, &iter,
2160 ICON_COLUMN, pixbuf,
2161 DISPLAY_NAME_COLUMN, label,
2162 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2163 DATA_COLUMN, g_object_ref (file),
2164 IS_FOLDER_COLUMN, TRUE,
2168 g_object_unref (pixbuf);
2173 model_add_other (GtkFileChooserButton *button)
2175 GtkListStore *store;
2179 store = GTK_LIST_STORE (button->priv->model);
2180 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2182 gtk_list_store_insert (store, &iter, pos);
2183 gtk_list_store_set (store, &iter,
2185 DISPLAY_NAME_COLUMN, NULL,
2186 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2188 IS_FOLDER_COLUMN, FALSE,
2190 button->priv->has_other_separator = TRUE;
2193 gtk_list_store_insert (store, &iter, pos);
2194 gtk_list_store_set (store, &iter,
2196 DISPLAY_NAME_COLUMN, _("Other…"),
2197 TYPE_COLUMN, ROW_TYPE_OTHER,
2199 IS_FOLDER_COLUMN, FALSE,
2204 model_remove_rows (GtkFileChooserButton *button,
2208 GtkListStore *store;
2213 store = GTK_LIST_STORE (button->priv->model);
2219 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2220 g_assert_not_reached ();
2222 model_free_row_data (button, &iter);
2223 gtk_list_store_remove (store, &iter);
2230 static inline gboolean
2231 test_if_file_is_visible (GtkFileSystem *fs,
2233 gboolean local_only,
2239 if (local_only && !g_file_is_native (file))
2249 filter_model_visible_func (GtkTreeModel *model,
2253 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2254 GtkFileChooserButtonPrivate *priv = button->priv;
2257 gboolean local_only, retval, is_folder;
2259 type = ROW_TYPE_INVALID;
2261 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2263 gtk_tree_model_get (model, iter,
2266 IS_FOLDER_COLUMN, &is_folder,
2271 case ROW_TYPE_CURRENT_FOLDER:
2274 case ROW_TYPE_SPECIAL:
2275 case ROW_TYPE_SHORTCUT:
2276 case ROW_TYPE_BOOKMARK:
2277 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2279 case ROW_TYPE_VOLUME:
2284 if (_gtk_file_system_volume_is_mounted (data))
2288 base_file = _gtk_file_system_volume_get_root (data);
2292 if (!g_file_is_native (base_file))
2294 g_object_unref (base_file);
2312 name_cell_data_func (GtkCellLayout *layout,
2313 GtkCellRenderer *cell,
2314 GtkTreeModel *model,
2321 gtk_tree_model_get (model, iter,
2325 if (type == ROW_TYPE_CURRENT_FOLDER)
2326 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2328 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2332 combo_box_row_separator_func (GtkTreeModel *model,
2336 gchar type = ROW_TYPE_INVALID;
2338 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2340 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2341 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2342 type == ROW_TYPE_OTHER_SEPARATOR);
2346 update_combo_box (GtkFileChooserButton *button)
2348 GtkFileChooserButtonPrivate *priv = button->priv;
2353 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2355 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2364 type = ROW_TYPE_INVALID;
2367 gtk_tree_model_get (priv->filter_model, &iter,
2374 case ROW_TYPE_SPECIAL:
2375 case ROW_TYPE_SHORTCUT:
2376 case ROW_TYPE_BOOKMARK:
2377 case ROW_TYPE_CURRENT_FOLDER:
2378 row_found = (file && g_file_equal (data, file));
2380 case ROW_TYPE_VOLUME:
2384 base_file = _gtk_file_system_volume_get_root (data);
2387 row_found = (file && g_file_equal (base_file, file));
2388 g_object_unref (base_file);
2399 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2400 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2402 g_signal_handler_unblock (priv->combo_box,
2403 priv->combo_box_changed_id);
2406 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2408 /* If it hasn't been found already, update & select the current-folder row. */
2409 if (!row_found && file)
2411 GtkTreeIter filter_iter;
2414 model_update_current_folder (button, file);
2415 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2417 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2418 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2420 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2421 &filter_iter, &iter);
2423 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2424 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2425 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2429 g_object_unref (file);
2434 update_label_get_info_cb (GCancellable *cancellable,
2436 const GError *error,
2439 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2441 GtkFileChooserButton *button = data;
2442 GtkFileChooserButtonPrivate *priv = button->priv;
2444 if (cancellable != priv->update_button_cancellable)
2447 priv->update_button_cancellable = NULL;
2449 if (cancelled || error)
2452 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2454 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2457 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2459 priv->icon_size, 0, NULL);
2461 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2463 g_object_unref (pixbuf);
2466 g_object_unref (button);
2467 g_object_unref (cancellable);
2471 update_label_and_image (GtkFileChooserButton *button)
2473 GtkFileChooserButtonPrivate *priv = button->priv;
2477 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2480 if (priv->update_button_cancellable)
2482 g_cancellable_cancel (priv->update_button_cancellable);
2483 priv->update_button_cancellable = NULL;
2488 GtkFileSystemVolume *volume = NULL;
2490 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2495 base_file = _gtk_file_system_volume_get_root (volume);
2496 if (base_file && g_file_equal (base_file, file))
2500 label_text = _gtk_file_system_volume_get_display_name (volume);
2501 pixbuf = _gtk_file_system_volume_render_icon (volume,
2502 GTK_WIDGET (button),
2505 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2507 g_object_unref (pixbuf);
2511 g_object_unref (base_file);
2513 _gtk_file_system_volume_unref (volume);
2519 if (g_file_is_native (file))
2521 priv->update_button_cancellable =
2522 _gtk_file_system_get_info (priv->fs, file,
2523 "standard::icon,standard::display-name",
2524 update_label_get_info_cb,
2525 g_object_ref (button));
2531 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2532 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2534 priv->icon_size, 0, NULL);
2535 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2537 g_object_unref (pixbuf);
2540 g_object_unref (file);
2546 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2547 g_free (label_text);
2551 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2552 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2557 /* ************************ *
2558 * Child Object Callbacks *
2559 * ************************ */
2563 fs_volumes_changed_cb (GtkFileSystem *fs,
2566 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2567 GtkFileChooserButtonPrivate *priv = button->priv;
2570 model_remove_rows (user_data,
2571 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2574 priv->n_volumes = 0;
2576 volumes = _gtk_file_system_list_volumes (fs);
2577 model_add_volumes (user_data, volumes);
2578 g_slist_free (volumes);
2580 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2582 update_label_and_image (user_data);
2583 update_combo_box (user_data);
2587 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2590 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2591 GtkFileChooserButtonPrivate *priv = button->priv;
2594 bookmarks = _gtk_file_system_list_bookmarks (fs);
2595 model_remove_rows (user_data,
2596 model_get_type_position (user_data,
2597 ROW_TYPE_BOOKMARK_SEPARATOR),
2598 (priv->n_bookmarks + priv->has_bookmark_separator));
2599 priv->has_bookmark_separator = FALSE;
2600 priv->n_bookmarks = 0;
2601 model_add_bookmarks (user_data, bookmarks);
2602 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2603 g_slist_free (bookmarks);
2605 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2607 update_label_and_image (user_data);
2608 update_combo_box (user_data);
2612 save_inactive_state (GtkFileChooserButton *button)
2614 GtkFileChooserButtonPrivate *priv = button->priv;
2616 if (priv->current_folder_while_inactive)
2617 g_object_unref (priv->current_folder_while_inactive);
2619 if (priv->selection_while_inactive)
2620 g_object_unref (priv->selection_while_inactive);
2622 priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->dialog));
2623 priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2627 restore_inactive_state (GtkFileChooserButton *button)
2629 GtkFileChooserButtonPrivate *priv = button->priv;
2631 if (priv->current_folder_while_inactive)
2632 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), priv->current_folder_while_inactive, NULL);
2634 if (priv->selection_while_inactive)
2635 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2637 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2642 open_dialog (GtkFileChooserButton *button)
2644 GtkFileChooserButtonPrivate *priv = button->priv;
2646 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2648 if (!gtk_widget_get_visible (priv->dialog))
2650 GtkWidget *toplevel;
2652 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2654 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2656 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2657 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2658 GTK_WINDOW (toplevel));
2660 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2661 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2667 g_signal_handler_block (priv->dialog,
2668 priv->dialog_folder_changed_id);
2669 g_signal_handler_block (priv->dialog,
2670 priv->dialog_file_activated_id);
2671 g_signal_handler_block (priv->dialog,
2672 priv->dialog_selection_changed_id);
2674 restore_inactive_state (button);
2676 priv->active = TRUE;
2679 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2680 gtk_window_present (GTK_WINDOW (priv->dialog));
2685 combo_box_changed_cb (GtkComboBox *combo_box,
2690 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2692 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2693 GtkFileChooserButtonPrivate *priv = button->priv;
2697 type = ROW_TYPE_INVALID;
2700 gtk_tree_model_get (priv->filter_model, &iter,
2707 case ROW_TYPE_SPECIAL:
2708 case ROW_TYPE_SHORTCUT:
2709 case ROW_TYPE_BOOKMARK:
2710 case ROW_TYPE_CURRENT_FOLDER:
2711 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2713 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2716 case ROW_TYPE_VOLUME:
2720 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2721 base_file = _gtk_file_system_volume_get_root (data);
2724 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2726 g_object_unref (base_file);
2730 case ROW_TYPE_OTHER:
2731 open_dialog (user_data);
2741 button_clicked_cb (GtkButton *real_button,
2744 open_dialog (user_data);
2749 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2752 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2754 g_signal_emit_by_name (button, "current-folder-changed");
2758 dialog_file_activated_cb (GtkFileChooser *dialog,
2761 g_signal_emit_by_name (user_data, "file-activated");
2765 dialog_selection_changed_cb (GtkFileChooser *dialog,
2768 update_label_and_image (user_data);
2769 update_combo_box (user_data);
2770 g_signal_emit_by_name (user_data, "selection-changed");
2774 dialog_update_preview_cb (GtkFileChooser *dialog,
2777 g_signal_emit_by_name (user_data, "update-preview");
2781 dialog_notify_cb (GObject *dialog,
2787 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2788 GTK_TYPE_FILE_CHOOSER);
2789 if (g_object_interface_find_property (iface, pspec->name))
2790 g_object_notify (user_data, pspec->name);
2792 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2794 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2795 GtkFileChooserButtonPrivate *priv = button->priv;
2797 if (priv->has_current_folder)
2803 pos = model_get_type_position (user_data,
2804 ROW_TYPE_CURRENT_FOLDER);
2805 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2808 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2810 /* If the path isn't local but we're in local-only mode now, remove
2811 * the custom-folder row */
2812 if (data && g_file_is_native (G_FILE (data)) &&
2813 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2816 model_remove_rows (user_data, pos, 2);
2820 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2821 update_combo_box (user_data);
2826 dialog_delete_event_cb (GtkWidget *dialog,
2830 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2836 dialog_response_cb (GtkDialog *dialog,
2840 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2841 GtkFileChooserButtonPrivate *priv = button->priv;
2843 if (response == GTK_RESPONSE_ACCEPT ||
2844 response == GTK_RESPONSE_OK)
2846 save_inactive_state (button);
2848 g_signal_emit_by_name (button, "current-folder-changed");
2849 g_signal_emit_by_name (button, "selection-changed");
2851 update_label_and_image (button);
2852 update_combo_box (button);
2856 restore_inactive_state (button);
2861 g_signal_handler_unblock (priv->dialog,
2862 priv->dialog_folder_changed_id);
2863 g_signal_handler_unblock (priv->dialog,
2864 priv->dialog_file_activated_id);
2865 g_signal_handler_unblock (priv->dialog,
2866 priv->dialog_selection_changed_id);
2867 priv->active = FALSE;
2870 gtk_widget_set_sensitive (priv->combo_box, TRUE);
2871 gtk_widget_hide (priv->dialog);
2873 if (response == GTK_RESPONSE_ACCEPT ||
2874 response == GTK_RESPONSE_OK)
2875 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
2879 /* ************************************************************************** *
2881 * ************************************************************************** */
2884 * gtk_file_chooser_button_new:
2885 * @title: the title of the browse dialog.
2886 * @action: the open mode for the widget.
2888 * Creates a new file-selecting button widget.
2890 * Returns: a new button widget.
2895 gtk_file_chooser_button_new (const gchar *title,
2896 GtkFileChooserAction action)
2898 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2899 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2901 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2903 "title", (title ? title : _(DEFAULT_TITLE)),
2908 * gtk_file_chooser_button_new_with_dialog:
2909 * @dialog: the widget to use as dialog
2911 * Creates a #GtkFileChooserButton widget which uses @dialog as its
2912 * file-picking window.
2914 * Note that @dialog must be a #GtkDialog (or subclass) which
2915 * implements the #GtkFileChooser interface and must not have
2916 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2918 * Also note that the dialog needs to have its confirmative button
2919 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2920 * order for the button to take over the file selected in the dialog.
2922 * Returns: a new button widget.
2927 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2929 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2931 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2937 * gtk_file_chooser_button_set_title:
2938 * @button: the button widget to modify.
2939 * @title: the new browse dialog title.
2941 * Modifies the @title of the browse dialog used by @button.
2946 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2949 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2951 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2952 g_object_notify (G_OBJECT (button), "title");
2956 * gtk_file_chooser_button_get_title:
2957 * @button: the button widget to examine.
2959 * Retrieves the title of the browse dialog used by @button. The returned value
2960 * should not be modified or freed.
2962 * Returns: a pointer to the browse dialog's title.
2967 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2969 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2971 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2975 * gtk_file_chooser_button_get_width_chars:
2976 * @button: the button widget to examine.
2978 * Retrieves the width in characters of the @button widget's entry and/or label.
2980 * Returns: an integer width (in characters) that the button will use to size itself.
2985 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2987 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2989 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2993 * gtk_file_chooser_button_set_width_chars:
2994 * @button: the button widget to examine.
2995 * @n_chars: the new width, in characters.
2997 * Sets the width (in characters) that @button will use to @n_chars.
3002 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
3005 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3007 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
3008 g_object_notify (G_OBJECT (button), "width-chars");
3012 * gtk_file_chooser_button_set_focus_on_click:
3013 * @button: a #GtkFileChooserButton
3014 * @focus_on_click: whether the button grabs focus when clicked with the mouse
3016 * Sets whether the button will grab focus when it is clicked with the mouse.
3017 * Making mouse clicks not grab focus is useful in places like toolbars where
3018 * you don't want the keyboard focus removed from the main area of the
3024 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
3025 gboolean focus_on_click)
3027 GtkFileChooserButtonPrivate *priv;
3029 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3031 priv = button->priv;
3033 focus_on_click = focus_on_click != FALSE;
3035 if (priv->focus_on_click != focus_on_click)
3037 priv->focus_on_click = focus_on_click;
3038 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
3039 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
3041 g_object_notify (G_OBJECT (button), "focus-on-click");
3046 * gtk_file_chooser_button_get_focus_on_click:
3047 * @button: a #GtkFileChooserButton
3049 * Returns whether the button grabs focus when it is clicked with the mouse.
3050 * See gtk_file_chooser_button_set_focus_on_click().
3052 * Return value: %TRUE if the button grabs focus when it is clicked with
3058 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
3060 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
3062 return button->priv->focus_on_click;