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