1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #include "gtkaccellabel.h"
28 #include "gtkmarshalers.h"
29 #include "gtkradiomenuitem.h"
32 static void gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass);
33 static void gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item);
34 static void gtk_radio_menu_item_destroy (GtkObject *object);
35 static void gtk_radio_menu_item_activate (GtkMenuItem *menu_item);
37 static GtkCheckMenuItemClass *parent_class = NULL;
39 static guint group_changed_signal = 0;
42 gtk_radio_menu_item_get_type (void)
44 static GType radio_menu_item_type = 0;
46 if (!radio_menu_item_type)
48 static const GTypeInfo radio_menu_item_info =
50 sizeof (GtkRadioMenuItemClass),
52 NULL, /* base_finalize */
53 (GClassInitFunc) gtk_radio_menu_item_class_init,
54 NULL, /* class_finalize */
55 NULL, /* class_data */
56 sizeof (GtkRadioMenuItem),
58 (GInstanceInitFunc) gtk_radio_menu_item_init,
61 radio_menu_item_type =
62 g_type_register_static (GTK_TYPE_CHECK_MENU_ITEM, "GtkRadioMenuItem",
63 &radio_menu_item_info, 0);
66 return radio_menu_item_type;
70 gtk_radio_menu_item_new (GSList *group)
72 GtkRadioMenuItem *radio_menu_item;
74 radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
76 gtk_radio_menu_item_set_group (radio_menu_item, group);
78 return GTK_WIDGET (radio_menu_item);
82 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
85 GtkWidget *old_group_singleton = NULL;
86 GtkWidget *new_group_singleton = NULL;
88 g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
89 g_return_if_fail (!g_slist_find (group, radio_menu_item));
91 if (radio_menu_item->group)
95 radio_menu_item->group = g_slist_remove (radio_menu_item->group, radio_menu_item);
97 if (radio_menu_item->group && !radio_menu_item->group->next)
98 old_group_singleton = g_object_ref (radio_menu_item->group->data);
100 for (slist = radio_menu_item->group; slist; slist = slist->next)
102 GtkRadioMenuItem *tmp_item;
104 tmp_item = slist->data;
106 tmp_item->group = radio_menu_item->group;
110 if (group && !group->next)
111 new_group_singleton = g_object_ref (group->data);
113 radio_menu_item->group = g_slist_prepend (group, radio_menu_item);
119 for (slist = group; slist; slist = slist->next)
121 GtkRadioMenuItem *tmp_item;
123 tmp_item = slist->data;
125 tmp_item->group = radio_menu_item->group;
130 GTK_CHECK_MENU_ITEM (radio_menu_item)->active = TRUE;
131 /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
135 g_object_ref (radio_menu_item);
137 g_signal_emit (radio_menu_item, group_changed_signal, 0);
138 if (old_group_singleton)
140 g_signal_emit (old_group_singleton, group_changed_signal, 0);
141 g_object_unref (old_group_singleton);
143 if (new_group_singleton)
145 g_signal_emit (new_group_singleton, group_changed_signal, 0);
146 g_object_unref (new_group_singleton);
149 g_object_unref (radio_menu_item);
153 gtk_radio_menu_item_new_with_label (GSList *group,
156 GtkWidget *radio_menu_item;
157 GtkWidget *accel_label;
159 radio_menu_item = gtk_radio_menu_item_new (group);
160 accel_label = gtk_accel_label_new (label);
161 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
162 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
163 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
164 gtk_widget_show (accel_label);
166 return radio_menu_item;
171 * gtk_radio_menu_item_new_with_mnemonic:
172 * @group: group the radio menu item is inside
173 * @label: the text of the button, with an underscore in front of the
175 * @returns: a new #GtkRadioMenuItem
177 * Creates a new #GtkRadioMenuItem containing a label. The label
178 * will be created using gtk_label_new_with_mnemonic(), so underscores
179 * in @label indicate the mnemonic for the menu item.
182 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
185 GtkWidget *radio_menu_item;
186 GtkWidget *accel_label;
188 radio_menu_item = gtk_radio_menu_item_new (group);
189 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
190 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
191 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
193 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
194 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
195 gtk_widget_show (accel_label);
197 return radio_menu_item;
201 * gtk_radio_menu_item_new_from_widget:
202 * @group: An existing #GtkRadioMenuItem
204 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
206 * Return value: The new #GtkRadioMenuItem
211 gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
215 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
218 list = gtk_radio_menu_item_get_group (group);
220 return gtk_radio_menu_item_new (list);
224 * gtk_radio_menu_item_new_with_mnemonic_from_widget:
225 * @group: An existing #GtkRadioMenuItem
226 * @label: the text of the button, with an underscore in front of the
229 * Creates a new GtkRadioMenuItem containing a label. The label will be
230 * created using gtk_label_new_with_mnemonic(), so underscores in label
231 * indicate the mnemonic for the menu item.
233 * The new #GtkRadioMenuItem is added to the same group as @group.
235 * Return value: The new #GtkRadioMenuItem
240 gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
245 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
248 list = gtk_radio_menu_item_get_group (group);
250 return gtk_radio_menu_item_new_with_mnemonic (list, label);
254 * gtk_radio_menu_item_new_with_label_from_widget:
255 * @group: an existing #GtkRadioMenuItem
256 * @label: the text for the label
258 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
259 * The new #GtkRadioMenuItem is added to the same group as @group.
261 * Return value: The new #GtkRadioMenuItem
266 gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
271 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
274 list = gtk_radio_menu_item_get_group (group);
276 return gtk_radio_menu_item_new_with_label (list, label);
280 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
282 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
284 return radio_menu_item->group;
289 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
291 GtkObjectClass *object_class;
292 GtkMenuItemClass *menu_item_class;
294 object_class = (GtkObjectClass*) klass;
295 menu_item_class = (GtkMenuItemClass*) klass;
297 parent_class = g_type_class_peek_parent (klass);
299 object_class->destroy = gtk_radio_menu_item_destroy;
301 menu_item_class->activate = gtk_radio_menu_item_activate;
304 * GtkStyle::group-changed:
305 * @style: the object which received the signal
307 * Emitted when the group of radio menu items that a radio menu item belongs
308 * to changes. This is emitted when a radio menu item switches from
309 * being alone to being part of a group of 2 or more menu items, or
310 * vice-versa, and when a buttton is moved from one group of 2 or
311 * more menu items to a different one, but not when the composition
312 * of the group that a menu item belongs to changes.
314 group_changed_signal = g_signal_new ("group-changed",
315 G_OBJECT_CLASS_TYPE (object_class),
317 G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
319 _gtk_marshal_VOID__VOID,
324 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
326 radio_menu_item->group = g_slist_prepend (NULL, radio_menu_item);
327 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
331 gtk_radio_menu_item_destroy (GtkObject *object)
333 GtkWidget *old_group_singleton = NULL;
334 GtkRadioMenuItem *radio_menu_item;
335 GtkRadioMenuItem *tmp_menu_item;
337 gboolean was_in_group;
339 g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (object));
341 radio_menu_item = GTK_RADIO_MENU_ITEM (object);
343 was_in_group = radio_menu_item->group && radio_menu_item->group->next;
345 radio_menu_item->group = g_slist_remove (radio_menu_item->group,
347 if (radio_menu_item->group && !radio_menu_item->group->next)
348 old_group_singleton = radio_menu_item->group->data;
350 tmp_list = radio_menu_item->group;
354 tmp_menu_item = tmp_list->data;
355 tmp_list = tmp_list->next;
357 tmp_menu_item->group = radio_menu_item->group;
360 /* this radio menu item is no longer in the group */
361 radio_menu_item->group = NULL;
363 if (old_group_singleton)
364 g_signal_emit (old_group_singleton, group_changed_signal, 0);
366 g_signal_emit (radio_menu_item, group_changed_signal, 0);
368 if (GTK_OBJECT_CLASS (parent_class)->destroy)
369 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
373 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
375 GtkRadioMenuItem *radio_menu_item;
376 GtkCheckMenuItem *check_menu_item;
377 GtkCheckMenuItem *tmp_menu_item;
381 g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (menu_item));
383 radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
384 check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
387 if (check_menu_item->active)
389 tmp_menu_item = NULL;
390 tmp_list = radio_menu_item->group;
394 tmp_menu_item = tmp_list->data;
395 tmp_list = tmp_list->next;
397 if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
400 tmp_menu_item = NULL;
406 check_menu_item->active = !check_menu_item->active;
412 check_menu_item->active = !check_menu_item->active;
414 tmp_list = radio_menu_item->group;
417 tmp_menu_item = tmp_list->data;
418 tmp_list = tmp_list->next;
420 if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
422 gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
429 gtk_check_menu_item_toggled (check_menu_item);
431 gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));