Fix wxTreeCtrl::ScrollTo() with hidden root item in wxMSW

Scroll to the topmost item, as the generic version does, instead of
crashing.

Closes #23534.
This commit is contained in:
Vadim Zeitlin 2023-05-10 19:08:00 +01:00
parent d39aac0fa8
commit 54a8a6ac16
2 changed files with 21 additions and 1 deletions

View file

@ -1979,7 +1979,18 @@ void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
{ {
if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) ) HTREEITEM htItem = HITEM(item);
if ( IsHiddenRoot(item) )
{
// Calling TreeView_SelectSetFirstVisible() with the invisible root
// item would simply crash (#23534), so don't do this. However also
// don't just assert and return as this works in the generic version,
// so do the same thing as it does here, and scroll to the top item.
htItem = TreeView_GetChild(GetHwnd(), htItem);
}
if ( !TreeView_SelectSetFirstVisible(GetHwnd(), htItem) )
{ {
wxLogLastError(wxT("TreeView_SelectSetFirstVisible")); wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
} }

View file

@ -57,6 +57,7 @@ private:
CPPUNIT_TEST( Focus ); CPPUNIT_TEST( Focus );
CPPUNIT_TEST( Bold ); CPPUNIT_TEST( Bold );
CPPUNIT_TEST( Visible ); CPPUNIT_TEST( Visible );
CPPUNIT_TEST( Scroll );
CPPUNIT_TEST( Sort ); CPPUNIT_TEST( Sort );
WXUISIM_TEST( KeyNavigation ); WXUISIM_TEST( KeyNavigation );
CPPUNIT_TEST( HasChildren ); CPPUNIT_TEST( HasChildren );
@ -85,6 +86,7 @@ private:
void Focus(); void Focus();
void Bold(); void Bold();
void Visible(); void Visible();
void Scroll();
void Sort(); void Sort();
void KeyNavigation(); void KeyNavigation();
void HasChildren(); void HasChildren();
@ -596,6 +598,13 @@ void TreeCtrlTestCase::Visible()
CPPUNIT_ASSERT(!m_tree->GetPrevVisible(m_root)); CPPUNIT_ASSERT(!m_tree->GetPrevVisible(m_root));
} }
void TreeCtrlTestCase::Scroll()
{
// This trivial test just checks that calling ScrollTo() with the root item
// doesn't crash any longer, as it used to do when the root item was hidden.
m_tree->ScrollTo(m_root);
}
void TreeCtrlTestCase::Sort() void TreeCtrlTestCase::Sort()
{ {
wxTreeItemId zitem = m_tree->AppendItem(m_root, "zzzz"); wxTreeItemId zitem = m_tree->AppendItem(m_root, "zzzz");