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/.
32 #include "gtktoggleaction.h"
33 #include "gtktoggletoolbutton.h"
34 #include "gtktogglebutton.h"
35 #include "gtkcheckmenuitem.h"
36 #include "gtkprivate.h"
40 * SECTION:gtktoggleaction
41 * @Short_description: An action which can be toggled between two states
42 * @Title: GtkToggleAction
44 * A #GtkToggleAction corresponds roughly to a #GtkCheckMenuItem. It has an
45 * "active" state specifying whether the action has been checked or not.
48 struct _GtkToggleActionPrivate
51 guint draw_as_radio : 1;
66 G_DEFINE_TYPE (GtkToggleAction, gtk_toggle_action, GTK_TYPE_ACTION)
68 static void gtk_toggle_action_activate (GtkAction *action);
69 static void set_property (GObject *object,
73 static void get_property (GObject *object,
77 static GtkWidget *create_menu_item (GtkAction *action);
80 static GObjectClass *parent_class = NULL;
81 static guint action_signals[LAST_SIGNAL] = { 0 };
84 gtk_toggle_action_class_init (GtkToggleActionClass *klass)
86 GObjectClass *gobject_class;
87 GtkActionClass *action_class;
89 parent_class = g_type_class_peek_parent (klass);
90 gobject_class = G_OBJECT_CLASS (klass);
91 action_class = GTK_ACTION_CLASS (klass);
93 gobject_class->set_property = set_property;
94 gobject_class->get_property = get_property;
96 action_class->activate = gtk_toggle_action_activate;
98 action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
99 action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
101 action_class->create_menu_item = create_menu_item;
103 klass->toggled = NULL;
106 * GtkToggleAction:draw-as-radio:
108 * Whether the proxies for this action look like radio action proxies.
110 * This is an appearance property and thus only applies if
111 * #GtkActivatable:use-action-appearance is %TRUE.
113 g_object_class_install_property (gobject_class,
115 g_param_spec_boolean ("draw-as-radio",
116 P_("Create the same proxies as a radio action"),
117 P_("Whether the proxies for this action look like radio action proxies"),
119 GTK_PARAM_READWRITE));
122 * GtkToggleAction:active:
124 * Whether the toggle action should be active.
128 g_object_class_install_property (gobject_class,
130 g_param_spec_boolean ("active",
132 P_("Whether the toggle action should be active"),
134 GTK_PARAM_READWRITE));
136 * GtkToggleAction::toggled:
137 * @toggleaction: the object which received the signal.
139 * Should be connected if you wish to perform an action
140 * whenever the #GtkToggleAction state is changed.
142 action_signals[TOGGLED] =
143 g_signal_new (I_("toggled"),
144 G_OBJECT_CLASS_TYPE (klass),
146 G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
148 g_cclosure_marshal_VOID__VOID,
151 g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
155 gtk_toggle_action_init (GtkToggleAction *action)
157 action->private_data = G_TYPE_INSTANCE_GET_PRIVATE (action,
158 GTK_TYPE_TOGGLE_ACTION,
159 GtkToggleActionPrivate);
160 action->private_data->active = FALSE;
161 action->private_data->draw_as_radio = FALSE;
165 * gtk_toggle_action_new:
166 * @name: A unique name for the action
167 * @label: (allow-none): The label displayed in menu items and on buttons,
169 * @tooltip: (allow-none): A tooltip for the action, or %NULL
170 * @stock_id: (allow-none): The stock icon to display in widgets representing
171 * the action, or %NULL
173 * Creates a new #GtkToggleAction object. To add the action to
174 * a #GtkActionGroup and set the accelerator for the action,
175 * call gtk_action_group_add_action_with_accel().
177 * Return value: a new #GtkToggleAction
182 gtk_toggle_action_new (const gchar *name,
184 const gchar *tooltip,
185 const gchar *stock_id)
187 g_return_val_if_fail (name != NULL, NULL);
189 return g_object_new (GTK_TYPE_TOGGLE_ACTION,
193 "stock-id", stock_id,
198 get_property (GObject *object,
203 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
207 case PROP_DRAW_AS_RADIO:
208 g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
211 g_value_set_boolean (value, gtk_toggle_action_get_active (action));
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 set_property (GObject *object,
225 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
229 case PROP_DRAW_AS_RADIO:
230 gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
233 gtk_toggle_action_set_active (action, g_value_get_boolean (value));
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 gtk_toggle_action_activate (GtkAction *action)
244 GtkToggleAction *toggle_action;
246 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
248 toggle_action = GTK_TOGGLE_ACTION (action);
250 toggle_action->private_data->active = !toggle_action->private_data->active;
252 g_object_notify (G_OBJECT (action), "active");
254 gtk_toggle_action_toggled (toggle_action);
258 * gtk_toggle_action_toggled:
259 * @action: the action object
261 * Emits the "toggled" signal on the toggle action.
266 gtk_toggle_action_toggled (GtkToggleAction *action)
268 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
270 g_signal_emit (action, action_signals[TOGGLED], 0);
274 * gtk_toggle_action_set_active:
275 * @action: the action object
276 * @is_active: whether the action should be checked or not
278 * Sets the checked state on the toggle action.
283 gtk_toggle_action_set_active (GtkToggleAction *action,
286 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
288 is_active = is_active != FALSE;
290 if (action->private_data->active != is_active)
291 _gtk_action_emit_activate (GTK_ACTION (action));
295 * gtk_toggle_action_get_active:
296 * @action: the action object
298 * Returns the checked state of the toggle action.
300 * Returns: the checked state of the toggle action
305 gtk_toggle_action_get_active (GtkToggleAction *action)
307 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
309 return action->private_data->active;
314 * gtk_toggle_action_set_draw_as_radio:
315 * @action: the action object
316 * @draw_as_radio: whether the action should have proxies like a radio
319 * Sets whether the action should have proxies like a radio action.
324 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action,
325 gboolean draw_as_radio)
327 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
329 draw_as_radio = draw_as_radio != FALSE;
331 if (action->private_data->draw_as_radio != draw_as_radio)
333 action->private_data->draw_as_radio = draw_as_radio;
335 g_object_notify (G_OBJECT (action), "draw-as-radio");
340 * gtk_toggle_action_get_draw_as_radio:
341 * @action: the action object
343 * Returns whether the action should have proxies like a radio action.
345 * Returns: whether the action should have proxies like a radio action.
350 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
352 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
354 return action->private_data->draw_as_radio;
358 create_menu_item (GtkAction *action)
360 GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
362 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
363 "draw-as-radio", toggle_action->private_data->draw_as_radio,
371 * _gtk_toggle_action_set_active:
372 * @toggle_action: a #GtkToggleAction
373 * @is_active: whether the action is active or not
375 * Sets the #GtkToggleAction:active property directly. This function does
376 * not emit signals or notifications: it is left to the caller to do so.
379 _gtk_toggle_action_set_active (GtkToggleAction *toggle_action,
382 GtkToggleActionPrivate *priv = toggle_action->private_data;
384 priv->active = is_active;