Created
July 25, 2021 08:08
-
-
Save PBfordev/f1e71970aedca86b609ccc954f9a20b9 to your computer and use it in GitHub Desktop.
Demonstrates focus issues with wxWebViewEdge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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