]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserbutton.c
Fix a small leak
[~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_OBJECT    /* 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_LOCAL_ONLY:
801     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
802     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
803     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
804     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
805     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
806     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
807       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
808       break;
809
810     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
811       /* Construct-only */
812       priv->backend = g_value_dup_string (value);
813       break;
814
815     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
816       g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
817                  G_STRFUNC, G_OBJECT_TYPE_NAME (object));
818       break;
819     default:
820       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
821       break;
822     }
823 }
824
825 static void
826 gtk_file_chooser_button_get_property (GObject    *object,
827                                       guint       param_id,
828                                       GValue     *value,
829                                       GParamSpec *pspec)
830 {
831   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
832   GtkFileChooserButtonPrivate *priv = button->priv;
833
834   switch (param_id)
835     {
836     case PROP_WIDTH_CHARS:
837       g_value_set_int (value,
838                        gtk_label_get_width_chars (GTK_LABEL (priv->label)));
839       break;
840     case PROP_FOCUS_ON_CLICK:
841       g_value_set_boolean (value,
842                            gtk_file_chooser_button_get_focus_on_click (button));
843       break;
844
845     case PROP_TITLE:
846     case GTK_FILE_CHOOSER_PROP_ACTION:
847     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
848     case GTK_FILE_CHOOSER_PROP_FILTER:
849     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
850     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
851     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
852     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
853     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
854     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
855     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
856     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
857       g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
858       break;
859
860     default:
861       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
862       break;
863     }
864 }
865
866 static void
867 gtk_file_chooser_button_finalize (GObject *object)
868 {
869   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
870   GtkFileChooserButtonPrivate *priv = button->priv;
871
872   if (priv->old_path)
873     gtk_file_path_free (priv->old_path);
874
875   if (G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize != NULL)
876     (*G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize) (object);
877 }
878
879 /* ********************* *
880  *  GtkObject Functions  *
881  * ********************* */
882
883 static void
884 gtk_file_chooser_button_destroy (GtkObject *object)
885 {
886   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
887   GtkFileChooserButtonPrivate *priv = button->priv;
888   GtkTreeIter iter;
889   GSList *l;
890
891   if (priv->dialog != NULL)
892     {
893       gtk_widget_destroy (priv->dialog);
894       priv->dialog = NULL;
895     }
896
897   gtk_tree_model_get_iter_first (priv->model, &iter);
898
899   do
900     {
901       model_free_row_data (button, &iter);
902     }
903   while (gtk_tree_model_iter_next (priv->model, &iter));
904
905   if (priv->dnd_select_folder_handle)
906     {
907       gtk_file_system_cancel_operation (priv->dnd_select_folder_handle);
908       priv->dnd_select_folder_handle = NULL;
909     }
910
911   if (priv->update_button_handle)
912     {
913       gtk_file_system_cancel_operation (priv->update_button_handle);
914       priv->update_button_handle = NULL;
915     }
916
917   if (priv->change_icon_theme_handles)
918     {
919       for (l = priv->change_icon_theme_handles; l; l = l->next)
920         {
921           GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
922           gtk_file_system_cancel_operation (handle);
923         }
924       g_slist_free (priv->change_icon_theme_handles);
925       priv->change_icon_theme_handles = NULL;
926     }
927
928   if (priv->model)
929     {
930       g_object_unref (priv->model);
931       priv->model = NULL;
932     }
933
934   if (priv->filter_model)
935     {
936       g_object_unref (priv->filter_model);
937       priv->filter_model = NULL;
938     }
939
940   if (priv->fs)
941     {
942       g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
943       g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
944       g_object_unref (priv->fs);
945       priv->fs = NULL;
946     }
947
948   if (GTK_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->destroy != NULL)
949     (*GTK_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->destroy) (object);
950 }
951
952
953 /* ********************* *
954  *  GtkWidget Functions  *
955  * ********************* */
956
957 struct DndSelectFolderData
958 {
959   GtkFileChooserButton *button;
960   GtkFileChooserAction action;
961   GtkFilePath *path;
962   gchar **uris;
963   guint i;
964   gboolean selected;
965 };
966
967 static void
968 dnd_select_folder_get_info_cb (GtkFileSystemHandle *handle,
969                                const GtkFileInfo   *info,
970                                const GError        *error,
971                                gpointer             user_data)
972 {
973   gboolean cancelled = handle->cancelled;
974   struct DndSelectFolderData *data = user_data;
975
976   if (handle != data->button->priv->dnd_select_folder_handle)
977     {
978       g_object_unref (data->button);
979       gtk_file_path_free (data->path);
980       g_strfreev (data->uris);
981       g_free (data);
982
983       g_object_unref (handle);
984       return;
985     }
986
987   data->button->priv->dnd_select_folder_handle = NULL;
988
989   if (!cancelled && !error && info != NULL)
990     {
991       data->selected = 
992         (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
993            gtk_file_info_get_is_folder (info)) ||
994           (data->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
995            !gtk_file_info_get_is_folder (info))) &&
996          _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (data->button->priv->dialog),
997                                         data->path, NULL));
998     }
999   else
1000     data->selected = FALSE;
1001
1002   if (data->selected || data->uris[++data->i] == NULL)
1003     {
1004       g_object_unref (data->button);
1005       gtk_file_path_free (data->path);
1006       g_strfreev (data->uris);
1007       g_free (data);
1008
1009       g_object_unref (handle);
1010       return;
1011     }
1012
1013   if (data->path)
1014     gtk_file_path_free (data->path);
1015
1016   data->path = gtk_file_system_uri_to_path (handle->file_system,
1017                                             data->uris[data->i]);
1018
1019   data->button->priv->dnd_select_folder_handle =
1020     gtk_file_system_get_info (handle->file_system, data->path,
1021                               GTK_FILE_INFO_IS_FOLDER,
1022                               dnd_select_folder_get_info_cb, user_data);
1023
1024   g_object_unref (handle);
1025 }
1026
1027 static void
1028 gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
1029                                             GdkDragContext   *context,
1030                                             gint              x,
1031                                             gint              y,
1032                                             GtkSelectionData *data,
1033                                             guint             info,
1034                                             guint             drag_time)
1035 {
1036   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1037   GtkFileChooserButtonPrivate *priv = button->priv;
1038   GtkFilePath *path;
1039   gchar *text;
1040
1041   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1042     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received) (widget,
1043                                                                                     context,
1044                                                                                     x, y,
1045                                                                                     data, info,
1046                                                                                     drag_time);
1047
1048   if (widget == NULL || context == NULL || data == NULL || data->length < 0)
1049     return;
1050
1051   switch (info)
1052     {
1053     case TEXT_URI_LIST:
1054       {
1055         gchar **uris;
1056         struct DndSelectFolderData *info;
1057
1058         uris = gtk_selection_data_get_uris (data);
1059         
1060         if (uris == NULL)
1061           break;
1062
1063         info = g_new0 (struct DndSelectFolderData, 1);
1064         info->button = g_object_ref (button);
1065         info->i = 0;
1066         info->uris = uris;
1067         info->selected = FALSE;
1068         g_object_get (priv->dialog, "action", &info->action, NULL);
1069
1070         info->path = gtk_file_system_uri_to_path (priv->fs,
1071                                                   info->uris[info->i]);
1072
1073         if (priv->dnd_select_folder_handle)
1074           gtk_file_system_cancel_operation (priv->dnd_select_folder_handle);
1075
1076         priv->dnd_select_folder_handle =
1077           gtk_file_system_get_info (priv->fs, info->path,
1078                                     GTK_FILE_INFO_IS_FOLDER,
1079                                     dnd_select_folder_get_info_cb, info);
1080       }
1081       break;
1082
1083     case TEXT_PLAIN:
1084       text = (char*) gtk_selection_data_get_text (data);
1085       path = gtk_file_path_new_steal (text);
1086       _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (priv->dialog), path,
1087                                      NULL);
1088       gtk_file_path_free (path);
1089       break;
1090
1091     default:
1092       break;
1093     }
1094
1095   gtk_drag_finish (context, TRUE, FALSE, drag_time);
1096 }
1097
1098 static void
1099 gtk_file_chooser_button_show_all (GtkWidget *widget)
1100 {
1101   gtk_widget_show (widget);
1102 }
1103
1104 static void
1105 gtk_file_chooser_button_hide_all (GtkWidget *widget)
1106 {
1107   gtk_widget_hide (widget);
1108 }
1109
1110 static void
1111 gtk_file_chooser_button_show (GtkWidget *widget)
1112 {
1113   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1114   GtkFileChooserButtonPrivate *priv = button->priv;
1115
1116   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1117     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show) (widget);
1118
1119   if (priv->active)
1120     open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1121 }
1122
1123 static void
1124 gtk_file_chooser_button_hide (GtkWidget *widget)
1125 {
1126   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1127   GtkFileChooserButtonPrivate *priv = button->priv;
1128
1129   gtk_widget_hide (priv->dialog);
1130
1131   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1132     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide) (widget);
1133 }
1134
1135 static void
1136 gtk_file_chooser_button_map (GtkWidget *widget)
1137 {
1138   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1139   GtkFileChooserButtonPrivate *priv = button->priv;
1140
1141   if (!priv->folder_has_been_set)
1142     {
1143       char *current_working_dir;
1144
1145       current_working_dir = g_get_current_dir ();
1146       gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), current_working_dir);
1147       g_free (current_working_dir);
1148
1149       priv->folder_has_been_set = TRUE;
1150     }
1151
1152   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map)
1153     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map) (widget);
1154 }
1155
1156 static gboolean
1157 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1158                                            gboolean   group_cycling)
1159 {
1160   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1161   GtkFileChooserButtonPrivate *priv = button->priv;
1162
1163   switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1164     {
1165     case GTK_FILE_CHOOSER_ACTION_OPEN:
1166       gtk_widget_grab_focus (priv->button);
1167       break;
1168     case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1169       return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1170       break;
1171     default:
1172       g_assert_not_reached ();
1173       break;
1174     }
1175
1176   return TRUE;
1177 }
1178
1179 /* Changes the icons wherever it is needed */
1180 struct ChangeIconThemeData
1181 {
1182   GtkFileChooserButton *button;
1183   GtkTreeRowReference *row_ref;
1184 };
1185
1186 static void
1187 change_icon_theme_get_info_cb (GtkFileSystemHandle *handle,
1188                                const GtkFileInfo   *info,
1189                                const GError        *error,
1190                                gpointer             user_data)
1191 {
1192   gboolean cancelled = handle->cancelled;
1193   GdkPixbuf *pixbuf;
1194   struct ChangeIconThemeData *data = user_data;
1195
1196   if (!g_slist_find (data->button->priv->change_icon_theme_handles, handle))
1197     goto out;
1198
1199   data->button->priv->change_icon_theme_handles =
1200     g_slist_remove (data->button->priv->change_icon_theme_handles, handle);
1201
1202   if (cancelled || error)
1203     goto out;
1204
1205   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1206                                       data->button->priv->icon_size, NULL);
1207
1208   if (pixbuf)
1209     {
1210       gint width = 0;
1211       GtkTreeIter iter;
1212       GtkTreePath *path;
1213
1214       width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1215
1216       path = gtk_tree_row_reference_get_path (data->row_ref);
1217       gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1218       gtk_tree_path_free (path);
1219
1220       gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1221                           ICON_COLUMN, pixbuf,
1222                           -1);
1223       g_object_unref (pixbuf);
1224
1225       g_object_set (data->button->priv->icon_cell,
1226                     "width", width,
1227                     NULL);
1228     }
1229
1230 out:
1231   g_object_unref (data->button);
1232   gtk_tree_row_reference_free (data->row_ref);
1233   g_free (data);
1234
1235   g_object_unref (handle);
1236 }
1237
1238 static void
1239 change_icon_theme (GtkFileChooserButton *button)
1240 {
1241   GtkFileChooserButtonPrivate *priv = button->priv;
1242   GtkSettings *settings;
1243   GtkIconTheme *theme;
1244   GtkTreeIter iter;
1245   GSList *l;
1246   gint width = 0, height = 0;
1247
1248   for (l = button->priv->change_icon_theme_handles; l; l = l->next)
1249     {
1250       GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1251       gtk_file_system_cancel_operation (handle);
1252     }
1253   g_slist_free (button->priv->change_icon_theme_handles);
1254   button->priv->change_icon_theme_handles = NULL;
1255
1256   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1257
1258   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1259                                          &width, &height))
1260     priv->icon_size = MAX (width, height);
1261   else
1262     priv->icon_size = FALLBACK_ICON_SIZE;
1263
1264   update_label_and_image (button);
1265
1266   gtk_tree_model_get_iter_first (priv->model, &iter);
1267
1268   theme = get_icon_theme (GTK_WIDGET (button));
1269
1270   do
1271     {
1272       GdkPixbuf *pixbuf;
1273       gchar type;
1274       gpointer data;
1275
1276       type = ROW_TYPE_INVALID;
1277       gtk_tree_model_get (priv->model, &iter,
1278                           TYPE_COLUMN, &type,
1279                           DATA_COLUMN, &data,
1280                           -1);
1281
1282       switch (type)
1283         {
1284         case ROW_TYPE_SPECIAL:
1285         case ROW_TYPE_SHORTCUT:
1286         case ROW_TYPE_BOOKMARK:
1287         case ROW_TYPE_CURRENT_FOLDER:
1288           if (data)
1289             {
1290               GtkTreePath *path;
1291               GtkFileSystemHandle *handle;
1292               struct ChangeIconThemeData *info;
1293
1294               info = g_new0 (struct ChangeIconThemeData, 1);
1295               info->button = g_object_ref (button);
1296               path = gtk_tree_model_get_path (priv->model, &iter);
1297               info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1298               gtk_tree_path_free (path);
1299
1300               handle =
1301                 gtk_file_system_get_info (priv->fs, data, GTK_FILE_INFO_ICON,
1302                                           change_icon_theme_get_info_cb,
1303                                           info);
1304               button->priv->change_icon_theme_handles =
1305                 g_slist_append (button->priv->change_icon_theme_handles, handle);
1306               pixbuf = NULL;
1307             }
1308           else
1309             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1310                                                priv->icon_size, 0, NULL);
1311           break;
1312         case ROW_TYPE_VOLUME:
1313           if (data)
1314             pixbuf = gtk_file_system_volume_render_icon (priv->fs, data,
1315                                                          GTK_WIDGET (button),
1316                                                          priv->icon_size,
1317                                                          NULL);
1318           else
1319             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1320                                                priv->icon_size, 0, NULL);
1321           break;
1322         default:
1323           continue;
1324           break;
1325         }
1326
1327       if (pixbuf)
1328         width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1329
1330       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1331                           ICON_COLUMN, pixbuf,
1332                           -1);
1333
1334       if (pixbuf)
1335         g_object_unref (pixbuf);
1336     }
1337   while (gtk_tree_model_iter_next (priv->model, &iter));
1338
1339   g_object_set (button->priv->icon_cell,
1340                 "width", width,
1341                 NULL);
1342 }
1343
1344 static void
1345 gtk_file_chooser_button_style_set (GtkWidget *widget,
1346                                    GtkStyle  *old_style)
1347 {
1348   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_set)
1349     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_set) (widget,
1350                                                                            old_style);
1351
1352   if (gtk_widget_has_screen (widget))
1353     change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1354 }
1355
1356 static void
1357 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1358                                         GdkScreen *old_screen)
1359 {
1360   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1361     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed) (widget,
1362                                                                                 old_screen);
1363
1364   change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget)); 
1365 }
1366
1367
1368 /* ******************* *
1369  *  Utility Functions  *
1370  * ******************* */
1371
1372 /* General */
1373 static GtkIconTheme *
1374 get_icon_theme (GtkWidget *widget)
1375 {
1376   if (gtk_widget_has_screen (widget))
1377     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1378
1379   return gtk_icon_theme_get_default ();
1380 }
1381
1382
1383 struct SetDisplayNameData
1384 {
1385   GtkFileChooserButton *button;
1386   GtkTreeRowReference *row_ref;
1387 };
1388
1389 static void
1390 set_info_get_info_cb (GtkFileSystemHandle *handle,
1391                       const GtkFileInfo   *info,
1392                       const GError        *error,
1393                       gpointer             callback_data)
1394 {
1395   gboolean cancelled = handle->cancelled;
1396   GdkPixbuf *pixbuf;
1397   GtkTreePath *path;
1398   GtkTreeIter iter;
1399   GtkFileSystemHandle *model_handle;
1400   struct SetDisplayNameData *data = callback_data;
1401
1402   path = gtk_tree_row_reference_get_path (data->row_ref);
1403   if (!path)
1404     /* Handle doesn't exist anymore in the model */
1405     goto out;
1406
1407   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1408   gtk_tree_path_free (path);
1409
1410   /* Validate the handle */
1411   gtk_tree_model_get (data->button->priv->model, &iter,
1412                       HANDLE_COLUMN, &model_handle,
1413                       -1);
1414   if (handle != model_handle)
1415     goto out;
1416
1417   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1418                       HANDLE_COLUMN, NULL,
1419                       -1);
1420
1421   if (cancelled || error)
1422     /* There was an error, leave the fallback name in there */
1423     goto out;
1424
1425   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1426                                       data->button->priv->icon_size, NULL);
1427
1428   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1429                       ICON_COLUMN, pixbuf,
1430                       DISPLAY_NAME_COLUMN, gtk_file_info_get_display_name (info),
1431                       IS_FOLDER_COLUMN, gtk_file_info_get_is_folder (info),
1432                       -1);
1433
1434   if (pixbuf)
1435     g_object_unref (pixbuf);
1436
1437 out:
1438   g_object_unref (data->button);
1439   gtk_tree_row_reference_free (data->row_ref);
1440   g_free (data);
1441
1442   g_object_unref (handle);
1443 }
1444
1445 static void
1446 set_info_for_path_at_iter (GtkFileChooserButton *button,
1447                            const GtkFilePath    *path,
1448                            GtkTreeIter          *iter)
1449 {
1450   struct SetDisplayNameData *data;
1451   GtkTreePath *tree_path;
1452   GtkFileSystemHandle *handle;
1453
1454   data = g_new0 (struct SetDisplayNameData, 1);
1455   data->button = g_object_ref (button);
1456
1457   tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1458   data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1459   gtk_tree_path_free (tree_path);
1460
1461   handle = gtk_file_system_get_info (button->priv->fs, path,
1462                                      GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_ICON,
1463                                      set_info_get_info_cb, data);
1464
1465   gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1466                       HANDLE_COLUMN, handle,
1467                       -1);
1468 }
1469
1470 /* Shortcuts Model */
1471 static gint
1472 model_get_type_position (GtkFileChooserButton *button,
1473                          RowType               row_type)
1474 {
1475   gint retval = 0;
1476
1477   if (row_type == ROW_TYPE_SPECIAL)
1478     return retval;
1479
1480   retval += button->priv->n_special;
1481
1482   if (row_type == ROW_TYPE_VOLUME)
1483     return retval;
1484
1485   retval += button->priv->n_volumes;
1486
1487   if (row_type == ROW_TYPE_SHORTCUT)
1488     return retval;
1489
1490   retval += button->priv->n_shortcuts;
1491
1492   if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1493     return retval;
1494
1495   retval += button->priv->has_bookmark_separator;
1496
1497   if (row_type == ROW_TYPE_BOOKMARK)
1498     return retval;
1499
1500   retval += button->priv->n_bookmarks;
1501
1502   if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1503     return retval;
1504
1505   retval += button->priv->has_current_folder_separator;
1506
1507   if (row_type == ROW_TYPE_CURRENT_FOLDER)
1508     return retval;
1509
1510   retval += button->priv->has_current_folder;
1511
1512   if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1513     return retval;
1514
1515   retval += button->priv->has_other_separator;
1516
1517   if (row_type == ROW_TYPE_OTHER)
1518     return retval;
1519
1520   g_assert_not_reached ();
1521   return -1;
1522 }
1523
1524 static void
1525 model_free_row_data (GtkFileChooserButton *button,
1526                      GtkTreeIter          *iter)
1527 {
1528   gchar type;
1529   gpointer data;
1530   GtkFileSystemHandle *handle;
1531
1532   gtk_tree_model_get (button->priv->model, iter,
1533                       TYPE_COLUMN, &type,
1534                       DATA_COLUMN, &data,
1535                       HANDLE_COLUMN, &handle,
1536                       -1);
1537
1538   if (handle)
1539     gtk_file_system_cancel_operation (handle);
1540
1541   switch (type)
1542     {
1543     case ROW_TYPE_SPECIAL:
1544     case ROW_TYPE_SHORTCUT:
1545     case ROW_TYPE_BOOKMARK:
1546     case ROW_TYPE_CURRENT_FOLDER:
1547       gtk_file_path_free (data);
1548       break;
1549     case ROW_TYPE_VOLUME:
1550       gtk_file_system_volume_free (button->priv->fs, data);
1551       break;
1552     default:
1553       break;
1554     }
1555 }
1556
1557 static void
1558 model_add_special_get_info_cb (GtkFileSystemHandle *handle,
1559                                const GtkFileInfo   *info,
1560                                const GError        *error,
1561                                gpointer             user_data)
1562 {
1563   gboolean cancelled = handle->cancelled;
1564   GtkTreeIter iter;
1565   GtkTreePath *path;
1566   GdkPixbuf *pixbuf;
1567   GtkFileSystemHandle *model_handle;
1568   struct ChangeIconThemeData *data = user_data;
1569
1570   path = gtk_tree_row_reference_get_path (data->row_ref);
1571   if (!path)
1572     /* Handle doesn't exist anymore in the model */
1573     goto out;
1574
1575   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1576   gtk_tree_path_free (path);
1577
1578   gtk_tree_model_get (data->button->priv->model, &iter,
1579                       HANDLE_COLUMN, &model_handle,
1580                       -1);
1581   if (handle != model_handle)
1582     goto out;
1583
1584   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1585                       HANDLE_COLUMN, NULL,
1586                       -1);
1587
1588   if (cancelled || error)
1589     goto out;
1590
1591   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1592                                       data->button->priv->icon_size, NULL);
1593
1594   if (pixbuf)
1595     {
1596       gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1597                           ICON_COLUMN, pixbuf,
1598                           -1);
1599       g_object_unref (pixbuf);
1600     }
1601
1602   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1603                       DISPLAY_NAME_COLUMN, gtk_file_info_get_display_name (info),
1604                       -1);
1605
1606 out:
1607   gtk_tree_row_reference_free (data->row_ref);
1608   g_free (data);
1609
1610   g_object_unref (handle);
1611 }
1612
1613 static inline void
1614 model_add_special (GtkFileChooserButton *button)
1615 {
1616   const gchar *homedir;
1617   gchar *desktopdir = NULL;
1618   GtkListStore *store;
1619   GtkTreeIter iter;
1620   GtkFilePath *path;
1621   gint pos;
1622
1623   store = GTK_LIST_STORE (button->priv->model);
1624   pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1625
1626   homedir = g_get_home_dir ();
1627
1628   if (homedir)
1629     {
1630       GtkTreePath *tree_path;
1631       GtkFileSystemHandle *handle;
1632       struct ChangeIconThemeData *info;
1633
1634       path = gtk_file_system_filename_to_path (button->priv->fs, homedir);
1635       gtk_list_store_insert (store, &iter, pos);
1636       pos++;
1637
1638       info = g_new0 (struct ChangeIconThemeData, 1);
1639       info->button = g_object_ref (button);
1640       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1641       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1642                                                   tree_path);
1643       gtk_tree_path_free (tree_path);
1644
1645       handle = gtk_file_system_get_info (button->priv->fs, path,
1646                                          GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
1647                                          model_add_special_get_info_cb, info);
1648
1649       gtk_list_store_set (store, &iter,
1650                           ICON_COLUMN, NULL,
1651                           DISPLAY_NAME_COLUMN, NULL,
1652                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1653                           DATA_COLUMN, path,
1654                           IS_FOLDER_COLUMN, TRUE,
1655                           HANDLE_COLUMN, handle,
1656                           -1);
1657
1658       button->priv->n_special++;
1659
1660 #ifndef G_OS_WIN32
1661       desktopdir = g_build_filename (homedir, DESKTOP_DISPLAY_NAME, NULL);
1662 #endif
1663     }
1664
1665 #ifdef G_OS_WIN32
1666   desktopdir = _gtk_file_system_win32_get_desktop ();
1667 #endif
1668
1669   if (desktopdir)
1670     {
1671       GtkTreePath *tree_path;
1672       GtkFileSystemHandle *handle;
1673       struct ChangeIconThemeData *info;
1674
1675       path = gtk_file_system_filename_to_path (button->priv->fs, desktopdir);
1676       g_free (desktopdir);
1677       gtk_list_store_insert (store, &iter, pos);
1678       pos++;
1679
1680       info = g_new0 (struct ChangeIconThemeData, 1);
1681       info->button = g_object_ref (button);
1682       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1683       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1684                                                   tree_path);
1685       gtk_tree_path_free (tree_path);
1686
1687       handle = gtk_file_system_get_info (button->priv->fs, path,
1688                                          GTK_FILE_INFO_ICON,
1689                                          model_add_special_get_info_cb, info);
1690
1691       gtk_list_store_set (store, &iter,
1692                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1693                           ICON_COLUMN, NULL,
1694                           DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1695                           DATA_COLUMN, path,
1696                           IS_FOLDER_COLUMN, TRUE,
1697                           -1);
1698
1699       button->priv->n_special++;
1700     }
1701 }
1702
1703 static void
1704 model_add_volumes (GtkFileChooserButton *button,
1705                    GSList               *volumes)
1706 {
1707   GtkListStore *store;
1708   gint pos;
1709
1710   if (!volumes)
1711     return;
1712
1713   store = GTK_LIST_STORE (button->priv->model);
1714   pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1715
1716   do
1717     {
1718       GtkTreeIter iter;
1719       GdkPixbuf *pixbuf;
1720       gchar *display_name;
1721
1722       pixbuf = gtk_file_system_volume_render_icon (button->priv->fs,
1723                                                    volumes->data,
1724                                                    GTK_WIDGET (button),
1725                                                    button->priv->icon_size,
1726                                                    NULL);
1727       display_name = gtk_file_system_volume_get_display_name (button->priv->fs,
1728                                                               volumes->data);
1729
1730       gtk_list_store_insert (store, &iter, pos);
1731       gtk_list_store_set (store, &iter,
1732                           ICON_COLUMN, pixbuf,
1733                           DISPLAY_NAME_COLUMN, display_name,
1734                           TYPE_COLUMN, ROW_TYPE_VOLUME,
1735                           DATA_COLUMN, volumes->data,
1736                           IS_FOLDER_COLUMN, TRUE,
1737                           -1);
1738
1739       if (pixbuf)
1740         g_object_unref (pixbuf);
1741       g_free (display_name);
1742
1743       button->priv->n_volumes++;
1744       pos++;
1745       volumes = volumes->next;
1746     }
1747   while (volumes);
1748 }
1749
1750 static void
1751 model_add_bookmarks (GtkFileChooserButton *button,
1752                      GSList               *bookmarks)
1753 {
1754   GtkListStore *store;
1755   GtkTreeIter iter;
1756   gint pos;
1757
1758   if (!bookmarks)
1759     return;
1760
1761   store = GTK_LIST_STORE (button->priv->model);
1762   pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
1763
1764   if (!button->priv->has_bookmark_separator)
1765     {
1766       gtk_list_store_insert (store, &iter, pos);
1767       gtk_list_store_set (store, &iter,
1768                           ICON_COLUMN, NULL,
1769                           DISPLAY_NAME_COLUMN, NULL,
1770                           TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
1771                           DATA_COLUMN, NULL,
1772                           IS_FOLDER_COLUMN, FALSE,
1773                           -1);
1774       button->priv->has_bookmark_separator = TRUE;
1775     }
1776
1777   do
1778     {
1779       pos++;
1780
1781       gtk_list_store_insert (store, &iter, pos);
1782       gtk_list_store_set (store, &iter,
1783                           ICON_COLUMN, NULL,
1784                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1785                           TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1786                           DATA_COLUMN, gtk_file_path_copy (bookmarks->data),
1787                           IS_FOLDER_COLUMN, FALSE,
1788                           -1);
1789       set_info_for_path_at_iter (button, bookmarks->data, &iter);
1790
1791       button->priv->n_bookmarks++;
1792       bookmarks = bookmarks->next;
1793     }
1794   while (bookmarks);
1795 }
1796
1797 static void
1798 model_update_current_folder (GtkFileChooserButton *button,
1799                              const GtkFilePath    *path)
1800 {
1801   GtkListStore *store;
1802   GtkTreeIter iter;
1803   gint pos;
1804
1805   if (!path) 
1806     return;
1807
1808   store = GTK_LIST_STORE (button->priv->model);
1809
1810   if (!button->priv->has_current_folder_separator)
1811     {
1812       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
1813       gtk_list_store_insert (store, &iter, pos);
1814       gtk_list_store_set (store, &iter,
1815                           ICON_COLUMN, NULL,
1816                           DISPLAY_NAME_COLUMN, NULL,
1817                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
1818                           DATA_COLUMN, NULL,
1819                           IS_FOLDER_COLUMN, FALSE,
1820                           -1);
1821       button->priv->has_current_folder_separator = TRUE;
1822     }
1823
1824   pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
1825   if (!button->priv->has_current_folder)
1826     {
1827       gtk_list_store_insert (store, &iter, pos);
1828       button->priv->has_current_folder = TRUE;
1829     }
1830   else
1831     {
1832       gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
1833       model_free_row_data (button, &iter);
1834     }
1835
1836   gtk_list_store_set (store, &iter,
1837                       ICON_COLUMN, NULL,
1838                       DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1839                       TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
1840                       DATA_COLUMN, gtk_file_path_copy (path),
1841                       IS_FOLDER_COLUMN, FALSE,
1842                       -1);
1843   set_info_for_path_at_iter (button, path, &iter);
1844 }
1845
1846 static inline void
1847 model_add_other (GtkFileChooserButton *button)
1848 {
1849   GtkListStore *store;
1850   GtkTreeIter iter;
1851   gint pos;
1852   
1853   store = GTK_LIST_STORE (button->priv->model);
1854   pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
1855
1856   gtk_list_store_insert (store, &iter, pos);
1857   gtk_list_store_set (store, &iter,
1858                       ICON_COLUMN, NULL,
1859                       DISPLAY_NAME_COLUMN, NULL,
1860                       TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
1861                       DATA_COLUMN, NULL,
1862                       IS_FOLDER_COLUMN, FALSE,
1863                       -1);
1864   button->priv->has_other_separator = TRUE;
1865   pos++;
1866
1867   gtk_list_store_insert (store, &iter, pos);
1868   gtk_list_store_set (store, &iter,
1869                       ICON_COLUMN, NULL,
1870                       DISPLAY_NAME_COLUMN, _("Other..."),
1871                       TYPE_COLUMN, ROW_TYPE_OTHER,
1872                       DATA_COLUMN, NULL,
1873                       IS_FOLDER_COLUMN, FALSE,
1874                       -1);
1875 }
1876
1877 static void
1878 model_remove_rows (GtkFileChooserButton *button,
1879                    gint                  pos,
1880                    gint                  n_rows)
1881 {
1882   GtkListStore *store;
1883
1884   if (!n_rows)
1885     return;
1886
1887   store = GTK_LIST_STORE (button->priv->model);
1888
1889   do
1890     {
1891       GtkTreeIter iter;
1892
1893       if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
1894         g_assert_not_reached ();
1895
1896       model_free_row_data (button, &iter);
1897       gtk_list_store_remove (store, &iter);
1898       n_rows--;
1899     }
1900   while (n_rows);
1901 }
1902
1903 /* Filter Model */
1904 static inline gboolean
1905 test_if_path_is_visible (GtkFileSystem     *fs,
1906                          const GtkFilePath *path,
1907                          gboolean           local_only,
1908                          gboolean           is_folder)
1909 {
1910   if (!path)
1911     return FALSE;
1912
1913   if (local_only && !gtk_file_system_path_is_local (fs, path))
1914     return FALSE;
1915
1916   if (!is_folder)
1917     return FALSE;
1918
1919   return TRUE;
1920 }
1921
1922 static gboolean
1923 filter_model_visible_func (GtkTreeModel *model,
1924                            GtkTreeIter  *iter,
1925                            gpointer      user_data)
1926 {
1927   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
1928   GtkFileChooserButtonPrivate *priv = button->priv;
1929   gchar type;
1930   gpointer data;
1931   gboolean local_only, retval, is_folder;
1932
1933   type = ROW_TYPE_INVALID;
1934   data = NULL;
1935   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
1936
1937   gtk_tree_model_get (model, iter,
1938                       TYPE_COLUMN, &type,
1939                       DATA_COLUMN, &data,
1940                       IS_FOLDER_COLUMN, &is_folder,
1941                       -1);
1942
1943   switch (type)
1944     {
1945     case ROW_TYPE_CURRENT_FOLDER:
1946       retval = TRUE;
1947       break;
1948     case ROW_TYPE_SPECIAL:
1949     case ROW_TYPE_SHORTCUT:
1950     case ROW_TYPE_BOOKMARK:
1951       retval = test_if_path_is_visible (priv->fs, data, local_only, is_folder);
1952       break;
1953     case ROW_TYPE_VOLUME:
1954       {
1955         GtkFilePath *base_path;
1956
1957         base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
1958         if (base_path)
1959           {
1960             retval = (!local_only ||
1961                       gtk_file_system_path_is_local (priv->fs, base_path));
1962             gtk_file_path_free (base_path);
1963           }
1964         else
1965           retval = FALSE;
1966       }
1967       break;
1968     default:
1969       retval = TRUE;
1970       break;
1971     }
1972
1973   return retval;
1974 }
1975
1976 /* Combo Box */
1977 static void
1978 name_cell_data_func (GtkCellLayout   *layout,
1979                      GtkCellRenderer *cell,
1980                      GtkTreeModel    *model,
1981                      GtkTreeIter     *iter,
1982                      gpointer         user_data)
1983 {
1984   gchar type;
1985
1986   type = 0;
1987   gtk_tree_model_get (model, iter,
1988                       TYPE_COLUMN, &type,
1989                       -1);
1990
1991   if (type == ROW_TYPE_CURRENT_FOLDER)
1992     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1993   else
1994     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
1995 }
1996
1997 static gboolean
1998 combo_box_row_separator_func (GtkTreeModel *model,
1999                               GtkTreeIter  *iter,
2000                               gpointer      user_data)
2001 {
2002   gchar type = ROW_TYPE_INVALID;
2003
2004   gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2005
2006   return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2007           type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2008           type == ROW_TYPE_OTHER_SEPARATOR);
2009 }                         
2010
2011 static void
2012 update_combo_box (GtkFileChooserButton *button)
2013 {
2014   GtkFileChooserButtonPrivate *priv = button->priv;
2015   GSList *paths;
2016   GtkTreeIter iter;
2017   gboolean row_found;
2018
2019   gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2020
2021   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2022
2023   row_found = FALSE;
2024
2025   do
2026     {
2027       gchar type;
2028       gpointer data;
2029
2030       type = ROW_TYPE_INVALID;
2031       data = NULL;
2032
2033       gtk_tree_model_get (priv->filter_model, &iter,
2034                           TYPE_COLUMN, &type,
2035                           DATA_COLUMN, &data,
2036                           -1);
2037     
2038       switch (type)
2039         {
2040         case ROW_TYPE_SPECIAL:
2041         case ROW_TYPE_SHORTCUT:
2042         case ROW_TYPE_BOOKMARK:
2043         case ROW_TYPE_CURRENT_FOLDER:
2044           row_found = (paths &&
2045                        paths->data &&
2046                        gtk_file_path_compare (data, paths->data) == 0);
2047           break;
2048         case ROW_TYPE_VOLUME:
2049           {
2050             GtkFilePath *base_path;
2051
2052             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2053             row_found = (paths &&
2054                          paths->data &&
2055                          gtk_file_path_compare (base_path, paths->data) == 0);
2056             gtk_file_path_free (base_path);
2057           }
2058           break;
2059         default:
2060           row_found = FALSE;
2061           break;
2062         }
2063
2064       if (row_found)
2065         {
2066           g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2067           gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2068                                          &iter);
2069           g_signal_handler_unblock (priv->combo_box,
2070                                     priv->combo_box_changed_id);
2071         }
2072     }
2073   while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2074
2075   /* If it hasn't been found already, update & select the current-folder row. */
2076   if (!row_found && paths && paths->data)
2077     {
2078       GtkTreeIter filter_iter;
2079       gint pos;
2080     
2081       model_update_current_folder (button, paths->data);
2082       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2083
2084       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2085       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2086
2087       gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2088                                                         &filter_iter, &iter);
2089
2090       g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2091       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2092       g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2093     }
2094
2095   gtk_file_paths_free (paths);
2096 }
2097
2098 /* Button */
2099 static void
2100 update_label_get_info_cb (GtkFileSystemHandle *handle,
2101                           const GtkFileInfo   *info,
2102                           const GError        *error,
2103                           gpointer             data)
2104 {
2105   gboolean cancelled = handle->cancelled;
2106   GdkPixbuf *pixbuf;
2107   GtkFileChooserButton *button = data;
2108   GtkFileChooserButtonPrivate *priv = button->priv;
2109
2110   if (handle != priv->update_button_handle)
2111     goto out;
2112
2113   priv->update_button_handle = NULL;
2114
2115   if (cancelled || error)
2116     goto out;
2117
2118   gtk_label_set_text (GTK_LABEL (priv->label), gtk_file_info_get_display_name (info));
2119
2120   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (priv->image),
2121                                       priv->icon_size, NULL);
2122   if (!pixbuf)
2123     pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2124                                        FALLBACK_ICON_NAME,
2125                                        priv->icon_size, 0, NULL);
2126
2127   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2128   if (pixbuf)
2129     g_object_unref (pixbuf);
2130
2131 out:
2132   g_object_unref (button);
2133   g_object_unref (handle);
2134 }
2135
2136 static void
2137 update_label_and_image (GtkFileChooserButton *button)
2138 {
2139   GtkFileChooserButtonPrivate *priv = button->priv;
2140   GdkPixbuf *pixbuf;
2141   gchar *label_text;
2142   GSList *paths;
2143
2144   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2145   label_text = NULL;
2146   pixbuf = NULL;
2147
2148   if (paths && paths->data)
2149     {
2150       GtkFilePath *path;
2151       GtkFileSystemVolume *volume = NULL;
2152     
2153       path = paths->data;
2154
2155       volume = gtk_file_system_get_volume_for_path (priv->fs, path);
2156       if (volume)
2157         {
2158           GtkFilePath *base_path;
2159
2160           base_path = gtk_file_system_volume_get_base_path (priv->fs, volume);
2161           if (base_path && gtk_file_path_compare (base_path, path) == 0)
2162             {
2163               label_text = gtk_file_system_volume_get_display_name (priv->fs,
2164                                                                     volume);
2165               pixbuf = gtk_file_system_volume_render_icon (priv->fs, volume,
2166                                                            GTK_WIDGET (button),
2167                                                            priv->icon_size,
2168                                                            NULL);
2169             }
2170
2171           if (base_path)
2172             gtk_file_path_free (base_path);
2173
2174           gtk_file_system_volume_free (priv->fs, volume);
2175
2176           if (label_text)
2177             goto out;
2178         }
2179
2180       if (priv->update_button_handle)
2181         gtk_file_system_cancel_operation (priv->update_button_handle);
2182
2183       priv->update_button_handle =
2184         gtk_file_system_get_info (priv->fs, path,
2185                                   GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
2186                                   update_label_get_info_cb,
2187                                   g_object_ref (button));
2188     }
2189 out:
2190   gtk_file_paths_free (paths);
2191
2192   if (label_text)
2193     {
2194       gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2195       g_free (label_text);
2196     }
2197   else
2198     gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2199 }
2200
2201
2202 /* ************************ *
2203  *  Child Object Callbacks  *
2204  * ************************ */
2205
2206 /* File System */
2207 static void
2208 fs_volumes_changed_cb (GtkFileSystem *fs,
2209                        gpointer       user_data)
2210 {
2211   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2212   GtkFileChooserButtonPrivate *priv = button->priv;
2213   GSList *volumes;
2214
2215   model_remove_rows (user_data,
2216                      model_get_type_position (user_data, ROW_TYPE_VOLUME),
2217                      priv->n_volumes);
2218
2219   priv->n_volumes = 0;
2220
2221   volumes = gtk_file_system_list_volumes (fs);
2222   model_add_volumes (user_data, volumes);
2223   g_slist_free (volumes);
2224
2225   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2226
2227   update_label_and_image (user_data);
2228   update_combo_box (user_data);
2229 }
2230
2231 static void
2232 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2233                          gpointer       user_data)
2234 {
2235   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2236   GtkFileChooserButtonPrivate *priv = button->priv;
2237   GSList *bookmarks;
2238
2239   bookmarks = gtk_file_system_list_bookmarks (fs);
2240   if (!bookmarks)
2241     {
2242       model_remove_rows (user_data,
2243                          model_get_type_position (user_data,
2244                                                   ROW_TYPE_BOOKMARK_SEPARATOR),
2245                          (priv->n_bookmarks + priv->has_bookmark_separator));
2246       priv->has_bookmark_separator = FALSE;
2247     }
2248   else
2249     model_remove_rows (user_data,
2250                        model_get_type_position (user_data, ROW_TYPE_BOOKMARK),
2251                        priv->n_bookmarks);
2252
2253   priv->n_bookmarks = 0;
2254   model_add_bookmarks (user_data, bookmarks);
2255   gtk_file_paths_free (bookmarks);
2256
2257   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2258
2259   update_label_and_image (user_data);
2260   update_combo_box (user_data);
2261 }
2262
2263 /* Dialog */
2264 static void
2265 open_dialog (GtkFileChooserButton *button)
2266 {
2267   GtkFileChooserButtonPrivate *priv = button->priv;
2268
2269   /* Setup the dialog parent to be chooser button's toplevel, and be modal
2270      as needed. */
2271   if (!GTK_WIDGET_VISIBLE (priv->dialog))
2272     {
2273       GtkWidget *toplevel;
2274
2275       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2276
2277       if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
2278         {
2279           if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2280             gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2281                                           GTK_WINDOW (toplevel));
2282               
2283           gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2284                                 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2285         }
2286     }
2287
2288   if (!priv->active)
2289     {
2290       GSList *paths;
2291
2292       g_signal_handler_block (priv->dialog,
2293                               priv->dialog_folder_changed_id);
2294       g_signal_handler_block (priv->dialog,
2295                               priv->dialog_file_activated_id);
2296       g_signal_handler_block (priv->dialog,
2297                               priv->dialog_selection_changed_id);
2298       paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2299       if (paths)
2300         {
2301           if (paths->data)
2302             priv->old_path = gtk_file_path_copy (paths->data);
2303
2304           gtk_file_paths_free (paths);
2305         }
2306
2307       priv->active = TRUE;
2308     }
2309
2310   gtk_widget_set_sensitive (priv->combo_box, FALSE);
2311   gtk_window_present (GTK_WINDOW (priv->dialog));
2312 }
2313
2314 /* Combo Box */
2315 static void
2316 combo_box_changed_cb (GtkComboBox *combo_box,
2317                       gpointer     user_data)
2318 {
2319   GtkTreeIter iter;
2320
2321   if (gtk_combo_box_get_active_iter (combo_box, &iter))
2322     {
2323       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2324       GtkFileChooserButtonPrivate *priv = button->priv;
2325       gchar type;
2326       gpointer data;
2327
2328       type = ROW_TYPE_INVALID;
2329       data = NULL;
2330
2331       gtk_tree_model_get (priv->filter_model, &iter,
2332                           TYPE_COLUMN, &type,
2333                           DATA_COLUMN, &data,
2334                           -1);
2335
2336       switch (type)
2337         {
2338         case ROW_TYPE_SPECIAL:
2339         case ROW_TYPE_SHORTCUT:
2340         case ROW_TYPE_BOOKMARK:
2341         case ROW_TYPE_CURRENT_FOLDER:
2342           gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2343           if (data)
2344             _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2345                                                        data, NULL);
2346           break;
2347         case ROW_TYPE_VOLUME:
2348           {
2349             GtkFilePath *base_path;
2350
2351             gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2352             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2353             if (base_path)
2354               {
2355                 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2356                                                            base_path, NULL);
2357                 gtk_file_path_free (base_path);
2358               }
2359           }
2360           break;
2361         case ROW_TYPE_OTHER:
2362           open_dialog (user_data);
2363           break;
2364         default:
2365           break;
2366         }
2367     }
2368 }
2369
2370 /* Button */
2371 static void
2372 button_clicked_cb (GtkButton *real_button,
2373                    gpointer   user_data)
2374 {
2375   open_dialog (user_data);
2376 }
2377
2378 /* Dialog */
2379 static void
2380 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2381                                   gpointer        user_data)
2382 {
2383   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2384   GtkFileChooserButtonPrivate *priv = button->priv;
2385
2386   priv->folder_has_been_set = TRUE;
2387
2388   g_signal_emit_by_name (button, "current-folder-changed");
2389 }
2390
2391 static void
2392 dialog_file_activated_cb (GtkFileChooser *dialog,
2393                           gpointer        user_data)
2394 {
2395   g_signal_emit_by_name (user_data, "file-activated");
2396 }
2397
2398 static void
2399 dialog_selection_changed_cb (GtkFileChooser *dialog,
2400                              gpointer        user_data)
2401 {
2402   update_label_and_image (user_data);
2403   update_combo_box (user_data);
2404   g_signal_emit_by_name (user_data, "selection-changed");
2405 }
2406
2407 static void
2408 dialog_update_preview_cb (GtkFileChooser *dialog,
2409                           gpointer        user_data)
2410 {
2411   g_signal_emit_by_name (user_data, "update-preview");
2412 }
2413
2414 static void
2415 dialog_notify_cb (GObject    *dialog,
2416                   GParamSpec *pspec,
2417                   gpointer    user_data)
2418 {
2419   gpointer iface;
2420
2421   iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2422                                  GTK_TYPE_FILE_CHOOSER);
2423   if (g_object_interface_find_property (iface, pspec->name))
2424     g_object_notify (user_data, pspec->name);
2425
2426   if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2427     {
2428       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2429       GtkFileChooserButtonPrivate *priv = button->priv;
2430
2431       if (priv->has_current_folder)
2432         {
2433           GtkTreeIter iter;
2434           gint pos;
2435           gpointer data;
2436
2437           pos = model_get_type_position (user_data,
2438                                          ROW_TYPE_CURRENT_FOLDER);
2439           gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2440
2441           data = NULL;
2442           gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2443
2444           /* If the path isn't local but we're in local-only mode now, remove
2445            * the custom-folder row */
2446           if (data &&
2447               (!gtk_file_system_path_is_local (priv->fs, data) &&
2448                gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog))))
2449             {
2450               pos--;
2451               model_remove_rows (user_data, pos, 2);
2452             }
2453         }
2454
2455       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2456       update_combo_box (user_data);
2457     }
2458 }
2459
2460 static gboolean
2461 dialog_delete_event_cb (GtkWidget *dialog,
2462                         GdkEvent  *event,
2463                         gpointer   user_data)
2464 {
2465   g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2466
2467   return TRUE;
2468 }
2469
2470 static void
2471 dialog_response_cb (GtkDialog *dialog,
2472                     gint       response,
2473                     gpointer   user_data)
2474 {
2475   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2476   GtkFileChooserButtonPrivate *priv = button->priv;
2477
2478   if (response == GTK_RESPONSE_ACCEPT)
2479     {
2480       g_signal_emit_by_name (user_data, "current-folder-changed");
2481       g_signal_emit_by_name (user_data, "selection-changed");
2482     }
2483   else if (priv->old_path)
2484     {
2485       switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)))
2486         {
2487         case GTK_FILE_CHOOSER_ACTION_OPEN:
2488           _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (dialog), priv->old_path,
2489                                          NULL);
2490           break;
2491         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
2492           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (dialog),
2493                                                      priv->old_path, NULL);
2494           break;
2495         default:
2496           g_assert_not_reached ();
2497           break;
2498         }
2499     }
2500   else
2501     gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (dialog));
2502
2503   if (priv->old_path)
2504     {
2505       gtk_file_path_free (priv->old_path);
2506       priv->old_path = NULL;
2507     }
2508
2509   update_label_and_image (user_data);
2510   update_combo_box (user_data);
2511   
2512   if (priv->active)
2513     {
2514       g_signal_handler_unblock (priv->dialog,
2515                                 priv->dialog_folder_changed_id);
2516       g_signal_handler_unblock (priv->dialog,
2517                                 priv->dialog_file_activated_id);
2518       g_signal_handler_unblock (priv->dialog,
2519                                 priv->dialog_selection_changed_id);
2520       priv->active = FALSE;
2521     }
2522
2523   gtk_widget_set_sensitive (priv->combo_box, TRUE);
2524   gtk_widget_hide (priv->dialog);
2525 }
2526
2527
2528 /* ************************************************************************** *
2529  *  Public API                                                                *
2530  * ************************************************************************** */
2531
2532 /**
2533  * gtk_file_chooser_button_new:
2534  * @title: the title of the browse dialog.
2535  * @action: the open mode for the widget.
2536  * 
2537  * Creates a new file-selecting button widget.
2538  * 
2539  * Returns: a new button widget.
2540  * 
2541  * Since: 2.6
2542  **/
2543 GtkWidget *
2544 gtk_file_chooser_button_new (const gchar          *title,
2545                              GtkFileChooserAction  action)
2546 {
2547   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2548                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2549
2550   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2551                        "action", action,
2552                        "title", (title ? title : _(DEFAULT_TITLE)),
2553                        NULL);
2554 }
2555
2556 /**
2557  * gtk_file_chooser_button_new_with_backend:
2558  * @title: the title of the browse dialog.
2559  * @action: the open mode for the widget.
2560  * @backend: the name of the #GtkFileSystem backend to use.
2561  * 
2562  * Creates a new file-selecting button widget using @backend.
2563  * 
2564  * Returns: a new button widget.
2565  * 
2566  * Since: 2.6
2567  **/
2568 GtkWidget *
2569 gtk_file_chooser_button_new_with_backend (const gchar          *title,
2570                                           GtkFileChooserAction  action,
2571                                           const gchar          *backend)
2572 {
2573   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2574                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2575
2576   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2577                        "action", action,
2578                        "title", (title ? title : _(DEFAULT_TITLE)),
2579                        "file-system-backend", backend,
2580                        NULL);
2581 }
2582
2583 /**
2584  * gtk_file_chooser_button_new_with_dialog:
2585  * @dialog: the #GtkFileChooserDialog widget to use.
2586  * 
2587  * Creates a #GtkFileChooserButton widget which uses @dialog as it's
2588  * file-picking window. Note that @dialog must be a #GtkFileChooserDialog (or
2589  * subclass) and must not have %GTK_DIALOG_DESTROY_WITH_PARENT set.
2590  * 
2591  * Returns: a new button widget.
2592  * 
2593  * Since: 2.6
2594  **/
2595 GtkWidget *
2596 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2597 {
2598   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_DIALOG (dialog), NULL);
2599
2600   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2601                        "dialog", dialog,
2602                        NULL);
2603 }
2604
2605 /**
2606  * gtk_file_chooser_button_set_title:
2607  * @button: the button widget to modify.
2608  * @title: the new browse dialog title.
2609  * 
2610  * Modifies the @title of the browse dialog used by @button.
2611  * 
2612  * Since: 2.6
2613  **/
2614 void
2615 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2616                                    const gchar          *title)
2617 {
2618   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2619
2620   gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2621   g_object_notify (G_OBJECT (button), "title");
2622 }
2623
2624 /**
2625  * gtk_file_chooser_button_get_title:
2626  * @button: the button widget to examine.
2627  * 
2628  * Retrieves the title of the browse dialog used by @button. The returned value
2629  * should not be modified or freed.
2630  * 
2631  * Returns: a pointer to the browse dialog's title.
2632  * 
2633  * Since: 2.6
2634  **/
2635 G_CONST_RETURN gchar *
2636 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2637 {
2638   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2639
2640   return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2641 }
2642
2643 /**
2644  * gtk_file_chooser_button_get_width_chars:
2645  * @button: the button widget to examine.
2646  * 
2647  * Retrieves the width in characters of the @button widget's entry and/or label.
2648  * 
2649  * Returns: an integer width (in characters) that the button will use to size itself.
2650  * 
2651  * Since: 2.6
2652  **/
2653 gint
2654 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2655 {
2656   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2657
2658   return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2659 }
2660
2661 /**
2662  * gtk_file_chooser_button_set_width_chars:
2663  * @button: the button widget to examine.
2664  * @n_chars: the new width, in characters.
2665  * 
2666  * Sets the width (in characters) that @button will use to @n_chars.
2667  * 
2668  * Since: 2.6
2669  **/
2670 void
2671 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
2672                                          gint                  n_chars)
2673 {
2674   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2675
2676   gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
2677   g_object_notify (G_OBJECT (button), "width-chars");
2678 }
2679
2680 /**
2681  * gtk_file_chooser_button_set_focus_on_click:
2682  * @button: a #GtkFileChooserButton
2683  * @focus_on_click: whether the button grabs focus when clicked with the mouse
2684  * 
2685  * Sets whether the button will grab focus when it is clicked with the mouse.
2686  * Making mouse clicks not grab focus is useful in places like toolbars where
2687  * you don't want the keyboard focus removed from the main area of the
2688  * application.
2689  *
2690  * Since: 2.10
2691  **/
2692 void
2693 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
2694                                             gboolean              focus_on_click)
2695 {
2696   GtkFileChooserButtonPrivate *priv;
2697
2698   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2699
2700   priv = button->priv;
2701
2702   focus_on_click = focus_on_click != FALSE;
2703
2704   if (priv->focus_on_click != focus_on_click)
2705     {
2706       priv->focus_on_click = focus_on_click;
2707       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
2708       gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
2709       
2710       g_object_notify (G_OBJECT (button), "focus-on-click");
2711     }
2712 }
2713
2714 /**
2715  * gtk_file_chooser_button_get_focus_on_click:
2716  * @button: a #GtkFileChooserButton
2717  * 
2718  * Returns whether the button grabs focus when it is clicked with the mouse.
2719  * See gtk_file_chooser_button_set_focus_on_click().
2720  *
2721  * Return value: %TRUE if the button grabs focus when it is clicked with
2722  *               the mouse.
2723  *
2724  * Since: 2.10
2725  **/
2726 gboolean
2727 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
2728 {
2729   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
2730   
2731   return button->priv->focus_on_click;
2732 }
2733
2734 #define __GTK_FILE_CHOOSER_BUTTON_C__
2735 #include "gtkaliasdef.c"