2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998, 1999 Red Hat, Inc.
6 * This Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * Author: James Henstridge <james@daa.com.au>
25 * Modified by the GTK+ Team and others 2003. See the AUTHORS
26 * file for a list of people on the GTK+ Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gtkradioaction.h"
34 #include "gtkradiomenuitem.h"
35 #include "gtktoggletoolbutton.h"
37 #include "gtkprivate.h"
40 * SECTION:gtkradioaction
41 * @Short_description: An action of which only one in a group can be active
42 * @Title: GtkRadioAction
44 * A #GtkRadioAction is similar to #GtkRadioMenuItem. A number of radio
45 * actions can be linked together so that only one may be active at any
50 struct _GtkRadioActionPrivate
70 static void gtk_radio_action_finalize (GObject *object);
71 static void gtk_radio_action_set_property (GObject *object,
75 static void gtk_radio_action_get_property (GObject *object,
79 static void gtk_radio_action_activate (GtkAction *action);
80 static GtkWidget *create_menu_item (GtkAction *action);
83 G_DEFINE_TYPE (GtkRadioAction, gtk_radio_action, GTK_TYPE_TOGGLE_ACTION)
85 static guint radio_action_signals[LAST_SIGNAL] = { 0 };
88 gtk_radio_action_class_init (GtkRadioActionClass *klass)
90 GObjectClass *gobject_class;
91 GtkActionClass *action_class;
93 gobject_class = G_OBJECT_CLASS (klass);
94 action_class = GTK_ACTION_CLASS (klass);
96 gobject_class->finalize = gtk_radio_action_finalize;
97 gobject_class->set_property = gtk_radio_action_set_property;
98 gobject_class->get_property = gtk_radio_action_get_property;
100 action_class->activate = gtk_radio_action_activate;
102 action_class->create_menu_item = create_menu_item;
105 * GtkRadioAction:value:
107 * The value is an arbitrary integer which can be used as a
108 * convenient way to determine which action in the group is
109 * currently active in an ::activate or ::changed signal handler.
110 * See gtk_radio_action_get_current_value() and #GtkRadioActionEntry
111 * for convenient ways to get and set this property.
115 g_object_class_install_property (gobject_class,
117 g_param_spec_int ("value",
119 P_("The value returned by gtk_radio_action_get_current_value() when this action is the current action of its group."),
123 GTK_PARAM_READWRITE));
126 * GtkRadioAction:group:
128 * Sets a new group for a radio action.
132 g_object_class_install_property (gobject_class,
134 g_param_spec_object ("group",
136 P_("The radio action whose group this action belongs to."),
137 GTK_TYPE_RADIO_ACTION,
138 GTK_PARAM_WRITABLE));
141 * GtkRadioAction:current-value:
143 * The value property of the currently active member of the group to which
144 * this action belongs.
148 g_object_class_install_property (gobject_class,
150 g_param_spec_int ("current-value",
151 P_("The current value"),
152 P_("The value property of the currently active member of the group to which this action belongs."),
156 GTK_PARAM_READWRITE));
159 * GtkRadioAction::changed:
160 * @action: the action on which the signal is emitted
161 * @current: the member of @action<!-- -->s group which has just been activated
163 * The ::changed signal is emitted on every member of a radio group when the
164 * active member is changed. The signal gets emitted after the ::activate signals
165 * for the previous and current active members.
169 radio_action_signals[CHANGED] =
170 g_signal_new (I_("changed"),
171 G_OBJECT_CLASS_TYPE (klass),
172 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
173 G_STRUCT_OFFSET (GtkRadioActionClass, changed), NULL, NULL,
174 g_cclosure_marshal_VOID__OBJECT,
175 G_TYPE_NONE, 1, GTK_TYPE_RADIO_ACTION);
177 g_type_class_add_private (gobject_class, sizeof (GtkRadioActionPrivate));
181 gtk_radio_action_init (GtkRadioAction *action)
183 action->private_data = G_TYPE_INSTANCE_GET_PRIVATE (action,
184 GTK_TYPE_RADIO_ACTION,
185 GtkRadioActionPrivate);
187 action->private_data->group = g_slist_prepend (NULL, action);
188 action->private_data->value = 0;
190 gtk_toggle_action_set_draw_as_radio (GTK_TOGGLE_ACTION (action), TRUE);
194 * gtk_radio_action_new:
195 * @name: A unique name for the action
196 * @label: (allow-none): The label displayed in menu items and on buttons,
198 * @tooltip: (allow-none): A tooltip for this action, or %NULL
199 * @stock_id: (allow-none): The stock icon to display in widgets representing
200 * this action, or %NULL
201 * @value: The value which gtk_radio_action_get_current_value() should
202 * return if this action is selected.
204 * Creates a new #GtkRadioAction object. To add the action to
205 * a #GtkActionGroup and set the accelerator for the action,
206 * call gtk_action_group_add_action_with_accel().
208 * Return value: a new #GtkRadioAction
213 gtk_radio_action_new (const gchar *name,
215 const gchar *tooltip,
216 const gchar *stock_id,
219 g_return_val_if_fail (name != NULL, NULL);
221 return g_object_new (GTK_TYPE_RADIO_ACTION,
225 "stock-id", stock_id,
231 gtk_radio_action_finalize (GObject *object)
233 GtkRadioAction *action;
236 action = GTK_RADIO_ACTION (object);
238 action->private_data->group = g_slist_remove (action->private_data->group, action);
240 tmp_list = action->private_data->group;
244 GtkRadioAction *tmp_action = tmp_list->data;
246 tmp_list = tmp_list->next;
247 tmp_action->private_data->group = action->private_data->group;
250 G_OBJECT_CLASS (gtk_radio_action_parent_class)->finalize (object);
254 gtk_radio_action_set_property (GObject *object,
259 GtkRadioAction *radio_action;
261 radio_action = GTK_RADIO_ACTION (object);
266 radio_action->private_data->value = g_value_get_int (value);
271 GSList *slist = NULL;
273 if (G_VALUE_HOLDS_OBJECT (value))
275 arg = GTK_RADIO_ACTION (g_value_get_object (value));
277 slist = gtk_radio_action_get_group (arg);
278 gtk_radio_action_set_group (radio_action, slist);
282 case PROP_CURRENT_VALUE:
283 gtk_radio_action_set_current_value (radio_action,
284 g_value_get_int (value));
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
293 gtk_radio_action_get_property (GObject *object,
298 GtkRadioAction *radio_action;
300 radio_action = GTK_RADIO_ACTION (object);
305 g_value_set_int (value, radio_action->private_data->value);
307 case PROP_CURRENT_VALUE:
308 g_value_set_int (value,
309 gtk_radio_action_get_current_value (radio_action));
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318 gtk_radio_action_activate (GtkAction *action)
320 GtkRadioAction *radio_action;
321 GtkToggleAction *toggle_action;
322 GtkToggleAction *tmp_action;
326 radio_action = GTK_RADIO_ACTION (action);
327 toggle_action = GTK_TOGGLE_ACTION (action);
329 active = gtk_toggle_action_get_active (toggle_action);
332 tmp_list = radio_action->private_data->group;
336 tmp_action = tmp_list->data;
337 tmp_list = tmp_list->next;
339 if (gtk_toggle_action_get_active (tmp_action) &&
340 (tmp_action != toggle_action))
342 _gtk_toggle_action_set_active (toggle_action, !active);
347 g_object_notify (G_OBJECT (action), "active");
351 _gtk_toggle_action_set_active (toggle_action, !active);
352 g_object_notify (G_OBJECT (action), "active");
354 tmp_list = radio_action->private_data->group;
357 tmp_action = tmp_list->data;
358 tmp_list = tmp_list->next;
360 if (gtk_toggle_action_get_active (tmp_action) &&
361 (tmp_action != toggle_action))
363 _gtk_action_emit_activate (GTK_ACTION (tmp_action));
368 tmp_list = radio_action->private_data->group;
371 tmp_action = tmp_list->data;
372 tmp_list = tmp_list->next;
374 g_object_notify (G_OBJECT (tmp_action), "current-value");
376 g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
380 gtk_toggle_action_toggled (toggle_action);
384 create_menu_item (GtkAction *action)
386 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
387 "draw-as-radio", TRUE,
392 * gtk_radio_action_get_group:
393 * @action: the action object
395 * Returns the list representing the radio group for this object.
396 * Note that the returned list is only valid until the next change
399 * A common way to set up a group of radio group is the following:
401 * GSList *group = NULL;
402 * GtkRadioAction *action;
404 * while (/* more actions to add */)
406 * action = gtk_radio_action_new (...);
408 * gtk_radio_action_set_group (action, group);
409 * group = gtk_radio_action_get_group (action);
413 * Returns: (element-type GtkRadioAction) (transfer none): the list representing the radio group for this object
418 gtk_radio_action_get_group (GtkRadioAction *action)
420 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
422 return action->private_data->group;
426 * gtk_radio_action_set_group:
427 * @action: the action object
428 * @group: (element-type GtkRadioAction): a list representing a radio group
430 * Sets the radio group for the radio action object.
435 gtk_radio_action_set_group (GtkRadioAction *action,
438 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
439 g_return_if_fail (!g_slist_find (group, action));
441 if (action->private_data->group)
445 action->private_data->group = g_slist_remove (action->private_data->group, action);
447 for (slist = action->private_data->group; slist; slist = slist->next)
449 GtkRadioAction *tmp_action = slist->data;
451 tmp_action->private_data->group = action->private_data->group;
455 action->private_data->group = g_slist_prepend (group, action);
461 for (slist = action->private_data->group; slist; slist = slist->next)
463 GtkRadioAction *tmp_action = slist->data;
465 tmp_action->private_data->group = action->private_data->group;
470 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
475 * gtk_radio_action_join_group:
476 * @action: the action object
477 * @group_source: (allow-none): a radio action object whos group we are
478 * joining, or %NULL to remove the radio action from its group
480 * Joins a radio action object to the group of another radio action object.
482 * Use this in language bindings instead of the gtk_radio_action_get_group()
483 * and gtk_radio_action_set_group() methods
485 * A common way to set up a group of radio actions is the following:
487 * GtkRadioAction *action;
488 * GtkRadioAction *last_action;
490 * while (/* more actions to add */)
492 * action = gtk_radio_action_new (...);
494 * gtk_radio_action_join_group (action, last_action);
495 * last_action = action;
502 gtk_radio_action_join_group (GtkRadioAction *action,
503 GtkRadioAction *group_source)
505 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
506 g_return_if_fail (group_source == NULL || GTK_IS_RADIO_ACTION (group_source));
511 group = gtk_radio_action_get_group (group_source);
515 /* if we are not already part of a group we need to set up a new one
516 and then get the newly created group */
517 gtk_radio_action_set_group (group_source, NULL);
518 group = gtk_radio_action_get_group (group_source);
521 gtk_radio_action_set_group (action, group);
525 gtk_radio_action_set_group (action, NULL);
530 * gtk_radio_action_get_current_value:
531 * @action: a #GtkRadioAction
533 * Obtains the value property of the currently active member of
534 * the group to which @action belongs.
536 * Return value: The value of the currently active group member
541 gtk_radio_action_get_current_value (GtkRadioAction *action)
545 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);
547 if (action->private_data->group)
549 for (slist = action->private_data->group; slist; slist = slist->next)
551 GtkToggleAction *toggle_action = slist->data;
553 if (gtk_toggle_action_get_active (toggle_action))
554 return GTK_RADIO_ACTION (toggle_action)->private_data->value;
558 return action->private_data->value;
562 * gtk_radio_action_set_current_value:
563 * @action: a #GtkRadioAction
564 * @current_value: the new value
566 * Sets the currently active group member to the member with value
567 * property @current_value.
572 gtk_radio_action_set_current_value (GtkRadioAction *action,
577 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
579 if (action->private_data->group)
581 for (slist = action->private_data->group; slist; slist = slist->next)
583 GtkRadioAction *radio_action = slist->data;
585 if (radio_action->private_data->value == current_value)
587 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action),
594 if (action->private_data->value == current_value)
595 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
597 g_warning ("Radio group does not contain an action with value '%d'",