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