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_EXPORTED_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 OnKeyUp( wxKeyEvent
&event
);
93 void OnKillFocus( wxFocusEvent
&event
);
98 wxGenericTreeCtrl
*m_owner
;
99 wxString m_startValue
;
101 DECLARE_EVENT_TABLE()
102 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
106 class WXDLLEXPORT wxGenericTreeItem
110 wxGenericTreeItem() { m_data
= NULL
; }
111 wxGenericTreeItem( wxGenericTreeItem
*parent
,
112 const wxString
& text
,
114 int image
, int selImage
,
115 wxTreeItemData
*data
);
117 ~wxGenericTreeItem();
120 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
122 const wxString
& GetText() const { return m_text
; }
123 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
124 { return m_images
[which
]; }
125 wxTreeItemData
*GetData() const { return m_data
; }
127 // returns the current image for the item (depending on its
128 // selected/expanded/whatever state)
129 int GetCurrentImage() const;
131 void SetText( const wxString
&text
);
132 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
133 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
135 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
137 void SetBold(bool bold
) { m_isBold
= bold
; }
139 int GetX() const { return m_x
; }
140 int GetY() const { return m_y
; }
142 void SetX(int x
) { m_x
= x
; }
143 void SetY(int y
) { m_y
= y
; }
145 int GetHeight() const { return m_height
; }
146 int GetWidth() const { return m_width
; }
148 void SetHeight(int h
) { m_height
= h
; }
149 void SetWidth(int w
) { m_width
= w
; }
152 wxGenericTreeItem
*GetParent() const { return m_parent
; }
155 // deletes all children notifying the treectrl about it if !NULL
157 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
158 // FIXME don't know what is it for
161 // get count of all children (and grand children if 'recursively')
162 size_t GetChildrenCount(bool recursively
= TRUE
) const;
164 void Insert(wxGenericTreeItem
*child
, size_t index
)
165 { m_children
.Insert(child
, index
); }
167 void SetCross( int x
, int y
);
168 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
170 // return the item at given position (or NULL if no item), onButton is
171 // TRUE if the point belongs to the item's button, otherwise it lies
172 // on the button's label
173 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxGenericTreeCtrl
*, int &flags
);
175 void Expand() { m_isCollapsed
= FALSE
; }
176 void Collapse() { m_isCollapsed
= TRUE
; }
178 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
181 bool HasChildren() const { return !m_children
.IsEmpty(); }
182 bool IsSelected() const { return m_hasHilight
!= 0; }
183 bool IsExpanded() const { return !m_isCollapsed
; }
184 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
185 bool IsBold() const { return m_isBold
!= 0; }
188 // get them - may be NULL
189 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
190 // get them ensuring that the pointer is not NULL
191 wxTreeItemAttr
& Attr()
194 m_attr
= new wxTreeItemAttr
;
202 // tree ctrl images for the normal, selected, expanded and
203 // expanded+selected states
204 int m_images
[wxTreeItemIcon_Max
];
206 wxTreeItemData
*m_data
;
208 // use bitfields to save size
209 int m_isCollapsed
:1;
210 int m_hasHilight
:1; // same as focused
211 int m_hasPlus
:1; // used for item which doesn't have
212 // children but has a [+] button
213 int m_isBold
:1; // render the label in bold font
216 wxCoord m_height
, m_width
;
217 int m_xCross
, m_yCross
;
220 wxArrayGenericTreeItems m_children
;
221 wxGenericTreeItem
*m_parent
;
223 wxTreeItemAttr
*m_attr
;
226 // =============================================================================
228 // =============================================================================
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 // translate the key or mouse event flags to the type of selection we're
236 static void EventFlagsToSelType(long style
,
240 bool &extended_select
,
241 bool &unselect_others
)
243 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
244 extended_select
= shiftDown
&& is_multiple
;
245 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
248 // -----------------------------------------------------------------------------
249 // wxTreeRenameTimer (internal)
250 // -----------------------------------------------------------------------------
252 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
257 void wxTreeRenameTimer::Notify()
259 m_owner
->OnRenameTimer();
262 //-----------------------------------------------------------------------------
263 // wxTreeTextCtrl (internal)
264 //-----------------------------------------------------------------------------
266 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
268 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
269 EVT_CHAR (wxTreeTextCtrl::OnChar
)
270 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
271 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
274 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
278 wxGenericTreeCtrl
*owner
,
279 const wxString
&value
,
283 const wxValidator
& validator
,
284 const wxString
&name
)
285 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
291 (*m_res
) = wxEmptyString
;
292 m_startValue
= value
;
295 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
297 if (event
.m_keyCode
== WXK_RETURN
)
300 (*m_res
) = GetValue();
302 if (!wxPendingDelete
.Member(this))
303 wxPendingDelete
.Append(this);
305 if ((*m_accept
) && ((*m_res
) != m_startValue
))
306 m_owner
->OnRenameAccept();
310 if (event
.m_keyCode
== WXK_ESCAPE
)
315 if (!wxPendingDelete
.Member(this))
316 wxPendingDelete
.Append(this);
323 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
325 // auto-grow the textctrl:
326 wxSize parentSize
= m_owner
->GetSize();
327 wxPoint myPos
= GetPosition();
328 wxSize mySize
= GetSize();
330 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
331 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
332 if (mySize
.x
> sx
) sx
= mySize
.x
;
338 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
340 if (!wxPendingDelete
.Member(this))
341 wxPendingDelete
.Append(this);
343 if ((*m_accept
) && ((*m_res
) != m_startValue
))
344 m_owner
->OnRenameAccept();
348 // -----------------------------------------------------------------------------
350 // -----------------------------------------------------------------------------
352 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
354 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
355 : wxNotifyEvent( commandType
, id
)
358 m_itemOld
= (wxGenericTreeItem
*)NULL
;
362 // -----------------------------------------------------------------------------
364 // -----------------------------------------------------------------------------
366 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
367 const wxString
& text
,
369 int image
, int selImage
,
370 wxTreeItemData
*data
)
373 m_images
[wxTreeItemIcon_Normal
] = image
;
374 m_images
[wxTreeItemIcon_Selected
] = selImage
;
375 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
376 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
380 m_xCross
= m_yCross
= 0;
384 m_isCollapsed
= TRUE
;
385 m_hasHilight
= FALSE
;
391 m_attr
= (wxTreeItemAttr
*)NULL
;
393 // We don't know the height here yet.
398 wxGenericTreeItem::~wxGenericTreeItem()
404 wxASSERT_MSG( m_children
.IsEmpty(),
405 wxT("please call DeleteChildren() before deleting the item") );
408 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
410 size_t count
= m_children
.Count();
411 for ( size_t n
= 0; n
< count
; n
++ )
413 wxGenericTreeItem
*child
= m_children
[n
];
415 tree
->SendDeleteEvent(child
);
417 child
->DeleteChildren(tree
);
424 void wxGenericTreeItem::SetText( const wxString
&text
)
429 void wxGenericTreeItem::Reset()
432 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
434 m_images
[i
] = NO_IMAGE
;
439 m_height
= m_width
= 0;
446 m_isCollapsed
= TRUE
;
448 m_parent
= (wxGenericTreeItem
*)NULL
;
451 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
453 size_t count
= m_children
.Count();
457 size_t total
= count
;
458 for (size_t n
= 0; n
< count
; ++n
)
460 total
+= m_children
[n
]->GetChildrenCount();
466 void wxGenericTreeItem::SetCross( int x
, int y
)
472 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxGenericTreeCtrl
*theTree
)
474 int bottomY
=m_y
+theTree
->GetLineHeight(this);
475 if ( y
< bottomY
) y
= bottomY
;
476 int width
= m_x
+ m_width
;
477 if ( x
< width
) x
= width
;
481 size_t count
= m_children
.Count();
482 for ( size_t n
= 0; n
< count
; ++n
)
484 m_children
[n
]->GetSize( x
, y
, theTree
);
489 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
490 const wxGenericTreeCtrl
*theTree
,
493 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
495 if (point
.y
< m_y
+theTree
->GetLineHeight(this)/2 )
496 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
498 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
500 // 5 is the size of the plus sign
501 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
502 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
503 (IsExpanded() || HasPlus()))
505 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
509 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
514 // assuming every image (normal and selected ) has the same size !
515 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
516 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
518 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
519 flags
|= wxTREE_HITTEST_ONITEMICON
;
521 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
527 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
528 if (point
.x
> m_x
+m_width
)
529 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
537 size_t count
= m_children
.Count();
538 for ( size_t n
= 0; n
< count
; n
++ )
540 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
547 flags
|=wxTREE_HITTEST_NOWHERE
;
549 return (wxGenericTreeItem
*) NULL
;
552 int wxGenericTreeItem::GetCurrentImage() const
554 int image
= NO_IMAGE
;
559 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
562 if ( image
== NO_IMAGE
)
564 // we usually fall back to the normal item, but try just the
565 // expanded one (and not selected) first in this case
566 image
= GetImage(wxTreeItemIcon_Expanded
);
572 image
= GetImage(wxTreeItemIcon_Selected
);
575 // may be it doesn't have the specific image we want, try the default one
577 if ( image
== NO_IMAGE
)
585 // -----------------------------------------------------------------------------
586 // wxGenericTreeCtrl implementation
587 // -----------------------------------------------------------------------------
589 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
591 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
592 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
593 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
594 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
595 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
596 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
597 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
600 #if !defined(__WXMSW__) || defined(__WIN16__)
602 * wxTreeCtrl has to be a real class or we have problems with
603 * the run-time information.
606 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
609 // -----------------------------------------------------------------------------
610 // construction/destruction
611 // -----------------------------------------------------------------------------
613 void wxGenericTreeCtrl::Init()
617 m_anchor
= (wxGenericTreeItem
*) NULL
;
627 m_hilightBrush
= new wxBrush
629 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
634 m_imageListState
= (wxImageList
*) NULL
;
635 m_ownsImageListNormal
=
636 m_ownsImageListState
= FALSE
;
639 m_isDragging
= FALSE
;
641 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
643 m_renameTimer
= new wxTreeRenameTimer( this );
644 m_lastOnSame
= FALSE
;
646 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
647 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
648 m_normalFont
.GetFamily(),
649 m_normalFont
.GetStyle(),
651 m_normalFont
.GetUnderlined());
654 bool wxGenericTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
655 const wxPoint
& pos
, const wxSize
& size
,
657 const wxValidator
&validator
,
658 const wxString
& name
)
660 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
663 SetValidator( validator
);
666 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
667 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
668 m_dottedPen
= wxPen( "grey", 0, 0 );
673 wxGenericTreeCtrl::~wxGenericTreeCtrl()
675 wxDELETE( m_hilightBrush
);
679 delete m_renameTimer
;
680 if (m_ownsImageListNormal
) delete m_imageListNormal
;
681 if (m_ownsImageListState
) delete m_imageListState
;
684 // -----------------------------------------------------------------------------
686 // -----------------------------------------------------------------------------
688 size_t wxGenericTreeCtrl::GetCount() const
690 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
693 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
699 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
705 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
707 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
709 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
712 // -----------------------------------------------------------------------------
713 // functions to work with tree items
714 // -----------------------------------------------------------------------------
716 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
718 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
720 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
723 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
724 wxTreeItemIcon which
) const
726 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
728 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
731 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
733 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
735 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
738 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
740 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
743 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
744 pItem
->SetText(text
);
745 CalculateSize(pItem
, dc
);
749 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
751 wxTreeItemIcon which
)
753 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
755 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
756 pItem
->SetImage(image
, which
);
759 CalculateSize(pItem
, dc
);
763 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
765 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
767 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
770 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
772 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
774 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
775 pItem
->SetHasPlus(has
);
779 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
781 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
783 // avoid redrawing the tree if no real change
784 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
785 if ( pItem
->IsBold() != bold
)
787 pItem
->SetBold(bold
);
792 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
795 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
797 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
798 pItem
->Attr().SetTextColour(col
);
802 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
805 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
807 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
808 pItem
->Attr().SetBackgroundColour(col
);
812 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
814 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
816 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
817 pItem
->Attr().SetFont(font
);
821 // -----------------------------------------------------------------------------
822 // item status inquiries
823 // -----------------------------------------------------------------------------
825 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
827 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
829 // An item is only visible if it's not a descendant of a collapsed item
830 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
831 wxGenericTreeItem
* parent
= pItem
->GetParent();
834 if (!parent
->IsExpanded())
836 parent
= parent
->GetParent();
840 GetViewStart(& startX
, & startY
);
842 wxSize clientSize
= GetClientSize();
845 if (!GetBoundingRect(item
, rect
))
847 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
849 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
851 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
857 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
859 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
861 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
864 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
866 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
868 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
871 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
873 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
875 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
878 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
880 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
882 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
885 // -----------------------------------------------------------------------------
887 // -----------------------------------------------------------------------------
889 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
891 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
893 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
896 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
898 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
901 return GetNextChild(item
, cookie
);
904 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
906 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
908 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
909 if ( (size_t)cookie
< children
.Count() )
911 return children
.Item((size_t)cookie
++);
915 // there are no more of them
916 return wxTreeItemId();
920 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
922 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
924 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
925 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
928 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
930 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
932 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
933 wxGenericTreeItem
*parent
= i
->GetParent();
934 if ( parent
== NULL
)
936 // root item doesn't have any siblings
937 return wxTreeItemId();
940 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
941 int index
= siblings
.Index(i
);
942 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
944 size_t n
= (size_t)(index
+ 1);
945 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
948 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
950 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
952 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
953 wxGenericTreeItem
*parent
= i
->GetParent();
954 if ( parent
== NULL
)
956 // root item doesn't have any siblings
957 return wxTreeItemId();
960 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
961 int index
= siblings
.Index(i
);
962 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
964 return index
== 0 ? wxTreeItemId()
965 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
968 // Only for internal use right now, but should probably be public
969 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
971 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
973 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
975 // First see if there are any children.
976 wxArrayGenericTreeItems
& children
= i
->GetChildren();
977 if (children
.GetCount() > 0)
979 return children
.Item(0);
983 // Try a sibling of this or ancestor instead
984 wxTreeItemId p
= item
;
988 toFind
= GetNextSibling(p
);
990 } while (p
.IsOk() && !toFind
.IsOk());
995 wxTreeItemId
wxGenericTreeCtrl::GetPrev(const wxTreeItemId
& item
) const
997 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
999 wxFAIL_MSG(wxT("not implemented"));
1001 return wxTreeItemId();
1004 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1006 wxTreeItemId id
= GetRootItem();
1015 } while (id
.IsOk());
1017 return wxTreeItemId();
1020 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1022 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1024 wxTreeItemId id
= item
;
1029 if (id
.IsOk() && IsVisible(id
))
1032 return wxTreeItemId();
1035 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1037 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1039 wxFAIL_MSG(wxT("not implemented"));
1041 return wxTreeItemId();
1044 // -----------------------------------------------------------------------------
1046 // -----------------------------------------------------------------------------
1048 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1050 const wxString
& text
,
1051 int image
, int selImage
,
1052 wxTreeItemData
*data
)
1054 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1057 // should we give a warning here?
1058 return AddRoot(text
, image
, selImage
, data
);
1061 wxClientDC
dc(this);
1062 wxGenericTreeItem
*item
=
1063 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1067 data
->m_pItem
= (long) item
;
1070 parent
->Insert( item
, previous
);
1077 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1078 int image
, int selImage
,
1079 wxTreeItemData
*data
)
1081 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1083 wxClientDC
dc(this);
1084 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1085 image
, selImage
, data
);
1088 data
->m_pItem
= (long) m_anchor
;
1091 if (!HasFlag(wxTR_MULTIPLE
))
1093 m_current
= m_key_current
= m_anchor
;
1094 m_current
->SetHilight( TRUE
);
1102 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1103 const wxString
& text
,
1104 int image
, int selImage
,
1105 wxTreeItemData
*data
)
1107 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1110 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1111 const wxTreeItemId
& idPrevious
,
1112 const wxString
& text
,
1113 int image
, int selImage
,
1114 wxTreeItemData
*data
)
1116 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1119 // should we give a warning here?
1120 return AddRoot(text
, image
, selImage
, data
);
1123 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1124 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1125 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1127 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1130 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1132 const wxString
& text
,
1133 int image
, int selImage
,
1134 wxTreeItemData
*data
)
1136 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1139 // should we give a warning here?
1140 return AddRoot(text
, image
, selImage
, data
);
1143 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1146 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1147 const wxString
& text
,
1148 int image
, int selImage
,
1149 wxTreeItemData
*data
)
1151 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1154 // should we give a warning here?
1155 return AddRoot(text
, image
, selImage
, data
);
1158 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1159 image
, selImage
, data
);
1162 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1164 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1165 event
.m_item
= (long) item
;
1166 event
.SetEventObject( this );
1167 ProcessEvent( event
);
1170 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1172 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1173 item
->DeleteChildren(this);
1178 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1180 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1182 // don't stay with invalid m_key_current or we will crash in the next call
1184 bool changeKeyCurrent
= FALSE
;
1185 wxGenericTreeItem
*itemKey
= m_key_current
;
1186 while ( itemKey
&& !changeKeyCurrent
)
1188 if ( itemKey
== item
)
1190 // m_key_current is a descendant of the item being deleted
1191 changeKeyCurrent
= TRUE
;
1195 itemKey
= itemKey
->GetParent();
1199 wxGenericTreeItem
*parent
= item
->GetParent();
1202 parent
->GetChildren().Remove( item
); // remove by value
1205 if ( changeKeyCurrent
)
1207 // may be NULL or not
1208 m_key_current
= parent
;
1211 item
->DeleteChildren(this);
1212 SendDeleteEvent(item
);
1218 void wxGenericTreeCtrl::DeleteAllItems()
1222 m_anchor
->DeleteChildren(this);
1231 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1233 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1235 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1237 if ( !item
->HasPlus() )
1240 if ( item
->IsExpanded() )
1243 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1244 event
.m_item
= (long) item
;
1245 event
.SetEventObject( this );
1247 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1249 // cancelled by program
1254 CalculatePositions();
1256 RefreshSubtree(item
);
1258 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1259 ProcessEvent( event
);
1262 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1265 if ( IsExpanded(item
) )
1268 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1269 while ( child
.IsOk() )
1273 child
= GetNextChild(item
, cookie
);
1278 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1280 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1282 if ( !item
->IsExpanded() )
1285 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1286 event
.m_item
= (long) item
;
1287 event
.SetEventObject( this );
1288 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1290 // cancelled by program
1296 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1297 size_t count
= children
.Count();
1298 for ( size_t n
= 0; n
< count
; n
++ )
1300 Collapse(children
[n
]);
1303 CalculatePositions();
1305 RefreshSubtree(item
);
1307 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1308 ProcessEvent( event
);
1311 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1314 DeleteChildren(item
);
1317 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1319 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1321 if (item
->IsExpanded())
1327 void wxGenericTreeCtrl::Unselect()
1331 m_current
->SetHilight( FALSE
);
1332 RefreshLine( m_current
);
1336 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1338 if (item
->IsSelected())
1340 item
->SetHilight(FALSE
);
1344 if (item
->HasChildren())
1346 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1347 size_t count
= children
.Count();
1348 for ( size_t n
= 0; n
< count
; ++n
)
1350 UnselectAllChildren(children
[n
]);
1355 void wxGenericTreeCtrl::UnselectAll()
1357 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1360 // Recursive function !
1361 // To stop we must have crt_item<last_item
1363 // Tag all next children, when no more children,
1364 // Move to parent (not to tag)
1365 // Keep going... if we found last_item, we stop.
1366 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1368 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1370 if (parent
== NULL
) // This is root item
1371 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1373 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1374 int index
= children
.Index(crt_item
);
1375 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1377 size_t count
= children
.Count();
1378 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1380 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1383 return TagNextChildren(parent
, last_item
, select
);
1386 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1388 crt_item
->SetHilight(select
);
1389 RefreshLine(crt_item
);
1391 if (crt_item
==last_item
)
1394 if (crt_item
->HasChildren())
1396 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1397 size_t count
= children
.Count();
1398 for ( size_t n
= 0; n
< count
; ++n
)
1400 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1408 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1410 // item2 is not necessary after item1
1411 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1413 // choice first' and 'last' between item1 and item2
1414 if (item1
->GetY()<item2
->GetY())
1425 bool select
= m_current
->IsSelected();
1427 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1430 TagNextChildren(first
,last
,select
);
1433 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1434 bool unselect_others
,
1435 bool extended_select
)
1437 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1439 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1440 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1442 //wxCHECK_RET( ( (!unselect_others) && is_single),
1443 // wxT("this is a single selection tree") );
1445 // to keep going anyhow !!!
1448 if (item
->IsSelected())
1449 return; // nothing to do
1450 unselect_others
= TRUE
;
1451 extended_select
= FALSE
;
1453 else if ( unselect_others
&& item
->IsSelected() )
1455 // selection change if there is more than one item currently selected
1456 wxArrayTreeItemIds selected_items
;
1457 if ( GetSelections(selected_items
) == 1 )
1461 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1462 event
.m_item
= (long) item
;
1463 event
.m_itemOld
= (long) m_current
;
1464 event
.SetEventObject( this );
1465 // TODO : Here we don't send any selection mode yet !
1467 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1471 if (unselect_others
)
1473 if (is_single
) Unselect(); // to speed up thing
1478 if (extended_select
)
1483 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1486 // don't change the mark (m_current)
1487 SelectItemRange(m_current
, item
);
1491 bool select
=TRUE
; // the default
1493 // Check if we need to toggle hilight (ctrl mode)
1494 if (!unselect_others
)
1495 select
=!item
->IsSelected();
1497 m_current
= m_key_current
= item
;
1498 m_current
->SetHilight(select
);
1499 RefreshLine( m_current
);
1502 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1503 GetEventHandler()->ProcessEvent( event
);
1506 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1507 wxArrayTreeItemIds
&array
) const
1509 if ( item
->IsSelected() )
1510 array
.Add(wxTreeItemId(item
));
1512 if ( item
->HasChildren() )
1514 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1515 size_t count
= children
.GetCount();
1516 for ( size_t n
= 0; n
< count
; ++n
)
1517 FillArray(children
[n
], array
);
1521 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1524 wxTreeItemId idRoot
= GetRootItem();
1525 if ( idRoot
.IsOk() )
1527 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1529 //else: the tree is empty, so no selections
1531 return array
.Count();
1534 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1536 if (!item
.IsOk()) return;
1538 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1540 // first expand all parent branches
1541 wxGenericTreeItem
*parent
= gitem
->GetParent();
1545 parent
= parent
->GetParent();
1548 //if (parent) CalculatePositions();
1553 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1555 if (!item
.IsOk()) return;
1557 // We have to call this here because the label in
1558 // question might just have been added and no screen
1559 // update taken place.
1560 if (m_dirty
) wxYieldIfNeeded();
1562 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1564 // now scroll to the item
1565 int item_y
= gitem
->GetY();
1569 ViewStart( &start_x
, &start_y
);
1570 start_y
*= PIXELS_PER_UNIT
;
1574 GetClientSize( &client_w
, &client_h
);
1576 if (item_y
< start_y
+3)
1581 m_anchor
->GetSize( x
, y
, this );
1582 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1583 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1584 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1585 // Item should appear at top
1586 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1588 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1593 m_anchor
->GetSize( x
, y
, this );
1594 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1595 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1596 item_y
+= PIXELS_PER_UNIT
+2;
1597 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1598 // Item should appear at bottom
1599 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
);
1603 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1604 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1606 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1607 wxGenericTreeItem
**item2
)
1609 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1611 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1614 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1615 const wxTreeItemId
& item2
)
1617 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1620 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1622 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1624 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1626 wxCHECK_RET( !s_treeBeingSorted
,
1627 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1629 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1630 if ( children
.Count() > 1 )
1632 s_treeBeingSorted
= this;
1633 children
.Sort(tree_ctrl_compare_func
);
1634 s_treeBeingSorted
= NULL
;
1638 //else: don't make the tree dirty as nothing changed
1641 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1643 return m_imageListNormal
;
1646 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1648 return m_imageListState
;
1651 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1653 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1655 m_imageListNormal
= imageList
;
1656 m_ownsImageListNormal
= FALSE
;
1658 if ( !m_imageListNormal
)
1661 // Calculate a m_lineHeight value from the image sizes.
1662 // May be toggle off. Then wxGenericTreeCtrl will spread when
1663 // necessary (which might look ugly).
1664 wxClientDC
dc(this);
1665 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1666 int width
= 0, height
= 0,
1667 n
= m_imageListNormal
->GetImageCount();
1669 for (int i
= 0; i
< n
; i
++)
1671 m_imageListNormal
->GetSize(i
, width
, height
);
1672 if (height
> m_lineHeight
) m_lineHeight
= height
;
1675 if (m_lineHeight
< 40)
1676 m_lineHeight
+= 2; // at least 2 pixels
1678 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1681 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1683 if (m_ownsImageListState
) delete m_imageListState
;
1684 m_imageListState
= imageList
;
1685 m_ownsImageListState
= FALSE
;
1688 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1690 SetImageList(imageList
);
1691 m_ownsImageListNormal
= TRUE
;
1694 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1696 SetStateImageList(imageList
);
1697 m_ownsImageListState
= TRUE
;
1700 // -----------------------------------------------------------------------------
1702 // -----------------------------------------------------------------------------
1704 void wxGenericTreeCtrl::AdjustMyScrollbars()
1710 m_anchor
->GetSize( x
, y
, this );
1711 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1712 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1713 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1714 int y_pos
= GetScrollPos( wxVERTICAL
);
1715 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1719 SetScrollbars( 0, 0, 0, 0 );
1723 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1725 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1726 return item
->GetHeight();
1728 return m_lineHeight
;
1731 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1733 wxTreeItemAttr
*attr
= item
->GetAttributes();
1734 if ( attr
&& attr
->HasFont() )
1735 dc
.SetFont(attr
->GetFont());
1736 else if (item
->IsBold())
1737 dc
.SetFont(m_boldFont
);
1741 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1745 int image
= item
->GetCurrentImage();
1746 if ( image
!= NO_IMAGE
)
1748 if ( m_imageListNormal
)
1750 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1759 int total_h
= GetLineHeight(item
);
1761 if (item
->IsSelected())
1762 dc
.SetBrush(*m_hilightBrush
);
1766 if ( attr
&& attr
->HasBackgroundColour() )
1767 colBg
= attr
->GetBackgroundColour();
1769 colBg
= m_backgroundColour
;
1770 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1773 if (item
->IsSelected() && image
!= NO_IMAGE
)
1775 // If it's selected, and there's an image, then we should
1776 // take care to leave the area under the image painted in the
1777 // background colour.
1778 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY(), item
->GetWidth() - image_w
+ 2, total_h
);
1781 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1783 if ( image
!= NO_IMAGE
)
1785 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1786 m_imageListNormal
->Draw( image
, dc
,
1788 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1789 wxIMAGELIST_DRAW_TRANSPARENT
);
1790 dc
.DestroyClippingRegion();
1793 dc
.SetBackgroundMode(wxTRANSPARENT
);
1794 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1795 dc
.DrawText( item
->GetText(),
1796 (wxCoord
)(image_w
+ item
->GetX()),
1797 (wxCoord
)(item
->GetY() + extraH
));
1799 // restore normal font
1800 dc
.SetFont( m_normalFont
);
1803 // Now y stands for the top of the item, whereas it used to stand for middle !
1804 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1806 int horizX
= level
*m_indent
;
1808 item
->SetX( horizX
+m_indent
+m_spacing
);
1812 y
+=GetLineHeight(item
)/2;
1814 item
->SetCross( horizX
+m_indent
, y
);
1816 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1817 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1819 bool drawLines
= ((GetWindowStyle() & wxTR_NO_LINES
) == 0);
1821 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1823 int startX
= horizX
;
1824 int endX
= horizX
+ (m_indent
-5);
1826 // if (!item->HasChildren()) endX += (m_indent+5);
1827 if (!item
->HasChildren()) endX
+= 20;
1830 dc
.DrawLine( startX
, y
, endX
, y
);
1832 if (item
->HasPlus())
1835 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1836 dc
.SetPen( *wxGREY_PEN
);
1837 dc
.SetBrush( *wxWHITE_BRUSH
);
1838 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1840 dc
.SetPen( *wxBLACK_PEN
);
1841 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1842 if (!item
->IsExpanded())
1843 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1845 dc
.SetPen( m_dottedPen
);
1848 wxPen
*pen
= wxTRANSPARENT_PEN
;
1851 if ( item
->IsSelected() )
1853 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1861 wxTreeItemAttr
*attr
= item
->GetAttributes();
1862 if ( attr
&& attr
->HasTextColour() )
1863 colText
= attr
->GetTextColour();
1865 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
);
1869 dc
.SetTextForeground(colText
);
1873 PaintItem(item
, dc
);
1875 // restore DC objects
1876 dc
.SetBrush( *wxWHITE_BRUSH
);
1877 dc
.SetPen( m_dottedPen
);
1878 dc
.SetTextForeground( *wxBLACK
);
1881 y
= oldY
+GetLineHeight(item
);
1883 if (item
->IsExpanded())
1885 oldY
+=GetLineHeight(item
)/2;
1888 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1889 size_t n
, count
= children
.Count();
1890 for ( n
= 0; n
< count
; ++n
)
1893 PaintLevel( children
[n
], dc
, level
+1, y
);
1896 // it may happen that the item is expanded but has no items (when you
1897 // delete all its children for example) - don't draw the vertical line
1901 semiOldY
+=GetLineHeight(children
[--n
])/2;
1903 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1908 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1912 if ( item
->HasPlus() )
1914 // it's a folder, indicate it by a border
1919 // draw a line under the drop target because the item will be
1921 DrawLine(item
, TRUE
/* below */);
1924 SetCursor(wxCURSOR_BULLSEYE
);
1929 SetCursor(wxCURSOR_NO_ENTRY
);
1933 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
1935 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1937 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1939 wxClientDC
dc(this);
1941 dc
.SetLogicalFunction(wxINVERT
);
1942 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1944 int w
= i
->GetWidth() + 2;
1945 int h
= GetLineHeight(i
) + 2;
1947 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
1950 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
1952 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1954 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1956 wxClientDC
dc(this);
1958 dc
.SetLogicalFunction(wxINVERT
);
1964 y
+= GetLineHeight(i
) - 1;
1967 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
1970 // -----------------------------------------------------------------------------
1971 // wxWindows callbacks
1972 // -----------------------------------------------------------------------------
1974 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1982 dc
.SetFont( m_normalFont
);
1983 dc
.SetPen( m_dottedPen
);
1985 // this is now done dynamically
1986 //if(GetImageList() == NULL)
1987 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1990 PaintLevel( m_anchor
, dc
, 0, y
);
1993 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1997 if (m_current
) RefreshLine( m_current
);
2000 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2004 if (m_current
) RefreshLine( m_current
);
2007 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2009 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2010 te
.m_code
= (int)event
.KeyCode();
2011 te
.SetEventObject( this );
2012 GetEventHandler()->ProcessEvent( te
);
2014 if ( (m_current
== 0) || (m_key_current
== 0) )
2020 // how should the selection work for this event?
2021 bool is_multiple
, extended_select
, unselect_others
;
2022 EventFlagsToSelType(GetWindowStyleFlag(),
2024 event
.ControlDown(),
2025 is_multiple
, extended_select
, unselect_others
);
2029 // * : Expand all/Collapse all
2030 // ' ' | return : activate
2031 // up : go up (not last children!)
2033 // left : go to parent
2034 // right : open if parent and go next
2035 // home : go to root
2036 // end : go to last item without opening parents
2037 switch (event
.KeyCode())
2041 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2049 if ( !IsExpanded(m_current
) )
2052 ExpandAll(m_current
);
2055 //else: fall through to Collapse() it
2059 if (IsExpanded(m_current
))
2061 Collapse(m_current
);
2068 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2069 event
.m_item
= (long) m_current
;
2071 event
.SetEventObject( this );
2072 GetEventHandler()->ProcessEvent( event
);
2076 // up goes to the previous sibling or to the last of its children if
2080 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2083 prev
= GetParent( m_key_current
);
2087 wxTreeItemId current
= m_key_current
;
2088 if (current
== GetFirstChild( prev
, cockie
))
2090 // otherwise we return to where we came from
2091 SelectItem( prev
, unselect_others
, extended_select
);
2092 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2093 EnsureVisible( prev
);
2100 while ( IsExpanded(prev
) && HasChildren(prev
) )
2102 wxTreeItemId child
= GetLastChild(prev
);
2109 SelectItem( prev
, unselect_others
, extended_select
);
2110 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2111 EnsureVisible( prev
);
2116 // left arrow goes to the parent
2119 wxTreeItemId prev
= GetParent( m_current
);
2122 EnsureVisible( prev
);
2123 SelectItem( prev
, unselect_others
, extended_select
);
2129 // this works the same as the down arrow except that we also expand the
2130 // item if it wasn't expanded yet
2136 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2139 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2140 SelectItem( child
, unselect_others
, extended_select
);
2141 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2142 EnsureVisible( child
);
2146 wxTreeItemId next
= GetNextSibling( m_key_current
);
2149 wxTreeItemId current
= m_key_current
;
2150 while (current
&& !next
)
2152 current
= GetParent( current
);
2153 if (current
) next
= GetNextSibling( current
);
2158 SelectItem( next
, unselect_others
, extended_select
);
2159 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2160 EnsureVisible( next
);
2166 // <End> selects the last visible tree item
2169 wxTreeItemId last
= GetRootItem();
2171 while ( last
.IsOk() && IsExpanded(last
) )
2173 wxTreeItemId lastChild
= GetLastChild(last
);
2175 // it may happen if the item was expanded but then all of
2176 // its children have been deleted - so IsExpanded() returned
2177 // TRUE, but GetLastChild() returned invalid item
2186 EnsureVisible( last
);
2187 SelectItem( last
, unselect_others
, extended_select
);
2192 // <Home> selects the root item
2195 wxTreeItemId prev
= GetRootItem();
2198 EnsureVisible( prev
);
2199 SelectItem( prev
, unselect_others
, extended_select
);
2209 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2211 // We have to call this here because the label in
2212 // question might just have been added and no screen
2213 // update taken place.
2214 if (m_dirty
) wxYieldIfNeeded();
2216 wxClientDC
dc(this);
2218 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2219 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2224 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2225 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2226 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2227 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2230 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2232 return wxTreeItemId();
2235 // get the bounding rectangle of the item (or of its label only)
2236 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2238 bool textOnly
) const
2240 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2242 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2245 GetViewStart(& startX
, & startY
);
2247 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2248 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2249 rect
.width
= i
->GetWidth();
2250 //rect.height = i->GetHeight();
2251 rect
.height
= GetLineHeight(i
);
2258 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2260 if (!item
.IsOk()) return;
2262 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2264 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2265 te
.m_item
= (long) m_currentEdit
;
2266 te
.SetEventObject( this );
2267 GetEventHandler()->ProcessEvent( te
);
2269 if (!te
.IsAllowed()) return;
2271 // We have to call this here because the label in
2272 // question might just have been added and no screen
2273 // update taken place.
2274 if (m_dirty
) wxYieldIfNeeded();
2276 wxString s
= m_currentEdit
->GetText();
2277 int x
= m_currentEdit
->GetX();
2278 int y
= m_currentEdit
->GetY();
2279 int w
= m_currentEdit
->GetWidth();
2280 int h
= m_currentEdit
->GetHeight();
2285 int image
= m_currentEdit
->GetCurrentImage();
2286 if ( image
!= NO_IMAGE
)
2288 if ( m_imageListNormal
)
2290 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2295 wxFAIL_MSG(_T("you must create an image list to use images!"));
2299 w
-= image_w
+ 4; // I don't know why +4 is needed
2301 wxClientDC
dc(this);
2303 x
= dc
.LogicalToDeviceX( x
);
2304 y
= dc
.LogicalToDeviceY( y
);
2306 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2316 void wxGenericTreeCtrl::OnRenameTimer()
2321 void wxGenericTreeCtrl::OnRenameAccept()
2323 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2324 le
.m_item
= (long) m_currentEdit
;
2325 le
.SetEventObject( this );
2326 le
.m_label
= m_renameRes
;
2327 GetEventHandler()->ProcessEvent( le
);
2329 if (!le
.IsAllowed()) return;
2331 SetItemText( m_currentEdit
, m_renameRes
);
2334 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2336 if ( !m_anchor
) return;
2338 // we process left mouse up event (enables in-place edit), right down
2339 // (pass to the user code), left dbl click (activate item) and
2340 // dragging/moving events for items drag-and-drop
2341 if ( !(event
.LeftDown() ||
2343 event
.RightDown() ||
2344 event
.LeftDClick() ||
2346 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2353 wxClientDC
dc(this);
2355 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2356 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2359 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2361 if ( event
.Dragging() && !m_isDragging
)
2363 if (m_dragCount
== 0)
2364 m_dragStart
= wxPoint(x
,y
);
2368 if (m_dragCount
!= 3)
2370 // wait until user drags a bit further...
2374 wxEventType command
= event
.RightIsDown()
2375 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2376 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2378 wxTreeEvent
nevent( command
, GetId() );
2379 nevent
.m_item
= (long) m_current
;
2380 nevent
.SetEventObject(this);
2382 // by default the dragging is not supported, the user code must
2383 // explicitly allow the event for it to take place
2386 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2388 // we're going to drag this item
2389 m_isDragging
= TRUE
;
2391 // remember the old cursor because we will change it while
2393 m_oldCursor
= m_cursor
;
2395 // in a single selection control, hide the selection temporarily
2396 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2398 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2400 if ( m_oldSelection
)
2402 m_oldSelection
->SetHilight(FALSE
);
2403 RefreshLine(m_oldSelection
);
2410 else if ( event
.Moving() )
2412 if ( item
!= m_dropTarget
)
2414 // unhighlight the previous drop target
2415 DrawDropEffect(m_dropTarget
);
2417 m_dropTarget
= item
;
2419 // highlight the current drop target if any
2420 DrawDropEffect(m_dropTarget
);
2425 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2427 // erase the highlighting
2428 DrawDropEffect(m_dropTarget
);
2430 // generate the drag end event
2431 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2433 event
.m_item
= (long) item
;
2434 event
.m_pointDrag
= wxPoint(x
, y
);
2435 event
.SetEventObject(this);
2437 (void)GetEventHandler()->ProcessEvent(event
);
2439 m_isDragging
= FALSE
;
2440 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2442 if ( m_oldSelection
)
2444 m_oldSelection
->SetHilight(TRUE
);
2445 RefreshLine(m_oldSelection
);
2446 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2451 SetCursor(m_oldCursor
);
2457 // here we process only the messages which happen on tree items
2461 if (item
== NULL
) return; /* we hit the blank area */
2463 if ( event
.RightDown() )
2465 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2466 nevent
.m_item
= (long) item
;
2468 CalcScrolledPosition(x
, y
,
2469 &nevent
.m_pointDrag
.x
,
2470 &nevent
.m_pointDrag
.y
);
2471 nevent
.SetEventObject(this);
2472 GetEventHandler()->ProcessEvent(nevent
);
2474 else if ( event
.LeftUp() )
2478 if ( (item
== m_current
) &&
2479 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2480 HasFlag(wxTR_EDIT_LABELS
) )
2482 if ( m_renameTimer
->IsRunning() )
2483 m_renameTimer
->Stop();
2485 m_renameTimer
->Start( 100, TRUE
);
2488 m_lastOnSame
= FALSE
;
2491 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2493 if ( event
.LeftDown() )
2495 m_lastOnSame
= item
== m_current
;
2498 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2500 // only toggle the item for a single click, double click on
2501 // the button doesn't do anything (it toggles the item twice)
2502 if ( event
.LeftDown() )
2507 // don't select the item if the button was clicked
2511 // how should the selection work for this event?
2512 bool is_multiple
, extended_select
, unselect_others
;
2513 EventFlagsToSelType(GetWindowStyleFlag(),
2515 event
.ControlDown(),
2516 is_multiple
, extended_select
, unselect_others
);
2518 SelectItem(item
, unselect_others
, extended_select
);
2520 if ( event
.LeftDClick() )
2522 // double clicking should not start editing the item label
2523 m_renameTimer
->Stop();
2524 m_lastOnSame
= FALSE
;
2526 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2527 nevent
.m_item
= (long) item
;
2529 CalcScrolledPosition(x
, y
,
2530 &nevent
.m_pointDrag
.x
,
2531 &nevent
.m_pointDrag
.y
);
2532 nevent
.SetEventObject( this );
2533 GetEventHandler()->ProcessEvent( nevent
);
2539 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2541 /* after all changes have been done to the tree control,
2542 * we actually redraw the tree when everything is over */
2549 CalculatePositions();
2551 AdjustMyScrollbars();
2554 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2560 dc
.SetFont(m_boldFont
);
2562 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2565 // restore normal font
2566 dc
.SetFont( m_normalFont
);
2570 int image
= item
->GetCurrentImage();
2571 if ( image
!= NO_IMAGE
)
2573 if ( m_imageListNormal
)
2575 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2580 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2583 total_h
+= 2; // at least 2 pixels
2585 total_h
+= total_h
/10; // otherwise 10% extra spacing
2587 item
->SetHeight(total_h
);
2588 if (total_h
>m_lineHeight
)
2589 m_lineHeight
=total_h
;
2591 item
->SetWidth(image_w
+text_w
+2);
2594 // -----------------------------------------------------------------------------
2595 // for developper : y is now the top of the level
2596 // not the middle of it !
2597 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2599 int horizX
= level
*m_indent
;
2601 CalculateSize( item
, dc
);
2604 item
->SetX( horizX
+m_indent
+m_spacing
);
2606 y
+=GetLineHeight(item
);
2608 if ( !item
->IsExpanded() )
2610 // we dont need to calculate collapsed branches
2614 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2615 size_t n
, count
= children
.Count();
2616 for (n
= 0; n
< count
; ++n
)
2617 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2620 void wxGenericTreeCtrl::CalculatePositions()
2622 if ( !m_anchor
) return;
2624 wxClientDC
dc(this);
2627 dc
.SetFont( m_normalFont
);
2629 dc
.SetPen( m_dottedPen
);
2630 //if(GetImageList() == NULL)
2631 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2634 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2637 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2639 if (m_dirty
) return;
2641 wxClientDC
dc(this);
2646 GetClientSize( &cw
, &ch
);
2649 rect
.x
= dc
.LogicalToDeviceX( 0 );
2651 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2654 Refresh( TRUE
, &rect
);
2656 AdjustMyScrollbars();
2659 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2661 if (m_dirty
) return;
2663 wxClientDC
dc(this);
2668 GetClientSize( &cw
, &ch
);
2671 rect
.x
= dc
.LogicalToDeviceX( 0 );
2672 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2674 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2676 Refresh( TRUE
, &rect
);