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"
31 #include "wx/generic/treectlg.h"
32 #include "wx/imaglist.h"
33 #include "wx/settings.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
48 //WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int NO_IMAGE
= -1;
56 #define PIXELS_PER_UNIT 10
58 // -----------------------------------------------------------------------------
60 // -----------------------------------------------------------------------------
62 // timer used for enabling in-place edit
63 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
66 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
71 wxGenericTreeCtrl
*m_owner
;
74 // control used for in-place edit
75 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
79 wxTreeTextCtrl( wxWindow
*parent
,
83 wxGenericTreeCtrl
*owner
,
84 const wxString
&value
= wxEmptyString
,
85 const wxPoint
&pos
= wxDefaultPosition
,
86 const wxSize
&size
= wxDefaultSize
,
87 int style
= wxSIMPLE_BORDER
,
88 const wxValidator
& validator
= wxDefaultValidator
,
89 const wxString
&name
= wxTextCtrlNameStr
);
91 void OnChar( wxKeyEvent
&event
);
92 void OnKillFocus( wxFocusEvent
&event
);
97 wxGenericTreeCtrl
*m_owner
;
98 wxString m_startValue
;
100 DECLARE_EVENT_TABLE()
101 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
105 class WXDLLEXPORT wxGenericTreeItem
109 wxGenericTreeItem() { m_data
= NULL
; }
110 wxGenericTreeItem( wxGenericTreeItem
*parent
,
111 const wxString
& text
,
113 int image
, int selImage
,
114 wxTreeItemData
*data
);
116 ~wxGenericTreeItem();
119 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
121 const wxString
& GetText() const { return m_text
; }
122 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
123 { return m_images
[which
]; }
124 wxTreeItemData
*GetData() const { return m_data
; }
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
130 void SetText( const wxString
&text
);
131 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
136 void SetBold(bool bold
) { m_isBold
= bold
; }
138 int GetX() const { return m_x
; }
139 int GetY() const { return m_y
; }
141 void SetX(int x
) { m_x
= x
; }
142 void SetY(int y
) { m_y
= y
; }
144 int GetHeight() const { return m_height
; }
145 int GetWidth() const { return m_width
; }
147 void SetHeight(int h
) { m_height
= h
; }
148 void SetWidth(int w
) { m_width
= w
; }
151 wxGenericTreeItem
*GetParent() const { return m_parent
; }
154 // deletes all children notifying the treectrl about it if !NULL
156 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
157 // FIXME don't know what is it for
160 // get count of all children (and grand children if 'recursively')
161 size_t GetChildrenCount(bool recursively
= TRUE
) const;
163 void Insert(wxGenericTreeItem
*child
, size_t index
)
164 { m_children
.Insert(child
, index
); }
166 void SetCross( int x
, int y
);
167 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
169 // return the item at given position (or NULL if no item), onButton is
170 // TRUE if the point belongs to the item's button, otherwise it lies
171 // on the button's label
172 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxGenericTreeCtrl
*, int &flags
);
174 void Expand() { m_isCollapsed
= FALSE
; }
175 void Collapse() { m_isCollapsed
= TRUE
; }
177 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
180 bool HasChildren() const { return !m_children
.IsEmpty(); }
181 bool IsSelected() const { return m_hasHilight
!= 0; }
182 bool IsExpanded() const { return !m_isCollapsed
; }
183 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
184 bool IsBold() const { return m_isBold
!= 0; }
187 // get them - may be NULL
188 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
189 // get them ensuring that the pointer is not NULL
190 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
201 // tree ctrl images for the normal, selected, expanded and
202 // expanded+selected states
203 int m_images
[wxTreeItemIcon_Max
];
205 wxTreeItemData
*m_data
;
207 // use bitfields to save size
208 int m_isCollapsed
:1;
209 int m_hasHilight
:1; // same as focused
210 int m_hasPlus
:1; // used for item which doesn't have
211 // children but has a [+] button
212 int m_isBold
:1; // render the label in bold font
215 wxCoord m_height
, m_width
;
216 int m_xCross
, m_yCross
;
219 wxArrayGenericTreeItems m_children
;
220 wxGenericTreeItem
*m_parent
;
222 wxTreeItemAttr
*m_attr
;
225 // =============================================================================
227 // =============================================================================
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 // translate the key or mouse event flags to the type of selection we're
235 static void EventFlagsToSelType(long style
,
239 bool &extended_select
,
240 bool &unselect_others
)
242 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
243 extended_select
= shiftDown
&& is_multiple
;
244 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
247 // -----------------------------------------------------------------------------
248 // wxTreeRenameTimer (internal)
249 // -----------------------------------------------------------------------------
251 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
256 void wxTreeRenameTimer::Notify()
258 m_owner
->OnRenameTimer();
261 //-----------------------------------------------------------------------------
262 // wxTreeTextCtrl (internal)
263 //-----------------------------------------------------------------------------
265 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
267 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
268 EVT_CHAR (wxTreeTextCtrl::OnChar
)
269 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
272 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
276 wxGenericTreeCtrl
*owner
,
277 const wxString
&value
,
281 const wxValidator
& validator
,
282 const wxString
&name
)
283 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
289 (*m_res
) = wxEmptyString
;
290 m_startValue
= value
;
293 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
295 if (event
.m_keyCode
== WXK_RETURN
)
298 (*m_res
) = GetValue();
300 if (!wxPendingDelete
.Member(this))
301 wxPendingDelete
.Append(this);
303 if ((*m_accept
) && ((*m_res
) != m_startValue
))
304 m_owner
->OnRenameAccept();
308 if (event
.m_keyCode
== WXK_ESCAPE
)
313 if (!wxPendingDelete
.Member(this))
314 wxPendingDelete
.Append(this);
321 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
323 if (!wxPendingDelete
.Member(this))
324 wxPendingDelete
.Append(this);
326 if ((*m_accept
) && ((*m_res
) != m_startValue
))
327 m_owner
->OnRenameAccept();
331 // -----------------------------------------------------------------------------
333 // -----------------------------------------------------------------------------
335 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
337 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
338 : wxNotifyEvent( commandType
, id
)
341 m_itemOld
= (wxGenericTreeItem
*)NULL
;
345 // -----------------------------------------------------------------------------
347 // -----------------------------------------------------------------------------
349 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
350 const wxString
& text
,
352 int image
, int selImage
,
353 wxTreeItemData
*data
)
356 m_images
[wxTreeItemIcon_Normal
] = image
;
357 m_images
[wxTreeItemIcon_Selected
] = selImage
;
358 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
359 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
363 m_xCross
= m_yCross
= 0;
367 m_isCollapsed
= TRUE
;
368 m_hasHilight
= FALSE
;
374 m_attr
= (wxTreeItemAttr
*)NULL
;
376 // We don't know the height here yet.
381 wxGenericTreeItem::~wxGenericTreeItem()
387 wxASSERT_MSG( m_children
.IsEmpty(),
388 wxT("please call DeleteChildren() before deleting the item") );
391 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
393 size_t count
= m_children
.Count();
394 for ( size_t n
= 0; n
< count
; n
++ )
396 wxGenericTreeItem
*child
= m_children
[n
];
398 tree
->SendDeleteEvent(child
);
400 child
->DeleteChildren(tree
);
407 void wxGenericTreeItem::SetText( const wxString
&text
)
412 void wxGenericTreeItem::Reset()
415 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
417 m_images
[i
] = NO_IMAGE
;
422 m_height
= m_width
= 0;
429 m_isCollapsed
= TRUE
;
431 m_parent
= (wxGenericTreeItem
*)NULL
;
434 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
436 size_t count
= m_children
.Count();
440 size_t total
= count
;
441 for (size_t n
= 0; n
< count
; ++n
)
443 total
+= m_children
[n
]->GetChildrenCount();
449 void wxGenericTreeItem::SetCross( int x
, int y
)
455 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxGenericTreeCtrl
*theTree
)
457 int bottomY
=m_y
+theTree
->GetLineHeight(this);
458 if ( y
< bottomY
) y
= bottomY
;
459 int width
= m_x
+ m_width
;
460 if ( x
< width
) x
= width
;
464 size_t count
= m_children
.Count();
465 for ( size_t n
= 0; n
< count
; ++n
)
467 m_children
[n
]->GetSize( x
, y
, theTree
);
472 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
473 const wxGenericTreeCtrl
*theTree
,
476 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
478 if (point
.y
< m_y
+theTree
->GetLineHeight(this)/2 )
479 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
481 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
483 // 5 is the size of the plus sign
484 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
485 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
486 (IsExpanded() || HasPlus()))
488 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
492 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
497 // assuming every image (normal and selected ) has the same size !
498 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
499 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
501 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
502 flags
|= wxTREE_HITTEST_ONITEMICON
;
504 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
510 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
511 if (point
.x
> m_x
+m_width
)
512 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
520 size_t count
= m_children
.Count();
521 for ( size_t n
= 0; n
< count
; n
++ )
523 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
530 flags
|=wxTREE_HITTEST_NOWHERE
;
532 return (wxGenericTreeItem
*) NULL
;
535 int wxGenericTreeItem::GetCurrentImage() const
537 int image
= NO_IMAGE
;
542 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
545 if ( image
== NO_IMAGE
)
547 // we usually fall back to the normal item, but try just the
548 // expanded one (and not selected) first in this case
549 image
= GetImage(wxTreeItemIcon_Expanded
);
555 image
= GetImage(wxTreeItemIcon_Selected
);
558 // may be it doesn't have the specific image we want, try the default one
560 if ( image
== NO_IMAGE
)
568 // -----------------------------------------------------------------------------
569 // wxGenericTreeCtrl implementation
570 // -----------------------------------------------------------------------------
572 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
574 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
575 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
576 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
577 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
578 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
579 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
580 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
583 #if !defined(__WXMSW__) || defined(__WIN16__)
585 * wxTreeCtrl has to be a real class or we have problems with
586 * the run-time information.
589 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
592 // -----------------------------------------------------------------------------
593 // construction/destruction
594 // -----------------------------------------------------------------------------
596 void wxGenericTreeCtrl::Init()
600 m_anchor
= (wxGenericTreeItem
*) NULL
;
610 m_hilightBrush
= new wxBrush
612 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
617 m_imageListState
= (wxImageList
*) NULL
;
618 m_ownsImageListNormal
=
619 m_ownsImageListState
= FALSE
;
622 m_isDragging
= FALSE
;
624 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
626 m_renameTimer
= new wxTreeRenameTimer( this );
627 m_lastOnSame
= FALSE
;
629 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
630 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
631 m_normalFont
.GetFamily(),
632 m_normalFont
.GetStyle(),
634 m_normalFont
.GetUnderlined());
637 bool wxGenericTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
638 const wxPoint
& pos
, const wxSize
& size
,
640 const wxValidator
&validator
,
641 const wxString
& name
)
643 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
646 SetValidator( validator
);
649 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
650 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
651 m_dottedPen
= wxPen( "grey", 0, 0 );
656 wxGenericTreeCtrl::~wxGenericTreeCtrl()
658 wxDELETE( m_hilightBrush
);
662 delete m_renameTimer
;
663 if (m_ownsImageListNormal
) delete m_imageListNormal
;
664 if (m_ownsImageListState
) delete m_imageListState
;
667 // -----------------------------------------------------------------------------
669 // -----------------------------------------------------------------------------
671 size_t wxGenericTreeCtrl::GetCount() const
673 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
676 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
682 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
688 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
690 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
692 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
695 // -----------------------------------------------------------------------------
696 // functions to work with tree items
697 // -----------------------------------------------------------------------------
699 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
701 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
703 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
706 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
707 wxTreeItemIcon which
) const
709 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
711 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
714 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
716 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
718 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
721 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
723 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
726 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
727 pItem
->SetText(text
);
728 CalculateSize(pItem
, dc
);
732 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
734 wxTreeItemIcon which
)
736 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
738 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
739 pItem
->SetImage(image
, which
);
742 CalculateSize(pItem
, dc
);
746 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
748 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
750 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
753 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
755 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
757 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
758 pItem
->SetHasPlus(has
);
762 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
764 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
766 // avoid redrawing the tree if no real change
767 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
768 if ( pItem
->IsBold() != bold
)
770 pItem
->SetBold(bold
);
775 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
778 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
780 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
781 pItem
->Attr().SetTextColour(col
);
785 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
788 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
790 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
791 pItem
->Attr().SetBackgroundColour(col
);
795 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
797 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
799 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
800 pItem
->Attr().SetFont(font
);
804 // -----------------------------------------------------------------------------
805 // item status inquiries
806 // -----------------------------------------------------------------------------
808 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
810 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
812 // An item is only visible if it's not a descendant of a collapsed item
813 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
814 wxGenericTreeItem
* parent
= pItem
->GetParent();
817 if (!parent
->IsExpanded())
819 parent
= parent
->GetParent();
823 GetViewStart(& startX
, & startY
);
825 wxSize clientSize
= GetClientSize();
828 if (!GetBoundingRect(item
, rect
))
830 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
832 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
834 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
840 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
842 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
844 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
847 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
849 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
851 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
854 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
856 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
858 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
861 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
863 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
865 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
868 // -----------------------------------------------------------------------------
870 // -----------------------------------------------------------------------------
872 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
874 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
876 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
879 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
881 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
884 return GetNextChild(item
, cookie
);
887 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
889 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
891 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
892 if ( (size_t)cookie
< children
.Count() )
894 return children
.Item((size_t)cookie
++);
898 // there are no more of them
899 return wxTreeItemId();
903 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
905 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
907 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
908 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
911 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
913 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
915 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
916 wxGenericTreeItem
*parent
= i
->GetParent();
917 if ( parent
== NULL
)
919 // root item doesn't have any siblings
920 return wxTreeItemId();
923 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
924 int index
= siblings
.Index(i
);
925 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
927 size_t n
= (size_t)(index
+ 1);
928 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
931 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
933 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
935 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
936 wxGenericTreeItem
*parent
= i
->GetParent();
937 if ( parent
== NULL
)
939 // root item doesn't have any siblings
940 return wxTreeItemId();
943 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
944 int index
= siblings
.Index(i
);
945 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
947 return index
== 0 ? wxTreeItemId()
948 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
951 // Only for internal use right now, but should probably be public
952 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
954 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
956 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
958 // First see if there are any children.
959 wxArrayGenericTreeItems
& children
= i
->GetChildren();
960 if (children
.GetCount() > 0)
962 return children
.Item(0);
966 // Try a sibling of this or ancestor instead
967 wxTreeItemId p
= item
;
971 toFind
= GetNextSibling(p
);
973 } while (p
.IsOk() && !toFind
.IsOk());
978 wxTreeItemId
wxGenericTreeCtrl::GetPrev(const wxTreeItemId
& item
) const
980 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
982 wxFAIL_MSG(wxT("not implemented"));
984 return wxTreeItemId();
987 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
989 wxTreeItemId id
= GetRootItem();
1000 return wxTreeItemId();
1003 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1005 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1007 wxTreeItemId id
= item
;
1012 if (id
.IsOk() && IsVisible(id
))
1015 return wxTreeItemId();
1018 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1020 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1022 wxFAIL_MSG(wxT("not implemented"));
1024 return wxTreeItemId();
1027 // -----------------------------------------------------------------------------
1029 // -----------------------------------------------------------------------------
1031 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1033 const wxString
& text
,
1034 int image
, int selImage
,
1035 wxTreeItemData
*data
)
1037 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1040 // should we give a warning here?
1041 return AddRoot(text
, image
, selImage
, data
);
1044 wxClientDC
dc(this);
1045 wxGenericTreeItem
*item
=
1046 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1050 data
->m_pItem
= (long) item
;
1053 parent
->Insert( item
, previous
);
1060 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1061 int image
, int selImage
,
1062 wxTreeItemData
*data
)
1064 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1066 wxClientDC
dc(this);
1067 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1068 image
, selImage
, data
);
1071 data
->m_pItem
= (long) m_anchor
;
1074 if (!HasFlag(wxTR_MULTIPLE
))
1076 m_current
= m_key_current
= m_anchor
;
1077 m_current
->SetHilight( TRUE
);
1085 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1086 const wxString
& text
,
1087 int image
, int selImage
,
1088 wxTreeItemData
*data
)
1090 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1093 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1094 const wxTreeItemId
& idPrevious
,
1095 const wxString
& text
,
1096 int image
, int selImage
,
1097 wxTreeItemData
*data
)
1099 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1102 // should we give a warning here?
1103 return AddRoot(text
, image
, selImage
, data
);
1106 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1107 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1108 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1110 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1113 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1115 const wxString
& text
,
1116 int image
, int selImage
,
1117 wxTreeItemData
*data
)
1119 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1122 // should we give a warning here?
1123 return AddRoot(text
, image
, selImage
, data
);
1126 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1129 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
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 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1142 image
, selImage
, data
);
1145 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1147 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1148 event
.m_item
= (long) item
;
1149 event
.SetEventObject( this );
1150 ProcessEvent( event
);
1153 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1155 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1156 item
->DeleteChildren(this);
1161 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1163 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1165 // don't stay with invalid m_key_current or we will crash in the next call
1167 bool changeKeyCurrent
= FALSE
;
1168 wxGenericTreeItem
*itemKey
= m_key_current
;
1169 while ( itemKey
&& !changeKeyCurrent
)
1171 if ( itemKey
== item
)
1173 // m_key_current is a descendant of the item being deleted
1174 changeKeyCurrent
= TRUE
;
1178 itemKey
= itemKey
->GetParent();
1182 wxGenericTreeItem
*parent
= item
->GetParent();
1185 parent
->GetChildren().Remove( item
); // remove by value
1188 if ( changeKeyCurrent
)
1190 // may be NULL or not
1191 m_key_current
= parent
;
1194 item
->DeleteChildren(this);
1195 SendDeleteEvent(item
);
1201 void wxGenericTreeCtrl::DeleteAllItems()
1205 m_anchor
->DeleteChildren(this);
1214 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1216 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1218 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1220 if ( !item
->HasPlus() )
1223 if ( item
->IsExpanded() )
1226 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1227 event
.m_item
= (long) item
;
1228 event
.SetEventObject( this );
1230 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1232 // cancelled by program
1237 CalculatePositions();
1239 RefreshSubtree(item
);
1241 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1242 ProcessEvent( event
);
1245 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1248 if ( IsExpanded(item
) )
1251 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1252 while ( child
.IsOk() )
1256 child
= GetNextChild(item
, cookie
);
1261 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1263 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1265 if ( !item
->IsExpanded() )
1268 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1269 event
.m_item
= (long) item
;
1270 event
.SetEventObject( this );
1271 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1273 // cancelled by program
1279 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1280 size_t count
= children
.Count();
1281 for ( size_t n
= 0; n
< count
; n
++ )
1283 Collapse(children
[n
]);
1286 CalculatePositions();
1288 RefreshSubtree(item
);
1290 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1291 ProcessEvent( event
);
1294 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1297 DeleteChildren(item
);
1300 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1302 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1304 if (item
->IsExpanded())
1310 void wxGenericTreeCtrl::Unselect()
1314 m_current
->SetHilight( FALSE
);
1315 RefreshLine( m_current
);
1319 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1321 if (item
->IsSelected())
1323 item
->SetHilight(FALSE
);
1327 if (item
->HasChildren())
1329 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1330 size_t count
= children
.Count();
1331 for ( size_t n
= 0; n
< count
; ++n
)
1333 UnselectAllChildren(children
[n
]);
1338 void wxGenericTreeCtrl::UnselectAll()
1340 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1343 // Recursive function !
1344 // To stop we must have crt_item<last_item
1346 // Tag all next children, when no more children,
1347 // Move to parent (not to tag)
1348 // Keep going... if we found last_item, we stop.
1349 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1351 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1353 if (parent
== NULL
) // This is root item
1354 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1356 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1357 int index
= children
.Index(crt_item
);
1358 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1360 size_t count
= children
.Count();
1361 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1363 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1366 return TagNextChildren(parent
, last_item
, select
);
1369 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1371 crt_item
->SetHilight(select
);
1372 RefreshLine(crt_item
);
1374 if (crt_item
==last_item
)
1377 if (crt_item
->HasChildren())
1379 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1380 size_t count
= children
.Count();
1381 for ( size_t n
= 0; n
< count
; ++n
)
1383 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1391 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1393 // item2 is not necessary after item1
1394 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1396 // choice first' and 'last' between item1 and item2
1397 if (item1
->GetY()<item2
->GetY())
1408 bool select
= m_current
->IsSelected();
1410 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1413 TagNextChildren(first
,last
,select
);
1416 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1417 bool unselect_others
,
1418 bool extended_select
)
1420 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1422 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1423 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1425 //wxCHECK_RET( ( (!unselect_others) && is_single),
1426 // wxT("this is a single selection tree") );
1428 // to keep going anyhow !!!
1431 if (item
->IsSelected())
1432 return; // nothing to do
1433 unselect_others
= TRUE
;
1434 extended_select
= FALSE
;
1436 else if ( unselect_others
&& item
->IsSelected() )
1438 // selection change if there is more than one item currently selected
1439 wxArrayTreeItemIds selected_items
;
1440 if ( GetSelections(selected_items
) == 1 )
1444 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1445 event
.m_item
= (long) item
;
1446 event
.m_itemOld
= (long) m_current
;
1447 event
.SetEventObject( this );
1448 // TODO : Here we don't send any selection mode yet !
1450 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1454 if (unselect_others
)
1456 if (is_single
) Unselect(); // to speed up thing
1461 if (extended_select
)
1466 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1469 // don't change the mark (m_current)
1470 SelectItemRange(m_current
, item
);
1474 bool select
=TRUE
; // the default
1476 // Check if we need to toggle hilight (ctrl mode)
1477 if (!unselect_others
)
1478 select
=!item
->IsSelected();
1480 m_current
= m_key_current
= item
;
1481 m_current
->SetHilight(select
);
1482 RefreshLine( m_current
);
1485 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1486 GetEventHandler()->ProcessEvent( event
);
1489 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1490 wxArrayTreeItemIds
&array
) const
1492 if ( item
->IsSelected() )
1493 array
.Add(wxTreeItemId(item
));
1495 if ( item
->HasChildren() )
1497 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1498 size_t count
= children
.GetCount();
1499 for ( size_t n
= 0; n
< count
; ++n
)
1500 FillArray(children
[n
], array
);
1504 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1507 wxTreeItemId idRoot
= GetRootItem();
1508 if ( idRoot
.IsOk() )
1510 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1512 //else: the tree is empty, so no selections
1514 return array
.Count();
1517 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1519 if (!item
.IsOk()) return;
1521 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1523 // first expand all parent branches
1524 wxGenericTreeItem
*parent
= gitem
->GetParent();
1528 parent
= parent
->GetParent();
1531 //if (parent) CalculatePositions();
1536 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1538 if (!item
.IsOk()) return;
1540 // We have to call this here because the label in
1541 // question might just have been added and no screen
1542 // update taken place.
1543 if (m_dirty
) wxYield();
1545 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1547 // now scroll to the item
1548 int item_y
= gitem
->GetY();
1552 ViewStart( &start_x
, &start_y
);
1553 start_y
*= PIXELS_PER_UNIT
;
1557 GetClientSize( &client_w
, &client_h
);
1559 if (item_y
< start_y
+3)
1564 m_anchor
->GetSize( x
, y
, this );
1565 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1566 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1567 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1568 // Item should appear at top
1569 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1571 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1576 m_anchor
->GetSize( x
, y
, this );
1577 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1578 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1579 item_y
+= PIXELS_PER_UNIT
+2;
1580 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1581 // Item should appear at bottom
1582 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
);
1586 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1587 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1589 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1590 wxGenericTreeItem
**item2
)
1592 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1594 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1597 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1598 const wxTreeItemId
& item2
)
1600 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1603 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1605 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1607 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1609 wxCHECK_RET( !s_treeBeingSorted
,
1610 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1612 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1613 if ( children
.Count() > 1 )
1615 s_treeBeingSorted
= this;
1616 children
.Sort(tree_ctrl_compare_func
);
1617 s_treeBeingSorted
= NULL
;
1621 //else: don't make the tree dirty as nothing changed
1624 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1626 return m_imageListNormal
;
1629 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1631 return m_imageListState
;
1634 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1636 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1638 m_imageListNormal
= imageList
;
1639 m_ownsImageListNormal
= FALSE
;
1641 if ( !m_imageListNormal
)
1644 // Calculate a m_lineHeight value from the image sizes.
1645 // May be toggle off. Then wxGenericTreeCtrl will spread when
1646 // necessary (which might look ugly).
1647 wxClientDC
dc(this);
1648 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1649 int width
= 0, height
= 0,
1650 n
= m_imageListNormal
->GetImageCount();
1652 for (int i
= 0; i
< n
; i
++)
1654 m_imageListNormal
->GetSize(i
, width
, height
);
1655 if (height
> m_lineHeight
) m_lineHeight
= height
;
1658 if (m_lineHeight
< 40)
1659 m_lineHeight
+= 2; // at least 2 pixels
1661 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1664 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1666 if (m_ownsImageListState
) delete m_imageListState
;
1667 m_imageListState
= imageList
;
1668 m_ownsImageListState
= FALSE
;
1671 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1673 SetImageList(imageList
);
1674 m_ownsImageListNormal
= TRUE
;
1677 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1679 SetStateImageList(imageList
);
1680 m_ownsImageListState
= TRUE
;
1683 // -----------------------------------------------------------------------------
1685 // -----------------------------------------------------------------------------
1687 void wxGenericTreeCtrl::AdjustMyScrollbars()
1693 m_anchor
->GetSize( x
, y
, this );
1694 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1695 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1696 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1697 int y_pos
= GetScrollPos( wxVERTICAL
);
1698 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1702 SetScrollbars( 0, 0, 0, 0 );
1706 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1708 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1709 return item
->GetHeight();
1711 return m_lineHeight
;
1714 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1716 wxTreeItemAttr
*attr
= item
->GetAttributes();
1717 if ( attr
&& attr
->HasFont() )
1718 dc
.SetFont(attr
->GetFont());
1719 else if (item
->IsBold())
1720 dc
.SetFont(m_boldFont
);
1724 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1728 int image
= item
->GetCurrentImage();
1729 if ( image
!= NO_IMAGE
)
1731 if ( m_imageListNormal
)
1733 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1742 int total_h
= GetLineHeight(item
);
1744 if (item
->IsSelected())
1745 dc
.SetBrush(*m_hilightBrush
);
1749 if ( attr
&& attr
->HasBackgroundColour() )
1750 colBg
= attr
->GetBackgroundColour();
1752 colBg
= m_backgroundColour
;
1753 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1756 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1758 if ( image
!= NO_IMAGE
)
1760 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1761 m_imageListNormal
->Draw( image
, dc
,
1763 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1764 wxIMAGELIST_DRAW_TRANSPARENT
);
1765 dc
.DestroyClippingRegion();
1768 dc
.SetBackgroundMode(wxTRANSPARENT
);
1769 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1770 dc
.DrawText( item
->GetText(),
1771 (wxCoord
)(image_w
+ item
->GetX()),
1772 (wxCoord
)(item
->GetY() + extraH
));
1774 // restore normal font
1775 dc
.SetFont( m_normalFont
);
1778 // Now y stands for the top of the item, whereas it used to stand for middle !
1779 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1781 int horizX
= level
*m_indent
;
1783 item
->SetX( horizX
+m_indent
+m_spacing
);
1787 y
+=GetLineHeight(item
)/2;
1789 item
->SetCross( horizX
+m_indent
, y
);
1791 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1792 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1794 bool drawLines
= ((GetWindowStyle() & wxTR_NO_LINES
) == 0);
1796 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1798 int startX
= horizX
;
1799 int endX
= horizX
+ (m_indent
-5);
1801 // if (!item->HasChildren()) endX += (m_indent+5);
1802 if (!item
->HasChildren()) endX
+= 20;
1805 dc
.DrawLine( startX
, y
, endX
, y
);
1807 if (item
->HasPlus())
1810 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1811 dc
.SetPen( *wxGREY_PEN
);
1812 dc
.SetBrush( *wxWHITE_BRUSH
);
1813 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1815 dc
.SetPen( *wxBLACK_PEN
);
1816 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1817 if (!item
->IsExpanded())
1818 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1820 dc
.SetPen( m_dottedPen
);
1823 wxPen
*pen
= wxTRANSPARENT_PEN
;
1826 if ( item
->IsSelected() )
1828 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1836 wxTreeItemAttr
*attr
= item
->GetAttributes();
1837 if ( attr
&& attr
->HasTextColour() )
1838 colText
= attr
->GetTextColour();
1840 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
);
1844 dc
.SetTextForeground(colText
);
1848 PaintItem(item
, dc
);
1850 // restore DC objects
1851 dc
.SetBrush( *wxWHITE_BRUSH
);
1852 dc
.SetPen( m_dottedPen
);
1853 dc
.SetTextForeground( *wxBLACK
);
1856 y
= oldY
+GetLineHeight(item
);
1858 if (item
->IsExpanded())
1860 oldY
+=GetLineHeight(item
)/2;
1863 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1864 size_t n
, count
= children
.Count();
1865 for ( n
= 0; n
< count
; ++n
)
1868 PaintLevel( children
[n
], dc
, level
+1, y
);
1871 // it may happen that the item is expanded but has no items (when you
1872 // delete all its children for example) - don't draw the vertical line
1876 semiOldY
+=GetLineHeight(children
[--n
])/2;
1878 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1883 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1887 if ( item
->HasPlus() )
1889 // it's a folder, indicate it by a border
1894 // draw a line under the drop target because the item will be
1896 DrawLine(item
, TRUE
/* below */);
1899 SetCursor(wxCURSOR_BULLSEYE
);
1904 SetCursor(wxCURSOR_NO_ENTRY
);
1908 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
1910 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1912 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1914 wxClientDC
dc(this);
1916 dc
.SetLogicalFunction(wxINVERT
);
1917 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1919 int w
= i
->GetWidth() + 2;
1920 int h
= GetLineHeight(i
) + 2;
1922 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
1925 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
1927 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1929 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1931 wxClientDC
dc(this);
1933 dc
.SetLogicalFunction(wxINVERT
);
1939 y
+= GetLineHeight(i
) - 1;
1942 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
1945 // -----------------------------------------------------------------------------
1946 // wxWindows callbacks
1947 // -----------------------------------------------------------------------------
1949 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1957 dc
.SetFont( m_normalFont
);
1958 dc
.SetPen( m_dottedPen
);
1960 // this is now done dynamically
1961 //if(GetImageList() == NULL)
1962 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1965 PaintLevel( m_anchor
, dc
, 0, y
);
1968 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1972 if (m_current
) RefreshLine( m_current
);
1975 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1979 if (m_current
) RefreshLine( m_current
);
1982 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
1984 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1985 te
.m_code
= (int)event
.KeyCode();
1986 te
.SetEventObject( this );
1987 GetEventHandler()->ProcessEvent( te
);
1989 if ( (m_current
== 0) || (m_key_current
== 0) )
1995 // how should the selection work for this event?
1996 bool is_multiple
, extended_select
, unselect_others
;
1997 EventFlagsToSelType(GetWindowStyleFlag(),
1999 event
.ControlDown(),
2000 is_multiple
, extended_select
, unselect_others
);
2004 // * : Expand all/Collapse all
2005 // ' ' | return : activate
2006 // up : go up (not last children!)
2008 // left : go to parent
2009 // right : open if parent and go next
2010 // home : go to root
2011 // end : go to last item without opening parents
2012 switch (event
.KeyCode())
2016 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2024 if ( !IsExpanded(m_current
) )
2027 ExpandAll(m_current
);
2030 //else: fall through to Collapse() it
2034 if (IsExpanded(m_current
))
2036 Collapse(m_current
);
2043 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2044 event
.m_item
= (long) m_current
;
2046 event
.SetEventObject( this );
2047 GetEventHandler()->ProcessEvent( event
);
2051 // up goes to the previous sibling or to the last of its children if
2055 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2058 prev
= GetParent( m_key_current
);
2062 wxTreeItemId current
= m_key_current
;
2063 if (current
== GetFirstChild( prev
, cockie
))
2065 // otherwise we return to where we came from
2066 SelectItem( prev
, unselect_others
, extended_select
);
2067 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2068 EnsureVisible( prev
);
2075 while ( IsExpanded(prev
) && HasChildren(prev
) )
2077 wxTreeItemId child
= GetLastChild(prev
);
2084 SelectItem( prev
, unselect_others
, extended_select
);
2085 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2086 EnsureVisible( prev
);
2091 // left arrow goes to the parent
2094 wxTreeItemId prev
= GetParent( m_current
);
2097 EnsureVisible( prev
);
2098 SelectItem( prev
, unselect_others
, extended_select
);
2104 // this works the same as the down arrow except that we also expand the
2105 // item if it wasn't expanded yet
2111 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2114 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2115 SelectItem( child
, unselect_others
, extended_select
);
2116 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2117 EnsureVisible( child
);
2121 wxTreeItemId next
= GetNextSibling( m_key_current
);
2124 wxTreeItemId current
= m_key_current
;
2125 while (current
&& !next
)
2127 current
= GetParent( current
);
2128 if (current
) next
= GetNextSibling( current
);
2133 SelectItem( next
, unselect_others
, extended_select
);
2134 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2135 EnsureVisible( next
);
2141 // <End> selects the last visible tree item
2144 wxTreeItemId last
= GetRootItem();
2146 while ( last
.IsOk() && IsExpanded(last
) )
2148 wxTreeItemId lastChild
= GetLastChild(last
);
2150 // it may happen if the item was expanded but then all of
2151 // its children have been deleted - so IsExpanded() returned
2152 // TRUE, but GetLastChild() returned invalid item
2161 EnsureVisible( last
);
2162 SelectItem( last
, unselect_others
, extended_select
);
2167 // <Home> selects the root item
2170 wxTreeItemId prev
= GetRootItem();
2173 EnsureVisible( prev
);
2174 SelectItem( prev
, unselect_others
, extended_select
);
2184 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2186 // We have to call this here because the label in
2187 // question might just have been added and no screen
2188 // update taken place.
2189 if (m_dirty
) wxYield();
2191 wxClientDC
dc(this);
2193 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2194 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2199 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2200 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2201 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2202 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2204 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2207 // get the bounding rectangle of the item (or of its label only)
2208 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2210 bool textOnly
) const
2212 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2214 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2217 GetViewStart(& startX
, & startY
);
2219 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2220 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2221 rect
.width
= i
->GetWidth();
2222 //rect.height = i->GetHeight();
2223 rect
.height
= GetLineHeight(i
);
2230 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2232 if (!item
.IsOk()) return;
2234 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2236 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2237 te
.m_item
= (long) m_currentEdit
;
2238 te
.SetEventObject( this );
2239 GetEventHandler()->ProcessEvent( te
);
2241 if (!te
.IsAllowed()) return;
2243 // We have to call this here because the label in
2244 // question might just have been added and no screen
2245 // update taken place.
2246 if (m_dirty
) wxYield();
2248 wxString s
= m_currentEdit
->GetText();
2249 int x
= m_currentEdit
->GetX();
2250 int y
= m_currentEdit
->GetY();
2251 int w
= m_currentEdit
->GetWidth();
2252 int h
= m_currentEdit
->GetHeight();
2257 int image
= m_currentEdit
->GetCurrentImage();
2258 if ( image
!= NO_IMAGE
)
2260 if ( m_imageListNormal
)
2262 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2267 wxFAIL_MSG(_T("you must create an image list to use images!"));
2271 w
-= image_w
+ 4; // I don't know why +4 is needed
2273 wxClientDC
dc(this);
2275 x
= dc
.LogicalToDeviceX( x
);
2276 y
= dc
.LogicalToDeviceY( y
);
2278 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
2279 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
2283 void wxGenericTreeCtrl::OnRenameTimer()
2288 void wxGenericTreeCtrl::OnRenameAccept()
2290 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2291 le
.m_item
= (long) m_currentEdit
;
2292 le
.SetEventObject( this );
2293 le
.m_label
= m_renameRes
;
2294 GetEventHandler()->ProcessEvent( le
);
2296 if (!le
.IsAllowed()) return;
2298 SetItemText( m_currentEdit
, m_renameRes
);
2301 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2303 if ( !m_anchor
) return;
2305 // we process left mouse up event (enables in-place edit), right down
2306 // (pass to the user code), left dbl click (activate item) and
2307 // dragging/moving events for items drag-and-drop
2308 if ( !(event
.LeftDown() ||
2310 event
.RightDown() ||
2311 event
.LeftDClick() ||
2313 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2320 wxClientDC
dc(this);
2322 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2323 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2326 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2328 if ( event
.Dragging() && !m_isDragging
)
2330 if (m_dragCount
== 0)
2331 m_dragStart
= wxPoint(x
,y
);
2335 if (m_dragCount
!= 3)
2337 // wait until user drags a bit further...
2341 wxEventType command
= event
.RightIsDown()
2342 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2343 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2345 wxTreeEvent
nevent( command
, GetId() );
2346 nevent
.m_item
= (long) m_current
;
2347 nevent
.SetEventObject(this);
2349 // by default the dragging is not supported, the user code must
2350 // explicitly allow the event for it to take place
2353 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2355 // we're going to drag this item
2356 m_isDragging
= TRUE
;
2358 // remember the old cursor because we will change it while
2360 m_oldCursor
= m_cursor
;
2362 // in a single selection control, hide the selection temporarily
2363 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2365 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2367 if ( m_oldSelection
)
2369 m_oldSelection
->SetHilight(FALSE
);
2370 RefreshLine(m_oldSelection
);
2377 else if ( event
.Moving() )
2379 if ( item
!= m_dropTarget
)
2381 // unhighlight the previous drop target
2382 DrawDropEffect(m_dropTarget
);
2384 m_dropTarget
= item
;
2386 // highlight the current drop target if any
2387 DrawDropEffect(m_dropTarget
);
2392 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2394 // erase the highlighting
2395 DrawDropEffect(m_dropTarget
);
2397 // generate the drag end event
2398 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2400 event
.m_item
= (long) item
;
2401 event
.m_pointDrag
= wxPoint(x
, y
);
2402 event
.SetEventObject(this);
2404 (void)GetEventHandler()->ProcessEvent(event
);
2406 m_isDragging
= FALSE
;
2407 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2409 if ( m_oldSelection
)
2411 m_oldSelection
->SetHilight(TRUE
);
2412 RefreshLine(m_oldSelection
);
2413 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2418 SetCursor(m_oldCursor
);
2424 // here we process only the messages which happen on tree items
2428 if (item
== NULL
) return; /* we hit the blank area */
2430 if ( event
.RightDown() )
2432 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2433 nevent
.m_item
= (long) item
;
2435 nevent
.SetEventObject(this);
2436 GetEventHandler()->ProcessEvent(nevent
);
2438 else if ( event
.LeftUp() )
2442 if ( (item
== m_current
) &&
2443 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2444 HasFlag(wxTR_EDIT_LABELS
) )
2446 if ( m_renameTimer
->IsRunning() )
2447 m_renameTimer
->Stop();
2449 m_renameTimer
->Start( 100, TRUE
);
2452 m_lastOnSame
= FALSE
;
2457 if ( event
.LeftDown() )
2459 m_lastOnSame
= item
== m_current
;
2462 // how should the selection work for this event?
2463 bool is_multiple
, extended_select
, unselect_others
;
2464 EventFlagsToSelType(GetWindowStyleFlag(),
2466 event
.ControlDown(),
2467 is_multiple
, extended_select
, unselect_others
);
2469 if ( (flags
& wxTREE_HITTEST_ONITEMBUTTON
) && event
.LeftDown() )
2476 SelectItem(item
, unselect_others
, extended_select
);
2478 if ( event
.LeftDClick() )
2480 // double clicking should not start editing the item label
2481 m_renameTimer
->Stop();
2482 m_lastOnSame
= FALSE
;
2484 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2485 nevent
.m_item
= (long) item
;
2487 nevent
.SetEventObject( this );
2488 GetEventHandler()->ProcessEvent( nevent
);
2494 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2496 /* after all changes have been done to the tree control,
2497 * we actually redraw the tree when everything is over */
2504 CalculatePositions();
2506 AdjustMyScrollbars();
2509 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2515 dc
.SetFont(m_boldFont
);
2517 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2520 // restore normal font
2521 dc
.SetFont( m_normalFont
);
2525 int image
= item
->GetCurrentImage();
2526 if ( image
!= NO_IMAGE
)
2528 if ( m_imageListNormal
)
2530 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2535 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2538 total_h
+= 2; // at least 2 pixels
2540 total_h
+= total_h
/10; // otherwise 10% extra spacing
2542 item
->SetHeight(total_h
);
2543 if (total_h
>m_lineHeight
)
2544 m_lineHeight
=total_h
;
2546 item
->SetWidth(image_w
+text_w
+2);
2549 // -----------------------------------------------------------------------------
2550 // for developper : y is now the top of the level
2551 // not the middle of it !
2552 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2554 int horizX
= level
*m_indent
;
2556 CalculateSize( item
, dc
);
2559 item
->SetX( horizX
+m_indent
+m_spacing
);
2561 y
+=GetLineHeight(item
);
2563 if ( !item
->IsExpanded() )
2565 // we dont need to calculate collapsed branches
2569 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2570 size_t n
, count
= children
.Count();
2571 for (n
= 0; n
< count
; ++n
)
2572 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2575 void wxGenericTreeCtrl::CalculatePositions()
2577 if ( !m_anchor
) return;
2579 wxClientDC
dc(this);
2582 dc
.SetFont( m_normalFont
);
2584 dc
.SetPen( m_dottedPen
);
2585 //if(GetImageList() == NULL)
2586 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2589 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2592 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2594 if (m_dirty
) return;
2596 wxClientDC
dc(this);
2601 GetClientSize( &cw
, &ch
);
2604 rect
.x
= dc
.LogicalToDeviceX( 0 );
2606 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2609 Refresh( TRUE
, &rect
);
2611 AdjustMyScrollbars();
2614 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2616 if (m_dirty
) return;
2618 wxClientDC
dc(this);
2623 GetClientSize( &cw
, &ch
);
2626 rect
.x
= dc
.LogicalToDeviceX( 0 );
2627 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2629 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2631 Refresh( TRUE
, &rect
);