* src/hildon-bread-crumb-trail.c: * src/hildon-bread-crumb-trail.h: Include hildon...
[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 };
51
52 /* Signals */
53
54 enum {
55   CRUMB_CLICKED,
56   LAST_SIGNAL
57 };
58
59 /* Properties */
60
61 enum {
62   PROP_0
63 };
64
65 static void hildon_bread_crumb_trail_size_request (GtkWidget *widget,
66                                                    GtkRequisition *requisition);
67 static void hildon_bread_crumb_trail_size_allocate (GtkWidget *widget,
68                                                     GtkAllocation *allocation);
69 static void hildon_bread_crumb_trail_add (GtkContainer *container,
70                                           GtkWidget *widget);
71 static void hildon_bread_crumb_trail_forall (GtkContainer *container,
72                                              gboolean include_internals,
73                                              GtkCallback callback,
74                                              gpointer callback_data);
75 static void hildon_bread_crumb_trail_remove (GtkContainer *container,
76                                              GtkWidget *widget);
77 static void hildon_bread_crumb_trail_finalize (GObject *object);
78 static void hildon_bread_crumb_trail_scroll_back (GtkWidget *button,
79                                                   HildonBreadCrumbTrail *bct);
80 static void hildon_bread_crumb_trail_update_back_button_sensitivity (HildonBreadCrumbTrail *bct);
81
82 static guint bread_crumb_trail_signals[LAST_SIGNAL] = { 0 };
83
84 /* GType methods */
85
86 G_DEFINE_TYPE (HildonBreadCrumbTrail, hildon_bread_crumb_trail, GTK_TYPE_CONTAINER)
87
88 static void
89 hildon_bread_crumb_trail_class_init (HildonBreadCrumbTrailClass *klass)
90 {
91   GObjectClass *gobject_class = (GObjectClass*)klass;
92   GtkObjectClass *object_class = (GtkObjectClass*)klass;
93   GtkWidgetClass *widget_class = (GtkWidgetClass*)klass;
94   GtkContainerClass *container_class = (GtkContainerClass*)klass;
95
96   /* GObject signals */
97   gobject_class->finalize = hildon_bread_crumb_trail_finalize;
98
99   /* GtkWidget signals */
100   widget_class->size_request = hildon_bread_crumb_trail_size_request;
101   widget_class->size_allocate = hildon_bread_crumb_trail_size_allocate;
102
103   /* GtkContainer signals */
104   container_class->add = hildon_bread_crumb_trail_add;
105   container_class->forall = hildon_bread_crumb_trail_forall;
106   container_class->remove = hildon_bread_crumb_trail_remove;
107
108   /* Style properties */
109
110 #define _BREAD_CRUMB_TRAIL_MINIMUM_WIDTH 10
111
112   /* FIXME: is this the best way to do it? */
113   gtk_widget_class_install_style_property (widget_class,
114                                            g_param_spec_int ("minimum-width",
115                                                              "Minimum width",
116                                                              "The minimum width in characters the children widgets will request",
117                                                              0,
118                                                              G_MAXINT,
119                                                              _BREAD_CRUMB_TRAIL_MINIMUM_WIDTH,
120                                                              G_PARAM_READABLE));
121
122 #define _BREAD_CRUMB_TRAIL_ARROW_SIZE 34
123
124   gtk_widget_class_install_style_property (widget_class,
125                                            g_param_spec_int ("arrow-size",
126                                                              "Arrow size",
127                                                              "Size of the back button arrow",
128                                                              0,
129                                                              G_MAXINT,
130                                                              _BREAD_CRUMB_TRAIL_ARROW_SIZE,
131                                                              G_PARAM_READABLE));
132   /* Signals */
133   bread_crumb_trail_signals[CRUMB_CLICKED] =
134     g_signal_new ("crumb-clicked",
135                   G_OBJECT_CLASS_TYPE (object_class),
136                   G_SIGNAL_RUN_LAST,
137                   G_STRUCT_OFFSET (HildonBreadCrumbTrailClass, crumb_clicked),
138                   g_signal_accumulator_true_handled, NULL,
139                   _hildon_marshal_BOOLEAN__POINTER,
140                   G_TYPE_BOOLEAN, 1,
141                   G_TYPE_POINTER);
142                   
143   /* Private data */
144   g_type_class_add_private (gobject_class, sizeof (HildonBreadCrumbTrailPrivate));
145 }
146
147 static void
148 hildon_bread_crumb_trail_finalize (GObject *object)
149 {
150   HildonBreadCrumbTrailPrivate *priv = HILDON_BREAD_CRUMB_TRAIL (object)->priv;
151
152   g_list_free (priv->item_list);
153
154   G_OBJECT_CLASS (hildon_bread_crumb_trail_parent_class)->finalize (object);
155 }
156
157 static void
158 hildon_bread_crumb_trail_size_request (GtkWidget *widget,
159                                        GtkRequisition *requisition)
160 {
161   GList *p;
162   GtkRequisition child_requisition;
163   HildonBreadCrumbTrail *bct;
164   HildonBreadCrumbTrailPrivate *priv;
165   gint minimum_width, width = 0;
166   PangoLayout *layout;
167   gchar *tmp = NULL;
168
169   bct= HILDON_BREAD_CRUMB_TRAIL (widget);
170   priv = bct->priv;
171
172   requisition->height = 0;
173   requisition->width = 0;
174
175   gtk_widget_size_request (priv->back_button, &child_requisition);
176   requisition->width = child_requisition.width;
177   requisition->height = child_requisition.height;
178
179   if (priv->item_list)
180     {
181       /* Add minimum width for one item */
182       /* TODO: this can be probably cached */
183       gtk_widget_style_get (widget,
184                             "minimum-width", &minimum_width,
185                             NULL);
186
187       tmp = g_strnfill ((gsize)minimum_width, 'm');
188       layout = gtk_widget_create_pango_layout (widget, tmp);
189       g_free (tmp);
190       pango_layout_get_size (layout, &width, NULL);
191       requisition->width += PANGO_PIXELS (width);
192       g_object_unref (layout);
193     }
194
195   /* Button requisitions */
196   for (p = priv->item_list; p; p = p->next)
197     {
198       GtkWidget *child = GTK_WIDGET (p->data);
199
200       if (GTK_WIDGET_VISIBLE (child))
201         gtk_widget_size_request (child, &child_requisition);
202     }
203
204   /* Border width */
205   requisition->width += GTK_CONTAINER (widget)->border_width * 2;
206   requisition->height += GTK_CONTAINER (widget)->border_width * 2;
207
208   widget->requisition = *requisition;
209 }
210
211 /* Document me please */
212
213 static void
214 hildon_bread_crumb_trail_size_allocate (GtkWidget *widget,
215                                         GtkAllocation *allocation)
216 {
217   GtkRequisition req;
218   gint natural_width, natural_height;
219   HildonBreadCrumb *item;
220   GtkAllocation child_allocation;
221   GtkRequisition child_requisition;
222   GtkWidget *child;
223   gint allocation_width;
224   gint border_width, width;
225   gint extra_space;
226   GList *p, *first_show, *first_hide;
227   HildonBreadCrumbTrailPrivate *priv = HILDON_BREAD_CRUMB_TRAIL (widget)->priv;
228
229   widget->allocation = *allocation;
230
231   border_width = (gint) GTK_CONTAINER (widget)->border_width;
232   allocation_width = allocation->width - 2 * border_width;
233
234   /* Allocate the back button */
235   child_allocation.x = allocation->x + border_width;
236   child_allocation.y = allocation->y + border_width;
237   gtk_widget_get_child_requisition (priv->back_button, &child_requisition);
238   child_allocation.width = child_requisition.width;
239   child_allocation.height = child_requisition.height;
240   gtk_widget_size_allocate (priv->back_button, &child_allocation);
241   child_allocation.x += child_allocation.width;
242
243   /* If there are no buttons there's nothing else to do */
244   if (priv->item_list == NULL)
245     return;
246
247   /* We find out how many buttons can we show, starting from the
248      the last one in the logical path (the first item in the list) */
249
250   width = child_allocation.width;
251   p = priv->item_list;
252   first_show = NULL;
253   first_hide = NULL; 
254   extra_space = 0;
255
256   for (p = priv->item_list; p; p = p->next)
257     {
258       item = HILDON_BREAD_CRUMB (p->data);
259       child = GTK_WIDGET (item);
260
261       /* Does the widget fit with its natural size? */
262       hildon_bread_crumb_get_natural_size (item,
263                                            &natural_width,
264                                            &natural_height);
265
266       if (width + natural_width <= allocation_width)
267         {
268           /* Yes, it does */
269           first_show = p;
270           first_hide = p->next;
271           width += natural_width;
272         }
273       else
274         {
275           /* No, it doesn't. Allocate as much as possible
276              and stop */
277           child_allocation.width = allocation_width - width;
278
279           gtk_widget_size_request (child, &req);
280
281           if (child_allocation.width > req.width)
282             {
283               first_hide = p->next;
284               gtk_widget_size_allocate (child, &child_allocation);
285               gtk_widget_show (child);
286               gtk_widget_set_child_visible (child, TRUE);
287               child_allocation.x += child_allocation.width;
288             }
289           else
290             {
291               extra_space = child_allocation.width;
292             }
293
294           break;
295         }
296     }
297
298   /* Not enough items to fill the breadcrumb? */
299   if (p == NULL && width < allocation_width)
300     {
301       extra_space = allocation_width - width;
302     }
303
304   /* Allocate the other buttons */
305   for (p = first_show; p; p = p->prev)
306     {
307       item = HILDON_BREAD_CRUMB (p->data);
308       child = GTK_WIDGET (item);
309
310       /* Does the widget fit with its natural size? */
311       hildon_bread_crumb_get_natural_size (item,
312                                            &natural_width,
313                                            &natural_height);
314
315       /* If I'm the last and there's extra space, use it */
316       if (p->prev == NULL && extra_space != 0)
317         {
318           natural_width += extra_space;
319         }
320
321       child_allocation.width = natural_width;
322       gtk_widget_size_allocate (child, &child_allocation);
323       gtk_widget_show (child);
324       gtk_widget_set_child_visible (child, TRUE);
325       child_allocation.x += child_allocation.width;
326     }
327
328   for (p = first_hide; p; p = p->next)
329     {
330       item = HILDON_BREAD_CRUMB (p->data);
331       child = GTK_WIDGET (item);
332
333       gtk_widget_hide (GTK_WIDGET (item));
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 == 0 || 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   gtk_widget_style_get (GTK_WIDGET (bct),
489                         "arrow-size", &arrow_size,
490                         NULL);
491   gtk_widget_set_size_request (arrow, arrow_size, arrow_size);
492
493   gtk_container_add (GTK_CONTAINER (button), arrow);
494   gtk_container_add (GTK_CONTAINER (bct), button);
495   gtk_widget_show_all (button);
496
497   gtk_widget_pop_composite_child ();
498
499   return button;
500 }
501
502 static void
503 hildon_bread_crumb_trail_init (HildonBreadCrumbTrail *bct)
504 {
505   HildonBreadCrumbTrailPrivate *priv = HILDON_BREAD_CRUMB_TRAIL_GET_PRIVATE (bct);
506
507   GTK_WIDGET_SET_FLAGS (bct, GTK_NO_WINDOW);
508   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (bct), FALSE);
509
510   bct->priv = priv;
511   priv->item_list = NULL;
512
513   priv->back_button = create_back_button (bct);
514   g_signal_connect (priv->back_button, "clicked",
515                     G_CALLBACK (hildon_bread_crumb_trail_scroll_back),
516                     bct);
517 }
518
519 static void
520 hildon_bread_crumb_trail_scroll_back (GtkWidget *button,
521                                       HildonBreadCrumbTrail *bct)
522 {
523   HildonBreadCrumb *item;
524
525   hildon_bread_crumb_trail_pop (bct);
526
527   item = HILDON_BREAD_CRUMB (bct->priv->item_list->data);
528
529   g_signal_emit (bct, bread_crumb_trail_signals[CRUMB_CLICKED], 0,
530                  get_bread_crumb_id (item));
531 }
532
533 static void
534 attach_bread_crumb (HildonBreadCrumbTrail *bct,
535                     GtkWidget *bread_crumb,
536                     gpointer id,
537                     GDestroyNotify destroy)
538 {
539   g_object_set_data_full (G_OBJECT (bread_crumb), "bread-crumb-id", id, destroy);
540
541   gtk_container_add (GTK_CONTAINER (bct), bread_crumb);
542
543   gtk_widget_show (bread_crumb);
544 }
545
546 /* PUBLIC API */
547
548 /**
549  * hildon_bread_crumb_trail_new:
550  * 
551  * Creates a new #HildonBreadCrumbTrail widget.
552  *
553  * Returns: a #GtkWidget pointer of newly created bread crumb trail
554  * widget
555  *
556  * Stability: Unstable
557  */
558
559 GtkWidget*
560 hildon_bread_crumb_trail_new (void)
561 {
562   return GTK_WIDGET (g_object_new (HILDON_TYPE_BREAD_CRUMB_TRAIL, NULL));
563 }
564
565 /**
566  * hildon_bread_crumb_trail_push:
567  * @bct: pointer to #HildonBreadCrumbTrail
568  * @item: the #HildonBreadCrumb to be added to the trail
569  * @id: optional id for the bread crumb
570  * @destroy: GDestroyNotify callback to be called when the bread crumb is destroyed
571  *
572  * Adds a new bread crumb to the end of the trail.
573  *
574  * Stability: Unstable
575  */
576
577 void
578 hildon_bread_crumb_trail_push (HildonBreadCrumbTrail *bct,
579                                HildonBreadCrumb *item,
580                                gpointer id,
581                                GDestroyNotify destroy)
582 {
583   GtkWidget *widget;
584
585   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
586   g_return_if_fail (HILDON_IS_BREAD_CRUMB (item));
587
588   widget = GTK_WIDGET (item);
589
590   attach_bread_crumb (bct, widget, id, destroy);
591 }
592
593 /**
594  * hildon_bread_crumb_trail_push_text:
595  * @bct: pointer to #HildonBreadCrumbTrail
596  * @text: content of the new bread crumb
597  * @id: optional id for the bread crumb
598  * @destroy: GDestroyNotify callback to be called when the bread crumb is destroyed
599  *
600  * Adds a new bread crumb to the end of the trail containing the specified text.
601  *
602  * Stability: Unstable
603  */
604
605 void
606 hildon_bread_crumb_trail_push_text (HildonBreadCrumbTrail *bct,
607                                     const gchar *text,
608                                     gpointer id,
609                                     GDestroyNotify destroy)
610 {
611   GtkWidget *widget;
612
613   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
614   g_return_if_fail (text != NULL);
615
616   widget = _hildon_bread_crumb_widget_new_with_text (text);
617   attach_bread_crumb (bct, widget, id, destroy);
618 }
619
620 /**
621  * hildon_bread_crumb_trail_push_text:
622  * @bct: pointer to #HildonBreadCrumbTrail
623  * @text: content of the new bread crumb
624  * @icon: a widget to set as the icon in the bread crumb
625  * @id: optional id for the bread crumb
626  * @destroy: GDestroyNotify callback to be called when the bread crumb is destroyed
627  *
628  * Adds a new bread crumb to the end of the trail containing the specified text and
629  * icon.
630  *
631  * Stability: Unstable
632  */
633
634 void
635 hildon_bread_crumb_trail_push_icon (HildonBreadCrumbTrail *bct,
636                                     const gchar *text,
637                                     GtkWidget *icon,
638                                     gpointer id,
639                                     GDestroyNotify destroy)
640 {
641   GtkWidget *widget;
642
643   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
644   g_return_if_fail (text != NULL);
645   g_return_if_fail (GTK_IS_WIDGET (icon));
646
647   widget = _hildon_bread_crumb_widget_new_with_icon (icon, text);
648   attach_bread_crumb (bct, widget, id, destroy);
649 }
650
651 /**
652  * hildon_bread_crumb_trail_pop:
653  * @bct: pointer to #HildonBreadCrumbTrail
654  *
655  * Removes the last bread crumb from the trail.
656  *
657  * Stability: Unstable
658  */
659
660 void
661 hildon_bread_crumb_trail_pop (HildonBreadCrumbTrail *bct)
662 {
663   GtkWidget *child;
664   HildonBreadCrumbTrailPrivate *priv;
665
666   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
667
668   priv = bct->priv;
669
670   if (priv->item_list == NULL)
671     return;
672
673   if (priv->item_list)
674     {
675       child = GTK_WIDGET (priv->item_list->data);
676       gtk_container_remove (GTK_CONTAINER (bct), child);
677     }
678
679   hildon_bread_crumb_trail_update_back_button_sensitivity (bct);
680 }
681
682 /**
683  * hildon_bread_crumb_trail_clear:
684  * @bct: pointer to #HildonBreadCrumbTrail
685  *
686  * Removes all the bread crumbs from the bread crumb trail.
687  *
688  * Stability: Unstable
689  */
690
691 void
692 hildon_bread_crumb_trail_clear (HildonBreadCrumbTrail *bct)
693 {
694   HildonBreadCrumbTrailPrivate *priv;
695
696   g_return_if_fail (HILDON_IS_BREAD_CRUMB_TRAIL (bct));
697
698   priv = bct->priv;
699
700   while (priv->item_list)
701     {
702       hildon_bread_crumb_trail_pop (bct);
703     }
704 }