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