]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiotoolbutton.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / gtk / gtkradiotoolbutton.c
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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <config.h>
24 #include "gtkradiotoolbutton.h"
25 #include "gtkradiobutton.h"
26 #include "gtkintl.h"
27
28 enum {
29   PROP_0,
30   PROP_GROUP
31 };
32
33 static void gtk_radio_tool_button_init         (GtkRadioToolButton      *button);
34 static void gtk_radio_tool_button_class_init   (GtkRadioToolButtonClass *klass);
35 static void gtk_radio_tool_button_set_property (GObject         *object,
36                                                 guint            prop_id,
37                                                 const GValue    *value,
38                                                 GParamSpec      *pspec);
39
40 GType
41 gtk_radio_tool_button_get_type (void)
42 {
43   static GType type = 0;
44
45   if (!type)
46     {
47       static const GTypeInfo type_info =
48         {
49           sizeof (GtkRadioToolButtonClass),
50           (GBaseInitFunc) NULL,
51           (GBaseFinalizeFunc) NULL,
52           (GClassInitFunc) gtk_radio_tool_button_class_init,
53           (GClassFinalizeFunc) NULL,
54           NULL,
55           sizeof (GtkRadioToolButton),
56           0, /* n_preallocs */
57           (GInstanceInitFunc) gtk_radio_tool_button_init
58         };
59
60       type = g_type_register_static (GTK_TYPE_TOGGLE_TOOL_BUTTON,
61                                      "GtkRadioToolButton", &type_info, 0);
62     }
63   return type;
64 }
65
66      
67 static void
68 gtk_radio_tool_button_class_init (GtkRadioToolButtonClass *klass)
69 {
70   GObjectClass *object_class;
71   GtkToolButtonClass *toolbutton_class;
72
73   object_class = (GObjectClass *)klass;
74   toolbutton_class = (GtkToolButtonClass *)klass;
75
76   object_class->set_property = gtk_radio_tool_button_set_property;
77   
78   toolbutton_class->button_type = GTK_TYPE_RADIO_BUTTON;  
79
80   /**
81    * GtkRadioToolButton:group:
82    *
83    * Sets a new group for a radio tool button.
84    *
85    * Since: 2.4
86    */
87   g_object_class_install_property (object_class,
88                                    PROP_GROUP,
89                                    g_param_spec_object ("group",
90                                                         _("Group"),
91                                                         _("The radio tool button whose group this button belongs to."),
92                                                         GTK_TYPE_RADIO_TOOL_BUTTON,
93                                                         G_PARAM_WRITABLE));
94
95 }
96
97 static void
98 gtk_radio_tool_button_init (GtkRadioToolButton *button)
99 {
100   GtkToolButton *tool_button = GTK_TOOL_BUTTON (button);
101   gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)), FALSE);
102 }
103
104 static void
105 gtk_radio_tool_button_set_property (GObject         *object,
106                                     guint            prop_id,
107                                     const GValue    *value,
108                                     GParamSpec      *pspec)
109 {
110   GtkRadioToolButton *button;
111
112   button = GTK_RADIO_TOOL_BUTTON (object);
113
114   switch (prop_id)
115     {
116     case PROP_GROUP:
117       {
118         GtkRadioToolButton *arg;
119         GSList *slist = NULL;
120         if (G_VALUE_HOLDS_OBJECT (value)) 
121           {
122             arg = GTK_RADIO_TOOL_BUTTON (g_value_get_object (value));
123             if (arg)
124               slist = gtk_radio_tool_button_get_group (arg);
125             gtk_radio_tool_button_set_group (button, slist);
126           }
127       }
128       break;
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132     }
133 }
134
135 /**
136  * gtk_radio_tool_button_new:
137  * @group: An existing radio button group, or %NULL if you are creating a new group
138  * 
139  * Creates a new #GtkRadioToolButton, adding it to @group.
140  * 
141  * Return value: The new #GtkRadioToolButton
142  * 
143  * Since: 2.4
144  **/
145 GtkToolItem *
146 gtk_radio_tool_button_new (GSList *group)
147 {
148   GtkRadioToolButton *button;
149   
150   button = g_object_new (GTK_TYPE_RADIO_TOOL_BUTTON,
151                          NULL);
152
153   gtk_radio_tool_button_set_group (button, group);
154   
155   return GTK_TOOL_ITEM (button);
156 }
157
158 /**
159  * gtk_radio_tool_button_new_from_stock:
160  * @group: an existing radio button group, or %NULL if you are creating a new group
161  * @stock_id: the name of a stock item
162  * 
163  * Creates a new #GtkRadioToolButton, adding it to @group. 
164  * The new #GtkRadioToolButton will contain an icon and label from the
165  * stock item indicated by @stock_id.
166  * 
167  * Return value: The new #GtkRadioToolItem
168  * 
169  * Since: 2.4
170  **/
171 GtkToolItem *
172 gtk_radio_tool_button_new_from_stock (GSList      *group,
173                                       const gchar *stock_id)
174 {
175   GtkRadioToolButton *button;
176
177   g_return_val_if_fail (stock_id != NULL, NULL);
178   
179   button = g_object_new (GTK_TYPE_RADIO_TOOL_BUTTON,
180                          "stock_id", stock_id,
181                          NULL);
182
183
184   gtk_radio_tool_button_set_group (button, group);
185   
186   return GTK_TOOL_ITEM (button);
187 }
188
189 /**
190  * gtk_radio_tool_button_new_from_widget:
191  * @group: An existing #GtkRadioToolButton
192  * 
193  * Creates a new #GtkRadioToolButton adding it to the same group as @gruup
194  * 
195  * Return value: The new #GtkRadioToolButton
196  * 
197  * Since: 2.4
198  **/
199 GtkToolItem *
200 gtk_radio_tool_button_new_from_widget (GtkRadioToolButton *group)
201 {
202   GSList *list = NULL;
203   
204   g_return_val_if_fail (GTK_IS_RADIO_TOOL_BUTTON (group), NULL);
205
206   if (group)
207     list = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (group));
208   
209   return gtk_radio_tool_button_new (list);
210 }
211
212 /**
213  * gtk_radio_tool_button_new_with_stock_from_widget:
214  * @group: An existing #GtkRadioToolButton.
215  * @stock_id: the name of a stock item 
216  * 
217  * Creates a new #GtkRadioToolButton adding it to the same group as @group.
218  * The new #GtkRadioToolButton will contain an icon and label from the
219  * stock item indicated by @stock_id.
220  * 
221  * Return value: A new #GtkRadioToolButton
222  * 
223  * Since: 2.4
224  **/
225 GtkToolItem *
226 gtk_radio_tool_button_new_with_stock_from_widget (GtkRadioToolButton *group,
227                                                   const gchar        *stock_id)
228 {
229   GSList *list = NULL;
230   
231   g_return_val_if_fail (GTK_IS_RADIO_TOOL_BUTTON (group), NULL);
232
233   if (group)
234     list = gtk_radio_tool_button_get_group (group);
235   
236   return gtk_radio_tool_button_new_from_stock (list, stock_id);
237 }
238
239 static GtkRadioButton *
240 get_radio_button (GtkRadioToolButton *button)
241 {
242   return GTK_RADIO_BUTTON (_gtk_tool_button_get_button (GTK_TOOL_BUTTON (button)));
243 }
244
245 /**
246  * gtk_radio_tool_button_get_group:
247  * @button: a #GtkRadioToolButton
248  *
249  * Returns the radio button group @button belongs to.
250  * 
251  * Return value: The group @button belongs to.
252  * 
253  * Since: 2.4
254  **/
255 GSList *
256 gtk_radio_tool_button_get_group (GtkRadioToolButton *button)
257 {
258   g_return_val_if_fail (GTK_IS_RADIO_TOOL_BUTTON (button), NULL);
259
260   return gtk_radio_button_get_group (get_radio_button (button));
261 }
262
263 /**
264  * gtk_radio_tool_button_set_group:
265  * @button: a #GtkRadioToolButton
266  * @group: an existing radio button group
267  * 
268  * Adds @button to @group, removing it from the group it belonged to before.
269  * 
270  * Since: 2.4
271  **/
272 void
273 gtk_radio_tool_button_set_group (GtkRadioToolButton *button,
274                                  GSList             *group)
275 {
276   g_return_if_fail (GTK_IS_RADIO_TOOL_BUTTON (button));
277
278   gtk_radio_button_set_group (get_radio_button (button), group);
279 }
280