]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserbutton.c
remove useless member "has_title" from the private struct and simply set
[~andy/gtk] / gtk / gtkfilechooserbutton.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */
2
3 /* GTK+: gtkfilechooserbutton.c
4  * 
5  * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <config.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #include <string.h>
32
33 #include "gtkintl.h"
34 #include "gtkbutton.h"
35 #include "gtkcelllayout.h"
36 #include "gtkcellrenderertext.h"
37 #include "gtkcellrendererpixbuf.h"
38 #include "gtkcombobox.h"
39 #include "gtkdnd.h"
40 #include "gtkicontheme.h"
41 #include "gtkiconfactory.h"
42 #include "gtkimage.h"
43 #include "gtklabel.h"
44 #include "gtkliststore.h"
45 #include "gtkstock.h"
46 #include "gtktreemodelfilter.h"
47 #include "gtkvseparator.h"
48 #include "gtkfilechooserdialog.h"
49 #include "gtkfilechooserprivate.h"
50 #include "gtkfilechooserutils.h"
51 #include "gtkmarshalers.h"
52
53 #include "gtkfilechooserbutton.h"
54
55 #ifdef G_OS_WIN32
56 #include "gtkfilesystemwin32.h"
57 #endif
58
59 #include "gtkprivate.h"
60 #include "gtkalias.h"
61
62 /* **************** *
63  *  Private Macros  *
64  * **************** */
65
66 #define GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_FILE_CHOOSER_BUTTON, GtkFileChooserButtonPrivate))
67
68 #define DEFAULT_TITLE           N_("Select A File")
69 #define DESKTOP_DISPLAY_NAME    N_("Desktop")
70 #define FALLBACK_DISPLAY_NAME   N_("(None)")
71 #define FALLBACK_ICON_NAME      "stock_unknown"
72 #define FALLBACK_ICON_SIZE      16
73
74
75 /* ********************** *
76  *  Private Enumerations  *
77  * ********************** */
78
79 /* Property IDs */
80 enum
81 {
82   PROP_0,
83
84   PROP_DIALOG,
85   PROP_FOCUS_ON_CLICK,
86   PROP_TITLE,
87   PROP_WIDTH_CHARS
88 };
89
90 /* Signals */
91 enum
92 {
93   FILE_SET,
94   LAST_SIGNAL
95 };
96
97 /* TreeModel Columns */
98 enum
99 {
100   ICON_COLUMN,
101   DISPLAY_NAME_COLUMN,
102   TYPE_COLUMN,
103   DATA_COLUMN,
104   IS_FOLDER_COLUMN,
105   HANDLE_COLUMN,
106   NUM_COLUMNS
107 };
108
109 /* TreeModel Row Types */
110 typedef enum
111 {
112   ROW_TYPE_SPECIAL,
113   ROW_TYPE_VOLUME,
114   ROW_TYPE_SHORTCUT,
115   ROW_TYPE_BOOKMARK_SEPARATOR,
116   ROW_TYPE_BOOKMARK,
117   ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
118   ROW_TYPE_CURRENT_FOLDER,
119   ROW_TYPE_OTHER_SEPARATOR,
120   ROW_TYPE_OTHER,
121
122   ROW_TYPE_INVALID = -1
123 }
124 RowType;
125
126
127 /* ******************** *
128  *  Private Structures  *
129  * ******************** */
130
131 struct _GtkFileChooserButtonPrivate
132 {
133   GtkWidget *dialog;
134   GtkWidget *button;
135   GtkWidget *image;
136   GtkWidget *label;
137   GtkWidget *combo_box;
138   GtkCellRenderer *icon_cell;
139   GtkCellRenderer *name_cell;
140
141   GtkTreeModel *model;
142   GtkTreeModel *filter_model;
143
144   gchar *backend;
145   GtkFileSystem *fs;
146   GtkFilePath *old_path;
147
148   gulong combo_box_changed_id;
149   gulong dialog_file_activated_id;
150   gulong dialog_folder_changed_id;
151   gulong dialog_selection_changed_id;
152   gulong fs_volumes_changed_id;
153   gulong fs_bookmarks_changed_id;
154
155   GtkFileSystemHandle *dnd_select_folder_handle;
156   GtkFileSystemHandle *update_button_handle;
157   GSList *change_icon_theme_handles;
158
159   gint icon_size;
160
161   guint8 n_special;
162   guint8 n_volumes;
163   guint8 n_shortcuts;
164   guint8 n_bookmarks;
165   guint8 has_bookmark_separator       : 1;
166   guint8 has_current_folder_separator : 1;
167   guint8 has_current_folder           : 1;
168   guint8 has_other_separator          : 1;
169
170   /* Used for hiding/showing the dialog when the button is hidden */
171   guint8 active                       : 1;
172
173   /* Used to track whether we need to set a default current folder on ::map() */
174   guint8 folder_has_been_set          : 1;
175
176   guint8 focus_on_click               : 1;
177 };
178
179
180 /* ************* *
181  *  DnD Support  *
182  * ************* */
183
184 enum
185 {
186   TEXT_PLAIN,
187   TEXT_URI_LIST
188 };
189
190
191 /* ********************* *
192  *  Function Prototypes  *
193  * ********************* */
194
195 /* GtkFileChooserIface Functions */
196 static void     gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
197 static gboolean gtk_file_chooser_button_add_shortcut_folder     (GtkFileChooser      *chooser,
198                                                                  const GtkFilePath   *path,
199                                                                  GError             **error);
200 static gboolean gtk_file_chooser_button_remove_shortcut_folder  (GtkFileChooser      *chooser,
201                                                                  const GtkFilePath   *path,
202                                                                  GError             **error);
203
204 /* GObject Functions */
205 static GObject *gtk_file_chooser_button_constructor        (GType             type,
206                                                             guint             n_params,
207                                                             GObjectConstructParam *params);
208 static void     gtk_file_chooser_button_set_property       (GObject          *object,
209                                                             guint             param_id,
210                                                             const GValue     *value,
211                                                             GParamSpec       *pspec);
212 static void     gtk_file_chooser_button_get_property       (GObject          *object,
213                                                             guint             param_id,
214                                                             GValue           *value,
215                                                             GParamSpec       *pspec);
216 static void     gtk_file_chooser_button_finalize           (GObject          *object);
217
218 /* GtkObject Functions */
219 static void     gtk_file_chooser_button_destroy            (GtkObject        *object);
220
221 /* GtkWidget Functions */
222 static void     gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
223                                                             GdkDragContext   *context,
224                                                             gint              x,
225                                                             gint              y,
226                                                             GtkSelectionData *data,
227                                                             guint             type,
228                                                             guint             drag_time);
229 static void     gtk_file_chooser_button_show_all           (GtkWidget        *widget);
230 static void     gtk_file_chooser_button_hide_all           (GtkWidget        *widget);
231 static void     gtk_file_chooser_button_show               (GtkWidget        *widget);
232 static void     gtk_file_chooser_button_hide               (GtkWidget        *widget);
233 static void     gtk_file_chooser_button_map                (GtkWidget        *widget);
234 static gboolean gtk_file_chooser_button_mnemonic_activate  (GtkWidget        *widget,
235                                                             gboolean          group_cycling);
236 static void     gtk_file_chooser_button_style_set          (GtkWidget        *widget,
237                                                             GtkStyle         *old_style);
238 static void     gtk_file_chooser_button_screen_changed     (GtkWidget        *widget,
239                                                             GdkScreen        *old_screen);
240
241 /* Utility Functions */
242 static GtkIconTheme *get_icon_theme               (GtkWidget            *widget);
243 static void          set_info_for_path_at_iter         (GtkFileChooserButton *fs,
244                                                         const GtkFilePath    *path,
245                                                         GtkTreeIter          *iter);
246
247 static gint          model_get_type_position      (GtkFileChooserButton *button,
248                                                    RowType               row_type);
249 static void          model_free_row_data          (GtkFileChooserButton *button,
250                                                    GtkTreeIter          *iter);
251 static inline void   model_add_special            (GtkFileChooserButton *button);
252 static inline void   model_add_other              (GtkFileChooserButton *button);
253 static void          model_add_volumes            (GtkFileChooserButton *button,
254                                                    GSList               *volumes);
255 static void          model_add_bookmarks          (GtkFileChooserButton *button,
256                                                    GSList               *bookmarks);
257 static void          model_update_current_folder  (GtkFileChooserButton *button,
258                                                    const GtkFilePath    *path);
259 static void          model_remove_rows            (GtkFileChooserButton *button,
260                                                    gint                  pos,
261                                                    gint                  n_rows);
262
263 static gboolean      filter_model_visible_func    (GtkTreeModel         *model,
264                                                    GtkTreeIter          *iter,
265                                                    gpointer              user_data);
266
267 static gboolean      combo_box_row_separator_func (GtkTreeModel         *model,
268                                                    GtkTreeIter          *iter,
269                                                    gpointer              user_data);
270 static void          name_cell_data_func          (GtkCellLayout        *layout,
271                                                    GtkCellRenderer      *cell,
272                                                    GtkTreeModel         *model,
273                                                    GtkTreeIter          *iter,
274                                                    gpointer              user_data);
275 static void          open_dialog                  (GtkFileChooserButton *button);
276 static void          update_combo_box             (GtkFileChooserButton *button);
277 static void          update_label_and_image       (GtkFileChooserButton *button);
278
279 /* Child Object Callbacks */
280 static void     fs_volumes_changed_cb            (GtkFileSystem  *fs,
281                                                   gpointer        user_data);
282 static void     fs_bookmarks_changed_cb          (GtkFileSystem  *fs,
283                                                   gpointer        user_data);
284
285 static void     combo_box_changed_cb             (GtkComboBox    *combo_box,
286                                                   gpointer        user_data);
287
288 static void     button_clicked_cb                (GtkButton      *real_button,
289                                                   gpointer        user_data);
290
291 static void     dialog_update_preview_cb         (GtkFileChooser *dialog,
292                                                   gpointer        user_data);
293 static void     dialog_selection_changed_cb      (GtkFileChooser *dialog,
294                                                   gpointer        user_data);
295 static void     dialog_file_activated_cb         (GtkFileChooser *dialog,
296                                                   gpointer        user_data);
297 static void     dialog_current_folder_changed_cb (GtkFileChooser *dialog,
298                                                   gpointer        user_data);
299 static void     dialog_notify_cb                 (GObject        *dialog,
300                                                   GParamSpec     *pspec,
301                                                   gpointer        user_data);
302 static gboolean dialog_delete_event_cb           (GtkWidget      *dialog,
303                                                   GdkEvent       *event,
304                                                   gpointer        user_data);
305 static void     dialog_response_cb               (GtkDialog      *dialog,
306                                                   gint            response,
307                                                   gpointer        user_data);
308
309 static guint file_chooser_button_signals[LAST_SIGNAL] = { 0 };
310
311 /* ******************* *
312  *  GType Declaration  *
313  * ******************* */
314
315 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_HBOX, { \
316     G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
317 })
318
319
320 /* ***************** *
321  *  GType Functions  *
322  * ***************** */
323
324 static void
325 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
326 {
327   GObjectClass *gobject_class;
328   GtkObjectClass *gtkobject_class;
329   GtkWidgetClass *widget_class;
330
331   gobject_class = G_OBJECT_CLASS (class);
332   gtkobject_class = GTK_OBJECT_CLASS (class);
333   widget_class = GTK_WIDGET_CLASS (class);
334
335   gobject_class->constructor = gtk_file_chooser_button_constructor;
336   gobject_class->set_property = gtk_file_chooser_button_set_property;
337   gobject_class->get_property = gtk_file_chooser_button_get_property;
338   gobject_class->finalize = gtk_file_chooser_button_finalize;
339
340   gtkobject_class->destroy = gtk_file_chooser_button_destroy;
341
342   widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
343   widget_class->show_all = gtk_file_chooser_button_show_all;
344   widget_class->hide_all = gtk_file_chooser_button_hide_all;
345   widget_class->show = gtk_file_chooser_button_show;
346   widget_class->hide = gtk_file_chooser_button_hide;
347   widget_class->map = gtk_file_chooser_button_map;
348   widget_class->style_set = gtk_file_chooser_button_style_set;
349   widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
350   widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
351   
352   /**
353    * GtkFileChooserButtons::file-set:
354    * @widget: the object which received the signal.
355    *
356    * The ::file-set signal is emitted when the user selects a file.
357    * 
358    * Note that this signal is only emitted when the <emphasis>user</emphasis>
359    * changes the file. 
360    *
361    * Since: 2.12
362    */
363   file_chooser_button_signals[FILE_SET] =
364     g_signal_new (I_("file-set"),
365                   G_TYPE_FROM_CLASS (gobject_class),
366                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
367                   G_STRUCT_OFFSET (GtkFileChooserButtonClass, file_set),
368                   NULL, NULL,
369                   _gtk_marshal_VOID__VOID,
370                   G_TYPE_NONE, 0);
371
372   /**
373    * GtkFileChooserButton:dialog:
374    * 
375    * Instance of the #GtkFileChooserDialog associated with the button.
376    *
377    * Since: 2.6
378    */
379   g_object_class_install_property (gobject_class, PROP_DIALOG,
380                                    g_param_spec_object ("dialog",
381                                                         P_("Dialog"),
382                                                         P_("The file chooser dialog to use."),
383                                                         GTK_TYPE_FILE_CHOOSER,
384                                                         (GTK_PARAM_WRITABLE |
385                                                          G_PARAM_CONSTRUCT_ONLY)));
386
387   /**
388    * GtkFileChooserButton:focus-on-click:
389    * 
390    * Whether the #GtkFileChooserButton button grabs focus when it is clicked
391    * with the mouse.
392    *
393    * Since: 2.10
394    */
395   g_object_class_install_property (gobject_class,
396                                    PROP_FOCUS_ON_CLICK,
397                                    g_param_spec_boolean ("focus-on-click",
398                                                          P_("Focus on click"),
399                                                          P_("Whether the button grabs focus when it is clicked with the mouse"),
400                                                          TRUE,
401                                                          GTK_PARAM_READWRITE));
402   
403   /**
404    * GtkFileChooserButton:title:
405    * 
406    * Title to put on the #GtkFileChooserDialog associated with the button.
407    *
408    * Since: 2.6
409    */
410   g_object_class_install_property (gobject_class, PROP_TITLE,
411                                    g_param_spec_string ("title",
412                                                         P_("Title"),
413                                                         P_("The title of the file chooser dialog."),
414                                                         _(DEFAULT_TITLE),
415                                                         GTK_PARAM_READWRITE));
416
417   /**
418    * GtkFileChooserButton:width-chars:
419    * 
420    * The width of the entry and label inside the button, in characters.
421    *
422    * Since: 2.6
423    */
424   g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
425                                    g_param_spec_int ("width-chars",
426                                                      P_("Width In Characters"),
427                                                      P_("The desired width of the button widget, in characters."),
428                                                      -1, G_MAXINT, -1,
429                                                      GTK_PARAM_READWRITE));
430
431   _gtk_file_chooser_install_properties (gobject_class);
432
433   g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
434 }
435
436 static void
437 gtk_file_chooser_button_init (GtkFileChooserButton *button)
438 {
439   GtkFileChooserButtonPrivate *priv;
440   GtkWidget *box, *image, *sep;
441   GtkTargetList *target_list;
442
443   priv = button->priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (button);
444
445   priv->icon_size = FALLBACK_ICON_SIZE;
446   priv->focus_on_click = TRUE;
447
448   gtk_widget_push_composite_child ();
449
450   /* Button */
451   priv->button = gtk_button_new ();
452   g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb),
453                     button);
454   gtk_container_add (GTK_CONTAINER (button), priv->button);
455   gtk_widget_show (priv->button);
456
457   box = gtk_hbox_new (FALSE, 4);
458   gtk_container_add (GTK_CONTAINER (priv->button), box);
459   gtk_widget_show (box);
460
461   priv->image = gtk_image_new ();
462   gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
463   gtk_widget_show (priv->image);
464
465   priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
466   gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
467   gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
468   gtk_container_add (GTK_CONTAINER (box), priv->label);
469   gtk_widget_show (priv->label);
470
471   sep = gtk_vseparator_new ();
472   gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
473   gtk_widget_show (sep);
474
475   image = gtk_image_new_from_stock (GTK_STOCK_OPEN,
476                                     GTK_ICON_SIZE_MENU);
477   gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
478   gtk_widget_show (image);
479
480   /* Combo Box */
481   /* Keep in sync with columns enum, line 88 */
482   priv->model =
483     GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
484                                         GDK_TYPE_PIXBUF, /* Icon */
485                                         G_TYPE_STRING,   /* Display Name */
486                                         G_TYPE_CHAR,     /* Row Type */
487                                         G_TYPE_POINTER   /* Volume || Path */,
488                                         G_TYPE_BOOLEAN   /* Is Folder? */,
489                                         G_TYPE_POINTER   /* handle */));
490
491   priv->combo_box = gtk_combo_box_new ();
492   priv->combo_box_changed_id =
493     g_signal_connect (priv->combo_box, "changed",
494                       G_CALLBACK (combo_box_changed_cb), button);
495   gtk_container_add (GTK_CONTAINER (button), priv->combo_box);
496
497   priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
498   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
499                               priv->icon_cell, FALSE);
500   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
501                                  priv->icon_cell, "pixbuf", ICON_COLUMN);
502
503   priv->name_cell = gtk_cell_renderer_text_new ();
504   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
505                               priv->name_cell, TRUE);
506   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
507                                  priv->name_cell, "text", DISPLAY_NAME_COLUMN);
508   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
509                                       priv->name_cell, name_cell_data_func,
510                                       NULL, NULL);
511
512   gtk_widget_pop_composite_child ();
513
514   /* DnD */
515   gtk_drag_dest_set (GTK_WIDGET (button),
516                      (GTK_DEST_DEFAULT_ALL),
517                      NULL, 0,
518                      GDK_ACTION_COPY);
519   target_list = gtk_target_list_new (NULL, 0);
520   gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
521   gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
522   gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
523   gtk_target_list_unref (target_list);
524 }
525
526
527 /* ******************************* *
528  *  GtkFileChooserIface Functions  *
529  * ******************************* */
530 static void
531 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
532 {
533   _gtk_file_chooser_delegate_iface_init (iface);
534
535   iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
536   iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
537 }
538
539 static gboolean
540 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser     *chooser,
541                                              const GtkFilePath  *path,
542                                              GError            **error)
543 {
544   GtkFileChooser *delegate;
545   gboolean retval;
546
547   delegate = g_object_get_qdata (G_OBJECT (chooser),
548                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
549   retval = _gtk_file_chooser_add_shortcut_folder (delegate, path, error);
550
551   if (retval)
552     {
553       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
554       GtkFileChooserButtonPrivate *priv = button->priv;
555       GtkTreeIter iter;
556       gint pos;
557
558       pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
559       pos += priv->n_shortcuts;
560
561       gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
562       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
563                           ICON_COLUMN, NULL,
564                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
565                           TYPE_COLUMN, ROW_TYPE_SHORTCUT,
566                           DATA_COLUMN, gtk_file_path_copy (path),
567                           IS_FOLDER_COLUMN, FALSE,
568                           -1);
569       set_info_for_path_at_iter (button, path, &iter);
570       priv->n_shortcuts++;
571
572       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
573     }
574
575   return retval;
576 }
577
578 static gboolean
579 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser     *chooser,
580                                                 const GtkFilePath  *path,
581                                                 GError            **error)
582 {
583   GtkFileChooser *delegate;
584   gboolean retval;
585
586   delegate = g_object_get_qdata (G_OBJECT (chooser),
587                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
588
589   retval = _gtk_file_chooser_remove_shortcut_folder (delegate, path, error);
590
591   if (retval)
592     {
593       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (chooser);
594       GtkFileChooserButtonPrivate *priv = button->priv;
595       GtkTreeIter iter;
596       gint pos;
597       gchar type;
598
599       pos = model_get_type_position (button, ROW_TYPE_SHORTCUT);
600       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
601
602       do
603         {
604           gpointer data;
605
606           gtk_tree_model_get (priv->model, &iter,
607                               TYPE_COLUMN, &type,
608                               DATA_COLUMN, &data,
609                               -1);
610
611           if (type == ROW_TYPE_SHORTCUT &&
612               data &&
613               gtk_file_path_compare (data, path) == 0)
614             {
615               model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
616               gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
617               priv->n_shortcuts--;
618               gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
619               update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
620               break;
621             }
622         }
623       while (type == ROW_TYPE_SHORTCUT &&
624              gtk_tree_model_iter_next (priv->model, &iter));
625     }
626
627   return retval;
628 }
629
630
631 /* ******************* *
632  *  GObject Functions  *
633  * ******************* */
634
635 static GObject *
636 gtk_file_chooser_button_constructor (GType                  type,
637                                      guint                  n_params,
638                                      GObjectConstructParam *params)
639 {
640   GObject *object;
641   GtkFileChooserButton *button;
642   GtkFileChooserButtonPrivate *priv;
643   GSList *list;
644   char *current_folder;
645
646   object = (*G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor) (type,
647                                                                                   n_params,
648                                                                                   params);
649   button = GTK_FILE_CHOOSER_BUTTON (object);
650   priv = button->priv;
651
652   if (!priv->dialog)
653     {
654       if (priv->backend)
655         priv->dialog = gtk_file_chooser_dialog_new_with_backend (NULL, NULL,
656                                                                  GTK_FILE_CHOOSER_ACTION_OPEN,
657                                                                  priv->backend,
658                                                                  GTK_STOCK_CANCEL,
659                                                                  GTK_RESPONSE_CANCEL,
660                                                                  GTK_STOCK_OPEN,
661                                                                  GTK_RESPONSE_ACCEPT,
662                                                                  NULL);
663       else
664         priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
665                                                     GTK_FILE_CHOOSER_ACTION_OPEN,
666                                                     GTK_STOCK_CANCEL,
667                                                     GTK_RESPONSE_CANCEL,
668                                                     GTK_STOCK_OPEN,
669                                                     GTK_RESPONSE_ACCEPT,
670                                                     NULL);
671
672       gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
673                                        GTK_RESPONSE_ACCEPT);
674       gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
675                                                GTK_RESPONSE_ACCEPT,
676                                                GTK_RESPONSE_CANCEL,
677                                                -1);
678
679       gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
680     }
681   else if (!GTK_WINDOW (priv->dialog)->title)
682     {
683       gtk_file_chooser_button_set_title (button, _(DEFAULT_TITLE));
684     }
685
686   current_folder = gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (priv->dialog));
687   if (current_folder != NULL)
688     {
689       priv->folder_has_been_set = TRUE;
690       g_free (current_folder);
691     }
692
693   g_free (priv->backend);
694   priv->backend = NULL;
695
696   g_signal_connect (priv->dialog, "delete_event",
697                     G_CALLBACK (dialog_delete_event_cb), object);
698   g_signal_connect (priv->dialog, "response",
699                     G_CALLBACK (dialog_response_cb), object);
700
701   /* This is used, instead of the standard delegate, to ensure that signals are only
702    * delegated when the OK button is pressed. */
703   g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
704   priv->dialog_folder_changed_id =
705     g_signal_connect (priv->dialog, "current-folder-changed",
706                       G_CALLBACK (dialog_current_folder_changed_cb), object);
707   priv->dialog_file_activated_id =
708     g_signal_connect (priv->dialog, "file-activated",
709                       G_CALLBACK (dialog_file_activated_cb), object);
710   priv->dialog_selection_changed_id =
711     g_signal_connect (priv->dialog, "selection-changed",
712                       G_CALLBACK (dialog_selection_changed_cb), object);
713   g_signal_connect (priv->dialog, "update-preview",
714                     G_CALLBACK (dialog_update_preview_cb), object);
715   g_signal_connect (priv->dialog, "notify",
716                     G_CALLBACK (dialog_notify_cb), object);
717   g_object_add_weak_pointer (G_OBJECT (priv->dialog),
718                              (gpointer) (&priv->dialog));
719
720   priv->fs =
721     g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
722
723   model_add_special (button);
724
725   list = gtk_file_system_list_volumes (priv->fs);
726   model_add_volumes (button, list);
727   g_slist_free (list);
728
729   list = gtk_file_system_list_bookmarks (priv->fs);
730   model_add_bookmarks (button, list);
731   gtk_file_paths_free (list);
732
733   model_add_other (button);
734
735   priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
736   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
737                                           filter_model_visible_func,
738                                           object, NULL);
739
740   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
741   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
742                                         combo_box_row_separator_func,
743                                         NULL, NULL);
744
745   /* set up the action for a user-provided dialog, this also updates
746    * the label, image and combobox
747    */
748   g_object_set (object, 
749                 "action", gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)),
750                 NULL);
751
752   priv->fs_volumes_changed_id =
753     g_signal_connect (priv->fs, "volumes-changed",
754                       G_CALLBACK (fs_volumes_changed_cb), object);
755   priv->fs_bookmarks_changed_id =
756     g_signal_connect (priv->fs, "bookmarks-changed",
757                       G_CALLBACK (fs_bookmarks_changed_cb), object);
758
759   return object;
760 }
761
762 static void
763 gtk_file_chooser_button_set_property (GObject      *object,
764                                       guint         param_id,
765                                       const GValue *value,
766                                       GParamSpec   *pspec)
767 {
768   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
769   GtkFileChooserButtonPrivate *priv = button->priv;
770
771   switch (param_id)
772     {
773     case PROP_DIALOG:
774       /* Construct-only */
775       priv->dialog = g_value_get_object (value);
776       break;
777     case PROP_FOCUS_ON_CLICK:
778       gtk_file_chooser_button_set_focus_on_click (button, g_value_get_boolean (value));
779       break;
780     case PROP_WIDTH_CHARS:
781       gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
782                                                g_value_get_int (value));
783       break;
784     case GTK_FILE_CHOOSER_PROP_ACTION:
785       switch (g_value_get_enum (value))
786         {
787         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
788         case GTK_FILE_CHOOSER_ACTION_SAVE:
789           {
790             GEnumClass *eclass;
791             GEnumValue *eval;
792
793             eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
794             eval = g_enum_get_value (eclass, g_value_get_enum (value));
795             g_warning ("%s: Choosers of type `%s' do not support `%s'.",
796                        G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
797
798             g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
799           }
800           break;
801         }
802
803       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
804       update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
805       update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
806
807       switch (g_value_get_enum (value))
808         {
809         case GTK_FILE_CHOOSER_ACTION_OPEN:
810           gtk_widget_hide (priv->combo_box);
811           gtk_widget_show (priv->button);
812           break;
813         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
814           gtk_widget_hide (priv->button);
815           gtk_widget_show (priv->combo_box);
816           break;
817         default:
818           g_assert_not_reached ();
819           break;
820         }
821       break;
822
823     case PROP_TITLE:
824     case GTK_FILE_CHOOSER_PROP_FILTER:
825     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
826     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
827     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
828     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
829     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
830     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
831       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
832       break;
833
834     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
835       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
836       fs_volumes_changed_cb (priv->fs, button);
837       fs_bookmarks_changed_cb (priv->fs, button);
838       break;
839
840     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
841       /* Construct-only */
842       priv->backend = g_value_dup_string (value);
843       break;
844
845     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
846       g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
847                  G_STRFUNC, G_OBJECT_TYPE_NAME (object));
848       break;
849     default:
850       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
851       break;
852     }
853 }
854
855 static void
856 gtk_file_chooser_button_get_property (GObject    *object,
857                                       guint       param_id,
858                                       GValue     *value,
859                                       GParamSpec *pspec)
860 {
861   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
862   GtkFileChooserButtonPrivate *priv = button->priv;
863
864   switch (param_id)
865     {
866     case PROP_WIDTH_CHARS:
867       g_value_set_int (value,
868                        gtk_label_get_width_chars (GTK_LABEL (priv->label)));
869       break;
870     case PROP_FOCUS_ON_CLICK:
871       g_value_set_boolean (value,
872                            gtk_file_chooser_button_get_focus_on_click (button));
873       break;
874
875     case PROP_TITLE:
876     case GTK_FILE_CHOOSER_PROP_ACTION:
877     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
878     case GTK_FILE_CHOOSER_PROP_FILTER:
879     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
880     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
881     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
882     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
883     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
884     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
885     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
886     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
887       g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
888       break;
889
890     default:
891       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
892       break;
893     }
894 }
895
896 static void
897 gtk_file_chooser_button_finalize (GObject *object)
898 {
899   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
900   GtkFileChooserButtonPrivate *priv = button->priv;
901
902   if (priv->old_path)
903     gtk_file_path_free (priv->old_path);
904
905   if (G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize != NULL)
906     (*G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize) (object);
907 }
908
909 /* ********************* *
910  *  GtkObject Functions  *
911  * ********************* */
912
913 static void
914 gtk_file_chooser_button_destroy (GtkObject *object)
915 {
916   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
917   GtkFileChooserButtonPrivate *priv = button->priv;
918   GtkTreeIter iter;
919   GSList *l;
920
921   if (priv->dialog != NULL)
922     {
923       gtk_widget_destroy (priv->dialog);
924       priv->dialog = NULL;
925     }
926
927   if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
928     {
929       model_free_row_data (button, &iter);
930     }
931   while (gtk_tree_model_iter_next (priv->model, &iter));
932
933   if (priv->dnd_select_folder_handle)
934     {
935       gtk_file_system_cancel_operation (priv->dnd_select_folder_handle);
936       priv->dnd_select_folder_handle = NULL;
937     }
938
939   if (priv->update_button_handle)
940     {
941       gtk_file_system_cancel_operation (priv->update_button_handle);
942       priv->update_button_handle = NULL;
943     }
944
945   if (priv->change_icon_theme_handles)
946     {
947       for (l = priv->change_icon_theme_handles; l; l = l->next)
948         {
949           GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
950           gtk_file_system_cancel_operation (handle);
951         }
952       g_slist_free (priv->change_icon_theme_handles);
953       priv->change_icon_theme_handles = NULL;
954     }
955
956   if (priv->model)
957     {
958       g_object_unref (priv->model);
959       priv->model = NULL;
960     }
961
962   if (priv->filter_model)
963     {
964       g_object_unref (priv->filter_model);
965       priv->filter_model = NULL;
966     }
967
968   if (priv->fs)
969     {
970       g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
971       g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
972       g_object_unref (priv->fs);
973       priv->fs = NULL;
974     }
975
976   if (GTK_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->destroy != NULL)
977     (*GTK_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->destroy) (object);
978 }
979
980
981 /* ********************* *
982  *  GtkWidget Functions  *
983  * ********************* */
984
985 struct DndSelectFolderData
986 {
987   GtkFileChooserButton *button;
988   GtkFileChooserAction action;
989   GtkFilePath *path;
990   gchar **uris;
991   guint i;
992   gboolean selected;
993 };
994
995 static void
996 dnd_select_folder_get_info_cb (GtkFileSystemHandle *handle,
997                                const GtkFileInfo   *info,
998                                const GError        *error,
999                                gpointer             user_data)
1000 {
1001   gboolean cancelled = handle->cancelled;
1002   struct DndSelectFolderData *data = user_data;
1003
1004   if (handle != data->button->priv->dnd_select_folder_handle)
1005     {
1006       g_object_unref (data->button);
1007       gtk_file_path_free (data->path);
1008       g_strfreev (data->uris);
1009       g_free (data);
1010
1011       g_object_unref (handle);
1012       return;
1013     }
1014
1015   data->button->priv->dnd_select_folder_handle = NULL;
1016
1017   if (!cancelled && !error && info != NULL)
1018     {
1019       data->selected = 
1020         (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
1021            gtk_file_info_get_is_folder (info)) ||
1022           (data->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
1023            !gtk_file_info_get_is_folder (info))) &&
1024          _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (data->button->priv->dialog),
1025                                         data->path, NULL));
1026     }
1027   else
1028     data->selected = FALSE;
1029
1030   if (data->selected || data->uris[++data->i] == NULL)
1031     {
1032       g_object_unref (data->button);
1033       gtk_file_path_free (data->path);
1034       g_strfreev (data->uris);
1035       g_free (data);
1036
1037       g_object_unref (handle);
1038       return;
1039     }
1040
1041   if (data->path)
1042     gtk_file_path_free (data->path);
1043
1044   data->path = gtk_file_system_uri_to_path (handle->file_system,
1045                                             data->uris[data->i]);
1046
1047   data->button->priv->dnd_select_folder_handle =
1048     gtk_file_system_get_info (handle->file_system, data->path,
1049                               GTK_FILE_INFO_IS_FOLDER,
1050                               dnd_select_folder_get_info_cb, user_data);
1051
1052   g_object_unref (handle);
1053 }
1054
1055 static void
1056 gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
1057                                             GdkDragContext   *context,
1058                                             gint              x,
1059                                             gint              y,
1060                                             GtkSelectionData *data,
1061                                             guint             type,
1062                                             guint             drag_time)
1063 {
1064   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1065   GtkFileChooserButtonPrivate *priv = button->priv;
1066   GtkFilePath *path;
1067   gchar *text;
1068
1069   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1070     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received) (widget,
1071                                                                                     context,
1072                                                                                     x, y,
1073                                                                                     data, type,
1074                                                                                     drag_time);
1075
1076   if (widget == NULL || context == NULL || data == NULL || data->length < 0)
1077     return;
1078
1079   switch (type)
1080     {
1081     case TEXT_URI_LIST:
1082       {
1083         gchar **uris;
1084         struct DndSelectFolderData *info;
1085
1086         uris = gtk_selection_data_get_uris (data);
1087         
1088         if (uris == NULL)
1089           break;
1090
1091         info = g_new0 (struct DndSelectFolderData, 1);
1092         info->button = g_object_ref (button);
1093         info->i = 0;
1094         info->uris = uris;
1095         info->selected = FALSE;
1096         g_object_get (priv->dialog, "action", &info->action, NULL);
1097
1098         info->path = gtk_file_system_uri_to_path (priv->fs,
1099                                                   info->uris[info->i]);
1100
1101         if (priv->dnd_select_folder_handle)
1102           gtk_file_system_cancel_operation (priv->dnd_select_folder_handle);
1103
1104         priv->dnd_select_folder_handle =
1105           gtk_file_system_get_info (priv->fs, info->path,
1106                                     GTK_FILE_INFO_IS_FOLDER,
1107                                     dnd_select_folder_get_info_cb, info);
1108       }
1109       break;
1110
1111     case TEXT_PLAIN:
1112       text = (char*) gtk_selection_data_get_text (data);
1113       path = gtk_file_path_new_steal (text);
1114       _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (priv->dialog), path,
1115                                      NULL);
1116       gtk_file_path_free (path);
1117       break;
1118
1119     default:
1120       break;
1121     }
1122
1123   gtk_drag_finish (context, TRUE, FALSE, drag_time);
1124 }
1125
1126 static void
1127 gtk_file_chooser_button_show_all (GtkWidget *widget)
1128 {
1129   gtk_widget_show (widget);
1130 }
1131
1132 static void
1133 gtk_file_chooser_button_hide_all (GtkWidget *widget)
1134 {
1135   gtk_widget_hide (widget);
1136 }
1137
1138 static void
1139 gtk_file_chooser_button_show (GtkWidget *widget)
1140 {
1141   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1142   GtkFileChooserButtonPrivate *priv = button->priv;
1143
1144   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1145     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show) (widget);
1146
1147   if (priv->active)
1148     open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1149 }
1150
1151 static void
1152 gtk_file_chooser_button_hide (GtkWidget *widget)
1153 {
1154   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1155   GtkFileChooserButtonPrivate *priv = button->priv;
1156
1157   gtk_widget_hide (priv->dialog);
1158
1159   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1160     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide) (widget);
1161 }
1162
1163 static void
1164 gtk_file_chooser_button_map (GtkWidget *widget)
1165 {
1166   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1167   GtkFileChooserButtonPrivate *priv = button->priv;
1168
1169   if (!priv->folder_has_been_set)
1170     {
1171       char *current_working_dir;
1172
1173       current_working_dir = g_get_current_dir ();
1174       gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), current_working_dir);
1175       g_free (current_working_dir);
1176
1177       priv->folder_has_been_set = TRUE;
1178     }
1179
1180   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map)
1181     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map) (widget);
1182 }
1183
1184 static gboolean
1185 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1186                                            gboolean   group_cycling)
1187 {
1188   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1189   GtkFileChooserButtonPrivate *priv = button->priv;
1190
1191   switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1192     {
1193     case GTK_FILE_CHOOSER_ACTION_OPEN:
1194       gtk_widget_grab_focus (priv->button);
1195       break;
1196     case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1197       return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1198       break;
1199     default:
1200       g_assert_not_reached ();
1201       break;
1202     }
1203
1204   return TRUE;
1205 }
1206
1207 /* Changes the icons wherever it is needed */
1208 struct ChangeIconThemeData
1209 {
1210   GtkFileChooserButton *button;
1211   GtkTreeRowReference *row_ref;
1212 };
1213
1214 static void
1215 change_icon_theme_get_info_cb (GtkFileSystemHandle *handle,
1216                                const GtkFileInfo   *info,
1217                                const GError        *error,
1218                                gpointer             user_data)
1219 {
1220   gboolean cancelled = handle->cancelled;
1221   GdkPixbuf *pixbuf;
1222   struct ChangeIconThemeData *data = user_data;
1223
1224   if (!g_slist_find (data->button->priv->change_icon_theme_handles, handle))
1225     goto out;
1226
1227   data->button->priv->change_icon_theme_handles =
1228     g_slist_remove (data->button->priv->change_icon_theme_handles, handle);
1229
1230   if (cancelled || error)
1231     goto out;
1232
1233   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1234                                       data->button->priv->icon_size, NULL);
1235
1236   if (pixbuf)
1237     {
1238       gint width = 0;
1239       GtkTreeIter iter;
1240       GtkTreePath *path;
1241
1242       width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1243
1244       path = gtk_tree_row_reference_get_path (data->row_ref);
1245       if (path) 
1246         {
1247           gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1248           gtk_tree_path_free (path);
1249
1250           gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1251                               ICON_COLUMN, pixbuf,
1252                               -1);
1253
1254           g_object_set (data->button->priv->icon_cell,
1255                         "width", width,
1256                         NULL);
1257         }
1258       g_object_unref (pixbuf);
1259     }
1260
1261 out:
1262   g_object_unref (data->button);
1263   gtk_tree_row_reference_free (data->row_ref);
1264   g_free (data);
1265
1266   g_object_unref (handle);
1267 }
1268
1269 static void
1270 change_icon_theme (GtkFileChooserButton *button)
1271 {
1272   GtkFileChooserButtonPrivate *priv = button->priv;
1273   GtkSettings *settings;
1274   GtkIconTheme *theme;
1275   GtkTreeIter iter;
1276   GSList *l;
1277   gint width = 0, height = 0;
1278
1279   for (l = button->priv->change_icon_theme_handles; l; l = l->next)
1280     {
1281       GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1282       gtk_file_system_cancel_operation (handle);
1283     }
1284   g_slist_free (button->priv->change_icon_theme_handles);
1285   button->priv->change_icon_theme_handles = NULL;
1286
1287   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1288
1289   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1290                                          &width, &height))
1291     priv->icon_size = MAX (width, height);
1292   else
1293     priv->icon_size = FALLBACK_ICON_SIZE;
1294
1295   update_label_and_image (button);
1296
1297   gtk_tree_model_get_iter_first (priv->model, &iter);
1298
1299   theme = get_icon_theme (GTK_WIDGET (button));
1300
1301   do
1302     {
1303       GdkPixbuf *pixbuf;
1304       gchar type;
1305       gpointer data;
1306
1307       type = ROW_TYPE_INVALID;
1308       gtk_tree_model_get (priv->model, &iter,
1309                           TYPE_COLUMN, &type,
1310                           DATA_COLUMN, &data,
1311                           -1);
1312
1313       switch (type)
1314         {
1315         case ROW_TYPE_SPECIAL:
1316         case ROW_TYPE_SHORTCUT:
1317         case ROW_TYPE_BOOKMARK:
1318         case ROW_TYPE_CURRENT_FOLDER:
1319           if (data)
1320             {
1321               if (gtk_file_system_path_is_local (priv->fs, (GtkFilePath *)data))
1322                 {
1323                   GtkTreePath *path;
1324                   GtkFileSystemHandle *handle;
1325                   struct ChangeIconThemeData *info;               
1326                   
1327                   info = g_new0 (struct ChangeIconThemeData, 1);
1328                   info->button = g_object_ref (button);
1329                   path = gtk_tree_model_get_path (priv->model, &iter);
1330                   info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1331                   gtk_tree_path_free (path);
1332                   
1333                   handle =
1334                     gtk_file_system_get_info (priv->fs, data, GTK_FILE_INFO_ICON,
1335                                               change_icon_theme_get_info_cb,
1336                                               info);
1337                   button->priv->change_icon_theme_handles =
1338                     g_slist_append (button->priv->change_icon_theme_handles, handle);
1339                   pixbuf = NULL;
1340                 }
1341               else
1342                 /* Don't call get_info for remote paths to avoid latency and
1343                  * auth dialogs.
1344                  * If we switch to a better bookmarks file format (XBEL), we
1345                  * should use mime info to get a better icon.
1346                  */
1347                 pixbuf = gtk_icon_theme_load_icon (theme, "gnome-fs-share",
1348                                                    priv->icon_size, 0, NULL);
1349             }
1350           else
1351             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1352                                                priv->icon_size, 0, NULL);
1353           break;
1354         case ROW_TYPE_VOLUME:
1355           if (data)
1356             pixbuf = gtk_file_system_volume_render_icon (priv->fs, data,
1357                                                          GTK_WIDGET (button),
1358                                                          priv->icon_size,
1359                                                          NULL);
1360           else
1361             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1362                                                priv->icon_size, 0, NULL);
1363           break;
1364         default:
1365           continue;
1366           break;
1367         }
1368
1369       if (pixbuf)
1370         width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1371
1372       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1373                           ICON_COLUMN, pixbuf,
1374                           -1);
1375
1376       if (pixbuf)
1377         g_object_unref (pixbuf);
1378     }
1379   while (gtk_tree_model_iter_next (priv->model, &iter));
1380
1381   g_object_set (button->priv->icon_cell,
1382                 "width", width,
1383                 NULL);
1384 }
1385
1386 static void
1387 gtk_file_chooser_button_style_set (GtkWidget *widget,
1388                                    GtkStyle  *old_style)
1389 {
1390   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_set)
1391     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_set) (widget,
1392                                                                            old_style);
1393
1394   if (gtk_widget_has_screen (widget))
1395     change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1396 }
1397
1398 static void
1399 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1400                                         GdkScreen *old_screen)
1401 {
1402   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1403     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed) (widget,
1404                                                                                 old_screen);
1405
1406   change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget)); 
1407 }
1408
1409
1410 /* ******************* *
1411  *  Utility Functions  *
1412  * ******************* */
1413
1414 /* General */
1415 static GtkIconTheme *
1416 get_icon_theme (GtkWidget *widget)
1417 {
1418   if (gtk_widget_has_screen (widget))
1419     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1420
1421   return gtk_icon_theme_get_default ();
1422 }
1423
1424
1425 struct SetDisplayNameData
1426 {
1427   GtkFileChooserButton *button;
1428   char *label;
1429   GtkTreeRowReference *row_ref;
1430 };
1431
1432 static void
1433 set_info_get_info_cb (GtkFileSystemHandle *handle,
1434                       const GtkFileInfo   *info,
1435                       const GError        *error,
1436                       gpointer             callback_data)
1437 {
1438   gboolean cancelled = handle->cancelled;
1439   GdkPixbuf *pixbuf;
1440   GtkTreePath *path;
1441   GtkTreeIter iter;
1442   GtkFileSystemHandle *model_handle;
1443   struct SetDisplayNameData *data = callback_data;
1444
1445   if (!data->button->priv->model)
1446     /* button got destroyed */
1447     goto out;
1448
1449   path = gtk_tree_row_reference_get_path (data->row_ref);
1450   if (!path)
1451     /* Handle doesn't exist anymore in the model */
1452     goto out;
1453
1454   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1455   gtk_tree_path_free (path);
1456
1457   /* Validate the handle */
1458   gtk_tree_model_get (data->button->priv->model, &iter,
1459                       HANDLE_COLUMN, &model_handle,
1460                       -1);
1461   if (handle != model_handle)
1462     goto out;
1463
1464   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1465                       HANDLE_COLUMN, NULL,
1466                       -1);
1467
1468   if (cancelled || error)
1469     /* There was an error, leave the fallback name in there */
1470     goto out;
1471
1472   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1473                                       data->button->priv->icon_size, NULL);
1474
1475   if (!data->label)
1476     data->label = g_strdup (gtk_file_info_get_display_name (info));
1477
1478   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1479                       ICON_COLUMN, pixbuf,
1480                       DISPLAY_NAME_COLUMN, data->label,
1481                       IS_FOLDER_COLUMN, gtk_file_info_get_is_folder (info),
1482                       -1);
1483
1484   if (pixbuf)
1485     g_object_unref (pixbuf);
1486
1487 out:
1488   g_object_unref (data->button);
1489   g_free (data->label);
1490   gtk_tree_row_reference_free (data->row_ref);
1491   g_free (data);
1492
1493   g_object_unref (handle);
1494 }
1495
1496 static void
1497 set_info_for_path_at_iter (GtkFileChooserButton *button,
1498                            const GtkFilePath    *path,
1499                            GtkTreeIter          *iter)
1500 {
1501   struct SetDisplayNameData *data;
1502   GtkTreePath *tree_path;
1503   GtkFileSystemHandle *handle;
1504
1505   data = g_new0 (struct SetDisplayNameData, 1);
1506   data->button = g_object_ref (button);
1507   data->label = gtk_file_system_get_bookmark_label (button->priv->fs, path);
1508
1509   tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1510   data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1511   gtk_tree_path_free (tree_path);
1512
1513   handle = gtk_file_system_get_info (button->priv->fs, path,
1514                                      GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_ICON,
1515                                      set_info_get_info_cb, data);
1516
1517   gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1518                       HANDLE_COLUMN, handle,
1519                       -1);
1520 }
1521
1522 /* Shortcuts Model */
1523 static gint
1524 model_get_type_position (GtkFileChooserButton *button,
1525                          RowType               row_type)
1526 {
1527   gint retval = 0;
1528
1529   if (row_type == ROW_TYPE_SPECIAL)
1530     return retval;
1531
1532   retval += button->priv->n_special;
1533
1534   if (row_type == ROW_TYPE_VOLUME)
1535     return retval;
1536
1537   retval += button->priv->n_volumes;
1538
1539   if (row_type == ROW_TYPE_SHORTCUT)
1540     return retval;
1541
1542   retval += button->priv->n_shortcuts;
1543
1544   if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1545     return retval;
1546
1547   retval += button->priv->has_bookmark_separator;
1548
1549   if (row_type == ROW_TYPE_BOOKMARK)
1550     return retval;
1551
1552   retval += button->priv->n_bookmarks;
1553
1554   if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1555     return retval;
1556
1557   retval += button->priv->has_current_folder_separator;
1558
1559   if (row_type == ROW_TYPE_CURRENT_FOLDER)
1560     return retval;
1561
1562   retval += button->priv->has_current_folder;
1563
1564   if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1565     return retval;
1566
1567   retval += button->priv->has_other_separator;
1568
1569   if (row_type == ROW_TYPE_OTHER)
1570     return retval;
1571
1572   g_assert_not_reached ();
1573   return -1;
1574 }
1575
1576 static void
1577 model_free_row_data (GtkFileChooserButton *button,
1578                      GtkTreeIter          *iter)
1579 {
1580   gchar type;
1581   gpointer data;
1582   GtkFileSystemHandle *handle;
1583
1584   gtk_tree_model_get (button->priv->model, iter,
1585                       TYPE_COLUMN, &type,
1586                       DATA_COLUMN, &data,
1587                       HANDLE_COLUMN, &handle,
1588                       -1);
1589
1590   if (handle)
1591     gtk_file_system_cancel_operation (handle);
1592
1593   switch (type)
1594     {
1595     case ROW_TYPE_SPECIAL:
1596     case ROW_TYPE_SHORTCUT:
1597     case ROW_TYPE_BOOKMARK:
1598     case ROW_TYPE_CURRENT_FOLDER:
1599       gtk_file_path_free (data);
1600       break;
1601     case ROW_TYPE_VOLUME:
1602       gtk_file_system_volume_free (button->priv->fs, data);
1603       break;
1604     default:
1605       break;
1606     }
1607 }
1608
1609 static void
1610 model_add_special_get_info_cb (GtkFileSystemHandle *handle,
1611                                const GtkFileInfo   *info,
1612                                const GError        *error,
1613                                gpointer             user_data)
1614 {
1615   gboolean cancelled = handle->cancelled;
1616   GtkTreeIter iter;
1617   GtkTreePath *path;
1618   GdkPixbuf *pixbuf;
1619   GtkFileSystemHandle *model_handle;
1620   struct ChangeIconThemeData *data = user_data;
1621   gchar *name;
1622
1623   if (!data->button->priv->model)
1624     /* button got destroyed */
1625     goto out;
1626
1627   path = gtk_tree_row_reference_get_path (data->row_ref);
1628   if (!path)
1629     /* Handle doesn't exist anymore in the model */
1630     goto out;
1631
1632   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1633   gtk_tree_path_free (path);
1634
1635   gtk_tree_model_get (data->button->priv->model, &iter,
1636                       HANDLE_COLUMN, &model_handle,
1637                       -1);
1638   if (handle != model_handle)
1639     goto out;
1640
1641   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1642                       HANDLE_COLUMN, NULL,
1643                       -1);
1644
1645   if (cancelled || error)
1646     goto out;
1647
1648   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1649                                       data->button->priv->icon_size, NULL);
1650
1651   if (pixbuf)
1652     {
1653       gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1654                           ICON_COLUMN, pixbuf,
1655                           -1);
1656       g_object_unref (pixbuf);
1657     }
1658
1659   gtk_tree_model_get (data->button->priv->model, &iter,
1660                       DISPLAY_NAME_COLUMN, &name,
1661                       -1);
1662   if (!name)
1663     gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1664                         DISPLAY_NAME_COLUMN, gtk_file_info_get_display_name (info),
1665                         -1);
1666   g_free (name);
1667    
1668 out:
1669   g_object_unref (data->button);
1670   gtk_tree_row_reference_free (data->row_ref);
1671   g_free (data);
1672
1673   g_object_unref (handle);
1674 }
1675
1676 static inline void
1677 model_add_special (GtkFileChooserButton *button)
1678 {
1679   const gchar *homedir;
1680   const gchar *desktopdir;
1681   GtkListStore *store;
1682   GtkTreeIter iter;
1683   GtkFilePath *path;
1684   gint pos;
1685
1686   store = GTK_LIST_STORE (button->priv->model);
1687   pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1688
1689   homedir = g_get_home_dir ();
1690
1691   if (homedir)
1692     {
1693       GtkTreePath *tree_path;
1694       GtkFileSystemHandle *handle;
1695       struct ChangeIconThemeData *info;
1696
1697       path = gtk_file_system_filename_to_path (button->priv->fs, homedir);
1698       gtk_list_store_insert (store, &iter, pos);
1699       pos++;
1700
1701       info = g_new0 (struct ChangeIconThemeData, 1);
1702       info->button = g_object_ref (button);
1703       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1704       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1705                                                   tree_path);
1706       gtk_tree_path_free (tree_path);
1707
1708       handle = gtk_file_system_get_info (button->priv->fs, path,
1709                                          GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
1710                                          model_add_special_get_info_cb, info);
1711
1712       gtk_list_store_set (store, &iter,
1713                           ICON_COLUMN, NULL,
1714                           DISPLAY_NAME_COLUMN, NULL,
1715                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1716                           DATA_COLUMN, path,
1717                           IS_FOLDER_COLUMN, TRUE,
1718                           HANDLE_COLUMN, handle,
1719                           -1);
1720
1721       button->priv->n_special++;
1722     }
1723
1724   desktopdir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1725
1726   if (desktopdir)
1727     {
1728       GtkTreePath *tree_path;
1729       GtkFileSystemHandle *handle;
1730       struct ChangeIconThemeData *info;
1731
1732       path = gtk_file_system_filename_to_path (button->priv->fs, desktopdir);
1733       gtk_list_store_insert (store, &iter, pos);
1734       pos++;
1735
1736       info = g_new0 (struct ChangeIconThemeData, 1);
1737       info->button = g_object_ref (button);
1738       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1739       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1740                                                   tree_path);
1741       gtk_tree_path_free (tree_path);
1742
1743       handle = gtk_file_system_get_info (button->priv->fs, path,
1744                                          GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
1745                                          model_add_special_get_info_cb, info);
1746
1747       gtk_list_store_set (store, &iter,
1748                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1749                           ICON_COLUMN, NULL,
1750                           DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1751                           DATA_COLUMN, path,
1752                           IS_FOLDER_COLUMN, TRUE,
1753                           HANDLE_COLUMN, handle,
1754                           -1);
1755
1756       button->priv->n_special++;
1757     }
1758 }
1759
1760 static void
1761 model_add_volumes (GtkFileChooserButton *button,
1762                    GSList               *volumes)
1763 {
1764   GtkListStore *store;
1765   gint pos;
1766   gboolean local_only;
1767   GtkFileSystem *file_system;
1768   GSList *l;
1769   
1770   if (!volumes)
1771     return;
1772
1773   store = GTK_LIST_STORE (button->priv->model);
1774   pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1775   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1776   file_system = button->priv->fs;
1777
1778   for (l = volumes; l; l = l->next)
1779     {
1780       GtkFileSystemVolume *volume;
1781       GtkTreeIter iter;
1782       GdkPixbuf *pixbuf;
1783       gchar *display_name;
1784
1785       volume = l->data;
1786
1787       if (local_only)
1788         {
1789           if (gtk_file_system_volume_get_is_mounted (file_system, volume))
1790             {
1791               GtkFilePath *base_path;
1792
1793               base_path = gtk_file_system_volume_get_base_path (file_system, volume);
1794               if (base_path != NULL)
1795                 {
1796                   gboolean is_local = gtk_file_system_path_is_local (file_system, base_path);
1797                   gtk_file_path_free (base_path);
1798
1799                   if (!is_local)
1800                     {
1801                       gtk_file_system_volume_free (file_system, volume);
1802                       continue;
1803                     }
1804                 }
1805             }
1806         }
1807
1808       pixbuf = gtk_file_system_volume_render_icon (file_system,
1809                                                    volume,
1810                                                    GTK_WIDGET (button),
1811                                                    button->priv->icon_size,
1812                                                    NULL);
1813       display_name = gtk_file_system_volume_get_display_name (file_system, volume);
1814
1815       gtk_list_store_insert (store, &iter, pos);
1816       gtk_list_store_set (store, &iter,
1817                           ICON_COLUMN, pixbuf,
1818                           DISPLAY_NAME_COLUMN, display_name,
1819                           TYPE_COLUMN, ROW_TYPE_VOLUME,
1820                           DATA_COLUMN, volume,
1821                           IS_FOLDER_COLUMN, TRUE,
1822                           -1);
1823
1824       if (pixbuf)
1825         g_object_unref (pixbuf);
1826       g_free (display_name);
1827
1828       button->priv->n_volumes++;
1829       pos++;
1830     }
1831 }
1832
1833 extern gchar * _gtk_file_chooser_label_for_uri (const gchar *uri);
1834
1835 static void
1836 model_add_bookmarks (GtkFileChooserButton *button,
1837                      GSList               *bookmarks)
1838 {
1839   GtkListStore *store;
1840   GtkTreeIter iter;
1841   gint pos;
1842   gboolean local_only;
1843   GSList *l;
1844
1845   if (!bookmarks)
1846     return;
1847
1848   store = GTK_LIST_STORE (button->priv->model);
1849   pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
1850   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1851
1852   for (l = bookmarks; l; l = l->next)
1853     {
1854       GtkFilePath *path;
1855
1856       path = l->data;
1857
1858       if (gtk_file_system_path_is_local (button->priv->fs, path))
1859         {
1860           gtk_list_store_insert (store, &iter, pos);
1861           gtk_list_store_set (store, &iter,
1862                               ICON_COLUMN, NULL,
1863                               DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1864                               TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1865                               DATA_COLUMN, gtk_file_path_copy (path),
1866                               IS_FOLDER_COLUMN, FALSE,
1867                               -1);
1868           set_info_for_path_at_iter (button, path, &iter);
1869         }
1870       else
1871         {
1872           gchar *label;
1873           GtkIconTheme *icon_theme;
1874           GdkPixbuf *pixbuf;
1875
1876           if (local_only)
1877             continue;
1878
1879           /* Don't call get_info for remote paths to avoid latency and
1880            * auth dialogs.
1881            * If we switch to a better bookmarks file format (XBEL), we
1882            * should use mime info to get a better icon.
1883            */
1884           label = gtk_file_system_get_bookmark_label (button->priv->fs, path);
1885           if (!label)
1886             {
1887               gchar *uri;
1888
1889               uri = gtk_file_system_path_to_uri (button->priv->fs, path);
1890               label = _gtk_file_chooser_label_for_uri (uri);
1891               g_free (uri);
1892             }
1893
1894           icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1895           pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-share", 
1896                                              button->priv->icon_size, 0, NULL);
1897
1898           gtk_list_store_insert (store, &iter, pos);
1899           gtk_list_store_set (store, &iter,
1900                               ICON_COLUMN, pixbuf,
1901                               DISPLAY_NAME_COLUMN, label,
1902                               TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1903                               DATA_COLUMN, gtk_file_path_copy (path),
1904                               IS_FOLDER_COLUMN, TRUE,
1905                               -1);
1906
1907           g_free (label);
1908           g_object_unref (pixbuf);
1909         }
1910
1911       button->priv->n_bookmarks++;
1912       pos++;
1913     }
1914
1915   if (button->priv->n_bookmarks > 0 && 
1916       !button->priv->has_bookmark_separator)
1917     {
1918       pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
1919
1920       gtk_list_store_insert (store, &iter, pos);
1921       gtk_list_store_set (store, &iter,
1922                           ICON_COLUMN, NULL,
1923                           DISPLAY_NAME_COLUMN, NULL,
1924                           TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
1925                           DATA_COLUMN, NULL,
1926                           IS_FOLDER_COLUMN, FALSE,
1927                           -1);
1928       button->priv->has_bookmark_separator = TRUE;
1929     }
1930 }
1931
1932 static void
1933 model_update_current_folder (GtkFileChooserButton *button,
1934                              const GtkFilePath    *path)
1935 {
1936   GtkListStore *store;
1937   GtkTreeIter iter;
1938   gint pos;
1939
1940   if (!path) 
1941     return;
1942
1943   store = GTK_LIST_STORE (button->priv->model);
1944
1945   if (!button->priv->has_current_folder_separator)
1946     {
1947       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
1948       gtk_list_store_insert (store, &iter, pos);
1949       gtk_list_store_set (store, &iter,
1950                           ICON_COLUMN, NULL,
1951                           DISPLAY_NAME_COLUMN, NULL,
1952                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
1953                           DATA_COLUMN, NULL,
1954                           IS_FOLDER_COLUMN, FALSE,
1955                           -1);
1956       button->priv->has_current_folder_separator = TRUE;
1957     }
1958
1959   pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
1960   if (!button->priv->has_current_folder)
1961     {
1962       gtk_list_store_insert (store, &iter, pos);
1963       button->priv->has_current_folder = TRUE;
1964     }
1965   else
1966     {
1967       gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
1968       model_free_row_data (button, &iter);
1969     }
1970
1971   if (gtk_file_system_path_is_local (button->priv->fs, path))
1972     {
1973       gtk_list_store_set (store, &iter,
1974                           ICON_COLUMN, NULL,
1975                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1976                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
1977                           DATA_COLUMN, gtk_file_path_copy (path),
1978                           IS_FOLDER_COLUMN, FALSE,
1979                           -1);
1980       set_info_for_path_at_iter (button, path, &iter);
1981     }
1982   else
1983     {
1984       gchar *label;
1985       GtkIconTheme *icon_theme;
1986       GdkPixbuf *pixbuf;
1987
1988       /* Don't call get_info for remote paths to avoid latency and
1989        * auth dialogs.
1990        * If we switch to a better bookmarks file format (XBEL), we
1991        * should use mime info to get a better icon.
1992        */
1993       label = gtk_file_system_get_bookmark_label (button->priv->fs, path);
1994       if (!label)
1995         {
1996           gchar *uri;
1997           
1998           uri = gtk_file_system_path_to_uri (button->priv->fs, path);
1999           label = _gtk_file_chooser_label_for_uri (uri);
2000           g_free (uri);
2001         }
2002       
2003       icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
2004       if (gtk_file_system_path_is_local (button->priv->fs, path)) 
2005           pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-directory", 
2006                                              button->priv->icon_size, 0, NULL);
2007       else
2008           pixbuf = gtk_icon_theme_load_icon (icon_theme, "gnome-fs-share", 
2009                                              button->priv->icon_size, 0, NULL);
2010
2011       gtk_list_store_set (store, &iter,
2012                           ICON_COLUMN, pixbuf,
2013                           DISPLAY_NAME_COLUMN, label,
2014                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
2015                           DATA_COLUMN, gtk_file_path_copy (path),
2016                           IS_FOLDER_COLUMN, TRUE,
2017                           -1);
2018       
2019       g_free (label);
2020       g_object_unref (pixbuf);
2021     }
2022 }
2023
2024 static inline void
2025 model_add_other (GtkFileChooserButton *button)
2026 {
2027   GtkListStore *store;
2028   GtkTreeIter iter;
2029   gint pos;
2030   
2031   store = GTK_LIST_STORE (button->priv->model);
2032   pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
2033
2034   gtk_list_store_insert (store, &iter, pos);
2035   gtk_list_store_set (store, &iter,
2036                       ICON_COLUMN, NULL,
2037                       DISPLAY_NAME_COLUMN, NULL,
2038                       TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
2039                       DATA_COLUMN, NULL,
2040                       IS_FOLDER_COLUMN, FALSE,
2041                       -1);
2042   button->priv->has_other_separator = TRUE;
2043   pos++;
2044
2045   gtk_list_store_insert (store, &iter, pos);
2046   gtk_list_store_set (store, &iter,
2047                       ICON_COLUMN, NULL,
2048                       DISPLAY_NAME_COLUMN, _("Other..."),
2049                       TYPE_COLUMN, ROW_TYPE_OTHER,
2050                       DATA_COLUMN, NULL,
2051                       IS_FOLDER_COLUMN, FALSE,
2052                       -1);
2053 }
2054
2055 static void
2056 model_remove_rows (GtkFileChooserButton *button,
2057                    gint                  pos,
2058                    gint                  n_rows)
2059 {
2060   GtkListStore *store;
2061
2062   if (!n_rows)
2063     return;
2064
2065   store = GTK_LIST_STORE (button->priv->model);
2066
2067   do
2068     {
2069       GtkTreeIter iter;
2070
2071       if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
2072         g_assert_not_reached ();
2073
2074       model_free_row_data (button, &iter);
2075       gtk_list_store_remove (store, &iter);
2076       n_rows--;
2077     }
2078   while (n_rows);
2079 }
2080
2081 /* Filter Model */
2082 static inline gboolean
2083 test_if_path_is_visible (GtkFileSystem     *fs,
2084                          const GtkFilePath *path,
2085                          gboolean           local_only,
2086                          gboolean           is_folder)
2087 {
2088   if (!path)
2089     return FALSE;
2090
2091   if (local_only && !gtk_file_system_path_is_local (fs, path))
2092     return FALSE;
2093
2094   if (!is_folder)
2095     return FALSE;
2096
2097   return TRUE;
2098 }
2099
2100 static gboolean
2101 filter_model_visible_func (GtkTreeModel *model,
2102                            GtkTreeIter  *iter,
2103                            gpointer      user_data)
2104 {
2105   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2106   GtkFileChooserButtonPrivate *priv = button->priv;
2107   gchar type;
2108   gpointer data;
2109   gboolean local_only, retval, is_folder;
2110
2111   type = ROW_TYPE_INVALID;
2112   data = NULL;
2113   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
2114
2115   gtk_tree_model_get (model, iter,
2116                       TYPE_COLUMN, &type,
2117                       DATA_COLUMN, &data,
2118                       IS_FOLDER_COLUMN, &is_folder,
2119                       -1);
2120
2121   switch (type)
2122     {
2123     case ROW_TYPE_CURRENT_FOLDER:
2124       retval = TRUE;
2125       break;
2126     case ROW_TYPE_SPECIAL:
2127     case ROW_TYPE_SHORTCUT:
2128     case ROW_TYPE_BOOKMARK:
2129       retval = test_if_path_is_visible (priv->fs, data, local_only, is_folder);
2130       break;
2131     case ROW_TYPE_VOLUME:
2132       {
2133         retval = TRUE;
2134         if (local_only)
2135           {
2136             if (gtk_file_system_volume_get_is_mounted (priv->fs, data))
2137               {
2138                 GtkFilePath *base_path;
2139                 
2140                 base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2141                 if (base_path)
2142                   {
2143                     gboolean is_local = gtk_file_system_path_is_local (priv->fs, base_path);
2144                     
2145                     gtk_file_path_free (base_path);
2146
2147                     if (!is_local)
2148                       retval = FALSE;
2149                   }
2150                 else
2151                   retval = FALSE;
2152               }
2153           }
2154       }
2155       break;
2156     default:
2157       retval = TRUE;
2158       break;
2159     }
2160
2161   return retval;
2162 }
2163
2164 /* Combo Box */
2165 static void
2166 name_cell_data_func (GtkCellLayout   *layout,
2167                      GtkCellRenderer *cell,
2168                      GtkTreeModel    *model,
2169                      GtkTreeIter     *iter,
2170                      gpointer         user_data)
2171 {
2172   gchar type;
2173
2174   type = 0;
2175   gtk_tree_model_get (model, iter,
2176                       TYPE_COLUMN, &type,
2177                       -1);
2178
2179   if (type == ROW_TYPE_CURRENT_FOLDER)
2180     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2181   else
2182     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2183 }
2184
2185 static gboolean
2186 combo_box_row_separator_func (GtkTreeModel *model,
2187                               GtkTreeIter  *iter,
2188                               gpointer      user_data)
2189 {
2190   gchar type = ROW_TYPE_INVALID;
2191
2192   gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2193
2194   return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2195           type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2196           type == ROW_TYPE_OTHER_SEPARATOR);
2197 }                         
2198
2199 static void
2200 update_combo_box (GtkFileChooserButton *button)
2201 {
2202   GtkFileChooserButtonPrivate *priv = button->priv;
2203   GSList *paths;
2204   GtkTreeIter iter;
2205   gboolean row_found;
2206
2207   gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2208
2209   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2210
2211   row_found = FALSE;
2212
2213   do
2214     {
2215       gchar type;
2216       gpointer data;
2217
2218       type = ROW_TYPE_INVALID;
2219       data = NULL;
2220
2221       gtk_tree_model_get (priv->filter_model, &iter,
2222                           TYPE_COLUMN, &type,
2223                           DATA_COLUMN, &data,
2224                           -1);
2225     
2226       switch (type)
2227         {
2228         case ROW_TYPE_SPECIAL:
2229         case ROW_TYPE_SHORTCUT:
2230         case ROW_TYPE_BOOKMARK:
2231         case ROW_TYPE_CURRENT_FOLDER:
2232           row_found = (paths &&
2233                        paths->data &&
2234                        gtk_file_path_compare (data, paths->data) == 0);
2235           break;
2236         case ROW_TYPE_VOLUME:
2237           {
2238             GtkFilePath *base_path;
2239
2240             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2241             if (base_path)
2242               {
2243                 row_found = (paths &&
2244                              paths->data &&
2245                              gtk_file_path_compare (base_path, paths->data) == 0);
2246                 gtk_file_path_free (base_path);
2247               }
2248           }
2249           break;
2250         default:
2251           row_found = FALSE;
2252           break;
2253         }
2254
2255       if (row_found)
2256         {
2257           g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2258           gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2259                                          &iter);
2260           g_signal_handler_unblock (priv->combo_box,
2261                                     priv->combo_box_changed_id);
2262         }
2263     }
2264   while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2265
2266   /* If it hasn't been found already, update & select the current-folder row. */
2267   if (!row_found && paths && paths->data)
2268     {
2269       GtkTreeIter filter_iter;
2270       gint pos;
2271     
2272       model_update_current_folder (button, paths->data);
2273       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2274
2275       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2276       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2277
2278       gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2279                                                         &filter_iter, &iter);
2280
2281       g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2282       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2283       g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2284     }
2285
2286   gtk_file_paths_free (paths);
2287 }
2288
2289 /* Button */
2290 static void
2291 update_label_get_info_cb (GtkFileSystemHandle *handle,
2292                           const GtkFileInfo   *info,
2293                           const GError        *error,
2294                           gpointer             data)
2295 {
2296   gboolean cancelled = handle->cancelled;
2297   GdkPixbuf *pixbuf;
2298   GtkFileChooserButton *button = data;
2299   GtkFileChooserButtonPrivate *priv = button->priv;
2300
2301   if (handle != priv->update_button_handle)
2302     goto out;
2303
2304   priv->update_button_handle = NULL;
2305
2306   if (cancelled || error)
2307     goto out;
2308
2309   gtk_label_set_text (GTK_LABEL (priv->label), gtk_file_info_get_display_name (info));
2310
2311   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (priv->image),
2312                                       priv->icon_size, NULL);
2313   if (!pixbuf)
2314     pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2315                                        FALLBACK_ICON_NAME,
2316                                        priv->icon_size, 0, NULL);
2317
2318   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2319   if (pixbuf)
2320     g_object_unref (pixbuf);
2321
2322 out:
2323   g_object_unref (button);
2324   g_object_unref (handle);
2325 }
2326
2327 static void
2328 update_label_and_image (GtkFileChooserButton *button)
2329 {
2330   GtkFileChooserButtonPrivate *priv = button->priv;
2331   GdkPixbuf *pixbuf;
2332   gchar *label_text;
2333   GSList *paths;
2334
2335   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2336   label_text = NULL;
2337   pixbuf = NULL;
2338
2339   if (paths && paths->data)
2340     {
2341       GtkFilePath *path;
2342       GtkFileSystemVolume *volume = NULL;
2343     
2344       path = paths->data;
2345
2346       volume = gtk_file_system_get_volume_for_path (priv->fs, path);
2347       if (volume)
2348         {
2349           GtkFilePath *base_path;
2350
2351           base_path = gtk_file_system_volume_get_base_path (priv->fs, volume);
2352           if (base_path && gtk_file_path_compare (base_path, path) == 0)
2353             {
2354               label_text = gtk_file_system_volume_get_display_name (priv->fs,
2355                                                                     volume);
2356               pixbuf = gtk_file_system_volume_render_icon (priv->fs, volume,
2357                                                            GTK_WIDGET (button),
2358                                                            priv->icon_size,
2359                                                            NULL);
2360             }
2361
2362           if (base_path)
2363             gtk_file_path_free (base_path);
2364
2365           gtk_file_system_volume_free (priv->fs, volume);
2366
2367           if (label_text)
2368             goto out;
2369         }
2370
2371       if (priv->update_button_handle)
2372         {
2373           gtk_file_system_cancel_operation (priv->update_button_handle);
2374           priv->update_button_handle = NULL;
2375         }
2376           
2377       if (gtk_file_system_path_is_local (priv->fs, path))
2378         {
2379           priv->update_button_handle =
2380             gtk_file_system_get_info (priv->fs, path,
2381                                       GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
2382                                       update_label_get_info_cb,
2383                                       g_object_ref (button));
2384         }
2385       else
2386         {
2387           GdkPixbuf *pixbuf;
2388
2389           label_text = gtk_file_system_get_bookmark_label (button->priv->fs, path);
2390           
2391           pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)), 
2392                                              "gnome-fs-regular",
2393                                              priv->icon_size, 0, NULL);
2394           
2395           gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2396
2397           if (pixbuf)
2398             g_object_unref (pixbuf);
2399         }
2400     }
2401 out:
2402   gtk_file_paths_free (paths);
2403
2404   if (label_text)
2405     {
2406       gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2407       g_free (label_text);
2408     }
2409   else
2410     gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2411 }
2412
2413
2414 /* ************************ *
2415  *  Child Object Callbacks  *
2416  * ************************ */
2417
2418 /* File System */
2419 static void
2420 fs_volumes_changed_cb (GtkFileSystem *fs,
2421                        gpointer       user_data)
2422 {
2423   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2424   GtkFileChooserButtonPrivate *priv = button->priv;
2425   GSList *volumes;
2426
2427   model_remove_rows (user_data,
2428                      model_get_type_position (user_data, ROW_TYPE_VOLUME),
2429                      priv->n_volumes);
2430
2431   priv->n_volumes = 0;
2432
2433   volumes = gtk_file_system_list_volumes (fs);
2434   model_add_volumes (user_data, volumes);
2435   g_slist_free (volumes);
2436
2437   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2438
2439   update_label_and_image (user_data);
2440   update_combo_box (user_data);
2441 }
2442
2443 static void
2444 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2445                          gpointer       user_data)
2446 {
2447   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2448   GtkFileChooserButtonPrivate *priv = button->priv;
2449   GSList *bookmarks;
2450
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   gtk_file_paths_free (bookmarks);
2460
2461   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2462
2463   update_label_and_image (user_data);
2464   update_combo_box (user_data);
2465 }
2466
2467 /* Dialog */
2468 static void
2469 open_dialog (GtkFileChooserButton *button)
2470 {
2471   GtkFileChooserButtonPrivate *priv = button->priv;
2472
2473   /* Setup the dialog parent to be chooser button's toplevel, and be modal
2474      as needed. */
2475   if (!GTK_WIDGET_VISIBLE (priv->dialog))
2476     {
2477       GtkWidget *toplevel;
2478
2479       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2480
2481       if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
2482         {
2483           if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2484             gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2485                                           GTK_WINDOW (toplevel));
2486               
2487           gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2488                                 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2489         }
2490     }
2491
2492   if (!priv->active)
2493     {
2494       GSList *paths;
2495
2496       g_signal_handler_block (priv->dialog,
2497                               priv->dialog_folder_changed_id);
2498       g_signal_handler_block (priv->dialog,
2499                               priv->dialog_file_activated_id);
2500       g_signal_handler_block (priv->dialog,
2501                               priv->dialog_selection_changed_id);
2502       paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2503       if (paths)
2504         {
2505           if (paths->data)
2506             priv->old_path = gtk_file_path_copy (paths->data);
2507
2508           gtk_file_paths_free (paths);
2509         }
2510
2511       priv->active = TRUE;
2512     }
2513
2514   gtk_widget_set_sensitive (priv->combo_box, FALSE);
2515   gtk_window_present (GTK_WINDOW (priv->dialog));
2516 }
2517
2518 /* Combo Box */
2519 static void
2520 combo_box_changed_cb (GtkComboBox *combo_box,
2521                       gpointer     user_data)
2522 {
2523   GtkTreeIter iter;
2524
2525   if (gtk_combo_box_get_active_iter (combo_box, &iter))
2526     {
2527       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2528       GtkFileChooserButtonPrivate *priv = button->priv;
2529       gchar type;
2530       gpointer data;
2531
2532       type = ROW_TYPE_INVALID;
2533       data = NULL;
2534
2535       gtk_tree_model_get (priv->filter_model, &iter,
2536                           TYPE_COLUMN, &type,
2537                           DATA_COLUMN, &data,
2538                           -1);
2539
2540       switch (type)
2541         {
2542         case ROW_TYPE_SPECIAL:
2543         case ROW_TYPE_SHORTCUT:
2544         case ROW_TYPE_BOOKMARK:
2545         case ROW_TYPE_CURRENT_FOLDER:
2546           gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2547           if (data)
2548             _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2549                                                        data, NULL);
2550           break;
2551         case ROW_TYPE_VOLUME:
2552           {
2553             GtkFilePath *base_path;
2554
2555             gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2556             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2557             if (base_path)
2558               {
2559                 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2560                                                            base_path, NULL);
2561                 gtk_file_path_free (base_path);
2562               }
2563           }
2564           break;
2565         case ROW_TYPE_OTHER:
2566           open_dialog (user_data);
2567           break;
2568         default:
2569           break;
2570         }
2571     }
2572 }
2573
2574 /* Button */
2575 static void
2576 button_clicked_cb (GtkButton *real_button,
2577                    gpointer   user_data)
2578 {
2579   open_dialog (user_data);
2580 }
2581
2582 /* Dialog */
2583 static void
2584 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2585                                   gpointer        user_data)
2586 {
2587   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2588   GtkFileChooserButtonPrivate *priv = button->priv;
2589
2590   priv->folder_has_been_set = TRUE;
2591
2592   g_signal_emit_by_name (button, "current-folder-changed");
2593 }
2594
2595 static void
2596 dialog_file_activated_cb (GtkFileChooser *dialog,
2597                           gpointer        user_data)
2598 {
2599   g_signal_emit_by_name (user_data, "file-activated");
2600 }
2601
2602 static void
2603 dialog_selection_changed_cb (GtkFileChooser *dialog,
2604                              gpointer        user_data)
2605 {
2606   update_label_and_image (user_data);
2607   update_combo_box (user_data);
2608   g_signal_emit_by_name (user_data, "selection-changed");
2609 }
2610
2611 static void
2612 dialog_update_preview_cb (GtkFileChooser *dialog,
2613                           gpointer        user_data)
2614 {
2615   g_signal_emit_by_name (user_data, "update-preview");
2616 }
2617
2618 static void
2619 dialog_notify_cb (GObject    *dialog,
2620                   GParamSpec *pspec,
2621                   gpointer    user_data)
2622 {
2623   gpointer iface;
2624
2625   iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2626                                  GTK_TYPE_FILE_CHOOSER);
2627   if (g_object_interface_find_property (iface, pspec->name))
2628     g_object_notify (user_data, pspec->name);
2629
2630   if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2631     {
2632       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2633       GtkFileChooserButtonPrivate *priv = button->priv;
2634
2635       if (priv->has_current_folder)
2636         {
2637           GtkTreeIter iter;
2638           gint pos;
2639           gpointer data;
2640
2641           pos = model_get_type_position (user_data,
2642                                          ROW_TYPE_CURRENT_FOLDER);
2643           gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2644
2645           data = NULL;
2646           gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2647
2648           /* If the path isn't local but we're in local-only mode now, remove
2649            * the custom-folder row */
2650           if (data &&
2651               (!gtk_file_system_path_is_local (priv->fs, data) &&
2652                gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog))))
2653             {
2654               pos--;
2655               model_remove_rows (user_data, pos, 2);
2656             }
2657         }
2658
2659       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2660       update_combo_box (user_data);
2661     }
2662 }
2663
2664 static gboolean
2665 dialog_delete_event_cb (GtkWidget *dialog,
2666                         GdkEvent  *event,
2667                         gpointer   user_data)
2668 {
2669   g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2670
2671   return TRUE;
2672 }
2673
2674 static void
2675 dialog_response_cb (GtkDialog *dialog,
2676                     gint       response,
2677                     gpointer   user_data)
2678 {
2679   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2680   GtkFileChooserButtonPrivate *priv = button->priv;
2681
2682   if (response == GTK_RESPONSE_ACCEPT ||
2683       response == GTK_RESPONSE_OK)
2684     {
2685       g_signal_emit_by_name (user_data, "current-folder-changed");
2686       g_signal_emit_by_name (user_data, "selection-changed");
2687     }
2688   else if (priv->old_path)
2689     {
2690       switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)))
2691         {
2692         case GTK_FILE_CHOOSER_ACTION_OPEN:
2693           _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (dialog), priv->old_path,
2694                                          NULL);
2695           break;
2696         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
2697           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (dialog),
2698                                                      priv->old_path, NULL);
2699           break;
2700         default:
2701           g_assert_not_reached ();
2702           break;
2703         }
2704     }
2705   else
2706     gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (dialog));
2707
2708   if (priv->old_path)
2709     {
2710       gtk_file_path_free (priv->old_path);
2711       priv->old_path = NULL;
2712     }
2713
2714   update_label_and_image (user_data);
2715   update_combo_box (user_data);
2716   
2717   if (priv->active)
2718     {
2719       g_signal_handler_unblock (priv->dialog,
2720                                 priv->dialog_folder_changed_id);
2721       g_signal_handler_unblock (priv->dialog,
2722                                 priv->dialog_file_activated_id);
2723       g_signal_handler_unblock (priv->dialog,
2724                                 priv->dialog_selection_changed_id);
2725       priv->active = FALSE;
2726     }
2727
2728   gtk_widget_set_sensitive (priv->combo_box, TRUE);
2729   gtk_widget_hide (priv->dialog);
2730
2731   g_signal_emit_by_name (user_data, "file-set");
2732 }
2733
2734
2735 /* ************************************************************************** *
2736  *  Public API                                                                *
2737  * ************************************************************************** */
2738
2739 /**
2740  * gtk_file_chooser_button_new:
2741  * @title: the title of the browse dialog.
2742  * @action: the open mode for the widget.
2743  * 
2744  * Creates a new file-selecting button widget.
2745  * 
2746  * Returns: a new button widget.
2747  * 
2748  * Since: 2.6
2749  **/
2750 GtkWidget *
2751 gtk_file_chooser_button_new (const gchar          *title,
2752                              GtkFileChooserAction  action)
2753 {
2754   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2755                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2756
2757   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2758                        "action", action,
2759                        "title", (title ? title : _(DEFAULT_TITLE)),
2760                        NULL);
2761 }
2762
2763 /**
2764  * gtk_file_chooser_button_new_with_backend:
2765  * @title: the title of the browse dialog.
2766  * @action: the open mode for the widget.
2767  * @backend: the name of the #GtkFileSystem backend to use.
2768  * 
2769  * Creates a new file-selecting button widget using @backend.
2770  * 
2771  * Returns: a new button widget.
2772  * 
2773  * Since: 2.6
2774  **/
2775 GtkWidget *
2776 gtk_file_chooser_button_new_with_backend (const gchar          *title,
2777                                           GtkFileChooserAction  action,
2778                                           const gchar          *backend)
2779 {
2780   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2781                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2782
2783   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2784                        "action", action,
2785                        "title", (title ? title : _(DEFAULT_TITLE)),
2786                        "file-system-backend", backend,
2787                        NULL);
2788 }
2789
2790 /**
2791  * gtk_file_chooser_button_new_with_dialog:
2792  * @dialog: the widget to use as dialog
2793  *
2794  * Creates a #GtkFileChooserButton widget which uses @dialog as its
2795  * file-picking window.
2796  *
2797  * Note that @dialog must be a #GtkDialog (or subclass) which
2798  * implements the #GtkFileChooser interface and must not have
2799  * %GTK_DIALOG_DESTROY_WITH_PARENT set.
2800  *
2801  * Also note that the dialog needs to have its confirmative button
2802  * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
2803  * order for the button to take over the file selected in the dialog.
2804  *
2805  * Returns: a new button widget.
2806  *
2807  * Since: 2.6
2808  **/
2809 GtkWidget *
2810 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2811 {
2812   g_return_val_if_fail (GTK_IS_FILE_CHOOSER (dialog) && GTK_IS_DIALOG (dialog), NULL);
2813
2814   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2815                        "dialog", dialog,
2816                        NULL);
2817 }
2818
2819 /**
2820  * gtk_file_chooser_button_set_title:
2821  * @button: the button widget to modify.
2822  * @title: the new browse dialog title.
2823  * 
2824  * Modifies the @title of the browse dialog used by @button.
2825  * 
2826  * Since: 2.6
2827  **/
2828 void
2829 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2830                                    const gchar          *title)
2831 {
2832   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2833
2834   gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2835   g_object_notify (G_OBJECT (button), "title");
2836 }
2837
2838 /**
2839  * gtk_file_chooser_button_get_title:
2840  * @button: the button widget to examine.
2841  * 
2842  * Retrieves the title of the browse dialog used by @button. The returned value
2843  * should not be modified or freed.
2844  * 
2845  * Returns: a pointer to the browse dialog's title.
2846  * 
2847  * Since: 2.6
2848  **/
2849 G_CONST_RETURN gchar *
2850 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2851 {
2852   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2853
2854   return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2855 }
2856
2857 /**
2858  * gtk_file_chooser_button_get_width_chars:
2859  * @button: the button widget to examine.
2860  * 
2861  * Retrieves the width in characters of the @button widget's entry and/or label.
2862  * 
2863  * Returns: an integer width (in characters) that the button will use to size itself.
2864  * 
2865  * Since: 2.6
2866  **/
2867 gint
2868 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2869 {
2870   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2871
2872   return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2873 }
2874
2875 /**
2876  * gtk_file_chooser_button_set_width_chars:
2877  * @button: the button widget to examine.
2878  * @n_chars: the new width, in characters.
2879  * 
2880  * Sets the width (in characters) that @button will use to @n_chars.
2881  * 
2882  * Since: 2.6
2883  **/
2884 void
2885 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
2886                                          gint                  n_chars)
2887 {
2888   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2889
2890   gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
2891   g_object_notify (G_OBJECT (button), "width-chars");
2892 }
2893
2894 /**
2895  * gtk_file_chooser_button_set_focus_on_click:
2896  * @button: a #GtkFileChooserButton
2897  * @focus_on_click: whether the button grabs focus when clicked with the mouse
2898  * 
2899  * Sets whether the button will grab focus when it is clicked with the mouse.
2900  * Making mouse clicks not grab focus is useful in places like toolbars where
2901  * you don't want the keyboard focus removed from the main area of the
2902  * application.
2903  *
2904  * Since: 2.10
2905  **/
2906 void
2907 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
2908                                             gboolean              focus_on_click)
2909 {
2910   GtkFileChooserButtonPrivate *priv;
2911
2912   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2913
2914   priv = button->priv;
2915
2916   focus_on_click = focus_on_click != FALSE;
2917
2918   if (priv->focus_on_click != focus_on_click)
2919     {
2920       priv->focus_on_click = focus_on_click;
2921       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
2922       gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
2923       
2924       g_object_notify (G_OBJECT (button), "focus-on-click");
2925     }
2926 }
2927
2928 /**
2929  * gtk_file_chooser_button_get_focus_on_click:
2930  * @button: a #GtkFileChooserButton
2931  * 
2932  * Returns whether the button grabs focus when it is clicked with the mouse.
2933  * See gtk_file_chooser_button_set_focus_on_click().
2934  *
2935  * Return value: %TRUE if the button grabs focus when it is clicked with
2936  *               the mouse.
2937  *
2938  * Since: 2.10
2939  **/
2940 gboolean
2941 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
2942 {
2943   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
2944   
2945   return button->priv->focus_on_click;
2946 }
2947
2948 #define __GTK_FILE_CHOOSER_BUTTON_C__
2949 #include "gtkaliasdef.c"