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 "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/generic/treectlg.h"
34 #include "wx/imaglist.h"
35 #include "wx/settings.h"
38 #include "wx/dynarray.h"
39 #include "wx/arrimpl.cpp"
40 #include "wx/dcclient.h"
41 #include "wx/msgdlg.h"
43 // -----------------------------------------------------------------------------
45 // -----------------------------------------------------------------------------
47 class WXDLLEXPORT wxGenericTreeItem
;
49 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
50 //WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 static const int NO_IMAGE
= -1;
58 #define PIXELS_PER_UNIT 10
60 // -----------------------------------------------------------------------------
62 // -----------------------------------------------------------------------------
64 // timer used for enabling in-place edit
65 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
68 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
73 wxGenericTreeCtrl
*m_owner
;
76 // control used for in-place edit
77 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
81 wxTreeTextCtrl( wxWindow
*parent
,
85 wxGenericTreeCtrl
*owner
,
86 const wxString
&value
= wxEmptyString
,
87 const wxPoint
&pos
= wxDefaultPosition
,
88 const wxSize
&size
= wxDefaultSize
,
89 int style
= wxSIMPLE_BORDER
,
90 const wxValidator
& validator
= wxDefaultValidator
,
91 const wxString
&name
= wxTextCtrlNameStr
);
93 void OnChar( wxKeyEvent
&event
);
94 void OnKeyUp( wxKeyEvent
&event
);
95 void OnKillFocus( wxFocusEvent
&event
);
100 wxGenericTreeCtrl
*m_owner
;
101 wxString m_startValue
;
103 DECLARE_EVENT_TABLE()
104 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
108 class WXDLLEXPORT wxGenericTreeItem
112 wxGenericTreeItem() { m_data
= NULL
; }
113 wxGenericTreeItem( wxGenericTreeItem
*parent
,
114 const wxString
& text
,
116 int image
, int selImage
,
117 wxTreeItemData
*data
);
119 ~wxGenericTreeItem();
122 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
124 const wxString
& GetText() const { return m_text
; }
125 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
126 { return m_images
[which
]; }
127 wxTreeItemData
*GetData() const { return m_data
; }
129 // returns the current image for the item (depending on its
130 // selected/expanded/whatever state)
131 int GetCurrentImage() const;
133 void SetText( const wxString
&text
);
134 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
135 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
137 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
139 void SetBold(bool bold
) { m_isBold
= bold
; }
141 int GetX() const { return m_x
; }
142 int GetY() const { return m_y
; }
144 void SetX(int x
) { m_x
= x
; }
145 void SetY(int y
) { m_y
= y
; }
147 int GetHeight() const { return m_height
; }
148 int GetWidth() const { return m_width
; }
150 void SetHeight(int h
) { m_height
= h
; }
151 void SetWidth(int w
) { m_width
= w
; }
154 wxGenericTreeItem
*GetParent() const { return m_parent
; }
157 // deletes all children notifying the treectrl about it if !NULL
159 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
160 // FIXME don't know what is it for
163 // get count of all children (and grand children if 'recursively')
164 size_t GetChildrenCount(bool recursively
= TRUE
) const;
166 void Insert(wxGenericTreeItem
*child
, size_t index
)
167 { m_children
.Insert(child
, index
); }
169 void SetCross( int x
, int y
);
170 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
172 // return the item at given position (or NULL if no item), onButton is
173 // TRUE if the point belongs to the item's button, otherwise it lies
174 // on the button's label
175 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxGenericTreeCtrl
*, int &flags
);
177 void Expand() { m_isCollapsed
= FALSE
; }
178 void Collapse() { m_isCollapsed
= TRUE
; }
180 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
183 bool HasChildren() const { return !m_children
.IsEmpty(); }
184 bool IsSelected() const { return m_hasHilight
!= 0; }
185 bool IsExpanded() const { return !m_isCollapsed
; }
186 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
187 bool IsBold() const { return m_isBold
!= 0; }
190 // get them - may be NULL
191 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
192 // get them ensuring that the pointer is not NULL
193 wxTreeItemAttr
& Attr()
196 m_attr
= new wxTreeItemAttr
;
204 // tree ctrl images for the normal, selected, expanded and
205 // expanded+selected states
206 int m_images
[wxTreeItemIcon_Max
];
208 wxTreeItemData
*m_data
;
210 // use bitfields to save size
211 int m_isCollapsed
:1;
212 int m_hasHilight
:1; // same as focused
213 int m_hasPlus
:1; // used for item which doesn't have
214 // children but has a [+] button
215 int m_isBold
:1; // render the label in bold font
218 wxCoord m_height
, m_width
;
219 int m_xCross
, m_yCross
;
222 wxArrayGenericTreeItems m_children
;
223 wxGenericTreeItem
*m_parent
;
225 wxTreeItemAttr
*m_attr
;
228 // =============================================================================
230 // =============================================================================
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 // translate the key or mouse event flags to the type of selection we're
238 static void EventFlagsToSelType(long style
,
242 bool &extended_select
,
243 bool &unselect_others
)
245 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
246 extended_select
= shiftDown
&& is_multiple
;
247 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
250 // -----------------------------------------------------------------------------
251 // wxTreeRenameTimer (internal)
252 // -----------------------------------------------------------------------------
254 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
259 void wxTreeRenameTimer::Notify()
261 m_owner
->OnRenameTimer();
264 //-----------------------------------------------------------------------------
265 // wxTreeTextCtrl (internal)
266 //-----------------------------------------------------------------------------
268 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
270 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
271 EVT_CHAR (wxTreeTextCtrl::OnChar
)
272 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
273 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
276 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
280 wxGenericTreeCtrl
*owner
,
281 const wxString
&value
,
285 const wxValidator
& validator
,
286 const wxString
&name
)
287 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
293 (*m_res
) = wxEmptyString
;
294 m_startValue
= value
;
297 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
299 if (event
.m_keyCode
== WXK_RETURN
)
302 (*m_res
) = GetValue();
304 if (!wxPendingDelete
.Member(this))
305 wxPendingDelete
.Append(this);
307 if ((*m_accept
) && ((*m_res
) != m_startValue
))
308 m_owner
->OnRenameAccept();
312 if (event
.m_keyCode
== WXK_ESCAPE
)
317 if (!wxPendingDelete
.Member(this))
318 wxPendingDelete
.Append(this);
325 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
327 // auto-grow the textctrl:
328 wxSize parentSize
= m_owner
->GetSize();
329 wxPoint myPos
= GetPosition();
330 wxSize mySize
= GetSize();
332 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
333 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
334 if (mySize
.x
> sx
) sx
= mySize
.x
;
340 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
342 if (!wxPendingDelete
.Member(this))
343 wxPendingDelete
.Append(this);
345 if ((*m_accept
) && ((*m_res
) != m_startValue
))
346 m_owner
->OnRenameAccept();
350 // -----------------------------------------------------------------------------
352 // -----------------------------------------------------------------------------
354 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
356 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
357 : wxNotifyEvent( commandType
, id
)
360 m_itemOld
= (wxGenericTreeItem
*)NULL
;
364 // -----------------------------------------------------------------------------
366 // -----------------------------------------------------------------------------
368 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
369 const wxString
& text
,
371 int image
, int selImage
,
372 wxTreeItemData
*data
)
375 m_images
[wxTreeItemIcon_Normal
] = image
;
376 m_images
[wxTreeItemIcon_Selected
] = selImage
;
377 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
378 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
382 m_xCross
= m_yCross
= 0;
386 m_isCollapsed
= TRUE
;
387 m_hasHilight
= FALSE
;
393 m_attr
= (wxTreeItemAttr
*)NULL
;
395 // We don't know the height here yet.
400 wxGenericTreeItem::~wxGenericTreeItem()
406 wxASSERT_MSG( m_children
.IsEmpty(),
407 wxT("please call DeleteChildren() before deleting the item") );
410 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
412 size_t count
= m_children
.Count();
413 for ( size_t n
= 0; n
< count
; n
++ )
415 wxGenericTreeItem
*child
= m_children
[n
];
417 tree
->SendDeleteEvent(child
);
419 child
->DeleteChildren(tree
);
426 void wxGenericTreeItem::SetText( const wxString
&text
)
431 void wxGenericTreeItem::Reset()
434 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
436 m_images
[i
] = NO_IMAGE
;
441 m_height
= m_width
= 0;
448 m_isCollapsed
= TRUE
;
450 m_parent
= (wxGenericTreeItem
*)NULL
;
453 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
455 size_t count
= m_children
.Count();
459 size_t total
= count
;
460 for (size_t n
= 0; n
< count
; ++n
)
462 total
+= m_children
[n
]->GetChildrenCount();
468 void wxGenericTreeItem::SetCross( int x
, int y
)
474 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxGenericTreeCtrl
*theTree
)
476 int bottomY
=m_y
+theTree
->GetLineHeight(this);
477 if ( y
< bottomY
) y
= bottomY
;
478 int width
= m_x
+ m_width
;
479 if ( x
< width
) x
= width
;
483 size_t count
= m_children
.Count();
484 for ( size_t n
= 0; n
< count
; ++n
)
486 m_children
[n
]->GetSize( x
, y
, theTree
);
491 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
492 const wxGenericTreeCtrl
*theTree
,
495 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
497 if (point
.y
< m_y
+theTree
->GetLineHeight(this)/2 )
498 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
500 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
502 // 5 is the size of the plus sign
503 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
504 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
505 (IsExpanded() || HasPlus()))
507 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
511 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
516 // assuming every image (normal and selected ) has the same size !
517 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
518 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
520 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
521 flags
|= wxTREE_HITTEST_ONITEMICON
;
523 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
529 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
530 if (point
.x
> m_x
+m_width
)
531 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
539 size_t count
= m_children
.Count();
540 for ( size_t n
= 0; n
< count
; n
++ )
542 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
549 flags
|=wxTREE_HITTEST_NOWHERE
;
551 return (wxGenericTreeItem
*) NULL
;
554 int wxGenericTreeItem::GetCurrentImage() const
556 int image
= NO_IMAGE
;
561 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
564 if ( image
== NO_IMAGE
)
566 // we usually fall back to the normal item, but try just the
567 // expanded one (and not selected) first in this case
568 image
= GetImage(wxTreeItemIcon_Expanded
);
574 image
= GetImage(wxTreeItemIcon_Selected
);
577 // may be it doesn't have the specific image we want, try the default one
579 if ( image
== NO_IMAGE
)
587 // -----------------------------------------------------------------------------
588 // wxGenericTreeCtrl implementation
589 // -----------------------------------------------------------------------------
591 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
593 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
594 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
595 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
596 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
597 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
598 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
599 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
602 #if !defined(__WXMSW__) || defined(__WIN16__)
604 * wxTreeCtrl has to be a real class or we have problems with
605 * the run-time information.
608 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
611 // -----------------------------------------------------------------------------
612 // construction/destruction
613 // -----------------------------------------------------------------------------
615 void wxGenericTreeCtrl::Init()
619 m_anchor
= (wxGenericTreeItem
*) NULL
;
629 m_hilightBrush
= new wxBrush
631 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
636 m_imageListState
= (wxImageList
*) NULL
;
637 m_ownsImageListNormal
=
638 m_ownsImageListState
= FALSE
;
641 m_isDragging
= FALSE
;
643 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
645 m_renameTimer
= new wxTreeRenameTimer( this );
646 m_lastOnSame
= FALSE
;
648 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
649 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
650 m_normalFont
.GetFamily(),
651 m_normalFont
.GetStyle(),
653 m_normalFont
.GetUnderlined());
656 bool wxGenericTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
657 const wxPoint
& pos
, const wxSize
& size
,
659 const wxValidator
&validator
,
660 const wxString
& name
)
662 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
665 SetValidator( validator
);
668 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
670 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
671 m_dottedPen
= wxPen( "grey", 0, 0 );
676 wxGenericTreeCtrl::~wxGenericTreeCtrl()
678 wxDELETE( m_hilightBrush
);
682 delete m_renameTimer
;
683 if (m_ownsImageListNormal
) delete m_imageListNormal
;
684 if (m_ownsImageListState
) delete m_imageListState
;
687 // -----------------------------------------------------------------------------
689 // -----------------------------------------------------------------------------
691 size_t wxGenericTreeCtrl::GetCount() const
693 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
696 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
702 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
708 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
710 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
712 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
715 // -----------------------------------------------------------------------------
716 // functions to work with tree items
717 // -----------------------------------------------------------------------------
719 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
721 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
723 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
726 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
727 wxTreeItemIcon which
) const
729 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
731 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
734 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
736 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
738 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
741 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
743 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
746 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
747 pItem
->SetText(text
);
748 CalculateSize(pItem
, dc
);
752 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
754 wxTreeItemIcon which
)
756 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
758 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
759 pItem
->SetImage(image
, which
);
762 CalculateSize(pItem
, dc
);
766 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
768 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
770 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
773 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
775 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
777 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
778 pItem
->SetHasPlus(has
);
782 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
784 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
786 // avoid redrawing the tree if no real change
787 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
788 if ( pItem
->IsBold() != bold
)
790 pItem
->SetBold(bold
);
795 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
798 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
800 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
801 pItem
->Attr().SetTextColour(col
);
805 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
808 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
810 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
811 pItem
->Attr().SetBackgroundColour(col
);
815 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
817 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
819 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
820 pItem
->Attr().SetFont(font
);
824 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
826 wxScrolledWindow::SetFont(font
);
828 m_normalFont
= font
;
829 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
830 m_normalFont
.GetFamily(),
831 m_normalFont
.GetStyle(),
833 m_normalFont
.GetUnderlined());
839 // -----------------------------------------------------------------------------
840 // item status inquiries
841 // -----------------------------------------------------------------------------
843 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
845 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
847 // An item is only visible if it's not a descendant of a collapsed item
848 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
849 wxGenericTreeItem
* parent
= pItem
->GetParent();
852 if (!parent
->IsExpanded())
854 parent
= parent
->GetParent();
858 GetViewStart(& startX
, & startY
);
860 wxSize clientSize
= GetClientSize();
863 if (!GetBoundingRect(item
, rect
))
865 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
867 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
869 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
875 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
877 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
879 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
882 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
884 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
886 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
889 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
891 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
893 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
896 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
898 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
900 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
903 // -----------------------------------------------------------------------------
905 // -----------------------------------------------------------------------------
907 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
909 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
911 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
914 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
916 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
919 return GetNextChild(item
, cookie
);
922 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
924 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
926 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
927 if ( (size_t)cookie
< children
.Count() )
929 return children
.Item((size_t)cookie
++);
933 // there are no more of them
934 return wxTreeItemId();
938 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
940 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
942 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
943 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
946 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
948 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
950 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
951 wxGenericTreeItem
*parent
= i
->GetParent();
952 if ( parent
== NULL
)
954 // root item doesn't have any siblings
955 return wxTreeItemId();
958 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
959 int index
= siblings
.Index(i
);
960 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
962 size_t n
= (size_t)(index
+ 1);
963 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
966 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
968 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
970 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
971 wxGenericTreeItem
*parent
= i
->GetParent();
972 if ( parent
== NULL
)
974 // root item doesn't have any siblings
975 return wxTreeItemId();
978 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
979 int index
= siblings
.Index(i
);
980 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
982 return index
== 0 ? wxTreeItemId()
983 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
986 // Only for internal use right now, but should probably be public
987 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
989 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
991 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
993 // First see if there are any children.
994 wxArrayGenericTreeItems
& children
= i
->GetChildren();
995 if (children
.GetCount() > 0)
997 return children
.Item(0);
1001 // Try a sibling of this or ancestor instead
1002 wxTreeItemId p
= item
;
1003 wxTreeItemId toFind
;
1006 toFind
= GetNextSibling(p
);
1008 } while (p
.IsOk() && !toFind
.IsOk());
1013 wxTreeItemId
wxGenericTreeCtrl::GetPrev(const wxTreeItemId
& item
) const
1015 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1017 wxFAIL_MSG(wxT("not implemented"));
1019 return wxTreeItemId();
1022 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1024 wxTreeItemId id
= GetRootItem();
1033 } while (id
.IsOk());
1035 return wxTreeItemId();
1038 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1040 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1042 wxTreeItemId id
= item
;
1047 if (id
.IsOk() && IsVisible(id
))
1050 return wxTreeItemId();
1053 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1055 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1057 wxFAIL_MSG(wxT("not implemented"));
1059 return wxTreeItemId();
1062 // -----------------------------------------------------------------------------
1064 // -----------------------------------------------------------------------------
1066 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1068 const wxString
& text
,
1069 int image
, int selImage
,
1070 wxTreeItemData
*data
)
1072 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1075 // should we give a warning here?
1076 return AddRoot(text
, image
, selImage
, data
);
1079 wxClientDC
dc(this);
1080 wxGenericTreeItem
*item
=
1081 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1085 data
->m_pItem
= (long) item
;
1088 parent
->Insert( item
, previous
);
1095 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1096 int image
, int selImage
,
1097 wxTreeItemData
*data
)
1099 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1101 wxClientDC
dc(this);
1102 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1103 image
, selImage
, data
);
1106 data
->m_pItem
= (long) m_anchor
;
1109 if (!HasFlag(wxTR_MULTIPLE
))
1111 m_current
= m_key_current
= m_anchor
;
1112 m_current
->SetHilight( TRUE
);
1120 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1121 const wxString
& text
,
1122 int image
, int selImage
,
1123 wxTreeItemData
*data
)
1125 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1128 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1129 const wxTreeItemId
& idPrevious
,
1130 const wxString
& text
,
1131 int image
, int selImage
,
1132 wxTreeItemData
*data
)
1134 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1137 // should we give a warning here?
1138 return AddRoot(text
, image
, selImage
, data
);
1141 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1142 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1143 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1145 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1148 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1150 const wxString
& text
,
1151 int image
, int selImage
,
1152 wxTreeItemData
*data
)
1154 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1157 // should we give a warning here?
1158 return AddRoot(text
, image
, selImage
, data
);
1161 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1164 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1165 const wxString
& text
,
1166 int image
, int selImage
,
1167 wxTreeItemData
*data
)
1169 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1172 // should we give a warning here?
1173 return AddRoot(text
, image
, selImage
, data
);
1176 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1177 image
, selImage
, data
);
1180 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1182 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1183 event
.m_item
= (long) item
;
1184 event
.SetEventObject( this );
1185 ProcessEvent( event
);
1188 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1190 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1191 item
->DeleteChildren(this);
1196 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1198 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1200 // don't stay with invalid m_key_current or we will crash in the next call
1202 bool changeKeyCurrent
= FALSE
;
1203 wxGenericTreeItem
*itemKey
= m_key_current
;
1204 while ( itemKey
&& !changeKeyCurrent
)
1206 if ( itemKey
== item
)
1208 // m_key_current is a descendant of the item being deleted
1209 changeKeyCurrent
= TRUE
;
1213 itemKey
= itemKey
->GetParent();
1217 wxGenericTreeItem
*parent
= item
->GetParent();
1220 parent
->GetChildren().Remove( item
); // remove by value
1223 if ( changeKeyCurrent
)
1225 // may be NULL or not
1226 m_key_current
= parent
;
1229 item
->DeleteChildren(this);
1230 SendDeleteEvent(item
);
1236 void wxGenericTreeCtrl::DeleteAllItems()
1240 m_anchor
->DeleteChildren(this);
1249 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1251 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1253 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1255 if ( !item
->HasPlus() )
1258 if ( item
->IsExpanded() )
1261 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1262 event
.m_item
= (long) item
;
1263 event
.SetEventObject( this );
1265 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1267 // cancelled by program
1272 CalculatePositions();
1274 RefreshSubtree(item
);
1276 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1277 ProcessEvent( event
);
1280 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1283 if ( IsExpanded(item
) )
1286 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1287 while ( child
.IsOk() )
1291 child
= GetNextChild(item
, cookie
);
1296 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1298 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1300 if ( !item
->IsExpanded() )
1303 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1304 event
.m_item
= (long) item
;
1305 event
.SetEventObject( this );
1306 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1308 // cancelled by program
1314 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1315 size_t count
= children
.Count();
1316 for ( size_t n
= 0; n
< count
; n
++ )
1318 Collapse(children
[n
]);
1321 CalculatePositions();
1323 RefreshSubtree(item
);
1325 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1326 ProcessEvent( event
);
1329 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1332 DeleteChildren(item
);
1335 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1337 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1339 if (item
->IsExpanded())
1345 void wxGenericTreeCtrl::Unselect()
1349 m_current
->SetHilight( FALSE
);
1350 RefreshLine( m_current
);
1354 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1356 if (item
->IsSelected())
1358 item
->SetHilight(FALSE
);
1362 if (item
->HasChildren())
1364 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1365 size_t count
= children
.Count();
1366 for ( size_t n
= 0; n
< count
; ++n
)
1368 UnselectAllChildren(children
[n
]);
1373 void wxGenericTreeCtrl::UnselectAll()
1375 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1378 // Recursive function !
1379 // To stop we must have crt_item<last_item
1381 // Tag all next children, when no more children,
1382 // Move to parent (not to tag)
1383 // Keep going... if we found last_item, we stop.
1384 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1386 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1388 if (parent
== NULL
) // This is root item
1389 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1391 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1392 int index
= children
.Index(crt_item
);
1393 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1395 size_t count
= children
.Count();
1396 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1398 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1401 return TagNextChildren(parent
, last_item
, select
);
1404 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1406 crt_item
->SetHilight(select
);
1407 RefreshLine(crt_item
);
1409 if (crt_item
==last_item
)
1412 if (crt_item
->HasChildren())
1414 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1415 size_t count
= children
.Count();
1416 for ( size_t n
= 0; n
< count
; ++n
)
1418 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1426 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1428 // item2 is not necessary after item1
1429 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1431 // choice first' and 'last' between item1 and item2
1432 if (item1
->GetY()<item2
->GetY())
1443 bool select
= m_current
->IsSelected();
1445 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1448 TagNextChildren(first
,last
,select
);
1451 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1452 bool unselect_others
,
1453 bool extended_select
)
1455 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1457 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1458 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1460 //wxCHECK_RET( ( (!unselect_others) && is_single),
1461 // wxT("this is a single selection tree") );
1463 // to keep going anyhow !!!
1466 if (item
->IsSelected())
1467 return; // nothing to do
1468 unselect_others
= TRUE
;
1469 extended_select
= FALSE
;
1471 else if ( unselect_others
&& item
->IsSelected() )
1473 // selection change if there is more than one item currently selected
1474 wxArrayTreeItemIds selected_items
;
1475 if ( GetSelections(selected_items
) == 1 )
1479 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1480 event
.m_item
= (long) item
;
1481 event
.m_itemOld
= (long) m_current
;
1482 event
.SetEventObject( this );
1483 // TODO : Here we don't send any selection mode yet !
1485 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1488 wxTreeItemId parent
= GetParent( itemId
);
1489 while (parent
.IsOk())
1491 if (!IsExpanded(parent
))
1494 parent
= GetParent( parent
);
1497 EnsureVisible( itemId
);
1500 if (unselect_others
)
1502 if (is_single
) Unselect(); // to speed up thing
1507 if (extended_select
)
1512 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1515 // don't change the mark (m_current)
1516 SelectItemRange(m_current
, item
);
1520 bool select
=TRUE
; // the default
1522 // Check if we need to toggle hilight (ctrl mode)
1523 if (!unselect_others
)
1524 select
=!item
->IsSelected();
1526 m_current
= m_key_current
= item
;
1527 m_current
->SetHilight(select
);
1528 RefreshLine( m_current
);
1531 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1532 GetEventHandler()->ProcessEvent( event
);
1535 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1536 wxArrayTreeItemIds
&array
) const
1538 if ( item
->IsSelected() )
1539 array
.Add(wxTreeItemId(item
));
1541 if ( item
->HasChildren() )
1543 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1544 size_t count
= children
.GetCount();
1545 for ( size_t n
= 0; n
< count
; ++n
)
1546 FillArray(children
[n
], array
);
1550 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1553 wxTreeItemId idRoot
= GetRootItem();
1554 if ( idRoot
.IsOk() )
1556 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1558 //else: the tree is empty, so no selections
1560 return array
.Count();
1563 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1565 if (!item
.IsOk()) return;
1567 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1569 // first expand all parent branches
1570 wxGenericTreeItem
*parent
= gitem
->GetParent();
1574 parent
= parent
->GetParent();
1577 //if (parent) CalculatePositions();
1582 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1584 if (!item
.IsOk()) return;
1586 // We have to call this here because the label in
1587 // question might just have been added and no screen
1588 // update taken place.
1589 if (m_dirty
) wxYieldIfNeeded();
1591 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1593 // now scroll to the item
1594 int item_y
= gitem
->GetY();
1598 GetViewStart( &start_x
, &start_y
);
1599 start_y
*= PIXELS_PER_UNIT
;
1603 GetClientSize( &client_w
, &client_h
);
1605 if (item_y
< start_y
+3)
1610 m_anchor
->GetSize( x
, y
, this );
1611 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1612 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1613 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1614 // Item should appear at top
1615 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1617 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1622 m_anchor
->GetSize( x
, y
, this );
1623 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1624 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1625 item_y
+= PIXELS_PER_UNIT
+2;
1626 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1627 // Item should appear at bottom
1628 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
);
1632 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1633 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1635 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1636 wxGenericTreeItem
**item2
)
1638 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1640 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1643 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1644 const wxTreeItemId
& item2
)
1646 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1649 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1651 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1653 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1655 wxCHECK_RET( !s_treeBeingSorted
,
1656 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1658 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1659 if ( children
.Count() > 1 )
1661 s_treeBeingSorted
= this;
1662 children
.Sort(tree_ctrl_compare_func
);
1663 s_treeBeingSorted
= NULL
;
1667 //else: don't make the tree dirty as nothing changed
1670 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1672 return m_imageListNormal
;
1675 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1677 return m_imageListState
;
1680 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1682 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1684 m_imageListNormal
= imageList
;
1685 m_ownsImageListNormal
= FALSE
;
1687 if ( !m_imageListNormal
)
1690 // Calculate a m_lineHeight value from the image sizes.
1691 // May be toggle off. Then wxGenericTreeCtrl will spread when
1692 // necessary (which might look ugly).
1693 wxClientDC
dc(this);
1694 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1695 int width
= 0, height
= 0,
1696 n
= m_imageListNormal
->GetImageCount();
1698 for (int i
= 0; i
< n
; i
++)
1700 m_imageListNormal
->GetSize(i
, width
, height
);
1701 if (height
> m_lineHeight
) m_lineHeight
= height
;
1704 if (m_lineHeight
< 40)
1705 m_lineHeight
+= 2; // at least 2 pixels
1707 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1710 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1712 if (m_ownsImageListState
) delete m_imageListState
;
1713 m_imageListState
= imageList
;
1714 m_ownsImageListState
= FALSE
;
1717 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1719 SetImageList(imageList
);
1720 m_ownsImageListNormal
= TRUE
;
1723 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1725 SetStateImageList(imageList
);
1726 m_ownsImageListState
= TRUE
;
1729 // -----------------------------------------------------------------------------
1731 // -----------------------------------------------------------------------------
1733 void wxGenericTreeCtrl::AdjustMyScrollbars()
1739 m_anchor
->GetSize( x
, y
, this );
1740 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1741 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1742 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1743 int y_pos
= GetScrollPos( wxVERTICAL
);
1744 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1748 SetScrollbars( 0, 0, 0, 0 );
1752 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1754 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1755 return item
->GetHeight();
1757 return m_lineHeight
;
1760 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1762 wxTreeItemAttr
*attr
= item
->GetAttributes();
1763 if ( attr
&& attr
->HasFont() )
1764 dc
.SetFont(attr
->GetFont());
1765 else if (item
->IsBold())
1766 dc
.SetFont(m_boldFont
);
1770 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1774 int image
= item
->GetCurrentImage();
1775 if ( image
!= NO_IMAGE
)
1777 if ( m_imageListNormal
)
1779 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1788 int total_h
= GetLineHeight(item
);
1790 bool paintBg
= item
->IsSelected() && m_hasFocus
;
1793 dc
.SetBrush(*m_hilightBrush
);
1798 if ( attr
&& attr
->HasBackgroundColour() )
1799 colBg
= attr
->GetBackgroundColour();
1801 colBg
= m_backgroundColour
;
1802 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1805 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1807 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1809 // If it's selected, and there's an image, then we should
1810 // take care to leave the area under the image painted in the
1811 // background colour.
1812 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1813 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1817 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1818 item
->GetWidth()+2, total_h
-offset
);
1821 if ( image
!= NO_IMAGE
)
1823 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1824 m_imageListNormal
->Draw( image
, dc
,
1826 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1827 wxIMAGELIST_DRAW_TRANSPARENT
);
1828 dc
.DestroyClippingRegion();
1831 dc
.SetBackgroundMode(wxTRANSPARENT
);
1832 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1833 dc
.DrawText( item
->GetText(),
1834 (wxCoord
)(image_w
+ item
->GetX()),
1835 (wxCoord
)(item
->GetY() + extraH
));
1837 // restore normal font
1838 dc
.SetFont( m_normalFont
);
1841 // Now y stands for the top of the item, whereas it used to stand for middle !
1842 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1844 int x
= (level
+1)*m_indent
;
1846 item
->SetX( x
+m_spacing
);
1850 y
+=GetLineHeight(item
)/2;
1852 item
->SetCross( x
, y
);
1854 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1855 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1857 bool drawLines
= (!HasFlag(wxTR_NO_LINES
) && !HasFlag(wxTR_MAC_BUTTONS
));
1859 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1861 int startX
= x
- m_indent
;
1864 if (!item
->HasChildren()) endX
+= 20;
1866 if (HasFlag( wxTR_MAC_BUTTONS
))
1868 if (item
->HasPlus())
1870 dc
.SetPen( *wxBLACK_PEN
);
1871 dc
.SetBrush( *m_hilightBrush
);
1875 if (item
->IsExpanded())
1893 dc
.DrawPolygon( 3, button
);
1895 dc
.SetPen( m_dottedPen
);
1901 dc
.DrawLine( startX
, y
, endX
, y
);
1903 if (item
->HasPlus())
1906 dc
.DrawLine( x
+5, y
, x
+15, y
);
1907 dc
.SetPen( *wxGREY_PEN
);
1908 dc
.SetBrush( *wxWHITE_BRUSH
);
1909 dc
.DrawRectangle( x
-5, y
-4, 11, 9 );
1911 dc
.SetPen( *wxBLACK_PEN
);
1912 dc
.DrawLine( x
-2, y
, x
+3, y
);
1913 if (!item
->IsExpanded())
1914 dc
.DrawLine( x
, y
-2, x
, y
+3 );
1915 dc
.SetPen( m_dottedPen
);
1921 // don't draw rect outline if we already have the background color
1923 if ( item
->IsSelected() )
1926 #endif // !__WXMAC__
1927 pen
= wxTRANSPARENT_PEN
;
1930 if ( item
->IsSelected() && m_hasFocus
)
1932 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1936 wxTreeItemAttr
*attr
= item
->GetAttributes();
1937 if ( attr
&& attr
->HasTextColour() )
1938 colText
= attr
->GetTextColour();
1940 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
);
1944 dc
.SetTextForeground(colText
);
1948 PaintItem(item
, dc
);
1950 if (HasFlag( wxTR_ROW_LINES
))
1952 dc
.SetPen( *wxWHITE_PEN
);
1953 dc
.DrawLine( 0, oldY
, 10000, oldY
);
1954 dc
.DrawLine( 0, oldY
+ GetLineHeight(item
), 10000, oldY
+ GetLineHeight(item
) );
1957 // restore DC objects
1958 dc
.SetBrush( *wxWHITE_BRUSH
);
1959 dc
.SetPen( m_dottedPen
);
1960 dc
.SetTextForeground( *wxBLACK
);
1963 y
= oldY
+GetLineHeight(item
);
1965 if (item
->IsExpanded())
1967 oldY
+=GetLineHeight(item
)/2;
1970 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1971 size_t n
, count
= children
.Count();
1972 for ( n
= 0; n
< count
; ++n
)
1975 PaintLevel( children
[n
], dc
, level
+1, y
);
1978 // it may happen that the item is expanded but has no items (when you
1979 // delete all its children for example) - don't draw the vertical line
1983 semiOldY
+=GetLineHeight(children
[--n
])/2;
1985 dc
.DrawLine( x
, oldY
+5, x
, semiOldY
);
1990 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1994 if ( item
->HasPlus() )
1996 // it's a folder, indicate it by a border
2001 // draw a line under the drop target because the item will be
2003 DrawLine(item
, TRUE
/* below */);
2006 SetCursor(wxCURSOR_BULLSEYE
);
2011 SetCursor(wxCURSOR_NO_ENTRY
);
2015 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2017 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2019 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2021 wxClientDC
dc(this);
2023 dc
.SetLogicalFunction(wxINVERT
);
2024 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2026 int w
= i
->GetWidth() + 2;
2027 int h
= GetLineHeight(i
) + 2;
2029 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2032 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2034 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2036 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2038 wxClientDC
dc(this);
2040 dc
.SetLogicalFunction(wxINVERT
);
2046 y
+= GetLineHeight(i
) - 1;
2049 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2052 // -----------------------------------------------------------------------------
2053 // wxWindows callbacks
2054 // -----------------------------------------------------------------------------
2056 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2064 dc
.SetFont( m_normalFont
);
2065 dc
.SetPen( m_dottedPen
);
2067 // this is now done dynamically
2068 //if(GetImageList() == NULL)
2069 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2072 PaintLevel( m_anchor
, dc
, 0, y
);
2075 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2080 RefreshLine( m_current
);
2083 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2088 RefreshLine( m_current
);
2091 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2093 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2094 te
.m_code
= (int)event
.KeyCode();
2095 te
.SetEventObject( this );
2096 GetEventHandler()->ProcessEvent( te
);
2098 if ( (m_current
== 0) || (m_key_current
== 0) )
2104 // how should the selection work for this event?
2105 bool is_multiple
, extended_select
, unselect_others
;
2106 EventFlagsToSelType(GetWindowStyleFlag(),
2108 event
.ControlDown(),
2109 is_multiple
, extended_select
, unselect_others
);
2113 // * : Expand all/Collapse all
2114 // ' ' | return : activate
2115 // up : go up (not last children!)
2117 // left : go to parent
2118 // right : open if parent and go next
2119 // home : go to root
2120 // end : go to last item without opening parents
2121 switch (event
.KeyCode())
2125 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2133 if ( !IsExpanded(m_current
) )
2136 ExpandAll(m_current
);
2139 //else: fall through to Collapse() it
2143 if (IsExpanded(m_current
))
2145 Collapse(m_current
);
2152 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2153 event
.m_item
= (long) m_current
;
2155 event
.SetEventObject( this );
2156 GetEventHandler()->ProcessEvent( event
);
2160 // up goes to the previous sibling or to the last of its children if
2164 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2167 prev
= GetParent( m_key_current
);
2171 wxTreeItemId current
= m_key_current
;
2172 if (current
== GetFirstChild( prev
, cockie
))
2174 // otherwise we return to where we came from
2175 SelectItem( prev
, unselect_others
, extended_select
);
2176 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2177 EnsureVisible( prev
);
2184 while ( IsExpanded(prev
) && HasChildren(prev
) )
2186 wxTreeItemId child
= GetLastChild(prev
);
2193 SelectItem( prev
, unselect_others
, extended_select
);
2194 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2195 EnsureVisible( prev
);
2200 // left arrow goes to the parent
2203 wxTreeItemId prev
= GetParent( m_current
);
2206 EnsureVisible( prev
);
2207 SelectItem( prev
, unselect_others
, extended_select
);
2213 // this works the same as the down arrow except that we also expand the
2214 // item if it wasn't expanded yet
2220 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2223 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2224 SelectItem( child
, unselect_others
, extended_select
);
2225 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2226 EnsureVisible( child
);
2230 wxTreeItemId next
= GetNextSibling( m_key_current
);
2233 wxTreeItemId current
= m_key_current
;
2234 while (current
&& !next
)
2236 current
= GetParent( current
);
2237 if (current
) next
= GetNextSibling( current
);
2242 SelectItem( next
, unselect_others
, extended_select
);
2243 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2244 EnsureVisible( next
);
2250 // <End> selects the last visible tree item
2253 wxTreeItemId last
= GetRootItem();
2255 while ( last
.IsOk() && IsExpanded(last
) )
2257 wxTreeItemId lastChild
= GetLastChild(last
);
2259 // it may happen if the item was expanded but then all of
2260 // its children have been deleted - so IsExpanded() returned
2261 // TRUE, but GetLastChild() returned invalid item
2270 EnsureVisible( last
);
2271 SelectItem( last
, unselect_others
, extended_select
);
2276 // <Home> selects the root item
2279 wxTreeItemId prev
= GetRootItem();
2282 EnsureVisible( prev
);
2283 SelectItem( prev
, unselect_others
, extended_select
);
2293 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2295 // We have to call this here because the label in
2296 // question might just have been added and no screen
2297 // update taken place.
2298 // JACS: removed this because the yield can cause the window to be
2299 // deleted from under us if a close window event is pending
2300 // if (m_dirty) wxYieldIfNeeded();
2302 wxClientDC
dc(this);
2304 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2305 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2310 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2311 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2312 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2313 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2316 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2318 return wxTreeItemId();
2321 // get the bounding rectangle of the item (or of its label only)
2322 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2324 bool WXUNUSED(textOnly
)) const
2326 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2328 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2331 GetViewStart(& startX
, & startY
);
2333 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2334 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2335 rect
.width
= i
->GetWidth();
2336 //rect.height = i->GetHeight();
2337 rect
.height
= GetLineHeight(i
);
2344 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2346 if (!item
.IsOk()) return;
2348 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2350 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2351 te
.m_item
= (long) m_currentEdit
;
2352 te
.SetEventObject( this );
2353 GetEventHandler()->ProcessEvent( te
);
2355 if (!te
.IsAllowed()) return;
2357 // We have to call this here because the label in
2358 // question might just have been added and no screen
2359 // update taken place.
2360 if (m_dirty
) wxYieldIfNeeded();
2362 wxString s
= m_currentEdit
->GetText();
2363 int x
= m_currentEdit
->GetX();
2364 int y
= m_currentEdit
->GetY();
2365 int w
= m_currentEdit
->GetWidth();
2366 int h
= m_currentEdit
->GetHeight();
2371 int image
= m_currentEdit
->GetCurrentImage();
2372 if ( image
!= NO_IMAGE
)
2374 if ( m_imageListNormal
)
2376 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2381 wxFAIL_MSG(_T("you must create an image list to use images!"));
2385 w
-= image_w
+ 4; // I don't know why +4 is needed
2387 wxClientDC
dc(this);
2389 x
= dc
.LogicalToDeviceX( x
);
2390 y
= dc
.LogicalToDeviceY( y
);
2392 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2402 void wxGenericTreeCtrl::OnRenameTimer()
2407 void wxGenericTreeCtrl::OnRenameAccept()
2409 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2410 le
.m_item
= (long) m_currentEdit
;
2411 le
.SetEventObject( this );
2412 le
.m_label
= m_renameRes
;
2413 GetEventHandler()->ProcessEvent( le
);
2415 if (!le
.IsAllowed()) return;
2417 SetItemText( m_currentEdit
, m_renameRes
);
2420 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2422 if ( !m_anchor
) return;
2424 // we process left mouse up event (enables in-place edit), right down
2425 // (pass to the user code), left dbl click (activate item) and
2426 // dragging/moving events for items drag-and-drop
2427 if ( !(event
.LeftDown() ||
2429 event
.RightDown() ||
2430 event
.LeftDClick() ||
2432 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2439 wxClientDC
dc(this);
2441 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2442 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2445 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2447 if ( event
.Dragging() && !m_isDragging
)
2449 if (m_dragCount
== 0)
2450 m_dragStart
= wxPoint(x
,y
);
2454 if (m_dragCount
!= 3)
2456 // wait until user drags a bit further...
2460 wxEventType command
= event
.RightIsDown()
2461 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2462 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2464 wxTreeEvent
nevent( command
, GetId() );
2465 nevent
.m_item
= (long) m_current
;
2466 nevent
.SetEventObject(this);
2468 // by default the dragging is not supported, the user code must
2469 // explicitly allow the event for it to take place
2472 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2474 // we're going to drag this item
2475 m_isDragging
= TRUE
;
2477 // remember the old cursor because we will change it while
2479 m_oldCursor
= m_cursor
;
2481 // in a single selection control, hide the selection temporarily
2482 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2484 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2486 if ( m_oldSelection
)
2488 m_oldSelection
->SetHilight(FALSE
);
2489 RefreshLine(m_oldSelection
);
2496 else if ( event
.Moving() )
2498 if ( item
!= m_dropTarget
)
2500 // unhighlight the previous drop target
2501 DrawDropEffect(m_dropTarget
);
2503 m_dropTarget
= item
;
2505 // highlight the current drop target if any
2506 DrawDropEffect(m_dropTarget
);
2511 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2513 // erase the highlighting
2514 DrawDropEffect(m_dropTarget
);
2516 if ( m_oldSelection
)
2518 m_oldSelection
->SetHilight(TRUE
);
2519 RefreshLine(m_oldSelection
);
2520 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2523 // generate the drag end event
2524 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2526 event
.m_item
= (long) item
;
2527 event
.m_pointDrag
= wxPoint(x
, y
);
2528 event
.SetEventObject(this);
2530 (void)GetEventHandler()->ProcessEvent(event
);
2532 m_isDragging
= FALSE
;
2533 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2537 SetCursor(m_oldCursor
);
2543 // here we process only the messages which happen on tree items
2547 if (item
== NULL
) return; /* we hit the blank area */
2549 if ( event
.RightDown() )
2551 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2552 nevent
.m_item
= (long) item
;
2554 CalcScrolledPosition(x
, y
,
2555 &nevent
.m_pointDrag
.x
,
2556 &nevent
.m_pointDrag
.y
);
2557 nevent
.SetEventObject(this);
2558 GetEventHandler()->ProcessEvent(nevent
);
2560 else if ( event
.LeftUp() )
2564 if ( (item
== m_current
) &&
2565 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2566 HasFlag(wxTR_EDIT_LABELS
) )
2568 if ( m_renameTimer
->IsRunning() )
2569 m_renameTimer
->Stop();
2571 m_renameTimer
->Start( 100, TRUE
);
2574 m_lastOnSame
= FALSE
;
2577 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2579 if ( event
.LeftDown() )
2581 m_lastOnSame
= item
== m_current
;
2584 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2586 // only toggle the item for a single click, double click on
2587 // the button doesn't do anything (it toggles the item twice)
2588 if ( event
.LeftDown() )
2593 // don't select the item if the button was clicked
2597 // how should the selection work for this event?
2598 bool is_multiple
, extended_select
, unselect_others
;
2599 EventFlagsToSelType(GetWindowStyleFlag(),
2601 event
.ControlDown(),
2602 is_multiple
, extended_select
, unselect_others
);
2604 SelectItem(item
, unselect_others
, extended_select
);
2606 if ( event
.LeftDClick() )
2608 // double clicking should not start editing the item label
2609 m_renameTimer
->Stop();
2610 m_lastOnSame
= FALSE
;
2612 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2613 nevent
.m_item
= (long) item
;
2615 CalcScrolledPosition(x
, y
,
2616 &nevent
.m_pointDrag
.x
,
2617 &nevent
.m_pointDrag
.y
);
2618 nevent
.SetEventObject( this );
2619 GetEventHandler()->ProcessEvent( nevent
);
2625 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2627 /* after all changes have been done to the tree control,
2628 * we actually redraw the tree when everything is over */
2635 CalculatePositions();
2637 AdjustMyScrollbars();
2640 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2646 dc
.SetFont(m_boldFont
);
2648 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2651 // restore normal font
2652 dc
.SetFont( m_normalFont
);
2656 int image
= item
->GetCurrentImage();
2657 if ( image
!= NO_IMAGE
)
2659 if ( m_imageListNormal
)
2661 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2666 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2669 total_h
+= 2; // at least 2 pixels
2671 total_h
+= total_h
/10; // otherwise 10% extra spacing
2673 item
->SetHeight(total_h
);
2674 if (total_h
>m_lineHeight
)
2675 m_lineHeight
=total_h
;
2677 item
->SetWidth(image_w
+text_w
+2);
2680 // -----------------------------------------------------------------------------
2681 // for developper : y is now the top of the level
2682 // not the middle of it !
2683 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2685 int horizX
= level
*m_indent
;
2687 CalculateSize( item
, dc
);
2690 item
->SetX( horizX
+m_indent
+m_spacing
);
2692 y
+=GetLineHeight(item
);
2694 if ( !item
->IsExpanded() )
2696 // we dont need to calculate collapsed branches
2700 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2701 size_t n
, count
= children
.Count();
2702 for (n
= 0; n
< count
; ++n
)
2703 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2706 void wxGenericTreeCtrl::CalculatePositions()
2708 if ( !m_anchor
) return;
2710 wxClientDC
dc(this);
2713 dc
.SetFont( m_normalFont
);
2715 dc
.SetPen( m_dottedPen
);
2716 //if(GetImageList() == NULL)
2717 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2720 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2723 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2725 if (m_dirty
) return;
2727 wxClientDC
dc(this);
2732 GetClientSize( &cw
, &ch
);
2735 rect
.x
= dc
.LogicalToDeviceX( 0 );
2737 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2740 Refresh( TRUE
, &rect
);
2742 AdjustMyScrollbars();
2745 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2747 if (m_dirty
) return;
2749 wxClientDC
dc(this);
2754 GetClientSize( &cw
, &ch
);
2757 rect
.x
= dc
.LogicalToDeviceX( 0 );
2758 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2760 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2762 Refresh( TRUE
, &rect
);
2765 #endif // wxUSE_TREECTRL