From e78b57308f2a9ac3f10596042f17242da8ab3732 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 1 Jun 2015 00:32:09 +0200 Subject: [PATCH] Get rid of wxListCtrl::m_count in wxMSW. This simplifies the code and fixes the bug with GetItemCount() returning wrong (old) value in wxEVT_LIST_INSERT_ITEM event handler as m_count wasn't updated by then yet. Closes #3793. --- docs/changes.txt | 1 + include/wx/msw/listctrl.h | 2 -- src/msw/listctrl.cpp | 32 ++------------------------------ 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 62d13a4659..1d55b3df2d 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -156,6 +156,7 @@ wxMSW: - Fix reading of not NUL-terminated strings using wxRegKey (Steffen Olszewski). - Fix unexpected change in MDI children order after showing a file dialog. - Don't send events for already selected radio popup menu items (Kinaou Hervé). +- wxListCtrl::GetItemCount() in wxEVT_LIST_INSERT_ITEM is no longer off by 1. wxOSX/Cocoa: diff --git a/include/wx/msw/listctrl.h b/include/wx/msw/listctrl.h index 6458429c1c..48449f8b1c 100644 --- a/include/wx/msw/listctrl.h +++ b/include/wx/msw/listctrl.h @@ -409,8 +409,6 @@ protected: int m_colCount; // Windows doesn't have GetColumnCount so must // keep track of inserted/deleted columns - long m_count; // Keep track of item count to save calls to - // ListView_GetItemCount // all wxMSWListItemData objects we use wxVector m_internalData; diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index 3615fe3cbc..1e53d52128 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -248,7 +248,6 @@ void wxListCtrl::Init() m_ownsImageListState = false; m_colCount = 0; - m_count = 0; m_textCtrl = NULL; m_hasAnyAttr = false; @@ -1145,7 +1144,7 @@ bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos) // Gets the number of items in the list control int wxListCtrl::GetItemCount() const { - return m_count; + return GetHwnd() ? ListView_GetItemCount(GetHwnd()) : 0; } wxSize wxListCtrl::GetItemSpacing() const @@ -1382,10 +1381,6 @@ bool wxListCtrl::DeleteItem(long item) return false; } - m_count--; - wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), - wxT("m_count should match ListView_GetItemCount")); - // the virtual list control doesn't refresh itself correctly, help it if ( IsVirtual() ) { @@ -1711,16 +1706,7 @@ long wxListCtrl::InsertItem(const wxListItem& info) } } - const long rv = ListView_InsertItem(GetHwnd(), & item); - - // failing to insert the item is really unexpected - wxCHECK_MSG( rv != -1, rv, "failed to insert an item in wxListCtrl" ); - - m_count++; - wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), - wxT("m_count should match ListView_GetItemCount")); - - return rv; + return ListView_InsertItem(GetHwnd(), &item); } long wxListCtrl::InsertItem(long index, const wxString& label) @@ -2171,16 +2157,8 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) break; case LVN_DELETEITEM: - if ( m_count == 0 ) - { - // this should be prevented by the post-processing code - // below, but "just in case" - return false; - } - eventType = wxEVT_LIST_DELETE_ITEM; event.m_itemIndex = iItem; - break; case LVN_INSERTITEM: @@ -2576,9 +2554,6 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) // the user should have access to it in OnDeleteAllItems() handler) FreeAllInternalData(); - // the control is empty now, synchronize the cached number of items - // with the real one - m_count = 0; return true; case LVN_DELETEITEM: @@ -3102,9 +3077,6 @@ void wxListCtrl::SetItemCount(long count) { wxLogLastError(wxT("ListView_SetItemCount")); } - m_count = count; - wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), - wxT("m_count should match ListView_GetItemCount")); } void wxListCtrl::RefreshItem(long item)