+ wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
+ te.m_code = (int)event.KeyCode();
+ te.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( te );
+
+ if ( (m_current == 0) || (m_key_current == 0) )
+ {
+ event.Skip();
+ return;
+ }
+
+ // how should the selection work for this event?
+ bool is_multiple, extended_select, unselect_others;
+ EventFlagsToSelType(GetWindowStyleFlag(),
+ event.ShiftDown(),
+ event.ControlDown(),
+ is_multiple, extended_select, unselect_others);
+
+ // + : Expand
+ // - : Collaspe
+ // * : Toggle Expand/Collapse
+ // ' ' | return : activate
+ // up : go up (not last children!)
+ // down : go down
+ // left : go to parent
+ // right : open if parent and go next
+ // home : go to root
+ // end : go to last item without opening parents
+ 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_key_current );
+ if (!prev)
+ {
+ prev = GetParent( m_key_current );
+ if (prev)
+ {
+ long cockie = 0;
+ wxTreeItemId current = m_key_current;
+ if (current == GetFirstChild( prev, cockie ))
+ {
+ // otherwise we return to where we came from
+ SelectItem( prev, unselect_others, extended_select );
+ m_key_current=prev.m_pItem;
+ EnsureVisible( prev );
+ break;
+ }
+ }
+ }
+ if (prev)
+ {
+ while ( IsExpanded(prev) && HasChildren(prev) )
+ {
+ wxTreeItemId child = GetLastChild(prev);
+ if ( child )
+ {
+ prev = child;
+ }
+ }
+
+ SelectItem( prev, unselect_others, extended_select );
+ m_key_current=prev.m_pItem;
+ 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_key_current) && HasChildren(m_key_current))
+ {
+ long cookie = 0;
+ wxTreeItemId child = GetFirstChild( m_key_current, cookie );
+ SelectItem( child, unselect_others, extended_select );
+ m_key_current=child.m_pItem;
+ EnsureVisible( child );
+ }
+ else
+ {
+ wxTreeItemId next = GetNextSibling( m_key_current );
+// if (next == 0)
+ if (!next)
+ {
+ wxTreeItemId current = m_key_current;
+ while (current && !next)
+ {
+ current = GetParent( current );
+ if (current) next = GetNextSibling( current );
+ }
+ }
+// if (next != 0)
+ if (next)
+ {
+ SelectItem( next, unselect_others, extended_select );
+ m_key_current=next.m_pItem;
+ 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();
+ }
+}
+
+wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
+{
+ // We have to call this here because the label in
+ // question might just have been added and no screen
+ // update taken place.
+ if (m_dirty) wxYield();
+
+ wxClientDC dc(this);
+ PrepareDC(dc);
+ wxCoord x = dc.DeviceToLogicalX( point.x );
+ wxCoord y = dc.DeviceToLogicalY( point.y );
+ int w, h;
+ GetSize(&w, &h);
+
+ flags=0;
+ if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
+ if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
+ if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
+ if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
+
+ return m_anchor->HitTest( wxPoint(x, y), this, flags);
+}
+
+/* **** */
+
+void wxTreeCtrl::Edit( const wxTreeItemId& item )
+{
+ if (!item.IsOk()) return;
+
+ m_currentEdit = item.m_pItem;
+
+ wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
+ te.m_item = m_currentEdit;
+ te.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( te );
+
+ if (!te.IsAllowed()) return;
+
+ // We have to call this here because the label in
+ // question might just have been added and no screen
+ // update taken place.
+ if (m_dirty) wxYield();
+
+ wxString s = m_currentEdit->GetText();
+ int x = m_currentEdit->GetX();
+ int y = m_currentEdit->GetY();
+ int w = m_currentEdit->GetWidth();
+ int h = m_currentEdit->GetHeight();
+
+ int image_h = 0;
+ int image_w = 0;
+
+ int image = m_currentEdit->GetCurrentImage();
+ if ( image != NO_IMAGE )
+ {
+ if ( m_imageListNormal )
+ {
+ m_imageListNormal->GetSize( image, image_w, image_h );
+ image_w += 4;
+ }
+ else
+ {
+ wxFAIL_MSG(_T("you must create an image list to use images!"));
+ }
+ }
+ x += image_w;
+ w -= image_w + 4; // I don't know why +4 is needed
+
+ wxClientDC dc(this);
+ PrepareDC( dc );
+ x = dc.LogicalToDeviceX( x );
+ y = dc.LogicalToDeviceY( y );
+
+ wxTreeTextCtrl *text = new wxTreeTextCtrl(
+ this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
+ text->SetFocus();
+}
+
+void wxTreeCtrl::OnRenameTimer()
+{
+ Edit( m_current );
+}
+
+void wxTreeCtrl::OnRenameAccept()
+{
+ wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
+ le.m_item = m_currentEdit;
+ le.SetEventObject( this );
+ le.m_label = m_renameRes;
+ GetEventHandler()->ProcessEvent( le );
+
+ if (!le.IsAllowed()) return;
+
+ SetItemText( m_currentEdit, m_renameRes );