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 // =============================================================================
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 // translate the key or mouse event flags to the type of selection we're
191 static void EventFlagsToSelType(long style
,
195 bool *extended_select
,
196 bool *unselect_others
)
198 *is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
199 *extended_select
= shiftDown
&& is_multiple
;
200 *unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
203 // -----------------------------------------------------------------------------
204 // wxTreeRenameTimer (internal)
205 // -----------------------------------------------------------------------------
207 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl
*owner
)
212 void wxTreeRenameTimer::Notify()
214 m_owner
->OnRenameTimer();
217 //-----------------------------------------------------------------------------
218 // wxTreeTextCtrl (internal)
219 //-----------------------------------------------------------------------------
221 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
223 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
224 EVT_CHAR (wxTreeTextCtrl::OnChar
)
225 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
228 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
233 const wxString
&value
,
237 const wxValidator
& validator
,
238 const wxString
&name
)
239 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
246 m_startValue
= value
;
249 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
251 if (event
.m_keyCode
== WXK_RETURN
)
254 (*m_res
) = GetValue();
258 if (event
.m_keyCode
== WXK_ESCAPE
)
268 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
270 if (wxPendingDelete
.Member(this)) return;
272 wxPendingDelete
.Append(this);
274 if ((*m_accept
) && ((*m_res
) != m_startValue
))
275 m_owner
->OnRenameAccept();
278 #define PIXELS_PER_UNIT 10
279 // -----------------------------------------------------------------------------
281 // -----------------------------------------------------------------------------
283 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
285 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
286 : wxNotifyEvent( commandType
, id
)
289 m_itemOld
= (wxGenericTreeItem
*)NULL
;
292 // -----------------------------------------------------------------------------
294 // -----------------------------------------------------------------------------
296 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
297 const wxString
& text
,
299 int image
, int selImage
,
300 wxTreeItemData
*data
)
303 m_images
[wxTreeItemIcon_Normal
] = image
;
304 m_images
[wxTreeItemIcon_Selected
] = selImage
;
305 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
306 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
310 m_xCross
= m_yCross
= 0;
314 m_isCollapsed
= TRUE
;
315 m_hasHilight
= FALSE
;
321 m_attr
= (wxTreeItemAttr
*)NULL
;
323 // We don't know the height here yet.
328 wxGenericTreeItem::~wxGenericTreeItem()
334 wxASSERT_MSG( m_children
.IsEmpty(),
335 wxT("please call DeleteChildren() before deleting the item") );
338 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
340 size_t count
= m_children
.Count();
341 for ( size_t n
= 0; n
< count
; n
++ )
343 wxGenericTreeItem
*child
= m_children
[n
];
345 tree
->SendDeleteEvent(child
);
347 child
->DeleteChildren(tree
);
354 void wxGenericTreeItem::SetText( const wxString
&text
)
359 void wxGenericTreeItem::Reset()
362 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
364 m_images
[i
] = NO_IMAGE
;
369 m_height
= m_width
= 0;
376 m_isCollapsed
= TRUE
;
378 m_parent
= (wxGenericTreeItem
*)NULL
;
381 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
383 size_t count
= m_children
.Count();
387 size_t total
= count
;
388 for (size_t n
= 0; n
< count
; ++n
)
390 total
+= m_children
[n
]->GetChildrenCount();
396 void wxGenericTreeItem::SetCross( int x
, int y
)
402 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
404 int bottomY
=m_y
+theTree
->GetLineHeight(this);
405 if ( y
< bottomY
) y
= bottomY
;
406 int width
= m_x
+ m_width
;
407 if ( x
< width
) x
= width
;
411 size_t count
= m_children
.Count();
412 for ( size_t n
= 0; n
< count
; ++n
)
414 m_children
[n
]->GetSize( x
, y
, theTree
);
419 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
420 const wxTreeCtrl
*theTree
,
423 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
425 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2)
426 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
428 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
430 // 5 is the size of the plus sign
431 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
432 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
433 (IsExpanded() || HasPlus()))
435 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
439 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
444 // assuming every image (normal and selected ) has the same size !
445 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
446 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
448 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
449 flags
|= wxTREE_HITTEST_ONITEMICON
;
451 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
457 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
458 if (point
.x
> m_x
+m_width
)
459 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
467 size_t count
= m_children
.Count();
468 for ( size_t n
= 0; n
< count
; n
++ )
470 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
477 flags
|=wxTREE_HITTEST_NOWHERE
;
479 return (wxGenericTreeItem
*) NULL
;
482 int wxGenericTreeItem::GetCurrentImage() const
484 int image
= NO_IMAGE
;
489 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
492 if ( image
== NO_IMAGE
)
494 // we usually fall back to the normal item, but try just the
495 // expanded one (and not selected) first in this case
496 image
= GetImage(wxTreeItemIcon_Expanded
);
502 image
= GetImage(wxTreeItemIcon_Selected
);
505 // may be it doesn't have the specific image we want, try the default one
507 if ( image
== NO_IMAGE
)
515 // -----------------------------------------------------------------------------
516 // wxTreeCtrl implementation
517 // -----------------------------------------------------------------------------
519 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
521 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
522 EVT_PAINT (wxTreeCtrl::OnPaint
)
523 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
524 EVT_CHAR (wxTreeCtrl::OnChar
)
525 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
526 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
527 EVT_IDLE (wxTreeCtrl::OnIdle
)
530 // -----------------------------------------------------------------------------
531 // construction/destruction
532 // -----------------------------------------------------------------------------
534 void wxTreeCtrl::Init()
538 m_anchor
= (wxGenericTreeItem
*) NULL
;
548 m_hilightBrush
= new wxBrush
550 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
555 m_imageListState
= (wxImageList
*) NULL
;
559 m_renameTimer
= new wxTreeRenameTimer( this );
561 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
562 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
563 m_normalFont
.GetFamily(),
564 m_normalFont
.GetStyle(),
566 m_normalFont
.GetUnderlined());
569 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
570 const wxPoint
& pos
, const wxSize
& size
,
572 const wxValidator
&validator
,
573 const wxString
& name
)
577 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
580 SetValidator( validator
);
583 SetBackgroundColour( *wxWHITE
);
584 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
585 m_dottedPen
= wxPen( "grey", 0, 0 );
590 wxTreeCtrl::~wxTreeCtrl()
592 wxDELETE( m_hilightBrush
);
596 delete m_renameTimer
;
599 // -----------------------------------------------------------------------------
601 // -----------------------------------------------------------------------------
603 size_t wxTreeCtrl::GetCount() const
605 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
608 void wxTreeCtrl::SetIndent(unsigned int indent
)
614 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
620 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
622 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
624 return item
.m_pItem
->GetChildrenCount(recursively
);
627 // -----------------------------------------------------------------------------
628 // functions to work with tree items
629 // -----------------------------------------------------------------------------
631 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
633 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
635 return item
.m_pItem
->GetText();
638 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
639 wxTreeItemIcon which
) const
641 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
643 return item
.m_pItem
->GetImage(which
);
646 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
648 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
650 return item
.m_pItem
->GetData();
653 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
655 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
658 wxGenericTreeItem
*pItem
= item
.m_pItem
;
659 pItem
->SetText(text
);
660 CalculateSize(pItem
, dc
);
664 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
666 wxTreeItemIcon which
)
668 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
670 wxGenericTreeItem
*pItem
= item
.m_pItem
;
671 pItem
->SetImage(image
, which
);
674 CalculateSize(pItem
, dc
);
678 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
680 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
682 item
.m_pItem
->SetData(data
);
685 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
687 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
689 wxGenericTreeItem
*pItem
= item
.m_pItem
;
690 pItem
->SetHasPlus(has
);
694 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
696 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
698 // avoid redrawing the tree if no real change
699 wxGenericTreeItem
*pItem
= item
.m_pItem
;
700 if ( pItem
->IsBold() != bold
)
702 pItem
->SetBold(bold
);
707 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
710 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
712 wxGenericTreeItem
*pItem
= item
.m_pItem
;
713 pItem
->Attr().SetTextColour(col
);
717 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
720 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
722 wxGenericTreeItem
*pItem
= item
.m_pItem
;
723 pItem
->Attr().SetBackgroundColour(col
);
727 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
729 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
731 wxGenericTreeItem
*pItem
= item
.m_pItem
;
732 pItem
->Attr().SetFont(font
);
736 // -----------------------------------------------------------------------------
737 // item status inquiries
738 // -----------------------------------------------------------------------------
740 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
742 wxFAIL_MSG(wxT("not implemented"));
747 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
749 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
751 return !item
.m_pItem
->GetChildren().IsEmpty();
754 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
756 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
758 return item
.m_pItem
->IsExpanded();
761 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
763 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
765 return item
.m_pItem
->IsSelected();
768 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
770 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
772 return item
.m_pItem
->IsBold();
775 // -----------------------------------------------------------------------------
777 // -----------------------------------------------------------------------------
779 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
781 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
783 return item
.m_pItem
->GetParent();
786 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
788 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
791 return GetNextChild(item
, cookie
);
794 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
796 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
798 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
799 if ( (size_t)cookie
< children
.Count() )
801 return children
.Item((size_t)cookie
++);
805 // there are no more of them
806 return wxTreeItemId();
810 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
812 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
814 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
815 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
818 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
820 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
822 wxGenericTreeItem
*i
= item
.m_pItem
;
823 wxGenericTreeItem
*parent
= i
->GetParent();
824 if ( parent
== NULL
)
826 // root item doesn't have any siblings
827 return wxTreeItemId();
830 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
831 int index
= siblings
.Index(i
);
832 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
834 size_t n
= (size_t)(index
+ 1);
835 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
838 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
840 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
842 wxGenericTreeItem
*i
= item
.m_pItem
;
843 wxGenericTreeItem
*parent
= i
->GetParent();
844 if ( parent
== NULL
)
846 // root item doesn't have any siblings
847 return wxTreeItemId();
850 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
851 int index
= siblings
.Index(i
);
852 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
854 return index
== 0 ? wxTreeItemId()
855 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
858 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
860 wxFAIL_MSG(wxT("not implemented"));
862 return wxTreeItemId();
865 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
867 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
869 wxFAIL_MSG(wxT("not implemented"));
871 return wxTreeItemId();
874 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
876 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
878 wxFAIL_MSG(wxT("not implemented"));
880 return wxTreeItemId();
883 // -----------------------------------------------------------------------------
885 // -----------------------------------------------------------------------------
887 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
889 const wxString
& text
,
890 int image
, int selImage
,
891 wxTreeItemData
*data
)
893 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
896 // should we give a warning here?
897 return AddRoot(text
, image
, selImage
, data
);
901 wxGenericTreeItem
*item
=
902 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
906 data
->m_pItem
= item
;
909 parent
->Insert( item
, previous
);
916 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
917 int image
, int selImage
,
918 wxTreeItemData
*data
)
920 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
923 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
924 image
, selImage
, data
);
927 data
->m_pItem
= m_anchor
;
930 if (!HasFlag(wxTR_MULTIPLE
))
932 m_current
= m_key_current
= m_anchor
;
933 m_current
->SetHilight( TRUE
);
937 AdjustMyScrollbars();
942 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
943 const wxString
& text
,
944 int image
, int selImage
,
945 wxTreeItemData
*data
)
947 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
950 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
951 const wxTreeItemId
& idPrevious
,
952 const wxString
& text
,
953 int image
, int selImage
,
954 wxTreeItemData
*data
)
956 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
959 // should we give a warning here?
960 return AddRoot(text
, image
, selImage
, data
);
963 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
964 wxASSERT_MSG( index
!= wxNOT_FOUND
,
965 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
967 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
970 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
972 const wxString
& text
,
973 int image
, int selImage
,
974 wxTreeItemData
*data
)
976 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
979 // should we give a warning here?
980 return AddRoot(text
, image
, selImage
, data
);
983 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
986 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
987 const wxString
& text
,
988 int image
, int selImage
,
989 wxTreeItemData
*data
)
991 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
994 // should we give a warning here?
995 return AddRoot(text
, image
, selImage
, data
);
998 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
999 image
, selImage
, data
);
1002 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1004 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1005 event
.m_item
= item
;
1006 event
.SetEventObject( this );
1007 ProcessEvent( event
);
1010 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1012 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1013 item
->DeleteChildren(this);
1018 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1020 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1021 wxGenericTreeItem
*parent
= item
->GetParent();
1025 parent
->GetChildren().Remove( item
); // remove by value
1028 item
->DeleteChildren(this);
1029 SendDeleteEvent(item
);
1035 void wxTreeCtrl::DeleteAllItems()
1039 m_anchor
->DeleteChildren(this);
1048 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1050 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1052 if ( !item
->HasPlus() )
1055 if ( item
->IsExpanded() )
1058 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1059 event
.m_item
= item
;
1060 event
.SetEventObject( this );
1062 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1063 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1065 // cancelled by program
1070 CalculatePositions();
1072 RefreshSubtree(item
);
1074 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1075 ProcessEvent( event
);
1078 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1080 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1082 if ( !item
->IsExpanded() )
1085 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1086 event
.m_item
= item
;
1087 event
.SetEventObject( this );
1088 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1090 // cancelled by program
1096 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1097 size_t count
= children
.Count();
1098 for ( size_t n
= 0; n
< count
; n
++ )
1100 Collapse(children
[n
]);
1103 CalculatePositions();
1105 RefreshSubtree(item
);
1107 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1108 ProcessEvent( event
);
1111 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1114 DeleteChildren(item
);
1117 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1119 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1121 if (item
->IsExpanded())
1127 void wxTreeCtrl::Unselect()
1131 m_current
->SetHilight( FALSE
);
1132 RefreshLine( m_current
);
1136 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1138 if (item
->IsSelected())
1140 item
->SetHilight(FALSE
);
1144 if (item
->HasChildren())
1146 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1147 size_t count
= children
.Count();
1148 for ( size_t n
= 0; n
< count
; ++n
)
1150 UnselectAllChildren(children
[n
]);
1155 void wxTreeCtrl::UnselectAll()
1157 UnselectAllChildren(GetRootItem().m_pItem
);
1160 // Recursive function !
1161 // To stop we must have crt_item<last_item
1163 // Tag all next children, when no more children,
1164 // Move to parent (not to tag)
1165 // Keep going... if we found last_item, we stop.
1166 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1168 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1170 if (parent
== NULL
) // This is root item
1171 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1173 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1174 int index
= children
.Index(crt_item
);
1175 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1177 size_t count
= children
.Count();
1178 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1180 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1183 return TagNextChildren(parent
, last_item
, select
);
1186 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1188 crt_item
->SetHilight(select
);
1189 RefreshLine(crt_item
);
1191 if (crt_item
==last_item
)
1194 if (crt_item
->HasChildren())
1196 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1197 size_t count
= children
.Count();
1198 for ( size_t n
= 0; n
< count
; ++n
)
1200 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1208 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1210 // item2 is not necessary after item1
1211 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1213 // choice first' and 'last' between item1 and item2
1214 if (item1
->GetY()<item2
->GetY())
1225 bool select
= m_current
->IsSelected();
1227 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1230 TagNextChildren(first
,last
,select
);
1233 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1234 bool unselect_others
,
1235 bool extended_select
)
1237 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1239 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1240 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1242 //wxCHECK_RET( ( (!unselect_others) && is_single),
1243 // wxT("this is a single selection tree") );
1245 // to keep going anyhow !!!
1248 if (item
->IsSelected())
1249 return; // nothing to do
1250 unselect_others
= TRUE
;
1251 extended_select
= FALSE
;
1253 else if ( unselect_others
&& item
->IsSelected() )
1255 // selection change if there is more than one item currently selected
1256 wxArrayTreeItemIds selected_items
;
1257 if ( GetSelections(selected_items
) == 1 )
1261 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1262 event
.m_item
= item
;
1263 event
.m_itemOld
= m_current
;
1264 event
.SetEventObject( this );
1265 // TODO : Here we don't send any selection mode yet !
1267 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1271 if (unselect_others
)
1273 if (is_single
) Unselect(); // to speed up thing
1278 if (extended_select
)
1280 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1281 // don't change the mark (m_current)
1282 SelectItemRange(m_current
, item
);
1286 bool select
=TRUE
; // the default
1288 // Check if we need to toggle hilight (ctrl mode)
1289 if (!unselect_others
)
1290 select
=!item
->IsSelected();
1292 m_current
= m_key_current
= item
;
1293 m_current
->SetHilight(select
);
1294 RefreshLine( m_current
);
1297 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1298 GetEventHandler()->ProcessEvent( event
);
1301 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1302 wxArrayTreeItemIds
&array
) const
1304 if ( item
->IsSelected() )
1305 array
.Add(wxTreeItemId(item
));
1307 if ( item
->HasChildren() )
1309 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1310 size_t count
= children
.GetCount();
1311 for ( size_t n
= 0; n
< count
; ++n
)
1312 FillArray(children
[n
],array
);
1316 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1319 FillArray(GetRootItem().m_pItem
, array
);
1321 return array
.Count();
1324 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1326 if (!item
.IsOk()) return;
1328 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1330 // first expand all parent branches
1331 wxGenericTreeItem
*parent
= gitem
->GetParent();
1335 parent
= parent
->GetParent();
1338 //if (parent) CalculatePositions();
1343 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1345 if (!item
.IsOk()) return;
1347 // We have to call this here because the label in
1348 // question might just have been added and no screen
1349 // update taken place.
1350 if (m_dirty
) wxYield();
1352 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1354 // now scroll to the item
1355 int item_y
= gitem
->GetY();
1359 ViewStart( &start_x
, &start_y
);
1360 start_y
*= PIXELS_PER_UNIT
;
1364 GetClientSize( &client_w
, &client_h
);
1366 if (item_y
< start_y
+3)
1371 m_anchor
->GetSize( x
, y
, this );
1372 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1373 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1374 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1375 // Item should appear at top
1376 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1378 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1383 m_anchor
->GetSize( x
, y
, this );
1384 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1385 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1386 item_y
+= PIXELS_PER_UNIT
+2;
1387 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1388 // Item should appear at bottom
1389 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
);
1393 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1394 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1396 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1397 wxGenericTreeItem
**item2
)
1399 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1401 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1404 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1405 const wxTreeItemId
& item2
)
1407 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1410 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1412 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1414 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1416 wxCHECK_RET( !s_treeBeingSorted
,
1417 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1419 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1420 if ( children
.Count() > 1 )
1422 s_treeBeingSorted
= this;
1423 children
.Sort(tree_ctrl_compare_func
);
1424 s_treeBeingSorted
= NULL
;
1428 //else: don't make the tree dirty as nothing changed
1431 wxImageList
*wxTreeCtrl::GetImageList() const
1433 return m_imageListNormal
;
1436 wxImageList
*wxTreeCtrl::GetStateImageList() const
1438 return m_imageListState
;
1441 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1443 m_imageListNormal
= imageList
;
1445 // Calculate a m_lineHeight value from the image sizes.
1446 // May be toggle off. Then wxTreeCtrl will spread when
1447 // necessary (which might look ugly).
1449 wxClientDC
dc(this);
1450 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1451 int width
= 0, height
= 0,
1452 n
= m_imageListNormal
->GetImageCount();
1454 for (int i
= 0; i
< n
; i
++)
1456 m_imageListNormal
->GetSize(i
, width
, height
);
1457 if (height
> m_lineHeight
) m_lineHeight
= height
;
1460 if (m_lineHeight
< 40)
1461 m_lineHeight
+= 2; // at least 2 pixels
1463 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1467 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1469 m_imageListState
= imageList
;
1472 // -----------------------------------------------------------------------------
1474 // -----------------------------------------------------------------------------
1476 void wxTreeCtrl::AdjustMyScrollbars()
1482 m_anchor
->GetSize( x
, y
, this );
1483 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1484 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1485 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1486 int y_pos
= GetScrollPos( wxVERTICAL
);
1487 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1491 SetScrollbars( 0, 0, 0, 0 );
1495 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1497 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1498 return item
->GetHeight();
1500 return m_lineHeight
;
1503 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1505 wxTreeItemAttr
*attr
= item
->GetAttributes();
1506 if ( attr
&& attr
->HasFont() )
1507 dc
.SetFont(attr
->GetFont());
1508 else if (item
->IsBold())
1509 dc
.SetFont(m_boldFont
);
1513 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1517 int image
= item
->GetCurrentImage();
1518 if ( image
!= NO_IMAGE
)
1520 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1524 int total_h
= GetLineHeight(item
);
1526 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1528 if ( image
!= NO_IMAGE
)
1530 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1531 m_imageListNormal
->Draw( image
, dc
,
1533 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1534 wxIMAGELIST_DRAW_TRANSPARENT
);
1535 dc
.DestroyClippingRegion();
1538 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1539 dc
.SetBackgroundMode(hasBgCol
? wxSOLID
: wxTRANSPARENT
);
1541 dc
.SetTextBackground(attr
->GetBackgroundColour());
1542 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1543 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1545 // restore normal font
1546 dc
.SetFont( m_normalFont
);
1549 // Now y stands for the top of the item, whereas it used to stand for middle !
1550 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1552 int horizX
= level
*m_indent
;
1554 item
->SetX( horizX
+m_indent
+m_spacing
);
1558 y
+=GetLineHeight(item
)/2;
1560 item
->SetCross( horizX
+m_indent
, y
);
1562 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1563 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1565 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1567 int startX
= horizX
;
1568 int endX
= horizX
+ (m_indent
-5);
1570 // if (!item->HasChildren()) endX += (m_indent+5);
1571 if (!item
->HasChildren()) endX
+= 20;
1573 dc
.DrawLine( startX
, y
, endX
, y
);
1575 if (item
->HasPlus())
1577 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1578 dc
.SetPen( *wxGREY_PEN
);
1579 dc
.SetBrush( *wxWHITE_BRUSH
);
1580 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1582 dc
.SetPen( *wxBLACK_PEN
);
1583 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1584 if (!item
->IsExpanded())
1585 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1587 dc
.SetPen( m_dottedPen
);
1590 wxPen
*pen
= wxTRANSPARENT_PEN
;
1591 wxBrush
*brush
; // FIXME is this really needed?
1594 if ( item
->IsSelected() )
1596 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1598 brush
= m_hilightBrush
;
1606 wxTreeItemAttr
*attr
= item
->GetAttributes();
1607 if ( attr
&& attr
->HasTextColour() )
1608 colText
= attr
->GetTextColour();
1612 brush
= wxWHITE_BRUSH
;
1616 dc
.SetTextForeground(colText
);
1618 dc
.SetBrush(*brush
);
1621 PaintItem(item
, dc
);
1623 // restore DC objects
1624 dc
.SetBrush( *wxWHITE_BRUSH
);
1625 dc
.SetPen( m_dottedPen
);
1626 dc
.SetTextForeground( *wxBLACK
);
1629 y
= oldY
+GetLineHeight(item
);
1631 if (item
->IsExpanded())
1633 oldY
+=GetLineHeight(item
)/2;
1636 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1637 size_t n
, count
= children
.Count();
1638 for ( n
= 0; n
< count
; ++n
)
1641 PaintLevel( children
[n
], dc
, level
+1, y
);
1644 // it may happen that the item is expanded but has no items (when you
1645 // delete all its children for example) - don't draw the vertical line
1649 semiOldY
+=GetLineHeight(children
[--n
])/2;
1650 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1655 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1659 wxGenericTreeItem
*i
=item
.m_pItem
;
1661 wxClientDC
dc(this);
1663 dc
.SetLogicalFunction(wxINVERT
);
1666 ViewStart(&x
,&h
); // we only need x
1667 GetClientSize(&w
,&h
); // we only need w
1669 h
=GetLineHeight(i
)+1;
1670 // 2 white column at border
1671 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1674 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1678 wxGenericTreeItem
*i
=item
.m_pItem
;
1680 wxClientDC
dc(this);
1682 dc
.SetLogicalFunction(wxINVERT
);
1687 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1690 dc
.DrawLine( 0, y
, w
, y
);
1693 // -----------------------------------------------------------------------------
1694 // wxWindows callbacks
1695 // -----------------------------------------------------------------------------
1697 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1705 dc
.SetFont( m_normalFont
);
1706 dc
.SetPen( m_dottedPen
);
1708 // this is now done dynamically
1709 //if(GetImageList() == NULL)
1710 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1713 PaintLevel( m_anchor
, dc
, 0, y
);
1716 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1720 if (m_current
) RefreshLine( m_current
);
1723 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1727 if (m_current
) RefreshLine( m_current
);
1730 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1732 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1733 te
.m_code
= (int)event
.KeyCode();
1734 te
.SetEventObject( this );
1735 GetEventHandler()->ProcessEvent( te
);
1737 if ( (m_current
== 0) || (m_key_current
== 0) )
1743 // how should the selection work for this event?
1744 bool is_multiple
, extended_select
, unselect_others
;
1745 EventFlagsToSelType(GetWindowStyleFlag(),
1747 event
.ControlDown(),
1748 &is_multiple
, &extended_select
, &unselect_others
);
1750 switch (event
.KeyCode())
1754 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1762 if (IsExpanded(m_current
))
1764 Collapse(m_current
);
1776 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1777 event
.m_item
= m_current
;
1779 event
.SetEventObject( this );
1780 GetEventHandler()->ProcessEvent( event
);
1784 // up goes to the previous sibling or to the last of its children if
1788 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1791 prev
= GetParent( m_key_current
);
1795 wxTreeItemId current
= m_key_current
;
1796 if (current
== GetFirstChild( prev
, cockie
))
1798 // otherwise we return to where we came from
1799 SelectItem( prev
, unselect_others
, extended_select
);
1800 m_key_current
=prev
.m_pItem
;
1801 EnsureVisible( prev
);
1808 while ( IsExpanded(prev
) && HasChildren(prev
) )
1810 wxTreeItemId child
= GetLastChild(prev
);
1817 SelectItem( prev
, unselect_others
, extended_select
);
1818 m_key_current
=prev
.m_pItem
;
1819 EnsureVisible( prev
);
1824 // left arrow goes to the parent
1827 wxTreeItemId prev
= GetParent( m_current
);
1830 EnsureVisible( prev
);
1831 SelectItem( prev
, unselect_others
, extended_select
);
1837 // this works the same as the down arrow except that we also expand the
1838 // item if it wasn't expanded yet
1844 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1847 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1848 SelectItem( child
, unselect_others
, extended_select
);
1849 m_key_current
=child
.m_pItem
;
1850 EnsureVisible( child
);
1854 wxTreeItemId next
= GetNextSibling( m_key_current
);
1858 wxTreeItemId current
= m_key_current
;
1859 while (current
&& !next
)
1861 current
= GetParent( current
);
1862 if (current
) next
= GetNextSibling( current
);
1868 SelectItem( next
, unselect_others
, extended_select
);
1869 m_key_current
=next
.m_pItem
;
1870 EnsureVisible( next
);
1876 // <End> selects the last visible tree item
1879 wxTreeItemId last
= GetRootItem();
1881 while ( last
.IsOk() && IsExpanded(last
) )
1883 wxTreeItemId lastChild
= GetLastChild(last
);
1885 // it may happen if the item was expanded but then all of
1886 // its children have been deleted - so IsExpanded() returned
1887 // TRUE, but GetLastChild() returned invalid item
1896 EnsureVisible( last
);
1897 SelectItem( last
, unselect_others
, extended_select
);
1902 // <Home> selects the root item
1905 wxTreeItemId prev
= GetRootItem();
1908 EnsureVisible( prev
);
1909 SelectItem( prev
, unselect_others
, extended_select
);
1919 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1921 // We have to call this here because the label in
1922 // question might just have been added and no screen
1923 // update taken place.
1924 if (m_dirty
) wxYield();
1926 wxClientDC
dc(this);
1928 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
1929 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
1934 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1935 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1936 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1937 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1939 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1944 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1946 if (!item
.IsOk()) return;
1948 m_currentEdit
= item
.m_pItem
;
1950 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1951 te
.m_item
= m_currentEdit
;
1952 te
.SetEventObject( this );
1953 GetEventHandler()->ProcessEvent( te
);
1955 if (!te
.IsAllowed()) return;
1957 // We have to call this here because the label in
1958 // question might just have been added and no screen
1959 // update taken place.
1960 if (m_dirty
) wxYield();
1962 wxString s
= m_currentEdit
->GetText();
1963 int x
= m_currentEdit
->GetX();
1964 int y
= m_currentEdit
->GetY();
1965 int w
= m_currentEdit
->GetWidth();
1966 int h
= m_currentEdit
->GetHeight();
1971 int image
= m_currentEdit
->GetCurrentImage();
1972 if ( image
!= NO_IMAGE
)
1974 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1978 w
-= image_w
+ 4; // I don't know why +4 is needed
1980 wxClientDC
dc(this);
1982 x
= dc
.LogicalToDeviceX( x
);
1983 y
= dc
.LogicalToDeviceY( y
);
1985 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1986 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1990 void wxTreeCtrl::OnRenameTimer()
1995 void wxTreeCtrl::OnRenameAccept()
1997 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1998 le
.m_item
= m_currentEdit
;
1999 le
.SetEventObject( this );
2000 le
.m_label
= m_renameRes
;
2001 GetEventHandler()->ProcessEvent( le
);
2003 if (!le
.IsAllowed()) return;
2005 SetItemText( m_currentEdit
, m_renameRes
);
2008 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
2010 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
2012 if ( !m_anchor
) return;
2014 wxClientDC
dc(this);
2016 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2017 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2020 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2021 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
2023 if (event
.Dragging())
2025 if (m_dragCount
== 0)
2026 m_dragStart
= wxPoint(x
,y
);
2030 if (m_dragCount
!= 3) return;
2032 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2033 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2035 wxTreeEvent
nevent( command
, GetId() );
2036 nevent
.m_item
= m_current
;
2037 nevent
.SetEventObject(this);
2038 GetEventHandler()->ProcessEvent(nevent
);
2046 if (item
== NULL
) return; /* we hit the blank area */
2048 if (event
.RightDown()) {
2049 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
2052 nevent
.SetEventObject(this);
2053 GetEventHandler()->ProcessEvent(nevent
);
2057 if (event
.LeftUp() && (item
== m_current
) &&
2058 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2059 HasFlag(wxTR_EDIT_LABELS
) )
2061 m_renameTimer
->Start( 100, TRUE
);
2065 // how should the selection work for this event?
2066 bool is_multiple
, extended_select
, unselect_others
;
2067 EventFlagsToSelType(GetWindowStyleFlag(),
2069 event
.ControlDown(),
2070 &is_multiple
, &extended_select
, &unselect_others
);
2079 SelectItem(item
, unselect_others
, extended_select
);
2081 if (event
.LeftDClick())
2083 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2084 event
.m_item
= item
;
2086 event
.SetEventObject( this );
2087 GetEventHandler()->ProcessEvent( event
);
2091 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2093 /* after all changes have been done to the tree control,
2094 * we actually redraw the tree when everything is over */
2101 CalculatePositions();
2103 AdjustMyScrollbars();
2106 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2112 dc
.SetFont(m_boldFont
);
2114 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2117 // restore normal font
2118 dc
.SetFont( m_normalFont
);
2122 int image
= item
->GetCurrentImage();
2123 if ( image
!= NO_IMAGE
)
2125 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2129 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2132 total_h
+= 2; // at least 2 pixels
2134 total_h
+= total_h
/10; // otherwise 10% extra spacing
2136 item
->SetHeight(total_h
);
2137 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2139 item
->SetWidth(image_w
+text_w
+2);
2142 // -----------------------------------------------------------------------------
2143 // for developper : y is now the top of the level
2144 // not the middle of it !
2145 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2147 int horizX
= level
*m_indent
;
2149 CalculateSize( item
, dc
);
2152 item
->SetX( horizX
+m_indent
+m_spacing
);
2154 y
+=GetLineHeight(item
);
2156 if ( !item
->IsExpanded() )
2158 // we dont need to calculate collapsed branches
2162 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2163 size_t n
, count
= children
.Count();
2164 for (n
= 0; n
< count
; ++n
)
2165 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2168 void wxTreeCtrl::CalculatePositions()
2170 if ( !m_anchor
) return;
2172 wxClientDC
dc(this);
2175 dc
.SetFont( m_normalFont
);
2177 dc
.SetPen( m_dottedPen
);
2178 //if(GetImageList() == NULL)
2179 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2182 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2185 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2187 if (m_dirty
) return;
2189 wxClientDC
dc(this);
2194 GetClientSize( &cw
, &ch
);
2197 rect
.x
= dc
.LogicalToDeviceX( 0 );
2199 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2202 Refresh( TRUE
, &rect
);
2204 AdjustMyScrollbars();
2207 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2209 if (m_dirty
) return;
2211 wxClientDC
dc(this);
2216 GetClientSize( &cw
, &ch
);
2219 rect
.x
= dc
.LogicalToDeviceX( 0 );
2220 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2222 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2224 Refresh( TRUE
, &rect
);