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
This commit is contained in:
@@ -78,6 +78,19 @@ public:
|
|||||||
: m_itemsSel.GetCount();
|
: 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<unsigned>(-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:
|
private:
|
||||||
// (re)init
|
// (re)init
|
||||||
void Init() { m_count = 0; m_defaultState = false; }
|
void Init() { m_count = 0; m_defaultState = false; }
|
||||||
|
@@ -229,3 +229,42 @@ void wxSelectionStore::SetItemCount(unsigned count)
|
|||||||
// remember the new number of items
|
// remember the new number of items
|
||||||
m_count = count;
|
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++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -45,12 +45,14 @@ private:
|
|||||||
CPPUNIT_TEST( SelectRange );
|
CPPUNIT_TEST( SelectRange );
|
||||||
CPPUNIT_TEST( SetItemCount );
|
CPPUNIT_TEST( SetItemCount );
|
||||||
CPPUNIT_TEST( Clear );
|
CPPUNIT_TEST( Clear );
|
||||||
|
CPPUNIT_TEST( Iterate );
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
void SelectItem();
|
void SelectItem();
|
||||||
void SelectRange();
|
void SelectRange();
|
||||||
void SetItemCount();
|
void SetItemCount();
|
||||||
void Clear();
|
void Clear();
|
||||||
|
void Iterate();
|
||||||
|
|
||||||
// NB: must be even
|
// NB: must be even
|
||||||
static const unsigned NUM_ITEMS;
|
static const unsigned NUM_ITEMS;
|
||||||
@@ -131,3 +133,19 @@ void SelStoreTestCase::Clear()
|
|||||||
CPPUNIT_ASSERT_EQUAL( 0u, m_store->GetSelectedCount() );
|
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));
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user