2 * Copyright (C) 2004 Red Hat, Inc., Jonathan Blandford <jrb@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkpathbar.h"
23 #include "gtktogglebutton.h"
27 #include "gtkicontheme.h"
28 #include "gtkiconfactory.h"
32 #include "gtkmarshalers.h"
46 #define BUTTON_DATA(x) ((ButtonData *)(x))
48 static guint path_bar_signals [LAST_SIGNAL] = { 0 };
50 /* Icon size for if we can't get it from the theme */
51 #define FALLBACK_ICON_SIZE 20
53 typedef struct _ButtonData ButtonData;
63 gboolean ignore_changes;
66 G_DEFINE_TYPE (GtkPathBar,
70 static void gtk_path_bar_finalize (GObject *object);
71 static void gtk_path_bar_dispose (GObject *object);
72 static void gtk_path_bar_size_request (GtkWidget *widget,
73 GtkRequisition *requisition);
74 static void gtk_path_bar_size_allocate (GtkWidget *widget,
75 GtkAllocation *allocation);
76 static void gtk_path_bar_direction_changed (GtkWidget *widget,
77 GtkTextDirection direction);
78 static void gtk_path_bar_add (GtkContainer *container,
80 static void gtk_path_bar_remove (GtkContainer *container,
82 static void gtk_path_bar_forall (GtkContainer *container,
83 gboolean include_internals,
85 gpointer callback_data);
86 static void gtk_path_bar_scroll_up (GtkWidget *button,
87 GtkPathBar *path_bar);
88 static void gtk_path_bar_scroll_down (GtkWidget *button,
89 GtkPathBar *path_bar);
90 static void gtk_path_bar_style_set (GtkWidget *widget,
91 GtkStyle *previous_style);
92 static void gtk_path_bar_screen_changed (GtkWidget *widget,
93 GdkScreen *previous_screen);
94 static void gtk_path_bar_check_icon_theme (GtkPathBar *path_bar);
95 static void gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
96 ButtonData *button_data,
97 gboolean current_dir);
100 get_slider_button (GtkPathBar *path_bar)
104 gtk_widget_push_composite_child ();
106 button = gtk_button_new ();
107 gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_OUT));
108 gtk_container_add (GTK_CONTAINER (path_bar), button);
109 gtk_widget_show_all (button);
111 gtk_widget_pop_composite_child ();
117 gtk_path_bar_init (GtkPathBar *path_bar)
119 GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
120 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
122 path_bar->spacing = 3;
123 path_bar->up_slider_button = get_slider_button (path_bar);
124 path_bar->down_slider_button = get_slider_button (path_bar);
125 path_bar->icon_size = FALLBACK_ICON_SIZE;
127 g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
128 g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
132 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
134 GObjectClass *gobject_class;
135 GtkObjectClass *object_class;
136 GtkWidgetClass *widget_class;
137 GtkContainerClass *container_class;
139 gobject_class = (GObjectClass *) path_bar_class;
140 object_class = (GtkObjectClass *) path_bar_class;
141 widget_class = (GtkWidgetClass *) path_bar_class;
142 container_class = (GtkContainerClass *) path_bar_class;
144 gobject_class->finalize = gtk_path_bar_finalize;
145 gobject_class->dispose = gtk_path_bar_dispose;
147 widget_class->size_request = gtk_path_bar_size_request;
148 widget_class->size_allocate = gtk_path_bar_size_allocate;
149 widget_class->direction_changed = gtk_path_bar_direction_changed;
150 widget_class->style_set = gtk_path_bar_style_set;
151 widget_class->screen_changed = gtk_path_bar_screen_changed;
153 container_class->add = gtk_path_bar_add;
154 container_class->forall = gtk_path_bar_forall;
155 container_class->remove = gtk_path_bar_remove;
157 /* container_class->child_type = gtk_path_bar_child_type;*/
159 path_bar_signals [PATH_CLICKED] =
160 g_signal_new ("path_clicked",
161 G_OBJECT_CLASS_TYPE (object_class),
163 G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
165 _gtk_marshal_VOID__POINTER,
172 gtk_path_bar_finalize (GObject *object)
174 GtkPathBar *path_bar;
176 path_bar = GTK_PATH_BAR (object);
177 g_list_free (path_bar->button_list);
178 if (path_bar->root_path)
179 gtk_file_path_free (path_bar->root_path);
180 if (path_bar->home_path)
181 gtk_file_path_free (path_bar->home_path);
182 if (path_bar->desktop_path)
183 gtk_file_path_free (path_bar->desktop_path);
185 if (path_bar->root_icon)
186 g_object_unref (path_bar->root_icon);
187 if (path_bar->home_icon)
188 g_object_unref (path_bar->home_icon);
189 if (path_bar->desktop_icon)
190 g_object_unref (path_bar->desktop_icon);
192 if (path_bar->file_system)
193 g_object_unref (path_bar->file_system);
195 G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
198 /* Removes the settings signal handler. It's safe to call multiple times */
200 remove_settings_signal (GtkPathBar *path_bar,
203 if (path_bar->settings_signal_id)
205 GtkSettings *settings;
207 settings = gtk_settings_get_for_screen (screen);
208 g_signal_handler_disconnect (settings,
209 path_bar->settings_signal_id);
210 path_bar->settings_signal_id = 0;
215 gtk_path_bar_dispose (GObject *object)
217 remove_settings_signal (GTK_PATH_BAR (object),
218 gtk_widget_get_screen (GTK_WIDGET (object)));
220 G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
225 * Ideally, our size is determined by another widget, and we are just filling
229 gtk_path_bar_size_request (GtkWidget *widget,
230 GtkRequisition *requisition)
232 ButtonData *button_data;
233 GtkPathBar *path_bar;
234 GtkRequisition child_requisition;
237 path_bar = GTK_PATH_BAR (widget);
239 requisition->width = 0;
240 requisition->height = 0;
242 for (list = path_bar->button_list; list; list = list->next)
244 button_data = BUTTON_DATA (list->data);
245 gtk_widget_size_request (button_data->button, &child_requisition);
246 requisition->width = MAX (child_requisition.width, requisition->width);
247 requisition->height = MAX (child_requisition.height, requisition->height);
250 /* Add space for slider, if we have more than one path */
251 /* Theoretically, the slider could be bigger than the other button. But we're
252 * not going to worry about that now.
254 path_bar->slider_width = requisition->height / 2 + 5;
255 if (path_bar->button_list && path_bar->button_list->next != NULL)
256 requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
258 gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
259 gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
261 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
262 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
264 widget->requisition = *requisition;
268 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
270 GtkTextDirection direction;
272 direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
273 if (direction == GTK_TEXT_DIR_RTL)
277 arrow = gtk_bin_get_child (GTK_BIN (path_bar->up_slider_button));
278 g_object_set (arrow, "arrow_type", GTK_ARROW_RIGHT, NULL);
280 arrow = gtk_bin_get_child (GTK_BIN (path_bar->down_slider_button));
281 g_object_set (arrow, "arrow_type", GTK_ARROW_LEFT, NULL);
287 arrow = gtk_bin_get_child (GTK_BIN (path_bar->up_slider_button));
288 g_object_set (arrow, "arrow_type", GTK_ARROW_LEFT, NULL);
290 arrow = gtk_bin_get_child (GTK_BIN (path_bar->down_slider_button));
291 g_object_set (arrow, "arrow_type", GTK_ARROW_RIGHT, NULL);
294 if (path_bar->button_list)
298 button = BUTTON_DATA (path_bar->button_list->data)->button;
299 if (gtk_widget_get_child_visible (button))
300 gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
302 gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
304 button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
305 if (gtk_widget_get_child_visible (button))
306 gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
308 gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
312 /* This is a tad complicated
315 gtk_path_bar_size_allocate (GtkWidget *widget,
316 GtkAllocation *allocation)
319 GtkPathBar *path_bar = GTK_PATH_BAR (widget);
320 GtkTextDirection direction;
321 GtkAllocation child_allocation;
322 GList *list, *first_button;
324 gint allocation_width;
326 gboolean need_sliders = FALSE;
327 gint up_slider_offset = 0;
328 gint down_slider_offset = 0;
330 widget->allocation = *allocation;
332 /* No path is set; we don't have to allocate anything. */
333 if (path_bar->button_list == NULL)
336 direction = gtk_widget_get_direction (widget);
337 border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
338 allocation_width = allocation->width - 2 * border_width;
340 /* First, we check to see if we need the scrollbars. */
341 width = BUTTON_DATA (path_bar->button_list->data)->button->requisition.width;
342 for (list = path_bar->button_list->next; list; list = list->next)
344 child = BUTTON_DATA (list->data)->button;
346 width += child->requisition.width + path_bar->spacing;
349 if (width <= allocation_width)
351 first_button = g_list_last (path_bar->button_list);
355 gboolean reached_end = FALSE;
356 gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
358 if (path_bar->first_scrolled_button)
359 first_button = path_bar->first_scrolled_button;
361 first_button = path_bar->button_list;
364 /* To see how much space we have, and how many buttons we can display.
365 * We start at the first button, count forward until hit the new
366 * button, then count backwards.
368 /* Count down the path chain towards the end. */
369 width = BUTTON_DATA (first_button->data)->button->requisition.width;
370 list = first_button->prev;
371 while (list && !reached_end)
373 child = BUTTON_DATA (list->data)->button;
375 if (width + child->requisition.width +
376 path_bar->spacing + slider_space > allocation_width)
379 width += child->requisition.width + path_bar->spacing;
384 /* Finally, we walk up, seeing how many of the previous buttons we can
386 while (first_button->next && ! reached_end)
388 child = BUTTON_DATA (first_button->next->data)->button;
390 if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
396 width += child->requisition.width + path_bar->spacing;
397 first_button = first_button->next;
402 /* Now, we allocate space to the buttons */
403 child_allocation.y = allocation->y + border_width;
404 child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
406 if (direction == GTK_TEXT_DIR_RTL)
408 child_allocation.x = allocation->x + allocation->width - border_width;
411 child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
412 up_slider_offset = allocation->width - border_width - path_bar->slider_width;
417 child_allocation.x = allocation->x + border_width;
420 up_slider_offset = border_width;
421 child_allocation.x += (path_bar->spacing + path_bar->slider_width);
425 for (list = first_button; list; list = list->prev)
427 child = BUTTON_DATA (list->data)->button;
429 child_allocation.width = child->requisition.width;
430 if (direction == GTK_TEXT_DIR_RTL)
431 child_allocation.x -= child_allocation.width;
433 /* Check to see if we've don't have any more space to allocate buttons */
434 if (need_sliders && direction == GTK_TEXT_DIR_RTL)
436 if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
439 else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
441 if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
442 widget->allocation.x + border_width + allocation_width)
446 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
447 gtk_widget_size_allocate (child, &child_allocation);
449 if (direction == GTK_TEXT_DIR_RTL)
451 child_allocation.x -= path_bar->spacing;
452 down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
456 child_allocation.x += child_allocation.width + path_bar->spacing;
457 down_slider_offset = child_allocation.x - widget->allocation.x;
460 /* Now we go hide all the widgets that don't fit */
463 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
466 for (list = first_button->next; list; list = list->next)
468 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
473 child_allocation.width = path_bar->slider_width;
475 child_allocation.x = up_slider_offset + allocation->x;
476 gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
478 child_allocation.x = down_slider_offset + allocation->x;
479 gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
481 gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
482 gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
483 gtk_widget_show_all (path_bar->up_slider_button);
484 gtk_widget_show_all (path_bar->down_slider_button);
485 gtk_path_bar_update_slider_buttons (path_bar);
489 gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
490 gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
495 gtk_path_bar_direction_changed (GtkWidget *widget,
496 GtkTextDirection direction)
498 gtk_path_bar_update_slider_buttons (GTK_PATH_BAR (widget));
500 (* GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->direction_changed) (widget, direction);
504 gtk_path_bar_style_set (GtkWidget *widget,
505 GtkStyle *previous_style)
507 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
508 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
510 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
514 gtk_path_bar_screen_changed (GtkWidget *widget,
515 GdkScreen *previous_screen)
517 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
518 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
520 /* We might nave a new settings, so we remove the old one */
522 remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
524 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
528 gtk_path_bar_add (GtkContainer *container,
531 gtk_widget_set_parent (widget, GTK_WIDGET (container));
535 gtk_path_bar_remove (GtkContainer *container,
538 GtkPathBar *path_bar;
541 path_bar = GTK_PATH_BAR (container);
543 children = path_bar->button_list;
547 if (widget == BUTTON_DATA (children->data)->button)
549 gboolean was_visible;
551 was_visible = GTK_WIDGET_VISIBLE (widget);
552 gtk_widget_unparent (widget);
554 path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
555 g_list_free (children);
558 gtk_widget_queue_resize (GTK_WIDGET (container));
562 children = children->next;
567 gtk_path_bar_forall (GtkContainer *container,
568 gboolean include_internals,
569 GtkCallback callback,
570 gpointer callback_data)
572 GtkPathBar *path_bar;
575 g_return_if_fail (callback != NULL);
576 path_bar = GTK_PATH_BAR (container);
578 children = path_bar->button_list;
582 child = BUTTON_DATA (children->data)->button;
583 children = children->next;
585 (* callback) (child, callback_data);
588 (* callback) (path_bar->up_slider_button, callback_data);
589 (* callback) (path_bar->down_slider_button, callback_data);
593 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
596 GList *down_button = NULL;
597 GList *up_button = NULL;
598 gint space_available;
601 GtkTextDirection direction;
603 border_width = GTK_CONTAINER (path_bar)->border_width;
604 direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
606 /* We find the button at the 'down' end that we have to make
608 for (list = path_bar->button_list; list; list = list->next)
610 if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
617 /* Find the last visible button on the 'up' end
619 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
621 if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
628 space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
629 if (direction == GTK_TEXT_DIR_RTL)
630 space_available = GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width;
632 space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
633 (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
635 /* We have space_available extra space that's not being used. We
636 * need space_needed space to make the button fit. So we walk down
637 * from the end, removing buttons until we get all the space we
639 while (space_available < space_needed)
641 space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
642 up_button = up_button->prev;
643 path_bar->first_scrolled_button = up_button;
648 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
652 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
654 if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
656 path_bar->first_scrolled_button = list;
662 /* Changes the icons wherever it is needed */
664 reload_icons (GtkPathBar *path_bar)
668 if (path_bar->root_icon)
670 g_object_unref (path_bar->root_icon);
671 path_bar->root_icon = NULL;
673 if (path_bar->home_icon)
675 g_object_unref (path_bar->home_icon);
676 path_bar->home_icon = NULL;
678 if (path_bar->desktop_icon)
680 g_object_unref (path_bar->desktop_icon);
681 path_bar->desktop_icon = NULL;
684 for (list = path_bar->button_list; list; list = list->next)
686 ButtonData *button_data;
687 gboolean current_dir;
689 button_data = BUTTON_DATA (list->data);
690 if (button_data->type != NORMAL_BUTTON)
692 current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
693 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
700 change_icon_theme (GtkPathBar *path_bar)
702 GtkSettings *settings;
705 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
707 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_BUTTON, &width, &height))
708 path_bar->icon_size = MAX (width, height);
710 path_bar->icon_size = FALLBACK_ICON_SIZE;
712 reload_icons (path_bar);
714 /* Callback used when a GtkSettings value changes */
716 settings_notify_cb (GObject *object,
718 GtkPathBar *path_bar)
722 name = g_param_spec_get_name (pspec);
724 if (! strcmp (name, "gtk-icon-theme-name") ||
725 ! strcmp (name, "gtk-icon-sizes"))
726 change_icon_theme (path_bar);
730 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
732 GtkSettings *settings;
734 if (path_bar->settings_signal_id)
737 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
738 path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
740 change_icon_theme (path_bar);
743 /* Public functions and their helpers */
745 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
747 while (path_bar->button_list != NULL)
749 gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
751 path_bar->first_scrolled_button = NULL;
755 button_clicked_cb (GtkWidget *button,
758 ButtonData *button_data;
761 button_data = BUTTON_DATA (data);
762 if (button_data->ignore_changes)
765 path_bar = button->parent;
766 g_assert (GTK_IS_PATH_BAR (path_bar));
767 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
769 g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0, button_data->path);
773 get_button_image (GtkPathBar *path_bar,
774 ButtonType button_type)
776 GtkFileSystemVolume *volume;
782 if (path_bar->root_icon != NULL)
783 return path_bar->root_icon;
785 volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
789 path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
791 GTK_WIDGET (path_bar),
794 gtk_file_system_volume_free (path_bar->file_system, volume);
796 return path_bar->root_icon;
798 if (path_bar->home_icon != NULL)
799 return path_bar->home_icon;
801 path_bar->home_icon = gtk_file_system_render_icon (path_bar->file_system,
803 GTK_WIDGET (path_bar),
806 return path_bar->home_icon;
808 if (path_bar->desktop_icon != NULL)
809 return path_bar->desktop_icon;
811 path_bar->desktop_icon = gtk_file_system_render_icon (path_bar->file_system,
812 path_bar->desktop_path,
813 GTK_WIDGET (path_bar),
816 return path_bar->desktop_icon;
825 button_data_free (ButtonData *button_data)
827 gtk_file_path_free (button_data->path);
828 g_free (button_data->dir_name);
829 g_free (button_data);
833 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
834 ButtonData *button_data,
835 gboolean current_dir)
837 const gchar *dir_name;
839 if (button_data->type == HOME_BUTTON)
840 dir_name = _("Home");
841 else if (button_data->type == DESKTOP_BUTTON)
842 dir_name = _("Desktop");
844 dir_name = button_data->dir_name;
846 if (button_data->label != NULL)
852 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
853 gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
858 gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
862 if (button_data->image != NULL)
865 pixbuf = get_button_image (path_bar, button_data->type);
866 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), pixbuf);
869 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
871 button_data->ignore_changes = TRUE;
872 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
873 button_data->ignore_changes = FALSE;
878 find_button_type (GtkPathBar *path_bar,
881 if (! gtk_file_path_compare (path, path_bar->root_path))
883 if (! gtk_file_path_compare (path, path_bar->home_path))
885 if (! gtk_file_path_compare (path, path_bar->desktop_path))
886 return DESKTOP_BUTTON;
888 return NORMAL_BUTTON;
892 make_directory_button (GtkPathBar *path_bar,
893 const char *dir_name,
895 gboolean current_dir)
897 GtkWidget *child = NULL;
898 ButtonData *button_data;
900 /* Is it a special button? */
901 button_data = g_new0 (ButtonData, 1);
903 button_data->type = find_button_type (path_bar, path);
904 button_data->button = gtk_toggle_button_new ();
906 switch (button_data->type)
909 button_data->image = gtk_image_new ();
910 child = button_data->image;
911 button_data->label = NULL;
915 button_data->image = gtk_image_new ();
916 button_data->label = gtk_label_new (NULL);
917 child = gtk_hbox_new (FALSE, 2);
918 gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
919 gtk_box_pack_start (GTK_BOX (child), button_data->label, FALSE, FALSE, 0);
923 button_data->label = gtk_label_new (NULL);
924 child = button_data->label;
925 button_data->image = NULL;
928 button_data->dir_name = g_strdup (dir_name);
929 button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
931 gtk_container_add (GTK_CONTAINER (button_data->button), child);
932 gtk_widget_show_all (button_data->button);
934 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
936 g_signal_connect (button_data->button, "clicked",
937 G_CALLBACK (button_clicked_cb),
939 g_object_weak_ref (G_OBJECT (button_data->button),
940 (GWeakNotify) button_data_free, button_data);
946 gtk_path_bar_check_parent_path (GtkPathBar *path_bar,
947 const GtkFilePath *file_path,
948 GtkFileSystem *file_system)
951 GList *current_path = NULL;
953 for (list = path_bar->button_list; list; list = list->next)
955 ButtonData *button_data;
957 button_data = list->data;
958 if (! gtk_file_path_compare (file_path, button_data->path))
967 for (list = path_bar->button_list; list; list = list->next)
969 gtk_path_bar_update_button_appearance (path_bar,
970 BUTTON_DATA (list->data),
971 (list == current_path) ? TRUE : FALSE);
979 _gtk_path_bar_set_path (GtkPathBar *path_bar,
980 const GtkFilePath *file_path,
984 gboolean first_directory = TRUE;
986 GList *new_buttons = NULL;
988 g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
989 g_return_val_if_fail (file_path != NULL, FALSE);
993 /* Check whether the new path is already present in the pathbar as buttons.
994 * This could be a parent directory or a previous selected subdirectory.
996 if (gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
999 path = gtk_file_path_copy (file_path);
1001 gtk_widget_push_composite_child ();
1003 while (path != NULL)
1005 GtkFilePath *parent_path = NULL;
1006 ButtonData *button_data;
1007 const gchar *display_name;
1008 GtkFileFolder *file_folder;
1009 GtkFileInfo *file_info;
1012 valid = gtk_file_system_get_parent (path_bar->file_system,
1019 gtk_file_path_free (path);
1023 file_folder = gtk_file_system_get_folder (path_bar->file_system,
1024 parent_path ? parent_path : path,
1025 GTK_FILE_INFO_DISPLAY_NAME,
1027 file_info = gtk_file_folder_get_info (file_folder, path, error);
1028 g_object_unref (file_folder);
1034 gtk_file_path_free (parent_path);
1035 gtk_file_path_free (path);
1039 display_name = gtk_file_info_get_display_name (file_info);
1041 button_data = make_directory_button (path_bar, display_name, path, first_directory);
1042 gtk_file_info_free (file_info);
1043 gtk_file_path_free (path);
1045 new_buttons = g_list_prepend (new_buttons, button_data);
1047 if (button_data->type != NORMAL_BUTTON)
1049 gtk_file_path_free (parent_path);
1054 first_directory = FALSE;
1061 gtk_path_bar_clear_buttons (path_bar);
1062 path_bar->button_list = g_list_reverse (new_buttons);
1064 for (l = path_bar->button_list; l; l = l->next)
1066 GtkWidget *button = BUTTON_DATA (l->data)->button;
1067 gtk_container_add (GTK_CONTAINER (path_bar), button);
1074 for (l = new_buttons; l; l = l->next)
1076 GtkWidget *button = BUTTON_DATA (l->data)->button;
1077 gtk_widget_destroy (button);
1078 gtk_widget_unref (button);
1081 g_list_free (new_buttons);
1084 gtk_widget_pop_composite_child ();
1090 /* FIXME: This should be a construct-only property */
1092 _gtk_path_bar_set_file_system (GtkPathBar *path_bar,
1093 GtkFileSystem *file_system)
1098 g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1100 g_assert (path_bar->file_system == NULL);
1102 path_bar->file_system = g_object_ref (file_system);
1104 home = g_get_home_dir ();
1105 desktop = g_build_filename (home, "Desktop", NULL);
1106 path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1107 path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1108 path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");