524c24d8b4212493f75ebd6d579834069d76cba6
[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. 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 | G_PARAM_CONSTRUCT));
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.
464  *
465  * Returns: a new #HildonButton
466  *
467  * Since: 2.2
468  **/
469 GtkWidget *
470 hildon_button_new_with_text                     (HildonSizeType           size,
471                                                  HildonButtonArrangement  arrangement,
472                                                  const gchar             *title,
473                                                  const gchar             *value)
474 {
475     GtkWidget *button;
476
477     /* Create widget */
478     button = g_object_new (HILDON_TYPE_BUTTON,
479                            "size", size,
480                            "title", title,
481                            "value", value,
482                            "arrangement", arrangement,
483                            NULL);
484
485     return button;
486 }
487
488 static void
489 hildon_button_set_arrangement                   (HildonButton            *button,
490                                                  HildonButtonArrangement  arrangement)
491 {
492     HildonButtonPrivate *priv;
493
494     priv = HILDON_BUTTON_GET_PRIVATE (button);
495
496     /* Pack everything */
497     if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
498         priv->label_box = gtk_vbox_new (FALSE, 0);
499         hildon_helper_set_logical_font (GTK_WIDGET (priv->value), "SmallSystemFont");
500     } else {
501         priv->label_box = gtk_hbox_new (FALSE, 0);
502     }
503
504     g_object_ref_sink (priv->label_box);
505
506     /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
507      * can be painted outside of the button in some situations, see
508      * NB#88126 */
509     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
510     gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
511
512     hildon_button_construct_child (button);
513 }
514
515 /**
516  * hildon_button_set_title:
517  * @button: a #HildonButton
518  * @title: a new title (main label) for the button, or %NULL
519  *
520  * Sets the title (main label) of @button to @title.
521  *
522  * This will clear any previously set title.
523  *
524  * If @title is set to %NULL, the title label will be hidden and the
525  * value label will be realigned.
526  *
527  * Since: 2.2
528  **/
529 void
530 hildon_button_set_title                         (HildonButton *button,
531                                                  const gchar  *title)
532 {
533     HildonButtonPrivate *priv;
534
535     g_return_if_fail (HILDON_IS_BUTTON (button));
536
537     priv = HILDON_BUTTON_GET_PRIVATE (button);
538     gtk_label_set_text (priv->title, title);
539
540     /* If the button has no title, hide the label so the value is
541      * properly aligned */
542     if (title) {
543         hildon_button_construct_child (button);
544         gtk_widget_show (GTK_WIDGET (priv->title));
545     } else {
546         gtk_widget_hide (GTK_WIDGET (priv->title));
547     }
548
549     g_object_notify (G_OBJECT (button), "title");
550 }
551
552 /**
553  * hildon_button_set_value:
554  * @button: a #HildonButton
555  * @value: a new value (secondary label) for the button, or %NULL
556  *
557  * Sets the value (secondary label) of @button to @value.
558  *
559  * This will clear any previously set value.
560  *
561  * If @value is set to %NULL, the value label will be hidden and the
562  * title label will be realigned.
563  *
564  *
565  * Since: 2.2
566  **/
567 void
568 hildon_button_set_value                         (HildonButton *button,
569                                                  const gchar  *value)
570 {
571     HildonButtonPrivate *priv;
572
573     g_return_if_fail (HILDON_IS_BUTTON (button));
574
575     priv = HILDON_BUTTON_GET_PRIVATE (button);
576     gtk_label_set_text (priv->value, value);
577
578     /* If the button has no value, hide the label so the title is
579      * properly aligned */
580     if (value) {
581         hildon_button_construct_child (button);
582         gtk_widget_show (GTK_WIDGET (priv->value));
583     } else {
584         gtk_widget_hide (GTK_WIDGET (priv->value));
585     }
586
587     g_object_notify (G_OBJECT (button), "value");
588 }
589
590 /**
591  * hildon_button_get_title:
592  * @button: a #HildonButton
593  *
594  * Gets the text from the main label (title) of @button, or %NULL if
595  * none has been set.
596  *
597  * Returns: The text of the title label. This string is owned by the
598  * widget and must not be modified or freed.
599  *
600  * Since: 2.2
601  **/
602 const gchar *
603 hildon_button_get_title                         (HildonButton *button)
604 {
605     HildonButtonPrivate *priv;
606
607     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
608
609     priv = HILDON_BUTTON_GET_PRIVATE (button);
610
611     return gtk_label_get_text (priv->title);
612 }
613
614 /**
615  * hildon_button_get_value:
616  * @button: a #HildonButton
617  *
618  * Gets the text from the secondary label (value) of @button, or %NULL
619  * if none has been set.
620  *
621  * Returns: The text of the value label. This string is owned by the
622  * widget and must not be modified or freed.
623  *
624  * Since: 2.2
625  **/
626 const gchar *
627 hildon_button_get_value                         (HildonButton *button)
628 {
629     HildonButtonPrivate *priv;
630
631     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
632
633     priv = HILDON_BUTTON_GET_PRIVATE (button);
634
635     return gtk_label_get_text (priv->value);
636 }
637
638 /**
639  * hildon_button_set_text:
640  * @button: a #HildonButton
641  * @title: new text for the button title (main label)
642  * @value: new text for the button value (secondary label)
643  *
644  * Convenience function to change both labels of a #HildonButton
645  *
646  * Since: 2.2
647  **/
648 void
649 hildon_button_set_text                          (HildonButton *button,
650                                                  const gchar  *title,
651                                                  const gchar  *value)
652 {
653     hildon_button_set_title (button, title);
654     hildon_button_set_value (button, value);
655 }
656
657 /**
658  * hildon_button_set_image:
659  * @button: a #HildonButton
660  * @image: a widget to set as the button image
661  *
662  * Sets the image of @button to the given widget. The previous image
663  * (if any) will be removed.
664  *
665  * Since: 2.2
666  **/
667 void
668 hildon_button_set_image                         (HildonButton *button,
669                                                  GtkWidget    *image)
670 {
671     HildonButtonPrivate *priv;
672
673     g_return_if_fail (HILDON_IS_BUTTON (button));
674     g_return_if_fail (!image || GTK_IS_WIDGET (image));
675
676     priv = HILDON_BUTTON_GET_PRIVATE (button);
677
678     /* Return if there's nothing to do */
679     if (image == priv->image)
680         return;
681
682     if (priv->image && priv->image->parent)
683         gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
684
685     priv->image = image;
686
687     hildon_button_construct_child (button);
688 }
689
690 /**
691  * hildon_button_get_image:
692  * @button: a #HildonButton
693  *
694  * Gets the widget that is currenty set as the image of @button,
695  * previously set with hildon_button_set_image()
696  *
697  * Returns: a #GtkWidget or %NULL in case there is no image
698  *
699  * Since: 2.2
700  **/
701 GtkWidget *
702 hildon_button_get_image                         (HildonButton *button)
703 {
704     HildonButtonPrivate *priv;
705
706     g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
707
708     priv = HILDON_BUTTON_GET_PRIVATE (button);
709
710     return priv->image;
711 }
712
713 /**
714  * hildon_button_set_image_position:
715  * @button: a #HildonButton
716  * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
717  *
718  * Sets the position of the image inside @button. Only %GTK_POS_LEFT
719  * and %GTK_POS_RIGHT are currently supported.
720  *
721  * Since: 2.2
722  **/
723 void
724 hildon_button_set_image_position                (HildonButton    *button,
725                                                  GtkPositionType  position)
726 {
727     HildonButtonPrivate *priv;
728
729     g_return_if_fail (HILDON_IS_BUTTON (button));
730     g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
731
732     priv = HILDON_BUTTON_GET_PRIVATE (button);
733
734     /* Return if there's nothing to do */
735     if (priv->image_position == position)
736         return;
737
738     priv->image_position = position;
739
740     hildon_button_construct_child (button);
741 }
742
743 /**
744  * hildon_button_set_alignment:
745  * @button: a #HildonButton
746  * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
747  * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
748  * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
749  * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
750  *
751  * Sets the alignment of the contents of the widget. If you don't need
752  * to change @xscale or @yscale you can just use
753  * gtk_button_set_alignment() instead.
754  *
755  * Since: 2.2
756  **/
757 void
758 hildon_button_set_alignment                     (HildonButton *button,
759                                                  gfloat        xalign,
760                                                  gfloat        yalign,
761                                                  gfloat        xscale,
762                                                  gfloat        yscale)
763 {
764     HildonButtonPrivate *priv;
765     GtkWidget *child;
766
767     g_return_if_fail (HILDON_IS_BUTTON (button));
768
769     priv = HILDON_BUTTON_GET_PRIVATE (button);
770
771     child = gtk_bin_get_child (GTK_BIN (button));
772
773     if (GTK_IS_ALIGNMENT (child)) {
774         gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign);
775         g_object_set (child, "xscale", xscale, "yscale", yscale, NULL);
776     }
777 }
778
779 /**
780  * hildon_button_set_title_alignment:
781  * @button: a #HildonButton
782  * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
783  * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
784  *
785  * Sets the alignment of the title label. See also
786  * hildon_button_set_alignment() to set the alignment of the whole
787  * contents of the button.
788  *
789  * Since: 2.2
790  **/
791 void
792 hildon_button_set_title_alignment               (HildonButton *button,
793                                                  gfloat        xalign,
794                                                  gfloat        yalign)
795 {
796     HildonButtonPrivate *priv;
797
798     g_return_if_fail (HILDON_IS_BUTTON (button));
799
800     priv = HILDON_BUTTON_GET_PRIVATE (button);
801
802     gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign);
803 }
804
805 /**
806  * hildon_button_set_value_alignment:
807  * @button: a #HildonButton
808  * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
809  * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
810  *
811  * Sets the alignment of the value 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_value_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->value), xalign, yalign);
829 }
830
831 /**
832  * hildon_button_set_image_alignment:
833  * @button: a #HildonButton
834  * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
835  * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
836  *
837  * Sets the alignment of the image. 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_image_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     /* Return if there's nothing to do */
855     if (priv->image_xalign == xalign && priv->image_yalign == yalign)
856         return;
857
858     priv->image_xalign = xalign;
859     priv->image_yalign = yalign;
860
861     hildon_button_construct_child (button);
862 }
863
864 /**
865  * hildon_button_set_style:
866  * @button: A #HildonButton
867  * @style: A #HildonButtonStyle for @button
868  *
869  * Sets the style of @button to @style. This changes the visual
870  * appearance of the button (colors, font sizes) according to the
871  * particular style chosen, but the general layout is not altered.
872  *
873  * Use %HILDON_BUTTON_STYLE_NORMAL to make it look like a normal
874  * #HildonButton, or %HILDON_BUTTON_STYLE_PICKER to make it look like
875  * a #HildonPickerButton.
876  *
877  * Since: 2.2
878  */
879 void
880 hildon_button_set_style                         (HildonButton      *button,
881                                                  HildonButtonStyle  style)
882 {
883     HildonButtonPrivate *priv;
884     const gchar *color;
885
886     g_return_if_fail (HILDON_IS_BUTTON (button));
887
888     switch (style) {
889     case HILDON_BUTTON_STYLE_NORMAL:
890         color = "SecondaryTextColor";
891         break;
892     case HILDON_BUTTON_STYLE_PICKER:
893         color = "ActiveTextColor";
894         break;
895     default:
896         g_return_if_reached ();
897     }
898
899     priv = HILDON_BUTTON_GET_PRIVATE (button);
900
901     hildon_helper_set_logical_color (GTK_WIDGET (priv->value), GTK_RC_FG, GTK_STATE_NORMAL, color);
902     hildon_helper_set_logical_color (GTK_WIDGET (priv->value), GTK_RC_FG, GTK_STATE_PRELIGHT, color);
903
904     priv->style = style;
905
906     g_object_notify (G_OBJECT (button), "style");
907 }
908
909 /**
910  * hildon_button_get_style:
911  * @button: A #HildonButton
912  *
913  * Gets the visual style of the button.
914  *
915  * Returns: a #HildonButtonStyle
916  *
917  * Since: 2.2
918  */
919 HildonButtonStyle
920 hildon_button_get_style                         (HildonButton *button)
921 {
922     HildonButtonPrivate *priv;
923
924     g_return_val_if_fail (HILDON_IS_BUTTON (button), HILDON_BUTTON_STYLE_NORMAL);
925
926     priv = HILDON_BUTTON_GET_PRIVATE (button);
927
928     return priv->style;
929 }
930
931 static void
932 hildon_button_construct_child                   (HildonButton *button)
933 {
934     HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
935     GtkWidget *child;
936     gint image_spacing;
937     const gchar *title, *value;
938
939     /* Don't do anything if the button is not constructed yet */
940     if (G_UNLIKELY (priv->label_box == NULL))
941         return;
942
943     /* Don't do anything if the button has no contents */
944     title = gtk_label_get_text (priv->title);
945     value = gtk_label_get_text (priv->value);
946     if (!priv->image && !title[0] && !value[0])
947         return;
948
949     /* Save a ref to the image, and remove it from its container if necessary */
950     if (priv->image) {
951         g_object_ref (priv->image);
952         if (priv->image->parent != NULL)
953             gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
954     }
955
956     if (priv->label_box->parent != NULL) {
957         gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
958     }
959
960     /* Remove the child from the container and add priv->alignment */
961     child = gtk_bin_get_child (GTK_BIN (button));
962     if (child != NULL && child != priv->alignment) {
963         gtk_container_remove (GTK_CONTAINER (button), child);
964         child = NULL;
965     }
966
967     if (child == NULL) {
968         gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
969     }
970
971     /* Create a new hbox */
972     if (priv->hbox) {
973         gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
974     }
975     gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
976     priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
977     gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
978
979     /* Pack the image and the alignment in the new hbox */
980     if (priv->image && priv->image_position == GTK_POS_LEFT)
981         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
982
983     gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
984
985     if (priv->image && priv->image_position == GTK_POS_RIGHT)
986         gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
987
988     /* Set image alignment and remove previously set ref */
989     if (priv->image) {
990         gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
991         g_object_unref (priv->image);
992     }
993
994     /* Show everything */
995     gtk_widget_show_all (GTK_WIDGET (priv->alignment));
996 }