]> Pileus Git - ~andy/gtk/blob - gtk/gtkpathbar.c
gtk: Make widget only invalidate widget positions
[~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   gdk_window_set_user_data (path_bar->event_window, widget);
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   gdk_window_set_user_data (path_bar->event_window, NULL);
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
530   gtk_widget_set_allocation (widget, allocation);
531
532   if (gtk_widget_get_realized (widget))
533     gdk_window_move_resize (path_bar->event_window,
534                             allocation->x, allocation->y,
535                             allocation->width, allocation->height);
536
537   /* No path is set; we don't have to allocate anything. */
538   if (path_bar->button_list == NULL)
539     return;
540
541   direction = gtk_widget_get_direction (widget);
542   allocation_width = allocation->width;
543
544   /* First, we check to see if we need the scrollbars. */
545   if (path_bar->fake_root)
546     width = path_bar->spacing + path_bar->slider_width;
547   else
548     width = 0;
549
550   for (list = path_bar->button_list; list; list = list->next)
551     {
552       child = BUTTON_DATA (list->data)->button;
553
554       gtk_widget_get_preferred_size (child, &child_requisition, NULL);
555
556       width += child_requisition.width + path_bar->spacing;
557       if (list == path_bar->fake_root)
558         break;
559     }
560
561   if (width <= allocation_width)
562     {
563       if (path_bar->fake_root)
564         first_button = path_bar->fake_root;
565       else
566         first_button = g_list_last (path_bar->button_list);
567     }
568   else
569     {
570       gboolean reached_end = FALSE;
571       gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
572
573       if (path_bar->first_scrolled_button)
574         first_button = path_bar->first_scrolled_button;
575       else
576         first_button = path_bar->button_list;
577       need_sliders = TRUE;
578       
579       /* To see how much space we have, and how many buttons we can display.
580        * We start at the first button, count forward until hit the new
581        * button, then count backwards.
582        */
583       /* Count down the path chain towards the end. */
584       gtk_widget_get_preferred_size (BUTTON_DATA (first_button->data)->button,
585                                      &child_requisition, NULL);
586
587       width = child_requisition.width;
588       list = first_button->prev;
589       while (list && !reached_end)
590         {
591           child = BUTTON_DATA (list->data)->button;
592
593           gtk_widget_get_preferred_size (child, &child_requisition, NULL);
594
595           if (width + child_requisition.width +
596               path_bar->spacing + slider_space > allocation_width)
597             reached_end = TRUE;
598           else if (list == path_bar->fake_root)
599             break;
600           else
601             width += child_requisition.width + path_bar->spacing;
602
603           list = list->prev;
604         }
605
606       /* Finally, we walk up, seeing how many of the previous buttons we can
607        * add */
608       while (first_button->next && !reached_end)
609         {
610           child = BUTTON_DATA (first_button->next->data)->button;
611
612           gtk_widget_get_preferred_size (child, &child_requisition, NULL);
613
614           if (width + child_requisition.width + path_bar->spacing + slider_space > allocation_width)
615             {
616               reached_end = TRUE;
617             }
618           else
619             {
620               width += child_requisition.width + path_bar->spacing;
621               if (first_button == path_bar->fake_root)
622                 break;
623               first_button = first_button->next;
624             }
625         }
626     }
627
628   /* Now, we allocate space to the buttons */
629   child_allocation.y = allocation->y;
630   child_allocation.height = allocation->height;
631
632   if (direction == GTK_TEXT_DIR_RTL)
633     {
634       child_allocation.x = allocation->x + allocation->width;
635       if (need_sliders || path_bar->fake_root)
636         {
637           child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
638           up_slider_offset = allocation->width - path_bar->slider_width;
639         }
640     }
641   else
642     {
643       child_allocation.x = allocation->x;
644       if (need_sliders || path_bar->fake_root)
645         {
646           up_slider_offset = 0;
647           child_allocation.x += (path_bar->spacing + path_bar->slider_width);
648         }
649     }
650
651   for (list = first_button; list; list = list->prev)
652     {
653       GtkAllocation widget_allocation;
654       ButtonData *button_data;
655
656       button_data = BUTTON_DATA (list->data);
657       child = button_data->button;
658
659       gtk_widget_get_preferred_size (child, &child_requisition, NULL);
660
661       child_allocation.width = MIN (child_requisition.width,
662                                     allocation_width - (path_bar->spacing + path_bar->slider_width) * 2);
663
664       if (direction == GTK_TEXT_DIR_RTL)
665         child_allocation.x -= child_allocation.width;
666
667       /* Check to see if we've don't have any more space to allocate buttons */
668       if (need_sliders && direction == GTK_TEXT_DIR_RTL)
669         {
670           gtk_widget_get_allocation (widget, &widget_allocation);
671           if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget_allocation.x)
672             break;
673         }
674       else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
675         {
676           gtk_widget_get_allocation (widget, &widget_allocation);
677           if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
678               widget_allocation.x + allocation_width)
679             break;
680         }
681
682       if (child_allocation.width < child_requisition.width)
683         {
684           if (!gtk_widget_get_has_tooltip (child))
685             gtk_widget_set_tooltip_text (child, button_data->dir_name);
686         }
687       else if (gtk_widget_get_has_tooltip (child))
688         gtk_widget_set_tooltip_text (child, NULL);
689       
690       gtk_widget_set_child_visible (child, TRUE);
691       gtk_widget_size_allocate (child, &child_allocation);
692
693       if (direction == GTK_TEXT_DIR_RTL)
694         child_allocation.x -= path_bar->spacing;
695       else
696         child_allocation.x += child_allocation.width + path_bar->spacing;
697     }
698   /* Now we go hide all the widgets that don't fit */
699   while (list)
700     {
701       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
702       list = list->prev;
703     }
704   for (list = first_button->next; list; list = list->next)
705     {
706       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
707     }
708
709   if (need_sliders || path_bar->fake_root)
710     {
711       child_allocation.width = path_bar->slider_width;
712       child_allocation.x = up_slider_offset + allocation->x;
713       gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
714
715       gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
716       gtk_widget_show_all (path_bar->up_slider_button);
717     }
718   else
719     gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
720       
721   if (need_sliders)
722     {
723       child_allocation.width = path_bar->slider_width;
724
725       if (direction == GTK_TEXT_DIR_RTL)
726         child_allocation.x = 0;
727       else
728         child_allocation.x = allocation->width - path_bar->slider_width;
729
730       child_allocation.x += allocation->x;
731       
732       gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
733
734       gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
735       gtk_widget_show_all (path_bar->down_slider_button);
736       gtk_path_bar_update_slider_buttons (path_bar);
737     }
738   else
739     gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
740
741   child_ordering_changed (path_bar);
742 }
743
744 static void
745 gtk_path_bar_style_updated (GtkWidget *widget)
746 {
747   GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_updated (widget);
748
749   gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
750 }
751
752 static void
753 gtk_path_bar_screen_changed (GtkWidget *widget,
754                              GdkScreen *previous_screen)
755 {
756   if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
757     GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
758
759   /* We might nave a new settings, so we remove the old one */
760   if (previous_screen)
761     remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
762
763   gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
764 }
765
766 static gboolean
767 gtk_path_bar_scroll (GtkWidget      *widget,
768                      GdkEventScroll *event)
769 {
770   switch (event->direction)
771     {
772     case GDK_SCROLL_RIGHT:
773     case GDK_SCROLL_DOWN:
774       gtk_path_bar_scroll_down (GTK_PATH_BAR (widget));
775       break;
776     case GDK_SCROLL_LEFT:
777     case GDK_SCROLL_UP:
778       gtk_path_bar_scroll_up (GTK_PATH_BAR (widget));
779       break;
780     case GDK_SCROLL_SMOOTH:
781       break;
782     }
783
784   return TRUE;
785 }
786
787 static void
788 gtk_path_bar_add (GtkContainer *container,
789                   GtkWidget    *widget)
790
791 {
792   gtk_widget_set_parent (widget, GTK_WIDGET (container));
793 }
794
795 static void
796 gtk_path_bar_remove_1 (GtkContainer *container,
797                        GtkWidget    *widget)
798 {
799   gboolean was_visible = gtk_widget_get_visible (widget);
800   gtk_widget_unparent (widget);
801   if (was_visible)
802     gtk_widget_queue_resize (GTK_WIDGET (container));
803 }
804
805 static void
806 gtk_path_bar_remove (GtkContainer *container,
807                      GtkWidget    *widget)
808 {
809   GtkPathBar *path_bar;
810   GList *children;
811
812   path_bar = GTK_PATH_BAR (container);
813
814   if (widget == path_bar->up_slider_button)
815     {
816       gtk_path_bar_remove_1 (container, widget);
817       path_bar->up_slider_button = NULL;
818       return;
819     }
820
821   if (widget == path_bar->down_slider_button)
822     {
823       gtk_path_bar_remove_1 (container, widget);
824       path_bar->down_slider_button = NULL;
825       return;
826     }
827
828   children = path_bar->button_list;
829   while (children)
830     {
831       if (widget == BUTTON_DATA (children->data)->button)
832         {
833           gtk_path_bar_remove_1 (container, widget);
834           path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
835           g_list_free (children);
836           return;
837         }
838       
839       children = children->next;
840     }
841 }
842
843 static void
844 gtk_path_bar_forall (GtkContainer *container,
845                      gboolean      include_internals,
846                      GtkCallback   callback,
847                      gpointer      callback_data)
848 {
849   GtkPathBar *path_bar;
850   GList *children;
851
852   g_return_if_fail (callback != NULL);
853   path_bar = GTK_PATH_BAR (container);
854
855   children = path_bar->button_list;
856   while (children)
857     {
858       GtkWidget *child;
859       child = BUTTON_DATA (children->data)->button;
860       children = children->next;
861
862       (* callback) (child, callback_data);
863     }
864
865   if (path_bar->up_slider_button)
866     (* callback) (path_bar->up_slider_button, callback_data);
867
868   if (path_bar->down_slider_button)
869     (* callback) (path_bar->down_slider_button, callback_data);
870 }
871
872 static GtkWidgetPath *
873 gtk_path_bar_get_path_for_child (GtkContainer *container,
874                                  GtkWidget    *child)
875 {
876   GtkPathBar *path_bar = GTK_PATH_BAR (container);
877   GtkWidgetPath *path;
878
879   path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (path_bar)));
880
881   if (gtk_widget_get_visible (child) &&
882       gtk_widget_get_child_visible (child))
883     {
884       GtkWidgetPath *sibling_path;
885       GList *visible_children;
886       GList *l;
887       int pos;
888
889       /* 1. Build the list of visible children, in visually left-to-right order
890        * (i.e. independently of the widget's direction).  Note that our
891        * button_list is stored in innermost-to-outermost path order!
892        */
893
894       visible_children = NULL;
895
896       if (gtk_widget_get_visible (path_bar->down_slider_button) &&
897           gtk_widget_get_child_visible (path_bar->down_slider_button))
898         visible_children = g_list_prepend (visible_children, path_bar->down_slider_button);
899
900       for (l = path_bar->button_list; l; l = l->next)
901         {
902           ButtonData *data = l->data;
903
904           if (gtk_widget_get_visible (data->button) &&
905               gtk_widget_get_child_visible (data->button))
906             visible_children = g_list_prepend (visible_children, data->button);
907         }
908
909       if (gtk_widget_get_visible (path_bar->up_slider_button) &&
910           gtk_widget_get_child_visible (path_bar->up_slider_button))
911         visible_children = g_list_prepend (visible_children, path_bar->up_slider_button);
912
913       if (gtk_widget_get_direction (GTK_WIDGET (path_bar)) == GTK_TEXT_DIR_RTL)
914         visible_children = g_list_reverse (visible_children);
915
916       /* 2. Find the index of the child within that list */
917
918       pos = 0;
919
920       for (l = visible_children; l; l = l->next)
921         {
922           GtkWidget *button = l->data;
923
924           if (button == child)
925             break;
926
927           pos++;
928         }
929
930       /* 3. Build the path */
931
932       sibling_path = gtk_widget_path_new ();
933
934       for (l = visible_children; l; l = l->next)
935         gtk_widget_path_append_for_widget (sibling_path, l->data);
936
937       gtk_widget_path_append_with_siblings (path, sibling_path, pos);
938
939       g_list_free (visible_children);
940       gtk_widget_path_unref (sibling_path);
941     }
942   else
943     gtk_widget_path_append_for_widget (path, child);
944
945   return path;
946 }
947
948 static void
949 gtk_path_bar_scroll_down (GtkPathBar *path_bar)
950 {
951   GtkAllocation allocation, button_allocation;
952   GList *list;
953   GList *down_button = NULL;
954   gint space_available;
955
956   if (path_bar->ignore_click)
957     {
958       path_bar->ignore_click = FALSE;
959       return;   
960     }
961
962   if (gtk_widget_get_child_visible (BUTTON_DATA (path_bar->button_list->data)->button))
963     {
964       /* Return if the last button is already visible */
965       return;
966     }
967
968   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
969
970   /* We find the button at the 'down' end that we have to make
971    * visible */
972   for (list = path_bar->button_list; list; list = list->next)
973     {
974       if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
975         {
976           down_button = list;
977           break;
978         }
979     }
980
981   gtk_widget_get_allocation (GTK_WIDGET (path_bar), &allocation);
982   gtk_widget_get_allocation (BUTTON_DATA (down_button->data)->button, &button_allocation);
983
984   space_available = (allocation.width
985                      - 2 * path_bar->spacing - 2 * path_bar->slider_width
986                      - button_allocation.width);
987   path_bar->first_scrolled_button = down_button;
988   
989   /* We have space_available free space that's not being used.  
990    * So we walk down from the end, adding buttons until we use all free space.
991    */
992   while (space_available > 0)
993     {
994       path_bar->first_scrolled_button = down_button;
995       down_button = down_button->next;
996       if (!down_button)
997         break;
998       space_available -= (button_allocation.width
999                           + path_bar->spacing);
1000     }
1001 }
1002
1003 static void
1004 gtk_path_bar_scroll_up (GtkPathBar *path_bar)
1005 {
1006   GList *list;
1007
1008   if (path_bar->ignore_click)
1009     {
1010       path_bar->ignore_click = FALSE;
1011       return;   
1012     }
1013
1014   list = g_list_last (path_bar->button_list);
1015
1016   if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
1017     {
1018       /* Return if the first button is already visible */
1019       return;
1020     }
1021
1022   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1023
1024   for ( ; list; list = list->prev)
1025     {
1026       if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
1027         {
1028           if (list->prev == path_bar->fake_root)
1029             path_bar->fake_root = NULL;
1030           path_bar->first_scrolled_button = list;
1031           return;
1032         }
1033     }
1034 }
1035
1036 static gboolean
1037 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
1038 {
1039   gboolean retval = FALSE;
1040
1041   if (path_bar->timer)
1042     {
1043       if (path_bar->scrolling_up)
1044         gtk_path_bar_scroll_up (path_bar);
1045       else if (path_bar->scrolling_down)
1046         gtk_path_bar_scroll_down (path_bar);
1047
1048       if (path_bar->need_timer) 
1049         {
1050           GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (path_bar));
1051           guint        timeout;
1052
1053           g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
1054
1055           path_bar->need_timer = FALSE;
1056
1057           path_bar->timer = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
1058                                            (GSourceFunc)gtk_path_bar_scroll_timeout,
1059                                            path_bar);
1060         }
1061       else
1062         retval = TRUE;
1063     }
1064
1065   return retval;
1066 }
1067
1068 static void 
1069 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
1070 {
1071   if (path_bar->timer)
1072     {
1073       g_source_remove (path_bar->timer);
1074       path_bar->timer = 0;
1075       path_bar->need_timer = FALSE;
1076     }
1077 }
1078
1079 static gboolean
1080 gtk_path_bar_slider_up_defocus (GtkWidget      *widget,
1081                                     GdkEventButton *event,
1082                                     GtkPathBar     *path_bar)
1083 {
1084   GList *list;
1085   GList *up_button = NULL;
1086
1087   if (event->type != GDK_FOCUS_CHANGE)
1088     return FALSE;
1089
1090   for (list = g_list_last (path_bar->button_list); list; list = list->prev)
1091     {
1092       if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
1093         {
1094           up_button = list;
1095           break;
1096         }
1097     }
1098
1099   /* don't let the focus vanish */
1100   if ((!gtk_widget_is_sensitive (path_bar->up_slider_button)) ||
1101       (!gtk_widget_get_child_visible (path_bar->up_slider_button)))
1102     gtk_widget_grab_focus (BUTTON_DATA (up_button->data)->button);
1103
1104   return FALSE;
1105 }
1106
1107 static gboolean
1108 gtk_path_bar_slider_down_defocus (GtkWidget      *widget,
1109                                     GdkEventButton *event,
1110                                     GtkPathBar     *path_bar)
1111 {
1112   GList *list;
1113   GList *down_button = NULL;
1114
1115   if (event->type != GDK_FOCUS_CHANGE)
1116     return FALSE;
1117
1118   for (list = path_bar->button_list; list; list = list->next)
1119     {
1120       if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
1121         {
1122           down_button = list;
1123           break;
1124         }
1125     }
1126
1127   /* don't let the focus vanish */
1128   if ((!gtk_widget_is_sensitive (path_bar->down_slider_button)) ||
1129       (!gtk_widget_get_child_visible (path_bar->down_slider_button)))
1130     gtk_widget_grab_focus (BUTTON_DATA (down_button->data)->button);
1131
1132   return FALSE;
1133 }
1134
1135 static gboolean
1136 gtk_path_bar_slider_button_press (GtkWidget      *widget, 
1137                                   GdkEventButton *event,
1138                                   GtkPathBar     *path_bar)
1139 {
1140   if (event->type != GDK_BUTTON_PRESS || event->button != GDK_BUTTON_PRIMARY)
1141     return FALSE;
1142
1143   path_bar->ignore_click = FALSE;
1144
1145   if (widget == path_bar->up_slider_button)
1146     {
1147       path_bar->scrolling_down = FALSE;
1148       path_bar->scrolling_up = TRUE;
1149       gtk_path_bar_scroll_up (path_bar);
1150     }
1151   else if (widget == path_bar->down_slider_button)
1152     {
1153       path_bar->scrolling_up = FALSE;
1154       path_bar->scrolling_down = TRUE;
1155       gtk_path_bar_scroll_down (path_bar);
1156     }
1157
1158   if (!path_bar->timer)
1159     {
1160       GtkSettings *settings = gtk_widget_get_settings (widget);
1161       guint        timeout;
1162
1163       g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
1164
1165       path_bar->need_timer = TRUE;
1166       path_bar->timer = gdk_threads_add_timeout (timeout,
1167                                        (GSourceFunc)gtk_path_bar_scroll_timeout,
1168                                        path_bar);
1169     }
1170
1171   return FALSE;
1172 }
1173
1174 static gboolean
1175 gtk_path_bar_slider_button_release (GtkWidget      *widget, 
1176                                     GdkEventButton *event,
1177                                     GtkPathBar     *path_bar)
1178 {
1179   if (event->type != GDK_BUTTON_RELEASE)
1180     return FALSE;
1181
1182   path_bar->ignore_click = TRUE;
1183   gtk_path_bar_stop_scrolling (path_bar);
1184
1185   return FALSE;
1186 }
1187
1188 static void
1189 gtk_path_bar_grab_notify (GtkWidget *widget,
1190                           gboolean   was_grabbed)
1191 {
1192   if (!was_grabbed)
1193     gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
1194 }
1195
1196 static void
1197 gtk_path_bar_state_changed (GtkWidget    *widget,
1198                             GtkStateType  previous_state)
1199 {
1200   if (!gtk_widget_is_sensitive (widget))
1201     gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
1202 }
1203
1204
1205 /* Changes the icons wherever it is needed */
1206 static void
1207 reload_icons (GtkPathBar *path_bar)
1208 {
1209   GList *list;
1210
1211   if (path_bar->root_icon)
1212     {
1213       g_object_unref (path_bar->root_icon);
1214       path_bar->root_icon = NULL;
1215     }
1216   if (path_bar->home_icon)
1217     {
1218       g_object_unref (path_bar->home_icon);
1219       path_bar->home_icon = NULL;
1220     }
1221   if (path_bar->desktop_icon)
1222     {
1223       g_object_unref (path_bar->desktop_icon);
1224       path_bar->desktop_icon = NULL;
1225     }
1226
1227   for (list = path_bar->button_list; list; list = list->next)
1228     {
1229       ButtonData *button_data;
1230       gboolean current_dir;
1231
1232       button_data = BUTTON_DATA (list->data);
1233       if (button_data->type != NORMAL_BUTTON)
1234         {
1235           current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
1236           gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1237         }
1238     }
1239   
1240 }
1241
1242 static void
1243 change_icon_theme (GtkPathBar *path_bar)
1244 {
1245   GtkSettings *settings;
1246   gint width, height;
1247
1248   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
1249
1250   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
1251     path_bar->icon_size = MAX (width, height);
1252   else
1253     path_bar->icon_size = FALLBACK_ICON_SIZE;
1254
1255   reload_icons (path_bar);
1256 }
1257 /* Callback used when a GtkSettings value changes */
1258 static void
1259 settings_notify_cb (GObject    *object,
1260                     GParamSpec *pspec,
1261                     GtkPathBar *path_bar)
1262 {
1263   const char *name;
1264
1265   name = g_param_spec_get_name (pspec);
1266
1267   if (! strcmp (name, "gtk-icon-theme-name") ||
1268       ! strcmp (name, "gtk-icon-sizes"))
1269     change_icon_theme (path_bar);
1270 }
1271
1272 static void
1273 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
1274 {
1275   GtkSettings *settings;
1276
1277   if (path_bar->settings_signal_id)
1278     return;
1279
1280   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
1281   path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
1282
1283   change_icon_theme (path_bar);
1284 }
1285
1286 /* Public functions and their helpers */
1287 static void
1288 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
1289 {
1290   while (path_bar->button_list != NULL)
1291     {
1292       gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
1293     }
1294   path_bar->first_scrolled_button = NULL;
1295   path_bar->fake_root = NULL;
1296 }
1297
1298 static void
1299 button_clicked_cb (GtkWidget *button,
1300                    gpointer   data)
1301 {
1302   ButtonData *button_data;
1303   GtkPathBar *path_bar;
1304   GList *button_list;
1305   gboolean child_is_hidden;
1306   GFile *child_file;
1307
1308   button_data = BUTTON_DATA (data);
1309   if (button_data->ignore_changes)
1310     return;
1311
1312   path_bar = GTK_PATH_BAR (gtk_widget_get_parent (button));
1313
1314   button_list = g_list_find (path_bar->button_list, button_data);
1315   g_assert (button_list != NULL);
1316
1317   g_signal_handlers_block_by_func (button,
1318                                    G_CALLBACK (button_clicked_cb), data);
1319   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
1320   g_signal_handlers_unblock_by_func (button,
1321                                      G_CALLBACK (button_clicked_cb), data);
1322
1323   if (button_list->prev)
1324     {
1325       ButtonData *child_data;
1326
1327       child_data = BUTTON_DATA (button_list->prev->data);
1328       child_file = child_data->file;
1329       child_is_hidden = child_data->file_is_hidden;
1330     }
1331   else
1332     {
1333       child_file = NULL;
1334       child_is_hidden = FALSE;
1335     }
1336
1337   g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0,
1338                  button_data->file, child_file, child_is_hidden);
1339 }
1340
1341 struct SetButtonImageData
1342 {
1343   GtkPathBar *path_bar;
1344   ButtonData *button_data;
1345 };
1346
1347 static void
1348 set_button_image_get_info_cb (GCancellable *cancellable,
1349                               GFileInfo    *info,
1350                               const GError *error,
1351                               gpointer      user_data)
1352 {
1353   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1354   GdkPixbuf *pixbuf;
1355   struct SetButtonImageData *data = user_data;
1356
1357   if (cancellable != data->button_data->cancellable)
1358     goto out;
1359
1360   data->button_data->cancellable = NULL;
1361
1362   if (!data->button_data->button)
1363     {
1364       g_free (data->button_data);
1365       goto out;
1366     }
1367
1368   if (cancelled || error)
1369     goto out;
1370
1371   pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (data->path_bar),
1372                                        data->path_bar->icon_size);
1373   gtk_image_set_from_pixbuf (GTK_IMAGE (data->button_data->image), pixbuf);
1374
1375   switch (data->button_data->type)
1376     {
1377       case HOME_BUTTON:
1378         if (data->path_bar->home_icon)
1379           g_object_unref (pixbuf);
1380         else
1381           data->path_bar->home_icon = pixbuf;
1382         break;
1383
1384       case DESKTOP_BUTTON:
1385         if (data->path_bar->desktop_icon)
1386           g_object_unref (pixbuf);
1387         else
1388           data->path_bar->desktop_icon = pixbuf;
1389         break;
1390
1391       default:
1392         break;
1393     };
1394
1395 out:
1396   g_free (data);
1397   g_object_unref (cancellable);
1398 }
1399
1400 static void
1401 set_button_image (GtkPathBar *path_bar,
1402                   ButtonData *button_data)
1403 {
1404   GtkFileSystemVolume *volume;
1405   struct SetButtonImageData *data;
1406
1407   switch (button_data->type)
1408     {
1409     case ROOT_BUTTON:
1410
1411       if (path_bar->root_icon != NULL)
1412         {
1413           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1414           break;
1415         }
1416
1417       volume = _gtk_file_system_get_volume_for_file (path_bar->file_system, path_bar->root_file);
1418       if (volume == NULL)
1419         return;
1420
1421       path_bar->root_icon = _gtk_file_system_volume_render_icon (volume,
1422                                                                  GTK_WIDGET (path_bar),
1423                                                                  path_bar->icon_size,
1424                                                                  NULL);
1425       _gtk_file_system_volume_unref (volume);
1426
1427       gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1428       break;
1429
1430     case HOME_BUTTON:
1431       if (path_bar->home_icon != NULL)
1432         {
1433           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->home_icon);
1434           break;
1435         }
1436
1437       data = g_new0 (struct SetButtonImageData, 1);
1438       data->path_bar = path_bar;
1439       data->button_data = button_data;
1440
1441       if (button_data->cancellable)
1442         g_cancellable_cancel (button_data->cancellable);
1443
1444       button_data->cancellable =
1445         _gtk_file_system_get_info (path_bar->file_system,
1446                                    path_bar->home_file,
1447                                    "standard::icon",
1448                                    set_button_image_get_info_cb,
1449                                    data);
1450       break;
1451
1452     case DESKTOP_BUTTON:
1453       if (path_bar->desktop_icon != NULL)
1454         {
1455           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->desktop_icon);
1456           break;
1457         }
1458
1459       data = g_new0 (struct SetButtonImageData, 1);
1460       data->path_bar = path_bar;
1461       data->button_data = button_data;
1462
1463       if (button_data->cancellable)
1464         g_cancellable_cancel (button_data->cancellable);
1465
1466       button_data->cancellable =
1467         _gtk_file_system_get_info (path_bar->file_system,
1468                                    path_bar->desktop_file,
1469                                    "standard::icon",
1470                                    set_button_image_get_info_cb,
1471                                    data);
1472       break;
1473     default:
1474       break;
1475     }
1476 }
1477
1478 static void
1479 button_data_free (ButtonData *button_data)
1480 {
1481   if (button_data->file)
1482     g_object_unref (button_data->file);
1483   button_data->file = NULL;
1484
1485   g_free (button_data->dir_name);
1486   button_data->dir_name = NULL;
1487
1488   button_data->button = NULL;
1489
1490   if (button_data->cancellable)
1491     g_cancellable_cancel (button_data->cancellable);
1492   else
1493     g_free (button_data);
1494 }
1495
1496 static const char *
1497 get_dir_name (ButtonData *button_data)
1498 {
1499   return button_data->dir_name;
1500 }
1501
1502 /* We always want to request the same size for the label, whether
1503  * or not the contents are bold
1504  */
1505 static void
1506 set_label_size_request (GtkWidget  *widget,
1507                         ButtonData *button_data)
1508 {
1509   const gchar *dir_name = get_dir_name (button_data);
1510   PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1511   gint width, height, bold_width, bold_height;
1512   gchar *markup;
1513   
1514   pango_layout_get_pixel_size (layout, &width, &height);
1515   
1516   markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1517   pango_layout_set_markup (layout, markup, -1);
1518   g_free (markup);
1519
1520   pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1521
1522   gtk_widget_set_size_request (widget,
1523                                MAX (width, bold_width),
1524                                MAX (height, bold_height));
1525   g_object_unref (layout);
1526 }
1527
1528 static void
1529 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1530                                        ButtonData *button_data,
1531                                        gboolean    current_dir)
1532 {
1533   const gchar *dir_name = get_dir_name (button_data);
1534
1535   if (button_data->label != NULL)
1536     {
1537       if (current_dir)
1538         {
1539           char *markup;
1540
1541           markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1542           gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1543           g_free (markup);
1544         }
1545       else
1546         {
1547           gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1548         }
1549     }
1550
1551   if (button_data->image != NULL)
1552     {
1553       set_button_image (path_bar, button_data);
1554     }
1555
1556   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1557     {
1558       button_data->ignore_changes = TRUE;
1559       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1560       button_data->ignore_changes = FALSE;
1561     }
1562 }
1563
1564 static ButtonType
1565 find_button_type (GtkPathBar  *path_bar,
1566                   GFile       *file)
1567 {
1568   if (path_bar->root_file != NULL &&
1569       g_file_equal (file, path_bar->root_file))
1570     return ROOT_BUTTON;
1571   if (path_bar->home_file != NULL &&
1572       g_file_equal (file, path_bar->home_file))
1573     return HOME_BUTTON;
1574   if (path_bar->desktop_file != NULL &&
1575       g_file_equal (file, path_bar->desktop_file))
1576     return DESKTOP_BUTTON;
1577
1578  return NORMAL_BUTTON;
1579 }
1580
1581 static void
1582 button_drag_data_get_cb (GtkWidget        *widget,
1583                          GdkDragContext   *context,
1584                          GtkSelectionData *selection_data,
1585                          guint             info,
1586                          guint             time_,
1587                          gpointer          data)
1588 {
1589   ButtonData *button_data;
1590   char *uris[2];
1591
1592   button_data = data;
1593
1594   uris[0] = g_file_get_uri (button_data->file);
1595   uris[1] = NULL;
1596
1597   gtk_selection_data_set_uris (selection_data, uris);
1598
1599   g_free (uris[0]);
1600 }
1601
1602 static ButtonData *
1603 make_directory_button (GtkPathBar  *path_bar,
1604                        const char  *dir_name,
1605                        GFile       *file,
1606                        gboolean     current_dir,
1607                        gboolean     file_is_hidden)
1608 {
1609   AtkObject *atk_obj;
1610   GtkWidget *child = NULL;
1611   ButtonData *button_data;
1612
1613   file_is_hidden = !! file_is_hidden;
1614   /* Is it a special button? */
1615   button_data = g_new0 (ButtonData, 1);
1616
1617   button_data->type = find_button_type (path_bar, file);
1618   button_data->button = gtk_toggle_button_new ();
1619   atk_obj = gtk_widget_get_accessible (button_data->button);
1620   gtk_button_set_focus_on_click (GTK_BUTTON (button_data->button), FALSE);
1621   gtk_widget_add_events (button_data->button, GDK_SCROLL_MASK);
1622
1623   switch (button_data->type)
1624     {
1625     case ROOT_BUTTON:
1626       button_data->image = gtk_image_new ();
1627       child = button_data->image;
1628       button_data->label = NULL;
1629       atk_object_set_name (atk_obj, _("File System Root"));
1630       break;
1631     case HOME_BUTTON:
1632     case DESKTOP_BUTTON:
1633       button_data->image = gtk_image_new ();
1634       button_data->label = gtk_label_new (NULL);
1635       child = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
1636       gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1637       gtk_box_pack_start (GTK_BOX (child), button_data->label, FALSE, FALSE, 0);
1638       break;
1639     case NORMAL_BUTTON:
1640     default:
1641       button_data->label = gtk_label_new (NULL);
1642       gtk_label_set_ellipsize (GTK_LABEL (button_data->label), PANGO_ELLIPSIZE_END);
1643       child = button_data->label;
1644       button_data->image = NULL;
1645     }
1646
1647   button_data->dir_name = g_strdup (dir_name);
1648   button_data->file = g_object_ref (file);
1649   button_data->file_is_hidden = file_is_hidden;
1650
1651   /*
1652    * The following function ensures that the alignment will always
1653    * request the same size whether the button's text is bold or not.
1654    */
1655   if (button_data->label)
1656     set_label_size_request (button_data->label, button_data);
1657
1658   gtk_container_add (GTK_CONTAINER (button_data->button), child);
1659   gtk_widget_show_all (button_data->button);
1660
1661   gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1662
1663   g_signal_connect (button_data->button, "clicked",
1664                     G_CALLBACK (button_clicked_cb),
1665                     button_data);
1666   g_object_weak_ref (G_OBJECT (button_data->button),
1667                      (GWeakNotify) button_data_free, button_data);
1668
1669   gtk_drag_source_set (button_data->button,
1670                        GDK_BUTTON1_MASK,
1671                        NULL, 0,
1672                        GDK_ACTION_COPY);
1673   gtk_drag_source_add_uri_targets (button_data->button);
1674   g_signal_connect (button_data->button, "drag-data-get",
1675                     G_CALLBACK (button_drag_data_get_cb), button_data);
1676
1677   return button_data;
1678 }
1679
1680 static gboolean
1681 gtk_path_bar_check_parent_path (GtkPathBar         *path_bar,
1682                                 GFile              *file)
1683 {
1684   GList *list;
1685   GList *current_path = NULL;
1686   gboolean need_new_fake_root = FALSE;
1687
1688   for (list = path_bar->button_list; list; list = list->next)
1689     {
1690       ButtonData *button_data;
1691
1692       button_data = list->data;
1693       if (g_file_equal (file, button_data->file))
1694         {
1695           current_path = list;
1696           break;
1697         }
1698       if (list == path_bar->fake_root)
1699         need_new_fake_root = TRUE;
1700     }
1701
1702   if (current_path)
1703     {
1704       if (need_new_fake_root)
1705         {
1706           path_bar->fake_root = NULL;
1707           for (list = current_path; list; list = list->next)
1708             {
1709               ButtonData *button_data;
1710
1711               button_data = list->data;
1712               if (BUTTON_IS_FAKE_ROOT (button_data))
1713                 {
1714                   path_bar->fake_root = list;
1715                   break;
1716                 }
1717             }
1718         }
1719
1720       for (list = path_bar->button_list; list; list = list->next)
1721         {
1722           gtk_path_bar_update_button_appearance (path_bar,
1723                                                  BUTTON_DATA (list->data),
1724                                                  (list == current_path) ? TRUE : FALSE);
1725         }
1726
1727       if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1728         {
1729           path_bar->first_scrolled_button = current_path;
1730           gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1731         }
1732
1733       return TRUE;
1734     }
1735   return FALSE;
1736 }
1737
1738
1739 struct SetFileInfo
1740 {
1741   GFile *file;
1742   GFile *parent_file;
1743   GtkPathBar *path_bar;
1744   GList *new_buttons;
1745   GList *fake_root;
1746   gboolean first_directory;
1747 };
1748
1749 static void
1750 gtk_path_bar_set_file_finish (struct SetFileInfo *info,
1751                               gboolean            result)
1752 {
1753   if (result)
1754     {
1755       GList *l;
1756
1757       gtk_path_bar_clear_buttons (info->path_bar);
1758       info->path_bar->button_list = g_list_reverse (info->new_buttons);
1759       info->path_bar->fake_root = info->fake_root;
1760
1761       for (l = info->path_bar->button_list; l; l = l->next)
1762         {
1763           GtkWidget *button = BUTTON_DATA (l->data)->button;
1764           gtk_container_add (GTK_CONTAINER (info->path_bar), button);
1765         }
1766
1767       child_ordering_changed (info->path_bar);
1768     }
1769   else
1770     {
1771       GList *l;
1772
1773       for (l = info->new_buttons; l; l = l->next)
1774         {
1775           ButtonData *button_data;
1776
1777           button_data = BUTTON_DATA (l->data);
1778           gtk_widget_destroy (button_data->button);
1779         }
1780
1781       g_list_free (info->new_buttons);
1782     }
1783
1784   if (info->file)
1785     g_object_unref (info->file);
1786   if (info->parent_file)
1787     g_object_unref (info->parent_file);
1788
1789   g_free (info);
1790 }
1791
1792 static void
1793 gtk_path_bar_get_info_callback (GCancellable *cancellable,
1794                                 GFileInfo    *info,
1795                                 const GError *error,
1796                                 gpointer      data)
1797 {
1798   gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1799   struct SetFileInfo *file_info = data;
1800   ButtonData *button_data;
1801   const gchar *display_name;
1802   gboolean is_hidden;
1803
1804   if (cancellable != file_info->path_bar->get_info_cancellable)
1805     {
1806       gtk_path_bar_set_file_finish (file_info, FALSE);
1807       g_object_unref (cancellable);
1808       return;
1809     }
1810
1811   g_object_unref (cancellable);
1812   file_info->path_bar->get_info_cancellable = NULL;
1813
1814   if (cancelled || !info)
1815     {
1816       gtk_path_bar_set_file_finish (file_info, FALSE);
1817       return;
1818     }
1819
1820   display_name = g_file_info_get_display_name (info);
1821   is_hidden = g_file_info_get_is_hidden (info) || g_file_info_get_is_backup (info);
1822
1823   gtk_widget_push_composite_child ();
1824   button_data = make_directory_button (file_info->path_bar, display_name,
1825                                        file_info->file,
1826                                        file_info->first_directory, is_hidden);
1827   gtk_widget_pop_composite_child ();
1828   g_object_unref (file_info->file);
1829
1830   file_info->new_buttons = g_list_prepend (file_info->new_buttons, button_data);
1831
1832   if (BUTTON_IS_FAKE_ROOT (button_data))
1833     file_info->fake_root = file_info->new_buttons;
1834
1835   /* We have assigned the info for the innermost button, i.e. the deepest directory.
1836    * Now, go on to fetch the info for this directory's parent.
1837    */
1838
1839   file_info->file = file_info->parent_file;
1840   file_info->first_directory = FALSE;
1841
1842   if (!file_info->file)
1843     {
1844       /* No parent?  Okay, we are done. */
1845       gtk_path_bar_set_file_finish (file_info, TRUE);
1846       return;
1847     }
1848
1849   file_info->parent_file = g_file_get_parent (file_info->file);
1850
1851   /* Recurse asynchronously */
1852   file_info->path_bar->get_info_cancellable =
1853     _gtk_file_system_get_info (file_info->path_bar->file_system,
1854                                file_info->file,
1855                                "standard::display-name,standard::is-hidden,standard::is-backup",
1856                                gtk_path_bar_get_info_callback,
1857                                file_info);
1858 }
1859
1860 void
1861 _gtk_path_bar_set_file (GtkPathBar      *path_bar,
1862                         GFile           *file,
1863                         const gboolean   keep_trail)
1864 {
1865   struct SetFileInfo *info;
1866
1867   g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1868   g_return_if_fail (G_IS_FILE (file));
1869
1870   /* Check whether the new path is already present in the pathbar as buttons.
1871    * This could be a parent directory or a previous selected subdirectory.
1872    */
1873   if (keep_trail && gtk_path_bar_check_parent_path (path_bar, file))
1874     return;
1875
1876   info = g_new0 (struct SetFileInfo, 1);
1877   info->file = g_object_ref (file);
1878   info->path_bar = path_bar;
1879   info->first_directory = TRUE;
1880   info->parent_file = g_file_get_parent (info->file);
1881
1882   if (path_bar->get_info_cancellable)
1883     g_cancellable_cancel (path_bar->get_info_cancellable);
1884
1885   path_bar->get_info_cancellable =
1886     _gtk_file_system_get_info (path_bar->file_system,
1887                                info->file,
1888                                "standard::display-name,standard::is-hidden,standard::is-backup",
1889                                gtk_path_bar_get_info_callback,
1890                                info);
1891 }
1892
1893 /* FIXME: This should be a construct-only property */
1894 void
1895 _gtk_path_bar_set_file_system (GtkPathBar    *path_bar,
1896                                GtkFileSystem *file_system)
1897 {
1898   const char *home;
1899
1900   g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1901
1902   g_assert (path_bar->file_system == NULL);
1903
1904   path_bar->file_system = g_object_ref (file_system);
1905
1906   home = g_get_home_dir ();
1907   if (home != NULL)
1908     {
1909       const gchar *desktop;
1910
1911       path_bar->home_file = g_file_new_for_path (home);
1912       /* FIXME: Need file system backend specific way of getting the
1913        * Desktop path.
1914        */
1915       desktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1916       if (desktop != NULL)
1917         path_bar->desktop_file = g_file_new_for_path (desktop);
1918       else 
1919         path_bar->desktop_file = NULL;
1920     }
1921   else
1922     {
1923       path_bar->home_file = NULL;
1924       path_bar->desktop_file = NULL;
1925     }
1926   path_bar->root_file = g_file_new_for_path ("/");
1927 }
1928
1929 /**
1930  * _gtk_path_bar_up:
1931  * @path_bar: a #GtkPathBar
1932  * 
1933  * If the selected button in the pathbar is not the furthest button "up" (in the
1934  * root direction), act as if the user clicked on the next button up.
1935  **/
1936 void
1937 _gtk_path_bar_up (GtkPathBar *path_bar)
1938 {
1939   GList *l;
1940
1941   for (l = path_bar->button_list; l; l = l->next)
1942     {
1943       GtkWidget *button = BUTTON_DATA (l->data)->button;
1944       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1945         {
1946           if (l->next)
1947             {
1948               GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1949               button_clicked_cb (next_button, l->next->data);
1950             }
1951           break;
1952         }
1953     }
1954 }
1955
1956 /**
1957  * _gtk_path_bar_down:
1958  * @path_bar: a #GtkPathBar
1959  * 
1960  * If the selected button in the pathbar is not the furthest button "down" (in the
1961  * leaf direction), act as if the user clicked on the next button down.
1962  **/
1963 void
1964 _gtk_path_bar_down (GtkPathBar *path_bar)
1965 {
1966   GList *l;
1967
1968   for (l = path_bar->button_list; l; l = l->next)
1969     {
1970       GtkWidget *button = BUTTON_DATA (l->data)->button;
1971       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1972         {
1973           if (l->prev)
1974             {
1975               GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1976               button_clicked_cb (prev_button, l->prev->data);
1977             }
1978           break;
1979         }
1980     }
1981 }