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