Improve behaviour of Left/Right arrow keys in wxGenericTreeCtrl

Make the left arrow collapse the item if it's currently expanded before
falling back to its previous behaviour of going to the item parent and
the right arrow to go to the first child if the item is already
expanded.

This is compatible with the native MSW control behaviour, but mostly
just more useful and convenient.

Closes #18684.
This commit is contained in:
Anton Triest 2020-03-16 00:55:04 +01:00 committed by Vadim Zeitlin
parent 730b3d1a44
commit 6b04c9938d

View File

@ -3081,8 +3081,8 @@ void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
// ' ' | return : activate
// up : go up (not last children!)
// down : go down
// left : go to parent
// right : open if parent and go next
// left : collapse or go to parent
// right : expand or go to first child
// home : go to root
// end : go to last item without opening parents
// alnum : start or continue searching for the item with this prefix
@ -3210,28 +3210,57 @@ void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
}
break;
// left arrow goes to the parent
// left arrow collapses or goes to the parent if it's not expanded
case WXK_LEFT:
{
wxTreeItemId prev = GetItemParent( m_current );
if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
if (m_current == GetRootItem().m_pItem && HasFlag(wxTR_HIDE_ROOT))
{
// don't go to root if it is hidden
prev = GetPrevSibling( m_current );
// don't try to collapse hidden root item
// (which can be the current one when the tree is empty)
break;
}
if (prev)
if (IsExpanded(m_current))
{
DoSelectItem( prev, unselect_others, extended_select );
Collapse(m_current);
}
else
{
// select parent unless it's the hidden root
wxTreeItemId parent = GetItemParent(m_current);
if (parent && (parent != GetRootItem() || !HasFlag(wxTR_HIDE_ROOT)))
{
DoSelectItem(parent, unselect_others, extended_select);
}
}
}
break;
// right arrow expands or goes to first child if it's already expanded
case WXK_RIGHT:
// right arrow just expand the item will be fine
if (m_current != GetRootItem().m_pItem || !HasFlag(wxTR_HIDE_ROOT))
Expand(m_current);
//else: don't try to expand hidden root item (which can be the
// current one when the tree is empty)
{
if (m_current == GetRootItem().m_pItem && HasFlag(wxTR_HIDE_ROOT))
{
// don't try to expand hidden root item
// (which can be the current one when the tree is empty)
break;
}
if (HasChildren(m_current))
{
if (IsExpanded(m_current))
{
wxTreeItemIdValue cookie;
wxTreeItemId child = GetFirstChild(m_current, cookie);
if (child)
{
DoSelectItem(child, unselect_others, extended_select);
}
}
else
{
Expand(m_current);
}
}
}
break;
case WXK_DOWN: