2003/12/21 08:50:38
[org.ibex.core.git] / src / org / xwt / Box.java
index 6f96cc2..25dc935 100644 (file)
@@ -72,6 +72,7 @@ public final class Box extends JSScope implements Scheduler.Task {
         DEFAULT_FONT = f;
     }
 
+    // FIXME update these
     // box properties can not be trapped
     static final String[] props = new String[] {
         "fill", "stroke", "image", "tile", "fixedaspect", "text", "path", "font",
@@ -82,6 +83,7 @@ public final class Box extends JSScope implements Scheduler.Task {
         "mouseinside", "root", "thisbox", "indexof"
     };
 
+    // FIXME update these
     // events can have write traps, but not read traps
     static final String[] events = new String[] {
         "Press1", "Press2", "Press3",
@@ -90,9 +92,7 @@ public final class Box extends JSScope implements Scheduler.Task {
         "DoubleClick1", "DoubleClick2", "DoubleClick3",
         "Enter", "Leave", "Move", 
         "KeyPressed", "KeyReleased", "PosChange", "SizeChange",
-
         "childadded", "childremoved",
-
         "Focused", "Maximized", "Minimized", "Close",
         "icon", "titlebar", "toback", "tofront"
     };
@@ -122,13 +122,14 @@ public final class Box extends JSScope implements Scheduler.Task {
     static final int ALIGNS       = 0x0000f000;
     static final int CURSOR       = 0x00010000;  // if true, this box has a cursor in the cursor hash; FEATURE: GC issues?
     static final int NOCLIP       = 0x00020000;
+    static final int STOP_UPWARD_PROPAGATION    = 0x00040000;
 
 
     // Instance Data //////////////////////////////////////////////////////////////////////
 
     Box parent = null;
     Box redirect = this;
-    int flags = VISIBLE | PACKED | REPACK | REFLOW | RESIZE | FIXED /* ROWS */;
+    int flags = VISIBLE | PACKED | REPACK | REFLOW | RESIZE | FIXED /* ROWS */ | STOP_UPWARD_PROPAGATION;
 
     private String text = null;
     private Font font = DEFAULT_FONT; 
@@ -202,25 +203,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         }
     }
 
-    /** update MOUSEINSIDE, check for Enter/Leave/Move */
-    void Move(int oldmousex, int oldmousey, int mousex, int mousey) { Move(oldmousex, oldmousey, mousex, mousey, false); }
-    void Move(int oldmousex, int oldmousey, int mousex, int mousey, boolean forceleave) {
-        boolean wasinside = test(MOUSEINSIDE);
-        boolean isinside = test(VISIBLE) && inside(mousex, mousey) && !forceleave;
-        if (isinside) set(MOUSEINSIDE); else clear(MOUSEINSIDE);
-        if (!wasinside && !isinside) return;
-        
-        if (isinside && test(CURSOR)) Surface.fromBox(getRoot()).cursor = (String)boxToCursor.get(this);
-        if (!wasinside && isinside && getTrap("Enter") != null) putAndTriggerTrapsAndCatchExceptions("Enter", T);
-        else if (wasinside && !isinside && getTrap("Leave") != null) putAndTriggerTrapsAndCatchExceptions("Leave", T);
-        else if (wasinside && isinside && (mousex != oldmousex || mousey != oldmousey) && getTrap("Move")!= null)
-            putAndTriggerTrapsAndCatchExceptions("Move", T);
-        for(Box b = getChild(treeSize() - 1); b != null; b = b.prevSibling()) {
-            b.Move(oldmousex - b.x, oldmousey - b.y, mousex - b.x, mousey - b.y, forceleave);
-            if (b.inside(mousex - b.x, mousey - b.y)) forceleave = true;
-        }
-    }
-
 
     // Reflow ////////////////////////////////////////////////////////////////////////////////////////
 
@@ -492,18 +474,6 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "align": clear(ALIGNS); setAlign(value == null ? "center" : value); MARK_RESIZE;
         case "cursor": setCursor(value);
         case "fill": setFill(value);
-        case "Press1": mouseEvent("Press1", value);
-        case "Press2": mouseEvent("Press2", value);
-        case "Press3": mouseEvent("Press3", value);
-        case "Release1": mouseEvent("Release1", value);
-        case "Release2": mouseEvent("Release2", value);
-        case "Release3": mouseEvent("Release3", value);
-        case "Click1": mouseEvent("Click1", value);
-        case "Click2": mouseEvent("Click2", value);
-        case "Click3": mouseEvent("Click3", value);
-        case "DoubleClick1": mouseEvent("DoubleClick1", value);
-        case "DoubleClick2": mouseEvent("DoubleClick2", value);
-        case "DoubleClick3": mouseEvent("DoubleClick3", value);
         case "Minimized": if (parent == null && getSurface() != null) getSurface().minimized = toBoolean(value);  // FEATURE
         case "Maximized": if (parent == null && getSurface() != null) getSurface().maximized = toBoolean(value);  // FEATURE
         case "Close": if (parent == null && getSurface() != null) getSurface().dispose(true);
@@ -514,14 +484,49 @@ public final class Box extends JSScope implements Scheduler.Task {
         case "fontsize": font = Font.getFont(font == null ? null : font.res, toInt(value)); MARK_RESIZE; dirty();
         case "x": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(x); } else { if (test(PACKED) && parent != null) return; CHECKSET_INT(x); dirty(); MARK_RESIZE; dirty(); }
         case "y": if (parent==null && Surface.fromBox(this)!=null) { CHECKSET_INT(y); } else { if (test(PACKED) && parent != null) return; CHECKSET_INT(y); dirty(); MARK_RESIZE; dirty(); }
-        case "KeyPressed":   return;  // prevent stuff from hitting the Hash
-        case "KeyReleased":   return; // prevent stuff from hitting the Hash
-        case "PosChange":   return;   // prevent stuff from hitting the Hash
-        case "SizeChange":   return;  // prevent stuff from hitting the Hash
-        case "childadded":   return;  // prevent stuff from hitting the Hash
-        case "childremoved": return;  // prevent stuff from hitting the Hash
-        case "thisbox": if (value == null) removeSelf();
-        default: super.put(name, value);
+
+        case "Press1":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Press2":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Press3":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Release1":      if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Release2":      if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Release3":      if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Click1":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Click2":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Click3":        if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "DoubleClick1":  if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "DoubleClick2":  if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "DoubleClick3":  if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "KeyPressed":    if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "KeyReleased":   if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Move":          if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Enter":         if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+        case "Leave":         if (!test(STOP_UPWARD_PROPAGATION) && parent != null) parent.put(name, value);
+
+        case "_Move":         propagateDownward(name, value, false);
+        case "_Press1":       propagateDownward(name, value, false);
+        case "_Press2":       propagateDownward(name, value, false);
+        case "_Press3":       propagateDownward(name, value, false);
+        case "_Release1":     propagateDownward(name, value, false);
+        case "_Release2":     propagateDownward(name, value, false);
+        case "_Release3":     propagateDownward(name, value, false);
+        case "_Click1":       propagateDownward(name, value, false);
+        case "_Click2":       propagateDownward(name, value, false);
+        case "_Click3":       propagateDownward(name, value, false);
+        case "_DoubleClick1": propagateDownward(name, value, false);
+        case "_DoubleClick2": propagateDownward(name, value, false);
+        case "_DoubleClick3": propagateDownward(name, value, false);
+        case "_KeyPressed":   propagateDownward(name, value, false);
+        case "_KeyReleased":  propagateDownward(name, value, false);
+
+        case "PosChange":     return;
+        case "SizeChange":    return;
+        case "childadded":    return;
+        case "childremoved":  return;
+
+        case "thisbox":       if (value == null) removeSelf();
+
+        default:              super.put(name, value);
         //#end
     }
 
@@ -562,7 +567,8 @@ public final class Box extends JSScope implements Scheduler.Task {
         boxToCursor.put(this, value);
         Surface surface = getSurface();
         String tempcursor = surface.cursor;
-        Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
+        // FIXME
+        //Move(surface.mousex, surface.mousey, surface.mousex, surface.mousey);
         if (surface.cursor != tempcursor) surface.syncCursor();
     }
 
@@ -580,15 +586,42 @@ public final class Box extends JSScope implements Scheduler.Task {
 
         texture = Picture.load((Res)value, this);
     }
-        
-    private void mouseEvent(String name, Object value) {
-        Surface surface = getSurface();
-        if (surface == null) return;
-        int mousex = globalToLocalX(surface.mousex);
-        int mousey = globalToLocalY(surface.mousey);
-        for(Box c = prevSibling(); c != null; c = c.prevSibling())
-            if (c.inside(mousex - c.x, mousey - c.y)) { c.putAndTriggerTrapsAndCatchExceptions(name, value); return; }
-        if (parent != null) parent.putAndTriggerTrapsAndCatchExceptions(name, value);
+
+    /**
+     *  Handles events which propagate down the box tree.  If obscured
+     *  is set, then we merely check for Enter/Leave.
+     */
+    private void propagateDownward(Object name_, Object value, boolean obscured) {
+
+        String name = (String)name_;
+        if (getSurface() == null) return;
+        int x = globalToLocalX(getSurface().mousex);
+        int y = globalToLocalY(getSurface().mousey);
+        boolean wasinside = test(MOUSEINSIDE);
+        boolean isinside = test(VISIBLE) && inside(x, y) && !obscured;
+        if (!wasinside && isinside) { set(MOUSEINSIDE);   putAndTriggerTrapsAndCatchExceptions("Enter", T); }
+        if (wasinside && !isinside) { clear(MOUSEINSIDE); putAndTriggerTrapsAndCatchExceptions("Leave", T); }
+
+        boolean found = false;
+        if (wasinside || isinside)
+            for(Box child = getChild(treeSize() - 1); child != null; child = child.prevSibling()) {
+                boolean save_stop = child.test(STOP_UPWARD_PROPAGATION);
+                if (obscured || !child.inside(x - child.x, y - child.y)) {
+                    child.propagateDownward(name, value, true);
+                } else try {
+                    found = true;
+                    child.clear(STOP_UPWARD_PROPAGATION);
+                    child.putAndTriggerTrapsAndCatchExceptions(name, value);
+                } finally {
+                    if (save_stop) child.set(STOP_UPWARD_PROPAGATION); else child.clear(STOP_UPWARD_PROPAGATION);
+                }
+                if (child.inside(x - child.x, y - child.y))
+                    if (name.equals("_Move")) obscured = true;
+                    else break;
+            }
+
+        if (!obscured && !found)
+            if (!name.equals("_Move") || wasinside) putAndTriggerTrapsAndCatchExceptions(name.substring(1), value);
     }
 
     private static int stringToColor(String s) {
@@ -755,7 +788,7 @@ public final class Box extends JSScope implements Scheduler.Task {
         }
     }
 
-    private void putAndTriggerTrapsAndCatchExceptions(Object name, Object val) {
+    void putAndTriggerTrapsAndCatchExceptions(Object name, Object val) {
         try {
             putAndTriggerTraps(name, val);
         } catch (Exception e) {