From 9d05d9a0097b219102343647c5596db2823de389 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 4 Feb 2014 15:59:52 +0000 Subject: [PATCH] Allow using wxRearrangeList::Check() to change state programmatically. Previous this resulted in an assert and broken behaviour as it didn't update the internally stored state. Do update it now and remove the assert as it isn't possible to distinguish between user code calling Check() and wxGTK doing it itself from wxCheckListBox implementation. Closes #15940. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75786 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/rearrangectrl.h | 4 ++++ src/common/rearrangectrl.cpp | 26 +++++++++++++++++++------- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 5874f7b657..82e7e63731 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -29,6 +29,7 @@ All (GUI): - Add wxHtmlWindow::SetDefaultHTMLCursor() (Jeff A. Marr). - Add default ctor and Create() to wxContextHelpButton (Hanmac). - Send events when toggling wxPropertyGrid nodes from keyboard (Armel Asselin). +- Fix wxRearrangeList::Check() which asserted and misbehaved before. wxGTK: diff --git a/include/wx/rearrangectrl.h b/include/wx/rearrangectrl.h index b54b04fa11..e334ca247e 100644 --- a/include/wx/rearrangectrl.h +++ b/include/wx/rearrangectrl.h @@ -95,6 +95,10 @@ public: bool MoveCurrentUp(); bool MoveCurrentDown(); + + // Override this to keep our m_order array in sync with the real item state. + virtual void Check(unsigned int item, bool check = true); + private: // swap two items at the given positions in the listbox void Swap(int pos1, int pos2); diff --git a/src/common/rearrangectrl.cpp b/src/common/rearrangectrl.cpp index a6865f9036..ebea20dc4c 100644 --- a/src/common/rearrangectrl.cpp +++ b/src/common/rearrangectrl.cpp @@ -78,7 +78,11 @@ bool wxRearrangeList::Create(wxWindow *parent, for ( n = 0; n < count; n++ ) { if ( order[n] >= 0 ) - Check(n); + { + // Be careful to call the base class version here and not our own + // which would also update m_order itself. + wxCheckListBox::Check(n); + } } m_order = order; @@ -137,8 +141,8 @@ void wxRearrangeList::Swap(int pos1, int pos2) // then the checked state const bool checkedTmp = IsChecked(pos1); - Check(pos1, IsChecked(pos2)); - Check(pos2, checkedTmp); + wxCheckListBox::Check(pos1, IsChecked(pos2)); + wxCheckListBox::Check(pos2, checkedTmp); // and finally the client data, if necessary switch ( GetClientDataType() ) @@ -165,15 +169,23 @@ void wxRearrangeList::Swap(int pos1, int pos2) } } +void wxRearrangeList::Check(unsigned int item, bool check) +{ + if ( check == IsChecked(item) ) + return; + + wxCheckListBox::Check(item, check); + + m_order[item] = ~m_order[item]; +} + void wxRearrangeList::OnCheck(wxCommandEvent& event) { // update the internal state to match the new item state const int n = event.GetInt(); - m_order[n] = ~m_order[n]; - - wxASSERT_MSG( (m_order[n] >= 0) == IsChecked(n), - "discrepancy between internal state and GUI" ); + if ( (m_order[n] >= 0) != IsChecked(n) ) + m_order[n] = ~m_order[n]; } // ============================================================================