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/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 // =============================================================================
160 #define PIXELS_PER_UNIT 10
161 // -----------------------------------------------------------------------------
163 // -----------------------------------------------------------------------------
165 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
167 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
168 : wxNotifyEvent( commandType
, id
)
171 m_itemOld
= (wxGenericTreeItem
*)NULL
;
174 // -----------------------------------------------------------------------------
176 // -----------------------------------------------------------------------------
178 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
179 const wxString
& text
,
181 int image
, int selImage
,
182 wxTreeItemData
*data
)
186 m_selImage
= selImage
;
189 m_xCross
= m_yCross
= 0;
193 m_isCollapsed
= TRUE
;
194 m_hasHilight
= FALSE
;
200 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
201 // TODO : Add the width of the image
202 // PB : We don't know which image is shown (image, selImage)
203 // We don't even know imageList from the treectrl this item belongs to !!!
204 // At this point m_width doesn't mean much, this can be remove !
207 wxGenericTreeItem::~wxGenericTreeItem()
211 wxASSERT_MSG( m_children
.IsEmpty(),
212 _T("please call DeleteChildren() before deleting the item") );
215 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
217 size_t count
= m_children
.Count();
218 for ( size_t n
= 0; n
< count
; n
++ )
220 wxGenericTreeItem
*child
= m_children
[n
];
223 tree
->SendDeleteEvent(child
);
226 child
->DeleteChildren(tree
);
233 void wxGenericTreeItem::SetText( const wxString
&text
)
238 void wxGenericTreeItem::Reset()
245 m_height
= m_width
= 0;
252 m_isCollapsed
= TRUE
;
254 m_parent
= (wxGenericTreeItem
*)NULL
;
257 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
259 size_t count
= m_children
.Count();
263 size_t total
= count
;
264 for ( size_t n
= 0; n
< count
; ++n
)
266 total
+= m_children
[n
]->GetChildrenCount();
272 void wxGenericTreeItem::SetCross( int x
, int y
)
278 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
280 int bottomY
=m_y
+theTree
->GetLineHeight(this);
281 if ( y
< bottomY
) y
= bottomY
;
282 int width
= m_x
+ m_width
;
283 if ( x
< width
) x
= width
;
287 size_t count
= m_children
.Count();
288 for ( size_t n
= 0; n
< count
; ++n
)
290 m_children
[n
]->GetSize( x
, y
, theTree
);
295 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
296 const wxTreeCtrl
*theTree
,
299 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
301 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2) flags
|=wxTREE_HITTEST_ONITEMUPPERPART
;
302 else flags
|=wxTREE_HITTEST_ONITEMLOWERPART
;
305 // Because that is the size of the plus sign, RR
306 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
307 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
308 (IsExpanded() || HasPlus()))
310 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
314 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
319 // assuming every image (normal and selected ) has the same size !
320 if ((m_image
!=-1) && theTree
->m_imageListNormal
)
321 theTree
->m_imageListNormal
->GetSize(m_image
, image_w
, image_h
);
323 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
324 flags
|=wxTREE_HITTEST_ONITEMICON
;
326 flags
|=wxTREE_HITTEST_ONITEMLABEL
;
331 if (point
.x
< m_x
) flags
|=wxTREE_HITTEST_ONITEMIDENT
;
332 if (point
.x
> m_x
+m_width
) flags
|=wxTREE_HITTEST_ONITEMRIGHT
;
340 size_t count
= m_children
.Count();
341 for ( size_t n
= 0; n
< count
; n
++ )
343 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
350 flags
|=wxTREE_HITTEST_NOWHERE
;
354 // -----------------------------------------------------------------------------
355 // wxTreeCtrl implementation
356 // -----------------------------------------------------------------------------
358 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
360 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
361 EVT_PAINT (wxTreeCtrl::OnPaint
)
362 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
363 EVT_CHAR (wxTreeCtrl::OnChar
)
364 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
365 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
366 EVT_IDLE (wxTreeCtrl::OnIdle
)
369 // -----------------------------------------------------------------------------
370 // construction/destruction
371 // -----------------------------------------------------------------------------
373 void wxTreeCtrl::Init()
377 m_anchor
= (wxGenericTreeItem
*) NULL
;
387 m_hilightBrush
= new wxBrush
389 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
394 m_imageListState
= (wxImageList
*) NULL
;
399 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
400 const wxPoint
& pos
, const wxSize
& size
,
402 const wxValidator
&validator
,
403 const wxString
& name
)
407 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
410 SetValidator( validator
);
413 SetBackgroundColour( *wxWHITE
);
414 m_dottedPen
= wxPen( "GREY", 0, wxDOT
);
419 wxTreeCtrl::~wxTreeCtrl()
421 wxDELETE( m_hilightBrush
);
426 // -----------------------------------------------------------------------------
428 // -----------------------------------------------------------------------------
430 size_t wxTreeCtrl::GetCount() const
432 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
435 void wxTreeCtrl::SetIndent(unsigned int indent
)
442 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
449 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
451 wxCHECK_MSG( item
.IsOk(), 0u, _T("invalid tree item") );
453 return item
.m_pItem
->GetChildrenCount(recursively
);
456 // -----------------------------------------------------------------------------
457 // functions to work with tree items
458 // -----------------------------------------------------------------------------
460 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
462 wxCHECK_MSG( item
.IsOk(), _T(""), _T("invalid tree item") );
464 return item
.m_pItem
->GetText();
467 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
469 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
471 return item
.m_pItem
->GetImage();
474 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
476 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
478 return item
.m_pItem
->GetSelectedImage();
481 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
483 wxCHECK_MSG( item
.IsOk(), NULL
, _T("invalid tree item") );
485 return item
.m_pItem
->GetData();
488 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
490 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
493 wxGenericTreeItem
*pItem
= item
.m_pItem
;
494 pItem
->SetText(text
);
495 CalculateSize(pItem
, dc
);
499 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
501 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
504 wxGenericTreeItem
*pItem
= item
.m_pItem
;
505 pItem
->SetImage(image
);
506 CalculateSize(pItem
, dc
);
510 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
512 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
515 wxGenericTreeItem
*pItem
= item
.m_pItem
;
516 pItem
->SetSelectedImage(image
);
517 CalculateSize(pItem
, dc
);
521 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
523 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
525 item
.m_pItem
->SetData(data
);
528 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
530 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
532 wxGenericTreeItem
*pItem
= item
.m_pItem
;
533 pItem
->SetHasPlus(has
);
537 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
539 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
541 // avoid redrawing the tree if no real change
542 wxGenericTreeItem
*pItem
= item
.m_pItem
;
543 if ( pItem
->IsBold() != bold
)
545 pItem
->SetBold(bold
);
550 // -----------------------------------------------------------------------------
551 // item status inquiries
552 // -----------------------------------------------------------------------------
554 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
556 wxFAIL_MSG(_T("not implemented"));
561 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
563 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
565 return !item
.m_pItem
->GetChildren().IsEmpty();
568 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
570 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
572 return item
.m_pItem
->IsExpanded();
575 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
577 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
579 return item
.m_pItem
->HasHilight();
582 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
584 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
586 return item
.m_pItem
->IsBold();
589 // -----------------------------------------------------------------------------
591 // -----------------------------------------------------------------------------
593 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
595 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
597 return item
.m_pItem
->GetParent();
600 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
602 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
605 return GetNextChild(item
, cookie
);
608 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
610 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
612 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
613 if ( (size_t)cookie
< children
.Count() )
615 return children
.Item(cookie
++);
619 // there are no more of them
620 return wxTreeItemId();
624 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
626 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
628 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
629 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
632 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
634 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
636 wxGenericTreeItem
*i
= item
.m_pItem
;
637 wxGenericTreeItem
*parent
= i
->GetParent();
638 if ( parent
== NULL
)
640 // root item doesn't have any siblings
641 return wxTreeItemId();
644 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
645 int index
= siblings
.Index(i
);
646 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
648 size_t n
= (size_t)(index
+ 1);
649 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
652 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
654 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
656 wxGenericTreeItem
*i
= item
.m_pItem
;
657 wxGenericTreeItem
*parent
= i
->GetParent();
658 if ( parent
== NULL
)
660 // root item doesn't have any siblings
661 return wxTreeItemId();
664 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
665 int index
= siblings
.Index(i
);
666 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
668 return index
== 0 ? wxTreeItemId()
669 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
672 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
674 wxFAIL_MSG(_T("not implemented"));
676 return wxTreeItemId();
679 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
681 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
683 wxFAIL_MSG(_T("not implemented"));
685 return wxTreeItemId();
688 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
690 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
692 wxFAIL_MSG(_T("not implemented"));
694 return wxTreeItemId();
697 // -----------------------------------------------------------------------------
699 // -----------------------------------------------------------------------------
701 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
703 const wxString
& text
,
704 int image
, int selImage
,
705 wxTreeItemData
*data
)
707 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
710 // should we give a warning here?
711 return AddRoot(text
, image
, selImage
, data
);
715 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
722 data
->m_pItem
= item
;
725 parent
->Insert( item
, previous
);
732 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
733 int image
, int selImage
,
734 wxTreeItemData
*data
)
736 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), _T("tree can have only one root") );
739 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
740 image
, selImage
, data
);
743 data
->m_pItem
= m_anchor
;
747 AdjustMyScrollbars();
752 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
753 const wxString
& text
,
754 int image
, int selImage
,
755 wxTreeItemData
*data
)
757 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
760 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
761 const wxTreeItemId
& idPrevious
,
762 const wxString
& text
,
763 int image
, int selImage
,
764 wxTreeItemData
*data
)
766 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
769 // should we give a warning here?
770 return AddRoot(text
, image
, selImage
, data
);
773 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
774 wxASSERT_MSG( index
!= wxNOT_FOUND
,
775 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
776 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
779 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
780 const wxString
& text
,
781 int image
, int selImage
,
782 wxTreeItemData
*data
)
784 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
787 // should we give a warning here?
788 return AddRoot(text
, image
, selImage
, data
);
791 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
792 image
, selImage
, data
);
795 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
797 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
799 event
.SetEventObject( this );
800 ProcessEvent( event
);
803 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
805 wxGenericTreeItem
*item
= itemId
.m_pItem
;
806 item
->DeleteChildren(this);
811 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
813 wxGenericTreeItem
*item
= itemId
.m_pItem
;
814 wxGenericTreeItem
*parent
= item
->GetParent();
818 parent
->GetChildren().Remove(item
);
821 item
->DeleteChildren(this);
822 SendDeleteEvent(item
);
828 void wxTreeCtrl::DeleteAllItems()
832 m_anchor
->DeleteChildren(this);
841 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
843 wxGenericTreeItem
*item
= itemId
.m_pItem
;
845 if ( !item
->HasPlus() )
848 if ( item
->IsExpanded() )
851 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
853 event
.SetEventObject( this );
854 if ( ProcessEvent( event
) && event
.m_code
)
856 // cancelled by program
861 CalculatePositions();
863 RefreshSubtree(item
);
865 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
866 ProcessEvent( event
);
869 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
871 wxGenericTreeItem
*item
= itemId
.m_pItem
;
873 if ( !item
->IsExpanded() )
876 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
878 event
.SetEventObject( this );
879 if ( ProcessEvent( event
) && event
.m_code
)
881 // cancelled by program
887 wxArrayGenericTreeItems
& children
= item
->GetChildren();
888 size_t count
= children
.Count();
889 for ( size_t n
= 0; n
< count
; n
++ )
891 Collapse(children
[n
]);
894 CalculatePositions();
896 RefreshSubtree(item
);
898 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
899 ProcessEvent( event
);
902 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
905 DeleteChildren(item
);
908 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
910 wxGenericTreeItem
*item
= itemId
.m_pItem
;
912 if ( item
->IsExpanded() )
918 void wxTreeCtrl::Unselect()
922 m_current
->SetHilight( FALSE
);
923 RefreshLine( m_current
);
927 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
929 item
->SetHilight(FALSE
);
932 if (item
->HasChildren())
934 wxArrayGenericTreeItems
& children
= item
->GetChildren();
935 size_t count
= children
.Count();
936 for ( size_t n
= 0; n
< count
; ++n
)
937 UnselectAllChildren(children
[n
]);
941 void wxTreeCtrl::UnselectAll()
943 UnselectAllChildren(GetRootItem().m_pItem
);
946 // Recursive function !
947 // To stop we must have crt_item<last_item
949 // Tag all next children, when no more children,
950 // Move to parent (not to tag)
951 // Keep going... if we found last_item, we stop.
952 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
954 wxGenericTreeItem
*parent
= crt_item
->GetParent();
956 if ( parent
== NULL
) // This is root item
957 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
959 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
960 int index
= children
.Index(crt_item
);
961 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
963 size_t count
= children
.Count();
964 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
965 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
967 return TagNextChildren(parent
, last_item
, select
);
970 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
972 crt_item
->SetHilight(select
);
973 RefreshLine(crt_item
);
975 if (crt_item
==last_item
) return TRUE
;
977 if (crt_item
->HasChildren())
979 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
980 size_t count
= children
.Count();
981 for ( size_t n
= 0; n
< count
; ++n
)
982 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
988 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
990 // item2 is not necessary after item1
991 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
993 // choice first' and 'last' between item1 and item2
994 if (item1
->GetY()<item2
->GetY())
1005 bool select
=m_current
->HasHilight();
1007 if (TagAllChildrenUntilLast(first
,last
,select
)) return;
1009 TagNextChildren(first
,last
,select
);
1012 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1013 bool unselect_others
,
1014 bool extended_select
)
1016 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1018 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1020 //wxCHECK_RET( ( (!unselect_others) && is_single),
1021 // _T("this is a single selection tree") );
1023 // to keep going anyhow !!!
1026 unselect_others
=TRUE
;
1027 extended_select
=FALSE
;
1030 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1032 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1033 event
.m_item
= item
;
1034 event
.m_itemOld
= m_current
;
1035 event
.SetEventObject( this );
1036 // TODO : Here we don't send any selection mode yet !
1038 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
1042 if (unselect_others
)
1044 if (is_single
) Unselect(); // to speed up thing
1049 if (extended_select
)
1051 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1052 // don't change the mark (m_current)
1053 SelectItemRange(m_current
, item
);
1057 bool select
=TRUE
; // the default
1059 // Check if we need to toggle hilight (ctrl mode)
1060 if (!unselect_others
)
1061 select
=!item
->HasHilight();
1063 m_current
= m_key_current
= item
;
1064 m_current
->SetHilight(select
);
1065 RefreshLine( m_current
);
1068 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1069 GetEventHandler()->ProcessEvent( event
);
1072 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
, wxArrayTreeItemIds
&array
) const
1074 if (item
->HasHilight()) array
.Add(wxTreeItemId(item
));
1076 if (item
->HasChildren())
1078 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1079 size_t count
= children
.Count();
1080 for ( size_t n
= 0; n
< count
; ++n
)
1081 FillArray(children
[n
],array
);
1085 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1088 FillArray(GetRootItem().m_pItem
, array
);
1090 return array
.Count();
1093 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1095 if (!item
.IsOk()) return;
1097 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1099 // first expand all parent branches
1100 wxGenericTreeItem
*parent
= gitem
->GetParent();
1101 while ( parent
&& !parent
->IsExpanded() )
1105 parent
= parent
->GetParent();
1108 if (parent
) CalculatePositions();
1113 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1115 if (!item
.IsOk()) return;
1117 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1119 // now scroll to the item
1120 int item_y
= gitem
->GetY();
1124 ViewStart( &start_x
, &start_y
);
1125 start_y
*= PIXELS_PER_UNIT
;
1129 GetClientSize( &client_w
, &client_h
);
1131 if (item_y
< start_y
+3)
1136 m_anchor
->GetSize( x
, y
, this );
1137 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1138 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1139 // Item should appear at top
1140 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1142 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1147 m_anchor
->GetSize( x
, y
, this );
1148 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1149 item_y
+= PIXELS_PER_UNIT
+2;
1150 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1151 // Item should appear at bottom
1152 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
);
1156 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
1157 wxClassInfo
* WXUNUSED(textCtrlClass
) )
1159 wxFAIL_MSG(_T("not implemented"));
1161 return (wxTextCtrl
*)NULL
;
1164 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1166 wxFAIL_MSG(_T("not implemented"));
1168 return (wxTextCtrl
*)NULL
;
1171 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
1173 wxFAIL_MSG(_T("not implemented"));
1176 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1177 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1179 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1180 wxGenericTreeItem
**item2
)
1182 wxCHECK_MSG( s_treeBeingSorted
, 0, _T("bug in wxTreeCtrl::SortChildren()") );
1184 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1187 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1188 const wxTreeItemId
& item2
)
1190 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1193 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1195 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1197 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1199 wxCHECK_RET( !s_treeBeingSorted
,
1200 _T("wxTreeCtrl::SortChildren is not reentrant") );
1202 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1203 if ( children
.Count() > 1 )
1205 s_treeBeingSorted
= this;
1206 children
.Sort(tree_ctrl_compare_func
);
1207 s_treeBeingSorted
= NULL
;
1211 //else: don't make the tree dirty as nothing changed
1214 wxImageList
*wxTreeCtrl::GetImageList() const
1216 return m_imageListNormal
;
1219 wxImageList
*wxTreeCtrl::GetStateImageList() const
1221 return m_imageListState
;
1224 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1226 m_imageListNormal
= imageList
;
1228 // Calculate a m_lineHeight value from the image sizes.
1229 // May be toggle off. Then wxTreeCtrl will spread when
1230 // necessary (which might look ugly).
1233 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1237 n
= m_imageListNormal
->GetImageCount();
1238 for(int i
= 0; i
< n
; i
++)
1240 m_imageListNormal
->GetSize(i
, width
, height
);
1241 if(height
> m_lineHeight
) m_lineHeight
= height
;
1244 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1245 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1250 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1252 m_imageListState
= imageList
;
1255 // -----------------------------------------------------------------------------
1257 // -----------------------------------------------------------------------------
1259 void wxTreeCtrl::AdjustMyScrollbars()
1265 m_anchor
->GetSize( x
, y
, this );
1266 //y += GetLineHeight(m_anchor);
1267 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1268 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1269 int y_pos
= GetScrollPos( wxVERTICAL
);
1270 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1274 SetScrollbars( 0, 0, 0, 0 );
1278 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1280 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HIGHT
)
1281 return item
->GetHeight();
1283 return m_lineHeight
;
1286 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1288 // render bold items in bold
1294 fontOld
= dc
.GetFont();
1297 // VZ: is there any better way to make a bold variant of old font?
1298 fontNew
= wxFont( fontOld
.GetPointSize(),
1299 fontOld
.GetFamily(),
1302 fontOld
.GetUnderlined());
1303 dc
.SetFont(fontNew
);
1307 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1313 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1317 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1319 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1322 else if (item
->GetImage() != -1)
1324 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1328 int total_h
= GetLineHeight(item
);
1330 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1332 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1334 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1335 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1337 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1338 wxIMAGELIST_DRAW_TRANSPARENT
);
1339 dc
.DestroyClippingRegion();
1341 else if (item
->GetImage() != -1)
1343 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1344 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1346 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1347 wxIMAGELIST_DRAW_TRANSPARENT
);
1348 dc
.DestroyClippingRegion();
1351 dc
.SetBackgroundMode(wxTRANSPARENT
);
1352 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1353 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1355 // restore normal font for bold items
1358 dc
.SetFont( fontOld
);
1362 // Now y stands for the top of the item, whereas it used to stand for middle !
1363 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1365 int horizX
= level
*m_indent
;
1367 item
->SetX( horizX
+m_indent
+m_spacing
);
1371 y
+=GetLineHeight(item
)/2;
1373 item
->SetCross( horizX
+m_indent
, y
);
1375 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1376 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1378 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
)+4 )) // 10000 = very much
1380 int startX
= horizX
;
1381 int endX
= horizX
+ (m_indent
-5);
1383 // if (!item->HasChildren()) endX += (m_indent+5);
1384 if (!item
->HasChildren()) endX
+= 20;
1386 dc
.DrawLine( startX
, y
, endX
, y
);
1388 if (item
->HasPlus())
1390 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1391 dc
.SetPen( *wxGREY_PEN
);
1392 dc
.SetBrush( *wxWHITE_BRUSH
);
1393 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1395 dc
.SetPen( *wxBLACK_PEN
);
1396 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1397 if (!item
->IsExpanded())
1398 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1400 dc
.SetPen( m_dottedPen
);
1403 if (item
->HasHilight())
1405 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1407 dc
.SetBrush( *m_hilightBrush
);
1410 dc
.SetPen( *wxBLACK_PEN
);
1412 dc
.SetPen( *wxTRANSPARENT_PEN
);
1414 PaintItem(item
, dc
);
1416 dc
.SetPen( m_dottedPen
);
1417 dc
.SetTextForeground( *wxBLACK
);
1418 dc
.SetBrush( *wxWHITE_BRUSH
);
1422 dc
.SetBrush( *wxWHITE_BRUSH
);
1423 dc
.SetPen( *wxTRANSPARENT_PEN
);
1425 PaintItem(item
, dc
);
1427 dc
.SetPen( m_dottedPen
);
1431 y
= oldY
+GetLineHeight(item
);
1433 if (item
->IsExpanded())
1435 oldY
+=GetLineHeight(item
)/2;
1436 int semiOldY
=y
; // (=y) for stupid compilator
1438 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1439 size_t n
, count
= children
.Count();
1440 for ( n
= 0; n
< count
; ++n
)
1443 PaintLevel( children
[n
], dc
, level
+1, y
);
1446 // it may happen that the item is expanded but has no items (when you
1447 // delete all its children for example) - don't draw the vertical line
1451 semiOldY
+=GetLineHeight(children
[--n
])/2;
1452 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1457 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1461 wxGenericTreeItem
*i
=item
.m_pItem
;
1465 dc
.SetLogicalFunction(wxINVERT
);
1468 ViewStart(&x
,&h
); // we only need x
1469 GetClientSize(&w
,&h
); // we only need w
1471 h
=GetLineHeight(i
)+1;
1472 // 2 white column at border
1473 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1476 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1480 wxGenericTreeItem
*i
=item
.m_pItem
;
1484 dc
.SetLogicalFunction(wxINVERT
);
1489 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1492 dc
.DrawLine( 0, y
, w
, y
);
1495 // -----------------------------------------------------------------------------
1496 // wxWindows callbacks
1497 // -----------------------------------------------------------------------------
1499 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1507 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1509 dc
.SetPen( m_dottedPen
);
1510 //if(GetImageList() == NULL)
1511 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1514 PaintLevel( m_anchor
, dc
, 0, y
);
1517 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1521 if (m_current
) RefreshLine( m_current
);
1524 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1528 if (m_current
) RefreshLine( m_current
);
1531 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1533 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1534 te
.m_code
= event
.KeyCode();
1535 te
.SetEventObject( this );
1536 GetEventHandler()->ProcessEvent( te
);
1538 if ( (m_current
== 0) || (m_key_current
== 0) )
1544 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1545 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1546 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1548 switch (event
.KeyCode())
1552 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1560 if (IsExpanded(m_current
))
1562 Collapse(m_current
);
1574 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1575 event
.m_item
= m_current
;
1577 event
.SetEventObject( this );
1578 GetEventHandler()->ProcessEvent( event
);
1582 // up goes to the previous sibling or to the last of its children if
1586 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1589 prev
= GetParent( m_key_current
);
1593 wxTreeItemId current
= m_key_current
;
1594 if (current
== GetFirstChild( prev
, cockie
))
1596 // otherwise we return to where we came from
1597 SelectItem( prev
, unselect_others
, extended_select
);
1598 m_key_current
=prev
.m_pItem
;
1599 EnsureVisible( prev
);
1606 while ( IsExpanded(prev
) && HasChildren(prev
) )
1608 wxTreeItemId child
= GetLastChild(prev
);
1615 SelectItem( prev
, unselect_others
, extended_select
);
1616 m_key_current
=prev
.m_pItem
;
1617 EnsureVisible( prev
);
1622 // left arrow goes to the parent
1625 wxTreeItemId prev
= GetParent( m_current
);
1628 EnsureVisible( prev
);
1629 SelectItem( prev
, unselect_others
, extended_select
);
1635 // this works the same as the down arrow except that we also expand the
1636 // item if it wasn't expanded yet
1642 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1645 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1646 SelectItem( child
, unselect_others
, extended_select
);
1647 m_key_current
=child
.m_pItem
;
1648 EnsureVisible( child
);
1652 wxTreeItemId next
= GetNextSibling( m_key_current
);
1655 wxTreeItemId current
= m_key_current
;
1656 while (current
&& !next
)
1658 current
= GetParent( current
);
1659 if (current
) next
= GetNextSibling( current
);
1664 SelectItem( next
, unselect_others
, extended_select
);
1665 m_key_current
=next
.m_pItem
;
1666 EnsureVisible( next
);
1672 // <End> selects the last visible tree item
1675 wxTreeItemId last
= GetRootItem();
1677 while ( last
.IsOk() && IsExpanded(last
) )
1679 wxTreeItemId lastChild
= GetLastChild(last
);
1681 // it may happen if the item was expanded but then all of
1682 // its children have been deleted - so IsExpanded() returned
1683 // TRUE, but GetLastChild() returned invalid item
1692 EnsureVisible( last
);
1693 SelectItem( last
, unselect_others
, extended_select
);
1698 // <Home> selects the root item
1701 wxTreeItemId prev
= GetRootItem();
1704 EnsureVisible( prev
);
1705 SelectItem( prev
, unselect_others
, extended_select
);
1715 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1717 wxClientDC
dc(this);
1719 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1720 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1725 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1726 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1727 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1728 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1730 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1733 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1735 if (!event
.LeftIsDown()) m_dragCount
= 0;
1737 if ( !(event
.LeftUp() || event
.LeftDClick() || event
.Dragging()) ) return;
1739 if ( !m_anchor
) return;
1741 wxClientDC
dc(this);
1743 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1744 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1747 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1748 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1750 if (item
== NULL
) return; /* we hit the blank area */
1752 if (event
.Dragging())
1754 if (m_dragCount
== 2) /* small drag latency (3?) */
1758 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1759 nevent
.m_item
= m_current
;
1760 nevent
.SetEventObject(this);
1761 GetEventHandler()->ProcessEvent(nevent
);
1770 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1771 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1772 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1777 if (is_multiple
) return;
1780 SelectItem(item
, unselect_others
, extended_select
);
1782 if (event
.LeftDClick())
1784 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1785 event
.m_item
= item
;
1787 event
.SetEventObject( this );
1788 GetEventHandler()->ProcessEvent( event
);
1792 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1794 /* after all changes have been done to the tree control,
1795 * we actually redraw the tree when everything is over */
1802 CalculatePositions();
1804 AdjustMyScrollbars();
1807 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
1811 // TODO : check for boldness. Here with suppose that font normal and bold
1812 // have the same height !
1813 // TODO : bug here, text_w is sometime not the correct answer !!!
1814 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1819 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1821 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1824 else if (item
->GetImage() != -1)
1826 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1830 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
1832 if (total_h
<40) total_h
+=4; // at least 4 pixels
1833 else total_h
+=total_h
/10; // otherwise 10% extra spacing
1835 item
->SetHeight(total_h
);
1836 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
1838 item
->SetWidth(image_w
+text_w
+2);
1841 // -----------------------------------------------------------------------------
1842 // for developper : y is now the top of the level
1843 // not the middle of it !
1844 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1846 int horizX
= level
*m_indent
;
1848 CalculateSize( item
, dc
);
1851 item
->SetX( horizX
+m_indent
+m_spacing
);
1853 y
+=GetLineHeight(item
);
1855 if ( !item
->IsExpanded() )
1857 // we dont need to calculate collapsed branches
1861 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1862 size_t n
, count
= children
.Count();
1863 for (n
= 0; n
< count
; ++n
)
1864 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
1867 void wxTreeCtrl::CalculatePositions()
1869 if ( !m_anchor
) return;
1871 wxClientDC
dc(this);
1874 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1876 dc
.SetPen( m_dottedPen
);
1877 //if(GetImageList() == NULL)
1878 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1880 int y
= 2; //GetLineHeight(m_anchor) / 2 + 2;
1881 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
1884 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1886 wxClientDC
dc(this);
1891 GetClientSize( &cw
, &ch
);
1894 rect
.x
= dc
.LogicalToDeviceX( 0 );
1896 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1899 Refresh( TRUE
, &rect
);
1901 AdjustMyScrollbars();
1904 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1906 wxClientDC
dc(this);
1910 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1911 rect
.y
= dc
.LogicalToDeviceY( item
->GetY());
1913 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
1915 Refresh( TRUE
, &rect
);