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"
36 struct _GtkRadioMenuItemPrivate
47 static void gtk_radio_menu_item_destroy (GtkWidget *widget);
48 static void gtk_radio_menu_item_activate (GtkMenuItem *menu_item);
49 static void gtk_radio_menu_item_set_property (GObject *object,
53 static void gtk_radio_menu_item_get_property (GObject *object,
58 static guint group_changed_signal = 0;
60 G_DEFINE_TYPE (GtkRadioMenuItem, gtk_radio_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
63 gtk_radio_menu_item_new (GSList *group)
65 GtkRadioMenuItem *radio_menu_item;
67 radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
69 gtk_radio_menu_item_set_group (radio_menu_item, group);
71 return GTK_WIDGET (radio_menu_item);
75 gtk_radio_menu_item_set_property (GObject *object,
80 GtkRadioMenuItem *radio_menu_item;
82 radio_menu_item = GTK_RADIO_MENU_ITEM (object);
89 if (G_VALUE_HOLDS_OBJECT (value))
90 slist = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
93 gtk_radio_menu_item_set_group (radio_menu_item, slist);
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102 gtk_radio_menu_item_get_property (GObject *object,
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
119 GtkRadioMenuItemPrivate *priv;
120 GtkWidget *old_group_singleton = NULL;
121 GtkWidget *new_group_singleton = NULL;
123 g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
124 g_return_if_fail (!g_slist_find (group, radio_menu_item));
126 priv = radio_menu_item->priv;
132 priv->group = g_slist_remove (priv->group, radio_menu_item);
134 if (priv->group && !priv->group->next)
135 old_group_singleton = g_object_ref (priv->group->data);
137 for (slist = priv->group; slist; slist = slist->next)
139 GtkRadioMenuItem *tmp_item;
141 tmp_item = slist->data;
143 tmp_item->priv->group = priv->group;
147 if (group && !group->next)
148 new_group_singleton = g_object_ref (group->data);
150 priv->group = g_slist_prepend (group, radio_menu_item);
156 for (slist = group; slist; slist = slist->next)
158 GtkRadioMenuItem *tmp_item;
160 tmp_item = slist->data;
162 tmp_item->priv->group = priv->group;
167 _gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
168 /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
172 g_object_ref (radio_menu_item);
174 g_object_notify (G_OBJECT (radio_menu_item), "group");
175 g_signal_emit (radio_menu_item, group_changed_signal, 0);
176 if (old_group_singleton)
178 g_signal_emit (old_group_singleton, group_changed_signal, 0);
179 g_object_unref (old_group_singleton);
181 if (new_group_singleton)
183 g_signal_emit (new_group_singleton, group_changed_signal, 0);
184 g_object_unref (new_group_singleton);
187 g_object_unref (radio_menu_item);
192 * gtk_radio_menu_item_new_with_label:
193 * @group: (element-type GtkRadioMenuItem) (transfer full):
194 * @label: the text for the label
196 * Creates a new #GtkRadioMenuItem whose child is a simple #GtkLabel.
198 * Returns: (transfer none): A new #GtkRadioMenuItem
201 gtk_radio_menu_item_new_with_label (GSList *group,
204 GtkWidget *radio_menu_item;
205 GtkWidget *accel_label;
207 radio_menu_item = gtk_radio_menu_item_new (group);
208 accel_label = gtk_accel_label_new (label);
209 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
210 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
211 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
212 gtk_widget_show (accel_label);
214 return radio_menu_item;
219 * gtk_radio_menu_item_new_with_mnemonic:
220 * @group: group the radio menu item is inside
221 * @label: the text of the button, with an underscore in front of the
223 * @returns: a new #GtkRadioMenuItem
225 * Creates a new #GtkRadioMenuItem containing a label. The label
226 * will be created using gtk_label_new_with_mnemonic(), so underscores
227 * in @label indicate the mnemonic for the menu item.
230 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
233 GtkWidget *radio_menu_item;
234 GtkWidget *accel_label;
236 radio_menu_item = gtk_radio_menu_item_new (group);
237 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
238 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
239 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
241 gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
242 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
243 gtk_widget_show (accel_label);
245 return radio_menu_item;
249 * gtk_radio_menu_item_new_from_widget:
250 * @group: An existing #GtkRadioMenuItem
252 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
254 * Return value: (transfer full): The new #GtkRadioMenuItem
259 gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
263 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
266 list = gtk_radio_menu_item_get_group (group);
268 return gtk_radio_menu_item_new (list);
272 * gtk_radio_menu_item_new_with_mnemonic_from_widget:
273 * @group: An existing #GtkRadioMenuItem
274 * @label: the text of the button, with an underscore in front of the
277 * Creates a new GtkRadioMenuItem containing a label. The label will be
278 * created using gtk_label_new_with_mnemonic(), so underscores in label
279 * indicate the mnemonic for the menu item.
281 * The new #GtkRadioMenuItem is added to the same group as @group.
283 * Return value: (transfer full): The new #GtkRadioMenuItem
288 gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
293 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
296 list = gtk_radio_menu_item_get_group (group);
298 return gtk_radio_menu_item_new_with_mnemonic (list, label);
302 * gtk_radio_menu_item_new_with_label_from_widget:
303 * @group: an existing #GtkRadioMenuItem
304 * @label: the text for the label
306 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
307 * The new #GtkRadioMenuItem is added to the same group as @group.
309 * Return value: (transfer full): The new #GtkRadioMenuItem
314 gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
319 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
322 list = gtk_radio_menu_item_get_group (group);
324 return gtk_radio_menu_item_new_with_label (list, label);
328 * gtk_radio_menu_item_get_group:
329 * @radio_menu_item: a #GtkRadioMenuItem
331 * Returns the group to which the radio menu item belongs, as a #GList of
332 * #GtkRadioMenuItem. The list belongs to GTK+ and should not be freed.
334 * Returns: (transfer none): the group of @radio_menu_item
337 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
339 g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
341 return radio_menu_item->priv->group;
346 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
348 GObjectClass *gobject_class;
349 GtkWidgetClass *widget_class;
350 GtkMenuItemClass *menu_item_class;
352 gobject_class = G_OBJECT_CLASS (klass);
353 widget_class = GTK_WIDGET_CLASS (klass);
354 menu_item_class = GTK_MENU_ITEM_CLASS (klass);
356 gobject_class->set_property = gtk_radio_menu_item_set_property;
357 gobject_class->get_property = gtk_radio_menu_item_get_property;
359 widget_class->destroy = gtk_radio_menu_item_destroy;
361 menu_item_class->activate = gtk_radio_menu_item_activate;
364 * GtkRadioMenuItem:group:
366 * The radio menu item whose group this widget belongs to.
370 g_object_class_install_property (gobject_class,
372 g_param_spec_object ("group",
374 P_("The radio menu item whose group this widget belongs to."),
375 GTK_TYPE_RADIO_MENU_ITEM,
376 GTK_PARAM_WRITABLE));
379 * GtkStyle::group-changed:
380 * @style: the object which received the signal
382 * Emitted when the group of radio menu items that a radio menu item belongs
383 * to changes. This is emitted when a radio menu item switches from
384 * being alone to being part of a group of 2 or more menu items, or
385 * vice-versa, and when a button is moved from one group of 2 or
386 * more menu items ton a different one, but not when the composition
387 * of the group that a menu item belongs to changes.
391 group_changed_signal = g_signal_new (I_("group-changed"),
392 G_OBJECT_CLASS_TYPE (gobject_class),
394 G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
396 _gtk_marshal_VOID__VOID,
399 g_type_class_add_private (klass, sizeof (GtkRadioMenuItemPrivate));
403 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
405 GtkRadioMenuItemPrivate *priv;
407 radio_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (radio_menu_item,
408 GTK_TYPE_RADIO_MENU_ITEM,
409 GtkRadioMenuItemPrivate);
410 priv = radio_menu_item->priv;
412 priv->group = g_slist_prepend (NULL, radio_menu_item);
413 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
417 gtk_radio_menu_item_destroy (GtkWidget *widget)
419 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (widget);
420 GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
421 GtkWidget *old_group_singleton = NULL;
422 GtkRadioMenuItem *tmp_menu_item;
424 gboolean was_in_group;
426 was_in_group = priv->group && priv->group->next;
428 priv->group = g_slist_remove (priv->group, radio_menu_item);
429 if (priv->group && !priv->group->next)
430 old_group_singleton = priv->group->data;
432 tmp_list = priv->group;
436 tmp_menu_item = tmp_list->data;
437 tmp_list = tmp_list->next;
439 tmp_menu_item->priv->group = priv->group;
442 /* this radio menu item is no longer in the group */
445 if (old_group_singleton)
446 g_signal_emit (old_group_singleton, group_changed_signal, 0);
448 g_signal_emit (radio_menu_item, group_changed_signal, 0);
450 GTK_WIDGET_CLASS (gtk_radio_menu_item_parent_class)->destroy (widget);
454 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
456 GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
457 GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
458 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
459 GtkCheckMenuItem *tmp_menu_item;
465 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
466 if (action && gtk_menu_item_get_submenu (menu_item) == NULL)
467 gtk_action_activate (action);
471 active = gtk_check_menu_item_get_active (check_menu_item);
474 tmp_menu_item = NULL;
475 tmp_list = priv->group;
479 tmp_menu_item = tmp_list->data;
480 tmp_list = tmp_list->next;
482 if (gtk_check_menu_item_get_active (tmp_menu_item) &&
483 tmp_menu_item != check_menu_item)
486 tmp_menu_item = NULL;
492 _gtk_check_menu_item_set_active (check_menu_item, !active);
498 _gtk_check_menu_item_set_active (check_menu_item, !active);
500 tmp_list = priv->group;
503 tmp_menu_item = tmp_list->data;
504 tmp_list = tmp_list->next;
506 if (gtk_check_menu_item_get_active (tmp_menu_item) &&
507 tmp_menu_item != check_menu_item)
509 gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
517 gtk_check_menu_item_toggled (check_menu_item);
520 gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));