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 "gtktoggleactionprivate.h"
36 #include "gtktoggletoolbutton.h"
38 #include "gtkprivate.h"
41 #define GTK_RADIO_ACTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RADIO_ACTION, GtkRadioActionPrivate))
43 struct _GtkRadioActionPrivate
63 static void gtk_radio_action_finalize (GObject *object);
64 static void gtk_radio_action_set_property (GObject *object,
68 static void gtk_radio_action_get_property (GObject *object,
72 static void gtk_radio_action_activate (GtkAction *action);
73 static GtkWidget *create_menu_item (GtkAction *action);
76 G_DEFINE_TYPE (GtkRadioAction, gtk_radio_action, GTK_TYPE_TOGGLE_ACTION)
78 static guint radio_action_signals[LAST_SIGNAL] = { 0 };
81 gtk_radio_action_class_init (GtkRadioActionClass *klass)
83 GObjectClass *gobject_class;
84 GtkActionClass *action_class;
86 gobject_class = G_OBJECT_CLASS (klass);
87 action_class = GTK_ACTION_CLASS (klass);
89 gobject_class->finalize = gtk_radio_action_finalize;
90 gobject_class->set_property = gtk_radio_action_set_property;
91 gobject_class->get_property = gtk_radio_action_get_property;
93 action_class->activate = gtk_radio_action_activate;
95 action_class->create_menu_item = create_menu_item;
98 * GtkRadioAction:value:
100 * The value is an arbitrary integer which can be used as a
101 * convenient way to determine which action in the group is
102 * currently active in an ::activate or ::changed signal handler.
103 * See gtk_radio_action_get_current_value() and #GtkRadioActionEntry
104 * for convenient ways to get and set this property.
108 g_object_class_install_property (gobject_class,
110 g_param_spec_int ("value",
112 P_("The value returned by gtk_radio_action_get_current_value() when this action is the current action of its group."),
116 GTK_PARAM_READWRITE));
119 * GtkRadioAction:group:
121 * Sets a new group for a radio action.
125 g_object_class_install_property (gobject_class,
127 g_param_spec_object ("group",
129 P_("The radio action whose group this action belongs to."),
130 GTK_TYPE_RADIO_ACTION,
131 GTK_PARAM_WRITABLE));
134 * GtkRadioAction:current-value:
136 * The value property of the currently active member of the group to which
137 * this action belongs.
141 g_object_class_install_property (gobject_class,
143 g_param_spec_int ("current-value",
144 P_("The current value"),
145 P_("The value property of the currently active member of the group to which this action belongs."),
149 GTK_PARAM_READWRITE));
152 * GtkRadioAction::changed:
153 * @action: the action on which the signal is emitted
154 * @current: the member of @action<!-- -->s group which has just been activated
156 * The ::changed signal is emitted on every member of a radio group when the
157 * active member is changed. The signal gets emitted after the ::activate signals
158 * for the previous and current active members.
162 radio_action_signals[CHANGED] =
163 g_signal_new (I_("changed"),
164 G_OBJECT_CLASS_TYPE (klass),
165 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
166 G_STRUCT_OFFSET (GtkRadioActionClass, changed), NULL, NULL,
167 g_cclosure_marshal_VOID__OBJECT,
168 G_TYPE_NONE, 1, GTK_TYPE_RADIO_ACTION);
170 g_type_class_add_private (gobject_class, sizeof (GtkRadioActionPrivate));
174 gtk_radio_action_init (GtkRadioAction *action)
176 action->private_data = GTK_RADIO_ACTION_GET_PRIVATE (action);
177 action->private_data->group = g_slist_prepend (NULL, action);
178 action->private_data->value = 0;
182 * gtk_radio_action_new:
183 * @name: A unique name for the action
184 * @label: The label displayed in menu items and on buttons, or %NULL
185 * @tooltip: A tooltip for this action, or %NULL
186 * @stock_id: The stock icon to display in widgets representing this
188 * @value: The value which gtk_radio_action_get_current_value() should
189 * return if this action is selected.
191 * Creates a new #GtkRadioAction object. To add the action to
192 * a #GtkActionGroup and set the accelerator for the action,
193 * call gtk_action_group_add_action_with_accel().
195 * Return value: a new #GtkRadioAction
200 gtk_radio_action_new (const gchar *name,
202 const gchar *tooltip,
203 const gchar *stock_id,
206 g_return_val_if_fail (name != NULL, NULL);
208 return g_object_new (GTK_TYPE_RADIO_ACTION,
212 "stock-id", stock_id,
218 gtk_radio_action_finalize (GObject *object)
220 GtkRadioAction *action;
223 action = GTK_RADIO_ACTION (object);
225 action->private_data->group = g_slist_remove (action->private_data->group, action);
227 tmp_list = action->private_data->group;
231 GtkRadioAction *tmp_action = tmp_list->data;
233 tmp_list = tmp_list->next;
234 tmp_action->private_data->group = action->private_data->group;
237 G_OBJECT_CLASS (gtk_radio_action_parent_class)->finalize (object);
241 gtk_radio_action_set_property (GObject *object,
246 GtkRadioAction *radio_action;
248 radio_action = GTK_RADIO_ACTION (object);
253 radio_action->private_data->value = g_value_get_int (value);
258 GSList *slist = NULL;
260 if (G_VALUE_HOLDS_OBJECT (value))
262 arg = GTK_RADIO_ACTION (g_value_get_object (value));
264 slist = gtk_radio_action_get_group (arg);
265 gtk_radio_action_set_group (radio_action, slist);
269 case PROP_CURRENT_VALUE:
270 gtk_radio_action_set_current_value (radio_action,
271 g_value_get_int (value));
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 gtk_radio_action_get_property (GObject *object,
285 GtkRadioAction *radio_action;
287 radio_action = GTK_RADIO_ACTION (object);
292 g_value_set_int (value, radio_action->private_data->value);
294 case PROP_CURRENT_VALUE:
295 g_value_set_int (value,
296 gtk_radio_action_get_current_value (radio_action));
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 gtk_radio_action_activate (GtkAction *action)
307 GtkRadioAction *radio_action;
308 GtkToggleAction *toggle_action;
309 GtkToggleAction *tmp_action;
312 radio_action = GTK_RADIO_ACTION (action);
313 toggle_action = GTK_TOGGLE_ACTION (action);
315 if (toggle_action->private_data->active)
317 tmp_list = radio_action->private_data->group;
321 tmp_action = tmp_list->data;
322 tmp_list = tmp_list->next;
324 if (tmp_action->private_data->active && (tmp_action != toggle_action))
326 toggle_action->private_data->active = !toggle_action->private_data->active;
333 toggle_action->private_data->active = !toggle_action->private_data->active;
335 tmp_list = radio_action->private_data->group;
338 tmp_action = tmp_list->data;
339 tmp_list = tmp_list->next;
341 if (tmp_action->private_data->active && (tmp_action != toggle_action))
343 _gtk_action_emit_activate (GTK_ACTION (tmp_action));
348 tmp_list = radio_action->private_data->group;
351 tmp_action = tmp_list->data;
352 tmp_list = tmp_list->next;
354 g_object_notify (G_OBJECT (tmp_action), "current-value");
356 g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
360 gtk_toggle_action_toggled (toggle_action);
364 create_menu_item (GtkAction *action)
366 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
367 "draw-as-radio", TRUE,
372 * gtk_radio_action_get_group:
373 * @action: the action object
375 * Returns the list representing the radio group for this object.
376 * Note that the returned list is only valid until the next change
379 * A common way to set up a group of radio group is the following:
381 * GSList *group = NULL;
382 * GtkRadioAction *action;
384 * while (/* more actions to add */)
386 * action = gtk_radio_action_new (...);
388 * gtk_radio_action_set_group (action, group);
389 * group = gtk_radio_action_get_group (action);
393 * Returns: the list representing the radio group for this object
398 gtk_radio_action_get_group (GtkRadioAction *action)
400 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
402 return action->private_data->group;
406 * gtk_radio_action_set_group:
407 * @action: the action object
408 * @group: a list representing a radio group
410 * Sets the radio group for the radio action object.
415 gtk_radio_action_set_group (GtkRadioAction *action,
418 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
419 g_return_if_fail (!g_slist_find (group, action));
421 if (action->private_data->group)
425 action->private_data->group = g_slist_remove (action->private_data->group, action);
427 for (slist = action->private_data->group; slist; slist = slist->next)
429 GtkRadioAction *tmp_action = slist->data;
431 tmp_action->private_data->group = action->private_data->group;
435 action->private_data->group = g_slist_prepend (group, action);
441 for (slist = action->private_data->group; slist; slist = slist->next)
443 GtkRadioAction *tmp_action = slist->data;
445 tmp_action->private_data->group = action->private_data->group;
450 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
455 * gtk_radio_action_get_current_value:
456 * @action: a #GtkRadioAction
458 * Obtains the value property of the currently active member of
459 * the group to which @action belongs.
461 * Return value: The value of the currently active group member
466 gtk_radio_action_get_current_value (GtkRadioAction *action)
470 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);
472 if (action->private_data->group)
474 for (slist = action->private_data->group; slist; slist = slist->next)
476 GtkToggleAction *toggle_action = slist->data;
478 if (toggle_action->private_data->active)
479 return GTK_RADIO_ACTION (toggle_action)->private_data->value;
483 return action->private_data->value;
487 * gtk_radio_action_set_current_value:
488 * @action: a #GtkRadioAction
489 * @current_value: the new value
491 * Sets the currently active group member to the member with value
492 * property @current_value.
497 gtk_radio_action_set_current_value (GtkRadioAction *action,
502 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
504 if (action->private_data->group)
506 for (slist = action->private_data->group; slist; slist = slist->next)
508 GtkRadioAction *radio_action = slist->data;
510 if (radio_action->private_data->value == current_value)
512 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action),
519 if (action->private_data->value == current_value)
520 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
522 g_warning ("Radio group does not contain an action with value '%d'",
526 #define __GTK_RADIO_ACTION_C__
527 #include "gtkaliasdef.c"