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