* src/hildon-bread-crumb-trail.c: Use set_child_visible only to control the visibilit...
[hildon] / src / hildon-bread-crumb-trail.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2007 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
7  * Author: Xan Lopez <xan.lopez@nokia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  *
24  */
25
26
27 /**
28  * SECTION:hildon-bread-crumb-trail
29  * @short_description: Widget used to represent a specific path in a hierarchical tree.
30  * Stability: Unstable
31  *
32  * HildonBreadCrumbTrail is a GTK widget used to represent the currently active path in
33  * some kind of hierarchical structure (file system, media library, structured document, etc).
34  *
35  * It has built-in support for text and icon bread crumbs, but the trail only requires a very
36  * simple interface to be implemented for its children and thus new types of items can be
37  * implemented if needed. See #HildonBreadCrumb for more details.
38  */
39
40 #include "hildon-marshalers.h"
41 #include "hildon-bread-crumb-trail.h"
42 #include "hildon-bread-crumb-widget.h"
43
44 #define HILDON_BREAD_CRUMB_TRAIL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HILDON_TYPE_BREAD_CRUMB_TRAIL, HildonBreadCrumbTrailPrivate))
45
46 struct _HildonBreadCrumbTrailPrivate
47 {
48   GtkWidget *back_button;
49   GList *item_list;
50   GtkWidget *arrow;
51 };
52
53 /* Signals */
54
55 enum {
56   CRUMB_CLICKED,
57   LAST_SIGNAL
58 };
59
60 /* Properties */
61
62 enum {
63   PROP_0
64 };
65
66 static void hildon_bread_crumb_trail_size_request (GtkWidget *widget,
67                                                    GtkRequisition *requisition);
68 static void hildon_bread_crumb_trail_size_allocate (GtkWidget *widget,
69                                                     GtkAllocation *allocation);
70 static void hildon_bread_crumb_trail_add (GtkContainer *container,
71                                           GtkWidget *widget);
72 static void hildon_bread_crumb_trail_forall (GtkContainer *container,
73                                              gboolean include_internals,
74                                              GtkCallback callback,
75                                              gpointer callback_data);
76 static void hildon_bread_crumb_trail_remove (GtkContainer *container,
77                                              GtkWidget *widget);
78 static void hildon_bread_crumb_trail_finalize (GObject *object);
79 static void hildon_bread_crumb_trail_scroll_back (GtkWidget *button,
80                                                   HildonBreadCrumbTrail *bct);
81 static void hildon_bread_crumb_trail_update_back_button_sensitivity (HildonBreadCrumbTrail *bct);
82
83 static guint bread_crumb_trail_signals[LAST_SIGNAL] = { 0 };
84
85 /* GType methods */
86
87 G_DEFINE_TYPE (HildonBreadCrumbTrail, hildon_bread_crumb_trail, GTK_TYPE_CONTAINER)
88
89 static void
90 hildon_bread_crumb_trail_class_init (HildonBreadCrumbTrailClass *klass)
91 {
92   GObjectClass *gobject_class = (GObjectClass*)klass;
93   GtkObjectClass *object_class = (GtkObjectClass*)klass;
94   GtkWidgetClass *widget_class = (GtkWidgetClass*)klass;
95   GtkContainerClass *container_class = (GtkContainerClass*)klass;
96
97   /* GObject signals */
98   gobject_class->finalize = hildon_bread_crumb_trail_finalize;
99
100   /* GtkWidget signals */
101   widget_class->size_request = hildon_bread_crumb_trail_size_request;
102   widget_class->size_allocate = hildon_bread_crumb_trail_size_allocate;
103
104   /* GtkContainer signals */
105   container_class->add = hildon_bread_crumb_trail_add;
106   container_class->forall = hildon_bread_crumb_trail_forall;
107   container_class->remove = hildon_bread_crumb_trail_remove;
108
109   /* Style properties */
110
111 #define _BREAD_CRUMB_TRAIL_MINIMUM_WIDTH 10
112
113   /* FIXME: is this the best way to do it? */
114   gtk_widget_class_install_style_property (widget_class,
115                                            g_param_spec_int ("minimum-width",
116                                                              "Minimum width",
117                                                              "The minimum width in characters the children widgets will request",
118                                                              0,
119                                                              G_MAXINT,
120                                                              _BREAD_CRUMB_TRAIL_MINIMUM_WIDTH,
121                                                              G_PARAM_READABLE));
122
123 #define _BREAD_CRUMB_TRAIL_ARROW_SIZE 34
124
125   gtk_widget_class_install_style_property (widget_class,
126                                            g_param_spec_int ("arrow-size",
127                                                              "Arrow size",
128                                                              "Size of the back button arrow",
129                                                              0,
130                                                              G_MAXINT,
131                                                              _BREAD_CRUMB_TRAIL_ARROW_SIZE,
132                                                              G_PARAM_READABLE));
133   /* Signals */
134   bread_crumb_trail_signals[CRUMB_CLICKED] =
135     g_signal_new ("crumb-clicked",
136                   G_OBJECT_CLASS_TYPE (object_class),
137                   G_SIGNAL_RUN_LAST,
138                   G_STRUCT_OFFSET (HildonBreadCrumbTrailClass, crumb_clicked),
139                   g_signal_accumulator_true_handled, NULL,
140                   _hildon_marshal_BOOLEAN__POINTER,
141                   G_TYPE_BOOLEAN, 1,
142                   G_TYPE_POINTER);
143                   
144   /* Private data */
145   g_type_class_add_private (gobject_class, sizeof (HildonBreadCrumbTrailPrivate));
146 }
147
148 static void
149 hildon_bread_crumb_trail_finalize (GObject *object)
150 {
151   HildonBreadCrumbTrailPrivate *priv = HILDON_BREAD_CRUMB_TRAIL (object)->priv;
152
153   g_list_free (priv->item_list);
154
155   G_OBJECT_CLASS (hildon_bread_crumb_trail_parent_class)->finalize (object);
156 }
157
158 static void
159 hildon_bread_crumb_trail_size_request (GtkWidget *widget,
160                                        GtkRequisition *requisition)
161 {
162   GList *p;
163   GtkRequisition child_requisition;
164   HildonBreadCrumbTrail *bct;
165   HildonBreadCrumbTrailPrivate *priv;
166   gint minimum_width, width = 0;
167   PangoLayout *layout;
168   gchar *tmp = NULL;
169
170   bct= HILDON_BREAD_CRUMB_TRAIL (widget);
171   priv = bct->priv;
172
173   requisition->height = 0;
174   requisition->width = 0;
175
176   gtk_widget_size_request (priv->back_button, &child_requisition);
177   requisition->width = child_requisition.width;
178   requisition->height = child_requisition.height;
179
180   if (priv->item_list)
181     {
182       /* Add minimum width for one item */
183       /* TODO: this can be probably cached */
184       gtk_widget_style_get (widget,
185                             "minimum-width", &minimum_width,
186                             NULL);
187
188       tmp = g_strnfill ((gsize)minimum_width, 'm');
189       layout = gtk_widget_create_pango_layout (widget, tmp);
190       g_free (tmp);
191       pango_layout_get_size (layout, &width, NULL);
192       requisition->width += PANGO_PIXELS (width);
193       g_object_unref (layout);
194     }
195
196   /* Button requisitions */
197   for (p = priv->item_list; p; p = p->next)
198     {
199       GtkWidget *child = GTK_WIDGET (p->data);
200
201       if (GTK_WIDGET_VISIBLE (child))
202         gtk_widget_size_request (child, &child_requisition);
203     }
204
205   /* Border width */
206   requisition->width += GTK_CONTAINER (widget)->border_width * 2;
207   requisition->height += GTK_CONTAINER (widget)->border_width * 2;
208
209   widget->requisition = *requisition;
210 }
211
212 /* Document me please */
213
214 static void
215 hildon_bread_crumb_trail_size_allocate (GtkWidget *widget,
216                                         GtkAllocation *allocation)
217 {
218   GtkRequisition req;
219   gint natural_width, natural_height;
220   HildonBreadCrumb *item;
221   GtkAllocation child_allocation;
222   GtkRequisition child_requisition;
223   GtkWidget *child;
224   gint allocation_width;
225   gint border_width, width;
226   gint extra_space;
227   GList *p, *first_show, *first_hide;
228   gint back_button_size;
229   HildonBreadCrumbTrailPrivate *priv = HILDON_BREAD_CRUMB_TRAIL (widget)->priv;
230
231   widget->allocation = *allocation;
232
233   border_width = (gint) GTK_CONTAINER (widget)->border_width;
234   allocation_width = allocation->width - 2 * border_width;
235
236   /* Allocate the back button */
237   child_allocation.x = allocation->x + border_width;
238   child_allocation.y = allocation->y + border_width;
239   gtk_widget_get_child_requisition (priv->back_button, &child_requisition);
240   /* We want the back button to be a square */
241   back_button_size = MAX (child_requisition.width, child_requisition.height);
242   child_allocation.width = child_allocation.height = back_button_size;
243   gtk_widget_size_allocate (priv->back_button, &child_allocation);
244   child_allocation.x += back_button_size;
245
246   /* If there are no buttons there's nothing else to do */
247   if (priv->item_list == NULL)
248     return;
249
250   /* We find out how many buttons can we show, starting from the
251      the last one in the logical path (the first item in the list) */
252
253   width = back_button_size;
254   p = priv->item_list;
255   first_show = NULL;
256   first_hide = NULL; 
257   extra_space = 0;
258
259   for (p = priv->item_list; p; p = p->next)
260     {
261       item = HILDON_BREAD_CRUMB (p->data);
262       child = GTK_WIDGET (item);
263
264       /* Does the widget fit with its natural size? */
265       hildon_bread_crumb_get_natural_size (item,
266                                            &natural_width,
267                                            &natural_height);
268
269       if (width + natural_width <= allocation_width)
270         {
271           /* Yes, it does */
272           first_show = p;
273           first_hide = p->next;
274           width += natural_width;
275         }
276       else
277         {
278           /* No, it doesn't. Allocate as much as possible
279              and stop */
280           child_allocation.width = allocation_width - width;
281
282           gtk_widget_size_request (child, &req);
283
284           if (child_allocation.width > req.width)
285             {
286               first_hide = p->next;
287               gtk_widget_size_allocate (child, &child_allocation);
288               gtk_widget_set_child_visible (child, TRUE);
289               child_allocation.x += child_allocation.width;
290             }
291           else
292             {
293               extra_space = child_allocation.width;
294             }
295
296           break;
297         }
298     }
299
300   /* Not enough items to fill the breadcrumb? */
301   if (p == NULL && width < allocation_width)
302     {
303       extra_space = allocation_width - width;
304     }
305
306   /* Allocate the other buttons */
307   for (p = first_show; p; p = p->prev)
308     {
309       item = HILDON_BREAD_CRUMB (p->data);
310       child = GTK_WIDGET (item);
311
312       /* Does the widget fit with its natural size? */
313       hildon_bread_crumb_get_natural_size (item,
314                                            &natural_width,
315                                            &natural_height);
316
317       /* If I'm the last and there's extra space, use it */
318       if (p->prev == NULL && extra_space != 0)
319         {
320           natural_width += extra_space;
321         }
322
323       child_allocation.width = natural_width;
324       gtk_widget_size_allocate (child, &child_allocation);
325       gtk_widget_set_child_visible (child, TRUE);
326       child_allocation.x += child_allocation.width;
327     }
328
329   for (p = first_hide; p; p = p->next)
330     {
331       item = HILDON_BREAD_CRUMB (p->data);
332       child = GTK_WIDGET (item);
333
334       gtk_widget_set_child_visible (GTK_WIDGET (item), FALSE);
335     }
336 }
337
338 static gpointer
339 get_bread_crumb_id (HildonBreadCrumb *item)
340 {
341   return g_object_get_data (G_OBJECT (item), "bread-crumb-id");
342 }
343
344 static void
345 crumb_activated_cb (GtkWidget *button,
346                     HildonBreadCrumbTrail *bct)
347 {
348   gboolean signal_handled = FALSE;
349
350   g_signal_emit (bct, bread_crumb_trail_signals[CRUMB_CLICKED], 0,
351                  get_bread_crumb_id (HILDON_BREAD_CRUMB (button)),
352                  &signal_handled);
353
354   if (signal_handled == FALSE)
355     {
356       GtkWidget *child;
357       HildonBreadCrumbTrailPrivate *priv;
358
359       priv = bct->priv;
360
361       child = GTK_WIDGET (priv->item_list->data);
362
363       /* We remove the tip of the list until we hit the clicked button */
364       while (child != button)
365         {
366           gtk_container_remove (GTK_CONTAINER (bct), child);
367
368           child = GTK_WIDGET (priv->item_list->data);
369         }
370     }
371 }
372
373 static void
374 hildon_bread_crumb_trail_add (GtkContainer *container,
375                               GtkWidget *widget)
376 {
377   gtk_widget_set_parent (widget, GTK_WIDGET (container));
378
379   if (HILDON_IS_BREAD_CRUMB (widget))
380     {
381       HildonBreadCrumbTrail *bct = HILDON_BREAD_CRUMB_TRAIL (container);
382
383       g_signal_connect (G_OBJECT (widget), "crumb-activated",
384                         G_CALLBACK (crumb_activated_cb), container);
385
386       bct->priv->item_list = g_list_prepend (bct->priv->item_list, widget);
387
388       hildon_bread_crumb_trail_update_back_button_sensitivity (bct);
389     }
390 }
391
392 static void
393 hildon_bread_crumb_trail_forall (GtkContainer *container,
394                                  gboolean include_internals,
395                                  GtkCallback callback,
396                                  gpointer callback_data)
397 {
398   g_return_if_fail (callback != NULL);
399   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (container));
400
401   GList *children;
402   HildonBreadCrumbTrailPrivate *priv = HILDON_BREAD_CRUMB_TRAIL (container)->priv;
403
404   children = priv->item_list;
405
406   while (children)
407     {
408       GtkWidget *child;
409       child = GTK_WIDGET (children->data);
410       children = children->next;
411
412       (*callback) (child, callback_data);
413     }
414
415   if (include_internals && priv->back_button)
416     {
417       (*callback) (priv->back_button, callback_data);
418     }
419 }
420
421 static void
422 hildon_bread_crumb_trail_remove (GtkContainer *container,
423                                  GtkWidget *widget)
424 {
425   GList *p;
426   HildonBreadCrumbTrailPrivate *priv;
427   gboolean was_visible = GTK_WIDGET_VISIBLE (widget);
428
429   priv = HILDON_BREAD_CRUMB_TRAIL (container)->priv;
430
431   p = priv->item_list;
432
433   while (p)
434     {
435       if (widget == GTK_WIDGET (p->data))
436         {
437           g_signal_handlers_disconnect_by_func (widget, G_CALLBACK (crumb_activated_cb),
438                                                 HILDON_BREAD_CRUMB_TRAIL (container));
439           gtk_widget_unparent (widget);
440
441           priv->item_list = g_list_remove_link (priv->item_list, p);
442           g_list_free (p);
443
444           hildon_bread_crumb_trail_update_back_button_sensitivity (HILDON_BREAD_CRUMB_TRAIL (container));
445
446           if (was_visible)
447             {
448               gtk_widget_queue_resize (GTK_WIDGET (container));
449             }
450         }
451
452       p = p->next;
453     }
454 }
455
456 static void
457 hildon_bread_crumb_trail_update_back_button_sensitivity (HildonBreadCrumbTrail *bct)
458 {
459   guint list_length;
460   HildonBreadCrumbTrailPrivate *priv = bct->priv;
461
462   list_length = g_list_length (priv->item_list);
463
464   if (list_length <= 1)
465     {
466       gtk_widget_set_sensitive (priv->back_button, FALSE);
467     }
468   else
469     {
470       gtk_widget_set_sensitive (priv->back_button, TRUE);
471     }
472 }
473
474 static GtkWidget*
475 create_back_button (HildonBreadCrumbTrail *bct)
476 {
477   GtkWidget *button;
478   GtkWidget *arrow;
479   gint arrow_size;
480
481   gtk_widget_push_composite_child ();
482
483   button = gtk_button_new ();
484   gtk_widget_set_name (button, "hildon-bread-crumb-back-button");
485   gtk_widget_set_sensitive (button, FALSE);
486
487   arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
488   bct->priv->arrow = arrow;
489   gtk_widget_style_get (GTK_WIDGET (bct),
490                         "arrow-size", &arrow_size,
491                         NULL);
492   gtk_widget_set_size_request (arrow, arrow_size, arrow_size);
493
494   gtk_container_add (GTK_CONTAINER (button), arrow);
495   gtk_container_add (GTK_CONTAINER (bct), button);
496   gtk_widget_show_all (button);
497
498   gtk_widget_pop_composite_child ();
499
500   return button;
501 }
502
503 static void
504 hildon_bread_crumb_trail_init (HildonBreadCrumbTrail *bct)
505 {
506   HildonBreadCrumbTrailPrivate *priv = HILDON_BREAD_CRUMB_TRAIL_GET_PRIVATE (bct);
507
508   GTK_WIDGET_SET_FLAGS (bct, GTK_NO_WINDOW);
509   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (bct), FALSE);
510
511   bct->priv = priv;
512   priv->item_list = NULL;
513
514   priv->back_button = create_back_button (bct);
515   g_signal_connect (priv->back_button, "clicked",
516                     G_CALLBACK (hildon_bread_crumb_trail_scroll_back),
517                     bct);
518 }
519
520 static void
521 hildon_bread_crumb_trail_scroll_back (GtkWidget *button,
522                                       HildonBreadCrumbTrail *bct)
523 {
524   HildonBreadCrumb *item;
525
526   hildon_bread_crumb_trail_pop (bct);
527
528   item = HILDON_BREAD_CRUMB (bct->priv->item_list->data);
529
530   g_signal_emit (bct, bread_crumb_trail_signals[CRUMB_CLICKED], 0,
531                  get_bread_crumb_id (item));
532 }
533
534 static void
535 attach_bread_crumb (HildonBreadCrumbTrail *bct,
536                     GtkWidget *bread_crumb,
537                     gpointer id,
538                     GDestroyNotify destroy)
539 {
540   g_object_set_data_full (G_OBJECT (bread_crumb), "bread-crumb-id", id, destroy);
541
542   gtk_container_add (GTK_CONTAINER (bct), bread_crumb);
543
544   gtk_widget_show (bread_crumb);
545 }
546
547 /* PUBLIC API */
548
549 /**
550  * hildon_bread_crumb_trail_new:
551  * 
552  * Creates a new #HildonBreadCrumbTrail widget.
553  *
554  * Returns: a #GtkWidget pointer of newly created bread crumb trail
555  * widget
556  *
557  * Stability: Unstable
558  */
559
560 GtkWidget*
561 hildon_bread_crumb_trail_new (void)
562 {
563   return GTK_WIDGET (g_object_new (HILDON_TYPE_BREAD_CRUMB_TRAIL, NULL));
564 }
565
566 /**
567  * hildon_bread_crumb_trail_push:
568  * @bct: pointer to #HildonBreadCrumbTrail
569  * @item: the #HildonBreadCrumb to be added to the trail
570  * @id: optional id for the bread crumb
571  * @destroy: GDestroyNotify callback to be called when the bread crumb is destroyed
572  *
573  * Adds a new bread crumb to the end of the trail.
574  *
575  * Stability: Unstable
576  */
577
578 void
579 hildon_bread_crumb_trail_push (HildonBreadCrumbTrail *bct,
580                                HildonBreadCrumb *item,
581                                gpointer id,
582                                GDestroyNotify destroy)
583 {
584   GtkWidget *widget;
585
586   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
587   g_return_if_fail (HILDON_IS_BREAD_CRUMB (item));
588
589   widget = GTK_WIDGET (item);
590
591   attach_bread_crumb (bct, widget, id, destroy);
592 }
593
594 /**
595  * hildon_bread_crumb_trail_push_text:
596  * @bct: pointer to #HildonBreadCrumbTrail
597  * @text: content of the new bread crumb
598  * @id: optional id for the bread crumb
599  * @destroy: GDestroyNotify callback to be called when the bread crumb is destroyed
600  *
601  * Adds a new bread crumb to the end of the trail containing the specified text.
602  *
603  * Stability: Unstable
604  */
605
606 void
607 hildon_bread_crumb_trail_push_text (HildonBreadCrumbTrail *bct,
608                                     const gchar *text,
609                                     gpointer id,
610                                     GDestroyNotify destroy)
611 {
612   GtkWidget *widget;
613
614   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
615   g_return_if_fail (text != NULL);
616
617   widget = _hildon_bread_crumb_widget_new_with_text (text);
618   attach_bread_crumb (bct, widget, id, destroy);
619 }
620
621 /**
622  * hildon_bread_crumb_trail_push_text:
623  * @bct: pointer to #HildonBreadCrumbTrail
624  * @text: content of the new bread crumb
625  * @icon: a widget to set as the icon in the bread crumb
626  * @id: optional id for the bread crumb
627  * @destroy: GDestroyNotify callback to be called when the bread crumb is destroyed
628  *
629  * Adds a new bread crumb to the end of the trail containing the specified text and
630  * icon.
631  *
632  * Stability: Unstable
633  */
634
635 void
636 hildon_bread_crumb_trail_push_icon (HildonBreadCrumbTrail *bct,
637                                     const gchar *text,
638                                     GtkWidget *icon,
639                                     gpointer id,
640                                     GDestroyNotify destroy)
641 {
642   GtkWidget *widget;
643
644   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
645   g_return_if_fail (text != NULL);
646   g_return_if_fail (GTK_IS_WIDGET (icon));
647
648   widget = _hildon_bread_crumb_widget_new_with_icon (icon, text);
649   attach_bread_crumb (bct, widget, id, destroy);
650 }
651
652 /**
653  * hildon_bread_crumb_trail_pop:
654  * @bct: pointer to #HildonBreadCrumbTrail
655  *
656  * Removes the last bread crumb from the trail.
657  *
658  * Stability: Unstable
659  */
660
661 void
662 hildon_bread_crumb_trail_pop (HildonBreadCrumbTrail *bct)
663 {
664   GtkWidget *child;
665   HildonBreadCrumbTrailPrivate *priv;
666
667   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
668
669   priv = bct->priv;
670
671   if (priv->item_list == NULL)
672     return;
673
674   if (priv->item_list)
675     {
676       child = GTK_WIDGET (priv->item_list->data);
677       gtk_container_remove (GTK_CONTAINER (bct), child);
678     }
679
680   hildon_bread_crumb_trail_update_back_button_sensitivity (bct);
681 }
682
683 /**
684  * hildon_bread_crumb_trail_clear:
685  * @bct: pointer to #HildonBreadCrumbTrail
686  *
687  * Removes all the bread crumbs from the bread crumb trail.
688  *
689  * Stability: Unstable
690  */
691
692 void
693 hildon_bread_crumb_trail_clear (HildonBreadCrumbTrail *bct)
694 {
695   HildonBreadCrumbTrailPrivate *priv;
696
697   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
698
699   priv = bct->priv;
700
701   while (priv->item_list)
702     {
703       hildon_bread_crumb_trail_pop (bct);
704     }
705 }