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
185 * @tooltip: A tooltip for this action
186 * @stock_id: The stock icon to display in widgets representing this action
187 * @value: The value which gtk_radio_action_get_current_value() should return
188 * if this action is selected.
190 * Creates a new #GtkRadioAction object. To add the action to
191 * a #GtkActionGroup and set the accelerator for the action,
192 * call gtk_action_group_add_action_with_accel().
194 * Return value: a new #GtkRadioAction
199 gtk_radio_action_new (const gchar *name,
201 const gchar *tooltip,
202 const gchar *stock_id,
205 GtkRadioAction *action;
207 action = g_object_new (GTK_TYPE_RADIO_ACTION,
211 "stock-id", stock_id,
219 gtk_radio_action_finalize (GObject *object)
221 GtkRadioAction *action;
224 action = GTK_RADIO_ACTION (object);
226 action->private_data->group = g_slist_remove (action->private_data->group, action);
228 tmp_list = action->private_data->group;
232 GtkRadioAction *tmp_action = tmp_list->data;
234 tmp_list = tmp_list->next;
235 tmp_action->private_data->group = action->private_data->group;
238 G_OBJECT_CLASS (gtk_radio_action_parent_class)->finalize (object);
242 gtk_radio_action_set_property (GObject *object,
247 GtkRadioAction *radio_action;
249 radio_action = GTK_RADIO_ACTION (object);
254 radio_action->private_data->value = g_value_get_int (value);
259 GSList *slist = NULL;
261 if (G_VALUE_HOLDS_OBJECT (value))
263 arg = GTK_RADIO_ACTION (g_value_get_object (value));
265 slist = gtk_radio_action_get_group (arg);
266 gtk_radio_action_set_group (radio_action, slist);
270 case PROP_CURRENT_VALUE:
271 gtk_radio_action_set_current_value (radio_action,
272 g_value_get_int (value));
275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281 gtk_radio_action_get_property (GObject *object,
286 GtkRadioAction *radio_action;
288 radio_action = GTK_RADIO_ACTION (object);
293 g_value_set_int (value, radio_action->private_data->value);
295 case PROP_CURRENT_VALUE:
296 g_value_set_int (value,
297 gtk_radio_action_get_current_value (radio_action));
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306 gtk_radio_action_activate (GtkAction *action)
308 GtkRadioAction *radio_action;
309 GtkToggleAction *toggle_action;
310 GtkToggleAction *tmp_action;
313 radio_action = GTK_RADIO_ACTION (action);
314 toggle_action = GTK_TOGGLE_ACTION (action);
316 if (toggle_action->private_data->active)
318 tmp_list = radio_action->private_data->group;
322 tmp_action = tmp_list->data;
323 tmp_list = tmp_list->next;
325 if (tmp_action->private_data->active && (tmp_action != toggle_action))
327 toggle_action->private_data->active = !toggle_action->private_data->active;
334 toggle_action->private_data->active = !toggle_action->private_data->active;
336 tmp_list = radio_action->private_data->group;
339 tmp_action = tmp_list->data;
340 tmp_list = tmp_list->next;
342 if (tmp_action->private_data->active && (tmp_action != toggle_action))
344 _gtk_action_emit_activate (GTK_ACTION (tmp_action));
349 tmp_list = radio_action->private_data->group;
352 tmp_action = tmp_list->data;
353 tmp_list = tmp_list->next;
355 g_object_notify (G_OBJECT (tmp_action), "current-value");
357 g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
361 gtk_toggle_action_toggled (toggle_action);
365 create_menu_item (GtkAction *action)
367 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
368 "draw-as-radio", TRUE,
373 * gtk_radio_action_get_group:
374 * @action: the action object
376 * Returns the list representing the radio group for this object.
377 * Note that the returned list is only valid until the next change
380 * A common way to set up a group of radio group is the following:
381 * <informalexample><programlisting>
382 * GSList *group = NULL;
383 * GtkRadioAction *action;
385 * while (/<!-- -->* more actions to add *<!-- -->/)
387 * action = gtk_radio_action_new (...);
389 * gtk_radio_action_set_group (action, group);
390 * group = gtk_radio_action_get_group (action);
392 * </programlisting></informalexample>
394 * Returns: the list representing the radio group for this object
399 gtk_radio_action_get_group (GtkRadioAction *action)
401 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
403 return action->private_data->group;
407 * gtk_radio_action_set_group:
408 * @action: the action object
409 * @group: a list representing a radio group
411 * Sets the radio group for the radio action object.
416 gtk_radio_action_set_group (GtkRadioAction *action,
419 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
420 g_return_if_fail (!g_slist_find (group, action));
422 if (action->private_data->group)
426 action->private_data->group = g_slist_remove (action->private_data->group, action);
428 for (slist = action->private_data->group; slist; slist = slist->next)
430 GtkRadioAction *tmp_action = slist->data;
432 tmp_action->private_data->group = action->private_data->group;
436 action->private_data->group = g_slist_prepend (group, action);
442 for (slist = action->private_data->group; slist; slist = slist->next)
444 GtkRadioAction *tmp_action = slist->data;
446 tmp_action->private_data->group = action->private_data->group;
451 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
456 * gtk_radio_action_get_current_value:
457 * @action: a #GtkRadioAction
459 * Obtains the value property of the currently active member of
460 * the group to which @action belongs.
462 * Return value: The value of the currently active group member
467 gtk_radio_action_get_current_value (GtkRadioAction *action)
471 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);
473 if (action->private_data->group)
475 for (slist = action->private_data->group; slist; slist = slist->next)
477 GtkToggleAction *toggle_action = slist->data;
479 if (toggle_action->private_data->active)
480 return GTK_RADIO_ACTION (toggle_action)->private_data->value;
484 return action->private_data->value;
488 * gtk_radio_action_set_current_value:
489 * @action: a #GtkRadioAction
490 * @current_value: the new value
492 * Sets the currently active group member to the member with value
493 * property @current_value.
498 gtk_radio_action_set_current_value (GtkRadioAction *action,
503 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
505 if (action->private_data->group)
507 for (slist = action->private_data->group; slist; slist = slist->next)
509 GtkRadioAction *radio_action = slist->data;
511 if (radio_action->private_data->value == current_value)
513 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action),
520 if (action->private_data->value == current_value)
521 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
523 g_warning ("Radio group does not contain an action with value '%d'",
527 #define __GTK_RADIO_ACTION_C__
528 #include "gtkaliasdef.c"