1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/webview_webkit.cpp
3 // Purpose: GTK WebKit backend for web view component
4 // Author: Marianne Gagnon, Robert Roebling
6 // Copyright: (c) 2010 Marianne Gagnon, 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT
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"
22 #include <webkit/webkit.h>
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
32 wxgtk_webview_webkit_load_status(GtkWidget
* widget
,
34 wxWebViewWebKit
*webKitCtrl
)
36 wxString url
= webKitCtrl
->GetCurrentURL();
38 WebKitLoadStatus status
;
39 g_object_get(G_OBJECT(widget
), "load-status", &status
, NULL
);
41 wxString target
; // TODO: get target (if possible)
43 if (status
== WEBKIT_LOAD_FINISHED
)
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
)
53 newitem
= webkit_web_history_item_new_with_data
56 webKitCtrl
->GetCurrentTitle().utf8_str()
58 webkit_web_back_forward_list_add_item(hist
, newitem
);
61 webKitCtrl
->m_busy
= false;
62 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
,
66 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
67 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
69 else if (status
== WEBKIT_LOAD_COMMITTED
)
71 webKitCtrl
->m_busy
= true;
72 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
76 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
77 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
82 wxgtk_webview_webkit_navigation(WebKitWebView
*,
83 WebKitWebFrame
*frame
,
84 WebKitNetworkRequest
*request
,
85 WebKitWebNavigationAction
*,
86 WebKitWebPolicyDecision
*policy_decision
,
87 wxWebViewWebKit
*webKitCtrl
)
89 if(webKitCtrl
->m_guard
)
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
);
99 webKitCtrl
->m_busy
= true;
101 const gchar
* uri
= webkit_network_request_get_uri(request
);
103 wxString target
= webkit_web_frame_get_name (frame
);
104 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
106 wxString( uri
, wxConvUTF8
),
109 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
110 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
112 if (!event
.IsAllowed())
114 webKitCtrl
->m_busy
= false;
115 webkit_web_policy_decision_ignore(policy_decision
);
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
)
127 if(wxuri
.substr(0, (*it
)->GetName().length()) == (*it
)->GetName())
132 //If we found a handler we can then use it to load the file directly
136 webKitCtrl
->m_guard
= true;
137 wxFSFile
* file
= handler
->GetFile(wxuri
);
140 webKitCtrl
->SetPage(*file
->GetStream(), wxuri
);
142 //We need to throw some sort of error here if file is NULL
143 webkit_web_policy_decision_ignore(policy_decision
);
151 wxgtk_webview_webkit_error(WebKitWebView
*,
155 wxWebViewWebKit
* webKitWindow
)
157 webKitWindow
->m_busy
= false;
158 wxWebViewNavigationError type
= wxWEB_NAV_ERR_OTHER
;
160 GError
* error
= (GError
*)web_error
;
161 wxString
description(error
->message
, wxConvUTF8
);
163 if (strcmp(g_quark_to_string(error
->domain
), "soup_http_error_quark") == 0)
167 case SOUP_STATUS_CANCELLED
:
168 type
= wxWEB_NAV_ERR_USER_CANCELLED
;
171 case SOUP_STATUS_CANT_RESOLVE
:
172 case SOUP_STATUS_NOT_FOUND
:
173 type
= wxWEB_NAV_ERR_NOT_FOUND
;
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
;
184 case SOUP_STATUS_MALFORMED
:
185 //case SOUP_STATUS_TOO_MANY_REDIRECTS:
186 type
= wxWEB_NAV_ERR_REQUEST
;
189 //case SOUP_STATUS_NO_CONTENT:
190 //case SOUP_STATUS_RESET_CONTENT:
192 case SOUP_STATUS_BAD_REQUEST
:
193 type
= wxWEB_NAV_ERR_REQUEST
;
196 case SOUP_STATUS_UNAUTHORIZED
:
197 case SOUP_STATUS_FORBIDDEN
:
198 type
= wxWEB_NAV_ERR_AUTH
;
201 case SOUP_STATUS_METHOD_NOT_ALLOWED
:
202 case SOUP_STATUS_NOT_ACCEPTABLE
:
203 type
= wxWEB_NAV_ERR_SECURITY
;
206 case SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED
:
207 type
= wxWEB_NAV_ERR_AUTH
;
210 case SOUP_STATUS_REQUEST_TIMEOUT
:
211 type
= wxWEB_NAV_ERR_CONNECTION
;
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
;
221 case SOUP_STATUS_BAD_GATEWAY
:
222 case SOUP_STATUS_SERVICE_UNAVAILABLE
:
223 case SOUP_STATUS_GATEWAY_TIMEOUT
:
224 type
= wxWEB_NAV_ERR_CONNECTION
;
227 case SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED
:
228 type
= wxWEB_NAV_ERR_REQUEST
;
230 //case SOUP_STATUS_INSUFFICIENT_STORAGE:
231 //case SOUP_STATUS_NOT_EXTENDED:
234 else if (strcmp(g_quark_to_string(error
->domain
),
235 "webkit-network-error-quark") == 0)
239 //WEBKIT_NETWORK_ERROR_FAILED:
240 //WEBKIT_NETWORK_ERROR_TRANSPORT:
242 case WEBKIT_NETWORK_ERROR_UNKNOWN_PROTOCOL
:
243 type
= wxWEB_NAV_ERR_REQUEST
;
246 case WEBKIT_NETWORK_ERROR_CANCELLED
:
247 type
= wxWEB_NAV_ERR_USER_CANCELLED
;
250 case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST
:
251 type
= wxWEB_NAV_ERR_NOT_FOUND
;
255 else if (strcmp(g_quark_to_string(error
->domain
),
256 "webkit-policy-error-quark") == 0)
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
;
270 webkit_plugin_error_quark
273 printf("Error domain %s\n", g_quark_to_string(error->domain));
277 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
,
278 webKitWindow
->GetId(),
280 event
.SetString(description
);
283 if (webKitWindow
&& webKitWindow
->GetEventHandler())
285 webKitWindow
->GetEventHandler()->ProcessEvent(event
);
292 wxgtk_webview_webkit_new_window(WebKitWebView
*,
293 WebKitWebFrame
*frame
,
294 WebKitNetworkRequest
*request
,
295 WebKitWebNavigationAction
*,
296 WebKitWebPolicyDecision
*policy_decision
,
297 wxWebViewWebKit
*webKitCtrl
)
299 const gchar
* uri
= webkit_network_request_get_uri(request
);
301 wxString target
= webkit_web_frame_get_name (frame
);
302 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
304 wxString( uri
, wxConvUTF8
),
307 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
308 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
310 //We always want the user to handle this themselves
311 webkit_web_policy_decision_ignore(policy_decision
);
316 wxgtk_webview_webkit_title_changed(WebKitWebView
*,
319 wxWebViewWebKit
*webKitCtrl
)
321 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
323 webKitCtrl
->GetCurrentURL(),
325 event
.SetString(wxString(title
, wxConvUTF8
));
327 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
328 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
333 wxgtk_webview_webkit_resource_req(WebKitWebView
*,
336 WebKitNetworkRequest
*request
,
337 WebKitNetworkResponse
*,
338 wxWebViewWebKit
*webKitCtrl
)
340 wxString uri
= webkit_network_request_get_uri(request
);
342 wxSharedPtr
<wxWebViewHandler
> handler
;
343 wxVector
<wxSharedPtr
<wxWebViewHandler
> > hanlders
= webKitCtrl
->GetHandlers();
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
)
349 if(uri
.substr(0, (*it
)->GetName().length()) == (*it
)->GetName())
354 //If we found a handler we can then use it to load the file directly
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
)
363 wxFSFile
* file
= handler
->GetFile(uri
);
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
);
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());
383 //-----------------------------------------------------------------------------
385 //-----------------------------------------------------------------------------
387 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit
, wxWebView
);
389 wxWebViewWebKit::wxWebViewWebKit()
394 bool wxWebViewWebKit::Create(wxWindow
*parent
,
400 const wxString
& name
)
406 // We currently unconditionally impose scrolling in both directions as it's
407 // necessary to show arbitrary pages.
408 style
|= wxHSCROLL
| wxVSCROLL
;
410 if (!PreCreation( parent
, pos
, size
) ||
411 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
413 wxFAIL_MSG( wxT("wxWebViewWebKit creation failed") );
417 m_web_view
= WEBKIT_WEB_VIEW(webkit_web_view_new());
418 GTKCreateScrolledWindowWith(GTK_WIDGET(m_web_view
));
419 g_object_ref(m_widget
);
421 g_signal_connect_after(m_web_view
, "navigation-policy-decision-requested",
422 G_CALLBACK(wxgtk_webview_webkit_navigation
),
424 g_signal_connect_after(m_web_view
, "load-error",
425 G_CALLBACK(wxgtk_webview_webkit_error
),
428 g_signal_connect_after(m_web_view
, "new-window-policy-decision-requested",
429 G_CALLBACK(wxgtk_webview_webkit_new_window
), this);
431 g_signal_connect_after(m_web_view
, "title-changed",
432 G_CALLBACK(wxgtk_webview_webkit_title_changed
), this);
434 g_signal_connect_after(m_web_view
, "resource-request-starting",
435 G_CALLBACK(wxgtk_webview_webkit_resource_req
), this);
437 m_parent
->DoAddChild( this );
442 webkit_web_view_load_uri(m_web_view
, url
.utf8_str());
444 //Get the initial history limit so we can enable and disable it later
445 WebKitWebBackForwardList
* history
;
446 history
= webkit_web_view_get_back_forward_list(m_web_view
);
447 m_historyLimit
= webkit_web_back_forward_list_get_limit(history
);
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
),
457 wxWebViewWebKit::~wxWebViewWebKit()
460 GTKDisconnect(m_web_view
);
463 bool wxWebViewWebKit::Enable( bool enable
)
465 if (!wxControl::Enable(enable
))
468 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget
)), enable
);
471 // GTKFixSensitivity();
477 wxWebViewWebKit::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
479 GdkWindow
* window
= gtk_widget_get_parent_window(m_widget
);
483 void wxWebViewWebKit::ZoomIn()
485 webkit_web_view_zoom_in(m_web_view
);
488 void wxWebViewWebKit::ZoomOut()
490 webkit_web_view_zoom_out(m_web_view
);
493 void wxWebViewWebKit::SetWebkitZoom(float level
)
495 webkit_web_view_set_zoom_level(m_web_view
, level
);
498 float wxWebViewWebKit::GetWebkitZoom() const
500 return webkit_web_view_get_zoom_level(m_web_view
);
503 void wxWebViewWebKit::Stop()
505 webkit_web_view_stop_loading(m_web_view
);
508 void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags
)
510 if (flags
& wxWEB_VIEW_RELOAD_NO_CACHE
)
512 webkit_web_view_reload_bypass_cache(m_web_view
);
516 webkit_web_view_reload(m_web_view
);
520 void wxWebViewWebKit::LoadURL(const wxString
& url
)
522 webkit_web_view_load_uri(m_web_view
, wxGTK_CONV(url
));
526 void wxWebViewWebKit::GoBack()
528 webkit_web_view_go_back(m_web_view
);
531 void wxWebViewWebKit::GoForward()
533 webkit_web_view_go_forward(m_web_view
);
537 bool wxWebViewWebKit::CanGoBack() const
539 return webkit_web_view_can_go_back(m_web_view
);
543 bool wxWebViewWebKit::CanGoForward() const
545 return webkit_web_view_can_go_forward(m_web_view
);
548 void wxWebViewWebKit::ClearHistory()
550 WebKitWebBackForwardList
* history
;
551 history
= webkit_web_view_get_back_forward_list(m_web_view
);
552 webkit_web_back_forward_list_clear(history
);
555 void wxWebViewWebKit::EnableHistory(bool enable
)
557 WebKitWebBackForwardList
* history
;
558 history
= webkit_web_view_get_back_forward_list(m_web_view
);
561 webkit_web_back_forward_list_set_limit(history
, m_historyLimit
);
565 webkit_web_back_forward_list_set_limit(history
, 0);
569 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewWebKit::GetBackwardHistory()
571 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > backhist
;
572 WebKitWebBackForwardList
* history
;
573 history
= webkit_web_view_get_back_forward_list(m_web_view
);
574 GList
* list
= webkit_web_back_forward_list_get_back_list_with_limit(history
,
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
--)
579 WebKitWebHistoryItem
* gtkitem
= (WebKitWebHistoryItem
*)g_list_nth_data(list
, i
);
580 wxWebViewHistoryItem
* wxitem
= new wxWebViewHistoryItem(
581 webkit_web_history_item_get_uri(gtkitem
),
582 webkit_web_history_item_get_title(gtkitem
));
583 wxitem
->m_histItem
= gtkitem
;
584 wxSharedPtr
<wxWebViewHistoryItem
> item(wxitem
);
585 backhist
.push_back(item
);
590 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewWebKit::GetForwardHistory()
592 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forwardhist
;
593 WebKitWebBackForwardList
* history
;
594 history
= webkit_web_view_get_back_forward_list(m_web_view
);
595 GList
* list
= webkit_web_back_forward_list_get_forward_list_with_limit(history
,
597 for(guint i
= 0; i
< g_list_length(list
); i
++)
599 WebKitWebHistoryItem
* gtkitem
= (WebKitWebHistoryItem
*)g_list_nth_data(list
, i
);
600 wxWebViewHistoryItem
* wxitem
= new wxWebViewHistoryItem(
601 webkit_web_history_item_get_uri(gtkitem
),
602 webkit_web_history_item_get_title(gtkitem
));
603 wxitem
->m_histItem
= gtkitem
;
604 wxSharedPtr
<wxWebViewHistoryItem
> item(wxitem
);
605 forwardhist
.push_back(item
);
610 void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
)
612 WebKitWebHistoryItem
* gtkitem
= (WebKitWebHistoryItem
*)item
->m_histItem
;
615 webkit_web_view_go_to_back_forward_item(m_web_view
,
616 WEBKIT_WEB_HISTORY_ITEM(gtkitem
));
620 bool wxWebViewWebKit::CanCut() const
622 return webkit_web_view_can_cut_clipboard(m_web_view
);
625 bool wxWebViewWebKit::CanCopy() const
627 return webkit_web_view_can_copy_clipboard(m_web_view
);
630 bool wxWebViewWebKit::CanPaste() const
632 return webkit_web_view_can_paste_clipboard(m_web_view
);
635 void wxWebViewWebKit::Cut()
637 webkit_web_view_cut_clipboard(m_web_view
);
640 void wxWebViewWebKit::Copy()
642 webkit_web_view_copy_clipboard(m_web_view
);
645 void wxWebViewWebKit::Paste()
647 webkit_web_view_paste_clipboard(m_web_view
);
650 bool wxWebViewWebKit::CanUndo() const
652 return webkit_web_view_can_undo(m_web_view
);
655 bool wxWebViewWebKit::CanRedo() const
657 return webkit_web_view_can_redo(m_web_view
);
660 void wxWebViewWebKit::Undo()
662 webkit_web_view_undo(m_web_view
);
665 void wxWebViewWebKit::Redo()
667 webkit_web_view_redo(m_web_view
);
670 wxString
wxWebViewWebKit::GetCurrentURL() const
672 // FIXME: check which encoding the web kit control uses instead of
673 // assuming UTF8 (here and elsewhere too)
674 return wxString::FromUTF8(webkit_web_view_get_uri(m_web_view
));
678 wxString
wxWebViewWebKit::GetCurrentTitle() const
680 return wxString::FromUTF8(webkit_web_view_get_title(m_web_view
));
684 wxString
wxWebViewWebKit::GetPageSource() const
686 WebKitWebFrame
* frame
= webkit_web_view_get_main_frame(m_web_view
);
687 WebKitWebDataSource
* src
= webkit_web_frame_get_data_source (frame
);
689 // TODO: check encoding with
691 // webkit_web_data_source_get_encoding(WebKitWebDataSource *data_source);
692 return wxString(webkit_web_data_source_get_data (src
)->str
, wxConvUTF8
);
696 wxWebViewZoom
wxWebViewWebKit::GetZoom() const
698 float zoom
= GetWebkitZoom();
700 // arbitrary way to map float zoom to our common zoom enum
703 return wxWEB_VIEW_ZOOM_TINY
;
705 else if (zoom
> 0.65 && zoom
<= 0.90)
707 return wxWEB_VIEW_ZOOM_SMALL
;
709 else if (zoom
> 0.90 && zoom
<= 1.15)
711 return wxWEB_VIEW_ZOOM_MEDIUM
;
713 else if (zoom
> 1.15 && zoom
<= 1.45)
715 return wxWEB_VIEW_ZOOM_LARGE
;
717 else if (zoom
> 1.45)
719 return wxWEB_VIEW_ZOOM_LARGEST
;
722 // to shut up compilers, this can never be reached logically
724 return wxWEB_VIEW_ZOOM_MEDIUM
;
728 void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom
)
730 // arbitrary way to map our common zoom enum to float zoom
733 case wxWEB_VIEW_ZOOM_TINY
:
737 case wxWEB_VIEW_ZOOM_SMALL
:
741 case wxWEB_VIEW_ZOOM_MEDIUM
:
745 case wxWEB_VIEW_ZOOM_LARGE
:
749 case wxWEB_VIEW_ZOOM_LARGEST
:
758 void wxWebViewWebKit::SetZoomType(wxWebViewZoomType type
)
760 webkit_web_view_set_full_content_zoom(m_web_view
,
761 (type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
?
765 wxWebViewZoomType
wxWebViewWebKit::GetZoomType() const
767 gboolean fczoom
= webkit_web_view_get_full_content_zoom(m_web_view
);
769 if (fczoom
) return wxWEB_VIEW_ZOOM_TYPE_LAYOUT
;
770 else return wxWEB_VIEW_ZOOM_TYPE_TEXT
;
773 bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType
) const
775 // this port supports all zoom types
779 void wxWebViewWebKit::DoSetPage(const wxString
& html
, const wxString
& baseUri
)
781 webkit_web_view_load_string (m_web_view
,
782 html
.mb_str(wxConvUTF8
),
785 baseUri
.mb_str(wxConvUTF8
));
788 void wxWebViewWebKit::Print()
790 WebKitWebFrame
* frame
= webkit_web_view_get_main_frame(m_web_view
);
791 webkit_web_frame_print (frame
);
793 // GtkPrintOperationResult webkit_web_frame_print_full
794 // (WebKitWebFrame *frame,
795 // GtkPrintOperation *operation,
796 // GtkPrintOperationAction action,
802 bool wxWebViewWebKit::IsBusy() const
806 // This code looks nice but returns true after a page was cancelled
808 WebKitLoadStatus status = webkit_web_view_get_load_status
809 (WEBKIT_WEB_VIEW(web_view));
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)
819 if (status == WEBKIT_LOAD_FINISHED)
828 void wxWebViewWebKit::SetEditable(bool enable
)
830 webkit_web_view_set_editable(m_web_view
, enable
);
833 bool wxWebViewWebKit::IsEditable() const
835 return webkit_web_view_get_editable(m_web_view
);
838 void wxWebViewWebKit::DeleteSelection()
840 webkit_web_view_delete_selection(m_web_view
);
843 bool wxWebViewWebKit::HasSelection() const
845 return webkit_web_view_has_selection(m_web_view
);
848 void wxWebViewWebKit::SelectAll()
850 webkit_web_view_select_all(m_web_view
);
853 wxString
wxWebViewWebKit::GetSelectedText() const
855 WebKitDOMDocument
* doc
;
856 WebKitDOMDOMWindow
* win
;
857 WebKitDOMDOMSelection
* sel
;
858 WebKitDOMRange
* range
;
860 doc
= webkit_web_view_get_dom_document(m_web_view
);
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
));
863 range
= webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel
),
865 return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range
)),
869 wxString
wxWebViewWebKit::GetSelectedSource() const
871 WebKitDOMDocument
* doc
;
872 WebKitDOMDOMWindow
* win
;
873 WebKitDOMDOMSelection
* sel
;
874 WebKitDOMRange
* range
;
875 WebKitDOMElement
* div
;
876 WebKitDOMDocumentFragment
* clone
;
877 WebKitDOMHTMLElement
* html
;
879 doc
= webkit_web_view_get_dom_document(m_web_view
);
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
));
882 range
= webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel
),
884 div
= webkit_dom_document_create_element(WEBKIT_DOM_DOCUMENT(doc
), "div", NULL
);
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
;
890 return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html
)),
894 void wxWebViewWebKit::ClearSelection()
896 WebKitDOMDocument
* doc
;
897 WebKitDOMDOMWindow
* win
;
898 WebKitDOMDOMSelection
* sel
;
900 doc
= webkit_web_view_get_dom_document(m_web_view
);
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
));
907 wxString
wxWebViewWebKit::GetPageText() const
909 WebKitDOMDocument
* doc
;
910 WebKitDOMHTMLElement
* body
;
912 doc
= webkit_web_view_get_dom_document(m_web_view
);
913 body
= webkit_dom_document_get_body(WEBKIT_DOM_DOCUMENT(doc
));
914 return wxString(webkit_dom_html_element_get_inner_text(WEBKIT_DOM_HTML_ELEMENT(body
)),
918 void wxWebViewWebKit::RunScript(const wxString
& javascript
)
920 webkit_web_view_execute_script(m_web_view
,
921 javascript
.mb_str(wxConvUTF8
));
924 void wxWebViewWebKit::RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
)
926 m_handlerList
.push_back(handler
);
929 long wxWebViewWebKit::Find(const wxString
& text
, int flags
)
931 bool newSearch
= false;
932 if(text
!= m_findText
||
933 (flags
& wxWEB_VIEW_FIND_MATCH_CASE
) != (m_findFlags
& wxWEB_VIEW_FIND_MATCH_CASE
))
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);
944 //If the search string is empty then we clear any selection and highlight
947 webkit_web_view_unmark_text_matches(m_web_view
);
948 webkit_web_view_set_highlight_text_matches(m_web_view
, false);
953 bool wrap
= false, matchCase
= false, forward
= true;
954 if(flags
& wxWEB_VIEW_FIND_WRAP
)
956 if(flags
& wxWEB_VIEW_FIND_MATCH_CASE
)
958 if(flags
& wxWEB_VIEW_FIND_BACKWARDS
)
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
975 if(m_findPosition
< 0)
976 m_findPosition
+= m_findCount
;
977 if(m_findPosition
> m_findCount
)
978 m_findPosition
-= m_findCount
;
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
);
985 if(!webkit_web_view_search_text(m_web_view
, wxGTK_CONV(text
), matchCase
, forward
, wrap
))
991 wxLogMessage(wxString::Format("Returning %d", m_findPosition
));
992 return newSearch
? m_findCount
: m_findPosition
;
995 void wxWebViewWebKit::FindClear()
1000 m_findPosition
= -1;
1005 wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1007 return GetDefaultAttributesFromGTKWidget(webkit_web_view_new());
1011 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT