From 97c3d00b1930a66f7664d5680fb9bfbf6a02b2bf Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 17 Apr 2023 22:33:51 +0100 Subject: [PATCH] Remove useless wxTreeCtrl::m_hasAnyAttr We can simply check if m_attrs is empty instead. This was added back in 696e1ea0b7cee1394693a0e507bbf49c9863952b (in the last millennium) and apparently there was no simple way to check if wxHashTable used for m_attrs back then was empty, but it's trivial to do it with std::unordered_map<>. --- include/wx/msw/treectrl.h | 3 --- src/msw/treectrl.cpp | 24 +++++++----------------- 2 files changed, 7 insertions(+), 20 deletions(-) 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,