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