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
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
126 // TRUE if the point belongs to the item's button, otherwise it lies
127 // on the button's label
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
; }
143 // get them - may be NULL
144 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
145 // get them ensuring that the pointer is not NULL
146 wxTreeItemAttr
& Attr()
149 m_attr
= new wxTreeItemAttr
;
157 // tree ctrl images for the normal, selected, expanded and
158 // expanded+selected states
159 int m_images
[wxTreeItemIcon_Max
];
161 wxTreeItemData
*m_data
;
163 // use bitfields to save size
164 int m_isCollapsed
:1;
165 int m_hasHilight
:1; // same as focused
166 int m_hasPlus
:1; // used for item which doesn't have
167 // children but has a [+] button
168 int m_isBold
:1; // render the label in bold font
171 wxCoord m_height
, m_width
;
172 int m_xCross
, m_yCross
;
175 wxArrayGenericTreeItems m_children
;
176 wxGenericTreeItem
*m_parent
;
178 wxTreeItemAttr
*m_attr
;
181 // =============================================================================
183 // =============================================================================
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 // translate the key or mouse event flags to the type of selection we're
191 static void EventFlagsToSelType(long style
,
195 bool *extended_select
,
196 bool *unselect_others
)
198 *is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
199 *extended_select
= shiftDown
&& is_multiple
;
200 *unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
203 // -----------------------------------------------------------------------------
204 // wxTreeRenameTimer (internal)
205 // -----------------------------------------------------------------------------
207 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl
*owner
)
212 void wxTreeRenameTimer::Notify()
214 m_owner
->OnRenameTimer();
217 //-----------------------------------------------------------------------------
218 // wxTreeTextCtrl (internal)
219 //-----------------------------------------------------------------------------
221 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
223 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
224 EVT_CHAR (wxTreeTextCtrl::OnChar
)
225 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
228 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
233 const wxString
&value
,
237 const wxValidator
& validator
,
238 const wxString
&name
)
239 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
246 m_startValue
= value
;
249 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
251 if (event
.m_keyCode
== WXK_RETURN
)
254 (*m_res
) = GetValue();
256 if (!wxPendingDelete
.Member(this))
257 wxPendingDelete
.Append(this);
259 if ((*m_accept
) && ((*m_res
) != m_startValue
))
260 m_owner
->OnRenameAccept();
264 if (event
.m_keyCode
== WXK_ESCAPE
)
269 if (!wxPendingDelete
.Member(this))
270 wxPendingDelete
.Append(this);
277 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
279 if (!wxPendingDelete
.Member(this))
280 wxPendingDelete
.Append(this);
282 if ((*m_accept
) && ((*m_res
) != m_startValue
))
283 m_owner
->OnRenameAccept();
286 #define PIXELS_PER_UNIT 10
287 // -----------------------------------------------------------------------------
289 // -----------------------------------------------------------------------------
291 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
293 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
294 : wxNotifyEvent( commandType
, id
)
297 m_itemOld
= (wxGenericTreeItem
*)NULL
;
300 // -----------------------------------------------------------------------------
302 // -----------------------------------------------------------------------------
304 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
305 const wxString
& text
,
307 int image
, int selImage
,
308 wxTreeItemData
*data
)
311 m_images
[wxTreeItemIcon_Normal
] = image
;
312 m_images
[wxTreeItemIcon_Selected
] = selImage
;
313 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
314 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
318 m_xCross
= m_yCross
= 0;
322 m_isCollapsed
= TRUE
;
323 m_hasHilight
= FALSE
;
329 m_attr
= (wxTreeItemAttr
*)NULL
;
331 // We don't know the height here yet.
336 wxGenericTreeItem::~wxGenericTreeItem()
342 wxASSERT_MSG( m_children
.IsEmpty(),
343 wxT("please call DeleteChildren() before deleting the item") );
346 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl
*tree
)
348 size_t count
= m_children
.Count();
349 for ( size_t n
= 0; n
< count
; n
++ )
351 wxGenericTreeItem
*child
= m_children
[n
];
353 tree
->SendDeleteEvent(child
);
355 child
->DeleteChildren(tree
);
362 void wxGenericTreeItem::SetText( const wxString
&text
)
367 void wxGenericTreeItem::Reset()
370 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
372 m_images
[i
] = NO_IMAGE
;
377 m_height
= m_width
= 0;
384 m_isCollapsed
= TRUE
;
386 m_parent
= (wxGenericTreeItem
*)NULL
;
389 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
391 size_t count
= m_children
.Count();
395 size_t total
= count
;
396 for (size_t n
= 0; n
< count
; ++n
)
398 total
+= m_children
[n
]->GetChildrenCount();
404 void wxGenericTreeItem::SetCross( int x
, int y
)
410 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxTreeCtrl
*theTree
)
412 int bottomY
=m_y
+theTree
->GetLineHeight(this);
413 if ( y
< bottomY
) y
= bottomY
;
414 int width
= m_x
+ m_width
;
415 if ( x
< width
) x
= width
;
419 size_t count
= m_children
.Count();
420 for ( size_t n
= 0; n
< count
; ++n
)
422 m_children
[n
]->GetSize( x
, y
, theTree
);
427 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
428 const wxTreeCtrl
*theTree
,
431 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
433 if (point
.y
<m_y
+theTree
->GetLineHeight(this)/2)
434 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
436 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
438 // 5 is the size of the plus sign
439 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
440 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
441 (IsExpanded() || HasPlus()))
443 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
447 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
452 // assuming every image (normal and selected ) has the same size !
453 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
454 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
456 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
457 flags
|= wxTREE_HITTEST_ONITEMICON
;
459 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
465 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
466 if (point
.x
> m_x
+m_width
)
467 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
475 size_t count
= m_children
.Count();
476 for ( size_t n
= 0; n
< count
; n
++ )
478 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
485 flags
|=wxTREE_HITTEST_NOWHERE
;
487 return (wxGenericTreeItem
*) NULL
;
490 int wxGenericTreeItem::GetCurrentImage() const
492 int image
= NO_IMAGE
;
497 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
500 if ( image
== NO_IMAGE
)
502 // we usually fall back to the normal item, but try just the
503 // expanded one (and not selected) first in this case
504 image
= GetImage(wxTreeItemIcon_Expanded
);
510 image
= GetImage(wxTreeItemIcon_Selected
);
513 // may be it doesn't have the specific image we want, try the default one
515 if ( image
== NO_IMAGE
)
523 // -----------------------------------------------------------------------------
524 // wxTreeCtrl implementation
525 // -----------------------------------------------------------------------------
527 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxScrolledWindow
)
529 BEGIN_EVENT_TABLE(wxTreeCtrl
,wxScrolledWindow
)
530 EVT_PAINT (wxTreeCtrl::OnPaint
)
531 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse
)
532 EVT_CHAR (wxTreeCtrl::OnChar
)
533 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus
)
534 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus
)
535 EVT_IDLE (wxTreeCtrl::OnIdle
)
538 // -----------------------------------------------------------------------------
539 // construction/destruction
540 // -----------------------------------------------------------------------------
542 void wxTreeCtrl::Init()
546 m_anchor
= (wxGenericTreeItem
*) NULL
;
556 m_hilightBrush
= new wxBrush
558 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
561 m_normalBrush
= new wxBrush
563 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_LISTBOX
),
568 m_imageListState
= (wxImageList
*) NULL
;
572 m_renameTimer
= new wxTreeRenameTimer( this );
574 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
575 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
576 m_normalFont
.GetFamily(),
577 m_normalFont
.GetStyle(),
579 m_normalFont
.GetUnderlined());
582 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
583 const wxPoint
& pos
, const wxSize
& size
,
585 const wxValidator
&validator
,
586 const wxString
& name
)
590 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
593 SetValidator( validator
);
596 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
597 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
598 m_dottedPen
= wxPen( "grey", 0, 0 );
603 wxTreeCtrl::~wxTreeCtrl()
605 wxDELETE( m_hilightBrush
);
606 wxDELETE( m_normalBrush
);
610 delete m_renameTimer
;
613 // -----------------------------------------------------------------------------
615 // -----------------------------------------------------------------------------
617 size_t wxTreeCtrl::GetCount() const
619 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
622 void wxTreeCtrl::SetIndent(unsigned int indent
)
628 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
634 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
636 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
638 return item
.m_pItem
->GetChildrenCount(recursively
);
641 // -----------------------------------------------------------------------------
642 // functions to work with tree items
643 // -----------------------------------------------------------------------------
645 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
647 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
649 return item
.m_pItem
->GetText();
652 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
653 wxTreeItemIcon which
) const
655 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
657 return item
.m_pItem
->GetImage(which
);
660 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
662 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
664 return item
.m_pItem
->GetData();
667 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
669 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
672 wxGenericTreeItem
*pItem
= item
.m_pItem
;
673 pItem
->SetText(text
);
674 CalculateSize(pItem
, dc
);
678 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
680 wxTreeItemIcon which
)
682 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
684 wxGenericTreeItem
*pItem
= item
.m_pItem
;
685 pItem
->SetImage(image
, which
);
688 CalculateSize(pItem
, dc
);
692 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
694 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
696 item
.m_pItem
->SetData(data
);
699 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
701 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
703 wxGenericTreeItem
*pItem
= item
.m_pItem
;
704 pItem
->SetHasPlus(has
);
708 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
710 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
712 // avoid redrawing the tree if no real change
713 wxGenericTreeItem
*pItem
= item
.m_pItem
;
714 if ( pItem
->IsBold() != bold
)
716 pItem
->SetBold(bold
);
721 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
724 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
726 wxGenericTreeItem
*pItem
= item
.m_pItem
;
727 pItem
->Attr().SetTextColour(col
);
731 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
734 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
736 wxGenericTreeItem
*pItem
= item
.m_pItem
;
737 pItem
->Attr().SetBackgroundColour(col
);
741 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
743 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
745 wxGenericTreeItem
*pItem
= item
.m_pItem
;
746 pItem
->Attr().SetFont(font
);
750 // -----------------------------------------------------------------------------
751 // item status inquiries
752 // -----------------------------------------------------------------------------
754 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
756 wxFAIL_MSG(wxT("not implemented"));
761 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
763 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
765 return !item
.m_pItem
->GetChildren().IsEmpty();
768 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
770 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
772 return item
.m_pItem
->IsExpanded();
775 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
777 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
779 return item
.m_pItem
->IsSelected();
782 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
784 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
786 return item
.m_pItem
->IsBold();
789 // -----------------------------------------------------------------------------
791 // -----------------------------------------------------------------------------
793 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
795 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
797 return item
.m_pItem
->GetParent();
800 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
802 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
805 return GetNextChild(item
, cookie
);
808 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
810 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
812 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
813 if ( (size_t)cookie
< children
.Count() )
815 return children
.Item((size_t)cookie
++);
819 // there are no more of them
820 return wxTreeItemId();
824 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
826 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
828 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
829 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
832 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
834 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
836 wxGenericTreeItem
*i
= item
.m_pItem
;
837 wxGenericTreeItem
*parent
= i
->GetParent();
838 if ( parent
== NULL
)
840 // root item doesn't have any siblings
841 return wxTreeItemId();
844 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
845 int index
= siblings
.Index(i
);
846 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
848 size_t n
= (size_t)(index
+ 1);
849 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
852 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
854 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
856 wxGenericTreeItem
*i
= item
.m_pItem
;
857 wxGenericTreeItem
*parent
= i
->GetParent();
858 if ( parent
== NULL
)
860 // root item doesn't have any siblings
861 return wxTreeItemId();
864 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
865 int index
= siblings
.Index(i
);
866 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
868 return index
== 0 ? wxTreeItemId()
869 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
872 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
874 wxFAIL_MSG(wxT("not implemented"));
876 return wxTreeItemId();
879 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
881 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
883 wxFAIL_MSG(wxT("not implemented"));
885 return wxTreeItemId();
888 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
890 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
892 wxFAIL_MSG(wxT("not implemented"));
894 return wxTreeItemId();
897 // -----------------------------------------------------------------------------
899 // -----------------------------------------------------------------------------
901 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
903 const wxString
& text
,
904 int image
, int selImage
,
905 wxTreeItemData
*data
)
907 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
910 // should we give a warning here?
911 return AddRoot(text
, image
, selImage
, data
);
915 wxGenericTreeItem
*item
=
916 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
920 data
->m_pItem
= item
;
923 parent
->Insert( item
, previous
);
930 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
931 int image
, int selImage
,
932 wxTreeItemData
*data
)
934 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
937 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
938 image
, selImage
, data
);
941 data
->m_pItem
= m_anchor
;
944 if (!HasFlag(wxTR_MULTIPLE
))
946 m_current
= m_key_current
= m_anchor
;
947 m_current
->SetHilight( TRUE
);
951 AdjustMyScrollbars();
956 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
957 const wxString
& text
,
958 int image
, int selImage
,
959 wxTreeItemData
*data
)
961 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
964 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
965 const wxTreeItemId
& idPrevious
,
966 const wxString
& text
,
967 int image
, int selImage
,
968 wxTreeItemData
*data
)
970 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
973 // should we give a warning here?
974 return AddRoot(text
, image
, selImage
, data
);
977 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
978 wxASSERT_MSG( index
!= wxNOT_FOUND
,
979 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
981 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
984 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
986 const wxString
& text
,
987 int image
, int selImage
,
988 wxTreeItemData
*data
)
990 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
993 // should we give a warning here?
994 return AddRoot(text
, image
, selImage
, data
);
997 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1000 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1001 const wxString
& text
,
1002 int image
, int selImage
,
1003 wxTreeItemData
*data
)
1005 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
1008 // should we give a warning here?
1009 return AddRoot(text
, image
, selImage
, data
);
1012 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1013 image
, selImage
, data
);
1016 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1018 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1019 event
.m_item
= item
;
1020 event
.SetEventObject( this );
1021 ProcessEvent( event
);
1024 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1026 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1027 item
->DeleteChildren(this);
1032 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1034 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1035 wxGenericTreeItem
*parent
= item
->GetParent();
1039 parent
->GetChildren().Remove( item
); // remove by value
1042 item
->DeleteChildren(this);
1043 SendDeleteEvent(item
);
1049 void wxTreeCtrl::DeleteAllItems()
1053 m_anchor
->DeleteChildren(this);
1062 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1064 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1066 if ( !item
->HasPlus() )
1069 if ( item
->IsExpanded() )
1072 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1073 event
.m_item
= item
;
1074 event
.SetEventObject( this );
1076 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1077 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1079 // cancelled by program
1084 CalculatePositions();
1086 RefreshSubtree(item
);
1088 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1089 ProcessEvent( event
);
1092 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1094 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1096 if ( !item
->IsExpanded() )
1099 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1100 event
.m_item
= item
;
1101 event
.SetEventObject( this );
1102 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1104 // cancelled by program
1110 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1111 size_t count
= children
.Count();
1112 for ( size_t n
= 0; n
< count
; n
++ )
1114 Collapse(children
[n
]);
1117 CalculatePositions();
1119 RefreshSubtree(item
);
1121 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1122 ProcessEvent( event
);
1125 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1128 DeleteChildren(item
);
1131 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1133 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1135 if (item
->IsExpanded())
1141 void wxTreeCtrl::Unselect()
1145 m_current
->SetHilight( FALSE
);
1146 RefreshLine( m_current
);
1150 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1152 if (item
->IsSelected())
1154 item
->SetHilight(FALSE
);
1158 if (item
->HasChildren())
1160 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1161 size_t count
= children
.Count();
1162 for ( size_t n
= 0; n
< count
; ++n
)
1164 UnselectAllChildren(children
[n
]);
1169 void wxTreeCtrl::UnselectAll()
1171 UnselectAllChildren(GetRootItem().m_pItem
);
1174 // Recursive function !
1175 // To stop we must have crt_item<last_item
1177 // Tag all next children, when no more children,
1178 // Move to parent (not to tag)
1179 // Keep going... if we found last_item, we stop.
1180 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1182 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1184 if (parent
== NULL
) // This is root item
1185 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1187 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1188 int index
= children
.Index(crt_item
);
1189 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1191 size_t count
= children
.Count();
1192 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1194 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1197 return TagNextChildren(parent
, last_item
, select
);
1200 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1202 crt_item
->SetHilight(select
);
1203 RefreshLine(crt_item
);
1205 if (crt_item
==last_item
)
1208 if (crt_item
->HasChildren())
1210 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1211 size_t count
= children
.Count();
1212 for ( size_t n
= 0; n
< count
; ++n
)
1214 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1222 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1224 // item2 is not necessary after item1
1225 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1227 // choice first' and 'last' between item1 and item2
1228 if (item1
->GetY()<item2
->GetY())
1239 bool select
= m_current
->IsSelected();
1241 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1244 TagNextChildren(first
,last
,select
);
1247 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1248 bool unselect_others
,
1249 bool extended_select
)
1251 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1253 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1254 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1256 //wxCHECK_RET( ( (!unselect_others) && is_single),
1257 // wxT("this is a single selection tree") );
1259 // to keep going anyhow !!!
1262 if (item
->IsSelected())
1263 return; // nothing to do
1264 unselect_others
= TRUE
;
1265 extended_select
= FALSE
;
1267 else if ( unselect_others
&& item
->IsSelected() )
1269 // selection change if there is more than one item currently selected
1270 wxArrayTreeItemIds selected_items
;
1271 if ( GetSelections(selected_items
) == 1 )
1275 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1276 event
.m_item
= item
;
1277 event
.m_itemOld
= m_current
;
1278 event
.SetEventObject( this );
1279 // TODO : Here we don't send any selection mode yet !
1281 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1285 if (unselect_others
)
1287 if (is_single
) Unselect(); // to speed up thing
1292 if (extended_select
)
1294 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1295 // don't change the mark (m_current)
1296 SelectItemRange(m_current
, item
);
1300 bool select
=TRUE
; // the default
1302 // Check if we need to toggle hilight (ctrl mode)
1303 if (!unselect_others
)
1304 select
=!item
->IsSelected();
1306 m_current
= m_key_current
= item
;
1307 m_current
->SetHilight(select
);
1308 RefreshLine( m_current
);
1311 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1312 GetEventHandler()->ProcessEvent( event
);
1315 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1316 wxArrayTreeItemIds
&array
) const
1318 if ( item
->IsSelected() )
1319 array
.Add(wxTreeItemId(item
));
1321 if ( item
->HasChildren() )
1323 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1324 size_t count
= children
.GetCount();
1325 for ( size_t n
= 0; n
< count
; ++n
)
1326 FillArray(children
[n
],array
);
1330 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1333 FillArray(GetRootItem().m_pItem
, array
);
1335 return array
.Count();
1338 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1340 if (!item
.IsOk()) return;
1342 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1344 // first expand all parent branches
1345 wxGenericTreeItem
*parent
= gitem
->GetParent();
1349 parent
= parent
->GetParent();
1352 //if (parent) CalculatePositions();
1357 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1359 if (!item
.IsOk()) return;
1361 // We have to call this here because the label in
1362 // question might just have been added and no screen
1363 // update taken place.
1364 if (m_dirty
) wxYield();
1366 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1368 // now scroll to the item
1369 int item_y
= gitem
->GetY();
1373 ViewStart( &start_x
, &start_y
);
1374 start_y
*= PIXELS_PER_UNIT
;
1378 GetClientSize( &client_w
, &client_h
);
1380 if (item_y
< start_y
+3)
1385 m_anchor
->GetSize( x
, y
, this );
1386 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1387 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1388 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1389 // Item should appear at top
1390 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1392 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1397 m_anchor
->GetSize( x
, y
, this );
1398 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1399 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1400 item_y
+= PIXELS_PER_UNIT
+2;
1401 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1402 // Item should appear at bottom
1403 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
);
1407 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1408 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1410 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1411 wxGenericTreeItem
**item2
)
1413 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1415 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1418 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1419 const wxTreeItemId
& item2
)
1421 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1424 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1426 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1428 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1430 wxCHECK_RET( !s_treeBeingSorted
,
1431 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1433 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1434 if ( children
.Count() > 1 )
1436 s_treeBeingSorted
= this;
1437 children
.Sort(tree_ctrl_compare_func
);
1438 s_treeBeingSorted
= NULL
;
1442 //else: don't make the tree dirty as nothing changed
1445 wxImageList
*wxTreeCtrl::GetImageList() const
1447 return m_imageListNormal
;
1450 wxImageList
*wxTreeCtrl::GetStateImageList() const
1452 return m_imageListState
;
1455 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1457 m_imageListNormal
= imageList
;
1459 // Calculate a m_lineHeight value from the image sizes.
1460 // May be toggle off. Then wxTreeCtrl will spread when
1461 // necessary (which might look ugly).
1463 wxClientDC
dc(this);
1464 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1465 int width
= 0, height
= 0,
1466 n
= m_imageListNormal
->GetImageCount();
1468 for (int i
= 0; i
< n
; i
++)
1470 m_imageListNormal
->GetSize(i
, width
, height
);
1471 if (height
> m_lineHeight
) m_lineHeight
= height
;
1474 if (m_lineHeight
< 40)
1475 m_lineHeight
+= 2; // at least 2 pixels
1477 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1481 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1483 m_imageListState
= imageList
;
1486 // -----------------------------------------------------------------------------
1488 // -----------------------------------------------------------------------------
1490 void wxTreeCtrl::AdjustMyScrollbars()
1496 m_anchor
->GetSize( x
, y
, this );
1497 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1498 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1499 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1500 int y_pos
= GetScrollPos( wxVERTICAL
);
1501 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1505 SetScrollbars( 0, 0, 0, 0 );
1509 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1511 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1512 return item
->GetHeight();
1514 return m_lineHeight
;
1517 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1519 wxTreeItemAttr
*attr
= item
->GetAttributes();
1520 if ( attr
&& attr
->HasFont() )
1521 dc
.SetFont(attr
->GetFont());
1522 else if (item
->IsBold())
1523 dc
.SetFont(m_boldFont
);
1527 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1531 int image
= item
->GetCurrentImage();
1532 if ( image
!= NO_IMAGE
)
1534 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1538 int total_h
= GetLineHeight(item
);
1541 if ( attr
&& attr
->HasBackgroundColour() )
1542 colBg
= attr
->GetBackgroundColour();
1544 colBg
= m_backgroundColour
;
1545 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1547 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1549 if ( image
!= NO_IMAGE
)
1551 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1552 m_imageListNormal
->Draw( image
, dc
,
1554 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1555 wxIMAGELIST_DRAW_TRANSPARENT
);
1556 dc
.DestroyClippingRegion();
1559 dc
.SetBackgroundMode(wxTRANSPARENT
);
1560 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1561 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1563 // restore normal font
1564 dc
.SetFont( m_normalFont
);
1567 // Now y stands for the top of the item, whereas it used to stand for middle !
1568 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1570 int horizX
= level
*m_indent
;
1572 item
->SetX( horizX
+m_indent
+m_spacing
);
1576 y
+=GetLineHeight(item
)/2;
1578 item
->SetCross( horizX
+m_indent
, y
);
1580 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1581 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1583 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1585 int startX
= horizX
;
1586 int endX
= horizX
+ (m_indent
-5);
1588 // if (!item->HasChildren()) endX += (m_indent+5);
1589 if (!item
->HasChildren()) endX
+= 20;
1591 dc
.DrawLine( startX
, y
, endX
, y
);
1593 if (item
->HasPlus())
1595 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1596 dc
.SetPen( *wxGREY_PEN
);
1597 dc
.SetBrush( *wxWHITE_BRUSH
);
1598 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1600 dc
.SetPen( *wxBLACK_PEN
);
1601 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1602 if (!item
->IsExpanded())
1603 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1605 dc
.SetPen( m_dottedPen
);
1608 wxPen
*pen
= wxTRANSPARENT_PEN
;
1609 wxBrush
*brush
; // FIXME is this really needed?
1612 if ( item
->IsSelected() )
1614 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1616 brush
= m_hilightBrush
;
1624 wxTreeItemAttr
*attr
= item
->GetAttributes();
1625 if ( attr
&& attr
->HasTextColour() )
1626 colText
= attr
->GetTextColour();
1630 brush
= m_normalBrush
;
1634 dc
.SetTextForeground(colText
);
1636 dc
.SetBrush(*brush
);
1639 PaintItem(item
, dc
);
1641 // restore DC objects
1642 dc
.SetBrush( *wxWHITE_BRUSH
);
1643 dc
.SetPen( m_dottedPen
);
1644 dc
.SetTextForeground( *wxBLACK
);
1647 y
= oldY
+GetLineHeight(item
);
1649 if (item
->IsExpanded())
1651 oldY
+=GetLineHeight(item
)/2;
1654 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1655 size_t n
, count
= children
.Count();
1656 for ( n
= 0; n
< count
; ++n
)
1659 PaintLevel( children
[n
], dc
, level
+1, y
);
1662 // it may happen that the item is expanded but has no items (when you
1663 // delete all its children for example) - don't draw the vertical line
1667 semiOldY
+=GetLineHeight(children
[--n
])/2;
1668 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1673 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1677 wxGenericTreeItem
*i
=item
.m_pItem
;
1679 wxClientDC
dc(this);
1681 dc
.SetLogicalFunction(wxINVERT
);
1684 ViewStart(&x
,&h
); // we only need x
1685 GetClientSize(&w
,&h
); // we only need w
1687 h
=GetLineHeight(i
)+1;
1688 // 2 white column at border
1689 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1692 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1696 wxGenericTreeItem
*i
=item
.m_pItem
;
1698 wxClientDC
dc(this);
1700 dc
.SetLogicalFunction(wxINVERT
);
1705 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1708 dc
.DrawLine( 0, y
, w
, y
);
1711 // -----------------------------------------------------------------------------
1712 // wxWindows callbacks
1713 // -----------------------------------------------------------------------------
1715 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1723 dc
.SetFont( m_normalFont
);
1724 dc
.SetPen( m_dottedPen
);
1726 // this is now done dynamically
1727 //if(GetImageList() == NULL)
1728 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1731 PaintLevel( m_anchor
, dc
, 0, y
);
1734 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1738 if (m_current
) RefreshLine( m_current
);
1741 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1745 if (m_current
) RefreshLine( m_current
);
1748 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1750 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1751 te
.m_code
= (int)event
.KeyCode();
1752 te
.SetEventObject( this );
1753 GetEventHandler()->ProcessEvent( te
);
1755 if ( (m_current
== 0) || (m_key_current
== 0) )
1761 // how should the selection work for this event?
1762 bool is_multiple
, extended_select
, unselect_others
;
1763 EventFlagsToSelType(GetWindowStyleFlag(),
1765 event
.ControlDown(),
1766 &is_multiple
, &extended_select
, &unselect_others
);
1768 switch (event
.KeyCode())
1772 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1780 if (IsExpanded(m_current
))
1782 Collapse(m_current
);
1794 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1795 event
.m_item
= m_current
;
1797 event
.SetEventObject( this );
1798 GetEventHandler()->ProcessEvent( event
);
1802 // up goes to the previous sibling or to the last of its children if
1806 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1809 prev
= GetParent( m_key_current
);
1813 wxTreeItemId current
= m_key_current
;
1814 if (current
== GetFirstChild( prev
, cockie
))
1816 // otherwise we return to where we came from
1817 SelectItem( prev
, unselect_others
, extended_select
);
1818 m_key_current
=prev
.m_pItem
;
1819 EnsureVisible( prev
);
1826 while ( IsExpanded(prev
) && HasChildren(prev
) )
1828 wxTreeItemId child
= GetLastChild(prev
);
1835 SelectItem( prev
, unselect_others
, extended_select
);
1836 m_key_current
=prev
.m_pItem
;
1837 EnsureVisible( prev
);
1842 // left arrow goes to the parent
1845 wxTreeItemId prev
= GetParent( m_current
);
1848 EnsureVisible( prev
);
1849 SelectItem( prev
, unselect_others
, extended_select
);
1855 // this works the same as the down arrow except that we also expand the
1856 // item if it wasn't expanded yet
1862 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1865 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1866 SelectItem( child
, unselect_others
, extended_select
);
1867 m_key_current
=child
.m_pItem
;
1868 EnsureVisible( child
);
1872 wxTreeItemId next
= GetNextSibling( m_key_current
);
1876 wxTreeItemId current
= m_key_current
;
1877 while (current
&& !next
)
1879 current
= GetParent( current
);
1880 if (current
) next
= GetNextSibling( current
);
1886 SelectItem( next
, unselect_others
, extended_select
);
1887 m_key_current
=next
.m_pItem
;
1888 EnsureVisible( next
);
1894 // <End> selects the last visible tree item
1897 wxTreeItemId last
= GetRootItem();
1899 while ( last
.IsOk() && IsExpanded(last
) )
1901 wxTreeItemId lastChild
= GetLastChild(last
);
1903 // it may happen if the item was expanded but then all of
1904 // its children have been deleted - so IsExpanded() returned
1905 // TRUE, but GetLastChild() returned invalid item
1914 EnsureVisible( last
);
1915 SelectItem( last
, unselect_others
, extended_select
);
1920 // <Home> selects the root item
1923 wxTreeItemId prev
= GetRootItem();
1926 EnsureVisible( prev
);
1927 SelectItem( prev
, unselect_others
, extended_select
);
1937 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1939 // We have to call this here because the label in
1940 // question might just have been added and no screen
1941 // update taken place.
1942 if (m_dirty
) wxYield();
1944 wxClientDC
dc(this);
1946 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
1947 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
1952 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1953 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1954 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1955 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1957 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1962 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1964 if (!item
.IsOk()) return;
1966 m_currentEdit
= item
.m_pItem
;
1968 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1969 te
.m_item
= m_currentEdit
;
1970 te
.SetEventObject( this );
1971 GetEventHandler()->ProcessEvent( te
);
1973 if (!te
.IsAllowed()) return;
1975 // We have to call this here because the label in
1976 // question might just have been added and no screen
1977 // update taken place.
1978 if (m_dirty
) wxYield();
1980 wxString s
= m_currentEdit
->GetText();
1981 int x
= m_currentEdit
->GetX();
1982 int y
= m_currentEdit
->GetY();
1983 int w
= m_currentEdit
->GetWidth();
1984 int h
= m_currentEdit
->GetHeight();
1989 int image
= m_currentEdit
->GetCurrentImage();
1990 if ( image
!= NO_IMAGE
)
1992 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1996 w
-= image_w
+ 4; // I don't know why +4 is needed
1998 wxClientDC
dc(this);
2000 x
= dc
.LogicalToDeviceX( x
);
2001 y
= dc
.LogicalToDeviceY( y
);
2003 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
2004 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
2008 void wxTreeCtrl::OnRenameTimer()
2013 void wxTreeCtrl::OnRenameAccept()
2015 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2016 le
.m_item
= m_currentEdit
;
2017 le
.SetEventObject( this );
2018 le
.m_label
= m_renameRes
;
2019 GetEventHandler()->ProcessEvent( le
);
2021 if (!le
.IsAllowed()) return;
2023 SetItemText( m_currentEdit
, m_renameRes
);
2026 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
2028 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
2030 if ( !m_anchor
) return;
2032 wxClientDC
dc(this);
2034 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2035 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2038 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2039 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
2041 if (event
.Dragging())
2043 if (m_dragCount
== 0)
2044 m_dragStart
= wxPoint(x
,y
);
2048 if (m_dragCount
!= 3) return;
2050 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2051 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2053 wxTreeEvent
nevent( command
, GetId() );
2054 nevent
.m_item
= m_current
;
2055 nevent
.SetEventObject(this);
2056 GetEventHandler()->ProcessEvent(nevent
);
2064 if (item
== NULL
) return; /* we hit the blank area */
2066 if (event
.RightDown()) {
2067 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
2070 nevent
.SetEventObject(this);
2071 GetEventHandler()->ProcessEvent(nevent
);
2075 if (event
.LeftUp() && (item
== m_current
) &&
2076 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2077 HasFlag(wxTR_EDIT_LABELS
) )
2079 m_renameTimer
->Start( 100, TRUE
);
2083 // how should the selection work for this event?
2084 bool is_multiple
, extended_select
, unselect_others
;
2085 EventFlagsToSelType(GetWindowStyleFlag(),
2087 event
.ControlDown(),
2088 &is_multiple
, &extended_select
, &unselect_others
);
2097 SelectItem(item
, unselect_others
, extended_select
);
2099 if (event
.LeftDClick())
2101 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2102 event
.m_item
= item
;
2104 event
.SetEventObject( this );
2105 GetEventHandler()->ProcessEvent( event
);
2109 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2111 /* after all changes have been done to the tree control,
2112 * we actually redraw the tree when everything is over */
2119 CalculatePositions();
2121 AdjustMyScrollbars();
2124 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2130 dc
.SetFont(m_boldFont
);
2132 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2135 // restore normal font
2136 dc
.SetFont( m_normalFont
);
2140 int image
= item
->GetCurrentImage();
2141 if ( image
!= NO_IMAGE
)
2143 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2147 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2150 total_h
+= 2; // at least 2 pixels
2152 total_h
+= total_h
/10; // otherwise 10% extra spacing
2154 item
->SetHeight(total_h
);
2155 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2157 item
->SetWidth(image_w
+text_w
+2);
2160 // -----------------------------------------------------------------------------
2161 // for developper : y is now the top of the level
2162 // not the middle of it !
2163 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2165 int horizX
= level
*m_indent
;
2167 CalculateSize( item
, dc
);
2170 item
->SetX( horizX
+m_indent
+m_spacing
);
2172 y
+=GetLineHeight(item
);
2174 if ( !item
->IsExpanded() )
2176 // we dont need to calculate collapsed branches
2180 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2181 size_t n
, count
= children
.Count();
2182 for (n
= 0; n
< count
; ++n
)
2183 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2186 void wxTreeCtrl::CalculatePositions()
2188 if ( !m_anchor
) return;
2190 wxClientDC
dc(this);
2193 dc
.SetFont( m_normalFont
);
2195 dc
.SetPen( m_dottedPen
);
2196 //if(GetImageList() == NULL)
2197 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2200 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2203 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2205 if (m_dirty
) return;
2207 wxClientDC
dc(this);
2212 GetClientSize( &cw
, &ch
);
2215 rect
.x
= dc
.LogicalToDeviceX( 0 );
2217 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2220 Refresh( TRUE
, &rect
);
2222 AdjustMyScrollbars();
2225 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2227 if (m_dirty
) return;
2229 wxClientDC
dc(this);
2234 GetClientSize( &cw
, &ch
);
2237 rect
.x
= dc
.LogicalToDeviceX( 0 );
2238 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2240 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2242 Refresh( TRUE
, &rect
);