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