diff --git a/include/wx/msw/treectrl.h b/include/wx/msw/treectrl.h index d30a9f9755..cdc4aebd32 100644 --- a/include/wx/msw/treectrl.h +++ b/include/wx/msw/treectrl.h @@ -310,9 +310,6 @@ private: // the hash storing the items attributes (indexed by item ids) std::unordered_map> m_attrs; - // true if the hash above is not empty - bool m_hasAnyAttr; - #if wxUSE_DRAGIMAGE // used for dragging wxDragImage *m_dragImage; diff --git a/src/msw/treectrl.cpp b/src/msw/treectrl.cpp index 8bba9f308b..ebb174d0fe 100644 --- a/src/msw/treectrl.cpp +++ b/src/msw/treectrl.cpp @@ -732,7 +732,6 @@ wxTreeCtrl::wxTreeCtrl(wxWindow *parent, void wxTreeCtrl::Init() { m_textCtrl = nullptr; - m_hasAnyAttr = false; #if wxUSE_DRAGIMAGE m_dragImage = nullptr; #endif @@ -853,14 +852,10 @@ wxTreeCtrl::~wxTreeCtrl() { m_isBeingDeleted = true; - // delete any attributes - if ( m_hasAnyAttr ) - { - m_attrs.clear(); - - // prevent TVN_DELETEITEM handler from deleting the attributes again! - m_hasAnyAttr = false; - } + // delete any attributes: it's important to do it explicitly before calling + // DeleteAllItems() to prevent TVN_DELETEITEM handler from deleting the + // attributes again! + m_attrs.clear(); DeleteTextCtrl(); @@ -1207,8 +1202,6 @@ wxItemAttr* wxTreeCtrl::DoGetAttrPtr(const wxTreeItemId& item) const auto it = m_attrs.find(item.m_pItem); if ( it == m_attrs.end() ) { - m_hasAnyAttr = true; - attr = new wxItemAttr; m_attrs[item.m_pItem] = std::unique_ptr(attr); } @@ -3335,10 +3328,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) event.m_item = tv->itemOld.hItem; - if ( m_hasAnyAttr ) - { - m_attrs.erase(tv->itemOld.hItem); - } + m_attrs.erase(tv->itemOld.hItem); } break; @@ -3530,8 +3520,8 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) case CDDS_PREPAINT: // if we've got any items with non standard attributes, // notify us before painting each item - *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW - : CDRF_DODEFAULT; + *result = m_attrs.empty() ? CDRF_DODEFAULT + : CDRF_NOTIFYITEMDRAW; // windows in TreeCtrl use one-based index for item state images, // 0 indexed image is not being used, we're using zero-based index,