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_add (GtkContainer *container,
78 static void gtk_path_bar_remove (GtkContainer *container,
80 static void gtk_path_bar_forall (GtkContainer *container,
81 gboolean include_internals,
83 gpointer callback_data);
84 static void gtk_path_bar_scroll_up (GtkWidget *button,
85 GtkPathBar *path_bar);
86 static void gtk_path_bar_scroll_down (GtkWidget *button,
87 GtkPathBar *path_bar);
88 static void gtk_path_bar_style_set (GtkWidget *widget,
89 GtkStyle *previous_style);
90 static void gtk_path_bar_screen_changed (GtkWidget *widget,
91 GdkScreen *previous_screen);
92 static void gtk_path_bar_check_icon_theme (GtkPathBar *path_bar);
93 static void gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
94 ButtonData *button_data,
95 gboolean current_dir);
98 get_slider_button (GtkPathBar *path_bar,
99 GtkArrowType arrow_type)
103 gtk_widget_push_composite_child ();
105 button = gtk_button_new ();
106 gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
107 gtk_container_add (GTK_CONTAINER (path_bar), button);
108 gtk_widget_show_all (button);
110 gtk_widget_pop_composite_child ();
116 gtk_path_bar_init (GtkPathBar *path_bar)
118 GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
119 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
121 path_bar->spacing = 3;
122 path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
123 path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
124 path_bar->icon_size = FALLBACK_ICON_SIZE;
126 g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
127 g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
131 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
133 GObjectClass *gobject_class;
134 GtkObjectClass *object_class;
135 GtkWidgetClass *widget_class;
136 GtkContainerClass *container_class;
138 gobject_class = (GObjectClass *) path_bar_class;
139 object_class = (GtkObjectClass *) path_bar_class;
140 widget_class = (GtkWidgetClass *) path_bar_class;
141 container_class = (GtkContainerClass *) path_bar_class;
143 gobject_class->finalize = gtk_path_bar_finalize;
144 gobject_class->dispose = gtk_path_bar_dispose;
146 widget_class->size_request = gtk_path_bar_size_request;
147 widget_class->size_allocate = gtk_path_bar_size_allocate;
148 widget_class->style_set = gtk_path_bar_style_set;
149 widget_class->screen_changed = gtk_path_bar_screen_changed;
151 container_class->add = gtk_path_bar_add;
152 container_class->forall = gtk_path_bar_forall;
153 container_class->remove = gtk_path_bar_remove;
155 /* container_class->child_type = gtk_path_bar_child_type;*/
157 path_bar_signals [PATH_CLICKED] =
158 g_signal_new ("path_clicked",
159 G_OBJECT_CLASS_TYPE (object_class),
161 G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
163 _gtk_marshal_VOID__POINTER,
170 gtk_path_bar_finalize (GObject *object)
172 GtkPathBar *path_bar;
174 path_bar = GTK_PATH_BAR (object);
175 g_list_free (path_bar->button_list);
176 if (path_bar->root_path)
177 gtk_file_path_free (path_bar->root_path);
178 if (path_bar->home_path)
179 gtk_file_path_free (path_bar->home_path);
180 if (path_bar->desktop_path)
181 gtk_file_path_free (path_bar->desktop_path);
183 if (path_bar->root_icon)
184 g_object_unref (path_bar->root_icon);
185 if (path_bar->home_icon)
186 g_object_unref (path_bar->home_icon);
187 if (path_bar->desktop_icon)
188 g_object_unref (path_bar->desktop_icon);
190 if (path_bar->file_system)
191 g_object_unref (path_bar->file_system);
193 G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
196 /* Removes the settings signal handler. It's safe to call multiple times */
198 remove_settings_signal (GtkPathBar *path_bar,
201 if (path_bar->settings_signal_id)
203 GtkSettings *settings;
205 settings = gtk_settings_get_for_screen (screen);
206 g_signal_handler_disconnect (settings,
207 path_bar->settings_signal_id);
208 path_bar->settings_signal_id = 0;
213 gtk_path_bar_dispose (GObject *object)
215 remove_settings_signal (GTK_PATH_BAR (object),
216 gtk_widget_get_screen (GTK_WIDGET (object)));
218 G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
223 * Ideally, our size is determined by another widget, and we are just filling
227 gtk_path_bar_size_request (GtkWidget *widget,
228 GtkRequisition *requisition)
230 ButtonData *button_data;
231 GtkPathBar *path_bar;
232 GtkRequisition child_requisition;
235 path_bar = GTK_PATH_BAR (widget);
237 requisition->width = 0;
238 requisition->height = 0;
240 for (list = path_bar->button_list; list; list = list->next)
242 button_data = BUTTON_DATA (list->data);
243 gtk_widget_size_request (button_data->button, &child_requisition);
244 requisition->width = MAX (child_requisition.width, requisition->width);
245 requisition->height = MAX (child_requisition.height, requisition->height);
248 /* Add space for slider, if we have more than one path */
249 /* Theoretically, the slider could be bigger than the other button. But we're
250 * not going to worry about that now.
252 path_bar->slider_width = requisition->height / 2 + 5;
253 if (path_bar->button_list && path_bar->button_list->next != NULL)
254 requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
256 gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
257 gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
259 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
260 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
262 widget->requisition = *requisition;
266 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
268 if (path_bar->button_list)
272 button = BUTTON_DATA (path_bar->button_list->data)->button;
273 if (gtk_widget_get_child_visible (button))
274 gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
276 gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
278 button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
279 if (gtk_widget_get_child_visible (button))
280 gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
282 gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
286 /* This is a tad complicated
289 gtk_path_bar_size_allocate (GtkWidget *widget,
290 GtkAllocation *allocation)
293 GtkPathBar *path_bar = GTK_PATH_BAR (widget);
294 GtkTextDirection direction;
295 GtkAllocation child_allocation;
296 GList *list, *first_button;
298 gint allocation_width;
300 gboolean need_sliders = FALSE;
301 gint up_slider_offset = 0;
302 gint down_slider_offset = 0;
304 widget->allocation = *allocation;
306 /* No path is set; we don't have to allocate anything. */
307 if (path_bar->button_list == NULL)
310 direction = gtk_widget_get_direction (widget);
311 border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
312 allocation_width = allocation->width - 2 * border_width;
314 /* First, we check to see if we need the scrollbars. */
315 width = BUTTON_DATA (path_bar->button_list->data)->button->requisition.width;
316 for (list = path_bar->button_list->next; list; list = list->next)
318 child = BUTTON_DATA (list->data)->button;
320 width += child->requisition.width + path_bar->spacing;
323 if (width <= allocation_width)
325 first_button = g_list_last (path_bar->button_list);
329 gboolean reached_end = FALSE;
330 gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
332 if (path_bar->first_scrolled_button)
333 first_button = path_bar->first_scrolled_button;
335 first_button = path_bar->button_list;
338 /* To see how much space we have, and how many buttons we can display.
339 * We start at the first button, count forward until hit the new
340 * button, then count backwards.
342 /* Count down the path chain towards the end. */
343 width = BUTTON_DATA (first_button->data)->button->requisition.width;
344 list = first_button->prev;
345 while (list && !reached_end)
347 child = BUTTON_DATA (list->data)->button;
349 if (width + child->requisition.width +
350 path_bar->spacing + slider_space > allocation_width)
353 width += child->requisition.width + path_bar->spacing;
358 /* Finally, we walk up, seeing how many of the previous buttons we can
360 while (first_button->next && ! reached_end)
362 child = BUTTON_DATA (first_button->next->data)->button;
364 if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
370 width += child->requisition.width + path_bar->spacing;
371 first_button = first_button->next;
376 /* Now, we allocate space to the buttons */
377 child_allocation.y = allocation->y + border_width;
378 child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
380 if (direction == GTK_TEXT_DIR_RTL)
382 child_allocation.x = allocation->x + allocation->width - border_width;
385 child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
386 up_slider_offset = allocation->width - border_width - path_bar->slider_width;
391 child_allocation.x = allocation->x + border_width;
394 up_slider_offset = border_width;
395 child_allocation.x += (path_bar->spacing + path_bar->slider_width);
399 for (list = first_button; list; list = list->prev)
401 child = BUTTON_DATA (list->data)->button;
403 child_allocation.width = child->requisition.width;
404 if (direction == GTK_TEXT_DIR_RTL)
405 child_allocation.x -= child_allocation.width;
407 /* Check to see if we've don't have any more space to allocate buttons */
408 if (need_sliders && direction == GTK_TEXT_DIR_RTL)
410 if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
413 else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
415 if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
416 widget->allocation.x + border_width + allocation_width)
420 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
421 gtk_widget_size_allocate (child, &child_allocation);
423 if (direction == GTK_TEXT_DIR_RTL)
425 child_allocation.x -= path_bar->spacing;
426 down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
430 child_allocation.x += child_allocation.width + path_bar->spacing;
431 down_slider_offset = child_allocation.x - widget->allocation.x;
434 /* Now we go hide all the widgets that don't fit */
437 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
440 for (list = first_button->next; list; list = list->next)
442 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
447 child_allocation.width = path_bar->slider_width;
449 child_allocation.x = up_slider_offset + allocation->x;
450 gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
452 child_allocation.x = down_slider_offset + allocation->x;
453 gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
455 gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
456 gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
457 gtk_widget_show_all (path_bar->up_slider_button);
458 gtk_widget_show_all (path_bar->down_slider_button);
459 gtk_path_bar_update_slider_buttons (path_bar);
463 gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
464 gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
469 gtk_path_bar_style_set (GtkWidget *widget,
470 GtkStyle *previous_style)
472 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
473 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
475 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
479 gtk_path_bar_screen_changed (GtkWidget *widget,
480 GdkScreen *previous_screen)
482 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
483 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
485 /* We might nave a new settings, so we remove the old one */
487 remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
489 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
493 gtk_path_bar_add (GtkContainer *container,
496 gtk_widget_set_parent (widget, GTK_WIDGET (container));
500 gtk_path_bar_remove_1 (GtkContainer *container,
503 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
504 gtk_widget_unparent (widget);
506 gtk_widget_queue_resize (GTK_WIDGET (container));
510 gtk_path_bar_remove (GtkContainer *container,
513 GtkPathBar *path_bar;
516 path_bar = GTK_PATH_BAR (container);
518 if (widget == path_bar->up_slider_button)
520 gtk_path_bar_remove_1 (container, widget);
521 path_bar->up_slider_button = NULL;
525 if (widget == path_bar->down_slider_button)
527 gtk_path_bar_remove_1 (container, widget);
528 path_bar->down_slider_button = NULL;
532 children = path_bar->button_list;
535 if (widget == BUTTON_DATA (children->data)->button)
537 gtk_path_bar_remove_1 (container, widget);
538 path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
539 g_list_free (children);
543 children = children->next;
548 gtk_path_bar_forall (GtkContainer *container,
549 gboolean include_internals,
550 GtkCallback callback,
551 gpointer callback_data)
553 GtkPathBar *path_bar;
556 g_return_if_fail (callback != NULL);
557 path_bar = GTK_PATH_BAR (container);
559 children = path_bar->button_list;
563 child = BUTTON_DATA (children->data)->button;
564 children = children->next;
566 (* callback) (child, callback_data);
569 if (path_bar->up_slider_button)
570 (* callback) (path_bar->up_slider_button, callback_data);
572 if (path_bar->down_slider_button)
573 (* callback) (path_bar->down_slider_button, callback_data);
577 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
580 GList *down_button = NULL;
581 GList *up_button = NULL;
582 gint space_available;
585 GtkTextDirection direction;
587 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
589 border_width = GTK_CONTAINER (path_bar)->border_width;
590 direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
592 /* We find the button at the 'down' end that we have to make
594 for (list = path_bar->button_list; list; list = list->next)
596 if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
603 /* Find the last visible button on the 'up' end
605 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
607 if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
614 space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
615 if (direction == GTK_TEXT_DIR_RTL)
616 space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
618 space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
619 (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
621 /* We have space_available extra space that's not being used. We
622 * need space_needed space to make the button fit. So we walk down
623 * from the end, removing buttons until we get all the space we
625 while (space_available < space_needed)
627 space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
628 up_button = up_button->prev;
629 path_bar->first_scrolled_button = up_button;
634 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
638 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
640 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
642 if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
644 path_bar->first_scrolled_button = list;
650 /* Changes the icons wherever it is needed */
652 reload_icons (GtkPathBar *path_bar)
656 if (path_bar->root_icon)
658 g_object_unref (path_bar->root_icon);
659 path_bar->root_icon = NULL;
661 if (path_bar->home_icon)
663 g_object_unref (path_bar->home_icon);
664 path_bar->home_icon = NULL;
666 if (path_bar->desktop_icon)
668 g_object_unref (path_bar->desktop_icon);
669 path_bar->desktop_icon = NULL;
672 for (list = path_bar->button_list; list; list = list->next)
674 ButtonData *button_data;
675 gboolean current_dir;
677 button_data = BUTTON_DATA (list->data);
678 if (button_data->type != NORMAL_BUTTON)
680 current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
681 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
688 change_icon_theme (GtkPathBar *path_bar)
690 GtkSettings *settings;
693 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
695 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_BUTTON, &width, &height))
696 path_bar->icon_size = MAX (width, height);
698 path_bar->icon_size = FALLBACK_ICON_SIZE;
700 reload_icons (path_bar);
702 /* Callback used when a GtkSettings value changes */
704 settings_notify_cb (GObject *object,
706 GtkPathBar *path_bar)
710 name = g_param_spec_get_name (pspec);
712 if (! strcmp (name, "gtk-icon-theme-name") ||
713 ! strcmp (name, "gtk-icon-sizes"))
714 change_icon_theme (path_bar);
718 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
720 GtkSettings *settings;
722 if (path_bar->settings_signal_id)
725 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
726 path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
728 change_icon_theme (path_bar);
731 /* Public functions and their helpers */
733 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
735 while (path_bar->button_list != NULL)
737 gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
739 path_bar->first_scrolled_button = NULL;
743 button_clicked_cb (GtkWidget *button,
746 ButtonData *button_data;
749 button_data = BUTTON_DATA (data);
750 if (button_data->ignore_changes)
753 path_bar = button->parent;
754 g_assert (GTK_IS_PATH_BAR (path_bar));
755 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
757 g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0, button_data->path);
761 get_button_image (GtkPathBar *path_bar,
762 ButtonType button_type)
764 GtkFileSystemVolume *volume;
770 if (path_bar->root_icon != NULL)
771 return path_bar->root_icon;
773 volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
777 path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
779 GTK_WIDGET (path_bar),
782 gtk_file_system_volume_free (path_bar->file_system, volume);
784 return path_bar->root_icon;
786 if (path_bar->home_icon != NULL)
787 return path_bar->home_icon;
789 path_bar->home_icon = gtk_file_system_render_icon (path_bar->file_system,
791 GTK_WIDGET (path_bar),
794 return path_bar->home_icon;
796 if (path_bar->desktop_icon != NULL)
797 return path_bar->desktop_icon;
799 path_bar->desktop_icon = gtk_file_system_render_icon (path_bar->file_system,
800 path_bar->desktop_path,
801 GTK_WIDGET (path_bar),
804 return path_bar->desktop_icon;
813 button_data_free (ButtonData *button_data)
815 gtk_file_path_free (button_data->path);
816 g_free (button_data->dir_name);
817 g_free (button_data);
821 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
822 ButtonData *button_data,
823 gboolean current_dir)
825 const gchar *dir_name;
827 if (button_data->type == HOME_BUTTON)
828 dir_name = _("Home");
829 else if (button_data->type == DESKTOP_BUTTON)
830 dir_name = _("Desktop");
832 dir_name = button_data->dir_name;
834 if (button_data->label != NULL)
840 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
841 gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
846 gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
850 if (button_data->image != NULL)
853 pixbuf = get_button_image (path_bar, button_data->type);
854 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), pixbuf);
857 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
859 button_data->ignore_changes = TRUE;
860 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
861 button_data->ignore_changes = FALSE;
866 find_button_type (GtkPathBar *path_bar,
869 if (! gtk_file_path_compare (path, path_bar->root_path))
871 if (! gtk_file_path_compare (path, path_bar->home_path))
873 if (! gtk_file_path_compare (path, path_bar->desktop_path))
874 return DESKTOP_BUTTON;
876 return NORMAL_BUTTON;
880 make_directory_button (GtkPathBar *path_bar,
881 const char *dir_name,
883 gboolean current_dir)
885 GtkWidget *child = NULL;
886 ButtonData *button_data;
888 /* Is it a special button? */
889 button_data = g_new0 (ButtonData, 1);
891 button_data->type = find_button_type (path_bar, path);
892 button_data->button = gtk_toggle_button_new ();
894 switch (button_data->type)
897 button_data->image = gtk_image_new ();
898 child = button_data->image;
899 button_data->label = NULL;
903 button_data->image = gtk_image_new ();
904 button_data->label = gtk_label_new (NULL);
905 child = gtk_hbox_new (FALSE, 2);
906 gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
907 gtk_box_pack_start (GTK_BOX (child), button_data->label, FALSE, FALSE, 0);
911 button_data->label = gtk_label_new (NULL);
912 child = button_data->label;
913 button_data->image = NULL;
916 button_data->dir_name = g_strdup (dir_name);
917 button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
919 gtk_container_add (GTK_CONTAINER (button_data->button), child);
920 gtk_widget_show_all (button_data->button);
922 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
924 g_signal_connect (button_data->button, "clicked",
925 G_CALLBACK (button_clicked_cb),
927 g_object_weak_ref (G_OBJECT (button_data->button),
928 (GWeakNotify) button_data_free, button_data);
934 gtk_path_bar_check_parent_path (GtkPathBar *path_bar,
935 const GtkFilePath *file_path,
936 GtkFileSystem *file_system)
939 GList *current_path = NULL;
941 for (list = path_bar->button_list; list; list = list->next)
943 ButtonData *button_data;
945 button_data = list->data;
946 if (! gtk_file_path_compare (file_path, button_data->path))
955 for (list = path_bar->button_list; list; list = list->next)
957 gtk_path_bar_update_button_appearance (path_bar,
958 BUTTON_DATA (list->data),
959 (list == current_path) ? TRUE : FALSE);
967 _gtk_path_bar_set_path (GtkPathBar *path_bar,
968 const GtkFilePath *file_path,
972 gboolean first_directory = TRUE;
974 GList *new_buttons = NULL;
976 g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
977 g_return_val_if_fail (file_path != NULL, FALSE);
981 /* Check whether the new path is already present in the pathbar as buttons.
982 * This could be a parent directory or a previous selected subdirectory.
984 if (gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
987 path = gtk_file_path_copy (file_path);
989 gtk_widget_push_composite_child ();
993 GtkFilePath *parent_path = NULL;
994 ButtonData *button_data;
995 const gchar *display_name;
996 GtkFileFolder *file_folder;
997 GtkFileInfo *file_info;
1000 valid = gtk_file_system_get_parent (path_bar->file_system,
1007 gtk_file_path_free (path);
1011 file_folder = gtk_file_system_get_folder (path_bar->file_system,
1012 parent_path ? parent_path : path,
1013 GTK_FILE_INFO_DISPLAY_NAME,
1018 gtk_file_path_free (parent_path);
1019 gtk_file_path_free (path);
1023 file_info = gtk_file_folder_get_info (file_folder, parent_path ? path : NULL, error);
1024 g_object_unref (file_folder);
1029 gtk_file_path_free (parent_path);
1030 gtk_file_path_free (path);
1034 display_name = gtk_file_info_get_display_name (file_info);
1036 button_data = make_directory_button (path_bar, display_name, path, first_directory);
1037 gtk_file_info_free (file_info);
1038 gtk_file_path_free (path);
1040 new_buttons = g_list_prepend (new_buttons, button_data);
1042 if (button_data->type != NORMAL_BUTTON)
1044 gtk_file_path_free (parent_path);
1049 first_directory = FALSE;
1056 gtk_path_bar_clear_buttons (path_bar);
1057 path_bar->button_list = g_list_reverse (new_buttons);
1059 for (l = path_bar->button_list; l; l = l->next)
1061 GtkWidget *button = BUTTON_DATA (l->data)->button;
1062 gtk_container_add (GTK_CONTAINER (path_bar), button);
1069 for (l = new_buttons; l; l = l->next)
1071 GtkWidget *button = BUTTON_DATA (l->data)->button;
1072 gtk_widget_destroy (button);
1073 gtk_widget_unref (button);
1076 g_list_free (new_buttons);
1079 gtk_widget_pop_composite_child ();
1085 /* FIXME: This should be a construct-only property */
1087 _gtk_path_bar_set_file_system (GtkPathBar *path_bar,
1088 GtkFileSystem *file_system)
1093 g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1095 g_assert (path_bar->file_system == NULL);
1097 path_bar->file_system = g_object_ref (file_system);
1099 home = g_get_home_dir ();
1100 desktop = g_build_filename (home, "Desktop", NULL);
1101 path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1102 path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1103 path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1109 * @path_bar: a #GtkPathBar
1111 * If the selected button in the pathbar is not the furthest button "up" (in the
1112 * root direction), act as if the user clicked on the next button up.
1115 _gtk_path_bar_up (GtkPathBar *path_bar)
1119 for (l = path_bar->button_list; l; l = l->next)
1121 GtkWidget *button = BUTTON_DATA (l->data)->button;
1122 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1126 GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1127 button_clicked_cb (next_button, l->next->data);
1135 * _gtk_path_bar_down:
1136 * @path_bar: a #GtkPathBar
1138 * If the selected button in the pathbar is not the furthest button "down" (in the
1139 * leaf direction), act as if the user clicked on the next button down.
1142 _gtk_path_bar_down (GtkPathBar *path_bar)
1146 for (l = path_bar->button_list; l; l = l->next)
1148 GtkWidget *button = BUTTON_DATA (l->data)->button;
1149 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1153 GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1154 button_clicked_cb (prev_button, l->prev->data);