2 * GTK - The GTK+ 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 gtk_toggle_action_real_toggled (GtkToggleAction *action);
58 static void connect_proxy (GtkAction *action,
60 static void disconnect_proxy (GtkAction *action,
62 static void set_property (GObject *object,
66 static void get_property (GObject *object,
70 static GtkWidget *create_menu_item (GtkAction *action);
73 static GObjectClass *parent_class = NULL;
74 static guint action_signals[LAST_SIGNAL] = { 0 };
77 gtk_toggle_action_class_init (GtkToggleActionClass *klass)
79 GObjectClass *gobject_class;
80 GtkActionClass *action_class;
82 parent_class = g_type_class_peek_parent (klass);
83 gobject_class = G_OBJECT_CLASS (klass);
84 action_class = GTK_ACTION_CLASS (klass);
86 gobject_class->set_property = set_property;
87 gobject_class->get_property = get_property;
89 action_class->activate = gtk_toggle_action_activate;
90 action_class->connect_proxy = connect_proxy;
91 action_class->disconnect_proxy = disconnect_proxy;
93 action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
94 action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
96 action_class->create_menu_item = create_menu_item;
98 klass->toggled = gtk_toggle_action_real_toggled;
100 g_object_class_install_property (gobject_class,
102 g_param_spec_boolean ("draw-as-radio",
103 P_("Create the same proxies as a radio action"),
104 P_("Whether the proxies for this action look like radio action proxies"),
106 GTK_PARAM_READWRITE));
109 * GtkToggleAction:active:
111 * If the toggle action should be active in or not.
115 g_object_class_install_property (gobject_class,
117 g_param_spec_boolean ("active",
119 P_("If the toggle action should be active in or not"),
121 GTK_PARAM_READWRITE));
123 action_signals[TOGGLED] =
124 g_signal_new (I_("toggled"),
125 G_OBJECT_CLASS_TYPE (klass),
127 G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
129 g_cclosure_marshal_VOID__VOID,
132 g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
136 gtk_toggle_action_init (GtkToggleAction *action)
138 action->private_data = GTK_TOGGLE_ACTION_GET_PRIVATE (action);
139 action->private_data->active = FALSE;
140 action->private_data->draw_as_radio = FALSE;
144 * gtk_toggle_action_new:
145 * @name: A unique name for the action
146 * @label: The label displayed in menu items and on buttons, or %NULL
147 * @tooltip: A tooltip for the action, or %NULL
148 * @stock_id: The stock icon to display in widgets representing the
151 * Creates a new #GtkToggleAction object. To add the action to
152 * a #GtkActionGroup and set the accelerator for the action,
153 * call gtk_action_group_add_action_with_accel().
155 * Return value: a new #GtkToggleAction
160 gtk_toggle_action_new (const gchar *name,
162 const gchar *tooltip,
163 const gchar *stock_id)
165 g_return_val_if_fail (name != NULL, NULL);
167 return g_object_new (GTK_TYPE_TOGGLE_ACTION,
171 "stock-id", stock_id,
176 get_property (GObject *object,
181 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
185 case PROP_DRAW_AS_RADIO:
186 g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
189 g_value_set_boolean (value, gtk_toggle_action_get_active (action));
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 set_property (GObject *object,
203 GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
207 case PROP_DRAW_AS_RADIO:
208 gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
211 gtk_toggle_action_set_active (action, g_value_get_boolean (value));
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gtk_toggle_action_activate (GtkAction *action)
222 GtkToggleAction *toggle_action;
224 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
226 toggle_action = GTK_TOGGLE_ACTION (action);
228 toggle_action->private_data->active = !toggle_action->private_data->active;
230 g_object_notify (G_OBJECT (action), "active");
232 gtk_toggle_action_toggled (toggle_action);
236 gtk_toggle_action_real_toggled (GtkToggleAction *action)
240 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
242 for (slist = gtk_action_get_proxies (GTK_ACTION (action)); slist; slist = slist->next)
244 GtkWidget *proxy = slist->data;
246 gtk_action_block_activate_from (GTK_ACTION (action), proxy);
247 if (GTK_IS_CHECK_MENU_ITEM (proxy))
248 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (proxy),
249 action->private_data->active);
250 else if (GTK_IS_TOGGLE_TOOL_BUTTON (proxy))
251 gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (proxy),
252 action->private_data->active);
253 else if (GTK_IS_TOGGLE_BUTTON (proxy))
254 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (proxy),
255 action->private_data->active);
257 g_warning ("Don't know how to toggle `%s' widgets",
258 G_OBJECT_TYPE_NAME (proxy));
260 gtk_action_unblock_activate_from (GTK_ACTION (action), proxy);
265 connect_proxy (GtkAction *action,
268 GtkToggleAction *toggle_action;
270 toggle_action = GTK_TOGGLE_ACTION (action);
272 /* do this before hand, so that we don't call the "activate" handler */
273 if (GTK_IS_CHECK_MENU_ITEM (proxy))
274 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (proxy),
275 toggle_action->private_data->active);
276 else if (GTK_IS_TOGGLE_TOOL_BUTTON (proxy))
277 gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (proxy),
278 toggle_action->private_data->active);
279 else if (GTK_IS_TOGGLE_BUTTON (proxy))
280 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (proxy),
281 toggle_action->private_data->active);
283 (* GTK_ACTION_CLASS (parent_class)->connect_proxy) (action, proxy);
287 disconnect_proxy (GtkAction *action,
290 (* GTK_ACTION_CLASS (parent_class)->disconnect_proxy) (action, proxy);
294 * gtk_toggle_action_toggled:
295 * @action: the action object
297 * Emits the "toggled" signal on the toggle action.
302 gtk_toggle_action_toggled (GtkToggleAction *action)
304 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
306 g_signal_emit (action, action_signals[TOGGLED], 0);
310 * gtk_toggle_action_set_active:
311 * @action: the action object
312 * @is_active: whether the action should be checked or not
314 * Sets the checked state on the toggle action.
319 gtk_toggle_action_set_active (GtkToggleAction *action,
322 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
324 is_active = is_active != FALSE;
326 if (action->private_data->active != is_active)
328 _gtk_action_emit_activate (GTK_ACTION (action));
333 * gtk_toggle_action_get_active:
334 * @action: the action object
336 * Returns the checked state of the toggle action.
338 * Returns: the checked state of the toggle action
343 gtk_toggle_action_get_active (GtkToggleAction *action)
345 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
347 return action->private_data->active;
352 * gtk_toggle_action_set_draw_as_radio:
353 * @action: the action object
354 * @draw_as_radio: whether the action should have proxies like a radio
357 * Sets whether the action should have proxies like a radio action.
362 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action,
363 gboolean draw_as_radio)
365 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
367 draw_as_radio = draw_as_radio != FALSE;
369 if (action->private_data->draw_as_radio != draw_as_radio)
371 action->private_data->draw_as_radio = draw_as_radio;
373 g_object_notify (G_OBJECT (action), "draw-as-radio");
378 * gtk_toggle_action_get_draw_as_radio:
379 * @action: the action object
381 * Returns whether the action should have proxies like a radio action.
383 * Returns: whether the action should have proxies like a radio action.
388 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
390 g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
392 return action->private_data->draw_as_radio;
396 create_menu_item (GtkAction *action)
398 GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
400 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
401 "draw-as-radio", toggle_action->private_data->draw_as_radio,
405 #define __GTK_TOGGLE_ACTION_C__
406 #include "gtkaliasdef.c"