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 #define PIXELS_PER_UNIT 10
58 // -----------------------------------------------------------------------------
60 // -----------------------------------------------------------------------------
62 // timer used for enabling in-place edit
63 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
66 wxTreeRenameTimer( wxTreeCtrl
*owner
);
74 // control used for in-place edit
75 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
79 wxTreeTextCtrl( wxWindow
*parent
,
84 const wxString
&value
= wxEmptyString
,
85 const wxPoint
&pos
= wxDefaultPosition
,
86 const wxSize
&size
= wxDefaultSize
,
88 const wxValidator
& validator
= wxDefaultValidator
,
89 const wxString
&name
= wxTextCtrlNameStr
);
91 void OnChar( wxKeyEvent
&event
);
92 void OnKillFocus( wxFocusEvent
&event
);
98 wxString m_startValue
;
100 DECLARE_EVENT_TABLE()
101 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
105 class WXDLLEXPORT wxGenericTreeItem
109 wxGenericTreeItem() { m_data
= NULL
; }
110 wxGenericTreeItem( wxGenericTreeItem
*parent
,
111 const wxString
& text
,
113 int image
, int selImage
,
114 wxTreeItemData
*data
);
116 ~wxGenericTreeItem();
119 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
121 const wxString
& GetText() const { return m_text
; }
122 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
123 { return m_images
[which
]; }
124 wxTreeItemData
*GetData() const { return m_data
; }
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
130 void SetText( const wxString
&text
);
131 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
136 void SetBold(bool bold
) { m_isBold
= bold
; }
138 int GetX() const { return m_x
; }
139 int GetY() const { return m_y
; }
141 void SetX(int x
) { m_x
= x
; }
142 void SetY(int y
) { m_y
= y
; }
144 int GetHeight() const { return m_height
; }
145 int GetWidth() const { return m_width
; }
147 void SetHeight(int h
) { m_height
= h
; }
148 void SetWidth(int w
) { m_width
= w
; }
151 wxGenericTreeItem
*GetParent() const { return m_parent
; }
154 // deletes all children notifying the treectrl about it if !NULL
156 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
157 // FIXME don't know what is it for
160 // get count of all children (and grand children if 'recursively')
161 size_t GetChildrenCount(bool recursively
= TRUE
) const;
163 void Insert(wxGenericTreeItem
*child
, size_t index
)
164 { m_children
.Insert(child
, index
); }
166 void SetCross( int x
, int y
);
167 void GetSize( int &x
, int &y
, const wxTreeCtrl
* );
169 // return the item at given position (or NULL if no item), onButton is
170 // TRUE if the point belongs to the item's button, otherwise it lies
171 // on the button's label
172 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxTreeCtrl
*, int &flags
);
174 void Expand() { m_isCollapsed
= FALSE
; }
175 void Collapse() { m_isCollapsed
= TRUE
; }
177 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
180 bool HasChildren() const { return !m_children
.IsEmpty(); }
181 bool IsSelected() const { return m_hasHilight
; }
182 bool IsExpanded() const { return !m_isCollapsed
; }
183 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
184 bool IsBold() const { return m_isBold
; }
187 // get them - may be NULL
188 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
189 // get them ensuring that the pointer is not NULL
190 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
201 // tree ctrl images for the normal, selected, expanded and
202 // expanded+selected states
203 int m_images
[wxTreeItemIcon_Max
];
205 wxTreeItemData
*m_data
;
207 // use bitfields to save size
208 int m_isCollapsed
:1;
209 int m_hasHilight
:1; // same as focused
210 int m_hasPlus
:1; // used for item which doesn't have
211 // children but has a [+] button
212 int m_isBold
:1; // render the label in bold font
215 wxCoord m_height
, m_width
;
216 int m_xCross
, m_yCross
;
219 wxArrayGenericTreeItems m_children
;
220 wxGenericTreeItem
*m_parent
;
222 wxTreeItemAttr
*m_attr
;
225 // =============================================================================
227 // =============================================================================
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 // translate the key or mouse event flags to the type of selection we're
235 static void EventFlagsToSelType(long style
,
239 bool &extended_select
,
240 bool &unselect_others
)
242 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
243 extended_select
= shiftDown
&& is_multiple
;
244 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
247 // -----------------------------------------------------------------------------
248 // wxTreeRenameTimer (internal)
249 // -----------------------------------------------------------------------------
251 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl
*owner
)
256 void wxTreeRenameTimer::Notify()
258 m_owner
->OnRenameTimer();
261 //-----------------------------------------------------------------------------
262 // wxTreeTextCtrl (internal)
263 //-----------------------------------------------------------------------------
265 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
267 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
268 EVT_CHAR (wxTreeTextCtrl::OnChar
)
269 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
272 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
277 const wxString
&value
,
281 const wxValidator
& validator
,
282 const wxString
&name
)
283 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
289 (*m_res
) = wxEmptyString
;
290 m_startValue
= value
;
293 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
295 if (event
.m_keyCode
== WXK_RETURN
)
298 (*m_res
) = GetValue();
300 if (!wxPendingDelete
.Member(this))
301 wxPendingDelete
.Append(this);
303 if ((*m_accept
) && ((*m_res
) != m_startValue
))
304 m_owner
->OnRenameAccept();
308 if (event
.m_keyCode
== WXK_ESCAPE
)
313 if (!wxPendingDelete
.Member(this))
314 wxPendingDelete
.Append(this);
321 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
323 if (!wxPendingDelete
.Member(this))
324 wxPendingDelete
.Append(this);
326 if ((*m_accept
) && ((*m_res
) != m_startValue
))
327 m_owner
->OnRenameAccept();
330 // -----------------------------------------------------------------------------
332 // -----------------------------------------------------------------------------
334 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
336 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
337 : wxNotifyEvent( commandType
, id
)
340 m_itemOld
= (wxGenericTreeItem
*)NULL
;
343 // -----------------------------------------------------------------------------
345 // -----------------------------------------------------------------------------
347 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
348 const wxString
& text
,
350 int image
, int selImage
,
351 wxTreeItemData
*data
)
354 m_images
[wxTreeItemIcon_Normal
] = image
;
355 m_images
[wxTreeItemIcon_Selected
] = selImage
;
356 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
357 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
361 m_xCross
= m_yCross
= 0;
365 m_isCollapsed
= TRUE
;
366 m_hasHilight
= FALSE
;
372 m_attr
= (wxTreeItemAttr
*)NULL
;
374 // We don't know the height here yet.
379 wxGenericTreeItem::~wxGenericTreeItem()
385 wxASSERT_MSG( m_children
.IsEmpty(),
386 wxT("please call DeleteChildren() before deleting the item") );
389 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
391 size_t count
= m_children
.Count();
392 for ( size_t n
= 0; n
< count
; n
++ )
394 wxGenericTreeItem
*child
= m_children
[n
];
396 tree
->SendDeleteEvent(child
);
398 child
->DeleteChildren(tree
);
405 void wxGenericTreeItem::SetText( const wxString
&text
)
410 void wxGenericTreeItem::Reset()
413 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
415 m_images
[i
] = NO_IMAGE
;
420 m_height
= m_width
= 0;
427 m_isCollapsed
= TRUE
;
429 m_parent
= (wxGenericTreeItem
*)NULL
;
432 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
434 size_t count
= m_children
.Count();
438 size_t total
= count
;
439 for (size_t n
= 0; n
< count
; ++n
)
441 total
+= m_children
[n
]->GetChildrenCount();
447 void wxGenericTreeItem::SetCross( int x
, int y
)
453 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
455 int bottomY
=m_y
+theTree
->GetLineHeight(this);
456 if ( y
< bottomY
) y
= bottomY
;
457 int width
= m_x
+ m_width
;
458 if ( x
< width
) x
= width
;
462 size_t count
= m_children
.Count();
463 for ( size_t n
= 0; n
< count
; ++n
)
465 m_children
[n
]->GetSize( x
, y
, theTree
);
470 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
471 const wxTreeCtrl
*theTree
,
474 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
476 if (point
.y
< m_y
+theTree
->GetLineHeight(this)/2 )
477 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
479 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
481 // 5 is the size of the plus sign
482 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
483 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
484 (IsExpanded() || HasPlus()))
486 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
490 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
495 // assuming every image (normal and selected ) has the same size !
496 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
497 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
499 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
500 flags
|= wxTREE_HITTEST_ONITEMICON
;
502 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
508 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
509 if (point
.x
> m_x
+m_width
)
510 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
518 size_t count
= m_children
.Count();
519 for ( size_t n
= 0; n
< count
; n
++ )
521 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
528 flags
|=wxTREE_HITTEST_NOWHERE
;
530 return (wxGenericTreeItem
*) NULL
;
533 int wxGenericTreeItem::GetCurrentImage() const
535 int image
= NO_IMAGE
;
540 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
543 if ( image
== NO_IMAGE
)
545 // we usually fall back to the normal item, but try just the
546 // expanded one (and not selected) first in this case
547 image
= GetImage(wxTreeItemIcon_Expanded
);
553 image
= GetImage(wxTreeItemIcon_Selected
);
556 // may be it doesn't have the specific image we want, try the default one
558 if ( image
== NO_IMAGE
)
566 // -----------------------------------------------------------------------------
567 // wxTreeCtrl implementation
568 // -----------------------------------------------------------------------------
570 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
572 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
573 EVT_PAINT (wxTreeCtrl::OnPaint
)
574 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
575 EVT_CHAR (wxTreeCtrl::OnChar
)
576 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
577 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
578 EVT_IDLE (wxTreeCtrl::OnIdle
)
581 // -----------------------------------------------------------------------------
582 // construction/destruction
583 // -----------------------------------------------------------------------------
585 void wxTreeCtrl::Init()
589 m_anchor
= (wxGenericTreeItem
*) NULL
;
599 m_hilightBrush
= new wxBrush
601 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
606 m_imageListState
= (wxImageList
*) NULL
;
609 m_isDragging
= FALSE
;
611 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
613 m_renameTimer
= new wxTreeRenameTimer( this );
614 m_lastOnSame
= FALSE
;
616 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
617 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
618 m_normalFont
.GetFamily(),
619 m_normalFont
.GetStyle(),
621 m_normalFont
.GetUnderlined());
624 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
625 const wxPoint
& pos
, const wxSize
& size
,
627 const wxValidator
&validator
,
628 const wxString
& name
)
632 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
635 SetValidator( validator
);
638 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
639 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
640 m_dottedPen
= wxPen( "grey", 0, 0 );
645 wxTreeCtrl::~wxTreeCtrl()
647 wxDELETE( m_hilightBrush
);
651 delete m_renameTimer
;
654 // -----------------------------------------------------------------------------
656 // -----------------------------------------------------------------------------
658 size_t wxTreeCtrl::GetCount() const
660 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
663 void wxTreeCtrl::SetIndent(unsigned int indent
)
669 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
675 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
677 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
679 return item
.m_pItem
->GetChildrenCount(recursively
);
682 // -----------------------------------------------------------------------------
683 // functions to work with tree items
684 // -----------------------------------------------------------------------------
686 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
688 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
690 return item
.m_pItem
->GetText();
693 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
694 wxTreeItemIcon which
) const
696 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
698 return item
.m_pItem
->GetImage(which
);
701 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
703 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
705 return item
.m_pItem
->GetData();
708 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
710 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
713 wxGenericTreeItem
*pItem
= item
.m_pItem
;
714 pItem
->SetText(text
);
715 CalculateSize(pItem
, dc
);
719 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
721 wxTreeItemIcon which
)
723 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
725 wxGenericTreeItem
*pItem
= item
.m_pItem
;
726 pItem
->SetImage(image
, which
);
729 CalculateSize(pItem
, dc
);
733 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
735 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
737 item
.m_pItem
->SetData(data
);
740 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
742 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
744 wxGenericTreeItem
*pItem
= item
.m_pItem
;
745 pItem
->SetHasPlus(has
);
749 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
751 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
753 // avoid redrawing the tree if no real change
754 wxGenericTreeItem
*pItem
= item
.m_pItem
;
755 if ( pItem
->IsBold() != bold
)
757 pItem
->SetBold(bold
);
762 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
765 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
767 wxGenericTreeItem
*pItem
= item
.m_pItem
;
768 pItem
->Attr().SetTextColour(col
);
772 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
775 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
777 wxGenericTreeItem
*pItem
= item
.m_pItem
;
778 pItem
->Attr().SetBackgroundColour(col
);
782 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
784 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
786 wxGenericTreeItem
*pItem
= item
.m_pItem
;
787 pItem
->Attr().SetFont(font
);
791 // -----------------------------------------------------------------------------
792 // item status inquiries
793 // -----------------------------------------------------------------------------
795 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
797 wxFAIL_MSG(wxT("not implemented"));
802 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
804 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
806 return !item
.m_pItem
->GetChildren().IsEmpty();
809 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
811 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
813 return item
.m_pItem
->IsExpanded();
816 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
818 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
820 return item
.m_pItem
->IsSelected();
823 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
825 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
827 return item
.m_pItem
->IsBold();
830 // -----------------------------------------------------------------------------
832 // -----------------------------------------------------------------------------
834 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
836 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
838 return item
.m_pItem
->GetParent();
841 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
843 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
846 return GetNextChild(item
, cookie
);
849 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
851 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
853 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
854 if ( (size_t)cookie
< children
.Count() )
856 return children
.Item((size_t)cookie
++);
860 // there are no more of them
861 return wxTreeItemId();
865 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
867 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
869 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
870 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
873 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
875 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
877 wxGenericTreeItem
*i
= item
.m_pItem
;
878 wxGenericTreeItem
*parent
= i
->GetParent();
879 if ( parent
== NULL
)
881 // root item doesn't have any siblings
882 return wxTreeItemId();
885 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
886 int index
= siblings
.Index(i
);
887 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
889 size_t n
= (size_t)(index
+ 1);
890 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
893 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
895 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
897 wxGenericTreeItem
*i
= item
.m_pItem
;
898 wxGenericTreeItem
*parent
= i
->GetParent();
899 if ( parent
== NULL
)
901 // root item doesn't have any siblings
902 return wxTreeItemId();
905 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
906 int index
= siblings
.Index(i
);
907 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
909 return index
== 0 ? wxTreeItemId()
910 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
913 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
915 wxFAIL_MSG(wxT("not implemented"));
917 return wxTreeItemId();
920 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
922 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
924 wxFAIL_MSG(wxT("not implemented"));
926 return wxTreeItemId();
929 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
931 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
933 wxFAIL_MSG(wxT("not implemented"));
935 return wxTreeItemId();
938 // -----------------------------------------------------------------------------
940 // -----------------------------------------------------------------------------
942 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
944 const wxString
& text
,
945 int image
, int selImage
,
946 wxTreeItemData
*data
)
948 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
951 // should we give a warning here?
952 return AddRoot(text
, image
, selImage
, data
);
956 wxGenericTreeItem
*item
=
957 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
961 data
->m_pItem
= item
;
964 parent
->Insert( item
, previous
);
971 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
972 int image
, int selImage
,
973 wxTreeItemData
*data
)
975 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
978 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
979 image
, selImage
, data
);
982 data
->m_pItem
= m_anchor
;
985 if (!HasFlag(wxTR_MULTIPLE
))
987 m_current
= m_key_current
= m_anchor
;
988 m_current
->SetHilight( TRUE
);
996 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
997 const wxString
& text
,
998 int image
, int selImage
,
999 wxTreeItemData
*data
)
1001 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1004 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1005 const wxTreeItemId
& idPrevious
,
1006 const wxString
& text
,
1007 int image
, int selImage
,
1008 wxTreeItemData
*data
)
1010 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
1013 // should we give a warning here?
1014 return AddRoot(text
, image
, selImage
, data
);
1017 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
1018 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1019 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
1021 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1024 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1026 const wxString
& text
,
1027 int image
, int selImage
,
1028 wxTreeItemData
*data
)
1030 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
1033 // should we give a warning here?
1034 return AddRoot(text
, image
, selImage
, data
);
1037 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1040 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1041 const wxString
& text
,
1042 int image
, int selImage
,
1043 wxTreeItemData
*data
)
1045 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
1048 // should we give a warning here?
1049 return AddRoot(text
, image
, selImage
, data
);
1052 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1053 image
, selImage
, data
);
1056 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1058 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1059 event
.m_item
= item
;
1060 event
.SetEventObject( this );
1061 ProcessEvent( event
);
1064 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1066 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1067 item
->DeleteChildren(this);
1072 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1074 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1076 // don't stay with invalid m_key_current or we will crash in the next call
1078 bool changeKeyCurrent
= FALSE
;
1079 wxGenericTreeItem
*itemKey
= m_key_current
;
1080 while ( itemKey
&& !changeKeyCurrent
)
1082 if ( itemKey
== item
)
1084 // m_key_current is a descendant of the item being deleted
1085 changeKeyCurrent
= TRUE
;
1089 itemKey
= itemKey
->GetParent();
1093 wxGenericTreeItem
*parent
= item
->GetParent();
1096 parent
->GetChildren().Remove( item
); // remove by value
1099 if ( changeKeyCurrent
)
1101 // may be NULL or not
1102 m_key_current
= parent
;
1105 item
->DeleteChildren(this);
1106 SendDeleteEvent(item
);
1112 void wxTreeCtrl::DeleteAllItems()
1116 m_anchor
->DeleteChildren(this);
1125 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1127 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1129 wxCHECK_RET( item
, _T("invalid item in wxTreeCtrl::Expand") );
1131 if ( !item
->HasPlus() )
1134 if ( item
->IsExpanded() )
1137 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1138 event
.m_item
= item
;
1139 event
.SetEventObject( this );
1141 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1143 // cancelled by program
1148 CalculatePositions();
1150 RefreshSubtree(item
);
1152 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1153 ProcessEvent( event
);
1156 void wxTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1159 if ( IsExpanded(item
) )
1162 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1163 while ( child
.IsOk() )
1167 child
= GetNextChild(item
, cookie
);
1172 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1174 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1176 if ( !item
->IsExpanded() )
1179 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1180 event
.m_item
= item
;
1181 event
.SetEventObject( this );
1182 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1184 // cancelled by program
1190 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1191 size_t count
= children
.Count();
1192 for ( size_t n
= 0; n
< count
; n
++ )
1194 Collapse(children
[n
]);
1197 CalculatePositions();
1199 RefreshSubtree(item
);
1201 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1202 ProcessEvent( event
);
1205 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1208 DeleteChildren(item
);
1211 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1213 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1215 if (item
->IsExpanded())
1221 void wxTreeCtrl::Unselect()
1225 m_current
->SetHilight( FALSE
);
1226 RefreshLine( m_current
);
1230 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1232 if (item
->IsSelected())
1234 item
->SetHilight(FALSE
);
1238 if (item
->HasChildren())
1240 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1241 size_t count
= children
.Count();
1242 for ( size_t n
= 0; n
< count
; ++n
)
1244 UnselectAllChildren(children
[n
]);
1249 void wxTreeCtrl::UnselectAll()
1251 UnselectAllChildren(GetRootItem().m_pItem
);
1254 // Recursive function !
1255 // To stop we must have crt_item<last_item
1257 // Tag all next children, when no more children,
1258 // Move to parent (not to tag)
1259 // Keep going... if we found last_item, we stop.
1260 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1262 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1264 if (parent
== NULL
) // This is root item
1265 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1267 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1268 int index
= children
.Index(crt_item
);
1269 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1271 size_t count
= children
.Count();
1272 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1274 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1277 return TagNextChildren(parent
, last_item
, select
);
1280 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1282 crt_item
->SetHilight(select
);
1283 RefreshLine(crt_item
);
1285 if (crt_item
==last_item
)
1288 if (crt_item
->HasChildren())
1290 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1291 size_t count
= children
.Count();
1292 for ( size_t n
= 0; n
< count
; ++n
)
1294 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1302 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1304 // item2 is not necessary after item1
1305 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1307 // choice first' and 'last' between item1 and item2
1308 if (item1
->GetY()<item2
->GetY())
1319 bool select
= m_current
->IsSelected();
1321 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1324 TagNextChildren(first
,last
,select
);
1327 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1328 bool unselect_others
,
1329 bool extended_select
)
1331 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1333 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1334 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1336 //wxCHECK_RET( ( (!unselect_others) && is_single),
1337 // wxT("this is a single selection tree") );
1339 // to keep going anyhow !!!
1342 if (item
->IsSelected())
1343 return; // nothing to do
1344 unselect_others
= TRUE
;
1345 extended_select
= FALSE
;
1347 else if ( unselect_others
&& item
->IsSelected() )
1349 // selection change if there is more than one item currently selected
1350 wxArrayTreeItemIds selected_items
;
1351 if ( GetSelections(selected_items
) == 1 )
1355 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1356 event
.m_item
= item
;
1357 event
.m_itemOld
= m_current
;
1358 event
.SetEventObject( this );
1359 // TODO : Here we don't send any selection mode yet !
1361 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1365 if (unselect_others
)
1367 if (is_single
) Unselect(); // to speed up thing
1372 if (extended_select
)
1377 m_key_current
= GetRootItem().m_pItem
;
1380 // don't change the mark (m_current)
1381 SelectItemRange(m_current
, item
);
1385 bool select
=TRUE
; // the default
1387 // Check if we need to toggle hilight (ctrl mode)
1388 if (!unselect_others
)
1389 select
=!item
->IsSelected();
1391 m_current
= m_key_current
= item
;
1392 m_current
->SetHilight(select
);
1393 RefreshLine( m_current
);
1396 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1397 GetEventHandler()->ProcessEvent( event
);
1400 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1401 wxArrayTreeItemIds
&array
) const
1403 if ( item
->IsSelected() )
1404 array
.Add(wxTreeItemId(item
));
1406 if ( item
->HasChildren() )
1408 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1409 size_t count
= children
.GetCount();
1410 for ( size_t n
= 0; n
< count
; ++n
)
1411 FillArray(children
[n
], array
);
1415 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1418 wxTreeItemId idRoot
= GetRootItem();
1419 if ( idRoot
.IsOk() )
1421 FillArray(idRoot
.m_pItem
, array
);
1423 //else: the tree is empty, so no selections
1425 return array
.Count();
1428 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1430 if (!item
.IsOk()) return;
1432 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1434 // first expand all parent branches
1435 wxGenericTreeItem
*parent
= gitem
->GetParent();
1439 parent
= parent
->GetParent();
1442 //if (parent) CalculatePositions();
1447 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1449 if (!item
.IsOk()) return;
1451 // We have to call this here because the label in
1452 // question might just have been added and no screen
1453 // update taken place.
1454 if (m_dirty
) wxYield();
1456 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1458 // now scroll to the item
1459 int item_y
= gitem
->GetY();
1463 ViewStart( &start_x
, &start_y
);
1464 start_y
*= PIXELS_PER_UNIT
;
1468 GetClientSize( &client_w
, &client_h
);
1470 if (item_y
< start_y
+3)
1475 m_anchor
->GetSize( x
, y
, this );
1476 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1477 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1478 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1479 // Item should appear at top
1480 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1482 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1487 m_anchor
->GetSize( x
, y
, this );
1488 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1489 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1490 item_y
+= PIXELS_PER_UNIT
+2;
1491 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1492 // Item should appear at bottom
1493 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
);
1497 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1498 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1500 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1501 wxGenericTreeItem
**item2
)
1503 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1505 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1508 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1509 const wxTreeItemId
& item2
)
1511 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1514 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1516 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1518 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1520 wxCHECK_RET( !s_treeBeingSorted
,
1521 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1523 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1524 if ( children
.Count() > 1 )
1526 s_treeBeingSorted
= this;
1527 children
.Sort(tree_ctrl_compare_func
);
1528 s_treeBeingSorted
= NULL
;
1532 //else: don't make the tree dirty as nothing changed
1535 wxImageList
*wxTreeCtrl::GetImageList() const
1537 return m_imageListNormal
;
1540 wxImageList
*wxTreeCtrl::GetStateImageList() const
1542 return m_imageListState
;
1545 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1547 m_imageListNormal
= imageList
;
1549 if ( !m_imageListNormal
)
1552 // Calculate a m_lineHeight value from the image sizes.
1553 // May be toggle off. Then wxTreeCtrl will spread when
1554 // necessary (which might look ugly).
1555 wxClientDC
dc(this);
1556 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1557 int width
= 0, height
= 0,
1558 n
= m_imageListNormal
->GetImageCount();
1560 for (int i
= 0; i
< n
; i
++)
1562 m_imageListNormal
->GetSize(i
, width
, height
);
1563 if (height
> m_lineHeight
) m_lineHeight
= height
;
1566 if (m_lineHeight
< 40)
1567 m_lineHeight
+= 2; // at least 2 pixels
1569 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1572 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1574 m_imageListState
= imageList
;
1577 // -----------------------------------------------------------------------------
1579 // -----------------------------------------------------------------------------
1581 void wxTreeCtrl::AdjustMyScrollbars()
1587 m_anchor
->GetSize( x
, y
, this );
1588 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1589 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1590 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1591 int y_pos
= GetScrollPos( wxVERTICAL
);
1592 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1596 SetScrollbars( 0, 0, 0, 0 );
1600 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1602 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1603 return item
->GetHeight();
1605 return m_lineHeight
;
1608 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1610 wxTreeItemAttr
*attr
= item
->GetAttributes();
1611 if ( attr
&& attr
->HasFont() )
1612 dc
.SetFont(attr
->GetFont());
1613 else if (item
->IsBold())
1614 dc
.SetFont(m_boldFont
);
1618 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1622 int image
= item
->GetCurrentImage();
1623 if ( image
!= NO_IMAGE
)
1625 if ( m_imageListNormal
)
1627 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1636 int total_h
= GetLineHeight(item
);
1638 if (item
->IsSelected())
1639 dc
.SetBrush(*m_hilightBrush
);
1643 if ( attr
&& attr
->HasBackgroundColour() )
1644 colBg
= attr
->GetBackgroundColour();
1646 colBg
= m_backgroundColour
;
1647 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1650 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1652 if ( image
!= NO_IMAGE
)
1654 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1655 m_imageListNormal
->Draw( image
, dc
,
1657 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1658 wxIMAGELIST_DRAW_TRANSPARENT
);
1659 dc
.DestroyClippingRegion();
1662 dc
.SetBackgroundMode(wxTRANSPARENT
);
1663 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1664 dc
.DrawText( item
->GetText(),
1665 (wxCoord
)(image_w
+ item
->GetX()),
1666 (wxCoord
)(item
->GetY() + extraH
));
1668 // restore normal font
1669 dc
.SetFont( m_normalFont
);
1672 // Now y stands for the top of the item, whereas it used to stand for middle !
1673 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1675 int horizX
= level
*m_indent
;
1677 item
->SetX( horizX
+m_indent
+m_spacing
);
1681 y
+=GetLineHeight(item
)/2;
1683 item
->SetCross( horizX
+m_indent
, y
);
1685 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1686 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1688 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1690 int startX
= horizX
;
1691 int endX
= horizX
+ (m_indent
-5);
1693 // if (!item->HasChildren()) endX += (m_indent+5);
1694 if (!item
->HasChildren()) endX
+= 20;
1696 dc
.DrawLine( startX
, y
, endX
, y
);
1698 if (item
->HasPlus())
1700 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1701 dc
.SetPen( *wxGREY_PEN
);
1702 dc
.SetBrush( *wxWHITE_BRUSH
);
1703 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1705 dc
.SetPen( *wxBLACK_PEN
);
1706 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1707 if (!item
->IsExpanded())
1708 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1710 dc
.SetPen( m_dottedPen
);
1713 wxPen
*pen
= wxTRANSPARENT_PEN
;
1716 if ( item
->IsSelected() )
1718 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1726 wxTreeItemAttr
*attr
= item
->GetAttributes();
1727 if ( attr
&& attr
->HasTextColour() )
1728 colText
= attr
->GetTextColour();
1734 dc
.SetTextForeground(colText
);
1738 PaintItem(item
, dc
);
1740 // restore DC objects
1741 dc
.SetBrush( *wxWHITE_BRUSH
);
1742 dc
.SetPen( m_dottedPen
);
1743 dc
.SetTextForeground( *wxBLACK
);
1746 y
= oldY
+GetLineHeight(item
);
1748 if (item
->IsExpanded())
1750 oldY
+=GetLineHeight(item
)/2;
1753 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1754 size_t n
, count
= children
.Count();
1755 for ( n
= 0; n
< count
; ++n
)
1758 PaintLevel( children
[n
], dc
, level
+1, y
);
1761 // it may happen that the item is expanded but has no items (when you
1762 // delete all its children for example) - don't draw the vertical line
1766 semiOldY
+=GetLineHeight(children
[--n
])/2;
1767 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1772 void wxTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1776 if ( item
->HasPlus() )
1778 // it's a folder, indicate it by a border
1783 // draw a line under the drop target because the item will be
1785 DrawLine(item
, TRUE
/* below */);
1788 SetCursor(wxCURSOR_BULLSEYE
);
1793 SetCursor(wxCURSOR_NO_ENTRY
);
1797 void wxTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
1799 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") );
1801 wxGenericTreeItem
*i
= item
.m_pItem
;
1803 wxClientDC
dc(this);
1805 dc
.SetLogicalFunction(wxINVERT
);
1806 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1808 int w
= i
->GetWidth() + 2;
1809 int h
= GetLineHeight(i
) + 2;
1811 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
1814 void wxTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
1816 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") );
1818 wxGenericTreeItem
*i
= item
.m_pItem
;
1820 wxClientDC
dc(this);
1822 dc
.SetLogicalFunction(wxINVERT
);
1828 y
+= GetLineHeight(i
) - 1;
1831 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
1834 // -----------------------------------------------------------------------------
1835 // wxWindows callbacks
1836 // -----------------------------------------------------------------------------
1838 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1846 dc
.SetFont( m_normalFont
);
1847 dc
.SetPen( m_dottedPen
);
1849 // this is now done dynamically
1850 //if(GetImageList() == NULL)
1851 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1854 PaintLevel( m_anchor
, dc
, 0, y
);
1857 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1861 if (m_current
) RefreshLine( m_current
);
1864 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1868 if (m_current
) RefreshLine( m_current
);
1871 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1873 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1874 te
.m_code
= (int)event
.KeyCode();
1875 te
.SetEventObject( this );
1876 GetEventHandler()->ProcessEvent( te
);
1878 if ( (m_current
== 0) || (m_key_current
== 0) )
1884 // how should the selection work for this event?
1885 bool is_multiple
, extended_select
, unselect_others
;
1886 EventFlagsToSelType(GetWindowStyleFlag(),
1888 event
.ControlDown(),
1889 is_multiple
, extended_select
, unselect_others
);
1893 // * : Expand all/Collapse all
1894 // ' ' | return : activate
1895 // up : go up (not last children!)
1897 // left : go to parent
1898 // right : open if parent and go next
1899 // home : go to root
1900 // end : go to last item without opening parents
1901 switch (event
.KeyCode())
1905 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1913 if ( !IsExpanded(m_current
) )
1916 ExpandAll(m_current
);
1919 //else: fall through to Collapse() it
1923 if (IsExpanded(m_current
))
1925 Collapse(m_current
);
1932 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1933 event
.m_item
= m_current
;
1935 event
.SetEventObject( this );
1936 GetEventHandler()->ProcessEvent( event
);
1940 // up goes to the previous sibling or to the last of its children if
1944 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1947 prev
= GetParent( m_key_current
);
1951 wxTreeItemId current
= m_key_current
;
1952 if (current
== GetFirstChild( prev
, cockie
))
1954 // otherwise we return to where we came from
1955 SelectItem( prev
, unselect_others
, extended_select
);
1956 m_key_current
=prev
.m_pItem
;
1957 EnsureVisible( prev
);
1964 while ( IsExpanded(prev
) && HasChildren(prev
) )
1966 wxTreeItemId child
= GetLastChild(prev
);
1973 SelectItem( prev
, unselect_others
, extended_select
);
1974 m_key_current
=prev
.m_pItem
;
1975 EnsureVisible( prev
);
1980 // left arrow goes to the parent
1983 wxTreeItemId prev
= GetParent( m_current
);
1986 EnsureVisible( prev
);
1987 SelectItem( prev
, unselect_others
, extended_select
);
1993 // this works the same as the down arrow except that we also expand the
1994 // item if it wasn't expanded yet
2000 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2003 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2004 SelectItem( child
, unselect_others
, extended_select
);
2005 m_key_current
=child
.m_pItem
;
2006 EnsureVisible( child
);
2010 wxTreeItemId next
= GetNextSibling( m_key_current
);
2013 wxTreeItemId current
= m_key_current
;
2014 while (current
&& !next
)
2016 current
= GetParent( current
);
2017 if (current
) next
= GetNextSibling( current
);
2022 SelectItem( next
, unselect_others
, extended_select
);
2023 m_key_current
=next
.m_pItem
;
2024 EnsureVisible( next
);
2030 // <End> selects the last visible tree item
2033 wxTreeItemId last
= GetRootItem();
2035 while ( last
.IsOk() && IsExpanded(last
) )
2037 wxTreeItemId lastChild
= GetLastChild(last
);
2039 // it may happen if the item was expanded but then all of
2040 // its children have been deleted - so IsExpanded() returned
2041 // TRUE, but GetLastChild() returned invalid item
2050 EnsureVisible( last
);
2051 SelectItem( last
, unselect_others
, extended_select
);
2056 // <Home> selects the root item
2059 wxTreeItemId prev
= GetRootItem();
2062 EnsureVisible( prev
);
2063 SelectItem( prev
, unselect_others
, extended_select
);
2073 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2075 // We have to call this here because the label in
2076 // question might just have been added and no screen
2077 // update taken place.
2078 if (m_dirty
) wxYield();
2080 wxClientDC
dc(this);
2082 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2083 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2088 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2089 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2090 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2091 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2093 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2098 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
2100 if (!item
.IsOk()) return;
2102 m_currentEdit
= item
.m_pItem
;
2104 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2105 te
.m_item
= m_currentEdit
;
2106 te
.SetEventObject( this );
2107 GetEventHandler()->ProcessEvent( te
);
2109 if (!te
.IsAllowed()) return;
2111 // We have to call this here because the label in
2112 // question might just have been added and no screen
2113 // update taken place.
2114 if (m_dirty
) wxYield();
2116 wxString s
= m_currentEdit
->GetText();
2117 int x
= m_currentEdit
->GetX();
2118 int y
= m_currentEdit
->GetY();
2119 int w
= m_currentEdit
->GetWidth();
2120 int h
= m_currentEdit
->GetHeight();
2125 int image
= m_currentEdit
->GetCurrentImage();
2126 if ( image
!= NO_IMAGE
)
2128 if ( m_imageListNormal
)
2130 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2135 wxFAIL_MSG(_T("you must create an image list to use images!"));
2139 w
-= image_w
+ 4; // I don't know why +4 is needed
2141 wxClientDC
dc(this);
2143 x
= dc
.LogicalToDeviceX( x
);
2144 y
= dc
.LogicalToDeviceY( y
);
2146 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
2147 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
2151 void wxTreeCtrl::OnRenameTimer()
2156 void wxTreeCtrl::OnRenameAccept()
2158 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2159 le
.m_item
= m_currentEdit
;
2160 le
.SetEventObject( this );
2161 le
.m_label
= m_renameRes
;
2162 GetEventHandler()->ProcessEvent( le
);
2164 if (!le
.IsAllowed()) return;
2166 SetItemText( m_currentEdit
, m_renameRes
);
2169 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
2171 if ( !m_anchor
) return;
2173 // we process left mouse up event (enables in-place edit), right down
2174 // (pass to the user code), left dbl click (activate item) and
2175 // dragging/moving events for items drag-and-drop
2176 if ( !(event
.LeftDown() ||
2178 event
.RightDown() ||
2179 event
.LeftDClick() ||
2181 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2188 wxClientDC
dc(this);
2190 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2191 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2194 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2196 if ( event
.Dragging() && !m_isDragging
)
2198 if (m_dragCount
== 0)
2199 m_dragStart
= wxPoint(x
,y
);
2203 if (m_dragCount
!= 3)
2205 // wait until user drags a bit further...
2209 wxEventType command
= event
.RightIsDown()
2210 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2211 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2213 wxTreeEvent
nevent( command
, GetId() );
2214 nevent
.m_item
= m_current
;
2215 nevent
.SetEventObject(this);
2217 // by default the dragging is not supported, the user code must
2218 // explicitly allow the event for it to take place
2221 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2223 // we're going to drag this item
2224 m_isDragging
= TRUE
;
2226 // remember the old cursor because we will change it while
2228 m_oldCursor
= m_cursor
;
2230 // in a single selection control, hide the selection temporarily
2231 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2233 m_oldSelection
= GetSelection().m_pItem
;
2235 if ( m_oldSelection
)
2237 m_oldSelection
->SetHilight(FALSE
);
2238 RefreshLine(m_oldSelection
);
2245 else if ( event
.Moving() )
2247 if ( item
!= m_dropTarget
)
2249 // unhighlight the previous drop target
2250 DrawDropEffect(m_dropTarget
);
2252 m_dropTarget
= item
;
2254 // highlight the current drop target if any
2255 DrawDropEffect(m_dropTarget
);
2260 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2262 // erase the highlighting
2263 DrawDropEffect(m_dropTarget
);
2265 // generate the drag end event
2266 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2268 event
.m_item
= item
;
2269 event
.m_pointDrag
= wxPoint(x
, y
);
2270 event
.SetEventObject(this);
2272 (void)GetEventHandler()->ProcessEvent(event
);
2274 m_isDragging
= FALSE
;
2275 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2277 if ( m_oldSelection
)
2279 m_oldSelection
->SetHilight(TRUE
);
2280 RefreshLine(m_oldSelection
);
2281 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2286 SetCursor(m_oldCursor
);
2292 // here we process only the messages which happen on tree items
2296 if (item
== NULL
) return; /* we hit the blank area */
2298 if ( event
.RightDown() )
2300 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2301 nevent
.m_item
= item
;
2303 nevent
.SetEventObject(this);
2304 GetEventHandler()->ProcessEvent(nevent
);
2306 else if ( event
.LeftUp() && m_lastOnSame
)
2308 if ( (item
== m_current
) &&
2309 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2310 HasFlag(wxTR_EDIT_LABELS
) )
2312 m_renameTimer
->Start( 100, TRUE
);
2315 m_lastOnSame
= FALSE
;
2319 if ( event
.LeftDown() )
2321 m_lastOnSame
= item
== m_current
;
2324 // how should the selection work for this event?
2325 bool is_multiple
, extended_select
, unselect_others
;
2326 EventFlagsToSelType(GetWindowStyleFlag(),
2328 event
.ControlDown(),
2329 is_multiple
, extended_select
, unselect_others
);
2331 if ( (flags
& wxTREE_HITTEST_ONITEMBUTTON
) && event
.LeftDown() )
2338 SelectItem(item
, unselect_others
, extended_select
);
2340 if ( event
.LeftDClick() )
2342 // double clicking should not start editing the item label
2343 m_renameTimer
->Stop();
2344 m_lastOnSame
= FALSE
;
2346 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2347 nevent
.m_item
= item
;
2349 nevent
.SetEventObject( this );
2350 GetEventHandler()->ProcessEvent( nevent
);
2356 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2358 /* after all changes have been done to the tree control,
2359 * we actually redraw the tree when everything is over */
2366 CalculatePositions();
2368 AdjustMyScrollbars();
2371 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2377 dc
.SetFont(m_boldFont
);
2379 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2382 // restore normal font
2383 dc
.SetFont( m_normalFont
);
2387 int image
= item
->GetCurrentImage();
2388 if ( image
!= NO_IMAGE
)
2390 if ( m_imageListNormal
)
2392 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2397 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2400 total_h
+= 2; // at least 2 pixels
2402 total_h
+= total_h
/10; // otherwise 10% extra spacing
2404 item
->SetHeight(total_h
);
2405 if (total_h
>m_lineHeight
)
2406 m_lineHeight
=total_h
;
2408 item
->SetWidth(image_w
+text_w
+2);
2411 // -----------------------------------------------------------------------------
2412 // for developper : y is now the top of the level
2413 // not the middle of it !
2414 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2416 int horizX
= level
*m_indent
;
2418 CalculateSize( item
, dc
);
2421 item
->SetX( horizX
+m_indent
+m_spacing
);
2423 y
+=GetLineHeight(item
);
2425 if ( !item
->IsExpanded() )
2427 // we dont need to calculate collapsed branches
2431 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2432 size_t n
, count
= children
.Count();
2433 for (n
= 0; n
< count
; ++n
)
2434 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2437 void wxTreeCtrl::CalculatePositions()
2439 if ( !m_anchor
) return;
2441 wxClientDC
dc(this);
2444 dc
.SetFont( m_normalFont
);
2446 dc
.SetPen( m_dottedPen
);
2447 //if(GetImageList() == NULL)
2448 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2451 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2454 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2456 if (m_dirty
) return;
2458 wxClientDC
dc(this);
2463 GetClientSize( &cw
, &ch
);
2466 rect
.x
= dc
.LogicalToDeviceX( 0 );
2468 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2471 Refresh( TRUE
, &rect
);
2473 AdjustMyScrollbars();
2476 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2478 if (m_dirty
) return;
2480 wxClientDC
dc(this);
2485 GetClientSize( &cw
, &ch
);
2488 rect
.x
= dc
.LogicalToDeviceX( 0 );
2489 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2491 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2493 Refresh( TRUE
, &rect
);