2008-08-14 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-check-button.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
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.
9  *
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.
14  *
15  */
16
17 /**
18  * SECTION:hildon-check-button
19  * @short_description: Button with a check box inside
20  *
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()
25  *
26  * <example>
27  * <programlisting>
28  * void
29  * button_clicked (GtkButton *button, gpointer user_data)
30  * {
31  *     gboolean active;
32  * <!-- -->
33  *     active = hildon_check_button_get_active (button);
34  *     if (active)
35  *        g_debug ("Button is active");
36  *     else
37  *        g_debug ("Button is not active");
38  * }
39  * <!-- -->
40  * GtkWidget *
41  * create_button (void)
42  * {
43  *     GtkWidget *button;
44  * <!-- -->
45  *     button = hildon_check_button_new (HILDON_SIZE_AUTO);
46  *     gtk_button_set_label (GTK_BUTTON (button), "Click me");
47  * <!-- -->
48  *     g_signal_connect (button, "clicked", G_CALLBACK (button_clicked), NULL);
49  * <!-- -->
50  *     return button;
51  * }
52  * </programlisting>
53  * </example>
54
55  */
56
57 #include                                        "hildon-check-button.h"
58
59 static void
60 check_button_clicked                            (GtkButton             *button,
61                                                  GtkCellRendererToggle *renderer)
62 {
63     gboolean current = gtk_cell_renderer_toggle_get_active (renderer);
64     gtk_cell_renderer_toggle_set_active (renderer, !current);
65 }
66
67 /**
68  * hildon_check_button_set_label:
69  * @button: A #GtkButton created with hildon_check_button_new()
70  * @label: New text for the label.
71  *
72  * Sets the text of the button label to @label.
73  *
74  * Deprecated: Use gtk_button_set_label() instead.
75  **/
76 void
77 hildon_check_button_set_label                   (GtkButton   *button,
78                                                  const gchar *label)
79 {
80     gtk_button_set_label (button, label);
81 }
82
83 /**
84  * hildon_check_button_get_label:
85  * @button: A #GtkButton created with hildon_check_button_new()
86  *
87  * Gets the text of the label inside the button.
88  *
89  * Return value: the text of the label. This string is owned by the
90  * button and must not be modified or freed.
91  *
92  * Deprecated: Use gtk_button_get_label() instead.
93  **/
94 const gchar *
95 hildon_check_button_get_label                   (GtkButton *button)
96 {
97     return gtk_button_get_label (button);
98 }
99
100 /**
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
104  *
105  * Sets the state of the check box.
106  **/
107 void
108 hildon_check_button_set_active                  (GtkButton *button,
109                                                  gboolean   is_active)
110 {
111     GtkCellRendererToggle *toggle_renderer;
112
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));
116
117     gtk_cell_renderer_toggle_set_active (toggle_renderer, is_active);
118 }
119
120 /**
121  * hildon_check_button_get_active:
122  * @button: A #GtkButton created with hildon_check_button_new()
123  *
124  * Gets the state of the check box.
125  *
126  * Return value: %TRUE if the check box is active, %FALSE otherwise.
127  **/
128 gboolean
129 hildon_check_button_get_active                  (GtkButton *button)
130 {
131     GtkCellRendererToggle *toggle_renderer;
132
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);
136
137     return gtk_cell_renderer_toggle_get_active (toggle_renderer);
138 }
139
140 /**
141  * hildon_check_button_new:
142  * @size: Flags indicating the size of the new button
143  *
144  * This function creates a #GtkButton containing a label and a check
145  * box.
146  *
147  * This button has specific functions to get and set the value of the
148  * check box.
149  *
150  * Return value: A newly created #GtkButton widget.
151  **/
152 GtkWidget *
153 hildon_check_button_new                         (HildonSizeType size)
154 {
155     GtkWidget *button = gtk_button_new ();
156     GtkWidget *cell_view = gtk_cell_view_new ();
157     GtkCellRenderer *toggle_renderer = gtk_cell_renderer_toggle_new ();
158
159     /* Set the size of the button */
160     hildon_gtk_widget_set_theme_size (button, size);
161
162     /* Toggle the check box when the button is clicked */
163     g_signal_connect (button, "clicked", G_CALLBACK (check_button_clicked), toggle_renderer);
164
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);
167
168     /* Store the renderer for later use */
169     g_object_set_data (G_OBJECT (button), "toggle-renderer", toggle_renderer);
170
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);
174
175     return button;
176 }