fixed bug 510
[org.ibex.core.git] / src / org / ibex / Box.java
index 3892ce9..28a3229 100644 (file)
@@ -423,13 +423,28 @@ public final class Box extends JSScope implements Scheduler.Task {
     public int localToGlobalY(int y) { return parent == null ? y : parent.globalToLocalY(y + this.y); }
     
     public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-        if (nargs != 1 || !"indexof".equals(method)) return super.callMethod(method, a0, a1, a2, rest, nargs);
-        Box b = (Box)a0;
-        if (b.parent != this)
-            return (redirect == null || redirect == this) ?
-                N(-1) :
-                redirect.callMethod(method, a0, a1, a2, rest, nargs);
-        return N(b.getIndexInParent());
+        switch (nargs) {
+            case 1: {
+                //#switch(method)
+                case "indexof":
+                    Box b = (Box)a0;
+                    if (b.parent != this)
+                        return (redirect == null || redirect == this) ?
+                            N(-1) :
+                            redirect.callMethod(method, a0, a1, a2, rest, nargs);
+                    return N(b.getIndexInParent());
+
+                case "distanceto":
+                    Box b = (Box)a0;
+                    JS ret = new JS();
+                    ret.put("x", N(b.localToGlobalX(0) - localToGlobalX(0)));
+                    ret.put("y", N(b.localToGlobalY(0) - localToGlobalY(0)));
+                    return ret;
+
+                //#end
+            }
+        }
+        return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
 
     public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); }
@@ -453,6 +468,7 @@ public final class Box extends JSScope implements Scheduler.Task {
         //#switch(name)
         case "surface": return parent == null ? null : parent.getAndTriggerTraps("surface");
         case "indexof": return METHOD;
+        case "distanceto": return METHOD;
         case "text": return text;
         case "path": throw new JSExn("cannot read from the path property");
         case "fill": return colorToString(fillcolor);
@@ -563,6 +579,7 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "Maximized": if (parent == null && getSurface() != null) getSurface().maximized = toBoolean(value);  // FEATURE
         case "Close": if (parent == null && getSurface() != null) getSurface().dispose(true);
         case "redirect":
+            if (value == null) { redirect = null; return; }
             for(Box cur = (Box)value; cur != null; cur = cur.parent)
                 if (cur == redirect) {
                     redirect = (Box)value;