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