2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser Public License as published by
8 * the Free Software Foundation; version 2 of the license.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser Public License for more details.
18 * SECTION:hildon-check-button
19 * @short_description: Button with a check box inside
21 * #HildonCheckButton is a button containing a label and a check box
22 * which will remain 'pressed-in' when clicked. Clicking again will
23 * make the check box toggle its state.
25 * #HildonCheckButton is similar to the #GtkCheckButton widget, but
26 * with a different appearance that combines a standard button and a
29 * The state of a #HildonCheckButton can be set using
30 * hildon_check_button_set_active(), and retrieved using
31 * hildon_check_button_get_active(). The label can be set using
32 * gtk_button_set_label() and retrieved using gtk_button_get_label().
36 * #HildonCheckButton does NOT support an image, so don't use
37 * gtk_button_set_image().
42 * <title>Using a Hildon check button</title>
45 * button_toggled (HildonCheckButton *button, gpointer user_data)
49 * active = hildon_check_button_get_active (button);
51 * g_debug ("Button is active");
53 * g_debug ("Button is not active");
57 * create_button (void)
61 * button = hildon_check_button_new (HILDON_SIZE_AUTO);
62 * gtk_button_set_label (GTK_BUTTON (button), "Click me");
64 * g_signal_connect (button, "toggled", G_CALLBACK (button_toggled), NULL);
72 #include "hildon-check-button.h"
83 static guint signals[LAST_SIGNAL] = { 0 };
85 G_DEFINE_TYPE (HildonCheckButton, hildon_check_button, GTK_TYPE_BUTTON);
87 #define HILDON_CHECK_BUTTON_GET_PRIVATE(obj) \
88 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
89 HILDON_TYPE_CHECK_BUTTON, HildonCheckButtonPrivate));
91 struct _HildonCheckButtonPrivate
93 GtkCellRendererToggle *toggle_renderer;
97 * hildon_check_button_toggled:
98 * @button: A #HildonCheckButton
100 * Emits the #HildonCheckButton::toggled signal on the #HildonCheckButton.
101 * There is no good reason for an application ever to call this function.
106 hildon_check_button_toggled (HildonCheckButton *button)
108 g_return_if_fail (HILDON_IS_CHECK_BUTTON (button));
110 g_signal_emit (button, signals[TOGGLED], 0);
114 * hildon_check_button_set_active:
115 * @button: A #HildonCheckButton
116 * @is_active: new state for the button
118 * Sets the status of a #HildonCheckButton. Set to %TRUE if you want
119 * @button to be 'pressed-in', and %FALSE to raise it. This action
120 * causes the #HildonCheckButton::toggled signal to be emitted.
125 hildon_check_button_set_active (HildonCheckButton *button,
128 gboolean prev_is_active;
130 g_return_if_fail (HILDON_IS_CHECK_BUTTON (button));
132 prev_is_active = hildon_check_button_get_active (button);
134 if (prev_is_active != is_active) {
135 gtk_button_clicked (GTK_BUTTON (button));
136 gtk_widget_queue_draw (GTK_WIDGET (button));
141 * hildon_check_button_get_active:
142 * @button: A #HildonCheckButton
144 * Gets the current state of @button.
146 * Return value: %TRUE if @button is active, %FALSE otherwise.
151 hildon_check_button_get_active (HildonCheckButton *button)
153 g_return_val_if_fail (HILDON_IS_CHECK_BUTTON (button), FALSE);
155 return gtk_cell_renderer_toggle_get_active (button->priv->toggle_renderer);
159 * hildon_check_button_new:
160 * @size: Flags indicating the size of the new button
162 * Creates a new #HildonCheckButton.
164 * Return value: A newly created #HildonCheckButton
169 hildon_check_button_new (HildonSizeType size)
171 return g_object_new (HILDON_TYPE_CHECK_BUTTON, "xalign", 0.0, "size", size, NULL);
175 hildon_check_button_clicked (GtkButton *button)
177 HildonCheckButton *checkbutton = HILDON_CHECK_BUTTON (button);
178 gboolean current = hildon_check_button_get_active (checkbutton);
180 gtk_cell_renderer_toggle_set_active (checkbutton->priv->toggle_renderer, !current);
182 hildon_check_button_toggled (checkbutton);
186 hildon_check_button_apply_style (GtkWidget *widget)
189 HildonCheckButtonPrivate *priv = HILDON_CHECK_BUTTON (widget)->priv;
191 gtk_widget_style_get (widget, "checkbox-size", &checkbox_size, NULL);
193 g_object_set (priv->toggle_renderer, "indicator-size", checkbox_size, NULL);
197 hildon_check_button_style_set (GtkWidget *widget,
198 GtkStyle *previous_style)
200 if (GTK_WIDGET_CLASS (hildon_check_button_parent_class)->style_set)
201 GTK_WIDGET_CLASS (hildon_check_button_parent_class)->style_set (widget, previous_style);
203 hildon_check_button_apply_style (widget);
207 set_property (GObject *object,
215 hildon_gtk_widget_set_theme_size (GTK_WIDGET (object), g_value_get_flags (value));
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 hildon_check_button_class_init (HildonCheckButtonClass *klass)
226 GObjectClass *gobject_class = (GObjectClass*) klass;
227 GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
228 GtkButtonClass *button_class = (GtkButtonClass*) klass;
230 gobject_class->set_property = set_property;
231 widget_class->style_set = hildon_check_button_style_set;
232 button_class->clicked = hildon_check_button_clicked;
234 klass->toggled = NULL;
237 * HildonCheckButton::toggled
239 * Emitted when the #HildonCheckButton's state is changed.
244 g_signal_new ("toggled",
245 G_OBJECT_CLASS_TYPE (gobject_class),
247 G_STRUCT_OFFSET (HildonCheckButtonClass, toggled),
249 g_cclosure_marshal_VOID__VOID,
252 gtk_widget_class_install_style_property (
256 "Size of the check box",
257 "Size of the check box",
261 g_object_class_install_property (
267 "Size request for the button",
268 HILDON_TYPE_SIZE_TYPE,
272 g_type_class_add_private (klass, sizeof (HildonCheckButtonPrivate));
276 hildon_check_button_init (HildonCheckButton *button)
278 HildonCheckButtonPrivate *priv = HILDON_CHECK_BUTTON_GET_PRIVATE (button);
279 GtkWidget *cell_view = gtk_cell_view_new ();
281 /* Store private part */
284 /* Make sure that the check box is always shown, no matter the value of gtk-button-images */
285 g_signal_connect (cell_view, "notify::visible", G_CALLBACK (gtk_widget_show), NULL);
287 /* Create toggle renderer and pack it into the cell view */
288 priv->toggle_renderer = GTK_CELL_RENDERER_TOGGLE (gtk_cell_renderer_toggle_new ());
289 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
290 GTK_CELL_RENDERER (priv->toggle_renderer), FALSE);
292 /* Add cell view to the image */
293 gtk_button_set_image (GTK_BUTTON (button), cell_view);
295 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
297 hildon_check_button_apply_style (GTK_WIDGET (button));