Skip to content

Instantly share code, notes, and snippets.

@PBfordev
Created July 25, 2021 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PBfordev/f1e71970aedca86b609ccc954f9a20b9 to your computer and use it in GitHub Desktop.
Save PBfordev/f1e71970aedca86b609ccc954f9a20b9 to your computer and use it in GitHub Desktop.
Demonstrates focus issues with wxWebViewEdge
#include <wx/wx.h>
#include <wx/webview.h>
const char* HTMLSource =
R"(<!DOCTYPE html>
<html>
<body>
<h1>Log In</h1>
<form>
<label for="uname">User name:</label>
<input type="text" id="uname" name="uname">
<label for="passwd">Password:</label>
<input type="text" id="passwd" name="passwd">
</form>
</body>
</html>)";
class MyFrame: public wxFrame
{
public:
MyFrame() : wxFrame(nullptr, wxID_ANY, "Test")
{
wxPanel* mainPanel = new wxPanel(this);
wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->Add(new wxButton(mainPanel, wxID_ANY, "Focus wxWebView Form"), wxSizerFlags().Expand().Border());
Bind(wxEVT_BUTTON, &MyFrame::OnFocusWebForm, this);
m_webView = wxWebView::New(mainPanel, wxID_ANY, wxWebViewDefaultURLStr,
wxDefaultPosition, wxDefaultSize, wxWebViewBackendEdge);
wxCHECK_RET(m_webView, "Couldn't create wxWebViewEdge");
m_webView->Bind(wxEVT_WEBVIEW_LOADED, &MyFrame::OnWebViewLoaded, this);
mainSizer->Add(m_webView, wxSizerFlags(1).Expand().DoubleBorder());
mainSizer->Add(new wxTextCtrl(mainPanel, wxID_ANY, ""), wxSizerFlags().Expand().Border());
SetMinClientSize(FromDIP(wxSize(600,300)));
mainPanel->SetSizer(mainSizer);
}
private:
wxWebView* m_webView;
void OnFocusWebForm(wxCommandEvent&)
{
m_webView->SetFocus();
m_webView->RunScript(R"(document.getElementById("uname").focus();)");
}
void OnWebViewLoaded(wxWebViewEvent&)
{ // SetPage() can be called only when the view was loaded
m_webView->Unbind(wxEVT_WEBVIEW_LOADED, &MyFrame::OnWebViewLoaded, this);
m_webView->SetPage(HTMLSource, wxWebViewDefaultURLStr);
}
};
class MyApp : public wxApp
{
public:
bool OnInit() override
{
(new MyFrame())->Show();
return true;
}
}; wxIMPLEMENT_APP(MyApp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment