propose-patch
[org.ibex.core.git] / src / org / xwt / plat / Win32.java
index 3e647ac..a17323b 100644 (file)
@@ -1,14 +1,14 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [LGPL]
 package org.xwt.plat;
 
 import org.xwt.*;
 import org.xwt.util.*;
 import java.util.*;
+import org.xwt.js.*;
 import java.io.*;
 
 /** Platform specific code for GCJ-compiled Win32 binaries */
 public class Win32 extends GCJ {
-
     // Initialization ////////////////////////////////////////////////////////////////////////////
 
     // Win32 often asks for a DC/Handle when it doesn't really need one
@@ -38,70 +38,15 @@ public class Win32 extends GCJ {
 
     public static native String getTempPath();
     public static native void natInit();
+    public static native void natPreInit();
 
     protected native String _fileDialog(String suggestedFileName, boolean write);
 
-    public Win32() { }
-
-    public void init() {
-        String logfile = getTempPath() + "xwt-log.txt";
-        try {
-            PrintStream ps = new PrintStream(new FileOutputStream(logfile));
-            System.setOut(ps);
-            System.setErr(ps);
-        } catch (Throwable e) {
-            criticalAbort("Exception while attempting to redirect logging to " + logfile + " -- " + e);
-        }
+    public Win32() { natPreInit(); }
 
+    public void postInit() {
         new Thread() { public void run() { natInit(); } }.start();
         messagePumpStarted.block();
-        fontList = new String[fontListVec.size()];
-        fontListVec.toArray(fontList);
-        fontListVec = null;
-    }
-
-
-    // Font Handling ////////////////////////////////////////////////////////////////////////////
-
-    // FEATURE: query the registry for the user's default font
-    protected String _getDefaultFont() { return "dialog8"; }
-    protected int _getMaxAscent(String font) { return getFont(font).maxAscent; }
-    protected int _getMaxDescent(String font) { return getFont(font).maxDescent; }
-    protected native int _stringWidth(String font, String text);
-
-    // methods/members used to enumerate platform fonts on startup
-    public static Vector fontListVec = new Vector();
-    public static String[] fontList = null;
-    protected String[] _listFonts() { return fontList; }
-    public static void addFont(String name, int height, boolean italic, boolean bold) {
-        fontListVec.addElement(name.replace(' ', '_').toLowerCase() + "" + height + (italic ? "i" : "") + (bold ? "b" : ""));
-    }
-
-    static Hash fontCache = new Hash();
-    public static class Win32Font {
-        int hfont;
-        int maxAscent;
-        int maxDescent;
-    }
-
-    /** takes a parsed font and finds the closest platform-specific font */
-    static native Win32Font mapFont(Platform.ParsedFont pf);
-
-    /** takes an unparsed font and finds the closest platform-specific font */
-    static Win32Font getFont(String font) {
-        Win32Font ret = (Win32Font)fontCache.get(font);
-        if (ret != null) return ret;
-
-        Platform.ParsedFont pf = new Platform.ParsedFont(font);
-        if (pf.name.equals("serif")) pf.name = "Times New Roman";
-        else if (pf.name.equals("sansserif")) pf.name = "Arial";
-        else if (pf.name.equals("monospace")) pf.name = "Courier New";
-        else if (pf.name.equals("dialog")) pf.name = "MS Sans Serif";
-        else if (pf.name.equals("tty")) pf.name = "FixedSys";
-
-        ret = mapFont(pf);
-        fontCache.put(font, ret);
-        return ret;
     }
 
 
@@ -111,8 +56,8 @@ public class Win32 extends GCJ {
     protected boolean _needsAutoClick() { return true; }
     protected String getDescriptiveName() { return "GCJ Win32 Binary"; }
     protected Surface _createSurface(Box b, boolean framed) { return new Win32Surface(b, framed); }
-    protected DoubleBuffer _createDoubleBuffer(int w, int h, Surface owner) { return new Win32DoubleBuffer(w, h, (Win32Surface)owner); }
-    protected Picture _createPicture(int[] b, int w, int h) { return new Win32Picture(b, w, h); }
+    protected PixelBuffer _createPixelBuffer(int w, int h, Surface owner) { return new Win32PixelBuffer(w, h, (Win32Surface)owner); }
+    protected Picture _createPicture(JS r) { return new Win32Picture(r); }
     protected native int _getScreenWidth();
     protected native int _getScreenHeight();
     protected boolean _supressDirtyOnResize() { return false; }
@@ -123,23 +68,23 @@ public class Win32 extends GCJ {
 
     private native void __detectProxy(String[] container);
 
-    protected synchronized Proxy _detectProxy() {
+    protected synchronized HTTP.Proxy _detectProxy() {
 
         String[] container = new String[] { null, null, null };
-        if (Log.on) Log.log(this, "accessing Win32 registry");
+        if (Log.on) Log.info(this, "accessing Win32 registry");
         __detectProxy(container);
         if (container[2] == null && container[0] == null) {
-            if (Log.on) Log.log(this, "no proxy settings in the Win32 registry");
+            if (Log.on) Log.info(this, "no proxy settings in the Win32 registry");
             return null;
         }
         
-        if (Log.on) Log.log(this, "PAC Script URL: " + container[2]);
-        if (Log.on) Log.log(this, "Proxy Server String: " + container[0]);
-        if (Log.on) Log.log(this, "Proxy Override String: " + container[1]);
+        if (Log.on) Log.info(this, "PAC Script URL: " + container[2]);
+        if (Log.on) Log.info(this, "Proxy Server String: " + container[0]);
+        if (Log.on) Log.info(this, "Proxy Override String: " + container[1]);
 
-        Proxy ret = new Proxy();
+        HTTP.Proxy ret = new HTTP.Proxy();
         if (container[2] != null) {
-            ret.proxyAutoConfigFunction = Proxy.getProxyAutoConfigFunction(container[2]);
+            ret.proxyAutoConfigFunction = HTTP.Proxy.getProxyAutoConfigFunction(container[2]);
             if (ret.proxyAutoConfigFunction != null) return ret;
         }
 
@@ -185,12 +130,12 @@ public class Win32 extends GCJ {
     protected native boolean _newBrowserWindow_(String url);
     protected void _newBrowserWindow(String url) {
         if (!_newBrowserWindow_(url))
-            if (Log.on) Log.log(this, "ShellExecuteEx() failed trying to open url " + url);
+            if (Log.on) Log.info(this, "ShellExecuteEx() failed trying to open url " + url);
     }
 
     // Win32Surface ////////////////////////////////////////////////////////////////////////////
 
-    public static class Win32Surface extends Surface {
+    public static class Win32Surface extends Surface.DoubleBufferedSurface {
 
         /** used to block while waiting for the message pump thread to create a hwnd for us */
         public Semaphore hwndCreated = new Semaphore();
@@ -240,20 +185,18 @@ public class Win32 extends GCJ {
         public native void _setMinimized(boolean m);
         public native void setInvisible(boolean i);
         public native void _setMaximized(boolean m);
-        public native void setSize(int w, int h);
-        public native void setLocation(int x, int y);
+        public native void _setSize(int w, int h);
+        public native void setLocation();
         public native void setTitleBarText(String s);
         public native void setIcon(Picture p);
         public native void _dispose();
-        public native void blit(DoubleBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
+        public native void blit(PixelBuffer s, int sx, int sy, int dx, int dy, int dx2, int dy2);
     }
 
 
     // Win32Picture ////////////////////////////////////////////////////////////////////////////
 
-    public static class Win32Picture implements Picture {
-        int w = 0, h = 0;
-        int[] data = null;
+    public static class Win32Picture extends Picture {
 
         /** the Win32 bitmap version of this Picture */
         int hbitmap = -1;
@@ -273,17 +216,19 @@ public class Win32 extends GCJ {
         /** dc of the mask */
         int maskdc = -1;
 
-        public int getWidth() { return w; };
-        public int getHeight() { return h; };
+        public Win32Picture(JS r) { super(r); }
+        public int getWidth() { return width; };
+        public int getHeight() { return height; };
         public int[] getData() { return data; }
+        boolean initialized = false;
+        public void init() { if (!initialized && isLoaded) natInit(); initialized = true; }
         public native void natInit();
-        public Win32Picture(int[] data, int w, int h) { this.w = w; this.h = h; this.data = data; natInit(); }
     }
     
 
-    // Win32DoubleBuffer //////////////////////////////////////////////////////////////////////////
+    // Win32PixelBuffer //////////////////////////////////////////////////////////////////////////
 
-    public static class Win32DoubleBuffer implements DoubleBuffer {
+    public static class Win32PixelBuffer extends PixelBuffer {
 
         int w = 0;
         int h = 0;
@@ -300,7 +245,7 @@ public class Win32 extends GCJ {
         public int getWidth() { return w; }
 
         public native void natInit();
-        public Win32DoubleBuffer(int w, int h, Win32Surface owner) {
+        public Win32PixelBuffer(int w, int h, Win32Surface owner) {
             this.w = w;
             this.h = h;
             clipx2 = w;
@@ -308,15 +253,34 @@ public class Win32 extends GCJ {
             natInit();
         }
 
-        public native void setClip(int x, int y, int x2, int y2);
         public native void fillRect(int x, int y, int x2, int y2, int color);
-        public native void drawString(String font, String text, int x, int y, int color);
-        public native void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2);
-        public native void finalize();
-        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) {
+            ((Win32Picture)source).init();
+            drawPicture(source, dx, dy, cx1, cy1, cx2, cy2, 0, false);
         }
+        public void drawGlyph(org.xwt.Font.Glyph source, int dx, int dy, int cx1, int cy1, int cx2, int cy2, int rgb) {
+            Win32Picture p = ((Win32Picture)((Platform.DefaultGlyph)source).getPicture());
+            p.init();
+            drawPicture(p, dx, dy, cx1, cy1, cx2, cy2, rgb, true);
+        }
+        public native void drawPicture(Picture source, int dx, int dy, int cx1, int cy1, int cx2, int cy2,
+                                       int rgb, boolean alphaOnly);
+
+        public native void finalize();
 
+        // FIXME: try to use os acceleration
+        public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
+            if (x1 == x3 && x2 == x4) {
+                fillRect(x1, y1, x4, y2, argb);
+            } 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; }
+                fillRect(_x1, _y1, _x2, _y2, argb);
+            }
+        }
     }
 
 }