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