1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 */
26
27 #include "config.h"
28 #include "gtkaccellabel.h"
29 #include "gtkmarshalers.h"
30 #include "gtkradiomenuitem.h"
31 #include "gtkactivatable.h"
32 #include "gtkprivate.h"
33 #include "gtkintl.h"
34 #include "gtkalias.h"
35
36
37 enum {
38 PROP_0,
39 PROP_GROUP
40 };
41
42
43 static void gtk_radio_menu_item_destroy (GtkObject *object);
44 static void gtk_radio_menu_item_activate (GtkMenuItem *menu_item);
45 static void gtk_radio_menu_item_set_property (GObject *object,
46 guint prop_id,
47 const GValue *value,
48 GParamSpec *pspec);
49 static void gtk_radio_menu_item_get_property (GObject *object,
50 guint prop_id,
51 GValue *value,
52 GParamSpec *pspec);
53
54 static guint group_changed_signal = 0;
55
G_DEFINE_TYPE(GtkRadioMenuItem,gtk_radio_menu_item,GTK_TYPE_CHECK_MENU_ITEM)56 G_DEFINE_TYPE (GtkRadioMenuItem, gtk_radio_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
57
58 GtkWidget*
59 gtk_radio_menu_item_new (GSList *group)
60 {
61 GtkRadioMenuItem *radio_menu_item;
62
63 radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
64
65 gtk_radio_menu_item_set_group (radio_menu_item, group);
66
67 return GTK_WIDGET (radio_menu_item);
68 }
69
70 static void
gtk_radio_menu_item_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)71 gtk_radio_menu_item_set_property (GObject *object,
72 guint prop_id,
73 const GValue *value,
74 GParamSpec *pspec)
75 {
76 GtkRadioMenuItem *radio_menu_item;
77
78 radio_menu_item = GTK_RADIO_MENU_ITEM (object);
79
80 switch (prop_id)
81 {
82 GSList *slist;
83
84 case PROP_GROUP:
85 if (G_VALUE_HOLDS_OBJECT (value))
86 slist = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
87 else
88 slist = NULL;
89 gtk_radio_menu_item_set_group (radio_menu_item, slist);
90 break;
91 default:
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 break;
94 }
95 }
96
97 static void
gtk_radio_menu_item_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)98 gtk_radio_menu_item_get_property (GObject *object,
99 guint prop_id,
100 GValue *value,
101 GParamSpec *pspec)
102 {
103 switch (prop_id)
104 {
105 default:
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 break;
108 }
109 }
110
111 void
gtk_radio_menu_item_set_group(GtkRadioMenuItem * radio_menu_item,GSList * group)112 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
113 GSList *group)
114 {
115 GtkWidget *old_group_singleton = NULL;
116 GtkWidget *new_group_singleton = NULL;
117
118 g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
119 g_return_if_fail (!g_slist_find (group, radio_menu_item));
120
121 if (radio_menu_item->group)
122 {
123 GSList *slist;
124
125 radio_menu_item->group = g_slist_remove (radio_menu_item->group, radio_menu_item);
126
127 if (radio_menu_item->group && !radio_menu_item->group->next)
128 old_group_singleton = g_object_ref (radio_menu_item->group->data);
129
130 for (slist = radio_menu_item->group; slist; slist = slist->next)
131 {
132 GtkRadioMenuItem *tmp_item;
133
134 tmp_item = slist->data;
135
136 tmp_item->group = radio_menu_item->group;
137 }
138 }
139
140 if (group && !group->next)
141 new_group_singleton = g_object_ref (group->data);
142
143 radio_menu_item->group = g_slist_prepend (group, radio_menu_item);
144
145 if (group)
146 {
147 GSList *slist;
148
149 for (slist = group; slist; slist = slist->next)
150 {
151 GtkRadioMenuItem *tmp_item;
152
153 tmp_item = slist->data;
154
155 tmp_item->group = radio_menu_item->group;
156 }
157 }
158 else
159 {
160 GTK_CHECK_MENU_ITEM (radio_menu_item)->active = TRUE;
161 /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
162 */
163 }
164
165 g_object_ref (radio_menu_item);
166
167 g_object_notify (G_OBJECT (radio_menu_item), "group");
168 g_signal_emit (radio_menu_item, group_changed_signal, 0);
169 if (old_group_singleton)
170 {
171 g_signal_emit (old_group_singleton, group_changed_signal, 0);
172 g_object_unref (old_group_singleton);
173 }
174 if (new_group_singleton)
175 {
176 g_signal_emit (new_group_singleton, group_changed_signal, 0);
177 g_object_unref (new_group_singleton);
178 }
179
180 g_object_unref (radio_menu_item);
181 }
182
183
184 /**
185 * gtk_radio_menu_item_new_with_label:
186 * @group: (element-type GtkRadioMenuItem) (transfer full):
187 * @label: the text for the label
188 *
189 * Creates a new #GtkRadioMenuItem whose child is a simple #GtkLabel.
190 *
191 * Returns: (transfer none): A new #GtkRadioMenuItem
192 */
193 GtkWidget*
gtk_radio_menu_item_new_with_label(GSList * group,const gchar * label)194 gtk_radio_menu_item_new_with_label (GSList *group,
195 const gchar *label)
196 {
197 GtkWidget *radio_menu_item;
198 GtkWidget *accel_label;
199
200 radio_menu_item = gtk_radio_menu_item_new (group);
201 accel_label = gtk_accel_label_new (label);
202 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
203 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
204 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
205 gtk_widget_show (accel_label);
206
207 return radio_menu_item;
208 }
209
210
211 /**
212 * gtk_radio_menu_item_new_with_mnemonic:
213 * @group: group the radio menu item is inside
214 * @label: the text of the button, with an underscore in front of the
215 * mnemonic character
216 * @returns: a new #GtkRadioMenuItem
217 *
218 * Creates a new #GtkRadioMenuItem containing a label. The label
219 * will be created using gtk_label_new_with_mnemonic(), so underscores
220 * in @label indicate the mnemonic for the menu item.
221 **/
222 GtkWidget*
gtk_radio_menu_item_new_with_mnemonic(GSList * group,const gchar * label)223 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
224 const gchar *label)
225 {
226 GtkWidget *radio_menu_item;
227 GtkWidget *accel_label;
228
229 radio_menu_item = gtk_radio_menu_item_new (group);
230 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
231 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
232 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
233
234 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
235 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
236 gtk_widget_show (accel_label);
237
238 return radio_menu_item;
239 }
240
241 /**
242 * gtk_radio_menu_item_new_from_widget:
243 * @group: An existing #GtkRadioMenuItem
244 *
245 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
246 *
247 * Return value: (transfer none): The new #GtkRadioMenuItem
248 *
249 * Since: 2.4
250 **/
251 GtkWidget *
gtk_radio_menu_item_new_from_widget(GtkRadioMenuItem * group)252 gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
253 {
254 GSList *list = NULL;
255
256 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
257
258 if (group)
259 list = gtk_radio_menu_item_get_group (group);
260
261 return gtk_radio_menu_item_new (list);
262 }
263
264 /**
265 * gtk_radio_menu_item_new_with_mnemonic_from_widget:
266 * @group: An existing #GtkRadioMenuItem
267 * @label: the text of the button, with an underscore in front of the
268 * mnemonic character
269 *
270 * Creates a new GtkRadioMenuItem containing a label. The label will be
271 * created using gtk_label_new_with_mnemonic(), so underscores in label
272 * indicate the mnemonic for the menu item.
273 *
274 * The new #GtkRadioMenuItem is added to the same group as @group.
275 *
276 * Return value: (transfer none): The new #GtkRadioMenuItem
277 *
278 * Since: 2.4
279 **/
280 GtkWidget *
gtk_radio_menu_item_new_with_mnemonic_from_widget(GtkRadioMenuItem * group,const gchar * label)281 gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
282 const gchar *label)
283 {
284 GSList *list = NULL;
285
286 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
287
288 if (group)
289 list = gtk_radio_menu_item_get_group (group);
290
291 return gtk_radio_menu_item_new_with_mnemonic (list, label);
292 }
293
294 /**
295 * gtk_radio_menu_item_new_with_label_from_widget:
296 * @group: an existing #GtkRadioMenuItem
297 * @label: the text for the label
298 *
299 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
300 * The new #GtkRadioMenuItem is added to the same group as @group.
301 *
302 * Return value: (transfer none): The new #GtkRadioMenuItem
303 *
304 * Since: 2.4
305 **/
306 GtkWidget *
gtk_radio_menu_item_new_with_label_from_widget(GtkRadioMenuItem * group,const gchar * label)307 gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
308 const gchar *label)
309 {
310 GSList *list = NULL;
311
312 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
313
314 if (group)
315 list = gtk_radio_menu_item_get_group (group);
316
317 return gtk_radio_menu_item_new_with_label (list, label);
318 }
319
320 /**
321 * gtk_radio_menu_item_get_group:
322 * @radio_menu_item: a #GtkRadioMenuItem
323 *
324 * Returns the group to which the radio menu item belongs, as a #GList of
325 * #GtkRadioMenuItem. The list belongs to GTK+ and should not be freed.
326 *
327 * Returns: (transfer none): the group of @radio_menu_item
328 */
329 GSList*
gtk_radio_menu_item_get_group(GtkRadioMenuItem * radio_menu_item)330 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
331 {
332 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
333
334 return radio_menu_item->group;
335 }
336
337
338 static void
gtk_radio_menu_item_class_init(GtkRadioMenuItemClass * klass)339 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
340 {
341 GObjectClass *gobject_class;
342 GtkObjectClass *object_class;
343 GtkMenuItemClass *menu_item_class;
344
345 gobject_class = G_OBJECT_CLASS (klass);
346 object_class = GTK_OBJECT_CLASS (klass);
347 menu_item_class = GTK_MENU_ITEM_CLASS (klass);
348
349 gobject_class->set_property = gtk_radio_menu_item_set_property;
350 gobject_class->get_property = gtk_radio_menu_item_get_property;
351
352 /**
353 * GtkRadioMenuItem:group:
354 *
355 * The radio menu item whose group this widget belongs to.
356 *
357 * Since: 2.8
358 */
359 g_object_class_install_property (gobject_class,
360 PROP_GROUP,
361 g_param_spec_object ("group",
362 P_("Group"),
363 P_("The radio menu item whose group this widget belongs to."),
364 GTK_TYPE_RADIO_MENU_ITEM,
365 GTK_PARAM_WRITABLE));
366
367 object_class->destroy = gtk_radio_menu_item_destroy;
368
369 menu_item_class->activate = gtk_radio_menu_item_activate;
370
371 /**
372 * GtkStyle::group-changed:
373 * @style: the object which received the signal
374 *
375 * Emitted when the group of radio menu items that a radio menu item belongs
376 * to changes. This is emitted when a radio menu item switches from
377 * being alone to being part of a group of 2 or more menu items, or
378 * vice-versa, and when a button is moved from one group of 2 or
379 * more menu items ton a different one, but not when the composition
380 * of the group that a menu item belongs to changes.
381 *
382 * Since: 2.4
383 */
384 group_changed_signal = g_signal_new (I_("group-changed"),
385 G_OBJECT_CLASS_TYPE (object_class),
386 G_SIGNAL_RUN_FIRST,
387 G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
388 NULL, NULL,
389 _gtk_marshal_VOID__VOID,
390 G_TYPE_NONE, 0);
391 }
392
393 static void
gtk_radio_menu_item_init(GtkRadioMenuItem * radio_menu_item)394 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
395 {
396 radio_menu_item->group = g_slist_prepend (NULL, radio_menu_item);
397 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
398 }
399
400 static void
gtk_radio_menu_item_destroy(GtkObject * object)401 gtk_radio_menu_item_destroy (GtkObject *object)
402 {
403 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (object);
404 GtkWidget *old_group_singleton = NULL;
405 GtkRadioMenuItem *tmp_menu_item;
406 GSList *tmp_list;
407 gboolean was_in_group;
408
409 was_in_group = radio_menu_item->group && radio_menu_item->group->next;
410
411 radio_menu_item->group = g_slist_remove (radio_menu_item->group,
412 radio_menu_item);
413 if (radio_menu_item->group && !radio_menu_item->group->next)
414 old_group_singleton = radio_menu_item->group->data;
415
416 tmp_list = radio_menu_item->group;
417
418 while (tmp_list)
419 {
420 tmp_menu_item = tmp_list->data;
421 tmp_list = tmp_list->next;
422
423 tmp_menu_item->group = radio_menu_item->group;
424 }
425
426 /* this radio menu item is no longer in the group */
427 radio_menu_item->group = NULL;
428
429 if (old_group_singleton)
430 g_signal_emit (old_group_singleton, group_changed_signal, 0);
431 if (was_in_group)
432 g_signal_emit (radio_menu_item, group_changed_signal, 0);
433
434 GTK_OBJECT_CLASS (gtk_radio_menu_item_parent_class)->destroy (object);
435 }
436
437 static void
gtk_radio_menu_item_activate(GtkMenuItem * menu_item)438 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
439 {
440 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
441 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
442 GtkCheckMenuItem *tmp_menu_item;
443 GtkAction *action;
444 GSList *tmp_list;
445 gint toggled;
446
447 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
448 if (action && gtk_menu_item_get_submenu (menu_item) == NULL)
449 gtk_action_activate (action);
450
451 toggled = FALSE;
452
453 if (check_menu_item->active)
454 {
455 tmp_menu_item = NULL;
456 tmp_list = radio_menu_item->group;
457
458 while (tmp_list)
459 {
460 tmp_menu_item = tmp_list->data;
461 tmp_list = tmp_list->next;
462
463 if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
464 break;
465
466 tmp_menu_item = NULL;
467 }
468
469 if (tmp_menu_item)
470 {
471 toggled = TRUE;
472 check_menu_item->active = !check_menu_item->active;
473 }
474 }
475 else
476 {
477 toggled = TRUE;
478 check_menu_item->active = !check_menu_item->active;
479
480 tmp_list = radio_menu_item->group;
481 while (tmp_list)
482 {
483 tmp_menu_item = tmp_list->data;
484 tmp_list = tmp_list->next;
485
486 if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
487 {
488 gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
489 break;
490 }
491 }
492 }
493
494 if (toggled)
495 {
496 gtk_check_menu_item_toggled (check_menu_item);
497 }
498
499 gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));
500 }
501
502 #define __GTK_RADIO_MENU_ITEM_C__
503 #include "gtkaliasdef.c"
504