From 4d60017aa5c220c62f6d9d4cd70d33d1fb90a005 Mon Sep 17 00:00:00 2001 From: Stefan Neis Date: Wed, 16 Feb 2000 16:08:16 +0000 Subject: [PATCH] Added UpdateAttrRows/Cols to change the row/column info in attributes as needed when Deleting/Inserting rows/columns. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6080 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 6 ++ src/generic/grid.cpp | 142 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 144 insertions(+), 4 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index a572f9d4a0..c11656fdd5 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -311,6 +311,8 @@ public: virtual void SetAttr(wxGridCellAttr *attr, int row, int col); virtual void SetRowAttr(wxGridCellAttr *attr, int row); virtual void SetColAttr(wxGridCellAttr *attr, int col); + void UpdateAttrRows( size_t pos, int numRows ); + void UpdateAttrCols( size_t pos, int numCols ); private: void InitData(); @@ -366,6 +368,10 @@ public: // get the currently used attr provider (may be NULL) wxGridCellAttrProvider *GetAttrProvider() const { return m_attrProvider; } + // change row/col number in attribute if needed + void UpdateAttrRows( size_t pos, int numRows ); + void UpdateAttrCols( size_t pos, int numCols ); + // by default forwarded to wxGridCellAttrProvider if any. May be // overridden to handle attributes directly in this class. virtual wxGridCellAttr *GetAttr( int row, int col ); diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index fd9629d865..37688f3073 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -208,6 +208,8 @@ class WXDLLEXPORT wxGridCellAttrData public: void SetAttr(wxGridCellAttr *attr, int row, int col); wxGridCellAttr *GetAttr(int row, int col) const; + void UpdateAttrRows( size_t pos, int numRows ); + void UpdateAttrCols( size_t pos, int numCols ); private: // searches for the attr for given cell, returns wxNOT_FOUND if not found @@ -224,6 +226,7 @@ public: void SetAttr(wxGridCellAttr *attr, int rowOrCol); wxGridCellAttr *GetAttr(int rowOrCol) const; + void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ); private: wxArrayInt m_rowsOrCols; @@ -640,6 +643,72 @@ wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const return attr; } +void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) +{ + size_t count = m_attrs.GetCount(); + for ( size_t n = 0; n < count; n++ ) + { + wxGridCellCoords& coords = m_attrs[n].coords; + wxCoord row = coords.GetRow(); + if ((size_t)row >= pos) + { + if (numRows > 0) + { + // If rows inserted, include row counter where necessary + coords.SetRow(row + numRows); + } + else if (numRows < 0) + { + // If rows deleted ... + if ((size_t)row >= pos - numRows) + { + // ...either decrement row counter (if row still exists)... + coords.SetRow(row + numRows); + } + else + { + // ...or remove the attribute + m_attrs.RemoveAt((size_t)n); + n--; count--; + } + } + } + } +} + +void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) +{ + size_t count = m_attrs.GetCount(); + for ( size_t n = 0; n < count; n++ ) + { + wxGridCellCoords& coords = m_attrs[n].coords; + wxCoord col = coords.GetCol(); + if ( (size_t)col >= pos ) + { + if ( numCols > 0 ) + { + // If rows inserted, include row counter where necessary + coords.SetCol(col + numCols); + } + else if (numCols < 0) + { + // If rows deleted ... + if ((size_t)col >= pos - numCols) + { + // ...either decrement row counter (if row still exists)... + coords.SetCol(col + numCols); + } + else + { + // ...or remove the attribute + m_attrs.RemoveAt((size_t)n); + n--; count--; + } + } + } + } +} + int wxGridCellAttrData::FindIndex(int row, int col) const { size_t count = m_attrs.GetCount(); @@ -708,6 +777,35 @@ void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) } } +void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) +{ + size_t count = m_attrs.GetCount(); + for ( size_t n = 0; n < count; n++ ) + { + int & rowOrCol = m_rowsOrCols[n]; + if ( (size_t)rowOrCol >= pos ) + { + if ( numRowsOrCols > 0 ) + { + // If rows inserted, include row counter where necessary + rowOrCol += numRowsOrCols; + } + else if ( numRowsOrCols < 0) + { + // If rows deleted, either decrement row counter (if row still exists) + if ((size_t)rowOrCol >= pos - numRowsOrCols) + rowOrCol += numRowsOrCols; + else + { + m_rowsOrCols.RemoveAt((size_t)n); + m_attrs.RemoveAt((size_t)n); + n--; count--; + } + } + } + } +} + // ---------------------------------------------------------------------------- // wxGridCellAttrProvider // ---------------------------------------------------------------------------- @@ -777,6 +875,26 @@ void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) m_data->m_colAttrs.SetAttr(attr, col); } +void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) +{ + if ( m_data ) + { + m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); + + m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); + } +} + +void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) +{ + if ( m_data ) + { + m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); + + m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); + } +} + // ---------------------------------------------------------------------------- // wxGridTableBase // ---------------------------------------------------------------------------- @@ -855,6 +973,22 @@ void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) } } +void wxGridTableBase::UpdateAttrRows( size_t pos, int numRows ) +{ + if ( m_attrProvider ) + { + m_attrProvider->UpdateAttrRows( pos, numRows ); + } +} + +void wxGridTableBase::UpdateAttrCols( size_t pos, int numCols ) +{ + if ( m_attrProvider ) + { + m_attrProvider->UpdateAttrCols( pos, numCols ); + } +} + bool wxGridTableBase::InsertRows( size_t pos, size_t numRows ) { wxFAIL_MSG( wxT("Called grid table class function InsertRows\n" @@ -1082,7 +1216,7 @@ bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) { m_data.Insert( sa, row ); } - + UpdateAttrRows( pos, numRows ); if ( GetView() ) { wxGridTableMessage msg( this, @@ -1162,7 +1296,7 @@ bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) m_data.Remove( pos ); } } - + UpdateAttrRows( pos, -numRows ); if ( GetView() ) { wxGridTableMessage msg( this, @@ -1195,7 +1329,7 @@ bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) m_data[row].Insert( wxEmptyString, col ); } } - + UpdateAttrCols( pos, numCols ); if ( GetView() ) { wxGridTableMessage msg( this, @@ -1279,7 +1413,7 @@ bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) } } } - + UpdateAttrCols( pos, -numCols ); if ( GetView() ) { wxGridTableMessage msg( this, -- 2.45.2