]> Pileus Git - ~andy/gtk/blob - gtk/gtkradiomenuitem.c
gtk/gtkradiobutton.c (gtk_radio_button_draw_indicator): Use
[~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 "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_destroy        (GtkObject             *object);
34 static void gtk_radio_menu_item_activate       (GtkMenuItem           *menu_item);
35 static void gtk_radio_menu_item_draw_indicator (GtkCheckMenuItem      *check_menu_item,
36                                                 GdkRectangle          *area);
37
38 static GtkCheckMenuItemClass *parent_class = NULL;
39
40 GType
41 gtk_radio_menu_item_get_type (void)
42 {
43   static GType radio_menu_item_type = 0;
44
45   if (!radio_menu_item_type)
46     {
47       static const GTypeInfo radio_menu_item_info =
48       {
49         sizeof (GtkRadioMenuItemClass),
50         NULL,           /* base_init */
51         NULL,           /* base_finalize */
52         (GClassInitFunc) gtk_radio_menu_item_class_init,
53         NULL,           /* class_finalize */
54         NULL,           /* class_data */
55         sizeof (GtkRadioMenuItem),
56         0,              /* n_preallocs */
57         (GInstanceInitFunc) gtk_radio_menu_item_init,
58       };
59
60       radio_menu_item_type =
61         g_type_register_static (GTK_TYPE_CHECK_MENU_ITEM, "GtkRadioMenuItem",
62                                 &radio_menu_item_info, 0);
63     }
64
65   return radio_menu_item_type;
66 }
67
68 GtkWidget*
69 gtk_radio_menu_item_new (GSList *group)
70 {
71   GtkRadioMenuItem *radio_menu_item;
72
73   radio_menu_item = g_object_new (GTK_TYPE_RADIO_MENU_ITEM, NULL);
74
75   gtk_radio_menu_item_set_group (radio_menu_item, group);
76
77   return GTK_WIDGET (radio_menu_item);
78 }
79
80 void
81 gtk_radio_menu_item_set_group (GtkRadioMenuItem *radio_menu_item,
82                                GSList           *group)
83 {
84   g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item));
85   g_return_if_fail (!g_slist_find (group, radio_menu_item));
86   
87   if (radio_menu_item->group)
88     {
89       GSList *slist;
90       
91       radio_menu_item->group = g_slist_remove (radio_menu_item->group, radio_menu_item);
92       
93       for (slist = radio_menu_item->group; slist; slist = slist->next)
94         {
95           GtkRadioMenuItem *tmp_item;
96           
97           tmp_item = slist->data;
98           
99           tmp_item->group = radio_menu_item->group;
100         }
101     }
102   
103   radio_menu_item->group = g_slist_prepend (group, radio_menu_item);
104   
105   if (group)
106     {
107       GSList *slist;
108       
109       for (slist = group; slist; slist = slist->next)
110         {
111           GtkRadioMenuItem *tmp_item;
112           
113           tmp_item = slist->data;
114           
115           tmp_item->group = radio_menu_item->group;
116         }
117     }
118   else
119     {
120       GTK_CHECK_MENU_ITEM (radio_menu_item)->active = TRUE;
121       /* gtk_widget_set_state (GTK_WIDGET (radio_menu_item), GTK_STATE_ACTIVE);
122        */
123     }
124 }
125
126 GtkWidget*
127 gtk_radio_menu_item_new_with_label (GSList *group,
128                                     const gchar *label)
129 {
130   GtkWidget *radio_menu_item;
131   GtkWidget *accel_label;
132
133   radio_menu_item = gtk_radio_menu_item_new (group);
134   accel_label = gtk_accel_label_new (label);
135   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
136   gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
137   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
138   gtk_widget_show (accel_label);
139
140   return radio_menu_item;
141 }
142
143
144 /**
145  * gtk_radio_menu_item_new_with_mnemonic:
146  * @group: group the radio menu item is inside
147  * @label: the text of the button, with an underscore in front of the
148  *         mnemonic character
149  * @returns: a new #GtkRadioMenuItem
150  *
151  * Creates a new #GtkRadioMenuItem containing a label. The label
152  * will be created using gtk_label_new_with_mnemonic(), so underscores
153  * in @label indicate the mnemonic for the menu item.
154  **/
155 GtkWidget*
156 gtk_radio_menu_item_new_with_mnemonic (GSList *group,
157                                        const gchar *label)
158 {
159   GtkWidget *radio_menu_item;
160   GtkWidget *accel_label;
161
162   radio_menu_item = gtk_radio_menu_item_new (group);
163   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
164   gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
165   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
166
167   gtk_container_add (GTK_CONTAINER (radio_menu_item), accel_label);
168   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), radio_menu_item);
169   gtk_widget_show (accel_label);
170
171   return radio_menu_item;
172 }
173
174 GSList*
175 gtk_radio_menu_item_get_group (GtkRadioMenuItem *radio_menu_item)
176 {
177   g_return_val_if_fail (GTK_IS_RADIO_MENU_ITEM (radio_menu_item), NULL);
178
179   return radio_menu_item->group;
180 }
181
182
183 static void
184 gtk_radio_menu_item_class_init (GtkRadioMenuItemClass *klass)
185 {
186   GtkObjectClass *object_class;
187   GtkMenuItemClass *menu_item_class;
188   GtkCheckMenuItemClass *check_menu_item_class;
189
190   object_class = (GtkObjectClass*) klass;
191   menu_item_class = (GtkMenuItemClass*) klass;
192   check_menu_item_class = (GtkCheckMenuItemClass*) klass;
193
194   parent_class = g_type_class_peek_parent (klass);
195
196   object_class->destroy = gtk_radio_menu_item_destroy;
197
198   menu_item_class->activate = gtk_radio_menu_item_activate;
199
200   check_menu_item_class->draw_indicator = gtk_radio_menu_item_draw_indicator;
201 }
202
203 static void
204 gtk_radio_menu_item_init (GtkRadioMenuItem *radio_menu_item)
205 {
206   radio_menu_item->group = g_slist_prepend (NULL, radio_menu_item);
207 }
208
209 static void
210 gtk_radio_menu_item_destroy (GtkObject *object)
211 {
212   GtkRadioMenuItem *radio_menu_item;
213   GtkRadioMenuItem *tmp_menu_item;
214   GSList *tmp_list;
215
216   g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (object));
217
218   radio_menu_item = GTK_RADIO_MENU_ITEM (object);
219
220   radio_menu_item->group = g_slist_remove (radio_menu_item->group,
221                                            radio_menu_item);
222   tmp_list = radio_menu_item->group;
223
224   while (tmp_list)
225     {
226       tmp_menu_item = tmp_list->data;
227       tmp_list = tmp_list->next;
228
229       tmp_menu_item->group = radio_menu_item->group;
230     }
231
232   /* this radio menu item is no longer in the group */
233   radio_menu_item->group = NULL;
234   
235   if (GTK_OBJECT_CLASS (parent_class)->destroy)
236     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
237 }
238
239 static void
240 gtk_radio_menu_item_activate (GtkMenuItem *menu_item)
241 {
242   GtkRadioMenuItem *radio_menu_item;
243   GtkCheckMenuItem *check_menu_item;
244   GtkCheckMenuItem *tmp_menu_item;
245   GSList *tmp_list;
246   gint toggled;
247
248   g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (menu_item));
249
250   radio_menu_item = GTK_RADIO_MENU_ITEM (menu_item);
251   check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
252   toggled = FALSE;
253
254   if (check_menu_item->active)
255     {
256       tmp_menu_item = NULL;
257       tmp_list = radio_menu_item->group;
258
259       while (tmp_list)
260         {
261           tmp_menu_item = tmp_list->data;
262           tmp_list = tmp_list->next;
263
264           if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
265             break;
266
267           tmp_menu_item = NULL;
268         }
269
270       if (tmp_menu_item)
271         {
272           toggled = TRUE;
273           check_menu_item->active = !check_menu_item->active;
274         }
275     }
276   else
277     {
278       toggled = TRUE;
279       check_menu_item->active = !check_menu_item->active;
280
281       tmp_list = radio_menu_item->group;
282       while (tmp_list)
283         {
284           tmp_menu_item = tmp_list->data;
285           tmp_list = tmp_list->next;
286
287           if (tmp_menu_item->active && (tmp_menu_item != check_menu_item))
288             {
289               gtk_menu_item_activate (GTK_MENU_ITEM (tmp_menu_item));
290               break;
291             }
292         }
293     }
294
295   if (toggled)
296     gtk_check_menu_item_toggled (check_menu_item);
297   gtk_widget_queue_draw (GTK_WIDGET (radio_menu_item));
298 }
299
300 static void
301 gtk_radio_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
302                                     GdkRectangle     *area)
303 {
304   GtkWidget *widget;
305   GtkStateType state_type;
306   GtkShadowType shadow_type;
307   gint width, height;
308   gint x, y;
309   gint offset;
310
311   g_return_if_fail (GTK_IS_RADIO_MENU_ITEM (check_menu_item));
312
313   if (GTK_WIDGET_DRAWABLE (check_menu_item))
314     {
315       widget = GTK_WIDGET (check_menu_item);
316
317       width = 8;
318       height = 8;
319       offset = GTK_CONTAINER (check_menu_item)->border_width +
320         widget->style->xthickness + 2;
321       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
322         x = widget->allocation.x + offset;
323       else 
324         x = widget->allocation.x + widget->allocation.width - width - offset;
325       y = widget->allocation.y + (widget->allocation.height - height) / 2;
326
327       if (check_menu_item->active ||
328           check_menu_item->always_show_toggle ||
329           (GTK_WIDGET_STATE (check_menu_item) == GTK_STATE_PRELIGHT))
330         {
331           state_type = GTK_WIDGET_STATE (widget);
332           if (check_menu_item->active ||
333               !check_menu_item->always_show_toggle)
334             shadow_type = GTK_SHADOW_IN;
335           else
336             shadow_type = GTK_SHADOW_OUT;
337
338           if (check_menu_item->inconsistent)
339             shadow_type = GTK_SHADOW_ETCHED_IN;
340           
341           if (!GTK_WIDGET_IS_SENSITIVE (widget))
342             state_type = GTK_STATE_INSENSITIVE;
343               
344           gtk_paint_option (widget->style, widget->window,
345                             state_type, shadow_type,
346                             area, widget, "option",
347                             x, y, width, height);
348         }
349     }
350 }
351