]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gailmenushell.c
gail: Move from modules/other/gail to gtk/a11y
[~andy/gtk] / gtk / a11y / 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   GList *kids;
92   GtkWidget *item;
93   guint length;
94   GtkWidget *widget;
95
96   widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
97   if (widget == NULL)
98   {
99     /* State is defunct */
100     return FALSE;
101   }
102
103   kids = gtk_container_get_children (GTK_CONTAINER (widget));
104   length = g_list_length (kids);
105   if (i < 0 || i > length)
106     {
107       g_list_free (kids);
108       return FALSE;
109     }
110
111   item = g_list_nth_data (kids, i);
112   g_list_free (kids);
113   g_return_val_if_fail (GTK_IS_MENU_ITEM(item), FALSE);
114   gtk_menu_shell_select_item (GTK_MENU_SHELL (widget), item);
115   return TRUE;
116 }
117
118 static gboolean
119 gail_menu_shell_clear_selection (AtkSelection   *selection)
120 {
121   GtkMenuShell *shell;
122   GtkWidget *widget;
123
124   widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
125   if (widget == NULL)
126   {
127     /* State is defunct */
128     return FALSE;
129   }
130
131   shell = GTK_MENU_SHELL (widget);
132
133   gtk_menu_shell_deselect (shell);
134   return TRUE;
135 }
136
137 static AtkObject*
138 gail_menu_shell_ref_selection (AtkSelection   *selection,
139                                gint           i)
140 {
141   GtkMenuShell *shell;
142   AtkObject *obj;
143   GtkWidget *widget;
144   GtkWidget *item;
145
146   if (i != 0)
147     return NULL;
148
149   widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
150   if (widget == NULL)
151   {
152     /* State is defunct */
153     return NULL;
154   }
155
156   shell = GTK_MENU_SHELL (widget);
157
158   item = gtk_menu_shell_get_selected_item (shell);
159   if (item != NULL)
160   {
161     obj = gtk_widget_get_accessible (item);
162     g_object_ref (obj);
163     return obj;
164   }
165   else
166   {
167     return NULL;
168   }
169 }
170
171 static gint
172 gail_menu_shell_get_selection_count (AtkSelection   *selection)
173 {
174   GtkMenuShell *shell;
175   GtkWidget *widget;
176
177   widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
178   if (widget == NULL)
179   {
180     /* State is defunct */
181     return 0;
182   }
183
184   shell = GTK_MENU_SHELL (widget);
185
186   /*
187    * Identifies the currently selected menu item
188    */
189   if (gtk_menu_shell_get_selected_item (shell) == NULL)
190   {
191     return 0;
192   }
193   else
194   {
195     return 1;
196   }
197 }
198
199 static gboolean
200 gail_menu_shell_is_child_selected (AtkSelection   *selection,
201                                    gint           i)
202 {
203   GtkMenuShell *shell;
204   GList *kids;
205   gint j;
206   GtkWidget *widget;
207   GtkWidget *item;
208
209   widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
210   if (widget == NULL)
211   {
212     /* State is defunct */
213     return FALSE;
214   }
215
216   shell = GTK_MENU_SHELL (widget);
217   item = gtk_menu_shell_get_selected_item (shell);
218   if (item == NULL)
219     return FALSE;
220
221   kids = gtk_container_get_children (GTK_CONTAINER (shell));
222   j = g_list_index (kids, item);
223   g_list_free (kids);
224
225   return (j==i);
226 }
227
228 static gboolean
229 gail_menu_shell_remove_selection (AtkSelection   *selection,
230                                   gint           i)
231 {
232   GtkMenuShell *shell;
233   GtkWidget *widget;
234   GtkWidget *item;
235
236   if (i != 0)
237     return FALSE;
238
239   widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
240   if (widget == NULL)
241   {
242     /* State is defunct */
243     return FALSE;
244   }
245
246   shell = GTK_MENU_SHELL (widget);
247
248   item = gtk_menu_shell_get_selected_item (shell);
249   if (item && gtk_menu_item_get_submenu (GTK_MENU_ITEM (item)))
250   {
251     /*
252      * Menu item contains a menu and it is the selected menu item
253      * so deselect it.
254      */
255     gtk_menu_shell_deselect (shell);
256   }
257   return TRUE;
258 }