1 /* gtkradiotoolbutton.c
2 *
3 * Copyright (C) 2002 Anders Carlsson <andersca@gnome.og>
4 * Copyright (C) 2002 James Henstridge <james@daa.com.au>
5 * Copyright (C) 2003 Soeren Sandmann <sandmann@daimi.au.dk>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22 #include "gtkradiotoolbutton.h"
23 #include "gtkradiobutton.h"
24 #include "gtkintl.h"
25 #include "gtkprivate.h"
26
27
28 /**
29 * SECTION:gtkradiotoolbutton
30 * @Short_description: A toolbar item that contains a radio button
31 * @Title: GtkRadioToolButton
32 * @See_also: #GtkToolbar, #GtkToolButton
33 *
34 * A #GtkRadioToolButton is a #GtkToolItem that contains a radio button,
35 * that is, a button that is part of a group of toggle buttons where only
36 * one button can be active at a time.
37 *
38 * Use gtk_radio_tool_button_new() to create a new GtkRadioToolButton. Use
39 * gtk_radio_tool_button_new_from_widget() to create a new GtkRadioToolButton
40 * that is part of the same group as an existing GtkRadioToolButton.
41 *
42 * # CSS nodes
43 *
44 * GtkRadioToolButton has a single CSS node with name toolbutton.
45 */
46
47
48 enum {
49 PROP_0,
50 PROP_GROUP
51 };
52
53 static void gtk_radio_tool_button_set_property (GObject *object,
54 guint prop_id,
55 const GValue *value,
56 GParamSpec *pspec);
57
G_DEFINE_TYPE(GtkRadioToolButton,gtk_radio_tool_button,GTK_TYPE_TOGGLE_TOOL_BUTTON)58 G_DEFINE_TYPE (GtkRadioToolButton, gtk_radio_tool_button, GTK_TYPE_TOGGLE_TOOL_BUTTON)
59
60 static void
61 gtk_radio_tool_button_class_init (GtkRadioToolButtonClass *klass)
62 {
63 GObjectClass *object_class;
64 GtkToolButtonClass *toolbutton_class;
65
66 object_class = (GObjectClass *)klass;
67 toolbutton_class = (GtkToolButtonClass *)klass;
68
69 object_class->set_property = gtk_radio_tool_button_set_property;
70
71 toolbutton_class->button_type = GTK_TYPE_RADIO_BUTTON;
72
73 /**
74 * GtkRadioToolButton:group:
75 *
76 * Sets a new group for a radio tool button.
77 *
78 * Since: 2.4
79 */
80 g_object_class_install_property (object_class,
81 PROP_GROUP,
82 g_param_spec_object ("group",
83 P_("Group"),
84 P_("The radio tool button whose group this button belongs to."),
85 GTK_TYPE_RADIO_TOOL_BUTTON,
86 GTK_PARAM_WRITABLE));
87
88 }
89
90 static void
gtk_radio_tool_button_init(GtkRadioToolButton * button)91 gtk_radio_tool_button_init (GtkRadioToolButton *button)
92 {
93 GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
94 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)), FALSE);
95 }
96
97 static void
gtk_radio_tool_button_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)98 gtk_radio_tool_button_set_property (GObject *object,
99 guint prop_id,
100 const GValue *value,
101 GParamSpec *pspec)
102 {
103 GtkRadioToolButton *button;
104
105 button = GTK_RADIO_TOOL_BUTTON (object);
106
107 switch (prop_id)
108 {
109 case PROP_GROUP:
110 {
111 GtkRadioToolButton *arg;
112 GSList *slist = NULL;
113 if (G_VALUE_HOLDS_OBJECT (value))
114 {
115 arg = GTK_RADIO_TOOL_BUTTON (g_value_get_object (value));
116 if (arg)
117 slist = gtk_radio_tool_button_get_group (arg);
118 gtk_radio_tool_button_set_group (button, slist);
119 }
120 }
121 break;
122 default:
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 break;
125 }
126 }
127
128 /**
129 * gtk_radio_tool_button_new:
130 * @group: (allow-none) (element-type GtkRadioButton): An
131 * existing radio button group, or %NULL if you are creating a new group
132 *
133 * Creates a new #GtkRadioToolButton, adding it to @group.
134 *
135 * Returns: The new #GtkRadioToolButton
136 *
137 * Since: 2.4
138 **/
139 GtkToolItem *
gtk_radio_tool_button_new(GSList * group)140 gtk_radio_tool_button_new (GSList *group)
141 {
142 GtkRadioToolButton *button;
143
144 button = g_object_new (GTK_TYPE_RADIO_TOOL_BUTTON,
145 NULL);
146
147 gtk_radio_tool_button_set_group (button, group);
148
149 return GTK_TOOL_ITEM (button);
150 }
151
152 /**
153 * gtk_radio_tool_button_new_from_stock:
154 * @group: (allow-none) (element-type GtkRadioButton): an existing radio button
155 * group, or %NULL if you are creating a new group
156 * @stock_id: the name of a stock item
157 *
158 * Creates a new #GtkRadioToolButton, adding it to @group.
159 * The new #GtkRadioToolButton will contain an icon and label from the
160 * stock item indicated by @stock_id.
161 *
162 * Returns: The new #GtkRadioToolButton
163 *
164 * Since: 2.4
165 *
166 * Deprecated: 3.10: Use gtk_radio_tool_button_new() instead.
167 **/
168 GtkToolItem *
gtk_radio_tool_button_new_from_stock(GSList * group,const gchar * stock_id)169 gtk_radio_tool_button_new_from_stock (GSList *group,
170 const gchar *stock_id)
171 {
172 GtkRadioToolButton *button;
173
174 g_return_val_if_fail (stock_id != NULL, NULL);
175
176 button = g_object_new (GTK_TYPE_RADIO_TOOL_BUTTON,
177 "stock-id", stock_id,
178 NULL);
179
180
181 gtk_radio_tool_button_set_group (button, group);
182
183 return GTK_TOOL_ITEM (button);
184 }
185
186 /**
187 * gtk_radio_tool_button_new_from_widget: (constructor)
188 * @group: (allow-none): An existing #GtkRadioToolButton, or %NULL
189 *
190 * Creates a new #GtkRadioToolButton adding it to the same group as @gruup
191 *
192 * Returns: (transfer none): The new #GtkRadioToolButton
193 *
194 * Since: 2.4
195 **/
196 GtkToolItem *
gtk_radio_tool_button_new_from_widget(GtkRadioToolButton * group)197 gtk_radio_tool_button_new_from_widget (GtkRadioToolButton *group)
198 {
199 GSList *list = NULL;
200
201 g_return_val_if_fail (group == NULL || GTK_IS_RADIO_TOOL_BUTTON (group), NULL);
202
203 if (group != NULL)
204 list = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (group));
205
206 return gtk_radio_tool_button_new (list);
207 }
208
209 /**
210 * gtk_radio_tool_button_new_with_stock_from_widget: (constructor)
211 * @group: (allow-none): An existing #GtkRadioToolButton.
212 * @stock_id: the name of a stock item
213 *
214 * Creates a new #GtkRadioToolButton adding it to the same group as @group.
215 * The new #GtkRadioToolButton will contain an icon and label from the
216 * stock item indicated by @stock_id.
217 *
218 * Returns: (transfer none): A new #GtkRadioToolButton
219 *
220 * Since: 2.4
221 *
222 * Deprecated: 3.10: gtk_radio_tool_button_new_from_widget
223 **/
224 GtkToolItem *
gtk_radio_tool_button_new_with_stock_from_widget(GtkRadioToolButton * group,const gchar * stock_id)225 gtk_radio_tool_button_new_with_stock_from_widget (GtkRadioToolButton *group,
226 const gchar *stock_id)
227 {
228 GSList *list = NULL;
229 GtkToolItem *item;
230
231 g_return_val_if_fail (group == NULL || GTK_IS_RADIO_TOOL_BUTTON (group), NULL);
232
233 if (group != NULL)
234 list = gtk_radio_tool_button_get_group (group);
235
236 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
237 item = gtk_radio_tool_button_new_from_stock (list, stock_id);
238 G_GNUC_END_IGNORE_DEPRECATIONS;
239
240 return item;
241 }
242
243 static GtkRadioButton *
get_radio_button(GtkRadioToolButton * button)244 get_radio_button (GtkRadioToolButton *button)
245 {
246 return GTK_RADIO_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button)));
247 }
248
249 /**
250 * gtk_radio_tool_button_get_group:
251 * @button: a #GtkRadioToolButton
252 *
253 * Returns the radio button group @button belongs to.
254 *
255 * Returns: (transfer none) (element-type GtkRadioButton): The group @button belongs to.
256 *
257 * Since: 2.4
258 */
259 GSList *
gtk_radio_tool_button_get_group(GtkRadioToolButton * button)260 gtk_radio_tool_button_get_group (GtkRadioToolButton *button)
261 {
262 g_return_val_if_fail (GTK_IS_RADIO_TOOL_BUTTON (button), NULL);
263
264 return gtk_radio_button_get_group (get_radio_button (button));
265 }
266
267 /**
268 * gtk_radio_tool_button_set_group:
269 * @button: a #GtkRadioToolButton
270 * @group: (element-type GtkRadioButton) (allow-none): an existing radio button group, or %NULL
271 *
272 * Adds @button to @group, removing it from the group it belonged to before.
273 *
274 * Since: 2.4
275 **/
276 void
gtk_radio_tool_button_set_group(GtkRadioToolButton * button,GSList * group)277 gtk_radio_tool_button_set_group (GtkRadioToolButton *button,
278 GSList *group)
279 {
280 g_return_if_fail (GTK_IS_RADIO_TOOL_BUTTON (button));
281
282 gtk_radio_button_set_group (get_radio_button (button), group);
283 }
284