X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fhildon-button.c;h=e6b6141a7ed19a5b361e622489305e3a6e32d4b1;hb=db9685e21af93630b100473c49d066de0ad3691f;hp=0cf881936af0c4f03799cd47aa2ca677705bc7a0;hpb=bbfcdeedfceaab02d64a7f1f9b1b5fcd2fb6cc2f;p=hildon diff --git a/src/hildon-button.c b/src/hildon-button.c index 0cf8819..e6b6141 100644 --- a/src/hildon-button.c +++ b/src/hildon-button.c @@ -20,7 +20,7 @@ * SECTION:hildon-button * @short_description: Widget representing a button in the Hildon framework. * - * The #HildonButton is a GTK widget which represent a clickable + * The #HildonButton is a GTK widget which represents a clickable * button. It is derived from the GtkButton widget and provides * additional commodities specific to the Hildon framework. * @@ -30,16 +30,54 @@ * "auto" so it behaves like a standard GtkButton. * * The #HildonButton can hold any valid child widget, but it usually - * contains two labels: title and value (the latter being optional). + * contains two labels, named title and value, and it can also contain + * an image. To change the alignment of the button contents, use + * gtk_button_set_alignment() + * + * If only one label is needed, #GtkButton can be used as well, see + * also hildon_gtk_button_new(). + * + * + * Creating a HildonButton + * + * void + * button_clicked (HildonButton *button, gpointer user_data) + * { + * const gchar *title, *value; + * + * title = hildon_button_get_title (button); + * value = hildon_button_get_value (button); + * g_debug ("Button clicked with title '%s' and value '%s'", title, value); + * } + * + * GtkWidget * + * create_button (void) + * { + * GtkWidget *button; + * GtkWidget *image; + * + * button = hildon_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT, + * HILDON_BUTTON_ARRANGEMENT_VERTICAL); + * hildon_button_set_text (HILDON_BUTTON (button), "Some title", "Some value"); + * + * image = gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON); + * hildon_button_set_image (HILDON_BUTTON (button), image); + * hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT); + * + * gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5); + * + * g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), NULL); + * + * return button; + * } + * + * */ #include "hildon-button.h" #include "hildon-enum-types.h" - -#define FINGER_BUTTON_HEIGHT 70 -#define THUMB_BUTTON_HEIGHT 105 -#define HALFSCREEN_BUTTON_WIDTH 400 -#define FULLSCREEN_BUTTON_WIDTH 800 +#include "hildon-gtk.h" +#include "hildon-helper.h" G_DEFINE_TYPE (HildonButton, hildon_button, GTK_TYPE_BUTTON); @@ -53,18 +91,25 @@ struct _HildonButtonPrivate { GtkLabel *title; GtkLabel *value; + GtkBox *hbox; + GtkWidget *label_box; GtkWidget *alignment; + GtkWidget *image; + GtkPositionType image_position; + gfloat image_xalign; + gfloat image_yalign; }; enum { - PROP_TITLE = 1, - PROP_VALUE, - PROP_ARRANGEMENT_FLAGS + PROP_TITLE = 1, + PROP_VALUE, + PROP_SIZE, + PROP_ARRANGEMENT }; static void -hildon_button_set_arrangement (HildonButton *button, - HildonButtonFlags flags); +hildon_button_set_arrangement (HildonButton *button, + HildonButtonArrangement arrangement); static void hildon_button_construct_child (HildonButton *button); @@ -85,7 +130,10 @@ hildon_button_set_property (GObject *object, case PROP_VALUE: hildon_button_set_value (button, g_value_get_string (value)); break; - case PROP_ARRANGEMENT_FLAGS: + case PROP_SIZE: + hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value)); + break; + case PROP_ARRANGEMENT: hildon_button_set_arrangement (button, g_value_get_enum (value)); break; default: @@ -101,15 +149,14 @@ hildon_button_get_property (GObject *object, GParamSpec *pspec) { HildonButton *button = HILDON_BUTTON (object); - HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button); switch (prop_id) { case PROP_TITLE: - g_value_set_string (value, gtk_label_get_text (priv->title)); + g_value_set_string (value, hildon_button_get_title (button)); break; case PROP_VALUE: - g_value_set_string (value, gtk_label_get_text (priv->value)); + g_value_set_string (value, hildon_button_get_value (button)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -118,6 +165,44 @@ hildon_button_get_property (GObject *object, } static void +hildon_button_style_set (GtkWidget *widget, + GtkStyle *previous_style) +{ + guint horizontal_spacing, vertical_spacing, image_spacing; + HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (widget); + + if (GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set) + GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set (widget, previous_style); + + gtk_widget_style_get (widget, + "horizontal-spacing", &horizontal_spacing, + "vertical-spacing", &vertical_spacing, + "image-spacing", &image_spacing, + NULL); + + if (GTK_IS_HBOX (priv->label_box)) { + gtk_box_set_spacing (GTK_BOX (priv->label_box), horizontal_spacing); + } else { + gtk_box_set_spacing (GTK_BOX (priv->label_box), vertical_spacing); + } + + if (GTK_IS_BOX (priv->hbox)) { + gtk_box_set_spacing (priv->hbox, image_spacing); + } +} + +static void +hildon_button_finalize (GObject *object) +{ + HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (object); + + g_object_unref (priv->alignment); + g_object_unref (priv->label_box); + + G_OBJECT_CLASS (hildon_button_parent_class)->finalize (object); +} + +static void hildon_button_class_init (HildonButtonClass *klass) { GObjectClass *gobject_class = (GObjectClass *)klass; @@ -125,6 +210,8 @@ hildon_button_class_init (HildonButtonClass *klass) gobject_class->set_property = hildon_button_set_property; gobject_class->get_property = hildon_button_get_property; + gobject_class->finalize = hildon_button_finalize; + widget_class->style_set = hildon_button_style_set; g_object_class_install_property ( gobject_class, @@ -148,13 +235,24 @@ hildon_button_class_init (HildonButtonClass *klass) g_object_class_install_property ( gobject_class, - PROP_ARRANGEMENT_FLAGS, + PROP_SIZE, g_param_spec_flags ( - "arrangement-flags", - "Arrangement flags", + "size", + "Size", + "Size request for the button", + HILDON_TYPE_SIZE_TYPE, + HILDON_SIZE_AUTO, + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property ( + gobject_class, + PROP_ARRANGEMENT, + g_param_spec_enum ( + "arrangement", + "Arrangement", "How the button contents must be arranged", - HILDON_TYPE_BUTTON_FLAGS, - HILDON_BUTTON_WITH_HORIZONTAL_VALUE, + HILDON_TYPE_BUTTON_ARRANGEMENT, + HILDON_BUTTON_ARRANGEMENT_HORIZONTAL, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); gtk_widget_class_install_style_property ( @@ -186,6 +284,12 @@ hildon_button_init (HildonButton *self) priv->title = GTK_LABEL (gtk_label_new (NULL)); priv->value = GTK_LABEL (gtk_label_new (NULL)); priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0); + priv->image = NULL; + priv->image_position = GTK_POS_LEFT; + priv->image_xalign = 0.5; + priv->image_yalign = 0.5; + priv->hbox = NULL; + priv->label_box = NULL; gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title"); gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value"); @@ -193,146 +297,199 @@ hildon_button_init (HildonButton *self) gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5); gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5); - /* The value label is not shown automatically, see hildon_button_set_value() */ + g_object_ref_sink (priv->alignment); + + /* The labels are not shown automatically, see hildon_button_set_(title|value) */ + gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE); gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE); } +/** + * hildon_button_add_title_size_group: + * @button: a #HildonButton + * @size_group: A #GtkSizeGroup for the button title (main label) + * + * Adds the title label of @button to @size_group. + **/ void -hildon_button_set_size_groups (HildonButton *button, - GtkSizeGroup *title_size_group, - GtkSizeGroup *value_size_group) +hildon_button_add_title_size_group (HildonButton *button, + GtkSizeGroup *size_group) +{ + HildonButtonPrivate *priv; + + g_return_if_fail (HILDON_IS_BUTTON (button)); + g_return_if_fail (GTK_IS_SIZE_GROUP (size_group)); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->title)); +} + +/** + * hildon_button_add_value_size_group: + * @button: a #HildonButton + * @size_group: A #GtkSizeGroup for the button value (secondary label) + * + * Adds the value label of @button to @size_group. + **/ +void +hildon_button_add_value_size_group (HildonButton *button, + GtkSizeGroup *size_group) +{ + HildonButtonPrivate *priv; + + g_return_if_fail (HILDON_IS_BUTTON (button)); + g_return_if_fail (GTK_IS_SIZE_GROUP (size_group)); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->value)); +} + +/** + * hildon_button_add_image_size_group: + * @button: a #HildonButton + * @size_group: A #GtkSizeGroup for the button image + * + * Adds the image of @button to @size_group. You must add an image + * using hildon_button_set_image() before calling this function. + **/ +void +hildon_button_add_image_size_group (HildonButton *button, + GtkSizeGroup *size_group) { HildonButtonPrivate *priv; g_return_if_fail (HILDON_IS_BUTTON (button)); + g_return_if_fail (GTK_IS_SIZE_GROUP (size_group)); priv = HILDON_BUTTON_GET_PRIVATE (button); + g_return_if_fail (GTK_IS_WIDGET (priv->image)); + + gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->image)); +} + +/** + * hildon_button_add_size_groups: + * @button: a #HildonButton + * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL + * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL + * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL + * + * Convenience function to add title, value and image to size + * groups. %NULL size groups will be ignored. + **/ +void +hildon_button_add_size_groups (HildonButton *button, + GtkSizeGroup *title_size_group, + GtkSizeGroup *value_size_group, + GtkSizeGroup *image_size_group) +{ if (title_size_group) - gtk_size_group_add_widget (title_size_group, GTK_WIDGET (priv->title)); + hildon_button_add_title_size_group (button, title_size_group); if (value_size_group) - gtk_size_group_add_widget (value_size_group, GTK_WIDGET (priv->value)); + hildon_button_add_value_size_group (button, value_size_group); + + if (image_size_group) + hildon_button_add_image_size_group (button, image_size_group); } /** * hildon_button_new: - * @flags: flags to set the size and layout of the button + * @size: Flags to set the size of the button. + * @arrangement: How the labels must be arranged. * - * Creates a new #HildonButton. To add a child widget use gtk_container_add(). + * Creates a new #HildonButton. To set text in the labels, use + * hildon_button_set_title() and + * hildon_button_set_value(). Alternatively, you can add a custom + * child widget using gtk_container_add(). * * Returns: a new #HildonButton **/ GtkWidget * -hildon_button_new (HildonButtonFlags flags) +hildon_button_new (HildonSizeType size, + HildonButtonArrangement arrangement) { - return hildon_button_new_full (flags, NULL, NULL, NULL, NULL); + return hildon_button_new_with_text (size, arrangement, NULL, NULL); } /** * hildon_button_new_with_text: - * @flags: flags to set the size and layout of the button - * @title: Title of the button (main label) + * @size: Flags to set the size of the button. + * @arrangement: How the labels must be arranged. + * @title: Title of the button (main label), or %NULL * @value: Value of the button (secondary label), or %NULL * * Creates a new #HildonButton with two labels, @title and @value. * - * If you just want to use the main label, set @value to %NULL. You - * can set it to a non-%NULL value at any time later. + * If you just don't want to use one of the labels, set it to + * %NULL. You can set it to a non-%NULL value at any time later. * * Returns: a new #HildonButton **/ GtkWidget * -hildon_button_new_with_text (HildonButtonFlags flags, - const char *title, - const char *value) -{ - return hildon_button_new_full (flags, title, value, NULL, NULL); -} - -GtkWidget * -hildon_button_new_full (HildonButtonFlags flags, - const char *title, - const char *value, - GtkSizeGroup *title_size_group, - GtkSizeGroup *value_size_group) +hildon_button_new_with_text (HildonSizeType size, + HildonButtonArrangement arrangement, + const gchar *title, + const gchar *value) { GtkWidget *button; /* Create widget */ button = g_object_new (HILDON_TYPE_BUTTON, - "arrangement-flags", flags, + "size", size, "title", title, "value", value, - "name", "hildon-button", + "arrangement", arrangement, NULL); - /* Set size groups */ - if (title_size_group || value_size_group) - hildon_button_set_size_groups (HILDON_BUTTON (button), title_size_group, value_size_group); return button; } static void -hildon_button_set_arrangement (HildonButton *button, - HildonButtonFlags flags) +hildon_button_set_arrangement (HildonButton *button, + HildonButtonArrangement arrangement) { - GtkWidget *box; HildonButtonPrivate *priv; - guint horizontal_spacing; - guint vertical_spacing; - gint width = -1; - gint height = -1; - const char *widget_name = NULL; priv = HILDON_BUTTON_GET_PRIVATE (button); - /* Requested height */ - if (flags & HILDON_BUTTON_FINGER_HEIGHT) { - height = FINGER_BUTTON_HEIGHT; - widget_name = "hildon-finger-button"; - } else if (flags & HILDON_BUTTON_THUMB_HEIGHT) { - height = THUMB_BUTTON_HEIGHT; - widget_name = "hildon-thumb-button"; - } - - if (widget_name) { - gtk_widget_set_name (GTK_WIDGET (button), widget_name); - } - - /* Requested width */ - if (flags & HILDON_BUTTON_HALFSCREEN_WIDTH) { - width = HALFSCREEN_BUTTON_WIDTH; - } else if (flags & HILDON_BUTTON_FULLSCREEN_WIDTH) { - width = FULLSCREEN_BUTTON_WIDTH; - } - - g_object_set (button, - "width-request", width, - "height-request", height, - NULL); - /* Pack everything */ - gtk_widget_style_get (GTK_WIDGET (button), - "horizontal-spacing", &horizontal_spacing, - "vertical-spacing", &vertical_spacing, - NULL); - - if (flags & HILDON_BUTTON_WITH_VERTICAL_VALUE) { - box = gtk_vbox_new (FALSE, vertical_spacing); + if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) { + priv->label_box = gtk_vbox_new (FALSE, 0); + hildon_helper_set_logical_font (GTK_WIDGET (priv->value), "SmallSystemFont"); } else { - box = gtk_hbox_new (FALSE, horizontal_spacing); + priv->label_box = gtk_hbox_new (FALSE, 0); } - gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->title), TRUE, TRUE, 0); - gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->value), TRUE, TRUE, 0); + g_object_ref_sink (priv->label_box); - gtk_container_add (GTK_CONTAINER (priv->alignment), box); + /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they + * can be painted outside of the button in some situations, see + * NB#88126 */ + gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0); + gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0); + + hildon_button_construct_child (button); } +/** + * hildon_button_set_title: + * @button: a #HildonButton + * @title: a new title (main label) for the button, or %NULL + * + * Sets the title (main label) of @button to @title. + * + * This will clear any previously set title. + * + * If @title is set to %NULL, the title label will be hidden and the + * value label will be realigned. + **/ void hildon_button_set_title (HildonButton *button, - const char *title) + const gchar *title) { HildonButtonPrivate *priv; @@ -341,15 +498,34 @@ hildon_button_set_title (HildonButton *button, priv = HILDON_BUTTON_GET_PRIVATE (button); gtk_label_set_text (priv->title, title); - if (title) + /* If the button has no title, hide the label so the value is + * properly aligned */ + if (title) { hildon_button_construct_child (button); + gtk_widget_show (GTK_WIDGET (priv->title)); + } else { + gtk_widget_hide (GTK_WIDGET (priv->title)); + } g_object_notify (G_OBJECT (button), "title"); } +/** + * hildon_button_set_value: + * @button: a #HildonButton + * @value: a new value (secondary label) for the button, or %NULL + * + * Sets the value (secondary label) of @button to @value. + * + * This will clear any previously set value. + * + * If @value is set to %NULL, the value label will be hidden and the + * title label will be realigned. + * + **/ void hildon_button_set_value (HildonButton *button, - const char *value) + const gchar *value) { HildonButtonPrivate *priv; @@ -360,15 +536,27 @@ hildon_button_set_value (HildonButton *button, /* If the button has no value, hide the label so the title is * properly aligned */ - if (value) + if (value) { + hildon_button_construct_child (button); gtk_widget_show (GTK_WIDGET (priv->value)); - else + } else { gtk_widget_hide (GTK_WIDGET (priv->value)); + } g_object_notify (G_OBJECT (button), "value"); } -const char * +/** + * hildon_button_get_title: + * @button: a #HildonButton + * + * Gets the text from the main label (title) of @button, or %NULL if + * none has been set. + * + * Returns: The text of the title label. This string is owned by the + * widget and must not be modified or freed. + **/ +const gchar * hildon_button_get_title (HildonButton *button) { HildonButtonPrivate *priv; @@ -380,7 +568,17 @@ hildon_button_get_title (HildonButton *button) return gtk_label_get_text (priv->title); } -const char * +/** + * hildon_button_get_value: + * @button: a #HildonButton + * + * Gets the text from the secondary label (value) of @button, or %NULL + * if none has been set. + * + * Returns: The text of the value label. This string is owned by the + * widget and must not be modified or freed. + **/ +const gchar * hildon_button_get_value (HildonButton *button) { HildonButtonPrivate *priv; @@ -392,29 +590,279 @@ hildon_button_get_value (HildonButton *button) return gtk_label_get_text (priv->value); } +/** + * hildon_button_set_text: + * @button: a #HildonButton + * @title: new text for the button title (main label) + * @value: new text for the button value (secondary label) + * + * Convenience function to change both labels of a #HildonButton + **/ void hildon_button_set_text (HildonButton *button, - const char *title, - const char *value) + const gchar *title, + const gchar *value) { hildon_button_set_title (button, title); hildon_button_set_value (button, value); } +/** + * hildon_button_set_image: + * @button: a #HildonButton + * @image: a widget to set as the button image + * + * Sets the image of @button to the given widget. The previous image + * (if any) will be removed. + **/ +void +hildon_button_set_image (HildonButton *button, + GtkWidget *image) +{ + HildonButtonPrivate *priv; + + g_return_if_fail (HILDON_IS_BUTTON (button)); + g_return_if_fail (!image || GTK_IS_WIDGET (image)); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + /* Return if there's nothing to do */ + if (image == priv->image) + return; + + if (priv->image && priv->image->parent) + gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image); + + priv->image = image; + + hildon_button_construct_child (button); +} + +/** + * hildon_button_get_image: + * @button: a #HildonButton + * + * Gets the widget that is currenty set as the image of @button, + * previously set with hildon_button_set_image() + * + * Returns: a #GtkWidget or %NULL in case there is no image + **/ +GtkWidget * +hildon_button_get_image (HildonButton *button) +{ + HildonButtonPrivate *priv; + + g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + return priv->image; +} + +/** + * hildon_button_set_image_position: + * @button: a #HildonButton + * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT) + * + * Sets the position of the image inside @button. Only %GTK_POS_LEFT + * and %GTK_POS_RIGHT are currently supported. + **/ +void +hildon_button_set_image_position (HildonButton *button, + GtkPositionType position) +{ + HildonButtonPrivate *priv; + + g_return_if_fail (HILDON_IS_BUTTON (button)); + g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + /* Return if there's nothing to do */ + if (priv->image_position == position) + return; + + priv->image_position = position; + + hildon_button_construct_child (button); +} + +/** + * hildon_button_set_alignment: + * @button: a #HildonButton + * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right). + * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom). + * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1 + * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1 + * + * Sets the alignment of the contents of the widget. If you don't need + * to change @xscale or @yscale you can just use + * gtk_button_set_alignment() instead. + **/ +void +hildon_button_set_alignment (HildonButton *button, + gfloat xalign, + gfloat yalign, + gfloat xscale, + gfloat yscale) +{ + HildonButtonPrivate *priv; + GtkWidget *child; + + g_return_if_fail (HILDON_IS_BUTTON (button)); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + child = gtk_bin_get_child (GTK_BIN (button)); + + if (GTK_IS_ALIGNMENT (child)) { + gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign); + g_object_set (child, "xscale", xscale, "yscale", yscale, NULL); + } +} + +/** + * hildon_button_set_title_alignment: + * @button: a #HildonButton + * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right). + * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom). + * + * Sets the alignment of the title label. See also + * hildon_button_set_alignment() to set the alignment of the whole + * contents of the button. + **/ +void +hildon_button_set_title_alignment (HildonButton *button, + gfloat xalign, + gfloat yalign) +{ + HildonButtonPrivate *priv; + + g_return_if_fail (HILDON_IS_BUTTON (button)); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign); +} + +/** + * hildon_button_set_value_alignment: + * @button: a #HildonButton + * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right). + * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom). + * + * Sets the alignment of the value label. See also + * hildon_button_set_alignment() to set the alignment of the whole + * contents of the button. + **/ +void +hildon_button_set_value_alignment (HildonButton *button, + gfloat xalign, + gfloat yalign) +{ + HildonButtonPrivate *priv; + + g_return_if_fail (HILDON_IS_BUTTON (button)); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + gtk_misc_set_alignment (GTK_MISC (priv->value), xalign, yalign); +} + +/** + * hildon_button_set_image_alignment: + * @button: a #HildonButton + * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right). + * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom). + * + * Sets the alignment of the image. See also + * hildon_button_set_alignment() to set the alignment of the whole + * contents of the button. + **/ +void +hildon_button_set_image_alignment (HildonButton *button, + gfloat xalign, + gfloat yalign) +{ + HildonButtonPrivate *priv; + + g_return_if_fail (HILDON_IS_BUTTON (button)); + + priv = HILDON_BUTTON_GET_PRIVATE (button); + + /* Return if there's nothing to do */ + if (priv->image_xalign == xalign && priv->image_yalign == yalign) + return; + + priv->image_xalign = xalign; + priv->image_yalign = yalign; + + hildon_button_construct_child (button); +} + static void hildon_button_construct_child (HildonButton *button) { HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button); - GtkBin *bin = GTK_BIN (button); + GtkWidget *child; + gint image_spacing; + const gchar *title, *value; - /* Return if there's nothing to do */ - if (bin->child == priv->alignment) + /* Don't do anything if the button is not constructed yet */ + if (G_UNLIKELY (priv->label_box == NULL)) return; - if (bin->child) { - gtk_container_remove (GTK_CONTAINER (button), bin->child); + /* Don't do anything if the button has no contents */ + title = gtk_label_get_text (priv->title); + value = gtk_label_get_text (priv->value); + if (!priv->image && !title[0] && !value[0]) + return; + + /* Save a ref to the image, and remove it from its container if necessary */ + if (priv->image) { + g_object_ref (priv->image); + if (priv->image->parent != NULL) + gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image); + } + + if (priv->label_box->parent != NULL) { + gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box); + } + + /* Remove the child from the container and add priv->alignment */ + child = gtk_bin_get_child (GTK_BIN (button)); + if (child != NULL && child != priv->alignment) { + gtk_container_remove (GTK_CONTAINER (button), child); + child = NULL; + } + + if (child == NULL) { + gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment)); + } + + /* Create a new hbox */ + if (priv->hbox) { + gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox)); + } + gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL); + priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing)); + gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox)); + + /* Pack the image and the alignment in the new hbox */ + if (priv->image && priv->image_position == GTK_POS_LEFT) + gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0); + + gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0); + + if (priv->image && priv->image_position == GTK_POS_RIGHT) + gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0); + + /* Set image alignment and remove previously set ref */ + if (priv->image) { + gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign); + g_object_unref (priv->image); } - gtk_container_add (GTK_CONTAINER (button), priv->alignment); - gtk_widget_show_all (priv->alignment); + /* Show everything */ + gtk_widget_show_all (GTK_WIDGET (priv->alignment)); }