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: (element-type GtkRadioMenuItem): the group to which the
97 * radio menu item is to be attached
99 * Creates a new #GtkRadioMenuItem.
101 * Returns: a new #GtkRadioMenuItem
104 gtk_radio_menu_item_new (GSList *group)
106 GtkRadioMenuItem *radio_menu_item;
108 radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
110 gtk_radio_menu_item_set_group (radio_menu_item, group);
112 return GTK_WIDGET (radio_menu_item);
116 gtk_radio_menu_item_set_property (GObject *object,
121 GtkRadioMenuItem *radio_menu_item;
123 radio_menu_item = GTK_RADIO_MENU_ITEM (object);
130 if (G_VALUE_HOLDS_OBJECT (value))
131 slist = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
134 gtk_radio_menu_item_set_group (radio_menu_item, slist);
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 gtk_radio_menu_item_get_property (GObject *object,
151 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 * gtk_radio_menu_item_set_group:
158 * @radio_menu_item: a #GtkRadioMenuItem.
159 * @group: (element-type GtkRadioMenuItem): the new group.
161 * Sets the group of a radio menu item, or changes it.
164 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
167 GtkRadioMenuItemPrivate *priv;
168 GtkWidget *old_group_singleton = NULL;
169 GtkWidget *new_group_singleton = NULL;
171 g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
172 g_return_if_fail (!g_slist_find (group, radio_menu_item));
174 priv = radio_menu_item->priv;
180 priv->group = g_slist_remove (priv->group, radio_menu_item);
182 if (priv->group && !priv->group->next)
183 old_group_singleton = g_object_ref (priv->group->data);
185 for (slist = priv->group; slist; slist = slist->next)
187 GtkRadioMenuItem *tmp_item;
189 tmp_item = slist->data;
191 tmp_item->priv->group = priv->group;
195 if (group && !group->next)
196 new_group_singleton = g_object_ref (group->data);
198 priv->group = g_slist_prepend (group, radio_menu_item);
204 for (slist = group; slist; slist = slist->next)
206 GtkRadioMenuItem *tmp_item;
208 tmp_item = slist->data;
210 tmp_item->priv->group = priv->group;
215 _gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
216 /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
220 g_object_ref (radio_menu_item);
222 g_object_notify (G_OBJECT (radio_menu_item), "group");
223 g_signal_emit (radio_menu_item, group_changed_signal, 0);
224 if (old_group_singleton)
226 g_signal_emit (old_group_singleton, group_changed_signal, 0);
227 g_object_unref (old_group_singleton);
229 if (new_group_singleton)
231 g_signal_emit (new_group_singleton, group_changed_signal, 0);
232 g_object_unref (new_group_singleton);
235 g_object_unref (radio_menu_item);
240 * gtk_radio_menu_item_new_with_label:
241 * @group: (element-type GtkRadioMenuItem) (transfer full):
242 * @label: the text for the label
244 * Creates a new #GtkRadioMenuItem whose child is a simple #GtkLabel.
246 * Returns: (transfer none): A new #GtkRadioMenuItem
249 gtk_radio_menu_item_new_with_label (GSList *group,
252 GtkWidget *radio_menu_item;
253 GtkWidget *accel_label;
255 radio_menu_item = gtk_radio_menu_item_new (group);
256 accel_label = gtk_accel_label_new (label);
257 gtk_widget_set_halign (accel_label, GTK_ALIGN_START);
258 gtk_widget_set_valign (accel_label, GTK_ALIGN_CENTER);
259 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
260 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
261 gtk_widget_show (accel_label);
263 return radio_menu_item;
268 * gtk_radio_menu_item_new_with_mnemonic:
269 * @group: (element-type GtkRadioMenuItem): group the radio menu item is inside
270 * @label: the text of the button, with an underscore in front of the
273 * Creates a new #GtkRadioMenuItem containing a label. The label
274 * will be created using gtk_label_new_with_mnemonic(), so underscores
275 * in @label indicate the mnemonic for the menu item.
277 * Returns: a new #GtkRadioMenuItem
280 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
283 GtkWidget *radio_menu_item;
284 GtkWidget *accel_label;
286 radio_menu_item = gtk_radio_menu_item_new (group);
287 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
288 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
289 gtk_widget_set_halign (accel_label, GTK_ALIGN_START);
290 gtk_widget_set_valign (accel_label, GTK_ALIGN_CENTER);
292 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
293 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
294 gtk_widget_show (accel_label);
296 return radio_menu_item;
300 * gtk_radio_menu_item_new_from_widget: (constructor)
301 * @group: An existing #GtkRadioMenuItem
303 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
305 * Return value: (transfer none): The new #GtkRadioMenuItem
310 gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
314 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
317 list = gtk_radio_menu_item_get_group (group);
319 return gtk_radio_menu_item_new (list);
323 * gtk_radio_menu_item_new_with_mnemonic_from_widget: (constructor)
324 * @group: An existing #GtkRadioMenuItem
325 * @label: the text of the button, with an underscore in front of the
328 * Creates a new GtkRadioMenuItem containing a label. The label will be
329 * created using gtk_label_new_with_mnemonic(), so underscores in label
330 * indicate the mnemonic for the menu item.
332 * The new #GtkRadioMenuItem is added to the same group as @group.
334 * Return value: (transfer none): The new #GtkRadioMenuItem
339 gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
344 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
347 list = gtk_radio_menu_item_get_group (group);
349 return gtk_radio_menu_item_new_with_mnemonic (list, label);
353 * gtk_radio_menu_item_new_with_label_from_widget: (constructor)
354 * @group: an existing #GtkRadioMenuItem
355 * @label: the text for the label
357 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
358 * The new #GtkRadioMenuItem is added to the same group as @group.
360 * Return value: (transfer none): The new #GtkRadioMenuItem
365 gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
370 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
373 list = gtk_radio_menu_item_get_group (group);
375 return gtk_radio_menu_item_new_with_label (list, label);
379 * gtk_radio_menu_item_get_group:
380 * @radio_menu_item: a #GtkRadioMenuItem
382 * Returns the group to which the radio menu item belongs, as a #GList of
383 * #GtkRadioMenuItem. The list belongs to GTK+ and should not be freed.
385 * Returns: (element-type GtkRadioMenuItem) (transfer none): the group
386 * of @radio_menu_item
389 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
391 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
393 return radio_menu_item->priv->group;
397 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
399 GObjectClass *gobject_class;
400 GtkWidgetClass *widget_class;
401 GtkMenuItemClass *menu_item_class;
403 gobject_class = G_OBJECT_CLASS (klass);
404 widget_class = GTK_WIDGET_CLASS (klass);
405 menu_item_class = GTK_MENU_ITEM_CLASS (klass);
407 gobject_class->set_property = gtk_radio_menu_item_set_property;
408 gobject_class->get_property = gtk_radio_menu_item_get_property;
410 widget_class->destroy = gtk_radio_menu_item_destroy;
412 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_RADIO_MENU_ITEM_ACCESSIBLE);
414 menu_item_class->activate = gtk_radio_menu_item_activate;
417 * GtkRadioMenuItem:group:
419 * The radio menu item whose group this widget belongs to.
423 g_object_class_install_property (gobject_class,
425 g_param_spec_object ("group",
427 P_("The radio menu item whose group this widget belongs to."),
428 GTK_TYPE_RADIO_MENU_ITEM,
429 GTK_PARAM_WRITABLE));
432 * GtkStyle::group-changed:
433 * @style: the object which received the signal
435 * Emitted when the group of radio menu items that a radio menu item belongs
436 * to changes. This is emitted when a radio menu item switches from
437 * being alone to being part of a group of 2 or more menu items, or
438 * vice-versa, and when a button is moved from one group of 2 or
439 * more menu items ton a different one, but not when the composition
440 * of the group that a menu item belongs to changes.
444 group_changed_signal = g_signal_new (I_("group-changed"),
445 G_OBJECT_CLASS_TYPE (gobject_class),
447 G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
449 _gtk_marshal_VOID__VOID,
452 g_type_class_add_private (klass, sizeof (GtkRadioMenuItemPrivate));
456 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
458 GtkRadioMenuItemPrivate *priv;
460 radio_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (radio_menu_item,
461 GTK_TYPE_RADIO_MENU_ITEM,
462 GtkRadioMenuItemPrivate);
463 priv = radio_menu_item->priv;
465 priv->group = g_slist_prepend (NULL, radio_menu_item);
466 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
470 gtk_radio_menu_item_destroy (GtkWidget *widget)
472 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (widget);
473 GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
474 GtkWidget *old_group_singleton = NULL;
475 GtkRadioMenuItem *tmp_menu_item;
477 gboolean was_in_group;
479 was_in_group = priv->group && priv->group->next;
481 priv->group = g_slist_remove (priv->group, radio_menu_item);
482 if (priv->group && !priv->group->next)
483 old_group_singleton = priv->group->data;
485 tmp_list = priv->group;
489 tmp_menu_item = tmp_list->data;
490 tmp_list = tmp_list->next;
492 tmp_menu_item->priv->group = priv->group;
495 /* this radio menu item is no longer in the group */
498 if (old_group_singleton)
499 g_signal_emit (old_group_singleton, group_changed_signal, 0);
501 g_signal_emit (radio_menu_item, group_changed_signal, 0);
503 GTK_WIDGET_CLASS (gtk_radio_menu_item_parent_class)->destroy (widget);
507 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
509 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
510 GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
511 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
512 GtkCheckMenuItem *tmp_menu_item;
518 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
519 if (action && gtk_menu_item_get_submenu (menu_item) == NULL)
520 gtk_action_activate (action);
524 active = gtk_check_menu_item_get_active (check_menu_item);
527 tmp_menu_item = NULL;
528 tmp_list = priv->group;
532 tmp_menu_item = tmp_list->data;
533 tmp_list = tmp_list->next;
535 if (gtk_check_menu_item_get_active (tmp_menu_item) &&
536 tmp_menu_item != check_menu_item)
539 tmp_menu_item = NULL;
545 _gtk_check_menu_item_set_active (check_menu_item, !active);
551 _gtk_check_menu_item_set_active (check_menu_item, !active);
553 tmp_list = priv->group;
556 tmp_menu_item = tmp_list->data;
557 tmp_list = tmp_list->next;
559 if (gtk_check_menu_item_get_active (tmp_menu_item) &&
560 tmp_menu_item != check_menu_item)
562 gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
570 gtk_check_menu_item_toggled (check_menu_item);
573 gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));