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 "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 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
291 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
292 (IsExpanded() || HasPlus()))
299 if (m_image
!= -1) w
+= 20;
301 if ((point
.x
> m_x
) && (point
.x
< m_x
+w
))
311 size_t count
= m_children
.Count();
312 for ( size_t n
= 0; n
< count
; n
++ )
314 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, onButton
);
324 // -----------------------------------------------------------------------------
325 // wxTreeCtrl implementation
326 // -----------------------------------------------------------------------------
328 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
330 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
331 EVT_PAINT (wxTreeCtrl::OnPaint
)
332 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
333 EVT_CHAR (wxTreeCtrl::OnChar
)
334 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
335 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
336 EVT_IDLE (wxTreeCtrl::OnIdle
)
339 // -----------------------------------------------------------------------------
340 // construction/destruction
341 // -----------------------------------------------------------------------------
342 void wxTreeCtrl::Init()
345 m_anchor
= (wxGenericTreeItem
*) NULL
;
354 m_hilightBrush
= new wxBrush
356 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
361 m_imageListState
= (wxImageList
*) NULL
;
366 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
367 const wxPoint
& pos
, const wxSize
& size
,
369 const wxValidator
&validator
,
370 const wxString
& name
)
374 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
376 SetValidator( validator
);
378 SetBackgroundColour( *wxWHITE
);
379 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
384 wxTreeCtrl::~wxTreeCtrl()
386 wxDELETE( m_hilightBrush
);
391 // -----------------------------------------------------------------------------
393 // -----------------------------------------------------------------------------
395 size_t wxTreeCtrl::GetCount() const
397 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
400 void wxTreeCtrl::SetIndent(unsigned int indent
)
406 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
408 wxCHECK_MSG( item
.IsOk(), 0u, "invalid tree item" );
410 return item
.m_pItem
->GetChildrenCount(recursively
);
413 // -----------------------------------------------------------------------------
414 // functions to work with tree items
415 // -----------------------------------------------------------------------------
417 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
419 wxCHECK_MSG( item
.IsOk(), "", "invalid tree item" );
421 return item
.m_pItem
->GetText();
424 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
426 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
428 return item
.m_pItem
->GetImage();
431 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
433 wxCHECK_MSG( item
.IsOk(), -1, "invalid tree item" );
435 return item
.m_pItem
->GetSelectedImage();
438 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
440 wxCHECK_MSG( item
.IsOk(), NULL
, "invalid tree item" );
442 return item
.m_pItem
->GetData();
445 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
447 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
450 wxGenericTreeItem
*pItem
= item
.m_pItem
;
451 pItem
->SetText(text
, dc
);
455 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
457 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
459 wxGenericTreeItem
*pItem
= item
.m_pItem
;
460 pItem
->SetImage(image
);
464 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
466 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
468 wxGenericTreeItem
*pItem
= item
.m_pItem
;
469 pItem
->SetSelectedImage(image
);
473 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
475 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
477 item
.m_pItem
->SetData(data
);
480 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
482 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
484 wxGenericTreeItem
*pItem
= item
.m_pItem
;
485 pItem
->SetHasPlus(has
);
489 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
491 wxCHECK_RET( item
.IsOk(), "invalid tree item" );
493 // avoid redrawing the tree if no real change
494 wxGenericTreeItem
*pItem
= item
.m_pItem
;
495 if ( pItem
->IsBold() != bold
)
497 pItem
->SetBold(bold
);
502 // -----------------------------------------------------------------------------
503 // item status inquiries
504 // -----------------------------------------------------------------------------
506 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
508 wxFAIL_MSG("not implemented");
513 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
515 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
517 return !item
.m_pItem
->GetChildren().IsEmpty();
520 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
522 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
524 return item
.m_pItem
->IsExpanded();
527 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
529 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
531 return item
.m_pItem
->HasHilight();
534 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
536 wxCHECK_MSG( item
.IsOk(), FALSE
, "invalid tree item" );
538 return item
.m_pItem
->IsBold();
541 // -----------------------------------------------------------------------------
543 // -----------------------------------------------------------------------------
545 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
547 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
549 return item
.m_pItem
->GetParent();
552 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
554 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
557 return GetNextChild(item
, cookie
);
560 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
562 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
564 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
565 if ( (size_t)cookie
< children
.Count() )
567 return children
.Item(cookie
++);
571 // there are no more of them
572 return wxTreeItemId();
576 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
578 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
580 wxArrayTreeItems
& children
= item
.m_pItem
->GetChildren();
581 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
584 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
586 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
588 wxGenericTreeItem
*i
= item
.m_pItem
;
589 wxGenericTreeItem
*parent
= i
->GetParent();
590 if ( parent
== NULL
)
592 // root item doesn't have any siblings
593 return wxTreeItemId();
596 wxArrayTreeItems
& siblings
= parent
->GetChildren();
597 int index
= siblings
.Index(i
);
598 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
600 size_t n
= (size_t)(index
+ 1);
601 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
604 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
606 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
608 wxGenericTreeItem
*i
= item
.m_pItem
;
609 wxGenericTreeItem
*parent
= i
->GetParent();
610 if ( parent
== NULL
)
612 // root item doesn't have any siblings
613 return wxTreeItemId();
616 wxArrayTreeItems
& siblings
= parent
->GetChildren();
617 int index
= siblings
.Index(i
);
618 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
620 return index
== 0 ? wxTreeItemId()
621 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
624 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
626 wxFAIL_MSG("not implemented");
628 return wxTreeItemId();
631 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
633 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
635 wxFAIL_MSG("not implemented");
637 return wxTreeItemId();
640 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
642 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), "invalid tree item" );
644 wxFAIL_MSG("not implemented");
646 return wxTreeItemId();
649 // -----------------------------------------------------------------------------
651 // -----------------------------------------------------------------------------
653 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
655 const wxString
& text
,
656 int image
, int selImage
,
657 wxTreeItemData
*data
)
659 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
662 // should we give a warning here?
663 return AddRoot(text
, image
, selImage
, data
);
667 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
674 data
->m_pItem
= item
;
677 parent
->Insert( item
, previous
);
684 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
685 int image
, int selImage
,
686 wxTreeItemData
*data
)
688 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
691 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
692 image
, selImage
, data
);
695 data
->m_pItem
= m_anchor
;
698 AdjustMyScrollbars();
704 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
705 const wxString
& text
,
706 int image
, int selImage
,
707 wxTreeItemData
*data
)
709 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
712 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
713 const wxTreeItemId
& idPrevious
,
714 const wxString
& text
,
715 int image
, int selImage
,
716 wxTreeItemData
*data
)
718 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
721 // should we give a warning here?
722 return AddRoot(text
, image
, selImage
, data
);
725 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
726 wxASSERT_MSG( index
!= wxNOT_FOUND
,
727 "previous item in wxTreeCtrl::InsertItem() is not a sibling" );
728 return DoInsertItem(parentId
, (size_t)index
, text
, image
, selImage
, data
);
731 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
732 const wxString
& text
,
733 int image
, int selImage
,
734 wxTreeItemData
*data
)
736 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
739 // should we give a warning here?
740 return AddRoot(text
, image
, selImage
, data
);
743 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
744 image
, selImage
, data
);
747 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
749 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
751 event
.SetEventObject( this );
752 ProcessEvent( event
);
755 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
757 wxGenericTreeItem
*item
= itemId
.m_pItem
;
758 item
->DeleteChildren(this);
763 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
765 wxGenericTreeItem
*item
= itemId
.m_pItem
;
766 wxGenericTreeItem
*parent
= item
->GetParent();
770 parent
->GetChildren().Remove(item
);
773 item
->DeleteChildren(this);
774 SendDeleteEvent(item
);
780 void wxTreeCtrl::DeleteAllItems()
784 m_anchor
->DeleteChildren(this);
793 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
795 wxGenericTreeItem
*item
= itemId
.m_pItem
;
797 if ( !item
->HasPlus() )
800 if ( item
->IsExpanded() )
803 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
805 event
.SetEventObject( this );
806 if ( ProcessEvent( event
) && event
.m_code
)
808 // cancelled by program
813 CalculatePositions();
815 RefreshSubtree(item
);
817 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
818 ProcessEvent( event
);
821 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
823 wxGenericTreeItem
*item
= itemId
.m_pItem
;
825 if ( !item
->IsExpanded() )
828 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
830 event
.SetEventObject( this );
831 if ( ProcessEvent( event
) && event
.m_code
)
833 // cancelled by program
839 wxArrayTreeItems
& children
= item
->GetChildren();
840 size_t count
= children
.Count();
841 for ( size_t n
= 0; n
< count
; n
++ )
843 Collapse(children
[n
]);
846 CalculatePositions();
848 RefreshSubtree(item
);
850 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
851 ProcessEvent( event
);
854 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
857 DeleteChildren(item
);
860 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
862 wxGenericTreeItem
*item
= itemId
.m_pItem
;
864 if ( item
->IsExpanded() )
870 void wxTreeCtrl::Unselect()
874 m_current
->SetHilight( FALSE
);
875 RefreshLine( m_current
);
879 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
)
881 wxGenericTreeItem
*item
= itemId
.m_pItem
;
883 if ( m_current
!= item
)
885 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
887 event
.m_itemOld
= m_current
;
888 event
.SetEventObject( this );
889 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
894 m_current
->SetHilight( FALSE
);
895 RefreshLine( m_current
);
899 m_current
->SetHilight( TRUE
);
900 RefreshLine( m_current
);
902 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
903 GetEventHandler()->ProcessEvent( event
);
907 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
909 wxGenericTreeItem
*gitem
= item
.m_pItem
;
911 int item_y
= gitem
->GetY();
915 ViewStart( &start_x
, &start_y
);
920 GetClientSize( &client_w
, &client_h
);
922 if (item_y
< start_y
+3)
926 m_anchor
->GetSize( x
, y
);
928 int x_pos
= GetScrollPos( wxHORIZONTAL
);
929 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
933 if (item_y
> start_y
+client_h
-16)
937 m_anchor
->GetSize( x
, y
);
939 int x_pos
= GetScrollPos( wxHORIZONTAL
);
940 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
945 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
947 wxFAIL_MSG("not implemented");
950 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
951 wxClassInfo
* WXUNUSED(textCtrlClass
) )
953 wxFAIL_MSG("not implemented");
955 return (wxTextCtrl
*)NULL
;
958 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
960 wxFAIL_MSG("not implemented");
962 return (wxTextCtrl
*)NULL
;
965 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
967 wxFAIL_MSG("not implemented");
970 // FIXME: tree sorting functions are not reentrant and not MT-safe!
971 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
973 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
974 wxGenericTreeItem
**item2
)
976 wxCHECK_MSG( s_treeBeingSorted
, 0, "bug in wxTreeCtrl::SortChildren()" );
978 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
981 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
982 const wxTreeItemId
& item2
)
984 return strcmp(GetItemText(item1
), GetItemText(item2
));
987 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
989 wxCHECK_RET( itemId
.IsOk(), "invalid tree item" );
991 wxGenericTreeItem
*item
= itemId
.m_pItem
;
993 wxCHECK_RET( !s_treeBeingSorted
,
994 "wxTreeCtrl::SortChildren is not reentrant" );
996 wxArrayTreeItems
& children
= item
->GetChildren();
997 if ( children
.Count() > 1 )
999 s_treeBeingSorted
= this;
1000 children
.Sort(tree_ctrl_compare_func
);
1001 s_treeBeingSorted
= NULL
;
1005 //else: don't make the tree dirty as nothing changed
1008 wxImageList
*wxTreeCtrl::GetImageList() const
1010 return m_imageListNormal
;
1013 wxImageList
*wxTreeCtrl::GetStateImageList() const
1015 return m_imageListState
;
1018 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1020 m_imageListNormal
= imageList
;
1023 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1025 m_imageListState
= imageList
;
1028 // -----------------------------------------------------------------------------
1030 // -----------------------------------------------------------------------------
1032 void wxTreeCtrl::AdjustMyScrollbars()
1038 m_anchor
->GetSize( x
, y
);
1039 y
+= 2*m_lineHeight
;
1040 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1041 int y_pos
= GetScrollPos( wxVERTICAL
);
1042 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1046 SetScrollbars( 0, 0, 0, 0 );
1050 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1052 /* render bold items in bold */
1058 fontOld
= dc
.GetFont();
1061 /* @@ is there any better way to make a bold variant of old font? */
1062 fontNew
= wxFont( fontOld
.GetPointSize(),
1063 fontOld
.GetFamily(),
1066 fontOld
.GetUnderlined());
1067 dc
.SetFont(fontNew
);
1071 wxFAIL_MSG("wxDC::GetFont() failed!");
1077 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1081 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1083 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1086 else if (item
->GetImage() != -1)
1088 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1092 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()-2, image_w
+text_w
+4, text_h
+4 );
1094 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1096 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1097 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1098 item
->GetX(), item
->GetY()-1,
1099 wxIMAGELIST_DRAW_TRANSPARENT
);
1100 dc
.DestroyClippingRegion();
1102 else if (item
->GetImage() != -1)
1104 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, text_h
);
1105 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1106 item
->GetX(), item
->GetY()-1,
1107 wxIMAGELIST_DRAW_TRANSPARENT
);
1108 dc
.DestroyClippingRegion();
1111 dc
.SetBackgroundMode(wxTRANSPARENT
);
1112 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY() );
1114 /* restore normal font for bold items */
1117 dc
.SetFont( fontOld
);
1121 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1123 int horizX
= level
*m_indent
;
1125 item
->SetX( horizX
+33 );
1126 item
->SetY( y
-m_lineHeight
/3 );
1127 item
->SetHeight( m_lineHeight
);
1129 item
->SetCross( horizX
+15, y
);
1133 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1134 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1136 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1138 int startX
= horizX
;
1139 int endX
= horizX
+ 10;
1141 if (!item
->HasChildren()) endX
+= 20;
1143 dc
.DrawLine( startX
, y
, endX
, y
);
1145 if (item
->HasPlus())
1147 dc
.DrawLine( horizX
+20, y
, horizX
+30, y
);
1148 dc
.SetPen( *wxGREY_PEN
);
1149 dc
.SetBrush( *wxWHITE_BRUSH
);
1150 dc
.DrawRectangle( horizX
+10, y
-4, 11, 9 );
1151 dc
.SetPen( *wxBLACK_PEN
);
1152 dc
.DrawLine( horizX
+13, y
, horizX
+18, y
);
1154 if (!item
->IsExpanded())
1156 dc
.DrawLine( horizX
+15, y
-2, horizX
+15, y
+3 );
1160 if (item
->HasHilight())
1162 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1164 dc
.SetBrush( *m_hilightBrush
);
1167 dc
.SetPen( *wxBLACK_PEN
);
1169 dc
.SetPen( *wxTRANSPARENT_PEN
);
1171 PaintItem(item
, dc
);
1173 dc
.SetPen( *wxBLACK_PEN
);
1174 dc
.SetTextForeground( *wxBLACK
);
1175 dc
.SetBrush( *wxWHITE_BRUSH
);
1179 dc
.SetBrush( *wxWHITE_BRUSH
);
1180 dc
.SetPen( *wxTRANSPARENT_PEN
);
1182 PaintItem(item
, dc
);
1184 dc
.SetPen( *wxBLACK_PEN
);
1188 if (item
->IsExpanded())
1192 wxArrayTreeItems
& children
= item
->GetChildren();
1193 size_t count
= children
.Count();
1194 for ( size_t n
= 0; n
< count
; n
++ )
1198 PaintLevel( children
[n
], dc
, level
+1, y
);
1201 /* it may happen that the item is expanded but has no items (when you
1202 * delete all its children for example) - don't draw the vertical line
1204 if (count
> 0) dc
.DrawLine( horizX
+15, oldY
+5, horizX
+15, semiOldY
);
1208 // -----------------------------------------------------------------------------
1209 // wxWindows callbacks
1210 // -----------------------------------------------------------------------------
1212 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1220 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1222 dc
.SetPen( m_dottedPen
);
1223 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1225 int y
= m_lineHeight
/ 2 + 2;
1226 PaintLevel( m_anchor
, dc
, 0, y
);
1229 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1233 if (m_current
) RefreshLine( m_current
);
1236 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1240 if (m_current
) RefreshLine( m_current
);
1243 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1245 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1246 te
.m_code
= event
.KeyCode();
1247 te
.SetEventObject( this );
1248 GetEventHandler()->ProcessEvent( te
);
1256 switch (event
.KeyCode())
1260 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1268 if (IsExpanded(m_current
))
1270 Collapse(m_current
);
1282 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1283 event
.m_item
= m_current
;
1285 event
.SetEventObject( this );
1286 GetEventHandler()->ProcessEvent( event
);
1290 // up goes to the previous sibling or to the last of its children if
1294 wxTreeItemId prev
= GetPrevSibling( m_current
);
1297 prev
= GetParent( m_current
);
1299 wxTreeItemId current
= m_current
;
1300 if (current
== GetFirstChild( prev
, cockie
))
1302 // otherwise we return to where we came from
1304 EnsureVisible( prev
);
1310 while ( IsExpanded(prev
) && HasChildren(prev
) )
1312 wxTreeItemId child
= GetLastChild(prev
);
1320 EnsureVisible( prev
);
1325 // left arrow goes to the parent
1328 wxTreeItemId prev
= GetParent( m_current
);
1331 EnsureVisible( prev
);
1338 // this works the same as the down arrow except that we also expand the
1339 // item if it wasn't expanded yet
1345 if (IsExpanded(m_current
) && HasChildren(m_current
))
1348 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1349 SelectItem( child
);
1350 EnsureVisible( child
);
1354 wxTreeItemId next
= GetNextSibling( m_current
);
1357 wxTreeItemId current
= m_current
;
1358 while (current
&& !next
)
1360 current
= GetParent( current
);
1361 if (current
) next
= GetNextSibling( current
);
1367 EnsureVisible( next
);
1373 // <End> selects the last visible tree item
1376 wxTreeItemId last
= GetRootItem();
1378 while ( last
.IsOk() && IsExpanded(last
) )
1380 wxTreeItemId lastChild
= GetLastChild(last
);
1382 // it may happen if the item was expanded but then all of
1383 // its children have been deleted - so IsExpanded() returned
1384 // TRUE, but GetLastChild() returned invalid item
1393 EnsureVisible( last
);
1399 // <Home> selects the root item
1402 wxTreeItemId prev
= GetRootItem();
1405 EnsureVisible( prev
);
1416 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1418 bool onButton
= FALSE
;
1419 return m_anchor
->HitTest( point
, onButton
);
1422 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1424 if (!event
.LeftIsDown()) m_dragCount
= 0;
1426 if ( !(event
.LeftDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1428 if ( !m_anchor
) return;
1430 wxClientDC
dc(this);
1432 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1433 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1435 bool onButton
= FALSE
;
1436 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1438 if (item
== NULL
) return; /* we hit the blank area */
1440 if (event
.Dragging())
1442 if (m_dragCount
== 2) /* small drag latency (3?) */
1446 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1447 nevent
.m_item
= m_current
;
1448 nevent
.SetEventObject(this);
1449 GetEventHandler()->ProcessEvent(nevent
);
1458 if (!IsSelected(item
)) SelectItem(item
); /* we dont support multiple selections, BTW */
1460 if (event
.LeftDClick())
1462 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1463 event
.m_item
= item
;
1465 event
.SetEventObject( this );
1466 GetEventHandler()->ProcessEvent( event
);
1475 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1477 /* after all changes have been done to the tree control,
1478 * we actually redraw the tree when everything is over */
1480 if (!m_dirty
) return;
1484 CalculatePositions();
1486 AdjustMyScrollbars();
1489 // -----------------------------------------------------------------------------
1491 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1493 int horizX
= level
*m_indent
;
1495 item
->SetX( horizX
+33 );
1496 item
->SetY( y
-m_lineHeight
/3-2 );
1497 item
->SetHeight( m_lineHeight
);
1499 if ( !item
->IsExpanded() )
1501 /* we dont need to calculate collapsed branches */
1505 wxArrayTreeItems
& children
= item
->GetChildren();
1506 size_t count
= children
.Count();
1507 for ( size_t n
= 0; n
< count
; n
++ )
1510 CalculateLevel( children
[n
], dc
, level
+1, y
); /* recurse */
1514 void wxTreeCtrl::CalculatePositions()
1516 if ( !m_anchor
) return;
1518 wxClientDC
dc(this);
1521 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1523 dc
.SetPen( m_dottedPen
);
1524 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1526 int y
= m_lineHeight
/ 2 + 2;
1527 CalculateLevel( m_anchor
, dc
, 0, y
); /* start recursion */
1530 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1532 wxClientDC
dc(this);
1537 GetClientSize( &cw
, &ch
);
1540 rect
.x
= dc
.LogicalToDeviceX( 0 );
1542 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1545 Refresh( TRUE
, &rect
);
1547 AdjustMyScrollbars();
1550 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1552 wxClientDC
dc(this);
1556 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1557 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1559 rect
.height
= dc
.GetCharHeight() + 6;
1561 Refresh( TRUE
, &rect
);