[java-atk-wrapper] Fix coordinates



commit 59cd44a0d1fa79f0dc105ad4bacf6cf5d3de9606
Author: Samuel Thibault <samuel thibault ens-lyon org>
Date:   Sat Jul 27 22:41:54 2019 +0200

    Fix coordinates
    
    Depending on caller preferences and Accessible interfaces they are either
    relative to the object, to the parent, to the window, or to the screen...

 jni/src/jawcomponent.c                            |   4 +-
 wrapper/org/GNOME/Accessibility/AtkComponent.java | 150 +++++++++++++++-------
 wrapper/org/GNOME/Accessibility/AtkCoordType.java |   1 +
 wrapper/org/GNOME/Accessibility/AtkImage.java     |  20 ++-
 wrapper/org/GNOME/Accessibility/AtkText.java      |  43 +++----
 5 files changed, 143 insertions(+), 75 deletions(-)
---
diff --git a/jni/src/jawcomponent.c b/jni/src/jawcomponent.c
index e2c1025..90d0622 100644
--- a/jni/src/jawcomponent.c
+++ b/jni/src/jawcomponent.c
@@ -220,9 +220,9 @@ jaw_component_get_extents (AtkComponent *component,
   jmethodID jmid = (*jniEnv)->GetMethodID(jniEnv,
                                           classAtkComponent,
                                           "get_extents",
-                                          "()Ljava/awt/Rectangle;");
+                                          "(I)Ljava/awt/Rectangle;");
 
-  jobject jrectangle = (*jniEnv)->CallObjectMethod(jniEnv, atk_component, jmid);
+  jobject jrectangle = (*jniEnv)->CallObjectMethod(jniEnv, atk_component, jmid, (jint) coord_type);
   (*jniEnv)->DeleteGlobalRef(jniEnv, atk_component);
 
   if (jrectangle == NULL)
diff --git a/wrapper/org/GNOME/Accessibility/AtkComponent.java 
b/wrapper/org/GNOME/Accessibility/AtkComponent.java
index b0cf883..6f841ac 100644
--- a/wrapper/org/GNOME/Accessibility/AtkComponent.java
+++ b/wrapper/org/GNOME/Accessibility/AtkComponent.java
@@ -40,49 +40,111 @@ public class AtkComponent {
       return AtkUtil.invokeInSwing ( () -> { return new AtkComponent(ac); }, null);
   }
 
+  static public Point getWindowLocation(AccessibleContext ac) {
+      while (ac != null) {
+          AccessibleRole role = ac.getAccessibleRole();
+          if (role == AccessibleRole.DIALOG ||
+              role == AccessibleRole.FRAME ||
+              role == AccessibleRole.WINDOW) {
+              AccessibleComponent acc_comp = ac.getAccessibleComponent();
+              if (acc_comp == null)
+                  return null;
+              return acc_comp.getLocationOnScreen();
+          }
+          Accessible parent = ac.getAccessibleParent();
+          if (parent == null)
+              return null;
+          ac = parent.getAccessibleContext();
+      }
+      return null;
+  }
+
+  // Return the position of the object relative to the coordinate type
+  public static Point getComponentOrigin(AccessibleContext ac, AccessibleComponent acc_component, int 
coord_type) {
+      if (coord_type == AtkCoordType.SCREEN)
+          return acc_component.getLocationOnScreen();
+
+      if (coord_type == AtkCoordType.WINDOW)
+      {
+          Point win_p = getWindowLocation(ac);
+         if (win_p == null)
+             return null;
+         Point p = acc_component.getLocationOnScreen();
+         if (p == null)
+             return null;
+         p.translate(-win_p.x, -win_p.y);
+         return p;
+      }
+
+      if (coord_type == AtkCoordType.PARENT)
+          return acc_component.getLocation();
+
+      return null;
+  }
+
+  // Return the position of the parent relative to the coordinate type
+  public static Point getParentOrigin(AccessibleContext ac, AccessibleComponent acc_component, int 
coord_type) {
+      if (coord_type == AtkCoordType.PARENT)
+          return new Point(0, 0);
+
+      Accessible parent = ac.getAccessibleParent();
+      if (parent == null)
+          return null;
+      AccessibleContext parent_ac = parent.getAccessibleContext();
+      if (parent_ac == null)
+          return null;
+      AccessibleComponent parent_component = parent_ac.getAccessibleComponent();
+      if (parent_component == null)
+          return null;
+
+      if (coord_type == AtkCoordType.SCREEN) {
+          return parent_component.getLocationOnScreen();
+      }
+
+      if (coord_type == AtkCoordType.WINDOW) {
+          Point window_origin = getWindowLocation(ac);
+          Point parent_origin = parent_component.getLocationOnScreen();
+          parent_origin.translate(-window_origin.x, -window_origin.y);
+         return parent_origin;
+      }
+      return null;
+  }
+
   public boolean contains (int x, int y, int coord_type) {
+      AccessibleContext ac = _ac.get();
+      if (ac == null)
+          return false;
       AccessibleComponent acc_component = _acc_component.get();
       if (acc_component == null)
           return false;
 
       return AtkUtil.invokeInSwing ( () -> {
-          if(!acc_component.isVisible()){
-              final int rightX;
-              final int rightY;
-              if (coord_type == AtkCoordType.SCREEN) {
-                  Point p = acc_component.getLocationOnScreen();
-                  rightX = x - p.x;
-                  rightY = y - p.y;
-              }
-              else{
-                  rightX = x;
-                  rightY = y;
-              }
-              return acc_component.contains(new Point(rightX, rightY));
+          if(acc_component.isVisible()){
+              Point p = getComponentOrigin(ac, acc_component, coord_type);
+              if (p == null)
+                  return false;
+
+              return acc_component.contains(new Point(x - p.x, y - p.y));
           }
           return false;
       }, false);
   }
 
   public AccessibleContext get_accessible_at_point (int x, int y, int coord_type) {
+      AccessibleContext ac = _ac.get();
+      if (ac == null)
+          return null;
       AccessibleComponent acc_component = _acc_component.get();
       if (acc_component == null)
           return null;
 
       return AtkUtil.invokeInSwing ( () -> {
-           if(acc_component.isVisible()){
-              final int rightX;
-              final int rightY;
-              if (coord_type == AtkCoordType.SCREEN) {
-                  Point p = acc_component.getLocationOnScreen();
-                  rightX = x - p.x;
-                  rightY = y - p.y;
-              }
-              else{
-                  rightX = x;
-                  rightY = y;
-              }
-              Accessible accessible = acc_component.getAccessibleAt(new Point(rightX, rightY));
+          if(acc_component.isVisible()){
+              Point p = getComponentOrigin(ac, acc_component, coord_type);
+              if (p == null)
+                  return null;
+
+              Accessible accessible = acc_component.getAccessibleAt(new Point(x - p.x, y - p.y));
               if (accessible == null)
                   return null;
               return accessible.getAccessibleContext();
@@ -105,31 +167,30 @@ public class AtkComponent {
     }
 
     public boolean set_extents(int x, int y, int width, int height, int coord_type) {
+        AccessibleContext ac = _ac.get();
+        if (ac == null)
+            return false;
         AccessibleComponent acc_component = _acc_component.get();
         if (acc_component == null)
             return false;
 
         return AtkUtil.invokeInSwing( () -> {
             if(acc_component.isVisible()){
-                final int rightX;
-                final int rightY;
-                if (coord_type == AtkCoordType.SCREEN) {
-                    Point p = acc_component.getLocationOnScreen();
-                    rightX = x - p.x;
-                    rightY = y - p.y;
-                }
-                else{
-                    rightX = x;
-                    rightY = y;
-                }
-                acc_component.setBounds(new Rectangle(rightX, rightY, width, height));
+                Point p = getParentOrigin(ac, acc_component, coord_type);
+                if (p == null)
+                    return false;
+
+                acc_component.setBounds(new Rectangle(x - p.x, y - p.y, width, height));
                 return true;
             }
             return false;
         }, false);
     }
 
-    public Rectangle get_extents() {
+    public Rectangle get_extents(int coord_type) {
+        AccessibleContext ac = _ac.get();
+        if (ac == null)
+            return null;
         AccessibleComponent acc_component = _acc_component.get();
         if (acc_component == null)
             return null;
@@ -137,11 +198,14 @@ public class AtkComponent {
         return AtkUtil.invokeInSwing ( () -> {
             if(acc_component.isVisible()){
                 Rectangle rect = acc_component.getBounds();
-                Point p = acc_component.getLocationOnScreen();
-                if (rect == null || p == null)
+                if (rect == null)
+                    return null;
+               Point p = getParentOrigin(ac, acc_component, coord_type);
+                if (p == null)
                     return null;
-                rect.x = p.x;
-                rect.y = p.y;
+
+                rect.x += p.x;
+                rect.y += p.y;
                 return rect;
             }
             return null;
diff --git a/wrapper/org/GNOME/Accessibility/AtkCoordType.java 
b/wrapper/org/GNOME/Accessibility/AtkCoordType.java
index b7902f9..fb2fccf 100644
--- a/wrapper/org/GNOME/Accessibility/AtkCoordType.java
+++ b/wrapper/org/GNOME/Accessibility/AtkCoordType.java
@@ -22,5 +22,6 @@ package org.GNOME.Accessibility;
 public interface AtkCoordType {
        public int SCREEN = 0;
        public int WINDOW = 1;
+       public int PARENT = 2;
 }
 
diff --git a/wrapper/org/GNOME/Accessibility/AtkImage.java b/wrapper/org/GNOME/Accessibility/AtkImage.java
index bc3c549..6a9dc9e 100644
--- a/wrapper/org/GNOME/Accessibility/AtkImage.java
+++ b/wrapper/org/GNOME/Accessibility/AtkImage.java
@@ -22,6 +22,7 @@ package org.GNOME.Accessibility;
 import javax.accessibility.*;
 import java.awt.Point;
 import java.awt.Dimension;
+import java.awt.Rectangle;
 import java.lang.ref.WeakReference;
 
 public class AtkImage {
@@ -48,9 +49,7 @@ public class AtkImage {
                        AccessibleComponent acc_component = ac.getAccessibleComponent();
                        if (acc_component == null)
                                return null;
-                       if (coord_type == AtkCoordType.SCREEN)
-                               return acc_component.getLocationOnScreen();
-                       return acc_component.getLocation();
+                       return AtkComponent.getComponentOrigin(ac, acc_component, coord_type);
                }, null);
        }
 
@@ -73,14 +72,25 @@ public class AtkImage {
        public Dimension get_image_size () {
                Dimension d = new Dimension(0, 0);
 
-               AccessibleIcon[] acc_icons = _acc_icons.get();
-               if (acc_icons == null)
+               AccessibleContext ac = _ac.get();
+               if (ac == null)
                        return d;
 
+               AccessibleIcon[] acc_icons = _acc_icons.get();
+
                return AtkUtil.invokeInSwing ( () -> {
                        if (acc_icons != null && acc_icons.length > 0) {
                                d.height = acc_icons[0].getAccessibleIconHeight();
                                d.width = acc_icons[0].getAccessibleIconWidth();
+                       } else {
+                               AccessibleComponent acc_component = ac.getAccessibleComponent();
+                               if (acc_component != null) {
+                                       Rectangle rect = acc_component.getBounds();
+                                       if (rect != null) {
+                                               d.height = rect.height;
+                                               d.width = rect.width;
+                                       }
+                               }
                        }
                        return d;
                },d);
diff --git a/wrapper/org/GNOME/Accessibility/AtkText.java b/wrapper/org/GNOME/Accessibility/AtkText.java
index 6638057..54c8612 100644
--- a/wrapper/org/GNOME/Accessibility/AtkText.java
+++ b/wrapper/org/GNOME/Accessibility/AtkText.java
@@ -145,14 +145,12 @@ public class AtkText {
                        Rectangle rect = acc_text.getCharacterBounds(offset);
                        if (rect == null)
                                return null;
-                       if (coord_type == AtkCoordType.SCREEN) {
-                               AccessibleComponent component = ac.getAccessibleComponent();
-                               if (component == null)
-                                       return null;
-                               Point p = component.getLocationOnScreen();
-                               rect.x += p.x;
-                               rect.y += p.y;
-                       }
+                       AccessibleComponent component = ac.getAccessibleComponent();
+                       if (component == null)
+                               return null;
+                       Point p = AtkComponent.getComponentOrigin(ac, component, coord_type);
+                       rect.x += p.x;
+                       rect.y += p.y;
                        return rect;
                }, null);
        }
@@ -174,14 +172,11 @@ public class AtkText {
                        return -1;
 
                return AtkUtil.invokeInSwing ( () -> {
-                       if (coord_type == AtkCoordType.SCREEN) {
-                               AccessibleComponent component = ac.getAccessibleComponent();
-                               if (component == null)
-                                       return -1;
-                               Point p = component.getLocationOnScreen();
-                               return acc_text.getIndexAtPoint(new Point(x-p.x, y-p.y));
-                       }
-                       return acc_text.getIndexAtPoint(new Point(x, y));
+                       AccessibleComponent component = ac.getAccessibleComponent();
+                       if (component == null)
+                               return -1;
+                       Point p = AtkComponent.getComponentOrigin(ac, component, coord_type);
+                       return acc_text.getIndexAtPoint(new Point(x-p.x, y-p.y));
                }, -1);
        }
 
@@ -199,17 +194,15 @@ public class AtkText {
                                final int rightEnd = getRightEnd(start, end, acc_text.getCharCount());
 
                                AccessibleExtendedText acc_ext_text = (AccessibleExtendedText)acc_text;
-                               Rectangle rect = acc_ext_text.getTextBounds(rightStart, rightEnd-1);
+                               Rectangle rect = acc_ext_text.getTextBounds(rightStart, rightEnd);
                                if (rect == null)
                                        return null;
-                               if (coord_type == AtkCoordType.SCREEN) {
-                                       AccessibleComponent component = ac.getAccessibleComponent();
-                                       if (component == null)
-                                               return null;
-                                       Point p = component.getLocationOnScreen();
-                                       rect.x += p.x;
-                                       rect.y += p.y;
-                               }
+                               AccessibleComponent component = ac.getAccessibleComponent();
+                               if (component == null)
+                                       return null;
+                               Point p = AtkComponent.getComponentOrigin(ac, component, coord_type);
+                               rect.x += p.x;
+                               rect.y += p.y;
                                return rect;
                        }
                        return null;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]