]> Pileus Git - ~andy/gtk/blob - gtk/gtkpathbar.c
Merged the federico-filename-entry branch, to fix bug #136541. Combined
[~andy/gtk] / gtk / gtkpathbar.c
1 /* gtkpathbar.c
2  * Copyright (C) 2004  Red Hat, Inc.,  Jonathan Blandford <jrb@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21 #include <string.h>
22 #include "gtkpathbar.h"
23 #include "gtktogglebutton.h"
24 #include "gtkalignment.h"
25 #include "gtkarrow.h"
26 #include "gtkdnd.h"
27 #include "gtkimage.h"
28 #include "gtkintl.h"
29 #include "gtkicontheme.h"
30 #include "gtkiconfactory.h"
31 #include "gtklabel.h"
32 #include "gtkhbox.h"
33 #include "gtkmain.h"
34 #include "gtkmarshalers.h"
35 #include "gtkalias.h"
36
37 enum {
38   PATH_CLICKED,
39   LAST_SIGNAL
40 };
41
42 typedef enum {
43   NORMAL_BUTTON,
44   ROOT_BUTTON,
45   HOME_BUTTON,
46   DESKTOP_BUTTON
47 } ButtonType;
48
49 #define BUTTON_DATA(x) ((ButtonData *)(x))
50
51 #define SCROLL_DELAY_FACTOR 5
52
53 static guint path_bar_signals [LAST_SIGNAL] = { 0 };
54
55 /* Icon size for if we can't get it from the theme */
56 #define FALLBACK_ICON_SIZE 16
57
58 typedef struct _ButtonData ButtonData;
59
60 struct _ButtonData
61 {
62   GtkWidget *button;
63   ButtonType type;
64   char *dir_name;
65   GtkFilePath *path;
66   GtkWidget *image;
67   GtkWidget *label;
68   GtkFileSystemHandle *handle;
69   guint ignore_changes : 1;
70   guint file_is_hidden : 1;
71 };
72 /* This macro is used to check if a button can be used as a fake root.
73  * All buttons in front of a fake root are automatically hidden when in a
74  * directory below a fake root and replaced with the "<" arrow button.
75  */
76 #define BUTTON_IS_FAKE_ROOT(button) ((button)->type == HOME_BUTTON)
77
78 G_DEFINE_TYPE (GtkPathBar, gtk_path_bar, GTK_TYPE_CONTAINER);
79
80 static void gtk_path_bar_finalize                 (GObject          *object);
81 static void gtk_path_bar_dispose                  (GObject          *object);
82 static void gtk_path_bar_size_request             (GtkWidget        *widget,
83                                                    GtkRequisition   *requisition);
84 static void gtk_path_bar_unmap                    (GtkWidget          *widget);
85 static void gtk_path_bar_size_allocate            (GtkWidget        *widget,
86                                                    GtkAllocation    *allocation);
87 static void gtk_path_bar_add                      (GtkContainer     *container,
88                                                    GtkWidget        *widget);
89 static void gtk_path_bar_remove                   (GtkContainer     *container,
90                                                    GtkWidget        *widget);
91 static void gtk_path_bar_forall                   (GtkContainer     *container,
92                                                    gboolean          include_internals,
93                                                    GtkCallback       callback,
94                                                    gpointer          callback_data);
95 static void gtk_path_bar_scroll_up                (GtkWidget        *button,
96                                                    GtkPathBar       *path_bar);
97 static void gtk_path_bar_scroll_down              (GtkWidget        *button,
98                                                    GtkPathBar       *path_bar);
99 static void gtk_path_bar_stop_scrolling           (GtkPathBar       *path_bar);
100 static gboolean gtk_path_bar_slider_button_press  (GtkWidget        *widget,
101                                                    GdkEventButton   *event,
102                                                    GtkPathBar       *path_bar);
103 static gboolean gtk_path_bar_slider_button_release(GtkWidget        *widget,
104                                                    GdkEventButton   *event,
105                                                    GtkPathBar       *path_bar);
106 static void gtk_path_bar_grab_notify              (GtkWidget        *widget,
107                                                    gboolean          was_grabbed);
108 static void gtk_path_bar_state_changed            (GtkWidget        *widget,
109                                                    GtkStateType      previous_state);
110 static void gtk_path_bar_style_set                (GtkWidget        *widget,
111                                                    GtkStyle         *previous_style);
112 static void gtk_path_bar_screen_changed           (GtkWidget        *widget,
113                                                    GdkScreen        *previous_screen);
114 static void gtk_path_bar_check_icon_theme         (GtkPathBar       *path_bar);
115 static void gtk_path_bar_update_button_appearance (GtkPathBar       *path_bar,
116                                                    ButtonData       *button_data,
117                                                    gboolean          current_dir);
118
119 static GtkWidget *
120 get_slider_button (GtkPathBar  *path_bar,
121                    GtkArrowType arrow_type)
122 {
123   GtkWidget *button;
124
125   gtk_widget_push_composite_child ();
126
127   button = gtk_button_new ();
128   gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
129   gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
130   gtk_container_add (GTK_CONTAINER (path_bar), button);
131   gtk_widget_show_all (button);
132
133   gtk_widget_pop_composite_child ();
134
135   return button;
136 }
137
138 static void
139 gtk_path_bar_init (GtkPathBar *path_bar)
140 {
141   GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
142   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
143
144   path_bar->set_path_handle = NULL;
145
146   path_bar->spacing = 0;
147   path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
148   path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
149   path_bar->icon_size = FALLBACK_ICON_SIZE;
150   
151   g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
152   g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
153
154   g_signal_connect (path_bar->up_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
155   g_signal_connect (path_bar->up_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
156   g_signal_connect (path_bar->down_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
157   g_signal_connect (path_bar->down_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
158 }
159
160 static void
161 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
162 {
163   GObjectClass *gobject_class;
164   GtkObjectClass *object_class;
165   GtkWidgetClass *widget_class;
166   GtkContainerClass *container_class;
167
168   gobject_class = (GObjectClass *) path_bar_class;
169   object_class = (GtkObjectClass *) path_bar_class;
170   widget_class = (GtkWidgetClass *) path_bar_class;
171   container_class = (GtkContainerClass *) path_bar_class;
172
173   gobject_class->finalize = gtk_path_bar_finalize;
174   gobject_class->dispose = gtk_path_bar_dispose;
175
176   widget_class->size_request = gtk_path_bar_size_request;
177   widget_class->unmap = gtk_path_bar_unmap;
178   widget_class->size_allocate = gtk_path_bar_size_allocate;
179   widget_class->style_set = gtk_path_bar_style_set;
180   widget_class->screen_changed = gtk_path_bar_screen_changed;
181   widget_class->grab_notify = gtk_path_bar_grab_notify;
182   widget_class->state_changed = gtk_path_bar_state_changed;
183
184   container_class->add = gtk_path_bar_add;
185   container_class->forall = gtk_path_bar_forall;
186   container_class->remove = gtk_path_bar_remove;
187   /* FIXME: */
188   /*  container_class->child_type = gtk_path_bar_child_type;*/
189
190   path_bar_signals [PATH_CLICKED] =
191     g_signal_new (I_("path-clicked"),
192                   G_OBJECT_CLASS_TYPE (object_class),
193                   G_SIGNAL_RUN_FIRST,
194                   G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
195                   NULL, NULL,
196                   _gtk_marshal_VOID__POINTER_POINTER_BOOLEAN,
197                   G_TYPE_NONE, 3,
198                   G_TYPE_POINTER,
199                   G_TYPE_POINTER,
200                   G_TYPE_BOOLEAN);
201 }
202
203
204 static void
205 gtk_path_bar_finalize (GObject *object)
206 {
207   GtkPathBar *path_bar;
208
209   path_bar = GTK_PATH_BAR (object);
210
211   gtk_path_bar_stop_scrolling (path_bar);
212
213   g_list_free (path_bar->button_list);
214   if (path_bar->root_path)
215     gtk_file_path_free (path_bar->root_path);
216   if (path_bar->home_path)
217     gtk_file_path_free (path_bar->home_path);
218   if (path_bar->desktop_path)
219     gtk_file_path_free (path_bar->desktop_path);
220
221   if (path_bar->root_icon)
222     g_object_unref (path_bar->root_icon);
223   if (path_bar->home_icon)
224     g_object_unref (path_bar->home_icon);
225   if (path_bar->desktop_icon)
226     g_object_unref (path_bar->desktop_icon);
227
228   if (path_bar->file_system)
229     g_object_unref (path_bar->file_system);
230
231   G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
232 }
233
234 /* Removes the settings signal handler.  It's safe to call multiple times */
235 static void
236 remove_settings_signal (GtkPathBar *path_bar,
237                         GdkScreen  *screen)
238 {
239   if (path_bar->settings_signal_id)
240     {
241       GtkSettings *settings;
242
243       settings = gtk_settings_get_for_screen (screen);
244       g_signal_handler_disconnect (settings,
245                                    path_bar->settings_signal_id);
246       path_bar->settings_signal_id = 0;
247     }
248 }
249
250 static void
251 gtk_path_bar_dispose (GObject *object)
252 {
253   GtkPathBar *path_bar = GTK_PATH_BAR (object);
254
255   remove_settings_signal (path_bar, gtk_widget_get_screen (GTK_WIDGET (object)));
256
257   if (path_bar->set_path_handle)
258     gtk_file_system_cancel_operation (path_bar->set_path_handle);
259   path_bar->set_path_handle = NULL;
260
261   G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
262 }
263
264 /* Size requisition:
265  * 
266  * Ideally, our size is determined by another widget, and we are just filling
267  * available space.
268  */
269 static void
270 gtk_path_bar_size_request (GtkWidget      *widget,
271                            GtkRequisition *requisition)
272 {
273   ButtonData *button_data;
274   GtkPathBar *path_bar;
275   GtkRequisition child_requisition;
276   GList *list;
277
278   path_bar = GTK_PATH_BAR (widget);
279
280   requisition->width = 0;
281   requisition->height = 0;
282
283   for (list = path_bar->button_list; list; list = list->next)
284     {
285       button_data = BUTTON_DATA (list->data);
286       gtk_widget_size_request (button_data->button, &child_requisition);
287       requisition->width = MAX (child_requisition.width, requisition->width);
288       requisition->height = MAX (child_requisition.height, requisition->height);
289     }
290
291   /* Add space for slider, if we have more than one path */
292   /* Theoretically, the slider could be bigger than the other button.  But we're
293    * not going to worry about that now.
294    */
295   path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
296   if (path_bar->button_list && path_bar->button_list->next != NULL)
297     requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
298
299   gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
300   gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
301
302   requisition->width += GTK_CONTAINER (widget)->border_width * 2;
303   requisition->height += GTK_CONTAINER (widget)->border_width * 2;
304
305   widget->requisition = *requisition;
306 }
307
308 static void
309 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
310 {
311   if (path_bar->button_list)
312     {
313       GtkWidget *button;
314
315       button = BUTTON_DATA (path_bar->button_list->data)->button;
316       if (gtk_widget_get_child_visible (button))
317         {
318           gtk_path_bar_stop_scrolling (path_bar);
319           gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
320         }
321       else
322         gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
323
324       button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
325       if (gtk_widget_get_child_visible (button))
326         {
327           gtk_path_bar_stop_scrolling (path_bar);
328           gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
329         }
330       else
331         gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
332     }
333 }
334
335 static void
336 gtk_path_bar_unmap (GtkWidget *widget)
337 {
338   gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
339
340   GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unmap (widget);
341 }
342
343 /* This is a tad complicated
344  */
345 static void
346 gtk_path_bar_size_allocate (GtkWidget     *widget,
347                             GtkAllocation *allocation)
348 {
349   GtkWidget *child;
350   GtkPathBar *path_bar = GTK_PATH_BAR (widget);
351   GtkTextDirection direction;
352   GtkAllocation child_allocation;
353   GList *list, *first_button;
354   gint width;
355   gint allocation_width;
356   gint border_width;
357   gboolean need_sliders = FALSE;
358   gint up_slider_offset = 0;
359   gint down_slider_offset = 0;
360
361   widget->allocation = *allocation;
362
363   /* No path is set; we don't have to allocate anything. */
364   if (path_bar->button_list == NULL)
365     return;
366
367   direction = gtk_widget_get_direction (widget);
368   border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
369   allocation_width = allocation->width - 2 * border_width;
370
371   /* First, we check to see if we need the scrollbars. */
372   if (path_bar->fake_root)
373     width = path_bar->spacing + path_bar->slider_width;
374   else
375       width = 0;
376
377   for (list = path_bar->button_list; list; list = list->next)
378     {
379       child = BUTTON_DATA (list->data)->button;
380
381       width += child->requisition.width + path_bar->spacing;
382       if (list == path_bar->fake_root)
383         break;
384     }
385
386   if (width <= allocation_width)
387     {
388       if (path_bar->fake_root)
389         first_button = path_bar->fake_root;
390       else
391         first_button = g_list_last (path_bar->button_list);
392     }
393   else
394     {
395       gboolean reached_end = FALSE;
396       gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
397
398       if (path_bar->first_scrolled_button)
399         first_button = path_bar->first_scrolled_button;
400       else
401         first_button = path_bar->button_list;
402       need_sliders = TRUE;
403       
404       /* To see how much space we have, and how many buttons we can display.
405        * We start at the first button, count forward until hit the new
406        * button, then count backwards.
407        */
408       /* Count down the path chain towards the end. */
409       width = BUTTON_DATA (first_button->data)->button->requisition.width;
410       list = first_button->prev;
411       while (list && !reached_end)
412         {
413           child = BUTTON_DATA (list->data)->button;
414
415           if (width + child->requisition.width +
416               path_bar->spacing + slider_space > allocation_width)
417             reached_end = TRUE;
418           else if (list == path_bar->fake_root)
419             break;
420           else
421             width += child->requisition.width + path_bar->spacing;
422
423           list = list->prev;
424         }
425
426       /* Finally, we walk up, seeing how many of the previous buttons we can
427        * add */
428       while (first_button->next && !reached_end)
429         {
430           child = BUTTON_DATA (first_button->next->data)->button;
431
432           if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
433             {
434               reached_end = TRUE;
435             }
436           else
437             {
438               width += child->requisition.width + path_bar->spacing;
439               if (first_button == path_bar->fake_root)
440                 break;
441               first_button = first_button->next;
442             }
443         }
444     }
445
446   /* Now, we allocate space to the buttons */
447   child_allocation.y = allocation->y + border_width;
448   child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
449
450   if (direction == GTK_TEXT_DIR_RTL)
451     {
452       child_allocation.x = allocation->x + allocation->width - border_width;
453       if (need_sliders || path_bar->fake_root)
454         {
455           child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
456           up_slider_offset = allocation->width - border_width - path_bar->slider_width;
457         }
458     }
459   else
460     {
461       child_allocation.x = allocation->x + border_width;
462       if (need_sliders || path_bar->fake_root)
463         {
464           up_slider_offset = border_width;
465           child_allocation.x += (path_bar->spacing + path_bar->slider_width);
466         }
467     }
468
469   for (list = first_button; list; list = list->prev)
470     {
471       child = BUTTON_DATA (list->data)->button;
472
473       child_allocation.width = child->requisition.width;
474       if (direction == GTK_TEXT_DIR_RTL)
475         child_allocation.x -= child_allocation.width;
476
477       /* Check to see if we've don't have any more space to allocate buttons */
478       if (need_sliders && direction == GTK_TEXT_DIR_RTL)
479         {
480           if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
481             break;
482         }
483       else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
484         {
485           if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
486               widget->allocation.x + border_width + allocation_width)
487             break;
488         }
489
490       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
491       gtk_widget_size_allocate (child, &child_allocation);
492
493       if (direction == GTK_TEXT_DIR_RTL)
494         {
495           child_allocation.x -= path_bar->spacing;
496           down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
497           down_slider_offset = border_width;
498         }
499       else
500         {
501           down_slider_offset = child_allocation.x - widget->allocation.x;
502           down_slider_offset = allocation->width - border_width - path_bar->slider_width;
503           child_allocation.x += child_allocation.width + path_bar->spacing;
504         }
505     }
506   /* Now we go hide all the widgets that don't fit */
507   while (list)
508     {
509       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
510       list = list->prev;
511     }
512   for (list = first_button->next; list; list = list->next)
513     {
514       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
515     }
516
517   if (need_sliders || path_bar->fake_root)
518     {
519       child_allocation.width = path_bar->slider_width;
520       child_allocation.x = up_slider_offset + allocation->x;
521       gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
522
523       gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
524       gtk_widget_show_all (path_bar->up_slider_button);
525     }
526   else
527     gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
528       
529   if (need_sliders)
530     {
531       child_allocation.width = path_bar->slider_width;
532       child_allocation.x = down_slider_offset + allocation->x;
533       gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
534
535       gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
536       gtk_widget_show_all (path_bar->down_slider_button);
537       gtk_path_bar_update_slider_buttons (path_bar);
538     }
539   else
540     gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
541 }
542
543 static void
544 gtk_path_bar_style_set (GtkWidget *widget,
545                         GtkStyle  *previous_style)
546 {
547   if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
548     GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
549
550   gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
551 }
552
553 static void
554 gtk_path_bar_screen_changed (GtkWidget *widget,
555                              GdkScreen *previous_screen)
556 {
557   if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
558     GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
559
560   /* We might nave a new settings, so we remove the old one */
561   if (previous_screen)
562     remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
563
564   gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
565 }
566
567 static void
568 gtk_path_bar_add (GtkContainer *container,
569                   GtkWidget    *widget)
570 {
571   gtk_widget_set_parent (widget, GTK_WIDGET (container));
572 }
573
574 static void
575 gtk_path_bar_remove_1 (GtkContainer *container,
576                        GtkWidget    *widget)
577 {
578   gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
579   gtk_widget_unparent (widget);
580   if (was_visible)
581     gtk_widget_queue_resize (GTK_WIDGET (container));
582 }
583
584 static void
585 gtk_path_bar_remove (GtkContainer *container,
586                      GtkWidget    *widget)
587 {
588   GtkPathBar *path_bar;
589   GList *children;
590
591   path_bar = GTK_PATH_BAR (container);
592
593   if (widget == path_bar->up_slider_button)
594     {
595       gtk_path_bar_remove_1 (container, widget);
596       path_bar->up_slider_button = NULL;
597       return;
598     }
599
600   if (widget == path_bar->down_slider_button)
601     {
602       gtk_path_bar_remove_1 (container, widget);
603       path_bar->down_slider_button = NULL;
604       return;
605     }
606
607   children = path_bar->button_list;
608   while (children)
609     {
610       if (widget == BUTTON_DATA (children->data)->button)
611         {
612           gtk_path_bar_remove_1 (container, widget);
613           path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
614           g_list_free (children);
615           return;
616         }
617       
618       children = children->next;
619     }
620 }
621
622 static void
623 gtk_path_bar_forall (GtkContainer *container,
624                      gboolean      include_internals,
625                      GtkCallback   callback,
626                      gpointer      callback_data)
627 {
628   GtkPathBar *path_bar;
629   GList *children;
630
631   g_return_if_fail (callback != NULL);
632   path_bar = GTK_PATH_BAR (container);
633
634   children = path_bar->button_list;
635   while (children)
636     {
637       GtkWidget *child;
638       child = BUTTON_DATA (children->data)->button;
639       children = children->next;
640
641       (* callback) (child, callback_data);
642     }
643
644   if (path_bar->up_slider_button)
645     (* callback) (path_bar->up_slider_button, callback_data);
646
647   if (path_bar->down_slider_button)
648     (* callback) (path_bar->down_slider_button, callback_data);
649 }
650
651 static void
652 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
653 {
654   GList *list;
655   GList *down_button = NULL;
656   GList *up_button = NULL;
657   gint space_available;
658   gint space_needed;
659   gint border_width;
660   GtkTextDirection direction;
661
662   if (path_bar->ignore_click)
663     {
664       path_bar->ignore_click = FALSE;
665       return;   
666     }
667
668   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
669
670   border_width = GTK_CONTAINER (path_bar)->border_width;
671   direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
672   
673   /* We find the button at the 'down' end that we have to make
674    * visible */
675   for (list = path_bar->button_list; list; list = list->next)
676     {
677       if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
678         {
679           down_button = list;
680           break;
681         }
682     }
683   
684   /* Find the last visible button on the 'up' end
685    */
686   for (list = g_list_last (path_bar->button_list); list; list = list->prev)
687     {
688       if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
689         {
690           up_button = list;
691           break;
692         }
693     }
694
695   space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
696   if (direction == GTK_TEXT_DIR_RTL)
697     space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
698   else
699     space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
700       (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
701
702   /* We have space_available extra space that's not being used.  We
703    * need space_needed space to make the button fit.  So we walk down
704    * from the end, removing buttons until we get all the space we
705    * need. */
706   while (space_available < space_needed)
707     {
708       space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
709       up_button = up_button->prev;
710       path_bar->first_scrolled_button = up_button;
711     }
712 }
713
714 static void
715 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
716 {
717   GList *list;
718
719   if (path_bar->ignore_click)
720     {
721       path_bar->ignore_click = FALSE;
722       return;   
723     }
724
725   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
726
727   for (list = g_list_last (path_bar->button_list); list; list = list->prev)
728     {
729       if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
730         {
731           if (list->prev == path_bar->fake_root)
732             path_bar->fake_root = NULL;
733           path_bar->first_scrolled_button = list;
734           return;
735         }
736     }
737 }
738
739 static gboolean
740 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
741 {
742   gboolean retval = FALSE;
743
744   GDK_THREADS_ENTER ();
745
746   if (path_bar->timer)
747     {
748       if (path_bar->scrolling_up)
749         gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
750       else if (path_bar->scrolling_down)
751         gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
752
753       if (path_bar->need_timer) 
754         {
755           GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (path_bar));
756           guint        timeout;
757
758           g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
759
760           path_bar->need_timer = FALSE;
761
762           path_bar->timer = g_timeout_add (timeout * SCROLL_DELAY_FACTOR,
763                                            (GSourceFunc)gtk_path_bar_scroll_timeout,
764                                            path_bar);
765         }
766       else
767         retval = TRUE;
768     }
769
770   GDK_THREADS_LEAVE ();
771
772   return retval;
773 }
774
775 static void 
776 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
777 {
778   if (path_bar->timer)
779     {
780       g_source_remove (path_bar->timer);
781       path_bar->timer = 0;
782       path_bar->need_timer = FALSE;
783     }
784 }
785
786 static gboolean
787 gtk_path_bar_slider_button_press (GtkWidget      *widget, 
788                                   GdkEventButton *event,
789                                   GtkPathBar     *path_bar)
790 {
791   if (event->type != GDK_BUTTON_PRESS || event->button != 1)
792     return FALSE;
793
794   path_bar->ignore_click = FALSE;
795
796   if (widget == path_bar->up_slider_button)
797     {
798       path_bar->scrolling_down = FALSE;
799       path_bar->scrolling_up = TRUE;
800       gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
801     }
802   else if (widget == path_bar->down_slider_button)
803     {
804       path_bar->scrolling_up = FALSE;
805       path_bar->scrolling_down = TRUE;
806       gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
807     }
808
809   if (!path_bar->timer)
810     {
811       GtkSettings *settings = gtk_widget_get_settings (widget);
812       guint        timeout;
813
814       g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
815
816       path_bar->need_timer = TRUE;
817       path_bar->timer = g_timeout_add (timeout,
818                                        (GSourceFunc)gtk_path_bar_scroll_timeout,
819                                        path_bar);
820     }
821
822   return FALSE;
823 }
824
825 static gboolean
826 gtk_path_bar_slider_button_release (GtkWidget      *widget, 
827                                     GdkEventButton *event,
828                                     GtkPathBar     *path_bar)
829 {
830   if (event->type != GDK_BUTTON_RELEASE)
831     return FALSE;
832
833   path_bar->ignore_click = TRUE;
834   gtk_path_bar_stop_scrolling (path_bar);
835
836   return FALSE;
837 }
838
839 static void
840 gtk_path_bar_grab_notify (GtkWidget *widget,
841                           gboolean   was_grabbed)
842 {
843   if (!was_grabbed)
844     gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
845 }
846
847 static void
848 gtk_path_bar_state_changed (GtkWidget    *widget,
849                             GtkStateType  previous_state)
850 {
851   if (!GTK_WIDGET_IS_SENSITIVE (widget)) 
852     gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
853 }
854
855
856 /* Changes the icons wherever it is needed */
857 static void
858 reload_icons (GtkPathBar *path_bar)
859 {
860   GList *list;
861
862   if (path_bar->root_icon)
863     {
864       g_object_unref (path_bar->root_icon);
865       path_bar->root_icon = NULL;
866     }
867   if (path_bar->home_icon)
868     {
869       g_object_unref (path_bar->home_icon);
870       path_bar->home_icon = NULL;
871     }
872   if (path_bar->desktop_icon)
873     {
874       g_object_unref (path_bar->desktop_icon);
875       path_bar->desktop_icon = NULL;
876     }
877
878   for (list = path_bar->button_list; list; list = list->next)
879     {
880       ButtonData *button_data;
881       gboolean current_dir;
882
883       button_data = BUTTON_DATA (list->data);
884       if (button_data->type != NORMAL_BUTTON)
885         {
886           current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
887           gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
888         }
889     }
890   
891 }
892
893 static void
894 change_icon_theme (GtkPathBar *path_bar)
895 {
896   GtkSettings *settings;
897   gint width, height;
898
899   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
900
901   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
902     path_bar->icon_size = MAX (width, height);
903   else
904     path_bar->icon_size = FALLBACK_ICON_SIZE;
905
906   reload_icons (path_bar);
907 }
908 /* Callback used when a GtkSettings value changes */
909 static void
910 settings_notify_cb (GObject    *object,
911                     GParamSpec *pspec,
912                     GtkPathBar *path_bar)
913 {
914   const char *name;
915
916   name = g_param_spec_get_name (pspec);
917
918   if (! strcmp (name, "gtk-icon-theme-name") ||
919       ! strcmp (name, "gtk-icon-sizes"))
920     change_icon_theme (path_bar);
921 }
922
923 static void
924 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
925 {
926   GtkSettings *settings;
927
928   if (path_bar->settings_signal_id)
929     return;
930
931   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
932   path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
933
934   change_icon_theme (path_bar);
935 }
936
937 /* Public functions and their helpers */
938 static void
939 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
940 {
941   while (path_bar->button_list != NULL)
942     {
943       gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
944     }
945   path_bar->first_scrolled_button = NULL;
946   path_bar->fake_root = NULL;
947 }
948
949 static void
950 button_clicked_cb (GtkWidget *button,
951                    gpointer   data)
952 {
953   ButtonData *button_data;
954   GtkPathBar *path_bar;
955   GList *button_list;
956   gboolean child_is_hidden;
957   GtkFilePath *child_path;
958
959   button_data = BUTTON_DATA (data);
960   if (button_data->ignore_changes)
961     return;
962
963   path_bar = GTK_PATH_BAR (button->parent);
964
965   button_list = g_list_find (path_bar->button_list, button_data);
966   g_assert (button_list != NULL);
967
968   g_signal_handlers_block_by_func (button,
969                                    G_CALLBACK (button_clicked_cb), data);
970   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
971   g_signal_handlers_unblock_by_func (button,
972                                      G_CALLBACK (button_clicked_cb), data);
973
974   if (button_list->prev)
975     {
976       ButtonData *child_data;
977
978       child_data = BUTTON_DATA (button_list->prev->data);
979       child_path = child_data->path;
980       child_is_hidden = child_data->file_is_hidden;
981     }
982   else
983     {
984       child_path = NULL;
985       child_is_hidden = FALSE;
986     }
987
988   g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0,
989                  button_data->path, child_path, child_is_hidden);
990 }
991
992 struct SetButtonImageData
993 {
994   GtkPathBar *path_bar;
995   ButtonData *button_data;
996 };
997
998 static void
999 set_button_image_get_info_cb (GtkFileSystemHandle *handle,
1000                               const GtkFileInfo   *info,
1001                               const GError        *error,
1002                               gpointer             user_data)
1003 {
1004   gboolean cancelled = handle->cancelled;
1005   GdkPixbuf *pixbuf;
1006   struct SetButtonImageData *data = user_data;
1007
1008   if (handle != data->button_data->handle)
1009     goto out;
1010
1011   data->button_data->handle = NULL;
1012
1013   if (cancelled || error)
1014     goto out;
1015
1016   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->path_bar),
1017                                       data->path_bar->icon_size, NULL);
1018   gtk_image_set_from_pixbuf (GTK_IMAGE (data->button_data->image), pixbuf);
1019
1020   switch (data->button_data->type)
1021     {
1022       case HOME_BUTTON:
1023         if (data->path_bar->home_icon)
1024           g_object_unref (pixbuf);
1025         else
1026           data->path_bar->home_icon = pixbuf;
1027         break;
1028
1029       case DESKTOP_BUTTON:
1030         if (data->path_bar->desktop_icon)
1031           g_object_unref (pixbuf);
1032         else
1033           data->path_bar->desktop_icon = pixbuf;
1034         break;
1035
1036       default:
1037         break;
1038     };
1039
1040 out:
1041   g_free (data);
1042   g_object_unref (handle);
1043 }
1044
1045 static void
1046 set_button_image (GtkPathBar *path_bar,
1047                   ButtonData *button_data)
1048 {
1049   GtkFileSystemVolume *volume;
1050   struct SetButtonImageData *data;
1051
1052   switch (button_data->type)
1053     {
1054     case ROOT_BUTTON:
1055
1056       if (path_bar->root_icon != NULL)
1057         {
1058           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1059           break;
1060         }
1061       
1062       volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
1063       if (volume == NULL)
1064         return;
1065
1066       path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
1067                                                                 volume,
1068                                                                 GTK_WIDGET (path_bar),
1069                                                                 path_bar->icon_size,
1070                                                                 NULL);
1071       gtk_file_system_volume_free (path_bar->file_system, volume);
1072
1073       gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1074       break;
1075
1076     case HOME_BUTTON:
1077       if (path_bar->home_icon != NULL)
1078         {
1079           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->home_icon);
1080           break;
1081         }
1082
1083       data = g_new0 (struct SetButtonImageData, 1);
1084       data->path_bar = path_bar;
1085       data->button_data = button_data;
1086
1087       if (button_data->handle)
1088         gtk_file_system_cancel_operation (button_data->handle);
1089
1090       button_data->handle =
1091         gtk_file_system_get_info (path_bar->file_system,
1092                                   path_bar->home_path,
1093                                   GTK_FILE_INFO_ICON,
1094                                   set_button_image_get_info_cb,
1095                                   data);
1096       break;
1097
1098     case DESKTOP_BUTTON:
1099       if (path_bar->desktop_icon != NULL)
1100         {
1101           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->desktop_icon);
1102           break;
1103         }
1104
1105       data = g_new0 (struct SetButtonImageData, 1);
1106       data->path_bar = path_bar;
1107       data->button_data = button_data;
1108
1109       if (button_data->handle)
1110         gtk_file_system_cancel_operation (button_data->handle);
1111
1112       button_data->handle =
1113         gtk_file_system_get_info (path_bar->file_system,
1114                                   path_bar->desktop_path,
1115                                   GTK_FILE_INFO_ICON,
1116                                   set_button_image_get_info_cb,
1117                                   data);
1118       break;
1119     default:
1120       break;
1121     }
1122 }
1123
1124 static void
1125 button_data_free (ButtonData *button_data)
1126 {
1127   if (button_data->handle)
1128     gtk_file_system_cancel_operation (button_data->handle);
1129
1130   gtk_file_path_free (button_data->path);
1131   g_free (button_data->dir_name);
1132   g_free (button_data);
1133 }
1134
1135 static const char *
1136 get_dir_name (ButtonData *button_data)
1137 {
1138   return button_data->dir_name;
1139 }
1140
1141 /* We always want to request the same size for the label, whether
1142  * or not the contents are bold
1143  */
1144 static void
1145 label_size_request_cb (GtkWidget      *widget,
1146                        GtkRequisition *requisition,
1147                        ButtonData     *button_data)
1148 {
1149   const gchar *dir_name = get_dir_name (button_data);
1150   PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1151   gint bold_width, bold_height;
1152   gchar *markup;
1153
1154   pango_layout_get_pixel_size (layout, &requisition->width, &requisition->height);
1155   
1156   markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1157   pango_layout_set_markup (layout, markup, -1);
1158   g_free (markup);
1159
1160   pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1161   requisition->width = MAX (requisition->width, bold_width);
1162   requisition->height = MAX (requisition->height, bold_height);
1163   
1164   g_object_unref (layout);
1165 }
1166
1167 static void
1168 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1169                                        ButtonData *button_data,
1170                                        gboolean    current_dir)
1171 {
1172   const gchar *dir_name = get_dir_name (button_data);
1173
1174   if (button_data->label != NULL)
1175     {
1176       if (current_dir)
1177         {
1178           char *markup;
1179
1180           markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1181           gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1182           g_free (markup);
1183         }
1184       else
1185         {
1186           gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1187         }
1188     }
1189
1190   if (button_data->image != NULL)
1191     {
1192       set_button_image (path_bar, button_data);
1193     }
1194
1195   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1196     {
1197       button_data->ignore_changes = TRUE;
1198       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1199       button_data->ignore_changes = FALSE;
1200     }
1201 }
1202
1203 static ButtonType
1204 find_button_type (GtkPathBar  *path_bar,
1205                   GtkFilePath *path)
1206 {
1207   if (path_bar->root_path != NULL &&
1208       ! gtk_file_path_compare (path, path_bar->root_path))
1209     return ROOT_BUTTON;
1210   if (path_bar->home_path != NULL &&
1211       ! gtk_file_path_compare (path, path_bar->home_path))
1212     return HOME_BUTTON;
1213   if (path_bar->desktop_path != NULL &&
1214       ! gtk_file_path_compare (path, path_bar->desktop_path))
1215     return DESKTOP_BUTTON;
1216
1217  return NORMAL_BUTTON;
1218 }
1219
1220 static void
1221 button_drag_data_get_cb (GtkWidget          *widget,
1222                          GdkDragContext     *context,
1223                          GtkSelectionData   *selection_data,
1224                          guint               info,
1225                          guint               time_,
1226                          gpointer            data)
1227 {
1228   ButtonData *button_data;
1229   GtkPathBar *path_bar;
1230   char *uri;
1231   char *uri_list;
1232
1233   button_data = data;
1234   path_bar = GTK_PATH_BAR (widget->parent); /* the button's parent *is* the path bar */
1235
1236   uri = gtk_file_system_path_to_uri (path_bar->file_system, button_data->path);
1237   uri_list = g_strconcat (uri, "\r\n", NULL);
1238   g_free (uri);
1239
1240   gtk_selection_data_set (selection_data,
1241                           selection_data->target,
1242                           8,
1243                           (guchar *)uri_list,
1244                           strlen (uri_list));
1245   g_free (uri_list);
1246 }
1247
1248 static ButtonData *
1249 make_directory_button (GtkPathBar  *path_bar,
1250                        const char  *dir_name,
1251                        GtkFilePath *path,
1252                        gboolean     current_dir,
1253                        gboolean     file_is_hidden)
1254 {
1255   const GtkTargetEntry targets[] = {
1256     { "text/uri-list", 0, 0 }
1257   };
1258
1259   GtkWidget *child = NULL;
1260   GtkWidget *label_alignment = NULL;
1261   ButtonData *button_data;
1262
1263   file_is_hidden = !! file_is_hidden;
1264   /* Is it a special button? */
1265   button_data = g_new0 (ButtonData, 1);
1266
1267   button_data->type = find_button_type (path_bar, path);
1268   button_data->button = gtk_toggle_button_new ();
1269   gtk_button_set_focus_on_click (GTK_BUTTON (button_data->button), FALSE);
1270
1271   switch (button_data->type)
1272     {
1273     case ROOT_BUTTON:
1274       button_data->image = gtk_image_new ();
1275       child = button_data->image;
1276       button_data->label = NULL;
1277       break;
1278     case HOME_BUTTON:
1279     case DESKTOP_BUTTON:
1280       button_data->image = gtk_image_new ();
1281       button_data->label = gtk_label_new (NULL);
1282       label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1283       gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1284       child = gtk_hbox_new (FALSE, 2);
1285       gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1286       gtk_box_pack_start (GTK_BOX (child), label_alignment, FALSE, FALSE, 0);
1287       break;
1288     case NORMAL_BUTTON:
1289     default:
1290       button_data->label = gtk_label_new (NULL);
1291       label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1292       gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1293       child = label_alignment;
1294       button_data->image = NULL;
1295     }
1296
1297   /* label_alignment is created because we can't override size-request
1298    * on label itself and still have the contents of the label centered
1299    * properly in the label's requisition
1300    */
1301   if (label_alignment)
1302     g_signal_connect (label_alignment, "size-request",
1303                       G_CALLBACK (label_size_request_cb), button_data);
1304
1305   button_data->dir_name = g_strdup (dir_name);
1306   button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
1307   button_data->file_is_hidden = file_is_hidden;
1308                           
1309   gtk_container_add (GTK_CONTAINER (button_data->button), child);
1310   gtk_widget_show_all (button_data->button);
1311
1312   gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1313
1314   g_signal_connect (button_data->button, "clicked",
1315                     G_CALLBACK (button_clicked_cb),
1316                     button_data);
1317   g_object_weak_ref (G_OBJECT (button_data->button),
1318                      (GWeakNotify) button_data_free, button_data);
1319
1320   gtk_drag_source_set (button_data->button,
1321                        GDK_BUTTON1_MASK,
1322                        targets,
1323                        G_N_ELEMENTS (targets),
1324                        GDK_ACTION_COPY);
1325   g_signal_connect (button_data->button, "drag-data-get",
1326                     G_CALLBACK (button_drag_data_get_cb), button_data);
1327
1328   return button_data;
1329 }
1330
1331 static gboolean
1332 gtk_path_bar_check_parent_path (GtkPathBar         *path_bar,
1333                                 const GtkFilePath  *file_path,
1334                                 GtkFileSystem      *file_system)
1335 {
1336   GList *list;
1337   GList *current_path = NULL;
1338   gboolean need_new_fake_root = FALSE;
1339
1340   for (list = path_bar->button_list; list; list = list->next)
1341     {
1342       ButtonData *button_data;
1343
1344       button_data = list->data;
1345       if (! gtk_file_path_compare (file_path, button_data->path))
1346         {
1347           current_path = list;
1348           break;
1349         }
1350       if (list == path_bar->fake_root)
1351         need_new_fake_root = TRUE;
1352     }
1353
1354   if (current_path)
1355     {
1356       if (need_new_fake_root)
1357         {
1358           path_bar->fake_root = NULL;
1359           for (list = current_path; list; list = list->next)
1360             {
1361               ButtonData *button_data;
1362
1363               button_data = list->data;
1364               if (BUTTON_IS_FAKE_ROOT (button_data))
1365                 {
1366                   path_bar->fake_root = list;
1367                   break;
1368                 }
1369             }
1370         }
1371
1372       for (list = path_bar->button_list; list; list = list->next)
1373         {
1374           gtk_path_bar_update_button_appearance (path_bar,
1375                                                  BUTTON_DATA (list->data),
1376                                                  (list == current_path) ? TRUE : FALSE);
1377         }
1378
1379       if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1380         {
1381           path_bar->first_scrolled_button = current_path;
1382           gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1383         }
1384
1385       return TRUE;
1386     }
1387   return FALSE;
1388 }
1389
1390
1391 struct SetPathInfo
1392 {
1393   GtkFilePath *path;
1394   GtkFilePath *parent_path;
1395   GtkPathBar *path_bar;
1396   GList *new_buttons;
1397   GList *fake_root;
1398   gboolean first_directory;
1399 };
1400
1401 static void
1402 gtk_path_bar_set_path_finish (struct SetPathInfo *info,
1403                               gboolean result)
1404 {
1405   if (result)
1406     {
1407       GList *l;
1408
1409       gtk_path_bar_clear_buttons (info->path_bar);
1410       info->path_bar->button_list = g_list_reverse (info->new_buttons);
1411       info->path_bar->fake_root = info->fake_root;
1412
1413       for (l = info->path_bar->button_list; l; l = l->next)
1414         {
1415           GtkWidget *button = BUTTON_DATA (l->data)->button;
1416           gtk_container_add (GTK_CONTAINER (info->path_bar), button);
1417         }
1418     }
1419   else
1420     {
1421       GList *l;
1422
1423       for (l = info->new_buttons; l; l = l->next)
1424         {
1425           ButtonData *button_data;
1426
1427           button_data = BUTTON_DATA (l->data);
1428           gtk_widget_destroy (button_data->button);
1429         }
1430
1431       g_list_free (info->new_buttons);
1432     }
1433
1434   if (info->path)
1435     gtk_file_path_free (info->path);
1436   if (info->parent_path)
1437     gtk_file_path_free (info->parent_path);
1438   g_free (info);
1439 }
1440
1441 static void
1442 gtk_path_bar_get_info_callback (GtkFileSystemHandle *handle,
1443                                 const GtkFileInfo   *file_info,
1444                                 const GError        *error,
1445                                 gpointer             data)
1446 {
1447   gboolean cancelled = handle->cancelled;
1448   struct SetPathInfo *path_info = data;
1449   ButtonData *button_data;
1450   const gchar *display_name;
1451   gboolean is_hidden;
1452   gboolean valid;
1453
1454   if (handle != path_info->path_bar->set_path_handle)
1455     {
1456       gtk_path_bar_set_path_finish (path_info, FALSE);
1457       g_object_unref (handle);
1458       return;
1459     }
1460
1461   g_object_unref (handle);
1462   path_info->path_bar->set_path_handle = NULL;
1463
1464   if (cancelled || !file_info)
1465     {
1466       gtk_path_bar_set_path_finish (path_info, FALSE);
1467       return;
1468     }
1469
1470   display_name = gtk_file_info_get_display_name (file_info);
1471   is_hidden = gtk_file_info_get_is_hidden (file_info);
1472
1473   gtk_widget_push_composite_child ();
1474   button_data = make_directory_button (path_info->path_bar, display_name,
1475                                        path_info->path,
1476                                        path_info->first_directory, is_hidden);
1477   gtk_widget_pop_composite_child ();
1478   gtk_file_path_free (path_info->path);
1479
1480   path_info->new_buttons = g_list_prepend (path_info->new_buttons, button_data);
1481
1482   if (BUTTON_IS_FAKE_ROOT (button_data))
1483     path_info->fake_root = path_info->new_buttons;
1484
1485   path_info->path = path_info->parent_path;
1486   path_info->first_directory = FALSE;
1487
1488   if (!path_info->path)
1489     {
1490       gtk_path_bar_set_path_finish (path_info, TRUE);
1491       return;
1492     }
1493
1494   valid = gtk_file_system_get_parent (path_info->path_bar->file_system,
1495                                       path_info->path,
1496                                       &path_info->parent_path,
1497                                       NULL);
1498   if (!valid)
1499     {
1500       gtk_path_bar_set_path_finish (path_info, FALSE);
1501       return;
1502     }
1503
1504   path_info->path_bar->set_path_handle =
1505     gtk_file_system_get_info (handle->file_system,
1506                               path_info->path,
1507                               GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1508                               gtk_path_bar_get_info_callback,
1509                               path_info);
1510 }
1511
1512 gboolean
1513 _gtk_path_bar_set_path (GtkPathBar         *path_bar,
1514                         const GtkFilePath  *file_path,
1515                         const gboolean      keep_trail,     
1516                         GError            **error)
1517 {
1518   struct SetPathInfo *info;
1519   gboolean result;
1520
1521   g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
1522   g_return_val_if_fail (file_path != NULL, FALSE);
1523
1524   result = TRUE;
1525
1526   /* Check whether the new path is already present in the pathbar as buttons.
1527    * This could be a parent directory or a previous selected subdirectory.
1528    */
1529   if (keep_trail &&
1530       gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
1531     return TRUE;
1532
1533   info = g_new0 (struct SetPathInfo, 1);
1534   info->path = gtk_file_path_copy (file_path);
1535   info->path_bar = path_bar;
1536   info->first_directory = TRUE;
1537
1538   result = gtk_file_system_get_parent (path_bar->file_system,
1539                                        info->path, &info->parent_path, error);
1540   if (!result)
1541     {
1542       gtk_file_path_free (info->path);
1543       g_free (info);
1544       return result;
1545     }
1546
1547   if (path_bar->set_path_handle)
1548     gtk_file_system_cancel_operation (path_bar->set_path_handle);
1549
1550   path_bar->set_path_handle =
1551     gtk_file_system_get_info (path_bar->file_system,
1552                               info->path,
1553                               GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1554                               gtk_path_bar_get_info_callback,
1555                               info);
1556
1557   return TRUE;
1558 }
1559
1560 /* FIXME: This should be a construct-only property */
1561 void
1562 _gtk_path_bar_set_file_system (GtkPathBar    *path_bar,
1563                                GtkFileSystem *file_system)
1564 {
1565   const char *home;
1566   char *desktop;
1567
1568   g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1569
1570   g_assert (path_bar->file_system == NULL);
1571
1572   path_bar->file_system = g_object_ref (file_system);
1573
1574   home = g_get_home_dir ();
1575   if (home != NULL)
1576     {
1577       path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1578       /* FIXME: Need file system backend specific way of getting the
1579        * Desktop path.
1580        */
1581       desktop = g_build_filename (home, "Desktop", NULL);
1582       path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1583       g_free (desktop);
1584     }
1585   else
1586     {
1587       path_bar->home_path = NULL;
1588       path_bar->desktop_path = NULL;
1589     }
1590   path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1591 }
1592
1593 /**
1594  * _gtk_path_bar_up:
1595  * @path_bar: a #GtkPathBar
1596  * 
1597  * If the selected button in the pathbar is not the furthest button "up" (in the
1598  * root direction), act as if the user clicked on the next button up.
1599  **/
1600 void
1601 _gtk_path_bar_up (GtkPathBar *path_bar)
1602 {
1603   GList *l;
1604
1605   for (l = path_bar->button_list; l; l = l->next)
1606     {
1607       GtkWidget *button = BUTTON_DATA (l->data)->button;
1608       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1609         {
1610           if (l->next)
1611             {
1612               GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1613               button_clicked_cb (next_button, l->next->data);
1614             }
1615           break;
1616         }
1617     }
1618 }
1619
1620 /**
1621  * _gtk_path_bar_down:
1622  * @path_bar: a #GtkPathBar
1623  * 
1624  * If the selected button in the pathbar is not the furthest button "down" (in the
1625  * leaf direction), act as if the user clicked on the next button down.
1626  **/
1627 void
1628 _gtk_path_bar_down (GtkPathBar *path_bar)
1629 {
1630   GList *l;
1631
1632   for (l = path_bar->button_list; l; l = l->next)
1633     {
1634       GtkWidget *button = BUTTON_DATA (l->data)->button;
1635       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1636         {
1637           if (l->prev)
1638             {
1639               GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1640               button_clicked_cb (prev_button, l->prev->data);
1641             }
1642           break;
1643         }
1644     }
1645 }