]> Pileus Git - ~andy/gtk/blob - gtk/gtktoggleaction.c
Add _gtk_toggle_action_set_active() internal function
[~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    * If the toggle action should be active in or not.
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_("If the toggle action should be active in or not"),
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, or %NULL
170  * @tooltip: (allow-none): A tooltip for the action, or %NULL
171  * @stock_id: The stock icon to display in widgets representing the
172  *   action, or %NULL
173  *
174  * Creates a new #GtkToggleAction object. To add the action to
175  * a #GtkActionGroup and set the accelerator for the action,
176  * call gtk_action_group_add_action_with_accel().
177  *
178  * Return value: a new #GtkToggleAction
179  *
180  * Since: 2.4
181  */
182 GtkToggleAction *
183 gtk_toggle_action_new (const gchar *name,
184                        const gchar *label,
185                        const gchar *tooltip,
186                        const gchar *stock_id)
187 {
188   g_return_val_if_fail (name != NULL, NULL);
189
190   return g_object_new (GTK_TYPE_TOGGLE_ACTION,
191                        "name", name,
192                        "label", label,
193                        "tooltip", tooltip,
194                        "stock-id", stock_id,
195                        NULL);
196 }
197
198 static void
199 get_property (GObject     *object,
200               guint        prop_id,
201               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       g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
210       break;
211     case PROP_ACTIVE:
212       g_value_set_boolean (value, gtk_toggle_action_get_active (action));
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217     }
218 }
219
220 static void
221 set_property (GObject      *object,
222               guint         prop_id,
223               const GValue *value,
224               GParamSpec   *pspec)
225 {
226   GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
227   
228   switch (prop_id)
229     {
230     case PROP_DRAW_AS_RADIO:
231       gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
232       break;
233     case PROP_ACTIVE:
234       gtk_toggle_action_set_active (action, g_value_get_boolean (value));
235       break;
236     default:
237       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238       break;
239     }
240 }
241
242 static void
243 gtk_toggle_action_activate (GtkAction *action)
244 {
245   GtkToggleAction *toggle_action;
246
247   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
248
249   toggle_action = GTK_TOGGLE_ACTION (action);
250
251   toggle_action->private_data->active = !toggle_action->private_data->active;
252
253   g_object_notify (G_OBJECT (action), "active");
254
255   gtk_toggle_action_toggled (toggle_action);
256 }
257
258 /**
259  * gtk_toggle_action_toggled:
260  * @action: the action object
261  *
262  * Emits the "toggled" signal on the toggle action.
263  *
264  * Since: 2.4
265  */
266 void
267 gtk_toggle_action_toggled (GtkToggleAction *action)
268 {
269   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
270
271   g_signal_emit (action, action_signals[TOGGLED], 0);
272 }
273
274 /**
275  * gtk_toggle_action_set_active:
276  * @action: the action object
277  * @is_active: whether the action should be checked or not
278  *
279  * Sets the checked state on the toggle action.
280  *
281  * Since: 2.4
282  */
283 void
284 gtk_toggle_action_set_active (GtkToggleAction *action, 
285                               gboolean         is_active)
286 {
287   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
288
289   is_active = is_active != FALSE;
290
291   if (action->private_data->active != is_active)
292     _gtk_action_emit_activate (GTK_ACTION (action));
293 }
294
295 /**
296  * gtk_toggle_action_get_active:
297  * @action: the action object
298  *
299  * Returns the checked state of the toggle action.
300  *
301  * Returns: the checked state of the toggle action
302  *
303  * Since: 2.4
304  */
305 gboolean
306 gtk_toggle_action_get_active (GtkToggleAction *action)
307 {
308   g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
309
310   return action->private_data->active;
311 }
312
313
314 /**
315  * gtk_toggle_action_set_draw_as_radio:
316  * @action: the action object
317  * @draw_as_radio: whether the action should have proxies like a radio 
318  *    action
319  *
320  * Sets whether the action should have proxies like a radio action.
321  *
322  * Since: 2.4
323  */
324 void
325 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action, 
326                                      gboolean         draw_as_radio)
327 {
328   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
329
330   draw_as_radio = draw_as_radio != FALSE;
331
332   if (action->private_data->draw_as_radio != draw_as_radio)
333     {
334       action->private_data->draw_as_radio = draw_as_radio;
335       
336       g_object_notify (G_OBJECT (action), "draw-as-radio");      
337     }
338 }
339
340 /**
341  * gtk_toggle_action_get_draw_as_radio:
342  * @action: the action object
343  *
344  * Returns whether the action should have proxies like a radio action.
345  *
346  * Returns: whether the action should have proxies like a radio action.
347  *
348  * Since: 2.4
349  */
350 gboolean
351 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
352 {
353   g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
354
355   return action->private_data->draw_as_radio;
356 }
357
358 static GtkWidget *
359 create_menu_item (GtkAction *action)
360 {
361   GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
362
363   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
364                        "draw-as-radio", toggle_action->private_data->draw_as_radio,
365                        NULL);
366 }
367
368
369 /* Private */
370
371 /*
372  * _gtk_toggle_action_set_active:
373  * @toggle_action: a #GtkToggleAction
374  * @is_active: whether the action is active or not
375  *
376  * Sets the #GtkToggleAction:active property directly. This function does
377  * not emit signals or notifications: it is left to the caller to do so.
378  */
379 void
380 _gtk_toggle_action_set_active (GtkToggleAction *toggle_action,
381                                gboolean         is_active)
382 {
383   GtkToggleActionPrivate *priv = toggle_action->private_data;
384
385   priv->active = is_active;
386 }