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