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_WEBVIEW_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_WEBVIEW_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_WEBVIEW_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
= wxWEBVIEW_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
= wxWEBVIEW_NAV_ERR_USER_CANCELLED
;
171 case SOUP_STATUS_CANT_RESOLVE
:
172 case SOUP_STATUS_NOT_FOUND
:
173 type
= wxWEBVIEW_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
= wxWEBVIEW_NAV_ERR_CONNECTION
;
184 case SOUP_STATUS_MALFORMED
:
185 //case SOUP_STATUS_TOO_MANY_REDIRECTS:
186 type
= wxWEBVIEW_NAV_ERR_REQUEST
;
189 //case SOUP_STATUS_NO_CONTENT:
190 //case SOUP_STATUS_RESET_CONTENT:
192 case SOUP_STATUS_BAD_REQUEST
:
193 type
= wxWEBVIEW_NAV_ERR_REQUEST
;
196 case SOUP_STATUS_UNAUTHORIZED
:
197 case SOUP_STATUS_FORBIDDEN
:
198 type
= wxWEBVIEW_NAV_ERR_AUTH
;
201 case SOUP_STATUS_METHOD_NOT_ALLOWED
:
202 case SOUP_STATUS_NOT_ACCEPTABLE
:
203 type
= wxWEBVIEW_NAV_ERR_SECURITY
;
206 case SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED
:
207 type
= wxWEBVIEW_NAV_ERR_AUTH
;
210 case SOUP_STATUS_REQUEST_TIMEOUT
:
211 type
= wxWEBVIEW_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
= wxWEBVIEW_NAV_ERR_REQUEST
;
221 case SOUP_STATUS_BAD_GATEWAY
:
222 case SOUP_STATUS_SERVICE_UNAVAILABLE
:
223 case SOUP_STATUS_GATEWAY_TIMEOUT
:
224 type
= wxWEBVIEW_NAV_ERR_CONNECTION
;
227 case SOUP_STATUS_HTTP_VERSION_NOT_SUPPORTED
:
228 type
= wxWEBVIEW_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
= wxWEBVIEW_NAV_ERR_REQUEST
;
246 case WEBKIT_NETWORK_ERROR_CANCELLED
:
247 type
= wxWEBVIEW_NAV_ERR_USER_CANCELLED
;
250 case WEBKIT_NETWORK_ERROR_FILE_DOES_NOT_EXIST
:
251 type
= wxWEBVIEW_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
= wxWEBVIEW_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_WEBVIEW_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_WEBVIEW_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_WEBVIEW_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());
381 #if WEBKIT_CHECK_VERSION(1, 10, 0)
384 wxgtk_webview_webkit_context_menu(WebKitWebView
*,
386 WebKitHitTestResult
*,
388 wxWebViewWebKit
*webKitCtrl
)
390 if(webKitCtrl
->IsContextMenuEnabled())
400 //-----------------------------------------------------------------------------
402 //-----------------------------------------------------------------------------
404 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit
, wxWebView
);
406 wxWebViewWebKit::wxWebViewWebKit()
411 bool wxWebViewWebKit::Create(wxWindow
*parent
,
417 const wxString
& name
)
423 // We currently unconditionally impose scrolling in both directions as it's
424 // necessary to show arbitrary pages.
425 style
|= wxHSCROLL
| wxVSCROLL
;
427 if (!PreCreation( parent
, pos
, size
) ||
428 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
430 wxFAIL_MSG( wxT("wxWebViewWebKit creation failed") );
434 m_web_view
= WEBKIT_WEB_VIEW(webkit_web_view_new());
435 GTKCreateScrolledWindowWith(GTK_WIDGET(m_web_view
));
436 g_object_ref(m_widget
);
438 g_signal_connect_after(m_web_view
, "navigation-policy-decision-requested",
439 G_CALLBACK(wxgtk_webview_webkit_navigation
),
441 g_signal_connect_after(m_web_view
, "load-error",
442 G_CALLBACK(wxgtk_webview_webkit_error
),
445 g_signal_connect_after(m_web_view
, "new-window-policy-decision-requested",
446 G_CALLBACK(wxgtk_webview_webkit_new_window
), this);
448 g_signal_connect_after(m_web_view
, "title-changed",
449 G_CALLBACK(wxgtk_webview_webkit_title_changed
), this);
451 g_signal_connect_after(m_web_view
, "resource-request-starting",
452 G_CALLBACK(wxgtk_webview_webkit_resource_req
), this);
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);
459 m_parent
->DoAddChild( this );
464 webkit_web_view_load_uri(m_web_view
, url
.utf8_str());
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
);
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
),
479 wxWebViewWebKit::~wxWebViewWebKit()
482 GTKDisconnect(m_web_view
);
485 bool wxWebViewWebKit::Enable( bool enable
)
487 if (!wxControl::Enable(enable
))
490 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget
)), enable
);
493 // GTKFixSensitivity();
499 wxWebViewWebKit::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
501 GdkWindow
* window
= gtk_widget_get_parent_window(m_widget
);
505 void wxWebViewWebKit::ZoomIn()
507 webkit_web_view_zoom_in(m_web_view
);
510 void wxWebViewWebKit::ZoomOut()
512 webkit_web_view_zoom_out(m_web_view
);
515 void wxWebViewWebKit::SetWebkitZoom(float level
)
517 webkit_web_view_set_zoom_level(m_web_view
, level
);
520 float wxWebViewWebKit::GetWebkitZoom() const
522 return webkit_web_view_get_zoom_level(m_web_view
);
525 void wxWebViewWebKit::Stop()
527 webkit_web_view_stop_loading(m_web_view
);
530 void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags
)
532 if (flags
& wxWEBVIEW_RELOAD_NO_CACHE
)
534 webkit_web_view_reload_bypass_cache(m_web_view
);
538 webkit_web_view_reload(m_web_view
);
542 void wxWebViewWebKit::LoadURL(const wxString
& url
)
544 webkit_web_view_load_uri(m_web_view
, wxGTK_CONV(url
));
548 void wxWebViewWebKit::GoBack()
550 webkit_web_view_go_back(m_web_view
);
553 void wxWebViewWebKit::GoForward()
555 webkit_web_view_go_forward(m_web_view
);
559 bool wxWebViewWebKit::CanGoBack() const
561 return webkit_web_view_can_go_back(m_web_view
);
565 bool wxWebViewWebKit::CanGoForward() const
567 return webkit_web_view_can_go_forward(m_web_view
);
570 void wxWebViewWebKit::ClearHistory()
572 WebKitWebBackForwardList
* history
;
573 history
= webkit_web_view_get_back_forward_list(m_web_view
);
574 webkit_web_back_forward_list_clear(history
);
577 void wxWebViewWebKit::EnableHistory(bool enable
)
579 WebKitWebBackForwardList
* history
;
580 history
= webkit_web_view_get_back_forward_list(m_web_view
);
583 webkit_web_back_forward_list_set_limit(history
, m_historyLimit
);
587 webkit_web_back_forward_list_set_limit(history
, 0);
591 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewWebKit::GetBackwardHistory()
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
,
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
--)
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
);
612 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewWebKit::GetForwardHistory()
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
,
619 for(guint i
= 0; i
< g_list_length(list
); i
++)
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
);
632 void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
)
634 WebKitWebHistoryItem
* gtkitem
= (WebKitWebHistoryItem
*)item
->m_histItem
;
637 webkit_web_view_go_to_back_forward_item(m_web_view
,
638 WEBKIT_WEB_HISTORY_ITEM(gtkitem
));
642 bool wxWebViewWebKit::CanCut() const
644 return webkit_web_view_can_cut_clipboard(m_web_view
);
647 bool wxWebViewWebKit::CanCopy() const
649 return webkit_web_view_can_copy_clipboard(m_web_view
);
652 bool wxWebViewWebKit::CanPaste() const
654 return webkit_web_view_can_paste_clipboard(m_web_view
);
657 void wxWebViewWebKit::Cut()
659 webkit_web_view_cut_clipboard(m_web_view
);
662 void wxWebViewWebKit::Copy()
664 webkit_web_view_copy_clipboard(m_web_view
);
667 void wxWebViewWebKit::Paste()
669 webkit_web_view_paste_clipboard(m_web_view
);
672 bool wxWebViewWebKit::CanUndo() const
674 return webkit_web_view_can_undo(m_web_view
);
677 bool wxWebViewWebKit::CanRedo() const
679 return webkit_web_view_can_redo(m_web_view
);
682 void wxWebViewWebKit::Undo()
684 webkit_web_view_undo(m_web_view
);
687 void wxWebViewWebKit::Redo()
689 webkit_web_view_redo(m_web_view
);
692 wxString
wxWebViewWebKit::GetCurrentURL() const
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
));
700 wxString
wxWebViewWebKit::GetCurrentTitle() const
702 return wxString::FromUTF8(webkit_web_view_get_title(m_web_view
));
706 wxString
wxWebViewWebKit::GetPageSource() const
708 WebKitWebFrame
* frame
= webkit_web_view_get_main_frame(m_web_view
);
709 WebKitWebDataSource
* src
= webkit_web_frame_get_data_source (frame
);
711 // TODO: check encoding with
713 // webkit_web_data_source_get_encoding(WebKitWebDataSource *data_source);
714 return wxString(webkit_web_data_source_get_data (src
)->str
, wxConvUTF8
);
718 wxWebViewZoom
wxWebViewWebKit::GetZoom() const
720 float zoom
= GetWebkitZoom();
722 // arbitrary way to map float zoom to our common zoom enum
725 return wxWEBVIEW_ZOOM_TINY
;
727 else if (zoom
> 0.65 && zoom
<= 0.90)
729 return wxWEBVIEW_ZOOM_SMALL
;
731 else if (zoom
> 0.90 && zoom
<= 1.15)
733 return wxWEBVIEW_ZOOM_MEDIUM
;
735 else if (zoom
> 1.15 && zoom
<= 1.45)
737 return wxWEBVIEW_ZOOM_LARGE
;
739 else if (zoom
> 1.45)
741 return wxWEBVIEW_ZOOM_LARGEST
;
744 // to shut up compilers, this can never be reached logically
746 return wxWEBVIEW_ZOOM_MEDIUM
;
750 void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom
)
752 // arbitrary way to map our common zoom enum to float zoom
755 case wxWEBVIEW_ZOOM_TINY
:
759 case wxWEBVIEW_ZOOM_SMALL
:
763 case wxWEBVIEW_ZOOM_MEDIUM
:
767 case wxWEBVIEW_ZOOM_LARGE
:
771 case wxWEBVIEW_ZOOM_LARGEST
:
780 void wxWebViewWebKit::SetZoomType(wxWebViewZoomType type
)
782 webkit_web_view_set_full_content_zoom(m_web_view
,
783 (type
== wxWEBVIEW_ZOOM_TYPE_LAYOUT
?
787 wxWebViewZoomType
wxWebViewWebKit::GetZoomType() const
789 gboolean fczoom
= webkit_web_view_get_full_content_zoom(m_web_view
);
791 if (fczoom
) return wxWEBVIEW_ZOOM_TYPE_LAYOUT
;
792 else return wxWEBVIEW_ZOOM_TYPE_TEXT
;
795 bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType
) const
797 // this port supports all zoom types
801 void wxWebViewWebKit::DoSetPage(const wxString
& html
, const wxString
& baseUri
)
803 webkit_web_view_load_string (m_web_view
,
804 html
.mb_str(wxConvUTF8
),
807 baseUri
.mb_str(wxConvUTF8
));
810 void wxWebViewWebKit::Print()
812 WebKitWebFrame
* frame
= webkit_web_view_get_main_frame(m_web_view
);
813 webkit_web_frame_print (frame
);
815 // GtkPrintOperationResult webkit_web_frame_print_full
816 // (WebKitWebFrame *frame,
817 // GtkPrintOperation *operation,
818 // GtkPrintOperationAction action,
824 bool wxWebViewWebKit::IsBusy() const
828 // This code looks nice but returns true after a page was cancelled
830 WebKitLoadStatus status = webkit_web_view_get_load_status
831 (WEBKIT_WEB_VIEW(web_view));
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)
841 if (status == WEBKIT_LOAD_FINISHED)
850 void wxWebViewWebKit::SetEditable(bool enable
)
852 webkit_web_view_set_editable(m_web_view
, enable
);
855 bool wxWebViewWebKit::IsEditable() const
857 return webkit_web_view_get_editable(m_web_view
);
860 void wxWebViewWebKit::DeleteSelection()
862 webkit_web_view_delete_selection(m_web_view
);
865 bool wxWebViewWebKit::HasSelection() const
867 return webkit_web_view_has_selection(m_web_view
);
870 void wxWebViewWebKit::SelectAll()
872 webkit_web_view_select_all(m_web_view
);
875 wxString
wxWebViewWebKit::GetSelectedText() const
877 WebKitDOMDocument
* doc
;
878 WebKitDOMDOMWindow
* win
;
879 WebKitDOMDOMSelection
* sel
;
880 WebKitDOMRange
* range
;
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
),
887 return wxString(webkit_dom_range_get_text(WEBKIT_DOM_RANGE(range
)),
891 wxString
wxWebViewWebKit::GetSelectedSource() const
893 WebKitDOMDocument
* doc
;
894 WebKitDOMDOMWindow
* win
;
895 WebKitDOMDOMSelection
* sel
;
896 WebKitDOMRange
* range
;
897 WebKitDOMElement
* div
;
898 WebKitDOMDocumentFragment
* clone
;
899 WebKitDOMHTMLElement
* html
;
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
),
906 div
= webkit_dom_document_create_element(WEBKIT_DOM_DOCUMENT(doc
), "div", NULL
);
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
;
912 return wxString(webkit_dom_html_element_get_inner_html(WEBKIT_DOM_HTML_ELEMENT(html
)),
916 void wxWebViewWebKit::ClearSelection()
918 WebKitDOMDocument
* doc
;
919 WebKitDOMDOMWindow
* win
;
920 WebKitDOMDOMSelection
* sel
;
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
));
929 wxString
wxWebViewWebKit::GetPageText() const
931 WebKitDOMDocument
* doc
;
932 WebKitDOMHTMLElement
* body
;
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
)),
940 void wxWebViewWebKit::RunScript(const wxString
& javascript
)
942 webkit_web_view_execute_script(m_web_view
,
943 javascript
.mb_str(wxConvUTF8
));
946 void wxWebViewWebKit::RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
)
948 m_handlerList
.push_back(handler
);
951 void wxWebViewWebKit::EnableContextMenu(bool enable
)
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
);
957 wxWebView::EnableContextMenu(enable
);
960 long wxWebViewWebKit::Find(const wxString
& text
, int flags
)
962 bool newSearch
= false;
963 if(text
!= m_findText
||
964 (flags
& wxWEBVIEW_FIND_MATCH_CASE
) != (m_findFlags
& wxWEBVIEW_FIND_MATCH_CASE
))
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);
975 //If the search string is empty then we clear any selection and highlight
978 webkit_web_view_unmark_text_matches(m_web_view
);
979 webkit_web_view_set_highlight_text_matches(m_web_view
, false);
984 bool wrap
= false, matchCase
= false, forward
= true;
985 if(flags
& wxWEBVIEW_FIND_WRAP
)
987 if(flags
& wxWEBVIEW_FIND_MATCH_CASE
)
989 if(flags
& wxWEBVIEW_FIND_BACKWARDS
)
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
1006 if(m_findPosition
< 0)
1007 m_findPosition
+= m_findCount
;
1008 if(m_findPosition
> m_findCount
)
1009 m_findPosition
-= m_findCount
;
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
);
1016 if(!webkit_web_view_search_text(m_web_view
, wxGTK_CONV(text
), matchCase
, forward
, wrap
))
1018 m_findPosition
= -1;
1022 wxLogMessage(wxString::Format("Returning %d", m_findPosition
));
1023 return newSearch
? m_findCount
: m_findPosition
;
1026 void wxWebViewWebKit::FindClear()
1031 m_findPosition
= -1;
1036 wxWebViewWebKit::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1038 return GetDefaultAttributesFromGTKWidget(webkit_web_view_new());
1042 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT