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