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