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