Debian Codegraph @

gtk/a11y/gtkaccessibility.c

raw
1 /* GTK+ - accessibility implementations
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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include "gtkaccessibility.h"
21 #include "gtkaccessibilityutil.h"
22 #include "gtkaccessibilitymisc.h"
23
24 #include "gtkwindowaccessible.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include <gdk/gdk.h>
30 #include <gtk/gtkcombobox.h>
31 #include <gtk/gtkentry.h>
32 #include <gtk/gtknotebook.h>
33 #include <gtk/gtkmenuitem.h>
34 #include <gtk/gtkmenu.h>
35 #include <gtk/gtkmenubar.h>
36 #include <gtk/gtksocket.h>
37 #include <gtk/gtktogglebutton.h>
38 #include <gtk/gtkaccessible.h>
39
40 #ifdef GDK_WINDOWING_X11
41 #include <atk-bridge.h>
42 #endif
43
44 static gboolean gail_focus_watcher (GSignalInvocationHint *ihint,
45 guint n_param_values,
46 const GValue *param_values,
47 gpointer data);
48 static gboolean gail_select_watcher (GSignalInvocationHint *ihint,
49 guint n_param_values,
50 const GValue *param_values,
51 gpointer data);
52 static gboolean gail_deselect_watcher (GSignalInvocationHint *ihint,
53 guint n_param_values,
54 const GValue *param_values,
55 gpointer data);
56 static gboolean gail_switch_page_watcher(GSignalInvocationHint *ihint,
57 guint n_param_values,
58 const GValue *param_values,
59 gpointer data);
60 static void gail_finish_select (GtkWidget *widget);
61 static void gail_map_cb (GtkWidget *widget);
62 static void gail_map_submenu_cb (GtkWidget *widget);
63 static gint gail_focus_idle_handler (gpointer data);
64 static void gail_focus_notify (GtkWidget *widget);
65 static void gail_focus_notify_when_idle (GtkWidget *widget);
66
67 static void gail_focus_tracker_init (void);
68 static void gail_focus_object_destroyed (gpointer data);
69 static void gail_focus_tracker (AtkObject *object);
70 static void gail_set_focus_widget (GtkWidget *focus_widget,
71 GtkWidget *widget);
72 static void gail_set_focus_object (AtkObject *focus_obj,
73 AtkObject *obj);
74
75 GtkWidget* _focus_widget = NULL;
76 static GtkWidget* next_focus_widget = NULL;
77 static gboolean was_deselect = FALSE;
78 static GtkWidget* subsequent_focus_widget = NULL;
79 static GtkWidget* focus_before_menu = NULL;
80 static guint focus_notify_handler = 0;
81 static guint focus_tracker_id = 0;
82 static GQuark quark_focus_object = 0;
83 static int initialized = FALSE;
84
85 static AtkObject*
86 get_accessible_for_widget (GtkWidget *widget,
87 gboolean *transient)
88 {
89 AtkObject *obj = NULL;
90
91 *transient = FALSE;
92 if (!widget)
93 return NULL;
94
95 if (GTK_IS_ENTRY (widget))
96 ;
97 else if (GTK_IS_NOTEBOOK (widget))
98 {
99 GtkNotebook *notebook;
100 gint page_num = -1;
101
102 notebook = GTK_NOTEBOOK (widget);
103 page_num = gtk_notebook_get_current_page (notebook);
104 if (page_num != -1)
105 {
106 obj = gtk_widget_get_accessible (widget);
107 obj = atk_object_ref_accessible_child (obj, page_num);
108 g_object_unref (obj);
109 }
110 }
111 else if (GTK_IS_TOGGLE_BUTTON (widget))
112 {
113 GtkWidget *other_widget = gtk_widget_get_parent (widget);
114 if (GTK_IS_COMBO_BOX (other_widget))
115 {
116 gail_set_focus_widget (other_widget, widget);
117 widget = other_widget;
118 }
119 }
120 if (obj == NULL)
121 {
122 AtkObject *focus_object;
123
124 obj = gtk_widget_get_accessible (widget);
125 focus_object = g_object_get_qdata (G_OBJECT (obj), quark_focus_object);
126 /*
127 * We check whether the object for this focus_object has been deleted.
128 * This can happen when navigating to an empty directory in nautilus.
129 * See bug #141907.
130 */
131 if (ATK_IS_GOBJECT_ACCESSIBLE (focus_object))
132 {
133 if (!atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (focus_object)))
134 focus_object = NULL;
135 }
136 if (focus_object)
137 obj = focus_object;
138 }
139
140 return obj;
141 }
142
143 static gboolean
144 gail_focus_watcher (GSignalInvocationHint *ihint,
145 guint n_param_values,
146 const GValue *param_values,
147 gpointer data)
148 {
149 GObject *object;
150 GtkWidget *widget;
151 GdkEvent *event;
152
153 object = g_value_get_object (param_values + 0);
154 g_return_val_if_fail (GTK_IS_WIDGET(object), FALSE);
155
156 event = g_value_get_boxed (param_values + 1);
157 widget = GTK_WIDGET (object);
158
159 if (event->type == GDK_FOCUS_CHANGE)
160 {
161 if (event->focus_change.in)
162 {
163 if (GTK_IS_WINDOW (widget))
164 {
165 GtkWidget *focus_widget;
166 GtkWindow *window;
167 GtkWindowType type;
168
169 window = GTK_WINDOW (widget);
170 focus_widget = gtk_window_get_focus (window);
171 g_object_get (window, "type", &type, NULL);
172
173 if (focus_widget)
174 {
175 /*
176 * If we already have a potential focus widget set this
177 * windows's focus widget to focus_before_menu so that
178 * it will be reported when menu item is unset.
179 */
180 if (next_focus_widget)
181 {
182 if (GTK_IS_MENU_ITEM (next_focus_widget) &&
183 !focus_before_menu)
184 {
185 void *vp_focus_before_menu = &focus_before_menu;
186 focus_before_menu = focus_widget;
187 g_object_add_weak_pointer (G_OBJECT (focus_before_menu), vp_focus_before_menu);
188 }
189
190 return TRUE;
191 }
192 widget = focus_widget;
193 }
194 else if (type == GTK_WINDOW_POPUP)
195 {
196 if (GTK_IS_BIN (widget))
197 {
198 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
199
200 if (GTK_IS_WIDGET (child) && gtk_widget_has_grab (child))
201 {
202 if (GTK_IS_MENU_SHELL (child))
203 {
204 if (gtk_menu_shell_get_selected_item (GTK_MENU_SHELL (child)))
205 {
206 /*
207 * We have a menu which has a menu item selected
208 * so we do not report focus on the menu.
209 */
210 return TRUE;
211 }
212 }
213 widget = child;
214 }
215 }
216 else /* popup window has no children; this edge case occurs in some custom code (OOo for instance) */
217 {
218 return TRUE;
219 }
220 }
221 else /* Widget is a non-popup toplevel with no focus children;
222 don't emit for this case either, as it's useless */
223 {
224 return TRUE;
225 }
226 }
227 }
228 else
229 {
230 if (next_focus_widget)
231 {
232 GtkWidget *toplevel;
233
234 toplevel = gtk_widget_get_toplevel (next_focus_widget);
235 if (toplevel == widget)
236 next_focus_widget = NULL;
237 }
238 /* focus out */
239 widget = NULL;
240 }
241 }
242 else
243 {
244 if (event->type == GDK_MOTION_NOTIFY && gtk_widget_has_focus (widget))
245 {
246 if (widget == _focus_widget)
247 {
248 return TRUE;
249 }
250 }
251 else
252 {
253 return TRUE;
254 }
255 }
256
257 #ifdef GDK_WINDOWING_X11
258 /*
259 * If the focus widget is a GtkSocket without a plug
260 * then ignore the focus notification as the embedded
261 * plug will report a focus notification.
262 */
263 if (GTK_IS_SOCKET (widget) &&
264 gtk_socket_get_plug_window (GTK_SOCKET (widget)) != NULL)
265 return TRUE;
266 #endif
267
268 /*
269 * The widget may not yet be visible on the screen so we wait until it is.
270 */
271 gail_focus_notify_when_idle (widget);
272 return TRUE;
273 }
274
275 static gboolean
276 gail_select_watcher (GSignalInvocationHint *ihint,
277 guint n_param_values,
278 const GValue *param_values,
279 gpointer data)
280 {
281 GObject *object;
282 GtkWidget *widget;
283
284 object = g_value_get_object (param_values + 0);
285 g_return_val_if_fail (GTK_IS_WIDGET(object), FALSE);
286
287 widget = GTK_WIDGET (object);
288
289 if (!gtk_widget_get_mapped (widget))
290 {
291 g_signal_connect (widget, "map",
292 G_CALLBACK (gail_map_cb),
293 NULL);
294 }
295 else
296 gail_finish_select (widget);
297
298 return TRUE;
299 }
300
301 static void
302 gail_finish_select (GtkWidget *widget)
303 {
304 if (GTK_IS_MENU_ITEM (widget))
305 {
306 GtkMenuItem* menu_item;
307 GtkWidget *submenu;
308
309 menu_item = GTK_MENU_ITEM (widget);
310 submenu = gtk_menu_item_get_submenu (menu_item);
311 if (submenu &&
312 !gtk_widget_get_mapped (submenu))
313 {
314 /*
315 * If the submenu is not visble, wait until it is before
316 * reporting focus on the menu item.
317 */
318 gulong handler_id;
319
320 handler_id = g_signal_handler_find (submenu,
321 G_SIGNAL_MATCH_FUNC,
322 g_signal_lookup ("map",
323 GTK_TYPE_WINDOW),
324 0,
325 NULL,
326 (gpointer) gail_map_submenu_cb,
327 NULL);
328 if (!handler_id)
329 g_signal_connect (submenu, "map",
330 G_CALLBACK (gail_map_submenu_cb),
331 NULL);
332
333 return;
334 }
335 /*
336 * If we are waiting to report focus on a menubar or a menu item
337 * because of a previous deselect, cancel it.
338 */
339 if (was_deselect &&
340 focus_notify_handler &&
341 next_focus_widget &&
342 (GTK_IS_MENU_BAR (next_focus_widget) ||
343 GTK_IS_MENU_ITEM (next_focus_widget)))
344 {
345 void *vp_next_focus_widget = &next_focus_widget;
346 g_source_remove (focus_notify_handler);
347 g_object_remove_weak_pointer (G_OBJECT (next_focus_widget), vp_next_focus_widget);
348 next_focus_widget = NULL;
349 focus_notify_handler = 0;
350 was_deselect = FALSE;
351 }
352 }
353 /*
354 * If previously focused widget is not a GtkMenuItem or a GtkMenu,
355 * keep track of it so we can return to it after menubar is deactivated
356 */
357 if (_focus_widget &&
358 !GTK_IS_MENU_ITEM (_focus_widget) &&
359 !GTK_IS_MENU (_focus_widget))
360 {
361 void *vp_focus_before_menu = &focus_before_menu;
362 focus_before_menu = _focus_widget;
363 g_object_add_weak_pointer (G_OBJECT (focus_before_menu), vp_focus_before_menu);
364
365 }
366 gail_focus_notify_when_idle (widget);
367
368 return;
369 }
370
371 static void
372 gail_map_cb (GtkWidget *widget)
373 {
374 gail_finish_select (widget);
375 }
376
377 static void
378 gail_map_submenu_cb (GtkWidget *widget)
379 {
380 if (GTK_IS_MENU (widget))
381 {
382 GtkWidget *parent_menu_item;
383
384 parent_menu_item = gtk_menu_get_attach_widget (GTK_MENU (widget));
385 if (parent_menu_item)
386 gail_finish_select (parent_menu_item);
387 }
388 }
389
390
391 static gboolean
392 gail_deselect_watcher (GSignalInvocationHint *ihint,
393 guint n_param_values,
394 const GValue *param_values,
395 gpointer data)
396 {
397 GObject *object;
398 GtkWidget *widget;
399 GtkWidget *menu_shell;
400
401 object = g_value_get_object (param_values + 0);
402 g_return_val_if_fail (GTK_IS_WIDGET(object), FALSE);
403
404 widget = GTK_WIDGET (object);
405
406 if (!GTK_IS_MENU_ITEM (widget))
407 return TRUE;
408
409 if (subsequent_focus_widget == widget)
410 subsequent_focus_widget = NULL;
411
412 menu_shell = gtk_widget_get_parent (widget);
413 if (GTK_IS_MENU_SHELL (menu_shell))
414 {
415 GtkWidget *parent_menu_shell;
416
417 parent_menu_shell = gtk_menu_shell_get_parent_shell (GTK_MENU_SHELL (menu_shell));
418 if (parent_menu_shell)
419 {
420 GtkWidget *active_menu_item;
421
422 active_menu_item = gtk_menu_shell_get_selected_item (GTK_MENU_SHELL (parent_menu_shell));
423 if (active_menu_item)
424 {
425 gail_focus_notify_when_idle (active_menu_item);
426 }
427 }
428 else
429 {
430 if (!GTK_IS_MENU_BAR (menu_shell))
431 {
432 gail_focus_notify_when_idle (menu_shell);
433 }
434 }
435 }
436 was_deselect = TRUE;
437 return TRUE;
438 }
439
440 static gboolean
441 gail_switch_page_watcher (GSignalInvocationHint *ihint,
442 guint n_param_values,
443 const GValue *param_values,
444 gpointer data)
445 {
446 GObject *object;
447 GtkWidget *widget;
448
449 object = g_value_get_object (param_values + 0);
450 g_return_val_if_fail (GTK_IS_WIDGET(object), FALSE);
451
452 widget = GTK_WIDGET (object);
453
454 if (!GTK_IS_NOTEBOOK (widget))
455 return TRUE;
456
457 if (gtk_notebook_get_current_page (GTK_NOTEBOOK (widget)) == -1)
458 return TRUE;
459
460 gail_focus_notify_when_idle (widget);
461 return TRUE;
462 }
463
464 static gboolean
465 gail_focus_idle_handler (gpointer data)
466 {
467 focus_notify_handler = 0;
468 /*
469 * The widget which was to receive focus may have been removed
470 */
471 if (!next_focus_widget)
472 {
473 if (next_focus_widget != data)
474 return FALSE;
475 }
476 else
477 {
478 void *vp_next_focus_widget = &next_focus_widget;
479 g_object_remove_weak_pointer (G_OBJECT (next_focus_widget), vp_next_focus_widget);
480 next_focus_widget = NULL;
481 }
482
483 gail_focus_notify (data);
484
485 return FALSE;
486 }
487
488 static void
489 gail_focus_notify (GtkWidget *widget)
490 {
491 AtkObject *atk_obj;
492 gboolean transient;
493
494 if (widget != _focus_widget)
495 {
496 if (_focus_widget)
497 {
498 void *vp_focus_widget = &_focus_widget;
499 g_object_remove_weak_pointer (G_OBJECT (_focus_widget), vp_focus_widget);
500 }
501 _focus_widget = widget;
502 if (_focus_widget)
503 {
504 void *vp_focus_widget = &_focus_widget;
505 g_object_add_weak_pointer (G_OBJECT (_focus_widget), vp_focus_widget);
506 /*
507 * The UI may not have been updated yet; e.g. in gtkhtml2
508 * html_view_layout() is called in a idle handler
509 */
510 if (_focus_widget == focus_before_menu)
511 {
512 void *vp_focus_before_menu = &focus_before_menu;
513 g_object_remove_weak_pointer (G_OBJECT (focus_before_menu), vp_focus_before_menu);
514 focus_before_menu = NULL;
515 }
516 }
517 gail_focus_notify_when_idle (_focus_widget);
518 }
519 else
520 {
521 if (_focus_widget)
522 atk_obj = get_accessible_for_widget (_focus_widget, &transient);
523 else
524 atk_obj = NULL;
525 /*
526 * Do not report focus on redundant object
527 */
528 if (atk_obj &&
529 (atk_object_get_role(atk_obj) != ATK_ROLE_REDUNDANT_OBJECT))
530 atk_object_notify_state_change (atk_obj, ATK_STATE_FOCUSED, TRUE);
531 if (atk_obj && transient)
532 g_object_unref (atk_obj);
533 if (subsequent_focus_widget)
534 {
535 GtkWidget *tmp_widget = subsequent_focus_widget;
536 subsequent_focus_widget = NULL;
537 gail_focus_notify_when_idle (tmp_widget);
538 }
539 }
540 }
541
542 static void
543 gail_focus_notify_when_idle (GtkWidget *widget)
544 {
545 if (focus_notify_handler)
546 {
547 if (widget)
548 {
549 /*
550 * Ignore focus request when menu item is going to be focused.
551 * See bug #124232.
552 */
553 if (GTK_IS_MENU_ITEM (next_focus_widget) && !GTK_IS_MENU_ITEM (widget))
554 return;
555
556 if (next_focus_widget)
557 {
558 if (GTK_IS_MENU_ITEM (next_focus_widget) && GTK_IS_MENU_ITEM (widget))
559 {
560 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (next_focus_widget)) == gtk_widget_get_parent (widget))
561 {
562 if (subsequent_focus_widget)
563 g_assert_not_reached ();
564 subsequent_focus_widget = widget;
565 return;
566 }
567 }
568 }
569 g_source_remove (focus_notify_handler);
570 if (next_focus_widget)
571 {
572 void *vp_next_focus_widget = &next_focus_widget;
573 g_object_remove_weak_pointer (G_OBJECT (next_focus_widget), vp_next_focus_widget);
574 next_focus_widget = NULL;
575 }
576 }
577 else
578 /*
579 * Ignore if focus is being set to NULL and we are waiting to set focus
580 */
581 return;
582 }
583
584 if (widget)
585 {
586 void *vp_next_focus_widget = &next_focus_widget;
587 next_focus_widget = widget;
588 g_object_add_weak_pointer (G_OBJECT (next_focus_widget), vp_next_focus_widget);
589 }
590 else
591 {
592 /*
593 * We are about to report focus as NULL so remove the weak pointer
594 * for the widget we were waiting to report focus on.
595 */
596 if (next_focus_widget)
597 {
598 void *vp_next_focus_widget = &next_focus_widget;
599 g_object_remove_weak_pointer (G_OBJECT (next_focus_widget), vp_next_focus_widget);
600 next_focus_widget = NULL;
601 }
602 }
603
604 focus_notify_handler = gdk_threads_add_idle (gail_focus_idle_handler, widget);
605 g_source_set_name_by_id (focus_notify_handler, "[gtk+] gail_focus_idle_handler");
606 }
607
608 static gboolean
609 gail_deactivate_watcher (GSignalInvocationHint *ihint,
610 guint n_param_values,
611 const GValue *param_values,
612 gpointer data)
613 {
614 GObject *object;
615 GtkWidget *widget;
616 GtkMenuShell *shell;
617 GtkWidget *focus = NULL;
618
619 object = g_value_get_object (param_values + 0);
620 g_return_val_if_fail (GTK_IS_WIDGET(object), FALSE);
621 widget = GTK_WIDGET (object);
622
623 g_return_val_if_fail (GTK_IS_MENU_SHELL(widget), TRUE);
624 shell = GTK_MENU_SHELL(widget);
625 if (! gtk_menu_shell_get_parent_shell (shell))
626 focus = focus_before_menu;
627
628 /*
629 * If we are waiting to report focus on a menubar or a menu item
630 * because of a previous deselect, cancel it.
631 */
632 if (was_deselect &&
633 focus_notify_handler &&
634 next_focus_widget &&
635 (GTK_IS_MENU_BAR (next_focus_widget) ||
636 GTK_IS_MENU_ITEM (next_focus_widget)))
637 {
638 void *vp_next_focus_widget = &next_focus_widget;
639 g_source_remove (focus_notify_handler);
640 g_object_remove_weak_pointer (G_OBJECT (next_focus_widget), vp_next_focus_widget);
641 next_focus_widget = NULL;
642 focus_notify_handler = 0;
643 was_deselect = FALSE;
644 }
645 gail_focus_notify_when_idle (focus);
646
647 return TRUE;
648 }
649
650 static void
651 gail_focus_tracker_init (void)
652 {
653 static gboolean emission_hooks_added = FALSE;
654
655 if (!emission_hooks_added)
656 {
657 /*
658 * We cannot be sure that the classes exist so we make sure that they do.
659 */
660 g_type_class_ref (GTK_TYPE_WIDGET);
661 g_type_class_ref (GTK_TYPE_MENU_ITEM);
662 g_type_class_ref (GTK_TYPE_MENU_SHELL);
663 g_type_class_ref (GTK_TYPE_NOTEBOOK);
664
665 /*
666 * We listen for event_after signal and then check that the
667 * event was a focus in event so we get called after the event.
668 */
669 g_signal_add_emission_hook (
670 g_signal_lookup ("event-after", GTK_TYPE_WIDGET), 0,
671 gail_focus_watcher, NULL, (GDestroyNotify) NULL);
672 /*
673 * A "select" signal is emitted when arrow key is used to
674 * move to a list item in the popup window of a GtkCombo or
675 * a menu item in a menu.
676 */
677 g_signal_add_emission_hook (
678 g_signal_lookup ("select", GTK_TYPE_MENU_ITEM), 0,
679 gail_select_watcher, NULL, (GDestroyNotify) NULL);
680
681 /*
682 * A "deselect" signal is emitted when arrow key is used to
683 * move from a menu item in a menu to the parent menu.
684 */
685 g_signal_add_emission_hook (
686 g_signal_lookup ("deselect", GTK_TYPE_MENU_ITEM), 0,
687 gail_deselect_watcher, NULL, (GDestroyNotify) NULL);
688
689 /*
690 * We listen for deactivate signals on menushells to determine
691 * when the "focus" has left the menus.
692 */
693 g_signal_add_emission_hook (
694 g_signal_lookup ("deactivate", GTK_TYPE_MENU_SHELL), 0,
695 gail_deactivate_watcher, NULL, (GDestroyNotify) NULL);
696
697 /*
698 * We listen for "switch-page" signal on a GtkNotebook to notify
699 * when page has changed because of clicking on a notebook tab.
700 */
701 g_signal_add_emission_hook (
702 g_signal_lookup ("switch-page", GTK_TYPE_NOTEBOOK), 0,
703 gail_switch_page_watcher, NULL, (GDestroyNotify) NULL);
704 emission_hooks_added = TRUE;
705 }
706 }
707
708 static void
709 gail_focus_object_destroyed (gpointer data)
710 {
711 GObject *obj;
712
713 obj = G_OBJECT (data);
714 g_object_set_qdata (obj, quark_focus_object, NULL);
715 g_object_unref (obj);
716 }
717
718 static void
719 gail_focus_tracker (AtkObject *focus_object)
720 {
721 /*
722 * Do not report focus on redundant object
723 */
724 if (focus_object &&
725 (atk_object_get_role(focus_object) != ATK_ROLE_REDUNDANT_OBJECT))
726 {
727 AtkObject *old_focus_object;
728
729 if (!GTK_IS_ACCESSIBLE (focus_object))
730 {
731 AtkObject *parent;
732
733 parent = focus_object;
734 while (1)
735 {
736 parent = atk_object_get_parent (parent);
737 if (parent == NULL)
738 break;
739 if (GTK_IS_ACCESSIBLE (parent))
740 break;
741 }
742
743 if (parent)
744 {
745 gail_set_focus_object (focus_object, parent);
746 }
747 }
748 else
749 {
750 old_focus_object = g_object_get_qdata (G_OBJECT (focus_object), quark_focus_object);
751 if (old_focus_object)
752 {
753 g_object_weak_unref (G_OBJECT (old_focus_object),
754 (GWeakNotify) gail_focus_object_destroyed,
755 focus_object);
756 g_object_set_qdata (G_OBJECT (focus_object), quark_focus_object, NULL);
757 g_object_unref (G_OBJECT (focus_object));
758 }
759 }
760 }
761 }
762
763 static void
764 gail_set_focus_widget (GtkWidget *focus_widget,
765 GtkWidget *widget)
766 {
767 AtkObject *focus_obj;
768 AtkObject *obj;
769
770 focus_obj = gtk_widget_get_accessible (focus_widget);
771 obj = gtk_widget_get_accessible (widget);
772 gail_set_focus_object (focus_obj, obj);
773 }
774
775 static void
776 gail_set_focus_object (AtkObject *focus_obj,
777 AtkObject *obj)
778 {
779 AtkObject *old_focus_obj;
780
781 old_focus_obj = g_object_get_qdata (G_OBJECT (obj), quark_focus_object);
782 if (old_focus_obj != obj)
783 {
784 if (old_focus_obj)
785 g_object_weak_unref (G_OBJECT (old_focus_obj),
786 (GWeakNotify) gail_focus_object_destroyed,
787 obj);
788 else
789 /*
790 * We call g_object_ref as if obj is destroyed
791 * while the weak reference exists then destroying the
792 * focus_obj would cause gail_focus_object_destroyed to be
793 * called when obj is not a valid GObject.
794 */
795 g_object_ref (obj);
796
797 g_object_weak_ref (G_OBJECT (focus_obj),
798 (GWeakNotify) gail_focus_object_destroyed,
799 obj);
800 g_object_set_qdata (G_OBJECT (obj), quark_focus_object, focus_obj);
801 }
802 }
803
804 static gboolean
805 state_event_watcher (GSignalInvocationHint *hint,
806 guint n_param_values,
807 const GValue *param_values,
808 gpointer data)
809 {
810 GObject *object;
811 GtkWidget *widget;
812 AtkObject *atk_obj;
813 AtkObject *parent;
814 GdkEventWindowState *event;
815 gchar *signal_name;
816
817 object = g_value_get_object (param_values + 0);
818 if (!GTK_IS_WINDOW (object))
819 return FALSE;
820
821 event = g_value_get_boxed (param_values + 1);
822 if (event->type == GDK_WINDOW_STATE)
823 return FALSE;
824 widget = GTK_WIDGET (object);
825
826 if (event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED)
827 signal_name = "maximize";
828 else if (event->new_window_state & GDK_WINDOW_STATE_ICONIFIED)
829 signal_name = "minimize";
830 else if (event->new_window_state == 0)
831 signal_name = "restore";
832 else
833 return TRUE;
834
835 atk_obj = gtk_widget_get_accessible (widget);
836 if (GTK_IS_WINDOW_ACCESSIBLE (atk_obj))
837 {
838 parent = atk_object_get_parent (atk_obj);
839 if (parent == atk_get_root ())
840 g_signal_emit_by_name (atk_obj, signal_name);
841
842 return TRUE;
843 }
844
845 return FALSE;
846 }
847
848 static gboolean
849 configure_event_watcher (GSignalInvocationHint *hint,
850 guint n_param_values,
851 const GValue *param_values,
852 gpointer data)
853 {
854 GtkAllocation allocation;
855 GObject *object;
856 GtkWidget *widget;
857 AtkObject *atk_obj;
858 AtkObject *parent;
859 GdkEvent *event;
860 gchar *signal_name;
861
862 object = g_value_get_object (param_values + 0);
863 if (!GTK_IS_WINDOW (object))
864 return FALSE;
865
866 event = g_value_get_boxed (param_values + 1);
867 if (event->type != GDK_CONFIGURE)
868 return FALSE;
869 widget = GTK_WIDGET (object);
870 gtk_widget_get_allocation (widget, &allocation);
871 if (allocation.x == ((GdkEventConfigure *)event)->x &&
872 allocation.y == ((GdkEventConfigure *)event)->y &&
873 allocation.width == ((GdkEventConfigure *)event)->width &&
874 allocation.height == ((GdkEventConfigure *)event)->height)
875 return TRUE;
876
877 if (allocation.width != ((GdkEventConfigure *)event)->width ||
878 allocation.height != ((GdkEventConfigure *)event)->height)
879 signal_name = "resize";
880 else
881 signal_name = "move";
882
883 atk_obj = gtk_widget_get_accessible (widget);
884 if (GTK_IS_WINDOW_ACCESSIBLE (atk_obj))
885 {
886 parent = atk_object_get_parent (atk_obj);
887 if (parent == atk_get_root ())
888 g_signal_emit_by_name (atk_obj, signal_name);
889
890 return TRUE;
891 }
892
893 return FALSE;
894 }
895
896 static gboolean
897 window_focus (GtkWidget *widget,
898 GdkEventFocus *event)
899 {
900 AtkObject *atk_obj;
901
902 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
903
904 atk_obj = gtk_widget_get_accessible (widget);
905 g_signal_emit_by_name (atk_obj, event->in ? "activate" : "deactivate");
906
907 return FALSE;
908 }
909
910 static void
911 window_added (AtkObject *atk_obj,
912 guint index,
913 AtkObject *child)
914 {
915 GtkWidget *widget;
916
917 if (!GTK_IS_WINDOW_ACCESSIBLE (child))
918 return;
919
920 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (child));
921 if (!widget)
922 return;
923
924 g_signal_connect (widget, "focus-in-event", (GCallback) window_focus, NULL);
925 g_signal_connect (widget, "focus-out-event", (GCallback) window_focus, NULL);
926 g_signal_emit_by_name (child, "create");
927 }
928
929 static void
930 window_removed (AtkObject *atk_obj,
931 guint index,
932 AtkObject *child)
933 {
934 GtkWidget *widget;
935 GtkWindow *window;
936
937 if (!GTK_IS_WINDOW_ACCESSIBLE (child))
938 return;
939
940 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (child));
941 if (!widget)
942 return;
943
944 window = GTK_WINDOW (widget);
945 /*
946 * Deactivate window if it is still focused and we are removing it. This
947 * can happen when a dialog displayed by gok is removed.
948 */
949 if (gtk_window_is_active (window) && gtk_window_has_toplevel_focus (window))
950 g_signal_emit_by_name (child, "deactivate");
951
952 g_signal_handlers_disconnect_by_func (widget, (gpointer) window_focus, NULL);
953 g_signal_emit_by_name (child, "destroy");
954 }
955
956 static void
957 do_window_event_initialization (void)
958 {
959 AtkObject *root;
960
961 g_type_class_ref (GTK_TYPE_WINDOW_ACCESSIBLE);
962 g_signal_add_emission_hook (g_signal_lookup ("window-state-event", GTK_TYPE_WIDGET),
963 0, state_event_watcher, NULL, (GDestroyNotify) NULL);
964 g_signal_add_emission_hook (g_signal_lookup ("configure-event", GTK_TYPE_WIDGET),
965 0, configure_event_watcher, NULL, (GDestroyNotify) NULL);
966
967 root = atk_get_root ();
968 g_signal_connect (root, "children-changed::add", (GCallback) window_added, NULL);
969 g_signal_connect (root, "children-changed::remove", (GCallback) window_removed, NULL);
970 }
971
972 void
973 _gtk_accessibility_init (void)
974 {
975 if (initialized)
976 return;
977
978 initialized = TRUE;
979 quark_focus_object = g_quark_from_static_string ("gail-focus-object");
980
981 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
982 atk_focus_tracker_init (gail_focus_tracker_init);
983 focus_tracker_id = atk_add_focus_tracker (gail_focus_tracker);
984 G_GNUC_END_IGNORE_DEPRECATIONS;
985
986 _gtk_accessibility_override_atk_util ();
987 do_window_event_initialization ();
988
989 #ifdef GDK_WINDOWING_X11
990 atk_bridge_adaptor_init (NULL, NULL);
991 #endif
992
993 atk_misc_instance = g_object_new (GTK_TYPE_MISC_IMPL, NULL);
994 }