dd28e270701cfa7e7a40435350fced2127833e0d
[hildon] / hildon / 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: Rodrigo Novo <rodrigo.novo@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: Two-label buttons in the Hildon framework.
22  *
23  * The #HildonButton is a clickable button for Hildon applications.
24  * 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. The contents of the button are packed together inside a
35  * #GtkAlignment and they do not expand by default (they don't use the
36  * full space of the button).
37  *
38  * To change the alignment of both labels, use gtk_button_set_alignment()
39  *
40  * To make them expand and use the full space of the button, use
41  * hildon_button_set_alignment().
42  *
43  * To change the relative alignment of each label, use
44  * hildon_button_set_title_alignment() and
45  * hildon_button_set_value_alignment().
46  *
47  * In hildon-button-example.c included in the Hildon distribution you
48  * can see examples of how to create the most common button
49  * layouts.
50  *
51  * If only one label is needed, #GtkButton can be used as well, see
52  * also hildon_gtk_button_new().
53  *
54  * <example>
55  * <title>Creating a HildonButton</title>
56  * <programlisting>
57  * void
58  * button_clicked (HildonButton *button, gpointer user_data)
59  * {
60  *     const gchar *title, *value;
61  * <!-- -->
62  *     title = hildon_button_get_title (button);
63  *     value = hildon_button_get_value (button);
64  *     g_debug ("Button clicked with title '&percnt;s' and value '&percnt;s'", title, value);
65  * }
66  * <!-- -->
67  * GtkWidget *
68  * create_button (void)
69  * {
70  *     GtkWidget *button;
71  *     GtkWidget *image;
72  * <!-- -->
73  *     button = hildon_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
74  *                                 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
75  *     hildon_button_set_text (HILDON_BUTTON (button), "Some title", "Some value");
76  * <!-- -->
77  *     image = gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON);
78  *     hildon_button_set_image (HILDON_BUTTON (button), image);
79  *     hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
80  * <!-- -->
81  *     gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5);
82  * <!-- -->
83  *     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), NULL);
84  * <!-- -->
85  *     return button;
86  * }
87  * </programlisting>
88  * </example>
89  */
90
91 #include                                        "hildon-button.h"
92 #include                                        "hildon-enum-types.h"
93 #include                                        "hildon-gtk.h"
94 #include                                        "hildon-helper.h"
95
96 G_DEFINE_TYPE                                   (HildonButton, hildon_button, GTK_TYPE_BUTTON);
97
98 #define                                         HILDON_BUTTON_GET_PRIVATE(obj) \
99                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
100                                                 HILDON_TYPE_BUTTON, HildonButtonPrivate));
101
102 typedef struct                                  _HildonButtonPrivate HildonButtonPrivate;
103
104 struct                                          _HildonButtonPrivate
105 {
106     GtkLabel *title;
107     GtkLabel *value;
108     GtkBox *hbox;
109     GtkWidget *label_box;
110     GtkWidget *alignment;
111     GtkWidget *image;
112     GtkPositionType image_position;
113     gfloat image_xalign;
114     gfloat image_yalign;
115     HildonButtonStyle style;
116 };
117
118 enum {
119     PROP_TITLE = 1,
120     PROP_VALUE,
121     PROP_SIZE,
122     PROP_ARRANGEMENT,
123     PROP_STYLE
124 };
125
126 static void
127 hildon_button_set_arrangement                   (HildonButton            *button,
128                                                  HildonButtonArrangement  arrangement);
129
130 static void
131 hildon_button_construct_child                   (HildonButton *button);
132
133 static void
134 hildon_button_set_property                      (GObject      *object,
135                                                  guint         prop_id,
136                                                  const GValue *value,
137                                                  GParamSpec   *pspec)
138 {
139     HildonButton *button = HILDON_BUTTON (object);
140
141     switch (prop_id)
142     {
143     case PROP_TITLE:
144         hildon_button_set_title (button, g_value_get_string (value));
145         break;
146     case PROP_VALUE:
147         hildon_button_set_value (button, g_value_get_string (value));
148         break;
149     case PROP_SIZE:
150         hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
151         break;
152     case PROP_ARRANGEMENT:
153         hildon_button_set_arrangement (button, g_value_get_enum (value));
154         break;
155     case PROP_STYLE:
156         hildon_button_set_style (button, g_value_get_enum (value));
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_get_property                      (GObject    *object,
166                                                  guint       prop_id,
167                                                  GValue     *value,
168                                                  GParamSpec *pspec)
169 {
170     HildonButton *button = HILDON_BUTTON (object);
171
172     switch (prop_id)
173     {
174     case PROP_TITLE:
175         g_value_set_string (value, hildon_button_get_title (button));
176         break;
177     case PROP_VALUE:
178         g_value_set_string (value, hildon_button_get_value (button));
179         break;
180     case PROP_STYLE:
181         g_value_set_enum (value, hildon_button_get_style (button));
182         break;
183     default:
184         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185         break;
186     }
187 }
188
189 static void
190 hildon_button_style_set                         (GtkWidget *widget,
191                                                  GtkStyle  *previous_style)
192 {
193     guint horizontal_spacing, vertical_spacing, image_spacing;
194     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (widget);
195
196     if (GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set)
197         GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set (widget, previous_style);
198
199     gtk_widget_style_get (widget,
200                           "horizontal-spacing", &horizontal_spacing,
201                           "vertical-spacing", &vertical_spacing,
202                           "image-spacing", &image_spacing,
203                           NULL);
204
205     if (GTK_IS_HBOX (priv->label_box)) {
206         gtk_box_set_spacing (GTK_BOX (priv->label_box), horizontal_spacing);
207     } else {
208         gtk_box_set_spacing (GTK_BOX (priv->label_box), vertical_spacing);
209     }
210
211     if (GTK_IS_BOX (priv->hbox)) {
212         gtk_box_set_spacing (priv->hbox, image_spacing);
213     }
214 }
215
216 static void
217 hildon_button_finalize                          (GObject *object)
218 {
219     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (object);
220
221     g_object_unref (priv->alignment);
222     g_object_unref (priv->label_box);
223
224     G_OBJECT_CLASS (hildon_button_parent_class)->finalize (object);
225 }
226
227 static void
228 hildon_button_class_init                        (HildonButtonClass *klass)
229 {
230     GObjectClass *gobject_class = (GObjectClass *)klass;
231     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
232
233     gobject_class->set_property = hildon_button_set_property;
234     gobject_class->get_property = hildon_button_get_property;
235     gobject_class->finalize = hildon_button_finalize;
236     widget_class->style_set = hildon_button_style_set;
237
238     g_object_class_install_property (
239         gobject_class,
240         PROP_TITLE,
241         g_param_spec_string (
242             "title",
243             "Title",
244             "Text of the title label inside the button",
245             NULL,
246             G_PARAM_READWRITE));
247
248     g_object_class_install_property (
249         gobject_class,
250         PROP_VALUE,
251         g_param_spec_string (
252             "value",
253             "Value",
254             "Text of the value label inside the button",
255             NULL,
256             G_PARAM_READWRITE));
257
258     g_object_class_install_property (
259         gobject_class,
260         PROP_SIZE,
261         g_param_spec_flags (
262             "size",
263             "Size",
264             "Size request for the button",
265             HILDON_TYPE_SIZE_TYPE,
266             HILDON_SIZE_AUTO,
267             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
268
269     g_object_class_install_property (
270         gobject_class,
271         PROP_ARRANGEMENT,
272         g_param_spec_enum (
273             "arrangement",
274             "Arrangement",
275             "How the button contents must be arranged",
276             HILDON_TYPE_BUTTON_ARRANGEMENT,
277             HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
278             G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
279
280     g_object_class_install_property (
281         gobject_class,
282         PROP_STYLE,
283         g_param_spec_enum (
284             "style",
285             "Style",
286             "Visual style of the button",
287             HILDON_TYPE_BUTTON_STYLE,
288             HILDON_BUTTON_STYLE_NORMAL,
289             G_PARAM_READWRITE));
290
291     gtk_widget_class_install_style_property (
292         widget_class,
293         g_param_spec_uint (
294             "horizontal-spacing",
295             "Horizontal spacing between labels",
296             "Horizontal spacing between the title and value labels, when in horizontal mode",
297             0, G_MAXUINT, 25,
298             G_PARAM_READABLE));
299
300     gtk_widget_class_install_style_property (
301         widget_class,
302         g_param_spec_uint (
303             "vertical-spacing",
304             "Vertical spacing between labels",
305             "Vertical spacing between the title and value labels, when in vertical mode",
306             0, G_MAXUINT, 5,
307             G_PARAM_READABLE));
308
309     g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
310 }
311
312 static void
313 hildon_button_init                              (HildonButton *self)
314 {
315     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
316
317     priv->title = GTK_LABEL (gtk_label_new (NULL));
318     priv->value = GTK_LABEL (gtk_label_new (NULL));
319     priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
320     priv->image = NULL;
321     priv->image_position = GTK_POS_LEFT;
322     priv->image_xalign = 0.5;
323     priv->image_yalign = 0.5;
324     priv->hbox = NULL;
325     priv->label_box = NULL;
326
327     gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
328     gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
329
330     hildon_button_set_style (self, HILDON_BUTTON_STYLE_NORMAL);
331
332     gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
333     gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
334
335     g_object_ref_sink (priv->alignment);
336
337     /* The labels are not shown automatically, see hildon_button_set_(title|value) */
338     gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
339     gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
340
341     gtk_button_set_focus_on_click (GTK_BUTTON (self), FALSE);
342 }
343
344 /**
345  * hildon_button_add_title_size_group:
346  * @button: a #HildonButton
347  * @size_group: A #GtkSizeGroup for the button title (main label)
348  *
349  * Adds the title label of @button to @size_group.
350  *
351  * Since: 2.2
352  **/
353 void
354 hildon_button_add_title_size_group              (HildonButton *button,
355                                                  GtkSizeGroup *size_group)
356 {
357     HildonButtonPrivate *priv;
358
359     g_return_if_fail (HILDON_IS_BUTTON (button));
360     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
361
362     priv = HILDON_BUTTON_GET_PRIVATE (button);
363
364     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->title));
365 }
366
367 /**
368  * hildon_button_add_value_size_group:
369  * @button: a #HildonButton
370  * @size_group: A #GtkSizeGroup for the button value (secondary label)
371  *
372  * Adds the value label of @button to @size_group.
373  *
374  * Since: 2.2
375  **/
376 void
377 hildon_button_add_value_size_group              (HildonButton *button,
378                                                  GtkSizeGroup *size_group)
379 {
380     HildonButtonPrivate *priv;
381
382     g_return_if_fail (HILDON_IS_BUTTON (button));
383     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
384
385     priv = HILDON_BUTTON_GET_PRIVATE (button);
386
387     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->value));
388 }
389
390 /**
391  * hildon_button_add_image_size_group:
392  * @button: a #HildonButton
393  * @size_group: A #GtkSizeGroup for the button image
394  *
395  * Adds the image of @button to @size_group. You must add an image
396  * using hildon_button_set_image() before calling this function.
397  *
398  * Since: 2.2
399  **/
400 void
401 hildon_button_add_image_size_group              (HildonButton *button,
402                                                  GtkSizeGroup *size_group)
403 {
404     HildonButtonPrivate *priv;
405
406     g_return_if_fail (HILDON_IS_BUTTON (button));
407     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
408
409     priv = HILDON_BUTTON_GET_PRIVATE (button);
410
411     g_return_if_fail (GTK_IS_WIDGET (priv->image));
412
413     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->image));
414 }
415
416 /**
417  * hildon_button_add_size_groups:
418  * @button: a #HildonButton
419  * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
420  * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
421  * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
422  *
423  * Convenience function to add title, value and image to size
424  * groups. %NULL size groups will be ignored.
425  *
426  * Since: 2.2
427  **/
428 void
429 hildon_button_add_size_groups                   (HildonButton *button,
430                                                  GtkSizeGroup *title_size_group,
431                                                  GtkSizeGroup *value_size_group,
432                                                  GtkSizeGroup *image_size_group)
433 {
434     if (title_size_group)
435         hildon_button_add_title_size_group (button, title_size_group);
436
437     if (value_size_group)
438         hildon_button_add_value_size_group (button, value_size_group);
439
440     if (image_size_group)
441         hildon_button_add_image_size_group (button, image_size_group);
442 }
443
444 /**
445  * hildon_button_new:
446  * @size: Flags to set the size of the button.
447  * @arrangement: How the labels must be arranged.
448  *
449  * Creates a new #HildonButton. To set text in the labels, use
450  * hildon_button_set_title() and
451  * hildon_button_set_value(). Alternatively, you can add a custom
452  * child widget using gtk_container_add().
453  *
454  * Returns: a new #HildonButton
455  *
456  * Since: 2.2
457  **/
458 GtkWidget *
459 hildon_button_new                               (HildonSizeType          size,
460                                                  HildonButtonArrangement arrangement)
461 {
462     return hildon_button_new_with_text (size, arrangement, NULL, NULL);
463 }
464
465 /**
466  * hildon_button_new_with_text:
467  * @size: Flags to set the size of the button.
468  * @arrangement: How the labels must be arranged.
469  * @title: Title of the button (main label), or %NULL
470  * @value: Value of the button (secondary label), or %NULL
471  *
472  * Creates a new #HildonButton with two labels, @title and @value.
473  *
474  * If you just don't want to use one of the labels, set it to
475  * %NULL. You can set it to a non-%NULL value at any time later using
476  * hildon_button_set_title() or hildon_button_set_value() .
477  *
478  * Returns: a new #HildonButton
479  *
480  * Since: 2.2
481  **/
482 GtkWidget *
483 hildon_button_new_with_text                     (HildonSizeType           size,
484                                                  HildonButtonArrangement  arrangement,
485                                                  const gchar             *title,
486                                                  const gchar             *value)
487 {
488     GtkWidget *button;
489
490     /* Create widget */
491     button = g_object_new (HILDON_TYPE_BUTTON,
492                            "size", size,
493                            "title", title,
494                            "value", value,
495                            "arrangement", arrangement,
496                            NULL);
497
498     return button;
499 }
500
501 static void
502 hildon_button_set_arrangement                   (HildonButton            *button,
503                                                  HildonButtonArrangement  arrangement)
504 {
505     HildonButtonPrivate *priv;
506
507     priv = HILDON_BUTTON_GET_PRIVATE (button);
508
509     /* Pack everything */
510     if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
511         priv->label_box = gtk_vbox_new (FALSE, 0);
512         hildon_helper_set_logical_font (GTK_WIDGET (priv->value), "SmallSystemFont");
513     } else {
514         priv->label_box = gtk_hbox_new (FALSE, 0);
515     }
516
517     g_object_ref_sink (priv->label_box);
518
519     /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
520      * can be painted outside of the button in some situations, see
521      * NB#88126 and NB#110689 */
522     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), FALSE, FALSE, 0);
523     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), TRUE, TRUE, 0);
524
525     hildon_button_construct_child (button);
526 }
527
528 /**
529  * hildon_button_set_title:
530  * @button: a #HildonButton
531  * @title: a new title (main label) for the button, or %NULL
532  *
533  * Sets the title (main label) of @button to @title.
534  *
535  * This will clear any previously set title.
536  *
537  * If @title is set to %NULL or an empty string, the title label will
538  * be hidden and the value label will be realigned.
539  *
540  * Since: 2.2
541  **/
542 void
543 hildon_button_set_title                         (HildonButton *button,
544                                                  const gchar  *title)
545 {
546     HildonButtonPrivate *priv;
547
548     g_return_if_fail (HILDON_IS_BUTTON (button));
549
550     priv = HILDON_BUTTON_GET_PRIVATE (button);
551     gtk_label_set_text (priv->title, title);
552
553     /* If the button has no title, hide the label so the value is
554      * properly aligned */
555     if (title && title[0] != '\0') {
556         hildon_button_construct_child (button);
557         gtk_widget_show (GTK_WIDGET (priv->title));
558     } else {
559         gtk_widget_hide (GTK_WIDGET (priv->title));
560     }
561
562     g_object_notify (G_OBJECT (button), "title");
563 }
564
565 /**
566  * hildon_button_set_value:
567  * @button: a #HildonButton
568  * @value: a new value (secondary label) for the button, or %NULL
569  *
570  * Sets the value (secondary label) of @button to @value.
571  *
572  * This will clear any previously set value.
573  *
574  * If @value is set to %NULL or an empty string, the value label will
575  * be hidden and the title label will be realigned.
576  *
577  * Since: 2.2
578  **/
579 void
580 hildon_button_set_value                         (HildonButton *button,
581                                                  const gchar  *value)
582 {
583     HildonButtonPrivate *priv;
584
585     g_return_if_fail (HILDON_IS_BUTTON (button));
586
587     priv = HILDON_BUTTON_GET_PRIVATE (button);
588     gtk_label_set_text (priv->value, value);
589
590     /* If the button has no value, hide the label so the title is
591      * properly aligned */
592     if (value && value[0] != '\0') {
593         hildon_button_construct_child (button);
594         gtk_widget_show (GTK_WIDGET (priv->value));
595     } else {
596         gtk_widget_hide (GTK_WIDGET (priv->value));
597     }
598
599     g_object_notify (G_OBJECT (button), "value");
600 }
601
602 /**
603  * hildon_button_get_title:
604  * @button: a #HildonButton
605  *
606  * Fetches the text from the main label (title) of @button,
607  * as set by hildon_button_set_title() or hildon_button_set_text().
608  * If the label text has not been set the return value will be %NULL.
609  * This will be the case if you create an empty button with
610  * hildon_button_new() to use as a container.
611  *
612  * Returns: The text of the title label. This string is owned by the
613  * widget and must not be modified or freed.
614  *
615  * Since: 2.2
616  **/
617 const gchar *
618 hildon_button_get_title                         (HildonButton *button)
619 {
620     HildonButtonPrivate *priv;
621
622     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
623
624     priv = HILDON_BUTTON_GET_PRIVATE (button);
625
626     return gtk_label_get_text (priv->title);
627 }
628
629 /**
630  * hildon_button_get_value:
631  * @button: a #HildonButton
632  *
633  * Fetches the text from the secondary label (value) of @button,
634  * as set by hildon_button_set_value() or hildon_button_set_text().
635  * If the label text has not been set the return value will be %NULL.
636  * This will be the case if you create an empty button with hildon_button_new()
637  * to use as a container.
638  *
639  * Returns: The text of the value label. This string is owned by the
640  * widget and must not be modified or freed.
641  *
642  * Since: 2.2
643  **/
644 const gchar *
645 hildon_button_get_value                         (HildonButton *button)
646 {
647     HildonButtonPrivate *priv;
648
649     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
650
651     priv = HILDON_BUTTON_GET_PRIVATE (button);
652
653     return gtk_label_get_text (priv->value);
654 }
655
656 /**
657  * hildon_button_set_text:
658  * @button: a #HildonButton
659  * @title: new text for the button title (main label)
660  * @value: new text for the button value (secondary label)
661  *
662  * Convenience function to change both labels of a #HildonButton
663  *
664  * Since: 2.2
665  **/
666 void
667 hildon_button_set_text                          (HildonButton *button,
668                                                  const gchar  *title,
669                                                  const gchar  *value)
670 {
671     hildon_button_set_title (button, title);
672     hildon_button_set_value (button, value);
673 }
674
675 /**
676  * hildon_button_set_image:
677  * @button: a #HildonButton
678  * @image: a widget to set as the button image
679  *
680  * Sets the image of @button to the given widget. The previous image
681  * (if any) will be removed.
682  *
683  * Since: 2.2
684  **/
685 void
686 hildon_button_set_image                         (HildonButton *button,
687                                                  GtkWidget    *image)
688 {
689     HildonButtonPrivate *priv;
690
691     g_return_if_fail (HILDON_IS_BUTTON (button));
692     g_return_if_fail (!image || GTK_IS_WIDGET (image));
693
694     priv = HILDON_BUTTON_GET_PRIVATE (button);
695
696     /* Return if there's nothing to do */
697     if (image == priv->image)
698         return;
699
700     if (priv->image && priv->image->parent)
701         gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
702
703     priv->image = image;
704
705     hildon_button_construct_child (button);
706 }
707
708 /**
709  * hildon_button_get_image:
710  * @button: a #HildonButton
711  *
712  * Gets the widget that is currenty set as the image of @button,
713  * previously set with hildon_button_set_image()
714  *
715  * Returns: a #GtkWidget or %NULL in case there is no image
716  *
717  * Since: 2.2
718  **/
719 GtkWidget *
720 hildon_button_get_image                         (HildonButton *button)
721 {
722     HildonButtonPrivate *priv;
723
724     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
725
726     priv = HILDON_BUTTON_GET_PRIVATE (button);
727
728     return priv->image;
729 }
730
731 /**
732  * hildon_button_set_image_position:
733  * @button: a #HildonButton
734  * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
735  *
736  * Sets the position of the image inside @button. Only %GTK_POS_LEFT
737  * and %GTK_POS_RIGHT are currently supported.
738  *
739  * Since: 2.2
740  **/
741 void
742 hildon_button_set_image_position                (HildonButton    *button,
743                                                  GtkPositionType  position)
744 {
745     HildonButtonPrivate *priv;
746
747     g_return_if_fail (HILDON_IS_BUTTON (button));
748     g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
749
750     priv = HILDON_BUTTON_GET_PRIVATE (button);
751
752     /* Return if there's nothing to do */
753     if (priv->image_position == position)
754         return;
755
756     priv->image_position = position;
757
758     hildon_button_construct_child (button);
759 }
760
761 /**
762  * hildon_button_set_alignment:
763  * @button: a #HildonButton
764  * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
765  * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
766  * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
767  * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
768  *
769  * Sets the alignment of the contents of the widget. If you don't need
770  * to change @xscale or @yscale you can just use
771  * gtk_button_set_alignment() instead.
772  *
773  * Note that for this method to work properly the, child widget of
774  * @button must be a #GtkAlignment. That's what #HildonButton uses by
775  * default, so this function will work unless you add a custom widget
776  * to @button.
777  *
778  * Since: 2.2
779  **/
780 void
781 hildon_button_set_alignment                     (HildonButton *button,
782                                                  gfloat        xalign,
783                                                  gfloat        yalign,
784                                                  gfloat        xscale,
785                                                  gfloat        yscale)
786 {
787     HildonButtonPrivate *priv;
788     GtkWidget *child;
789
790     g_return_if_fail (HILDON_IS_BUTTON (button));
791
792     priv = HILDON_BUTTON_GET_PRIVATE (button);
793
794     child = gtk_bin_get_child (GTK_BIN (button));
795
796     /* If the button has no child, use priv->alignment, which is the default one */
797     if (child == NULL)
798         child = priv->alignment;
799
800     if (GTK_IS_ALIGNMENT (child)) {
801         gtk_alignment_set (GTK_ALIGNMENT (priv->alignment), xalign, yalign, xscale, yscale);
802     }
803 }
804
805 /**
806  * hildon_button_set_title_alignment:
807  * @button: a #HildonButton
808  * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
809  * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
810  *
811  * Sets the alignment of the title label. See also
812  * hildon_button_set_alignment() to set the alignment of the whole
813  * contents of the button.
814  *
815  * Since: 2.2
816  **/
817 void
818 hildon_button_set_title_alignment               (HildonButton *button,
819                                                  gfloat        xalign,
820                                                  gfloat        yalign)
821 {
822     HildonButtonPrivate *priv;
823
824     g_return_if_fail (HILDON_IS_BUTTON (button));
825
826     priv = HILDON_BUTTON_GET_PRIVATE (button);
827
828     gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign);
829 }
830
831 /**
832  * hildon_button_set_value_alignment:
833  * @button: a #HildonButton
834  * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
835  * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
836  *
837  * Sets the alignment of the value label. See also
838  * hildon_button_set_alignment() to set the alignment of the whole
839  * contents of the button.
840  *
841  * Since: 2.2
842  **/
843 void
844 hildon_button_set_value_alignment               (HildonButton *button,
845                                                  gfloat        xalign,
846                                                  gfloat        yalign)
847 {
848     HildonButtonPrivate *priv;
849
850     g_return_if_fail (HILDON_IS_BUTTON (button));
851
852     priv = HILDON_BUTTON_GET_PRIVATE (button);
853
854     gtk_misc_set_alignment (GTK_MISC (priv->value), xalign, yalign);
855 }
856
857 /**
858  * hildon_button_set_image_alignment:
859  * @button: a #HildonButton
860  * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
861  * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
862  *
863  * Sets the alignment of the image. See also
864  * hildon_button_set_alignment() to set the alignment of the whole
865  * contents of the button.
866  *
867  * Since: 2.2
868  **/
869 void
870 hildon_button_set_image_alignment               (HildonButton *button,
871                                                  gfloat        xalign,
872                                                  gfloat        yalign)
873 {
874     HildonButtonPrivate *priv;
875
876     g_return_if_fail (HILDON_IS_BUTTON (button));
877
878     priv = HILDON_BUTTON_GET_PRIVATE (button);
879
880     /* Return if there's nothing to do */
881     if (priv->image_xalign == xalign && priv->image_yalign == yalign)
882         return;
883
884     priv->image_xalign = xalign;
885     priv->image_yalign = yalign;
886
887     hildon_button_construct_child (button);
888 }
889
890 /**
891  * hildon_button_set_style:
892  * @button: A #HildonButton
893  * @style: A #HildonButtonStyle for @button
894  *
895  * Sets the style of @button to @style. This changes the visual
896  * appearance of the button (colors, font sizes) according to the
897  * particular style chosen, but the general layout is not altered.
898  *
899  * Use %HILDON_BUTTON_STYLE_NORMAL to make it look like a normal
900  * #HildonButton, or %HILDON_BUTTON_STYLE_PICKER to make it look like
901  * a #HildonPickerButton.
902  *
903  * Since: 2.2
904  */
905 void
906 hildon_button_set_style                         (HildonButton      *button,
907                                                  HildonButtonStyle  style)
908 {
909     HildonButtonPrivate *priv;
910     const gchar *color;
911
912     g_return_if_fail (HILDON_IS_BUTTON (button));
913
914     switch (style) {
915     case HILDON_BUTTON_STYLE_NORMAL:
916         color = "SecondaryTextColor";
917         break;
918     case HILDON_BUTTON_STYLE_PICKER:
919         color = "ActiveTextColor";
920         break;
921     default:
922         g_return_if_reached ();
923     }
924
925     priv = HILDON_BUTTON_GET_PRIVATE (button);
926
927     hildon_helper_set_logical_color (GTK_WIDGET (priv->value), GTK_RC_FG, GTK_STATE_NORMAL, color);
928     hildon_helper_set_logical_color (GTK_WIDGET (priv->value), GTK_RC_FG, GTK_STATE_PRELIGHT, color);
929
930     priv->style = style;
931
932     g_object_notify (G_OBJECT (button), "style");
933 }
934
935 /**
936  * hildon_button_get_style:
937  * @button: A #HildonButton
938  *
939  * Gets the visual style of the button.
940  *
941  * Returns: a #HildonButtonStyle
942  *
943  * Since: 2.2
944  */
945 HildonButtonStyle
946 hildon_button_get_style                         (HildonButton *button)
947 {
948     HildonButtonPrivate *priv;
949
950     g_return_val_if_fail (HILDON_IS_BUTTON (button), HILDON_BUTTON_STYLE_NORMAL);
951
952     priv = HILDON_BUTTON_GET_PRIVATE (button);
953
954     return priv->style;
955 }
956
957 static void
958 hildon_button_construct_child                   (HildonButton *button)
959 {
960     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
961     GtkWidget *child;
962     gint image_spacing;
963     const gchar *title, *value;
964
965     /* Don't do anything if the button is not constructed yet */
966     if (G_UNLIKELY (priv->label_box == NULL))
967         return;
968
969     /* Don't do anything if the button has no contents */
970     title = gtk_label_get_text (priv->title);
971     value = gtk_label_get_text (priv->value);
972     if (!priv->image && !title[0] && !value[0])
973         return;
974
975     /* Save a ref to the image, and remove it from its container if necessary */
976     if (priv->image) {
977         g_object_ref (priv->image);
978         if (priv->image->parent != NULL)
979             gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
980     }
981
982     if (priv->label_box->parent != NULL) {
983         gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
984     }
985
986     /* Remove the child from the container and add priv->alignment */
987     child = gtk_bin_get_child (GTK_BIN (button));
988     if (child != NULL && child != priv->alignment) {
989         gtk_container_remove (GTK_CONTAINER (button), child);
990         child = NULL;
991     }
992
993     if (child == NULL) {
994         gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
995     }
996
997     /* Create a new hbox */
998     if (priv->hbox) {
999         gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
1000     }
1001     gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
1002     priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
1003     gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
1004
1005     /* Pack the image and the alignment in the new hbox */
1006     if (priv->image && priv->image_position == GTK_POS_LEFT)
1007         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
1008
1009     gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
1010
1011     if (priv->image && priv->image_position == GTK_POS_RIGHT)
1012         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
1013
1014     /* Set image alignment and remove previously set ref */
1015     if (priv->image) {
1016         gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
1017         g_object_unref (priv->image);
1018     }
1019
1020     /* Show everything */
1021     gtk_widget_show_all (GTK_WIDGET (priv->alignment));
1022 }