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)
254 GtkPathBar *path_bar = GTK_PATH_BAR (object);
256 remove_settings_signal (path_bar, gtk_widget_get_screen (GTK_WIDGET (object)));
258 if (path_bar->set_path_handle)
259 gtk_file_system_cancel_operation (path_bar->set_path_handle);
260 path_bar->set_path_handle = NULL;
262 G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
267 * Ideally, our size is determined by another widget, and we are just filling
271 gtk_path_bar_size_request (GtkWidget *widget,
272 GtkRequisition *requisition)
274 ButtonData *button_data;
275 GtkPathBar *path_bar;
276 GtkRequisition child_requisition;
279 path_bar = GTK_PATH_BAR (widget);
281 requisition->width = 0;
282 requisition->height = 0;
284 for (list = path_bar->button_list; list; list = list->next)
286 button_data = BUTTON_DATA (list->data);
287 gtk_widget_size_request (button_data->button, &child_requisition);
288 requisition->width = MAX (child_requisition.width, requisition->width);
289 requisition->height = MAX (child_requisition.height, requisition->height);
292 /* Add space for slider, if we have more than one path */
293 /* Theoretically, the slider could be bigger than the other button. But we're
294 * not going to worry about that now.
296 path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
297 if (path_bar->button_list && path_bar->button_list->next != NULL)
298 requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
300 gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
301 gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
303 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
304 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
306 widget->requisition = *requisition;
310 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
312 if (path_bar->button_list)
316 button = BUTTON_DATA (path_bar->button_list->data)->button;
317 if (gtk_widget_get_child_visible (button))
319 gtk_path_bar_stop_scrolling (path_bar);
320 gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
323 gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
325 button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
326 if (gtk_widget_get_child_visible (button))
328 gtk_path_bar_stop_scrolling (path_bar);
329 gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
332 gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
337 gtk_path_bar_unmap (GtkWidget *widget)
339 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
341 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->unmap (widget);
344 /* This is a tad complicated
347 gtk_path_bar_size_allocate (GtkWidget *widget,
348 GtkAllocation *allocation)
351 GtkPathBar *path_bar = GTK_PATH_BAR (widget);
352 GtkTextDirection direction;
353 GtkAllocation child_allocation;
354 GList *list, *first_button;
356 gint allocation_width;
358 gboolean need_sliders = FALSE;
359 gint up_slider_offset = 0;
360 gint down_slider_offset = 0;
362 widget->allocation = *allocation;
364 /* No path is set; we don't have to allocate anything. */
365 if (path_bar->button_list == NULL)
368 direction = gtk_widget_get_direction (widget);
369 border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
370 allocation_width = allocation->width - 2 * border_width;
372 /* First, we check to see if we need the scrollbars. */
373 if (path_bar->fake_root)
374 width = path_bar->spacing + path_bar->slider_width;
378 for (list = path_bar->button_list; list; list = list->next)
380 child = BUTTON_DATA (list->data)->button;
382 width += child->requisition.width + path_bar->spacing;
383 if (list == path_bar->fake_root)
387 if (width <= allocation_width)
389 if (path_bar->fake_root)
390 first_button = path_bar->fake_root;
392 first_button = g_list_last (path_bar->button_list);
396 gboolean reached_end = FALSE;
397 gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
399 if (path_bar->first_scrolled_button)
400 first_button = path_bar->first_scrolled_button;
402 first_button = path_bar->button_list;
405 /* To see how much space we have, and how many buttons we can display.
406 * We start at the first button, count forward until hit the new
407 * button, then count backwards.
409 /* Count down the path chain towards the end. */
410 width = BUTTON_DATA (first_button->data)->button->requisition.width;
411 list = first_button->prev;
412 while (list && !reached_end)
414 child = BUTTON_DATA (list->data)->button;
416 if (width + child->requisition.width +
417 path_bar->spacing + slider_space > allocation_width)
419 else if (list == path_bar->fake_root)
422 width += child->requisition.width + path_bar->spacing;
427 /* Finally, we walk up, seeing how many of the previous buttons we can
429 while (first_button->next && !reached_end)
431 child = BUTTON_DATA (first_button->next->data)->button;
433 if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
439 width += child->requisition.width + path_bar->spacing;
440 if (first_button == path_bar->fake_root)
442 first_button = first_button->next;
447 /* Now, we allocate space to the buttons */
448 child_allocation.y = allocation->y + border_width;
449 child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
451 if (direction == GTK_TEXT_DIR_RTL)
453 child_allocation.x = allocation->x + allocation->width - border_width;
454 if (need_sliders || path_bar->fake_root)
456 child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
457 up_slider_offset = allocation->width - border_width - path_bar->slider_width;
462 child_allocation.x = allocation->x + border_width;
463 if (need_sliders || path_bar->fake_root)
465 up_slider_offset = border_width;
466 child_allocation.x += (path_bar->spacing + path_bar->slider_width);
470 for (list = first_button; list; list = list->prev)
472 child = BUTTON_DATA (list->data)->button;
474 child_allocation.width = child->requisition.width;
475 if (direction == GTK_TEXT_DIR_RTL)
476 child_allocation.x -= child_allocation.width;
478 /* Check to see if we've don't have any more space to allocate buttons */
479 if (need_sliders && direction == GTK_TEXT_DIR_RTL)
481 if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
484 else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
486 if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
487 widget->allocation.x + border_width + allocation_width)
491 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
492 gtk_widget_size_allocate (child, &child_allocation);
494 if (direction == GTK_TEXT_DIR_RTL)
496 child_allocation.x -= path_bar->spacing;
497 down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
498 down_slider_offset = border_width;
502 down_slider_offset = child_allocation.x - widget->allocation.x;
503 down_slider_offset = allocation->width - border_width - path_bar->slider_width;
504 child_allocation.x += child_allocation.width + path_bar->spacing;
507 /* Now we go hide all the widgets that don't fit */
510 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
513 for (list = first_button->next; list; list = list->next)
515 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
518 if (need_sliders || path_bar->fake_root)
520 child_allocation.width = path_bar->slider_width;
521 child_allocation.x = up_slider_offset + allocation->x;
522 gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
524 gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
525 gtk_widget_show_all (path_bar->up_slider_button);
528 gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
532 child_allocation.width = path_bar->slider_width;
533 child_allocation.x = down_slider_offset + allocation->x;
534 gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
536 gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
537 gtk_widget_show_all (path_bar->down_slider_button);
538 gtk_path_bar_update_slider_buttons (path_bar);
541 gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
545 gtk_path_bar_style_set (GtkWidget *widget,
546 GtkStyle *previous_style)
548 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
549 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
551 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
555 gtk_path_bar_screen_changed (GtkWidget *widget,
556 GdkScreen *previous_screen)
558 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
559 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
561 /* We might nave a new settings, so we remove the old one */
563 remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
565 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
569 gtk_path_bar_add (GtkContainer *container,
572 gtk_widget_set_parent (widget, GTK_WIDGET (container));
576 gtk_path_bar_remove_1 (GtkContainer *container,
579 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
580 gtk_widget_unparent (widget);
582 gtk_widget_queue_resize (GTK_WIDGET (container));
586 gtk_path_bar_remove (GtkContainer *container,
589 GtkPathBar *path_bar;
592 path_bar = GTK_PATH_BAR (container);
594 if (widget == path_bar->up_slider_button)
596 gtk_path_bar_remove_1 (container, widget);
597 path_bar->up_slider_button = NULL;
601 if (widget == path_bar->down_slider_button)
603 gtk_path_bar_remove_1 (container, widget);
604 path_bar->down_slider_button = NULL;
608 children = path_bar->button_list;
611 if (widget == BUTTON_DATA (children->data)->button)
613 gtk_path_bar_remove_1 (container, widget);
614 path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
615 g_list_free (children);
619 children = children->next;
624 gtk_path_bar_forall (GtkContainer *container,
625 gboolean include_internals,
626 GtkCallback callback,
627 gpointer callback_data)
629 GtkPathBar *path_bar;
632 g_return_if_fail (callback != NULL);
633 path_bar = GTK_PATH_BAR (container);
635 children = path_bar->button_list;
639 child = BUTTON_DATA (children->data)->button;
640 children = children->next;
642 (* callback) (child, callback_data);
645 if (path_bar->up_slider_button)
646 (* callback) (path_bar->up_slider_button, callback_data);
648 if (path_bar->down_slider_button)
649 (* callback) (path_bar->down_slider_button, callback_data);
653 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
656 GList *down_button = NULL;
657 GList *up_button = NULL;
658 gint space_available;
661 GtkTextDirection direction;
663 if (path_bar->ignore_click)
665 path_bar->ignore_click = FALSE;
669 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
671 border_width = GTK_CONTAINER (path_bar)->border_width;
672 direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
674 /* We find the button at the 'down' end that we have to make
676 for (list = path_bar->button_list; list; list = list->next)
678 if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
685 /* Find the last visible button on the 'up' end
687 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
689 if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
696 space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
697 if (direction == GTK_TEXT_DIR_RTL)
698 space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
700 space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
701 (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
703 /* We have space_available extra space that's not being used. We
704 * need space_needed space to make the button fit. So we walk down
705 * from the end, removing buttons until we get all the space we
707 while (space_available < space_needed)
709 space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
710 up_button = up_button->prev;
711 path_bar->first_scrolled_button = up_button;
716 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
720 if (path_bar->ignore_click)
722 path_bar->ignore_click = FALSE;
726 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
728 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
730 if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
732 if (list->prev == path_bar->fake_root)
733 path_bar->fake_root = NULL;
734 path_bar->first_scrolled_button = list;
741 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
743 gboolean retval = FALSE;
745 GDK_THREADS_ENTER ();
749 if (path_bar->scrolling_up)
750 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
751 else if (path_bar->scrolling_down)
752 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
754 if (path_bar->need_timer)
756 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (path_bar));
759 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
761 path_bar->need_timer = FALSE;
763 path_bar->timer = g_timeout_add (timeout * SCROLL_DELAY_FACTOR,
764 (GSourceFunc)gtk_path_bar_scroll_timeout,
771 GDK_THREADS_LEAVE ();
777 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
781 g_source_remove (path_bar->timer);
783 path_bar->need_timer = FALSE;
788 gtk_path_bar_slider_button_press (GtkWidget *widget,
789 GdkEventButton *event,
790 GtkPathBar *path_bar)
792 if (event->type != GDK_BUTTON_PRESS || event->button != 1)
795 path_bar->ignore_click = FALSE;
797 if (widget == path_bar->up_slider_button)
799 path_bar->scrolling_down = FALSE;
800 path_bar->scrolling_up = TRUE;
801 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
803 else if (widget == path_bar->down_slider_button)
805 path_bar->scrolling_up = FALSE;
806 path_bar->scrolling_down = TRUE;
807 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
810 if (!path_bar->timer)
812 GtkSettings *settings = gtk_widget_get_settings (widget);
815 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
817 path_bar->need_timer = TRUE;
818 path_bar->timer = g_timeout_add (timeout,
819 (GSourceFunc)gtk_path_bar_scroll_timeout,
827 gtk_path_bar_slider_button_release (GtkWidget *widget,
828 GdkEventButton *event,
829 GtkPathBar *path_bar)
831 if (event->type != GDK_BUTTON_RELEASE)
834 path_bar->ignore_click = TRUE;
835 gtk_path_bar_stop_scrolling (path_bar);
841 gtk_path_bar_grab_notify (GtkWidget *widget,
842 gboolean was_grabbed)
845 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
849 gtk_path_bar_state_changed (GtkWidget *widget,
850 GtkStateType previous_state)
852 if (!GTK_WIDGET_IS_SENSITIVE (widget))
853 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
857 /* Changes the icons wherever it is needed */
859 reload_icons (GtkPathBar *path_bar)
863 if (path_bar->root_icon)
865 g_object_unref (path_bar->root_icon);
866 path_bar->root_icon = NULL;
868 if (path_bar->home_icon)
870 g_object_unref (path_bar->home_icon);
871 path_bar->home_icon = NULL;
873 if (path_bar->desktop_icon)
875 g_object_unref (path_bar->desktop_icon);
876 path_bar->desktop_icon = NULL;
879 for (list = path_bar->button_list; list; list = list->next)
881 ButtonData *button_data;
882 gboolean current_dir;
884 button_data = BUTTON_DATA (list->data);
885 if (button_data->type != NORMAL_BUTTON)
887 current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
888 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
895 change_icon_theme (GtkPathBar *path_bar)
897 GtkSettings *settings;
900 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
902 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU, &width, &height))
903 path_bar->icon_size = MAX (width, height);
905 path_bar->icon_size = FALLBACK_ICON_SIZE;
907 reload_icons (path_bar);
909 /* Callback used when a GtkSettings value changes */
911 settings_notify_cb (GObject *object,
913 GtkPathBar *path_bar)
917 name = g_param_spec_get_name (pspec);
919 if (! strcmp (name, "gtk-icon-theme-name") ||
920 ! strcmp (name, "gtk-icon-sizes"))
921 change_icon_theme (path_bar);
925 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
927 GtkSettings *settings;
929 if (path_bar->settings_signal_id)
932 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
933 path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
935 change_icon_theme (path_bar);
938 /* Public functions and their helpers */
940 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
942 while (path_bar->button_list != NULL)
944 gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
946 path_bar->first_scrolled_button = NULL;
947 path_bar->fake_root = NULL;
951 button_clicked_cb (GtkWidget *button,
954 ButtonData *button_data;
955 GtkPathBar *path_bar;
957 gboolean child_is_hidden;
958 GtkFilePath *child_path;
960 button_data = BUTTON_DATA (data);
961 if (button_data->ignore_changes)
964 path_bar = GTK_PATH_BAR (button->parent);
966 button_list = g_list_find (path_bar->button_list, button_data);
967 g_assert (button_list != NULL);
969 g_signal_handlers_block_by_func (button,
970 G_CALLBACK (button_clicked_cb), data);
971 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
972 g_signal_handlers_unblock_by_func (button,
973 G_CALLBACK (button_clicked_cb), data);
975 if (button_list->prev)
977 ButtonData *child_data;
979 child_data = BUTTON_DATA (button_list->prev->data);
980 child_path = child_data->path;
981 child_is_hidden = child_data->file_is_hidden;
986 child_is_hidden = FALSE;
989 g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0,
990 button_data->path, child_path, child_is_hidden);
993 struct SetButtonImageData
995 GtkPathBar *path_bar;
996 ButtonData *button_data;
1000 set_button_image_get_info_cb (GtkFileSystemHandle *handle,
1001 const GtkFileInfo *info,
1002 const GError *error,
1005 gboolean cancelled = handle->cancelled;
1007 struct SetButtonImageData *data = user_data;
1009 if (handle != data->button_data->handle)
1012 data->button_data->handle = NULL;
1014 if (!data->button_data->button)
1016 g_free (data->button_data);
1020 if (cancelled || error)
1023 pixbuf = gtk_file_info_render_icon (info, GTK_WIDGET (data->path_bar),
1024 data->path_bar->icon_size, NULL);
1025 gtk_image_set_from_pixbuf (GTK_IMAGE (data->button_data->image), pixbuf);
1027 switch (data->button_data->type)
1030 if (data->path_bar->home_icon)
1031 g_object_unref (pixbuf);
1033 data->path_bar->home_icon = pixbuf;
1036 case DESKTOP_BUTTON:
1037 if (data->path_bar->desktop_icon)
1038 g_object_unref (pixbuf);
1040 data->path_bar->desktop_icon = pixbuf;
1049 g_object_unref (handle);
1053 set_button_image (GtkPathBar *path_bar,
1054 ButtonData *button_data)
1056 GtkFileSystemVolume *volume;
1057 struct SetButtonImageData *data;
1059 switch (button_data->type)
1063 if (path_bar->root_icon != NULL)
1065 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1069 volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
1073 path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
1075 GTK_WIDGET (path_bar),
1076 path_bar->icon_size,
1078 gtk_file_system_volume_free (path_bar->file_system, volume);
1080 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->root_icon);
1084 if (path_bar->home_icon != NULL)
1086 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->home_icon);
1090 data = g_new0 (struct SetButtonImageData, 1);
1091 data->path_bar = path_bar;
1092 data->button_data = button_data;
1094 if (button_data->handle)
1095 gtk_file_system_cancel_operation (button_data->handle);
1097 button_data->handle =
1098 gtk_file_system_get_info (path_bar->file_system,
1099 path_bar->home_path,
1101 set_button_image_get_info_cb,
1105 case DESKTOP_BUTTON:
1106 if (path_bar->desktop_icon != NULL)
1108 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), path_bar->desktop_icon);
1112 data = g_new0 (struct SetButtonImageData, 1);
1113 data->path_bar = path_bar;
1114 data->button_data = button_data;
1116 if (button_data->handle)
1117 gtk_file_system_cancel_operation (button_data->handle);
1119 button_data->handle =
1120 gtk_file_system_get_info (path_bar->file_system,
1121 path_bar->desktop_path,
1123 set_button_image_get_info_cb,
1132 button_data_free (ButtonData *button_data)
1134 if (button_data->path)
1135 gtk_file_path_free (button_data->path);
1136 button_data->path = NULL;
1138 if (button_data->dir_name)
1139 g_free (button_data->dir_name);
1140 button_data->dir_name = NULL;
1142 button_data->button = NULL;
1144 if (button_data->handle)
1145 gtk_file_system_cancel_operation (button_data->handle);
1147 g_free (button_data);
1151 get_dir_name (ButtonData *button_data)
1153 return button_data->dir_name;
1156 /* We always want to request the same size for the label, whether
1157 * or not the contents are bold
1160 label_size_request_cb (GtkWidget *widget,
1161 GtkRequisition *requisition,
1162 ButtonData *button_data)
1164 const gchar *dir_name = get_dir_name (button_data);
1165 PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1166 gint bold_width, bold_height;
1169 pango_layout_get_pixel_size (layout, &requisition->width, &requisition->height);
1171 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1172 pango_layout_set_markup (layout, markup, -1);
1175 pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1176 requisition->width = MAX (requisition->width, bold_width);
1177 requisition->height = MAX (requisition->height, bold_height);
1179 g_object_unref (layout);
1183 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1184 ButtonData *button_data,
1185 gboolean current_dir)
1187 const gchar *dir_name = get_dir_name (button_data);
1189 if (button_data->label != NULL)
1195 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1196 gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1201 gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1205 if (button_data->image != NULL)
1207 set_button_image (path_bar, button_data);
1210 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1212 button_data->ignore_changes = TRUE;
1213 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1214 button_data->ignore_changes = FALSE;
1219 find_button_type (GtkPathBar *path_bar,
1222 if (path_bar->root_path != NULL &&
1223 ! gtk_file_path_compare (path, path_bar->root_path))
1225 if (path_bar->home_path != NULL &&
1226 ! gtk_file_path_compare (path, path_bar->home_path))
1228 if (path_bar->desktop_path != NULL &&
1229 ! gtk_file_path_compare (path, path_bar->desktop_path))
1230 return DESKTOP_BUTTON;
1232 return NORMAL_BUTTON;
1236 button_drag_data_get_cb (GtkWidget *widget,
1237 GdkDragContext *context,
1238 GtkSelectionData *selection_data,
1243 ButtonData *button_data;
1244 GtkPathBar *path_bar;
1249 path_bar = GTK_PATH_BAR (widget->parent); /* the button's parent *is* the path bar */
1251 uri = gtk_file_system_path_to_uri (path_bar->file_system, button_data->path);
1252 uri_list = g_strconcat (uri, "\r\n", NULL);
1255 gtk_selection_data_set (selection_data,
1256 selection_data->target,
1264 make_directory_button (GtkPathBar *path_bar,
1265 const char *dir_name,
1267 gboolean current_dir,
1268 gboolean file_is_hidden)
1270 const GtkTargetEntry targets[] = {
1271 { "text/uri-list", 0, 0 }
1274 GtkWidget *child = NULL;
1275 GtkWidget *label_alignment = NULL;
1276 ButtonData *button_data;
1278 file_is_hidden = !! file_is_hidden;
1279 /* Is it a special button? */
1280 button_data = g_new0 (ButtonData, 1);
1282 button_data->type = find_button_type (path_bar, path);
1283 button_data->button = gtk_toggle_button_new ();
1284 gtk_button_set_focus_on_click (GTK_BUTTON (button_data->button), FALSE);
1286 switch (button_data->type)
1289 button_data->image = gtk_image_new ();
1290 child = button_data->image;
1291 button_data->label = NULL;
1294 case DESKTOP_BUTTON:
1295 button_data->image = gtk_image_new ();
1296 button_data->label = gtk_label_new (NULL);
1297 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1298 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1299 child = gtk_hbox_new (FALSE, 2);
1300 gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1301 gtk_box_pack_start (GTK_BOX (child), label_alignment, FALSE, FALSE, 0);
1305 button_data->label = gtk_label_new (NULL);
1306 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1307 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1308 child = label_alignment;
1309 button_data->image = NULL;
1312 /* label_alignment is created because we can't override size-request
1313 * on label itself and still have the contents of the label centered
1314 * properly in the label's requisition
1316 if (label_alignment)
1317 g_signal_connect (label_alignment, "size-request",
1318 G_CALLBACK (label_size_request_cb), button_data);
1320 button_data->dir_name = g_strdup (dir_name);
1321 button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
1322 button_data->file_is_hidden = file_is_hidden;
1324 gtk_container_add (GTK_CONTAINER (button_data->button), child);
1325 gtk_widget_show_all (button_data->button);
1327 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1329 g_signal_connect (button_data->button, "clicked",
1330 G_CALLBACK (button_clicked_cb),
1332 g_object_weak_ref (G_OBJECT (button_data->button),
1333 (GWeakNotify) button_data_free, button_data);
1335 gtk_drag_source_set (button_data->button,
1338 G_N_ELEMENTS (targets),
1340 g_signal_connect (button_data->button, "drag_data_get",
1341 G_CALLBACK (button_drag_data_get_cb), button_data);
1347 gtk_path_bar_check_parent_path (GtkPathBar *path_bar,
1348 const GtkFilePath *file_path,
1349 GtkFileSystem *file_system)
1352 GList *current_path = NULL;
1353 gboolean need_new_fake_root = FALSE;
1355 for (list = path_bar->button_list; list; list = list->next)
1357 ButtonData *button_data;
1359 button_data = list->data;
1360 if (! gtk_file_path_compare (file_path, button_data->path))
1362 current_path = list;
1365 if (list == path_bar->fake_root)
1366 need_new_fake_root = TRUE;
1371 if (need_new_fake_root)
1373 path_bar->fake_root = NULL;
1374 for (list = current_path; list; list = list->next)
1376 ButtonData *button_data;
1378 button_data = list->data;
1379 if (BUTTON_IS_FAKE_ROOT (button_data))
1381 path_bar->fake_root = list;
1387 for (list = path_bar->button_list; list; list = list->next)
1389 gtk_path_bar_update_button_appearance (path_bar,
1390 BUTTON_DATA (list->data),
1391 (list == current_path) ? TRUE : FALSE);
1394 if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1396 path_bar->first_scrolled_button = current_path;
1397 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1409 GtkFilePath *parent_path;
1410 GtkPathBar *path_bar;
1413 gboolean first_directory;
1417 gtk_path_bar_set_path_finish (struct SetPathInfo *info,
1424 gtk_path_bar_clear_buttons (info->path_bar);
1425 info->path_bar->button_list = g_list_reverse (info->new_buttons);
1426 info->path_bar->fake_root = info->fake_root;
1428 for (l = info->path_bar->button_list; l; l = l->next)
1430 GtkWidget *button = BUTTON_DATA (l->data)->button;
1431 gtk_container_add (GTK_CONTAINER (info->path_bar), button);
1438 for (l = info->new_buttons; l; l = l->next)
1440 ButtonData *button_data;
1442 button_data = BUTTON_DATA (l->data);
1443 gtk_widget_destroy (button_data->button);
1446 g_list_free (info->new_buttons);
1450 gtk_file_path_free (info->path);
1451 if (info->parent_path)
1452 gtk_file_path_free (info->parent_path);
1457 gtk_path_bar_get_info_callback (GtkFileSystemHandle *handle,
1458 const GtkFileInfo *file_info,
1459 const GError *error,
1462 gboolean cancelled = handle->cancelled;
1463 struct SetPathInfo *path_info = data;
1464 ButtonData *button_data;
1465 const gchar *display_name;
1469 if (handle != path_info->path_bar->set_path_handle)
1471 gtk_path_bar_set_path_finish (path_info, FALSE);
1472 g_object_unref (handle);
1476 g_object_unref (handle);
1477 path_info->path_bar->set_path_handle = NULL;
1479 if (cancelled || !file_info)
1481 gtk_path_bar_set_path_finish (path_info, FALSE);
1485 display_name = gtk_file_info_get_display_name (file_info);
1486 is_hidden = gtk_file_info_get_is_hidden (file_info);
1488 gtk_widget_push_composite_child ();
1489 button_data = make_directory_button (path_info->path_bar, display_name,
1491 path_info->first_directory, is_hidden);
1492 gtk_widget_pop_composite_child ();
1493 gtk_file_path_free (path_info->path);
1495 path_info->new_buttons = g_list_prepend (path_info->new_buttons, button_data);
1497 if (BUTTON_IS_FAKE_ROOT (button_data))
1498 path_info->fake_root = path_info->new_buttons;
1500 path_info->path = path_info->parent_path;
1501 path_info->first_directory = FALSE;
1503 if (!path_info->path)
1505 gtk_path_bar_set_path_finish (path_info, TRUE);
1509 valid = gtk_file_system_get_parent (path_info->path_bar->file_system,
1511 &path_info->parent_path,
1515 gtk_path_bar_set_path_finish (path_info, FALSE);
1519 path_info->path_bar->set_path_handle =
1520 gtk_file_system_get_info (handle->file_system,
1522 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1523 gtk_path_bar_get_info_callback,
1528 _gtk_path_bar_set_path (GtkPathBar *path_bar,
1529 const GtkFilePath *file_path,
1530 const gboolean keep_trail,
1533 struct SetPathInfo *info;
1536 g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
1537 g_return_val_if_fail (file_path != NULL, FALSE);
1541 /* Check whether the new path is already present in the pathbar as buttons.
1542 * This could be a parent directory or a previous selected subdirectory.
1545 gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
1548 info = g_new0 (struct SetPathInfo, 1);
1549 info->path = gtk_file_path_copy (file_path);
1550 info->path_bar = path_bar;
1551 info->first_directory = TRUE;
1553 result = gtk_file_system_get_parent (path_bar->file_system,
1554 info->path, &info->parent_path, error);
1557 gtk_file_path_free (info->path);
1562 if (path_bar->set_path_handle)
1563 gtk_file_system_cancel_operation (path_bar->set_path_handle);
1565 path_bar->set_path_handle =
1566 gtk_file_system_get_info (path_bar->file_system,
1568 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1569 gtk_path_bar_get_info_callback,
1575 /* FIXME: This should be a construct-only property */
1577 _gtk_path_bar_set_file_system (GtkPathBar *path_bar,
1578 GtkFileSystem *file_system)
1583 g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1585 g_assert (path_bar->file_system == NULL);
1587 path_bar->file_system = g_object_ref (file_system);
1589 home = g_get_home_dir ();
1592 path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1593 /* FIXME: Need file system backend specific way of getting the
1596 desktop = g_build_filename (home, "Desktop", NULL);
1597 path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1602 path_bar->home_path = NULL;
1603 path_bar->desktop_path = NULL;
1605 path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1610 * @path_bar: a #GtkPathBar
1612 * If the selected button in the pathbar is not the furthest button "up" (in the
1613 * root direction), act as if the user clicked on the next button up.
1616 _gtk_path_bar_up (GtkPathBar *path_bar)
1620 for (l = path_bar->button_list; l; l = l->next)
1622 GtkWidget *button = BUTTON_DATA (l->data)->button;
1623 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1627 GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1628 button_clicked_cb (next_button, l->next->data);
1636 * _gtk_path_bar_down:
1637 * @path_bar: a #GtkPathBar
1639 * If the selected button in the pathbar is not the furthest button "down" (in the
1640 * leaf direction), act as if the user clicked on the next button down.
1643 _gtk_path_bar_down (GtkPathBar *path_bar)
1647 for (l = path_bar->button_list; l; l = l->next)
1649 GtkWidget *button = BUTTON_DATA (l->data)->button;
1650 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1654 GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1655 button_clicked_cb (prev_button, l->prev->data);
1662 #define __GTK_PATH_BAR_C__
1663 #include "gtkaliasdef.c"