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, or %NULL
170 * @tooltip: (allow-none): A tooltip for the action, or %NULL
171 * @stock_id: The stock icon to display in widgets representing the
174 * Creates a new #GtkToggleAction object. To add the action to
175 * a #GtkActionGroup and set the accelerator for the action,
176 * call gtk_action_group_add_action_with_accel().
178 * Return value: a new #GtkToggleAction
183 gtk_toggle_action_new (const gchar *name,
185 const gchar *tooltip,
186 const gchar *stock_id)
188 g_return_val_if_fail (name != NULL, NULL);
190 return g_object_new (GTK_TYPE_TOGGLE_ACTION,
194 "stock-id", stock_id,
199 get_property (GObject *object,
204 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
208 case PROP_DRAW_AS_RADIO:
209 g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
212 g_value_set_boolean (value, gtk_toggle_action_get_active (action));
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 set_property (GObject *object,
226 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
230 case PROP_DRAW_AS_RADIO:
231 gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
234 gtk_toggle_action_set_active (action, g_value_get_boolean (value));
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 gtk_toggle_action_activate (GtkAction *action)
245 GtkToggleAction *toggle_action;
247 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
249 toggle_action = GTK_TOGGLE_ACTION (action);
251 toggle_action->private_data->active = !toggle_action->private_data->active;
253 g_object_notify (G_OBJECT (action), "active");
255 gtk_toggle_action_toggled (toggle_action);
259 * gtk_toggle_action_toggled:
260 * @action: the action object
262 * Emits the "toggled" signal on the toggle action.
267 gtk_toggle_action_toggled (GtkToggleAction *action)
269 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
271 g_signal_emit (action, action_signals[TOGGLED], 0);
275 * gtk_toggle_action_set_active:
276 * @action: the action object
277 * @is_active: whether the action should be checked or not
279 * Sets the checked state on the toggle action.
284 gtk_toggle_action_set_active (GtkToggleAction *action,
287 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
289 is_active = is_active != FALSE;
291 if (action->private_data->active != is_active)
292 _gtk_action_emit_activate (GTK_ACTION (action));
296 * gtk_toggle_action_get_active:
297 * @action: the action object
299 * Returns the checked state of the toggle action.
301 * Returns: the checked state of the toggle action
306 gtk_toggle_action_get_active (GtkToggleAction *action)
308 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
310 return action->private_data->active;
315 * gtk_toggle_action_set_draw_as_radio:
316 * @action: the action object
317 * @draw_as_radio: whether the action should have proxies like a radio
320 * Sets whether the action should have proxies like a radio action.
325 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action,
326 gboolean draw_as_radio)
328 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
330 draw_as_radio = draw_as_radio != FALSE;
332 if (action->private_data->draw_as_radio != draw_as_radio)
334 action->private_data->draw_as_radio = draw_as_radio;
336 g_object_notify (G_OBJECT (action), "draw-as-radio");
341 * gtk_toggle_action_get_draw_as_radio:
342 * @action: the action object
344 * Returns whether the action should have proxies like a radio action.
346 * Returns: whether the action should have proxies like a radio action.
351 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
353 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
355 return action->private_data->draw_as_radio;
359 create_menu_item (GtkAction *action)
361 GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
363 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
364 "draw-as-radio", toggle_action->private_data->draw_as_radio,
372 * _gtk_toggle_action_set_active:
373 * @toggle_action: a #GtkToggleAction
374 * @is_active: whether the action is active or not
376 * Sets the #GtkToggleAction:active property directly. This function does
377 * not emit signals or notifications: it is left to the caller to do so.
380 _gtk_toggle_action_set_active (GtkToggleAction *toggle_action,
383 GtkToggleActionPrivate *priv = toggle_action->private_data;
385 priv->active = is_active;