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 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
302 // TODO : Add the width of the image
303 // PB : We don't know which image is shown (image, selImage)
304 // We don't even know imageList from the treectrl this item belongs to !!!
305 // At this point m_width doesn't mean much, this can be remove !
308 wxGenericTreeItem::~wxGenericTreeItem()
314 wxASSERT_MSG( m_children
.IsEmpty(),
315 wxT("please call DeleteChildren() before deleting the item") );
318 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
320 size_t count
= m_children
.Count();
321 for ( size_t n
= 0; n
< count
; n
++ )
323 wxGenericTreeItem
*child
= m_children
[n
];
326 tree
->SendDeleteEvent(child
);
329 child
->DeleteChildren(tree
);
336 void wxGenericTreeItem::SetText( const wxString
&text
)
341 void wxGenericTreeItem::Reset()
344 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
346 m_images
[i
] = NO_IMAGE
;
351 m_height
= m_width
= 0;
358 m_isCollapsed
= TRUE
;
360 m_parent
= (wxGenericTreeItem
*)NULL
;
363 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
365 size_t count
= m_children
.Count();
369 size_t total
= count
;
370 for ( size_t n
= 0; n
< count
; ++n
)
372 total
+= m_children
[n
]->GetChildrenCount();
378 void wxGenericTreeItem::SetCross( int x
, int y
)
384 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
386 int bottomY
=m_y
+theTree
->GetLineHeight(this);
387 if ( y
< bottomY
) y
= bottomY
;
388 int width
= m_x
+ m_width
;
389 if ( x
< width
) x
= width
;
393 size_t count
= m_children
.Count();
394 for ( size_t n
= 0; n
< count
; ++n
)
396 m_children
[n
]->GetSize( x
, y
, theTree
);
401 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
402 const wxTreeCtrl
*theTree
,
405 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
407 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2)
408 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
410 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
412 // 5 is the size of the plus sign
413 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
414 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
415 (IsExpanded() || HasPlus()))
417 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
421 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
426 // assuming every image (normal and selected ) has the same size !
427 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
428 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
430 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
431 flags
|= wxTREE_HITTEST_ONITEMICON
;
433 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
439 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
440 if (point
.x
> m_x
+m_width
)
441 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
449 size_t count
= m_children
.Count();
450 for ( size_t n
= 0; n
< count
; n
++ )
452 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
459 flags
|=wxTREE_HITTEST_NOWHERE
;
463 int wxGenericTreeItem::GetCurrentImage() const
465 int image
= NO_IMAGE
;
470 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
473 if ( image
== NO_IMAGE
)
475 // we usually fall back to the normal item, but try just the
476 // expanded one (and not selected) first in this case
477 image
= GetImage(wxTreeItemIcon_Expanded
);
483 image
= GetImage(wxTreeItemIcon_Selected
);
486 // may be it doesn't have the specific image we want, try the default one
488 if ( image
== NO_IMAGE
)
496 // -----------------------------------------------------------------------------
497 // wxTreeCtrl implementation
498 // -----------------------------------------------------------------------------
500 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
502 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
503 EVT_PAINT (wxTreeCtrl::OnPaint
)
504 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
505 EVT_CHAR (wxTreeCtrl::OnChar
)
506 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
507 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
508 EVT_IDLE (wxTreeCtrl::OnIdle
)
511 // -----------------------------------------------------------------------------
512 // construction/destruction
513 // -----------------------------------------------------------------------------
515 void wxTreeCtrl::Init()
519 m_anchor
= (wxGenericTreeItem
*) NULL
;
529 m_hilightBrush
= new wxBrush
531 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
536 m_imageListState
= (wxImageList
*) NULL
;
540 m_renameTimer
= new wxTreeRenameTimer( this );
542 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
543 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
544 m_normalFont
.GetFamily(),
545 m_normalFont
.GetStyle(),
547 m_normalFont
.GetUnderlined());
550 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
551 const wxPoint
& pos
, const wxSize
& size
,
554 const wxValidator
&validator
,
556 const wxString
& name
)
560 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
563 SetValidator( validator
);
566 SetBackgroundColour( *wxWHITE
);
567 // m_dottedPen = wxPen( "grey", 0, wxDOT );
568 m_dottedPen
= wxPen( "grey", 0, 0 );
573 wxTreeCtrl::~wxTreeCtrl()
575 wxDELETE( m_hilightBrush
);
579 delete m_renameTimer
;
582 // -----------------------------------------------------------------------------
584 // -----------------------------------------------------------------------------
586 size_t wxTreeCtrl::GetCount() const
588 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
591 void wxTreeCtrl::SetIndent(unsigned int indent
)
598 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
605 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
607 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
609 return item
.m_pItem
->GetChildrenCount(recursively
);
612 // -----------------------------------------------------------------------------
613 // functions to work with tree items
614 // -----------------------------------------------------------------------------
616 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
618 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
620 return item
.m_pItem
->GetText();
623 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
624 wxTreeItemIcon which
) const
626 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
628 return item
.m_pItem
->GetImage(which
);
631 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
633 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
635 return item
.m_pItem
->GetData();
638 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
640 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
643 wxGenericTreeItem
*pItem
= item
.m_pItem
;
644 pItem
->SetText(text
);
645 CalculateSize(pItem
, dc
);
649 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
651 wxTreeItemIcon which
)
653 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
655 wxGenericTreeItem
*pItem
= item
.m_pItem
;
656 pItem
->SetImage(image
, which
);
659 CalculateSize(pItem
, dc
);
663 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
665 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
667 item
.m_pItem
->SetData(data
);
670 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
672 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
674 wxGenericTreeItem
*pItem
= item
.m_pItem
;
675 pItem
->SetHasPlus(has
);
679 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
681 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
683 // avoid redrawing the tree if no real change
684 wxGenericTreeItem
*pItem
= item
.m_pItem
;
685 if ( pItem
->IsBold() != bold
)
687 pItem
->SetBold(bold
);
692 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
695 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
697 wxGenericTreeItem
*pItem
= item
.m_pItem
;
698 pItem
->Attr().SetTextColour(col
);
702 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
705 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
707 wxGenericTreeItem
*pItem
= item
.m_pItem
;
708 pItem
->Attr().SetBackgroundColour(col
);
712 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
714 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
716 wxGenericTreeItem
*pItem
= item
.m_pItem
;
717 pItem
->Attr().SetFont(font
);
721 // -----------------------------------------------------------------------------
722 // item status inquiries
723 // -----------------------------------------------------------------------------
725 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
727 wxFAIL_MSG(wxT("not implemented"));
732 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
734 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
736 return !item
.m_pItem
->GetChildren().IsEmpty();
739 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
741 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
743 return item
.m_pItem
->IsExpanded();
746 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
748 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
750 return item
.m_pItem
->IsSelected();
753 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
755 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
757 return item
.m_pItem
->IsBold();
760 // -----------------------------------------------------------------------------
762 // -----------------------------------------------------------------------------
764 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
766 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
768 return item
.m_pItem
->GetParent();
771 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
773 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
776 return GetNextChild(item
, cookie
);
779 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
781 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
783 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
784 if ( (size_t)cookie
< children
.Count() )
786 return children
.Item(cookie
++);
790 // there are no more of them
791 return wxTreeItemId();
795 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
797 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
799 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
800 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
803 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
805 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
807 wxGenericTreeItem
*i
= item
.m_pItem
;
808 wxGenericTreeItem
*parent
= i
->GetParent();
809 if ( parent
== NULL
)
811 // root item doesn't have any siblings
812 return wxTreeItemId();
815 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
816 int index
= siblings
.Index(i
);
817 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
819 size_t n
= (size_t)(index
+ 1);
820 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
823 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
825 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
827 wxGenericTreeItem
*i
= item
.m_pItem
;
828 wxGenericTreeItem
*parent
= i
->GetParent();
829 if ( parent
== NULL
)
831 // root item doesn't have any siblings
832 return wxTreeItemId();
835 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
836 int index
= siblings
.Index(i
);
837 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
839 return index
== 0 ? wxTreeItemId()
840 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
843 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
845 wxFAIL_MSG(wxT("not implemented"));
847 return wxTreeItemId();
850 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
852 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
854 wxFAIL_MSG(wxT("not implemented"));
856 return wxTreeItemId();
859 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
861 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
863 wxFAIL_MSG(wxT("not implemented"));
865 return wxTreeItemId();
868 // -----------------------------------------------------------------------------
870 // -----------------------------------------------------------------------------
872 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
874 const wxString
& text
,
875 int image
, int selImage
,
876 wxTreeItemData
*data
)
878 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
881 // should we give a warning here?
882 return AddRoot(text
, image
, selImage
, data
);
886 wxGenericTreeItem
*item
=
887 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
891 data
->m_pItem
= item
;
894 parent
->Insert( item
, previous
);
901 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
902 int image
, int selImage
,
903 wxTreeItemData
*data
)
905 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
908 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
909 image
, selImage
, data
);
912 data
->m_pItem
= m_anchor
;
915 if (!HasFlag(wxTR_MULTIPLE
))
917 m_current
= m_key_current
= m_anchor
;
918 m_current
->SetHilight( TRUE
);
922 AdjustMyScrollbars();
927 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
928 const wxString
& text
,
929 int image
, int selImage
,
930 wxTreeItemData
*data
)
932 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
935 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
936 const wxTreeItemId
& idPrevious
,
937 const wxString
& text
,
938 int image
, int selImage
,
939 wxTreeItemData
*data
)
941 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
944 // should we give a warning here?
945 return AddRoot(text
, image
, selImage
, data
);
948 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
949 wxASSERT_MSG( index
!= wxNOT_FOUND
,
950 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
951 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
954 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
955 const wxString
& text
,
956 int image
, int selImage
,
957 wxTreeItemData
*data
)
959 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
962 // should we give a warning here?
963 return AddRoot(text
, image
, selImage
, data
);
966 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
967 image
, selImage
, data
);
970 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
972 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
974 event
.SetEventObject( this );
975 ProcessEvent( event
);
978 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
980 wxGenericTreeItem
*item
= itemId
.m_pItem
;
981 item
->DeleteChildren(this);
986 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
988 wxGenericTreeItem
*item
= itemId
.m_pItem
;
989 wxGenericTreeItem
*parent
= item
->GetParent();
993 parent
->GetChildren().Remove( item
); // remove by value
996 item
->DeleteChildren(this);
997 SendDeleteEvent(item
);
1003 void wxTreeCtrl::DeleteAllItems()
1007 m_anchor
->DeleteChildren(this);
1016 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1018 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1020 if ( !item
->HasPlus() )
1023 if ( item
->IsExpanded() )
1026 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1027 event
.m_item
= item
;
1028 event
.SetEventObject( this );
1030 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1031 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1033 // cancelled by program
1038 CalculatePositions();
1040 RefreshSubtree(item
);
1042 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1043 ProcessEvent( event
);
1046 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1048 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1050 if ( !item
->IsExpanded() )
1053 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1054 event
.m_item
= item
;
1055 event
.SetEventObject( this );
1056 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1058 // cancelled by program
1064 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1065 size_t count
= children
.Count();
1066 for ( size_t n
= 0; n
< count
; n
++ )
1068 Collapse(children
[n
]);
1071 CalculatePositions();
1073 RefreshSubtree(item
);
1075 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1076 ProcessEvent( event
);
1079 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1082 DeleteChildren(item
);
1085 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1087 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1089 if ( item
->IsExpanded() )
1095 void wxTreeCtrl::Unselect()
1099 m_current
->SetHilight( FALSE
);
1100 RefreshLine( m_current
);
1104 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1106 item
->SetHilight(FALSE
);
1109 if (item
->HasChildren())
1111 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1112 size_t count
= children
.Count();
1113 for ( size_t n
= 0; n
< count
; ++n
)
1114 UnselectAllChildren(children
[n
]);
1118 void wxTreeCtrl::UnselectAll()
1120 UnselectAllChildren(GetRootItem().m_pItem
);
1123 // Recursive function !
1124 // To stop we must have crt_item<last_item
1126 // Tag all next children, when no more children,
1127 // Move to parent (not to tag)
1128 // Keep going... if we found last_item, we stop.
1129 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1131 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1133 if ( parent
== NULL
) // This is root item
1134 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1136 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1137 int index
= children
.Index(crt_item
);
1138 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1140 size_t count
= children
.Count();
1141 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1142 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1144 return TagNextChildren(parent
, last_item
, select
);
1147 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1149 crt_item
->SetHilight(select
);
1150 RefreshLine(crt_item
);
1152 if (crt_item
==last_item
) return TRUE
;
1154 if (crt_item
->HasChildren())
1156 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1157 size_t count
= children
.Count();
1158 for ( size_t n
= 0; n
< count
; ++n
)
1159 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1165 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1167 // item2 is not necessary after item1
1168 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1170 // choice first' and 'last' between item1 and item2
1171 if (item1
->GetY()<item2
->GetY())
1182 bool select
= m_current
->IsSelected();
1184 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1187 TagNextChildren(first
,last
,select
);
1190 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1191 bool unselect_others
,
1192 bool extended_select
)
1194 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1196 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1197 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1199 //wxCHECK_RET( ( (!unselect_others) && is_single),
1200 // wxT("this is a single selection tree") );
1202 // to keep going anyhow !!!
1205 if (item
->IsSelected())
1206 return; // nothing to do
1207 unselect_others
= TRUE
;
1208 extended_select
= FALSE
;
1210 else if ( unselect_others
&& item
->IsSelected() )
1212 // selection change if there is more than one item currently selected
1213 wxArrayTreeItemIds selected_items
;
1214 if ( GetSelections(selected_items
) == 1 )
1218 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1219 event
.m_item
= item
;
1220 event
.m_itemOld
= m_current
;
1221 event
.SetEventObject( this );
1222 // TODO : Here we don't send any selection mode yet !
1224 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1228 if (unselect_others
)
1230 if (is_single
) Unselect(); // to speed up thing
1235 if (extended_select
)
1237 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1238 // don't change the mark (m_current)
1239 SelectItemRange(m_current
, item
);
1243 bool select
=TRUE
; // the default
1245 // Check if we need to toggle hilight (ctrl mode)
1246 if (!unselect_others
)
1247 select
=!item
->IsSelected();
1249 m_current
= m_key_current
= item
;
1250 m_current
->SetHilight(select
);
1251 RefreshLine( m_current
);
1254 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1255 GetEventHandler()->ProcessEvent( event
);
1258 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1259 wxArrayTreeItemIds
&array
) const
1261 if ( item
->IsSelected() )
1262 array
.Add(wxTreeItemId(item
));
1264 if ( item
->HasChildren() )
1266 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1267 size_t count
= children
.GetCount();
1268 for ( size_t n
= 0; n
< count
; ++n
)
1269 FillArray(children
[n
],array
);
1273 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1276 FillArray(GetRootItem().m_pItem
, array
);
1278 return array
.Count();
1281 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1283 if (!item
.IsOk()) return;
1285 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1287 // first expand all parent branches
1288 wxGenericTreeItem
*parent
= gitem
->GetParent();
1292 parent
= parent
->GetParent();
1295 //if (parent) CalculatePositions();
1300 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1302 if (!item
.IsOk()) return;
1304 // We have to call this here because the label in
1305 // question might just have been added and no screen
1306 // update taken place.
1307 if (m_dirty
) wxYield();
1309 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1311 // now scroll to the item
1312 int item_y
= gitem
->GetY();
1316 ViewStart( &start_x
, &start_y
);
1317 start_y
*= PIXELS_PER_UNIT
;
1321 GetClientSize( &client_w
, &client_h
);
1323 if (item_y
< start_y
+3)
1328 m_anchor
->GetSize( x
, y
, this );
1329 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1330 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1331 // Item should appear at top
1332 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1334 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1339 m_anchor
->GetSize( x
, y
, this );
1340 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1341 item_y
+= PIXELS_PER_UNIT
+2;
1342 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1343 // Item should appear at bottom
1344 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
);
1348 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1349 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1351 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1352 wxGenericTreeItem
**item2
)
1354 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1356 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1359 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1360 const wxTreeItemId
& item2
)
1362 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1365 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1367 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1369 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1371 wxCHECK_RET( !s_treeBeingSorted
,
1372 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1374 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1375 if ( children
.Count() > 1 )
1377 s_treeBeingSorted
= this;
1378 children
.Sort(tree_ctrl_compare_func
);
1379 s_treeBeingSorted
= NULL
;
1383 //else: don't make the tree dirty as nothing changed
1386 wxImageList
*wxTreeCtrl::GetImageList() const
1388 return m_imageListNormal
;
1391 wxImageList
*wxTreeCtrl::GetStateImageList() const
1393 return m_imageListState
;
1396 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1398 m_imageListNormal
= imageList
;
1400 // Calculate a m_lineHeight value from the image sizes.
1401 // May be toggle off. Then wxTreeCtrl will spread when
1402 // necessary (which might look ugly).
1404 wxClientDC
dc(this);
1405 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1409 n
= m_imageListNormal
->GetImageCount();
1410 for(int i
= 0; i
< n
; i
++)
1412 m_imageListNormal
->GetSize(i
, width
, height
);
1413 if(height
> m_lineHeight
) m_lineHeight
= height
;
1416 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1417 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1422 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1424 m_imageListState
= imageList
;
1427 // -----------------------------------------------------------------------------
1429 // -----------------------------------------------------------------------------
1431 void wxTreeCtrl::AdjustMyScrollbars()
1437 m_anchor
->GetSize( x
, y
, this );
1438 //y += GetLineHeight(m_anchor);
1439 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1440 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1441 int y_pos
= GetScrollPos( wxVERTICAL
);
1442 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1446 SetScrollbars( 0, 0, 0, 0 );
1450 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1452 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1453 return item
->GetHeight();
1455 return m_lineHeight
;
1458 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1460 wxTreeItemAttr
*attr
= item
->GetAttributes();
1461 if ( attr
&& attr
->HasFont() )
1462 dc
.SetFont(attr
->GetFont());
1463 else if (item
->IsBold())
1464 dc
.SetFont(m_boldFont
);
1468 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1472 int image
= item
->GetCurrentImage();
1473 if ( image
!= NO_IMAGE
)
1475 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1479 int total_h
= GetLineHeight(item
);
1481 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1483 if ( image
!= NO_IMAGE
)
1485 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1486 m_imageListNormal
->Draw( image
, dc
,
1488 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1489 wxIMAGELIST_DRAW_TRANSPARENT
);
1490 dc
.DestroyClippingRegion();
1493 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1494 dc
.SetBackgroundMode(hasBgCol
? wxSOLID
: wxTRANSPARENT
);
1496 dc
.SetTextBackground(attr
->GetBackgroundColour());
1497 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1498 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1500 // restore normal font
1501 dc
.SetFont( m_normalFont
);
1504 // Now y stands for the top of the item, whereas it used to stand for middle !
1505 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1507 int horizX
= level
*m_indent
;
1509 item
->SetX( horizX
+m_indent
+m_spacing
);
1513 y
+=GetLineHeight(item
)/2;
1515 item
->SetCross( horizX
+m_indent
, y
);
1517 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1518 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1520 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1522 int startX
= horizX
;
1523 int endX
= horizX
+ (m_indent
-5);
1525 // if (!item->HasChildren()) endX += (m_indent+5);
1526 if (!item
->HasChildren()) endX
+= 20;
1528 dc
.DrawLine( startX
, y
, endX
, y
);
1530 if (item
->HasPlus())
1532 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1533 dc
.SetPen( *wxGREY_PEN
);
1534 dc
.SetBrush( *wxWHITE_BRUSH
);
1535 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1537 dc
.SetPen( *wxBLACK_PEN
);
1538 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1539 if (!item
->IsExpanded())
1540 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1542 dc
.SetPen( m_dottedPen
);
1545 wxPen
*pen
= wxTRANSPARENT_PEN
;
1546 wxBrush
*brush
; // FIXME is this really needed?
1549 if ( item
->IsSelected() )
1551 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1553 brush
= m_hilightBrush
;
1561 wxTreeItemAttr
*attr
= item
->GetAttributes();
1562 if ( attr
&& attr
->HasTextColour() )
1563 colText
= attr
->GetTextColour();
1567 brush
= wxWHITE_BRUSH
;
1571 dc
.SetTextForeground(colText
);
1573 dc
.SetBrush(*brush
);
1576 PaintItem(item
, dc
);
1578 // restore DC objects
1579 dc
.SetBrush( *wxWHITE_BRUSH
);
1580 dc
.SetPen( m_dottedPen
);
1581 dc
.SetTextForeground( *wxBLACK
);
1584 y
= oldY
+GetLineHeight(item
);
1586 if (item
->IsExpanded())
1588 oldY
+=GetLineHeight(item
)/2;
1591 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1592 size_t n
, count
= children
.Count();
1593 for ( n
= 0; n
< count
; ++n
)
1596 PaintLevel( children
[n
], dc
, level
+1, y
);
1599 // it may happen that the item is expanded but has no items (when you
1600 // delete all its children for example) - don't draw the vertical line
1604 semiOldY
+=GetLineHeight(children
[--n
])/2;
1605 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1610 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1614 wxGenericTreeItem
*i
=item
.m_pItem
;
1616 wxClientDC
dc(this);
1618 dc
.SetLogicalFunction(wxINVERT
);
1621 ViewStart(&x
,&h
); // we only need x
1622 GetClientSize(&w
,&h
); // we only need w
1624 h
=GetLineHeight(i
)+1;
1625 // 2 white column at border
1626 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1629 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1633 wxGenericTreeItem
*i
=item
.m_pItem
;
1635 wxClientDC
dc(this);
1637 dc
.SetLogicalFunction(wxINVERT
);
1642 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1645 dc
.DrawLine( 0, y
, w
, y
);
1648 // -----------------------------------------------------------------------------
1649 // wxWindows callbacks
1650 // -----------------------------------------------------------------------------
1652 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1660 dc
.SetFont( m_normalFont
);
1661 dc
.SetPen( m_dottedPen
);
1663 // this is now done dynamically
1664 //if(GetImageList() == NULL)
1665 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1668 PaintLevel( m_anchor
, dc
, 0, y
);
1671 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1675 if (m_current
) RefreshLine( m_current
);
1678 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1682 if (m_current
) RefreshLine( m_current
);
1685 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1687 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1688 te
.m_code
= event
.KeyCode();
1689 te
.SetEventObject( this );
1690 GetEventHandler()->ProcessEvent( te
);
1692 if ( (m_current
== 0) || (m_key_current
== 0) )
1698 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1699 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1700 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1702 switch (event
.KeyCode())
1706 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1714 if (IsExpanded(m_current
))
1716 Collapse(m_current
);
1728 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1729 event
.m_item
= m_current
;
1731 event
.SetEventObject( this );
1732 GetEventHandler()->ProcessEvent( event
);
1736 // up goes to the previous sibling or to the last of its children if
1740 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1743 prev
= GetParent( m_key_current
);
1747 wxTreeItemId current
= m_key_current
;
1748 if (current
== GetFirstChild( prev
, cockie
))
1750 // otherwise we return to where we came from
1751 SelectItem( prev
, unselect_others
, extended_select
);
1752 m_key_current
=prev
.m_pItem
;
1753 EnsureVisible( prev
);
1760 while ( IsExpanded(prev
) && HasChildren(prev
) )
1762 wxTreeItemId child
= GetLastChild(prev
);
1769 SelectItem( prev
, unselect_others
, extended_select
);
1770 m_key_current
=prev
.m_pItem
;
1771 EnsureVisible( prev
);
1776 // left arrow goes to the parent
1779 wxTreeItemId prev
= GetParent( m_current
);
1782 EnsureVisible( prev
);
1783 SelectItem( prev
, unselect_others
, extended_select
);
1789 // this works the same as the down arrow except that we also expand the
1790 // item if it wasn't expanded yet
1796 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1799 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1800 SelectItem( child
, unselect_others
, extended_select
);
1801 m_key_current
=child
.m_pItem
;
1802 EnsureVisible( child
);
1806 wxTreeItemId next
= GetNextSibling( m_key_current
);
1810 wxTreeItemId current
= m_key_current
;
1811 while (current
&& !next
)
1813 current
= GetParent( current
);
1814 if (current
) next
= GetNextSibling( current
);
1820 SelectItem( next
, unselect_others
, extended_select
);
1821 m_key_current
=next
.m_pItem
;
1822 EnsureVisible( next
);
1828 // <End> selects the last visible tree item
1831 wxTreeItemId last
= GetRootItem();
1833 while ( last
.IsOk() && IsExpanded(last
) )
1835 wxTreeItemId lastChild
= GetLastChild(last
);
1837 // it may happen if the item was expanded but then all of
1838 // its children have been deleted - so IsExpanded() returned
1839 // TRUE, but GetLastChild() returned invalid item
1848 EnsureVisible( last
);
1849 SelectItem( last
, unselect_others
, extended_select
);
1854 // <Home> selects the root item
1857 wxTreeItemId prev
= GetRootItem();
1860 EnsureVisible( prev
);
1861 SelectItem( prev
, unselect_others
, extended_select
);
1871 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1873 // We have to call this here because the label in
1874 // question might just have been added and no screen
1875 // update taken place.
1876 if (m_dirty
) wxYield();
1878 wxClientDC
dc(this);
1880 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1881 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1886 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1887 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1888 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1889 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1891 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1896 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1898 if (!item
.IsOk()) return;
1900 m_currentEdit
= item
.m_pItem
;
1902 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1903 te
.m_item
= m_currentEdit
;
1904 te
.SetEventObject( this );
1905 GetEventHandler()->ProcessEvent( te
);
1907 if (!te
.IsAllowed()) return;
1909 // We have to call this here because the label in
1910 // question might just have been added and no screen
1911 // update taken place.
1912 if (m_dirty
) wxYield();
1914 wxString s
= m_currentEdit
->GetText();
1915 int x
= m_currentEdit
->GetX();
1916 int y
= m_currentEdit
->GetY();
1917 int w
= m_currentEdit
->GetWidth();
1918 int h
= m_currentEdit
->GetHeight();
1923 int image
= m_currentEdit
->GetCurrentImage();
1924 if ( image
!= NO_IMAGE
)
1926 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1930 w
-= image_w
+ 4; // I don't know why +4 is needed
1932 wxClientDC
dc(this);
1934 x
= dc
.LogicalToDeviceX( x
);
1935 y
= dc
.LogicalToDeviceY( y
);
1937 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1938 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1942 void wxTreeCtrl::OnRenameTimer()
1947 void wxTreeCtrl::OnRenameAccept()
1949 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1950 le
.m_item
= m_currentEdit
;
1951 le
.SetEventObject( this );
1952 le
.m_label
= m_renameRes
;
1953 GetEventHandler()->ProcessEvent( le
);
1955 if (!le
.IsAllowed()) return;
1957 SetItemText( m_currentEdit
, m_renameRes
);
1960 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1962 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1964 if ( !m_anchor
) return;
1966 wxClientDC
dc(this);
1968 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1969 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1972 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1973 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1975 if (event
.Dragging())
1977 if (m_dragCount
== 0)
1978 m_dragStart
= wxPoint(x
,y
);
1982 if (m_dragCount
!= 3) return;
1984 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1985 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1987 wxTreeEvent
nevent( command
, GetId() );
1988 nevent
.m_item
= m_current
;
1989 nevent
.SetEventObject(this);
1990 GetEventHandler()->ProcessEvent(nevent
);
1998 if (item
== NULL
) return; /* we hit the blank area */
2000 if (event
.RightDown()) {
2001 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
2004 nevent
.SetEventObject(this);
2005 GetEventHandler()->ProcessEvent(nevent
);
2009 if (event
.LeftUp() && (item
== m_current
) &&
2010 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2011 HasFlag(wxTR_EDIT_LABELS
) )
2013 m_renameTimer
->Start( 100, TRUE
);
2017 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2018 bool extended_select
=(event
.ShiftDown() && is_multiple
);
2019 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
2028 SelectItem(item
, unselect_others
, extended_select
);
2030 if (event
.LeftDClick())
2032 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2033 event
.m_item
= item
;
2035 event
.SetEventObject( this );
2036 GetEventHandler()->ProcessEvent( event
);
2040 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2042 /* after all changes have been done to the tree control,
2043 * we actually redraw the tree when everything is over */
2050 CalculatePositions();
2052 AdjustMyScrollbars();
2055 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2061 dc
.SetFont(m_boldFont
);
2063 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2066 // restore normal font
2067 dc
.SetFont( m_normalFont
);
2071 int image
= item
->GetCurrentImage();
2072 if ( image
!= NO_IMAGE
)
2074 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2078 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2080 if (total_h
<40) total_h
+=4; // at least 4 pixels
2081 else total_h
+=total_h
/10; // otherwise 10% extra spacing
2083 item
->SetHeight(total_h
);
2084 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2086 item
->SetWidth(image_w
+text_w
+2);
2089 // -----------------------------------------------------------------------------
2090 // for developper : y is now the top of the level
2091 // not the middle of it !
2092 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2094 int horizX
= level
*m_indent
;
2096 CalculateSize( item
, dc
);
2099 item
->SetX( horizX
+m_indent
+m_spacing
);
2101 y
+=GetLineHeight(item
);
2103 if ( !item
->IsExpanded() )
2105 // we dont need to calculate collapsed branches
2109 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2110 size_t n
, count
= children
.Count();
2111 for (n
= 0; n
< count
; ++n
)
2112 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2115 void wxTreeCtrl::CalculatePositions()
2117 if ( !m_anchor
) return;
2119 wxClientDC
dc(this);
2122 dc
.SetFont( m_normalFont
);
2124 dc
.SetPen( m_dottedPen
);
2125 //if(GetImageList() == NULL)
2126 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2129 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2132 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2134 wxClientDC
dc(this);
2139 GetClientSize( &cw
, &ch
);
2142 rect
.x
= dc
.LogicalToDeviceX( 0 );
2144 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2147 Refresh( TRUE
, &rect
);
2149 AdjustMyScrollbars();
2152 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2154 wxClientDC
dc(this);
2159 GetClientSize( &cw
, &ch
);
2162 rect
.x
= dc
.LogicalToDeviceX( 0 );
2163 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2165 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2167 Refresh( TRUE
, &rect
);