Add wxGrid::DoEnableCellEditControl() with bool return value

Checking the new function return value is simpler than checking the
value of m_cellEditCtrlEnabled after calling EnableCellEditControl().

Do this now also when starting editing using the mouse, as it was simply
forgotten before and so StartingClick() was still called even if editing
was vetoed.

Also add DoDisableCellEditControl(), but this one exists purely for the
symmetry.
This commit is contained in:
Vadim Zeitlin
2020-06-28 01:47:06 +02:00
parent 2d9112bd9b
commit a400a380f2
2 changed files with 35 additions and 19 deletions

View File

@@ -2912,6 +2912,14 @@ private:
void DoShowCellEditControl(); void DoShowCellEditControl();
void DoHideCellEditControl(); void DoHideCellEditControl();
// Unconditionally try showing the editor for the current cell.
//
// Returns false if the user code vetoed wxEVT_GRID_EDITOR_SHOWN.
bool DoEnableCellEditControl();
// Unconditionally disable (accepting the changes) the editor.
void DoDisableCellEditControl();
// Accept the changes in the edit control, i.e. save them to the table and // Accept the changes in the edit control, i.e. save them to the table and
// dismiss the editor. Also reset m_cellEditCtrlEnabled. // dismiss the editor. Also reset m_cellEditCtrlEnabled.
void DoAcceptCellEditControl(); void DoAcceptCellEditControl();

View File

@@ -4657,9 +4657,9 @@ wxGrid::DoGridCellLeftUp(wxMouseEvent& event,
if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() ) if ( coords == m_currentCellCoords && m_waitForSlowClick && CanEnableCellControl() )
{ {
ClearSelection(); ClearSelection();
EnableCellEditControl();
GetCurrentCellEditorPtr()->StartingClick(); if ( DoEnableCellEditControl() )
GetCurrentCellEditorPtr()->StartingClick();
m_waitForSlowClick = false; m_waitForSlowClick = false;
} }
@@ -5968,18 +5968,14 @@ void wxGrid::OnChar( wxKeyEvent& event )
// <F2> is special and will always start editing, for // <F2> is special and will always start editing, for
// other keys - ask the editor itself // other keys - ask the editor itself
if ( (event.GetKeyCode() == WXK_F2 && !event.HasModifiers()) const bool specialEditKey = event.GetKeyCode() == WXK_F2 &&
|| editor->IsAcceptedKey(event) ) !event.HasModifiers();
if ( specialEditKey || editor->IsAcceptedKey(event) )
{ {
// ensure cell is visble // ensure cell is visble
MakeCellVisible(m_currentCellCoords); MakeCellVisible(m_currentCellCoords);
EnableCellEditControl();
// a problem can arise if the cell is not completely if ( DoEnableCellEditControl() && !specialEditKey )
// visible (even after calling MakeCellVisible the
// control is not created and calling StartingKey will
// crash the app
if ( event.GetKeyCode() != WXK_F2 && editor->IsCreated() && m_cellEditCtrlEnabled )
editor->StartingKey(event); editor->StartingKey(event);
} }
else else
@@ -7150,22 +7146,34 @@ void wxGrid::EnableCellEditControl( bool enable )
// this should be checked by the caller! // this should be checked by the caller!
wxCHECK_RET( CanEnableCellControl(), wxT("can't enable editing for this cell!") ); wxCHECK_RET( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 ) DoEnableCellEditControl();
return;
m_cellEditCtrlEnabled = true;
DoShowCellEditControl();
} }
else else
{ {
SendEvent(wxEVT_GRID_EDITOR_HIDDEN); DoDisableCellEditControl();
DoAcceptCellEditControl();
} }
} }
} }
bool wxGrid::DoEnableCellEditControl()
{
if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN) == -1 )
return false;
m_cellEditCtrlEnabled = true;
DoShowCellEditControl();
return true;
}
void wxGrid::DoDisableCellEditControl()
{
SendEvent(wxEVT_GRID_EDITOR_HIDDEN);
DoAcceptCellEditControl();
}
bool wxGrid::IsCurrentCellReadOnly() const bool wxGrid::IsCurrentCellReadOnly() const
{ {
return const_cast<wxGrid *>(this)-> return const_cast<wxGrid *>(this)->