new PixelBuffer API (mainly tons of renames)
[org.ibex.core.git] / src / org / ibex / plat / AWT.java
index c7a1dce..d937588 100644 (file)
@@ -1,7 +1,9 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+// 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.plat;
 
-import org.ibex.*;
 import org.ibex.util.*;
 import java.io.*;
 import org.ibex.js.*;
@@ -9,6 +11,9 @@ import java.awt.*;
 import java.awt.datatransfer.*;
 import java.awt.image.*;
 import java.awt.event.*;
+import org.ibex.graphics.*;
+import org.ibex.core.*;
+import org.ibex.net.*;
 
 /** Platform subclass for all VM's providing AWT 1.1 functionality */
 public class AWT extends JVM {
@@ -113,9 +118,10 @@ public class AWT extends JVM {
 
     // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
 
-    protected org.ibex.Font.Glyph _createGlyph(org.ibex.Font f, char c) { return new AWTGlyph(f, c); }
-    protected static class AWTGlyph extends org.ibex.Font.Glyph {
+    protected org.ibex.graphics.Font.Glyph _createGlyph(org.ibex.graphics.Font f, char c) { return new AWTGlyph(f, c); }
+    protected static class AWTGlyph extends org.ibex.graphics.Font.Glyph {
         private Image i = null;
+        private Image i2 = null;
         private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
 
         // this doesn't work on Win32 because the JVM is broken
@@ -128,7 +134,7 @@ public class AWT extends JVM {
         };
         */
 
-        public AWTGlyph(org.ibex.Font f, char c) { super(f, c); }
+        public AWTGlyph(org.ibex.graphics.Font f, char c) { super(f, c); }
         Image getImage() {
             if (i == null && isLoaded) {
 
@@ -181,15 +187,17 @@ public class AWT extends JVM {
         }
     }
     
-    protected static class AWTPixelBuffer extends PixelBuffer {
+    protected static class AWTPixelBuffer implements PixelBuffer {
         
         protected Image i = null;
         protected Graphics g = null;
-        
+        protected AWTSurface surface = null;
+
         /** JDK1.1 platforms require that a component be associated with each off-screen buffer */
         static Component component = null;
 
-        protected AWTPixelBuffer() { }
+        public AWTPixelBuffer(Image i) { this.i = i; }
+        public AWTPixelBuffer(AWTSurface s) { this.surface = s; }
         public AWTPixelBuffer(int w, int h) {
             synchronized(AWTPixelBuffer.class) {
                 if (component == null) {
@@ -199,55 +207,69 @@ public class AWT extends JVM {
                 }
             }
             i = component.createImage(w, h);
-            g = i.getGraphics();
+        }
+        Graphics getGraphics() {
+            if (surface != null) return surface.getGraphics();
+            if (g != null) return g;
+            if (i != null) { g = i.getGraphics(); return g; }
+            return null;
         }
         
-        public int getHeight() { return i == null ? 0 : i.getHeight(null); }
-        public int getWidth() { return i == null ? 0 : i.getWidth(null); }
-
         public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
             ((AWTPicture)source).init();
+            Graphics g = getGraphics();
             g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
             g.drawImage(((AWTPicture)source).i, dx, dy, null);
-            g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
+            if (i != null) g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
         }
 
-        /** implemented with java.awt 1.1's setXORMode() */
-        public void drawGlyph(org.ibex.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
-
-            // XOR the target region
-            g.setXORMode(new Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
-            g.setColor(new Color(0x0, 0x0, 0x0));
-            g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1);
-
-            // blacken the area we want the glyph to cover
-            g.setPaintMode();
-            g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
-            g.drawImage(((AWTGlyph)source).getImage(), dx, dy, null);
-            g.setClip(0, 0, i.getWidth(null), i.getHeight(null));
+        public void drawLine(int x1, int y1, int x2, int y2, int rgb) {
+            Graphics g = getGraphics();
+            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
+            g.setColor(new java.awt.Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
+            g.drawLine(x1, y1, x2, y2);
+        }
 
-            // XOR back, turning black into the chosen rgb color
-            g.setXORMode(new Color((rgb & 0x00ff0000) >> 16, (rgb & 0x0000ff00) >> 8, rgb & 0x000000ff));
-            g.setColor(new Color(0x0, 0x0, 0x0));
-            g.fillRect(cx1, cy1, cx2 - cx1, cy2 - cy1);
+        public void stroke(org.ibex.graphics.Polygon p, int color) { p.stroke(this, color); }
+        public void fill(org.ibex.graphics.Polygon p, org.ibex.graphics.Paint paint) { p.fill(this, paint); }
 
-            // restore the graphics context
-            g.setPaintMode();
+        private static int[] xa = new int[4];
+        private static int[] ya = new int[4];
+        public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
+            Graphics g = getGraphics();
+            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); 
+            g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
+            xa[0]=x1;
+            xa[1]=x2;
+            xa[2]=x4;
+            xa[3]=x3;
+            ya[0]=y1;
+            ya[1]=y1;
+            ya[2]=y2;
+            ya[3]=y2;
+            g.fillPolygon(xa, ya, 4);
         }
 
-        // FIXME: try to use os acceleration
-        public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
-            g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
-            if (x1 == x3 && x2 == x4) {
-                g.fillRect(x1, y1, x4 - x1, y2 - y1);
-            } else for(int y=y1; y<y2; y++) {
-                int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
-                int _y1 = (int)Math.floor(y);
-                int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
-                int _y2 = (int)Math.floor(y) + 1;
-                if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
-                g.fillRect(_x1, _y1, _x2 - _x1, _y2 - _y1);
-            }
+        // this doens't seem to work on Windows
+        public void drawGlyph(org.ibex.graphics.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2,
+                              int rgb, int argb) {
+            Image i = ((AWTGlyph)source).getImage();
+            if (((AWTGlyph)source).i2 == null)
+                ((AWTGlyph)source).i2 = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB);
+            Image i2 = ((AWTGlyph)source).i2;
+            Graphics g2 = i2.getGraphics();
+            g2.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
+            g2.fillRect(0, 0, i2.getWidth(null), i2.getHeight(null));
+            g2.drawImage(i, 0, 0, null);
+            Graphics g = getGraphics();
+            g.setClip(cx1, cy1, cx2 - cx1, cy2 - cy1);
+            g.drawImage(i2, dx, dy, i2.getWidth(null), i2.getHeight(null), null);
+            g.setColor(new java.awt.Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
+            g.fillRect(cx1, cy1, dx - cx1,  cy2 - cy1);
+            g.fillRect(cx1, cy1, cx2 - cx1, dy - cy1);
+            g.fillRect(dx+i2.getWidth(null), cy1, cx2 - (dx+i2.getWidth(null)), cy2 - cy1);
+            g.fillRect(cx1, dy+i2.getHeight(null), cx2 - cx1, cy2 - (dy+i2.getHeight(null)));
+            g.setClip(0, 0, 1000, 1000);
         }
     }
     
@@ -255,32 +277,32 @@ public class AWT extends JVM {
     protected static class AWTSurface extends Surface.DoubleBufferedSurface
         implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
 
-        public void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
-            insets = (frame == null ? window : frame).getInsets();
-            window.getGraphics().drawImage(((AWTPixelBuffer)s).i,
-                                  dx + insets.left,
-                                  dy + insets.top,
-                                  dx2 + insets.left,
-                                  dy2 + insets.top,
-                                  sx, sy, sx + (dx2 - dx), sy + (dy2 - dy), null);
-        }
-        
-        /** if (component instanceof Frame) then frame == window else frame == null */
+        protected AWTPixelBuffer pb = null;
+        private Graphics g = null;
         Frame frame = null;
         Window window = null;
+
+        public void blit(PixelBuffer source, int sx, int sy, int dx, int dy, int dx2, int dy2) {
+            getGraphics().drawImage(((AWTPixelBuffer)source).i, sx, sy, sx+(dx2-dx), sy+(dy2-dy), dx, dy, dx2, dy2, null);
+        }
         
         /** our component's insets */
         protected Insets insets = new Insets(0, 0, 0, 0);
-        
-        /** some JDKs let us recycle a single Dimension object when calling getSize() */
-        Dimension singleSize = new Dimension();
-        
+        Graphics getGraphics() { if (g==null) g=window==null ? frame.getGraphics() : window.getGraphics(); return g; }
+        public void _fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color) {
+            if (pb ==null) pb = new AWTPixelBuffer(this);
+            pb.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
+        }
+        //public PixelBuffer getPixelBuffer() { return pb != null ? pb : (pb = new AWTPixelBuffer(this)); } 
+        public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); }
         public void toBack() { if (window != null) window.toBack(); }
         public void toFront() { if (window != null) window.toFront(); }
         public void setLocation() { window.setLocation(root.x, root.y); }
         public void setTitleBarText(String s) { if (frame != null) frame.setTitle(s); }
         public void setIcon(Picture i) { if (frame != null) frame.setIconImage(((AWTPicture)i).i); }
-        public void _setSize(int width, int height) { window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); }
+        public void _setSize(int width, int height) {
+            g = null;
+            window.setSize(width + (insets.left + insets.right), height + (insets.top + insets.bottom)); }
         public void setInvisible(boolean b) { window.setVisible(!b); }
         protected void _setMinimized(boolean b) { if (Log.on) Log.info(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
         protected void _setMaximized(boolean b) {
@@ -290,14 +312,15 @@ public class AWT extends JVM {
             }
             window.setLocation(new Point(0, 0));
             window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
+            g = null;
         }
 
         class InnerFrame extends Frame {
             public InnerFrame() throws java.lang.UnsupportedOperationException { }
-            public Dimension getMinimumSize() {
-                return new Dimension(root == null ? 0 : root.minwidth, root == null ? 0 : root.minheight); }
+            public Dimension getMinimumSize() { return new Dimension(root.minwidth(), root.minheight()); }
             public void update(Graphics gr) { paint(gr); }
             public void paint(Graphics gr) {
+                // Mac OS X Jdk1.4 Bug: after a componentResized(), you must wait for the paint() before you redraw
                 Rectangle r = gr.getClipBounds();
 
                 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
@@ -307,31 +330,32 @@ public class AWT extends JVM {
                     componentResized(window.getWidth() - insets.left - insets.right,
                                      window.getHeight() - insets.top - insets.bottom);
 
-                Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
+                //Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
+                g = gr;
+                blit(r.x - insets.left, r.y - insets.top, r.width, r.height);
+                g.setClip(0, 0, getWidth(), getHeight());
             }
         }
 
         class InnerWindow extends Window {
             public InnerWindow() throws java.lang.UnsupportedOperationException { super(new Frame()); }
-            public Dimension getMinimumSize() {
-                return new Dimension(root == null ? 0 : root.minwidth, root == null ? 0 : root.minheight); }
+            public Dimension getMinimumSize() { return new Dimension(root.minwidth(), root.minheight()); }
             public void update(Graphics gr) { paint(gr); }
             public void paint(Graphics gr) {
+                g = null;
                 Rectangle r = gr.getClipBounds();
                 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
             }
         }
 
-        public void setMinimumSize(int minx, int miny, boolean resizable) { if (frame != null) frame.setResizable(resizable); }
-
         private int oldfill = 0x0;
         public void render() {
             // useful optimizatin;
             if (oldfill != root.fillcolor) {
                 oldfill = root.fillcolor;
                 window.setBackground((root.fillcolor & 0xFF000000) == 0 ?
-                                     Color.white :
-                                     new Color((root.fillcolor >> 16) & 0xff,
+                                     java.awt.Color.white :
+                                     new java.awt.Color((root.fillcolor >> 16) & 0xff,
                                                (root.fillcolor >> 8) & 0xff,
                                                (root.fillcolor) & 0xff));
             }
@@ -420,7 +444,11 @@ public class AWT extends JVM {
             componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
         }
 
-        public void componentResized(int newwidth, int newheight) { SizeChange(newwidth, newheight); }
+        public void componentResized(int newwidth, int newheight) {
+            SizeChange(newwidth, newheight);
+            //if (newwidth > root.width) Dirty(root.width, 0, newwidth-root.width, newheight);
+            //if (newheight > root.height) Dirty(0, root.height, newwidth, newheight-root.height);
+        }
 
         public void keyTyped(KeyEvent k) { }
         public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }