Provide generic wxWebView::Find() implementation

This implementation is based on javascript 'window.find()'
https://developer.mozilla.org/en-US/docs/Web/API/Window/find
It's not standard, but implemented in most browsers.

It's not a complete implementation as it can't determine how
many search results can be found.

If the backends API provides a native find interface it should be
used instead of this implementation, but it's good fallback.
This commit is contained in:
Tobias Taschner
2021-02-10 23:55:18 +01:00
parent 5512089d5b
commit e09063186c
3 changed files with 18 additions and 4 deletions

View File

@@ -257,6 +257,7 @@ private:
static wxStringWebViewFactoryMap::iterator FindFactory(const wxString &backend); static wxStringWebViewFactoryMap::iterator FindFactory(const wxString &backend);
bool m_showMenu; bool m_showMenu;
wxString m_findText;
static wxStringWebViewFactoryMap m_factoryMap; static wxStringWebViewFactoryMap m_factoryMap;
wxDECLARE_ABSTRACT_CLASS(wxWebView); wxDECLARE_ABSTRACT_CLASS(wxWebView);

View File

@@ -317,7 +317,7 @@ public:
<a href="https://docs.microsoft.com/en-us/microsoft-edge/hosting/webview2">Edge WebView2</a>. <a href="https://docs.microsoft.com/en-us/microsoft-edge/hosting/webview2">Edge WebView2</a>.
It is available for Windows 7 and newer. It is available for Windows 7 and newer.
The following features are currently unsupported with this backend: The following features are currently unsupported with this backend:
virtual filesystems, custom urls, find. virtual filesystems, custom urls.
This backend is not enabled by default, to build it follow these steps: This backend is not enabled by default, to build it follow these steps:
- Visual Studio 2015, or newer, is required - Visual Studio 2015, or newer, is required

View File

@@ -203,10 +203,23 @@ void wxWebView::SelectAll()
RunScript("window.getSelection().selectAllChildren(document.body);"); RunScript("window.getSelection().selectAllChildren(document.body);");
} }
long wxWebView::Find(const wxString& WXUNUSED(text), int WXUNUSED(flags)) long wxWebView::Find(const wxString& text, int flags)
{ {
// TODO: could probably be implemented by script if (text != m_findText)
return -1; ClearSelection();
m_findText = text;
wxString output;
RunScript(wxString::Format("window.find('%s', %s, %s, %s, %s)",
text,
(flags & wxWEBVIEW_FIND_MATCH_CASE) ? "true" : "false",
(flags & wxWEBVIEW_FIND_BACKWARDS) ? "true" : "false",
(flags & wxWEBVIEW_FIND_WRAP) ? "true" : "false",
(flags & wxWEBVIEW_FIND_ENTIRE_WORD) ? "true" : "false"
), &output);
if (output.IsSameAs("false", false))
return wxNOT_FOUND;
else
return 1;
} }
// static // static