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 this library. If not, see <http://www.gnu.org/licenses/>.
21 * Author: James Henstridge <james@daa.com.au>
23 * Modified by the GTK+ Team and others 2003. See the AUTHORS
24 * file for a list of people on the GTK+ Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkradioaction.h"
32 #include "gtkradiomenuitem.h"
33 #include "gtktoggletoolbutton.h"
35 #include "gtkprivate.h"
38 * SECTION:gtkradioaction
39 * @Short_description: An action of which only one in a group can be active
40 * @Title: GtkRadioAction
42 * A #GtkRadioAction is similar to #GtkRadioMenuItem. A number of radio
43 * actions can be linked together so that only one may be active at any
48 struct _GtkRadioActionPrivate
68 static void gtk_radio_action_finalize (GObject *object);
69 static void gtk_radio_action_set_property (GObject *object,
73 static void gtk_radio_action_get_property (GObject *object,
77 static void gtk_radio_action_activate (GtkAction *action);
78 static GtkWidget *create_menu_item (GtkAction *action);
81 G_DEFINE_TYPE (GtkRadioAction, gtk_radio_action, GTK_TYPE_TOGGLE_ACTION)
83 static guint radio_action_signals[LAST_SIGNAL] = { 0 };
86 gtk_radio_action_class_init (GtkRadioActionClass *klass)
88 GObjectClass *gobject_class;
89 GtkActionClass *action_class;
91 gobject_class = G_OBJECT_CLASS (klass);
92 action_class = GTK_ACTION_CLASS (klass);
94 gobject_class->finalize = gtk_radio_action_finalize;
95 gobject_class->set_property = gtk_radio_action_set_property;
96 gobject_class->get_property = gtk_radio_action_get_property;
98 action_class->activate = gtk_radio_action_activate;
100 action_class->create_menu_item = create_menu_item;
103 * GtkRadioAction:value:
105 * The value is an arbitrary integer which can be used as a
106 * convenient way to determine which action in the group is
107 * currently active in an ::activate or ::changed signal handler.
108 * See gtk_radio_action_get_current_value() and #GtkRadioActionEntry
109 * for convenient ways to get and set this property.
113 g_object_class_install_property (gobject_class,
115 g_param_spec_int ("value",
117 P_("The value returned by gtk_radio_action_get_current_value() when this action is the current action of its group."),
121 GTK_PARAM_READWRITE));
124 * GtkRadioAction:group:
126 * Sets a new group for a radio action.
130 g_object_class_install_property (gobject_class,
132 g_param_spec_object ("group",
134 P_("The radio action whose group this action belongs to."),
135 GTK_TYPE_RADIO_ACTION,
136 GTK_PARAM_WRITABLE));
139 * GtkRadioAction:current-value:
141 * The value property of the currently active member of the group to which
142 * this action belongs.
146 g_object_class_install_property (gobject_class,
148 g_param_spec_int ("current-value",
149 P_("The current value"),
150 P_("The value property of the currently active member of the group to which this action belongs."),
154 GTK_PARAM_READWRITE));
157 * GtkRadioAction::changed:
158 * @action: the action on which the signal is emitted
159 * @current: the member of @action<!-- -->s group which has just been activated
161 * The ::changed signal is emitted on every member of a radio group when the
162 * active member is changed. The signal gets emitted after the ::activate signals
163 * for the previous and current active members.
167 radio_action_signals[CHANGED] =
168 g_signal_new (I_("changed"),
169 G_OBJECT_CLASS_TYPE (klass),
170 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
171 G_STRUCT_OFFSET (GtkRadioActionClass, changed), NULL, NULL,
172 g_cclosure_marshal_VOID__OBJECT,
173 G_TYPE_NONE, 1, GTK_TYPE_RADIO_ACTION);
175 g_type_class_add_private (gobject_class, sizeof (GtkRadioActionPrivate));
179 gtk_radio_action_init (GtkRadioAction *action)
181 action->private_data = G_TYPE_INSTANCE_GET_PRIVATE (action,
182 GTK_TYPE_RADIO_ACTION,
183 GtkRadioActionPrivate);
185 action->private_data->group = g_slist_prepend (NULL, action);
186 action->private_data->value = 0;
188 gtk_toggle_action_set_draw_as_radio (GTK_TOGGLE_ACTION (action), TRUE);
192 * gtk_radio_action_new:
193 * @name: A unique name for the action
194 * @label: (allow-none): The label displayed in menu items and on buttons,
196 * @tooltip: (allow-none): A tooltip for this action, or %NULL
197 * @stock_id: (allow-none): The stock icon to display in widgets representing
198 * this action, or %NULL
199 * @value: The value which gtk_radio_action_get_current_value() should
200 * return if this action is selected.
202 * Creates a new #GtkRadioAction object. To add the action to
203 * a #GtkActionGroup and set the accelerator for the action,
204 * call gtk_action_group_add_action_with_accel().
206 * Return value: a new #GtkRadioAction
211 gtk_radio_action_new (const gchar *name,
213 const gchar *tooltip,
214 const gchar *stock_id,
217 g_return_val_if_fail (name != NULL, NULL);
219 return g_object_new (GTK_TYPE_RADIO_ACTION,
223 "stock-id", stock_id,
229 gtk_radio_action_finalize (GObject *object)
231 GtkRadioAction *action;
234 action = GTK_RADIO_ACTION (object);
236 action->private_data->group = g_slist_remove (action->private_data->group, action);
238 tmp_list = action->private_data->group;
242 GtkRadioAction *tmp_action = tmp_list->data;
244 tmp_list = tmp_list->next;
245 tmp_action->private_data->group = action->private_data->group;
248 G_OBJECT_CLASS (gtk_radio_action_parent_class)->finalize (object);
252 gtk_radio_action_set_property (GObject *object,
257 GtkRadioAction *radio_action;
259 radio_action = GTK_RADIO_ACTION (object);
264 radio_action->private_data->value = g_value_get_int (value);
269 GSList *slist = NULL;
271 if (G_VALUE_HOLDS_OBJECT (value))
273 arg = GTK_RADIO_ACTION (g_value_get_object (value));
275 slist = gtk_radio_action_get_group (arg);
276 gtk_radio_action_set_group (radio_action, slist);
280 case PROP_CURRENT_VALUE:
281 gtk_radio_action_set_current_value (radio_action,
282 g_value_get_int (value));
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 gtk_radio_action_get_property (GObject *object,
296 GtkRadioAction *radio_action;
298 radio_action = GTK_RADIO_ACTION (object);
303 g_value_set_int (value, radio_action->private_data->value);
305 case PROP_CURRENT_VALUE:
306 g_value_set_int (value,
307 gtk_radio_action_get_current_value (radio_action));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 gtk_radio_action_activate (GtkAction *action)
318 GtkRadioAction *radio_action;
319 GtkToggleAction *toggle_action;
320 GtkToggleAction *tmp_action;
324 radio_action = GTK_RADIO_ACTION (action);
325 toggle_action = GTK_TOGGLE_ACTION (action);
327 active = gtk_toggle_action_get_active (toggle_action);
330 tmp_list = radio_action->private_data->group;
334 tmp_action = tmp_list->data;
335 tmp_list = tmp_list->next;
337 if (gtk_toggle_action_get_active (tmp_action) &&
338 (tmp_action != toggle_action))
340 _gtk_toggle_action_set_active (toggle_action, !active);
345 g_object_notify (G_OBJECT (action), "active");
349 _gtk_toggle_action_set_active (toggle_action, !active);
350 g_object_notify (G_OBJECT (action), "active");
352 tmp_list = radio_action->private_data->group;
355 tmp_action = tmp_list->data;
356 tmp_list = tmp_list->next;
358 if (gtk_toggle_action_get_active (tmp_action) &&
359 (tmp_action != toggle_action))
361 _gtk_action_emit_activate (GTK_ACTION (tmp_action));
366 tmp_list = radio_action->private_data->group;
369 tmp_action = tmp_list->data;
370 tmp_list = tmp_list->next;
372 g_object_notify (G_OBJECT (tmp_action), "current-value");
374 g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
378 gtk_toggle_action_toggled (toggle_action);
382 create_menu_item (GtkAction *action)
384 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
385 "draw-as-radio", TRUE,
390 * gtk_radio_action_get_group:
391 * @action: the action object
393 * Returns the list representing the radio group for this object.
394 * Note that the returned list is only valid until the next change
397 * A common way to set up a group of radio group is the following:
399 * GSList *group = NULL;
400 * GtkRadioAction *action;
402 * while (/* more actions to add */)
404 * action = gtk_radio_action_new (...);
406 * gtk_radio_action_set_group (action, group);
407 * group = gtk_radio_action_get_group (action);
411 * Returns: (element-type GtkRadioAction) (transfer none): the list representing the radio group for this object
416 gtk_radio_action_get_group (GtkRadioAction *action)
418 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
420 return action->private_data->group;
424 * gtk_radio_action_set_group:
425 * @action: the action object
426 * @group: (element-type GtkRadioAction): a list representing a radio group
428 * Sets the radio group for the radio action object.
433 gtk_radio_action_set_group (GtkRadioAction *action,
436 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
437 g_return_if_fail (!g_slist_find (group, action));
439 if (action->private_data->group)
443 action->private_data->group = g_slist_remove (action->private_data->group, action);
445 for (slist = action->private_data->group; slist; slist = slist->next)
447 GtkRadioAction *tmp_action = slist->data;
449 tmp_action->private_data->group = action->private_data->group;
453 action->private_data->group = g_slist_prepend (group, action);
459 for (slist = action->private_data->group; slist; slist = slist->next)
461 GtkRadioAction *tmp_action = slist->data;
463 tmp_action->private_data->group = action->private_data->group;
468 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
473 * gtk_radio_action_join_group:
474 * @action: the action object
475 * @group_source: (allow-none): a radio action object whos group we are
476 * joining, or %NULL to remove the radio action from its group
478 * Joins a radio action object to the group of another radio action object.
480 * Use this in language bindings instead of the gtk_radio_action_get_group()
481 * and gtk_radio_action_set_group() methods
483 * A common way to set up a group of radio actions is the following:
485 * GtkRadioAction *action;
486 * GtkRadioAction *last_action;
488 * while (/* more actions to add */)
490 * action = gtk_radio_action_new (...);
492 * gtk_radio_action_join_group (action, last_action);
493 * last_action = action;
500 gtk_radio_action_join_group (GtkRadioAction *action,
501 GtkRadioAction *group_source)
503 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
504 g_return_if_fail (group_source == NULL || GTK_IS_RADIO_ACTION (group_source));
509 group = gtk_radio_action_get_group (group_source);
513 /* if we are not already part of a group we need to set up a new one
514 and then get the newly created group */
515 gtk_radio_action_set_group (group_source, NULL);
516 group = gtk_radio_action_get_group (group_source);
519 gtk_radio_action_set_group (action, group);
523 gtk_radio_action_set_group (action, NULL);
528 * gtk_radio_action_get_current_value:
529 * @action: a #GtkRadioAction
531 * Obtains the value property of the currently active member of
532 * the group to which @action belongs.
534 * Return value: The value of the currently active group member
539 gtk_radio_action_get_current_value (GtkRadioAction *action)
543 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);
545 if (action->private_data->group)
547 for (slist = action->private_data->group; slist; slist = slist->next)
549 GtkToggleAction *toggle_action = slist->data;
551 if (gtk_toggle_action_get_active (toggle_action))
552 return GTK_RADIO_ACTION (toggle_action)->private_data->value;
556 return action->private_data->value;
560 * gtk_radio_action_set_current_value:
561 * @action: a #GtkRadioAction
562 * @current_value: the new value
564 * Sets the currently active group member to the member with value
565 * property @current_value.
570 gtk_radio_action_set_current_value (GtkRadioAction *action,
575 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
577 if (action->private_data->group)
579 for (slist = action->private_data->group; slist; slist = slist->next)
581 GtkRadioAction *radio_action = slist->data;
583 if (radio_action->private_data->value == current_value)
585 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action),
592 if (action->private_data->value == current_value)
593 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
595 g_warning ("Radio group does not contain an action with value '%d'",