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 * #HildonCheckButton derives from #GtkToggleButton so its state can
30 * be set with gtk_toggle_button_set_active(), and retrieved using
31 * gtk_toggle_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 (GtkToggleButton *button, gpointer user_data)
49 * active = gtk_toggle_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 #undef HILDON_DISABLE_DEPRECATED
74 #include "hildon-check-button.h"
80 G_DEFINE_TYPE (HildonCheckButton, hildon_check_button, GTK_TYPE_TOGGLE_BUTTON);
82 #define HILDON_CHECK_BUTTON_GET_PRIVATE(obj) \
83 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
84 HILDON_TYPE_CHECK_BUTTON, HildonCheckButtonPrivate));
86 struct _HildonCheckButtonPrivate
88 GtkCellRendererToggle *toggle_renderer;
92 * hildon_check_button_toggled:
93 * @button: A #HildonCheckButton
95 * Emits the #HildonCheckButton::toggled signal on the #HildonCheckButton.
97 * Deprecated: use gtk_toggle_button_toggled()
102 hildon_check_button_toggled (HildonCheckButton *button)
104 g_return_if_fail (HILDON_IS_CHECK_BUTTON (button));
106 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (button));
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.
116 * Deprecated: use gtk_toggle_button_set_active()
121 hildon_check_button_set_active (HildonCheckButton *button,
124 g_return_if_fail (HILDON_IS_CHECK_BUTTON (button));
126 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), is_active);
130 * hildon_check_button_get_active:
131 * @button: A #HildonCheckButton
133 * Gets the current state of @button.
135 * Return value: %TRUE if @button is active, %FALSE otherwise.
137 * Deprecated: use gtk_toggle_button_get_active()
142 hildon_check_button_get_active (HildonCheckButton *button)
144 g_return_val_if_fail (HILDON_IS_CHECK_BUTTON (button), FALSE);
146 return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
150 * hildon_check_button_new:
151 * @size: Flags indicating the size of the new button
153 * Creates a new #HildonCheckButton.
155 * Return value: A newly created #HildonCheckButton
160 hildon_check_button_new (HildonSizeType size)
162 return g_object_new (HILDON_TYPE_CHECK_BUTTON, "xalign", 0.0, "size", size, NULL);
166 hildon_check_button_clicked (GtkButton *button)
168 HildonCheckButton *checkbutton = HILDON_CHECK_BUTTON (button);
169 GtkToggleButton *togglebutton = GTK_TOGGLE_BUTTON (button);
171 togglebutton->active = !togglebutton->active;
172 gtk_cell_renderer_toggle_set_active (checkbutton->priv->toggle_renderer,
173 togglebutton->active);
175 gtk_toggle_button_toggled (togglebutton);
177 gtk_widget_queue_draw (GTK_WIDGET (button));
179 g_object_notify (G_OBJECT (button), "active");
183 hildon_check_button_apply_style (GtkWidget *widget)
186 HildonCheckButtonPrivate *priv = HILDON_CHECK_BUTTON (widget)->priv;
188 gtk_widget_style_get (widget, "checkbox-size", &checkbox_size, NULL);
190 g_object_set (priv->toggle_renderer, "indicator-size", checkbox_size, NULL);
194 hildon_check_button_style_set (GtkWidget *widget,
195 GtkStyle *previous_style)
197 if (GTK_WIDGET_CLASS (hildon_check_button_parent_class)->style_set)
198 GTK_WIDGET_CLASS (hildon_check_button_parent_class)->style_set (widget, previous_style);
200 hildon_check_button_apply_style (widget);
204 set_property (GObject *object,
212 hildon_gtk_widget_set_theme_size (GTK_WIDGET (object), g_value_get_flags (value));
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 hildon_check_button_class_init (HildonCheckButtonClass *klass)
223 GObjectClass *gobject_class = (GObjectClass*) klass;
224 GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
225 GtkButtonClass *button_class = (GtkButtonClass*) klass;
226 GtkButtonClass *gtk_button_class = g_type_class_peek_parent (hildon_check_button_parent_class);
228 gobject_class->set_property = set_property;
229 widget_class->style_set = hildon_check_button_style_set;
230 button_class->clicked = hildon_check_button_clicked;
231 button_class->pressed = gtk_button_class->pressed;
232 button_class->released = gtk_button_class->released;
233 button_class->enter = gtk_button_class->enter;
234 button_class->leave = gtk_button_class->leave;
236 gtk_widget_class_install_style_property (
240 "Size of the check box",
241 "Size of the check box",
245 g_object_class_install_property (
251 "Size request for the button",
252 HILDON_TYPE_SIZE_TYPE,
256 g_type_class_add_private (klass, sizeof (HildonCheckButtonPrivate));
260 hildon_check_button_init (HildonCheckButton *button)
262 HildonCheckButtonPrivate *priv = HILDON_CHECK_BUTTON_GET_PRIVATE (button);
263 GtkWidget *cell_view = gtk_cell_view_new ();
265 /* Store private part */
268 /* Make sure that the check box is always shown, no matter the value of gtk-button-images */
269 g_signal_connect (cell_view, "notify::visible", G_CALLBACK (gtk_widget_show), NULL);
271 /* Create toggle renderer and pack it into the cell view */
272 priv->toggle_renderer = GTK_CELL_RENDERER_TOGGLE (gtk_cell_renderer_toggle_new ());
273 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view),
274 GTK_CELL_RENDERER (priv->toggle_renderer), FALSE);
276 /* Add cell view to the image */
277 gtk_button_set_image (GTK_BUTTON (button), cell_view);
279 gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);
281 hildon_check_button_apply_style (GTK_WIDGET (button));