1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
3 /* GTK+: gtkfilechooserbutton.c
5 * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
32 #include "gtkbutton.h"
33 #include "gtkcelllayout.h"
34 #include "gtkcellrenderertext.h"
35 #include "gtkcellrendererpixbuf.h"
36 #include "gtkcombobox.h"
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
42 #include "gtkliststore.h"
44 #include "gtktreemodelfilter.h"
45 #include "gtkseparator.h"
46 #include "gtkfilechooserdialog.h"
47 #include "gtkfilechooserprivate.h"
48 #include "gtkfilechooserutils.h"
49 #include "gtkmarshalers.h"
51 #include "gtkfilechooserbutton.h"
53 #include "gtkorientable.h"
55 #include "gtktypebuiltins.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
61 * SECTION:gtkfilechooserbutton
62 * @Short_description: A button to launch a file selection dialog
63 * @Title: GtkFileChooserButton
64 * @See_also:#GtkFileChooserDialog
66 * The #GtkFileChooserButton is a widget that lets the user select a
67 * file. It implements the #GtkFileChooser interface. Visually, it is a
68 * file name with a button to bring up a #GtkFileChooserDialog.
69 * The user can then use that dialog to change the file associated with
70 * that button. This widget does not support setting the
71 * #GtkFileChooser:select-multiple property to %TRUE.
74 * <title>Create a button to let the user select a file in /etc</title>
79 * button = gtk_file_chooser_button_new (_("Select a file"),
80 * GTK_FILE_CHOOSER_ACTION_OPEN);
81 * gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
87 * The #GtkFileChooserButton supports the #GtkFileChooserAction<!-- -->s
88 * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
91 * The #GtkFileChooserButton will ellipsize the label,
92 * and thus will thus request little horizontal space. To give the button
93 * more space, you should call gtk_widget_get_preferred_size(),
94 * gtk_file_chooser_button_set_width_chars(), or pack the button in
95 * such a way that other interface elements give space to the widget.
100 /* **************** *
102 * **************** */
104 #define DEFAULT_TITLE N_("Select a File")
105 #define DESKTOP_DISPLAY_NAME N_("Desktop")
106 #define FALLBACK_DISPLAY_NAME N_("(None)") /* this string is used in gtk+/gtk/tests/filechooser.c - change it there if you change it here */
107 #define FALLBACK_ICON_NAME "stock_unknown"
108 #define FALLBACK_ICON_SIZE 16
111 /* ********************** *
112 * Private Enumerations *
113 * ********************** */
133 /* TreeModel Columns */
145 /* TreeModel Row Types */
151 ROW_TYPE_BOOKMARK_SEPARATOR,
153 ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
154 ROW_TYPE_CURRENT_FOLDER,
155 ROW_TYPE_OTHER_SEPARATOR,
157 ROW_TYPE_EMPTY_SELECTION,
159 ROW_TYPE_INVALID = -1
164 /* ******************** *
165 * Private Structures *
166 * ******************** */
168 struct _GtkFileChooserButtonPrivate
174 GtkWidget *combo_box;
175 GtkCellRenderer *icon_cell;
176 GtkCellRenderer *name_cell;
179 GtkTreeModel *filter_model;
182 GFile *selection_while_inactive;
183 GFile *current_folder_while_inactive;
185 gulong combo_box_changed_id;
186 gulong dialog_file_activated_id;
187 gulong dialog_folder_changed_id;
188 gulong dialog_selection_changed_id;
189 gulong fs_volumes_changed_id;
190 gulong fs_bookmarks_changed_id;
192 GCancellable *dnd_select_folder_cancellable;
193 GCancellable *update_button_cancellable;
194 GSList *change_icon_theme_cancellables;
202 guint has_bookmark_separator : 1;
203 guint has_current_folder_separator : 1;
204 guint has_current_folder : 1;
205 guint has_other_separator : 1;
207 /* Used for hiding/showing the dialog when the button is hidden */
210 guint focus_on_click : 1;
225 /* ********************* *
226 * Function Prototypes *
227 * ********************* */
229 /* GtkFileChooserIface Functions */
230 static void gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
231 static gboolean gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
234 static GFile *gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser);
235 static gboolean gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
238 static void gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
240 static void gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser);
241 static GSList *gtk_file_chooser_button_get_files (GtkFileChooser *chooser);
242 static gboolean gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
245 static gboolean gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
249 /* GObject Functions */
250 static GObject *gtk_file_chooser_button_constructor (GType type,
252 GObjectConstructParam *params);
253 static void gtk_file_chooser_button_set_property (GObject *object,
257 static void gtk_file_chooser_button_get_property (GObject *object,
261 static void gtk_file_chooser_button_finalize (GObject *object);
263 /* GtkWidget Functions */
264 static void gtk_file_chooser_button_destroy (GtkWidget *widget);
265 static void gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
266 GdkDragContext *context,
269 GtkSelectionData *data,
272 static void gtk_file_chooser_button_show_all (GtkWidget *widget);
273 static void gtk_file_chooser_button_show (GtkWidget *widget);
274 static void gtk_file_chooser_button_hide (GtkWidget *widget);
275 static void gtk_file_chooser_button_map (GtkWidget *widget);
276 static gboolean gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
277 gboolean group_cycling);
278 static void gtk_file_chooser_button_style_updated (GtkWidget *widget);
279 static void gtk_file_chooser_button_screen_changed (GtkWidget *widget,
280 GdkScreen *old_screen);
282 /* Utility Functions */
283 static GtkIconTheme *get_icon_theme (GtkWidget *widget);
284 static void set_info_for_file_at_iter (GtkFileChooserButton *fs,
288 static gint model_get_type_position (GtkFileChooserButton *button,
290 static void model_free_row_data (GtkFileChooserButton *button,
292 static void model_add_special (GtkFileChooserButton *button);
293 static void model_add_other (GtkFileChooserButton *button);
294 static void model_add_empty_selection (GtkFileChooserButton *button);
295 static void model_add_volumes (GtkFileChooserButton *button,
297 static void model_add_bookmarks (GtkFileChooserButton *button,
299 static void model_update_current_folder (GtkFileChooserButton *button,
301 static void model_remove_rows (GtkFileChooserButton *button,
305 static gboolean filter_model_visible_func (GtkTreeModel *model,
309 static gboolean combo_box_row_separator_func (GtkTreeModel *model,
312 static void name_cell_data_func (GtkCellLayout *layout,
313 GtkCellRenderer *cell,
317 static void open_dialog (GtkFileChooserButton *button);
318 static void update_combo_box (GtkFileChooserButton *button);
319 static void update_label_and_image (GtkFileChooserButton *button);
321 /* Child Object Callbacks */
322 static void fs_volumes_changed_cb (GtkFileSystem *fs,
324 static void fs_bookmarks_changed_cb (GtkFileSystem *fs,
327 static void combo_box_changed_cb (GtkComboBox *combo_box,
330 static void button_clicked_cb (GtkButton *real_button,
333 static void dialog_update_preview_cb (GtkFileChooser *dialog,
335 static void dialog_selection_changed_cb (GtkFileChooser *dialog,
337 static void dialog_file_activated_cb (GtkFileChooser *dialog,
339 static void dialog_current_folder_changed_cb (GtkFileChooser *dialog,
341 static void dialog_notify_cb (GObject *dialog,
344 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
347 static void dialog_response_cb (GtkDialog *dialog,
351 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
353 /* ******************* *
354 * GType Declaration *
355 * ******************* */
357 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
358 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
362 /* ***************** *
364 * ***************** */
367 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
369 GObjectClass *gobject_class;
370 GtkWidgetClass *widget_class;
372 gobject_class = G_OBJECT_CLASS (class);
373 widget_class = GTK_WIDGET_CLASS (class);
375 gobject_class->constructor = gtk_file_chooser_button_constructor;
376 gobject_class->set_property = gtk_file_chooser_button_set_property;
377 gobject_class->get_property = gtk_file_chooser_button_get_property;
378 gobject_class->finalize = gtk_file_chooser_button_finalize;
380 widget_class->destroy = gtk_file_chooser_button_destroy;
381 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
382 widget_class->show_all = gtk_file_chooser_button_show_all;
383 widget_class->show = gtk_file_chooser_button_show;
384 widget_class->hide = gtk_file_chooser_button_hide;
385 widget_class->map = gtk_file_chooser_button_map;
386 widget_class->style_updated = gtk_file_chooser_button_style_updated;
387 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
388 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
391 * GtkFileChooserButton::file-set:
392 * @widget: the object which received the signal.
394 * The ::file-set signal is emitted when the user selects a file.
396 * Note that this signal is only emitted when the <emphasis>user</emphasis>
401 file_chooser_button_signals[FILE_SET] =
402 g_signal_new (I_("file-set"),
403 G_TYPE_FROM_CLASS (gobject_class),
405 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
407 _gtk_marshal_VOID__VOID,
411 * GtkFileChooserButton:dialog:
413 * Instance of the #GtkFileChooserDialog associated with the button.
417 g_object_class_install_property (gobject_class, PROP_DIALOG,
418 g_param_spec_object ("dialog",
420 P_("The file chooser dialog to use."),
421 GTK_TYPE_FILE_CHOOSER,
422 (GTK_PARAM_WRITABLE |
423 G_PARAM_CONSTRUCT_ONLY)));
426 * GtkFileChooserButton:focus-on-click:
428 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
433 g_object_class_install_property (gobject_class,
435 g_param_spec_boolean ("focus-on-click",
436 P_("Focus on click"),
437 P_("Whether the button grabs focus when it is clicked with the mouse"),
439 GTK_PARAM_READWRITE));
442 * GtkFileChooserButton:title:
444 * Title to put on the #GtkFileChooserDialog associated with the button.
448 g_object_class_install_property (gobject_class, PROP_TITLE,
449 g_param_spec_string ("title",
451 P_("The title of the file chooser dialog."),
453 GTK_PARAM_READWRITE));
456 * GtkFileChooserButton:width-chars:
458 * The width of the entry and label inside the button, in characters.
462 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
463 g_param_spec_int ("width-chars",
464 P_("Width In Characters"),
465 P_("The desired width of the button widget, in characters."),
467 GTK_PARAM_READWRITE));
469 _gtk_file_chooser_install_properties (gobject_class);
471 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
475 gtk_file_chooser_button_init (GtkFileChooserButton *button)
477 GtkFileChooserButtonPrivate *priv;
478 GtkWidget *box, *image, *sep;
479 GtkTargetList *target_list;
481 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
482 GTK_TYPE_FILE_CHOOSER_BUTTON,
483 GtkFileChooserButtonPrivate);
485 priv->icon_size = FALLBACK_ICON_SIZE;
486 priv->focus_on_click = TRUE;
488 gtk_widget_push_composite_child ();
491 priv->button = gtk_button_new ();
492 g_signal_connect (priv->button, "clicked",
493 G_CALLBACK (button_clicked_cb), button);
494 gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
495 gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
496 gtk_widget_show (priv->button);
498 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
499 gtk_container_add (GTK_CONTAINER (priv->button), box);
500 gtk_widget_show (box);
502 priv->image = gtk_image_new ();
503 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
504 gtk_widget_show (priv->image);
506 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
507 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
508 gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
509 gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
510 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
511 //gtk_container_add (GTK_CONTAINER (box), priv->label);
512 gtk_widget_show (priv->label);
514 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
515 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
516 gtk_widget_show (sep);
518 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
519 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
520 gtk_widget_show (image);
523 /* Keep in sync with columns enum, line 88 */
525 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
526 GDK_TYPE_PIXBUF, /* ICON_COLUMN */
527 G_TYPE_STRING, /* DISPLAY_NAME_COLUMN */
528 G_TYPE_CHAR, /* TYPE_COLUMN */
529 G_TYPE_POINTER /* DATA_COLUMN (Volume || Path) */,
530 G_TYPE_BOOLEAN /* IS_FOLDER_COLUMN */,
531 G_TYPE_POINTER /* CANCELLABLE_COLUMN */));
533 priv->combo_box = gtk_combo_box_new ();
534 priv->combo_box_changed_id =
535 g_signal_connect (priv->combo_box, "changed",
536 G_CALLBACK (combo_box_changed_cb), button);
537 gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
538 gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
540 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
541 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
542 priv->icon_cell, FALSE);
543 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
544 priv->icon_cell, "pixbuf", ICON_COLUMN);
546 priv->name_cell = gtk_cell_renderer_text_new ();
547 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
548 priv->name_cell, TRUE);
549 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
550 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
551 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
552 priv->name_cell, name_cell_data_func,
555 gtk_widget_pop_composite_child ();
558 gtk_drag_dest_set (GTK_WIDGET (button),
559 (GTK_DEST_DEFAULT_ALL),
562 target_list = gtk_target_list_new (NULL, 0);
563 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
564 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
565 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
566 gtk_target_list_unref (target_list);
570 /* ******************************* *
571 * GtkFileChooserIface Functions *
572 * ******************************* */
574 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
576 _gtk_file_chooser_delegate_iface_init (iface);
578 iface->set_current_folder = gtk_file_chooser_button_set_current_folder;
579 iface->get_current_folder = gtk_file_chooser_button_get_current_folder;
580 iface->select_file = gtk_file_chooser_button_select_file;
581 iface->unselect_file = gtk_file_chooser_button_unselect_file;
582 iface->unselect_all = gtk_file_chooser_button_unselect_all;
583 iface->get_files = gtk_file_chooser_button_get_files;
584 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
585 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
589 gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
593 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
594 GtkFileChooserButtonPrivate *priv = button->priv;
595 GtkFileChooser *delegate;
597 delegate = g_object_get_qdata (G_OBJECT (chooser),
598 GTK_FILE_CHOOSER_DELEGATE_QUARK);
601 return gtk_file_chooser_set_current_folder_file (delegate, file, error);
604 if (priv->current_folder_while_inactive)
605 g_object_unref (priv->current_folder_while_inactive);
607 priv->current_folder_while_inactive = g_object_ref (file);
609 update_combo_box (button);
611 g_signal_emit_by_name (button, "current-folder-changed");
618 gtk_file_chooser_button_get_current_folder (GtkFileChooser *chooser)
620 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
621 GtkFileChooserButtonPrivate *priv = button->priv;
622 GtkFileChooser *delegate;
624 delegate = g_object_get_qdata (G_OBJECT (chooser),
625 GTK_FILE_CHOOSER_DELEGATE_QUARK);
628 return gtk_file_chooser_get_current_folder_file (delegate);
631 if (priv->current_folder_while_inactive)
632 return g_object_ref (priv->current_folder_while_inactive);
639 gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
643 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
644 GtkFileChooserButtonPrivate *priv = button->priv;
645 GtkFileChooser *delegate;
647 delegate = g_object_get_qdata (G_OBJECT (chooser),
648 GTK_FILE_CHOOSER_DELEGATE_QUARK);
651 return gtk_file_chooser_select_file (delegate, file, error);
654 if (priv->selection_while_inactive)
655 g_object_unref (priv->selection_while_inactive);
657 priv->selection_while_inactive = g_object_ref (file);
659 update_label_and_image (button);
660 update_combo_box (button);
667 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
670 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
671 GtkFileChooserButtonPrivate *priv = button->priv;
672 GtkFileChooser *delegate;
674 delegate = g_object_get_qdata (G_OBJECT (chooser),
675 GTK_FILE_CHOOSER_DELEGATE_QUARK);
678 gtk_file_chooser_unselect_file (delegate, file);
681 if (g_file_equal (priv->selection_while_inactive, file))
683 if (priv->selection_while_inactive)
685 g_object_unref (priv->selection_while_inactive);
686 priv->selection_while_inactive = NULL;
689 update_label_and_image (button);
690 update_combo_box (button);
696 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
698 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
699 GtkFileChooserButtonPrivate *priv = button->priv;
700 GtkFileChooser *delegate;
702 delegate = g_object_get_qdata (G_OBJECT (chooser),
703 GTK_FILE_CHOOSER_DELEGATE_QUARK);
706 gtk_file_chooser_unselect_all (delegate);
709 if (priv->selection_while_inactive)
711 g_object_unref (priv->selection_while_inactive);
712 priv->selection_while_inactive = NULL;
714 update_label_and_image (button);
715 update_combo_box (button);
721 get_selected_file (GtkFileChooserButton *button)
723 GtkFileChooserButtonPrivate *priv = button->priv;
726 return gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
729 if (priv->selection_while_inactive)
730 return g_object_ref (priv->selection_while_inactive);
731 else if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)) == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
733 /* If there is no "real" selection in SELECT_FOLDER mode, then we'll just return
734 * the current folder, since that is what GtkFileChooserDefault would do.
736 if (priv->current_folder_while_inactive)
737 return g_object_ref (priv->current_folder_while_inactive);
745 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
747 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
749 return g_slist_prepend (NULL, get_selected_file (button));
753 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
757 GtkFileChooser *delegate;
760 delegate = g_object_get_qdata (G_OBJECT (chooser),
761 GTK_FILE_CHOOSER_DELEGATE_QUARK);
762 retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
766 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
767 GtkFileChooserButtonPrivate *priv = button->priv;
771 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
772 pos += priv->n_shortcuts;
774 gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
775 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
777 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
778 TYPE_COLUMN, ROW_TYPE_SHORTCUT,
779 DATA_COLUMN, g_object_ref (file),
780 IS_FOLDER_COLUMN, FALSE,
782 set_info_for_file_at_iter (button, file, &iter);
785 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
792 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
796 GtkFileChooser *delegate;
799 delegate = g_object_get_qdata (G_OBJECT (chooser),
800 GTK_FILE_CHOOSER_DELEGATE_QUARK);
802 retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
806 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
807 GtkFileChooserButtonPrivate *priv = button->priv;
812 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
813 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
819 gtk_tree_model_get (priv->model, &iter,
824 if (type == ROW_TYPE_SHORTCUT &&
825 data && g_file_equal (data, file))
827 model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
828 gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
830 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
831 update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
835 while (type == ROW_TYPE_SHORTCUT &&
836 gtk_tree_model_iter_next (priv->model, &iter));
843 /* ******************* *
844 * GObject Functions *
845 * ******************* */
848 gtk_file_chooser_button_constructor (GType type,
850 GObjectConstructParam *params)
853 GtkFileChooserButton *button;
854 GtkFileChooserButtonPrivate *priv;
857 object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
860 button = GTK_FILE_CHOOSER_BUTTON (object);
865 priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
866 GTK_FILE_CHOOSER_ACTION_OPEN,
873 gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
874 GTK_RESPONSE_ACCEPT);
875 gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
880 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
882 else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
884 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
887 g_signal_connect (priv->dialog, "delete-event",
888 G_CALLBACK (dialog_delete_event_cb), object);
889 g_signal_connect (priv->dialog, "response",
890 G_CALLBACK (dialog_response_cb), object);
892 /* This is used, instead of the standard delegate, to ensure that signals are only
893 * delegated when the OK button is pressed. */
894 g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
895 priv->dialog_folder_changed_id =
896 g_signal_connect (priv->dialog, "current-folder-changed",
897 G_CALLBACK (dialog_current_folder_changed_cb), object);
898 priv->dialog_file_activated_id =
899 g_signal_connect (priv->dialog, "file-activated",
900 G_CALLBACK (dialog_file_activated_cb), object);
901 priv->dialog_selection_changed_id =
902 g_signal_connect (priv->dialog, "selection-changed",
903 G_CALLBACK (dialog_selection_changed_cb), object);
904 g_signal_connect (priv->dialog, "update-preview",
905 G_CALLBACK (dialog_update_preview_cb), object);
906 g_signal_connect (priv->dialog, "notify",
907 G_CALLBACK (dialog_notify_cb), object);
908 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
909 (gpointer) (&priv->dialog));
912 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
914 model_add_special (button);
916 list = _gtk_file_system_list_volumes (priv->fs);
917 model_add_volumes (button, list);
920 list = _gtk_file_system_list_bookmarks (priv->fs);
921 model_add_bookmarks (button, list);
922 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
925 model_add_other (button);
927 model_add_empty_selection (button);
929 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
930 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
931 filter_model_visible_func,
934 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
935 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
936 combo_box_row_separator_func,
939 /* set up the action for a user-provided dialog, this also updates
940 * the label, image and combobox
942 g_object_set (object,
943 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
946 priv->fs_volumes_changed_id =
947 g_signal_connect (priv->fs, "volumes-changed",
948 G_CALLBACK (fs_volumes_changed_cb), object);
949 priv->fs_bookmarks_changed_id =
950 g_signal_connect (priv->fs, "bookmarks-changed",
951 G_CALLBACK (fs_bookmarks_changed_cb), object);
953 update_label_and_image (button);
954 update_combo_box (button);
960 gtk_file_chooser_button_set_property (GObject *object,
965 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
966 GtkFileChooserButtonPrivate *priv = button->priv;
972 priv->dialog = g_value_get_object (value);
974 case PROP_FOCUS_ON_CLICK:
975 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
977 case PROP_WIDTH_CHARS:
978 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
979 g_value_get_int (value));
981 case GTK_FILE_CHOOSER_PROP_ACTION:
982 switch (g_value_get_enum (value))
984 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
985 case GTK_FILE_CHOOSER_ACTION_SAVE:
990 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
991 eval = g_enum_get_value (eclass, g_value_get_enum (value));
992 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
993 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
995 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
1000 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1001 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
1002 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
1004 switch (g_value_get_enum (value))
1006 case GTK_FILE_CHOOSER_ACTION_OPEN:
1007 gtk_widget_hide (priv->combo_box);
1008 gtk_widget_show (priv->button);
1010 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1011 gtk_widget_hide (priv->button);
1012 gtk_widget_show (priv->combo_box);
1015 g_assert_not_reached ();
1021 case GTK_FILE_CHOOSER_PROP_FILTER:
1022 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1023 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1024 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1025 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1026 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1027 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1028 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1029 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1032 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1033 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
1034 fs_volumes_changed_cb (priv->fs, button);
1035 fs_bookmarks_changed_cb (priv->fs, button);
1038 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1039 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
1040 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
1043 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1049 gtk_file_chooser_button_get_property (GObject *object,
1054 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1055 GtkFileChooserButtonPrivate *priv = button->priv;
1059 case PROP_WIDTH_CHARS:
1060 g_value_set_int (value,
1061 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
1063 case PROP_FOCUS_ON_CLICK:
1064 g_value_set_boolean (value,
1065 gtk_file_chooser_button_get_focus_on_click (button));
1069 case GTK_FILE_CHOOSER_PROP_ACTION:
1070 case GTK_FILE_CHOOSER_PROP_FILTER:
1071 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
1072 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
1073 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1074 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1075 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1076 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1077 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1078 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1079 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1080 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1084 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1090 gtk_file_chooser_button_finalize (GObject *object)
1092 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1093 GtkFileChooserButtonPrivate *priv = button->priv;
1095 if (priv->selection_while_inactive)
1096 g_object_unref (priv->selection_while_inactive);
1098 if (priv->current_folder_while_inactive)
1099 g_object_unref (priv->current_folder_while_inactive);
1101 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1104 /* ********************* *
1105 * GtkWidget Functions *
1106 * ********************* */
1109 gtk_file_chooser_button_destroy (GtkWidget *widget)
1111 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1112 GtkFileChooserButtonPrivate *priv = button->priv;
1116 if (priv->dialog != NULL)
1118 gtk_widget_destroy (priv->dialog);
1119 priv->dialog = NULL;
1122 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1124 model_free_row_data (button, &iter);
1126 while (gtk_tree_model_iter_next (priv->model, &iter));
1128 if (priv->dnd_select_folder_cancellable)
1130 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1131 priv->dnd_select_folder_cancellable = NULL;
1134 if (priv->update_button_cancellable)
1136 g_cancellable_cancel (priv->update_button_cancellable);
1137 priv->update_button_cancellable = NULL;
1140 if (priv->change_icon_theme_cancellables)
1142 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1144 GCancellable *cancellable = G_CANCELLABLE (l->data);
1145 g_cancellable_cancel (cancellable);
1147 g_slist_free (priv->change_icon_theme_cancellables);
1148 priv->change_icon_theme_cancellables = NULL;
1153 g_object_unref (priv->model);
1157 if (priv->filter_model)
1159 g_object_unref (priv->filter_model);
1160 priv->filter_model = NULL;
1165 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1166 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1167 g_object_unref (priv->fs);
1171 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1174 struct DndSelectFolderData
1176 GtkFileSystem *file_system;
1177 GtkFileChooserButton *button;
1178 GtkFileChooserAction action;
1186 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1188 const GError *error,
1191 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1192 struct DndSelectFolderData *data = user_data;
1194 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1196 g_object_unref (data->button);
1197 g_object_unref (data->file);
1198 g_strfreev (data->uris);
1201 g_object_unref (cancellable);
1205 data->button->priv->dnd_select_folder_cancellable = NULL;
1207 if (!cancelled && !error && info != NULL)
1211 is_folder = _gtk_file_info_consider_as_directory (info);
1214 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1215 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1216 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1220 data->selected = FALSE;
1222 if (data->selected || data->uris[++data->i] == NULL)
1224 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1226 g_object_unref (data->button);
1227 g_object_unref (data->file);
1228 g_strfreev (data->uris);
1231 g_object_unref (cancellable);
1236 g_object_unref (data->file);
1238 data->file = g_file_new_for_uri (data->uris[data->i]);
1240 data->button->priv->dnd_select_folder_cancellable =
1241 _gtk_file_system_get_info (data->file_system, data->file,
1243 dnd_select_folder_get_info_cb, user_data);
1245 g_object_unref (cancellable);
1249 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1250 GdkDragContext *context,
1253 GtkSelectionData *data,
1257 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1258 GtkFileChooserButtonPrivate *priv = button->priv;
1262 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1263 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1269 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1277 struct DndSelectFolderData *info;
1279 uris = gtk_selection_data_get_uris (data);
1284 info = g_new0 (struct DndSelectFolderData, 1);
1285 info->button = g_object_ref (button);
1288 info->selected = FALSE;
1289 info->file_system = priv->fs;
1290 g_object_get (priv->dialog, "action", &info->action, NULL);
1292 info->file = g_file_new_for_uri (info->uris[info->i]);
1294 if (priv->dnd_select_folder_cancellable)
1295 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1297 priv->dnd_select_folder_cancellable =
1298 _gtk_file_system_get_info (priv->fs, info->file,
1300 dnd_select_folder_get_info_cb, info);
1305 text = (char*) gtk_selection_data_get_text (data);
1306 file = g_file_new_for_uri (text);
1307 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1309 g_object_unref (file);
1311 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1318 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1322 gtk_file_chooser_button_show_all (GtkWidget *widget)
1324 gtk_widget_show (widget);
1328 gtk_file_chooser_button_show (GtkWidget *widget)
1330 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1331 GtkFileChooserButtonPrivate *priv = button->priv;
1333 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1334 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1337 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1341 gtk_file_chooser_button_hide (GtkWidget *widget)
1343 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1344 GtkFileChooserButtonPrivate *priv = button->priv;
1346 gtk_widget_hide (priv->dialog);
1348 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1349 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1353 gtk_file_chooser_button_map (GtkWidget *widget)
1355 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1359 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1360 gboolean group_cycling)
1362 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1363 GtkFileChooserButtonPrivate *priv = button->priv;
1365 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1367 case GTK_FILE_CHOOSER_ACTION_OPEN:
1368 gtk_widget_grab_focus (priv->button);
1370 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1371 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1374 g_assert_not_reached ();
1381 /* Changes the icons wherever it is needed */
1382 struct ChangeIconThemeData
1384 GtkFileChooserButton *button;
1385 GtkTreeRowReference *row_ref;
1389 change_icon_theme_get_info_cb (GCancellable *cancellable,
1391 const GError *error,
1394 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1396 struct ChangeIconThemeData *data = user_data;
1398 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1401 data->button->priv->change_icon_theme_cancellables =
1402 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1404 if (cancelled || error)
1407 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1415 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1417 path = gtk_tree_row_reference_get_path (data->row_ref);
1420 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1421 gtk_tree_path_free (path);
1423 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1424 ICON_COLUMN, pixbuf,
1427 g_object_set (data->button->priv->icon_cell,
1431 g_object_unref (pixbuf);
1435 g_object_unref (data->button);
1436 gtk_tree_row_reference_free (data->row_ref);
1439 g_object_unref (cancellable);
1443 change_icon_theme (GtkFileChooserButton *button)
1445 GtkFileChooserButtonPrivate *priv = button->priv;
1446 GtkSettings *settings;
1447 GtkIconTheme *theme;
1450 gint width = 0, height = 0;
1452 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1454 GCancellable *cancellable = G_CANCELLABLE (l->data);
1455 g_cancellable_cancel (cancellable);
1457 g_slist_free (button->priv->change_icon_theme_cancellables);
1458 button->priv->change_icon_theme_cancellables = NULL;
1460 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1462 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1464 priv->icon_size = MAX (width, height);
1466 priv->icon_size = FALLBACK_ICON_SIZE;
1468 update_label_and_image (button);
1470 gtk_tree_model_get_iter_first (priv->model, &iter);
1472 theme = get_icon_theme (GTK_WIDGET (button));
1480 type = ROW_TYPE_INVALID;
1481 gtk_tree_model_get (priv->model, &iter,
1488 case ROW_TYPE_SPECIAL:
1489 case ROW_TYPE_SHORTCUT:
1490 case ROW_TYPE_BOOKMARK:
1491 case ROW_TYPE_CURRENT_FOLDER:
1494 if (g_file_is_native (G_FILE (data)))
1497 GCancellable *cancellable;
1498 struct ChangeIconThemeData *info;
1500 info = g_new0 (struct ChangeIconThemeData, 1);
1501 info->button = g_object_ref (button);
1502 path = gtk_tree_model_get_path (priv->model, &iter);
1503 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1504 gtk_tree_path_free (path);
1507 _gtk_file_system_get_info (priv->fs, data,
1509 change_icon_theme_get_info_cb,
1511 button->priv->change_icon_theme_cancellables =
1512 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1516 /* Don't call get_info for remote paths to avoid latency and
1518 * If we switch to a better bookmarks file format (XBEL), we
1519 * should use mime info to get a better icon.
1521 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1522 priv->icon_size, 0, NULL);
1525 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1526 priv->icon_size, 0, NULL);
1528 case ROW_TYPE_VOLUME:
1530 pixbuf = _gtk_file_system_volume_render_icon (data,
1531 GTK_WIDGET (button),
1535 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1536 priv->icon_size, 0, NULL);
1544 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1546 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1547 ICON_COLUMN, pixbuf,
1551 g_object_unref (pixbuf);
1553 while (gtk_tree_model_iter_next (priv->model, &iter));
1555 g_object_set (button->priv->icon_cell,
1561 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1563 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1565 if (gtk_widget_has_screen (widget))
1566 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1570 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1571 GdkScreen *old_screen)
1573 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1574 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1577 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1581 /* ******************* *
1582 * Utility Functions *
1583 * ******************* */
1586 static GtkIconTheme *
1587 get_icon_theme (GtkWidget *widget)
1589 if (gtk_widget_has_screen (widget))
1590 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1592 return gtk_icon_theme_get_default ();
1596 struct SetDisplayNameData
1598 GtkFileChooserButton *button;
1600 GtkTreeRowReference *row_ref;
1604 set_info_get_info_cb (GCancellable *cancellable,
1606 const GError *error,
1607 gpointer callback_data)
1609 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1613 GCancellable *model_cancellable = NULL;
1614 struct SetDisplayNameData *data = callback_data;
1617 if (!data->button->priv->model)
1618 /* button got destroyed */
1621 path = gtk_tree_row_reference_get_path (data->row_ref);
1623 /* Cancellable doesn't exist anymore in the model */
1626 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1627 gtk_tree_path_free (path);
1629 /* Validate the cancellable */
1630 gtk_tree_model_get (data->button->priv->model, &iter,
1631 CANCELLABLE_COLUMN, &model_cancellable,
1633 if (cancellable != model_cancellable)
1636 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1637 CANCELLABLE_COLUMN, NULL,
1640 if (cancelled || error)
1641 /* There was an error, leave the fallback name in there */
1644 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1647 data->label = g_strdup (g_file_info_get_display_name (info));
1649 is_folder = _gtk_file_info_consider_as_directory (info);
1651 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1652 ICON_COLUMN, pixbuf,
1653 DISPLAY_NAME_COLUMN, data->label,
1654 IS_FOLDER_COLUMN, is_folder,
1658 g_object_unref (pixbuf);
1661 g_object_unref (data->button);
1662 g_free (data->label);
1663 gtk_tree_row_reference_free (data->row_ref);
1666 if (model_cancellable)
1667 g_object_unref (model_cancellable);
1671 set_info_for_file_at_iter (GtkFileChooserButton *button,
1675 struct SetDisplayNameData *data;
1676 GtkTreePath *tree_path;
1677 GCancellable *cancellable;
1679 data = g_new0 (struct SetDisplayNameData, 1);
1680 data->button = g_object_ref (button);
1681 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1683 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1684 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1685 gtk_tree_path_free (tree_path);
1687 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1688 "standard::type,standard::icon,standard::display-name",
1689 set_info_get_info_cb, data);
1691 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1692 CANCELLABLE_COLUMN, cancellable,
1696 /* Shortcuts Model */
1698 model_get_type_position (GtkFileChooserButton *button,
1703 if (row_type == ROW_TYPE_SPECIAL)
1706 retval += button->priv->n_special;
1708 if (row_type == ROW_TYPE_VOLUME)
1711 retval += button->priv->n_volumes;
1713 if (row_type == ROW_TYPE_SHORTCUT)
1716 retval += button->priv->n_shortcuts;
1718 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1721 retval += button->priv->has_bookmark_separator;
1723 if (row_type == ROW_TYPE_BOOKMARK)
1726 retval += button->priv->n_bookmarks;
1728 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1731 retval += button->priv->has_current_folder_separator;
1733 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1736 retval += button->priv->has_current_folder;
1738 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1741 retval += button->priv->has_other_separator;
1743 if (row_type == ROW_TYPE_OTHER)
1748 if (row_type == ROW_TYPE_EMPTY_SELECTION)
1751 g_assert_not_reached ();
1756 model_free_row_data (GtkFileChooserButton *button,
1761 GCancellable *cancellable;
1763 gtk_tree_model_get (button->priv->model, iter,
1766 CANCELLABLE_COLUMN, &cancellable,
1771 g_cancellable_cancel (cancellable);
1772 g_object_unref (cancellable);
1777 case ROW_TYPE_SPECIAL:
1778 case ROW_TYPE_SHORTCUT:
1779 case ROW_TYPE_BOOKMARK:
1780 case ROW_TYPE_CURRENT_FOLDER:
1781 g_object_unref (data);
1783 case ROW_TYPE_VOLUME:
1784 _gtk_file_system_volume_unref (data);
1792 model_add_special_get_info_cb (GCancellable *cancellable,
1794 const GError *error,
1797 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1801 GCancellable *model_cancellable = NULL;
1802 struct ChangeIconThemeData *data = user_data;
1805 if (!data->button->priv->model)
1806 /* button got destroyed */
1809 path = gtk_tree_row_reference_get_path (data->row_ref);
1811 /* Cancellable doesn't exist anymore in the model */
1814 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1815 gtk_tree_path_free (path);
1817 gtk_tree_model_get (data->button->priv->model, &iter,
1818 CANCELLABLE_COLUMN, &model_cancellable,
1820 if (cancellable != model_cancellable)
1823 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1824 CANCELLABLE_COLUMN, NULL,
1827 if (cancelled || error)
1830 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1834 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1835 ICON_COLUMN, pixbuf,
1837 g_object_unref (pixbuf);
1840 gtk_tree_model_get (data->button->priv->model, &iter,
1841 DISPLAY_NAME_COLUMN, &name,
1844 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1845 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1850 g_object_unref (data->button);
1851 gtk_tree_row_reference_free (data->row_ref);
1854 if (model_cancellable)
1855 g_object_unref (model_cancellable);
1859 model_add_special (GtkFileChooserButton *button)
1861 const gchar *homedir;
1862 const gchar *desktopdir;
1863 GtkListStore *store;
1868 store = GTK_LIST_STORE (button->priv->model);
1869 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1871 homedir = g_get_home_dir ();
1875 GtkTreePath *tree_path;
1876 GCancellable *cancellable;
1877 struct ChangeIconThemeData *info;
1879 file = g_file_new_for_path (homedir);
1880 gtk_list_store_insert (store, &iter, pos);
1883 info = g_new0 (struct ChangeIconThemeData, 1);
1884 info->button = g_object_ref (button);
1885 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1886 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1888 gtk_tree_path_free (tree_path);
1890 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1891 "standard::icon,standard::display-name",
1892 model_add_special_get_info_cb, info);
1894 gtk_list_store_set (store, &iter,
1896 DISPLAY_NAME_COLUMN, NULL,
1897 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1899 IS_FOLDER_COLUMN, TRUE,
1900 CANCELLABLE_COLUMN, cancellable,
1903 button->priv->n_special++;
1906 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1908 /* "To disable a directory, point it to the homedir."
1909 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1911 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1913 GtkTreePath *tree_path;
1914 GCancellable *cancellable;
1915 struct ChangeIconThemeData *info;
1917 file = g_file_new_for_path (desktopdir);
1918 gtk_list_store_insert (store, &iter, pos);
1921 info = g_new0 (struct ChangeIconThemeData, 1);
1922 info->button = g_object_ref (button);
1923 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1924 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1926 gtk_tree_path_free (tree_path);
1928 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1929 "standard::icon,standard::display-name",
1930 model_add_special_get_info_cb, info);
1932 gtk_list_store_set (store, &iter,
1933 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1935 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1937 IS_FOLDER_COLUMN, TRUE,
1938 CANCELLABLE_COLUMN, cancellable,
1941 button->priv->n_special++;
1946 model_add_volumes (GtkFileChooserButton *button,
1949 GtkListStore *store;
1951 gboolean local_only;
1957 store = GTK_LIST_STORE (button->priv->model);
1958 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1959 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1961 for (l = volumes; l; l = l->next)
1963 GtkFileSystemVolume *volume;
1966 gchar *display_name;
1972 if (_gtk_file_system_volume_is_mounted (volume))
1976 base_file = _gtk_file_system_volume_get_root (volume);
1977 if (base_file != NULL)
1979 if (!_gtk_file_has_native_path (base_file))
1981 g_object_unref (base_file);
1985 g_object_unref (base_file);
1990 pixbuf = _gtk_file_system_volume_render_icon (volume,
1991 GTK_WIDGET (button),
1992 button->priv->icon_size,
1994 display_name = _gtk_file_system_volume_get_display_name (volume);
1996 gtk_list_store_insert (store, &iter, pos);
1997 gtk_list_store_set (store, &iter,
1998 ICON_COLUMN, pixbuf,
1999 DISPLAY_NAME_COLUMN, display_name,
2000 TYPE_COLUMN, ROW_TYPE_VOLUME,
2001 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
2002 IS_FOLDER_COLUMN, TRUE,
2006 g_object_unref (pixbuf);
2007 g_free (display_name);
2009 button->priv->n_volumes++;
2014 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
2017 model_add_bookmarks (GtkFileChooserButton *button,
2020 GtkListStore *store;
2023 gboolean local_only;
2029 store = GTK_LIST_STORE (button->priv->model);
2030 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
2031 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
2033 for (l = bookmarks; l; l = l->next)
2039 if (_gtk_file_has_native_path (file))
2041 gtk_list_store_insert (store, &iter, pos);
2042 gtk_list_store_set (store, &iter,
2044 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2045 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2046 DATA_COLUMN, g_object_ref (file),
2047 IS_FOLDER_COLUMN, FALSE,
2049 set_info_for_file_at_iter (button, file, &iter);
2054 GtkIconTheme *icon_theme;
2060 /* Don't call get_info for remote paths to avoid latency and
2062 * If we switch to a better bookmarks file format (XBEL), we
2063 * should use mime info to get a better icon.
2065 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2067 label = _gtk_file_chooser_label_for_file (file);
2069 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2070 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2071 button->priv->icon_size, 0, NULL);
2073 gtk_list_store_insert (store, &iter, pos);
2074 gtk_list_store_set (store, &iter,
2075 ICON_COLUMN, pixbuf,
2076 DISPLAY_NAME_COLUMN, label,
2077 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
2078 DATA_COLUMN, g_object_ref (file),
2079 IS_FOLDER_COLUMN, TRUE,
2083 g_object_unref (pixbuf);
2086 button->priv->n_bookmarks++;
2090 if (button->priv->n_bookmarks > 0 &&
2091 !button->priv->has_bookmark_separator)
2093 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2095 gtk_list_store_insert (store, &iter, pos);
2096 gtk_list_store_set (store, &iter,
2098 DISPLAY_NAME_COLUMN, NULL,
2099 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2101 IS_FOLDER_COLUMN, FALSE,
2103 button->priv->has_bookmark_separator = TRUE;
2108 model_update_current_folder (GtkFileChooserButton *button,
2111 GtkListStore *store;
2118 store = GTK_LIST_STORE (button->priv->model);
2120 if (!button->priv->has_current_folder_separator)
2122 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
2123 gtk_list_store_insert (store, &iter, pos);
2124 gtk_list_store_set (store, &iter,
2126 DISPLAY_NAME_COLUMN, NULL,
2127 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
2129 IS_FOLDER_COLUMN, FALSE,
2131 button->priv->has_current_folder_separator = TRUE;
2134 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2135 if (!button->priv->has_current_folder)
2137 gtk_list_store_insert (store, &iter, pos);
2138 button->priv->has_current_folder = TRUE;
2142 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2143 model_free_row_data (button, &iter);
2146 if (g_file_is_native (file))
2148 gtk_list_store_set (store, &iter,
2150 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2151 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2152 DATA_COLUMN, g_object_ref (file),
2153 IS_FOLDER_COLUMN, FALSE,
2155 set_info_for_file_at_iter (button, file, &iter);
2160 GtkIconTheme *icon_theme;
2163 /* Don't call get_info for remote paths to avoid latency and
2165 * If we switch to a better bookmarks file format (XBEL), we
2166 * should use mime info to get a better icon.
2168 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2170 label = _gtk_file_chooser_label_for_file (file);
2172 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2174 if (g_file_is_native (file))
2175 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2176 button->priv->icon_size, 0, NULL);
2178 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2179 button->priv->icon_size, 0, NULL);
2181 gtk_list_store_set (store, &iter,
2182 ICON_COLUMN, pixbuf,
2183 DISPLAY_NAME_COLUMN, label,
2184 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2185 DATA_COLUMN, g_object_ref (file),
2186 IS_FOLDER_COLUMN, TRUE,
2190 g_object_unref (pixbuf);
2195 model_add_other (GtkFileChooserButton *button)
2197 GtkListStore *store;
2201 store = GTK_LIST_STORE (button->priv->model);
2202 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2204 gtk_list_store_insert (store, &iter, pos);
2205 gtk_list_store_set (store, &iter,
2207 DISPLAY_NAME_COLUMN, NULL,
2208 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2210 IS_FOLDER_COLUMN, FALSE,
2212 button->priv->has_other_separator = TRUE;
2215 gtk_list_store_insert (store, &iter, pos);
2216 gtk_list_store_set (store, &iter,
2218 DISPLAY_NAME_COLUMN, _("Other…"),
2219 TYPE_COLUMN, ROW_TYPE_OTHER,
2221 IS_FOLDER_COLUMN, FALSE,
2226 model_add_empty_selection (GtkFileChooserButton *button)
2228 GtkListStore *store;
2232 store = GTK_LIST_STORE (button->priv->model);
2233 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2235 gtk_list_store_insert (store, &iter, pos);
2236 gtk_list_store_set (store, &iter,
2238 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2239 TYPE_COLUMN, ROW_TYPE_EMPTY_SELECTION,
2241 IS_FOLDER_COLUMN, FALSE,
2246 model_remove_rows (GtkFileChooserButton *button,
2250 GtkListStore *store;
2255 store = GTK_LIST_STORE (button->priv->model);
2261 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2262 g_assert_not_reached ();
2264 model_free_row_data (button, &iter);
2265 gtk_list_store_remove (store, &iter);
2273 test_if_file_is_visible (GtkFileSystem *fs,
2275 gboolean local_only,
2281 if (local_only && !_gtk_file_has_native_path (file))
2291 filter_model_visible_func (GtkTreeModel *model,
2295 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2296 GtkFileChooserButtonPrivate *priv = button->priv;
2299 gboolean local_only, retval, is_folder;
2301 type = ROW_TYPE_INVALID;
2303 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2305 gtk_tree_model_get (model, iter,
2308 IS_FOLDER_COLUMN, &is_folder,
2313 case ROW_TYPE_CURRENT_FOLDER:
2316 case ROW_TYPE_SPECIAL:
2317 case ROW_TYPE_SHORTCUT:
2318 case ROW_TYPE_BOOKMARK:
2319 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2321 case ROW_TYPE_VOLUME:
2326 if (_gtk_file_system_volume_is_mounted (data))
2330 base_file = _gtk_file_system_volume_get_root (data);
2334 if (!_gtk_file_has_native_path (base_file))
2336 g_object_unref (base_file);
2354 name_cell_data_func (GtkCellLayout *layout,
2355 GtkCellRenderer *cell,
2356 GtkTreeModel *model,
2363 gtk_tree_model_get (model, iter,
2367 if (type == ROW_TYPE_CURRENT_FOLDER)
2368 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2370 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2374 combo_box_row_separator_func (GtkTreeModel *model,
2378 gchar type = ROW_TYPE_INVALID;
2380 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2382 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2383 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2384 type == ROW_TYPE_OTHER_SEPARATOR);
2388 update_combo_box (GtkFileChooserButton *button)
2390 GtkFileChooserButtonPrivate *priv = button->priv;
2395 file = get_selected_file (button);
2399 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2406 type = ROW_TYPE_INVALID;
2409 gtk_tree_model_get (priv->filter_model, &iter,
2416 case ROW_TYPE_SPECIAL:
2417 case ROW_TYPE_SHORTCUT:
2418 case ROW_TYPE_BOOKMARK:
2419 case ROW_TYPE_CURRENT_FOLDER:
2420 row_found = (file && g_file_equal (data, file));
2422 case ROW_TYPE_VOLUME:
2426 base_file = _gtk_file_system_volume_get_root (data);
2429 row_found = (file && g_file_equal (base_file, file));
2430 g_object_unref (base_file);
2441 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2442 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2444 g_signal_handler_unblock (priv->combo_box,
2445 priv->combo_box_changed_id);
2448 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2453 GtkTreeIter filter_iter;
2455 /* If it hasn't been found already, update & select the current-folder row. */
2458 model_update_current_folder (button, file);
2459 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2461 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2465 /* No selection; switch to that row */
2467 pos = model_get_type_position (button, ROW_TYPE_EMPTY_SELECTION);
2470 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2471 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2472 &filter_iter, &iter);
2474 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2475 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2476 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2480 g_object_unref (file);
2485 update_label_get_info_cb (GCancellable *cancellable,
2487 const GError *error,
2490 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2492 GtkFileChooserButton *button = data;
2493 GtkFileChooserButtonPrivate *priv = button->priv;
2495 if (cancellable != priv->update_button_cancellable)
2498 priv->update_button_cancellable = NULL;
2500 if (cancelled || error)
2503 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2505 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2508 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2510 priv->icon_size, 0, NULL);
2512 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2514 g_object_unref (pixbuf);
2517 g_object_unref (button);
2518 g_object_unref (cancellable);
2522 update_label_and_image (GtkFileChooserButton *button)
2524 GtkFileChooserButtonPrivate *priv = button->priv;
2528 file = get_selected_file (button);
2532 if (priv->update_button_cancellable)
2534 g_cancellable_cancel (priv->update_button_cancellable);
2535 priv->update_button_cancellable = NULL;
2540 GtkFileSystemVolume *volume = NULL;
2542 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2547 base_file = _gtk_file_system_volume_get_root (volume);
2548 if (base_file && g_file_equal (base_file, file))
2552 label_text = _gtk_file_system_volume_get_display_name (volume);
2553 pixbuf = _gtk_file_system_volume_render_icon (volume,
2554 GTK_WIDGET (button),
2557 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2559 g_object_unref (pixbuf);
2563 g_object_unref (base_file);
2565 _gtk_file_system_volume_unref (volume);
2571 if (g_file_is_native (file))
2573 priv->update_button_cancellable =
2574 _gtk_file_system_get_info (priv->fs, file,
2575 "standard::icon,standard::display-name",
2576 update_label_get_info_cb,
2577 g_object_ref (button));
2583 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2584 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2586 priv->icon_size, 0, NULL);
2587 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2589 g_object_unref (pixbuf);
2595 g_object_unref (file);
2599 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2600 g_free (label_text);
2604 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2605 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2610 /* ************************ *
2611 * Child Object Callbacks *
2612 * ************************ */
2616 fs_volumes_changed_cb (GtkFileSystem *fs,
2619 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2620 GtkFileChooserButtonPrivate *priv = button->priv;
2623 model_remove_rows (user_data,
2624 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2627 priv->n_volumes = 0;
2629 volumes = _gtk_file_system_list_volumes (fs);
2630 model_add_volumes (user_data, volumes);
2631 g_slist_free (volumes);
2633 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2635 update_label_and_image (user_data);
2636 update_combo_box (user_data);
2640 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2643 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2644 GtkFileChooserButtonPrivate *priv = button->priv;
2647 bookmarks = _gtk_file_system_list_bookmarks (fs);
2648 model_remove_rows (user_data,
2649 model_get_type_position (user_data,
2650 ROW_TYPE_BOOKMARK_SEPARATOR),
2651 (priv->n_bookmarks + priv->has_bookmark_separator));
2652 priv->has_bookmark_separator = FALSE;
2653 priv->n_bookmarks = 0;
2654 model_add_bookmarks (user_data, bookmarks);
2655 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2656 g_slist_free (bookmarks);
2658 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2660 update_label_and_image (user_data);
2661 update_combo_box (user_data);
2665 save_inactive_state (GtkFileChooserButton *button)
2667 GtkFileChooserButtonPrivate *priv = button->priv;
2669 if (priv->current_folder_while_inactive)
2670 g_object_unref (priv->current_folder_while_inactive);
2672 if (priv->selection_while_inactive)
2673 g_object_unref (priv->selection_while_inactive);
2675 priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->dialog));
2676 priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2680 restore_inactive_state (GtkFileChooserButton *button)
2682 GtkFileChooserButtonPrivate *priv = button->priv;
2684 if (priv->current_folder_while_inactive)
2685 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog), priv->current_folder_while_inactive, NULL);
2687 if (priv->selection_while_inactive)
2688 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2690 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2695 open_dialog (GtkFileChooserButton *button)
2697 GtkFileChooserButtonPrivate *priv = button->priv;
2699 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2701 if (!gtk_widget_get_visible (priv->dialog))
2703 GtkWidget *toplevel;
2705 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2707 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2709 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2710 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2711 GTK_WINDOW (toplevel));
2713 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2714 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2720 g_signal_handler_block (priv->dialog,
2721 priv->dialog_folder_changed_id);
2722 g_signal_handler_block (priv->dialog,
2723 priv->dialog_file_activated_id);
2724 g_signal_handler_block (priv->dialog,
2725 priv->dialog_selection_changed_id);
2727 restore_inactive_state (button);
2729 priv->active = TRUE;
2732 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2733 gtk_window_present (GTK_WINDOW (priv->dialog));
2738 combo_box_changed_cb (GtkComboBox *combo_box,
2743 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2745 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2746 GtkFileChooserButtonPrivate *priv = button->priv;
2750 type = ROW_TYPE_INVALID;
2753 gtk_tree_model_get (priv->filter_model, &iter,
2760 case ROW_TYPE_SPECIAL:
2761 case ROW_TYPE_SHORTCUT:
2762 case ROW_TYPE_BOOKMARK:
2763 case ROW_TYPE_CURRENT_FOLDER:
2764 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2766 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2769 case ROW_TYPE_VOLUME:
2773 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2774 base_file = _gtk_file_system_volume_get_root (data);
2777 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2779 g_object_unref (base_file);
2783 case ROW_TYPE_OTHER:
2784 open_dialog (user_data);
2794 button_clicked_cb (GtkButton *real_button,
2797 open_dialog (user_data);
2802 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2805 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2807 g_signal_emit_by_name (button, "current-folder-changed");
2811 dialog_file_activated_cb (GtkFileChooser *dialog,
2814 g_signal_emit_by_name (user_data, "file-activated");
2818 dialog_selection_changed_cb (GtkFileChooser *dialog,
2821 update_label_and_image (user_data);
2822 update_combo_box (user_data);
2823 g_signal_emit_by_name (user_data, "selection-changed");
2827 dialog_update_preview_cb (GtkFileChooser *dialog,
2830 g_signal_emit_by_name (user_data, "update-preview");
2834 dialog_notify_cb (GObject *dialog,
2840 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2841 GTK_TYPE_FILE_CHOOSER);
2842 if (g_object_interface_find_property (iface, pspec->name))
2843 g_object_notify (user_data, pspec->name);
2845 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2847 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2848 GtkFileChooserButtonPrivate *priv = button->priv;
2850 if (priv->has_current_folder)
2856 pos = model_get_type_position (user_data,
2857 ROW_TYPE_CURRENT_FOLDER);
2858 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2861 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2863 /* If the path isn't local but we're in local-only mode now, remove
2864 * the custom-folder row */
2865 if (data && _gtk_file_has_native_path (G_FILE (data)) &&
2866 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2869 model_remove_rows (user_data, pos, 2);
2873 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2874 update_combo_box (user_data);
2879 dialog_delete_event_cb (GtkWidget *dialog,
2883 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2889 dialog_response_cb (GtkDialog *dialog,
2893 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2894 GtkFileChooserButtonPrivate *priv = button->priv;
2896 if (response == GTK_RESPONSE_ACCEPT ||
2897 response == GTK_RESPONSE_OK)
2899 save_inactive_state (button);
2901 g_signal_emit_by_name (button, "current-folder-changed");
2902 g_signal_emit_by_name (button, "selection-changed");
2904 update_label_and_image (button);
2905 update_combo_box (button);
2909 restore_inactive_state (button);
2914 g_signal_handler_unblock (priv->dialog,
2915 priv->dialog_folder_changed_id);
2916 g_signal_handler_unblock (priv->dialog,
2917 priv->dialog_file_activated_id);
2918 g_signal_handler_unblock (priv->dialog,
2919 priv->dialog_selection_changed_id);
2920 priv->active = FALSE;
2923 gtk_widget_set_sensitive (priv->combo_box, TRUE);
2924 gtk_widget_hide (priv->dialog);
2926 if (response == GTK_RESPONSE_ACCEPT ||
2927 response == GTK_RESPONSE_OK)
2928 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
2932 /* ************************************************************************** *
2934 * ************************************************************************** */
2937 * gtk_file_chooser_button_new:
2938 * @title: the title of the browse dialog.
2939 * @action: the open mode for the widget.
2941 * Creates a new file-selecting button widget.
2943 * Returns: a new button widget.
2948 gtk_file_chooser_button_new (const gchar *title,
2949 GtkFileChooserAction action)
2951 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2952 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2954 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2956 "title", (title ? title : _(DEFAULT_TITLE)),
2961 * gtk_file_chooser_button_new_with_dialog:
2962 * @dialog: the widget to use as dialog
2964 * Creates a #GtkFileChooserButton widget which uses @dialog as its
2965 * file-picking window.
2967 * Note that @dialog must be a #GtkDialog (or subclass) which
2968 * implements the #GtkFileChooser interface and must not have
2969 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2971 * Also note that the dialog needs to have its confirmative button
2972 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2973 * order for the button to take over the file selected in the dialog.
2975 * Returns: a new button widget.
2980 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2982 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2984 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2990 * gtk_file_chooser_button_set_title:
2991 * @button: the button widget to modify.
2992 * @title: the new browse dialog title.
2994 * Modifies the @title of the browse dialog used by @button.
2999 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
3002 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3004 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
3005 g_object_notify (G_OBJECT (button), "title");
3009 * gtk_file_chooser_button_get_title:
3010 * @button: the button widget to examine.
3012 * Retrieves the title of the browse dialog used by @button. The returned value
3013 * should not be modified or freed.
3015 * Returns: a pointer to the browse dialog's title.
3020 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
3022 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
3024 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
3028 * gtk_file_chooser_button_get_width_chars:
3029 * @button: the button widget to examine.
3031 * Retrieves the width in characters of the @button widget's entry and/or label.
3033 * Returns: an integer width (in characters) that the button will use to size itself.
3038 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
3040 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
3042 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
3046 * gtk_file_chooser_button_set_width_chars:
3047 * @button: the button widget to examine.
3048 * @n_chars: the new width, in characters.
3050 * Sets the width (in characters) that @button will use to @n_chars.
3055 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
3058 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3060 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
3061 g_object_notify (G_OBJECT (button), "width-chars");
3065 * gtk_file_chooser_button_set_focus_on_click:
3066 * @button: a #GtkFileChooserButton
3067 * @focus_on_click: whether the button grabs focus when clicked with the mouse
3069 * Sets whether the button will grab focus when it is clicked with the mouse.
3070 * Making mouse clicks not grab focus is useful in places like toolbars where
3071 * you don't want the keyboard focus removed from the main area of the
3077 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
3078 gboolean focus_on_click)
3080 GtkFileChooserButtonPrivate *priv;
3082 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
3084 priv = button->priv;
3086 focus_on_click = focus_on_click != FALSE;
3088 if (priv->focus_on_click != focus_on_click)
3090 priv->focus_on_click = focus_on_click;
3091 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
3092 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
3094 g_object_notify (G_OBJECT (button), "focus-on-click");
3099 * gtk_file_chooser_button_get_focus_on_click:
3100 * @button: a #GtkFileChooserButton
3102 * Returns whether the button grabs focus when it is clicked with the mouse.
3103 * See gtk_file_chooser_button_set_focus_on_click().
3105 * Return value: %TRUE if the button grabs focus when it is clicked with
3111 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
3113 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
3115 return button->priv->focus_on_click;