summaryrefslogtreecommitdiff
path: root/gtk/gtkpathbar.c
blob: 7aa9faf22fce304358aefbd9485620554bc591ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/* gtkpathbar.h
 * Copyright (C) 2004  Red Hat, Inc.,  Jonathan Blandford <jrb@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include "gtkpathbar.h"
#include "gtktogglebutton.h"
#include "gtkarrow.h"
#include "gtklabel.h"
#include "gtkmain.h"
#include "gtkmarshalers.h"

enum {
  PATH_CLICKED,
  LAST_SIGNAL
};

static guint path_bar_signals [LAST_SIGNAL] = { 0 };


G_DEFINE_TYPE (GtkPathBar,
	       gtk_path_bar,
	       GTK_TYPE_CONTAINER);


static void gtk_path_bar_finalize (GObject *object);
static void gtk_path_bar_size_request  (GtkWidget      *widget,
					GtkRequisition *requisition);
static void gtk_path_bar_size_allocate (GtkWidget      *widget,
					GtkAllocation  *allocation);
static void gtk_path_bar_direction_changed (GtkWidget *widget,
					    GtkTextDirection direction);
static void gtk_path_bar_add (GtkContainer *container,
			      GtkWidget    *widget);
static void gtk_path_bar_remove (GtkContainer *container,
				 GtkWidget    *widget);
static void gtk_path_bar_forall (GtkContainer *container,
				 gboolean      include_internals,
				 GtkCallback   callback,
				 gpointer      callback_data);
static void gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar);
static void gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar);

static GtkWidget *
get_slider_button (GtkPathBar *path_bar)
{
  GtkWidget *button;

  gtk_widget_push_composite_child ();

  button = gtk_button_new ();
  gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_OUT));
  gtk_container_add (GTK_CONTAINER (path_bar), button);
  gtk_widget_show_all (button);

  gtk_widget_pop_composite_child ();

  return button;
}

static void
gtk_path_bar_init (GtkPathBar *path_bar)
{
  GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);

  path_bar->spacing = 3;
  path_bar->up_slider_button = get_slider_button (path_bar);
  path_bar->down_slider_button = get_slider_button (path_bar);

  g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
  g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
}

static void
gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
{
  GObjectClass *gobject_class;
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  gobject_class = (GObjectClass *) path_bar_class;
  object_class = (GtkObjectClass *) path_bar_class;
  widget_class = (GtkWidgetClass *) path_bar_class;
  container_class = (GtkContainerClass *) path_bar_class;

  gobject_class->finalize = gtk_path_bar_finalize;

  widget_class->size_request = gtk_path_bar_size_request;
  widget_class->size_allocate = gtk_path_bar_size_allocate;
  widget_class->direction_changed = gtk_path_bar_direction_changed;

  container_class->add = gtk_path_bar_add;
  container_class->forall = gtk_path_bar_forall;
  container_class->remove = gtk_path_bar_remove;
  /* FIXME: */
  /*  container_class->child_type = gtk_path_bar_child_type;*/

  path_bar_signals [PATH_CLICKED] =
    g_signal_new ("path_clicked",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
		  NULL, NULL,
		  _gtk_marshal_VOID__POINTER,
		  G_TYPE_NONE, 1,
		  G_TYPE_POINTER);
}


static void
gtk_path_bar_finalize (GObject *object)
{
  GtkPathBar *path_bar;

  path_bar = GTK_PATH_BAR (object);
  g_list_free (path_bar->button_list);
  if (path_bar->home_directory)
    gtk_file_path_free (path_bar->home_directory);
  if (path_bar->home_icon)
    g_object_unref (path_bar->home_icon);
  if (path_bar->root_icon)
    g_object_unref (path_bar->home_icon);

  G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
}

/* Size requisition:
 * 
 * Ideally, our size is determined by another widget, and we are just filling
 * available space.
 */
static void
gtk_path_bar_size_request (GtkWidget      *widget,
			   GtkRequisition *requisition)
{
  GtkPathBar *path_bar;
  GtkRequisition child_requisition;
  GList *list;

  path_bar = GTK_PATH_BAR (widget);

  requisition->width = 0;
  requisition->height = 0;

  for (list = path_bar->button_list; list; list = list->next)
    {
      gtk_widget_size_request (GTK_WIDGET (list->data),
			       &child_requisition);
      requisition->width = MAX (child_requisition.width, requisition->width);
      requisition->height = MAX (child_requisition.height, requisition->height);
    }

  /* Add space for slider, if we have more than one path */
  /* Theoretically, the slider could be bigger than the other button.  But we're
   * not going to worry about that now.
   */
  path_bar->slider_width = requisition->height / 2 + 5;
  if (path_bar->button_list && path_bar->button_list->next != NULL)
    requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;

  gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
  gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);

  requisition->width += GTK_CONTAINER (widget)->border_width * 2;
  requisition->height += GTK_CONTAINER (widget)->border_width * 2;

  widget->requisition = *requisition;
}

static void
gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
{
  GtkTextDirection direction;

  direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
  if (direction == GTK_TEXT_DIR_RTL)
    {
      GtkWidget *arrow;

      arrow = gtk_bin_get_child (GTK_BIN (path_bar->up_slider_button));
      g_object_set (arrow, "arrow_type", GTK_ARROW_RIGHT, NULL);

      arrow = gtk_bin_get_child (GTK_BIN (path_bar->down_slider_button));
      g_object_set (arrow, "arrow_type", GTK_ARROW_LEFT, NULL);
    }
  else
    {
      GtkWidget *arrow;

      arrow = gtk_bin_get_child (GTK_BIN (path_bar->up_slider_button));
      g_object_set (arrow, "arrow_type", GTK_ARROW_LEFT, NULL);

      arrow = gtk_bin_get_child (GTK_BIN (path_bar->down_slider_button));
      g_object_set (arrow, "arrow_type", GTK_ARROW_RIGHT, NULL);
    }

  if (path_bar->button_list)
    {
      GtkWidget *button;

      button = path_bar->button_list->data;
      if (gtk_widget_get_child_visible (button))
	gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
      else
	gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);

      button = g_list_last (path_bar->button_list)->data;
      if (gtk_widget_get_child_visible (button))
	gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
      else
	gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
    }
}

/* This is a tad complicated
 */
static void
gtk_path_bar_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
{
  GtkWidget *child;
  GtkPathBar *path_bar = GTK_PATH_BAR (widget);
  GtkTextDirection direction;
  GtkAllocation child_allocation;
  GList *list, *first_button;
  gint width;
  gint allocation_width;
  gint border_width;
  gboolean need_sliders = FALSE;
  gint up_slider_offset = 0;
  gint down_slider_offset = 0;

  widget->allocation = *allocation;

  /* No path is set; we don't have to allocate anything. */
  if (path_bar->button_list == NULL)
    return;

  direction = gtk_widget_get_direction (widget);
  border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
  allocation_width = allocation->width - 2 * border_width;

  /* First, we check to see if we need the scrollbars. */
  width = GTK_WIDGET (path_bar->button_list->data)->requisition.width;
  for (list = path_bar->button_list->next; list; list = list->next)
    {
      child = GTK_WIDGET (list->data);

      width += child->requisition.width + path_bar->spacing;
    }

  if (width <= allocation_width)
    {
      first_button = g_list_last (path_bar->button_list);
    }
  else
    {
      gboolean reached_end = FALSE;
      gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);

      if (path_bar->first_scrolled_button)
	first_button = path_bar->first_scrolled_button;
      else
	first_button = path_bar->button_list;
      need_sliders = TRUE;
      
      /* To see how much space we have, and how many buttons we can display.
       * We start at the first button, count forward until hit the new
       * button, then count backwards.
       */
      /* Count down the path chain towards the end. */
      width = GTK_WIDGET (first_button->data)->requisition.width;
      list = first_button->prev;
      while (list && !reached_end)
	{
	  child = GTK_WIDGET (list->data);

	  if (width + child->requisition.width +
	      path_bar->spacing + slider_space > allocation_width)
	    reached_end = TRUE;
	  else
	    width += child->requisition.width + path_bar->spacing;

	  list = list->prev;
	}

      /* Finally, we walk up, seeing how many of the previous buttons we can
       * add */
      while (first_button->next && ! reached_end)
	{
	  child = GTK_WIDGET (first_button->next->data);

	  if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
	    {
	      reached_end = TRUE;
	    }
	  else
	    {
	      width += child->requisition.width + path_bar->spacing;
	      first_button = first_button->next;
	    }
	}
    }

  /* Now, we allocate space to the buttons */
  child_allocation.y = allocation->y + border_width;
  child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);

  if (direction == GTK_TEXT_DIR_RTL)
    {
      child_allocation.x = allocation->x + allocation->width - border_width;
      if (need_sliders)
	{
	  child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
	  up_slider_offset = allocation->width - border_width - path_bar->slider_width;
	}
    }
  else
    {
      child_allocation.x = allocation->x + border_width;
      if (need_sliders)
	{
	  up_slider_offset = border_width;
	  child_allocation.x += (path_bar->spacing + path_bar->slider_width);
	}
    }

  for (list = first_button; list; list = list->prev)
    {
      child = GTK_WIDGET (list->data);

      child_allocation.width = child->requisition.width;
      if (direction == GTK_TEXT_DIR_RTL)
	child_allocation.x -= child_allocation.width;

      /* Check to see if we've don't have any more space to allocate buttons */
      if (need_sliders && direction == GTK_TEXT_DIR_RTL)
	{
	  if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
	    break;
	}
      else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
	{
	  if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
	      widget->allocation.x + border_width + allocation_width)
	    break;
	}

      gtk_widget_set_child_visible (list->data, TRUE);
      gtk_widget_size_allocate (child, &child_allocation);

      if (direction == GTK_TEXT_DIR_RTL)
	{
	  child_allocation.x -= path_bar->spacing;
	  down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
	}
      else
	{
	  child_allocation.x += child_allocation.width + path_bar->spacing;
	  down_slider_offset = child_allocation.x - widget->allocation.x;
	}
    }
  /* Now we go hide all the widgets that don't fit */
  while (list)
    {
      gtk_widget_set_child_visible (list->data, FALSE);
      list = list->prev;
    }
  for (list = first_button->next; list; list = list->next)
    {
      gtk_widget_set_child_visible (list->data, FALSE);
    }

  if (need_sliders)
    {
      child_allocation.width = path_bar->slider_width;
      
      child_allocation.x = up_slider_offset + allocation->x;
      gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);

      child_allocation.x = down_slider_offset + allocation->x;
      gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);

      gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
      gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
      gtk_widget_show_all (path_bar->up_slider_button);
      gtk_widget_show_all (path_bar->down_slider_button);
      gtk_path_bar_update_slider_buttons (path_bar);
    }
  else
    {
      gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
      gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
    }
}

static void
 gtk_path_bar_direction_changed (GtkWidget *widget,
				 GtkTextDirection direction)
{
  gtk_path_bar_update_slider_buttons (GTK_PATH_BAR (widget));

  (* GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->direction_changed) (widget, direction);
}

static void
gtk_path_bar_add (GtkContainer *container,
		  GtkWidget    *widget)
{
  gtk_widget_set_parent (widget, GTK_WIDGET (container));
}

static void
gtk_path_bar_remove (GtkContainer *container,
		     GtkWidget    *widget)
{
  GtkPathBar *path_bar;
  GList *children;

  path_bar = GTK_PATH_BAR (container);

  children = path_bar->button_list;

  while (children)
    {
      if (widget == children->data)
	{
	  gboolean was_visible;

	  was_visible = GTK_WIDGET_VISIBLE (widget);
	  gtk_widget_unparent (widget);

	  path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
	  g_list_free (children);

	  if (was_visible)
	    gtk_widget_queue_resize (GTK_WIDGET (container));
	  break;
	}
      
      children = children->next;
    }
}

static void
gtk_path_bar_forall (GtkContainer *container,
		     gboolean      include_internals,
		     GtkCallback   callback,
		     gpointer      callback_data)
{
  GtkPathBar *path_bar;
  GList *children;

  g_return_if_fail (callback != NULL);
  path_bar = GTK_PATH_BAR (container);

  children = path_bar->button_list;
  while (children)
    {
      GtkWidget *child;
      child = children->data;
      children = children->next;

      (* callback) (child, callback_data);
    }

  (* callback) (path_bar->up_slider_button, callback_data);
  (* callback) (path_bar->down_slider_button, callback_data);
}

static void
 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
{
  GList *list;
  GList *down_button = NULL;
  GList *up_button = NULL;
  gint space_available;
  gint space_needed;
  gint border_width;
  GtkTextDirection direction;
  
  border_width = GTK_CONTAINER (path_bar)->border_width;
  direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
  
  /* We find the button at the 'down' end that we have to make
   * visible */
  for (list = path_bar->button_list; list; list = list->next)
    {
      if (list->next && gtk_widget_get_child_visible (GTK_WIDGET (list->next->data)))
	{
	  down_button = list;
	  break;
	}
    }
  
  /* Find the last visible button on the 'up' end
   */
  for (list = g_list_last (path_bar->button_list); list; list = list->prev)
    {
      if (gtk_widget_get_child_visible (GTK_WIDGET (list->data)))
	{
	  up_button = list;
	  break;
	}
    }

  space_needed = GTK_WIDGET (down_button->data)->allocation.width + path_bar->spacing;
  if (direction == GTK_TEXT_DIR_RTL)
    space_available = GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width;
  else
    space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
      (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);

  /* We have space_available extra space that's not being used.  We
   * need space_needed space to make the button fit.  So we walk down
   * from the end, removing buttons until we get all the space we
   * need. */
  while (space_available < space_needed)
    {
      space_available += GTK_WIDGET (up_button->data)->allocation.width + path_bar->spacing;
      up_button = up_button->prev;
      path_bar->first_scrolled_button = up_button;
    }
}

static void
 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
{
  GList *list;

  for (list = g_list_last (path_bar->button_list); list; list = list->prev)
    {
      if (list->prev && gtk_widget_get_child_visible (GTK_WIDGET (list->prev->data)))
	{
	  path_bar->first_scrolled_button = list;
	  return;
	}
    }
}



/* Public functions. */
static void
gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
{
  while (path_bar->button_list != NULL)
    {
      gtk_container_remove (GTK_CONTAINER (path_bar), path_bar->button_list->data);
    }
  path_bar->first_scrolled_button = NULL;
}

static void
button_clicked_cb (GtkWidget *button,
		   gpointer   data)
{
  GtkWidget *path_bar;
  GtkFilePath *file_path;

  path_bar = button->parent;
  g_assert (GTK_IS_PATH_BAR (path_bar));

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);

  file_path = g_object_get_data (G_OBJECT (button), "gtk-path-bar-button-path");
  g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0, file_path);
}

static void
update_button_appearance (GtkWidget *button,
			  gboolean   current_dir)
{
  GtkWidget *label;
  const gchar *dir_name;

  dir_name = (const gchar *) g_object_get_data (G_OBJECT (button),
						"gtk-path-bar-button-dir-name");
  label = gtk_bin_get_child (GTK_BIN (button));

  if (current_dir)
    {
      char *markup;

      markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
      gtk_label_set_markup (GTK_LABEL (label), markup);
      g_free (markup);
    }
  else
    {
      gtk_label_set_text (GTK_LABEL (label), dir_name);
    }

  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)) != current_dir)
    {
      g_signal_handlers_block_by_func (G_OBJECT (button), button_clicked_cb, NULL);
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), current_dir);
      g_signal_handlers_unblock_by_func (G_OBJECT (button), button_clicked_cb, NULL);
    }
}

/* Since gtk_file_path_free() can be a macro, we provide a real function that
 * can be used as a callback.
 */
static void
file_path_destroy (GtkFilePath *path)
{
  gtk_file_path_free (path);
}

static GtkWidget *
make_directory_button (const char  *dir_name,
		       GtkFilePath *path,
		       gboolean     current_dir)
{
  GtkWidget *button, *label;
      
  button = gtk_toggle_button_new ();
  label = gtk_label_new (NULL);

  g_signal_connect (button, "clicked",
		    G_CALLBACK (button_clicked_cb),
		    NULL);

  g_object_set_data_full (G_OBJECT (button), "gtk-path-bar-button-dir-name",
			  g_strdup (dir_name),
			  (GDestroyNotify) g_free);
  g_object_set_data_full (G_OBJECT (button), "gtk-path-bar-button-path",
			  gtk_file_path_new_dup (gtk_file_path_get_string (path)),
			  (GDestroyNotify) file_path_destroy);

  gtk_container_add (GTK_CONTAINER (button), label);
  gtk_widget_show_all (button);

  update_button_appearance (button, current_dir);

  return button;
}

static gboolean
gtk_path_bar_check_parent_path (GtkPathBar         *path_bar,
				const GtkFilePath  *file_path,
				GtkFileSystem      *file_system)
{
  GList *list;
  GList *current_path = NULL;

  for (list = path_bar->button_list; list; list = list->next)
    {
      GtkFilePath *tmp_path;

      tmp_path = (GtkFilePath *) g_object_get_data (G_OBJECT (list->data),
						    "gtk-path-bar-button-path");
      if (! gtk_file_path_compare (file_path, tmp_path))
	{
	  current_path = list;
	  break;
	}

    }

  if (current_path)
    {
      for (list = path_bar->button_list; list; list = list->next)
	{
	  update_button_appearance (GTK_WIDGET (list->data),
				    (list == current_path) ? TRUE : FALSE);
	}
      return TRUE;
    }
  return FALSE;
}

gboolean
_gtk_path_bar_set_path (GtkPathBar         *path_bar,
			const GtkFilePath  *file_path,
			GtkFileSystem      *file_system,
			GError            **error)
{
  GtkFilePath *path;
  gboolean first_directory = TRUE;
  gboolean result;

  g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
  g_return_val_if_fail (file_path != NULL, FALSE);
  g_return_val_if_fail (file_system != NULL, FALSE);

  result = TRUE;

  if (gtk_path_bar_check_parent_path (path_bar, file_path, file_system))
    return TRUE;
      
  gtk_path_bar_clear_buttons (path_bar);
  path = gtk_file_path_copy (file_path);

  gtk_widget_push_composite_child ();

  while (path != NULL)
    {
      GtkFilePath *parent_path = NULL;
      GtkWidget *button;
      const gchar *display_name;
      GError *err = NULL;
      GtkFileFolder *file_folder;
      GtkFileInfo *file_info;
      gboolean valid;

      valid = gtk_file_system_get_parent (file_system,
					  path,
					  &parent_path,
					  &err);
      if (!valid)
	{
	  result = FALSE;
	  g_propagate_error (error, err);
	  gtk_file_path_free (path);
	  break;
	}

      if (parent_path)
	file_folder = gtk_file_system_get_folder (file_system, parent_path,
						  GTK_FILE_INFO_DISPLAY_NAME, NULL);
      else
	file_folder = gtk_file_system_get_folder (file_system, path,
						  GTK_FILE_INFO_DISPLAY_NAME, NULL);

      file_info = gtk_file_folder_get_info (file_folder, path, &err);
      if (!file_info)
	{
	  result = FALSE;
	  g_propagate_error (error, err);
	  g_object_unref (file_folder);
	  gtk_file_path_free (parent_path);
	  gtk_file_path_free (path);
	  break;
	}

      display_name = gtk_file_info_get_display_name (file_info);
      /* FIXME: Do this better */
      if (! strcmp ("/", display_name))
	display_name = " / ";
      button = make_directory_button (display_name, path, first_directory);
      gtk_file_info_free (file_info);
      gtk_file_path_free (path);
      g_object_unref (file_folder);

      gtk_container_add (GTK_CONTAINER (path_bar), button);
      path_bar->button_list = g_list_prepend (path_bar->button_list, button);

      path = parent_path;
      first_directory = FALSE;
    }

  gtk_widget_pop_composite_child ();

  path_bar->button_list = g_list_reverse (path_bar->button_list);

  return result;
}

void
gtk_path_bar_set_root_icon (GtkPathBar *path_bar,
			    GdkPixbuf  *root_icon)
{
  g_return_if_fail (GTK_IS_PATH_BAR (path_bar));

  if (root_icon)
    path_bar->home_icon = g_object_ref (root_icon);

  if (path_bar->root_icon)
    g_object_unref (root_icon);

  path_bar->root_icon = root_icon;
}

void
gtk_path_bar_set_home_icon (GtkPathBar        *path_bar,
			    const GtkFilePath *home_directory,
			    GdkPixbuf         *home_icon)
{
  g_return_if_fail (GTK_IS_PATH_BAR (path_bar));

  if (home_icon)
    g_object_ref (home_icon);

  if (path_bar->home_directory != NULL)
    gtk_file_path_free (path_bar->home_directory);
  if (path_bar->home_icon)
    g_object_unref (home_icon);

  if (home_directory)
    path_bar->home_directory = gtk_file_path_new_dup (gtk_file_path_get_string (home_directory));
  else
    path_bar->home_directory = NULL;
  path_bar->home_icon = home_icon;
}