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"
54 G_DEFINE_TYPE (GtkToggleAction, gtk_toggle_action, GTK_TYPE_ACTION)
56 static void gtk_toggle_action_activate (GtkAction *action);
57 static void set_property (GObject *object,
61 static void get_property (GObject *object,
65 static GtkWidget *create_menu_item (GtkAction *action);
68 static GObjectClass *parent_class = NULL;
69 static guint action_signals[LAST_SIGNAL] = { 0 };
72 gtk_toggle_action_class_init (GtkToggleActionClass *klass)
74 GObjectClass *gobject_class;
75 GtkActionClass *action_class;
77 parent_class = g_type_class_peek_parent (klass);
78 gobject_class = G_OBJECT_CLASS (klass);
79 action_class = GTK_ACTION_CLASS (klass);
81 gobject_class->set_property = set_property;
82 gobject_class->get_property = get_property;
84 action_class->activate = gtk_toggle_action_activate;
86 action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
87 action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
89 action_class->create_menu_item = create_menu_item;
91 klass->toggled = NULL;
94 * GtkToggleAction:draw-as-radio:
96 * Whether the proxies for this action look like radio action proxies.
98 * This is an appearance property and thus only applies if
99 * #GtkActivatable:use-action-appearance is %TRUE.
101 g_object_class_install_property (gobject_class,
103 g_param_spec_boolean ("draw-as-radio",
104 P_("Create the same proxies as a radio action"),
105 P_("Whether the proxies for this action look like radio action proxies"),
107 GTK_PARAM_READWRITE));
110 * GtkToggleAction:active:
112 * If the toggle action should be active in or not.
116 g_object_class_install_property (gobject_class,
118 g_param_spec_boolean ("active",
120 P_("If the toggle action should be active in or not"),
122 GTK_PARAM_READWRITE));
124 action_signals[TOGGLED] =
125 g_signal_new (I_("toggled"),
126 G_OBJECT_CLASS_TYPE (klass),
128 G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
130 g_cclosure_marshal_VOID__VOID,
133 g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
137 gtk_toggle_action_init (GtkToggleAction *action)
139 action->private_data = GTK_TOGGLE_ACTION_GET_PRIVATE (action);
140 action->private_data->active = FALSE;
141 action->private_data->draw_as_radio = FALSE;
145 * gtk_toggle_action_new:
146 * @name: A unique name for the action
147 * @label: (allow-none): The label displayed in menu items and on buttons, or %NULL
148 * @tooltip: (allow-none): A tooltip for the action, or %NULL
149 * @stock_id: The stock icon to display in widgets representing the
152 * Creates a new #GtkToggleAction object. To add the action to
153 * a #GtkActionGroup and set the accelerator for the action,
154 * call gtk_action_group_add_action_with_accel().
156 * Return value: a new #GtkToggleAction
161 gtk_toggle_action_new (const gchar *name,
163 const gchar *tooltip,
164 const gchar *stock_id)
166 g_return_val_if_fail (name != NULL, NULL);
168 return g_object_new (GTK_TYPE_TOGGLE_ACTION,
172 "stock-id", stock_id,
177 get_property (GObject *object,
182 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
186 case PROP_DRAW_AS_RADIO:
187 g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
190 g_value_set_boolean (value, gtk_toggle_action_get_active (action));
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 set_property (GObject *object,
204 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
208 case PROP_DRAW_AS_RADIO:
209 gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
212 gtk_toggle_action_set_active (action, g_value_get_boolean (value));
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gtk_toggle_action_activate (GtkAction *action)
223 GtkToggleAction *toggle_action;
225 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
227 toggle_action = GTK_TOGGLE_ACTION (action);
229 toggle_action->private_data->active = !toggle_action->private_data->active;
231 g_object_notify (G_OBJECT (action), "active");
233 gtk_toggle_action_toggled (toggle_action);
237 * gtk_toggle_action_toggled:
238 * @action: the action object
240 * Emits the "toggled" signal on the toggle action.
245 gtk_toggle_action_toggled (GtkToggleAction *action)
247 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
249 g_signal_emit (action, action_signals[TOGGLED], 0);
253 * gtk_toggle_action_set_active:
254 * @action: the action object
255 * @is_active: whether the action should be checked or not
257 * Sets the checked state on the toggle action.
262 gtk_toggle_action_set_active (GtkToggleAction *action,
265 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
267 is_active = is_active != FALSE;
269 if (action->private_data->active != is_active)
270 _gtk_action_emit_activate (GTK_ACTION (action));
274 * gtk_toggle_action_get_active:
275 * @action: the action object
277 * Returns the checked state of the toggle action.
279 * Returns: the checked state of the toggle action
284 gtk_toggle_action_get_active (GtkToggleAction *action)
286 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
288 return action->private_data->active;
293 * gtk_toggle_action_set_draw_as_radio:
294 * @action: the action object
295 * @draw_as_radio: whether the action should have proxies like a radio
298 * Sets whether the action should have proxies like a radio action.
303 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action,
304 gboolean draw_as_radio)
306 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
308 draw_as_radio = draw_as_radio != FALSE;
310 if (action->private_data->draw_as_radio != draw_as_radio)
312 action->private_data->draw_as_radio = draw_as_radio;
314 g_object_notify (G_OBJECT (action), "draw-as-radio");
319 * gtk_toggle_action_get_draw_as_radio:
320 * @action: the action object
322 * Returns whether the action should have proxies like a radio action.
324 * Returns: whether the action should have proxies like a radio action.
329 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
331 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
333 return action->private_data->draw_as_radio;
337 create_menu_item (GtkAction *action)
339 GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
341 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
342 "draw-as-radio", toggle_action->private_data->draw_as_radio,
346 #define __GTK_TOGGLE_ACTION_C__
347 #include "gtkaliasdef.c"