]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtkwidgetaccessible.c
Merge branch 'bgo687196-filesystemmodel-crash'
[~andy/gtk] / gtk / a11y / gtkwidgetaccessible.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 <gtk/gtk.h>
21 #ifdef GDK_WINDOWING_X11
22 #include <gdk/x11/gdkx.h>
23 #endif
24 #include "gtkwidgetaccessible.h"
25 #include "gtknotebookpageaccessible.h"
26
27 struct _GtkWidgetAccessiblePrivate
28 {
29   AtkLayer layer;
30 };
31
32 #define TOOLTIP_KEY "tooltip"
33
34 extern GtkWidget *_focus_widget;
35
36
37 static gboolean gtk_widget_accessible_on_screen           (GtkWidget *widget);
38 static gboolean gtk_widget_accessible_all_parents_visible (GtkWidget *widget);
39
40 static void atk_component_interface_init (AtkComponentIface *iface);
41
42 G_DEFINE_TYPE_WITH_CODE (GtkWidgetAccessible, _gtk_widget_accessible, GTK_TYPE_ACCESSIBLE,
43                          G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init))
44
45 /* Translate GtkWidget::focus-in/out-event to AtkObject::focus-event */
46 static gboolean
47 focus_cb (GtkWidget     *widget,
48           GdkEventFocus *event)
49 {
50   AtkObject *obj;
51
52   obj = gtk_widget_get_accessible (widget);
53
54   g_signal_emit_by_name (obj, "focus-event", event->in);
55
56   return FALSE;
57 }
58
59 /* Translate GtkWidget property change notification to the notify_gtk vfunc */
60 static void
61 notify_cb (GObject    *obj,
62            GParamSpec *pspec)
63 {
64   GtkWidgetAccessible *widget;
65   GtkWidgetAccessibleClass *klass;
66
67   widget = GTK_WIDGET_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (obj)));
68   klass = GTK_WIDGET_ACCESSIBLE_GET_CLASS (widget);
69   if (klass->notify_gtk)
70     klass->notify_gtk (obj, pspec);
71 }
72
73 /* Translate GtkWidget::size-allocate to AtkComponent::bounds-changed */
74 static void
75 size_allocate_cb (GtkWidget     *widget,
76                   GtkAllocation *allocation)
77 {
78   AtkObject* accessible;
79   AtkRectangle rect;
80
81   accessible = gtk_widget_get_accessible (widget);
82   if (ATK_IS_COMPONENT (accessible))
83     {
84       rect.x = allocation->x;
85       rect.y = allocation->y;
86       rect.width = allocation->width;
87       rect.height = allocation->height;
88       g_signal_emit_by_name (accessible, "bounds-changed", &rect);
89     }
90 }
91
92 /* Translate GtkWidget mapped state into AtkObject showing */
93 static gint
94 map_cb (GtkWidget *widget)
95 {
96   AtkObject *accessible;
97
98   accessible = gtk_widget_get_accessible (widget);
99   atk_object_notify_state_change (accessible, ATK_STATE_SHOWING,
100                                   gtk_widget_get_mapped (widget));
101   return 1;
102 }
103
104 static void
105 gtk_widget_accessible_focus_event (AtkObject *obj,
106                                    gboolean   focus_in)
107 {
108   AtkObject *focus_obj;
109
110   focus_obj = g_object_get_data (G_OBJECT (obj), "gail-focus-object");
111   if (focus_obj == NULL)
112     focus_obj = obj;
113   atk_object_notify_state_change (focus_obj, ATK_STATE_FOCUSED, focus_in);
114 }
115
116 static void
117 gtk_widget_accessible_update_tooltip (GtkWidgetAccessible *accessible,
118                                       GtkWidget *widget)
119 {
120   g_object_set_data_full (G_OBJECT (accessible),
121                           TOOLTIP_KEY,
122                           gtk_widget_get_tooltip_text (widget),
123                           g_free);
124 }
125
126 static void
127 gtk_widget_accessible_initialize (AtkObject *obj,
128                                   gpointer   data)
129 {
130   GtkWidget *widget;
131
132   widget = GTK_WIDGET (data);
133
134   g_signal_connect_after (widget, "focus-in-event", G_CALLBACK (focus_cb), NULL);
135   g_signal_connect_after (widget, "focus-out-event", G_CALLBACK (focus_cb), NULL);
136   g_signal_connect (widget, "notify", G_CALLBACK (notify_cb), NULL);
137   g_signal_connect (widget, "size-allocate", G_CALLBACK (size_allocate_cb), NULL);
138   g_signal_connect (widget, "map", G_CALLBACK (map_cb), NULL);
139   g_signal_connect (widget, "unmap", G_CALLBACK (map_cb), NULL);
140
141   GTK_WIDGET_ACCESSIBLE (obj)->priv->layer = ATK_LAYER_WIDGET;
142   obj->role = ATK_ROLE_UNKNOWN;
143
144   gtk_widget_accessible_update_tooltip (GTK_WIDGET_ACCESSIBLE (obj), widget);
145 }
146
147 static const gchar *
148 gtk_widget_accessible_get_description (AtkObject *accessible)
149 {
150   GtkWidget *widget;
151
152   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
153   if (widget == NULL)
154     return NULL;
155
156   if (accessible->description)
157     return accessible->description;
158
159   return g_object_get_data (G_OBJECT (accessible), TOOLTIP_KEY);
160 }
161
162 static AtkObject *
163 gtk_widget_accessible_get_parent (AtkObject *accessible)
164 {
165   AtkObject *parent;
166   GtkWidget *widget, *parent_widget;
167
168   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
169   if (widget == NULL)
170     return NULL;
171
172   parent = accessible->accessible_parent;
173   if (parent != NULL)
174     return parent;
175
176   parent_widget = gtk_widget_get_parent (widget);
177   if (parent_widget == NULL)
178     return NULL;
179
180   /* For a widget whose parent is a GtkNoteBook, we return the
181    * accessible object corresponding the GtkNotebookPage containing
182    * the widget as the accessible parent.
183    */
184   if (GTK_IS_NOTEBOOK (parent_widget))
185     {
186       gint page_num;
187       GtkWidget *child;
188       GtkNotebook *notebook;
189
190       page_num = 0;
191       notebook = GTK_NOTEBOOK (parent_widget);
192       while (TRUE)
193         {
194           child = gtk_notebook_get_nth_page (notebook, page_num);
195           if (!child)
196             break;
197           if (child == widget)
198             {
199               parent = gtk_widget_get_accessible (parent_widget);
200               parent = atk_object_ref_accessible_child (parent, page_num);
201               g_object_unref (parent);
202               return parent;
203             }
204           page_num++;
205         }
206     }
207   parent = gtk_widget_get_accessible (parent_widget);
208   return parent;
209 }
210
211 static GtkWidget *
212 find_label (GtkWidget *widget)
213 {
214   GList *labels;
215   GtkWidget *label;
216   GtkWidget *temp_widget;
217   GList *ptr;
218
219   labels = gtk_widget_list_mnemonic_labels (widget);
220   label = NULL;
221   ptr = labels;
222   while (ptr)
223     {
224       if (ptr->data)
225         {
226           label = ptr->data;
227           break;
228         }
229       ptr = ptr->next;
230     }
231   g_list_free (labels);
232
233   /* Ignore a label within a button; bug #136602 */
234   if (label && GTK_IS_BUTTON (widget))
235     {
236       temp_widget = label;
237       while (temp_widget)
238         {
239           if (temp_widget == widget)
240             {
241               label = NULL;
242               break;
243             }
244           temp_widget = gtk_widget_get_parent (temp_widget);
245         }
246     }
247   return label;
248 }
249
250 static AtkRelationSet *
251 gtk_widget_accessible_ref_relation_set (AtkObject *obj)
252 {
253   GtkWidget *widget;
254   AtkRelationSet *relation_set;
255   GtkWidget *label;
256   AtkObject *array[1];
257   AtkRelation* relation;
258
259   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
260   if (widget == NULL)
261     return NULL;
262
263   relation_set = ATK_OBJECT_CLASS (_gtk_widget_accessible_parent_class)->ref_relation_set (obj);
264
265   if (GTK_IS_BOX (widget))
266     return relation_set;
267
268   if (!atk_relation_set_contains (relation_set, ATK_RELATION_LABELLED_BY))
269     {
270       label = find_label (widget);
271       if (label == NULL)
272         {
273           if (GTK_IS_BUTTON (widget))
274             /*
275              * Handle the case where GnomeIconEntry is the mnemonic widget.
276              * The GtkButton which is a grandchild of the GnomeIconEntry
277              * should really be the mnemonic widget. See bug #133967.
278              */
279             {
280               GtkWidget *temp_widget;
281
282               temp_widget = gtk_widget_get_parent (widget);
283
284               if (GTK_IS_ALIGNMENT (temp_widget))
285                 {
286                   temp_widget = gtk_widget_get_parent (temp_widget);
287                   if (GTK_IS_BOX (temp_widget))
288                     {
289                       label = find_label (temp_widget);
290                       if (!label)
291                         label = find_label (gtk_widget_get_parent (temp_widget));
292                     }
293                 }
294             }
295           else if (GTK_IS_COMBO_BOX (widget))
296             /*
297              * Handle the case when GtkFileChooserButton is the mnemonic
298              * widget.  The GtkComboBox which is a child of the
299              * GtkFileChooserButton should be the mnemonic widget.
300              * See bug #359843.
301              */
302             {
303               GtkWidget *temp_widget;
304
305               temp_widget = gtk_widget_get_parent (widget);
306               if (GTK_IS_BOX (temp_widget))
307                 {
308                   label = find_label (temp_widget);
309                 }
310             }
311         }
312
313       if (label)
314         {
315           array[0] = gtk_widget_get_accessible (label);
316
317           relation = atk_relation_new (array, 1, ATK_RELATION_LABELLED_BY);
318           atk_relation_set_add (relation_set, relation);
319           g_object_unref (relation);
320         }
321     }
322
323   return relation_set;
324 }
325
326 static AtkStateSet *
327 gtk_widget_accessible_ref_state_set (AtkObject *accessible)
328 {
329   GtkWidget *widget;
330   AtkStateSet *state_set;
331
332   state_set = ATK_OBJECT_CLASS (_gtk_widget_accessible_parent_class)->ref_state_set (accessible);
333
334   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
335   if (widget == NULL)
336     atk_state_set_add_state (state_set, ATK_STATE_DEFUNCT);
337   else
338     {
339       if (gtk_widget_is_sensitive (widget))
340         {
341           atk_state_set_add_state (state_set, ATK_STATE_SENSITIVE);
342           atk_state_set_add_state (state_set, ATK_STATE_ENABLED);
343         }
344   
345       if (gtk_widget_get_can_focus (widget))
346         {
347           atk_state_set_add_state (state_set, ATK_STATE_FOCUSABLE);
348         }
349       /*
350        * We do not currently generate notifications when an ATK object
351        * corresponding to a GtkWidget changes visibility by being scrolled
352        * on or off the screen.  The testcase for this is the main window
353        * of the testgtk application in which a set of buttons in a GtkVBox
354        * is in a scrolled window with a viewport.
355        *
356        * To generate the notifications we would need to do the following:
357        * 1) Find the GtkViewport among the ancestors of the objects
358        * 2) Create an accessible for the viewport
359        * 3) Connect to the value-changed signal on the viewport
360        * 4) When the signal is received we need to traverse the children
361        *    of the viewport and check whether the children are visible or not
362        *    visible; we may want to restrict this to the widgets for which
363        *    accessible objects have been created.
364        * 5) We probably need to store a variable on_screen in the
365        *    GtkWidgetAccessible data structure so we can determine whether
366        *    the value has changed.
367        */
368       if (gtk_widget_get_visible (widget))
369         {
370           atk_state_set_add_state (state_set, ATK_STATE_VISIBLE);
371           if (gtk_widget_accessible_on_screen (widget) &&
372               gtk_widget_get_mapped (widget) &&
373               gtk_widget_accessible_all_parents_visible (widget))
374             atk_state_set_add_state (state_set, ATK_STATE_SHOWING);
375         }
376
377       if (gtk_widget_has_focus (widget) && (widget == _focus_widget))
378         {
379           AtkObject *focus_obj;
380
381           focus_obj = g_object_get_data (G_OBJECT (accessible), "gail-focus-object");
382           if (focus_obj == NULL)
383             atk_state_set_add_state (state_set, ATK_STATE_FOCUSED);
384         }
385
386       if (gtk_widget_has_default (widget))
387         atk_state_set_add_state (state_set, ATK_STATE_DEFAULT);
388
389       if (GTK_IS_ORIENTABLE (widget))
390         {
391           if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL)
392             atk_state_set_add_state (state_set, ATK_STATE_HORIZONTAL);
393           else
394             atk_state_set_add_state (state_set, ATK_STATE_VERTICAL);
395         }
396     }
397   return state_set;
398 }
399
400 static gint
401 gtk_widget_accessible_get_index_in_parent (AtkObject *accessible)
402 {
403   GtkWidget *widget;
404   GtkWidget *parent_widget;
405   gint index;
406   GList *children;
407
408   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
409
410   if (widget == NULL)
411     return -1;
412
413   if (accessible->accessible_parent)
414     {
415       AtkObject *parent;
416
417       parent = accessible->accessible_parent;
418
419       if (GTK_IS_NOTEBOOK_PAGE_ACCESSIBLE (parent))
420         return 0;
421       else
422         {
423           gint n_children, i;
424           gboolean found = FALSE;
425
426           n_children = atk_object_get_n_accessible_children (parent);
427           for (i = 0; i < n_children; i++)
428             {
429               AtkObject *child;
430
431               child = atk_object_ref_accessible_child (parent, i);
432               if (child == accessible)
433                 found = TRUE;
434
435               g_object_unref (child);
436               if (found)
437                 return i;
438             }
439         }
440     }
441
442   if (!GTK_IS_WIDGET (widget))
443     return -1;
444   parent_widget = gtk_widget_get_parent (widget);
445   if (!GTK_IS_CONTAINER (parent_widget))
446     return -1;
447
448   children = gtk_container_get_children (GTK_CONTAINER (parent_widget));
449
450   index = g_list_index (children, widget);
451   g_list_free (children);
452   return index;
453 }
454
455 /* This function is the default implementation for the notify_gtk
456  * vfunc which gets called when a property changes value on the
457  * GtkWidget associated with a GtkWidgetAccessible. It constructs
458  * an AtkPropertyValues structure and emits a "property_changed"
459  * signal which causes the user specified AtkPropertyChangeHandler
460  * to be called.
461  */
462 static void
463 gtk_widget_accessible_notify_gtk (GObject    *obj,
464                                   GParamSpec *pspec)
465 {
466   GtkWidget* widget = GTK_WIDGET (obj);
467   AtkObject* atk_obj = gtk_widget_get_accessible (widget);
468   AtkState state;
469   gboolean value;
470
471   if (g_strcmp0 (pspec->name, "has-focus") == 0)
472     /*
473      * We use focus-in-event and focus-out-event signals to catch
474      * focus changes so we ignore this.
475      */
476     return;
477   else if (g_strcmp0 (pspec->name, "visible") == 0)
478     {
479       state = ATK_STATE_VISIBLE;
480       value = gtk_widget_get_visible (widget);
481     }
482   else if (g_strcmp0 (pspec->name, "sensitive") == 0)
483     {
484       state = ATK_STATE_SENSITIVE;
485       value = gtk_widget_get_sensitive (widget);
486     }
487   else if (g_strcmp0 (pspec->name, "orientation") == 0 &&
488            GTK_IS_ORIENTABLE (widget))
489     {
490       GtkOrientable *orientable;
491
492       orientable = GTK_ORIENTABLE (widget);
493
494       state = ATK_STATE_HORIZONTAL;
495       value = (gtk_orientable_get_orientation (orientable) == GTK_ORIENTATION_HORIZONTAL);
496     }
497   else if (g_strcmp0 (pspec->name, "tooltip-text") == 0)
498     {
499       gtk_widget_accessible_update_tooltip (GTK_WIDGET_ACCESSIBLE (atk_obj),
500                                             widget);
501     }
502     return;
503
504   atk_object_notify_state_change (atk_obj, state, value);
505   if (state == ATK_STATE_SENSITIVE)
506     atk_object_notify_state_change (atk_obj, ATK_STATE_ENABLED, value);
507
508   if (state == ATK_STATE_HORIZONTAL)
509     atk_object_notify_state_change (atk_obj, ATK_STATE_VERTICAL, !value);
510 }
511
512 static AtkAttributeSet *
513 gtk_widget_accessible_get_attributes (AtkObject *obj)
514 {
515   AtkAttributeSet *attributes;
516   AtkAttribute *toolkit;
517
518   toolkit = g_new (AtkAttribute, 1);
519   toolkit->name = g_strdup ("toolkit");
520   toolkit->value = g_strdup ("gtk");
521
522   attributes = g_slist_append (NULL, toolkit);
523
524   return attributes;
525 }
526
527 static void
528 _gtk_widget_accessible_class_init (GtkWidgetAccessibleClass *klass)
529 {
530   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
531
532   klass->notify_gtk = gtk_widget_accessible_notify_gtk;
533
534   class->get_description = gtk_widget_accessible_get_description;
535   class->get_parent = gtk_widget_accessible_get_parent;
536   class->ref_relation_set = gtk_widget_accessible_ref_relation_set;
537   class->ref_state_set = gtk_widget_accessible_ref_state_set;
538   class->get_index_in_parent = gtk_widget_accessible_get_index_in_parent;
539   class->initialize = gtk_widget_accessible_initialize;
540   class->get_attributes = gtk_widget_accessible_get_attributes;
541   class->focus_event = gtk_widget_accessible_focus_event;
542
543   g_type_class_add_private (klass, sizeof (GtkWidgetAccessiblePrivate));
544 }
545
546 static void
547 _gtk_widget_accessible_init (GtkWidgetAccessible *accessible)
548 {
549   accessible->priv = G_TYPE_INSTANCE_GET_PRIVATE (accessible,
550                                                   GTK_TYPE_WIDGET_ACCESSIBLE,
551                                                   GtkWidgetAccessiblePrivate);
552 }
553
554 static void
555 gtk_widget_accessible_get_extents (AtkComponent   *component,
556                                    gint           *x,
557                                    gint           *y,
558                                    gint           *width,
559                                    gint           *height,
560                                    AtkCoordType    coord_type)
561 {
562   GdkWindow *window;
563   gint x_window, y_window;
564   gint x_toplevel, y_toplevel;
565   GtkWidget *widget;
566   GtkAllocation allocation;
567
568   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
569   if (widget == NULL)
570     return;
571
572   gtk_widget_get_allocation (widget, &allocation);
573   *width = allocation.width;
574   *height = allocation.height;
575   if (!gtk_widget_accessible_on_screen (widget) || (!gtk_widget_is_drawable (widget)))
576     {
577       *x = G_MININT;
578       *y = G_MININT;
579       return;
580     }
581
582   if (gtk_widget_get_parent (widget))
583     {
584       *x = allocation.x;
585       *y = allocation.y;
586       window = gtk_widget_get_parent_window (widget);
587     }
588   else
589     {
590       *x = 0;
591       *y = 0;
592       window = gtk_widget_get_window (widget);
593     }
594   gdk_window_get_origin (window, &x_window, &y_window);
595   *x += x_window;
596   *y += y_window;
597
598   if (coord_type == ATK_XY_WINDOW)
599     {
600       window = gdk_window_get_toplevel (gtk_widget_get_window (widget));
601       gdk_window_get_origin (window, &x_toplevel, &y_toplevel);
602
603       *x -= x_toplevel;
604       *y -= y_toplevel;
605     }
606 }
607
608 static void
609 gtk_widget_accessible_get_size (AtkComponent *component,
610                                 gint         *width,
611                                 gint         *height)
612 {
613   GtkWidget *widget;
614
615   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
616   if (widget == NULL)
617     return;
618
619   *width = gtk_widget_get_allocated_width (widget);
620   *height = gtk_widget_get_allocated_height (widget);
621 }
622
623 static AtkLayer
624 gtk_widget_accessible_get_layer (AtkComponent *component)
625 {
626   GtkWidgetAccessible *accessible = GTK_WIDGET_ACCESSIBLE (component);
627
628   return accessible->priv->layer;
629 }
630
631 static gboolean
632 gtk_widget_accessible_grab_focus (AtkComponent *component)
633 {
634   GtkWidget *widget;
635   GtkWidget *toplevel;
636
637   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
638   if (!widget)
639     return FALSE;
640
641   if (!gtk_widget_get_can_focus (widget))
642     return FALSE;
643
644   gtk_widget_grab_focus (widget);
645   toplevel = gtk_widget_get_toplevel (widget);
646   if (gtk_widget_is_toplevel (toplevel))
647     {
648 #ifdef GDK_WINDOWING_X11
649       gtk_window_present_with_time (GTK_WINDOW (toplevel),
650       gdk_x11_get_server_time (gtk_widget_get_window (widget)));
651 #else
652       gtk_window_present (GTK_WINDOW (toplevel));
653 #endif
654     }
655   return TRUE;
656 }
657
658 static gboolean
659 gtk_widget_accessible_set_extents (AtkComponent *component,
660                                    gint          x,
661                                    gint          y,
662                                    gint          width,
663                                    gint          height,
664                                    AtkCoordType  coord_type)
665 {
666   GtkWidget *widget;
667
668   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
669   if (widget == NULL)
670     return FALSE;
671
672   if (!gtk_widget_is_toplevel (widget))
673     return FALSE;
674
675   if (coord_type == ATK_XY_WINDOW)
676     {
677       gint x_current, y_current;
678       GdkWindow *window = gtk_widget_get_window (widget);
679
680       gdk_window_get_origin (window, &x_current, &y_current);
681       x_current += x;
682       y_current += y;
683       if (x_current < 0 || y_current < 0)
684         return FALSE;
685       else
686         {
687           gtk_window_move (GTK_WINDOW (widget), x_current, y_current);
688           gtk_widget_set_size_request (widget, width, height);
689           return TRUE;
690         }
691     }
692   else if (coord_type == ATK_XY_SCREEN)
693     {
694       gtk_window_move (GTK_WINDOW (widget), x, y);
695       gtk_widget_set_size_request (widget, width, height);
696       return TRUE;
697     }
698   return FALSE;
699 }
700
701 static gboolean
702 gtk_widget_accessible_set_position (AtkComponent *component,
703                                     gint          x,
704                                     gint          y,
705                                     AtkCoordType  coord_type)
706 {
707   GtkWidget *widget;
708
709   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
710   if (widget == NULL)
711     return FALSE;
712
713   if (gtk_widget_is_toplevel (widget))
714     {
715       if (coord_type == ATK_XY_WINDOW)
716         {
717           gint x_current, y_current;
718           GdkWindow *window = gtk_widget_get_window (widget);
719
720           gdk_window_get_origin (window, &x_current, &y_current);
721           x_current += x;
722           y_current += y;
723           if (x_current < 0 || y_current < 0)
724             return FALSE;
725           else
726             {
727               gtk_window_move (GTK_WINDOW (widget), x_current, y_current);
728               return TRUE;
729             }
730         }
731       else if (coord_type == ATK_XY_SCREEN)
732         {
733           gtk_window_move (GTK_WINDOW (widget), x, y);
734           return TRUE;
735         }
736     }
737   return FALSE;
738 }
739
740 static gboolean
741 gtk_widget_accessible_set_size (AtkComponent *component,
742                                 gint          width,
743                                 gint          height)
744 {
745   GtkWidget *widget;
746
747   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
748   if (widget == NULL)
749     return FALSE;
750
751   if (gtk_widget_is_toplevel (widget))
752     {
753       gtk_widget_set_size_request (widget, width, height);
754       return TRUE;
755     }
756   else
757    return FALSE;
758 }
759
760 static void
761 atk_component_interface_init (AtkComponentIface *iface)
762 {
763   iface->get_extents = gtk_widget_accessible_get_extents;
764   iface->get_size = gtk_widget_accessible_get_size;
765   iface->get_layer = gtk_widget_accessible_get_layer;
766   iface->grab_focus = gtk_widget_accessible_grab_focus;
767   iface->set_extents = gtk_widget_accessible_set_extents;
768   iface->set_position = gtk_widget_accessible_set_position;
769   iface->set_size = gtk_widget_accessible_set_size;
770 }
771
772 /* This function checks whether the widget has an ancestor which is
773  * a GtkViewport and, if so, whether any part of the widget intersects
774  * the visible rectangle of the GtkViewport.
775  */
776 static gboolean
777 gtk_widget_accessible_on_screen (GtkWidget *widget)
778 {
779   GtkAllocation allocation;
780   GtkWidget *viewport;
781   gboolean return_value;
782
783   gtk_widget_get_allocation (widget, &allocation);
784
785   viewport = gtk_widget_get_ancestor (widget, GTK_TYPE_VIEWPORT);
786   if (viewport)
787     {
788       GtkAllocation viewport_allocation;
789       GtkAdjustment *adjustment;
790       GdkRectangle visible_rect;
791
792       gtk_widget_get_allocation (viewport, &viewport_allocation);
793
794       adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (viewport));
795       visible_rect.y = gtk_adjustment_get_value (adjustment);
796       adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (viewport));
797       visible_rect.x = gtk_adjustment_get_value (adjustment);
798       visible_rect.width = viewport_allocation.width;
799       visible_rect.height = viewport_allocation.height;
800
801       if (((allocation.x + allocation.width) < visible_rect.x) ||
802          ((allocation.y + allocation.height) < visible_rect.y) ||
803          (allocation.x > (visible_rect.x + visible_rect.width)) ||
804          (allocation.y > (visible_rect.y + visible_rect.height)))
805         return_value = FALSE;
806       else
807         return_value = TRUE;
808     }
809   else
810     {
811       /* Check whether the widget has been placed of the screen.
812        * The widget may be MAPPED as when toolbar items do not
813        * fit on the toolbar.
814        */
815       if (allocation.x + allocation.width <= 0 &&
816           allocation.y + allocation.height <= 0)
817         return_value = FALSE;
818       else
819         return_value = TRUE;
820     }
821
822   return return_value;
823 }
824
825 /* Checks if all the predecessors (the parent widget, his parent, etc)
826  * are visible Used to check properly the SHOWING state.
827  */
828 static gboolean
829 gtk_widget_accessible_all_parents_visible (GtkWidget *widget)
830 {
831   GtkWidget *iter_parent = NULL;
832   gboolean result = TRUE;
833
834   for (iter_parent = gtk_widget_get_parent (widget); iter_parent;
835        iter_parent = gtk_widget_get_parent (iter_parent))
836     {
837       if (!gtk_widget_get_visible (iter_parent))
838         {
839           result = FALSE;
840           break;
841         }
842     }
843
844   return result;
845 }
846
847 void
848 _gtk_widget_accessible_set_layer (GtkWidgetAccessible *accessible,
849                                   AtkLayer             layer)
850 {
851   accessible->priv->layer = layer;
852 }