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 wxT("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, wxT("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(), wxT(""), wxT("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, wxT("invalid tree item") );
598 return item
.m_pItem
->GetImage(which
);
601 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
603 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
605 return item
.m_pItem
->GetData();
608 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
610 wxCHECK_RET( item
.IsOk(), wxT("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(), wxT("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(), wxT("invalid tree item") );
637 item
.m_pItem
->SetData(data
);
640 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
642 wxCHECK_RET( item
.IsOk(), wxT("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(), wxT("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(wxT("not implemented"));
673 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
675 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
677 return !item
.m_pItem
->GetChildren().IsEmpty();
680 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
682 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
684 return item
.m_pItem
->IsExpanded();
687 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
689 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
691 return item
.m_pItem
->IsSelected();
694 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
696 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("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(), wxT("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(), wxT("invalid tree item") );
717 return GetNextChild(item
, cookie
);
720 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
722 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("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(), wxT("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(), wxT("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(), wxT("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(wxT("not implemented"));
788 return wxTreeItemId();
791 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
793 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
795 wxFAIL_MSG(wxT("not implemented"));
797 return wxTreeItemId();
800 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
802 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
804 wxFAIL_MSG(wxT("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(), wxT("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 wxT("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 );
967 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
968 if ( ProcessEvent( event
) && !event
.IsAllowed() )
970 // cancelled by program
975 CalculatePositions();
977 RefreshSubtree(item
);
979 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
980 ProcessEvent( event
);
983 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
985 wxGenericTreeItem
*item
= itemId
.m_pItem
;
987 if ( !item
->IsExpanded() )
990 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
992 event
.SetEventObject( this );
993 if ( ProcessEvent( event
) && !event
.IsAllowed() )
995 // cancelled by program
1001 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1002 size_t count
= children
.Count();
1003 for ( size_t n
= 0; n
< count
; n
++ )
1005 Collapse(children
[n
]);
1008 CalculatePositions();
1010 RefreshSubtree(item
);
1012 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1013 ProcessEvent( event
);
1016 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1019 DeleteChildren(item
);
1022 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1024 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1026 if ( item
->IsExpanded() )
1032 void wxTreeCtrl::Unselect()
1036 m_current
->SetHilight( FALSE
);
1037 RefreshLine( m_current
);
1041 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1043 item
->SetHilight(FALSE
);
1046 if (item
->HasChildren())
1048 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1049 size_t count
= children
.Count();
1050 for ( size_t n
= 0; n
< count
; ++n
)
1051 UnselectAllChildren(children
[n
]);
1055 void wxTreeCtrl::UnselectAll()
1057 UnselectAllChildren(GetRootItem().m_pItem
);
1060 // Recursive function !
1061 // To stop we must have crt_item<last_item
1063 // Tag all next children, when no more children,
1064 // Move to parent (not to tag)
1065 // Keep going... if we found last_item, we stop.
1066 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1068 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1070 if ( parent
== NULL
) // This is root item
1071 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1073 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1074 int index
= children
.Index(crt_item
);
1075 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1077 size_t count
= children
.Count();
1078 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1079 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1081 return TagNextChildren(parent
, last_item
, select
);
1084 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1086 crt_item
->SetHilight(select
);
1087 RefreshLine(crt_item
);
1089 if (crt_item
==last_item
) return TRUE
;
1091 if (crt_item
->HasChildren())
1093 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1094 size_t count
= children
.Count();
1095 for ( size_t n
= 0; n
< count
; ++n
)
1096 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1102 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1104 // item2 is not necessary after item1
1105 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1107 // choice first' and 'last' between item1 and item2
1108 if (item1
->GetY()<item2
->GetY())
1119 bool select
= m_current
->IsSelected();
1121 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1124 TagNextChildren(first
,last
,select
);
1127 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1128 bool unselect_others
,
1129 bool extended_select
)
1131 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1133 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1134 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1136 //wxCHECK_RET( ( (!unselect_others) && is_single),
1137 // wxT("this is a single selection tree") );
1139 // to keep going anyhow !!!
1142 if (item
->IsSelected())
1143 return; // nothing to do
1144 unselect_others
= TRUE
;
1145 extended_select
= FALSE
;
1147 else if ( unselect_others
&& item
->IsSelected() )
1149 // selection change if there is more than one item currently selected
1150 wxArrayTreeItemIds selected_items
;
1151 if ( GetSelections(selected_items
) == 1 )
1155 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1156 event
.m_item
= item
;
1157 event
.m_itemOld
= m_current
;
1158 event
.SetEventObject( this );
1159 // TODO : Here we don't send any selection mode yet !
1161 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1165 if (unselect_others
)
1167 if (is_single
) Unselect(); // to speed up thing
1172 if (extended_select
)
1174 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1175 // don't change the mark (m_current)
1176 SelectItemRange(m_current
, item
);
1180 bool select
=TRUE
; // the default
1182 // Check if we need to toggle hilight (ctrl mode)
1183 if (!unselect_others
)
1184 select
=!item
->IsSelected();
1186 m_current
= m_key_current
= item
;
1187 m_current
->SetHilight(select
);
1188 RefreshLine( m_current
);
1191 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1192 GetEventHandler()->ProcessEvent( event
);
1195 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1196 wxArrayTreeItemIds
&array
) const
1198 if ( item
->IsSelected() )
1199 array
.Add(wxTreeItemId(item
));
1201 if ( item
->HasChildren() )
1203 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1204 size_t count
= children
.GetCount();
1205 for ( size_t n
= 0; n
< count
; ++n
)
1206 FillArray(children
[n
],array
);
1210 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1213 FillArray(GetRootItem().m_pItem
, array
);
1215 return array
.Count();
1218 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1220 if (!item
.IsOk()) return;
1222 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1224 // first expand all parent branches
1225 wxGenericTreeItem
*parent
= gitem
->GetParent();
1229 parent
= parent
->GetParent();
1232 //if (parent) CalculatePositions();
1237 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1239 if (!item
.IsOk()) return;
1241 // We have to call this here because the label in
1242 // question might just have been added and no screen
1243 // update taken place.
1244 if (m_dirty
) wxYield();
1246 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1248 // now scroll to the item
1249 int item_y
= gitem
->GetY();
1253 ViewStart( &start_x
, &start_y
);
1254 start_y
*= PIXELS_PER_UNIT
;
1258 GetClientSize( &client_w
, &client_h
);
1260 if (item_y
< start_y
+3)
1265 m_anchor
->GetSize( x
, y
, this );
1266 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1267 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1268 // Item should appear at top
1269 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1271 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1276 m_anchor
->GetSize( x
, y
, this );
1277 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1278 item_y
+= PIXELS_PER_UNIT
+2;
1279 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1280 // Item should appear at bottom
1281 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
);
1285 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1286 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1288 static int tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1289 wxGenericTreeItem
**item2
)
1291 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1293 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1296 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1297 const wxTreeItemId
& item2
)
1299 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1302 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1304 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1306 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1308 wxCHECK_RET( !s_treeBeingSorted
,
1309 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1311 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1312 if ( children
.Count() > 1 )
1314 s_treeBeingSorted
= this;
1315 children
.Sort(tree_ctrl_compare_func
);
1316 s_treeBeingSorted
= NULL
;
1320 //else: don't make the tree dirty as nothing changed
1323 wxImageList
*wxTreeCtrl::GetImageList() const
1325 return m_imageListNormal
;
1328 wxImageList
*wxTreeCtrl::GetStateImageList() const
1330 return m_imageListState
;
1333 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1335 m_imageListNormal
= imageList
;
1337 // Calculate a m_lineHeight value from the image sizes.
1338 // May be toggle off. Then wxTreeCtrl will spread when
1339 // necessary (which might look ugly).
1341 wxClientDC
dc(this);
1342 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1346 n
= m_imageListNormal
->GetImageCount();
1347 for(int i
= 0; i
< n
; i
++)
1349 m_imageListNormal
->GetSize(i
, width
, height
);
1350 if(height
> m_lineHeight
) m_lineHeight
= height
;
1353 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1354 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1359 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1361 m_imageListState
= imageList
;
1364 // -----------------------------------------------------------------------------
1366 // -----------------------------------------------------------------------------
1368 void wxTreeCtrl::AdjustMyScrollbars()
1374 m_anchor
->GetSize( x
, y
, this );
1375 //y += GetLineHeight(m_anchor);
1376 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1377 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1378 int y_pos
= GetScrollPos( wxVERTICAL
);
1379 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1383 SetScrollbars( 0, 0, 0, 0 );
1387 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1389 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1390 return item
->GetHeight();
1392 return m_lineHeight
;
1395 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1397 // render bold items in bold
1403 fontOld
= dc
.GetFont();
1406 // VZ: is there any better way to make a bold variant of old font?
1407 fontNew
= wxFont( fontOld
.GetPointSize(),
1408 fontOld
.GetFamily(),
1411 fontOld
.GetUnderlined());
1412 dc
.SetFont(fontNew
);
1416 wxFAIL_MSG(wxT("wxDC::GetFont() failed!"));
1422 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1426 int image
= item
->GetCurrentImage();
1427 if ( image
!= NO_IMAGE
)
1429 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1433 int total_h
= GetLineHeight(item
);
1435 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1437 if ( image
!= NO_IMAGE
)
1439 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1440 m_imageListNormal
->Draw( image
, dc
,
1442 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1443 wxIMAGELIST_DRAW_TRANSPARENT
);
1444 dc
.DestroyClippingRegion();
1447 dc
.SetBackgroundMode(wxTRANSPARENT
);
1448 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1449 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1451 // restore normal font for bold items
1454 dc
.SetFont( fontOld
);
1458 // Now y stands for the top of the item, whereas it used to stand for middle !
1459 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1461 int horizX
= level
*m_indent
;
1463 item
->SetX( horizX
+m_indent
+m_spacing
);
1467 y
+=GetLineHeight(item
)/2;
1469 item
->SetCross( horizX
+m_indent
, y
);
1471 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1472 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1474 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1476 int startX
= horizX
;
1477 int endX
= horizX
+ (m_indent
-5);
1479 // if (!item->HasChildren()) endX += (m_indent+5);
1480 if (!item
->HasChildren()) endX
+= 20;
1482 dc
.DrawLine( startX
, y
, endX
, y
);
1484 if (item
->HasPlus())
1486 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1487 dc
.SetPen( *wxGREY_PEN
);
1488 dc
.SetBrush( *wxWHITE_BRUSH
);
1489 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1491 dc
.SetPen( *wxBLACK_PEN
);
1492 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1493 if (!item
->IsExpanded())
1494 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1496 dc
.SetPen( m_dottedPen
);
1499 if (item
->IsSelected())
1501 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1503 dc
.SetBrush( *m_hilightBrush
);
1506 dc
.SetPen( *wxBLACK_PEN
);
1508 dc
.SetPen( *wxTRANSPARENT_PEN
);
1510 PaintItem(item
, dc
);
1512 dc
.SetPen( m_dottedPen
);
1513 dc
.SetTextForeground( *wxBLACK
);
1514 dc
.SetBrush( *wxWHITE_BRUSH
);
1518 dc
.SetBrush( *wxWHITE_BRUSH
);
1519 dc
.SetPen( *wxTRANSPARENT_PEN
);
1521 PaintItem(item
, dc
);
1523 dc
.SetPen( m_dottedPen
);
1527 y
= oldY
+GetLineHeight(item
);
1529 if (item
->IsExpanded())
1531 oldY
+=GetLineHeight(item
)/2;
1532 int semiOldY
=y
; // (=y) for stupid compilator
1534 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1535 size_t n
, count
= children
.Count();
1536 for ( n
= 0; n
< count
; ++n
)
1539 PaintLevel( children
[n
], dc
, level
+1, y
);
1542 // it may happen that the item is expanded but has no items (when you
1543 // delete all its children for example) - don't draw the vertical line
1547 semiOldY
+=GetLineHeight(children
[--n
])/2;
1548 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1553 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1557 wxGenericTreeItem
*i
=item
.m_pItem
;
1559 wxClientDC
dc(this);
1561 dc
.SetLogicalFunction(wxINVERT
);
1564 ViewStart(&x
,&h
); // we only need x
1565 GetClientSize(&w
,&h
); // we only need w
1567 h
=GetLineHeight(i
)+1;
1568 // 2 white column at border
1569 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1572 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1576 wxGenericTreeItem
*i
=item
.m_pItem
;
1578 wxClientDC
dc(this);
1580 dc
.SetLogicalFunction(wxINVERT
);
1585 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1588 dc
.DrawLine( 0, y
, w
, y
);
1591 // -----------------------------------------------------------------------------
1592 // wxWindows callbacks
1593 // -----------------------------------------------------------------------------
1595 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1603 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
1605 dc
.SetPen( m_dottedPen
);
1606 //if(GetImageList() == NULL)
1607 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1610 PaintLevel( m_anchor
, dc
, 0, y
);
1613 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1617 if (m_current
) RefreshLine( m_current
);
1620 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1624 if (m_current
) RefreshLine( m_current
);
1627 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1629 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1630 te
.m_code
= event
.KeyCode();
1631 te
.SetEventObject( this );
1632 GetEventHandler()->ProcessEvent( te
);
1634 if ( (m_current
== 0) || (m_key_current
== 0) )
1640 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1641 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1642 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1644 switch (event
.KeyCode())
1648 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1656 if (IsExpanded(m_current
))
1658 Collapse(m_current
);
1670 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1671 event
.m_item
= m_current
;
1673 event
.SetEventObject( this );
1674 GetEventHandler()->ProcessEvent( event
);
1678 // up goes to the previous sibling or to the last of its children if
1682 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1685 prev
= GetParent( m_key_current
);
1689 wxTreeItemId current
= m_key_current
;
1690 if (current
== GetFirstChild( prev
, cockie
))
1692 // otherwise we return to where we came from
1693 SelectItem( prev
, unselect_others
, extended_select
);
1694 m_key_current
=prev
.m_pItem
;
1695 EnsureVisible( prev
);
1702 while ( IsExpanded(prev
) && HasChildren(prev
) )
1704 wxTreeItemId child
= GetLastChild(prev
);
1711 SelectItem( prev
, unselect_others
, extended_select
);
1712 m_key_current
=prev
.m_pItem
;
1713 EnsureVisible( prev
);
1718 // left arrow goes to the parent
1721 wxTreeItemId prev
= GetParent( m_current
);
1724 EnsureVisible( prev
);
1725 SelectItem( prev
, unselect_others
, extended_select
);
1731 // this works the same as the down arrow except that we also expand the
1732 // item if it wasn't expanded yet
1738 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1741 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1742 SelectItem( child
, unselect_others
, extended_select
);
1743 m_key_current
=child
.m_pItem
;
1744 EnsureVisible( child
);
1748 wxTreeItemId next
= GetNextSibling( m_key_current
);
1752 wxTreeItemId current
= m_key_current
;
1753 while (current
&& !next
)
1755 current
= GetParent( current
);
1756 if (current
) next
= GetNextSibling( current
);
1762 SelectItem( next
, unselect_others
, extended_select
);
1763 m_key_current
=next
.m_pItem
;
1764 EnsureVisible( next
);
1770 // <End> selects the last visible tree item
1773 wxTreeItemId last
= GetRootItem();
1775 while ( last
.IsOk() && IsExpanded(last
) )
1777 wxTreeItemId lastChild
= GetLastChild(last
);
1779 // it may happen if the item was expanded but then all of
1780 // its children have been deleted - so IsExpanded() returned
1781 // TRUE, but GetLastChild() returned invalid item
1790 EnsureVisible( last
);
1791 SelectItem( last
, unselect_others
, extended_select
);
1796 // <Home> selects the root item
1799 wxTreeItemId prev
= GetRootItem();
1802 EnsureVisible( prev
);
1803 SelectItem( prev
, unselect_others
, extended_select
);
1813 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1815 // We have to call this here because the label in
1816 // question might just have been added and no screen
1817 // update taken place.
1818 if (m_dirty
) wxYield();
1820 wxClientDC
dc(this);
1822 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1823 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1828 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1829 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1830 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1831 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1833 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1838 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1840 if (!item
.IsOk()) return;
1842 m_currentEdit
= item
.m_pItem
;
1844 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1845 te
.m_item
= m_currentEdit
;
1846 te
.SetEventObject( this );
1847 GetEventHandler()->ProcessEvent( te
);
1849 if (!te
.IsAllowed()) return;
1851 // We have to call this here because the label in
1852 // question might just have been added and no screen
1853 // update taken place.
1854 if (m_dirty
) wxYield();
1856 wxString s
= m_currentEdit
->GetText();
1857 int x
= m_currentEdit
->GetX();
1858 int y
= m_currentEdit
->GetY();
1859 int w
= m_currentEdit
->GetWidth();
1860 int h
= m_currentEdit
->GetHeight();
1865 int image
= m_currentEdit
->GetCurrentImage();
1866 if ( image
!= NO_IMAGE
)
1868 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1872 w
-= image_w
+ 4; // I don't know why +4 is needed
1874 wxClientDC
dc(this);
1876 x
= dc
.LogicalToDeviceX( x
);
1877 y
= dc
.LogicalToDeviceY( y
);
1879 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1880 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1884 void wxTreeCtrl::OnRenameTimer()
1889 void wxTreeCtrl::OnRenameAccept()
1891 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1892 le
.m_item
= m_currentEdit
;
1893 le
.SetEventObject( this );
1894 le
.m_label
= m_renameRes
;
1895 GetEventHandler()->ProcessEvent( le
);
1897 if (!le
.IsAllowed()) return;
1899 SetItemText( m_currentEdit
, m_renameRes
);
1902 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1904 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1906 if ( !m_anchor
) return;
1908 wxClientDC
dc(this);
1910 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1911 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1914 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1915 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1917 if (event
.Dragging())
1919 if (m_dragCount
== 0)
1920 m_dragStart
= wxPoint(x
,y
);
1924 if (m_dragCount
!= 3) return;
1926 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1927 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1929 wxTreeEvent
nevent( command
, GetId() );
1930 nevent
.m_item
= m_current
;
1931 nevent
.SetEventObject(this);
1932 GetEventHandler()->ProcessEvent(nevent
);
1940 if (item
== NULL
) return; /* we hit the blank area */
1942 if (event
.RightDown()) {
1943 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
1946 nevent
.SetEventObject(this);
1947 GetEventHandler()->ProcessEvent(nevent
);
1951 if (event
.LeftUp() && (item
== m_current
) &&
1952 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
1953 HasFlag(wxTR_EDIT_LABELS
) )
1955 m_renameTimer
->Start( 100, TRUE
);
1959 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1960 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1961 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1970 SelectItem(item
, unselect_others
, extended_select
);
1972 if (event
.LeftDClick())
1974 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1975 event
.m_item
= item
;
1977 event
.SetEventObject( this );
1978 GetEventHandler()->ProcessEvent( event
);
1982 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1984 /* after all changes have been done to the tree control,
1985 * we actually redraw the tree when everything is over */
1992 CalculatePositions();
1994 AdjustMyScrollbars();
1997 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2006 fontOld
= dc
.GetFont();
2009 // VZ: is there any better way to make a bold variant of old font?
2010 fontNew
= wxFont( fontOld
.GetPointSize(),
2011 fontOld
.GetFamily(),
2014 fontOld
.GetUnderlined());
2015 dc
.SetFont(fontNew
);
2019 wxFAIL_MSG(wxT("wxDC::GetFont() failed!"));
2023 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2026 // restore normal font for bold items
2028 dc
.SetFont( fontOld
);
2032 int image
= item
->GetCurrentImage();
2033 if ( image
!= NO_IMAGE
)
2035 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2039 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2041 if (total_h
<40) total_h
+=4; // at least 4 pixels
2042 else total_h
+=total_h
/10; // otherwise 10% extra spacing
2044 item
->SetHeight(total_h
);
2045 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2047 item
->SetWidth(image_w
+text_w
+2);
2050 // -----------------------------------------------------------------------------
2051 // for developper : y is now the top of the level
2052 // not the middle of it !
2053 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2055 int horizX
= level
*m_indent
;
2057 CalculateSize( item
, dc
);
2060 item
->SetX( horizX
+m_indent
+m_spacing
);
2062 y
+=GetLineHeight(item
);
2064 if ( !item
->IsExpanded() )
2066 // we dont need to calculate collapsed branches
2070 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2071 size_t n
, count
= children
.Count();
2072 for (n
= 0; n
< count
; ++n
)
2073 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2076 void wxTreeCtrl::CalculatePositions()
2078 if ( !m_anchor
) return;
2080 wxClientDC
dc(this);
2083 dc
.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
) );
2085 dc
.SetPen( m_dottedPen
);
2086 //if(GetImageList() == NULL)
2087 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2090 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2093 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2095 wxClientDC
dc(this);
2100 GetClientSize( &cw
, &ch
);
2103 rect
.x
= dc
.LogicalToDeviceX( 0 );
2105 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2108 Refresh( TRUE
, &rect
);
2110 AdjustMyScrollbars();
2113 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2115 wxClientDC
dc(this);
2120 GetClientSize( &cw
, &ch
);
2123 rect
.x
= dc
.LogicalToDeviceX( 0 );
2124 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2126 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2128 Refresh( TRUE
, &rect
);