]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/webview_webkit.cpp
Fix for specific filename syntax of OpenVMS
[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;
04fa04d8
SL
62 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED,
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;
04fa04d8
SL
72 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
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);
04fa04d8
SL
104 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
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;
04fa04d8 158 wxWebViewNavigationError type = wxWEB_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:
168 type = wxWEB_NAV_ERR_USER_CANCELLED;
169 break;
170
171 case SOUP_STATUS_CANT_RESOLVE:
172 case SOUP_STATUS_NOT_FOUND:
173 type = wxWEB_NAV_ERR_NOT_FOUND;
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:
181 type = wxWEB_NAV_ERR_CONNECTION;
182 break;
183
184 case SOUP_STATUS_MALFORMED:
185 //case SOUP_STATUS_TOO_MANY_REDIRECTS:
186 type = wxWEB_NAV_ERR_REQUEST;
187 break;
188
189 //case SOUP_STATUS_NO_CONTENT:
190 //case SOUP_STATUS_RESET_CONTENT:
191
192 case SOUP_STATUS_BAD_REQUEST:
193 type = wxWEB_NAV_ERR_REQUEST;
194 break;
195
196 case SOUP_STATUS_UNAUTHORIZED:
197 case SOUP_STATUS_FORBIDDEN:
198 type = wxWEB_NAV_ERR_AUTH;
199 break;
200
201 case SOUP_STATUS_METHOD_NOT_ALLOWED:
202 case SOUP_STATUS_NOT_ACCEPTABLE:
203 type = wxWEB_NAV_ERR_SECURITY;
204 break;
205
206 case SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED:
207 type = wxWEB_NAV_ERR_AUTH;
208 break;
209
210 case SOUP_STATUS_REQUEST_TIMEOUT:
211 type = wxWEB_NAV_ERR_CONNECTION;
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:
218 type = wxWEB_NAV_ERR_REQUEST;
219 break;
220
221 case SOUP_STATUS_BAD_GATEWAY:
222 case SOUP_STATUS_SERVICE_UNAVAILABLE:
223 case SOUP_STATUS_GATEWAY_TIMEOUT:
224 type = wxWEB_NAV_ERR_CONNECTION;
225 break;
226
227 case SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED:
228 type = wxWEB_NAV_ERR_REQUEST;
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:
243 type = wxWEB_NAV_ERR_REQUEST;
244 break;
245
246 case WEBKIT_NETWORK_ERROR_CANCELLED:
247 type = wxWEB_NAV_ERR_USER_CANCELLED;
248 break;
249
250 case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST:
251 type = wxWEB_NAV_ERR_NOT_FOUND;
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:
265 type = wxWEB_NAV_ERR_SECURITY;
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
04fa04d8
SL
277 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR,
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);
04fa04d8
SL
302 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
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{
04fa04d8
SL
321 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
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
61b98a2d
SL
381} // extern "C"
382
383//-----------------------------------------------------------------------------
b64b4e70 384// wxWebViewWebKit
61b98a2d
SL
385//-----------------------------------------------------------------------------
386
cddf4541 387wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxWebView);
61b98a2d 388
8ab75332
PC
389wxWebViewWebKit::wxWebViewWebKit()
390{
391 m_web_view = NULL;
392}
393
b64b4e70 394bool wxWebViewWebKit::Create(wxWindow *parent,
61b98a2d
SL
395 wxWindowID id,
396 const wxString &url,
397 const wxPoint& pos,
398 const wxSize& size,
399 long style,
400 const wxString& name)
401{
61b98a2d 402 m_busy = false;
36b52591 403 m_guard = false;
66ac0400 404 FindClear();
61b98a2d 405
1f7c17f4
VZ
406 // We currently unconditionally impose scrolling in both directions as it's
407 // necessary to show arbitrary pages.
408 style |= wxHSCROLL | wxVSCROLL;
409
61b98a2d
SL
410 if (!PreCreation( parent, pos, size ) ||
411 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
412 {
b64b4e70 413 wxFAIL_MSG( wxT("wxWebViewWebKit creation failed") );
61b98a2d
SL
414 return false;
415 }
416
4c9bde5e 417 m_web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
1f7c17f4
VZ
418 GTKCreateScrolledWindowWith(GTK_WIDGET(m_web_view));
419 g_object_ref(m_widget);
61b98a2d 420
4c9bde5e 421 g_signal_connect_after(m_web_view, "navigation-policy-decision-requested",
780f7262 422 G_CALLBACK(wxgtk_webview_webkit_navigation),
61b98a2d 423 this);
34326da7 424 g_signal_connect_after(m_web_view, "load-error",
d363c7db 425 G_CALLBACK(wxgtk_webview_webkit_error),
61b98a2d
SL
426 this);
427
4c9bde5e 428 g_signal_connect_after(m_web_view, "new-window-policy-decision-requested",
d363c7db 429 G_CALLBACK(wxgtk_webview_webkit_new_window), this);
61b98a2d 430
4c9bde5e 431 g_signal_connect_after(m_web_view, "title-changed",
153530af
SL
432 G_CALLBACK(wxgtk_webview_webkit_title_changed), this);
433
4c9bde5e 434 g_signal_connect_after(m_web_view, "resource-request-starting",
211da8a5
SL
435 G_CALLBACK(wxgtk_webview_webkit_resource_req), this);
436
61b98a2d
SL
437 m_parent->DoAddChild( this );
438
439 PostCreation(size);
440
441 /* Open a webpage */
4c9bde5e 442 webkit_web_view_load_uri(m_web_view, url.utf8_str());
61b98a2d 443
152a5808
SL
444 //Get the initial history limit so we can enable and disable it later
445 WebKitWebBackForwardList* history;
4c9bde5e 446 history = webkit_web_view_get_back_forward_list(m_web_view);
152a5808
SL
447 m_historyLimit = webkit_web_back_forward_list_get_limit(history);
448
7cf17ea2
PC
449 // last to avoid getting signal too early
450 g_signal_connect_after(m_web_view, "notify::load-status",
451 G_CALLBACK(wxgtk_webview_webkit_load_status),
452 this);
61b98a2d
SL
453
454 return true;
455}
456
c3980646
VZ
457wxWebViewWebKit::~wxWebViewWebKit()
458{
8ab75332
PC
459 if (m_web_view)
460 GTKDisconnect(m_web_view);
c3980646
VZ
461}
462
b64b4e70 463bool wxWebViewWebKit::Enable( bool enable )
61b98a2d
SL
464{
465 if (!wxControl::Enable(enable))
466 return false;
467
9dc44eff 468 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable);
61b98a2d
SL
469
470 //if (enable)
471 // GTKFixSensitivity();
472
473 return true;
474}
475
476GdkWindow*
b64b4e70 477wxWebViewWebKit::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
61b98a2d
SL
478{
479 GdkWindow* window = gtk_widget_get_parent_window(m_widget);
480 return window;
481}
482
b64b4e70 483void wxWebViewWebKit::ZoomIn()
61b98a2d 484{
4c9bde5e 485 webkit_web_view_zoom_in(m_web_view);
61b98a2d
SL
486}
487
b64b4e70 488void wxWebViewWebKit::ZoomOut()
61b98a2d 489{
4c9bde5e 490 webkit_web_view_zoom_out(m_web_view);
61b98a2d
SL
491}
492
b64b4e70 493void wxWebViewWebKit::SetWebkitZoom(float level)
61b98a2d 494{
4c9bde5e 495 webkit_web_view_set_zoom_level(m_web_view, level);
61b98a2d
SL
496}
497
e669ddde 498float wxWebViewWebKit::GetWebkitZoom() const
61b98a2d 499{
4c9bde5e 500 return webkit_web_view_get_zoom_level(m_web_view);
61b98a2d
SL
501}
502
b64b4e70 503void wxWebViewWebKit::Stop()
61b98a2d 504{
4c9bde5e 505 webkit_web_view_stop_loading(m_web_view);
61b98a2d
SL
506}
507
b64b4e70 508void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags)
61b98a2d
SL
509{
510 if (flags & wxWEB_VIEW_RELOAD_NO_CACHE)
511 {
4c9bde5e 512 webkit_web_view_reload_bypass_cache(m_web_view);
61b98a2d
SL
513 }
514 else
515 {
4c9bde5e 516 webkit_web_view_reload(m_web_view);
61b98a2d
SL
517 }
518}
519
4d0dddc7 520void wxWebViewWebKit::LoadURL(const wxString& url)
61b98a2d 521{
4c9bde5e 522 webkit_web_view_load_uri(m_web_view, wxGTK_CONV(url));
61b98a2d
SL
523}
524
525
b64b4e70 526void wxWebViewWebKit::GoBack()
61b98a2d 527{
4c9bde5e 528 webkit_web_view_go_back(m_web_view);
61b98a2d
SL
529}
530
b64b4e70 531void wxWebViewWebKit::GoForward()
61b98a2d 532{
4c9bde5e 533 webkit_web_view_go_forward(m_web_view);
61b98a2d
SL
534}
535
536
e669ddde 537bool wxWebViewWebKit::CanGoBack() const
61b98a2d 538{
4c9bde5e 539 return webkit_web_view_can_go_back(m_web_view);
61b98a2d
SL
540}
541
542
e669ddde 543bool wxWebViewWebKit::CanGoForward() const
61b98a2d 544{
4c9bde5e 545 return webkit_web_view_can_go_forward(m_web_view);
61b98a2d
SL
546}
547
152a5808
SL
548void wxWebViewWebKit::ClearHistory()
549{
550 WebKitWebBackForwardList* history;
4c9bde5e 551 history = webkit_web_view_get_back_forward_list(m_web_view);
152a5808
SL
552 webkit_web_back_forward_list_clear(history);
553}
554
555void wxWebViewWebKit::EnableHistory(bool enable)
556{
557 WebKitWebBackForwardList* history;
4c9bde5e 558 history = webkit_web_view_get_back_forward_list(m_web_view);
152a5808
SL
559 if(enable)
560 {
561 webkit_web_back_forward_list_set_limit(history, m_historyLimit);
562 }
563 else
564 {
565 webkit_web_back_forward_list_set_limit(history, 0);
566 }
567}
61b98a2d 568
c13d6ac1 569wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetBackwardHistory()
19fc1a2f 570{
34326da7 571 wxVector<wxSharedPtr<wxWebViewHistoryItem> > backhist;
19fc1a2f 572 WebKitWebBackForwardList* history;
4c9bde5e 573 history = webkit_web_view_get_back_forward_list(m_web_view);
34326da7 574 GList* list = webkit_web_back_forward_list_get_back_list_with_limit(history,
19fc1a2f
SL
575 m_historyLimit);
576 //We need to iterate in reverse to get the order we desire
577 for(int i = g_list_length(list) - 1; i >= 0 ; i--)
578 {
579 WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)g_list_nth_data(list, i);
c13d6ac1 580 wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem(
263e3a83
SL
581 webkit_web_history_item_get_uri(gtkitem),
582 webkit_web_history_item_get_title(gtkitem));
583 wxitem->m_histItem = gtkitem;
c13d6ac1 584 wxSharedPtr<wxWebViewHistoryItem> item(wxitem);
19fc1a2f 585 backhist.push_back(item);
19fc1a2f
SL
586 }
587 return backhist;
588}
589
c13d6ac1 590wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetForwardHistory()
19fc1a2f 591{
34326da7 592 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forwardhist;
19fc1a2f 593 WebKitWebBackForwardList* history;
4c9bde5e 594 history = webkit_web_view_get_back_forward_list(m_web_view);
34326da7 595 GList* list = webkit_web_back_forward_list_get_forward_list_with_limit(history,
19fc1a2f
SL
596 m_historyLimit);
597 for(guint i = 0; i < g_list_length(list); i++)
598 {
599 WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)g_list_nth_data(list, i);
c13d6ac1 600 wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem(
263e3a83
SL
601 webkit_web_history_item_get_uri(gtkitem),
602 webkit_web_history_item_get_title(gtkitem));
603 wxitem->m_histItem = gtkitem;
c13d6ac1 604 wxSharedPtr<wxWebViewHistoryItem> item(wxitem);
19fc1a2f 605 forwardhist.push_back(item);
19fc1a2f
SL
606 }
607 return forwardhist;
608}
609
c13d6ac1 610void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item)
19fc1a2f 611{
a0ff3611 612 WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)item->m_histItem;
19fc1a2f
SL
613 if(gtkitem)
614 {
34326da7 615 webkit_web_view_go_to_back_forward_item(m_web_view,
263e3a83 616 WEBKIT_WEB_HISTORY_ITEM(gtkitem));
19fc1a2f
SL
617 }
618}
619
e669ddde 620bool wxWebViewWebKit::CanCut() const
ae26e17b 621{
4c9bde5e 622 return webkit_web_view_can_cut_clipboard(m_web_view);
ae26e17b
SL
623}
624
e669ddde 625bool wxWebViewWebKit::CanCopy() const
ae26e17b 626{
4c9bde5e 627 return webkit_web_view_can_copy_clipboard(m_web_view);
ae26e17b
SL
628}
629
e669ddde 630bool wxWebViewWebKit::CanPaste() const
ae26e17b 631{
4c9bde5e 632 return webkit_web_view_can_paste_clipboard(m_web_view);
ae26e17b
SL
633}
634
635void wxWebViewWebKit::Cut()
636{
4c9bde5e 637 webkit_web_view_cut_clipboard(m_web_view);
ae26e17b
SL
638}
639
640void wxWebViewWebKit::Copy()
641{
4c9bde5e 642 webkit_web_view_copy_clipboard(m_web_view);
ae26e17b
SL
643}
644
645void wxWebViewWebKit::Paste()
646{
4c9bde5e 647 webkit_web_view_paste_clipboard(m_web_view);
ae26e17b
SL
648}
649
e669ddde 650bool wxWebViewWebKit::CanUndo() const
97e49559 651{
4c9bde5e 652 return webkit_web_view_can_undo(m_web_view);
97e49559
SL
653}
654
e669ddde 655bool wxWebViewWebKit::CanRedo() const
97e49559 656{
4c9bde5e 657 return webkit_web_view_can_redo(m_web_view);
97e49559
SL
658}
659
660void wxWebViewWebKit::Undo()
661{
4c9bde5e 662 webkit_web_view_undo(m_web_view);
97e49559
SL
663}
664
665void wxWebViewWebKit::Redo()
666{
4c9bde5e 667 webkit_web_view_redo(m_web_view);
97e49559
SL
668}
669
e669ddde 670wxString wxWebViewWebKit::GetCurrentURL() const
61b98a2d
SL
671{
672 // FIXME: check which encoding the web kit control uses instead of
673 // assuming UTF8 (here and elsewhere too)
4c9bde5e 674 return wxString::FromUTF8(webkit_web_view_get_uri(m_web_view));
61b98a2d
SL
675}
676
677
e669ddde 678wxString wxWebViewWebKit::GetCurrentTitle() const
61b98a2d 679{
4c9bde5e 680 return wxString::FromUTF8(webkit_web_view_get_title(m_web_view));
61b98a2d
SL
681}
682
683
e669ddde 684wxString wxWebViewWebKit::GetPageSource() const
61b98a2d 685{
4c9bde5e 686 WebKitWebFrame* frame = webkit_web_view_get_main_frame(m_web_view);
61b98a2d
SL
687 WebKitWebDataSource* src = webkit_web_frame_get_data_source (frame);
688
689 // TODO: check encoding with
690 // const gchar*
691 // webkit_web_data_source_get_encoding(WebKitWebDataSource *data_source);
692 return wxString(webkit_web_data_source_get_data (src)->str, wxConvUTF8);
693}
694
695
e669ddde 696wxWebViewZoom wxWebViewWebKit::GetZoom() const
61b98a2d
SL
697{
698 float zoom = GetWebkitZoom();
699
700 // arbitrary way to map float zoom to our common zoom enum
701 if (zoom <= 0.65)
702 {
703 return wxWEB_VIEW_ZOOM_TINY;
704 }
705 else if (zoom > 0.65 && zoom <= 0.90)
706 {
707 return wxWEB_VIEW_ZOOM_SMALL;
708 }
709 else if (zoom > 0.90 && zoom <= 1.15)
710 {
711 return wxWEB_VIEW_ZOOM_MEDIUM;
712 }
713 else if (zoom > 1.15 && zoom <= 1.45)
714 {
715 return wxWEB_VIEW_ZOOM_LARGE;
716 }
717 else if (zoom > 1.45)
718 {
719 return wxWEB_VIEW_ZOOM_LARGEST;
720 }
721
722 // to shut up compilers, this can never be reached logically
723 wxASSERT(false);
724 return wxWEB_VIEW_ZOOM_MEDIUM;
725}
726
727
b64b4e70 728void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom)
61b98a2d
SL
729{
730 // arbitrary way to map our common zoom enum to float zoom
731 switch (zoom)
732 {
733 case wxWEB_VIEW_ZOOM_TINY:
734 SetWebkitZoom(0.6f);
735 break;
736
737 case wxWEB_VIEW_ZOOM_SMALL:
738 SetWebkitZoom(0.8f);
739 break;
740
741 case wxWEB_VIEW_ZOOM_MEDIUM:
742 SetWebkitZoom(1.0f);
743 break;
744
745 case wxWEB_VIEW_ZOOM_LARGE:
746 SetWebkitZoom(1.3);
747 break;
748
749 case wxWEB_VIEW_ZOOM_LARGEST:
750 SetWebkitZoom(1.6);
751 break;
752
753 default:
754 wxASSERT(false);
755 }
756}
757
b64b4e70 758void wxWebViewWebKit::SetZoomType(wxWebViewZoomType type)
61b98a2d 759{
4c9bde5e 760 webkit_web_view_set_full_content_zoom(m_web_view,
61b98a2d
SL
761 (type == wxWEB_VIEW_ZOOM_TYPE_LAYOUT ?
762 TRUE : FALSE));
763}
764
b64b4e70 765wxWebViewZoomType wxWebViewWebKit::GetZoomType() const
61b98a2d 766{
4c9bde5e 767 gboolean fczoom = webkit_web_view_get_full_content_zoom(m_web_view);
61b98a2d
SL
768
769 if (fczoom) return wxWEB_VIEW_ZOOM_TYPE_LAYOUT;
770 else return wxWEB_VIEW_ZOOM_TYPE_TEXT;
771}
772
b64b4e70 773bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType) const
61b98a2d
SL
774{
775 // this port supports all zoom types
776 return true;
777}
778
a977376a 779void wxWebViewWebKit::DoSetPage(const wxString& html, const wxString& baseUri)
61b98a2d 780{
4c9bde5e 781 webkit_web_view_load_string (m_web_view,
61b98a2d
SL
782 html.mb_str(wxConvUTF8),
783 "text/html",
784 "UTF-8",
785 baseUri.mb_str(wxConvUTF8));
786}
787
b64b4e70 788void wxWebViewWebKit::Print()
61b98a2d 789{
4c9bde5e 790 WebKitWebFrame* frame = webkit_web_view_get_main_frame(m_web_view);
61b98a2d
SL
791 webkit_web_frame_print (frame);
792
793 // GtkPrintOperationResult webkit_web_frame_print_full
794 // (WebKitWebFrame *frame,
795 // GtkPrintOperation *operation,
796 // GtkPrintOperationAction action,
797 // GError **error);
798
799}
800
801
e669ddde 802bool wxWebViewWebKit::IsBusy() const
61b98a2d
SL
803{
804 return m_busy;
805
806 // This code looks nice but returns true after a page was cancelled
807 /*
808 WebKitLoadStatus status = webkit_web_view_get_load_status
809 (WEBKIT_WEB_VIEW(web_view));
810
811
812#if WEBKIT_CHECK_VERSION(1,1,16)
813 // WEBKIT_LOAD_FAILED is new in webkit 1.1.16
814 if (status == WEBKIT_LOAD_FAILED)
815 {
816 return false;
817 }
818#endif
819 if (status == WEBKIT_LOAD_FINISHED)
820 {
821 return false;
822 }
823
824 return true;
825 */
826}
827
c7cbe308
SL
828void wxWebViewWebKit::SetEditable(bool enable)
829{
4c9bde5e 830 webkit_web_view_set_editable(m_web_view, enable);
c7cbe308
SL
831}
832
e669ddde 833bool wxWebViewWebKit::IsEditable() const
c7cbe308 834{
4c9bde5e 835 return webkit_web_view_get_editable(m_web_view);
c7cbe308
SL
836}
837
63a65070
SL
838void wxWebViewWebKit::DeleteSelection()
839{
4c9bde5e 840 webkit_web_view_delete_selection(m_web_view);
63a65070
SL
841}
842
e669ddde 843bool wxWebViewWebKit::HasSelection() const
63a65070 844{
4c9bde5e 845 return webkit_web_view_has_selection(m_web_view);
63a65070
SL
846}
847
848void wxWebViewWebKit::SelectAll()
849{
4c9bde5e 850 webkit_web_view_select_all(m_web_view);
63a65070
SL
851}
852
e669ddde 853wxString wxWebViewWebKit::GetSelectedText() const
c9355a3d 854{
34326da7 855 WebKitDOMDocument* doc;
c9355a3d
SL
856 WebKitDOMDOMWindow* win;
857 WebKitDOMDOMSelection* sel;
858 WebKitDOMRange* range;
859
4c9bde5e 860 doc = webkit_web_view_get_dom_document(m_web_view);
c9355a3d
SL
861 win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc));
862 sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win));
34326da7 863 range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel),
c9355a3d 864 0, NULL);
34326da7 865 return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range)),
c9355a3d
SL
866 wxConvUTF8);
867}
63a65070 868
e669ddde 869wxString wxWebViewWebKit::GetSelectedSource() const
0fe8a1b6 870{
34326da7 871 WebKitDOMDocument* doc;
0fe8a1b6
SL
872 WebKitDOMDOMWindow* win;
873 WebKitDOMDOMSelection* sel;
874 WebKitDOMRange* range;
875 WebKitDOMElement* div;
876 WebKitDOMDocumentFragment* clone;
877 WebKitDOMHTMLElement* html;
878
4c9bde5e 879 doc = webkit_web_view_get_dom_document(m_web_view);
0fe8a1b6
SL
880 win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc));
881 sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win));
34326da7 882 range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel),
0fe8a1b6
SL
883 0, NULL);
884 div = webkit_dom_document_create_element(WEBKIT_DOM_DOCUMENT(doc), "div", NULL);
885
886 clone = webkit_dom_range_clone_contents(WEBKIT_DOM_RANGE(range), NULL);
887 webkit_dom_node_append_child(&div->parent_instance, &clone->parent_instance, NULL);
888 html = (WebKitDOMHTMLElement*)div;
889
34326da7 890 return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html)),
0fe8a1b6
SL
891 wxConvUTF8);
892}
893
41933aa5
SL
894void wxWebViewWebKit::ClearSelection()
895{
34326da7 896 WebKitDOMDocument* doc;
41933aa5
SL
897 WebKitDOMDOMWindow* win;
898 WebKitDOMDOMSelection* sel;
899
4c9bde5e 900 doc = webkit_web_view_get_dom_document(m_web_view);
41933aa5
SL
901 win = webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc));
902 sel = webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win));
903 webkit_dom_dom_selection_remove_all_ranges(WEBKIT_DOM_DOM_SELECTION(sel));
904
905}
906
e669ddde 907wxString wxWebViewWebKit::GetPageText() const
241b769f 908{
34326da7 909 WebKitDOMDocument* doc;
241b769f
SL
910 WebKitDOMHTMLElement* body;
911
4c9bde5e 912 doc = webkit_web_view_get_dom_document(m_web_view);
241b769f 913 body = webkit_dom_document_get_body(WEBKIT_DOM_DOCUMENT(doc));
34326da7 914 return wxString(webkit_dom_html_element_get_inner_text(WEBKIT_DOM_HTML_ELEMENT(body)),
241b769f
SL
915 wxConvUTF8);
916}
917
c9ccc09c
SL
918void wxWebViewWebKit::RunScript(const wxString& javascript)
919{
34326da7 920 webkit_web_view_execute_script(m_web_view,
c9ccc09c
SL
921 javascript.mb_str(wxConvUTF8));
922}
923
7d8d6163 924void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
f2049b68
SL
925{
926 m_handlerList.push_back(handler);
927}
928
66ac0400
SL
929long wxWebViewWebKit::Find(const wxString& text, int flags)
930{
931 bool newSearch = false;
932 if(text != m_findText ||
933 (flags & wxWEB_VIEW_FIND_MATCH_CASE) != (m_findFlags & wxWEB_VIEW_FIND_MATCH_CASE))
934 {
935 newSearch = true;
936 //If it is a new search we need to clear existing highlights
937 webkit_web_view_unmark_text_matches(m_web_view);
938 webkit_web_view_set_highlight_text_matches(m_web_view, false);
939 }
940
941 m_findFlags = flags;
942 m_findText = text;
943
944 //If the search string is empty then we clear any selection and highlight
945 if(text == "")
946 {
947 webkit_web_view_unmark_text_matches(m_web_view);
948 webkit_web_view_set_highlight_text_matches(m_web_view, false);
949 ClearSelection();
950 return wxNOT_FOUND;
951 }
952
953 bool wrap = false, matchCase = false, forward = true;
954 if(flags & wxWEB_VIEW_FIND_WRAP)
955 wrap = true;
956 if(flags & wxWEB_VIEW_FIND_MATCH_CASE)
957 matchCase = true;
958 if(flags & wxWEB_VIEW_FIND_BACKWARDS)
959 forward = false;
960
961 if(newSearch)
962 {
963 //Initially we mark the matches to know how many we have
964 m_findCount = webkit_web_view_mark_text_matches(m_web_view, wxGTK_CONV(text), matchCase, 0);
965 //In this case we return early to match IE behaviour
966 m_findPosition = -1;
967 return m_findCount;
968 }
969 else
970 {
971 if(forward)
972 m_findPosition++;
973 else
974 m_findPosition--;
975 if(m_findPosition < 0)
976 m_findPosition += m_findCount;
977 if(m_findPosition > m_findCount)
978 m_findPosition -= m_findCount;
979 }
980
981 //Highlight them if needed
982 bool highlight = flags & wxWEB_VIEW_FIND_HIGHLIGHT_RESULT ? true : false;
983 webkit_web_view_set_highlight_text_matches(m_web_view, highlight);
984
985 if(!webkit_web_view_search_text(m_web_view, wxGTK_CONV(text), matchCase, forward, wrap))
986 {
987 m_findPosition = -1;
988 ClearSelection();
989 return wxNOT_FOUND;
990 }
991 wxLogMessage(wxString::Format("Returning %d", m_findPosition));
992 return newSearch ? m_findCount : m_findPosition;
993}
994
995void wxWebViewWebKit::FindClear()
996{
997 m_findCount = 0;
998 m_findFlags = 0;
999 m_findText = "";
1000 m_findPosition = -1;
1001}
1002
61b98a2d
SL
1003// static
1004wxVisualAttributes
b64b4e70 1005wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
61b98a2d
SL
1006{
1007 return GetDefaultAttributesFromGTKWidget(webkit_web_view_new);
1008}
1009
1010
9d2f31db 1011#endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT