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 "gtktoggleactionprivate.h"
36 #include "gtktoggletoolbutton.h"
37 #include "gtktogglebutton.h"
38 #include "gtkcheckmenuitem.h"
39 #include "gtkprivate.h"
44 * SECTION:gtktoggleaction
45 * @Short_description: An action which can be toggled between two states
46 * @Title: GtkToggleAction
48 * A #GtkToggleAction corresponds roughly to a #GtkCheckMenuItem. It has an
49 * "active" state specifying whether the action has been checked or not.
65 G_DEFINE_TYPE (GtkToggleAction, gtk_toggle_action, GTK_TYPE_ACTION)
67 static void gtk_toggle_action_activate (GtkAction *action);
68 static void set_property (GObject *object,
72 static void get_property (GObject *object,
76 static GtkWidget *create_menu_item (GtkAction *action);
79 static GObjectClass *parent_class = NULL;
80 static guint action_signals[LAST_SIGNAL] = { 0 };
83 gtk_toggle_action_class_init (GtkToggleActionClass *klass)
85 GObjectClass *gobject_class;
86 GtkActionClass *action_class;
88 parent_class = g_type_class_peek_parent (klass);
89 gobject_class = G_OBJECT_CLASS (klass);
90 action_class = GTK_ACTION_CLASS (klass);
92 gobject_class->set_property = set_property;
93 gobject_class->get_property = get_property;
95 action_class->activate = gtk_toggle_action_activate;
97 action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
98 action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
100 action_class->create_menu_item = create_menu_item;
102 klass->toggled = NULL;
105 * GtkToggleAction:draw-as-radio:
107 * Whether the proxies for this action look like radio action proxies.
109 * This is an appearance property and thus only applies if
110 * #GtkActivatable:use-action-appearance is %TRUE.
112 g_object_class_install_property (gobject_class,
114 g_param_spec_boolean ("draw-as-radio",
115 P_("Create the same proxies as a radio action"),
116 P_("Whether the proxies for this action look like radio action proxies"),
118 GTK_PARAM_READWRITE));
121 * GtkToggleAction:active:
123 * If the toggle action should be active in or not.
127 g_object_class_install_property (gobject_class,
129 g_param_spec_boolean ("active",
131 P_("If the toggle action should be active in or not"),
133 GTK_PARAM_READWRITE));
135 * GtkToggleAction::toggled:
136 * @toggleaction: the object which received the signal.
138 * Should be connected if you wish to perform an action
139 * whenever the #GtkToggleAction state is changed.
141 action_signals[TOGGLED] =
142 g_signal_new (I_("toggled"),
143 G_OBJECT_CLASS_TYPE (klass),
145 G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
147 g_cclosure_marshal_VOID__VOID,
150 g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
154 gtk_toggle_action_init (GtkToggleAction *action)
156 action->private_data = GTK_TOGGLE_ACTION_GET_PRIVATE (action);
157 action->private_data->active = FALSE;
158 action->private_data->draw_as_radio = FALSE;
162 * gtk_toggle_action_new:
163 * @name: A unique name for the action
164 * @label: (allow-none): The label displayed in menu items and on buttons, or %NULL
165 * @tooltip: (allow-none): A tooltip for the action, or %NULL
166 * @stock_id: The stock icon to display in widgets representing the
169 * Creates a new #GtkToggleAction object. To add the action to
170 * a #GtkActionGroup and set the accelerator for the action,
171 * call gtk_action_group_add_action_with_accel().
173 * Return value: a new #GtkToggleAction
178 gtk_toggle_action_new (const gchar *name,
180 const gchar *tooltip,
181 const gchar *stock_id)
183 g_return_val_if_fail (name != NULL, NULL);
185 return g_object_new (GTK_TYPE_TOGGLE_ACTION,
189 "stock-id", stock_id,
194 get_property (GObject *object,
199 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
203 case PROP_DRAW_AS_RADIO:
204 g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
207 g_value_set_boolean (value, gtk_toggle_action_get_active (action));
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 set_property (GObject *object,
221 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
225 case PROP_DRAW_AS_RADIO:
226 gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
229 gtk_toggle_action_set_active (action, g_value_get_boolean (value));
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 gtk_toggle_action_activate (GtkAction *action)
240 GtkToggleAction *toggle_action;
242 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
244 toggle_action = GTK_TOGGLE_ACTION (action);
246 toggle_action->private_data->active = !toggle_action->private_data->active;
248 g_object_notify (G_OBJECT (action), "active");
250 gtk_toggle_action_toggled (toggle_action);
254 * gtk_toggle_action_toggled:
255 * @action: the action object
257 * Emits the "toggled" signal on the toggle action.
262 gtk_toggle_action_toggled (GtkToggleAction *action)
264 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
266 g_signal_emit (action, action_signals[TOGGLED], 0);
270 * gtk_toggle_action_set_active:
271 * @action: the action object
272 * @is_active: whether the action should be checked or not
274 * Sets the checked state on the toggle action.
279 gtk_toggle_action_set_active (GtkToggleAction *action,
282 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
284 is_active = is_active != FALSE;
286 if (action->private_data->active != is_active)
287 _gtk_action_emit_activate (GTK_ACTION (action));
291 * gtk_toggle_action_get_active:
292 * @action: the action object
294 * Returns the checked state of the toggle action.
296 * Returns: the checked state of the toggle action
301 gtk_toggle_action_get_active (GtkToggleAction *action)
303 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
305 return action->private_data->active;
310 * gtk_toggle_action_set_draw_as_radio:
311 * @action: the action object
312 * @draw_as_radio: whether the action should have proxies like a radio
315 * Sets whether the action should have proxies like a radio action.
320 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action,
321 gboolean draw_as_radio)
323 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
325 draw_as_radio = draw_as_radio != FALSE;
327 if (action->private_data->draw_as_radio != draw_as_radio)
329 action->private_data->draw_as_radio = draw_as_radio;
331 g_object_notify (G_OBJECT (action), "draw-as-radio");
336 * gtk_toggle_action_get_draw_as_radio:
337 * @action: the action object
339 * Returns whether the action should have proxies like a radio action.
341 * Returns: whether the action should have proxies like a radio action.
346 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
348 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
350 return action->private_data->draw_as_radio;
354 create_menu_item (GtkAction *action)
356 GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
358 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
359 "draw-as-radio", toggle_action->private_data->draw_as_radio,
363 #define __GTK_TOGGLE_ACTION_C__
364 #include "gtkaliasdef.c"