1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/generic/treectrl.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/settings.h"
36 #include "wx/dynarray.h"
37 #include "wx/dcclient.h"
38 #include "wx/msgdlg.h"
40 // -----------------------------------------------------------------------------
42 // -----------------------------------------------------------------------------
44 class WXDLLEXPORT wxGenericTreeItem
;
46 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayTreeItems
);
48 // -----------------------------------------------------------------------------
50 // -----------------------------------------------------------------------------
53 class WXDLLEXPORT wxGenericTreeItem
57 wxGenericTreeItem() { m_data
= NULL
; }
58 wxGenericTreeItem( wxGenericTreeItem
*parent
,
61 int image
, int selImage
,
62 wxTreeItemData
*data
);
67 wxArrayTreeItems
& GetChildren() { return m_children
; }
69 const wxString
& GetText() const { return m_text
; }
70 int GetImage() const { return m_image
; }
71 int GetSelectedImage() const { return m_selImage
; }
72 wxTreeItemData
*GetData() const { return m_data
; }
74 void SetText( const wxString
&text
, wxDC
& dc
);
75 void SetImage(int image
) { m_image
= image
; }
76 void SetSelectedImage(int image
) { m_selImage
= image
; }
77 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
79 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
81 void SetBold(bool bold
) { m_isBold
= bold
; }
83 int GetX() const { return m_x
; }
84 int GetY() const { return m_y
; }
86 void SetHeight(int h
) { m_height
= h
; }
88 void SetX(int x
) { m_x
= x
; }
89 void SetY(int y
) { m_y
= y
; }
91 wxGenericTreeItem
*GetParent() const { return m_parent
; }
94 // deletes all children notifying the treectrl about it if !NULL pointer
96 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
97 // FIXME don't know what is it for
100 // get count of all children (and grand children if 'recursively')
101 size_t GetChildrenCount(bool recursively
= TRUE
) const;
103 void Insert(wxGenericTreeItem
*child
, size_t index
)
104 { m_children
.Insert(child
, index
); }
106 void SetCross( int x
, int y
);
107 void GetSize( int &x
, int &y
);
109 // return the item at given position (or NULL if no item), onButton is TRUE
110 // if the point belongs to the item's button, otherwise it lies on the
112 wxGenericTreeItem
*HitTest( const wxPoint
& point
, bool &onButton
);
114 void Expand() { m_isCollapsed
= FALSE
; }
115 void Collapse() { m_isCollapsed
= TRUE
; }
117 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
120 bool HasChildren() const { return !m_children
.IsEmpty(); }
121 bool HasHilight() const { return m_hasHilight
; }
122 bool IsExpanded() const { return !m_isCollapsed
; }
123 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
124 bool IsBold() const { return m_isBold
; }
132 wxTreeItemData
*m_data
;
134 // use bitfields to save size
135 int m_isCollapsed
:1;
136 int m_hasHilight
:1; // same as focused
137 int m_hasPlus
:1; // used for item which doesn't have
138 // children but still has a [+] button
139 int m_isBold
:1; // render the label in bold font
142 long m_height
, m_width
;
143 int m_xCross
, m_yCross
;
145 wxArrayTreeItems m_children
;
146 wxGenericTreeItem
*m_parent
;
149 // =============================================================================
151 // =============================================================================
153 // -----------------------------------------------------------------------------
155 // -----------------------------------------------------------------------------
157 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
159 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
160 : wxNotifyEvent( commandType
, id
)
163 m_itemOld
= (wxGenericTreeItem
*)NULL
;
166 // -----------------------------------------------------------------------------
168 // -----------------------------------------------------------------------------
170 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
171 const wxString
& text
,
173 int image
, int selImage
,
174 wxTreeItemData
*data
)
178 m_selImage
= selImage
;
181 m_xCross
= m_yCross
= 0;
185 m_isCollapsed
= TRUE
;
186 m_hasHilight
= FALSE
;
192 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
195 wxGenericTreeItem::~wxGenericTreeItem()
199 wxASSERT_MSG( m_children
.IsEmpty(),
200 _T("please call DeleteChildren() before deleting the item") );
203 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
205 size_t count
= m_children
.Count();
206 for ( size_t n
= 0; n
< count
; n
++ )
208 wxGenericTreeItem
*child
= m_children
[n
];
211 tree
->SendDeleteEvent(child
);
214 child
->DeleteChildren(tree
);
221 void wxGenericTreeItem::SetText( const wxString
&text
, wxDC
& dc
)
225 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
228 void wxGenericTreeItem::Reset()
235 m_height
= m_width
= 0;
242 m_isCollapsed
= TRUE
;
244 m_parent
= (wxGenericTreeItem
*)NULL
;
247 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
249 size_t count
= m_children
.Count();
253 size_t total
= count
;
254 for ( size_t n
= 0; n
< count
; n
++ )
256 total
+= m_children
[n
]->GetChildrenCount();
262 void wxGenericTreeItem::SetCross( int x
, int y
)
268 void wxGenericTreeItem::GetSize( int &x
, int &y
)
270 if ( y
< m_y
) y
= m_y
;
271 int width
= m_x
+ m_width
;
272 if (width
> x
) x
= width
;
276 size_t count
= m_children
.Count();
277 for ( size_t n
= 0; n
< count
; n
++ )
279 m_children
[n
]->GetSize( x
, y
);
284 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
287 if ((point
.y
> m_y
) && (point
.y
< m_y
+ m_height
))
290 // Because that is the size of the plus sign, RR
291 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
292 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
293 (IsExpanded() || HasPlus()))
299 /* TODO: we should do a query here like
300 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h ); */
302 if (m_image
!= -1) w
+= 24;
304 if ((point
.x
> m_x
) && (point
.x
< m_x
+w
))
314 size_t count
= m_children
.Count();
315 for ( size_t n
= 0; n
< count
; n
++ )
317 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
327 // -----------------------------------------------------------------------------
328 // wxTreeCtrl implementation
329 // -----------------------------------------------------------------------------
331 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
333 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
334 EVT_PAINT (wxTreeCtrl::OnPaint
)
335 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
336 EVT_CHAR (wxTreeCtrl::OnChar
)
337 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
338 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
339 EVT_IDLE (wxTreeCtrl::OnIdle
)
342 // -----------------------------------------------------------------------------
343 // construction/destruction
344 // -----------------------------------------------------------------------------
345 void wxTreeCtrl::Init()
348 m_anchor
= (wxGenericTreeItem
*) NULL
;
358 m_hilightBrush
= new wxBrush
360 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
365 m_imageListState
= (wxImageList
*) NULL
;
370 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
371 const wxPoint
& pos
, const wxSize
& size
,
373 const wxValidator
&validator
,
374 const wxString
& name
)
378 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
380 SetValidator( validator
);
382 SetBackgroundColour( *wxWHITE
);
383 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
388 wxTreeCtrl::~wxTreeCtrl()
390 wxDELETE( m_hilightBrush
);
395 // -----------------------------------------------------------------------------
397 // -----------------------------------------------------------------------------
399 size_t wxTreeCtrl::GetCount() const
401 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
404 void wxTreeCtrl::SetIndent(unsigned int indent
)
411 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
418 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
420 wxCHECK_MSG( item
.IsOk(), 0u, _T("invalid tree item") );
422 return item
.m_pItem
->GetChildrenCount(recursively
);
425 // -----------------------------------------------------------------------------
426 // functions to work with tree items
427 // -----------------------------------------------------------------------------
429 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
431 wxCHECK_MSG( item
.IsOk(), _T(""), _T("invalid tree item") );
433 return item
.m_pItem
->GetText();
436 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
438 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
440 return item
.m_pItem
->GetImage();
443 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
445 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
447 return item
.m_pItem
->GetSelectedImage();
450 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
452 wxCHECK_MSG( item
.IsOk(), NULL
, _T("invalid tree item") );
454 return item
.m_pItem
->GetData();
457 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
459 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
462 wxGenericTreeItem
*pItem
= item
.m_pItem
;
463 pItem
->SetText(text
, dc
);
467 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
469 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
471 wxGenericTreeItem
*pItem
= item
.m_pItem
;
472 pItem
->SetImage(image
);
476 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
478 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
480 wxGenericTreeItem
*pItem
= item
.m_pItem
;
481 pItem
->SetSelectedImage(image
);
485 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
487 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
489 item
.m_pItem
->SetData(data
);
492 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
494 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
496 wxGenericTreeItem
*pItem
= item
.m_pItem
;
497 pItem
->SetHasPlus(has
);
501 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
503 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
505 // avoid redrawing the tree if no real change
506 wxGenericTreeItem
*pItem
= item
.m_pItem
;
507 if ( pItem
->IsBold() != bold
)
509 pItem
->SetBold(bold
);
514 // -----------------------------------------------------------------------------
515 // item status inquiries
516 // -----------------------------------------------------------------------------
518 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
520 wxFAIL_MSG(_T("not implemented"));
525 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
527 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
529 return !item
.m_pItem
->GetChildren().IsEmpty();
532 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
534 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
536 return item
.m_pItem
->IsExpanded();
539 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
541 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
543 return item
.m_pItem
->HasHilight();
546 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
548 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
550 return item
.m_pItem
->IsBold();
553 // -----------------------------------------------------------------------------
555 // -----------------------------------------------------------------------------
557 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
559 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
561 return item
.m_pItem
->GetParent();
564 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
566 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
569 return GetNextChild(item
, cookie
);
572 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
574 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
576 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
577 if ( (size_t)cookie
< children
.Count() )
579 return children
.Item(cookie
++);
583 // there are no more of them
584 return wxTreeItemId();
588 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
590 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
592 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
593 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
596 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
598 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
600 wxGenericTreeItem
*i
= item
.m_pItem
;
601 wxGenericTreeItem
*parent
= i
->GetParent();
602 if ( parent
== NULL
)
604 // root item doesn't have any siblings
605 return wxTreeItemId();
608 wxArrayTreeItems
& siblings
= parent
->GetChildren();
609 int index
= siblings
.Index(i
);
610 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
612 size_t n
= (size_t)(index
+ 1);
613 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
616 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
618 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
620 wxGenericTreeItem
*i
= item
.m_pItem
;
621 wxGenericTreeItem
*parent
= i
->GetParent();
622 if ( parent
== NULL
)
624 // root item doesn't have any siblings
625 return wxTreeItemId();
628 wxArrayTreeItems
& siblings
= parent
->GetChildren();
629 int index
= siblings
.Index(i
);
630 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
632 return index
== 0 ? wxTreeItemId()
633 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
636 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
638 wxFAIL_MSG(_T("not implemented"));
640 return wxTreeItemId();
643 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
645 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
647 wxFAIL_MSG(_T("not implemented"));
649 return wxTreeItemId();
652 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
654 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
656 wxFAIL_MSG(_T("not implemented"));
658 return wxTreeItemId();
661 // -----------------------------------------------------------------------------
663 // -----------------------------------------------------------------------------
665 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
667 const wxString
& text
,
668 int image
, int selImage
,
669 wxTreeItemData
*data
)
671 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
674 // should we give a warning here?
675 return AddRoot(text
, image
, selImage
, data
);
679 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
686 data
->m_pItem
= item
;
689 parent
->Insert( item
, previous
);
696 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
697 int image
, int selImage
,
698 wxTreeItemData
*data
)
700 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), _T("tree can have only one root") );
703 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
704 image
, selImage
, data
);
707 data
->m_pItem
= m_anchor
;
710 AdjustMyScrollbars();
716 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
717 const wxString
& text
,
718 int image
, int selImage
,
719 wxTreeItemData
*data
)
721 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
724 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
725 const wxTreeItemId
& idPrevious
,
726 const wxString
& text
,
727 int image
, int selImage
,
728 wxTreeItemData
*data
)
730 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
733 // should we give a warning here?
734 return AddRoot(text
, image
, selImage
, data
);
737 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
738 wxASSERT_MSG( index
!= wxNOT_FOUND
,
739 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
740 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
743 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
744 const wxString
& text
,
745 int image
, int selImage
,
746 wxTreeItemData
*data
)
748 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
751 // should we give a warning here?
752 return AddRoot(text
, image
, selImage
, data
);
755 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
756 image
, selImage
, data
);
759 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
761 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
763 event
.SetEventObject( this );
764 ProcessEvent( event
);
767 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
769 wxGenericTreeItem
*item
= itemId
.m_pItem
;
770 item
->DeleteChildren(this);
775 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
777 wxGenericTreeItem
*item
= itemId
.m_pItem
;
778 wxGenericTreeItem
*parent
= item
->GetParent();
782 parent
->GetChildren().Remove(item
);
785 item
->DeleteChildren(this);
786 SendDeleteEvent(item
);
792 void wxTreeCtrl::DeleteAllItems()
796 m_anchor
->DeleteChildren(this);
805 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
807 wxGenericTreeItem
*item
= itemId
.m_pItem
;
809 if ( !item
->HasPlus() )
812 if ( item
->IsExpanded() )
815 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
817 event
.SetEventObject( this );
818 if ( ProcessEvent( event
) && event
.m_code
)
820 // cancelled by program
825 CalculatePositions();
827 RefreshSubtree(item
);
829 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
830 ProcessEvent( event
);
833 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
835 wxGenericTreeItem
*item
= itemId
.m_pItem
;
837 if ( !item
->IsExpanded() )
840 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
842 event
.SetEventObject( this );
843 if ( ProcessEvent( event
) && event
.m_code
)
845 // cancelled by program
851 wxArrayTreeItems
& children
= item
->GetChildren();
852 size_t count
= children
.Count();
853 for ( size_t n
= 0; n
< count
; n
++ )
855 Collapse(children
[n
]);
858 CalculatePositions();
860 RefreshSubtree(item
);
862 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
863 ProcessEvent( event
);
866 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
869 DeleteChildren(item
);
872 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
874 wxGenericTreeItem
*item
= itemId
.m_pItem
;
876 if ( item
->IsExpanded() )
882 void wxTreeCtrl::Unselect()
886 m_current
->SetHilight( FALSE
);
887 RefreshLine( m_current
);
891 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
893 wxGenericTreeItem
*item
= itemId
.m_pItem
;
895 if ( m_current
!= item
)
897 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
899 event
.m_itemOld
= m_current
;
900 event
.SetEventObject( this );
901 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
906 m_current
->SetHilight( FALSE
);
907 RefreshLine( m_current
);
911 m_current
->SetHilight( TRUE
);
912 RefreshLine( m_current
);
914 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
915 GetEventHandler()->ProcessEvent( event
);
919 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
921 wxGenericTreeItem
*gitem
= item
.m_pItem
;
923 // first expand all parent branches
924 wxGenericTreeItem
*parent
= gitem
->GetParent();
925 while ( parent
&& !parent
->IsExpanded() )
929 parent
= parent
->GetParent();
932 // now scroll to the item
933 int item_y
= gitem
->GetY();
937 ViewStart( &start_x
, &start_y
);
942 GetClientSize( &client_w
, &client_h
);
944 if (item_y
< start_y
+3)
948 m_anchor
->GetSize( x
, y
);
950 int x_pos
= GetScrollPos( wxHORIZONTAL
);
951 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
953 else if (item_y
> start_y
+client_h
-16)
957 m_anchor
->GetSize( x
, y
);
959 int x_pos
= GetScrollPos( wxHORIZONTAL
);
960 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
964 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
966 wxFAIL_MSG(_T("not implemented"));
969 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
970 wxClassInfo
* WXUNUSED(textCtrlClass
) )
972 wxFAIL_MSG(_T("not implemented"));
974 return (wxTextCtrl
*)NULL
;
977 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
979 wxFAIL_MSG(_T("not implemented"));
981 return (wxTextCtrl
*)NULL
;
984 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
986 wxFAIL_MSG(_T("not implemented"));
989 // FIXME: tree sorting functions are not reentrant and not MT-safe!
990 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
992 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
993 wxGenericTreeItem
**item2
)
995 wxCHECK_MSG( s_treeBeingSorted
, 0, _T("bug in wxTreeCtrl::SortChildren()") );
997 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1000 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1001 const wxTreeItemId
& item2
)
1003 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1006 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1008 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1010 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1012 wxCHECK_RET( !s_treeBeingSorted
,
1013 _T("wxTreeCtrl::SortChildren is not reentrant") );
1015 wxArrayTreeItems
& children
= item
->GetChildren();
1016 if ( children
.Count() > 1 )
1018 s_treeBeingSorted
= this;
1019 children
.Sort(tree_ctrl_compare_func
);
1020 s_treeBeingSorted
= NULL
;
1024 //else: don't make the tree dirty as nothing changed
1027 wxImageList
*wxTreeCtrl::GetImageList() const
1029 return m_imageListNormal
;
1032 wxImageList
*wxTreeCtrl::GetStateImageList() const
1034 return m_imageListState
;
1037 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1039 m_imageListNormal
= imageList
;
1040 // calculate a m_lineHeight value from the image sizes
1043 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1047 n
= m_imageListNormal
->GetImageCount();
1048 for(int i
= 0; i
< n
; i
++)
1050 m_imageListNormal
->GetSize(i
, width
, height
);
1051 height
+= height
/5; //20% extra spacing
1052 if(height
> m_lineHeight
) m_lineHeight
= height
;
1056 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1058 m_imageListState
= imageList
;
1061 // -----------------------------------------------------------------------------
1063 // -----------------------------------------------------------------------------
1065 void wxTreeCtrl::AdjustMyScrollbars()
1071 m_anchor
->GetSize( x
, y
);
1072 y
+= 2*m_lineHeight
;
1073 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1074 int y_pos
= GetScrollPos( wxVERTICAL
);
1075 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1079 SetScrollbars( 0, 0, 0, 0 );
1083 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1085 // render bold items in bold
1091 fontOld
= dc
.GetFont();
1094 // VZ: is there any better way to make a bold variant of old font?
1095 fontNew
= wxFont( fontOld
.GetPointSize(),
1096 fontOld
.GetFamily(),
1099 fontOld
.GetUnderlined());
1100 dc
.SetFont(fontNew
);
1104 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1110 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1114 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1116 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1119 else if (item
->GetImage() != -1)
1121 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1125 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
1126 if(m_lineHeight
> total_h
) total_h
= m_lineHeight
;
1128 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), image_w
+text_w
+4, total_h
);
1130 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1132 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1133 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1135 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1136 wxIMAGELIST_DRAW_TRANSPARENT
);
1137 dc
.DestroyClippingRegion();
1139 else if (item
->GetImage() != -1)
1141 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1142 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1144 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1145 wxIMAGELIST_DRAW_TRANSPARENT
);
1146 dc
.DestroyClippingRegion();
1149 dc
.SetBackgroundMode(wxTRANSPARENT
);
1150 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1151 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1153 // restore normal font for bold items
1156 dc
.SetFont( fontOld
);
1160 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1162 int horizX
= level
*m_indent
;
1164 item
->SetX( horizX
+m_indent
+m_spacing
);
1165 item
->SetY( y
-m_lineHeight
/2 );
1166 item
->SetHeight( m_lineHeight
);
1168 item
->SetCross( horizX
+m_indent
, y
);
1172 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1173 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1175 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1177 int startX
= horizX
;
1178 int endX
= horizX
+ (m_indent
-5);
1180 // if (!item->HasChildren()) endX += (m_indent+5);
1181 if (!item
->HasChildren()) endX
+= 20;
1183 dc
.DrawLine( startX
, y
, endX
, y
);
1185 if (item
->HasPlus())
1187 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1188 dc
.SetPen( *wxGREY_PEN
);
1189 dc
.SetBrush( *wxWHITE_BRUSH
);
1190 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1191 dc
.SetPen( *wxBLACK_PEN
);
1192 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1194 if (!item
->IsExpanded())
1196 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1200 if (item
->HasHilight())
1202 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1204 dc
.SetBrush( *m_hilightBrush
);
1207 dc
.SetPen( *wxBLACK_PEN
);
1209 dc
.SetPen( *wxTRANSPARENT_PEN
);
1211 PaintItem(item
, dc
);
1213 dc
.SetPen( *wxBLACK_PEN
);
1214 dc
.SetTextForeground( *wxBLACK
);
1215 dc
.SetBrush( *wxWHITE_BRUSH
);
1219 dc
.SetBrush( *wxWHITE_BRUSH
);
1220 dc
.SetPen( *wxTRANSPARENT_PEN
);
1222 PaintItem(item
, dc
);
1224 dc
.SetPen( *wxBLACK_PEN
);
1228 if (item
->IsExpanded())
1232 wxArrayTreeItems
& children
= item
->GetChildren();
1233 size_t count
= children
.Count();
1234 for ( size_t n
= 0; n
< count
; n
++ )
1238 PaintLevel( children
[n
], dc
, level
+1, y
);
1241 // it may happen that the item is expanded but has no items (when you
1242 // delete all its children for example) - don't draw the vertical line
1245 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1249 // -----------------------------------------------------------------------------
1250 // wxWindows callbacks
1251 // -----------------------------------------------------------------------------
1253 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1261 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1263 dc
.SetPen( m_dottedPen
);
1264 if(GetImageList() == NULL
)
1265 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1267 int y
= m_lineHeight
/ 2 + 2;
1268 PaintLevel( m_anchor
, dc
, 0, y
);
1271 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1275 if (m_current
) RefreshLine( m_current
);
1278 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1282 if (m_current
) RefreshLine( m_current
);
1285 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1287 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1288 te
.m_code
= event
.KeyCode();
1289 te
.SetEventObject( this );
1290 GetEventHandler()->ProcessEvent( te
);
1298 switch (event
.KeyCode())
1302 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1310 if (IsExpanded(m_current
))
1312 Collapse(m_current
);
1324 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1325 event
.m_item
= m_current
;
1327 event
.SetEventObject( this );
1328 GetEventHandler()->ProcessEvent( event
);
1332 // up goes to the previous sibling or to the last of its children if
1336 wxTreeItemId prev
= GetPrevSibling( m_current
);
1339 prev
= GetParent( m_current
);
1343 wxTreeItemId current
= m_current
;
1344 if (current
== GetFirstChild( prev
, cockie
))
1346 // otherwise we return to where we came from
1348 EnsureVisible( prev
);
1355 while ( IsExpanded(prev
) && HasChildren(prev
) )
1357 wxTreeItemId child
= GetLastChild(prev
);
1365 EnsureVisible( prev
);
1370 // left arrow goes to the parent
1373 wxTreeItemId prev
= GetParent( m_current
);
1376 EnsureVisible( prev
);
1383 // this works the same as the down arrow except that we also expand the
1384 // item if it wasn't expanded yet
1390 if (IsExpanded(m_current
) && HasChildren(m_current
))
1393 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1394 SelectItem( child
);
1395 EnsureVisible( child
);
1399 wxTreeItemId next
= GetNextSibling( m_current
);
1402 wxTreeItemId current
= m_current
;
1403 while (current
&& !next
)
1405 current
= GetParent( current
);
1406 if (current
) next
= GetNextSibling( current
);
1412 EnsureVisible( next
);
1418 // <End> selects the last visible tree item
1421 wxTreeItemId last
= GetRootItem();
1423 while ( last
.IsOk() && IsExpanded(last
) )
1425 wxTreeItemId lastChild
= GetLastChild(last
);
1427 // it may happen if the item was expanded but then all of
1428 // its children have been deleted - so IsExpanded() returned
1429 // TRUE, but GetLastChild() returned invalid item
1438 EnsureVisible( last
);
1444 // <Home> selects the root item
1447 wxTreeItemId prev
= GetRootItem();
1450 EnsureVisible( prev
);
1461 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1463 wxClientDC
dc(this);
1465 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1466 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1468 bool onButton
= FALSE
;
1469 return m_anchor
->HitTest( wxPoint(x
, y
), onButton
);
1472 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1474 if (!event
.LeftIsDown()) m_dragCount
= 0;
1476 if ( !(event
.LeftDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1478 if ( !m_anchor
) return;
1480 wxClientDC
dc(this);
1482 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1483 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1485 bool onButton
= FALSE
;
1486 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1488 if (item
== NULL
) return; /* we hit the blank area */
1490 if (event
.Dragging())
1492 if (m_dragCount
== 2) /* small drag latency (3?) */
1496 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1497 nevent
.m_item
= m_current
;
1498 nevent
.SetEventObject(this);
1499 GetEventHandler()->ProcessEvent(nevent
);
1508 if (!IsSelected(item
))
1509 SelectItem(item
); /* we dont support multiple selections, BTW */
1511 if (event
.LeftDClick())
1513 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1514 event
.m_item
= item
;
1516 event
.SetEventObject( this );
1517 GetEventHandler()->ProcessEvent( event
);
1526 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1528 /* after all changes have been done to the tree control,
1529 * we actually redraw the tree when everything is over */
1536 CalculatePositions();
1538 AdjustMyScrollbars();
1541 // -----------------------------------------------------------------------------
1543 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1545 int horizX
= level
*m_indent
;
1547 item
->SetX( horizX
+m_indent
+m_spacing
);
1548 item
->SetY( y
-m_lineHeight
/2 );
1549 item
->SetHeight( m_lineHeight
);
1551 if ( !item
->IsExpanded() )
1553 // we dont need to calculate collapsed branches
1557 wxArrayTreeItems
& children
= item
->GetChildren();
1558 size_t count
= children
.Count();
1559 for ( size_t n
= 0; n
< count
; n
++ )
1562 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
1566 void wxTreeCtrl::CalculatePositions()
1568 if ( !m_anchor
) return;
1570 wxClientDC
dc(this);
1573 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1575 dc
.SetPen( m_dottedPen
);
1576 if(GetImageList() == NULL
)
1577 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1579 int y
= m_lineHeight
/ 2 + 2;
1580 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
1583 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1585 wxClientDC
dc(this);
1590 GetClientSize( &cw
, &ch
);
1593 rect
.x
= dc
.LogicalToDeviceX( 0 );
1595 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1598 Refresh( TRUE
, &rect
);
1600 AdjustMyScrollbars();
1603 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1605 wxClientDC
dc(this);
1609 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1610 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1612 rect
.height
= dc
.GetCharHeight() + 6;
1614 Refresh( TRUE
, &rect
);