From a23b3783b6a4d197ddf97b571cb182466a1931cc Mon Sep 17 00:00:00 2001 From: Kvaz1r Date: Sun, 5 Jan 2020 13:42:16 +0200 Subject: [PATCH] Add precondition checks to operations in the listctrl sample Avoid showing assert failures in debug builds if a menu item not applicable in the current state is selected and just show an error instead. Closes #18572. Closes https://github.com/wxWidgets/wxWidgets/pull/1694 --- samples/listctrl/listtest.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/samples/listctrl/listtest.cpp b/samples/listctrl/listtest.cpp index 5cc6cf6dd4..1c4213098b 100644 --- a/samples/listctrl/listtest.cpp +++ b/samples/listctrl/listtest.cpp @@ -402,6 +402,12 @@ void MyFrame::OnToggleMacUseGeneric(wxCommandEvent& event) void MyFrame::OnGoTo(wxCommandEvent& WXUNUSED(event)) { + if ( m_listCtrl->IsEmpty() ) + { + wxLogMessage("Attempt go to item #3 when list is empty"); + return; + } + long index = 3; m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED); @@ -426,7 +432,14 @@ void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event)) { - m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); + if ( !m_listCtrl->IsEmpty() ) + { + m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); + } + else + { + wxLogMessage("Attempt toggle first item when list is empty"); + } } void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))