]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiomenuitem.c
stylecontext: If we force invalidate, invalidate all properties
[~andy/gtk] / gtk / gtkradiomenuitem.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include "config.h"
26 #include "gtkaccellabel.h"
27 #include "gtkmarshalers.h"
28 #include "gtkradiomenuitem.h"
29 #include "gtkactivatable.h"
30 #include "gtkprivate.h"
31 #include "gtkintl.h"
32 #include "a11y/gtkradiomenuitemaccessible.h"
33
34 /**
35  * SECTION:gtkradiomenuitem
36  * @Short_description: A choice from multiple check menu items
37  * @Title: GtkRadioMenuItem
38  * @See_also: #GtkMenuItem, #GtkCheckMenuItem
39  *
40  * A radio menu item is a check menu item that belongs to a group. At each
41  * instant exactly one of the radio menu items from a group is selected.
42  *
43  * The group list does not need to be freed, as each #GtkRadioMenuItem will
44  * remove itself and its list item when it is destroyed.
45  *
46  * The correct way to create a group of radio menu items is approximatively
47  * this:
48  *
49  * <example>
50  * <title>How to create a group of radio menu items.</title>
51  * <programlisting>
52  * GSList *group = NULL;
53  * GtkWidget *item;
54  * gint i;
55  *
56  * for (i = 0; i < 5; i++)
57  * {
58  *   item = gtk_radio_menu_item_new_with_label (group, "This is an example");
59  *   group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
60  *   if (i == 1)
61  *     gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
62  * }
63  * </programlisting>
64  * </example>
65  */
66
67
68 struct _GtkRadioMenuItemPrivate
69 {
70   GSList *group;
71 };
72
73 enum {
74   PROP_0,
75   PROP_GROUP
76 };
77
78
79 static void gtk_radio_menu_item_destroy        (GtkWidget             *widget);
80 static void gtk_radio_menu_item_activate       (GtkMenuItem           *menu_item);
81 static void gtk_radio_menu_item_set_property   (GObject               *object,
82                                                 guint                  prop_id,
83                                                 const GValue          *value,
84                                                 GParamSpec            *pspec);
85 static void gtk_radio_menu_item_get_property   (GObject               *object,
86                                                 guint                  prop_id,
87                                                 GValue                *value,
88                                                 GParamSpec            *pspec);
89
90 static guint group_changed_signal = 0;
91
92 G_DEFINE_TYPE (GtkRadioMenuItem, gtk_radio_menu_item, GTK_TYPE_CHECK_MENU_ITEM)
93
94 /**
95  * gtk_radio_menu_item_new:
96  * @group: (element-type GtkRadioMenuItem): the group to which the
97  *    radio menu item is to be attached
98  *
99  * Creates a new #GtkRadioMenuItem.
100  *
101  * Returns: a new #GtkRadioMenuItem
102  */
103 GtkWidget*
104 gtk_radio_menu_item_new (GSList *group)
105 {
106   GtkRadioMenuItem *radio_menu_item;
107
108   radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
109
110   gtk_radio_menu_item_set_group (radio_menu_item, group);
111
112   return GTK_WIDGET (radio_menu_item);
113 }
114
115 static void
116 gtk_radio_menu_item_set_property (GObject      *object,
117                                   guint         prop_id,
118                                   const GValue *value,
119                                   GParamSpec   *pspec)
120 {
121   GtkRadioMenuItem *radio_menu_item;
122
123   radio_menu_item = GTK_RADIO_MENU_ITEM (object);
124
125   switch (prop_id)
126     {
127       GSList *slist;
128
129     case PROP_GROUP:
130       slist = g_value_get_object (value);
131       if (slist)
132         slist = gtk_radio_menu_item_get_group ((GtkRadioMenuItem*) g_value_get_object (value));
133       gtk_radio_menu_item_set_group (radio_menu_item, slist);
134       break;
135     default:
136       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137       break;
138     }
139 }
140
141 static void
142 gtk_radio_menu_item_get_property (GObject    *object,
143                                   guint       prop_id,
144                                   GValue     *value,
145                                   GParamSpec *pspec)
146 {
147   switch (prop_id)
148     {
149     default:
150       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151       break;
152     }
153 }
154
155 /**
156  * gtk_radio_menu_item_set_group:
157  * @radio_menu_item: a #GtkRadioMenuItem.
158  * @group: (element-type GtkRadioMenuItem): the new group.
159  *
160  * Sets the group of a radio menu item, or changes it.
161  */
162 void
163 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
164                                GSList           *group)
165 {
166   GtkRadioMenuItemPrivate *priv;
167   GtkWidget *old_group_singleton = NULL;
168   GtkWidget *new_group_singleton = NULL;
169   
170   g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
171   g_return_if_fail (!g_slist_find (group, radio_menu_item));
172
173   priv = radio_menu_item->priv;
174
175   if (priv->group)
176     {
177       GSList *slist;
178
179       priv->group = g_slist_remove (priv->group, radio_menu_item);
180
181       if (priv->group && !priv->group->next)
182         old_group_singleton = g_object_ref (priv->group->data);
183
184       for (slist = priv->group; slist; slist = slist->next)
185         {
186           GtkRadioMenuItem *tmp_item;
187           
188           tmp_item = slist->data;
189
190           tmp_item->priv->group = priv->group;
191         }
192     }
193   
194   if (group && !group->next)
195     new_group_singleton = g_object_ref (group->data);
196
197   priv->group = g_slist_prepend (group, radio_menu_item);
198
199   if (group)
200     {
201       GSList *slist;
202       
203       for (slist = group; slist; slist = slist->next)
204         {
205           GtkRadioMenuItem *tmp_item;
206           
207           tmp_item = slist->data;
208
209           tmp_item->priv->group = priv->group;
210         }
211     }
212   else
213     {
214       _gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
215       /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
216        */
217     }
218
219   g_object_ref (radio_menu_item);
220
221   g_object_notify (G_OBJECT (radio_menu_item), "group");
222   g_signal_emit (radio_menu_item, group_changed_signal, 0);
223   if (old_group_singleton)
224     {
225       g_signal_emit (old_group_singleton, group_changed_signal, 0);
226       g_object_unref (old_group_singleton);
227     }
228   if (new_group_singleton)
229     {
230       g_signal_emit (new_group_singleton, group_changed_signal, 0);
231       g_object_unref (new_group_singleton);
232     }
233
234   g_object_unref (radio_menu_item);
235 }
236
237
238 /**
239  * gtk_radio_menu_item_new_with_label:
240  * @group: (element-type GtkRadioMenuItem) (transfer full):
241  * @label: the text for the label
242  *
243  * Creates a new #GtkRadioMenuItem whose child is a simple #GtkLabel.
244  *
245  * Returns: (transfer none): A new #GtkRadioMenuItem
246  */
247 GtkWidget*
248 gtk_radio_menu_item_new_with_label (GSList *group,
249                                     const gchar *label)
250 {
251   return g_object_new (GTK_TYPE_RADIO_MENU_ITEM,
252           "group", (group) ? group->data : NULL,
253           "label", label,
254           NULL);
255 }
256
257
258 /**
259  * gtk_radio_menu_item_new_with_mnemonic:
260  * @group: (element-type GtkRadioMenuItem): group the radio menu item is inside
261  * @label: the text of the button, with an underscore in front of the
262  *         mnemonic character
263  *
264  * Creates a new #GtkRadioMenuItem containing a label. The label
265  * will be created using gtk_label_new_with_mnemonic(), so underscores
266  * in @label indicate the mnemonic for the menu item.
267  *
268  * Returns: a new #GtkRadioMenuItem
269  */
270 GtkWidget*
271 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
272                                        const gchar *label)
273 {
274   return g_object_new (GTK_TYPE_RADIO_MENU_ITEM,
275           "group", (group) ? group->data : NULL,
276           "label", label,
277           "use-underline", TRUE,
278           NULL);
279 }
280
281 /**
282  * gtk_radio_menu_item_new_from_widget: (constructor)
283  * @group: An existing #GtkRadioMenuItem
284  *
285  * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
286  *
287  * Return value: (transfer none): The new #GtkRadioMenuItem
288  *
289  * Since: 2.4
290  **/
291 GtkWidget *
292 gtk_radio_menu_item_new_from_widget (GtkRadioMenuItem *group)
293 {
294   GSList *list = NULL;
295   
296   g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
297
298   if (group)
299     list = gtk_radio_menu_item_get_group (group);
300   
301   return gtk_radio_menu_item_new (list);
302 }
303
304 /**
305  * gtk_radio_menu_item_new_with_mnemonic_from_widget: (constructor)
306  * @group: An existing #GtkRadioMenuItem
307  * @label: the text of the button, with an underscore in front of the
308  *         mnemonic character
309  *
310  * Creates a new GtkRadioMenuItem containing a label. The label will be
311  * created using gtk_label_new_with_mnemonic(), so underscores in label
312  * indicate the mnemonic for the menu item.
313  *
314  * The new #GtkRadioMenuItem is added to the same group as @group.
315  *
316  * Return value: (transfer none): The new #GtkRadioMenuItem
317  *
318  * Since: 2.4
319  **/
320 GtkWidget *
321 gtk_radio_menu_item_new_with_mnemonic_from_widget (GtkRadioMenuItem *group,
322                                                    const gchar *label)
323 {
324   GSList *list = NULL;
325
326   g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
327
328   if (group)
329     list = gtk_radio_menu_item_get_group (group);
330
331   return gtk_radio_menu_item_new_with_mnemonic (list, label);
332 }
333
334 /**
335  * gtk_radio_menu_item_new_with_label_from_widget: (constructor)
336  * @group: an existing #GtkRadioMenuItem
337  * @label: the text for the label
338  *
339  * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
340  * The new #GtkRadioMenuItem is added to the same group as @group.
341  *
342  * Return value: (transfer none): The new #GtkRadioMenuItem
343  *
344  * Since: 2.4
345  **/
346 GtkWidget *
347 gtk_radio_menu_item_new_with_label_from_widget (GtkRadioMenuItem *group,
348                                                 const gchar *label)
349 {
350   GSList *list = NULL;
351
352   g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (group), NULL);
353
354   if (group)
355     list = gtk_radio_menu_item_get_group (group);
356
357   return gtk_radio_menu_item_new_with_label (list, label);
358 }
359
360 /**
361  * gtk_radio_menu_item_get_group:
362  * @radio_menu_item: a #GtkRadioMenuItem
363  *
364  * Returns the group to which the radio menu item belongs, as a #GList of
365  * #GtkRadioMenuItem. The list belongs to GTK+ and should not be freed.
366  *
367  * Returns: (element-type GtkRadioMenuItem) (transfer none): the group
368  *     of @radio_menu_item
369  */
370 GSList*
371 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
372 {
373   g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
374
375   return radio_menu_item->priv->group;
376 }
377
378 static void
379 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
380 {
381   GObjectClass *gobject_class;
382   GtkWidgetClass *widget_class;
383   GtkMenuItemClass *menu_item_class;
384
385   gobject_class = G_OBJECT_CLASS (klass);
386   widget_class = GTK_WIDGET_CLASS (klass);
387   menu_item_class = GTK_MENU_ITEM_CLASS (klass);
388
389   gobject_class->set_property = gtk_radio_menu_item_set_property;
390   gobject_class->get_property = gtk_radio_menu_item_get_property;
391
392   widget_class->destroy = gtk_radio_menu_item_destroy;
393
394   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_RADIO_MENU_ITEM_ACCESSIBLE);
395
396   menu_item_class->activate = gtk_radio_menu_item_activate;
397
398   /**
399    * GtkRadioMenuItem:group:
400    * 
401    * The radio menu item whose group this widget belongs to.
402    * 
403    * Since: 2.8
404    */
405   g_object_class_install_property (gobject_class,
406                                    PROP_GROUP,
407                                    g_param_spec_object ("group",
408                                                         P_("Group"),
409                                                         P_("The radio menu item whose group this widget belongs to."),
410                                                         GTK_TYPE_RADIO_MENU_ITEM,
411                                                         GTK_PARAM_WRITABLE));
412
413   /**
414    * GtkStyle::group-changed:
415    * @style: the object which received the signal
416    *
417    * Emitted when the group of radio menu items that a radio menu item belongs
418    * to changes. This is emitted when a radio menu item switches from
419    * being alone to being part of a group of 2 or more menu items, or
420    * vice-versa, and when a button is moved from one group of 2 or
421    * more menu items ton a different one, but not when the composition
422    * of the group that a menu item belongs to changes.
423    *
424    * Since: 2.4
425    */
426   group_changed_signal = g_signal_new (I_("group-changed"),
427                                        G_OBJECT_CLASS_TYPE (gobject_class),
428                                        G_SIGNAL_RUN_FIRST,
429                                        G_STRUCT_OFFSET (GtkRadioMenuItemClass, group_changed),
430                                        NULL, NULL,
431                                        _gtk_marshal_VOID__VOID,
432                                        G_TYPE_NONE, 0);
433
434   g_type_class_add_private (klass, sizeof (GtkRadioMenuItemPrivate));
435 }
436
437 static void
438 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
439 {
440   GtkRadioMenuItemPrivate *priv;
441
442   radio_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (radio_menu_item,
443                                                        GTK_TYPE_RADIO_MENU_ITEM,
444                                                        GtkRadioMenuItemPrivate);
445   priv = radio_menu_item->priv;
446
447   priv->group = g_slist_prepend (NULL, radio_menu_item);
448   gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (radio_menu_item), TRUE);
449 }
450
451 static void
452 gtk_radio_menu_item_destroy (GtkWidget *widget)
453 {
454   GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (widget);
455   GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
456   GtkWidget *old_group_singleton = NULL;
457   GtkRadioMenuItem *tmp_menu_item;
458   GSList *tmp_list;
459   gboolean was_in_group;
460
461   was_in_group = priv->group && priv->group->next;
462
463   priv->group = g_slist_remove (priv->group, radio_menu_item);
464   if (priv->group && !priv->group->next)
465     old_group_singleton = priv->group->data;
466
467   tmp_list = priv->group;
468
469   while (tmp_list)
470     {
471       tmp_menu_item = tmp_list->data;
472       tmp_list = tmp_list->next;
473
474       tmp_menu_item->priv->group = priv->group;
475     }
476
477   /* this radio menu item is no longer in the group */
478   priv->group = NULL;
479   
480   if (old_group_singleton)
481     g_signal_emit (old_group_singleton, group_changed_signal, 0);
482   if (was_in_group)
483     g_signal_emit (radio_menu_item, group_changed_signal, 0);
484
485   GTK_WIDGET_CLASS (gtk_radio_menu_item_parent_class)->destroy (widget);
486 }
487
488 static void
489 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
490 {
491   GtkRadioMenuItem *radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
492   GtkRadioMenuItemPrivate *priv = radio_menu_item->priv;
493   GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
494   GtkCheckMenuItem *tmp_menu_item;
495   GtkAction        *action;
496   GSList *tmp_list;
497   gboolean active;
498   gint toggled;
499
500   action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (menu_item));
501   if (action && gtk_menu_item_get_submenu (menu_item) == NULL)
502     gtk_action_activate (action);
503
504   toggled = FALSE;
505
506   active = gtk_check_menu_item_get_active (check_menu_item);
507   if (active)
508     {
509       tmp_menu_item = NULL;
510       tmp_list = priv->group;
511
512       while (tmp_list)
513         {
514           tmp_menu_item = tmp_list->data;
515           tmp_list = tmp_list->next;
516
517           if (gtk_check_menu_item_get_active (tmp_menu_item) &&
518               tmp_menu_item != check_menu_item)
519             break;
520
521           tmp_menu_item = NULL;
522         }
523
524       if (tmp_menu_item)
525         {
526           toggled = TRUE;
527           _gtk_check_menu_item_set_active (check_menu_item, !active);
528         }
529     }
530   else
531     {
532       toggled = TRUE;
533       _gtk_check_menu_item_set_active (check_menu_item, !active);
534
535       tmp_list = priv->group;
536       while (tmp_list)
537         {
538           tmp_menu_item = tmp_list->data;
539           tmp_list = tmp_list->next;
540
541           if (gtk_check_menu_item_get_active (tmp_menu_item) &&
542               tmp_menu_item != check_menu_item)
543             {
544               gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
545               break;
546             }
547         }
548     }
549
550   if (toggled)
551     {
552       gtk_check_menu_item_toggled (check_menu_item);
553     }
554
555   gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));
556 }