From dc69cb79d731950fee9f7598e8d6827fc0ad8be0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 30 Jun 2020 02:31:29 +0200 Subject: [PATCH] Support activation in wxGridCellBoolEditor This makes it much more convenient to use interactively, as the cell value is toggled immediately and, even more importantly, the UI doesn't enter the confusing editing mode which doesn't look any different from the normal grid appearance except for the current cell border absence. Note that we still keep support for classic in-place editing to preserve compatibility with the code which calls EnableCellEditControl() explicitly and expects an editor to be shown, but perhaps we could switch to using only activation in the future. --- include/wx/generic/grideditors.h | 5 +++ src/generic/grideditors.cpp | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/include/wx/generic/grideditors.h b/include/wx/generic/grideditors.h index 3b4d47b72e..ad6e90f19c 100644 --- a/include/wx/generic/grideditors.h +++ b/include/wx/generic/grideditors.h @@ -238,6 +238,11 @@ class WXDLLIMPEXP_ADV wxGridCellBoolEditor : public wxGridCellEditor public: wxGridCellBoolEditor() { } + virtual wxGridActivationResult + TryActivate(int row, int col, wxGrid* grid, + const wxGridActivationSource& actSource) wxOVERRIDE; + virtual void DoActivate(int row, int col, wxGrid* grid) wxOVERRIDE; + virtual void Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler) wxOVERRIDE; diff --git a/src/generic/grideditors.cpp b/src/generic/grideditors.cpp index 3b5b2b1e2a..ca9026c52a 100644 --- a/src/generic/grideditors.cpp +++ b/src/generic/grideditors.cpp @@ -1247,6 +1247,61 @@ bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) // the default values for GetValue() wxString wxGridCellBoolEditor::ms_stringValues[2] = { wxT(""), wxT("1") }; +wxGridActivationResult +wxGridCellBoolEditor::TryActivate(int row, int col, wxGrid* grid, + const wxGridActivationSource& actSource) +{ + SetValueFromGrid(row, col, grid); + + switch ( actSource.GetOrigin() ) + { + case wxGridActivationSource::Program: + // It's not really clear what should happen in this case, so for + // now show the editor interactively to avoid making the choice. + return wxGridActivationResult::DoEdit(); + + case wxGridActivationSource::Mouse: + m_value = !m_value; + return wxGridActivationResult::DoChange(GetStringValue()); + + case wxGridActivationSource::Key: + switch ( actSource.GetKeyEvent().GetKeyCode() ) + { + // Handle F2 as space here because we must handle it somehow, + // because pressing it always starts editing in wxGrid, and + // it's not really clear what else could it do. + case WXK_F2: + case WXK_SPACE: + m_value = !m_value; + break; + + case '+': + if ( m_value ) + return wxGridActivationResult::DoNothing(); + + m_value = true; + break; + + case '-': + if ( !m_value ) + return wxGridActivationResult::DoNothing(); + + m_value = false; + break; + } + + return wxGridActivationResult::DoChange(GetStringValue()); + } + + wxFAIL_MSG( "unknown activation source origin" ); + return wxGridActivationResult::DoNothing(); +} + +void wxGridCellBoolEditor::DoActivate(int row, int col, wxGrid* grid) +{ + SetGridFromValue(row, col, grid); +} + void wxGridCellBoolEditor::Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler)