aboutsummaryrefslogtreecommitdiff
path: root/hildon/hildon-caption.c
blob: ed0135591171274c37a4896d9cab5b7e76e6fa86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
/*
 * This file is a part of hildon
 *
 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
 *
 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:hildon-caption
 * @short_description: A single-child container widget that precedes the 
 * contained widget with a field label and an optional icon.
 *
 * #HildonCaption is a single-child container widget that precedes the 
 * contained widget with a field label and an optional icon. It allows 
 * grouping of several controls together. When a captioned widget has focus, 
 * both widget and caption label are displayed with active focus. 
 */

#ifdef                                          HAVE_CONFIG_H
#include                                        <config.h>
#endif

#include                                        <libintl.h>
#include                                        <gtk/gtk.h>

#include                                        "hildon-defines.h"
#include                                        "hildon-caption.h"
#include                                        "hildon-caption-private.h"

#define                                         _(String)\
                                                dgettext("hildon-libs", String)

#define                                         HILDON_CAPTION_SPACING 6

static GtkEventBox*                             parent_class = NULL;

static void 
hildon_caption_class_init                       (HildonCaptionClass *caption_class);

static void
hildon_caption_init                             (HildonCaption *caption);

static void 
hildon_caption_size_request                     (GtkWidget *widget, 
                                                 GtkRequisition *requisition);

static void 
hildon_caption_size_allocate                    (GtkWidget *widget, 
                                                 GtkAllocation *allocation);

static void 
hildon_caption_forall                           (GtkContainer *container,
                                                 gboolean include_internals,
                                                 GtkCallback callback, 
                                                 gpointer data );

static void
hildon_caption_hierarchy_changed                (GtkWidget *widget,
                                                 GtkWidget *previous_toplevel);

static void 
hildon_caption_set_focus                        (GtkWindow *window, 
                                                 GtkWidget *widget,
                                                 GtkWidget *caption);

static void 
hildon_caption_grab_focus                       (GtkWidget *widget);

static void 
hildon_caption_activate                         (GtkWidget *widget);

static void
hildon_caption_set_property                     (GObject *object, 
                                                 guint param_id,
                                                 const GValue *value, 
                                                 GParamSpec *pspec );

static void
hildon_caption_get_property                     (GObject *object, 
                                                 guint param_id,
                                                 GValue *value, 
                                                 GParamSpec *pspec);

static void 
hildon_caption_destroy                          (GtkObject *self);

static gboolean 
hildon_caption_button_press                     (GtkWidget *widget, 
                                                 GdkEventButton *event);

static void
hildon_caption_set_label_text                   (HildonCaptionPrivate *priv, 
                                                 gboolean markup);

static void 
hildon_caption_set_child_property               (GtkContainer *container,
                                                 GtkWidget *child,
                                                 guint property_id,
                                                 const GValue *value,
                                                 GParamSpec *pspec);

static void 
hildon_caption_get_child_property               (GtkContainer *container,
                                                 GtkWidget *child,
                                                 guint property_id,
                                                 GValue *value,
                                                 GParamSpec *pspec);

enum
{
    PROP_0,
    PROP_LABEL,
    PROP_MARKUP,
    PROP_ICON,
    PROP_STATUS,
    PROP_SEPARATOR,
    PROP_SIZE_GROUP,
    PROP_ICON_POSITION,
};

enum
{
    CHILD_PROP_NONE,
    CHILD_PROP_EXPAND
};

/**
 * hildon_caption_get_type:
 *
 * Initializes and returns the type of a hildon caption.
 *
 * Returns: GType of #HildonCaption
 */
GType G_GNUC_CONST 
hildon_caption_get_type                         (void)
{
    static GType caption_type = 0;

    if (!caption_type)
    {
        static const GTypeInfo caption_info = {
            sizeof (HildonCaptionClass),
            NULL,       /* base_init */
            NULL,       /* base_finalize */
            (GClassInitFunc) hildon_caption_class_init,
            NULL,       /* class_finalize */
            NULL,       /* class_data */
            sizeof (HildonCaption),
            0,  /* n_preallocs */
            (GInstanceInitFunc) hildon_caption_init,
        };
        caption_type = g_type_register_static (GTK_TYPE_EVENT_BOX,
                "HildonCaption", &caption_info, 0 );
    }
    return caption_type;
}

/*
 * Initialises the caption class.
 */
static void 
hildon_caption_class_init                       (HildonCaptionClass *caption_class)
{
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (caption_class);
    GObjectClass *gobject_class = G_OBJECT_CLASS (caption_class);
    GtkContainerClass *container_class = GTK_CONTAINER_CLASS (caption_class);

    parent_class = g_type_class_peek_parent (caption_class);

    g_type_class_add_private (caption_class, sizeof (HildonCaptionPrivate));

    /* Override virtual functions */
    gobject_class->get_property                 = hildon_caption_get_property;
    gobject_class->set_property                 = hildon_caption_set_property;
    caption_class->activate                     = (gpointer) hildon_caption_activate;
    GTK_OBJECT_CLASS(caption_class)->destroy    = hildon_caption_destroy;

    container_class->forall                     = hildon_caption_forall;
    container_class->set_child_property         = hildon_caption_set_child_property;
    container_class->get_child_property         = hildon_caption_get_child_property;

    widget_class->hierarchy_changed             = hildon_caption_hierarchy_changed;
    widget_class->size_request                  = hildon_caption_size_request;
    widget_class->size_allocate                 = hildon_caption_size_allocate;
    widget_class->button_press_event            = hildon_caption_button_press;
    widget_class->grab_focus                    = hildon_caption_grab_focus;

    /* Create new signals and properties */
    widget_class->activate_signal = g_signal_new ("activate",
            G_OBJECT_CLASS_TYPE (
                gobject_class),
            G_SIGNAL_RUN_FIRST |
            G_SIGNAL_ACTION,
            G_STRUCT_OFFSET (HildonCaptionClass,
                activate), NULL, NULL,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE, 0);

    /**
     * HildonCaption:label:
     *
     * Caption label.
     */
    g_object_class_install_property (gobject_class, PROP_LABEL,
            g_param_spec_string ("label",
                "Current label", "Caption label",
                NULL, G_PARAM_READABLE | G_PARAM_WRITABLE) );
    
    /**
     * HildonCaption:markup:
     *
     * Caption markup. Mutually exclusive with label.
     */
    g_object_class_install_property (gobject_class, PROP_MARKUP,
            g_param_spec_string ("markup",
                "Current markup", "Caption markup",
                NULL, G_PARAM_WRITABLE) );

    /**
     * HildonCaption:icon:
     *
     * The icon shown on the caption area.
     */
    g_object_class_install_property (gobject_class, PROP_ICON,
            g_param_spec_object ("icon",
                "Current icon",
                "The icon shown on the caption area",
                GTK_TYPE_WIDGET, G_PARAM_READABLE | 
                G_PARAM_WRITABLE) );
    /**
     * HildonCaption:status:
     *
     * Mandatory or optional status.
     */
    g_object_class_install_property (gobject_class, PROP_STATUS,
            g_param_spec_enum ("status",
                "Current status",
                "Mandatory or optional status",
                HILDON_TYPE_CAPTION_STATUS,
                HILDON_CAPTION_OPTIONAL,
                G_PARAM_READABLE | G_PARAM_WRITABLE) );
    /**
     * HildonCaption:icon-position:
     *
     * If the icon is positioned on the left or right side.
     */
    g_object_class_install_property (gobject_class, PROP_ICON_POSITION,
            g_param_spec_enum ("icon-position",
                "Icon position",
                "Whether the icon is on the left or right side",
                HILDON_TYPE_CAPTION_ICON_POSITION,
                HILDON_CAPTION_POSITION_RIGHT,
                G_PARAM_READABLE | G_PARAM_WRITABLE) );

    /**
     * HildonCaption:size_group:
     *
     * Current size group the caption is in.
     */
    g_object_class_install_property (gobject_class, PROP_SIZE_GROUP,
            g_param_spec_object ("size_group",
                "Current size group",
                "Current size group the caption is in",
                GTK_TYPE_SIZE_GROUP, G_PARAM_READABLE | 
                G_PARAM_WRITABLE) );

    /**
     * HildonCaption:separator:
     *
     * The current separator.
     */
    g_object_class_install_property (gobject_class, PROP_SEPARATOR,
            g_param_spec_string ("separator",
                "Current separator", "Current separator",
                _("ecdg_ti_caption_separator"),
                G_PARAM_READABLE | G_PARAM_WRITABLE) );

    /* Create child properties. These are related to
       child <-> parent relationship, not to either of objects itself */
    gtk_container_class_install_child_property (container_class,
            CHILD_PROP_EXPAND,
            g_param_spec_boolean ("expand",
                "Same as GtkBox expand.",
                "Same as GtkBox expand. Wheter the child should be expanded or not.",
                FALSE,
                G_PARAM_READWRITE));
}

/* Destroy can be called multiple times, remember to set pointers to NULL */
static void 
hildon_caption_destroy                          (GtkObject *self)
{
    HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (self);

    /* Free our internal child */
    if (priv && priv->caption_area)
    {
        gtk_widget_unparent (priv->caption_area);
        priv->caption_area = NULL;
    }

    /* Free user provided strings */
    if (priv && priv->text)
    {
        g_free (priv->text);
        priv->text = NULL;
    }

    if (priv && priv->separator)
    {
        g_free (priv->separator);
        priv->separator = NULL;
    }

    /* Parent classes destroy takes care of user packed contents */
    if (GTK_OBJECT_CLASS (parent_class)->destroy)
        GTK_OBJECT_CLASS (parent_class)->destroy (self);
}

static void 
hildon_caption_set_property                     (GObject *object, 
                                                 guint param_id,
                                                 const GValue *value,
                                                 GParamSpec *pspec)
{
    HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (object);

    switch (param_id) 
    {
        case PROP_ICON_POSITION:
            hildon_caption_set_icon_position (HILDON_CAPTION (object), g_value_get_enum (value));
            break;

        case PROP_LABEL:
            /* Free old label string */
            if (priv->text)
            {
                g_free (priv->text);
                priv->text = NULL;
            }

            /* Update label */
            priv->text = g_value_dup_string (value);
            hildon_caption_set_label_text (priv, FALSE);
            break;

        case PROP_MARKUP:
            /* Free old label string */
            if (priv->text)
            {
                g_free (priv->text);
                priv->text = NULL;
            }

            /* Update label */
            priv->text = g_value_dup_string (value);
            hildon_caption_set_label_text (priv, TRUE);
            break;

        case PROP_ICON:
            /* Remove old icon */
            if (priv->icon)
                gtk_container_remove (GTK_CONTAINER (priv->icon_align), priv->icon);

            /* Pack and display new icon */
            priv->icon = g_value_get_object (value);
            if (priv->icon)
            {
                gtk_container_add (GTK_CONTAINER (priv->icon_align), priv->icon);
                gtk_widget_show_all (priv->caption_area);
            }
            break;

        case PROP_STATUS:
            priv->status = g_value_get_enum (value);
            break;

        case PROP_SIZE_GROUP:
            /* Detach from previous size group */
            if (priv->group)
                gtk_size_group_remove_widget (priv->group, priv->caption_area);

            priv->group = g_value_get_object (value);

            /* Attach to new size group */
            if (priv->group)
                gtk_size_group_add_widget (priv->group, priv->caption_area);

            gtk_widget_queue_draw (GTK_WIDGET(object));
            break;

        case PROP_SEPARATOR:

            /* Free old separator */
            if (priv->separator)
            {
                g_free (priv->separator);
                priv->separator = NULL;
            }

            priv->separator = g_value_dup_string (value);
            hildon_caption_set_label_text (priv, FALSE);
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
            break;
    }
}

static void 
hildon_caption_get_property                     (GObject *object, 
                                                 guint param_id,
                                                 GValue *value, 
                                                 GParamSpec *pspec)
{
    HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (object);

    switch (param_id) 
    {
        case PROP_LABEL:
            g_value_set_string (value, priv->text);
            break;

        case PROP_ICON:
            g_value_set_object (value, priv->icon);
            break;

        case PROP_STATUS:
            g_value_set_enum (value, priv->status);
            break;

        case PROP_ICON_POSITION:
            g_value_set_enum (value, priv->icon_position);
            break;

        case PROP_SIZE_GROUP:
            g_value_set_object (value, priv->group);
            break;

        case PROP_SEPARATOR:
            g_value_set_string (value, priv->separator);
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
            break;
    }
}

static void 
hildon_caption_set_child_property               (GtkContainer *container,
                                                 GtkWidget *child,
                                                 guint property_id,
                                                 const GValue *value,
                                                 GParamSpec *pspec)
{
    switch (property_id)
    {
        case CHILD_PROP_EXPAND:
            hildon_caption_set_child_expand (HILDON_CAPTION (container),
                    g_value_get_boolean (value));
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (container, property_id, pspec);
            break;
    }
}

static void 
hildon_caption_get_child_property               (GtkContainer *container,
                                                 GtkWidget *child,
                                                 guint property_id,
                                                 GValue *value,
                                                 GParamSpec *pspec)
{
    switch (property_id)
    {
        case CHILD_PROP_EXPAND:
            g_value_set_boolean (value, hildon_caption_get_child_expand(
                        HILDON_CAPTION (container)));
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID(container, property_id, pspec);
            break;
    }
}

/* We want to activate out child control on button press */
static gboolean
hildon_caption_button_press                     (GtkWidget *widget, 
                                                 GdkEventButton *event)
{
    gtk_widget_grab_focus (GTK_BIN (widget)->child);
    
    /* we'll update our focused state in set-focus when/if the child receives
     * focus */

    return FALSE;
}

static void 
hildon_caption_init                             (HildonCaption *caption)
{
    HildonCaptionPrivate *priv = NULL;

    /* Initialize startup state */
    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    priv->status = HILDON_CAPTION_OPTIONAL;
    priv->icon = NULL;
    priv->group = NULL;
    priv->is_focused = FALSE;
    priv->text = NULL;

    priv->separator = g_strdup(_("ecdg_ti_caption_separator"));

    gtk_widget_push_composite_child();

    /* Create caption text */
    priv->caption_area = gtk_hbox_new (FALSE, HILDON_CAPTION_SPACING); 
    priv->label = gtk_label_new (NULL);
    priv->icon_align = gtk_alignment_new (0.5f, 0.5f, 0.0f, 0.0f);
    priv->icon_position = HILDON_CAPTION_POSITION_RIGHT;

    /* We want to receive button presses for child widget activation */
    gtk_event_box_set_above_child (GTK_EVENT_BOX (caption), FALSE);
    gtk_widget_add_events (GTK_WIDGET (caption), GDK_BUTTON_PRESS_MASK);

    /* Pack text label caption layout */
    gtk_box_pack_end (GTK_BOX (priv->caption_area), priv->icon_align, FALSE, FALSE, 0);
    gtk_box_pack_end (GTK_BOX (priv->caption_area), priv->label, FALSE, FALSE, 0);
    gtk_widget_set_parent (priv->caption_area, GTK_WIDGET (caption));

    gtk_widget_pop_composite_child ();

    hildon_caption_set_child_expand (caption, TRUE);

    gtk_widget_show_all (priv->caption_area);
}

static void 
hildon_caption_set_focus                        (GtkWindow *window, 
                                                 GtkWidget *widget,
                                                 GtkWidget *caption)
{
    HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (caption);

    /* check if ancestor gone */
    if (! widget)
    {
        return;
    }

    /* Try to find caption among the ancestors of widget */
    if (gtk_widget_is_ancestor (widget, caption))
    {
        priv->is_focused = TRUE;
        gtk_widget_queue_draw (caption);
        return;
    }

    if (priv->is_focused == TRUE)
    {
        /* Caption wasn't found, so cannot focus */
        priv->is_focused = FALSE;
        gtk_widget_queue_draw (caption);
    }
}

/* We need to connect/disconnect signal handlers to toplevel window
   in which we reside. Ww want to update connected signals if our
   parent changes */
static void 
hildon_caption_hierarchy_changed                (GtkWidget *widget,
                                                 GtkWidget *previous_toplevel)
{
    GtkWidget *current_ancestor;

    if (GTK_WIDGET_CLASS (parent_class)->hierarchy_changed)
        GTK_WIDGET_CLASS (parent_class)->hierarchy_changed (widget, previous_toplevel);

    /* If we already were inside a window, remove old handler */
    if (previous_toplevel) {
        /* This is a compilation workaround for gcc > 3.3 since glib is buggy */
        /* see http://bugzilla.gnome.org/show_bug.cgi?id=310175 */
#ifdef __GNUC__
        __extension__
#endif
            g_signal_handlers_disconnect_by_func
            (previous_toplevel, (gpointer) hildon_caption_set_focus, widget);
    }

    current_ancestor = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);

    /* Install new handler for focus movement */
    if (current_ancestor)
        g_signal_connect( current_ancestor, "set-focus", 
                G_CALLBACK (hildon_caption_set_focus), widget );
}

static void 
hildon_caption_size_request                     (GtkWidget *widget,
                                                 GtkRequisition *requisition)
{
    GtkRequisition req;
    HildonCaptionPrivate *priv = NULL;
    g_return_if_fail (HILDON_IS_CAPTION(widget));

    priv = HILDON_CAPTION_GET_PRIVATE (widget);

    /* Use the same size requisition for the main box of the caption */
    gtk_widget_size_request (priv->caption_area, &req);

    if (GTK_WIDGET_CLASS (parent_class)->size_request)
        GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);

    requisition->width += req.width + HILDON_CAPTION_SPACING * 3;

    if ((req.height + (2 * widget->style->ythickness)) > requisition->height)
        requisition->height = req.height + (2 * widget->style->ythickness);
}

/* We use HILDON_CAPTION_SPACING to make it look a bit nicer */
static void 
hildon_caption_size_allocate                    (GtkWidget *widget,
                                                 GtkAllocation *allocation)
{
    GtkAllocation child_alloc;
    GtkAllocation caption_alloc;
    GtkRequisition req, child_req = {0};
    GtkWidget *child = NULL;
    HildonCaptionPrivate *priv = NULL;
    gboolean rtl;

    g_assert (HILDON_IS_CAPTION (widget));
    priv = HILDON_CAPTION_GET_PRIVATE (widget);

    /* Get the rtl status */
    rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);

    /* Position the caption to its allocated location */
    if (GTK_WIDGET_REALIZED (widget))
        gdk_window_move_resize (widget->window,
                allocation->x + GTK_CONTAINER (widget)->border_width,
                allocation->y + GTK_CONTAINER (widget)->border_width,
                MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0),
                MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0));

    child = GTK_BIN (widget)->child;
    if (child)
        gtk_widget_get_child_requisition (child, &child_req);

    widget->allocation = *allocation;
    gtk_widget_get_child_requisition (priv->caption_area, &req);

    child_alloc.height = caption_alloc.height = allocation->height;
    child_alloc.width  = caption_alloc.width  = allocation->width;
    child_alloc.x = caption_alloc.x = caption_alloc.y = child_alloc.y = 0;

    /* Center the captioned widget */
    if (rtl)
    {
        if (caption_alloc.width > child_req.width + HILDON_CAPTION_SPACING)
        {
            caption_alloc.x = caption_alloc.width - req.width;
            child_alloc.width = child_req.width;
        }
        caption_alloc.width -= child_req.width + HILDON_CAPTION_SPACING * 2;
    }
    else
    {
        if (child_alloc.width > req.width + HILDON_CAPTION_SPACING)
        {
            child_alloc.x += req.width + HILDON_CAPTION_SPACING * 2;
            caption_alloc.width = req.width;
        }
        /* Leave at least the space of the HILDON_CAPTION_SPACING in the left */
        caption_alloc.x = HILDON_CAPTION_SPACING;

        /* Leave room for the other drawable parts of the caption control */
        child_alloc.width -= req.width + HILDON_CAPTION_SPACING * 2;
    }

    /* Give the child at least its minimum requisition, unless it is expandable */
    if (! priv->expand && child && GTK_WIDGET_VISIBLE(child))
    {
        child_alloc.width  = MIN (child_alloc.width,  child_req.width);
        child_alloc.height = MIN (child_alloc.height, child_req.height);
	/* Center the child */
	child_alloc.y = (allocation->height - child_alloc.height -
			 2 * GTK_CONTAINER (widget)->border_width)/2;
    }

    /* Ensure there are no negative dimensions */
    if (child_alloc.width < 0)
    {
        caption_alloc.width = req.width + child_alloc.width;
        child_alloc.width = 0;
        caption_alloc.width = MAX (caption_alloc.width, 0);
    }

    if (rtl)
        child_alloc.x = caption_alloc.x - child_req.width - HILDON_CAPTION_SPACING * 2;

    child_alloc.height = MAX (child_alloc.height, 0);
    caption_alloc.height = MAX (caption_alloc.height, 0);

    if (child && GTK_WIDGET_VISIBLE(child) )
        gtk_widget_size_allocate (child, &child_alloc );

    gtk_widget_size_allocate (priv->caption_area, &caption_alloc);
}

static void 
hildon_caption_forall                           (GtkContainer *container,
                                                 gboolean include_internals,
                                                 GtkCallback callback, 
                                                 gpointer data)
{
    HildonCaptionPrivate *priv = NULL;

    g_assert (HILDON_IS_CAPTION (container));
    g_assert (callback != NULL);

    priv = HILDON_CAPTION_GET_PRIVATE (container);

    /* Execute callback for the child widgets */
    if (GTK_CONTAINER_CLASS (parent_class)->forall)
        GTK_CONTAINER_CLASS (parent_class)->forall (container, include_internals, callback, data);

    if (include_internals)
        /* Execute callback for the parent box as well */
        (*callback) (priv->caption_area, data);
}

/**
 * hildon_caption_set_size_group:
 * @caption: a #HildonCaption
 * @new_group: a #GtkSizeGroup
 *
 * Sets a #GtkSizeGroup of a given captioned control.
 *
 */
void 
hildon_caption_set_size_group                   (const HildonCaption *self,
                                                 GtkSizeGroup *group)
{
    g_object_set (G_OBJECT(self), "size_group", group, NULL);
}

/**
 * hildon_caption_get_size_group:
 * @caption: a #HildonCaption
 *
 * Queries given captioned control for the #GtkSizeGroup assigned to it.
 *
 * Returns: a #GtkSizeGroup
 * 
 */
GtkSizeGroup*
hildon_caption_get_size_group                   (const HildonCaption *self)
{
    HildonCaptionPrivate *priv;
    g_return_val_if_fail (HILDON_IS_CAPTION (self), NULL);

    priv = HILDON_CAPTION_GET_PRIVATE(self);

    return priv->group;
}

/**
 * hildon_caption_new:
 * @group: a #GtkSizeGroup for controlling the size of related captions or %NULL
 * @value: the caption text to accompany the text entry.  The widget makes
 *          a copy of this text.
 * @control: the control that is to be captioned.
 * @icon: an icon to accompany the label or %NULL in case no icon should be displayed.
 * @flag: indicates whether this captioned control is mandatory or
 *         optional.
 *
 * Creates a new instance of #HildonCaption widget, with a specific
 * control and image.
 * Note: Clicking on a focused caption will trigger the activate signal.
 * The default behaviour for the caption's activate signal is to call	 
 * gtk_widget_activate() on its control.
 * 
 * Returns: a new #HildonCaption
 */
GtkWidget*
hildon_caption_new                              (GtkSizeGroup *group, 
                                                 const gchar *value,
                                                 GtkWidget *child, 
                                                 GtkWidget *icon,
                                                 HildonCaptionStatus flag)
{
    GtkWidget *widget;
    g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);

    widget = g_object_new (HILDON_TYPE_CAPTION, 
            "label", value,
            "child", child, 
            "size_group", group, 
            "icon", icon, 
            "status", flag,
            NULL);

    /* Do not expand GtkCheckButton by default, we want to reduce its activation area */
    if (GTK_IS_CHECK_BUTTON (child))
      hildon_caption_set_child_expand (HILDON_CAPTION (widget), FALSE);

    return widget;
}

/**
 * hildon_caption_is_mandatory:
 * @caption: a #HildonCaption
 * 
 * Queries whether @caption is mandatory.
 *
 * Returns: Whether this captioned control is mandatory.
 */
gboolean 
hildon_caption_is_mandatory                     (const HildonCaption *caption)
{
    HildonCaptionPrivate *priv;

    g_return_val_if_fail (HILDON_IS_CAPTION (caption), FALSE);

    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    return (priv->status == HILDON_CAPTION_MANDATORY);
}

/**
 * hildon_caption_set_icon_position:
 * @caption: a #HildonCaption
 * @pos: one of the values from #HildonCaptionIconPosition
 *
 * Sets @caption<!-- -->'s icon position.
 *
 */
void 
hildon_caption_set_icon_position                (HildonCaption *caption,
                                                 HildonCaptionIconPosition pos)
{
    g_return_if_fail (HILDON_IS_CAPTION (caption));
    HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (caption);

    g_return_if_fail (priv->caption_area != NULL);
    
    int order = (pos == HILDON_CAPTION_POSITION_LEFT) ? -1 : 0;
    gtk_box_reorder_child (GTK_BOX (priv->caption_area), priv->icon_align, order);

    priv->icon_position = pos;
}

/**
 * hildon_caption_get_icon_position:
 * @caption: a #HildonCaption
 *
 * Gets @caption<!-- -->'s icon position.
 *
 * Returns: one of the values from #HildonCaptionIconPosition.
 *
 */

HildonCaptionIconPosition 
hildon_caption_get_icon_position                (const HildonCaption *caption)
{
    g_return_val_if_fail (HILDON_IS_CAPTION (caption), 0);

    HildonCaptionPrivate *priv = HILDON_CAPTION_GET_PRIVATE (caption);

    return priv->icon_position;
}

/**
 * hildon_caption_set_status:
 * @caption: a #HildonCaption
 * @flag: one of the values from #HildonCaptionStatus
 *
 * Sets @caption<!-- -->'s status.
 */
void 
hildon_caption_set_status                       (HildonCaption *caption,
                                                 HildonCaptionStatus flag)
{
    g_return_if_fail (HILDON_IS_CAPTION(caption));

    g_object_set (G_OBJECT(caption), "status", flag, NULL);
}

/**
 * hildon_caption_get_status:
 * @caption: a #HildonCaption
 *
 * Gets @caption<!-- -->'s status.
 *
 * Returns: one of the values from #HildonCaptionStatus
 */
HildonCaptionStatus 
hildon_caption_get_status                       (const HildonCaption *caption)
{
    HildonCaptionPrivate *priv;
    g_return_val_if_fail (HILDON_IS_CAPTION (caption), HILDON_CAPTION_OPTIONAL);

    priv = HILDON_CAPTION_GET_PRIVATE(caption);

    return priv->status;
}

/**
 * hildon_caption_set_icon_image:
 * @caption: a #HildonCaption
 * @icon : the #GtkImage to use as the icon. 
 *         Calls gtk_widget_show() on the icon if it is not visible.
 *
 * Sets the icon image widget to be used by @caption.
 */
void 
hildon_caption_set_icon_image                   (HildonCaption *caption, 
                                                 GtkWidget *icon)
{
    g_return_if_fail (HILDON_IS_CAPTION(caption));

    g_object_set (G_OBJECT(caption), "icon", icon, NULL);
}

/**
 * hildon_caption_get_icon_image:
 * @caption: a #HildonCaption
 *
 * Gets the icon image widget used by @caption.
 *
 * Returns: the #GtkImage widget that is being used as the icon by
 *            @caption, or %NULL if no icon image is in use.
 */
GtkWidget*
hildon_caption_get_icon_image                   (const HildonCaption *caption)
{
    HildonCaptionPrivate *priv;

    g_return_val_if_fail (HILDON_IS_CAPTION (caption), NULL);

    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    return priv->icon;
}

/**
 * hildon_caption_set_label:
 * @caption: a #HildonCaption
 * @label: the text to use
 *
 * Sets the label text that appears before the control.  
 * Separator character is added to the end of the label string. By default
 * the separator is ":". See also hildon_caption_set_separator().
 *
*/
void 
hildon_caption_set_label                        (HildonCaption *caption, 
                                                 const gchar *label)
{
    g_return_if_fail (HILDON_IS_CAPTION (caption));

    g_object_set (G_OBJECT(caption), "label", label, NULL);
}

/**
 * hildon_caption_set_label_markup:
 * @caption : a #HildonCaption
 * @markup : the markup text to use
 *
 * Sets the label markup text that appears before the control. It acts like
 * hildon_caption_set_label() but is using the markup text that allows to
 * specify text properties such as bold or italic.
 */
void 
hildon_caption_set_label_markup                 (HildonCaption *caption, 
                                                 const gchar *markup)
{
    g_return_if_fail (HILDON_IS_CAPTION (caption));

    g_object_set (G_OBJECT(caption), "markup", markup, NULL);
}

/**
 * hildon_caption_get_label:
 * @caption : a #HildonCaption
 *
 * Gets the label of @caption
 * 
 * Returns: the text currently being used as the label of @caption.
 * The string is owned by the label and the caller should never
 * free or modify this value.
 */
gchar*
hildon_caption_get_label                        (const HildonCaption *caption)
{
    HildonCaptionPrivate *priv;
    g_return_val_if_fail (HILDON_IS_CAPTION (caption), "");
    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    return (gchar*) gtk_label_get_text (GTK_LABEL (priv->label));
}

/**
 * hildon_caption_set_separator:
 * @caption: a #HildonCaption
 * @separator: the separator to use
 *
 * Sets the separator character that appears after the label.  
 * The default seaparator character is ":".
 */
void 
hildon_caption_set_separator                    (HildonCaption *caption, 
                                                 const gchar *separator)
{
    g_return_if_fail (HILDON_IS_CAPTION (caption));

    g_object_set (G_OBJECT (caption), "separator", separator, NULL);
}

/**
 * hildon_caption_get_separator:
 * @caption: a #HildonCaption
 *
 * Gets the separator string of @caption
 *
 * Returns: the text currently being used as the separator of the caption
 *  control. The string is owned by @caption and the caller should
 *  never free or modify this value.
 */
gchar*
hildon_caption_get_separator                    (const HildonCaption *caption)
{
    HildonCaptionPrivate *priv;
    g_return_val_if_fail (HILDON_IS_CAPTION (caption), "");

    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    return priv->separator;
}

/* Activates the child control
 * We have this signal so that if needed an application can 
 * know when we've been activated (useful for captions with
 * multiple children
 * FIXME: There never are multiple children. Possibly activate
 *  signal could be removed entirely? (does anyone use it?) 
 */
static void 
hildon_caption_activate                         (GtkWidget *widget)
{
    GtkWidget *child = GTK_BIN (widget)->child;
    gtk_widget_grab_focus (child);
}

static void
hildon_caption_grab_focus                       (GtkWidget *widget)
{
    gtk_widget_grab_focus (GTK_BIN (widget)->child);
}

/**
 * hildon_caption_set_child_expand:
 * @caption: a #HildonCaption
 * @expand: whether the child is expandable.
 *
 * Sets child expandability.
 */
void 
hildon_caption_set_child_expand                 (HildonCaption *caption, 
                                                 gboolean expand)
{
    HildonCaptionPrivate *priv = NULL;
    GtkWidget *child = NULL;
    g_return_if_fail (HILDON_IS_CAPTION (caption));

    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    /* Did the setting really change? */
    if (priv->expand == expand)
        return;

    priv->expand = expand;
    child = GTK_BIN (caption)->child;

    /* We do not have a child, nothing to do */
    if (! GTK_IS_WIDGET (child))
        return;

    if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (caption))
        gtk_widget_queue_resize (child);

    gtk_widget_child_notify (child, "expand");
}

/**
 * hildon_caption_get_child_expand:
 * @caption: a #HildonCaption
 *
 * Gets childs expandability.
 *
 * Returns: wheter the child is expandable or not.
 */
gboolean 
hildon_caption_get_child_expand                 (const HildonCaption *caption)
{
    HildonCaptionPrivate *priv = NULL;
    g_return_val_if_fail (HILDON_IS_CAPTION (caption), FALSE);

    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    return priv->expand;
}

static void
hildon_caption_set_label_text                   (HildonCaptionPrivate *priv, 
                                                 gboolean markup)
{
    gchar *tmp = NULL;
    g_assert (priv != NULL);

    if (priv->text)
    {
        if (priv->separator)
        {
            /* Don't duplicate the separator, if the string already contains one */
            if (g_str_has_suffix (priv->text, priv->separator))
            {
                if (markup)
                    gtk_label_set_markup (GTK_LABEL (priv->label), priv->text);
                else
                    gtk_label_set_text (GTK_LABEL (priv->label), priv->text);
            }
            else
            {
                /* Append separator and set text */
                tmp = g_strconcat( priv->text, priv->separator, NULL );
                
                if (markup)
                    gtk_label_set_markup (GTK_LABEL (priv->label), tmp);
                else
                    gtk_label_set_text (GTK_LABEL (priv->label), tmp);

                g_free (tmp);
            }
        }
        else
        {
            if (markup) 
                gtk_label_set_markup (GTK_LABEL (priv->label), priv->text);
            else
                gtk_label_set_text (GTK_LABEL (priv->label), priv->text);
        }
    }
    else
    {
        /* Clear the label */
        if (markup)
            gtk_label_set_markup (GTK_LABEL (priv->label), "");
        else
            gtk_label_set_text (GTK_LABEL (priv->label), "" );
    }
}

/**
 * hildon_caption_set_label_alignment:
 * @caption: a #HildonCaption widget
 * @alignment: new vertical alignment
 *
 * Sets the vertical alignment to be used for the
 * text part of the caption. Applications need to
 * align the child control themselves.
 *
 */   
void 
hildon_caption_set_label_alignment              (HildonCaption *caption, 
                                                 gfloat alignment)
{
    HildonCaptionPrivate *priv;

    g_return_if_fail (HILDON_IS_CAPTION (caption));

    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    g_object_set (priv->label, "yalign", alignment, NULL);
    g_object_set (priv->icon_align, "yalign", alignment, NULL);

}

/**
 * hildon_caption_get_label_alignment:
 * @caption: a #HildonCaption widget
 *
 * Gets current vertical alignment for the text part.
 *
 * Returns: vertical alignment
 *
 */
gfloat 
hildon_caption_get_label_alignment              (HildonCaption *caption)
{
    HildonCaptionPrivate *priv;
    gfloat result;

    g_return_val_if_fail (HILDON_IS_CAPTION (caption), 0);
    priv = HILDON_CAPTION_GET_PRIVATE (caption);

    g_object_get (priv->label, "yalign", &result, NULL);

    return result;
}