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"
80 #include "hildon-helper.h"
82 G_DEFINE_TYPE (HildonButton, hildon_button, GTK_TYPE_BUTTON);
84 #define HILDON_BUTTON_GET_PRIVATE(obj) \
85 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
86 HILDON_TYPE_BUTTON, HildonButtonPrivate));
88 typedef struct _HildonButtonPrivate HildonButtonPrivate;
90 struct _HildonButtonPrivate
98 GtkPositionType image_position;
111 hildon_button_set_arrangement (HildonButton *button,
112 HildonButtonArrangement arrangement);
115 hildon_button_construct_child (HildonButton *button);
118 hildon_button_set_property (GObject *object,
123 HildonButton *button = HILDON_BUTTON (object);
128 hildon_button_set_title (button, g_value_get_string (value));
131 hildon_button_set_value (button, g_value_get_string (value));
134 hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
136 case PROP_ARRANGEMENT:
137 hildon_button_set_arrangement (button, g_value_get_enum (value));
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 hildon_button_get_property (GObject *object,
151 HildonButton *button = HILDON_BUTTON (object);
156 g_value_set_string (value, hildon_button_get_title (button));
159 g_value_set_string (value, hildon_button_get_value (button));
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168 hildon_button_style_set (GtkWidget *widget,
169 GtkStyle *previous_style)
171 guint horizontal_spacing, vertical_spacing, image_spacing;
172 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (widget);
174 if (GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set)
175 GTK_WIDGET_CLASS (hildon_button_parent_class)->style_set (widget, previous_style);
177 gtk_widget_style_get (widget,
178 "horizontal-spacing", &horizontal_spacing,
179 "vertical-spacing", &vertical_spacing,
180 "image-spacing", &image_spacing,
183 if (GTK_IS_HBOX (priv->label_box)) {
184 gtk_box_set_spacing (GTK_BOX (priv->label_box), horizontal_spacing);
186 gtk_box_set_spacing (GTK_BOX (priv->label_box), vertical_spacing);
189 if (GTK_IS_BOX (priv->hbox)) {
190 gtk_box_set_spacing (priv->hbox, image_spacing);
195 hildon_button_finalize (GObject *object)
197 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (object);
199 g_object_unref (priv->alignment);
200 g_object_unref (priv->label_box);
202 G_OBJECT_CLASS (hildon_button_parent_class)->finalize (object);
206 hildon_button_class_init (HildonButtonClass *klass)
208 GObjectClass *gobject_class = (GObjectClass *)klass;
209 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
211 gobject_class->set_property = hildon_button_set_property;
212 gobject_class->get_property = hildon_button_get_property;
213 gobject_class->finalize = hildon_button_finalize;
214 widget_class->style_set = hildon_button_style_set;
216 g_object_class_install_property (
219 g_param_spec_string (
222 "Text of the title label inside the button",
224 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
226 g_object_class_install_property (
229 g_param_spec_string (
232 "Text of the value label inside the button",
234 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
236 g_object_class_install_property (
242 "Size request for the button",
243 HILDON_TYPE_SIZE_TYPE,
245 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
247 g_object_class_install_property (
253 "How the button contents must be arranged",
254 HILDON_TYPE_BUTTON_ARRANGEMENT,
255 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
256 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
258 gtk_widget_class_install_style_property (
261 "horizontal-spacing",
262 "Horizontal spacing between labels",
263 "Horizontal spacing between the title and value labels, when in horizontal mode",
267 gtk_widget_class_install_style_property (
271 "Vertical spacing between labels",
272 "Vertical spacing between the title and value labels, when in vertical mode",
276 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
280 hildon_button_init (HildonButton *self)
282 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (self);
284 priv->title = GTK_LABEL (gtk_label_new (NULL));
285 priv->value = GTK_LABEL (gtk_label_new (NULL));
286 priv->alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
288 priv->image_position = GTK_POS_LEFT;
289 priv->image_xalign = 0.5;
290 priv->image_yalign = 0.5;
292 priv->label_box = NULL;
294 gtk_widget_set_name (GTK_WIDGET (priv->title), "hildon-button-title");
295 gtk_widget_set_name (GTK_WIDGET (priv->value), "hildon-button-value");
297 gtk_misc_set_alignment (GTK_MISC (priv->title), 0, 0.5);
298 gtk_misc_set_alignment (GTK_MISC (priv->value), 0, 0.5);
300 g_object_ref_sink (priv->alignment);
302 /* The labels are not shown automatically, see hildon_button_set_(title|value) */
303 gtk_widget_set_no_show_all (GTK_WIDGET (priv->title), TRUE);
304 gtk_widget_set_no_show_all (GTK_WIDGET (priv->value), TRUE);
308 * hildon_button_add_title_size_group:
309 * @button: a #HildonButton
310 * @size_group: A #GtkSizeGroup for the button title (main label)
312 * Adds the title label of @button to @size_group.
315 hildon_button_add_title_size_group (HildonButton *button,
316 GtkSizeGroup *size_group)
318 HildonButtonPrivate *priv;
320 g_return_if_fail (HILDON_IS_BUTTON (button));
321 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
323 priv = HILDON_BUTTON_GET_PRIVATE (button);
325 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->title));
329 * hildon_button_add_value_size_group:
330 * @button: a #HildonButton
331 * @size_group: A #GtkSizeGroup for the button value (secondary label)
333 * Adds the value label of @button to @size_group.
336 hildon_button_add_value_size_group (HildonButton *button,
337 GtkSizeGroup *size_group)
339 HildonButtonPrivate *priv;
341 g_return_if_fail (HILDON_IS_BUTTON (button));
342 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
344 priv = HILDON_BUTTON_GET_PRIVATE (button);
346 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->value));
350 * hildon_button_add_image_size_group:
351 * @button: a #HildonButton
352 * @size_group: A #GtkSizeGroup for the button image
354 * Adds the image of @button to @size_group. You must add an image
355 * using hildon_button_set_image() before calling this function.
358 hildon_button_add_image_size_group (HildonButton *button,
359 GtkSizeGroup *size_group)
361 HildonButtonPrivate *priv;
363 g_return_if_fail (HILDON_IS_BUTTON (button));
364 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
366 priv = HILDON_BUTTON_GET_PRIVATE (button);
368 g_return_if_fail (GTK_IS_WIDGET (priv->image));
370 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->image));
374 * hildon_button_add_size_groups:
375 * @button: a #HildonButton
376 * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
377 * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
378 * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
380 * Convenience function to add title, value and image to size
381 * groups. %NULL size groups will be ignored.
384 hildon_button_add_size_groups (HildonButton *button,
385 GtkSizeGroup *title_size_group,
386 GtkSizeGroup *value_size_group,
387 GtkSizeGroup *image_size_group)
389 if (title_size_group)
390 hildon_button_add_title_size_group (button, title_size_group);
392 if (value_size_group)
393 hildon_button_add_value_size_group (button, value_size_group);
395 if (image_size_group)
396 hildon_button_add_image_size_group (button, image_size_group);
401 * @size: Flags to set the size of the button.
402 * @arrangement: How the labels must be arranged.
404 * Creates a new #HildonButton. To set text in the labels, use
405 * hildon_button_set_title() and
406 * hildon_button_set_value(). Alternatively, you can add a custom
407 * child widget using gtk_container_add().
409 * Returns: a new #HildonButton
412 hildon_button_new (HildonSizeType size,
413 HildonButtonArrangement arrangement)
415 return hildon_button_new_with_text (size, arrangement, NULL, NULL);
419 * hildon_button_new_with_text:
420 * @size: Flags to set the size of the button.
421 * @arrangement: How the labels must be arranged.
422 * @title: Title of the button (main label), or %NULL
423 * @value: Value of the button (secondary label), or %NULL
425 * Creates a new #HildonButton with two labels, @title and @value.
427 * If you just don't want to use one of the labels, set it to
428 * %NULL. You can set it to a non-%NULL value at any time later.
430 * Returns: a new #HildonButton
433 hildon_button_new_with_text (HildonSizeType size,
434 HildonButtonArrangement arrangement,
441 button = g_object_new (HILDON_TYPE_BUTTON,
445 "arrangement", arrangement,
452 hildon_button_set_arrangement (HildonButton *button,
453 HildonButtonArrangement arrangement)
455 HildonButtonPrivate *priv;
457 priv = HILDON_BUTTON_GET_PRIVATE (button);
459 /* Pack everything */
460 if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
461 priv->label_box = gtk_vbox_new (FALSE, 0);
462 hildon_helper_set_logical_font (GTK_WIDGET (priv->value), "SmallSystemFont");
464 priv->label_box = gtk_hbox_new (FALSE, 0);
467 g_object_ref_sink (priv->label_box);
469 /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
470 * can be painted outside of the button in some situations, see
472 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
473 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
475 hildon_button_construct_child (button);
479 * hildon_button_set_title:
480 * @button: a #HildonButton
481 * @title: a new title (main label) for the button, or %NULL
483 * Sets the title (main label) of @button to @title.
485 * This will clear any previously set title.
487 * If @title is set to %NULL, the title label will be hidden and the
488 * value label will be realigned.
491 hildon_button_set_title (HildonButton *button,
494 HildonButtonPrivate *priv;
496 g_return_if_fail (HILDON_IS_BUTTON (button));
498 priv = HILDON_BUTTON_GET_PRIVATE (button);
499 gtk_label_set_text (priv->title, title);
501 /* If the button has no title, hide the label so the value is
502 * properly aligned */
504 hildon_button_construct_child (button);
505 gtk_widget_show (GTK_WIDGET (priv->title));
507 gtk_widget_hide (GTK_WIDGET (priv->title));
510 g_object_notify (G_OBJECT (button), "title");
514 * hildon_button_set_value:
515 * @button: a #HildonButton
516 * @value: a new value (secondary label) for the button, or %NULL
518 * Sets the value (secondary label) of @button to @value.
520 * This will clear any previously set value.
522 * If @value is set to %NULL, the value label will be hidden and the
523 * title label will be realigned.
527 hildon_button_set_value (HildonButton *button,
530 HildonButtonPrivate *priv;
532 g_return_if_fail (HILDON_IS_BUTTON (button));
534 priv = HILDON_BUTTON_GET_PRIVATE (button);
535 gtk_label_set_text (priv->value, value);
537 /* If the button has no value, hide the label so the title is
538 * properly aligned */
540 hildon_button_construct_child (button);
541 gtk_widget_show (GTK_WIDGET (priv->value));
543 gtk_widget_hide (GTK_WIDGET (priv->value));
546 g_object_notify (G_OBJECT (button), "value");
550 * hildon_button_get_title:
551 * @button: a #HildonButton
553 * Gets the text from the main label (title) of @button, or %NULL if
556 * Returns: The text of the title label. This string is owned by the
557 * widget and must not be modified or freed.
560 hildon_button_get_title (HildonButton *button)
562 HildonButtonPrivate *priv;
564 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
566 priv = HILDON_BUTTON_GET_PRIVATE (button);
568 return gtk_label_get_text (priv->title);
572 * hildon_button_get_value:
573 * @button: a #HildonButton
575 * Gets the text from the secondary label (value) of @button, or %NULL
576 * if none has been set.
578 * Returns: The text of the value label. This string is owned by the
579 * widget and must not be modified or freed.
582 hildon_button_get_value (HildonButton *button)
584 HildonButtonPrivate *priv;
586 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
588 priv = HILDON_BUTTON_GET_PRIVATE (button);
590 return gtk_label_get_text (priv->value);
594 * hildon_button_set_text:
595 * @button: a #HildonButton
596 * @title: new text for the button title (main label)
597 * @value: new text for the button value (secondary label)
599 * Convenience function to change both labels of a #HildonButton
602 hildon_button_set_text (HildonButton *button,
606 hildon_button_set_title (button, title);
607 hildon_button_set_value (button, value);
611 * hildon_button_set_image:
612 * @button: a #HildonButton
613 * @image: a widget to set as the button image
615 * Sets the image of @button to the given widget. The previous image
616 * (if any) will be removed.
619 hildon_button_set_image (HildonButton *button,
622 HildonButtonPrivate *priv;
624 g_return_if_fail (HILDON_IS_BUTTON (button));
625 g_return_if_fail (!image || GTK_IS_WIDGET (image));
627 priv = HILDON_BUTTON_GET_PRIVATE (button);
629 /* Return if there's nothing to do */
630 if (image == priv->image)
633 if (priv->image && priv->image->parent)
634 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
638 hildon_button_construct_child (button);
642 * hildon_button_get_image:
643 * @button: a #HildonButton
645 * Gets the widget that is currenty set as the image of @button,
646 * previously set with hildon_button_set_image()
648 * Returns: a #GtkWidget or %NULL in case there is no image
651 hildon_button_get_image (HildonButton *button)
653 HildonButtonPrivate *priv;
655 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
657 priv = HILDON_BUTTON_GET_PRIVATE (button);
663 * hildon_button_set_image_position:
664 * @button: a #HildonButton
665 * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
667 * Sets the position of the image inside @button. Only %GTK_POS_LEFT
668 * and %GTK_POS_RIGHT are currently supported.
671 hildon_button_set_image_position (HildonButton *button,
672 GtkPositionType position)
674 HildonButtonPrivate *priv;
676 g_return_if_fail (HILDON_IS_BUTTON (button));
677 g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
679 priv = HILDON_BUTTON_GET_PRIVATE (button);
681 /* Return if there's nothing to do */
682 if (priv->image_position == position)
685 priv->image_position = position;
687 hildon_button_construct_child (button);
691 * hildon_button_set_alignment:
692 * @button: a #HildonButton
693 * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
694 * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
695 * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
696 * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
698 * Sets the alignment of the contents of the widget. If you don't need
699 * to change @xscale or @yscale you can just use
700 * gtk_button_set_alignment() instead.
703 hildon_button_set_alignment (HildonButton *button,
709 HildonButtonPrivate *priv;
712 g_return_if_fail (HILDON_IS_BUTTON (button));
714 priv = HILDON_BUTTON_GET_PRIVATE (button);
716 child = gtk_bin_get_child (GTK_BIN (button));
718 if (GTK_IS_ALIGNMENT (child)) {
719 gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign);
720 g_object_set (child, "xscale", xscale, "yscale", yscale, NULL);
725 * hildon_button_set_title_alignment:
726 * @button: a #HildonButton
727 * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
728 * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
730 * Sets the alignment of the title label. See also
731 * hildon_button_set_alignment() to set the alignment of the whole
732 * contents of the button.
735 hildon_button_set_title_alignment (HildonButton *button,
739 HildonButtonPrivate *priv;
741 g_return_if_fail (HILDON_IS_BUTTON (button));
743 priv = HILDON_BUTTON_GET_PRIVATE (button);
745 gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign);
749 * hildon_button_set_value_alignment:
750 * @button: a #HildonButton
751 * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
752 * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
754 * Sets the alignment of the value label. See also
755 * hildon_button_set_alignment() to set the alignment of the whole
756 * contents of the button.
759 hildon_button_set_value_alignment (HildonButton *button,
763 HildonButtonPrivate *priv;
765 g_return_if_fail (HILDON_IS_BUTTON (button));
767 priv = HILDON_BUTTON_GET_PRIVATE (button);
769 gtk_misc_set_alignment (GTK_MISC (priv->value), xalign, yalign);
773 * hildon_button_set_image_alignment:
774 * @button: a #HildonButton
775 * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
776 * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
778 * Sets the alignment of the image. See also
779 * hildon_button_set_alignment() to set the alignment of the whole
780 * contents of the button.
783 hildon_button_set_image_alignment (HildonButton *button,
787 HildonButtonPrivate *priv;
789 g_return_if_fail (HILDON_IS_BUTTON (button));
791 priv = HILDON_BUTTON_GET_PRIVATE (button);
793 /* Return if there's nothing to do */
794 if (priv->image_xalign == xalign && priv->image_yalign == yalign)
797 priv->image_xalign = xalign;
798 priv->image_yalign = yalign;
800 hildon_button_construct_child (button);
804 hildon_button_construct_child (HildonButton *button)
806 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
809 const gchar *title, *value;
811 /* Don't do anything if the button is not constructed yet */
812 if (G_UNLIKELY (priv->label_box == NULL))
815 /* Don't do anything if the button has no contents */
816 title = gtk_label_get_text (priv->title);
817 value = gtk_label_get_text (priv->value);
818 if (!priv->image && !title[0] && !value[0])
821 /* Save a ref to the image, and remove it from its container if necessary */
823 g_object_ref (priv->image);
824 if (priv->image->parent != NULL)
825 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
828 if (priv->label_box->parent != NULL) {
829 gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
832 /* Remove the child from the container and add priv->alignment */
833 child = gtk_bin_get_child (GTK_BIN (button));
834 if (child != NULL && child != priv->alignment) {
835 gtk_container_remove (GTK_CONTAINER (button), child);
840 gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
843 /* Create a new hbox */
845 gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
847 gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
848 priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
849 gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
851 /* Pack the image and the alignment in the new hbox */
852 if (priv->image && priv->image_position == GTK_POS_LEFT)
853 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
855 gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
857 if (priv->image && priv->image_position == GTK_POS_RIGHT)
858 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
860 /* Set image alignment and remove previously set ref */
862 gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
863 g_object_unref (priv->image);
866 /* Show everything */
867 gtk_widget_show_all (GTK_WIDGET (priv->alignment));