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