2003/12/22 04:50:08
[org.ibex.core.git] / src / org / xwt / Surface.java
index f3891ad..eca0dee 100644 (file)
@@ -42,17 +42,17 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
 
     // Instance Data ///////////////////////////////////////////////////////////////////////
 
-    Vec keywatchers = new Vec();
-
     public Box root;                                   ///< The Box at the root of this surface
     public String cursor = "default";                  ///< The active cursor to switch to when syncCursor() is called
     public int mousex;                                 ///< x position of the mouse, in Scheduler-time
     public int mousey;                                 ///< y position of the mouse, in Scheduler-time
+    public int _mousex;                                ///< x position of the mouse, in Scheduler-time
+    public int _mousey;                                ///< y position of the mouse, in Scheduler-time
     public int newmousex = -1;                         ///< x position of the mouse, in realtime
     public int newmousey = -1;                         ///< y position of the mouse, in realtime
     public boolean minimized = false;                  ///< True iff this surface is minimized, in real time
     public boolean maximized = false;                  ///< True iff this surface is maximized, in real time
-    private DirtyList dirtyRegions = new DirtyList();  ///< Dirty regions on the *screen*
+    DirtyList dirtyRegions = new DirtyList();  ///< Dirty regions on the *screen*
 
     // Used For Simulating Clicks and DoubleClicks /////////////////////////////////////////////////
 
@@ -88,14 +88,14 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         else if (button == 2) button2 = true;
         else if (button == 3) button3 = true;
 
-        if (button == 1) new SimpleMessage("Press1", T, Box.whoIs(root, mousex, mousey));
-        else if (button == 2) new SimpleMessage("Press2", T, Box.whoIs(root, mousex, mousey));
+        if (button == 1) new SimpleMessage("_Press1", T, root);
+        else if (button == 2) new SimpleMessage("_Press2", T, root);
         else if (button == 3) {
-            final Box who = Box.whoIs(root, mousex, mousey);
+            final Box who = root;
             Scheduler.add(new Scheduler.Task() { public void perform() throws JSExn {
                 Platform.clipboardReadEnabled = true;
                 try {
-                    root.putAndTriggerTraps("Press3", T);
+                    root.putAndTriggerTraps("_Press3", T);
                 } finally {
                     Platform.clipboardReadEnabled = false;
                 }
@@ -108,9 +108,9 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         else if (button == 2) button2 = false;
         else if (button == 3) button3 = false;
 
-        if (button == 1) new SimpleMessage("Release1", T, Box.whoIs(root, mousex, mousey));
-        else if (button == 2) new SimpleMessage("Release2", T, Box.whoIs(root, mousex, mousey));
-        else if (button == 3) new SimpleMessage("Release3", T, Box.whoIs(root, mousex, mousey));
+        if (button == 1) new SimpleMessage("_Release1", T, root);
+        else if (button == 2) new SimpleMessage("_Release2", T, root);
+        else if (button == 3) new SimpleMessage("_Release3", T, root);
 
         if (Platform.needsAutoClick() && Math.abs(last_press_x - mousex) < 5 && Math.abs(last_press_y - mousey) < 5) Click(button);
         last_press_x = Integer.MAX_VALUE;
@@ -118,9 +118,9 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
     }
 
     protected final void Click(int button) {
-        if (button == 1) new SimpleMessage("Click1", T, Box.whoIs(root, mousex, mousey));
-        else if (button == 2) new SimpleMessage("Click2", T, Box.whoIs(root, mousex, mousey));
-        else if (button == 3) new SimpleMessage("Click3", T, Box.whoIs(root, mousex, mousey));
+        if (button == 1) new SimpleMessage("_Click1", T, root);
+        else if (button == 2) new SimpleMessage("_Click2", T, root);
+        else if (button == 3) new SimpleMessage("_Click3", T, root);
         if (Platform.needsAutoDoubleClick()) {
             long now = System.currentTimeMillis();
             if (lastClickButton == button && now - lastClickTime < 350) DoubleClick(button);
@@ -129,63 +129,6 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         }
     }
 
-    protected final void DoubleClick(int button) {
-        if (button == 1) new SimpleMessage("DoubleClick1", T, Box.whoIs(root, mousex, mousey));
-        else if (button == 2) new SimpleMessage("DoubleClick2", T, Box.whoIs(root, mousex, mousey));
-        else if (button == 3) new SimpleMessage("DoubleClick3", T, Box.whoIs(root, mousex, mousey));
-    }
-
-    /** Send a KeyPressed message; subclasses should not add the C- or A- prefixes or should they capitalize alphabet chars */
-    protected final void KeyPressed(String key) {
-        if (key == null) return;
-        if (key.toLowerCase().endsWith("shift")) shift = true;     else if (shift) key = key.toUpperCase();
-        if (key.toLowerCase().equals("alt")) alt = true;           else if (alt) key = "A-" + key;
-        if (key.toLowerCase().endsWith("control")) control = true; else if (control) key = "C-" + key;
-        Scheduler.add(new KMessage(key));
-    }
-
-    // This is implemented as a private static class instead of an anonymous class to work around a GCJ bug
-    private class KMessage implements Scheduler.Task {
-        String key = null;
-        public KMessage(String k) { key = k; }
-        public void perform() {
-            if (key.equals("C-v") || key.equals("A-v")) Platform.clipboardReadEnabled = true;
-            outer: for(int i=0; i<keywatchers.size(); i++) {
-                Box b = (Box)keywatchers.elementAt(i);
-                for(Box cur = b; cur != null; cur = cur.parent)
-                    if (!cur.test(cur.VISIBLE)) continue outer;
-                try {
-                    b.putAndTriggerTraps("KeyPressed", key);
-                } catch (JSExn e) {
-                    Log.log(Surface.class, "Exception thrown from KeyPressed handler");
-                    Log.logJS(e);
-                }
-            }
-            Platform.clipboardReadEnabled = false;
-        }
-    }
-
-    /** sends a KeyReleased message; subclasses should not add the C- or A- prefixes or capitalize alpha chars */
-    protected final void KeyReleased(final String key) {
-        if (key == null) return;
-        if (key.toLowerCase().equals("alt")) alt = false;
-        else if (key.toLowerCase().equals("control")) control = false;
-        else if (key.toLowerCase().equals("shift")) shift = false;
-        Scheduler.add(new Scheduler.Task() { public void perform() {
-            outer: for(int i=0; i<keywatchers.size(); i++) {
-                Box b = (Box)keywatchers.elementAt(i);
-                for(Box cur = b; cur != null; cur = cur.parent)
-                    if (!cur.test(cur.VISIBLE)) continue outer;
-                try {
-                    b.putAndTriggerTraps("KeyReleased", key);
-                } catch (JSExn e) {
-                    Log.log(Surface.class, "Exception thrown from KeyReleased handler");
-                    Log.logJS(e);
-                }
-            }
-        }});
-    }
-
     /** we enqueue ourselves in the Scheduler when we have a Move message to deal with */
     public void perform() {
         if (mousex == newmousex && mousey == newmousey) return;
@@ -195,12 +138,11 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         // Root gets motion events outside itself (if trapped)
         if (!root.inside(oldmousex, oldmousey) && !root.inside(mousex, mousey) && (button1 || button2 || button3))
             try {
-                root.putAndTriggerTraps("Move", T);
+                root.putAndTriggerTraps("_Move", T);
             } catch (JSExn e) {
                 Log.log(Surface.class, "Exception thrown from Move message handler");
                 Log.logJS(e);
             }
-        root.Move(oldmousex, oldmousey, mousex, mousey);
         if (!cursor.equals(oldcursor)) syncCursor();
     }
 
@@ -216,15 +158,21 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         Scheduler.add(this);
     }
 
+    private Scheduler.Task nextSizeChange = null;
     // FEATURE: can we avoid creating objects here?
     /** subclasses should invoke this method when the user resizes the window */
     protected final void SizeChange(final int width, final int height) {
-        Scheduler.addAtFront(new Scheduler.Task() { public void perform() {
+        if (root.maxwidth == width && root.maxheight == height) return;
+        nextSizeChange = new Scheduler.Task() { public void perform() {
+            if (nextSizeChange != this) return;
             // dirty the place where the scar used to be
             root.dirty(0, root.maxheight - Main.scarImage.height, Main.scarImage.width, Main.scarImage.height);
             root.setMaxWidth(JS.N(width));
             root.setMaxHeight(JS.N(height));
-        }});
+            nextSizeChange = null;
+            Refresh();
+        }};
+        Scheduler.addAtFront(nextSizeChange);
         abort = true;
     }
 
@@ -237,16 +185,27 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         }});
     }
 
+    private final String[] doubleClick = new String[] { null, "_DoubleClick1", "_DoubleClick2", "_DoubleClick3" };
+    protected final void DoubleClick(int button) { new SimpleMessage(doubleClick[button], T, root); }
+    protected final void KeyPressed(String key) { new SimpleMessage("_KeyPressed", key, root); }
+    protected final void KeyReleased(String key) { new SimpleMessage("_KeyReleased", key, root); }
     protected final void Close() { new SimpleMessage("Close", T, root); }
     protected final void Minimized(boolean b) { minimized = b; new SimpleMessage("Minimized", b ? T : F, root); }
     protected final void Maximized(boolean b) { maximized = b; new SimpleMessage("Maximized", b ? T : F, root); }
     protected final void Focused(boolean b) { new SimpleMessage("Focused", b ? T : F, root); }
-    public static void Refresh() { Scheduler.add(renderAll); }
-
-    public static final Scheduler.Task renderAll = new Scheduler.Task() { public void perform() {
-        for(int i=0; i<allSurfaces.size(); i++)
-            ((Surface)allSurfaces.elementAt(i)).render();
-    } };
+    public static void Refresh() { needRender = true; Scheduler.add(new Scheduler.Task() { public void perform() { } }); }
+
+    public static boolean needRender = false;
+    public static final Scheduler.Task renderAll = new Scheduler.Task() {
+            public void perform() {
+                for(int i=0; i<allSurfaces.size(); i++) {
+                    Surface s = ((Surface)allSurfaces.elementAt(i));
+                    do {
+                        s.render(); 
+                    } while(s.abort);
+                }
+            }
+        };
 
     public final void setMaximized(boolean b) { if (b != maximized) _setMaximized(maximized = b); }
     public final void setMinimized(boolean b) { if (b != minimized) _setMinimized(minimized = b); }
@@ -292,7 +251,6 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
 
     /** runs the prerender() and render() pipelines in the root Box to regenerate the backbuffer, then blits it to the screen */
     public synchronized void render() {
-
         // dirty the place where the scar used to be in case the root window size was programmatically changed
         if (root.maxwidth != root.width || root.maxheight != root.height)
             root.dirty(0, root.height - Main.scarImage.height, Main.scarImage.width, Main.scarImage.height);
@@ -306,7 +264,7 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
             _setSize(root.width, root.height);
             String oldcursor = cursor;
             cursor = "default";
-            root.Move(mousex, mousey, mousex, mousey);
+            root.putAndTriggerTrapsAndCatchExceptions("_Move", JS.T);
             if (!cursor.equals(oldcursor)) syncCursor();
         } while(abort);
 
@@ -333,8 +291,6 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
                     if (dirt[j] != null)
                         dirtyRegions.dirty(dirt[j][0], dirt[j][1], dirt[j][2], dirt[j][3]);
 
-                // tail-recurse
-                render();
                 return;
             }
         }
@@ -354,7 +310,28 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
             Scheduler.add(this);
         }
         
-        public void perform() throws JSExn { boxContainingMouse.putAndTriggerTraps(name, value); }
+        public void perform() {
+            if (name.equals("_KeyPressed")) {
+                String value = (String)this.value;
+                if (value.toLowerCase().endsWith("shift")) shift = true;     else if (shift) value = value.toUpperCase();
+                if (value.toLowerCase().equals("alt")) alt = true;           else if (alt) value = "A-" + value;
+                if (value.toLowerCase().endsWith("control")) control = true; else if (control) value = "C-" + value;
+                if (value.equals("C-v") || value.equals("A-v")) Platform.clipboardReadEnabled = true;
+            } else if (name.equals("_KeyReleased")) {
+                String value = (String)this.value;
+                if (value.toLowerCase().equals("alt")) alt = false;
+                else if (value.toLowerCase().equals("control")) control = false;
+                else if (value.toLowerCase().equals("shift")) shift = false;
+            }
+            try {
+                boxContainingMouse.putAndTriggerTraps(name, value);
+            } catch (JSExn e) {
+                Log.log(Surface.class, "Exception thrown from "+name+" handler");
+                Log.logJS(e);
+            } finally {
+                Platform.clipboardReadEnabled = false;
+            }
+        }
         public String toString() { return "SimpleMessage [name=" + name + ", value=" + value + "]"; }
 
     }
@@ -383,12 +360,10 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
             backbuffer.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
         }
 
+        public void render_() { render(); }
         public void render() {
             super.render();
-            render_();
-        }
-
-        public void render_() {
+            if (abort) return;
             int[][] dirt = screenDirtyRegions.flush();
             for(int i = 0; dirt != null && i < dirt.length; i++) {
                 if (dirt[i] == null) continue;
@@ -401,6 +376,7 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
                 if (x+w > root.width) w = root.width - x;
                 if (y+h > root.height) h = root.height - y;
                 if (w <= 0 || h <= 0) continue;
+                if (abort) return;
                 blit(backbuffer, x, y, x, y, w + x, h + y);
             }
         }
@@ -411,6 +387,11 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
             Refresh();
         }
 
+        public void dirty(int x, int y, int w, int h) {
+            screenDirtyRegions.dirty(x, y, w, h);
+            super.dirty(x, y, w, h);
+        }
+
         /** copies a region from the doublebuffer to this surface */
         public abstract void blit(PixelBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2);