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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 #include "gtkaccellabel.h"
27 #include "gtkmarshalers.h"
28 #include "gtkradiomenuitem.h"
29 #include "gtkactivatable.h"
30 #include "gtkprivate.h"
32 #include "a11y/gtkradiomenuitemaccessible.h"
35 * SECTION:gtkradiomenuitem
36 * @Short_description: A choice from multiple check menu items
37 * @Title: GtkRadioMenuItem
38 * @See_also: #GtkMenuItem, #GtkCheckMenuItem
40 * A radio menu item is a check menu item that belongs to a group. At each
41 * instant exactly one of the radio menu items from a group is selected.
43 * The group list does not need to be freed, as each #GtkRadioMenuItem will
44 * remove itself and its list item when it is destroyed.
46 * The correct way to create a group of radio menu items is approximatively
50 * <title>How to create a group of radio menu items.</title>
52 * GSList *group = NULL;
56 * for (i = 0; i < 5; i++)
58 * item = gtk_radio_menu_item_new_with_label (group, "This is an example");
59 * group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
61 * gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
68 struct _GtkRadioMenuItemPrivate
79 static void gtk_radio_menu_item_destroy (GtkWidget *widget);
80 static void gtk_radio_menu_item_activate (GtkMenuItem *menu_item);
81 static void gtk_radio_menu_item_set_property (GObject *object,
85 static void gtk_radio_menu_item_get_property (GObject *object,
90 static guint group_changed_signal = 0;
92 G_DEFINE_TYPE (GtkRadioMenuItem, gtk_radio_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
95 * gtk_radio_menu_item_new:
96 * @group: the group to which the radio menu item is to be attached
98 * Creates a new #GtkRadioMenuItem.
100 * Returns: a new #GtkRadioMenuItem
103 gtk_radio_menu_item_new (GSList *group)
105 GtkRadioMenuItem *radio_menu_item;
107 radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
109 gtk_radio_menu_item_set_group (radio_menu_item, group);
111 return GTK_WIDGET (radio_menu_item);
115 gtk_radio_menu_item_set_property (GObject *object,
120 GtkRadioMenuItem *radio_menu_item;
122 radio_menu_item = GTK_RADIO_MENU_ITEM (object);
129 if (G_VALUE_HOLDS_OBJECT (value))
130 slist = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
133 gtk_radio_menu_item_set_group (radio_menu_item, slist);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 gtk_radio_menu_item_get_property (GObject *object,
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156 * gtk_radio_menu_item_set_group:
157 * @radio_menu_item: a #GtkRadioMenuItem.
158 * @group: the new group.
160 * Sets the group of a radio menu item, or changes it.
163 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
166 GtkRadioMenuItemPrivate *priv;
167 GtkWidget *old_group_singleton = NULL;
168 GtkWidget *new_group_singleton = NULL;
170 g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
171 g_return_if_fail (!g_slist_find (group, radio_menu_item));
173 priv = radio_menu_item->priv;
179 priv->group = g_slist_remove (priv->group, radio_menu_item);
181 if (priv->group && !priv->group->next)
182 old_group_singleton = g_object_ref (priv->group->data);
184 for (slist = priv->group; slist; slist = slist->next)
186 GtkRadioMenuItem *tmp_item;
188 tmp_item = slist->data;
190 tmp_item->priv->group = priv->group;
194 if (group && !group->next)
195 new_group_singleton = g_object_ref (group->data);
197 priv->group = g_slist_prepend (group, radio_menu_item);
203 for (slist = group; slist; slist = slist->next)
205 GtkRadioMenuItem *tmp_item;
207 tmp_item = slist->data;
209 tmp_item->priv->group = priv->group;
214 _gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
215 /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
219 g_object_ref (radio_menu_item);
221 g_object_notify (G_OBJECT (radio_menu_item), "group");
222 g_signal_emit (radio_menu_item, group_changed_signal, 0);
223 if (old_group_singleton)
225 g_signal_emit (old_group_singleton, group_changed_signal, 0);
226 g_object_unref (old_group_singleton);
228 if (new_group_singleton)
230 g_signal_emit (new_group_singleton, group_changed_signal, 0);
231 g_object_unref (new_group_singleton);
234 g_object_unref (radio_menu_item);
239 * gtk_radio_menu_item_new_with_label:
240 * @group: (element-type GtkRadioMenuItem) (transfer full):
241 * @label: the text for the label
243 * Creates a new #GtkRadioMenuItem whose child is a simple #GtkLabel.
245 * Returns: (transfer none): A new #GtkRadioMenuItem
248 gtk_radio_menu_item_new_with_label (GSList *group,
251 GtkWidget *radio_menu_item;
252 GtkWidget *accel_label;
254 radio_menu_item = gtk_radio_menu_item_new (group);
255 accel_label = gtk_accel_label_new (label);
256 gtk_widget_set_halign (accel_label, GTK_ALIGN_START);
257 gtk_widget_set_valign (accel_label, GTK_ALIGN_CENTER);
258 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
259 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
260 gtk_widget_show (accel_label);
262 return radio_menu_item;
267 * gtk_radio_menu_item_new_with_mnemonic:
268 * @group: group the radio menu item is inside
269 * @label: the text of the button, with an underscore in front of the
272 * Creates a new #GtkRadioMenuItem containing a label. The label
273 * will be created using gtk_label_new_with_mnemonic(), so underscores
274 * in @label indicate the mnemonic for the menu item.
276 * Returns: a new #GtkRadioMenuItem
279 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
282 GtkWidget *radio_menu_item;
283 GtkWidget *accel_label;
285 radio_menu_item = gtk_radio_menu_item_new (group);
286 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
287 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
288 gtk_widget_set_halign (accel_label, GTK_ALIGN_START);
289 gtk_widget_set_valign (accel_label, GTK_ALIGN_CENTER);
291 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
292 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
293 gtk_widget_show (accel_label);
295 return radio_menu_item;
299 * gtk_radio_menu_item_new_from_widget: (constructor)
300 * @group: An existing #GtkRadioMenuItem
302 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
304 * Return value: (transfer none): The new #GtkRadioMenuItem
309 gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
313 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
316 list = gtk_radio_menu_item_get_group (group);
318 return gtk_radio_menu_item_new (list);
322 * gtk_radio_menu_item_new_with_mnemonic_from_widget: (constructor)
323 * @group: An existing #GtkRadioMenuItem
324 * @label: the text of the button, with an underscore in front of the
327 * Creates a new GtkRadioMenuItem containing a label. The label will be
328 * created using gtk_label_new_with_mnemonic(), so underscores in label
329 * indicate the mnemonic for the menu item.
331 * The new #GtkRadioMenuItem is added to the same group as @group.
333 * Return value: (transfer none): The new #GtkRadioMenuItem
338 gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
343 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
346 list = gtk_radio_menu_item_get_group (group);
348 return gtk_radio_menu_item_new_with_mnemonic (list, label);
352 * gtk_radio_menu_item_new_with_label_from_widget: (constructor)
353 * @group: an existing #GtkRadioMenuItem
354 * @label: the text for the label
356 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
357 * The new #GtkRadioMenuItem is added to the same group as @group.
359 * Return value: (transfer none): The new #GtkRadioMenuItem
364 gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
369 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
372 list = gtk_radio_menu_item_get_group (group);
374 return gtk_radio_menu_item_new_with_label (list, label);
378 * gtk_radio_menu_item_get_group:
379 * @radio_menu_item: a #GtkRadioMenuItem
381 * Returns the group to which the radio menu item belongs, as a #GList of
382 * #GtkRadioMenuItem. The list belongs to GTK+ and should not be freed.
384 * Returns: (element-type GtkRadioMenuItem) (transfer none): the group
385 * of @radio_menu_item
388 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
390 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
392 return radio_menu_item->priv->group;
396 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
398 GObjectClass *gobject_class;
399 GtkWidgetClass *widget_class;
400 GtkMenuItemClass *menu_item_class;
402 gobject_class = G_OBJECT_CLASS (klass);
403 widget_class = GTK_WIDGET_CLASS (klass);
404 menu_item_class = GTK_MENU_ITEM_CLASS (klass);
406 gobject_class->set_property = gtk_radio_menu_item_set_property;
407 gobject_class->get_property = gtk_radio_menu_item_get_property;
409 widget_class->destroy = gtk_radio_menu_item_destroy;
411 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_RADIO_MENU_ITEM_ACCESSIBLE);
413 menu_item_class->activate = gtk_radio_menu_item_activate;
416 * GtkRadioMenuItem:group:
418 * The radio menu item whose group this widget belongs to.
422 g_object_class_install_property (gobject_class,
424 g_param_spec_object ("group",
426 P_("The radio menu item whose group this widget belongs to."),
427 GTK_TYPE_RADIO_MENU_ITEM,
428 GTK_PARAM_WRITABLE));
431 * GtkStyle::group-changed:
432 * @style: the object which received the signal
434 * Emitted when the group of radio menu items that a radio menu item belongs
435 * to changes. This is emitted when a radio menu item switches from
436 * being alone to being part of a group of 2 or more menu items, or
437 * vice-versa, and when a button is moved from one group of 2 or
438 * more menu items ton a different one, but not when the composition
439 * of the group that a menu item belongs to changes.
443 group_changed_signal = g_signal_new (I_("group-changed"),
444 G_OBJECT_CLASS_TYPE (gobject_class),
446 G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
448 _gtk_marshal_VOID__VOID,
451 g_type_class_add_private (klass, sizeof (GtkRadioMenuItemPrivate));
455 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
457 GtkRadioMenuItemPrivate *priv;
459 radio_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (radio_menu_item,
460 GTK_TYPE_RADIO_MENU_ITEM,
461 GtkRadioMenuItemPrivate);
462 priv = radio_menu_item->priv;
464 priv->group = g_slist_prepend (NULL, radio_menu_item);
465 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
469 gtk_radio_menu_item_destroy (GtkWidget *widget)
471 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (widget);
472 GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
473 GtkWidget *old_group_singleton = NULL;
474 GtkRadioMenuItem *tmp_menu_item;
476 gboolean was_in_group;
478 was_in_group = priv->group && priv->group->next;
480 priv->group = g_slist_remove (priv->group, radio_menu_item);
481 if (priv->group && !priv->group->next)
482 old_group_singleton = priv->group->data;
484 tmp_list = priv->group;
488 tmp_menu_item = tmp_list->data;
489 tmp_list = tmp_list->next;
491 tmp_menu_item->priv->group = priv->group;
494 /* this radio menu item is no longer in the group */
497 if (old_group_singleton)
498 g_signal_emit (old_group_singleton, group_changed_signal, 0);
500 g_signal_emit (radio_menu_item, group_changed_signal, 0);
502 GTK_WIDGET_CLASS (gtk_radio_menu_item_parent_class)->destroy (widget);
506 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
508 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
509 GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
510 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
511 GtkCheckMenuItem *tmp_menu_item;
517 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
518 if (action && gtk_menu_item_get_submenu (menu_item) == NULL)
519 gtk_action_activate (action);
523 active = gtk_check_menu_item_get_active (check_menu_item);
526 tmp_menu_item = NULL;
527 tmp_list = priv->group;
531 tmp_menu_item = tmp_list->data;
532 tmp_list = tmp_list->next;
534 if (gtk_check_menu_item_get_active (tmp_menu_item) &&
535 tmp_menu_item != check_menu_item)
538 tmp_menu_item = NULL;
544 _gtk_check_menu_item_set_active (check_menu_item, !active);
550 _gtk_check_menu_item_set_active (check_menu_item, !active);
552 tmp_list = priv->group;
555 tmp_menu_item = tmp_list->data;
556 tmp_list = tmp_list->next;
558 if (gtk_check_menu_item_get_active (tmp_menu_item) &&
559 tmp_menu_item != check_menu_item)
561 gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
569 gtk_check_menu_item_toggled (check_menu_item);
572 gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));