27#include <wx/webviewarchivehandler.h>
28#include <wx/webviewfshandler.h>
34 wxPanel( aParent, aId, aPos, aSize, aStyle ),
43 wxBoxSizer* sizer =
new wxBoxSizer( wxVERTICAL );
45 if( !wxGetEnv( wxT(
"WEBKIT_DISABLE_COMPOSITING_MODE" ),
nullptr ) )
47 wxSetEnv( wxT(
"WEBKIT_DISABLE_COMPOSITING_MODE" ), wxT(
"1" ) );
51 m_browser->RegisterHandler( wxSharedPtr<wxWebViewHandler>(
new wxWebViewArchiveHandler(
"wxfs" ) ) );
52 m_browser->RegisterHandler( wxSharedPtr<wxWebViewHandler>(
new wxWebViewFSHandler(
"memory" ) ) );
55 m_browser->Create(
this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize );
60 m_browser->RegisterHandler( wxSharedPtr<wxWebViewHandler>(
new wxWebViewArchiveHandler(
"wxfs" ) ) );
61 m_browser->RegisterHandler( wxSharedPtr<wxWebViewHandler>(
new wxWebViewFSHandler(
"memory" ) ) );
86 wxLogTrace(
"webview",
"Loading URL: %s", aURL );
88 if( aURL.starts_with(
"file:/" ) && !aURL.starts_with(
"file:///" ) )
90 wxString new_url = wxString(
"file:///" ) + aURL.AfterFirst(
'/' );
95 if( !aURL.StartsWith(
"http://" ) && !aURL.StartsWith(
"https://" ) && !aURL.StartsWith(
"file://" ) )
97 wxLogError(
"Invalid URL: %s", aURL );
106 wxLogTrace(
"webview",
"Setting page content" );
107 m_browser->SetPage( aHtmlContent,
"file://" );
112 wxLogTrace(
"webview",
"Adding message handler for: %s", aName );
117 it->second = std::move( aHandler );
125 if( !
m_browser->AddScriptMessageHandler( aName ) )
126 wxLogDebug(
"Could not add script message handler %s", aName );
134 wxLogTrace(
"webview",
"Clearing all message handlers" );
137 m_browser->RemoveScriptMessageHandler( handler.first );
145 wxLogTrace(
"webview",
"Navigation request to URL: %s", aEvt.GetURL() );
147 bool isExternal = aEvt.GetURL().StartsWith(
"http://" ) || aEvt.GetURL().StartsWith(
"https://" );
151 wxLogTrace(
"webview",
"Opening external URL in system browser: %s", aEvt.GetURL() );
152 wxLaunchDefaultBrowser( aEvt.GetURL() );
162 auto initFunc = [
this]() {
165 if( !
m_browser->AddScriptMessageHandler( handler.first ) )
167 wxLogDebug(
"Could not add script message handler %s", handler.first );
174 // Change window.open to navigate in the same window
175 window.open = function(url) { if (url) window.location.href = url; return null; };
176 window.showModalDialog = function() { return null; };
178 if (window.external && window.external.invoke) {
179 function notifyHost() {
180 window.external.invoke('navigation:' + window.location.href);
182 window.addEventListener('popstate', notifyHost);
183 window.addEventListener('pushstate', notifyHost);
184 window.addEventListener('replacestate', notifyHost);
185 ['pushState', 'replaceState'].forEach(function(type) {
186 var orig = history[type];
187 history[type] = function() {
188 var rv = orig.apply(this, arguments);
189 window.dispatchEvent(new Event(type.toLowerCase()));
202 CallAfter( initFunc );
214 wxLogTrace(
"webview",
"New window requested for URL: %s", aEvt.GetURL() );
215 wxLogTrace(
"webview",
"Target: %s", aEvt.GetTarget() );
216 wxLogTrace(
"webview",
"Action flags: %d",
static_cast<int>(aEvt.GetNavigationAction()) );
217 wxLogTrace(
"webview",
"Message handler: %s", aEvt.GetMessageHandler() );
222 wxLogTrace(
"webview",
"Script message received: %s for handler %s", aEvt.GetString(), aEvt.GetMessageHandler() );
223 wxString handler = aEvt.GetMessageHandler();
224 handler.Trim(
true).Trim(
false);
226 if( handler.IsEmpty() )
230 wxLogTrace(
"webview",
"No handler specified, trying: %s", handlerPair.first );
231 handlerPair.second( aEvt.GetString() );
241 wxLogDebug(
"No handler registered for message: %s", handler );
246 wxLogTrace(
"webview",
"Calling handler for message: %s", handler );
247 it->second( aEvt.GetString() );
253 wxLogDebug(
"Async script execution failed: %s", aEvt.GetString() );
259 wxLogDebug(
"WebView error: %s", aEvt.GetString() );
wxString GetMajorMinorPatchVersion()
Get the major, minor and patch version in a string major.minor.patch This is extracted by CMake from ...
WEBVIEW_PANEL(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, const int style=0, TOOL_MANAGER *aToolManager=nullptr, TOOL_BASE *aTool=nullptr)
TOOL_MANAGER * m_toolManager
void OnScriptMessage(wxWebViewEvent &evt)
void OnNewWindow(wxWebViewEvent &evt)
std::map< wxString, MESSAGE_HANDLER > m_msgHandlers
void SetPage(const wxString &htmlContent)
void OnWebViewLoaded(wxWebViewEvent &evt)
void OnNavigationRequest(wxWebViewEvent &evt)
void ClearMessageHandlers()
bool AddMessageHandler(const wxString &name, MESSAGE_HANDLER handler)
bool m_handleExternalLinks
std::function< void(const wxString &)> MESSAGE_HANDLER
void OnScriptResult(wxWebViewEvent &evt)
void OnError(wxWebViewEvent &evt)
~WEBVIEW_PANEL() override
void LoadURL(const wxString &url)