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"
24 #include "gtkalignment.h"
29 #include "gtkicontheme.h"
30 #include "gtkiconfactory.h"
34 #include "gtkmarshalers.h"
49 #define BUTTON_DATA(x) ((ButtonData *)(x))
51 #define SCROLL_TIMEOUT 150
52 #define INITIAL_SCROLL_TIMEOUT 300
54 static guint path_bar_signals [LAST_SIGNAL] = { 0 };
56 /* Icon size for if we can't get it from the theme */
57 #define FALLBACK_ICON_SIZE 16
59 typedef struct _ButtonData ButtonData;
69 guint ignore_changes : 1;
70 guint file_is_hidden : 1;
72 /* This macro is used to check if a button can be used as a fake root.
73 * All buttons in front of a fake root are automatically hidden when in a
74 * directory below a fake root and replaced with the "<" arrow button.
76 #define BUTTON_IS_FAKE_ROOT(button) ((button)->type == HOME_BUTTON)
78 G_DEFINE_TYPE (GtkPathBar, gtk_path_bar, GTK_TYPE_CONTAINER);
80 static void gtk_path_bar_finalize (GObject *object);
81 static void gtk_path_bar_dispose (GObject *object);
82 static void gtk_path_bar_size_request (GtkWidget *widget,
83 GtkRequisition *requisition);
84 static void gtk_path_bar_unmap (GtkWidget *widget);
85 static void gtk_path_bar_size_allocate (GtkWidget *widget,
86 GtkAllocation *allocation);
87 static void gtk_path_bar_add (GtkContainer *container,
89 static void gtk_path_bar_remove (GtkContainer *container,
91 static void gtk_path_bar_forall (GtkContainer *container,
92 gboolean include_internals,
94 gpointer callback_data);
95 static void gtk_path_bar_scroll_up (GtkWidget *button,
96 GtkPathBar *path_bar);
97 static void gtk_path_bar_scroll_down (GtkWidget *button,
98 GtkPathBar *path_bar);
99 static void gtk_path_bar_stop_scrolling (GtkPathBar *path_bar);
100 static gboolean gtk_path_bar_slider_button_press (GtkWidget *widget,
101 GdkEventButton *event,
102 GtkPathBar *path_bar);
103 static gboolean gtk_path_bar_slider_button_release(GtkWidget *widget,
104 GdkEventButton *event,
105 GtkPathBar *path_bar);
106 static void gtk_path_bar_grab_notify (GtkWidget *widget,
107 gboolean was_grabbed);
108 static void gtk_path_bar_state_changed (GtkWidget *widget,
109 GtkStateType previous_state);
110 static void gtk_path_bar_style_set (GtkWidget *widget,
111 GtkStyle *previous_style);
112 static void gtk_path_bar_screen_changed (GtkWidget *widget,
113 GdkScreen *previous_screen);
114 static void gtk_path_bar_check_icon_theme (GtkPathBar *path_bar);
115 static void gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
116 ButtonData *button_data,
117 gboolean current_dir);
120 get_slider_button (GtkPathBar *path_bar,
121 GtkArrowType arrow_type)
125 gtk_widget_push_composite_child ();
127 button = gtk_button_new ();
128 gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
129 gtk_container_add (GTK_CONTAINER (path_bar), button);
130 gtk_widget_show_all (button);
132 gtk_widget_pop_composite_child ();
138 gtk_path_bar_init (GtkPathBar *path_bar)
140 GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
141 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
143 path_bar->spacing = 3;
144 path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
145 path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
146 path_bar->icon_size = FALLBACK_ICON_SIZE;
148 g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
149 g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
151 g_signal_connect (path_bar->up_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
152 g_signal_connect (path_bar->up_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
153 g_signal_connect (path_bar->down_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
154 g_signal_connect (path_bar->down_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
158 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
160 GObjectClass *gobject_class;
161 GtkObjectClass *object_class;
162 GtkWidgetClass *widget_class;
163 GtkContainerClass *container_class;
165 gobject_class = (GObjectClass *) path_bar_class;
166 object_class = (GtkObjectClass *) path_bar_class;
167 widget_class = (GtkWidgetClass *) path_bar_class;
168 container_class = (GtkContainerClass *) path_bar_class;
170 gobject_class->finalize = gtk_path_bar_finalize;
171 gobject_class->dispose = gtk_path_bar_dispose;
173 widget_class->size_request = gtk_path_bar_size_request;
174 widget_class->unmap = gtk_path_bar_unmap;
175 widget_class->size_allocate = gtk_path_bar_size_allocate;
176 widget_class->style_set = gtk_path_bar_style_set;
177 widget_class->screen_changed = gtk_path_bar_screen_changed;
178 widget_class->grab_notify = gtk_path_bar_grab_notify;
179 widget_class->state_changed = gtk_path_bar_state_changed;
181 container_class->add = gtk_path_bar_add;
182 container_class->forall = gtk_path_bar_forall;
183 container_class->remove = gtk_path_bar_remove;
185 /* container_class->child_type = gtk_path_bar_child_type;*/
187 path_bar_signals [PATH_CLICKED] =
188 g_signal_new (I_("path-clicked"),
189 G_OBJECT_CLASS_TYPE (object_class),
191 G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
193 _gtk_marshal_VOID__POINTER_BOOLEAN,
201 gtk_path_bar_finalize (GObject *object)
203 GtkPathBar *path_bar;
205 path_bar = GTK_PATH_BAR (object);
207 gtk_path_bar_stop_scrolling (path_bar);
209 g_list_free (path_bar->button_list);
210 if (path_bar->root_path)
211 gtk_file_path_free (path_bar->root_path);
212 if (path_bar->home_path)
213 gtk_file_path_free (path_bar->home_path);
214 if (path_bar->desktop_path)
215 gtk_file_path_free (path_bar->desktop_path);
217 if (path_bar->root_icon)
218 g_object_unref (path_bar->root_icon);
219 if (path_bar->home_icon)
220 g_object_unref (path_bar->home_icon);
221 if (path_bar->desktop_icon)
222 g_object_unref (path_bar->desktop_icon);
224 if (path_bar->file_system)
225 g_object_unref (path_bar->file_system);
227 G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
230 /* Removes the settings signal handler. It's safe to call multiple times */
232 remove_settings_signal (GtkPathBar *path_bar,
235 if (path_bar->settings_signal_id)
237 GtkSettings *settings;
239 settings = gtk_settings_get_for_screen (screen);
240 g_signal_handler_disconnect (settings,
241 path_bar->settings_signal_id);
242 path_bar->settings_signal_id = 0;
247 gtk_path_bar_dispose (GObject *object)
249 remove_settings_signal (GTK_PATH_BAR (object),
250 gtk_widget_get_screen (GTK_WIDGET (object)));
252 G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
257 * Ideally, our size is determined by another widget, and we are just filling
261 gtk_path_bar_size_request (GtkWidget *widget,
262 GtkRequisition *requisition)
264 ButtonData *button_data;
265 GtkPathBar *path_bar;
266 GtkRequisition child_requisition;
269 path_bar = GTK_PATH_BAR (widget);
271 requisition->width = 0;
272 requisition->height = 0;
274 for (list = path_bar->button_list; list; list = list->next)
276 button_data = BUTTON_DATA (list->data);
277 gtk_widget_size_request (button_data->button, &child_requisition);
278 requisition->width = MAX (child_requisition.width, requisition->width);
279 requisition->height = MAX (child_requisition.height, requisition->height);
282 /* Add space for slider, if we have more than one path */
283 /* Theoretically, the slider could be bigger than the other button. But we're
284 * not going to worry about that now.
286 path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
287 if (path_bar->button_list && path_bar->button_list->next != NULL)
288 requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
290 gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
291 gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
293 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
294 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
296 widget->requisition = *requisition;
300 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
302 if (path_bar->button_list)
306 button = BUTTON_DATA (path_bar->button_list->data)->button;
307 if (gtk_widget_get_child_visible (button))
308 gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
310 gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
312 button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
313 if (gtk_widget_get_child_visible (button))
314 gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
316 gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
321 gtk_path_bar_unmap (GtkWidget *widget)
323 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
325 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unmap (widget);
328 /* This is a tad complicated
331 gtk_path_bar_size_allocate (GtkWidget *widget,
332 GtkAllocation *allocation)
335 GtkPathBar *path_bar = GTK_PATH_BAR (widget);
336 GtkTextDirection direction;
337 GtkAllocation child_allocation;
338 GList *list, *first_button;
340 gint allocation_width;
342 gboolean need_sliders = FALSE;
343 gint up_slider_offset = 0;
344 gint down_slider_offset = 0;
346 widget->allocation = *allocation;
348 /* No path is set; we don't have to allocate anything. */
349 if (path_bar->button_list == NULL)
352 direction = gtk_widget_get_direction (widget);
353 border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
354 allocation_width = allocation->width - 2 * border_width;
356 /* First, we check to see if we need the scrollbars. */
357 if (path_bar->fake_root)
358 width = path_bar->spacing + path_bar->slider_width;
362 for (list = path_bar->button_list; list; list = list->next)
364 child = BUTTON_DATA (list->data)->button;
366 width += child->requisition.width + path_bar->spacing;
367 if (list == path_bar->fake_root)
371 if (width <= allocation_width)
373 if (path_bar->fake_root)
374 first_button = path_bar->fake_root;
376 first_button = g_list_last (path_bar->button_list);
380 gboolean reached_end = FALSE;
381 gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
383 if (path_bar->first_scrolled_button)
384 first_button = path_bar->first_scrolled_button;
386 first_button = path_bar->button_list;
389 /* To see how much space we have, and how many buttons we can display.
390 * We start at the first button, count forward until hit the new
391 * button, then count backwards.
393 /* Count down the path chain towards the end. */
394 width = BUTTON_DATA (first_button->data)->button->requisition.width;
395 list = first_button->prev;
396 while (list && !reached_end)
398 child = BUTTON_DATA (list->data)->button;
400 if (width + child->requisition.width +
401 path_bar->spacing + slider_space > allocation_width)
403 else if (list == path_bar->fake_root)
406 width += child->requisition.width + path_bar->spacing;
411 /* Finally, we walk up, seeing how many of the previous buttons we can
413 while (first_button->next && !reached_end)
415 child = BUTTON_DATA (first_button->next->data)->button;
417 if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
423 width += child->requisition.width + path_bar->spacing;
424 if (first_button == path_bar->fake_root)
426 first_button = first_button->next;
431 /* Now, we allocate space to the buttons */
432 child_allocation.y = allocation->y + border_width;
433 child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
435 if (direction == GTK_TEXT_DIR_RTL)
437 child_allocation.x = allocation->x + allocation->width - border_width;
438 if (need_sliders || path_bar->fake_root)
440 child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
441 up_slider_offset = allocation->width - border_width - path_bar->slider_width;
446 child_allocation.x = allocation->x + border_width;
447 if (need_sliders || path_bar->fake_root)
449 up_slider_offset = border_width;
450 child_allocation.x += (path_bar->spacing + path_bar->slider_width);
454 for (list = first_button; list; list = list->prev)
456 child = BUTTON_DATA (list->data)->button;
458 child_allocation.width = child->requisition.width;
459 if (direction == GTK_TEXT_DIR_RTL)
460 child_allocation.x -= child_allocation.width;
462 /* Check to see if we've don't have any more space to allocate buttons */
463 if (need_sliders && direction == GTK_TEXT_DIR_RTL)
465 if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
468 else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
470 if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
471 widget->allocation.x + border_width + allocation_width)
475 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
476 gtk_widget_size_allocate (child, &child_allocation);
478 if (direction == GTK_TEXT_DIR_RTL)
480 child_allocation.x -= path_bar->spacing;
481 down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
482 down_slider_offset = border_width;
486 down_slider_offset = child_allocation.x - widget->allocation.x;
487 down_slider_offset = allocation->width - border_width - path_bar->slider_width;
488 child_allocation.x += child_allocation.width + path_bar->spacing;
491 /* Now we go hide all the widgets that don't fit */
494 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
497 for (list = first_button->next; list; list = list->next)
499 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
502 if (need_sliders || path_bar->fake_root)
504 child_allocation.width = path_bar->slider_width;
505 child_allocation.x = up_slider_offset + allocation->x;
506 gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
508 gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
509 gtk_widget_show_all (path_bar->up_slider_button);
512 gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
516 child_allocation.width = path_bar->slider_width;
517 child_allocation.x = down_slider_offset + allocation->x;
518 gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
520 gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
521 gtk_widget_show_all (path_bar->down_slider_button);
522 gtk_path_bar_update_slider_buttons (path_bar);
525 gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
529 gtk_path_bar_style_set (GtkWidget *widget,
530 GtkStyle *previous_style)
532 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
533 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
535 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
539 gtk_path_bar_screen_changed (GtkWidget *widget,
540 GdkScreen *previous_screen)
542 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
543 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
545 /* We might nave a new settings, so we remove the old one */
547 remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
549 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
553 gtk_path_bar_add (GtkContainer *container,
556 gtk_widget_set_parent (widget, GTK_WIDGET (container));
560 gtk_path_bar_remove_1 (GtkContainer *container,
563 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
564 gtk_widget_unparent (widget);
566 gtk_widget_queue_resize (GTK_WIDGET (container));
570 gtk_path_bar_remove (GtkContainer *container,
573 GtkPathBar *path_bar;
576 path_bar = GTK_PATH_BAR (container);
578 if (widget == path_bar->up_slider_button)
580 gtk_path_bar_remove_1 (container, widget);
581 path_bar->up_slider_button = NULL;
585 if (widget == path_bar->down_slider_button)
587 gtk_path_bar_remove_1 (container, widget);
588 path_bar->down_slider_button = NULL;
592 children = path_bar->button_list;
595 if (widget == BUTTON_DATA (children->data)->button)
597 gtk_path_bar_remove_1 (container, widget);
598 path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
599 g_list_free (children);
603 children = children->next;
608 gtk_path_bar_forall (GtkContainer *container,
609 gboolean include_internals,
610 GtkCallback callback,
611 gpointer callback_data)
613 GtkPathBar *path_bar;
616 g_return_if_fail (callback != NULL);
617 path_bar = GTK_PATH_BAR (container);
619 children = path_bar->button_list;
623 child = BUTTON_DATA (children->data)->button;
624 children = children->next;
626 (* callback) (child, callback_data);
629 if (path_bar->up_slider_button)
630 (* callback) (path_bar->up_slider_button, callback_data);
632 if (path_bar->down_slider_button)
633 (* callback) (path_bar->down_slider_button, callback_data);
637 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
640 GList *down_button = NULL;
641 GList *up_button = NULL;
642 gint space_available;
645 GtkTextDirection direction;
647 if (path_bar->ignore_click)
649 path_bar->ignore_click = FALSE;
653 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
655 border_width = GTK_CONTAINER (path_bar)->border_width;
656 direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
658 /* We find the button at the 'down' end that we have to make
660 for (list = path_bar->button_list; list; list = list->next)
662 if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
669 /* Find the last visible button on the 'up' end
671 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
673 if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
680 space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
681 if (direction == GTK_TEXT_DIR_RTL)
682 space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
684 space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
685 (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
687 /* We have space_available extra space that's not being used. We
688 * need space_needed space to make the button fit. So we walk down
689 * from the end, removing buttons until we get all the space we
691 while (space_available < space_needed)
693 space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
694 up_button = up_button->prev;
695 path_bar->first_scrolled_button = up_button;
700 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
704 if (path_bar->ignore_click)
706 path_bar->ignore_click = FALSE;
710 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
712 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
714 if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
716 if (list->prev == path_bar->fake_root)
717 path_bar->fake_root = NULL;
718 path_bar->first_scrolled_button = list;
725 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
727 gboolean retval = FALSE;
729 GDK_THREADS_ENTER ();
733 if (GTK_WIDGET_HAS_FOCUS (path_bar->up_slider_button))
734 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
735 else if (GTK_WIDGET_HAS_FOCUS (path_bar->down_slider_button))
736 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
738 if (path_bar->need_timer)
740 path_bar->need_timer = FALSE;
742 path_bar->timer = g_timeout_add (SCROLL_TIMEOUT,
743 (GSourceFunc)gtk_path_bar_scroll_timeout,
752 GDK_THREADS_LEAVE ();
758 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
762 g_source_remove (path_bar->timer);
764 path_bar->need_timer = FALSE;
769 gtk_path_bar_slider_button_press (GtkWidget *widget,
770 GdkEventButton *event,
771 GtkPathBar *path_bar)
773 if (!GTK_WIDGET_HAS_FOCUS (widget))
774 gtk_widget_grab_focus (widget);
776 if (event->type != GDK_BUTTON_PRESS || event->button != 1)
779 path_bar->ignore_click = FALSE;
781 if (widget == path_bar->up_slider_button)
782 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
783 else if (widget == path_bar->down_slider_button)
784 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
786 if (!path_bar->timer)
788 path_bar->need_timer = TRUE;
789 path_bar->timer = g_timeout_add (INITIAL_SCROLL_TIMEOUT,
790 (GSourceFunc)gtk_path_bar_scroll_timeout,
798 gtk_path_bar_slider_button_release (GtkWidget *widget,
799 GdkEventButton *event,
800 GtkPathBar *path_bar)
802 if (event->type != GDK_BUTTON_RELEASE)
805 path_bar->ignore_click = TRUE;
806 gtk_path_bar_stop_scrolling (path_bar);
812 gtk_path_bar_grab_notify (GtkWidget *widget,
813 gboolean was_grabbed)
816 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
820 gtk_path_bar_state_changed (GtkWidget *widget,
821 GtkStateType previous_state)
823 if (!GTK_WIDGET_IS_SENSITIVE (widget))
824 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
828 /* Changes the icons wherever it is needed */
830 reload_icons (GtkPathBar *path_bar)
834 if (path_bar->root_icon)
836 g_object_unref (path_bar->root_icon);
837 path_bar->root_icon = NULL;
839 if (path_bar->home_icon)
841 g_object_unref (path_bar->home_icon);
842 path_bar->home_icon = NULL;
844 if (path_bar->desktop_icon)
846 g_object_unref (path_bar->desktop_icon);
847 path_bar->desktop_icon = NULL;
850 for (list = path_bar->button_list; list; list = list->next)
852 ButtonData *button_data;
853 gboolean current_dir;
855 button_data = BUTTON_DATA (list->data);
856 if (button_data->type != NORMAL_BUTTON)
858 current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
859 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
866 change_icon_theme (GtkPathBar *path_bar)
868 GtkSettings *settings;
871 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
873 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
874 path_bar->icon_size = MAX (width, height);
876 path_bar->icon_size = FALLBACK_ICON_SIZE;
878 reload_icons (path_bar);
880 /* Callback used when a GtkSettings value changes */
882 settings_notify_cb (GObject *object,
884 GtkPathBar *path_bar)
888 name = g_param_spec_get_name (pspec);
890 if (! strcmp (name, "gtk-icon-theme-name") ||
891 ! strcmp (name, "gtk-icon-sizes"))
892 change_icon_theme (path_bar);
896 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
898 GtkSettings *settings;
900 if (path_bar->settings_signal_id)
903 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
904 path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
906 change_icon_theme (path_bar);
909 /* Public functions and their helpers */
911 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
913 while (path_bar->button_list != NULL)
915 gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
917 path_bar->first_scrolled_button = NULL;
918 path_bar->fake_root = NULL;
922 button_clicked_cb (GtkWidget *button,
925 ButtonData *button_data;
926 GtkPathBar *path_bar;
928 gboolean child_is_hidden;
930 button_data = BUTTON_DATA (data);
931 if (button_data->ignore_changes)
934 path_bar = GTK_PATH_BAR (button->parent);
936 button_list = g_list_find (path_bar->button_list, button_data);
937 g_assert (button_list != NULL);
939 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
941 if (button_list->prev)
943 ButtonData *child_data;
945 child_data = BUTTON_DATA (button_list->prev->data);
946 child_is_hidden = child_data->file_is_hidden;
949 child_is_hidden = FALSE;
951 g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0, button_data->path, child_is_hidden);
955 get_button_image (GtkPathBar *path_bar,
956 ButtonType button_type)
958 GtkFileSystemVolume *volume;
964 if (path_bar->root_icon != NULL)
965 return path_bar->root_icon;
967 volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
971 path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
973 GTK_WIDGET (path_bar),
976 gtk_file_system_volume_free (path_bar->file_system, volume);
978 return path_bar->root_icon;
980 if (path_bar->home_icon != NULL)
981 return path_bar->home_icon;
983 path_bar->home_icon = gtk_file_system_render_icon (path_bar->file_system,
985 GTK_WIDGET (path_bar),
988 return path_bar->home_icon;
990 if (path_bar->desktop_icon != NULL)
991 return path_bar->desktop_icon;
993 path_bar->desktop_icon = gtk_file_system_render_icon (path_bar->file_system,
994 path_bar->desktop_path,
995 GTK_WIDGET (path_bar),
998 return path_bar->desktop_icon;
1007 button_data_free (ButtonData *button_data)
1009 gtk_file_path_free (button_data->path);
1010 g_free (button_data->dir_name);
1011 g_free (button_data);
1015 get_dir_name (ButtonData *button_data)
1017 return button_data->dir_name;
1020 /* We always want to request the same size for the label, whether
1021 * or not the contents are bold
1024 label_size_request_cb (GtkWidget *widget,
1025 GtkRequisition *requisition,
1026 ButtonData *button_data)
1028 const gchar *dir_name = get_dir_name (button_data);
1029 PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1030 gint bold_width, bold_height;
1033 pango_layout_get_pixel_size (layout, &requisition->width, &requisition->height);
1035 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1036 pango_layout_set_markup (layout, markup, -1);
1039 pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1040 requisition->width = MAX (requisition->width, bold_width);
1041 requisition->height = MAX (requisition->height, bold_height);
1043 g_object_unref (layout);
1047 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1048 ButtonData *button_data,
1049 gboolean current_dir)
1051 const gchar *dir_name = get_dir_name (button_data);
1053 if (button_data->label != NULL)
1059 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1060 gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1065 gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1069 if (button_data->image != NULL)
1072 pixbuf = get_button_image (path_bar, button_data->type);
1073 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), pixbuf);
1076 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1078 button_data->ignore_changes = TRUE;
1079 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1080 button_data->ignore_changes = FALSE;
1085 find_button_type (GtkPathBar *path_bar,
1088 if (path_bar->root_path != NULL &&
1089 ! gtk_file_path_compare (path, path_bar->root_path))
1091 if (path_bar->home_path != NULL &&
1092 ! gtk_file_path_compare (path, path_bar->home_path))
1094 if (path_bar->desktop_path != NULL &&
1095 ! gtk_file_path_compare (path, path_bar->desktop_path))
1096 return DESKTOP_BUTTON;
1098 return NORMAL_BUTTON;
1102 button_drag_data_get_cb (GtkWidget *widget,
1103 GdkDragContext *context,
1104 GtkSelectionData *selection_data,
1109 ButtonData *button_data;
1110 GtkPathBar *path_bar;
1115 path_bar = GTK_PATH_BAR (widget->parent); /* the button's parent *is* the path bar */
1117 uri = gtk_file_system_path_to_uri (path_bar->file_system, button_data->path);
1118 uri_list = g_strconcat (uri, "\r\n", NULL);
1121 gtk_selection_data_set (selection_data,
1122 selection_data->target,
1130 make_directory_button (GtkPathBar *path_bar,
1131 const char *dir_name,
1133 gboolean current_dir,
1134 gboolean file_is_hidden)
1136 const GtkTargetEntry targets[] = {
1137 { "text/uri-list", 0, 0 }
1140 GtkWidget *child = NULL;
1141 GtkWidget *label_alignment = NULL;
1142 ButtonData *button_data;
1144 file_is_hidden = !! file_is_hidden;
1145 /* Is it a special button? */
1146 button_data = g_new0 (ButtonData, 1);
1148 button_data->type = find_button_type (path_bar, path);
1149 button_data->button = gtk_toggle_button_new ();
1151 switch (button_data->type)
1154 button_data->image = gtk_image_new ();
1155 child = button_data->image;
1156 button_data->label = NULL;
1159 case DESKTOP_BUTTON:
1160 button_data->image = gtk_image_new ();
1161 button_data->label = gtk_label_new (NULL);
1162 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1163 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1164 child = gtk_hbox_new (FALSE, 2);
1165 gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1166 gtk_box_pack_start (GTK_BOX (child), label_alignment, FALSE, FALSE, 0);
1170 button_data->label = gtk_label_new (NULL);
1171 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1172 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1173 child = label_alignment;
1174 button_data->image = NULL;
1177 /* label_alignment is created because we can't override size-request
1178 * on label itself and still have the contents of the label centered
1179 * properly in the label's requisition
1181 if (label_alignment)
1182 g_signal_connect (label_alignment, "size-request",
1183 G_CALLBACK (label_size_request_cb), button_data);
1185 button_data->dir_name = g_strdup (dir_name);
1186 button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
1187 button_data->file_is_hidden = file_is_hidden;
1189 gtk_container_add (GTK_CONTAINER (button_data->button), child);
1190 gtk_widget_show_all (button_data->button);
1192 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1194 g_signal_connect (button_data->button, "clicked",
1195 G_CALLBACK (button_clicked_cb),
1197 g_object_weak_ref (G_OBJECT (button_data->button),
1198 (GWeakNotify) button_data_free, button_data);
1200 gtk_drag_source_set (button_data->button,
1203 G_N_ELEMENTS (targets),
1205 g_signal_connect (button_data->button, "drag-data-get",
1206 G_CALLBACK (button_drag_data_get_cb), button_data);
1212 gtk_path_bar_check_parent_path (GtkPathBar *path_bar,
1213 const GtkFilePath *file_path,
1214 GtkFileSystem *file_system)
1217 GList *current_path = NULL;
1218 gboolean need_new_fake_root = FALSE;
1220 for (list = path_bar->button_list; list; list = list->next)
1222 ButtonData *button_data;
1224 button_data = list->data;
1225 if (! gtk_file_path_compare (file_path, button_data->path))
1227 current_path = list;
1230 if (list == path_bar->fake_root)
1231 need_new_fake_root = TRUE;
1236 if (need_new_fake_root)
1238 path_bar->fake_root = NULL;
1239 for (list = current_path; list; list = list->next)
1241 ButtonData *button_data;
1243 button_data = list->data;
1244 if (BUTTON_IS_FAKE_ROOT (button_data))
1246 path_bar->fake_root = list;
1252 for (list = path_bar->button_list; list; list = list->next)
1254 gtk_path_bar_update_button_appearance (path_bar,
1255 BUTTON_DATA (list->data),
1256 (list == current_path) ? TRUE : FALSE);
1259 if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1261 path_bar->first_scrolled_button = current_path;
1262 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1271 _gtk_path_bar_set_path (GtkPathBar *path_bar,
1272 const GtkFilePath *file_path,
1273 const gboolean keep_trail,
1277 gboolean first_directory = TRUE;
1279 GList *new_buttons = NULL;
1280 GList *fake_root = NULL;
1282 g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
1283 g_return_val_if_fail (file_path != NULL, FALSE);
1287 /* Check whether the new path is already present in the pathbar as buttons.
1288 * This could be a parent directory or a previous selected subdirectory.
1291 gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
1294 path = gtk_file_path_copy (file_path);
1296 gtk_widget_push_composite_child ();
1298 while (path != NULL)
1300 GtkFilePath *parent_path = NULL;
1301 ButtonData *button_data;
1302 const gchar *display_name;
1304 GtkFileFolder *file_folder;
1305 GtkFileInfo *file_info;
1308 valid = gtk_file_system_get_parent (path_bar->file_system,
1315 gtk_file_path_free (path);
1319 file_folder = gtk_file_system_get_folder (path_bar->file_system,
1320 parent_path ? parent_path : path,
1321 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1326 gtk_file_path_free (parent_path);
1327 gtk_file_path_free (path);
1331 file_info = gtk_file_folder_get_info (file_folder, parent_path ? path : NULL, error);
1332 g_object_unref (file_folder);
1337 gtk_file_path_free (parent_path);
1338 gtk_file_path_free (path);
1342 display_name = gtk_file_info_get_display_name (file_info);
1343 is_hidden = gtk_file_info_get_is_hidden (file_info);
1345 button_data = make_directory_button (path_bar, display_name, path, first_directory, is_hidden);
1346 gtk_file_info_free (file_info);
1347 gtk_file_path_free (path);
1349 new_buttons = g_list_prepend (new_buttons, button_data);
1351 if (BUTTON_IS_FAKE_ROOT (button_data))
1352 fake_root = new_buttons;
1355 first_directory = FALSE;
1362 gtk_path_bar_clear_buttons (path_bar);
1363 path_bar->button_list = g_list_reverse (new_buttons);
1364 path_bar->fake_root = fake_root;
1366 for (l = path_bar->button_list; l; l = l->next)
1368 GtkWidget *button = BUTTON_DATA (l->data)->button;
1369 gtk_container_add (GTK_CONTAINER (path_bar), button);
1376 for (l = new_buttons; l; l = l->next)
1378 ButtonData *button_data;
1380 button_data = BUTTON_DATA (l->data);
1381 gtk_widget_destroy (button_data->button);
1384 g_list_free (new_buttons);
1387 gtk_widget_pop_composite_child ();
1392 /* FIXME: This should be a construct-only property */
1394 _gtk_path_bar_set_file_system (GtkPathBar *path_bar,
1395 GtkFileSystem *file_system)
1400 g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1402 g_assert (path_bar->file_system == NULL);
1404 path_bar->file_system = g_object_ref (file_system);
1406 home = g_get_home_dir ();
1409 path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1410 /* FIXME: Need file system backend specific way of getting the
1413 desktop = g_build_filename (home, "Desktop", NULL);
1414 path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1419 path_bar->home_path = NULL;
1420 path_bar->desktop_path = NULL;
1422 path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1427 * @path_bar: a #GtkPathBar
1429 * If the selected button in the pathbar is not the furthest button "up" (in the
1430 * root direction), act as if the user clicked on the next button up.
1433 _gtk_path_bar_up (GtkPathBar *path_bar)
1437 for (l = path_bar->button_list; l; l = l->next)
1439 GtkWidget *button = BUTTON_DATA (l->data)->button;
1440 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1444 GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1445 button_clicked_cb (next_button, l->next->data);
1453 * _gtk_path_bar_down:
1454 * @path_bar: a #GtkPathBar
1456 * If the selected button in the pathbar is not the furthest button "down" (in the
1457 * leaf direction), act as if the user clicked on the next button down.
1460 _gtk_path_bar_down (GtkPathBar *path_bar)
1464 for (l = path_bar->button_list; l; l = l->next)
1466 GtkWidget *button = BUTTON_DATA (l->data)->button;
1467 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1471 GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1472 button_clicked_cb (prev_button, l->prev->data);