2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
6 * Contact: Karl Lattimer <karl.lattimer@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: Widget representing a button in the Hildon framework.
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.
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 * 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. To change the alignment of the button contents, use
35 * gtk_button_set_alignment()
37 * If only one label is needed, #GtkButton can be used as well, see
38 * also hildon_gtk_button_new().
41 * <title>Creating a HildonButton</title>
44 * button_clicked (HildonButton *button, gpointer user_data)
46 * const gchar *title, *value;
48 * title = hildon_button_get_title (button);
49 * value = hildon_button_get_value (button);
50 * g_debug ("Button clicked with title '%s' and value '%s'", title, value);
54 * create_button (void)
59 * button = hildon_button_new (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
60 * HILDON_BUTTON_ARRANGEMENT_VERTICAL);
61 * hildon_button_set_text (HILDON_BUTTON (button), "Some title", "Some value");
63 * image = gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_BUTTON);
64 * hildon_button_set_image (HILDON_BUTTON (button), image);
65 * hildon_button_set_image_position (HILDON_BUTTON (button), GTK_POS_RIGHT);
67 * gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5);
69 * g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), NULL);
77 #include "hildon-button.h"
78 #include "hildon-enum-types.h"
79 #include "hildon-gtk.h"
81 G_DEFINE_TYPE (HildonButton, hildon_button, GTK_TYPE_BUTTON);
83 #define HILDON_BUTTON_GET_PRIVATE(obj) \
84 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
85 HILDON_TYPE_BUTTON, HildonButtonPrivate));
87 struct _HildonButtonPrivate
95 GtkPositionType image_position;
108 hildon_button_set_arrangement (HildonButton *button,
109 HildonButtonArrangement arrangement);
112 hildon_button_construct_child (HildonButton *button);
115 hildon_button_set_property (GObject *object,
120 HildonButton *button = HILDON_BUTTON (object);
125 hildon_button_set_title (button, g_value_get_string (value));
128 hildon_button_set_value (button, g_value_get_string (value));
131 hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
133 case PROP_ARRANGEMENT:
134 hildon_button_set_arrangement (button, g_value_get_enum (value));
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 hildon_button_get_property (GObject *object,
148 HildonButton *button = HILDON_BUTTON (object);
149 HildonButtonPrivate *priv = HILDON_BUTTON (button)->priv;
154 g_value_set_string (value, gtk_label_get_text (priv->title));
157 g_value_set_string (value, gtk_label_get_text (priv->value));
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 hildon_button_style_set (GtkWidget *widget,
167 GtkStyle *previous_style)
169 guint horizontal_spacing, vertical_spacing, image_spacing;
170 HildonButtonPrivate *priv = HILDON_BUTTON (widget)->priv;
172 if (GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set)
173 GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set (widget, previous_style);
175 gtk_widget_style_get (widget,
176 "horizontal-spacing", &horizontal_spacing,
177 "vertical-spacing", &vertical_spacing,
178 "image-spacing", &image_spacing,
181 if (GTK_IS_HBOX (priv->label_box)) {
182 gtk_box_set_spacing (GTK_BOX (priv->label_box), horizontal_spacing);
184 gtk_box_set_spacing (GTK_BOX (priv->label_box), vertical_spacing);
187 if (GTK_IS_BOX (priv->hbox)) {
188 gtk_box_set_spacing (priv->hbox, image_spacing);
193 hildon_button_class_init (HildonButtonClass *klass)
195 GObjectClass *gobject_class = (GObjectClass *)klass;
196 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
198 gobject_class->set_property = hildon_button_set_property;
199 gobject_class->get_property = hildon_button_get_property;
200 widget_class->style_set = hildon_button_style_set;
202 g_object_class_install_property (
205 g_param_spec_string (
208 "Text of the title label inside the button",
210 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
212 g_object_class_install_property (
215 g_param_spec_string (
218 "Text of the value label inside the button",
220 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
222 g_object_class_install_property (
228 "Size request for the button",
229 HILDON_TYPE_SIZE_TYPE,
231 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
233 g_object_class_install_property (
239 "How the button contents must be arranged",
240 HILDON_TYPE_BUTTON_ARRANGEMENT,
241 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
242 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
244 gtk_widget_class_install_style_property (
247 "horizontal-spacing",
248 "Horizontal spacing between labels",
249 "Horizontal spacing between the title and value labels, when in horizontal mode",
253 gtk_widget_class_install_style_property (
257 "Vertical spacing between labels",
258 "Vertical spacing between the title and value labels, when in vertical mode",
262 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
266 hildon_button_init (HildonButton *self)
268 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
272 priv->title = GTK_LABEL (gtk_label_new (NULL));
273 priv->value = GTK_LABEL (gtk_label_new (NULL));
274 priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
276 priv->image_position = GTK_POS_LEFT;
277 priv->image_xalign = 0.0;
278 priv->image_yalign = 0.0;
280 priv->label_box = NULL;
282 gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
283 gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
285 gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
286 gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
288 /* The labels are not shown automatically, see hildon_button_set_(title|value) */
289 gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
290 gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
294 * hildon_button_add_title_size_group:
295 * @button: a #HildonButton
296 * @size_group: A #GtkSizeGroup for the button title (main label)
298 * Adds the title label of @button to @size_group.
301 hildon_button_add_title_size_group (HildonButton *button,
302 GtkSizeGroup *size_group)
304 g_return_if_fail (HILDON_IS_BUTTON (button));
305 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
307 gtk_size_group_add_widget (size_group, GTK_WIDGET (button->priv->title));
311 * hildon_button_add_value_size_group:
312 * @button: a #HildonButton
313 * @size_group: A #GtkSizeGroup for the button value (secondary label)
315 * Adds the value label of @button to @size_group.
318 hildon_button_add_value_size_group (HildonButton *button,
319 GtkSizeGroup *size_group)
321 g_return_if_fail (HILDON_IS_BUTTON (button));
322 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
324 gtk_size_group_add_widget (size_group, GTK_WIDGET (button->priv->value));
328 * hildon_button_add_image_size_group:
329 * @button: a #HildonButton
330 * @size_group: A #GtkSizeGroup for the button image
332 * Adds the image of @button to @size_group. You must add an image
333 * using hildon_button_set_image() before calling this function.
336 hildon_button_add_image_size_group (HildonButton *button,
337 GtkSizeGroup *size_group)
339 g_return_if_fail (HILDON_IS_BUTTON (button));
340 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
342 g_return_if_fail (GTK_IS_WIDGET (button->priv->image));
344 gtk_size_group_add_widget (size_group, GTK_WIDGET (button->priv->image));
348 * hildon_button_add_size_groups:
349 * @button: a #HildonButton
350 * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
351 * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
352 * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
354 * Convenience function to add title, value and image to size
355 * groups. %NULL size groups will be ignored.
358 hildon_button_add_size_groups (HildonButton *button,
359 GtkSizeGroup *title_size_group,
360 GtkSizeGroup *value_size_group,
361 GtkSizeGroup *image_size_group)
363 if (title_size_group)
364 hildon_button_add_title_size_group (button, title_size_group);
366 if (value_size_group)
367 hildon_button_add_value_size_group (button, value_size_group);
369 if (image_size_group)
370 hildon_button_add_image_size_group (button, image_size_group);
375 * @size: Flags to set the size of the button.
376 * @arrangement: How the labels must be arranged.
378 * Creates a new #HildonButton. To set text in the labels, use
379 * hildon_button_set_title() and
380 * hildon_button_set_value(). Alternatively, you can add a custom
381 * child widget using gtk_container_add().
383 * Returns: a new #HildonButton
386 hildon_button_new (HildonSizeType size,
387 HildonButtonArrangement arrangement)
389 return hildon_button_new_with_text (size, arrangement, NULL, NULL);
393 * hildon_button_new_with_text:
394 * @size: Flags to set the size of the button.
395 * @arrangement: How the labels must be arranged.
396 * @title: Title of the button (main label), or %NULL
397 * @value: Value of the button (secondary label), or %NULL
399 * Creates a new #HildonButton with two labels, @title and @value.
401 * If you just don't want to use one of the labels, set it to
402 * %NULL. You can set it to a non-%NULL value at any time later.
404 * Returns: a new #HildonButton
407 hildon_button_new_with_text (HildonSizeType size,
408 HildonButtonArrangement arrangement,
415 button = g_object_new (HILDON_TYPE_BUTTON,
419 "arrangement", arrangement,
426 hildon_button_set_arrangement (HildonButton *button,
427 HildonButtonArrangement arrangement)
429 HildonButtonPrivate *priv = button->priv;
431 /* Pack everything */
432 if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
433 priv->label_box = gtk_vbox_new (FALSE, 0);
435 priv->label_box = gtk_hbox_new (FALSE, 0);
438 /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
439 * can be painted outside of the button in some situations, see
441 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
442 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
444 hildon_button_construct_child (button);
448 * hildon_button_set_title:
449 * @button: a #HildonButton
450 * @title: a new title (main label) for the button, or %NULL
452 * Sets the title (main label) of @button to @title.
454 * This will clear any previously set title.
456 * If @title is set to %NULL, the title label will be hidden and the
457 * value label will be realigned.
460 hildon_button_set_title (HildonButton *button,
463 HildonButtonPrivate *priv;
465 g_return_if_fail (HILDON_IS_BUTTON (button));
468 gtk_label_set_text (priv->title, title);
470 /* If the button has no title, hide the label so the value is
471 * properly aligned */
473 hildon_button_construct_child (button);
474 gtk_widget_show (GTK_WIDGET (priv->title));
476 gtk_widget_hide (GTK_WIDGET (priv->title));
479 g_object_notify (G_OBJECT (button), "title");
483 * hildon_button_set_value:
484 * @button: a #HildonButton
485 * @value: a new value (secondary label) for the button, or %NULL
487 * Sets the value (secondary label) of @button to @value.
489 * This will clear any previously set value.
491 * If @value is set to %NULL, the value label will be hidden and the
492 * title label will be realigned.
496 hildon_button_set_value (HildonButton *button,
499 HildonButtonPrivate *priv;
501 g_return_if_fail (HILDON_IS_BUTTON (button));
504 gtk_label_set_text (priv->value, value);
506 /* If the button has no value, hide the label so the title is
507 * properly aligned */
509 hildon_button_construct_child (button);
510 gtk_widget_show (GTK_WIDGET (priv->value));
512 gtk_widget_hide (GTK_WIDGET (priv->value));
515 g_object_notify (G_OBJECT (button), "value");
519 * hildon_button_get_title:
520 * @button: a #HildonButton
522 * Gets the text from the main label (title) of @button, or %NULL if
525 * Returns: The text of the title label. This string is owned by the
526 * widget and must not be modified or freed.
529 hildon_button_get_title (HildonButton *button)
531 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
533 return gtk_label_get_text (button->priv->title);
537 * hildon_button_get_value:
538 * @button: a #HildonButton
540 * Gets the text from the secondary label (value) of @button, or %NULL
541 * if none has been set.
543 * Returns: The text of the value label. This string is owned by the
544 * widget and must not be modified or freed.
547 hildon_button_get_value (HildonButton *button)
549 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
551 return gtk_label_get_text (button->priv->value);
555 * hildon_button_set_text:
556 * @button: a #HildonButton
557 * @title: new text for the button title (main label)
558 * @value: new text for the button value (secondary label)
560 * Convenience function to change both labels of a #HildonButton
563 hildon_button_set_text (HildonButton *button,
567 hildon_button_set_title (button, title);
568 hildon_button_set_value (button, value);
572 * hildon_button_set_image:
573 * @button: a #HildonButton
574 * @image: a widget to set as the button image
576 * Sets the image of @button to the given widget. The previous image
577 * (if any) will be removed.
580 hildon_button_set_image (HildonButton *button,
583 HildonButtonPrivate *priv;
585 g_return_if_fail (HILDON_IS_BUTTON (button));
586 g_return_if_fail (!image || GTK_IS_WIDGET (image));
590 /* Return if there's nothing to do */
591 if (image == priv->image)
594 if (priv->image && priv->image->parent)
595 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
599 hildon_button_construct_child (button);
603 * hildon_button_get_image:
604 * @button: a #HildonButton
606 * Gets the widget that is currenty set as the image of @button,
607 * previously set with hildon_button_set_image()
609 * Returns: a #GtkWidget or %NULL in case there is no image
612 hildon_button_get_image (HildonButton *button)
614 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
616 return button->priv->image;
620 * hildon_button_set_image_position:
621 * @button: a #HildonButton
622 * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
624 * Sets the position of the image inside @button. Only %GTK_POS_LEFT
625 * and %GTK_POS_RIGHT are currently supported.
628 hildon_button_set_image_position (HildonButton *button,
629 GtkPositionType position)
631 g_return_if_fail (HILDON_IS_BUTTON (button));
632 g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
634 /* Return if there's nothing to do */
635 if (button->priv->image_position == position)
638 button->priv->image_position = position;
640 hildon_button_construct_child (button);
644 * hildon_button_set_alignment:
645 * @button: a #HildonButton
646 * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
647 * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
648 * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
649 * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
651 * Sets the alignment of the contents of the widget. If you don't need
652 * to change @xscale or @yscale you can just use
653 * gtk_button_set_alignment() instead.
656 hildon_button_set_alignment (HildonButton *button,
664 g_return_if_fail (HILDON_IS_BUTTON (button));
666 child = gtk_bin_get_child (GTK_BIN (button));
668 if (GTK_IS_ALIGNMENT (child)) {
669 gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign);
670 g_object_set (child, "xscale", xscale, "yscale", yscale, NULL);
675 * hildon_button_set_title_alignment:
676 * @button: a #HildonButton
677 * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
678 * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
680 * Sets the alignment of the title label. See also
681 * hildon_button_set_alignment() to set the alignment of the whole
682 * contents of the button.
685 hildon_button_set_title_alignment (HildonButton *button,
689 g_return_if_fail (HILDON_IS_BUTTON (button));
691 gtk_misc_set_alignment (GTK_MISC (button->priv->title), xalign, yalign);
695 * hildon_button_set_value_alignment:
696 * @button: a #HildonButton
697 * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
698 * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
700 * Sets the alignment of the value label. See also
701 * hildon_button_set_alignment() to set the alignment of the whole
702 * contents of the button.
705 hildon_button_set_value_alignment (HildonButton *button,
709 g_return_if_fail (HILDON_IS_BUTTON (button));
711 gtk_misc_set_alignment (GTK_MISC (button->priv->value), xalign, yalign);
715 * hildon_button_set_image_alignment:
716 * @button: a #HildonButton
717 * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
718 * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
720 * Sets the alignment of the image. See also
721 * hildon_button_set_alignment() to set the alignment of the whole
722 * contents of the button.
725 hildon_button_set_image_alignment (HildonButton *button,
729 HildonButtonPrivate *priv;
731 g_return_if_fail (HILDON_IS_BUTTON (button));
735 /* Return if there's nothing to do */
736 if (priv->image_xalign == xalign && priv->image_yalign == yalign)
739 priv->image_xalign = xalign;
740 priv->image_yalign = yalign;
742 hildon_button_construct_child (button);
746 hildon_button_construct_child (HildonButton *button)
748 HildonButtonPrivate *priv = button->priv;
752 /* Don't do anything if the button is not constructed yet */
753 if (priv->label_box == NULL)
756 /* Save a ref to the image if necessary */
757 if (priv->image && priv->image->parent != NULL) {
758 g_object_ref (priv->image);
759 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
762 /* Save a ref to the label box if necessary */
763 if (priv->label_box->parent != NULL) {
764 g_object_ref (priv->label_box);
765 gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
768 /* Remove the child from the container and add priv->alignment */
769 child = gtk_bin_get_child (GTK_BIN (button));
770 if (child != NULL && child != priv->alignment) {
771 gtk_container_remove (GTK_CONTAINER (button), child);
776 gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
779 /* Create a new hbox */
781 gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
783 gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
784 priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
785 gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
787 /* Pack the image and the alignment in the new hbox */
788 if (priv->image && priv->image_position == GTK_POS_LEFT)
789 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
791 gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
793 if (priv->image && priv->image_position == GTK_POS_RIGHT)
794 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
796 /* Set image alignment */
798 gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
800 /* Show everything */
801 gtk_widget_show_all (GTK_WIDGET (priv->alignment));