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 wxCoord 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
,
216 const wxString
&value
,
220 const wxValidator
& validator
,
221 const wxString
&name
)
222 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
229 m_startValue
= value
;
232 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
234 if (event
.m_keyCode
== WXK_RETURN
)
237 (*m_res
) = GetValue();
241 if (event
.m_keyCode
== WXK_ESCAPE
)
251 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
253 if (wxPendingDelete
.Member(this)) return;
255 wxPendingDelete
.Append(this);
257 if ((*m_accept
) && ((*m_res
) != m_startValue
))
258 m_owner
->OnRenameAccept();
261 #define PIXELS_PER_UNIT 10
262 // -----------------------------------------------------------------------------
264 // -----------------------------------------------------------------------------
266 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
268 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
269 : wxNotifyEvent( commandType
, id
)
272 m_itemOld
= (wxGenericTreeItem
*)NULL
;
275 // -----------------------------------------------------------------------------
277 // -----------------------------------------------------------------------------
279 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
280 const wxString
& text
,
282 int image
, int selImage
,
283 wxTreeItemData
*data
)
286 m_images
[wxTreeItemIcon_Normal
] = image
;
287 m_images
[wxTreeItemIcon_Selected
] = selImage
;
288 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
289 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
293 m_xCross
= m_yCross
= 0;
297 m_isCollapsed
= TRUE
;
298 m_hasHilight
= FALSE
;
304 m_attr
= (wxTreeItemAttr
*)NULL
;
306 // We don't know the height here yet.
311 wxGenericTreeItem::~wxGenericTreeItem()
317 wxASSERT_MSG( m_children
.IsEmpty(),
318 wxT("please call DeleteChildren() before deleting the item") );
321 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
323 size_t count
= m_children
.Count();
324 for ( size_t n
= 0; n
< count
; n
++ )
326 wxGenericTreeItem
*child
= m_children
[n
];
328 tree
->SendDeleteEvent(child
);
330 child
->DeleteChildren(tree
);
337 void wxGenericTreeItem::SetText( const wxString
&text
)
342 void wxGenericTreeItem::Reset()
345 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
347 m_images
[i
] = NO_IMAGE
;
352 m_height
= m_width
= 0;
359 m_isCollapsed
= TRUE
;
361 m_parent
= (wxGenericTreeItem
*)NULL
;
364 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
366 size_t count
= m_children
.Count();
370 size_t total
= count
;
371 for (size_t n
= 0; n
< count
; ++n
)
373 total
+= m_children
[n
]->GetChildrenCount();
379 void wxGenericTreeItem::SetCross( int x
, int y
)
385 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
387 int bottomY
=m_y
+theTree
->GetLineHeight(this);
388 if ( y
< bottomY
) y
= bottomY
;
389 int width
= m_x
+ m_width
;
390 if ( x
< width
) x
= width
;
394 size_t count
= m_children
.Count();
395 for ( size_t n
= 0; n
< count
; ++n
)
397 m_children
[n
]->GetSize( x
, y
, theTree
);
402 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
403 const wxTreeCtrl
*theTree
,
406 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
408 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2)
409 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
411 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
413 // 5 is the size of the plus sign
414 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
415 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
416 (IsExpanded() || HasPlus()))
418 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
422 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
427 // assuming every image (normal and selected ) has the same size !
428 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
429 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
431 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
432 flags
|= wxTREE_HITTEST_ONITEMICON
;
434 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
440 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
441 if (point
.x
> m_x
+m_width
)
442 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
450 size_t count
= m_children
.Count();
451 for ( size_t n
= 0; n
< count
; n
++ )
453 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
460 flags
|=wxTREE_HITTEST_NOWHERE
;
462 return (wxGenericTreeItem
*) NULL
;
465 int wxGenericTreeItem::GetCurrentImage() const
467 int image
= NO_IMAGE
;
472 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
475 if ( image
== NO_IMAGE
)
477 // we usually fall back to the normal item, but try just the
478 // expanded one (and not selected) first in this case
479 image
= GetImage(wxTreeItemIcon_Expanded
);
485 image
= GetImage(wxTreeItemIcon_Selected
);
488 // may be it doesn't have the specific image we want, try the default one
490 if ( image
== NO_IMAGE
)
498 // -----------------------------------------------------------------------------
499 // wxTreeCtrl implementation
500 // -----------------------------------------------------------------------------
502 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
504 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
505 EVT_PAINT (wxTreeCtrl::OnPaint
)
506 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
507 EVT_CHAR (wxTreeCtrl::OnChar
)
508 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
509 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
510 EVT_IDLE (wxTreeCtrl::OnIdle
)
513 // -----------------------------------------------------------------------------
514 // construction/destruction
515 // -----------------------------------------------------------------------------
517 void wxTreeCtrl::Init()
521 m_anchor
= (wxGenericTreeItem
*) NULL
;
531 m_hilightBrush
= new wxBrush
533 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
538 m_imageListState
= (wxImageList
*) NULL
;
542 m_renameTimer
= new wxTreeRenameTimer( this );
544 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
545 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
546 m_normalFont
.GetFamily(),
547 m_normalFont
.GetStyle(),
549 m_normalFont
.GetUnderlined());
552 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
553 const wxPoint
& pos
, const wxSize
& size
,
555 const wxValidator
&validator
,
556 const wxString
& name
)
560 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
563 SetValidator( validator
);
566 SetBackgroundColour( *wxWHITE
);
567 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
568 m_dottedPen
= wxPen( "grey", 0, 0 );
573 wxTreeCtrl::~wxTreeCtrl()
575 wxDELETE( m_hilightBrush
);
579 delete m_renameTimer
;
582 // -----------------------------------------------------------------------------
584 // -----------------------------------------------------------------------------
586 size_t wxTreeCtrl::GetCount() const
588 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
591 void wxTreeCtrl::SetIndent(unsigned int indent
)
597 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
603 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
605 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
607 return item
.m_pItem
->GetChildrenCount(recursively
);
610 // -----------------------------------------------------------------------------
611 // functions to work with tree items
612 // -----------------------------------------------------------------------------
614 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
616 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
618 return item
.m_pItem
->GetText();
621 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
622 wxTreeItemIcon which
) const
624 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
626 return item
.m_pItem
->GetImage(which
);
629 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
631 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
633 return item
.m_pItem
->GetData();
636 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
638 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
641 wxGenericTreeItem
*pItem
= item
.m_pItem
;
642 pItem
->SetText(text
);
643 CalculateSize(pItem
, dc
);
647 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
649 wxTreeItemIcon which
)
651 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
653 wxGenericTreeItem
*pItem
= item
.m_pItem
;
654 pItem
->SetImage(image
, which
);
657 CalculateSize(pItem
, dc
);
661 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
663 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
665 item
.m_pItem
->SetData(data
);
668 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
670 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
672 wxGenericTreeItem
*pItem
= item
.m_pItem
;
673 pItem
->SetHasPlus(has
);
677 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
679 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
681 // avoid redrawing the tree if no real change
682 wxGenericTreeItem
*pItem
= item
.m_pItem
;
683 if ( pItem
->IsBold() != bold
)
685 pItem
->SetBold(bold
);
690 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
693 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
695 wxGenericTreeItem
*pItem
= item
.m_pItem
;
696 pItem
->Attr().SetTextColour(col
);
700 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
703 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
705 wxGenericTreeItem
*pItem
= item
.m_pItem
;
706 pItem
->Attr().SetBackgroundColour(col
);
710 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
712 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
714 wxGenericTreeItem
*pItem
= item
.m_pItem
;
715 pItem
->Attr().SetFont(font
);
719 // -----------------------------------------------------------------------------
720 // item status inquiries
721 // -----------------------------------------------------------------------------
723 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
725 wxFAIL_MSG(wxT("not implemented"));
730 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
732 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
734 return !item
.m_pItem
->GetChildren().IsEmpty();
737 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
739 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
741 return item
.m_pItem
->IsExpanded();
744 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
746 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
748 return item
.m_pItem
->IsSelected();
751 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
753 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
755 return item
.m_pItem
->IsBold();
758 // -----------------------------------------------------------------------------
760 // -----------------------------------------------------------------------------
762 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
764 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
766 return item
.m_pItem
->GetParent();
769 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
771 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
774 return GetNextChild(item
, cookie
);
777 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
779 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
781 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
782 if ( (size_t)cookie
< children
.Count() )
784 return children
.Item(cookie
++);
788 // there are no more of them
789 return wxTreeItemId();
793 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
795 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
797 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
798 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
801 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
803 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
805 wxGenericTreeItem
*i
= item
.m_pItem
;
806 wxGenericTreeItem
*parent
= i
->GetParent();
807 if ( parent
== NULL
)
809 // root item doesn't have any siblings
810 return wxTreeItemId();
813 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
814 int index
= siblings
.Index(i
);
815 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
817 size_t n
= (size_t)(index
+ 1);
818 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
821 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
823 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
825 wxGenericTreeItem
*i
= item
.m_pItem
;
826 wxGenericTreeItem
*parent
= i
->GetParent();
827 if ( parent
== NULL
)
829 // root item doesn't have any siblings
830 return wxTreeItemId();
833 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
834 int index
= siblings
.Index(i
);
835 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
837 return index
== 0 ? wxTreeItemId()
838 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
841 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
843 wxFAIL_MSG(wxT("not implemented"));
845 return wxTreeItemId();
848 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
850 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
852 wxFAIL_MSG(wxT("not implemented"));
854 return wxTreeItemId();
857 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
859 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
861 wxFAIL_MSG(wxT("not implemented"));
863 return wxTreeItemId();
866 // -----------------------------------------------------------------------------
868 // -----------------------------------------------------------------------------
870 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
872 const wxString
& text
,
873 int image
, int selImage
,
874 wxTreeItemData
*data
)
876 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
879 // should we give a warning here?
880 return AddRoot(text
, image
, selImage
, data
);
884 wxGenericTreeItem
*item
=
885 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
889 data
->m_pItem
= item
;
892 parent
->Insert( item
, previous
);
899 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
900 int image
, int selImage
,
901 wxTreeItemData
*data
)
903 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
906 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
907 image
, selImage
, data
);
910 data
->m_pItem
= m_anchor
;
913 if (!HasFlag(wxTR_MULTIPLE
))
915 m_current
= m_key_current
= m_anchor
;
916 m_current
->SetHilight( TRUE
);
920 AdjustMyScrollbars();
925 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
926 const wxString
& text
,
927 int image
, int selImage
,
928 wxTreeItemData
*data
)
930 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
933 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
934 const wxTreeItemId
& idPrevious
,
935 const wxString
& text
,
936 int image
, int selImage
,
937 wxTreeItemData
*data
)
939 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
942 // should we give a warning here?
943 return AddRoot(text
, image
, selImage
, data
);
946 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
947 wxASSERT_MSG( index
!= wxNOT_FOUND
,
948 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
950 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
953 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
955 const wxString
& text
,
956 int image
, int selImage
,
957 wxTreeItemData
*data
)
959 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
962 // should we give a warning here?
963 return AddRoot(text
, image
, selImage
, data
);
966 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
969 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
970 const wxString
& text
,
971 int image
, int selImage
,
972 wxTreeItemData
*data
)
974 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
977 // should we give a warning here?
978 return AddRoot(text
, image
, selImage
, data
);
981 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
982 image
, selImage
, data
);
985 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
987 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
989 event
.SetEventObject( this );
990 ProcessEvent( event
);
993 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
995 wxGenericTreeItem
*item
= itemId
.m_pItem
;
996 item
->DeleteChildren(this);
1001 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1003 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1004 wxGenericTreeItem
*parent
= item
->GetParent();
1008 parent
->GetChildren().Remove( item
); // remove by value
1011 item
->DeleteChildren(this);
1012 SendDeleteEvent(item
);
1018 void wxTreeCtrl::DeleteAllItems()
1022 m_anchor
->DeleteChildren(this);
1031 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1033 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1035 if ( !item
->HasPlus() )
1038 if ( item
->IsExpanded() )
1041 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1042 event
.m_item
= item
;
1043 event
.SetEventObject( this );
1045 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1046 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1048 // cancelled by program
1053 CalculatePositions();
1055 RefreshSubtree(item
);
1057 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1058 ProcessEvent( event
);
1061 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1063 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1065 if ( !item
->IsExpanded() )
1068 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1069 event
.m_item
= item
;
1070 event
.SetEventObject( this );
1071 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1073 // cancelled by program
1079 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1080 size_t count
= children
.Count();
1081 for ( size_t n
= 0; n
< count
; n
++ )
1083 Collapse(children
[n
]);
1086 CalculatePositions();
1088 RefreshSubtree(item
);
1090 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1091 ProcessEvent( event
);
1094 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1097 DeleteChildren(item
);
1100 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1102 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1104 if (item
->IsExpanded())
1110 void wxTreeCtrl::Unselect()
1114 m_current
->SetHilight( FALSE
);
1115 RefreshLine( m_current
);
1119 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1121 if (item
->IsSelected())
1123 item
->SetHilight(FALSE
);
1127 if (item
->HasChildren())
1129 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1130 size_t count
= children
.Count();
1131 for ( size_t n
= 0; n
< count
; ++n
)
1133 UnselectAllChildren(children
[n
]);
1138 void wxTreeCtrl::UnselectAll()
1140 UnselectAllChildren(GetRootItem().m_pItem
);
1143 // Recursive function !
1144 // To stop we must have crt_item<last_item
1146 // Tag all next children, when no more children,
1147 // Move to parent (not to tag)
1148 // Keep going... if we found last_item, we stop.
1149 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1151 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1153 if (parent
== NULL
) // This is root item
1154 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1156 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1157 int index
= children
.Index(crt_item
);
1158 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1160 size_t count
= children
.Count();
1161 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1163 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1166 return TagNextChildren(parent
, last_item
, select
);
1169 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1171 crt_item
->SetHilight(select
);
1172 RefreshLine(crt_item
);
1174 if (crt_item
==last_item
)
1177 if (crt_item
->HasChildren())
1179 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1180 size_t count
= children
.Count();
1181 for ( size_t n
= 0; n
< count
; ++n
)
1183 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1191 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1193 // item2 is not necessary after item1
1194 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1196 // choice first' and 'last' between item1 and item2
1197 if (item1
->GetY()<item2
->GetY())
1208 bool select
= m_current
->IsSelected();
1210 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1213 TagNextChildren(first
,last
,select
);
1216 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1217 bool unselect_others
,
1218 bool extended_select
)
1220 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1222 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1223 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1225 //wxCHECK_RET( ( (!unselect_others) && is_single),
1226 // wxT("this is a single selection tree") );
1228 // to keep going anyhow !!!
1231 if (item
->IsSelected())
1232 return; // nothing to do
1233 unselect_others
= TRUE
;
1234 extended_select
= FALSE
;
1236 else if ( unselect_others
&& item
->IsSelected() )
1238 // selection change if there is more than one item currently selected
1239 wxArrayTreeItemIds selected_items
;
1240 if ( GetSelections(selected_items
) == 1 )
1244 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1245 event
.m_item
= item
;
1246 event
.m_itemOld
= m_current
;
1247 event
.SetEventObject( this );
1248 // TODO : Here we don't send any selection mode yet !
1250 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1254 if (unselect_others
)
1256 if (is_single
) Unselect(); // to speed up thing
1261 if (extended_select
)
1263 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1264 // don't change the mark (m_current)
1265 SelectItemRange(m_current
, item
);
1269 bool select
=TRUE
; // the default
1271 // Check if we need to toggle hilight (ctrl mode)
1272 if (!unselect_others
)
1273 select
=!item
->IsSelected();
1275 m_current
= m_key_current
= item
;
1276 m_current
->SetHilight(select
);
1277 RefreshLine( m_current
);
1280 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1281 GetEventHandler()->ProcessEvent( event
);
1284 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1285 wxArrayTreeItemIds
&array
) const
1287 if ( item
->IsSelected() )
1288 array
.Add(wxTreeItemId(item
));
1290 if ( item
->HasChildren() )
1292 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1293 size_t count
= children
.GetCount();
1294 for ( size_t n
= 0; n
< count
; ++n
)
1295 FillArray(children
[n
],array
);
1299 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1302 FillArray(GetRootItem().m_pItem
, array
);
1304 return array
.Count();
1307 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1309 if (!item
.IsOk()) return;
1311 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1313 // first expand all parent branches
1314 wxGenericTreeItem
*parent
= gitem
->GetParent();
1318 parent
= parent
->GetParent();
1321 //if (parent) CalculatePositions();
1326 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1328 if (!item
.IsOk()) return;
1330 // We have to call this here because the label in
1331 // question might just have been added and no screen
1332 // update taken place.
1333 if (m_dirty
) wxYield();
1335 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1337 // now scroll to the item
1338 int item_y
= gitem
->GetY();
1342 ViewStart( &start_x
, &start_y
);
1343 start_y
*= PIXELS_PER_UNIT
;
1347 GetClientSize( &client_w
, &client_h
);
1349 if (item_y
< start_y
+3)
1354 m_anchor
->GetSize( x
, y
, this );
1355 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1356 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1357 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1358 // Item should appear at top
1359 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1361 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1366 m_anchor
->GetSize( x
, y
, this );
1367 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1368 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1369 item_y
+= PIXELS_PER_UNIT
+2;
1370 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1371 // Item should appear at bottom
1372 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
);
1376 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1377 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1379 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1380 wxGenericTreeItem
**item2
)
1382 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1384 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1387 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1388 const wxTreeItemId
& item2
)
1390 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1393 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1395 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1397 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1399 wxCHECK_RET( !s_treeBeingSorted
,
1400 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1402 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1403 if ( children
.Count() > 1 )
1405 s_treeBeingSorted
= this;
1406 children
.Sort(tree_ctrl_compare_func
);
1407 s_treeBeingSorted
= NULL
;
1411 //else: don't make the tree dirty as nothing changed
1414 wxImageList
*wxTreeCtrl::GetImageList() const
1416 return m_imageListNormal
;
1419 wxImageList
*wxTreeCtrl::GetStateImageList() const
1421 return m_imageListState
;
1424 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1426 m_imageListNormal
= imageList
;
1428 // Calculate a m_lineHeight value from the image sizes.
1429 // May be toggle off. Then wxTreeCtrl will spread when
1430 // necessary (which might look ugly).
1432 wxClientDC
dc(this);
1433 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1434 int width
= 0, height
= 0,
1435 n
= m_imageListNormal
->GetImageCount();
1437 for (int i
= 0; i
< n
; i
++)
1439 m_imageListNormal
->GetSize(i
, width
, height
);
1440 if (height
> m_lineHeight
) m_lineHeight
= height
;
1443 if (m_lineHeight
< 40)
1444 m_lineHeight
+= 2; // at least 2 pixels
1446 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1450 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1452 m_imageListState
= imageList
;
1455 // -----------------------------------------------------------------------------
1457 // -----------------------------------------------------------------------------
1459 void wxTreeCtrl::AdjustMyScrollbars()
1465 m_anchor
->GetSize( x
, y
, this );
1466 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1467 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1468 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1469 int y_pos
= GetScrollPos( wxVERTICAL
);
1470 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1474 SetScrollbars( 0, 0, 0, 0 );
1478 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1480 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1481 return item
->GetHeight();
1483 return m_lineHeight
;
1486 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1488 wxTreeItemAttr
*attr
= item
->GetAttributes();
1489 if ( attr
&& attr
->HasFont() )
1490 dc
.SetFont(attr
->GetFont());
1491 else if (item
->IsBold())
1492 dc
.SetFont(m_boldFont
);
1496 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1500 int image
= item
->GetCurrentImage();
1501 if ( image
!= NO_IMAGE
)
1503 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1507 int total_h
= GetLineHeight(item
);
1509 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1511 if ( image
!= NO_IMAGE
)
1513 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1514 m_imageListNormal
->Draw( image
, dc
,
1516 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1517 wxIMAGELIST_DRAW_TRANSPARENT
);
1518 dc
.DestroyClippingRegion();
1521 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1522 dc
.SetBackgroundMode(hasBgCol
? wxSOLID
: wxTRANSPARENT
);
1524 dc
.SetTextBackground(attr
->GetBackgroundColour());
1525 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1526 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1528 // restore normal font
1529 dc
.SetFont( m_normalFont
);
1532 // Now y stands for the top of the item, whereas it used to stand for middle !
1533 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1535 int horizX
= level
*m_indent
;
1537 item
->SetX( horizX
+m_indent
+m_spacing
);
1541 y
+=GetLineHeight(item
)/2;
1543 item
->SetCross( horizX
+m_indent
, y
);
1545 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1546 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1548 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1550 int startX
= horizX
;
1551 int endX
= horizX
+ (m_indent
-5);
1553 // if (!item->HasChildren()) endX += (m_indent+5);
1554 if (!item
->HasChildren()) endX
+= 20;
1556 dc
.DrawLine( startX
, y
, endX
, y
);
1558 if (item
->HasPlus())
1560 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1561 dc
.SetPen( *wxGREY_PEN
);
1562 dc
.SetBrush( *wxWHITE_BRUSH
);
1563 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1565 dc
.SetPen( *wxBLACK_PEN
);
1566 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1567 if (!item
->IsExpanded())
1568 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1570 dc
.SetPen( m_dottedPen
);
1573 wxPen
*pen
= wxTRANSPARENT_PEN
;
1574 wxBrush
*brush
; // FIXME is this really needed?
1577 if ( item
->IsSelected() )
1579 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1581 brush
= m_hilightBrush
;
1589 wxTreeItemAttr
*attr
= item
->GetAttributes();
1590 if ( attr
&& attr
->HasTextColour() )
1591 colText
= attr
->GetTextColour();
1595 brush
= wxWHITE_BRUSH
;
1599 dc
.SetTextForeground(colText
);
1601 dc
.SetBrush(*brush
);
1604 PaintItem(item
, dc
);
1606 // restore DC objects
1607 dc
.SetBrush( *wxWHITE_BRUSH
);
1608 dc
.SetPen( m_dottedPen
);
1609 dc
.SetTextForeground( *wxBLACK
);
1612 y
= oldY
+GetLineHeight(item
);
1614 if (item
->IsExpanded())
1616 oldY
+=GetLineHeight(item
)/2;
1619 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1620 size_t n
, count
= children
.Count();
1621 for ( n
= 0; n
< count
; ++n
)
1624 PaintLevel( children
[n
], dc
, level
+1, y
);
1627 // it may happen that the item is expanded but has no items (when you
1628 // delete all its children for example) - don't draw the vertical line
1632 semiOldY
+=GetLineHeight(children
[--n
])/2;
1633 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1638 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1642 wxGenericTreeItem
*i
=item
.m_pItem
;
1644 wxClientDC
dc(this);
1646 dc
.SetLogicalFunction(wxINVERT
);
1649 ViewStart(&x
,&h
); // we only need x
1650 GetClientSize(&w
,&h
); // we only need w
1652 h
=GetLineHeight(i
)+1;
1653 // 2 white column at border
1654 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1657 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1661 wxGenericTreeItem
*i
=item
.m_pItem
;
1663 wxClientDC
dc(this);
1665 dc
.SetLogicalFunction(wxINVERT
);
1670 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1673 dc
.DrawLine( 0, y
, w
, y
);
1676 // -----------------------------------------------------------------------------
1677 // wxWindows callbacks
1678 // -----------------------------------------------------------------------------
1680 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1688 dc
.SetFont( m_normalFont
);
1689 dc
.SetPen( m_dottedPen
);
1691 // this is now done dynamically
1692 //if(GetImageList() == NULL)
1693 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1696 PaintLevel( m_anchor
, dc
, 0, y
);
1699 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1703 if (m_current
) RefreshLine( m_current
);
1706 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1710 if (m_current
) RefreshLine( m_current
);
1713 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1715 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1716 te
.m_code
= event
.KeyCode();
1717 te
.SetEventObject( this );
1718 GetEventHandler()->ProcessEvent( te
);
1720 if ( (m_current
== 0) || (m_key_current
== 0) )
1726 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1727 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1728 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1730 switch (event
.KeyCode())
1734 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1742 if (IsExpanded(m_current
))
1744 Collapse(m_current
);
1756 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1757 event
.m_item
= m_current
;
1759 event
.SetEventObject( this );
1760 GetEventHandler()->ProcessEvent( event
);
1764 // up goes to the previous sibling or to the last of its children if
1768 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1771 prev
= GetParent( m_key_current
);
1775 wxTreeItemId current
= m_key_current
;
1776 if (current
== GetFirstChild( prev
, cockie
))
1778 // otherwise we return to where we came from
1779 SelectItem( prev
, unselect_others
, extended_select
);
1780 m_key_current
=prev
.m_pItem
;
1781 EnsureVisible( prev
);
1788 while ( IsExpanded(prev
) && HasChildren(prev
) )
1790 wxTreeItemId child
= GetLastChild(prev
);
1797 SelectItem( prev
, unselect_others
, extended_select
);
1798 m_key_current
=prev
.m_pItem
;
1799 EnsureVisible( prev
);
1804 // left arrow goes to the parent
1807 wxTreeItemId prev
= GetParent( m_current
);
1810 EnsureVisible( prev
);
1811 SelectItem( prev
, unselect_others
, extended_select
);
1817 // this works the same as the down arrow except that we also expand the
1818 // item if it wasn't expanded yet
1824 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1827 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1828 SelectItem( child
, unselect_others
, extended_select
);
1829 m_key_current
=child
.m_pItem
;
1830 EnsureVisible( child
);
1834 wxTreeItemId next
= GetNextSibling( m_key_current
);
1838 wxTreeItemId current
= m_key_current
;
1839 while (current
&& !next
)
1841 current
= GetParent( current
);
1842 if (current
) next
= GetNextSibling( current
);
1848 SelectItem( next
, unselect_others
, extended_select
);
1849 m_key_current
=next
.m_pItem
;
1850 EnsureVisible( next
);
1856 // <End> selects the last visible tree item
1859 wxTreeItemId last
= GetRootItem();
1861 while ( last
.IsOk() && IsExpanded(last
) )
1863 wxTreeItemId lastChild
= GetLastChild(last
);
1865 // it may happen if the item was expanded but then all of
1866 // its children have been deleted - so IsExpanded() returned
1867 // TRUE, but GetLastChild() returned invalid item
1876 EnsureVisible( last
);
1877 SelectItem( last
, unselect_others
, extended_select
);
1882 // <Home> selects the root item
1885 wxTreeItemId prev
= GetRootItem();
1888 EnsureVisible( prev
);
1889 SelectItem( prev
, unselect_others
, extended_select
);
1899 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1901 // We have to call this here because the label in
1902 // question might just have been added and no screen
1903 // update taken place.
1904 if (m_dirty
) wxYield();
1906 wxClientDC
dc(this);
1908 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1909 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1914 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1915 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1916 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1917 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1919 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1924 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1926 if (!item
.IsOk()) return;
1928 m_currentEdit
= item
.m_pItem
;
1930 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1931 te
.m_item
= m_currentEdit
;
1932 te
.SetEventObject( this );
1933 GetEventHandler()->ProcessEvent( te
);
1935 if (!te
.IsAllowed()) return;
1937 // We have to call this here because the label in
1938 // question might just have been added and no screen
1939 // update taken place.
1940 if (m_dirty
) wxYield();
1942 wxString s
= m_currentEdit
->GetText();
1943 int x
= m_currentEdit
->GetX();
1944 int y
= m_currentEdit
->GetY();
1945 int w
= m_currentEdit
->GetWidth();
1946 int h
= m_currentEdit
->GetHeight();
1951 int image
= m_currentEdit
->GetCurrentImage();
1952 if ( image
!= NO_IMAGE
)
1954 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1958 w
-= image_w
+ 4; // I don't know why +4 is needed
1960 wxClientDC
dc(this);
1962 x
= dc
.LogicalToDeviceX( x
);
1963 y
= dc
.LogicalToDeviceY( y
);
1965 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1966 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1970 void wxTreeCtrl::OnRenameTimer()
1975 void wxTreeCtrl::OnRenameAccept()
1977 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1978 le
.m_item
= m_currentEdit
;
1979 le
.SetEventObject( this );
1980 le
.m_label
= m_renameRes
;
1981 GetEventHandler()->ProcessEvent( le
);
1983 if (!le
.IsAllowed()) return;
1985 SetItemText( m_currentEdit
, m_renameRes
);
1988 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1990 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1992 if ( !m_anchor
) return;
1994 wxClientDC
dc(this);
1996 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1997 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
2000 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2001 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
2003 if (event
.Dragging())
2005 if (m_dragCount
== 0)
2006 m_dragStart
= wxPoint(x
,y
);
2010 if (m_dragCount
!= 3) return;
2012 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2013 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2015 wxTreeEvent
nevent( command
, GetId() );
2016 nevent
.m_item
= m_current
;
2017 nevent
.SetEventObject(this);
2018 GetEventHandler()->ProcessEvent(nevent
);
2026 if (item
== NULL
) return; /* we hit the blank area */
2028 if (event
.RightDown()) {
2029 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
2032 nevent
.SetEventObject(this);
2033 GetEventHandler()->ProcessEvent(nevent
);
2037 if (event
.LeftUp() && (item
== m_current
) &&
2038 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2039 HasFlag(wxTR_EDIT_LABELS
) )
2041 m_renameTimer
->Start( 100, TRUE
);
2045 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2046 bool extended_select
=(event
.ShiftDown() && is_multiple
);
2047 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
2056 SelectItem(item
, unselect_others
, extended_select
);
2058 if (event
.LeftDClick())
2060 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2061 event
.m_item
= item
;
2063 event
.SetEventObject( this );
2064 GetEventHandler()->ProcessEvent( event
);
2068 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2070 /* after all changes have been done to the tree control,
2071 * we actually redraw the tree when everything is over */
2078 CalculatePositions();
2080 AdjustMyScrollbars();
2083 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2089 dc
.SetFont(m_boldFont
);
2091 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2094 // restore normal font
2095 dc
.SetFont( m_normalFont
);
2099 int image
= item
->GetCurrentImage();
2100 if ( image
!= NO_IMAGE
)
2102 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2106 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2109 total_h
+= 2; // at least 2 pixels
2111 total_h
+= total_h
/10; // otherwise 10% extra spacing
2113 item
->SetHeight(total_h
);
2114 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2116 item
->SetWidth(image_w
+text_w
+2);
2119 // -----------------------------------------------------------------------------
2120 // for developper : y is now the top of the level
2121 // not the middle of it !
2122 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2124 int horizX
= level
*m_indent
;
2126 CalculateSize( item
, dc
);
2129 item
->SetX( horizX
+m_indent
+m_spacing
);
2131 y
+=GetLineHeight(item
);
2133 if ( !item
->IsExpanded() )
2135 // we dont need to calculate collapsed branches
2139 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2140 size_t n
, count
= children
.Count();
2141 for (n
= 0; n
< count
; ++n
)
2142 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2145 void wxTreeCtrl::CalculatePositions()
2147 if ( !m_anchor
) return;
2149 wxClientDC
dc(this);
2152 dc
.SetFont( m_normalFont
);
2154 dc
.SetPen( m_dottedPen
);
2155 //if(GetImageList() == NULL)
2156 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2159 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2162 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2164 if (m_dirty
) return;
2166 wxClientDC
dc(this);
2171 GetClientSize( &cw
, &ch
);
2174 rect
.x
= dc
.LogicalToDeviceX( 0 );
2176 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2179 Refresh( TRUE
, &rect
);
2181 AdjustMyScrollbars();
2184 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2186 if (m_dirty
) return;
2188 wxClientDC
dc(this);
2193 GetClientSize( &cw
, &ch
);
2196 rect
.x
= dc
.LogicalToDeviceX( 0 );
2197 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2199 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2201 Refresh( TRUE
, &rect
);