e6b6141a7ed19a5b361e622489305e3a6e32d4b1
[hildon] / src / hildon-button.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Karl Lattimer <karl.lattimer@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser Public License as published by
10  * the Free Software Foundation; version 2 of the license.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser Public License for more details.
16  *
17  */
18
19 /**
20  * SECTION:hildon-button
21  * @short_description: Widget representing a button in the Hildon framework.
22  *
23  * The #HildonButton is a GTK widget which represents a clickable
24  * button. It is derived from the GtkButton widget and provides
25  * additional commodities specific to the Hildon framework.
26  *
27  * The height of a #HildonButton can be set to either "finger" height
28  * or "thumb" height. It can also be configured to use halfscreen or
29  * fullscreen width. Alternatively, either dimension can be set to
30  * "auto" so it behaves like a standard GtkButton.
31  *
32  * The #HildonButton can hold any valid child widget, but it usually
33  * contains two labels, named title and value, and it can also contain
34  * an image. To change the alignment of the button contents, use
35  * gtk_button_set_alignment()
36  *
37  * If only one label is needed, #GtkButton can be used as well, see
38  * also hildon_gtk_button_new().
39  *
40  * <example>
41  * <title>Creating a HildonButton</title>
42  * <programlisting>
43  * void
44  * button_clicked (HildonButton *button, gpointer user_data)
45  * {
46  *     const gchar *title, *value;
47  * <!-- -->
48  *     title = hildon_button_get_title (button);
49  *     value = hildon_button_get_value (button);
50  *     g_debug ("Button clicked with title '&percnt;s' and value '&percnt;s'", title, value);
51  * }
52  * <!-- -->
53  * GtkWidget *
54  * create_button (void)
55  * {
56  *     GtkWidget *button;
57  *     GtkWidget *image;
58  * <!-- -->
59  *     button = hildon_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
60  *                                 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
61  *     hildon_button_set_text (HILDON_BUTTON (button), "Some title", "Some value");
62  * <!-- -->
63  *     image = gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON);
64  *     hildon_button_set_image (HILDON_BUTTON (button), image);
65  *     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
66  * <!-- -->
67  *     gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5);
68  * <!-- -->
69  *     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), NULL);
70  * <!-- -->
71  *     return button;
72  * }
73  * </programlisting>
74  * </example>
75  */
76
77 #include                                        "hildon-button.h"
78 #include                                        "hildon-enum-types.h"
79 #include                                        "hildon-gtk.h"
80 #include                                        "hildon-helper.h"
81
82 G_DEFINE_TYPE                                   (HildonButton, hildon_button, GTK_TYPE_BUTTON);
83
84 #define                                         HILDON_BUTTON_GET_PRIVATE(obj) \
85                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
86                                                 HILDON_TYPE_BUTTON, HildonButtonPrivate));
87
88 typedef struct                                  _HildonButtonPrivate HildonButtonPrivate;
89
90 struct                                          _HildonButtonPrivate
91 {
92     GtkLabel *title;
93     GtkLabel *value;
94     GtkBox *hbox;
95     GtkWidget *label_box;
96     GtkWidget *alignment;
97     GtkWidget *image;
98     GtkPositionType image_position;
99     gfloat image_xalign;
100     gfloat image_yalign;
101 };
102
103 enum {
104     PROP_TITLE = 1,
105     PROP_VALUE,
106     PROP_SIZE,
107     PROP_ARRANGEMENT
108 };
109
110 static void
111 hildon_button_set_arrangement                   (HildonButton            *button,
112                                                  HildonButtonArrangement  arrangement);
113
114 static void
115 hildon_button_construct_child                   (HildonButton *button);
116
117 static void
118 hildon_button_set_property                      (GObject      *object,
119                                                  guint         prop_id,
120                                                  const GValue *value,
121                                                  GParamSpec   *pspec)
122 {
123     HildonButton *button = HILDON_BUTTON (object);
124
125     switch (prop_id)
126     {
127     case PROP_TITLE:
128         hildon_button_set_title (button, g_value_get_string (value));
129         break;
130     case PROP_VALUE:
131         hildon_button_set_value (button, g_value_get_string (value));
132         break;
133     case PROP_SIZE:
134         hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
135         break;
136     case PROP_ARRANGEMENT:
137         hildon_button_set_arrangement (button, g_value_get_enum (value));
138         break;
139     default:
140         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141         break;
142     }
143 }
144
145 static void
146 hildon_button_get_property                      (GObject    *object,
147                                                  guint       prop_id,
148                                                  GValue     *value,
149                                                  GParamSpec *pspec)
150 {
151     HildonButton *button = HILDON_BUTTON (object);
152
153     switch (prop_id)
154     {
155     case PROP_TITLE:
156         g_value_set_string (value, hildon_button_get_title (button));
157         break;
158     case PROP_VALUE:
159         g_value_set_string (value, hildon_button_get_value (button));
160         break;
161     default:
162         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163         break;
164     }
165 }
166
167 static void
168 hildon_button_style_set                         (GtkWidget *widget,
169                                                  GtkStyle  *previous_style)
170 {
171     guint horizontal_spacing, vertical_spacing, image_spacing;
172     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (widget);
173
174     if (GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set)
175         GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set (widget, previous_style);
176
177     gtk_widget_style_get (widget,
178                           "horizontal-spacing", &horizontal_spacing,
179                           "vertical-spacing", &vertical_spacing,
180                           "image-spacing", &image_spacing,
181                           NULL);
182
183     if (GTK_IS_HBOX (priv->label_box)) {
184         gtk_box_set_spacing (GTK_BOX (priv->label_box), horizontal_spacing);
185     } else {
186         gtk_box_set_spacing (GTK_BOX (priv->label_box), vertical_spacing);
187     }
188
189     if (GTK_IS_BOX (priv->hbox)) {
190         gtk_box_set_spacing (priv->hbox, image_spacing);
191     }
192 }
193
194 static void
195 hildon_button_finalize                          (GObject *object)
196 {
197     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (object);
198
199     g_object_unref (priv->alignment);
200     g_object_unref (priv->label_box);
201
202     G_OBJECT_CLASS (hildon_button_parent_class)->finalize (object);
203 }
204
205 static void
206 hildon_button_class_init                        (HildonButtonClass *klass)
207 {
208     GObjectClass *gobject_class = (GObjectClass *)klass;
209     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
210
211     gobject_class->set_property = hildon_button_set_property;
212     gobject_class->get_property = hildon_button_get_property;
213     gobject_class->finalize = hildon_button_finalize;
214     widget_class->style_set = hildon_button_style_set;
215
216     g_object_class_install_property (
217         gobject_class,
218         PROP_TITLE,
219         g_param_spec_string (
220             "title",
221             "Title",
222             "Text of the title label inside the button",
223             NULL,
224             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
225
226     g_object_class_install_property (
227         gobject_class,
228         PROP_VALUE,
229         g_param_spec_string (
230             "value",
231             "Value",
232             "Text of the value label inside the button",
233             NULL,
234             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
235
236     g_object_class_install_property (
237         gobject_class,
238         PROP_SIZE,
239         g_param_spec_flags (
240             "size",
241             "Size",
242             "Size request for the button",
243             HILDON_TYPE_SIZE_TYPE,
244             HILDON_SIZE_AUTO,
245             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
246
247     g_object_class_install_property (
248         gobject_class,
249         PROP_ARRANGEMENT,
250         g_param_spec_enum (
251             "arrangement",
252             "Arrangement",
253             "How the button contents must be arranged",
254             HILDON_TYPE_BUTTON_ARRANGEMENT,
255             HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
256             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
257
258     gtk_widget_class_install_style_property (
259         widget_class,
260         g_param_spec_uint (
261             "horizontal-spacing",
262             "Horizontal spacing between labels",
263             "Horizontal spacing between the title and value labels, when in horizontal mode",
264             0, G_MAXUINT, 25,
265             G_PARAM_READABLE));
266
267     gtk_widget_class_install_style_property (
268         widget_class,
269         g_param_spec_uint (
270             "vertical-spacing",
271             "Vertical spacing between labels",
272             "Vertical spacing between the title and value labels, when in vertical mode",
273             0, G_MAXUINT, 5,
274             G_PARAM_READABLE));
275
276     g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
277 }
278
279 static void
280 hildon_button_init                              (HildonButton *self)
281 {
282     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
283
284     priv->title = GTK_LABEL (gtk_label_new (NULL));
285     priv->value = GTK_LABEL (gtk_label_new (NULL));
286     priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
287     priv->image = NULL;
288     priv->image_position = GTK_POS_LEFT;
289     priv->image_xalign = 0.5;
290     priv->image_yalign = 0.5;
291     priv->hbox = NULL;
292     priv->label_box = NULL;
293
294     gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
295     gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
296
297     gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
298     gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
299
300     g_object_ref_sink (priv->alignment);
301
302     /* The labels are not shown automatically, see hildon_button_set_(title|value) */
303     gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
304     gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
305 }
306
307 /**
308  * hildon_button_add_title_size_group:
309  * @button: a #HildonButton
310  * @size_group: A #GtkSizeGroup for the button title (main label)
311  *
312  * Adds the title label of @button to @size_group.
313  **/
314 void
315 hildon_button_add_title_size_group              (HildonButton *button,
316                                                  GtkSizeGroup *size_group)
317 {
318     HildonButtonPrivate *priv;
319
320     g_return_if_fail (HILDON_IS_BUTTON (button));
321     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
322
323     priv = HILDON_BUTTON_GET_PRIVATE (button);
324
325     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->title));
326 }
327
328 /**
329  * hildon_button_add_value_size_group:
330  * @button: a #HildonButton
331  * @size_group: A #GtkSizeGroup for the button value (secondary label)
332  *
333  * Adds the value label of @button to @size_group.
334  **/
335 void
336 hildon_button_add_value_size_group              (HildonButton *button,
337                                                  GtkSizeGroup *size_group)
338 {
339     HildonButtonPrivate *priv;
340
341     g_return_if_fail (HILDON_IS_BUTTON (button));
342     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
343
344     priv = HILDON_BUTTON_GET_PRIVATE (button);
345
346     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->value));
347 }
348
349 /**
350  * hildon_button_add_image_size_group:
351  * @button: a #HildonButton
352  * @size_group: A #GtkSizeGroup for the button image
353  *
354  * Adds the image of @button to @size_group. You must add an image
355  * using hildon_button_set_image() before calling this function.
356  **/
357 void
358 hildon_button_add_image_size_group              (HildonButton *button,
359                                                  GtkSizeGroup *size_group)
360 {
361     HildonButtonPrivate *priv;
362
363     g_return_if_fail (HILDON_IS_BUTTON (button));
364     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
365
366     priv = HILDON_BUTTON_GET_PRIVATE (button);
367
368     g_return_if_fail (GTK_IS_WIDGET (priv->image));
369
370     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->image));
371 }
372
373 /**
374  * hildon_button_add_size_groups:
375  * @button: a #HildonButton
376  * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
377  * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
378  * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
379  *
380  * Convenience function to add title, value and image to size
381  * groups. %NULL size groups will be ignored.
382  **/
383 void
384 hildon_button_add_size_groups                   (HildonButton *button,
385                                                  GtkSizeGroup *title_size_group,
386                                                  GtkSizeGroup *value_size_group,
387                                                  GtkSizeGroup *image_size_group)
388 {
389     if (title_size_group)
390         hildon_button_add_title_size_group (button, title_size_group);
391
392     if (value_size_group)
393         hildon_button_add_value_size_group (button, value_size_group);
394
395     if (image_size_group)
396         hildon_button_add_image_size_group (button, image_size_group);
397 }
398
399 /**
400  * hildon_button_new:
401  * @size: Flags to set the size of the button.
402  * @arrangement: How the labels must be arranged.
403  *
404  * Creates a new #HildonButton. To set text in the labels, use
405  * hildon_button_set_title() and
406  * hildon_button_set_value(). Alternatively, you can add a custom
407  * child widget using gtk_container_add().
408  *
409  * Returns: a new #HildonButton
410  **/
411 GtkWidget *
412 hildon_button_new                               (HildonSizeType          size,
413                                                  HildonButtonArrangement arrangement)
414 {
415     return hildon_button_new_with_text (size, arrangement, NULL, NULL);
416 }
417
418 /**
419  * hildon_button_new_with_text:
420  * @size: Flags to set the size of the button.
421  * @arrangement: How the labels must be arranged.
422  * @title: Title of the button (main label), or %NULL
423  * @value: Value of the button (secondary label), or %NULL
424  *
425  * Creates a new #HildonButton with two labels, @title and @value.
426  *
427  * If you just don't want to use one of the labels, set it to
428  * %NULL. You can set it to a non-%NULL value at any time later.
429  *
430  * Returns: a new #HildonButton
431  **/
432 GtkWidget *
433 hildon_button_new_with_text                     (HildonSizeType           size,
434                                                  HildonButtonArrangement  arrangement,
435                                                  const gchar             *title,
436                                                  const gchar             *value)
437 {
438     GtkWidget *button;
439
440     /* Create widget */
441     button = g_object_new (HILDON_TYPE_BUTTON,
442                            "size", size,
443                            "title", title,
444                            "value", value,
445                            "arrangement", arrangement,
446                            NULL);
447
448     return button;
449 }
450
451 static void
452 hildon_button_set_arrangement                   (HildonButton            *button,
453                                                  HildonButtonArrangement  arrangement)
454 {
455     HildonButtonPrivate *priv;
456
457     priv = HILDON_BUTTON_GET_PRIVATE (button);
458
459     /* Pack everything */
460     if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
461         priv->label_box = gtk_vbox_new (FALSE, 0);
462         hildon_helper_set_logical_font (GTK_WIDGET (priv->value), "SmallSystemFont");
463     } else {
464         priv->label_box = gtk_hbox_new (FALSE, 0);
465     }
466
467     g_object_ref_sink (priv->label_box);
468
469     /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
470      * can be painted outside of the button in some situations, see
471      * NB#88126 */
472     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
473     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
474
475     hildon_button_construct_child (button);
476 }
477
478 /**
479  * hildon_button_set_title:
480  * @button: a #HildonButton
481  * @title: a new title (main label) for the button, or %NULL
482  *
483  * Sets the title (main label) of @button to @title.
484  *
485  * This will clear any previously set title.
486  *
487  * If @title is set to %NULL, the title label will be hidden and the
488  * value label will be realigned.
489  **/
490 void
491 hildon_button_set_title                         (HildonButton *button,
492                                                  const gchar  *title)
493 {
494     HildonButtonPrivate *priv;
495
496     g_return_if_fail (HILDON_IS_BUTTON (button));
497
498     priv = HILDON_BUTTON_GET_PRIVATE (button);
499     gtk_label_set_text (priv->title, title);
500
501     /* If the button has no title, hide the label so the value is
502      * properly aligned */
503     if (title) {
504         hildon_button_construct_child (button);
505         gtk_widget_show (GTK_WIDGET (priv->title));
506     } else {
507         gtk_widget_hide (GTK_WIDGET (priv->title));
508     }
509
510     g_object_notify (G_OBJECT (button), "title");
511 }
512
513 /**
514  * hildon_button_set_value:
515  * @button: a #HildonButton
516  * @value: a new value (secondary label) for the button, or %NULL
517  *
518  * Sets the value (secondary label) of @button to @value.
519  *
520  * This will clear any previously set value.
521  *
522  * If @value is set to %NULL, the value label will be hidden and the
523  * title label will be realigned.
524  *
525  **/
526 void
527 hildon_button_set_value                         (HildonButton *button,
528                                                  const gchar  *value)
529 {
530     HildonButtonPrivate *priv;
531
532     g_return_if_fail (HILDON_IS_BUTTON (button));
533
534     priv = HILDON_BUTTON_GET_PRIVATE (button);
535     gtk_label_set_text (priv->value, value);
536
537     /* If the button has no value, hide the label so the title is
538      * properly aligned */
539     if (value) {
540         hildon_button_construct_child (button);
541         gtk_widget_show (GTK_WIDGET (priv->value));
542     } else {
543         gtk_widget_hide (GTK_WIDGET (priv->value));
544     }
545
546     g_object_notify (G_OBJECT (button), "value");
547 }
548
549 /**
550  * hildon_button_get_title:
551  * @button: a #HildonButton
552  *
553  * Gets the text from the main label (title) of @button, or %NULL if
554  * none has been set.
555  *
556  * Returns: The text of the title label. This string is owned by the
557  * widget and must not be modified or freed.
558  **/
559 const gchar *
560 hildon_button_get_title                         (HildonButton *button)
561 {
562     HildonButtonPrivate *priv;
563
564     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
565
566     priv = HILDON_BUTTON_GET_PRIVATE (button);
567
568     return gtk_label_get_text (priv->title);
569 }
570
571 /**
572  * hildon_button_get_value:
573  * @button: a #HildonButton
574  *
575  * Gets the text from the secondary label (value) of @button, or %NULL
576  * if none has been set.
577  *
578  * Returns: The text of the value label. This string is owned by the
579  * widget and must not be modified or freed.
580  **/
581 const gchar *
582 hildon_button_get_value                         (HildonButton *button)
583 {
584     HildonButtonPrivate *priv;
585
586     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
587
588     priv = HILDON_BUTTON_GET_PRIVATE (button);
589
590     return gtk_label_get_text (priv->value);
591 }
592
593 /**
594  * hildon_button_set_text:
595  * @button: a #HildonButton
596  * @title: new text for the button title (main label)
597  * @value: new text for the button value (secondary label)
598  *
599  * Convenience function to change both labels of a #HildonButton
600  **/
601 void
602 hildon_button_set_text                          (HildonButton *button,
603                                                  const gchar  *title,
604                                                  const gchar  *value)
605 {
606     hildon_button_set_title (button, title);
607     hildon_button_set_value (button, value);
608 }
609
610 /**
611  * hildon_button_set_image:
612  * @button: a #HildonButton
613  * @image: a widget to set as the button image
614  *
615  * Sets the image of @button to the given widget. The previous image
616  * (if any) will be removed.
617  **/
618 void
619 hildon_button_set_image                         (HildonButton *button,
620                                                  GtkWidget    *image)
621 {
622     HildonButtonPrivate *priv;
623
624     g_return_if_fail (HILDON_IS_BUTTON (button));
625     g_return_if_fail (!image || GTK_IS_WIDGET (image));
626
627     priv = HILDON_BUTTON_GET_PRIVATE (button);
628
629     /* Return if there's nothing to do */
630     if (image == priv->image)
631         return;
632
633     if (priv->image && priv->image->parent)
634         gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
635
636     priv->image = image;
637
638     hildon_button_construct_child (button);
639 }
640
641 /**
642  * hildon_button_get_image:
643  * @button: a #HildonButton
644  *
645  * Gets the widget that is currenty set as the image of @button,
646  * previously set with hildon_button_set_image()
647  *
648  * Returns: a #GtkWidget or %NULL in case there is no image
649  **/
650 GtkWidget *
651 hildon_button_get_image                         (HildonButton *button)
652 {
653     HildonButtonPrivate *priv;
654
655     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
656
657     priv = HILDON_BUTTON_GET_PRIVATE (button);
658
659     return priv->image;
660 }
661
662 /**
663  * hildon_button_set_image_position:
664  * @button: a #HildonButton
665  * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
666  *
667  * Sets the position of the image inside @button. Only %GTK_POS_LEFT
668  * and %GTK_POS_RIGHT are currently supported.
669  **/
670 void
671 hildon_button_set_image_position                (HildonButton    *button,
672                                                  GtkPositionType  position)
673 {
674     HildonButtonPrivate *priv;
675
676     g_return_if_fail (HILDON_IS_BUTTON (button));
677     g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
678
679     priv = HILDON_BUTTON_GET_PRIVATE (button);
680
681     /* Return if there's nothing to do */
682     if (priv->image_position == position)
683         return;
684
685     priv->image_position = position;
686
687     hildon_button_construct_child (button);
688 }
689
690 /**
691  * hildon_button_set_alignment:
692  * @button: a #HildonButton
693  * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
694  * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
695  * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
696  * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
697  *
698  * Sets the alignment of the contents of the widget. If you don't need
699  * to change @xscale or @yscale you can just use
700  * gtk_button_set_alignment() instead.
701  **/
702 void
703 hildon_button_set_alignment                     (HildonButton *button,
704                                                  gfloat        xalign,
705                                                  gfloat        yalign,
706                                                  gfloat        xscale,
707                                                  gfloat        yscale)
708 {
709     HildonButtonPrivate *priv;
710     GtkWidget *child;
711
712     g_return_if_fail (HILDON_IS_BUTTON (button));
713
714     priv = HILDON_BUTTON_GET_PRIVATE (button);
715
716     child = gtk_bin_get_child (GTK_BIN (button));
717
718     if (GTK_IS_ALIGNMENT (child)) {
719         gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign);
720         g_object_set (child, "xscale", xscale, "yscale", yscale, NULL);
721     }
722 }
723
724 /**
725  * hildon_button_set_title_alignment:
726  * @button: a #HildonButton
727  * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
728  * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
729  *
730  * Sets the alignment of the title label. See also
731  * hildon_button_set_alignment() to set the alignment of the whole
732  * contents of the button.
733  **/
734 void
735 hildon_button_set_title_alignment               (HildonButton *button,
736                                                  gfloat        xalign,
737                                                  gfloat        yalign)
738 {
739     HildonButtonPrivate *priv;
740
741     g_return_if_fail (HILDON_IS_BUTTON (button));
742
743     priv = HILDON_BUTTON_GET_PRIVATE (button);
744
745     gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign);
746 }
747
748 /**
749  * hildon_button_set_value_alignment:
750  * @button: a #HildonButton
751  * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
752  * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
753  *
754  * Sets the alignment of the value label. See also
755  * hildon_button_set_alignment() to set the alignment of the whole
756  * contents of the button.
757  **/
758 void
759 hildon_button_set_value_alignment               (HildonButton *button,
760                                                  gfloat        xalign,
761                                                  gfloat        yalign)
762 {
763     HildonButtonPrivate *priv;
764
765     g_return_if_fail (HILDON_IS_BUTTON (button));
766
767     priv = HILDON_BUTTON_GET_PRIVATE (button);
768
769     gtk_misc_set_alignment (GTK_MISC (priv->value), xalign, yalign);
770 }
771
772 /**
773  * hildon_button_set_image_alignment:
774  * @button: a #HildonButton
775  * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
776  * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
777  *
778  * Sets the alignment of the image. See also
779  * hildon_button_set_alignment() to set the alignment of the whole
780  * contents of the button.
781  **/
782 void
783 hildon_button_set_image_alignment               (HildonButton *button,
784                                                  gfloat        xalign,
785                                                  gfloat        yalign)
786 {
787     HildonButtonPrivate *priv;
788
789     g_return_if_fail (HILDON_IS_BUTTON (button));
790
791     priv = HILDON_BUTTON_GET_PRIVATE (button);
792
793     /* Return if there's nothing to do */
794     if (priv->image_xalign == xalign && priv->image_yalign == yalign)
795         return;
796
797     priv->image_xalign = xalign;
798     priv->image_yalign = yalign;
799
800     hildon_button_construct_child (button);
801 }
802
803 static void
804 hildon_button_construct_child                   (HildonButton *button)
805 {
806     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
807     GtkWidget *child;
808     gint image_spacing;
809     const gchar *title, *value;
810
811     /* Don't do anything if the button is not constructed yet */
812     if (G_UNLIKELY (priv->label_box == NULL))
813         return;
814
815     /* Don't do anything if the button has no contents */
816     title = gtk_label_get_text (priv->title);
817     value = gtk_label_get_text (priv->value);
818     if (!priv->image && !title[0] && !value[0])
819         return;
820
821     /* Save a ref to the image, and remove it from its container if necessary */
822     if (priv->image) {
823         g_object_ref (priv->image);
824         if (priv->image->parent != NULL)
825             gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
826     }
827
828     if (priv->label_box->parent != NULL) {
829         gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
830     }
831
832     /* Remove the child from the container and add priv->alignment */
833     child = gtk_bin_get_child (GTK_BIN (button));
834     if (child != NULL && child != priv->alignment) {
835         gtk_container_remove (GTK_CONTAINER (button), child);
836         child = NULL;
837     }
838
839     if (child == NULL) {
840         gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
841     }
842
843     /* Create a new hbox */
844     if (priv->hbox) {
845         gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
846     }
847     gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
848     priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
849     gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
850
851     /* Pack the image and the alignment in the new hbox */
852     if (priv->image && priv->image_position == GTK_POS_LEFT)
853         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
854
855     gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
856
857     if (priv->image && priv->image_position == GTK_POS_RIGHT)
858         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
859
860     /* Set image alignment and remove previously set ref */
861     if (priv->image) {
862         gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
863         g_object_unref (priv->image);
864     }
865
866     /* Show everything */
867     gtk_widget_show_all (GTK_WIDGET (priv->alignment));
868 }