]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiomenuitem.c
Added notice to look in AUTHORS and ChangeLog files for a list of changes.
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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-1999.  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 "gtkaccellabel.h"
28 #include "gtkradiomenuitem.h"
29
30
31 static void gtk_radio_menu_item_class_init     (GtkRadioMenuItemClass *klass);
32 static void gtk_radio_menu_item_init           (GtkRadioMenuItem      *radio_menu_item);
33 static void gtk_radio_menu_item_activate       (GtkMenuItem           *menu_item);
34 static void gtk_radio_menu_item_draw_indicator (GtkCheckMenuItem      *check_menu_item,
35                                                 GdkRectangle          *area);
36
37
38 GtkType
39 gtk_radio_menu_item_get_type (void)
40 {
41   static GtkType radio_menu_item_type = 0;
42
43   if (!radio_menu_item_type)
44     {
45       static const GtkTypeInfo radio_menu_item_info =
46       {
47         "GtkRadioMenuItem",
48         sizeof (GtkRadioMenuItem),
49         sizeof (GtkRadioMenuItemClass),
50         (GtkClassInitFunc) gtk_radio_menu_item_class_init,
51         (GtkObjectInitFunc) gtk_radio_menu_item_init,
52         /* reserved_1 */ NULL,
53         /* reserved_2 */ NULL,
54         (GtkClassInitFunc) NULL,
55       };
56
57       radio_menu_item_type = gtk_type_unique (gtk_check_menu_item_get_type (), &radio_menu_item_info);
58     }
59
60   return radio_menu_item_type;
61 }
62
63 GtkWidget*
64 gtk_radio_menu_item_new (GSList *group)
65 {
66   GtkRadioMenuItem *radio_menu_item;
67
68   radio_menu_item = gtk_type_new (gtk_radio_menu_item_get_type ());
69
70   gtk_radio_menu_item_set_group (radio_menu_item, group);
71
72   return GTK_WIDGET (radio_menu_item);
73 }
74
75 void
76 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
77                                GSList           *group)
78 {
79   g_return_if_fail (radio_menu_item != NULL);
80   g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
81   g_return_if_fail (!g_slist_find (group, radio_menu_item));
82   
83   if (radio_menu_item->group)
84     {
85       GSList *slist;
86       
87       radio_menu_item->group = g_slist_remove (radio_menu_item->group, radio_menu_item);
88       
89       for (slist = radio_menu_item->group; slist; slist = slist->next)
90         {
91           GtkRadioMenuItem *tmp_item;
92           
93           tmp_item = slist->data;
94           
95           tmp_item->group = radio_menu_item->group;
96         }
97     }
98   
99   radio_menu_item->group = g_slist_prepend (group, radio_menu_item);
100   
101   if (group)
102     {
103       GSList *slist;
104       
105       for (slist = group; slist; slist = slist->next)
106         {
107           GtkRadioMenuItem *tmp_item;
108           
109           tmp_item = slist->data;
110           
111           tmp_item->group = radio_menu_item->group;
112         }
113     }
114   else
115     {
116       GTK_CHECK_MENU_ITEM (radio_menu_item)->active = TRUE;
117       /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
118        */
119     }
120 }
121
122 GtkWidget*
123 gtk_radio_menu_item_new_with_label (GSList *group,
124                                     const gchar *label)
125 {
126   GtkWidget *radio_menu_item;
127   GtkWidget *accel_label;
128
129   radio_menu_item = gtk_radio_menu_item_new (group);
130   accel_label = gtk_accel_label_new (label);
131   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
132   gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
133   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
134   gtk_widget_show (accel_label);
135
136   return radio_menu_item;
137 }
138
139 GSList*
140 gtk_radio_menu_item_group (GtkRadioMenuItem *radio_menu_item)
141 {
142   g_return_val_if_fail (radio_menu_item != NULL, NULL);
143   g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
144
145   return radio_menu_item->group;
146 }
147
148
149 static void
150 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
151 {
152   GtkMenuItemClass *menu_item_class;
153   GtkCheckMenuItemClass *check_menu_item_class;
154
155   menu_item_class = (GtkMenuItemClass*) klass;
156   check_menu_item_class = (GtkCheckMenuItemClass*) klass;
157
158   menu_item_class->activate = gtk_radio_menu_item_activate;
159
160   check_menu_item_class->draw_indicator = gtk_radio_menu_item_draw_indicator;
161 }
162
163 static void
164 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
165 {
166   radio_menu_item->group = g_slist_prepend (NULL, radio_menu_item);
167 }
168
169 static void
170 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
171 {
172   GtkRadioMenuItem *radio_menu_item;
173   GtkCheckMenuItem *check_menu_item;
174   GtkCheckMenuItem *tmp_menu_item;
175   GSList *tmp_list;
176   gint toggled;
177
178   g_return_if_fail (menu_item != NULL);
179   g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (menu_item));
180
181   radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
182   check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
183   toggled = FALSE;
184
185   if (check_menu_item->active)
186     {
187       tmp_menu_item = NULL;
188       tmp_list = radio_menu_item->group;
189
190       while (tmp_list)
191         {
192           tmp_menu_item = tmp_list->data;
193           tmp_list = tmp_list->next;
194
195           if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
196             break;
197
198           tmp_menu_item = NULL;
199         }
200
201       if (tmp_menu_item)
202         {
203           toggled = TRUE;
204           check_menu_item->active = !check_menu_item->active;
205         }
206     }
207   else
208     {
209       toggled = TRUE;
210       check_menu_item->active = !check_menu_item->active;
211
212       tmp_list = radio_menu_item->group;
213       while (tmp_list)
214         {
215           tmp_menu_item = tmp_list->data;
216           tmp_list = tmp_list->next;
217
218           if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
219             {
220               gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
221               break;
222             }
223         }
224     }
225
226   if (toggled)
227     gtk_check_menu_item_toggled (check_menu_item);
228   gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));
229 }
230
231 static void
232 gtk_radio_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
233                                     GdkRectangle     *area)
234 {
235   GtkWidget *widget;
236   GtkStateType state_type;
237   GtkShadowType shadow_type;
238   gint width, height;
239   gint x, y;
240
241   g_return_if_fail (check_menu_item != NULL);
242   g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (check_menu_item));
243
244   if (GTK_WIDGET_DRAWABLE (check_menu_item))
245     {
246       widget = GTK_WIDGET (check_menu_item);
247
248       width = 8;
249       height = 8;
250       x = (GTK_CONTAINER (check_menu_item)->border_width +
251            widget->style->klass->xthickness + 2);
252       y = (widget->allocation.height - height) / 2;
253
254       if (check_menu_item->active ||
255           check_menu_item->always_show_toggle ||
256           (GTK_WIDGET_STATE (check_menu_item) == GTK_STATE_PRELIGHT))
257         {
258           state_type = GTK_WIDGET_STATE (widget);
259           if (check_menu_item->active ||
260               !check_menu_item->always_show_toggle)
261             shadow_type = GTK_SHADOW_IN;
262           else
263             shadow_type = GTK_SHADOW_OUT;
264
265           gtk_paint_option (widget->style, widget->window,
266                             state_type, shadow_type,
267                             area, widget, "option",
268                             x, y, width, height);
269         }
270     }
271 }
272