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/treectrl.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/settings.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
48 WX_DEFINE_OBJARRAY(wxArrayTreeItemIds
);
50 // -----------------------------------------------------------------------------
52 // -----------------------------------------------------------------------------
55 class WXDLLEXPORT wxGenericTreeItem
59 wxGenericTreeItem() { m_data
= NULL
; }
60 wxGenericTreeItem( wxGenericTreeItem
*parent
,
63 int image
, int selImage
,
64 wxTreeItemData
*data
);
69 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
71 const wxString
& GetText() const { return m_text
; }
72 int GetImage() const { return m_image
; }
73 int GetSelectedImage() const { return m_selImage
; }
74 wxTreeItemData
*GetData() const { return m_data
; }
76 void SetText( const wxString
&text
);
77 void SetImage(int image
) { m_image
= image
; }
78 void SetSelectedImage(int image
) { m_selImage
= image
; }
79 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
81 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
83 void SetBold(bool bold
) { m_isBold
= bold
; }
85 int GetX() const { return m_x
; }
86 int GetY() const { return m_y
; }
88 void SetX(int x
) { m_x
= x
; }
89 void SetY(int y
) { m_y
= y
; }
91 int GetHeight() const { return m_height
; }
92 int GetWidth() const { return m_width
; }
94 void SetHeight(int h
) { m_height
= h
; }
95 void SetWidth(int w
) { m_width
= w
; }
98 wxGenericTreeItem
*GetParent() const { return m_parent
; }
101 // deletes all children notifying the treectrl about it if !NULL pointer
103 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
104 // FIXME don't know what is it for
107 // get count of all children (and grand children if 'recursively')
108 size_t GetChildrenCount(bool recursively
= TRUE
) const;
110 void Insert(wxGenericTreeItem
*child
, size_t index
)
111 { m_children
.Insert(child
, index
); }
113 void SetCross( int x
, int y
);
114 void GetSize( int &x
, int &y
, const wxTreeCtrl
* );
116 // return the item at given position (or NULL if no item), onButton is TRUE
117 // if the point belongs to the item's button, otherwise it lies on the
119 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxTreeCtrl
*, int &flags
);
121 void Expand() { m_isCollapsed
= FALSE
; }
122 void Collapse() { m_isCollapsed
= TRUE
; }
124 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
127 bool HasChildren() const { return !m_children
.IsEmpty(); }
128 bool HasHilight() const { return m_hasHilight
; }
129 bool IsExpanded() const { return !m_isCollapsed
; }
130 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
131 bool IsBold() const { return m_isBold
; }
139 wxTreeItemData
*m_data
;
141 // use bitfields to save size
142 int m_isCollapsed
:1;
143 int m_hasHilight
:1; // same as focused
144 int m_hasPlus
:1; // used for item which doesn't have
145 // children but still has a [+] button
146 int m_isBold
:1; // render the label in bold font
149 long m_height
, m_width
;
150 int m_xCross
, m_yCross
;
152 wxArrayGenericTreeItems m_children
;
153 wxGenericTreeItem
*m_parent
;
156 // =============================================================================
158 // =============================================================================
161 // -----------------------------------------------------------------------------
162 // wxTreeRenameTimer (internal)
163 // -----------------------------------------------------------------------------
165 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl
*owner
)
170 void wxTreeRenameTimer::Notify()
172 m_owner
->OnRenameTimer();
175 //-----------------------------------------------------------------------------
176 // wxTreeTextCtrl (internal)
177 //-----------------------------------------------------------------------------
179 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
181 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
182 EVT_CHAR (wxTreeTextCtrl::OnChar
)
183 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
186 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
, const wxWindowID id
,
187 bool *accept
, wxString
*res
, wxTreeCtrl
*owner
,
188 const wxString
&value
, const wxPoint
&pos
, const wxSize
&size
,
189 int style
, const wxValidator
& validator
, const wxString
&name
) :
190 wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
197 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
199 if (event
.m_keyCode
== WXK_RETURN
)
202 (*m_res
) = GetValue();
203 m_owner
->OnRenameAccept();
204 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
207 if (event
.m_keyCode
== WXK_ESCAPE
)
211 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
217 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
221 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
224 #define PIXELS_PER_UNIT 10
225 // -----------------------------------------------------------------------------
227 // -----------------------------------------------------------------------------
229 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
231 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
232 : wxNotifyEvent( commandType
, id
)
235 m_itemOld
= (wxGenericTreeItem
*)NULL
;
238 // -----------------------------------------------------------------------------
240 // -----------------------------------------------------------------------------
242 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
243 const wxString
& text
,
245 int image
, int selImage
,
246 wxTreeItemData
*data
)
250 m_selImage
= selImage
;
253 m_xCross
= m_yCross
= 0;
257 m_isCollapsed
= TRUE
;
258 m_hasHilight
= FALSE
;
264 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
265 // TODO : Add the width of the image
266 // PB : We don't know which image is shown (image, selImage)
267 // We don't even know imageList from the treectrl this item belongs to !!!
268 // At this point m_width doesn't mean much, this can be remove !
271 wxGenericTreeItem::~wxGenericTreeItem()
275 wxASSERT_MSG( m_children
.IsEmpty(),
276 _T("please call DeleteChildren() before deleting the item") );
279 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
281 size_t count
= m_children
.Count();
282 for ( size_t n
= 0; n
< count
; n
++ )
284 wxGenericTreeItem
*child
= m_children
[n
];
287 tree
->SendDeleteEvent(child
);
290 child
->DeleteChildren(tree
);
297 void wxGenericTreeItem::SetText( const wxString
&text
)
302 void wxGenericTreeItem::Reset()
309 m_height
= m_width
= 0;
316 m_isCollapsed
= TRUE
;
318 m_parent
= (wxGenericTreeItem
*)NULL
;
321 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
323 size_t count
= m_children
.Count();
327 size_t total
= count
;
328 for ( size_t n
= 0; n
< count
; ++n
)
330 total
+= m_children
[n
]->GetChildrenCount();
336 void wxGenericTreeItem::SetCross( int x
, int y
)
342 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
344 int bottomY
=m_y
+theTree
->GetLineHeight(this);
345 if ( y
< bottomY
) y
= bottomY
;
346 int width
= m_x
+ m_width
;
347 if ( x
< width
) x
= width
;
351 size_t count
= m_children
.Count();
352 for ( size_t n
= 0; n
< count
; ++n
)
354 m_children
[n
]->GetSize( x
, y
, theTree
);
359 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
360 const wxTreeCtrl
*theTree
,
363 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
365 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2) flags
|=wxTREE_HITTEST_ONITEMUPPERPART
;
366 else flags
|=wxTREE_HITTEST_ONITEMLOWERPART
;
369 // Because that is the size of the plus sign, RR
370 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
371 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
372 (IsExpanded() || HasPlus()))
374 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
378 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
383 // assuming every image (normal and selected ) has the same size !
384 if ((m_image
!=-1) && theTree
->m_imageListNormal
)
385 theTree
->m_imageListNormal
->GetSize(m_image
, image_w
, image_h
);
387 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
388 flags
|=wxTREE_HITTEST_ONITEMICON
;
390 flags
|=wxTREE_HITTEST_ONITEMLABEL
;
395 if (point
.x
< m_x
) flags
|=wxTREE_HITTEST_ONITEMIDENT
;
396 if (point
.x
> m_x
+m_width
) flags
|=wxTREE_HITTEST_ONITEMRIGHT
;
404 size_t count
= m_children
.Count();
405 for ( size_t n
= 0; n
< count
; n
++ )
407 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
414 flags
|=wxTREE_HITTEST_NOWHERE
;
418 // -----------------------------------------------------------------------------
419 // wxTreeCtrl implementation
420 // -----------------------------------------------------------------------------
422 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
424 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
425 EVT_PAINT (wxTreeCtrl::OnPaint
)
426 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
427 EVT_CHAR (wxTreeCtrl::OnChar
)
428 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
429 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
430 EVT_IDLE (wxTreeCtrl::OnIdle
)
433 // -----------------------------------------------------------------------------
434 // construction/destruction
435 // -----------------------------------------------------------------------------
437 void wxTreeCtrl::Init()
441 m_anchor
= (wxGenericTreeItem
*) NULL
;
451 m_hilightBrush
= new wxBrush
453 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
458 m_imageListState
= (wxImageList
*) NULL
;
462 m_renameTimer
= new wxTreeRenameTimer( this );
465 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
466 const wxPoint
& pos
, const wxSize
& size
,
468 const wxValidator
&validator
,
469 const wxString
& name
)
473 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
476 SetValidator( validator
);
479 SetBackgroundColour( *wxWHITE
);
480 // m_dottedPen = wxPen( "grey", 0, wxDOT );
481 m_dottedPen
= wxPen( "grey", 0, 0 );
486 wxTreeCtrl::~wxTreeCtrl()
488 wxDELETE( m_hilightBrush
);
492 delete m_renameTimer
;
495 // -----------------------------------------------------------------------------
497 // -----------------------------------------------------------------------------
499 size_t wxTreeCtrl::GetCount() const
501 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
504 void wxTreeCtrl::SetIndent(unsigned int indent
)
511 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
518 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
520 wxCHECK_MSG( item
.IsOk(), 0u, _T("invalid tree item") );
522 return item
.m_pItem
->GetChildrenCount(recursively
);
525 // -----------------------------------------------------------------------------
526 // functions to work with tree items
527 // -----------------------------------------------------------------------------
529 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
531 wxCHECK_MSG( item
.IsOk(), _T(""), _T("invalid tree item") );
533 return item
.m_pItem
->GetText();
536 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
538 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
540 return item
.m_pItem
->GetImage();
543 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
545 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
547 return item
.m_pItem
->GetSelectedImage();
550 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
552 wxCHECK_MSG( item
.IsOk(), NULL
, _T("invalid tree item") );
554 return item
.m_pItem
->GetData();
557 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
559 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
562 wxGenericTreeItem
*pItem
= item
.m_pItem
;
563 pItem
->SetText(text
);
564 CalculateSize(pItem
, dc
);
568 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
570 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
573 wxGenericTreeItem
*pItem
= item
.m_pItem
;
574 pItem
->SetImage(image
);
575 CalculateSize(pItem
, dc
);
579 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
581 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
584 wxGenericTreeItem
*pItem
= item
.m_pItem
;
585 pItem
->SetSelectedImage(image
);
586 CalculateSize(pItem
, dc
);
590 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
592 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
594 item
.m_pItem
->SetData(data
);
597 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
599 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
601 wxGenericTreeItem
*pItem
= item
.m_pItem
;
602 pItem
->SetHasPlus(has
);
606 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
608 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
610 // avoid redrawing the tree if no real change
611 wxGenericTreeItem
*pItem
= item
.m_pItem
;
612 if ( pItem
->IsBold() != bold
)
614 pItem
->SetBold(bold
);
619 // -----------------------------------------------------------------------------
620 // item status inquiries
621 // -----------------------------------------------------------------------------
623 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
625 wxFAIL_MSG(_T("not implemented"));
630 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
632 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
634 return !item
.m_pItem
->GetChildren().IsEmpty();
637 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
639 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
641 return item
.m_pItem
->IsExpanded();
644 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
646 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
648 return item
.m_pItem
->HasHilight();
651 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
653 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
655 return item
.m_pItem
->IsBold();
658 // -----------------------------------------------------------------------------
660 // -----------------------------------------------------------------------------
662 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
664 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
666 return item
.m_pItem
->GetParent();
669 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
671 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
674 return GetNextChild(item
, cookie
);
677 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
679 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
681 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
682 if ( (size_t)cookie
< children
.Count() )
684 return children
.Item(cookie
++);
688 // there are no more of them
689 return wxTreeItemId();
693 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
695 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
697 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
698 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
701 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
703 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
705 wxGenericTreeItem
*i
= item
.m_pItem
;
706 wxGenericTreeItem
*parent
= i
->GetParent();
707 if ( parent
== NULL
)
709 // root item doesn't have any siblings
710 return wxTreeItemId();
713 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
714 int index
= siblings
.Index(i
);
715 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
717 size_t n
= (size_t)(index
+ 1);
718 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
721 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
723 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
725 wxGenericTreeItem
*i
= item
.m_pItem
;
726 wxGenericTreeItem
*parent
= i
->GetParent();
727 if ( parent
== NULL
)
729 // root item doesn't have any siblings
730 return wxTreeItemId();
733 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
734 int index
= siblings
.Index(i
);
735 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
737 return index
== 0 ? wxTreeItemId()
738 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
741 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
743 wxFAIL_MSG(_T("not implemented"));
745 return wxTreeItemId();
748 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
750 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
752 wxFAIL_MSG(_T("not implemented"));
754 return wxTreeItemId();
757 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
759 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
761 wxFAIL_MSG(_T("not implemented"));
763 return wxTreeItemId();
766 // -----------------------------------------------------------------------------
768 // -----------------------------------------------------------------------------
770 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
772 const wxString
& text
,
773 int image
, int selImage
,
774 wxTreeItemData
*data
)
776 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
779 // should we give a warning here?
780 return AddRoot(text
, image
, selImage
, data
);
784 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
791 data
->m_pItem
= item
;
794 parent
->Insert( item
, previous
);
801 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
802 int image
, int selImage
,
803 wxTreeItemData
*data
)
805 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), _T("tree can have only one root") );
808 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
809 image
, selImage
, data
);
812 data
->m_pItem
= m_anchor
;
816 AdjustMyScrollbars();
821 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
822 const wxString
& text
,
823 int image
, int selImage
,
824 wxTreeItemData
*data
)
826 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
829 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
830 const wxTreeItemId
& idPrevious
,
831 const wxString
& text
,
832 int image
, int selImage
,
833 wxTreeItemData
*data
)
835 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
838 // should we give a warning here?
839 return AddRoot(text
, image
, selImage
, data
);
842 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
843 wxASSERT_MSG( index
!= wxNOT_FOUND
,
844 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
845 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
848 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
849 const wxString
& text
,
850 int image
, int selImage
,
851 wxTreeItemData
*data
)
853 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
856 // should we give a warning here?
857 return AddRoot(text
, image
, selImage
, data
);
860 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
861 image
, selImage
, data
);
864 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
866 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
868 event
.SetEventObject( this );
869 ProcessEvent( event
);
872 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
874 wxGenericTreeItem
*item
= itemId
.m_pItem
;
875 item
->DeleteChildren(this);
880 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
882 wxGenericTreeItem
*item
= itemId
.m_pItem
;
883 wxGenericTreeItem
*parent
= item
->GetParent();
887 parent
->GetChildren().Remove(item
);
890 item
->DeleteChildren(this);
891 SendDeleteEvent(item
);
897 void wxTreeCtrl::DeleteAllItems()
901 m_anchor
->DeleteChildren(this);
910 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
912 wxGenericTreeItem
*item
= itemId
.m_pItem
;
914 if ( !item
->HasPlus() )
917 if ( item
->IsExpanded() )
920 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
922 event
.SetEventObject( this );
923 if ( ProcessEvent( event
) && event
.m_code
)
925 // cancelled by program
930 CalculatePositions();
932 RefreshSubtree(item
);
934 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
935 ProcessEvent( event
);
938 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
940 wxGenericTreeItem
*item
= itemId
.m_pItem
;
942 if ( !item
->IsExpanded() )
945 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
947 event
.SetEventObject( this );
948 if ( ProcessEvent( event
) && event
.m_code
)
950 // cancelled by program
956 wxArrayGenericTreeItems
& children
= item
->GetChildren();
957 size_t count
= children
.Count();
958 for ( size_t n
= 0; n
< count
; n
++ )
960 Collapse(children
[n
]);
963 CalculatePositions();
965 RefreshSubtree(item
);
967 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
968 ProcessEvent( event
);
971 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
974 DeleteChildren(item
);
977 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
979 wxGenericTreeItem
*item
= itemId
.m_pItem
;
981 if ( item
->IsExpanded() )
987 void wxTreeCtrl::Unselect()
991 m_current
->SetHilight( FALSE
);
992 RefreshLine( m_current
);
996 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
998 item
->SetHilight(FALSE
);
1001 if (item
->HasChildren())
1003 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1004 size_t count
= children
.Count();
1005 for ( size_t n
= 0; n
< count
; ++n
)
1006 UnselectAllChildren(children
[n
]);
1010 void wxTreeCtrl::UnselectAll()
1012 UnselectAllChildren(GetRootItem().m_pItem
);
1015 // Recursive function !
1016 // To stop we must have crt_item<last_item
1018 // Tag all next children, when no more children,
1019 // Move to parent (not to tag)
1020 // Keep going... if we found last_item, we stop.
1021 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1023 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1025 if ( parent
== NULL
) // This is root item
1026 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1028 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1029 int index
= children
.Index(crt_item
);
1030 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1032 size_t count
= children
.Count();
1033 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1034 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1036 return TagNextChildren(parent
, last_item
, select
);
1039 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1041 crt_item
->SetHilight(select
);
1042 RefreshLine(crt_item
);
1044 if (crt_item
==last_item
) return TRUE
;
1046 if (crt_item
->HasChildren())
1048 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1049 size_t count
= children
.Count();
1050 for ( size_t n
= 0; n
< count
; ++n
)
1051 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1057 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1059 // item2 is not necessary after item1
1060 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1062 // choice first' and 'last' between item1 and item2
1063 if (item1
->GetY()<item2
->GetY())
1074 bool select
=m_current
->HasHilight();
1076 if (TagAllChildrenUntilLast(first
,last
,select
)) return;
1078 TagNextChildren(first
,last
,select
);
1081 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1082 bool unselect_others
,
1083 bool extended_select
)
1085 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1087 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1089 //wxCHECK_RET( ( (!unselect_others) && is_single),
1090 // _T("this is a single selection tree") );
1092 // to keep going anyhow !!!
1095 unselect_others
=TRUE
;
1096 extended_select
=FALSE
;
1099 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1101 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1102 event
.m_item
= item
;
1103 event
.m_itemOld
= m_current
;
1104 event
.SetEventObject( this );
1105 // TODO : Here we don't send any selection mode yet !
1107 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1111 if (unselect_others
)
1113 if (is_single
) Unselect(); // to speed up thing
1118 if (extended_select
)
1120 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1121 // don't change the mark (m_current)
1122 SelectItemRange(m_current
, item
);
1126 bool select
=TRUE
; // the default
1128 // Check if we need to toggle hilight (ctrl mode)
1129 if (!unselect_others
)
1130 select
=!item
->HasHilight();
1132 m_current
= m_key_current
= item
;
1133 m_current
->SetHilight(select
);
1134 RefreshLine( m_current
);
1137 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1138 GetEventHandler()->ProcessEvent( event
);
1141 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
, wxArrayTreeItemIds
&array
) const
1143 if (item
->HasHilight()) array
.Add(wxTreeItemId(item
));
1145 if (item
->HasChildren())
1147 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1148 size_t count
= children
.Count();
1149 for ( size_t n
= 0; n
< count
; ++n
)
1150 FillArray(children
[n
],array
);
1154 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1157 FillArray(GetRootItem().m_pItem
, array
);
1159 return array
.Count();
1162 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1164 if (!item
.IsOk()) return;
1166 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1168 // first expand all parent branches
1169 wxGenericTreeItem
*parent
= gitem
->GetParent();
1170 while ( parent
&& !parent
->IsExpanded() )
1174 parent
= parent
->GetParent();
1177 if (parent
) CalculatePositions();
1182 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1184 if (!item
.IsOk()) return;
1186 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1188 // now scroll to the item
1189 int item_y
= gitem
->GetY();
1193 ViewStart( &start_x
, &start_y
);
1194 start_y
*= PIXELS_PER_UNIT
;
1198 GetClientSize( &client_w
, &client_h
);
1200 if (item_y
< start_y
+3)
1205 m_anchor
->GetSize( x
, y
, this );
1206 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1207 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1208 // Item should appear at top
1209 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1211 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1216 m_anchor
->GetSize( x
, y
, this );
1217 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1218 item_y
+= PIXELS_PER_UNIT
+2;
1219 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1220 // Item should appear at bottom
1221 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
1225 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1226 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1228 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1229 wxGenericTreeItem
**item2
)
1231 wxCHECK_MSG( s_treeBeingSorted
, 0, _T("bug in wxTreeCtrl::SortChildren()") );
1233 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1236 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1237 const wxTreeItemId
& item2
)
1239 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1242 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1244 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1246 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1248 wxCHECK_RET( !s_treeBeingSorted
,
1249 _T("wxTreeCtrl::SortChildren is not reentrant") );
1251 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1252 if ( children
.Count() > 1 )
1254 s_treeBeingSorted
= this;
1255 children
.Sort(tree_ctrl_compare_func
);
1256 s_treeBeingSorted
= NULL
;
1260 //else: don't make the tree dirty as nothing changed
1263 wxImageList
*wxTreeCtrl::GetImageList() const
1265 return m_imageListNormal
;
1268 wxImageList
*wxTreeCtrl::GetStateImageList() const
1270 return m_imageListState
;
1273 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1275 m_imageListNormal
= imageList
;
1277 // Calculate a m_lineHeight value from the image sizes.
1278 // May be toggle off. Then wxTreeCtrl will spread when
1279 // necessary (which might look ugly).
1282 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1286 n
= m_imageListNormal
->GetImageCount();
1287 for(int i
= 0; i
< n
; i
++)
1289 m_imageListNormal
->GetSize(i
, width
, height
);
1290 if(height
> m_lineHeight
) m_lineHeight
= height
;
1293 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1294 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1299 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1301 m_imageListState
= imageList
;
1304 // -----------------------------------------------------------------------------
1306 // -----------------------------------------------------------------------------
1308 void wxTreeCtrl::AdjustMyScrollbars()
1314 m_anchor
->GetSize( x
, y
, this );
1315 //y += GetLineHeight(m_anchor);
1316 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1317 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1318 int y_pos
= GetScrollPos( wxVERTICAL
);
1319 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1323 SetScrollbars( 0, 0, 0, 0 );
1327 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1329 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1330 return item
->GetHeight();
1332 return m_lineHeight
;
1335 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1337 // render bold items in bold
1343 fontOld
= dc
.GetFont();
1346 // VZ: is there any better way to make a bold variant of old font?
1347 fontNew
= wxFont( fontOld
.GetPointSize(),
1348 fontOld
.GetFamily(),
1351 fontOld
.GetUnderlined());
1352 dc
.SetFont(fontNew
);
1356 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1362 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1366 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1368 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1371 else if (item
->GetImage() != -1)
1373 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1377 int total_h
= GetLineHeight(item
);
1379 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1381 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1383 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1384 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1386 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1387 wxIMAGELIST_DRAW_TRANSPARENT
);
1388 dc
.DestroyClippingRegion();
1390 else if (item
->GetImage() != -1)
1392 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1393 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1395 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1396 wxIMAGELIST_DRAW_TRANSPARENT
);
1397 dc
.DestroyClippingRegion();
1400 dc
.SetBackgroundMode(wxTRANSPARENT
);
1401 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1402 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1404 // restore normal font for bold items
1407 dc
.SetFont( fontOld
);
1411 // Now y stands for the top of the item, whereas it used to stand for middle !
1412 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1414 int horizX
= level
*m_indent
;
1416 item
->SetX( horizX
+m_indent
+m_spacing
);
1420 y
+=GetLineHeight(item
)/2;
1422 item
->SetCross( horizX
+m_indent
, y
);
1424 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1425 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1427 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
)+4 )) // 10000 = very much
1429 int startX
= horizX
;
1430 int endX
= horizX
+ (m_indent
-5);
1432 // if (!item->HasChildren()) endX += (m_indent+5);
1433 if (!item
->HasChildren()) endX
+= 20;
1435 dc
.DrawLine( startX
, y
, endX
, y
);
1437 if (item
->HasPlus())
1439 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1440 dc
.SetPen( *wxGREY_PEN
);
1441 dc
.SetBrush( *wxWHITE_BRUSH
);
1442 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1444 dc
.SetPen( *wxBLACK_PEN
);
1445 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1446 if (!item
->IsExpanded())
1447 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1449 dc
.SetPen( m_dottedPen
);
1452 if (item
->HasHilight())
1454 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1456 dc
.SetBrush( *m_hilightBrush
);
1459 dc
.SetPen( *wxBLACK_PEN
);
1461 dc
.SetPen( *wxTRANSPARENT_PEN
);
1463 PaintItem(item
, dc
);
1465 dc
.SetPen( m_dottedPen
);
1466 dc
.SetTextForeground( *wxBLACK
);
1467 dc
.SetBrush( *wxWHITE_BRUSH
);
1471 dc
.SetBrush( *wxWHITE_BRUSH
);
1472 dc
.SetPen( *wxTRANSPARENT_PEN
);
1474 PaintItem(item
, dc
);
1476 dc
.SetPen( m_dottedPen
);
1480 y
= oldY
+GetLineHeight(item
);
1482 if (item
->IsExpanded())
1484 oldY
+=GetLineHeight(item
)/2;
1485 int semiOldY
=y
; // (=y) for stupid compilator
1487 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1488 size_t n
, count
= children
.Count();
1489 for ( n
= 0; n
< count
; ++n
)
1492 PaintLevel( children
[n
], dc
, level
+1, y
);
1495 // it may happen that the item is expanded but has no items (when you
1496 // delete all its children for example) - don't draw the vertical line
1500 semiOldY
+=GetLineHeight(children
[--n
])/2;
1501 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1506 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1510 wxGenericTreeItem
*i
=item
.m_pItem
;
1514 dc
.SetLogicalFunction(wxINVERT
);
1517 ViewStart(&x
,&h
); // we only need x
1518 GetClientSize(&w
,&h
); // we only need w
1520 h
=GetLineHeight(i
)+1;
1521 // 2 white column at border
1522 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1525 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1529 wxGenericTreeItem
*i
=item
.m_pItem
;
1533 dc
.SetLogicalFunction(wxINVERT
);
1538 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1541 dc
.DrawLine( 0, y
, w
, y
);
1544 // -----------------------------------------------------------------------------
1545 // wxWindows callbacks
1546 // -----------------------------------------------------------------------------
1548 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1556 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1558 dc
.SetPen( m_dottedPen
);
1559 //if(GetImageList() == NULL)
1560 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1563 PaintLevel( m_anchor
, dc
, 0, y
);
1566 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1570 if (m_current
) RefreshLine( m_current
);
1573 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1577 if (m_current
) RefreshLine( m_current
);
1580 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1582 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1583 te
.m_code
= event
.KeyCode();
1584 te
.SetEventObject( this );
1585 GetEventHandler()->ProcessEvent( te
);
1587 if ( (m_current
== 0) || (m_key_current
== 0) )
1593 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1594 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1595 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1597 switch (event
.KeyCode())
1601 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1609 if (IsExpanded(m_current
))
1611 Collapse(m_current
);
1623 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1624 event
.m_item
= m_current
;
1626 event
.SetEventObject( this );
1627 GetEventHandler()->ProcessEvent( event
);
1631 // up goes to the previous sibling or to the last of its children if
1635 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1638 prev
= GetParent( m_key_current
);
1642 wxTreeItemId current
= m_key_current
;
1643 if (current
== GetFirstChild( prev
, cockie
))
1645 // otherwise we return to where we came from
1646 SelectItem( prev
, unselect_others
, extended_select
);
1647 m_key_current
=prev
.m_pItem
;
1648 EnsureVisible( prev
);
1655 while ( IsExpanded(prev
) && HasChildren(prev
) )
1657 wxTreeItemId child
= GetLastChild(prev
);
1664 SelectItem( prev
, unselect_others
, extended_select
);
1665 m_key_current
=prev
.m_pItem
;
1666 EnsureVisible( prev
);
1671 // left arrow goes to the parent
1674 wxTreeItemId prev
= GetParent( m_current
);
1677 EnsureVisible( prev
);
1678 SelectItem( prev
, unselect_others
, extended_select
);
1684 // this works the same as the down arrow except that we also expand the
1685 // item if it wasn't expanded yet
1691 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1694 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1695 SelectItem( child
, unselect_others
, extended_select
);
1696 m_key_current
=child
.m_pItem
;
1697 EnsureVisible( child
);
1701 wxTreeItemId next
= GetNextSibling( m_key_current
);
1705 wxTreeItemId current
= m_key_current
;
1706 while (current
&& !next
)
1708 current
= GetParent( current
);
1709 if (current
) next
= GetNextSibling( current
);
1715 SelectItem( next
, unselect_others
, extended_select
);
1716 m_key_current
=next
.m_pItem
;
1717 EnsureVisible( next
);
1723 // <End> selects the last visible tree item
1726 wxTreeItemId last
= GetRootItem();
1728 while ( last
.IsOk() && IsExpanded(last
) )
1730 wxTreeItemId lastChild
= GetLastChild(last
);
1732 // it may happen if the item was expanded but then all of
1733 // its children have been deleted - so IsExpanded() returned
1734 // TRUE, but GetLastChild() returned invalid item
1743 EnsureVisible( last
);
1744 SelectItem( last
, unselect_others
, extended_select
);
1749 // <Home> selects the root item
1752 wxTreeItemId prev
= GetRootItem();
1755 EnsureVisible( prev
);
1756 SelectItem( prev
, unselect_others
, extended_select
);
1766 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1768 wxClientDC
dc(this);
1770 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1771 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1776 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1777 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1778 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1779 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1781 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1786 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1788 if (!item
.IsOk()) return;
1790 m_currentEdit
= item
.m_pItem
;
1792 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1793 te
.m_item
= m_currentEdit
;
1794 te
.SetEventObject( this );
1795 GetEventHandler()->ProcessEvent( te
);
1797 if (!te
.IsAllowed()) return;
1799 wxString s
= m_currentEdit
->GetText();
1800 int x
= m_currentEdit
->GetX();
1801 int y
= m_currentEdit
->GetY();
1802 int w
= m_currentEdit
->GetWidth();
1803 int h
= m_currentEdit
->GetHeight();
1805 wxClientDC
dc(this);
1807 x
= dc
.LogicalToDeviceX( x
);
1808 y
= dc
.LogicalToDeviceY( y
);
1810 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1811 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1815 void wxTreeCtrl::OnRenameTimer()
1820 void wxTreeCtrl::OnRenameAccept()
1822 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1823 le
.m_item
= m_currentEdit
;
1824 le
.SetEventObject( this );
1825 le
.m_label
= m_renameRes
;
1826 GetEventHandler()->ProcessEvent( le
);
1828 if (!le
.IsAllowed()) return;
1830 /* DO CHANGE LABEL */
1833 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1835 if (!event
.LeftIsDown()) m_dragCount
= 0;
1837 if ( !(event
.LeftUp() || event
.LeftDClick() || event
.Dragging()) ) return;
1839 if ( !m_anchor
) return;
1841 wxClientDC
dc(this);
1843 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1844 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1847 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1848 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1850 if (item
== NULL
) return; /* we hit the blank area */
1852 if (event
.Dragging())
1854 if (m_dragCount
== 2) /* small drag latency (3?) */
1858 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1859 nevent
.m_item
= m_current
;
1860 nevent
.SetEventObject(this);
1861 GetEventHandler()->ProcessEvent(nevent
);
1870 if (event
.LeftUp() && (item
== m_current
) &&
1871 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
1872 HasFlag(wxTR_EDIT_LABELS
) )
1874 m_renameTimer
->Start( 100, TRUE
);
1878 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1879 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1880 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1889 SelectItem(item
, unselect_others
, extended_select
);
1891 if (event
.LeftDClick())
1893 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1894 event
.m_item
= item
;
1896 event
.SetEventObject( this );
1897 GetEventHandler()->ProcessEvent( event
);
1901 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1903 /* after all changes have been done to the tree control,
1904 * we actually redraw the tree when everything is over */
1911 CalculatePositions();
1913 AdjustMyScrollbars();
1916 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
1920 // TODO : check for boldness. Here with suppose that font normal and bold
1921 // have the same height !
1922 // TODO : bug here, text_w is sometime not the correct answer !!!
1923 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1928 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1930 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1933 else if (item
->GetImage() != -1)
1935 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1939 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
1941 if (total_h
<40) total_h
+=4; // at least 4 pixels
1942 else total_h
+=total_h
/10; // otherwise 10% extra spacing
1944 item
->SetHeight(total_h
);
1945 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
1947 item
->SetWidth(image_w
+text_w
+2);
1950 // -----------------------------------------------------------------------------
1951 // for developper : y is now the top of the level
1952 // not the middle of it !
1953 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1955 int horizX
= level
*m_indent
;
1957 CalculateSize( item
, dc
);
1960 item
->SetX( horizX
+m_indent
+m_spacing
);
1962 y
+=GetLineHeight(item
);
1964 if ( !item
->IsExpanded() )
1966 // we dont need to calculate collapsed branches
1970 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1971 size_t n
, count
= children
.Count();
1972 for (n
= 0; n
< count
; ++n
)
1973 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
1976 void wxTreeCtrl::CalculatePositions()
1978 if ( !m_anchor
) return;
1980 wxClientDC
dc(this);
1983 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1985 dc
.SetPen( m_dottedPen
);
1986 //if(GetImageList() == NULL)
1987 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1989 int y
= 2; //GetLineHeight(m_anchor) / 2 + 2;
1990 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
1993 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1995 wxClientDC
dc(this);
2000 GetClientSize( &cw
, &ch
);
2003 rect
.x
= dc
.LogicalToDeviceX( 0 );
2005 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2008 Refresh( TRUE
, &rect
);
2010 AdjustMyScrollbars();
2013 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2015 wxClientDC
dc(this);
2019 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
2020 rect
.y
= dc
.LogicalToDeviceY( item
->GetY());
2022 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2024 Refresh( TRUE
, &rect
);