+ wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
+ te.m_code = event.KeyCode();
+ te.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( te );
+
+ if (m_current == 0)
+ {
+ event.Skip();
+ return;
+ }
+
+ bool is_multiple=(GetWindowStyleFlag() & wxTR_MULTIPLE);
+ bool extended_select=(event.ShiftDown() && is_multiple);
+ bool unselect_others=!(extended_select || (event.ControlDown() && is_multiple));
+
+ switch (event.KeyCode())
+ {
+ case '+':
+ case WXK_ADD:
+ if (m_current->HasPlus() && !IsExpanded(m_current))
+ {
+ Expand(m_current);
+ }
+ break;
+
+ case '-':
+ case WXK_SUBTRACT:
+ if (IsExpanded(m_current))
+ {
+ Collapse(m_current);
+ }
+ break;
+
+ case '*':
+ case WXK_MULTIPLY:
+ Toggle(m_current);
+ break;
+
+ case ' ':
+ case WXK_RETURN:
+ {
+ wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
+ event.m_item = m_current;
+ event.m_code = 0;
+ event.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( event );
+ }
+ break;
+
+ // up goes to the previous sibling or to the last of its children if
+ // it's expanded
+ case WXK_UP:
+ {
+ wxTreeItemId prev = GetPrevSibling( m_current );
+ if (!prev)
+ {
+ prev = GetParent( m_current );
+ if (prev)
+ {
+ long cockie = 0;
+ wxTreeItemId current = m_current;
+ if (current == GetFirstChild( prev, cockie ))
+ {
+ // otherwise we return to where we came from
+ SelectItem( prev, unselect_others, extended_select );
+ EnsureVisible( prev );
+ break;
+ }
+ }
+ }
+ if (prev)
+ {
+ while ( IsExpanded(prev) && HasChildren(prev) )
+ {
+ wxTreeItemId child = GetLastChild(prev);
+ if ( child )
+ {
+ prev = child;
+ }
+ }
+
+ SelectItem( prev, unselect_others, extended_select );
+ EnsureVisible( prev );
+ }
+ }
+ break;
+
+ // left arrow goes to the parent
+ case WXK_LEFT:
+ {
+ wxTreeItemId prev = GetParent( m_current );
+ if (prev)
+ {
+ EnsureVisible( prev );
+ SelectItem( prev, unselect_others, extended_select );
+ }
+ }
+ break;
+
+ case WXK_RIGHT:
+ // this works the same as the down arrow except that we also expand the
+ // item if it wasn't expanded yet
+ Expand(m_current);
+ // fall through
+
+ case WXK_DOWN:
+ {
+ if (IsExpanded(m_current) && HasChildren(m_current))
+ {
+ long cookie = 0;
+ wxTreeItemId child = GetFirstChild( m_current, cookie );
+ SelectItem( child, unselect_others, extended_select );
+ EnsureVisible( child );
+ }
+ else
+ {
+ wxTreeItemId next = GetNextSibling( m_current );
+ if (next == 0)
+ {
+ wxTreeItemId current = m_current;
+ while (current && !next)
+ {
+ current = GetParent( current );
+ if (current) next = GetNextSibling( current );
+ }
+ }
+ if (next != 0)
+ {
+ SelectItem( next, unselect_others, extended_select );
+ EnsureVisible( next );
+ }
+ }
+ }
+ break;
+
+ // <End> selects the last visible tree item
+ case WXK_END:
+ {
+ wxTreeItemId last = GetRootItem();
+
+ while ( last.IsOk() && IsExpanded(last) )
+ {
+ wxTreeItemId lastChild = GetLastChild(last);
+
+ // it may happen if the item was expanded but then all of
+ // its children have been deleted - so IsExpanded() returned
+ // TRUE, but GetLastChild() returned invalid item
+ if ( !lastChild )
+ break;
+
+ last = lastChild;
+ }
+
+ if ( last.IsOk() )
+ {
+ EnsureVisible( last );
+ SelectItem( last, unselect_others, extended_select );
+ }
+ }
+ break;
+
+ // <Home> selects the root item
+ case WXK_HOME:
+ {
+ wxTreeItemId prev = GetRootItem();
+ if (prev)
+ {
+ EnsureVisible( prev );
+ SelectItem( prev, unselect_others, extended_select );
+ }
+ }
+ break;
+
+ default:
+ event.Skip();
+ }
+}