2009-01-13 Claudio Saavedra <csaavedra@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: 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: 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. The contents of the button are packed together and they
35  * do not expand by default (they don't use the full space of the
36  * 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 | G_PARAM_CONSTRUCT));
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 | G_PARAM_CONSTRUCT));
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     gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
331     gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
332
333     g_object_ref_sink (priv->alignment);
334
335     /* The labels are not shown automatically, see hildon_button_set_(title|value) */
336     gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
337     gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
338 }
339
340 /**
341  * hildon_button_add_title_size_group:
342  * @button: a #HildonButton
343  * @size_group: A #GtkSizeGroup for the button title (main label)
344  *
345  * Adds the title label of @button to @size_group.
346  **/
347 void
348 hildon_button_add_title_size_group              (HildonButton *button,
349                                                  GtkSizeGroup *size_group)
350 {
351     HildonButtonPrivate *priv;
352
353     g_return_if_fail (HILDON_IS_BUTTON (button));
354     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
355
356     priv = HILDON_BUTTON_GET_PRIVATE (button);
357
358     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->title));
359 }
360
361 /**
362  * hildon_button_add_value_size_group:
363  * @button: a #HildonButton
364  * @size_group: A #GtkSizeGroup for the button value (secondary label)
365  *
366  * Adds the value label of @button to @size_group.
367  **/
368 void
369 hildon_button_add_value_size_group              (HildonButton *button,
370                                                  GtkSizeGroup *size_group)
371 {
372     HildonButtonPrivate *priv;
373
374     g_return_if_fail (HILDON_IS_BUTTON (button));
375     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
376
377     priv = HILDON_BUTTON_GET_PRIVATE (button);
378
379     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->value));
380 }
381
382 /**
383  * hildon_button_add_image_size_group:
384  * @button: a #HildonButton
385  * @size_group: A #GtkSizeGroup for the button image
386  *
387  * Adds the image of @button to @size_group. You must add an image
388  * using hildon_button_set_image() before calling this function.
389  **/
390 void
391 hildon_button_add_image_size_group              (HildonButton *button,
392                                                  GtkSizeGroup *size_group)
393 {
394     HildonButtonPrivate *priv;
395
396     g_return_if_fail (HILDON_IS_BUTTON (button));
397     g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
398
399     priv = HILDON_BUTTON_GET_PRIVATE (button);
400
401     g_return_if_fail (GTK_IS_WIDGET (priv->image));
402
403     gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->image));
404 }
405
406 /**
407  * hildon_button_add_size_groups:
408  * @button: a #HildonButton
409  * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
410  * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
411  * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
412  *
413  * Convenience function to add title, value and image to size
414  * groups. %NULL size groups will be ignored.
415  **/
416 void
417 hildon_button_add_size_groups                   (HildonButton *button,
418                                                  GtkSizeGroup *title_size_group,
419                                                  GtkSizeGroup *value_size_group,
420                                                  GtkSizeGroup *image_size_group)
421 {
422     if (title_size_group)
423         hildon_button_add_title_size_group (button, title_size_group);
424
425     if (value_size_group)
426         hildon_button_add_value_size_group (button, value_size_group);
427
428     if (image_size_group)
429         hildon_button_add_image_size_group (button, image_size_group);
430 }
431
432 /**
433  * hildon_button_new:
434  * @size: Flags to set the size of the button.
435  * @arrangement: How the labels must be arranged.
436  *
437  * Creates a new #HildonButton. To set text in the labels, use
438  * hildon_button_set_title() and
439  * hildon_button_set_value(). Alternatively, you can add a custom
440  * child widget using gtk_container_add().
441  *
442  * Returns: a new #HildonButton
443  *
444  * Since: 2.2
445  **/
446 GtkWidget *
447 hildon_button_new                               (HildonSizeType          size,
448                                                  HildonButtonArrangement arrangement)
449 {
450     return hildon_button_new_with_text (size, arrangement, NULL, NULL);
451 }
452
453 /**
454  * hildon_button_new_with_text:
455  * @size: Flags to set the size of the button.
456  * @arrangement: How the labels must be arranged.
457  * @title: Title of the button (main label), or %NULL
458  * @value: Value of the button (secondary label), or %NULL
459  *
460  * Creates a new #HildonButton with two labels, @title and @value.
461  *
462  * If you just don't want to use one of the labels, set it to
463  * %NULL. You can set it to a non-%NULL value at any time later using
464  * hildon_button_set_title() or hildon_button_set_value() .
465  *
466  * Returns: a new #HildonButton
467  *
468  * Since: 2.2
469  **/
470 GtkWidget *
471 hildon_button_new_with_text                     (HildonSizeType           size,
472                                                  HildonButtonArrangement  arrangement,
473                                                  const gchar             *title,
474                                                  const gchar             *value)
475 {
476     GtkWidget *button;
477
478     /* Create widget */
479     button = g_object_new (HILDON_TYPE_BUTTON,
480                            "size", size,
481                            "title", title,
482                            "value", value,
483                            "arrangement", arrangement,
484                            NULL);
485
486     return button;
487 }
488
489 static void
490 hildon_button_set_arrangement                   (HildonButton            *button,
491                                                  HildonButtonArrangement  arrangement)
492 {
493     HildonButtonPrivate *priv;
494
495     priv = HILDON_BUTTON_GET_PRIVATE (button);
496
497     /* Pack everything */
498     if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
499         priv->label_box = gtk_vbox_new (FALSE, 0);
500         hildon_helper_set_logical_font (GTK_WIDGET (priv->value), "SmallSystemFont");
501     } else {
502         priv->label_box = gtk_hbox_new (FALSE, 0);
503     }
504
505     g_object_ref_sink (priv->label_box);
506
507     /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
508      * can be painted outside of the button in some situations, see
509      * NB#88126 */
510     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
511     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
512
513     hildon_button_construct_child (button);
514 }
515
516 /**
517  * hildon_button_set_title:
518  * @button: a #HildonButton
519  * @title: a new title (main label) for the button, or %NULL
520  *
521  * Sets the title (main label) of @button to @title.
522  *
523  * This will clear any previously set title.
524  *
525  * If @title is set to %NULL, the title label will be hidden and the
526  * value label will be realigned.
527  *
528  * Since: 2.2
529  **/
530 void
531 hildon_button_set_title                         (HildonButton *button,
532                                                  const gchar  *title)
533 {
534     HildonButtonPrivate *priv;
535
536     g_return_if_fail (HILDON_IS_BUTTON (button));
537
538     priv = HILDON_BUTTON_GET_PRIVATE (button);
539     gtk_label_set_text (priv->title, title);
540
541     /* If the button has no title, hide the label so the value is
542      * properly aligned */
543     if (title) {
544         hildon_button_construct_child (button);
545         gtk_widget_show (GTK_WIDGET (priv->title));
546     } else {
547         gtk_widget_hide (GTK_WIDGET (priv->title));
548     }
549
550     g_object_notify (G_OBJECT (button), "title");
551 }
552
553 /**
554  * hildon_button_set_value:
555  * @button: a #HildonButton
556  * @value: a new value (secondary label) for the button, or %NULL
557  *
558  * Sets the value (secondary label) of @button to @value.
559  *
560  * This will clear any previously set value.
561  *
562  * If @value is set to %NULL, the value label will be hidden and the
563  * title label will be realigned.
564  *
565  *
566  * Since: 2.2
567  **/
568 void
569 hildon_button_set_value                         (HildonButton *button,
570                                                  const gchar  *value)
571 {
572     HildonButtonPrivate *priv;
573
574     g_return_if_fail (HILDON_IS_BUTTON (button));
575
576     priv = HILDON_BUTTON_GET_PRIVATE (button);
577     gtk_label_set_text (priv->value, value);
578
579     /* If the button has no value, hide the label so the title is
580      * properly aligned */
581     if (value) {
582         hildon_button_construct_child (button);
583         gtk_widget_show (GTK_WIDGET (priv->value));
584     } else {
585         gtk_widget_hide (GTK_WIDGET (priv->value));
586     }
587
588     g_object_notify (G_OBJECT (button), "value");
589 }
590
591 /**
592  * hildon_button_get_title:
593  * @button: a #HildonButton
594  *
595  * Fetches the text from the main label (title) of @button,
596  * as set by hildon_button_set_title() or hildon_button_set_text().
597  * If the label text has not been set the return value will be %NULL.
598  * This will be the case if you create an empty button with
599  * hildon_button_new() to use as a container.
600  *
601  * Returns: The text of the title label. This string is owned by the
602  * widget and must not be modified or freed.
603  *
604  * Since: 2.2
605  **/
606 const gchar *
607 hildon_button_get_title                         (HildonButton *button)
608 {
609     HildonButtonPrivate *priv;
610
611     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
612
613     priv = HILDON_BUTTON_GET_PRIVATE (button);
614
615     return gtk_label_get_text (priv->title);
616 }
617
618 /**
619  * hildon_button_get_value:
620  * @button: a #HildonButton
621  *
622  * Fetches the text from the secondary label (value) of @button,
623  * as set by hildon_button_set_value() or hildon_button_set_text().
624  * If the label text has not been set the return value will be %NULL.
625  * This will be the case if you create an empty button with hildon_button_new()
626  * to use as a container.
627  *
628  * Returns: The text of the value label. This string is owned by the
629  * widget and must not be modified or freed.
630  *
631  * Since: 2.2
632  **/
633 const gchar *
634 hildon_button_get_value                         (HildonButton *button)
635 {
636     HildonButtonPrivate *priv;
637
638     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
639
640     priv = HILDON_BUTTON_GET_PRIVATE (button);
641
642     return gtk_label_get_text (priv->value);
643 }
644
645 /**
646  * hildon_button_set_text:
647  * @button: a #HildonButton
648  * @title: new text for the button title (main label)
649  * @value: new text for the button value (secondary label)
650  *
651  * Convenience function to change both labels of a #HildonButton
652  *
653  * Since: 2.2
654  **/
655 void
656 hildon_button_set_text                          (HildonButton *button,
657                                                  const gchar  *title,
658                                                  const gchar  *value)
659 {
660     hildon_button_set_title (button, title);
661     hildon_button_set_value (button, value);
662 }
663
664 /**
665  * hildon_button_set_image:
666  * @button: a #HildonButton
667  * @image: a widget to set as the button image
668  *
669  * Sets the image of @button to the given widget. The previous image
670  * (if any) will be removed.
671  *
672  * Since: 2.2
673  **/
674 void
675 hildon_button_set_image                         (HildonButton *button,
676                                                  GtkWidget    *image)
677 {
678     HildonButtonPrivate *priv;
679
680     g_return_if_fail (HILDON_IS_BUTTON (button));
681     g_return_if_fail (!image || GTK_IS_WIDGET (image));
682
683     priv = HILDON_BUTTON_GET_PRIVATE (button);
684
685     /* Return if there's nothing to do */
686     if (image == priv->image)
687         return;
688
689     if (priv->image && priv->image->parent)
690         gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
691
692     priv->image = image;
693
694     hildon_button_construct_child (button);
695 }
696
697 /**
698  * hildon_button_get_image:
699  * @button: a #HildonButton
700  *
701  * Gets the widget that is currenty set as the image of @button,
702  * previously set with hildon_button_set_image()
703  *
704  * Returns: a #GtkWidget or %NULL in case there is no image
705  *
706  * Since: 2.2
707  **/
708 GtkWidget *
709 hildon_button_get_image                         (HildonButton *button)
710 {
711     HildonButtonPrivate *priv;
712
713     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
714
715     priv = HILDON_BUTTON_GET_PRIVATE (button);
716
717     return priv->image;
718 }
719
720 /**
721  * hildon_button_set_image_position:
722  * @button: a #HildonButton
723  * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
724  *
725  * Sets the position of the image inside @button. Only %GTK_POS_LEFT
726  * and %GTK_POS_RIGHT are currently supported.
727  *
728  * Since: 2.2
729  **/
730 void
731 hildon_button_set_image_position                (HildonButton    *button,
732                                                  GtkPositionType  position)
733 {
734     HildonButtonPrivate *priv;
735
736     g_return_if_fail (HILDON_IS_BUTTON (button));
737     g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
738
739     priv = HILDON_BUTTON_GET_PRIVATE (button);
740
741     /* Return if there's nothing to do */
742     if (priv->image_position == position)
743         return;
744
745     priv->image_position = position;
746
747     hildon_button_construct_child (button);
748 }
749
750 /**
751  * hildon_button_set_alignment:
752  * @button: a #HildonButton
753  * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
754  * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
755  * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
756  * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
757  *
758  * Sets the alignment of the contents of the widget. If you don't need
759  * to change @xscale or @yscale you can just use
760  * gtk_button_set_alignment() instead.
761  *
762  * Since: 2.2
763  **/
764 void
765 hildon_button_set_alignment                     (HildonButton *button,
766                                                  gfloat        xalign,
767                                                  gfloat        yalign,
768                                                  gfloat        xscale,
769                                                  gfloat        yscale)
770 {
771     HildonButtonPrivate *priv;
772     GtkWidget *child;
773
774     g_return_if_fail (HILDON_IS_BUTTON (button));
775
776     priv = HILDON_BUTTON_GET_PRIVATE (button);
777
778     child = gtk_bin_get_child (GTK_BIN (button));
779
780     if (GTK_IS_ALIGNMENT (child)) {
781         gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign);
782         g_object_set (child, "xscale", xscale, "yscale", yscale, NULL);
783     }
784 }
785
786 /**
787  * hildon_button_set_title_alignment:
788  * @button: a #HildonButton
789  * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
790  * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
791  *
792  * Sets the alignment of the title label. See also
793  * hildon_button_set_alignment() to set the alignment of the whole
794  * contents of the button.
795  *
796  * Since: 2.2
797  **/
798 void
799 hildon_button_set_title_alignment               (HildonButton *button,
800                                                  gfloat        xalign,
801                                                  gfloat        yalign)
802 {
803     HildonButtonPrivate *priv;
804
805     g_return_if_fail (HILDON_IS_BUTTON (button));
806
807     priv = HILDON_BUTTON_GET_PRIVATE (button);
808
809     gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign);
810 }
811
812 /**
813  * hildon_button_set_value_alignment:
814  * @button: a #HildonButton
815  * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
816  * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
817  *
818  * Sets the alignment of the value label. See also
819  * hildon_button_set_alignment() to set the alignment of the whole
820  * contents of the button.
821  *
822  * Since: 2.2
823  **/
824 void
825 hildon_button_set_value_alignment               (HildonButton *button,
826                                                  gfloat        xalign,
827                                                  gfloat        yalign)
828 {
829     HildonButtonPrivate *priv;
830
831     g_return_if_fail (HILDON_IS_BUTTON (button));
832
833     priv = HILDON_BUTTON_GET_PRIVATE (button);
834
835     gtk_misc_set_alignment (GTK_MISC (priv->value), xalign, yalign);
836 }
837
838 /**
839  * hildon_button_set_image_alignment:
840  * @button: a #HildonButton
841  * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
842  * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
843  *
844  * Sets the alignment of the image. See also
845  * hildon_button_set_alignment() to set the alignment of the whole
846  * contents of the button.
847  *
848  * Since: 2.2
849  **/
850 void
851 hildon_button_set_image_alignment               (HildonButton *button,
852                                                  gfloat        xalign,
853                                                  gfloat        yalign)
854 {
855     HildonButtonPrivate *priv;
856
857     g_return_if_fail (HILDON_IS_BUTTON (button));
858
859     priv = HILDON_BUTTON_GET_PRIVATE (button);
860
861     /* Return if there's nothing to do */
862     if (priv->image_xalign == xalign && priv->image_yalign == yalign)
863         return;
864
865     priv->image_xalign = xalign;
866     priv->image_yalign = yalign;
867
868     hildon_button_construct_child (button);
869 }
870
871 /**
872  * hildon_button_set_style:
873  * @button: A #HildonButton
874  * @style: A #HildonButtonStyle for @button
875  *
876  * Sets the style of @button to @style. This changes the visual
877  * appearance of the button (colors, font sizes) according to the
878  * particular style chosen, but the general layout is not altered.
879  *
880  * Use %HILDON_BUTTON_STYLE_NORMAL to make it look like a normal
881  * #HildonButton, or %HILDON_BUTTON_STYLE_PICKER to make it look like
882  * a #HildonPickerButton.
883  *
884  * Since: 2.2
885  */
886 void
887 hildon_button_set_style                         (HildonButton      *button,
888                                                  HildonButtonStyle  style)
889 {
890     HildonButtonPrivate *priv;
891     const gchar *color;
892
893     g_return_if_fail (HILDON_IS_BUTTON (button));
894
895     switch (style) {
896     case HILDON_BUTTON_STYLE_NORMAL:
897         color = "SecondaryTextColor";
898         break;
899     case HILDON_BUTTON_STYLE_PICKER:
900         color = "ActiveTextColor";
901         break;
902     default:
903         g_return_if_reached ();
904     }
905
906     priv = HILDON_BUTTON_GET_PRIVATE (button);
907
908     hildon_helper_set_logical_color (GTK_WIDGET (priv->value), GTK_RC_FG, GTK_STATE_NORMAL, color);
909     hildon_helper_set_logical_color (GTK_WIDGET (priv->value), GTK_RC_FG, GTK_STATE_PRELIGHT, color);
910
911     priv->style = style;
912
913     g_object_notify (G_OBJECT (button), "style");
914 }
915
916 /**
917  * hildon_button_get_style:
918  * @button: A #HildonButton
919  *
920  * Gets the visual style of the button.
921  *
922  * Returns: a #HildonButtonStyle
923  *
924  * Since: 2.2
925  */
926 HildonButtonStyle
927 hildon_button_get_style                         (HildonButton *button)
928 {
929     HildonButtonPrivate *priv;
930
931     g_return_val_if_fail (HILDON_IS_BUTTON (button), HILDON_BUTTON_STYLE_NORMAL);
932
933     priv = HILDON_BUTTON_GET_PRIVATE (button);
934
935     return priv->style;
936 }
937
938 static void
939 hildon_button_construct_child                   (HildonButton *button)
940 {
941     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
942     GtkWidget *child;
943     gint image_spacing;
944     const gchar *title, *value;
945
946     /* Don't do anything if the button is not constructed yet */
947     if (G_UNLIKELY (priv->label_box == NULL))
948         return;
949
950     /* Don't do anything if the button has no contents */
951     title = gtk_label_get_text (priv->title);
952     value = gtk_label_get_text (priv->value);
953     if (!priv->image && !title[0] && !value[0])
954         return;
955
956     /* Save a ref to the image, and remove it from its container if necessary */
957     if (priv->image) {
958         g_object_ref (priv->image);
959         if (priv->image->parent != NULL)
960             gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
961     }
962
963     if (priv->label_box->parent != NULL) {
964         gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
965     }
966
967     /* Remove the child from the container and add priv->alignment */
968     child = gtk_bin_get_child (GTK_BIN (button));
969     if (child != NULL && child != priv->alignment) {
970         gtk_container_remove (GTK_CONTAINER (button), child);
971         child = NULL;
972     }
973
974     if (child == NULL) {
975         gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
976     }
977
978     /* Create a new hbox */
979     if (priv->hbox) {
980         gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
981     }
982     gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
983     priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
984     gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
985
986     /* Pack the image and the alignment in the new hbox */
987     if (priv->image && priv->image_position == GTK_POS_LEFT)
988         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
989
990     gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
991
992     if (priv->image && priv->image_position == GTK_POS_RIGHT)
993         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
994
995     /* Set image alignment and remove previously set ref */
996     if (priv->image) {
997         gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
998         g_object_unref (priv->image);
999     }
1000
1001     /* Show everything */
1002     gtk_widget_show_all (GTK_WIDGET (priv->alignment));
1003 }