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"
34 #include "a11y/gtkradiomenuitemaccessible.h"
37 * SECTION:gtkradiomenuitem
38 * @Short_description: A choice from multiple check menu items
39 * @Title: GtkRadioMenuItem
40 * @See_also: #GtkMenuItem, #GtkCheckMenuItem
42 * A radio menu item is a check menu item that belongs to a group. At each
43 * instant exactly one of the radio menu items from a group is selected.
45 * The group list does not need to be freed, as each #GtkRadioMenuItem will
46 * remove itself and its list item when it is destroyed.
48 * The correct way to create a group of radio menu items is approximatively
52 * <title>How to create a group of radio menu items.</title>
54 * GSList *group = NULL;
58 * for (i = 0; i < 5; i++)
60 * item = gtk_radio_menu_item_new_with_label (group, "This is an example");
61 * group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
63 * gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
70 struct _GtkRadioMenuItemPrivate
81 static void gtk_radio_menu_item_destroy (GtkWidget *widget);
82 static void gtk_radio_menu_item_activate (GtkMenuItem *menu_item);
83 static void gtk_radio_menu_item_set_property (GObject *object,
87 static void gtk_radio_menu_item_get_property (GObject *object,
92 static guint group_changed_signal = 0;
94 G_DEFINE_TYPE (GtkRadioMenuItem, gtk_radio_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
97 * gtk_radio_menu_item_new:
98 * @group: the group to which the radio menu item is to be attached
100 * Creates a new #GtkRadioMenuItem.
102 * Returns: a new #GtkRadioMenuItem
105 gtk_radio_menu_item_new (GSList *group)
107 GtkRadioMenuItem *radio_menu_item;
109 radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
111 gtk_radio_menu_item_set_group (radio_menu_item, group);
113 return GTK_WIDGET (radio_menu_item);
117 gtk_radio_menu_item_set_property (GObject *object,
122 GtkRadioMenuItem *radio_menu_item;
124 radio_menu_item = GTK_RADIO_MENU_ITEM (object);
131 if (G_VALUE_HOLDS_OBJECT (value))
132 slist = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
135 gtk_radio_menu_item_set_group (radio_menu_item, slist);
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144 gtk_radio_menu_item_get_property (GObject *object,
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158 * gtk_radio_menu_item_set_group:
159 * @radio_menu_item: a #GtkRadioMenuItem.
160 * @group: the new group.
162 * Sets the group of a radio menu item, or changes it.
165 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
168 GtkRadioMenuItemPrivate *priv;
169 GtkWidget *old_group_singleton = NULL;
170 GtkWidget *new_group_singleton = NULL;
172 g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
173 g_return_if_fail (!g_slist_find (group, radio_menu_item));
175 priv = radio_menu_item->priv;
181 priv->group = g_slist_remove (priv->group, radio_menu_item);
183 if (priv->group && !priv->group->next)
184 old_group_singleton = g_object_ref (priv->group->data);
186 for (slist = priv->group; slist; slist = slist->next)
188 GtkRadioMenuItem *tmp_item;
190 tmp_item = slist->data;
192 tmp_item->priv->group = priv->group;
196 if (group && !group->next)
197 new_group_singleton = g_object_ref (group->data);
199 priv->group = g_slist_prepend (group, radio_menu_item);
205 for (slist = group; slist; slist = slist->next)
207 GtkRadioMenuItem *tmp_item;
209 tmp_item = slist->data;
211 tmp_item->priv->group = priv->group;
216 _gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
217 /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
221 g_object_ref (radio_menu_item);
223 g_object_notify (G_OBJECT (radio_menu_item), "group");
224 g_signal_emit (radio_menu_item, group_changed_signal, 0);
225 if (old_group_singleton)
227 g_signal_emit (old_group_singleton, group_changed_signal, 0);
228 g_object_unref (old_group_singleton);
230 if (new_group_singleton)
232 g_signal_emit (new_group_singleton, group_changed_signal, 0);
233 g_object_unref (new_group_singleton);
236 g_object_unref (radio_menu_item);
241 * gtk_radio_menu_item_new_with_label:
242 * @group: (element-type GtkRadioMenuItem) (transfer full):
243 * @label: the text for the label
245 * Creates a new #GtkRadioMenuItem whose child is a simple #GtkLabel.
247 * Returns: (transfer none): A new #GtkRadioMenuItem
250 gtk_radio_menu_item_new_with_label (GSList *group,
253 GtkWidget *radio_menu_item;
254 GtkWidget *accel_label;
256 radio_menu_item = gtk_radio_menu_item_new (group);
257 accel_label = gtk_accel_label_new (label);
258 gtk_widget_set_halign (accel_label, GTK_ALIGN_START);
259 gtk_widget_set_valign (accel_label, GTK_ALIGN_CENTER);
260 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
261 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
262 gtk_widget_show (accel_label);
264 return radio_menu_item;
269 * gtk_radio_menu_item_new_with_mnemonic:
270 * @group: group the radio menu item is inside
271 * @label: the text of the button, with an underscore in front of the
274 * Creates a new #GtkRadioMenuItem containing a label. The label
275 * will be created using gtk_label_new_with_mnemonic(), so underscores
276 * in @label indicate the mnemonic for the menu item.
278 * Returns: a new #GtkRadioMenuItem
281 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
284 GtkWidget *radio_menu_item;
285 GtkWidget *accel_label;
287 radio_menu_item = gtk_radio_menu_item_new (group);
288 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
289 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
290 gtk_widget_set_halign (accel_label, GTK_ALIGN_START);
291 gtk_widget_set_valign (accel_label, GTK_ALIGN_CENTER);
293 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
294 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
295 gtk_widget_show (accel_label);
297 return radio_menu_item;
301 * gtk_radio_menu_item_new_from_widget: (constructor)
302 * @group: An existing #GtkRadioMenuItem
304 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
306 * Return value: (transfer none): The new #GtkRadioMenuItem
311 gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
315 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
318 list = gtk_radio_menu_item_get_group (group);
320 return gtk_radio_menu_item_new (list);
324 * gtk_radio_menu_item_new_with_mnemonic_from_widget: (constructor)
325 * @group: An existing #GtkRadioMenuItem
326 * @label: the text of the button, with an underscore in front of the
329 * Creates a new GtkRadioMenuItem containing a label. The label will be
330 * created using gtk_label_new_with_mnemonic(), so underscores in label
331 * indicate the mnemonic for the menu item.
333 * The new #GtkRadioMenuItem is added to the same group as @group.
335 * Return value: (transfer none): The new #GtkRadioMenuItem
340 gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
345 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
348 list = gtk_radio_menu_item_get_group (group);
350 return gtk_radio_menu_item_new_with_mnemonic (list, label);
354 * gtk_radio_menu_item_new_with_label_from_widget: (constructor)
355 * @group: an existing #GtkRadioMenuItem
356 * @label: the text for the label
358 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
359 * The new #GtkRadioMenuItem is added to the same group as @group.
361 * Return value: (transfer none): The new #GtkRadioMenuItem
366 gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
371 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
374 list = gtk_radio_menu_item_get_group (group);
376 return gtk_radio_menu_item_new_with_label (list, label);
380 * gtk_radio_menu_item_get_group:
381 * @radio_menu_item: a #GtkRadioMenuItem
383 * Returns the group to which the radio menu item belongs, as a #GList of
384 * #GtkRadioMenuItem. The list belongs to GTK+ and should not be freed.
386 * Returns: (element-type GtkRadioMenuItem) (transfer none): the group
387 * of @radio_menu_item
390 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
392 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
394 return radio_menu_item->priv->group;
398 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
400 GObjectClass *gobject_class;
401 GtkWidgetClass *widget_class;
402 GtkMenuItemClass *menu_item_class;
404 gobject_class = G_OBJECT_CLASS (klass);
405 widget_class = GTK_WIDGET_CLASS (klass);
406 menu_item_class = GTK_MENU_ITEM_CLASS (klass);
408 gobject_class->set_property = gtk_radio_menu_item_set_property;
409 gobject_class->get_property = gtk_radio_menu_item_get_property;
411 widget_class->destroy = gtk_radio_menu_item_destroy;
413 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_RADIO_MENU_ITEM_ACCESSIBLE);
415 menu_item_class->activate = gtk_radio_menu_item_activate;
418 * GtkRadioMenuItem:group:
420 * The radio menu item whose group this widget belongs to.
424 g_object_class_install_property (gobject_class,
426 g_param_spec_object ("group",
428 P_("The radio menu item whose group this widget belongs to."),
429 GTK_TYPE_RADIO_MENU_ITEM,
430 GTK_PARAM_WRITABLE));
433 * GtkStyle::group-changed:
434 * @style: the object which received the signal
436 * Emitted when the group of radio menu items that a radio menu item belongs
437 * to changes. This is emitted when a radio menu item switches from
438 * being alone to being part of a group of 2 or more menu items, or
439 * vice-versa, and when a button is moved from one group of 2 or
440 * more menu items ton a different one, but not when the composition
441 * of the group that a menu item belongs to changes.
445 group_changed_signal = g_signal_new (I_("group-changed"),
446 G_OBJECT_CLASS_TYPE (gobject_class),
448 G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
450 _gtk_marshal_VOID__VOID,
453 g_type_class_add_private (klass, sizeof (GtkRadioMenuItemPrivate));
457 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
459 GtkRadioMenuItemPrivate *priv;
461 radio_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (radio_menu_item,
462 GTK_TYPE_RADIO_MENU_ITEM,
463 GtkRadioMenuItemPrivate);
464 priv = radio_menu_item->priv;
466 priv->group = g_slist_prepend (NULL, radio_menu_item);
467 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
471 gtk_radio_menu_item_destroy (GtkWidget *widget)
473 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (widget);
474 GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
475 GtkWidget *old_group_singleton = NULL;
476 GtkRadioMenuItem *tmp_menu_item;
478 gboolean was_in_group;
480 was_in_group = priv->group && priv->group->next;
482 priv->group = g_slist_remove (priv->group, radio_menu_item);
483 if (priv->group && !priv->group->next)
484 old_group_singleton = priv->group->data;
486 tmp_list = priv->group;
490 tmp_menu_item = tmp_list->data;
491 tmp_list = tmp_list->next;
493 tmp_menu_item->priv->group = priv->group;
496 /* this radio menu item is no longer in the group */
499 if (old_group_singleton)
500 g_signal_emit (old_group_singleton, group_changed_signal, 0);
502 g_signal_emit (radio_menu_item, group_changed_signal, 0);
504 GTK_WIDGET_CLASS (gtk_radio_menu_item_parent_class)->destroy (widget);
508 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
510 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
511 GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
512 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
513 GtkCheckMenuItem *tmp_menu_item;
519 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
520 if (action && gtk_menu_item_get_submenu (menu_item) == NULL)
521 gtk_action_activate (action);
525 active = gtk_check_menu_item_get_active (check_menu_item);
528 tmp_menu_item = NULL;
529 tmp_list = priv->group;
533 tmp_menu_item = tmp_list->data;
534 tmp_list = tmp_list->next;
536 if (gtk_check_menu_item_get_active (tmp_menu_item) &&
537 tmp_menu_item != check_menu_item)
540 tmp_menu_item = NULL;
546 _gtk_check_menu_item_set_active (check_menu_item, !active);
552 _gtk_check_menu_item_set_active (check_menu_item, !active);
554 tmp_list = priv->group;
557 tmp_menu_item = tmp_list->data;
558 tmp_list = tmp_list->next;
560 if (gtk_check_menu_item_get_active (tmp_menu_item) &&
561 tmp_menu_item != check_menu_item)
563 gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
571 gtk_check_menu_item_toggled (check_menu_item);
574 gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));