2 * This file is a part of hildon
4 * Copyright (C) 2008, 2009 Nokia Corporation, all rights reserved.
6 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
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.
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.
20 * SECTION:hildon-button
21 * @short_description: Two-label buttons in the Hildon framework.
23 * #HildonButton is a clickable button for Hildon applications. It is
24 * derived from the #GtkButton widget and provides additional
25 * commodities specific to the Hildon framework.
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.
32 * A #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).
38 * To change the alignment of both labels, use gtk_button_set_alignment()
40 * To make them expand and use the full space of the button, use
41 * hildon_button_set_alignment().
43 * To change the relative alignment of each label, use
44 * hildon_button_set_title_alignment() and
45 * hildon_button_set_value_alignment().
47 * In hildon-button-example.c included in the Hildon distribution you
48 * can see examples of how to create the most common button
51 * If only one label is needed, #GtkButton can be used as well, see
52 * also hildon_gtk_button_new().
55 * <title>Creating a HildonButton</title>
58 * button_clicked (HildonButton *button, gpointer user_data)
60 * const gchar *title, *value;
62 * title = hildon_button_get_title (button);
63 * value = hildon_button_get_value (button);
64 * g_debug ("Button clicked with title '%s' and value '%s'", title, value);
68 * create_button (void)
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");
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);
81 * gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5);
83 * g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), NULL);
91 #include "hildon-button.h"
92 #include "hildon-enum-types.h"
93 #include "hildon-gtk.h"
95 G_DEFINE_TYPE (HildonButton, hildon_button, GTK_TYPE_BUTTON);
97 #define HILDON_BUTTON_GET_PRIVATE(obj) \
98 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
99 HILDON_TYPE_BUTTON, HildonButtonPrivate));
101 typedef struct _HildonButtonPrivate HildonButtonPrivate;
103 struct _HildonButtonPrivate
108 GtkWidget *label_box;
109 GtkWidget *alignment;
111 GtkPositionType image_position;
114 HildonButtonStyle style;
115 guint setting_style : 1;
127 hildon_button_set_arrangement (HildonButton *button,
128 HildonButtonArrangement arrangement);
131 hildon_button_construct_child (HildonButton *button);
134 hildon_button_set_property (GObject *object,
139 HildonButton *button = HILDON_BUTTON (object);
144 hildon_button_set_title (button, g_value_get_string (value));
147 hildon_button_set_value (button, g_value_get_string (value));
150 hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
152 case PROP_ARRANGEMENT:
153 hildon_button_set_arrangement (button, g_value_get_enum (value));
156 hildon_button_set_style (button, g_value_get_enum (value));
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 hildon_button_get_property (GObject *object,
170 HildonButton *button = HILDON_BUTTON (object);
175 g_value_set_string (value, hildon_button_get_title (button));
178 g_value_set_string (value, hildon_button_get_value (button));
181 g_value_set_enum (value, hildon_button_get_style (button));
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 set_logical_font (GtkWidget *button)
192 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
194 /* In buttons with vertical arrangement, the 'value' label uses a
196 if (GTK_IS_VBOX (priv->label_box)) {
197 GtkStyle *style = gtk_rc_get_style_by_paths (
198 gtk_settings_get_default (), "SmallSystemFont", NULL, G_TYPE_NONE);
200 PangoFontDescription *font_desc = style->font_desc;
201 if (font_desc != NULL) {
202 priv->setting_style = TRUE;
203 gtk_widget_modify_font (GTK_WIDGET (priv->value), font_desc);
204 priv->setting_style = FALSE;
211 set_logical_color (GtkWidget *button)
214 const gchar *colorname;
215 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
216 GtkWidget *label = GTK_WIDGET (priv->value);
218 switch (priv->style) {
219 case HILDON_BUTTON_STYLE_NORMAL:
220 colorname = "SecondaryTextColor";
222 case HILDON_BUTTON_STYLE_PICKER:
223 colorname = "ActiveTextColor";
226 g_return_if_reached ();
229 gtk_widget_ensure_style (label);
230 if (gtk_style_lookup_color (label->style, colorname, &color) == TRUE) {
231 priv->setting_style = TRUE;
232 gtk_widget_modify_fg (label, GTK_STATE_NORMAL, &color);
233 gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, &color);
234 priv->setting_style = FALSE;
239 hildon_button_style_set (GtkWidget *widget,
240 GtkStyle *previous_style)
242 guint horizontal_spacing, vertical_spacing, image_spacing;
243 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (widget);
245 if (GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set)
246 GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set (widget, previous_style);
248 /* Prevent infinite recursion when calling set_logical_font() and
249 * set_logical_color() */
250 if (priv->setting_style)
253 gtk_widget_style_get (widget,
254 "horizontal-spacing", &horizontal_spacing,
255 "vertical-spacing", &vertical_spacing,
256 "image-spacing", &image_spacing,
259 if (GTK_IS_HBOX (priv->label_box)) {
260 gtk_box_set_spacing (GTK_BOX (priv->label_box), horizontal_spacing);
262 gtk_box_set_spacing (GTK_BOX (priv->label_box), vertical_spacing);
265 if (GTK_IS_BOX (priv->hbox)) {
266 gtk_box_set_spacing (priv->hbox, image_spacing);
269 set_logical_font (widget);
270 set_logical_color (widget);
274 hildon_button_finalize (GObject *object)
276 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (object);
278 g_object_unref (priv->alignment);
279 g_object_unref (priv->label_box);
281 G_OBJECT_CLASS (hildon_button_parent_class)->finalize (object);
285 hildon_button_class_init (HildonButtonClass *klass)
287 GObjectClass *gobject_class = (GObjectClass *)klass;
288 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
290 gobject_class->set_property = hildon_button_set_property;
291 gobject_class->get_property = hildon_button_get_property;
292 gobject_class->finalize = hildon_button_finalize;
293 widget_class->style_set = hildon_button_style_set;
295 g_object_class_install_property (
298 g_param_spec_string (
301 "Text of the title label inside the button",
305 g_object_class_install_property (
308 g_param_spec_string (
311 "Text of the value label inside the button",
315 g_object_class_install_property (
321 "Size request for the button",
322 HILDON_TYPE_SIZE_TYPE,
324 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
326 g_object_class_install_property (
332 "How the button contents must be arranged",
333 HILDON_TYPE_BUTTON_ARRANGEMENT,
334 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
335 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
337 g_object_class_install_property (
343 "Visual style of the button",
344 HILDON_TYPE_BUTTON_STYLE,
345 HILDON_BUTTON_STYLE_NORMAL,
348 gtk_widget_class_install_style_property (
351 "horizontal-spacing",
352 "Horizontal spacing between labels",
353 "Horizontal spacing between the title and value labels, when in horizontal mode",
357 gtk_widget_class_install_style_property (
361 "Vertical spacing between labels",
362 "Vertical spacing between the title and value labels, when in vertical mode",
366 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
370 hildon_button_init (HildonButton *self)
372 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
374 priv->title = GTK_LABEL (gtk_label_new (NULL));
375 priv->value = GTK_LABEL (gtk_label_new (NULL));
376 priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
378 priv->image_position = GTK_POS_LEFT;
379 priv->image_xalign = 0.5;
380 priv->image_yalign = 0.5;
382 priv->label_box = NULL;
383 priv->style = HILDON_BUTTON_STYLE_NORMAL;
384 priv->setting_style = FALSE;
386 gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
387 gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
389 hildon_button_set_style (self, HILDON_BUTTON_STYLE_NORMAL);
391 gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
392 gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
394 g_object_ref_sink (priv->alignment);
396 /* The labels are not shown automatically, see hildon_button_set_(title|value) */
397 gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
398 gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
400 gtk_button_set_focus_on_click (GTK_BUTTON (self), FALSE);
404 * hildon_button_add_title_size_group:
405 * @button: a #HildonButton
406 * @size_group: A #GtkSizeGroup for the button title (main label)
408 * Adds the title label of @button to @size_group.
413 hildon_button_add_title_size_group (HildonButton *button,
414 GtkSizeGroup *size_group)
416 HildonButtonPrivate *priv;
418 g_return_if_fail (HILDON_IS_BUTTON (button));
419 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
421 priv = HILDON_BUTTON_GET_PRIVATE (button);
423 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->title));
427 * hildon_button_add_value_size_group:
428 * @button: a #HildonButton
429 * @size_group: A #GtkSizeGroup for the button value (secondary label)
431 * Adds the value label of @button to @size_group.
436 hildon_button_add_value_size_group (HildonButton *button,
437 GtkSizeGroup *size_group)
439 HildonButtonPrivate *priv;
441 g_return_if_fail (HILDON_IS_BUTTON (button));
442 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
444 priv = HILDON_BUTTON_GET_PRIVATE (button);
446 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->value));
450 * hildon_button_add_image_size_group:
451 * @button: a #HildonButton
452 * @size_group: A #GtkSizeGroup for the button image
454 * Adds the image of @button to @size_group. You must add an image
455 * using hildon_button_set_image() before calling this function.
460 hildon_button_add_image_size_group (HildonButton *button,
461 GtkSizeGroup *size_group)
463 HildonButtonPrivate *priv;
465 g_return_if_fail (HILDON_IS_BUTTON (button));
466 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
468 priv = HILDON_BUTTON_GET_PRIVATE (button);
470 g_return_if_fail (GTK_IS_WIDGET (priv->image));
472 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->image));
476 * hildon_button_add_size_groups:
477 * @button: a #HildonButton
478 * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
479 * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
480 * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
482 * Convenience function to add title, value and image to size
483 * groups. %NULL size groups will be ignored.
488 hildon_button_add_size_groups (HildonButton *button,
489 GtkSizeGroup *title_size_group,
490 GtkSizeGroup *value_size_group,
491 GtkSizeGroup *image_size_group)
493 if (title_size_group)
494 hildon_button_add_title_size_group (button, title_size_group);
496 if (value_size_group)
497 hildon_button_add_value_size_group (button, value_size_group);
499 if (image_size_group)
500 hildon_button_add_image_size_group (button, image_size_group);
505 * @size: Flags to set the size of the button.
506 * @arrangement: How the labels must be arranged.
508 * Creates a new #HildonButton. To set text in the labels, use
509 * hildon_button_set_title() and
510 * hildon_button_set_value(). Alternatively, you can add a custom
511 * child widget using gtk_container_add().
513 * Returns: a new #HildonButton
518 hildon_button_new (HildonSizeType size,
519 HildonButtonArrangement arrangement)
521 return hildon_button_new_with_text (size, arrangement, NULL, NULL);
525 * hildon_button_new_with_text:
526 * @size: Flags to set the size of the button.
527 * @arrangement: How the labels must be arranged.
528 * @title: Title of the button (main label), or %NULL
529 * @value: Value of the button (secondary label), or %NULL
531 * Creates a new #HildonButton with two labels, @title and @value.
533 * If you just don't want to use one of the labels, set it to
534 * %NULL. You can set it to a non-%NULL value at any time later using
535 * hildon_button_set_title() or hildon_button_set_value() .
537 * Returns: a new #HildonButton
542 hildon_button_new_with_text (HildonSizeType size,
543 HildonButtonArrangement arrangement,
550 button = g_object_new (HILDON_TYPE_BUTTON,
554 "arrangement", arrangement,
561 hildon_button_set_arrangement (HildonButton *button,
562 HildonButtonArrangement arrangement)
564 HildonButtonPrivate *priv;
566 priv = HILDON_BUTTON_GET_PRIVATE (button);
568 /* Pack everything */
569 if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
570 priv->label_box = gtk_vbox_new (FALSE, 0);
571 set_logical_font (GTK_WIDGET (button));
573 priv->label_box = gtk_hbox_new (FALSE, 0);
576 g_object_ref_sink (priv->label_box);
578 /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
579 * can be painted outside of the button in some situations, see
580 * NB#88126 and NB#110689 */
581 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), FALSE, FALSE, 0);
582 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), TRUE, TRUE, 0);
584 hildon_button_construct_child (button);
588 * hildon_button_set_title:
589 * @button: a #HildonButton
590 * @title: a new title (main label) for the button, or %NULL
592 * Sets the title (main label) of @button to @title.
594 * This will clear any previously set title.
596 * If @title is set to %NULL or an empty string, the title label will
597 * be hidden and the value label will be realigned.
602 hildon_button_set_title (HildonButton *button,
605 HildonButtonPrivate *priv;
607 g_return_if_fail (HILDON_IS_BUTTON (button));
609 priv = HILDON_BUTTON_GET_PRIVATE (button);
610 gtk_label_set_text (priv->title, title);
612 /* If the button has no title, hide the label so the value is
613 * properly aligned */
614 if (title && title[0] != '\0') {
615 hildon_button_construct_child (button);
616 gtk_widget_show (GTK_WIDGET (priv->title));
618 gtk_widget_hide (GTK_WIDGET (priv->title));
621 g_object_notify (G_OBJECT (button), "title");
625 * hildon_button_set_value:
626 * @button: a #HildonButton
627 * @value: a new value (secondary label) for the button, or %NULL
629 * Sets the value (secondary label) of @button to @value.
631 * This will clear any previously set value.
633 * If @value is set to %NULL or an empty string, the value label will
634 * be hidden and the title label will be realigned.
639 hildon_button_set_value (HildonButton *button,
642 HildonButtonPrivate *priv;
644 g_return_if_fail (HILDON_IS_BUTTON (button));
646 priv = HILDON_BUTTON_GET_PRIVATE (button);
647 gtk_label_set_text (priv->value, value);
649 /* If the button has no value, hide the label so the title is
650 * properly aligned */
651 if (value && value[0] != '\0') {
652 hildon_button_construct_child (button);
653 gtk_widget_show (GTK_WIDGET (priv->value));
655 gtk_widget_hide (GTK_WIDGET (priv->value));
658 g_object_notify (G_OBJECT (button), "value");
662 * hildon_button_get_title:
663 * @button: a #HildonButton
665 * Fetches the text from the main label (title) of @button,
666 * as set by hildon_button_set_title() or hildon_button_set_text().
667 * If the label text has not been set the return value will be %NULL.
668 * This will be the case if you create an empty button with
669 * hildon_button_new() to use as a container.
671 * Returns: The text of the title label. This string is owned by the
672 * widget and must not be modified or freed.
677 hildon_button_get_title (HildonButton *button)
679 HildonButtonPrivate *priv;
681 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
683 priv = HILDON_BUTTON_GET_PRIVATE (button);
685 return gtk_label_get_text (priv->title);
689 * hildon_button_get_value:
690 * @button: a #HildonButton
692 * Fetches the text from the secondary label (value) of @button,
693 * as set by hildon_button_set_value() or hildon_button_set_text().
694 * If the label text has not been set the return value will be %NULL.
695 * This will be the case if you create an empty button with hildon_button_new()
696 * to use as a container.
698 * Returns: The text of the value label. This string is owned by the
699 * widget and must not be modified or freed.
704 hildon_button_get_value (HildonButton *button)
706 HildonButtonPrivate *priv;
708 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
710 priv = HILDON_BUTTON_GET_PRIVATE (button);
712 return gtk_label_get_text (priv->value);
716 * hildon_button_set_text:
717 * @button: a #HildonButton
718 * @title: new text for the button title (main label)
719 * @value: new text for the button value (secondary label)
721 * Convenience function to change both labels of a #HildonButton
726 hildon_button_set_text (HildonButton *button,
730 hildon_button_set_title (button, title);
731 hildon_button_set_value (button, value);
735 * hildon_button_set_image:
736 * @button: a #HildonButton
737 * @image: a widget to set as the button image
739 * Sets the image of @button to the given widget. The previous image
740 * (if any) will be removed.
745 hildon_button_set_image (HildonButton *button,
748 HildonButtonPrivate *priv;
750 g_return_if_fail (HILDON_IS_BUTTON (button));
751 g_return_if_fail (!image || GTK_IS_WIDGET (image));
753 priv = HILDON_BUTTON_GET_PRIVATE (button);
755 /* Return if there's nothing to do */
756 if (image == priv->image)
759 if (priv->image && priv->image->parent)
760 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
764 hildon_button_construct_child (button);
768 * hildon_button_get_image:
769 * @button: a #HildonButton
771 * Gets the widget that is currenty set as the image of @button,
772 * previously set with hildon_button_set_image()
774 * Returns: a #GtkWidget or %NULL in case there is no image
779 hildon_button_get_image (HildonButton *button)
781 HildonButtonPrivate *priv;
783 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
785 priv = HILDON_BUTTON_GET_PRIVATE (button);
791 * hildon_button_set_image_position:
792 * @button: a #HildonButton
793 * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
795 * Sets the position of the image inside @button. Only %GTK_POS_LEFT
796 * and %GTK_POS_RIGHT are currently supported.
801 hildon_button_set_image_position (HildonButton *button,
802 GtkPositionType position)
804 HildonButtonPrivate *priv;
806 g_return_if_fail (HILDON_IS_BUTTON (button));
807 g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
809 priv = HILDON_BUTTON_GET_PRIVATE (button);
811 /* Return if there's nothing to do */
812 if (priv->image_position == position)
815 priv->image_position = position;
817 hildon_button_construct_child (button);
821 * hildon_button_set_alignment:
822 * @button: a #HildonButton
823 * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
824 * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
825 * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
826 * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
828 * Sets the alignment of the contents of the widget. If you don't need
829 * to change @xscale or @yscale you can just use
830 * gtk_button_set_alignment() instead.
832 * Note that for this method to work properly, the child widget of
833 * @button must be a #GtkAlignment. That's what #HildonButton uses by
834 * default, so this function will work unless you add a custom widget
840 hildon_button_set_alignment (HildonButton *button,
846 HildonButtonPrivate *priv;
849 g_return_if_fail (HILDON_IS_BUTTON (button));
851 priv = HILDON_BUTTON_GET_PRIVATE (button);
853 child = gtk_bin_get_child (GTK_BIN (button));
855 /* If the button has no child, use priv->alignment, which is the default one */
857 child = priv->alignment;
859 if (GTK_IS_ALIGNMENT (child)) {
860 gtk_alignment_set (GTK_ALIGNMENT (priv->alignment), xalign, yalign, xscale, yscale);
865 * hildon_button_set_title_alignment:
866 * @button: a #HildonButton
867 * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
868 * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
870 * Sets the alignment of the title label. See also
871 * hildon_button_set_alignment() to set the alignment of the whole
872 * contents of the button.
877 hildon_button_set_title_alignment (HildonButton *button,
881 HildonButtonPrivate *priv;
883 g_return_if_fail (HILDON_IS_BUTTON (button));
885 priv = HILDON_BUTTON_GET_PRIVATE (button);
887 gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign);
891 * hildon_button_set_value_alignment:
892 * @button: a #HildonButton
893 * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
894 * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
896 * Sets the alignment of the value label. See also
897 * hildon_button_set_alignment() to set the alignment of the whole
898 * contents of the button.
903 hildon_button_set_value_alignment (HildonButton *button,
907 HildonButtonPrivate *priv;
909 g_return_if_fail (HILDON_IS_BUTTON (button));
911 priv = HILDON_BUTTON_GET_PRIVATE (button);
913 gtk_misc_set_alignment (GTK_MISC (priv->value), xalign, yalign);
917 * hildon_button_set_image_alignment:
918 * @button: a #HildonButton
919 * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
920 * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
922 * Sets the alignment of the image. See also
923 * hildon_button_set_alignment() to set the alignment of the whole
924 * contents of the button.
929 hildon_button_set_image_alignment (HildonButton *button,
933 HildonButtonPrivate *priv;
935 g_return_if_fail (HILDON_IS_BUTTON (button));
937 priv = HILDON_BUTTON_GET_PRIVATE (button);
939 /* Return if there's nothing to do */
940 if (priv->image_xalign == xalign && priv->image_yalign == yalign)
943 priv->image_xalign = xalign;
944 priv->image_yalign = yalign;
946 hildon_button_construct_child (button);
950 * hildon_button_set_style:
951 * @button: A #HildonButton
952 * @style: A #HildonButtonStyle for @button
954 * Sets the style of @button to @style. This changes the visual
955 * appearance of the button (colors, font sizes) according to the
956 * particular style chosen, but the general layout is not altered.
958 * Use %HILDON_BUTTON_STYLE_NORMAL to make it look like a normal
959 * #HildonButton, or %HILDON_BUTTON_STYLE_PICKER to make it look like
960 * a #HildonPickerButton.
965 hildon_button_set_style (HildonButton *button,
966 HildonButtonStyle style)
968 HildonButtonPrivate *priv;
970 g_return_if_fail (HILDON_IS_BUTTON (button));
971 priv = HILDON_BUTTON_GET_PRIVATE (button);
974 set_logical_color (GTK_WIDGET (button));
976 g_object_notify (G_OBJECT (button), "style");
980 * hildon_button_get_style:
981 * @button: A #HildonButton
983 * Gets the visual style of the button.
985 * Returns: a #HildonButtonStyle
990 hildon_button_get_style (HildonButton *button)
992 HildonButtonPrivate *priv;
994 g_return_val_if_fail (HILDON_IS_BUTTON (button), HILDON_BUTTON_STYLE_NORMAL);
996 priv = HILDON_BUTTON_GET_PRIVATE (button);
1002 hildon_button_construct_child (HildonButton *button)
1004 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
1007 const gchar *title, *value;
1009 /* Don't do anything if the button is not constructed yet */
1010 if (G_UNLIKELY (priv->label_box == NULL))
1013 /* Don't do anything if the button has no contents */
1014 title = gtk_label_get_text (priv->title);
1015 value = gtk_label_get_text (priv->value);
1016 if (!priv->image && !title[0] && !value[0])
1019 /* Save a ref to the image, and remove it from its container if necessary */
1021 g_object_ref (priv->image);
1022 if (priv->image->parent != NULL)
1023 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
1026 if (priv->label_box->parent != NULL) {
1027 gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
1030 /* Remove the child from the container and add priv->alignment */
1031 child = gtk_bin_get_child (GTK_BIN (button));
1032 if (child != NULL && child != priv->alignment) {
1033 gtk_container_remove (GTK_CONTAINER (button), child);
1037 if (child == NULL) {
1038 gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
1041 /* Create a new hbox */
1043 gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
1045 gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
1046 priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
1047 gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
1049 /* Pack the image and the alignment in the new hbox */
1050 if (priv->image && priv->image_position == GTK_POS_LEFT)
1051 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
1053 gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
1055 if (priv->image && priv->image_position == GTK_POS_RIGHT)
1056 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
1058 /* Set image alignment and remove previously set ref */
1060 gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
1061 g_object_unref (priv->image);
1064 /* Show everything */
1065 gtk_widget_show_all (GTK_WIDGET (priv->alignment));