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;
180 gtk_toggle_action_set_draw_as_radio (GTK_TOGGLE_ACTION (action), TRUE);
184 * gtk_radio_action_new:
185 * @name: A unique name for the action
186 * @label: (allow-none): The label displayed in menu items and on buttons, or %NULL
187 * @tooltip: (allow-none): A tooltip for this action, or %NULL
188 * @stock_id: The stock icon to display in widgets representing this
190 * @value: The value which gtk_radio_action_get_current_value() should
191 * return if this action is selected.
193 * Creates a new #GtkRadioAction object. To add the action to
194 * a #GtkActionGroup and set the accelerator for the action,
195 * call gtk_action_group_add_action_with_accel().
197 * Return value: a new #GtkRadioAction
202 gtk_radio_action_new (const gchar *name,
204 const gchar *tooltip,
205 const gchar *stock_id,
208 g_return_val_if_fail (name != NULL, NULL);
210 return g_object_new (GTK_TYPE_RADIO_ACTION,
214 "stock-id", stock_id,
220 gtk_radio_action_finalize (GObject *object)
222 GtkRadioAction *action;
225 action = GTK_RADIO_ACTION (object);
227 action->private_data->group = g_slist_remove (action->private_data->group, action);
229 tmp_list = action->private_data->group;
233 GtkRadioAction *tmp_action = tmp_list->data;
235 tmp_list = tmp_list->next;
236 tmp_action->private_data->group = action->private_data->group;
239 G_OBJECT_CLASS (gtk_radio_action_parent_class)->finalize (object);
243 gtk_radio_action_set_property (GObject *object,
248 GtkRadioAction *radio_action;
250 radio_action = GTK_RADIO_ACTION (object);
255 radio_action->private_data->value = g_value_get_int (value);
260 GSList *slist = NULL;
262 if (G_VALUE_HOLDS_OBJECT (value))
264 arg = GTK_RADIO_ACTION (g_value_get_object (value));
266 slist = gtk_radio_action_get_group (arg);
267 gtk_radio_action_set_group (radio_action, slist);
271 case PROP_CURRENT_VALUE:
272 gtk_radio_action_set_current_value (radio_action,
273 g_value_get_int (value));
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 gtk_radio_action_get_property (GObject *object,
287 GtkRadioAction *radio_action;
289 radio_action = GTK_RADIO_ACTION (object);
294 g_value_set_int (value, radio_action->private_data->value);
296 case PROP_CURRENT_VALUE:
297 g_value_set_int (value,
298 gtk_radio_action_get_current_value (radio_action));
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
307 gtk_radio_action_activate (GtkAction *action)
309 GtkRadioAction *radio_action;
310 GtkToggleAction *toggle_action;
311 GtkToggleAction *tmp_action;
314 radio_action = GTK_RADIO_ACTION (action);
315 toggle_action = GTK_TOGGLE_ACTION (action);
317 if (toggle_action->private_data->active)
319 tmp_list = radio_action->private_data->group;
323 tmp_action = tmp_list->data;
324 tmp_list = tmp_list->next;
326 if (tmp_action->private_data->active && (tmp_action != toggle_action))
328 toggle_action->private_data->active = !toggle_action->private_data->active;
333 g_object_notify (G_OBJECT (action), "active");
337 toggle_action->private_data->active = !toggle_action->private_data->active;
338 g_object_notify (G_OBJECT (action), "active");
340 tmp_list = radio_action->private_data->group;
343 tmp_action = tmp_list->data;
344 tmp_list = tmp_list->next;
346 if (tmp_action->private_data->active && (tmp_action != toggle_action))
348 _gtk_action_emit_activate (GTK_ACTION (tmp_action));
353 tmp_list = radio_action->private_data->group;
356 tmp_action = tmp_list->data;
357 tmp_list = tmp_list->next;
359 g_object_notify (G_OBJECT (tmp_action), "current-value");
361 g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
365 gtk_toggle_action_toggled (toggle_action);
369 create_menu_item (GtkAction *action)
371 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
372 "draw-as-radio", TRUE,
377 * gtk_radio_action_get_group:
378 * @action: the action object
380 * Returns the list representing the radio group for this object.
381 * Note that the returned list is only valid until the next change
384 * A common way to set up a group of radio group is the following:
386 * GSList *group = NULL;
387 * GtkRadioAction *action;
389 * while (/* more actions to add */)
391 * action = gtk_radio_action_new (...);
393 * gtk_radio_action_set_group (action, group);
394 * group = gtk_radio_action_get_group (action);
398 * Returns: (element-type GtkAction) (transfer none): the list representing the radio group for this object
403 gtk_radio_action_get_group (GtkRadioAction *action)
405 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
407 return action->private_data->group;
411 * gtk_radio_action_set_group:
412 * @action: the action object
413 * @group: (element-type GtkAction): a list representing a radio group
415 * Sets the radio group for the radio action object.
420 gtk_radio_action_set_group (GtkRadioAction *action,
423 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
424 g_return_if_fail (!g_slist_find (group, action));
426 if (action->private_data->group)
430 action->private_data->group = g_slist_remove (action->private_data->group, action);
432 for (slist = action->private_data->group; slist; slist = slist->next)
434 GtkRadioAction *tmp_action = slist->data;
436 tmp_action->private_data->group = action->private_data->group;
440 action->private_data->group = g_slist_prepend (group, action);
446 for (slist = action->private_data->group; slist; slist = slist->next)
448 GtkRadioAction *tmp_action = slist->data;
450 tmp_action->private_data->group = action->private_data->group;
455 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
460 * gtk_radio_action_get_current_value:
461 * @action: a #GtkRadioAction
463 * Obtains the value property of the currently active member of
464 * the group to which @action belongs.
466 * Return value: The value of the currently active group member
471 gtk_radio_action_get_current_value (GtkRadioAction *action)
475 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);
477 if (action->private_data->group)
479 for (slist = action->private_data->group; slist; slist = slist->next)
481 GtkToggleAction *toggle_action = slist->data;
483 if (toggle_action->private_data->active)
484 return GTK_RADIO_ACTION (toggle_action)->private_data->value;
488 return action->private_data->value;
492 * gtk_radio_action_set_current_value:
493 * @action: a #GtkRadioAction
494 * @current_value: the new value
496 * Sets the currently active group member to the member with value
497 * property @current_value.
502 gtk_radio_action_set_current_value (GtkRadioAction *action,
507 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
509 if (action->private_data->group)
511 for (slist = action->private_data->group; slist; slist = slist->next)
513 GtkRadioAction *radio_action = slist->data;
515 if (radio_action->private_data->value == current_value)
517 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (radio_action),
524 if (action->private_data->value == current_value)
525 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
527 g_warning ("Radio group does not contain an action with value '%d'",
531 #define __GTK_RADIO_ACTION_C__
532 #include "gtkaliasdef.c"