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
,
199 int style
, const wxValidator
& validator
, const wxString
&name
) :
200 wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
207 m_startValue
= value
;
210 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
212 if (event
.m_keyCode
== WXK_RETURN
)
215 (*m_res
) = GetValue();
219 if (event
.m_keyCode
== WXK_ESCAPE
)
229 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
231 if (wxPendingDelete
.Member(this)) return;
233 wxPendingDelete
.Append(this);
235 if ((*m_accept
) && ((*m_res
) != m_startValue
))
236 m_owner
->OnRenameAccept();
239 #define PIXELS_PER_UNIT 10
240 // -----------------------------------------------------------------------------
242 // -----------------------------------------------------------------------------
244 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
246 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
247 : wxNotifyEvent( commandType
, id
)
250 m_itemOld
= (wxGenericTreeItem
*)NULL
;
253 // -----------------------------------------------------------------------------
255 // -----------------------------------------------------------------------------
257 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
258 const wxString
& text
,
260 int image
, int selImage
,
261 wxTreeItemData
*data
)
264 m_images
[wxTreeItemIcon_Normal
] = image
;
265 m_images
[wxTreeItemIcon_Selected
] = selImage
;
266 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
267 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
271 m_xCross
= m_yCross
= 0;
275 m_isCollapsed
= TRUE
;
276 m_hasHilight
= FALSE
;
282 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
283 // TODO : Add the width of the image
284 // PB : We don't know which image is shown (image, selImage)
285 // We don't even know imageList from the treectrl this item belongs to !!!
286 // At this point m_width doesn't mean much, this can be remove !
289 wxGenericTreeItem::~wxGenericTreeItem()
293 wxASSERT_MSG( m_children
.IsEmpty(),
294 T("please call DeleteChildren() before deleting the item") );
297 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
299 size_t count
= m_children
.Count();
300 for ( size_t n
= 0; n
< count
; n
++ )
302 wxGenericTreeItem
*child
= m_children
[n
];
305 tree
->SendDeleteEvent(child
);
308 child
->DeleteChildren(tree
);
315 void wxGenericTreeItem::SetText( const wxString
&text
)
320 void wxGenericTreeItem::Reset()
323 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
325 m_images
[i
] = NO_IMAGE
;
330 m_height
= m_width
= 0;
337 m_isCollapsed
= TRUE
;
339 m_parent
= (wxGenericTreeItem
*)NULL
;
342 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
344 size_t count
= m_children
.Count();
348 size_t total
= count
;
349 for ( size_t n
= 0; n
< count
; ++n
)
351 total
+= m_children
[n
]->GetChildrenCount();
357 void wxGenericTreeItem::SetCross( int x
, int y
)
363 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
365 int bottomY
=m_y
+theTree
->GetLineHeight(this);
366 if ( y
< bottomY
) y
= bottomY
;
367 int width
= m_x
+ m_width
;
368 if ( x
< width
) x
= width
;
372 size_t count
= m_children
.Count();
373 for ( size_t n
= 0; n
< count
; ++n
)
375 m_children
[n
]->GetSize( x
, y
, theTree
);
380 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
381 const wxTreeCtrl
*theTree
,
384 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
386 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2)
387 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
389 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
391 // 5 is the size of the plus sign
392 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
393 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
394 (IsExpanded() || HasPlus()))
396 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
400 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
405 // assuming every image (normal and selected ) has the same size !
406 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
407 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
409 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
410 flags
|= wxTREE_HITTEST_ONITEMICON
;
412 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
418 flags
|= wxTREE_HITTEST_ONITEMIDENT
;
419 if (point
.x
> m_x
+m_width
)
420 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
428 size_t count
= m_children
.Count();
429 for ( size_t n
= 0; n
< count
; n
++ )
431 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
438 flags
|=wxTREE_HITTEST_NOWHERE
;
442 int wxGenericTreeItem::GetCurrentImage() const
444 int image
= NO_IMAGE
;
449 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
452 if ( image
== NO_IMAGE
)
454 // we usually fall back to the normal item, but try just the
455 // expanded one (and not selected) first in this case
456 image
= GetImage(wxTreeItemIcon_Expanded
);
462 image
= GetImage(wxTreeItemIcon_Selected
);
465 // may be it doesn't have the specific image we want, try the default one
467 if ( image
== NO_IMAGE
)
475 // -----------------------------------------------------------------------------
476 // wxTreeCtrl implementation
477 // -----------------------------------------------------------------------------
479 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
481 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
482 EVT_PAINT (wxTreeCtrl::OnPaint
)
483 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
484 EVT_CHAR (wxTreeCtrl::OnChar
)
485 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
486 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
487 EVT_IDLE (wxTreeCtrl::OnIdle
)
490 // -----------------------------------------------------------------------------
491 // construction/destruction
492 // -----------------------------------------------------------------------------
494 void wxTreeCtrl::Init()
498 m_anchor
= (wxGenericTreeItem
*) NULL
;
508 m_hilightBrush
= new wxBrush
510 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
515 m_imageListState
= (wxImageList
*) NULL
;
519 m_renameTimer
= new wxTreeRenameTimer( this );
522 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
523 const wxPoint
& pos
, const wxSize
& size
,
525 const wxValidator
&validator
,
526 const wxString
& name
)
530 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
533 SetValidator( validator
);
536 SetBackgroundColour( *wxWHITE
);
537 // m_dottedPen = wxPen( "grey", 0, wxDOT );
538 m_dottedPen
= wxPen( "grey", 0, 0 );
543 wxTreeCtrl::~wxTreeCtrl()
545 wxDELETE( m_hilightBrush
);
549 delete m_renameTimer
;
552 // -----------------------------------------------------------------------------
554 // -----------------------------------------------------------------------------
556 size_t wxTreeCtrl::GetCount() const
558 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
561 void wxTreeCtrl::SetIndent(unsigned int indent
)
568 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
575 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
577 wxCHECK_MSG( item
.IsOk(), 0u, T("invalid tree item") );
579 return item
.m_pItem
->GetChildrenCount(recursively
);
582 // -----------------------------------------------------------------------------
583 // functions to work with tree items
584 // -----------------------------------------------------------------------------
586 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
588 wxCHECK_MSG( item
.IsOk(), T(""), T("invalid tree item") );
590 return item
.m_pItem
->GetText();
593 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
594 wxTreeItemIcon which
) const
596 wxCHECK_MSG( item
.IsOk(), -1, T("invalid tree item") );
598 return item
.m_pItem
->GetImage(which
);
601 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
603 wxCHECK_MSG( item
.IsOk(), NULL
, T("invalid tree item") );
605 return item
.m_pItem
->GetData();
608 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
610 wxCHECK_RET( item
.IsOk(), T("invalid tree item") );
613 wxGenericTreeItem
*pItem
= item
.m_pItem
;
614 pItem
->SetText(text
);
615 CalculateSize(pItem
, dc
);
619 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
621 wxTreeItemIcon which
)
623 wxCHECK_RET( item
.IsOk(), T("invalid tree item") );
625 wxGenericTreeItem
*pItem
= item
.m_pItem
;
626 pItem
->SetImage(image
, which
);
629 CalculateSize(pItem
, dc
);
633 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
635 wxCHECK_RET( item
.IsOk(), T("invalid tree item") );
637 item
.m_pItem
->SetData(data
);
640 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
642 wxCHECK_RET( item
.IsOk(), T("invalid tree item") );
644 wxGenericTreeItem
*pItem
= item
.m_pItem
;
645 pItem
->SetHasPlus(has
);
649 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
651 wxCHECK_RET( item
.IsOk(), T("invalid tree item") );
653 // avoid redrawing the tree if no real change
654 wxGenericTreeItem
*pItem
= item
.m_pItem
;
655 if ( pItem
->IsBold() != bold
)
657 pItem
->SetBold(bold
);
662 // -----------------------------------------------------------------------------
663 // item status inquiries
664 // -----------------------------------------------------------------------------
666 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
668 wxFAIL_MSG(T("not implemented"));
673 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
675 wxCHECK_MSG( item
.IsOk(), FALSE
, T("invalid tree item") );
677 return !item
.m_pItem
->GetChildren().IsEmpty();
680 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
682 wxCHECK_MSG( item
.IsOk(), FALSE
, T("invalid tree item") );
684 return item
.m_pItem
->IsExpanded();
687 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
689 wxCHECK_MSG( item
.IsOk(), FALSE
, T("invalid tree item") );
691 return item
.m_pItem
->IsSelected();
694 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
696 wxCHECK_MSG( item
.IsOk(), FALSE
, T("invalid tree item") );
698 return item
.m_pItem
->IsBold();
701 // -----------------------------------------------------------------------------
703 // -----------------------------------------------------------------------------
705 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
707 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), T("invalid tree item") );
709 return item
.m_pItem
->GetParent();
712 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
714 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), T("invalid tree item") );
717 return GetNextChild(item
, cookie
);
720 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
722 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), T("invalid tree item") );
724 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
725 if ( (size_t)cookie
< children
.Count() )
727 return children
.Item(cookie
++);
731 // there are no more of them
732 return wxTreeItemId();
736 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
738 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), T("invalid tree item") );
740 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
741 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
744 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
746 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), T("invalid tree item") );
748 wxGenericTreeItem
*i
= item
.m_pItem
;
749 wxGenericTreeItem
*parent
= i
->GetParent();
750 if ( parent
== NULL
)
752 // root item doesn't have any siblings
753 return wxTreeItemId();
756 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
757 int index
= siblings
.Index(i
);
758 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
760 size_t n
= (size_t)(index
+ 1);
761 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
764 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
766 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), T("invalid tree item") );
768 wxGenericTreeItem
*i
= item
.m_pItem
;
769 wxGenericTreeItem
*parent
= i
->GetParent();
770 if ( parent
== NULL
)
772 // root item doesn't have any siblings
773 return wxTreeItemId();
776 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
777 int index
= siblings
.Index(i
);
778 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
780 return index
== 0 ? wxTreeItemId()
781 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
784 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
786 wxFAIL_MSG(T("not implemented"));
788 return wxTreeItemId();
791 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
793 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), T("invalid tree item") );
795 wxFAIL_MSG(T("not implemented"));
797 return wxTreeItemId();
800 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
802 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), T("invalid tree item") );
804 wxFAIL_MSG(T("not implemented"));
806 return wxTreeItemId();
809 // -----------------------------------------------------------------------------
811 // -----------------------------------------------------------------------------
813 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
815 const wxString
& text
,
816 int image
, int selImage
,
817 wxTreeItemData
*data
)
819 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
822 // should we give a warning here?
823 return AddRoot(text
, image
, selImage
, data
);
827 wxGenericTreeItem
*item
= new wxGenericTreeItem(parent
,
834 data
->m_pItem
= item
;
837 parent
->Insert( item
, previous
);
844 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
845 int image
, int selImage
,
846 wxTreeItemData
*data
)
848 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), T("tree can have only one root") );
851 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
852 image
, selImage
, data
);
855 data
->m_pItem
= m_anchor
;
859 AdjustMyScrollbars();
864 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
865 const wxString
& text
,
866 int image
, int selImage
,
867 wxTreeItemData
*data
)
869 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
872 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
873 const wxTreeItemId
& idPrevious
,
874 const wxString
& text
,
875 int image
, int selImage
,
876 wxTreeItemData
*data
)
878 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
881 // should we give a warning here?
882 return AddRoot(text
, image
, selImage
, data
);
885 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
886 wxASSERT_MSG( index
!= wxNOT_FOUND
,
887 T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
888 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
891 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
892 const wxString
& text
,
893 int image
, int selImage
,
894 wxTreeItemData
*data
)
896 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
899 // should we give a warning here?
900 return AddRoot(text
, image
, selImage
, data
);
903 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
904 image
, selImage
, data
);
907 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
909 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
911 event
.SetEventObject( this );
912 ProcessEvent( event
);
915 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
917 wxGenericTreeItem
*item
= itemId
.m_pItem
;
918 item
->DeleteChildren(this);
923 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
925 wxGenericTreeItem
*item
= itemId
.m_pItem
;
926 wxGenericTreeItem
*parent
= item
->GetParent();
930 parent
->GetChildren().Remove(item
);
933 item
->DeleteChildren(this);
934 SendDeleteEvent(item
);
940 void wxTreeCtrl::DeleteAllItems()
944 m_anchor
->DeleteChildren(this);
953 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
955 wxGenericTreeItem
*item
= itemId
.m_pItem
;
957 if ( !item
->HasPlus() )
960 if ( item
->IsExpanded() )
963 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
965 event
.SetEventObject( this );
966 if ( ProcessEvent( event
) && event
.m_code
)
968 // cancelled by program
973 CalculatePositions();
975 RefreshSubtree(item
);
977 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
978 ProcessEvent( event
);
981 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
983 wxGenericTreeItem
*item
= itemId
.m_pItem
;
985 if ( !item
->IsExpanded() )
988 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
990 event
.SetEventObject( this );
991 if ( ProcessEvent( event
) && !event
.IsAllowed() )
993 // cancelled by program
999 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1000 size_t count
= children
.Count();
1001 for ( size_t n
= 0; n
< count
; n
++ )
1003 Collapse(children
[n
]);
1006 CalculatePositions();
1008 RefreshSubtree(item
);
1010 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1011 ProcessEvent( event
);
1014 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1017 DeleteChildren(item
);
1020 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1022 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1024 if ( item
->IsExpanded() )
1030 void wxTreeCtrl::Unselect()
1034 m_current
->SetHilight( FALSE
);
1035 RefreshLine( m_current
);
1039 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1041 item
->SetHilight(FALSE
);
1044 if (item
->HasChildren())
1046 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1047 size_t count
= children
.Count();
1048 for ( size_t n
= 0; n
< count
; ++n
)
1049 UnselectAllChildren(children
[n
]);
1053 void wxTreeCtrl::UnselectAll()
1055 UnselectAllChildren(GetRootItem().m_pItem
);
1058 // Recursive function !
1059 // To stop we must have crt_item<last_item
1061 // Tag all next children, when no more children,
1062 // Move to parent (not to tag)
1063 // Keep going... if we found last_item, we stop.
1064 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1066 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1068 if ( parent
== NULL
) // This is root item
1069 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1071 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1072 int index
= children
.Index(crt_item
);
1073 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1075 size_t count
= children
.Count();
1076 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1077 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1079 return TagNextChildren(parent
, last_item
, select
);
1082 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1084 crt_item
->SetHilight(select
);
1085 RefreshLine(crt_item
);
1087 if (crt_item
==last_item
) return TRUE
;
1089 if (crt_item
->HasChildren())
1091 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1092 size_t count
= children
.Count();
1093 for ( size_t n
= 0; n
< count
; ++n
)
1094 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1100 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1102 // item2 is not necessary after item1
1103 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1105 // choice first' and 'last' between item1 and item2
1106 if (item1
->GetY()<item2
->GetY())
1117 bool select
= m_current
->IsSelected();
1119 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1122 TagNextChildren(first
,last
,select
);
1125 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1126 bool unselect_others
,
1127 bool extended_select
)
1129 wxCHECK_RET( itemId
.IsOk(), T("invalid tree item") );
1131 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1132 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1134 //wxCHECK_RET( ( (!unselect_others) && is_single),
1135 // T("this is a single selection tree") );
1137 // to keep going anyhow !!!
1140 if (item
->IsSelected())
1141 return; // nothing to do
1142 unselect_others
= TRUE
;
1143 extended_select
= FALSE
;
1145 else if ( unselect_others
&& item
->IsSelected() )
1147 // selection change if there is more than one item currently selected
1148 wxArrayTreeItemIds selected_items
;
1149 if ( GetSelections(selected_items
) == 1 )
1153 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1154 event
.m_item
= item
;
1155 event
.m_itemOld
= m_current
;
1156 event
.SetEventObject( this );
1157 // TODO : Here we don't send any selection mode yet !
1159 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1163 if (unselect_others
)
1165 if (is_single
) Unselect(); // to speed up thing
1170 if (extended_select
)
1172 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1173 // don't change the mark (m_current)
1174 SelectItemRange(m_current
, item
);
1178 bool select
=TRUE
; // the default
1180 // Check if we need to toggle hilight (ctrl mode)
1181 if (!unselect_others
)
1182 select
=!item
->IsSelected();
1184 m_current
= m_key_current
= item
;
1185 m_current
->SetHilight(select
);
1186 RefreshLine( m_current
);
1189 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1190 GetEventHandler()->ProcessEvent( event
);
1193 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1194 wxArrayTreeItemIds
&array
) const
1196 if ( item
->IsSelected() )
1197 array
.Add(wxTreeItemId(item
));
1199 if ( item
->HasChildren() )
1201 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1202 size_t count
= children
.GetCount();
1203 for ( size_t n
= 0; n
< count
; ++n
)
1204 FillArray(children
[n
],array
);
1208 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1211 FillArray(GetRootItem().m_pItem
, array
);
1213 return array
.Count();
1216 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1218 if (!item
.IsOk()) return;
1220 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1222 // first expand all parent branches
1223 wxGenericTreeItem
*parent
= gitem
->GetParent();
1227 parent
= parent
->GetParent();
1230 //if (parent) CalculatePositions();
1235 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1237 if (!item
.IsOk()) return;
1239 // We have to call this here because the label in
1240 // question might just have been added and no screen
1241 // update taken place.
1242 if (m_dirty
) wxYield();
1244 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1246 // now scroll to the item
1247 int item_y
= gitem
->GetY();
1251 ViewStart( &start_x
, &start_y
);
1252 start_y
*= PIXELS_PER_UNIT
;
1256 GetClientSize( &client_w
, &client_h
);
1258 if (item_y
< start_y
+3)
1263 m_anchor
->GetSize( x
, y
, this );
1264 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1265 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1266 // Item should appear at top
1267 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1269 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1274 m_anchor
->GetSize( x
, y
, this );
1275 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1276 item_y
+= PIXELS_PER_UNIT
+2;
1277 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1278 // Item should appear at bottom
1279 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
);
1283 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1284 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1286 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1287 wxGenericTreeItem
**item2
)
1289 wxCHECK_MSG( s_treeBeingSorted
, 0, T("bug in wxTreeCtrl::SortChildren()") );
1291 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1294 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1295 const wxTreeItemId
& item2
)
1297 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1300 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1302 wxCHECK_RET( itemId
.IsOk(), T("invalid tree item") );
1304 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1306 wxCHECK_RET( !s_treeBeingSorted
,
1307 T("wxTreeCtrl::SortChildren is not reentrant") );
1309 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1310 if ( children
.Count() > 1 )
1312 s_treeBeingSorted
= this;
1313 children
.Sort(tree_ctrl_compare_func
);
1314 s_treeBeingSorted
= NULL
;
1318 //else: don't make the tree dirty as nothing changed
1321 wxImageList
*wxTreeCtrl::GetImageList() const
1323 return m_imageListNormal
;
1326 wxImageList
*wxTreeCtrl::GetStateImageList() const
1328 return m_imageListState
;
1331 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1333 m_imageListNormal
= imageList
;
1335 // Calculate a m_lineHeight value from the image sizes.
1336 // May be toggle off. Then wxTreeCtrl will spread when
1337 // necessary (which might look ugly).
1339 wxClientDC
dc(this);
1340 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1344 n
= m_imageListNormal
->GetImageCount();
1345 for(int i
= 0; i
< n
; i
++)
1347 m_imageListNormal
->GetSize(i
, width
, height
);
1348 if(height
> m_lineHeight
) m_lineHeight
= height
;
1351 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1352 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1357 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1359 m_imageListState
= imageList
;
1362 // -----------------------------------------------------------------------------
1364 // -----------------------------------------------------------------------------
1366 void wxTreeCtrl::AdjustMyScrollbars()
1372 m_anchor
->GetSize( x
, y
, this );
1373 //y += GetLineHeight(m_anchor);
1374 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1375 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1376 int y_pos
= GetScrollPos( wxVERTICAL
);
1377 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1381 SetScrollbars( 0, 0, 0, 0 );
1385 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1387 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1388 return item
->GetHeight();
1390 return m_lineHeight
;
1393 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1395 // render bold items in bold
1401 fontOld
= dc
.GetFont();
1404 // VZ: is there any better way to make a bold variant of old font?
1405 fontNew
= wxFont( fontOld
.GetPointSize(),
1406 fontOld
.GetFamily(),
1409 fontOld
.GetUnderlined());
1410 dc
.SetFont(fontNew
);
1414 wxFAIL_MSG(T("wxDC::GetFont() failed!"));
1420 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1424 int image
= item
->GetCurrentImage();
1425 if ( image
!= NO_IMAGE
)
1427 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1431 int total_h
= GetLineHeight(item
);
1433 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1435 if ( image
!= NO_IMAGE
)
1437 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1438 m_imageListNormal
->Draw( image
, dc
,
1440 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1441 wxIMAGELIST_DRAW_TRANSPARENT
);
1442 dc
.DestroyClippingRegion();
1445 dc
.SetBackgroundMode(wxTRANSPARENT
);
1446 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1447 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1449 // restore normal font for bold items
1452 dc
.SetFont( fontOld
);
1456 // Now y stands for the top of the item, whereas it used to stand for middle !
1457 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1459 int horizX
= level
*m_indent
;
1461 item
->SetX( horizX
+m_indent
+m_spacing
);
1465 y
+=GetLineHeight(item
)/2;
1467 item
->SetCross( horizX
+m_indent
, y
);
1469 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1470 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1472 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1474 int startX
= horizX
;
1475 int endX
= horizX
+ (m_indent
-5);
1477 // if (!item->HasChildren()) endX += (m_indent+5);
1478 if (!item
->HasChildren()) endX
+= 20;
1480 dc
.DrawLine( startX
, y
, endX
, y
);
1482 if (item
->HasPlus())
1484 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1485 dc
.SetPen( *wxGREY_PEN
);
1486 dc
.SetBrush( *wxWHITE_BRUSH
);
1487 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1489 dc
.SetPen( *wxBLACK_PEN
);
1490 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1491 if (!item
->IsExpanded())
1492 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1494 dc
.SetPen( m_dottedPen
);
1497 if (item
->IsSelected())
1499 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1501 dc
.SetBrush( *m_hilightBrush
);
1504 dc
.SetPen( *wxBLACK_PEN
);
1506 dc
.SetPen( *wxTRANSPARENT_PEN
);
1508 PaintItem(item
, dc
);
1510 dc
.SetPen( m_dottedPen
);
1511 dc
.SetTextForeground( *wxBLACK
);
1512 dc
.SetBrush( *wxWHITE_BRUSH
);
1516 dc
.SetBrush( *wxWHITE_BRUSH
);
1517 dc
.SetPen( *wxTRANSPARENT_PEN
);
1519 PaintItem(item
, dc
);
1521 dc
.SetPen( m_dottedPen
);
1525 y
= oldY
+GetLineHeight(item
);
1527 if (item
->IsExpanded())
1529 oldY
+=GetLineHeight(item
)/2;
1530 int semiOldY
=y
; // (=y) for stupid compilator
1532 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1533 size_t n
, count
= children
.Count();
1534 for ( n
= 0; n
< count
; ++n
)
1537 PaintLevel( children
[n
], dc
, level
+1, y
);
1540 // it may happen that the item is expanded but has no items (when you
1541 // delete all its children for example) - don't draw the vertical line
1545 semiOldY
+=GetLineHeight(children
[--n
])/2;
1546 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1551 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1555 wxGenericTreeItem
*i
=item
.m_pItem
;
1557 wxClientDC
dc(this);
1559 dc
.SetLogicalFunction(wxINVERT
);
1562 ViewStart(&x
,&h
); // we only need x
1563 GetClientSize(&w
,&h
); // we only need w
1565 h
=GetLineHeight(i
)+1;
1566 // 2 white column at border
1567 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1570 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1574 wxGenericTreeItem
*i
=item
.m_pItem
;
1576 wxClientDC
dc(this);
1578 dc
.SetLogicalFunction(wxINVERT
);
1583 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1586 dc
.DrawLine( 0, y
, w
, y
);
1589 // -----------------------------------------------------------------------------
1590 // wxWindows callbacks
1591 // -----------------------------------------------------------------------------
1593 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1601 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1603 dc
.SetPen( m_dottedPen
);
1604 //if(GetImageList() == NULL)
1605 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1608 PaintLevel( m_anchor
, dc
, 0, y
);
1611 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1615 if (m_current
) RefreshLine( m_current
);
1618 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1622 if (m_current
) RefreshLine( m_current
);
1625 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1627 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1628 te
.m_code
= event
.KeyCode();
1629 te
.SetEventObject( this );
1630 GetEventHandler()->ProcessEvent( te
);
1632 if ( (m_current
== 0) || (m_key_current
== 0) )
1638 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1639 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1640 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1642 switch (event
.KeyCode())
1646 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1654 if (IsExpanded(m_current
))
1656 Collapse(m_current
);
1668 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1669 event
.m_item
= m_current
;
1671 event
.SetEventObject( this );
1672 GetEventHandler()->ProcessEvent( event
);
1676 // up goes to the previous sibling or to the last of its children if
1680 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1683 prev
= GetParent( m_key_current
);
1687 wxTreeItemId current
= m_key_current
;
1688 if (current
== GetFirstChild( prev
, cockie
))
1690 // otherwise we return to where we came from
1691 SelectItem( prev
, unselect_others
, extended_select
);
1692 m_key_current
=prev
.m_pItem
;
1693 EnsureVisible( prev
);
1700 while ( IsExpanded(prev
) && HasChildren(prev
) )
1702 wxTreeItemId child
= GetLastChild(prev
);
1709 SelectItem( prev
, unselect_others
, extended_select
);
1710 m_key_current
=prev
.m_pItem
;
1711 EnsureVisible( prev
);
1716 // left arrow goes to the parent
1719 wxTreeItemId prev
= GetParent( m_current
);
1722 EnsureVisible( prev
);
1723 SelectItem( prev
, unselect_others
, extended_select
);
1729 // this works the same as the down arrow except that we also expand the
1730 // item if it wasn't expanded yet
1736 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1739 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1740 SelectItem( child
, unselect_others
, extended_select
);
1741 m_key_current
=child
.m_pItem
;
1742 EnsureVisible( child
);
1746 wxTreeItemId next
= GetNextSibling( m_key_current
);
1750 wxTreeItemId current
= m_key_current
;
1751 while (current
&& !next
)
1753 current
= GetParent( current
);
1754 if (current
) next
= GetNextSibling( current
);
1760 SelectItem( next
, unselect_others
, extended_select
);
1761 m_key_current
=next
.m_pItem
;
1762 EnsureVisible( next
);
1768 // <End> selects the last visible tree item
1771 wxTreeItemId last
= GetRootItem();
1773 while ( last
.IsOk() && IsExpanded(last
) )
1775 wxTreeItemId lastChild
= GetLastChild(last
);
1777 // it may happen if the item was expanded but then all of
1778 // its children have been deleted - so IsExpanded() returned
1779 // TRUE, but GetLastChild() returned invalid item
1788 EnsureVisible( last
);
1789 SelectItem( last
, unselect_others
, extended_select
);
1794 // <Home> selects the root item
1797 wxTreeItemId prev
= GetRootItem();
1800 EnsureVisible( prev
);
1801 SelectItem( prev
, unselect_others
, extended_select
);
1811 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1813 // We have to call this here because the label in
1814 // question might just have been added and no screen
1815 // update taken place.
1816 if (m_dirty
) wxYield();
1818 wxClientDC
dc(this);
1820 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1821 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1826 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1827 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1828 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1829 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1831 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1836 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1838 if (!item
.IsOk()) return;
1840 m_currentEdit
= item
.m_pItem
;
1842 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1843 te
.m_item
= m_currentEdit
;
1844 te
.SetEventObject( this );
1845 GetEventHandler()->ProcessEvent( te
);
1847 if (!te
.IsAllowed()) return;
1849 // We have to call this here because the label in
1850 // question might just have been added and no screen
1851 // update taken place.
1852 if (m_dirty
) wxYield();
1854 wxString s
= m_currentEdit
->GetText();
1855 int x
= m_currentEdit
->GetX();
1856 int y
= m_currentEdit
->GetY();
1857 int w
= m_currentEdit
->GetWidth();
1858 int h
= m_currentEdit
->GetHeight();
1863 int image
= m_currentEdit
->GetCurrentImage();
1864 if ( image
!= NO_IMAGE
)
1866 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1870 w
-= image_w
+ 4; // I don't know why +4 is needed
1872 wxClientDC
dc(this);
1874 x
= dc
.LogicalToDeviceX( x
);
1875 y
= dc
.LogicalToDeviceY( y
);
1877 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1878 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1882 void wxTreeCtrl::OnRenameTimer()
1887 void wxTreeCtrl::OnRenameAccept()
1889 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1890 le
.m_item
= m_currentEdit
;
1891 le
.SetEventObject( this );
1892 le
.m_label
= m_renameRes
;
1893 GetEventHandler()->ProcessEvent( le
);
1895 if (!le
.IsAllowed()) return;
1897 SetItemText( m_currentEdit
, m_renameRes
);
1900 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1902 if ( !(event
.LeftUp() || event
.LeftDClick() || event
.Dragging()) ) return;
1904 if ( !m_anchor
) return;
1906 wxClientDC
dc(this);
1908 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1909 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1912 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1913 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1915 if (event
.Dragging())
1917 if (m_dragCount
== 0)
1918 m_dragStart
= wxPoint(x
,y
);
1922 if (m_dragCount
!= 3) return;
1924 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1925 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1927 wxTreeEvent
nevent( command
, GetId() );
1928 nevent
.m_item
= m_current
;
1929 nevent
.SetEventObject(this);
1930 GetEventHandler()->ProcessEvent(nevent
);
1938 if (item
== NULL
) return; /* we hit the blank area */
1940 if (event
.LeftUp() && (item
== m_current
) &&
1941 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
1942 HasFlag(wxTR_EDIT_LABELS
) )
1944 m_renameTimer
->Start( 100, TRUE
);
1948 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1949 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1950 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1959 SelectItem(item
, unselect_others
, extended_select
);
1961 if (event
.LeftDClick())
1963 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1964 event
.m_item
= item
;
1966 event
.SetEventObject( this );
1967 GetEventHandler()->ProcessEvent( event
);
1971 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1973 /* after all changes have been done to the tree control,
1974 * we actually redraw the tree when everything is over */
1981 CalculatePositions();
1983 AdjustMyScrollbars();
1986 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
1995 fontOld
= dc
.GetFont();
1998 // VZ: is there any better way to make a bold variant of old font?
1999 fontNew
= wxFont( fontOld
.GetPointSize(),
2000 fontOld
.GetFamily(),
2003 fontOld
.GetUnderlined());
2004 dc
.SetFont(fontNew
);
2008 wxFAIL_MSG(T("wxDC::GetFont() failed!"));
2012 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2015 // restore normal font for bold items
2017 dc
.SetFont( fontOld
);
2021 int image
= item
->GetCurrentImage();
2022 if ( image
!= NO_IMAGE
)
2024 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2028 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2030 if (total_h
<40) total_h
+=4; // at least 4 pixels
2031 else total_h
+=total_h
/10; // otherwise 10% extra spacing
2033 item
->SetHeight(total_h
);
2034 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2036 item
->SetWidth(image_w
+text_w
+2);
2039 // -----------------------------------------------------------------------------
2040 // for developper : y is now the top of the level
2041 // not the middle of it !
2042 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2044 int horizX
= level
*m_indent
;
2046 CalculateSize( item
, dc
);
2049 item
->SetX( horizX
+m_indent
+m_spacing
);
2051 y
+=GetLineHeight(item
);
2053 if ( !item
->IsExpanded() )
2055 // we dont need to calculate collapsed branches
2059 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2060 size_t n
, count
= children
.Count();
2061 for (n
= 0; n
< count
; ++n
)
2062 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2065 void wxTreeCtrl::CalculatePositions()
2067 if ( !m_anchor
) return;
2069 wxClientDC
dc(this);
2072 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
2074 dc
.SetPen( m_dottedPen
);
2075 //if(GetImageList() == NULL)
2076 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2079 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2082 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2084 wxClientDC
dc(this);
2089 GetClientSize( &cw
, &ch
);
2092 rect
.x
= dc
.LogicalToDeviceX( 0 );
2094 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2097 Refresh( TRUE
, &rect
);
2099 AdjustMyScrollbars();
2102 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2104 wxClientDC
dc(this);
2109 GetClientSize( &cw
, &ch
);
2112 rect
.x
= dc
.LogicalToDeviceX( 0 );
2113 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2115 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2117 Refresh( TRUE
, &rect
);