]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/webview_webkit.cpp | |
3 | // Purpose: GTK WebKit backend for web view component | |
4 | // Author: Marianne Gagnon, Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 2010 Marianne Gagnon, 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT | |
14 | ||
15 | #include "wx/stockitem.h" | |
16 | #include "wx/gtk/webview_webkit.h" | |
17 | #include "wx/gtk/control.h" | |
18 | #include "wx/gtk/private.h" | |
19 | #include "wx/filesys.h" | |
20 | #include "wx/base64.h" | |
21 | #include <webkit/webkit.h> | |
22 | ||
23 | // ---------------------------------------------------------------------------- | |
24 | // GTK callbacks | |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
27 | extern "C" | |
28 | { | |
29 | ||
30 | static void | |
31 | wxgtk_webview_webkit_load_status(GtkWidget* widget, | |
32 | GParamSpec*, | |
33 | wxWebViewWebKit *webKitCtrl) | |
34 | { | |
35 | // We can be called from webkit_web_view_dispose() during the window | |
36 | // destruction, don't use half-destroyed object in this case. | |
37 | if ( webKitCtrl->IsBeingDeleted() ) | |
38 | return; | |
39 | ||
40 | wxString url = webKitCtrl->GetCurrentURL(); | |
41 | ||
42 | WebKitLoadStatus status; | |
43 | g_object_get(G_OBJECT(widget), "load-status", &status, NULL); | |
44 | ||
45 | wxString target; // TODO: get target (if possible) | |
46 | ||
47 | if (status == WEBKIT_LOAD_FINISHED) | |
48 | { | |
49 | WebKitWebBackForwardList* hist = webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(widget)); | |
50 | WebKitWebHistoryItem* item = webkit_web_back_forward_list_get_current_item(hist); | |
51 | //We have to check if we are actually storing history | |
52 | //If the item isn't added we add it ourselves, it isn't added otherwise | |
53 | //with a custom scheme. | |
54 | if(WEBKIT_IS_WEB_HISTORY_ITEM(item) && webkit_web_history_item_get_uri(item) != url) | |
55 | { | |
56 | WebKitWebHistoryItem* | |
57 | newitem = webkit_web_history_item_new_with_data | |
58 | ( | |
59 | url.utf8_str(), | |
60 | webKitCtrl->GetCurrentTitle().utf8_str() | |
61 | ); | |
62 | webkit_web_back_forward_list_add_item(hist, newitem); | |
63 | } | |
64 | ||
65 | webKitCtrl->m_busy = false; | |
66 | wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED, | |
67 | webKitCtrl->GetId(), | |
68 | url, target); | |
69 | ||
70 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
71 | webKitCtrl->GetEventHandler()->ProcessEvent(event); | |
72 | } | |
73 | else if (status == WEBKIT_LOAD_COMMITTED) | |
74 | { | |
75 | webKitCtrl->m_busy = true; | |
76 | wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED, | |
77 | webKitCtrl->GetId(), | |
78 | url, target); | |
79 | ||
80 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
81 | webKitCtrl->GetEventHandler()->ProcessEvent(event); | |
82 | } | |
83 | } | |
84 | ||
85 | static gboolean | |
86 | wxgtk_webview_webkit_navigation(WebKitWebView *, | |
87 | WebKitWebFrame *frame, | |
88 | WebKitNetworkRequest *request, | |
89 | WebKitWebNavigationAction *, | |
90 | WebKitWebPolicyDecision *policy_decision, | |
91 | wxWebViewWebKit *webKitCtrl) | |
92 | { | |
93 | if(webKitCtrl->m_guard) | |
94 | { | |
95 | webKitCtrl->m_guard = false; | |
96 | //We set this to make sure that we don't try to load the page again from | |
97 | //the resource request callback | |
98 | webKitCtrl->m_vfsurl = webkit_network_request_get_uri(request); | |
99 | webkit_web_policy_decision_use(policy_decision); | |
100 | return FALSE; | |
101 | } | |
102 | ||
103 | webKitCtrl->m_busy = true; | |
104 | ||
105 | const gchar* uri = webkit_network_request_get_uri(request); | |
106 | ||
107 | wxString target = webkit_web_frame_get_name (frame); | |
108 | wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING, | |
109 | webKitCtrl->GetId(), | |
110 | wxString( uri, wxConvUTF8 ), | |
111 | target); | |
112 | ||
113 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
114 | webKitCtrl->GetEventHandler()->ProcessEvent(event); | |
115 | ||
116 | if (!event.IsAllowed()) | |
117 | { | |
118 | webKitCtrl->m_busy = false; | |
119 | webkit_web_policy_decision_ignore(policy_decision); | |
120 | return TRUE; | |
121 | } | |
122 | else | |
123 | { | |
124 | wxString wxuri = uri; | |
125 | wxSharedPtr<wxWebViewHandler> handler; | |
126 | wxVector<wxSharedPtr<wxWebViewHandler> > hanlders = webKitCtrl->GetHandlers(); | |
127 | //We are not vetoed so see if we match one of the additional handlers | |
128 | for(wxVector<wxSharedPtr<wxWebViewHandler> >::iterator it = hanlders.begin(); | |
129 | it != hanlders.end(); ++it) | |
130 | { | |
131 | if(wxuri.substr(0, (*it)->GetName().length()) == (*it)->GetName()) | |
132 | { | |
133 | handler = (*it); | |
134 | } | |
135 | } | |
136 | //If we found a handler we can then use it to load the file directly | |
137 | //ourselves | |
138 | if(handler) | |
139 | { | |
140 | webKitCtrl->m_guard = true; | |
141 | wxFSFile* file = handler->GetFile(wxuri); | |
142 | if(file) | |
143 | { | |
144 | webKitCtrl->SetPage(*file->GetStream(), wxuri); | |
145 | } | |
146 | //We need to throw some sort of error here if file is NULL | |
147 | webkit_web_policy_decision_ignore(policy_decision); | |
148 | return TRUE; | |
149 | } | |
150 | return FALSE; | |
151 | } | |
152 | } | |
153 | ||
154 | static gboolean | |
155 | wxgtk_webview_webkit_error(WebKitWebView*, | |
156 | WebKitWebFrame*, | |
157 | gchar *uri, | |
158 | gpointer web_error, | |
159 | wxWebViewWebKit* webKitWindow) | |
160 | { | |
161 | webKitWindow->m_busy = false; | |
162 | wxWebViewNavigationError type = wxWEB_NAV_ERR_OTHER; | |
163 | ||
164 | GError* error = (GError*)web_error; | |
165 | wxString description(error->message, wxConvUTF8); | |
166 | ||
167 | if (strcmp(g_quark_to_string(error->domain), "soup_http_error_quark") == 0) | |
168 | { | |
169 | switch (error->code) | |
170 | { | |
171 | case SOUP_STATUS_CANCELLED: | |
172 | type = wxWEB_NAV_ERR_USER_CANCELLED; | |
173 | break; | |
174 | ||
175 | case SOUP_STATUS_CANT_RESOLVE: | |
176 | case SOUP_STATUS_NOT_FOUND: | |
177 | type = wxWEB_NAV_ERR_NOT_FOUND; | |
178 | break; | |
179 | ||
180 | case SOUP_STATUS_CANT_RESOLVE_PROXY: | |
181 | case SOUP_STATUS_CANT_CONNECT: | |
182 | case SOUP_STATUS_CANT_CONNECT_PROXY: | |
183 | case SOUP_STATUS_SSL_FAILED: | |
184 | case SOUP_STATUS_IO_ERROR: | |
185 | type = wxWEB_NAV_ERR_CONNECTION; | |
186 | break; | |
187 | ||
188 | case SOUP_STATUS_MALFORMED: | |
189 | //case SOUP_STATUS_TOO_MANY_REDIRECTS: | |
190 | type = wxWEB_NAV_ERR_REQUEST; | |
191 | break; | |
192 | ||
193 | //case SOUP_STATUS_NO_CONTENT: | |
194 | //case SOUP_STATUS_RESET_CONTENT: | |
195 | ||
196 | case SOUP_STATUS_BAD_REQUEST: | |
197 | type = wxWEB_NAV_ERR_REQUEST; | |
198 | break; | |
199 | ||
200 | case SOUP_STATUS_UNAUTHORIZED: | |
201 | case SOUP_STATUS_FORBIDDEN: | |
202 | type = wxWEB_NAV_ERR_AUTH; | |
203 | break; | |
204 | ||
205 | case SOUP_STATUS_METHOD_NOT_ALLOWED: | |
206 | case SOUP_STATUS_NOT_ACCEPTABLE: | |
207 | type = wxWEB_NAV_ERR_SECURITY; | |
208 | break; | |
209 | ||
210 | case SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED: | |
211 | type = wxWEB_NAV_ERR_AUTH; | |
212 | break; | |
213 | ||
214 | case SOUP_STATUS_REQUEST_TIMEOUT: | |
215 | type = wxWEB_NAV_ERR_CONNECTION; | |
216 | break; | |
217 | ||
218 | //case SOUP_STATUS_PAYMENT_REQUIRED: | |
219 | case SOUP_STATUS_REQUEST_ENTITY_TOO_LARGE: | |
220 | case SOUP_STATUS_REQUEST_URI_TOO_LONG: | |
221 | case SOUP_STATUS_UNSUPPORTED_MEDIA_TYPE: | |
222 | type = wxWEB_NAV_ERR_REQUEST; | |
223 | break; | |
224 | ||
225 | case SOUP_STATUS_BAD_GATEWAY: | |
226 | case SOUP_STATUS_SERVICE_UNAVAILABLE: | |
227 | case SOUP_STATUS_GATEWAY_TIMEOUT: | |
228 | type = wxWEB_NAV_ERR_CONNECTION; | |
229 | break; | |
230 | ||
231 | case SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED: | |
232 | type = wxWEB_NAV_ERR_REQUEST; | |
233 | break; | |
234 | //case SOUP_STATUS_INSUFFICIENT_STORAGE: | |
235 | //case SOUP_STATUS_NOT_EXTENDED: | |
236 | } | |
237 | } | |
238 | else if (strcmp(g_quark_to_string(error->domain), | |
239 | "webkit-network-error-quark") == 0) | |
240 | { | |
241 | switch (error->code) | |
242 | { | |
243 | //WEBKIT_NETWORK_ERROR_FAILED: | |
244 | //WEBKIT_NETWORK_ERROR_TRANSPORT: | |
245 | ||
246 | case WEBKIT_NETWORK_ERROR_UNKNOWN_PROTOCOL: | |
247 | type = wxWEB_NAV_ERR_REQUEST; | |
248 | break; | |
249 | ||
250 | case WEBKIT_NETWORK_ERROR_CANCELLED: | |
251 | type = wxWEB_NAV_ERR_USER_CANCELLED; | |
252 | break; | |
253 | ||
254 | case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST: | |
255 | type = wxWEB_NAV_ERR_NOT_FOUND; | |
256 | break; | |
257 | } | |
258 | } | |
259 | else if (strcmp(g_quark_to_string(error->domain), | |
260 | "webkit-policy-error-quark") == 0) | |
261 | { | |
262 | switch (error->code) | |
263 | { | |
264 | //case WEBKIT_POLICY_ERROR_FAILED: | |
265 | //case WEBKIT_POLICY_ERROR_CANNOT_SHOW_MIME_TYPE: | |
266 | //case WEBKIT_POLICY_ERROR_CANNOT_SHOW_URL: | |
267 | //case WEBKIT_POLICY_ERROR_FRAME_LOAD_INTERRUPTED_BY_POLICY_CHANGE: | |
268 | case WEBKIT_POLICY_ERROR_CANNOT_USE_RESTRICTED_PORT: | |
269 | type = wxWEB_NAV_ERR_SECURITY; | |
270 | break; | |
271 | } | |
272 | } | |
273 | /* | |
274 | webkit_plugin_error_quark | |
275 | else | |
276 | { | |
277 | printf("Error domain %s\n", g_quark_to_string(error->domain)); | |
278 | } | |
279 | */ | |
280 | ||
281 | wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR, | |
282 | webKitWindow->GetId(), | |
283 | uri, ""); | |
284 | event.SetString(description); | |
285 | event.SetInt(type); | |
286 | ||
287 | if (webKitWindow && webKitWindow->GetEventHandler()) | |
288 | { | |
289 | webKitWindow->GetEventHandler()->ProcessEvent(event); | |
290 | } | |
291 | ||
292 | return FALSE; | |
293 | } | |
294 | ||
295 | static gboolean | |
296 | wxgtk_webview_webkit_new_window(WebKitWebView*, | |
297 | WebKitWebFrame *frame, | |
298 | WebKitNetworkRequest *request, | |
299 | WebKitWebNavigationAction*, | |
300 | WebKitWebPolicyDecision *policy_decision, | |
301 | wxWebViewWebKit *webKitCtrl) | |
302 | { | |
303 | const gchar* uri = webkit_network_request_get_uri(request); | |
304 | ||
305 | wxString target = webkit_web_frame_get_name (frame); | |
306 | wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, | |
307 | webKitCtrl->GetId(), | |
308 | wxString( uri, wxConvUTF8 ), | |
309 | target); | |
310 | ||
311 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
312 | webKitCtrl->GetEventHandler()->ProcessEvent(event); | |
313 | ||
314 | //We always want the user to handle this themselves | |
315 | webkit_web_policy_decision_ignore(policy_decision); | |
316 | return TRUE; | |
317 | } | |
318 | ||
319 | static void | |
320 | wxgtk_webview_webkit_title_changed(WebKitWebView*, | |
321 | WebKitWebFrame*, | |
322 | gchar *title, | |
323 | wxWebViewWebKit *webKitCtrl) | |
324 | { | |
325 | wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED, | |
326 | webKitCtrl->GetId(), | |
327 | webKitCtrl->GetCurrentURL(), | |
328 | ""); | |
329 | event.SetString(wxString(title, wxConvUTF8)); | |
330 | ||
331 | if (webKitCtrl && webKitCtrl->GetEventHandler()) | |
332 | webKitCtrl->GetEventHandler()->ProcessEvent(event); | |
333 | ||
334 | } | |
335 | ||
336 | static void | |
337 | wxgtk_webview_webkit_resource_req(WebKitWebView *, | |
338 | WebKitWebFrame *, | |
339 | WebKitWebResource *, | |
340 | WebKitNetworkRequest *request, | |
341 | WebKitNetworkResponse *, | |
342 | wxWebViewWebKit *webKitCtrl) | |
343 | { | |
344 | wxString uri = webkit_network_request_get_uri(request); | |
345 | ||
346 | wxSharedPtr<wxWebViewHandler> handler; | |
347 | wxVector<wxSharedPtr<wxWebViewHandler> > hanlders = webKitCtrl->GetHandlers(); | |
348 | ||
349 | //We are not vetoed so see if we match one of the additional handlers | |
350 | for(wxVector<wxSharedPtr<wxWebViewHandler> >::iterator it = hanlders.begin(); | |
351 | it != hanlders.end(); ++it) | |
352 | { | |
353 | if(uri.substr(0, (*it)->GetName().length()) == (*it)->GetName()) | |
354 | { | |
355 | handler = (*it); | |
356 | } | |
357 | } | |
358 | //If we found a handler we can then use it to load the file directly | |
359 | //ourselves | |
360 | if(handler) | |
361 | { | |
362 | //If it is requsting the page itself then return as we have already | |
363 | //loaded it from the archive | |
364 | if(webKitCtrl->m_vfsurl == uri) | |
365 | return; | |
366 | ||
367 | wxFSFile* file = handler->GetFile(uri); | |
368 | if(file) | |
369 | { | |
370 | //We load the data into a data url to save it being written out again | |
371 | size_t size = file->GetStream()->GetLength(); | |
372 | char *buffer = new char[size]; | |
373 | file->GetStream()->Read(buffer, size); | |
374 | wxString data = wxBase64Encode(buffer, size); | |
375 | delete[] buffer; | |
376 | wxString mime = file->GetMimeType(); | |
377 | wxString path = "data:" + mime + ";base64," + data; | |
378 | //Then we can redirect the call | |
379 | webkit_network_request_set_uri(request, path.utf8_str()); | |
380 | } | |
381 | ||
382 | } | |
383 | } | |
384 | ||
385 | } // extern "C" | |
386 | ||
387 | //----------------------------------------------------------------------------- | |
388 | // wxWebViewWebKit | |
389 | //----------------------------------------------------------------------------- | |
390 | ||
391 | wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxWebView); | |
392 | ||
393 | bool wxWebViewWebKit::Create(wxWindow *parent, | |
394 | wxWindowID id, | |
395 | const wxString &url, | |
396 | const wxPoint& pos, | |
397 | const wxSize& size, | |
398 | long style, | |
399 | const wxString& name) | |
400 | { | |
401 | m_busy = false; | |
402 | m_guard = false; | |
403 | FindClear(); | |
404 | ||
405 | // We currently unconditionally impose scrolling in both directions as it's | |
406 | // necessary to show arbitrary pages. | |
407 | style |= wxHSCROLL | wxVSCROLL; | |
408 | ||
409 | if (!PreCreation( parent, pos, size ) || | |
410 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
411 | { | |
412 | wxFAIL_MSG( wxT("wxWebViewWebKit creation failed") ); | |
413 | return false; | |
414 | } | |
415 | ||
416 | m_web_view = WEBKIT_WEB_VIEW(webkit_web_view_new()); | |
417 | GTKCreateScrolledWindowWith(GTK_WIDGET(m_web_view)); | |
418 | g_object_ref(m_widget); | |
419 | ||
420 | g_signal_connect_after(m_web_view, "navigation-policy-decision-requested", | |
421 | G_CALLBACK(wxgtk_webview_webkit_navigation), | |
422 | this); | |
423 | g_signal_connect_after(m_web_view, "load-error", | |
424 | G_CALLBACK(wxgtk_webview_webkit_error), | |
425 | this); | |
426 | ||
427 | g_signal_connect_after(m_web_view, "new-window-policy-decision-requested", | |
428 | G_CALLBACK(wxgtk_webview_webkit_new_window), this); | |
429 | ||
430 | g_signal_connect_after(m_web_view, "title-changed", | |
431 | G_CALLBACK(wxgtk_webview_webkit_title_changed), this); | |
432 | ||
433 | g_signal_connect_after(m_web_view, "resource-request-starting", | |
434 | G_CALLBACK(wxgtk_webview_webkit_resource_req), this); | |
435 | ||
436 | m_parent->DoAddChild( this ); | |
437 | ||
438 | PostCreation(size); | |
439 | ||
440 | /* Open a webpage */ | |
441 | webkit_web_view_load_uri(m_web_view, url.utf8_str()); | |
442 | ||
443 | //Get the initial history limit so we can enable and disable it later | |
444 | WebKitWebBackForwardList* history; | |
445 | history = webkit_web_view_get_back_forward_list(m_web_view); | |
446 | m_historyLimit = webkit_web_back_forward_list_get_limit(history); | |
447 | ||
448 | // last to avoid getting signal too early | |
449 | g_signal_connect_after(m_web_view, "notify::load-status", | |
450 | G_CALLBACK(wxgtk_webview_webkit_load_status), | |
451 | this); | |
452 | ||
453 | return true; | |
454 | } | |
455 | ||
456 | wxWebViewWebKit::~wxWebViewWebKit() | |
457 | { | |
458 | // The main goal here is to set m_isBeingDeleted to true to avoid the use | |
459 | // of this -- already half-destroyed -- object from WebKit callbacks, but | |
460 | // just setting it would prevent wxWindowDestroyEvent from being sent, so | |
461 | // send it now instead. | |
462 | SendDestroyEvent(); | |
463 | } | |
464 | ||
465 | bool wxWebViewWebKit::Enable( bool enable ) | |
466 | { | |
467 | if (!wxControl::Enable(enable)) | |
468 | return false; | |
469 | ||
470 | gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable); | |
471 | ||
472 | //if (enable) | |
473 | // GTKFixSensitivity(); | |
474 | ||
475 | return true; | |
476 | } | |
477 | ||
478 | GdkWindow* | |
479 | wxWebViewWebKit::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
480 | { | |
481 | GdkWindow* window = gtk_widget_get_parent_window(m_widget); | |
482 | return window; | |
483 | } | |
484 | ||
485 | void wxWebViewWebKit::ZoomIn() | |
486 | { | |
487 | webkit_web_view_zoom_in(m_web_view); | |
488 | } | |
489 | ||
490 | void wxWebViewWebKit::ZoomOut() | |
491 | { | |
492 | webkit_web_view_zoom_out(m_web_view); | |
493 | } | |
494 | ||
495 | void wxWebViewWebKit::SetWebkitZoom(float level) | |
496 | { | |
497 | webkit_web_view_set_zoom_level(m_web_view, level); | |
498 | } | |
499 | ||
500 | float wxWebViewWebKit::GetWebkitZoom() const | |
501 | { | |
502 | return webkit_web_view_get_zoom_level(m_web_view); | |
503 | } | |
504 | ||
505 | void wxWebViewWebKit::Stop() | |
506 | { | |
507 | webkit_web_view_stop_loading(m_web_view); | |
508 | } | |
509 | ||
510 | void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags) | |
511 | { | |
512 | if (flags & wxWEB_VIEW_RELOAD_NO_CACHE) | |
513 | { | |
514 | webkit_web_view_reload_bypass_cache(m_web_view); | |
515 | } | |
516 | else | |
517 | { | |
518 | webkit_web_view_reload(m_web_view); | |
519 | } | |
520 | } | |
521 | ||
522 | void wxWebViewWebKit::LoadURL(const wxString& url) | |
523 | { | |
524 | webkit_web_view_load_uri(m_web_view, wxGTK_CONV(url)); | |
525 | } | |
526 | ||
527 | ||
528 | void wxWebViewWebKit::GoBack() | |
529 | { | |
530 | webkit_web_view_go_back(m_web_view); | |
531 | } | |
532 | ||
533 | void wxWebViewWebKit::GoForward() | |
534 | { | |
535 | webkit_web_view_go_forward(m_web_view); | |
536 | } | |
537 | ||
538 | ||
539 | bool wxWebViewWebKit::CanGoBack() const | |
540 | { | |
541 | return webkit_web_view_can_go_back(m_web_view); | |
542 | } | |
543 | ||
544 | ||
545 | bool wxWebViewWebKit::CanGoForward() const | |
546 | { | |
547 | return webkit_web_view_can_go_forward(m_web_view); | |
548 | } | |
549 | ||
550 | void wxWebViewWebKit::ClearHistory() | |
551 | { | |
552 | WebKitWebBackForwardList* history; | |
553 | history = webkit_web_view_get_back_forward_list(m_web_view); | |
554 | webkit_web_back_forward_list_clear(history); | |
555 | } | |
556 | ||
557 | void wxWebViewWebKit::EnableHistory(bool enable) | |
558 | { | |
559 | WebKitWebBackForwardList* history; | |
560 | history = webkit_web_view_get_back_forward_list(m_web_view); | |
561 | if(enable) | |
562 | { | |
563 | webkit_web_back_forward_list_set_limit(history, m_historyLimit); | |
564 | } | |
565 | else | |
566 | { | |
567 | webkit_web_back_forward_list_set_limit(history, 0); | |
568 | } | |
569 | } | |
570 | ||
571 | wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetBackwardHistory() | |
572 | { | |
573 | wxVector<wxSharedPtr<wxWebViewHistoryItem> > backhist; | |
574 | WebKitWebBackForwardList* history; | |
575 | history = webkit_web_view_get_back_forward_list(m_web_view); | |
576 | GList* list = webkit_web_back_forward_list_get_back_list_with_limit(history, | |
577 | m_historyLimit); | |
578 | //We need to iterate in reverse to get the order we desire | |
579 | for(int i = g_list_length(list) - 1; i >= 0 ; i--) | |
580 | { | |
581 | WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)g_list_nth_data(list, i); | |
582 | wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem( | |
583 | webkit_web_history_item_get_uri(gtkitem), | |
584 | webkit_web_history_item_get_title(gtkitem)); | |
585 | wxitem->m_histItem = gtkitem; | |
586 | wxSharedPtr<wxWebViewHistoryItem> item(wxitem); | |
587 | backhist.push_back(item); | |
588 | } | |
589 | return backhist; | |
590 | } | |
591 | ||
592 | wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetForwardHistory() | |
593 | { | |
594 | wxVector<wxSharedPtr<wxWebViewHistoryItem> > forwardhist; | |
595 | WebKitWebBackForwardList* history; | |
596 | history = webkit_web_view_get_back_forward_list(m_web_view); | |
597 | GList* list = webkit_web_back_forward_list_get_forward_list_with_limit(history, | |
598 | m_historyLimit); | |
599 | for(guint i = 0; i < g_list_length(list); i++) | |
600 | { | |
601 | WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)g_list_nth_data(list, i); | |
602 | wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem( | |
603 | webkit_web_history_item_get_uri(gtkitem), | |
604 | webkit_web_history_item_get_title(gtkitem)); | |
605 | wxitem->m_histItem = gtkitem; | |
606 | wxSharedPtr<wxWebViewHistoryItem> item(wxitem); | |
607 | forwardhist.push_back(item); | |
608 | } | |
609 | return forwardhist; | |
610 | } | |
611 | ||
612 | void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item) | |
613 | { | |
614 | WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)item->m_histItem; | |
615 | if(gtkitem) | |
616 | { | |
617 | webkit_web_view_go_to_back_forward_item(m_web_view, | |
618 | WEBKIT_WEB_HISTORY_ITEM(gtkitem)); | |
619 | } | |
620 | } | |
621 | ||
622 | bool wxWebViewWebKit::CanCut() const | |
623 | { | |
624 | return webkit_web_view_can_cut_clipboard(m_web_view); | |
625 | } | |
626 | ||
627 | bool wxWebViewWebKit::CanCopy() const | |
628 | { | |
629 | return webkit_web_view_can_copy_clipboard(m_web_view); | |
630 | } | |
631 | ||
632 | bool wxWebViewWebKit::CanPaste() const | |
633 | { | |
634 | return webkit_web_view_can_paste_clipboard(m_web_view); | |
635 | } | |
636 | ||
637 | void wxWebViewWebKit::Cut() | |
638 | { | |
639 | webkit_web_view_cut_clipboard(m_web_view); | |
640 | } | |
641 | ||
642 | void wxWebViewWebKit::Copy() | |
643 | { | |
644 | webkit_web_view_copy_clipboard(m_web_view); | |
645 | } | |
646 | ||
647 | void wxWebViewWebKit::Paste() | |
648 | { | |
649 | webkit_web_view_paste_clipboard(m_web_view); | |
650 | } | |
651 | ||
652 | bool wxWebViewWebKit::CanUndo() const | |
653 | { | |
654 | return webkit_web_view_can_undo(m_web_view); | |
655 | } | |
656 | ||
657 | bool wxWebViewWebKit::CanRedo() const | |
658 | { | |
659 | return webkit_web_view_can_redo(m_web_view); | |
660 | } | |
661 | ||
662 | void wxWebViewWebKit::Undo() | |
663 | { | |
664 | webkit_web_view_undo(m_web_view); | |
665 | } | |
666 | ||
667 | void wxWebViewWebKit::Redo() | |
668 | { | |
669 | webkit_web_view_redo(m_web_view); | |
670 | } | |
671 | ||
672 | wxString wxWebViewWebKit::GetCurrentURL() const | |
673 | { | |
674 | // FIXME: check which encoding the web kit control uses instead of | |
675 | // assuming UTF8 (here and elsewhere too) | |
676 | return wxString::FromUTF8(webkit_web_view_get_uri(m_web_view)); | |
677 | } | |
678 | ||
679 | ||
680 | wxString wxWebViewWebKit::GetCurrentTitle() const | |
681 | { | |
682 | return wxString::FromUTF8(webkit_web_view_get_title(m_web_view)); | |
683 | } | |
684 | ||
685 | ||
686 | wxString wxWebViewWebKit::GetPageSource() const | |
687 | { | |
688 | WebKitWebFrame* frame = webkit_web_view_get_main_frame(m_web_view); | |
689 | WebKitWebDataSource* src = webkit_web_frame_get_data_source (frame); | |
690 | ||
691 | // TODO: check encoding with | |
692 | // const gchar* | |
693 | // webkit_web_data_source_get_encoding(WebKitWebDataSource *data_source); | |
694 | return wxString(webkit_web_data_source_get_data (src)->str, wxConvUTF8); | |
695 | } | |
696 | ||
697 | ||
698 | wxWebViewZoom wxWebViewWebKit::GetZoom() const | |
699 | { | |
700 | float zoom = GetWebkitZoom(); | |
701 | ||
702 | // arbitrary way to map float zoom to our common zoom enum | |
703 | if (zoom <= 0.65) | |
704 | { | |
705 | return wxWEB_VIEW_ZOOM_TINY; | |
706 | } | |
707 | else if (zoom > 0.65 && zoom <= 0.90) | |
708 | { | |
709 | return wxWEB_VIEW_ZOOM_SMALL; | |
710 | } | |
711 | else if (zoom > 0.90 && zoom <= 1.15) | |
712 | { | |
713 | return wxWEB_VIEW_ZOOM_MEDIUM; | |
714 | } | |
715 | else if (zoom > 1.15 && zoom <= 1.45) | |
716 | { | |
717 | return wxWEB_VIEW_ZOOM_LARGE; | |
718 | } | |
719 | else if (zoom > 1.45) | |
720 | { | |
721 | return wxWEB_VIEW_ZOOM_LARGEST; | |
722 | } | |
723 | ||
724 | // to shut up compilers, this can never be reached logically | |
725 | wxASSERT(false); | |
726 | return wxWEB_VIEW_ZOOM_MEDIUM; | |
727 | } | |
728 | ||
729 | ||
730 | void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom) | |
731 | { | |
732 | // arbitrary way to map our common zoom enum to float zoom | |
733 | switch (zoom) | |
734 | { | |
735 | case wxWEB_VIEW_ZOOM_TINY: | |
736 | SetWebkitZoom(0.6f); | |
737 | break; | |
738 | ||
739 | case wxWEB_VIEW_ZOOM_SMALL: | |
740 | SetWebkitZoom(0.8f); | |
741 | break; | |
742 | ||
743 | case wxWEB_VIEW_ZOOM_MEDIUM: | |
744 | SetWebkitZoom(1.0f); | |
745 | break; | |
746 | ||
747 | case wxWEB_VIEW_ZOOM_LARGE: | |
748 | SetWebkitZoom(1.3); | |
749 | break; | |
750 | ||
751 | case wxWEB_VIEW_ZOOM_LARGEST: | |
752 | SetWebkitZoom(1.6); | |
753 | break; | |
754 | ||
755 | default: | |
756 | wxASSERT(false); | |
757 | } | |
758 | } | |
759 | ||
760 | void wxWebViewWebKit::SetZoomType(wxWebViewZoomType type) | |
761 | { | |
762 | webkit_web_view_set_full_content_zoom(m_web_view, | |
763 | (type == wxWEB_VIEW_ZOOM_TYPE_LAYOUT ? | |
764 | TRUE : FALSE)); | |
765 | } | |
766 | ||
767 | wxWebViewZoomType wxWebViewWebKit::GetZoomType() const | |
768 | { | |
769 | gboolean fczoom = webkit_web_view_get_full_content_zoom(m_web_view); | |
770 | ||
771 | if (fczoom) return wxWEB_VIEW_ZOOM_TYPE_LAYOUT; | |
772 | else return wxWEB_VIEW_ZOOM_TYPE_TEXT; | |
773 | } | |
774 | ||
775 | bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType) const | |
776 | { | |
777 | // this port supports all zoom types | |
778 | return true; | |
779 | } | |
780 | ||
781 | void wxWebViewWebKit::DoSetPage(const wxString& html, const wxString& baseUri) | |
782 | { | |
783 | webkit_web_view_load_string (m_web_view, | |
784 | html.mb_str(wxConvUTF8), | |
785 | "text/html", | |
786 | "UTF-8", | |
787 | baseUri.mb_str(wxConvUTF8)); | |
788 | } | |
789 | ||
790 | void wxWebViewWebKit::Print() | |
791 | { | |
792 | WebKitWebFrame* frame = webkit_web_view_get_main_frame(m_web_view); | |
793 | webkit_web_frame_print (frame); | |
794 | ||
795 | // GtkPrintOperationResult webkit_web_frame_print_full | |
796 | // (WebKitWebFrame *frame, | |
797 | // GtkPrintOperation *operation, | |
798 | // GtkPrintOperationAction action, | |
799 | // GError **error); | |
800 | ||
801 | } | |
802 | ||
803 | ||
804 | bool wxWebViewWebKit::IsBusy() const | |
805 | { | |
806 | return m_busy; | |
807 | ||
808 | // This code looks nice but returns true after a page was cancelled | |
809 | /* | |
810 | WebKitLoadStatus status = webkit_web_view_get_load_status | |
811 | (WEBKIT_WEB_VIEW(web_view)); | |
812 | ||
813 | ||
814 | #if WEBKIT_CHECK_VERSION(1,1,16) | |
815 | // WEBKIT_LOAD_FAILED is new in webkit 1.1.16 | |
816 | if (status == WEBKIT_LOAD_FAILED) | |
817 | { | |
818 | return false; | |
819 | } | |
820 | #endif | |
821 | if (status == WEBKIT_LOAD_FINISHED) | |
822 | { | |
823 | return false; | |
824 | } | |
825 | ||
826 | return true; | |
827 | */ | |
828 | } | |
829 | ||
830 | void wxWebViewWebKit::SetEditable(bool enable) | |
831 | { | |
832 | webkit_web_view_set_editable(m_web_view, enable); | |
833 | } | |
834 | ||
835 | bool wxWebViewWebKit::IsEditable() const | |
836 | { | |
837 | return webkit_web_view_get_editable(m_web_view); | |
838 | } | |
839 | ||
840 | void wxWebViewWebKit::DeleteSelection() | |
841 | { | |
842 | webkit_web_view_delete_selection(m_web_view); | |
843 | } | |
844 | ||
845 | bool wxWebViewWebKit::HasSelection() const | |
846 | { | |
847 | return webkit_web_view_has_selection(m_web_view); | |
848 | } | |
849 | ||
850 | void wxWebViewWebKit::SelectAll() | |
851 | { | |
852 | webkit_web_view_select_all(m_web_view); | |
853 | } | |
854 | ||
855 | wxString wxWebViewWebKit::GetSelectedText() const | |
856 | { | |
857 | WebKitDOMDocument* doc; | |
858 | WebKitDOMDOMWindow* win; | |
859 | WebKitDOMDOMSelection* sel; | |
860 | WebKitDOMRange* range; | |
861 | ||
862 | doc = webkit_web_view_get_dom_document(m_web_view); | |
863 | win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc)); | |
864 | sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win)); | |
865 | range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), | |
866 | 0, NULL); | |
867 | return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range)), | |
868 | wxConvUTF8); | |
869 | } | |
870 | ||
871 | wxString wxWebViewWebKit::GetSelectedSource() const | |
872 | { | |
873 | WebKitDOMDocument* doc; | |
874 | WebKitDOMDOMWindow* win; | |
875 | WebKitDOMDOMSelection* sel; | |
876 | WebKitDOMRange* range; | |
877 | WebKitDOMElement* div; | |
878 | WebKitDOMDocumentFragment* clone; | |
879 | WebKitDOMHTMLElement* html; | |
880 | ||
881 | doc = webkit_web_view_get_dom_document(m_web_view); | |
882 | win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc)); | |
883 | sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win)); | |
884 | range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel), | |
885 | 0, NULL); | |
886 | div = webkit_dom_document_create_element(WEBKIT_DOM_DOCUMENT(doc), "div", NULL); | |
887 | ||
888 | clone = webkit_dom_range_clone_contents(WEBKIT_DOM_RANGE(range), NULL); | |
889 | webkit_dom_node_append_child(&div->parent_instance, &clone->parent_instance, NULL); | |
890 | html = (WebKitDOMHTMLElement*)div; | |
891 | ||
892 | return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html)), | |
893 | wxConvUTF8); | |
894 | } | |
895 | ||
896 | void wxWebViewWebKit::ClearSelection() | |
897 | { | |
898 | WebKitDOMDocument* doc; | |
899 | WebKitDOMDOMWindow* win; | |
900 | WebKitDOMDOMSelection* sel; | |
901 | ||
902 | doc = webkit_web_view_get_dom_document(m_web_view); | |
903 | win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc)); | |
904 | sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win)); | |
905 | webkit_dom_dom_selection_remove_all_ranges(WEBKIT_DOM_DOM_SELECTION(sel)); | |
906 | ||
907 | } | |
908 | ||
909 | wxString wxWebViewWebKit::GetPageText() const | |
910 | { | |
911 | WebKitDOMDocument* doc; | |
912 | WebKitDOMHTMLElement* body; | |
913 | ||
914 | doc = webkit_web_view_get_dom_document(m_web_view); | |
915 | body = webkit_dom_document_get_body(WEBKIT_DOM_DOCUMENT(doc)); | |
916 | return wxString(webkit_dom_html_element_get_inner_text(WEBKIT_DOM_HTML_ELEMENT(body)), | |
917 | wxConvUTF8); | |
918 | } | |
919 | ||
920 | void wxWebViewWebKit::RunScript(const wxString& javascript) | |
921 | { | |
922 | webkit_web_view_execute_script(m_web_view, | |
923 | javascript.mb_str(wxConvUTF8)); | |
924 | } | |
925 | ||
926 | void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) | |
927 | { | |
928 | m_handlerList.push_back(handler); | |
929 | } | |
930 | ||
931 | long wxWebViewWebKit::Find(const wxString& text, int flags) | |
932 | { | |
933 | bool newSearch = false; | |
934 | if(text != m_findText || | |
935 | (flags & wxWEB_VIEW_FIND_MATCH_CASE) != (m_findFlags & wxWEB_VIEW_FIND_MATCH_CASE)) | |
936 | { | |
937 | newSearch = true; | |
938 | //If it is a new search we need to clear existing highlights | |
939 | webkit_web_view_unmark_text_matches(m_web_view); | |
940 | webkit_web_view_set_highlight_text_matches(m_web_view, false); | |
941 | } | |
942 | ||
943 | m_findFlags = flags; | |
944 | m_findText = text; | |
945 | ||
946 | //If the search string is empty then we clear any selection and highlight | |
947 | if(text == "") | |
948 | { | |
949 | webkit_web_view_unmark_text_matches(m_web_view); | |
950 | webkit_web_view_set_highlight_text_matches(m_web_view, false); | |
951 | ClearSelection(); | |
952 | return wxNOT_FOUND; | |
953 | } | |
954 | ||
955 | bool wrap = false, matchCase = false, forward = true; | |
956 | if(flags & wxWEB_VIEW_FIND_WRAP) | |
957 | wrap = true; | |
958 | if(flags & wxWEB_VIEW_FIND_MATCH_CASE) | |
959 | matchCase = true; | |
960 | if(flags & wxWEB_VIEW_FIND_BACKWARDS) | |
961 | forward = false; | |
962 | ||
963 | if(newSearch) | |
964 | { | |
965 | //Initially we mark the matches to know how many we have | |
966 | m_findCount = webkit_web_view_mark_text_matches(m_web_view, wxGTK_CONV(text), matchCase, 0); | |
967 | //In this case we return early to match IE behaviour | |
968 | m_findPosition = -1; | |
969 | return m_findCount; | |
970 | } | |
971 | else | |
972 | { | |
973 | if(forward) | |
974 | m_findPosition++; | |
975 | else | |
976 | m_findPosition--; | |
977 | if(m_findPosition < 0) | |
978 | m_findPosition += m_findCount; | |
979 | if(m_findPosition > m_findCount) | |
980 | m_findPosition -= m_findCount; | |
981 | } | |
982 | ||
983 | //Highlight them if needed | |
984 | bool highlight = flags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT ? true : false; | |
985 | webkit_web_view_set_highlight_text_matches(m_web_view, highlight); | |
986 | ||
987 | if(!webkit_web_view_search_text(m_web_view, wxGTK_CONV(text), matchCase, forward, wrap)) | |
988 | { | |
989 | m_findPosition = -1; | |
990 | ClearSelection(); | |
991 | return wxNOT_FOUND; | |
992 | } | |
993 | wxLogMessage(wxString::Format("Returning %d", m_findPosition)); | |
994 | return newSearch ? m_findCount : m_findPosition; | |
995 | } | |
996 | ||
997 | void wxWebViewWebKit::FindClear() | |
998 | { | |
999 | m_findCount = 0; | |
1000 | m_findFlags = 0; | |
1001 | m_findText = ""; | |
1002 | m_findPosition = -1; | |
1003 | } | |
1004 | ||
1005 | // static | |
1006 | wxVisualAttributes | |
1007 | wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
1008 | { | |
1009 | return GetDefaultAttributesFromGTKWidget(webkit_web_view_new); | |
1010 | } | |
1011 | ||
1012 | ||
1013 | #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT |