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