]> Pileus Git - ~andy/gtk/blob - gtk/gtkpathbar.c
pathbar: Fix scrolling
[~andy/gtk] / gtk / gtkpathbar.c
1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* gtkpathbar.c
3  * Copyright (C) 2004  Red Hat, Inc.,  Jonathan Blandford <jrb@gnome.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20
21 #include "gtkpathbar.h"
22
23 #include <string.h>
24
25 #include "gtktogglebutton.h"
26 #include "gtkarrow.h"
27 #include "gtkdnd.h"
28 #include "gtkimage.h"
29 #include "gtkintl.h"
30 #include "gtkicontheme.h"
31 #include "gtkiconfactory.h"
32 #include "gtklabel.h"
33 #include "gtkbox.h"
34 #include "gtkmain.h"
35 #include "gtkmarshalers.h"
36
37
38 enum {
39   PATH_CLICKED,
40   LAST_SIGNAL
41 };
42
43 typedef enum {
44   NORMAL_BUTTON,
45   ROOT_BUTTON,
46   HOME_BUTTON,
47   DESKTOP_BUTTON
48 } ButtonType;
49
50 #define BUTTON_DATA(x) ((ButtonData *)(x))
51
52 #define SCROLL_DELAY_FACTOR 5
53
54 static guint path_bar_signals [LAST_SIGNAL] = { 0 };
55
56 /* Icon size for if we can't get it from the theme */
57 #define FALLBACK_ICON_SIZE 16
58
59 typedef struct _ButtonData ButtonData;
60
61 struct _ButtonData
62 {
63   GtkWidget *button;
64   ButtonType type;
65   char *dir_name;
66   GFile *file;
67   GtkWidget *image;
68   GtkWidget *label;
69   GCancellable *cancellable;
70   guint ignore_changes : 1;
71   guint file_is_hidden : 1;
72 };
73 /* This macro is used to check if a button can be used as a fake root.
74  * All buttons in front of a fake root are automatically hidden when in a
75  * directory below a fake root and replaced with the "<" arrow button.
76  */
77 #define BUTTON_IS_FAKE_ROOT(button) ((button)->type == HOME_BUTTON)
78
79 G_DEFINE_TYPE (GtkPathBar, gtk_path_bar, GTK_TYPE_CONTAINER)
80
81 static void gtk_path_bar_finalize                 (GObject          *object);
82 static void gtk_path_bar_dispose                  (GObject          *object);
83 static void gtk_path_bar_realize                  (GtkWidget        *widget);
84 static void gtk_path_bar_unrealize                (GtkWidget        *widget);
85 static void gtk_path_bar_get_preferred_width      (GtkWidget        *widget,
86                                                    gint             *minimum,
87                                                    gint             *natural);
88 static void gtk_path_bar_get_preferred_height     (GtkWidget        *widget,
89                                                    gint             *minimum,
90                                                    gint             *natural);
91 static void gtk_path_bar_map                      (GtkWidget        *widget);
92 static void gtk_path_bar_unmap                    (GtkWidget        *widget);
93 static void gtk_path_bar_size_allocate            (GtkWidget        *widget,
94                                                    GtkAllocation    *allocation);
95 static void gtk_path_bar_add                      (GtkContainer     *container,
96                                                    GtkWidget        *widget);
97 static void gtk_path_bar_remove                   (GtkContainer     *container,
98                                                    GtkWidget        *widget);
99 static void gtk_path_bar_forall                   (GtkContainer     *container,
100                                                    gboolean          include_internals,
101                                                    GtkCallback       callback,
102                                                    gpointer          callback_data);
103 static GtkWidgetPath *gtk_path_bar_get_path_for_child (GtkContainer *container,
104                                                        GtkWidget    *child);
105 static gboolean gtk_path_bar_scroll               (GtkWidget        *widget,
106                                                    GdkEventScroll   *event);
107 static void gtk_path_bar_scroll_up                (GtkPathBar       *path_bar);
108 static void gtk_path_bar_scroll_down              (GtkPathBar       *path_bar);
109 static void gtk_path_bar_stop_scrolling           (GtkPathBar       *path_bar);
110 static gboolean gtk_path_bar_slider_up_defocus    (GtkWidget        *widget,
111                                                    GdkEventButton   *event,
112                                                    GtkPathBar       *path_bar);
113 static gboolean gtk_path_bar_slider_down_defocus  (GtkWidget        *widget,
114                                                    GdkEventButton   *event,
115                                                    GtkPathBar       *path_bar);
116 static gboolean gtk_path_bar_slider_button_press  (GtkWidget        *widget,
117                                                    GdkEventButton   *event,
118                                                    GtkPathBar       *path_bar);
119 static gboolean gtk_path_bar_slider_button_release(GtkWidget        *widget,
120                                                    GdkEventButton   *event,
121                                                    GtkPathBar       *path_bar);
122 static void gtk_path_bar_grab_notify              (GtkWidget        *widget,
123                                                    gboolean          was_grabbed);
124 static void gtk_path_bar_state_changed            (GtkWidget        *widget,
125                                                    GtkStateType      previous_state);
126 static void gtk_path_bar_style_updated            (GtkWidget        *widget);
127 static void gtk_path_bar_screen_changed           (GtkWidget        *widget,
128                                                    GdkScreen        *previous_screen);
129 static void gtk_path_bar_check_icon_theme         (GtkPathBar       *path_bar);
130 static void gtk_path_bar_update_button_appearance (GtkPathBar       *path_bar,
131                                                    ButtonData       *button_data,
132                                                    gboolean          current_dir);
133
134 static void
135 on_slider_unmap (GtkWidget  *widget,
136                  GtkPathBar *path_bar)
137 {
138   if (path_bar->timer &&
139       ((widget == path_bar->up_slider_button && path_bar->scrolling_up) ||
140        (widget == path_bar->down_slider_button && path_bar->scrolling_down)))
141     gtk_path_bar_stop_scrolling (path_bar);
142 }
143
144 static GtkWidget *
145 get_slider_button (GtkPathBar  *path_bar,
146                    GtkArrowType arrow_type)
147 {
148   GtkWidget *button;
149   AtkObject *atk_obj;
150
151   gtk_widget_push_composite_child ();
152
153   button = gtk_button_new ();
154   atk_obj = gtk_widget_get_accessible (button);
155   if (arrow_type == GTK_ARROW_LEFT)
156     atk_object_set_name (atk_obj, _("Up Path"));
157   else
158     atk_object_set_name (atk_obj, _("Down Path"));
159
160   gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
161   gtk_widget_add_events (button, GDK_SCROLL_MASK);
162   gtk_container_add (GTK_CONTAINER (button),
163                      gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
164   gtk_container_add (GTK_CONTAINER (path_bar), button);
165   gtk_widget_show_all (button);
166
167   g_signal_connect (G_OBJECT (button), "unmap",
168                     G_CALLBACK (on_slider_unmap), path_bar);
169
170   gtk_widget_pop_composite_child ();
171
172   return button;
173 }
174
175 static void
176 gtk_path_bar_init (GtkPathBar *path_bar)
177 {
178   GtkStyleContext *context;
179
180   gtk_widget_set_has_window (GTK_WIDGET (path_bar), FALSE);
181   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
182
183   path_bar->get_info_cancellable = NULL;
184
185   path_bar->spacing = 0;
186   path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
187   path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
188   path_bar->icon_size = FALLBACK_ICON_SIZE;
189   
190   g_signal_connect_swapped (path_bar->up_slider_button, "clicked",
191                             G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
192   g_signal_connect_swapped (path_bar->down_slider_button, "clicked",
193                             G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
194
195   g_signal_connect (path_bar->up_slider_button, "focus-out-event",
196                     G_CALLBACK (gtk_path_bar_slider_up_defocus), path_bar);
197   g_signal_connect (path_bar->down_slider_button, "focus-out-event",
198                     G_CALLBACK (gtk_path_bar_slider_down_defocus), path_bar);
199
200   g_signal_connect (path_bar->up_slider_button, "button-press-event",
201                     G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
202   g_signal_connect (path_bar->up_slider_button, "button-release-event",
203                     G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
204   g_signal_connect (path_bar->down_slider_button, "button-press-event",
205                     G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
206   g_signal_connect (path_bar->down_slider_button, "button-release-event",
207                     G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
208
209   context = gtk_widget_get_style_context (GTK_WIDGET (path_bar));
210   gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
211 }
212
213 static void
214 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
215 {
216   GObjectClass *gobject_class;
217   GtkWidgetClass *widget_class;
218   GtkContainerClass *container_class;
219
220   gobject_class = (GObjectClass *) path_bar_class;
221   widget_class = (GtkWidgetClass *) path_bar_class;
222   container_class = (GtkContainerClass *) path_bar_class;
223
224   gobject_class->finalize = gtk_path_bar_finalize;
225   gobject_class->dispose = gtk_path_bar_dispose;
226
227   widget_class->get_preferred_width = gtk_path_bar_get_preferred_width;
228   widget_class->get_preferred_height = gtk_path_bar_get_preferred_height;
229   widget_class->realize = gtk_path_bar_realize;
230   widget_class->unrealize = gtk_path_bar_unrealize;
231   widget_class->map = gtk_path_bar_map;
232   widget_class->unmap = gtk_path_bar_unmap;
233   widget_class->size_allocate = gtk_path_bar_size_allocate;
234   widget_class->style_updated = gtk_path_bar_style_updated;
235   widget_class->screen_changed = gtk_path_bar_screen_changed;
236   widget_class->grab_notify = gtk_path_bar_grab_notify;
237   widget_class->state_changed = gtk_path_bar_state_changed;
238   widget_class->scroll_event = gtk_path_bar_scroll;
239
240   container_class->add = gtk_path_bar_add;
241   container_class->forall = gtk_path_bar_forall;
242   container_class->remove = gtk_path_bar_remove;
243   container_class->get_path_for_child = gtk_path_bar_get_path_for_child;
244   gtk_container_class_handle_border_width (container_class);
245   /* FIXME: */
246   /*  container_class->child_type = gtk_path_bar_child_type;*/
247
248   path_bar_signals [PATH_CLICKED] =
249     g_signal_new (I_("path-clicked"),
250                   G_OBJECT_CLASS_TYPE (gobject_class),
251                   G_SIGNAL_RUN_FIRST,
252                   G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
253                   NULL, NULL,
254                   _gtk_marshal_VOID__POINTER_POINTER_BOOLEAN,
255                   G_TYPE_NONE, 3,
256                   G_TYPE_POINTER,
257                   G_TYPE_POINTER,
258                   G_TYPE_BOOLEAN);
259 }
260
261
262 static void
263 gtk_path_bar_finalize (GObject *object)
264 {
265   GtkPathBar *path_bar;
266
267   path_bar = GTK_PATH_BAR (object);
268
269   gtk_path_bar_stop_scrolling (path_bar);
270
271   g_list_free (path_bar->button_list);
272   if (path_bar->root_file)
273     g_object_unref (path_bar->root_file);
274   if (path_bar->home_file)
275     g_object_unref (path_bar->home_file);
276   if (path_bar->desktop_file)
277     g_object_unref (path_bar->desktop_file);
278
279   if (path_bar->root_icon)
280     g_object_unref (path_bar->root_icon);
281   if (path_bar->home_icon)
282     g_object_unref (path_bar->home_icon);
283   if (path_bar->desktop_icon)
284     g_object_unref (path_bar->desktop_icon);
285
286   if (path_bar->file_system)
287     g_object_unref (path_bar->file_system);
288
289   G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
290 }
291
292 /* Removes the settings signal handler.  It's safe to call multiple times */
293 static void
294 remove_settings_signal (GtkPathBar *path_bar,
295                         GdkScreen  *screen)
296 {
297   if (path_bar->settings_signal_id)
298     {
299       GtkSettings *settings;
300
301       settings = gtk_settings_get_for_screen (screen);
302       g_signal_handler_disconnect (settings,
303                                    path_bar->settings_signal_id);
304       path_bar->settings_signal_id = 0;
305     }
306 }
307
308 static void
309 gtk_path_bar_dispose (GObject *object)
310 {
311   GtkPathBar *path_bar = GTK_PATH_BAR (object);
312
313   remove_settings_signal (path_bar, gtk_widget_get_screen (GTK_WIDGET (object)));
314
315   if (path_bar->get_info_cancellable)
316     g_cancellable_cancel (path_bar->get_info_cancellable);
317   path_bar->get_info_cancellable = NULL;
318
319   G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
320 }
321
322 /* Size requisition:
323  * 
324  * Ideally, our size is determined by another widget, and we are just filling
325  * available space.
326  */
327 static void
328 gtk_path_bar_get_preferred_width (GtkWidget *widget,
329                                   gint      *minimum,
330                                   gint      *natural)
331 {
332   ButtonData *button_data;
333   GtkPathBar *path_bar;
334   GList *list;
335   gint child_height;
336   gint height;
337   gint child_min, child_nat;
338
339   path_bar = GTK_PATH_BAR (widget);
340
341   *minimum = *natural = 0;
342   height = 0;
343
344   for (list = path_bar->button_list; list; list = list->next)
345     {
346       button_data = BUTTON_DATA (list->data);
347       gtk_widget_get_preferred_width (button_data->button, &child_min, &child_nat);
348       gtk_widget_get_preferred_height (button_data->button, &child_height, NULL);
349       height = MAX (height, child_height);
350
351       if (button_data->type == NORMAL_BUTTON)
352         {
353           /* Use 2*Height as button width because of ellipsized label.  */
354           child_min = MAX (child_min, child_height * 2);
355           child_nat = MAX (child_min, child_height * 2);
356         }
357
358       *minimum = MAX (*minimum, child_min);
359       *natural = MAX (*natural, child_nat);
360     }
361
362   /* Add space for slider, if we have more than one path */
363   /* Theoretically, the slider could be bigger than the other button.  But we're
364    * not going to worry about that now.
365    */
366   path_bar->slider_width = MIN (height * 2 / 3 + 5, height);
367   if (path_bar->button_list && path_bar->button_list->next != NULL)
368     {
369       *minimum += (path_bar->spacing + path_bar->slider_width) * 2;
370       *natural += (path_bar->spacing + path_bar->slider_width) * 2;
371     }
372 }
373
374 static void
375 gtk_path_bar_get_preferred_height (GtkWidget *widget,
376                                    gint      *minimum,
377                                    gint      *natural)
378 {
379   ButtonData *button_data;
380   GtkPathBar *path_bar;
381   GList *list;
382   gint child_min, child_nat;
383
384   path_bar = GTK_PATH_BAR (widget);
385
386   *minimum = *natural = 0;
387
388   for (list = path_bar->button_list; list; list = list->next)
389     {
390       button_data = BUTTON_DATA (list->data);
391       gtk_widget_get_preferred_height (button_data->button, &child_min, &child_nat);
392
393       *minimum = MAX (*minimum, child_min);
394       *natural = MAX (*natural, child_nat);
395     }
396 }
397
398 static void
399 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
400 {
401   if (path_bar->button_list)
402     {
403       GtkWidget *button;
404
405       button = BUTTON_DATA (path_bar->button_list->data)->button;
406       if (gtk_widget_get_child_visible (button))
407         {
408           gtk_path_bar_stop_scrolling (path_bar);
409           gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
410         }
411       else
412         gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
413
414       button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
415       if (gtk_widget_get_child_visible (button))
416         {
417           gtk_path_bar_stop_scrolling (path_bar);
418           gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
419         }
420       else
421         gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
422     }
423 }
424
425 static void
426 gtk_path_bar_map (GtkWidget *widget)
427 {
428   gdk_window_show (GTK_PATH_BAR (widget)->event_window);
429
430   GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->map (widget);
431 }
432
433 static void
434 gtk_path_bar_unmap (GtkWidget *widget)
435 {
436   gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
437   gdk_window_hide (GTK_PATH_BAR (widget)->event_window);
438
439   GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unmap (widget);
440 }
441
442 static void
443 gtk_path_bar_realize (GtkWidget *widget)
444 {
445   GtkPathBar *path_bar;
446   GtkAllocation allocation;
447   GdkWindow *window;
448   GdkWindowAttr attributes;
449   gint attributes_mask;
450
451   gtk_widget_set_realized (widget, TRUE);
452
453   path_bar = GTK_PATH_BAR (widget);
454   window = gtk_widget_get_parent_window (widget);
455   gtk_widget_set_window (widget, window);
456   g_object_ref (window);
457
458   gtk_widget_get_allocation (widget, &allocation);
459
460   attributes.window_type = GDK_WINDOW_CHILD;
461   attributes.x = allocation.x;
462   attributes.y = allocation.y;
463   attributes.width = allocation.width;
464   attributes.height = allocation.height;
465   attributes.wclass = GDK_INPUT_ONLY;
466   attributes.event_mask = gtk_widget_get_events (widget);
467   attributes.event_mask |= GDK_SCROLL_MASK;
468   attributes_mask = GDK_WA_X | GDK_WA_Y;
469
470   path_bar->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
471                                            &attributes, attributes_mask);
472   gdk_window_set_user_data (path_bar->event_window, widget);
473 }
474
475 static void
476 gtk_path_bar_unrealize (GtkWidget *widget)
477 {
478   GtkPathBar *path_bar;
479
480   path_bar = GTK_PATH_BAR (widget);
481
482   gdk_window_set_user_data (path_bar->event_window, NULL);
483   gdk_window_destroy (path_bar->event_window);
484   path_bar->event_window = NULL;
485
486   GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unrealize (widget);
487 }
488
489 static void
490 child_ordering_changed (GtkPathBar *path_bar)
491 {
492   GList *l;
493
494   if (path_bar->up_slider_button)
495     gtk_widget_reset_style (path_bar->up_slider_button);
496   if (path_bar->down_slider_button)
497     gtk_widget_reset_style (path_bar->down_slider_button);
498
499   for (l = path_bar->button_list; l; l = l->next)
500     {
501       ButtonData *data = l->data;
502
503       gtk_widget_reset_style (data->button);
504     }
505 }
506
507 /* This is a tad complicated
508  */
509 static void
510 gtk_path_bar_size_allocate (GtkWidget     *widget,
511                             GtkAllocation *allocation)
512 {
513   GtkWidget *child;
514   GtkPathBar *path_bar = GTK_PATH_BAR (widget);
515   GtkTextDirection direction;
516   GtkAllocation child_allocation;
517   GList *list, *first_button;
518   gint width;
519   gint allocation_width;
520   gboolean need_sliders = FALSE;
521   gint up_slider_offset = 0;
522   GtkRequisition child_requisition;
523
524   gtk_widget_set_allocation (widget, allocation);
525
526   if (gtk_widget_get_realized (widget))
527     gdk_window_move_resize (path_bar->event_window,
528                             allocation->x, allocation->y,
529                             allocation->width, allocation->height);
530
531   /* No path is set; we don't have to allocate anything. */
532   if (path_bar->button_list == NULL)
533     return;
534
535   direction = gtk_widget_get_direction (widget);
536   allocation_width = allocation->width;
537
538   /* First, we check to see if we need the scrollbars. */
539   if (path_bar->fake_root)
540     width = path_bar->spacing + path_bar->slider_width;
541   else
542     width = 0;
543
544   for (list = path_bar->button_list; list; list = list->next)
545     {
546       child = BUTTON_DATA (list->data)->button;
547
548       gtk_widget_get_preferred_size (child, &child_requisition, NULL);
549
550       width += child_requisition.width + path_bar->spacing;
551       if (list == path_bar->fake_root)
552         break;
553     }
554
555   if (width <= allocation_width)
556     {
557       if (path_bar->fake_root)
558         first_button = path_bar->fake_root;
559       else
560         first_button = g_list_last (path_bar->button_list);
561     }
562   else
563     {
564       gboolean reached_end = FALSE;
565       gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
566
567       if (path_bar->first_scrolled_button)
568         first_button = path_bar->first_scrolled_button;
569       else
570         first_button = path_bar->button_list;
571       need_sliders = TRUE;
572       
573       /* To see how much space we have, and how many buttons we can display.
574        * We start at the first button, count forward until hit the new
575        * button, then count backwards.
576        */
577       /* Count down the path chain towards the end. */
578       gtk_widget_get_preferred_size (BUTTON_DATA (first_button->data)->button,
579                                      &child_requisition, NULL);
580
581       width = child_requisition.width;
582       list = first_button->prev;
583       while (list && !reached_end)
584         {
585           child = BUTTON_DATA (list->data)->button;
586
587           gtk_widget_get_preferred_size (child, &child_requisition, NULL);
588
589           if (width + child_requisition.width +
590               path_bar->spacing + slider_space > allocation_width)
591             reached_end = TRUE;
592           else if (list == path_bar->fake_root)
593             break;
594           else
595             width += child_requisition.width + path_bar->spacing;
596
597           list = list->prev;
598         }
599
600       /* Finally, we walk up, seeing how many of the previous buttons we can
601        * add */
602       while (first_button->next && !reached_end)
603         {
604           child = BUTTON_DATA (first_button->next->data)->button;
605
606           gtk_widget_get_preferred_size (child, &child_requisition, NULL);
607
608           if (width + child_requisition.width + path_bar->spacing + slider_space > allocation_width)
609             {
610               reached_end = TRUE;
611             }
612           else
613             {
614               width += child_requisition.width + path_bar->spacing;
615               if (first_button == path_bar->fake_root)
616                 break;
617               first_button = first_button->next;
618             }
619         }
620     }
621
622   /* Now, we allocate space to the buttons */
623   child_allocation.y = allocation->y;
624   child_allocation.height = allocation->height;
625
626   if (direction == GTK_TEXT_DIR_RTL)
627     {
628       child_allocation.x = allocation->x + allocation->width;
629       if (need_sliders || path_bar->fake_root)
630         {
631           child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
632           up_slider_offset = allocation->width - path_bar->slider_width;
633         }
634     }
635   else
636     {
637       child_allocation.x = allocation->x;
638       if (need_sliders || path_bar->fake_root)
639         {
640           up_slider_offset = 0;
641           child_allocation.x += (path_bar->spacing + path_bar->slider_width);
642         }
643     }
644
645   for (list = first_button; list; list = list->prev)
646     {
647       GtkAllocation widget_allocation;
648       ButtonData *button_data;
649
650       button_data = BUTTON_DATA (list->data);
651       child = button_data->button;
652
653       gtk_widget_get_preferred_size (child, &child_requisition, NULL);
654
655       child_allocation.width = MIN (child_requisition.width,
656                                     allocation_width - (path_bar->spacing + path_bar->slider_width) * 2);
657
658       if (direction == GTK_TEXT_DIR_RTL)
659         child_allocation.x -= child_allocation.width;
660
661       /* Check to see if we've don't have any more space to allocate buttons */
662       if (need_sliders && direction == GTK_TEXT_DIR_RTL)
663         {
664           gtk_widget_get_allocation (widget, &widget_allocation);
665           if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget_allocation.x)
666             break;
667         }
668       else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
669         {
670           gtk_widget_get_allocation (widget, &widget_allocation);
671           if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
672               widget_allocation.x + allocation_width)
673             break;
674         }
675
676       if (child_allocation.width < child_requisition.width)
677         {
678           if (!gtk_widget_get_has_tooltip (child))
679             gtk_widget_set_tooltip_text (child, button_data->dir_name);
680         }
681       else if (gtk_widget_get_has_tooltip (child))
682         gtk_widget_set_tooltip_text (child, NULL);
683       
684       gtk_widget_set_child_visible (child, TRUE);
685       gtk_widget_size_allocate (child, &child_allocation);
686
687       if (direction == GTK_TEXT_DIR_RTL)
688         child_allocation.x -= path_bar->spacing;
689       else
690         child_allocation.x += child_allocation.width + path_bar->spacing;
691     }
692   /* Now we go hide all the widgets that don't fit */
693   while (list)
694     {
695       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
696       list = list->prev;
697     }
698   for (list = first_button->next; list; list = list->next)
699     {
700       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
701     }
702
703   if (need_sliders || path_bar->fake_root)
704     {
705       child_allocation.width = path_bar->slider_width;
706       child_allocation.x = up_slider_offset + allocation->x;
707       gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
708
709       gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
710       gtk_widget_show_all (path_bar->up_slider_button);
711     }
712   else
713     gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
714       
715   if (need_sliders)
716     {
717       child_allocation.width = path_bar->slider_width;
718
719       if (direction == GTK_TEXT_DIR_RTL)
720         child_allocation.x = 0;
721       else
722         child_allocation.x = allocation->width - path_bar->slider_width;
723
724       child_allocation.x += allocation->x;
725       
726       gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
727
728       gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
729       gtk_widget_show_all (path_bar->down_slider_button);
730       gtk_path_bar_update_slider_buttons (path_bar);
731     }
732   else
733     gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
734
735   child_ordering_changed (path_bar);
736 }
737
738 static void
739 gtk_path_bar_style_updated (GtkWidget *widget)
740 {
741   GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_updated (widget);
742
743   gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
744 }
745
746 static void
747 gtk_path_bar_screen_changed (GtkWidget *widget,
748                              GdkScreen *previous_screen)
749 {
750   if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
751     GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
752
753   /* We might nave a new settings, so we remove the old one */
754   if (previous_screen)
755     remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
756
757   gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
758 }
759
760 static gboolean
761 gtk_path_bar_scroll (GtkWidget      *widget,
762                      GdkEventScroll *event)
763 {
764   switch (event->direction)
765     {
766     case GDK_SCROLL_RIGHT:
767     case GDK_SCROLL_DOWN:
768       gtk_path_bar_scroll_down (GTK_PATH_BAR (widget));
769       break;
770     case GDK_SCROLL_LEFT:
771     case GDK_SCROLL_UP:
772       gtk_path_bar_scroll_up (GTK_PATH_BAR (widget));
773       break;
774     case GDK_SCROLL_SMOOTH:
775       break;
776     }
777
778   return TRUE;
779 }
780
781 static void
782 gtk_path_bar_add (GtkContainer *container,
783                   GtkWidget    *widget)
784
785 {
786   gtk_widget_set_parent (widget, GTK_WIDGET (container));
787 }
788
789 static void
790 gtk_path_bar_remove_1 (GtkContainer *container,
791                        GtkWidget    *widget)
792 {
793   gboolean was_visible = gtk_widget_get_visible (widget);
794   gtk_widget_unparent (widget);
795   if (was_visible)
796     gtk_widget_queue_resize (GTK_WIDGET (container));
797 }
798
799 static void
800 gtk_path_bar_remove (GtkContainer *container,
801                      GtkWidget    *widget)
802 {
803   GtkPathBar *path_bar;
804   GList *children;
805
806   path_bar = GTK_PATH_BAR (container);
807
808   if (widget == path_bar->up_slider_button)
809     {
810       gtk_path_bar_remove_1 (container, widget);
811       path_bar->up_slider_button = NULL;
812       return;
813     }
814
815   if (widget == path_bar->down_slider_button)
816     {
817       gtk_path_bar_remove_1 (container, widget);
818       path_bar->down_slider_button = NULL;
819       return;
820     }
821
822   children = path_bar->button_list;
823   while (children)
824     {
825       if (widget == BUTTON_DATA (children->data)->button)
826         {
827           gtk_path_bar_remove_1 (container, widget);
828           path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
829           g_list_free (children);
830           return;
831         }
832       
833       children = children->next;
834     }
835 }
836
837 static void
838 gtk_path_bar_forall (GtkContainer *container,
839                      gboolean      include_internals,
840                      GtkCallback   callback,
841                      gpointer      callback_data)
842 {
843   GtkPathBar *path_bar;
844   GList *children;
845
846   g_return_if_fail (callback != NULL);
847   path_bar = GTK_PATH_BAR (container);
848
849   children = path_bar->button_list;
850   while (children)
851     {
852       GtkWidget *child;
853       child = BUTTON_DATA (children->data)->button;
854       children = children->next;
855
856       (* callback) (child, callback_data);
857     }
858
859   if (path_bar->up_slider_button)
860     (* callback) (path_bar->up_slider_button, callback_data);
861
862   if (path_bar->down_slider_button)
863     (* callback) (path_bar->down_slider_button, callback_data);
864 }
865
866 static GtkWidgetPath *
867 gtk_path_bar_get_path_for_child (GtkContainer *container,
868                                  GtkWidget    *child)
869 {
870   GtkPathBar *path_bar = GTK_PATH_BAR (container);
871   GtkWidgetPath *path;
872
873   path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (path_bar)));
874
875   if (gtk_widget_get_visible (child) &&
876       gtk_widget_get_child_visible (child))
877     {
878       GtkWidgetPath *sibling_path;
879       GList *visible_children;
880       GList *l;
881       int pos;
882
883       /* 1. Build the list of visible children, in visually left-to-right order
884        * (i.e. independently of the widget's direction).  Note that our
885        * button_list is stored in innermost-to-outermost path order!
886        */
887
888       visible_children = NULL;
889
890       if (gtk_widget_get_visible (path_bar->down_slider_button) &&
891           gtk_widget_get_child_visible (path_bar->down_slider_button))
892         visible_children = g_list_prepend (visible_children, path_bar->down_slider_button);
893
894       for (l = path_bar->button_list; l; l = l->next)
895         {
896           ButtonData *data = l->data;
897
898           if (gtk_widget_get_visible (data->button) &&
899               gtk_widget_get_child_visible (data->button))
900             visible_children = g_list_prepend (visible_children, data->button);
901         }
902
903       if (gtk_widget_get_visible (path_bar->up_slider_button) &&
904           gtk_widget_get_child_visible (path_bar->up_slider_button))
905         visible_children = g_list_prepend (visible_children, path_bar->up_slider_button);
906
907       if (gtk_widget_get_direction (GTK_WIDGET (path_bar)) == GTK_TEXT_DIR_RTL)
908         visible_children = g_list_reverse (visible_children);
909
910       /* 2. Find the index of the child within that list */
911
912       pos = 0;
913
914       for (l = visible_children; l; l = l->next)
915         {
916           GtkWidget *button = l->data;
917
918           if (button == child)
919             break;
920
921           pos++;
922         }
923
924       /* 3. Build the path */
925
926       sibling_path = gtk_widget_path_new ();
927
928       for (l = visible_children; l; l = l->next)
929         gtk_widget_path_append_for_widget (sibling_path, l->data);
930
931       gtk_widget_path_append_with_siblings (path, sibling_path, pos);
932
933       g_list_free (visible_children);
934       gtk_widget_path_unref (sibling_path);
935     }
936   else
937     gtk_widget_path_append_for_widget (path, child);
938
939   return path;
940 }
941
942 static void
943 gtk_path_bar_scroll_down (GtkPathBar *path_bar)
944 {
945   GtkAllocation allocation, button_allocation;
946   GList *list;
947   GList *down_button = NULL;
948   gint space_available;
949
950   if (path_bar->ignore_click)
951     {
952       path_bar->ignore_click = FALSE;
953       return;   
954     }
955
956   if (gtk_widget_get_child_visible (BUTTON_DATA (path_bar->button_list->data)->button))
957     {
958       /* Return if the last button is already visible */
959       return;
960     }
961
962   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
963
964   /* We find the button at the 'down' end that we have to make
965    * visible */
966   for (list = path_bar->button_list; list; list = list->next)
967     {
968       if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
969         {
970           down_button = list;
971           break;
972         }
973     }
974
975   gtk_widget_get_allocation (GTK_WIDGET (path_bar), &allocation);
976   gtk_widget_get_allocation (BUTTON_DATA (down_button->data)->button, &button_allocation);
977
978   space_available = (allocation.width
979                      - 2 * path_bar->spacing - 2 * path_bar->slider_width
980                      - button_allocation.width);
981   path_bar->first_scrolled_button = down_button;
982   
983   /* We have space_available free space that's not being used.  
984    * So we walk down from the end, adding buttons until we use all free space.
985    */
986   while (space_available > 0)
987     {
988       path_bar->first_scrolled_button = down_button;
989       down_button = down_button->next;
990       if (!down_button)
991         break;
992       space_available -= (button_allocation.width
993                           + path_bar->spacing);
994     }
995 }
996
997 static void
998 gtk_path_bar_scroll_up (GtkPathBar *path_bar)
999 {
1000   GList *list;
1001
1002   if (path_bar->ignore_click)
1003     {
1004       path_bar->ignore_click = FALSE;
1005       return;   
1006     }
1007
1008   list = g_list_last (path_bar->button_list);
1009
1010   if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
1011     {
1012       /* Return if the first button is already visible */
1013       return;
1014     }
1015
1016   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1017
1018   for ( ; list; list = list->prev)
1019     {
1020       if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
1021         {
1022           if (list->prev == path_bar->fake_root)
1023             path_bar->fake_root = NULL;
1024           path_bar->first_scrolled_button = list;
1025           return;
1026         }
1027     }
1028 }
1029
1030 static gboolean
1031 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
1032 {
1033   gboolean retval = FALSE;
1034
1035   if (path_bar->timer)
1036     {
1037       if (path_bar->scrolling_up)
1038         gtk_path_bar_scroll_up (path_bar);
1039       else if (path_bar->scrolling_down)
1040         gtk_path_bar_scroll_down (path_bar);
1041
1042       if (path_bar->need_timer) 
1043         {
1044           GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (path_bar));
1045           guint        timeout;
1046
1047           g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1048
1049           path_bar->need_timer = FALSE;
1050
1051           path_bar->timer = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
1052                                            (GSourceFunc)gtk_path_bar_scroll_timeout,
1053                                            path_bar);
1054         }
1055       else
1056         retval = TRUE;
1057     }
1058
1059   return retval;
1060 }
1061
1062 static void 
1063 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
1064 {
1065   if (path_bar->timer)
1066     {
1067       g_source_remove (path_bar->timer);
1068       path_bar->timer = 0;
1069       path_bar->need_timer = FALSE;
1070     }
1071 }
1072
1073 static gboolean
1074 gtk_path_bar_slider_up_defocus (GtkWidget      *widget,
1075                                     GdkEventButton *event,
1076                                     GtkPathBar     *path_bar)
1077 {
1078   GList *list;
1079   GList *up_button = NULL;
1080
1081   if (event->type != GDK_FOCUS_CHANGE)
1082     return FALSE;
1083
1084   for (list = g_list_last (path_bar->button_list); list; list = list->prev)
1085     {
1086       if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
1087         {
1088           up_button = list;
1089           break;
1090         }
1091     }
1092
1093   /* don't let the focus vanish */
1094   if ((!gtk_widget_is_sensitive (path_bar->up_slider_button)) ||
1095       (!gtk_widget_get_child_visible (path_bar->up_slider_button)))
1096     gtk_widget_grab_focus (BUTTON_DATA (up_button->data)->button);
1097
1098   return FALSE;
1099 }
1100
1101 static gboolean
1102 gtk_path_bar_slider_down_defocus (GtkWidget      *widget,
1103                                     GdkEventButton *event,
1104                                     GtkPathBar     *path_bar)
1105 {
1106   GList *list;
1107   GList *down_button = NULL;
1108
1109   if (event->type != GDK_FOCUS_CHANGE)
1110     return FALSE;
1111
1112   for (list = path_bar->button_list; list; list = list->next)
1113     {
1114       if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
1115         {
1116           down_button = list;
1117           break;
1118         }
1119     }
1120
1121   /* don't let the focus vanish */
1122   if ((!gtk_widget_is_sensitive (path_bar->down_slider_button)) ||
1123       (!gtk_widget_get_child_visible (path_bar->down_slider_button)))
1124     gtk_widget_grab_focus (BUTTON_DATA (down_button->data)->button);
1125
1126   return FALSE;
1127 }
1128
1129 static gboolean
1130 gtk_path_bar_slider_button_press (GtkWidget      *widget, 
1131                                   GdkEventButton *event,
1132                                   GtkPathBar     *path_bar)
1133 {
1134   if (event->type != GDK_BUTTON_PRESS || event->button != GDK_BUTTON_PRIMARY)
1135     return FALSE;
1136
1137   path_bar->ignore_click = FALSE;
1138
1139   if (widget == path_bar->up_slider_button)
1140     {
1141       path_bar->scrolling_down = FALSE;
1142       path_bar->scrolling_up = TRUE;
1143       gtk_path_bar_scroll_up (path_bar);
1144     }
1145   else if (widget == path_bar->down_slider_button)
1146     {
1147       path_bar->scrolling_up = FALSE;
1148       path_bar->scrolling_down = TRUE;
1149       gtk_path_bar_scroll_down (path_bar);
1150     }
1151
1152   if (!path_bar->timer)
1153     {
1154       GtkSettings *settings = gtk_widget_get_settings (widget);
1155       guint        timeout;
1156
1157       g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1158
1159       path_bar->need_timer = TRUE;
1160       path_bar->timer = gdk_threads_add_timeout (timeout,
1161                                        (GSourceFunc)gtk_path_bar_scroll_timeout,
1162                                        path_bar);
1163     }
1164
1165   return FALSE;
1166 }
1167
1168 static gboolean
1169 gtk_path_bar_slider_button_release (GtkWidget      *widget, 
1170                                     GdkEventButton *event,
1171                                     GtkPathBar     *path_bar)
1172 {
1173   if (event->type != GDK_BUTTON_RELEASE)
1174     return FALSE;
1175
1176   path_bar->ignore_click = TRUE;
1177   gtk_path_bar_stop_scrolling (path_bar);
1178
1179   return FALSE;
1180 }
1181
1182 static void
1183 gtk_path_bar_grab_notify (GtkWidget *widget,
1184                           gboolean   was_grabbed)
1185 {
1186   if (!was_grabbed)
1187     gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
1188 }
1189
1190 static void
1191 gtk_path_bar_state_changed (GtkWidget    *widget,
1192                             GtkStateType  previous_state)
1193 {
1194   if (!gtk_widget_is_sensitive (widget))
1195     gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
1196 }
1197
1198
1199 /* Changes the icons wherever it is needed */
1200 static void
1201 reload_icons (GtkPathBar *path_bar)
1202 {
1203   GList *list;
1204
1205   if (path_bar->root_icon)
1206     {
1207       g_object_unref (path_bar->root_icon);
1208       path_bar->root_icon = NULL;
1209     }
1210   if (path_bar->home_icon)
1211     {
1212       g_object_unref (path_bar->home_icon);
1213       path_bar->home_icon = NULL;
1214     }
1215   if (path_bar->desktop_icon)
1216     {
1217       g_object_unref (path_bar->desktop_icon);
1218       path_bar->desktop_icon = NULL;
1219     }
1220
1221   for (list = path_bar->button_list; list; list = list->next)
1222     {
1223       ButtonData *button_data;
1224       gboolean current_dir;
1225
1226       button_data = BUTTON_DATA (list->data);
1227       if (button_data->type != NORMAL_BUTTON)
1228         {
1229           current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
1230           gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1231         }
1232     }
1233   
1234 }
1235
1236 static void
1237 change_icon_theme (GtkPathBar *path_bar)
1238 {
1239   GtkSettings *settings;
1240   gint width, height;
1241
1242   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
1243
1244   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
1245     path_bar->icon_size = MAX (width, height);
1246   else
1247     path_bar->icon_size = FALLBACK_ICON_SIZE;
1248
1249   reload_icons (path_bar);
1250 }
1251 /* Callback used when a GtkSettings value changes */
1252 static void
1253 settings_notify_cb (GObject    *object,
1254                     GParamSpec *pspec,
1255                     GtkPathBar *path_bar)
1256 {
1257   const char *name;
1258
1259   name = g_param_spec_get_name (pspec);
1260
1261   if (! strcmp (name, "gtk-icon-theme-name") ||
1262       ! strcmp (name, "gtk-icon-sizes"))
1263     change_icon_theme (path_bar);
1264 }
1265
1266 static void
1267 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
1268 {
1269   GtkSettings *settings;
1270
1271   if (path_bar->settings_signal_id)
1272     return;
1273
1274   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
1275   path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
1276
1277   change_icon_theme (path_bar);
1278 }
1279
1280 /* Public functions and their helpers */
1281 static void
1282 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
1283 {
1284   while (path_bar->button_list != NULL)
1285     {
1286       gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
1287     }
1288   path_bar->first_scrolled_button = NULL;
1289   path_bar->fake_root = NULL;
1290 }
1291
1292 static void
1293 button_clicked_cb (GtkWidget *button,
1294                    gpointer   data)
1295 {
1296   ButtonData *button_data;
1297   GtkPathBar *path_bar;
1298   GList *button_list;
1299   gboolean child_is_hidden;
1300   GFile *child_file;
1301
1302   button_data = BUTTON_DATA (data);
1303   if (button_data->ignore_changes)
1304     return;
1305
1306   path_bar = GTK_PATH_BAR (gtk_widget_get_parent (button));
1307
1308   button_list = g_list_find (path_bar->button_list, button_data);
1309   g_assert (button_list != NULL);
1310
1311   g_signal_handlers_block_by_func (button,
1312                                    G_CALLBACK (button_clicked_cb), data);
1313   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
1314   g_signal_handlers_unblock_by_func (button,
1315                                      G_CALLBACK (button_clicked_cb), data);
1316
1317   if (button_list->prev)
1318     {
1319       ButtonData *child_data;
1320
1321       child_data = BUTTON_DATA (button_list->prev->data);
1322       child_file = child_data->file;
1323       child_is_hidden = child_data->file_is_hidden;
1324     }
1325   else
1326     {
1327       child_file = NULL;
1328       child_is_hidden = FALSE;
1329     }
1330
1331   g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0,
1332                  button_data->file, child_file, child_is_hidden);
1333 }
1334
1335 struct SetButtonImageData
1336 {
1337   GtkPathBar *path_bar;
1338   ButtonData *button_data;
1339 };
1340
1341 static void
1342 set_button_image_get_info_cb (GCancellable *cancellable,
1343                               GFileInfo    *info,
1344                               const GError *error,
1345                               gpointer      user_data)
1346 {
1347   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1348   GdkPixbuf *pixbuf;
1349   struct SetButtonImageData *data = user_data;
1350
1351   if (cancellable != data->button_data->cancellable)
1352     goto out;
1353
1354   data->button_data->cancellable = NULL;
1355
1356   if (!data->button_data->button)
1357     {
1358       g_free (data->button_data);
1359       goto out;
1360     }
1361
1362   if (cancelled || error)
1363     goto out;
1364
1365   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->path_bar),
1366                                        data->path_bar->icon_size);
1367   gtk_image_set_from_pixbuf (GTK_IMAGE (data->button_data->image), pixbuf);
1368
1369   switch (data->button_data->type)
1370     {
1371       case HOME_BUTTON:
1372         if (data->path_bar->home_icon)
1373           g_object_unref (pixbuf);
1374         else
1375           data->path_bar->home_icon = pixbuf;
1376         break;
1377
1378       case DESKTOP_BUTTON:
1379         if (data->path_bar->desktop_icon)
1380           g_object_unref (pixbuf);
1381         else
1382           data->path_bar->desktop_icon = pixbuf;
1383         break;
1384
1385       default:
1386         break;
1387     };
1388
1389 out:
1390   g_free (data);
1391   g_object_unref (cancellable);
1392 }
1393
1394 static void
1395 set_button_image (GtkPathBar *path_bar,
1396                   ButtonData *button_data)
1397 {
1398   GtkFileSystemVolume *volume;
1399   struct SetButtonImageData *data;
1400
1401   switch (button_data->type)
1402     {
1403     case ROOT_BUTTON:
1404
1405       if (path_bar->root_icon != NULL)
1406         {
1407           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1408           break;
1409         }
1410
1411       volume = _gtk_file_system_get_volume_for_file (path_bar->file_system, path_bar->root_file);
1412       if (volume == NULL)
1413         return;
1414
1415       path_bar->root_icon = _gtk_file_system_volume_render_icon (volume,
1416                                                                  GTK_WIDGET (path_bar),
1417                                                                  path_bar->icon_size,
1418                                                                  NULL);
1419       _gtk_file_system_volume_unref (volume);
1420
1421       gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1422       break;
1423
1424     case HOME_BUTTON:
1425       if (path_bar->home_icon != NULL)
1426         {
1427           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->home_icon);
1428           break;
1429         }
1430
1431       data = g_new0 (struct SetButtonImageData, 1);
1432       data->path_bar = path_bar;
1433       data->button_data = button_data;
1434
1435       if (button_data->cancellable)
1436         g_cancellable_cancel (button_data->cancellable);
1437
1438       button_data->cancellable =
1439         _gtk_file_system_get_info (path_bar->file_system,
1440                                    path_bar->home_file,
1441                                    "standard::icon",
1442                                    set_button_image_get_info_cb,
1443                                    data);
1444       break;
1445
1446     case DESKTOP_BUTTON:
1447       if (path_bar->desktop_icon != NULL)
1448         {
1449           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->desktop_icon);
1450           break;
1451         }
1452
1453       data = g_new0 (struct SetButtonImageData, 1);
1454       data->path_bar = path_bar;
1455       data->button_data = button_data;
1456
1457       if (button_data->cancellable)
1458         g_cancellable_cancel (button_data->cancellable);
1459
1460       button_data->cancellable =
1461         _gtk_file_system_get_info (path_bar->file_system,
1462                                    path_bar->desktop_file,
1463                                    "standard::icon",
1464                                    set_button_image_get_info_cb,
1465                                    data);
1466       break;
1467     default:
1468       break;
1469     }
1470 }
1471
1472 static void
1473 button_data_free (ButtonData *button_data)
1474 {
1475   if (button_data->file)
1476     g_object_unref (button_data->file);
1477   button_data->file = NULL;
1478
1479   g_free (button_data->dir_name);
1480   button_data->dir_name = NULL;
1481
1482   button_data->button = NULL;
1483
1484   if (button_data->cancellable)
1485     g_cancellable_cancel (button_data->cancellable);
1486   else
1487     g_free (button_data);
1488 }
1489
1490 static const char *
1491 get_dir_name (ButtonData *button_data)
1492 {
1493   return button_data->dir_name;
1494 }
1495
1496 /* We always want to request the same size for the label, whether
1497  * or not the contents are bold
1498  */
1499 static void
1500 set_label_size_request (GtkWidget  *widget,
1501                         ButtonData *button_data)
1502 {
1503   const gchar *dir_name = get_dir_name (button_data);
1504   PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1505   gint width, height, bold_width, bold_height;
1506   gchar *markup;
1507   
1508   pango_layout_get_pixel_size (layout, &width, &height);
1509   
1510   markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1511   pango_layout_set_markup (layout, markup, -1);
1512   g_free (markup);
1513
1514   pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1515
1516   gtk_widget_set_size_request (widget,
1517                                MAX (width, bold_width),
1518                                MAX (height, bold_height));
1519   g_object_unref (layout);
1520 }
1521
1522 static void
1523 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1524                                        ButtonData *button_data,
1525                                        gboolean    current_dir)
1526 {
1527   const gchar *dir_name = get_dir_name (button_data);
1528
1529   if (button_data->label != NULL)
1530     {
1531       if (current_dir)
1532         {
1533           char *markup;
1534
1535           markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1536           gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1537           g_free (markup);
1538         }
1539       else
1540         {
1541           gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1542         }
1543     }
1544
1545   if (button_data->image != NULL)
1546     {
1547       set_button_image (path_bar, button_data);
1548     }
1549
1550   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1551     {
1552       button_data->ignore_changes = TRUE;
1553       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1554       button_data->ignore_changes = FALSE;
1555     }
1556 }
1557
1558 static ButtonType
1559 find_button_type (GtkPathBar  *path_bar,
1560                   GFile       *file)
1561 {
1562   if (path_bar->root_file != NULL &&
1563       g_file_equal (file, path_bar->root_file))
1564     return ROOT_BUTTON;
1565   if (path_bar->home_file != NULL &&
1566       g_file_equal (file, path_bar->home_file))
1567     return HOME_BUTTON;
1568   if (path_bar->desktop_file != NULL &&
1569       g_file_equal (file, path_bar->desktop_file))
1570     return DESKTOP_BUTTON;
1571
1572  return NORMAL_BUTTON;
1573 }
1574
1575 static void
1576 button_drag_data_get_cb (GtkWidget        *widget,
1577                          GdkDragContext   *context,
1578                          GtkSelectionData *selection_data,
1579                          guint             info,
1580                          guint             time_,
1581                          gpointer          data)
1582 {
1583   ButtonData *button_data;
1584   char *uris[2];
1585
1586   button_data = data;
1587
1588   uris[0] = g_file_get_uri (button_data->file);
1589   uris[1] = NULL;
1590
1591   gtk_selection_data_set_uris (selection_data, uris);
1592
1593   g_free (uris[0]);
1594 }
1595
1596 static ButtonData *
1597 make_directory_button (GtkPathBar  *path_bar,
1598                        const char  *dir_name,
1599                        GFile       *file,
1600                        gboolean     current_dir,
1601                        gboolean     file_is_hidden)
1602 {
1603   AtkObject *atk_obj;
1604   GtkWidget *child = NULL;
1605   ButtonData *button_data;
1606
1607   file_is_hidden = !! file_is_hidden;
1608   /* Is it a special button? */
1609   button_data = g_new0 (ButtonData, 1);
1610
1611   button_data->type = find_button_type (path_bar, file);
1612   button_data->button = gtk_toggle_button_new ();
1613   atk_obj = gtk_widget_get_accessible (button_data->button);
1614   gtk_button_set_focus_on_click (GTK_BUTTON (button_data->button), FALSE);
1615   gtk_widget_add_events (button_data->button, GDK_SCROLL_MASK);
1616
1617   switch (button_data->type)
1618     {
1619     case ROOT_BUTTON:
1620       button_data->image = gtk_image_new ();
1621       child = button_data->image;
1622       button_data->label = NULL;
1623       atk_object_set_name (atk_obj, _("File System Root"));
1624       break;
1625     case HOME_BUTTON:
1626     case DESKTOP_BUTTON:
1627       button_data->image = gtk_image_new ();
1628       button_data->label = gtk_label_new (NULL);
1629       child = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
1630       gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1631       gtk_box_pack_start (GTK_BOX (child), button_data->label, FALSE, FALSE, 0);
1632       break;
1633     case NORMAL_BUTTON:
1634     default:
1635       button_data->label = gtk_label_new (NULL);
1636       gtk_label_set_ellipsize (GTK_LABEL (button_data->label), PANGO_ELLIPSIZE_END);
1637       child = button_data->label;
1638       button_data->image = NULL;
1639     }
1640
1641   button_data->dir_name = g_strdup (dir_name);
1642   button_data->file = g_object_ref (file);
1643   button_data->file_is_hidden = file_is_hidden;
1644
1645   /*
1646    * The following function ensures that the alignment will always
1647    * request the same size whether the button's text is bold or not.
1648    */
1649   if (button_data->label)
1650     set_label_size_request (button_data->label, button_data);
1651
1652   gtk_container_add (GTK_CONTAINER (button_data->button), child);
1653   gtk_widget_show_all (button_data->button);
1654
1655   gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1656
1657   g_signal_connect (button_data->button, "clicked",
1658                     G_CALLBACK (button_clicked_cb),
1659                     button_data);
1660   g_object_weak_ref (G_OBJECT (button_data->button),
1661                      (GWeakNotify) button_data_free, button_data);
1662
1663   gtk_drag_source_set (button_data->button,
1664                        GDK_BUTTON1_MASK,
1665                        NULL, 0,
1666                        GDK_ACTION_COPY);
1667   gtk_drag_source_add_uri_targets (button_data->button);
1668   g_signal_connect (button_data->button, "drag-data-get",
1669                     G_CALLBACK (button_drag_data_get_cb), button_data);
1670
1671   return button_data;
1672 }
1673
1674 static gboolean
1675 gtk_path_bar_check_parent_path (GtkPathBar         *path_bar,
1676                                 GFile              *file)
1677 {
1678   GList *list;
1679   GList *current_path = NULL;
1680   gboolean need_new_fake_root = FALSE;
1681
1682   for (list = path_bar->button_list; list; list = list->next)
1683     {
1684       ButtonData *button_data;
1685
1686       button_data = list->data;
1687       if (g_file_equal (file, button_data->file))
1688         {
1689           current_path = list;
1690           break;
1691         }
1692       if (list == path_bar->fake_root)
1693         need_new_fake_root = TRUE;
1694     }
1695
1696   if (current_path)
1697     {
1698       if (need_new_fake_root)
1699         {
1700           path_bar->fake_root = NULL;
1701           for (list = current_path; list; list = list->next)
1702             {
1703               ButtonData *button_data;
1704
1705               button_data = list->data;
1706               if (BUTTON_IS_FAKE_ROOT (button_data))
1707                 {
1708                   path_bar->fake_root = list;
1709                   break;
1710                 }
1711             }
1712         }
1713
1714       for (list = path_bar->button_list; list; list = list->next)
1715         {
1716           gtk_path_bar_update_button_appearance (path_bar,
1717                                                  BUTTON_DATA (list->data),
1718                                                  (list == current_path) ? TRUE : FALSE);
1719         }
1720
1721       if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1722         {
1723           path_bar->first_scrolled_button = current_path;
1724           gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1725         }
1726
1727       return TRUE;
1728     }
1729   return FALSE;
1730 }
1731
1732
1733 struct SetFileInfo
1734 {
1735   GFile *file;
1736   GFile *parent_file;
1737   GtkPathBar *path_bar;
1738   GList *new_buttons;
1739   GList *fake_root;
1740   gboolean first_directory;
1741 };
1742
1743 static void
1744 gtk_path_bar_set_file_finish (struct SetFileInfo *info,
1745                               gboolean            result)
1746 {
1747   if (result)
1748     {
1749       GList *l;
1750
1751       gtk_path_bar_clear_buttons (info->path_bar);
1752       info->path_bar->button_list = g_list_reverse (info->new_buttons);
1753       info->path_bar->fake_root = info->fake_root;
1754
1755       for (l = info->path_bar->button_list; l; l = l->next)
1756         {
1757           GtkWidget *button = BUTTON_DATA (l->data)->button;
1758           gtk_container_add (GTK_CONTAINER (info->path_bar), button);
1759         }
1760
1761       child_ordering_changed (info->path_bar);
1762     }
1763   else
1764     {
1765       GList *l;
1766
1767       for (l = info->new_buttons; l; l = l->next)
1768         {
1769           ButtonData *button_data;
1770
1771           button_data = BUTTON_DATA (l->data);
1772           gtk_widget_destroy (button_data->button);
1773         }
1774
1775       g_list_free (info->new_buttons);
1776     }
1777
1778   if (info->file)
1779     g_object_unref (info->file);
1780   if (info->parent_file)
1781     g_object_unref (info->parent_file);
1782
1783   g_free (info);
1784 }
1785
1786 static void
1787 gtk_path_bar_get_info_callback (GCancellable *cancellable,
1788                                 GFileInfo    *info,
1789                                 const GError *error,
1790                                 gpointer      data)
1791 {
1792   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1793   struct SetFileInfo *file_info = data;
1794   ButtonData *button_data;
1795   const gchar *display_name;
1796   gboolean is_hidden;
1797
1798   if (cancellable != file_info->path_bar->get_info_cancellable)
1799     {
1800       gtk_path_bar_set_file_finish (file_info, FALSE);
1801       g_object_unref (cancellable);
1802       return;
1803     }
1804
1805   g_object_unref (cancellable);
1806   file_info->path_bar->get_info_cancellable = NULL;
1807
1808   if (cancelled || !info)
1809     {
1810       gtk_path_bar_set_file_finish (file_info, FALSE);
1811       return;
1812     }
1813
1814   display_name = g_file_info_get_display_name (info);
1815   is_hidden = g_file_info_get_is_hidden (info) || g_file_info_get_is_backup (info);
1816
1817   gtk_widget_push_composite_child ();
1818   button_data = make_directory_button (file_info->path_bar, display_name,
1819                                        file_info->file,
1820                                        file_info->first_directory, is_hidden);
1821   gtk_widget_pop_composite_child ();
1822   g_object_unref (file_info->file);
1823
1824   file_info->new_buttons = g_list_prepend (file_info->new_buttons, button_data);
1825
1826   if (BUTTON_IS_FAKE_ROOT (button_data))
1827     file_info->fake_root = file_info->new_buttons;
1828
1829   /* We have assigned the info for the innermost button, i.e. the deepest directory.
1830    * Now, go on to fetch the info for this directory's parent.
1831    */
1832
1833   file_info->file = file_info->parent_file;
1834   file_info->first_directory = FALSE;
1835
1836   if (!file_info->file)
1837     {
1838       /* No parent?  Okay, we are done. */
1839       gtk_path_bar_set_file_finish (file_info, TRUE);
1840       return;
1841     }
1842
1843   file_info->parent_file = g_file_get_parent (file_info->file);
1844
1845   /* Recurse asynchronously */
1846   file_info->path_bar->get_info_cancellable =
1847     _gtk_file_system_get_info (file_info->path_bar->file_system,
1848                                file_info->file,
1849                                "standard::display-name,standard::is-hidden,standard::is-backup",
1850                                gtk_path_bar_get_info_callback,
1851                                file_info);
1852 }
1853
1854 void
1855 _gtk_path_bar_set_file (GtkPathBar      *path_bar,
1856                         GFile           *file,
1857                         const gboolean   keep_trail)
1858 {
1859   struct SetFileInfo *info;
1860
1861   g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1862   g_return_if_fail (G_IS_FILE (file));
1863
1864   /* Check whether the new path is already present in the pathbar as buttons.
1865    * This could be a parent directory or a previous selected subdirectory.
1866    */
1867   if (keep_trail && gtk_path_bar_check_parent_path (path_bar, file))
1868     return;
1869
1870   info = g_new0 (struct SetFileInfo, 1);
1871   info->file = g_object_ref (file);
1872   info->path_bar = path_bar;
1873   info->first_directory = TRUE;
1874   info->parent_file = g_file_get_parent (info->file);
1875
1876   if (path_bar->get_info_cancellable)
1877     g_cancellable_cancel (path_bar->get_info_cancellable);
1878
1879   path_bar->get_info_cancellable =
1880     _gtk_file_system_get_info (path_bar->file_system,
1881                                info->file,
1882                                "standard::display-name,standard::is-hidden,standard::is-backup",
1883                                gtk_path_bar_get_info_callback,
1884                                info);
1885 }
1886
1887 /* FIXME: This should be a construct-only property */
1888 void
1889 _gtk_path_bar_set_file_system (GtkPathBar    *path_bar,
1890                                GtkFileSystem *file_system)
1891 {
1892   const char *home;
1893
1894   g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1895
1896   g_assert (path_bar->file_system == NULL);
1897
1898   path_bar->file_system = g_object_ref (file_system);
1899
1900   home = g_get_home_dir ();
1901   if (home != NULL)
1902     {
1903       const gchar *desktop;
1904
1905       path_bar->home_file = g_file_new_for_path (home);
1906       /* FIXME: Need file system backend specific way of getting the
1907        * Desktop path.
1908        */
1909       desktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1910       if (desktop != NULL)
1911         path_bar->desktop_file = g_file_new_for_path (desktop);
1912       else 
1913         path_bar->desktop_file = NULL;
1914     }
1915   else
1916     {
1917       path_bar->home_file = NULL;
1918       path_bar->desktop_file = NULL;
1919     }
1920   path_bar->root_file = g_file_new_for_path ("/");
1921 }
1922
1923 /**
1924  * _gtk_path_bar_up:
1925  * @path_bar: a #GtkPathBar
1926  * 
1927  * If the selected button in the pathbar is not the furthest button "up" (in the
1928  * root direction), act as if the user clicked on the next button up.
1929  **/
1930 void
1931 _gtk_path_bar_up (GtkPathBar *path_bar)
1932 {
1933   GList *l;
1934
1935   for (l = path_bar->button_list; l; l = l->next)
1936     {
1937       GtkWidget *button = BUTTON_DATA (l->data)->button;
1938       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1939         {
1940           if (l->next)
1941             {
1942               GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1943               button_clicked_cb (next_button, l->next->data);
1944             }
1945           break;
1946         }
1947     }
1948 }
1949
1950 /**
1951  * _gtk_path_bar_down:
1952  * @path_bar: a #GtkPathBar
1953  * 
1954  * If the selected button in the pathbar is not the furthest button "down" (in the
1955  * leaf direction), act as if the user clicked on the next button down.
1956  **/
1957 void
1958 _gtk_path_bar_down (GtkPathBar *path_bar)
1959 {
1960   GList *l;
1961
1962   for (l = path_bar->button_list; l; l = l->next)
1963     {
1964       GtkWidget *button = BUTTON_DATA (l->data)->button;
1965       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1966         {
1967           if (l->prev)
1968             {
1969               GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1970               button_clicked_cb (prev_button, l->prev->data);
1971             }
1972           break;
1973         }
1974     }
1975 }