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