1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */
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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 #include <sys/types.h>
34 #include "gtkbutton.h"
35 #include "gtkcelllayout.h"
36 #include "gtkcellrenderertext.h"
37 #include "gtkcellrendererpixbuf.h"
38 #include "gtkcombobox.h"
40 #include "gtkicontheme.h"
41 #include "gtkiconfactory.h"
44 #include "gtkliststore.h"
46 #include "gtktreemodelfilter.h"
47 #include "gtkvseparator.h"
48 #include "gtkfilechooserdialog.h"
49 #include "gtkfilechooserprivate.h"
50 #include "gtkfilechooserutils.h"
51 #include "gtkmarshalers.h"
53 #include "gtkfilechooserbutton.h"
55 #include "gtktypebuiltins.h"
56 #include "gtkprivate.h"
60 * SECTION:gtkfilechooserbutton
61 * @Short_description: A button to launch a file selection dialog
62 * @Title: GtkFileChooserButton
63 * @See_also:#GtkFileChooserDialog
65 * The #GtkFileChooserButton is a widget that lets the user select a
66 * file. It implements the #GtkFileChooser interface. Visually, it is a
67 * file name with a button to bring up a #GtkFileChooserDialog.
68 * The user can then use that dialog to change the file associated with
69 * that button. This widget does not support setting the
70 * #GtkFileChooser:select-multiple property to %TRUE.
73 * <title>Create a button to let the user select a file in /etc</title>
78 * button = gtk_file_chooser_button_new (_("Select a file"),
79 * GTK_FILE_CHOOSER_ACTION_OPEN);
80 * gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
86 * The #GtkFileChooserButton supports the #GtkFileChooserAction<!-- -->s
87 * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
90 * The #GtkFileChooserButton will ellipsize the label,
91 * and thus will thus request little horizontal space. To give the button
92 * more space, you should call gtk_widget_get_preferred_size(),
93 * gtk_file_chooser_button_set_width_chars(), or pack the button in
94 * such a way that other interface elements give space to the widget.
101 * **************** */
103 #define DEFAULT_TITLE N_("Select a File")
104 #define DESKTOP_DISPLAY_NAME N_("Desktop")
105 #define FALLBACK_DISPLAY_NAME N_("(None)")
106 #define FALLBACK_ICON_NAME "stock_unknown"
107 #define FALLBACK_ICON_SIZE 16
110 /* ********************** *
111 * Private Enumerations *
112 * ********************** */
132 /* TreeModel Columns */
144 /* TreeModel Row Types */
150 ROW_TYPE_BOOKMARK_SEPARATOR,
152 ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
153 ROW_TYPE_CURRENT_FOLDER,
154 ROW_TYPE_OTHER_SEPARATOR,
157 ROW_TYPE_INVALID = -1
162 /* ******************** *
163 * Private Structures *
164 * ******************** */
166 struct _GtkFileChooserButtonPrivate
172 GtkWidget *combo_box;
173 GtkCellRenderer *icon_cell;
174 GtkCellRenderer *name_cell;
177 GtkTreeModel *filter_model;
182 gulong combo_box_changed_id;
183 gulong dialog_file_activated_id;
184 gulong dialog_folder_changed_id;
185 gulong dialog_selection_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 /* Used to track whether we need to set a default current folder on ::map() */
208 guint folder_has_been_set : 1;
210 guint focus_on_click : 1;
225 /* ********************* *
226 * Function Prototypes *
227 * ********************* */
229 /* GtkFileChooserIface Functions */
230 static void gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
231 static gboolean gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
234 static gboolean gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
238 /* GObject Functions */
239 static GObject *gtk_file_chooser_button_constructor (GType type,
241 GObjectConstructParam *params);
242 static void gtk_file_chooser_button_set_property (GObject *object,
246 static void gtk_file_chooser_button_get_property (GObject *object,
250 static void gtk_file_chooser_button_finalize (GObject *object);
252 /* GtkWidget Functions */
253 static void gtk_file_chooser_button_destroy (GtkWidget *widget);
254 static void gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
255 GdkDragContext *context,
258 GtkSelectionData *data,
261 static void gtk_file_chooser_button_show_all (GtkWidget *widget);
262 static void gtk_file_chooser_button_show (GtkWidget *widget);
263 static void gtk_file_chooser_button_hide (GtkWidget *widget);
264 static void gtk_file_chooser_button_map (GtkWidget *widget);
265 static gboolean gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
266 gboolean group_cycling);
267 static void gtk_file_chooser_button_style_updated (GtkWidget *widget);
268 static void gtk_file_chooser_button_screen_changed (GtkWidget *widget,
269 GdkScreen *old_screen);
271 /* Utility Functions */
272 static GtkIconTheme *get_icon_theme (GtkWidget *widget);
273 static void set_info_for_file_at_iter (GtkFileChooserButton *fs,
277 static gint model_get_type_position (GtkFileChooserButton *button,
279 static void model_free_row_data (GtkFileChooserButton *button,
281 static inline void model_add_special (GtkFileChooserButton *button);
282 static inline void model_add_other (GtkFileChooserButton *button);
283 static void model_add_volumes (GtkFileChooserButton *button,
285 static void model_add_bookmarks (GtkFileChooserButton *button,
287 static void model_update_current_folder (GtkFileChooserButton *button,
289 static void model_remove_rows (GtkFileChooserButton *button,
293 static gboolean filter_model_visible_func (GtkTreeModel *model,
297 static gboolean combo_box_row_separator_func (GtkTreeModel *model,
300 static void name_cell_data_func (GtkCellLayout *layout,
301 GtkCellRenderer *cell,
305 static void open_dialog (GtkFileChooserButton *button);
306 static void update_combo_box (GtkFileChooserButton *button);
307 static void update_label_and_image (GtkFileChooserButton *button);
309 /* Child Object Callbacks */
310 static void fs_volumes_changed_cb (GtkFileSystem *fs,
312 static void fs_bookmarks_changed_cb (GtkFileSystem *fs,
315 static void combo_box_changed_cb (GtkComboBox *combo_box,
318 static void button_clicked_cb (GtkButton *real_button,
321 static void dialog_update_preview_cb (GtkFileChooser *dialog,
323 static void dialog_selection_changed_cb (GtkFileChooser *dialog,
325 static void dialog_file_activated_cb (GtkFileChooser *dialog,
327 static void dialog_current_folder_changed_cb (GtkFileChooser *dialog,
329 static void dialog_notify_cb (GObject *dialog,
332 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
335 static void dialog_response_cb (GtkDialog *dialog,
339 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
341 /* ******************* *
342 * GType Declaration *
343 * ******************* */
345 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_HBOX, { \
346 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
350 /* ***************** *
352 * ***************** */
355 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
357 GObjectClass *gobject_class;
358 GtkWidgetClass *widget_class;
360 gobject_class = G_OBJECT_CLASS (class);
361 widget_class = GTK_WIDGET_CLASS (class);
363 gobject_class->constructor = gtk_file_chooser_button_constructor;
364 gobject_class->set_property = gtk_file_chooser_button_set_property;
365 gobject_class->get_property = gtk_file_chooser_button_get_property;
366 gobject_class->finalize = gtk_file_chooser_button_finalize;
368 widget_class->destroy = gtk_file_chooser_button_destroy;
369 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
370 widget_class->show_all = gtk_file_chooser_button_show_all;
371 widget_class->show = gtk_file_chooser_button_show;
372 widget_class->hide = gtk_file_chooser_button_hide;
373 widget_class->map = gtk_file_chooser_button_map;
374 widget_class->style_updated = gtk_file_chooser_button_style_updated;
375 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
376 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
379 * GtkFileChooserButton::file-set:
380 * @widget: the object which received the signal.
382 * The ::file-set signal is emitted when the user selects a file.
384 * Note that this signal is only emitted when the <emphasis>user</emphasis>
389 file_chooser_button_signals[FILE_SET] =
390 g_signal_new (I_("file-set"),
391 G_TYPE_FROM_CLASS (gobject_class),
393 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
395 _gtk_marshal_VOID__VOID,
399 * GtkFileChooserButton:dialog:
401 * Instance of the #GtkFileChooserDialog associated with the button.
405 g_object_class_install_property (gobject_class, PROP_DIALOG,
406 g_param_spec_object ("dialog",
408 P_("The file chooser dialog to use."),
409 GTK_TYPE_FILE_CHOOSER,
410 (GTK_PARAM_WRITABLE |
411 G_PARAM_CONSTRUCT_ONLY)));
414 * GtkFileChooserButton:focus-on-click:
416 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
421 g_object_class_install_property (gobject_class,
423 g_param_spec_boolean ("focus-on-click",
424 P_("Focus on click"),
425 P_("Whether the button grabs focus when it is clicked with the mouse"),
427 GTK_PARAM_READWRITE));
430 * GtkFileChooserButton:title:
432 * Title to put on the #GtkFileChooserDialog associated with the button.
436 g_object_class_install_property (gobject_class, PROP_TITLE,
437 g_param_spec_string ("title",
439 P_("The title of the file chooser dialog."),
441 GTK_PARAM_READWRITE));
444 * GtkFileChooserButton:width-chars:
446 * The width of the entry and label inside the button, in characters.
450 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
451 g_param_spec_int ("width-chars",
452 P_("Width In Characters"),
453 P_("The desired width of the button widget, in characters."),
455 GTK_PARAM_READWRITE));
457 _gtk_file_chooser_install_properties (gobject_class);
459 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
463 gtk_file_chooser_button_init (GtkFileChooserButton *button)
465 GtkFileChooserButtonPrivate *priv;
466 GtkWidget *box, *image, *sep;
467 GtkTargetList *target_list;
469 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
470 GTK_TYPE_FILE_CHOOSER_BUTTON,
471 GtkFileChooserButtonPrivate);
473 priv->icon_size = FALLBACK_ICON_SIZE;
474 priv->focus_on_click = TRUE;
476 gtk_widget_push_composite_child ();
479 priv->button = gtk_button_new ();
480 g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb),
482 gtk_container_add (GTK_CONTAINER (button), priv->button);
483 gtk_widget_show (priv->button);
485 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
486 gtk_container_add (GTK_CONTAINER (priv->button), box);
487 gtk_widget_show (box);
489 priv->image = gtk_image_new ();
490 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
491 gtk_widget_show (priv->image);
493 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
494 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
495 gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
496 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
497 //gtk_container_add (GTK_CONTAINER (box), priv->label);
498 gtk_widget_show (priv->label);
500 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
501 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
502 gtk_widget_show (sep);
504 image = gtk_image_new_from_stock (GTK_STOCK_OPEN,
506 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
507 gtk_widget_show (image);
510 /* Keep in sync with columns enum, line 88 */
512 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
513 GDK_TYPE_PIXBUF, /* Icon */
514 G_TYPE_STRING, /* Display Name */
515 G_TYPE_CHAR, /* Row Type */
516 G_TYPE_POINTER /* Volume || Path */,
517 G_TYPE_BOOLEAN /* Is Folder? */,
518 G_TYPE_POINTER /* cancellable */));
520 priv->combo_box = gtk_combo_box_new ();
521 priv->combo_box_changed_id =
522 g_signal_connect (priv->combo_box, "changed",
523 G_CALLBACK (combo_box_changed_cb), button);
524 gtk_container_add (GTK_CONTAINER (button), priv->combo_box);
526 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
527 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
528 priv->icon_cell, FALSE);
529 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
530 priv->icon_cell, "pixbuf", ICON_COLUMN);
532 priv->name_cell = gtk_cell_renderer_text_new ();
533 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
534 priv->name_cell, TRUE);
535 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
536 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
537 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
538 priv->name_cell, name_cell_data_func,
541 gtk_widget_pop_composite_child ();
544 gtk_drag_dest_set (GTK_WIDGET (button),
545 (GTK_DEST_DEFAULT_ALL),
548 target_list = gtk_target_list_new (NULL, 0);
549 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
550 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
551 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
552 gtk_target_list_unref (target_list);
556 /* ******************************* *
557 * GtkFileChooserIface Functions *
558 * ******************************* */
560 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
562 _gtk_file_chooser_delegate_iface_init (iface);
564 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
565 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
569 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
573 GtkFileChooser *delegate;
576 delegate = g_object_get_qdata (G_OBJECT (chooser),
577 GTK_FILE_CHOOSER_DELEGATE_QUARK);
578 retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
582 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
583 GtkFileChooserButtonPrivate *priv = button->priv;
587 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
588 pos += priv->n_shortcuts;
590 gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
591 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
593 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
594 TYPE_COLUMN, ROW_TYPE_SHORTCUT,
595 DATA_COLUMN, g_object_ref (file),
596 IS_FOLDER_COLUMN, FALSE,
598 set_info_for_file_at_iter (button, file, &iter);
601 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
608 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
612 GtkFileChooser *delegate;
615 delegate = g_object_get_qdata (G_OBJECT (chooser),
616 GTK_FILE_CHOOSER_DELEGATE_QUARK);
618 retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
622 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
623 GtkFileChooserButtonPrivate *priv = button->priv;
628 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
629 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
635 gtk_tree_model_get (priv->model, &iter,
640 if (type == ROW_TYPE_SHORTCUT &&
641 data && g_file_equal (data, file))
643 model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
644 gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
646 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
647 update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
651 while (type == ROW_TYPE_SHORTCUT &&
652 gtk_tree_model_iter_next (priv->model, &iter));
659 /* ******************* *
660 * GObject Functions *
661 * ******************* */
664 gtk_file_chooser_button_constructor (GType type,
666 GObjectConstructParam *params)
669 GtkFileChooserButton *button;
670 GtkFileChooserButtonPrivate *priv;
672 char *current_folder;
674 object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
677 button = GTK_FILE_CHOOSER_BUTTON (object);
682 priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
683 GTK_FILE_CHOOSER_ACTION_OPEN,
690 gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
691 GTK_RESPONSE_ACCEPT);
692 gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
697 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
699 else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
701 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
704 current_folder = gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (priv->dialog));
705 if (current_folder != NULL)
707 priv->folder_has_been_set = TRUE;
708 g_free (current_folder);
711 g_signal_connect (priv->dialog, "delete-event",
712 G_CALLBACK (dialog_delete_event_cb), object);
713 g_signal_connect (priv->dialog, "response",
714 G_CALLBACK (dialog_response_cb), object);
716 /* This is used, instead of the standard delegate, to ensure that signals are only
717 * delegated when the OK button is pressed. */
718 g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
719 priv->dialog_folder_changed_id =
720 g_signal_connect (priv->dialog, "current-folder-changed",
721 G_CALLBACK (dialog_current_folder_changed_cb), object);
722 priv->dialog_file_activated_id =
723 g_signal_connect (priv->dialog, "file-activated",
724 G_CALLBACK (dialog_file_activated_cb), object);
725 priv->dialog_selection_changed_id =
726 g_signal_connect (priv->dialog, "selection-changed",
727 G_CALLBACK (dialog_selection_changed_cb), object);
728 g_signal_connect (priv->dialog, "update-preview",
729 G_CALLBACK (dialog_update_preview_cb), object);
730 g_signal_connect (priv->dialog, "notify",
731 G_CALLBACK (dialog_notify_cb), object);
732 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
733 (gpointer) (&priv->dialog));
736 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
738 model_add_special (button);
740 list = _gtk_file_system_list_volumes (priv->fs);
741 model_add_volumes (button, list);
744 list = _gtk_file_system_list_bookmarks (priv->fs);
745 model_add_bookmarks (button, list);
746 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
749 model_add_other (button);
751 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
752 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
753 filter_model_visible_func,
756 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
757 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
758 combo_box_row_separator_func,
761 /* set up the action for a user-provided dialog, this also updates
762 * the label, image and combobox
764 g_object_set (object,
765 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
768 priv->fs_volumes_changed_id =
769 g_signal_connect (priv->fs, "volumes-changed",
770 G_CALLBACK (fs_volumes_changed_cb), object);
771 priv->fs_bookmarks_changed_id =
772 g_signal_connect (priv->fs, "bookmarks-changed",
773 G_CALLBACK (fs_bookmarks_changed_cb), object);
779 gtk_file_chooser_button_set_property (GObject *object,
784 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
785 GtkFileChooserButtonPrivate *priv = button->priv;
791 priv->dialog = g_value_get_object (value);
793 case PROP_FOCUS_ON_CLICK:
794 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
796 case PROP_WIDTH_CHARS:
797 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
798 g_value_get_int (value));
800 case GTK_FILE_CHOOSER_PROP_ACTION:
801 switch (g_value_get_enum (value))
803 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
804 case GTK_FILE_CHOOSER_ACTION_SAVE:
809 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
810 eval = g_enum_get_value (eclass, g_value_get_enum (value));
811 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
812 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
814 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
819 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
820 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
821 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
823 switch (g_value_get_enum (value))
825 case GTK_FILE_CHOOSER_ACTION_OPEN:
826 gtk_widget_hide (priv->combo_box);
827 gtk_widget_show (priv->button);
829 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
830 gtk_widget_hide (priv->button);
831 gtk_widget_show (priv->combo_box);
834 g_assert_not_reached ();
840 case GTK_FILE_CHOOSER_PROP_FILTER:
841 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
842 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
843 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
844 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
845 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
846 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
847 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
848 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
851 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
852 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
853 fs_volumes_changed_cb (priv->fs, button);
854 fs_bookmarks_changed_cb (priv->fs, button);
857 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
858 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
859 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
862 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
868 gtk_file_chooser_button_get_property (GObject *object,
873 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
874 GtkFileChooserButtonPrivate *priv = button->priv;
878 case PROP_WIDTH_CHARS:
879 g_value_set_int (value,
880 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
882 case PROP_FOCUS_ON_CLICK:
883 g_value_set_boolean (value,
884 gtk_file_chooser_button_get_focus_on_click (button));
888 case GTK_FILE_CHOOSER_PROP_ACTION:
889 case GTK_FILE_CHOOSER_PROP_FILTER:
890 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
891 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
892 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
893 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
894 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
895 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
896 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
897 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
898 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
899 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
903 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
909 gtk_file_chooser_button_finalize (GObject *object)
911 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
912 GtkFileChooserButtonPrivate *priv = button->priv;
915 g_object_unref (priv->old_file);
917 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
920 /* ********************* *
921 * GtkWidget Functions *
922 * ********************* */
925 gtk_file_chooser_button_destroy (GtkWidget *widget)
927 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
928 GtkFileChooserButtonPrivate *priv = button->priv;
932 if (priv->dialog != NULL)
934 gtk_widget_destroy (priv->dialog);
938 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
940 model_free_row_data (button, &iter);
942 while (gtk_tree_model_iter_next (priv->model, &iter));
944 if (priv->dnd_select_folder_cancellable)
946 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
947 priv->dnd_select_folder_cancellable = NULL;
950 if (priv->update_button_cancellable)
952 g_cancellable_cancel (priv->update_button_cancellable);
953 priv->update_button_cancellable = NULL;
956 if (priv->change_icon_theme_cancellables)
958 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
960 GCancellable *cancellable = G_CANCELLABLE (l->data);
961 g_cancellable_cancel (cancellable);
963 g_slist_free (priv->change_icon_theme_cancellables);
964 priv->change_icon_theme_cancellables = NULL;
969 g_object_unref (priv->model);
973 if (priv->filter_model)
975 g_object_unref (priv->filter_model);
976 priv->filter_model = NULL;
981 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
982 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
983 g_object_unref (priv->fs);
987 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
990 struct DndSelectFolderData
992 GtkFileSystem *file_system;
993 GtkFileChooserButton *button;
994 GtkFileChooserAction action;
1002 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1004 const GError *error,
1007 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1008 struct DndSelectFolderData *data = user_data;
1010 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1012 g_object_unref (data->button);
1013 g_object_unref (data->file);
1014 g_strfreev (data->uris);
1017 g_object_unref (cancellable);
1021 data->button->priv->dnd_select_folder_cancellable = NULL;
1023 if (!cancelled && !error && info != NULL)
1027 is_folder = _gtk_file_info_consider_as_directory (info);
1030 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1031 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1032 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1036 data->selected = FALSE;
1038 if (data->selected || data->uris[++data->i] == NULL)
1040 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1042 g_object_unref (data->button);
1043 g_object_unref (data->file);
1044 g_strfreev (data->uris);
1047 g_object_unref (cancellable);
1052 g_object_unref (data->file);
1054 data->file = g_file_new_for_uri (data->uris[data->i]);
1056 data->button->priv->dnd_select_folder_cancellable =
1057 _gtk_file_system_get_info (data->file_system, data->file,
1059 dnd_select_folder_get_info_cb, user_data);
1061 g_object_unref (cancellable);
1065 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1066 GdkDragContext *context,
1069 GtkSelectionData *data,
1073 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1074 GtkFileChooserButtonPrivate *priv = button->priv;
1078 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1079 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1085 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1093 struct DndSelectFolderData *info;
1095 uris = gtk_selection_data_get_uris (data);
1100 info = g_new0 (struct DndSelectFolderData, 1);
1101 info->button = g_object_ref (button);
1104 info->selected = FALSE;
1105 info->file_system = priv->fs;
1106 g_object_get (priv->dialog, "action", &info->action, NULL);
1108 info->file = g_file_new_for_uri (info->uris[info->i]);
1110 if (priv->dnd_select_folder_cancellable)
1111 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1113 priv->dnd_select_folder_cancellable =
1114 _gtk_file_system_get_info (priv->fs, info->file,
1116 dnd_select_folder_get_info_cb, info);
1121 text = (char*) gtk_selection_data_get_text (data);
1122 file = g_file_new_for_uri (text);
1123 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1125 g_object_unref (file);
1127 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1134 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1138 gtk_file_chooser_button_show_all (GtkWidget *widget)
1140 gtk_widget_show (widget);
1144 gtk_file_chooser_button_show (GtkWidget *widget)
1146 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1147 GtkFileChooserButtonPrivate *priv = button->priv;
1149 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1150 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1153 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1157 gtk_file_chooser_button_hide (GtkWidget *widget)
1159 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1160 GtkFileChooserButtonPrivate *priv = button->priv;
1162 gtk_widget_hide (priv->dialog);
1164 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1165 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1169 gtk_file_chooser_button_map (GtkWidget *widget)
1171 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1172 GtkFileChooserButtonPrivate *priv = button->priv;
1174 if (!priv->folder_has_been_set)
1176 char *current_working_dir;
1178 current_working_dir = g_get_current_dir ();
1179 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), current_working_dir);
1180 g_free (current_working_dir);
1182 priv->folder_has_been_set = TRUE;
1185 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1189 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1190 gboolean group_cycling)
1192 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1193 GtkFileChooserButtonPrivate *priv = button->priv;
1195 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1197 case GTK_FILE_CHOOSER_ACTION_OPEN:
1198 gtk_widget_grab_focus (priv->button);
1200 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1201 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1204 g_assert_not_reached ();
1211 /* Changes the icons wherever it is needed */
1212 struct ChangeIconThemeData
1214 GtkFileChooserButton *button;
1215 GtkTreeRowReference *row_ref;
1219 change_icon_theme_get_info_cb (GCancellable *cancellable,
1221 const GError *error,
1224 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1226 struct ChangeIconThemeData *data = user_data;
1228 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1231 data->button->priv->change_icon_theme_cancellables =
1232 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1234 if (cancelled || error)
1237 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1245 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1247 path = gtk_tree_row_reference_get_path (data->row_ref);
1250 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1251 gtk_tree_path_free (path);
1253 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1254 ICON_COLUMN, pixbuf,
1257 g_object_set (data->button->priv->icon_cell,
1261 g_object_unref (pixbuf);
1265 g_object_unref (data->button);
1266 gtk_tree_row_reference_free (data->row_ref);
1269 g_object_unref (cancellable);
1273 change_icon_theme (GtkFileChooserButton *button)
1275 GtkFileChooserButtonPrivate *priv = button->priv;
1276 GtkSettings *settings;
1277 GtkIconTheme *theme;
1280 gint width = 0, height = 0;
1282 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1284 GCancellable *cancellable = G_CANCELLABLE (l->data);
1285 g_cancellable_cancel (cancellable);
1287 g_slist_free (button->priv->change_icon_theme_cancellables);
1288 button->priv->change_icon_theme_cancellables = NULL;
1290 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1292 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1294 priv->icon_size = MAX (width, height);
1296 priv->icon_size = FALLBACK_ICON_SIZE;
1298 update_label_and_image (button);
1300 gtk_tree_model_get_iter_first (priv->model, &iter);
1302 theme = get_icon_theme (GTK_WIDGET (button));
1310 type = ROW_TYPE_INVALID;
1311 gtk_tree_model_get (priv->model, &iter,
1318 case ROW_TYPE_SPECIAL:
1319 case ROW_TYPE_SHORTCUT:
1320 case ROW_TYPE_BOOKMARK:
1321 case ROW_TYPE_CURRENT_FOLDER:
1324 if (g_file_is_native (G_FILE (data)))
1327 GCancellable *cancellable;
1328 struct ChangeIconThemeData *info;
1330 info = g_new0 (struct ChangeIconThemeData, 1);
1331 info->button = g_object_ref (button);
1332 path = gtk_tree_model_get_path (priv->model, &iter);
1333 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1334 gtk_tree_path_free (path);
1337 _gtk_file_system_get_info (priv->fs, data,
1339 change_icon_theme_get_info_cb,
1341 button->priv->change_icon_theme_cancellables =
1342 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1346 /* Don't call get_info for remote paths to avoid latency and
1348 * If we switch to a better bookmarks file format (XBEL), we
1349 * should use mime info to get a better icon.
1351 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1352 priv->icon_size, 0, NULL);
1355 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1356 priv->icon_size, 0, NULL);
1358 case ROW_TYPE_VOLUME:
1360 pixbuf = _gtk_file_system_volume_render_icon (data,
1361 GTK_WIDGET (button),
1365 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1366 priv->icon_size, 0, NULL);
1374 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1376 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1377 ICON_COLUMN, pixbuf,
1381 g_object_unref (pixbuf);
1383 while (gtk_tree_model_iter_next (priv->model, &iter));
1385 g_object_set (button->priv->icon_cell,
1391 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1393 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1395 if (gtk_widget_has_screen (widget))
1396 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1400 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1401 GdkScreen *old_screen)
1403 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1404 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1407 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1411 /* ******************* *
1412 * Utility Functions *
1413 * ******************* */
1416 static GtkIconTheme *
1417 get_icon_theme (GtkWidget *widget)
1419 if (gtk_widget_has_screen (widget))
1420 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1422 return gtk_icon_theme_get_default ();
1426 struct SetDisplayNameData
1428 GtkFileChooserButton *button;
1430 GtkTreeRowReference *row_ref;
1434 set_info_get_info_cb (GCancellable *cancellable,
1436 const GError *error,
1437 gpointer callback_data)
1439 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1443 GCancellable *model_cancellable = NULL;
1444 struct SetDisplayNameData *data = callback_data;
1447 if (!data->button->priv->model)
1448 /* button got destroyed */
1451 path = gtk_tree_row_reference_get_path (data->row_ref);
1453 /* Cancellable doesn't exist anymore in the model */
1456 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1457 gtk_tree_path_free (path);
1459 /* Validate the cancellable */
1460 gtk_tree_model_get (data->button->priv->model, &iter,
1461 CANCELLABLE_COLUMN, &model_cancellable,
1463 if (cancellable != model_cancellable)
1466 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1467 CANCELLABLE_COLUMN, NULL,
1470 if (cancelled || error)
1471 /* There was an error, leave the fallback name in there */
1474 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1477 data->label = g_strdup (g_file_info_get_display_name (info));
1479 is_folder = _gtk_file_info_consider_as_directory (info);
1481 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1482 ICON_COLUMN, pixbuf,
1483 DISPLAY_NAME_COLUMN, data->label,
1484 IS_FOLDER_COLUMN, is_folder,
1488 g_object_unref (pixbuf);
1491 g_object_unref (data->button);
1492 g_free (data->label);
1493 gtk_tree_row_reference_free (data->row_ref);
1496 if (model_cancellable)
1497 g_object_unref (model_cancellable);
1501 set_info_for_file_at_iter (GtkFileChooserButton *button,
1505 struct SetDisplayNameData *data;
1506 GtkTreePath *tree_path;
1507 GCancellable *cancellable;
1509 data = g_new0 (struct SetDisplayNameData, 1);
1510 data->button = g_object_ref (button);
1511 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1513 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1514 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1515 gtk_tree_path_free (tree_path);
1517 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1518 "standard::type,standard::icon,standard::display-name",
1519 set_info_get_info_cb, data);
1521 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1522 CANCELLABLE_COLUMN, cancellable,
1526 /* Shortcuts Model */
1528 model_get_type_position (GtkFileChooserButton *button,
1533 if (row_type == ROW_TYPE_SPECIAL)
1536 retval += button->priv->n_special;
1538 if (row_type == ROW_TYPE_VOLUME)
1541 retval += button->priv->n_volumes;
1543 if (row_type == ROW_TYPE_SHORTCUT)
1546 retval += button->priv->n_shortcuts;
1548 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1551 retval += button->priv->has_bookmark_separator;
1553 if (row_type == ROW_TYPE_BOOKMARK)
1556 retval += button->priv->n_bookmarks;
1558 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1561 retval += button->priv->has_current_folder_separator;
1563 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1566 retval += button->priv->has_current_folder;
1568 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1571 retval += button->priv->has_other_separator;
1573 if (row_type == ROW_TYPE_OTHER)
1576 g_assert_not_reached ();
1581 model_free_row_data (GtkFileChooserButton *button,
1586 GCancellable *cancellable;
1588 gtk_tree_model_get (button->priv->model, iter,
1591 CANCELLABLE_COLUMN, &cancellable,
1596 g_cancellable_cancel (cancellable);
1597 g_object_unref (cancellable);
1602 case ROW_TYPE_SPECIAL:
1603 case ROW_TYPE_SHORTCUT:
1604 case ROW_TYPE_BOOKMARK:
1605 case ROW_TYPE_CURRENT_FOLDER:
1606 g_object_unref (data);
1608 case ROW_TYPE_VOLUME:
1609 _gtk_file_system_volume_unref (data);
1617 model_add_special_get_info_cb (GCancellable *cancellable,
1619 const GError *error,
1622 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1626 GCancellable *model_cancellable = NULL;
1627 struct ChangeIconThemeData *data = user_data;
1630 if (!data->button->priv->model)
1631 /* button got destroyed */
1634 path = gtk_tree_row_reference_get_path (data->row_ref);
1636 /* Cancellable doesn't exist anymore in the model */
1639 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1640 gtk_tree_path_free (path);
1642 gtk_tree_model_get (data->button->priv->model, &iter,
1643 CANCELLABLE_COLUMN, &model_cancellable,
1645 if (cancellable != model_cancellable)
1648 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1649 CANCELLABLE_COLUMN, NULL,
1652 if (cancelled || error)
1655 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1659 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1660 ICON_COLUMN, pixbuf,
1662 g_object_unref (pixbuf);
1665 gtk_tree_model_get (data->button->priv->model, &iter,
1666 DISPLAY_NAME_COLUMN, &name,
1669 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1670 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1675 g_object_unref (data->button);
1676 gtk_tree_row_reference_free (data->row_ref);
1679 if (model_cancellable)
1680 g_object_unref (model_cancellable);
1684 model_add_special (GtkFileChooserButton *button)
1686 const gchar *homedir;
1687 const gchar *desktopdir;
1688 GtkListStore *store;
1693 store = GTK_LIST_STORE (button->priv->model);
1694 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1696 homedir = g_get_home_dir ();
1700 GtkTreePath *tree_path;
1701 GCancellable *cancellable;
1702 struct ChangeIconThemeData *info;
1704 file = g_file_new_for_path (homedir);
1705 gtk_list_store_insert (store, &iter, pos);
1708 info = g_new0 (struct ChangeIconThemeData, 1);
1709 info->button = g_object_ref (button);
1710 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1711 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1713 gtk_tree_path_free (tree_path);
1715 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1716 "standard::icon,standard::display-name",
1717 model_add_special_get_info_cb, info);
1719 gtk_list_store_set (store, &iter,
1721 DISPLAY_NAME_COLUMN, NULL,
1722 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1724 IS_FOLDER_COLUMN, TRUE,
1725 CANCELLABLE_COLUMN, cancellable,
1728 button->priv->n_special++;
1731 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1733 /* "To disable a directory, point it to the homedir."
1734 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1736 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1738 GtkTreePath *tree_path;
1739 GCancellable *cancellable;
1740 struct ChangeIconThemeData *info;
1742 file = g_file_new_for_path (desktopdir);
1743 gtk_list_store_insert (store, &iter, pos);
1746 info = g_new0 (struct ChangeIconThemeData, 1);
1747 info->button = g_object_ref (button);
1748 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1749 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1751 gtk_tree_path_free (tree_path);
1753 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1754 "standard::icon,standard::display-name",
1755 model_add_special_get_info_cb, info);
1757 gtk_list_store_set (store, &iter,
1758 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1760 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1762 IS_FOLDER_COLUMN, TRUE,
1763 CANCELLABLE_COLUMN, cancellable,
1766 button->priv->n_special++;
1771 model_add_volumes (GtkFileChooserButton *button,
1774 GtkListStore *store;
1776 gboolean local_only;
1782 store = GTK_LIST_STORE (button->priv->model);
1783 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1784 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1786 for (l = volumes; l; l = l->next)
1788 GtkFileSystemVolume *volume;
1791 gchar *display_name;
1797 if (_gtk_file_system_volume_is_mounted (volume))
1801 base_file = _gtk_file_system_volume_get_root (volume);
1802 if (base_file != NULL)
1804 if (!g_file_is_native (base_file))
1806 g_object_unref (base_file);
1810 g_object_unref (base_file);
1815 pixbuf = _gtk_file_system_volume_render_icon (volume,
1816 GTK_WIDGET (button),
1817 button->priv->icon_size,
1819 display_name = _gtk_file_system_volume_get_display_name (volume);
1821 gtk_list_store_insert (store, &iter, pos);
1822 gtk_list_store_set (store, &iter,
1823 ICON_COLUMN, pixbuf,
1824 DISPLAY_NAME_COLUMN, display_name,
1825 TYPE_COLUMN, ROW_TYPE_VOLUME,
1826 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
1827 IS_FOLDER_COLUMN, TRUE,
1831 g_object_unref (pixbuf);
1832 g_free (display_name);
1834 button->priv->n_volumes++;
1839 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
1842 model_add_bookmarks (GtkFileChooserButton *button,
1845 GtkListStore *store;
1848 gboolean local_only;
1854 store = GTK_LIST_STORE (button->priv->model);
1855 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
1856 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1858 for (l = bookmarks; l; l = l->next)
1864 if (g_file_is_native (file))
1866 gtk_list_store_insert (store, &iter, pos);
1867 gtk_list_store_set (store, &iter,
1869 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1870 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1871 DATA_COLUMN, g_object_ref (file),
1872 IS_FOLDER_COLUMN, FALSE,
1874 set_info_for_file_at_iter (button, file, &iter);
1879 GtkIconTheme *icon_theme;
1885 /* Don't call get_info for remote paths to avoid latency and
1887 * If we switch to a better bookmarks file format (XBEL), we
1888 * should use mime info to get a better icon.
1890 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1892 label = _gtk_file_chooser_label_for_file (file);
1894 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1895 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1896 button->priv->icon_size, 0, NULL);
1898 gtk_list_store_insert (store, &iter, pos);
1899 gtk_list_store_set (store, &iter,
1900 ICON_COLUMN, pixbuf,
1901 DISPLAY_NAME_COLUMN, label,
1902 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1903 DATA_COLUMN, g_object_ref (file),
1904 IS_FOLDER_COLUMN, TRUE,
1908 g_object_unref (pixbuf);
1911 button->priv->n_bookmarks++;
1915 if (button->priv->n_bookmarks > 0 &&
1916 !button->priv->has_bookmark_separator)
1918 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
1920 gtk_list_store_insert (store, &iter, pos);
1921 gtk_list_store_set (store, &iter,
1923 DISPLAY_NAME_COLUMN, NULL,
1924 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
1926 IS_FOLDER_COLUMN, FALSE,
1928 button->priv->has_bookmark_separator = TRUE;
1933 model_update_current_folder (GtkFileChooserButton *button,
1936 GtkListStore *store;
1943 store = GTK_LIST_STORE (button->priv->model);
1945 if (!button->priv->has_current_folder_separator)
1947 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
1948 gtk_list_store_insert (store, &iter, pos);
1949 gtk_list_store_set (store, &iter,
1951 DISPLAY_NAME_COLUMN, NULL,
1952 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
1954 IS_FOLDER_COLUMN, FALSE,
1956 button->priv->has_current_folder_separator = TRUE;
1959 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
1960 if (!button->priv->has_current_folder)
1962 gtk_list_store_insert (store, &iter, pos);
1963 button->priv->has_current_folder = TRUE;
1967 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
1968 model_free_row_data (button, &iter);
1971 if (g_file_is_native (file))
1973 gtk_list_store_set (store, &iter,
1975 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1976 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
1977 DATA_COLUMN, g_object_ref (file),
1978 IS_FOLDER_COLUMN, FALSE,
1980 set_info_for_file_at_iter (button, file, &iter);
1985 GtkIconTheme *icon_theme;
1988 /* Don't call get_info for remote paths to avoid latency and
1990 * If we switch to a better bookmarks file format (XBEL), we
1991 * should use mime info to get a better icon.
1993 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1995 label = _gtk_file_chooser_label_for_file (file);
1997 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1999 if (g_file_is_native (file))
2000 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2001 button->priv->icon_size, 0, NULL);
2003 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2004 button->priv->icon_size, 0, NULL);
2006 gtk_list_store_set (store, &iter,
2007 ICON_COLUMN, pixbuf,
2008 DISPLAY_NAME_COLUMN, label,
2009 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2010 DATA_COLUMN, g_object_ref (file),
2011 IS_FOLDER_COLUMN, TRUE,
2015 g_object_unref (pixbuf);
2020 model_add_other (GtkFileChooserButton *button)
2022 GtkListStore *store;
2026 store = GTK_LIST_STORE (button->priv->model);
2027 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2029 gtk_list_store_insert (store, &iter, pos);
2030 gtk_list_store_set (store, &iter,
2032 DISPLAY_NAME_COLUMN, NULL,
2033 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2035 IS_FOLDER_COLUMN, FALSE,
2037 button->priv->has_other_separator = TRUE;
2040 gtk_list_store_insert (store, &iter, pos);
2041 gtk_list_store_set (store, &iter,
2043 DISPLAY_NAME_COLUMN, _("Other..."),
2044 TYPE_COLUMN, ROW_TYPE_OTHER,
2046 IS_FOLDER_COLUMN, FALSE,
2051 model_remove_rows (GtkFileChooserButton *button,
2055 GtkListStore *store;
2060 store = GTK_LIST_STORE (button->priv->model);
2066 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2067 g_assert_not_reached ();
2069 model_free_row_data (button, &iter);
2070 gtk_list_store_remove (store, &iter);
2077 static inline gboolean
2078 test_if_file_is_visible (GtkFileSystem *fs,
2080 gboolean local_only,
2086 if (local_only && !g_file_is_native (file))
2096 filter_model_visible_func (GtkTreeModel *model,
2100 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2101 GtkFileChooserButtonPrivate *priv = button->priv;
2104 gboolean local_only, retval, is_folder;
2106 type = ROW_TYPE_INVALID;
2108 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2110 gtk_tree_model_get (model, iter,
2113 IS_FOLDER_COLUMN, &is_folder,
2118 case ROW_TYPE_CURRENT_FOLDER:
2121 case ROW_TYPE_SPECIAL:
2122 case ROW_TYPE_SHORTCUT:
2123 case ROW_TYPE_BOOKMARK:
2124 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2126 case ROW_TYPE_VOLUME:
2131 if (_gtk_file_system_volume_is_mounted (data))
2135 base_file = _gtk_file_system_volume_get_root (data);
2139 if (!g_file_is_native (base_file))
2141 g_object_unref (base_file);
2159 name_cell_data_func (GtkCellLayout *layout,
2160 GtkCellRenderer *cell,
2161 GtkTreeModel *model,
2168 gtk_tree_model_get (model, iter,
2172 if (type == ROW_TYPE_CURRENT_FOLDER)
2173 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2175 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2179 combo_box_row_separator_func (GtkTreeModel *model,
2183 gchar type = ROW_TYPE_INVALID;
2185 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2187 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2188 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2189 type == ROW_TYPE_OTHER_SEPARATOR);
2193 update_combo_box (GtkFileChooserButton *button)
2195 GtkFileChooserButtonPrivate *priv = button->priv;
2200 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2202 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2211 type = ROW_TYPE_INVALID;
2214 gtk_tree_model_get (priv->filter_model, &iter,
2221 case ROW_TYPE_SPECIAL:
2222 case ROW_TYPE_SHORTCUT:
2223 case ROW_TYPE_BOOKMARK:
2224 case ROW_TYPE_CURRENT_FOLDER:
2225 row_found = (files &&
2227 g_file_equal (data, files->data));
2229 case ROW_TYPE_VOLUME:
2233 base_file = _gtk_file_system_volume_get_root (data);
2236 row_found = (files &&
2238 g_file_equal (base_file, files->data));
2239 g_object_unref (base_file);
2250 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2251 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2253 g_signal_handler_unblock (priv->combo_box,
2254 priv->combo_box_changed_id);
2257 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2259 /* If it hasn't been found already, update & select the current-folder row. */
2260 if (!row_found && files && files->data)
2262 GtkTreeIter filter_iter;
2265 model_update_current_folder (button, files->data);
2266 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2268 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2269 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2271 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2272 &filter_iter, &iter);
2274 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2275 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2276 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2279 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2280 g_slist_free (files);
2285 update_label_get_info_cb (GCancellable *cancellable,
2287 const GError *error,
2290 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2292 GtkFileChooserButton *button = data;
2293 GtkFileChooserButtonPrivate *priv = button->priv;
2295 if (cancellable != priv->update_button_cancellable)
2298 priv->update_button_cancellable = NULL;
2300 if (cancelled || error)
2303 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2305 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2308 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2310 priv->icon_size, 0, NULL);
2312 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2314 g_object_unref (pixbuf);
2317 g_object_unref (button);
2318 g_object_unref (cancellable);
2322 update_label_and_image (GtkFileChooserButton *button)
2324 GtkFileChooserButtonPrivate *priv = button->priv;
2328 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2331 if (priv->update_button_cancellable)
2333 g_cancellable_cancel (priv->update_button_cancellable);
2334 priv->update_button_cancellable = NULL;
2337 if (files && files->data)
2340 GtkFileSystemVolume *volume = NULL;
2344 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2349 base_file = _gtk_file_system_volume_get_root (volume);
2350 if (base_file && g_file_equal (base_file, file))
2354 label_text = _gtk_file_system_volume_get_display_name (volume);
2355 pixbuf = _gtk_file_system_volume_render_icon (volume,
2356 GTK_WIDGET (button),
2359 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2361 g_object_unref (pixbuf);
2365 g_object_unref (base_file);
2367 _gtk_file_system_volume_unref (volume);
2373 if (g_file_is_native (file))
2375 priv->update_button_cancellable =
2376 _gtk_file_system_get_info (priv->fs, file,
2377 "standard::icon,standard::display-name",
2378 update_label_get_info_cb,
2379 g_object_ref (button));
2385 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2386 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2388 priv->icon_size, 0, NULL);
2389 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2391 g_object_unref (pixbuf);
2395 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2396 g_slist_free (files);
2400 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2401 g_free (label_text);
2405 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2406 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2411 /* ************************ *
2412 * Child Object Callbacks *
2413 * ************************ */
2417 fs_volumes_changed_cb (GtkFileSystem *fs,
2420 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2421 GtkFileChooserButtonPrivate *priv = button->priv;
2424 model_remove_rows (user_data,
2425 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2428 priv->n_volumes = 0;
2430 volumes = _gtk_file_system_list_volumes (fs);
2431 model_add_volumes (user_data, volumes);
2432 g_slist_free (volumes);
2434 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2436 update_label_and_image (user_data);
2437 update_combo_box (user_data);
2441 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2444 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2445 GtkFileChooserButtonPrivate *priv = button->priv;
2448 bookmarks = _gtk_file_system_list_bookmarks (fs);
2449 model_remove_rows (user_data,
2450 model_get_type_position (user_data,
2451 ROW_TYPE_BOOKMARK_SEPARATOR),
2452 (priv->n_bookmarks + priv->has_bookmark_separator));
2453 priv->has_bookmark_separator = FALSE;
2454 priv->n_bookmarks = 0;
2455 model_add_bookmarks (user_data, bookmarks);
2456 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2457 g_slist_free (bookmarks);
2459 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2461 update_label_and_image (user_data);
2462 update_combo_box (user_data);
2467 open_dialog (GtkFileChooserButton *button)
2469 GtkFileChooserButtonPrivate *priv = button->priv;
2471 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2473 if (!gtk_widget_get_visible (priv->dialog))
2475 GtkWidget *toplevel;
2477 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2479 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2481 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2482 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2483 GTK_WINDOW (toplevel));
2485 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2486 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2494 g_signal_handler_block (priv->dialog,
2495 priv->dialog_folder_changed_id);
2496 g_signal_handler_block (priv->dialog,
2497 priv->dialog_file_activated_id);
2498 g_signal_handler_block (priv->dialog,
2499 priv->dialog_selection_changed_id);
2500 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2504 priv->old_file = g_object_ref (files->data);
2506 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2507 g_slist_free (files);
2510 priv->active = TRUE;
2513 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2514 gtk_window_present (GTK_WINDOW (priv->dialog));
2519 combo_box_changed_cb (GtkComboBox *combo_box,
2524 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2526 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2527 GtkFileChooserButtonPrivate *priv = button->priv;
2531 type = ROW_TYPE_INVALID;
2534 gtk_tree_model_get (priv->filter_model, &iter,
2541 case ROW_TYPE_SPECIAL:
2542 case ROW_TYPE_SHORTCUT:
2543 case ROW_TYPE_BOOKMARK:
2544 case ROW_TYPE_CURRENT_FOLDER:
2545 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2547 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2550 case ROW_TYPE_VOLUME:
2554 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2555 base_file = _gtk_file_system_volume_get_root (data);
2558 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2560 g_object_unref (base_file);
2564 case ROW_TYPE_OTHER:
2565 open_dialog (user_data);
2575 button_clicked_cb (GtkButton *real_button,
2578 open_dialog (user_data);
2583 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2586 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2587 GtkFileChooserButtonPrivate *priv = button->priv;
2589 priv->folder_has_been_set = TRUE;
2591 g_signal_emit_by_name (button, "current-folder-changed");
2595 dialog_file_activated_cb (GtkFileChooser *dialog,
2598 g_signal_emit_by_name (user_data, "file-activated");
2602 dialog_selection_changed_cb (GtkFileChooser *dialog,
2605 update_label_and_image (user_data);
2606 update_combo_box (user_data);
2607 g_signal_emit_by_name (user_data, "selection-changed");
2611 dialog_update_preview_cb (GtkFileChooser *dialog,
2614 g_signal_emit_by_name (user_data, "update-preview");
2618 dialog_notify_cb (GObject *dialog,
2624 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2625 GTK_TYPE_FILE_CHOOSER);
2626 if (g_object_interface_find_property (iface, pspec->name))
2627 g_object_notify (user_data, pspec->name);
2629 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2631 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2632 GtkFileChooserButtonPrivate *priv = button->priv;
2634 if (priv->has_current_folder)
2640 pos = model_get_type_position (user_data,
2641 ROW_TYPE_CURRENT_FOLDER);
2642 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2645 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2647 /* If the path isn't local but we're in local-only mode now, remove
2648 * the custom-folder row */
2649 if (data && g_file_is_native (G_FILE (data)) &&
2650 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2653 model_remove_rows (user_data, pos, 2);
2657 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2658 update_combo_box (user_data);
2663 dialog_delete_event_cb (GtkWidget *dialog,
2667 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2673 dialog_response_cb (GtkDialog *dialog,
2677 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2678 GtkFileChooserButtonPrivate *priv = button->priv;
2680 if (response == GTK_RESPONSE_ACCEPT ||
2681 response == GTK_RESPONSE_OK)
2683 g_signal_emit_by_name (user_data, "current-folder-changed");
2684 g_signal_emit_by_name (user_data, "selection-changed");
2686 else if (priv->old_file)
2688 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)))
2690 case GTK_FILE_CHOOSER_ACTION_OPEN:
2691 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (dialog), priv->old_file,
2694 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
2695 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog),
2696 priv->old_file, NULL);
2699 g_assert_not_reached ();
2704 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (dialog));
2708 g_object_unref (priv->old_file);
2709 priv->old_file = NULL;
2712 update_label_and_image (user_data);
2713 update_combo_box (user_data);
2717 g_signal_handler_unblock (priv->dialog,
2718 priv->dialog_folder_changed_id);
2719 g_signal_handler_unblock (priv->dialog,
2720 priv->dialog_file_activated_id);
2721 g_signal_handler_unblock (priv->dialog,
2722 priv->dialog_selection_changed_id);
2723 priv->active = FALSE;
2726 gtk_widget_set_sensitive (priv->combo_box, TRUE);
2727 gtk_widget_hide (priv->dialog);
2729 if (response == GTK_RESPONSE_ACCEPT ||
2730 response == GTK_RESPONSE_OK)
2731 g_signal_emit (user_data, file_chooser_button_signals[FILE_SET], 0);
2735 /* ************************************************************************** *
2737 * ************************************************************************** */
2740 * gtk_file_chooser_button_new:
2741 * @title: the title of the browse dialog.
2742 * @action: the open mode for the widget.
2744 * Creates a new file-selecting button widget.
2746 * Returns: a new button widget.
2751 gtk_file_chooser_button_new (const gchar *title,
2752 GtkFileChooserAction action)
2754 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2755 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2757 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2759 "title", (title ? title : _(DEFAULT_TITLE)),
2764 * gtk_file_chooser_button_new_with_dialog:
2765 * @dialog: the widget to use as dialog
2767 * Creates a #GtkFileChooserButton widget which uses @dialog as its
2768 * file-picking window.
2770 * Note that @dialog must be a #GtkDialog (or subclass) which
2771 * implements the #GtkFileChooser interface and must not have
2772 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2774 * Also note that the dialog needs to have its confirmative button
2775 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2776 * order for the button to take over the file selected in the dialog.
2778 * Returns: a new button widget.
2783 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2785 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2787 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2793 * gtk_file_chooser_button_set_title:
2794 * @button: the button widget to modify.
2795 * @title: the new browse dialog title.
2797 * Modifies the @title of the browse dialog used by @button.
2802 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2805 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2807 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2808 g_object_notify (G_OBJECT (button), "title");
2812 * gtk_file_chooser_button_get_title:
2813 * @button: the button widget to examine.
2815 * Retrieves the title of the browse dialog used by @button. The returned value
2816 * should not be modified or freed.
2818 * Returns: a pointer to the browse dialog's title.
2822 G_CONST_RETURN gchar *
2823 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2825 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2827 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2831 * gtk_file_chooser_button_get_width_chars:
2832 * @button: the button widget to examine.
2834 * Retrieves the width in characters of the @button widget's entry and/or label.
2836 * Returns: an integer width (in characters) that the button will use to size itself.
2841 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2843 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2845 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2849 * gtk_file_chooser_button_set_width_chars:
2850 * @button: the button widget to examine.
2851 * @n_chars: the new width, in characters.
2853 * Sets the width (in characters) that @button will use to @n_chars.
2858 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
2861 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2863 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
2864 g_object_notify (G_OBJECT (button), "width-chars");
2868 * gtk_file_chooser_button_set_focus_on_click:
2869 * @button: a #GtkFileChooserButton
2870 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2872 * Sets whether the button will grab focus when it is clicked with the mouse.
2873 * Making mouse clicks not grab focus is useful in places like toolbars where
2874 * you don't want the keyboard focus removed from the main area of the
2880 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
2881 gboolean focus_on_click)
2883 GtkFileChooserButtonPrivate *priv;
2885 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2887 priv = button->priv;
2889 focus_on_click = focus_on_click != FALSE;
2891 if (priv->focus_on_click != focus_on_click)
2893 priv->focus_on_click = focus_on_click;
2894 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
2895 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
2897 g_object_notify (G_OBJECT (button), "focus-on-click");
2902 * gtk_file_chooser_button_get_focus_on_click:
2903 * @button: a #GtkFileChooserButton
2905 * Returns whether the button grabs focus when it is clicked with the mouse.
2906 * See gtk_file_chooser_button_set_focus_on_click().
2908 * Return value: %TRUE if the button grabs focus when it is clicked with
2914 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
2916 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
2918 return button->priv->focus_on_click;