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