+ int horizX = level*m_indent;
+
+ item->SetX( horizX+m_indent+m_spacing );
+ item->SetY( y-m_lineHeight/2 );
+ item->SetHeight( m_lineHeight );
+
+ item->SetCross( horizX+m_indent, y );
+
+ int oldY = y;
+
+ int exposed_x = dc.LogicalToDeviceX( 0 );
+ int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 );
+
+ if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
+ {
+ int startX = horizX;
+ int endX = horizX + (m_indent-5);
+
+// if (!item->HasChildren()) endX += (m_indent+5);
+ if (!item->HasChildren()) endX += 20;
+
+ dc.DrawLine( startX, y, endX, y );
+
+ if (item->HasPlus())
+ {
+ dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
+ dc.SetPen( *wxGREY_PEN );
+ dc.SetBrush( *wxWHITE_BRUSH );
+ dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
+ dc.SetPen( *wxBLACK_PEN );
+ dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
+
+ if (!item->IsExpanded())
+ {
+ dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
+ }
+ }
+
+ if (item->HasHilight())
+ {
+ dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
+
+ dc.SetBrush( *m_hilightBrush );
+
+ if (m_hasFocus)
+ dc.SetPen( *wxBLACK_PEN );
+ else
+ dc.SetPen( *wxTRANSPARENT_PEN );
+
+ PaintItem(item, dc);
+
+ dc.SetPen( *wxBLACK_PEN );
+ dc.SetTextForeground( *wxBLACK );
+ dc.SetBrush( *wxWHITE_BRUSH );
+ }
+ else
+ {
+ dc.SetBrush( *wxWHITE_BRUSH );
+ dc.SetPen( *wxTRANSPARENT_PEN );
+
+ PaintItem(item, dc);
+
+ dc.SetPen( *wxBLACK_PEN );
+ }
+ }
+
+ if (item->IsExpanded())
+ {
+ int semiOldY = y;
+
+ wxArrayTreeItems& children = item->GetChildren();
+ size_t count = children.Count();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ y += m_lineHeight;
+ semiOldY = y;
+ PaintLevel( children[n], dc, level+1, y );
+ }
+
+ // it may happen that the item is expanded but has no items (when you
+ // delete all its children for example) - don't draw the vertical line
+ // in this case
+ if (count > 0)
+ dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
+ }
+}
+
+// -----------------------------------------------------------------------------
+// wxWindows callbacks
+// -----------------------------------------------------------------------------
+
+void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
+{
+ if ( !m_anchor )
+ return;
+
+ wxPaintDC dc(this);
+ PrepareDC( dc );
+
+ dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
+
+ dc.SetPen( m_dottedPen );
+ if(GetImageList() == NULL)
+ m_lineHeight = (int)(dc.GetCharHeight() + 4);
+
+ int y = m_lineHeight / 2 + 2;
+ PaintLevel( m_anchor, dc, 0, y );
+}
+
+void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
+{
+ m_hasFocus = TRUE;
+
+ if (m_current) RefreshLine( m_current );
+}
+
+void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
+{
+ m_hasFocus = FALSE;
+
+ if (m_current) RefreshLine( m_current );
+}
+
+void wxTreeCtrl::OnChar( wxKeyEvent &event )
+{
+ 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;
+ }
+
+ 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 );
+ EnsureVisible( prev );
+ break;
+ }
+ }
+ }
+ if (prev)
+ {
+ while ( IsExpanded(prev) && HasChildren(prev) )
+ {
+ wxTreeItemId child = GetLastChild(prev);
+ if ( child )
+ {
+ prev = child;
+ }
+ }
+
+ SelectItem( prev );
+ EnsureVisible( prev );
+ }
+ }
+ break;
+
+ // left arrow goes to the parent
+ case WXK_LEFT:
+ {
+ wxTreeItemId prev = GetParent( m_current );
+ if (prev)
+ {
+ EnsureVisible( prev );
+ SelectItem( prev );
+ }
+ }
+ 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 );
+ 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 );
+ 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 );
+ }
+ }
+ break;
+
+ // <Home> selects the root item
+ case WXK_HOME:
+ {
+ wxTreeItemId prev = GetRootItem();
+ if (prev)
+ {
+ EnsureVisible( prev );
+ SelectItem( prev );
+ }
+ }
+ break;
+
+ default:
+ event.Skip();
+ }
+}
+
+wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags))
+{
+ wxClientDC dc(this);
+ PrepareDC(dc);
+ long x = dc.DeviceToLogicalX( (long)point.x );
+ long y = dc.DeviceToLogicalY( (long)point.y );
+
+ bool onButton = FALSE;
+ return m_anchor->HitTest( wxPoint(x, y), onButton );
+}
+
+void wxTreeCtrl::OnMouse( wxMouseEvent &event )
+{
+ if (!event.LeftIsDown()) m_dragCount = 0;
+
+ if ( !(event.LeftDown() || event.LeftDClick() || event.Dragging()) ) return;
+
+ if ( !m_anchor ) return;
+
+ wxClientDC dc(this);
+ PrepareDC(dc);
+ long x = dc.DeviceToLogicalX( (long)event.GetX() );
+ long y = dc.DeviceToLogicalY( (long)event.GetY() );
+
+ bool onButton = FALSE;
+ wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton );
+
+ if (item == NULL) return; /* we hit the blank area */
+
+ if (event.Dragging())
+ {
+ if (m_dragCount == 2) /* small drag latency (3?) */
+ {
+ m_dragCount = 0;
+
+ wxTreeEvent nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG, GetId());
+ nevent.m_item = m_current;
+ nevent.SetEventObject(this);
+ GetEventHandler()->ProcessEvent(nevent);
+ }
+ else
+ {
+ m_dragCount++;
+ }
+ return;
+ }
+
+ if (!IsSelected(item))
+ SelectItem(item); /* we dont support multiple selections, BTW */
+
+ if (event.LeftDClick())
+ {
+ wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
+ event.m_item = item;
+ event.m_code = 0;
+ event.SetEventObject( this );
+ GetEventHandler()->ProcessEvent( event );
+ }
+
+ if (onButton)
+ {
+ Toggle( item );
+ }
+}
+
+void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
+{
+ /* after all changes have been done to the tree control,
+ * we actually redraw the tree when everything is over */
+
+ if (!m_dirty)
+ return;
+
+ m_dirty = FALSE;
+
+ CalculatePositions();
+
+ AdjustMyScrollbars();
+}
+
+// -----------------------------------------------------------------------------
+
+void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
+{
+ int horizX = level*m_indent;
+
+ item->SetX( horizX+m_indent+m_spacing );
+ item->SetY( y-m_lineHeight/2 );
+ item->SetHeight( m_lineHeight );
+
+ if ( !item->IsExpanded() )
+ {
+ // we dont need to calculate collapsed branches
+ return;
+ }
+
+ wxArrayTreeItems& children = item->GetChildren();
+ size_t count = children.Count();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ y += m_lineHeight;
+ CalculateLevel( children[n], dc, level+1, y ); // recurse
+ }
+}
+
+void wxTreeCtrl::CalculatePositions()
+{
+ if ( !m_anchor ) return;
+
+ wxClientDC dc(this);
+ PrepareDC( dc );
+
+ dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
+
+ dc.SetPen( m_dottedPen );
+ if(GetImageList() == NULL)
+ m_lineHeight = (int)(dc.GetCharHeight() + 4);
+
+ int y = m_lineHeight / 2 + 2;
+ CalculateLevel( m_anchor, dc, 0, y ); // start recursion
+}
+
+void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
+{
+ wxClientDC dc(this);
+ PrepareDC(dc);
+
+ int cw = 0;
+ int ch = 0;
+ GetClientSize( &cw, &ch );
+
+ wxRect rect;
+ rect.x = dc.LogicalToDeviceX( 0 );
+ rect.width = cw;
+ rect.y = dc.LogicalToDeviceY( item->GetY() );
+ rect.height = ch;
+
+ Refresh( TRUE, &rect );
+
+ AdjustMyScrollbars();
+}