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);
153 g_value_set_string (value, hildon_button_get_title (button));
156 g_value_set_string (value, hildon_button_get_value (button));
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 hildon_button_style_set (GtkWidget *widget,
166 GtkStyle *previous_style)
168 guint horizontal_spacing, vertical_spacing, image_spacing;
169 HildonButtonPrivate *priv = HILDON_BUTTON (widget)->priv;
171 if (GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set)
172 GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set (widget, previous_style);
174 gtk_widget_style_get (widget,
175 "horizontal-spacing", &horizontal_spacing,
176 "vertical-spacing", &vertical_spacing,
177 "image-spacing", &image_spacing,
180 if (GTK_IS_HBOX (priv->label_box)) {
181 gtk_box_set_spacing (GTK_BOX (priv->label_box), horizontal_spacing);
183 gtk_box_set_spacing (GTK_BOX (priv->label_box), vertical_spacing);
186 if (GTK_IS_BOX (priv->hbox)) {
187 gtk_box_set_spacing (priv->hbox, image_spacing);
192 hildon_button_finalize (GObject *object)
194 HildonButtonPrivate *priv = HILDON_BUTTON (object)->priv;
196 g_object_unref (priv->alignment);
197 g_object_unref (priv->label_box);
199 G_OBJECT_CLASS (hildon_button_parent_class)->finalize (object);
203 hildon_button_class_init (HildonButtonClass *klass)
205 GObjectClass *gobject_class = (GObjectClass *)klass;
206 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
208 gobject_class->set_property = hildon_button_set_property;
209 gobject_class->get_property = hildon_button_get_property;
210 gobject_class->finalize = hildon_button_finalize;
211 widget_class->style_set = hildon_button_style_set;
213 g_object_class_install_property (
216 g_param_spec_string (
219 "Text of the title label inside the button",
221 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
223 g_object_class_install_property (
226 g_param_spec_string (
229 "Text of the value label inside the button",
231 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
233 g_object_class_install_property (
239 "Size request for the button",
240 HILDON_TYPE_SIZE_TYPE,
242 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
244 g_object_class_install_property (
250 "How the button contents must be arranged",
251 HILDON_TYPE_BUTTON_ARRANGEMENT,
252 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
253 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
255 gtk_widget_class_install_style_property (
258 "horizontal-spacing",
259 "Horizontal spacing between labels",
260 "Horizontal spacing between the title and value labels, when in horizontal mode",
264 gtk_widget_class_install_style_property (
268 "Vertical spacing between labels",
269 "Vertical spacing between the title and value labels, when in vertical mode",
273 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
277 hildon_button_init (HildonButton *self)
279 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
283 priv->title = GTK_LABEL (gtk_label_new (NULL));
284 priv->value = GTK_LABEL (gtk_label_new (NULL));
285 priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
287 priv->image_position = GTK_POS_LEFT;
288 priv->image_xalign = 0.0;
289 priv->image_yalign = 0.0;
291 priv->label_box = NULL;
293 gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
294 gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
296 gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
297 gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
299 g_object_ref_sink (priv->alignment);
301 /* The labels are not shown automatically, see hildon_button_set_(title|value) */
302 gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
303 gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
307 * hildon_button_add_title_size_group:
308 * @button: a #HildonButton
309 * @size_group: A #GtkSizeGroup for the button title (main label)
311 * Adds the title label of @button to @size_group.
314 hildon_button_add_title_size_group (HildonButton *button,
315 GtkSizeGroup *size_group)
317 g_return_if_fail (HILDON_IS_BUTTON (button));
318 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
320 gtk_size_group_add_widget (size_group, GTK_WIDGET (button->priv->title));
324 * hildon_button_add_value_size_group:
325 * @button: a #HildonButton
326 * @size_group: A #GtkSizeGroup for the button value (secondary label)
328 * Adds the value label of @button to @size_group.
331 hildon_button_add_value_size_group (HildonButton *button,
332 GtkSizeGroup *size_group)
334 g_return_if_fail (HILDON_IS_BUTTON (button));
335 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
337 gtk_size_group_add_widget (size_group, GTK_WIDGET (button->priv->value));
341 * hildon_button_add_image_size_group:
342 * @button: a #HildonButton
343 * @size_group: A #GtkSizeGroup for the button image
345 * Adds the image of @button to @size_group. You must add an image
346 * using hildon_button_set_image() before calling this function.
349 hildon_button_add_image_size_group (HildonButton *button,
350 GtkSizeGroup *size_group)
352 g_return_if_fail (HILDON_IS_BUTTON (button));
353 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
355 g_return_if_fail (GTK_IS_WIDGET (button->priv->image));
357 gtk_size_group_add_widget (size_group, GTK_WIDGET (button->priv->image));
361 * hildon_button_add_size_groups:
362 * @button: a #HildonButton
363 * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
364 * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
365 * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
367 * Convenience function to add title, value and image to size
368 * groups. %NULL size groups will be ignored.
371 hildon_button_add_size_groups (HildonButton *button,
372 GtkSizeGroup *title_size_group,
373 GtkSizeGroup *value_size_group,
374 GtkSizeGroup *image_size_group)
376 if (title_size_group)
377 hildon_button_add_title_size_group (button, title_size_group);
379 if (value_size_group)
380 hildon_button_add_value_size_group (button, value_size_group);
382 if (image_size_group)
383 hildon_button_add_image_size_group (button, image_size_group);
388 * @size: Flags to set the size of the button.
389 * @arrangement: How the labels must be arranged.
391 * Creates a new #HildonButton. To set text in the labels, use
392 * hildon_button_set_title() and
393 * hildon_button_set_value(). Alternatively, you can add a custom
394 * child widget using gtk_container_add().
396 * Returns: a new #HildonButton
399 hildon_button_new (HildonSizeType size,
400 HildonButtonArrangement arrangement)
402 return hildon_button_new_with_text (size, arrangement, NULL, NULL);
406 * hildon_button_new_with_text:
407 * @size: Flags to set the size of the button.
408 * @arrangement: How the labels must be arranged.
409 * @title: Title of the button (main label), or %NULL
410 * @value: Value of the button (secondary label), or %NULL
412 * Creates a new #HildonButton with two labels, @title and @value.
414 * If you just don't want to use one of the labels, set it to
415 * %NULL. You can set it to a non-%NULL value at any time later.
417 * Returns: a new #HildonButton
420 hildon_button_new_with_text (HildonSizeType size,
421 HildonButtonArrangement arrangement,
428 button = g_object_new (HILDON_TYPE_BUTTON,
432 "arrangement", arrangement,
439 hildon_button_set_arrangement (HildonButton *button,
440 HildonButtonArrangement arrangement)
442 HildonButtonPrivate *priv = button->priv;
444 /* Pack everything */
445 if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
446 priv->label_box = gtk_vbox_new (FALSE, 0);
448 priv->label_box = gtk_hbox_new (FALSE, 0);
451 g_object_ref_sink (priv->label_box);
453 /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
454 * can be painted outside of the button in some situations, see
456 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
457 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
459 hildon_button_construct_child (button);
463 * hildon_button_set_title:
464 * @button: a #HildonButton
465 * @title: a new title (main label) for the button, or %NULL
467 * Sets the title (main label) of @button to @title.
469 * This will clear any previously set title.
471 * If @title is set to %NULL, the title label will be hidden and the
472 * value label will be realigned.
475 hildon_button_set_title (HildonButton *button,
478 HildonButtonPrivate *priv;
480 g_return_if_fail (HILDON_IS_BUTTON (button));
483 gtk_label_set_text (priv->title, title);
485 /* If the button has no title, hide the label so the value is
486 * properly aligned */
488 hildon_button_construct_child (button);
489 gtk_widget_show (GTK_WIDGET (priv->title));
491 gtk_widget_hide (GTK_WIDGET (priv->title));
494 g_object_notify (G_OBJECT (button), "title");
498 * hildon_button_set_value:
499 * @button: a #HildonButton
500 * @value: a new value (secondary label) for the button, or %NULL
502 * Sets the value (secondary label) of @button to @value.
504 * This will clear any previously set value.
506 * If @value is set to %NULL, the value label will be hidden and the
507 * title label will be realigned.
511 hildon_button_set_value (HildonButton *button,
514 HildonButtonPrivate *priv;
516 g_return_if_fail (HILDON_IS_BUTTON (button));
519 gtk_label_set_text (priv->value, value);
521 /* If the button has no value, hide the label so the title is
522 * properly aligned */
524 hildon_button_construct_child (button);
525 gtk_widget_show (GTK_WIDGET (priv->value));
527 gtk_widget_hide (GTK_WIDGET (priv->value));
530 g_object_notify (G_OBJECT (button), "value");
534 * hildon_button_get_title:
535 * @button: a #HildonButton
537 * Gets the text from the main label (title) of @button, or %NULL if
540 * Returns: The text of the title label. This string is owned by the
541 * widget and must not be modified or freed.
544 hildon_button_get_title (HildonButton *button)
546 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
548 return gtk_label_get_text (button->priv->title);
552 * hildon_button_get_value:
553 * @button: a #HildonButton
555 * Gets the text from the secondary label (value) of @button, or %NULL
556 * if none has been set.
558 * Returns: The text of the value label. This string is owned by the
559 * widget and must not be modified or freed.
562 hildon_button_get_value (HildonButton *button)
564 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
566 return gtk_label_get_text (button->priv->value);
570 * hildon_button_set_text:
571 * @button: a #HildonButton
572 * @title: new text for the button title (main label)
573 * @value: new text for the button value (secondary label)
575 * Convenience function to change both labels of a #HildonButton
578 hildon_button_set_text (HildonButton *button,
582 hildon_button_set_title (button, title);
583 hildon_button_set_value (button, value);
587 * hildon_button_set_image:
588 * @button: a #HildonButton
589 * @image: a widget to set as the button image
591 * Sets the image of @button to the given widget. The previous image
592 * (if any) will be removed.
595 hildon_button_set_image (HildonButton *button,
598 HildonButtonPrivate *priv;
600 g_return_if_fail (HILDON_IS_BUTTON (button));
601 g_return_if_fail (!image || GTK_IS_WIDGET (image));
605 /* Return if there's nothing to do */
606 if (image == priv->image)
609 if (priv->image && priv->image->parent)
610 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
614 hildon_button_construct_child (button);
618 * hildon_button_get_image:
619 * @button: a #HildonButton
621 * Gets the widget that is currenty set as the image of @button,
622 * previously set with hildon_button_set_image()
624 * Returns: a #GtkWidget or %NULL in case there is no image
627 hildon_button_get_image (HildonButton *button)
629 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
631 return button->priv->image;
635 * hildon_button_set_image_position:
636 * @button: a #HildonButton
637 * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
639 * Sets the position of the image inside @button. Only %GTK_POS_LEFT
640 * and %GTK_POS_RIGHT are currently supported.
643 hildon_button_set_image_position (HildonButton *button,
644 GtkPositionType position)
646 g_return_if_fail (HILDON_IS_BUTTON (button));
647 g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
649 /* Return if there's nothing to do */
650 if (button->priv->image_position == position)
653 button->priv->image_position = position;
655 hildon_button_construct_child (button);
659 * hildon_button_set_alignment:
660 * @button: a #HildonButton
661 * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
662 * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
663 * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
664 * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
666 * Sets the alignment of the contents of the widget. If you don't need
667 * to change @xscale or @yscale you can just use
668 * gtk_button_set_alignment() instead.
671 hildon_button_set_alignment (HildonButton *button,
679 g_return_if_fail (HILDON_IS_BUTTON (button));
681 child = gtk_bin_get_child (GTK_BIN (button));
683 if (GTK_IS_ALIGNMENT (child)) {
684 gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign);
685 g_object_set (child, "xscale", xscale, "yscale", yscale, NULL);
690 * hildon_button_set_title_alignment:
691 * @button: a #HildonButton
692 * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
693 * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
695 * Sets the alignment of the title label. See also
696 * hildon_button_set_alignment() to set the alignment of the whole
697 * contents of the button.
700 hildon_button_set_title_alignment (HildonButton *button,
704 g_return_if_fail (HILDON_IS_BUTTON (button));
706 gtk_misc_set_alignment (GTK_MISC (button->priv->title), xalign, yalign);
710 * hildon_button_set_value_alignment:
711 * @button: a #HildonButton
712 * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
713 * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
715 * Sets the alignment of the value label. See also
716 * hildon_button_set_alignment() to set the alignment of the whole
717 * contents of the button.
720 hildon_button_set_value_alignment (HildonButton *button,
724 g_return_if_fail (HILDON_IS_BUTTON (button));
726 gtk_misc_set_alignment (GTK_MISC (button->priv->value), xalign, yalign);
730 * hildon_button_set_image_alignment:
731 * @button: a #HildonButton
732 * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
733 * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
735 * Sets the alignment of the image. See also
736 * hildon_button_set_alignment() to set the alignment of the whole
737 * contents of the button.
740 hildon_button_set_image_alignment (HildonButton *button,
744 HildonButtonPrivate *priv;
746 g_return_if_fail (HILDON_IS_BUTTON (button));
750 /* Return if there's nothing to do */
751 if (priv->image_xalign == xalign && priv->image_yalign == yalign)
754 priv->image_xalign = xalign;
755 priv->image_yalign = yalign;
757 hildon_button_construct_child (button);
761 hildon_button_construct_child (HildonButton *button)
763 HildonButtonPrivate *priv = button->priv;
766 const gchar *title, *value;
768 /* Don't do anything if the button is not constructed yet */
769 if (G_UNLIKELY (priv->label_box == NULL))
772 /* Don't do anything if the button has no contents */
773 title = gtk_label_get_text (priv->title);
774 value = gtk_label_get_text (priv->value);
775 if (!priv->image && !title[0] && !value[0])
778 /* Save a ref to the image, and remove it from its container if necessary */
780 g_object_ref (priv->image);
781 if (priv->image->parent != NULL)
782 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
785 if (priv->label_box->parent != NULL) {
786 gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
789 /* Remove the child from the container and add priv->alignment */
790 child = gtk_bin_get_child (GTK_BIN (button));
791 if (child != NULL && child != priv->alignment) {
792 gtk_container_remove (GTK_CONTAINER (button), child);
797 gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
800 /* Create a new hbox */
802 gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
804 gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
805 priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
806 gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
808 /* Pack the image and the alignment in the new hbox */
809 if (priv->image && priv->image_position == GTK_POS_LEFT)
810 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
812 gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
814 if (priv->image && priv->image_position == GTK_POS_RIGHT)
815 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
817 /* Set image alignment and remove previously set ref */
819 gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
820 g_object_unref (priv->image);
823 /* Show everything */
824 gtk_widget_show_all (GTK_WIDGET (priv->alignment));