|
1 /******************************************************************************* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
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 * Nokia Corporation - QT implementation |
|
10 *******************************************************************************/ |
|
11 package org.eclipse.swt.browser; |
|
12 |
|
13 import org.eclipse.swt.SWT; |
|
14 import org.eclipse.swt.internal.qt.OS; |
|
15 import org.eclipse.swt.widgets.Composite; |
|
16 import org.eclipse.swt.widgets.Display; |
|
17 import org.eclipse.swt.widgets.Event; |
|
18 import org.eclipse.swt.widgets.Internal_PackageSupport; |
|
19 import org.eclipse.swt.widgets.Listener; |
|
20 |
|
21 class QtWebKit extends WebBrowser { |
|
22 |
|
23 private static final String ADD_WIDGET_KEY = "org.eclipse.swt.internal.addWidget"; |
|
24 private static final String REMOVE_WIDGET_KEY = "org.eclipse.swt.internal.removeWidget"; |
|
25 private static final String SET_CANVAS_STATE_KEY = "org.eclipse.swt.internal.setCanvasState"; |
|
26 private static final String SET_NO_TRAVERSE_STATE_KEY = "org.eclipse.swt.internal.setNoTraverseState"; |
|
27 private static final String SET_NO_KEY_PROPAGATE_STATE_KEY = "org.eclipse.swt.internal.setNoKeyPropagateState"; |
|
28 private static final String SET_NO_MOUSE_PROPAGATE_STATE_KEY = "org.eclipse.swt.internal.setNoMousePropagateState"; |
|
29 private static final String SET_EMBEDDED_SCROLLBARS_STATE_KEY = "org.eclipse.swt.internal.setEmbeddedScrollbarsState"; |
|
30 private boolean ignoreDispose = false; |
|
31 private int webViewHandle; |
|
32 |
|
33 /** |
|
34 * @see WebBrowser |
|
35 */ |
|
36 public void create(Composite parent, int style) { |
|
37 |
|
38 // At this point, browser.handle is the view port of QScrollArea, set by Composite |
|
39 int scrollAreaHandle = Internal_PackageSupport.topHandle(browser); |
|
40 int viewPortHandle = Internal_PackageSupport.handle(browser); |
|
41 webViewHandle = OS.QWebView_new(); |
|
42 OS.QWidget_setParent(webViewHandle, scrollAreaHandle); |
|
43 |
|
44 int layout = OS.QVBoxLayout_new(0); |
|
45 OS.QLayout_setContentsMargins(layout, 0, 0, 0, 0); |
|
46 OS.QLayout_setSpacing(layout, 0); |
|
47 OS.QLayout_addWidget(layout, webViewHandle); |
|
48 OS.QWidget_setLayout(scrollAreaHandle, layout); |
|
49 OS.QWidget_setVisible(webViewHandle, true); |
|
50 OS.QLayout_activate(layout); |
|
51 OS.QLayout_update(layout); |
|
52 |
|
53 Display display = browser.getDisplay(); |
|
54 display.setData(ADD_WIDGET_KEY, new Object[] {new Integer(webViewHandle), browser}); |
|
55 Internal_PackageSupport.setHandle(browser, webViewHandle); |
|
56 |
|
57 // Just in case focus ends up in the browser "internal" parents |
|
58 OS.QWidget_setFocusProxy(scrollAreaHandle, webViewHandle); |
|
59 OS.QWidget_setFocusProxy(viewPortHandle, webViewHandle); |
|
60 |
|
61 Listener listener = new Listener() { |
|
62 public void handleEvent(Event e) { |
|
63 switch (e.type) { |
|
64 case SWT.Dispose: { |
|
65 if (ignoreDispose) { |
|
66 ignoreDispose = false; |
|
67 break; |
|
68 } |
|
69 ignoreDispose = true; |
|
70 browser.notifyListeners (e.type, e); |
|
71 e.type = SWT.NONE; |
|
72 e.display.setData(REMOVE_WIDGET_KEY, new Object[] {new Integer(webViewHandle)}); |
|
73 break; |
|
74 } |
|
75 } |
|
76 } |
|
77 }; |
|
78 browser.addListener(SWT.Dispose, listener); |
|
79 |
|
80 browser.setData(SET_CANVAS_STATE_KEY, new Boolean(false)); |
|
81 browser.setData(SET_NO_TRAVERSE_STATE_KEY, new Boolean(true)); |
|
82 browser.setData(SET_NO_KEY_PROPAGATE_STATE_KEY, new Boolean(true)); |
|
83 browser.setData(SET_NO_MOUSE_PROPAGATE_STATE_KEY, new Boolean(true)); |
|
84 browser.setData(SET_EMBEDDED_SCROLLBARS_STATE_KEY, new Boolean(true)); |
|
85 |
|
86 int handler1 = OS.SignalHandler_new(webViewHandle, this, OS.QSIGNAL_WEBVIEW_LOADFINISHED); |
|
87 OS.QObject_connectOrThrow(webViewHandle, "loadFinished(bool)", |
|
88 handler1, "widgetSignal(bool)", OS.QT_AUTOCONNECTION); |
|
89 int handler2 = OS.SignalHandler_new(webViewHandle, this, OS.QSIGNAL_WEBVIEW_LOADPROGRESS); |
|
90 OS.QObject_connectOrThrow(webViewHandle, "loadProgress(int)", |
|
91 handler2, "widgetSignal(int)", OS.QT_AUTOCONNECTION); |
|
92 int handler3 = OS.SignalHandler_new(webViewHandle, this, OS.QSIGNAL_WEBVIEW_LOADSTARTED); |
|
93 OS.QObject_connectOrThrow(webViewHandle, "loadStarted()", |
|
94 handler3, "widgetSignal()", OS.QT_AUTOCONNECTION); |
|
95 int handler4 = OS.SignalHandler_new(webViewHandle, this, OS.QSIGNAL_WEBVIEW_STATUSBARMESSAGE); |
|
96 OS.QObject_connectOrThrow(webViewHandle, "statusBarMessage(const QString&)", |
|
97 handler4, "widgetSignal(const QString&)", OS.QT_AUTOCONNECTION); |
|
98 int handler5 = OS.SignalHandler_new(webViewHandle, this, OS.QSIGNAL_WEBVIEW_TITLECHANGED); |
|
99 OS.QObject_connectOrThrow(webViewHandle, "titleChanged(const QString&)", |
|
100 handler5, "widgetSignal(const QString&)", OS.QT_AUTOCONNECTION); |
|
101 int handler6 = OS.SignalHandler_new(webViewHandle, this, OS.QSIGNAL_WEBVIEW_URLCHANGED); |
|
102 OS.QObject_connectOrThrow(webViewHandle, "urlChanged(const QUrl&)", |
|
103 handler6, "widgetSignal(const QUrl&)", OS.QT_AUTOCONNECTION); |
|
104 int handler7 = OS.SignalHandler_new(webViewHandle, this, OS.QSIGNAL_WEBVIEW_LINKCLICKED); |
|
105 OS.QObject_connectOrThrow(webViewHandle, "linkClicked(const QUrl&)", |
|
106 handler7, "widgetSignal(const QUrl&)", OS.QT_AUTOCONNECTION); |
|
107 } |
|
108 |
|
109 /** |
|
110 * @see WebBrowser |
|
111 */ |
|
112 public boolean back() { |
|
113 if (OS.QWebView_swt_canGoBack(webViewHandle)) { |
|
114 if (sendLocationEventChanging((int)System.currentTimeMillis(), |
|
115 OS.QWebView_swt_backUrl(webViewHandle))) { |
|
116 OS.QWebView_back(webViewHandle); |
|
117 return true; |
|
118 } |
|
119 } |
|
120 return false; |
|
121 } |
|
122 |
|
123 /** |
|
124 * @see WebBrowser |
|
125 */ |
|
126 public boolean execute(String script) { |
|
127 return OS.QWebView_swt_evaluateJavaScript(webViewHandle, script); |
|
128 } |
|
129 |
|
130 /** |
|
131 * @see WebBrowser |
|
132 */ |
|
133 public boolean forward() { |
|
134 if (OS.QWebView_swt_canGoForward(webViewHandle)) { |
|
135 if (sendLocationEventChanging((int)System.currentTimeMillis(), |
|
136 OS.QWebView_swt_forwardUrl(webViewHandle))) { |
|
137 OS.QWebView_forward(webViewHandle); |
|
138 return true; |
|
139 } |
|
140 } |
|
141 return false; |
|
142 } |
|
143 |
|
144 /** |
|
145 * @see WebBrowser |
|
146 */ |
|
147 public String getBrowserType() { |
|
148 return "qtwebkit"; |
|
149 } |
|
150 |
|
151 /** |
|
152 * @see WebBrowser |
|
153 */ |
|
154 public String getText() { // Not eSWT API |
|
155 return null; |
|
156 } |
|
157 |
|
158 /** |
|
159 * @see WebBrowser |
|
160 */ |
|
161 public String getUrl() { |
|
162 return OS.QWebView_url(webViewHandle); |
|
163 } |
|
164 |
|
165 /** |
|
166 * @see WebBrowser |
|
167 */ |
|
168 public boolean isBackEnabled() { |
|
169 return OS.QWebView_swt_canGoBack(webViewHandle); |
|
170 } |
|
171 |
|
172 /** |
|
173 * @see WebBrowser |
|
174 */ |
|
175 public boolean isForwardEnabled() { |
|
176 return OS.QWebView_swt_canGoForward(webViewHandle); |
|
177 } |
|
178 |
|
179 /** |
|
180 * @see WebBrowser |
|
181 */ |
|
182 public void refresh() { |
|
183 if (sendLocationEventChanging((int)System.currentTimeMillis(), |
|
184 OS.QWebView_url(webViewHandle))) { |
|
185 OS.QWebView_reload(webViewHandle); |
|
186 } |
|
187 } |
|
188 |
|
189 /** |
|
190 * @see WebBrowser |
|
191 */ |
|
192 public boolean setText(String html) { |
|
193 if (sendLocationEventChanging((int)System.currentTimeMillis(), "about:blank")) { |
|
194 OS.QWebView_setHtml(webViewHandle, html); |
|
195 return true; |
|
196 } |
|
197 else { |
|
198 return false; |
|
199 } |
|
200 } |
|
201 |
|
202 /** |
|
203 * @see WebBrowser |
|
204 */ |
|
205 public boolean setUrl(String url) { |
|
206 if (sendLocationEventChanging((int)System.currentTimeMillis(), url)) { |
|
207 OS.QWebView_setUrl(webViewHandle, url); |
|
208 return true; |
|
209 } |
|
210 else { |
|
211 return false; |
|
212 } |
|
213 } |
|
214 |
|
215 /** |
|
216 * @see WebBrowser |
|
217 */ |
|
218 public void stop() { |
|
219 OS.QWebView_stop(webViewHandle); |
|
220 } |
|
221 |
|
222 final boolean eventProcess( int widgetHandle, int eventType, int time, |
|
223 int arg1, int arg2, int arg3, int arg4, int arg5, String arg6 ){ |
|
224 if (browser.isDisposed()) { |
|
225 return false; |
|
226 } |
|
227 |
|
228 if (widgetHandle != webViewHandle) { |
|
229 return false; |
|
230 } |
|
231 |
|
232 switch (eventType) { |
|
233 case OS.QSIGNAL_WEBVIEW_LOADFINISHED: |
|
234 sendProgressEventCompleted(time); |
|
235 break; |
|
236 case OS.QSIGNAL_WEBVIEW_LOADPROGRESS: |
|
237 sendProgressEventChanged(time, arg1); |
|
238 break; |
|
239 case OS.QSIGNAL_WEBVIEW_LOADSTARTED: |
|
240 sendProgressEventChanged(time, 0); |
|
241 break; |
|
242 case OS.QSIGNAL_WEBVIEW_LINKCLICKED: |
|
243 sendLocationEventChanging(time, arg6); |
|
244 break; |
|
245 case OS.QSIGNAL_WEBVIEW_STATUSBARMESSAGE: |
|
246 sendStatusTextEventChanged(time, arg6); |
|
247 break; |
|
248 case OS.QSIGNAL_WEBVIEW_TITLECHANGED: |
|
249 sendTitleEventChanged(time, arg6); |
|
250 break; |
|
251 case OS.QSIGNAL_WEBVIEW_URLCHANGED: |
|
252 sendLocationEventChanged(time, arg6); |
|
253 break; |
|
254 default: |
|
255 break; |
|
256 } |
|
257 |
|
258 return false; |
|
259 } |
|
260 |
|
261 private void sendProgressEventChanged(int time, int current) { |
|
262 ProgressEvent e = new ProgressEvent(browser); |
|
263 e.display = browser.getDisplay(); |
|
264 e.widget = browser; |
|
265 e.time = time; |
|
266 e.current = current; // % |
|
267 e.total = 100; // % |
|
268 for (int i = 0; i < progressListeners.length; i++) { |
|
269 progressListeners[i].changed(e); |
|
270 } |
|
271 } |
|
272 |
|
273 private void sendProgressEventCompleted(int time) { |
|
274 ProgressEvent e = new ProgressEvent(browser); |
|
275 e.display = browser.getDisplay(); |
|
276 e.widget = browser; |
|
277 e.time = time; |
|
278 e.current = 100; // % |
|
279 e.total = 100; // % |
|
280 for (int i = 0; i < progressListeners.length; i++) { |
|
281 progressListeners[i].completed(e); |
|
282 } |
|
283 } |
|
284 |
|
285 private void sendStatusTextEventChanged(int time, String text) { |
|
286 StatusTextEvent e = new StatusTextEvent(browser); |
|
287 e.display = browser.getDisplay(); |
|
288 e.widget = browser; |
|
289 e.time = time; |
|
290 e.text = text; |
|
291 for (int i = 0; i < statusTextListeners.length; i++) { |
|
292 statusTextListeners[i].changed(e); |
|
293 } |
|
294 } |
|
295 |
|
296 private void sendTitleEventChanged(int time, String text) { |
|
297 TitleEvent e = new TitleEvent(browser); |
|
298 e.display = browser.getDisplay(); |
|
299 e.widget = browser; |
|
300 e.time = time; |
|
301 e.title = text; |
|
302 for (int i = 0; i < titleListeners.length; i++) { |
|
303 titleListeners[i].changed(e); |
|
304 } |
|
305 } |
|
306 |
|
307 private void sendLocationEventChanged(int time, String text) { |
|
308 LocationEvent e = new LocationEvent(browser); |
|
309 e.display = browser.getDisplay(); |
|
310 e.widget = browser; |
|
311 e.time = time; |
|
312 e.location = text; |
|
313 e.doit = true; |
|
314 e.top = true; |
|
315 for (int i = 0; i < locationListeners.length; i++) { |
|
316 locationListeners[i].changed(e); |
|
317 } |
|
318 } |
|
319 |
|
320 private boolean sendLocationEventChanging(int time, String text) { |
|
321 LocationEvent e = new LocationEvent(browser); |
|
322 e.display = browser.getDisplay(); |
|
323 e.widget = browser; |
|
324 e.time = time; |
|
325 e.location = text; |
|
326 e.doit = true; |
|
327 boolean globalDoIt = true; |
|
328 for (int i = 0; i < locationListeners.length; i++) { |
|
329 locationListeners[i].changing(e); |
|
330 globalDoIt &= e.doit; |
|
331 } |
|
332 return globalDoIt; |
|
333 } |
|
334 } |