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_DELAY_FACTOR 5
53 static guint path_bar_signals [LAST_SIGNAL] = { 0 };
55 /* Icon size for if we can't get it from the theme */
56 #define FALLBACK_ICON_SIZE 16
58 typedef struct _ButtonData ButtonData;
68 GtkFileSystemHandle *handle;
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_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
129 gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
130 gtk_container_add (GTK_CONTAINER (path_bar), button);
131 gtk_widget_show_all (button);
133 gtk_widget_pop_composite_child ();
139 gtk_path_bar_init (GtkPathBar *path_bar)
141 GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
142 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
144 path_bar->set_path_handle = NULL;
146 path_bar->spacing = 0;
147 path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
148 path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
149 path_bar->icon_size = FALLBACK_ICON_SIZE;
151 g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
152 g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
154 g_signal_connect (path_bar->up_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
155 g_signal_connect (path_bar->up_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
156 g_signal_connect (path_bar->down_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
157 g_signal_connect (path_bar->down_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
161 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
163 GObjectClass *gobject_class;
164 GtkObjectClass *object_class;
165 GtkWidgetClass *widget_class;
166 GtkContainerClass *container_class;
168 gobject_class = (GObjectClass *) path_bar_class;
169 object_class = (GtkObjectClass *) path_bar_class;
170 widget_class = (GtkWidgetClass *) path_bar_class;
171 container_class = (GtkContainerClass *) path_bar_class;
173 gobject_class->finalize = gtk_path_bar_finalize;
174 gobject_class->dispose = gtk_path_bar_dispose;
176 widget_class->size_request = gtk_path_bar_size_request;
177 widget_class->unmap = gtk_path_bar_unmap;
178 widget_class->size_allocate = gtk_path_bar_size_allocate;
179 widget_class->style_set = gtk_path_bar_style_set;
180 widget_class->screen_changed = gtk_path_bar_screen_changed;
181 widget_class->grab_notify = gtk_path_bar_grab_notify;
182 widget_class->state_changed = gtk_path_bar_state_changed;
184 container_class->add = gtk_path_bar_add;
185 container_class->forall = gtk_path_bar_forall;
186 container_class->remove = gtk_path_bar_remove;
188 /* container_class->child_type = gtk_path_bar_child_type;*/
190 path_bar_signals [PATH_CLICKED] =
191 g_signal_new (I_("path-clicked"),
192 G_OBJECT_CLASS_TYPE (object_class),
194 G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
196 _gtk_marshal_VOID__POINTER_POINTER_BOOLEAN,
205 gtk_path_bar_finalize (GObject *object)
207 GtkPathBar *path_bar;
209 path_bar = GTK_PATH_BAR (object);
211 gtk_path_bar_stop_scrolling (path_bar);
213 g_list_free (path_bar->button_list);
214 if (path_bar->root_path)
215 gtk_file_path_free (path_bar->root_path);
216 if (path_bar->home_path)
217 gtk_file_path_free (path_bar->home_path);
218 if (path_bar->desktop_path)
219 gtk_file_path_free (path_bar->desktop_path);
221 if (path_bar->root_icon)
222 g_object_unref (path_bar->root_icon);
223 if (path_bar->home_icon)
224 g_object_unref (path_bar->home_icon);
225 if (path_bar->desktop_icon)
226 g_object_unref (path_bar->desktop_icon);
228 if (path_bar->file_system)
229 g_object_unref (path_bar->file_system);
231 G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
234 /* Removes the settings signal handler. It's safe to call multiple times */
236 remove_settings_signal (GtkPathBar *path_bar,
239 if (path_bar->settings_signal_id)
241 GtkSettings *settings;
243 settings = gtk_settings_get_for_screen (screen);
244 g_signal_handler_disconnect (settings,
245 path_bar->settings_signal_id);
246 path_bar->settings_signal_id = 0;
251 gtk_path_bar_dispose (GObject *object)
253 GtkPathBar *path_bar = GTK_PATH_BAR (object);
255 remove_settings_signal (path_bar, gtk_widget_get_screen (GTK_WIDGET (object)));
257 if (path_bar->set_path_handle)
258 gtk_file_system_cancel_operation (path_bar->set_path_handle);
259 path_bar->set_path_handle = NULL;
261 G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
266 * Ideally, our size is determined by another widget, and we are just filling
270 gtk_path_bar_size_request (GtkWidget *widget,
271 GtkRequisition *requisition)
273 ButtonData *button_data;
274 GtkPathBar *path_bar;
275 GtkRequisition child_requisition;
278 path_bar = GTK_PATH_BAR (widget);
280 requisition->width = 0;
281 requisition->height = 0;
283 for (list = path_bar->button_list; list; list = list->next)
285 button_data = BUTTON_DATA (list->data);
286 gtk_widget_size_request (button_data->button, &child_requisition);
287 requisition->width = MAX (child_requisition.width, requisition->width);
288 requisition->height = MAX (child_requisition.height, requisition->height);
291 /* Add space for slider, if we have more than one path */
292 /* Theoretically, the slider could be bigger than the other button. But we're
293 * not going to worry about that now.
295 path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
296 if (path_bar->button_list && path_bar->button_list->next != NULL)
297 requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
299 gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
300 gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
302 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
303 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
305 widget->requisition = *requisition;
309 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
311 if (path_bar->button_list)
315 button = BUTTON_DATA (path_bar->button_list->data)->button;
316 if (gtk_widget_get_child_visible (button))
318 gtk_path_bar_stop_scrolling (path_bar);
319 gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
322 gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
324 button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
325 if (gtk_widget_get_child_visible (button))
327 gtk_path_bar_stop_scrolling (path_bar);
328 gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
331 gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
336 gtk_path_bar_unmap (GtkWidget *widget)
338 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
340 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unmap (widget);
343 /* This is a tad complicated
346 gtk_path_bar_size_allocate (GtkWidget *widget,
347 GtkAllocation *allocation)
350 GtkPathBar *path_bar = GTK_PATH_BAR (widget);
351 GtkTextDirection direction;
352 GtkAllocation child_allocation;
353 GList *list, *first_button;
355 gint allocation_width;
357 gboolean need_sliders = FALSE;
358 gint up_slider_offset = 0;
359 gint down_slider_offset = 0;
361 widget->allocation = *allocation;
363 /* No path is set; we don't have to allocate anything. */
364 if (path_bar->button_list == NULL)
367 direction = gtk_widget_get_direction (widget);
368 border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
369 allocation_width = allocation->width - 2 * border_width;
371 /* First, we check to see if we need the scrollbars. */
372 if (path_bar->fake_root)
373 width = path_bar->spacing + path_bar->slider_width;
377 for (list = path_bar->button_list; list; list = list->next)
379 child = BUTTON_DATA (list->data)->button;
381 width += child->requisition.width + path_bar->spacing;
382 if (list == path_bar->fake_root)
386 if (width <= allocation_width)
388 if (path_bar->fake_root)
389 first_button = path_bar->fake_root;
391 first_button = g_list_last (path_bar->button_list);
395 gboolean reached_end = FALSE;
396 gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
398 if (path_bar->first_scrolled_button)
399 first_button = path_bar->first_scrolled_button;
401 first_button = path_bar->button_list;
404 /* To see how much space we have, and how many buttons we can display.
405 * We start at the first button, count forward until hit the new
406 * button, then count backwards.
408 /* Count down the path chain towards the end. */
409 width = BUTTON_DATA (first_button->data)->button->requisition.width;
410 list = first_button->prev;
411 while (list && !reached_end)
413 child = BUTTON_DATA (list->data)->button;
415 if (width + child->requisition.width +
416 path_bar->spacing + slider_space > allocation_width)
418 else if (list == path_bar->fake_root)
421 width += child->requisition.width + path_bar->spacing;
426 /* Finally, we walk up, seeing how many of the previous buttons we can
428 while (first_button->next && !reached_end)
430 child = BUTTON_DATA (first_button->next->data)->button;
432 if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
438 width += child->requisition.width + path_bar->spacing;
439 if (first_button == path_bar->fake_root)
441 first_button = first_button->next;
446 /* Now, we allocate space to the buttons */
447 child_allocation.y = allocation->y + border_width;
448 child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
450 if (direction == GTK_TEXT_DIR_RTL)
452 child_allocation.x = allocation->x + allocation->width - border_width;
453 if (need_sliders || path_bar->fake_root)
455 child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
456 up_slider_offset = allocation->width - border_width - path_bar->slider_width;
461 child_allocation.x = allocation->x + border_width;
462 if (need_sliders || path_bar->fake_root)
464 up_slider_offset = border_width;
465 child_allocation.x += (path_bar->spacing + path_bar->slider_width);
469 for (list = first_button; list; list = list->prev)
471 child = BUTTON_DATA (list->data)->button;
473 child_allocation.width = child->requisition.width;
474 if (direction == GTK_TEXT_DIR_RTL)
475 child_allocation.x -= child_allocation.width;
477 /* Check to see if we've don't have any more space to allocate buttons */
478 if (need_sliders && direction == GTK_TEXT_DIR_RTL)
480 if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
483 else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
485 if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
486 widget->allocation.x + border_width + allocation_width)
490 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
491 gtk_widget_size_allocate (child, &child_allocation);
493 if (direction == GTK_TEXT_DIR_RTL)
495 child_allocation.x -= path_bar->spacing;
496 down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
497 down_slider_offset = border_width;
501 down_slider_offset = child_allocation.x - widget->allocation.x;
502 down_slider_offset = allocation->width - border_width - path_bar->slider_width;
503 child_allocation.x += child_allocation.width + path_bar->spacing;
506 /* Now we go hide all the widgets that don't fit */
509 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
512 for (list = first_button->next; list; list = list->next)
514 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
517 if (need_sliders || path_bar->fake_root)
519 child_allocation.width = path_bar->slider_width;
520 child_allocation.x = up_slider_offset + allocation->x;
521 gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
523 gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
524 gtk_widget_show_all (path_bar->up_slider_button);
527 gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
531 child_allocation.width = path_bar->slider_width;
532 child_allocation.x = down_slider_offset + allocation->x;
533 gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
535 gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
536 gtk_widget_show_all (path_bar->down_slider_button);
537 gtk_path_bar_update_slider_buttons (path_bar);
540 gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
544 gtk_path_bar_style_set (GtkWidget *widget,
545 GtkStyle *previous_style)
547 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
548 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
550 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
554 gtk_path_bar_screen_changed (GtkWidget *widget,
555 GdkScreen *previous_screen)
557 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
558 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
560 /* We might nave a new settings, so we remove the old one */
562 remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
564 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
568 gtk_path_bar_add (GtkContainer *container,
571 gtk_widget_set_parent (widget, GTK_WIDGET (container));
575 gtk_path_bar_remove_1 (GtkContainer *container,
578 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
579 gtk_widget_unparent (widget);
581 gtk_widget_queue_resize (GTK_WIDGET (container));
585 gtk_path_bar_remove (GtkContainer *container,
588 GtkPathBar *path_bar;
591 path_bar = GTK_PATH_BAR (container);
593 if (widget == path_bar->up_slider_button)
595 gtk_path_bar_remove_1 (container, widget);
596 path_bar->up_slider_button = NULL;
600 if (widget == path_bar->down_slider_button)
602 gtk_path_bar_remove_1 (container, widget);
603 path_bar->down_slider_button = NULL;
607 children = path_bar->button_list;
610 if (widget == BUTTON_DATA (children->data)->button)
612 gtk_path_bar_remove_1 (container, widget);
613 path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
614 g_list_free (children);
618 children = children->next;
623 gtk_path_bar_forall (GtkContainer *container,
624 gboolean include_internals,
625 GtkCallback callback,
626 gpointer callback_data)
628 GtkPathBar *path_bar;
631 g_return_if_fail (callback != NULL);
632 path_bar = GTK_PATH_BAR (container);
634 children = path_bar->button_list;
638 child = BUTTON_DATA (children->data)->button;
639 children = children->next;
641 (* callback) (child, callback_data);
644 if (path_bar->up_slider_button)
645 (* callback) (path_bar->up_slider_button, callback_data);
647 if (path_bar->down_slider_button)
648 (* callback) (path_bar->down_slider_button, callback_data);
652 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
655 GList *down_button = NULL;
656 GList *up_button = NULL;
657 gint space_available;
660 GtkTextDirection direction;
662 if (path_bar->ignore_click)
664 path_bar->ignore_click = FALSE;
668 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
670 border_width = GTK_CONTAINER (path_bar)->border_width;
671 direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
673 /* We find the button at the 'down' end that we have to make
675 for (list = path_bar->button_list; list; list = list->next)
677 if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
684 /* Find the last visible button on the 'up' end
686 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
688 if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
695 space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
696 if (direction == GTK_TEXT_DIR_RTL)
697 space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
699 space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
700 (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
702 /* We have space_available extra space that's not being used. We
703 * need space_needed space to make the button fit. So we walk down
704 * from the end, removing buttons until we get all the space we
706 while (space_available < space_needed)
708 space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
709 up_button = up_button->prev;
710 path_bar->first_scrolled_button = up_button;
715 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
719 if (path_bar->ignore_click)
721 path_bar->ignore_click = FALSE;
725 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
727 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
729 if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
731 if (list->prev == path_bar->fake_root)
732 path_bar->fake_root = NULL;
733 path_bar->first_scrolled_button = list;
740 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
742 gboolean retval = FALSE;
744 GDK_THREADS_ENTER ();
748 if (path_bar->scrolling_up)
749 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
750 else if (path_bar->scrolling_down)
751 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
753 if (path_bar->need_timer)
755 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (path_bar));
758 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
760 path_bar->need_timer = FALSE;
762 path_bar->timer = g_timeout_add (timeout * SCROLL_DELAY_FACTOR,
763 (GSourceFunc)gtk_path_bar_scroll_timeout,
770 GDK_THREADS_LEAVE ();
776 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
780 g_source_remove (path_bar->timer);
782 path_bar->need_timer = FALSE;
787 gtk_path_bar_slider_button_press (GtkWidget *widget,
788 GdkEventButton *event,
789 GtkPathBar *path_bar)
791 if (event->type != GDK_BUTTON_PRESS || event->button != 1)
794 path_bar->ignore_click = FALSE;
796 if (widget == path_bar->up_slider_button)
798 path_bar->scrolling_down = FALSE;
799 path_bar->scrolling_up = TRUE;
800 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
802 else if (widget == path_bar->down_slider_button)
804 path_bar->scrolling_up = FALSE;
805 path_bar->scrolling_down = TRUE;
806 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
809 if (!path_bar->timer)
811 GtkSettings *settings = gtk_widget_get_settings (widget);
814 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
816 path_bar->need_timer = TRUE;
817 path_bar->timer = g_timeout_add (timeout,
818 (GSourceFunc)gtk_path_bar_scroll_timeout,
826 gtk_path_bar_slider_button_release (GtkWidget *widget,
827 GdkEventButton *event,
828 GtkPathBar *path_bar)
830 if (event->type != GDK_BUTTON_RELEASE)
833 path_bar->ignore_click = TRUE;
834 gtk_path_bar_stop_scrolling (path_bar);
840 gtk_path_bar_grab_notify (GtkWidget *widget,
841 gboolean was_grabbed)
844 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
848 gtk_path_bar_state_changed (GtkWidget *widget,
849 GtkStateType previous_state)
851 if (!GTK_WIDGET_IS_SENSITIVE (widget))
852 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
856 /* Changes the icons wherever it is needed */
858 reload_icons (GtkPathBar *path_bar)
862 if (path_bar->root_icon)
864 g_object_unref (path_bar->root_icon);
865 path_bar->root_icon = NULL;
867 if (path_bar->home_icon)
869 g_object_unref (path_bar->home_icon);
870 path_bar->home_icon = NULL;
872 if (path_bar->desktop_icon)
874 g_object_unref (path_bar->desktop_icon);
875 path_bar->desktop_icon = NULL;
878 for (list = path_bar->button_list; list; list = list->next)
880 ButtonData *button_data;
881 gboolean current_dir;
883 button_data = BUTTON_DATA (list->data);
884 if (button_data->type != NORMAL_BUTTON)
886 current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
887 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
894 change_icon_theme (GtkPathBar *path_bar)
896 GtkSettings *settings;
899 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
901 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
902 path_bar->icon_size = MAX (width, height);
904 path_bar->icon_size = FALLBACK_ICON_SIZE;
906 reload_icons (path_bar);
908 /* Callback used when a GtkSettings value changes */
910 settings_notify_cb (GObject *object,
912 GtkPathBar *path_bar)
916 name = g_param_spec_get_name (pspec);
918 if (! strcmp (name, "gtk-icon-theme-name") ||
919 ! strcmp (name, "gtk-icon-sizes"))
920 change_icon_theme (path_bar);
924 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
926 GtkSettings *settings;
928 if (path_bar->settings_signal_id)
931 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
932 path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
934 change_icon_theme (path_bar);
937 /* Public functions and their helpers */
939 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
941 while (path_bar->button_list != NULL)
943 gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
945 path_bar->first_scrolled_button = NULL;
946 path_bar->fake_root = NULL;
950 button_clicked_cb (GtkWidget *button,
953 ButtonData *button_data;
954 GtkPathBar *path_bar;
956 gboolean child_is_hidden;
957 GtkFilePath *child_path;
959 button_data = BUTTON_DATA (data);
960 if (button_data->ignore_changes)
963 path_bar = GTK_PATH_BAR (button->parent);
965 button_list = g_list_find (path_bar->button_list, button_data);
966 g_assert (button_list != NULL);
968 g_signal_handlers_block_by_func (button,
969 G_CALLBACK (button_clicked_cb), data);
970 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
971 g_signal_handlers_unblock_by_func (button,
972 G_CALLBACK (button_clicked_cb), data);
974 if (button_list->prev)
976 ButtonData *child_data;
978 child_data = BUTTON_DATA (button_list->prev->data);
979 child_path = child_data->path;
980 child_is_hidden = child_data->file_is_hidden;
985 child_is_hidden = FALSE;
988 g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0,
989 button_data->path, child_path, child_is_hidden);
992 struct SetButtonImageData
994 GtkPathBar *path_bar;
995 ButtonData *button_data;
999 set_button_image_get_info_cb (GtkFileSystemHandle *handle,
1000 const GtkFileInfo *info,
1001 const GError *error,
1004 gboolean cancelled = handle->cancelled;
1006 struct SetButtonImageData *data = user_data;
1008 if (handle != data->button_data->handle)
1011 data->button_data->handle = NULL;
1013 if (cancelled || error)
1016 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->path_bar),
1017 data->path_bar->icon_size, NULL);
1018 gtk_image_set_from_pixbuf (GTK_IMAGE (data->button_data->image), pixbuf);
1020 switch (data->button_data->type)
1023 if (data->path_bar->home_icon)
1024 g_object_unref (pixbuf);
1026 data->path_bar->home_icon = pixbuf;
1029 case DESKTOP_BUTTON:
1030 if (data->path_bar->desktop_icon)
1031 g_object_unref (pixbuf);
1033 data->path_bar->desktop_icon = pixbuf;
1042 g_object_unref (handle);
1046 set_button_image (GtkPathBar *path_bar,
1047 ButtonData *button_data)
1049 GtkFileSystemVolume *volume;
1050 struct SetButtonImageData *data;
1052 switch (button_data->type)
1056 if (path_bar->root_icon != NULL)
1058 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1062 volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
1066 path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
1068 GTK_WIDGET (path_bar),
1069 path_bar->icon_size,
1071 gtk_file_system_volume_free (path_bar->file_system, volume);
1073 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1077 if (path_bar->home_icon != NULL)
1079 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->home_icon);
1083 data = g_new0 (struct SetButtonImageData, 1);
1084 data->path_bar = path_bar;
1085 data->button_data = button_data;
1087 if (button_data->handle)
1088 gtk_file_system_cancel_operation (button_data->handle);
1090 button_data->handle =
1091 gtk_file_system_get_info (path_bar->file_system,
1092 path_bar->home_path,
1094 set_button_image_get_info_cb,
1098 case DESKTOP_BUTTON:
1099 if (path_bar->desktop_icon != NULL)
1101 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->desktop_icon);
1105 data = g_new0 (struct SetButtonImageData, 1);
1106 data->path_bar = path_bar;
1107 data->button_data = button_data;
1109 if (button_data->handle)
1110 gtk_file_system_cancel_operation (button_data->handle);
1112 button_data->handle =
1113 gtk_file_system_get_info (path_bar->file_system,
1114 path_bar->desktop_path,
1116 set_button_image_get_info_cb,
1125 button_data_free (ButtonData *button_data)
1127 if (button_data->handle)
1128 gtk_file_system_cancel_operation (button_data->handle);
1130 gtk_file_path_free (button_data->path);
1131 g_free (button_data->dir_name);
1132 g_free (button_data);
1136 get_dir_name (ButtonData *button_data)
1138 return button_data->dir_name;
1141 /* We always want to request the same size for the label, whether
1142 * or not the contents are bold
1145 label_size_request_cb (GtkWidget *widget,
1146 GtkRequisition *requisition,
1147 ButtonData *button_data)
1149 const gchar *dir_name = get_dir_name (button_data);
1150 PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1151 gint bold_width, bold_height;
1154 pango_layout_get_pixel_size (layout, &requisition->width, &requisition->height);
1156 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1157 pango_layout_set_markup (layout, markup, -1);
1160 pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1161 requisition->width = MAX (requisition->width, bold_width);
1162 requisition->height = MAX (requisition->height, bold_height);
1164 g_object_unref (layout);
1168 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1169 ButtonData *button_data,
1170 gboolean current_dir)
1172 const gchar *dir_name = get_dir_name (button_data);
1174 if (button_data->label != NULL)
1180 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1181 gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1186 gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1190 if (button_data->image != NULL)
1192 set_button_image (path_bar, button_data);
1195 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1197 button_data->ignore_changes = TRUE;
1198 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1199 button_data->ignore_changes = FALSE;
1204 find_button_type (GtkPathBar *path_bar,
1207 if (path_bar->root_path != NULL &&
1208 ! gtk_file_path_compare (path, path_bar->root_path))
1210 if (path_bar->home_path != NULL &&
1211 ! gtk_file_path_compare (path, path_bar->home_path))
1213 if (path_bar->desktop_path != NULL &&
1214 ! gtk_file_path_compare (path, path_bar->desktop_path))
1215 return DESKTOP_BUTTON;
1217 return NORMAL_BUTTON;
1221 button_drag_data_get_cb (GtkWidget *widget,
1222 GdkDragContext *context,
1223 GtkSelectionData *selection_data,
1228 ButtonData *button_data;
1229 GtkPathBar *path_bar;
1234 path_bar = GTK_PATH_BAR (widget->parent); /* the button's parent *is* the path bar */
1236 uri = gtk_file_system_path_to_uri (path_bar->file_system, button_data->path);
1237 uri_list = g_strconcat (uri, "\r\n", NULL);
1240 gtk_selection_data_set (selection_data,
1241 selection_data->target,
1249 make_directory_button (GtkPathBar *path_bar,
1250 const char *dir_name,
1252 gboolean current_dir,
1253 gboolean file_is_hidden)
1255 const GtkTargetEntry targets[] = {
1256 { "text/uri-list", 0, 0 }
1259 GtkWidget *child = NULL;
1260 GtkWidget *label_alignment = NULL;
1261 ButtonData *button_data;
1263 file_is_hidden = !! file_is_hidden;
1264 /* Is it a special button? */
1265 button_data = g_new0 (ButtonData, 1);
1267 button_data->type = find_button_type (path_bar, path);
1268 button_data->button = gtk_toggle_button_new ();
1269 gtk_button_set_focus_on_click (GTK_BUTTON (button_data->button), FALSE);
1271 switch (button_data->type)
1274 button_data->image = gtk_image_new ();
1275 child = button_data->image;
1276 button_data->label = NULL;
1279 case DESKTOP_BUTTON:
1280 button_data->image = gtk_image_new ();
1281 button_data->label = gtk_label_new (NULL);
1282 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1283 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1284 child = gtk_hbox_new (FALSE, 2);
1285 gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1286 gtk_box_pack_start (GTK_BOX (child), label_alignment, FALSE, FALSE, 0);
1290 button_data->label = gtk_label_new (NULL);
1291 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1292 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1293 child = label_alignment;
1294 button_data->image = NULL;
1297 /* label_alignment is created because we can't override size-request
1298 * on label itself and still have the contents of the label centered
1299 * properly in the label's requisition
1301 if (label_alignment)
1302 g_signal_connect (label_alignment, "size-request",
1303 G_CALLBACK (label_size_request_cb), button_data);
1305 button_data->dir_name = g_strdup (dir_name);
1306 button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
1307 button_data->file_is_hidden = file_is_hidden;
1309 gtk_container_add (GTK_CONTAINER (button_data->button), child);
1310 gtk_widget_show_all (button_data->button);
1312 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1314 g_signal_connect (button_data->button, "clicked",
1315 G_CALLBACK (button_clicked_cb),
1317 g_object_weak_ref (G_OBJECT (button_data->button),
1318 (GWeakNotify) button_data_free, button_data);
1320 gtk_drag_source_set (button_data->button,
1323 G_N_ELEMENTS (targets),
1325 g_signal_connect (button_data->button, "drag-data-get",
1326 G_CALLBACK (button_drag_data_get_cb), button_data);
1332 gtk_path_bar_check_parent_path (GtkPathBar *path_bar,
1333 const GtkFilePath *file_path,
1334 GtkFileSystem *file_system)
1337 GList *current_path = NULL;
1338 gboolean need_new_fake_root = FALSE;
1340 for (list = path_bar->button_list; list; list = list->next)
1342 ButtonData *button_data;
1344 button_data = list->data;
1345 if (! gtk_file_path_compare (file_path, button_data->path))
1347 current_path = list;
1350 if (list == path_bar->fake_root)
1351 need_new_fake_root = TRUE;
1356 if (need_new_fake_root)
1358 path_bar->fake_root = NULL;
1359 for (list = current_path; list; list = list->next)
1361 ButtonData *button_data;
1363 button_data = list->data;
1364 if (BUTTON_IS_FAKE_ROOT (button_data))
1366 path_bar->fake_root = list;
1372 for (list = path_bar->button_list; list; list = list->next)
1374 gtk_path_bar_update_button_appearance (path_bar,
1375 BUTTON_DATA (list->data),
1376 (list == current_path) ? TRUE : FALSE);
1379 if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1381 path_bar->first_scrolled_button = current_path;
1382 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1394 GtkFilePath *parent_path;
1395 GtkPathBar *path_bar;
1398 gboolean first_directory;
1402 gtk_path_bar_set_path_finish (struct SetPathInfo *info,
1409 gtk_path_bar_clear_buttons (info->path_bar);
1410 info->path_bar->button_list = g_list_reverse (info->new_buttons);
1411 info->path_bar->fake_root = info->fake_root;
1413 for (l = info->path_bar->button_list; l; l = l->next)
1415 GtkWidget *button = BUTTON_DATA (l->data)->button;
1416 gtk_container_add (GTK_CONTAINER (info->path_bar), button);
1423 for (l = info->new_buttons; l; l = l->next)
1425 ButtonData *button_data;
1427 button_data = BUTTON_DATA (l->data);
1428 gtk_widget_destroy (button_data->button);
1431 g_list_free (info->new_buttons);
1435 gtk_file_path_free (info->path);
1436 if (info->parent_path)
1437 gtk_file_path_free (info->parent_path);
1442 gtk_path_bar_get_info_callback (GtkFileSystemHandle *handle,
1443 const GtkFileInfo *file_info,
1444 const GError *error,
1447 gboolean cancelled = handle->cancelled;
1448 struct SetPathInfo *path_info = data;
1449 ButtonData *button_data;
1450 const gchar *display_name;
1454 if (handle != path_info->path_bar->set_path_handle)
1456 gtk_path_bar_set_path_finish (path_info, FALSE);
1457 g_object_unref (handle);
1461 g_object_unref (handle);
1462 path_info->path_bar->set_path_handle = NULL;
1464 if (cancelled || !file_info)
1466 gtk_path_bar_set_path_finish (path_info, FALSE);
1470 display_name = gtk_file_info_get_display_name (file_info);
1471 is_hidden = gtk_file_info_get_is_hidden (file_info);
1473 gtk_widget_push_composite_child ();
1474 button_data = make_directory_button (path_info->path_bar, display_name,
1476 path_info->first_directory, is_hidden);
1477 gtk_widget_pop_composite_child ();
1478 gtk_file_path_free (path_info->path);
1480 path_info->new_buttons = g_list_prepend (path_info->new_buttons, button_data);
1482 if (BUTTON_IS_FAKE_ROOT (button_data))
1483 path_info->fake_root = path_info->new_buttons;
1485 path_info->path = path_info->parent_path;
1486 path_info->first_directory = FALSE;
1488 if (!path_info->path)
1490 gtk_path_bar_set_path_finish (path_info, TRUE);
1494 valid = gtk_file_system_get_parent (path_info->path_bar->file_system,
1496 &path_info->parent_path,
1500 gtk_path_bar_set_path_finish (path_info, FALSE);
1504 path_info->path_bar->set_path_handle =
1505 gtk_file_system_get_info (handle->file_system,
1507 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1508 gtk_path_bar_get_info_callback,
1513 _gtk_path_bar_set_path (GtkPathBar *path_bar,
1514 const GtkFilePath *file_path,
1515 const gboolean keep_trail,
1518 struct SetPathInfo *info;
1521 g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
1522 g_return_val_if_fail (file_path != NULL, FALSE);
1526 /* Check whether the new path is already present in the pathbar as buttons.
1527 * This could be a parent directory or a previous selected subdirectory.
1530 gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
1533 info = g_new0 (struct SetPathInfo, 1);
1534 info->path = gtk_file_path_copy (file_path);
1535 info->path_bar = path_bar;
1536 info->first_directory = TRUE;
1538 result = gtk_file_system_get_parent (path_bar->file_system,
1539 info->path, &info->parent_path, error);
1542 gtk_file_path_free (info->path);
1547 if (path_bar->set_path_handle)
1548 gtk_file_system_cancel_operation (path_bar->set_path_handle);
1550 path_bar->set_path_handle =
1551 gtk_file_system_get_info (path_bar->file_system,
1553 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1554 gtk_path_bar_get_info_callback,
1560 /* FIXME: This should be a construct-only property */
1562 _gtk_path_bar_set_file_system (GtkPathBar *path_bar,
1563 GtkFileSystem *file_system)
1568 g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1570 g_assert (path_bar->file_system == NULL);
1572 path_bar->file_system = g_object_ref (file_system);
1574 home = g_get_home_dir ();
1577 path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1578 /* FIXME: Need file system backend specific way of getting the
1581 desktop = g_build_filename (home, "Desktop", NULL);
1582 path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1587 path_bar->home_path = NULL;
1588 path_bar->desktop_path = NULL;
1590 path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1595 * @path_bar: a #GtkPathBar
1597 * If the selected button in the pathbar is not the furthest button "up" (in the
1598 * root direction), act as if the user clicked on the next button up.
1601 _gtk_path_bar_up (GtkPathBar *path_bar)
1605 for (l = path_bar->button_list; l; l = l->next)
1607 GtkWidget *button = BUTTON_DATA (l->data)->button;
1608 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1612 GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1613 button_clicked_cb (next_button, l->next->data);
1621 * _gtk_path_bar_down:
1622 * @path_bar: a #GtkPathBar
1624 * If the selected button in the pathbar is not the furthest button "down" (in the
1625 * leaf direction), act as if the user clicked on the next button down.
1628 _gtk_path_bar_down (GtkPathBar *path_bar)
1632 for (l = path_bar->button_list; l; l = l->next)
1634 GtkWidget *button = BUTTON_DATA (l->data)->button;
1635 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1639 GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1640 button_clicked_cb (prev_button, l->prev->data);