]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserbutton.c
c9d8809e13b8f0f502580c115dc189d314ddc4d1
[~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_LOCAL_ONLY:
801     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
802     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
803     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
804     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
805     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
806     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
807       g_object_set_property (G_OBJECT (priv->dialog), pspec->name, value);
808       break;
809
810     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
811       /* Construct-only */
812       priv->backend = g_value_dup_string (value);
813       break;
814
815     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
816       g_warning ("%s: Choosers of type `%s` do not support selecting multiple files.",
817                  G_STRFUNC, G_OBJECT_TYPE_NAME (object));
818       break;
819     default:
820       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
821       break;
822     }
823 }
824
825 static void
826 gtk_file_chooser_button_get_property (GObject    *object,
827                                       guint       param_id,
828                                       GValue     *value,
829                                       GParamSpec *pspec)
830 {
831   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
832   GtkFileChooserButtonPrivate *priv = button->priv;
833
834   switch (param_id)
835     {
836     case PROP_WIDTH_CHARS:
837       g_value_set_int (value,
838                        gtk_label_get_width_chars (GTK_LABEL (priv->label)));
839       break;
840     case PROP_FOCUS_ON_CLICK:
841       g_value_set_boolean (value,
842                            gtk_file_chooser_button_get_focus_on_click (button));
843       break;
844
845     case PROP_TITLE:
846     case GTK_FILE_CHOOSER_PROP_ACTION:
847     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
848     case GTK_FILE_CHOOSER_PROP_FILTER:
849     case GTK_FILE_CHOOSER_PROP_LOCAL_ONLY:
850     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET:
851     case GTK_FILE_CHOOSER_PROP_PREVIEW_WIDGET_ACTIVE:
852     case GTK_FILE_CHOOSER_PROP_USE_PREVIEW_LABEL:
853     case GTK_FILE_CHOOSER_PROP_EXTRA_WIDGET:
854     case GTK_FILE_CHOOSER_PROP_SELECT_MULTIPLE:
855     case GTK_FILE_CHOOSER_PROP_SHOW_HIDDEN:
856     case GTK_FILE_CHOOSER_PROP_DO_OVERWRITE_CONFIRMATION:
857       g_object_get_property (G_OBJECT (priv->dialog), pspec->name, value);
858       break;
859
860     default:
861       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
862       break;
863     }
864 }
865
866 static void
867 gtk_file_chooser_button_finalize (GObject *object)
868 {
869   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
870   GtkFileChooserButtonPrivate *priv = button->priv;
871
872   if (priv->old_path)
873     gtk_file_path_free (priv->old_path);
874
875   if (G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize != NULL)
876     (*G_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->finalize) (object);
877 }
878
879 /* ********************* *
880  *  GtkObject Functions  *
881  * ********************* */
882
883 static void
884 gtk_file_chooser_button_destroy (GtkObject *object)
885 {
886   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (object);
887   GtkFileChooserButtonPrivate *priv = button->priv;
888   GtkTreeIter iter;
889   GSList *l;
890
891   if (priv->dialog != NULL)
892     {
893       gtk_widget_destroy (priv->dialog);
894       priv->dialog = NULL;
895     }
896
897   if (priv->model && gtk_tree_model_get_iter_first (priv->model, &iter)) do
898     {
899       model_free_row_data (button, &iter);
900     }
901   while (gtk_tree_model_iter_next (priv->model, &iter));
902
903   if (priv->dnd_select_folder_handle)
904     {
905       gtk_file_system_cancel_operation (priv->dnd_select_folder_handle);
906       priv->dnd_select_folder_handle = NULL;
907     }
908
909   if (priv->update_button_handle)
910     {
911       gtk_file_system_cancel_operation (priv->update_button_handle);
912       priv->update_button_handle = NULL;
913     }
914
915   if (priv->change_icon_theme_handles)
916     {
917       for (l = priv->change_icon_theme_handles; l; l = l->next)
918         {
919           GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
920           gtk_file_system_cancel_operation (handle);
921         }
922       g_slist_free (priv->change_icon_theme_handles);
923       priv->change_icon_theme_handles = NULL;
924     }
925
926   if (priv->model)
927     {
928       g_object_unref (priv->model);
929       priv->model = NULL;
930     }
931
932   if (priv->filter_model)
933     {
934       g_object_unref (priv->filter_model);
935       priv->filter_model = NULL;
936     }
937
938   if (priv->fs)
939     {
940       g_signal_handler_disconnect (priv->fs, priv->fs_volumes_changed_id);
941       g_signal_handler_disconnect (priv->fs, priv->fs_bookmarks_changed_id);
942       g_object_unref (priv->fs);
943       priv->fs = NULL;
944     }
945
946   if (GTK_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->destroy != NULL)
947     (*GTK_OBJECT_CLASS (gtk_file_chooser_button_parent_class)->destroy) (object);
948 }
949
950
951 /* ********************* *
952  *  GtkWidget Functions  *
953  * ********************* */
954
955 struct DndSelectFolderData
956 {
957   GtkFileChooserButton *button;
958   GtkFileChooserAction action;
959   GtkFilePath *path;
960   gchar **uris;
961   guint i;
962   gboolean selected;
963 };
964
965 static void
966 dnd_select_folder_get_info_cb (GtkFileSystemHandle *handle,
967                                const GtkFileInfo   *info,
968                                const GError        *error,
969                                gpointer             user_data)
970 {
971   gboolean cancelled = handle->cancelled;
972   struct DndSelectFolderData *data = user_data;
973
974   if (handle != data->button->priv->dnd_select_folder_handle)
975     {
976       g_object_unref (data->button);
977       gtk_file_path_free (data->path);
978       g_strfreev (data->uris);
979       g_free (data);
980
981       g_object_unref (handle);
982       return;
983     }
984
985   data->button->priv->dnd_select_folder_handle = NULL;
986
987   if (!cancelled && !error && info != NULL)
988     {
989       data->selected = 
990         (((data->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER &&
991            gtk_file_info_get_is_folder (info)) ||
992           (data->action == GTK_FILE_CHOOSER_ACTION_OPEN &&
993            !gtk_file_info_get_is_folder (info))) &&
994          _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (data->button->priv->dialog),
995                                         data->path, NULL));
996     }
997   else
998     data->selected = FALSE;
999
1000   if (data->selected || data->uris[++data->i] == NULL)
1001     {
1002       g_object_unref (data->button);
1003       gtk_file_path_free (data->path);
1004       g_strfreev (data->uris);
1005       g_free (data);
1006
1007       g_object_unref (handle);
1008       return;
1009     }
1010
1011   if (data->path)
1012     gtk_file_path_free (data->path);
1013
1014   data->path = gtk_file_system_uri_to_path (handle->file_system,
1015                                             data->uris[data->i]);
1016
1017   data->button->priv->dnd_select_folder_handle =
1018     gtk_file_system_get_info (handle->file_system, data->path,
1019                               GTK_FILE_INFO_IS_FOLDER,
1020                               dnd_select_folder_get_info_cb, user_data);
1021
1022   g_object_unref (handle);
1023 }
1024
1025 static void
1026 gtk_file_chooser_button_drag_data_received (GtkWidget        *widget,
1027                                             GdkDragContext   *context,
1028                                             gint              x,
1029                                             gint              y,
1030                                             GtkSelectionData *data,
1031                                             guint             info,
1032                                             guint             drag_time)
1033 {
1034   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1035   GtkFileChooserButtonPrivate *priv = button->priv;
1036   GtkFilePath *path;
1037   gchar *text;
1038
1039   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received != NULL)
1040     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->drag_data_received) (widget,
1041                                                                                     context,
1042                                                                                     x, y,
1043                                                                                     data, info,
1044                                                                                     drag_time);
1045
1046   if (widget == NULL || context == NULL || data == NULL || data->length < 0)
1047     return;
1048
1049   switch (info)
1050     {
1051     case TEXT_URI_LIST:
1052       {
1053         gchar **uris;
1054         struct DndSelectFolderData *info;
1055
1056         uris = gtk_selection_data_get_uris (data);
1057         
1058         if (uris == NULL)
1059           break;
1060
1061         info = g_new0 (struct DndSelectFolderData, 1);
1062         info->button = g_object_ref (button);
1063         info->i = 0;
1064         info->uris = uris;
1065         info->selected = FALSE;
1066         g_object_get (priv->dialog, "action", &info->action, NULL);
1067
1068         info->path = gtk_file_system_uri_to_path (priv->fs,
1069                                                   info->uris[info->i]);
1070
1071         if (priv->dnd_select_folder_handle)
1072           gtk_file_system_cancel_operation (priv->dnd_select_folder_handle);
1073
1074         priv->dnd_select_folder_handle =
1075           gtk_file_system_get_info (priv->fs, info->path,
1076                                     GTK_FILE_INFO_IS_FOLDER,
1077                                     dnd_select_folder_get_info_cb, info);
1078       }
1079       break;
1080
1081     case TEXT_PLAIN:
1082       text = (char*) gtk_selection_data_get_text (data);
1083       path = gtk_file_path_new_steal (text);
1084       _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (priv->dialog), path,
1085                                      NULL);
1086       gtk_file_path_free (path);
1087       break;
1088
1089     default:
1090       break;
1091     }
1092
1093   gtk_drag_finish (context, TRUE, FALSE, drag_time);
1094 }
1095
1096 static void
1097 gtk_file_chooser_button_show_all (GtkWidget *widget)
1098 {
1099   gtk_widget_show (widget);
1100 }
1101
1102 static void
1103 gtk_file_chooser_button_hide_all (GtkWidget *widget)
1104 {
1105   gtk_widget_hide (widget);
1106 }
1107
1108 static void
1109 gtk_file_chooser_button_show (GtkWidget *widget)
1110 {
1111   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1112   GtkFileChooserButtonPrivate *priv = button->priv;
1113
1114   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show)
1115     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->show) (widget);
1116
1117   if (priv->active)
1118     open_dialog (GTK_FILE_CHOOSER_BUTTON (widget));
1119 }
1120
1121 static void
1122 gtk_file_chooser_button_hide (GtkWidget *widget)
1123 {
1124   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1125   GtkFileChooserButtonPrivate *priv = button->priv;
1126
1127   gtk_widget_hide (priv->dialog);
1128
1129   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide)
1130     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->hide) (widget);
1131 }
1132
1133 static void
1134 gtk_file_chooser_button_map (GtkWidget *widget)
1135 {
1136   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1137   GtkFileChooserButtonPrivate *priv = button->priv;
1138
1139   if (!priv->folder_has_been_set)
1140     {
1141       char *current_working_dir;
1142
1143       current_working_dir = g_get_current_dir ();
1144       gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), current_working_dir);
1145       g_free (current_working_dir);
1146
1147       priv->folder_has_been_set = TRUE;
1148     }
1149
1150   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map)
1151     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->map) (widget);
1152 }
1153
1154 static gboolean
1155 gtk_file_chooser_button_mnemonic_activate (GtkWidget *widget,
1156                                            gboolean   group_cycling)
1157 {
1158   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (widget);
1159   GtkFileChooserButtonPrivate *priv = button->priv;
1160
1161   switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (priv->dialog)))
1162     {
1163     case GTK_FILE_CHOOSER_ACTION_OPEN:
1164       gtk_widget_grab_focus (priv->button);
1165       break;
1166     case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1167       return gtk_widget_mnemonic_activate (priv->combo_box, group_cycling);
1168       break;
1169     default:
1170       g_assert_not_reached ();
1171       break;
1172     }
1173
1174   return TRUE;
1175 }
1176
1177 /* Changes the icons wherever it is needed */
1178 struct ChangeIconThemeData
1179 {
1180   GtkFileChooserButton *button;
1181   GtkTreeRowReference *row_ref;
1182 };
1183
1184 static void
1185 change_icon_theme_get_info_cb (GtkFileSystemHandle *handle,
1186                                const GtkFileInfo   *info,
1187                                const GError        *error,
1188                                gpointer             user_data)
1189 {
1190   gboolean cancelled = handle->cancelled;
1191   GdkPixbuf *pixbuf;
1192   struct ChangeIconThemeData *data = user_data;
1193
1194   if (!g_slist_find (data->button->priv->change_icon_theme_handles, handle))
1195     goto out;
1196
1197   data->button->priv->change_icon_theme_handles =
1198     g_slist_remove (data->button->priv->change_icon_theme_handles, handle);
1199
1200   if (cancelled || error)
1201     goto out;
1202
1203   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1204                                       data->button->priv->icon_size, NULL);
1205
1206   if (pixbuf)
1207     {
1208       gint width = 0;
1209       GtkTreeIter iter;
1210       GtkTreePath *path;
1211
1212       width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1213
1214       path = gtk_tree_row_reference_get_path (data->row_ref);
1215       gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1216       gtk_tree_path_free (path);
1217
1218       gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1219                           ICON_COLUMN, pixbuf,
1220                           -1);
1221       g_object_unref (pixbuf);
1222
1223       g_object_set (data->button->priv->icon_cell,
1224                     "width", width,
1225                     NULL);
1226     }
1227
1228 out:
1229   g_object_unref (data->button);
1230   gtk_tree_row_reference_free (data->row_ref);
1231   g_free (data);
1232
1233   g_object_unref (handle);
1234 }
1235
1236 static void
1237 change_icon_theme (GtkFileChooserButton *button)
1238 {
1239   GtkFileChooserButtonPrivate *priv = button->priv;
1240   GtkSettings *settings;
1241   GtkIconTheme *theme;
1242   GtkTreeIter iter;
1243   GSList *l;
1244   gint width = 0, height = 0;
1245
1246   for (l = button->priv->change_icon_theme_handles; l; l = l->next)
1247     {
1248       GtkFileSystemHandle *handle = GTK_FILE_SYSTEM_HANDLE (l->data);
1249       gtk_file_system_cancel_operation (handle);
1250     }
1251   g_slist_free (button->priv->change_icon_theme_handles);
1252   button->priv->change_icon_theme_handles = NULL;
1253
1254   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (button)));
1255
1256   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1257                                          &width, &height))
1258     priv->icon_size = MAX (width, height);
1259   else
1260     priv->icon_size = FALLBACK_ICON_SIZE;
1261
1262   update_label_and_image (button);
1263
1264   gtk_tree_model_get_iter_first (priv->model, &iter);
1265
1266   theme = get_icon_theme (GTK_WIDGET (button));
1267
1268   do
1269     {
1270       GdkPixbuf *pixbuf;
1271       gchar type;
1272       gpointer data;
1273
1274       type = ROW_TYPE_INVALID;
1275       gtk_tree_model_get (priv->model, &iter,
1276                           TYPE_COLUMN, &type,
1277                           DATA_COLUMN, &data,
1278                           -1);
1279
1280       switch (type)
1281         {
1282         case ROW_TYPE_SPECIAL:
1283         case ROW_TYPE_SHORTCUT:
1284         case ROW_TYPE_BOOKMARK:
1285         case ROW_TYPE_CURRENT_FOLDER:
1286           if (data)
1287             {
1288               GtkTreePath *path;
1289               GtkFileSystemHandle *handle;
1290               struct ChangeIconThemeData *info;
1291
1292               info = g_new0 (struct ChangeIconThemeData, 1);
1293               info->button = g_object_ref (button);
1294               path = gtk_tree_model_get_path (priv->model, &iter);
1295               info->row_ref = gtk_tree_row_reference_new (priv->model, path);
1296               gtk_tree_path_free (path);
1297
1298               handle =
1299                 gtk_file_system_get_info (priv->fs, data, GTK_FILE_INFO_ICON,
1300                                           change_icon_theme_get_info_cb,
1301                                           info);
1302               button->priv->change_icon_theme_handles =
1303                 g_slist_append (button->priv->change_icon_theme_handles, handle);
1304               pixbuf = NULL;
1305             }
1306           else
1307             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1308                                                priv->icon_size, 0, NULL);
1309           break;
1310         case ROW_TYPE_VOLUME:
1311           if (data)
1312             pixbuf = gtk_file_system_volume_render_icon (priv->fs, data,
1313                                                          GTK_WIDGET (button),
1314                                                          priv->icon_size,
1315                                                          NULL);
1316           else
1317             pixbuf = gtk_icon_theme_load_icon (theme, FALLBACK_ICON_NAME,
1318                                                priv->icon_size, 0, NULL);
1319           break;
1320         default:
1321           continue;
1322           break;
1323         }
1324
1325       if (pixbuf)
1326         width = MAX (width, gdk_pixbuf_get_width (pixbuf));
1327
1328       gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
1329                           ICON_COLUMN, pixbuf,
1330                           -1);
1331
1332       if (pixbuf)
1333         g_object_unref (pixbuf);
1334     }
1335   while (gtk_tree_model_iter_next (priv->model, &iter));
1336
1337   g_object_set (button->priv->icon_cell,
1338                 "width", width,
1339                 NULL);
1340 }
1341
1342 static void
1343 gtk_file_chooser_button_style_set (GtkWidget *widget,
1344                                    GtkStyle  *old_style)
1345 {
1346   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_set)
1347     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->style_set) (widget,
1348                                                                            old_style);
1349
1350   if (gtk_widget_has_screen (widget))
1351     change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget));
1352 }
1353
1354 static void
1355 gtk_file_chooser_button_screen_changed (GtkWidget *widget,
1356                                         GdkScreen *old_screen)
1357 {
1358   if (GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed)
1359     (*GTK_WIDGET_CLASS (gtk_file_chooser_button_parent_class)->screen_changed) (widget,
1360                                                                                 old_screen);
1361
1362   change_icon_theme (GTK_FILE_CHOOSER_BUTTON (widget)); 
1363 }
1364
1365
1366 /* ******************* *
1367  *  Utility Functions  *
1368  * ******************* */
1369
1370 /* General */
1371 static GtkIconTheme *
1372 get_icon_theme (GtkWidget *widget)
1373 {
1374   if (gtk_widget_has_screen (widget))
1375     return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
1376
1377   return gtk_icon_theme_get_default ();
1378 }
1379
1380
1381 struct SetDisplayNameData
1382 {
1383   GtkFileChooserButton *button;
1384   GtkTreeRowReference *row_ref;
1385 };
1386
1387 static void
1388 set_info_get_info_cb (GtkFileSystemHandle *handle,
1389                       const GtkFileInfo   *info,
1390                       const GError        *error,
1391                       gpointer             callback_data)
1392 {
1393   gboolean cancelled = handle->cancelled;
1394   GdkPixbuf *pixbuf;
1395   GtkTreePath *path;
1396   GtkTreeIter iter;
1397   GtkFileSystemHandle *model_handle;
1398   struct SetDisplayNameData *data = callback_data;
1399
1400   if (!data->button->priv->model)
1401     /* button got destroyed */
1402     goto out;
1403
1404   path = gtk_tree_row_reference_get_path (data->row_ref);
1405   if (!path)
1406     /* Handle doesn't exist anymore in the model */
1407     goto out;
1408
1409   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1410   gtk_tree_path_free (path);
1411
1412   /* Validate the handle */
1413   gtk_tree_model_get (data->button->priv->model, &iter,
1414                       HANDLE_COLUMN, &model_handle,
1415                       -1);
1416   if (handle != model_handle)
1417     goto out;
1418
1419   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1420                       HANDLE_COLUMN, NULL,
1421                       -1);
1422
1423   if (cancelled || error)
1424     /* There was an error, leave the fallback name in there */
1425     goto out;
1426
1427   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1428                                       data->button->priv->icon_size, NULL);
1429
1430   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1431                       ICON_COLUMN, pixbuf,
1432                       DISPLAY_NAME_COLUMN, gtk_file_info_get_display_name (info),
1433                       IS_FOLDER_COLUMN, gtk_file_info_get_is_folder (info),
1434                       -1);
1435
1436   if (pixbuf)
1437     g_object_unref (pixbuf);
1438
1439 out:
1440   g_object_unref (data->button);
1441   gtk_tree_row_reference_free (data->row_ref);
1442   g_free (data);
1443
1444   g_object_unref (handle);
1445 }
1446
1447 static void
1448 set_info_for_path_at_iter (GtkFileChooserButton *button,
1449                            const GtkFilePath    *path,
1450                            GtkTreeIter          *iter)
1451 {
1452   struct SetDisplayNameData *data;
1453   GtkTreePath *tree_path;
1454   GtkFileSystemHandle *handle;
1455
1456   data = g_new0 (struct SetDisplayNameData, 1);
1457   data->button = g_object_ref (button);
1458
1459   tree_path = gtk_tree_model_get_path (button->priv->model, iter);
1460   data->row_ref = gtk_tree_row_reference_new (button->priv->model, tree_path);
1461   gtk_tree_path_free (tree_path);
1462
1463   handle = gtk_file_system_get_info (button->priv->fs, path,
1464                                      GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER | GTK_FILE_INFO_ICON,
1465                                      set_info_get_info_cb, data);
1466
1467   gtk_list_store_set (GTK_LIST_STORE (button->priv->model), iter,
1468                       HANDLE_COLUMN, handle,
1469                       -1);
1470 }
1471
1472 /* Shortcuts Model */
1473 static gint
1474 model_get_type_position (GtkFileChooserButton *button,
1475                          RowType               row_type)
1476 {
1477   gint retval = 0;
1478
1479   if (row_type == ROW_TYPE_SPECIAL)
1480     return retval;
1481
1482   retval += button->priv->n_special;
1483
1484   if (row_type == ROW_TYPE_VOLUME)
1485     return retval;
1486
1487   retval += button->priv->n_volumes;
1488
1489   if (row_type == ROW_TYPE_SHORTCUT)
1490     return retval;
1491
1492   retval += button->priv->n_shortcuts;
1493
1494   if (row_type == ROW_TYPE_BOOKMARK_SEPARATOR)
1495     return retval;
1496
1497   retval += button->priv->has_bookmark_separator;
1498
1499   if (row_type == ROW_TYPE_BOOKMARK)
1500     return retval;
1501
1502   retval += button->priv->n_bookmarks;
1503
1504   if (row_type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR)
1505     return retval;
1506
1507   retval += button->priv->has_current_folder_separator;
1508
1509   if (row_type == ROW_TYPE_CURRENT_FOLDER)
1510     return retval;
1511
1512   retval += button->priv->has_current_folder;
1513
1514   if (row_type == ROW_TYPE_OTHER_SEPARATOR)
1515     return retval;
1516
1517   retval += button->priv->has_other_separator;
1518
1519   if (row_type == ROW_TYPE_OTHER)
1520     return retval;
1521
1522   g_assert_not_reached ();
1523   return -1;
1524 }
1525
1526 static void
1527 model_free_row_data (GtkFileChooserButton *button,
1528                      GtkTreeIter          *iter)
1529 {
1530   gchar type;
1531   gpointer data;
1532   GtkFileSystemHandle *handle;
1533
1534   gtk_tree_model_get (button->priv->model, iter,
1535                       TYPE_COLUMN, &type,
1536                       DATA_COLUMN, &data,
1537                       HANDLE_COLUMN, &handle,
1538                       -1);
1539
1540   if (handle)
1541     gtk_file_system_cancel_operation (handle);
1542
1543   switch (type)
1544     {
1545     case ROW_TYPE_SPECIAL:
1546     case ROW_TYPE_SHORTCUT:
1547     case ROW_TYPE_BOOKMARK:
1548     case ROW_TYPE_CURRENT_FOLDER:
1549       gtk_file_path_free (data);
1550       break;
1551     case ROW_TYPE_VOLUME:
1552       gtk_file_system_volume_free (button->priv->fs, data);
1553       break;
1554     default:
1555       break;
1556     }
1557 }
1558
1559 static void
1560 model_add_special_get_info_cb (GtkFileSystemHandle *handle,
1561                                const GtkFileInfo   *info,
1562                                const GError        *error,
1563                                gpointer             user_data)
1564 {
1565   gboolean cancelled = handle->cancelled;
1566   GtkTreeIter iter;
1567   GtkTreePath *path;
1568   GdkPixbuf *pixbuf;
1569   GtkFileSystemHandle *model_handle;
1570   struct ChangeIconThemeData *data = user_data;
1571
1572   if (!data->button->priv->model)
1573     /* button got destroyed */
1574     goto out;
1575
1576   path = gtk_tree_row_reference_get_path (data->row_ref);
1577   if (!path)
1578     /* Handle doesn't exist anymore in the model */
1579     goto out;
1580
1581   gtk_tree_model_get_iter (data->button->priv->model, &iter, path);
1582   gtk_tree_path_free (path);
1583
1584   gtk_tree_model_get (data->button->priv->model, &iter,
1585                       HANDLE_COLUMN, &model_handle,
1586                       -1);
1587   if (handle != model_handle)
1588     goto out;
1589
1590   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1591                       HANDLE_COLUMN, NULL,
1592                       -1);
1593
1594   if (cancelled || error)
1595     goto out;
1596
1597   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->button),
1598                                       data->button->priv->icon_size, NULL);
1599
1600   if (pixbuf)
1601     {
1602       gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1603                           ICON_COLUMN, pixbuf,
1604                           -1);
1605       g_object_unref (pixbuf);
1606     }
1607
1608   gtk_list_store_set (GTK_LIST_STORE (data->button->priv->model), &iter,
1609                       DISPLAY_NAME_COLUMN, gtk_file_info_get_display_name (info),
1610                       -1);
1611
1612 out:
1613   g_object_unref (data->button);
1614   gtk_tree_row_reference_free (data->row_ref);
1615   g_free (data);
1616
1617   g_object_unref (handle);
1618 }
1619
1620 static inline void
1621 model_add_special (GtkFileChooserButton *button)
1622 {
1623   const gchar *homedir;
1624   gchar *desktopdir = NULL;
1625   GtkListStore *store;
1626   GtkTreeIter iter;
1627   GtkFilePath *path;
1628   gint pos;
1629
1630   store = GTK_LIST_STORE (button->priv->model);
1631   pos = model_get_type_position (button, ROW_TYPE_SPECIAL);
1632
1633   homedir = g_get_home_dir ();
1634
1635   if (homedir)
1636     {
1637       GtkTreePath *tree_path;
1638       GtkFileSystemHandle *handle;
1639       struct ChangeIconThemeData *info;
1640
1641       path = gtk_file_system_filename_to_path (button->priv->fs, homedir);
1642       gtk_list_store_insert (store, &iter, pos);
1643       pos++;
1644
1645       info = g_new0 (struct ChangeIconThemeData, 1);
1646       info->button = g_object_ref (button);
1647       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1648       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1649                                                   tree_path);
1650       gtk_tree_path_free (tree_path);
1651
1652       handle = gtk_file_system_get_info (button->priv->fs, path,
1653                                          GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
1654                                          model_add_special_get_info_cb, info);
1655
1656       gtk_list_store_set (store, &iter,
1657                           ICON_COLUMN, NULL,
1658                           DISPLAY_NAME_COLUMN, NULL,
1659                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1660                           DATA_COLUMN, path,
1661                           IS_FOLDER_COLUMN, TRUE,
1662                           HANDLE_COLUMN, handle,
1663                           -1);
1664
1665       button->priv->n_special++;
1666
1667 #ifndef G_OS_WIN32
1668       desktopdir = g_build_filename (homedir, DESKTOP_DISPLAY_NAME, NULL);
1669 #endif
1670     }
1671
1672 #ifdef G_OS_WIN32
1673   desktopdir = _gtk_file_system_win32_get_desktop ();
1674 #endif
1675
1676   if (desktopdir)
1677     {
1678       GtkTreePath *tree_path;
1679       GtkFileSystemHandle *handle;
1680       struct ChangeIconThemeData *info;
1681
1682       path = gtk_file_system_filename_to_path (button->priv->fs, desktopdir);
1683       g_free (desktopdir);
1684       gtk_list_store_insert (store, &iter, pos);
1685       pos++;
1686
1687       info = g_new0 (struct ChangeIconThemeData, 1);
1688       info->button = g_object_ref (button);
1689       tree_path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
1690       info->row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store),
1691                                                   tree_path);
1692       gtk_tree_path_free (tree_path);
1693
1694       handle = gtk_file_system_get_info (button->priv->fs, path,
1695                                          GTK_FILE_INFO_ICON,
1696                                          model_add_special_get_info_cb, info);
1697
1698       gtk_list_store_set (store, &iter,
1699                           TYPE_COLUMN, ROW_TYPE_SPECIAL,
1700                           ICON_COLUMN, NULL,
1701                           DISPLAY_NAME_COLUMN, _(DESKTOP_DISPLAY_NAME),
1702                           DATA_COLUMN, path,
1703                           IS_FOLDER_COLUMN, TRUE,
1704                           HANDLE_COLUMN, handle,
1705                           -1);
1706
1707       button->priv->n_special++;
1708     }
1709 }
1710
1711 static void
1712 model_add_volumes (GtkFileChooserButton *button,
1713                    GSList               *volumes)
1714 {
1715   GtkListStore *store;
1716   gint pos;
1717
1718   if (!volumes)
1719     return;
1720
1721   store = GTK_LIST_STORE (button->priv->model);
1722   pos = model_get_type_position (button, ROW_TYPE_VOLUME);
1723
1724   do
1725     {
1726       GtkTreeIter iter;
1727       GdkPixbuf *pixbuf;
1728       gchar *display_name;
1729
1730       pixbuf = gtk_file_system_volume_render_icon (button->priv->fs,
1731                                                    volumes->data,
1732                                                    GTK_WIDGET (button),
1733                                                    button->priv->icon_size,
1734                                                    NULL);
1735       display_name = gtk_file_system_volume_get_display_name (button->priv->fs,
1736                                                               volumes->data);
1737
1738       gtk_list_store_insert (store, &iter, pos);
1739       gtk_list_store_set (store, &iter,
1740                           ICON_COLUMN, pixbuf,
1741                           DISPLAY_NAME_COLUMN, display_name,
1742                           TYPE_COLUMN, ROW_TYPE_VOLUME,
1743                           DATA_COLUMN, volumes->data,
1744                           IS_FOLDER_COLUMN, TRUE,
1745                           -1);
1746
1747       if (pixbuf)
1748         g_object_unref (pixbuf);
1749       g_free (display_name);
1750
1751       button->priv->n_volumes++;
1752       pos++;
1753       volumes = volumes->next;
1754     }
1755   while (volumes);
1756 }
1757
1758 static void
1759 model_add_bookmarks (GtkFileChooserButton *button,
1760                      GSList               *bookmarks)
1761 {
1762   GtkListStore *store;
1763   GtkTreeIter iter;
1764   gint pos;
1765
1766   if (!bookmarks)
1767     return;
1768
1769   store = GTK_LIST_STORE (button->priv->model);
1770   pos = model_get_type_position (button, ROW_TYPE_BOOKMARK_SEPARATOR);
1771
1772   if (!button->priv->has_bookmark_separator)
1773     {
1774       gtk_list_store_insert (store, &iter, pos);
1775       gtk_list_store_set (store, &iter,
1776                           ICON_COLUMN, NULL,
1777                           DISPLAY_NAME_COLUMN, NULL,
1778                           TYPE_COLUMN, ROW_TYPE_BOOKMARK_SEPARATOR,
1779                           DATA_COLUMN, NULL,
1780                           IS_FOLDER_COLUMN, FALSE,
1781                           -1);
1782       button->priv->has_bookmark_separator = TRUE;
1783     }
1784
1785   do
1786     {
1787       pos++;
1788
1789       gtk_list_store_insert (store, &iter, pos);
1790       gtk_list_store_set (store, &iter,
1791                           ICON_COLUMN, NULL,
1792                           DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1793                           TYPE_COLUMN, ROW_TYPE_BOOKMARK,
1794                           DATA_COLUMN, gtk_file_path_copy (bookmarks->data),
1795                           IS_FOLDER_COLUMN, FALSE,
1796                           -1);
1797       set_info_for_path_at_iter (button, bookmarks->data, &iter);
1798
1799       button->priv->n_bookmarks++;
1800       bookmarks = bookmarks->next;
1801     }
1802   while (bookmarks);
1803 }
1804
1805 static void
1806 model_update_current_folder (GtkFileChooserButton *button,
1807                              const GtkFilePath    *path)
1808 {
1809   GtkListStore *store;
1810   GtkTreeIter iter;
1811   gint pos;
1812
1813   if (!path) 
1814     return;
1815
1816   store = GTK_LIST_STORE (button->priv->model);
1817
1818   if (!button->priv->has_current_folder_separator)
1819     {
1820       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER_SEPARATOR);
1821       gtk_list_store_insert (store, &iter, pos);
1822       gtk_list_store_set (store, &iter,
1823                           ICON_COLUMN, NULL,
1824                           DISPLAY_NAME_COLUMN, NULL,
1825                           TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER_SEPARATOR,
1826                           DATA_COLUMN, NULL,
1827                           IS_FOLDER_COLUMN, FALSE,
1828                           -1);
1829       button->priv->has_current_folder_separator = TRUE;
1830     }
1831
1832   pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
1833   if (!button->priv->has_current_folder)
1834     {
1835       gtk_list_store_insert (store, &iter, pos);
1836       button->priv->has_current_folder = TRUE;
1837     }
1838   else
1839     {
1840       gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos);
1841       model_free_row_data (button, &iter);
1842     }
1843
1844   gtk_list_store_set (store, &iter,
1845                       ICON_COLUMN, NULL,
1846                       DISPLAY_NAME_COLUMN, _(FALLBACK_DISPLAY_NAME),
1847                       TYPE_COLUMN, ROW_TYPE_CURRENT_FOLDER,
1848                       DATA_COLUMN, gtk_file_path_copy (path),
1849                       IS_FOLDER_COLUMN, FALSE,
1850                       -1);
1851   set_info_for_path_at_iter (button, path, &iter);
1852 }
1853
1854 static inline void
1855 model_add_other (GtkFileChooserButton *button)
1856 {
1857   GtkListStore *store;
1858   GtkTreeIter iter;
1859   gint pos;
1860   
1861   store = GTK_LIST_STORE (button->priv->model);
1862   pos = model_get_type_position (button, ROW_TYPE_OTHER_SEPARATOR);
1863
1864   gtk_list_store_insert (store, &iter, pos);
1865   gtk_list_store_set (store, &iter,
1866                       ICON_COLUMN, NULL,
1867                       DISPLAY_NAME_COLUMN, NULL,
1868                       TYPE_COLUMN, ROW_TYPE_OTHER_SEPARATOR,
1869                       DATA_COLUMN, NULL,
1870                       IS_FOLDER_COLUMN, FALSE,
1871                       -1);
1872   button->priv->has_other_separator = TRUE;
1873   pos++;
1874
1875   gtk_list_store_insert (store, &iter, pos);
1876   gtk_list_store_set (store, &iter,
1877                       ICON_COLUMN, NULL,
1878                       DISPLAY_NAME_COLUMN, _("Other..."),
1879                       TYPE_COLUMN, ROW_TYPE_OTHER,
1880                       DATA_COLUMN, NULL,
1881                       IS_FOLDER_COLUMN, FALSE,
1882                       -1);
1883 }
1884
1885 static void
1886 model_remove_rows (GtkFileChooserButton *button,
1887                    gint                  pos,
1888                    gint                  n_rows)
1889 {
1890   GtkListStore *store;
1891
1892   if (!n_rows)
1893     return;
1894
1895   store = GTK_LIST_STORE (button->priv->model);
1896
1897   do
1898     {
1899       GtkTreeIter iter;
1900
1901       if (!gtk_tree_model_iter_nth_child (button->priv->model, &iter, NULL, pos))
1902         g_assert_not_reached ();
1903
1904       model_free_row_data (button, &iter);
1905       gtk_list_store_remove (store, &iter);
1906       n_rows--;
1907     }
1908   while (n_rows);
1909 }
1910
1911 /* Filter Model */
1912 static inline gboolean
1913 test_if_path_is_visible (GtkFileSystem     *fs,
1914                          const GtkFilePath *path,
1915                          gboolean           local_only,
1916                          gboolean           is_folder)
1917 {
1918   if (!path)
1919     return FALSE;
1920
1921   if (local_only && !gtk_file_system_path_is_local (fs, path))
1922     return FALSE;
1923
1924   if (!is_folder)
1925     return FALSE;
1926
1927   return TRUE;
1928 }
1929
1930 static gboolean
1931 filter_model_visible_func (GtkTreeModel *model,
1932                            GtkTreeIter  *iter,
1933                            gpointer      user_data)
1934 {
1935   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
1936   GtkFileChooserButtonPrivate *priv = button->priv;
1937   gchar type;
1938   gpointer data;
1939   gboolean local_only, retval, is_folder;
1940
1941   type = ROW_TYPE_INVALID;
1942   data = NULL;
1943   local_only = gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog));
1944
1945   gtk_tree_model_get (model, iter,
1946                       TYPE_COLUMN, &type,
1947                       DATA_COLUMN, &data,
1948                       IS_FOLDER_COLUMN, &is_folder,
1949                       -1);
1950
1951   switch (type)
1952     {
1953     case ROW_TYPE_CURRENT_FOLDER:
1954       retval = TRUE;
1955       break;
1956     case ROW_TYPE_SPECIAL:
1957     case ROW_TYPE_SHORTCUT:
1958     case ROW_TYPE_BOOKMARK:
1959       retval = test_if_path_is_visible (priv->fs, data, local_only, is_folder);
1960       break;
1961     case ROW_TYPE_VOLUME:
1962       {
1963         GtkFilePath *base_path;
1964
1965         base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
1966         if (base_path)
1967           {
1968             retval = (!local_only ||
1969                       gtk_file_system_path_is_local (priv->fs, base_path));
1970             gtk_file_path_free (base_path);
1971           }
1972         else
1973           retval = FALSE;
1974       }
1975       break;
1976     default:
1977       retval = TRUE;
1978       break;
1979     }
1980
1981   return retval;
1982 }
1983
1984 /* Combo Box */
1985 static void
1986 name_cell_data_func (GtkCellLayout   *layout,
1987                      GtkCellRenderer *cell,
1988                      GtkTreeModel    *model,
1989                      GtkTreeIter     *iter,
1990                      gpointer         user_data)
1991 {
1992   gchar type;
1993
1994   type = 0;
1995   gtk_tree_model_get (model, iter,
1996                       TYPE_COLUMN, &type,
1997                       -1);
1998
1999   if (type == ROW_TYPE_CURRENT_FOLDER)
2000     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2001   else
2002     g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_NONE, NULL);
2003 }
2004
2005 static gboolean
2006 combo_box_row_separator_func (GtkTreeModel *model,
2007                               GtkTreeIter  *iter,
2008                               gpointer      user_data)
2009 {
2010   gchar type = ROW_TYPE_INVALID;
2011
2012   gtk_tree_model_get (model, iter, TYPE_COLUMN, &type, -1);
2013
2014   return (type == ROW_TYPE_BOOKMARK_SEPARATOR ||
2015           type == ROW_TYPE_CURRENT_FOLDER_SEPARATOR ||
2016           type == ROW_TYPE_OTHER_SEPARATOR);
2017 }                         
2018
2019 static void
2020 update_combo_box (GtkFileChooserButton *button)
2021 {
2022   GtkFileChooserButtonPrivate *priv = button->priv;
2023   GSList *paths;
2024   GtkTreeIter iter;
2025   gboolean row_found;
2026
2027   gtk_tree_model_get_iter_first (priv->filter_model, &iter);
2028
2029   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2030
2031   row_found = FALSE;
2032
2033   do
2034     {
2035       gchar type;
2036       gpointer data;
2037
2038       type = ROW_TYPE_INVALID;
2039       data = NULL;
2040
2041       gtk_tree_model_get (priv->filter_model, &iter,
2042                           TYPE_COLUMN, &type,
2043                           DATA_COLUMN, &data,
2044                           -1);
2045     
2046       switch (type)
2047         {
2048         case ROW_TYPE_SPECIAL:
2049         case ROW_TYPE_SHORTCUT:
2050         case ROW_TYPE_BOOKMARK:
2051         case ROW_TYPE_CURRENT_FOLDER:
2052           row_found = (paths &&
2053                        paths->data &&
2054                        gtk_file_path_compare (data, paths->data) == 0);
2055           break;
2056         case ROW_TYPE_VOLUME:
2057           {
2058             GtkFilePath *base_path;
2059
2060             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2061             row_found = (paths &&
2062                          paths->data &&
2063                          gtk_file_path_compare (base_path, paths->data) == 0);
2064             gtk_file_path_free (base_path);
2065           }
2066           break;
2067         default:
2068           row_found = FALSE;
2069           break;
2070         }
2071
2072       if (row_found)
2073         {
2074           g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2075           gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box),
2076                                          &iter);
2077           g_signal_handler_unblock (priv->combo_box,
2078                                     priv->combo_box_changed_id);
2079         }
2080     }
2081   while (!row_found && gtk_tree_model_iter_next (priv->filter_model, &iter));
2082
2083   /* If it hasn't been found already, update & select the current-folder row. */
2084   if (!row_found && paths && paths->data)
2085     {
2086       GtkTreeIter filter_iter;
2087       gint pos;
2088     
2089       model_update_current_folder (button, paths->data);
2090       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2091
2092       pos = model_get_type_position (button, ROW_TYPE_CURRENT_FOLDER);
2093       gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2094
2095       gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
2096                                                         &filter_iter, &iter);
2097
2098       g_signal_handler_block (priv->combo_box, priv->combo_box_changed_id);
2099       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->combo_box), &filter_iter);
2100       g_signal_handler_unblock (priv->combo_box, priv->combo_box_changed_id);
2101     }
2102
2103   gtk_file_paths_free (paths);
2104 }
2105
2106 /* Button */
2107 static void
2108 update_label_get_info_cb (GtkFileSystemHandle *handle,
2109                           const GtkFileInfo   *info,
2110                           const GError        *error,
2111                           gpointer             data)
2112 {
2113   gboolean cancelled = handle->cancelled;
2114   GdkPixbuf *pixbuf;
2115   GtkFileChooserButton *button = data;
2116   GtkFileChooserButtonPrivate *priv = button->priv;
2117
2118   if (handle != priv->update_button_handle)
2119     goto out;
2120
2121   priv->update_button_handle = NULL;
2122
2123   if (cancelled || error)
2124     goto out;
2125
2126   gtk_label_set_text (GTK_LABEL (priv->label), gtk_file_info_get_display_name (info));
2127
2128   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (priv->image),
2129                                       priv->icon_size, NULL);
2130   if (!pixbuf)
2131     pixbuf = gtk_icon_theme_load_icon (get_icon_theme (GTK_WIDGET (priv->image)),
2132                                        FALLBACK_ICON_NAME,
2133                                        priv->icon_size, 0, NULL);
2134
2135   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), pixbuf);
2136   if (pixbuf)
2137     g_object_unref (pixbuf);
2138
2139 out:
2140   g_object_unref (button);
2141   g_object_unref (handle);
2142 }
2143
2144 static void
2145 update_label_and_image (GtkFileChooserButton *button)
2146 {
2147   GtkFileChooserButtonPrivate *priv = button->priv;
2148   GdkPixbuf *pixbuf;
2149   gchar *label_text;
2150   GSList *paths;
2151
2152   paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2153   label_text = NULL;
2154   pixbuf = NULL;
2155
2156   if (paths && paths->data)
2157     {
2158       GtkFilePath *path;
2159       GtkFileSystemVolume *volume = NULL;
2160     
2161       path = paths->data;
2162
2163       volume = gtk_file_system_get_volume_for_path (priv->fs, path);
2164       if (volume)
2165         {
2166           GtkFilePath *base_path;
2167
2168           base_path = gtk_file_system_volume_get_base_path (priv->fs, volume);
2169           if (base_path && gtk_file_path_compare (base_path, path) == 0)
2170             {
2171               label_text = gtk_file_system_volume_get_display_name (priv->fs,
2172                                                                     volume);
2173               pixbuf = gtk_file_system_volume_render_icon (priv->fs, volume,
2174                                                            GTK_WIDGET (button),
2175                                                            priv->icon_size,
2176                                                            NULL);
2177             }
2178
2179           if (base_path)
2180             gtk_file_path_free (base_path);
2181
2182           gtk_file_system_volume_free (priv->fs, volume);
2183
2184           if (label_text)
2185             goto out;
2186         }
2187
2188       if (priv->update_button_handle)
2189         gtk_file_system_cancel_operation (priv->update_button_handle);
2190
2191       priv->update_button_handle =
2192         gtk_file_system_get_info (priv->fs, path,
2193                                   GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_ICON,
2194                                   update_label_get_info_cb,
2195                                   g_object_ref (button));
2196     }
2197 out:
2198   gtk_file_paths_free (paths);
2199
2200   if (label_text)
2201     {
2202       gtk_label_set_text (GTK_LABEL (priv->label), label_text);
2203       g_free (label_text);
2204     }
2205   else
2206     gtk_label_set_text (GTK_LABEL (priv->label), _(FALLBACK_DISPLAY_NAME));
2207 }
2208
2209
2210 /* ************************ *
2211  *  Child Object Callbacks  *
2212  * ************************ */
2213
2214 /* File System */
2215 static void
2216 fs_volumes_changed_cb (GtkFileSystem *fs,
2217                        gpointer       user_data)
2218 {
2219   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2220   GtkFileChooserButtonPrivate *priv = button->priv;
2221   GSList *volumes;
2222
2223   model_remove_rows (user_data,
2224                      model_get_type_position (user_data, ROW_TYPE_VOLUME),
2225                      priv->n_volumes);
2226
2227   priv->n_volumes = 0;
2228
2229   volumes = gtk_file_system_list_volumes (fs);
2230   model_add_volumes (user_data, volumes);
2231   g_slist_free (volumes);
2232
2233   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2234
2235   update_label_and_image (user_data);
2236   update_combo_box (user_data);
2237 }
2238
2239 static void
2240 fs_bookmarks_changed_cb (GtkFileSystem *fs,
2241                          gpointer       user_data)
2242 {
2243   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2244   GtkFileChooserButtonPrivate *priv = button->priv;
2245   GSList *bookmarks;
2246
2247   bookmarks = gtk_file_system_list_bookmarks (fs);
2248   if (!bookmarks)
2249     {
2250       model_remove_rows (user_data,
2251                          model_get_type_position (user_data,
2252                                                   ROW_TYPE_BOOKMARK_SEPARATOR),
2253                          (priv->n_bookmarks + priv->has_bookmark_separator));
2254       priv->has_bookmark_separator = FALSE;
2255     }
2256   else
2257     model_remove_rows (user_data,
2258                        model_get_type_position (user_data, ROW_TYPE_BOOKMARK),
2259                        priv->n_bookmarks);
2260
2261   priv->n_bookmarks = 0;
2262   model_add_bookmarks (user_data, bookmarks);
2263   gtk_file_paths_free (bookmarks);
2264
2265   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2266
2267   update_label_and_image (user_data);
2268   update_combo_box (user_data);
2269 }
2270
2271 /* Dialog */
2272 static void
2273 open_dialog (GtkFileChooserButton *button)
2274 {
2275   GtkFileChooserButtonPrivate *priv = button->priv;
2276
2277   /* Setup the dialog parent to be chooser button's toplevel, and be modal
2278      as needed. */
2279   if (!GTK_WIDGET_VISIBLE (priv->dialog))
2280     {
2281       GtkWidget *toplevel;
2282
2283       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
2284
2285       if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
2286         {
2287           if (GTK_WINDOW (toplevel) != gtk_window_get_transient_for (GTK_WINDOW (priv->dialog)))
2288             gtk_window_set_transient_for (GTK_WINDOW (priv->dialog),
2289                                           GTK_WINDOW (toplevel));
2290               
2291           gtk_window_set_modal (GTK_WINDOW (priv->dialog),
2292                                 gtk_window_get_modal (GTK_WINDOW (toplevel)));
2293         }
2294     }
2295
2296   if (!priv->active)
2297     {
2298       GSList *paths;
2299
2300       g_signal_handler_block (priv->dialog,
2301                               priv->dialog_folder_changed_id);
2302       g_signal_handler_block (priv->dialog,
2303                               priv->dialog_file_activated_id);
2304       g_signal_handler_block (priv->dialog,
2305                               priv->dialog_selection_changed_id);
2306       paths = _gtk_file_chooser_get_paths (GTK_FILE_CHOOSER (priv->dialog));
2307       if (paths)
2308         {
2309           if (paths->data)
2310             priv->old_path = gtk_file_path_copy (paths->data);
2311
2312           gtk_file_paths_free (paths);
2313         }
2314
2315       priv->active = TRUE;
2316     }
2317
2318   gtk_widget_set_sensitive (priv->combo_box, FALSE);
2319   gtk_window_present (GTK_WINDOW (priv->dialog));
2320 }
2321
2322 /* Combo Box */
2323 static void
2324 combo_box_changed_cb (GtkComboBox *combo_box,
2325                       gpointer     user_data)
2326 {
2327   GtkTreeIter iter;
2328
2329   if (gtk_combo_box_get_active_iter (combo_box, &iter))
2330     {
2331       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2332       GtkFileChooserButtonPrivate *priv = button->priv;
2333       gchar type;
2334       gpointer data;
2335
2336       type = ROW_TYPE_INVALID;
2337       data = NULL;
2338
2339       gtk_tree_model_get (priv->filter_model, &iter,
2340                           TYPE_COLUMN, &type,
2341                           DATA_COLUMN, &data,
2342                           -1);
2343
2344       switch (type)
2345         {
2346         case ROW_TYPE_SPECIAL:
2347         case ROW_TYPE_SHORTCUT:
2348         case ROW_TYPE_BOOKMARK:
2349         case ROW_TYPE_CURRENT_FOLDER:
2350           gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2351           if (data)
2352             _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2353                                                        data, NULL);
2354           break;
2355         case ROW_TYPE_VOLUME:
2356           {
2357             GtkFilePath *base_path;
2358
2359             gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->dialog));
2360             base_path = gtk_file_system_volume_get_base_path (priv->fs, data);
2361             if (base_path)
2362               {
2363                 _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (priv->dialog),
2364                                                            base_path, NULL);
2365                 gtk_file_path_free (base_path);
2366               }
2367           }
2368           break;
2369         case ROW_TYPE_OTHER:
2370           open_dialog (user_data);
2371           break;
2372         default:
2373           break;
2374         }
2375     }
2376 }
2377
2378 /* Button */
2379 static void
2380 button_clicked_cb (GtkButton *real_button,
2381                    gpointer   user_data)
2382 {
2383   open_dialog (user_data);
2384 }
2385
2386 /* Dialog */
2387 static void
2388 dialog_current_folder_changed_cb (GtkFileChooser *dialog,
2389                                   gpointer        user_data)
2390 {
2391   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2392   GtkFileChooserButtonPrivate *priv = button->priv;
2393
2394   priv->folder_has_been_set = TRUE;
2395
2396   g_signal_emit_by_name (button, "current-folder-changed");
2397 }
2398
2399 static void
2400 dialog_file_activated_cb (GtkFileChooser *dialog,
2401                           gpointer        user_data)
2402 {
2403   g_signal_emit_by_name (user_data, "file-activated");
2404 }
2405
2406 static void
2407 dialog_selection_changed_cb (GtkFileChooser *dialog,
2408                              gpointer        user_data)
2409 {
2410   update_label_and_image (user_data);
2411   update_combo_box (user_data);
2412   g_signal_emit_by_name (user_data, "selection-changed");
2413 }
2414
2415 static void
2416 dialog_update_preview_cb (GtkFileChooser *dialog,
2417                           gpointer        user_data)
2418 {
2419   g_signal_emit_by_name (user_data, "update-preview");
2420 }
2421
2422 static void
2423 dialog_notify_cb (GObject    *dialog,
2424                   GParamSpec *pspec,
2425                   gpointer    user_data)
2426 {
2427   gpointer iface;
2428
2429   iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (dialog)),
2430                                  GTK_TYPE_FILE_CHOOSER);
2431   if (g_object_interface_find_property (iface, pspec->name))
2432     g_object_notify (user_data, pspec->name);
2433
2434   if (g_ascii_strcasecmp (pspec->name, "local-only") == 0)
2435     {
2436       GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2437       GtkFileChooserButtonPrivate *priv = button->priv;
2438
2439       if (priv->has_current_folder)
2440         {
2441           GtkTreeIter iter;
2442           gint pos;
2443           gpointer data;
2444
2445           pos = model_get_type_position (user_data,
2446                                          ROW_TYPE_CURRENT_FOLDER);
2447           gtk_tree_model_iter_nth_child (priv->model, &iter, NULL, pos);
2448
2449           data = NULL;
2450           gtk_tree_model_get (priv->model, &iter, DATA_COLUMN, &data, -1);
2451
2452           /* If the path isn't local but we're in local-only mode now, remove
2453            * the custom-folder row */
2454           if (data &&
2455               (!gtk_file_system_path_is_local (priv->fs, data) &&
2456                gtk_file_chooser_get_local_only (GTK_FILE_CHOOSER (priv->dialog))))
2457             {
2458               pos--;
2459               model_remove_rows (user_data, pos, 2);
2460             }
2461         }
2462
2463       gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
2464       update_combo_box (user_data);
2465     }
2466 }
2467
2468 static gboolean
2469 dialog_delete_event_cb (GtkWidget *dialog,
2470                         GdkEvent  *event,
2471                         gpointer   user_data)
2472 {
2473   g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_DELETE_EVENT);
2474
2475   return TRUE;
2476 }
2477
2478 static void
2479 dialog_response_cb (GtkDialog *dialog,
2480                     gint       response,
2481                     gpointer   user_data)
2482 {
2483   GtkFileChooserButton *button = GTK_FILE_CHOOSER_BUTTON (user_data);
2484   GtkFileChooserButtonPrivate *priv = button->priv;
2485
2486   if (response == GTK_RESPONSE_ACCEPT)
2487     {
2488       g_signal_emit_by_name (user_data, "current-folder-changed");
2489       g_signal_emit_by_name (user_data, "selection-changed");
2490     }
2491   else if (priv->old_path)
2492     {
2493       switch (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)))
2494         {
2495         case GTK_FILE_CHOOSER_ACTION_OPEN:
2496           _gtk_file_chooser_select_path (GTK_FILE_CHOOSER (dialog), priv->old_path,
2497                                          NULL);
2498           break;
2499         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
2500           _gtk_file_chooser_set_current_folder_path (GTK_FILE_CHOOSER (dialog),
2501                                                      priv->old_path, NULL);
2502           break;
2503         default:
2504           g_assert_not_reached ();
2505           break;
2506         }
2507     }
2508   else
2509     gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (dialog));
2510
2511   if (priv->old_path)
2512     {
2513       gtk_file_path_free (priv->old_path);
2514       priv->old_path = NULL;
2515     }
2516
2517   update_label_and_image (user_data);
2518   update_combo_box (user_data);
2519   
2520   if (priv->active)
2521     {
2522       g_signal_handler_unblock (priv->dialog,
2523                                 priv->dialog_folder_changed_id);
2524       g_signal_handler_unblock (priv->dialog,
2525                                 priv->dialog_file_activated_id);
2526       g_signal_handler_unblock (priv->dialog,
2527                                 priv->dialog_selection_changed_id);
2528       priv->active = FALSE;
2529     }
2530
2531   gtk_widget_set_sensitive (priv->combo_box, TRUE);
2532   gtk_widget_hide (priv->dialog);
2533 }
2534
2535
2536 /* ************************************************************************** *
2537  *  Public API                                                                *
2538  * ************************************************************************** */
2539
2540 /**
2541  * gtk_file_chooser_button_new:
2542  * @title: the title of the browse dialog.
2543  * @action: the open mode for the widget.
2544  * 
2545  * Creates a new file-selecting button widget.
2546  * 
2547  * Returns: a new button widget.
2548  * 
2549  * Since: 2.6
2550  **/
2551 GtkWidget *
2552 gtk_file_chooser_button_new (const gchar          *title,
2553                              GtkFileChooserAction  action)
2554 {
2555   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2556                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2557
2558   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2559                        "action", action,
2560                        "title", (title ? title : _(DEFAULT_TITLE)),
2561                        NULL);
2562 }
2563
2564 /**
2565  * gtk_file_chooser_button_new_with_backend:
2566  * @title: the title of the browse dialog.
2567  * @action: the open mode for the widget.
2568  * @backend: the name of the #GtkFileSystem backend to use.
2569  * 
2570  * Creates a new file-selecting button widget using @backend.
2571  * 
2572  * Returns: a new button widget.
2573  * 
2574  * Since: 2.6
2575  **/
2576 GtkWidget *
2577 gtk_file_chooser_button_new_with_backend (const gchar          *title,
2578                                           GtkFileChooserAction  action,
2579                                           const gchar          *backend)
2580 {
2581   g_return_val_if_fail (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
2582                         action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, NULL);
2583
2584   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2585                        "action", action,
2586                        "title", (title ? title : _(DEFAULT_TITLE)),
2587                        "file-system-backend", backend,
2588                        NULL);
2589 }
2590
2591 /**
2592  * gtk_file_chooser_button_new_with_dialog:
2593  * @dialog: the #GtkFileChooserDialog widget to use.
2594  * 
2595  * Creates a #GtkFileChooserButton widget which uses @dialog as it's
2596  * file-picking window. Note that @dialog must be a #GtkFileChooserDialog (or
2597  * subclass) and must not have %GTK_DIALOG_DESTROY_WITH_PARENT set.
2598  * 
2599  * Returns: a new button widget.
2600  * 
2601  * Since: 2.6
2602  **/
2603 GtkWidget *
2604 gtk_file_chooser_button_new_with_dialog (GtkWidget *dialog)
2605 {
2606   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_DIALOG (dialog), NULL);
2607
2608   return g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
2609                        "dialog", dialog,
2610                        NULL);
2611 }
2612
2613 /**
2614  * gtk_file_chooser_button_set_title:
2615  * @button: the button widget to modify.
2616  * @title: the new browse dialog title.
2617  * 
2618  * Modifies the @title of the browse dialog used by @button.
2619  * 
2620  * Since: 2.6
2621  **/
2622 void
2623 gtk_file_chooser_button_set_title (GtkFileChooserButton *button,
2624                                    const gchar          *title)
2625 {
2626   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2627
2628   gtk_window_set_title (GTK_WINDOW (button->priv->dialog), title);
2629   g_object_notify (G_OBJECT (button), "title");
2630 }
2631
2632 /**
2633  * gtk_file_chooser_button_get_title:
2634  * @button: the button widget to examine.
2635  * 
2636  * Retrieves the title of the browse dialog used by @button. The returned value
2637  * should not be modified or freed.
2638  * 
2639  * Returns: a pointer to the browse dialog's title.
2640  * 
2641  * Since: 2.6
2642  **/
2643 G_CONST_RETURN gchar *
2644 gtk_file_chooser_button_get_title (GtkFileChooserButton *button)
2645 {
2646   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), NULL);
2647
2648   return gtk_window_get_title (GTK_WINDOW (button->priv->dialog));
2649 }
2650
2651 /**
2652  * gtk_file_chooser_button_get_width_chars:
2653  * @button: the button widget to examine.
2654  * 
2655  * Retrieves the width in characters of the @button widget's entry and/or label.
2656  * 
2657  * Returns: an integer width (in characters) that the button will use to size itself.
2658  * 
2659  * Since: 2.6
2660  **/
2661 gint
2662 gtk_file_chooser_button_get_width_chars (GtkFileChooserButton *button)
2663 {
2664   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), -1);
2665
2666   return gtk_label_get_width_chars (GTK_LABEL (button->priv->label));
2667 }
2668
2669 /**
2670  * gtk_file_chooser_button_set_width_chars:
2671  * @button: the button widget to examine.
2672  * @n_chars: the new width, in characters.
2673  * 
2674  * Sets the width (in characters) that @button will use to @n_chars.
2675  * 
2676  * Since: 2.6
2677  **/
2678 void
2679 gtk_file_chooser_button_set_width_chars (GtkFileChooserButton *button,
2680                                          gint                  n_chars)
2681 {
2682   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2683
2684   gtk_label_set_width_chars (GTK_LABEL (button->priv->label), n_chars);
2685   g_object_notify (G_OBJECT (button), "width-chars");
2686 }
2687
2688 /**
2689  * gtk_file_chooser_button_set_focus_on_click:
2690  * @button: a #GtkFileChooserButton
2691  * @focus_on_click: whether the button grabs focus when clicked with the mouse
2692  * 
2693  * Sets whether the button will grab focus when it is clicked with the mouse.
2694  * Making mouse clicks not grab focus is useful in places like toolbars where
2695  * you don't want the keyboard focus removed from the main area of the
2696  * application.
2697  *
2698  * Since: 2.10
2699  **/
2700 void
2701 gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
2702                                             gboolean              focus_on_click)
2703 {
2704   GtkFileChooserButtonPrivate *priv;
2705
2706   g_return_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button));
2707
2708   priv = button->priv;
2709
2710   focus_on_click = focus_on_click != FALSE;
2711
2712   if (priv->focus_on_click != focus_on_click)
2713     {
2714       priv->focus_on_click = focus_on_click;
2715       gtk_button_set_focus_on_click (GTK_BUTTON (priv->button), focus_on_click);
2716       gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (priv->combo_box), focus_on_click);
2717       
2718       g_object_notify (G_OBJECT (button), "focus-on-click");
2719     }
2720 }
2721
2722 /**
2723  * gtk_file_chooser_button_get_focus_on_click:
2724  * @button: a #GtkFileChooserButton
2725  * 
2726  * Returns whether the button grabs focus when it is clicked with the mouse.
2727  * See gtk_file_chooser_button_set_focus_on_click().
2728  *
2729  * Return value: %TRUE if the button grabs focus when it is clicked with
2730  *               the mouse.
2731  *
2732  * Since: 2.10
2733  **/
2734 gboolean
2735 gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button)
2736 {
2737   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_BUTTON (button), FALSE);
2738   
2739   return button->priv->focus_on_click;
2740 }
2741
2742 #define __GTK_FILE_CHOOSER_BUTTON_C__
2743 #include "gtkaliasdef.c"