2002/04/28 04:36:08
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:46:07 +0000 (06:46 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:46:07 +0000 (06:46 +0000)
darcs-hash:20040130064607-2ba56-5e9ee5752bbb31aaba6142ac94d6c535e4ccf5d7.gz

CHANGES
src/org/xwt/Main.java
src/org/xwt/Platform.java
src/org/xwt/XWT.java
src/org/xwt/plat/Java2.java
src/org/xwt/plat/Win32.cc
src/org/xwt/plat/Win32.java

diff --git a/CHANGES b/CHANGES
index 9306fa8..960ab32 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -57,4 +57,7 @@
 
 27-Apr megacz JSObject.java: added extra debugging info
 
+27-Apr megacz XWT.java, Platform.java, Main.java, Java2.java,
+              Win32.java, Win32.cc, faq.html: added support for
+              xwt.newBrowserWindow()
 
index 1817671..aec6296 100644 (file)
@@ -35,11 +35,14 @@ public class Main extends Applet {
     public void paint(Graphics g) { }    // do-nothing method to avoid repaints
     public void update(Graphics g) { }   // do-nothing method, to avoid repaints
 
+    public static Applet applet = null;
+
     /** applet entry point */
     public void init() {
         if ("true".equals(getParameter("showRenders"))) showRenders = true;
         if ("true".equals(getParameter("verbose"))) Log.verbose = true;
-        main(new String[] { getParameter("xwar-url"), "main" });
+        applet = this;
+        main(new String[] { getParameter("initial-xwar-url"), "main" });
     }
 
     /** common entry point */
index 173ae0c..710df27 100644 (file)
@@ -156,6 +156,11 @@ public class Platform {
 
     protected boolean _needsAutoClick() { return false; }
 
+    protected void _newBrowserWindow(String url) {
+        if (Log.on) Log.log(this, "Platform " + platform.getClass().getName() + " cannot open browser windows");
+        return;
+    }
+
     // Static methods -- thunk to the instance /////////////////////////////////////////////////////////////////////////
 
     /** if true, org.xwt.Surface should generate Click messages automatically when a Release happens after a Press and the mouse has not moved much */
@@ -212,6 +217,15 @@ public class Platform {
     /** creates and returns a picture */
     public static Picture createPicture(ImageDecoder i) { return platform._createPicture(i.getData(), i.getWidth(), i.getHeight()); }
 
+    /** creates and returns a picture */
+    public static void newBrowserWindow(String url) {
+        if (!(url.startsWith("https://") || url.startsWith("http://") || url.startsWith("ftp://") || url.startsWith("mailto:"))) {
+            if (Log.on) Log.log(Platform.class, "xwt.newBrowserWindow() only supports http and https urls");
+            return;
+        }
+        platform._newBrowserWindow(url);
+    }
+
     /** quits XWT */
     public static void exit() {
         Log.log(Platform.class, "exiting via Platform.exit()");
index e3368aa..1351177 100644 (file)
@@ -28,6 +28,7 @@ public final class XWT extends JSObject {
         else if (name.equals("regexp")) return regexp;
         else if (name.equals("sleep")) return sleep;
         else if (name.equals("yield")) return yield;
+        else if (name.equals("newBrowserWindow")) return newBrowserWindow;
         else if (name.equals("textwidth")) return textwidth;
         else if (name.equals("textheight")) return textheight;
         else if (name.equals("newBox")) return newBox;
@@ -64,6 +65,14 @@ public final class XWT extends JSObject {
         public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
     }
 
+    private static final JSFunction newBrowserWindow = new JSFunction() {
+            public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+                if (args.length != 1 || args[0] == null) return null;
+                Platform.newBrowserWindow(args[0].toString());
+                return null;
+            }
+        };
+
     private static final JSFunction yield = new JSFunction() {
             public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
                 sleep.call(cx, null, null, null);
index 9ae2043..1ff8764 100644 (file)
@@ -177,4 +177,24 @@ public class Java2 extends AWT {
     }
 
     protected String getDescriptiveName() { return "Java 1.2+ JVM"; }
+
+    protected void _newBrowserWindow(String url) {
+        if (Main.applet == null) {
+            if (Log.on) Log.log(this, "Main.applet is null; cannot invoke showDocument()");
+            return;
+        }
+        if (Log.on) Log.log(this, "asking browser to show URL " + url);
+        try {
+            Main.applet.getAppletContext().showDocument(new URL(url), "_blank");
+        } catch (MalformedURLException e) {
+            if (Log.on) Log.log(this, e);
+        }
+    }
+
+    /** used to notify the user of very serious failures; usually used when logging is not working or unavailable */
+    protected void _criticalAbort(String message) {
+        if (Log.on) Log.log(this, message);
+        new Semaphore().block();
+    }
+
 }
index af70e1d..f3f5fd7 100644 (file)
@@ -324,6 +324,23 @@ jint org::xwt::plat::Win32::_stringWidth(jstring font, jstring text) {
     return size.cx;
 }
 
+jboolean org::xwt::plat::Win32::_newBrowserWindow_(jstring url) {
+
+    int len = min(2048, JvGetStringUTFLength(url));
+    char buf[len + 1];
+    JvGetStringUTFRegion(url, 0, len, buf);
+    buf[len] = '\0';
+
+    SHELLEXECUTEINFO ei;
+    memset(&ei, 0, sizeof(ei));
+    ei.cbSize = sizeof(ei);
+    ei.lpVerb = "open";
+    ei.lpFile = buf;
+    ei.fMask  = SEE_MASK_NOCLOSEPROCESS;
+    ei.nShow  = SW_SHOWDEFAULT;
+    return (ShellExecuteEx(&ei) == 0);
+
+}
 
 
 // Win32DoubleBuffer /////////////////////////////////////////////////////////////////////////
index 27ce98b..65f79ec 100644 (file)
@@ -117,6 +117,11 @@ public class Win32 extends GCJ {
     protected native String _getClipBoard();
     protected native void _setClipBoard(String s);
 
+    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);
+    }
 
     // Win32Surface ////////////////////////////////////////////////////////////////////////////