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 #define PIXELS_PER_UNIT 10
58 // -----------------------------------------------------------------------------
60 // -----------------------------------------------------------------------------
62 // timer used for enabling in-place edit
63 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
66 wxTreeRenameTimer( wxTreeCtrl
*owner
);
74 // control used for in-place edit
75 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
79 wxTreeTextCtrl( wxWindow
*parent
,
84 const wxString
&value
= wxEmptyString
,
85 const wxPoint
&pos
= wxDefaultPosition
,
86 const wxSize
&size
= wxDefaultSize
,
88 const wxValidator
& validator
= wxDefaultValidator
,
89 const wxString
&name
= wxTextCtrlNameStr
);
91 void OnChar( wxKeyEvent
&event
);
92 void OnKillFocus( wxFocusEvent
&event
);
98 wxString m_startValue
;
100 DECLARE_EVENT_TABLE()
101 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
105 class WXDLLEXPORT wxGenericTreeItem
109 wxGenericTreeItem() { m_data
= NULL
; }
110 wxGenericTreeItem( wxGenericTreeItem
*parent
,
111 const wxString
& text
,
113 int image
, int selImage
,
114 wxTreeItemData
*data
);
116 ~wxGenericTreeItem();
119 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
121 const wxString
& GetText() const { return m_text
; }
122 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
123 { return m_images
[which
]; }
124 wxTreeItemData
*GetData() const { return m_data
; }
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
130 void SetText( const wxString
&text
);
131 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
136 void SetBold(bool bold
) { m_isBold
= bold
; }
138 int GetX() const { return m_x
; }
139 int GetY() const { return m_y
; }
141 void SetX(int x
) { m_x
= x
; }
142 void SetY(int y
) { m_y
= y
; }
144 int GetHeight() const { return m_height
; }
145 int GetWidth() const { return m_width
; }
147 void SetHeight(int h
) { m_height
= h
; }
148 void SetWidth(int w
) { m_width
= w
; }
151 wxGenericTreeItem
*GetParent() const { return m_parent
; }
154 // deletes all children notifying the treectrl about it if !NULL
156 void DeleteChildren(wxTreeCtrl
*tree
= NULL
);
157 // FIXME don't know what is it for
160 // get count of all children (and grand children if 'recursively')
161 size_t GetChildrenCount(bool recursively
= TRUE
) const;
163 void Insert(wxGenericTreeItem
*child
, size_t index
)
164 { m_children
.Insert(child
, index
); }
166 void SetCross( int x
, int y
);
167 void GetSize( int &x
, int &y
, const wxTreeCtrl
* );
169 // return the item at given position (or NULL if no item), onButton is
170 // TRUE if the point belongs to the item's button, otherwise it lies
171 // on the button's label
172 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxTreeCtrl
*, int &flags
);
174 void Expand() { m_isCollapsed
= FALSE
; }
175 void Collapse() { m_isCollapsed
= TRUE
; }
177 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
180 bool HasChildren() const { return !m_children
.IsEmpty(); }
181 bool IsSelected() const { return m_hasHilight
; }
182 bool IsExpanded() const { return !m_isCollapsed
; }
183 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
184 bool IsBold() const { return m_isBold
; }
187 // get them - may be NULL
188 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
189 // get them ensuring that the pointer is not NULL
190 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
201 // tree ctrl images for the normal, selected, expanded and
202 // expanded+selected states
203 int m_images
[wxTreeItemIcon_Max
];
205 wxTreeItemData
*m_data
;
207 // use bitfields to save size
208 int m_isCollapsed
:1;
209 int m_hasHilight
:1; // same as focused
210 int m_hasPlus
:1; // used for item which doesn't have
211 // children but has a [+] button
212 int m_isBold
:1; // render the label in bold font
215 wxCoord m_height
, m_width
;
216 int m_xCross
, m_yCross
;
219 wxArrayGenericTreeItems m_children
;
220 wxGenericTreeItem
*m_parent
;
222 wxTreeItemAttr
*m_attr
;
225 // =============================================================================
227 // =============================================================================
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 // translate the key or mouse event flags to the type of selection we're
235 static void EventFlagsToSelType(long style
,
239 bool *extended_select
,
240 bool *unselect_others
)
242 *is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
243 *extended_select
= shiftDown
&& is_multiple
;
244 *unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
247 // -----------------------------------------------------------------------------
248 // wxTreeRenameTimer (internal)
249 // -----------------------------------------------------------------------------
251 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl
*owner
)
256 void wxTreeRenameTimer::Notify()
258 m_owner
->OnRenameTimer();
261 //-----------------------------------------------------------------------------
262 // wxTreeTextCtrl (internal)
263 //-----------------------------------------------------------------------------
265 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
267 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
268 EVT_CHAR (wxTreeTextCtrl::OnChar
)
269 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
272 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
277 const wxString
&value
,
281 const wxValidator
& validator
,
282 const wxString
&name
)
283 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
289 (*m_res
) = wxEmptyString
;
290 m_startValue
= value
;
293 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
295 if (event
.m_keyCode
== WXK_RETURN
)
298 (*m_res
) = GetValue();
300 if (!wxPendingDelete
.Member(this))
301 wxPendingDelete
.Append(this);
303 if ((*m_accept
) && ((*m_res
) != m_startValue
))
304 m_owner
->OnRenameAccept();
308 if (event
.m_keyCode
== WXK_ESCAPE
)
313 if (!wxPendingDelete
.Member(this))
314 wxPendingDelete
.Append(this);
321 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
323 if (!wxPendingDelete
.Member(this))
324 wxPendingDelete
.Append(this);
326 if ((*m_accept
) && ((*m_res
) != m_startValue
))
327 m_owner
->OnRenameAccept();
330 // -----------------------------------------------------------------------------
332 // -----------------------------------------------------------------------------
334 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
336 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
337 : wxNotifyEvent( commandType
, id
)
340 m_itemOld
= (wxGenericTreeItem
*)NULL
;
343 // -----------------------------------------------------------------------------
345 // -----------------------------------------------------------------------------
347 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
348 const wxString
& text
,
350 int image
, int selImage
,
351 wxTreeItemData
*data
)
354 m_images
[wxTreeItemIcon_Normal
] = image
;
355 m_images
[wxTreeItemIcon_Selected
] = selImage
;
356 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
357 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
361 m_xCross
= m_yCross
= 0;
365 m_isCollapsed
= TRUE
;
366 m_hasHilight
= FALSE
;
372 m_attr
= (wxTreeItemAttr
*)NULL
;
374 // We don't know the height here yet.
379 wxGenericTreeItem::~wxGenericTreeItem()
385 wxASSERT_MSG( m_children
.IsEmpty(),
386 wxT("please call DeleteChildren() before deleting the item") );
389 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
391 size_t count
= m_children
.Count();
392 for ( size_t n
= 0; n
< count
; n
++ )
394 wxGenericTreeItem
*child
= m_children
[n
];
396 tree
->SendDeleteEvent(child
);
398 child
->DeleteChildren(tree
);
405 void wxGenericTreeItem::SetText( const wxString
&text
)
410 void wxGenericTreeItem::Reset()
413 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
415 m_images
[i
] = NO_IMAGE
;
420 m_height
= m_width
= 0;
427 m_isCollapsed
= TRUE
;
429 m_parent
= (wxGenericTreeItem
*)NULL
;
432 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
434 size_t count
= m_children
.Count();
438 size_t total
= count
;
439 for (size_t n
= 0; n
< count
; ++n
)
441 total
+= m_children
[n
]->GetChildrenCount();
447 void wxGenericTreeItem::SetCross( int x
, int y
)
453 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
455 int bottomY
=m_y
+theTree
->GetLineHeight(this);
456 if ( y
< bottomY
) y
= bottomY
;
457 int width
= m_x
+ m_width
;
458 if ( x
< width
) x
= width
;
462 size_t count
= m_children
.Count();
463 for ( size_t n
= 0; n
< count
; ++n
)
465 m_children
[n
]->GetSize( x
, y
, theTree
);
470 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
471 const wxTreeCtrl
*theTree
,
474 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
476 if (point
.y
< m_y
+theTree
->GetLineHeight(this)/2 )
477 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
479 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
481 // 5 is the size of the plus sign
482 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
483 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
484 (IsExpanded() || HasPlus()))
486 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
490 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
495 // assuming every image (normal and selected ) has the same size !
496 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
497 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
499 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
500 flags
|= wxTREE_HITTEST_ONITEMICON
;
502 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
508 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
509 if (point
.x
> m_x
+m_width
)
510 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
518 size_t count
= m_children
.Count();
519 for ( size_t n
= 0; n
< count
; n
++ )
521 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
528 flags
|=wxTREE_HITTEST_NOWHERE
;
530 return (wxGenericTreeItem
*) NULL
;
533 int wxGenericTreeItem::GetCurrentImage() const
535 int image
= NO_IMAGE
;
540 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
543 if ( image
== NO_IMAGE
)
545 // we usually fall back to the normal item, but try just the
546 // expanded one (and not selected) first in this case
547 image
= GetImage(wxTreeItemIcon_Expanded
);
553 image
= GetImage(wxTreeItemIcon_Selected
);
556 // may be it doesn't have the specific image we want, try the default one
558 if ( image
== NO_IMAGE
)
566 // -----------------------------------------------------------------------------
567 // wxTreeCtrl implementation
568 // -----------------------------------------------------------------------------
570 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
572 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
573 EVT_PAINT (wxTreeCtrl::OnPaint
)
574 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
575 EVT_CHAR (wxTreeCtrl::OnChar
)
576 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
577 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
578 EVT_IDLE (wxTreeCtrl::OnIdle
)
581 // -----------------------------------------------------------------------------
582 // construction/destruction
583 // -----------------------------------------------------------------------------
585 void wxTreeCtrl::Init()
589 m_anchor
= (wxGenericTreeItem
*) NULL
;
599 m_hilightBrush
= new wxBrush
601 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
606 m_imageListState
= (wxImageList
*) NULL
;
609 m_isDragging
= FALSE
;
610 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
612 m_renameTimer
= new wxTreeRenameTimer( this );
614 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
615 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
616 m_normalFont
.GetFamily(),
617 m_normalFont
.GetStyle(),
619 m_normalFont
.GetUnderlined());
622 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
623 const wxPoint
& pos
, const wxSize
& size
,
625 const wxValidator
&validator
,
626 const wxString
& name
)
630 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
633 SetValidator( validator
);
636 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
637 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
638 m_dottedPen
= wxPen( "grey", 0, 0 );
643 wxTreeCtrl::~wxTreeCtrl()
645 wxDELETE( m_hilightBrush
);
649 delete m_renameTimer
;
652 // -----------------------------------------------------------------------------
654 // -----------------------------------------------------------------------------
656 size_t wxTreeCtrl::GetCount() const
658 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
661 void wxTreeCtrl::SetIndent(unsigned int indent
)
667 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
673 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
675 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
677 return item
.m_pItem
->GetChildrenCount(recursively
);
680 // -----------------------------------------------------------------------------
681 // functions to work with tree items
682 // -----------------------------------------------------------------------------
684 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
686 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
688 return item
.m_pItem
->GetText();
691 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
692 wxTreeItemIcon which
) const
694 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
696 return item
.m_pItem
->GetImage(which
);
699 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
701 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
703 return item
.m_pItem
->GetData();
706 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
708 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
711 wxGenericTreeItem
*pItem
= item
.m_pItem
;
712 pItem
->SetText(text
);
713 CalculateSize(pItem
, dc
);
717 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
719 wxTreeItemIcon which
)
721 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
723 wxGenericTreeItem
*pItem
= item
.m_pItem
;
724 pItem
->SetImage(image
, which
);
727 CalculateSize(pItem
, dc
);
731 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
733 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
735 item
.m_pItem
->SetData(data
);
738 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
740 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
742 wxGenericTreeItem
*pItem
= item
.m_pItem
;
743 pItem
->SetHasPlus(has
);
747 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
749 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
751 // avoid redrawing the tree if no real change
752 wxGenericTreeItem
*pItem
= item
.m_pItem
;
753 if ( pItem
->IsBold() != bold
)
755 pItem
->SetBold(bold
);
760 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
763 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
765 wxGenericTreeItem
*pItem
= item
.m_pItem
;
766 pItem
->Attr().SetTextColour(col
);
770 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
773 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
775 wxGenericTreeItem
*pItem
= item
.m_pItem
;
776 pItem
->Attr().SetBackgroundColour(col
);
780 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
782 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
784 wxGenericTreeItem
*pItem
= item
.m_pItem
;
785 pItem
->Attr().SetFont(font
);
789 // -----------------------------------------------------------------------------
790 // item status inquiries
791 // -----------------------------------------------------------------------------
793 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
795 wxFAIL_MSG(wxT("not implemented"));
800 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
802 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
804 return !item
.m_pItem
->GetChildren().IsEmpty();
807 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
809 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
811 return item
.m_pItem
->IsExpanded();
814 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
816 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
818 return item
.m_pItem
->IsSelected();
821 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
823 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
825 return item
.m_pItem
->IsBold();
828 // -----------------------------------------------------------------------------
830 // -----------------------------------------------------------------------------
832 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
834 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
836 return item
.m_pItem
->GetParent();
839 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
841 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
844 return GetNextChild(item
, cookie
);
847 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
849 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
851 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
852 if ( (size_t)cookie
< children
.Count() )
854 return children
.Item((size_t)cookie
++);
858 // there are no more of them
859 return wxTreeItemId();
863 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
865 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
867 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
868 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
871 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
873 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
875 wxGenericTreeItem
*i
= item
.m_pItem
;
876 wxGenericTreeItem
*parent
= i
->GetParent();
877 if ( parent
== NULL
)
879 // root item doesn't have any siblings
880 return wxTreeItemId();
883 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
884 int index
= siblings
.Index(i
);
885 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
887 size_t n
= (size_t)(index
+ 1);
888 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
891 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
893 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
895 wxGenericTreeItem
*i
= item
.m_pItem
;
896 wxGenericTreeItem
*parent
= i
->GetParent();
897 if ( parent
== NULL
)
899 // root item doesn't have any siblings
900 return wxTreeItemId();
903 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
904 int index
= siblings
.Index(i
);
905 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
907 return index
== 0 ? wxTreeItemId()
908 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
911 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
913 wxFAIL_MSG(wxT("not implemented"));
915 return wxTreeItemId();
918 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
920 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
922 wxFAIL_MSG(wxT("not implemented"));
924 return wxTreeItemId();
927 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
929 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
931 wxFAIL_MSG(wxT("not implemented"));
933 return wxTreeItemId();
936 // -----------------------------------------------------------------------------
938 // -----------------------------------------------------------------------------
940 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
942 const wxString
& text
,
943 int image
, int selImage
,
944 wxTreeItemData
*data
)
946 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
949 // should we give a warning here?
950 return AddRoot(text
, image
, selImage
, data
);
954 wxGenericTreeItem
*item
=
955 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
959 data
->m_pItem
= item
;
962 parent
->Insert( item
, previous
);
969 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
970 int image
, int selImage
,
971 wxTreeItemData
*data
)
973 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
976 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
977 image
, selImage
, data
);
980 data
->m_pItem
= m_anchor
;
983 if (!HasFlag(wxTR_MULTIPLE
))
985 m_current
= m_key_current
= m_anchor
;
986 m_current
->SetHilight( TRUE
);
990 AdjustMyScrollbars();
995 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
996 const wxString
& text
,
997 int image
, int selImage
,
998 wxTreeItemData
*data
)
1000 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1003 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1004 const wxTreeItemId
& idPrevious
,
1005 const wxString
& text
,
1006 int image
, int selImage
,
1007 wxTreeItemData
*data
)
1009 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
1012 // should we give a warning here?
1013 return AddRoot(text
, image
, selImage
, data
);
1016 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
1017 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1018 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
1020 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1023 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1025 const wxString
& text
,
1026 int image
, int selImage
,
1027 wxTreeItemData
*data
)
1029 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
1032 // should we give a warning here?
1033 return AddRoot(text
, image
, selImage
, data
);
1036 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1039 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1040 const wxString
& text
,
1041 int image
, int selImage
,
1042 wxTreeItemData
*data
)
1044 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
1047 // should we give a warning here?
1048 return AddRoot(text
, image
, selImage
, data
);
1051 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1052 image
, selImage
, data
);
1055 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1057 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1058 event
.m_item
= item
;
1059 event
.SetEventObject( this );
1060 ProcessEvent( event
);
1063 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1065 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1066 item
->DeleteChildren(this);
1071 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1073 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1074 wxGenericTreeItem
*parent
= item
->GetParent();
1078 parent
->GetChildren().Remove( item
); // remove by value
1081 item
->DeleteChildren(this);
1082 SendDeleteEvent(item
);
1088 void wxTreeCtrl::DeleteAllItems()
1092 m_anchor
->DeleteChildren(this);
1101 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1103 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1105 if ( !item
->HasPlus() )
1108 if ( item
->IsExpanded() )
1111 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1112 event
.m_item
= item
;
1113 event
.SetEventObject( this );
1115 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1116 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1118 // cancelled by program
1123 CalculatePositions();
1125 RefreshSubtree(item
);
1127 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1128 ProcessEvent( event
);
1131 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1133 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1135 if ( !item
->IsExpanded() )
1138 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1139 event
.m_item
= item
;
1140 event
.SetEventObject( this );
1141 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1143 // cancelled by program
1149 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1150 size_t count
= children
.Count();
1151 for ( size_t n
= 0; n
< count
; n
++ )
1153 Collapse(children
[n
]);
1156 CalculatePositions();
1158 RefreshSubtree(item
);
1160 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1161 ProcessEvent( event
);
1164 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1167 DeleteChildren(item
);
1170 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1172 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1174 if (item
->IsExpanded())
1180 void wxTreeCtrl::Unselect()
1184 m_current
->SetHilight( FALSE
);
1185 RefreshLine( m_current
);
1189 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1191 if (item
->IsSelected())
1193 item
->SetHilight(FALSE
);
1197 if (item
->HasChildren())
1199 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1200 size_t count
= children
.Count();
1201 for ( size_t n
= 0; n
< count
; ++n
)
1203 UnselectAllChildren(children
[n
]);
1208 void wxTreeCtrl::UnselectAll()
1210 UnselectAllChildren(GetRootItem().m_pItem
);
1213 // Recursive function !
1214 // To stop we must have crt_item<last_item
1216 // Tag all next children, when no more children,
1217 // Move to parent (not to tag)
1218 // Keep going... if we found last_item, we stop.
1219 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1221 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1223 if (parent
== NULL
) // This is root item
1224 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1226 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1227 int index
= children
.Index(crt_item
);
1228 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1230 size_t count
= children
.Count();
1231 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1233 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1236 return TagNextChildren(parent
, last_item
, select
);
1239 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1241 crt_item
->SetHilight(select
);
1242 RefreshLine(crt_item
);
1244 if (crt_item
==last_item
)
1247 if (crt_item
->HasChildren())
1249 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1250 size_t count
= children
.Count();
1251 for ( size_t n
= 0; n
< count
; ++n
)
1253 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1261 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1263 // item2 is not necessary after item1
1264 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1266 // choice first' and 'last' between item1 and item2
1267 if (item1
->GetY()<item2
->GetY())
1278 bool select
= m_current
->IsSelected();
1280 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1283 TagNextChildren(first
,last
,select
);
1286 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1287 bool unselect_others
,
1288 bool extended_select
)
1290 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1292 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1293 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1295 //wxCHECK_RET( ( (!unselect_others) && is_single),
1296 // wxT("this is a single selection tree") );
1298 // to keep going anyhow !!!
1301 if (item
->IsSelected())
1302 return; // nothing to do
1303 unselect_others
= TRUE
;
1304 extended_select
= FALSE
;
1306 else if ( unselect_others
&& item
->IsSelected() )
1308 // selection change if there is more than one item currently selected
1309 wxArrayTreeItemIds selected_items
;
1310 if ( GetSelections(selected_items
) == 1 )
1314 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1315 event
.m_item
= item
;
1316 event
.m_itemOld
= m_current
;
1317 event
.SetEventObject( this );
1318 // TODO : Here we don't send any selection mode yet !
1320 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1324 if (unselect_others
)
1326 if (is_single
) Unselect(); // to speed up thing
1331 if (extended_select
)
1333 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1334 // don't change the mark (m_current)
1335 SelectItemRange(m_current
, item
);
1339 bool select
=TRUE
; // the default
1341 // Check if we need to toggle hilight (ctrl mode)
1342 if (!unselect_others
)
1343 select
=!item
->IsSelected();
1345 m_current
= m_key_current
= item
;
1346 m_current
->SetHilight(select
);
1347 RefreshLine( m_current
);
1350 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1351 GetEventHandler()->ProcessEvent( event
);
1354 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1355 wxArrayTreeItemIds
&array
) const
1357 if ( item
->IsSelected() )
1358 array
.Add(wxTreeItemId(item
));
1360 if ( item
->HasChildren() )
1362 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1363 size_t count
= children
.GetCount();
1364 for ( size_t n
= 0; n
< count
; ++n
)
1365 FillArray(children
[n
],array
);
1369 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1372 FillArray(GetRootItem().m_pItem
, array
);
1374 return array
.Count();
1377 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1379 if (!item
.IsOk()) return;
1381 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1383 // first expand all parent branches
1384 wxGenericTreeItem
*parent
= gitem
->GetParent();
1388 parent
= parent
->GetParent();
1391 //if (parent) CalculatePositions();
1396 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1398 if (!item
.IsOk()) return;
1400 // We have to call this here because the label in
1401 // question might just have been added and no screen
1402 // update taken place.
1403 if (m_dirty
) wxYield();
1405 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1407 // now scroll to the item
1408 int item_y
= gitem
->GetY();
1412 ViewStart( &start_x
, &start_y
);
1413 start_y
*= PIXELS_PER_UNIT
;
1417 GetClientSize( &client_w
, &client_h
);
1419 if (item_y
< start_y
+3)
1424 m_anchor
->GetSize( x
, y
, this );
1425 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1426 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1427 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1428 // Item should appear at top
1429 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1431 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1436 m_anchor
->GetSize( x
, y
, this );
1437 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1438 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1439 item_y
+= PIXELS_PER_UNIT
+2;
1440 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1441 // Item should appear at bottom
1442 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
);
1446 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1447 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1449 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1450 wxGenericTreeItem
**item2
)
1452 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1454 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1457 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1458 const wxTreeItemId
& item2
)
1460 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1463 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1465 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1467 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1469 wxCHECK_RET( !s_treeBeingSorted
,
1470 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1472 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1473 if ( children
.Count() > 1 )
1475 s_treeBeingSorted
= this;
1476 children
.Sort(tree_ctrl_compare_func
);
1477 s_treeBeingSorted
= NULL
;
1481 //else: don't make the tree dirty as nothing changed
1484 wxImageList
*wxTreeCtrl::GetImageList() const
1486 return m_imageListNormal
;
1489 wxImageList
*wxTreeCtrl::GetStateImageList() const
1491 return m_imageListState
;
1494 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1496 m_imageListNormal
= imageList
;
1498 // Calculate a m_lineHeight value from the image sizes.
1499 // May be toggle off. Then wxTreeCtrl will spread when
1500 // necessary (which might look ugly).
1502 wxClientDC
dc(this);
1503 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1504 int width
= 0, height
= 0,
1505 n
= m_imageListNormal
->GetImageCount();
1507 for (int i
= 0; i
< n
; i
++)
1509 m_imageListNormal
->GetSize(i
, width
, height
);
1510 if (height
> m_lineHeight
) m_lineHeight
= height
;
1513 if (m_lineHeight
< 40)
1514 m_lineHeight
+= 2; // at least 2 pixels
1516 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1520 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1522 m_imageListState
= imageList
;
1525 // -----------------------------------------------------------------------------
1527 // -----------------------------------------------------------------------------
1529 void wxTreeCtrl::AdjustMyScrollbars()
1535 m_anchor
->GetSize( x
, y
, this );
1536 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1537 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1538 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1539 int y_pos
= GetScrollPos( wxVERTICAL
);
1540 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1544 SetScrollbars( 0, 0, 0, 0 );
1548 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1550 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1551 return item
->GetHeight();
1553 return m_lineHeight
;
1556 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1558 wxTreeItemAttr
*attr
= item
->GetAttributes();
1559 if ( attr
&& attr
->HasFont() )
1560 dc
.SetFont(attr
->GetFont());
1561 else if (item
->IsBold())
1562 dc
.SetFont(m_boldFont
);
1566 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1570 int image
= item
->GetCurrentImage();
1571 if ( image
!= NO_IMAGE
)
1573 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1577 int total_h
= GetLineHeight(item
);
1579 if (item
->IsSelected())
1580 dc
.SetBrush(*m_hilightBrush
);
1584 if ( attr
&& attr
->HasBackgroundColour() )
1585 colBg
= attr
->GetBackgroundColour();
1587 colBg
= m_backgroundColour
;
1588 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1591 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1593 if ( image
!= NO_IMAGE
)
1595 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1596 m_imageListNormal
->Draw( image
, dc
,
1598 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1599 wxIMAGELIST_DRAW_TRANSPARENT
);
1600 dc
.DestroyClippingRegion();
1603 dc
.SetBackgroundMode(wxTRANSPARENT
);
1604 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), (wxCoord
)item
->GetY()
1605 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1607 // restore normal font
1608 dc
.SetFont( m_normalFont
);
1611 // Now y stands for the top of the item, whereas it used to stand for middle !
1612 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1614 int horizX
= level
*m_indent
;
1616 item
->SetX( horizX
+m_indent
+m_spacing
);
1620 y
+=GetLineHeight(item
)/2;
1622 item
->SetCross( horizX
+m_indent
, y
);
1624 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1625 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1627 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1629 int startX
= horizX
;
1630 int endX
= horizX
+ (m_indent
-5);
1632 // if (!item->HasChildren()) endX += (m_indent+5);
1633 if (!item
->HasChildren()) endX
+= 20;
1635 dc
.DrawLine( startX
, y
, endX
, y
);
1637 if (item
->HasPlus())
1639 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1640 dc
.SetPen( *wxGREY_PEN
);
1641 dc
.SetBrush( *wxWHITE_BRUSH
);
1642 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1644 dc
.SetPen( *wxBLACK_PEN
);
1645 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1646 if (!item
->IsExpanded())
1647 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1649 dc
.SetPen( m_dottedPen
);
1652 wxPen
*pen
= wxTRANSPARENT_PEN
;
1655 if ( item
->IsSelected() )
1657 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1665 wxTreeItemAttr
*attr
= item
->GetAttributes();
1666 if ( attr
&& attr
->HasTextColour() )
1667 colText
= attr
->GetTextColour();
1673 dc
.SetTextForeground(colText
);
1677 PaintItem(item
, dc
);
1679 // restore DC objects
1680 dc
.SetBrush( *wxWHITE_BRUSH
);
1681 dc
.SetPen( m_dottedPen
);
1682 dc
.SetTextForeground( *wxBLACK
);
1685 y
= oldY
+GetLineHeight(item
);
1687 if (item
->IsExpanded())
1689 oldY
+=GetLineHeight(item
)/2;
1692 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1693 size_t n
, count
= children
.Count();
1694 for ( n
= 0; n
< count
; ++n
)
1697 PaintLevel( children
[n
], dc
, level
+1, y
);
1700 // it may happen that the item is expanded but has no items (when you
1701 // delete all its children for example) - don't draw the vertical line
1705 semiOldY
+=GetLineHeight(children
[--n
])/2;
1706 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1711 void wxTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1715 if ( item
->HasPlus() )
1717 // it's a folder, indicate it by a border
1722 // draw a line under the drop target because the item will be
1724 DrawLine(item
, TRUE
/* below */);
1727 SetCursor(wxCURSOR_BULLSEYE
);
1732 SetCursor(wxCURSOR_NO_ENTRY
);
1736 void wxTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
1738 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") );
1740 wxGenericTreeItem
*i
= item
.m_pItem
;
1742 wxClientDC
dc(this);
1744 dc
.SetLogicalFunction(wxINVERT
);
1745 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1747 int w
= i
->GetWidth() + 2;
1748 int h
= GetLineHeight(i
) + 2;
1750 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
1753 void wxTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
1755 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxTreeCtrl::DrawLine") );
1757 wxGenericTreeItem
*i
= item
.m_pItem
;
1759 wxClientDC
dc(this);
1761 dc
.SetLogicalFunction(wxINVERT
);
1767 y
+= GetLineHeight(i
) - 1;
1770 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
1773 // -----------------------------------------------------------------------------
1774 // wxWindows callbacks
1775 // -----------------------------------------------------------------------------
1777 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1785 dc
.SetFont( m_normalFont
);
1786 dc
.SetPen( m_dottedPen
);
1788 // this is now done dynamically
1789 //if(GetImageList() == NULL)
1790 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1793 PaintLevel( m_anchor
, dc
, 0, y
);
1796 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1800 if (m_current
) RefreshLine( m_current
);
1803 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1807 if (m_current
) RefreshLine( m_current
);
1810 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1812 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1813 te
.m_code
= (int)event
.KeyCode();
1814 te
.SetEventObject( this );
1815 GetEventHandler()->ProcessEvent( te
);
1817 if ( (m_current
== 0) || (m_key_current
== 0) )
1823 // how should the selection work for this event?
1824 bool is_multiple
, extended_select
, unselect_others
;
1825 EventFlagsToSelType(GetWindowStyleFlag(),
1827 event
.ControlDown(),
1828 &is_multiple
, &extended_select
, &unselect_others
);
1830 switch (event
.KeyCode())
1834 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1842 if (IsExpanded(m_current
))
1844 Collapse(m_current
);
1856 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1857 event
.m_item
= m_current
;
1859 event
.SetEventObject( this );
1860 GetEventHandler()->ProcessEvent( event
);
1864 // up goes to the previous sibling or to the last of its children if
1868 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1871 prev
= GetParent( m_key_current
);
1875 wxTreeItemId current
= m_key_current
;
1876 if (current
== GetFirstChild( prev
, cockie
))
1878 // otherwise we return to where we came from
1879 SelectItem( prev
, unselect_others
, extended_select
);
1880 m_key_current
=prev
.m_pItem
;
1881 EnsureVisible( prev
);
1888 while ( IsExpanded(prev
) && HasChildren(prev
) )
1890 wxTreeItemId child
= GetLastChild(prev
);
1897 SelectItem( prev
, unselect_others
, extended_select
);
1898 m_key_current
=prev
.m_pItem
;
1899 EnsureVisible( prev
);
1904 // left arrow goes to the parent
1907 wxTreeItemId prev
= GetParent( m_current
);
1910 EnsureVisible( prev
);
1911 SelectItem( prev
, unselect_others
, extended_select
);
1917 // this works the same as the down arrow except that we also expand the
1918 // item if it wasn't expanded yet
1924 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1927 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1928 SelectItem( child
, unselect_others
, extended_select
);
1929 m_key_current
=child
.m_pItem
;
1930 EnsureVisible( child
);
1934 wxTreeItemId next
= GetNextSibling( m_key_current
);
1938 wxTreeItemId current
= m_key_current
;
1939 while (current
&& !next
)
1941 current
= GetParent( current
);
1942 if (current
) next
= GetNextSibling( current
);
1948 SelectItem( next
, unselect_others
, extended_select
);
1949 m_key_current
=next
.m_pItem
;
1950 EnsureVisible( next
);
1956 // <End> selects the last visible tree item
1959 wxTreeItemId last
= GetRootItem();
1961 while ( last
.IsOk() && IsExpanded(last
) )
1963 wxTreeItemId lastChild
= GetLastChild(last
);
1965 // it may happen if the item was expanded but then all of
1966 // its children have been deleted - so IsExpanded() returned
1967 // TRUE, but GetLastChild() returned invalid item
1976 EnsureVisible( last
);
1977 SelectItem( last
, unselect_others
, extended_select
);
1982 // <Home> selects the root item
1985 wxTreeItemId prev
= GetRootItem();
1988 EnsureVisible( prev
);
1989 SelectItem( prev
, unselect_others
, extended_select
);
1999 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2001 // We have to call this here because the label in
2002 // question might just have been added and no screen
2003 // update taken place.
2004 if (m_dirty
) wxYield();
2006 wxClientDC
dc(this);
2008 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2009 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2014 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2015 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2016 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2017 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2019 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2024 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
2026 if (!item
.IsOk()) return;
2028 m_currentEdit
= item
.m_pItem
;
2030 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2031 te
.m_item
= m_currentEdit
;
2032 te
.SetEventObject( this );
2033 GetEventHandler()->ProcessEvent( te
);
2035 if (!te
.IsAllowed()) return;
2037 // We have to call this here because the label in
2038 // question might just have been added and no screen
2039 // update taken place.
2040 if (m_dirty
) wxYield();
2042 wxString s
= m_currentEdit
->GetText();
2043 int x
= m_currentEdit
->GetX();
2044 int y
= m_currentEdit
->GetY();
2045 int w
= m_currentEdit
->GetWidth();
2046 int h
= m_currentEdit
->GetHeight();
2051 int image
= m_currentEdit
->GetCurrentImage();
2052 if ( image
!= NO_IMAGE
)
2054 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2058 w
-= image_w
+ 4; // I don't know why +4 is needed
2060 wxClientDC
dc(this);
2062 x
= dc
.LogicalToDeviceX( x
);
2063 y
= dc
.LogicalToDeviceY( y
);
2065 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
2066 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
2070 void wxTreeCtrl::OnRenameTimer()
2075 void wxTreeCtrl::OnRenameAccept()
2077 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2078 le
.m_item
= m_currentEdit
;
2079 le
.SetEventObject( this );
2080 le
.m_label
= m_renameRes
;
2081 GetEventHandler()->ProcessEvent( le
);
2083 if (!le
.IsAllowed()) return;
2085 SetItemText( m_currentEdit
, m_renameRes
);
2088 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
2090 if ( !m_anchor
) return;
2092 // we process left mouse up event (enables in-place edit), right down
2093 // (pass to the user code), left dbl click (activate item) and
2094 // dragging/moving events for items drag-and-drop
2095 if ( !(event
.LeftUp() ||
2096 event
.RightDown() ||
2097 event
.LeftDClick() ||
2099 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2106 wxClientDC
dc(this);
2108 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2109 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2112 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2114 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
2116 if ( event
.Dragging() && !m_isDragging
)
2118 if (m_dragCount
== 0)
2119 m_dragStart
= wxPoint(x
,y
);
2123 if (m_dragCount
!= 3)
2125 // wait until user drags a bit further...
2129 wxEventType command
= event
.RightIsDown()
2130 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2131 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2133 wxTreeEvent
nevent( command
, GetId() );
2134 nevent
.m_item
= m_current
;
2135 nevent
.SetEventObject(this);
2137 // by default the dragging is not supported, the user code must
2138 // explicitly allow the event for it to take place
2141 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2143 // we're going to drag this item
2144 m_isDragging
= TRUE
;
2149 else if ( event
.Moving() )
2151 if ( item
!= m_dropTarget
)
2153 // unhighlight the previous drop target
2154 DrawDropEffect(m_dropTarget
);
2156 m_dropTarget
= item
;
2158 // highlight the current drop target if any
2159 DrawDropEffect(m_dropTarget
);
2164 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2166 // erase the highlighting
2167 DrawDropEffect(m_dropTarget
);
2169 // generate the drag end event
2170 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2172 event
.m_item
= item
;
2173 event
.m_pointDrag
= wxPoint(x
, y
);
2174 event
.SetEventObject(this);
2176 (void)GetEventHandler()->ProcessEvent(event
);
2178 m_isDragging
= FALSE
;
2179 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2183 SetCursor(wxCURSOR_DEFAULT
);
2189 // here we process only the messages which happen on tree items
2193 if (item
== NULL
) return; /* we hit the blank area */
2195 if ( event
.RightDown() )
2197 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2198 nevent
.m_item
= item
;
2200 nevent
.SetEventObject(this);
2201 GetEventHandler()->ProcessEvent(nevent
);
2203 else if ( event
.LeftUp() && (item
== m_current
) &&
2204 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2205 HasFlag(wxTR_EDIT_LABELS
) )
2207 m_renameTimer
->Start( 100, TRUE
);
2211 // how should the selection work for this event?
2212 bool is_multiple
, extended_select
, unselect_others
;
2213 EventFlagsToSelType(GetWindowStyleFlag(),
2215 event
.ControlDown(),
2216 &is_multiple
, &extended_select
, &unselect_others
);
2225 SelectItem(item
, unselect_others
, extended_select
);
2227 if ( event
.LeftDClick() )
2229 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2230 nevent
.m_item
= item
;
2232 nevent
.SetEventObject( this );
2233 GetEventHandler()->ProcessEvent( nevent
);
2239 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2241 /* after all changes have been done to the tree control,
2242 * we actually redraw the tree when everything is over */
2249 CalculatePositions();
2251 AdjustMyScrollbars();
2254 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2260 dc
.SetFont(m_boldFont
);
2262 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2265 // restore normal font
2266 dc
.SetFont( m_normalFont
);
2270 int image
= item
->GetCurrentImage();
2271 if ( image
!= NO_IMAGE
)
2273 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2277 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2280 total_h
+= 2; // at least 2 pixels
2282 total_h
+= total_h
/10; // otherwise 10% extra spacing
2284 item
->SetHeight(total_h
);
2285 if (total_h
>m_lineHeight
)
2286 m_lineHeight
=total_h
;
2288 item
->SetWidth(image_w
+text_w
+2);
2291 // -----------------------------------------------------------------------------
2292 // for developper : y is now the top of the level
2293 // not the middle of it !
2294 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2296 int horizX
= level
*m_indent
;
2298 CalculateSize( item
, dc
);
2301 item
->SetX( horizX
+m_indent
+m_spacing
);
2303 y
+=GetLineHeight(item
);
2305 if ( !item
->IsExpanded() )
2307 // we dont need to calculate collapsed branches
2311 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2312 size_t n
, count
= children
.Count();
2313 for (n
= 0; n
< count
; ++n
)
2314 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2317 void wxTreeCtrl::CalculatePositions()
2319 if ( !m_anchor
) return;
2321 wxClientDC
dc(this);
2324 dc
.SetFont( m_normalFont
);
2326 dc
.SetPen( m_dottedPen
);
2327 //if(GetImageList() == NULL)
2328 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2331 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2334 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2336 if (m_dirty
) return;
2338 wxClientDC
dc(this);
2343 GetClientSize( &cw
, &ch
);
2346 rect
.x
= dc
.LogicalToDeviceX( 0 );
2348 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2351 Refresh( TRUE
, &rect
);
2353 AdjustMyScrollbars();
2356 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2358 if (m_dirty
) return;
2360 wxClientDC
dc(this);
2365 GetClientSize( &cw
, &ch
);
2368 rect
.x
= dc
.LogicalToDeviceX( 0 );
2369 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2371 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2373 Refresh( TRUE
, &rect
);