diff --git a/include/wx/generic/private/grid.h b/include/wx/generic/private/grid.h index 077aa8849f..a6c1ccc8b8 100644 --- a/include/wx/generic/private/grid.h +++ b/include/wx/generic/private/grid.h @@ -24,54 +24,7 @@ WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs, class WXDLLIMPEXP_ADV); -struct wxGridCellWithAttr -{ - wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) - : coords(row, col), attr(attr_) - { - wxASSERT( attr ); - } - - wxGridCellWithAttr(const wxGridCellWithAttr& other) - : coords(other.coords), - attr(other.attr) - { - attr->IncRef(); - } - - wxGridCellWithAttr& operator=(const wxGridCellWithAttr& other) - { - coords = other.coords; - if (attr != other.attr) - { - attr->DecRef(); - attr = other.attr; - attr->IncRef(); - } - return *this; - } - - void ChangeAttr(wxGridCellAttr* new_attr) - { - if (attr != new_attr) - { - // "Delete" (i.e. DecRef) the old attribute. - attr->DecRef(); - attr = new_attr; - // Take ownership of the new attribute, i.e. no IncRef. - } - } - - ~wxGridCellWithAttr() - { - attr->DecRef(); - } - - wxGridCellCoords coords; - wxGridCellAttr *attr; -}; - -WX_DECLARE_HASH_MAP_WITH_DECL(wxLongLong_t, wxGridCellWithAttr*, +WX_DECLARE_HASH_MAP_WITH_DECL(wxLongLong_t, wxGridCellAttr*, wxIntegerHash, wxIntegerEqual, wxGridCoordsToAttrMap, class WXDLLIMPEXP_CORE); diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 40affcd430..261c7d2520 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -788,7 +788,7 @@ wxGridCellAttrData::~wxGridCellAttrData() it != m_attrs.end(); ++it ) { - delete it->second; + it->second->DecRef(); } m_attrs.clear(); @@ -796,32 +796,27 @@ wxGridCellAttrData::~wxGridCellAttrData() void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) { - // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not - // touch attribute's reference counting explicitly, since this - // is managed by class wxGridCellWithAttr wxGridCoordsToAttrMap::iterator it = FindIndex(row, col); if ( it == m_attrs.end() ) { if ( attr ) { // add the attribute - m_attrs[CoordsToKey(row, col)] = new wxGridCellWithAttr(row, col, attr); + m_attrs[CoordsToKey(row, col)] = attr; } //else: nothing to do } else // we already have an attribute for this cell { + // See note near DecRef() in wxGridRowOrColAttrData::SetAttr for why + // this also works when old and new attribute are the same. + it->second->DecRef(); + + // Change or remove the attribute. if ( attr ) - { - // change the attribute - it->second->ChangeAttr(attr); - } + it->second = attr; else - { - // remove this attribute - delete it->second; m_attrs.erase(it); - } } } @@ -832,7 +827,7 @@ wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const wxGridCoordsToAttrMap::iterator it = FindIndex(row, col); if ( it != m_attrs.end() ) { - attr = it->second->attr; + attr = it->second; attr->IncRef(); } @@ -864,8 +859,7 @@ void UpdateCellAttrRowsOrCols(wxGridCoordsToAttrMap& attrs, int editPos, ++it ) { const wxGridCoordsToAttrMap::key_type oldCoords = it->first; - wxGridCellWithAttr* cellWithAttr = it->second; - wxGridCellAttr* cellAttr = cellWithAttr->attr; + wxGridCellAttr* cellAttr = it->second; int cellRows, cellCols; cellAttr->GetSize(&cellRows, &cellCols); @@ -873,9 +867,6 @@ void UpdateCellAttrRowsOrCols(wxGridCoordsToAttrMap& attrs, int editPos, int cellRow, cellCol; KeyToCoords(oldCoords, &cellRow, &cellCol); - wxGridCellCoords& coords = cellWithAttr->coords; - wxASSERT(wxGridCellCoords(cellRow, cellCol) == coords); - const int cellPos = isEditingRows ? cellRow : cellCol; if ( cellPos < editPos ) @@ -944,7 +935,7 @@ void UpdateCellAttrRowsOrCols(wxGridCoordsToAttrMap& attrs, int editPos, } // Set attribute at old/unmodified coords. - newAttrs[oldCoords] = cellWithAttr; + newAttrs[oldCoords] = cellAttr; continue; } @@ -953,7 +944,7 @@ void UpdateCellAttrRowsOrCols(wxGridCoordsToAttrMap& attrs, int editPos, { // This row/col is deleted and the cell doesn't exist any longer: // Remove the attribute. - delete cellWithAttr; + cellAttr->DecRef(); continue; } @@ -965,8 +956,7 @@ void UpdateCellAttrRowsOrCols(wxGridCoordsToAttrMap& attrs, int editPos, { // Rows/cols inserted or deleted (and this cell still exists): // Adjust cell coords. - coords.Set(cellRow + editRowCount, cellCol + editColCount); - newAttrs[newCoords] = cellWithAttr; + newAttrs[newCoords] = cellAttr; // Nothing more to do: cell is not an inside cell of a multicell. continue; @@ -982,15 +972,14 @@ void UpdateCellAttrRowsOrCols(wxGridCoordsToAttrMap& attrs, int editPos, // On a position that still exists after deletion but main cell // of multicell is within deletion range so the multicell is gone: // Remove the attribute. - delete cellWithAttr; + cellAttr->DecRef(); continue; } // Rows/cols inserted or deleted (and this inside cell still exists): // Adjust (inside) cell coords. - coords.Set(cellRow + editRowCount, cellCol + editColCount); - newAttrs[newCoords] = cellWithAttr; + newAttrs[newCoords] = cellAttr; if ( mainPos >= editPos ) { @@ -1017,7 +1006,7 @@ void UpdateCellAttrRowsOrCols(wxGridCoordsToAttrMap& attrs, int editPos, const int row = cellRow + adjustRows, col = cellCol + adjustCols; - newAttrs[CoordsToKey(row, col)] = new wxGridCellWithAttr(row, col, attr); + newAttrs[CoordsToKey(row, col)] = attr; } }