]> Pileus Git - ~andy/gtk/blob - gtk/gtktoggleaction.c
Include "config.h" instead of <config.h> Command used: find -name
[~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 gtk_toggle_action_real_toggled (GtkToggleAction *action);
58 static void connect_proxy                  (GtkAction       *action,
59                                             GtkWidget       *proxy);
60 static void disconnect_proxy               (GtkAction       *action,
61                                             GtkWidget       *proxy);
62 static void set_property                   (GObject         *object,
63                                             guint            prop_id,
64                                             const GValue    *value,
65                                             GParamSpec      *pspec);
66 static void get_property                   (GObject         *object,
67                                             guint            prop_id,
68                                             GValue          *value,
69                                             GParamSpec      *pspec);
70 static GtkWidget *create_menu_item         (GtkAction       *action);
71
72
73 static GObjectClass *parent_class = NULL;
74 static guint         action_signals[LAST_SIGNAL] = { 0 };
75
76 static void
77 gtk_toggle_action_class_init (GtkToggleActionClass *klass)
78 {
79   GObjectClass *gobject_class;
80   GtkActionClass *action_class;
81
82   parent_class = g_type_class_peek_parent (klass);
83   gobject_class = G_OBJECT_CLASS (klass);
84   action_class = GTK_ACTION_CLASS (klass);
85
86   gobject_class->set_property = set_property;
87   gobject_class->get_property = get_property;
88
89   action_class->activate = gtk_toggle_action_activate;
90   action_class->connect_proxy = connect_proxy;
91   action_class->disconnect_proxy = disconnect_proxy;
92
93   action_class->menu_item_type = GTK_TYPE_CHECK_MENU_ITEM;
94   action_class->toolbar_item_type = GTK_TYPE_TOGGLE_TOOL_BUTTON;
95
96   action_class->create_menu_item = create_menu_item;
97
98   klass->toggled = gtk_toggle_action_real_toggled;
99
100   g_object_class_install_property (gobject_class,
101                                    PROP_DRAW_AS_RADIO,
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"),
105                                                          FALSE,
106                                                          GTK_PARAM_READWRITE));
107
108   /**
109    * GtkToggleAction:active:
110    *
111    * If the toggle action should be active in or not.
112    *
113    * Since: 2.10
114    */
115   g_object_class_install_property (gobject_class,
116                                    PROP_ACTIVE,
117                                    g_param_spec_boolean ("active",
118                                                          P_("Active"),
119                                                          P_("If the toggle action should be active in or not"),
120                                                          FALSE,
121                                                          GTK_PARAM_READWRITE));
122
123   action_signals[TOGGLED] =
124     g_signal_new (I_("toggled"),
125                   G_OBJECT_CLASS_TYPE (klass),
126                   G_SIGNAL_RUN_FIRST,
127                   G_STRUCT_OFFSET (GtkToggleActionClass, toggled),
128                   NULL, NULL,
129                   g_cclosure_marshal_VOID__VOID,
130                   G_TYPE_NONE, 0);
131
132   g_type_class_add_private (gobject_class, sizeof (GtkToggleActionPrivate));
133 }
134
135 static void
136 gtk_toggle_action_init (GtkToggleAction *action)
137 {
138   action->private_data = GTK_TOGGLE_ACTION_GET_PRIVATE (action);
139   action->private_data->active = FALSE;
140   action->private_data->draw_as_radio = FALSE;
141 }
142
143 /**
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
149  *   action, or %NULL
150  *
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().
154  *
155  * Return value: a new #GtkToggleAction
156  *
157  * Since: 2.4
158  */
159 GtkToggleAction *
160 gtk_toggle_action_new (const gchar *name,
161                        const gchar *label,
162                        const gchar *tooltip,
163                        const gchar *stock_id)
164 {
165   g_return_val_if_fail (name != NULL, NULL);
166
167   return g_object_new (GTK_TYPE_TOGGLE_ACTION,
168                        "name", name,
169                        "label", label,
170                        "tooltip", tooltip,
171                        "stock-id", stock_id,
172                        NULL);
173 }
174
175 static void
176 get_property (GObject     *object,
177               guint        prop_id,
178               GValue      *value,
179               GParamSpec  *pspec)
180 {
181   GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
182   
183   switch (prop_id)
184     {
185     case PROP_DRAW_AS_RADIO:
186       g_value_set_boolean (value, gtk_toggle_action_get_draw_as_radio (action));
187       break;
188     case PROP_ACTIVE:
189       g_value_set_boolean (value, gtk_toggle_action_get_active (action));
190       break;
191     default:
192       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193       break;
194     }
195 }
196
197 static void
198 set_property (GObject      *object,
199               guint         prop_id,
200               const GValue *value,
201               GParamSpec   *pspec)
202 {
203   GtkToggleAction *action = GTK_TOGGLE_ACTION (object);
204   
205   switch (prop_id)
206     {
207     case PROP_DRAW_AS_RADIO:
208       gtk_toggle_action_set_draw_as_radio (action, g_value_get_boolean (value));
209       break;
210     case PROP_ACTIVE:
211       gtk_toggle_action_set_active (action, g_value_get_boolean (value));
212       break;
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216     }
217 }
218
219 static void
220 gtk_toggle_action_activate (GtkAction *action)
221 {
222   GtkToggleAction *toggle_action;
223
224   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
225
226   toggle_action = GTK_TOGGLE_ACTION (action);
227
228   toggle_action->private_data->active = !toggle_action->private_data->active;
229
230   g_object_notify (G_OBJECT (action), "active");
231
232   gtk_toggle_action_toggled (toggle_action);
233 }
234
235 static void
236 gtk_toggle_action_real_toggled (GtkToggleAction *action)
237 {
238   GSList *slist;
239
240   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
241
242   for (slist = gtk_action_get_proxies (GTK_ACTION (action)); slist; slist = slist->next)
243     {
244       GtkWidget *proxy = slist->data;
245
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);
256       else {
257         g_warning ("Don't know how to toggle `%s' widgets",
258                    G_OBJECT_TYPE_NAME (proxy));
259       }
260       gtk_action_unblock_activate_from (GTK_ACTION (action), proxy);
261     }
262 }
263
264 static void
265 connect_proxy (GtkAction *action, 
266                GtkWidget *proxy)
267 {
268   GtkToggleAction *toggle_action;
269
270   toggle_action = GTK_TOGGLE_ACTION (action);
271
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);
282
283   (* GTK_ACTION_CLASS (parent_class)->connect_proxy) (action, proxy);
284 }
285
286 static void
287 disconnect_proxy (GtkAction *action, 
288                   GtkWidget *proxy)
289 {
290   (* GTK_ACTION_CLASS (parent_class)->disconnect_proxy) (action, proxy);
291 }
292
293 /**
294  * gtk_toggle_action_toggled:
295  * @action: the action object
296  *
297  * Emits the "toggled" signal on the toggle action.
298  *
299  * Since: 2.4
300  */
301 void
302 gtk_toggle_action_toggled (GtkToggleAction *action)
303 {
304   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
305
306   g_signal_emit (action, action_signals[TOGGLED], 0);
307 }
308
309 /**
310  * gtk_toggle_action_set_active:
311  * @action: the action object
312  * @is_active: whether the action should be checked or not
313  *
314  * Sets the checked state on the toggle action.
315  *
316  * Since: 2.4
317  */
318 void
319 gtk_toggle_action_set_active (GtkToggleAction *action, 
320                               gboolean         is_active)
321 {
322   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
323
324   is_active = is_active != FALSE;
325
326   if (action->private_data->active != is_active)
327     {
328       _gtk_action_emit_activate (GTK_ACTION (action));
329     }
330 }
331
332 /**
333  * gtk_toggle_action_get_active:
334  * @action: the action object
335  *
336  * Returns the checked state of the toggle action.
337
338  * Returns: the checked state of the toggle action
339  *
340  * Since: 2.4
341  */
342 gboolean
343 gtk_toggle_action_get_active (GtkToggleAction *action)
344 {
345   g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
346
347   return action->private_data->active;
348 }
349
350
351 /**
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 
355  *    action
356  *
357  * Sets whether the action should have proxies like a radio action.
358  *
359  * Since: 2.4
360  */
361 void
362 gtk_toggle_action_set_draw_as_radio (GtkToggleAction *action, 
363                                      gboolean         draw_as_radio)
364 {
365   g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
366
367   draw_as_radio = draw_as_radio != FALSE;
368
369   if (action->private_data->draw_as_radio != draw_as_radio)
370     {
371       action->private_data->draw_as_radio = draw_as_radio;
372       
373       g_object_notify (G_OBJECT (action), "draw-as-radio");      
374     }
375 }
376
377 /**
378  * gtk_toggle_action_get_draw_as_radio:
379  * @action: the action object
380  *
381  * Returns whether the action should have proxies like a radio action.
382  *
383  * Returns: whether the action should have proxies like a radio action.
384  *
385  * Since: 2.4
386  */
387 gboolean
388 gtk_toggle_action_get_draw_as_radio (GtkToggleAction *action)
389 {
390   g_return_val_if_fail (GTK_IS_TOGGLE_ACTION (action), FALSE);
391
392   return action->private_data->draw_as_radio;
393 }
394
395 static GtkWidget *
396 create_menu_item (GtkAction *action)
397 {
398   GtkToggleAction *toggle_action = GTK_TOGGLE_ACTION (action);
399
400   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, 
401                        "draw-as-radio", toggle_action->private_data->draw_as_radio,
402                        NULL);
403 }
404
405 #define __GTK_TOGGLE_ACTION_C__
406 #include "gtkaliasdef.c"