Merge branch 'grid-event-delete'

Handle deleting grid rows/columns in event handlers gracefully.

See https://github.com/wxWidgets/wxWidgets/pull/1834
This commit is contained in:
Vadim Zeitlin
2020-05-06 18:45:16 +02:00
2 changed files with 34 additions and 22 deletions

View File

@@ -97,6 +97,7 @@ class WXDLLIMPEXP_FWD_CORE wxGridCellAttr;
class WXDLLIMPEXP_FWD_CORE wxGridCellAttrProviderData; class WXDLLIMPEXP_FWD_CORE wxGridCellAttrProviderData;
class WXDLLIMPEXP_FWD_CORE wxGridColLabelWindow; class WXDLLIMPEXP_FWD_CORE wxGridColLabelWindow;
class WXDLLIMPEXP_FWD_CORE wxGridCornerLabelWindow; class WXDLLIMPEXP_FWD_CORE wxGridCornerLabelWindow;
class WXDLLIMPEXP_FWD_CORE wxGridEvent;
class WXDLLIMPEXP_FWD_CORE wxGridRowLabelWindow; class WXDLLIMPEXP_FWD_CORE wxGridRowLabelWindow;
class WXDLLIMPEXP_FWD_CORE wxGridWindow; class WXDLLIMPEXP_FWD_CORE wxGridWindow;
class WXDLLIMPEXP_FWD_CORE wxGridTypeRegistry; class WXDLLIMPEXP_FWD_CORE wxGridTypeRegistry;
@@ -2554,8 +2555,13 @@ protected:
bool Redimension( wxGridTableMessage& ); bool Redimension( wxGridTableMessage& );
// generate the appropriate grid event and return -1 if it was vetoed, 1 if // Send the given grid event and return -1 if it was vetoed or, as a
// it was processed (but not vetoed) and 0 if it wasn't processed // special exception, if an event for a particular cell resulted in this
// cell being deleted, 1 if it was processed (but not vetoed) and 0 if it
// wasn't processed.
int DoSendEvent(wxGridEvent& gridEvt);
// Generate an event of the given type and call DoSendEvent().
int SendEvent(wxEventType evtType, int SendEvent(wxEventType evtType,
int row, int col, int row, int col,
const wxMouseEvent& e); const wxMouseEvent& e);

View File

@@ -5297,16 +5297,35 @@ wxGrid::SendGridSizeEvent(wxEventType type,
return GetEventHandler()->ProcessEvent(gridEvt); return GetEventHandler()->ProcessEvent(gridEvt);
} }
// Generate a grid event based on a mouse event and return: // Process the event and return
// -1 if the event was vetoed // -1 if the event was vetoed or if event cell was deleted
// +1 if the event was processed (but not vetoed) // +1 if the event was processed (but not vetoed)
// 0 if the event wasn't handled // 0 if the event wasn't handled
int wxGrid::DoSendEvent(wxGridEvent& gridEvt)
{
const bool claimed = GetEventHandler()->ProcessEvent(gridEvt);
// A Veto'd event may not be `claimed' so test this first
if ( !gridEvt.IsAllowed() )
return -1;
// We also return -1 if the event cell was deleted, as this allows to have
// checks in several functions that generate an event and then proceed
// doing something by default with the selected cell: this shouldn't be
// done if the user-defined handler deleted this cell.
if ( gridEvt.GetRow() >= GetNumberRows() ||
gridEvt.GetCol() >= GetNumberCols() )
return -1;
return claimed ? 1 : 0;
}
// Generate a grid event based on a mouse event and call DoSendEvent() with it.
int int
wxGrid::SendEvent(wxEventType type, wxGrid::SendEvent(wxEventType type,
int row, int col, int row, int col,
const wxMouseEvent& mouseEv) const wxMouseEvent& mouseEv)
{ {
bool claimed, vetoed;
if ( type == wxEVT_GRID_LABEL_LEFT_CLICK || if ( type == wxEVT_GRID_LABEL_LEFT_CLICK ||
type == wxEVT_GRID_LABEL_LEFT_DCLICK || type == wxEVT_GRID_LABEL_LEFT_DCLICK ||
@@ -5328,8 +5347,8 @@ wxGrid::SendEvent(wxEventType type,
pos.y, pos.y,
false, false,
mouseEv); mouseEv);
claimed = GetEventHandler()->ProcessEvent(gridEvt);
vetoed = !gridEvt.IsAllowed(); return DoSendEvent(gridEvt);
} }
else else
{ {
@@ -5349,15 +5368,8 @@ wxGrid::SendEvent(wxEventType type,
gridEvt.Veto(); gridEvt.Veto();
} }
claimed = GetEventHandler()->ProcessEvent(gridEvt); return DoSendEvent(gridEvt);
vetoed = !gridEvt.IsAllowed();
} }
// A Veto'd event may not be `claimed' so test this first
if (vetoed)
return -1;
return claimed ? 1 : 0;
} }
// Generate a grid event of specified type, return value same as above // Generate a grid event of specified type, return value same as above
@@ -5368,13 +5380,7 @@ wxGrid::SendEvent(wxEventType type, int row, int col, const wxString& s)
wxGridEvent gridEvt( GetId(), type, this, row, col ); wxGridEvent gridEvt( GetId(), type, this, row, col );
gridEvt.SetString(s); gridEvt.SetString(s);
const bool claimed = GetEventHandler()->ProcessEvent(gridEvt); return DoSendEvent(gridEvt);
// A Veto'd event may not be `claimed' so test this first
if ( !gridEvt.IsAllowed() )
return -1;
return claimed ? 1 : 0;
} }
void wxGrid::Refresh(bool eraseb, const wxRect* rect) void wxGrid::Refresh(bool eraseb, const wxRect* rect)