1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/treectrl.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/settings.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
48 WX_DEFINE_OBJARRAY(wxArrayTreeItemIds
);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int NO_IMAGE
= -1;
56 // -----------------------------------------------------------------------------
58 // -----------------------------------------------------------------------------
61 class WXDLLEXPORT wxGenericTreeItem
65 wxGenericTreeItem() { m_data
= NULL
; }
66 wxGenericTreeItem( wxGenericTreeItem
*parent
,
69 int image
, int selImage
,
70 wxTreeItemData
*data
);
75 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
77 const wxString
& GetText() const { return m_text
; }
78 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
79 { return m_images
[which
]; }
80 wxTreeItemData
*GetData() const { return m_data
; }
82 // returns the current image for the item (depending on its
83 // selected/expanded/whatever state)
84 int GetCurrentImage() const;
86 void SetText( const wxString
&text
);
87 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
88 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
90 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
92 void SetBold(bool bold
) { m_isBold
= bold
; }
94 int GetX() const { return m_x
; }
95 int GetY() const { return m_y
; }
97 void SetX(int x
) { m_x
= x
; }
98 void SetY(int y
) { m_y
= y
; }
100 int GetHeight() const { return m_height
; }
101 int GetWidth() const { return m_width
; }
103 void SetHeight(int h
) { m_height
= h
; }
104 void SetWidth(int w
) { m_width
= w
; }
107 wxGenericTreeItem
*GetParent() const { return m_parent
; }
110 // deletes all children notifying the treectrl about it if !NULL pointer
112 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
113 // FIXME don't know what is it for
116 // get count of all children (and grand children if 'recursively')
117 size_t GetChildrenCount(bool recursively
= TRUE
) const;
119 void Insert(wxGenericTreeItem
*child
, size_t index
)
120 { m_children
.Insert(child
, index
); }
122 void SetCross( int x
, int y
);
123 void GetSize( int &x
, int &y
, const wxTreeCtrl
* );
125 // return the item at given position (or NULL if no item), onButton is TRUE
126 // if the point belongs to the item's button, otherwise it lies on the
128 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxTreeCtrl
*, int &flags
);
130 void Expand() { m_isCollapsed
= FALSE
; }
131 void Collapse() { m_isCollapsed
= TRUE
; }
133 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
136 bool HasChildren() const { return !m_children
.IsEmpty(); }
137 bool IsSelected() const { return m_hasHilight
; }
138 bool IsExpanded() const { return !m_isCollapsed
; }
139 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
140 bool IsBold() const { return m_isBold
; }
145 // tree ctrl images for the normal, selected, expanded and expanded+selected
147 int m_images
[wxTreeItemIcon_Max
];
149 wxTreeItemData
*m_data
;
151 // use bitfields to save size
152 int m_isCollapsed
:1;
153 int m_hasHilight
:1; // same as focused
154 int m_hasPlus
:1; // used for item which doesn't have
155 // children but still has a [+] button
156 int m_isBold
:1; // render the label in bold font
159 long m_height
, m_width
;
160 int m_xCross
, m_yCross
;
162 wxArrayGenericTreeItems m_children
;
163 wxGenericTreeItem
*m_parent
;
166 // =============================================================================
168 // =============================================================================
171 // -----------------------------------------------------------------------------
172 // wxTreeRenameTimer (internal)
173 // -----------------------------------------------------------------------------
175 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl
*owner
)
180 void wxTreeRenameTimer::Notify()
182 m_owner
->OnRenameTimer();
185 //-----------------------------------------------------------------------------
186 // wxTreeTextCtrl (internal)
187 //-----------------------------------------------------------------------------
189 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
191 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
192 EVT_CHAR (wxTreeTextCtrl::OnChar
)
193 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
196 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
, const wxWindowID id
,
197 bool *accept
, wxString
*res
, wxTreeCtrl
*owner
,
198 const wxString
&value
, const wxPoint
&pos
, const wxSize
&size
,
200 int style
, const wxValidator
& validator
, const wxString
&name
) :
202 wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
209 m_startValue
= value
;
212 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
214 if (event
.m_keyCode
== WXK_RETURN
)
217 (*m_res
) = GetValue();
221 if (event
.m_keyCode
== WXK_ESCAPE
)
231 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
233 if (wxPendingDelete
.Member(this)) return;
235 wxPendingDelete
.Append(this);
237 if ((*m_accept
) && ((*m_res
) != m_startValue
))
238 m_owner
->OnRenameAccept();
241 #define PIXELS_PER_UNIT 10
242 // -----------------------------------------------------------------------------
244 // -----------------------------------------------------------------------------
246 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
248 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
249 : wxNotifyEvent( commandType
, id
)
252 m_itemOld
= (wxGenericTreeItem
*)NULL
;
255 // -----------------------------------------------------------------------------
257 // -----------------------------------------------------------------------------
259 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
260 const wxString
& text
,
262 int image
, int selImage
,
263 wxTreeItemData
*data
)
266 m_images
[wxTreeItemIcon_Normal
] = image
;
267 m_images
[wxTreeItemIcon_Selected
] = selImage
;
268 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
269 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
273 m_xCross
= m_yCross
= 0;
277 m_isCollapsed
= TRUE
;
278 m_hasHilight
= FALSE
;
284 dc
.GetTextExtent( m_text
, &m_width
, &m_height
);
285 // TODO : Add the width of the image
286 // PB : We don't know which image is shown (image, selImage)
287 // We don't even know imageList from the treectrl this item belongs to !!!
288 // At this point m_width doesn't mean much, this can be remove !
291 wxGenericTreeItem::~wxGenericTreeItem()
295 wxASSERT_MSG( m_children
.IsEmpty(),
296 wxT("please call DeleteChildren() before deleting the item") );
299 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
301 size_t count
= m_children
.Count();
302 for ( size_t n
= 0; n
< count
; n
++ )
304 wxGenericTreeItem
*child
= m_children
[n
];
307 tree
->SendDeleteEvent(child
);
310 child
->DeleteChildren(tree
);
317 void wxGenericTreeItem::SetText( const wxString
&text
)
322 void wxGenericTreeItem::Reset()
325 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
327 m_images
[i
] = NO_IMAGE
;
332 m_height
= m_width
= 0;
339 m_isCollapsed
= TRUE
;
341 m_parent
= (wxGenericTreeItem
*)NULL
;
344 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
346 size_t count
= m_children
.Count();
350 size_t total
= count
;
351 for ( size_t n
= 0; n
< count
; ++n
)
353 total
+= m_children
[n
]->GetChildrenCount();
359 void wxGenericTreeItem::SetCross( int x
, int y
)
365 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
367 int bottomY
=m_y
+theTree
->GetLineHeight(this);
368 if ( y
< bottomY
) y
= bottomY
;
369 int width
= m_x
+ m_width
;
370 if ( x
< width
) x
= width
;
374 size_t count
= m_children
.Count();
375 for ( size_t n
= 0; n
< count
; ++n
)
377 m_children
[n
]->GetSize( x
, y
, theTree
);
382 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
383 const wxTreeCtrl
*theTree
,
386 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
388 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2)
389 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
391 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
393 // 5 is the size of the plus sign
394 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
395 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
396 (IsExpanded() || HasPlus()))
398 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
402 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
407 // assuming every image (normal and selected ) has the same size !
408 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
409 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
411 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
412 flags
|= wxTREE_HITTEST_ONITEMICON
;
414 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
420 flags
|= wxTREE_HITTEST_ONITEMIDENT
;
421 if (point
.x
> m_x
+m_width
)
422 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
430 size_t count
= m_children
.Count();
431 for ( size_t n
= 0; n
< count
; n
++ )
433 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
440 flags
|=wxTREE_HITTEST_NOWHERE
;
444 int wxGenericTreeItem::GetCurrentImage() const
446 int image
= NO_IMAGE
;
451 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
454 if ( image
== NO_IMAGE
)
456 // we usually fall back to the normal item, but try just the
457 // expanded one (and not selected) first in this case
458 image
= GetImage(wxTreeItemIcon_Expanded
);
464 image
= GetImage(wxTreeItemIcon_Selected
);
467 // may be it doesn't have the specific image we want, try the default one
469 if ( image
== NO_IMAGE
)
477 // -----------------------------------------------------------------------------
478 // wxTreeCtrl implementation
479 // -----------------------------------------------------------------------------
481 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
483 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
484 EVT_PAINT (wxTreeCtrl::OnPaint
)
485 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
486 EVT_CHAR (wxTreeCtrl::OnChar
)
487 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
488 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
489 EVT_IDLE (wxTreeCtrl::OnIdle
)
492 // -----------------------------------------------------------------------------
493 // construction/destruction
494 // -----------------------------------------------------------------------------
496 void wxTreeCtrl::Init()
500 m_anchor
= (wxGenericTreeItem
*) NULL
;
510 m_hilightBrush
= new wxBrush
512 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
517 m_imageListState
= (wxImageList
*) NULL
;
521 m_renameTimer
= new wxTreeRenameTimer( this );
523 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
524 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
525 m_normalFont
.GetFamily(),
526 m_normalFont
.GetStyle(),
528 m_normalFont
.GetUnderlined());
531 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
532 const wxPoint
& pos
, const wxSize
& size
,
535 const wxValidator
&validator
,
537 const wxString
& name
)
541 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
544 SetValidator( validator
);
547 SetBackgroundColour( *wxWHITE
);
548 // m_dottedPen = wxPen( "grey", 0, wxDOT );
549 m_dottedPen
= wxPen( "grey", 0, 0 );
554 wxTreeCtrl::~wxTreeCtrl()
556 wxDELETE( m_hilightBrush
);
560 delete m_renameTimer
;
563 // -----------------------------------------------------------------------------
565 // -----------------------------------------------------------------------------
567 size_t wxTreeCtrl::GetCount() const
569 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
572 void wxTreeCtrl::SetIndent(unsigned int indent
)
579 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
586 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
588 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
590 return item
.m_pItem
->GetChildrenCount(recursively
);
593 // -----------------------------------------------------------------------------
594 // functions to work with tree items
595 // -----------------------------------------------------------------------------
597 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
599 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
601 return item
.m_pItem
->GetText();
604 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
605 wxTreeItemIcon which
) const
607 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
609 return item
.m_pItem
->GetImage(which
);
612 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
614 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
616 return item
.m_pItem
->GetData();
619 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
621 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
624 wxGenericTreeItem
*pItem
= item
.m_pItem
;
625 pItem
->SetText(text
);
626 CalculateSize(pItem
, dc
);
630 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
632 wxTreeItemIcon which
)
634 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
636 wxGenericTreeItem
*pItem
= item
.m_pItem
;
637 pItem
->SetImage(image
, which
);
640 CalculateSize(pItem
, dc
);
644 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
646 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
648 item
.m_pItem
->SetData(data
);
651 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
653 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
655 wxGenericTreeItem
*pItem
= item
.m_pItem
;
656 pItem
->SetHasPlus(has
);
660 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
662 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
664 // avoid redrawing the tree if no real change
665 wxGenericTreeItem
*pItem
= item
.m_pItem
;
666 if ( pItem
->IsBold() != bold
)
668 pItem
->SetBold(bold
);
673 // -----------------------------------------------------------------------------
674 // item status inquiries
675 // -----------------------------------------------------------------------------
677 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
679 wxFAIL_MSG(wxT("not implemented"));
684 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
686 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
688 return !item
.m_pItem
->GetChildren().IsEmpty();
691 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
693 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
695 return item
.m_pItem
->IsExpanded();
698 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
700 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
702 return item
.m_pItem
->IsSelected();
705 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
707 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
709 return item
.m_pItem
->IsBold();
712 // -----------------------------------------------------------------------------
714 // -----------------------------------------------------------------------------
716 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
718 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
720 return item
.m_pItem
->GetParent();
723 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
725 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
728 return GetNextChild(item
, cookie
);
731 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
733 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
735 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
736 if ( (size_t)cookie
< children
.Count() )
738 return children
.Item(cookie
++);
742 // there are no more of them
743 return wxTreeItemId();
747 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
749 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
751 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
752 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
755 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
757 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
759 wxGenericTreeItem
*i
= item
.m_pItem
;
760 wxGenericTreeItem
*parent
= i
->GetParent();
761 if ( parent
== NULL
)
763 // root item doesn't have any siblings
764 return wxTreeItemId();
767 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
768 int index
= siblings
.Index(i
);
769 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
771 size_t n
= (size_t)(index
+ 1);
772 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
775 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
777 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
779 wxGenericTreeItem
*i
= item
.m_pItem
;
780 wxGenericTreeItem
*parent
= i
->GetParent();
781 if ( parent
== NULL
)
783 // root item doesn't have any siblings
784 return wxTreeItemId();
787 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
788 int index
= siblings
.Index(i
);
789 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
791 return index
== 0 ? wxTreeItemId()
792 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
795 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
797 wxFAIL_MSG(wxT("not implemented"));
799 return wxTreeItemId();
802 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
804 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
806 wxFAIL_MSG(wxT("not implemented"));
808 return wxTreeItemId();
811 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
813 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
815 wxFAIL_MSG(wxT("not implemented"));
817 return wxTreeItemId();
820 // -----------------------------------------------------------------------------
822 // -----------------------------------------------------------------------------
824 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
826 const wxString
& text
,
827 int image
, int selImage
,
828 wxTreeItemData
*data
)
830 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
833 // should we give a warning here?
834 return AddRoot(text
, image
, selImage
, data
);
838 wxGenericTreeItem
*item
=
839 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
843 data
->m_pItem
= item
;
846 parent
->Insert( item
, previous
);
853 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
854 int image
, int selImage
,
855 wxTreeItemData
*data
)
857 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
860 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
861 image
, selImage
, data
);
864 data
->m_pItem
= m_anchor
;
867 if (!HasFlag(wxTR_MULTIPLE
))
869 m_current
= m_key_current
= m_anchor
;
870 m_current
->SetHilight( TRUE
);
874 AdjustMyScrollbars();
879 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
880 const wxString
& text
,
881 int image
, int selImage
,
882 wxTreeItemData
*data
)
884 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
887 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
888 const wxTreeItemId
& idPrevious
,
889 const wxString
& text
,
890 int image
, int selImage
,
891 wxTreeItemData
*data
)
893 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
896 // should we give a warning here?
897 return AddRoot(text
, image
, selImage
, data
);
900 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
901 wxASSERT_MSG( index
!= wxNOT_FOUND
,
902 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
903 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
906 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
907 const wxString
& text
,
908 int image
, int selImage
,
909 wxTreeItemData
*data
)
911 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
914 // should we give a warning here?
915 return AddRoot(text
, image
, selImage
, data
);
918 return DoInsertItem(parent
, parent
->GetChildren().Count(), text
,
919 image
, selImage
, data
);
922 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
924 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
926 event
.SetEventObject( this );
927 ProcessEvent( event
);
930 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
932 wxGenericTreeItem
*item
= itemId
.m_pItem
;
933 item
->DeleteChildren(this);
938 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
940 wxGenericTreeItem
*item
= itemId
.m_pItem
;
941 wxGenericTreeItem
*parent
= item
->GetParent();
945 parent
->GetChildren().Remove( item
); // remove by value
948 item
->DeleteChildren(this);
949 SendDeleteEvent(item
);
955 void wxTreeCtrl::DeleteAllItems()
959 m_anchor
->DeleteChildren(this);
968 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
970 wxGenericTreeItem
*item
= itemId
.m_pItem
;
972 if ( !item
->HasPlus() )
975 if ( item
->IsExpanded() )
978 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
980 event
.SetEventObject( this );
982 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
983 if ( ProcessEvent( event
) && !event
.IsAllowed() )
985 // cancelled by program
990 CalculatePositions();
992 RefreshSubtree(item
);
994 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
995 ProcessEvent( event
);
998 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1000 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1002 if ( !item
->IsExpanded() )
1005 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1006 event
.m_item
= item
;
1007 event
.SetEventObject( this );
1008 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1010 // cancelled by program
1016 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1017 size_t count
= children
.Count();
1018 for ( size_t n
= 0; n
< count
; n
++ )
1020 Collapse(children
[n
]);
1023 CalculatePositions();
1025 RefreshSubtree(item
);
1027 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1028 ProcessEvent( event
);
1031 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1034 DeleteChildren(item
);
1037 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1039 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1041 if ( item
->IsExpanded() )
1047 void wxTreeCtrl::Unselect()
1051 m_current
->SetHilight( FALSE
);
1052 RefreshLine( m_current
);
1056 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1058 item
->SetHilight(FALSE
);
1061 if (item
->HasChildren())
1063 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1064 size_t count
= children
.Count();
1065 for ( size_t n
= 0; n
< count
; ++n
)
1066 UnselectAllChildren(children
[n
]);
1070 void wxTreeCtrl::UnselectAll()
1072 UnselectAllChildren(GetRootItem().m_pItem
);
1075 // Recursive function !
1076 // To stop we must have crt_item<last_item
1078 // Tag all next children, when no more children,
1079 // Move to parent (not to tag)
1080 // Keep going... if we found last_item, we stop.
1081 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1083 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1085 if ( parent
== NULL
) // This is root item
1086 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1088 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1089 int index
= children
.Index(crt_item
);
1090 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1092 size_t count
= children
.Count();
1093 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1094 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1096 return TagNextChildren(parent
, last_item
, select
);
1099 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1101 crt_item
->SetHilight(select
);
1102 RefreshLine(crt_item
);
1104 if (crt_item
==last_item
) return TRUE
;
1106 if (crt_item
->HasChildren())
1108 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1109 size_t count
= children
.Count();
1110 for ( size_t n
= 0; n
< count
; ++n
)
1111 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1117 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1119 // item2 is not necessary after item1
1120 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1122 // choice first' and 'last' between item1 and item2
1123 if (item1
->GetY()<item2
->GetY())
1134 bool select
= m_current
->IsSelected();
1136 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1139 TagNextChildren(first
,last
,select
);
1142 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1143 bool unselect_others
,
1144 bool extended_select
)
1146 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1148 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1149 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1151 //wxCHECK_RET( ( (!unselect_others) && is_single),
1152 // wxT("this is a single selection tree") );
1154 // to keep going anyhow !!!
1157 if (item
->IsSelected())
1158 return; // nothing to do
1159 unselect_others
= TRUE
;
1160 extended_select
= FALSE
;
1162 else if ( unselect_others
&& item
->IsSelected() )
1164 // selection change if there is more than one item currently selected
1165 wxArrayTreeItemIds selected_items
;
1166 if ( GetSelections(selected_items
) == 1 )
1170 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1171 event
.m_item
= item
;
1172 event
.m_itemOld
= m_current
;
1173 event
.SetEventObject( this );
1174 // TODO : Here we don't send any selection mode yet !
1176 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1180 if (unselect_others
)
1182 if (is_single
) Unselect(); // to speed up thing
1187 if (extended_select
)
1189 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1190 // don't change the mark (m_current)
1191 SelectItemRange(m_current
, item
);
1195 bool select
=TRUE
; // the default
1197 // Check if we need to toggle hilight (ctrl mode)
1198 if (!unselect_others
)
1199 select
=!item
->IsSelected();
1201 m_current
= m_key_current
= item
;
1202 m_current
->SetHilight(select
);
1203 RefreshLine( m_current
);
1206 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1207 GetEventHandler()->ProcessEvent( event
);
1210 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1211 wxArrayTreeItemIds
&array
) const
1213 if ( item
->IsSelected() )
1214 array
.Add(wxTreeItemId(item
));
1216 if ( item
->HasChildren() )
1218 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1219 size_t count
= children
.GetCount();
1220 for ( size_t n
= 0; n
< count
; ++n
)
1221 FillArray(children
[n
],array
);
1225 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1228 FillArray(GetRootItem().m_pItem
, array
);
1230 return array
.Count();
1233 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1235 if (!item
.IsOk()) return;
1237 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1239 // first expand all parent branches
1240 wxGenericTreeItem
*parent
= gitem
->GetParent();
1244 parent
= parent
->GetParent();
1247 //if (parent) CalculatePositions();
1252 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1254 if (!item
.IsOk()) return;
1256 // We have to call this here because the label in
1257 // question might just have been added and no screen
1258 // update taken place.
1259 if (m_dirty
) wxYield();
1261 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1263 // now scroll to the item
1264 int item_y
= gitem
->GetY();
1268 ViewStart( &start_x
, &start_y
);
1269 start_y
*= PIXELS_PER_UNIT
;
1273 GetClientSize( &client_w
, &client_h
);
1275 if (item_y
< start_y
+3)
1280 m_anchor
->GetSize( x
, y
, this );
1281 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1282 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1283 // Item should appear at top
1284 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1286 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1291 m_anchor
->GetSize( x
, y
, this );
1292 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1293 item_y
+= PIXELS_PER_UNIT
+2;
1294 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1295 // Item should appear at bottom
1296 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
);
1300 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1301 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1303 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1304 wxGenericTreeItem
**item2
)
1306 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1308 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1311 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1312 const wxTreeItemId
& item2
)
1314 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1317 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1319 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1321 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1323 wxCHECK_RET( !s_treeBeingSorted
,
1324 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1326 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1327 if ( children
.Count() > 1 )
1329 s_treeBeingSorted
= this;
1330 children
.Sort(tree_ctrl_compare_func
);
1331 s_treeBeingSorted
= NULL
;
1335 //else: don't make the tree dirty as nothing changed
1338 wxImageList
*wxTreeCtrl::GetImageList() const
1340 return m_imageListNormal
;
1343 wxImageList
*wxTreeCtrl::GetStateImageList() const
1345 return m_imageListState
;
1348 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1350 m_imageListNormal
= imageList
;
1352 // Calculate a m_lineHeight value from the image sizes.
1353 // May be toggle off. Then wxTreeCtrl will spread when
1354 // necessary (which might look ugly).
1356 wxClientDC
dc(this);
1357 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1361 n
= m_imageListNormal
->GetImageCount();
1362 for(int i
= 0; i
< n
; i
++)
1364 m_imageListNormal
->GetSize(i
, width
, height
);
1365 if(height
> m_lineHeight
) m_lineHeight
= height
;
1368 if (m_lineHeight
<40) m_lineHeight
+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1369 else m_lineHeight
+=m_lineHeight
/10; // otherwise 10% extra spacing
1374 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1376 m_imageListState
= imageList
;
1379 // -----------------------------------------------------------------------------
1381 // -----------------------------------------------------------------------------
1383 void wxTreeCtrl::AdjustMyScrollbars()
1389 m_anchor
->GetSize( x
, y
, this );
1390 //y += GetLineHeight(m_anchor);
1391 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1392 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1393 int y_pos
= GetScrollPos( wxVERTICAL
);
1394 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1398 SetScrollbars( 0, 0, 0, 0 );
1402 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1404 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1405 return item
->GetHeight();
1407 return m_lineHeight
;
1410 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1413 dc
.SetFont(m_boldFont
);
1417 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1421 int image
= item
->GetCurrentImage();
1422 if ( image
!= NO_IMAGE
)
1424 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1428 int total_h
= GetLineHeight(item
);
1430 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1432 if ( image
!= NO_IMAGE
)
1434 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1435 m_imageListNormal
->Draw( image
, dc
,
1437 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1438 wxIMAGELIST_DRAW_TRANSPARENT
);
1439 dc
.DestroyClippingRegion();
1442 dc
.SetBackgroundMode(wxTRANSPARENT
);
1443 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1444 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1446 // restore normal font
1447 dc
.SetFont( m_normalFont
);
1450 // Now y stands for the top of the item, whereas it used to stand for middle !
1451 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1453 int horizX
= level
*m_indent
;
1455 item
->SetX( horizX
+m_indent
+m_spacing
);
1459 y
+=GetLineHeight(item
)/2;
1461 item
->SetCross( horizX
+m_indent
, y
);
1463 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1464 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1466 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1468 int startX
= horizX
;
1469 int endX
= horizX
+ (m_indent
-5);
1471 // if (!item->HasChildren()) endX += (m_indent+5);
1472 if (!item
->HasChildren()) endX
+= 20;
1474 dc
.DrawLine( startX
, y
, endX
, y
);
1476 if (item
->HasPlus())
1478 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1479 dc
.SetPen( *wxGREY_PEN
);
1480 dc
.SetBrush( *wxWHITE_BRUSH
);
1481 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1483 dc
.SetPen( *wxBLACK_PEN
);
1484 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1485 if (!item
->IsExpanded())
1486 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1488 dc
.SetPen( m_dottedPen
);
1491 if (item
->IsSelected())
1493 dc
.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
) );
1495 dc
.SetBrush( *m_hilightBrush
);
1498 dc
.SetPen( *wxBLACK_PEN
);
1500 dc
.SetPen( *wxTRANSPARENT_PEN
);
1502 PaintItem(item
, dc
);
1504 dc
.SetPen( m_dottedPen
);
1505 dc
.SetTextForeground( *wxBLACK
);
1506 dc
.SetBrush( *wxWHITE_BRUSH
);
1510 dc
.SetBrush( *wxWHITE_BRUSH
);
1511 dc
.SetPen( *wxTRANSPARENT_PEN
);
1513 PaintItem(item
, dc
);
1515 dc
.SetPen( m_dottedPen
);
1519 y
= oldY
+GetLineHeight(item
);
1521 if (item
->IsExpanded())
1523 oldY
+=GetLineHeight(item
)/2;
1524 int semiOldY
=y
; // (=y) for stupid compilator
1526 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1527 size_t n
, count
= children
.Count();
1528 for ( n
= 0; n
< count
; ++n
)
1531 PaintLevel( children
[n
], dc
, level
+1, y
);
1534 // it may happen that the item is expanded but has no items (when you
1535 // delete all its children for example) - don't draw the vertical line
1539 semiOldY
+=GetLineHeight(children
[--n
])/2;
1540 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1545 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1549 wxGenericTreeItem
*i
=item
.m_pItem
;
1551 wxClientDC
dc(this);
1553 dc
.SetLogicalFunction(wxINVERT
);
1556 ViewStart(&x
,&h
); // we only need x
1557 GetClientSize(&w
,&h
); // we only need w
1559 h
=GetLineHeight(i
)+1;
1560 // 2 white column at border
1561 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1564 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1568 wxGenericTreeItem
*i
=item
.m_pItem
;
1570 wxClientDC
dc(this);
1572 dc
.SetLogicalFunction(wxINVERT
);
1577 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1580 dc
.DrawLine( 0, y
, w
, y
);
1583 // -----------------------------------------------------------------------------
1584 // wxWindows callbacks
1585 // -----------------------------------------------------------------------------
1587 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1595 dc
.SetFont( m_normalFont
);
1596 dc
.SetPen( m_dottedPen
);
1598 // this is now done dynamically
1599 //if(GetImageList() == NULL)
1600 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1603 PaintLevel( m_anchor
, dc
, 0, y
);
1606 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1610 if (m_current
) RefreshLine( m_current
);
1613 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1617 if (m_current
) RefreshLine( m_current
);
1620 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1622 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1623 te
.m_code
= event
.KeyCode();
1624 te
.SetEventObject( this );
1625 GetEventHandler()->ProcessEvent( te
);
1627 if ( (m_current
== 0) || (m_key_current
== 0) )
1633 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1634 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1635 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1637 switch (event
.KeyCode())
1641 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1649 if (IsExpanded(m_current
))
1651 Collapse(m_current
);
1663 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1664 event
.m_item
= m_current
;
1666 event
.SetEventObject( this );
1667 GetEventHandler()->ProcessEvent( event
);
1671 // up goes to the previous sibling or to the last of its children if
1675 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1678 prev
= GetParent( m_key_current
);
1682 wxTreeItemId current
= m_key_current
;
1683 if (current
== GetFirstChild( prev
, cockie
))
1685 // otherwise we return to where we came from
1686 SelectItem( prev
, unselect_others
, extended_select
);
1687 m_key_current
=prev
.m_pItem
;
1688 EnsureVisible( prev
);
1695 while ( IsExpanded(prev
) && HasChildren(prev
) )
1697 wxTreeItemId child
= GetLastChild(prev
);
1704 SelectItem( prev
, unselect_others
, extended_select
);
1705 m_key_current
=prev
.m_pItem
;
1706 EnsureVisible( prev
);
1711 // left arrow goes to the parent
1714 wxTreeItemId prev
= GetParent( m_current
);
1717 EnsureVisible( prev
);
1718 SelectItem( prev
, unselect_others
, extended_select
);
1724 // this works the same as the down arrow except that we also expand the
1725 // item if it wasn't expanded yet
1731 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1734 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1735 SelectItem( child
, unselect_others
, extended_select
);
1736 m_key_current
=child
.m_pItem
;
1737 EnsureVisible( child
);
1741 wxTreeItemId next
= GetNextSibling( m_key_current
);
1745 wxTreeItemId current
= m_key_current
;
1746 while (current
&& !next
)
1748 current
= GetParent( current
);
1749 if (current
) next
= GetNextSibling( current
);
1755 SelectItem( next
, unselect_others
, extended_select
);
1756 m_key_current
=next
.m_pItem
;
1757 EnsureVisible( next
);
1763 // <End> selects the last visible tree item
1766 wxTreeItemId last
= GetRootItem();
1768 while ( last
.IsOk() && IsExpanded(last
) )
1770 wxTreeItemId lastChild
= GetLastChild(last
);
1772 // it may happen if the item was expanded but then all of
1773 // its children have been deleted - so IsExpanded() returned
1774 // TRUE, but GetLastChild() returned invalid item
1783 EnsureVisible( last
);
1784 SelectItem( last
, unselect_others
, extended_select
);
1789 // <Home> selects the root item
1792 wxTreeItemId prev
= GetRootItem();
1795 EnsureVisible( prev
);
1796 SelectItem( prev
, unselect_others
, extended_select
);
1806 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1808 // We have to call this here because the label in
1809 // question might just have been added and no screen
1810 // update taken place.
1811 if (m_dirty
) wxYield();
1813 wxClientDC
dc(this);
1815 long x
= dc
.DeviceToLogicalX( (long)point
.x
);
1816 long y
= dc
.DeviceToLogicalY( (long)point
.y
);
1821 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1822 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1823 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1824 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1826 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1831 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1833 if (!item
.IsOk()) return;
1835 m_currentEdit
= item
.m_pItem
;
1837 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1838 te
.m_item
= m_currentEdit
;
1839 te
.SetEventObject( this );
1840 GetEventHandler()->ProcessEvent( te
);
1842 if (!te
.IsAllowed()) return;
1844 // We have to call this here because the label in
1845 // question might just have been added and no screen
1846 // update taken place.
1847 if (m_dirty
) wxYield();
1849 wxString s
= m_currentEdit
->GetText();
1850 int x
= m_currentEdit
->GetX();
1851 int y
= m_currentEdit
->GetY();
1852 int w
= m_currentEdit
->GetWidth();
1853 int h
= m_currentEdit
->GetHeight();
1858 int image
= m_currentEdit
->GetCurrentImage();
1859 if ( image
!= NO_IMAGE
)
1861 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1865 w
-= image_w
+ 4; // I don't know why +4 is needed
1867 wxClientDC
dc(this);
1869 x
= dc
.LogicalToDeviceX( x
);
1870 y
= dc
.LogicalToDeviceY( y
);
1872 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1873 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
1877 void wxTreeCtrl::OnRenameTimer()
1882 void wxTreeCtrl::OnRenameAccept()
1884 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
1885 le
.m_item
= m_currentEdit
;
1886 le
.SetEventObject( this );
1887 le
.m_label
= m_renameRes
;
1888 GetEventHandler()->ProcessEvent( le
);
1890 if (!le
.IsAllowed()) return;
1892 SetItemText( m_currentEdit
, m_renameRes
);
1895 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
1897 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
1899 if ( !m_anchor
) return;
1901 wxClientDC
dc(this);
1903 long x
= dc
.DeviceToLogicalX( (long)event
.GetX() );
1904 long y
= dc
.DeviceToLogicalY( (long)event
.GetY() );
1907 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
1908 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
1910 if (event
.Dragging())
1912 if (m_dragCount
== 0)
1913 m_dragStart
= wxPoint(x
,y
);
1917 if (m_dragCount
!= 3) return;
1919 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1920 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1922 wxTreeEvent
nevent( command
, GetId() );
1923 nevent
.m_item
= m_current
;
1924 nevent
.SetEventObject(this);
1925 GetEventHandler()->ProcessEvent(nevent
);
1933 if (item
== NULL
) return; /* we hit the blank area */
1935 if (event
.RightDown()) {
1936 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
1939 nevent
.SetEventObject(this);
1940 GetEventHandler()->ProcessEvent(nevent
);
1944 if (event
.LeftUp() && (item
== m_current
) &&
1945 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
1946 HasFlag(wxTR_EDIT_LABELS
) )
1948 m_renameTimer
->Start( 100, TRUE
);
1952 bool is_multiple
=(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1953 bool extended_select
=(event
.ShiftDown() && is_multiple
);
1954 bool unselect_others
=!(extended_select
|| (event
.ControlDown() && is_multiple
));
1963 SelectItem(item
, unselect_others
, extended_select
);
1965 if (event
.LeftDClick())
1967 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1968 event
.m_item
= item
;
1970 event
.SetEventObject( this );
1971 GetEventHandler()->ProcessEvent( event
);
1975 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
1977 /* after all changes have been done to the tree control,
1978 * we actually redraw the tree when everything is over */
1985 CalculatePositions();
1987 AdjustMyScrollbars();
1990 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
1996 dc
.SetFont(m_boldFont
);
1998 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2001 // restore normal font
2002 dc
.SetFont( m_normalFont
);
2006 int image
= item
->GetCurrentImage();
2007 if ( image
!= NO_IMAGE
)
2009 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2013 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2015 if (total_h
<40) total_h
+=4; // at least 4 pixels
2016 else total_h
+=total_h
/10; // otherwise 10% extra spacing
2018 item
->SetHeight(total_h
);
2019 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2021 item
->SetWidth(image_w
+text_w
+2);
2024 // -----------------------------------------------------------------------------
2025 // for developper : y is now the top of the level
2026 // not the middle of it !
2027 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2029 int horizX
= level
*m_indent
;
2031 CalculateSize( item
, dc
);
2034 item
->SetX( horizX
+m_indent
+m_spacing
);
2036 y
+=GetLineHeight(item
);
2038 if ( !item
->IsExpanded() )
2040 // we dont need to calculate collapsed branches
2044 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2045 size_t n
, count
= children
.Count();
2046 for (n
= 0; n
< count
; ++n
)
2047 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2050 void wxTreeCtrl::CalculatePositions()
2052 if ( !m_anchor
) return;
2054 wxClientDC
dc(this);
2057 dc
.SetFont( m_normalFont
);
2059 dc
.SetPen( m_dottedPen
);
2060 //if(GetImageList() == NULL)
2061 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2064 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2067 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2069 wxClientDC
dc(this);
2074 GetClientSize( &cw
, &ch
);
2077 rect
.x
= dc
.LogicalToDeviceX( 0 );
2079 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2082 Refresh( TRUE
, &rect
);
2084 AdjustMyScrollbars();
2087 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2089 wxClientDC
dc(this);
2094 GetClientSize( &cw
, &ch
);
2097 rect
.x
= dc
.LogicalToDeviceX( 0 );
2098 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2100 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2102 Refresh( TRUE
, &rect
);