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