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/.
34 #include "gtktoggleaction.h"
35 #include "gtktoggletoolbutton.h"
36 #include "gtktogglebutton.h"
37 #include "gtkcheckmenuitem.h"
38 #include "gtkprivate.h"
42 * SECTION:gtktoggleaction
43 * @Short_description: An action which can be toggled between two states
44 * @Title: GtkToggleAction
46 * A #GtkToggleAction corresponds roughly to a #GtkCheckMenuItem. It has an
47 * "active" state specifying whether the action has been checked or not.
50 struct _GtkToggleActionPrivate
53 guint draw_as_radio : 1;
68 G_DEFINE_TYPE (GtkToggleAction, gtk_toggle_action, GTK_TYPE_ACTION)
70 static void gtk_toggle_action_activate (GtkAction *action);
71 static void set_property (GObject *object,
75 static void get_property (GObject *object,
79 static GtkWidget *create_menu_item (GtkAction *action);
82 static GObjectClass *parent_class = NULL;
83 static guint action_signals[LAST_SIGNAL] = { 0 };
86 gtk_toggle_action_class_init (GtkToggleActionClass *klass)
88 GObjectClass *gobject_class;
89 GtkActionClass *action_class;
91 parent_class = g_type_class_peek_parent (klass);
92 gobject_class = G_OBJECT_CLASS (klass);
93 action_class = GTK_ACTION_CLASS (klass);
95 gobject_class->set_property = set_property;
96 gobject_class->get_property = get_property;
98 action_class->activate = gtk_toggle_action_activate;
100 action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
101 action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
103 action_class->create_menu_item = create_menu_item;
105 klass->toggled = NULL;
108 * GtkToggleAction:draw-as-radio:
110 * Whether the proxies for this action look like radio action proxies.
112 * This is an appearance property and thus only applies if
113 * #GtkActivatable:use-action-appearance is %TRUE.
115 g_object_class_install_property (gobject_class,
117 g_param_spec_boolean ("draw-as-radio",
118 P_("Create the same proxies as a radio action"),
119 P_("Whether the proxies for this action look like radio action proxies"),
121 GTK_PARAM_READWRITE));
124 * GtkToggleAction:active:
126 * Whether the toggle action should be active.
130 g_object_class_install_property (gobject_class,
132 g_param_spec_boolean ("active",
134 P_("Whether the toggle action should be active"),
136 GTK_PARAM_READWRITE));
138 * GtkToggleAction::toggled:
139 * @toggleaction: the object which received the signal.
141 * Should be connected if you wish to perform an action
142 * whenever the #GtkToggleAction state is changed.
144 action_signals[TOGGLED] =
145 g_signal_new (I_("toggled"),
146 G_OBJECT_CLASS_TYPE (klass),
148 G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
150 g_cclosure_marshal_VOID__VOID,
153 g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
157 gtk_toggle_action_init (GtkToggleAction *action)
159 action->private_data = G_TYPE_INSTANCE_GET_PRIVATE (action,
160 GTK_TYPE_TOGGLE_ACTION,
161 GtkToggleActionPrivate);
162 action->private_data->active = FALSE;
163 action->private_data->draw_as_radio = FALSE;
167 * gtk_toggle_action_new:
168 * @name: A unique name for the action
169 * @label: (allow-none): The label displayed in menu items and on buttons,
171 * @tooltip: (allow-none): A tooltip for the action, or %NULL
172 * @stock_id: (allow-none): The stock icon to display in widgets representing
173 * the action, or %NULL
175 * Creates a new #GtkToggleAction object. To add the action to
176 * a #GtkActionGroup and set the accelerator for the action,
177 * call gtk_action_group_add_action_with_accel().
179 * Return value: a new #GtkToggleAction
184 gtk_toggle_action_new (const gchar *name,
186 const gchar *tooltip,
187 const gchar *stock_id)
189 g_return_val_if_fail (name != NULL, NULL);
191 return g_object_new (GTK_TYPE_TOGGLE_ACTION,
195 "stock-id", stock_id,
200 get_property (GObject *object,
205 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
209 case PROP_DRAW_AS_RADIO:
210 g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
213 g_value_set_boolean (value, gtk_toggle_action_get_active (action));
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 set_property (GObject *object,
227 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
231 case PROP_DRAW_AS_RADIO:
232 gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
235 gtk_toggle_action_set_active (action, g_value_get_boolean (value));
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244 gtk_toggle_action_activate (GtkAction *action)
246 GtkToggleAction *toggle_action;
248 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
250 toggle_action = GTK_TOGGLE_ACTION (action);
252 toggle_action->private_data->active = !toggle_action->private_data->active;
254 g_object_notify (G_OBJECT (action), "active");
256 gtk_toggle_action_toggled (toggle_action);
260 * gtk_toggle_action_toggled:
261 * @action: the action object
263 * Emits the "toggled" signal on the toggle action.
268 gtk_toggle_action_toggled (GtkToggleAction *action)
270 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
272 g_signal_emit (action, action_signals[TOGGLED], 0);
276 * gtk_toggle_action_set_active:
277 * @action: the action object
278 * @is_active: whether the action should be checked or not
280 * Sets the checked state on the toggle action.
285 gtk_toggle_action_set_active (GtkToggleAction *action,
288 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
290 is_active = is_active != FALSE;
292 if (action->private_data->active != is_active)
293 _gtk_action_emit_activate (GTK_ACTION (action));
297 * gtk_toggle_action_get_active:
298 * @action: the action object
300 * Returns the checked state of the toggle action.
302 * Returns: the checked state of the toggle action
307 gtk_toggle_action_get_active (GtkToggleAction *action)
309 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
311 return action->private_data->active;
316 * gtk_toggle_action_set_draw_as_radio:
317 * @action: the action object
318 * @draw_as_radio: whether the action should have proxies like a radio
321 * Sets whether the action should have proxies like a radio action.
326 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action,
327 gboolean draw_as_radio)
329 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
331 draw_as_radio = draw_as_radio != FALSE;
333 if (action->private_data->draw_as_radio != draw_as_radio)
335 action->private_data->draw_as_radio = draw_as_radio;
337 g_object_notify (G_OBJECT (action), "draw-as-radio");
342 * gtk_toggle_action_get_draw_as_radio:
343 * @action: the action object
345 * Returns whether the action should have proxies like a radio action.
347 * Returns: whether the action should have proxies like a radio action.
352 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
354 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
356 return action->private_data->draw_as_radio;
360 create_menu_item (GtkAction *action)
362 GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
364 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
365 "draw-as-radio", toggle_action->private_data->draw_as_radio,
373 * _gtk_toggle_action_set_active:
374 * @toggle_action: a #GtkToggleAction
375 * @is_active: whether the action is active or not
377 * Sets the #GtkToggleAction:active property directly. This function does
378 * not emit signals or notifications: it is left to the caller to do so.
381 _gtk_toggle_action_set_active (GtkToggleAction *toggle_action,
384 GtkToggleActionPrivate *priv = toggle_action->private_data;
386 priv->active = is_active;