feat(modulo): submit forms by just pressing Enter when possible, and also add help label. Fix #709
This commit is contained in:
parent
b470aaad8a
commit
3034dbe122
|
@ -89,11 +89,16 @@ public:
|
||||||
std::vector<void *> fields;
|
std::vector<void *> fields;
|
||||||
std::unordered_map<const char *, std::unique_ptr<FieldWrapper>> idMap;
|
std::unordered_map<const char *, std::unique_ptr<FieldWrapper>> idMap;
|
||||||
wxButton *submit;
|
wxButton *submit;
|
||||||
|
wxStaticText *helpText;
|
||||||
|
bool hasFocusedMultilineControl;
|
||||||
private:
|
private:
|
||||||
void AddComponent(wxPanel *parent, wxBoxSizer *sizer, FieldMetadata meta);
|
void AddComponent(wxPanel *parent, wxBoxSizer *sizer, FieldMetadata meta);
|
||||||
void Submit();
|
void Submit();
|
||||||
void OnSubmitBtn(wxCommandEvent& event);
|
void OnSubmitBtn(wxCommandEvent& event);
|
||||||
void OnEscape(wxKeyEvent& event);
|
void OnCharHook(wxKeyEvent& event);
|
||||||
|
void UpdateHelpText();
|
||||||
|
void HandleNormalFocus(wxFocusEvent& event);
|
||||||
|
void HandleMultilineFocus(wxFocusEvent& event);
|
||||||
};
|
};
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
|
@ -113,6 +118,8 @@ bool FormApp::OnInit()
|
||||||
FormFrame::FormFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
|
FormFrame::FormFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
|
||||||
: wxFrame(NULL, wxID_ANY, title, pos, size, DEFAULT_STYLE)
|
: wxFrame(NULL, wxID_ANY, title, pos, size, DEFAULT_STYLE)
|
||||||
{
|
{
|
||||||
|
hasFocusedMultilineControl = false;
|
||||||
|
|
||||||
panel = new wxPanel(this, wxID_ANY);
|
panel = new wxPanel(this, wxID_ANY);
|
||||||
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
|
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
|
||||||
panel->SetSizer(vbox);
|
panel->SetSizer(vbox);
|
||||||
|
@ -125,8 +132,15 @@ FormFrame::FormFrame(const wxString& title, const wxPoint& pos, const wxSize& si
|
||||||
submit = new wxButton(panel, ID_Submit, "Submit");
|
submit = new wxButton(panel, ID_Submit, "Submit");
|
||||||
vbox->Add(submit, 1, wxEXPAND | wxALL, PADDING);
|
vbox->Add(submit, 1, wxEXPAND | wxALL, PADDING);
|
||||||
|
|
||||||
|
helpText = new wxStaticText(panel, wxID_ANY, "", wxDefaultPosition, wxDefaultSize);
|
||||||
|
wxFont helpFont = helpText->GetFont();
|
||||||
|
helpFont.SetPointSize(8);
|
||||||
|
helpText->SetFont(helpFont);
|
||||||
|
vbox->Add(helpText, 0, wxLEFT | wxRIGHT | wxBOTTOM, PADDING);
|
||||||
|
UpdateHelpText();
|
||||||
|
|
||||||
Bind(wxEVT_BUTTON, &FormFrame::OnSubmitBtn, this, ID_Submit);
|
Bind(wxEVT_BUTTON, &FormFrame::OnSubmitBtn, this, ID_Submit);
|
||||||
Bind(wxEVT_CHAR_HOOK, &FormFrame::OnEscape, this, wxID_ANY);
|
Bind(wxEVT_CHAR_HOOK, &FormFrame::OnCharHook, this, wxID_ANY);
|
||||||
// TODO: register ESC click handler: https://forums.wxwidgets.org/viewtopic.php?t=41926
|
// TODO: register ESC click handler: https://forums.wxwidgets.org/viewtopic.php?t=41926
|
||||||
|
|
||||||
this->SetClientSize(panel->GetBestSize());
|
this->SetClientSize(panel->GetBestSize());
|
||||||
|
@ -157,6 +171,9 @@ void FormFrame::AddComponent(wxPanel *parent, wxBoxSizer *sizer, FieldMetadata m
|
||||||
|
|
||||||
if (textMeta->multiline) {
|
if (textMeta->multiline) {
|
||||||
textControl->SetMinSize(wxSize(MULTILINE_MIN_WIDTH, MULTILINE_MIN_HEIGHT));
|
textControl->SetMinSize(wxSize(MULTILINE_MIN_WIDTH, MULTILINE_MIN_HEIGHT));
|
||||||
|
textControl->Bind(wxEVT_SET_FOCUS, &FormFrame::HandleMultilineFocus, this, wxID_ANY);
|
||||||
|
} else {
|
||||||
|
textControl->Bind(wxEVT_SET_FOCUS, &FormFrame::HandleNormalFocus, this, wxID_ANY);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the field wrapper
|
// Create the field wrapper
|
||||||
|
@ -188,6 +205,8 @@ void FormFrame::AddComponent(wxPanel *parent, wxBoxSizer *sizer, FieldMetadata m
|
||||||
((wxChoice*)choice)->SetSelection(selectedItem);
|
((wxChoice*)choice)->SetSelection(selectedItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
((wxChoice*)choice)->Bind(wxEVT_SET_FOCUS, &FormFrame::HandleNormalFocus, this, wxID_ANY);
|
||||||
|
|
||||||
// Create the field wrapper
|
// Create the field wrapper
|
||||||
std::unique_ptr<FieldWrapper> field((FieldWrapper*) new ChoiceFieldWrapper((wxChoice*) choice));
|
std::unique_ptr<FieldWrapper> field((FieldWrapper*) new ChoiceFieldWrapper((wxChoice*) choice));
|
||||||
idMap[meta.id] = std::move(field);
|
idMap[meta.id] = std::move(field);
|
||||||
|
@ -198,13 +217,13 @@ void FormFrame::AddComponent(wxPanel *parent, wxBoxSizer *sizer, FieldMetadata m
|
||||||
((wxListBox*)choice)->SetSelection(selectedItem);
|
((wxListBox*)choice)->SetSelection(selectedItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
((wxListBox*)choice)->Bind(wxEVT_SET_FOCUS, &FormFrame::HandleNormalFocus, this, wxID_ANY);
|
||||||
|
|
||||||
// Create the field wrapper
|
// Create the field wrapper
|
||||||
std::unique_ptr<FieldWrapper> field((FieldWrapper*) new ListFieldWrapper((wxListBox*) choice));
|
std::unique_ptr<FieldWrapper> field((FieldWrapper*) new ListFieldWrapper((wxListBox*) choice));
|
||||||
idMap[meta.id] = std::move(field);
|
idMap[meta.id] = std::move(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
control = choice;
|
control = choice;
|
||||||
fields.push_back(choice);
|
fields.push_back(choice);
|
||||||
break;
|
break;
|
||||||
|
@ -253,15 +272,40 @@ void FormFrame::Submit() {
|
||||||
Close(true);
|
Close(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FormFrame::HandleNormalFocus(wxFocusEvent& event) {
|
||||||
|
hasFocusedMultilineControl = false;
|
||||||
|
UpdateHelpText();
|
||||||
|
event.Skip();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FormFrame::HandleMultilineFocus(wxFocusEvent& event) {
|
||||||
|
hasFocusedMultilineControl = true;
|
||||||
|
UpdateHelpText();
|
||||||
|
event.Skip();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FormFrame::UpdateHelpText() {
|
||||||
|
if (hasFocusedMultilineControl) {
|
||||||
|
helpText->SetLabel("(or press CTRL+Enter to submit, ESC to cancel)");
|
||||||
|
} else {
|
||||||
|
helpText->SetLabel("(or press Enter to submit, ESC to cancel)");
|
||||||
|
}
|
||||||
|
this->SetClientSize(panel->GetBestSize());
|
||||||
|
}
|
||||||
|
|
||||||
void FormFrame::OnSubmitBtn(wxCommandEvent &event) {
|
void FormFrame::OnSubmitBtn(wxCommandEvent &event) {
|
||||||
Submit();
|
Submit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FormFrame::OnEscape(wxKeyEvent& event) {
|
void FormFrame::OnCharHook(wxKeyEvent& event) {
|
||||||
if (event.GetKeyCode() == WXK_ESCAPE) {
|
if (event.GetKeyCode() == WXK_ESCAPE) {
|
||||||
Close(true);
|
Close(true);
|
||||||
}else if(event.GetKeyCode() == WXK_RETURN && wxGetKeyState(WXK_RAW_CONTROL)) {
|
}else if(event.GetKeyCode() == WXK_RETURN) {
|
||||||
|
if (!hasFocusedMultilineControl || wxGetKeyState(WXK_RAW_CONTROL)) {
|
||||||
Submit();
|
Submit();
|
||||||
|
} else {
|
||||||
|
event.Skip();
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
event.Skip();
|
event.Skip();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user