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
,
114 wxTreeItemData
*data
);
116 ~wxGenericTreeItem();
119 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
121 const wxString
& GetText() const { return m_text
; }
122 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
123 { return m_images
[which
]; }
124 wxTreeItemData
*GetData() const { return m_data
; }
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
130 void SetText( const wxString
&text
);
131 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
136 void SetBold(bool bold
) { m_isBold
= bold
; }
138 int GetX() const { return m_x
; }
139 int GetY() const { return m_y
; }
141 void SetX(int x
) { m_x
= x
; }
142 void SetY(int y
) { m_y
= y
; }
144 int GetHeight() const { return m_height
; }
145 int GetWidth() const { return m_width
; }
147 void SetHeight(int h
) { m_height
= h
; }
148 void SetWidth(int w
) { m_width
= w
; }
150 wxGenericTreeItem
*GetParent() const { return m_parent
; }
153 // deletes all children notifying the treectrl about it if !NULL
155 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
157 // get count of all children (and grand children if 'recursively')
158 size_t GetChildrenCount(bool recursively
= TRUE
) const;
160 void Insert(wxGenericTreeItem
*child
, size_t index
)
161 { m_children
.Insert(child
, index
); }
163 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
165 // return the item at given position (or NULL if no item), onButton is
166 // TRUE if the point belongs to the item's button, otherwise it lies
167 // on the button's label
168 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
169 const wxGenericTreeCtrl
*,
173 void Expand() { m_isCollapsed
= FALSE
; }
174 void Collapse() { m_isCollapsed
= TRUE
; }
176 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
179 bool HasChildren() const { return !m_children
.IsEmpty(); }
180 bool IsSelected() const { return m_hasHilight
!= 0; }
181 bool IsExpanded() const { return !m_isCollapsed
; }
182 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
183 bool IsBold() const { return m_isBold
!= 0; }
186 // get them - may be NULL
187 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
188 // get them ensuring that the pointer is not NULL
189 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
199 void SetAttributes(wxTreeItemAttr
*attr
)
201 if ( m_ownsAttr
) delete m_attr
;
205 // set them and delete when done
206 void AssignAttributes(wxTreeItemAttr
*attr
)
213 // since there can be very many of these, we save size by chosing
214 // the smallest representation for the elements and by ordering
215 // the members to avoid padding.
216 wxString m_text
; // label to be rendered for item
218 wxTreeItemData
*m_data
; // user-provided data
220 wxArrayGenericTreeItems m_children
; // list of children
221 wxGenericTreeItem
*m_parent
; // parent of this item
223 wxTreeItemAttr
*m_attr
; // attributes???
225 // tree ctrl images for the normal, selected, expanded and
226 // expanded+selected states
227 short m_images
[wxTreeItemIcon_Max
];
229 wxCoord m_x
; // (virtual) offset from top
230 short m_y
; // (virtual) offset from left
231 short m_width
; // width of this item
232 unsigned char m_height
; // height of this item
234 // use bitfields to save size
235 int m_isCollapsed
:1;
236 int m_hasHilight
:1; // same as focused
237 int m_hasPlus
:1; // used for item which doesn't have
238 // children but has a [+] button
239 int m_isBold
:1; // render the label in bold font
240 int m_ownsAttr
:1; // delete attribute when done
243 // =============================================================================
245 // =============================================================================
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // translate the key or mouse event flags to the type of selection we're
253 static void EventFlagsToSelType(long style
,
257 bool &extended_select
,
258 bool &unselect_others
)
260 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
261 extended_select
= shiftDown
&& is_multiple
;
262 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
265 // -----------------------------------------------------------------------------
266 // wxTreeRenameTimer (internal)
267 // -----------------------------------------------------------------------------
269 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
274 void wxTreeRenameTimer::Notify()
276 m_owner
->OnRenameTimer();
279 //-----------------------------------------------------------------------------
280 // wxTreeTextCtrl (internal)
281 //-----------------------------------------------------------------------------
283 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
284 EVT_CHAR (wxTreeTextCtrl::OnChar
)
285 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
286 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
289 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
293 wxGenericTreeCtrl
*owner
,
294 const wxString
&value
,
298 const wxValidator
& validator
,
299 const wxString
&name
)
300 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
306 (*m_res
) = wxEmptyString
;
307 m_startValue
= value
;
310 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
312 // TODO focus doesn't return to the wxTextCtrl when this closes...
313 if (event
.m_keyCode
== WXK_RETURN
)
316 (*m_res
) = GetValue();
318 if ((*m_accept
) && ((*m_res
) != m_startValue
))
319 m_owner
->OnRenameAccept();
321 if (!wxPendingDelete
.Member(this))
322 wxPendingDelete
.Append(this);
326 if (event
.m_keyCode
== WXK_ESCAPE
)
331 if (!wxPendingDelete
.Member(this))
332 wxPendingDelete
.Append(this);
339 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
341 // auto-grow the textctrl:
342 wxSize parentSize
= m_owner
->GetSize();
343 wxPoint myPos
= GetPosition();
344 wxSize mySize
= GetSize();
346 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
347 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
348 if (mySize
.x
> sx
) sx
= mySize
.x
;
354 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
356 if (!wxPendingDelete
.Member(this))
357 wxPendingDelete
.Append(this);
359 if ((*m_accept
) && ((*m_res
) != m_startValue
))
360 m_owner
->OnRenameAccept();
363 // -----------------------------------------------------------------------------
365 // -----------------------------------------------------------------------------
367 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
368 const wxString
& text
,
370 int image
, int selImage
,
371 wxTreeItemData
*data
)
374 m_images
[wxTreeItemIcon_Normal
] = image
;
375 m_images
[wxTreeItemIcon_Selected
] = selImage
;
376 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
377 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
382 m_isCollapsed
= TRUE
;
383 m_hasHilight
= FALSE
;
389 m_attr
= (wxTreeItemAttr
*)NULL
;
392 // We don't know the height here yet.
397 wxGenericTreeItem::~wxGenericTreeItem()
401 if (m_ownsAttr
) delete m_attr
;
403 wxASSERT_MSG( m_children
.IsEmpty(),
404 wxT("please call DeleteChildren() before deleting the item") );
407 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
409 size_t count
= m_children
.Count();
410 for ( size_t n
= 0; n
< count
; n
++ )
412 wxGenericTreeItem
*child
= m_children
[n
];
414 tree
->SendDeleteEvent(child
);
416 child
->DeleteChildren(tree
);
423 void wxGenericTreeItem::SetText( const wxString
&text
)
428 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
430 size_t count
= m_children
.Count();
434 size_t total
= count
;
435 for (size_t n
= 0; n
< count
; ++n
)
437 total
+= m_children
[n
]->GetChildrenCount();
443 void wxGenericTreeItem::GetSize( int &x
, int &y
,
444 const wxGenericTreeCtrl
*theButton
)
446 int bottomY
=m_y
+theButton
->GetLineHeight(this);
447 if ( y
< bottomY
) y
= bottomY
;
448 int width
= m_x
+ m_width
;
449 if ( x
< width
) x
= width
;
453 size_t count
= m_children
.Count();
454 for ( size_t n
= 0; n
< count
; ++n
)
456 m_children
[n
]->GetSize( x
, y
, theButton
);
461 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
462 const wxGenericTreeCtrl
*theCtrl
,
466 // for a hidden root node, don't evaluate it, but do evaluate children
467 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
470 int h
= theCtrl
->GetLineHeight(this);
471 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
473 int y_mid
= m_y
+ h
/2;
474 if (point
.y
< y_mid
)
475 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
477 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
479 // 5 is the size of the plus sign
480 int xCross
= m_x
- theCtrl
->GetSpacing();
481 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
482 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
483 HasPlus() && theCtrl
->HasButtons() )
485 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
489 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
494 // assuming every image (normal and selected) has the same size!
495 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
496 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
499 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
500 flags
|= wxTREE_HITTEST_ONITEMICON
;
502 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
508 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
509 if (point
.x
> m_x
+m_width
)
510 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
515 // if children are expanded, fall through to evaluate them
516 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
520 size_t count
= m_children
.Count();
521 for ( size_t n
= 0; n
< count
; n
++ )
523 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
531 return (wxGenericTreeItem
*) NULL
;
534 int wxGenericTreeItem::GetCurrentImage() const
536 int image
= NO_IMAGE
;
541 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
544 if ( image
== NO_IMAGE
)
546 // we usually fall back to the normal item, but try just the
547 // expanded one (and not selected) first in this case
548 image
= GetImage(wxTreeItemIcon_Expanded
);
554 image
= GetImage(wxTreeItemIcon_Selected
);
557 // maybe it doesn't have the specific image we want,
558 // try the default one instead
559 if ( image
== NO_IMAGE
) image
= GetImage();
564 // -----------------------------------------------------------------------------
565 // wxGenericTreeCtrl implementation
566 // -----------------------------------------------------------------------------
568 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
570 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
571 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
572 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
573 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
574 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
575 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
576 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
579 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
581 * wxTreeCtrl has to be a real class or we have problems with
582 * the run-time information.
585 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
588 // -----------------------------------------------------------------------------
589 // construction/destruction
590 // -----------------------------------------------------------------------------
592 void wxGenericTreeCtrl::Init()
594 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
602 m_hilightBrush
= new wxBrush
604 wxSystemSettings::GetSystemColour
606 wxSYS_COLOUR_HIGHLIGHT
611 m_hilightUnfocusedBrush
= new wxBrush
613 wxSystemSettings::GetSystemColour
615 wxSYS_COLOUR_BTNSHADOW
620 m_imageListNormal
= m_imageListButtons
=
621 m_imageListState
= (wxImageList
*) NULL
;
622 m_ownsImageListNormal
= m_ownsImageListButtons
=
623 m_ownsImageListState
= FALSE
;
626 m_isDragging
= FALSE
;
627 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
629 m_renameTimer
= new wxTreeRenameTimer( this );
630 m_lastOnSame
= FALSE
;
632 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
633 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
634 m_normalFont
.GetFamily(),
635 m_normalFont
.GetStyle(),
637 m_normalFont
.GetUnderlined());
640 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
645 const wxValidator
&validator
,
646 const wxString
& name
)
648 wxScrolledWindow::Create( parent
, id
, pos
, size
,
649 style
|wxHSCROLL
|wxVSCROLL
, name
);
651 // If the tree display has no buttons, but does have
652 // connecting lines, we can use a narrower layout.
653 // It may not be a good idea to force this...
654 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
661 SetValidator( validator
);
664 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
666 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
667 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
672 wxGenericTreeCtrl::~wxGenericTreeCtrl()
674 delete m_hilightBrush
;
675 delete m_hilightUnfocusedBrush
;
679 delete m_renameTimer
;
680 if (m_ownsImageListNormal
) delete m_imageListNormal
;
681 if (m_ownsImageListState
) delete m_imageListState
;
682 if (m_ownsImageListButtons
) delete m_imageListButtons
;
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 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
715 // right now, just sets the styles. Eventually, we may
716 // want to update the inherited styles, but right now
717 // none of the parents has updatable styles
718 m_windowStyle
= styles
;
722 // -----------------------------------------------------------------------------
723 // functions to work with tree items
724 // -----------------------------------------------------------------------------
726 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
728 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
730 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
733 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
734 wxTreeItemIcon which
) const
736 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
738 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
741 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
743 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
745 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
748 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
750 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
753 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
754 pItem
->SetText(text
);
755 CalculateSize(pItem
, dc
);
759 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
761 wxTreeItemIcon which
)
763 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
765 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
766 pItem
->SetImage(image
, which
);
769 CalculateSize(pItem
, dc
);
773 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
775 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
777 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
780 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
782 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
784 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
785 pItem
->SetHasPlus(has
);
789 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
791 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
793 // avoid redrawing the tree if no real change
794 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
795 if ( pItem
->IsBold() != bold
)
797 pItem
->SetBold(bold
);
802 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
805 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
807 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
808 pItem
->Attr().SetTextColour(col
);
812 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
815 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
817 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
818 pItem
->Attr().SetBackgroundColour(col
);
822 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
824 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
826 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
827 pItem
->Attr().SetFont(font
);
831 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
833 wxScrolledWindow::SetFont(font
);
835 m_normalFont
= font
;
836 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
837 m_normalFont
.GetFamily(),
838 m_normalFont
.GetStyle(),
840 m_normalFont
.GetUnderlined());
846 // -----------------------------------------------------------------------------
847 // item status inquiries
848 // -----------------------------------------------------------------------------
850 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
852 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
854 // An item is only visible if it's not a descendant of a collapsed item
855 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
856 wxGenericTreeItem
* parent
= pItem
->GetParent();
859 if (!parent
->IsExpanded())
861 parent
= parent
->GetParent();
865 GetViewStart(& startX
, & startY
);
867 wxSize clientSize
= GetClientSize();
870 if (!GetBoundingRect(item
, rect
))
872 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
874 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
876 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
882 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
884 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
886 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
889 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
891 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
893 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
896 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
898 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
900 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
903 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
905 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
907 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
910 // -----------------------------------------------------------------------------
912 // -----------------------------------------------------------------------------
914 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
916 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
918 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
921 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
923 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
926 return GetNextChild(item
, cookie
);
929 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
931 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
933 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
934 if ( (size_t)cookie
< children
.Count() )
936 return children
.Item((size_t)cookie
++);
940 // there are no more of them
941 return wxTreeItemId();
945 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
947 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
949 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
950 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
953 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
955 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
957 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
958 wxGenericTreeItem
*parent
= i
->GetParent();
959 if ( parent
== NULL
)
961 // root item doesn't have any siblings
962 return wxTreeItemId();
965 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
966 int index
= siblings
.Index(i
);
967 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
969 size_t n
= (size_t)(index
+ 1);
970 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
973 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
975 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
977 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
978 wxGenericTreeItem
*parent
= i
->GetParent();
979 if ( parent
== NULL
)
981 // root item doesn't have any siblings
982 return wxTreeItemId();
985 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
986 int index
= siblings
.Index(i
);
987 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
989 return index
== 0 ? wxTreeItemId()
990 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
993 // Only for internal use right now, but should probably be public
994 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
996 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
998 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1000 // First see if there are any children.
1001 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1002 if (children
.GetCount() > 0)
1004 return children
.Item(0);
1008 // Try a sibling of this or ancestor instead
1009 wxTreeItemId p
= item
;
1010 wxTreeItemId toFind
;
1013 toFind
= GetNextSibling(p
);
1015 } while (p
.IsOk() && !toFind
.IsOk());
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
;
1043 while (id
= GetNext(id
), id
.IsOk())
1049 return wxTreeItemId();
1052 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1054 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1056 wxFAIL_MSG(wxT("not implemented"));
1058 return wxTreeItemId();
1061 // -----------------------------------------------------------------------------
1063 // -----------------------------------------------------------------------------
1065 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1067 const wxString
& text
,
1068 int image
, int selImage
,
1069 wxTreeItemData
*data
)
1071 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1074 // should we give a warning here?
1075 return AddRoot(text
, image
, selImage
, data
);
1078 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1080 wxClientDC
dc(this);
1081 wxGenericTreeItem
*item
=
1082 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1086 data
->m_pItem
= (long) item
;
1089 parent
->Insert( item
, previous
);
1094 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1095 int image
, int selImage
,
1096 wxTreeItemData
*data
)
1098 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1100 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1102 wxClientDC
dc(this);
1103 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1104 image
, selImage
, data
);
1105 if (HasFlag(wxTR_HIDE_ROOT
))
1107 // if root is hidden, make sure we can navigate
1109 m_anchor
->SetHasPlus();
1114 data
->m_pItem
= (long) m_anchor
;
1117 if (!HasFlag(wxTR_MULTIPLE
))
1119 m_current
= m_key_current
= m_anchor
;
1120 m_current
->SetHilight( TRUE
);
1126 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1127 const wxString
& text
,
1128 int image
, int selImage
,
1129 wxTreeItemData
*data
)
1131 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1134 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1135 const wxTreeItemId
& idPrevious
,
1136 const wxString
& text
,
1137 int image
, int selImage
,
1138 wxTreeItemData
*data
)
1140 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1143 // should we give a warning here?
1144 return AddRoot(text
, image
, selImage
, data
);
1147 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1148 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1149 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1151 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1154 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1156 const wxString
& text
,
1157 int image
, int selImage
,
1158 wxTreeItemData
*data
)
1160 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1163 // should we give a warning here?
1164 return AddRoot(text
, image
, selImage
, data
);
1167 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1170 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1171 const wxString
& text
,
1172 int image
, int selImage
,
1173 wxTreeItemData
*data
)
1175 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1178 // should we give a warning here?
1179 return AddRoot(text
, image
, selImage
, data
);
1182 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1183 image
, selImage
, data
);
1186 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1188 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1189 event
.m_item
= (long) item
;
1190 event
.SetEventObject( this );
1191 ProcessEvent( event
);
1194 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1196 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1198 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1199 item
->DeleteChildren(this);
1202 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1204 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1206 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1208 // don't stay with invalid m_key_current or we will crash in
1209 // the next call to OnChar()
1210 bool changeKeyCurrent
= FALSE
;
1211 wxGenericTreeItem
*itemKey
= m_key_current
;
1214 if ( itemKey
== item
)
1216 // m_key_current is a descendant of the item being deleted
1217 changeKeyCurrent
= TRUE
;
1220 itemKey
= itemKey
->GetParent();
1223 wxGenericTreeItem
*parent
= item
->GetParent();
1226 parent
->GetChildren().Remove( item
); // remove by value
1229 if ( changeKeyCurrent
)
1231 // may be NULL or not
1232 m_key_current
= parent
;
1235 item
->DeleteChildren(this);
1236 SendDeleteEvent(item
);
1240 void wxGenericTreeCtrl::DeleteAllItems()
1246 m_anchor
->DeleteChildren(this);
1253 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1255 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1257 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1259 if ( !item
->HasPlus() )
1262 if ( item
->IsExpanded() )
1265 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1266 event
.m_item
= (long) item
;
1267 event
.SetEventObject( this );
1269 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1271 // cancelled by program
1276 CalculatePositions();
1278 RefreshSubtree(item
);
1280 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1281 ProcessEvent( event
);
1284 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1287 if ( IsExpanded(item
) )
1290 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1291 while ( child
.IsOk() )
1295 child
= GetNextChild(item
, cookie
);
1300 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1302 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1304 if ( !item
->IsExpanded() )
1307 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1308 event
.m_item
= (long) item
;
1309 event
.SetEventObject( this );
1310 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1312 // cancelled by program
1318 #if 0 // TODO why should items be collapsed recursively?
1319 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1320 size_t count
= children
.Count();
1321 for ( size_t n
= 0; n
< count
; n
++ )
1323 Collapse(children
[n
]);
1327 CalculatePositions();
1329 RefreshSubtree(item
);
1331 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1332 ProcessEvent( event
);
1335 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1338 DeleteChildren(item
);
1341 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1343 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1345 if (item
->IsExpanded())
1351 void wxGenericTreeCtrl::Unselect()
1355 m_current
->SetHilight( FALSE
);
1356 RefreshLine( m_current
);
1360 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1362 if (item
->IsSelected())
1364 item
->SetHilight(FALSE
);
1368 if (item
->HasChildren())
1370 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1371 size_t count
= children
.Count();
1372 for ( size_t n
= 0; n
< count
; ++n
)
1374 UnselectAllChildren(children
[n
]);
1379 void wxGenericTreeCtrl::UnselectAll()
1381 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1384 // Recursive function !
1385 // To stop we must have crt_item<last_item
1387 // Tag all next children, when no more children,
1388 // Move to parent (not to tag)
1389 // Keep going... if we found last_item, we stop.
1390 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1392 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1394 if (parent
== NULL
) // This is root item
1395 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1397 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1398 int index
= children
.Index(crt_item
);
1399 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1401 size_t count
= children
.Count();
1402 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1404 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1407 return TagNextChildren(parent
, last_item
, select
);
1410 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1412 crt_item
->SetHilight(select
);
1413 RefreshLine(crt_item
);
1415 if (crt_item
==last_item
)
1418 if (crt_item
->HasChildren())
1420 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1421 size_t count
= children
.Count();
1422 for ( size_t n
= 0; n
< count
; ++n
)
1424 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1432 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1434 // item2 is not necessary after item1
1435 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1437 // choice first' and 'last' between item1 and item2
1438 if (item1
->GetY()<item2
->GetY())
1449 bool select
= m_current
->IsSelected();
1451 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1454 TagNextChildren(first
,last
,select
);
1457 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1458 bool unselect_others
,
1459 bool extended_select
)
1461 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1463 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1464 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1466 //wxCHECK_RET( ( (!unselect_others) && is_single),
1467 // wxT("this is a single selection tree") );
1469 // to keep going anyhow !!!
1472 if (item
->IsSelected())
1473 return; // nothing to do
1474 unselect_others
= TRUE
;
1475 extended_select
= FALSE
;
1477 else if ( unselect_others
&& item
->IsSelected() )
1479 // selection change if there is more than one item currently selected
1480 wxArrayTreeItemIds selected_items
;
1481 if ( GetSelections(selected_items
) == 1 )
1485 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1486 event
.m_item
= (long) item
;
1487 event
.m_itemOld
= (long) m_current
;
1488 event
.SetEventObject( this );
1489 // TODO : Here we don't send any selection mode yet !
1491 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1494 wxTreeItemId parent
= GetParent( itemId
);
1495 while (parent
.IsOk())
1497 if (!IsExpanded(parent
))
1500 parent
= GetParent( parent
);
1503 EnsureVisible( itemId
);
1506 if (unselect_others
)
1508 if (is_single
) Unselect(); // to speed up thing
1513 if (extended_select
)
1517 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1520 // don't change the mark (m_current)
1521 SelectItemRange(m_current
, item
);
1525 bool select
=TRUE
; // the default
1527 // Check if we need to toggle hilight (ctrl mode)
1528 if (!unselect_others
)
1529 select
=!item
->IsSelected();
1531 m_current
= m_key_current
= item
;
1532 m_current
->SetHilight(select
);
1533 RefreshLine( m_current
);
1536 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1537 GetEventHandler()->ProcessEvent( event
);
1540 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1541 wxArrayTreeItemIds
&array
) const
1543 if ( item
->IsSelected() )
1544 array
.Add(wxTreeItemId(item
));
1546 if ( item
->HasChildren() )
1548 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1549 size_t count
= children
.GetCount();
1550 for ( size_t n
= 0; n
< count
; ++n
)
1551 FillArray(children
[n
], array
);
1555 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1558 wxTreeItemId idRoot
= GetRootItem();
1559 if ( idRoot
.IsOk() )
1561 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1563 //else: the tree is empty, so no selections
1565 return array
.Count();
1568 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1570 if (!item
.IsOk()) return;
1572 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1574 // first expand all parent branches
1575 wxGenericTreeItem
*parent
= gitem
->GetParent();
1579 parent
= parent
->GetParent();
1582 //if (parent) CalculatePositions();
1587 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1589 if (!item
.IsOk()) return;
1591 // We have to call this here because the label in
1592 // question might just have been added and no screen
1593 // update taken place.
1594 if (m_dirty
) wxYieldIfNeeded();
1596 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1598 // now scroll to the item
1599 int item_y
= gitem
->GetY();
1603 GetViewStart( &start_x
, &start_y
);
1604 start_y
*= PIXELS_PER_UNIT
;
1608 GetClientSize( &client_w
, &client_h
);
1610 if (item_y
< start_y
+3)
1615 m_anchor
->GetSize( x
, y
, this );
1616 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1617 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1618 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1619 // Item should appear at top
1620 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1622 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1627 m_anchor
->GetSize( x
, y
, this );
1628 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1629 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1630 item_y
+= PIXELS_PER_UNIT
+2;
1631 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1632 // Item should appear at bottom
1633 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
);
1637 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1638 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1640 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1641 wxGenericTreeItem
**item2
)
1643 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1645 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1648 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1649 const wxTreeItemId
& item2
)
1651 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1654 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1656 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1658 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1660 wxCHECK_RET( !s_treeBeingSorted
,
1661 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1663 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1664 if ( children
.Count() > 1 )
1668 s_treeBeingSorted
= this;
1669 children
.Sort(tree_ctrl_compare_func
);
1670 s_treeBeingSorted
= NULL
;
1672 //else: don't make the tree dirty as nothing changed
1675 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1677 return m_imageListNormal
;
1680 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1682 return m_imageListButtons
;
1685 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1687 return m_imageListState
;
1690 void wxGenericTreeCtrl::CalculateLineHeight()
1692 wxClientDC
dc(this);
1693 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1695 if ( m_imageListNormal
)
1697 // Calculate a m_lineHeight value from the normal Image sizes.
1698 // May be toggle off. Then wxGenericTreeCtrl will spread when
1699 // necessary (which might look ugly).
1700 int n
= m_imageListNormal
->GetImageCount();
1701 for (int i
= 0; i
< n
; i
++)
1703 int width
= 0, height
= 0;
1704 m_imageListNormal
->GetSize(i
, width
, height
);
1705 if (height
> m_lineHeight
) m_lineHeight
= height
;
1709 if (m_imageListButtons
)
1711 // Calculate a m_lineHeight value from the Button image sizes.
1712 // May be toggle off. Then wxGenericTreeCtrl will spread when
1713 // necessary (which might look ugly).
1714 int n
= m_imageListButtons
->GetImageCount();
1715 for (int i
= 0; i
< n
; i
++)
1717 int width
= 0, height
= 0;
1718 m_imageListButtons
->GetSize(i
, width
, height
);
1719 if (height
> m_lineHeight
) m_lineHeight
= height
;
1723 if (m_lineHeight
< 30)
1724 m_lineHeight
+= 2; // at least 2 pixels
1726 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1729 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1731 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1732 m_imageListNormal
= imageList
;
1733 m_ownsImageListNormal
= FALSE
;
1735 CalculateLineHeight();
1738 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1740 if (m_ownsImageListState
) delete m_imageListState
;
1741 m_imageListState
= imageList
;
1742 m_ownsImageListState
= FALSE
;
1745 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1747 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1748 m_imageListButtons
= imageList
;
1749 m_ownsImageListButtons
= FALSE
;
1751 CalculateLineHeight();
1754 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1756 SetImageList(imageList
);
1757 m_ownsImageListNormal
= TRUE
;
1760 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1762 SetStateImageList(imageList
);
1763 m_ownsImageListState
= TRUE
;
1766 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1768 SetButtonsImageList(imageList
);
1769 m_ownsImageListButtons
= TRUE
;
1772 // -----------------------------------------------------------------------------
1774 // -----------------------------------------------------------------------------
1776 void wxGenericTreeCtrl::AdjustMyScrollbars()
1781 m_anchor
->GetSize( x
, y
, this );
1782 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1783 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1784 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1785 int y_pos
= GetScrollPos( wxVERTICAL
);
1786 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1790 SetScrollbars( 0, 0, 0, 0 );
1794 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1796 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1797 return item
->GetHeight();
1799 return m_lineHeight
;
1802 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1804 // TODO implement "state" icon on items
1806 wxTreeItemAttr
*attr
= item
->GetAttributes();
1807 if ( attr
&& attr
->HasFont() )
1808 dc
.SetFont(attr
->GetFont());
1809 else if (item
->IsBold())
1810 dc
.SetFont(m_boldFont
);
1812 long text_w
= 0, text_h
= 0;
1813 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1815 int image_h
= 0, image_w
= 0;
1816 int image
= item
->GetCurrentImage();
1817 if ( image
!= NO_IMAGE
)
1819 if ( m_imageListNormal
)
1821 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1830 int total_h
= GetLineHeight(item
);
1832 if ( item
->IsSelected() )
1834 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1839 if ( attr
&& attr
->HasBackgroundColour() )
1840 colBg
= attr
->GetBackgroundColour();
1842 colBg
= m_backgroundColour
;
1843 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1846 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1848 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1850 // If it's selected, and there's an image, then we should
1851 // take care to leave the area under the image painted in the
1852 // background colour.
1853 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1854 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1858 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1859 item
->GetWidth()+2, total_h
-offset
);
1862 if ( image
!= NO_IMAGE
)
1864 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1865 m_imageListNormal
->Draw( image
, dc
,
1867 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1868 wxIMAGELIST_DRAW_TRANSPARENT
);
1869 dc
.DestroyClippingRegion();
1872 dc
.SetBackgroundMode(wxTRANSPARENT
);
1873 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1874 dc
.DrawText( item
->GetText(),
1875 (wxCoord
)(image_w
+ item
->GetX()),
1876 (wxCoord
)(item
->GetY() + extraH
));
1878 // restore normal font
1879 dc
.SetFont( m_normalFont
);
1882 // Now y stands for the top of the item, whereas it used to stand for middle !
1883 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1885 int x
= level
*m_indent
;
1886 if (!HasFlag(wxTR_HIDE_ROOT
))
1890 else if (level
== 0)
1892 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1893 size_t n
, count
= children
.Count();
1894 for (n
= 0; n
< count
; ++n
)
1895 PaintLevel(children
[n
], dc
, 1, y
);
1899 item
->SetX(x
+m_spacing
);
1902 int h
= GetLineHeight(item
);
1904 int y_mid
= y_top
+ (h
>>1);
1907 int exposed_x
= dc
.LogicalToDeviceX(0);
1908 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1910 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1912 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1914 if (!HasFlag(wxTR_NO_LINES
))
1916 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
1917 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1918 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1921 if (m_imageListButtons
!= NULL
)
1923 // draw the image button here
1924 int image_h
= 0, image_w
= 0, image
= wxCLOSED_BUTTON
;
1925 if (item
->IsExpanded()) image
= wxOPEN_BUTTON
;
1926 if (item
->IsSelected())
1927 image
+= wxOPEN_BUTTON_SELECTED
- wxOPEN_BUTTON
;
1928 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1929 int xx
= x
- (image_w
>>1);
1930 int yy
= y_mid
- (image_h
>>1);
1931 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1932 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1933 wxIMAGELIST_DRAW_TRANSPARENT
);
1934 dc
.DestroyClippingRegion();
1936 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1938 // draw the twisty button here
1939 dc
.SetPen(*wxBLACK_PEN
);
1940 dc
.SetBrush(*m_hilightBrush
);
1944 if (item
->IsExpanded())
1947 button
[0].y
= y_mid
-2;
1949 button
[1].y
= y_mid
-2;
1951 button
[2].y
= y_mid
+3;
1955 button
[0].y
= y_mid
-5;
1957 button
[1].y
= y_mid
+5;
1959 button
[2].y
= y_mid
;
1962 dc
.DrawPolygon(3, button
);
1964 dc
.SetPen(m_dottedPen
);
1966 else // if (HasFlag(wxTR_HAS_BUTTONS))
1968 // draw the plus sign here
1969 dc
.SetPen(*wxGREY_PEN
);
1970 dc
.SetBrush(*wxWHITE_BRUSH
);
1971 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
1972 dc
.SetPen(*wxBLACK_PEN
);
1973 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
1974 if (!item
->IsExpanded())
1975 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
1976 dc
.SetPen(m_dottedPen
);
1979 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
1981 // draw the horizontal line here
1983 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
1984 x_start
-= m_indent
;
1985 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
1990 // don't draw rect outline if we already have the
1991 // background color under Mac
1992 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
1993 #endif // !__WXMAC__
1997 if ( item
->IsSelected() )
1999 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2003 wxTreeItemAttr
*attr
= item
->GetAttributes();
2004 if (attr
&& attr
->HasTextColour())
2005 colText
= attr
->GetTextColour();
2007 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2011 dc
.SetTextForeground(colText
);
2015 PaintItem(item
, dc
);
2017 if (HasFlag(wxTR_ROW_LINES
))
2019 dc
.SetPen(*wxWHITE_PEN
);
2020 dc
.DrawLine(0, y_top
, 10000, y_top
);
2021 dc
.DrawLine(0, y
, 10000, y
);
2024 // restore DC objects
2025 dc
.SetBrush(*wxWHITE_BRUSH
);
2026 dc
.SetPen(m_dottedPen
);
2027 dc
.SetTextForeground(*wxBLACK
);
2030 if (item
->IsExpanded())
2032 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2033 int count
= children
.Count();
2040 PaintLevel(children
[n
], dc
, level
, y
);
2041 } while (++n
< count
);
2043 if (!HasFlag(wxTR_NO_LINES
))
2045 // draw line down to last child
2046 oldY
+= GetLineHeight(children
[n
-1])/2;
2047 if (HasButtons()) y_mid
+= 5;
2048 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2054 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2058 if ( item
->HasPlus() )
2060 // it's a folder, indicate it by a border
2065 // draw a line under the drop target because the item will be
2067 DrawLine(item
, TRUE
/* below */);
2070 SetCursor(wxCURSOR_BULLSEYE
);
2075 SetCursor(wxCURSOR_NO_ENTRY
);
2079 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2081 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2083 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2085 wxClientDC
dc(this);
2087 dc
.SetLogicalFunction(wxINVERT
);
2088 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2090 int w
= i
->GetWidth() + 2;
2091 int h
= GetLineHeight(i
) + 2;
2093 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2096 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2098 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2100 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2102 wxClientDC
dc(this);
2104 dc
.SetLogicalFunction(wxINVERT
);
2110 y
+= GetLineHeight(i
) - 1;
2113 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2116 // -----------------------------------------------------------------------------
2117 // wxWindows callbacks
2118 // -----------------------------------------------------------------------------
2120 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2128 dc
.SetFont( m_normalFont
);
2129 dc
.SetPen( m_dottedPen
);
2131 // this is now done dynamically
2132 //if(GetImageList() == NULL)
2133 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2136 PaintLevel( m_anchor
, dc
, 0, y
);
2139 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2148 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2157 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2159 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2160 te
.m_evtKey
= event
;
2161 te
.SetEventObject( this );
2162 if ( GetEventHandler()->ProcessEvent( te
) )
2164 // intercepted by the user code
2168 if ( (m_current
== 0) || (m_key_current
== 0) )
2174 // how should the selection work for this event?
2175 bool is_multiple
, extended_select
, unselect_others
;
2176 EventFlagsToSelType(GetWindowStyleFlag(),
2178 event
.ControlDown(),
2179 is_multiple
, extended_select
, unselect_others
);
2183 // * : Expand all/Collapse all
2184 // ' ' | return : activate
2185 // up : go up (not last children!)
2187 // left : go to parent
2188 // right : open if parent and go next
2189 // home : go to root
2190 // end : go to last item without opening parents
2191 switch (event
.KeyCode())
2195 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2203 if ( !IsExpanded(m_current
) )
2206 ExpandAll(m_current
);
2209 //else: fall through to Collapse() it
2213 if (IsExpanded(m_current
))
2215 Collapse(m_current
);
2222 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2223 event
.m_item
= (long) m_current
;
2224 event
.SetEventObject( this );
2225 GetEventHandler()->ProcessEvent( event
);
2229 // up goes to the previous sibling or to the last
2230 // of its children if it's expanded
2233 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2236 prev
= GetParent( m_key_current
);
2237 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2239 break; // don't go to root if it is hidden
2244 wxTreeItemId current
= m_key_current
;
2245 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2246 if (current
== GetFirstChild( prev
, cookie
))
2248 // otherwise we return to where we came from
2249 SelectItem( prev
, unselect_others
, extended_select
);
2250 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2251 EnsureVisible( prev
);
2258 while ( IsExpanded(prev
) && HasChildren(prev
) )
2260 wxTreeItemId child
= GetLastChild(prev
);
2267 SelectItem( prev
, unselect_others
, extended_select
);
2268 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2269 EnsureVisible( prev
);
2274 // left arrow goes to the parent
2277 wxTreeItemId prev
= GetParent( m_current
);
2278 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2280 // don't go to root if it is hidden
2281 prev
= GetPrevSibling( m_current
);
2285 EnsureVisible( prev
);
2286 SelectItem( prev
, unselect_others
, extended_select
);
2292 // this works the same as the down arrow except that we
2293 // also expand the item if it wasn't expanded yet
2299 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2302 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2303 SelectItem( child
, unselect_others
, extended_select
);
2304 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2305 EnsureVisible( child
);
2309 wxTreeItemId next
= GetNextSibling( m_key_current
);
2312 wxTreeItemId current
= m_key_current
;
2313 while (current
&& !next
)
2315 current
= GetParent( current
);
2316 if (current
) next
= GetNextSibling( current
);
2321 SelectItem( next
, unselect_others
, extended_select
);
2322 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2323 EnsureVisible( next
);
2329 // <End> selects the last visible tree item
2332 wxTreeItemId last
= GetRootItem();
2334 while ( last
.IsOk() && IsExpanded(last
) )
2336 wxTreeItemId lastChild
= GetLastChild(last
);
2338 // it may happen if the item was expanded but then all of
2339 // its children have been deleted - so IsExpanded() returned
2340 // TRUE, but GetLastChild() returned invalid item
2349 EnsureVisible( last
);
2350 SelectItem( last
, unselect_others
, extended_select
);
2355 // <Home> selects the root item
2358 wxTreeItemId prev
= GetRootItem();
2360 if (HasFlag(wxTR_HIDE_ROOT
))
2363 prev
= GetFirstChild(prev
, dummy
);
2366 EnsureVisible( prev
);
2367 SelectItem( prev
, unselect_others
, extended_select
);
2376 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2378 // JACS: removed wxYieldIfNeeded() because it can cause the window
2379 // to be deleted from under us if a close window event is pending
2384 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2385 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2386 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2387 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2388 if (flags
) return wxTreeItemId();
2390 if (m_anchor
== NULL
)
2392 flags
= wxTREE_HITTEST_NOWHERE
;
2393 return wxTreeItemId();
2396 wxClientDC
dc(this);
2398 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2399 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2400 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2406 flags
= wxTREE_HITTEST_NOWHERE
;
2407 return wxTreeItemId();
2412 // get the bounding rectangle of the item (or of its label only)
2413 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2415 bool WXUNUSED(textOnly
)) const
2417 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2419 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2422 GetViewStart(& startX
, & startY
);
2424 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2425 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2426 rect
.width
= i
->GetWidth();
2427 //rect.height = i->GetHeight();
2428 rect
.height
= GetLineHeight(i
);
2435 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2437 if (!item
.IsOk()) return;
2439 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2441 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2442 te
.m_item
= (long) m_currentEdit
;
2443 te
.SetEventObject( this );
2444 GetEventHandler()->ProcessEvent( te
);
2446 if (!te
.IsAllowed()) return;
2448 // We have to call this here because the label in
2449 // question might just have been added and no screen
2450 // update taken place.
2451 if (m_dirty
) wxYieldIfNeeded();
2453 wxString s
= m_currentEdit
->GetText();
2454 int x
= m_currentEdit
->GetX();
2455 int y
= m_currentEdit
->GetY();
2456 int w
= m_currentEdit
->GetWidth();
2457 int h
= m_currentEdit
->GetHeight();
2462 int image
= m_currentEdit
->GetCurrentImage();
2463 if ( image
!= NO_IMAGE
)
2465 if ( m_imageListNormal
)
2467 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2472 wxFAIL_MSG(_T("you must create an image list to use images!"));
2476 w
-= image_w
+ 4; // I don't know why +4 is needed
2478 wxClientDC
dc(this);
2480 x
= dc
.LogicalToDeviceX( x
);
2481 y
= dc
.LogicalToDeviceY( y
);
2483 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2493 void wxGenericTreeCtrl::OnRenameTimer()
2498 void wxGenericTreeCtrl::OnRenameAccept()
2500 // TODO if the validator fails this causes a crash
2501 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2502 le
.m_item
= (long) m_currentEdit
;
2503 le
.SetEventObject( this );
2504 le
.m_label
= m_renameRes
;
2505 GetEventHandler()->ProcessEvent( le
);
2507 if (!le
.IsAllowed()) return;
2509 SetItemText( m_currentEdit
, m_renameRes
);
2512 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2514 if ( !m_anchor
) return;
2516 // we process left mouse up event (enables in-place edit), right down
2517 // (pass to the user code), left dbl click (activate item) and
2518 // dragging/moving events for items drag-and-drop
2519 if ( !(event
.LeftDown() ||
2521 event
.RightDown() ||
2522 event
.LeftDClick() ||
2524 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2531 wxClientDC
dc(this);
2533 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2534 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2537 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2542 if ( event
.Dragging() && !m_isDragging
)
2544 if (m_dragCount
== 0)
2545 m_dragStart
= wxPoint(x
,y
);
2549 if (m_dragCount
!= 3)
2551 // wait until user drags a bit further...
2555 wxEventType command
= event
.RightIsDown()
2556 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2557 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2559 wxTreeEvent
nevent( command
, GetId() );
2560 nevent
.m_item
= (long) m_current
;
2561 nevent
.SetEventObject(this);
2563 // by default the dragging is not supported, the user code must
2564 // explicitly allow the event for it to take place
2567 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2569 // we're going to drag this item
2570 m_isDragging
= TRUE
;
2572 // remember the old cursor because we will change it while
2574 m_oldCursor
= m_cursor
;
2576 // in a single selection control, hide the selection temporarily
2577 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2579 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2581 if ( m_oldSelection
)
2583 m_oldSelection
->SetHilight(FALSE
);
2584 RefreshLine(m_oldSelection
);
2591 else if ( event
.Moving() )
2593 if ( item
!= m_dropTarget
)
2595 // unhighlight the previous drop target
2596 DrawDropEffect(m_dropTarget
);
2598 m_dropTarget
= item
;
2600 // highlight the current drop target if any
2601 DrawDropEffect(m_dropTarget
);
2606 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2608 // erase the highlighting
2609 DrawDropEffect(m_dropTarget
);
2611 if ( m_oldSelection
)
2613 m_oldSelection
->SetHilight(TRUE
);
2614 RefreshLine(m_oldSelection
);
2615 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2618 // generate the drag end event
2619 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2621 event
.m_item
= (long) item
;
2622 event
.m_pointDrag
= wxPoint(x
, y
);
2623 event
.SetEventObject(this);
2625 (void)GetEventHandler()->ProcessEvent(event
);
2627 m_isDragging
= FALSE
;
2628 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2632 SetCursor(m_oldCursor
);
2638 // here we process only the messages which happen on tree items
2642 if (item
== NULL
) return; /* we hit the blank area */
2644 if ( event
.RightDown() )
2646 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2647 nevent
.m_item
= (long) item
;
2648 CalcScrolledPosition(x
, y
,
2649 &nevent
.m_pointDrag
.x
,
2650 &nevent
.m_pointDrag
.y
);
2651 nevent
.SetEventObject(this);
2652 GetEventHandler()->ProcessEvent(nevent
);
2654 else if ( event
.LeftUp() )
2658 if ( (item
== m_current
) &&
2659 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2660 HasFlag(wxTR_EDIT_LABELS
) )
2662 if ( m_renameTimer
->IsRunning() )
2663 m_renameTimer
->Stop();
2665 m_renameTimer
->Start( 100, TRUE
);
2668 m_lastOnSame
= FALSE
;
2671 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2673 if ( event
.LeftDown() )
2675 m_lastOnSame
= item
== m_current
;
2678 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2680 // only toggle the item for a single click, double click on
2681 // the button doesn't do anything (it toggles the item twice)
2682 if ( event
.LeftDown() )
2687 // don't select the item if the button was clicked
2691 // how should the selection work for this event?
2692 bool is_multiple
, extended_select
, unselect_others
;
2693 EventFlagsToSelType(GetWindowStyleFlag(),
2695 event
.ControlDown(),
2696 is_multiple
, extended_select
, unselect_others
);
2698 SelectItem(item
, unselect_others
, extended_select
);
2700 // For some reason, Windows isn't recognizing a left double-click,
2701 // so we need to simulate it here. Allow 200 milliseconds for now.
2702 if ( event
.LeftDClick() )
2704 // double clicking should not start editing the item label
2705 m_renameTimer
->Stop();
2706 m_lastOnSame
= FALSE
;
2708 // send activate event first
2709 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2710 nevent
.m_item
= (long) item
;
2711 CalcScrolledPosition(x
, y
,
2712 &nevent
.m_pointDrag
.x
,
2713 &nevent
.m_pointDrag
.y
);
2714 nevent
.SetEventObject( this );
2715 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2717 // if the user code didn't process the activate event,
2718 // handle it ourselves by toggling the item when it is
2720 if ( item
->HasPlus() )
2730 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2732 /* after all changes have been done to the tree control,
2733 * we actually redraw the tree when everything is over */
2735 if (!m_dirty
) return;
2739 CalculatePositions();
2741 AdjustMyScrollbars();
2744 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2750 dc
.SetFont(m_boldFont
);
2752 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2755 // restore normal font
2756 dc
.SetFont( m_normalFont
);
2760 int image
= item
->GetCurrentImage();
2761 if ( image
!= NO_IMAGE
)
2763 if ( m_imageListNormal
)
2765 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2770 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2773 total_h
+= 2; // at least 2 pixels
2775 total_h
+= total_h
/10; // otherwise 10% extra spacing
2777 item
->SetHeight(total_h
);
2778 if (total_h
>m_lineHeight
)
2779 m_lineHeight
=total_h
;
2781 item
->SetWidth(image_w
+text_w
+2);
2784 // -----------------------------------------------------------------------------
2785 // for developper : y is now the top of the level
2786 // not the middle of it !
2787 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2789 int x
= level
*m_indent
;
2790 if (!HasFlag(wxTR_HIDE_ROOT
))
2794 else if (level
== 0)
2796 // a hidden root is not evaluated, but its
2797 // children are always calculated
2801 CalculateSize( item
, dc
);
2804 item
->SetX( x
+m_spacing
);
2806 y
+= GetLineHeight(item
);
2808 if ( !item
->IsExpanded() )
2810 // we don't need to calculate collapsed branches
2815 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2816 size_t n
, count
= children
.Count();
2818 for (n
= 0; n
< count
; ++n
)
2819 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2822 void wxGenericTreeCtrl::CalculatePositions()
2824 if ( !m_anchor
) return;
2826 wxClientDC
dc(this);
2829 dc
.SetFont( m_normalFont
);
2831 dc
.SetPen( m_dottedPen
);
2832 //if(GetImageList() == NULL)
2833 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2836 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2839 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2841 if (m_dirty
) return;
2843 wxClientDC
dc(this);
2848 GetClientSize( &cw
, &ch
);
2851 rect
.x
= dc
.LogicalToDeviceX( 0 );
2853 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2856 Refresh( TRUE
, &rect
);
2858 AdjustMyScrollbars();
2861 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2863 if (m_dirty
) return;
2865 wxClientDC
dc(this);
2870 GetClientSize( &cw
, &ch
);
2873 rect
.x
= dc
.LogicalToDeviceX( 0 );
2874 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2876 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2878 Refresh( TRUE
, &rect
);
2881 void wxGenericTreeCtrl::RefreshSelected()
2883 // TODO: this is awfully inefficient, we should keep the list of all
2884 // selected items internally, should be much faster
2886 RefreshSelectedUnder(m_anchor
);
2889 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2891 if ( item
->IsSelected() )
2894 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2895 size_t count
= children
.GetCount();
2896 for ( size_t n
= 0; n
< count
; n
++ )
2898 RefreshSelectedUnder(children
[n
]);
2902 // ----------------------------------------------------------------------------
2903 // changing colours: we need to refresh the tree control
2904 // ----------------------------------------------------------------------------
2906 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
2908 if ( !wxWindow::SetBackgroundColour(colour
) )
2916 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
2918 if ( !wxWindow::SetForegroundColour(colour
) )
2926 #endif // wxUSE_TREECTRL