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