]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailmenushell.c
0fe91f447963a63c3bbdee5aeaeb8940a561df64
[~andy/gtk] / modules / other / gail / gailmenushell.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001 Sun Microsystems Inc.
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 #include <config.h>
21
22 #include <gtk/gtk.h>
23 #include "gailmenushell.h"
24
25 static void         gail_menu_shell_class_init          (GailMenuShellClass *klass);
26 static void         gail_menu_shell_init                (GailMenuShell      *menu_shell);
27 static void         gail_menu_shell_initialize          (AtkObject          *accessible,
28                                                          gpointer            data);
29 static void         atk_selection_interface_init        (AtkSelectionIface  *iface);
30 static gboolean     gail_menu_shell_add_selection       (AtkSelection   *selection,
31                                                          gint           i);
32 static gboolean     gail_menu_shell_clear_selection     (AtkSelection   *selection);
33 static AtkObject*   gail_menu_shell_ref_selection       (AtkSelection   *selection,
34                                                          gint           i);
35 static gint         gail_menu_shell_get_selection_count (AtkSelection   *selection);
36 static gboolean     gail_menu_shell_is_child_selected   (AtkSelection   *selection,
37                                                          gint           i);
38 static gboolean     gail_menu_shell_remove_selection    (AtkSelection   *selection,
39                                                          gint           i);
40
41 G_DEFINE_TYPE_WITH_CODE (GailMenuShell, gail_menu_shell, GAIL_TYPE_CONTAINER,
42                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
43
44 static void
45 gail_menu_shell_class_init (GailMenuShellClass *klass)
46 {
47   AtkObjectClass *atk_object_class = ATK_OBJECT_CLASS (klass);
48
49   atk_object_class->initialize = gail_menu_shell_initialize;
50 }
51
52 static void
53 gail_menu_shell_init (GailMenuShell *menu_shell)
54 {
55 }
56
57 static void
58 gail_menu_shell_initialize (AtkObject *accessible,
59                             gpointer  data)
60 {
61   ATK_OBJECT_CLASS (gail_menu_shell_parent_class)->initialize (accessible, data);
62
63   if (GTK_IS_MENU_BAR (data))
64     accessible->role = ATK_ROLE_MENU_BAR;
65   else
66     /*
67      * Accessible object for Menu is created in gailmenu.c
68      */
69     accessible->role = ATK_ROLE_UNKNOWN;
70 }
71
72 static void
73 atk_selection_interface_init (AtkSelectionIface *iface)
74 {
75   iface->add_selection = gail_menu_shell_add_selection;
76   iface->clear_selection = gail_menu_shell_clear_selection;
77   iface->ref_selection = gail_menu_shell_ref_selection;
78   iface->get_selection_count = gail_menu_shell_get_selection_count;
79   iface->is_child_selected = gail_menu_shell_is_child_selected;
80   iface->remove_selection = gail_menu_shell_remove_selection;
81   /*
82    * select_all_selection does not make sense for a menu_shell
83    * so no implementation is provided.
84    */
85 }
86
87 static gboolean
88 gail_menu_shell_add_selection (AtkSelection *selection,
89                                gint          i)
90 {
91   GtkMenuShell *shell;
92   GList *item;
93   guint length;
94   GtkWidget *widget;
95
96   widget =  GTK_ACCESSIBLE (selection)->widget;
97   if (widget == NULL)
98   {
99     /* State is defunct */
100     return FALSE;
101   }
102
103   shell = GTK_MENU_SHELL (widget);
104   length = g_list_length (shell->children);
105   if (i < 0 || i > length)
106     return FALSE;
107
108   item = g_list_nth (shell->children, i);
109   g_return_val_if_fail (item != NULL, FALSE);
110   g_return_val_if_fail (GTK_IS_MENU_ITEM(item->data), FALSE);
111    
112   gtk_menu_shell_select_item (shell, GTK_WIDGET (item->data));
113   return TRUE;
114 }
115
116 static gboolean
117 gail_menu_shell_clear_selection (AtkSelection   *selection)
118 {
119   GtkMenuShell *shell;
120   GtkWidget *widget;
121
122   widget =  GTK_ACCESSIBLE (selection)->widget;
123   if (widget == NULL)
124   {
125     /* State is defunct */
126     return FALSE;
127   }
128
129   shell = GTK_MENU_SHELL (widget);
130
131   gtk_menu_shell_deselect (shell);
132   return TRUE;
133 }
134
135 static AtkObject*
136 gail_menu_shell_ref_selection (AtkSelection   *selection,
137                                gint           i)
138 {
139   GtkMenuShell *shell;
140   AtkObject *obj;
141   GtkWidget *widget;
142
143   if (i != 0)
144     return NULL;
145
146   widget =  GTK_ACCESSIBLE (selection)->widget;
147   if (widget == NULL)
148   {
149     /* State is defunct */
150     return NULL;
151   }
152
153   shell = GTK_MENU_SHELL (widget);
154   
155   if (shell->active_menu_item != NULL)
156   {
157     obj = gtk_widget_get_accessible (shell->active_menu_item);
158     g_object_ref (obj);
159     return obj;
160   }
161   else
162   {
163     return NULL;
164   }
165 }
166
167 static gint
168 gail_menu_shell_get_selection_count (AtkSelection   *selection)
169 {
170   GtkMenuShell *shell;
171   GtkWidget *widget;
172
173   widget =  GTK_ACCESSIBLE (selection)->widget;
174   if (widget == NULL)
175   {
176     /* State is defunct */
177     return 0;
178   }
179
180   shell = GTK_MENU_SHELL (widget);
181
182   /*
183    * Identifies the currently selected menu item
184    */
185   if (shell->active_menu_item == NULL)
186   {
187     return 0;
188   }
189   else
190   {
191     return 1;
192   }
193 }
194
195 static gboolean
196 gail_menu_shell_is_child_selected (AtkSelection   *selection,
197                                    gint           i)
198 {
199   GtkMenuShell *shell;
200   gint j;
201   GtkWidget *widget;
202
203   widget =  GTK_ACCESSIBLE (selection)->widget;
204   if (widget == NULL)
205   {
206     /* State is defunct */
207     return FALSE;
208   }
209
210   shell = GTK_MENU_SHELL (widget);
211   if (shell->active_menu_item == NULL)
212     return FALSE;
213   
214   j = g_list_index (shell->children, shell->active_menu_item);
215
216   return (j==i);   
217 }
218
219 static gboolean
220 gail_menu_shell_remove_selection (AtkSelection   *selection,
221                                   gint           i)
222 {
223   GtkMenuShell *shell;
224   GtkWidget *widget;
225
226   if (i != 0)
227     return FALSE;
228
229   widget =  GTK_ACCESSIBLE (selection)->widget;
230   if (widget == NULL)
231   {
232     /* State is defunct */
233     return FALSE;
234   }
235
236   shell = GTK_MENU_SHELL (widget);
237
238   if (shell->active_menu_item && 
239       GTK_MENU_ITEM (shell->active_menu_item)->submenu)
240   {
241     /*
242      * Menu item contains a menu and it is the selected menu item
243      * so deselect it.
244      */
245     gtk_menu_shell_deselect (shell);
246   }
247   return TRUE;
248 }