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