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 * This is a standard GtkButton which contains a check box and a
22 * label. Functions are provided to get and set the value of the check
23 * box. Note that this button does NOT support an image, so don't use
24 * gtk_button_set_image()
29 * button_clicked (GtkButton *button, gpointer user_data)
33 * active = hildon_check_button_get_active (button);
35 * g_debug ("Button is active");
37 * g_debug ("Button is not active");
41 * create_button (void)
45 * button = hildon_check_button_new (HILDON_SIZE_AUTO);
46 * gtk_button_set_label (GTK_BUTTON (button), "Click me");
48 * g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), NULL);
57 #include "hildon-check-button.h"
60 check_button_clicked (GtkButton *button,
61 GtkCellRendererToggle *renderer)
63 gboolean current = gtk_cell_renderer_toggle_get_active (renderer);
64 gtk_cell_renderer_toggle_set_active (renderer, !current);
68 * hildon_check_button_set_label:
69 * @button: A #GtkButton created with hildon_check_button_new()
70 * @label: New text for the label.
72 * Sets the text of the button label to @label.
74 * Deprecated: Use gtk_button_set_label() instead.
77 hildon_check_button_set_label (GtkButton *button,
80 gtk_button_set_label (button, label);
84 * hildon_check_button_get_label:
85 * @button: A #GtkButton created with hildon_check_button_new()
87 * Gets the text of the label inside the button.
89 * Return value: the text of the label. This string is owned by the
90 * button and must not be modified or freed.
92 * Deprecated: Use gtk_button_get_label() instead.
95 hildon_check_button_get_label (GtkButton *button)
97 return gtk_button_get_label (button);
101 * hildon_check_button_set_active:
102 * @button: A #GtkButton created with hildon_check_button_new()
103 * @is_active: new state for the check box
105 * Sets the state of the check box.
108 hildon_check_button_set_active (GtkButton *button,
111 GtkCellRendererToggle *toggle_renderer;
113 g_return_if_fail (GTK_IS_BUTTON (button));
114 toggle_renderer = GTK_CELL_RENDERER_TOGGLE (g_object_get_data (G_OBJECT (button), "toggle-renderer"));
115 g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle_renderer));
117 gtk_cell_renderer_toggle_set_active (toggle_renderer, is_active);
121 * hildon_check_button_get_active:
122 * @button: A #GtkButton created with hildon_check_button_new()
124 * Gets the state of the check box.
126 * Return value: %TRUE if the check box is active, %FALSE otherwise.
129 hildon_check_button_get_active (GtkButton *button)
131 GtkCellRendererToggle *toggle_renderer;
133 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
134 toggle_renderer = GTK_CELL_RENDERER_TOGGLE (g_object_get_data (G_OBJECT (button), "toggle-renderer"));
135 g_return_val_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (toggle_renderer), FALSE);
137 return gtk_cell_renderer_toggle_get_active (toggle_renderer);
141 * hildon_check_button_new:
142 * @size: Flags indicating the size of the new button
144 * This function creates a #GtkButton containing a label and a check
147 * This button has specific functions to get and set the value of the
150 * Return value: A newly created #GtkButton widget.
153 hildon_check_button_new (HildonSizeType size)
155 GtkWidget *button = gtk_button_new ();
156 GtkWidget *cell_view = gtk_cell_view_new ();
157 GtkCellRenderer *toggle_renderer = gtk_cell_renderer_toggle_new ();
159 /* Set the size of the button */
160 hildon_gtk_widget_set_theme_size (button, size);
162 /* Toggle the check box when the button is clicked */
163 g_signal_connect (button, "clicked", G_CALLBACK (check_button_clicked), toggle_renderer);
165 /* Make sure that the check box is always shown, no matter the value of gtk-button-images */
166 g_signal_connect (cell_view, "notify::visible", G_CALLBACK (gtk_widget_show), NULL);
168 /* Store the renderer for later use */
169 g_object_set_data (G_OBJECT (button), "toggle-renderer", toggle_renderer);
171 /* Pack everything */
172 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cell_view), toggle_renderer, FALSE);
173 gtk_button_set_image (GTK_BUTTON (button), cell_view);