Handle multiple item insertion and deletion in wxSelectionStore.

Rename the existing but not implemented and never used OnItemAdd() method to
OnItemsInserted() and add OnItemsDeleted(), which is more efficient than
OnItemDelete() when many items are being removed from the control at once.

This is not used yet, but will be used in wxDataViewCtrl soon and maybe in the
other controls later.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77904 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2014-09-27 20:46:21 +00:00
parent 06ab4da3f3
commit adf8f9d0cd
3 changed files with 101 additions and 3 deletions

View File

@@ -190,6 +190,31 @@ bool wxSelectionStore::SelectRange(unsigned itemFrom, unsigned itemTo,
// callbacks
// ----------------------------------------------------------------------------
void wxSelectionStore::OnItemsInserted(unsigned item, unsigned numItems)
{
const size_t count = m_itemsSel.GetCount();
size_t idx = m_itemsSel.IndexForInsert(item);
for ( size_t i = idx; i < count; i++ )
{
m_itemsSel[i] += numItems;
}
if ( m_defaultState )
{
// All newly inserted items are not selected, so if the default state
// is to be selected, we need to manually add them to the deselected
// items indices.
for ( unsigned n = item; n < item + numItems; n++ )
{
m_itemsSel.AddAt(item, idx++);
}
}
m_count += numItems;
}
void wxSelectionStore::OnItemDelete(unsigned item)
{
size_t count = m_itemsSel.GetCount(),
@@ -211,8 +236,44 @@ void wxSelectionStore::OnItemDelete(unsigned item)
m_itemsSel[i++]--;
}
m_count--;
}
bool wxSelectionStore::OnItemsDeleted(unsigned item, unsigned numItems)
{
bool anyDeletedInSelItems = false,
allDeletedInSelItems = true;
size_t i = m_itemsSel.IndexForInsert(item);
const unsigned firstAfterDeleted = item + numItems;
while ( i < m_itemsSel.size() )
{
if ( m_itemsSel[i] < firstAfterDeleted )
{
// This item is going to be deleted, so remove it from the
// selected indices entirely. Notice that we do not update i here
// as it now refers to the next element.
m_itemsSel.RemoveAt(i);
anyDeletedInSelItems = true;
}
else
{
// This item remains, just update its index.
m_itemsSel[i++] -= numItems;
allDeletedInSelItems = false;
}
}
m_count -= numItems;
return m_defaultState ? allDeletedInSelItems : anyDeletedInSelItems;
}
void wxSelectionStore::SetItemCount(unsigned count)
{
// forget about all items whose indices are now invalid if the size