]> Pileus Git - ~andy/gtk/blob - gtk/gtkpathbar.c
fdda4f9308f8fcc7dcb4d2e804bc89a793807026
[~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   space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
726   if (direction == GTK_TEXT_DIR_RTL)
727     space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
728   else
729     space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
730       (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
731
732   /* We have space_available extra space that's not being used.  We
733    * need space_needed space to make the button fit.  So we walk down
734    * from the end, removing buttons until we get all the space we
735    * need. */
736   while (space_available < space_needed)
737     {
738       space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
739       up_button = up_button->prev;
740       path_bar->first_scrolled_button = up_button;
741     }
742 }
743
744 static void
745 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
746 {
747   GList *list;
748
749   if (path_bar->ignore_click)
750     {
751       path_bar->ignore_click = FALSE;
752       return;   
753     }
754
755   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
756
757   for (list = g_list_last (path_bar->button_list); list; list = list->prev)
758     {
759       if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
760         {
761           if (list->prev == path_bar->fake_root)
762             path_bar->fake_root = NULL;
763           path_bar->first_scrolled_button = list;
764           return;
765         }
766     }
767 }
768
769 static gboolean
770 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
771 {
772   gboolean retval = FALSE;
773
774   if (path_bar->timer)
775     {
776       if (path_bar->scrolling_up)
777         gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
778       else if (path_bar->scrolling_down)
779         gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
780
781       if (path_bar->need_timer) 
782         {
783           GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (path_bar));
784           guint        timeout;
785
786           g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
787
788           path_bar->need_timer = FALSE;
789
790           path_bar->timer = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
791                                            (GSourceFunc)gtk_path_bar_scroll_timeout,
792                                            path_bar);
793         }
794       else
795         retval = TRUE;
796     }
797
798   return retval;
799 }
800
801 static void 
802 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
803 {
804   if (path_bar->timer)
805     {
806       g_source_remove (path_bar->timer);
807       path_bar->timer = 0;
808       path_bar->need_timer = FALSE;
809     }
810 }
811
812 static gboolean
813 gtk_path_bar_slider_up_defocus (GtkWidget      *widget,
814                                     GdkEventButton *event,
815                                     GtkPathBar     *path_bar)
816 {
817   GList *list;
818   GList *up_button = NULL;
819
820   if (event->type != GDK_FOCUS_CHANGE)
821     return FALSE;
822
823   for (list = g_list_last (path_bar->button_list); list; list = list->prev)
824     {
825       if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
826         {
827           up_button = list;
828           break;
829         }
830     }
831
832   /* don't let the focus vanish */
833   if ((!GTK_WIDGET_IS_SENSITIVE (path_bar->up_slider_button)) || 
834       (!gtk_widget_get_child_visible (path_bar->up_slider_button)))
835     gtk_widget_grab_focus (BUTTON_DATA (up_button->data)->button);
836
837   return FALSE;
838 }
839
840 static gboolean
841 gtk_path_bar_slider_down_defocus (GtkWidget      *widget,
842                                     GdkEventButton *event,
843                                     GtkPathBar     *path_bar)
844 {
845   GList *list;
846   GList *down_button = NULL;
847
848   if (event->type != GDK_FOCUS_CHANGE)
849     return FALSE;
850
851   for (list = path_bar->button_list; list; list = list->next)
852     {
853       if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
854         {
855           down_button = list;
856           break;
857         }
858     }
859
860   /* don't let the focus vanish */
861   if ((!GTK_WIDGET_IS_SENSITIVE (path_bar->down_slider_button)) || 
862       (!gtk_widget_get_child_visible (path_bar->down_slider_button)))
863     gtk_widget_grab_focus (BUTTON_DATA (down_button->data)->button);
864
865   return FALSE;
866 }
867
868 static gboolean
869 gtk_path_bar_slider_button_press (GtkWidget      *widget, 
870                                   GdkEventButton *event,
871                                   GtkPathBar     *path_bar)
872 {
873   if (event->type != GDK_BUTTON_PRESS || event->button != 1)
874     return FALSE;
875
876   path_bar->ignore_click = FALSE;
877
878   if (widget == path_bar->up_slider_button)
879     {
880       path_bar->scrolling_down = FALSE;
881       path_bar->scrolling_up = TRUE;
882       gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
883     }
884   else if (widget == path_bar->down_slider_button)
885     {
886       path_bar->scrolling_up = FALSE;
887       path_bar->scrolling_down = TRUE;
888       gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
889     }
890
891   if (!path_bar->timer)
892     {
893       GtkSettings *settings = gtk_widget_get_settings (widget);
894       guint        timeout;
895
896       g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
897
898       path_bar->need_timer = TRUE;
899       path_bar->timer = gdk_threads_add_timeout (timeout,
900                                        (GSourceFunc)gtk_path_bar_scroll_timeout,
901                                        path_bar);
902     }
903
904   return FALSE;
905 }
906
907 static gboolean
908 gtk_path_bar_slider_button_release (GtkWidget      *widget, 
909                                     GdkEventButton *event,
910                                     GtkPathBar     *path_bar)
911 {
912   if (event->type != GDK_BUTTON_RELEASE)
913     return FALSE;
914
915   path_bar->ignore_click = TRUE;
916   gtk_path_bar_stop_scrolling (path_bar);
917
918   return FALSE;
919 }
920
921 static void
922 gtk_path_bar_grab_notify (GtkWidget *widget,
923                           gboolean   was_grabbed)
924 {
925   if (!was_grabbed)
926     gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
927 }
928
929 static void
930 gtk_path_bar_state_changed (GtkWidget    *widget,
931                             GtkStateType  previous_state)
932 {
933   if (!GTK_WIDGET_IS_SENSITIVE (widget)) 
934     gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
935 }
936
937
938 /* Changes the icons wherever it is needed */
939 static void
940 reload_icons (GtkPathBar *path_bar)
941 {
942   GList *list;
943
944   if (path_bar->root_icon)
945     {
946       g_object_unref (path_bar->root_icon);
947       path_bar->root_icon = NULL;
948     }
949   if (path_bar->home_icon)
950     {
951       g_object_unref (path_bar->home_icon);
952       path_bar->home_icon = NULL;
953     }
954   if (path_bar->desktop_icon)
955     {
956       g_object_unref (path_bar->desktop_icon);
957       path_bar->desktop_icon = NULL;
958     }
959
960   for (list = path_bar->button_list; list; list = list->next)
961     {
962       ButtonData *button_data;
963       gboolean current_dir;
964
965       button_data = BUTTON_DATA (list->data);
966       if (button_data->type != NORMAL_BUTTON)
967         {
968           current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
969           gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
970         }
971     }
972   
973 }
974
975 static void
976 change_icon_theme (GtkPathBar *path_bar)
977 {
978   GtkSettings *settings;
979   gint width, height;
980
981   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
982
983   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
984     path_bar->icon_size = MAX (width, height);
985   else
986     path_bar->icon_size = FALLBACK_ICON_SIZE;
987
988   reload_icons (path_bar);
989 }
990 /* Callback used when a GtkSettings value changes */
991 static void
992 settings_notify_cb (GObject    *object,
993                     GParamSpec *pspec,
994                     GtkPathBar *path_bar)
995 {
996   const char *name;
997
998   name = g_param_spec_get_name (pspec);
999
1000   if (! strcmp (name, "gtk-icon-theme-name") ||
1001       ! strcmp (name, "gtk-icon-sizes"))
1002     change_icon_theme (path_bar);
1003 }
1004
1005 static void
1006 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
1007 {
1008   GtkSettings *settings;
1009
1010   if (path_bar->settings_signal_id)
1011     return;
1012
1013   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
1014   path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
1015
1016   change_icon_theme (path_bar);
1017 }
1018
1019 /* Public functions and their helpers */
1020 static void
1021 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
1022 {
1023   while (path_bar->button_list != NULL)
1024     {
1025       gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
1026     }
1027   path_bar->first_scrolled_button = NULL;
1028   path_bar->fake_root = NULL;
1029 }
1030
1031 static void
1032 button_clicked_cb (GtkWidget *button,
1033                    gpointer   data)
1034 {
1035   ButtonData *button_data;
1036   GtkPathBar *path_bar;
1037   GList *button_list;
1038   gboolean child_is_hidden;
1039   GtkFilePath *child_path;
1040
1041   button_data = BUTTON_DATA (data);
1042   if (button_data->ignore_changes)
1043     return;
1044
1045   path_bar = GTK_PATH_BAR (button->parent);
1046
1047   button_list = g_list_find (path_bar->button_list, button_data);
1048   g_assert (button_list != NULL);
1049
1050   g_signal_handlers_block_by_func (button,
1051                                    G_CALLBACK (button_clicked_cb), data);
1052   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
1053   g_signal_handlers_unblock_by_func (button,
1054                                      G_CALLBACK (button_clicked_cb), data);
1055
1056   if (button_list->prev)
1057     {
1058       ButtonData *child_data;
1059
1060       child_data = BUTTON_DATA (button_list->prev->data);
1061       child_path = child_data->path;
1062       child_is_hidden = child_data->file_is_hidden;
1063     }
1064   else
1065     {
1066       child_path = NULL;
1067       child_is_hidden = FALSE;
1068     }
1069
1070   g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0,
1071                  button_data->path, child_path, child_is_hidden);
1072 }
1073
1074 struct SetButtonImageData
1075 {
1076   GtkPathBar *path_bar;
1077   ButtonData *button_data;
1078 };
1079
1080 static void
1081 set_button_image_get_info_cb (GtkFileSystemHandle *handle,
1082                               const GtkFileInfo   *info,
1083                               const GError        *error,
1084                               gpointer             user_data)
1085 {
1086   gboolean cancelled = handle->cancelled;
1087   GdkPixbuf *pixbuf;
1088   struct SetButtonImageData *data = user_data;
1089
1090   if (handle != data->button_data->handle)
1091     goto out;
1092
1093   data->button_data->handle = NULL;
1094
1095   if (!data->button_data->button)
1096     {
1097       g_free (data->button_data);
1098       goto out;
1099     }
1100
1101   if (cancelled || error)
1102     goto out;
1103
1104   pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->path_bar),
1105                                       data->path_bar->icon_size, NULL);
1106   gtk_image_set_from_pixbuf (GTK_IMAGE (data->button_data->image), pixbuf);
1107
1108   switch (data->button_data->type)
1109     {
1110       case HOME_BUTTON:
1111         if (data->path_bar->home_icon)
1112           g_object_unref (pixbuf);
1113         else
1114           data->path_bar->home_icon = pixbuf;
1115         break;
1116
1117       case DESKTOP_BUTTON:
1118         if (data->path_bar->desktop_icon)
1119           g_object_unref (pixbuf);
1120         else
1121           data->path_bar->desktop_icon = pixbuf;
1122         break;
1123
1124       default:
1125         break;
1126     };
1127
1128 out:
1129   g_free (data);
1130   g_object_unref (handle);
1131 }
1132
1133 static void
1134 set_button_image (GtkPathBar *path_bar,
1135                   ButtonData *button_data)
1136 {
1137   GtkFileSystemVolume *volume;
1138   struct SetButtonImageData *data;
1139
1140   switch (button_data->type)
1141     {
1142     case ROOT_BUTTON:
1143
1144       if (path_bar->root_icon != NULL)
1145         {
1146           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1147           break;
1148         }
1149       
1150       volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
1151       if (volume == NULL)
1152         return;
1153
1154       path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
1155                                                                 volume,
1156                                                                 GTK_WIDGET (path_bar),
1157                                                                 path_bar->icon_size,
1158                                                                 NULL);
1159       gtk_file_system_volume_free (path_bar->file_system, volume);
1160
1161       gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1162       break;
1163
1164     case HOME_BUTTON:
1165       if (path_bar->home_icon != NULL)
1166         {
1167           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->home_icon);
1168           break;
1169         }
1170
1171       data = g_new0 (struct SetButtonImageData, 1);
1172       data->path_bar = path_bar;
1173       data->button_data = button_data;
1174
1175       if (button_data->handle)
1176         gtk_file_system_cancel_operation (button_data->handle);
1177
1178       button_data->handle =
1179         gtk_file_system_get_info (path_bar->file_system,
1180                                   path_bar->home_path,
1181                                   GTK_FILE_INFO_ICON,
1182                                   set_button_image_get_info_cb,
1183                                   data);
1184       break;
1185
1186     case DESKTOP_BUTTON:
1187       if (path_bar->desktop_icon != NULL)
1188         {
1189           gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->desktop_icon);
1190           break;
1191         }
1192
1193       data = g_new0 (struct SetButtonImageData, 1);
1194       data->path_bar = path_bar;
1195       data->button_data = button_data;
1196
1197       if (button_data->handle)
1198         gtk_file_system_cancel_operation (button_data->handle);
1199
1200       button_data->handle =
1201         gtk_file_system_get_info (path_bar->file_system,
1202                                   path_bar->desktop_path,
1203                                   GTK_FILE_INFO_ICON,
1204                                   set_button_image_get_info_cb,
1205                                   data);
1206       break;
1207     default:
1208       break;
1209     }
1210 }
1211
1212 static void
1213 button_data_free (ButtonData *button_data)
1214 {
1215   if (button_data->path)
1216     gtk_file_path_free (button_data->path);
1217   button_data->path = NULL;
1218
1219   g_free (button_data->dir_name);
1220   button_data->dir_name = NULL;
1221
1222   button_data->button = NULL;
1223
1224   if (button_data->handle)
1225     gtk_file_system_cancel_operation (button_data->handle);
1226   else
1227     g_free (button_data);
1228 }
1229
1230 static const char *
1231 get_dir_name (ButtonData *button_data)
1232 {
1233   return button_data->dir_name;
1234 }
1235
1236 /* We always want to request the same size for the label, whether
1237  * or not the contents are bold
1238  */
1239 static void
1240 label_size_request_cb (GtkWidget      *widget,
1241                        GtkRequisition *requisition,
1242                        ButtonData     *button_data)
1243 {
1244   const gchar *dir_name = get_dir_name (button_data);
1245   PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1246   gint bold_width, bold_height;
1247   gchar *markup;
1248
1249   pango_layout_get_pixel_size (layout, &requisition->width, &requisition->height);
1250   
1251   markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1252   pango_layout_set_markup (layout, markup, -1);
1253   g_free (markup);
1254
1255   pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1256   requisition->width = MAX (requisition->width, bold_width);
1257   requisition->height = MAX (requisition->height, bold_height);
1258   
1259   g_object_unref (layout);
1260 }
1261
1262 static void
1263 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1264                                        ButtonData *button_data,
1265                                        gboolean    current_dir)
1266 {
1267   const gchar *dir_name = get_dir_name (button_data);
1268
1269   if (button_data->label != NULL)
1270     {
1271       if (current_dir)
1272         {
1273           char *markup;
1274
1275           markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1276           gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1277           g_free (markup);
1278         }
1279       else
1280         {
1281           gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1282         }
1283     }
1284
1285   if (button_data->image != NULL)
1286     {
1287       set_button_image (path_bar, button_data);
1288     }
1289
1290   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1291     {
1292       button_data->ignore_changes = TRUE;
1293       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1294       button_data->ignore_changes = FALSE;
1295     }
1296 }
1297
1298 static ButtonType
1299 find_button_type (GtkPathBar  *path_bar,
1300                   GtkFilePath *path)
1301 {
1302   if (path_bar->root_path != NULL &&
1303       ! gtk_file_path_compare (path, path_bar->root_path))
1304     return ROOT_BUTTON;
1305   if (path_bar->home_path != NULL &&
1306       ! gtk_file_path_compare (path, path_bar->home_path))
1307     return HOME_BUTTON;
1308   if (path_bar->desktop_path != NULL &&
1309       ! gtk_file_path_compare (path, path_bar->desktop_path))
1310     return DESKTOP_BUTTON;
1311
1312  return NORMAL_BUTTON;
1313 }
1314
1315 static void
1316 button_drag_data_get_cb (GtkWidget          *widget,
1317                          GdkDragContext     *context,
1318                          GtkSelectionData   *selection_data,
1319                          guint               info,
1320                          guint               time_,
1321                          gpointer            data)
1322 {
1323   ButtonData *button_data;
1324   GtkPathBar *path_bar;
1325   char *uri;
1326   char *uri_list;
1327
1328   button_data = data;
1329   path_bar = GTK_PATH_BAR (widget->parent); /* the button's parent *is* the path bar */
1330
1331   uri = gtk_file_system_path_to_uri (path_bar->file_system, button_data->path);
1332   uri_list = g_strconcat (uri, "\r\n", NULL);
1333   g_free (uri);
1334
1335   gtk_selection_data_set (selection_data,
1336                           selection_data->target,
1337                           8,
1338                           (guchar *)uri_list,
1339                           strlen (uri_list));
1340   g_free (uri_list);
1341 }
1342
1343 static ButtonData *
1344 make_directory_button (GtkPathBar  *path_bar,
1345                        const char  *dir_name,
1346                        GtkFilePath *path,
1347                        gboolean     current_dir,
1348                        gboolean     file_is_hidden)
1349 {
1350   const GtkTargetEntry targets[] = {
1351     { "text/uri-list", 0, 0 }
1352   };
1353
1354   AtkObject *atk_obj;
1355   GtkWidget *child = NULL;
1356   GtkWidget *label_alignment = NULL;
1357   ButtonData *button_data;
1358
1359   file_is_hidden = !! file_is_hidden;
1360   /* Is it a special button? */
1361   button_data = g_new0 (ButtonData, 1);
1362
1363   button_data->type = find_button_type (path_bar, path);
1364   button_data->button = gtk_toggle_button_new ();
1365   atk_obj = gtk_widget_get_accessible (button_data->button);
1366   gtk_button_set_focus_on_click (GTK_BUTTON (button_data->button), FALSE);
1367
1368   switch (button_data->type)
1369     {
1370     case ROOT_BUTTON:
1371       button_data->image = gtk_image_new ();
1372       child = button_data->image;
1373       button_data->label = gtk_label_new (NULL);
1374       atk_object_set_name (atk_obj, _("File System Root"));
1375       break;
1376     case HOME_BUTTON:
1377     case DESKTOP_BUTTON:
1378       button_data->image = gtk_image_new ();
1379       button_data->label = gtk_label_new (NULL);
1380       label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1381       gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1382       child = gtk_hbox_new (FALSE, 2);
1383       gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1384       gtk_box_pack_start (GTK_BOX (child), label_alignment, FALSE, FALSE, 0);
1385       break;
1386     case NORMAL_BUTTON:
1387     default:
1388       button_data->label = gtk_label_new (NULL);
1389       label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1390       gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1391       child = label_alignment;
1392       button_data->image = NULL;
1393     }
1394
1395   /* label_alignment is created because we can't override size-request
1396    * on label itself and still have the contents of the label centered
1397    * properly in the label's requisition
1398    */
1399   if (label_alignment)
1400     g_signal_connect (label_alignment, "size-request",
1401                       G_CALLBACK (label_size_request_cb), button_data);
1402
1403   button_data->dir_name = g_strdup (dir_name);
1404   button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
1405   button_data->file_is_hidden = file_is_hidden;
1406                           
1407   gtk_container_add (GTK_CONTAINER (button_data->button), child);
1408   gtk_widget_show_all (button_data->button);
1409
1410   gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1411
1412   g_signal_connect (button_data->button, "clicked",
1413                     G_CALLBACK (button_clicked_cb),
1414                     button_data);
1415   g_object_weak_ref (G_OBJECT (button_data->button),
1416                      (GWeakNotify) button_data_free, button_data);
1417
1418   gtk_drag_source_set (button_data->button,
1419                        GDK_BUTTON1_MASK,
1420                        targets,
1421                        G_N_ELEMENTS (targets),
1422                        GDK_ACTION_COPY);
1423   g_signal_connect (button_data->button, "drag_data_get",
1424                     G_CALLBACK (button_drag_data_get_cb), button_data);
1425
1426   return button_data;
1427 }
1428
1429 static gboolean
1430 gtk_path_bar_check_parent_path (GtkPathBar         *path_bar,
1431                                 const GtkFilePath  *file_path,
1432                                 GtkFileSystem      *file_system)
1433 {
1434   GList *list;
1435   GList *current_path = NULL;
1436   gboolean need_new_fake_root = FALSE;
1437
1438   for (list = path_bar->button_list; list; list = list->next)
1439     {
1440       ButtonData *button_data;
1441
1442       button_data = list->data;
1443       if (! gtk_file_path_compare (file_path, button_data->path))
1444         {
1445           current_path = list;
1446           break;
1447         }
1448       if (list == path_bar->fake_root)
1449         need_new_fake_root = TRUE;
1450     }
1451
1452   if (current_path)
1453     {
1454       if (need_new_fake_root)
1455         {
1456           path_bar->fake_root = NULL;
1457           for (list = current_path; list; list = list->next)
1458             {
1459               ButtonData *button_data;
1460
1461               button_data = list->data;
1462               if (BUTTON_IS_FAKE_ROOT (button_data))
1463                 {
1464                   path_bar->fake_root = list;
1465                   break;
1466                 }
1467             }
1468         }
1469
1470       for (list = path_bar->button_list; list; list = list->next)
1471         {
1472           gtk_path_bar_update_button_appearance (path_bar,
1473                                                  BUTTON_DATA (list->data),
1474                                                  (list == current_path) ? TRUE : FALSE);
1475         }
1476
1477       if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1478         {
1479           path_bar->first_scrolled_button = current_path;
1480           gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1481         }
1482
1483       return TRUE;
1484     }
1485   return FALSE;
1486 }
1487
1488
1489 struct SetPathInfo
1490 {
1491   GtkFilePath *path;
1492   GtkFilePath *parent_path;
1493   GtkPathBar *path_bar;
1494   GList *new_buttons;
1495   GList *fake_root;
1496   gboolean first_directory;
1497 };
1498
1499 static void
1500 gtk_path_bar_set_path_finish (struct SetPathInfo *info,
1501                               gboolean result)
1502 {
1503   if (result)
1504     {
1505       GList *l;
1506
1507       gtk_path_bar_clear_buttons (info->path_bar);
1508       info->path_bar->button_list = g_list_reverse (info->new_buttons);
1509       info->path_bar->fake_root = info->fake_root;
1510
1511       for (l = info->path_bar->button_list; l; l = l->next)
1512         {
1513           GtkWidget *button = BUTTON_DATA (l->data)->button;
1514           gtk_container_add (GTK_CONTAINER (info->path_bar), button);
1515         }
1516     }
1517   else
1518     {
1519       GList *l;
1520
1521       for (l = info->new_buttons; l; l = l->next)
1522         {
1523           ButtonData *button_data;
1524
1525           button_data = BUTTON_DATA (l->data);
1526           gtk_widget_destroy (button_data->button);
1527         }
1528
1529       g_list_free (info->new_buttons);
1530     }
1531
1532   if (info->path)
1533     gtk_file_path_free (info->path);
1534   if (info->parent_path)
1535     gtk_file_path_free (info->parent_path);
1536   g_free (info);
1537 }
1538
1539 static void
1540 gtk_path_bar_get_info_callback (GtkFileSystemHandle *handle,
1541                                 const GtkFileInfo   *file_info,
1542                                 const GError        *error,
1543                                 gpointer             data)
1544 {
1545   gboolean cancelled = handle->cancelled;
1546   struct SetPathInfo *path_info = data;
1547   ButtonData *button_data;
1548   const gchar *display_name;
1549   gboolean is_hidden;
1550   gboolean valid;
1551
1552   if (handle != path_info->path_bar->set_path_handle)
1553     {
1554       gtk_path_bar_set_path_finish (path_info, FALSE);
1555       g_object_unref (handle);
1556       return;
1557     }
1558
1559   g_object_unref (handle);
1560   path_info->path_bar->set_path_handle = NULL;
1561
1562   if (cancelled || !file_info)
1563     {
1564       gtk_path_bar_set_path_finish (path_info, FALSE);
1565       return;
1566     }
1567
1568   display_name = gtk_file_info_get_display_name (file_info);
1569   is_hidden = gtk_file_info_get_is_hidden (file_info);
1570
1571   gtk_widget_push_composite_child ();
1572   button_data = make_directory_button (path_info->path_bar, display_name,
1573                                        path_info->path,
1574                                        path_info->first_directory, is_hidden);
1575   gtk_widget_pop_composite_child ();
1576   gtk_file_path_free (path_info->path);
1577
1578   path_info->new_buttons = g_list_prepend (path_info->new_buttons, button_data);
1579
1580   if (BUTTON_IS_FAKE_ROOT (button_data))
1581     path_info->fake_root = path_info->new_buttons;
1582
1583   path_info->path = path_info->parent_path;
1584   path_info->first_directory = FALSE;
1585
1586   if (!path_info->path)
1587     {
1588       gtk_path_bar_set_path_finish (path_info, TRUE);
1589       return;
1590     }
1591
1592   valid = gtk_file_system_get_parent (path_info->path_bar->file_system,
1593                                       path_info->path,
1594                                       &path_info->parent_path,
1595                                       NULL);
1596   if (!valid)
1597     {
1598       gtk_path_bar_set_path_finish (path_info, FALSE);
1599       return;
1600     }
1601
1602   path_info->path_bar->set_path_handle =
1603     gtk_file_system_get_info (handle->file_system,
1604                               path_info->path,
1605                               GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1606                               gtk_path_bar_get_info_callback,
1607                               path_info);
1608 }
1609
1610 gboolean
1611 _gtk_path_bar_set_path (GtkPathBar         *path_bar,
1612                         const GtkFilePath  *file_path,
1613                         const gboolean      keep_trail,     
1614                         GError            **error)
1615 {
1616   struct SetPathInfo *info;
1617   gboolean result;
1618
1619   g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
1620   g_return_val_if_fail (file_path != NULL, FALSE);
1621
1622   result = TRUE;
1623
1624   /* Check whether the new path is already present in the pathbar as buttons.
1625    * This could be a parent directory or a previous selected subdirectory.
1626    */
1627   if (keep_trail &&
1628       gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
1629     return TRUE;
1630
1631   info = g_new0 (struct SetPathInfo, 1);
1632   info->path = gtk_file_path_copy (file_path);
1633   info->path_bar = path_bar;
1634   info->first_directory = TRUE;
1635
1636   result = gtk_file_system_get_parent (path_bar->file_system,
1637                                        info->path, &info->parent_path, error);
1638   if (!result)
1639     {
1640       gtk_file_path_free (info->path);
1641       g_free (info);
1642       return result;
1643     }
1644
1645   if (path_bar->set_path_handle)
1646     gtk_file_system_cancel_operation (path_bar->set_path_handle);
1647
1648   path_bar->set_path_handle =
1649     gtk_file_system_get_info (path_bar->file_system,
1650                               info->path,
1651                               GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1652                               gtk_path_bar_get_info_callback,
1653                               info);
1654
1655   return TRUE;
1656 }
1657
1658 /* FIXME: This should be a construct-only property */
1659 void
1660 _gtk_path_bar_set_file_system (GtkPathBar    *path_bar,
1661                                GtkFileSystem *file_system)
1662 {
1663   const char *home;
1664   char *desktop;
1665
1666   g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1667
1668   g_assert (path_bar->file_system == NULL);
1669
1670   path_bar->file_system = g_object_ref (file_system);
1671
1672   home = g_get_home_dir ();
1673   if (home != NULL)
1674     {
1675       path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1676       /* FIXME: Need file system backend specific way of getting the
1677        * Desktop path.
1678        */
1679       desktop = g_build_filename (home, "Desktop", NULL);
1680       path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1681       g_free (desktop);
1682     }
1683   else
1684     {
1685       path_bar->home_path = NULL;
1686       path_bar->desktop_path = NULL;
1687     }
1688   path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1689 }
1690
1691 /**
1692  * _gtk_path_bar_up:
1693  * @path_bar: a #GtkPathBar
1694  * 
1695  * If the selected button in the pathbar is not the furthest button "up" (in the
1696  * root direction), act as if the user clicked on the next button up.
1697  **/
1698 void
1699 _gtk_path_bar_up (GtkPathBar *path_bar)
1700 {
1701   GList *l;
1702
1703   for (l = path_bar->button_list; l; l = l->next)
1704     {
1705       GtkWidget *button = BUTTON_DATA (l->data)->button;
1706       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1707         {
1708           if (l->next)
1709             {
1710               GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1711               button_clicked_cb (next_button, l->next->data);
1712             }
1713           break;
1714         }
1715     }
1716 }
1717
1718 /**
1719  * _gtk_path_bar_down:
1720  * @path_bar: a #GtkPathBar
1721  * 
1722  * If the selected button in the pathbar is not the furthest button "down" (in the
1723  * leaf direction), act as if the user clicked on the next button down.
1724  **/
1725 void
1726 _gtk_path_bar_down (GtkPathBar *path_bar)
1727 {
1728   GList *l;
1729
1730   for (l = path_bar->button_list; l; l = l->next)
1731     {
1732       GtkWidget *button = BUTTON_DATA (l->data)->button;
1733       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1734         {
1735           if (l->prev)
1736             {
1737               GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1738               button_clicked_cb (prev_button, l->prev->data);
1739             }
1740           break;
1741         }
1742     }
1743 }
1744
1745 #define __GTK_PATH_BAR_C__
1746 #include "gtkaliasdef.c"