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