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
))
318 // assuming every image (normal and selected ) has the same size !
319 theTree
->m_imageListNormal
->GetSize(m_image
, image_w
, image_h
);
320 if (point
.x
<=m_x
+image_w
+1)
321 flags
|=wxTREE_HITTEST_ONITEMICON
;
323 flags
|=wxTREE_HITTEST_ONITEMLABEL
;
328 if (point
.x
< m_x
) flags
|=wxTREE_HITTEST_ONITEMIDENT
;
329 if (point
.x
> m_x
+m_width
) flags
|=wxTREE_HITTEST_ONITEMRIGHT
;
337 size_t count
= m_children
.Count();
338 for ( size_t n
= 0; n
< count
; n
++ )
340 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
347 flags
|=wxTREE_HITTEST_NOWHERE
;
351 // -----------------------------------------------------------------------------
352 // wxTreeCtrl implementation
353 // -----------------------------------------------------------------------------
355 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
357 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
358 EVT_PAINT (wxTreeCtrl::OnPaint
)
359 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
360 EVT_CHAR (wxTreeCtrl::OnChar
)
361 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
362 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
363 EVT_IDLE (wxTreeCtrl::OnIdle
)
366 // -----------------------------------------------------------------------------
367 // construction/destruction
368 // -----------------------------------------------------------------------------
370 void wxTreeCtrl::Init()
374 m_anchor
= (wxGenericTreeItem
*) NULL
;
384 m_hilightBrush
= new wxBrush
386 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
391 m_imageListState
= (wxImageList
*) NULL
;
396 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
397 const wxPoint
& pos
, const wxSize
& size
,
399 const wxValidator
&validator
,
400 const wxString
& name
)
404 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
407 SetValidator( validator
);
410 SetBackgroundColour( *wxWHITE
);
411 m_dottedPen
= wxPen( *wxBLACK
, 0, 0 );
416 wxTreeCtrl::~wxTreeCtrl()
418 wxDELETE( m_hilightBrush
);
423 // -----------------------------------------------------------------------------
425 // -----------------------------------------------------------------------------
427 size_t wxTreeCtrl::GetCount() const
429 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
432 void wxTreeCtrl::SetIndent(unsigned int indent
)
439 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
446 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
448 wxCHECK_MSG( item
.IsOk(), 0u, _T("invalid tree item") );
450 return item
.m_pItem
->GetChildrenCount(recursively
);
453 // -----------------------------------------------------------------------------
454 // functions to work with tree items
455 // -----------------------------------------------------------------------------
457 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
459 wxCHECK_MSG( item
.IsOk(), _T(""), _T("invalid tree item") );
461 return item
.m_pItem
->GetText();
464 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
466 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
468 return item
.m_pItem
->GetImage();
471 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
473 wxCHECK_MSG( item
.IsOk(), -1, _T("invalid tree item") );
475 return item
.m_pItem
->GetSelectedImage();
478 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
480 wxCHECK_MSG( item
.IsOk(), NULL
, _T("invalid tree item") );
482 return item
.m_pItem
->GetData();
485 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
487 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
490 wxGenericTreeItem
*pItem
= item
.m_pItem
;
491 pItem
->SetText(text
);
492 CalculateSize(pItem
, dc
);
496 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
498 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
501 wxGenericTreeItem
*pItem
= item
.m_pItem
;
502 pItem
->SetImage(image
);
503 CalculateSize(pItem
, dc
);
507 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
509 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
512 wxGenericTreeItem
*pItem
= item
.m_pItem
;
513 pItem
->SetSelectedImage(image
);
514 CalculateSize(pItem
, dc
);
518 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
520 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
522 item
.m_pItem
->SetData(data
);
525 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
527 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
529 wxGenericTreeItem
*pItem
= item
.m_pItem
;
530 pItem
->SetHasPlus(has
);
534 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
536 wxCHECK_RET( item
.IsOk(), _T("invalid tree item") );
538 // avoid redrawing the tree if no real change
539 wxGenericTreeItem
*pItem
= item
.m_pItem
;
540 if ( pItem
->IsBold() != bold
)
542 pItem
->SetBold(bold
);
547 // -----------------------------------------------------------------------------
548 // item status inquiries
549 // -----------------------------------------------------------------------------
551 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
553 wxFAIL_MSG(_T("not implemented"));
558 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
560 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
562 return !item
.m_pItem
->GetChildren().IsEmpty();
565 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
567 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
569 return item
.m_pItem
->IsExpanded();
572 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
574 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
576 return item
.m_pItem
->HasHilight();
579 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
581 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid tree item") );
583 return item
.m_pItem
->IsBold();
586 // -----------------------------------------------------------------------------
588 // -----------------------------------------------------------------------------
590 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
592 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
594 return item
.m_pItem
->GetParent();
597 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
599 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
602 return GetNextChild(item
, cookie
);
605 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
607 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
609 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
610 if ( (size_t)cookie
< children
.Count() )
612 return children
.Item(cookie
++);
616 // there are no more of them
617 return wxTreeItemId();
621 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
623 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
625 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
626 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
629 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
631 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
633 wxGenericTreeItem
*i
= item
.m_pItem
;
634 wxGenericTreeItem
*parent
= i
->GetParent();
635 if ( parent
== NULL
)
637 // root item doesn't have any siblings
638 return wxTreeItemId();
641 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
642 int index
= siblings
.Index(i
);
643 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
645 size_t n
= (size_t)(index
+ 1);
646 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
649 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
651 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
653 wxGenericTreeItem
*i
= item
.m_pItem
;
654 wxGenericTreeItem
*parent
= i
->GetParent();
655 if ( parent
== NULL
)
657 // root item doesn't have any siblings
658 return wxTreeItemId();
661 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
662 int index
= siblings
.Index(i
);
663 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
665 return index
== 0 ? wxTreeItemId()
666 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
669 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
671 wxFAIL_MSG(_T("not implemented"));
673 return wxTreeItemId();
676 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
678 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
680 wxFAIL_MSG(_T("not implemented"));
682 return wxTreeItemId();
685 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
687 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), _T("invalid tree item") );
689 wxFAIL_MSG(_T("not implemented"));
691 return wxTreeItemId();
694 // -----------------------------------------------------------------------------
696 // -----------------------------------------------------------------------------
698 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
700 const wxString
& text
,
701 int image
, int selImage
,
702 wxTreeItemData
*data
)
704 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
707 // should we give a warning here?
708 return AddRoot(text
, image
, selImage
, data
);
712 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
719 data
->m_pItem
= item
;
722 parent
->Insert( item
, previous
);
729 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
730 int image
, int selImage
,
731 wxTreeItemData
*data
)
733 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), _T("tree can have only one root") );
736 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
737 image
, selImage
, data
);
740 data
->m_pItem
= m_anchor
;
744 AdjustMyScrollbars();
749 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
750 const wxString
& text
,
751 int image
, int selImage
,
752 wxTreeItemData
*data
)
754 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
757 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
758 const wxTreeItemId
& idPrevious
,
759 const wxString
& text
,
760 int image
, int selImage
,
761 wxTreeItemData
*data
)
763 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
766 // should we give a warning here?
767 return AddRoot(text
, image
, selImage
, data
);
770 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
771 wxASSERT_MSG( index
!= wxNOT_FOUND
,
772 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
773 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
776 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
777 const wxString
& text
,
778 int image
, int selImage
,
779 wxTreeItemData
*data
)
781 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
784 // should we give a warning here?
785 return AddRoot(text
, image
, selImage
, data
);
788 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
789 image
, selImage
, data
);
792 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
794 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
796 event
.SetEventObject( this );
797 ProcessEvent( event
);
800 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
802 wxGenericTreeItem
*item
= itemId
.m_pItem
;
803 item
->DeleteChildren(this);
808 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
810 wxGenericTreeItem
*item
= itemId
.m_pItem
;
811 wxGenericTreeItem
*parent
= item
->GetParent();
815 parent
->GetChildren().Remove(item
);
818 item
->DeleteChildren(this);
819 SendDeleteEvent(item
);
825 void wxTreeCtrl::DeleteAllItems()
829 m_anchor
->DeleteChildren(this);
838 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
840 wxGenericTreeItem
*item
= itemId
.m_pItem
;
842 if ( !item
->HasPlus() )
845 if ( item
->IsExpanded() )
848 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
850 event
.SetEventObject( this );
851 if ( ProcessEvent( event
) && event
.m_code
)
853 // cancelled by program
858 CalculatePositions();
860 RefreshSubtree(item
);
862 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
863 ProcessEvent( event
);
866 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
868 wxGenericTreeItem
*item
= itemId
.m_pItem
;
870 if ( !item
->IsExpanded() )
873 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
875 event
.SetEventObject( this );
876 if ( ProcessEvent( event
) && event
.m_code
)
878 // cancelled by program
884 wxArrayGenericTreeItems
& children
= item
->GetChildren();
885 size_t count
= children
.Count();
886 for ( size_t n
= 0; n
< count
; n
++ )
888 Collapse(children
[n
]);
891 CalculatePositions();
893 RefreshSubtree(item
);
895 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
896 ProcessEvent( event
);
899 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
902 DeleteChildren(item
);
905 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
907 wxGenericTreeItem
*item
= itemId
.m_pItem
;
909 if ( item
->IsExpanded() )
915 void wxTreeCtrl::Unselect()
919 m_current
->SetHilight( FALSE
);
920 RefreshLine( m_current
);
924 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
926 item
->SetHilight(FALSE
);
929 if (item
->HasChildren())
931 wxArrayGenericTreeItems
& children
= item
->GetChildren();
932 size_t count
= children
.Count();
933 for ( size_t n
= 0; n
< count
; ++n
)
934 UnselectAllChildren(children
[n
]);
938 void wxTreeCtrl::UnselectAll()
940 UnselectAllChildren(GetRootItem().m_pItem
);
943 // Recursive function !
944 // To stop we must have crt_item<last_item
946 // Tag all next children, when no more children,
947 // Move to parent (not to tag)
948 // Keep going... if we found last_item, we stop.
949 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
951 wxGenericTreeItem
*parent
= crt_item
->GetParent();
953 if ( parent
== NULL
) // This is root item
954 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
956 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
957 int index
= children
.Index(crt_item
);
958 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
960 size_t count
= children
.Count();
961 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
962 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
964 return TagNextChildren(parent
, last_item
, select
);
967 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
969 crt_item
->SetHilight(select
);
970 RefreshLine(crt_item
);
972 if (crt_item
==last_item
) return TRUE
;
974 if (crt_item
->HasChildren())
976 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
977 size_t count
= children
.Count();
978 for ( size_t n
= 0; n
< count
; ++n
)
979 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
985 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
987 // item2 is not necessary after item1
988 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
990 // choice first' and 'last' between item1 and item2
991 if (item1
->GetY()<item2
->GetY())
1002 bool select
=m_current
->HasHilight();
1004 if (TagAllChildrenUntilLast(first
,last
,select
)) return;
1006 TagNextChildren(first
,last
,select
);
1009 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1010 bool unselect_others
,
1011 bool extended_select
)
1013 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1015 //wxCHECK_RET( ( (!unselect_others) && is_single),
1016 // _T("this is a single selection tree") );
1018 // to keep going anyhow !!!
1021 unselect_others
=TRUE
;
1022 extended_select
=FALSE
;
1025 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1027 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1028 event
.m_item
= item
;
1029 event
.m_itemOld
= m_current
;
1030 event
.SetEventObject( this );
1031 // TODO : Here we don't send any selection mode yet !
1033 if ( GetEventHandler()->ProcessEvent( event
) && event
.WasVetoed() )
1037 if (unselect_others
)
1039 if (is_single
) Unselect(); // to speed up thing
1044 if (extended_select
)
1046 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1047 // don't change the mark (m_current)
1048 SelectItemRange(m_current
, item
);
1052 bool select
=TRUE
; // the default
1054 // Check if we need to toggle hilight (ctrl mode)
1055 if (!unselect_others
)
1056 select
=!item
->HasHilight();
1058 m_current
= m_key_current
= item
;
1059 m_current
->SetHilight(select
);
1060 RefreshLine( m_current
);
1063 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1064 GetEventHandler()->ProcessEvent( event
);
1067 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
, wxArrayTreeItemIds
&array
) const
1069 if (item
->HasHilight()) array
.Add(wxTreeItemId(item
));
1071 if (item
->HasChildren())
1073 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1074 size_t count
= children
.Count();
1075 for ( size_t n
= 0; n
< count
; ++n
)
1076 FillArray(children
[n
],array
);
1080 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1083 FillArray(GetRootItem().m_pItem
, array
);
1085 return array
.Count();
1088 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1090 if (!item
.IsOk()) return;
1092 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1094 // first expand all parent branches
1095 wxGenericTreeItem
*parent
= gitem
->GetParent();
1096 while ( parent
&& !parent
->IsExpanded() )
1100 parent
= parent
->GetParent();
1103 if (parent
) CalculatePositions();
1108 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1110 if (!item
.IsOk()) return;
1112 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1114 // now scroll to the item
1115 int item_y
= gitem
->GetY();
1119 ViewStart( &start_x
, &start_y
);
1120 start_y
*= PIXELS_PER_UNIT
;
1124 GetClientSize( &client_w
, &client_h
);
1126 if (item_y
< start_y
+3)
1131 m_anchor
->GetSize( x
, y
, this );
1132 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1133 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1134 // Item should appear at top
1135 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1137 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1142 m_anchor
->GetSize( x
, y
, this );
1143 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1144 item_y
+= PIXELS_PER_UNIT
+2;
1145 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1146 // Item should appear at bottom
1147 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
);
1151 wxTextCtrl
*wxTreeCtrl::EditLabel( const wxTreeItemId
& WXUNUSED(item
),
1152 wxClassInfo
* WXUNUSED(textCtrlClass
) )
1154 wxFAIL_MSG(_T("not implemented"));
1156 return (wxTextCtrl
*)NULL
;
1159 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1161 wxFAIL_MSG(_T("not implemented"));
1163 return (wxTextCtrl
*)NULL
;
1166 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool WXUNUSED(discardChanges
))
1168 wxFAIL_MSG(_T("not implemented"));
1171 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1172 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1174 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1175 wxGenericTreeItem
**item2
)
1177 wxCHECK_MSG( s_treeBeingSorted
, 0, _T("bug in wxTreeCtrl::SortChildren()") );
1179 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1182 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1183 const wxTreeItemId
& item2
)
1185 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1188 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1190 wxCHECK_RET( itemId
.IsOk(), _T("invalid tree item") );
1192 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1194 wxCHECK_RET( !s_treeBeingSorted
,
1195 _T("wxTreeCtrl::SortChildren is not reentrant") );
1197 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1198 if ( children
.Count() > 1 )
1200 s_treeBeingSorted
= this;
1201 children
.Sort(tree_ctrl_compare_func
);
1202 s_treeBeingSorted
= NULL
;
1206 //else: don't make the tree dirty as nothing changed
1209 wxImageList
*wxTreeCtrl::GetImageList() const
1211 return m_imageListNormal
;
1214 wxImageList
*wxTreeCtrl::GetStateImageList() const
1216 return m_imageListState
;
1219 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1221 m_imageListNormal
= imageList
;
1223 // Calculate a m_lineHeight value from the image sizes.
1224 // May be toggle off. Then wxTreeCtrl will spread when
1225 // necessary (which might look ugly).
1228 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1232 n
= m_imageListNormal
->GetImageCount();
1233 for(int i
= 0; i
< n
; i
++)
1235 m_imageListNormal
->GetSize(i
, width
, height
);
1236 if(height
> m_lineHeight
) m_lineHeight
= height
;
1239 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1240 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1245 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1247 m_imageListState
= imageList
;
1250 // -----------------------------------------------------------------------------
1252 // -----------------------------------------------------------------------------
1254 void wxTreeCtrl::AdjustMyScrollbars()
1260 m_anchor
->GetSize( x
, y
, this );
1261 //y += GetLineHeight(m_anchor);
1262 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1263 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1264 int y_pos
= GetScrollPos( wxVERTICAL
);
1265 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1269 SetScrollbars( 0, 0, 0, 0 );
1273 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1275 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HIGHT
)
1276 return item
->GetHeight();
1278 return m_lineHeight
;
1281 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1283 // render bold items in bold
1289 fontOld
= dc
.GetFont();
1292 // VZ: is there any better way to make a bold variant of old font?
1293 fontNew
= wxFont( fontOld
.GetPointSize(),
1294 fontOld
.GetFamily(),
1297 fontOld
.GetUnderlined());
1298 dc
.SetFont(fontNew
);
1302 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1308 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1312 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1314 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1317 else if (item
->GetImage() != -1)
1319 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1323 int total_h
= GetLineHeight(item
);
1325 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1327 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1329 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1330 m_imageListNormal
->Draw( item
->GetSelectedImage(), dc
,
1332 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1333 wxIMAGELIST_DRAW_TRANSPARENT
);
1334 dc
.DestroyClippingRegion();
1336 else if (item
->GetImage() != -1)
1338 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1339 m_imageListNormal
->Draw( item
->GetImage(), dc
,
1341 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1342 wxIMAGELIST_DRAW_TRANSPARENT
);
1343 dc
.DestroyClippingRegion();
1346 dc
.SetBackgroundMode(wxTRANSPARENT
);
1347 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1348 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1350 // restore normal font for bold items
1353 dc
.SetFont( fontOld
);
1357 // Now y stands for the top of the item, whereas it used to stand for middle !
1358 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1360 int horizX
= level
*m_indent
;
1362 item
->SetX( horizX
+m_indent
+m_spacing
);
1366 y
+=GetLineHeight(item
)/2;
1368 item
->SetCross( horizX
+m_indent
, y
);
1370 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1371 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY()-2 );
1373 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
)+4 )) // 10000 = very much
1375 int startX
= horizX
;
1376 int endX
= horizX
+ (m_indent
-5);
1378 // if (!item->HasChildren()) endX += (m_indent+5);
1379 if (!item
->HasChildren()) endX
+= 20;
1381 dc
.DrawLine( startX
, y
, endX
, y
);
1383 if (item
->HasPlus())
1385 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1386 dc
.SetPen( *wxGREY_PEN
);
1387 dc
.SetBrush( *wxWHITE_BRUSH
);
1388 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1389 dc
.SetPen( *wxBLACK_PEN
);
1390 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1392 if (!item
->IsExpanded())
1394 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1398 if (item
->HasHilight())
1400 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1402 dc
.SetBrush( *m_hilightBrush
);
1405 dc
.SetPen( *wxBLACK_PEN
);
1407 dc
.SetPen( *wxTRANSPARENT_PEN
);
1409 PaintItem(item
, dc
);
1411 dc
.SetPen( *wxBLACK_PEN
);
1412 dc
.SetTextForeground( *wxBLACK
);
1413 dc
.SetBrush( *wxWHITE_BRUSH
);
1417 dc
.SetBrush( *wxWHITE_BRUSH
);
1418 dc
.SetPen( *wxTRANSPARENT_PEN
);
1420 PaintItem(item
, dc
);
1422 dc
.SetPen( *wxBLACK_PEN
);
1426 y
= oldY
+GetLineHeight(item
);
1428 if (item
->IsExpanded())
1430 oldY
+=GetLineHeight(item
)/2;
1431 int semiOldY
=y
; // (=y) for stupid compilator
1433 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1434 size_t n
, count
= children
.Count();
1435 for ( n
= 0; n
< count
; ++n
)
1438 PaintLevel( children
[n
], dc
, level
+1, y
);
1441 // it may happen that the item is expanded but has no items (when you
1442 // delete all its children for example) - don't draw the vertical line
1446 semiOldY
+=GetLineHeight(children
[--n
])/2;
1447 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1452 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1456 wxGenericTreeItem
*i
=item
.m_pItem
;
1460 dc
.SetLogicalFunction(wxINVERT
);
1463 ViewStart(&x
,&h
); // we only need x
1464 GetClientSize(&w
,&h
); // we only need w
1466 h
=GetLineHeight(i
)+1;
1467 // 2 white column at border
1468 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1471 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1475 wxGenericTreeItem
*i
=item
.m_pItem
;
1479 dc
.SetLogicalFunction(wxINVERT
);
1484 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1487 dc
.DrawLine( 0, y
, w
, y
);
1490 // -----------------------------------------------------------------------------
1491 // wxWindows callbacks
1492 // -----------------------------------------------------------------------------
1494 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1502 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1504 dc
.SetPen( m_dottedPen
);
1505 //if(GetImageList() == NULL)
1506 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1509 PaintLevel( m_anchor
, dc
, 0, y
);
1512 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1516 if (m_current
) RefreshLine( m_current
);
1519 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1523 if (m_current
) RefreshLine( m_current
);
1526 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1528 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1529 te
.m_code
= event
.KeyCode();
1530 te
.SetEventObject( this );
1531 GetEventHandler()->ProcessEvent( te
);
1533 if ( (m_current
== 0) || (m_key_current
== 0) )
1539 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1540 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1541 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1543 switch (event
.KeyCode())
1547 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1555 if (IsExpanded(m_current
))
1557 Collapse(m_current
);
1569 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1570 event
.m_item
= m_current
;
1572 event
.SetEventObject( this );
1573 GetEventHandler()->ProcessEvent( event
);
1577 // up goes to the previous sibling or to the last of its children if
1581 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1584 prev
= GetParent( m_key_current
);
1588 wxTreeItemId current
= m_key_current
;
1589 if (current
== GetFirstChild( prev
, cockie
))
1591 // otherwise we return to where we came from
1592 SelectItem( prev
, unselect_others
, extended_select
);
1593 m_key_current
=prev
.m_pItem
;
1594 EnsureVisible( prev
);
1601 while ( IsExpanded(prev
) && HasChildren(prev
) )
1603 wxTreeItemId child
= GetLastChild(prev
);
1610 SelectItem( prev
, unselect_others
, extended_select
);
1611 m_key_current
=prev
.m_pItem
;
1612 EnsureVisible( prev
);
1617 // left arrow goes to the parent
1620 wxTreeItemId prev
= GetParent( m_current
);
1623 EnsureVisible( prev
);
1624 SelectItem( prev
, unselect_others
, extended_select
);
1630 // this works the same as the down arrow except that we also expand the
1631 // item if it wasn't expanded yet
1637 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1640 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1641 SelectItem( child
, unselect_others
, extended_select
);
1642 m_key_current
=child
.m_pItem
;
1643 EnsureVisible( child
);
1647 wxTreeItemId next
= GetNextSibling( m_key_current
);
1650 wxTreeItemId current
= m_key_current
;
1651 while (current
&& !next
)
1653 current
= GetParent( current
);
1654 if (current
) next
= GetNextSibling( current
);
1659 SelectItem( next
, unselect_others
, extended_select
);
1660 m_key_current
=next
.m_pItem
;
1661 EnsureVisible( next
);
1667 // <End> selects the last visible tree item
1670 wxTreeItemId last
= GetRootItem();
1672 while ( last
.IsOk() && IsExpanded(last
) )
1674 wxTreeItemId lastChild
= GetLastChild(last
);
1676 // it may happen if the item was expanded but then all of
1677 // its children have been deleted - so IsExpanded() returned
1678 // TRUE, but GetLastChild() returned invalid item
1687 EnsureVisible( last
);
1688 SelectItem( last
, unselect_others
, extended_select
);
1693 // <Home> selects the root item
1696 wxTreeItemId prev
= GetRootItem();
1699 EnsureVisible( prev
);
1700 SelectItem( prev
, unselect_others
, extended_select
);
1710 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1712 wxClientDC
dc(this);
1714 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1715 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1720 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1721 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1722 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1723 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1725 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1728 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1730 if (!event
.LeftIsDown()) m_dragCount
= 0;
1732 if ( !(event
.LeftUp() || event
.LeftDClick() || event
.Dragging()) ) return;
1734 if ( !m_anchor
) return;
1736 wxClientDC
dc(this);
1738 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1739 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1742 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1743 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1745 if (item
== NULL
) return; /* we hit the blank area */
1747 if (event
.Dragging())
1749 if (m_dragCount
== 2) /* small drag latency (3?) */
1753 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG
, GetId());
1754 nevent
.m_item
= m_current
;
1755 nevent
.SetEventObject(this);
1756 GetEventHandler()->ProcessEvent(nevent
);
1765 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1766 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1767 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1772 if (is_multiple
) return;
1775 SelectItem(item
, unselect_others
, extended_select
);
1777 if (event
.LeftDClick())
1779 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1780 event
.m_item
= item
;
1782 event
.SetEventObject( this );
1783 GetEventHandler()->ProcessEvent( event
);
1787 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1789 /* after all changes have been done to the tree control,
1790 * we actually redraw the tree when everything is over */
1797 CalculatePositions();
1799 AdjustMyScrollbars();
1802 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
1806 // TODO : check for boldness. Here with suppose that font normal and bold
1807 // have the same height !
1808 // TODO : bug here, text_w is sometime not the correct answer !!!
1809 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1814 if ((item
->IsExpanded()) && (item
->GetSelectedImage() != -1))
1816 m_imageListNormal
->GetSize( item
->GetSelectedImage(), image_w
, image_h
);
1819 else if (item
->GetImage() != -1)
1821 m_imageListNormal
->GetSize( item
->GetImage(), image_w
, image_h
);
1825 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
1827 if (total_h
<40) total_h
+=4; // at least 4 pixels
1828 else total_h
+=total_h
/10; // otherwise 10% extra spacing
1830 item
->SetHeight(total_h
);
1831 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
1833 item
->SetWidth(image_w
+text_w
+2);
1836 // -----------------------------------------------------------------------------
1837 // for developper : y is now the top of the level
1838 // not the middle of it !
1839 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1841 int horizX
= level
*m_indent
;
1843 CalculateSize( item
, dc
);
1846 item
->SetX( horizX
+m_indent
+m_spacing
);
1848 y
+=GetLineHeight(item
);
1850 if ( !item
->IsExpanded() )
1852 // we dont need to calculate collapsed branches
1856 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1857 size_t n
, count
= children
.Count();
1858 for (n
= 0; n
< count
; ++n
)
1859 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
1862 void wxTreeCtrl::CalculatePositions()
1864 if ( !m_anchor
) return;
1866 wxClientDC
dc(this);
1869 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1871 dc
.SetPen( m_dottedPen
);
1872 //if(GetImageList() == NULL)
1873 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1875 int y
= 2; //GetLineHeight(m_anchor) / 2 + 2;
1876 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
1879 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
1881 wxClientDC
dc(this);
1886 GetClientSize( &cw
, &ch
);
1889 rect
.x
= dc
.LogicalToDeviceX( 0 );
1891 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
1894 Refresh( TRUE
, &rect
);
1896 AdjustMyScrollbars();
1899 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
1901 wxClientDC
dc(this);
1905 rect
.x
= dc
.LogicalToDeviceX( item
->GetX() - 2 );
1906 rect
.y
= dc
.LogicalToDeviceY( item
->GetY());
1908 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
1910 Refresh( TRUE
, &rect
);