1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/treebase.h"
34 #include "wx/generic/treectlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.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
78 wxTreeTextCtrl( wxWindow
*parent
,
82 wxGenericTreeCtrl
*owner
,
83 const wxString
&value
= wxEmptyString
,
84 const wxPoint
&pos
= wxDefaultPosition
,
85 const wxSize
&size
= wxDefaultSize
,
86 int style
= wxSIMPLE_BORDER
,
87 const wxValidator
& validator
= wxDefaultValidator
,
88 const wxString
&name
= wxTextCtrlNameStr
);
90 void OnChar( wxKeyEvent
&event
);
91 void OnKeyUp( wxKeyEvent
&event
);
92 void OnKillFocus( wxFocusEvent
&event
);
97 wxGenericTreeCtrl
*m_owner
;
98 wxString m_startValue
;
100 DECLARE_EVENT_TABLE()
104 class WXDLLEXPORT wxGenericTreeItem
108 wxGenericTreeItem() { m_data
= NULL
; }
109 wxGenericTreeItem( wxGenericTreeItem
*parent
,
110 const wxString
& text
,
113 wxTreeItemData
*data
);
115 ~wxGenericTreeItem();
118 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
120 const wxString
& GetText() const { return m_text
; }
121 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
122 { return m_images
[which
]; }
123 wxTreeItemData
*GetData() const { return m_data
; }
125 // returns the current image for the item (depending on its
126 // selected/expanded/whatever state)
127 int GetCurrentImage() const;
129 void SetText( const wxString
&text
);
130 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
131 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
133 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
135 void SetBold(bool bold
) { m_isBold
= bold
; }
137 int GetX() const { return m_x
; }
138 int GetY() const { return m_y
; }
140 void SetX(int x
) { m_x
= x
; }
141 void SetY(int y
) { m_y
= y
; }
143 int GetHeight() const { return m_height
; }
144 int GetWidth() const { return m_width
; }
146 void SetHeight(int h
) { m_height
= h
; }
147 void SetWidth(int w
) { m_width
= w
; }
149 wxGenericTreeItem
*GetParent() const { return m_parent
; }
152 // deletes all children notifying the treectrl about it if !NULL
154 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
156 // get count of all children (and grand children if 'recursively')
157 size_t GetChildrenCount(bool recursively
= TRUE
) const;
159 void Insert(wxGenericTreeItem
*child
, size_t index
)
160 { m_children
.Insert(child
, index
); }
162 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
164 // return the item at given position (or NULL if no item), onButton is
165 // TRUE if the point belongs to the item's button, otherwise it lies
166 // on the button's label
167 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
168 const wxGenericTreeCtrl
*,
172 void Expand() { m_isCollapsed
= FALSE
; }
173 void Collapse() { m_isCollapsed
= TRUE
; }
175 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
178 bool HasChildren() const { return !m_children
.IsEmpty(); }
179 bool IsSelected() const { return m_hasHilight
!= 0; }
180 bool IsExpanded() const { return !m_isCollapsed
; }
181 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
182 bool IsBold() const { return m_isBold
!= 0; }
185 // get them - may be NULL
186 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
187 // get them ensuring that the pointer is not NULL
188 wxTreeItemAttr
& Attr()
192 m_attr
= new wxTreeItemAttr
;
198 void SetAttributes(wxTreeItemAttr
*attr
)
200 if ( m_ownsAttr
) delete m_attr
;
204 // set them and delete when done
205 void AssignAttributes(wxTreeItemAttr
*attr
)
212 // since there can be very many of these, we save size by chosing
213 // the smallest representation for the elements and by ordering
214 // the members to avoid padding.
215 wxString m_text
; // label to be rendered for item
217 wxTreeItemData
*m_data
; // user-provided data
219 wxArrayGenericTreeItems m_children
; // list of children
220 wxGenericTreeItem
*m_parent
; // parent of this item
222 wxTreeItemAttr
*m_attr
; // attributes???
224 // tree ctrl images for the normal, selected, expanded and
225 // expanded+selected states
226 short m_images
[wxTreeItemIcon_Max
];
228 wxCoord m_x
; // (virtual) offset from top
229 short m_y
; // (virtual) offset from left
230 short m_width
; // width of this item
231 unsigned char m_height
; // height of this item
233 // use bitfields to save size
234 int m_isCollapsed
:1;
235 int m_hasHilight
:1; // same as focused
236 int m_hasPlus
:1; // used for item which doesn't have
237 // children but has a [+] button
238 int m_isBold
:1; // render the label in bold font
239 int m_ownsAttr
:1; // delete attribute when done
242 // =============================================================================
244 // =============================================================================
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 // translate the key or mouse event flags to the type of selection we're
252 static void EventFlagsToSelType(long style
,
256 bool &extended_select
,
257 bool &unselect_others
)
259 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
260 extended_select
= shiftDown
&& is_multiple
;
261 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
264 // -----------------------------------------------------------------------------
265 // wxTreeRenameTimer (internal)
266 // -----------------------------------------------------------------------------
268 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
273 void wxTreeRenameTimer::Notify()
275 m_owner
->OnRenameTimer();
278 //-----------------------------------------------------------------------------
279 // wxTreeTextCtrl (internal)
280 //-----------------------------------------------------------------------------
282 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
283 EVT_CHAR (wxTreeTextCtrl::OnChar
)
284 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
285 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
288 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
292 wxGenericTreeCtrl
*owner
,
293 const wxString
&value
,
297 const wxValidator
& validator
,
298 const wxString
&name
)
299 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
305 (*m_res
) = wxEmptyString
;
306 m_startValue
= value
;
309 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
311 // TODO focus doesn't return to the wxTextCtrl when this closes...
312 if (event
.m_keyCode
== WXK_RETURN
)
315 (*m_res
) = GetValue();
317 if ((*m_accept
) && ((*m_res
) != m_startValue
))
318 m_owner
->OnRenameAccept();
320 if (!wxPendingDelete
.Member(this))
321 wxPendingDelete
.Append(this);
325 if (event
.m_keyCode
== WXK_ESCAPE
)
330 if (!wxPendingDelete
.Member(this))
331 wxPendingDelete
.Append(this);
338 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
340 // auto-grow the textctrl:
341 wxSize parentSize
= m_owner
->GetSize();
342 wxPoint myPos
= GetPosition();
343 wxSize mySize
= GetSize();
345 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
346 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
347 if (mySize
.x
> sx
) sx
= mySize
.x
;
353 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
355 if (!wxPendingDelete
.Member(this))
356 wxPendingDelete
.Append(this);
358 if ((*m_accept
) && ((*m_res
) != m_startValue
))
359 m_owner
->OnRenameAccept();
362 // -----------------------------------------------------------------------------
364 // -----------------------------------------------------------------------------
366 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
367 const wxString
& text
,
368 int image
, int selImage
,
369 wxTreeItemData
*data
)
372 m_images
[wxTreeItemIcon_Normal
] = image
;
373 m_images
[wxTreeItemIcon_Selected
] = selImage
;
374 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
375 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
380 m_isCollapsed
= TRUE
;
381 m_hasHilight
= FALSE
;
387 m_attr
= (wxTreeItemAttr
*)NULL
;
390 // We don't know the height here yet.
395 wxGenericTreeItem::~wxGenericTreeItem()
399 if (m_ownsAttr
) delete m_attr
;
401 wxASSERT_MSG( m_children
.IsEmpty(),
402 wxT("please call DeleteChildren() before deleting the item") );
405 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
407 size_t count
= m_children
.Count();
408 for ( size_t n
= 0; n
< count
; n
++ )
410 wxGenericTreeItem
*child
= m_children
[n
];
412 tree
->SendDeleteEvent(child
);
414 child
->DeleteChildren(tree
);
421 void wxGenericTreeItem::SetText( const wxString
&text
)
426 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
428 size_t count
= m_children
.Count();
432 size_t total
= count
;
433 for (size_t n
= 0; n
< count
; ++n
)
435 total
+= m_children
[n
]->GetChildrenCount();
441 void wxGenericTreeItem::GetSize( int &x
, int &y
,
442 const wxGenericTreeCtrl
*theButton
)
444 int bottomY
=m_y
+theButton
->GetLineHeight(this);
445 if ( y
< bottomY
) y
= bottomY
;
446 int width
= m_x
+ m_width
;
447 if ( x
< width
) x
= width
;
451 size_t count
= m_children
.Count();
452 for ( size_t n
= 0; n
< count
; ++n
)
454 m_children
[n
]->GetSize( x
, y
, theButton
);
459 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
460 const wxGenericTreeCtrl
*theCtrl
,
464 // for a hidden root node, don't evaluate it, but do evaluate children
465 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
468 int h
= theCtrl
->GetLineHeight(this);
469 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
471 int y_mid
= m_y
+ h
/2;
472 if (point
.y
< y_mid
)
473 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
475 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
477 // 5 is the size of the plus sign
478 int xCross
= m_x
- theCtrl
->GetSpacing();
479 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
480 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
481 HasPlus() && theCtrl
->HasButtons() )
483 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
487 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
492 // assuming every image (normal and selected) has the same size!
493 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
494 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
497 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
498 flags
|= wxTREE_HITTEST_ONITEMICON
;
500 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
506 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
507 if (point
.x
> m_x
+m_width
)
508 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
513 // if children are expanded, fall through to evaluate them
514 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
518 size_t count
= m_children
.Count();
519 for ( size_t n
= 0; n
< count
; n
++ )
521 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
529 return (wxGenericTreeItem
*) NULL
;
532 int wxGenericTreeItem::GetCurrentImage() const
534 int image
= NO_IMAGE
;
539 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
542 if ( image
== NO_IMAGE
)
544 // we usually fall back to the normal item, but try just the
545 // expanded one (and not selected) first in this case
546 image
= GetImage(wxTreeItemIcon_Expanded
);
552 image
= GetImage(wxTreeItemIcon_Selected
);
555 // maybe it doesn't have the specific image we want,
556 // try the default one instead
557 if ( image
== NO_IMAGE
) image
= GetImage();
562 // -----------------------------------------------------------------------------
563 // wxGenericTreeCtrl implementation
564 // -----------------------------------------------------------------------------
566 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
568 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
569 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
570 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
571 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
572 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
573 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
574 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
577 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
579 * wxTreeCtrl has to be a real class or we have problems with
580 * the run-time information.
583 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
586 // -----------------------------------------------------------------------------
587 // construction/destruction
588 // -----------------------------------------------------------------------------
590 void wxGenericTreeCtrl::Init()
592 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
600 m_hilightBrush
= new wxBrush
602 wxSystemSettings::GetSystemColour
604 wxSYS_COLOUR_HIGHLIGHT
609 m_hilightUnfocusedBrush
= new wxBrush
611 wxSystemSettings::GetSystemColour
613 wxSYS_COLOUR_BTNSHADOW
618 m_imageListNormal
= m_imageListButtons
=
619 m_imageListState
= (wxImageList
*) NULL
;
620 m_ownsImageListNormal
= m_ownsImageListButtons
=
621 m_ownsImageListState
= FALSE
;
624 m_isDragging
= FALSE
;
625 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
627 m_renameTimer
= new wxTreeRenameTimer( this );
628 m_lastOnSame
= FALSE
;
630 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
631 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
632 m_normalFont
.GetFamily(),
633 m_normalFont
.GetStyle(),
635 m_normalFont
.GetUnderlined());
638 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
643 const wxValidator
&validator
,
644 const wxString
& name
)
646 wxScrolledWindow::Create( parent
, id
, pos
, size
,
647 style
|wxHSCROLL
|wxVSCROLL
, name
);
649 // If the tree display has no buttons, but does have
650 // connecting lines, we can use a narrower layout.
651 // It may not be a good idea to force this...
652 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
659 SetValidator( validator
);
662 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
664 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
665 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
670 wxGenericTreeCtrl::~wxGenericTreeCtrl()
672 delete m_hilightBrush
;
673 delete m_hilightUnfocusedBrush
;
677 delete m_renameTimer
;
678 if (m_ownsImageListNormal
) delete m_imageListNormal
;
679 if (m_ownsImageListState
) delete m_imageListState
;
680 if (m_ownsImageListButtons
) delete m_imageListButtons
;
683 // -----------------------------------------------------------------------------
685 // -----------------------------------------------------------------------------
687 size_t wxGenericTreeCtrl::GetCount() const
689 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
692 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
698 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
704 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
706 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
708 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
711 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
713 // right now, just sets the styles. Eventually, we may
714 // want to update the inherited styles, but right now
715 // none of the parents has updatable styles
716 m_windowStyle
= styles
;
720 // -----------------------------------------------------------------------------
721 // functions to work with tree items
722 // -----------------------------------------------------------------------------
724 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
726 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
728 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
731 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
732 wxTreeItemIcon which
) const
734 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
736 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
739 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
741 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
743 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
746 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
748 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
751 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
752 pItem
->SetText(text
);
753 CalculateSize(pItem
, dc
);
757 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
759 wxTreeItemIcon which
)
761 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
763 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
764 pItem
->SetImage(image
, which
);
767 CalculateSize(pItem
, dc
);
771 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
773 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
775 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
778 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
780 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
782 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
783 pItem
->SetHasPlus(has
);
787 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
789 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
791 // avoid redrawing the tree if no real change
792 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
793 if ( pItem
->IsBold() != bold
)
795 pItem
->SetBold(bold
);
800 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
803 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
805 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
806 pItem
->Attr().SetTextColour(col
);
810 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
813 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
815 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
816 pItem
->Attr().SetBackgroundColour(col
);
820 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
822 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
824 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
825 pItem
->Attr().SetFont(font
);
829 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
831 wxScrolledWindow::SetFont(font
);
833 m_normalFont
= font
;
834 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
835 m_normalFont
.GetFamily(),
836 m_normalFont
.GetStyle(),
838 m_normalFont
.GetUnderlined());
844 // -----------------------------------------------------------------------------
845 // item status inquiries
846 // -----------------------------------------------------------------------------
848 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
850 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
852 // An item is only visible if it's not a descendant of a collapsed item
853 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
854 wxGenericTreeItem
* parent
= pItem
->GetParent();
857 if (!parent
->IsExpanded())
859 parent
= parent
->GetParent();
863 GetViewStart(& startX
, & startY
);
865 wxSize clientSize
= GetClientSize();
868 if (!GetBoundingRect(item
, rect
))
870 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
872 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
874 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
880 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
882 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
884 // consider that the item does have children if it has the "+" button: it
885 // might not have them (if it had never been expanded yet) but then it
886 // could have them as well and it's better to err on this side rather than
887 // disabling some operations which are restricted to the items with
888 // children for an item which does have them
889 return !((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
892 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
894 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
896 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
899 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
901 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
903 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
906 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
908 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
910 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
913 // -----------------------------------------------------------------------------
915 // -----------------------------------------------------------------------------
917 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
919 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
921 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
924 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
926 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
929 return GetNextChild(item
, cookie
);
932 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
934 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
936 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
937 if ( (size_t)cookie
< children
.Count() )
939 return children
.Item((size_t)cookie
++);
943 // there are no more of them
944 return wxTreeItemId();
948 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
950 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
952 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
953 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
956 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
958 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
960 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
961 wxGenericTreeItem
*parent
= i
->GetParent();
962 if ( parent
== NULL
)
964 // root item doesn't have any siblings
965 return wxTreeItemId();
968 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
969 int index
= siblings
.Index(i
);
970 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
972 size_t n
= (size_t)(index
+ 1);
973 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
976 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
978 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
980 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
981 wxGenericTreeItem
*parent
= i
->GetParent();
982 if ( parent
== NULL
)
984 // root item doesn't have any siblings
985 return wxTreeItemId();
988 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
989 int index
= siblings
.Index(i
);
990 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
992 return index
== 0 ? wxTreeItemId()
993 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
996 // Only for internal use right now, but should probably be public
997 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
999 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1001 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1003 // First see if there are any children.
1004 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1005 if (children
.GetCount() > 0)
1007 return children
.Item(0);
1011 // Try a sibling of this or ancestor instead
1012 wxTreeItemId p
= item
;
1013 wxTreeItemId toFind
;
1016 toFind
= GetNextSibling(p
);
1018 } while (p
.IsOk() && !toFind
.IsOk());
1023 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1025 wxTreeItemId id
= GetRootItem();
1034 } while (id
.IsOk());
1036 return wxTreeItemId();
1039 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1041 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1043 wxTreeItemId id
= item
;
1046 while (id
= GetNext(id
), id
.IsOk())
1052 return wxTreeItemId();
1055 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1057 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1059 wxFAIL_MSG(wxT("not implemented"));
1061 return wxTreeItemId();
1064 // -----------------------------------------------------------------------------
1066 // -----------------------------------------------------------------------------
1068 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1070 const wxString
& text
,
1071 int image
, int selImage
,
1072 wxTreeItemData
*data
)
1074 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1077 // should we give a warning here?
1078 return AddRoot(text
, image
, selImage
, data
);
1081 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1083 wxGenericTreeItem
*item
=
1084 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1088 data
->m_pItem
= (long) item
;
1091 parent
->Insert( item
, previous
);
1096 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1097 int image
, int selImage
,
1098 wxTreeItemData
*data
)
1100 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1102 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1104 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1105 image
, selImage
, data
);
1106 if (HasFlag(wxTR_HIDE_ROOT
))
1108 // if root is hidden, make sure we can navigate
1110 m_anchor
->SetHasPlus();
1115 data
->m_pItem
= (long) m_anchor
;
1118 if (!HasFlag(wxTR_MULTIPLE
))
1120 m_current
= m_key_current
= m_anchor
;
1121 m_current
->SetHilight( TRUE
);
1127 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1128 const wxString
& text
,
1129 int image
, int selImage
,
1130 wxTreeItemData
*data
)
1132 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1135 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1136 const wxTreeItemId
& idPrevious
,
1137 const wxString
& text
,
1138 int image
, int selImage
,
1139 wxTreeItemData
*data
)
1141 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1144 // should we give a warning here?
1145 return AddRoot(text
, image
, selImage
, data
);
1148 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1149 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1150 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1152 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1155 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1157 const wxString
& text
,
1158 int image
, int selImage
,
1159 wxTreeItemData
*data
)
1161 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1164 // should we give a warning here?
1165 return AddRoot(text
, image
, selImage
, data
);
1168 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1171 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1172 const wxString
& text
,
1173 int image
, int selImage
,
1174 wxTreeItemData
*data
)
1176 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1179 // should we give a warning here?
1180 return AddRoot(text
, image
, selImage
, data
);
1183 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1184 image
, selImage
, data
);
1187 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1189 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1190 event
.m_item
= (long) item
;
1191 event
.SetEventObject( this );
1192 ProcessEvent( event
);
1195 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1197 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1199 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1200 item
->DeleteChildren(this);
1203 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1205 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1207 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1209 // don't stay with invalid m_key_current or we will crash in
1210 // the next call to OnChar()
1211 bool changeKeyCurrent
= FALSE
;
1212 wxGenericTreeItem
*itemKey
= m_key_current
;
1215 if ( itemKey
== item
)
1217 // m_key_current is a descendant of the item being deleted
1218 changeKeyCurrent
= TRUE
;
1221 itemKey
= itemKey
->GetParent();
1224 wxGenericTreeItem
*parent
= item
->GetParent();
1227 parent
->GetChildren().Remove( item
); // remove by value
1230 if ( changeKeyCurrent
)
1232 // may be NULL or not
1233 m_key_current
= parent
;
1236 item
->DeleteChildren(this);
1237 SendDeleteEvent(item
);
1241 void wxGenericTreeCtrl::DeleteAllItems()
1247 m_anchor
->DeleteChildren(this);
1254 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1256 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1258 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1260 if ( !item
->HasPlus() )
1263 if ( item
->IsExpanded() )
1266 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1267 event
.m_item
= (long) item
;
1268 event
.SetEventObject( this );
1270 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1272 // cancelled by program
1277 CalculatePositions();
1279 RefreshSubtree(item
);
1281 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1282 ProcessEvent( event
);
1285 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1288 if ( IsExpanded(item
) )
1291 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1292 while ( child
.IsOk() )
1296 child
= GetNextChild(item
, cookie
);
1301 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1303 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1305 if ( !item
->IsExpanded() )
1308 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1309 event
.m_item
= (long) item
;
1310 event
.SetEventObject( this );
1311 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1313 // cancelled by program
1319 #if 0 // TODO why should items be collapsed recursively?
1320 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1321 size_t count
= children
.Count();
1322 for ( size_t n
= 0; n
< count
; n
++ )
1324 Collapse(children
[n
]);
1328 CalculatePositions();
1330 RefreshSubtree(item
);
1332 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1333 ProcessEvent( event
);
1336 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1339 DeleteChildren(item
);
1342 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1344 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1346 if (item
->IsExpanded())
1352 void wxGenericTreeCtrl::Unselect()
1356 m_current
->SetHilight( FALSE
);
1357 RefreshLine( m_current
);
1361 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1363 if (item
->IsSelected())
1365 item
->SetHilight(FALSE
);
1369 if (item
->HasChildren())
1371 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1372 size_t count
= children
.Count();
1373 for ( size_t n
= 0; n
< count
; ++n
)
1375 UnselectAllChildren(children
[n
]);
1380 void wxGenericTreeCtrl::UnselectAll()
1382 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1385 // Recursive function !
1386 // To stop we must have crt_item<last_item
1388 // Tag all next children, when no more children,
1389 // Move to parent (not to tag)
1390 // Keep going... if we found last_item, we stop.
1391 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1393 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1395 if (parent
== NULL
) // This is root item
1396 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1398 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1399 int index
= children
.Index(crt_item
);
1400 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1402 size_t count
= children
.Count();
1403 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1405 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1408 return TagNextChildren(parent
, last_item
, select
);
1411 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1413 crt_item
->SetHilight(select
);
1414 RefreshLine(crt_item
);
1416 if (crt_item
==last_item
)
1419 if (crt_item
->HasChildren())
1421 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1422 size_t count
= children
.Count();
1423 for ( size_t n
= 0; n
< count
; ++n
)
1425 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1433 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1435 // item2 is not necessary after item1
1436 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1438 // choice first' and 'last' between item1 and item2
1439 if (item1
->GetY()<item2
->GetY())
1450 bool select
= m_current
->IsSelected();
1452 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1455 TagNextChildren(first
,last
,select
);
1458 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1459 bool unselect_others
,
1460 bool extended_select
)
1462 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1464 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1465 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1467 //wxCHECK_RET( ( (!unselect_others) && is_single),
1468 // wxT("this is a single selection tree") );
1470 // to keep going anyhow !!!
1473 if (item
->IsSelected())
1474 return; // nothing to do
1475 unselect_others
= TRUE
;
1476 extended_select
= FALSE
;
1478 else if ( unselect_others
&& item
->IsSelected() )
1480 // selection change if there is more than one item currently selected
1481 wxArrayTreeItemIds selected_items
;
1482 if ( GetSelections(selected_items
) == 1 )
1486 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1487 event
.m_item
= (long) item
;
1488 event
.m_itemOld
= (long) m_current
;
1489 event
.SetEventObject( this );
1490 // TODO : Here we don't send any selection mode yet !
1492 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1495 wxTreeItemId parent
= GetParent( itemId
);
1496 while (parent
.IsOk())
1498 if (!IsExpanded(parent
))
1501 parent
= GetParent( parent
);
1504 EnsureVisible( itemId
);
1507 if (unselect_others
)
1509 if (is_single
) Unselect(); // to speed up thing
1514 if (extended_select
)
1518 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1521 // don't change the mark (m_current)
1522 SelectItemRange(m_current
, item
);
1526 bool select
=TRUE
; // the default
1528 // Check if we need to toggle hilight (ctrl mode)
1529 if (!unselect_others
)
1530 select
=!item
->IsSelected();
1532 m_current
= m_key_current
= item
;
1533 m_current
->SetHilight(select
);
1534 RefreshLine( m_current
);
1537 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1538 GetEventHandler()->ProcessEvent( event
);
1541 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1542 wxArrayTreeItemIds
&array
) const
1544 if ( item
->IsSelected() )
1545 array
.Add(wxTreeItemId(item
));
1547 if ( item
->HasChildren() )
1549 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1550 size_t count
= children
.GetCount();
1551 for ( size_t n
= 0; n
< count
; ++n
)
1552 FillArray(children
[n
], array
);
1556 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1559 wxTreeItemId idRoot
= GetRootItem();
1560 if ( idRoot
.IsOk() )
1562 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1564 //else: the tree is empty, so no selections
1566 return array
.Count();
1569 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1571 if (!item
.IsOk()) return;
1573 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1575 // first expand all parent branches
1576 wxGenericTreeItem
*parent
= gitem
->GetParent();
1580 parent
= parent
->GetParent();
1583 //if (parent) CalculatePositions();
1588 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1590 if (!item
.IsOk()) return;
1592 // We have to call this here because the label in
1593 // question might just have been added and no screen
1594 // update taken place.
1595 if (m_dirty
) wxYieldIfNeeded();
1597 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1599 // now scroll to the item
1600 int item_y
= gitem
->GetY();
1604 GetViewStart( &start_x
, &start_y
);
1605 start_y
*= PIXELS_PER_UNIT
;
1609 GetClientSize( &client_w
, &client_h
);
1611 if (item_y
< start_y
+3)
1616 m_anchor
->GetSize( x
, y
, this );
1617 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1618 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1619 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1620 // Item should appear at top
1621 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1623 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1628 m_anchor
->GetSize( x
, y
, this );
1629 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1630 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1631 item_y
+= PIXELS_PER_UNIT
+2;
1632 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1633 // Item should appear at bottom
1634 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
);
1638 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1639 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1641 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1642 wxGenericTreeItem
**item2
)
1644 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1646 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1649 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1650 const wxTreeItemId
& item2
)
1652 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1655 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1657 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1659 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1661 wxCHECK_RET( !s_treeBeingSorted
,
1662 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1664 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1665 if ( children
.Count() > 1 )
1669 s_treeBeingSorted
= this;
1670 children
.Sort(tree_ctrl_compare_func
);
1671 s_treeBeingSorted
= NULL
;
1673 //else: don't make the tree dirty as nothing changed
1676 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1678 return m_imageListNormal
;
1681 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1683 return m_imageListButtons
;
1686 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1688 return m_imageListState
;
1691 void wxGenericTreeCtrl::CalculateLineHeight()
1693 wxClientDC
dc(this);
1694 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1696 if ( m_imageListNormal
)
1698 // Calculate a m_lineHeight value from the normal Image sizes.
1699 // May be toggle off. Then wxGenericTreeCtrl will spread when
1700 // necessary (which might look ugly).
1701 int n
= m_imageListNormal
->GetImageCount();
1702 for (int i
= 0; i
< n
; i
++)
1704 int width
= 0, height
= 0;
1705 m_imageListNormal
->GetSize(i
, width
, height
);
1706 if (height
> m_lineHeight
) m_lineHeight
= height
;
1710 if (m_imageListButtons
)
1712 // Calculate a m_lineHeight value from the Button image sizes.
1713 // May be toggle off. Then wxGenericTreeCtrl will spread when
1714 // necessary (which might look ugly).
1715 int n
= m_imageListButtons
->GetImageCount();
1716 for (int i
= 0; i
< n
; i
++)
1718 int width
= 0, height
= 0;
1719 m_imageListButtons
->GetSize(i
, width
, height
);
1720 if (height
> m_lineHeight
) m_lineHeight
= height
;
1724 if (m_lineHeight
< 30)
1725 m_lineHeight
+= 2; // at least 2 pixels
1727 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1730 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1732 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1733 m_imageListNormal
= imageList
;
1734 m_ownsImageListNormal
= FALSE
;
1736 CalculateLineHeight();
1739 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1741 if (m_ownsImageListState
) delete m_imageListState
;
1742 m_imageListState
= imageList
;
1743 m_ownsImageListState
= FALSE
;
1746 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1748 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1749 m_imageListButtons
= imageList
;
1750 m_ownsImageListButtons
= FALSE
;
1752 CalculateLineHeight();
1755 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1757 SetImageList(imageList
);
1758 m_ownsImageListNormal
= TRUE
;
1761 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1763 SetStateImageList(imageList
);
1764 m_ownsImageListState
= TRUE
;
1767 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1769 SetButtonsImageList(imageList
);
1770 m_ownsImageListButtons
= TRUE
;
1773 // -----------------------------------------------------------------------------
1775 // -----------------------------------------------------------------------------
1777 void wxGenericTreeCtrl::AdjustMyScrollbars()
1782 m_anchor
->GetSize( x
, y
, this );
1783 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1784 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1785 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1786 int y_pos
= GetScrollPos( wxVERTICAL
);
1787 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1791 SetScrollbars( 0, 0, 0, 0 );
1795 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1797 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1798 return item
->GetHeight();
1800 return m_lineHeight
;
1803 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1805 // TODO implement "state" icon on items
1807 wxTreeItemAttr
*attr
= item
->GetAttributes();
1808 if ( attr
&& attr
->HasFont() )
1809 dc
.SetFont(attr
->GetFont());
1810 else if (item
->IsBold())
1811 dc
.SetFont(m_boldFont
);
1813 long text_w
= 0, text_h
= 0;
1814 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1816 int image_h
= 0, image_w
= 0;
1817 int image
= item
->GetCurrentImage();
1818 if ( image
!= NO_IMAGE
)
1820 if ( m_imageListNormal
)
1822 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1831 int total_h
= GetLineHeight(item
);
1833 if ( item
->IsSelected() )
1835 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1840 if ( attr
&& attr
->HasBackgroundColour() )
1841 colBg
= attr
->GetBackgroundColour();
1843 colBg
= m_backgroundColour
;
1844 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1847 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1849 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1851 // If it's selected, and there's an image, then we should
1852 // take care to leave the area under the image painted in the
1853 // background colour.
1854 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1855 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1859 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1860 item
->GetWidth()+2, total_h
-offset
);
1863 if ( image
!= NO_IMAGE
)
1865 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1866 m_imageListNormal
->Draw( image
, dc
,
1868 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1869 wxIMAGELIST_DRAW_TRANSPARENT
);
1870 dc
.DestroyClippingRegion();
1873 dc
.SetBackgroundMode(wxTRANSPARENT
);
1874 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1875 dc
.DrawText( item
->GetText(),
1876 (wxCoord
)(image_w
+ item
->GetX()),
1877 (wxCoord
)(item
->GetY() + extraH
));
1879 // restore normal font
1880 dc
.SetFont( m_normalFont
);
1883 // Now y stands for the top of the item, whereas it used to stand for middle !
1884 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1886 int x
= level
*m_indent
;
1887 if (!HasFlag(wxTR_HIDE_ROOT
))
1891 else if (level
== 0)
1893 // always expand hidden root
1895 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1896 int count
= children
.Count();
1902 PaintLevel(children
[n
], dc
, 1, y
);
1903 } while (++n
< count
);
1905 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
1907 // draw line down to last child
1908 origY
+= GetLineHeight(children
[0])>>1;
1909 oldY
+= GetLineHeight(children
[n
-1])>>1;
1910 dc
.DrawLine(3, origY
, 3, oldY
);
1916 item
->SetX(x
+m_spacing
);
1919 int h
= GetLineHeight(item
);
1921 int y_mid
= y_top
+ (h
>>1);
1924 int exposed_x
= dc
.LogicalToDeviceX(0);
1925 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1927 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1929 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1931 if (!HasFlag(wxTR_NO_LINES
))
1933 if (x
> (signed)m_indent
)
1934 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1935 else if (HasFlag(wxTR_LINES_AT_ROOT
))
1936 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
1937 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1940 if (m_imageListButtons
!= NULL
)
1942 // draw the image button here
1943 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
1944 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
1945 if (item
->IsSelected())
1946 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
1947 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1948 int xx
= x
- (image_w
>>1);
1949 int yy
= y_mid
- (image_h
>>1);
1950 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1951 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1952 wxIMAGELIST_DRAW_TRANSPARENT
);
1953 dc
.DestroyClippingRegion();
1955 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1957 // draw the twisty button here
1958 dc
.SetPen(*wxBLACK_PEN
);
1959 dc
.SetBrush(*m_hilightBrush
);
1963 if (item
->IsExpanded())
1966 button
[0].y
= y_mid
-2;
1968 button
[1].y
= y_mid
-2;
1970 button
[2].y
= y_mid
+3;
1974 button
[0].y
= y_mid
-5;
1976 button
[1].y
= y_mid
+5;
1978 button
[2].y
= y_mid
;
1981 dc
.DrawPolygon(3, button
);
1983 dc
.SetPen(m_dottedPen
);
1985 else // if (HasFlag(wxTR_HAS_BUTTONS))
1987 // draw the plus sign here
1988 dc
.SetPen(*wxGREY_PEN
);
1989 dc
.SetBrush(*wxWHITE_BRUSH
);
1990 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
1991 dc
.SetPen(*wxBLACK_PEN
);
1992 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
1993 if (!item
->IsExpanded())
1994 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
1995 dc
.SetPen(m_dottedPen
);
1998 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2000 // draw the horizontal line here
2002 if (x
> (signed)m_indent
)
2003 x_start
-= m_indent
;
2004 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2006 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2011 // don't draw rect outline if we already have the
2012 // background color under Mac
2013 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2014 #endif // !__WXMAC__
2018 if ( item
->IsSelected() )
2020 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2024 wxTreeItemAttr
*attr
= item
->GetAttributes();
2025 if (attr
&& attr
->HasTextColour())
2026 colText
= attr
->GetTextColour();
2028 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2032 dc
.SetTextForeground(colText
);
2036 PaintItem(item
, dc
);
2038 if (HasFlag(wxTR_ROW_LINES
))
2040 // if the background colour is white, choose a
2041 // contrasting color for the lines
2042 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2043 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2044 dc
.DrawLine(0, y_top
, 10000, y_top
);
2045 dc
.DrawLine(0, y
, 10000, y
);
2048 // restore DC objects
2049 dc
.SetBrush(*wxWHITE_BRUSH
);
2050 dc
.SetPen(m_dottedPen
);
2051 dc
.SetTextForeground(*wxBLACK
);
2054 if (item
->IsExpanded())
2056 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2057 int count
= children
.Count();
2064 PaintLevel(children
[n
], dc
, level
, y
);
2065 } while (++n
< count
);
2067 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2069 // draw line down to last child
2070 oldY
+= GetLineHeight(children
[n
-1])>>1;
2071 if (HasButtons()) y_mid
+= 5;
2072 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2078 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2082 if ( item
->HasPlus() )
2084 // it's a folder, indicate it by a border
2089 // draw a line under the drop target because the item will be
2091 DrawLine(item
, TRUE
/* below */);
2094 SetCursor(wxCURSOR_BULLSEYE
);
2099 SetCursor(wxCURSOR_NO_ENTRY
);
2103 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2105 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2107 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2109 wxClientDC
dc(this);
2111 dc
.SetLogicalFunction(wxINVERT
);
2112 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2114 int w
= i
->GetWidth() + 2;
2115 int h
= GetLineHeight(i
) + 2;
2117 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2120 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2122 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2124 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2126 wxClientDC
dc(this);
2128 dc
.SetLogicalFunction(wxINVERT
);
2134 y
+= GetLineHeight(i
) - 1;
2137 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2140 // -----------------------------------------------------------------------------
2141 // wxWindows callbacks
2142 // -----------------------------------------------------------------------------
2144 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2152 dc
.SetFont( m_normalFont
);
2153 dc
.SetPen( m_dottedPen
);
2155 // this is now done dynamically
2156 //if(GetImageList() == NULL)
2157 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2160 PaintLevel( m_anchor
, dc
, 0, y
);
2163 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2172 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2181 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2183 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2184 te
.m_evtKey
= event
;
2185 te
.SetEventObject( this );
2186 if ( GetEventHandler()->ProcessEvent( te
) )
2188 // intercepted by the user code
2192 if ( (m_current
== 0) || (m_key_current
== 0) )
2198 // how should the selection work for this event?
2199 bool is_multiple
, extended_select
, unselect_others
;
2200 EventFlagsToSelType(GetWindowStyleFlag(),
2202 event
.ControlDown(),
2203 is_multiple
, extended_select
, unselect_others
);
2207 // * : Expand all/Collapse all
2208 // ' ' | return : activate
2209 // up : go up (not last children!)
2211 // left : go to parent
2212 // right : open if parent and go next
2213 // home : go to root
2214 // end : go to last item without opening parents
2215 switch (event
.KeyCode())
2219 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2227 if ( !IsExpanded(m_current
) )
2230 ExpandAll(m_current
);
2233 //else: fall through to Collapse() it
2237 if (IsExpanded(m_current
))
2239 Collapse(m_current
);
2246 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2247 event
.m_item
= (long) m_current
;
2248 event
.SetEventObject( this );
2249 GetEventHandler()->ProcessEvent( event
);
2253 // up goes to the previous sibling or to the last
2254 // of its children if it's expanded
2257 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2260 prev
= GetParent( m_key_current
);
2261 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2263 break; // don't go to root if it is hidden
2268 wxTreeItemId current
= m_key_current
;
2269 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2270 if (current
== GetFirstChild( prev
, cookie
))
2272 // otherwise we return to where we came from
2273 SelectItem( prev
, unselect_others
, extended_select
);
2274 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2275 EnsureVisible( prev
);
2282 while ( IsExpanded(prev
) && HasChildren(prev
) )
2284 wxTreeItemId child
= GetLastChild(prev
);
2291 SelectItem( prev
, unselect_others
, extended_select
);
2292 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2293 EnsureVisible( prev
);
2298 // left arrow goes to the parent
2301 wxTreeItemId prev
= GetParent( m_current
);
2302 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2304 // don't go to root if it is hidden
2305 prev
= GetPrevSibling( m_current
);
2309 EnsureVisible( prev
);
2310 SelectItem( prev
, unselect_others
, extended_select
);
2316 // this works the same as the down arrow except that we
2317 // also expand the item if it wasn't expanded yet
2323 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2326 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2327 SelectItem( child
, unselect_others
, extended_select
);
2328 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2329 EnsureVisible( child
);
2333 wxTreeItemId next
= GetNextSibling( m_key_current
);
2336 wxTreeItemId current
= m_key_current
;
2337 while (current
&& !next
)
2339 current
= GetParent( current
);
2340 if (current
) next
= GetNextSibling( current
);
2345 SelectItem( next
, unselect_others
, extended_select
);
2346 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2347 EnsureVisible( next
);
2353 // <End> selects the last visible tree item
2356 wxTreeItemId last
= GetRootItem();
2358 while ( last
.IsOk() && IsExpanded(last
) )
2360 wxTreeItemId lastChild
= GetLastChild(last
);
2362 // it may happen if the item was expanded but then all of
2363 // its children have been deleted - so IsExpanded() returned
2364 // TRUE, but GetLastChild() returned invalid item
2373 EnsureVisible( last
);
2374 SelectItem( last
, unselect_others
, extended_select
);
2379 // <Home> selects the root item
2382 wxTreeItemId prev
= GetRootItem();
2384 if (HasFlag(wxTR_HIDE_ROOT
))
2387 prev
= GetFirstChild(prev
, dummy
);
2390 EnsureVisible( prev
);
2391 SelectItem( prev
, unselect_others
, extended_select
);
2400 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2402 // JACS: removed wxYieldIfNeeded() because it can cause the window
2403 // to be deleted from under us if a close window event is pending
2408 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2409 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2410 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2411 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2412 if (flags
) return wxTreeItemId();
2414 if (m_anchor
== NULL
)
2416 flags
= wxTREE_HITTEST_NOWHERE
;
2417 return wxTreeItemId();
2420 wxClientDC
dc(this);
2422 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2423 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2424 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2430 flags
= wxTREE_HITTEST_NOWHERE
;
2431 return wxTreeItemId();
2436 // get the bounding rectangle of the item (or of its label only)
2437 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2439 bool WXUNUSED(textOnly
)) const
2441 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2443 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2446 GetViewStart(& startX
, & startY
);
2448 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2449 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2450 rect
.width
= i
->GetWidth();
2451 //rect.height = i->GetHeight();
2452 rect
.height
= GetLineHeight(i
);
2459 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2461 if (!item
.IsOk()) return;
2463 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2465 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2466 te
.m_item
= (long) m_currentEdit
;
2467 te
.SetEventObject( this );
2468 GetEventHandler()->ProcessEvent( te
);
2470 if (!te
.IsAllowed()) return;
2472 // We have to call this here because the label in
2473 // question might just have been added and no screen
2474 // update taken place.
2475 if (m_dirty
) wxYieldIfNeeded();
2477 wxString s
= m_currentEdit
->GetText();
2478 int x
= m_currentEdit
->GetX();
2479 int y
= m_currentEdit
->GetY();
2480 int w
= m_currentEdit
->GetWidth();
2481 int h
= m_currentEdit
->GetHeight();
2486 int image
= m_currentEdit
->GetCurrentImage();
2487 if ( image
!= NO_IMAGE
)
2489 if ( m_imageListNormal
)
2491 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2496 wxFAIL_MSG(_T("you must create an image list to use images!"));
2500 w
-= image_w
+ 4; // I don't know why +4 is needed
2502 wxClientDC
dc(this);
2504 x
= dc
.LogicalToDeviceX( x
);
2505 y
= dc
.LogicalToDeviceY( y
);
2507 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2517 void wxGenericTreeCtrl::OnRenameTimer()
2522 void wxGenericTreeCtrl::OnRenameAccept()
2524 // TODO if the validator fails this causes a crash
2525 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2526 le
.m_item
= (long) m_currentEdit
;
2527 le
.SetEventObject( this );
2528 le
.m_label
= m_renameRes
;
2529 GetEventHandler()->ProcessEvent( le
);
2531 if (!le
.IsAllowed()) return;
2533 SetItemText( m_currentEdit
, m_renameRes
);
2536 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2538 if ( !m_anchor
) return;
2540 // we process left mouse up event (enables in-place edit), right down
2541 // (pass to the user code), left dbl click (activate item) and
2542 // dragging/moving events for items drag-and-drop
2543 if ( !(event
.LeftDown() ||
2545 event
.RightDown() ||
2546 event
.LeftDClick() ||
2548 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2555 wxClientDC
dc(this);
2557 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2558 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2561 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2566 if ( event
.Dragging() && !m_isDragging
)
2568 if (m_dragCount
== 0)
2569 m_dragStart
= wxPoint(x
,y
);
2573 if (m_dragCount
!= 3)
2575 // wait until user drags a bit further...
2579 wxEventType command
= event
.RightIsDown()
2580 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2581 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2583 wxTreeEvent
nevent( command
, GetId() );
2584 nevent
.m_item
= (long) m_current
;
2585 nevent
.SetEventObject(this);
2587 // by default the dragging is not supported, the user code must
2588 // explicitly allow the event for it to take place
2591 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2593 // we're going to drag this item
2594 m_isDragging
= TRUE
;
2596 // remember the old cursor because we will change it while
2598 m_oldCursor
= m_cursor
;
2600 // in a single selection control, hide the selection temporarily
2601 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2603 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2605 if ( m_oldSelection
)
2607 m_oldSelection
->SetHilight(FALSE
);
2608 RefreshLine(m_oldSelection
);
2615 else if ( event
.Moving() )
2617 if ( item
!= m_dropTarget
)
2619 // unhighlight the previous drop target
2620 DrawDropEffect(m_dropTarget
);
2622 m_dropTarget
= item
;
2624 // highlight the current drop target if any
2625 DrawDropEffect(m_dropTarget
);
2630 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2632 // erase the highlighting
2633 DrawDropEffect(m_dropTarget
);
2635 if ( m_oldSelection
)
2637 m_oldSelection
->SetHilight(TRUE
);
2638 RefreshLine(m_oldSelection
);
2639 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2642 // generate the drag end event
2643 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2645 event
.m_item
= (long) item
;
2646 event
.m_pointDrag
= wxPoint(x
, y
);
2647 event
.SetEventObject(this);
2649 (void)GetEventHandler()->ProcessEvent(event
);
2651 m_isDragging
= FALSE
;
2652 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2656 SetCursor(m_oldCursor
);
2662 // here we process only the messages which happen on tree items
2666 if (item
== NULL
) return; /* we hit the blank area */
2668 if ( event
.RightDown() )
2670 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2671 nevent
.m_item
= (long) item
;
2672 CalcScrolledPosition(x
, y
,
2673 &nevent
.m_pointDrag
.x
,
2674 &nevent
.m_pointDrag
.y
);
2675 nevent
.SetEventObject(this);
2676 GetEventHandler()->ProcessEvent(nevent
);
2678 else if ( event
.LeftUp() )
2682 if ( (item
== m_current
) &&
2683 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2684 HasFlag(wxTR_EDIT_LABELS
) )
2686 if ( m_renameTimer
->IsRunning() )
2687 m_renameTimer
->Stop();
2689 m_renameTimer
->Start( 100, TRUE
);
2692 m_lastOnSame
= FALSE
;
2695 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2697 if ( event
.LeftDown() )
2699 m_lastOnSame
= item
== m_current
;
2702 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2704 // only toggle the item for a single click, double click on
2705 // the button doesn't do anything (it toggles the item twice)
2706 if ( event
.LeftDown() )
2711 // don't select the item if the button was clicked
2715 // how should the selection work for this event?
2716 bool is_multiple
, extended_select
, unselect_others
;
2717 EventFlagsToSelType(GetWindowStyleFlag(),
2719 event
.ControlDown(),
2720 is_multiple
, extended_select
, unselect_others
);
2722 SelectItem(item
, unselect_others
, extended_select
);
2724 // For some reason, Windows isn't recognizing a left double-click,
2725 // so we need to simulate it here. Allow 200 milliseconds for now.
2726 if ( event
.LeftDClick() )
2728 // double clicking should not start editing the item label
2729 m_renameTimer
->Stop();
2730 m_lastOnSame
= FALSE
;
2732 // send activate event first
2733 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2734 nevent
.m_item
= (long) item
;
2735 CalcScrolledPosition(x
, y
,
2736 &nevent
.m_pointDrag
.x
,
2737 &nevent
.m_pointDrag
.y
);
2738 nevent
.SetEventObject( this );
2739 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2741 // if the user code didn't process the activate event,
2742 // handle it ourselves by toggling the item when it is
2744 if ( item
->HasPlus() )
2754 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2756 /* after all changes have been done to the tree control,
2757 * we actually redraw the tree when everything is over */
2759 if (!m_dirty
) return;
2763 CalculatePositions();
2765 AdjustMyScrollbars();
2768 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2774 dc
.SetFont(m_boldFont
);
2776 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2779 // restore normal font
2780 dc
.SetFont( m_normalFont
);
2784 int image
= item
->GetCurrentImage();
2785 if ( image
!= NO_IMAGE
)
2787 if ( m_imageListNormal
)
2789 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2794 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2797 total_h
+= 2; // at least 2 pixels
2799 total_h
+= total_h
/10; // otherwise 10% extra spacing
2801 item
->SetHeight(total_h
);
2802 if (total_h
>m_lineHeight
)
2803 m_lineHeight
=total_h
;
2805 item
->SetWidth(image_w
+text_w
+2);
2808 // -----------------------------------------------------------------------------
2809 // for developper : y is now the top of the level
2810 // not the middle of it !
2811 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2813 int x
= level
*m_indent
;
2814 if (!HasFlag(wxTR_HIDE_ROOT
))
2818 else if (level
== 0)
2820 // a hidden root is not evaluated, but its
2821 // children are always calculated
2825 CalculateSize( item
, dc
);
2828 item
->SetX( x
+m_spacing
);
2830 y
+= GetLineHeight(item
);
2832 if ( !item
->IsExpanded() )
2834 // we don't need to calculate collapsed branches
2839 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2840 size_t n
, count
= children
.Count();
2842 for (n
= 0; n
< count
; ++n
)
2843 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2846 void wxGenericTreeCtrl::CalculatePositions()
2848 if ( !m_anchor
) return;
2850 wxClientDC
dc(this);
2853 dc
.SetFont( m_normalFont
);
2855 dc
.SetPen( m_dottedPen
);
2856 //if(GetImageList() == NULL)
2857 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2860 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2863 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2865 if (m_dirty
) return;
2867 wxClientDC
dc(this);
2872 GetClientSize( &cw
, &ch
);
2875 rect
.x
= dc
.LogicalToDeviceX( 0 );
2877 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2880 Refresh( TRUE
, &rect
);
2882 AdjustMyScrollbars();
2885 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2887 if (m_dirty
) return;
2889 wxClientDC
dc(this);
2894 GetClientSize( &cw
, &ch
);
2897 rect
.x
= dc
.LogicalToDeviceX( 0 );
2898 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2900 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2902 Refresh( TRUE
, &rect
);
2905 void wxGenericTreeCtrl::RefreshSelected()
2907 // TODO: this is awfully inefficient, we should keep the list of all
2908 // selected items internally, should be much faster
2910 RefreshSelectedUnder(m_anchor
);
2913 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2915 if ( item
->IsSelected() )
2918 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2919 size_t count
= children
.GetCount();
2920 for ( size_t n
= 0; n
< count
; n
++ )
2922 RefreshSelectedUnder(children
[n
]);
2926 // ----------------------------------------------------------------------------
2927 // changing colours: we need to refresh the tree control
2928 // ----------------------------------------------------------------------------
2930 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
2932 if ( !wxWindow::SetBackgroundColour(colour
) )
2940 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
2942 if ( !wxWindow::SetForegroundColour(colour
) )
2950 #endif // wxUSE_TREECTRL