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 typedef struct _HildonButtonPrivate HildonButtonPrivate;
89 struct _HildonButtonPrivate
97 GtkPositionType image_position;
110 hildon_button_set_arrangement (HildonButton *button,
111 HildonButtonArrangement arrangement);
114 hildon_button_construct_child (HildonButton *button);
117 hildon_button_set_property (GObject *object,
122 HildonButton *button = HILDON_BUTTON (object);
127 hildon_button_set_title (button, g_value_get_string (value));
130 hildon_button_set_value (button, g_value_get_string (value));
133 hildon_gtk_widget_set_theme_size (GTK_WIDGET (button), g_value_get_flags (value));
135 case PROP_ARRANGEMENT:
136 hildon_button_set_arrangement (button, g_value_get_enum (value));
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 hildon_button_get_property (GObject *object,
150 HildonButton *button = HILDON_BUTTON (object);
151 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
156 g_value_set_string (value, gtk_label_get_text (priv->title));
159 g_value_set_string (value, gtk_label_get_text (priv->value));
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_class_init (HildonButtonClass *klass)
197 GObjectClass *gobject_class = (GObjectClass *)klass;
198 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
200 gobject_class->set_property = hildon_button_set_property;
201 gobject_class->get_property = hildon_button_get_property;
202 widget_class->style_set = hildon_button_style_set;
204 g_object_class_install_property (
207 g_param_spec_string (
210 "Text of the title label inside the button",
212 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
214 g_object_class_install_property (
217 g_param_spec_string (
220 "Text of the value label inside the button",
222 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
224 g_object_class_install_property (
230 "Size request for the button",
231 HILDON_TYPE_SIZE_TYPE,
233 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
235 g_object_class_install_property (
241 "How the button contents must be arranged",
242 HILDON_TYPE_BUTTON_ARRANGEMENT,
243 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
244 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
246 gtk_widget_class_install_style_property (
249 "horizontal-spacing",
250 "Horizontal spacing between labels",
251 "Horizontal spacing between the title and value labels, when in horizontal mode",
255 gtk_widget_class_install_style_property (
259 "Vertical spacing between labels",
260 "Vertical spacing between the title and value labels, when in vertical mode",
264 g_type_class_add_private (klass, sizeof (HildonButtonPrivate));
268 hildon_button_init (HildonButton *self)
270 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 HildonButtonPrivate *priv;
306 g_return_if_fail (HILDON_IS_BUTTON (button));
307 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
309 priv = HILDON_BUTTON_GET_PRIVATE (button);
311 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->title));
315 * hildon_button_add_value_size_group:
316 * @button: a #HildonButton
317 * @size_group: A #GtkSizeGroup for the button value (secondary label)
319 * Adds the value label of @button to @size_group.
322 hildon_button_add_value_size_group (HildonButton *button,
323 GtkSizeGroup *size_group)
325 HildonButtonPrivate *priv;
327 g_return_if_fail (HILDON_IS_BUTTON (button));
328 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
330 priv = HILDON_BUTTON_GET_PRIVATE (button);
332 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->value));
336 * hildon_button_add_image_size_group:
337 * @button: a #HildonButton
338 * @size_group: A #GtkSizeGroup for the button image
340 * Adds the image of @button to @size_group. You must add an image
341 * using hildon_button_set_image() before calling this function.
344 hildon_button_add_image_size_group (HildonButton *button,
345 GtkSizeGroup *size_group)
347 HildonButtonPrivate *priv;
349 g_return_if_fail (HILDON_IS_BUTTON (button));
350 g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
352 priv = HILDON_BUTTON_GET_PRIVATE (button);
354 g_return_if_fail (GTK_IS_WIDGET (priv->image));
356 gtk_size_group_add_widget (size_group, GTK_WIDGET (priv->image));
360 * hildon_button_add_size_groups:
361 * @button: a #HildonButton
362 * @title_size_group: A #GtkSizeGroup for the button title (main label), or %NULL
363 * @value_size_group: A #GtkSizeGroup group for the button value (secondary label), or %NULL
364 * @image_size_group: A #GtkSizeGroup group for the button image, or %NULL
366 * Convenience function to add title, value and image to size
367 * groups. %NULL size groups will be ignored.
370 hildon_button_add_size_groups (HildonButton *button,
371 GtkSizeGroup *title_size_group,
372 GtkSizeGroup *value_size_group,
373 GtkSizeGroup *image_size_group)
375 if (title_size_group)
376 hildon_button_add_title_size_group (button, title_size_group);
378 if (value_size_group)
379 hildon_button_add_value_size_group (button, value_size_group);
381 if (image_size_group)
382 hildon_button_add_image_size_group (button, image_size_group);
387 * @size: Flags to set the size of the button.
388 * @arrangement: How the labels must be arranged.
390 * Creates a new #HildonButton. To set text in the labels, use
391 * hildon_button_set_title() and
392 * hildon_button_set_value(). Alternatively, you can add a custom
393 * child widget using gtk_container_add().
395 * Returns: a new #HildonButton
398 hildon_button_new (HildonSizeType size,
399 HildonButtonArrangement arrangement)
401 return hildon_button_new_with_text (size, arrangement, NULL, NULL);
405 * hildon_button_new_with_text:
406 * @size: Flags to set the size of the button.
407 * @arrangement: How the labels must be arranged.
408 * @title: Title of the button (main label), or %NULL
409 * @value: Value of the button (secondary label), or %NULL
411 * Creates a new #HildonButton with two labels, @title and @value.
413 * If you just don't want to use one of the labels, set it to
414 * %NULL. You can set it to a non-%NULL value at any time later.
416 * Returns: a new #HildonButton
419 hildon_button_new_with_text (HildonSizeType size,
420 HildonButtonArrangement arrangement,
427 button = g_object_new (HILDON_TYPE_BUTTON,
431 "arrangement", arrangement,
438 hildon_button_set_arrangement (HildonButton *button,
439 HildonButtonArrangement arrangement)
441 HildonButtonPrivate *priv;
443 priv = HILDON_BUTTON_GET_PRIVATE (button);
445 /* Pack everything */
446 if (arrangement == HILDON_BUTTON_ARRANGEMENT_VERTICAL) {
447 priv->label_box = gtk_vbox_new (FALSE, 0);
449 priv->label_box = gtk_hbox_new (FALSE, 0);
452 /* If we pack both labels with (TRUE, TRUE) or (FALSE, FALSE) they
453 * can be painted outside of the button in some situations, see
455 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->title), TRUE, TRUE, 0);
456 gtk_box_pack_start (GTK_BOX (priv->label_box), GTK_WIDGET (priv->value), FALSE, FALSE, 0);
458 hildon_button_construct_child (button);
462 * hildon_button_set_title:
463 * @button: a #HildonButton
464 * @title: a new title (main label) for the button, or %NULL
466 * Sets the title (main label) of @button to @title.
468 * This will clear any previously set title.
470 * If @title is set to %NULL, the title label will be hidden and the
471 * value label will be realigned.
474 hildon_button_set_title (HildonButton *button,
477 HildonButtonPrivate *priv;
479 g_return_if_fail (HILDON_IS_BUTTON (button));
481 priv = HILDON_BUTTON_GET_PRIVATE (button);
482 gtk_label_set_text (priv->title, title);
484 /* If the button has no title, hide the label so the value is
485 * properly aligned */
487 hildon_button_construct_child (button);
488 gtk_widget_show (GTK_WIDGET (priv->title));
490 gtk_widget_hide (GTK_WIDGET (priv->title));
493 g_object_notify (G_OBJECT (button), "title");
497 * hildon_button_set_value:
498 * @button: a #HildonButton
499 * @value: a new value (secondary label) for the button, or %NULL
501 * Sets the value (secondary label) of @button to @value.
503 * This will clear any previously set value.
505 * If @value is set to %NULL, the value label will be hidden and the
506 * title label will be realigned.
510 hildon_button_set_value (HildonButton *button,
513 HildonButtonPrivate *priv;
515 g_return_if_fail (HILDON_IS_BUTTON (button));
517 priv = HILDON_BUTTON_GET_PRIVATE (button);
518 gtk_label_set_text (priv->value, value);
520 /* If the button has no value, hide the label so the title is
521 * properly aligned */
523 hildon_button_construct_child (button);
524 gtk_widget_show (GTK_WIDGET (priv->value));
526 gtk_widget_hide (GTK_WIDGET (priv->value));
529 g_object_notify (G_OBJECT (button), "value");
533 * hildon_button_get_title:
534 * @button: a #HildonButton
536 * Gets the text from the main label (title) of @button, or %NULL if
539 * Returns: The text of the title label. This string is owned by the
540 * widget and must not be modified or freed.
543 hildon_button_get_title (HildonButton *button)
545 HildonButtonPrivate *priv;
547 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
549 priv = HILDON_BUTTON_GET_PRIVATE (button);
551 return gtk_label_get_text (priv->title);
555 * hildon_button_get_value:
556 * @button: a #HildonButton
558 * Gets the text from the secondary label (value) of @button, or %NULL
559 * if none has been set.
561 * Returns: The text of the value label. This string is owned by the
562 * widget and must not be modified or freed.
565 hildon_button_get_value (HildonButton *button)
567 HildonButtonPrivate *priv;
569 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
571 priv = HILDON_BUTTON_GET_PRIVATE (button);
573 return gtk_label_get_text (priv->value);
577 * hildon_button_set_text:
578 * @button: a #HildonButton
579 * @title: new text for the button title (main label)
580 * @value: new text for the button value (secondary label)
582 * Convenience function to change both labels of a #HildonButton
585 hildon_button_set_text (HildonButton *button,
589 hildon_button_set_title (button, title);
590 hildon_button_set_value (button, value);
594 * hildon_button_set_image:
595 * @button: a #HildonButton
596 * @image: a widget to set as the button image
598 * Sets the image of @button to the given widget. The previous image
599 * (if any) will be removed.
602 hildon_button_set_image (HildonButton *button,
605 HildonButtonPrivate *priv;
607 g_return_if_fail (HILDON_IS_BUTTON (button));
608 g_return_if_fail (!image || GTK_IS_WIDGET (image));
610 priv = HILDON_BUTTON_GET_PRIVATE (button);
612 /* Return if there's nothing to do */
613 if (image == priv->image)
616 if (priv->image && priv->image->parent)
617 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
621 hildon_button_construct_child (button);
625 * hildon_button_get_image:
626 * @button: a #HildonButton
628 * Gets the widget that is currenty set as the image of @button,
629 * previously set with hildon_button_set_image()
631 * Returns: a #GtkWidget or %NULL in case there is no image
634 hildon_button_get_image (HildonButton *button)
636 HildonButtonPrivate *priv;
638 g_return_val_if_fail (HILDON_IS_BUTTON (button), NULL);
640 priv = HILDON_BUTTON_GET_PRIVATE (button);
646 * hildon_button_set_image_position:
647 * @button: a #HildonButton
648 * @position: the position of the image (%GTK_POS_LEFT or %GTK_POS_RIGHT)
650 * Sets the position of the image inside @button. Only %GTK_POS_LEFT
651 * and %GTK_POS_RIGHT are currently supported.
654 hildon_button_set_image_position (HildonButton *button,
655 GtkPositionType position)
657 HildonButtonPrivate *priv;
659 g_return_if_fail (HILDON_IS_BUTTON (button));
660 g_return_if_fail (position == GTK_POS_LEFT || position == GTK_POS_RIGHT);
662 priv = HILDON_BUTTON_GET_PRIVATE (button);
664 /* Return if there's nothing to do */
665 if (priv->image_position == position)
668 priv->image_position = position;
670 hildon_button_construct_child (button);
674 * hildon_button_set_alignment:
675 * @button: a #HildonButton
676 * @xalign: the horizontal alignment of the contents, from 0 (left) to 1 (right).
677 * @yalign: the vertical alignment of the contents, from 0 (top) to 1 (bottom).
678 * @xscale: the amount that the child widget expands horizontally to fill up unused space, from 0 to 1
679 * @yscale: the amount that the child widget expands vertically to fill up unused space, from 0 to 1
681 * Sets the alignment of the contents of the widget. If you don't need
682 * to change @xscale or @yscale you can just use
683 * gtk_button_set_alignment() instead.
686 hildon_button_set_alignment (HildonButton *button,
692 HildonButtonPrivate *priv;
695 g_return_if_fail (HILDON_IS_BUTTON (button));
697 priv = HILDON_BUTTON_GET_PRIVATE (button);
699 child = gtk_bin_get_child (GTK_BIN (button));
701 if (GTK_IS_ALIGNMENT (child)) {
702 gtk_button_set_alignment (GTK_BUTTON (button), xalign, yalign);
703 g_object_set (child, "xscale", xscale, "yscale", yscale, NULL);
708 * hildon_button_set_title_alignment:
709 * @button: a #HildonButton
710 * @xalign: the horizontal alignment of the title label, from 0 (left) to 1 (right).
711 * @yalign: the vertical alignment of the title label, from 0 (top) to 1 (bottom).
713 * Sets the alignment of the title label. See also
714 * hildon_button_set_alignment() to set the alignment of the whole
715 * contents of the button.
718 hildon_button_set_title_alignment (HildonButton *button,
722 HildonButtonPrivate *priv;
724 g_return_if_fail (HILDON_IS_BUTTON (button));
726 priv = HILDON_BUTTON_GET_PRIVATE (button);
728 gtk_misc_set_alignment (GTK_MISC (priv->title), xalign, yalign);
732 * hildon_button_set_value_alignment:
733 * @button: a #HildonButton
734 * @xalign: the horizontal alignment of the value label, from 0 (left) to 1 (right).
735 * @yalign: the vertical alignment of the value label, from 0 (top) to 1 (bottom).
737 * Sets the alignment of the value label. See also
738 * hildon_button_set_alignment() to set the alignment of the whole
739 * contents of the button.
742 hildon_button_set_value_alignment (HildonButton *button,
746 HildonButtonPrivate *priv;
748 g_return_if_fail (HILDON_IS_BUTTON (button));
750 priv = HILDON_BUTTON_GET_PRIVATE (button);
752 gtk_misc_set_alignment (GTK_MISC (priv->value), xalign, yalign);
756 * hildon_button_set_image_alignment:
757 * @button: a #HildonButton
758 * @xalign: the horizontal alignment of the image, from 0 (left) to 1 (right).
759 * @yalign: the vertical alignment of the image, from 0 (top) to 1 (bottom).
761 * Sets the alignment of the image. See also
762 * hildon_button_set_alignment() to set the alignment of the whole
763 * contents of the button.
766 hildon_button_set_image_alignment (HildonButton *button,
770 HildonButtonPrivate *priv;
772 g_return_if_fail (HILDON_IS_BUTTON (button));
774 priv = HILDON_BUTTON_GET_PRIVATE (button);
776 /* Return if there's nothing to do */
777 if (priv->image_xalign == xalign && priv->image_yalign == yalign)
780 priv->image_xalign = xalign;
781 priv->image_yalign = yalign;
783 hildon_button_construct_child (button);
787 hildon_button_construct_child (HildonButton *button)
789 HildonButtonPrivate *priv = HILDON_BUTTON_GET_PRIVATE (button);
793 /* Don't do anything if the button is not constructed yet */
794 if (priv->label_box == NULL)
797 /* Save a ref to the image if necessary */
798 if (priv->image && priv->image->parent != NULL) {
799 g_object_ref (priv->image);
800 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
803 /* Save a ref to the label box if necessary */
804 if (priv->label_box->parent != NULL) {
805 g_object_ref (priv->label_box);
806 gtk_container_remove (GTK_CONTAINER (priv->label_box->parent), priv->label_box);
809 /* Remove the child from the container and add priv->alignment */
810 child = gtk_bin_get_child (GTK_BIN (button));
811 if (child != NULL && child != priv->alignment) {
812 gtk_container_remove (GTK_CONTAINER (button), child);
817 gtk_container_add (GTK_CONTAINER (button), GTK_WIDGET (priv->alignment));
820 /* Create a new hbox */
822 gtk_container_remove (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
824 gtk_widget_style_get (GTK_WIDGET (button), "image-spacing", &image_spacing, NULL);
825 priv->hbox = GTK_BOX (gtk_hbox_new (FALSE, image_spacing));
826 gtk_container_add (GTK_CONTAINER (priv->alignment), GTK_WIDGET (priv->hbox));
828 /* Pack the image and the alignment in the new hbox */
829 if (priv->image && priv->image_position == GTK_POS_LEFT)
830 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
832 gtk_box_pack_start (priv->hbox, priv->label_box, TRUE, TRUE, 0);
834 if (priv->image && priv->image_position == GTK_POS_RIGHT)
835 gtk_box_pack_start (priv->hbox, priv->image, FALSE, FALSE, 0);
837 /* Set image alignment */
839 gtk_misc_set_alignment (GTK_MISC (priv->image), priv->image_xalign, priv->image_yalign);
841 /* Show everything */
842 gtk_widget_show_all (GTK_WIDGET (priv->alignment));