1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
3 /* GTK+: gtkfilechooserbutton.c
5 * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
32 #include "gtkbutton.h"
33 #include "gtkcelllayout.h"
34 #include "gtkcellrenderertext.h"
35 #include "gtkcellrendererpixbuf.h"
36 #include "gtkcombobox.h"
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
42 #include "gtkliststore.h"
44 #include "gtktreemodelfilter.h"
45 #include "gtkseparator.h"
46 #include "gtkfilechooserdialog.h"
47 #include "gtkfilechooserprivate.h"
48 #include "gtkfilechooserutils.h"
49 #include "gtkmarshalers.h"
51 #include "gtkfilechooserbutton.h"
53 #include "gtkorientable.h"
55 #include "gtktypebuiltins.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
61 * SECTION:gtkfilechooserbutton
62 * @Short_description: A button to launch a file selection dialog
63 * @Title: GtkFileChooserButton
64 * @See_also:#GtkFileChooserDialog
66 * The #GtkFileChooserButton is a widget that lets the user select a
67 * file. It implements the #GtkFileChooser interface. Visually, it is a
68 * file name with a button to bring up a #GtkFileChooserDialog.
69 * The user can then use that dialog to change the file associated with
70 * that button. This widget does not support setting the
71 * #GtkFileChooser:select-multiple property to %TRUE.
74 * <title>Create a button to let the user select a file in /etc</title>
79 * button = gtk_file_chooser_button_new (_("Select a file"),
80 * GTK_FILE_CHOOSER_ACTION_OPEN);
81 * gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
87 * The #GtkFileChooserButton supports the #GtkFileChooserAction<!-- -->s
88 * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
91 * The #GtkFileChooserButton will ellipsize the label,
92 * and thus will thus request little horizontal space. To give the button
93 * more space, you should call gtk_widget_get_preferred_size(),
94 * gtk_file_chooser_button_set_width_chars(), or pack the button in
95 * such a way that other interface elements give space to the widget.
100 /* **************** *
102 * **************** */
104 #define DEFAULT_TITLE N_("Select a File")
105 #define DESKTOP_DISPLAY_NAME N_("Desktop")
106 #define FALLBACK_DISPLAY_NAME N_("(None)")
107 #define FALLBACK_ICON_NAME "stock_unknown"
108 #define FALLBACK_ICON_SIZE 16
111 /* ********************** *
112 * Private Enumerations *
113 * ********************** */
133 /* TreeModel Columns */
145 /* TreeModel Row Types */
151 ROW_TYPE_BOOKMARK_SEPARATOR,
153 ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
154 ROW_TYPE_CURRENT_FOLDER,
155 ROW_TYPE_OTHER_SEPARATOR,
158 ROW_TYPE_INVALID = -1
163 /* ******************** *
164 * Private Structures *
165 * ******************** */
167 struct _GtkFileChooserButtonPrivate
173 GtkWidget *combo_box;
174 GtkCellRenderer *icon_cell;
175 GtkCellRenderer *name_cell;
178 GtkTreeModel *filter_model;
181 GFile *selection_while_inactive;
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 guint focus_on_click : 1;
223 /* ********************* *
224 * Function Prototypes *
225 * ********************* */
227 /* GtkFileChooserIface Functions */
228 static void gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
229 static gboolean gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
232 static void gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
234 static void gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser);
235 static GSList *gtk_file_chooser_button_get_files (GtkFileChooser *chooser);
236 static gboolean gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
239 static gboolean gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
243 /* GObject Functions */
244 static GObject *gtk_file_chooser_button_constructor (GType type,
246 GObjectConstructParam *params);
247 static void gtk_file_chooser_button_set_property (GObject *object,
251 static void gtk_file_chooser_button_get_property (GObject *object,
255 static void gtk_file_chooser_button_finalize (GObject *object);
257 /* GtkWidget Functions */
258 static void gtk_file_chooser_button_destroy (GtkWidget *widget);
259 static void gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
260 GdkDragContext *context,
263 GtkSelectionData *data,
266 static void gtk_file_chooser_button_show_all (GtkWidget *widget);
267 static void gtk_file_chooser_button_show (GtkWidget *widget);
268 static void gtk_file_chooser_button_hide (GtkWidget *widget);
269 static void gtk_file_chooser_button_map (GtkWidget *widget);
270 static gboolean gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
271 gboolean group_cycling);
272 static void gtk_file_chooser_button_style_updated (GtkWidget *widget);
273 static void gtk_file_chooser_button_screen_changed (GtkWidget *widget,
274 GdkScreen *old_screen);
276 /* Utility Functions */
277 static GtkIconTheme *get_icon_theme (GtkWidget *widget);
278 static void set_info_for_file_at_iter (GtkFileChooserButton *fs,
282 static gint model_get_type_position (GtkFileChooserButton *button,
284 static void model_free_row_data (GtkFileChooserButton *button,
286 static inline void model_add_special (GtkFileChooserButton *button);
287 static inline void model_add_other (GtkFileChooserButton *button);
288 static void model_add_volumes (GtkFileChooserButton *button,
290 static void model_add_bookmarks (GtkFileChooserButton *button,
292 static void model_update_current_folder (GtkFileChooserButton *button,
294 static void model_remove_rows (GtkFileChooserButton *button,
298 static gboolean filter_model_visible_func (GtkTreeModel *model,
302 static gboolean combo_box_row_separator_func (GtkTreeModel *model,
305 static void name_cell_data_func (GtkCellLayout *layout,
306 GtkCellRenderer *cell,
310 static void open_dialog (GtkFileChooserButton *button);
311 static void update_combo_box (GtkFileChooserButton *button);
312 static void update_label_and_image (GtkFileChooserButton *button);
314 /* Child Object Callbacks */
315 static void fs_volumes_changed_cb (GtkFileSystem *fs,
317 static void fs_bookmarks_changed_cb (GtkFileSystem *fs,
320 static void combo_box_changed_cb (GtkComboBox *combo_box,
323 static void button_clicked_cb (GtkButton *real_button,
326 static void dialog_update_preview_cb (GtkFileChooser *dialog,
328 static void dialog_selection_changed_cb (GtkFileChooser *dialog,
330 static void dialog_file_activated_cb (GtkFileChooser *dialog,
332 static void dialog_current_folder_changed_cb (GtkFileChooser *dialog,
334 static void dialog_notify_cb (GObject *dialog,
337 static gboolean dialog_delete_event_cb (GtkWidget *dialog,
340 static void dialog_response_cb (GtkDialog *dialog,
344 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
346 /* ******************* *
347 * GType Declaration *
348 * ******************* */
350 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_BOX, { \
351 G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
355 /* ***************** *
357 * ***************** */
360 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
362 GObjectClass *gobject_class;
363 GtkWidgetClass *widget_class;
365 gobject_class = G_OBJECT_CLASS (class);
366 widget_class = GTK_WIDGET_CLASS (class);
368 gobject_class->constructor = gtk_file_chooser_button_constructor;
369 gobject_class->set_property = gtk_file_chooser_button_set_property;
370 gobject_class->get_property = gtk_file_chooser_button_get_property;
371 gobject_class->finalize = gtk_file_chooser_button_finalize;
373 widget_class->destroy = gtk_file_chooser_button_destroy;
374 widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
375 widget_class->show_all = gtk_file_chooser_button_show_all;
376 widget_class->show = gtk_file_chooser_button_show;
377 widget_class->hide = gtk_file_chooser_button_hide;
378 widget_class->map = gtk_file_chooser_button_map;
379 widget_class->style_updated = gtk_file_chooser_button_style_updated;
380 widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
381 widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
384 * GtkFileChooserButton::file-set:
385 * @widget: the object which received the signal.
387 * The ::file-set signal is emitted when the user selects a file.
389 * Note that this signal is only emitted when the <emphasis>user</emphasis>
394 file_chooser_button_signals[FILE_SET] =
395 g_signal_new (I_("file-set"),
396 G_TYPE_FROM_CLASS (gobject_class),
398 G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
400 _gtk_marshal_VOID__VOID,
404 * GtkFileChooserButton:dialog:
406 * Instance of the #GtkFileChooserDialog associated with the button.
410 g_object_class_install_property (gobject_class, PROP_DIALOG,
411 g_param_spec_object ("dialog",
413 P_("The file chooser dialog to use."),
414 GTK_TYPE_FILE_CHOOSER,
415 (GTK_PARAM_WRITABLE |
416 G_PARAM_CONSTRUCT_ONLY)));
419 * GtkFileChooserButton:focus-on-click:
421 * Whether the #GtkFileChooserButton button grabs focus when it is clicked
426 g_object_class_install_property (gobject_class,
428 g_param_spec_boolean ("focus-on-click",
429 P_("Focus on click"),
430 P_("Whether the button grabs focus when it is clicked with the mouse"),
432 GTK_PARAM_READWRITE));
435 * GtkFileChooserButton:title:
437 * Title to put on the #GtkFileChooserDialog associated with the button.
441 g_object_class_install_property (gobject_class, PROP_TITLE,
442 g_param_spec_string ("title",
444 P_("The title of the file chooser dialog."),
446 GTK_PARAM_READWRITE));
449 * GtkFileChooserButton:width-chars:
451 * The width of the entry and label inside the button, in characters.
455 g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
456 g_param_spec_int ("width-chars",
457 P_("Width In Characters"),
458 P_("The desired width of the button widget, in characters."),
460 GTK_PARAM_READWRITE));
462 _gtk_file_chooser_install_properties (gobject_class);
464 g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
468 gtk_file_chooser_button_init (GtkFileChooserButton *button)
470 GtkFileChooserButtonPrivate *priv;
471 GtkWidget *box, *image, *sep;
472 GtkTargetList *target_list;
474 priv = button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
475 GTK_TYPE_FILE_CHOOSER_BUTTON,
476 GtkFileChooserButtonPrivate);
478 priv->icon_size = FALLBACK_ICON_SIZE;
479 priv->focus_on_click = TRUE;
481 gtk_widget_push_composite_child ();
484 priv->button = gtk_button_new ();
485 g_signal_connect (priv->button, "clicked",
486 G_CALLBACK (button_clicked_cb), button);
487 gtk_box_pack_start (GTK_BOX (button), priv->button, TRUE, TRUE, 0);
488 gtk_widget_set_halign (priv->button, GTK_ALIGN_FILL);
489 gtk_widget_show (priv->button);
491 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
492 gtk_container_add (GTK_CONTAINER (priv->button), box);
493 gtk_widget_show (box);
495 priv->image = gtk_image_new ();
496 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
497 gtk_widget_show (priv->image);
499 priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
500 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
501 gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
502 gtk_widget_set_valign (priv->label, GTK_ALIGN_CENTER);
503 gtk_box_pack_start (GTK_BOX (box), priv->label, TRUE, TRUE, 0);
504 //gtk_container_add (GTK_CONTAINER (box), priv->label);
505 gtk_widget_show (priv->label);
507 sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
508 gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
509 gtk_widget_show (sep);
511 image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
512 gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
513 gtk_widget_show (image);
516 /* Keep in sync with columns enum, line 88 */
518 GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
519 GDK_TYPE_PIXBUF, /* Icon */
520 G_TYPE_STRING, /* Display Name */
521 G_TYPE_CHAR, /* Row Type */
522 G_TYPE_POINTER /* Volume || Path */,
523 G_TYPE_BOOLEAN /* Is Folder? */,
524 G_TYPE_POINTER /* cancellable */));
526 priv->combo_box = gtk_combo_box_new ();
527 priv->combo_box_changed_id =
528 g_signal_connect (priv->combo_box, "changed",
529 G_CALLBACK (combo_box_changed_cb), button);
530 gtk_box_pack_start (GTK_BOX (button), priv->combo_box, TRUE, TRUE, 0);
531 gtk_widget_set_halign (priv->combo_box, GTK_ALIGN_FILL);
533 priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
534 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
535 priv->icon_cell, FALSE);
536 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
537 priv->icon_cell, "pixbuf", ICON_COLUMN);
539 priv->name_cell = gtk_cell_renderer_text_new ();
540 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
541 priv->name_cell, TRUE);
542 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
543 priv->name_cell, "text", DISPLAY_NAME_COLUMN);
544 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
545 priv->name_cell, name_cell_data_func,
548 gtk_widget_pop_composite_child ();
551 gtk_drag_dest_set (GTK_WIDGET (button),
552 (GTK_DEST_DEFAULT_ALL),
555 target_list = gtk_target_list_new (NULL, 0);
556 gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
557 gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
558 gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
559 gtk_target_list_unref (target_list);
563 /* ******************************* *
564 * GtkFileChooserIface Functions *
565 * ******************************* */
567 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
569 _gtk_file_chooser_delegate_iface_init (iface);
571 iface->select_file = gtk_file_chooser_button_select_file;
572 iface->unselect_file = gtk_file_chooser_button_unselect_file;
573 iface->unselect_all = gtk_file_chooser_button_unselect_all;
574 iface->get_files = gtk_file_chooser_button_get_files;
575 iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
576 iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
580 gtk_file_chooser_button_select_file (GtkFileChooser *chooser,
584 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
585 GtkFileChooserButtonPrivate *priv = button->priv;
586 GtkFileChooser *delegate;
588 delegate = g_object_get_qdata (G_OBJECT (chooser),
589 GTK_FILE_CHOOSER_DELEGATE_QUARK);
592 return gtk_file_chooser_select_file (delegate, file, error);
595 if (priv->selection_while_inactive)
596 g_object_unref (priv->selection_while_inactive);
598 priv->selection_while_inactive = g_object_ref (file);
600 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
607 gtk_file_chooser_button_unselect_file (GtkFileChooser *chooser,
610 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
611 GtkFileChooserButtonPrivate *priv = button->priv;
612 GtkFileChooser *delegate;
614 delegate = g_object_get_qdata (G_OBJECT (chooser),
615 GTK_FILE_CHOOSER_DELEGATE_QUARK);
618 gtk_file_chooser_unselect_file (delegate, file);
621 if (g_file_equal (priv->selection_while_inactive, file))
623 if (priv->selection_while_inactive)
625 g_object_unref (priv->selection_while_inactive);
626 priv->selection_while_inactive = NULL;
629 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
635 gtk_file_chooser_button_unselect_all (GtkFileChooser *chooser)
637 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
638 GtkFileChooserButtonPrivate *priv = button->priv;
639 GtkFileChooser *delegate;
641 delegate = g_object_get_qdata (G_OBJECT (chooser),
642 GTK_FILE_CHOOSER_DELEGATE_QUARK);
645 gtk_file_chooser_unselect_all (delegate);
648 if (priv->selection_while_inactive)
650 g_object_unref (priv->selection_while_inactive);
651 priv->selection_while_inactive = NULL;
654 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
659 gtk_file_chooser_button_get_files (GtkFileChooser *chooser)
661 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
662 GtkFileChooserButtonPrivate *priv = button->priv;
663 GtkFileChooser *delegate;
665 delegate = g_object_get_qdata (G_OBJECT (chooser),
666 GTK_FILE_CHOOSER_DELEGATE_QUARK);
669 return gtk_file_chooser_get_files (delegate);
674 if (priv->selection_while_inactive)
675 result = g_slist_prepend (NULL, g_object_ref (priv->selection_while_inactive));
684 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
688 GtkFileChooser *delegate;
691 delegate = g_object_get_qdata (G_OBJECT (chooser),
692 GTK_FILE_CHOOSER_DELEGATE_QUARK);
693 retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error);
697 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
698 GtkFileChooserButtonPrivate *priv = button->priv;
702 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
703 pos += priv->n_shortcuts;
705 gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
706 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
708 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
709 TYPE_COLUMN, ROW_TYPE_SHORTCUT,
710 DATA_COLUMN, g_object_ref (file),
711 IS_FOLDER_COLUMN, FALSE,
713 set_info_for_file_at_iter (button, file, &iter);
716 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
723 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
727 GtkFileChooser *delegate;
730 delegate = g_object_get_qdata (G_OBJECT (chooser),
731 GTK_FILE_CHOOSER_DELEGATE_QUARK);
733 retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
737 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
738 GtkFileChooserButtonPrivate *priv = button->priv;
743 pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
744 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
750 gtk_tree_model_get (priv->model, &iter,
755 if (type == ROW_TYPE_SHORTCUT &&
756 data && g_file_equal (data, file))
758 model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
759 gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
761 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
762 update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
766 while (type == ROW_TYPE_SHORTCUT &&
767 gtk_tree_model_iter_next (priv->model, &iter));
774 /* ******************* *
775 * GObject Functions *
776 * ******************* */
779 gtk_file_chooser_button_constructor (GType type,
781 GObjectConstructParam *params)
784 GtkFileChooserButton *button;
785 GtkFileChooserButtonPrivate *priv;
788 object = G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor (type,
791 button = GTK_FILE_CHOOSER_BUTTON (object);
796 priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
797 GTK_FILE_CHOOSER_ACTION_OPEN,
804 gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
805 GTK_RESPONSE_ACCEPT);
806 gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
811 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
813 else if (!gtk_window_get_title (GTK_WINDOW (priv->dialog)))
815 gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
818 g_signal_connect (priv->dialog, "delete-event",
819 G_CALLBACK (dialog_delete_event_cb), object);
820 g_signal_connect (priv->dialog, "response",
821 G_CALLBACK (dialog_response_cb), object);
823 /* This is used, instead of the standard delegate, to ensure that signals are only
824 * delegated when the OK button is pressed. */
825 g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
826 priv->dialog_folder_changed_id =
827 g_signal_connect (priv->dialog, "current-folder-changed",
828 G_CALLBACK (dialog_current_folder_changed_cb), object);
829 priv->dialog_file_activated_id =
830 g_signal_connect (priv->dialog, "file-activated",
831 G_CALLBACK (dialog_file_activated_cb), object);
832 priv->dialog_selection_changed_id =
833 g_signal_connect (priv->dialog, "selection-changed",
834 G_CALLBACK (dialog_selection_changed_cb), object);
835 g_signal_connect (priv->dialog, "update-preview",
836 G_CALLBACK (dialog_update_preview_cb), object);
837 g_signal_connect (priv->dialog, "notify",
838 G_CALLBACK (dialog_notify_cb), object);
839 g_object_add_weak_pointer (G_OBJECT (priv->dialog),
840 (gpointer) (&priv->dialog));
843 g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
845 model_add_special (button);
847 list = _gtk_file_system_list_volumes (priv->fs);
848 model_add_volumes (button, list);
851 list = _gtk_file_system_list_bookmarks (priv->fs);
852 model_add_bookmarks (button, list);
853 g_slist_foreach (list, (GFunc) g_object_unref, NULL);
856 model_add_other (button);
858 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
859 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
860 filter_model_visible_func,
863 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
864 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
865 combo_box_row_separator_func,
868 /* set up the action for a user-provided dialog, this also updates
869 * the label, image and combobox
871 g_object_set (object,
872 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
875 priv->fs_volumes_changed_id =
876 g_signal_connect (priv->fs, "volumes-changed",
877 G_CALLBACK (fs_volumes_changed_cb), object);
878 priv->fs_bookmarks_changed_id =
879 g_signal_connect (priv->fs, "bookmarks-changed",
880 G_CALLBACK (fs_bookmarks_changed_cb), object);
886 gtk_file_chooser_button_set_property (GObject *object,
891 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
892 GtkFileChooserButtonPrivate *priv = button->priv;
898 priv->dialog = g_value_get_object (value);
900 case PROP_FOCUS_ON_CLICK:
901 gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
903 case PROP_WIDTH_CHARS:
904 gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
905 g_value_get_int (value));
907 case GTK_FILE_CHOOSER_PROP_ACTION:
908 switch (g_value_get_enum (value))
910 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
911 case GTK_FILE_CHOOSER_ACTION_SAVE:
916 eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
917 eval = g_enum_get_value (eclass, g_value_get_enum (value));
918 g_warning ("%s: Choosers of type `%s' do not support `%s'.",
919 G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
921 g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
926 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
927 update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
928 update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
930 switch (g_value_get_enum (value))
932 case GTK_FILE_CHOOSER_ACTION_OPEN:
933 gtk_widget_hide (priv->combo_box);
934 gtk_widget_show (priv->button);
936 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
937 gtk_widget_hide (priv->button);
938 gtk_widget_show (priv->combo_box);
941 g_assert_not_reached ();
947 case GTK_FILE_CHOOSER_PROP_FILTER:
948 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
949 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
950 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
951 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
952 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
953 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
954 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
955 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
958 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
959 g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
960 fs_volumes_changed_cb (priv->fs, button);
961 fs_bookmarks_changed_cb (priv->fs, button);
964 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
965 g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
966 G_STRFUNC, G_OBJECT_TYPE_NAME (object));
969 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
975 gtk_file_chooser_button_get_property (GObject *object,
980 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
981 GtkFileChooserButtonPrivate *priv = button->priv;
985 case PROP_WIDTH_CHARS:
986 g_value_set_int (value,
987 gtk_label_get_width_chars (GTK_LABEL (priv->label)));
989 case PROP_FOCUS_ON_CLICK:
990 g_value_set_boolean (value,
991 gtk_file_chooser_button_get_focus_on_click (button));
995 case GTK_FILE_CHOOSER_PROP_ACTION:
996 case GTK_FILE_CHOOSER_PROP_FILTER:
997 case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
998 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
999 case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
1000 case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
1001 case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
1002 case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
1003 case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
1004 case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
1005 case GTK_FILE_CHOOSER_PROP_CREATE_FOLDERS:
1006 g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
1010 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
1016 gtk_file_chooser_button_finalize (GObject *object)
1018 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
1019 GtkFileChooserButtonPrivate *priv = button->priv;
1021 if (priv->selection_while_inactive)
1022 g_object_unref (priv->selection_while_inactive);
1024 G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize (object);
1027 /* ********************* *
1028 * GtkWidget Functions *
1029 * ********************* */
1032 gtk_file_chooser_button_destroy (GtkWidget *widget)
1034 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1035 GtkFileChooserButtonPrivate *priv = button->priv;
1039 if (priv->dialog != NULL)
1041 gtk_widget_destroy (priv->dialog);
1042 priv->dialog = NULL;
1045 if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
1047 model_free_row_data (button, &iter);
1049 while (gtk_tree_model_iter_next (priv->model, &iter));
1051 if (priv->dnd_select_folder_cancellable)
1053 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1054 priv->dnd_select_folder_cancellable = NULL;
1057 if (priv->update_button_cancellable)
1059 g_cancellable_cancel (priv->update_button_cancellable);
1060 priv->update_button_cancellable = NULL;
1063 if (priv->change_icon_theme_cancellables)
1065 for (l = priv->change_icon_theme_cancellables; l; l = l->next)
1067 GCancellable *cancellable = G_CANCELLABLE (l->data);
1068 g_cancellable_cancel (cancellable);
1070 g_slist_free (priv->change_icon_theme_cancellables);
1071 priv->change_icon_theme_cancellables = NULL;
1076 g_object_unref (priv->model);
1080 if (priv->filter_model)
1082 g_object_unref (priv->filter_model);
1083 priv->filter_model = NULL;
1088 g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
1089 g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
1090 g_object_unref (priv->fs);
1094 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->destroy (widget);
1097 struct DndSelectFolderData
1099 GtkFileSystem *file_system;
1100 GtkFileChooserButton *button;
1101 GtkFileChooserAction action;
1109 dnd_select_folder_get_info_cb (GCancellable *cancellable,
1111 const GError *error,
1114 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1115 struct DndSelectFolderData *data = user_data;
1117 if (cancellable != data->button->priv->dnd_select_folder_cancellable)
1119 g_object_unref (data->button);
1120 g_object_unref (data->file);
1121 g_strfreev (data->uris);
1124 g_object_unref (cancellable);
1128 data->button->priv->dnd_select_folder_cancellable = NULL;
1130 if (!cancelled && !error && info != NULL)
1134 is_folder = _gtk_file_info_consider_as_directory (info);
1137 (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER && is_folder) ||
1138 (data->action == GTK_FILE_CHOOSER_ACTION_OPEN && !is_folder)) &&
1139 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (data->button->priv->dialog),
1143 data->selected = FALSE;
1145 if (data->selected || data->uris[++data->i] == NULL)
1147 g_signal_emit (data->button, file_chooser_button_signals[FILE_SET], 0);
1149 g_object_unref (data->button);
1150 g_object_unref (data->file);
1151 g_strfreev (data->uris);
1154 g_object_unref (cancellable);
1159 g_object_unref (data->file);
1161 data->file = g_file_new_for_uri (data->uris[data->i]);
1163 data->button->priv->dnd_select_folder_cancellable =
1164 _gtk_file_system_get_info (data->file_system, data->file,
1166 dnd_select_folder_get_info_cb, user_data);
1168 g_object_unref (cancellable);
1172 gtk_file_chooser_button_drag_data_received (GtkWidget *widget,
1173 GdkDragContext *context,
1176 GtkSelectionData *data,
1180 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1181 GtkFileChooserButtonPrivate *priv = button->priv;
1185 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1186 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received (widget,
1192 if (widget == NULL || context == NULL || data == NULL || gtk_selection_data_get_length (data) < 0)
1200 struct DndSelectFolderData *info;
1202 uris = gtk_selection_data_get_uris (data);
1207 info = g_new0 (struct DndSelectFolderData, 1);
1208 info->button = g_object_ref (button);
1211 info->selected = FALSE;
1212 info->file_system = priv->fs;
1213 g_object_get (priv->dialog, "action", &info->action, NULL);
1215 info->file = g_file_new_for_uri (info->uris[info->i]);
1217 if (priv->dnd_select_folder_cancellable)
1218 g_cancellable_cancel (priv->dnd_select_folder_cancellable);
1220 priv->dnd_select_folder_cancellable =
1221 _gtk_file_system_get_info (priv->fs, info->file,
1223 dnd_select_folder_get_info_cb, info);
1228 text = (char*) gtk_selection_data_get_text (data);
1229 file = g_file_new_for_uri (text);
1230 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), file,
1232 g_object_unref (file);
1234 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
1241 gtk_drag_finish (context, TRUE, FALSE, drag_time);
1245 gtk_file_chooser_button_show_all (GtkWidget *widget)
1247 gtk_widget_show (widget);
1251 gtk_file_chooser_button_show (GtkWidget *widget)
1253 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1254 GtkFileChooserButtonPrivate *priv = button->priv;
1256 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1257 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show (widget);
1260 open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1264 gtk_file_chooser_button_hide (GtkWidget *widget)
1266 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1267 GtkFileChooserButtonPrivate *priv = button->priv;
1269 gtk_widget_hide (priv->dialog);
1271 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1272 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide (widget);
1276 gtk_file_chooser_button_map (GtkWidget *widget)
1278 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map (widget);
1282 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1283 gboolean group_cycling)
1285 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1286 GtkFileChooserButtonPrivate *priv = button->priv;
1288 switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1290 case GTK_FILE_CHOOSER_ACTION_OPEN:
1291 gtk_widget_grab_focus (priv->button);
1293 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1294 return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1297 g_assert_not_reached ();
1304 /* Changes the icons wherever it is needed */
1305 struct ChangeIconThemeData
1307 GtkFileChooserButton *button;
1308 GtkTreeRowReference *row_ref;
1312 change_icon_theme_get_info_cb (GCancellable *cancellable,
1314 const GError *error,
1317 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1319 struct ChangeIconThemeData *data = user_data;
1321 if (!g_slist_find (data->button->priv->change_icon_theme_cancellables, cancellable))
1324 data->button->priv->change_icon_theme_cancellables =
1325 g_slist_remove (data->button->priv->change_icon_theme_cancellables, cancellable);
1327 if (cancelled || error)
1330 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1338 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1340 path = gtk_tree_row_reference_get_path (data->row_ref);
1343 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1344 gtk_tree_path_free (path);
1346 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1347 ICON_COLUMN, pixbuf,
1350 g_object_set (data->button->priv->icon_cell,
1354 g_object_unref (pixbuf);
1358 g_object_unref (data->button);
1359 gtk_tree_row_reference_free (data->row_ref);
1362 g_object_unref (cancellable);
1366 change_icon_theme (GtkFileChooserButton *button)
1368 GtkFileChooserButtonPrivate *priv = button->priv;
1369 GtkSettings *settings;
1370 GtkIconTheme *theme;
1373 gint width = 0, height = 0;
1375 for (l = button->priv->change_icon_theme_cancellables; l; l = l->next)
1377 GCancellable *cancellable = G_CANCELLABLE (l->data);
1378 g_cancellable_cancel (cancellable);
1380 g_slist_free (button->priv->change_icon_theme_cancellables);
1381 button->priv->change_icon_theme_cancellables = NULL;
1383 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1385 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1387 priv->icon_size = MAX (width, height);
1389 priv->icon_size = FALLBACK_ICON_SIZE;
1391 update_label_and_image (button);
1393 gtk_tree_model_get_iter_first (priv->model, &iter);
1395 theme = get_icon_theme (GTK_WIDGET (button));
1403 type = ROW_TYPE_INVALID;
1404 gtk_tree_model_get (priv->model, &iter,
1411 case ROW_TYPE_SPECIAL:
1412 case ROW_TYPE_SHORTCUT:
1413 case ROW_TYPE_BOOKMARK:
1414 case ROW_TYPE_CURRENT_FOLDER:
1417 if (g_file_is_native (G_FILE (data)))
1420 GCancellable *cancellable;
1421 struct ChangeIconThemeData *info;
1423 info = g_new0 (struct ChangeIconThemeData, 1);
1424 info->button = g_object_ref (button);
1425 path = gtk_tree_model_get_path (priv->model, &iter);
1426 info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1427 gtk_tree_path_free (path);
1430 _gtk_file_system_get_info (priv->fs, data,
1432 change_icon_theme_get_info_cb,
1434 button->priv->change_icon_theme_cancellables =
1435 g_slist_append (button->priv->change_icon_theme_cancellables, cancellable);
1439 /* Don't call get_info for remote paths to avoid latency and
1441 * If we switch to a better bookmarks file format (XBEL), we
1442 * should use mime info to get a better icon.
1444 pixbuf = gtk_icon_theme_load_icon (theme, "folder-remote",
1445 priv->icon_size, 0, NULL);
1448 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1449 priv->icon_size, 0, NULL);
1451 case ROW_TYPE_VOLUME:
1453 pixbuf = _gtk_file_system_volume_render_icon (data,
1454 GTK_WIDGET (button),
1458 pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1459 priv->icon_size, 0, NULL);
1467 width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1469 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1470 ICON_COLUMN, pixbuf,
1474 g_object_unref (pixbuf);
1476 while (gtk_tree_model_iter_next (priv->model, &iter));
1478 g_object_set (button->priv->icon_cell,
1484 gtk_file_chooser_button_style_updated (GtkWidget *widget)
1486 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_updated (widget);
1488 if (gtk_widget_has_screen (widget))
1489 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1493 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1494 GdkScreen *old_screen)
1496 if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1497 GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed (widget,
1500 change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1504 /* ******************* *
1505 * Utility Functions *
1506 * ******************* */
1509 static GtkIconTheme *
1510 get_icon_theme (GtkWidget *widget)
1512 if (gtk_widget_has_screen (widget))
1513 return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1515 return gtk_icon_theme_get_default ();
1519 struct SetDisplayNameData
1521 GtkFileChooserButton *button;
1523 GtkTreeRowReference *row_ref;
1527 set_info_get_info_cb (GCancellable *cancellable,
1529 const GError *error,
1530 gpointer callback_data)
1532 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1536 GCancellable *model_cancellable = NULL;
1537 struct SetDisplayNameData *data = callback_data;
1540 if (!data->button->priv->model)
1541 /* button got destroyed */
1544 path = gtk_tree_row_reference_get_path (data->row_ref);
1546 /* Cancellable doesn't exist anymore in the model */
1549 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1550 gtk_tree_path_free (path);
1552 /* Validate the cancellable */
1553 gtk_tree_model_get (data->button->priv->model, &iter,
1554 CANCELLABLE_COLUMN, &model_cancellable,
1556 if (cancellable != model_cancellable)
1559 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1560 CANCELLABLE_COLUMN, NULL,
1563 if (cancelled || error)
1564 /* There was an error, leave the fallback name in there */
1567 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1570 data->label = g_strdup (g_file_info_get_display_name (info));
1572 is_folder = _gtk_file_info_consider_as_directory (info);
1574 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1575 ICON_COLUMN, pixbuf,
1576 DISPLAY_NAME_COLUMN, data->label,
1577 IS_FOLDER_COLUMN, is_folder,
1581 g_object_unref (pixbuf);
1584 g_object_unref (data->button);
1585 g_free (data->label);
1586 gtk_tree_row_reference_free (data->row_ref);
1589 if (model_cancellable)
1590 g_object_unref (model_cancellable);
1594 set_info_for_file_at_iter (GtkFileChooserButton *button,
1598 struct SetDisplayNameData *data;
1599 GtkTreePath *tree_path;
1600 GCancellable *cancellable;
1602 data = g_new0 (struct SetDisplayNameData, 1);
1603 data->button = g_object_ref (button);
1604 data->label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1606 tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1607 data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1608 gtk_tree_path_free (tree_path);
1610 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1611 "standard::type,standard::icon,standard::display-name",
1612 set_info_get_info_cb, data);
1614 gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1615 CANCELLABLE_COLUMN, cancellable,
1619 /* Shortcuts Model */
1621 model_get_type_position (GtkFileChooserButton *button,
1626 if (row_type == ROW_TYPE_SPECIAL)
1629 retval += button->priv->n_special;
1631 if (row_type == ROW_TYPE_VOLUME)
1634 retval += button->priv->n_volumes;
1636 if (row_type == ROW_TYPE_SHORTCUT)
1639 retval += button->priv->n_shortcuts;
1641 if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1644 retval += button->priv->has_bookmark_separator;
1646 if (row_type == ROW_TYPE_BOOKMARK)
1649 retval += button->priv->n_bookmarks;
1651 if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1654 retval += button->priv->has_current_folder_separator;
1656 if (row_type == ROW_TYPE_CURRENT_FOLDER)
1659 retval += button->priv->has_current_folder;
1661 if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1664 retval += button->priv->has_other_separator;
1666 if (row_type == ROW_TYPE_OTHER)
1669 g_assert_not_reached ();
1674 model_free_row_data (GtkFileChooserButton *button,
1679 GCancellable *cancellable;
1681 gtk_tree_model_get (button->priv->model, iter,
1684 CANCELLABLE_COLUMN, &cancellable,
1689 g_cancellable_cancel (cancellable);
1690 g_object_unref (cancellable);
1695 case ROW_TYPE_SPECIAL:
1696 case ROW_TYPE_SHORTCUT:
1697 case ROW_TYPE_BOOKMARK:
1698 case ROW_TYPE_CURRENT_FOLDER:
1699 g_object_unref (data);
1701 case ROW_TYPE_VOLUME:
1702 _gtk_file_system_volume_unref (data);
1710 model_add_special_get_info_cb (GCancellable *cancellable,
1712 const GError *error,
1715 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1719 GCancellable *model_cancellable = NULL;
1720 struct ChangeIconThemeData *data = user_data;
1723 if (!data->button->priv->model)
1724 /* button got destroyed */
1727 path = gtk_tree_row_reference_get_path (data->row_ref);
1729 /* Cancellable doesn't exist anymore in the model */
1732 gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1733 gtk_tree_path_free (path);
1735 gtk_tree_model_get (data->button->priv->model, &iter,
1736 CANCELLABLE_COLUMN, &model_cancellable,
1738 if (cancellable != model_cancellable)
1741 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1742 CANCELLABLE_COLUMN, NULL,
1745 if (cancelled || error)
1748 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->button), data->button->priv->icon_size);
1752 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1753 ICON_COLUMN, pixbuf,
1755 g_object_unref (pixbuf);
1758 gtk_tree_model_get (data->button->priv->model, &iter,
1759 DISPLAY_NAME_COLUMN, &name,
1762 gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1763 DISPLAY_NAME_COLUMN, g_file_info_get_display_name (info),
1768 g_object_unref (data->button);
1769 gtk_tree_row_reference_free (data->row_ref);
1772 if (model_cancellable)
1773 g_object_unref (model_cancellable);
1777 model_add_special (GtkFileChooserButton *button)
1779 const gchar *homedir;
1780 const gchar *desktopdir;
1781 GtkListStore *store;
1786 store = GTK_LIST_STORE (button->priv->model);
1787 pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1789 homedir = g_get_home_dir ();
1793 GtkTreePath *tree_path;
1794 GCancellable *cancellable;
1795 struct ChangeIconThemeData *info;
1797 file = g_file_new_for_path (homedir);
1798 gtk_list_store_insert (store, &iter, pos);
1801 info = g_new0 (struct ChangeIconThemeData, 1);
1802 info->button = g_object_ref (button);
1803 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1804 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1806 gtk_tree_path_free (tree_path);
1808 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1809 "standard::icon,standard::display-name",
1810 model_add_special_get_info_cb, info);
1812 gtk_list_store_set (store, &iter,
1814 DISPLAY_NAME_COLUMN, NULL,
1815 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1817 IS_FOLDER_COLUMN, TRUE,
1818 CANCELLABLE_COLUMN, cancellable,
1821 button->priv->n_special++;
1824 desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1826 /* "To disable a directory, point it to the homedir."
1827 * See http://freedesktop.org/wiki/Software/xdg-user-dirs
1829 if (g_strcmp0 (desktopdir, g_get_home_dir ()) != 0)
1831 GtkTreePath *tree_path;
1832 GCancellable *cancellable;
1833 struct ChangeIconThemeData *info;
1835 file = g_file_new_for_path (desktopdir);
1836 gtk_list_store_insert (store, &iter, pos);
1839 info = g_new0 (struct ChangeIconThemeData, 1);
1840 info->button = g_object_ref (button);
1841 tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1842 info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1844 gtk_tree_path_free (tree_path);
1846 cancellable = _gtk_file_system_get_info (button->priv->fs, file,
1847 "standard::icon,standard::display-name",
1848 model_add_special_get_info_cb, info);
1850 gtk_list_store_set (store, &iter,
1851 TYPE_COLUMN, ROW_TYPE_SPECIAL,
1853 DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1855 IS_FOLDER_COLUMN, TRUE,
1856 CANCELLABLE_COLUMN, cancellable,
1859 button->priv->n_special++;
1864 model_add_volumes (GtkFileChooserButton *button,
1867 GtkListStore *store;
1869 gboolean local_only;
1875 store = GTK_LIST_STORE (button->priv->model);
1876 pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1877 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1879 for (l = volumes; l; l = l->next)
1881 GtkFileSystemVolume *volume;
1884 gchar *display_name;
1890 if (_gtk_file_system_volume_is_mounted (volume))
1894 base_file = _gtk_file_system_volume_get_root (volume);
1895 if (base_file != NULL)
1897 if (!g_file_is_native (base_file))
1899 g_object_unref (base_file);
1903 g_object_unref (base_file);
1908 pixbuf = _gtk_file_system_volume_render_icon (volume,
1909 GTK_WIDGET (button),
1910 button->priv->icon_size,
1912 display_name = _gtk_file_system_volume_get_display_name (volume);
1914 gtk_list_store_insert (store, &iter, pos);
1915 gtk_list_store_set (store, &iter,
1916 ICON_COLUMN, pixbuf,
1917 DISPLAY_NAME_COLUMN, display_name,
1918 TYPE_COLUMN, ROW_TYPE_VOLUME,
1919 DATA_COLUMN, _gtk_file_system_volume_ref (volume),
1920 IS_FOLDER_COLUMN, TRUE,
1924 g_object_unref (pixbuf);
1925 g_free (display_name);
1927 button->priv->n_volumes++;
1932 extern gchar * _gtk_file_chooser_label_for_file (GFile *file);
1935 model_add_bookmarks (GtkFileChooserButton *button,
1938 GtkListStore *store;
1941 gboolean local_only;
1947 store = GTK_LIST_STORE (button->priv->model);
1948 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
1949 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1951 for (l = bookmarks; l; l = l->next)
1957 if (g_file_is_native (file))
1959 gtk_list_store_insert (store, &iter, pos);
1960 gtk_list_store_set (store, &iter,
1962 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1963 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1964 DATA_COLUMN, g_object_ref (file),
1965 IS_FOLDER_COLUMN, FALSE,
1967 set_info_for_file_at_iter (button, file, &iter);
1972 GtkIconTheme *icon_theme;
1978 /* Don't call get_info for remote paths to avoid latency and
1980 * If we switch to a better bookmarks file format (XBEL), we
1981 * should use mime info to get a better icon.
1983 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
1985 label = _gtk_file_chooser_label_for_file (file);
1987 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1988 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
1989 button->priv->icon_size, 0, NULL);
1991 gtk_list_store_insert (store, &iter, pos);
1992 gtk_list_store_set (store, &iter,
1993 ICON_COLUMN, pixbuf,
1994 DISPLAY_NAME_COLUMN, label,
1995 TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1996 DATA_COLUMN, g_object_ref (file),
1997 IS_FOLDER_COLUMN, TRUE,
2001 g_object_unref (pixbuf);
2004 button->priv->n_bookmarks++;
2008 if (button->priv->n_bookmarks > 0 &&
2009 !button->priv->has_bookmark_separator)
2011 pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
2013 gtk_list_store_insert (store, &iter, pos);
2014 gtk_list_store_set (store, &iter,
2016 DISPLAY_NAME_COLUMN, NULL,
2017 TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
2019 IS_FOLDER_COLUMN, FALSE,
2021 button->priv->has_bookmark_separator = TRUE;
2026 model_update_current_folder (GtkFileChooserButton *button,
2029 GtkListStore *store;
2036 store = GTK_LIST_STORE (button->priv->model);
2038 if (!button->priv->has_current_folder_separator)
2040 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
2041 gtk_list_store_insert (store, &iter, pos);
2042 gtk_list_store_set (store, &iter,
2044 DISPLAY_NAME_COLUMN, NULL,
2045 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
2047 IS_FOLDER_COLUMN, FALSE,
2049 button->priv->has_current_folder_separator = TRUE;
2052 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2053 if (!button->priv->has_current_folder)
2055 gtk_list_store_insert (store, &iter, pos);
2056 button->priv->has_current_folder = TRUE;
2060 gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
2061 model_free_row_data (button, &iter);
2064 if (g_file_is_native (file))
2066 gtk_list_store_set (store, &iter,
2068 DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
2069 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2070 DATA_COLUMN, g_object_ref (file),
2071 IS_FOLDER_COLUMN, FALSE,
2073 set_info_for_file_at_iter (button, file, &iter);
2078 GtkIconTheme *icon_theme;
2081 /* Don't call get_info for remote paths to avoid latency and
2083 * If we switch to a better bookmarks file format (XBEL), we
2084 * should use mime info to get a better icon.
2086 label = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2088 label = _gtk_file_chooser_label_for_file (file);
2090 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2092 if (g_file_is_native (file))
2093 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder",
2094 button->priv->icon_size, 0, NULL);
2096 pixbuf = gtk_icon_theme_load_icon (icon_theme, "folder-remote",
2097 button->priv->icon_size, 0, NULL);
2099 gtk_list_store_set (store, &iter,
2100 ICON_COLUMN, pixbuf,
2101 DISPLAY_NAME_COLUMN, label,
2102 TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2103 DATA_COLUMN, g_object_ref (file),
2104 IS_FOLDER_COLUMN, TRUE,
2108 g_object_unref (pixbuf);
2113 model_add_other (GtkFileChooserButton *button)
2115 GtkListStore *store;
2119 store = GTK_LIST_STORE (button->priv->model);
2120 pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2122 gtk_list_store_insert (store, &iter, pos);
2123 gtk_list_store_set (store, &iter,
2125 DISPLAY_NAME_COLUMN, NULL,
2126 TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2128 IS_FOLDER_COLUMN, FALSE,
2130 button->priv->has_other_separator = TRUE;
2133 gtk_list_store_insert (store, &iter, pos);
2134 gtk_list_store_set (store, &iter,
2136 DISPLAY_NAME_COLUMN, _("Other…"),
2137 TYPE_COLUMN, ROW_TYPE_OTHER,
2139 IS_FOLDER_COLUMN, FALSE,
2144 model_remove_rows (GtkFileChooserButton *button,
2148 GtkListStore *store;
2153 store = GTK_LIST_STORE (button->priv->model);
2159 if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2160 g_assert_not_reached ();
2162 model_free_row_data (button, &iter);
2163 gtk_list_store_remove (store, &iter);
2170 static inline gboolean
2171 test_if_file_is_visible (GtkFileSystem *fs,
2173 gboolean local_only,
2179 if (local_only && !g_file_is_native (file))
2189 filter_model_visible_func (GtkTreeModel *model,
2193 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2194 GtkFileChooserButtonPrivate *priv = button->priv;
2197 gboolean local_only, retval, is_folder;
2199 type = ROW_TYPE_INVALID;
2201 local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2203 gtk_tree_model_get (model, iter,
2206 IS_FOLDER_COLUMN, &is_folder,
2211 case ROW_TYPE_CURRENT_FOLDER:
2214 case ROW_TYPE_SPECIAL:
2215 case ROW_TYPE_SHORTCUT:
2216 case ROW_TYPE_BOOKMARK:
2217 retval = test_if_file_is_visible (priv->fs, data, local_only, is_folder);
2219 case ROW_TYPE_VOLUME:
2224 if (_gtk_file_system_volume_is_mounted (data))
2228 base_file = _gtk_file_system_volume_get_root (data);
2232 if (!g_file_is_native (base_file))
2234 g_object_unref (base_file);
2252 name_cell_data_func (GtkCellLayout *layout,
2253 GtkCellRenderer *cell,
2254 GtkTreeModel *model,
2261 gtk_tree_model_get (model, iter,
2265 if (type == ROW_TYPE_CURRENT_FOLDER)
2266 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2268 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2272 combo_box_row_separator_func (GtkTreeModel *model,
2276 gchar type = ROW_TYPE_INVALID;
2278 gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2280 return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2281 type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2282 type == ROW_TYPE_OTHER_SEPARATOR);
2286 update_combo_box (GtkFileChooserButton *button)
2288 GtkFileChooserButtonPrivate *priv = button->priv;
2293 gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2295 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2304 type = ROW_TYPE_INVALID;
2307 gtk_tree_model_get (priv->filter_model, &iter,
2314 case ROW_TYPE_SPECIAL:
2315 case ROW_TYPE_SHORTCUT:
2316 case ROW_TYPE_BOOKMARK:
2317 case ROW_TYPE_CURRENT_FOLDER:
2318 row_found = (files &&
2320 g_file_equal (data, files->data));
2322 case ROW_TYPE_VOLUME:
2326 base_file = _gtk_file_system_volume_get_root (data);
2329 row_found = (files &&
2331 g_file_equal (base_file, files->data));
2332 g_object_unref (base_file);
2343 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2344 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2346 g_signal_handler_unblock (priv->combo_box,
2347 priv->combo_box_changed_id);
2350 while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2352 /* If it hasn't been found already, update & select the current-folder row. */
2353 if (!row_found && files && files->data)
2355 GtkTreeIter filter_iter;
2358 model_update_current_folder (button, files->data);
2359 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2361 pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2362 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2364 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2365 &filter_iter, &iter);
2367 g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2368 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2369 g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2372 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2373 g_slist_free (files);
2378 update_label_get_info_cb (GCancellable *cancellable,
2380 const GError *error,
2383 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
2385 GtkFileChooserButton *button = data;
2386 GtkFileChooserButtonPrivate *priv = button->priv;
2388 if (cancellable != priv->update_button_cancellable)
2391 priv->update_button_cancellable = NULL;
2393 if (cancelled || error)
2396 gtk_label_set_text (GTK_LABEL (priv->label), g_file_info_get_display_name (info));
2398 pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (priv->image), priv->icon_size);
2401 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2403 priv->icon_size, 0, NULL);
2405 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2407 g_object_unref (pixbuf);
2410 g_object_unref (button);
2411 g_object_unref (cancellable);
2415 update_label_and_image (GtkFileChooserButton *button)
2417 GtkFileChooserButtonPrivate *priv = button->priv;
2421 files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->dialog));
2424 if (priv->update_button_cancellable)
2426 g_cancellable_cancel (priv->update_button_cancellable);
2427 priv->update_button_cancellable = NULL;
2430 if (files && files->data)
2433 GtkFileSystemVolume *volume = NULL;
2437 volume = _gtk_file_system_get_volume_for_file (priv->fs, file);
2442 base_file = _gtk_file_system_volume_get_root (volume);
2443 if (base_file && g_file_equal (base_file, file))
2447 label_text = _gtk_file_system_volume_get_display_name (volume);
2448 pixbuf = _gtk_file_system_volume_render_icon (volume,
2449 GTK_WIDGET (button),
2452 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2454 g_object_unref (pixbuf);
2458 g_object_unref (base_file);
2460 _gtk_file_system_volume_unref (volume);
2466 if (g_file_is_native (file))
2468 priv->update_button_cancellable =
2469 _gtk_file_system_get_info (priv->fs, file,
2470 "standard::icon,standard::display-name",
2471 update_label_get_info_cb,
2472 g_object_ref (button));
2478 label_text = _gtk_file_system_get_bookmark_label (button->priv->fs, file);
2479 pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2481 priv->icon_size, 0, NULL);
2482 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2484 g_object_unref (pixbuf);
2488 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
2489 g_slist_free (files);
2493 gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2494 g_free (label_text);
2498 gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2499 gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), NULL);
2504 /* ************************ *
2505 * Child Object Callbacks *
2506 * ************************ */
2510 fs_volumes_changed_cb (GtkFileSystem *fs,
2513 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2514 GtkFileChooserButtonPrivate *priv = button->priv;
2517 model_remove_rows (user_data,
2518 model_get_type_position (user_data, ROW_TYPE_VOLUME),
2521 priv->n_volumes = 0;
2523 volumes = _gtk_file_system_list_volumes (fs);
2524 model_add_volumes (user_data, volumes);
2525 g_slist_free (volumes);
2527 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2529 update_label_and_image (user_data);
2530 update_combo_box (user_data);
2534 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2537 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2538 GtkFileChooserButtonPrivate *priv = button->priv;
2541 bookmarks = _gtk_file_system_list_bookmarks (fs);
2542 model_remove_rows (user_data,
2543 model_get_type_position (user_data,
2544 ROW_TYPE_BOOKMARK_SEPARATOR),
2545 (priv->n_bookmarks + priv->has_bookmark_separator));
2546 priv->has_bookmark_separator = FALSE;
2547 priv->n_bookmarks = 0;
2548 model_add_bookmarks (user_data, bookmarks);
2549 g_slist_foreach (bookmarks, (GFunc) g_object_unref, NULL);
2550 g_slist_free (bookmarks);
2552 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2554 update_label_and_image (user_data);
2555 update_combo_box (user_data);
2560 open_dialog (GtkFileChooserButton *button)
2562 GtkFileChooserButtonPrivate *priv = button->priv;
2564 /* Setup the dialog parent to be chooser button's toplevel, and be modal
2566 if (!gtk_widget_get_visible (priv->dialog))
2568 GtkWidget *toplevel;
2570 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2572 if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
2574 if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2575 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2576 GTK_WINDOW (toplevel));
2578 gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2579 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2585 g_signal_handler_block (priv->dialog,
2586 priv->dialog_folder_changed_id);
2587 g_signal_handler_block (priv->dialog,
2588 priv->dialog_file_activated_id);
2589 g_signal_handler_block (priv->dialog,
2590 priv->dialog_selection_changed_id);
2592 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->dialog), priv->selection_while_inactive, NULL);
2594 priv->active = TRUE;
2597 gtk_widget_set_sensitive (priv->combo_box, FALSE);
2598 gtk_window_present (GTK_WINDOW (priv->dialog));
2603 combo_box_changed_cb (GtkComboBox *combo_box,
2608 if (gtk_combo_box_get_active_iter (combo_box, &iter))
2610 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2611 GtkFileChooserButtonPrivate *priv = button->priv;
2615 type = ROW_TYPE_INVALID;
2618 gtk_tree_model_get (priv->filter_model, &iter,
2625 case ROW_TYPE_SPECIAL:
2626 case ROW_TYPE_SHORTCUT:
2627 case ROW_TYPE_BOOKMARK:
2628 case ROW_TYPE_CURRENT_FOLDER:
2629 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2631 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2634 case ROW_TYPE_VOLUME:
2638 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2639 base_file = _gtk_file_system_volume_get_root (data);
2642 gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->dialog),
2644 g_object_unref (base_file);
2648 case ROW_TYPE_OTHER:
2649 open_dialog (user_data);
2659 button_clicked_cb (GtkButton *real_button,
2662 open_dialog (user_data);
2667 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2670 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2672 g_signal_emit_by_name (button, "current-folder-changed");
2676 dialog_file_activated_cb (GtkFileChooser *dialog,
2679 g_signal_emit_by_name (user_data, "file-activated");
2683 dialog_selection_changed_cb (GtkFileChooser *dialog,
2686 update_label_and_image (user_data);
2687 update_combo_box (user_data);
2688 g_signal_emit_by_name (user_data, "selection-changed");
2692 dialog_update_preview_cb (GtkFileChooser *dialog,
2695 g_signal_emit_by_name (user_data, "update-preview");
2699 dialog_notify_cb (GObject *dialog,
2705 iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2706 GTK_TYPE_FILE_CHOOSER);
2707 if (g_object_interface_find_property (iface, pspec->name))
2708 g_object_notify (user_data, pspec->name);
2710 if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2712 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2713 GtkFileChooserButtonPrivate *priv = button->priv;
2715 if (priv->has_current_folder)
2721 pos = model_get_type_position (user_data,
2722 ROW_TYPE_CURRENT_FOLDER);
2723 gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2726 gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2728 /* If the path isn't local but we're in local-only mode now, remove
2729 * the custom-folder row */
2730 if (data && g_file_is_native (G_FILE (data)) &&
2731 gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog)))
2734 model_remove_rows (user_data, pos, 2);
2738 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2739 update_combo_box (user_data);
2744 dialog_delete_event_cb (GtkWidget *dialog,
2748 g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2754 dialog_response_cb (GtkDialog *dialog,
2758 GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2759 GtkFileChooserButtonPrivate *priv = button->priv;
2761 if (response == GTK_RESPONSE_ACCEPT ||
2762 response == GTK_RESPONSE_OK)
2764 if (priv->selection_while_inactive)
2765 g_object_unref (priv->selection_while_inactive);
2767 priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->dialog));
2769 g_signal_emit_by_name (button, "current-folder-changed");
2770 g_signal_emit_by_name (button, "selection-changed");
2772 update_label_and_image (button);
2773 update_combo_box (button);
2775 else if (priv->selection_while_inactive)
2777 gtk_file_chooser_select_file (GTK_FILE_CHOOSER (dialog), priv->selection_while_inactive, NULL);
2780 gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (dialog));
2784 g_signal_handler_unblock (priv->dialog,
2785 priv->dialog_folder_changed_id);
2786 g_signal_handler_unblock (priv->dialog,
2787 priv->dialog_file_activated_id);
2788 g_signal_handler_unblock (priv->dialog,
2789 priv->dialog_selection_changed_id);
2790 priv->active = FALSE;
2793 gtk_widget_set_sensitive (priv->combo_box, TRUE);
2794 gtk_widget_hide (priv->dialog);
2796 if (response == GTK_RESPONSE_ACCEPT ||
2797 response == GTK_RESPONSE_OK)
2798 g_signal_emit (button, file_chooser_button_signals[FILE_SET], 0);
2802 /* ************************************************************************** *
2804 * ************************************************************************** */
2807 * gtk_file_chooser_button_new:
2808 * @title: the title of the browse dialog.
2809 * @action: the open mode for the widget.
2811 * Creates a new file-selecting button widget.
2813 * Returns: a new button widget.
2818 gtk_file_chooser_button_new (const gchar *title,
2819 GtkFileChooserAction action)
2821 g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2822 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2824 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2826 "title", (title ? title : _(DEFAULT_TITLE)),
2831 * gtk_file_chooser_button_new_with_dialog:
2832 * @dialog: the widget to use as dialog
2834 * Creates a #GtkFileChooserButton widget which uses @dialog as its
2835 * file-picking window.
2837 * Note that @dialog must be a #GtkDialog (or subclass) which
2838 * implements the #GtkFileChooser interface and must not have
2839 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2841 * Also note that the dialog needs to have its confirmative button
2842 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2843 * order for the button to take over the file selected in the dialog.
2845 * Returns: a new button widget.
2850 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2852 g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2854 return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2860 * gtk_file_chooser_button_set_title:
2861 * @button: the button widget to modify.
2862 * @title: the new browse dialog title.
2864 * Modifies the @title of the browse dialog used by @button.
2869 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2872 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2874 gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2875 g_object_notify (G_OBJECT (button), "title");
2879 * gtk_file_chooser_button_get_title:
2880 * @button: the button widget to examine.
2882 * Retrieves the title of the browse dialog used by @button. The returned value
2883 * should not be modified or freed.
2885 * Returns: a pointer to the browse dialog's title.
2890 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2892 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2894 return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2898 * gtk_file_chooser_button_get_width_chars:
2899 * @button: the button widget to examine.
2901 * Retrieves the width in characters of the @button widget's entry and/or label.
2903 * Returns: an integer width (in characters) that the button will use to size itself.
2908 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2910 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2912 return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2916 * gtk_file_chooser_button_set_width_chars:
2917 * @button: the button widget to examine.
2918 * @n_chars: the new width, in characters.
2920 * Sets the width (in characters) that @button will use to @n_chars.
2925 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
2928 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2930 gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
2931 g_object_notify (G_OBJECT (button), "width-chars");
2935 * gtk_file_chooser_button_set_focus_on_click:
2936 * @button: a #GtkFileChooserButton
2937 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2939 * Sets whether the button will grab focus when it is clicked with the mouse.
2940 * Making mouse clicks not grab focus is useful in places like toolbars where
2941 * you don't want the keyboard focus removed from the main area of the
2947 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
2948 gboolean focus_on_click)
2950 GtkFileChooserButtonPrivate *priv;
2952 g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2954 priv = button->priv;
2956 focus_on_click = focus_on_click != FALSE;
2958 if (priv->focus_on_click != focus_on_click)
2960 priv->focus_on_click = focus_on_click;
2961 gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
2962 gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
2964 g_object_notify (G_OBJECT (button), "focus-on-click");
2969 * gtk_file_chooser_button_get_focus_on_click:
2970 * @button: a #GtkFileChooserButton
2972 * Returns whether the button grabs focus when it is clicked with the mouse.
2973 * See gtk_file_chooser_button_set_focus_on_click().
2975 * Return value: %TRUE if the button grabs focus when it is clicked with
2981 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
2983 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
2985 return button->priv->focus_on_click;