From 8b7fdc5e5bf18fb34726b16cf106550aef48271c Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Thu, 9 Jul 2020 17:56:43 +0100 Subject: [PATCH] Close the cell editor when the editors combobox is closed Closes #16404 --- include/wx/generic/grideditors.h | 3 +++ src/generic/grideditors.cpp | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/wx/generic/grideditors.h b/include/wx/generic/grideditors.h index ad6e90f19c..a6bdbc6377 100644 --- a/include/wx/generic/grideditors.h +++ b/include/wx/generic/grideditors.h @@ -335,6 +335,9 @@ public: protected: wxComboBox *Combo() const { return (wxComboBox *)m_control; } + void onComboCloseUp(wxCommandEvent& evt); + void onComboDropDown(wxCommandEvent& evt); + wxString m_value; wxArrayString m_choices; bool m_allowOthers; diff --git a/src/generic/grideditors.cpp b/src/generic/grideditors.cpp index 00d1aa8918..29dbaaa571 100644 --- a/src/generic/grideditors.cpp +++ b/src/generic/grideditors.cpp @@ -1580,7 +1580,12 @@ void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid) wxGridCellEditorEvtHandler* evtHandler = NULL; if (m_control) + { + // These event handlers are needed to properly dismiss the editor when the popup is closed + m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &wxGridCellChoiceEditor::onComboDropDown, this); + m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &wxGridCellChoiceEditor::onComboCloseUp, this); evtHandler = wxDynamicCast(m_control->GetEventHandler(), wxGridCellEditorEvtHandler); + } // Don't immediately end if we get a kill focus event within BeginEdit if (evtHandler) @@ -1679,6 +1684,35 @@ wxString wxGridCellChoiceEditor::GetValue() const return Combo()->GetValue(); } +void wxGridCellChoiceEditor::onComboDropDown(wxCommandEvent& WXUNUSED(evt)) +{ + wxGridCellEditorEvtHandler* evtHandler = wxDynamicCast(m_control->GetEventHandler(), + wxGridCellEditorEvtHandler); + + if ( !evtHandler ) + return; + + // Once the combobox is dropped, reset the flag to allow the focus-loss handler + // to function and close the editor. + evtHandler->SetInSetFocus(false); +} + +void wxGridCellChoiceEditor::onComboCloseUp(wxCommandEvent& WXUNUSED(evt)) +{ + wxGridCellEditorEvtHandler* evtHandler = wxDynamicCast(m_control->GetEventHandler(), + wxGridCellEditorEvtHandler); + + if ( !evtHandler ) + return; + + // Forward the combobox close up event to the cell event handler as a focus kill event + // so that the grid editor is dismissed when the combox closes, otherwise it leaves the + // dropdown arrow visible in the cell. + wxFocusEvent event(wxEVT_KILL_FOCUS, m_control->GetId()); + event.SetEventObject(m_control); + evtHandler->ProcessEvent(event); +} + #endif // wxUSE_COMBOBOX #if wxUSE_COMBOBOX