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