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 pointer
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 TRUE
126 // if the point belongs to the item's button, otherwise it lies on the
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
; }
145 // tree ctrl images for the normal, selected, expanded and expanded+selected
147 int m_images
[wxTreeItemIcon_Max
];
149 wxTreeItemData
*m_data
;
151 // use bitfields to save size
152 int m_isCollapsed
:1;
153 int m_hasHilight
:1; // same as focused
154 int m_hasPlus
:1; // used for item which doesn't have
155 // children but still has a [+] button
156 int m_isBold
:1; // render the label in bold font
159 long m_height
, m_width
;
160 int m_xCross
, m_yCross
;
162 wxArrayGenericTreeItems m_children
;
163 wxGenericTreeItem
*m_parent
;
166 // =============================================================================
168 // =============================================================================
171 // -----------------------------------------------------------------------------
172 // wxTreeRenameTimer (internal)
173 // -----------------------------------------------------------------------------
175 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl
*owner
)
180 void wxTreeRenameTimer::Notify()
182 m_owner
->OnRenameTimer();
185 //-----------------------------------------------------------------------------
186 // wxTreeTextCtrl (internal)
187 //-----------------------------------------------------------------------------
189 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
191 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
192 EVT_CHAR (wxTreeTextCtrl::OnChar
)
193 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
196 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
, const wxWindowID id
,
197 bool *accept
, wxString
*res
, wxTreeCtrl
*owner
,
198 const wxString
&value
, const wxPoint
&pos
, const wxSize
&size
,
200 # if defined(__VISAGECPP__)
201 int style
, const wxValidator
* validator
, const wxString
&name
) :
203 int style
, const wxValidator
& validator
, const wxString
&name
) :
206 wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
213 m_startValue
= value
;
216 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
218 if (event
.m_keyCode
== WXK_RETURN
)
221 (*m_res
) = GetValue();
225 if (event
.m_keyCode
== WXK_ESCAPE
)
235 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
237 if (wxPendingDelete
.Member(this)) return;
239 wxPendingDelete
.Append(this);
241 if ((*m_accept
) && ((*m_res
) != m_startValue
))
242 m_owner
->OnRenameAccept();
245 #define PIXELS_PER_UNIT 10
246 // -----------------------------------------------------------------------------
248 // -----------------------------------------------------------------------------
250 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
252 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
253 : wxNotifyEvent( commandType
, id
)
256 m_itemOld
= (wxGenericTreeItem
*)NULL
;
259 // -----------------------------------------------------------------------------
261 // -----------------------------------------------------------------------------
263 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
264 const wxString
& text
,
266 int image
, int selImage
,
267 wxTreeItemData
*data
)
270 m_images
[wxTreeItemIcon_Normal
] = image
;
271 m_images
[wxTreeItemIcon_Selected
] = selImage
;
272 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
273 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
277 m_xCross
= m_yCross
= 0;
281 m_isCollapsed
= TRUE
;
282 m_hasHilight
= FALSE
;
288 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
289 // TODO : Add the width of the image
290 // PB : We don't know which image is shown (image, selImage)
291 // We don't even know imageList from the treectrl this item belongs to !!!
292 // At this point m_width doesn't mean much, this can be remove !
295 wxGenericTreeItem::~wxGenericTreeItem()
299 wxASSERT_MSG( m_children
.IsEmpty(),
300 wxT("please call DeleteChildren() before deleting the item") );
303 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
305 size_t count
= m_children
.Count();
306 for ( size_t n
= 0; n
< count
; n
++ )
308 wxGenericTreeItem
*child
= m_children
[n
];
311 tree
->SendDeleteEvent(child
);
314 child
->DeleteChildren(tree
);
321 void wxGenericTreeItem::SetText( const wxString
&text
)
326 void wxGenericTreeItem::Reset()
329 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
331 m_images
[i
] = NO_IMAGE
;
336 m_height
= m_width
= 0;
343 m_isCollapsed
= TRUE
;
345 m_parent
= (wxGenericTreeItem
*)NULL
;
348 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
350 size_t count
= m_children
.Count();
354 size_t total
= count
;
355 for ( size_t n
= 0; n
< count
; ++n
)
357 total
+= m_children
[n
]->GetChildrenCount();
363 void wxGenericTreeItem::SetCross( int x
, int y
)
369 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
371 int bottomY
=m_y
+theTree
->GetLineHeight(this);
372 if ( y
< bottomY
) y
= bottomY
;
373 int width
= m_x
+ m_width
;
374 if ( x
< width
) x
= width
;
378 size_t count
= m_children
.Count();
379 for ( size_t n
= 0; n
< count
; ++n
)
381 m_children
[n
]->GetSize( x
, y
, theTree
);
386 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
387 const wxTreeCtrl
*theTree
,
390 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
392 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2)
393 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
395 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
397 // 5 is the size of the plus sign
398 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
399 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
400 (IsExpanded() || HasPlus()))
402 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
406 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
411 // assuming every image (normal and selected ) has the same size !
412 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
413 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
415 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
416 flags
|= wxTREE_HITTEST_ONITEMICON
;
418 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
424 flags
|= wxTREE_HITTEST_ONITEMIDENT
;
425 if (point
.x
> m_x
+m_width
)
426 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
434 size_t count
= m_children
.Count();
435 for ( size_t n
= 0; n
< count
; n
++ )
437 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
444 flags
|=wxTREE_HITTEST_NOWHERE
;
448 int wxGenericTreeItem::GetCurrentImage() const
450 int image
= NO_IMAGE
;
455 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
458 if ( image
== NO_IMAGE
)
460 // we usually fall back to the normal item, but try just the
461 // expanded one (and not selected) first in this case
462 image
= GetImage(wxTreeItemIcon_Expanded
);
468 image
= GetImage(wxTreeItemIcon_Selected
);
471 // may be it doesn't have the specific image we want, try the default one
473 if ( image
== NO_IMAGE
)
481 // -----------------------------------------------------------------------------
482 // wxTreeCtrl implementation
483 // -----------------------------------------------------------------------------
485 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
487 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
488 EVT_PAINT (wxTreeCtrl::OnPaint
)
489 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
490 EVT_CHAR (wxTreeCtrl::OnChar
)
491 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
492 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
493 EVT_IDLE (wxTreeCtrl::OnIdle
)
496 // -----------------------------------------------------------------------------
497 // construction/destruction
498 // -----------------------------------------------------------------------------
500 void wxTreeCtrl::Init()
504 m_anchor
= (wxGenericTreeItem
*) NULL
;
514 m_hilightBrush
= new wxBrush
516 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
521 m_imageListState
= (wxImageList
*) NULL
;
525 m_renameTimer
= new wxTreeRenameTimer( this );
527 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
528 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
529 m_normalFont
.GetFamily(),
530 m_normalFont
.GetStyle(),
532 m_normalFont
.GetUnderlined());
535 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
536 const wxPoint
& pos
, const wxSize
& size
,
539 # if defined(__VISAGECPP__)
540 const wxValidator
*validator
,
542 const wxValidator
&validator
,
545 const wxString
& name
)
549 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
552 SetValidator( validator
);
555 SetBackgroundColour( *wxWHITE
);
556 // m_dottedPen = wxPen( "grey", 0, wxDOT );
557 m_dottedPen
= wxPen( "grey", 0, 0 );
562 wxTreeCtrl::~wxTreeCtrl()
564 wxDELETE( m_hilightBrush
);
568 delete m_renameTimer
;
571 // -----------------------------------------------------------------------------
573 // -----------------------------------------------------------------------------
575 size_t wxTreeCtrl::GetCount() const
577 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
580 void wxTreeCtrl::SetIndent(unsigned int indent
)
587 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
594 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
596 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
598 return item
.m_pItem
->GetChildrenCount(recursively
);
601 // -----------------------------------------------------------------------------
602 // functions to work with tree items
603 // -----------------------------------------------------------------------------
605 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
607 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
609 return item
.m_pItem
->GetText();
612 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
613 wxTreeItemIcon which
) const
615 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
617 return item
.m_pItem
->GetImage(which
);
620 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
622 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
624 return item
.m_pItem
->GetData();
627 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
629 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
632 wxGenericTreeItem
*pItem
= item
.m_pItem
;
633 pItem
->SetText(text
);
634 CalculateSize(pItem
, dc
);
638 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
640 wxTreeItemIcon which
)
642 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
644 wxGenericTreeItem
*pItem
= item
.m_pItem
;
645 pItem
->SetImage(image
, which
);
648 CalculateSize(pItem
, dc
);
652 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
654 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
656 item
.m_pItem
->SetData(data
);
659 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
661 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
663 wxGenericTreeItem
*pItem
= item
.m_pItem
;
664 pItem
->SetHasPlus(has
);
668 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
670 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
672 // avoid redrawing the tree if no real change
673 wxGenericTreeItem
*pItem
= item
.m_pItem
;
674 if ( pItem
->IsBold() != bold
)
676 pItem
->SetBold(bold
);
681 // -----------------------------------------------------------------------------
682 // item status inquiries
683 // -----------------------------------------------------------------------------
685 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
687 wxFAIL_MSG(wxT("not implemented"));
692 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
694 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
696 return !item
.m_pItem
->GetChildren().IsEmpty();
699 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
701 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
703 return item
.m_pItem
->IsExpanded();
706 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
708 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
710 return item
.m_pItem
->IsSelected();
713 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
715 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
717 return item
.m_pItem
->IsBold();
720 // -----------------------------------------------------------------------------
722 // -----------------------------------------------------------------------------
724 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
726 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
728 return item
.m_pItem
->GetParent();
731 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
733 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
736 return GetNextChild(item
, cookie
);
739 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
741 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
743 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
744 if ( (size_t)cookie
< children
.Count() )
746 return children
.Item(cookie
++);
750 // there are no more of them
751 return wxTreeItemId();
755 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
757 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
759 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
760 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
763 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
765 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
767 wxGenericTreeItem
*i
= item
.m_pItem
;
768 wxGenericTreeItem
*parent
= i
->GetParent();
769 if ( parent
== NULL
)
771 // root item doesn't have any siblings
772 return wxTreeItemId();
775 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
776 int index
= siblings
.Index(i
);
777 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
779 size_t n
= (size_t)(index
+ 1);
780 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
783 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
785 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
787 wxGenericTreeItem
*i
= item
.m_pItem
;
788 wxGenericTreeItem
*parent
= i
->GetParent();
789 if ( parent
== NULL
)
791 // root item doesn't have any siblings
792 return wxTreeItemId();
795 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
796 int index
= siblings
.Index(i
);
797 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
799 return index
== 0 ? wxTreeItemId()
800 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
803 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
805 wxFAIL_MSG(wxT("not implemented"));
807 return wxTreeItemId();
810 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
812 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
814 wxFAIL_MSG(wxT("not implemented"));
816 return wxTreeItemId();
819 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
821 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
823 wxFAIL_MSG(wxT("not implemented"));
825 return wxTreeItemId();
828 // -----------------------------------------------------------------------------
830 // -----------------------------------------------------------------------------
832 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
834 const wxString
& text
,
835 int image
, int selImage
,
836 wxTreeItemData
*data
)
838 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
841 // should we give a warning here?
842 return AddRoot(text
, image
, selImage
, data
);
846 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
853 data
->m_pItem
= item
;
856 parent
->Insert( item
, previous
);
863 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
864 int image
, int selImage
,
865 wxTreeItemData
*data
)
867 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
870 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
871 image
, selImage
, data
);
874 data
->m_pItem
= m_anchor
;
878 AdjustMyScrollbars();
883 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
884 const wxString
& text
,
885 int image
, int selImage
,
886 wxTreeItemData
*data
)
888 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
891 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
892 const wxTreeItemId
& idPrevious
,
893 const wxString
& text
,
894 int image
, int selImage
,
895 wxTreeItemData
*data
)
897 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
900 // should we give a warning here?
901 return AddRoot(text
, image
, selImage
, data
);
904 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
905 wxASSERT_MSG( index
!= wxNOT_FOUND
,
906 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
907 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
910 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
911 const wxString
& text
,
912 int image
, int selImage
,
913 wxTreeItemData
*data
)
915 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
918 // should we give a warning here?
919 return AddRoot(text
, image
, selImage
, data
);
922 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
923 image
, selImage
, data
);
926 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
928 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
930 event
.SetEventObject( this );
931 ProcessEvent( event
);
934 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
936 wxGenericTreeItem
*item
= itemId
.m_pItem
;
937 item
->DeleteChildren(this);
942 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
944 wxGenericTreeItem
*item
= itemId
.m_pItem
;
945 wxGenericTreeItem
*parent
= item
->GetParent();
949 parent
->GetChildren().Remove(item
);
952 item
->DeleteChildren(this);
953 SendDeleteEvent(item
);
959 void wxTreeCtrl::DeleteAllItems()
963 m_anchor
->DeleteChildren(this);
972 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
974 wxGenericTreeItem
*item
= itemId
.m_pItem
;
976 if ( !item
->HasPlus() )
979 if ( item
->IsExpanded() )
982 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
984 event
.SetEventObject( this );
986 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
987 if ( ProcessEvent( event
) && !event
.IsAllowed() )
989 // cancelled by program
994 CalculatePositions();
996 RefreshSubtree(item
);
998 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
999 ProcessEvent( event
);
1002 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1004 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1006 if ( !item
->IsExpanded() )
1009 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1010 event
.m_item
= item
;
1011 event
.SetEventObject( this );
1012 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1014 // cancelled by program
1020 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1021 size_t count
= children
.Count();
1022 for ( size_t n
= 0; n
< count
; n
++ )
1024 Collapse(children
[n
]);
1027 CalculatePositions();
1029 RefreshSubtree(item
);
1031 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1032 ProcessEvent( event
);
1035 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1038 DeleteChildren(item
);
1041 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1043 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1045 if ( item
->IsExpanded() )
1051 void wxTreeCtrl::Unselect()
1055 m_current
->SetHilight( FALSE
);
1056 RefreshLine( m_current
);
1060 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1062 item
->SetHilight(FALSE
);
1065 if (item
->HasChildren())
1067 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1068 size_t count
= children
.Count();
1069 for ( size_t n
= 0; n
< count
; ++n
)
1070 UnselectAllChildren(children
[n
]);
1074 void wxTreeCtrl::UnselectAll()
1076 UnselectAllChildren(GetRootItem().m_pItem
);
1079 // Recursive function !
1080 // To stop we must have crt_item<last_item
1082 // Tag all next children, when no more children,
1083 // Move to parent (not to tag)
1084 // Keep going... if we found last_item, we stop.
1085 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1087 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1089 if ( parent
== NULL
) // This is root item
1090 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1092 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1093 int index
= children
.Index(crt_item
);
1094 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1096 size_t count
= children
.Count();
1097 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1098 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1100 return TagNextChildren(parent
, last_item
, select
);
1103 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1105 crt_item
->SetHilight(select
);
1106 RefreshLine(crt_item
);
1108 if (crt_item
==last_item
) return TRUE
;
1110 if (crt_item
->HasChildren())
1112 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1113 size_t count
= children
.Count();
1114 for ( size_t n
= 0; n
< count
; ++n
)
1115 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1121 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1123 // item2 is not necessary after item1
1124 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1126 // choice first' and 'last' between item1 and item2
1127 if (item1
->GetY()<item2
->GetY())
1138 bool select
= m_current
->IsSelected();
1140 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1143 TagNextChildren(first
,last
,select
);
1146 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1147 bool unselect_others
,
1148 bool extended_select
)
1150 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1152 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1153 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1155 //wxCHECK_RET( ( (!unselect_others) && is_single),
1156 // wxT("this is a single selection tree") );
1158 // to keep going anyhow !!!
1161 if (item
->IsSelected())
1162 return; // nothing to do
1163 unselect_others
= TRUE
;
1164 extended_select
= FALSE
;
1166 else if ( unselect_others
&& item
->IsSelected() )
1168 // selection change if there is more than one item currently selected
1169 wxArrayTreeItemIds selected_items
;
1170 if ( GetSelections(selected_items
) == 1 )
1174 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1175 event
.m_item
= item
;
1176 event
.m_itemOld
= m_current
;
1177 event
.SetEventObject( this );
1178 // TODO : Here we don't send any selection mode yet !
1180 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1184 if (unselect_others
)
1186 if (is_single
) Unselect(); // to speed up thing
1191 if (extended_select
)
1193 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1194 // don't change the mark (m_current)
1195 SelectItemRange(m_current
, item
);
1199 bool select
=TRUE
; // the default
1201 // Check if we need to toggle hilight (ctrl mode)
1202 if (!unselect_others
)
1203 select
=!item
->IsSelected();
1205 m_current
= m_key_current
= item
;
1206 m_current
->SetHilight(select
);
1207 RefreshLine( m_current
);
1210 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1211 GetEventHandler()->ProcessEvent( event
);
1214 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1215 wxArrayTreeItemIds
&array
) const
1217 if ( item
->IsSelected() )
1218 array
.Add(wxTreeItemId(item
));
1220 if ( item
->HasChildren() )
1222 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1223 size_t count
= children
.GetCount();
1224 for ( size_t n
= 0; n
< count
; ++n
)
1225 FillArray(children
[n
],array
);
1229 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1232 FillArray(GetRootItem().m_pItem
, array
);
1234 return array
.Count();
1237 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1239 if (!item
.IsOk()) return;
1241 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1243 // first expand all parent branches
1244 wxGenericTreeItem
*parent
= gitem
->GetParent();
1248 parent
= parent
->GetParent();
1251 //if (parent) CalculatePositions();
1256 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1258 if (!item
.IsOk()) return;
1260 // We have to call this here because the label in
1261 // question might just have been added and no screen
1262 // update taken place.
1263 if (m_dirty
) wxYield();
1265 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1267 // now scroll to the item
1268 int item_y
= gitem
->GetY();
1272 ViewStart( &start_x
, &start_y
);
1273 start_y
*= PIXELS_PER_UNIT
;
1277 GetClientSize( &client_w
, &client_h
);
1279 if (item_y
< start_y
+3)
1284 m_anchor
->GetSize( x
, y
, this );
1285 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1286 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1287 // Item should appear at top
1288 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1290 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1295 m_anchor
->GetSize( x
, y
, this );
1296 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1297 item_y
+= PIXELS_PER_UNIT
+2;
1298 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1299 // Item should appear at bottom
1300 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
);
1304 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1305 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1307 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1308 wxGenericTreeItem
**item2
)
1310 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1312 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1315 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1316 const wxTreeItemId
& item2
)
1318 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1321 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1323 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1325 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1327 wxCHECK_RET( !s_treeBeingSorted
,
1328 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1330 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1331 if ( children
.Count() > 1 )
1333 s_treeBeingSorted
= this;
1334 children
.Sort(tree_ctrl_compare_func
);
1335 s_treeBeingSorted
= NULL
;
1339 //else: don't make the tree dirty as nothing changed
1342 wxImageList
*wxTreeCtrl::GetImageList() const
1344 return m_imageListNormal
;
1347 wxImageList
*wxTreeCtrl::GetStateImageList() const
1349 return m_imageListState
;
1352 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1354 m_imageListNormal
= imageList
;
1356 // Calculate a m_lineHeight value from the image sizes.
1357 // May be toggle off. Then wxTreeCtrl will spread when
1358 // necessary (which might look ugly).
1360 wxClientDC
dc(this);
1361 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1365 n
= m_imageListNormal
->GetImageCount();
1366 for(int i
= 0; i
< n
; i
++)
1368 m_imageListNormal
->GetSize(i
, width
, height
);
1369 if(height
> m_lineHeight
) m_lineHeight
= height
;
1372 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1373 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1378 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1380 m_imageListState
= imageList
;
1383 // -----------------------------------------------------------------------------
1385 // -----------------------------------------------------------------------------
1387 void wxTreeCtrl::AdjustMyScrollbars()
1393 m_anchor
->GetSize( x
, y
, this );
1394 //y += GetLineHeight(m_anchor);
1395 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1396 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1397 int y_pos
= GetScrollPos( wxVERTICAL
);
1398 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1402 SetScrollbars( 0, 0, 0, 0 );
1406 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1408 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1409 return item
->GetHeight();
1411 return m_lineHeight
;
1414 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1417 dc
.SetFont(m_boldFont
);
1421 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1425 int image
= item
->GetCurrentImage();
1426 if ( image
!= NO_IMAGE
)
1428 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1432 int total_h
= GetLineHeight(item
);
1434 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1436 if ( image
!= NO_IMAGE
)
1438 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1439 m_imageListNormal
->Draw( image
, dc
,
1441 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1442 wxIMAGELIST_DRAW_TRANSPARENT
);
1443 dc
.DestroyClippingRegion();
1446 dc
.SetBackgroundMode(wxTRANSPARENT
);
1447 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1448 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1450 // restore normal font
1451 dc
.SetFont( m_normalFont
);
1454 // Now y stands for the top of the item, whereas it used to stand for middle !
1455 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1457 int horizX
= level
*m_indent
;
1459 item
->SetX( horizX
+m_indent
+m_spacing
);
1463 y
+=GetLineHeight(item
)/2;
1465 item
->SetCross( horizX
+m_indent
, y
);
1467 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1468 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1470 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1472 int startX
= horizX
;
1473 int endX
= horizX
+ (m_indent
-5);
1475 // if (!item->HasChildren()) endX += (m_indent+5);
1476 if (!item
->HasChildren()) endX
+= 20;
1478 dc
.DrawLine( startX
, y
, endX
, y
);
1480 if (item
->HasPlus())
1482 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1483 dc
.SetPen( *wxGREY_PEN
);
1484 dc
.SetBrush( *wxWHITE_BRUSH
);
1485 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1487 dc
.SetPen( *wxBLACK_PEN
);
1488 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1489 if (!item
->IsExpanded())
1490 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1492 dc
.SetPen( m_dottedPen
);
1495 if (item
->IsSelected())
1497 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1499 dc
.SetBrush( *m_hilightBrush
);
1502 dc
.SetPen( *wxBLACK_PEN
);
1504 dc
.SetPen( *wxTRANSPARENT_PEN
);
1506 PaintItem(item
, dc
);
1508 dc
.SetPen( m_dottedPen
);
1509 dc
.SetTextForeground( *wxBLACK
);
1510 dc
.SetBrush( *wxWHITE_BRUSH
);
1514 dc
.SetBrush( *wxWHITE_BRUSH
);
1515 dc
.SetPen( *wxTRANSPARENT_PEN
);
1517 PaintItem(item
, dc
);
1519 dc
.SetPen( m_dottedPen
);
1523 y
= oldY
+GetLineHeight(item
);
1525 if (item
->IsExpanded())
1527 oldY
+=GetLineHeight(item
)/2;
1528 int semiOldY
=y
; // (=y) for stupid compilator
1530 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1531 size_t n
, count
= children
.Count();
1532 for ( n
= 0; n
< count
; ++n
)
1535 PaintLevel( children
[n
], dc
, level
+1, y
);
1538 // it may happen that the item is expanded but has no items (when you
1539 // delete all its children for example) - don't draw the vertical line
1543 semiOldY
+=GetLineHeight(children
[--n
])/2;
1544 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1549 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1553 wxGenericTreeItem
*i
=item
.m_pItem
;
1555 wxClientDC
dc(this);
1557 dc
.SetLogicalFunction(wxINVERT
);
1560 ViewStart(&x
,&h
); // we only need x
1561 GetClientSize(&w
,&h
); // we only need w
1563 h
=GetLineHeight(i
)+1;
1564 // 2 white column at border
1565 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1568 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1572 wxGenericTreeItem
*i
=item
.m_pItem
;
1574 wxClientDC
dc(this);
1576 dc
.SetLogicalFunction(wxINVERT
);
1581 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1584 dc
.DrawLine( 0, y
, w
, y
);
1587 // -----------------------------------------------------------------------------
1588 // wxWindows callbacks
1589 // -----------------------------------------------------------------------------
1591 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1599 dc
.SetFont( m_normalFont
);
1600 dc
.SetPen( m_dottedPen
);
1602 // this is now done dynamically
1603 //if(GetImageList() == NULL)
1604 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1607 PaintLevel( m_anchor
, dc
, 0, y
);
1610 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1614 if (m_current
) RefreshLine( m_current
);
1617 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1621 if (m_current
) RefreshLine( m_current
);
1624 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1626 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1627 te
.m_code
= event
.KeyCode();
1628 te
.SetEventObject( this );
1629 GetEventHandler()->ProcessEvent( te
);
1631 if ( (m_current
== 0) || (m_key_current
== 0) )
1637 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1638 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1639 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1641 switch (event
.KeyCode())
1645 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1653 if (IsExpanded(m_current
))
1655 Collapse(m_current
);
1667 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1668 event
.m_item
= m_current
;
1670 event
.SetEventObject( this );
1671 GetEventHandler()->ProcessEvent( event
);
1675 // up goes to the previous sibling or to the last of its children if
1679 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1682 prev
= GetParent( m_key_current
);
1686 wxTreeItemId current
= m_key_current
;
1687 if (current
== GetFirstChild( prev
, cockie
))
1689 // otherwise we return to where we came from
1690 SelectItem( prev
, unselect_others
, extended_select
);
1691 m_key_current
=prev
.m_pItem
;
1692 EnsureVisible( prev
);
1699 while ( IsExpanded(prev
) && HasChildren(prev
) )
1701 wxTreeItemId child
= GetLastChild(prev
);
1708 SelectItem( prev
, unselect_others
, extended_select
);
1709 m_key_current
=prev
.m_pItem
;
1710 EnsureVisible( prev
);
1715 // left arrow goes to the parent
1718 wxTreeItemId prev
= GetParent( m_current
);
1721 EnsureVisible( prev
);
1722 SelectItem( prev
, unselect_others
, extended_select
);
1728 // this works the same as the down arrow except that we also expand the
1729 // item if it wasn't expanded yet
1735 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1738 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1739 SelectItem( child
, unselect_others
, extended_select
);
1740 m_key_current
=child
.m_pItem
;
1741 EnsureVisible( child
);
1745 wxTreeItemId next
= GetNextSibling( m_key_current
);
1749 wxTreeItemId current
= m_key_current
;
1750 while (current
&& !next
)
1752 current
= GetParent( current
);
1753 if (current
) next
= GetNextSibling( current
);
1759 SelectItem( next
, unselect_others
, extended_select
);
1760 m_key_current
=next
.m_pItem
;
1761 EnsureVisible( next
);
1767 // <End> selects the last visible tree item
1770 wxTreeItemId last
= GetRootItem();
1772 while ( last
.IsOk() && IsExpanded(last
) )
1774 wxTreeItemId lastChild
= GetLastChild(last
);
1776 // it may happen if the item was expanded but then all of
1777 // its children have been deleted - so IsExpanded() returned
1778 // TRUE, but GetLastChild() returned invalid item
1787 EnsureVisible( last
);
1788 SelectItem( last
, unselect_others
, extended_select
);
1793 // <Home> selects the root item
1796 wxTreeItemId prev
= GetRootItem();
1799 EnsureVisible( prev
);
1800 SelectItem( prev
, unselect_others
, extended_select
);
1810 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1812 // We have to call this here because the label in
1813 // question might just have been added and no screen
1814 // update taken place.
1815 if (m_dirty
) wxYield();
1817 wxClientDC
dc(this);
1819 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1820 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1825 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1826 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1827 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1828 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1830 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1835 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1837 if (!item
.IsOk()) return;
1839 m_currentEdit
= item
.m_pItem
;
1841 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1842 te
.m_item
= m_currentEdit
;
1843 te
.SetEventObject( this );
1844 GetEventHandler()->ProcessEvent( te
);
1846 if (!te
.IsAllowed()) return;
1848 // We have to call this here because the label in
1849 // question might just have been added and no screen
1850 // update taken place.
1851 if (m_dirty
) wxYield();
1853 wxString s
= m_currentEdit
->GetText();
1854 int x
= m_currentEdit
->GetX();
1855 int y
= m_currentEdit
->GetY();
1856 int w
= m_currentEdit
->GetWidth();
1857 int h
= m_currentEdit
->GetHeight();
1862 int image
= m_currentEdit
->GetCurrentImage();
1863 if ( image
!= NO_IMAGE
)
1865 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1869 w
-= image_w
+ 4; // I don't know why +4 is needed
1871 wxClientDC
dc(this);
1873 x
= dc
.LogicalToDeviceX( x
);
1874 y
= dc
.LogicalToDeviceY( y
);
1876 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1877 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1881 void wxTreeCtrl::OnRenameTimer()
1886 void wxTreeCtrl::OnRenameAccept()
1888 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1889 le
.m_item
= m_currentEdit
;
1890 le
.SetEventObject( this );
1891 le
.m_label
= m_renameRes
;
1892 GetEventHandler()->ProcessEvent( le
);
1894 if (!le
.IsAllowed()) return;
1896 SetItemText( m_currentEdit
, m_renameRes
);
1899 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1901 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1903 if ( !m_anchor
) return;
1905 wxClientDC
dc(this);
1907 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1908 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1911 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1912 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1914 if (event
.Dragging())
1916 if (m_dragCount
== 0)
1917 m_dragStart
= wxPoint(x
,y
);
1921 if (m_dragCount
!= 3) return;
1923 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1924 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1926 wxTreeEvent
nevent( command
, GetId() );
1927 nevent
.m_item
= m_current
;
1928 nevent
.SetEventObject(this);
1929 GetEventHandler()->ProcessEvent(nevent
);
1937 if (item
== NULL
) return; /* we hit the blank area */
1939 if (event
.RightDown()) {
1940 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
1943 nevent
.SetEventObject(this);
1944 GetEventHandler()->ProcessEvent(nevent
);
1948 if (event
.LeftUp() && (item
== m_current
) &&
1949 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
1950 HasFlag(wxTR_EDIT_LABELS
) )
1952 m_renameTimer
->Start( 100, TRUE
);
1956 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1957 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1958 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1967 SelectItem(item
, unselect_others
, extended_select
);
1969 if (event
.LeftDClick())
1971 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1972 event
.m_item
= item
;
1974 event
.SetEventObject( this );
1975 GetEventHandler()->ProcessEvent( event
);
1979 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1981 /* after all changes have been done to the tree control,
1982 * we actually redraw the tree when everything is over */
1989 CalculatePositions();
1991 AdjustMyScrollbars();
1994 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2000 dc
.SetFont(m_boldFont
);
2002 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2005 // restore normal font
2006 dc
.SetFont( m_normalFont
);
2010 int image
= item
->GetCurrentImage();
2011 if ( image
!= NO_IMAGE
)
2013 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2017 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2019 if (total_h
<40) total_h
+=4; // at least 4 pixels
2020 else total_h
+=total_h
/10; // otherwise 10% extra spacing
2022 item
->SetHeight(total_h
);
2023 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2025 item
->SetWidth(image_w
+text_w
+2);
2028 // -----------------------------------------------------------------------------
2029 // for developper : y is now the top of the level
2030 // not the middle of it !
2031 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2033 int horizX
= level
*m_indent
;
2035 CalculateSize( item
, dc
);
2038 item
->SetX( horizX
+m_indent
+m_spacing
);
2040 y
+=GetLineHeight(item
);
2042 if ( !item
->IsExpanded() )
2044 // we dont need to calculate collapsed branches
2048 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2049 size_t n
, count
= children
.Count();
2050 for (n
= 0; n
< count
; ++n
)
2051 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2054 void wxTreeCtrl::CalculatePositions()
2056 if ( !m_anchor
) return;
2058 wxClientDC
dc(this);
2061 dc
.SetFont( m_normalFont
);
2063 dc
.SetPen( m_dottedPen
);
2064 //if(GetImageList() == NULL)
2065 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2068 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2071 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2073 wxClientDC
dc(this);
2078 GetClientSize( &cw
, &ch
);
2081 rect
.x
= dc
.LogicalToDeviceX( 0 );
2083 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2086 Refresh( TRUE
, &rect
);
2088 AdjustMyScrollbars();
2091 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2093 wxClientDC
dc(this);
2098 GetClientSize( &cw
, &ch
);
2101 rect
.x
= dc
.LogicalToDeviceX( 0 );
2102 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2104 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2106 Refresh( TRUE
, &rect
);