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, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
32 #include "gtkbutton.h"
33 #include "gtkcelllayout.h"
34 #include "gtkcellrenderertext.h"
35 #include "gtkcellrendererpixbuf.h"
36 #include "gtkcombobox.h"
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
42 #include "gtkliststore.h"
44 #include "gtktreemodelfilter.h"
45 #include "gtkseparator.h"
46 #include "gtkfilechooserdialog.h"
47 #include "gtkfilechooserprivate.h"
48 #include "gtkfilechooserutils.h"
49 #include "gtkmarshalers.h"
51 #include "gtkfilechooserbutton.h"
53 #include "gtkorientable.h"
55 #include "gtktypebuiltins.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
61 * SECTION:gtkfilechooserbutton
62 * @Short_description: A button to launch a file selection dialog
63 * @Title: GtkFileChooserButton
64 * @See_also:#GtkFileChooserDialog
66 * The #GtkFileChooserButton is a widget that lets the user select a
67 * file. It implements the #GtkFileChooser interface. Visually, it is a
68 * file name with a button to bring up a #GtkFileChooserDialog.
69 * The user can then use that dialog to change the file associated with
70 * that button. This widget does not support setting the
71 * #GtkFileChooser:select-multiple property to %TRUE.
74 * <title>Create a button to let the user select a file in /etc</title>
79 * button = gtk_file_chooser_button_new (_("Select a file"),
80 * GTK_FILE_CHOOSER_ACTION_OPEN);
81 * gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
87 * The #GtkFileChooserButton supports the #GtkFileChooserAction<!-- -->s
88 * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
91 * The #GtkFileChooserButton will ellipsize the label,
92 * and thus will thus request little horizontal space. To give the button
93 * more space, you should call gtk_widget_get_preferred_size(),
94 * gtk_file_chooser_button_set_width_chars(), or pack the button in
95 * such a way that other interface elements give space to the widget.
100 /* **************** *
102 * **************** */
104 #define DEFAULT_TITLE N_("Select a File")
105 #define DESKTOP_DISPLAY_NAME N_("Desktop")
106 #define FALLBACK_DISPLAY_NAME N_("(None)")
107 #define FALLBACK_ICON_NAME "stock_unknown"
108 #define FALLBACK_ICON_SIZE 16
111 /* ********************** *
112 * Private Enumerations *
113 * ********************** */
133 /* TreeModel Columns */
145 /* TreeModel Row Types */
151 ROW_TYPE_BOOKMARK_SEPARATOR,
153 ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
154 ROW_TYPE_CURRENT_FOLDER,
155 ROW_TYPE_OTHER_SEPARATOR,
158 ROW_TYPE_INVALID = -1
163 /* ******************** *
164 * Private Structures *
165 * ******************** */
167 struct _GtkFileChooserButtonPrivate
173 GtkWidget *combo_box;
174 GtkCellRenderer *icon_cell;
175 GtkCellRenderer *name_cell;
178 GtkTreeModel *filter_model;
183 gulong combo_box_changed_id;
184 gulong dialog_file_activated_id;
185 gulong dialog_folder_changed_id;
186 gulong dialog_selection_changed_id;
187 gulong fs_volumes_changed_id;
188 gulong fs_bookmarks_changed_id;
190 GCancellable *dnd_select_folder_cancellable;
191 GCancellable *update_button_cancellable;
192 GSList *change_icon_theme_cancellables;
200 guint has_bookmark_separator : 1;
201 guint has_current_folder_separator : 1;
202 guint has_current_folder : 1;
203 guint has_other_separator : 1;
205 /* Used for hiding/showing the dialog when the button is hidden */
208 /* Used to track whether we need to set a default current folder on ::map() */
209 guint folder_has_been_set : 1;
211 guint focus_on_click : 1;
226 /* ********************* *
227 * Function Prototypes *
228 * ********************* */
230 /* GtkFileChooserIface Functions */
231 static void gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
232 static gboolean gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
235 static gboolean gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
239 /* GObject Functions */
240 static GObject *gtk_file_chooser_button_constructor (GType type,
242 GObjectConstructParam *params);
243 static void gtk_file_chooser_button_set_property (GObject *object,
247 static void gtk_file_chooser_button_get_property (GObject *object,
251 static void gtk_file_chooser_button_finalize (GObject *object);
253 /* GtkWidget Functions */
254 static void gtk_file_chooser_button_destroy (GtkWidget *widget);
255 static void gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
256 GdkDragContext *context,
259 GtkSelectionData *data,
262 static void gtk_file_chooser_button_show_all (GtkWidget *widget);
263 static void gtk_file_chooser_button_show (GtkWidget *widget);
264 static void gtk_file_chooser_button_hide (GtkWidget *widget);
265 static void gtk_file_chooser_button_map (GtkWidget *widget);
266 static gboolean gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
267 gboolean group_cycling);
268 static void gtk_file_chooser_button_style_updated (GtkWidget *widget);
269 static void gtk_file_chooser_button_screen_changed (GtkWidget *widget,
270 GdkScreen *old_screen);
272 /* Utility Functions */
273 static GtkIconTheme *get_icon_theme (GtkWidget *widget);
274 static void set_info_for_file_at_iter (GtkFileChooserButton *fs,
278 static gint model_get_type_position (GtkFileChooserButton *button,
280 static void model_free_row_data (GtkFileChooserButton *button,
282 static inline void model_add_special (GtkFileChooserButton *button);
283 static inline void model_add_other (GtkFileChooserButton *button);
284 static void model_add_volumes (GtkFileChooserButton *button,
286 static void model_add_bookmarks (GtkFileChooserButton *button,
288 static void model_update_current_folder (GtkFileChooserButton *button,
290 static void model_remove_rows (GtkFileChooserButton *button,
294 static gboolean filter_model_visible_func (GtkTreeModel *model,
298 static gboolean combo_box_row_separator_func (GtkTreeModel *model,
301 static void name_cell_data_func (GtkCellLayout *layout,
302 GtkCellRenderer *cell,
306 static void open_dialog (GtkFileChooserButton *button);
307 static void update_combo_box (GtkFileChooserButton *button);
308 static void update_label_and_image (GtkFileChooserButton *button);
310 /* Child Object Callbacks */
311 static void fs_volumes_changed_cb (GtkFileSystem *fs,
313 static void fs_bookmarks_changed_cb (GtkFileSystem *fs,
316 static void combo_box_changed_cb (GtkComboBox *combo_box,
319 static void button_clicked_cb (GtkButton *real_button,
322 static void dialog_update_preview_cb (GtkFileChooser *dialog,
324 static void dialog_selection_changed_cb (GtkFileChooser *dialog,
326 static void dialog_file_activated_cb (GtkFileChooser *dialog,
328 static void dialog_current_folder_changed_cb (GtkFileChooser *dialog,
330 static void dialog_notify_cb (GObject *dialog,
333 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
336 static void dialog_response_cb (GtkDialog *dialog,
340 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
342 /* ******************* *
343 * GType Declaration *
344 * ******************* */
346 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
347 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
351 /* ***************** *
353 * ***************** */
356 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
358 GObjectClass *gobject_class;
359 GtkWidgetClass *widget_class;
361 gobject_class = G_OBJECT_CLASS (class);
362 widget_class = GTK_WIDGET_CLASS (class);
364 gobject_class->constructor = gtk_file_chooser_button_constructor;
365 gobject_class->set_property = gtk_file_chooser_button_set_property;
366 gobject_class->get_property = gtk_file_chooser_button_get_property;
367 gobject_class->finalize = gtk_file_chooser_button_finalize;
369 widget_class->destroy = gtk_file_chooser_button_destroy;
370 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
371 widget_class->show_all = gtk_file_chooser_button_show_all;
372 widget_class->show = gtk_file_chooser_button_show;
373 widget_class->hide = gtk_file_chooser_button_hide;
374 widget_class->map = gtk_file_chooser_button_map;
375 widget_class->style_updated = gtk_file_chooser_button_style_updated;
376 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
377 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
380 * GtkFileChooserButton::file-set:
381 * @widget: the object which received the signal.
383 * The ::file-set signal is emitted when the user selects a file.
385 * Note that this signal is only emitted when the <emphasis>user</emphasis>
390 file_chooser_button_signals[FILE_SET] =
391 g_signal_new (I_("file-set"),
392 G_TYPE_FROM_CLASS (gobject_class),
394 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
396 _gtk_marshal_VOID__VOID,
400 * GtkFileChooserButton:dialog:
402 * Instance of the #GtkFileChooserDialog associated with the button.
406 g_object_class_install_property (gobject_class, PROP_DIALOG,
407 g_param_spec_object ("dialog",
409 P_("The file chooser dialog to use."),
410 GTK_TYPE_FILE_CHOOSER,
411 (GTK_PARAM_WRITABLE |
412 G_PARAM_CONSTRUCT_ONLY)));
415 * GtkFileChooserButton:focus-on-click:
417 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
422 g_object_class_install_property (gobject_class,
424 g_param_spec_boolean ("focus-on-click",
425 P_("Focus on click"),
426 P_("Whether the button grabs focus when it is clicked with the mouse"),
428 GTK_PARAM_READWRITE));
431 * GtkFileChooserButton:title:
433 * Title to put on the #GtkFileChooserDialog associated with the button.
437 g_object_class_install_property (gobject_class, PROP_TITLE,
438 g_param_spec_string ("title",
440 P_("The title of the file chooser dialog."),
442 GTK_PARAM_READWRITE));
445 * GtkFileChooserButton:width-chars:
447 * The width of the entry and label inside the button, in characters.
451 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
452 g_param_spec_int ("width-chars",
453 P_("Width In Characters"),
454 P_("The desired width of the button widget, in characters."),
456 GTK_PARAM_READWRITE));
458 _gtk_file_chooser_install_properties (gobject_class);
460 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
464 gtk_file_chooser_button_init (GtkFileChooserButton *button)
466 GtkFileChooserButtonPrivate *priv;
467 GtkWidget *box, *image, *sep;
468 GtkTargetList *target_list;
470 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
471 GTK_TYPE_FILE_CHOOSER_BUTTON,
472 GtkFileChooserButtonPrivate);
474 priv->icon_size = FALLBACK_ICON_SIZE;
475 priv->focus_on_click = TRUE;
477 gtk_widget_push_composite_child ();
480 priv->button = gtk_button_new ();
481 g_signal_connect (priv->button, "clicked",
482 G_CALLBACK (button_clicked_cb), button);
483 gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
484 gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
485 gtk_widget_show (priv->button);
487 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
488 gtk_container_add (GTK_CONTAINER (priv->button), box);
489 gtk_widget_show (box);
491 priv->image = gtk_image_new ();
492 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
493 gtk_widget_show (priv->image);
495 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
496 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
497 gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
498 gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
499 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
500 //gtk_container_add (GTK_CONTAINER (box), priv->label);
501 gtk_widget_show (priv->label);
503 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
504 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
505 gtk_widget_show (sep);
507 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
508 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
509 gtk_widget_show (image);
512 /* Keep in sync with columns enum, line 88 */
514 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
515 GDK_TYPE_PIXBUF, /* Icon */
516 G_TYPE_STRING, /* Display Name */
517 G_TYPE_CHAR, /* Row Type */
518 G_TYPE_POINTER /* Volume || Path */,
519 G_TYPE_BOOLEAN /* Is Folder? */,
520 G_TYPE_POINTER /* cancellable */));
522 priv->combo_box = gtk_combo_box_new ();
523 priv->combo_box_changed_id =
524 g_signal_connect (priv->combo_box, "changed",
525 G_CALLBACK (combo_box_changed_cb), button);
526 gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
527 gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
529 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
530 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
531 priv->icon_cell, FALSE);
532 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
533 priv->icon_cell, "pixbuf", ICON_COLUMN);
535 priv->name_cell = gtk_cell_renderer_text_new ();
536 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
537 priv->name_cell, TRUE);
538 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
539 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
540 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
541 priv->name_cell, name_cell_data_func,
544 gtk_widget_pop_composite_child ();
547 gtk_drag_dest_set (GTK_WIDGET (button),
548 (GTK_DEST_DEFAULT_ALL),
551 target_list = gtk_target_list_new (NULL, 0);
552 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
553 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
554 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
555 gtk_target_list_unref (target_list);
559 /* ******************************* *
560 * GtkFileChooserIface Functions *
561 * ******************************* */
563 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
565 _gtk_file_chooser_delegate_iface_init (iface);
567 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
568 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
572 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
576 GtkFileChooser *delegate;
579 delegate = g_object_get_qdata (G_OBJECT (chooser),
580 GTK_FILE_CHOOSER_DELEGATE_QUARK);
581 retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
585 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
586 GtkFileChooserButtonPrivate *priv = button->priv;
590 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
591 pos += priv->n_shortcuts;
593 gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
594 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
596 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
597 TYPE_COLUMN, ROW_TYPE_SHORTCUT,
598 DATA_COLUMN, g_object_ref (file),
599 IS_FOLDER_COLUMN, FALSE,
601 set_info_for_file_at_iter (button, file, &iter);
604 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
611 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
615 GtkFileChooser *delegate;
618 delegate = g_object_get_qdata (G_OBJECT (chooser),
619 GTK_FILE_CHOOSER_DELEGATE_QUARK);
621 retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
625 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
626 GtkFileChooserButtonPrivate *priv = button->priv;
631 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
632 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
638 gtk_tree_model_get (priv->model, &iter,
643 if (type == ROW_TYPE_SHORTCUT &&
644 data && g_file_equal (data, file))
646 model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
647 gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
649 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
650 update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
654 while (type == ROW_TYPE_SHORTCUT &&
655 gtk_tree_model_iter_next (priv->model, &iter));
662 /* ******************* *
663 * GObject Functions *
664 * ******************* */
667 gtk_file_chooser_button_constructor (GType type,
669 GObjectConstructParam *params)
672 GtkFileChooserButton *button;
673 GtkFileChooserButtonPrivate *priv;
675 char *current_folder;
677 object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
680 button = GTK_FILE_CHOOSER_BUTTON (object);
685 priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
686 GTK_FILE_CHOOSER_ACTION_OPEN,
693 gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
694 GTK_RESPONSE_ACCEPT);
695 gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
700 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
702 else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
704 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
707 current_folder = gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (priv->dialog));
708 if (current_folder != NULL)
710 priv->folder_has_been_set = TRUE;
711 g_free (current_folder);
714 g_signal_connect (priv->dialog, "delete-event",
715 G_CALLBACK (dialog_delete_event_cb), object);
716 g_signal_connect (priv->dialog, "response",
717 G_CALLBACK (dialog_response_cb), object);
719 /* This is used, instead of the standard delegate, to ensure that signals are only
720 * delegated when the OK button is pressed. */
721 g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
722 priv->dialog_folder_changed_id =
723 g_signal_connect (priv->dialog, "current-folder-changed",
724 G_CALLBACK (dialog_current_folder_changed_cb), object);
725 priv->dialog_file_activated_id =
726 g_signal_connect (priv->dialog, "file-activated",
727 G_CALLBACK (dialog_file_activated_cb), object);
728 priv->dialog_selection_changed_id =
729 g_signal_connect (priv->dialog, "selection-changed",
730 G_CALLBACK (dialog_selection_changed_cb), object);
731 g_signal_connect (priv->dialog, "update-preview",
732 G_CALLBACK (dialog_update_preview_cb), object);
733 g_signal_connect (priv->dialog, "notify",
734 G_CALLBACK (dialog_notify_cb), object);
735 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
736 (gpointer) (&priv->dialog));
739 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
741 model_add_special (button);
743 list = _gtk_file_system_list_volumes (priv->fs);
744 model_add_volumes (button, list);
747 list = _gtk_file_system_list_bookmarks (priv->fs);
748 model_add_bookmarks (button, list);
749 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
752 model_add_other (button);
754 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
755 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
756 filter_model_visible_func,
759 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
760 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
761 combo_box_row_separator_func,
764 /* set up the action for a user-provided dialog, this also updates
765 * the label, image and combobox
767 g_object_set (object,
768 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
771 priv->fs_volumes_changed_id =
772 g_signal_connect (priv->fs, "volumes-changed",
773 G_CALLBACK (fs_volumes_changed_cb), object);
774 priv->fs_bookmarks_changed_id =
775 g_signal_connect (priv->fs, "bookmarks-changed",
776 G_CALLBACK (fs_bookmarks_changed_cb), object);
782 gtk_file_chooser_button_set_property (GObject *object,
787 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
788 GtkFileChooserButtonPrivate *priv = button->priv;
794 priv->dialog = g_value_get_object (value);
796 case PROP_FOCUS_ON_CLICK:
797 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
799 case PROP_WIDTH_CHARS:
800 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
801 g_value_get_int (value));
803 case GTK_FILE_CHOOSER_PROP_ACTION:
804 switch (g_value_get_enum (value))
806 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
807 case GTK_FILE_CHOOSER_ACTION_SAVE:
812 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
813 eval = g_enum_get_value (eclass, g_value_get_enum (value));
814 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
815 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
817 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
822 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
823 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
824 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
826 switch (g_value_get_enum (value))
828 case GTK_FILE_CHOOSER_ACTION_OPEN:
829 gtk_widget_hide (priv->combo_box);
830 gtk_widget_show (priv->button);
832 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
833 gtk_widget_hide (priv->button);
834 gtk_widget_show (priv->combo_box);
837 g_assert_not_reached ();
843 case GTK_FILE_CHOOSER_PROP_FILTER:
844 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
845 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
846 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
847 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
848 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
849 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
850 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
851 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
854 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
855 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
856 fs_volumes_changed_cb (priv->fs, button);
857 fs_bookmarks_changed_cb (priv->fs, button);
860 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
861 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
862 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
865 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
871 gtk_file_chooser_button_get_property (GObject *object,
876 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
877 GtkFileChooserButtonPrivate *priv = button->priv;
881 case PROP_WIDTH_CHARS:
882 g_value_set_int (value,
883 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
885 case PROP_FOCUS_ON_CLICK:
886 g_value_set_boolean (value,
887 gtk_file_chooser_button_get_focus_on_click (button));
891 case GTK_FILE_CHOOSER_PROP_ACTION:
892 case GTK_FILE_CHOOSER_PROP_FILTER:
893 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
894 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
895 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
896 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
897 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
898 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
899 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
900 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
901 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
902 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
906 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
912 gtk_file_chooser_button_finalize (GObject *object)
914 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
915 GtkFileChooserButtonPrivate *priv = button->priv;
918 g_object_unref (priv->old_file);
920 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
923 /* ********************* *
924 * GtkWidget Functions *
925 * ********************* */
928 gtk_file_chooser_button_destroy (GtkWidget *widget)
930 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
931 GtkFileChooserButtonPrivate *priv = button->priv;
935 if (priv->dialog != NULL)
937 gtk_widget_destroy (priv->dialog);
941 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
943 model_free_row_data (button, &iter);
945 while (gtk_tree_model_iter_next (priv->model, &iter));
947 if (priv->dnd_select_folder_cancellable)
949 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
950 priv->dnd_select_folder_cancellable = NULL;
953 if (priv->update_button_cancellable)
955 g_cancellable_cancel (priv->update_button_cancellable);
956 priv->update_button_cancellable = NULL;
959 if (priv->change_icon_theme_cancellables)
961 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
963 GCancellable *cancellable = G_CANCELLABLE (l->data);
964 g_cancellable_cancel (cancellable);
966 g_slist_free (priv->change_icon_theme_cancellables);
967 priv->change_icon_theme_cancellables = NULL;
972 g_object_unref (priv->model);
976 if (priv->filter_model)
978 g_object_unref (priv->filter_model);
979 priv->filter_model = NULL;
984 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
985 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
986 g_object_unref (priv->fs);
990 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
993 struct DndSelectFolderData
995 GtkFileSystem *file_system;
996 GtkFileChooserButton *button;
997 GtkFileChooserAction action;
1005 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1007 const GError *error,
1010 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1011 struct DndSelectFolderData *data = user_data;
1013 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1015 g_object_unref (data->button);
1016 g_object_unref (data->file);
1017 g_strfreev (data->uris);
1020 g_object_unref (cancellable);
1024 data->button->priv->dnd_select_folder_cancellable = NULL;
1026 if (!cancelled && !error && info != NULL)
1030 is_folder = _gtk_file_info_consider_as_directory (info);
1033 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1034 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1035 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1039 data->selected = FALSE;
1041 if (data->selected || data->uris[++data->i] == NULL)
1043 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1045 g_object_unref (data->button);
1046 g_object_unref (data->file);
1047 g_strfreev (data->uris);
1050 g_object_unref (cancellable);
1055 g_object_unref (data->file);
1057 data->file = g_file_new_for_uri (data->uris[data->i]);
1059 data->button->priv->dnd_select_folder_cancellable =
1060 _gtk_file_system_get_info (data->file_system, data->file,
1062 dnd_select_folder_get_info_cb, user_data);
1064 g_object_unref (cancellable);
1068 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1069 GdkDragContext *context,
1072 GtkSelectionData *data,
1076 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1077 GtkFileChooserButtonPrivate *priv = button->priv;
1081 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1082 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1088 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1096 struct DndSelectFolderData *info;
1098 uris = gtk_selection_data_get_uris (data);
1103 info = g_new0 (struct DndSelectFolderData, 1);
1104 info->button = g_object_ref (button);
1107 info->selected = FALSE;
1108 info->file_system = priv->fs;
1109 g_object_get (priv->dialog, "action", &info->action, NULL);
1111 info->file = g_file_new_for_uri (info->uris[info->i]);
1113 if (priv->dnd_select_folder_cancellable)
1114 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1116 priv->dnd_select_folder_cancellable =
1117 _gtk_file_system_get_info (priv->fs, info->file,
1119 dnd_select_folder_get_info_cb, info);
1124 text = (char*) gtk_selection_data_get_text (data);
1125 file = g_file_new_for_uri (text);
1126 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1128 g_object_unref (file);
1130 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1137 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1141 gtk_file_chooser_button_show_all (GtkWidget *widget)
1143 gtk_widget_show (widget);
1147 gtk_file_chooser_button_show (GtkWidget *widget)
1149 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1150 GtkFileChooserButtonPrivate *priv = button->priv;
1152 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1153 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1156 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1160 gtk_file_chooser_button_hide (GtkWidget *widget)
1162 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1163 GtkFileChooserButtonPrivate *priv = button->priv;
1165 gtk_widget_hide (priv->dialog);
1167 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1168 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1172 gtk_file_chooser_button_map (GtkWidget *widget)
1174 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1175 GtkFileChooserButtonPrivate *priv = button->priv;
1177 if (!priv->folder_has_been_set)
1179 char *current_working_dir;
1181 current_working_dir = g_get_current_dir ();
1182 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), current_working_dir);
1183 g_free (current_working_dir);
1185 priv->folder_has_been_set = TRUE;
1188 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1192 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1193 gboolean group_cycling)
1195 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1196 GtkFileChooserButtonPrivate *priv = button->priv;
1198 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1200 case GTK_FILE_CHOOSER_ACTION_OPEN:
1201 gtk_widget_grab_focus (priv->button);
1203 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1204 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1207 g_assert_not_reached ();
1214 /* Changes the icons wherever it is needed */
1215 struct ChangeIconThemeData
1217 GtkFileChooserButton *button;
1218 GtkTreeRowReference *row_ref;
1222 change_icon_theme_get_info_cb (GCancellable *cancellable,
1224 const GError *error,
1227 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1229 struct ChangeIconThemeData *data = user_data;
1231 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1234 data->button->priv->change_icon_theme_cancellables =
1235 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1237 if (cancelled || error)
1240 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1248 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1250 path = gtk_tree_row_reference_get_path (data->row_ref);
1253 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1254 gtk_tree_path_free (path);
1256 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1257 ICON_COLUMN, pixbuf,
1260 g_object_set (data->button->priv->icon_cell,
1264 g_object_unref (pixbuf);
1268 g_object_unref (data->button);
1269 gtk_tree_row_reference_free (data->row_ref);
1272 g_object_unref (cancellable);
1276 change_icon_theme (GtkFileChooserButton *button)
1278 GtkFileChooserButtonPrivate *priv = button->priv;
1279 GtkSettings *settings;
1280 GtkIconTheme *theme;
1283 gint width = 0, height = 0;
1285 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1287 GCancellable *cancellable = G_CANCELLABLE (l->data);
1288 g_cancellable_cancel (cancellable);
1290 g_slist_free (button->priv->change_icon_theme_cancellables);
1291 button->priv->change_icon_theme_cancellables = NULL;
1293 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1295 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1297 priv->icon_size = MAX (width, height);
1299 priv->icon_size = FALLBACK_ICON_SIZE;
1301 update_label_and_image (button);
1303 gtk_tree_model_get_iter_first (priv->model, &iter);
1305 theme = get_icon_theme (GTK_WIDGET (button));
1313 type = ROW_TYPE_INVALID;
1314 gtk_tree_model_get (priv->model, &iter,
1321 case ROW_TYPE_SPECIAL:
1322 case ROW_TYPE_SHORTCUT:
1323 case ROW_TYPE_BOOKMARK:
1324 case ROW_TYPE_CURRENT_FOLDER:
1327 if (g_file_is_native (G_FILE (data)))
1330 GCancellable *cancellable;
1331 struct ChangeIconThemeData *info;
1333 info = g_new0 (struct ChangeIconThemeData, 1);
1334 info->button = g_object_ref (button);
1335 path = gtk_tree_model_get_path (priv->model, &iter);
1336 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1337 gtk_tree_path_free (path);
1340 _gtk_file_system_get_info (priv->fs, data,
1342 change_icon_theme_get_info_cb,
1344 button->priv->change_icon_theme_cancellables =
1345 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1349 /* Don't call get_info for remote paths to avoid latency and
1351 * If we switch to a better bookmarks file format (XBEL), we
1352 * should use mime info to get a better icon.
1354 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1355 priv->icon_size, 0, NULL);
1358 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1359 priv->icon_size, 0, NULL);
1361 case ROW_TYPE_VOLUME:
1363 pixbuf = _gtk_file_system_volume_render_icon (data,
1364 GTK_WIDGET (button),
1368 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1369 priv->icon_size, 0, NULL);
1377 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1379 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1380 ICON_COLUMN, pixbuf,
1384 g_object_unref (pixbuf);
1386 while (gtk_tree_model_iter_next (priv->model, &iter));
1388 g_object_set (button->priv->icon_cell,
1394 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1396 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1398 if (gtk_widget_has_screen (widget))
1399 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1403 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1404 GdkScreen *old_screen)
1406 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1407 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1410 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1414 /* ******************* *
1415 * Utility Functions *
1416 * ******************* */
1419 static GtkIconTheme *
1420 get_icon_theme (GtkWidget *widget)
1422 if (gtk_widget_has_screen (widget))
1423 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1425 return gtk_icon_theme_get_default ();
1429 struct SetDisplayNameData
1431 GtkFileChooserButton *button;
1433 GtkTreeRowReference *row_ref;
1437 set_info_get_info_cb (GCancellable *cancellable,
1439 const GError *error,
1440 gpointer callback_data)
1442 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1446 GCancellable *model_cancellable = NULL;
1447 struct SetDisplayNameData *data = callback_data;
1450 if (!data->button->priv->model)
1451 /* button got destroyed */
1454 path = gtk_tree_row_reference_get_path (data->row_ref);
1456 /* Cancellable doesn't exist anymore in the model */
1459 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1460 gtk_tree_path_free (path);
1462 /* Validate the cancellable */
1463 gtk_tree_model_get (data->button->priv->model, &iter,
1464 CANCELLABLE_COLUMN, &model_cancellable,
1466 if (cancellable != model_cancellable)
1469 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1470 CANCELLABLE_COLUMN, NULL,
1473 if (cancelled || error)
1474 /* There was an error, leave the fallback name in there */
1477 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1480 data->label = g_strdup (g_file_info_get_display_name (info));
1482 is_folder = _gtk_file_info_consider_as_directory (info);
1484 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1485 ICON_COLUMN, pixbuf,
1486 DISPLAY_NAME_COLUMN, data->label,
1487 IS_FOLDER_COLUMN, is_folder,
1491 g_object_unref (pixbuf);
1494 g_object_unref (data->button);
1495 g_free (data->label);
1496 gtk_tree_row_reference_free (data->row_ref);
1499 if (model_cancellable)
1500 g_object_unref (model_cancellable);
1504 set_info_for_file_at_iter (GtkFileChooserButton *button,
1508 struct SetDisplayNameData *data;
1509 GtkTreePath *tree_path;
1510 GCancellable *cancellable;
1512 data = g_new0 (struct SetDisplayNameData, 1);
1513 data->button = g_object_ref (button);
1514 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1516 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1517 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1518 gtk_tree_path_free (tree_path);
1520 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1521 "standard::type,standard::icon,standard::display-name",
1522 set_info_get_info_cb, data);
1524 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1525 CANCELLABLE_COLUMN, cancellable,
1529 /* Shortcuts Model */
1531 model_get_type_position (GtkFileChooserButton *button,
1536 if (row_type == ROW_TYPE_SPECIAL)
1539 retval += button->priv->n_special;
1541 if (row_type == ROW_TYPE_VOLUME)
1544 retval += button->priv->n_volumes;
1546 if (row_type == ROW_TYPE_SHORTCUT)
1549 retval += button->priv->n_shortcuts;
1551 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1554 retval += button->priv->has_bookmark_separator;
1556 if (row_type == ROW_TYPE_BOOKMARK)
1559 retval += button->priv->n_bookmarks;
1561 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1564 retval += button->priv->has_current_folder_separator;
1566 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1569 retval += button->priv->has_current_folder;
1571 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1574 retval += button->priv->has_other_separator;
1576 if (row_type == ROW_TYPE_OTHER)
1579 g_assert_not_reached ();
1584 model_free_row_data (GtkFileChooserButton *button,
1589 GCancellable *cancellable;
1591 gtk_tree_model_get (button->priv->model, iter,
1594 CANCELLABLE_COLUMN, &cancellable,
1599 g_cancellable_cancel (cancellable);
1600 g_object_unref (cancellable);
1605 case ROW_TYPE_SPECIAL:
1606 case ROW_TYPE_SHORTCUT:
1607 case ROW_TYPE_BOOKMARK:
1608 case ROW_TYPE_CURRENT_FOLDER:
1609 g_object_unref (data);
1611 case ROW_TYPE_VOLUME:
1612 _gtk_file_system_volume_unref (data);
1620 model_add_special_get_info_cb (GCancellable *cancellable,
1622 const GError *error,
1625 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1629 GCancellable *model_cancellable = NULL;
1630 struct ChangeIconThemeData *data = user_data;
1633 if (!data->button->priv->model)
1634 /* button got destroyed */
1637 path = gtk_tree_row_reference_get_path (data->row_ref);
1639 /* Cancellable doesn't exist anymore in the model */
1642 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1643 gtk_tree_path_free (path);
1645 gtk_tree_model_get (data->button->priv->model, &iter,
1646 CANCELLABLE_COLUMN, &model_cancellable,
1648 if (cancellable != model_cancellable)
1651 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1652 CANCELLABLE_COLUMN, NULL,
1655 if (cancelled || error)
1658 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1662 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1663 ICON_COLUMN, pixbuf,
1665 g_object_unref (pixbuf);
1668 gtk_tree_model_get (data->button->priv->model, &iter,
1669 DISPLAY_NAME_COLUMN, &name,
1672 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1673 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1678 g_object_unref (data->button);
1679 gtk_tree_row_reference_free (data->row_ref);
1682 if (model_cancellable)
1683 g_object_unref (model_cancellable);
1687 model_add_special (GtkFileChooserButton *button)
1689 const gchar *homedir;
1690 const gchar *desktopdir;
1691 GtkListStore *store;
1696 store = GTK_LIST_STORE (button->priv->model);
1697 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1699 homedir = g_get_home_dir ();
1703 GtkTreePath *tree_path;
1704 GCancellable *cancellable;
1705 struct ChangeIconThemeData *info;
1707 file = g_file_new_for_path (homedir);
1708 gtk_list_store_insert (store, &iter, pos);
1711 info = g_new0 (struct ChangeIconThemeData, 1);
1712 info->button = g_object_ref (button);
1713 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1714 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1716 gtk_tree_path_free (tree_path);
1718 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1719 "standard::icon,standard::display-name",
1720 model_add_special_get_info_cb, info);
1722 gtk_list_store_set (store, &iter,
1724 DISPLAY_NAME_COLUMN, NULL,
1725 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1727 IS_FOLDER_COLUMN, TRUE,
1728 CANCELLABLE_COLUMN, cancellable,
1731 button->priv->n_special++;
1734 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1736 /* "To disable a directory, point it to the homedir."
1737 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1739 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1741 GtkTreePath *tree_path;
1742 GCancellable *cancellable;
1743 struct ChangeIconThemeData *info;
1745 file = g_file_new_for_path (desktopdir);
1746 gtk_list_store_insert (store, &iter, pos);
1749 info = g_new0 (struct ChangeIconThemeData, 1);
1750 info->button = g_object_ref (button);
1751 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1752 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1754 gtk_tree_path_free (tree_path);
1756 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1757 "standard::icon,standard::display-name",
1758 model_add_special_get_info_cb, info);
1760 gtk_list_store_set (store, &iter,
1761 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1763 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1765 IS_FOLDER_COLUMN, TRUE,
1766 CANCELLABLE_COLUMN, cancellable,
1769 button->priv->n_special++;
1774 model_add_volumes (GtkFileChooserButton *button,
1777 GtkListStore *store;
1779 gboolean local_only;
1785 store = GTK_LIST_STORE (button->priv->model);
1786 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1787 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1789 for (l = volumes; l; l = l->next)
1791 GtkFileSystemVolume *volume;
1794 gchar *display_name;
1800 if (_gtk_file_system_volume_is_mounted (volume))
1804 base_file = _gtk_file_system_volume_get_root (volume);
1805 if (base_file != NULL)
1807 if (!g_file_is_native (base_file))
1809 g_object_unref (base_file);
1813 g_object_unref (base_file);
1818 pixbuf = _gtk_file_system_volume_render_icon (volume,
1819 GTK_WIDGET (button),
1820 button->priv->icon_size,
1822 display_name = _gtk_file_system_volume_get_display_name (volume);
1824 gtk_list_store_insert (store, &iter, pos);
1825 gtk_list_store_set (store, &iter,
1826 ICON_COLUMN, pixbuf,
1827 DISPLAY_NAME_COLUMN, display_name,
1828 TYPE_COLUMN, ROW_TYPE_VOLUME,
1829 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
1830 IS_FOLDER_COLUMN, TRUE,
1834 g_object_unref (pixbuf);
1835 g_free (display_name);
1837 button->priv->n_volumes++;
1842 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
1845 model_add_bookmarks (GtkFileChooserButton *button,
1848 GtkListStore *store;
1851 gboolean local_only;
1857 store = GTK_LIST_STORE (button->priv->model);
1858 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
1859 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1861 for (l = bookmarks; l; l = l->next)
1867 if (g_file_is_native (file))
1869 gtk_list_store_insert (store, &iter, pos);
1870 gtk_list_store_set (store, &iter,
1872 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1873 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1874 DATA_COLUMN, g_object_ref (file),
1875 IS_FOLDER_COLUMN, FALSE,
1877 set_info_for_file_at_iter (button, file, &iter);
1882 GtkIconTheme *icon_theme;
1888 /* Don't call get_info for remote paths to avoid latency and
1890 * If we switch to a better bookmarks file format (XBEL), we
1891 * should use mime info to get a better icon.
1893 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1895 label = _gtk_file_chooser_label_for_file (file);
1897 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1898 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1899 button->priv->icon_size, 0, NULL);
1901 gtk_list_store_insert (store, &iter, pos);
1902 gtk_list_store_set (store, &iter,
1903 ICON_COLUMN, pixbuf,
1904 DISPLAY_NAME_COLUMN, label,
1905 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1906 DATA_COLUMN, g_object_ref (file),
1907 IS_FOLDER_COLUMN, TRUE,
1911 g_object_unref (pixbuf);
1914 button->priv->n_bookmarks++;
1918 if (button->priv->n_bookmarks > 0 &&
1919 !button->priv->has_bookmark_separator)
1921 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
1923 gtk_list_store_insert (store, &iter, pos);
1924 gtk_list_store_set (store, &iter,
1926 DISPLAY_NAME_COLUMN, NULL,
1927 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
1929 IS_FOLDER_COLUMN, FALSE,
1931 button->priv->has_bookmark_separator = TRUE;
1936 model_update_current_folder (GtkFileChooserButton *button,
1939 GtkListStore *store;
1946 store = GTK_LIST_STORE (button->priv->model);
1948 if (!button->priv->has_current_folder_separator)
1950 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
1951 gtk_list_store_insert (store, &iter, pos);
1952 gtk_list_store_set (store, &iter,
1954 DISPLAY_NAME_COLUMN, NULL,
1955 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
1957 IS_FOLDER_COLUMN, FALSE,
1959 button->priv->has_current_folder_separator = TRUE;
1962 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
1963 if (!button->priv->has_current_folder)
1965 gtk_list_store_insert (store, &iter, pos);
1966 button->priv->has_current_folder = TRUE;
1970 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
1971 model_free_row_data (button, &iter);
1974 if (g_file_is_native (file))
1976 gtk_list_store_set (store, &iter,
1978 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1979 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
1980 DATA_COLUMN, g_object_ref (file),
1981 IS_FOLDER_COLUMN, FALSE,
1983 set_info_for_file_at_iter (button, file, &iter);
1988 GtkIconTheme *icon_theme;
1991 /* Don't call get_info for remote paths to avoid latency and
1993 * If we switch to a better bookmarks file format (XBEL), we
1994 * should use mime info to get a better icon.
1996 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1998 label = _gtk_file_chooser_label_for_file (file);
2000 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2002 if (g_file_is_native (file))
2003 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2004 button->priv->icon_size, 0, NULL);
2006 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2007 button->priv->icon_size, 0, NULL);
2009 gtk_list_store_set (store, &iter,
2010 ICON_COLUMN, pixbuf,
2011 DISPLAY_NAME_COLUMN, label,
2012 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2013 DATA_COLUMN, g_object_ref (file),
2014 IS_FOLDER_COLUMN, TRUE,
2018 g_object_unref (pixbuf);
2023 model_add_other (GtkFileChooserButton *button)
2025 GtkListStore *store;
2029 store = GTK_LIST_STORE (button->priv->model);
2030 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2032 gtk_list_store_insert (store, &iter, pos);
2033 gtk_list_store_set (store, &iter,
2035 DISPLAY_NAME_COLUMN, NULL,
2036 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2038 IS_FOLDER_COLUMN, FALSE,
2040 button->priv->has_other_separator = TRUE;
2043 gtk_list_store_insert (store, &iter, pos);
2044 gtk_list_store_set (store, &iter,
2046 DISPLAY_NAME_COLUMN, _("Other..."),
2047 TYPE_COLUMN, ROW_TYPE_OTHER,
2049 IS_FOLDER_COLUMN, FALSE,
2054 model_remove_rows (GtkFileChooserButton *button,
2058 GtkListStore *store;
2063 store = GTK_LIST_STORE (button->priv->model);
2069 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2070 g_assert_not_reached ();
2072 model_free_row_data (button, &iter);
2073 gtk_list_store_remove (store, &iter);
2080 static inline gboolean
2081 test_if_file_is_visible (GtkFileSystem *fs,
2083 gboolean local_only,
2089 if (local_only && !g_file_is_native (file))
2099 filter_model_visible_func (GtkTreeModel *model,
2103 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2104 GtkFileChooserButtonPrivate *priv = button->priv;
2107 gboolean local_only, retval, is_folder;
2109 type = ROW_TYPE_INVALID;
2111 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2113 gtk_tree_model_get (model, iter,
2116 IS_FOLDER_COLUMN, &is_folder,
2121 case ROW_TYPE_CURRENT_FOLDER:
2124 case ROW_TYPE_SPECIAL:
2125 case ROW_TYPE_SHORTCUT:
2126 case ROW_TYPE_BOOKMARK:
2127 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2129 case ROW_TYPE_VOLUME:
2134 if (_gtk_file_system_volume_is_mounted (data))
2138 base_file = _gtk_file_system_volume_get_root (data);
2142 if (!g_file_is_native (base_file))
2144 g_object_unref (base_file);
2162 name_cell_data_func (GtkCellLayout *layout,
2163 GtkCellRenderer *cell,
2164 GtkTreeModel *model,
2171 gtk_tree_model_get (model, iter,
2175 if (type == ROW_TYPE_CURRENT_FOLDER)
2176 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2178 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2182 combo_box_row_separator_func (GtkTreeModel *model,
2186 gchar type = ROW_TYPE_INVALID;
2188 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2190 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2191 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2192 type == ROW_TYPE_OTHER_SEPARATOR);
2196 update_combo_box (GtkFileChooserButton *button)
2198 GtkFileChooserButtonPrivate *priv = button->priv;
2203 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2205 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2214 type = ROW_TYPE_INVALID;
2217 gtk_tree_model_get (priv->filter_model, &iter,
2224 case ROW_TYPE_SPECIAL:
2225 case ROW_TYPE_SHORTCUT:
2226 case ROW_TYPE_BOOKMARK:
2227 case ROW_TYPE_CURRENT_FOLDER:
2228 row_found = (files &&
2230 g_file_equal (data, files->data));
2232 case ROW_TYPE_VOLUME:
2236 base_file = _gtk_file_system_volume_get_root (data);
2239 row_found = (files &&
2241 g_file_equal (base_file, files->data));
2242 g_object_unref (base_file);
2253 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2254 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2256 g_signal_handler_unblock (priv->combo_box,
2257 priv->combo_box_changed_id);
2260 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2262 /* If it hasn't been found already, update & select the current-folder row. */
2263 if (!row_found && files && files->data)
2265 GtkTreeIter filter_iter;
2268 model_update_current_folder (button, files->data);
2269 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2271 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2272 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2274 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2275 &filter_iter, &iter);
2277 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2278 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2279 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2282 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2283 g_slist_free (files);
2288 update_label_get_info_cb (GCancellable *cancellable,
2290 const GError *error,
2293 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2295 GtkFileChooserButton *button = data;
2296 GtkFileChooserButtonPrivate *priv = button->priv;
2298 if (cancellable != priv->update_button_cancellable)
2301 priv->update_button_cancellable = NULL;
2303 if (cancelled || error)
2306 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2308 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2311 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2313 priv->icon_size, 0, NULL);
2315 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2317 g_object_unref (pixbuf);
2320 g_object_unref (button);
2321 g_object_unref (cancellable);
2325 update_label_and_image (GtkFileChooserButton *button)
2327 GtkFileChooserButtonPrivate *priv = button->priv;
2331 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2334 if (priv->update_button_cancellable)
2336 g_cancellable_cancel (priv->update_button_cancellable);
2337 priv->update_button_cancellable = NULL;
2340 if (files && files->data)
2343 GtkFileSystemVolume *volume = NULL;
2347 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2352 base_file = _gtk_file_system_volume_get_root (volume);
2353 if (base_file && g_file_equal (base_file, file))
2357 label_text = _gtk_file_system_volume_get_display_name (volume);
2358 pixbuf = _gtk_file_system_volume_render_icon (volume,
2359 GTK_WIDGET (button),
2362 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2364 g_object_unref (pixbuf);
2368 g_object_unref (base_file);
2370 _gtk_file_system_volume_unref (volume);
2376 if (g_file_is_native (file))
2378 priv->update_button_cancellable =
2379 _gtk_file_system_get_info (priv->fs, file,
2380 "standard::icon,standard::display-name",
2381 update_label_get_info_cb,
2382 g_object_ref (button));
2388 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2389 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2391 priv->icon_size, 0, NULL);
2392 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2394 g_object_unref (pixbuf);
2398 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2399 g_slist_free (files);
2403 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2404 g_free (label_text);
2408 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2409 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2414 /* ************************ *
2415 * Child Object Callbacks *
2416 * ************************ */
2420 fs_volumes_changed_cb (GtkFileSystem *fs,
2423 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2424 GtkFileChooserButtonPrivate *priv = button->priv;
2427 model_remove_rows (user_data,
2428 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2431 priv->n_volumes = 0;
2433 volumes = _gtk_file_system_list_volumes (fs);
2434 model_add_volumes (user_data, volumes);
2435 g_slist_free (volumes);
2437 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2439 update_label_and_image (user_data);
2440 update_combo_box (user_data);
2444 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2447 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2448 GtkFileChooserButtonPrivate *priv = button->priv;
2451 bookmarks = _gtk_file_system_list_bookmarks (fs);
2452 model_remove_rows (user_data,
2453 model_get_type_position (user_data,
2454 ROW_TYPE_BOOKMARK_SEPARATOR),
2455 (priv->n_bookmarks + priv->has_bookmark_separator));
2456 priv->has_bookmark_separator = FALSE;
2457 priv->n_bookmarks = 0;
2458 model_add_bookmarks (user_data, bookmarks);
2459 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2460 g_slist_free (bookmarks);
2462 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2464 update_label_and_image (user_data);
2465 update_combo_box (user_data);
2470 open_dialog (GtkFileChooserButton *button)
2472 GtkFileChooserButtonPrivate *priv = button->priv;
2474 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2476 if (!gtk_widget_get_visible (priv->dialog))
2478 GtkWidget *toplevel;
2480 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2482 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2484 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2485 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2486 GTK_WINDOW (toplevel));
2488 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2489 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2497 g_signal_handler_block (priv->dialog,
2498 priv->dialog_folder_changed_id);
2499 g_signal_handler_block (priv->dialog,
2500 priv->dialog_file_activated_id);
2501 g_signal_handler_block (priv->dialog,
2502 priv->dialog_selection_changed_id);
2503 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2507 priv->old_file = g_object_ref (files->data);
2509 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2510 g_slist_free (files);
2513 priv->active = TRUE;
2516 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2517 gtk_window_present (GTK_WINDOW (priv->dialog));
2522 combo_box_changed_cb (GtkComboBox *combo_box,
2527 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2529 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2530 GtkFileChooserButtonPrivate *priv = button->priv;
2534 type = ROW_TYPE_INVALID;
2537 gtk_tree_model_get (priv->filter_model, &iter,
2544 case ROW_TYPE_SPECIAL:
2545 case ROW_TYPE_SHORTCUT:
2546 case ROW_TYPE_BOOKMARK:
2547 case ROW_TYPE_CURRENT_FOLDER:
2548 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2550 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2553 case ROW_TYPE_VOLUME:
2557 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2558 base_file = _gtk_file_system_volume_get_root (data);
2561 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2563 g_object_unref (base_file);
2567 case ROW_TYPE_OTHER:
2568 open_dialog (user_data);
2578 button_clicked_cb (GtkButton *real_button,
2581 open_dialog (user_data);
2586 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2589 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2590 GtkFileChooserButtonPrivate *priv = button->priv;
2592 priv->folder_has_been_set = TRUE;
2594 g_signal_emit_by_name (button, "current-folder-changed");
2598 dialog_file_activated_cb (GtkFileChooser *dialog,
2601 g_signal_emit_by_name (user_data, "file-activated");
2605 dialog_selection_changed_cb (GtkFileChooser *dialog,
2608 update_label_and_image (user_data);
2609 update_combo_box (user_data);
2610 g_signal_emit_by_name (user_data, "selection-changed");
2614 dialog_update_preview_cb (GtkFileChooser *dialog,
2617 g_signal_emit_by_name (user_data, "update-preview");
2621 dialog_notify_cb (GObject *dialog,
2627 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2628 GTK_TYPE_FILE_CHOOSER);
2629 if (g_object_interface_find_property (iface, pspec->name))
2630 g_object_notify (user_data, pspec->name);
2632 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2634 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2635 GtkFileChooserButtonPrivate *priv = button->priv;
2637 if (priv->has_current_folder)
2643 pos = model_get_type_position (user_data,
2644 ROW_TYPE_CURRENT_FOLDER);
2645 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2648 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2650 /* If the path isn't local but we're in local-only mode now, remove
2651 * the custom-folder row */
2652 if (data && g_file_is_native (G_FILE (data)) &&
2653 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2656 model_remove_rows (user_data, pos, 2);
2660 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2661 update_combo_box (user_data);
2666 dialog_delete_event_cb (GtkWidget *dialog,
2670 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2676 dialog_response_cb (GtkDialog *dialog,
2680 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2681 GtkFileChooserButtonPrivate *priv = button->priv;
2683 if (response == GTK_RESPONSE_ACCEPT ||
2684 response == GTK_RESPONSE_OK)
2686 g_signal_emit_by_name (user_data, "current-folder-changed");
2687 g_signal_emit_by_name (user_data, "selection-changed");
2689 else if (priv->old_file)
2691 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)))
2693 case GTK_FILE_CHOOSER_ACTION_OPEN:
2694 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (dialog), priv->old_file,
2697 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
2698 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog),
2699 priv->old_file, NULL);
2702 g_assert_not_reached ();
2707 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (dialog));
2711 g_object_unref (priv->old_file);
2712 priv->old_file = NULL;
2715 update_label_and_image (user_data);
2716 update_combo_box (user_data);
2720 g_signal_handler_unblock (priv->dialog,
2721 priv->dialog_folder_changed_id);
2722 g_signal_handler_unblock (priv->dialog,
2723 priv->dialog_file_activated_id);
2724 g_signal_handler_unblock (priv->dialog,
2725 priv->dialog_selection_changed_id);
2726 priv->active = FALSE;
2729 gtk_widget_set_sensitive (priv->combo_box, TRUE);
2730 gtk_widget_hide (priv->dialog);
2732 if (response == GTK_RESPONSE_ACCEPT ||
2733 response == GTK_RESPONSE_OK)
2734 g_signal_emit (user_data, file_chooser_button_signals[FILE_SET], 0);
2738 /* ************************************************************************** *
2740 * ************************************************************************** */
2743 * gtk_file_chooser_button_new:
2744 * @title: the title of the browse dialog.
2745 * @action: the open mode for the widget.
2747 * Creates a new file-selecting button widget.
2749 * Returns: a new button widget.
2754 gtk_file_chooser_button_new (const gchar *title,
2755 GtkFileChooserAction action)
2757 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2758 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2760 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2762 "title", (title ? title : _(DEFAULT_TITLE)),
2767 * gtk_file_chooser_button_new_with_dialog:
2768 * @dialog: the widget to use as dialog
2770 * Creates a #GtkFileChooserButton widget which uses @dialog as its
2771 * file-picking window.
2773 * Note that @dialog must be a #GtkDialog (or subclass) which
2774 * implements the #GtkFileChooser interface and must not have
2775 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2777 * Also note that the dialog needs to have its confirmative button
2778 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2779 * order for the button to take over the file selected in the dialog.
2781 * Returns: a new button widget.
2786 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2788 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2790 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2796 * gtk_file_chooser_button_set_title:
2797 * @button: the button widget to modify.
2798 * @title: the new browse dialog title.
2800 * Modifies the @title of the browse dialog used by @button.
2805 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2808 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2810 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2811 g_object_notify (G_OBJECT (button), "title");
2815 * gtk_file_chooser_button_get_title:
2816 * @button: the button widget to examine.
2818 * Retrieves the title of the browse dialog used by @button. The returned value
2819 * should not be modified or freed.
2821 * Returns: a pointer to the browse dialog's title.
2826 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2828 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2830 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2834 * gtk_file_chooser_button_get_width_chars:
2835 * @button: the button widget to examine.
2837 * Retrieves the width in characters of the @button widget's entry and/or label.
2839 * Returns: an integer width (in characters) that the button will use to size itself.
2844 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2846 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2848 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2852 * gtk_file_chooser_button_set_width_chars:
2853 * @button: the button widget to examine.
2854 * @n_chars: the new width, in characters.
2856 * Sets the width (in characters) that @button will use to @n_chars.
2861 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
2864 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2866 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
2867 g_object_notify (G_OBJECT (button), "width-chars");
2871 * gtk_file_chooser_button_set_focus_on_click:
2872 * @button: a #GtkFileChooserButton
2873 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2875 * Sets whether the button will grab focus when it is clicked with the mouse.
2876 * Making mouse clicks not grab focus is useful in places like toolbars where
2877 * you don't want the keyboard focus removed from the main area of the
2883 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
2884 gboolean focus_on_click)
2886 GtkFileChooserButtonPrivate *priv;
2888 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2890 priv = button->priv;
2892 focus_on_click = focus_on_click != FALSE;
2894 if (priv->focus_on_click != focus_on_click)
2896 priv->focus_on_click = focus_on_click;
2897 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
2898 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
2900 g_object_notify (G_OBJECT (button), "focus-on-click");
2905 * gtk_file_chooser_button_get_focus_on_click:
2906 * @button: a #GtkFileChooserButton
2908 * Returns whether the button grabs focus when it is clicked with the mouse.
2909 * See gtk_file_chooser_button_set_focus_on_click().
2911 * Return value: %TRUE if the button grabs focus when it is clicked with
2917 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
2919 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
2921 return button->priv->focus_on_click;