diff dwt/browser/WindowCreator2.d @ 125:5583f8eeee6c

Synced mozilla with dwt-linux
author Jacob Carlborg <doob@me.com>
date Fri, 16 Jan 2009 12:49:08 +0100
parents d8635bb48c7c
children
line wrap: on
line diff
--- a/dwt/browser/WindowCreator2.d	Fri Jan 16 12:19:08 2009 +0100
+++ b/dwt/browser/WindowCreator2.d	Fri Jan 16 12:49:08 2009 +0100
@@ -1,4 +1,4 @@
-/*******************************************************************************
+/*******************************************************************************
  * Copyright (c) 2003, 2007 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -7,6 +7,8 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *      John Reimer <terminal.node@gmail.com>
  *******************************************************************************/
 module dwt.browser.WindowCreator2;
 
@@ -14,10 +16,12 @@
 
 import dwt.DWT;
 import dwt.graphics.Point;
-import dwt.internal.C;
+
 import dwt.internal.Platform;
-import dwt.internal.mozilla.XPCOM;
-import dwt.internal.mozilla.XPCOMObject;
+import dwt.internal.mozilla.Common;
+
+import XPCOM = dwt.internal.mozilla.XPCOM;
+
 import dwt.internal.mozilla.nsIBaseWindow;
 import dwt.internal.mozilla.nsID;
 import dwt.internal.mozilla.nsISupports;
@@ -26,137 +30,102 @@
 import dwt.internal.mozilla.nsIWebBrowserChrome;
 import dwt.internal.mozilla.nsIWindowCreator;
 import dwt.internal.mozilla.nsIWindowCreator2;
+import dwt.internal.mozilla.nsStringAPI;
+import dwt.internal.mozilla.nsEmbedString;
+
 import dwt.layout.FillLayout;
 import dwt.widgets.Shell;
 
-class WindowCreator2 {
-    XPCOMObject supports;
-    XPCOMObject windowCreator;
-    XPCOMObject windowCreator2;
+import dwt.browser.Browser;
+import dwt.browser.Mozilla;
+import dwt.browser.VisibilityWindowListener;
+import dwt.browser.CloseWindowListener;
+import dwt.browser.WindowEvent;
+
+class WindowCreator2 : nsIWindowCreator2 {
     int refCount = 0;
 
-this () {
-    createCOMInterfaces ();
-}
+this () {}
 
-int AddRef () {
+extern(System)
+nsrefcnt AddRef () {
     refCount++;
     return refCount;
 }
 
-void createCOMInterfaces () {
-    /* Create each of the interfaces that this object implements */
-    supports = new XPCOMObject (new int[] {2, 0, 0}) {
-        public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
-        public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
-        public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
-    };
-
-    windowCreator = new XPCOMObject (new int[] {2, 0, 0, 3}) {
-        public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
-        public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
-        public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
-        public int /*long*/ method3 (int /*long*/[] args) {return CreateChromeWindow (args[0], cast(int)/*64*/args[1], args[2]);}
-    };
-
-    windowCreator2 = new XPCOMObject (new int[] {2, 0, 0, 3, 6}) {
-        public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
-        public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
-        public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
-        public int /*long*/ method3 (int /*long*/[] args) {return CreateChromeWindow (args[0], cast(int)/*64*/args[1], args[2]);}
-        public int /*long*/ method4 (int /*long*/[] args) {return CreateChromeWindow2 (args[0], cast(int)/*64*/args[1], cast(int)/*64*/args[2], args[3], args[4], args[5]);}
-    };
-}
-
-void disposeCOMInterfaces () {
-    if (supports !is null) {
-        supports.dispose ();
-        supports = null;
-    }   
-    if (windowCreator !is null) {
-        windowCreator.dispose ();
-        windowCreator = null;   
-    }
-
-    if (windowCreator2 !is null) {
-        windowCreator2.dispose ();
-        windowCreator2 = null;  
-    }
-}
-
-int /*long*/ getAddress () {
-    return windowCreator.getAddress ();
-}
-
-int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
-    if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
-    nsID guid = new nsID ();
-    XPCOM.memmove (guid, riid, nsID.sizeof);
+extern(System)
+nsresult QueryInterface (nsID* riid, void** ppvObject) {
+    if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
+    //nsID guid = new nsID ();
+    //XPCOM.memmove (guid, riid, nsID.sizeof);
     
-    if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
-        XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
+    if (*riid == nsISupports.IID) {
+        *ppvObject = cast(void*)cast(nsISupports)this;
         AddRef ();
         return XPCOM.NS_OK;
     }
-    if (guid.Equals (nsIWindowCreator.NS_IWINDOWCREATOR_IID)) {
-        XPCOM.memmove (ppvObject, new int /*long*/[] {windowCreator.getAddress ()}, C.PTR_SIZEOF);
+    if (*riid == nsIWindowCreator.IID) {
+        *ppvObject = cast(void*)cast(nsIWindowCreator)this;
         AddRef ();
         return XPCOM.NS_OK;
     }
-    if (guid.Equals (nsIWindowCreator2.NS_IWINDOWCREATOR2_IID)) {
-        XPCOM.memmove (ppvObject, new int /*long*/[] {windowCreator2.getAddress ()}, C.PTR_SIZEOF);
+    if (*riid == nsIWindowCreator2.IID) {
+        *ppvObject = cast(void*)cast(nsIWindowCreator2)this;
         AddRef ();
         return XPCOM.NS_OK;
     }
 
-    XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
+    *ppvObject = null;
     return XPCOM.NS_ERROR_NO_INTERFACE;
 }
-            
-int Release () {
+
+extern(System)
+nsresult Release () {
     refCount--;
-    if (refCount is 0) disposeCOMInterfaces ();
+    //if (refCount is 0) disposeCOMInterfaces ();
     return refCount;
 }
-    
+
 /* nsIWindowCreator */
 
-int CreateChromeWindow (int /*long*/ parent, int chromeFlags, int /*long*/ _retval) {
-    return CreateChromeWindow2 (parent, chromeFlags, 0, 0, 0, _retval);
+extern(System)
+nsresult CreateChromeWindow (nsIWebBrowserChrome parent, PRUint32 chromeFlags, nsIWebBrowserChrome* _retval) {
+    return CreateChromeWindow2 (parent, chromeFlags, 0, null, null, _retval);
 }
 
 /* nsIWindowCreator2 */
 
-int CreateChromeWindow2 (int /*long*/ parent, int chromeFlags, int contextFlags, int /*long*/ uri, int /*long*/ cancel, int /*long*/ _retval) {
-    if (parent is 0 && (chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) is 0) {
+extern(System)
+nsresult CreateChromeWindow2 (nsIWebBrowserChrome parent, PRUint32 chromeFlags, PRUint32 contextFlags, nsIURI uri, PRBool* cancel, nsIWebBrowserChrome* _retval) {
+    if (parent is null && (chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) is 0) {
         return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
     }
     Browser src = null; 
-    if (parent !is 0) {
-        nsIWebBrowserChrome browserChromeParent = new nsIWebBrowserChrome (parent);
-        int /*long*/[] aWebBrowser = new int /*long*/[1];
-        int rc = browserChromeParent.GetWebBrowser (aWebBrowser);
+    if (parent !is null) {
+        //nsIWebBrowserChrome browserChromeParent = new nsIWebBrowserChrome (parent);
+        nsIWebBrowser webBrowser;
+        int rc = parent.GetWebBrowser (&webBrowser);
         if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-        if (aWebBrowser[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
+        if (webBrowser is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
 
-        nsIWebBrowser webBrowser = new nsIWebBrowser (aWebBrowser[0]);
-        int /*long*/[] result = new int /*long*/[1];
-        rc = webBrowser.QueryInterface (nsIBaseWindow.NS_IBASEWINDOW_IID, result);
+        //nsIWebBrowser webBrowser = new nsIWebBrowser (aWebBrowser[0]);
+        nsIBaseWindow baseWindow;
+        rc = webBrowser.QueryInterface (&nsIBaseWindow.IID, cast(void**)&baseWindow);
         if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-        if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
+        if (baseWindow is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
         webBrowser.Release ();
 
-        nsIBaseWindow baseWindow = new nsIBaseWindow (result[0]);
-        result[0] = 0;
-        int /*long*/[] aParentNativeWindow = new int /*long*/[1];
-        rc = baseWindow.GetParentNativeWindow (aParentNativeWindow);
+        //nsIBaseWindow baseWindow = new nsIBaseWindow (result[0]);
+        //result[0] = 0;
+        nativeWindow aParentNativeWindow;  // nativeWindow is "void*" (represents GtkWidget*)
+        rc = baseWindow.GetParentNativeWindow (&aParentNativeWindow);
         if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-        if (aParentNativeWindow[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
+        if (aParentNativeWindow is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
         baseWindow.Release ();
 
-        src = Mozilla.findBrowser (aParentNativeWindow[0]);
+        src = Mozilla.findBrowser (aParentNativeWindow);
     }
-    final Browser browser;
+    Browser browser;
     bool doit = true;
     if ((chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) !is 0) {
         /*
@@ -166,41 +135,45 @@
         */
         int style = DWT.DIALOG_TRIM;
         if ((chromeFlags & nsIWebBrowserChrome.CHROME_MODAL) !is 0) style |= DWT.APPLICATION_MODAL; 
-        final Shell shell = src is null ?
+        Shell shell = src is null ?
             new Shell (style) :
             new Shell (src.getShell(), style);
         shell.setLayout (new FillLayout ());
         browser = new Browser (shell, src is null ? DWT.MOZILLA : src.getStyle () & DWT.MOZILLA);
-        browser.addVisibilityWindowListener (new VisibilityWindowListener () {
+        browser.addVisibilityWindowListener (new class(shell) VisibilityWindowListener {
+            Shell sh;
+            this (Shell shell) { this.sh = shell; }
             public void hide (WindowEvent event) {
             }
             public void show (WindowEvent event) {
-                if (event.location !is null) shell.setLocation (event.location);
+                if (event.location !is null) sh.setLocation (event.location);
                 if (event.size !is null) {
                     Point size = event.size;
-                    shell.setSize (shell.computeSize (size.x, size.y));
+                    sh.setSize (sh.computeSize (size.x, size.y));
                 }
                 shell.open ();
             }
         });
-        browser.addCloseWindowListener (new CloseWindowListener () {
+        browser.addCloseWindowListener (new class(shell) CloseWindowListener {
+            Shell sh;
+            this (Shell shell) { this.sh = shell; }
             public void close (WindowEvent event) {
-                shell.close ();
+                sh.close ();
             }
         });
-        if (uri !is 0) {
-            nsIURI location = new nsIURI (uri);
-            int /*long*/ aSpec = XPCOM.nsEmbedCString_new ();
-            if (location.GetSpec (aSpec) is XPCOM.NS_OK) {
-                int length = XPCOM.nsEmbedCString_Length (aSpec);
-                if (length > 0) {
-                    int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec);
-                    byte[] dest = new byte[length];
-                    XPCOM.memmove (dest, buffer, length);
-                    browser.setUrl (new String (dest));
+        if (uri !is null) {
+            //nsIURI location = new nsIURI (uri);
+            scope auto aSpec = new nsEmbedCString;
+            if (uri.GetSpec (cast(nsACString*)aSpec) is XPCOM.NS_OK) {
+                int span = aSpec.toString().length;
+                if (span > 0) {
+                    //int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec);
+                    // byte[] dest = new byte[length];
+                    //XPCOM.memmove (dest, buffer, length);
+                    browser.setUrl (aSpec.toString);
                 }
             }
-            XPCOM.nsEmbedCString_delete (aSpec);
+            //XPCOM.nsEmbedCString_delete (aSpec);
         }
     } else {
         WindowEvent event = new WindowEvent (src);
@@ -216,21 +189,31 @@
         doit = browser !is null && !browser.isDisposed ();
         if (doit) {
             String platform = Platform.PLATFORM;
-            bool isMozillaNativePlatform = platform.equals ("gtk") || platform.equals ("motif"); //$NON-NLS-1$ //$NON-NLS-2$
+            bool isMozillaNativePlatform = platform == "gtk" || platform == "motif"; //$NON-NLS-1$ //$NON-NLS-2$
             doit = isMozillaNativePlatform || (browser.getStyle () & DWT.MOZILLA) !is 0;
         }
     }
     if (doit) {
+        // STRANGE but TRUE:  browser.webBrowser is always instantiated as Mozilla (on this platform),
+        // so it can be cast back to the subclass Mozilla safely.  Looks very dangerous, though... 
+        // considering the next few lines of code that cast the Mozilla class to the interface, 
+        // nsIWebBrowserChrome.
+        // This is an ugly D conversion hack because interfaces are implemented differently than 
+        // in the Java SWT version.  Watch this code section carefully for errors/bugs. -JJR
         Mozilla mozilla = cast(Mozilla)browser.webBrowser;
         mozilla.isChild = true;
-        int /*long*/ chromePtr = mozilla.webBrowserChrome.getAddress ();
-        nsIWebBrowserChrome webBrowserChrome = new nsIWebBrowserChrome (chromePtr);
-        webBrowserChrome.SetChromeFlags (chromeFlags);
-        webBrowserChrome.AddRef ();
-        XPCOM.memmove (_retval, new int /*long*/[] {chromePtr}, C.PTR_SIZEOF);
+        // And since Mozilla class implements the nsIWebBrowserChrome interface....
+        nsIWebBrowserChrome chrome;
+        nsresult rc = mozilla.QueryInterface( &nsIWebBrowserChrome.IID, cast(void**)&chrome);
+        if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
+        //nsIWebBrowserChrome webBrowserChrome = new nsIWebBrowserChrome (chromePtr);
+        chrome.SetChromeFlags (chromeFlags);
+        //chrome.AddRef ();
+        //XPCOM.memmove (_retval, new int /*long*/[] {chromePtr}, C.PTR_SIZEOF);
+        *_retval = chrome;
     } else {
-        if (cancel !is 0) {
-            C.memmove (cancel, new int[] {1}, 4);   /* PRBool */
+        if (cancel !is null) {
+            *cancel = 1;   /* PRBool */
         }
     }
     return doit ? XPCOM.NS_OK : XPCOM.NS_ERROR_NOT_IMPLEMENTED;