ugly hacks to accomodate Apple's buggy AWT implementation
[org.ibex.core.git] / src / org / ibex / graphics / Surface.java
index 7e23169..ebf4670 100644 (file)
@@ -1,8 +1,13 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.ibex;
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the GNU General Public License version 2 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.ibex.graphics;
 
 import org.ibex.js.*;
 import org.ibex.util.*;
+import org.ibex.plat.*;
+import org.ibex.core.*;
 
 /** 
  *  A Surface, as described in the Ibex Reference.
@@ -10,23 +15,23 @@ import org.ibex.util.*;
  *  Platform subclasses should include an inner class subclass of
  *  Surface to return from the Platform._createSurface() method
  */
-public abstract class Surface extends PixelBuffer implements Scheduler.Task {
+public abstract class Surface implements Callable {
 
     // Static Data ////////////////////////////////////////////////////////////////////////////////
 
-    private static Boolean T = Boolean.TRUE;
-    private static Boolean F = Boolean.FALSE;
+    private static final JS T = JSU.T;
+    private static final JS F = JSU.F;
 
-    /** all instances of Surface which need to be refreshed by the Scheduler */
+    /** all instances of Surface which need to be refreshed by the Platform.Scheduler */
     public static Vec allSurfaces = new Vec();
     
     /** When set to true, render() should abort as soon as possible and restart the rendering process */
-    volatile boolean abort = false;
+    public volatile boolean abort = false;
 
     // these three variables are used to ensure that user resizes trump programmatic resizes
-    volatile boolean syncRootBoxToSurface = false;
-    volatile int pendingWidth = 0;
-    volatile int pendingHeight = 0;
+    public volatile boolean syncRootBoxToSurface = false;
+    public volatile int pendingWidth = 0;
+    public volatile int pendingHeight = 0;
 
     public static boolean alt = false;          ///< true iff the alt button is pressed down
     public static boolean control = false;      ///< true iff the control button is pressed down
@@ -48,6 +53,7 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
     public int newmousey = -1;                         ///< y position of the mouse, in real time; this lets us collapse Move's
     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
+    public boolean unrendered = true;                  ///< True iff this surface has not yet been rendered
     DirtyList dirtyRegions = new DirtyList();          ///< Dirty regions on the surface
 
     // Used For Simulating Clicks and DoubleClicks /////////////////////////////////////////////////
@@ -60,6 +66,7 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
     
     // Methods to be overridden by subclasses ///////////////////////////////////////////////////////
 
+    public abstract PixelBuffer getPixelBuffer();      ///< returns a PixelBuffer representing this Surface
     public abstract void toBack();                     ///< should push surface to the back of the stacking order
     public abstract void toFront();                    ///< should pull surface to the front of the stacking order
     public abstract void syncCursor();                 ///< set the actual cursor to this.cursor if they do not match
@@ -74,6 +81,7 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
     public void setMinimumSize(int minx, int miny, boolean resizable) { }
     protected void setSize(int w, int h) { _setSize(w, h); }
 
+    public static Picture scarImage = null;
 
     // Helper methods for subclasses ////////////////////////////////////////////////////////////
 
@@ -88,13 +96,14 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         if (button == 1) new Message("_Press1", T, root);
         else if (button == 2) new Message("_Press2", T, root);
         else if (button == 3) {
-            Scheduler.add(new Scheduler.Task() { public void perform() throws JSExn {
+            Platform.Scheduler.add(new Callable() { public Object run(Object o) throws JSExn {
                 Platform.clipboardReadEnabled = true;
                 try {
-                    root.putAndTriggerTraps("_Press3", T);
+                    root.putAndTriggerTraps(JSU.S("_Press3"), T);
                 } finally {
                     Platform.clipboardReadEnabled = false;
                 }
+                return null;
             }});
         }
     }
@@ -125,17 +134,19 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         }
     }
 
-    /** we enqueue ourselves in the Scheduler when we have a Move message to deal with */
-    private Scheduler.Task mover = new Scheduler.Task() {
-            public void perform() {
-                if (mousex == newmousex && mousey == newmousey) return;
+    private final static JS MOVE = JSU.S("_Move");
+    /** we enqueue ourselves in the Platform.Scheduler when we have a Move message to deal with */
+    private Callable mover = new Callable() {
+        public Object run(Object o) {
+                if (mousex == newmousex && mousey == newmousey) return null;
                 int oldmousex = mousex;     mousex = newmousex;
                 int oldmousey = mousey;     mousey = newmousey;
                 String oldcursor = cursor;  cursor = "default";
                 // FIXME: Root (ONLY) gets motion events outside itself (if trapped)
                 if (oldmousex != mousex || oldmousey != mousey)
-                    root.putAndTriggerTrapsAndCatchExceptions("_Move", T);
+                    root.putAndTriggerTrapsAndCatchExceptions(MOVE, T);
                 if (!cursor.equals(oldcursor)) syncCursor();
+                return null;
             } };
 
     /**
@@ -147,45 +158,46 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
     protected final void Move(final int newmousex, final int newmousey) {
         this.newmousex = newmousex;
         this.newmousey = newmousey;
-        Scheduler.add(mover);
+        Platform.Scheduler.add(mover);
     }
 
-    protected final void HScroll(int pixels) { new Message("_HScroll", new Integer(pixels), root); }
-    protected final void VScroll(int pixels) { new Message("_VScroll", new Integer(pixels), root); }
-    protected final void HScroll(float lines) { new Message("_HScroll", new Float(lines), root); }
-    protected final void VScroll(float lines) { new Message("_VScroll", new Float(lines), root); }
+    protected final void HScroll(int pixels) { new Message("_HScroll", JSU.N(pixels), root); }
+    protected final void VScroll(int pixels) { new Message("_VScroll", JSU.N(pixels), root); }
+    protected final void HScroll(float lines) { new Message("_HScroll", JSU.N(lines), root); }
+    protected final void VScroll(float lines) { new Message("_VScroll", JSU.N(lines), root); }
 
     /** subclasses should invoke this method when the user resizes the window */
     protected final void SizeChange(final int width, final int height) {
-        if (pendingWidth == width && pendingHeight == height) return;
+        if (unrendered || (pendingWidth == width && pendingHeight == height)) return;
         pendingWidth = width;
         pendingHeight = height;
         syncRootBoxToSurface = true;
         abort = true;
-        Scheduler.renderAll();
+        Refresh();
     }
 
     // FEATURE: can we avoid creating objects here?
     protected final void PosChange(final int x, final int y) {
-        Scheduler.add(new Scheduler.Task() { public void perform() throws JSExn {
+        Platform.Scheduler.add(new Callable() { public Object run(Object o) throws JSExn {
             root.x = x;
             root.y = y;
-            root.putAndTriggerTrapsAndCatchExceptions("PosChange", T);
+            root.putAndTriggerTrapsAndCatchExceptions(JSU.S("PosChange"), T);
+            return null;
         }});
     }
 
     private final String[] doubleClick = new String[] { null, "_DoubleClick1", "_DoubleClick2", "_DoubleClick3" };
     protected final void DoubleClick(int button) { new Message(doubleClick[button], T, root); }
-    protected final void KeyPressed(String key) { new Message("_KeyPressed", key, root); }
-    protected final void KeyReleased(String key) { new Message("_KeyReleased", key, root); }
+    protected final void KeyPressed(String key) { new Message("_KeyPressed", JSU.S(key), root); }
+    protected final void KeyReleased(String key) { new Message("_KeyReleased", JSU.S(key), root); }
     protected final void Close() { new Message("Close", T, root); }
     protected final void Minimized(boolean b) { minimized = b; new Message("Minimized", b ? T : F, root); }
     protected final void Maximized(boolean b) { maximized = b; new Message("Maximized", b ? T : F, root); }
     protected final void Focused(boolean b) { new Message("Focused", b ? T : F, root); }
 
     private boolean scheduled = false;
-    public void Refresh() { if (!scheduled) Scheduler.add(this); scheduled = true; }
-    public void perform() { scheduled = false; Scheduler.renderAll(); }
+    public void Refresh() { if (!scheduled) Platform.Scheduler.add(this); scheduled = true; }
+    public Object run(Object o) { scheduled = false; Platform.Scheduler.renderAll(); return null; }
 
     public final void setMaximized(boolean b) { if (b != maximized) _setMaximized(maximized = b); }
     public final void setMinimized(boolean b) { if (b != minimized) _setMinimized(minimized = b); }
@@ -204,10 +216,9 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
         }
     }
 
-    public void dirty(int x, int y, int w, int h) {
-        dirtyRegions.dirty(x, y, w, h);
-        Refresh();
-    }
+    // This is how subclasses signal a 'shallow dirty', indicating that although the backbuffer is valid, the screen is not
+    public void Dirty(int x, int y, int w, int h) { dirty(x,y,w,h); }
+    public void dirty(int x, int y, int w, int h) { dirtyRegions.dirty(x, y, w, h); Refresh(); }
 
     public static Surface fromBox(Box b) {
         // FIXME use a hash table here
@@ -220,15 +231,26 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
 
     public Surface(Box root) {
         this.root = root;
-        root.setMaxWidth(JS.N(Math.min(Platform.getScreenWidth(), root.maxwidth)));
-        root.setMaxHeight(JS.N(Math.min(Platform.getScreenHeight(), root.maxheight)));
+        // FIXME: document this in the reference
+        if (!root.test(root.HSHRINK) && root.maxwidth == Integer.MAX_VALUE)
+            root.maxwidth = Platform.getScreenWidth() / 2;
+        if (!root.test(root.VSHRINK) && root.maxheight == Integer.MAX_VALUE)
+            root.maxheight = Platform.getScreenHeight() / 2;
+        root.setWidth(root.minwidth,
+                      root.test(root.HSHRINK)
+                      ? Math.max(root.minwidth, root.contentwidth)
+                      : Math.min(Platform.getScreenWidth(), root.maxwidth));
+        root.setHeight(root.minheight,
+                      root.test(root.VSHRINK)
+                      ? Math.max(root.minheight, root.contentheight)
+                      : Math.min(Platform.getScreenHeight(), root.maxheight));
         Surface old = fromBox(root);
         if (old != null) old.dispose(false);
         else root.removeSelf();
         Refresh();
     }
 
-    private static VectorGraphics.Affine identity = VectorGraphics.Affine.identity();
+    private static Affine identity = Affine.identity();
 
     /** runs the prerender() and render() pipelines in the root Box to regenerate the backbuffer, then blits it to the screen */
     public synchronized void render() {
@@ -238,20 +260,22 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
             abort = false;
             root.pack();
             if (syncRootBoxToSurface) {
-                root.setMaxWidth(JS.N(pendingWidth));
-                root.setMaxHeight(JS.N(pendingHeight));
+                root.setWidth(root.minwidth, pendingWidth);
+                root.setHeight(root.minheight, pendingHeight);
                 syncRootBoxToSurface = false;
             }
-            if (root.maxwidth != root.width || root.maxheight != root.height) {
+            int rootwidth = root.test(root.HSHRINK) ? root.contentwidth : root.maxwidth;
+            int rootheight = root.test(root.VSHRINK) ? root.contentheight : root.maxheight;
+            if (rootwidth != root.width || rootheight != root.height) {
                 // dirty the place where the scar used to be and where it is now
-                dirty(0, root.height - Main.scarImage.height, Main.scarImage.width, Main.scarImage.height);
-                dirty(0, root.maxheight - Main.scarImage.height, Main.scarImage.width, Main.scarImage.height);
+                dirty(0, root.height - scarImage.height, scarImage.width, scarImage.height);
+                dirty(0, rootheight - scarImage.height, scarImage.width, scarImage.height);
             }
             root.reflow();
-            setSize(root.width, root.height);
+            setSize(rootwidth, rootheight);
             /*String oldcursor = cursor;
             cursor = "default";
-            root.putAndTriggerTrapsAndCatchExceptions("_Move", JS.T);
+            root.putAndTriggerTrapsAndCatchExceptions("_Move", JSU.T);
             if (!cursor.equals(oldcursor)) syncCursor();*/
         } while(abort);
 
@@ -265,8 +289,8 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
             if (y+h > root.height) h = root.height - y;
             if (w <= 0 || h <= 0) continue;
 
-            root.render(0, 0, x, y, x + w, y + h, this, identity);
-            drawPicture(Main.scarImage, 0, root.height - Main.scarImage.height, x, y, x+w, y+h);
+            root.render(0, 0, x, y, x + w, y + h, this.getPixelBuffer(), identity);
+            getPixelBuffer().drawPicture(scarImage, 0, root.height - scarImage.height, x, y, x+w, y+h);
             
             if (abort) {
                 // x,y,w,h is only partially reconstructed, so we must be careful not to re-blit it
@@ -278,72 +302,92 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
                 return;
             }
         }
+
+        unrendered = false;
     }
 
     // FEATURE: reinstate recycler
-    public class Message implements Scheduler.Task {
+    public class Message implements Callable {
         
         private Box boxContainingMouse;
-        private Object value;
+        private JS value;
         public String name;
         
-        Message(String name, Object value, Box boxContainingMouse) {
+        Message(String name, JS value, Box boxContainingMouse) {
             this.boxContainingMouse = boxContainingMouse;
             this.name = name;
             this.value = value;
-            Scheduler.add(this);
+            Platform.Scheduler.add(this);
         }
         
-        public void perform() {
+        public Object run(Object o) throws JSExn {
             if (name.equals("_KeyPressed")) {
-                String value = (String)this.value;
+                String value = JSU.toString(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;
-                this.value = value;
+                this.value = JSU.S(value);
             } else if (name.equals("_KeyReleased")) {
-                String value = (String)this.value;
+                String value = JSU.toString(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;
-                this.value = value;
+                this.value = JSU.S(value);
             } else if (name.equals("_HScroll") || name.equals("_VScroll")) {
                 // FIXME: technically points != pixels
-                if (value instanceof Integer)
-                    value = new Float(((Integer)value).intValue() * root.fontSize());
+                if (JSU.isInt(value))
+                    value = JSU.N(JSU.toInt(value) * root.fontSize());
             }
             try {
-                boxContainingMouse.putAndTriggerTrapsAndCatchExceptions(name, value);
+                boxContainingMouse.putAndTriggerTrapsAndCatchExceptions(JSU.S(name), value);
             } finally {
                 Platform.clipboardReadEnabled = false;
             }
+            return null;
         }
-        public String toString() { return "Message [name=" + name + ", value=" + value + "]"; }
+        public String toString() { return "Message [name=" + name + ", value=" + JSU.str(value) + "]"; }
     }
 
 
     // Default PixelBuffer implementation /////////////////////////////////////////////////////////
 
-    public static abstract class DoubleBufferedSurface extends Surface {
+    public static abstract class DoubleBufferedSurface extends Surface implements PixelBuffer {
 
         public DoubleBufferedSurface(Box root) { super(root); }
         PixelBuffer backbuffer = Platform.createPixelBuffer(Platform.getScreenWidth(), Platform.getScreenHeight(), this);
         DirtyList screenDirtyRegions = new DirtyList();
 
+        public PixelBuffer getPixelBuffer() { return this; }
         public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
             screenDirtyRegions.dirty(cx1, cy1, cx2 - cx1, cy2 - cy1);
             backbuffer.drawPicture(source, dx, dy, cx1, cy1, cx2, cy2);
         }
 
-        public void drawGlyph(Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int argb) {
+        public void drawGlyph(Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int argb, int bc) {
             screenDirtyRegions.dirty(cx1, cy1, cx2 - cx1, cy2 - cy1);
-            backbuffer.drawGlyph(source, dx, dy, cx1, cy1, cx2, cy2, argb);
+            backbuffer.drawGlyph(source, dx, dy, cx1, cy1, cx2, cy2, argb, bc);
+        }
+
+        public void stroke(Polygon p, int color) {
+            // FIXME
+        }
+        
+        public void fill(Polygon p, Paint paint) {
+            // FIXME
+        }
+
+        public void drawLine(int x1, int y1, int x2, int y2, int color) {
+            screenDirtyRegions.dirty(x1, y1, x2, y2);
+            backbuffer.drawLine(x1, y1, x2, y2, color);
         }
 
+        public abstract void _fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
         public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) {
+            // we don't dirty trapezoid-fills since it's faster to just do them directly than to copy from the backbuffer
             screenDirtyRegions.dirty(Math.min(x1, x3), y1, Math.max(x2, x4) - Math.min(x1, x3), y2 - y1);
             backbuffer.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
+            //_fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
         }
 
         public void render() {
@@ -366,10 +410,11 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
             }
         }
 
-        /** This is how subclasses signal a 'shallow dirty', indicating that although the backbuffer is valid, the screen is not */
+        // This is how subclasses signal a 'shallow dirty', indicating that although the backbuffer is valid, the screen is not
         public final void Dirty(int x, int y, int w, int h) {
-            screenDirtyRegions.dirty(x, y, w, h);
-            Scheduler.renderAll();
+            //screenDirtyRegions.dirty(x, y, w, h);
+            //Refresh();
+            blit(x,y,w,h);
         }
 
         public void dirty(int x, int y, int w, int h) {
@@ -377,9 +422,11 @@ public abstract class Surface extends PixelBuffer implements Scheduler.Task {
             super.dirty(x, y, w, h);
         }
 
-        /** copies a region from the doublebuffer to this surface */
+        // 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);
-
+        protected void blit(int x, int y, int w, int h) {
+            blit(backbuffer, x, y, x, y, w + x, h + y);
+        }
     }
 
 }