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