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