]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserbutton.c
b99274f0953aaf00c29f5c2a19e813b50302a7bc
[~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   char *label;
1390   GtkTreeRowReference *row_ref;
1391 };
1392
1393 static void
1394 set_info_get_info_cb (GtkFileSystemHandle *handle,
1395                       const GtkFileInfo   *info,
1396                       const GError        *error,
1397                       gpointer             callback_data)
1398 {
1399   gboolean cancelled = handle->cancelled;
1400   GdkPixbuf *pixbuf;
1401   GtkTreePath *path;
1402   GtkTreeIter iter;
1403   GtkFileSystemHandle *model_handle;
1404   struct SetDisplayNameData *data = callback_data;
1405
1406   if (!data->button->priv->model)
1407     /* button got destroyed */
1408     goto out;
1409
1410   path = gtk_tree_row_reference_get_path (data->row_ref);
1411   if (!path)
1412     /* Handle doesn't exist anymore in the model */
1413     goto out;
1414
1415   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1416   gtk_tree_path_free (path);
1417
1418   /* Validate the handle */
1419   gtk_tree_model_get (data->button->priv->model, &iter,
1420                       HANDLE_COLUMN, &model_handle,
1421                       -1);
1422   if (handle != model_handle)
1423     goto out;
1424
1425   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1426                       HANDLE_COLUMN, NULL,
1427                       -1);
1428
1429   if (cancelled || error)
1430     /* There was an error, leave the fallback name in there */
1431     goto out;
1432
1433   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1434                                       data->button->priv->icon_size, NULL);
1435
1436   if (!data->label)
1437     data->label = g_strdup (gtk_file_info_get_display_name (info));
1438
1439   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1440                       ICON_COLUMN, pixbuf,
1441                       DISPLAY_NAME_COLUMN, data->label,
1442                       IS_FOLDER_COLUMN, gtk_file_info_get_is_folder (info),
1443                       -1);
1444
1445   if (pixbuf)
1446     g_object_unref (pixbuf);
1447
1448 out:
1449   g_object_unref (data->button);
1450   g_free (data->label);
1451   gtk_tree_row_reference_free (data->row_ref);
1452   g_free (data);
1453
1454   g_object_unref (handle);
1455 }
1456
1457 static void
1458 set_info_for_path_at_iter (GtkFileChooserButton *button,
1459                            const GtkFilePath    *path,
1460                            GtkTreeIter          *iter)
1461 {
1462   struct SetDisplayNameData *data;
1463   GtkTreePath *tree_path;
1464   GtkFileSystemHandle *handle;
1465
1466   data = g_new0 (struct SetDisplayNameData, 1);
1467   data->button = g_object_ref (button);
1468
1469   data->label = gtk_file_system_get_bookmark_label (button->priv->fs, path);
1470
1471   tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1472   data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1473   gtk_tree_path_free (tree_path);
1474
1475   handle = gtk_file_system_get_info (button->priv->fs, path,
1476                                      GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_ICON,
1477                                      set_info_get_info_cb, data);
1478
1479   gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1480                       HANDLE_COLUMN, handle,
1481                       -1);
1482 }
1483
1484 /* Shortcuts Model */
1485 static gint
1486 model_get_type_position (GtkFileChooserButton *button,
1487                          RowType               row_type)
1488 {
1489   gint retval = 0;
1490
1491   if (row_type == ROW_TYPE_SPECIAL)
1492     return retval;
1493
1494   retval += button->priv->n_special;
1495
1496   if (row_type == ROW_TYPE_VOLUME)
1497     return retval;
1498
1499   retval += button->priv->n_volumes;
1500
1501   if (row_type == ROW_TYPE_SHORTCUT)
1502     return retval;
1503
1504   retval += button->priv->n_shortcuts;
1505
1506   if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1507     return retval;
1508
1509   retval += button->priv->has_bookmark_separator;
1510
1511   if (row_type == ROW_TYPE_BOOKMARK)
1512     return retval;
1513
1514   retval += button->priv->n_bookmarks;
1515
1516   if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1517     return retval;
1518
1519   retval += button->priv->has_current_folder_separator;
1520
1521   if (row_type == ROW_TYPE_CURRENT_FOLDER)
1522     return retval;
1523
1524   retval += button->priv->has_current_folder;
1525
1526   if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1527     return retval;
1528
1529   retval += button->priv->has_other_separator;
1530
1531   if (row_type == ROW_TYPE_OTHER)
1532     return retval;
1533
1534   g_assert_not_reached ();
1535   return -1;
1536 }
1537
1538 static void
1539 model_free_row_data (GtkFileChooserButton *button,
1540                      GtkTreeIter          *iter)
1541 {
1542   gchar type;
1543   gpointer data;
1544   GtkFileSystemHandle *handle;
1545
1546   gtk_tree_model_get (button->priv->model, iter,
1547                       TYPE_COLUMN, &type,
1548                       DATA_COLUMN, &data,
1549                       HANDLE_COLUMN, &handle,
1550                       -1);
1551
1552   if (handle)
1553     gtk_file_system_cancel_operation (handle);
1554
1555   switch (type)
1556     {
1557     case ROW_TYPE_SPECIAL:
1558     case ROW_TYPE_SHORTCUT:
1559     case ROW_TYPE_BOOKMARK:
1560     case ROW_TYPE_CURRENT_FOLDER:
1561       gtk_file_path_free (data);
1562       break;
1563     case ROW_TYPE_VOLUME:
1564       gtk_file_system_volume_free (button->priv->fs, data);
1565       break;
1566     default:
1567       break;
1568     }
1569 }
1570
1571 static void
1572 model_add_special_get_info_cb (GtkFileSystemHandle *handle,
1573                                const GtkFileInfo   *info,
1574                                const GError        *error,
1575                                gpointer             user_data)
1576 {
1577   gboolean cancelled = handle->cancelled;
1578   GtkTreeIter iter;
1579   GtkTreePath *path;
1580   GdkPixbuf *pixbuf;
1581   GtkFileSystemHandle *model_handle;
1582   struct ChangeIconThemeData *data = user_data;
1583
1584   if (!data->button->priv->model)
1585     /* button got destroyed */
1586     goto out;
1587
1588   path = gtk_tree_row_reference_get_path (data->row_ref);
1589   if (!path)
1590     /* Handle doesn't exist anymore in the model */
1591     goto out;
1592
1593   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1594   gtk_tree_path_free (path);
1595
1596   gtk_tree_model_get (data->button->priv->model, &iter,
1597                       HANDLE_COLUMN, &model_handle,
1598                       -1);
1599   if (handle != model_handle)
1600     goto out;
1601
1602   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1603                       HANDLE_COLUMN, NULL,
1604                       -1);
1605
1606   if (cancelled || error)
1607     goto out;
1608
1609   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1610                                       data->button->priv->icon_size, NULL);
1611
1612   if (pixbuf)
1613     {
1614       gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1615                           ICON_COLUMN, pixbuf,
1616                           -1);
1617       g_object_unref (pixbuf);
1618     }
1619
1620   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1621                       DISPLAY_NAME_COLUMN, gtk_file_info_get_display_name (info),
1622                       -1);
1623
1624 out:
1625   g_object_unref (data->button);
1626   gtk_tree_row_reference_free (data->row_ref);
1627   g_free (data);
1628
1629   g_object_unref (handle);
1630 }
1631
1632 static inline void
1633 model_add_special (GtkFileChooserButton *button)
1634 {
1635   const gchar *homedir;
1636   gchar *desktopdir = NULL;
1637   GtkListStore *store;
1638   GtkTreeIter iter;
1639   GtkFilePath *path;
1640   gint pos;
1641
1642   store = GTK_LIST_STORE (button->priv->model);
1643   pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1644
1645   homedir = g_get_home_dir ();
1646
1647   if (homedir)
1648     {
1649       GtkTreePath *tree_path;
1650       GtkFileSystemHandle *handle;
1651       struct ChangeIconThemeData *info;
1652
1653       path = gtk_file_system_filename_to_path (button->priv->fs, homedir);
1654       gtk_list_store_insert (store, &iter, pos);
1655       pos++;
1656
1657       info = g_new0 (struct ChangeIconThemeData, 1);
1658       info->button = g_object_ref (button);
1659       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1660       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1661                                                   tree_path);
1662       gtk_tree_path_free (tree_path);
1663
1664       handle = gtk_file_system_get_info (button->priv->fs, path,
1665                                          GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
1666                                          model_add_special_get_info_cb, info);
1667
1668       gtk_list_store_set (store, &iter,
1669                           ICON_COLUMN, NULL,
1670                           DISPLAY_NAME_COLUMN, NULL,
1671                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1672                           DATA_COLUMN, path,
1673                           IS_FOLDER_COLUMN, TRUE,
1674                           HANDLE_COLUMN, handle,
1675                           -1);
1676
1677       button->priv->n_special++;
1678
1679 #ifndef G_OS_WIN32
1680       desktopdir = g_build_filename (homedir, DESKTOP_DISPLAY_NAME, NULL);
1681 #endif
1682     }
1683
1684 #ifdef G_OS_WIN32
1685   desktopdir = _gtk_file_system_win32_get_desktop ();
1686 #endif
1687
1688   if (desktopdir)
1689     {
1690       GtkTreePath *tree_path;
1691       GtkFileSystemHandle *handle;
1692       struct ChangeIconThemeData *info;
1693
1694       path = gtk_file_system_filename_to_path (button->priv->fs, desktopdir);
1695       g_free (desktopdir);
1696       gtk_list_store_insert (store, &iter, pos);
1697       pos++;
1698
1699       info = g_new0 (struct ChangeIconThemeData, 1);
1700       info->button = g_object_ref (button);
1701       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1702       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1703                                                   tree_path);
1704       gtk_tree_path_free (tree_path);
1705
1706       handle = gtk_file_system_get_info (button->priv->fs, path,
1707                                          GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
1708                                          model_add_special_get_info_cb, info);
1709
1710       gtk_list_store_set (store, &iter,
1711                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1712                           ICON_COLUMN, NULL,
1713                           DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1714                           DATA_COLUMN, path,
1715                           IS_FOLDER_COLUMN, TRUE,
1716                           HANDLE_COLUMN, handle,
1717                           -1);
1718
1719       button->priv->n_special++;
1720     }
1721 }
1722
1723 static void
1724 model_add_volumes (GtkFileChooserButton *button,
1725                    GSList               *volumes)
1726 {
1727   GtkListStore *store;
1728   gint pos;
1729   gboolean local_only;
1730   GtkFileSystem *file_system;
1731   GSList *l;
1732   
1733   if (!volumes)
1734     return;
1735
1736   store = GTK_LIST_STORE (button->priv->model);
1737   pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1738   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1739   file_system = button->priv->fs;
1740
1741   for (l = volumes; l; l = l->next)
1742     {
1743       GtkFileSystemVolume *volume;
1744       GtkTreeIter iter;
1745       GdkPixbuf *pixbuf;
1746       gchar *display_name;
1747
1748       volume = l->data;
1749
1750       if (local_only)
1751         {
1752           if (gtk_file_system_volume_get_is_mounted (file_system, volume))
1753             {
1754               GtkFilePath *base_path;
1755
1756               base_path = gtk_file_system_volume_get_base_path (file_system, volume);
1757               if (base_path != NULL)
1758                 {
1759                   gboolean is_local = gtk_file_system_path_is_local (file_system, base_path);
1760                   gtk_file_path_free (base_path);
1761
1762                   if (!is_local)
1763                     {
1764                       gtk_file_system_volume_free (file_system, volume);
1765                       continue;
1766                     }
1767                 }
1768             }
1769         }
1770
1771       pixbuf = gtk_file_system_volume_render_icon (file_system,
1772                                                    volume,
1773                                                    GTK_WIDGET (button),
1774                                                    button->priv->icon_size,
1775                                                    NULL);
1776       display_name = gtk_file_system_volume_get_display_name (file_system, volume);
1777
1778       gtk_list_store_insert (store, &iter, pos);
1779       gtk_list_store_set (store, &iter,
1780                           ICON_COLUMN, pixbuf,
1781                           DISPLAY_NAME_COLUMN, display_name,
1782                           TYPE_COLUMN, ROW_TYPE_VOLUME,
1783                           DATA_COLUMN, volume,
1784                           IS_FOLDER_COLUMN, TRUE,
1785                           -1);
1786
1787       if (pixbuf)
1788         g_object_unref (pixbuf);
1789       g_free (display_name);
1790
1791       button->priv->n_volumes++;
1792       pos++;
1793     }
1794 }
1795
1796 static void
1797 model_add_bookmarks (GtkFileChooserButton *button,
1798                      GSList               *bookmarks)
1799 {
1800   GtkListStore *store;
1801   GtkTreeIter iter;
1802   gint pos;
1803   gboolean local_only;
1804   GSList *l;
1805
1806   if (!bookmarks)
1807     return;
1808
1809   store = GTK_LIST_STORE (button->priv->model);
1810   pos = model_get_type_position (button, ROW_TYPE_BOOKMARK);
1811   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (button->priv->dialog));
1812
1813   for (l = bookmarks; l; l = l->next)
1814     {
1815       GtkFilePath *path;
1816
1817       path = l->data;
1818
1819       if (local_only &&
1820           !gtk_file_system_path_is_local (button->priv->fs, path))
1821         continue;
1822
1823       gtk_list_store_insert (store, &iter, pos);
1824       gtk_list_store_set (store, &iter,
1825                           ICON_COLUMN, NULL,
1826                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1827                           TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1828                           DATA_COLUMN, gtk_file_path_copy (path),
1829                           IS_FOLDER_COLUMN, FALSE,
1830                           -1);
1831       set_info_for_path_at_iter (button, path, &iter);
1832
1833       button->priv->n_bookmarks++;
1834       pos++;
1835     }
1836
1837   if (button->priv->n_bookmarks > 0 && 
1838       !button->priv->has_bookmark_separator)
1839     {
1840       pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
1841
1842       gtk_list_store_insert (store, &iter, pos);
1843       gtk_list_store_set (store, &iter,
1844                           ICON_COLUMN, NULL,
1845                           DISPLAY_NAME_COLUMN, NULL,
1846                           TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
1847                           DATA_COLUMN, NULL,
1848                           IS_FOLDER_COLUMN, FALSE,
1849                           -1);
1850       button->priv->has_bookmark_separator = TRUE;
1851     }
1852 }
1853
1854 static void
1855 model_update_current_folder (GtkFileChooserButton *button,
1856                              const GtkFilePath    *path)
1857 {
1858   GtkListStore *store;
1859   GtkTreeIter iter;
1860   gint pos;
1861
1862   if (!path) 
1863     return;
1864
1865   store = GTK_LIST_STORE (button->priv->model);
1866
1867   if (!button->priv->has_current_folder_separator)
1868     {
1869       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
1870       gtk_list_store_insert (store, &iter, pos);
1871       gtk_list_store_set (store, &iter,
1872                           ICON_COLUMN, NULL,
1873                           DISPLAY_NAME_COLUMN, NULL,
1874                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
1875                           DATA_COLUMN, NULL,
1876                           IS_FOLDER_COLUMN, FALSE,
1877                           -1);
1878       button->priv->has_current_folder_separator = TRUE;
1879     }
1880
1881   pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
1882   if (!button->priv->has_current_folder)
1883     {
1884       gtk_list_store_insert (store, &iter, pos);
1885       button->priv->has_current_folder = TRUE;
1886     }
1887   else
1888     {
1889       gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
1890       model_free_row_data (button, &iter);
1891     }
1892
1893   gtk_list_store_set (store, &iter,
1894                       ICON_COLUMN, NULL,
1895                       DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1896                       TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
1897                       DATA_COLUMN, gtk_file_path_copy (path),
1898                       IS_FOLDER_COLUMN, FALSE,
1899                       -1);
1900   set_info_for_path_at_iter (button, path, &iter);
1901 }
1902
1903 static inline void
1904 model_add_other (GtkFileChooserButton *button)
1905 {
1906   GtkListStore *store;
1907   GtkTreeIter iter;
1908   gint pos;
1909   
1910   store = GTK_LIST_STORE (button->priv->model);
1911   pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
1912
1913   gtk_list_store_insert (store, &iter, pos);
1914   gtk_list_store_set (store, &iter,
1915                       ICON_COLUMN, NULL,
1916                       DISPLAY_NAME_COLUMN, NULL,
1917                       TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
1918                       DATA_COLUMN, NULL,
1919                       IS_FOLDER_COLUMN, FALSE,
1920                       -1);
1921   button->priv->has_other_separator = TRUE;
1922   pos++;
1923
1924   gtk_list_store_insert (store, &iter, pos);
1925   gtk_list_store_set (store, &iter,
1926                       ICON_COLUMN, NULL,
1927                       DISPLAY_NAME_COLUMN, _("Other..."),
1928                       TYPE_COLUMN, ROW_TYPE_OTHER,
1929                       DATA_COLUMN, NULL,
1930                       IS_FOLDER_COLUMN, FALSE,
1931                       -1);
1932 }
1933
1934 static void
1935 model_remove_rows (GtkFileChooserButton *button,
1936                    gint                  pos,
1937                    gint                  n_rows)
1938 {
1939   GtkListStore *store;
1940
1941   if (!n_rows)
1942     return;
1943
1944   store = GTK_LIST_STORE (button->priv->model);
1945
1946   do
1947     {
1948       GtkTreeIter iter;
1949
1950       if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
1951         g_assert_not_reached ();
1952
1953       model_free_row_data (button, &iter);
1954       gtk_list_store_remove (store, &iter);
1955       n_rows--;
1956     }
1957   while (n_rows);
1958 }
1959
1960 /* Filter Model */
1961 static inline gboolean
1962 test_if_path_is_visible (GtkFileSystem     *fs,
1963                          const GtkFilePath *path,
1964                          gboolean           local_only,
1965                          gboolean           is_folder)
1966 {
1967   if (!path)
1968     return FALSE;
1969
1970   if (local_only && !gtk_file_system_path_is_local (fs, path))
1971     return FALSE;
1972
1973   if (!is_folder)
1974     return FALSE;
1975
1976   return TRUE;
1977 }
1978
1979 static gboolean
1980 filter_model_visible_func (GtkTreeModel *model,
1981                            GtkTreeIter  *iter,
1982                            gpointer      user_data)
1983 {
1984   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
1985   GtkFileChooserButtonPrivate *priv = button->priv;
1986   gchar type;
1987   gpointer data;
1988   gboolean local_only, retval, is_folder;
1989
1990   type = ROW_TYPE_INVALID;
1991   data = NULL;
1992   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
1993
1994   gtk_tree_model_get (model, iter,
1995                       TYPE_COLUMN, &type,
1996                       DATA_COLUMN, &data,
1997                       IS_FOLDER_COLUMN, &is_folder,
1998                       -1);
1999
2000   switch (type)
2001     {
2002     case ROW_TYPE_CURRENT_FOLDER:
2003       retval = TRUE;
2004       break;
2005     case ROW_TYPE_SPECIAL:
2006     case ROW_TYPE_SHORTCUT:
2007     case ROW_TYPE_BOOKMARK:
2008       retval = test_if_path_is_visible (priv->fs, data, local_only, is_folder);
2009       break;
2010     case ROW_TYPE_VOLUME:
2011       {
2012         if (local_only)
2013           {
2014             if (gtk_file_system_volume_get_is_mounted (priv->fs, data))
2015               {
2016                 GtkFilePath *base_path;
2017                 
2018                 base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2019                 if (base_path)
2020                   {
2021                     gboolean is_local = gtk_file_system_path_is_local (priv->fs, base_path);
2022                     
2023                     gtk_file_path_free (base_path);
2024
2025                     if (!is_local)
2026                       retval = FALSE;
2027                   }
2028                 else
2029                   retval = FALSE;
2030               }
2031           }
2032       }
2033       break;
2034     default:
2035       retval = TRUE;
2036       break;
2037     }
2038
2039   return retval;
2040 }
2041
2042 /* Combo Box */
2043 static void
2044 name_cell_data_func (GtkCellLayout   *layout,
2045                      GtkCellRenderer *cell,
2046                      GtkTreeModel    *model,
2047                      GtkTreeIter     *iter,
2048                      gpointer         user_data)
2049 {
2050   gchar type;
2051
2052   type = 0;
2053   gtk_tree_model_get (model, iter,
2054                       TYPE_COLUMN, &type,
2055                       -1);
2056
2057   if (type == ROW_TYPE_CURRENT_FOLDER)
2058     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2059   else
2060     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2061 }
2062
2063 static gboolean
2064 combo_box_row_separator_func (GtkTreeModel *model,
2065                               GtkTreeIter  *iter,
2066                               gpointer      user_data)
2067 {
2068   gchar type = ROW_TYPE_INVALID;
2069
2070   gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2071
2072   return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2073           type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2074           type == ROW_TYPE_OTHER_SEPARATOR);
2075 }                         
2076
2077 static void
2078 update_combo_box (GtkFileChooserButton *button)
2079 {
2080   GtkFileChooserButtonPrivate *priv = button->priv;
2081   GSList *paths;
2082   GtkTreeIter iter;
2083   gboolean row_found;
2084
2085   gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2086
2087   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2088
2089   row_found = FALSE;
2090
2091   do
2092     {
2093       gchar type;
2094       gpointer data;
2095
2096       type = ROW_TYPE_INVALID;
2097       data = NULL;
2098
2099       gtk_tree_model_get (priv->filter_model, &iter,
2100                           TYPE_COLUMN, &type,
2101                           DATA_COLUMN, &data,
2102                           -1);
2103     
2104       switch (type)
2105         {
2106         case ROW_TYPE_SPECIAL:
2107         case ROW_TYPE_SHORTCUT:
2108         case ROW_TYPE_BOOKMARK:
2109         case ROW_TYPE_CURRENT_FOLDER:
2110           row_found = (paths &&
2111                        paths->data &&
2112                        gtk_file_path_compare (data, paths->data) == 0);
2113           break;
2114         case ROW_TYPE_VOLUME:
2115           {
2116             GtkFilePath *base_path;
2117
2118             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2119             row_found = (paths &&
2120                          paths->data &&
2121                          gtk_file_path_compare (base_path, paths->data) == 0);
2122             gtk_file_path_free (base_path);
2123           }
2124           break;
2125         default:
2126           row_found = FALSE;
2127           break;
2128         }
2129
2130       if (row_found)
2131         {
2132           g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2133           gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2134                                          &iter);
2135           g_signal_handler_unblock (priv->combo_box,
2136                                     priv->combo_box_changed_id);
2137         }
2138     }
2139   while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2140
2141   /* If it hasn't been found already, update & select the current-folder row. */
2142   if (!row_found && paths && paths->data)
2143     {
2144       GtkTreeIter filter_iter;
2145       gint pos;
2146     
2147       model_update_current_folder (button, paths->data);
2148       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2149
2150       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2151       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2152
2153       gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2154                                                         &filter_iter, &iter);
2155
2156       g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2157       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2158       g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2159     }
2160
2161   gtk_file_paths_free (paths);
2162 }
2163
2164 /* Button */
2165 static void
2166 update_label_get_info_cb (GtkFileSystemHandle *handle,
2167                           const GtkFileInfo   *info,
2168                           const GError        *error,
2169                           gpointer             data)
2170 {
2171   gboolean cancelled = handle->cancelled;
2172   GdkPixbuf *pixbuf;
2173   GtkFileChooserButton *button = data;
2174   GtkFileChooserButtonPrivate *priv = button->priv;
2175
2176   if (handle != priv->update_button_handle)
2177     goto out;
2178
2179   priv->update_button_handle = NULL;
2180
2181   if (cancelled || error)
2182     goto out;
2183
2184   gtk_label_set_text (GTK_LABEL (priv->label), gtk_file_info_get_display_name (info));
2185
2186   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (priv->image),
2187                                       priv->icon_size, NULL);
2188   if (!pixbuf)
2189     pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2190                                        FALLBACK_ICON_NAME,
2191                                        priv->icon_size, 0, NULL);
2192
2193   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2194   if (pixbuf)
2195     g_object_unref (pixbuf);
2196
2197 out:
2198   g_object_unref (button);
2199   g_object_unref (handle);
2200 }
2201
2202 static void
2203 update_label_and_image (GtkFileChooserButton *button)
2204 {
2205   GtkFileChooserButtonPrivate *priv = button->priv;
2206   GdkPixbuf *pixbuf;
2207   gchar *label_text;
2208   GSList *paths;
2209
2210   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2211   label_text = NULL;
2212   pixbuf = NULL;
2213
2214   if (paths && paths->data)
2215     {
2216       GtkFilePath *path;
2217       GtkFileSystemVolume *volume = NULL;
2218     
2219       path = paths->data;
2220
2221       volume = gtk_file_system_get_volume_for_path (priv->fs, path);
2222       if (volume)
2223         {
2224           GtkFilePath *base_path;
2225
2226           base_path = gtk_file_system_volume_get_base_path (priv->fs, volume);
2227           if (base_path && gtk_file_path_compare (base_path, path) == 0)
2228             {
2229               label_text = gtk_file_system_volume_get_display_name (priv->fs,
2230                                                                     volume);
2231               pixbuf = gtk_file_system_volume_render_icon (priv->fs, volume,
2232                                                            GTK_WIDGET (button),
2233                                                            priv->icon_size,
2234                                                            NULL);
2235             }
2236
2237           if (base_path)
2238             gtk_file_path_free (base_path);
2239
2240           gtk_file_system_volume_free (priv->fs, volume);
2241
2242           if (label_text)
2243             goto out;
2244         }
2245
2246       if (priv->update_button_handle)
2247         gtk_file_system_cancel_operation (priv->update_button_handle);
2248
2249       priv->update_button_handle =
2250         gtk_file_system_get_info (priv->fs, path,
2251                                   GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
2252                                   update_label_get_info_cb,
2253                                   g_object_ref (button));
2254     }
2255 out:
2256   gtk_file_paths_free (paths);
2257
2258   if (label_text)
2259     {
2260       gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2261       g_free (label_text);
2262     }
2263   else
2264     gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2265 }
2266
2267
2268 /* ************************ *
2269  *  Child Object Callbacks  *
2270  * ************************ */
2271
2272 /* File System */
2273 static void
2274 fs_volumes_changed_cb (GtkFileSystem *fs,
2275                        gpointer       user_data)
2276 {
2277   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2278   GtkFileChooserButtonPrivate *priv = button->priv;
2279   GSList *volumes;
2280
2281   model_remove_rows (user_data,
2282                      model_get_type_position (user_data, ROW_TYPE_VOLUME),
2283                      priv->n_volumes);
2284
2285   priv->n_volumes = 0;
2286
2287   volumes = gtk_file_system_list_volumes (fs);
2288   model_add_volumes (user_data, volumes);
2289   g_slist_free (volumes);
2290
2291   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2292
2293   update_label_and_image (user_data);
2294   update_combo_box (user_data);
2295 }
2296
2297 static void
2298 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2299                          gpointer       user_data)
2300 {
2301   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2302   GtkFileChooserButtonPrivate *priv = button->priv;
2303   GSList *bookmarks;
2304
2305   bookmarks = gtk_file_system_list_bookmarks (fs);
2306   model_remove_rows (user_data,
2307                      model_get_type_position (user_data,
2308                                               ROW_TYPE_BOOKMARK_SEPARATOR),
2309                      (priv->n_bookmarks + priv->has_bookmark_separator));
2310   priv->has_bookmark_separator = FALSE;
2311   priv->n_bookmarks = 0;
2312   model_add_bookmarks (user_data, bookmarks);
2313   gtk_file_paths_free (bookmarks);
2314
2315   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2316
2317   update_label_and_image (user_data);
2318   update_combo_box (user_data);
2319 }
2320
2321 /* Dialog */
2322 static void
2323 open_dialog (GtkFileChooserButton *button)
2324 {
2325   GtkFileChooserButtonPrivate *priv = button->priv;
2326
2327   /* Setup the dialog parent to be chooser button's toplevel, and be modal
2328      as needed. */
2329   if (!GTK_WIDGET_VISIBLE (priv->dialog))
2330     {
2331       GtkWidget *toplevel;
2332
2333       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2334
2335       if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
2336         {
2337           if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2338             gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2339                                           GTK_WINDOW (toplevel));
2340               
2341           gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2342                                 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2343         }
2344     }
2345
2346   if (!priv->active)
2347     {
2348       GSList *paths;
2349
2350       g_signal_handler_block (priv->dialog,
2351                               priv->dialog_folder_changed_id);
2352       g_signal_handler_block (priv->dialog,
2353                               priv->dialog_file_activated_id);
2354       g_signal_handler_block (priv->dialog,
2355                               priv->dialog_selection_changed_id);
2356       paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2357       if (paths)
2358         {
2359           if (paths->data)
2360             priv->old_path = gtk_file_path_copy (paths->data);
2361
2362           gtk_file_paths_free (paths);
2363         }
2364
2365       priv->active = TRUE;
2366     }
2367
2368   gtk_widget_set_sensitive (priv->combo_box, FALSE);
2369   gtk_window_present (GTK_WINDOW (priv->dialog));
2370 }
2371
2372 /* Combo Box */
2373 static void
2374 combo_box_changed_cb (GtkComboBox *combo_box,
2375                       gpointer     user_data)
2376 {
2377   GtkTreeIter iter;
2378
2379   if (gtk_combo_box_get_active_iter (combo_box, &iter))
2380     {
2381       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2382       GtkFileChooserButtonPrivate *priv = button->priv;
2383       gchar type;
2384       gpointer data;
2385
2386       type = ROW_TYPE_INVALID;
2387       data = NULL;
2388
2389       gtk_tree_model_get (priv->filter_model, &iter,
2390                           TYPE_COLUMN, &type,
2391                           DATA_COLUMN, &data,
2392                           -1);
2393
2394       switch (type)
2395         {
2396         case ROW_TYPE_SPECIAL:
2397         case ROW_TYPE_SHORTCUT:
2398         case ROW_TYPE_BOOKMARK:
2399         case ROW_TYPE_CURRENT_FOLDER:
2400           gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2401           if (data)
2402             _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2403                                                        data, NULL);
2404           break;
2405         case ROW_TYPE_VOLUME:
2406           {
2407             GtkFilePath *base_path;
2408
2409             gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2410             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2411             if (base_path)
2412               {
2413                 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2414                                                            base_path, NULL);
2415                 gtk_file_path_free (base_path);
2416               }
2417           }
2418           break;
2419         case ROW_TYPE_OTHER:
2420           open_dialog (user_data);
2421           break;
2422         default:
2423           break;
2424         }
2425     }
2426 }
2427
2428 /* Button */
2429 static void
2430 button_clicked_cb (GtkButton *real_button,
2431                    gpointer   user_data)
2432 {
2433   open_dialog (user_data);
2434 }
2435
2436 /* Dialog */
2437 static void
2438 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2439                                   gpointer        user_data)
2440 {
2441   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2442   GtkFileChooserButtonPrivate *priv = button->priv;
2443
2444   priv->folder_has_been_set = TRUE;
2445
2446   g_signal_emit_by_name (button, "current-folder-changed");
2447 }
2448
2449 static void
2450 dialog_file_activated_cb (GtkFileChooser *dialog,
2451                           gpointer        user_data)
2452 {
2453   g_signal_emit_by_name (user_data, "file-activated");
2454 }
2455
2456 static void
2457 dialog_selection_changed_cb (GtkFileChooser *dialog,
2458                              gpointer        user_data)
2459 {
2460   update_label_and_image (user_data);
2461   update_combo_box (user_data);
2462   g_signal_emit_by_name (user_data, "selection-changed");
2463 }
2464
2465 static void
2466 dialog_update_preview_cb (GtkFileChooser *dialog,
2467                           gpointer        user_data)
2468 {
2469   g_signal_emit_by_name (user_data, "update-preview");
2470 }
2471
2472 static void
2473 dialog_notify_cb (GObject    *dialog,
2474                   GParamSpec *pspec,
2475                   gpointer    user_data)
2476 {
2477   gpointer iface;
2478
2479   iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2480                                  GTK_TYPE_FILE_CHOOSER);
2481   if (g_object_interface_find_property (iface, pspec->name))
2482     g_object_notify (user_data, pspec->name);
2483
2484   if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2485     {
2486       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2487       GtkFileChooserButtonPrivate *priv = button->priv;
2488
2489       if (priv->has_current_folder)
2490         {
2491           GtkTreeIter iter;
2492           gint pos;
2493           gpointer data;
2494
2495           pos = model_get_type_position (user_data,
2496                                          ROW_TYPE_CURRENT_FOLDER);
2497           gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2498
2499           data = NULL;
2500           gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2501
2502           /* If the path isn't local but we're in local-only mode now, remove
2503            * the custom-folder row */
2504           if (data &&
2505               (!gtk_file_system_path_is_local (priv->fs, data) &&
2506                gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog))))
2507             {
2508               pos--;
2509               model_remove_rows (user_data, pos, 2);
2510             }
2511         }
2512
2513       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2514       update_combo_box (user_data);
2515     }
2516 }
2517
2518 static gboolean
2519 dialog_delete_event_cb (GtkWidget *dialog,
2520                         GdkEvent  *event,
2521                         gpointer   user_data)
2522 {
2523   g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2524
2525   return TRUE;
2526 }
2527
2528 static void
2529 dialog_response_cb (GtkDialog *dialog,
2530                     gint       response,
2531                     gpointer   user_data)
2532 {
2533   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2534   GtkFileChooserButtonPrivate *priv = button->priv;
2535
2536   if (response == GTK_RESPONSE_ACCEPT)
2537     {
2538       g_signal_emit_by_name (user_data, "current-folder-changed");
2539       g_signal_emit_by_name (user_data, "selection-changed");
2540     }
2541   else if (priv->old_path)
2542     {
2543       switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)))
2544         {
2545         case GTK_FILE_CHOOSER_ACTION_OPEN:
2546           _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (dialog), priv->old_path,
2547                                          NULL);
2548           break;
2549         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
2550           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (dialog),
2551                                                      priv->old_path, NULL);
2552           break;
2553         default:
2554           g_assert_not_reached ();
2555           break;
2556         }
2557     }
2558   else
2559     gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (dialog));
2560
2561   if (priv->old_path)
2562     {
2563       gtk_file_path_free (priv->old_path);
2564       priv->old_path = NULL;
2565     }
2566
2567   update_label_and_image (user_data);
2568   update_combo_box (user_data);
2569   
2570   if (priv->active)
2571     {
2572       g_signal_handler_unblock (priv->dialog,
2573                                 priv->dialog_folder_changed_id);
2574       g_signal_handler_unblock (priv->dialog,
2575                                 priv->dialog_file_activated_id);
2576       g_signal_handler_unblock (priv->dialog,
2577                                 priv->dialog_selection_changed_id);
2578       priv->active = FALSE;
2579     }
2580
2581   gtk_widget_set_sensitive (priv->combo_box, TRUE);
2582   gtk_widget_hide (priv->dialog);
2583 }
2584
2585
2586 /* ************************************************************************** *
2587  *  Public API                                                                *
2588  * ************************************************************************** */
2589
2590 /**
2591  * gtk_file_chooser_button_new:
2592  * @title: the title of the browse dialog.
2593  * @action: the open mode for the widget.
2594  * 
2595  * Creates a new file-selecting button widget.
2596  * 
2597  * Returns: a new button widget.
2598  * 
2599  * Since: 2.6
2600  **/
2601 GtkWidget *
2602 gtk_file_chooser_button_new (const gchar          *title,
2603                              GtkFileChooserAction  action)
2604 {
2605   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2606                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2607
2608   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2609                        "action", action,
2610                        "title", (title ? title : _(DEFAULT_TITLE)),
2611                        NULL);
2612 }
2613
2614 /**
2615  * gtk_file_chooser_button_new_with_backend:
2616  * @title: the title of the browse dialog.
2617  * @action: the open mode for the widget.
2618  * @backend: the name of the #GtkFileSystem backend to use.
2619  * 
2620  * Creates a new file-selecting button widget using @backend.
2621  * 
2622  * Returns: a new button widget.
2623  * 
2624  * Since: 2.6
2625  **/
2626 GtkWidget *
2627 gtk_file_chooser_button_new_with_backend (const gchar          *title,
2628                                           GtkFileChooserAction  action,
2629                                           const gchar          *backend)
2630 {
2631   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2632                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2633
2634   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2635                        "action", action,
2636                        "title", (title ? title : _(DEFAULT_TITLE)),
2637                        "file-system-backend", backend,
2638                        NULL);
2639 }
2640
2641 /**
2642  * gtk_file_chooser_button_new_with_dialog:
2643  * @dialog: the #GtkFileChooserDialog widget to use.
2644  * 
2645  * Creates a #GtkFileChooserButton widget which uses @dialog as it's
2646  * file-picking window. Note that @dialog must be a #GtkFileChooserDialog (or
2647  * subclass) and must not have %GTK_DIALOG_DESTROY_WITH_PARENT set.
2648  * 
2649  * Returns: a new button widget.
2650  * 
2651  * Since: 2.6
2652  **/
2653 GtkWidget *
2654 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2655 {
2656   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_DIALOG (dialog), NULL);
2657
2658   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2659                        "dialog", dialog,
2660                        NULL);
2661 }
2662
2663 /**
2664  * gtk_file_chooser_button_set_title:
2665  * @button: the button widget to modify.
2666  * @title: the new browse dialog title.
2667  * 
2668  * Modifies the @title of the browse dialog used by @button.
2669  * 
2670  * Since: 2.6
2671  **/
2672 void
2673 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2674                                    const gchar          *title)
2675 {
2676   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2677
2678   gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2679   g_object_notify (G_OBJECT (button), "title");
2680 }
2681
2682 /**
2683  * gtk_file_chooser_button_get_title:
2684  * @button: the button widget to examine.
2685  * 
2686  * Retrieves the title of the browse dialog used by @button. The returned value
2687  * should not be modified or freed.
2688  * 
2689  * Returns: a pointer to the browse dialog's title.
2690  * 
2691  * Since: 2.6
2692  **/
2693 G_CONST_RETURN gchar *
2694 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2695 {
2696   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2697
2698   return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2699 }
2700
2701 /**
2702  * gtk_file_chooser_button_get_width_chars:
2703  * @button: the button widget to examine.
2704  * 
2705  * Retrieves the width in characters of the @button widget's entry and/or label.
2706  * 
2707  * Returns: an integer width (in characters) that the button will use to size itself.
2708  * 
2709  * Since: 2.6
2710  **/
2711 gint
2712 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2713 {
2714   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2715
2716   return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2717 }
2718
2719 /**
2720  * gtk_file_chooser_button_set_width_chars:
2721  * @button: the button widget to examine.
2722  * @n_chars: the new width, in characters.
2723  * 
2724  * Sets the width (in characters) that @button will use to @n_chars.
2725  * 
2726  * Since: 2.6
2727  **/
2728 void
2729 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
2730                                          gint                  n_chars)
2731 {
2732   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2733
2734   gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
2735   g_object_notify (G_OBJECT (button), "width-chars");
2736 }
2737
2738 /**
2739  * gtk_file_chooser_button_set_focus_on_click:
2740  * @button: a #GtkFileChooserButton
2741  * @focus_on_click: whether the button grabs focus when clicked with the mouse
2742  * 
2743  * Sets whether the button will grab focus when it is clicked with the mouse.
2744  * Making mouse clicks not grab focus is useful in places like toolbars where
2745  * you don't want the keyboard focus removed from the main area of the
2746  * application.
2747  *
2748  * Since: 2.10
2749  **/
2750 void
2751 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
2752                                             gboolean              focus_on_click)
2753 {
2754   GtkFileChooserButtonPrivate *priv;
2755
2756   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2757
2758   priv = button->priv;
2759
2760   focus_on_click = focus_on_click != FALSE;
2761
2762   if (priv->focus_on_click != focus_on_click)
2763     {
2764       priv->focus_on_click = focus_on_click;
2765       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
2766       gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
2767       
2768       g_object_notify (G_OBJECT (button), "focus-on-click");
2769     }
2770 }
2771
2772 /**
2773  * gtk_file_chooser_button_get_focus_on_click:
2774  * @button: a #GtkFileChooserButton
2775  * 
2776  * Returns whether the button grabs focus when it is clicked with the mouse.
2777  * See gtk_file_chooser_button_set_focus_on_click().
2778  *
2779  * Return value: %TRUE if the button grabs focus when it is clicked with
2780  *               the mouse.
2781  *
2782  * Since: 2.10
2783  **/
2784 gboolean
2785 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
2786 {
2787   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
2788   
2789   return button->priv->focus_on_click;
2790 }
2791
2792 #define __GTK_FILE_CHOOSER_BUTTON_C__
2793 #include "gtkaliasdef.c"