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/.
28 #include "gtkaccellabel.h"
29 #include "gtkmarshalers.h"
30 #include "gtkradiomenuitem.h"
31 #include "gtkactivatable.h"
32 #include "gtkprivate.h"
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,
49 static void gtk_radio_menu_item_get_property (GObject *object,
54 static guint group_changed_signal = 0;
56 G_DEFINE_TYPE (GtkRadioMenuItem, gtk_radio_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
59 gtk_radio_menu_item_new (GSList *group)
61 GtkRadioMenuItem *radio_menu_item;
63 radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
65 gtk_radio_menu_item_set_group (radio_menu_item, group);
67 return GTK_WIDGET (radio_menu_item);
71 gtk_radio_menu_item_set_property (GObject *object,
76 GtkRadioMenuItem *radio_menu_item;
78 radio_menu_item = GTK_RADIO_MENU_ITEM (object);
85 if (G_VALUE_HOLDS_OBJECT (value))
86 slist = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
89 gtk_radio_menu_item_set_group (radio_menu_item, slist);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98 gtk_radio_menu_item_get_property (GObject *object,
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
115 GtkWidget *old_group_singleton = NULL;
116 GtkWidget *new_group_singleton = NULL;
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));
121 if (radio_menu_item->group)
125 radio_menu_item->group = g_slist_remove (radio_menu_item->group, radio_menu_item);
127 if (radio_menu_item->group && !radio_menu_item->group->next)
128 old_group_singleton = g_object_ref (radio_menu_item->group->data);
130 for (slist = radio_menu_item->group; slist; slist = slist->next)
132 GtkRadioMenuItem *tmp_item;
134 tmp_item = slist->data;
136 tmp_item->group = radio_menu_item->group;
140 if (group && !group->next)
141 new_group_singleton = g_object_ref (group->data);
143 radio_menu_item->group = g_slist_prepend (group, radio_menu_item);
149 for (slist = group; slist; slist = slist->next)
151 GtkRadioMenuItem *tmp_item;
153 tmp_item = slist->data;
155 tmp_item->group = radio_menu_item->group;
160 GTK_CHECK_MENU_ITEM (radio_menu_item)->active = TRUE;
161 /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
165 g_object_ref (radio_menu_item);
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)
171 g_signal_emit (old_group_singleton, group_changed_signal, 0);
172 g_object_unref (old_group_singleton);
174 if (new_group_singleton)
176 g_signal_emit (new_group_singleton, group_changed_signal, 0);
177 g_object_unref (new_group_singleton);
180 g_object_unref (radio_menu_item);
184 gtk_radio_menu_item_new_with_label (GSList *group,
187 GtkWidget *radio_menu_item;
188 GtkWidget *accel_label;
190 radio_menu_item = gtk_radio_menu_item_new (group);
191 accel_label = gtk_accel_label_new (label);
192 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;
202 * gtk_radio_menu_item_new_with_mnemonic:
203 * @group: group the radio menu item is inside
204 * @label: the text of the button, with an underscore in front of the
206 * @returns: a new #GtkRadioMenuItem
208 * Creates a new #GtkRadioMenuItem containing a label. The label
209 * will be created using gtk_label_new_with_mnemonic(), so underscores
210 * in @label indicate the mnemonic for the menu item.
213 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
216 GtkWidget *radio_menu_item;
217 GtkWidget *accel_label;
219 radio_menu_item = gtk_radio_menu_item_new (group);
220 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
221 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
222 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
224 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
225 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
226 gtk_widget_show (accel_label);
228 return radio_menu_item;
232 * gtk_radio_menu_item_new_from_widget:
233 * @group: An existing #GtkRadioMenuItem
235 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
237 * Return value: The new #GtkRadioMenuItem
242 gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
246 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
249 list = gtk_radio_menu_item_get_group (group);
251 return gtk_radio_menu_item_new (list);
255 * gtk_radio_menu_item_new_with_mnemonic_from_widget:
256 * @group: An existing #GtkRadioMenuItem
257 * @label: the text of the button, with an underscore in front of the
260 * Creates a new GtkRadioMenuItem containing a label. The label will be
261 * created using gtk_label_new_with_mnemonic(), so underscores in label
262 * indicate the mnemonic for the menu item.
264 * The new #GtkRadioMenuItem is added to the same group as @group.
266 * Return value: The new #GtkRadioMenuItem
271 gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
276 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
279 list = gtk_radio_menu_item_get_group (group);
281 return gtk_radio_menu_item_new_with_mnemonic (list, label);
285 * gtk_radio_menu_item_new_with_label_from_widget:
286 * @group: an existing #GtkRadioMenuItem
287 * @label: the text for the label
289 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
290 * The new #GtkRadioMenuItem is added to the same group as @group.
292 * Return value: The new #GtkRadioMenuItem
297 gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
302 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
305 list = gtk_radio_menu_item_get_group (group);
307 return gtk_radio_menu_item_new_with_label (list, label);
311 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
313 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
315 return radio_menu_item->group;
320 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
322 GObjectClass *gobject_class;
323 GtkObjectClass *object_class;
324 GtkMenuItemClass *menu_item_class;
326 gobject_class = G_OBJECT_CLASS (klass);
327 object_class = GTK_OBJECT_CLASS (klass);
328 menu_item_class = GTK_MENU_ITEM_CLASS (klass);
330 gobject_class->set_property = gtk_radio_menu_item_set_property;
331 gobject_class->get_property = gtk_radio_menu_item_get_property;
334 * GtkRadioMenuItem:group:
336 * The radio menu item whose group this widget belongs to.
340 g_object_class_install_property (gobject_class,
342 g_param_spec_object ("group",
344 P_("The radio menu item whose group this widget belongs to."),
345 GTK_TYPE_RADIO_MENU_ITEM,
346 GTK_PARAM_WRITABLE));
348 object_class->destroy = gtk_radio_menu_item_destroy;
350 menu_item_class->activate = gtk_radio_menu_item_activate;
353 * GtkStyle::group-changed:
354 * @style: the object which received the signal
356 * Emitted when the group of radio menu items that a radio menu item belongs
357 * to changes. This is emitted when a radio menu item switches from
358 * being alone to being part of a group of 2 or more menu items, or
359 * vice-versa, and when a button is moved from one group of 2 or
360 * more menu items ton a different one, but not when the composition
361 * of the group that a menu item belongs to changes.
365 group_changed_signal = g_signal_new (I_("group-changed"),
366 G_OBJECT_CLASS_TYPE (object_class),
368 G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
370 _gtk_marshal_VOID__VOID,
375 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
377 radio_menu_item->group = g_slist_prepend (NULL, radio_menu_item);
378 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
382 gtk_radio_menu_item_destroy (GtkObject *object)
384 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (object);
385 GtkWidget *old_group_singleton = NULL;
386 GtkRadioMenuItem *tmp_menu_item;
388 gboolean was_in_group;
390 was_in_group = radio_menu_item->group && radio_menu_item->group->next;
392 radio_menu_item->group = g_slist_remove (radio_menu_item->group,
394 if (radio_menu_item->group && !radio_menu_item->group->next)
395 old_group_singleton = radio_menu_item->group->data;
397 tmp_list = radio_menu_item->group;
401 tmp_menu_item = tmp_list->data;
402 tmp_list = tmp_list->next;
404 tmp_menu_item->group = radio_menu_item->group;
407 /* this radio menu item is no longer in the group */
408 radio_menu_item->group = NULL;
410 if (old_group_singleton)
411 g_signal_emit (old_group_singleton, group_changed_signal, 0);
413 g_signal_emit (radio_menu_item, group_changed_signal, 0);
415 GTK_OBJECT_CLASS (gtk_radio_menu_item_parent_class)->destroy (object);
419 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
421 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
422 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
423 GtkCheckMenuItem *tmp_menu_item;
428 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
429 if (action && gtk_menu_item_get_submenu (menu_item) == NULL)
430 gtk_action_activate (action);
434 if (check_menu_item->active)
436 tmp_menu_item = NULL;
437 tmp_list = radio_menu_item->group;
441 tmp_menu_item = tmp_list->data;
442 tmp_list = tmp_list->next;
444 if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
447 tmp_menu_item = NULL;
453 check_menu_item->active = !check_menu_item->active;
459 check_menu_item->active = !check_menu_item->active;
461 tmp_list = radio_menu_item->group;
464 tmp_menu_item = tmp_list->data;
465 tmp_list = tmp_list->next;
467 if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
469 gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
477 gtk_check_menu_item_toggled (check_menu_item);
480 gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));
483 #define __GTK_RADIO_MENU_ITEM_C__
484 #include "gtkaliasdef.c"