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
),
563 m_imageListState
= (wxImageList
*) NULL
;
567 m_renameTimer
= new wxTreeRenameTimer( this );
569 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
570 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
571 m_normalFont
.GetFamily(),
572 m_normalFont
.GetStyle(),
574 m_normalFont
.GetUnderlined());
577 bool wxTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
578 const wxPoint
& pos
, const wxSize
& size
,
580 const wxValidator
&validator
,
581 const wxString
& name
)
585 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
588 SetValidator( validator
);
591 SetBackgroundColour( *wxWHITE
);
592 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
593 m_dottedPen
= wxPen( "grey", 0, 0 );
598 wxTreeCtrl::~wxTreeCtrl()
600 wxDELETE( m_hilightBrush
);
604 delete m_renameTimer
;
607 // -----------------------------------------------------------------------------
609 // -----------------------------------------------------------------------------
611 size_t wxTreeCtrl::GetCount() const
613 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
616 void wxTreeCtrl::SetIndent(unsigned int indent
)
622 void wxTreeCtrl::SetSpacing(unsigned int spacing
)
628 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
630 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
632 return item
.m_pItem
->GetChildrenCount(recursively
);
635 // -----------------------------------------------------------------------------
636 // functions to work with tree items
637 // -----------------------------------------------------------------------------
639 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
641 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
643 return item
.m_pItem
->GetText();
646 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
647 wxTreeItemIcon which
) const
649 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
651 return item
.m_pItem
->GetImage(which
);
654 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
656 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
658 return item
.m_pItem
->GetData();
661 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
663 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
666 wxGenericTreeItem
*pItem
= item
.m_pItem
;
667 pItem
->SetText(text
);
668 CalculateSize(pItem
, dc
);
672 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
674 wxTreeItemIcon which
)
676 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
678 wxGenericTreeItem
*pItem
= item
.m_pItem
;
679 pItem
->SetImage(image
, which
);
682 CalculateSize(pItem
, dc
);
686 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
688 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
690 item
.m_pItem
->SetData(data
);
693 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
695 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
697 wxGenericTreeItem
*pItem
= item
.m_pItem
;
698 pItem
->SetHasPlus(has
);
702 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
704 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
706 // avoid redrawing the tree if no real change
707 wxGenericTreeItem
*pItem
= item
.m_pItem
;
708 if ( pItem
->IsBold() != bold
)
710 pItem
->SetBold(bold
);
715 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
718 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
720 wxGenericTreeItem
*pItem
= item
.m_pItem
;
721 pItem
->Attr().SetTextColour(col
);
725 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
728 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
730 wxGenericTreeItem
*pItem
= item
.m_pItem
;
731 pItem
->Attr().SetBackgroundColour(col
);
735 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
737 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
739 wxGenericTreeItem
*pItem
= item
.m_pItem
;
740 pItem
->Attr().SetFont(font
);
744 // -----------------------------------------------------------------------------
745 // item status inquiries
746 // -----------------------------------------------------------------------------
748 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
750 wxFAIL_MSG(wxT("not implemented"));
755 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
757 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
759 return !item
.m_pItem
->GetChildren().IsEmpty();
762 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
764 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
766 return item
.m_pItem
->IsExpanded();
769 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
771 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
773 return item
.m_pItem
->IsSelected();
776 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
778 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
780 return item
.m_pItem
->IsBold();
783 // -----------------------------------------------------------------------------
785 // -----------------------------------------------------------------------------
787 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
789 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
791 return item
.m_pItem
->GetParent();
794 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
796 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
799 return GetNextChild(item
, cookie
);
802 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
804 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
806 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
807 if ( (size_t)cookie
< children
.Count() )
809 return children
.Item((size_t)cookie
++);
813 // there are no more of them
814 return wxTreeItemId();
818 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
820 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
822 wxArrayGenericTreeItems
& children
= item
.m_pItem
->GetChildren();
823 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
826 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
828 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
830 wxGenericTreeItem
*i
= item
.m_pItem
;
831 wxGenericTreeItem
*parent
= i
->GetParent();
832 if ( parent
== NULL
)
834 // root item doesn't have any siblings
835 return wxTreeItemId();
838 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
839 int index
= siblings
.Index(i
);
840 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
842 size_t n
= (size_t)(index
+ 1);
843 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
846 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
848 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
850 wxGenericTreeItem
*i
= item
.m_pItem
;
851 wxGenericTreeItem
*parent
= i
->GetParent();
852 if ( parent
== NULL
)
854 // root item doesn't have any siblings
855 return wxTreeItemId();
858 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
859 int index
= siblings
.Index(i
);
860 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
862 return index
== 0 ? wxTreeItemId()
863 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
866 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
868 wxFAIL_MSG(wxT("not implemented"));
870 return wxTreeItemId();
873 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
875 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
877 wxFAIL_MSG(wxT("not implemented"));
879 return wxTreeItemId();
882 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
884 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
886 wxFAIL_MSG(wxT("not implemented"));
888 return wxTreeItemId();
891 // -----------------------------------------------------------------------------
893 // -----------------------------------------------------------------------------
895 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
897 const wxString
& text
,
898 int image
, int selImage
,
899 wxTreeItemData
*data
)
901 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
904 // should we give a warning here?
905 return AddRoot(text
, image
, selImage
, data
);
909 wxGenericTreeItem
*item
=
910 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
914 data
->m_pItem
= item
;
917 parent
->Insert( item
, previous
);
924 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
925 int image
, int selImage
,
926 wxTreeItemData
*data
)
928 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
931 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
932 image
, selImage
, data
);
935 data
->m_pItem
= m_anchor
;
938 if (!HasFlag(wxTR_MULTIPLE
))
940 m_current
= m_key_current
= m_anchor
;
941 m_current
->SetHilight( TRUE
);
945 AdjustMyScrollbars();
950 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
951 const wxString
& text
,
952 int image
, int selImage
,
953 wxTreeItemData
*data
)
955 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
958 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
959 const wxTreeItemId
& idPrevious
,
960 const wxString
& text
,
961 int image
, int selImage
,
962 wxTreeItemData
*data
)
964 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
967 // should we give a warning here?
968 return AddRoot(text
, image
, selImage
, data
);
971 int index
= parent
->GetChildren().Index(idPrevious
.m_pItem
);
972 wxASSERT_MSG( index
!= wxNOT_FOUND
,
973 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
975 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
978 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
980 const wxString
& text
,
981 int image
, int selImage
,
982 wxTreeItemData
*data
)
984 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
987 // should we give a warning here?
988 return AddRoot(text
, image
, selImage
, data
);
991 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
994 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
995 const wxString
& text
,
996 int image
, int selImage
,
997 wxTreeItemData
*data
)
999 wxGenericTreeItem
*parent
= parentId
.m_pItem
;
1002 // should we give a warning here?
1003 return AddRoot(text
, image
, selImage
, data
);
1006 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1007 image
, selImage
, data
);
1010 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1012 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1013 event
.m_item
= item
;
1014 event
.SetEventObject( this );
1015 ProcessEvent( event
);
1018 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1020 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1021 item
->DeleteChildren(this);
1026 void wxTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1028 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1029 wxGenericTreeItem
*parent
= item
->GetParent();
1033 parent
->GetChildren().Remove( item
); // remove by value
1036 item
->DeleteChildren(this);
1037 SendDeleteEvent(item
);
1043 void wxTreeCtrl::DeleteAllItems()
1047 m_anchor
->DeleteChildren(this);
1056 void wxTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1058 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1060 if ( !item
->HasPlus() )
1063 if ( item
->IsExpanded() )
1066 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1067 event
.m_item
= item
;
1068 event
.SetEventObject( this );
1070 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1071 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1073 // cancelled by program
1078 CalculatePositions();
1080 RefreshSubtree(item
);
1082 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1083 ProcessEvent( event
);
1086 void wxTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1088 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1090 if ( !item
->IsExpanded() )
1093 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1094 event
.m_item
= item
;
1095 event
.SetEventObject( this );
1096 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1098 // cancelled by program
1104 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1105 size_t count
= children
.Count();
1106 for ( size_t n
= 0; n
< count
; n
++ )
1108 Collapse(children
[n
]);
1111 CalculatePositions();
1113 RefreshSubtree(item
);
1115 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1116 ProcessEvent( event
);
1119 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1122 DeleteChildren(item
);
1125 void wxTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1127 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1129 if (item
->IsExpanded())
1135 void wxTreeCtrl::Unselect()
1139 m_current
->SetHilight( FALSE
);
1140 RefreshLine( m_current
);
1144 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1146 if (item
->IsSelected())
1148 item
->SetHilight(FALSE
);
1152 if (item
->HasChildren())
1154 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1155 size_t count
= children
.Count();
1156 for ( size_t n
= 0; n
< count
; ++n
)
1158 UnselectAllChildren(children
[n
]);
1163 void wxTreeCtrl::UnselectAll()
1165 UnselectAllChildren(GetRootItem().m_pItem
);
1168 // Recursive function !
1169 // To stop we must have crt_item<last_item
1171 // Tag all next children, when no more children,
1172 // Move to parent (not to tag)
1173 // Keep going... if we found last_item, we stop.
1174 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1176 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1178 if (parent
== NULL
) // This is root item
1179 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1181 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1182 int index
= children
.Index(crt_item
);
1183 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1185 size_t count
= children
.Count();
1186 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1188 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1191 return TagNextChildren(parent
, last_item
, select
);
1194 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1196 crt_item
->SetHilight(select
);
1197 RefreshLine(crt_item
);
1199 if (crt_item
==last_item
)
1202 if (crt_item
->HasChildren())
1204 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1205 size_t count
= children
.Count();
1206 for ( size_t n
= 0; n
< count
; ++n
)
1208 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1216 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1218 // item2 is not necessary after item1
1219 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1221 // choice first' and 'last' between item1 and item2
1222 if (item1
->GetY()<item2
->GetY())
1233 bool select
= m_current
->IsSelected();
1235 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1238 TagNextChildren(first
,last
,select
);
1241 void wxTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1242 bool unselect_others
,
1243 bool extended_select
)
1245 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1247 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1248 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1250 //wxCHECK_RET( ( (!unselect_others) && is_single),
1251 // wxT("this is a single selection tree") );
1253 // to keep going anyhow !!!
1256 if (item
->IsSelected())
1257 return; // nothing to do
1258 unselect_others
= TRUE
;
1259 extended_select
= FALSE
;
1261 else if ( unselect_others
&& item
->IsSelected() )
1263 // selection change if there is more than one item currently selected
1264 wxArrayTreeItemIds selected_items
;
1265 if ( GetSelections(selected_items
) == 1 )
1269 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1270 event
.m_item
= item
;
1271 event
.m_itemOld
= m_current
;
1272 event
.SetEventObject( this );
1273 // TODO : Here we don't send any selection mode yet !
1275 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1279 if (unselect_others
)
1281 if (is_single
) Unselect(); // to speed up thing
1286 if (extended_select
)
1288 if (m_current
== NULL
) m_current
=m_key_current
=GetRootItem().m_pItem
;
1289 // don't change the mark (m_current)
1290 SelectItemRange(m_current
, item
);
1294 bool select
=TRUE
; // the default
1296 // Check if we need to toggle hilight (ctrl mode)
1297 if (!unselect_others
)
1298 select
=!item
->IsSelected();
1300 m_current
= m_key_current
= item
;
1301 m_current
->SetHilight(select
);
1302 RefreshLine( m_current
);
1305 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1306 GetEventHandler()->ProcessEvent( event
);
1309 void wxTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1310 wxArrayTreeItemIds
&array
) const
1312 if ( item
->IsSelected() )
1313 array
.Add(wxTreeItemId(item
));
1315 if ( item
->HasChildren() )
1317 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1318 size_t count
= children
.GetCount();
1319 for ( size_t n
= 0; n
< count
; ++n
)
1320 FillArray(children
[n
],array
);
1324 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1327 FillArray(GetRootItem().m_pItem
, array
);
1329 return array
.Count();
1332 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1334 if (!item
.IsOk()) return;
1336 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1338 // first expand all parent branches
1339 wxGenericTreeItem
*parent
= gitem
->GetParent();
1343 parent
= parent
->GetParent();
1346 //if (parent) CalculatePositions();
1351 void wxTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1353 if (!item
.IsOk()) return;
1355 // We have to call this here because the label in
1356 // question might just have been added and no screen
1357 // update taken place.
1358 if (m_dirty
) wxYield();
1360 wxGenericTreeItem
*gitem
= item
.m_pItem
;
1362 // now scroll to the item
1363 int item_y
= gitem
->GetY();
1367 ViewStart( &start_x
, &start_y
);
1368 start_y
*= PIXELS_PER_UNIT
;
1372 GetClientSize( &client_w
, &client_h
);
1374 if (item_y
< start_y
+3)
1379 m_anchor
->GetSize( x
, y
, this );
1380 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1381 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1382 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1383 // Item should appear at top
1384 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1386 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1391 m_anchor
->GetSize( x
, y
, this );
1392 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1393 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1394 item_y
+= PIXELS_PER_UNIT
+2;
1395 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1396 // Item should appear at bottom
1397 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
);
1401 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1402 static wxTreeCtrl
*s_treeBeingSorted
= NULL
;
1404 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1405 wxGenericTreeItem
**item2
)
1407 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1409 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1412 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1413 const wxTreeItemId
& item2
)
1415 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1418 void wxTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1420 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1422 wxGenericTreeItem
*item
= itemId
.m_pItem
;
1424 wxCHECK_RET( !s_treeBeingSorted
,
1425 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1427 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1428 if ( children
.Count() > 1 )
1430 s_treeBeingSorted
= this;
1431 children
.Sort(tree_ctrl_compare_func
);
1432 s_treeBeingSorted
= NULL
;
1436 //else: don't make the tree dirty as nothing changed
1439 wxImageList
*wxTreeCtrl::GetImageList() const
1441 return m_imageListNormal
;
1444 wxImageList
*wxTreeCtrl::GetStateImageList() const
1446 return m_imageListState
;
1449 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
1451 m_imageListNormal
= imageList
;
1453 // Calculate a m_lineHeight value from the image sizes.
1454 // May be toggle off. Then wxTreeCtrl will spread when
1455 // necessary (which might look ugly).
1457 wxClientDC
dc(this);
1458 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1459 int width
= 0, height
= 0,
1460 n
= m_imageListNormal
->GetImageCount();
1462 for (int i
= 0; i
< n
; i
++)
1464 m_imageListNormal
->GetSize(i
, width
, height
);
1465 if (height
> m_lineHeight
) m_lineHeight
= height
;
1468 if (m_lineHeight
< 40)
1469 m_lineHeight
+= 2; // at least 2 pixels
1471 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1475 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1477 m_imageListState
= imageList
;
1480 // -----------------------------------------------------------------------------
1482 // -----------------------------------------------------------------------------
1484 void wxTreeCtrl::AdjustMyScrollbars()
1490 m_anchor
->GetSize( x
, y
, this );
1491 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1492 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1493 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1494 int y_pos
= GetScrollPos( wxVERTICAL
);
1495 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1499 SetScrollbars( 0, 0, 0, 0 );
1503 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1505 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1506 return item
->GetHeight();
1508 return m_lineHeight
;
1511 void wxTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1513 wxTreeItemAttr
*attr
= item
->GetAttributes();
1514 if ( attr
&& attr
->HasFont() )
1515 dc
.SetFont(attr
->GetFont());
1516 else if (item
->IsBold())
1517 dc
.SetFont(m_boldFont
);
1521 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1525 int image
= item
->GetCurrentImage();
1526 if ( image
!= NO_IMAGE
)
1528 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1532 int total_h
= GetLineHeight(item
);
1535 if ( attr
&& attr
->HasBackgroundColour() )
1536 colBg
= attr
->GetBackgroundColour();
1538 colBg
= m_backgroundColour
;
1539 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1541 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1543 if ( image
!= NO_IMAGE
)
1545 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1546 m_imageListNormal
->Draw( image
, dc
,
1548 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1549 wxIMAGELIST_DRAW_TRANSPARENT
);
1550 dc
.DestroyClippingRegion();
1553 dc
.SetBackgroundMode(wxTRANSPARENT
);
1554 dc
.DrawText( item
->GetText(), image_w
+ item
->GetX(), item
->GetY()
1555 + ((total_h
> text_h
) ? (total_h
- text_h
)/2 : 0));
1557 // restore normal font
1558 dc
.SetFont( m_normalFont
);
1561 // Now y stands for the top of the item, whereas it used to stand for middle !
1562 void wxTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1564 int horizX
= level
*m_indent
;
1566 item
->SetX( horizX
+m_indent
+m_spacing
);
1570 y
+=GetLineHeight(item
)/2;
1572 item
->SetCross( horizX
+m_indent
, y
);
1574 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1575 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1577 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1579 int startX
= horizX
;
1580 int endX
= horizX
+ (m_indent
-5);
1582 // if (!item->HasChildren()) endX += (m_indent+5);
1583 if (!item
->HasChildren()) endX
+= 20;
1585 dc
.DrawLine( startX
, y
, endX
, y
);
1587 if (item
->HasPlus())
1589 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1590 dc
.SetPen( *wxGREY_PEN
);
1591 dc
.SetBrush( *wxWHITE_BRUSH
);
1592 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1594 dc
.SetPen( *wxBLACK_PEN
);
1595 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1596 if (!item
->IsExpanded())
1597 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1599 dc
.SetPen( m_dottedPen
);
1602 wxPen
*pen
= wxTRANSPARENT_PEN
;
1603 wxBrush
*brush
; // FIXME is this really needed?
1606 if ( item
->IsSelected() )
1608 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1610 brush
= m_hilightBrush
;
1618 wxTreeItemAttr
*attr
= item
->GetAttributes();
1619 if ( attr
&& attr
->HasTextColour() )
1620 colText
= attr
->GetTextColour();
1624 brush
= wxWHITE_BRUSH
;
1628 dc
.SetTextForeground(colText
);
1630 dc
.SetBrush(*brush
);
1633 PaintItem(item
, dc
);
1635 // restore DC objects
1636 dc
.SetBrush( *wxWHITE_BRUSH
);
1637 dc
.SetPen( m_dottedPen
);
1638 dc
.SetTextForeground( *wxBLACK
);
1641 y
= oldY
+GetLineHeight(item
);
1643 if (item
->IsExpanded())
1645 oldY
+=GetLineHeight(item
)/2;
1648 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1649 size_t n
, count
= children
.Count();
1650 for ( n
= 0; n
< count
; ++n
)
1653 PaintLevel( children
[n
], dc
, level
+1, y
);
1656 // it may happen that the item is expanded but has no items (when you
1657 // delete all its children for example) - don't draw the vertical line
1661 semiOldY
+=GetLineHeight(children
[--n
])/2;
1662 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1667 void wxTreeCtrl::DrawBorder(wxTreeItemId
&item
)
1671 wxGenericTreeItem
*i
=item
.m_pItem
;
1673 wxClientDC
dc(this);
1675 dc
.SetLogicalFunction(wxINVERT
);
1678 ViewStart(&x
,&h
); // we only need x
1679 GetClientSize(&w
,&h
); // we only need w
1681 h
=GetLineHeight(i
)+1;
1682 // 2 white column at border
1683 dc
.DrawRectangle( PIXELS_PER_UNIT
*x
+2, i
->GetY()-1, w
-6, h
);
1686 void wxTreeCtrl::DrawLine(wxTreeItemId
&item
, bool below
)
1690 wxGenericTreeItem
*i
=item
.m_pItem
;
1692 wxClientDC
dc(this);
1694 dc
.SetLogicalFunction(wxINVERT
);
1699 if (below
) y
=i
->GetY()+GetLineHeight(i
)-1;
1702 dc
.DrawLine( 0, y
, w
, y
);
1705 // -----------------------------------------------------------------------------
1706 // wxWindows callbacks
1707 // -----------------------------------------------------------------------------
1709 void wxTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1717 dc
.SetFont( m_normalFont
);
1718 dc
.SetPen( m_dottedPen
);
1720 // this is now done dynamically
1721 //if(GetImageList() == NULL)
1722 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1725 PaintLevel( m_anchor
, dc
, 0, y
);
1728 void wxTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1732 if (m_current
) RefreshLine( m_current
);
1735 void wxTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1739 if (m_current
) RefreshLine( m_current
);
1742 void wxTreeCtrl::OnChar( wxKeyEvent
&event
)
1744 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1745 te
.m_code
= (int)event
.KeyCode();
1746 te
.SetEventObject( this );
1747 GetEventHandler()->ProcessEvent( te
);
1749 if ( (m_current
== 0) || (m_key_current
== 0) )
1755 // how should the selection work for this event?
1756 bool is_multiple
, extended_select
, unselect_others
;
1757 EventFlagsToSelType(GetWindowStyleFlag(),
1759 event
.ControlDown(),
1760 &is_multiple
, &extended_select
, &unselect_others
);
1762 switch (event
.KeyCode())
1766 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1774 if (IsExpanded(m_current
))
1776 Collapse(m_current
);
1788 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1789 event
.m_item
= m_current
;
1791 event
.SetEventObject( this );
1792 GetEventHandler()->ProcessEvent( event
);
1796 // up goes to the previous sibling or to the last of its children if
1800 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1803 prev
= GetParent( m_key_current
);
1807 wxTreeItemId current
= m_key_current
;
1808 if (current
== GetFirstChild( prev
, cockie
))
1810 // otherwise we return to where we came from
1811 SelectItem( prev
, unselect_others
, extended_select
);
1812 m_key_current
=prev
.m_pItem
;
1813 EnsureVisible( prev
);
1820 while ( IsExpanded(prev
) && HasChildren(prev
) )
1822 wxTreeItemId child
= GetLastChild(prev
);
1829 SelectItem( prev
, unselect_others
, extended_select
);
1830 m_key_current
=prev
.m_pItem
;
1831 EnsureVisible( prev
);
1836 // left arrow goes to the parent
1839 wxTreeItemId prev
= GetParent( m_current
);
1842 EnsureVisible( prev
);
1843 SelectItem( prev
, unselect_others
, extended_select
);
1849 // this works the same as the down arrow except that we also expand the
1850 // item if it wasn't expanded yet
1856 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
1859 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
1860 SelectItem( child
, unselect_others
, extended_select
);
1861 m_key_current
=child
.m_pItem
;
1862 EnsureVisible( child
);
1866 wxTreeItemId next
= GetNextSibling( m_key_current
);
1870 wxTreeItemId current
= m_key_current
;
1871 while (current
&& !next
)
1873 current
= GetParent( current
);
1874 if (current
) next
= GetNextSibling( current
);
1880 SelectItem( next
, unselect_others
, extended_select
);
1881 m_key_current
=next
.m_pItem
;
1882 EnsureVisible( next
);
1888 // <End> selects the last visible tree item
1891 wxTreeItemId last
= GetRootItem();
1893 while ( last
.IsOk() && IsExpanded(last
) )
1895 wxTreeItemId lastChild
= GetLastChild(last
);
1897 // it may happen if the item was expanded but then all of
1898 // its children have been deleted - so IsExpanded() returned
1899 // TRUE, but GetLastChild() returned invalid item
1908 EnsureVisible( last
);
1909 SelectItem( last
, unselect_others
, extended_select
);
1914 // <Home> selects the root item
1917 wxTreeItemId prev
= GetRootItem();
1920 EnsureVisible( prev
);
1921 SelectItem( prev
, unselect_others
, extended_select
);
1931 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1933 // We have to call this here because the label in
1934 // question might just have been added and no screen
1935 // update taken place.
1936 if (m_dirty
) wxYield();
1938 wxClientDC
dc(this);
1940 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
1941 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
1946 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
1947 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
1948 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
1949 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
1951 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
1956 void wxTreeCtrl::Edit( const wxTreeItemId
& item
)
1958 if (!item
.IsOk()) return;
1960 m_currentEdit
= item
.m_pItem
;
1962 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
1963 te
.m_item
= m_currentEdit
;
1964 te
.SetEventObject( this );
1965 GetEventHandler()->ProcessEvent( te
);
1967 if (!te
.IsAllowed()) return;
1969 // We have to call this here because the label in
1970 // question might just have been added and no screen
1971 // update taken place.
1972 if (m_dirty
) wxYield();
1974 wxString s
= m_currentEdit
->GetText();
1975 int x
= m_currentEdit
->GetX();
1976 int y
= m_currentEdit
->GetY();
1977 int w
= m_currentEdit
->GetWidth();
1978 int h
= m_currentEdit
->GetHeight();
1983 int image
= m_currentEdit
->GetCurrentImage();
1984 if ( image
!= NO_IMAGE
)
1986 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1990 w
-= image_w
+ 4; // I don't know why +4 is needed
1992 wxClientDC
dc(this);
1994 x
= dc
.LogicalToDeviceX( x
);
1995 y
= dc
.LogicalToDeviceY( y
);
1997 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
1998 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
2002 void wxTreeCtrl::OnRenameTimer()
2007 void wxTreeCtrl::OnRenameAccept()
2009 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2010 le
.m_item
= m_currentEdit
;
2011 le
.SetEventObject( this );
2012 le
.m_label
= m_renameRes
;
2013 GetEventHandler()->ProcessEvent( le
);
2015 if (!le
.IsAllowed()) return;
2017 SetItemText( m_currentEdit
, m_renameRes
);
2020 void wxTreeCtrl::OnMouse( wxMouseEvent
&event
)
2022 if ( !(event
.LeftUp() || event
.RightDown() || event
.LeftDClick() || event
.Dragging()) ) return;
2024 if ( !m_anchor
) return;
2026 wxClientDC
dc(this);
2028 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2029 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2032 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2033 bool onButton
= flags
& wxTREE_HITTEST_ONITEMBUTTON
;
2035 if (event
.Dragging())
2037 if (m_dragCount
== 0)
2038 m_dragStart
= wxPoint(x
,y
);
2042 if (m_dragCount
!= 3) return;
2044 int command
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2045 if (event
.RightIsDown()) command
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2047 wxTreeEvent
nevent( command
, GetId() );
2048 nevent
.m_item
= m_current
;
2049 nevent
.SetEventObject(this);
2050 GetEventHandler()->ProcessEvent(nevent
);
2058 if (item
== NULL
) return; /* we hit the blank area */
2060 if (event
.RightDown()) {
2061 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
,GetId());
2064 nevent
.SetEventObject(this);
2065 GetEventHandler()->ProcessEvent(nevent
);
2069 if (event
.LeftUp() && (item
== m_current
) &&
2070 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2071 HasFlag(wxTR_EDIT_LABELS
) )
2073 m_renameTimer
->Start( 100, TRUE
);
2077 // how should the selection work for this event?
2078 bool is_multiple
, extended_select
, unselect_others
;
2079 EventFlagsToSelType(GetWindowStyleFlag(),
2081 event
.ControlDown(),
2082 &is_multiple
, &extended_select
, &unselect_others
);
2091 SelectItem(item
, unselect_others
, extended_select
);
2093 if (event
.LeftDClick())
2095 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2096 event
.m_item
= item
;
2098 event
.SetEventObject( this );
2099 GetEventHandler()->ProcessEvent( event
);
2103 void wxTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2105 /* after all changes have been done to the tree control,
2106 * we actually redraw the tree when everything is over */
2113 CalculatePositions();
2115 AdjustMyScrollbars();
2118 void wxTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2124 dc
.SetFont(m_boldFont
);
2126 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2129 // restore normal font
2130 dc
.SetFont( m_normalFont
);
2134 int image
= item
->GetCurrentImage();
2135 if ( image
!= NO_IMAGE
)
2137 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2141 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2144 total_h
+= 2; // at least 2 pixels
2146 total_h
+= total_h
/10; // otherwise 10% extra spacing
2148 item
->SetHeight(total_h
);
2149 if (total_h
>m_lineHeight
) m_lineHeight
=total_h
;
2151 item
->SetWidth(image_w
+text_w
+2);
2154 // -----------------------------------------------------------------------------
2155 // for developper : y is now the top of the level
2156 // not the middle of it !
2157 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2159 int horizX
= level
*m_indent
;
2161 CalculateSize( item
, dc
);
2164 item
->SetX( horizX
+m_indent
+m_spacing
);
2166 y
+=GetLineHeight(item
);
2168 if ( !item
->IsExpanded() )
2170 // we dont need to calculate collapsed branches
2174 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2175 size_t n
, count
= children
.Count();
2176 for (n
= 0; n
< count
; ++n
)
2177 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2180 void wxTreeCtrl::CalculatePositions()
2182 if ( !m_anchor
) return;
2184 wxClientDC
dc(this);
2187 dc
.SetFont( m_normalFont
);
2189 dc
.SetPen( m_dottedPen
);
2190 //if(GetImageList() == NULL)
2191 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2194 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2197 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2199 if (m_dirty
) return;
2201 wxClientDC
dc(this);
2206 GetClientSize( &cw
, &ch
);
2209 rect
.x
= dc
.LogicalToDeviceX( 0 );
2211 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2214 Refresh( TRUE
, &rect
);
2216 AdjustMyScrollbars();
2219 void wxTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2221 if (m_dirty
) return;
2223 wxClientDC
dc(this);
2228 GetClientSize( &cw
, &ch
);
2231 rect
.x
= dc
.LogicalToDeviceX( 0 );
2232 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2234 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2236 Refresh( TRUE
, &rect
);