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