KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > WindowCreator2


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.browser;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.internal.*;
16 import org.eclipse.swt.internal.mozilla.*;
17 import org.eclipse.swt.layout.*;
18 import org.eclipse.swt.widgets.*;
19
20 class WindowCreator2 {
21     XPCOMObject supports;
22     XPCOMObject windowCreator;
23     XPCOMObject windowCreator2;
24     int refCount = 0;
25
26 public WindowCreator2 () {
27     createCOMInterfaces ();
28 }
29
30 int AddRef () {
31     refCount++;
32     return refCount;
33 }
34
35 void createCOMInterfaces () {
36     /* Create each of the interfaces that this object implements */
37     supports = new XPCOMObject (new int[] {2, 0, 0}) {
38         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
39         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
40         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
41     };
42
43     windowCreator = new XPCOMObject (new int[] {2, 0, 0, 3}) {
44         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
45         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
46         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
47         public int /*long*/ method3 (int /*long*/[] args) {return CreateChromeWindow (args[0], args[1], args[2]);}
48     };
49
50     windowCreator2 = new XPCOMObject (new int[] {2, 0, 0, 3, 6}) {
51         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
52         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
53         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
54         public int /*long*/ method3 (int /*long*/[] args) {return CreateChromeWindow (args[0], args[1], args[2]);}
55         public int /*long*/ method4 (int /*long*/[] args) {return CreateChromeWindow2 (args[0], args[1], args[2], args[3], args[4], args[5]);}
56     };
57 }
58
59 void disposeCOMInterfaces () {
60     if (supports != null) {
61         supports.dispose ();
62         supports = null;
63     }
64     if (windowCreator != null) {
65         windowCreator.dispose ();
66         windowCreator = null;
67     }
68
69     if (windowCreator2 != null) {
70         windowCreator2.dispose ();
71         windowCreator2 = null;
72     }
73 }
74
75 int /*long*/ getAddress () {
76     return windowCreator.getAddress ();
77 }
78
79 int /*long*/ QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
80     if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE;
81     nsID guid = new nsID ();
82     XPCOM.memmove (guid, riid, nsID.sizeof);
83     
84     if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
85         XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
86         AddRef ();
87         return XPCOM.NS_OK;
88     }
89     if (guid.Equals (nsIWindowCreator.NS_IWINDOWCREATOR_IID)) {
90         XPCOM.memmove (ppvObject, new int /*long*/[] {windowCreator.getAddress ()}, C.PTR_SIZEOF);
91         AddRef ();
92         return XPCOM.NS_OK;
93     }
94     if (guid.Equals (nsIWindowCreator2.NS_IWINDOWCREATOR2_IID)) {
95         XPCOM.memmove (ppvObject, new int /*long*/[] {windowCreator2.getAddress ()}, C.PTR_SIZEOF);
96         AddRef ();
97         return XPCOM.NS_OK;
98     }
99
100     XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
101     return XPCOM.NS_ERROR_NO_INTERFACE;
102 }
103             
104 int Release () {
105     refCount--;
106     if (refCount == 0) disposeCOMInterfaces ();
107     return refCount;
108 }
109     
110 /* nsIWindowCreator */
111
112 int /*long*/ CreateChromeWindow (int /*long*/ parent, int /*long*/ chromeFlags, int /*long*/ _retval) {
113     return CreateChromeWindow2 (parent, chromeFlags, 0, 0, 0, _retval);
114 }
115
116 /* nsIWindowCreator2 */
117
118 int /*long*/ CreateChromeWindow2 (int /*long*/ parent, int /*long*/ chromeFlags, int /*long*/ contextFlags, int /*long*/ uri, int /*long*/ cancel, int /*long*/ _retval) {
119     if (parent == 0 && (chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) == 0) {
120         return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
121     }
122     Browser src = null;
123     if (parent != 0) {
124         nsIWebBrowserChrome browserChromeParent = new nsIWebBrowserChrome (parent);
125         int /*long*/[] aWebBrowser = new int /*long*/[1];
126         int rc = browserChromeParent.GetWebBrowser (aWebBrowser);
127         if (rc != XPCOM.NS_OK) Mozilla.error (rc);
128         if (aWebBrowser[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
129
130         nsIWebBrowser webBrowser = new nsIWebBrowser (aWebBrowser[0]);
131         int /*long*/[] result = new int /*long*/[1];
132         rc = webBrowser.QueryInterface (nsIBaseWindow.NS_IBASEWINDOW_IID, result);
133         if (rc != XPCOM.NS_OK) Mozilla.error (rc);
134         if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
135         webBrowser.Release ();
136
137         nsIBaseWindow baseWindow = new nsIBaseWindow (result[0]);
138         result[0] = 0;
139         int /*long*/[] aParentNativeWindow = new int /*long*/[1];
140         rc = baseWindow.GetParentNativeWindow (aParentNativeWindow);
141         if (rc != XPCOM.NS_OK) Mozilla.error (rc);
142         if (aParentNativeWindow[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
143         baseWindow.Release ();
144
145         src = Mozilla.findBrowser (aParentNativeWindow[0]);
146     }
147     final Browser browser;
148     boolean doit = true;
149     if ((chromeFlags & nsIWebBrowserChrome.CHROME_OPENAS_CHROME) != 0) {
150         /*
151         * Mozilla will request a new Browser in a modal window in order to emulate a native
152         * dialog that is not available to it (eg.- a print dialog on Linux). For this
153         * reason modal requests are handled here so that the user is not exposed to them.
154         */

155         int style = SWT.DIALOG_TRIM;
156         if ((chromeFlags & nsIWebBrowserChrome.CHROME_MODAL) != 0) style |= SWT.APPLICATION_MODAL;
157         final Shell shell = src == null ?
158             new Shell (style) :
159             new Shell (src.getShell(), style);
160         shell.setLayout (new FillLayout ());
161         browser = new Browser (shell, src == null ? SWT.MOZILLA : src.getStyle () & SWT.MOZILLA);
162         browser.addVisibilityWindowListener (new VisibilityWindowListener () {
163             public void hide (WindowEvent event) {
164             }
165             public void show (WindowEvent event) {
166                 if (event.location != null) shell.setLocation (event.location);
167                 if (event.size != null) {
168                     Point size = event.size;
169                     shell.setSize (shell.computeSize (size.x, size.y));
170                 }
171                 shell.open ();
172             }
173         });
174         browser.addCloseWindowListener (new CloseWindowListener () {
175             public void close (WindowEvent event) {
176                 shell.close ();
177             }
178         });
179         if (uri != 0) {
180             nsIURI location = new nsIURI (uri);
181             int /*long*/ aSpec = XPCOM.nsEmbedCString_new ();
182             if (location.GetSpec (aSpec) == XPCOM.NS_OK) {
183                 int length = XPCOM.nsEmbedCString_Length (aSpec);
184                 if (length > 0) {
185                     int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec);
186                     byte[] dest = new byte[length];
187                     XPCOM.memmove (dest, buffer, length);
188                     browser.setUrl (new String JavaDoc (dest));
189                 }
190             }
191             XPCOM.nsEmbedCString_delete (aSpec);
192         }
193     } else {
194         WindowEvent event = new WindowEvent (src);
195         event.display = src.getDisplay ();
196         event.widget = src;
197         event.required = true;
198         for (int i = 0; i < src.webBrowser.openWindowListeners.length; i++) {
199             src.webBrowser.openWindowListeners[i].open (event);
200         }
201         browser = event.browser;
202
203         /* Ensure that the Browser provided by the client is valid for use */
204         doit = browser != null && !browser.isDisposed ();
205         if (doit) {
206             String JavaDoc platform = Platform.PLATFORM;
207             boolean isMozillaNativePlatform = platform.equals ("gtk") || platform.equals ("motif"); //$NON-NLS-1$ //$NON-NLS-2$
208
doit = isMozillaNativePlatform || (browser.getStyle () & SWT.MOZILLA) != 0;
209         }
210     }
211     if (doit) {
212         Mozilla mozilla = (Mozilla)browser.webBrowser;
213         mozilla.isChild = true;
214         int /*long*/ chromePtr = mozilla.webBrowserChrome.getAddress ();
215         nsIWebBrowserChrome webBrowserChrome = new nsIWebBrowserChrome (chromePtr);
216         webBrowserChrome.SetChromeFlags ((int)/*64*/chromeFlags);
217         webBrowserChrome.AddRef ();
218         XPCOM.memmove (_retval, new int /*long*/[] {chromePtr}, C.PTR_SIZEOF);
219     } else {
220         if (cancel != 0) {
221             C.memmove (cancel, new int[] {1}, 4); /* PRBool */
222         }
223     }
224     return doit ? XPCOM.NS_OK : XPCOM.NS_ERROR_NOT_IMPLEMENTED;
225 }
226 }
227
Popular Tags