From d058c09e9ff148e13e8c172d02664af0bddfa2d8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 27 Sep 2014 20:46:15 +0000 Subject: [PATCH] Add possibility to iterate over all selected items in wxSelectionStore. This is necessary for retrieving all the selected items at once: while doing this is not recommended for a control with a potentially very large number of items, it must be possible to allow using wxSelectionStore for wxDataViewCtrl implementation as wxDataViewCtrl must implement its GetSelections() method. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77902 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/selstore.h | 13 +++++++++++++ src/generic/selstore.cpp | 39 +++++++++++++++++++++++++++++++++++++ tests/misc/selstoretest.cpp | 18 +++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/include/wx/selstore.h b/include/wx/selstore.h index 62ff2298ce..e52ac1cbc0 100644 --- a/include/wx/selstore.h +++ b/include/wx/selstore.h @@ -78,6 +78,19 @@ public: : m_itemsSel.GetCount(); } + // type of a "cookie" used to preserve the iteration state, this is an + // opaque type, don't rely on its current representation + typedef size_t IterationState; + + // constant representing absence of selection and hence end of iteration + static const unsigned NO_SELECTION = static_cast(-1); + + // get the first selected item in index order, return NO_SELECTION if none + unsigned GetFirstSelectedItem(IterationState& cookie) const; + + // get the next selected item, return NO_SELECTION if no more + unsigned GetNextSelectedItem(IterationState& cookie) const; + private: // (re)init void Init() { m_count = 0; m_defaultState = false; } diff --git a/src/generic/selstore.cpp b/src/generic/selstore.cpp index 6f606c335a..8ea6066c65 100644 --- a/src/generic/selstore.cpp +++ b/src/generic/selstore.cpp @@ -229,3 +229,42 @@ void wxSelectionStore::SetItemCount(unsigned count) // remember the new number of items m_count = count; } + +// ---------------------------------------------------------------------------- +// Iteration +// ---------------------------------------------------------------------------- + +unsigned wxSelectionStore::GetFirstSelectedItem(IterationState& cookie) const +{ + cookie = 0; + + return GetNextSelectedItem(cookie); +} + +unsigned wxSelectionStore::GetNextSelectedItem(IterationState& cookie) const +{ + if ( m_defaultState ) + { + // We have no choice but to iterate over all items in this case. It + // shouldn't be that bad in practice because (almost) all items are + // supposed to be selected if m_defaultState == true anyhow. + for ( unsigned item = cookie; ; item++ ) + { + if ( item >= m_count ) + return NO_SELECTION; + + if ( IsSelected(item) ) + { + cookie = item + 1; + return item; + } + } + } + else // Simple case when we directly have the selected items. + { + if ( cookie >= m_itemsSel.size() ) + return NO_SELECTION; + + return m_itemsSel[cookie++]; + } +} diff --git a/tests/misc/selstoretest.cpp b/tests/misc/selstoretest.cpp index 512e059dd6..3459abd4da 100644 --- a/tests/misc/selstoretest.cpp +++ b/tests/misc/selstoretest.cpp @@ -45,12 +45,14 @@ private: CPPUNIT_TEST( SelectRange ); CPPUNIT_TEST( SetItemCount ); CPPUNIT_TEST( Clear ); + CPPUNIT_TEST( Iterate ); CPPUNIT_TEST_SUITE_END(); void SelectItem(); void SelectRange(); void SetItemCount(); void Clear(); + void Iterate(); // NB: must be even static const unsigned NUM_ITEMS; @@ -131,3 +133,19 @@ void SelStoreTestCase::Clear() CPPUNIT_ASSERT_EQUAL( 0u, m_store->GetSelectedCount() ); } +void SelStoreTestCase::Iterate() +{ + m_store->SelectRange(NUM_ITEMS/2 - 1, NUM_ITEMS/2 + 1); + + wxSelectionStore::IterationState cookie; + CPPUNIT_ASSERT_EQUAL(NUM_ITEMS/2 - 1, m_store->GetFirstSelectedItem(cookie)); + CPPUNIT_ASSERT_EQUAL(NUM_ITEMS/2, m_store->GetNextSelectedItem(cookie)); + CPPUNIT_ASSERT_EQUAL(NUM_ITEMS/2 + 1, m_store->GetNextSelectedItem(cookie)); + + CPPUNIT_ASSERT_EQUAL(wxSelectionStore::NO_SELECTION, m_store->GetNextSelectedItem(cookie)); + + + m_store->SelectRange(0, NUM_ITEMS - 1); + m_store->SelectItem(0, false); + CPPUNIT_ASSERT_EQUAL(1, m_store->GetFirstSelectedItem(cookie)); +}