]> Pileus Git - ~andy/gtk/blob - gtk/gtktoggleaction.c
[GI] Cosmetic cleanups of annotations and doc comments
[~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 "gtktoggletoolbutton.h"
36 #include "gtktogglebutton.h"
37 #include "gtkcheckmenuitem.h"
38 #include "gtkprivate.h"
39
40
41 /**
42  * SECTION:gtktoggleaction
43  * @Short_description: An action which can be toggled between two states
44  * @Title: GtkToggleAction
45  *
46  * A #GtkToggleAction corresponds roughly to a #GtkCheckMenuItem. It has an
47  * "active" state specifying whether the action has been checked or not.
48  */
49
50 struct _GtkToggleActionPrivate
51 {
52   guint active        : 1;
53   guint draw_as_radio : 1;
54 };
55
56 enum 
57 {
58   TOGGLED,
59   LAST_SIGNAL
60 };
61
62 enum {
63   PROP_0,
64   PROP_DRAW_AS_RADIO,
65   PROP_ACTIVE
66 };
67
68 G_DEFINE_TYPE (GtkToggleAction, gtk_toggle_action, GTK_TYPE_ACTION)
69
70 static void gtk_toggle_action_activate     (GtkAction       *action);
71 static void set_property                   (GObject         *object,
72                                             guint            prop_id,
73                                             const GValue    *value,
74                                             GParamSpec      *pspec);
75 static void get_property                   (GObject         *object,
76                                             guint            prop_id,
77                                             GValue          *value,
78                                             GParamSpec      *pspec);
79 static GtkWidget *create_menu_item         (GtkAction       *action);
80
81
82 static GObjectClass *parent_class = NULL;
83 static guint         action_signals[LAST_SIGNAL] = { 0 };
84
85 static void
86 gtk_toggle_action_class_init (GtkToggleActionClass *klass)
87 {
88   GObjectClass *gobject_class;
89   GtkActionClass *action_class;
90
91   parent_class = g_type_class_peek_parent (klass);
92   gobject_class = G_OBJECT_CLASS (klass);
93   action_class = GTK_ACTION_CLASS (klass);
94
95   gobject_class->set_property = set_property;
96   gobject_class->get_property = get_property;
97
98   action_class->activate = gtk_toggle_action_activate;
99
100   action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
101   action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
102
103   action_class->create_menu_item = create_menu_item;
104
105   klass->toggled = NULL;
106
107   /**
108    * GtkToggleAction:draw-as-radio:
109    *
110    * Whether the proxies for this action look like radio action proxies.
111    *
112    * This is an appearance property and thus only applies if 
113    * #GtkActivatable:use-action-appearance is %TRUE.
114    */
115   g_object_class_install_property (gobject_class,
116                                    PROP_DRAW_AS_RADIO,
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"),
120                                                          FALSE,
121                                                          GTK_PARAM_READWRITE));
122
123   /**
124    * GtkToggleAction:active:
125    *
126    * Whether the toggle action should be active.
127    *
128    * Since: 2.10
129    */
130   g_object_class_install_property (gobject_class,
131                                    PROP_ACTIVE,
132                                    g_param_spec_boolean ("active",
133                                                          P_("Active"),
134                                                          P_("Whether the toggle action should be active"),
135                                                          FALSE,
136                                                          GTK_PARAM_READWRITE));
137   /**
138    * GtkToggleAction::toggled:
139    * @toggleaction: the object which received the signal.
140    *
141    * Should be connected if you wish to perform an action
142    * whenever the #GtkToggleAction state is changed.
143    */
144   action_signals[TOGGLED] =
145     g_signal_new (I_("toggled"),
146                   G_OBJECT_CLASS_TYPE (klass),
147                   G_SIGNAL_RUN_FIRST,
148                   G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
149                   NULL, NULL,
150                   g_cclosure_marshal_VOID__VOID,
151                   G_TYPE_NONE, 0);
152
153   g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
154 }
155
156 static void
157 gtk_toggle_action_init (GtkToggleAction *action)
158 {
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;
164 }
165
166 /**
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,
170  *         or %NULL
171  * @tooltip: (allow-none): A tooltip for the action, or %NULL
172  * @stock_id: (allow-none): The stock icon to display in widgets representing
173  *            the action, or %NULL
174  *
175  * Creates a new #GtkToggleAction object. To add the action to
176  * a #GtkActionGroup and set the accelerator for the action,
177  * call gtk_action_group_add_action_with_accel().
178  *
179  * Return value: a new #GtkToggleAction
180  *
181  * Since: 2.4
182  */
183 GtkToggleAction *
184 gtk_toggle_action_new (const gchar *name,
185                        const gchar *label,
186                        const gchar *tooltip,
187                        const gchar *stock_id)
188 {
189   g_return_val_if_fail (name != NULL, NULL);
190
191   return g_object_new (GTK_TYPE_TOGGLE_ACTION,
192                        "name", name,
193                        "label", label,
194                        "tooltip", tooltip,
195                        "stock-id", stock_id,
196                        NULL);
197 }
198
199 static void
200 get_property (GObject     *object,
201               guint        prop_id,
202               GValue      *value,
203               GParamSpec  *pspec)
204 {
205   GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
206   
207   switch (prop_id)
208     {
209     case PROP_DRAW_AS_RADIO:
210       g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
211       break;
212     case PROP_ACTIVE:
213       g_value_set_boolean (value, gtk_toggle_action_get_active (action));
214       break;
215     default:
216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217       break;
218     }
219 }
220
221 static void
222 set_property (GObject      *object,
223               guint         prop_id,
224               const GValue *value,
225               GParamSpec   *pspec)
226 {
227   GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
228   
229   switch (prop_id)
230     {
231     case PROP_DRAW_AS_RADIO:
232       gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
233       break;
234     case PROP_ACTIVE:
235       gtk_toggle_action_set_active (action, g_value_get_boolean (value));
236       break;
237     default:
238       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239       break;
240     }
241 }
242
243 static void
244 gtk_toggle_action_activate (GtkAction *action)
245 {
246   GtkToggleAction *toggle_action;
247
248   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
249
250   toggle_action = GTK_TOGGLE_ACTION (action);
251
252   toggle_action->private_data->active = !toggle_action->private_data->active;
253
254   g_object_notify (G_OBJECT (action), "active");
255
256   gtk_toggle_action_toggled (toggle_action);
257 }
258
259 /**
260  * gtk_toggle_action_toggled:
261  * @action: the action object
262  *
263  * Emits the "toggled" signal on the toggle action.
264  *
265  * Since: 2.4
266  */
267 void
268 gtk_toggle_action_toggled (GtkToggleAction *action)
269 {
270   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
271
272   g_signal_emit (action, action_signals[TOGGLED], 0);
273 }
274
275 /**
276  * gtk_toggle_action_set_active:
277  * @action: the action object
278  * @is_active: whether the action should be checked or not
279  *
280  * Sets the checked state on the toggle action.
281  *
282  * Since: 2.4
283  */
284 void
285 gtk_toggle_action_set_active (GtkToggleAction *action, 
286                               gboolean         is_active)
287 {
288   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
289
290   is_active = is_active != FALSE;
291
292   if (action->private_data->active != is_active)
293     _gtk_action_emit_activate (GTK_ACTION (action));
294 }
295
296 /**
297  * gtk_toggle_action_get_active:
298  * @action: the action object
299  *
300  * Returns the checked state of the toggle action.
301  *
302  * Returns: the checked state of the toggle action
303  *
304  * Since: 2.4
305  */
306 gboolean
307 gtk_toggle_action_get_active (GtkToggleAction *action)
308 {
309   g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
310
311   return action->private_data->active;
312 }
313
314
315 /**
316  * gtk_toggle_action_set_draw_as_radio:
317  * @action: the action object
318  * @draw_as_radio: whether the action should have proxies like a radio 
319  *    action
320  *
321  * Sets whether the action should have proxies like a radio action.
322  *
323  * Since: 2.4
324  */
325 void
326 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action, 
327                                      gboolean         draw_as_radio)
328 {
329   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
330
331   draw_as_radio = draw_as_radio != FALSE;
332
333   if (action->private_data->draw_as_radio != draw_as_radio)
334     {
335       action->private_data->draw_as_radio = draw_as_radio;
336       
337       g_object_notify (G_OBJECT (action), "draw-as-radio");      
338     }
339 }
340
341 /**
342  * gtk_toggle_action_get_draw_as_radio:
343  * @action: the action object
344  *
345  * Returns whether the action should have proxies like a radio action.
346  *
347  * Returns: whether the action should have proxies like a radio action.
348  *
349  * Since: 2.4
350  */
351 gboolean
352 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
353 {
354   g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
355
356   return action->private_data->draw_as_radio;
357 }
358
359 static GtkWidget *
360 create_menu_item (GtkAction *action)
361 {
362   GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
363
364   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
365                        "draw-as-radio", toggle_action->private_data->draw_as_radio,
366                        NULL);
367 }
368
369
370 /* Private */
371
372 /*
373  * _gtk_toggle_action_set_active:
374  * @toggle_action: a #GtkToggleAction
375  * @is_active: whether the action is active or not
376  *
377  * Sets the #GtkToggleAction:active property directly. This function does
378  * not emit signals or notifications: it is left to the caller to do so.
379  */
380 void
381 _gtk_toggle_action_set_active (GtkToggleAction *toggle_action,
382                                gboolean         is_active)
383 {
384   GtkToggleActionPrivate *priv = toggle_action->private_data;
385
386   priv->active = is_active;
387 }