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"
21 #include <webkit/webkit.h>
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
31 wxgtk_webview_webkit_load_status(GtkWidget
* widget
,
33 wxWebViewWebKit
*webKitCtrl
)
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() )
40 wxString url
= webKitCtrl
->GetCurrentURL();
42 WebKitLoadStatus status
;
43 g_object_get(G_OBJECT(widget
), "load-status", &status
, NULL
);
45 wxString target
; // TODO: get target (if possible)
47 if (status
== WEBKIT_LOAD_FINISHED
)
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
)
57 newitem
= webkit_web_history_item_new_with_data
60 webKitCtrl
->GetCurrentTitle().utf8_str()
62 webkit_web_back_forward_list_add_item(hist
, newitem
);
65 webKitCtrl
->m_busy
= false;
66 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
,
70 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
71 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
73 else if (status
== WEBKIT_LOAD_COMMITTED
)
75 webKitCtrl
->m_busy
= true;
76 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
80 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
81 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
86 wxgtk_webview_webkit_navigation(WebKitWebView
*,
87 WebKitWebFrame
*frame
,
88 WebKitNetworkRequest
*request
,
89 WebKitWebNavigationAction
*,
90 WebKitWebPolicyDecision
*policy_decision
,
91 wxWebViewWebKit
*webKitCtrl
)
93 if(webKitCtrl
->m_guard
)
95 webKitCtrl
->m_guard
= false;
96 //We set this to make sure that we don't try to load the page again from
97 //the resource request callback
98 webKitCtrl
->m_vfsurl
= webkit_network_request_get_uri(request
);
99 webkit_web_policy_decision_use(policy_decision
);
103 webKitCtrl
->m_busy
= true;
105 const gchar
* uri
= webkit_network_request_get_uri(request
);
107 wxString target
= webkit_web_frame_get_name (frame
);
108 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
110 wxString( uri
, wxConvUTF8
),
113 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
114 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
116 if (!event
.IsAllowed())
118 webKitCtrl
->m_busy
= false;
119 webkit_web_policy_decision_ignore(policy_decision
);
124 wxString wxuri
= uri
;
125 wxSharedPtr
<wxWebViewHandler
> handler
;
126 wxVector
<wxSharedPtr
<wxWebViewHandler
> > hanlders
= webKitCtrl
->GetHandlers();
127 //We are not vetoed so see if we match one of the additional handlers
128 for(wxVector
<wxSharedPtr
<wxWebViewHandler
> >::iterator it
= hanlders
.begin();
129 it
!= hanlders
.end(); ++it
)
131 if(wxuri
.substr(0, (*it
)->GetName().length()) == (*it
)->GetName())
136 //If we found a handler we can then use it to load the file directly
140 webKitCtrl
->m_guard
= true;
141 wxFSFile
* file
= handler
->GetFile(wxuri
);
144 webKitCtrl
->SetPage(*file
->GetStream(), wxuri
);
146 //We need to throw some sort of error here if file is NULL
147 webkit_web_policy_decision_ignore(policy_decision
);
155 wxgtk_webview_webkit_error(WebKitWebView
*,
159 wxWebViewWebKit
* webKitWindow
)
161 webKitWindow
->m_busy
= false;
162 wxWebViewNavigationError type
= wxWEB_NAV_ERR_OTHER
;
164 GError
* error
= (GError
*)web_error
;
165 wxString
description(error
->message
, wxConvUTF8
);
167 if (strcmp(g_quark_to_string(error
->domain
), "soup_http_error_quark") == 0)
171 case SOUP_STATUS_CANCELLED
:
172 type
= wxWEB_NAV_ERR_USER_CANCELLED
;
175 case SOUP_STATUS_CANT_RESOLVE
:
176 case SOUP_STATUS_NOT_FOUND
:
177 type
= wxWEB_NAV_ERR_NOT_FOUND
;
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
;
188 case SOUP_STATUS_MALFORMED
:
189 //case SOUP_STATUS_TOO_MANY_REDIRECTS:
190 type
= wxWEB_NAV_ERR_REQUEST
;
193 //case SOUP_STATUS_NO_CONTENT:
194 //case SOUP_STATUS_RESET_CONTENT:
196 case SOUP_STATUS_BAD_REQUEST
:
197 type
= wxWEB_NAV_ERR_REQUEST
;
200 case SOUP_STATUS_UNAUTHORIZED
:
201 case SOUP_STATUS_FORBIDDEN
:
202 type
= wxWEB_NAV_ERR_AUTH
;
205 case SOUP_STATUS_METHOD_NOT_ALLOWED
:
206 case SOUP_STATUS_NOT_ACCEPTABLE
:
207 type
= wxWEB_NAV_ERR_SECURITY
;
210 case SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED
:
211 type
= wxWEB_NAV_ERR_AUTH
;
214 case SOUP_STATUS_REQUEST_TIMEOUT
:
215 type
= wxWEB_NAV_ERR_CONNECTION
;
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
;
225 case SOUP_STATUS_BAD_GATEWAY
:
226 case SOUP_STATUS_SERVICE_UNAVAILABLE
:
227 case SOUP_STATUS_GATEWAY_TIMEOUT
:
228 type
= wxWEB_NAV_ERR_CONNECTION
;
231 case SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED
:
232 type
= wxWEB_NAV_ERR_REQUEST
;
234 //case SOUP_STATUS_INSUFFICIENT_STORAGE:
235 //case SOUP_STATUS_NOT_EXTENDED:
238 else if (strcmp(g_quark_to_string(error
->domain
),
239 "webkit-network-error-quark") == 0)
243 //WEBKIT_NETWORK_ERROR_FAILED:
244 //WEBKIT_NETWORK_ERROR_TRANSPORT:
246 case WEBKIT_NETWORK_ERROR_UNKNOWN_PROTOCOL
:
247 type
= wxWEB_NAV_ERR_REQUEST
;
250 case WEBKIT_NETWORK_ERROR_CANCELLED
:
251 type
= wxWEB_NAV_ERR_USER_CANCELLED
;
254 case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST
:
255 type
= wxWEB_NAV_ERR_NOT_FOUND
;
259 else if (strcmp(g_quark_to_string(error
->domain
),
260 "webkit-policy-error-quark") == 0)
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
;
274 webkit_plugin_error_quark
277 printf("Error domain %s\n", g_quark_to_string(error->domain));
281 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
,
282 webKitWindow
->GetId(),
284 event
.SetString(description
);
287 if (webKitWindow
&& webKitWindow
->GetEventHandler())
289 webKitWindow
->GetEventHandler()->ProcessEvent(event
);
296 wxgtk_webview_webkit_new_window(WebKitWebView
*,
297 WebKitWebFrame
*frame
,
298 WebKitNetworkRequest
*request
,
299 WebKitWebNavigationAction
*,
300 WebKitWebPolicyDecision
*policy_decision
,
301 wxWebViewWebKit
*webKitCtrl
)
303 const gchar
* uri
= webkit_network_request_get_uri(request
);
305 wxString target
= webkit_web_frame_get_name (frame
);
306 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
308 wxString( uri
, wxConvUTF8
),
311 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
312 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
314 //We always want the user to handle this themselves
315 webkit_web_policy_decision_ignore(policy_decision
);
320 wxgtk_webview_webkit_title_changed(WebKitWebView
*,
323 wxWebViewWebKit
*webKitCtrl
)
325 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
327 webKitCtrl
->GetCurrentURL(),
329 event
.SetString(wxString(title
, wxConvUTF8
));
331 if (webKitCtrl
&& webKitCtrl
->GetEventHandler())
332 webKitCtrl
->GetEventHandler()->ProcessEvent(event
);
337 wxgtk_webview_webkit_resource_req(WebKitWebView
*,
340 WebKitNetworkRequest
*request
,
341 WebKitNetworkResponse
*,
342 wxWebViewWebKit
*webKitCtrl
)
344 wxString uri
= webkit_network_request_get_uri(request
);
346 wxSharedPtr
<wxWebViewHandler
> handler
;
347 wxVector
<wxSharedPtr
<wxWebViewHandler
> > hanlders
= webKitCtrl
->GetHandlers();
349 //We are not vetoed so see if we match one of the additional handlers
350 for(wxVector
<wxSharedPtr
<wxWebViewHandler
> >::iterator it
= hanlders
.begin();
351 it
!= hanlders
.end(); ++it
)
353 if(uri
.substr(0, (*it
)->GetName().length()) == (*it
)->GetName())
358 //If we found a handler we can then use it to load the file directly
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
)
367 wxFSFile
* file
= handler
->GetFile(uri
);
370 //We load the data into a data url to save it being written out again
371 size_t size
= file
->GetStream()->GetLength();
372 char *buffer
= new char[size
];
373 file
->GetStream()->Read(buffer
, size
);
374 wxString data
= wxBase64Encode(buffer
, size
);
376 wxString mime
= file
->GetMimeType();
377 wxString path
= "data:" + mime
+ ";base64," + data
;
378 //Then we can redirect the call
379 webkit_network_request_set_uri(request
, path
.utf8_str());
387 //-----------------------------------------------------------------------------
389 //-----------------------------------------------------------------------------
391 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit
, wxWebView
);
393 bool wxWebViewWebKit::Create(wxWindow
*parent
,
399 const wxString
& name
)
405 // We currently unconditionally impose scrolling in both directions as it's
406 // necessary to show arbitrary pages.
407 style
|= wxHSCROLL
| wxVSCROLL
;
409 if (!PreCreation( parent
, pos
, size
) ||
410 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
412 wxFAIL_MSG( wxT("wxWebViewWebKit creation failed") );
416 m_web_view
= WEBKIT_WEB_VIEW(webkit_web_view_new());
417 GTKCreateScrolledWindowWith(GTK_WIDGET(m_web_view
));
418 g_object_ref(m_widget
);
420 g_signal_connect_after(m_web_view
, "navigation-policy-decision-requested",
421 G_CALLBACK(wxgtk_webview_webkit_navigation
),
423 g_signal_connect_after(m_web_view
, "load-error",
424 G_CALLBACK(wxgtk_webview_webkit_error
),
427 g_signal_connect_after(m_web_view
, "new-window-policy-decision-requested",
428 G_CALLBACK(wxgtk_webview_webkit_new_window
), this);
430 g_signal_connect_after(m_web_view
, "title-changed",
431 G_CALLBACK(wxgtk_webview_webkit_title_changed
), this);
433 g_signal_connect_after(m_web_view
, "resource-request-starting",
434 G_CALLBACK(wxgtk_webview_webkit_resource_req
), this);
436 m_parent
->DoAddChild( this );
441 webkit_web_view_load_uri(m_web_view
, url
.utf8_str());
443 //Get the initial history limit so we can enable and disable it later
444 WebKitWebBackForwardList
* history
;
445 history
= webkit_web_view_get_back_forward_list(m_web_view
);
446 m_historyLimit
= webkit_web_back_forward_list_get_limit(history
);
448 // last to avoid getting signal too early
449 g_signal_connect_after(m_web_view
, "notify::load-status",
450 G_CALLBACK(wxgtk_webview_webkit_load_status
),
456 wxWebViewWebKit::~wxWebViewWebKit()
458 // The main goal here is to set m_isBeingDeleted to true to avoid the use
459 // of this -- already half-destroyed -- object from WebKit callbacks, but
460 // just setting it would prevent wxWindowDestroyEvent from being sent, so
461 // send it now instead.
465 bool wxWebViewWebKit::Enable( bool enable
)
467 if (!wxControl::Enable(enable
))
470 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget
)), enable
);
473 // GTKFixSensitivity();
479 wxWebViewWebKit::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
481 GdkWindow
* window
= gtk_widget_get_parent_window(m_widget
);
485 void wxWebViewWebKit::ZoomIn()
487 webkit_web_view_zoom_in(m_web_view
);
490 void wxWebViewWebKit::ZoomOut()
492 webkit_web_view_zoom_out(m_web_view
);
495 void wxWebViewWebKit::SetWebkitZoom(float level
)
497 webkit_web_view_set_zoom_level(m_web_view
, level
);
500 float wxWebViewWebKit::GetWebkitZoom() const
502 return webkit_web_view_get_zoom_level(m_web_view
);
505 void wxWebViewWebKit::Stop()
507 webkit_web_view_stop_loading(m_web_view
);
510 void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags
)
512 if (flags
& wxWEB_VIEW_RELOAD_NO_CACHE
)
514 webkit_web_view_reload_bypass_cache(m_web_view
);
518 webkit_web_view_reload(m_web_view
);
522 void wxWebViewWebKit::LoadURL(const wxString
& url
)
524 webkit_web_view_load_uri(m_web_view
, wxGTK_CONV(url
));
528 void wxWebViewWebKit::GoBack()
530 webkit_web_view_go_back(m_web_view
);
533 void wxWebViewWebKit::GoForward()
535 webkit_web_view_go_forward(m_web_view
);
539 bool wxWebViewWebKit::CanGoBack() const
541 return webkit_web_view_can_go_back(m_web_view
);
545 bool wxWebViewWebKit::CanGoForward() const
547 return webkit_web_view_can_go_forward(m_web_view
);
550 void wxWebViewWebKit::ClearHistory()
552 WebKitWebBackForwardList
* history
;
553 history
= webkit_web_view_get_back_forward_list(m_web_view
);
554 webkit_web_back_forward_list_clear(history
);
557 void wxWebViewWebKit::EnableHistory(bool enable
)
559 WebKitWebBackForwardList
* history
;
560 history
= webkit_web_view_get_back_forward_list(m_web_view
);
563 webkit_web_back_forward_list_set_limit(history
, m_historyLimit
);
567 webkit_web_back_forward_list_set_limit(history
, 0);
571 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewWebKit::GetBackwardHistory()
573 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > backhist
;
574 WebKitWebBackForwardList
* history
;
575 history
= webkit_web_view_get_back_forward_list(m_web_view
);
576 GList
* list
= webkit_web_back_forward_list_get_back_list_with_limit(history
,
578 //We need to iterate in reverse to get the order we desire
579 for(int i
= g_list_length(list
) - 1; i
>= 0 ; i
--)
581 WebKitWebHistoryItem
* gtkitem
= (WebKitWebHistoryItem
*)g_list_nth_data(list
, i
);
582 wxWebViewHistoryItem
* wxitem
= new wxWebViewHistoryItem(
583 webkit_web_history_item_get_uri(gtkitem
),
584 webkit_web_history_item_get_title(gtkitem
));
585 wxitem
->m_histItem
= gtkitem
;
586 wxSharedPtr
<wxWebViewHistoryItem
> item(wxitem
);
587 backhist
.push_back(item
);
592 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewWebKit::GetForwardHistory()
594 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forwardhist
;
595 WebKitWebBackForwardList
* history
;
596 history
= webkit_web_view_get_back_forward_list(m_web_view
);
597 GList
* list
= webkit_web_back_forward_list_get_forward_list_with_limit(history
,
599 for(guint i
= 0; i
< g_list_length(list
); i
++)
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 forwardhist
.push_back(item
);
612 void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
)
614 WebKitWebHistoryItem
* gtkitem
= (WebKitWebHistoryItem
*)item
->m_histItem
;
617 webkit_web_view_go_to_back_forward_item(m_web_view
,
618 WEBKIT_WEB_HISTORY_ITEM(gtkitem
));
622 bool wxWebViewWebKit::CanCut() const
624 return webkit_web_view_can_cut_clipboard(m_web_view
);
627 bool wxWebViewWebKit::CanCopy() const
629 return webkit_web_view_can_copy_clipboard(m_web_view
);
632 bool wxWebViewWebKit::CanPaste() const
634 return webkit_web_view_can_paste_clipboard(m_web_view
);
637 void wxWebViewWebKit::Cut()
639 webkit_web_view_cut_clipboard(m_web_view
);
642 void wxWebViewWebKit::Copy()
644 webkit_web_view_copy_clipboard(m_web_view
);
647 void wxWebViewWebKit::Paste()
649 webkit_web_view_paste_clipboard(m_web_view
);
652 bool wxWebViewWebKit::CanUndo() const
654 return webkit_web_view_can_undo(m_web_view
);
657 bool wxWebViewWebKit::CanRedo() const
659 return webkit_web_view_can_redo(m_web_view
);
662 void wxWebViewWebKit::Undo()
664 webkit_web_view_undo(m_web_view
);
667 void wxWebViewWebKit::Redo()
669 webkit_web_view_redo(m_web_view
);
672 wxString
wxWebViewWebKit::GetCurrentURL() const
674 // FIXME: check which encoding the web kit control uses instead of
675 // assuming UTF8 (here and elsewhere too)
676 return wxString::FromUTF8(webkit_web_view_get_uri(m_web_view
));
680 wxString
wxWebViewWebKit::GetCurrentTitle() const
682 return wxString::FromUTF8(webkit_web_view_get_title(m_web_view
));
686 wxString
wxWebViewWebKit::GetPageSource() const
688 WebKitWebFrame
* frame
= webkit_web_view_get_main_frame(m_web_view
);
689 WebKitWebDataSource
* src
= webkit_web_frame_get_data_source (frame
);
691 // TODO: check encoding with
693 // webkit_web_data_source_get_encoding(WebKitWebDataSource *data_source);
694 return wxString(webkit_web_data_source_get_data (src
)->str
, wxConvUTF8
);
698 wxWebViewZoom
wxWebViewWebKit::GetZoom() const
700 float zoom
= GetWebkitZoom();
702 // arbitrary way to map float zoom to our common zoom enum
705 return wxWEB_VIEW_ZOOM_TINY
;
707 else if (zoom
> 0.65 && zoom
<= 0.90)
709 return wxWEB_VIEW_ZOOM_SMALL
;
711 else if (zoom
> 0.90 && zoom
<= 1.15)
713 return wxWEB_VIEW_ZOOM_MEDIUM
;
715 else if (zoom
> 1.15 && zoom
<= 1.45)
717 return wxWEB_VIEW_ZOOM_LARGE
;
719 else if (zoom
> 1.45)
721 return wxWEB_VIEW_ZOOM_LARGEST
;
724 // to shut up compilers, this can never be reached logically
726 return wxWEB_VIEW_ZOOM_MEDIUM
;
730 void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom
)
732 // arbitrary way to map our common zoom enum to float zoom
735 case wxWEB_VIEW_ZOOM_TINY
:
739 case wxWEB_VIEW_ZOOM_SMALL
:
743 case wxWEB_VIEW_ZOOM_MEDIUM
:
747 case wxWEB_VIEW_ZOOM_LARGE
:
751 case wxWEB_VIEW_ZOOM_LARGEST
:
760 void wxWebViewWebKit::SetZoomType(wxWebViewZoomType type
)
762 webkit_web_view_set_full_content_zoom(m_web_view
,
763 (type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
?
767 wxWebViewZoomType
wxWebViewWebKit::GetZoomType() const
769 gboolean fczoom
= webkit_web_view_get_full_content_zoom(m_web_view
);
771 if (fczoom
) return wxWEB_VIEW_ZOOM_TYPE_LAYOUT
;
772 else return wxWEB_VIEW_ZOOM_TYPE_TEXT
;
775 bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType
) const
777 // this port supports all zoom types
781 void wxWebViewWebKit::DoSetPage(const wxString
& html
, const wxString
& baseUri
)
783 webkit_web_view_load_string (m_web_view
,
784 html
.mb_str(wxConvUTF8
),
787 baseUri
.mb_str(wxConvUTF8
));
790 void wxWebViewWebKit::Print()
792 WebKitWebFrame
* frame
= webkit_web_view_get_main_frame(m_web_view
);
793 webkit_web_frame_print (frame
);
795 // GtkPrintOperationResult webkit_web_frame_print_full
796 // (WebKitWebFrame *frame,
797 // GtkPrintOperation *operation,
798 // GtkPrintOperationAction action,
804 bool wxWebViewWebKit::IsBusy() const
808 // This code looks nice but returns true after a page was cancelled
810 WebKitLoadStatus status = webkit_web_view_get_load_status
811 (WEBKIT_WEB_VIEW(web_view));
814 #if WEBKIT_CHECK_VERSION(1,1,16)
815 // WEBKIT_LOAD_FAILED is new in webkit 1.1.16
816 if (status == WEBKIT_LOAD_FAILED)
821 if (status == WEBKIT_LOAD_FINISHED)
830 void wxWebViewWebKit::SetEditable(bool enable
)
832 webkit_web_view_set_editable(m_web_view
, enable
);
835 bool wxWebViewWebKit::IsEditable() const
837 return webkit_web_view_get_editable(m_web_view
);
840 void wxWebViewWebKit::DeleteSelection()
842 webkit_web_view_delete_selection(m_web_view
);
845 bool wxWebViewWebKit::HasSelection() const
847 return webkit_web_view_has_selection(m_web_view
);
850 void wxWebViewWebKit::SelectAll()
852 webkit_web_view_select_all(m_web_view
);
855 wxString
wxWebViewWebKit::GetSelectedText() const
857 WebKitDOMDocument
* doc
;
858 WebKitDOMDOMWindow
* win
;
859 WebKitDOMDOMSelection
* sel
;
860 WebKitDOMRange
* range
;
862 doc
= webkit_web_view_get_dom_document(m_web_view
);
863 win
= webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc
));
864 sel
= webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win
));
865 range
= webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel
),
867 return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range
)),
871 wxString
wxWebViewWebKit::GetSelectedSource() const
873 WebKitDOMDocument
* doc
;
874 WebKitDOMDOMWindow
* win
;
875 WebKitDOMDOMSelection
* sel
;
876 WebKitDOMRange
* range
;
877 WebKitDOMElement
* div
;
878 WebKitDOMDocumentFragment
* clone
;
879 WebKitDOMHTMLElement
* html
;
881 doc
= webkit_web_view_get_dom_document(m_web_view
);
882 win
= webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc
));
883 sel
= webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win
));
884 range
= webkit_dom_dom_selection_get_range_at(WEBKIT_DOM_DOM_SELECTION(sel
),
886 div
= webkit_dom_document_create_element(WEBKIT_DOM_DOCUMENT(doc
), "div", NULL
);
888 clone
= webkit_dom_range_clone_contents(WEBKIT_DOM_RANGE(range
), NULL
);
889 webkit_dom_node_append_child(&div
->parent_instance
, &clone
->parent_instance
, NULL
);
890 html
= (WebKitDOMHTMLElement
*)div
;
892 return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html
)),
896 void wxWebViewWebKit::ClearSelection()
898 WebKitDOMDocument
* doc
;
899 WebKitDOMDOMWindow
* win
;
900 WebKitDOMDOMSelection
* sel
;
902 doc
= webkit_web_view_get_dom_document(m_web_view
);
903 win
= webkit_dom_document_get_default_view(WEBKIT_DOM_DOCUMENT(doc
));
904 sel
= webkit_dom_dom_window_get_selection(WEBKIT_DOM_DOM_WINDOW(win
));
905 webkit_dom_dom_selection_remove_all_ranges(WEBKIT_DOM_DOM_SELECTION(sel
));
909 wxString
wxWebViewWebKit::GetPageText() const
911 WebKitDOMDocument
* doc
;
912 WebKitDOMHTMLElement
* body
;
914 doc
= webkit_web_view_get_dom_document(m_web_view
);
915 body
= webkit_dom_document_get_body(WEBKIT_DOM_DOCUMENT(doc
));
916 return wxString(webkit_dom_html_element_get_inner_text(WEBKIT_DOM_HTML_ELEMENT(body
)),
920 void wxWebViewWebKit::RunScript(const wxString
& javascript
)
922 webkit_web_view_execute_script(m_web_view
,
923 javascript
.mb_str(wxConvUTF8
));
926 void wxWebViewWebKit::RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
)
928 m_handlerList
.push_back(handler
);
931 long wxWebViewWebKit::Find(const wxString
& text
, int flags
)
933 bool newSearch
= false;
934 if(text
!= m_findText
||
935 (flags
& wxWEB_VIEW_FIND_MATCH_CASE
) != (m_findFlags
& wxWEB_VIEW_FIND_MATCH_CASE
))
938 //If it is a new search we need to clear existing highlights
939 webkit_web_view_unmark_text_matches(m_web_view
);
940 webkit_web_view_set_highlight_text_matches(m_web_view
, false);
946 //If the search string is empty then we clear any selection and highlight
949 webkit_web_view_unmark_text_matches(m_web_view
);
950 webkit_web_view_set_highlight_text_matches(m_web_view
, false);
955 bool wrap
= false, matchCase
= false, forward
= true;
956 if(flags
& wxWEB_VIEW_FIND_WRAP
)
958 if(flags
& wxWEB_VIEW_FIND_MATCH_CASE
)
960 if(flags
& wxWEB_VIEW_FIND_BACKWARDS
)
965 //Initially we mark the matches to know how many we have
966 m_findCount
= webkit_web_view_mark_text_matches(m_web_view
, wxGTK_CONV(text
), matchCase
, 0);
967 //In this case we return early to match IE behaviour
977 if(m_findPosition
< 0)
978 m_findPosition
+= m_findCount
;
979 if(m_findPosition
> m_findCount
)
980 m_findPosition
-= m_findCount
;
983 //Highlight them if needed
984 bool highlight
= flags
& wxWEB_VIEW_FIND_HIGHLIGHT_RESULT
? true : false;
985 webkit_web_view_set_highlight_text_matches(m_web_view
, highlight
);
987 if(!webkit_web_view_search_text(m_web_view
, wxGTK_CONV(text
), matchCase
, forward
, wrap
))
993 wxLogMessage(wxString::Format("Returning %d", m_findPosition
));
994 return newSearch
? m_findCount
: m_findPosition
;
997 void wxWebViewWebKit::FindClear()
1002 m_findPosition
= -1;
1007 wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1009 return GetDefaultAttributesFromGTKWidget(webkit_web_view_new
);
1013 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT