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
) );
668 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
669 m_dottedPen
= wxPen( "grey", 0, 0 );
674 wxGenericTreeCtrl::~wxGenericTreeCtrl()
676 wxDELETE( m_hilightBrush
);
680 delete m_renameTimer
;
681 if (m_ownsImageListNormal
) delete m_imageListNormal
;
682 if (m_ownsImageListState
) delete m_imageListState
;
685 // -----------------------------------------------------------------------------
687 // -----------------------------------------------------------------------------
689 size_t wxGenericTreeCtrl::GetCount() const
691 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
694 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
700 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
706 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
708 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
710 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
713 // -----------------------------------------------------------------------------
714 // functions to work with tree items
715 // -----------------------------------------------------------------------------
717 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
719 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
721 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
724 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
725 wxTreeItemIcon which
) const
727 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
729 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
732 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
734 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
736 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
739 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
741 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
744 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
745 pItem
->SetText(text
);
746 CalculateSize(pItem
, dc
);
750 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
752 wxTreeItemIcon which
)
754 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
756 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
757 pItem
->SetImage(image
, which
);
760 CalculateSize(pItem
, dc
);
764 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
766 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
768 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
771 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
773 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
775 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
776 pItem
->SetHasPlus(has
);
780 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
782 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
784 // avoid redrawing the tree if no real change
785 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
786 if ( pItem
->IsBold() != bold
)
788 pItem
->SetBold(bold
);
793 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
796 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
798 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
799 pItem
->Attr().SetTextColour(col
);
803 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
806 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
808 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
809 pItem
->Attr().SetBackgroundColour(col
);
813 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
815 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
817 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
818 pItem
->Attr().SetFont(font
);
822 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
824 wxScrolledWindow::SetFont(font
);
826 m_normalFont
= font
;
827 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
828 m_normalFont
.GetFamily(),
829 m_normalFont
.GetStyle(),
831 m_normalFont
.GetUnderlined());
837 // -----------------------------------------------------------------------------
838 // item status inquiries
839 // -----------------------------------------------------------------------------
841 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
843 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
845 // An item is only visible if it's not a descendant of a collapsed item
846 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
847 wxGenericTreeItem
* parent
= pItem
->GetParent();
850 if (!parent
->IsExpanded())
852 parent
= parent
->GetParent();
856 GetViewStart(& startX
, & startY
);
858 wxSize clientSize
= GetClientSize();
861 if (!GetBoundingRect(item
, rect
))
863 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
865 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
867 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
873 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
875 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
877 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
880 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
882 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
884 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
887 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
889 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
891 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
894 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
896 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
898 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
901 // -----------------------------------------------------------------------------
903 // -----------------------------------------------------------------------------
905 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
907 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
909 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
912 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
914 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
917 return GetNextChild(item
, cookie
);
920 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
922 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
924 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
925 if ( (size_t)cookie
< children
.Count() )
927 return children
.Item((size_t)cookie
++);
931 // there are no more of them
932 return wxTreeItemId();
936 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
938 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
940 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
941 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
944 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
946 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
948 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
949 wxGenericTreeItem
*parent
= i
->GetParent();
950 if ( parent
== NULL
)
952 // root item doesn't have any siblings
953 return wxTreeItemId();
956 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
957 int index
= siblings
.Index(i
);
958 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
960 size_t n
= (size_t)(index
+ 1);
961 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
964 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
966 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
968 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
969 wxGenericTreeItem
*parent
= i
->GetParent();
970 if ( parent
== NULL
)
972 // root item doesn't have any siblings
973 return wxTreeItemId();
976 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
977 int index
= siblings
.Index(i
);
978 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
980 return index
== 0 ? wxTreeItemId()
981 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
984 // Only for internal use right now, but should probably be public
985 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
987 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
989 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
991 // First see if there are any children.
992 wxArrayGenericTreeItems
& children
= i
->GetChildren();
993 if (children
.GetCount() > 0)
995 return children
.Item(0);
999 // Try a sibling of this or ancestor instead
1000 wxTreeItemId p
= item
;
1001 wxTreeItemId toFind
;
1004 toFind
= GetNextSibling(p
);
1006 } while (p
.IsOk() && !toFind
.IsOk());
1011 wxTreeItemId
wxGenericTreeCtrl::GetPrev(const wxTreeItemId
& item
) const
1013 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1015 wxFAIL_MSG(wxT("not implemented"));
1017 return wxTreeItemId();
1020 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1022 wxTreeItemId id
= GetRootItem();
1031 } while (id
.IsOk());
1033 return wxTreeItemId();
1036 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1038 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1040 wxTreeItemId id
= item
;
1045 if (id
.IsOk() && IsVisible(id
))
1048 return wxTreeItemId();
1051 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1053 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1055 wxFAIL_MSG(wxT("not implemented"));
1057 return wxTreeItemId();
1060 // -----------------------------------------------------------------------------
1062 // -----------------------------------------------------------------------------
1064 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1066 const wxString
& text
,
1067 int image
, int selImage
,
1068 wxTreeItemData
*data
)
1070 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1073 // should we give a warning here?
1074 return AddRoot(text
, image
, selImage
, data
);
1077 wxClientDC
dc(this);
1078 wxGenericTreeItem
*item
=
1079 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1083 data
->m_pItem
= (long) item
;
1086 parent
->Insert( item
, previous
);
1093 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1094 int image
, int selImage
,
1095 wxTreeItemData
*data
)
1097 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1099 wxClientDC
dc(this);
1100 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1101 image
, selImage
, data
);
1104 data
->m_pItem
= (long) m_anchor
;
1107 if (!HasFlag(wxTR_MULTIPLE
))
1109 m_current
= m_key_current
= m_anchor
;
1110 m_current
->SetHilight( TRUE
);
1118 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1119 const wxString
& text
,
1120 int image
, int selImage
,
1121 wxTreeItemData
*data
)
1123 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1126 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1127 const wxTreeItemId
& idPrevious
,
1128 const wxString
& text
,
1129 int image
, int selImage
,
1130 wxTreeItemData
*data
)
1132 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1135 // should we give a warning here?
1136 return AddRoot(text
, image
, selImage
, data
);
1139 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1140 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1141 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1143 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1146 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1148 const wxString
& text
,
1149 int image
, int selImage
,
1150 wxTreeItemData
*data
)
1152 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1155 // should we give a warning here?
1156 return AddRoot(text
, image
, selImage
, data
);
1159 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1162 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1163 const wxString
& text
,
1164 int image
, int selImage
,
1165 wxTreeItemData
*data
)
1167 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1170 // should we give a warning here?
1171 return AddRoot(text
, image
, selImage
, data
);
1174 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1175 image
, selImage
, data
);
1178 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1180 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1181 event
.m_item
= (long) item
;
1182 event
.SetEventObject( this );
1183 ProcessEvent( event
);
1186 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1188 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1189 item
->DeleteChildren(this);
1194 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1196 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1198 // don't stay with invalid m_key_current or we will crash in the next call
1200 bool changeKeyCurrent
= FALSE
;
1201 wxGenericTreeItem
*itemKey
= m_key_current
;
1202 while ( itemKey
&& !changeKeyCurrent
)
1204 if ( itemKey
== item
)
1206 // m_key_current is a descendant of the item being deleted
1207 changeKeyCurrent
= TRUE
;
1211 itemKey
= itemKey
->GetParent();
1215 wxGenericTreeItem
*parent
= item
->GetParent();
1218 parent
->GetChildren().Remove( item
); // remove by value
1221 if ( changeKeyCurrent
)
1223 // may be NULL or not
1224 m_key_current
= parent
;
1227 item
->DeleteChildren(this);
1228 SendDeleteEvent(item
);
1234 void wxGenericTreeCtrl::DeleteAllItems()
1238 m_anchor
->DeleteChildren(this);
1247 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1249 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1251 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1253 if ( !item
->HasPlus() )
1256 if ( item
->IsExpanded() )
1259 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1260 event
.m_item
= (long) item
;
1261 event
.SetEventObject( this );
1263 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1265 // cancelled by program
1270 CalculatePositions();
1272 RefreshSubtree(item
);
1274 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1275 ProcessEvent( event
);
1278 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1281 if ( IsExpanded(item
) )
1284 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1285 while ( child
.IsOk() )
1289 child
= GetNextChild(item
, cookie
);
1294 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1296 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1298 if ( !item
->IsExpanded() )
1301 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1302 event
.m_item
= (long) item
;
1303 event
.SetEventObject( this );
1304 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1306 // cancelled by program
1312 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1313 size_t count
= children
.Count();
1314 for ( size_t n
= 0; n
< count
; n
++ )
1316 Collapse(children
[n
]);
1319 CalculatePositions();
1321 RefreshSubtree(item
);
1323 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1324 ProcessEvent( event
);
1327 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1330 DeleteChildren(item
);
1333 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1335 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1337 if (item
->IsExpanded())
1343 void wxGenericTreeCtrl::Unselect()
1347 m_current
->SetHilight( FALSE
);
1348 RefreshLine( m_current
);
1352 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1354 if (item
->IsSelected())
1356 item
->SetHilight(FALSE
);
1360 if (item
->HasChildren())
1362 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1363 size_t count
= children
.Count();
1364 for ( size_t n
= 0; n
< count
; ++n
)
1366 UnselectAllChildren(children
[n
]);
1371 void wxGenericTreeCtrl::UnselectAll()
1373 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1376 // Recursive function !
1377 // To stop we must have crt_item<last_item
1379 // Tag all next children, when no more children,
1380 // Move to parent (not to tag)
1381 // Keep going... if we found last_item, we stop.
1382 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1384 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1386 if (parent
== NULL
) // This is root item
1387 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1389 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1390 int index
= children
.Index(crt_item
);
1391 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1393 size_t count
= children
.Count();
1394 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1396 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1399 return TagNextChildren(parent
, last_item
, select
);
1402 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1404 crt_item
->SetHilight(select
);
1405 RefreshLine(crt_item
);
1407 if (crt_item
==last_item
)
1410 if (crt_item
->HasChildren())
1412 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1413 size_t count
= children
.Count();
1414 for ( size_t n
= 0; n
< count
; ++n
)
1416 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1424 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1426 // item2 is not necessary after item1
1427 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1429 // choice first' and 'last' between item1 and item2
1430 if (item1
->GetY()<item2
->GetY())
1441 bool select
= m_current
->IsSelected();
1443 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1446 TagNextChildren(first
,last
,select
);
1449 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1450 bool unselect_others
,
1451 bool extended_select
)
1453 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1455 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1456 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1458 //wxCHECK_RET( ( (!unselect_others) && is_single),
1459 // wxT("this is a single selection tree") );
1461 // to keep going anyhow !!!
1464 if (item
->IsSelected())
1465 return; // nothing to do
1466 unselect_others
= TRUE
;
1467 extended_select
= FALSE
;
1469 else if ( unselect_others
&& item
->IsSelected() )
1471 // selection change if there is more than one item currently selected
1472 wxArrayTreeItemIds selected_items
;
1473 if ( GetSelections(selected_items
) == 1 )
1477 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1478 event
.m_item
= (long) item
;
1479 event
.m_itemOld
= (long) m_current
;
1480 event
.SetEventObject( this );
1481 // TODO : Here we don't send any selection mode yet !
1483 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1487 if (unselect_others
)
1489 if (is_single
) Unselect(); // to speed up thing
1494 if (extended_select
)
1499 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1502 // don't change the mark (m_current)
1503 SelectItemRange(m_current
, item
);
1507 bool select
=TRUE
; // the default
1509 // Check if we need to toggle hilight (ctrl mode)
1510 if (!unselect_others
)
1511 select
=!item
->IsSelected();
1513 m_current
= m_key_current
= item
;
1514 m_current
->SetHilight(select
);
1515 RefreshLine( m_current
);
1518 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1519 GetEventHandler()->ProcessEvent( event
);
1522 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1523 wxArrayTreeItemIds
&array
) const
1525 if ( item
->IsSelected() )
1526 array
.Add(wxTreeItemId(item
));
1528 if ( item
->HasChildren() )
1530 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1531 size_t count
= children
.GetCount();
1532 for ( size_t n
= 0; n
< count
; ++n
)
1533 FillArray(children
[n
], array
);
1537 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1540 wxTreeItemId idRoot
= GetRootItem();
1541 if ( idRoot
.IsOk() )
1543 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1545 //else: the tree is empty, so no selections
1547 return array
.Count();
1550 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1552 if (!item
.IsOk()) return;
1554 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1556 // first expand all parent branches
1557 wxGenericTreeItem
*parent
= gitem
->GetParent();
1561 parent
= parent
->GetParent();
1564 //if (parent) CalculatePositions();
1569 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1571 if (!item
.IsOk()) return;
1573 // We have to call this here because the label in
1574 // question might just have been added and no screen
1575 // update taken place.
1576 if (m_dirty
) wxYieldIfNeeded();
1578 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1580 // now scroll to the item
1581 int item_y
= gitem
->GetY();
1585 ViewStart( &start_x
, &start_y
);
1586 start_y
*= PIXELS_PER_UNIT
;
1590 GetClientSize( &client_w
, &client_h
);
1592 if (item_y
< start_y
+3)
1597 m_anchor
->GetSize( x
, y
, this );
1598 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1599 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1600 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1601 // Item should appear at top
1602 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1604 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1609 m_anchor
->GetSize( x
, y
, this );
1610 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1611 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1612 item_y
+= PIXELS_PER_UNIT
+2;
1613 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1614 // Item should appear at bottom
1615 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
);
1619 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1620 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1622 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1623 wxGenericTreeItem
**item2
)
1625 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1627 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1630 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1631 const wxTreeItemId
& item2
)
1633 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1636 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1638 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1640 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1642 wxCHECK_RET( !s_treeBeingSorted
,
1643 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1645 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1646 if ( children
.Count() > 1 )
1648 s_treeBeingSorted
= this;
1649 children
.Sort(tree_ctrl_compare_func
);
1650 s_treeBeingSorted
= NULL
;
1654 //else: don't make the tree dirty as nothing changed
1657 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1659 return m_imageListNormal
;
1662 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1664 return m_imageListState
;
1667 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1669 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1671 m_imageListNormal
= imageList
;
1672 m_ownsImageListNormal
= FALSE
;
1674 if ( !m_imageListNormal
)
1677 // Calculate a m_lineHeight value from the image sizes.
1678 // May be toggle off. Then wxGenericTreeCtrl will spread when
1679 // necessary (which might look ugly).
1680 wxClientDC
dc(this);
1681 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1682 int width
= 0, height
= 0,
1683 n
= m_imageListNormal
->GetImageCount();
1685 for (int i
= 0; i
< n
; i
++)
1687 m_imageListNormal
->GetSize(i
, width
, height
);
1688 if (height
> m_lineHeight
) m_lineHeight
= height
;
1691 if (m_lineHeight
< 40)
1692 m_lineHeight
+= 2; // at least 2 pixels
1694 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1697 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1699 if (m_ownsImageListState
) delete m_imageListState
;
1700 m_imageListState
= imageList
;
1701 m_ownsImageListState
= FALSE
;
1704 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1706 SetImageList(imageList
);
1707 m_ownsImageListNormal
= TRUE
;
1710 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1712 SetStateImageList(imageList
);
1713 m_ownsImageListState
= TRUE
;
1716 // -----------------------------------------------------------------------------
1718 // -----------------------------------------------------------------------------
1720 void wxGenericTreeCtrl::AdjustMyScrollbars()
1726 m_anchor
->GetSize( x
, y
, this );
1727 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1728 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1729 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1730 int y_pos
= GetScrollPos( wxVERTICAL
);
1731 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1735 SetScrollbars( 0, 0, 0, 0 );
1739 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1741 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1742 return item
->GetHeight();
1744 return m_lineHeight
;
1747 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1749 wxTreeItemAttr
*attr
= item
->GetAttributes();
1750 if ( attr
&& attr
->HasFont() )
1751 dc
.SetFont(attr
->GetFont());
1752 else if (item
->IsBold())
1753 dc
.SetFont(m_boldFont
);
1757 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1761 int image
= item
->GetCurrentImage();
1762 if ( image
!= NO_IMAGE
)
1764 if ( m_imageListNormal
)
1766 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1775 int total_h
= GetLineHeight(item
);
1777 if (item
->IsSelected())
1778 dc
.SetBrush(*m_hilightBrush
);
1782 if ( attr
&& attr
->HasBackgroundColour() )
1783 colBg
= attr
->GetBackgroundColour();
1785 colBg
= m_backgroundColour
;
1786 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1790 if (HasFlag(wxTR_ROW_LINES
)) offset
= 1;
1792 if (item
->IsSelected() && image
!= NO_IMAGE
)
1794 // If it's selected, and there's an image, then we should
1795 // take care to leave the area under the image painted in the
1796 // background colour.
1797 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1798 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1802 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1803 item
->GetWidth()+2, total_h
-offset
);
1806 if ( image
!= NO_IMAGE
)
1808 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1809 m_imageListNormal
->Draw( image
, dc
,
1811 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1812 wxIMAGELIST_DRAW_TRANSPARENT
);
1813 dc
.DestroyClippingRegion();
1816 dc
.SetBackgroundMode(wxTRANSPARENT
);
1817 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1818 dc
.DrawText( item
->GetText(),
1819 (wxCoord
)(image_w
+ item
->GetX()),
1820 (wxCoord
)(item
->GetY() + extraH
));
1822 // restore normal font
1823 dc
.SetFont( m_normalFont
);
1826 // Now y stands for the top of the item, whereas it used to stand for middle !
1827 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1829 int x
= (level
+1)*m_indent
;
1831 item
->SetX( x
+m_spacing
);
1835 y
+=GetLineHeight(item
)/2;
1837 item
->SetCross( x
, y
);
1839 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1840 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1842 bool drawLines
= (!HasFlag(wxTR_NO_LINES
) && !HasFlag(wxTR_MAC_BUTTONS
));
1844 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1846 int startX
= x
- m_indent
;
1849 if (!item
->HasChildren()) endX
+= 20;
1851 if (HasFlag( wxTR_MAC_BUTTONS
))
1853 if (item
->HasPlus())
1855 dc
.SetPen( *wxBLACK_PEN
);
1856 dc
.SetBrush( *m_hilightBrush
);
1860 if (item
->IsExpanded())
1878 dc
.DrawPolygon( 3, button
);
1880 dc
.SetPen( m_dottedPen
);
1886 dc
.DrawLine( startX
, y
, endX
, y
);
1888 if (item
->HasPlus())
1891 dc
.DrawLine( x
+5, y
, x
+15, y
);
1892 dc
.SetPen( *wxGREY_PEN
);
1893 dc
.SetBrush( *wxWHITE_BRUSH
);
1894 dc
.DrawRectangle( x
-5, y
-4, 11, 9 );
1896 dc
.SetPen( *wxBLACK_PEN
);
1897 dc
.DrawLine( x
-2, y
, x
+3, y
);
1898 if (!item
->IsExpanded())
1899 dc
.DrawLine( x
, y
-2, x
, y
+3 );
1900 dc
.SetPen( m_dottedPen
);
1904 wxPen
*pen
= wxTRANSPARENT_PEN
;
1907 if ( item
->IsSelected() )
1909 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1917 wxTreeItemAttr
*attr
= item
->GetAttributes();
1918 if ( attr
&& attr
->HasTextColour() )
1919 colText
= attr
->GetTextColour();
1921 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
);
1925 dc
.SetTextForeground(colText
);
1929 PaintItem(item
, dc
);
1931 if (HasFlag( wxTR_ROW_LINES
))
1933 dc
.SetPen( *wxWHITE_PEN
);
1934 dc
.DrawLine( 0, oldY
, 10000, oldY
);
1935 dc
.DrawLine( 0, oldY
+ GetLineHeight(item
), 10000, oldY
+ GetLineHeight(item
) );
1938 // restore DC objects
1939 dc
.SetBrush( *wxWHITE_BRUSH
);
1940 dc
.SetPen( m_dottedPen
);
1941 dc
.SetTextForeground( *wxBLACK
);
1944 y
= oldY
+GetLineHeight(item
);
1946 if (item
->IsExpanded())
1948 oldY
+=GetLineHeight(item
)/2;
1951 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1952 size_t n
, count
= children
.Count();
1953 for ( n
= 0; n
< count
; ++n
)
1956 PaintLevel( children
[n
], dc
, level
+1, y
);
1959 // it may happen that the item is expanded but has no items (when you
1960 // delete all its children for example) - don't draw the vertical line
1964 semiOldY
+=GetLineHeight(children
[--n
])/2;
1966 dc
.DrawLine( x
, oldY
+5, x
, semiOldY
);
1971 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1975 if ( item
->HasPlus() )
1977 // it's a folder, indicate it by a border
1982 // draw a line under the drop target because the item will be
1984 DrawLine(item
, TRUE
/* below */);
1987 SetCursor(wxCURSOR_BULLSEYE
);
1992 SetCursor(wxCURSOR_NO_ENTRY
);
1996 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
1998 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2000 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2002 wxClientDC
dc(this);
2004 dc
.SetLogicalFunction(wxINVERT
);
2005 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2007 int w
= i
->GetWidth() + 2;
2008 int h
= GetLineHeight(i
) + 2;
2010 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2013 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2015 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2017 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2019 wxClientDC
dc(this);
2021 dc
.SetLogicalFunction(wxINVERT
);
2027 y
+= GetLineHeight(i
) - 1;
2030 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2033 // -----------------------------------------------------------------------------
2034 // wxWindows callbacks
2035 // -----------------------------------------------------------------------------
2037 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2045 dc
.SetFont( m_normalFont
);
2046 dc
.SetPen( m_dottedPen
);
2048 // this is now done dynamically
2049 //if(GetImageList() == NULL)
2050 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2053 PaintLevel( m_anchor
, dc
, 0, y
);
2056 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2060 if (m_current
) RefreshLine( m_current
);
2063 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2067 if (m_current
) RefreshLine( m_current
);
2070 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2072 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2073 te
.m_code
= (int)event
.KeyCode();
2074 te
.SetEventObject( this );
2075 GetEventHandler()->ProcessEvent( te
);
2077 if ( (m_current
== 0) || (m_key_current
== 0) )
2083 // how should the selection work for this event?
2084 bool is_multiple
, extended_select
, unselect_others
;
2085 EventFlagsToSelType(GetWindowStyleFlag(),
2087 event
.ControlDown(),
2088 is_multiple
, extended_select
, unselect_others
);
2092 // * : Expand all/Collapse all
2093 // ' ' | return : activate
2094 // up : go up (not last children!)
2096 // left : go to parent
2097 // right : open if parent and go next
2098 // home : go to root
2099 // end : go to last item without opening parents
2100 switch (event
.KeyCode())
2104 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2112 if ( !IsExpanded(m_current
) )
2115 ExpandAll(m_current
);
2118 //else: fall through to Collapse() it
2122 if (IsExpanded(m_current
))
2124 Collapse(m_current
);
2131 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2132 event
.m_item
= (long) m_current
;
2134 event
.SetEventObject( this );
2135 GetEventHandler()->ProcessEvent( event
);
2139 // up goes to the previous sibling or to the last of its children if
2143 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2146 prev
= GetParent( m_key_current
);
2150 wxTreeItemId current
= m_key_current
;
2151 if (current
== GetFirstChild( prev
, cockie
))
2153 // otherwise we return to where we came from
2154 SelectItem( prev
, unselect_others
, extended_select
);
2155 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2156 EnsureVisible( prev
);
2163 while ( IsExpanded(prev
) && HasChildren(prev
) )
2165 wxTreeItemId child
= GetLastChild(prev
);
2172 SelectItem( prev
, unselect_others
, extended_select
);
2173 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2174 EnsureVisible( prev
);
2179 // left arrow goes to the parent
2182 wxTreeItemId prev
= GetParent( m_current
);
2185 EnsureVisible( prev
);
2186 SelectItem( prev
, unselect_others
, extended_select
);
2192 // this works the same as the down arrow except that we also expand the
2193 // item if it wasn't expanded yet
2199 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2202 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2203 SelectItem( child
, unselect_others
, extended_select
);
2204 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2205 EnsureVisible( child
);
2209 wxTreeItemId next
= GetNextSibling( m_key_current
);
2212 wxTreeItemId current
= m_key_current
;
2213 while (current
&& !next
)
2215 current
= GetParent( current
);
2216 if (current
) next
= GetNextSibling( current
);
2221 SelectItem( next
, unselect_others
, extended_select
);
2222 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2223 EnsureVisible( next
);
2229 // <End> selects the last visible tree item
2232 wxTreeItemId last
= GetRootItem();
2234 while ( last
.IsOk() && IsExpanded(last
) )
2236 wxTreeItemId lastChild
= GetLastChild(last
);
2238 // it may happen if the item was expanded but then all of
2239 // its children have been deleted - so IsExpanded() returned
2240 // TRUE, but GetLastChild() returned invalid item
2249 EnsureVisible( last
);
2250 SelectItem( last
, unselect_others
, extended_select
);
2255 // <Home> selects the root item
2258 wxTreeItemId prev
= GetRootItem();
2261 EnsureVisible( prev
);
2262 SelectItem( prev
, unselect_others
, extended_select
);
2272 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2274 // We have to call this here because the label in
2275 // question might just have been added and no screen
2276 // update taken place.
2277 // JACS: removed this because the yield can cause the window to be
2278 // deleted from under us if a close window event is pending
2279 // if (m_dirty) wxYieldIfNeeded();
2281 wxClientDC
dc(this);
2283 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2284 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2289 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2290 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2291 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2292 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2295 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2297 return wxTreeItemId();
2300 // get the bounding rectangle of the item (or of its label only)
2301 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2303 bool WXUNUSED(textOnly
)) const
2305 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2307 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2310 GetViewStart(& startX
, & startY
);
2312 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2313 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2314 rect
.width
= i
->GetWidth();
2315 //rect.height = i->GetHeight();
2316 rect
.height
= GetLineHeight(i
);
2323 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2325 if (!item
.IsOk()) return;
2327 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2329 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2330 te
.m_item
= (long) m_currentEdit
;
2331 te
.SetEventObject( this );
2332 GetEventHandler()->ProcessEvent( te
);
2334 if (!te
.IsAllowed()) return;
2336 // We have to call this here because the label in
2337 // question might just have been added and no screen
2338 // update taken place.
2339 if (m_dirty
) wxYieldIfNeeded();
2341 wxString s
= m_currentEdit
->GetText();
2342 int x
= m_currentEdit
->GetX();
2343 int y
= m_currentEdit
->GetY();
2344 int w
= m_currentEdit
->GetWidth();
2345 int h
= m_currentEdit
->GetHeight();
2350 int image
= m_currentEdit
->GetCurrentImage();
2351 if ( image
!= NO_IMAGE
)
2353 if ( m_imageListNormal
)
2355 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2360 wxFAIL_MSG(_T("you must create an image list to use images!"));
2364 w
-= image_w
+ 4; // I don't know why +4 is needed
2366 wxClientDC
dc(this);
2368 x
= dc
.LogicalToDeviceX( x
);
2369 y
= dc
.LogicalToDeviceY( y
);
2371 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2381 void wxGenericTreeCtrl::OnRenameTimer()
2386 void wxGenericTreeCtrl::OnRenameAccept()
2388 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2389 le
.m_item
= (long) m_currentEdit
;
2390 le
.SetEventObject( this );
2391 le
.m_label
= m_renameRes
;
2392 GetEventHandler()->ProcessEvent( le
);
2394 if (!le
.IsAllowed()) return;
2396 SetItemText( m_currentEdit
, m_renameRes
);
2399 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2401 if ( !m_anchor
) return;
2403 // we process left mouse up event (enables in-place edit), right down
2404 // (pass to the user code), left dbl click (activate item) and
2405 // dragging/moving events for items drag-and-drop
2406 if ( !(event
.LeftDown() ||
2408 event
.RightDown() ||
2409 event
.LeftDClick() ||
2411 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2418 wxClientDC
dc(this);
2420 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2421 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2424 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2426 if ( event
.Dragging() && !m_isDragging
)
2428 if (m_dragCount
== 0)
2429 m_dragStart
= wxPoint(x
,y
);
2433 if (m_dragCount
!= 3)
2435 // wait until user drags a bit further...
2439 wxEventType command
= event
.RightIsDown()
2440 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2441 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2443 wxTreeEvent
nevent( command
, GetId() );
2444 nevent
.m_item
= (long) m_current
;
2445 nevent
.SetEventObject(this);
2447 // by default the dragging is not supported, the user code must
2448 // explicitly allow the event for it to take place
2451 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2453 // we're going to drag this item
2454 m_isDragging
= TRUE
;
2456 // remember the old cursor because we will change it while
2458 m_oldCursor
= m_cursor
;
2460 // in a single selection control, hide the selection temporarily
2461 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2463 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2465 if ( m_oldSelection
)
2467 m_oldSelection
->SetHilight(FALSE
);
2468 RefreshLine(m_oldSelection
);
2475 else if ( event
.Moving() )
2477 if ( item
!= m_dropTarget
)
2479 // unhighlight the previous drop target
2480 DrawDropEffect(m_dropTarget
);
2482 m_dropTarget
= item
;
2484 // highlight the current drop target if any
2485 DrawDropEffect(m_dropTarget
);
2490 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2492 // erase the highlighting
2493 DrawDropEffect(m_dropTarget
);
2495 // generate the drag end event
2496 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2498 event
.m_item
= (long) item
;
2499 event
.m_pointDrag
= wxPoint(x
, y
);
2500 event
.SetEventObject(this);
2502 (void)GetEventHandler()->ProcessEvent(event
);
2504 m_isDragging
= FALSE
;
2505 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2507 if ( m_oldSelection
)
2509 m_oldSelection
->SetHilight(TRUE
);
2510 RefreshLine(m_oldSelection
);
2511 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2516 SetCursor(m_oldCursor
);
2522 // here we process only the messages which happen on tree items
2526 if (item
== NULL
) return; /* we hit the blank area */
2528 if ( event
.RightDown() )
2530 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2531 nevent
.m_item
= (long) item
;
2533 CalcScrolledPosition(x
, y
,
2534 &nevent
.m_pointDrag
.x
,
2535 &nevent
.m_pointDrag
.y
);
2536 nevent
.SetEventObject(this);
2537 GetEventHandler()->ProcessEvent(nevent
);
2539 else if ( event
.LeftUp() )
2543 if ( (item
== m_current
) &&
2544 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2545 HasFlag(wxTR_EDIT_LABELS
) )
2547 if ( m_renameTimer
->IsRunning() )
2548 m_renameTimer
->Stop();
2550 m_renameTimer
->Start( 100, TRUE
);
2553 m_lastOnSame
= FALSE
;
2556 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2558 if ( event
.LeftDown() )
2560 m_lastOnSame
= item
== m_current
;
2563 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2565 // only toggle the item for a single click, double click on
2566 // the button doesn't do anything (it toggles the item twice)
2567 if ( event
.LeftDown() )
2572 // don't select the item if the button was clicked
2576 // how should the selection work for this event?
2577 bool is_multiple
, extended_select
, unselect_others
;
2578 EventFlagsToSelType(GetWindowStyleFlag(),
2580 event
.ControlDown(),
2581 is_multiple
, extended_select
, unselect_others
);
2583 SelectItem(item
, unselect_others
, extended_select
);
2585 if ( event
.LeftDClick() )
2587 // double clicking should not start editing the item label
2588 m_renameTimer
->Stop();
2589 m_lastOnSame
= FALSE
;
2591 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2592 nevent
.m_item
= (long) item
;
2594 CalcScrolledPosition(x
, y
,
2595 &nevent
.m_pointDrag
.x
,
2596 &nevent
.m_pointDrag
.y
);
2597 nevent
.SetEventObject( this );
2598 GetEventHandler()->ProcessEvent( nevent
);
2604 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2606 /* after all changes have been done to the tree control,
2607 * we actually redraw the tree when everything is over */
2614 CalculatePositions();
2616 AdjustMyScrollbars();
2619 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2625 dc
.SetFont(m_boldFont
);
2627 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2630 // restore normal font
2631 dc
.SetFont( m_normalFont
);
2635 int image
= item
->GetCurrentImage();
2636 if ( image
!= NO_IMAGE
)
2638 if ( m_imageListNormal
)
2640 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2645 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2648 total_h
+= 2; // at least 2 pixels
2650 total_h
+= total_h
/10; // otherwise 10% extra spacing
2652 item
->SetHeight(total_h
);
2653 if (total_h
>m_lineHeight
)
2654 m_lineHeight
=total_h
;
2656 item
->SetWidth(image_w
+text_w
+2);
2659 // -----------------------------------------------------------------------------
2660 // for developper : y is now the top of the level
2661 // not the middle of it !
2662 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2664 int horizX
= level
*m_indent
;
2666 CalculateSize( item
, dc
);
2669 item
->SetX( horizX
+m_indent
+m_spacing
);
2671 y
+=GetLineHeight(item
);
2673 if ( !item
->IsExpanded() )
2675 // we dont need to calculate collapsed branches
2679 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2680 size_t n
, count
= children
.Count();
2681 for (n
= 0; n
< count
; ++n
)
2682 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2685 void wxGenericTreeCtrl::CalculatePositions()
2687 if ( !m_anchor
) return;
2689 wxClientDC
dc(this);
2692 dc
.SetFont( m_normalFont
);
2694 dc
.SetPen( m_dottedPen
);
2695 //if(GetImageList() == NULL)
2696 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2699 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2702 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2704 if (m_dirty
) return;
2706 wxClientDC
dc(this);
2711 GetClientSize( &cw
, &ch
);
2714 rect
.x
= dc
.LogicalToDeviceX( 0 );
2716 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2719 Refresh( TRUE
, &rect
);
2721 AdjustMyScrollbars();
2724 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2726 if (m_dirty
) return;
2728 wxClientDC
dc(this);
2733 GetClientSize( &cw
, &ch
);
2736 rect
.x
= dc
.LogicalToDeviceX( 0 );
2737 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2739 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2741 Refresh( TRUE
, &rect
);