2003/09/19 05:26:47
[org.ibex.core.git] / src / org / xwt / plat / AWT.java
index 9b01d2f..6e01352 100644 (file)
@@ -1,9 +1,8 @@
-// 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.*;
 import org.xwt.util.*;
-import org.mozilla.javascript.*;
 import java.net.*;
 import java.io.*;
 import java.util.*;
@@ -11,13 +10,12 @@ import java.awt.*;
 import java.awt.datatransfer.*;
 import java.awt.image.*;
 import java.awt.event.*;
-import java.applet.*;
 
 /** Platform subclass for all VM's providing AWT 1.1 functionality */
-public class AWT extends Platform {
+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 PixelBuffer _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 int _getScreenWidth() { return Toolkit.getDefaultToolkit().getScreenSize().width; }
     protected int _getScreenHeight() { return Toolkit.getDefaultToolkit().getScreenSize().height; }
@@ -25,11 +23,10 @@ public class AWT extends Platform {
     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 true; }
+    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.log(Platform.class, "               color depth = " + Toolkit.getDefaultToolkit().getColorModel().getPixelSize() + "bpp");
     }
 
     protected void _criticalAbort(String message) {
@@ -69,8 +66,16 @@ public class AWT extends Platform {
     /** some platforms (cough, cough, NetscapeVM) have totally broken modifier masks; they will need to override this */
     protected static int modifiersToButtonNumber(int modifiers) {
         if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) return 1;
-        if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) return 3;
-        if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) return 2;
+        if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
+            // ugh, MacOSX reports the right mouse button as BUTTON2_MASK...
+            if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 2;
+            return 3;
+        }
+        if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
+            // ugh, MacOSX reports the right mouse button as BUTTON2_MASK...
+            if (System.getProperty("os.name", "").startsWith("Mac OS X")) return 3;
+            return 2;
+        }
         return 0;
     }
 
@@ -112,19 +117,19 @@ public class AWT extends Platform {
 
     // Inner Classes /////////////////////////////////////////////////////////////////////////////////////
 
-    protected static class AWTPicture implements Picture {
+    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; }
 
         int[] data = null;
         public Image i = null;
-        private static MediaTracker mediatracker = new MediaTracker(new Canvas());
         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));
+            MediaTracker mediatracker = new MediaTracker(new Canvas());
             mediatracker.addImage(img, 1);
             try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
             mediatracker.removeImage(img);
@@ -132,7 +137,7 @@ public class AWT extends Platform {
         }
     }
     
-    protected static class AWTDoubleBuffer implements DoubleBuffer {
+    protected static class AWTPixelBuffer extends DoubleBuffer {
         
         protected Image i = null;
         protected Graphics g = null;
@@ -140,9 +145,9 @@ public class AWT extends Platform {
         /** 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);
@@ -184,9 +189,9 @@ public class AWT extends Platform {
     protected static class AWTSurface extends Surface
         implements MouseListener, MouseMotionListener, KeyListener, ComponentListener, WindowListener {
 
-        public void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2) {
+        public void blit(PixelBuffer 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,
+            ourGraphics.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);
         }
         
@@ -227,15 +232,12 @@ public class AWT extends Platform {
                 Rectangle r = gr.getClipBounds();
 
                 // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches expansions during smooth resize
-                int newwidth = Math.max(r.x - insets.left + r.width, width);
-                int newheight = Math.max(r.y - insets.top + r.height, height);
-                if (newwidth > width || newheight > height)
+                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);
 
                 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
-
-                // this is safe because AWT's low-level drawing functions are threadsafe
-                blitDirtyScreenRegions();
             }
         }
 
@@ -245,9 +247,6 @@ public class AWT extends Platform {
             public void paint(Graphics gr) {
                 Rectangle r = gr.getClipBounds();
                 Dirty(r.x - insets.left, r.y - insets.top, r.width, r.height);
-
-                // this is safe because AWT's low-level drawing functions are threadsafe
-                blitDirtyScreenRegions();
             }
         }
 
@@ -273,9 +272,10 @@ public class AWT extends Platform {
 
             // IMPORTANT: this must be called before render() to ensure
             // that our peer has been created
-            window.setVisible(true);
-
+            makeVisible();
         }
+
+        protected void makeVisible() { window.setVisible(true); }
         
         public void _dispose() {
             window.removeMouseListener(this);
@@ -321,10 +321,10 @@ public class AWT extends Platform {
         public void windowClosed(WindowEvent e) { }
         public void windowClosing(WindowEvent e) { Close(); }
         public void windowIconified(WindowEvent e) { Minimized(true); }
-        public void windowDeiconified(WindowEvent e) { dirty(0, 0, width, height); Minimized(false); }
+        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, window.getLocation().y); }
+    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
@@ -333,8 +333,8 @@ public class AWT extends Platform {
         }
 
         public void componentResized(int newwidth, int newheight) {
-            int oldwidth = width;
-            int oldheight = height;
+            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
@@ -343,7 +343,7 @@ public class AWT extends Platform {
 
             ourGraphics = null;
         }
-        
+
         public void keyTyped(KeyEvent k) { }
         public void keyPressed(KeyEvent k) { KeyPressed(translateKey(k)); }
         public void keyReleased(KeyEvent k) { KeyReleased(translateKey(k)); }
@@ -355,7 +355,7 @@ public class AWT extends Platform {
             // ugly hack for Java1.4 dynamicLayout on Win32 -- this catches contractions during smooth resize
             int newwidth = window.getWidth() - insets.left - insets.right;
             int newheight = window.getHeight() - insets.top - insets.bottom;
-            if (newwidth != width || newheight != height) componentResized(newwidth, newheight);
+            if (newwidth != root.width || newheight != root.height) componentResized(newwidth, newheight);
             
             Move(m.getX() - insets.left, m.getY() - insets.top);
         }
@@ -408,11 +408,40 @@ public class AWT extends Platform {
         }
     }
 
+    protected ImageDecoder _decodeJPEG(InputStream is, String name) {
+        try {
+            Image i = Toolkit.getDefaultToolkit().createImage(org.xwt.Resources.isToByteArray(is));
+            MediaTracker mediatracker = new MediaTracker(new Canvas());
+            mediatracker.addImage(i, 1);
+            try { mediatracker.waitForAll(); } catch (InterruptedException e) { }
+            mediatracker.removeImage(i);
+            final int width = i.getWidth(null);
+            final int height = i.getHeight(null);
+            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; }
+                };
+        } 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++) {
@@ -421,6 +450,8 @@ public class AWT extends Platform {
             fontList[i * 4 + 2] = awtfonts[i] + "*i";
             fontList[i * 4 + 3] = awtfonts[i] + "*bi";
         }
+        */
+        fontList = new String[] { };
     }
 
     private static Hash fontCache = new Hash();