]> Pileus Git - ~andy/gtk/blob - gtk/gtkpathbar.c
Make this work when the slider buttons have been destroyed.
[~andy/gtk] / gtk / gtkpathbar.c
1 /* gtkpathbar.h
2  * Copyright (C) 2004  Red Hat, Inc.,  Jonathan Blandford <jrb@gnome.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <config.h>
21 #include <string.h>
22 #include "gtkpathbar.h"
23 #include "gtktogglebutton.h"
24 #include "gtkarrow.h"
25 #include "gtkimage.h"
26 #include "gtkintl.h"
27 #include "gtkicontheme.h"
28 #include "gtkiconfactory.h"
29 #include "gtklabel.h"
30 #include "gtkhbox.h"
31 #include "gtkmain.h"
32 #include "gtkmarshalers.h"
33
34 enum {
35   PATH_CLICKED,
36   LAST_SIGNAL
37 };
38
39 typedef enum {
40   NORMAL_BUTTON,
41   ROOT_BUTTON,
42   HOME_BUTTON,
43   DESKTOP_BUTTON
44 } ButtonType;
45
46 #define BUTTON_DATA(x) ((ButtonData *)(x))
47
48 static guint path_bar_signals [LAST_SIGNAL] = { 0 };
49
50 /* Icon size for if we can't get it from the theme */
51 #define FALLBACK_ICON_SIZE 20
52
53 typedef struct _ButtonData ButtonData;
54
55 struct _ButtonData
56 {
57   GtkWidget *button;
58   ButtonType type;
59   char *dir_name;
60   GtkFilePath *path;
61   GtkWidget *image;
62   GtkWidget *label;
63   gboolean ignore_changes;
64 };
65
66 G_DEFINE_TYPE (GtkPathBar,
67                gtk_path_bar,
68                GTK_TYPE_CONTAINER);
69
70 static void gtk_path_bar_finalize                 (GObject          *object);
71 static void gtk_path_bar_dispose                  (GObject          *object);
72 static void gtk_path_bar_size_request             (GtkWidget        *widget,
73                                                    GtkRequisition   *requisition);
74 static void gtk_path_bar_size_allocate            (GtkWidget        *widget,
75                                                    GtkAllocation    *allocation);
76 static void gtk_path_bar_add                      (GtkContainer     *container,
77                                                    GtkWidget        *widget);
78 static void gtk_path_bar_remove                   (GtkContainer     *container,
79                                                    GtkWidget        *widget);
80 static void gtk_path_bar_forall                   (GtkContainer     *container,
81                                                    gboolean          include_internals,
82                                                    GtkCallback       callback,
83                                                    gpointer          callback_data);
84 static void gtk_path_bar_scroll_up                (GtkWidget        *button,
85                                                    GtkPathBar       *path_bar);
86 static void gtk_path_bar_scroll_down              (GtkWidget        *button,
87                                                    GtkPathBar       *path_bar);
88 static void gtk_path_bar_style_set                (GtkWidget        *widget,
89                                                    GtkStyle         *previous_style);
90 static void gtk_path_bar_screen_changed           (GtkWidget        *widget,
91                                                    GdkScreen        *previous_screen);
92 static void gtk_path_bar_check_icon_theme         (GtkPathBar       *path_bar);
93 static void gtk_path_bar_update_button_appearance (GtkPathBar       *path_bar,
94                                                    ButtonData       *button_data,
95                                                    gboolean          current_dir);
96
97 static GtkWidget *
98 get_slider_button (GtkPathBar  *path_bar,
99                    GtkArrowType arrow_type)
100 {
101   GtkWidget *button;
102
103   gtk_widget_push_composite_child ();
104
105   button = gtk_button_new ();
106   gtk_container_add (GTK_CONTAINER (button), gtk_arrow_new (arrow_type, GTK_SHADOW_OUT));
107   gtk_container_add (GTK_CONTAINER (path_bar), button);
108   gtk_widget_show_all (button);
109
110   gtk_widget_pop_composite_child ();
111
112   return button;
113 }
114
115 static void
116 gtk_path_bar_init (GtkPathBar *path_bar)
117 {
118   GTK_WIDGET_SET_FLAGS (path_bar, GTK_NO_WINDOW);
119   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (path_bar), FALSE);
120
121   path_bar->spacing = 3;
122   path_bar->up_slider_button = get_slider_button (path_bar, GTK_ARROW_LEFT);
123   path_bar->down_slider_button = get_slider_button (path_bar, GTK_ARROW_RIGHT);
124   path_bar->icon_size = FALLBACK_ICON_SIZE;
125
126   g_signal_connect (path_bar->up_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_up), path_bar);
127   g_signal_connect (path_bar->down_slider_button, "clicked", G_CALLBACK (gtk_path_bar_scroll_down), path_bar);
128 }
129
130 static void
131 gtk_path_bar_class_init (GtkPathBarClass *path_bar_class)
132 {
133   GObjectClass *gobject_class;
134   GtkObjectClass *object_class;
135   GtkWidgetClass *widget_class;
136   GtkContainerClass *container_class;
137
138   gobject_class = (GObjectClass *) path_bar_class;
139   object_class = (GtkObjectClass *) path_bar_class;
140   widget_class = (GtkWidgetClass *) path_bar_class;
141   container_class = (GtkContainerClass *) path_bar_class;
142
143   gobject_class->finalize = gtk_path_bar_finalize;
144   gobject_class->dispose = gtk_path_bar_dispose;
145
146   widget_class->size_request = gtk_path_bar_size_request;
147   widget_class->size_allocate = gtk_path_bar_size_allocate;
148   widget_class->style_set = gtk_path_bar_style_set;
149   widget_class->screen_changed = gtk_path_bar_screen_changed;
150
151   container_class->add = gtk_path_bar_add;
152   container_class->forall = gtk_path_bar_forall;
153   container_class->remove = gtk_path_bar_remove;
154   /* FIXME: */
155   /*  container_class->child_type = gtk_path_bar_child_type;*/
156
157   path_bar_signals [PATH_CLICKED] =
158     g_signal_new ("path_clicked",
159                   G_OBJECT_CLASS_TYPE (object_class),
160                   G_SIGNAL_RUN_FIRST,
161                   G_STRUCT_OFFSET (GtkPathBarClass, path_clicked),
162                   NULL, NULL,
163                   _gtk_marshal_VOID__POINTER,
164                   G_TYPE_NONE, 1,
165                   G_TYPE_POINTER);
166 }
167
168
169 static void
170 gtk_path_bar_finalize (GObject *object)
171 {
172   GtkPathBar *path_bar;
173
174   path_bar = GTK_PATH_BAR (object);
175   g_list_free (path_bar->button_list);
176   if (path_bar->root_path)
177     gtk_file_path_free (path_bar->root_path);
178   if (path_bar->home_path)
179     gtk_file_path_free (path_bar->home_path);
180   if (path_bar->desktop_path)
181     gtk_file_path_free (path_bar->desktop_path);
182
183   if (path_bar->root_icon)
184     g_object_unref (path_bar->root_icon);
185   if (path_bar->home_icon)
186     g_object_unref (path_bar->home_icon);
187   if (path_bar->desktop_icon)
188     g_object_unref (path_bar->desktop_icon);
189
190   if (path_bar->file_system)
191     g_object_unref (path_bar->file_system);
192
193   G_OBJECT_CLASS (gtk_path_bar_parent_class)->finalize (object);
194 }
195
196 /* Removes the settings signal handler.  It's safe to call multiple times */
197 static void
198 remove_settings_signal (GtkPathBar *path_bar,
199                         GdkScreen  *screen)
200 {
201   if (path_bar->settings_signal_id)
202     {
203       GtkSettings *settings;
204
205       settings = gtk_settings_get_for_screen (screen);
206       g_signal_handler_disconnect (settings,
207                                    path_bar->settings_signal_id);
208       path_bar->settings_signal_id = 0;
209     }
210 }
211
212 static void
213 gtk_path_bar_dispose (GObject *object)
214 {
215   remove_settings_signal (GTK_PATH_BAR (object),
216                           gtk_widget_get_screen (GTK_WIDGET (object)));
217
218   G_OBJECT_CLASS (gtk_path_bar_parent_class)->dispose (object);
219 }
220
221 /* Size requisition:
222  * 
223  * Ideally, our size is determined by another widget, and we are just filling
224  * available space.
225  */
226 static void
227 gtk_path_bar_size_request (GtkWidget      *widget,
228                            GtkRequisition *requisition)
229 {
230   ButtonData *button_data;
231   GtkPathBar *path_bar;
232   GtkRequisition child_requisition;
233   GList *list;
234
235   path_bar = GTK_PATH_BAR (widget);
236
237   requisition->width = 0;
238   requisition->height = 0;
239
240   for (list = path_bar->button_list; list; list = list->next)
241     {
242       button_data = BUTTON_DATA (list->data);
243       gtk_widget_size_request (button_data->button, &child_requisition);
244       requisition->width = MAX (child_requisition.width, requisition->width);
245       requisition->height = MAX (child_requisition.height, requisition->height);
246     }
247
248   /* Add space for slider, if we have more than one path */
249   /* Theoretically, the slider could be bigger than the other button.  But we're
250    * not going to worry about that now.
251    */
252   path_bar->slider_width = requisition->height / 2 + 5;
253   if (path_bar->button_list && path_bar->button_list->next != NULL)
254     requisition->width += (path_bar->spacing + path_bar->slider_width) * 2;
255
256   gtk_widget_size_request (path_bar->up_slider_button, &child_requisition);
257   gtk_widget_size_request (path_bar->down_slider_button, &child_requisition);
258
259   requisition->width += GTK_CONTAINER (widget)->border_width * 2;
260   requisition->height += GTK_CONTAINER (widget)->border_width * 2;
261
262   widget->requisition = *requisition;
263 }
264
265 static void
266 gtk_path_bar_update_slider_buttons (GtkPathBar *path_bar)
267 {
268   if (path_bar->button_list)
269     {
270       GtkWidget *button;
271
272       button = BUTTON_DATA (path_bar->button_list->data)->button;
273       if (gtk_widget_get_child_visible (button))
274         gtk_widget_set_sensitive (path_bar->down_slider_button, FALSE);
275       else
276         gtk_widget_set_sensitive (path_bar->down_slider_button, TRUE);
277
278       button = BUTTON_DATA (g_list_last (path_bar->button_list)->data)->button;
279       if (gtk_widget_get_child_visible (button))
280         gtk_widget_set_sensitive (path_bar->up_slider_button, FALSE);
281       else
282         gtk_widget_set_sensitive (path_bar->up_slider_button, TRUE);
283     }
284 }
285
286 /* This is a tad complicated
287  */
288 static void
289 gtk_path_bar_size_allocate (GtkWidget     *widget,
290                             GtkAllocation *allocation)
291 {
292   GtkWidget *child;
293   GtkPathBar *path_bar = GTK_PATH_BAR (widget);
294   GtkTextDirection direction;
295   GtkAllocation child_allocation;
296   GList *list, *first_button;
297   gint width;
298   gint allocation_width;
299   gint border_width;
300   gboolean need_sliders = FALSE;
301   gint up_slider_offset = 0;
302   gint down_slider_offset = 0;
303
304   widget->allocation = *allocation;
305
306   /* No path is set; we don't have to allocate anything. */
307   if (path_bar->button_list == NULL)
308     return;
309
310   direction = gtk_widget_get_direction (widget);
311   border_width = (gint) GTK_CONTAINER (path_bar)->border_width;
312   allocation_width = allocation->width - 2 * border_width;
313
314   /* First, we check to see if we need the scrollbars. */
315   width = BUTTON_DATA (path_bar->button_list->data)->button->requisition.width;
316   for (list = path_bar->button_list->next; list; list = list->next)
317     {
318       child = BUTTON_DATA (list->data)->button;
319
320       width += child->requisition.width + path_bar->spacing;
321     }
322
323   if (width <= allocation_width)
324     {
325       first_button = g_list_last (path_bar->button_list);
326     }
327   else
328     {
329       gboolean reached_end = FALSE;
330       gint slider_space = 2 * (path_bar->spacing + path_bar->slider_width);
331
332       if (path_bar->first_scrolled_button)
333         first_button = path_bar->first_scrolled_button;
334       else
335         first_button = path_bar->button_list;
336       need_sliders = TRUE;
337       
338       /* To see how much space we have, and how many buttons we can display.
339        * We start at the first button, count forward until hit the new
340        * button, then count backwards.
341        */
342       /* Count down the path chain towards the end. */
343       width = BUTTON_DATA (first_button->data)->button->requisition.width;
344       list = first_button->prev;
345       while (list && !reached_end)
346         {
347           child = BUTTON_DATA (list->data)->button;
348
349           if (width + child->requisition.width +
350               path_bar->spacing + slider_space > allocation_width)
351             reached_end = TRUE;
352           else
353             width += child->requisition.width + path_bar->spacing;
354
355           list = list->prev;
356         }
357
358       /* Finally, we walk up, seeing how many of the previous buttons we can
359        * add */
360       while (first_button->next && ! reached_end)
361         {
362           child = BUTTON_DATA (first_button->next->data)->button;
363
364           if (width + child->requisition.width + path_bar->spacing + slider_space > allocation_width)
365             {
366               reached_end = TRUE;
367             }
368           else
369             {
370               width += child->requisition.width + path_bar->spacing;
371               first_button = first_button->next;
372             }
373         }
374     }
375
376   /* Now, we allocate space to the buttons */
377   child_allocation.y = allocation->y + border_width;
378   child_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
379
380   if (direction == GTK_TEXT_DIR_RTL)
381     {
382       child_allocation.x = allocation->x + allocation->width - border_width;
383       if (need_sliders)
384         {
385           child_allocation.x -= (path_bar->spacing + path_bar->slider_width);
386           up_slider_offset = allocation->width - border_width - path_bar->slider_width;
387         }
388     }
389   else
390     {
391       child_allocation.x = allocation->x + border_width;
392       if (need_sliders)
393         {
394           up_slider_offset = border_width;
395           child_allocation.x += (path_bar->spacing + path_bar->slider_width);
396         }
397     }
398
399   for (list = first_button; list; list = list->prev)
400     {
401       child = BUTTON_DATA (list->data)->button;
402
403       child_allocation.width = child->requisition.width;
404       if (direction == GTK_TEXT_DIR_RTL)
405         child_allocation.x -= child_allocation.width;
406
407       /* Check to see if we've don't have any more space to allocate buttons */
408       if (need_sliders && direction == GTK_TEXT_DIR_RTL)
409         {
410           if (child_allocation.x - path_bar->spacing - path_bar->slider_width < widget->allocation.x + border_width)
411             break;
412         }
413       else if (need_sliders && direction == GTK_TEXT_DIR_LTR)
414         {
415           if (child_allocation.x + child_allocation.width + path_bar->spacing + path_bar->slider_width >
416               widget->allocation.x + border_width + allocation_width)
417             break;
418         }
419
420       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, TRUE);
421       gtk_widget_size_allocate (child, &child_allocation);
422
423       if (direction == GTK_TEXT_DIR_RTL)
424         {
425           child_allocation.x -= path_bar->spacing;
426           down_slider_offset = child_allocation.x - widget->allocation.x - path_bar->slider_width;
427         }
428       else
429         {
430           child_allocation.x += child_allocation.width + path_bar->spacing;
431           down_slider_offset = child_allocation.x - widget->allocation.x;
432         }
433     }
434   /* Now we go hide all the widgets that don't fit */
435   while (list)
436     {
437       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
438       list = list->prev;
439     }
440   for (list = first_button->next; list; list = list->next)
441     {
442       gtk_widget_set_child_visible (BUTTON_DATA (list->data)->button, FALSE);
443     }
444
445   if (need_sliders)
446     {
447       child_allocation.width = path_bar->slider_width;
448       
449       child_allocation.x = up_slider_offset + allocation->x;
450       gtk_widget_size_allocate (path_bar->up_slider_button, &child_allocation);
451
452       child_allocation.x = down_slider_offset + allocation->x;
453       gtk_widget_size_allocate (path_bar->down_slider_button, &child_allocation);
454
455       gtk_widget_set_child_visible (path_bar->up_slider_button, TRUE);
456       gtk_widget_set_child_visible (path_bar->down_slider_button, TRUE);
457       gtk_widget_show_all (path_bar->up_slider_button);
458       gtk_widget_show_all (path_bar->down_slider_button);
459       gtk_path_bar_update_slider_buttons (path_bar);
460     }
461   else
462     {
463       gtk_widget_set_child_visible (path_bar->up_slider_button, FALSE);
464       gtk_widget_set_child_visible (path_bar->down_slider_button, FALSE);
465     }
466 }
467
468 static void
469 gtk_path_bar_style_set (GtkWidget *widget,
470                         GtkStyle  *previous_style)
471 {
472   if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set)
473     GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->style_set (widget, previous_style);
474
475   gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
476 }
477
478 static void
479 gtk_path_bar_screen_changed (GtkWidget *widget,
480                              GdkScreen *previous_screen)
481 {
482   if (GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed)
483     GTK_WIDGET_CLASS (gtk_path_bar_parent_class)->screen_changed (widget, previous_screen);
484
485   /* We might nave a new settings, so we remove the old one */
486   if (previous_screen)
487     remove_settings_signal (GTK_PATH_BAR (widget), previous_screen);
488
489   gtk_path_bar_check_icon_theme (GTK_PATH_BAR (widget));
490 }
491
492 static void
493 gtk_path_bar_add (GtkContainer *container,
494                   GtkWidget    *widget)
495 {
496   gtk_widget_set_parent (widget, GTK_WIDGET (container));
497 }
498
499 static void
500 gtk_path_bar_remove_1 (GtkContainer *container,
501                        GtkWidget    *widget)
502 {
503   gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
504   gtk_widget_unparent (widget);
505   if (was_visible)
506     gtk_widget_queue_resize (GTK_WIDGET (container));
507 }
508
509 static void
510 gtk_path_bar_remove (GtkContainer *container,
511                      GtkWidget    *widget)
512 {
513   GtkPathBar *path_bar;
514   GList *children;
515
516   path_bar = GTK_PATH_BAR (container);
517
518   if (widget == path_bar->up_slider_button)
519     {
520       gtk_path_bar_remove_1 (container, widget);
521       path_bar->up_slider_button = NULL;
522       return;
523     }
524
525   if (widget == path_bar->down_slider_button)
526     {
527       gtk_path_bar_remove_1 (container, widget);
528       path_bar->down_slider_button = NULL;
529       return;
530     }
531
532   children = path_bar->button_list;
533   while (children)
534     {
535       if (widget == BUTTON_DATA (children->data)->button)
536         {
537           gtk_path_bar_remove_1 (container, widget);
538           path_bar->button_list = g_list_remove_link (path_bar->button_list, children);
539           g_list_free (children);
540           return;
541         }
542       
543       children = children->next;
544     }
545 }
546
547 static void
548 gtk_path_bar_forall (GtkContainer *container,
549                      gboolean      include_internals,
550                      GtkCallback   callback,
551                      gpointer      callback_data)
552 {
553   GtkPathBar *path_bar;
554   GList *children;
555
556   g_return_if_fail (callback != NULL);
557   path_bar = GTK_PATH_BAR (container);
558
559   children = path_bar->button_list;
560   while (children)
561     {
562       GtkWidget *child;
563       child = BUTTON_DATA (children->data)->button;
564       children = children->next;
565
566       (* callback) (child, callback_data);
567     }
568
569   if (path_bar->up_slider_button)
570     (* callback) (path_bar->up_slider_button, callback_data);
571
572   if (path_bar->down_slider_button)
573     (* callback) (path_bar->down_slider_button, callback_data);
574 }
575
576 static void
577 gtk_path_bar_scroll_down (GtkWidget *button, GtkPathBar *path_bar)
578 {
579   GList *list;
580   GList *down_button = NULL;
581   GList *up_button = NULL;
582   gint space_available;
583   gint space_needed;
584   gint border_width;
585   GtkTextDirection direction;
586   
587   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
588
589   border_width = GTK_CONTAINER (path_bar)->border_width;
590   direction = gtk_widget_get_direction (GTK_WIDGET (path_bar));
591   
592   /* We find the button at the 'down' end that we have to make
593    * visible */
594   for (list = path_bar->button_list; list; list = list->next)
595     {
596       if (list->next && gtk_widget_get_child_visible (BUTTON_DATA (list->next->data)->button))
597         {
598           down_button = list;
599           break;
600         }
601     }
602   
603   /* Find the last visible button on the 'up' end
604    */
605   for (list = g_list_last (path_bar->button_list); list; list = list->prev)
606     {
607       if (gtk_widget_get_child_visible (BUTTON_DATA (list->data)->button))
608         {
609           up_button = list;
610           break;
611         }
612     }
613
614   space_needed = BUTTON_DATA (down_button->data)->button->allocation.width + path_bar->spacing;
615   if (direction == GTK_TEXT_DIR_RTL)
616     space_available = path_bar->down_slider_button->allocation.x - GTK_WIDGET (path_bar)->allocation.x;
617   else
618     space_available = (GTK_WIDGET (path_bar)->allocation.x + GTK_WIDGET (path_bar)->allocation.width - border_width) -
619       (path_bar->down_slider_button->allocation.x + path_bar->down_slider_button->allocation.width);
620
621   /* We have space_available extra space that's not being used.  We
622    * need space_needed space to make the button fit.  So we walk down
623    * from the end, removing buttons until we get all the space we
624    * need. */
625   while (space_available < space_needed)
626     {
627       space_available += BUTTON_DATA (up_button->data)->button->allocation.width + path_bar->spacing;
628       up_button = up_button->prev;
629       path_bar->first_scrolled_button = up_button;
630     }
631 }
632
633 static void
634 gtk_path_bar_scroll_up (GtkWidget *button, GtkPathBar *path_bar)
635 {
636   GList *list;
637
638   gtk_widget_queue_resize (GTK_WIDGET (path_bar));
639
640   for (list = g_list_last (path_bar->button_list); list; list = list->prev)
641     {
642       if (list->prev && gtk_widget_get_child_visible (BUTTON_DATA (list->prev->data)->button))
643         {
644           path_bar->first_scrolled_button = list;
645           return;
646         }
647     }
648 }
649
650 /* Changes the icons wherever it is needed */
651 static void
652 reload_icons (GtkPathBar *path_bar)
653 {
654   GList *list;
655
656   if (path_bar->root_icon)
657     {
658       g_object_unref (path_bar->root_icon);
659       path_bar->root_icon = NULL;
660     }
661   if (path_bar->home_icon)
662     {
663       g_object_unref (path_bar->home_icon);
664       path_bar->home_icon = NULL;
665     }
666   if (path_bar->desktop_icon)
667     {
668       g_object_unref (path_bar->desktop_icon);
669       path_bar->desktop_icon = NULL;
670     }
671
672   for (list = path_bar->button_list; list; list = list->next)
673     {
674       ButtonData *button_data;
675       gboolean current_dir;
676
677       button_data = BUTTON_DATA (list->data);
678       if (button_data->type != NORMAL_BUTTON)
679         {
680           current_dir = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button));
681           gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
682         }
683     }
684   
685 }
686
687 static void
688 change_icon_theme (GtkPathBar *path_bar)
689 {
690   GtkSettings *settings;
691   gint width, height;
692
693   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
694
695   if (gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_BUTTON, &width, &height))
696     path_bar->icon_size = MAX (width, height);
697   else
698     path_bar->icon_size = FALLBACK_ICON_SIZE;
699
700   reload_icons (path_bar);
701 }
702 /* Callback used when a GtkSettings value changes */
703 static void
704 settings_notify_cb (GObject    *object,
705                     GParamSpec *pspec,
706                     GtkPathBar *path_bar)
707 {
708   const char *name;
709
710   name = g_param_spec_get_name (pspec);
711
712   if (! strcmp (name, "gtk-icon-theme-name") ||
713       ! strcmp (name, "gtk-icon-sizes"))
714     change_icon_theme (path_bar);
715 }
716
717 static void
718 gtk_path_bar_check_icon_theme (GtkPathBar *path_bar)
719 {
720   GtkSettings *settings;
721
722   if (path_bar->settings_signal_id)
723     return;
724
725   settings = gtk_settings_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (path_bar)));
726   path_bar->settings_signal_id = g_signal_connect (settings, "notify", G_CALLBACK (settings_notify_cb), path_bar);
727
728   change_icon_theme (path_bar);
729 }
730
731 /* Public functions and their helpers */
732 static void
733 gtk_path_bar_clear_buttons (GtkPathBar *path_bar)
734 {
735   while (path_bar->button_list != NULL)
736     {
737       gtk_container_remove (GTK_CONTAINER (path_bar), BUTTON_DATA (path_bar->button_list->data)->button);
738     }
739   path_bar->first_scrolled_button = NULL;
740 }
741
742 static void
743 button_clicked_cb (GtkWidget *button,
744                    gpointer   data)
745 {
746   ButtonData *button_data;
747   GtkWidget *path_bar;
748
749   button_data = BUTTON_DATA (data);
750   if (button_data->ignore_changes)
751     return;
752
753   path_bar = button->parent;
754   g_assert (GTK_IS_PATH_BAR (path_bar));
755   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
756
757   g_signal_emit (path_bar, path_bar_signals [PATH_CLICKED], 0, button_data->path);
758 }
759
760 static GdkPixbuf *
761 get_button_image (GtkPathBar *path_bar,
762                   ButtonType  button_type)
763 {
764   GtkFileSystemVolume *volume;
765
766   switch (button_type)
767     {
768     case ROOT_BUTTON:
769
770       if (path_bar->root_icon != NULL)
771         return path_bar->root_icon;
772       
773       volume = gtk_file_system_get_volume_for_path (path_bar->file_system, path_bar->root_path);
774       if (volume == NULL)
775         return NULL;
776
777       path_bar->root_icon = gtk_file_system_volume_render_icon (path_bar->file_system,
778                                                                 volume,
779                                                                 GTK_WIDGET (path_bar),
780                                                                 path_bar->icon_size,
781                                                                 NULL);
782       gtk_file_system_volume_free (path_bar->file_system, volume);
783
784       return path_bar->root_icon;
785     case HOME_BUTTON:
786       if (path_bar->home_icon != NULL)
787         return path_bar->home_icon;
788
789       path_bar->home_icon = gtk_file_system_render_icon (path_bar->file_system,
790                                                          path_bar->home_path,
791                                                          GTK_WIDGET (path_bar),
792                                                          path_bar->icon_size,
793                                                          NULL);
794       return path_bar->home_icon;
795     case DESKTOP_BUTTON:
796       if (path_bar->desktop_icon != NULL)
797         return path_bar->desktop_icon;
798
799       path_bar->desktop_icon = gtk_file_system_render_icon (path_bar->file_system,
800                                                             path_bar->desktop_path,
801                                                             GTK_WIDGET (path_bar),
802                                                             path_bar->icon_size,
803                                                             NULL);
804       return path_bar->desktop_icon;
805     default:
806       return NULL;
807     }
808   
809   return NULL;
810 }
811
812 static void
813 button_data_free (ButtonData *button_data)
814 {
815   gtk_file_path_free (button_data->path);
816   g_free (button_data->dir_name);
817   g_free (button_data);
818 }
819
820 static void
821 gtk_path_bar_update_button_appearance (GtkPathBar *path_bar,
822                                        ButtonData *button_data,
823                                        gboolean    current_dir)
824 {
825   const gchar *dir_name;
826
827   if (button_data->type == HOME_BUTTON)
828     dir_name = _("Home");
829   else if (button_data->type == DESKTOP_BUTTON)
830     dir_name = _("Desktop");
831   else
832     dir_name = button_data->dir_name;
833
834   if (button_data->label != NULL)
835     {
836       if (current_dir)
837         {
838           char *markup;
839
840           markup = g_markup_printf_escaped ("<b>%s</b>", dir_name);
841           gtk_label_set_markup (GTK_LABEL (button_data->label), markup);
842           g_free (markup);
843         }
844       else
845         {
846           gtk_label_set_text (GTK_LABEL (button_data->label), dir_name);
847         }
848     }
849
850   if (button_data->image != NULL)
851     {
852       GdkPixbuf *pixbuf;
853       pixbuf = get_button_image (path_bar, button_data->type);
854       gtk_image_set_from_pixbuf (GTK_IMAGE (button_data->image), pixbuf);
855     }
856
857   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_data->button)) != current_dir)
858     {
859       button_data->ignore_changes = TRUE;
860       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_data->button), current_dir);
861       button_data->ignore_changes = FALSE;
862     }
863 }
864
865 static ButtonType
866 find_button_type (GtkPathBar  *path_bar,
867                   GtkFilePath *path)
868 {
869   if (! gtk_file_path_compare (path, path_bar->root_path))
870     return ROOT_BUTTON;
871   if (! gtk_file_path_compare (path, path_bar->home_path))
872     return HOME_BUTTON;
873   if (! gtk_file_path_compare (path, path_bar->desktop_path))
874     return DESKTOP_BUTTON;
875
876  return NORMAL_BUTTON;
877 }
878
879 static ButtonData *
880 make_directory_button (GtkPathBar  *path_bar,
881                        const char  *dir_name,
882                        GtkFilePath *path,
883                        gboolean     current_dir)
884 {
885   GtkWidget *child = NULL;
886   ButtonData *button_data;
887
888   /* Is it a special button? */
889   button_data = g_new0 (ButtonData, 1);
890
891   button_data->type = find_button_type (path_bar, path);
892   button_data->button = gtk_toggle_button_new ();
893
894   switch (button_data->type)
895     {
896     case ROOT_BUTTON:
897       button_data->image = gtk_image_new ();
898       child = button_data->image;
899       button_data->label = NULL;
900       break;
901     case HOME_BUTTON:
902     case DESKTOP_BUTTON:
903       button_data->image = gtk_image_new ();
904       button_data->label = gtk_label_new (NULL);
905       child = gtk_hbox_new (FALSE, 2);
906       gtk_box_pack_start (GTK_BOX (child), button_data->image, FALSE, FALSE, 0);
907       gtk_box_pack_start (GTK_BOX (child), button_data->label, FALSE, FALSE, 0);
908       break;
909     case NORMAL_BUTTON:
910     default:
911       button_data->label = gtk_label_new (NULL);
912       child = button_data->label;
913       button_data->image = NULL;
914     }
915
916   button_data->dir_name = g_strdup (dir_name);
917   button_data->path = gtk_file_path_new_dup (gtk_file_path_get_string (path));
918                           
919   gtk_container_add (GTK_CONTAINER (button_data->button), child);
920   gtk_widget_show_all (button_data->button);
921
922   gtk_path_bar_update_button_appearance (path_bar, button_data, current_dir);
923
924   g_signal_connect (button_data->button, "clicked",
925                     G_CALLBACK (button_clicked_cb),
926                     button_data);
927   g_object_weak_ref (G_OBJECT (button_data->button),
928                      (GWeakNotify) button_data_free, button_data);
929
930   return button_data;
931 }
932
933 static gboolean
934 gtk_path_bar_check_parent_path (GtkPathBar         *path_bar,
935                                 const GtkFilePath  *file_path,
936                                 GtkFileSystem      *file_system)
937 {
938   GList *list;
939   GList *current_path = NULL;
940
941   for (list = path_bar->button_list; list; list = list->next)
942     {
943       ButtonData *button_data;
944
945       button_data = list->data;
946       if (! gtk_file_path_compare (file_path, button_data->path))
947         {
948           current_path = list;
949           break;
950         }
951     }
952
953   if (current_path)
954     {
955       for (list = path_bar->button_list; list; list = list->next)
956         {
957           gtk_path_bar_update_button_appearance (path_bar,
958                                                  BUTTON_DATA (list->data),
959                                                  (list == current_path) ? TRUE : FALSE);
960         }
961       return TRUE;
962     }
963   return FALSE;
964 }
965
966 gboolean
967 _gtk_path_bar_set_path (GtkPathBar         *path_bar,
968                         const GtkFilePath  *file_path,
969                         GError            **error)
970 {
971   GtkFilePath *path;
972   gboolean first_directory = TRUE;
973   gboolean result;
974   GList *new_buttons = NULL;
975
976   g_return_val_if_fail (GTK_IS_PATH_BAR (path_bar), FALSE);
977   g_return_val_if_fail (file_path != NULL, FALSE);
978
979   result = TRUE;
980
981   /* Check whether the new path is already present in the pathbar as buttons.
982    * This could be a parent directory or a previous selected subdirectory.
983    */
984   if (gtk_path_bar_check_parent_path (path_bar, file_path, path_bar->file_system))
985     return TRUE;
986
987   path = gtk_file_path_copy (file_path);
988
989   gtk_widget_push_composite_child ();
990
991   while (path != NULL)
992     {
993       GtkFilePath *parent_path = NULL;
994       ButtonData *button_data;
995       const gchar *display_name;
996       GtkFileFolder *file_folder;
997       GtkFileInfo *file_info;
998       gboolean valid;
999
1000       valid = gtk_file_system_get_parent (path_bar->file_system,
1001                                           path,
1002                                           &parent_path,
1003                                           error);
1004       if (!valid)
1005         {
1006           result = FALSE;
1007           gtk_file_path_free (path);
1008           break;
1009         }
1010
1011       file_folder = gtk_file_system_get_folder (path_bar->file_system,
1012                                                 parent_path ? parent_path : path,
1013                                                 GTK_FILE_INFO_DISPLAY_NAME,
1014                                                 NULL);
1015       if (!file_folder)
1016         {
1017           result = FALSE;
1018           gtk_file_path_free (parent_path);
1019           gtk_file_path_free (path);
1020           break;
1021         }
1022
1023       file_info = gtk_file_folder_get_info (file_folder, parent_path ? path : NULL, error);
1024       g_object_unref (file_folder);
1025
1026       if (!file_info)
1027         {
1028           result = FALSE;
1029           gtk_file_path_free (parent_path);
1030           gtk_file_path_free (path);
1031           break;
1032         }
1033
1034       display_name = gtk_file_info_get_display_name (file_info);
1035
1036       button_data = make_directory_button (path_bar, display_name, path, first_directory);
1037       gtk_file_info_free (file_info);
1038       gtk_file_path_free (path);
1039
1040       new_buttons = g_list_prepend (new_buttons, button_data);
1041
1042       if (button_data->type != NORMAL_BUTTON)
1043         {
1044           gtk_file_path_free (parent_path);
1045           break;
1046         }
1047
1048       path = parent_path;
1049       first_directory = FALSE;
1050     }
1051
1052   if (result)
1053     {
1054       GList *l;
1055
1056       gtk_path_bar_clear_buttons (path_bar);
1057       path_bar->button_list = g_list_reverse (new_buttons);
1058
1059       for (l = path_bar->button_list; l; l = l->next)
1060         {
1061           GtkWidget *button = BUTTON_DATA (l->data)->button;
1062           gtk_container_add (GTK_CONTAINER (path_bar), button);
1063         }
1064     }
1065   else
1066     {
1067       GList *l;
1068
1069       for (l = new_buttons; l; l = l->next)
1070         {
1071           GtkWidget *button = BUTTON_DATA (l->data)->button;
1072           gtk_widget_destroy (button);
1073           gtk_widget_unref (button);
1074         }
1075
1076       g_list_free (new_buttons);
1077     }
1078
1079   gtk_widget_pop_composite_child ();
1080
1081   return result;
1082 }
1083
1084
1085 /* FIXME: This should be a construct-only property */
1086 void
1087 _gtk_path_bar_set_file_system (GtkPathBar    *path_bar,
1088                                GtkFileSystem *file_system)
1089 {
1090   const char *home;
1091   char *desktop;
1092
1093   g_return_if_fail (GTK_IS_PATH_BAR (path_bar));
1094
1095   g_assert (path_bar->file_system == NULL);
1096
1097   path_bar->file_system = g_object_ref (file_system);
1098
1099   home = g_get_home_dir ();
1100   desktop = g_build_filename (home, "Desktop", NULL);
1101   path_bar->home_path = gtk_file_system_filename_to_path (path_bar->file_system, home);
1102   path_bar->desktop_path = gtk_file_system_filename_to_path (path_bar->file_system, desktop);
1103   path_bar->root_path = gtk_file_system_filename_to_path (path_bar->file_system, "/");
1104   g_free (desktop);
1105 }
1106
1107 /**
1108  * _gtk_path_bar_up:
1109  * @path_bar: a #GtkPathBar
1110  * 
1111  * If the selected button in the pathbar is not the furthest button "up" (in the
1112  * root direction), act as if the user clicked on the next button up.
1113  **/
1114 void
1115 _gtk_path_bar_up (GtkPathBar *path_bar)
1116 {
1117   GList *l;
1118
1119   for (l = path_bar->button_list; l; l = l->next)
1120     {
1121       GtkWidget *button = BUTTON_DATA (l->data)->button;
1122       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1123         {
1124           if (l->next)
1125             {
1126               GtkWidget *next_button = BUTTON_DATA (l->next->data)->button;
1127               button_clicked_cb (next_button, l->next->data);
1128             }
1129           break;
1130         }
1131     }
1132 }
1133
1134 /**
1135  * _gtk_path_bar_down:
1136  * @path_bar: a #GtkPathBar
1137  * 
1138  * If the selected button in the pathbar is not the furthest button "down" (in the
1139  * leaf direction), act as if the user clicked on the next button down.
1140  **/
1141 void
1142 _gtk_path_bar_down (GtkPathBar *path_bar)
1143 {
1144   GList *l;
1145
1146   for (l = path_bar->button_list; l; l = l->next)
1147     {
1148       GtkWidget *button = BUTTON_DATA (l->data)->button;
1149       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)))
1150         {
1151           if (l->prev)
1152             {
1153               GtkWidget *prev_button = BUTTON_DATA (l->prev->data)->button;
1154               button_clicked_cb (prev_button, l->prev->data);
1155             }
1156           break;
1157         }
1158     }
1159 }