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,
82 static void gtk_path_bar_finalize (GObject *object);
83 static void gtk_path_bar_dispose (GObject *object);
84 static void gtk_path_bar_size_request (GtkWidget *widget,
85 GtkRequisition *requisition);
86 static void gtk_path_bar_unmap (GtkWidget *widget);
87 static void gtk_path_bar_size_allocate (GtkWidget *widget,
88 GtkAllocation *allocation);
89 static void gtk_path_bar_add (GtkContainer *container,
91 static void gtk_path_bar_remove (GtkContainer *container,
93 static void gtk_path_bar_forall (GtkContainer *container,
94 gboolean include_internals,
96 gpointer callback_data);
97 static void gtk_path_bar_scroll_up (GtkWidget *button,
98 GtkPathBar *path_bar);
99 static void gtk_path_bar_scroll_down (GtkWidget *button,
100 GtkPathBar *path_bar);
101 static void gtk_path_bar_stop_scrolling (GtkPathBar *path_bar);
102 static gboolean gtk_path_bar_slider_button_press (GtkWidget *widget,
103 GdkEventButton *event,
104 GtkPathBar *path_bar);
105 static gboolean gtk_path_bar_slider_button_release(GtkWidget *widget,
106 GdkEventButton *event,
107 GtkPathBar *path_bar);
108 static void gtk_path_bar_grab_notify (GtkWidget *widget,
109 gboolean was_grabbed);
110 static void gtk_path_bar_state_changed (GtkWidget *widget,
111 GtkStateType previous_state);
112 static void gtk_path_bar_style_set (GtkWidget *widget,
113 GtkStyle *previous_style);
114 static void gtk_path_bar_screen_changed (GtkWidget *widget,
115 GdkScreen *previous_screen);
116 static void gtk_path_bar_check_icon_theme (GtkPathBar *path_bar);
117 static void gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
118 ButtonData *button_data,
119 gboolean current_dir);
122 get_slider_button (GtkPathBar *path_bar,
123 GtkArrowType arrow_type)
127 gtk_widget_push_composite_child ();
129 button = gtk_button_new ();
130 gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
131 gtk_container_add (GTK_CONTAINER (path_bar), button);
132 gtk_widget_show_all (button);
134 gtk_widget_pop_composite_child ();
140 gtk_path_bar_init (GtkPathBar *path_bar)
142 GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
143 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
145 path_bar->spacing = 3;
146 path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
147 path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
148 path_bar->icon_size = FALLBACK_ICON_SIZE;
150 g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
151 g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
153 g_signal_connect (path_bar->up_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
154 g_signal_connect (path_bar->up_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
155 g_signal_connect (path_bar->down_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
156 g_signal_connect (path_bar->down_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
160 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
162 GObjectClass *gobject_class;
163 GtkObjectClass *object_class;
164 GtkWidgetClass *widget_class;
165 GtkContainerClass *container_class;
167 gobject_class = (GObjectClass *) path_bar_class;
168 object_class = (GtkObjectClass *) path_bar_class;
169 widget_class = (GtkWidgetClass *) path_bar_class;
170 container_class = (GtkContainerClass *) path_bar_class;
172 gobject_class->finalize = gtk_path_bar_finalize;
173 gobject_class->dispose = gtk_path_bar_dispose;
175 widget_class->size_request = gtk_path_bar_size_request;
176 widget_class->unmap = gtk_path_bar_unmap;
177 widget_class->size_allocate = gtk_path_bar_size_allocate;
178 widget_class->style_set = gtk_path_bar_style_set;
179 widget_class->screen_changed = gtk_path_bar_screen_changed;
180 widget_class->grab_notify = gtk_path_bar_grab_notify;
181 widget_class->state_changed = gtk_path_bar_state_changed;
183 container_class->add = gtk_path_bar_add;
184 container_class->forall = gtk_path_bar_forall;
185 container_class->remove = gtk_path_bar_remove;
187 /* container_class->child_type = gtk_path_bar_child_type;*/
189 path_bar_signals [PATH_CLICKED] =
190 g_signal_new ("path-clicked",
191 G_OBJECT_CLASS_TYPE (object_class),
193 G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
195 _gtk_marshal_VOID__POINTER_BOOLEAN,
203 gtk_path_bar_finalize (GObject *object)
205 GtkPathBar *path_bar;
207 path_bar = GTK_PATH_BAR (object);
209 gtk_path_bar_stop_scrolling (path_bar);
211 g_list_free (path_bar->button_list);
212 if (path_bar->root_path)
213 gtk_file_path_free (path_bar->root_path);
214 if (path_bar->home_path)
215 gtk_file_path_free (path_bar->home_path);
216 if (path_bar->desktop_path)
217 gtk_file_path_free (path_bar->desktop_path);
219 if (path_bar->root_icon)
220 g_object_unref (path_bar->root_icon);
221 if (path_bar->home_icon)
222 g_object_unref (path_bar->home_icon);
223 if (path_bar->desktop_icon)
224 g_object_unref (path_bar->desktop_icon);
226 if (path_bar->file_system)
227 g_object_unref (path_bar->file_system);
229 G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
232 /* Removes the settings signal handler. It's safe to call multiple times */
234 remove_settings_signal (GtkPathBar *path_bar,
237 if (path_bar->settings_signal_id)
239 GtkSettings *settings;
241 settings = gtk_settings_get_for_screen (screen);
242 g_signal_handler_disconnect (settings,
243 path_bar->settings_signal_id);
244 path_bar->settings_signal_id = 0;
249 gtk_path_bar_dispose (GObject *object)
251 remove_settings_signal (GTK_PATH_BAR (object),
252 gtk_widget_get_screen (GTK_WIDGET (object)));
254 G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
259 * Ideally, our size is determined by another widget, and we are just filling
263 gtk_path_bar_size_request (GtkWidget *widget,
264 GtkRequisition *requisition)
266 ButtonData *button_data;
267 GtkPathBar *path_bar;
268 GtkRequisition child_requisition;
271 path_bar = GTK_PATH_BAR (widget);
273 requisition->width = 0;
274 requisition->height = 0;
276 for (list = path_bar->button_list; list; list = list->next)
278 button_data = BUTTON_DATA (list->data);
279 gtk_widget_size_request (button_data->button, &child_requisition);
280 requisition->width = MAX (child_requisition.width, requisition->width);
281 requisition->height = MAX (child_requisition.height, requisition->height);
284 /* Add space for slider, if we have more than one path */
285 /* Theoretically, the slider could be bigger than the other button. But we're
286 * not going to worry about that now.
288 path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
289 if (path_bar->button_list && path_bar->button_list->next != NULL)
290 requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
292 gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
293 gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
295 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
296 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
298 widget->requisition = *requisition;
302 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
304 if (path_bar->button_list)
308 button = BUTTON_DATA (path_bar->button_list->data)->button;
309 if (gtk_widget_get_child_visible (button))
310 gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
312 gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
314 button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
315 if (gtk_widget_get_child_visible (button))
316 gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
318 gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
323 gtk_path_bar_unmap (GtkWidget *widget)
325 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
327 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unmap (widget);
330 /* This is a tad complicated
333 gtk_path_bar_size_allocate (GtkWidget *widget,
334 GtkAllocation *allocation)
337 GtkPathBar *path_bar = GTK_PATH_BAR (widget);
338 GtkTextDirection direction;
339 GtkAllocation child_allocation;
340 GList *list, *first_button;
342 gint allocation_width;
344 gboolean need_sliders = FALSE;
345 gint up_slider_offset = 0;
346 gint down_slider_offset = 0;
348 widget->allocation = *allocation;
350 /* No path is set; we don't have to allocate anything. */
351 if (path_bar->button_list == NULL)
354 direction = gtk_widget_get_direction (widget);
355 border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
356 allocation_width = allocation->width - 2 * border_width;
358 /* First, we check to see if we need the scrollbars. */
359 if (path_bar->fake_root)
360 width = path_bar->spacing + path_bar->slider_width;
364 for (list = path_bar->button_list; list; list = list->next)
366 child = BUTTON_DATA (list->data)->button;
368 width += child->requisition.width + path_bar->spacing;
369 if (list == path_bar->fake_root)
373 if (width <= allocation_width)
375 if (path_bar->fake_root)
376 first_button = path_bar->fake_root;
378 first_button = g_list_last (path_bar->button_list);
382 gboolean reached_end = FALSE;
383 gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
385 if (path_bar->first_scrolled_button)
386 first_button = path_bar->first_scrolled_button;
388 first_button = path_bar->button_list;
391 /* To see how much space we have, and how many buttons we can display.
392 * We start at the first button, count forward until hit the new
393 * button, then count backwards.
395 /* Count down the path chain towards the end. */
396 width = BUTTON_DATA (first_button->data)->button->requisition.width;
397 list = first_button->prev;
398 while (list && !reached_end)
400 child = BUTTON_DATA (list->data)->button;
402 if (width + child->requisition.width +
403 path_bar->spacing + slider_space > allocation_width)
405 else if (list == path_bar->fake_root)
408 width += child->requisition.width + path_bar->spacing;
413 /* Finally, we walk up, seeing how many of the previous buttons we can
415 while (first_button->next && !reached_end)
417 child = BUTTON_DATA (first_button->next->data)->button;
419 if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
425 width += child->requisition.width + path_bar->spacing;
426 if (first_button == path_bar->fake_root)
428 first_button = first_button->next;
433 /* Now, we allocate space to the buttons */
434 child_allocation.y = allocation->y + border_width;
435 child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
437 if (direction == GTK_TEXT_DIR_RTL)
439 child_allocation.x = allocation->x + allocation->width - border_width;
440 if (need_sliders || path_bar->fake_root)
442 child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
443 up_slider_offset = allocation->width - border_width - path_bar->slider_width;
448 child_allocation.x = allocation->x + border_width;
449 if (need_sliders || path_bar->fake_root)
451 up_slider_offset = border_width;
452 child_allocation.x += (path_bar->spacing + path_bar->slider_width);
456 for (list = first_button; list; list = list->prev)
458 child = BUTTON_DATA (list->data)->button;
460 child_allocation.width = child->requisition.width;
461 if (direction == GTK_TEXT_DIR_RTL)
462 child_allocation.x -= child_allocation.width;
464 /* Check to see if we've don't have any more space to allocate buttons */
465 if (need_sliders && direction == GTK_TEXT_DIR_RTL)
467 if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
470 else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
472 if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
473 widget->allocation.x + border_width + allocation_width)
477 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
478 gtk_widget_size_allocate (child, &child_allocation);
480 if (direction == GTK_TEXT_DIR_RTL)
482 child_allocation.x -= path_bar->spacing;
483 down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
484 down_slider_offset = border_width;
488 down_slider_offset = child_allocation.x - widget->allocation.x;
489 down_slider_offset = allocation->width - border_width - path_bar->slider_width;
490 child_allocation.x += child_allocation.width + path_bar->spacing;
493 /* Now we go hide all the widgets that don't fit */
496 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
499 for (list = first_button->next; list; list = list->next)
501 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
504 if (need_sliders || path_bar->fake_root)
506 child_allocation.width = path_bar->slider_width;
507 child_allocation.x = up_slider_offset + allocation->x;
508 gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
510 gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
511 gtk_widget_show_all (path_bar->up_slider_button);
514 gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
518 child_allocation.width = path_bar->slider_width;
519 child_allocation.x = down_slider_offset + allocation->x;
520 gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
522 gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
523 gtk_widget_show_all (path_bar->down_slider_button);
524 gtk_path_bar_update_slider_buttons (path_bar);
527 gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
531 gtk_path_bar_style_set (GtkWidget *widget,
532 GtkStyle *previous_style)
534 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
535 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
537 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
541 gtk_path_bar_screen_changed (GtkWidget *widget,
542 GdkScreen *previous_screen)
544 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
545 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
547 /* We might nave a new settings, so we remove the old one */
549 remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
551 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
555 gtk_path_bar_add (GtkContainer *container,
558 gtk_widget_set_parent (widget, GTK_WIDGET (container));
562 gtk_path_bar_remove_1 (GtkContainer *container,
565 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
566 gtk_widget_unparent (widget);
568 gtk_widget_queue_resize (GTK_WIDGET (container));
572 gtk_path_bar_remove (GtkContainer *container,
575 GtkPathBar *path_bar;
578 path_bar = GTK_PATH_BAR (container);
580 if (widget == path_bar->up_slider_button)
582 gtk_path_bar_remove_1 (container, widget);
583 path_bar->up_slider_button = NULL;
587 if (widget == path_bar->down_slider_button)
589 gtk_path_bar_remove_1 (container, widget);
590 path_bar->down_slider_button = NULL;
594 children = path_bar->button_list;
597 if (widget == BUTTON_DATA (children->data)->button)
599 gtk_path_bar_remove_1 (container, widget);
600 path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
601 g_list_free (children);
605 children = children->next;
610 gtk_path_bar_forall (GtkContainer *container,
611 gboolean include_internals,
612 GtkCallback callback,
613 gpointer callback_data)
615 GtkPathBar *path_bar;
618 g_return_if_fail (callback != NULL);
619 path_bar = GTK_PATH_BAR (container);
621 children = path_bar->button_list;
625 child = BUTTON_DATA (children->data)->button;
626 children = children->next;
628 (* callback) (child, callback_data);
631 if (path_bar->up_slider_button)
632 (* callback) (path_bar->up_slider_button, callback_data);
634 if (path_bar->down_slider_button)
635 (* callback) (path_bar->down_slider_button, callback_data);
639 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
642 GList *down_button = NULL;
643 GList *up_button = NULL;
644 gint space_available;
647 GtkTextDirection direction;
649 if (path_bar->ignore_click)
651 path_bar->ignore_click = FALSE;
655 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
657 border_width = GTK_CONTAINER (path_bar)->border_width;
658 direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
660 /* We find the button at the 'down' end that we have to make
662 for (list = path_bar->button_list; list; list = list->next)
664 if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
671 /* Find the last visible button on the 'up' end
673 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
675 if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
682 space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
683 if (direction == GTK_TEXT_DIR_RTL)
684 space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
686 space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
687 (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
689 /* We have space_available extra space that's not being used. We
690 * need space_needed space to make the button fit. So we walk down
691 * from the end, removing buttons until we get all the space we
693 while (space_available < space_needed)
695 space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
696 up_button = up_button->prev;
697 path_bar->first_scrolled_button = up_button;
702 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
706 if (path_bar->ignore_click)
708 path_bar->ignore_click = FALSE;
712 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
714 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
716 if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
718 if (list->prev == path_bar->fake_root)
719 path_bar->fake_root = NULL;
720 path_bar->first_scrolled_button = list;
727 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
729 gboolean retval = FALSE;
731 GDK_THREADS_ENTER ();
735 if (GTK_WIDGET_HAS_FOCUS (path_bar->up_slider_button))
736 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
737 else if (GTK_WIDGET_HAS_FOCUS (path_bar->down_slider_button))
738 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
740 if (path_bar->need_timer)
742 path_bar->need_timer = FALSE;
744 path_bar->timer = g_timeout_add (SCROLL_TIMEOUT,
745 (GSourceFunc)gtk_path_bar_scroll_timeout,
754 GDK_THREADS_LEAVE ();
760 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
764 g_source_remove (path_bar->timer);
766 path_bar->need_timer = FALSE;
771 gtk_path_bar_slider_button_press (GtkWidget *widget,
772 GdkEventButton *event,
773 GtkPathBar *path_bar)
775 if (!GTK_WIDGET_HAS_FOCUS (widget))
776 gtk_widget_grab_focus (widget);
778 if (event->type != GDK_BUTTON_PRESS || event->button != 1)
781 path_bar->ignore_click = FALSE;
783 if (widget == path_bar->up_slider_button)
784 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
785 else if (widget == path_bar->down_slider_button)
786 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
788 if (!path_bar->timer)
790 path_bar->need_timer = TRUE;
791 path_bar->timer = g_timeout_add (INITIAL_SCROLL_TIMEOUT,
792 (GSourceFunc)gtk_path_bar_scroll_timeout,
800 gtk_path_bar_slider_button_release (GtkWidget *widget,
801 GdkEventButton *event,
802 GtkPathBar *path_bar)
804 if (event->type != GDK_BUTTON_RELEASE)
807 path_bar->ignore_click = TRUE;
808 gtk_path_bar_stop_scrolling (path_bar);
814 gtk_path_bar_grab_notify (GtkWidget *widget,
815 gboolean was_grabbed)
818 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
822 gtk_path_bar_state_changed (GtkWidget *widget,
823 GtkStateType previous_state)
825 if (!GTK_WIDGET_IS_SENSITIVE (widget))
826 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
830 /* Changes the icons wherever it is needed */
832 reload_icons (GtkPathBar *path_bar)
836 if (path_bar->root_icon)
838 g_object_unref (path_bar->root_icon);
839 path_bar->root_icon = NULL;
841 if (path_bar->home_icon)
843 g_object_unref (path_bar->home_icon);
844 path_bar->home_icon = NULL;
846 if (path_bar->desktop_icon)
848 g_object_unref (path_bar->desktop_icon);
849 path_bar->desktop_icon = NULL;
852 for (list = path_bar->button_list; list; list = list->next)
854 ButtonData *button_data;
855 gboolean current_dir;
857 button_data = BUTTON_DATA (list->data);
858 if (button_data->type != NORMAL_BUTTON)
860 current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
861 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
868 change_icon_theme (GtkPathBar *path_bar)
870 GtkSettings *settings;
873 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
875 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
876 path_bar->icon_size = MAX (width, height);
878 path_bar->icon_size = FALLBACK_ICON_SIZE;
880 reload_icons (path_bar);
882 /* Callback used when a GtkSettings value changes */
884 settings_notify_cb (GObject *object,
886 GtkPathBar *path_bar)
890 name = g_param_spec_get_name (pspec);
892 if (! strcmp (name, "gtk-icon-theme-name") ||
893 ! strcmp (name, "gtk-icon-sizes"))
894 change_icon_theme (path_bar);
898 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
900 GtkSettings *settings;
902 if (path_bar->settings_signal_id)
905 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
906 path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
908 change_icon_theme (path_bar);
911 /* Public functions and their helpers */
913 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
915 while (path_bar->button_list != NULL)
917 gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
919 path_bar->first_scrolled_button = NULL;
920 path_bar->fake_root = NULL;
924 button_clicked_cb (GtkWidget *button,
927 ButtonData *button_data;
928 GtkPathBar *path_bar;
930 gboolean child_is_hidden;
932 button_data = BUTTON_DATA (data);
933 if (button_data->ignore_changes)
936 path_bar = GTK_PATH_BAR (button->parent);
938 button_list = g_list_find (path_bar->button_list, button_data);
939 g_assert (button_list != NULL);
941 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
943 if (button_list->prev)
945 ButtonData *child_data;
947 child_data = BUTTON_DATA (button_list->prev->data);
948 child_is_hidden = child_data->file_is_hidden;
951 child_is_hidden = FALSE;
953 g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0, button_data->path, child_is_hidden);
957 get_button_image (GtkPathBar *path_bar,
958 ButtonType button_type)
960 GtkFileSystemVolume *volume;
966 if (path_bar->root_icon != NULL)
967 return path_bar->root_icon;
969 volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
973 path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
975 GTK_WIDGET (path_bar),
978 gtk_file_system_volume_free (path_bar->file_system, volume);
980 return path_bar->root_icon;
982 if (path_bar->home_icon != NULL)
983 return path_bar->home_icon;
985 path_bar->home_icon = gtk_file_system_render_icon (path_bar->file_system,
987 GTK_WIDGET (path_bar),
990 return path_bar->home_icon;
992 if (path_bar->desktop_icon != NULL)
993 return path_bar->desktop_icon;
995 path_bar->desktop_icon = gtk_file_system_render_icon (path_bar->file_system,
996 path_bar->desktop_path,
997 GTK_WIDGET (path_bar),
1000 return path_bar->desktop_icon;
1009 button_data_free (ButtonData *button_data)
1011 gtk_file_path_free (button_data->path);
1012 g_free (button_data->dir_name);
1013 g_free (button_data);
1017 get_dir_name (ButtonData *button_data)
1019 if (button_data->type == HOME_BUTTON)
1021 else if (button_data->type == DESKTOP_BUTTON)
1022 return _("Desktop");
1024 return button_data->dir_name;
1028 /* We always want to request the same size for the label, whether
1029 * or not the contents are bold
1032 label_size_request_cb (GtkWidget *widget,
1033 GtkRequisition *requisition,
1034 ButtonData *button_data)
1036 const gchar *dir_name = get_dir_name (button_data);
1037 PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1038 gint bold_width, bold_height;
1041 pango_layout_get_pixel_size (layout, &requisition->width, &requisition->height);
1043 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1044 pango_layout_set_markup (layout, markup, -1);
1047 pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1048 requisition->width = MAX (requisition->width, bold_width);
1049 requisition->height = MAX (requisition->height, bold_height);
1051 g_object_unref (layout);
1055 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1056 ButtonData *button_data,
1057 gboolean current_dir)
1059 const gchar *dir_name = get_dir_name (button_data);
1061 if (button_data->label != NULL)
1067 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1068 gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1073 gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1077 if (button_data->image != NULL)
1080 pixbuf = get_button_image (path_bar, button_data->type);
1081 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), pixbuf);
1084 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1086 button_data->ignore_changes = TRUE;
1087 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1088 button_data->ignore_changes = FALSE;
1093 find_button_type (GtkPathBar *path_bar,
1096 if (path_bar->root_path != NULL &&
1097 ! gtk_file_path_compare (path, path_bar->root_path))
1099 if (path_bar->home_path != NULL &&
1100 ! gtk_file_path_compare (path, path_bar->home_path))
1102 if (path_bar->desktop_path != NULL &&
1103 ! gtk_file_path_compare (path, path_bar->desktop_path))
1104 return DESKTOP_BUTTON;
1106 return NORMAL_BUTTON;
1110 button_drag_data_get_cb (GtkWidget *widget,
1111 GdkDragContext *context,
1112 GtkSelectionData *selection_data,
1117 ButtonData *button_data;
1118 GtkPathBar *path_bar;
1123 path_bar = GTK_PATH_BAR (widget->parent); /* the button's parent *is* the path bar */
1125 uri = gtk_file_system_path_to_uri (path_bar->file_system, button_data->path);
1126 uri_list = g_strconcat (uri, "\r\n", NULL);
1129 gtk_selection_data_set (selection_data,
1130 selection_data->target,
1138 make_directory_button (GtkPathBar *path_bar,
1139 const char *dir_name,
1141 gboolean current_dir,
1142 gboolean file_is_hidden)
1144 const GtkTargetEntry targets[] = {
1145 { "text/uri-list", 0, 0 }
1148 GtkWidget *child = NULL;
1149 GtkWidget *label_alignment = NULL;
1150 ButtonData *button_data;
1152 file_is_hidden = !! file_is_hidden;
1153 /* Is it a special button? */
1154 button_data = g_new0 (ButtonData, 1);
1156 button_data->type = find_button_type (path_bar, path);
1157 button_data->button = gtk_toggle_button_new ();
1159 switch (button_data->type)
1162 button_data->image = gtk_image_new ();
1163 child = button_data->image;
1164 button_data->label = NULL;
1167 case DESKTOP_BUTTON:
1168 button_data->image = gtk_image_new ();
1169 button_data->label = gtk_label_new (NULL);
1170 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1171 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1172 child = gtk_hbox_new (FALSE, 2);
1173 gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1174 gtk_box_pack_start (GTK_BOX (child), label_alignment, FALSE, FALSE, 0);
1178 button_data->label = gtk_label_new (NULL);
1179 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1180 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1181 child = label_alignment;
1182 button_data->image = NULL;
1185 /* label_alignment is created because we can't override size-request
1186 * on label itself and still have the contents of the label centered
1187 * properly in the label's requisition
1189 if (label_alignment)
1190 g_signal_connect (label_alignment, "size-request",
1191 G_CALLBACK (label_size_request_cb), button_data);
1193 button_data->dir_name = g_strdup (dir_name);
1194 button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
1195 button_data->file_is_hidden = file_is_hidden;
1197 gtk_container_add (GTK_CONTAINER (button_data->button), child);
1198 gtk_widget_show_all (button_data->button);
1200 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1202 g_signal_connect (button_data->button, "clicked",
1203 G_CALLBACK (button_clicked_cb),
1205 g_object_weak_ref (G_OBJECT (button_data->button),
1206 (GWeakNotify) button_data_free, button_data);
1208 gtk_drag_source_set (button_data->button,
1211 G_N_ELEMENTS (targets),
1213 g_signal_connect (button_data->button, "drag-data-get",
1214 G_CALLBACK (button_drag_data_get_cb), button_data);
1220 gtk_path_bar_check_parent_path (GtkPathBar *path_bar,
1221 const GtkFilePath *file_path,
1222 GtkFileSystem *file_system)
1225 GList *current_path = NULL;
1226 gboolean need_new_fake_root = FALSE;
1228 for (list = path_bar->button_list; list; list = list->next)
1230 ButtonData *button_data;
1232 button_data = list->data;
1233 if (! gtk_file_path_compare (file_path, button_data->path))
1235 current_path = list;
1238 if (list == path_bar->fake_root)
1239 need_new_fake_root = TRUE;
1244 if (need_new_fake_root)
1246 path_bar->fake_root = NULL;
1247 for (list = current_path; list; list = list->next)
1249 ButtonData *button_data;
1251 button_data = list->data;
1252 if (BUTTON_IS_FAKE_ROOT (button_data))
1254 path_bar->fake_root = list;
1260 for (list = path_bar->button_list; list; list = list->next)
1262 gtk_path_bar_update_button_appearance (path_bar,
1263 BUTTON_DATA (list->data),
1264 (list == current_path) ? TRUE : FALSE);
1267 if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1269 path_bar->first_scrolled_button = current_path;
1270 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1279 _gtk_path_bar_set_path (GtkPathBar *path_bar,
1280 const GtkFilePath *file_path,
1284 gboolean first_directory = TRUE;
1286 GList *new_buttons = NULL;
1287 GList *fake_root = NULL;
1289 g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
1290 g_return_val_if_fail (file_path != NULL, FALSE);
1294 /* Check whether the new path is already present in the pathbar as buttons.
1295 * This could be a parent directory or a previous selected subdirectory.
1297 if (gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
1300 path = gtk_file_path_copy (file_path);
1302 gtk_widget_push_composite_child ();
1304 while (path != NULL)
1306 GtkFilePath *parent_path = NULL;
1307 ButtonData *button_data;
1308 const gchar *display_name;
1310 GtkFileFolder *file_folder;
1311 GtkFileInfo *file_info;
1314 valid = gtk_file_system_get_parent (path_bar->file_system,
1321 gtk_file_path_free (path);
1325 file_folder = gtk_file_system_get_folder (path_bar->file_system,
1326 parent_path ? parent_path : path,
1327 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1332 gtk_file_path_free (parent_path);
1333 gtk_file_path_free (path);
1337 file_info = gtk_file_folder_get_info (file_folder, parent_path ? path : NULL, error);
1338 g_object_unref (file_folder);
1343 gtk_file_path_free (parent_path);
1344 gtk_file_path_free (path);
1348 display_name = gtk_file_info_get_display_name (file_info);
1349 is_hidden = gtk_file_info_get_is_hidden (file_info);
1351 button_data = make_directory_button (path_bar, display_name, path, first_directory, is_hidden);
1352 gtk_file_info_free (file_info);
1353 gtk_file_path_free (path);
1355 new_buttons = g_list_prepend (new_buttons, button_data);
1357 if (BUTTON_IS_FAKE_ROOT (button_data))
1358 fake_root = new_buttons;
1361 first_directory = FALSE;
1368 gtk_path_bar_clear_buttons (path_bar);
1369 path_bar->button_list = g_list_reverse (new_buttons);
1370 path_bar->fake_root = fake_root;
1372 for (l = path_bar->button_list; l; l = l->next)
1374 GtkWidget *button = BUTTON_DATA (l->data)->button;
1375 gtk_container_add (GTK_CONTAINER (path_bar), button);
1382 for (l = new_buttons; l; l = l->next)
1384 ButtonData *button_data;
1386 button_data = BUTTON_DATA (l->data);
1387 gtk_widget_destroy (button_data->button);
1390 g_list_free (new_buttons);
1393 gtk_widget_pop_composite_child ();
1399 /* FIXME: This should be a construct-only property */
1401 _gtk_path_bar_set_file_system (GtkPathBar *path_bar,
1402 GtkFileSystem *file_system)
1407 g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1409 g_assert (path_bar->file_system == NULL);
1411 path_bar->file_system = g_object_ref (file_system);
1413 home = g_get_home_dir ();
1416 path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1417 /* FIXME: Need file system backend specific way of getting the
1420 desktop = g_build_filename (home, "Desktop", NULL);
1421 path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1426 path_bar->home_path = NULL;
1427 path_bar->desktop_path = NULL;
1429 path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1434 * @path_bar: a #GtkPathBar
1436 * If the selected button in the pathbar is not the furthest button "up" (in the
1437 * root direction), act as if the user clicked on the next button up.
1440 _gtk_path_bar_up (GtkPathBar *path_bar)
1444 for (l = path_bar->button_list; l; l = l->next)
1446 GtkWidget *button = BUTTON_DATA (l->data)->button;
1447 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1451 GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1452 button_clicked_cb (next_button, l->next->data);
1460 * _gtk_path_bar_down:
1461 * @path_bar: a #GtkPathBar
1463 * If the selected button in the pathbar is not the furthest button "down" (in the
1464 * leaf direction), act as if the user clicked on the next button down.
1467 _gtk_path_bar_down (GtkPathBar *path_bar)
1471 for (l = path_bar->button_list; l; l = l->next)
1473 GtkWidget *button = BUTTON_DATA (l->data)->button;
1474 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1478 GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1479 button_clicked_cb (prev_button, l->prev->data);