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