2008-09-12 Alberto Garcia <agarcia@igalia.com>
[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
81 G_DEFINE_TYPE                                   (HildonButton, hildon_button, GTK_TYPE_BUTTON);
82
83 #define                                         HILDON_BUTTON_GET_PRIVATE(obj) \
84                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
85                                                 HILDON_TYPE_BUTTON, HildonButtonPrivate));
86
87 typedef struct                                  _HildonButtonPrivate HildonButtonPrivate;
88
89 struct                                          _HildonButtonPrivate
90 {
91     GtkLabel *title;
92     GtkLabel *value;
93     GtkBox *hbox;
94     GtkWidget *label_box;
95     GtkWidget *alignment;
96     GtkWidget *image;
97     GtkPositionType image_position;
98     gfloat image_xalign;
99     gfloat image_yalign;
100 };
101
102 enum {
103     PROP_TITLE = 1,
104     PROP_VALUE,
105     PROP_SIZE,
106     PROP_ARRANGEMENT
107 };
108
109 static void
110 hildon_button_set_arrangement                   (HildonButton            *button,
111                                                  HildonButtonArrangement  arrangement);
112
113 static void
114 hildon_button_construct_child                   (HildonButton *button);
115
116 static void
117 hildon_button_set_property                      (GObject      *object,
118                                                  guint         prop_id,
119                                                  const GValue *value,
120                                                  GParamSpec   *pspec)
121 {
122     HildonButton *button = HILDON_BUTTON (object);
123
124     switch (prop_id)
125     {
126     case PROP_TITLE:
127         hildon_button_set_title (button, g_value_get_string (value));
128         break;
129     case PROP_VALUE:
130         hildon_button_set_value (button, g_value_get_string (value));
131         break;
132     case PROP_SIZE:
133         hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
134         break;
135     case PROP_ARRANGEMENT:
136         hildon_button_set_arrangement (button, g_value_get_enum (value));
137         break;
138     default:
139         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140         break;
141     }
142 }
143
144 static void
145 hildon_button_get_property                      (GObject    *object,
146                                                  guint       prop_id,
147                                                  GValue     *value,
148                                                  GParamSpec *pspec)
149 {
150     HildonButton *button = HILDON_BUTTON (object);
151     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
152
153     switch (prop_id)
154     {
155     case PROP_TITLE:
156         g_value_set_string (value, gtk_label_get_text (priv->title));
157         break;
158     case PROP_VALUE:
159         g_value_set_string (value, gtk_label_get_text (priv->value));
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_class_init                        (HildonButtonClass *klass)
196 {
197     GObjectClass *gobject_class = (GObjectClass *)klass;
198     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
199
200     gobject_class->set_property = hildon_button_set_property;
201     gobject_class->get_property = hildon_button_get_property;
202     widget_class->style_set = hildon_button_style_set;
203
204     g_object_class_install_property (
205         gobject_class,
206         PROP_TITLE,
207         g_param_spec_string (
208             "title",
209             "Title",
210             "Text of the title label inside the button",
211             NULL,
212             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
213
214     g_object_class_install_property (
215         gobject_class,
216         PROP_VALUE,
217         g_param_spec_string (
218             "value",
219             "Value",
220             "Text of the value label inside the button",
221             NULL,
222             G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
223
224     g_object_class_install_property (
225         gobject_class,
226         PROP_SIZE,
227         g_param_spec_flags (
228             "size",
229             "Size",
230             "Size request for the button",
231             HILDON_TYPE_SIZE_TYPE,
232             HILDON_SIZE_AUTO,
233             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
234
235     g_object_class_install_property (
236         gobject_class,
237         PROP_ARRANGEMENT,
238         g_param_spec_enum (
239             "arrangement",
240             "Arrangement",
241             "How the button contents must be arranged",
242             HILDON_TYPE_BUTTON_ARRANGEMENT,
243             HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
244             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
245
246     gtk_widget_class_install_style_property (
247         widget_class,
248         g_param_spec_uint (
249             "horizontal-spacing",
250             "Horizontal spacing between labels",
251             "Horizontal spacing between the title and value labels, when in horizontal mode",
252             0, G_MAXUINT, 25,
253             G_PARAM_READABLE));
254
255     gtk_widget_class_install_style_property (
256         widget_class,
257         g_param_spec_uint (
258             "vertical-spacing",
259             "Vertical spacing between labels",
260             "Vertical spacing between the title and value labels, when in vertical mode",
261             0, G_MAXUINT, 5,
262             G_PARAM_READABLE));
263
264     g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
265 }
266
267 static void
268 hildon_button_init                              (HildonButton *self)
269 {
270     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
271
272     priv->title = GTK_LABEL (gtk_label_new (NULL));
273     priv->value = GTK_LABEL (gtk_label_new (NULL));
274     priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
275     priv->image = NULL;
276     priv->image_position = GTK_POS_LEFT;
277     priv->image_xalign = 0.0;
278     priv->image_yalign = 0.0;
279     priv->hbox = NULL;
280     priv->label_box = NULL;
281
282     gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
283     gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
284
285     gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
286     gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
287
288     /* The labels are not shown automatically, see hildon_button_set_(title|value) */
289     gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
290     gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
291 }
292
293 /**
294  * hildon_button_add_title_size_group:
295  * @button: a #HildonButton
296  * @size_group: A #GtkSizeGroup for the button title (main label)
297  *
298  * Adds the title label of @button to @size_group.
299  **/
300 void
301 hildon_button_add_title_size_group              (HildonButton *button,
302                                                  GtkSizeGroup *size_group)
303 {
304     HildonButtonPrivate *priv;
305
306     g_return_if_fail (HILDON_IS_BUTTON (button));
307     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
308
309     priv = HILDON_BUTTON_GET_PRIVATE (button);
310
311     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->title));
312 }
313
314 /**
315  * hildon_button_add_value_size_group:
316  * @button: a #HildonButton
317  * @size_group: A #GtkSizeGroup for the button value (secondary label)
318  *
319  * Adds the value label of @button to @size_group.
320  **/
321 void
322 hildon_button_add_value_size_group              (HildonButton *button,
323                                                  GtkSizeGroup *size_group)
324 {
325     HildonButtonPrivate *priv;
326
327     g_return_if_fail (HILDON_IS_BUTTON (button));
328     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
329
330     priv = HILDON_BUTTON_GET_PRIVATE (button);
331
332     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->value));
333 }
334
335 /**
336  * hildon_button_add_image_size_group:
337  * @button: a #HildonButton
338  * @size_group: A #GtkSizeGroup for the button image
339  *
340  * Adds the image of @button to @size_group. You must add an image
341  * using hildon_button_set_image() before calling this function.
342  **/
343 void
344 hildon_button_add_image_size_group              (HildonButton *button,
345                                                  GtkSizeGroup *size_group)
346 {
347     HildonButtonPrivate *priv;
348
349     g_return_if_fail (HILDON_IS_BUTTON (button));
350     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
351
352     priv = HILDON_BUTTON_GET_PRIVATE (button);
353
354     g_return_if_fail (GTK_IS_WIDGET (priv->image));
355
356     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->image));
357 }
358
359 /**
360  * hildon_button_add_size_groups:
361  * @button: a #HildonButton
362  * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
363  * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
364  * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
365  *
366  * Convenience function to add title, value and image to size
367  * groups. %NULL size groups will be ignored.
368  **/
369 void
370 hildon_button_add_size_groups                   (HildonButton *button,
371                                                  GtkSizeGroup *title_size_group,
372                                                  GtkSizeGroup *value_size_group,
373                                                  GtkSizeGroup *image_size_group)
374 {
375     if (title_size_group)
376         hildon_button_add_title_size_group (button, title_size_group);
377
378     if (value_size_group)
379         hildon_button_add_value_size_group (button, value_size_group);
380
381     if (image_size_group)
382         hildon_button_add_image_size_group (button, image_size_group);
383 }
384
385 /**
386  * hildon_button_new:
387  * @size: Flags to set the size of the button.
388  * @arrangement: How the labels must be arranged.
389  *
390  * Creates a new #HildonButton. To set text in the labels, use
391  * hildon_button_set_title() and
392  * hildon_button_set_value(). Alternatively, you can add a custom
393  * child widget using gtk_container_add().
394  *
395  * Returns: a new #HildonButton
396  **/
397 GtkWidget *
398 hildon_button_new                               (HildonSizeType          size,
399                                                  HildonButtonArrangement arrangement)
400 {
401     return hildon_button_new_with_text (size, arrangement, NULL, NULL);
402 }
403
404 /**
405  * hildon_button_new_with_text:
406  * @size: Flags to set the size of the button.
407  * @arrangement: How the labels must be arranged.
408  * @title: Title of the button (main label), or %NULL
409  * @value: Value of the button (secondary label), or %NULL
410  *
411  * Creates a new #HildonButton with two labels, @title and @value.
412  *
413  * If you just don't want to use one of the labels, set it to
414  * %NULL. You can set it to a non-%NULL value at any time later.
415  *
416  * Returns: a new #HildonButton
417  **/
418 GtkWidget *
419 hildon_button_new_with_text                     (HildonSizeType           size,
420                                                  HildonButtonArrangement  arrangement,
421                                                  const gchar             *title,
422                                                  const gchar             *value)
423 {
424     GtkWidget *button;
425
426     /* Create widget */
427     button = g_object_new (HILDON_TYPE_BUTTON,
428                            "size", size,
429                            "title", title,
430                            "value", value,
431                            "arrangement", arrangement,
432                            NULL);
433
434     return button;
435 }
436
437 static void
438 hildon_button_set_arrangement                   (HildonButton            *button,
439                                                  HildonButtonArrangement  arrangement)
440 {
441     HildonButtonPrivate *priv;
442
443     priv = HILDON_BUTTON_GET_PRIVATE (button);
444
445     /* Pack everything */
446     if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
447         priv->label_box = gtk_vbox_new (FALSE, 0);
448     } else {
449         priv->label_box = gtk_hbox_new (FALSE, 0);
450     }
451
452     /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
453      * can be painted outside of the button in some situations, see
454      * NB#88126 */
455     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
456     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
457
458     hildon_button_construct_child (button);
459 }
460
461 /**
462  * hildon_button_set_title:
463  * @button: a #HildonButton
464  * @title: a new title (main label) for the button, or %NULL
465  *
466  * Sets the title (main label) of @button to @title.
467  *
468  * This will clear any previously set title.
469  *
470  * If @title is set to %NULL, the title label will be hidden and the
471  * value label will be realigned.
472  **/
473 void
474 hildon_button_set_title                         (HildonButton *button,
475                                                  const gchar  *title)
476 {
477     HildonButtonPrivate *priv;
478
479     g_return_if_fail (HILDON_IS_BUTTON (button));
480
481     priv = HILDON_BUTTON_GET_PRIVATE (button);
482     gtk_label_set_text (priv->title, title);
483
484     /* If the button has no title, hide the label so the value is
485      * properly aligned */
486     if (title) {
487         hildon_button_construct_child (button);
488         gtk_widget_show (GTK_WIDGET (priv->title));
489     } else {
490         gtk_widget_hide (GTK_WIDGET (priv->title));
491     }
492
493     g_object_notify (G_OBJECT (button), "title");
494 }
495
496 /**
497  * hildon_button_set_value:
498  * @button: a #HildonButton
499  * @value: a new value (secondary label) for the button, or %NULL
500  *
501  * Sets the value (secondary label) of @button to @value.
502  *
503  * This will clear any previously set value.
504  *
505  * If @value is set to %NULL, the value label will be hidden and the
506  * title label will be realigned.
507  *
508  **/
509 void
510 hildon_button_set_value                         (HildonButton *button,
511                                                  const gchar  *value)
512 {
513     HildonButtonPrivate *priv;
514
515     g_return_if_fail (HILDON_IS_BUTTON (button));
516
517     priv = HILDON_BUTTON_GET_PRIVATE (button);
518     gtk_label_set_text (priv->value, value);
519
520     /* If the button has no value, hide the label so the title is
521      * properly aligned */
522     if (value) {
523         hildon_button_construct_child (button);
524         gtk_widget_show (GTK_WIDGET (priv->value));
525     } else {
526         gtk_widget_hide (GTK_WIDGET (priv->value));
527     }
528
529     g_object_notify (G_OBJECT (button), "value");
530 }
531
532 /**
533  * hildon_button_get_title:
534  * @button: a #HildonButton
535  *
536  * Gets the text from the main label (title) of @button, or %NULL if
537  * none has been set.
538  *
539  * Returns: The text of the title label. This string is owned by the
540  * widget and must not be modified or freed.
541  **/
542 const gchar *
543 hildon_button_get_title                         (HildonButton *button)
544 {
545     HildonButtonPrivate *priv;
546
547     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
548
549     priv = HILDON_BUTTON_GET_PRIVATE (button);
550
551     return gtk_label_get_text (priv->title);
552 }
553
554 /**
555  * hildon_button_get_value:
556  * @button: a #HildonButton
557  *
558  * Gets the text from the secondary label (value) of @button, or %NULL
559  * if none has been set.
560  *
561  * Returns: The text of the value label. This string is owned by the
562  * widget and must not be modified or freed.
563  **/
564 const gchar *
565 hildon_button_get_value                         (HildonButton *button)
566 {
567     HildonButtonPrivate *priv;
568
569     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
570
571     priv = HILDON_BUTTON_GET_PRIVATE (button);
572
573     return gtk_label_get_text (priv->value);
574 }
575
576 /**
577  * hildon_button_set_text:
578  * @button: a #HildonButton
579  * @title: new text for the button title (main label)
580  * @value: new text for the button value (secondary label)
581  *
582  * Convenience function to change both labels of a #HildonButton
583  **/
584 void
585 hildon_button_set_text                          (HildonButton *button,
586                                                  const gchar  *title,
587                                                  const gchar  *value)
588 {
589     hildon_button_set_title (button, title);
590     hildon_button_set_value (button, value);
591 }
592
593 /**
594  * hildon_button_set_image:
595  * @button: a #HildonButton
596  * @image: a widget to set as the button image
597  *
598  * Sets the image of @button to the given widget. The previous image
599  * (if any) will be removed.
600  **/
601 void
602 hildon_button_set_image                         (HildonButton *button,
603                                                  GtkWidget    *image)
604 {
605     HildonButtonPrivate *priv;
606
607     g_return_if_fail (HILDON_IS_BUTTON (button));
608     g_return_if_fail (!image || GTK_IS_WIDGET (image));
609
610     priv = HILDON_BUTTON_GET_PRIVATE (button);
611
612     /* Return if there's nothing to do */
613     if (image == priv->image)
614         return;
615
616     if (priv->image && priv->image->parent)
617         gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
618
619     priv->image = image;
620
621     hildon_button_construct_child (button);
622 }
623
624 /**
625  * hildon_button_get_image:
626  * @button: a #HildonButton
627  *
628  * Gets the widget that is currenty set as the image of @button,
629  * previously set with hildon_button_set_image()
630  *
631  * Returns: a #GtkWidget or %NULL in case there is no image
632  **/
633 GtkWidget *
634 hildon_button_get_image                         (HildonButton *button)
635 {
636     HildonButtonPrivate *priv;
637
638     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
639
640     priv = HILDON_BUTTON_GET_PRIVATE (button);
641
642     return priv->image;
643 }
644
645 /**
646  * hildon_button_set_image_position:
647  * @button: a #HildonButton
648  * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
649  *
650  * Sets the position of the image inside @button. Only %GTK_POS_LEFT
651  * and %GTK_POS_RIGHT are currently supported.
652  **/
653 void
654 hildon_button_set_image_position                (HildonButton    *button,
655                                                  GtkPositionType  position)
656 {
657     HildonButtonPrivate *priv;
658
659     g_return_if_fail (HILDON_IS_BUTTON (button));
660     g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
661
662     priv = HILDON_BUTTON_GET_PRIVATE (button);
663
664     /* Return if there's nothing to do */
665     if (priv->image_position == position)
666         return;
667
668     priv->image_position = position;
669
670     hildon_button_construct_child (button);
671 }
672
673 /**
674  * hildon_button_set_alignment:
675  * @button: a #HildonButton
676  * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
677  * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
678  * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
679  * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
680  *
681  * Sets the alignment of the contents of the widget. If you don't need
682  * to change @xscale or @yscale you can just use
683  * gtk_button_set_alignment() instead.
684  **/
685 void
686 hildon_button_set_alignment                     (HildonButton *button,
687                                                  gfloat        xalign,
688                                                  gfloat        yalign,
689                                                  gfloat        xscale,
690                                                  gfloat        yscale)
691 {
692     HildonButtonPrivate *priv;
693     GtkWidget *child;
694
695     g_return_if_fail (HILDON_IS_BUTTON (button));
696
697     priv = HILDON_BUTTON_GET_PRIVATE (button);
698
699     child = gtk_bin_get_child (GTK_BIN (button));
700
701     if (GTK_IS_ALIGNMENT (child)) {
702         gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign);
703         g_object_set (child, "xscale", xscale, "yscale", yscale, NULL);
704     }
705 }
706
707 /**
708  * hildon_button_set_title_alignment:
709  * @button: a #HildonButton
710  * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
711  * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
712  *
713  * Sets the alignment of the title label. See also
714  * hildon_button_set_alignment() to set the alignment of the whole
715  * contents of the button.
716  **/
717 void
718 hildon_button_set_title_alignment               (HildonButton *button,
719                                                  gfloat        xalign,
720                                                  gfloat        yalign)
721 {
722     HildonButtonPrivate *priv;
723
724     g_return_if_fail (HILDON_IS_BUTTON (button));
725
726     priv = HILDON_BUTTON_GET_PRIVATE (button);
727
728     gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign);
729 }
730
731 /**
732  * hildon_button_set_value_alignment:
733  * @button: a #HildonButton
734  * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
735  * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
736  *
737  * Sets the alignment of the value label. See also
738  * hildon_button_set_alignment() to set the alignment of the whole
739  * contents of the button.
740  **/
741 void
742 hildon_button_set_value_alignment               (HildonButton *button,
743                                                  gfloat        xalign,
744                                                  gfloat        yalign)
745 {
746     HildonButtonPrivate *priv;
747
748     g_return_if_fail (HILDON_IS_BUTTON (button));
749
750     priv = HILDON_BUTTON_GET_PRIVATE (button);
751
752     gtk_misc_set_alignment (GTK_MISC (priv->value), xalign, yalign);
753 }
754
755 /**
756  * hildon_button_set_image_alignment:
757  * @button: a #HildonButton
758  * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
759  * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
760  *
761  * Sets the alignment of the image. See also
762  * hildon_button_set_alignment() to set the alignment of the whole
763  * contents of the button.
764  **/
765 void
766 hildon_button_set_image_alignment               (HildonButton *button,
767                                                  gfloat        xalign,
768                                                  gfloat        yalign)
769 {
770     HildonButtonPrivate *priv;
771
772     g_return_if_fail (HILDON_IS_BUTTON (button));
773
774     priv = HILDON_BUTTON_GET_PRIVATE (button);
775
776     /* Return if there's nothing to do */
777     if (priv->image_xalign == xalign && priv->image_yalign == yalign)
778         return;
779
780     priv->image_xalign = xalign;
781     priv->image_yalign = yalign;
782
783     hildon_button_construct_child (button);
784 }
785
786 static void
787 hildon_button_construct_child                   (HildonButton *button)
788 {
789     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
790     GtkWidget *child;
791     gint image_spacing;
792
793     /* Don't do anything if the button is not constructed yet */
794     if (priv->label_box == NULL)
795         return;
796
797     /* Save a ref to the image if necessary */
798     if (priv->image && priv->image->parent != NULL) {
799         g_object_ref (priv->image);
800         gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
801     }
802
803     /* Save a ref to the label box if necessary */
804     if (priv->label_box->parent != NULL) {
805         g_object_ref (priv->label_box);
806         gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
807     }
808
809     /* Remove the child from the container and add priv->alignment */
810     child = gtk_bin_get_child (GTK_BIN (button));
811     if (child != NULL && child != priv->alignment) {
812         gtk_container_remove (GTK_CONTAINER (button), child);
813         child = NULL;
814     }
815
816     if (child == NULL) {
817         gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
818     }
819
820     /* Create a new hbox */
821     if (priv->hbox) {
822         gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
823     }
824     gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
825     priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
826     gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
827
828     /* Pack the image and the alignment in the new hbox */
829     if (priv->image && priv->image_position == GTK_POS_LEFT)
830         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
831
832     gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
833
834     if (priv->image && priv->image_position == GTK_POS_RIGHT)
835         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
836
837     /* Set image alignment */
838     if (priv->image)
839         gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
840
841     /* Show everything */
842     gtk_widget_show_all (GTK_WIDGET (priv->alignment));
843 }