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