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
;
457 return (wxGenericTreeItem
*) NULL
;
460 int wxGenericTreeItem::GetCurrentImage() const
462 int image
= NO_IMAGE
;
467 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
470 if ( image
== NO_IMAGE
)
472 // we usually fall back to the normal item, but try just the
473 // expanded one (and not selected) first in this case
474 image
= GetImage(wxTreeItemIcon_Expanded
);
480 image
= GetImage(wxTreeItemIcon_Selected
);
483 // may be it doesn't have the specific image we want, try the default one
485 if ( image
== NO_IMAGE
)
493 // -----------------------------------------------------------------------------
494 // wxTreeCtrl implementation
495 // -----------------------------------------------------------------------------
497 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
499 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
500 EVT_PAINT (wxTreeCtrl::OnPaint
)
501 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
502 EVT_CHAR (wxTreeCtrl::OnChar
)
503 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
504 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
505 EVT_IDLE (wxTreeCtrl::OnIdle
)
508 // -----------------------------------------------------------------------------
509 // construction/destruction
510 // -----------------------------------------------------------------------------
512 void wxTreeCtrl::Init()
516 m_anchor
= (wxGenericTreeItem
*) NULL
;
526 m_hilightBrush
= new wxBrush
528 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
533 m_imageListState
= (wxImageList
*) NULL
;
537 m_renameTimer
= new wxTreeRenameTimer( this );
539 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
540 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
541 m_normalFont
.GetFamily(),
542 m_normalFont
.GetStyle(),
544 m_normalFont
.GetUnderlined());
547 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
548 const wxPoint
& pos
, const wxSize
& size
,
551 const wxValidator
&validator
,
553 const wxString
& name
)
557 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
560 SetValidator( validator
);
563 SetBackgroundColour( *wxWHITE
);
564 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
565 m_dottedPen
= wxPen( "grey", 0, 0 );
570 wxTreeCtrl::~wxTreeCtrl()
572 wxDELETE( m_hilightBrush
);
576 delete m_renameTimer
;
579 // -----------------------------------------------------------------------------
581 // -----------------------------------------------------------------------------
583 size_t wxTreeCtrl::GetCount() const
585 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
588 void wxTreeCtrl::SetIndent(unsigned int indent
)
594 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
600 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
602 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
604 return item
.m_pItem
->GetChildrenCount(recursively
);
607 // -----------------------------------------------------------------------------
608 // functions to work with tree items
609 // -----------------------------------------------------------------------------
611 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
613 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
615 return item
.m_pItem
->GetText();
618 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
619 wxTreeItemIcon which
) const
621 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
623 return item
.m_pItem
->GetImage(which
);
626 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
628 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
630 return item
.m_pItem
->GetData();
633 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
635 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
638 wxGenericTreeItem
*pItem
= item
.m_pItem
;
639 pItem
->SetText(text
);
640 CalculateSize(pItem
, dc
);
644 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
646 wxTreeItemIcon which
)
648 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
650 wxGenericTreeItem
*pItem
= item
.m_pItem
;
651 pItem
->SetImage(image
, which
);
654 CalculateSize(pItem
, dc
);
658 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
660 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
662 item
.m_pItem
->SetData(data
);
665 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
667 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
669 wxGenericTreeItem
*pItem
= item
.m_pItem
;
670 pItem
->SetHasPlus(has
);
674 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
676 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
678 // avoid redrawing the tree if no real change
679 wxGenericTreeItem
*pItem
= item
.m_pItem
;
680 if ( pItem
->IsBold() != bold
)
682 pItem
->SetBold(bold
);
687 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
690 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
692 wxGenericTreeItem
*pItem
= item
.m_pItem
;
693 pItem
->Attr().SetTextColour(col
);
697 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
700 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
702 wxGenericTreeItem
*pItem
= item
.m_pItem
;
703 pItem
->Attr().SetBackgroundColour(col
);
707 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
709 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
711 wxGenericTreeItem
*pItem
= item
.m_pItem
;
712 pItem
->Attr().SetFont(font
);
716 // -----------------------------------------------------------------------------
717 // item status inquiries
718 // -----------------------------------------------------------------------------
720 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
722 wxFAIL_MSG(wxT("not implemented"));
727 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
729 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
731 return !item
.m_pItem
->GetChildren().IsEmpty();
734 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
736 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
738 return item
.m_pItem
->IsExpanded();
741 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
743 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
745 return item
.m_pItem
->IsSelected();
748 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
750 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
752 return item
.m_pItem
->IsBold();
755 // -----------------------------------------------------------------------------
757 // -----------------------------------------------------------------------------
759 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
761 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
763 return item
.m_pItem
->GetParent();
766 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
768 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
771 return GetNextChild(item
, cookie
);
774 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
776 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
778 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
779 if ( (size_t)cookie
< children
.Count() )
781 return children
.Item(cookie
++);
785 // there are no more of them
786 return wxTreeItemId();
790 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
792 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
794 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
795 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
798 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
800 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
802 wxGenericTreeItem
*i
= item
.m_pItem
;
803 wxGenericTreeItem
*parent
= i
->GetParent();
804 if ( parent
== NULL
)
806 // root item doesn't have any siblings
807 return wxTreeItemId();
810 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
811 int index
= siblings
.Index(i
);
812 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
814 size_t n
= (size_t)(index
+ 1);
815 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
818 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
820 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
822 wxGenericTreeItem
*i
= item
.m_pItem
;
823 wxGenericTreeItem
*parent
= i
->GetParent();
824 if ( parent
== NULL
)
826 // root item doesn't have any siblings
827 return wxTreeItemId();
830 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
831 int index
= siblings
.Index(i
);
832 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
834 return index
== 0 ? wxTreeItemId()
835 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
838 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
840 wxFAIL_MSG(wxT("not implemented"));
842 return wxTreeItemId();
845 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
847 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
849 wxFAIL_MSG(wxT("not implemented"));
851 return wxTreeItemId();
854 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
856 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
858 wxFAIL_MSG(wxT("not implemented"));
860 return wxTreeItemId();
863 // -----------------------------------------------------------------------------
865 // -----------------------------------------------------------------------------
867 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
869 const wxString
& text
,
870 int image
, int selImage
,
871 wxTreeItemData
*data
)
873 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
876 // should we give a warning here?
877 return AddRoot(text
, image
, selImage
, data
);
881 wxGenericTreeItem
*item
=
882 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
886 data
->m_pItem
= item
;
889 parent
->Insert( item
, previous
);
896 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
897 int image
, int selImage
,
898 wxTreeItemData
*data
)
900 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
903 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
904 image
, selImage
, data
);
907 data
->m_pItem
= m_anchor
;
910 if (!HasFlag(wxTR_MULTIPLE
))
912 m_current
= m_key_current
= m_anchor
;
913 m_current
->SetHilight( TRUE
);
917 AdjustMyScrollbars();
922 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
923 const wxString
& text
,
924 int image
, int selImage
,
925 wxTreeItemData
*data
)
927 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
930 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
931 const wxTreeItemId
& idPrevious
,
932 const wxString
& text
,
933 int image
, int selImage
,
934 wxTreeItemData
*data
)
936 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
939 // should we give a warning here?
940 return AddRoot(text
, image
, selImage
, data
);
943 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
944 wxASSERT_MSG( index
!= wxNOT_FOUND
,
945 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
947 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
950 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
952 const wxString
& text
,
953 int image
, int selImage
,
954 wxTreeItemData
*data
)
956 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
959 // should we give a warning here?
960 return AddRoot(text
, image
, selImage
, data
);
963 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
966 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
967 const wxString
& text
,
968 int image
, int selImage
,
969 wxTreeItemData
*data
)
971 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
974 // should we give a warning here?
975 return AddRoot(text
, image
, selImage
, data
);
978 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
979 image
, selImage
, data
);
982 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
984 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
986 event
.SetEventObject( this );
987 ProcessEvent( event
);
990 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
992 wxGenericTreeItem
*item
= itemId
.m_pItem
;
993 item
->DeleteChildren(this);
998 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1000 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1001 wxGenericTreeItem
*parent
= item
->GetParent();
1005 parent
->GetChildren().Remove( item
); // remove by value
1008 item
->DeleteChildren(this);
1009 SendDeleteEvent(item
);
1015 void wxTreeCtrl::DeleteAllItems()
1019 m_anchor
->DeleteChildren(this);
1028 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1030 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1032 if ( !item
->HasPlus() )
1035 if ( item
->IsExpanded() )
1038 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1039 event
.m_item
= item
;
1040 event
.SetEventObject( this );
1042 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1043 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1045 // cancelled by program
1050 CalculatePositions();
1052 RefreshSubtree(item
);
1054 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1055 ProcessEvent( event
);
1058 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1060 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1062 if ( !item
->IsExpanded() )
1065 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1066 event
.m_item
= item
;
1067 event
.SetEventObject( this );
1068 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1070 // cancelled by program
1076 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1077 size_t count
= children
.Count();
1078 for ( size_t n
= 0; n
< count
; n
++ )
1080 Collapse(children
[n
]);
1083 CalculatePositions();
1085 RefreshSubtree(item
);
1087 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1088 ProcessEvent( event
);
1091 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1094 DeleteChildren(item
);
1097 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1099 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1101 if (item
->IsExpanded())
1107 void wxTreeCtrl::Unselect()
1111 m_current
->SetHilight( FALSE
);
1112 RefreshLine( m_current
);
1116 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1118 if (item
->IsSelected())
1120 item
->SetHilight(FALSE
);
1124 if (item
->HasChildren())
1126 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1127 size_t count
= children
.Count();
1128 for ( size_t n
= 0; n
< count
; ++n
)
1130 UnselectAllChildren(children
[n
]);
1135 void wxTreeCtrl::UnselectAll()
1137 UnselectAllChildren(GetRootItem().m_pItem
);
1140 // Recursive function !
1141 // To stop we must have crt_item<last_item
1143 // Tag all next children, when no more children,
1144 // Move to parent (not to tag)
1145 // Keep going... if we found last_item, we stop.
1146 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1148 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1150 if (parent
== NULL
) // This is root item
1151 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1153 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1154 int index
= children
.Index(crt_item
);
1155 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1157 size_t count
= children
.Count();
1158 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1160 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1163 return TagNextChildren(parent
, last_item
, select
);
1166 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1168 crt_item
->SetHilight(select
);
1169 RefreshLine(crt_item
);
1171 if (crt_item
==last_item
)
1174 if (crt_item
->HasChildren())
1176 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1177 size_t count
= children
.Count();
1178 for ( size_t n
= 0; n
< count
; ++n
)
1180 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1188 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1190 // item2 is not necessary after item1
1191 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1193 // choice first' and 'last' between item1 and item2
1194 if (item1
->GetY()<item2
->GetY())
1205 bool select
= m_current
->IsSelected();
1207 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1210 TagNextChildren(first
,last
,select
);
1213 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1214 bool unselect_others
,
1215 bool extended_select
)
1217 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1219 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1220 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1222 //wxCHECK_RET( ( (!unselect_others) && is_single),
1223 // wxT("this is a single selection tree") );
1225 // to keep going anyhow !!!
1228 if (item
->IsSelected())
1229 return; // nothing to do
1230 unselect_others
= TRUE
;
1231 extended_select
= FALSE
;
1233 else if ( unselect_others
&& item
->IsSelected() )
1235 // selection change if there is more than one item currently selected
1236 wxArrayTreeItemIds selected_items
;
1237 if ( GetSelections(selected_items
) == 1 )
1241 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1242 event
.m_item
= item
;
1243 event
.m_itemOld
= m_current
;
1244 event
.SetEventObject( this );
1245 // TODO : Here we don't send any selection mode yet !
1247 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1251 if (unselect_others
)
1253 if (is_single
) Unselect(); // to speed up thing
1258 if (extended_select
)
1260 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1261 // don't change the mark (m_current)
1262 SelectItemRange(m_current
, item
);
1266 bool select
=TRUE
; // the default
1268 // Check if we need to toggle hilight (ctrl mode)
1269 if (!unselect_others
)
1270 select
=!item
->IsSelected();
1272 m_current
= m_key_current
= item
;
1273 m_current
->SetHilight(select
);
1274 RefreshLine( m_current
);
1277 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1278 GetEventHandler()->ProcessEvent( event
);
1281 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1282 wxArrayTreeItemIds
&array
) const
1284 if ( item
->IsSelected() )
1285 array
.Add(wxTreeItemId(item
));
1287 if ( item
->HasChildren() )
1289 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1290 size_t count
= children
.GetCount();
1291 for ( size_t n
= 0; n
< count
; ++n
)
1292 FillArray(children
[n
],array
);
1296 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1299 FillArray(GetRootItem().m_pItem
, array
);
1301 return array
.Count();
1304 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1306 if (!item
.IsOk()) return;
1308 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1310 // first expand all parent branches
1311 wxGenericTreeItem
*parent
= gitem
->GetParent();
1315 parent
= parent
->GetParent();
1318 //if (parent) CalculatePositions();
1323 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1325 if (!item
.IsOk()) return;
1327 // We have to call this here because the label in
1328 // question might just have been added and no screen
1329 // update taken place.
1330 if (m_dirty
) wxYield();
1332 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1334 // now scroll to the item
1335 int item_y
= gitem
->GetY();
1339 ViewStart( &start_x
, &start_y
);
1340 start_y
*= PIXELS_PER_UNIT
;
1344 GetClientSize( &client_w
, &client_h
);
1346 if (item_y
< start_y
+3)
1351 m_anchor
->GetSize( x
, y
, this );
1352 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1353 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1354 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1355 // Item should appear at top
1356 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1358 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1363 m_anchor
->GetSize( x
, y
, this );
1364 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1365 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1366 item_y
+= PIXELS_PER_UNIT
+2;
1367 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1368 // Item should appear at bottom
1369 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
);
1373 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1374 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1376 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1377 wxGenericTreeItem
**item2
)
1379 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1381 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1384 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1385 const wxTreeItemId
& item2
)
1387 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1390 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1392 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1394 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1396 wxCHECK_RET( !s_treeBeingSorted
,
1397 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1399 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1400 if ( children
.Count() > 1 )
1402 s_treeBeingSorted
= this;
1403 children
.Sort(tree_ctrl_compare_func
);
1404 s_treeBeingSorted
= NULL
;
1408 //else: don't make the tree dirty as nothing changed
1411 wxImageList
*wxTreeCtrl::GetImageList() const
1413 return m_imageListNormal
;
1416 wxImageList
*wxTreeCtrl::GetStateImageList() const
1418 return m_imageListState
;
1421 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1423 m_imageListNormal
= imageList
;
1425 // Calculate a m_lineHeight value from the image sizes.
1426 // May be toggle off. Then wxTreeCtrl will spread when
1427 // necessary (which might look ugly).
1429 wxClientDC
dc(this);
1430 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1431 int width
= 0, height
= 0,
1432 n
= m_imageListNormal
->GetImageCount();
1434 for (int i
= 0; i
< n
; i
++)
1436 m_imageListNormal
->GetSize(i
, width
, height
);
1437 if (height
> m_lineHeight
) m_lineHeight
= height
;
1440 if (m_lineHeight
< 40)
1441 m_lineHeight
+= 2; // at least 2 pixels
1443 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1447 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1449 m_imageListState
= imageList
;
1452 // -----------------------------------------------------------------------------
1454 // -----------------------------------------------------------------------------
1456 void wxTreeCtrl::AdjustMyScrollbars()
1462 m_anchor
->GetSize( x
, y
, this );
1463 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1464 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1465 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1466 int y_pos
= GetScrollPos( wxVERTICAL
);
1467 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1471 SetScrollbars( 0, 0, 0, 0 );
1475 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1477 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1478 return item
->GetHeight();
1480 return m_lineHeight
;
1483 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1485 wxTreeItemAttr
*attr
= item
->GetAttributes();
1486 if ( attr
&& attr
->HasFont() )
1487 dc
.SetFont(attr
->GetFont());
1488 else if (item
->IsBold())
1489 dc
.SetFont(m_boldFont
);
1493 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1497 int image
= item
->GetCurrentImage();
1498 if ( image
!= NO_IMAGE
)
1500 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1504 int total_h
= GetLineHeight(item
);
1506 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1508 if ( image
!= NO_IMAGE
)
1510 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1511 m_imageListNormal
->Draw( image
, dc
,
1513 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1514 wxIMAGELIST_DRAW_TRANSPARENT
);
1515 dc
.DestroyClippingRegion();
1518 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1519 dc
.SetBackgroundMode(hasBgCol
? wxSOLID
: wxTRANSPARENT
);
1521 dc
.SetTextBackground(attr
->GetBackgroundColour());
1522 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1523 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1525 // restore normal font
1526 dc
.SetFont( m_normalFont
);
1529 // Now y stands for the top of the item, whereas it used to stand for middle !
1530 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1532 int horizX
= level
*m_indent
;
1534 item
->SetX( horizX
+m_indent
+m_spacing
);
1538 y
+=GetLineHeight(item
)/2;
1540 item
->SetCross( horizX
+m_indent
, y
);
1542 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1543 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1545 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1547 int startX
= horizX
;
1548 int endX
= horizX
+ (m_indent
-5);
1550 // if (!item->HasChildren()) endX += (m_indent+5);
1551 if (!item
->HasChildren()) endX
+= 20;
1553 dc
.DrawLine( startX
, y
, endX
, y
);
1555 if (item
->HasPlus())
1557 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1558 dc
.SetPen( *wxGREY_PEN
);
1559 dc
.SetBrush( *wxWHITE_BRUSH
);
1560 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1562 dc
.SetPen( *wxBLACK_PEN
);
1563 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1564 if (!item
->IsExpanded())
1565 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1567 dc
.SetPen( m_dottedPen
);
1570 wxPen
*pen
= wxTRANSPARENT_PEN
;
1571 wxBrush
*brush
; // FIXME is this really needed?
1574 if ( item
->IsSelected() )
1576 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1578 brush
= m_hilightBrush
;
1586 wxTreeItemAttr
*attr
= item
->GetAttributes();
1587 if ( attr
&& attr
->HasTextColour() )
1588 colText
= attr
->GetTextColour();
1592 brush
= wxWHITE_BRUSH
;
1596 dc
.SetTextForeground(colText
);
1598 dc
.SetBrush(*brush
);
1601 PaintItem(item
, dc
);
1603 // restore DC objects
1604 dc
.SetBrush( *wxWHITE_BRUSH
);
1605 dc
.SetPen( m_dottedPen
);
1606 dc
.SetTextForeground( *wxBLACK
);
1609 y
= oldY
+GetLineHeight(item
);
1611 if (item
->IsExpanded())
1613 oldY
+=GetLineHeight(item
)/2;
1616 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1617 size_t n
, count
= children
.Count();
1618 for ( n
= 0; n
< count
; ++n
)
1621 PaintLevel( children
[n
], dc
, level
+1, y
);
1624 // it may happen that the item is expanded but has no items (when you
1625 // delete all its children for example) - don't draw the vertical line
1629 semiOldY
+=GetLineHeight(children
[--n
])/2;
1630 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1635 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1639 wxGenericTreeItem
*i
=item
.m_pItem
;
1641 wxClientDC
dc(this);
1643 dc
.SetLogicalFunction(wxINVERT
);
1646 ViewStart(&x
,&h
); // we only need x
1647 GetClientSize(&w
,&h
); // we only need w
1649 h
=GetLineHeight(i
)+1;
1650 // 2 white column at border
1651 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1654 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1658 wxGenericTreeItem
*i
=item
.m_pItem
;
1660 wxClientDC
dc(this);
1662 dc
.SetLogicalFunction(wxINVERT
);
1667 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1670 dc
.DrawLine( 0, y
, w
, y
);
1673 // -----------------------------------------------------------------------------
1674 // wxWindows callbacks
1675 // -----------------------------------------------------------------------------
1677 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1685 dc
.SetFont( m_normalFont
);
1686 dc
.SetPen( m_dottedPen
);
1688 // this is now done dynamically
1689 //if(GetImageList() == NULL)
1690 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1693 PaintLevel( m_anchor
, dc
, 0, y
);
1696 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1700 if (m_current
) RefreshLine( m_current
);
1703 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1707 if (m_current
) RefreshLine( m_current
);
1710 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1712 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1713 te
.m_code
= event
.KeyCode();
1714 te
.SetEventObject( this );
1715 GetEventHandler()->ProcessEvent( te
);
1717 if ( (m_current
== 0) || (m_key_current
== 0) )
1723 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1724 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1725 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1727 switch (event
.KeyCode())
1731 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1739 if (IsExpanded(m_current
))
1741 Collapse(m_current
);
1753 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1754 event
.m_item
= m_current
;
1756 event
.SetEventObject( this );
1757 GetEventHandler()->ProcessEvent( event
);
1761 // up goes to the previous sibling or to the last of its children if
1765 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1768 prev
= GetParent( m_key_current
);
1772 wxTreeItemId current
= m_key_current
;
1773 if (current
== GetFirstChild( prev
, cockie
))
1775 // otherwise we return to where we came from
1776 SelectItem( prev
, unselect_others
, extended_select
);
1777 m_key_current
=prev
.m_pItem
;
1778 EnsureVisible( prev
);
1785 while ( IsExpanded(prev
) && HasChildren(prev
) )
1787 wxTreeItemId child
= GetLastChild(prev
);
1794 SelectItem( prev
, unselect_others
, extended_select
);
1795 m_key_current
=prev
.m_pItem
;
1796 EnsureVisible( prev
);
1801 // left arrow goes to the parent
1804 wxTreeItemId prev
= GetParent( m_current
);
1807 EnsureVisible( prev
);
1808 SelectItem( prev
, unselect_others
, extended_select
);
1814 // this works the same as the down arrow except that we also expand the
1815 // item if it wasn't expanded yet
1821 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1824 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1825 SelectItem( child
, unselect_others
, extended_select
);
1826 m_key_current
=child
.m_pItem
;
1827 EnsureVisible( child
);
1831 wxTreeItemId next
= GetNextSibling( m_key_current
);
1835 wxTreeItemId current
= m_key_current
;
1836 while (current
&& !next
)
1838 current
= GetParent( current
);
1839 if (current
) next
= GetNextSibling( current
);
1845 SelectItem( next
, unselect_others
, extended_select
);
1846 m_key_current
=next
.m_pItem
;
1847 EnsureVisible( next
);
1853 // <End> selects the last visible tree item
1856 wxTreeItemId last
= GetRootItem();
1858 while ( last
.IsOk() && IsExpanded(last
) )
1860 wxTreeItemId lastChild
= GetLastChild(last
);
1862 // it may happen if the item was expanded but then all of
1863 // its children have been deleted - so IsExpanded() returned
1864 // TRUE, but GetLastChild() returned invalid item
1873 EnsureVisible( last
);
1874 SelectItem( last
, unselect_others
, extended_select
);
1879 // <Home> selects the root item
1882 wxTreeItemId prev
= GetRootItem();
1885 EnsureVisible( prev
);
1886 SelectItem( prev
, unselect_others
, extended_select
);
1896 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1898 // We have to call this here because the label in
1899 // question might just have been added and no screen
1900 // update taken place.
1901 if (m_dirty
) wxYield();
1903 wxClientDC
dc(this);
1905 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1906 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1911 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1912 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1913 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1914 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1916 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1921 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1923 if (!item
.IsOk()) return;
1925 m_currentEdit
= item
.m_pItem
;
1927 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1928 te
.m_item
= m_currentEdit
;
1929 te
.SetEventObject( this );
1930 GetEventHandler()->ProcessEvent( te
);
1932 if (!te
.IsAllowed()) return;
1934 // We have to call this here because the label in
1935 // question might just have been added and no screen
1936 // update taken place.
1937 if (m_dirty
) wxYield();
1939 wxString s
= m_currentEdit
->GetText();
1940 int x
= m_currentEdit
->GetX();
1941 int y
= m_currentEdit
->GetY();
1942 int w
= m_currentEdit
->GetWidth();
1943 int h
= m_currentEdit
->GetHeight();
1948 int image
= m_currentEdit
->GetCurrentImage();
1949 if ( image
!= NO_IMAGE
)
1951 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1955 w
-= image_w
+ 4; // I don't know why +4 is needed
1957 wxClientDC
dc(this);
1959 x
= dc
.LogicalToDeviceX( x
);
1960 y
= dc
.LogicalToDeviceY( y
);
1962 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1963 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1967 void wxTreeCtrl::OnRenameTimer()
1972 void wxTreeCtrl::OnRenameAccept()
1974 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1975 le
.m_item
= m_currentEdit
;
1976 le
.SetEventObject( this );
1977 le
.m_label
= m_renameRes
;
1978 GetEventHandler()->ProcessEvent( le
);
1980 if (!le
.IsAllowed()) return;
1982 SetItemText( m_currentEdit
, m_renameRes
);
1985 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1987 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1989 if ( !m_anchor
) return;
1991 wxClientDC
dc(this);
1993 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1994 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1997 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1998 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
2000 if (event
.Dragging())
2002 if (m_dragCount
== 0)
2003 m_dragStart
= wxPoint(x
,y
);
2007 if (m_dragCount
!= 3) return;
2009 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2010 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2012 wxTreeEvent
nevent( command
, GetId() );
2013 nevent
.m_item
= m_current
;
2014 nevent
.SetEventObject(this);
2015 GetEventHandler()->ProcessEvent(nevent
);
2023 if (item
== NULL
) return; /* we hit the blank area */
2025 if (event
.RightDown()) {
2026 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
2029 nevent
.SetEventObject(this);
2030 GetEventHandler()->ProcessEvent(nevent
);
2034 if (event
.LeftUp() && (item
== m_current
) &&
2035 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2036 HasFlag(wxTR_EDIT_LABELS
) )
2038 m_renameTimer
->Start( 100, TRUE
);
2042 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2043 bool extended_select
=(event
.ShiftDown() && is_multiple
);
2044 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
2053 SelectItem(item
, unselect_others
, extended_select
);
2055 if (event
.LeftDClick())
2057 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2058 event
.m_item
= item
;
2060 event
.SetEventObject( this );
2061 GetEventHandler()->ProcessEvent( event
);
2065 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2067 /* after all changes have been done to the tree control,
2068 * we actually redraw the tree when everything is over */
2075 CalculatePositions();
2077 AdjustMyScrollbars();
2080 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2086 dc
.SetFont(m_boldFont
);
2088 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2091 // restore normal font
2092 dc
.SetFont( m_normalFont
);
2096 int image
= item
->GetCurrentImage();
2097 if ( image
!= NO_IMAGE
)
2099 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2103 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2106 total_h
+= 2; // at least 2 pixels
2108 total_h
+= total_h
/10; // otherwise 10% extra spacing
2110 item
->SetHeight(total_h
);
2111 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2113 item
->SetWidth(image_w
+text_w
+2);
2116 // -----------------------------------------------------------------------------
2117 // for developper : y is now the top of the level
2118 // not the middle of it !
2119 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2121 int horizX
= level
*m_indent
;
2123 CalculateSize( item
, dc
);
2126 item
->SetX( horizX
+m_indent
+m_spacing
);
2128 y
+=GetLineHeight(item
);
2130 if ( !item
->IsExpanded() )
2132 // we dont need to calculate collapsed branches
2136 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2137 size_t n
, count
= children
.Count();
2138 for (n
= 0; n
< count
; ++n
)
2139 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2142 void wxTreeCtrl::CalculatePositions()
2144 if ( !m_anchor
) return;
2146 wxClientDC
dc(this);
2149 dc
.SetFont( m_normalFont
);
2151 dc
.SetPen( m_dottedPen
);
2152 //if(GetImageList() == NULL)
2153 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2156 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2159 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2161 if (m_dirty
) return;
2163 wxClientDC
dc(this);
2168 GetClientSize( &cw
, &ch
);
2171 rect
.x
= dc
.LogicalToDeviceX( 0 );
2173 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2176 Refresh( TRUE
, &rect
);
2178 AdjustMyScrollbars();
2181 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2183 if (m_dirty
) return;
2185 wxClientDC
dc(this);
2190 GetClientSize( &cw
, &ch
);
2193 rect
.x
= dc
.LogicalToDeviceX( 0 );
2194 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2196 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2198 Refresh( TRUE
, &rect
);