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