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_text_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_text_height
, m_text_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_text_width
, &m_text_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_text_width
, &m_text_height
);
228 void wxGenericTreeItem::Reset()
235 m_text_height
= m_text_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_text_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_text_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 ); */
301 int w
= m_text_width
;
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::UnselectAllChildren(wxGenericTreeItem
*item
)
893 item
->SetHilight(FALSE
);
896 if (item
->HasChildren())
898 wxArrayTreeItems
& children
= item
->GetChildren();
899 size_t count
= children
.Count();
900 for ( size_t n
= 0; n
< count
; ++n
)
901 UnselectAllChildren(children
[n
]);
905 void wxTreeCtrl::UnselectAll()
907 UnselectAllChildren(GetRootItem().m_pItem
);
910 // Recursive function !
911 // To stop we must have crt_item<last_item
913 // Tag all next children, when no more children,
914 // Move to parent (not to tag)
915 // Keep going, if we found last_item, we stop
916 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
918 wxGenericTreeItem
*parent
= crt_item
->GetParent();
920 if ( parent
== NULL
) // This is root item
921 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
923 wxArrayTreeItems
& children
= parent
->GetChildren();
924 int index
= children
.Index(crt_item
);
925 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
927 size_t count
= children
.Count();
928 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
929 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
931 return TagNextChildren(parent
, last_item
, select
);
934 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
936 crt_item
->SetHilight(select
);
937 RefreshLine(crt_item
);
939 if (crt_item
==last_item
) return true;
941 if (crt_item
->HasChildren())
943 wxArrayTreeItems
& children
= crt_item
->GetChildren();
944 size_t count
= children
.Count();
945 for ( size_t n
= 0; n
< count
; ++n
)
946 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
952 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
954 // item2 is not necessary after item1
955 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
957 // choice first' and 'last' between item1 and item2
958 if (item1
->GetY()<item2
->GetY())
969 bool select
=m_current
->HasHilight();
971 if (TagAllChildrenUntilLast(first
,last
,select
)) return;
973 cout
<< first
->GetText() << " " << last
->GetText() << " " << (int) select
<< endl
;
974 TagNextChildren(first
,last
,select
);
977 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
978 bool unselect_others
,
979 bool extended_select
)
981 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
983 //wxCHECK_RET( ( (!unselect_others) && is_single),
984 // _T("this is a single selection tree") );
986 // to keep going anyhow !!!
989 unselect_others
=true;
990 extended_select
=false;
993 wxGenericTreeItem
*item
= itemId
.m_pItem
;
995 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
997 event
.m_itemOld
= m_current
;
998 event
.SetEventObject( this );
999 // Here we don't send any selection mode yet ! TO SEE
1002 cout
<< m_current
->GetText() << " " << (int)m_current
->HasHilight() << endl
;
1004 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
1008 cout
<< m_current
->GetText() << " " << (int)m_current
->HasHilight() << endl
;
1011 if (unselect_others
)
1013 if (is_single
) Unselect(); // to speed up thing
1018 if (extended_select
)
1020 if (m_current
== NULL
) m_current
=GetRootItem().m_pItem
;
1021 // don't change the mark (m_current)
1022 SelectItemRange(m_current
, item
);
1026 bool select
=true; // the default
1028 // Check if we need to toggle hilight (ctrl mode)
1029 if (!unselect_others
)
1030 select
=!item
->HasHilight();
1033 m_current
->SetHilight(select
);
1034 RefreshLine( m_current
);
1037 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1038 GetEventHandler()->ProcessEvent( event
);
1041 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1043 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1045 // first expand all parent branches
1046 wxGenericTreeItem
*parent
= gitem
->GetParent();
1047 while ( parent
&& !parent
->IsExpanded() )
1051 parent
= parent
->GetParent();
1054 // now scroll to the item
1055 int item_y
= gitem
->GetY();
1059 ViewStart( &start_x
, &start_y
);
1064 GetClientSize( &client_w
, &client_h
);
1066 if (item_y
< start_y
+3)
1070 m_anchor
->GetSize( x
, y
);
1071 y
+= 2*m_lineHeight
;
1072 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1073 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
1075 else if (item_y
> start_y
+client_h
-16)
1079 m_anchor
->GetSize( x
, y
);
1080 y
+= 2*m_lineHeight
;
1081 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1082 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, (item_y
-client_h
/2)/10 );
1086 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& WXUNUSED(item
))
1088 wxFAIL_MSG(_T("not implemented"));
1091 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
1092 wxClassInfo
* WXUNUSED(textCtrlClass
) )
1094 wxFAIL_MSG(_T("not implemented"));
1096 return (wxTextCtrl
*)NULL
;
1099 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1101 wxFAIL_MSG(_T("not implemented"));
1103 return (wxTextCtrl
*)NULL
;
1106 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
1108 wxFAIL_MSG(_T("not implemented"));
1111 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1112 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1114 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1115 wxGenericTreeItem
**item2
)
1117 wxCHECK_MSG( s_treeBeingSorted
, 0, _T("bug in wxTreeCtrl::SortChildren()") );
1119 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1122 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1123 const wxTreeItemId
& item2
)
1125 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1128 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1130 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1132 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1134 wxCHECK_RET( !s_treeBeingSorted
,
1135 _T("wxTreeCtrl::SortChildren is not reentrant") );
1137 wxArrayTreeItems
& children
= item
->GetChildren();
1138 if ( children
.Count() > 1 )
1140 s_treeBeingSorted
= this;
1141 children
.Sort(tree_ctrl_compare_func
);
1142 s_treeBeingSorted
= NULL
;
1146 //else: don't make the tree dirty as nothing changed
1149 wxImageList
*wxTreeCtrl::GetImageList() const
1151 return m_imageListNormal
;
1154 wxImageList
*wxTreeCtrl::GetStateImageList() const
1156 return m_imageListState
;
1159 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1161 m_imageListNormal
= imageList
;
1162 // calculate a m_lineHeight value from the image sizes
1165 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1169 n
= m_imageListNormal
->GetImageCount();
1170 for(int i
= 0; i
< n
; i
++)
1172 m_imageListNormal
->GetSize(i
, width
, height
);
1173 height
+= height
/5; //20% extra spacing
1174 if(height
> m_lineHeight
) m_lineHeight
= height
;
1178 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1180 m_imageListState
= imageList
;
1183 // -----------------------------------------------------------------------------
1185 // -----------------------------------------------------------------------------
1187 void wxTreeCtrl::AdjustMyScrollbars()
1193 m_anchor
->GetSize( x
, y
);
1194 y
+= 2*m_lineHeight
;
1195 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1196 int y_pos
= GetScrollPos( wxVERTICAL
);
1197 SetScrollbars( 10, 10, x
/10, y
/10, x_pos
, y_pos
);
1201 SetScrollbars( 0, 0, 0, 0 );
1205 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1207 // render bold items in bold
1213 fontOld
= dc
.GetFont();
1216 // VZ: is there any better way to make a bold variant of old font?
1217 fontNew
= wxFont( fontOld
.GetPointSize(),
1218 fontOld
.GetFamily(),
1221 fontOld
.GetUnderlined());
1222 dc
.SetFont(fontNew
);
1226 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1232 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1236 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1238 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1241 else if (item
->GetImage() != -1)
1243 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1247 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
1248 if(m_lineHeight
> total_h
) total_h
= m_lineHeight
;
1250 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), image_w
+text_w
+4, total_h
);
1252 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1254 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1255 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1257 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1258 wxIMAGELIST_DRAW_TRANSPARENT
);
1259 dc
.DestroyClippingRegion();
1261 else if (item
->GetImage() != -1)
1263 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1264 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1266 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1267 wxIMAGELIST_DRAW_TRANSPARENT
);
1268 dc
.DestroyClippingRegion();
1271 dc
.SetBackgroundMode(wxTRANSPARENT
);
1272 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1273 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1275 // restore normal font for bold items
1278 dc
.SetFont( fontOld
);
1282 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1284 int horizX
= level
*m_indent
;
1286 item
->SetX( horizX
+m_indent
+m_spacing
);
1287 item
->SetY( y
-m_lineHeight
/2 );
1288 item
->SetHeight( m_lineHeight
);
1290 item
->SetCross( horizX
+m_indent
, y
);
1294 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1295 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1297 if (IsExposed( exposed_x
, exposed_y
, 10000, m_lineHeight
+4 )) // 10000 = very much
1299 int startX
= horizX
;
1300 int endX
= horizX
+ (m_indent
-5);
1302 // if (!item->HasChildren()) endX += (m_indent+5);
1303 if (!item
->HasChildren()) endX
+= 20;
1305 dc
.DrawLine( startX
, y
, endX
, y
);
1307 if (item
->HasPlus())
1309 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1310 dc
.SetPen( *wxGREY_PEN
);
1311 dc
.SetBrush( *wxWHITE_BRUSH
);
1312 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1313 dc
.SetPen( *wxBLACK_PEN
);
1314 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1316 if (!item
->IsExpanded())
1318 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1322 if (item
->HasHilight())
1324 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1326 dc
.SetBrush( *m_hilightBrush
);
1329 dc
.SetPen( *wxBLACK_PEN
);
1331 dc
.SetPen( *wxTRANSPARENT_PEN
);
1333 PaintItem(item
, dc
);
1335 dc
.SetPen( *wxBLACK_PEN
);
1336 dc
.SetTextForeground( *wxBLACK
);
1337 dc
.SetBrush( *wxWHITE_BRUSH
);
1341 dc
.SetBrush( *wxWHITE_BRUSH
);
1342 dc
.SetPen( *wxTRANSPARENT_PEN
);
1344 PaintItem(item
, dc
);
1346 dc
.SetPen( *wxBLACK_PEN
);
1350 if (item
->IsExpanded())
1354 wxArrayTreeItems
& children
= item
->GetChildren();
1355 size_t count
= children
.Count();
1356 for ( size_t n
= 0; n
< count
; n
++ )
1360 PaintLevel( children
[n
], dc
, level
+1, y
);
1363 // it may happen that the item is expanded but has no items (when you
1364 // delete all its children for example) - don't draw the vertical line
1367 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1371 // -----------------------------------------------------------------------------
1372 // wxWindows callbacks
1373 // -----------------------------------------------------------------------------
1375 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1383 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1385 dc
.SetPen( m_dottedPen
);
1386 if(GetImageList() == NULL
)
1387 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1389 int y
= m_lineHeight
/ 2 + 2;
1390 PaintLevel( m_anchor
, dc
, 0, y
);
1393 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1397 if (m_current
) RefreshLine( m_current
);
1400 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1404 if (m_current
) RefreshLine( m_current
);
1407 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1409 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1410 te
.m_code
= event
.KeyCode();
1411 te
.SetEventObject( this );
1412 GetEventHandler()->ProcessEvent( te
);
1420 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1421 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1422 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1424 switch (event
.KeyCode())
1428 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1436 if (IsExpanded(m_current
))
1438 Collapse(m_current
);
1450 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1451 event
.m_item
= m_current
;
1453 event
.SetEventObject( this );
1454 GetEventHandler()->ProcessEvent( event
);
1458 // up goes to the previous sibling or to the last of its children if
1462 wxTreeItemId prev
= GetPrevSibling( m_current
);
1465 prev
= GetParent( m_current
);
1469 wxTreeItemId current
= m_current
;
1470 if (current
== GetFirstChild( prev
, cockie
))
1472 // otherwise we return to where we came from
1473 SelectItem( prev
, unselect_others
, extended_select
);
1474 EnsureVisible( prev
);
1481 while ( IsExpanded(prev
) && HasChildren(prev
) )
1483 wxTreeItemId child
= GetLastChild(prev
);
1490 SelectItem( prev
, unselect_others
, extended_select
);
1491 EnsureVisible( prev
);
1496 // left arrow goes to the parent
1499 wxTreeItemId prev
= GetParent( m_current
);
1502 EnsureVisible( prev
);
1503 SelectItem( prev
, unselect_others
, extended_select
);
1509 // this works the same as the down arrow except that we also expand the
1510 // item if it wasn't expanded yet
1516 if (IsExpanded(m_current
) && HasChildren(m_current
))
1519 wxTreeItemId child
= GetFirstChild( m_current
, cookie
);
1520 SelectItem( child
, unselect_others
, extended_select
);
1521 EnsureVisible( child
);
1525 wxTreeItemId next
= GetNextSibling( m_current
);
1528 wxTreeItemId current
= m_current
;
1529 while (current
&& !next
)
1531 current
= GetParent( current
);
1532 if (current
) next
= GetNextSibling( current
);
1537 SelectItem( next
, unselect_others
, extended_select
);
1538 EnsureVisible( next
);
1544 // <End> selects the last visible tree item
1547 wxTreeItemId last
= GetRootItem();
1549 while ( last
.IsOk() && IsExpanded(last
) )
1551 wxTreeItemId lastChild
= GetLastChild(last
);
1553 // it may happen if the item was expanded but then all of
1554 // its children have been deleted - so IsExpanded() returned
1555 // TRUE, but GetLastChild() returned invalid item
1564 EnsureVisible( last
);
1565 SelectItem( last
, unselect_others
, extended_select
);
1570 // <Home> selects the root item
1573 wxTreeItemId prev
= GetRootItem();
1576 EnsureVisible( prev
);
1577 SelectItem( prev
, unselect_others
, extended_select
);
1587 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& WXUNUSED(flags
))
1589 wxClientDC
dc(this);
1591 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1592 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1594 bool onButton
= FALSE
;
1595 return m_anchor
->HitTest( wxPoint(x
, y
), onButton
);
1598 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1600 if (!event
.LeftIsDown()) m_dragCount
= 0;
1602 if ( !(event
.LeftDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1604 if ( !m_anchor
) return;
1606 wxClientDC
dc(this);
1608 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1609 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1611 bool onButton
= FALSE
;
1612 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), onButton
);
1614 if (item
== NULL
) return; /* we hit the blank area */
1616 if (event
.Dragging())
1618 if (m_dragCount
== 2) /* small drag latency (3?) */
1622 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1623 nevent
.m_item
= m_current
;
1624 nevent
.SetEventObject(this);
1625 GetEventHandler()->ProcessEvent(nevent
);
1634 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1635 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1636 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1638 SelectItem(item
, unselect_others
, extended_select
);
1640 if (event
.LeftDClick())
1642 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1643 event
.m_item
= item
;
1645 event
.SetEventObject( this );
1646 GetEventHandler()->ProcessEvent( event
);
1655 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1657 /* after all changes have been done to the tree control,
1658 * we actually redraw the tree when everything is over */
1665 CalculatePositions();
1667 AdjustMyScrollbars();
1670 // -----------------------------------------------------------------------------
1672 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1674 int horizX
= level
*m_indent
;
1676 item
->SetX( horizX
+m_indent
+m_spacing
);
1677 item
->SetY( y
-m_lineHeight
/2 );
1678 item
->SetHeight( m_lineHeight
);
1680 if ( !item
->IsExpanded() )
1682 // we dont need to calculate collapsed branches
1686 wxArrayTreeItems
& children
= item
->GetChildren();
1687 size_t count
= children
.Count();
1688 for ( size_t n
= 0; n
< count
; n
++ )
1691 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
1695 void wxTreeCtrl::CalculatePositions()
1697 if ( !m_anchor
) return;
1699 wxClientDC
dc(this);
1702 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1704 dc
.SetPen( m_dottedPen
);
1705 if(GetImageList() == NULL
)
1706 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1708 int y
= m_lineHeight
/ 2 + 2;
1709 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
1712 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1714 wxClientDC
dc(this);
1719 GetClientSize( &cw
, &ch
);
1722 rect
.x
= dc
.LogicalToDeviceX( 0 );
1724 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1727 Refresh( TRUE
, &rect
);
1729 AdjustMyScrollbars();
1732 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1734 wxClientDC
dc(this);
1738 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1739 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() - 2 );
1741 rect
.height
= dc
.GetCharHeight() + 6;
1743 Refresh( TRUE
, &rect
);