1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/treectrl.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/settings.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
48 WX_DEFINE_OBJARRAY(wxArrayTreeItemIds
);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int NO_IMAGE
= -1;
56 // -----------------------------------------------------------------------------
58 // -----------------------------------------------------------------------------
61 class WXDLLEXPORT wxGenericTreeItem
65 wxGenericTreeItem() { m_data
= NULL
; }
66 wxGenericTreeItem( wxGenericTreeItem
*parent
,
69 int image
, int selImage
,
70 wxTreeItemData
*data
);
75 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
77 const wxString
& GetText() const { return m_text
; }
78 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
79 { return m_images
[which
]; }
80 wxTreeItemData
*GetData() const { return m_data
; }
82 // returns the current image for the item (depending on its
83 // selected/expanded/whatever state)
84 int GetCurrentImage() const;
86 void SetText( const wxString
&text
);
87 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
88 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
90 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
92 void SetBold(bool bold
) { m_isBold
= bold
; }
94 int GetX() const { return m_x
; }
95 int GetY() const { return m_y
; }
97 void SetX(int x
) { m_x
= x
; }
98 void SetY(int y
) { m_y
= y
; }
100 int GetHeight() const { return m_height
; }
101 int GetWidth() const { return m_width
; }
103 void SetHeight(int h
) { m_height
= h
; }
104 void SetWidth(int w
) { m_width
= w
; }
107 wxGenericTreeItem
*GetParent() const { return m_parent
; }
110 // deletes all children notifying the treectrl about it if !NULL
112 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
113 // FIXME don't know what is it for
116 // get count of all children (and grand children if 'recursively')
117 size_t GetChildrenCount(bool recursively
= TRUE
) const;
119 void Insert(wxGenericTreeItem
*child
, size_t index
)
120 { m_children
.Insert(child
, index
); }
122 void SetCross( int x
, int y
);
123 void GetSize( int &x
, int &y
, const wxTreeCtrl
* );
125 // return the item at given position (or NULL if no item), onButton is
126 // TRUE if the point belongs to the item's button, otherwise it lies
127 // on the button's label
128 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxTreeCtrl
*, int &flags
);
130 void Expand() { m_isCollapsed
= FALSE
; }
131 void Collapse() { m_isCollapsed
= TRUE
; }
133 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
136 bool HasChildren() const { return !m_children
.IsEmpty(); }
137 bool IsSelected() const { return m_hasHilight
; }
138 bool IsExpanded() const { return !m_isCollapsed
; }
139 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
140 bool IsBold() const { return m_isBold
; }
143 // get them - may be NULL
144 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
145 // get them ensuring that the pointer is not NULL
146 wxTreeItemAttr
& Attr()
149 m_attr
= new wxTreeItemAttr
;
157 // tree ctrl images for the normal, selected, expanded and
158 // expanded+selected states
159 int m_images
[wxTreeItemIcon_Max
];
161 wxTreeItemData
*m_data
;
163 // use bitfields to save size
164 int m_isCollapsed
:1;
165 int m_hasHilight
:1; // same as focused
166 int m_hasPlus
:1; // used for item which doesn't have
167 // children but has a [+] button
168 int m_isBold
:1; // render the label in bold font
171 long m_height
, m_width
;
172 int m_xCross
, m_yCross
;
175 wxArrayGenericTreeItems m_children
;
176 wxGenericTreeItem
*m_parent
;
178 wxTreeItemAttr
*m_attr
;
181 // =============================================================================
183 // =============================================================================
186 // -----------------------------------------------------------------------------
187 // wxTreeRenameTimer (internal)
188 // -----------------------------------------------------------------------------
190 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl
*owner
)
195 void wxTreeRenameTimer::Notify()
197 m_owner
->OnRenameTimer();
200 //-----------------------------------------------------------------------------
201 // wxTreeTextCtrl (internal)
202 //-----------------------------------------------------------------------------
204 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
206 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
207 EVT_CHAR (wxTreeTextCtrl::OnChar
)
208 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
211 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
, const wxWindowID id
,
212 bool *accept
, wxString
*res
, wxTreeCtrl
*owner
,
213 const wxString
&value
, const wxPoint
&pos
, const wxSize
&size
,
215 int style
, const wxValidator
& validator
, const wxString
&name
) :
217 wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
224 m_startValue
= value
;
227 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
229 if (event
.m_keyCode
== WXK_RETURN
)
232 (*m_res
) = GetValue();
236 if (event
.m_keyCode
== WXK_ESCAPE
)
246 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
248 if (wxPendingDelete
.Member(this)) return;
250 wxPendingDelete
.Append(this);
252 if ((*m_accept
) && ((*m_res
) != m_startValue
))
253 m_owner
->OnRenameAccept();
256 #define PIXELS_PER_UNIT 10
257 // -----------------------------------------------------------------------------
259 // -----------------------------------------------------------------------------
261 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
263 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
264 : wxNotifyEvent( commandType
, id
)
267 m_itemOld
= (wxGenericTreeItem
*)NULL
;
270 // -----------------------------------------------------------------------------
272 // -----------------------------------------------------------------------------
274 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
275 const wxString
& text
,
277 int image
, int selImage
,
278 wxTreeItemData
*data
)
281 m_images
[wxTreeItemIcon_Normal
] = image
;
282 m_images
[wxTreeItemIcon_Selected
] = selImage
;
283 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
284 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
288 m_xCross
= m_yCross
= 0;
292 m_isCollapsed
= TRUE
;
293 m_hasHilight
= FALSE
;
299 m_attr
= (wxTreeItemAttr
*)NULL
;
301 // We don't know the height here yet.
306 wxGenericTreeItem::~wxGenericTreeItem()
312 wxASSERT_MSG( m_children
.IsEmpty(),
313 wxT("please call DeleteChildren() before deleting the item") );
316 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
318 size_t count
= m_children
.Count();
319 for ( size_t n
= 0; n
< count
; n
++ )
321 wxGenericTreeItem
*child
= m_children
[n
];
323 tree
->SendDeleteEvent(child
);
325 child
->DeleteChildren(tree
);
332 void wxGenericTreeItem::SetText( const wxString
&text
)
337 void wxGenericTreeItem::Reset()
340 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
342 m_images
[i
] = NO_IMAGE
;
347 m_height
= m_width
= 0;
354 m_isCollapsed
= TRUE
;
356 m_parent
= (wxGenericTreeItem
*)NULL
;
359 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
361 size_t count
= m_children
.Count();
365 size_t total
= count
;
366 for ( size_t n
= 0; n
< count
; ++n
)
368 total
+= m_children
[n
]->GetChildrenCount();
374 void wxGenericTreeItem::SetCross( int x
, int y
)
380 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
382 int bottomY
=m_y
+theTree
->GetLineHeight(this);
383 if ( y
< bottomY
) y
= bottomY
;
384 int width
= m_x
+ m_width
;
385 if ( x
< width
) x
= width
;
389 size_t count
= m_children
.Count();
390 for ( size_t n
= 0; n
< count
; ++n
)
392 m_children
[n
]->GetSize( x
, y
, theTree
);
397 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
398 const wxTreeCtrl
*theTree
,
401 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
403 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2)
404 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
406 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
408 // 5 is the size of the plus sign
409 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
410 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
411 (IsExpanded() || HasPlus()))
413 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
417 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
422 // assuming every image (normal and selected ) has the same size !
423 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
424 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
426 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
427 flags
|= wxTREE_HITTEST_ONITEMICON
;
429 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
435 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
436 if (point
.x
> m_x
+m_width
)
437 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
445 size_t count
= m_children
.Count();
446 for ( size_t n
= 0; n
< count
; n
++ )
448 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
455 flags
|=wxTREE_HITTEST_NOWHERE
;
459 int wxGenericTreeItem::GetCurrentImage() const
461 int image
= NO_IMAGE
;
466 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
469 if ( image
== NO_IMAGE
)
471 // we usually fall back to the normal item, but try just the
472 // expanded one (and not selected) first in this case
473 image
= GetImage(wxTreeItemIcon_Expanded
);
479 image
= GetImage(wxTreeItemIcon_Selected
);
482 // may be it doesn't have the specific image we want, try the default one
484 if ( image
== NO_IMAGE
)
492 // -----------------------------------------------------------------------------
493 // wxTreeCtrl implementation
494 // -----------------------------------------------------------------------------
496 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
498 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
499 EVT_PAINT (wxTreeCtrl::OnPaint
)
500 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
501 EVT_CHAR (wxTreeCtrl::OnChar
)
502 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
503 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
504 EVT_IDLE (wxTreeCtrl::OnIdle
)
507 // -----------------------------------------------------------------------------
508 // construction/destruction
509 // -----------------------------------------------------------------------------
511 void wxTreeCtrl::Init()
515 m_anchor
= (wxGenericTreeItem
*) NULL
;
525 m_hilightBrush
= new wxBrush
527 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
532 m_imageListState
= (wxImageList
*) NULL
;
536 m_renameTimer
= new wxTreeRenameTimer( this );
538 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
539 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
540 m_normalFont
.GetFamily(),
541 m_normalFont
.GetStyle(),
543 m_normalFont
.GetUnderlined());
546 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
547 const wxPoint
& pos
, const wxSize
& size
,
550 const wxValidator
&validator
,
552 const wxString
& name
)
556 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
559 SetValidator( validator
);
562 SetBackgroundColour( *wxWHITE
);
563 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
564 m_dottedPen
= wxPen( "grey", 0, 0 );
569 wxTreeCtrl::~wxTreeCtrl()
571 wxDELETE( m_hilightBrush
);
575 delete m_renameTimer
;
578 // -----------------------------------------------------------------------------
580 // -----------------------------------------------------------------------------
582 size_t wxTreeCtrl::GetCount() const
584 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
587 void wxTreeCtrl::SetIndent(unsigned int indent
)
594 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
601 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
603 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
605 return item
.m_pItem
->GetChildrenCount(recursively
);
608 // -----------------------------------------------------------------------------
609 // functions to work with tree items
610 // -----------------------------------------------------------------------------
612 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
614 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
616 return item
.m_pItem
->GetText();
619 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
620 wxTreeItemIcon which
) const
622 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
624 return item
.m_pItem
->GetImage(which
);
627 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
629 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
631 return item
.m_pItem
->GetData();
634 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
636 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
639 wxGenericTreeItem
*pItem
= item
.m_pItem
;
640 pItem
->SetText(text
);
641 CalculateSize(pItem
, dc
);
645 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
647 wxTreeItemIcon which
)
649 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
651 wxGenericTreeItem
*pItem
= item
.m_pItem
;
652 pItem
->SetImage(image
, which
);
655 CalculateSize(pItem
, dc
);
659 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
661 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
663 item
.m_pItem
->SetData(data
);
666 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
668 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
670 wxGenericTreeItem
*pItem
= item
.m_pItem
;
671 pItem
->SetHasPlus(has
);
675 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
677 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
679 // avoid redrawing the tree if no real change
680 wxGenericTreeItem
*pItem
= item
.m_pItem
;
681 if ( pItem
->IsBold() != bold
)
683 pItem
->SetBold(bold
);
688 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
691 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
693 wxGenericTreeItem
*pItem
= item
.m_pItem
;
694 pItem
->Attr().SetTextColour(col
);
698 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
701 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
703 wxGenericTreeItem
*pItem
= item
.m_pItem
;
704 pItem
->Attr().SetBackgroundColour(col
);
708 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
710 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
712 wxGenericTreeItem
*pItem
= item
.m_pItem
;
713 pItem
->Attr().SetFont(font
);
717 // -----------------------------------------------------------------------------
718 // item status inquiries
719 // -----------------------------------------------------------------------------
721 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
723 wxFAIL_MSG(wxT("not implemented"));
728 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
730 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
732 return !item
.m_pItem
->GetChildren().IsEmpty();
735 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
737 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
739 return item
.m_pItem
->IsExpanded();
742 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
744 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
746 return item
.m_pItem
->IsSelected();
749 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
751 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
753 return item
.m_pItem
->IsBold();
756 // -----------------------------------------------------------------------------
758 // -----------------------------------------------------------------------------
760 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
762 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
764 return item
.m_pItem
->GetParent();
767 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
769 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
772 return GetNextChild(item
, cookie
);
775 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
777 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
779 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
780 if ( (size_t)cookie
< children
.Count() )
782 return children
.Item(cookie
++);
786 // there are no more of them
787 return wxTreeItemId();
791 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
793 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
795 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
796 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
799 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
801 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
803 wxGenericTreeItem
*i
= item
.m_pItem
;
804 wxGenericTreeItem
*parent
= i
->GetParent();
805 if ( parent
== NULL
)
807 // root item doesn't have any siblings
808 return wxTreeItemId();
811 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
812 int index
= siblings
.Index(i
);
813 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
815 size_t n
= (size_t)(index
+ 1);
816 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
819 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
821 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
823 wxGenericTreeItem
*i
= item
.m_pItem
;
824 wxGenericTreeItem
*parent
= i
->GetParent();
825 if ( parent
== NULL
)
827 // root item doesn't have any siblings
828 return wxTreeItemId();
831 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
832 int index
= siblings
.Index(i
);
833 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
835 return index
== 0 ? wxTreeItemId()
836 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
839 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
841 wxFAIL_MSG(wxT("not implemented"));
843 return wxTreeItemId();
846 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
848 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
850 wxFAIL_MSG(wxT("not implemented"));
852 return wxTreeItemId();
855 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
857 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
859 wxFAIL_MSG(wxT("not implemented"));
861 return wxTreeItemId();
864 // -----------------------------------------------------------------------------
866 // -----------------------------------------------------------------------------
868 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
870 const wxString
& text
,
871 int image
, int selImage
,
872 wxTreeItemData
*data
)
874 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
877 // should we give a warning here?
878 return AddRoot(text
, image
, selImage
, data
);
882 wxGenericTreeItem
*item
=
883 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
887 data
->m_pItem
= item
;
890 parent
->Insert( item
, previous
);
897 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
898 int image
, int selImage
,
899 wxTreeItemData
*data
)
901 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
904 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
905 image
, selImage
, data
);
908 data
->m_pItem
= m_anchor
;
911 if (!HasFlag(wxTR_MULTIPLE
))
913 m_current
= m_key_current
= m_anchor
;
914 m_current
->SetHilight( TRUE
);
918 AdjustMyScrollbars();
923 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
924 const wxString
& text
,
925 int image
, int selImage
,
926 wxTreeItemData
*data
)
928 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
931 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
932 const wxTreeItemId
& idPrevious
,
933 const wxString
& text
,
934 int image
, int selImage
,
935 wxTreeItemData
*data
)
937 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
940 // should we give a warning here?
941 return AddRoot(text
, image
, selImage
, data
);
944 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
945 wxASSERT_MSG( index
!= wxNOT_FOUND
,
946 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
947 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
950 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
951 const wxString
& text
,
952 int image
, int selImage
,
953 wxTreeItemData
*data
)
955 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
958 // should we give a warning here?
959 return AddRoot(text
, image
, selImage
, data
);
962 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
963 image
, selImage
, data
);
966 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
968 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
970 event
.SetEventObject( this );
971 ProcessEvent( event
);
974 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
976 wxGenericTreeItem
*item
= itemId
.m_pItem
;
977 item
->DeleteChildren(this);
982 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
984 wxGenericTreeItem
*item
= itemId
.m_pItem
;
985 wxGenericTreeItem
*parent
= item
->GetParent();
989 parent
->GetChildren().Remove( item
); // remove by value
992 item
->DeleteChildren(this);
993 SendDeleteEvent(item
);
999 void wxTreeCtrl::DeleteAllItems()
1003 m_anchor
->DeleteChildren(this);
1012 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1014 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1016 if ( !item
->HasPlus() )
1019 if ( item
->IsExpanded() )
1022 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1023 event
.m_item
= item
;
1024 event
.SetEventObject( this );
1026 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1027 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1029 // cancelled by program
1034 CalculatePositions();
1036 RefreshSubtree(item
);
1038 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1039 ProcessEvent( event
);
1042 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1044 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1046 if ( !item
->IsExpanded() )
1049 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1050 event
.m_item
= item
;
1051 event
.SetEventObject( this );
1052 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1054 // cancelled by program
1060 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1061 size_t count
= children
.Count();
1062 for ( size_t n
= 0; n
< count
; n
++ )
1064 Collapse(children
[n
]);
1067 CalculatePositions();
1069 RefreshSubtree(item
);
1071 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1072 ProcessEvent( event
);
1075 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1078 DeleteChildren(item
);
1081 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1083 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1085 if (item
->IsExpanded())
1091 void wxTreeCtrl::Unselect()
1095 m_current
->SetHilight( FALSE
);
1096 RefreshLine( m_current
);
1100 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1102 if (item
->IsSelected())
1104 item
->SetHilight(FALSE
);
1108 if (item
->HasChildren())
1110 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1111 size_t count
= children
.Count();
1112 for ( size_t n
= 0; n
< count
; ++n
)
1114 UnselectAllChildren(children
[n
]);
1119 void wxTreeCtrl::UnselectAll()
1121 UnselectAllChildren(GetRootItem().m_pItem
);
1124 // Recursive function !
1125 // To stop we must have crt_item<last_item
1127 // Tag all next children, when no more children,
1128 // Move to parent (not to tag)
1129 // Keep going... if we found last_item, we stop.
1130 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1132 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1134 if (parent
== NULL
) // This is root item
1135 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1137 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1138 int index
= children
.Index(crt_item
);
1139 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1141 size_t count
= children
.Count();
1142 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1144 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1147 return TagNextChildren(parent
, last_item
, select
);
1150 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1152 crt_item
->SetHilight(select
);
1153 RefreshLine(crt_item
);
1155 if (crt_item
==last_item
)
1158 if (crt_item
->HasChildren())
1160 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1161 size_t count
= children
.Count();
1162 for ( size_t n
= 0; n
< count
; ++n
)
1164 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1172 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1174 // item2 is not necessary after item1
1175 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1177 // choice first' and 'last' between item1 and item2
1178 if (item1
->GetY()<item2
->GetY())
1189 bool select
= m_current
->IsSelected();
1191 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1194 TagNextChildren(first
,last
,select
);
1197 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1198 bool unselect_others
,
1199 bool extended_select
)
1201 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1203 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1204 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1206 //wxCHECK_RET( ( (!unselect_others) && is_single),
1207 // wxT("this is a single selection tree") );
1209 // to keep going anyhow !!!
1212 if (item
->IsSelected())
1213 return; // nothing to do
1214 unselect_others
= TRUE
;
1215 extended_select
= FALSE
;
1217 else if ( unselect_others
&& item
->IsSelected() )
1219 // selection change if there is more than one item currently selected
1220 wxArrayTreeItemIds selected_items
;
1221 if ( GetSelections(selected_items
) == 1 )
1225 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1226 event
.m_item
= item
;
1227 event
.m_itemOld
= m_current
;
1228 event
.SetEventObject( this );
1229 // TODO : Here we don't send any selection mode yet !
1231 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1235 if (unselect_others
)
1237 if (is_single
) Unselect(); // to speed up thing
1242 if (extended_select
)
1244 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1245 // don't change the mark (m_current)
1246 SelectItemRange(m_current
, item
);
1250 bool select
=TRUE
; // the default
1252 // Check if we need to toggle hilight (ctrl mode)
1253 if (!unselect_others
)
1254 select
=!item
->IsSelected();
1256 m_current
= m_key_current
= item
;
1257 m_current
->SetHilight(select
);
1258 RefreshLine( m_current
);
1261 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1262 GetEventHandler()->ProcessEvent( event
);
1265 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1266 wxArrayTreeItemIds
&array
) const
1268 if ( item
->IsSelected() )
1269 array
.Add(wxTreeItemId(item
));
1271 if ( item
->HasChildren() )
1273 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1274 size_t count
= children
.GetCount();
1275 for ( size_t n
= 0; n
< count
; ++n
)
1276 FillArray(children
[n
],array
);
1280 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1283 FillArray(GetRootItem().m_pItem
, array
);
1285 return array
.Count();
1288 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1290 if (!item
.IsOk()) return;
1292 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1294 // first expand all parent branches
1295 wxGenericTreeItem
*parent
= gitem
->GetParent();
1299 parent
= parent
->GetParent();
1302 //if (parent) CalculatePositions();
1307 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1309 if (!item
.IsOk()) return;
1311 // We have to call this here because the label in
1312 // question might just have been added and no screen
1313 // update taken place.
1314 if (m_dirty
) wxYield();
1316 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1318 // now scroll to the item
1319 int item_y
= gitem
->GetY();
1323 ViewStart( &start_x
, &start_y
);
1324 start_y
*= PIXELS_PER_UNIT
;
1328 GetClientSize( &client_w
, &client_h
);
1330 if (item_y
< start_y
+3)
1335 m_anchor
->GetSize( x
, y
, this );
1336 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1337 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1338 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1339 // Item should appear at top
1340 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1342 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1347 m_anchor
->GetSize( x
, y
, this );
1348 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1349 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1350 item_y
+= PIXELS_PER_UNIT
+2;
1351 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1352 // Item should appear at bottom
1353 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
);
1357 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1358 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1360 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1361 wxGenericTreeItem
**item2
)
1363 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1365 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1368 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1369 const wxTreeItemId
& item2
)
1371 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1374 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1376 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1378 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1380 wxCHECK_RET( !s_treeBeingSorted
,
1381 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1383 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1384 if ( children
.Count() > 1 )
1386 s_treeBeingSorted
= this;
1387 children
.Sort(tree_ctrl_compare_func
);
1388 s_treeBeingSorted
= NULL
;
1392 //else: don't make the tree dirty as nothing changed
1395 wxImageList
*wxTreeCtrl::GetImageList() const
1397 return m_imageListNormal
;
1400 wxImageList
*wxTreeCtrl::GetStateImageList() const
1402 return m_imageListState
;
1405 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1407 m_imageListNormal
= imageList
;
1409 // Calculate a m_lineHeight value from the image sizes.
1410 // May be toggle off. Then wxTreeCtrl will spread when
1411 // necessary (which might look ugly).
1413 wxClientDC
dc(this);
1414 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1418 n
= m_imageListNormal
->GetImageCount();
1419 for(int i
= 0; i
< n
; i
++)
1421 m_imageListNormal
->GetSize(i
, width
, height
);
1422 if(height
> m_lineHeight
) m_lineHeight
= height
;
1425 if (m_lineHeight
<40) m_lineHeight
+=2; // at least 4 pixels (odd such that a line can be drawn in between)
1426 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1431 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1433 m_imageListState
= imageList
;
1436 // -----------------------------------------------------------------------------
1438 // -----------------------------------------------------------------------------
1440 void wxTreeCtrl::AdjustMyScrollbars()
1446 m_anchor
->GetSize( x
, y
, this );
1447 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1448 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1449 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1450 int y_pos
= GetScrollPos( wxVERTICAL
);
1451 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1455 SetScrollbars( 0, 0, 0, 0 );
1459 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1461 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1462 return item
->GetHeight();
1464 return m_lineHeight
;
1467 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1469 wxTreeItemAttr
*attr
= item
->GetAttributes();
1470 if ( attr
&& attr
->HasFont() )
1471 dc
.SetFont(attr
->GetFont());
1472 else if (item
->IsBold())
1473 dc
.SetFont(m_boldFont
);
1477 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1481 int image
= item
->GetCurrentImage();
1482 if ( image
!= NO_IMAGE
)
1484 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1488 int total_h
= GetLineHeight(item
);
1490 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1492 if ( image
!= NO_IMAGE
)
1494 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1495 m_imageListNormal
->Draw( image
, dc
,
1497 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1498 wxIMAGELIST_DRAW_TRANSPARENT
);
1499 dc
.DestroyClippingRegion();
1502 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1503 dc
.SetBackgroundMode(hasBgCol
? wxSOLID
: wxTRANSPARENT
);
1505 dc
.SetTextBackground(attr
->GetBackgroundColour());
1506 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1507 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1509 // restore normal font
1510 dc
.SetFont( m_normalFont
);
1513 // Now y stands for the top of the item, whereas it used to stand for middle !
1514 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1516 int horizX
= level
*m_indent
;
1518 item
->SetX( horizX
+m_indent
+m_spacing
);
1522 y
+=GetLineHeight(item
)/2;
1524 item
->SetCross( horizX
+m_indent
, y
);
1526 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1527 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1529 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1531 int startX
= horizX
;
1532 int endX
= horizX
+ (m_indent
-5);
1534 // if (!item->HasChildren()) endX += (m_indent+5);
1535 if (!item
->HasChildren()) endX
+= 20;
1537 dc
.DrawLine( startX
, y
, endX
, y
);
1539 if (item
->HasPlus())
1541 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1542 dc
.SetPen( *wxGREY_PEN
);
1543 dc
.SetBrush( *wxWHITE_BRUSH
);
1544 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1546 dc
.SetPen( *wxBLACK_PEN
);
1547 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1548 if (!item
->IsExpanded())
1549 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1551 dc
.SetPen( m_dottedPen
);
1554 wxPen
*pen
= wxTRANSPARENT_PEN
;
1555 wxBrush
*brush
; // FIXME is this really needed?
1558 if ( item
->IsSelected() )
1560 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1562 brush
= m_hilightBrush
;
1570 wxTreeItemAttr
*attr
= item
->GetAttributes();
1571 if ( attr
&& attr
->HasTextColour() )
1572 colText
= attr
->GetTextColour();
1576 brush
= wxWHITE_BRUSH
;
1580 dc
.SetTextForeground(colText
);
1582 dc
.SetBrush(*brush
);
1585 PaintItem(item
, dc
);
1587 // restore DC objects
1588 dc
.SetBrush( *wxWHITE_BRUSH
);
1589 dc
.SetPen( m_dottedPen
);
1590 dc
.SetTextForeground( *wxBLACK
);
1593 y
= oldY
+GetLineHeight(item
);
1595 if (item
->IsExpanded())
1597 oldY
+=GetLineHeight(item
)/2;
1600 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1601 size_t n
, count
= children
.Count();
1602 for ( n
= 0; n
< count
; ++n
)
1605 PaintLevel( children
[n
], dc
, level
+1, y
);
1608 // it may happen that the item is expanded but has no items (when you
1609 // delete all its children for example) - don't draw the vertical line
1613 semiOldY
+=GetLineHeight(children
[--n
])/2;
1614 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1619 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1623 wxGenericTreeItem
*i
=item
.m_pItem
;
1625 wxClientDC
dc(this);
1627 dc
.SetLogicalFunction(wxINVERT
);
1630 ViewStart(&x
,&h
); // we only need x
1631 GetClientSize(&w
,&h
); // we only need w
1633 h
=GetLineHeight(i
)+1;
1634 // 2 white column at border
1635 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1638 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1642 wxGenericTreeItem
*i
=item
.m_pItem
;
1644 wxClientDC
dc(this);
1646 dc
.SetLogicalFunction(wxINVERT
);
1651 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1654 dc
.DrawLine( 0, y
, w
, y
);
1657 // -----------------------------------------------------------------------------
1658 // wxWindows callbacks
1659 // -----------------------------------------------------------------------------
1661 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1669 dc
.SetFont( m_normalFont
);
1670 dc
.SetPen( m_dottedPen
);
1672 // this is now done dynamically
1673 //if(GetImageList() == NULL)
1674 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1677 PaintLevel( m_anchor
, dc
, 0, y
);
1680 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1684 if (m_current
) RefreshLine( m_current
);
1687 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1691 if (m_current
) RefreshLine( m_current
);
1694 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1696 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1697 te
.m_code
= event
.KeyCode();
1698 te
.SetEventObject( this );
1699 GetEventHandler()->ProcessEvent( te
);
1701 if ( (m_current
== 0) || (m_key_current
== 0) )
1707 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1708 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1709 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1711 switch (event
.KeyCode())
1715 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1723 if (IsExpanded(m_current
))
1725 Collapse(m_current
);
1737 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1738 event
.m_item
= m_current
;
1740 event
.SetEventObject( this );
1741 GetEventHandler()->ProcessEvent( event
);
1745 // up goes to the previous sibling or to the last of its children if
1749 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1752 prev
= GetParent( m_key_current
);
1756 wxTreeItemId current
= m_key_current
;
1757 if (current
== GetFirstChild( prev
, cockie
))
1759 // otherwise we return to where we came from
1760 SelectItem( prev
, unselect_others
, extended_select
);
1761 m_key_current
=prev
.m_pItem
;
1762 EnsureVisible( prev
);
1769 while ( IsExpanded(prev
) && HasChildren(prev
) )
1771 wxTreeItemId child
= GetLastChild(prev
);
1778 SelectItem( prev
, unselect_others
, extended_select
);
1779 m_key_current
=prev
.m_pItem
;
1780 EnsureVisible( prev
);
1785 // left arrow goes to the parent
1788 wxTreeItemId prev
= GetParent( m_current
);
1791 EnsureVisible( prev
);
1792 SelectItem( prev
, unselect_others
, extended_select
);
1798 // this works the same as the down arrow except that we also expand the
1799 // item if it wasn't expanded yet
1805 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1808 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1809 SelectItem( child
, unselect_others
, extended_select
);
1810 m_key_current
=child
.m_pItem
;
1811 EnsureVisible( child
);
1815 wxTreeItemId next
= GetNextSibling( m_key_current
);
1819 wxTreeItemId current
= m_key_current
;
1820 while (current
&& !next
)
1822 current
= GetParent( current
);
1823 if (current
) next
= GetNextSibling( current
);
1829 SelectItem( next
, unselect_others
, extended_select
);
1830 m_key_current
=next
.m_pItem
;
1831 EnsureVisible( next
);
1837 // <End> selects the last visible tree item
1840 wxTreeItemId last
= GetRootItem();
1842 while ( last
.IsOk() && IsExpanded(last
) )
1844 wxTreeItemId lastChild
= GetLastChild(last
);
1846 // it may happen if the item was expanded but then all of
1847 // its children have been deleted - so IsExpanded() returned
1848 // TRUE, but GetLastChild() returned invalid item
1857 EnsureVisible( last
);
1858 SelectItem( last
, unselect_others
, extended_select
);
1863 // <Home> selects the root item
1866 wxTreeItemId prev
= GetRootItem();
1869 EnsureVisible( prev
);
1870 SelectItem( prev
, unselect_others
, extended_select
);
1880 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1882 // We have to call this here because the label in
1883 // question might just have been added and no screen
1884 // update taken place.
1885 if (m_dirty
) wxYield();
1887 wxClientDC
dc(this);
1889 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1890 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1895 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1896 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1897 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1898 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1900 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1905 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1907 if (!item
.IsOk()) return;
1909 m_currentEdit
= item
.m_pItem
;
1911 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1912 te
.m_item
= m_currentEdit
;
1913 te
.SetEventObject( this );
1914 GetEventHandler()->ProcessEvent( te
);
1916 if (!te
.IsAllowed()) return;
1918 // We have to call this here because the label in
1919 // question might just have been added and no screen
1920 // update taken place.
1921 if (m_dirty
) wxYield();
1923 wxString s
= m_currentEdit
->GetText();
1924 int x
= m_currentEdit
->GetX();
1925 int y
= m_currentEdit
->GetY();
1926 int w
= m_currentEdit
->GetWidth();
1927 int h
= m_currentEdit
->GetHeight();
1932 int image
= m_currentEdit
->GetCurrentImage();
1933 if ( image
!= NO_IMAGE
)
1935 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1939 w
-= image_w
+ 4; // I don't know why +4 is needed
1941 wxClientDC
dc(this);
1943 x
= dc
.LogicalToDeviceX( x
);
1944 y
= dc
.LogicalToDeviceY( y
);
1946 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1947 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1951 void wxTreeCtrl::OnRenameTimer()
1956 void wxTreeCtrl::OnRenameAccept()
1958 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1959 le
.m_item
= m_currentEdit
;
1960 le
.SetEventObject( this );
1961 le
.m_label
= m_renameRes
;
1962 GetEventHandler()->ProcessEvent( le
);
1964 if (!le
.IsAllowed()) return;
1966 SetItemText( m_currentEdit
, m_renameRes
);
1969 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1971 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1973 if ( !m_anchor
) return;
1975 wxClientDC
dc(this);
1977 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1978 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1981 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1982 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1984 if (event
.Dragging())
1986 if (m_dragCount
== 0)
1987 m_dragStart
= wxPoint(x
,y
);
1991 if (m_dragCount
!= 3) return;
1993 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1994 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1996 wxTreeEvent
nevent( command
, GetId() );
1997 nevent
.m_item
= m_current
;
1998 nevent
.SetEventObject(this);
1999 GetEventHandler()->ProcessEvent(nevent
);
2007 if (item
== NULL
) return; /* we hit the blank area */
2009 if (event
.RightDown()) {
2010 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
2013 nevent
.SetEventObject(this);
2014 GetEventHandler()->ProcessEvent(nevent
);
2018 if (event
.LeftUp() && (item
== m_current
) &&
2019 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2020 HasFlag(wxTR_EDIT_LABELS
) )
2022 m_renameTimer
->Start( 100, TRUE
);
2026 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2027 bool extended_select
=(event
.ShiftDown() && is_multiple
);
2028 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
2037 SelectItem(item
, unselect_others
, extended_select
);
2039 if (event
.LeftDClick())
2041 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2042 event
.m_item
= item
;
2044 event
.SetEventObject( this );
2045 GetEventHandler()->ProcessEvent( event
);
2049 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2051 /* after all changes have been done to the tree control,
2052 * we actually redraw the tree when everything is over */
2059 CalculatePositions();
2061 AdjustMyScrollbars();
2064 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2070 dc
.SetFont(m_boldFont
);
2072 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2075 // restore normal font
2076 dc
.SetFont( m_normalFont
);
2080 int image
= item
->GetCurrentImage();
2081 if ( image
!= NO_IMAGE
)
2083 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2087 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2089 if (total_h
<40) total_h
+=2; // at least 4 pixels
2090 else total_h
+=total_h
/10; // otherwise 10% extra spacing
2092 item
->SetHeight(total_h
);
2093 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2095 item
->SetWidth(image_w
+text_w
+2);
2098 // -----------------------------------------------------------------------------
2099 // for developper : y is now the top of the level
2100 // not the middle of it !
2101 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2103 int horizX
= level
*m_indent
;
2105 CalculateSize( item
, dc
);
2108 item
->SetX( horizX
+m_indent
+m_spacing
);
2110 y
+=GetLineHeight(item
);
2112 if ( !item
->IsExpanded() )
2114 // we dont need to calculate collapsed branches
2118 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2119 size_t n
, count
= children
.Count();
2120 for (n
= 0; n
< count
; ++n
)
2121 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2124 void wxTreeCtrl::CalculatePositions()
2126 if ( !m_anchor
) return;
2128 wxClientDC
dc(this);
2131 dc
.SetFont( m_normalFont
);
2133 dc
.SetPen( m_dottedPen
);
2134 //if(GetImageList() == NULL)
2135 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2138 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2141 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2143 wxClientDC
dc(this);
2148 GetClientSize( &cw
, &ch
);
2151 rect
.x
= dc
.LogicalToDeviceX( 0 );
2153 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2156 Refresh( TRUE
, &rect
);
2158 AdjustMyScrollbars();
2161 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2163 wxClientDC
dc(this);
2168 GetClientSize( &cw
, &ch
);
2171 rect
.x
= dc
.LogicalToDeviceX( 0 );
2172 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2174 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2176 Refresh( TRUE
, &rect
);