From: Vadim Zeitlin Date: Mon, 21 Jan 2013 11:18:55 +0000 (+0000) Subject: Add wxCheckListBox::GetCheckedItems() helper. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ae72623b6469ff6f0e639a4c3d13a732d7fc372e Add wxCheckListBox::GetCheckedItems() helper. This method is similar to wxListBox::GetSelections() and allows to retrieve all checked items at once. Closes #14969. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73422 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index d202fc9ace..3637bd1d59 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -607,6 +607,7 @@ All (GUI): - Fix wrong tab order in wxAuiNotebook after dragging (Mark Barber). - Fix bug in generic wxDataViewCtrl column dragging (jobuz). - Add wxMask::GetBitmap() for wxMSW, wxGTK and wxOSX +- Add wxCheckListBox::GetCheckedItems() (hartwigw). wxGTK: diff --git a/include/wx/checklst.h b/include/wx/checklst.h index a96c2ae3da..80f08b37ec 100644 --- a/include/wx/checklst.h +++ b/include/wx/checklst.h @@ -37,6 +37,8 @@ public: virtual bool IsChecked(unsigned int item) const = 0; virtual void Check(unsigned int item, bool check = true) = 0; + virtual unsigned int GetCheckedItems(wxArrayInt& checkedItems) const; + wxDECLARE_NO_COPY_CLASS(wxCheckListBoxBase); }; diff --git a/interface/wx/checklst.h b/interface/wx/checklst.h index aa5fc98049..9689955c6b 100644 --- a/interface/wx/checklst.h +++ b/interface/wx/checklst.h @@ -151,6 +151,19 @@ public: Index of item whose check status is to be returned. */ bool IsChecked(unsigned int item) const; - + + /** + Return the indices of the checked items. + + @param checkedItems + A reference to the array that is filled with the indices of the + checked items. + @return The number of checked items. + + @see Check(), IsChecked() + + @since 2.9.5 + */ + unsigned int GetCheckedItems(wxArrayInt& checkedItems) const; }; diff --git a/src/common/checklstcmn.cpp b/src/common/checklstcmn.cpp index 6721038c15..fe451702a7 100644 --- a/src/common/checklstcmn.cpp +++ b/src/common/checklstcmn.cpp @@ -99,4 +99,22 @@ wxCONSTRUCTOR_4( wxCheckListBox, wxWindow*, Parent, wxWindowID, Id, \ wxPoint, Position, wxSize, Size ) +// ============================================================================ +// implementation +// ============================================================================ + +unsigned int wxCheckListBoxBase::GetCheckedItems(wxArrayInt& checkedItems) const +{ + unsigned int const numberOfItems = GetCount(); + + checkedItems.clear(); + for ( unsigned int i = 0; i < numberOfItems; ++i ) + { + if ( IsChecked(i) ) + checkedItems.push_back(i); + } + + return checkedItems.size(); +} + #endif diff --git a/tests/controls/checklistboxtest.cpp b/tests/controls/checklistboxtest.cpp index 16f3664d26..cf61babec0 100644 --- a/tests/controls/checklistboxtest.cpp +++ b/tests/controls/checklistboxtest.cpp @@ -67,6 +67,7 @@ void CheckListBoxTestCase::Check() { EventCounter toggled(m_check, wxEVT_COMMAND_CHECKLISTBOX_TOGGLED); + wxArrayInt checkedItems; wxArrayString testitems; testitems.Add("item 0"); testitems.Add("item 1"); @@ -84,6 +85,9 @@ void CheckListBoxTestCase::Check() CPPUNIT_ASSERT_EQUAL(true, m_check->IsChecked(0)); CPPUNIT_ASSERT_EQUAL(false, m_check->IsChecked(1)); + CPPUNIT_ASSERT_EQUAL(1, m_check->GetCheckedItems(checkedItems)); + CPPUNIT_ASSERT_EQUAL(0, checkedItems[0]); + //Make sure a double check of an items doesn't deselect it m_check->Check(0);