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