2003/12/29 03:25:43
[org.ibex.core.git] / src / org / xwt / plat / AWT.java
index 676378c..2af3e80 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt.plat;
 
 import org.xwt.*;
@@ -15,22 +15,19 @@ import java.awt.event.*;
 public class AWT extends JVM {
 
     protected String getDescriptiveName() { return "Generic JDK 1.1+ with AWT"; }
-    protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new AWTDoubleBuffer(w, h); }
-    protected Picture _createPicture(int[] b, int w, int h) { return new AWTPicture(b, w, h); }
+    protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new AWTPixelBuffer(w, h); }
+    protected Picture _createPicture(Res r) { return new AWTPicture(r); }
     protected int _getScreenWidth() { return Toolkit.getDefaultToolkit().getScreenSize().width; }
     protected int _getScreenHeight() { return Toolkit.getDefaultToolkit().getScreenSize().height; }
     protected Surface _createSurface(Box b, boolean framed) { return new AWTSurface(b, framed); }
-    protected int _stringWidth(String font, String text) { return getFont(font).metrics.stringWidth(text); }
-    protected int _getMaxAscent(String font) { return getFont(font).metrics.getMaxAscent(); }
-    protected int _getMaxDescent(String font) { return getFont(font).metrics.getMaxDescent(); }
-    protected boolean _supressDirtyOnResize() { return false; }
 
     protected void postInit() {
-        if (Log.on) Log.log(Platform.class, "               color depth = " + Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp");
+        if (Log.on) Log.info(Platform.class, "               color depth = " +
+                            Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp");
     }
 
     protected void _criticalAbort(String message) {
-        if (Log.on) Log.log(this, message);
+        if (Log.on) Log.info(this, message);
         final Dialog d = new Dialog(new Frame(), "XWT Cannot Continue");
         d.setLayout(new BorderLayout());
         TextArea ta = new TextArea("XWT cannot continue because:\n\n" + message, 10, 80);
@@ -111,33 +108,68 @@ public class AWT extends JVM {
         final Semaphore s = new Semaphore();
         FileDialogHelper fd = new FileDialogHelper(suggestedFileName, s, write);
         s.block();
-        return fd.getDirectory() + File.separatorChar + fd.getFile();
+        return fd.getDirectory() == null ? null : (fd.getDirectory() + File.separatorChar + fd.getFile());
     }
 
 
     // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
 
-    protected static class AWTPicture extends Picture {
-        public int getHeight() { return i.getHeight(null); }
-        public int getWidth() { return i.getWidth(null); } 
-        public int[] getData() { return data; }
+    protected org.xwt.Font.Glyph _createGlyph(org.xwt.Font f, char c) { return new AWTGlyph(f, c); }
+    protected static class AWTGlyph extends org.xwt.Font.Glyph {
+        private Image i = null;
+        static final ColorModel cmodel = new ColorModel(8) {
+            public int getRed(int p) { return 0; }
+            public int getGreen(int p) { return 0; }
+            public int getBlue(int p) { return 0; }
+            public int getAlpha(int p) { return p & 0xFF; }
+        };
+
+        public AWTGlyph(org.xwt.Font f, char c) { super(f, c); }
+        Image getImage() {
+            if (i == null && isLoaded) {
+                i = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, cmodel, data, 0, width));
+                MediaTracker mediatracker = new MediaTracker(new Canvas());
+                mediatracker.addImage(i, 1);
+                try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
+                mediatracker.removeImage(i);
+                synchronized(AWTPixelBuffer.class) { 
+                    if (AWTPixelBuffer.component == null) {
+                        AWTPixelBuffer.component = new Frame();
+                        AWTPixelBuffer.component.setVisible(false);
+                        AWTPixelBuffer.component.addNotify();
+                    }
+                }
+                data = null;
+            }
+            return i;
+        }
+    }
 
-        int[] data = null;
+    protected static class AWTPicture extends Picture {
         public Image i = null;
         private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
         
-        public AWTPicture(int[] b, int w, int h) {
-            data = b;
-            Image img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, cmodel, b, 0, w));
+        boolean initialized = false;
+        public AWTPicture(Res r) { super(r); }
+        public void init() {
+            if (initialized) return;
+            initialized = true;
+            i = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, cmodel, data, 0, width));
             MediaTracker mediatracker = new MediaTracker(new Canvas());
-            mediatracker.addImage(img, 1);
+            mediatracker.addImage(i, 1);
             try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
-            mediatracker.removeImage(img);
-            this.i = img;
+            mediatracker.removeImage(i);
+            synchronized(AWTPixelBuffer.class) { 
+                if (AWTPixelBuffer.component == null) {
+                    AWTPixelBuffer.component = new Frame();
+                    AWTPixelBuffer.component.setVisible(false);
+                    AWTPixelBuffer.component.addNotify();
+                }
+            }
         }
     }
     
-    protected static class AWTDoubleBuffer extends DoubleBuffer {
+    protected static class AWTPixelBuffer extends PixelBuffer {
         
         protected Image i = null;
         protected Graphics g = null;
@@ -145,9 +177,9 @@ public class AWT extends JVM {
         /** JDK1.1 platforms require that a component be associated with each off-screen buffer */
         static Component component = null;
 
-        protected AWTDoubleBuffer() { }
-        public AWTDoubleBuffer(int w, int h) {
-            synchronized(AWTDoubleBuffer.class) {
+        protected AWTPixelBuffer() { }
+        public AWTPixelBuffer(int w, int h) {
+            synchronized(AWTPixelBuffer.class) {
                 if (component == null) {
                     component = new Frame();
                     component.setVisible(false);
@@ -160,38 +192,64 @@ public class AWT extends JVM {
         
         public int getHeight() { return i == null ? 0 : i.getHeight(null); }
         public int getWidth() { return i == null ? 0 : i.getWidth(null); }
-        public void setClip(int x, int y, int x2, int y2) { g.setClip(x, y, x2 - x, y2 - y); }
 
-        public void drawPicture(Picture source, int x, int y) {
-            drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
+        public void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2) {
+            ((AWTPicture)source).init();
+            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));
         }
 
-        public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
-            g.drawImage(((AWTPicture)source).i, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
-        }
-        
-        public void drawString(String font, String text, int x, int y, int argb) {
-            // FEATURE: use an LRU cache for Color objects
-            g.setColor(new Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8, (argb & 0x000000FF)));
-            g.setFont(getFont(font));
-            g.drawString(text, x, y + 2);
+        /** implemented with java.awt 1.1's setXORMode() */
+        public void drawGlyph(org.xwt.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));
+
+            // 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);
+
+            // restore the graphics context
+            g.setPaintMode();
         }
-        
-        public void fillRect(int x, int y, int x2, int y2, int argb) {
-            // FEATURE: use an LRU cache for Color objects
+
+        // 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)));
-            g.fillRect(x, y, x2 - x, y2 - y);
+            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);
+            }
         }
-
     }
     
     
-    protected static class AWTSurface extends Surface
+    protected static class AWTSurface extends Surface.DoubleBufferedSurface
         implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
 
-        public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
-            if (ourGraphics == null) ourGraphics = window.getGraphics();
-            ourGraphics.drawImage(((AWTDoubleBuffer)s).i, dx + insets.left, dy + insets.top, dx2 + insets.left, dy2 + insets.top,
+        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);
         }
         
@@ -202,23 +260,20 @@ public class AWT extends JVM {
         /** our component's insets */
         protected Insets insets = new Insets(0, 0, 0, 0);
         
-        /** a Graphics context on <code>window</code> */
-        protected Graphics ourGraphics = null;
-        
         /** some JDKs let us recycle a single Dimension object when calling getSize() */
         Dimension singleSize = new Dimension();
         
         public void toBack() { if (window != null) window.toBack(); }
         public void toFront() { if (window != null) window.toFront(); }
-        public void setLocation(int x, int y) { window.setLocation(x, y); }
+        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) { 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.log(this, "JDK 1.1 platforms cannot minimize or unminimize windows"); }
+        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) {
             if (!b) {
-                if (Log.on) Log.log(this, "JDK 1.1 platforms cannot unmaximize windows");
+                if (Log.on) Log.info(this, "JDK 1.1 platforms cannot unmaximize windows");
                 return;
             }
             window.setLocation(new Point(0, 0));
@@ -235,7 +290,8 @@ public class AWT extends JVM {
                 int newwidth = Math.max(r.x - insets.left + r.width, root.width);
                 int newheight = Math.max(r.y - insets.top + r.height, root.height);
                 if (newwidth > root.width || newheight > root.height)
-                    componentResized(window.getWidth() - insets.left - insets.right, window.getHeight() - insets.top - insets.bottom);
+                    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);
             }
@@ -250,6 +306,16 @@ public class AWT extends JVM {
             }
         }
 
+        public void render() {
+            // useful optimizatin;
+            window.setBackground((root.fillcolor & 0xFF000000) == 0 ?
+                                 Color.white :
+                                 new Color((root.fillcolor >> 16) & 0xff,
+                                           (root.fillcolor >> 8) & 0xff,
+                                           (root.fillcolor) & 0xff));
+            super.render();
+        }
+
         AWTSurface(Box root, boolean framed) {
             super(root);
             try {
@@ -258,8 +324,8 @@ public class AWT extends JVM {
 
             // this is here to catch HeadlessException on jdk1.4
             } catch (java.lang.UnsupportedOperationException e) {
-                if (Log.on) Log.log(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
-                if (Log.on) Log.log(this, e);
+                if (Log.on) Log.info(this, "Exception thrown in AWTSurface$InnerFrame() -- this should never happen");
+                if (Log.on) Log.info(this, e);
             }
 
             insets = window.getInsets();
@@ -324,7 +390,7 @@ public class AWT extends JVM {
         public void windowDeiconified(WindowEvent e) { dirty(0, 0, root.width, root.height); Minimized(false); }
         public void windowActivated(WindowEvent e) { Focused(true); }
         public void windowDeactivated(WindowEvent e) { Focused(false); }
-    public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); }
+        public void componentMoved(ComponentEvent e) { PosChange(window.getLocation().x + insets.left, window.getLocation().y + insets.top); }
 
         public void componentResized(ComponentEvent e) {
             // we have to periodically do this; I don't know why
@@ -336,12 +402,6 @@ public class AWT extends JVM {
             int oldwidth = root.width;
             int oldheight = root.height;
             SizeChange(newwidth, newheight);
-
-            // we do this because JVMs which don't clear the background won't force repaints of these areas
-            root.dirty(Math.min(oldwidth, newwidth), 0, Math.abs(oldwidth - newwidth), Math.max(oldheight, newheight));
-            root.dirty(0, Math.min(oldheight, newheight), Math.max(oldwidth, newwidth), Math.abs(oldheight - newheight));
-
-            ourGraphics = null;
         }
 
         public void keyTyped(KeyEvent k) { }
@@ -384,7 +444,7 @@ public class AWT extends JVM {
             case KeyEvent.VK_F3: return "f3";
             case KeyEvent.VK_F4: return "f4";
             case KeyEvent.VK_F5: return "f5";
-            case KeyEvent.VK_F6: return "f6";
+            case KeyEvent.VK_F6: return "f6"; 
             case KeyEvent.VK_F7: return "f7";
             case KeyEvent.VK_F8: return "f8";
             case KeyEvent.VK_F9: return "f9";
@@ -408,9 +468,9 @@ public class AWT extends JVM {
         }
     }
 
-    protected ImageDecoder _decodeJPEG(InputStream is, String name) {
+    protected void _decodeJPEG(InputStream is, Picture p) {
         try {
-            Image i = Toolkit.getDefaultToolkit().createImage(org.xwt.Resources.isToByteArray(is));
+            Image i = Toolkit.getDefaultToolkit().createImage(InputStreamToByteArray.convert(is));
             MediaTracker mediatracker = new MediaTracker(new Canvas());
             mediatracker.addImage(i, 1);
             try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
@@ -420,63 +480,14 @@ public class AWT extends JVM {
             final int[] data = new int[width * height];
             PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, data, 0, width);
             pg.grabPixels();
-            if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
-                Log.log(this, "PixelGrabber reported an error while decoding JPEG image " + name);
-                return null;
-            }
-            return new ImageDecoder() {
-                    public int getWidth() { return width; }
-                    public int getHeight() { return height; }
-                    public int[] getData() { return data; }
-                };
+            if ((pg.getStatus() & ImageObserver.ABORT) != 0)
+                Log.info(this, "PixelGrabber reported an error while decoding JPEG image");
+            p.width = width;
+            p.height = height;
+            p.data = data;
         } catch (Exception e) {
-            Log.log(this, "Exception caught while decoding JPEG image " + name);
-            Log.log(this, e);
-            return null;
-        }
-    }
-
-    // Font Handling Stuff //////////////////////////////////////////////////////////
-
-    protected String[] _listFonts() { return fontList; }
-    private static String[] fontList;
-    static {
-        /*
-        String[] awtfonts = Toolkit.getDefaultToolkit().getFontList();
-        fontList = new String[awtfonts.length * 4];
-        for(int i=0; i<awtfonts.length; i++) {
-            fontList[i * 4] = awtfonts[i] + "*";
-            fontList[i * 4 + 1] = awtfonts[i] + "*b";
-            fontList[i * 4 + 2] = awtfonts[i] + "*i";
-            fontList[i * 4 + 3] = awtfonts[i] + "*bi";
+            Log.info(this, "Exception caught while decoding JPEG image");
+            Log.info(this, e);
         }
-        */
-        fontList = new String[] { };
     }
-
-    private static Hash fontCache = new Hash();
-    private static ParsedFont pf = new ParsedFont();
-    private static MetricatedFont getFont(String font) {
-        MetricatedFont ret = (MetricatedFont)fontCache.get(font);
-        if (ret == null) {
-            pf.parse(font);
-            if (pf.name.equals("tty")) pf.name = "monospace";
-            
-            // Java's fonts tend to be, on average, two points smaller than Win32/X11 fonts. This is most acute in
-            // the proxy password dialog on Linux
-            ret = new MetricatedFont(pf.name, (pf.bold ? Font.BOLD : 0) | (pf.italic ? Font.ITALIC : 0), pf.size + 2);
-
-            fontCache.put(font, ret);
-        }
-        return ret;
-    }
-    
-    private static class MetricatedFont extends Font {
-        public FontMetrics metrics = null;
-        public MetricatedFont(String name, int size, int style) {
-            super(name, size, style);
-            metrics = Toolkit.getDefaultToolkit().getFontMetrics(this);
-        }
-    }
-            
 }