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
)
595 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
602 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
604 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
606 return item
.m_pItem
->GetChildrenCount(recursively
);
609 // -----------------------------------------------------------------------------
610 // functions to work with tree items
611 // -----------------------------------------------------------------------------
613 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
615 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
617 return item
.m_pItem
->GetText();
620 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
621 wxTreeItemIcon which
) const
623 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
625 return item
.m_pItem
->GetImage(which
);
628 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
630 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
632 return item
.m_pItem
->GetData();
635 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
637 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
640 wxGenericTreeItem
*pItem
= item
.m_pItem
;
641 pItem
->SetText(text
);
642 CalculateSize(pItem
, dc
);
646 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
648 wxTreeItemIcon which
)
650 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
652 wxGenericTreeItem
*pItem
= item
.m_pItem
;
653 pItem
->SetImage(image
, which
);
656 CalculateSize(pItem
, dc
);
660 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
662 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
664 item
.m_pItem
->SetData(data
);
667 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
669 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
671 wxGenericTreeItem
*pItem
= item
.m_pItem
;
672 pItem
->SetHasPlus(has
);
676 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
678 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
680 // avoid redrawing the tree if no real change
681 wxGenericTreeItem
*pItem
= item
.m_pItem
;
682 if ( pItem
->IsBold() != bold
)
684 pItem
->SetBold(bold
);
689 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
692 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
694 wxGenericTreeItem
*pItem
= item
.m_pItem
;
695 pItem
->Attr().SetTextColour(col
);
699 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
702 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
704 wxGenericTreeItem
*pItem
= item
.m_pItem
;
705 pItem
->Attr().SetBackgroundColour(col
);
709 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
711 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
713 wxGenericTreeItem
*pItem
= item
.m_pItem
;
714 pItem
->Attr().SetFont(font
);
718 // -----------------------------------------------------------------------------
719 // item status inquiries
720 // -----------------------------------------------------------------------------
722 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
724 wxFAIL_MSG(wxT("not implemented"));
729 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
731 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
733 return !item
.m_pItem
->GetChildren().IsEmpty();
736 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
738 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
740 return item
.m_pItem
->IsExpanded();
743 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
745 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
747 return item
.m_pItem
->IsSelected();
750 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
752 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
754 return item
.m_pItem
->IsBold();
757 // -----------------------------------------------------------------------------
759 // -----------------------------------------------------------------------------
761 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
763 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
765 return item
.m_pItem
->GetParent();
768 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
770 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
773 return GetNextChild(item
, cookie
);
776 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
778 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
780 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
781 if ( (size_t)cookie
< children
.Count() )
783 return children
.Item(cookie
++);
787 // there are no more of them
788 return wxTreeItemId();
792 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
794 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
796 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
797 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
800 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
802 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
804 wxGenericTreeItem
*i
= item
.m_pItem
;
805 wxGenericTreeItem
*parent
= i
->GetParent();
806 if ( parent
== NULL
)
808 // root item doesn't have any siblings
809 return wxTreeItemId();
812 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
813 int index
= siblings
.Index(i
);
814 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
816 size_t n
= (size_t)(index
+ 1);
817 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
820 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
822 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
824 wxGenericTreeItem
*i
= item
.m_pItem
;
825 wxGenericTreeItem
*parent
= i
->GetParent();
826 if ( parent
== NULL
)
828 // root item doesn't have any siblings
829 return wxTreeItemId();
832 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
833 int index
= siblings
.Index(i
);
834 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
836 return index
== 0 ? wxTreeItemId()
837 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
840 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
842 wxFAIL_MSG(wxT("not implemented"));
844 return wxTreeItemId();
847 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
849 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
851 wxFAIL_MSG(wxT("not implemented"));
853 return wxTreeItemId();
856 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
858 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
860 wxFAIL_MSG(wxT("not implemented"));
862 return wxTreeItemId();
865 // -----------------------------------------------------------------------------
867 // -----------------------------------------------------------------------------
869 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
871 const wxString
& text
,
872 int image
, int selImage
,
873 wxTreeItemData
*data
)
875 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
878 // should we give a warning here?
879 return AddRoot(text
, image
, selImage
, data
);
883 wxGenericTreeItem
*item
=
884 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
888 data
->m_pItem
= item
;
891 parent
->Insert( item
, previous
);
898 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
899 int image
, int selImage
,
900 wxTreeItemData
*data
)
902 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
905 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
906 image
, selImage
, data
);
909 data
->m_pItem
= m_anchor
;
912 if (!HasFlag(wxTR_MULTIPLE
))
914 m_current
= m_key_current
= m_anchor
;
915 m_current
->SetHilight( TRUE
);
919 AdjustMyScrollbars();
924 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
925 const wxString
& text
,
926 int image
, int selImage
,
927 wxTreeItemData
*data
)
929 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
932 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
933 const wxTreeItemId
& idPrevious
,
934 const wxString
& text
,
935 int image
, int selImage
,
936 wxTreeItemData
*data
)
938 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
941 // should we give a warning here?
942 return AddRoot(text
, image
, selImage
, data
);
945 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
946 wxASSERT_MSG( index
!= wxNOT_FOUND
,
947 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
949 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
952 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
954 const wxString
& text
,
955 int image
, int selImage
,
956 wxTreeItemData
*data
)
958 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
961 // should we give a warning here?
962 return AddRoot(text
, image
, selImage
, data
);
965 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
968 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
969 const wxString
& text
,
970 int image
, int selImage
,
971 wxTreeItemData
*data
)
973 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
976 // should we give a warning here?
977 return AddRoot(text
, image
, selImage
, data
);
980 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
981 image
, selImage
, data
);
984 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
986 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
988 event
.SetEventObject( this );
989 ProcessEvent( event
);
992 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
994 wxGenericTreeItem
*item
= itemId
.m_pItem
;
995 item
->DeleteChildren(this);
1000 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1002 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1003 wxGenericTreeItem
*parent
= item
->GetParent();
1007 parent
->GetChildren().Remove( item
); // remove by value
1010 item
->DeleteChildren(this);
1011 SendDeleteEvent(item
);
1017 void wxTreeCtrl::DeleteAllItems()
1021 m_anchor
->DeleteChildren(this);
1030 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1032 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1034 if ( !item
->HasPlus() )
1037 if ( item
->IsExpanded() )
1040 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1041 event
.m_item
= item
;
1042 event
.SetEventObject( this );
1044 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1045 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1047 // cancelled by program
1052 CalculatePositions();
1054 RefreshSubtree(item
);
1056 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1057 ProcessEvent( event
);
1060 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1062 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1064 if ( !item
->IsExpanded() )
1067 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1068 event
.m_item
= item
;
1069 event
.SetEventObject( this );
1070 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1072 // cancelled by program
1078 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1079 size_t count
= children
.Count();
1080 for ( size_t n
= 0; n
< count
; n
++ )
1082 Collapse(children
[n
]);
1085 CalculatePositions();
1087 RefreshSubtree(item
);
1089 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1090 ProcessEvent( event
);
1093 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1096 DeleteChildren(item
);
1099 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1101 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1103 if (item
->IsExpanded())
1109 void wxTreeCtrl::Unselect()
1113 m_current
->SetHilight( FALSE
);
1114 RefreshLine( m_current
);
1118 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1120 if (item
->IsSelected())
1122 item
->SetHilight(FALSE
);
1126 if (item
->HasChildren())
1128 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1129 size_t count
= children
.Count();
1130 for ( size_t n
= 0; n
< count
; ++n
)
1132 UnselectAllChildren(children
[n
]);
1137 void wxTreeCtrl::UnselectAll()
1139 UnselectAllChildren(GetRootItem().m_pItem
);
1142 // Recursive function !
1143 // To stop we must have crt_item<last_item
1145 // Tag all next children, when no more children,
1146 // Move to parent (not to tag)
1147 // Keep going... if we found last_item, we stop.
1148 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1150 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1152 if (parent
== NULL
) // This is root item
1153 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1155 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1156 int index
= children
.Index(crt_item
);
1157 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1159 size_t count
= children
.Count();
1160 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1162 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1165 return TagNextChildren(parent
, last_item
, select
);
1168 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1170 crt_item
->SetHilight(select
);
1171 RefreshLine(crt_item
);
1173 if (crt_item
==last_item
)
1176 if (crt_item
->HasChildren())
1178 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1179 size_t count
= children
.Count();
1180 for ( size_t n
= 0; n
< count
; ++n
)
1182 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1190 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1192 // item2 is not necessary after item1
1193 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1195 // choice first' and 'last' between item1 and item2
1196 if (item1
->GetY()<item2
->GetY())
1207 bool select
= m_current
->IsSelected();
1209 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1212 TagNextChildren(first
,last
,select
);
1215 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1216 bool unselect_others
,
1217 bool extended_select
)
1219 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1221 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1222 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1224 //wxCHECK_RET( ( (!unselect_others) && is_single),
1225 // wxT("this is a single selection tree") );
1227 // to keep going anyhow !!!
1230 if (item
->IsSelected())
1231 return; // nothing to do
1232 unselect_others
= TRUE
;
1233 extended_select
= FALSE
;
1235 else if ( unselect_others
&& item
->IsSelected() )
1237 // selection change if there is more than one item currently selected
1238 wxArrayTreeItemIds selected_items
;
1239 if ( GetSelections(selected_items
) == 1 )
1243 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1244 event
.m_item
= item
;
1245 event
.m_itemOld
= m_current
;
1246 event
.SetEventObject( this );
1247 // TODO : Here we don't send any selection mode yet !
1249 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1253 if (unselect_others
)
1255 if (is_single
) Unselect(); // to speed up thing
1260 if (extended_select
)
1262 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1263 // don't change the mark (m_current)
1264 SelectItemRange(m_current
, item
);
1268 bool select
=TRUE
; // the default
1270 // Check if we need to toggle hilight (ctrl mode)
1271 if (!unselect_others
)
1272 select
=!item
->IsSelected();
1274 m_current
= m_key_current
= item
;
1275 m_current
->SetHilight(select
);
1276 RefreshLine( m_current
);
1279 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1280 GetEventHandler()->ProcessEvent( event
);
1283 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1284 wxArrayTreeItemIds
&array
) const
1286 if ( item
->IsSelected() )
1287 array
.Add(wxTreeItemId(item
));
1289 if ( item
->HasChildren() )
1291 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1292 size_t count
= children
.GetCount();
1293 for ( size_t n
= 0; n
< count
; ++n
)
1294 FillArray(children
[n
],array
);
1298 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1301 FillArray(GetRootItem().m_pItem
, array
);
1303 return array
.Count();
1306 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1308 if (!item
.IsOk()) return;
1310 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1312 // first expand all parent branches
1313 wxGenericTreeItem
*parent
= gitem
->GetParent();
1317 parent
= parent
->GetParent();
1320 //if (parent) CalculatePositions();
1325 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1327 if (!item
.IsOk()) return;
1329 // We have to call this here because the label in
1330 // question might just have been added and no screen
1331 // update taken place.
1332 if (m_dirty
) wxYield();
1334 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1336 // now scroll to the item
1337 int item_y
= gitem
->GetY();
1341 ViewStart( &start_x
, &start_y
);
1342 start_y
*= PIXELS_PER_UNIT
;
1346 GetClientSize( &client_w
, &client_h
);
1348 if (item_y
< start_y
+3)
1353 m_anchor
->GetSize( x
, y
, this );
1354 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1355 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1356 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1357 // Item should appear at top
1358 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1360 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1365 m_anchor
->GetSize( x
, y
, this );
1366 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1367 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1368 item_y
+= PIXELS_PER_UNIT
+2;
1369 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1370 // Item should appear at bottom
1371 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
);
1375 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1376 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1378 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1379 wxGenericTreeItem
**item2
)
1381 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1383 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1386 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1387 const wxTreeItemId
& item2
)
1389 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1392 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1394 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1396 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1398 wxCHECK_RET( !s_treeBeingSorted
,
1399 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1401 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1402 if ( children
.Count() > 1 )
1404 s_treeBeingSorted
= this;
1405 children
.Sort(tree_ctrl_compare_func
);
1406 s_treeBeingSorted
= NULL
;
1410 //else: don't make the tree dirty as nothing changed
1413 wxImageList
*wxTreeCtrl::GetImageList() const
1415 return m_imageListNormal
;
1418 wxImageList
*wxTreeCtrl::GetStateImageList() const
1420 return m_imageListState
;
1423 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1425 m_imageListNormal
= imageList
;
1427 // Calculate a m_lineHeight value from the image sizes.
1428 // May be toggle off. Then wxTreeCtrl will spread when
1429 // necessary (which might look ugly).
1431 wxClientDC
dc(this);
1432 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1433 int width
= 0, height
= 0,
1434 n
= m_imageListNormal
->GetImageCount();
1436 for (int i
= 0; i
< n
; i
++)
1438 m_imageListNormal
->GetSize(i
, width
, height
);
1439 if (height
> m_lineHeight
) m_lineHeight
= height
;
1442 if (m_lineHeight
< 40)
1443 m_lineHeight
+= 2; // at least 2 pixels
1445 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1449 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1451 m_imageListState
= imageList
;
1454 // -----------------------------------------------------------------------------
1456 // -----------------------------------------------------------------------------
1458 void wxTreeCtrl::AdjustMyScrollbars()
1464 m_anchor
->GetSize( x
, y
, this );
1465 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1466 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1467 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1468 int y_pos
= GetScrollPos( wxVERTICAL
);
1469 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1473 SetScrollbars( 0, 0, 0, 0 );
1477 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1479 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1480 return item
->GetHeight();
1482 return m_lineHeight
;
1485 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1487 wxTreeItemAttr
*attr
= item
->GetAttributes();
1488 if ( attr
&& attr
->HasFont() )
1489 dc
.SetFont(attr
->GetFont());
1490 else if (item
->IsBold())
1491 dc
.SetFont(m_boldFont
);
1495 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1499 int image
= item
->GetCurrentImage();
1500 if ( image
!= NO_IMAGE
)
1502 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1506 int total_h
= GetLineHeight(item
);
1508 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1510 if ( image
!= NO_IMAGE
)
1512 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1513 m_imageListNormal
->Draw( image
, dc
,
1515 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1516 wxIMAGELIST_DRAW_TRANSPARENT
);
1517 dc
.DestroyClippingRegion();
1520 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1521 dc
.SetBackgroundMode(hasBgCol
? wxSOLID
: wxTRANSPARENT
);
1523 dc
.SetTextBackground(attr
->GetBackgroundColour());
1524 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1525 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1527 // restore normal font
1528 dc
.SetFont( m_normalFont
);
1531 // Now y stands for the top of the item, whereas it used to stand for middle !
1532 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1534 int horizX
= level
*m_indent
;
1536 item
->SetX( horizX
+m_indent
+m_spacing
);
1540 y
+=GetLineHeight(item
)/2;
1542 item
->SetCross( horizX
+m_indent
, y
);
1544 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1545 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1547 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1549 int startX
= horizX
;
1550 int endX
= horizX
+ (m_indent
-5);
1552 // if (!item->HasChildren()) endX += (m_indent+5);
1553 if (!item
->HasChildren()) endX
+= 20;
1555 dc
.DrawLine( startX
, y
, endX
, y
);
1557 if (item
->HasPlus())
1559 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1560 dc
.SetPen( *wxGREY_PEN
);
1561 dc
.SetBrush( *wxWHITE_BRUSH
);
1562 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1564 dc
.SetPen( *wxBLACK_PEN
);
1565 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1566 if (!item
->IsExpanded())
1567 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1569 dc
.SetPen( m_dottedPen
);
1572 wxPen
*pen
= wxTRANSPARENT_PEN
;
1573 wxBrush
*brush
; // FIXME is this really needed?
1576 if ( item
->IsSelected() )
1578 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1580 brush
= m_hilightBrush
;
1588 wxTreeItemAttr
*attr
= item
->GetAttributes();
1589 if ( attr
&& attr
->HasTextColour() )
1590 colText
= attr
->GetTextColour();
1594 brush
= wxWHITE_BRUSH
;
1598 dc
.SetTextForeground(colText
);
1600 dc
.SetBrush(*brush
);
1603 PaintItem(item
, dc
);
1605 // restore DC objects
1606 dc
.SetBrush( *wxWHITE_BRUSH
);
1607 dc
.SetPen( m_dottedPen
);
1608 dc
.SetTextForeground( *wxBLACK
);
1611 y
= oldY
+GetLineHeight(item
);
1613 if (item
->IsExpanded())
1615 oldY
+=GetLineHeight(item
)/2;
1618 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1619 size_t n
, count
= children
.Count();
1620 for ( n
= 0; n
< count
; ++n
)
1623 PaintLevel( children
[n
], dc
, level
+1, y
);
1626 // it may happen that the item is expanded but has no items (when you
1627 // delete all its children for example) - don't draw the vertical line
1631 semiOldY
+=GetLineHeight(children
[--n
])/2;
1632 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1637 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1641 wxGenericTreeItem
*i
=item
.m_pItem
;
1643 wxClientDC
dc(this);
1645 dc
.SetLogicalFunction(wxINVERT
);
1648 ViewStart(&x
,&h
); // we only need x
1649 GetClientSize(&w
,&h
); // we only need w
1651 h
=GetLineHeight(i
)+1;
1652 // 2 white column at border
1653 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1656 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1660 wxGenericTreeItem
*i
=item
.m_pItem
;
1662 wxClientDC
dc(this);
1664 dc
.SetLogicalFunction(wxINVERT
);
1669 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1672 dc
.DrawLine( 0, y
, w
, y
);
1675 // -----------------------------------------------------------------------------
1676 // wxWindows callbacks
1677 // -----------------------------------------------------------------------------
1679 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1687 dc
.SetFont( m_normalFont
);
1688 dc
.SetPen( m_dottedPen
);
1690 // this is now done dynamically
1691 //if(GetImageList() == NULL)
1692 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1695 PaintLevel( m_anchor
, dc
, 0, y
);
1698 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1702 if (m_current
) RefreshLine( m_current
);
1705 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1709 if (m_current
) RefreshLine( m_current
);
1712 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1714 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1715 te
.m_code
= event
.KeyCode();
1716 te
.SetEventObject( this );
1717 GetEventHandler()->ProcessEvent( te
);
1719 if ( (m_current
== 0) || (m_key_current
== 0) )
1725 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1726 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1727 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1729 switch (event
.KeyCode())
1733 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1741 if (IsExpanded(m_current
))
1743 Collapse(m_current
);
1755 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1756 event
.m_item
= m_current
;
1758 event
.SetEventObject( this );
1759 GetEventHandler()->ProcessEvent( event
);
1763 // up goes to the previous sibling or to the last of its children if
1767 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1770 prev
= GetParent( m_key_current
);
1774 wxTreeItemId current
= m_key_current
;
1775 if (current
== GetFirstChild( prev
, cockie
))
1777 // otherwise we return to where we came from
1778 SelectItem( prev
, unselect_others
, extended_select
);
1779 m_key_current
=prev
.m_pItem
;
1780 EnsureVisible( prev
);
1787 while ( IsExpanded(prev
) && HasChildren(prev
) )
1789 wxTreeItemId child
= GetLastChild(prev
);
1796 SelectItem( prev
, unselect_others
, extended_select
);
1797 m_key_current
=prev
.m_pItem
;
1798 EnsureVisible( prev
);
1803 // left arrow goes to the parent
1806 wxTreeItemId prev
= GetParent( m_current
);
1809 EnsureVisible( prev
);
1810 SelectItem( prev
, unselect_others
, extended_select
);
1816 // this works the same as the down arrow except that we also expand the
1817 // item if it wasn't expanded yet
1823 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1826 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1827 SelectItem( child
, unselect_others
, extended_select
);
1828 m_key_current
=child
.m_pItem
;
1829 EnsureVisible( child
);
1833 wxTreeItemId next
= GetNextSibling( m_key_current
);
1837 wxTreeItemId current
= m_key_current
;
1838 while (current
&& !next
)
1840 current
= GetParent( current
);
1841 if (current
) next
= GetNextSibling( current
);
1847 SelectItem( next
, unselect_others
, extended_select
);
1848 m_key_current
=next
.m_pItem
;
1849 EnsureVisible( next
);
1855 // <End> selects the last visible tree item
1858 wxTreeItemId last
= GetRootItem();
1860 while ( last
.IsOk() && IsExpanded(last
) )
1862 wxTreeItemId lastChild
= GetLastChild(last
);
1864 // it may happen if the item was expanded but then all of
1865 // its children have been deleted - so IsExpanded() returned
1866 // TRUE, but GetLastChild() returned invalid item
1875 EnsureVisible( last
);
1876 SelectItem( last
, unselect_others
, extended_select
);
1881 // <Home> selects the root item
1884 wxTreeItemId prev
= GetRootItem();
1887 EnsureVisible( prev
);
1888 SelectItem( prev
, unselect_others
, extended_select
);
1898 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1900 // We have to call this here because the label in
1901 // question might just have been added and no screen
1902 // update taken place.
1903 if (m_dirty
) wxYield();
1905 wxClientDC
dc(this);
1907 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1908 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1913 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1914 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1915 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1916 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1918 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1923 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1925 if (!item
.IsOk()) return;
1927 m_currentEdit
= item
.m_pItem
;
1929 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1930 te
.m_item
= m_currentEdit
;
1931 te
.SetEventObject( this );
1932 GetEventHandler()->ProcessEvent( te
);
1934 if (!te
.IsAllowed()) return;
1936 // We have to call this here because the label in
1937 // question might just have been added and no screen
1938 // update taken place.
1939 if (m_dirty
) wxYield();
1941 wxString s
= m_currentEdit
->GetText();
1942 int x
= m_currentEdit
->GetX();
1943 int y
= m_currentEdit
->GetY();
1944 int w
= m_currentEdit
->GetWidth();
1945 int h
= m_currentEdit
->GetHeight();
1950 int image
= m_currentEdit
->GetCurrentImage();
1951 if ( image
!= NO_IMAGE
)
1953 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1957 w
-= image_w
+ 4; // I don't know why +4 is needed
1959 wxClientDC
dc(this);
1961 x
= dc
.LogicalToDeviceX( x
);
1962 y
= dc
.LogicalToDeviceY( y
);
1964 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1965 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1969 void wxTreeCtrl::OnRenameTimer()
1974 void wxTreeCtrl::OnRenameAccept()
1976 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1977 le
.m_item
= m_currentEdit
;
1978 le
.SetEventObject( this );
1979 le
.m_label
= m_renameRes
;
1980 GetEventHandler()->ProcessEvent( le
);
1982 if (!le
.IsAllowed()) return;
1984 SetItemText( m_currentEdit
, m_renameRes
);
1987 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1989 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1991 if ( !m_anchor
) return;
1993 wxClientDC
dc(this);
1995 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1996 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1999 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2000 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
2002 if (event
.Dragging())
2004 if (m_dragCount
== 0)
2005 m_dragStart
= wxPoint(x
,y
);
2009 if (m_dragCount
!= 3) return;
2011 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2012 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2014 wxTreeEvent
nevent( command
, GetId() );
2015 nevent
.m_item
= m_current
;
2016 nevent
.SetEventObject(this);
2017 GetEventHandler()->ProcessEvent(nevent
);
2025 if (item
== NULL
) return; /* we hit the blank area */
2027 if (event
.RightDown()) {
2028 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
2031 nevent
.SetEventObject(this);
2032 GetEventHandler()->ProcessEvent(nevent
);
2036 if (event
.LeftUp() && (item
== m_current
) &&
2037 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2038 HasFlag(wxTR_EDIT_LABELS
) )
2040 m_renameTimer
->Start( 100, TRUE
);
2044 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2045 bool extended_select
=(event
.ShiftDown() && is_multiple
);
2046 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
2055 SelectItem(item
, unselect_others
, extended_select
);
2057 if (event
.LeftDClick())
2059 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2060 event
.m_item
= item
;
2062 event
.SetEventObject( this );
2063 GetEventHandler()->ProcessEvent( event
);
2067 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2069 /* after all changes have been done to the tree control,
2070 * we actually redraw the tree when everything is over */
2077 CalculatePositions();
2079 AdjustMyScrollbars();
2082 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2088 dc
.SetFont(m_boldFont
);
2090 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2093 // restore normal font
2094 dc
.SetFont( m_normalFont
);
2098 int image
= item
->GetCurrentImage();
2099 if ( image
!= NO_IMAGE
)
2101 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2105 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2108 total_h
+= 2; // at least 2 pixels
2110 total_h
+= total_h
/10; // otherwise 10% extra spacing
2112 item
->SetHeight(total_h
);
2113 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2115 item
->SetWidth(image_w
+text_w
+2);
2118 // -----------------------------------------------------------------------------
2119 // for developper : y is now the top of the level
2120 // not the middle of it !
2121 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2123 int horizX
= level
*m_indent
;
2125 CalculateSize( item
, dc
);
2128 item
->SetX( horizX
+m_indent
+m_spacing
);
2130 y
+=GetLineHeight(item
);
2132 if ( !item
->IsExpanded() )
2134 // we dont need to calculate collapsed branches
2138 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2139 size_t n
, count
= children
.Count();
2140 for (n
= 0; n
< count
; ++n
)
2141 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2144 void wxTreeCtrl::CalculatePositions()
2146 if ( !m_anchor
) return;
2148 wxClientDC
dc(this);
2151 dc
.SetFont( m_normalFont
);
2153 dc
.SetPen( m_dottedPen
);
2154 //if(GetImageList() == NULL)
2155 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2158 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2161 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
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 wxClientDC
dc(this);
2188 GetClientSize( &cw
, &ch
);
2191 rect
.x
= dc
.LogicalToDeviceX( 0 );
2192 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2194 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2196 Refresh( TRUE
, &rect
);