]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserbutton.c
Don't add a shortcut here. (#162752, Tor Lillqvist)
[~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 "gtkalias.h"
34 #include "gtkintl.h"
35 #include "gtkbutton.h"
36 #include "gtkcelllayout.h"
37 #include "gtkcellrenderertext.h"
38 #include "gtkcellrendererpixbuf.h"
39 #include "gtkcombobox.h"
40 #include "gtkdnd.h"
41 #include "gtkicontheme.h"
42 #include "gtkiconfactory.h"
43 #include "gtkimage.h"
44 #include "gtklabel.h"
45 #include "gtkliststore.h"
46 #include "gtkstock.h"
47 #include "gtktreemodelfilter.h"
48 #include "gtkvseparator.h"
49 #include "gtkfilechooserdialog.h"
50 #include "gtkfilechooserprivate.h"
51 #include "gtkfilechooserutils.h"
52
53 #include "gtkfilechooserbutton.h"
54
55 #ifdef G_OS_WIN32
56 #include "gtkfilesystemwin32.h"
57 #endif
58
59 /* **************** *
60  *  Private Macros  *
61  * **************** */
62
63 #define GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE(object) (GTK_FILE_CHOOSER_BUTTON ((object))->priv)
64
65 #define HOME_DISPLAY_NAME       N_("Home")
66 #define DESKTOP_DISPLAY_NAME    N_("Desktop")
67 #define FALLBACK_DISPLAY_NAME   N_("(None)")
68 #define FALLBACK_ICON_NAME      "stock_unknown"
69 #define FALLBACK_ICON_SIZE      20
70
71
72 /* ********************** *
73  *  Private Enumerations  *
74  * ********************** */
75
76 /* Property IDs */
77 enum
78 {
79   PROP_0,
80
81   PROP_DIALOG,
82   PROP_TITLE,
83   PROP_WIDTH_CHARS
84 };
85
86 /* TreeModel Columns */
87 enum
88 {
89   ICON_COLUMN,
90   DISPLAY_NAME_COLUMN,
91   TYPE_COLUMN,
92   DATA_COLUMN,
93   NUM_COLUMNS
94 };
95
96 /* TreeModel Row Types */
97 typedef enum
98 {
99   ROW_TYPE_SPECIAL,
100   ROW_TYPE_VOLUME,
101   ROW_TYPE_SHORTCUT,
102   ROW_TYPE_BOOKMARK_SEPARATOR,
103   ROW_TYPE_BOOKMARK,
104   ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
105   ROW_TYPE_CURRENT_FOLDER,
106   ROW_TYPE_OTHER_SEPARATOR,
107   ROW_TYPE_OTHER,
108
109   ROW_TYPE_INVALID = -1
110 }
111 RowType;
112
113
114 /* ******************** *
115  *  Private Structures  *
116  * ******************** */
117
118 struct _GtkFileChooserButtonPrivate
119 {
120   GtkWidget *dialog;
121   GtkWidget *button;
122   GtkWidget *image;
123   GtkWidget *label;
124   GtkWidget *combo_box;
125   GtkCellRenderer *icon_cell;
126   GtkCellRenderer *name_cell;
127
128   GtkTreeModel *model;
129   GtkTreeModel *filter_model;
130
131   gchar *backend;
132   GtkFileSystem *fs;
133   GtkFilePath *old_path;
134
135   gulong combo_box_changed_id;
136   gulong dialog_file_activated_id;
137   gulong dialog_folder_changed_id;
138   gulong dialog_selection_changed_id;
139   gulong fs_volumes_changed_id;
140   gulong fs_bookmarks_changed_id;
141
142   gint icon_size;
143
144   guint8 n_special;
145   guint8 n_volumes;
146   guint8 n_shortcuts;
147   guint8 n_bookmarks;
148   guint8 has_bookmark_separator       : 1;
149   guint8 has_current_folder_separator : 1;
150   guint8 has_current_folder           : 1;
151   guint8 has_other_separator          : 1;
152
153   /* Used for hiding/showing the dialog when the button is hidden */
154   guint8 active                       : 1;
155 };
156
157
158 /* ************* *
159  *  DnD Support  *
160  * ************* */
161
162 enum
163 {
164   TEXT_PLAIN,
165   TEXT_URI_LIST
166 };
167
168
169 /* ********************* *
170  *  Function Prototypes  *
171  * ********************* */
172
173 /* GtkFileChooserIface Functions */
174 static void     gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface);
175 static gboolean gtk_file_chooser_button_add_shortcut_folder     (GtkFileChooser      *chooser,
176                                                                  const GtkFilePath   *path,
177                                                                  GError             **error);
178 static gboolean gtk_file_chooser_button_remove_shortcut_folder  (GtkFileChooser      *chooser,
179                                                                  const GtkFilePath   *path,
180                                                                  GError             **error);
181
182 /* GObject Functions */
183 static GObject *gtk_file_chooser_button_constructor        (GType             type,
184                                                             guint             n_params,
185                                                             GObjectConstructParam *params);
186 static void     gtk_file_chooser_button_set_property       (GObject          *object,
187                                                             guint             param_id,
188                                                             const GValue     *value,
189                                                             GParamSpec       *pspec);
190 static void     gtk_file_chooser_button_get_property       (GObject          *object,
191                                                             guint             param_id,
192                                                             GValue           *value,
193                                                             GParamSpec       *pspec);
194 static void     gtk_file_chooser_button_finalize           (GObject          *object);
195
196 /* GtkObject Functions */
197 static void     gtk_file_chooser_button_destroy            (GtkObject        *object);
198
199 /* GtkWidget Functions */
200 static void     gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
201                                                             GdkDragContext   *context,
202                                                             gint              x,
203                                                             gint              y,
204                                                             GtkSelectionData *data,
205                                                             guint             info,
206                                                             guint             drag_time);
207 static void     gtk_file_chooser_button_show_all           (GtkWidget        *widget);
208 static void     gtk_file_chooser_button_hide_all           (GtkWidget        *widget);
209 static void     gtk_file_chooser_button_show               (GtkWidget        *widget);
210 static void     gtk_file_chooser_button_hide               (GtkWidget        *widget);
211 static gboolean gtk_file_chooser_button_mnemonic_activate  (GtkWidget        *widget,
212                                                             gboolean          group_cycling);
213 static void     gtk_file_chooser_button_style_set          (GtkWidget        *widget,
214                                                             GtkStyle         *old_style);
215 static void     gtk_file_chooser_button_screen_changed     (GtkWidget        *widget,
216                                                             GdkScreen        *old_screen);
217
218 /* Utility Functions */
219 static GtkIconTheme *get_icon_theme               (GtkWidget            *widget);
220 static gchar        *get_display_name_for_path    (GtkFileSystem     *fs,
221                                                    const GtkFilePath *path);
222
223 static gint          model_get_type_position      (GtkFileChooserButton *button,
224                                                    RowType               row_type);
225 static void          model_free_row_data          (GtkFileChooserButton *button,
226                                                    GtkTreeIter          *iter);
227 static inline void   model_add_special            (GtkFileChooserButton *button);
228 static inline void   model_add_other              (GtkFileChooserButton *button);
229 static void          model_add_volumes            (GtkFileChooserButton *button,
230                                                    GSList               *volumes);
231 static void          model_add_bookmarks          (GtkFileChooserButton *button,
232                                                    GSList               *bookmarks);
233 static void          model_update_current_folder  (GtkFileChooserButton *button,
234                                                    const GtkFilePath    *path);
235 static void          model_remove_rows            (GtkFileChooserButton *button,
236                                                    gint                  pos,
237                                                    gint                  n_rows);
238
239 static gboolean      filter_model_visible_func    (GtkTreeModel         *model,
240                                                    GtkTreeIter          *iter,
241                                                    gpointer              user_data);
242
243 static gboolean      combo_box_row_separator_func (GtkTreeModel         *model,
244                                                    GtkTreeIter          *iter,
245                                                    gpointer              user_data);
246 static void          name_cell_data_func          (GtkCellLayout        *layout,
247                                                    GtkCellRenderer      *cell,
248                                                    GtkTreeModel         *model,
249                                                    GtkTreeIter          *iter,
250                                                    gpointer              user_data);
251 static void          open_dialog                  (GtkFileChooserButton *button);
252 static void          update_combo_box             (GtkFileChooserButton *button);
253 static void          update_label_and_image       (GtkFileChooserButton *button);
254
255 /* Child Object Callbacks */
256 static void     fs_volumes_changed_cb            (GtkFileSystem  *fs,
257                                                   gpointer        user_data);
258 static void     fs_bookmarks_changed_cb          (GtkFileSystem  *fs,
259                                                   gpointer        user_data);
260
261 static void     combo_box_changed_cb             (GtkComboBox    *combo_box,
262                                                   gpointer        user_data);
263
264 static void     button_clicked_cb                (GtkButton      *real_button,
265                                                   gpointer        user_data);
266
267 static void     dialog_update_preview_cb         (GtkFileChooser *dialog,
268                                                   gpointer        user_data);
269 static void     dialog_selection_changed_cb      (GtkFileChooser *dialog,
270                                                   gpointer        user_data);
271 static void     dialog_file_activated_cb         (GtkFileChooser *dialog,
272                                                   gpointer        user_data);
273 static void     dialog_current_folder_changed_cb (GtkFileChooser *dialog,
274                                                   gpointer        user_data);
275 static void     dialog_notify_cb                 (GObject        *dialog,
276                                                   GParamSpec     *pspec,
277                                                   gpointer        user_data);
278 static gboolean dialog_delete_event_cb           (GtkWidget      *dialog,
279                                                   GdkEvent       *event,
280                                                   gpointer        user_data);
281 static void     dialog_response_cb               (GtkDialog      *dialog,
282                                                   gint            response,
283                                                   gpointer        user_data);
284
285
286 /* ******************* *
287  *  GType Declaration  *
288  * ******************* */
289
290 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserButton, gtk_file_chooser_button, GTK_TYPE_HBOX, { \
291     G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER, gtk_file_chooser_button_file_chooser_iface_init) \
292 });
293
294
295 /* ***************** *
296  *  GType Functions  *
297  * ***************** */
298
299 static void
300 gtk_file_chooser_button_class_init (GtkFileChooserButtonClass * class)
301 {
302   GObjectClass *gobject_class;
303   GtkObjectClass *gtkobject_class;
304   GtkWidgetClass *widget_class;
305
306   gobject_class = G_OBJECT_CLASS (class);
307   gtkobject_class = GTK_OBJECT_CLASS (class);
308   widget_class = GTK_WIDGET_CLASS (class);
309
310   gobject_class->constructor = gtk_file_chooser_button_constructor;
311   gobject_class->set_property = gtk_file_chooser_button_set_property;
312   gobject_class->get_property = gtk_file_chooser_button_get_property;
313   gobject_class->finalize = gtk_file_chooser_button_finalize;
314
315   gtkobject_class->destroy = gtk_file_chooser_button_destroy;
316
317   widget_class->drag_data_received = gtk_file_chooser_button_drag_data_received;
318   widget_class->show_all = gtk_file_chooser_button_show_all;
319   widget_class->hide_all = gtk_file_chooser_button_hide_all;
320   widget_class->show = gtk_file_chooser_button_show;
321   widget_class->hide = gtk_file_chooser_button_hide;
322   widget_class->style_set = gtk_file_chooser_button_style_set;
323   widget_class->screen_changed = gtk_file_chooser_button_screen_changed;
324   widget_class->mnemonic_activate = gtk_file_chooser_button_mnemonic_activate;
325
326   /**
327    * GtkFileChooserButton:dialog:
328    * 
329    * Instance of the #GtkFileChooserDialog associated with the button.
330    *
331    * Since: 2.6
332    */
333   g_object_class_install_property (gobject_class, PROP_DIALOG,
334                                    g_param_spec_object ("dialog",
335                                                         P_("Dialog"),
336                                                         P_("The file chooser dialog to use."),
337                                                         GTK_TYPE_FILE_CHOOSER_DIALOG,
338                                                         (G_PARAM_WRITABLE |
339                                                          G_PARAM_CONSTRUCT_ONLY)));
340
341   /**
342    * GtkFileChooserButton:title:
343    * 
344    * Title to put on the #GtkFileChooserDialog associated with the button.
345    *
346    * Since: 2.6
347    */
348   g_object_class_install_property (gobject_class, PROP_TITLE,
349                                    g_param_spec_string ("title",
350                                                         P_("Title"),
351                                                         P_("The title of the file chooser dialog."),
352                                                         _("Select a File"),
353                                                         G_PARAM_READWRITE));
354
355   /**
356    * GtkFileChooserButton:width-chars:
357    * 
358    * The width of the entry and label inside the button, in characters.
359    *
360    * Since: 2.6
361    */
362   g_object_class_install_property (gobject_class, PROP_WIDTH_CHARS,
363                                    g_param_spec_int ("width-chars",
364                                                      P_("Width In Characters"),
365                                                      P_("The desired width of the button widget, in characters."),
366                                                      -1, G_MAXINT, -1,
367                                                      G_PARAM_READWRITE));
368
369   _gtk_file_chooser_install_properties (gobject_class);
370
371   g_type_class_add_private (class, sizeof (GtkFileChooserButtonPrivate));
372 }
373
374 static void
375 gtk_file_chooser_button_init (GtkFileChooserButton *button)
376 {
377   GtkFileChooserButtonPrivate *priv;
378   GtkWidget *box, *image, *sep;
379   GtkTargetList *target_list;
380
381   priv = G_TYPE_INSTANCE_GET_PRIVATE (button, GTK_TYPE_FILE_CHOOSER_BUTTON,
382                                       GtkFileChooserButtonPrivate);
383   button->priv = priv;
384
385   priv->icon_size = FALLBACK_ICON_SIZE;
386
387   gtk_widget_push_composite_child ();
388
389   /* Button */
390   priv->button = gtk_button_new ();
391   g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked_cb),
392                     button);
393   gtk_container_add (GTK_CONTAINER (button), priv->button);
394   gtk_widget_show (priv->button);
395
396   box = gtk_hbox_new (FALSE, 4);
397   gtk_container_add (GTK_CONTAINER (priv->button), box);
398   gtk_widget_show (box);
399
400   priv->image = gtk_image_new ();
401   gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
402   gtk_widget_show (priv->image);
403
404   priv->label = gtk_label_new (_(FALLBACK_DISPLAY_NAME));
405   gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
406   gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
407   gtk_container_add (GTK_CONTAINER (box), priv->label);
408   gtk_widget_show (priv->label);
409
410   sep = gtk_vseparator_new ();
411   gtk_box_pack_start (GTK_BOX (box), sep, FALSE, FALSE, 0);
412   gtk_widget_show (sep);
413
414   image = gtk_image_new_from_stock (GTK_STOCK_OPEN,
415                                     GTK_ICON_SIZE_SMALL_TOOLBAR);
416   gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 0);
417   gtk_widget_show (image);
418
419   /* Combo Box */
420   /* Keep in sync with columns enum, line 88 */
421   priv->model =
422     GTK_TREE_MODEL (gtk_list_store_new (NUM_COLUMNS,
423                                         GDK_TYPE_PIXBUF, /* Icon */
424                                         G_TYPE_STRING,   /* Display Name */
425                                         G_TYPE_CHAR,     /* Row Type */
426                                         G_TYPE_POINTER   /* Volume || Path */));
427
428   priv->combo_box = gtk_combo_box_new ();
429   priv->combo_box_changed_id =
430     g_signal_connect (priv->combo_box, "changed",
431                       G_CALLBACK (combo_box_changed_cb), button);
432   gtk_container_add (GTK_CONTAINER (button), priv->combo_box);
433
434   priv->icon_cell = gtk_cell_renderer_pixbuf_new ();
435   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
436                               priv->icon_cell, FALSE);
437   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
438                                  priv->icon_cell, "pixbuf", ICON_COLUMN);
439
440   priv->name_cell = gtk_cell_renderer_text_new ();
441   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (priv->combo_box),
442                               priv->name_cell, TRUE);
443   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->combo_box),
444                                  priv->name_cell, "text", DISPLAY_NAME_COLUMN);
445   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->combo_box),
446                                       priv->name_cell, name_cell_data_func,
447                                       NULL, NULL);
448
449   gtk_widget_pop_composite_child ();
450
451   /* DnD */
452   gtk_drag_dest_set (GTK_WIDGET (button),
453                      (GTK_DEST_DEFAULT_ALL),
454                      NULL, 0,
455                      GDK_ACTION_COPY);
456   target_list = gtk_target_list_new (NULL, 0);
457   gtk_target_list_add_uri_targets (target_list, TEXT_URI_LIST);
458   gtk_target_list_add_text_targets (target_list, TEXT_PLAIN);
459   gtk_drag_dest_set_target_list (GTK_WIDGET (button), target_list);
460   gtk_target_list_unref (target_list);
461 }
462
463
464 /* ******************************* *
465  *  GtkFileChooserIface Functions  *
466  * ******************************* */
467 static void
468 gtk_file_chooser_button_file_chooser_iface_init (GtkFileChooserIface *iface)
469 {
470   _gtk_file_chooser_delegate_iface_init (iface);
471
472   iface->add_shortcut_folder = gtk_file_chooser_button_add_shortcut_folder;
473   iface->remove_shortcut_folder = gtk_file_chooser_button_remove_shortcut_folder;
474 }
475
476 static gboolean
477 gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser     *chooser,
478                                              const GtkFilePath  *path,
479                                              GError            **error)
480 {
481   GtkFileChooser *delegate;
482   gboolean retval;
483
484   delegate = g_object_get_qdata (G_OBJECT (chooser),
485                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
486   retval = _gtk_file_chooser_add_shortcut_folder (delegate, path, error);
487
488   if (retval)
489     {
490       GtkFileChooserButtonPrivate *priv;
491       GtkTreeIter iter;
492       gint pos;
493       GdkPixbuf *pixbuf;
494       gchar *display_name;
495
496       priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (chooser);
497
498       pos = model_get_type_position (GTK_FILE_CHOOSER_BUTTON (chooser),
499                                      ROW_TYPE_SHORTCUT);
500       pos += priv->n_shortcuts;
501
502       pixbuf = gtk_file_system_render_icon (priv->fs, path,
503                                             GTK_WIDGET (chooser),
504                                             priv->icon_size, NULL);
505       display_name = get_display_name_for_path (priv->fs, path);
506
507       gtk_list_store_insert (GTK_LIST_STORE (priv->model), &iter, pos);
508       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
509                           ICON_COLUMN, pixbuf,
510                           DISPLAY_NAME_COLUMN, display_name,
511                           TYPE_COLUMN, ROW_TYPE_SHORTCUT,
512                           DATA_COLUMN, gtk_file_path_copy (path),
513                           -1);
514       priv->n_shortcuts++;
515
516       if (pixbuf)
517         g_object_unref (pixbuf);
518       g_free (display_name);
519
520       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
521     }
522
523   return retval;
524 }
525
526 static gboolean
527 gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser     *chooser,
528                                                 const GtkFilePath  *path,
529                                                 GError            **error)
530 {
531   GtkFileChooser *delegate;
532   gboolean retval;
533
534   delegate = g_object_get_qdata (G_OBJECT (chooser),
535                                  GTK_FILE_CHOOSER_DELEGATE_QUARK);
536
537   retval = _gtk_file_chooser_remove_shortcut_folder (delegate, path, error);
538
539   if (retval)
540     {
541       GtkFileChooserButtonPrivate *priv;
542       GtkTreeIter iter;
543       gint pos;
544       gchar type;
545
546       priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (chooser);
547
548       pos = model_get_type_position (GTK_FILE_CHOOSER_BUTTON (chooser),
549                                      ROW_TYPE_SHORTCUT);
550       g_assert (gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos));
551
552       do
553         {
554           gpointer data;
555
556           gtk_tree_model_get (priv->model, &iter,
557                               TYPE_COLUMN, &type,
558                               DATA_COLUMN, &data,
559                               -1);
560
561           if (type == ROW_TYPE_SHORTCUT &&
562               data &&
563               gtk_file_path_compare (data, path) == 0)
564             {
565               model_free_row_data (GTK_FILE_CHOOSER_BUTTON (chooser), &iter);
566               gtk_list_store_remove (GTK_LIST_STORE (priv->model), &iter);
567               priv->n_shortcuts--;
568               gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
569               update_combo_box (GTK_FILE_CHOOSER_BUTTON (chooser));
570               break;
571             }
572         }
573       while (type == ROW_TYPE_SHORTCUT &&
574              gtk_tree_model_iter_next (priv->model, &iter));
575     }
576
577   return retval;
578 }
579
580
581 /* ******************* *
582  *  GObject Functions  *
583  * ******************* */
584
585 static GObject *
586 gtk_file_chooser_button_constructor (GType                  type,
587                                      guint                  n_params,
588                                      GObjectConstructParam *params)
589 {
590   GObject *object;
591   GtkFileChooserButtonPrivate *priv;
592   GSList *list;
593
594   object = (*G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->constructor) (type,
595                                                                                   n_params,
596                                                                                   params);
597   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (object);
598
599   if (!priv->dialog)
600     {
601       if (priv->backend)
602         priv->dialog = gtk_file_chooser_dialog_new_with_backend (NULL, NULL,
603                                                                  GTK_FILE_CHOOSER_ACTION_OPEN,
604                                                                  priv->backend,
605                                                                  GTK_STOCK_CANCEL,
606                                                                  GTK_RESPONSE_CANCEL,
607                                                                  GTK_STOCK_OPEN,
608                                                                  GTK_RESPONSE_ACCEPT,
609                                                                  NULL);
610       else
611         priv->dialog = gtk_file_chooser_dialog_new (NULL, NULL,
612                                                     GTK_FILE_CHOOSER_ACTION_OPEN,
613                                                     GTK_STOCK_CANCEL,
614                                                     GTK_RESPONSE_CANCEL,
615                                                     GTK_STOCK_OPEN,
616                                                     GTK_RESPONSE_ACCEPT,
617                                                     NULL);
618
619       gtk_dialog_set_default_response (GTK_DIALOG (priv->dialog),
620                                        GTK_RESPONSE_ACCEPT);
621       gtk_dialog_set_alternative_button_order (GTK_DIALOG (priv->dialog),
622                                                GTK_RESPONSE_ACCEPT,
623                                                GTK_RESPONSE_CANCEL,
624                                                -1);
625     }
626
627   g_free (priv->backend);
628   priv->backend = NULL;
629
630   g_signal_connect (priv->dialog, "delete-event",
631                     G_CALLBACK (dialog_delete_event_cb), object);
632   g_signal_connect (priv->dialog, "response",
633                     G_CALLBACK (dialog_response_cb), object);
634
635   /* This is used, instead of the standard delegate, to ensure that signals are only
636    * delegated when the OK button is pressed. */
637   g_object_set_qdata (object, GTK_FILE_CHOOSER_DELEGATE_QUARK, priv->dialog);
638   priv->dialog_folder_changed_id =
639     g_signal_connect (priv->dialog, "current-folder-changed",
640                       G_CALLBACK (dialog_current_folder_changed_cb), object);
641   priv->dialog_file_activated_id =
642     g_signal_connect (priv->dialog, "file-activated",
643                       G_CALLBACK (dialog_file_activated_cb), object);
644   priv->dialog_selection_changed_id =
645     g_signal_connect (priv->dialog, "selection-changed",
646                       G_CALLBACK (dialog_selection_changed_cb), object);
647   g_signal_connect (priv->dialog, "update-preview",
648                     G_CALLBACK (dialog_update_preview_cb), object);
649   g_signal_connect (priv->dialog, "notify",
650                     G_CALLBACK (dialog_notify_cb), object);
651   g_object_add_weak_pointer (G_OBJECT (priv->dialog),
652                              (gpointer *) (&priv->dialog));
653
654   priv->fs =
655     g_object_ref (_gtk_file_chooser_get_file_system (GTK_FILE_CHOOSER (priv->dialog)));
656
657   model_add_special (GTK_FILE_CHOOSER_BUTTON (object));
658
659   list = gtk_file_system_list_volumes (priv->fs);
660   model_add_volumes (GTK_FILE_CHOOSER_BUTTON (object), list);
661   g_slist_free (list);
662
663   list = gtk_file_system_list_bookmarks (priv->fs);
664   model_add_bookmarks (GTK_FILE_CHOOSER_BUTTON (object), list);
665   gtk_file_paths_free (list);
666
667   model_add_other (GTK_FILE_CHOOSER_BUTTON (object));
668
669   priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
670   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
671                                           filter_model_visible_func,
672                                           object, NULL);
673
674   gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo_box), priv->filter_model);
675   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->combo_box),
676                                         combo_box_row_separator_func,
677                                         NULL, NULL);
678
679   update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
680   update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
681
682   priv->fs_volumes_changed_id =
683     g_signal_connect (priv->fs, "volumes-changed",
684                       G_CALLBACK (fs_volumes_changed_cb), object);
685   priv->fs_bookmarks_changed_id =
686     g_signal_connect (priv->fs, "bookmarks-changed",
687                       G_CALLBACK (fs_bookmarks_changed_cb), object);
688
689   return object;
690 }
691
692 static void
693 gtk_file_chooser_button_set_property (GObject      *object,
694                                       guint         param_id,
695                                       const GValue *value,
696                                       GParamSpec   *pspec)
697 {
698   GtkFileChooserButtonPrivate *priv;
699
700   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (object);
701
702   switch (param_id)
703     {
704     case PROP_DIALOG:
705       /* Construct-only */
706       priv->dialog = g_value_get_object (value);
707       break;
708     case PROP_WIDTH_CHARS:
709       gtk_file_chooser_button_set_width_chars (GTK_FILE_CHOOSER_BUTTON (object),
710                                                g_value_get_int (value));
711       break;
712
713     case GTK_FILE_CHOOSER_PROP_ACTION:
714       switch (g_value_get_enum (value))
715         {
716         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
717         case GTK_FILE_CHOOSER_ACTION_SAVE:
718           {
719             GEnumClass *eclass;
720             GEnumValue *eval;
721
722             eclass = g_type_class_peek (GTK_TYPE_FILE_CHOOSER_ACTION);
723             eval = g_enum_get_value (eclass, g_value_get_enum (value));
724             g_warning ("%s: Choosers of type `%s' do not support `%s'.",
725                        G_STRFUNC, G_OBJECT_TYPE_NAME (object), eval->value_name);
726
727             g_value_set_enum ((GValue *) value, GTK_FILE_CHOOSER_ACTION_OPEN);
728           }
729           break;
730         }
731
732       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
733       update_label_and_image (GTK_FILE_CHOOSER_BUTTON (object));
734       update_combo_box (GTK_FILE_CHOOSER_BUTTON (object));
735
736       switch (g_value_get_enum (value))
737         {
738         case GTK_FILE_CHOOSER_ACTION_OPEN:
739           gtk_widget_hide (priv->combo_box);
740           gtk_widget_show (priv->button);
741           break;
742         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
743           gtk_widget_hide (priv->button);
744           gtk_widget_show (priv->combo_box);
745           break;
746         default:
747           g_assert_not_reached ();
748           break;
749         }
750       break;
751
752     case PROP_TITLE:
753     case GTK_FILE_CHOOSER_PROP_FILTER:
754     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
755     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
756     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
757     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
758     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
759     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
760       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
761       break;
762
763     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
764       /* Construct-only */
765       priv->backend = g_value_dup_string (value);
766       break;
767
768     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
769       g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
770                  G_STRFUNC, G_OBJECT_TYPE_NAME (object));
771       break;
772     default:
773       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
774       break;
775     }
776 }
777
778 static void
779 gtk_file_chooser_button_get_property (GObject    *object,
780                                       guint       param_id,
781                                       GValue     *value,
782                                       GParamSpec *pspec)
783 {
784   GtkFileChooserButtonPrivate *priv;
785
786   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (object);
787
788   switch (param_id)
789     {
790     case PROP_WIDTH_CHARS:
791       g_value_set_int (value,
792                        gtk_label_get_width_chars (GTK_LABEL (priv->label)));
793       break;
794
795     case PROP_TITLE:
796     case GTK_FILE_CHOOSER_PROP_ACTION:
797     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
798     case GTK_FILE_CHOOSER_PROP_FILTER:
799     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
800     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
801     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
802     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
803     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
804     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
805     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
806       g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
807       break;
808
809     default:
810       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
811       break;
812     }
813 }
814
815 static void
816 gtk_file_chooser_button_finalize (GObject *object)
817 {
818   GtkFileChooserButtonPrivate *priv;
819   GtkTreeIter iter;
820
821   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (object);
822
823   if (priv->old_path)
824     gtk_file_path_free (priv->old_path);
825
826   g_assert (gtk_tree_model_get_iter_first (priv->model, &iter));
827
828   do
829     {
830       model_free_row_data (GTK_FILE_CHOOSER_BUTTON (object), &iter);
831     }
832   while (gtk_tree_model_iter_next (priv->model, &iter));
833
834   g_object_unref (priv->model);
835   g_object_unref (priv->filter_model);
836
837   g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
838   g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
839   g_object_unref (priv->fs);
840
841   if (G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize != NULL)
842     (*G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize) (object);
843 }
844
845 /* ********************* *
846  *  GtkObject Functions  *
847  * ********************* */
848
849 static void
850 gtk_file_chooser_button_destroy (GtkObject *object)
851 {
852   GtkFileChooserButtonPrivate *priv;
853
854   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (object);
855
856   if (priv->dialog != NULL)
857     gtk_widget_destroy (priv->dialog);
858
859   if (GTK_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->destroy != NULL)
860     (*GTK_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->destroy) (object);
861 }
862
863
864 /* ********************* *
865  *  GtkWidget Functions  *
866  * ********************* */
867
868 static void
869 gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
870                                             GdkDragContext   *context,
871                                             gint              x,
872                                             gint              y,
873                                             GtkSelectionData *data,
874                                             guint             info,
875                                             guint             drag_time)
876 {
877   GtkFileChooserButtonPrivate *priv;
878   GtkFilePath *path;
879   gchar *text;
880
881   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
882     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received) (widget,
883                                                                                     context,
884                                                                                     x, y,
885                                                                                     data, info,
886                                                                                     drag_time);
887
888   if (widget == NULL || context == NULL || data == NULL || data->length < 0)
889     return;
890
891   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (widget);
892
893   switch (info)
894     {
895     case TEXT_URI_LIST:
896       {
897         gchar **uris;
898         GtkFilePath *base_path;
899         guint i;
900         gboolean selected;
901
902         uris = gtk_selection_data_get_uris (data);
903         
904         if (uris == NULL)
905           break;
906
907         selected = FALSE;
908         for (i = 0; !selected && uris[i] != NULL; i++)
909           {
910             path = gtk_file_system_uri_to_path (priv->fs, uris[i]);
911
912             base_path = NULL;
913             if (path != NULL &&
914                 gtk_file_system_get_parent (priv->fs, path, &base_path, NULL))
915               {
916                 GtkFileFolder *folder;
917                 GtkFileInfo *info;
918
919                 folder = gtk_file_system_get_folder (priv->fs, base_path,
920                                                      GTK_FILE_INFO_IS_FOLDER,
921                                                      NULL);
922
923                 info = gtk_file_folder_get_info (folder, path, NULL);
924
925                 if (info != NULL)
926                   {
927                     GtkFileChooserAction action;
928
929                     g_object_get (priv->dialog, "action", &action, NULL);
930
931                     selected = 
932                       (((action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
933                          gtk_file_info_get_is_folder (info)) ||
934                         (action == GTK_FILE_CHOOSER_ACTION_OPEN &&
935                          !gtk_file_info_get_is_folder (info))) &&
936                         _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (priv->dialog),
937                                                        path, NULL));
938
939                     gtk_file_info_free (info);
940                   }
941                 else
942                   selected = FALSE;
943
944                 gtk_file_path_free (base_path);
945               }
946
947             gtk_file_path_free (path);
948           }
949
950         g_strfreev (uris);
951       }
952       break;
953
954     case TEXT_PLAIN:
955       text = gtk_selection_data_get_text (data);
956       path = gtk_file_path_new_steal (text);
957       _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (priv->dialog), path,
958                                      NULL);
959       gtk_file_path_free (path);
960       break;
961
962     default:
963       break;
964     }
965
966   gtk_drag_finish (context, TRUE, FALSE, drag_time);
967 }
968
969 static void
970 gtk_file_chooser_button_show_all (GtkWidget *widget)
971 {
972   gtk_widget_show (widget);
973 }
974
975 static void
976 gtk_file_chooser_button_hide_all (GtkWidget *widget)
977 {
978   gtk_widget_hide (widget);
979 }
980
981 static void
982 gtk_file_chooser_button_show (GtkWidget *widget)
983 {
984   GtkFileChooserButtonPrivate *priv;
985
986   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (widget);
987
988   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
989     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show) (widget);
990
991   if (priv->active)
992     open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
993 }
994
995 static void
996 gtk_file_chooser_button_hide (GtkWidget *widget)
997 {
998   GtkFileChooserButtonPrivate *priv;
999
1000   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (widget);
1001
1002   gtk_widget_hide (priv->dialog);
1003
1004   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1005     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide) (widget);
1006 }
1007
1008 static gboolean
1009 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1010                                            gboolean   group_cycling)
1011 {
1012   GtkFileChooserButtonPrivate *priv;
1013
1014   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (widget);
1015   switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1016     {
1017     case GTK_FILE_CHOOSER_ACTION_OPEN:
1018       gtk_widget_grab_focus (priv->button);
1019       break;
1020     case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1021       return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1022       break;
1023     default:
1024       g_assert_not_reached ();
1025       break;
1026     }
1027
1028   return TRUE;
1029 }
1030
1031 /* Changes the icons wherever it is needed */
1032 static void
1033 change_icon_theme (GtkFileChooserButton *button)
1034 {
1035   GtkFileChooserButtonPrivate *priv;
1036   GtkSettings *settings;
1037   GtkIconTheme *theme;
1038   GtkTreeIter iter;
1039   gint width, height;
1040
1041   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (button);
1042
1043   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1044
1045   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_SMALL_TOOLBAR,
1046                                          &width, &height))
1047     priv->icon_size = MAX (width, height);
1048   else
1049     priv->icon_size = FALLBACK_ICON_SIZE;
1050
1051   update_label_and_image (button);
1052
1053   g_assert (gtk_tree_model_get_iter_first (priv->model, &iter));
1054
1055   theme = get_icon_theme (GTK_WIDGET (button));
1056
1057   do
1058     {
1059       GdkPixbuf *pixbuf;
1060       gchar type;
1061       gpointer data;
1062
1063       type = ROW_TYPE_INVALID;
1064       gtk_tree_model_get (priv->model, &iter,
1065                           TYPE_COLUMN, &type,
1066                           DATA_COLUMN, &data,
1067                           -1);
1068
1069       switch (type)
1070         {
1071         case ROW_TYPE_SPECIAL:
1072         case ROW_TYPE_SHORTCUT:
1073         case ROW_TYPE_BOOKMARK:
1074         case ROW_TYPE_CURRENT_FOLDER:
1075           if (data)
1076             pixbuf = gtk_file_system_render_icon (priv->fs, data,
1077                                                   GTK_WIDGET (button),
1078                                                   priv->icon_size, NULL);
1079           else
1080             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1081                                                priv->icon_size, 0, NULL);
1082           break;
1083         case ROW_TYPE_VOLUME:
1084           if (data)
1085             pixbuf = gtk_file_system_volume_render_icon (priv->fs, data,
1086                                                          GTK_WIDGET (button),
1087                                                          priv->icon_size,
1088                                                          NULL);
1089           else
1090             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1091                                                priv->icon_size, 0, NULL);
1092           break;
1093         default:
1094           continue;
1095           break;
1096         }
1097
1098       if (pixbuf)
1099         width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1100
1101       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1102                           ICON_COLUMN, pixbuf,
1103                           -1);
1104     }
1105   while (gtk_tree_model_iter_next (priv->model, &iter));
1106
1107   g_object_set (button->priv->icon_cell,
1108                 "width", width,
1109                 NULL);
1110 }
1111
1112 static void
1113 gtk_file_chooser_button_style_set (GtkWidget *widget,
1114                                    GtkStyle  *old_style)
1115 {
1116   GtkFileChooserButtonPrivate *priv;
1117
1118   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_set)
1119     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_set) (widget,
1120                                                                            old_style);
1121
1122   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (widget);
1123
1124   if (gtk_widget_has_screen (widget))
1125     change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1126 }
1127
1128 static void
1129 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1130                                         GdkScreen *old_screen)
1131 {
1132   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1133     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed) (widget,
1134                                                                                 old_screen);
1135
1136   change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget)); 
1137 }
1138
1139
1140 /* ******************* *
1141  *  Utility Functions  *
1142  * ******************* */
1143
1144 /* General */
1145 static GtkIconTheme *
1146 get_icon_theme (GtkWidget *widget)
1147 {
1148   if (gtk_widget_has_screen (widget))
1149     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1150
1151   return gtk_icon_theme_get_default ();
1152 }
1153
1154 static gchar *
1155 get_display_name_for_path (GtkFileSystem     *fs,
1156                            const GtkFilePath *path)
1157 {
1158   GtkFilePath *parent_path;
1159   GtkFileFolder *folder;
1160   gchar *retval;
1161
1162   parent_path = NULL;
1163   retval = NULL;
1164
1165   gtk_file_system_get_parent (fs, path, &parent_path, NULL);
1166
1167   folder = gtk_file_system_get_folder (fs, parent_path ? parent_path : path,
1168                                        GTK_FILE_INFO_DISPLAY_NAME, NULL);
1169
1170   if (folder)
1171     {
1172       GtkFileInfo *info;
1173
1174       info = gtk_file_folder_get_info (folder, path, NULL);
1175       g_object_unref (folder);
1176
1177       if (info)
1178         {
1179           retval = g_strdup (gtk_file_info_get_display_name (info));
1180           gtk_file_info_free (info);
1181         }
1182     }
1183
1184   if (parent_path)
1185     gtk_file_path_free (parent_path);
1186
1187   if (!retval)
1188     retval = g_strdup (_(FALLBACK_DISPLAY_NAME));
1189
1190   return retval;
1191 }
1192
1193 /* Shortcuts Model */
1194 static gint
1195 model_get_type_position (GtkFileChooserButton *button,
1196                          RowType               row_type)
1197 {
1198   gint retval = 0;
1199
1200   if (row_type == ROW_TYPE_SPECIAL)
1201     return retval;
1202
1203   retval += button->priv->n_special;
1204
1205   if (row_type == ROW_TYPE_VOLUME)
1206     return retval;
1207
1208   retval += button->priv->n_volumes;
1209
1210   if (row_type == ROW_TYPE_SHORTCUT)
1211     return retval;
1212
1213   retval += button->priv->n_shortcuts;
1214
1215   if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1216     return retval;
1217
1218   retval += button->priv->has_bookmark_separator;
1219
1220   if (row_type == ROW_TYPE_BOOKMARK)
1221     return retval;
1222
1223   retval += button->priv->n_bookmarks;
1224
1225   if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1226     return retval;
1227
1228   retval += button->priv->has_current_folder_separator;
1229
1230   if (row_type == ROW_TYPE_CURRENT_FOLDER)
1231     return retval;
1232
1233   retval += button->priv->has_current_folder;
1234
1235   if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1236     return retval;
1237
1238   retval += button->priv->has_other_separator;
1239
1240   if (row_type == ROW_TYPE_OTHER)
1241     return retval;
1242
1243   g_assert_not_reached ();
1244   return -1;
1245 }
1246
1247 static void
1248 model_free_row_data (GtkFileChooserButton *button,
1249                      GtkTreeIter          *iter)
1250 {
1251   gchar type;
1252   gpointer data;
1253
1254   gtk_tree_model_get (button->priv->model, iter,
1255                       TYPE_COLUMN, &type,
1256                       DATA_COLUMN, &data,
1257                       -1);
1258
1259   switch (type)
1260     {
1261     case ROW_TYPE_SPECIAL:
1262     case ROW_TYPE_SHORTCUT:
1263     case ROW_TYPE_BOOKMARK:
1264     case ROW_TYPE_CURRENT_FOLDER:
1265       gtk_file_path_free (data);
1266       break;
1267     case ROW_TYPE_VOLUME:
1268       gtk_file_system_volume_free (button->priv->fs, data);
1269       break;
1270     default:
1271       break;
1272     }
1273 }
1274
1275 static inline void
1276 model_add_special (GtkFileChooserButton *button)
1277 {
1278   const gchar *homedir;
1279   gchar *desktopdir = NULL;
1280   GtkListStore *store;
1281   GtkTreeIter iter;
1282   GtkFilePath *path;
1283   GdkPixbuf *pixbuf;
1284   gint pos;
1285
1286   store = GTK_LIST_STORE (button->priv->model);
1287   pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1288
1289   homedir = g_get_home_dir ();
1290
1291   if (homedir)
1292     {
1293       path = gtk_file_system_filename_to_path (button->priv->fs, homedir);
1294       pixbuf = gtk_file_system_render_icon (button->priv->fs, path,
1295                                             GTK_WIDGET (button),
1296                                             button->priv->icon_size, NULL);
1297       gtk_list_store_insert (store, &iter, pos);
1298       pos++;
1299       gtk_list_store_set (store, &iter,
1300                           ICON_COLUMN, pixbuf,
1301                           DISPLAY_NAME_COLUMN, _(HOME_DISPLAY_NAME),
1302                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1303                           DATA_COLUMN, path,
1304                           -1);
1305
1306       g_object_unref (pixbuf);
1307       button->priv->n_special++;
1308
1309 #ifndef G_OS_WIN32
1310       desktopdir = g_build_filename (homedir, DESKTOP_DISPLAY_NAME, NULL);
1311 #endif
1312     }
1313
1314 #ifdef G_OS_WIN32
1315   desktopdir = _gtk_file_system_win32_get_desktop ();
1316 #endif
1317
1318   if (desktopdir)
1319     {
1320       path = gtk_file_system_filename_to_path (button->priv->fs, desktopdir);
1321       g_free (desktopdir);
1322       pixbuf = gtk_file_system_render_icon (button->priv->fs, path,
1323                                             GTK_WIDGET (button),
1324                                             button->priv->icon_size, NULL);
1325       gtk_list_store_insert (store, &iter, pos);
1326       pos++;
1327       gtk_list_store_set (store, &iter,
1328                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1329                           ICON_COLUMN, pixbuf,
1330                           DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1331                           DATA_COLUMN, path,
1332                           -1);
1333
1334       g_object_unref (pixbuf);
1335       button->priv->n_special++;
1336     }
1337 }
1338
1339 static void
1340 model_add_volumes (GtkFileChooserButton *button,
1341                    GSList               *volumes)
1342 {
1343   GtkListStore *store;
1344   gint pos;
1345
1346   if (!volumes)
1347     return;
1348
1349   store = GTK_LIST_STORE (button->priv->model);
1350   pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1351
1352   do
1353     {
1354       GtkTreeIter iter;
1355       GdkPixbuf *pixbuf;
1356       gchar *display_name;
1357
1358       pixbuf = gtk_file_system_volume_render_icon (button->priv->fs,
1359                                                    volumes->data,
1360                                                    GTK_WIDGET (button),
1361                                                    button->priv->icon_size,
1362                                                    NULL);
1363       display_name = gtk_file_system_volume_get_display_name (button->priv->fs,
1364                                                               volumes->data);
1365
1366       gtk_list_store_insert (store, &iter, pos);
1367       gtk_list_store_set (store, &iter,
1368                           ICON_COLUMN, pixbuf,
1369                           DISPLAY_NAME_COLUMN, display_name,
1370                           TYPE_COLUMN, ROW_TYPE_VOLUME,
1371                           DATA_COLUMN, volumes->data,
1372                           -1);
1373
1374       g_object_unref (pixbuf);
1375       g_free (display_name);
1376
1377       button->priv->n_volumes++;
1378       pos++;
1379       volumes = volumes->next;
1380     }
1381   while (volumes);
1382 }
1383
1384 static void
1385 model_add_bookmarks (GtkFileChooserButton *button,
1386                      GSList               *bookmarks)
1387 {
1388   GtkListStore *store;
1389   GtkTreeIter iter;
1390   gint pos;
1391
1392   if (!bookmarks)
1393     return;
1394
1395   store = GTK_LIST_STORE (button->priv->model);
1396   pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
1397
1398   if (!button->priv->has_bookmark_separator)
1399     {
1400       gtk_list_store_insert (store, &iter, pos);
1401       gtk_list_store_set (store, &iter,
1402                           ICON_COLUMN, NULL,
1403                           DISPLAY_NAME_COLUMN, NULL,
1404                           TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
1405                           DATA_COLUMN, NULL,
1406                           -1);
1407       button->priv->has_bookmark_separator = TRUE;
1408     }
1409
1410   do
1411     {
1412       GdkPixbuf *pixbuf;
1413       gchar *display_name;
1414
1415       pos++;
1416       pixbuf = gtk_file_system_render_icon (button->priv->fs, bookmarks->data,
1417                                             GTK_WIDGET (button),
1418                                             button->priv->icon_size, NULL);
1419       display_name = get_display_name_for_path (button->priv->fs,
1420                                                 bookmarks->data);
1421
1422       gtk_list_store_insert (store, &iter, pos);
1423       gtk_list_store_set (store, &iter,
1424                           ICON_COLUMN, pixbuf,
1425                           DISPLAY_NAME_COLUMN, display_name,
1426                           TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1427                           DATA_COLUMN, gtk_file_path_copy (bookmarks->data),
1428                           -1);
1429       if (pixbuf)
1430         g_object_unref (pixbuf);
1431       g_free (display_name);
1432
1433       button->priv->n_bookmarks++;
1434       bookmarks = bookmarks->next;
1435     }
1436   while (bookmarks);
1437 }
1438
1439 static void
1440 model_update_current_folder (GtkFileChooserButton *button,
1441                              const GtkFilePath    *path)
1442 {
1443   GtkListStore *store;
1444   GtkTreeIter iter;
1445   gint pos;
1446   GdkPixbuf *pixbuf;
1447   gchar *display_name;
1448
1449   if (!path) 
1450     return;
1451
1452   store = GTK_LIST_STORE (button->priv->model);
1453
1454   if (!button->priv->has_current_folder_separator)
1455     {
1456       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
1457       gtk_list_store_insert (store, &iter, pos);
1458       gtk_list_store_set (store, &iter,
1459                           ICON_COLUMN, NULL,
1460                           DISPLAY_NAME_COLUMN, NULL,
1461                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
1462                           DATA_COLUMN, NULL,
1463                           -1);
1464       button->priv->has_current_folder_separator = TRUE;
1465     }
1466
1467   pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
1468   if (!button->priv->has_current_folder)
1469     {
1470       gtk_list_store_insert (store, &iter, pos);
1471       button->priv->has_current_folder = TRUE;
1472     }
1473   else
1474     gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
1475
1476   pixbuf = gtk_file_system_render_icon (button->priv->fs, path,
1477                                         GTK_WIDGET (button),
1478                                         button->priv->icon_size, NULL);
1479   display_name = get_display_name_for_path (button->priv->fs, path);
1480   gtk_list_store_set (store, &iter,
1481                       ICON_COLUMN, pixbuf,
1482                       DISPLAY_NAME_COLUMN, display_name,
1483                       TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
1484                       DATA_COLUMN, gtk_file_path_copy (path),
1485                       -1);
1486   if (pixbuf)
1487     g_object_unref (pixbuf);
1488   g_free (display_name);
1489 }
1490
1491 static inline void
1492 model_add_other (GtkFileChooserButton *button)
1493 {
1494   GtkListStore *store;
1495   GtkTreeIter iter;
1496   gint pos;
1497   
1498   store = GTK_LIST_STORE (button->priv->model);
1499   pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
1500
1501   gtk_list_store_insert (store, &iter, pos);
1502   gtk_list_store_set (store, &iter,
1503                       ICON_COLUMN, NULL,
1504                       DISPLAY_NAME_COLUMN, NULL,
1505                       TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
1506                       DATA_COLUMN, NULL,
1507                       -1);
1508   button->priv->has_other_separator = TRUE;
1509   pos++;
1510
1511   gtk_list_store_insert (store, &iter, pos);
1512   gtk_list_store_set (store, &iter,
1513                       ICON_COLUMN, NULL,
1514                       DISPLAY_NAME_COLUMN, _("Other..."),
1515                       TYPE_COLUMN, ROW_TYPE_OTHER,
1516                       DATA_COLUMN, NULL,
1517                       -1);
1518 }
1519
1520 static void
1521 model_remove_rows (GtkFileChooserButton *button,
1522                    gint                  pos,
1523                    gint                  n_rows)
1524 {
1525   GtkListStore *store;
1526
1527   if (!n_rows)
1528     return;
1529
1530   store = GTK_LIST_STORE (button->priv->model);
1531
1532   do
1533     {
1534       GtkTreeIter iter;
1535
1536       if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
1537         g_assert_not_reached ();
1538
1539       model_free_row_data (button, &iter);
1540       gtk_list_store_remove (store, &iter);
1541       n_rows--;
1542     }
1543   while (n_rows);
1544 }
1545
1546 /* Filter Model */
1547 static inline gboolean
1548 test_if_path_is_visible (GtkFileSystem     *fs,
1549                          const GtkFilePath *path,
1550                          gboolean           local_only)
1551 {
1552   GtkFilePath *parent_path;
1553   GtkFileFolder *folder;
1554   GtkFileInfo *info;
1555
1556   if (!path)
1557     return FALSE;
1558
1559   if (local_only && !gtk_file_system_path_is_local (fs, path))
1560     return FALSE;
1561
1562   parent_path = NULL;
1563   gtk_file_system_get_parent (fs, path, &parent_path, NULL);
1564
1565   folder = gtk_file_system_get_folder (fs, parent_path ? parent_path : path,
1566                                        GTK_FILE_INFO_IS_FOLDER, NULL);
1567   gtk_file_path_free (parent_path);
1568
1569   if (folder)
1570     {
1571       info = gtk_file_folder_get_info (folder, path, NULL);
1572       g_object_unref (folder);
1573     }
1574   else
1575     info = NULL;
1576
1577   if (!info)
1578     return FALSE;
1579   else if (!gtk_file_info_get_is_folder (info))
1580     {
1581       gtk_file_info_free (info);
1582       return FALSE;
1583     }
1584
1585   gtk_file_info_free (info);
1586
1587   return TRUE;
1588 }
1589
1590 static gboolean
1591 filter_model_visible_func (GtkTreeModel *model,
1592                            GtkTreeIter  *iter,
1593                            gpointer      user_data)
1594 {
1595   GtkFileChooserButtonPrivate *priv;
1596   gchar type;
1597   gpointer data;
1598   gboolean local_only, retval;
1599
1600   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (user_data);
1601   type = ROW_TYPE_INVALID;
1602   data = NULL;
1603   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
1604
1605   gtk_tree_model_get (model, iter,
1606                       TYPE_COLUMN, &type,
1607                       DATA_COLUMN, &data,
1608                       -1);
1609
1610   switch (type)
1611     {
1612     case ROW_TYPE_CURRENT_FOLDER:
1613       retval = TRUE;
1614       break;
1615     case ROW_TYPE_SPECIAL:
1616     case ROW_TYPE_SHORTCUT:
1617     case ROW_TYPE_BOOKMARK:
1618       retval = test_if_path_is_visible (priv->fs, data, local_only);
1619       break;
1620     case ROW_TYPE_VOLUME:
1621       {
1622         GtkFilePath *base_path;
1623
1624         base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
1625         if (base_path)
1626           {
1627             retval = (!local_only ||
1628                       gtk_file_system_path_is_local (priv->fs, base_path));
1629             gtk_file_path_free (base_path);
1630           }
1631         else
1632           retval = FALSE;
1633       }
1634       break;
1635     default:
1636       retval = TRUE;
1637       break;
1638     }
1639
1640   return retval;
1641 }
1642
1643 /* Combo Box */
1644 static void
1645 name_cell_data_func (GtkCellLayout   *layout,
1646                      GtkCellRenderer *cell,
1647                      GtkTreeModel    *model,
1648                      GtkTreeIter     *iter,
1649                      gpointer         user_data)
1650 {
1651   gchar type;
1652
1653   type = 0;
1654   gtk_tree_model_get (model, iter,
1655                       TYPE_COLUMN, &type,
1656                       -1);
1657
1658   if (type == ROW_TYPE_CURRENT_FOLDER)
1659     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1660   else
1661     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
1662 }
1663
1664 static gboolean
1665 combo_box_row_separator_func (GtkTreeModel *model,
1666                               GtkTreeIter  *iter,
1667                               gpointer      user_data)
1668 {
1669   gchar type = ROW_TYPE_INVALID;
1670
1671   gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
1672
1673   return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
1674           type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
1675           type == ROW_TYPE_OTHER_SEPARATOR);
1676 }                         
1677
1678 static void
1679 update_combo_box (GtkFileChooserButton *button)
1680 {
1681   GtkFileChooserButtonPrivate *priv;
1682   GSList *paths;
1683   GtkTreeIter iter;
1684   gboolean row_found;
1685
1686   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (button);
1687
1688   g_assert (gtk_tree_model_get_iter_first (priv->filter_model, &iter));
1689
1690   _gtk_file_chooser_get_current_folder_path (GTK_FILE_CHOOSER (priv->dialog));
1691   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
1692
1693   row_found = FALSE;
1694
1695   do
1696     {
1697       gchar type;
1698       gpointer data;
1699
1700       type = ROW_TYPE_INVALID;
1701       data = NULL;
1702
1703       gtk_tree_model_get (priv->filter_model, &iter,
1704                           TYPE_COLUMN, &type,
1705                           DATA_COLUMN, &data,
1706                           -1);
1707     
1708       switch (type)
1709         {
1710         case ROW_TYPE_SPECIAL:
1711         case ROW_TYPE_SHORTCUT:
1712         case ROW_TYPE_BOOKMARK:
1713         case ROW_TYPE_CURRENT_FOLDER:
1714           row_found = (paths &&
1715                        paths->data &&
1716                        gtk_file_path_compare (data, paths->data) == 0);
1717           break;
1718         case ROW_TYPE_VOLUME:
1719           {
1720             GtkFilePath *base_path;
1721
1722             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
1723             row_found = (paths &&
1724                          paths->data &&
1725                          gtk_file_path_compare (base_path, paths->data) == 0);
1726             gtk_file_path_free (base_path);
1727           }
1728           break;
1729         default:
1730           row_found = FALSE;
1731           break;
1732         }
1733
1734       if (row_found)
1735         {
1736           g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
1737           gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
1738                                          &iter);
1739           g_signal_handler_unblock (priv->combo_box,
1740                                     priv->combo_box_changed_id);
1741         }
1742     }
1743   while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
1744
1745   /* If it hasn't been found already, update & select the current-folder row. */
1746   if (!row_found && paths && paths->data)
1747     {
1748       GtkTreeIter filter_iter;
1749       gint pos;
1750     
1751       model_update_current_folder (button, paths->data);
1752       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
1753
1754       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
1755       g_assert (gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos));
1756
1757       gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
1758                                                         &filter_iter, &iter);
1759
1760       g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
1761       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
1762       g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
1763     }
1764
1765   gtk_file_paths_free (paths);
1766 }
1767
1768 /* Button */
1769 static void
1770 update_label_and_image (GtkFileChooserButton *button)
1771 {
1772   GtkFileChooserButtonPrivate *priv;
1773   GdkPixbuf *pixbuf;
1774   gchar *label_text;
1775   GSList *paths;
1776
1777   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (button);
1778   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (button->priv->dialog));
1779   label_text = NULL;
1780   pixbuf = NULL;
1781
1782   if (paths)
1783     {
1784       GtkFilePath *path, *parent_path;
1785       GtkFileSystemVolume *volume;
1786       GtkFileFolder *folder;
1787     
1788       path = paths->data;
1789
1790       volume = gtk_file_system_get_volume_for_path (priv->fs, path);
1791       if (volume)
1792         {
1793           GtkFilePath *base_path;
1794
1795           base_path = gtk_file_system_volume_get_base_path (priv->fs, volume);
1796           if (base_path && gtk_file_path_compare (base_path, path) == 0)
1797             {
1798               label_text = gtk_file_system_volume_get_display_name (priv->fs,
1799                                                                     volume);
1800               pixbuf = gtk_file_system_volume_render_icon (priv->fs, volume,
1801                                                            GTK_WIDGET (button),
1802                                                            priv->icon_size,
1803                                                            NULL);
1804             }
1805
1806           if (base_path)
1807             gtk_file_path_free (base_path);
1808
1809           gtk_file_system_volume_free (priv->fs, volume);
1810
1811           if (label_text)
1812             goto out;
1813         }
1814
1815       if (!pixbuf)
1816         pixbuf = gtk_file_system_render_icon (priv->fs, path,
1817                                               GTK_WIDGET (button),
1818                                               priv->icon_size, NULL);
1819
1820       parent_path = NULL;
1821       gtk_file_system_get_parent (priv->fs, path, &parent_path, NULL);
1822
1823       folder = gtk_file_system_get_folder (priv->fs,
1824                                            parent_path ? parent_path : path,
1825                                            GTK_FILE_INFO_DISPLAY_NAME, NULL);
1826       if (folder)
1827         {
1828           GtkFileInfo *info;
1829
1830           info = gtk_file_folder_get_info (folder, path, NULL);
1831           g_object_unref (folder);
1832
1833           if (info)
1834             {
1835               label_text = g_strdup (gtk_file_info_get_display_name (info));
1836               gtk_file_info_free (info);
1837             }
1838         }
1839
1840      out:
1841       gtk_file_paths_free (paths);
1842     }
1843
1844   if (label_text)
1845     {
1846       gtk_label_set_text (GTK_LABEL (priv->label), label_text);
1847       g_free (label_text);
1848     }
1849   else
1850     gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
1851   
1852   if (!pixbuf)
1853     pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (button)),
1854                                        FALLBACK_ICON_NAME,
1855                                        priv->icon_size, 0, NULL);
1856
1857   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
1858   if (pixbuf)
1859     g_object_unref (pixbuf);
1860 }
1861
1862
1863 /* ************************ *
1864  *  Child Object Callbacks  *
1865  * ************************ */
1866
1867 /* File System */
1868 static void
1869 fs_volumes_changed_cb (GtkFileSystem *fs,
1870                        gpointer       user_data)
1871 {
1872   GtkFileChooserButtonPrivate *priv;
1873   GSList *volumes;
1874
1875   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (user_data);
1876   
1877   model_remove_rows (user_data,
1878                      model_get_type_position (user_data, ROW_TYPE_VOLUME),
1879                      priv->n_volumes);
1880
1881   priv->n_volumes = 0;
1882
1883   volumes = gtk_file_system_list_volumes (fs);
1884   model_add_volumes (user_data, volumes);
1885   g_slist_free (volumes);
1886
1887   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
1888
1889   update_label_and_image (user_data);
1890   update_combo_box (user_data);
1891 }
1892
1893 static void
1894 fs_bookmarks_changed_cb (GtkFileSystem *fs,
1895                          gpointer       user_data)
1896 {
1897   GtkFileChooserButtonPrivate *priv;
1898   GSList *bookmarks;
1899
1900   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (user_data);
1901
1902   bookmarks = gtk_file_system_list_bookmarks (fs);
1903   if (!bookmarks)
1904     {
1905       model_remove_rows (user_data,
1906                          model_get_type_position (user_data,
1907                                                   ROW_TYPE_BOOKMARK_SEPARATOR),
1908                          (priv->n_bookmarks + priv->has_bookmark_separator));
1909       priv->has_bookmark_separator = FALSE;
1910     }
1911   else
1912     model_remove_rows (user_data,
1913                        model_get_type_position (user_data, ROW_TYPE_BOOKMARK),
1914                        priv->n_bookmarks);
1915
1916   priv->n_bookmarks = 0;
1917   model_add_bookmarks (user_data, bookmarks);
1918   gtk_file_paths_free (bookmarks);
1919
1920   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
1921
1922   update_label_and_image (user_data);
1923   update_combo_box (user_data);
1924 }
1925
1926 /* Dialog */
1927 static void
1928 open_dialog (GtkFileChooserButton *button)
1929 {
1930   GtkFileChooserButtonPrivate *priv;
1931
1932   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (button);
1933
1934   /* Setup the dialog parent to be chooser button's toplevel, and be modal
1935      as needed. */
1936   if (!GTK_WIDGET_VISIBLE (priv->dialog))
1937     {
1938       GtkWidget *toplevel;
1939
1940       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
1941
1942       if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
1943         {
1944           if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
1945             gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
1946                                           GTK_WINDOW (toplevel));
1947               
1948           gtk_window_set_modal (GTK_WINDOW (priv->dialog),
1949                                 gtk_window_get_modal (GTK_WINDOW (toplevel)));
1950         }
1951     }
1952
1953   if (!priv->active)
1954     {
1955       GSList *paths;
1956
1957       g_signal_handler_block (priv->dialog,
1958                               priv->dialog_folder_changed_id);
1959       g_signal_handler_block (priv->dialog,
1960                               priv->dialog_file_activated_id);
1961       g_signal_handler_block (priv->dialog,
1962                               priv->dialog_selection_changed_id);
1963       paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
1964       if (paths)
1965         {
1966           if (paths->data)
1967             priv->old_path = gtk_file_path_copy (paths->data);
1968
1969           gtk_file_paths_free (paths);
1970         }
1971
1972       priv->active = TRUE;
1973     }
1974
1975   gtk_widget_set_sensitive (priv->combo_box, FALSE);
1976   gtk_window_present (GTK_WINDOW (priv->dialog));
1977 }
1978
1979 /* Combo Box */
1980 static void
1981 combo_box_changed_cb (GtkComboBox *combo_box,
1982                       gpointer     user_data)
1983 {
1984   GtkTreeIter iter;
1985
1986   if (gtk_combo_box_get_active_iter (combo_box, &iter))
1987     {
1988       GtkFileChooserButtonPrivate *priv;
1989       gchar type;
1990       gpointer data;
1991
1992       priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (user_data);
1993
1994       type = ROW_TYPE_INVALID;
1995       data = NULL;
1996
1997       gtk_tree_model_get (priv->filter_model, &iter,
1998                           TYPE_COLUMN, &type,
1999                           DATA_COLUMN, &data,
2000                           -1);
2001
2002       switch (type)
2003         {
2004         case ROW_TYPE_SPECIAL:
2005         case ROW_TYPE_SHORTCUT:
2006         case ROW_TYPE_BOOKMARK:
2007         case ROW_TYPE_CURRENT_FOLDER:
2008           gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2009           if (data)
2010             _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2011                                                        data, NULL);
2012           break;
2013         case ROW_TYPE_VOLUME:
2014           {
2015             GtkFilePath *base_path;
2016
2017             gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2018             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2019             if (base_path)
2020               {
2021                 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2022                                                            base_path, NULL);
2023                 gtk_file_path_free (base_path);
2024               }
2025           }
2026           break;
2027         case ROW_TYPE_OTHER:
2028           open_dialog (user_data);
2029           break;
2030         default:
2031           break;
2032         }
2033     }
2034 }
2035
2036 /* Button */
2037 static void
2038 button_clicked_cb (GtkButton *real_button,
2039                    gpointer   user_data)
2040 {
2041   open_dialog (user_data);
2042 }
2043
2044 /* Dialog */
2045 static void
2046 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2047                                   gpointer        user_data)
2048 {
2049   g_signal_emit_by_name (user_data, "current-folder-changed");
2050 }
2051
2052 static void
2053 dialog_file_activated_cb (GtkFileChooser *dialog,
2054                           gpointer        user_data)
2055 {
2056   g_signal_emit_by_name (user_data, "file-activated");
2057 }
2058
2059 static void
2060 dialog_selection_changed_cb (GtkFileChooser *dialog,
2061                              gpointer        user_data)
2062 {
2063   update_label_and_image (user_data);
2064   update_combo_box (user_data);
2065   g_signal_emit_by_name (user_data, "selection-changed");
2066 }
2067
2068 static void
2069 dialog_update_preview_cb (GtkFileChooser *dialog,
2070                           gpointer        user_data)
2071 {
2072   g_signal_emit_by_name (user_data, "update-preview");
2073 }
2074
2075 static void
2076 dialog_notify_cb (GObject    *dialog,
2077                   GParamSpec *pspec,
2078                   gpointer    user_data)
2079 {
2080   gpointer iface;
2081
2082   iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2083                                  GTK_TYPE_FILE_CHOOSER);
2084   if (g_object_interface_find_property (iface, pspec->name))
2085     g_object_notify (user_data, pspec->name);
2086
2087   if (g_ascii_strcasecmp (pspec->name, "local-only") == 0 ||
2088       g_ascii_strcasecmp (pspec->name, "local_only") == 0)
2089     {
2090       GtkFileChooserButtonPrivate *priv;
2091
2092       priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (user_data);
2093
2094       if (priv->has_current_folder)
2095         {
2096           GtkTreeIter iter;
2097           gint pos;
2098           gpointer data;
2099
2100           pos = model_get_type_position (user_data,
2101                                          ROW_TYPE_CURRENT_FOLDER);
2102           g_assert (gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos));
2103
2104           data = NULL;
2105           gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2106
2107           /* If the path isn't local but we're in local-only mode now, remove
2108            * the custom-folder row */
2109           if (data &&
2110               (!gtk_file_system_path_is_local (priv->fs, data) &&
2111                gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog))))
2112             {
2113               pos--;
2114               model_remove_rows (user_data, pos, 2);
2115             }
2116         }
2117
2118       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2119       update_combo_box (user_data);
2120     }
2121 }
2122
2123 static gboolean
2124 dialog_delete_event_cb (GtkWidget *dialog,
2125                         GdkEvent  *event,
2126                         gpointer   user_data)
2127 {
2128   g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2129
2130   return TRUE;
2131 }
2132
2133 static void
2134 dialog_response_cb (GtkDialog *dialog,
2135                     gint       response,
2136                     gpointer   user_data)
2137 {
2138   GtkFileChooserButtonPrivate *priv;
2139
2140   priv = GTK_FILE_CHOOSER_BUTTON_GET_PRIVATE (user_data);
2141
2142   if (response == GTK_RESPONSE_ACCEPT)
2143     {
2144       g_signal_emit_by_name (user_data, "current-folder-changed");
2145       g_signal_emit_by_name (user_data, "selection-changed");
2146     }
2147   else if (priv->old_path)
2148     {
2149       switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)))
2150         {
2151         case GTK_FILE_CHOOSER_ACTION_OPEN:
2152           _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (dialog), priv->old_path,
2153                                          NULL);
2154           break;
2155         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
2156           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (dialog),
2157                                                      priv->old_path, NULL);
2158           break;
2159         default:
2160           g_assert_not_reached ();
2161           break;
2162         }
2163     }
2164
2165   if (priv->old_path)
2166     {
2167       gtk_file_path_free (priv->old_path);
2168       priv->old_path = NULL;
2169     }
2170
2171   update_label_and_image (user_data);
2172   update_combo_box (user_data);
2173   
2174   if (priv->active)
2175     {
2176       g_signal_handler_unblock (priv->dialog,
2177                                 priv->dialog_folder_changed_id);
2178       g_signal_handler_unblock (priv->dialog,
2179                                 priv->dialog_file_activated_id);
2180       g_signal_handler_unblock (priv->dialog,
2181                                 priv->dialog_selection_changed_id);
2182       priv->active = FALSE;
2183     }
2184
2185   gtk_widget_set_sensitive (priv->combo_box, TRUE);
2186   gtk_widget_hide (priv->dialog);
2187 }
2188
2189
2190 /* ************************************************************************** *
2191  *  Public API                                                                *
2192  * ************************************************************************** */
2193
2194 /**
2195  * gtk_file_chooser_button_new:
2196  * @title: the title of the browse dialog.
2197  * @action: the open mode for the widget.
2198  * 
2199  * Creates a new file-selecting button widget.
2200  * 
2201  * Returns: a new button widget.
2202  * 
2203  * Since: 2.6
2204  **/
2205 GtkWidget *
2206 gtk_file_chooser_button_new (const gchar          *title,
2207                              GtkFileChooserAction  action)
2208 {
2209   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2210                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2211
2212   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2213                        "action", action,
2214                        "title", title,
2215                        NULL);
2216 }
2217
2218 /**
2219  * gtk_file_chooser_button_new_with_backend:
2220  * @title: the title of the browse dialog.
2221  * @action: the open mode for the widget.
2222  * @backend: the name of the #GtkFileSystem backend to use.
2223  * 
2224  * Creates a new file-selecting button widget using @backend.
2225  * 
2226  * Returns: a new button widget.
2227  * 
2228  * Since: 2.6
2229  **/
2230 GtkWidget *
2231 gtk_file_chooser_button_new_with_backend (const gchar          *title,
2232                                           GtkFileChooserAction  action,
2233                                           const gchar          *backend)
2234 {
2235   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2236                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2237
2238   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2239                        "title", title,
2240                        "action", action,
2241                        "file-system-backend", backend,
2242                        NULL);
2243 }
2244
2245 /**
2246  * gtk_file_chooser_button_new_with_dialog:
2247  * @dialog: the #GtkDialog widget to use.
2248  * 
2249  * Creates a #GtkFileChooserButton widget which uses @dialog as it's
2250  * file-picking window. Note that @dialog must be a #GtkFileChooserDialog (or
2251  * subclass).
2252  * 
2253  * Returns: a new button widget.
2254  * 
2255  * Since: 2.6
2256  **/
2257 GtkWidget *
2258 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2259 {
2260   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_DIALOG (dialog), NULL);
2261
2262   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2263                        "dialog", dialog,
2264                        NULL);
2265 }
2266
2267 /**
2268  * gtk_file_chooser_button_set_title:
2269  * @button: the button widget to modify.
2270  * @title: the new browse dialog title.
2271  * 
2272  * Modifies the @title of the browse dialog used by @button.
2273  * 
2274  * Since: 2.6
2275  **/
2276 void
2277 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2278                                    const gchar          *title)
2279 {
2280   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2281
2282   gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2283   g_object_notify (G_OBJECT (button), "title");
2284 }
2285
2286 /**
2287  * gtk_file_chooser_button_get_title:
2288  * @button: the button widget to examine.
2289  * 
2290  * Retrieves the title of the browse dialog used by @button. The returned value
2291  * should not be modified or freed.
2292  * 
2293  * Returns: a pointer to the browse dialog's title.
2294  * 
2295  * Since: 2.6
2296  **/
2297 G_CONST_RETURN gchar *
2298 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2299 {
2300   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2301
2302   return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2303 }
2304
2305 /**
2306  * gtk_file_chooser_button_get_width_chars:
2307  * @button: the button widget to examine.
2308  * 
2309  * Retrieves the width in characters of the @button widget's entry and/or label.
2310  * 
2311  * Returns: an integer width (in characters) that the button will use to size itself.
2312  * 
2313  * Since: 2.6
2314  **/
2315 gint
2316 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2317 {
2318   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2319
2320   return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2321 }
2322
2323 /**
2324  * gtk_file_chooser_button_set_width_chars:
2325  * @button: the button widget to examine.
2326  * @n_chars: the new width, in chracters.
2327  * 
2328  * Sets the width (in characters) that @button will use to @n_chars.
2329  * 
2330  * Since: 2.6
2331  **/
2332 void
2333 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
2334                                          gint                  n_chars)
2335 {
2336   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2337
2338   gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
2339   g_object_notify (G_OBJECT (button), "width-chars");
2340 }