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 * The state of a #HildonCheckButton can be set using
26 * hildon_check_button_set_active(), and retrieved using
27 * hildon_check_button_get_active(). The label can be set using
28 * gtk_button_set_label() and retrieved using gtk_button_get_label().
32 * #HildonCheckButton does NOT support an image, so don't use
33 * gtk_button_set_image().
38 * <title>Using a Hildon check button</title>
41 * button_toggled (HildonCheckButton *button, gpointer user_data)
45 * active = hildon_check_button_get_active (button);
47 * g_debug ("Button is active");
49 * g_debug ("Button is not active");
53 * create_button (void)
57 * button = hildon_check_button_new (HILDON_SIZE_AUTO);
58 * gtk_button_set_label (GTK_BUTTON (button), "Click me");
60 * g_signal_connect (button, "toggled", G_CALLBACK (button_toggled), NULL);
68 #include "hildon-check-button.h"
79 static guint signals[LAST_SIGNAL] = { 0 };
81 G_DEFINE_TYPE (HildonCheckButton, hildon_check_button, GTK_TYPE_BUTTON);
83 #define HILDON_CHECK_BUTTON_GET_PRIVATE(obj) \
84 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
85 HILDON_TYPE_CHECK_BUTTON, HildonCheckButtonPrivate));
87 struct _HildonCheckButtonPrivate
89 GtkCellRendererToggle *toggle_renderer;
93 * hildon_check_button_toggled:
94 * @button: A #HildonCheckButton
96 * Emits the #HildonCheckButton::toggled signal on the #HildonCheckButton.
97 * There is no good reason for an application ever to call this function.
102 hildon_check_button_toggled (HildonCheckButton *button)
104 g_return_if_fail (HILDON_IS_CHECK_BUTTON (button));
106 g_signal_emit (button, signals[TOGGLED], 0);
110 * hildon_check_button_set_active:
111 * @button: A #HildonCheckButton
112 * @is_active: new state for the button
114 * Sets the status of a #HildonCheckButton. Set to %TRUE if you want
115 * @button to be 'pressed-in', and %FALSE to raise it. This action
116 * causes the #HildonCheckButton::toggled signal to be emitted.
121 hildon_check_button_set_active (HildonCheckButton *button,
124 gboolean prev_is_active;
126 g_return_if_fail (HILDON_IS_CHECK_BUTTON (button));
128 prev_is_active = hildon_check_button_get_active (button);
130 if (prev_is_active != is_active) {
131 gtk_button_clicked (GTK_BUTTON (button));
132 gtk_widget_queue_draw (GTK_WIDGET (button));
137 * hildon_check_button_get_active:
138 * @button: A #HildonCheckButton
140 * Gets the current state of @button.
142 * Return value: %TRUE if @button is active, %FALSE otherwise.
147 hildon_check_button_get_active (HildonCheckButton *button)
149 g_return_val_if_fail (HILDON_IS_CHECK_BUTTON (button), FALSE);
151 return gtk_cell_renderer_toggle_get_active (button->priv->toggle_renderer);
155 * hildon_check_button_new:
156 * @size: Flags indicating the size of the new button
158 * Creates a new #HildonCheckButton.
160 * Return value: A newly created #HildonCheckButton
165 hildon_check_button_new (HildonSizeType size)
167 return g_object_new (HILDON_TYPE_CHECK_BUTTON, "xalign", 0.0, "size", size, NULL);
171 hildon_check_button_clicked (GtkButton *button)
173 HildonCheckButton *checkbutton = HILDON_CHECK_BUTTON (button);
174 gboolean current = hildon_check_button_get_active (checkbutton);
176 gtk_cell_renderer_toggle_set_active (checkbutton->priv->toggle_renderer, !current);
178 hildon_check_button_toggled (checkbutton);
182 hildon_check_button_apply_style (GtkWidget *widget)
185 HildonCheckButtonPrivate *priv = HILDON_CHECK_BUTTON (widget)->priv;
187 gtk_widget_style_get (widget, "checkbox-size", &checkbox_size, NULL);
189 g_object_set (priv->toggle_renderer, "indicator-size", checkbox_size, NULL);
193 hildon_check_button_style_set (GtkWidget *widget,
194 GtkStyle *previous_style)
196 if (GTK_WIDGET_CLASS (hildon_check_button_parent_class)->style_set)
197 GTK_WIDGET_CLASS (hildon_check_button_parent_class)->style_set (widget, previous_style);
199 hildon_check_button_apply_style (widget);
203 set_property (GObject *object,
211 hildon_gtk_widget_set_theme_size (GTK_WIDGET (object), g_value_get_flags (value));
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 hildon_check_button_class_init (HildonCheckButtonClass *klass)
222 GObjectClass *gobject_class = (GObjectClass*) klass;
223 GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
224 GtkButtonClass *button_class = (GtkButtonClass*) klass;
226 gobject_class->set_property = set_property;
227 widget_class->style_set = hildon_check_button_style_set;
228 button_class->clicked = hildon_check_button_clicked;
230 klass->toggled = NULL;
233 * HildonCheckButton::toggled
235 * Emitted when the #HildonCheckButton's state is changed.
240 g_signal_new ("toggled",
241 G_OBJECT_CLASS_TYPE (gobject_class),
243 G_STRUCT_OFFSET (HildonCheckButtonClass, toggled),
245 g_cclosure_marshal_VOID__VOID,
248 gtk_widget_class_install_style_property (
252 "Size of the check box",
253 "Size of the check box",
257 g_object_class_install_property (
263 "Size request for the button",
264 HILDON_TYPE_SIZE_TYPE,
268 g_type_class_add_private (klass, sizeof (HildonCheckButtonPrivate));
272 hildon_check_button_init (HildonCheckButton *button)
274 HildonCheckButtonPrivate *priv = HILDON_CHECK_BUTTON_GET_PRIVATE (button);
275 GtkWidget *cell_view = gtk_cell_view_new ();
277 /* Store private part */
280 /* Make sure that the check box is always shown, no matter the value of gtk-button-images */
281 g_signal_connect (cell_view, "notify::visible", G_CALLBACK (gtk_widget_show), NULL);
283 /* Create toggle renderer and pack it into the cell view */
284 priv->toggle_renderer = GTK_CELL_RENDERER_TOGGLE (gtk_cell_renderer_toggle_new ());
285 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
286 GTK_CELL_RENDERER (priv->toggle_renderer), FALSE);
288 /* Add cell view to the image */
289 gtk_button_set_image (GTK_BUTTON (button), cell_view);
291 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
293 hildon_check_button_apply_style (GTK_WIDGET (button));