From e25d86916860b1aaff9681e36b8ab9420bd020b9 Mon Sep 17 00:00:00 2001 From: Jorge Moraleda Date: Mon, 20 Apr 2020 17:11:24 -0700 Subject: [PATCH] Added dedicated command binding of HasPage tab of DVC sample so that the radio button column has always exactly one cell checked. Before this fix this tab was incorrectly bound to the same function that controlled the ListCtrl in the third tab. --- samples/dataview/dataview.cpp | 47 ++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index f6cab09aa8..3b840491ce 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -170,6 +170,9 @@ private: enum Lang { Lang_English, Lang_French }; void FillIndexList(Lang lang); + // HasValue page. + void OnHasValueValueChanged(wxDataViewEvent& event); + wxNotebook* m_notebook; @@ -1038,7 +1041,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l lc->AppendItem( data ); } - lc->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &MyFrame::OnListValueChanged, this); + lc->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &MyFrame::OnHasValueValueChanged, this); } break; } @@ -1772,3 +1775,45 @@ void MyFrame::OnIndexListSelectionChanged(wxDataViewEvent& event) wxLogMessage("Selected week day: %s", weekday); } + +// ---------------------------------------------------------------------------- +// MyFrame - event handlers for the HasValue (wxDataViewListCtrl) page +// ---------------------------------------------------------------------------- + +void MyFrame::OnHasValueValueChanged(wxDataViewEvent& event) +{ + // Ignore changes coming from our own SetToggleValue() calls below. + if ( m_eventFromProgram ) + { + m_eventFromProgram = false; + return; + } + + wxDataViewListCtrl* const lc = static_cast(m_ctrl[Page_HasValue]); + + const int columnToggle = 1; + + // Handle selecting a radio button by unselecting all the other ones. + if ( event.GetColumn() == columnToggle ) + { + const int rowChanged = lc->ItemToRow(event.GetItem()); + if ( lc->GetToggleValue(rowChanged, columnToggle) ) + { + for ( int row = 0; row < lc->GetItemCount(); ++row ) + { + if ( row != rowChanged ) + { + m_eventFromProgram = true; + lc->SetToggleValue(false, row, columnToggle); + } + } + } + else // The item was cleared. + { + // Explicitly check it back, we want to always have exactly one + // checked radio item in this column. + m_eventFromProgram = true; + lc->SetToggleValue(true, rowChanged, columnToggle); + } + } +}