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