]> Pileus Git - ~andy/gtk/blob - gtk/gtktoggleaction.c
[annotations] Add allow-none
[~andy/gtk] / gtk / gtktoggleaction.c
1 /*
2  * GTK - The GIMP Toolkit
3  * Copyright (C) 1998, 1999 Red Hat, Inc.
4  * All rights reserved.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /*
23  * Author: James Henstridge <james@daa.com.au>
24  *
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/. 
29  */
30
31 #include "config.h"
32
33 #include "gtkintl.h"
34 #include "gtktoggleaction.h"
35 #include "gtktoggleactionprivate.h"
36 #include "gtktoggletoolbutton.h"
37 #include "gtktogglebutton.h"
38 #include "gtkcheckmenuitem.h"
39 #include "gtkprivate.h"
40 #include "gtkalias.h"
41
42 enum 
43 {
44   TOGGLED,
45   LAST_SIGNAL
46 };
47
48 enum {
49   PROP_0,
50   PROP_DRAW_AS_RADIO,
51   PROP_ACTIVE
52 };
53
54 G_DEFINE_TYPE (GtkToggleAction, gtk_toggle_action, GTK_TYPE_ACTION)
55
56 static void gtk_toggle_action_activate     (GtkAction       *action);
57 static void set_property                   (GObject         *object,
58                                             guint            prop_id,
59                                             const GValue    *value,
60                                             GParamSpec      *pspec);
61 static void get_property                   (GObject         *object,
62                                             guint            prop_id,
63                                             GValue          *value,
64                                             GParamSpec      *pspec);
65 static GtkWidget *create_menu_item         (GtkAction       *action);
66
67
68 static GObjectClass *parent_class = NULL;
69 static guint         action_signals[LAST_SIGNAL] = { 0 };
70
71 static void
72 gtk_toggle_action_class_init (GtkToggleActionClass *klass)
73 {
74   GObjectClass *gobject_class;
75   GtkActionClass *action_class;
76
77   parent_class = g_type_class_peek_parent (klass);
78   gobject_class = G_OBJECT_CLASS (klass);
79   action_class = GTK_ACTION_CLASS (klass);
80
81   gobject_class->set_property = set_property;
82   gobject_class->get_property = get_property;
83
84   action_class->activate = gtk_toggle_action_activate;
85
86   action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
87   action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
88
89   action_class->create_menu_item = create_menu_item;
90
91   klass->toggled = NULL;
92
93   /**
94    * GtkToggleAction:draw-as-radio:
95    *
96    * Whether the proxies for this action look like radio action proxies.
97    *
98    * This is an appearance property and thus only applies if 
99    * #GtkActivatable:use-action-appearance is %TRUE.
100    */
101   g_object_class_install_property (gobject_class,
102                                    PROP_DRAW_AS_RADIO,
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"),
106                                                          FALSE,
107                                                          GTK_PARAM_READWRITE));
108
109   /**
110    * GtkToggleAction:active:
111    *
112    * If the toggle action should be active in or not.
113    *
114    * Since: 2.10
115    */
116   g_object_class_install_property (gobject_class,
117                                    PROP_ACTIVE,
118                                    g_param_spec_boolean ("active",
119                                                          P_("Active"),
120                                                          P_("If the toggle action should be active in or not"),
121                                                          FALSE,
122                                                          GTK_PARAM_READWRITE));
123
124   action_signals[TOGGLED] =
125     g_signal_new (I_("toggled"),
126                   G_OBJECT_CLASS_TYPE (klass),
127                   G_SIGNAL_RUN_FIRST,
128                   G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
129                   NULL, NULL,
130                   g_cclosure_marshal_VOID__VOID,
131                   G_TYPE_NONE, 0);
132
133   g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
134 }
135
136 static void
137 gtk_toggle_action_init (GtkToggleAction *action)
138 {
139   action->private_data = GTK_TOGGLE_ACTION_GET_PRIVATE (action);
140   action->private_data->active = FALSE;
141   action->private_data->draw_as_radio = FALSE;
142 }
143
144 /**
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
150  *   action, or %NULL
151  *
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().
155  *
156  * Return value: a new #GtkToggleAction
157  *
158  * Since: 2.4
159  */
160 GtkToggleAction *
161 gtk_toggle_action_new (const gchar *name,
162                        const gchar *label,
163                        const gchar *tooltip,
164                        const gchar *stock_id)
165 {
166   g_return_val_if_fail (name != NULL, NULL);
167
168   return g_object_new (GTK_TYPE_TOGGLE_ACTION,
169                        "name", name,
170                        "label", label,
171                        "tooltip", tooltip,
172                        "stock-id", stock_id,
173                        NULL);
174 }
175
176 static void
177 get_property (GObject     *object,
178               guint        prop_id,
179               GValue      *value,
180               GParamSpec  *pspec)
181 {
182   GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
183   
184   switch (prop_id)
185     {
186     case PROP_DRAW_AS_RADIO:
187       g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
188       break;
189     case PROP_ACTIVE:
190       g_value_set_boolean (value, gtk_toggle_action_get_active (action));
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195     }
196 }
197
198 static void
199 set_property (GObject      *object,
200               guint         prop_id,
201               const GValue *value,
202               GParamSpec   *pspec)
203 {
204   GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
205   
206   switch (prop_id)
207     {
208     case PROP_DRAW_AS_RADIO:
209       gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
210       break;
211     case PROP_ACTIVE:
212       gtk_toggle_action_set_active (action, g_value_get_boolean (value));
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217     }
218 }
219
220 static void
221 gtk_toggle_action_activate (GtkAction *action)
222 {
223   GtkToggleAction *toggle_action;
224
225   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
226
227   toggle_action = GTK_TOGGLE_ACTION (action);
228
229   toggle_action->private_data->active = !toggle_action->private_data->active;
230
231   g_object_notify (G_OBJECT (action), "active");
232
233   gtk_toggle_action_toggled (toggle_action);
234 }
235
236 /**
237  * gtk_toggle_action_toggled:
238  * @action: the action object
239  *
240  * Emits the "toggled" signal on the toggle action.
241  *
242  * Since: 2.4
243  */
244 void
245 gtk_toggle_action_toggled (GtkToggleAction *action)
246 {
247   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
248
249   g_signal_emit (action, action_signals[TOGGLED], 0);
250 }
251
252 /**
253  * gtk_toggle_action_set_active:
254  * @action: the action object
255  * @is_active: whether the action should be checked or not
256  *
257  * Sets the checked state on the toggle action.
258  *
259  * Since: 2.4
260  */
261 void
262 gtk_toggle_action_set_active (GtkToggleAction *action, 
263                               gboolean         is_active)
264 {
265   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
266
267   is_active = is_active != FALSE;
268
269   if (action->private_data->active != is_active)
270     _gtk_action_emit_activate (GTK_ACTION (action));
271 }
272
273 /**
274  * gtk_toggle_action_get_active:
275  * @action: the action object
276  *
277  * Returns the checked state of the toggle action.
278
279  * Returns: the checked state of the toggle action
280  *
281  * Since: 2.4
282  */
283 gboolean
284 gtk_toggle_action_get_active (GtkToggleAction *action)
285 {
286   g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
287
288   return action->private_data->active;
289 }
290
291
292 /**
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 
296  *    action
297  *
298  * Sets whether the action should have proxies like a radio action.
299  *
300  * Since: 2.4
301  */
302 void
303 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action, 
304                                      gboolean         draw_as_radio)
305 {
306   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
307
308   draw_as_radio = draw_as_radio != FALSE;
309
310   if (action->private_data->draw_as_radio != draw_as_radio)
311     {
312       action->private_data->draw_as_radio = draw_as_radio;
313       
314       g_object_notify (G_OBJECT (action), "draw-as-radio");      
315     }
316 }
317
318 /**
319  * gtk_toggle_action_get_draw_as_radio:
320  * @action: the action object
321  *
322  * Returns whether the action should have proxies like a radio action.
323  *
324  * Returns: whether the action should have proxies like a radio action.
325  *
326  * Since: 2.4
327  */
328 gboolean
329 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
330 {
331   g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
332
333   return action->private_data->draw_as_radio;
334 }
335
336 static GtkWidget *
337 create_menu_item (GtkAction *action)
338 {
339   GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
340
341   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
342                        "draw-as-radio", toggle_action->private_data->draw_as_radio,
343                        NULL);
344 }
345
346 #define __GTK_TOGGLE_ACTION_C__
347 #include "gtkaliasdef.c"