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
62 static void gtk_radio_action_init (GtkRadioAction *action);
63 static void gtk_radio_action_class_init (GtkRadioActionClass *class);
64 static void gtk_radio_action_finalize (GObject *object);
65 static void gtk_radio_action_set_property (GObject *object,
69 static void gtk_radio_action_get_property (GObject *object,
73 static void gtk_radio_action_activate (GtkAction *action);
74 static GtkWidget *create_menu_item (GtkAction *action);
78 gtk_radio_action_get_type (void)
80 static GtkType type = 0;
84 static const GTypeInfo type_info =
86 sizeof (GtkRadioActionClass),
88 (GBaseFinalizeFunc) NULL,
89 (GClassInitFunc) gtk_radio_action_class_init,
90 (GClassFinalizeFunc) NULL,
93 sizeof (GtkRadioAction),
95 (GInstanceInitFunc) gtk_radio_action_init,
98 type = g_type_register_static (GTK_TYPE_TOGGLE_ACTION,
105 static GObjectClass *parent_class = NULL;
106 static guint radio_action_signals[LAST_SIGNAL] = { 0 };
109 gtk_radio_action_class_init (GtkRadioActionClass *klass)
111 GObjectClass *gobject_class;
112 GtkActionClass *action_class;
114 parent_class = g_type_class_peek_parent (klass);
115 gobject_class = G_OBJECT_CLASS (klass);
116 action_class = GTK_ACTION_CLASS (klass);
118 gobject_class->finalize = gtk_radio_action_finalize;
119 gobject_class->set_property = gtk_radio_action_set_property;
120 gobject_class->get_property = gtk_radio_action_get_property;
122 action_class->activate = gtk_radio_action_activate;
124 action_class->create_menu_item = create_menu_item;
127 * GtkRadioAction:value:
129 * The value is an arbitrary integer which can be used as a
130 * convenient way to determine which action in the group is
131 * currently active in an ::activate or ::changed signal handler.
132 * See gtk_radio_action_get_current_value() and #GtkRadioActionEntry
133 * for convenient ways to get and set this property.
137 g_object_class_install_property (gobject_class,
139 g_param_spec_int ("value",
141 P_("The value returned by gtk_radio_action_get_current_value() when this action is the current action of its group."),
145 GTK_PARAM_READWRITE));
148 * GtkRadioAction:group:
150 * Sets a new group for a radio action.
154 g_object_class_install_property (gobject_class,
156 g_param_spec_object ("group",
158 P_("The radio action whose group this action belongs to."),
159 GTK_TYPE_RADIO_ACTION,
160 GTK_PARAM_WRITABLE));
163 * GtkRadioAction::changed:
164 * @action: the action on which the signal is emitted
165 * @current: the member of @action<!-- -->s group which has just been activated
167 * The ::changed signal is emitted on every member of a radio group when the
168 * active member is changed. The signal gets emitted after the ::activate signals
169 * for the previous and current active members.
173 radio_action_signals[CHANGED] =
174 g_signal_new (I_("changed"),
175 G_OBJECT_CLASS_TYPE (klass),
176 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
177 G_STRUCT_OFFSET (GtkRadioActionClass, changed), NULL, NULL,
178 g_cclosure_marshal_VOID__OBJECT,
179 G_TYPE_NONE, 1, GTK_TYPE_RADIO_ACTION);
181 g_type_class_add_private (gobject_class, sizeof (GtkRadioActionPrivate));
185 gtk_radio_action_init (GtkRadioAction *action)
187 action->private_data = GTK_RADIO_ACTION_GET_PRIVATE (action);
188 action->private_data->group = g_slist_prepend (NULL, action);
189 action->private_data->value = 0;
193 * gtk_radio_action_new:
194 * @name: A unique name for the action
195 * @label: The label displayed in menu items and on buttons
196 * @tooltip: A tooltip for this action
197 * @stock_id: The stock icon to display in widgets representing this action
198 * @value: The value which gtk_radio_action_get_current_value() should return
199 * if this action is selected.
201 * Creates a new #GtkRadioAction object. To add the action to
202 * a #GtkActionGroup and set the accelerator for the action,
203 * call gtk_action_group_add_action_with_accel().
205 * Return value: a new #GtkRadioAction
210 gtk_radio_action_new (const gchar *name,
212 const gchar *tooltip,
213 const gchar *stock_id,
216 GtkRadioAction *action;
218 action = g_object_new (GTK_TYPE_RADIO_ACTION,
222 "stock-id", stock_id,
230 gtk_radio_action_finalize (GObject *object)
232 GtkRadioAction *action;
235 action = GTK_RADIO_ACTION (object);
237 action->private_data->group = g_slist_remove (action->private_data->group, action);
239 tmp_list = action->private_data->group;
243 GtkRadioAction *tmp_action = tmp_list->data;
245 tmp_list = tmp_list->next;
246 tmp_action->private_data->group = action->private_data->group;
249 (* parent_class->finalize) (object);
253 gtk_radio_action_set_property (GObject *object,
258 GtkRadioAction *radio_action;
260 radio_action = GTK_RADIO_ACTION (object);
265 radio_action->private_data->value = g_value_get_int (value);
270 GSList *slist = NULL;
272 if (G_VALUE_HOLDS_OBJECT (value))
274 arg = GTK_RADIO_ACTION (g_value_get_object (value));
276 slist = gtk_radio_action_get_group (arg);
277 gtk_radio_action_set_group (radio_action, slist);
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 gtk_radio_action_get_property (GObject *object,
293 GtkRadioAction *radio_action;
295 radio_action = GTK_RADIO_ACTION (object);
300 g_value_set_int (value, radio_action->private_data->value);
303 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
309 gtk_radio_action_activate (GtkAction *action)
311 GtkRadioAction *radio_action;
312 GtkToggleAction *toggle_action;
313 GtkToggleAction *tmp_action;
316 radio_action = GTK_RADIO_ACTION (action);
317 toggle_action = GTK_TOGGLE_ACTION (action);
319 if (toggle_action->private_data->active)
321 tmp_list = radio_action->private_data->group;
325 tmp_action = tmp_list->data;
326 tmp_list = tmp_list->next;
328 if (tmp_action->private_data->active && (tmp_action != toggle_action))
330 toggle_action->private_data->active = !toggle_action->private_data->active;
337 toggle_action->private_data->active = !toggle_action->private_data->active;
339 tmp_list = radio_action->private_data->group;
342 tmp_action = tmp_list->data;
343 tmp_list = tmp_list->next;
345 if (tmp_action->private_data->active && (tmp_action != toggle_action))
347 _gtk_action_emit_activate (GTK_ACTION (tmp_action));
352 tmp_list = radio_action->private_data->group;
355 tmp_action = tmp_list->data;
356 tmp_list = tmp_list->next;
358 g_signal_emit (tmp_action, radio_action_signals[CHANGED], 0, radio_action);
362 gtk_toggle_action_toggled (toggle_action);
366 create_menu_item (GtkAction *action)
368 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
369 "draw-as-radio", TRUE,
374 * gtk_radio_action_get_group:
375 * @action: the action object
377 * Returns the list representing the radio group for this object.
378 * Note that the returned list is only valid until the next change
381 * A common way to set up a group of radio group is the following:
382 * <informalexample><programlisting>
383 * GSList *group = NULL;
384 * GtkRadioAction *action;
386 * while (/<!-- -->* more actions to add *<!-- -->/)
388 * action = gtk_radio_action_new (...);
390 * gtk_radio_action_set_group (action, group);
391 * group = gtk_radio_action_get_group (action);
393 * </programlisting></informalexample>
395 * Returns: the list representing the radio group for this object
400 gtk_radio_action_get_group (GtkRadioAction *action)
402 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), NULL);
404 return action->private_data->group;
408 * gtk_radio_action_set_group:
409 * @action: the action object
410 * @group: a list representing a radio group
412 * Sets the radio group for the radio action object.
417 gtk_radio_action_set_group (GtkRadioAction *action,
420 g_return_if_fail (GTK_IS_RADIO_ACTION (action));
421 g_return_if_fail (!g_slist_find (group, action));
423 if (action->private_data->group)
427 action->private_data->group = g_slist_remove (action->private_data->group, action);
429 for (slist = action->private_data->group; slist; slist = slist->next)
431 GtkRadioAction *tmp_action = slist->data;
433 tmp_action->private_data->group = action->private_data->group;
437 action->private_data->group = g_slist_prepend (group, action);
443 for (slist = action->private_data->group; slist; slist = slist->next)
445 GtkRadioAction *tmp_action = slist->data;
447 tmp_action->private_data->group = action->private_data->group;
452 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
457 * gtk_radio_action_get_current_value:
458 * @action: a #GtkRadioAction
460 * Obtains the value property of the currently active member of
461 * the group to which @action belongs.
463 * Return value: The value of the currently active group member
468 gtk_radio_action_get_current_value (GtkRadioAction *action)
472 g_return_val_if_fail (GTK_IS_RADIO_ACTION (action), 0);
474 if (action->private_data->group)
476 for (slist = action->private_data->group; slist; slist = slist->next)
478 GtkToggleAction *toggle_action = slist->data;
480 if (toggle_action->private_data->active)
481 return GTK_RADIO_ACTION (toggle_action)->private_data->value;
485 return action->private_data->value;
488 #define __GTK_RADIO_ACTION_C__
489 #include "gtkaliasdef.c"