comparison 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
comparison
equal deleted inserted replaced
124:540fa4e9974a 125:5583f8eeee6c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2003, 2007 IBM Corporation and others. 2 * Copyright (c) 2003, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * John Reimer <terminal.node@gmail.com>
10 *******************************************************************************/ 12 *******************************************************************************/
11 module dwt.browser.WindowCreator2; 13 module dwt.browser.WindowCreator2;
12 14
13 import dwt.dwthelper.utils; 15 import dwt.dwthelper.utils;
14 16
15 import dwt.DWT; 17 import dwt.DWT;
16 import dwt.graphics.Point; 18 import dwt.graphics.Point;
17 import dwt.internal.C; 19
18 import dwt.internal.Platform; 20 import dwt.internal.Platform;
19 import dwt.internal.mozilla.XPCOM; 21 import dwt.internal.mozilla.Common;
20 import dwt.internal.mozilla.XPCOMObject; 22
23 import XPCOM = dwt.internal.mozilla.XPCOM;
24
21 import dwt.internal.mozilla.nsIBaseWindow; 25 import dwt.internal.mozilla.nsIBaseWindow;
22 import dwt.internal.mozilla.nsID; 26 import dwt.internal.mozilla.nsID;
23 import dwt.internal.mozilla.nsISupports; 27 import dwt.internal.mozilla.nsISupports;
24 import dwt.internal.mozilla.nsIURI; 28 import dwt.internal.mozilla.nsIURI;
25 import dwt.internal.mozilla.nsIWebBrowser; 29 import dwt.internal.mozilla.nsIWebBrowser;
26 import dwt.internal.mozilla.nsIWebBrowserChrome; 30 import dwt.internal.mozilla.nsIWebBrowserChrome;
27 import dwt.internal.mozilla.nsIWindowCreator; 31 import dwt.internal.mozilla.nsIWindowCreator;
28 import dwt.internal.mozilla.nsIWindowCreator2; 32 import dwt.internal.mozilla.nsIWindowCreator2;
33 import dwt.internal.mozilla.nsStringAPI;
34 import dwt.internal.mozilla.nsEmbedString;
35
29 import dwt.layout.FillLayout; 36 import dwt.layout.FillLayout;
30 import dwt.widgets.Shell; 37 import dwt.widgets.Shell;
31 38
32 class WindowCreator2 { 39 import dwt.browser.Browser;
33 XPCOMObject supports; 40 import dwt.browser.Mozilla;
34 XPCOMObject windowCreator; 41 import dwt.browser.VisibilityWindowListener;
35 XPCOMObject windowCreator2; 42 import dwt.browser.CloseWindowListener;
43 import dwt.browser.WindowEvent;
44
45 class WindowCreator2 : nsIWindowCreator2 {
36 int refCount = 0; 46 int refCount = 0;
37 47
38 this () { 48 this () {}
39 createCOMInterfaces (); 49
40 } 50 extern(System)
41 51 nsrefcnt AddRef () {
42 int AddRef () {
43 refCount++; 52 refCount++;
44 return refCount; 53 return refCount;
45 } 54 }
46 55
47 void createCOMInterfaces () { 56 extern(System)
48 /* Create each of the interfaces that this object implements */ 57 nsresult QueryInterface (nsID* riid, void** ppvObject) {
49 supports = new XPCOMObject (new int[] {2, 0, 0}) { 58 if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
50 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} 59 //nsID guid = new nsID ();
51 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} 60 //XPCOM.memmove (guid, riid, nsID.sizeof);
52 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
53 };
54
55 windowCreator = new XPCOMObject (new int[] {2, 0, 0, 3}) {
56 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
57 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
58 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
59 public int /*long*/ method3 (int /*long*/[] args) {return CreateChromeWindow (args[0], cast(int)/*64*/args[1], args[2]);}
60 };
61
62 windowCreator2 = new XPCOMObject (new int[] {2, 0, 0, 3, 6}) {
63 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
64 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
65 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
66 public int /*long*/ method3 (int /*long*/[] args) {return CreateChromeWindow (args[0], cast(int)/*64*/args[1], args[2]);}
67 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]);}
68 };
69 }
70
71 void disposeCOMInterfaces () {
72 if (supports !is null) {
73 supports.dispose ();
74 supports = null;
75 }
76 if (windowCreator !is null) {
77 windowCreator.dispose ();
78 windowCreator = null;
79 }
80
81 if (windowCreator2 !is null) {
82 windowCreator2.dispose ();
83 windowCreator2 = null;
84 }
85 }
86
87 int /*long*/ getAddress () {
88 return windowCreator.getAddress ();
89 }
90
91 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
92 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
93 nsID guid = new nsID ();
94 XPCOM.memmove (guid, riid, nsID.sizeof);
95 61
96 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) { 62 if (*riid == nsISupports.IID) {
97 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF); 63 *ppvObject = cast(void*)cast(nsISupports)this;
98 AddRef (); 64 AddRef ();
99 return XPCOM.NS_OK; 65 return XPCOM.NS_OK;
100 } 66 }
101 if (guid.Equals (nsIWindowCreator.NS_IWINDOWCREATOR_IID)) { 67 if (*riid == nsIWindowCreator.IID) {
102 XPCOM.memmove (ppvObject, new int /*long*/[] {windowCreator.getAddress ()}, C.PTR_SIZEOF); 68 *ppvObject = cast(void*)cast(nsIWindowCreator)this;
103 AddRef (); 69 AddRef ();
104 return XPCOM.NS_OK; 70 return XPCOM.NS_OK;
105 } 71 }
106 if (guid.Equals (nsIWindowCreator2.NS_IWINDOWCREATOR2_IID)) { 72 if (*riid == nsIWindowCreator2.IID) {
107 XPCOM.memmove (ppvObject, new int /*long*/[] {windowCreator2.getAddress ()}, C.PTR_SIZEOF); 73 *ppvObject = cast(void*)cast(nsIWindowCreator2)this;
108 AddRef (); 74 AddRef ();
109 return XPCOM.NS_OK; 75 return XPCOM.NS_OK;
110 } 76 }
111 77
112 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF); 78 *ppvObject = null;
113 return XPCOM.NS_ERROR_NO_INTERFACE; 79 return XPCOM.NS_ERROR_NO_INTERFACE;
114 } 80 }
115 81
116 int Release () { 82 extern(System)
83 nsresult Release () {
117 refCount--; 84 refCount--;
118 if (refCount is 0) disposeCOMInterfaces (); 85 //if (refCount is 0) disposeCOMInterfaces ();
119 return refCount; 86 return refCount;
120 } 87 }
121 88
122 /* nsIWindowCreator */ 89 /* nsIWindowCreator */
123 90
124 int CreateChromeWindow (int /*long*/ parent, int chromeFlags, int /*long*/ _retval) { 91 extern(System)
125 return CreateChromeWindow2 (parent, chromeFlags, 0, 0, 0, _retval); 92 nsresult CreateChromeWindow (nsIWebBrowserChrome parent, PRUint32 chromeFlags, nsIWebBrowserChrome* _retval) {
93 return CreateChromeWindow2 (parent, chromeFlags, 0, null, null, _retval);
126 } 94 }
127 95
128 /* nsIWindowCreator2 */ 96 /* nsIWindowCreator2 */
129 97
130 int CreateChromeWindow2 (int /*long*/ parent, int chromeFlags, int contextFlags, int /*long*/ uri, int /*long*/ cancel, int /*long*/ _retval) { 98 extern(System)
131 if (parent is 0 && (chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) is 0) { 99 nsresult CreateChromeWindow2 (nsIWebBrowserChrome parent, PRUint32 chromeFlags, PRUint32 contextFlags, nsIURI uri, PRBool* cancel, nsIWebBrowserChrome* _retval) {
100 if (parent is null && (chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) is 0) {
132 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 101 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
133 } 102 }
134 Browser src = null; 103 Browser src = null;
135 if (parent !is 0) { 104 if (parent !is null) {
136 nsIWebBrowserChrome browserChromeParent = new nsIWebBrowserChrome (parent); 105 //nsIWebBrowserChrome browserChromeParent = new nsIWebBrowserChrome (parent);
137 int /*long*/[] aWebBrowser = new int /*long*/[1]; 106 nsIWebBrowser webBrowser;
138 int rc = browserChromeParent.GetWebBrowser (aWebBrowser); 107 int rc = parent.GetWebBrowser (&webBrowser);
139 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); 108 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
140 if (aWebBrowser[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE); 109 if (webBrowser is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
141 110
142 nsIWebBrowser webBrowser = new nsIWebBrowser (aWebBrowser[0]); 111 //nsIWebBrowser webBrowser = new nsIWebBrowser (aWebBrowser[0]);
143 int /*long*/[] result = new int /*long*/[1]; 112 nsIBaseWindow baseWindow;
144 rc = webBrowser.QueryInterface (nsIBaseWindow.NS_IBASEWINDOW_IID, result); 113 rc = webBrowser.QueryInterface (&nsIBaseWindow.IID, cast(void**)&baseWindow);
145 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); 114 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
146 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE); 115 if (baseWindow is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
147 webBrowser.Release (); 116 webBrowser.Release ();
148 117
149 nsIBaseWindow baseWindow = new nsIBaseWindow (result[0]); 118 //nsIBaseWindow baseWindow = new nsIBaseWindow (result[0]);
150 result[0] = 0; 119 //result[0] = 0;
151 int /*long*/[] aParentNativeWindow = new int /*long*/[1]; 120 nativeWindow aParentNativeWindow; // nativeWindow is "void*" (represents GtkWidget*)
152 rc = baseWindow.GetParentNativeWindow (aParentNativeWindow); 121 rc = baseWindow.GetParentNativeWindow (&aParentNativeWindow);
153 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); 122 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
154 if (aParentNativeWindow[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE); 123 if (aParentNativeWindow is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
155 baseWindow.Release (); 124 baseWindow.Release ();
156 125
157 src = Mozilla.findBrowser (aParentNativeWindow[0]); 126 src = Mozilla.findBrowser (aParentNativeWindow);
158 } 127 }
159 final Browser browser; 128 Browser browser;
160 bool doit = true; 129 bool doit = true;
161 if ((chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) !is 0) { 130 if ((chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) !is 0) {
162 /* 131 /*
163 * Mozilla will request a new Browser in a modal window in order to emulate a native 132 * Mozilla will request a new Browser in a modal window in order to emulate a native
164 * dialog that is not available to it (eg.- a print dialog on Linux). For this 133 * dialog that is not available to it (eg.- a print dialog on Linux). For this
165 * reason modal requests are handled here so that the user is not exposed to them. 134 * reason modal requests are handled here so that the user is not exposed to them.
166 */ 135 */
167 int style = DWT.DIALOG_TRIM; 136 int style = DWT.DIALOG_TRIM;
168 if ((chromeFlags & nsIWebBrowserChrome.CHROME_MODAL) !is 0) style |= DWT.APPLICATION_MODAL; 137 if ((chromeFlags & nsIWebBrowserChrome.CHROME_MODAL) !is 0) style |= DWT.APPLICATION_MODAL;
169 final Shell shell = src is null ? 138 Shell shell = src is null ?
170 new Shell (style) : 139 new Shell (style) :
171 new Shell (src.getShell(), style); 140 new Shell (src.getShell(), style);
172 shell.setLayout (new FillLayout ()); 141 shell.setLayout (new FillLayout ());
173 browser = new Browser (shell, src is null ? DWT.MOZILLA : src.getStyle () & DWT.MOZILLA); 142 browser = new Browser (shell, src is null ? DWT.MOZILLA : src.getStyle () & DWT.MOZILLA);
174 browser.addVisibilityWindowListener (new VisibilityWindowListener () { 143 browser.addVisibilityWindowListener (new class(shell) VisibilityWindowListener {
144 Shell sh;
145 this (Shell shell) { this.sh = shell; }
175 public void hide (WindowEvent event) { 146 public void hide (WindowEvent event) {
176 } 147 }
177 public void show (WindowEvent event) { 148 public void show (WindowEvent event) {
178 if (event.location !is null) shell.setLocation (event.location); 149 if (event.location !is null) sh.setLocation (event.location);
179 if (event.size !is null) { 150 if (event.size !is null) {
180 Point size = event.size; 151 Point size = event.size;
181 shell.setSize (shell.computeSize (size.x, size.y)); 152 sh.setSize (sh.computeSize (size.x, size.y));
182 } 153 }
183 shell.open (); 154 shell.open ();
184 } 155 }
185 }); 156 });
186 browser.addCloseWindowListener (new CloseWindowListener () { 157 browser.addCloseWindowListener (new class(shell) CloseWindowListener {
158 Shell sh;
159 this (Shell shell) { this.sh = shell; }
187 public void close (WindowEvent event) { 160 public void close (WindowEvent event) {
188 shell.close (); 161 sh.close ();
189 } 162 }
190 }); 163 });
191 if (uri !is 0) { 164 if (uri !is null) {
192 nsIURI location = new nsIURI (uri); 165 //nsIURI location = new nsIURI (uri);
193 int /*long*/ aSpec = XPCOM.nsEmbedCString_new (); 166 scope auto aSpec = new nsEmbedCString;
194 if (location.GetSpec (aSpec) is XPCOM.NS_OK) { 167 if (uri.GetSpec (cast(nsACString*)aSpec) is XPCOM.NS_OK) {
195 int length = XPCOM.nsEmbedCString_Length (aSpec); 168 int span = aSpec.toString().length;
196 if (length > 0) { 169 if (span > 0) {
197 int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec); 170 //int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec);
198 byte[] dest = new byte[length]; 171 // byte[] dest = new byte[length];
199 XPCOM.memmove (dest, buffer, length); 172 //XPCOM.memmove (dest, buffer, length);
200 browser.setUrl (new String (dest)); 173 browser.setUrl (aSpec.toString);
201 } 174 }
202 } 175 }
203 XPCOM.nsEmbedCString_delete (aSpec); 176 //XPCOM.nsEmbedCString_delete (aSpec);
204 } 177 }
205 } else { 178 } else {
206 WindowEvent event = new WindowEvent (src); 179 WindowEvent event = new WindowEvent (src);
207 event.display = src.getDisplay (); 180 event.display = src.getDisplay ();
208 event.widget = src; 181 event.widget = src;
214 187
215 /* Ensure that the Browser provided by the client is valid for use */ 188 /* Ensure that the Browser provided by the client is valid for use */
216 doit = browser !is null && !browser.isDisposed (); 189 doit = browser !is null && !browser.isDisposed ();
217 if (doit) { 190 if (doit) {
218 String platform = Platform.PLATFORM; 191 String platform = Platform.PLATFORM;
219 bool isMozillaNativePlatform = platform.equals ("gtk") || platform.equals ("motif"); //$NON-NLS-1$ //$NON-NLS-2$ 192 bool isMozillaNativePlatform = platform == "gtk" || platform == "motif"; //$NON-NLS-1$ //$NON-NLS-2$
220 doit = isMozillaNativePlatform || (browser.getStyle () & DWT.MOZILLA) !is 0; 193 doit = isMozillaNativePlatform || (browser.getStyle () & DWT.MOZILLA) !is 0;
221 } 194 }
222 } 195 }
223 if (doit) { 196 if (doit) {
197 // STRANGE but TRUE: browser.webBrowser is always instantiated as Mozilla (on this platform),
198 // so it can be cast back to the subclass Mozilla safely. Looks very dangerous, though...
199 // considering the next few lines of code that cast the Mozilla class to the interface,
200 // nsIWebBrowserChrome.
201 // This is an ugly D conversion hack because interfaces are implemented differently than
202 // in the Java SWT version. Watch this code section carefully for errors/bugs. -JJR
224 Mozilla mozilla = cast(Mozilla)browser.webBrowser; 203 Mozilla mozilla = cast(Mozilla)browser.webBrowser;
225 mozilla.isChild = true; 204 mozilla.isChild = true;
226 int /*long*/ chromePtr = mozilla.webBrowserChrome.getAddress (); 205 // And since Mozilla class implements the nsIWebBrowserChrome interface....
227 nsIWebBrowserChrome webBrowserChrome = new nsIWebBrowserChrome (chromePtr); 206 nsIWebBrowserChrome chrome;
228 webBrowserChrome.SetChromeFlags (chromeFlags); 207 nsresult rc = mozilla.QueryInterface( &nsIWebBrowserChrome.IID, cast(void**)&chrome);
229 webBrowserChrome.AddRef (); 208 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
230 XPCOM.memmove (_retval, new int /*long*/[] {chromePtr}, C.PTR_SIZEOF); 209 //nsIWebBrowserChrome webBrowserChrome = new nsIWebBrowserChrome (chromePtr);
210 chrome.SetChromeFlags (chromeFlags);
211 //chrome.AddRef ();
212 //XPCOM.memmove (_retval, new int /*long*/[] {chromePtr}, C.PTR_SIZEOF);
213 *_retval = chrome;
231 } else { 214 } else {
232 if (cancel !is 0) { 215 if (cancel !is null) {
233 C.memmove (cancel, new int[] {1}, 4); /* PRBool */ 216 *cancel = 1; /* PRBool */
234 } 217 }
235 } 218 }
236 return doit ? XPCOM.NS_OK : XPCOM.NS_ERROR_NOT_IMPLEMENTED; 219 return doit ? XPCOM.NS_OK : XPCOM.NS_ERROR_NOT_IMPLEMENTED;
237 } 220 }
238 } 221 }