2004/01/13 10:27:47
[org.ibex.core.git] / src / org / xwt / plat / AWT.java
index c63e9a7..6a0ed9b 100644 (file)
@@ -1,10 +1,11 @@
-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt.plat;
 
 import org.xwt.*;
 import org.xwt.util.*;
 import java.net.*;
 import java.io.*;
+import org.xwt.js.*;
 import java.util.*;
 import java.awt.*;
 import java.awt.datatransfer.*;
@@ -16,18 +17,18 @@ public class AWT extends JVM {
 
     protected String getDescriptiveName() { return "Generic JDK 1.1+ with AWT"; }
     protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new AWTPixelBuffer(w, h); }
-    protected Picture _createPicture(Res r) { return new AWTPicture(r); }
+    protected Picture _createPicture(JS 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 void postInit() {
-        if (Log.on) Log.log(Platform.class, "               color depth = " +
+        if (Log.on) Log.diag(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);
@@ -117,11 +118,28 @@ public class AWT extends JVM {
     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 ColorModel cmodel = new DirectColorModel(8, 0x00000000, 0x00000000, 0x00000000, 0xFF);
+        private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
+
+        // this doesn't work on Win32 because the JVM is broken
+        /*
+        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));
+
+                int[] data2 = new int[data.length];
+                for(int i=0; i<data2.length; i++) data2[i] = ((data[i]) & 0xff) << 24;
+
+                MemoryImageSource mis = new MemoryImageSource(width, height, cmodel, data2, 0, width);
+                mis.setAnimated(true);
+                i = Toolkit.getDefaultToolkit().createImage(mis);
                 MediaTracker mediatracker = new MediaTracker(new Canvas());
                 mediatracker.addImage(i, 1);
                 try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
@@ -144,11 +162,13 @@ public class AWT extends JVM {
         private static ColorModel cmodel = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
         
         boolean initialized = false;
-        public AWTPicture(Res r) { super(r); }
+        public AWTPicture(JS r) { super(r); }
         public void init() {
             if (initialized) return;
             initialized = true;
-            i = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, cmodel, data, 0, width));
+            MemoryImageSource mis = new MemoryImageSource(width, height, cmodel, data, 0, width);
+            mis.setAnimated(true);
+            i = Toolkit.getDefaultToolkit().createImage(mis);
             MediaTracker mediatracker = new MediaTracker(new Canvas());
             mediatracker.addImage(i, 1);
             try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
@@ -238,9 +258,8 @@ public class AWT extends JVM {
         implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
 
         public void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
-            if (ourGraphics == null) ourGraphics = window.getGraphics();
             insets = (frame == null ? window : frame).getInsets();
-            ourGraphics.drawImage(((AWTPixelBuffer)s).i,
+            window.getGraphics().drawImage(((AWTPixelBuffer)s).i,
                                   dx + insets.left,
                                   dy + insets.top,
                                   dx2 + insets.left,
@@ -255,9 +274,6 @@ 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();
         
@@ -268,10 +284,10 @@ public class AWT extends JVM {
         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 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));
@@ -304,6 +320,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 {
@@ -312,8 +338,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();
@@ -386,17 +412,7 @@ 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) {
-            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 componentResized(int newwidth, int newheight) { SizeChange(newwidth, newheight); }
 
         public void keyTyped(KeyEvent k) { }
         public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
@@ -475,13 +491,13 @@ public class AWT extends JVM {
             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");
+                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");
-            Log.log(this, e);
+            Log.info(this, "Exception caught while decoding JPEG image");
+            Log.info(this, e);
         }
     }
 }