* "auto" so it behaves like a standard GtkButton.
*
* The #HildonButton can hold any valid child widget, but it usually
- * contains two labels, named title and value. To change the alignment
- * of the labels, use gtk_button_set_alignment()
+ * 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().
+ *
+ * <example>
+ * <title>Creating a HildonButton</title>
+ * <programlisting>
+ * 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;
+ * }
+ * </programlisting>
+ * </example>
*/
#include "hildon-button.h"
#include "hildon-enum-types.h"
+#include "hildon-gtk.h"
G_DEFINE_TYPE (HildonButton, hildon_button, GTK_TYPE_BUTTON);
GtkWidget *alignment;
GtkWidget *image;
GtkPositionType image_position;
+ gfloat image_xalign;
+ gfloat image_yalign;
};
enum {
}
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_class_init (HildonButtonClass *klass)
{
GObjectClass *gobject_class = (GObjectClass *)klass;
gobject_class->set_property = hildon_button_set_property;
gobject_class->get_property = hildon_button_get_property;
+ widget_class->style_set = hildon_button_style_set;
g_object_class_install_property (
gobject_class,
priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
priv->image = NULL;
priv->image_position = GTK_POS_LEFT;
+ priv->image_xalign = 0.0;
+ priv->image_yalign = 0.0;
priv->hbox = NULL;
priv->label_box = NULL;
* @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.
+ * using hildon_button_set_image() before calling this function.
**/
void
hildon_button_add_image_size_group (HildonButton *button,
g_return_if_fail (HILDON_IS_BUTTON (button));
g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
- g_return_if_fail (GTK_IS_WIDGET (priv->image));
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));
}
* @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
**/
HildonButtonArrangement arrangement)
{
HildonButtonPrivate *priv;
- guint horizontal_spacing;
- guint vertical_spacing;
priv = HILDON_BUTTON_GET_PRIVATE (button);
/* Pack everything */
- gtk_widget_style_get (GTK_WIDGET (button),
- "horizontal-spacing", &horizontal_spacing,
- "vertical-spacing", &vertical_spacing,
- NULL);
-
if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
- priv->label_box = gtk_vbox_new (FALSE, vertical_spacing);
+ priv->label_box = gtk_vbox_new (FALSE, 0);
} else {
- priv->label_box = gtk_hbox_new (FALSE, horizontal_spacing);
+ priv->label_box = gtk_hbox_new (FALSE, 0);
}
- gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), FALSE, FALSE, 0);
+ /* 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);
*
* Sets the title (main label) of @button to @title.
*
- * This will clear the previously set 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.
*
* Sets the value (secondary label) of @button to @value.
*
- * This will clear the previously set 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.
}
/**
+ * 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 left and right
- * are supported.
+ * 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,
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);
GtkWidget *child;
+ gint image_spacing;
/* Don't do anything if the button is not constructed yet */
if (priv->label_box == NULL)
if (priv->hbox) {
gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
}
- priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, 10));
+ 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_RIGHT)
gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
+ /* Set image alignment */
+ if (priv->image)
+ gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
+
/* Show everything */
gtk_widget_show_all (GTK_WIDGET (priv->alignment));
}