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 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
887 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
889 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
891 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
894 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
896 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
898 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
901 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
903 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
905 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
908 // -----------------------------------------------------------------------------
910 // -----------------------------------------------------------------------------
912 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
914 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
916 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
919 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
921 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
924 return GetNextChild(item
, cookie
);
927 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
929 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
931 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
932 if ( (size_t)cookie
< children
.Count() )
934 return children
.Item((size_t)cookie
++);
938 // there are no more of them
939 return wxTreeItemId();
943 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
945 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
947 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
948 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
951 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
953 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
955 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
956 wxGenericTreeItem
*parent
= i
->GetParent();
957 if ( parent
== NULL
)
959 // root item doesn't have any siblings
960 return wxTreeItemId();
963 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
964 int index
= siblings
.Index(i
);
965 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
967 size_t n
= (size_t)(index
+ 1);
968 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
971 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
973 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
975 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
976 wxGenericTreeItem
*parent
= i
->GetParent();
977 if ( parent
== NULL
)
979 // root item doesn't have any siblings
980 return wxTreeItemId();
983 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
984 int index
= siblings
.Index(i
);
985 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
987 return index
== 0 ? wxTreeItemId()
988 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
991 // Only for internal use right now, but should probably be public
992 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
994 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
996 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
998 // First see if there are any children.
999 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1000 if (children
.GetCount() > 0)
1002 return children
.Item(0);
1006 // Try a sibling of this or ancestor instead
1007 wxTreeItemId p
= item
;
1008 wxTreeItemId toFind
;
1011 toFind
= GetNextSibling(p
);
1013 } while (p
.IsOk() && !toFind
.IsOk());
1018 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1020 wxTreeItemId id
= GetRootItem();
1029 } while (id
.IsOk());
1031 return wxTreeItemId();
1034 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1036 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1038 wxTreeItemId id
= item
;
1041 while (id
= GetNext(id
), id
.IsOk())
1047 return wxTreeItemId();
1050 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1052 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1054 wxFAIL_MSG(wxT("not implemented"));
1056 return wxTreeItemId();
1059 // -----------------------------------------------------------------------------
1061 // -----------------------------------------------------------------------------
1063 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1065 const wxString
& text
,
1066 int image
, int selImage
,
1067 wxTreeItemData
*data
)
1069 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1072 // should we give a warning here?
1073 return AddRoot(text
, image
, selImage
, data
);
1076 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1078 wxGenericTreeItem
*item
=
1079 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1083 data
->m_pItem
= (long) item
;
1086 parent
->Insert( item
, previous
);
1091 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1092 int image
, int selImage
,
1093 wxTreeItemData
*data
)
1095 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1097 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1099 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1100 image
, selImage
, data
);
1101 if (HasFlag(wxTR_HIDE_ROOT
))
1103 // if root is hidden, make sure we can navigate
1105 m_anchor
->SetHasPlus();
1110 data
->m_pItem
= (long) m_anchor
;
1113 if (!HasFlag(wxTR_MULTIPLE
))
1115 m_current
= m_key_current
= m_anchor
;
1116 m_current
->SetHilight( TRUE
);
1122 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1123 const wxString
& text
,
1124 int image
, int selImage
,
1125 wxTreeItemData
*data
)
1127 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1130 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1131 const wxTreeItemId
& idPrevious
,
1132 const wxString
& text
,
1133 int image
, int selImage
,
1134 wxTreeItemData
*data
)
1136 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1139 // should we give a warning here?
1140 return AddRoot(text
, image
, selImage
, data
);
1143 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1144 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1145 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1147 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1150 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1152 const wxString
& text
,
1153 int image
, int selImage
,
1154 wxTreeItemData
*data
)
1156 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1159 // should we give a warning here?
1160 return AddRoot(text
, image
, selImage
, data
);
1163 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1166 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1167 const wxString
& text
,
1168 int image
, int selImage
,
1169 wxTreeItemData
*data
)
1171 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1174 // should we give a warning here?
1175 return AddRoot(text
, image
, selImage
, data
);
1178 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1179 image
, selImage
, data
);
1182 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1184 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1185 event
.m_item
= (long) item
;
1186 event
.SetEventObject( this );
1187 ProcessEvent( event
);
1190 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1192 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1194 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1195 item
->DeleteChildren(this);
1198 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1200 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1202 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1204 // don't stay with invalid m_key_current or we will crash in
1205 // the next call to OnChar()
1206 bool changeKeyCurrent
= FALSE
;
1207 wxGenericTreeItem
*itemKey
= m_key_current
;
1210 if ( itemKey
== item
)
1212 // m_key_current is a descendant of the item being deleted
1213 changeKeyCurrent
= TRUE
;
1216 itemKey
= itemKey
->GetParent();
1219 wxGenericTreeItem
*parent
= item
->GetParent();
1222 parent
->GetChildren().Remove( item
); // remove by value
1225 if ( changeKeyCurrent
)
1227 // may be NULL or not
1228 m_key_current
= parent
;
1231 item
->DeleteChildren(this);
1232 SendDeleteEvent(item
);
1236 void wxGenericTreeCtrl::DeleteAllItems()
1242 m_anchor
->DeleteChildren(this);
1249 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1251 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1253 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1255 if ( !item
->HasPlus() )
1258 if ( item
->IsExpanded() )
1261 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1262 event
.m_item
= (long) item
;
1263 event
.SetEventObject( this );
1265 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1267 // cancelled by program
1272 CalculatePositions();
1274 RefreshSubtree(item
);
1276 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1277 ProcessEvent( event
);
1280 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1283 if ( IsExpanded(item
) )
1286 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1287 while ( child
.IsOk() )
1291 child
= GetNextChild(item
, cookie
);
1296 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1298 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1300 if ( !item
->IsExpanded() )
1303 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1304 event
.m_item
= (long) item
;
1305 event
.SetEventObject( this );
1306 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1308 // cancelled by program
1314 #if 0 // TODO why should items be collapsed recursively?
1315 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1316 size_t count
= children
.Count();
1317 for ( size_t n
= 0; n
< count
; n
++ )
1319 Collapse(children
[n
]);
1323 CalculatePositions();
1325 RefreshSubtree(item
);
1327 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1328 ProcessEvent( event
);
1331 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1334 DeleteChildren(item
);
1337 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1339 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1341 if (item
->IsExpanded())
1347 void wxGenericTreeCtrl::Unselect()
1351 m_current
->SetHilight( FALSE
);
1352 RefreshLine( m_current
);
1356 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1358 if (item
->IsSelected())
1360 item
->SetHilight(FALSE
);
1364 if (item
->HasChildren())
1366 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1367 size_t count
= children
.Count();
1368 for ( size_t n
= 0; n
< count
; ++n
)
1370 UnselectAllChildren(children
[n
]);
1375 void wxGenericTreeCtrl::UnselectAll()
1377 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1380 // Recursive function !
1381 // To stop we must have crt_item<last_item
1383 // Tag all next children, when no more children,
1384 // Move to parent (not to tag)
1385 // Keep going... if we found last_item, we stop.
1386 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1388 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1390 if (parent
== NULL
) // This is root item
1391 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1393 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1394 int index
= children
.Index(crt_item
);
1395 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1397 size_t count
= children
.Count();
1398 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1400 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1403 return TagNextChildren(parent
, last_item
, select
);
1406 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1408 crt_item
->SetHilight(select
);
1409 RefreshLine(crt_item
);
1411 if (crt_item
==last_item
)
1414 if (crt_item
->HasChildren())
1416 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1417 size_t count
= children
.Count();
1418 for ( size_t n
= 0; n
< count
; ++n
)
1420 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1428 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1430 // item2 is not necessary after item1
1431 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1433 // choice first' and 'last' between item1 and item2
1434 if (item1
->GetY()<item2
->GetY())
1445 bool select
= m_current
->IsSelected();
1447 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1450 TagNextChildren(first
,last
,select
);
1453 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1454 bool unselect_others
,
1455 bool extended_select
)
1457 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1459 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1460 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1462 //wxCHECK_RET( ( (!unselect_others) && is_single),
1463 // wxT("this is a single selection tree") );
1465 // to keep going anyhow !!!
1468 if (item
->IsSelected())
1469 return; // nothing to do
1470 unselect_others
= TRUE
;
1471 extended_select
= FALSE
;
1473 else if ( unselect_others
&& item
->IsSelected() )
1475 // selection change if there is more than one item currently selected
1476 wxArrayTreeItemIds selected_items
;
1477 if ( GetSelections(selected_items
) == 1 )
1481 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1482 event
.m_item
= (long) item
;
1483 event
.m_itemOld
= (long) m_current
;
1484 event
.SetEventObject( this );
1485 // TODO : Here we don't send any selection mode yet !
1487 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1490 wxTreeItemId parent
= GetParent( itemId
);
1491 while (parent
.IsOk())
1493 if (!IsExpanded(parent
))
1496 parent
= GetParent( parent
);
1499 EnsureVisible( itemId
);
1502 if (unselect_others
)
1504 if (is_single
) Unselect(); // to speed up thing
1509 if (extended_select
)
1513 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1516 // don't change the mark (m_current)
1517 SelectItemRange(m_current
, item
);
1521 bool select
=TRUE
; // the default
1523 // Check if we need to toggle hilight (ctrl mode)
1524 if (!unselect_others
)
1525 select
=!item
->IsSelected();
1527 m_current
= m_key_current
= item
;
1528 m_current
->SetHilight(select
);
1529 RefreshLine( m_current
);
1532 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1533 GetEventHandler()->ProcessEvent( event
);
1536 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1537 wxArrayTreeItemIds
&array
) const
1539 if ( item
->IsSelected() )
1540 array
.Add(wxTreeItemId(item
));
1542 if ( item
->HasChildren() )
1544 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1545 size_t count
= children
.GetCount();
1546 for ( size_t n
= 0; n
< count
; ++n
)
1547 FillArray(children
[n
], array
);
1551 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1554 wxTreeItemId idRoot
= GetRootItem();
1555 if ( idRoot
.IsOk() )
1557 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1559 //else: the tree is empty, so no selections
1561 return array
.Count();
1564 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1566 if (!item
.IsOk()) return;
1568 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1570 // first expand all parent branches
1571 wxGenericTreeItem
*parent
= gitem
->GetParent();
1575 parent
= parent
->GetParent();
1578 //if (parent) CalculatePositions();
1583 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1585 if (!item
.IsOk()) return;
1587 // We have to call this here because the label in
1588 // question might just have been added and no screen
1589 // update taken place.
1590 if (m_dirty
) wxYieldIfNeeded();
1592 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1594 // now scroll to the item
1595 int item_y
= gitem
->GetY();
1599 GetViewStart( &start_x
, &start_y
);
1600 start_y
*= PIXELS_PER_UNIT
;
1604 GetClientSize( &client_w
, &client_h
);
1606 if (item_y
< start_y
+3)
1611 m_anchor
->GetSize( x
, y
, this );
1612 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1613 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1614 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1615 // Item should appear at top
1616 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1618 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1623 m_anchor
->GetSize( x
, y
, this );
1624 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1625 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1626 item_y
+= PIXELS_PER_UNIT
+2;
1627 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1628 // Item should appear at bottom
1629 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
);
1633 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1634 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1636 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1637 wxGenericTreeItem
**item2
)
1639 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1641 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1644 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1645 const wxTreeItemId
& item2
)
1647 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1650 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1652 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1654 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1656 wxCHECK_RET( !s_treeBeingSorted
,
1657 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1659 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1660 if ( children
.Count() > 1 )
1664 s_treeBeingSorted
= this;
1665 children
.Sort(tree_ctrl_compare_func
);
1666 s_treeBeingSorted
= NULL
;
1668 //else: don't make the tree dirty as nothing changed
1671 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1673 return m_imageListNormal
;
1676 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1678 return m_imageListButtons
;
1681 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1683 return m_imageListState
;
1686 void wxGenericTreeCtrl::CalculateLineHeight()
1688 wxClientDC
dc(this);
1689 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1691 if ( m_imageListNormal
)
1693 // Calculate a m_lineHeight value from the normal Image sizes.
1694 // May be toggle off. Then wxGenericTreeCtrl will spread when
1695 // necessary (which might look ugly).
1696 int n
= m_imageListNormal
->GetImageCount();
1697 for (int i
= 0; i
< n
; i
++)
1699 int width
= 0, height
= 0;
1700 m_imageListNormal
->GetSize(i
, width
, height
);
1701 if (height
> m_lineHeight
) m_lineHeight
= height
;
1705 if (m_imageListButtons
)
1707 // Calculate a m_lineHeight value from the Button image sizes.
1708 // May be toggle off. Then wxGenericTreeCtrl will spread when
1709 // necessary (which might look ugly).
1710 int n
= m_imageListButtons
->GetImageCount();
1711 for (int i
= 0; i
< n
; i
++)
1713 int width
= 0, height
= 0;
1714 m_imageListButtons
->GetSize(i
, width
, height
);
1715 if (height
> m_lineHeight
) m_lineHeight
= height
;
1719 if (m_lineHeight
< 30)
1720 m_lineHeight
+= 2; // at least 2 pixels
1722 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1725 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1727 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1728 m_imageListNormal
= imageList
;
1729 m_ownsImageListNormal
= FALSE
;
1731 CalculateLineHeight();
1734 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1736 if (m_ownsImageListState
) delete m_imageListState
;
1737 m_imageListState
= imageList
;
1738 m_ownsImageListState
= FALSE
;
1741 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1743 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1744 m_imageListButtons
= imageList
;
1745 m_ownsImageListButtons
= FALSE
;
1747 CalculateLineHeight();
1750 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1752 SetImageList(imageList
);
1753 m_ownsImageListNormal
= TRUE
;
1756 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1758 SetStateImageList(imageList
);
1759 m_ownsImageListState
= TRUE
;
1762 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1764 SetButtonsImageList(imageList
);
1765 m_ownsImageListButtons
= TRUE
;
1768 // -----------------------------------------------------------------------------
1770 // -----------------------------------------------------------------------------
1772 void wxGenericTreeCtrl::AdjustMyScrollbars()
1777 m_anchor
->GetSize( x
, y
, this );
1778 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1779 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1780 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1781 int y_pos
= GetScrollPos( wxVERTICAL
);
1782 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1786 SetScrollbars( 0, 0, 0, 0 );
1790 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1792 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1793 return item
->GetHeight();
1795 return m_lineHeight
;
1798 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1800 // TODO implement "state" icon on items
1802 wxTreeItemAttr
*attr
= item
->GetAttributes();
1803 if ( attr
&& attr
->HasFont() )
1804 dc
.SetFont(attr
->GetFont());
1805 else if (item
->IsBold())
1806 dc
.SetFont(m_boldFont
);
1808 long text_w
= 0, text_h
= 0;
1809 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1811 int image_h
= 0, image_w
= 0;
1812 int image
= item
->GetCurrentImage();
1813 if ( image
!= NO_IMAGE
)
1815 if ( m_imageListNormal
)
1817 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1826 int total_h
= GetLineHeight(item
);
1828 if ( item
->IsSelected() )
1830 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1835 if ( attr
&& attr
->HasBackgroundColour() )
1836 colBg
= attr
->GetBackgroundColour();
1838 colBg
= m_backgroundColour
;
1839 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1842 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1844 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1846 // If it's selected, and there's an image, then we should
1847 // take care to leave the area under the image painted in the
1848 // background colour.
1849 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1850 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1854 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1855 item
->GetWidth()+2, total_h
-offset
);
1858 if ( image
!= NO_IMAGE
)
1860 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1861 m_imageListNormal
->Draw( image
, dc
,
1863 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1864 wxIMAGELIST_DRAW_TRANSPARENT
);
1865 dc
.DestroyClippingRegion();
1868 dc
.SetBackgroundMode(wxTRANSPARENT
);
1869 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1870 dc
.DrawText( item
->GetText(),
1871 (wxCoord
)(image_w
+ item
->GetX()),
1872 (wxCoord
)(item
->GetY() + extraH
));
1874 // restore normal font
1875 dc
.SetFont( m_normalFont
);
1878 // Now y stands for the top of the item, whereas it used to stand for middle !
1879 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1881 int x
= level
*m_indent
;
1882 if (!HasFlag(wxTR_HIDE_ROOT
))
1886 else if (level
== 0)
1888 // always expand hidden root
1890 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1891 int count
= children
.Count();
1897 PaintLevel(children
[n
], dc
, 1, y
);
1898 } while (++n
< count
);
1900 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
1902 // draw line down to last child
1903 origY
+= GetLineHeight(children
[0])>>1;
1904 oldY
+= GetLineHeight(children
[n
-1])>>1;
1905 dc
.DrawLine(3, origY
, 3, oldY
);
1911 item
->SetX(x
+m_spacing
);
1914 int h
= GetLineHeight(item
);
1916 int y_mid
= y_top
+ (h
>>1);
1919 int exposed_x
= dc
.LogicalToDeviceX(0);
1920 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1922 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1924 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1926 if (!HasFlag(wxTR_NO_LINES
))
1928 if (x
> (signed)m_indent
)
1929 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1930 else if (HasFlag(wxTR_LINES_AT_ROOT
))
1931 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
1932 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1935 if (m_imageListButtons
!= NULL
)
1937 // draw the image button here
1938 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
1939 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
1940 if (item
->IsSelected())
1941 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
1942 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1943 int xx
= x
- (image_w
>>1);
1944 int yy
= y_mid
- (image_h
>>1);
1945 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1946 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1947 wxIMAGELIST_DRAW_TRANSPARENT
);
1948 dc
.DestroyClippingRegion();
1950 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1952 // draw the twisty button here
1953 dc
.SetPen(*wxBLACK_PEN
);
1954 dc
.SetBrush(*m_hilightBrush
);
1958 if (item
->IsExpanded())
1961 button
[0].y
= y_mid
-2;
1963 button
[1].y
= y_mid
-2;
1965 button
[2].y
= y_mid
+3;
1969 button
[0].y
= y_mid
-5;
1971 button
[1].y
= y_mid
+5;
1973 button
[2].y
= y_mid
;
1976 dc
.DrawPolygon(3, button
);
1978 dc
.SetPen(m_dottedPen
);
1980 else // if (HasFlag(wxTR_HAS_BUTTONS))
1982 // draw the plus sign here
1983 dc
.SetPen(*wxGREY_PEN
);
1984 dc
.SetBrush(*wxWHITE_BRUSH
);
1985 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
1986 dc
.SetPen(*wxBLACK_PEN
);
1987 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
1988 if (!item
->IsExpanded())
1989 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
1990 dc
.SetPen(m_dottedPen
);
1993 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
1995 // draw the horizontal line here
1997 if (x
> (signed)m_indent
)
1998 x_start
-= m_indent
;
1999 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2001 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2006 // don't draw rect outline if we already have the
2007 // background color under Mac
2008 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2009 #endif // !__WXMAC__
2013 if ( item
->IsSelected() )
2015 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2019 wxTreeItemAttr
*attr
= item
->GetAttributes();
2020 if (attr
&& attr
->HasTextColour())
2021 colText
= attr
->GetTextColour();
2023 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2027 dc
.SetTextForeground(colText
);
2031 PaintItem(item
, dc
);
2033 if (HasFlag(wxTR_ROW_LINES
))
2035 // if the background colour is white, choose a
2036 // contrasting color for the lines
2037 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2038 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2039 dc
.DrawLine(0, y_top
, 10000, y_top
);
2040 dc
.DrawLine(0, y
, 10000, y
);
2043 // restore DC objects
2044 dc
.SetBrush(*wxWHITE_BRUSH
);
2045 dc
.SetPen(m_dottedPen
);
2046 dc
.SetTextForeground(*wxBLACK
);
2049 if (item
->IsExpanded())
2051 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2052 int count
= children
.Count();
2059 PaintLevel(children
[n
], dc
, level
, y
);
2060 } while (++n
< count
);
2062 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2064 // draw line down to last child
2065 oldY
+= GetLineHeight(children
[n
-1])>>1;
2066 if (HasButtons()) y_mid
+= 5;
2067 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2073 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2077 if ( item
->HasPlus() )
2079 // it's a folder, indicate it by a border
2084 // draw a line under the drop target because the item will be
2086 DrawLine(item
, TRUE
/* below */);
2089 SetCursor(wxCURSOR_BULLSEYE
);
2094 SetCursor(wxCURSOR_NO_ENTRY
);
2098 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2100 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2102 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2104 wxClientDC
dc(this);
2106 dc
.SetLogicalFunction(wxINVERT
);
2107 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2109 int w
= i
->GetWidth() + 2;
2110 int h
= GetLineHeight(i
) + 2;
2112 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2115 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2117 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2119 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2121 wxClientDC
dc(this);
2123 dc
.SetLogicalFunction(wxINVERT
);
2129 y
+= GetLineHeight(i
) - 1;
2132 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2135 // -----------------------------------------------------------------------------
2136 // wxWindows callbacks
2137 // -----------------------------------------------------------------------------
2139 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2147 dc
.SetFont( m_normalFont
);
2148 dc
.SetPen( m_dottedPen
);
2150 // this is now done dynamically
2151 //if(GetImageList() == NULL)
2152 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2155 PaintLevel( m_anchor
, dc
, 0, y
);
2158 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2167 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2176 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2178 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2179 te
.m_evtKey
= event
;
2180 te
.SetEventObject( this );
2181 if ( GetEventHandler()->ProcessEvent( te
) )
2183 // intercepted by the user code
2187 if ( (m_current
== 0) || (m_key_current
== 0) )
2193 // how should the selection work for this event?
2194 bool is_multiple
, extended_select
, unselect_others
;
2195 EventFlagsToSelType(GetWindowStyleFlag(),
2197 event
.ControlDown(),
2198 is_multiple
, extended_select
, unselect_others
);
2202 // * : Expand all/Collapse all
2203 // ' ' | return : activate
2204 // up : go up (not last children!)
2206 // left : go to parent
2207 // right : open if parent and go next
2208 // home : go to root
2209 // end : go to last item without opening parents
2210 switch (event
.KeyCode())
2214 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2222 if ( !IsExpanded(m_current
) )
2225 ExpandAll(m_current
);
2228 //else: fall through to Collapse() it
2232 if (IsExpanded(m_current
))
2234 Collapse(m_current
);
2241 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2242 event
.m_item
= (long) m_current
;
2243 event
.SetEventObject( this );
2244 GetEventHandler()->ProcessEvent( event
);
2248 // up goes to the previous sibling or to the last
2249 // of its children if it's expanded
2252 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2255 prev
= GetParent( m_key_current
);
2256 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2258 break; // don't go to root if it is hidden
2263 wxTreeItemId current
= m_key_current
;
2264 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2265 if (current
== GetFirstChild( prev
, cookie
))
2267 // otherwise we return to where we came from
2268 SelectItem( prev
, unselect_others
, extended_select
);
2269 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2270 EnsureVisible( prev
);
2277 while ( IsExpanded(prev
) && HasChildren(prev
) )
2279 wxTreeItemId child
= GetLastChild(prev
);
2286 SelectItem( prev
, unselect_others
, extended_select
);
2287 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2288 EnsureVisible( prev
);
2293 // left arrow goes to the parent
2296 wxTreeItemId prev
= GetParent( m_current
);
2297 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2299 // don't go to root if it is hidden
2300 prev
= GetPrevSibling( m_current
);
2304 EnsureVisible( prev
);
2305 SelectItem( prev
, unselect_others
, extended_select
);
2311 // this works the same as the down arrow except that we
2312 // also expand the item if it wasn't expanded yet
2318 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2321 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2322 SelectItem( child
, unselect_others
, extended_select
);
2323 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2324 EnsureVisible( child
);
2328 wxTreeItemId next
= GetNextSibling( m_key_current
);
2331 wxTreeItemId current
= m_key_current
;
2332 while (current
&& !next
)
2334 current
= GetParent( current
);
2335 if (current
) next
= GetNextSibling( current
);
2340 SelectItem( next
, unselect_others
, extended_select
);
2341 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2342 EnsureVisible( next
);
2348 // <End> selects the last visible tree item
2351 wxTreeItemId last
= GetRootItem();
2353 while ( last
.IsOk() && IsExpanded(last
) )
2355 wxTreeItemId lastChild
= GetLastChild(last
);
2357 // it may happen if the item was expanded but then all of
2358 // its children have been deleted - so IsExpanded() returned
2359 // TRUE, but GetLastChild() returned invalid item
2368 EnsureVisible( last
);
2369 SelectItem( last
, unselect_others
, extended_select
);
2374 // <Home> selects the root item
2377 wxTreeItemId prev
= GetRootItem();
2379 if (HasFlag(wxTR_HIDE_ROOT
))
2382 prev
= GetFirstChild(prev
, dummy
);
2385 EnsureVisible( prev
);
2386 SelectItem( prev
, unselect_others
, extended_select
);
2395 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2397 // JACS: removed wxYieldIfNeeded() because it can cause the window
2398 // to be deleted from under us if a close window event is pending
2403 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2404 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2405 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2406 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2407 if (flags
) return wxTreeItemId();
2409 if (m_anchor
== NULL
)
2411 flags
= wxTREE_HITTEST_NOWHERE
;
2412 return wxTreeItemId();
2415 wxClientDC
dc(this);
2417 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2418 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2419 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2425 flags
= wxTREE_HITTEST_NOWHERE
;
2426 return wxTreeItemId();
2431 // get the bounding rectangle of the item (or of its label only)
2432 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2434 bool WXUNUSED(textOnly
)) const
2436 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2438 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2441 GetViewStart(& startX
, & startY
);
2443 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2444 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2445 rect
.width
= i
->GetWidth();
2446 //rect.height = i->GetHeight();
2447 rect
.height
= GetLineHeight(i
);
2454 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2456 if (!item
.IsOk()) return;
2458 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2460 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2461 te
.m_item
= (long) m_currentEdit
;
2462 te
.SetEventObject( this );
2463 GetEventHandler()->ProcessEvent( te
);
2465 if (!te
.IsAllowed()) return;
2467 // We have to call this here because the label in
2468 // question might just have been added and no screen
2469 // update taken place.
2470 if (m_dirty
) wxYieldIfNeeded();
2472 wxString s
= m_currentEdit
->GetText();
2473 int x
= m_currentEdit
->GetX();
2474 int y
= m_currentEdit
->GetY();
2475 int w
= m_currentEdit
->GetWidth();
2476 int h
= m_currentEdit
->GetHeight();
2481 int image
= m_currentEdit
->GetCurrentImage();
2482 if ( image
!= NO_IMAGE
)
2484 if ( m_imageListNormal
)
2486 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2491 wxFAIL_MSG(_T("you must create an image list to use images!"));
2495 w
-= image_w
+ 4; // I don't know why +4 is needed
2497 wxClientDC
dc(this);
2499 x
= dc
.LogicalToDeviceX( x
);
2500 y
= dc
.LogicalToDeviceY( y
);
2502 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2512 void wxGenericTreeCtrl::OnRenameTimer()
2517 void wxGenericTreeCtrl::OnRenameAccept()
2519 // TODO if the validator fails this causes a crash
2520 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2521 le
.m_item
= (long) m_currentEdit
;
2522 le
.SetEventObject( this );
2523 le
.m_label
= m_renameRes
;
2524 GetEventHandler()->ProcessEvent( le
);
2526 if (!le
.IsAllowed()) return;
2528 SetItemText( m_currentEdit
, m_renameRes
);
2531 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2533 if ( !m_anchor
) return;
2535 // we process left mouse up event (enables in-place edit), right down
2536 // (pass to the user code), left dbl click (activate item) and
2537 // dragging/moving events for items drag-and-drop
2538 if ( !(event
.LeftDown() ||
2540 event
.RightDown() ||
2541 event
.LeftDClick() ||
2543 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2550 wxClientDC
dc(this);
2552 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2553 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2556 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2561 if ( event
.Dragging() && !m_isDragging
)
2563 if (m_dragCount
== 0)
2564 m_dragStart
= wxPoint(x
,y
);
2568 if (m_dragCount
!= 3)
2570 // wait until user drags a bit further...
2574 wxEventType command
= event
.RightIsDown()
2575 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2576 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2578 wxTreeEvent
nevent( command
, GetId() );
2579 nevent
.m_item
= (long) m_current
;
2580 nevent
.SetEventObject(this);
2582 // by default the dragging is not supported, the user code must
2583 // explicitly allow the event for it to take place
2586 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2588 // we're going to drag this item
2589 m_isDragging
= TRUE
;
2591 // remember the old cursor because we will change it while
2593 m_oldCursor
= m_cursor
;
2595 // in a single selection control, hide the selection temporarily
2596 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2598 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2600 if ( m_oldSelection
)
2602 m_oldSelection
->SetHilight(FALSE
);
2603 RefreshLine(m_oldSelection
);
2610 else if ( event
.Moving() )
2612 if ( item
!= m_dropTarget
)
2614 // unhighlight the previous drop target
2615 DrawDropEffect(m_dropTarget
);
2617 m_dropTarget
= item
;
2619 // highlight the current drop target if any
2620 DrawDropEffect(m_dropTarget
);
2625 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2627 // erase the highlighting
2628 DrawDropEffect(m_dropTarget
);
2630 if ( m_oldSelection
)
2632 m_oldSelection
->SetHilight(TRUE
);
2633 RefreshLine(m_oldSelection
);
2634 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2637 // generate the drag end event
2638 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2640 event
.m_item
= (long) item
;
2641 event
.m_pointDrag
= wxPoint(x
, y
);
2642 event
.SetEventObject(this);
2644 (void)GetEventHandler()->ProcessEvent(event
);
2646 m_isDragging
= FALSE
;
2647 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2651 SetCursor(m_oldCursor
);
2657 // here we process only the messages which happen on tree items
2661 if (item
== NULL
) return; /* we hit the blank area */
2663 if ( event
.RightDown() )
2665 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2666 nevent
.m_item
= (long) item
;
2667 CalcScrolledPosition(x
, y
,
2668 &nevent
.m_pointDrag
.x
,
2669 &nevent
.m_pointDrag
.y
);
2670 nevent
.SetEventObject(this);
2671 GetEventHandler()->ProcessEvent(nevent
);
2673 else if ( event
.LeftUp() )
2677 if ( (item
== m_current
) &&
2678 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2679 HasFlag(wxTR_EDIT_LABELS
) )
2681 if ( m_renameTimer
->IsRunning() )
2682 m_renameTimer
->Stop();
2684 m_renameTimer
->Start( 100, TRUE
);
2687 m_lastOnSame
= FALSE
;
2690 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2692 if ( event
.LeftDown() )
2694 m_lastOnSame
= item
== m_current
;
2697 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2699 // only toggle the item for a single click, double click on
2700 // the button doesn't do anything (it toggles the item twice)
2701 if ( event
.LeftDown() )
2706 // don't select the item if the button was clicked
2710 // how should the selection work for this event?
2711 bool is_multiple
, extended_select
, unselect_others
;
2712 EventFlagsToSelType(GetWindowStyleFlag(),
2714 event
.ControlDown(),
2715 is_multiple
, extended_select
, unselect_others
);
2717 SelectItem(item
, unselect_others
, extended_select
);
2719 // For some reason, Windows isn't recognizing a left double-click,
2720 // so we need to simulate it here. Allow 200 milliseconds for now.
2721 if ( event
.LeftDClick() )
2723 // double clicking should not start editing the item label
2724 m_renameTimer
->Stop();
2725 m_lastOnSame
= FALSE
;
2727 // send activate event first
2728 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2729 nevent
.m_item
= (long) item
;
2730 CalcScrolledPosition(x
, y
,
2731 &nevent
.m_pointDrag
.x
,
2732 &nevent
.m_pointDrag
.y
);
2733 nevent
.SetEventObject( this );
2734 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2736 // if the user code didn't process the activate event,
2737 // handle it ourselves by toggling the item when it is
2739 if ( item
->HasPlus() )
2749 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2751 /* after all changes have been done to the tree control,
2752 * we actually redraw the tree when everything is over */
2754 if (!m_dirty
) return;
2758 CalculatePositions();
2760 AdjustMyScrollbars();
2763 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2769 dc
.SetFont(m_boldFont
);
2771 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2774 // restore normal font
2775 dc
.SetFont( m_normalFont
);
2779 int image
= item
->GetCurrentImage();
2780 if ( image
!= NO_IMAGE
)
2782 if ( m_imageListNormal
)
2784 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2789 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2792 total_h
+= 2; // at least 2 pixels
2794 total_h
+= total_h
/10; // otherwise 10% extra spacing
2796 item
->SetHeight(total_h
);
2797 if (total_h
>m_lineHeight
)
2798 m_lineHeight
=total_h
;
2800 item
->SetWidth(image_w
+text_w
+2);
2803 // -----------------------------------------------------------------------------
2804 // for developper : y is now the top of the level
2805 // not the middle of it !
2806 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2808 int x
= level
*m_indent
;
2809 if (!HasFlag(wxTR_HIDE_ROOT
))
2813 else if (level
== 0)
2815 // a hidden root is not evaluated, but its
2816 // children are always calculated
2820 CalculateSize( item
, dc
);
2823 item
->SetX( x
+m_spacing
);
2825 y
+= GetLineHeight(item
);
2827 if ( !item
->IsExpanded() )
2829 // we don't need to calculate collapsed branches
2834 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2835 size_t n
, count
= children
.Count();
2837 for (n
= 0; n
< count
; ++n
)
2838 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2841 void wxGenericTreeCtrl::CalculatePositions()
2843 if ( !m_anchor
) return;
2845 wxClientDC
dc(this);
2848 dc
.SetFont( m_normalFont
);
2850 dc
.SetPen( m_dottedPen
);
2851 //if(GetImageList() == NULL)
2852 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2855 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2858 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2860 if (m_dirty
) return;
2862 wxClientDC
dc(this);
2867 GetClientSize( &cw
, &ch
);
2870 rect
.x
= dc
.LogicalToDeviceX( 0 );
2872 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2875 Refresh( TRUE
, &rect
);
2877 AdjustMyScrollbars();
2880 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2882 if (m_dirty
) return;
2884 wxClientDC
dc(this);
2889 GetClientSize( &cw
, &ch
);
2892 rect
.x
= dc
.LogicalToDeviceX( 0 );
2893 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2895 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2897 Refresh( TRUE
, &rect
);
2900 void wxGenericTreeCtrl::RefreshSelected()
2902 // TODO: this is awfully inefficient, we should keep the list of all
2903 // selected items internally, should be much faster
2905 RefreshSelectedUnder(m_anchor
);
2908 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2910 if ( item
->IsSelected() )
2913 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2914 size_t count
= children
.GetCount();
2915 for ( size_t n
= 0; n
< count
; n
++ )
2917 RefreshSelectedUnder(children
[n
]);
2921 // ----------------------------------------------------------------------------
2922 // changing colours: we need to refresh the tree control
2923 // ----------------------------------------------------------------------------
2925 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
2927 if ( !wxWindow::SetBackgroundColour(colour
) )
2935 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
2937 if ( !wxWindow::SetForegroundColour(colour
) )
2945 #endif // wxUSE_TREECTRL