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 (theTree
->m_imageListNormal
)
321 theTree
->m_imageListNormal
->GetSize(m_image
, image_w
, image_h
);
322 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
323 flags
|=wxTREE_HITTEST_ONITEMICON
;
325 flags
|=wxTREE_HITTEST_ONITEMLABEL
;
330 if (point
.x
< m_x
) flags
|=wxTREE_HITTEST_ONITEMIDENT
;
331 if (point
.x
> m_x
+m_width
) flags
|=wxTREE_HITTEST_ONITEMRIGHT
;
339 size_t count
= m_children
.Count();
340 for ( size_t n
= 0; n
< count
; n
++ )
342 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
349 flags
|=wxTREE_HITTEST_NOWHERE
;
353 // -----------------------------------------------------------------------------
354 // wxTreeCtrl implementation
355 // -----------------------------------------------------------------------------
357 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
359 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
360 EVT_PAINT (wxTreeCtrl::OnPaint
)
361 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
362 EVT_CHAR (wxTreeCtrl::OnChar
)
363 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
364 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
365 EVT_IDLE (wxTreeCtrl::OnIdle
)
368 // -----------------------------------------------------------------------------
369 // construction/destruction
370 // -----------------------------------------------------------------------------
372 void wxTreeCtrl::Init()
376 m_anchor
= (wxGenericTreeItem
*) NULL
;
386 m_hilightBrush
= new wxBrush
388 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
393 m_imageListState
= (wxImageList
*) NULL
;
398 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
399 const wxPoint
& pos
, const wxSize
& size
,
401 const wxValidator
&validator
,
402 const wxString
& name
)
406 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
409 SetValidator( validator
);
412 SetBackgroundColour( *wxWHITE
);
413 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
418 wxTreeCtrl::~wxTreeCtrl()
420 wxDELETE( m_hilightBrush
);
425 // -----------------------------------------------------------------------------
427 // -----------------------------------------------------------------------------
429 size_t wxTreeCtrl::GetCount() const
431 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
434 void wxTreeCtrl::SetIndent(unsigned int indent
)
441 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
448 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
450 wxCHECK_MSG( item
.IsOk(), 0u, _T("invalid tree item") );
452 return item
.m_pItem
->GetChildrenCount(recursively
);
455 // -----------------------------------------------------------------------------
456 // functions to work with tree items
457 // -----------------------------------------------------------------------------
459 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
461 wxCHECK_MSG( item
.IsOk(), _T(""), _T("invalid tree item") );
463 return item
.m_pItem
->GetText();
466 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
468 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
470 return item
.m_pItem
->GetImage();
473 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
475 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
477 return item
.m_pItem
->GetSelectedImage();
480 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
482 wxCHECK_MSG( item
.IsOk(), NULL
, _T("invalid tree item") );
484 return item
.m_pItem
->GetData();
487 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
489 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
492 wxGenericTreeItem
*pItem
= item
.m_pItem
;
493 pItem
->SetText(text
);
494 CalculateSize(pItem
, dc
);
498 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
500 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
503 wxGenericTreeItem
*pItem
= item
.m_pItem
;
504 pItem
->SetImage(image
);
505 CalculateSize(pItem
, dc
);
509 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
511 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
514 wxGenericTreeItem
*pItem
= item
.m_pItem
;
515 pItem
->SetSelectedImage(image
);
516 CalculateSize(pItem
, dc
);
520 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
522 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
524 item
.m_pItem
->SetData(data
);
527 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
529 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
531 wxGenericTreeItem
*pItem
= item
.m_pItem
;
532 pItem
->SetHasPlus(has
);
536 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
538 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
540 // avoid redrawing the tree if no real change
541 wxGenericTreeItem
*pItem
= item
.m_pItem
;
542 if ( pItem
->IsBold() != bold
)
544 pItem
->SetBold(bold
);
549 // -----------------------------------------------------------------------------
550 // item status inquiries
551 // -----------------------------------------------------------------------------
553 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
555 wxFAIL_MSG(_T("not implemented"));
560 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
562 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
564 return !item
.m_pItem
->GetChildren().IsEmpty();
567 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
569 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
571 return item
.m_pItem
->IsExpanded();
574 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
576 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
578 return item
.m_pItem
->HasHilight();
581 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
583 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
585 return item
.m_pItem
->IsBold();
588 // -----------------------------------------------------------------------------
590 // -----------------------------------------------------------------------------
592 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
594 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
596 return item
.m_pItem
->GetParent();
599 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
601 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
604 return GetNextChild(item
, cookie
);
607 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
609 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
611 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
612 if ( (size_t)cookie
< children
.Count() )
614 return children
.Item(cookie
++);
618 // there are no more of them
619 return wxTreeItemId();
623 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
625 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
627 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
628 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
631 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
633 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
635 wxGenericTreeItem
*i
= item
.m_pItem
;
636 wxGenericTreeItem
*parent
= i
->GetParent();
637 if ( parent
== NULL
)
639 // root item doesn't have any siblings
640 return wxTreeItemId();
643 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
644 int index
= siblings
.Index(i
);
645 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
647 size_t n
= (size_t)(index
+ 1);
648 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
651 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
653 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
655 wxGenericTreeItem
*i
= item
.m_pItem
;
656 wxGenericTreeItem
*parent
= i
->GetParent();
657 if ( parent
== NULL
)
659 // root item doesn't have any siblings
660 return wxTreeItemId();
663 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
664 int index
= siblings
.Index(i
);
665 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
667 return index
== 0 ? wxTreeItemId()
668 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
671 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
673 wxFAIL_MSG(_T("not implemented"));
675 return wxTreeItemId();
678 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
680 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
682 wxFAIL_MSG(_T("not implemented"));
684 return wxTreeItemId();
687 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
689 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
691 wxFAIL_MSG(_T("not implemented"));
693 return wxTreeItemId();
696 // -----------------------------------------------------------------------------
698 // -----------------------------------------------------------------------------
700 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
702 const wxString
& text
,
703 int image
, int selImage
,
704 wxTreeItemData
*data
)
706 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
709 // should we give a warning here?
710 return AddRoot(text
, image
, selImage
, data
);
714 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
721 data
->m_pItem
= item
;
724 parent
->Insert( item
, previous
);
731 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
732 int image
, int selImage
,
733 wxTreeItemData
*data
)
735 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), _T("tree can have only one root") );
738 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
739 image
, selImage
, data
);
742 data
->m_pItem
= m_anchor
;
746 AdjustMyScrollbars();
751 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
752 const wxString
& text
,
753 int image
, int selImage
,
754 wxTreeItemData
*data
)
756 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
759 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
760 const wxTreeItemId
& idPrevious
,
761 const wxString
& text
,
762 int image
, int selImage
,
763 wxTreeItemData
*data
)
765 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
768 // should we give a warning here?
769 return AddRoot(text
, image
, selImage
, data
);
772 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
773 wxASSERT_MSG( index
!= wxNOT_FOUND
,
774 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
775 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
778 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
779 const wxString
& text
,
780 int image
, int selImage
,
781 wxTreeItemData
*data
)
783 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
786 // should we give a warning here?
787 return AddRoot(text
, image
, selImage
, data
);
790 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
791 image
, selImage
, data
);
794 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
796 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
798 event
.SetEventObject( this );
799 ProcessEvent( event
);
802 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
804 wxGenericTreeItem
*item
= itemId
.m_pItem
;
805 item
->DeleteChildren(this);
810 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
812 wxGenericTreeItem
*item
= itemId
.m_pItem
;
813 wxGenericTreeItem
*parent
= item
->GetParent();
817 parent
->GetChildren().Remove(item
);
820 item
->DeleteChildren(this);
821 SendDeleteEvent(item
);
827 void wxTreeCtrl::DeleteAllItems()
831 m_anchor
->DeleteChildren(this);
840 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
842 wxGenericTreeItem
*item
= itemId
.m_pItem
;
844 if ( !item
->HasPlus() )
847 if ( item
->IsExpanded() )
850 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
852 event
.SetEventObject( this );
853 if ( ProcessEvent( event
) && event
.m_code
)
855 // cancelled by program
860 CalculatePositions();
862 RefreshSubtree(item
);
864 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
865 ProcessEvent( event
);
868 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
870 wxGenericTreeItem
*item
= itemId
.m_pItem
;
872 if ( !item
->IsExpanded() )
875 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
877 event
.SetEventObject( this );
878 if ( ProcessEvent( event
) && event
.m_code
)
880 // cancelled by program
886 wxArrayGenericTreeItems
& children
= item
->GetChildren();
887 size_t count
= children
.Count();
888 for ( size_t n
= 0; n
< count
; n
++ )
890 Collapse(children
[n
]);
893 CalculatePositions();
895 RefreshSubtree(item
);
897 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
898 ProcessEvent( event
);
901 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
904 DeleteChildren(item
);
907 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
909 wxGenericTreeItem
*item
= itemId
.m_pItem
;
911 if ( item
->IsExpanded() )
917 void wxTreeCtrl::Unselect()
921 m_current
->SetHilight( FALSE
);
922 RefreshLine( m_current
);
926 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
928 item
->SetHilight(FALSE
);
931 if (item
->HasChildren())
933 wxArrayGenericTreeItems
& children
= item
->GetChildren();
934 size_t count
= children
.Count();
935 for ( size_t n
= 0; n
< count
; ++n
)
936 UnselectAllChildren(children
[n
]);
940 void wxTreeCtrl::UnselectAll()
942 UnselectAllChildren(GetRootItem().m_pItem
);
945 // Recursive function !
946 // To stop we must have crt_item<last_item
948 // Tag all next children, when no more children,
949 // Move to parent (not to tag)
950 // Keep going... if we found last_item, we stop.
951 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
953 wxGenericTreeItem
*parent
= crt_item
->GetParent();
955 if ( parent
== NULL
) // This is root item
956 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
958 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
959 int index
= children
.Index(crt_item
);
960 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
962 size_t count
= children
.Count();
963 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
964 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
966 return TagNextChildren(parent
, last_item
, select
);
969 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
971 crt_item
->SetHilight(select
);
972 RefreshLine(crt_item
);
974 if (crt_item
==last_item
) return TRUE
;
976 if (crt_item
->HasChildren())
978 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
979 size_t count
= children
.Count();
980 for ( size_t n
= 0; n
< count
; ++n
)
981 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
987 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
989 // item2 is not necessary after item1
990 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
992 // choice first' and 'last' between item1 and item2
993 if (item1
->GetY()<item2
->GetY())
1004 bool select
=m_current
->HasHilight();
1006 if (TagAllChildrenUntilLast(first
,last
,select
)) return;
1008 TagNextChildren(first
,last
,select
);
1011 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1012 bool unselect_others
,
1013 bool extended_select
)
1015 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1017 //wxCHECK_RET( ( (!unselect_others) && is_single),
1018 // _T("this is a single selection tree") );
1020 // to keep going anyhow !!!
1023 unselect_others
=TRUE
;
1024 extended_select
=FALSE
;
1027 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1029 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1030 event
.m_item
= item
;
1031 event
.m_itemOld
= m_current
;
1032 event
.SetEventObject( this );
1033 // TODO : Here we don't send any selection mode yet !
1035 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
1039 if (unselect_others
)
1041 if (is_single
) Unselect(); // to speed up thing
1046 if (extended_select
)
1048 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1049 // don't change the mark (m_current)
1050 SelectItemRange(m_current
, item
);
1054 bool select
=TRUE
; // the default
1056 // Check if we need to toggle hilight (ctrl mode)
1057 if (!unselect_others
)
1058 select
=!item
->HasHilight();
1060 m_current
= m_key_current
= item
;
1061 m_current
->SetHilight(select
);
1062 RefreshLine( m_current
);
1065 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1066 GetEventHandler()->ProcessEvent( event
);
1069 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
, wxArrayTreeItemIds
&array
) const
1071 if (item
->HasHilight()) array
.Add(wxTreeItemId(item
));
1073 if (item
->HasChildren())
1075 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1076 size_t count
= children
.Count();
1077 for ( size_t n
= 0; n
< count
; ++n
)
1078 FillArray(children
[n
],array
);
1082 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1085 FillArray(GetRootItem().m_pItem
, array
);
1087 return array
.Count();
1090 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1092 if (!item
.IsOk()) return;
1094 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1096 // first expand all parent branches
1097 wxGenericTreeItem
*parent
= gitem
->GetParent();
1098 while ( parent
&& !parent
->IsExpanded() )
1102 parent
= parent
->GetParent();
1105 if (parent
) CalculatePositions();
1110 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1112 if (!item
.IsOk()) return;
1114 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1116 // now scroll to the item
1117 int item_y
= gitem
->GetY();
1121 ViewStart( &start_x
, &start_y
);
1122 start_y
*= PIXELS_PER_UNIT
;
1126 GetClientSize( &client_w
, &client_h
);
1128 if (item_y
< start_y
+3)
1133 m_anchor
->GetSize( x
, y
, this );
1134 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1135 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1136 // Item should appear at top
1137 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1139 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1144 m_anchor
->GetSize( x
, y
, this );
1145 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1146 item_y
+= PIXELS_PER_UNIT
+2;
1147 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1148 // Item should appear at bottom
1149 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
);
1153 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
1154 wxClassInfo
* WXUNUSED(textCtrlClass
) )
1156 wxFAIL_MSG(_T("not implemented"));
1158 return (wxTextCtrl
*)NULL
;
1161 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1163 wxFAIL_MSG(_T("not implemented"));
1165 return (wxTextCtrl
*)NULL
;
1168 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
1170 wxFAIL_MSG(_T("not implemented"));
1173 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1174 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1176 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1177 wxGenericTreeItem
**item2
)
1179 wxCHECK_MSG( s_treeBeingSorted
, 0, _T("bug in wxTreeCtrl::SortChildren()") );
1181 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1184 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1185 const wxTreeItemId
& item2
)
1187 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1190 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1192 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1194 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1196 wxCHECK_RET( !s_treeBeingSorted
,
1197 _T("wxTreeCtrl::SortChildren is not reentrant") );
1199 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1200 if ( children
.Count() > 1 )
1202 s_treeBeingSorted
= this;
1203 children
.Sort(tree_ctrl_compare_func
);
1204 s_treeBeingSorted
= NULL
;
1208 //else: don't make the tree dirty as nothing changed
1211 wxImageList
*wxTreeCtrl::GetImageList() const
1213 return m_imageListNormal
;
1216 wxImageList
*wxTreeCtrl::GetStateImageList() const
1218 return m_imageListState
;
1221 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1223 m_imageListNormal
= imageList
;
1225 // Calculate a m_lineHeight value from the image sizes.
1226 // May be toggle off. Then wxTreeCtrl will spread when
1227 // necessary (which might look ugly).
1230 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1234 n
= m_imageListNormal
->GetImageCount();
1235 for(int i
= 0; i
< n
; i
++)
1237 m_imageListNormal
->GetSize(i
, width
, height
);
1238 if(height
> m_lineHeight
) m_lineHeight
= height
;
1241 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1242 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1247 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1249 m_imageListState
= imageList
;
1252 // -----------------------------------------------------------------------------
1254 // -----------------------------------------------------------------------------
1256 void wxTreeCtrl::AdjustMyScrollbars()
1262 m_anchor
->GetSize( x
, y
, this );
1263 //y += GetLineHeight(m_anchor);
1264 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1265 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1266 int y_pos
= GetScrollPos( wxVERTICAL
);
1267 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1271 SetScrollbars( 0, 0, 0, 0 );
1275 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1277 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HIGHT
)
1278 return item
->GetHeight();
1280 return m_lineHeight
;
1283 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1285 // render bold items in bold
1291 fontOld
= dc
.GetFont();
1294 // VZ: is there any better way to make a bold variant of old font?
1295 fontNew
= wxFont( fontOld
.GetPointSize(),
1296 fontOld
.GetFamily(),
1299 fontOld
.GetUnderlined());
1300 dc
.SetFont(fontNew
);
1304 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1310 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1314 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1316 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1319 else if (item
->GetImage() != -1)
1321 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1325 int total_h
= GetLineHeight(item
);
1327 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1329 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1331 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1332 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1334 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1335 wxIMAGELIST_DRAW_TRANSPARENT
);
1336 dc
.DestroyClippingRegion();
1338 else if (item
->GetImage() != -1)
1340 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1341 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1343 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1344 wxIMAGELIST_DRAW_TRANSPARENT
);
1345 dc
.DestroyClippingRegion();
1348 dc
.SetBackgroundMode(wxTRANSPARENT
);
1349 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1350 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1352 // restore normal font for bold items
1355 dc
.SetFont( fontOld
);
1359 // Now y stands for the top of the item, whereas it used to stand for middle !
1360 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1362 int horizX
= level
*m_indent
;
1364 item
->SetX( horizX
+m_indent
+m_spacing
);
1368 y
+=GetLineHeight(item
)/2;
1370 item
->SetCross( horizX
+m_indent
, y
);
1372 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1373 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1375 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
)+4 )) // 10000 = very much
1377 int startX
= horizX
;
1378 int endX
= horizX
+ (m_indent
-5);
1380 // if (!item->HasChildren()) endX += (m_indent+5);
1381 if (!item
->HasChildren()) endX
+= 20;
1383 dc
.DrawLine( startX
, y
, endX
, y
);
1385 if (item
->HasPlus())
1387 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1388 dc
.SetPen( *wxGREY_PEN
);
1389 dc
.SetBrush( *wxWHITE_BRUSH
);
1390 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1391 dc
.SetPen( *wxBLACK_PEN
);
1392 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1394 if (!item
->IsExpanded())
1396 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1400 if (item
->HasHilight())
1402 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1404 dc
.SetBrush( *m_hilightBrush
);
1407 dc
.SetPen( *wxBLACK_PEN
);
1409 dc
.SetPen( *wxTRANSPARENT_PEN
);
1411 PaintItem(item
, dc
);
1413 dc
.SetPen( *wxBLACK_PEN
);
1414 dc
.SetTextForeground( *wxBLACK
);
1415 dc
.SetBrush( *wxWHITE_BRUSH
);
1419 dc
.SetBrush( *wxWHITE_BRUSH
);
1420 dc
.SetPen( *wxTRANSPARENT_PEN
);
1422 PaintItem(item
, dc
);
1424 dc
.SetPen( *wxBLACK_PEN
);
1428 y
= oldY
+GetLineHeight(item
);
1430 if (item
->IsExpanded())
1432 oldY
+=GetLineHeight(item
)/2;
1433 int semiOldY
=y
; // (=y) for stupid compilator
1435 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1436 size_t n
, count
= children
.Count();
1437 for ( n
= 0; n
< count
; ++n
)
1440 PaintLevel( children
[n
], dc
, level
+1, y
);
1443 // it may happen that the item is expanded but has no items (when you
1444 // delete all its children for example) - don't draw the vertical line
1448 semiOldY
+=GetLineHeight(children
[--n
])/2;
1449 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1454 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1458 wxGenericTreeItem
*i
=item
.m_pItem
;
1462 dc
.SetLogicalFunction(wxINVERT
);
1465 ViewStart(&x
,&h
); // we only need x
1466 GetClientSize(&w
,&h
); // we only need w
1468 h
=GetLineHeight(i
)+1;
1469 // 2 white column at border
1470 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1473 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1477 wxGenericTreeItem
*i
=item
.m_pItem
;
1481 dc
.SetLogicalFunction(wxINVERT
);
1486 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1489 dc
.DrawLine( 0, y
, w
, y
);
1492 // -----------------------------------------------------------------------------
1493 // wxWindows callbacks
1494 // -----------------------------------------------------------------------------
1496 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1504 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1506 dc
.SetPen( m_dottedPen
);
1507 //if(GetImageList() == NULL)
1508 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1511 PaintLevel( m_anchor
, dc
, 0, y
);
1514 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1518 if (m_current
) RefreshLine( m_current
);
1521 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1525 if (m_current
) RefreshLine( m_current
);
1528 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1530 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1531 te
.m_code
= event
.KeyCode();
1532 te
.SetEventObject( this );
1533 GetEventHandler()->ProcessEvent( te
);
1535 if ( (m_current
== 0) || (m_key_current
== 0) )
1541 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1542 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1543 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1545 switch (event
.KeyCode())
1549 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1557 if (IsExpanded(m_current
))
1559 Collapse(m_current
);
1571 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1572 event
.m_item
= m_current
;
1574 event
.SetEventObject( this );
1575 GetEventHandler()->ProcessEvent( event
);
1579 // up goes to the previous sibling or to the last of its children if
1583 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1586 prev
= GetParent( m_key_current
);
1590 wxTreeItemId current
= m_key_current
;
1591 if (current
== GetFirstChild( prev
, cockie
))
1593 // otherwise we return to where we came from
1594 SelectItem( prev
, unselect_others
, extended_select
);
1595 m_key_current
=prev
.m_pItem
;
1596 EnsureVisible( prev
);
1603 while ( IsExpanded(prev
) && HasChildren(prev
) )
1605 wxTreeItemId child
= GetLastChild(prev
);
1612 SelectItem( prev
, unselect_others
, extended_select
);
1613 m_key_current
=prev
.m_pItem
;
1614 EnsureVisible( prev
);
1619 // left arrow goes to the parent
1622 wxTreeItemId prev
= GetParent( m_current
);
1625 EnsureVisible( prev
);
1626 SelectItem( prev
, unselect_others
, extended_select
);
1632 // this works the same as the down arrow except that we also expand the
1633 // item if it wasn't expanded yet
1639 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1642 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1643 SelectItem( child
, unselect_others
, extended_select
);
1644 m_key_current
=child
.m_pItem
;
1645 EnsureVisible( child
);
1649 wxTreeItemId next
= GetNextSibling( m_key_current
);
1652 wxTreeItemId current
= m_key_current
;
1653 while (current
&& !next
)
1655 current
= GetParent( current
);
1656 if (current
) next
= GetNextSibling( current
);
1661 SelectItem( next
, unselect_others
, extended_select
);
1662 m_key_current
=next
.m_pItem
;
1663 EnsureVisible( next
);
1669 // <End> selects the last visible tree item
1672 wxTreeItemId last
= GetRootItem();
1674 while ( last
.IsOk() && IsExpanded(last
) )
1676 wxTreeItemId lastChild
= GetLastChild(last
);
1678 // it may happen if the item was expanded but then all of
1679 // its children have been deleted - so IsExpanded() returned
1680 // TRUE, but GetLastChild() returned invalid item
1689 EnsureVisible( last
);
1690 SelectItem( last
, unselect_others
, extended_select
);
1695 // <Home> selects the root item
1698 wxTreeItemId prev
= GetRootItem();
1701 EnsureVisible( prev
);
1702 SelectItem( prev
, unselect_others
, extended_select
);
1712 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1714 wxClientDC
dc(this);
1716 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1717 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1722 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1723 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1724 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1725 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1727 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1730 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1732 if (!event
.LeftIsDown()) m_dragCount
= 0;
1734 if ( !(event
.LeftUp() || event
.LeftDClick() || event
.Dragging()) ) return;
1736 if ( !m_anchor
) return;
1738 wxClientDC
dc(this);
1740 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1741 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1744 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1745 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1747 if (item
== NULL
) return; /* we hit the blank area */
1749 if (event
.Dragging())
1751 if (m_dragCount
== 2) /* small drag latency (3?) */
1755 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1756 nevent
.m_item
= m_current
;
1757 nevent
.SetEventObject(this);
1758 GetEventHandler()->ProcessEvent(nevent
);
1767 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1768 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1769 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1774 if (is_multiple
) return;
1777 SelectItem(item
, unselect_others
, extended_select
);
1779 if (event
.LeftDClick())
1781 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1782 event
.m_item
= item
;
1784 event
.SetEventObject( this );
1785 GetEventHandler()->ProcessEvent( event
);
1789 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1791 /* after all changes have been done to the tree control,
1792 * we actually redraw the tree when everything is over */
1799 CalculatePositions();
1801 AdjustMyScrollbars();
1804 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
1808 // TODO : check for boldness. Here with suppose that font normal and bold
1809 // have the same height !
1810 // TODO : bug here, text_w is sometime not the correct answer !!!
1811 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1816 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1818 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1821 else if (item
->GetImage() != -1)
1823 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1827 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
1829 if (total_h
<40) total_h
+=4; // at least 4 pixels
1830 else total_h
+=total_h
/10; // otherwise 10% extra spacing
1832 item
->SetHeight(total_h
);
1833 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
1835 item
->SetWidth(image_w
+text_w
+2);
1838 // -----------------------------------------------------------------------------
1839 // for developper : y is now the top of the level
1840 // not the middle of it !
1841 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1843 int horizX
= level
*m_indent
;
1845 CalculateSize( item
, dc
);
1848 item
->SetX( horizX
+m_indent
+m_spacing
);
1850 y
+=GetLineHeight(item
);
1852 if ( !item
->IsExpanded() )
1854 // we dont need to calculate collapsed branches
1858 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1859 size_t n
, count
= children
.Count();
1860 for (n
= 0; n
< count
; ++n
)
1861 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
1864 void wxTreeCtrl::CalculatePositions()
1866 if ( !m_anchor
) return;
1868 wxClientDC
dc(this);
1871 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1873 dc
.SetPen( m_dottedPen
);
1874 //if(GetImageList() == NULL)
1875 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1877 int y
= 2; //GetLineHeight(m_anchor) / 2 + 2;
1878 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
1881 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1883 wxClientDC
dc(this);
1888 GetClientSize( &cw
, &ch
);
1891 rect
.x
= dc
.LogicalToDeviceX( 0 );
1893 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1896 Refresh( TRUE
, &rect
);
1898 AdjustMyScrollbars();
1901 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1903 wxClientDC
dc(this);
1907 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1908 rect
.y
= dc
.LogicalToDeviceY( item
->GetY());
1910 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
1912 Refresh( TRUE
, &rect
);