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 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1017 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1019 //wxCHECK_RET( ( (!unselect_others) && is_single),
1020 // _T("this is a single selection tree") );
1022 // to keep going anyhow !!!
1025 unselect_others
=TRUE
;
1026 extended_select
=FALSE
;
1029 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1031 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1032 event
.m_item
= item
;
1033 event
.m_itemOld
= m_current
;
1034 event
.SetEventObject( this );
1035 // TODO : Here we don't send any selection mode yet !
1037 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
1041 if (unselect_others
)
1043 if (is_single
) Unselect(); // to speed up thing
1048 if (extended_select
)
1050 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1051 // don't change the mark (m_current)
1052 SelectItemRange(m_current
, item
);
1056 bool select
=TRUE
; // the default
1058 // Check if we need to toggle hilight (ctrl mode)
1059 if (!unselect_others
)
1060 select
=!item
->HasHilight();
1062 m_current
= m_key_current
= item
;
1063 m_current
->SetHilight(select
);
1064 RefreshLine( m_current
);
1067 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1068 GetEventHandler()->ProcessEvent( event
);
1071 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
, wxArrayTreeItemIds
&array
) const
1073 if (item
->HasHilight()) array
.Add(wxTreeItemId(item
));
1075 if (item
->HasChildren())
1077 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1078 size_t count
= children
.Count();
1079 for ( size_t n
= 0; n
< count
; ++n
)
1080 FillArray(children
[n
],array
);
1084 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1087 FillArray(GetRootItem().m_pItem
, array
);
1089 return array
.Count();
1092 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1094 if (!item
.IsOk()) return;
1096 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1098 // first expand all parent branches
1099 wxGenericTreeItem
*parent
= gitem
->GetParent();
1100 while ( parent
&& !parent
->IsExpanded() )
1104 parent
= parent
->GetParent();
1107 if (parent
) CalculatePositions();
1112 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1114 if (!item
.IsOk()) return;
1116 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1118 // now scroll to the item
1119 int item_y
= gitem
->GetY();
1123 ViewStart( &start_x
, &start_y
);
1124 start_y
*= PIXELS_PER_UNIT
;
1128 GetClientSize( &client_w
, &client_h
);
1130 if (item_y
< start_y
+3)
1135 m_anchor
->GetSize( x
, y
, this );
1136 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1137 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1138 // Item should appear at top
1139 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1141 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1146 m_anchor
->GetSize( x
, y
, this );
1147 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1148 item_y
+= PIXELS_PER_UNIT
+2;
1149 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1150 // Item should appear at bottom
1151 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
);
1155 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
1156 wxClassInfo
* WXUNUSED(textCtrlClass
) )
1158 wxFAIL_MSG(_T("not implemented"));
1160 return (wxTextCtrl
*)NULL
;
1163 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1165 wxFAIL_MSG(_T("not implemented"));
1167 return (wxTextCtrl
*)NULL
;
1170 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
1172 wxFAIL_MSG(_T("not implemented"));
1175 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1176 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1178 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1179 wxGenericTreeItem
**item2
)
1181 wxCHECK_MSG( s_treeBeingSorted
, 0, _T("bug in wxTreeCtrl::SortChildren()") );
1183 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1186 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1187 const wxTreeItemId
& item2
)
1189 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1192 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1194 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1196 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1198 wxCHECK_RET( !s_treeBeingSorted
,
1199 _T("wxTreeCtrl::SortChildren is not reentrant") );
1201 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1202 if ( children
.Count() > 1 )
1204 s_treeBeingSorted
= this;
1205 children
.Sort(tree_ctrl_compare_func
);
1206 s_treeBeingSorted
= NULL
;
1210 //else: don't make the tree dirty as nothing changed
1213 wxImageList
*wxTreeCtrl::GetImageList() const
1215 return m_imageListNormal
;
1218 wxImageList
*wxTreeCtrl::GetStateImageList() const
1220 return m_imageListState
;
1223 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1225 m_imageListNormal
= imageList
;
1227 // Calculate a m_lineHeight value from the image sizes.
1228 // May be toggle off. Then wxTreeCtrl will spread when
1229 // necessary (which might look ugly).
1232 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1236 n
= m_imageListNormal
->GetImageCount();
1237 for(int i
= 0; i
< n
; i
++)
1239 m_imageListNormal
->GetSize(i
, width
, height
);
1240 if(height
> m_lineHeight
) m_lineHeight
= height
;
1243 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1244 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1249 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1251 m_imageListState
= imageList
;
1254 // -----------------------------------------------------------------------------
1256 // -----------------------------------------------------------------------------
1258 void wxTreeCtrl::AdjustMyScrollbars()
1264 m_anchor
->GetSize( x
, y
, this );
1265 //y += GetLineHeight(m_anchor);
1266 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1267 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1268 int y_pos
= GetScrollPos( wxVERTICAL
);
1269 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1273 SetScrollbars( 0, 0, 0, 0 );
1277 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1279 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HIGHT
)
1280 return item
->GetHeight();
1282 return m_lineHeight
;
1285 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1287 // render bold items in bold
1293 fontOld
= dc
.GetFont();
1296 // VZ: is there any better way to make a bold variant of old font?
1297 fontNew
= wxFont( fontOld
.GetPointSize(),
1298 fontOld
.GetFamily(),
1301 fontOld
.GetUnderlined());
1302 dc
.SetFont(fontNew
);
1306 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1312 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1316 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1318 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1321 else if (item
->GetImage() != -1)
1323 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1327 int total_h
= GetLineHeight(item
);
1329 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1331 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1333 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1334 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1336 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1337 wxIMAGELIST_DRAW_TRANSPARENT
);
1338 dc
.DestroyClippingRegion();
1340 else if (item
->GetImage() != -1)
1342 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1343 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1345 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1346 wxIMAGELIST_DRAW_TRANSPARENT
);
1347 dc
.DestroyClippingRegion();
1350 dc
.SetBackgroundMode(wxTRANSPARENT
);
1351 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1352 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1354 // restore normal font for bold items
1357 dc
.SetFont( fontOld
);
1361 // Now y stands for the top of the item, whereas it used to stand for middle !
1362 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1364 int horizX
= level
*m_indent
;
1366 item
->SetX( horizX
+m_indent
+m_spacing
);
1370 y
+=GetLineHeight(item
)/2;
1372 item
->SetCross( horizX
+m_indent
, y
);
1374 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1375 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1377 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
)+4 )) // 10000 = very much
1379 int startX
= horizX
;
1380 int endX
= horizX
+ (m_indent
-5);
1382 // if (!item->HasChildren()) endX += (m_indent+5);
1383 if (!item
->HasChildren()) endX
+= 20;
1385 dc
.DrawLine( startX
, y
, endX
, y
);
1387 if (item
->HasPlus())
1389 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1390 dc
.SetPen( *wxGREY_PEN
);
1391 dc
.SetBrush( *wxWHITE_BRUSH
);
1392 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1393 dc
.SetPen( *wxBLACK_PEN
);
1394 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1396 if (!item
->IsExpanded())
1398 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1402 if (item
->HasHilight())
1404 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1406 dc
.SetBrush( *m_hilightBrush
);
1409 dc
.SetPen( *wxBLACK_PEN
);
1411 dc
.SetPen( *wxTRANSPARENT_PEN
);
1413 PaintItem(item
, dc
);
1415 dc
.SetPen( *wxBLACK_PEN
);
1416 dc
.SetTextForeground( *wxBLACK
);
1417 dc
.SetBrush( *wxWHITE_BRUSH
);
1421 dc
.SetBrush( *wxWHITE_BRUSH
);
1422 dc
.SetPen( *wxTRANSPARENT_PEN
);
1424 PaintItem(item
, dc
);
1426 dc
.SetPen( *wxBLACK_PEN
);
1430 y
= oldY
+GetLineHeight(item
);
1432 if (item
->IsExpanded())
1434 oldY
+=GetLineHeight(item
)/2;
1435 int semiOldY
=y
; // (=y) for stupid compilator
1437 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1438 size_t n
, count
= children
.Count();
1439 for ( n
= 0; n
< count
; ++n
)
1442 PaintLevel( children
[n
], dc
, level
+1, y
);
1445 // it may happen that the item is expanded but has no items (when you
1446 // delete all its children for example) - don't draw the vertical line
1450 semiOldY
+=GetLineHeight(children
[--n
])/2;
1451 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1456 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1460 wxGenericTreeItem
*i
=item
.m_pItem
;
1464 dc
.SetLogicalFunction(wxINVERT
);
1467 ViewStart(&x
,&h
); // we only need x
1468 GetClientSize(&w
,&h
); // we only need w
1470 h
=GetLineHeight(i
)+1;
1471 // 2 white column at border
1472 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1475 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1479 wxGenericTreeItem
*i
=item
.m_pItem
;
1483 dc
.SetLogicalFunction(wxINVERT
);
1488 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1491 dc
.DrawLine( 0, y
, w
, y
);
1494 // -----------------------------------------------------------------------------
1495 // wxWindows callbacks
1496 // -----------------------------------------------------------------------------
1498 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1506 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1508 dc
.SetPen( m_dottedPen
);
1509 //if(GetImageList() == NULL)
1510 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1513 PaintLevel( m_anchor
, dc
, 0, y
);
1516 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1520 if (m_current
) RefreshLine( m_current
);
1523 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1527 if (m_current
) RefreshLine( m_current
);
1530 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1532 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1533 te
.m_code
= event
.KeyCode();
1534 te
.SetEventObject( this );
1535 GetEventHandler()->ProcessEvent( te
);
1537 if ( (m_current
== 0) || (m_key_current
== 0) )
1543 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1544 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1545 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1547 switch (event
.KeyCode())
1551 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1559 if (IsExpanded(m_current
))
1561 Collapse(m_current
);
1573 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1574 event
.m_item
= m_current
;
1576 event
.SetEventObject( this );
1577 GetEventHandler()->ProcessEvent( event
);
1581 // up goes to the previous sibling or to the last of its children if
1585 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1588 prev
= GetParent( m_key_current
);
1592 wxTreeItemId current
= m_key_current
;
1593 if (current
== GetFirstChild( prev
, cockie
))
1595 // otherwise we return to where we came from
1596 SelectItem( prev
, unselect_others
, extended_select
);
1597 m_key_current
=prev
.m_pItem
;
1598 EnsureVisible( prev
);
1605 while ( IsExpanded(prev
) && HasChildren(prev
) )
1607 wxTreeItemId child
= GetLastChild(prev
);
1614 SelectItem( prev
, unselect_others
, extended_select
);
1615 m_key_current
=prev
.m_pItem
;
1616 EnsureVisible( prev
);
1621 // left arrow goes to the parent
1624 wxTreeItemId prev
= GetParent( m_current
);
1627 EnsureVisible( prev
);
1628 SelectItem( prev
, unselect_others
, extended_select
);
1634 // this works the same as the down arrow except that we also expand the
1635 // item if it wasn't expanded yet
1641 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1644 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1645 SelectItem( child
, unselect_others
, extended_select
);
1646 m_key_current
=child
.m_pItem
;
1647 EnsureVisible( child
);
1651 wxTreeItemId next
= GetNextSibling( m_key_current
);
1654 wxTreeItemId current
= m_key_current
;
1655 while (current
&& !next
)
1657 current
= GetParent( current
);
1658 if (current
) next
= GetNextSibling( current
);
1663 SelectItem( next
, unselect_others
, extended_select
);
1664 m_key_current
=next
.m_pItem
;
1665 EnsureVisible( next
);
1671 // <End> selects the last visible tree item
1674 wxTreeItemId last
= GetRootItem();
1676 while ( last
.IsOk() && IsExpanded(last
) )
1678 wxTreeItemId lastChild
= GetLastChild(last
);
1680 // it may happen if the item was expanded but then all of
1681 // its children have been deleted - so IsExpanded() returned
1682 // TRUE, but GetLastChild() returned invalid item
1691 EnsureVisible( last
);
1692 SelectItem( last
, unselect_others
, extended_select
);
1697 // <Home> selects the root item
1700 wxTreeItemId prev
= GetRootItem();
1703 EnsureVisible( prev
);
1704 SelectItem( prev
, unselect_others
, extended_select
);
1714 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1716 wxClientDC
dc(this);
1718 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1719 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1724 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1725 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1726 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1727 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1729 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1732 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1734 if (!event
.LeftIsDown()) m_dragCount
= 0;
1736 if ( !(event
.LeftUp() || event
.LeftDClick() || event
.Dragging()) ) return;
1738 if ( !m_anchor
) return;
1740 wxClientDC
dc(this);
1742 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1743 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1746 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1747 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1749 if (item
== NULL
) return; /* we hit the blank area */
1751 if (event
.Dragging())
1753 if (m_dragCount
== 2) /* small drag latency (3?) */
1757 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1758 nevent
.m_item
= m_current
;
1759 nevent
.SetEventObject(this);
1760 GetEventHandler()->ProcessEvent(nevent
);
1769 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1770 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1771 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1776 if (is_multiple
) return;
1779 SelectItem(item
, unselect_others
, extended_select
);
1781 if (event
.LeftDClick())
1783 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1784 event
.m_item
= item
;
1786 event
.SetEventObject( this );
1787 GetEventHandler()->ProcessEvent( event
);
1791 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1793 /* after all changes have been done to the tree control,
1794 * we actually redraw the tree when everything is over */
1801 CalculatePositions();
1803 AdjustMyScrollbars();
1806 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
1810 // TODO : check for boldness. Here with suppose that font normal and bold
1811 // have the same height !
1812 // TODO : bug here, text_w is sometime not the correct answer !!!
1813 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1818 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1820 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1823 else if (item
->GetImage() != -1)
1825 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1829 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
1831 if (total_h
<40) total_h
+=4; // at least 4 pixels
1832 else total_h
+=total_h
/10; // otherwise 10% extra spacing
1834 item
->SetHeight(total_h
);
1835 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
1837 item
->SetWidth(image_w
+text_w
+2);
1840 // -----------------------------------------------------------------------------
1841 // for developper : y is now the top of the level
1842 // not the middle of it !
1843 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1845 int horizX
= level
*m_indent
;
1847 CalculateSize( item
, dc
);
1850 item
->SetX( horizX
+m_indent
+m_spacing
);
1852 y
+=GetLineHeight(item
);
1854 if ( !item
->IsExpanded() )
1856 // we dont need to calculate collapsed branches
1860 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1861 size_t n
, count
= children
.Count();
1862 for (n
= 0; n
< count
; ++n
)
1863 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
1866 void wxTreeCtrl::CalculatePositions()
1868 if ( !m_anchor
) return;
1870 wxClientDC
dc(this);
1873 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1875 dc
.SetPen( m_dottedPen
);
1876 //if(GetImageList() == NULL)
1877 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1879 int y
= 2; //GetLineHeight(m_anchor) / 2 + 2;
1880 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
1883 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1885 wxClientDC
dc(this);
1890 GetClientSize( &cw
, &ch
);
1893 rect
.x
= dc
.LogicalToDeviceX( 0 );
1895 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1898 Refresh( TRUE
, &rect
);
1900 AdjustMyScrollbars();
1903 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1905 wxClientDC
dc(this);
1909 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1910 rect
.y
= dc
.LogicalToDeviceY( item
->GetY());
1912 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
1914 Refresh( TRUE
, &rect
);