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.
23 #include "gtkpathbar.h"
24 #include "gtktogglebutton.h"
25 #include "gtkalignment.h"
30 #include "gtkicontheme.h"
31 #include "gtkiconfactory.h"
35 #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 20
59 typedef struct _ButtonData ButtonData;
69 guint ignore_changes : 1;
70 guint file_is_hidden : 1;
73 G_DEFINE_TYPE (GtkPathBar,
77 static void gtk_path_bar_finalize (GObject *object);
78 static void gtk_path_bar_dispose (GObject *object);
79 static void gtk_path_bar_size_request (GtkWidget *widget,
80 GtkRequisition *requisition);
81 static void gtk_path_bar_size_allocate (GtkWidget *widget,
82 GtkAllocation *allocation);
83 static void gtk_path_bar_add (GtkContainer *container,
85 static void gtk_path_bar_remove (GtkContainer *container,
87 static void gtk_path_bar_forall (GtkContainer *container,
88 gboolean include_internals,
90 gpointer callback_data);
91 static void gtk_path_bar_scroll_up (GtkWidget *button,
92 GtkPathBar *path_bar);
93 static void gtk_path_bar_scroll_down (GtkWidget *button,
94 GtkPathBar *path_bar);
95 static gboolean gtk_path_bar_slider_button_press (GtkWidget *widget,
96 GdkEventButton *event,
97 GtkPathBar *path_bar);
98 static gboolean gtk_path_bar_slider_button_release(GtkWidget *widget,
99 GdkEventButton *event,
100 GtkPathBar *path_bar);
101 static void gtk_path_bar_grab_notify (GtkWidget *widget,
102 gboolean was_grabbed);
103 static void gtk_path_bar_state_changed (GtkWidget *widget,
104 GtkStateType previous_state);
105 static void gtk_path_bar_style_set (GtkWidget *widget,
106 GtkStyle *previous_style);
107 static void gtk_path_bar_screen_changed (GtkWidget *widget,
108 GdkScreen *previous_screen);
109 static void gtk_path_bar_check_icon_theme (GtkPathBar *path_bar);
110 static void gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
111 ButtonData *button_data,
112 gboolean current_dir);
115 get_slider_button (GtkPathBar *path_bar,
116 GtkArrowType arrow_type)
120 gtk_widget_push_composite_child ();
122 button = gtk_button_new ();
123 gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
124 gtk_container_add (GTK_CONTAINER (path_bar), button);
125 gtk_widget_show_all (button);
127 gtk_widget_pop_composite_child ();
133 gtk_path_bar_init (GtkPathBar *path_bar)
135 GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
136 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
138 path_bar->spacing = 3;
139 path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
140 path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
141 path_bar->icon_size = FALLBACK_ICON_SIZE;
143 g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
144 g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
146 g_signal_connect (path_bar->up_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
147 g_signal_connect (path_bar->up_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
148 g_signal_connect (path_bar->down_slider_button, "button_press_event", G_CALLBACK (gtk_path_bar_slider_button_press), path_bar);
149 g_signal_connect (path_bar->down_slider_button, "button_release_event", G_CALLBACK (gtk_path_bar_slider_button_release), path_bar);
153 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
155 GObjectClass *gobject_class;
156 GtkObjectClass *object_class;
157 GtkWidgetClass *widget_class;
158 GtkContainerClass *container_class;
160 gobject_class = (GObjectClass *) path_bar_class;
161 object_class = (GtkObjectClass *) path_bar_class;
162 widget_class = (GtkWidgetClass *) path_bar_class;
163 container_class = (GtkContainerClass *) path_bar_class;
165 gobject_class->finalize = gtk_path_bar_finalize;
166 gobject_class->dispose = gtk_path_bar_dispose;
168 widget_class->size_request = gtk_path_bar_size_request;
169 widget_class->size_allocate = gtk_path_bar_size_allocate;
170 widget_class->style_set = gtk_path_bar_style_set;
171 widget_class->screen_changed = gtk_path_bar_screen_changed;
172 widget_class->grab_notify = gtk_path_bar_grab_notify;
173 widget_class->state_changed = gtk_path_bar_state_changed;
175 container_class->add = gtk_path_bar_add;
176 container_class->forall = gtk_path_bar_forall;
177 container_class->remove = gtk_path_bar_remove;
179 /* container_class->child_type = gtk_path_bar_child_type;*/
181 path_bar_signals [PATH_CLICKED] =
182 g_signal_new ("path-clicked",
183 G_OBJECT_CLASS_TYPE (object_class),
185 G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
187 _gtk_marshal_VOID__POINTER_BOOLEAN,
195 gtk_path_bar_finalize (GObject *object)
197 GtkPathBar *path_bar;
199 path_bar = GTK_PATH_BAR (object);
200 g_list_free (path_bar->button_list);
201 if (path_bar->root_path)
202 gtk_file_path_free (path_bar->root_path);
203 if (path_bar->home_path)
204 gtk_file_path_free (path_bar->home_path);
205 if (path_bar->desktop_path)
206 gtk_file_path_free (path_bar->desktop_path);
208 if (path_bar->root_icon)
209 g_object_unref (path_bar->root_icon);
210 if (path_bar->home_icon)
211 g_object_unref (path_bar->home_icon);
212 if (path_bar->desktop_icon)
213 g_object_unref (path_bar->desktop_icon);
215 if (path_bar->file_system)
216 g_object_unref (path_bar->file_system);
218 G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
221 /* Removes the settings signal handler. It's safe to call multiple times */
223 remove_settings_signal (GtkPathBar *path_bar,
226 if (path_bar->settings_signal_id)
228 GtkSettings *settings;
230 settings = gtk_settings_get_for_screen (screen);
231 g_signal_handler_disconnect (settings,
232 path_bar->settings_signal_id);
233 path_bar->settings_signal_id = 0;
238 gtk_path_bar_dispose (GObject *object)
240 remove_settings_signal (GTK_PATH_BAR (object),
241 gtk_widget_get_screen (GTK_WIDGET (object)));
243 G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
248 * Ideally, our size is determined by another widget, and we are just filling
252 gtk_path_bar_size_request (GtkWidget *widget,
253 GtkRequisition *requisition)
255 ButtonData *button_data;
256 GtkPathBar *path_bar;
257 GtkRequisition child_requisition;
260 path_bar = GTK_PATH_BAR (widget);
262 requisition->width = 0;
263 requisition->height = 0;
265 for (list = path_bar->button_list; list; list = list->next)
267 button_data = BUTTON_DATA (list->data);
268 gtk_widget_size_request (button_data->button, &child_requisition);
269 requisition->width = MAX (child_requisition.width, requisition->width);
270 requisition->height = MAX (child_requisition.height, requisition->height);
273 /* Add space for slider, if we have more than one path */
274 /* Theoretically, the slider could be bigger than the other button. But we're
275 * not going to worry about that now.
277 path_bar->slider_width = MIN(requisition->height * 2 / 3 + 5, requisition->height);
278 if (path_bar->button_list && path_bar->button_list->next != NULL)
279 requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
281 gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
282 gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
284 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
285 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
287 widget->requisition = *requisition;
291 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
293 if (path_bar->button_list)
297 button = BUTTON_DATA (path_bar->button_list->data)->button;
298 if (gtk_widget_get_child_visible (button))
299 gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
301 gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
303 button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
304 if (gtk_widget_get_child_visible (button))
305 gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
307 gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
311 /* This is a tad complicated
314 gtk_path_bar_size_allocate (GtkWidget *widget,
315 GtkAllocation *allocation)
318 GtkPathBar *path_bar = GTK_PATH_BAR (widget);
319 GtkTextDirection direction;
320 GtkAllocation child_allocation;
321 GList *list, *first_button;
323 gint allocation_width;
325 gboolean need_sliders = FALSE;
326 gint up_slider_offset = 0;
327 gint down_slider_offset = 0;
329 widget->allocation = *allocation;
331 /* No path is set; we don't have to allocate anything. */
332 if (path_bar->button_list == NULL)
335 direction = gtk_widget_get_direction (widget);
336 border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
337 allocation_width = allocation->width - 2 * border_width;
339 /* First, we check to see if we need the scrollbars. */
340 width = BUTTON_DATA (path_bar->button_list->data)->button->requisition.width;
341 for (list = path_bar->button_list->next; list; list = list->next)
343 child = BUTTON_DATA (list->data)->button;
345 width += child->requisition.width + path_bar->spacing;
348 if (width <= allocation_width)
350 first_button = g_list_last (path_bar->button_list);
354 gboolean reached_end = FALSE;
355 gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
357 if (path_bar->first_scrolled_button)
358 first_button = path_bar->first_scrolled_button;
360 first_button = path_bar->button_list;
363 /* To see how much space we have, and how many buttons we can display.
364 * We start at the first button, count forward until hit the new
365 * button, then count backwards.
367 /* Count down the path chain towards the end. */
368 width = BUTTON_DATA (first_button->data)->button->requisition.width;
369 list = first_button->prev;
370 while (list && !reached_end)
372 child = BUTTON_DATA (list->data)->button;
374 if (width + child->requisition.width +
375 path_bar->spacing + slider_space > allocation_width)
378 width += child->requisition.width + path_bar->spacing;
383 /* Finally, we walk up, seeing how many of the previous buttons we can
385 while (first_button->next && ! reached_end)
387 child = BUTTON_DATA (first_button->next->data)->button;
389 if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
395 width += child->requisition.width + path_bar->spacing;
396 first_button = first_button->next;
401 /* Now, we allocate space to the buttons */
402 child_allocation.y = allocation->y + border_width;
403 child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
405 if (direction == GTK_TEXT_DIR_RTL)
407 child_allocation.x = allocation->x + allocation->width - border_width;
410 child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
411 up_slider_offset = allocation->width - border_width - path_bar->slider_width;
416 child_allocation.x = allocation->x + border_width;
419 up_slider_offset = border_width;
420 child_allocation.x += (path_bar->spacing + path_bar->slider_width);
424 for (list = first_button; list; list = list->prev)
426 child = BUTTON_DATA (list->data)->button;
428 child_allocation.width = child->requisition.width;
429 if (direction == GTK_TEXT_DIR_RTL)
430 child_allocation.x -= child_allocation.width;
432 /* Check to see if we've don't have any more space to allocate buttons */
433 if (need_sliders && direction == GTK_TEXT_DIR_RTL)
435 if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
438 else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
440 if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
441 widget->allocation.x + border_width + allocation_width)
445 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
446 gtk_widget_size_allocate (child, &child_allocation);
448 if (direction == GTK_TEXT_DIR_RTL)
450 child_allocation.x -= path_bar->spacing;
451 down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
452 down_slider_offset = border_width;
456 down_slider_offset = child_allocation.x - widget->allocation.x;
457 down_slider_offset = allocation->width - border_width - path_bar->slider_width;
458 child_allocation.x += child_allocation.width + path_bar->spacing;
461 /* Now we go hide all the widgets that don't fit */
464 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
467 for (list = first_button->next; list; list = list->next)
469 gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
474 child_allocation.width = path_bar->slider_width;
476 child_allocation.x = up_slider_offset + allocation->x;
477 gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
479 child_allocation.x = down_slider_offset + allocation->x;
480 gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
482 gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
483 gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
484 gtk_widget_show_all (path_bar->up_slider_button);
485 gtk_widget_show_all (path_bar->down_slider_button);
486 gtk_path_bar_update_slider_buttons (path_bar);
490 gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
491 gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
496 gtk_path_bar_style_set (GtkWidget *widget,
497 GtkStyle *previous_style)
499 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
500 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
502 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
506 gtk_path_bar_screen_changed (GtkWidget *widget,
507 GdkScreen *previous_screen)
509 if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
510 GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
512 /* We might nave a new settings, so we remove the old one */
514 remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
516 gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
520 gtk_path_bar_add (GtkContainer *container,
523 gtk_widget_set_parent (widget, GTK_WIDGET (container));
527 gtk_path_bar_remove_1 (GtkContainer *container,
530 gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
531 gtk_widget_unparent (widget);
533 gtk_widget_queue_resize (GTK_WIDGET (container));
537 gtk_path_bar_remove (GtkContainer *container,
540 GtkPathBar *path_bar;
543 path_bar = GTK_PATH_BAR (container);
545 if (widget == path_bar->up_slider_button)
547 gtk_path_bar_remove_1 (container, widget);
548 path_bar->up_slider_button = NULL;
552 if (widget == path_bar->down_slider_button)
554 gtk_path_bar_remove_1 (container, widget);
555 path_bar->down_slider_button = NULL;
559 children = path_bar->button_list;
562 if (widget == BUTTON_DATA (children->data)->button)
564 gtk_path_bar_remove_1 (container, widget);
565 path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
566 g_list_free (children);
570 children = children->next;
575 gtk_path_bar_forall (GtkContainer *container,
576 gboolean include_internals,
577 GtkCallback callback,
578 gpointer callback_data)
580 GtkPathBar *path_bar;
583 g_return_if_fail (callback != NULL);
584 path_bar = GTK_PATH_BAR (container);
586 children = path_bar->button_list;
590 child = BUTTON_DATA (children->data)->button;
591 children = children->next;
593 (* callback) (child, callback_data);
596 if (path_bar->up_slider_button)
597 (* callback) (path_bar->up_slider_button, callback_data);
599 if (path_bar->down_slider_button)
600 (* callback) (path_bar->down_slider_button, callback_data);
604 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
607 GList *down_button = NULL;
608 GList *up_button = NULL;
609 gint space_available;
612 GtkTextDirection direction;
614 if (path_bar->ignore_click)
616 path_bar->ignore_click = FALSE;
620 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
622 border_width = GTK_CONTAINER (path_bar)->border_width;
623 direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
625 /* We find the button at the 'down' end that we have to make
627 for (list = path_bar->button_list; list; list = list->next)
629 if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
636 /* Find the last visible button on the 'up' end
638 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
640 if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
647 space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
648 if (direction == GTK_TEXT_DIR_RTL)
649 space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
651 space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
652 (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
654 /* We have space_available extra space that's not being used. We
655 * need space_needed space to make the button fit. So we walk down
656 * from the end, removing buttons until we get all the space we
658 while (space_available < space_needed)
660 space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
661 up_button = up_button->prev;
662 path_bar->first_scrolled_button = up_button;
667 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
671 if (path_bar->ignore_click)
673 path_bar->ignore_click = FALSE;
677 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
679 for (list = g_list_last (path_bar->button_list); list; list = list->prev)
681 if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
683 path_bar->first_scrolled_button = list;
690 gtk_path_bar_scroll_timeout (GtkPathBar *path_bar)
692 gboolean retval = FALSE;
694 GDK_THREADS_ENTER ();
698 if (GTK_WIDGET_HAS_FOCUS (path_bar->up_slider_button))
699 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
700 else if (GTK_WIDGET_HAS_FOCUS (path_bar->down_slider_button))
701 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
703 if (path_bar->need_timer)
705 path_bar->need_timer = FALSE;
707 path_bar->timer = g_timeout_add (SCROLL_TIMEOUT,
708 (GSourceFunc)gtk_path_bar_scroll_timeout,
717 GDK_THREADS_LEAVE ();
723 gtk_path_bar_stop_scrolling (GtkPathBar *path_bar)
727 g_source_remove (path_bar->timer);
729 path_bar->need_timer = FALSE;
734 gtk_path_bar_slider_button_press (GtkWidget *widget,
735 GdkEventButton *event,
736 GtkPathBar *path_bar)
738 if (!GTK_WIDGET_HAS_FOCUS (widget))
739 gtk_widget_grab_focus (widget);
741 if (event->type != GDK_BUTTON_PRESS || event->button != 1)
744 path_bar->ignore_click = FALSE;
746 if (widget == path_bar->up_slider_button)
747 gtk_path_bar_scroll_up (path_bar->up_slider_button, path_bar);
748 else if (widget == path_bar->down_slider_button)
749 gtk_path_bar_scroll_down (path_bar->down_slider_button, path_bar);
751 if (!path_bar->timer)
753 path_bar->need_timer = TRUE;
754 path_bar->timer = g_timeout_add (INITIAL_SCROLL_TIMEOUT,
755 (GSourceFunc)gtk_path_bar_scroll_timeout,
763 gtk_path_bar_slider_button_release (GtkWidget *widget,
764 GdkEventButton *event,
765 GtkPathBar *path_bar)
767 if (event->type != GDK_BUTTON_RELEASE)
770 path_bar->ignore_click = TRUE;
771 gtk_path_bar_stop_scrolling (path_bar);
777 gtk_path_bar_grab_notify (GtkWidget *widget,
778 gboolean was_grabbed)
781 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
785 gtk_path_bar_state_changed (GtkWidget *widget,
786 GtkStateType previous_state)
788 if (!GTK_WIDGET_IS_SENSITIVE (widget))
789 gtk_path_bar_stop_scrolling (GTK_PATH_BAR (widget));
793 /* Changes the icons wherever it is needed */
795 reload_icons (GtkPathBar *path_bar)
799 if (path_bar->root_icon)
801 g_object_unref (path_bar->root_icon);
802 path_bar->root_icon = NULL;
804 if (path_bar->home_icon)
806 g_object_unref (path_bar->home_icon);
807 path_bar->home_icon = NULL;
809 if (path_bar->desktop_icon)
811 g_object_unref (path_bar->desktop_icon);
812 path_bar->desktop_icon = NULL;
815 for (list = path_bar->button_list; list; list = list->next)
817 ButtonData *button_data;
818 gboolean current_dir;
820 button_data = BUTTON_DATA (list->data);
821 if (button_data->type != NORMAL_BUTTON)
823 current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
824 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
831 change_icon_theme (GtkPathBar *path_bar)
833 GtkSettings *settings;
836 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
838 if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_BUTTON, &width, &height))
839 path_bar->icon_size = MAX (width, height);
841 path_bar->icon_size = FALLBACK_ICON_SIZE;
843 reload_icons (path_bar);
845 /* Callback used when a GtkSettings value changes */
847 settings_notify_cb (GObject *object,
849 GtkPathBar *path_bar)
853 name = g_param_spec_get_name (pspec);
855 if (! strcmp (name, "gtk-icon-theme-name") ||
856 ! strcmp (name, "gtk-icon-sizes"))
857 change_icon_theme (path_bar);
861 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
863 GtkSettings *settings;
865 if (path_bar->settings_signal_id)
868 settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
869 path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
871 change_icon_theme (path_bar);
874 /* Public functions and their helpers */
876 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
878 while (path_bar->button_list != NULL)
880 gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
882 path_bar->first_scrolled_button = NULL;
886 button_clicked_cb (GtkWidget *button,
889 ButtonData *button_data;
890 GtkPathBar *path_bar;
892 gboolean child_is_hidden;
894 button_data = BUTTON_DATA (data);
895 if (button_data->ignore_changes)
898 path_bar = GTK_PATH_BAR (button->parent);
900 button_list = g_list_find (path_bar->button_list, button_data);
901 g_assert (button_list != NULL);
903 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
905 if (button_list->prev)
907 ButtonData *child_data;
909 child_data = BUTTON_DATA (button_list->prev->data);
910 child_is_hidden = child_data->file_is_hidden;
913 child_is_hidden = FALSE;
915 g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0, button_data->path, child_is_hidden);
919 get_button_image (GtkPathBar *path_bar,
920 ButtonType button_type)
922 GtkFileSystemVolume *volume;
928 if (path_bar->root_icon != NULL)
929 return path_bar->root_icon;
931 volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
935 path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
937 GTK_WIDGET (path_bar),
940 gtk_file_system_volume_free (path_bar->file_system, volume);
942 return path_bar->root_icon;
944 if (path_bar->home_icon != NULL)
945 return path_bar->home_icon;
947 path_bar->home_icon = gtk_file_system_render_icon (path_bar->file_system,
949 GTK_WIDGET (path_bar),
952 return path_bar->home_icon;
954 if (path_bar->desktop_icon != NULL)
955 return path_bar->desktop_icon;
957 path_bar->desktop_icon = gtk_file_system_render_icon (path_bar->file_system,
958 path_bar->desktop_path,
959 GTK_WIDGET (path_bar),
962 return path_bar->desktop_icon;
971 button_data_free (ButtonData *button_data)
973 gtk_file_path_free (button_data->path);
974 g_free (button_data->dir_name);
975 g_free (button_data);
979 get_dir_name (ButtonData *button_data)
981 if (button_data->type == HOME_BUTTON)
983 else if (button_data->type == DESKTOP_BUTTON)
986 return button_data->dir_name;
990 /* We always want to request the same size for the label, whether
991 * or not the contents are bold
994 label_size_request_cb (GtkWidget *widget,
995 GtkRequisition *requisition,
996 ButtonData *button_data)
998 const gchar *dir_name = get_dir_name (button_data);
999 PangoLayout *layout = gtk_widget_create_pango_layout (button_data->label, dir_name);
1000 gint bold_width, bold_height;
1003 pango_layout_get_pixel_size (layout, &requisition->width, &requisition->height);
1005 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1006 pango_layout_set_markup (layout, markup, -1);
1009 pango_layout_get_pixel_size (layout, &bold_width, &bold_height);
1010 requisition->width = MAX (requisition->width, bold_width);
1011 requisition->height = MAX (requisition->height, bold_height);
1013 g_object_unref (layout);
1017 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
1018 ButtonData *button_data,
1019 gboolean current_dir)
1021 const gchar *dir_name = get_dir_name (button_data);
1023 if (button_data->label != NULL)
1029 markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
1030 gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
1035 gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
1039 if (button_data->image != NULL)
1042 pixbuf = get_button_image (path_bar, button_data->type);
1043 gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), pixbuf);
1046 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
1048 button_data->ignore_changes = TRUE;
1049 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
1050 button_data->ignore_changes = FALSE;
1055 find_button_type (GtkPathBar *path_bar,
1058 if (path_bar->root_path != NULL &&
1059 ! gtk_file_path_compare (path, path_bar->root_path))
1061 if (path_bar->home_path != NULL &&
1062 ! gtk_file_path_compare (path, path_bar->home_path))
1064 if (path_bar->desktop_path != NULL &&
1065 ! gtk_file_path_compare (path, path_bar->desktop_path))
1066 return DESKTOP_BUTTON;
1068 return NORMAL_BUTTON;
1072 button_drag_data_get_cb (GtkWidget *widget,
1073 GdkDragContext *context,
1074 GtkSelectionData *selection_data,
1079 ButtonData *button_data;
1080 GtkPathBar *path_bar;
1085 path_bar = GTK_PATH_BAR (widget->parent); /* the button's parent *is* the path bar */
1087 uri = gtk_file_system_path_to_uri (path_bar->file_system, button_data->path);
1088 uri_list = g_strconcat (uri, "\r\n", NULL);
1091 gtk_selection_data_set (selection_data,
1092 selection_data->target,
1100 make_directory_button (GtkPathBar *path_bar,
1101 const char *dir_name,
1103 gboolean current_dir,
1104 gboolean file_is_hidden)
1106 const GtkTargetEntry targets[] = {
1107 { "text/uri-list", 0, 0 }
1110 GtkWidget *child = NULL;
1111 GtkWidget *label_alignment = NULL;
1112 ButtonData *button_data;
1114 file_is_hidden = !! file_is_hidden;
1115 /* Is it a special button? */
1116 button_data = g_new0 (ButtonData, 1);
1118 button_data->type = find_button_type (path_bar, path);
1119 button_data->button = gtk_toggle_button_new ();
1121 switch (button_data->type)
1124 button_data->image = gtk_image_new ();
1125 child = button_data->image;
1126 button_data->label = NULL;
1129 case DESKTOP_BUTTON:
1130 button_data->image = gtk_image_new ();
1131 button_data->label = gtk_label_new (NULL);
1132 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1133 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1134 child = gtk_hbox_new (FALSE, 2);
1135 gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
1136 gtk_box_pack_start (GTK_BOX (child), label_alignment, FALSE, FALSE, 0);
1140 button_data->label = gtk_label_new (NULL);
1141 label_alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
1142 gtk_container_add (GTK_CONTAINER (label_alignment), button_data->label);
1143 child = label_alignment;
1144 button_data->image = NULL;
1147 /* label_alignment is created because we can't override size-request
1148 * on label itself and still have the contents of the label centered
1149 * properly in the label's requisition
1151 if (label_alignment)
1152 g_signal_connect (label_alignment, "size-request",
1153 G_CALLBACK (label_size_request_cb), button_data);
1155 button_data->dir_name = g_strdup (dir_name);
1156 button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
1157 button_data->file_is_hidden = file_is_hidden;
1159 gtk_container_add (GTK_CONTAINER (button_data->button), child);
1160 gtk_widget_show_all (button_data->button);
1162 gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
1164 g_signal_connect (button_data->button, "clicked",
1165 G_CALLBACK (button_clicked_cb),
1167 g_object_weak_ref (G_OBJECT (button_data->button),
1168 (GWeakNotify) button_data_free, button_data);
1170 gtk_drag_source_set (button_data->button,
1173 G_N_ELEMENTS (targets),
1175 g_signal_connect (button_data->button, "drag-data-get",
1176 G_CALLBACK (button_drag_data_get_cb), button_data);
1182 gtk_path_bar_check_parent_path (GtkPathBar *path_bar,
1183 const GtkFilePath *file_path,
1184 GtkFileSystem *file_system)
1187 GList *current_path = NULL;
1189 for (list = path_bar->button_list; list; list = list->next)
1191 ButtonData *button_data;
1193 button_data = list->data;
1194 if (! gtk_file_path_compare (file_path, button_data->path))
1196 current_path = list;
1203 for (list = path_bar->button_list; list; list = list->next)
1205 gtk_path_bar_update_button_appearance (path_bar,
1206 BUTTON_DATA (list->data),
1207 (list == current_path) ? TRUE : FALSE);
1210 if (!gtk_widget_get_child_visible (BUTTON_DATA (current_path->data)->button))
1212 path_bar->first_scrolled_button = current_path;
1213 gtk_widget_queue_resize (GTK_WIDGET (path_bar));
1222 _gtk_path_bar_set_path (GtkPathBar *path_bar,
1223 const GtkFilePath *file_path,
1227 gboolean first_directory = TRUE;
1229 GList *new_buttons = NULL;
1231 g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
1232 g_return_val_if_fail (file_path != NULL, FALSE);
1236 /* Check whether the new path is already present in the pathbar as buttons.
1237 * This could be a parent directory or a previous selected subdirectory.
1239 if (gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
1242 path = gtk_file_path_copy (file_path);
1244 gtk_widget_push_composite_child ();
1246 while (path != NULL)
1248 GtkFilePath *parent_path = NULL;
1249 ButtonData *button_data;
1250 const gchar *display_name;
1252 GtkFileFolder *file_folder;
1253 GtkFileInfo *file_info;
1256 valid = gtk_file_system_get_parent (path_bar->file_system,
1263 gtk_file_path_free (path);
1267 file_folder = gtk_file_system_get_folder (path_bar->file_system,
1268 parent_path ? parent_path : path,
1269 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_HIDDEN,
1274 gtk_file_path_free (parent_path);
1275 gtk_file_path_free (path);
1279 file_info = gtk_file_folder_get_info (file_folder, parent_path ? path : NULL, error);
1280 g_object_unref (file_folder);
1285 gtk_file_path_free (parent_path);
1286 gtk_file_path_free (path);
1290 display_name = gtk_file_info_get_display_name (file_info);
1291 is_hidden = gtk_file_info_get_is_hidden (file_info);
1293 button_data = make_directory_button (path_bar, display_name, path, first_directory, is_hidden);
1294 gtk_file_info_free (file_info);
1295 gtk_file_path_free (path);
1297 new_buttons = g_list_prepend (new_buttons, button_data);
1299 if (button_data->type != NORMAL_BUTTON)
1301 gtk_file_path_free (parent_path);
1306 first_directory = FALSE;
1313 gtk_path_bar_clear_buttons (path_bar);
1314 path_bar->button_list = g_list_reverse (new_buttons);
1316 for (l = path_bar->button_list; l; l = l->next)
1318 GtkWidget *button = BUTTON_DATA (l->data)->button;
1319 gtk_container_add (GTK_CONTAINER (path_bar), button);
1326 for (l = new_buttons; l; l = l->next)
1328 ButtonData *button_data;
1330 button_data = BUTTON_DATA (l->data);
1331 gtk_widget_destroy (button_data->button);
1334 g_list_free (new_buttons);
1337 gtk_widget_pop_composite_child ();
1343 /* FIXME: This should be a construct-only property */
1345 _gtk_path_bar_set_file_system (GtkPathBar *path_bar,
1346 GtkFileSystem *file_system)
1351 g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1353 g_assert (path_bar->file_system == NULL);
1355 path_bar->file_system = g_object_ref (file_system);
1357 home = g_get_home_dir ();
1360 path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1361 /* FIXME: Need file system backend specific way of getting the
1364 desktop = g_build_filename (home, "Desktop", NULL);
1365 path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1370 path_bar->home_path = NULL;
1371 path_bar->desktop_path = NULL;
1373 path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1378 * @path_bar: a #GtkPathBar
1380 * If the selected button in the pathbar is not the furthest button "up" (in the
1381 * root direction), act as if the user clicked on the next button up.
1384 _gtk_path_bar_up (GtkPathBar *path_bar)
1388 for (l = path_bar->button_list; l; l = l->next)
1390 GtkWidget *button = BUTTON_DATA (l->data)->button;
1391 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1395 GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1396 button_clicked_cb (next_button, l->next->data);
1404 * _gtk_path_bar_down:
1405 * @path_bar: a #GtkPathBar
1407 * If the selected button in the pathbar is not the furthest button "down" (in the
1408 * leaf direction), act as if the user clicked on the next button down.
1411 _gtk_path_bar_down (GtkPathBar *path_bar)
1415 for (l = path_bar->button_list; l; l = l->next)
1417 GtkWidget *button = BUTTON_DATA (l->data)->button;
1418 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1422 GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1423 button_clicked_cb (prev_button, l->prev->data);