]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtknotebookaccessible.c
a11y: Fix crash in notebook
[~andy/gtk] / gtk / a11y / gtknotebookaccessible.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001, 2002, 2003 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 <string.h>
23 #include <gtk/gtk.h>
24 #include "gtknotebookaccessible.h"
25 #include "gtknotebookpageaccessible.h"
26
27
28 static void atk_selection_interface_init (AtkSelectionIface *iface);
29
30 G_DEFINE_TYPE_WITH_CODE (GtkNotebookAccessible, _gtk_notebook_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
31                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
32
33 static gboolean
34 check_focus_tab (gpointer data)
35 {
36   GtkWidget *widget;
37   AtkObject *atk_obj;
38   gint focus_page_num, old_focus_page_num;
39   GtkNotebookAccessible *accessible;
40   GtkNotebook *notebook;
41
42   atk_obj = ATK_OBJECT (data);
43   accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
44   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_obj));
45   if (widget == NULL)
46     return FALSE;
47   notebook = GTK_NOTEBOOK (widget);
48
49   accessible->idle_focus_id = 0;
50
51   focus_page_num = gtk_notebook_get_current_page (notebook);
52   if (focus_page_num == -1)
53     return FALSE;
54
55   old_focus_page_num = accessible->focus_tab_page;
56   accessible->focus_tab_page = focus_page_num;
57   if (old_focus_page_num != focus_page_num)
58     {
59       AtkObject *obj;
60
61       obj = atk_object_ref_accessible_child (atk_obj, focus_page_num);
62       atk_focus_tracker_notify (obj);
63       g_object_unref (obj);
64     }
65
66   return FALSE;
67 }
68
69 static gboolean
70 focus_cb (GtkWidget        *widget,
71           GtkDirectionType  type)
72 {
73   AtkObject *atk_obj = gtk_widget_get_accessible (widget);
74   GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
75
76   switch (type)
77     {
78     case GTK_DIR_LEFT:
79     case GTK_DIR_RIGHT:
80       if (accessible->idle_focus_id == 0)
81         accessible->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
82       break;
83     default:
84       break;
85     }
86   return FALSE;
87 }
88
89 static void
90 create_notebook_page_accessible (GtkNotebookAccessible *accessible,
91                                  GtkNotebook           *notebook,
92                                  GtkWidget             *child,
93                                  gint                   page_num)
94 {
95   AtkObject *obj;
96
97   obj = _gtk_notebook_page_accessible_new (accessible, child);
98   g_hash_table_insert (accessible->pages, child, obj);
99   atk_object_set_parent (obj, ATK_OBJECT (accessible));
100   g_signal_emit_by_name (accessible, "children-changed::add", page_num, obj, NULL);
101 }
102
103 static void
104 page_added_cb (GtkNotebook *notebook,
105                GtkWidget   *child,
106                guint        page_num,
107                gpointer     data)
108 {
109   AtkObject *atk_obj;
110   GtkNotebookAccessible *accessible;
111
112   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (notebook));
113   accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
114   create_notebook_page_accessible (accessible, notebook, child, page_num);
115 }
116
117 static void
118 page_removed_cb (GtkNotebook *notebook,
119                  GtkWidget   *widget,
120                  guint        page_num,
121                  gpointer     data)
122 {
123   GtkNotebookAccessible *accessible;
124   AtkObject *obj;
125
126   accessible = GTK_NOTEBOOK_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (notebook)));
127
128   obj = g_hash_table_lookup (accessible->pages, widget);
129   g_return_if_fail (obj);
130   g_signal_emit_by_name (accessible, "children-changed::remove",
131                          page_num, obj, NULL);
132   _gtk_notebook_page_accessible_invalidate (GTK_NOTEBOOK_PAGE_ACCESSIBLE (obj));
133   g_hash_table_remove (accessible->pages, widget);
134 }
135
136
137 static void
138 gtk_notebook_accessible_initialize (AtkObject *obj,
139                                     gpointer   data)
140 {
141   GtkNotebookAccessible *accessible;
142   GtkNotebook *notebook;
143   gint i;
144
145   ATK_OBJECT_CLASS (_gtk_notebook_accessible_parent_class)->initialize (obj, data);
146
147   accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
148   notebook = GTK_NOTEBOOK (data);
149   for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
150     {
151       create_notebook_page_accessible (accessible,
152                                        notebook,
153                                        gtk_notebook_get_nth_page (notebook, i),
154                                        i);
155     }
156   accessible->selected_page = gtk_notebook_get_current_page (notebook);
157
158   g_signal_connect (notebook, "focus",
159                     G_CALLBACK (focus_cb), NULL);
160   g_signal_connect (notebook, "page-added",
161                     G_CALLBACK (page_added_cb), NULL);
162   g_signal_connect (notebook, "page-removed",
163                     G_CALLBACK (page_removed_cb), NULL);
164
165   obj->role = ATK_ROLE_PAGE_TAB_LIST;
166 }
167
168 static void
169 gtk_notebook_accessible_finalize (GObject *object)
170 {
171   GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (object);
172
173   g_hash_table_destroy (accessible->pages);
174
175   if (accessible->idle_focus_id)
176     g_source_remove (accessible->idle_focus_id);
177
178   G_OBJECT_CLASS (_gtk_notebook_accessible_parent_class)->finalize (object);
179 }
180
181 static AtkObject *
182 gtk_notebook_accessible_ref_child (AtkObject *obj,
183                                    gint       i)
184 {
185   AtkObject *child;
186   GtkNotebookAccessible *accessible;
187   GtkNotebook *notebook;
188   GtkWidget *widget;
189  
190   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
191   if (widget == NULL)
192     return NULL;
193
194   accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
195   notebook = GTK_NOTEBOOK (widget);
196
197   child = g_hash_table_lookup (accessible->pages,
198                                gtk_notebook_get_nth_page (notebook, i));
199   /* can return NULL when i >= n_children */
200
201   if (child)
202     g_object_ref (child);
203
204   return child;
205 }
206
207 static void
208 gtk_notebook_accessible_notify_gtk (GObject    *obj,
209                                     GParamSpec *pspec)
210 {
211   GtkWidget *widget;
212   AtkObject* atk_obj;
213
214   widget = GTK_WIDGET (obj);
215   atk_obj = gtk_widget_get_accessible (widget);
216
217   if (strcmp (pspec->name, "page") == 0)
218     {
219       gint page_num, old_page_num;
220       gint focus_page_num = 0;
221       gint old_focus_page_num;
222       GtkNotebookAccessible *accessible;
223       GtkNotebook *notebook;
224
225       accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
226       notebook = GTK_NOTEBOOK (widget);
227
228       /* Notify SELECTED state change for old and new page */
229       old_page_num = accessible->selected_page;
230       page_num = gtk_notebook_get_current_page (notebook);
231       accessible->selected_page = page_num;
232       accessible->focus_tab_page = page_num;
233       old_focus_page_num = accessible->focus_tab_page;
234
235       if (page_num != old_page_num)
236         {
237           AtkObject *child;
238
239           if (old_page_num != -1)
240             {
241               child = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
242               if (child)
243                 {
244                   atk_object_notify_state_change (child, ATK_STATE_SELECTED, FALSE);
245                   g_object_unref (child);
246                 }
247             }
248           child = gtk_notebook_accessible_ref_child (atk_obj, page_num);
249           if (child)
250             {
251               atk_object_notify_state_change (child, ATK_STATE_SELECTED, TRUE);
252               g_object_unref (child);
253               /*
254                * The page which is being displayed has changed but there is
255                * no need to tell the focus tracker as the focus page will also
256                * change or a widget in the page will receive focus if the
257                * Notebook does not have tabs.
258                */
259             }
260           g_signal_emit_by_name (atk_obj, "selection-changed");
261           g_signal_emit_by_name (atk_obj, "visible-data-changed");
262         }
263       if (gtk_notebook_get_show_tabs (notebook) &&
264          (focus_page_num != old_focus_page_num))
265         {
266           if (accessible->idle_focus_id)
267             g_source_remove (accessible->idle_focus_id);
268           accessible->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
269         }
270     }
271   else
272     GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_notebook_accessible_parent_class)->notify_gtk (obj, pspec);
273 }
274
275 /*
276  * GtkNotebook only supports the selection of one page at a time.
277  * Selecting a page unselects any previous selection, so this
278  * changes the current selection instead of adding to it.
279  */
280 static gboolean
281 gtk_notebook_accessible_add_selection (AtkSelection *selection,
282                                        gint          i)
283 {
284   GtkNotebook *notebook;
285   GtkWidget *widget;
286
287   widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
288   if (widget == NULL)
289     return FALSE;
290
291   notebook = GTK_NOTEBOOK (widget);
292   gtk_notebook_set_current_page (notebook, i);
293   return TRUE;
294 }
295
296 static void
297 _gtk_notebook_accessible_class_init (GtkNotebookAccessibleClass *klass)
298 {
299   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
300   AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
301   GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
302   GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
303
304
305   gobject_class->finalize = gtk_notebook_accessible_finalize;
306
307   class->ref_child = gtk_notebook_accessible_ref_child;
308   class->initialize = gtk_notebook_accessible_initialize;
309
310   widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;
311
312   /* we listen to page-added/-removed, so we don't care about these */
313   container_class->add_gtk = NULL;
314   container_class->remove_gtk = NULL;
315 }
316
317 static void
318 _gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
319 {
320   notebook->pages = g_hash_table_new_full (g_direct_hash,
321                                            g_direct_equal,
322                                            NULL,
323                                            g_object_unref);
324   notebook->selected_page = -1;
325   notebook->focus_tab_page = -1;
326   notebook->idle_focus_id = 0;
327 }
328
329 static AtkObject *
330 gtk_notebook_accessible_ref_selection (AtkSelection *selection,
331                                        gint          i)
332 {
333   AtkObject *accessible;
334   GtkWidget *widget;
335   GtkNotebook *notebook;
336   gint pagenum;
337
338   if (i != 0)
339     return NULL;
340
341   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
342   if (widget == NULL)
343     return NULL;
344
345   notebook = GTK_NOTEBOOK (widget);
346   pagenum = gtk_notebook_get_current_page (notebook);
347   if (pagenum == -1)
348     return NULL;
349   accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);
350
351   return accessible;
352 }
353
354 /* Always return 1 because there can only be one page
355  * selected at any time
356  */
357 static gint
358 gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
359 {
360   GtkWidget *widget;
361   GtkNotebook *notebook;
362
363   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
364   if (widget == NULL)
365     return 0;
366
367   notebook = GTK_NOTEBOOK (widget);
368   if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
369     return 0;
370
371   return 1;
372 }
373
374 static gboolean
375 gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
376                                            gint          i)
377 {
378   GtkWidget *widget;
379   GtkNotebook *notebook;
380   gint pagenumber;
381
382   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
383   if (widget == NULL)
384     return FALSE;
385
386   notebook = GTK_NOTEBOOK (widget);
387   pagenumber = gtk_notebook_get_current_page(notebook);
388
389   if (pagenumber == i)
390     return TRUE;
391
392   return FALSE;
393 }
394
395 static void
396 atk_selection_interface_init (AtkSelectionIface *iface)
397 {
398   iface->add_selection = gtk_notebook_accessible_add_selection;
399   iface->ref_selection = gtk_notebook_accessible_ref_selection;
400   iface->get_selection_count = gtk_notebook_accessible_get_selection_count;
401   iface->is_child_selected = gtk_notebook_accessible_is_child_selected;
402 }