]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/webview_webkit.cpp
remove symbol exports, this code is private to the library
[wxWidgets.git] / src / gtk / webview_webkit.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/webview_webkit.cpp
3 // Purpose: GTK WebKit backend for web view component
4 // Author: Marianne Gagnon, Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon, 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT
14
15 #include "wx/stockitem.h"
16 #include "wx/gtk/webview_webkit.h"
17 #include "wx/gtk/control.h"
18 #include "wx/gtk/private.h"
19 #include "wx/filesys.h"
20 #include "wx/base64.h"
21 #include "wx/log.h"
22 #include <webkit/webkit.h>
23
24 // ----------------------------------------------------------------------------
25 // GTK callbacks
26 // ----------------------------------------------------------------------------
27
28 extern "C"
29 {
30
31 static void
32 wxgtk_webview_webkit_load_status(GtkWidget* widget,
33 GParamSpec*,
34 wxWebViewWebKit *webKitCtrl)
35 {
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 {
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 {
52 WebKitWebHistoryItem*
53 newitem = webkit_web_history_item_new_with_data
54 (
55 url.utf8_str(),
56 webKitCtrl->GetCurrentTitle().utf8_str()
57 );
58 webkit_web_back_forward_list_add_item(hist, newitem);
59 }
60
61 webKitCtrl->m_busy = false;
62 wxWebViewEvent event(wxEVT_WEBVIEW_LOADED,
63 webKitCtrl->GetId(),
64 url, target);
65
66 if (webKitCtrl && webKitCtrl->GetEventHandler())
67 webKitCtrl->GetEventHandler()->ProcessEvent(event);
68 }
69 else if (status == WEBKIT_LOAD_COMMITTED)
70 {
71 webKitCtrl->m_busy = true;
72 wxWebViewEvent event(wxEVT_WEBVIEW_NAVIGATED,
73 webKitCtrl->GetId(),
74 url, target);
75
76 if (webKitCtrl && webKitCtrl->GetEventHandler())
77 webKitCtrl->GetEventHandler()->ProcessEvent(event);
78 }
79 }
80
81 static gboolean
82 wxgtk_webview_webkit_navigation(WebKitWebView *,
83 WebKitWebFrame *frame,
84 WebKitNetworkRequest *request,
85 WebKitWebNavigationAction *,
86 WebKitWebPolicyDecision *policy_decision,
87 wxWebViewWebKit *webKitCtrl)
88 {
89 if(webKitCtrl->m_guard)
90 {
91 webKitCtrl->m_guard = false;
92 //We set this to make sure that we don't try to load the page again from
93 //the resource request callback
94 webKitCtrl->m_vfsurl = webkit_network_request_get_uri(request);
95 webkit_web_policy_decision_use(policy_decision);
96 return FALSE;
97 }
98
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);
104 wxWebViewEvent event(wxEVT_WEBVIEW_NAVIGATING,
105 webKitCtrl->GetId(),
106 wxString( uri, wxConvUTF8 ),
107 target);
108
109 if (webKitCtrl && webKitCtrl->GetEventHandler())
110 webKitCtrl->GetEventHandler()->ProcessEvent(event);
111
112 if (!event.IsAllowed())
113 {
114 webKitCtrl->m_busy = false;
115 webkit_web_policy_decision_ignore(policy_decision);
116 return TRUE;
117 }
118 else
119 {
120 wxString wxuri = uri;
121 wxSharedPtr<wxWebViewHandler> handler;
122 wxVector<wxSharedPtr<wxWebViewHandler> > hanlders = webKitCtrl->GetHandlers();
123 //We are not vetoed so see if we match one of the additional handlers
124 for(wxVector<wxSharedPtr<wxWebViewHandler> >::iterator it = hanlders.begin();
125 it != hanlders.end(); ++it)
126 {
127 if(wxuri.substr(0, (*it)->GetName().length()) == (*it)->GetName())
128 {
129 handler = (*it);
130 }
131 }
132 //If we found a handler we can then use it to load the file directly
133 //ourselves
134 if(handler)
135 {
136 webKitCtrl->m_guard = true;
137 wxFSFile* file = handler->GetFile(wxuri);
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;
145 }
146 return FALSE;
147 }
148 }
149
150 static gboolean
151 wxgtk_webview_webkit_error(WebKitWebView*,
152 WebKitWebFrame*,
153 gchar *uri,
154 gpointer web_error,
155 wxWebViewWebKit* webKitWindow)
156 {
157 webKitWindow->m_busy = false;
158 wxWebViewNavigationError type = wxWEBVIEW_NAV_ERR_OTHER;
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 = wxWEBVIEW_NAV_ERR_USER_CANCELLED;
169 break;
170
171 case SOUP_STATUS_CANT_RESOLVE:
172 case SOUP_STATUS_NOT_FOUND:
173 type = wxWEBVIEW_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 = wxWEBVIEW_NAV_ERR_CONNECTION;
182 break;
183
184 case SOUP_STATUS_MALFORMED:
185 //case SOUP_STATUS_TOO_MANY_REDIRECTS:
186 type = wxWEBVIEW_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 = wxWEBVIEW_NAV_ERR_REQUEST;
194 break;
195
196 case SOUP_STATUS_UNAUTHORIZED:
197 case SOUP_STATUS_FORBIDDEN:
198 type = wxWEBVIEW_NAV_ERR_AUTH;
199 break;
200
201 case SOUP_STATUS_METHOD_NOT_ALLOWED:
202 case SOUP_STATUS_NOT_ACCEPTABLE:
203 type = wxWEBVIEW_NAV_ERR_SECURITY;
204 break;
205
206 case SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED:
207 type = wxWEBVIEW_NAV_ERR_AUTH;
208 break;
209
210 case SOUP_STATUS_REQUEST_TIMEOUT:
211 type = wxWEBVIEW_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 = wxWEBVIEW_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 = wxWEBVIEW_NAV_ERR_CONNECTION;
225 break;
226
227 case SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED:
228 type = wxWEBVIEW_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 = wxWEBVIEW_NAV_ERR_REQUEST;
244 break;
245
246 case WEBKIT_NETWORK_ERROR_CANCELLED:
247 type = wxWEBVIEW_NAV_ERR_USER_CANCELLED;
248 break;
249
250 case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST:
251 type = wxWEBVIEW_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 = wxWEBVIEW_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
277 wxWebViewEvent event(wxEVT_WEBVIEW_ERROR,
278 webKitWindow->GetId(),
279 uri, "");
280 event.SetString(description);
281 event.SetInt(type);
282
283 if (webKitWindow && webKitWindow->GetEventHandler())
284 {
285 webKitWindow->GetEventHandler()->ProcessEvent(event);
286 }
287
288 return FALSE;
289 }
290
291 static gboolean
292 wxgtk_webview_webkit_new_window(WebKitWebView*,
293 WebKitWebFrame *frame,
294 WebKitNetworkRequest *request,
295 WebKitWebNavigationAction*,
296 WebKitWebPolicyDecision *policy_decision,
297 wxWebViewWebKit *webKitCtrl)
298 {
299 const gchar* uri = webkit_network_request_get_uri(request);
300
301 wxString target = webkit_web_frame_get_name (frame);
302 wxWebViewEvent event(wxEVT_WEBVIEW_NEWWINDOW,
303 webKitCtrl->GetId(),
304 wxString( uri, wxConvUTF8 ),
305 target);
306
307 if (webKitCtrl && webKitCtrl->GetEventHandler())
308 webKitCtrl->GetEventHandler()->ProcessEvent(event);
309
310 //We always want the user to handle this themselves
311 webkit_web_policy_decision_ignore(policy_decision);
312 return TRUE;
313 }
314
315 static void
316 wxgtk_webview_webkit_title_changed(WebKitWebView*,
317 WebKitWebFrame*,
318 gchar *title,
319 wxWebViewWebKit *webKitCtrl)
320 {
321 wxWebViewEvent event(wxEVT_WEBVIEW_TITLE_CHANGED,
322 webKitCtrl->GetId(),
323 webKitCtrl->GetCurrentURL(),
324 "");
325 event.SetString(wxString(title, wxConvUTF8));
326
327 if (webKitCtrl && webKitCtrl->GetEventHandler())
328 webKitCtrl->GetEventHandler()->ProcessEvent(event);
329
330 }
331
332 static void
333 wxgtk_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);
341
342 wxSharedPtr<wxWebViewHandler> handler;
343 wxVector<wxSharedPtr<wxWebViewHandler> > hanlders = webKitCtrl->GetHandlers();
344
345 //We are not vetoed so see if we match one of the additional handlers
346 for(wxVector<wxSharedPtr<wxWebViewHandler> >::iterator it = hanlders.begin();
347 it != hanlders.end(); ++it)
348 {
349 if(uri.substr(0, (*it)->GetName().length()) == (*it)->GetName())
350 {
351 handler = (*it);
352 }
353 }
354 //If we found a handler we can then use it to load the file directly
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 {
366 //We load the data into a data url to save it being written out again
367 size_t size = file->GetStream()->GetLength();
368 char *buffer = new char[size];
369 file->GetStream()->Read(buffer, size);
370 wxString data = wxBase64Encode(buffer, size);
371 delete[] buffer;
372 wxString mime = file->GetMimeType();
373 wxString path = "data:" + mime + ";base64," + data;
374 //Then we can redirect the call
375 webkit_network_request_set_uri(request, path.utf8_str());
376 }
377
378 }
379 }
380
381 #if WEBKIT_CHECK_VERSION(1, 10, 0)
382
383 static gboolean
384 wxgtk_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
398 } // extern "C"
399
400 //-----------------------------------------------------------------------------
401 // wxWebViewWebKit
402 //-----------------------------------------------------------------------------
403
404 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxWebView);
405
406 wxWebViewWebKit::wxWebViewWebKit()
407 {
408 m_web_view = NULL;
409 }
410
411 bool wxWebViewWebKit::Create(wxWindow *parent,
412 wxWindowID id,
413 const wxString &url,
414 const wxPoint& pos,
415 const wxSize& size,
416 long style,
417 const wxString& name)
418 {
419 m_busy = false;
420 m_guard = false;
421 FindClear();
422
423 // We currently unconditionally impose scrolling in both directions as it's
424 // necessary to show arbitrary pages.
425 style |= wxHSCROLL | wxVSCROLL;
426
427 if (!PreCreation( parent, pos, size ) ||
428 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
429 {
430 wxFAIL_MSG( wxT("wxWebViewWebKit creation failed") );
431 return false;
432 }
433
434 m_web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());
435 GTKCreateScrolledWindowWith(GTK_WIDGET(m_web_view));
436 g_object_ref(m_widget);
437
438 g_signal_connect_after(m_web_view, "navigation-policy-decision-requested",
439 G_CALLBACK(wxgtk_webview_webkit_navigation),
440 this);
441 g_signal_connect_after(m_web_view, "load-error",
442 G_CALLBACK(wxgtk_webview_webkit_error),
443 this);
444
445 g_signal_connect_after(m_web_view, "new-window-policy-decision-requested",
446 G_CALLBACK(wxgtk_webview_webkit_new_window), this);
447
448 g_signal_connect_after(m_web_view, "title-changed",
449 G_CALLBACK(wxgtk_webview_webkit_title_changed), this);
450
451 g_signal_connect_after(m_web_view, "resource-request-starting",
452 G_CALLBACK(wxgtk_webview_webkit_resource_req), this);
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
458
459 m_parent->DoAddChild( this );
460
461 PostCreation(size);
462
463 /* Open a webpage */
464 webkit_web_view_load_uri(m_web_view, url.utf8_str());
465
466 //Get the initial history limit so we can enable and disable it later
467 WebKitWebBackForwardList* history;
468 history = webkit_web_view_get_back_forward_list(m_web_view);
469 m_historyLimit = webkit_web_back_forward_list_get_limit(history);
470
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);
475
476 return true;
477 }
478
479 wxWebViewWebKit::~wxWebViewWebKit()
480 {
481 if (m_web_view)
482 GTKDisconnect(m_web_view);
483 }
484
485 bool wxWebViewWebKit::Enable( bool enable )
486 {
487 if (!wxControl::Enable(enable))
488 return false;
489
490 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable);
491
492 //if (enable)
493 // GTKFixSensitivity();
494
495 return true;
496 }
497
498 GdkWindow*
499 wxWebViewWebKit::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
500 {
501 GdkWindow* window = gtk_widget_get_parent_window(m_widget);
502 return window;
503 }
504
505 void wxWebViewWebKit::ZoomIn()
506 {
507 webkit_web_view_zoom_in(m_web_view);
508 }
509
510 void wxWebViewWebKit::ZoomOut()
511 {
512 webkit_web_view_zoom_out(m_web_view);
513 }
514
515 void wxWebViewWebKit::SetWebkitZoom(float level)
516 {
517 webkit_web_view_set_zoom_level(m_web_view, level);
518 }
519
520 float wxWebViewWebKit::GetWebkitZoom() const
521 {
522 return webkit_web_view_get_zoom_level(m_web_view);
523 }
524
525 void wxWebViewWebKit::Stop()
526 {
527 webkit_web_view_stop_loading(m_web_view);
528 }
529
530 void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags)
531 {
532 if (flags & wxWEBVIEW_RELOAD_NO_CACHE)
533 {
534 webkit_web_view_reload_bypass_cache(m_web_view);
535 }
536 else
537 {
538 webkit_web_view_reload(m_web_view);
539 }
540 }
541
542 void wxWebViewWebKit::LoadURL(const wxString& url)
543 {
544 webkit_web_view_load_uri(m_web_view, wxGTK_CONV(url));
545 }
546
547
548 void wxWebViewWebKit::GoBack()
549 {
550 webkit_web_view_go_back(m_web_view);
551 }
552
553 void wxWebViewWebKit::GoForward()
554 {
555 webkit_web_view_go_forward(m_web_view);
556 }
557
558
559 bool wxWebViewWebKit::CanGoBack() const
560 {
561 return webkit_web_view_can_go_back(m_web_view);
562 }
563
564
565 bool wxWebViewWebKit::CanGoForward() const
566 {
567 return webkit_web_view_can_go_forward(m_web_view);
568 }
569
570 void wxWebViewWebKit::ClearHistory()
571 {
572 WebKitWebBackForwardList* history;
573 history = webkit_web_view_get_back_forward_list(m_web_view);
574 webkit_web_back_forward_list_clear(history);
575 }
576
577 void wxWebViewWebKit::EnableHistory(bool enable)
578 {
579 WebKitWebBackForwardList* history;
580 history = webkit_web_view_get_back_forward_list(m_web_view);
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 }
590
591 wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetBackwardHistory()
592 {
593 wxVector<wxSharedPtr<wxWebViewHistoryItem> > backhist;
594 WebKitWebBackForwardList* history;
595 history = webkit_web_view_get_back_forward_list(m_web_view);
596 GList* list = webkit_web_back_forward_list_get_back_list_with_limit(history,
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);
602 wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem(
603 webkit_web_history_item_get_uri(gtkitem),
604 webkit_web_history_item_get_title(gtkitem));
605 wxitem->m_histItem = gtkitem;
606 wxSharedPtr<wxWebViewHistoryItem> item(wxitem);
607 backhist.push_back(item);
608 }
609 return backhist;
610 }
611
612 wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetForwardHistory()
613 {
614 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forwardhist;
615 WebKitWebBackForwardList* history;
616 history = webkit_web_view_get_back_forward_list(m_web_view);
617 GList* list = webkit_web_back_forward_list_get_forward_list_with_limit(history,
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);
622 wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem(
623 webkit_web_history_item_get_uri(gtkitem),
624 webkit_web_history_item_get_title(gtkitem));
625 wxitem->m_histItem = gtkitem;
626 wxSharedPtr<wxWebViewHistoryItem> item(wxitem);
627 forwardhist.push_back(item);
628 }
629 return forwardhist;
630 }
631
632 void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item)
633 {
634 WebKitWebHistoryItem* gtkitem = (WebKitWebHistoryItem*)item->m_histItem;
635 if(gtkitem)
636 {
637 webkit_web_view_go_to_back_forward_item(m_web_view,
638 WEBKIT_WEB_HISTORY_ITEM(gtkitem));
639 }
640 }
641
642 bool wxWebViewWebKit::CanCut() const
643 {
644 return webkit_web_view_can_cut_clipboard(m_web_view);
645 }
646
647 bool wxWebViewWebKit::CanCopy() const
648 {
649 return webkit_web_view_can_copy_clipboard(m_web_view);
650 }
651
652 bool wxWebViewWebKit::CanPaste() const
653 {
654 return webkit_web_view_can_paste_clipboard(m_web_view);
655 }
656
657 void wxWebViewWebKit::Cut()
658 {
659 webkit_web_view_cut_clipboard(m_web_view);
660 }
661
662 void wxWebViewWebKit::Copy()
663 {
664 webkit_web_view_copy_clipboard(m_web_view);
665 }
666
667 void wxWebViewWebKit::Paste()
668 {
669 webkit_web_view_paste_clipboard(m_web_view);
670 }
671
672 bool wxWebViewWebKit::CanUndo() const
673 {
674 return webkit_web_view_can_undo(m_web_view);
675 }
676
677 bool wxWebViewWebKit::CanRedo() const
678 {
679 return webkit_web_view_can_redo(m_web_view);
680 }
681
682 void wxWebViewWebKit::Undo()
683 {
684 webkit_web_view_undo(m_web_view);
685 }
686
687 void wxWebViewWebKit::Redo()
688 {
689 webkit_web_view_redo(m_web_view);
690 }
691
692 wxString wxWebViewWebKit::GetCurrentURL() const
693 {
694 // FIXME: check which encoding the web kit control uses instead of
695 // assuming UTF8 (here and elsewhere too)
696 return wxString::FromUTF8(webkit_web_view_get_uri(m_web_view));
697 }
698
699
700 wxString wxWebViewWebKit::GetCurrentTitle() const
701 {
702 return wxString::FromUTF8(webkit_web_view_get_title(m_web_view));
703 }
704
705
706 wxString wxWebViewWebKit::GetPageSource() const
707 {
708 WebKitWebFrame* frame = webkit_web_view_get_main_frame(m_web_view);
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
718 wxWebViewZoom wxWebViewWebKit::GetZoom() const
719 {
720 float zoom = GetWebkitZoom();
721
722 // arbitrary way to map float zoom to our common zoom enum
723 if (zoom <= 0.65)
724 {
725 return wxWEBVIEW_ZOOM_TINY;
726 }
727 else if (zoom > 0.65 && zoom <= 0.90)
728 {
729 return wxWEBVIEW_ZOOM_SMALL;
730 }
731 else if (zoom > 0.90 && zoom <= 1.15)
732 {
733 return wxWEBVIEW_ZOOM_MEDIUM;
734 }
735 else if (zoom > 1.15 && zoom <= 1.45)
736 {
737 return wxWEBVIEW_ZOOM_LARGE;
738 }
739 else if (zoom > 1.45)
740 {
741 return wxWEBVIEW_ZOOM_LARGEST;
742 }
743
744 // to shut up compilers, this can never be reached logically
745 wxASSERT(false);
746 return wxWEBVIEW_ZOOM_MEDIUM;
747 }
748
749
750 void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom)
751 {
752 // arbitrary way to map our common zoom enum to float zoom
753 switch (zoom)
754 {
755 case wxWEBVIEW_ZOOM_TINY:
756 SetWebkitZoom(0.6f);
757 break;
758
759 case wxWEBVIEW_ZOOM_SMALL:
760 SetWebkitZoom(0.8f);
761 break;
762
763 case wxWEBVIEW_ZOOM_MEDIUM:
764 SetWebkitZoom(1.0f);
765 break;
766
767 case wxWEBVIEW_ZOOM_LARGE:
768 SetWebkitZoom(1.3);
769 break;
770
771 case wxWEBVIEW_ZOOM_LARGEST:
772 SetWebkitZoom(1.6);
773 break;
774
775 default:
776 wxASSERT(false);
777 }
778 }
779
780 void wxWebViewWebKit::SetZoomType(wxWebViewZoomType type)
781 {
782 webkit_web_view_set_full_content_zoom(m_web_view,
783 (type == wxWEBVIEW_ZOOM_TYPE_LAYOUT ?
784 TRUE : FALSE));
785 }
786
787 wxWebViewZoomType wxWebViewWebKit::GetZoomType() const
788 {
789 gboolean fczoom = webkit_web_view_get_full_content_zoom(m_web_view);
790
791 if (fczoom) return wxWEBVIEW_ZOOM_TYPE_LAYOUT;
792 else return wxWEBVIEW_ZOOM_TYPE_TEXT;
793 }
794
795 bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType) const
796 {
797 // this port supports all zoom types
798 return true;
799 }
800
801 void wxWebViewWebKit::DoSetPage(const wxString& html, const wxString& baseUri)
802 {
803 webkit_web_view_load_string (m_web_view,
804 html.mb_str(wxConvUTF8),
805 "text/html",
806 "UTF-8",
807 baseUri.mb_str(wxConvUTF8));
808 }
809
810 void wxWebViewWebKit::Print()
811 {
812 WebKitWebFrame* frame = webkit_web_view_get_main_frame(m_web_view);
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
824 bool wxWebViewWebKit::IsBusy() const
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
850 void wxWebViewWebKit::SetEditable(bool enable)
851 {
852 webkit_web_view_set_editable(m_web_view, enable);
853 }
854
855 bool wxWebViewWebKit::IsEditable() const
856 {
857 return webkit_web_view_get_editable(m_web_view);
858 }
859
860 void wxWebViewWebKit::DeleteSelection()
861 {
862 webkit_web_view_delete_selection(m_web_view);
863 }
864
865 bool wxWebViewWebKit::HasSelection() const
866 {
867 return webkit_web_view_has_selection(m_web_view);
868 }
869
870 void wxWebViewWebKit::SelectAll()
871 {
872 webkit_web_view_select_all(m_web_view);
873 }
874
875 wxString wxWebViewWebKit::GetSelectedText() const
876 {
877 WebKitDOMDocument* doc;
878 WebKitDOMDOMWindow* win;
879 WebKitDOMDOMSelection* sel;
880 WebKitDOMRange* range;
881
882 doc = webkit_web_view_get_dom_document(m_web_view);
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));
885 range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel),
886 0, NULL);
887 return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range)),
888 wxConvUTF8);
889 }
890
891 wxString wxWebViewWebKit::GetSelectedSource() const
892 {
893 WebKitDOMDocument* doc;
894 WebKitDOMDOMWindow* win;
895 WebKitDOMDOMSelection* sel;
896 WebKitDOMRange* range;
897 WebKitDOMElement* div;
898 WebKitDOMDocumentFragment* clone;
899 WebKitDOMHTMLElement* html;
900
901 doc = webkit_web_view_get_dom_document(m_web_view);
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 range = webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel),
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
912 return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html)),
913 wxConvUTF8);
914 }
915
916 void wxWebViewWebKit::ClearSelection()
917 {
918 WebKitDOMDocument* doc;
919 WebKitDOMDOMWindow* win;
920 WebKitDOMDOMSelection* sel;
921
922 doc = webkit_web_view_get_dom_document(m_web_view);
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
929 wxString wxWebViewWebKit::GetPageText() const
930 {
931 WebKitDOMDocument* doc;
932 WebKitDOMHTMLElement* body;
933
934 doc = webkit_web_view_get_dom_document(m_web_view);
935 body = webkit_dom_document_get_body(WEBKIT_DOM_DOCUMENT(doc));
936 return wxString(webkit_dom_html_element_get_inner_text(WEBKIT_DOM_HTML_ELEMENT(body)),
937 wxConvUTF8);
938 }
939
940 void wxWebViewWebKit::RunScript(const wxString& javascript)
941 {
942 webkit_web_view_execute_script(m_web_view,
943 javascript.mb_str(wxConvUTF8));
944 }
945
946 void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
947 {
948 m_handlerList.push_back(handler);
949 }
950
951 void 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
960 long wxWebViewWebKit::Find(const wxString& text, int flags)
961 {
962 bool newSearch = false;
963 if(text != m_findText ||
964 (flags & wxWEBVIEW_FIND_MATCH_CASE) != (m_findFlags & wxWEBVIEW_FIND_MATCH_CASE))
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;
985 if(flags & wxWEBVIEW_FIND_WRAP)
986 wrap = true;
987 if(flags & wxWEBVIEW_FIND_MATCH_CASE)
988 matchCase = true;
989 if(flags & wxWEBVIEW_FIND_BACKWARDS)
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
1013 bool highlight = flags & wxWEBVIEW_FIND_HIGHLIGHT_RESULT ? true : false;
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
1026 void wxWebViewWebKit::FindClear()
1027 {
1028 m_findCount = 0;
1029 m_findFlags = 0;
1030 m_findText = "";
1031 m_findPosition = -1;
1032 }
1033
1034 // static
1035 wxVisualAttributes
1036 wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1037 {
1038 return GetDefaultAttributesFromGTKWidget(webkit_web_view_new());
1039 }
1040
1041
1042 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT