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
;
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGenericTreeItem
109 wxGenericTreeItem() { m_data
= NULL
; }
110 wxGenericTreeItem( wxGenericTreeItem
*parent
,
111 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
;
311 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
313 if (event
.m_keyCode
== WXK_RETURN
)
316 (*m_res
) = GetValue();
318 if ((*m_res
) != m_startValue
)
319 m_owner
->OnRenameAccept();
321 if (!wxPendingDelete
.Member(this))
322 wxPendingDelete
.Append(this);
325 m_owner
->SetFocus(); // This doesn't work. TODO.
329 if (event
.m_keyCode
== WXK_ESCAPE
)
334 if (!wxPendingDelete
.Member(this))
335 wxPendingDelete
.Append(this);
338 m_owner
->SetFocus(); // This doesn't work. TODO.
345 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
353 // auto-grow the textctrl:
354 wxSize parentSize
= m_owner
->GetSize();
355 wxPoint myPos
= GetPosition();
356 wxSize mySize
= GetSize();
358 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
359 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
360 if (mySize
.x
> sx
) sx
= mySize
.x
;
366 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
374 if (!wxPendingDelete
.Member(this))
375 wxPendingDelete
.Append(this);
378 (*m_res
) = GetValue();
380 if ((*m_res
) != m_startValue
)
381 m_owner
->OnRenameAccept();
384 // -----------------------------------------------------------------------------
386 // -----------------------------------------------------------------------------
388 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
389 const wxString
& text
,
390 int image
, int selImage
,
391 wxTreeItemData
*data
)
394 m_images
[wxTreeItemIcon_Normal
] = image
;
395 m_images
[wxTreeItemIcon_Selected
] = selImage
;
396 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
397 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
402 m_isCollapsed
= TRUE
;
403 m_hasHilight
= FALSE
;
409 m_attr
= (wxTreeItemAttr
*)NULL
;
412 // We don't know the height here yet.
417 wxGenericTreeItem::~wxGenericTreeItem()
421 if (m_ownsAttr
) delete m_attr
;
423 wxASSERT_MSG( m_children
.IsEmpty(),
424 wxT("please call DeleteChildren() before deleting the item") );
427 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
429 size_t count
= m_children
.Count();
430 for ( size_t n
= 0; n
< count
; n
++ )
432 wxGenericTreeItem
*child
= m_children
[n
];
434 tree
->SendDeleteEvent(child
);
436 child
->DeleteChildren(tree
);
443 void wxGenericTreeItem::SetText( const wxString
&text
)
448 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
450 size_t count
= m_children
.Count();
454 size_t total
= count
;
455 for (size_t n
= 0; n
< count
; ++n
)
457 total
+= m_children
[n
]->GetChildrenCount();
463 void wxGenericTreeItem::GetSize( int &x
, int &y
,
464 const wxGenericTreeCtrl
*theButton
)
466 int bottomY
=m_y
+theButton
->GetLineHeight(this);
467 if ( y
< bottomY
) y
= bottomY
;
468 int width
= m_x
+ m_width
;
469 if ( x
< width
) x
= width
;
473 size_t count
= m_children
.Count();
474 for ( size_t n
= 0; n
< count
; ++n
)
476 m_children
[n
]->GetSize( x
, y
, theButton
);
481 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
482 const wxGenericTreeCtrl
*theCtrl
,
486 // for a hidden root node, don't evaluate it, but do evaluate children
487 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
490 int h
= theCtrl
->GetLineHeight(this);
491 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
493 int y_mid
= m_y
+ h
/2;
494 if (point
.y
< y_mid
)
495 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
497 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
499 // 5 is the size of the plus sign
500 int xCross
= m_x
- theCtrl
->GetSpacing();
501 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
502 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
503 HasPlus() && theCtrl
->HasButtons() )
505 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
509 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
514 // assuming every image (normal and selected) has the same size!
515 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
516 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
519 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
520 flags
|= wxTREE_HITTEST_ONITEMICON
;
522 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
528 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
529 if (point
.x
> m_x
+m_width
)
530 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
535 // if children are expanded, fall through to evaluate them
536 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
540 size_t count
= m_children
.Count();
541 for ( size_t n
= 0; n
< count
; n
++ )
543 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
551 return (wxGenericTreeItem
*) NULL
;
554 int wxGenericTreeItem::GetCurrentImage() const
556 int image
= NO_IMAGE
;
561 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
564 if ( image
== NO_IMAGE
)
566 // we usually fall back to the normal item, but try just the
567 // expanded one (and not selected) first in this case
568 image
= GetImage(wxTreeItemIcon_Expanded
);
574 image
= GetImage(wxTreeItemIcon_Selected
);
577 // maybe it doesn't have the specific image we want,
578 // try the default one instead
579 if ( image
== NO_IMAGE
) image
= GetImage();
584 // -----------------------------------------------------------------------------
585 // wxGenericTreeCtrl implementation
586 // -----------------------------------------------------------------------------
588 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
590 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
591 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
592 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
593 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
594 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
595 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
596 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
599 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
601 * wxTreeCtrl has to be a real class or we have problems with
602 * the run-time information.
605 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
608 // -----------------------------------------------------------------------------
609 // construction/destruction
610 // -----------------------------------------------------------------------------
612 void wxGenericTreeCtrl::Init()
614 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
622 m_hilightBrush
= new wxBrush
624 wxSystemSettings::GetSystemColour
626 wxSYS_COLOUR_HIGHLIGHT
631 m_hilightUnfocusedBrush
= new wxBrush
633 wxSystemSettings::GetSystemColour
635 wxSYS_COLOUR_BTNSHADOW
640 m_imageListNormal
= m_imageListButtons
=
641 m_imageListState
= (wxImageList
*) NULL
;
642 m_ownsImageListNormal
= m_ownsImageListButtons
=
643 m_ownsImageListState
= FALSE
;
646 m_isDragging
= FALSE
;
647 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
649 m_renameTimer
= new wxTreeRenameTimer( this );
650 m_lastOnSame
= FALSE
;
652 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
653 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
654 m_normalFont
.GetFamily(),
655 m_normalFont
.GetStyle(),
657 m_normalFont
.GetUnderlined());
660 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
665 const wxValidator
&validator
,
666 const wxString
& name
)
668 wxScrolledWindow::Create( parent
, id
, pos
, size
,
669 style
|wxHSCROLL
|wxVSCROLL
, name
);
671 // If the tree display has no buttons, but does have
672 // connecting lines, we can use a narrower layout.
673 // It may not be a good idea to force this...
674 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
681 SetValidator( validator
);
684 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
686 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
687 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
692 wxGenericTreeCtrl::~wxGenericTreeCtrl()
694 delete m_hilightBrush
;
695 delete m_hilightUnfocusedBrush
;
699 delete m_renameTimer
;
700 if (m_ownsImageListNormal
) delete m_imageListNormal
;
701 if (m_ownsImageListState
) delete m_imageListState
;
702 if (m_ownsImageListButtons
) delete m_imageListButtons
;
705 // -----------------------------------------------------------------------------
707 // -----------------------------------------------------------------------------
709 size_t wxGenericTreeCtrl::GetCount() const
711 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
714 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
720 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
726 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
728 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
730 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
733 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
735 // right now, just sets the styles. Eventually, we may
736 // want to update the inherited styles, but right now
737 // none of the parents has updatable styles
738 m_windowStyle
= styles
;
742 // -----------------------------------------------------------------------------
743 // functions to work with tree items
744 // -----------------------------------------------------------------------------
746 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
748 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
750 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
753 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
754 wxTreeItemIcon which
) const
756 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
758 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
761 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
763 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
765 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
768 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
770 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
773 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
774 pItem
->SetText(text
);
775 CalculateSize(pItem
, dc
);
779 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
781 wxTreeItemIcon which
)
783 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
785 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
786 pItem
->SetImage(image
, which
);
789 CalculateSize(pItem
, dc
);
793 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
795 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
797 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
800 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
802 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
804 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
805 pItem
->SetHasPlus(has
);
809 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
811 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
813 // avoid redrawing the tree if no real change
814 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
815 if ( pItem
->IsBold() != bold
)
817 pItem
->SetBold(bold
);
822 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
825 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
827 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
828 pItem
->Attr().SetTextColour(col
);
832 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
835 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
837 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
838 pItem
->Attr().SetBackgroundColour(col
);
842 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
844 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
846 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
847 pItem
->Attr().SetFont(font
);
851 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
853 wxScrolledWindow::SetFont(font
);
855 m_normalFont
= font
;
856 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
857 m_normalFont
.GetFamily(),
858 m_normalFont
.GetStyle(),
860 m_normalFont
.GetUnderlined());
866 // -----------------------------------------------------------------------------
867 // item status inquiries
868 // -----------------------------------------------------------------------------
870 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
872 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
874 // An item is only visible if it's not a descendant of a collapsed item
875 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
876 wxGenericTreeItem
* parent
= pItem
->GetParent();
879 if (!parent
->IsExpanded())
881 parent
= parent
->GetParent();
885 GetViewStart(& startX
, & startY
);
887 wxSize clientSize
= GetClientSize();
890 if (!GetBoundingRect(item
, rect
))
892 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
894 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
896 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
902 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
904 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
906 // consider that the item does have children if it has the "+" button: it
907 // might not have them (if it had never been expanded yet) but then it
908 // could have them as well and it's better to err on this side rather than
909 // disabling some operations which are restricted to the items with
910 // children for an item which does have them
911 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
914 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
916 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
918 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
921 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
923 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
925 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
928 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
930 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
932 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
935 // -----------------------------------------------------------------------------
937 // -----------------------------------------------------------------------------
939 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
941 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
943 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
946 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
948 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
951 return GetNextChild(item
, cookie
);
954 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
956 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
958 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
959 if ( (size_t)cookie
< children
.Count() )
961 return children
.Item((size_t)cookie
++);
965 // there are no more of them
966 return wxTreeItemId();
970 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
972 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
974 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
975 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
978 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
980 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
982 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
983 wxGenericTreeItem
*parent
= i
->GetParent();
984 if ( parent
== NULL
)
986 // root item doesn't have any siblings
987 return wxTreeItemId();
990 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
991 int index
= siblings
.Index(i
);
992 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
994 size_t n
= (size_t)(index
+ 1);
995 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
998 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1000 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1002 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1003 wxGenericTreeItem
*parent
= i
->GetParent();
1004 if ( parent
== NULL
)
1006 // root item doesn't have any siblings
1007 return wxTreeItemId();
1010 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1011 int index
= siblings
.Index(i
);
1012 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1014 return index
== 0 ? wxTreeItemId()
1015 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1018 // Only for internal use right now, but should probably be public
1019 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1021 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1023 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1025 // First see if there are any children.
1026 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1027 if (children
.GetCount() > 0)
1029 return children
.Item(0);
1033 // Try a sibling of this or ancestor instead
1034 wxTreeItemId p
= item
;
1035 wxTreeItemId toFind
;
1038 toFind
= GetNextSibling(p
);
1040 } while (p
.IsOk() && !toFind
.IsOk());
1045 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1047 wxTreeItemId id
= GetRootItem();
1056 } while (id
.IsOk());
1058 return wxTreeItemId();
1061 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1063 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1065 wxTreeItemId id
= item
;
1068 while (id
= GetNext(id
), id
.IsOk())
1074 return wxTreeItemId();
1077 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1079 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1081 wxFAIL_MSG(wxT("not implemented"));
1083 return wxTreeItemId();
1086 // -----------------------------------------------------------------------------
1088 // -----------------------------------------------------------------------------
1090 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1092 const wxString
& text
,
1093 int image
, int selImage
,
1094 wxTreeItemData
*data
)
1096 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1099 // should we give a warning here?
1100 return AddRoot(text
, image
, selImage
, data
);
1103 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1105 wxGenericTreeItem
*item
=
1106 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1110 data
->m_pItem
= (long) item
;
1113 parent
->Insert( item
, previous
);
1118 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1119 int image
, int selImage
,
1120 wxTreeItemData
*data
)
1122 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1124 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1126 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1127 image
, selImage
, data
);
1128 if (HasFlag(wxTR_HIDE_ROOT
))
1130 // if root is hidden, make sure we can navigate
1132 m_anchor
->SetHasPlus();
1137 data
->m_pItem
= (long) m_anchor
;
1140 if (!HasFlag(wxTR_MULTIPLE
))
1142 m_current
= m_key_current
= m_anchor
;
1143 m_current
->SetHilight( TRUE
);
1149 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1150 const wxString
& text
,
1151 int image
, int selImage
,
1152 wxTreeItemData
*data
)
1154 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1157 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1158 const wxTreeItemId
& idPrevious
,
1159 const wxString
& text
,
1160 int image
, int selImage
,
1161 wxTreeItemData
*data
)
1163 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1166 // should we give a warning here?
1167 return AddRoot(text
, image
, selImage
, data
);
1170 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1171 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1172 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1174 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1177 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1179 const wxString
& text
,
1180 int image
, int selImage
,
1181 wxTreeItemData
*data
)
1183 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1186 // should we give a warning here?
1187 return AddRoot(text
, image
, selImage
, data
);
1190 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1193 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1194 const wxString
& text
,
1195 int image
, int selImage
,
1196 wxTreeItemData
*data
)
1198 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1201 // should we give a warning here?
1202 return AddRoot(text
, image
, selImage
, data
);
1205 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1206 image
, selImage
, data
);
1209 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1211 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1212 event
.m_item
= (long) item
;
1213 event
.SetEventObject( this );
1214 ProcessEvent( event
);
1217 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1219 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1221 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1222 item
->DeleteChildren(this);
1225 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1227 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1229 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1231 // don't stay with invalid m_key_current or we will crash in
1232 // the next call to OnChar()
1233 bool changeKeyCurrent
= FALSE
;
1234 wxGenericTreeItem
*itemKey
= m_key_current
;
1237 if ( itemKey
== item
)
1239 // m_key_current is a descendant of the item being deleted
1240 changeKeyCurrent
= TRUE
;
1243 itemKey
= itemKey
->GetParent();
1246 wxGenericTreeItem
*parent
= item
->GetParent();
1249 parent
->GetChildren().Remove( item
); // remove by value
1252 if ( changeKeyCurrent
)
1254 // may be NULL or not
1255 m_key_current
= parent
;
1258 item
->DeleteChildren(this);
1259 SendDeleteEvent(item
);
1263 void wxGenericTreeCtrl::DeleteAllItems()
1269 m_anchor
->DeleteChildren(this);
1276 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1278 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1280 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1282 if ( !item
->HasPlus() )
1285 if ( item
->IsExpanded() )
1288 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1289 event
.m_item
= (long) item
;
1290 event
.SetEventObject( this );
1292 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1294 // cancelled by program
1299 CalculatePositions();
1301 RefreshSubtree(item
);
1303 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1304 ProcessEvent( event
);
1307 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1310 if ( IsExpanded(item
) )
1313 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1314 while ( child
.IsOk() )
1318 child
= GetNextChild(item
, cookie
);
1323 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1325 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1327 if ( !item
->IsExpanded() )
1330 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1331 event
.m_item
= (long) item
;
1332 event
.SetEventObject( this );
1333 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1335 // cancelled by program
1341 #if 0 // TODO why should items be collapsed recursively?
1342 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1343 size_t count
= children
.Count();
1344 for ( size_t n
= 0; n
< count
; n
++ )
1346 Collapse(children
[n
]);
1350 CalculatePositions();
1352 RefreshSubtree(item
);
1354 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1355 ProcessEvent( event
);
1358 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1361 DeleteChildren(item
);
1364 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1366 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1368 if (item
->IsExpanded())
1374 void wxGenericTreeCtrl::Unselect()
1378 m_current
->SetHilight( FALSE
);
1379 RefreshLine( m_current
);
1383 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1385 if (item
->IsSelected())
1387 item
->SetHilight(FALSE
);
1391 if (item
->HasChildren())
1393 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1394 size_t count
= children
.Count();
1395 for ( size_t n
= 0; n
< count
; ++n
)
1397 UnselectAllChildren(children
[n
]);
1402 void wxGenericTreeCtrl::UnselectAll()
1404 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1407 // Recursive function !
1408 // To stop we must have crt_item<last_item
1410 // Tag all next children, when no more children,
1411 // Move to parent (not to tag)
1412 // Keep going... if we found last_item, we stop.
1413 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1415 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1417 if (parent
== NULL
) // This is root item
1418 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1420 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1421 int index
= children
.Index(crt_item
);
1422 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1424 size_t count
= children
.Count();
1425 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1427 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1430 return TagNextChildren(parent
, last_item
, select
);
1433 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1435 crt_item
->SetHilight(select
);
1436 RefreshLine(crt_item
);
1438 if (crt_item
==last_item
)
1441 if (crt_item
->HasChildren())
1443 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1444 size_t count
= children
.Count();
1445 for ( size_t n
= 0; n
< count
; ++n
)
1447 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1455 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1457 // item2 is not necessary after item1
1458 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1460 // choice first' and 'last' between item1 and item2
1461 if (item1
->GetY()<item2
->GetY())
1472 bool select
= m_current
->IsSelected();
1474 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1477 TagNextChildren(first
,last
,select
);
1480 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1481 bool unselect_others
,
1482 bool extended_select
)
1484 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1486 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1487 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1489 //wxCHECK_RET( ( (!unselect_others) && is_single),
1490 // wxT("this is a single selection tree") );
1492 // to keep going anyhow !!!
1495 if (item
->IsSelected())
1496 return; // nothing to do
1497 unselect_others
= TRUE
;
1498 extended_select
= FALSE
;
1500 else if ( unselect_others
&& item
->IsSelected() )
1502 // selection change if there is more than one item currently selected
1503 wxArrayTreeItemIds selected_items
;
1504 if ( GetSelections(selected_items
) == 1 )
1508 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1509 event
.m_item
= (long) item
;
1510 event
.m_itemOld
= (long) m_current
;
1511 event
.SetEventObject( this );
1512 // TODO : Here we don't send any selection mode yet !
1514 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1517 wxTreeItemId parent
= GetParent( itemId
);
1518 while (parent
.IsOk())
1520 if (!IsExpanded(parent
))
1523 parent
= GetParent( parent
);
1526 EnsureVisible( itemId
);
1529 if (unselect_others
)
1531 if (is_single
) Unselect(); // to speed up thing
1536 if (extended_select
)
1540 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1543 // don't change the mark (m_current)
1544 SelectItemRange(m_current
, item
);
1548 bool select
=TRUE
; // the default
1550 // Check if we need to toggle hilight (ctrl mode)
1551 if (!unselect_others
)
1552 select
=!item
->IsSelected();
1554 m_current
= m_key_current
= item
;
1555 m_current
->SetHilight(select
);
1556 RefreshLine( m_current
);
1559 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1560 GetEventHandler()->ProcessEvent( event
);
1563 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1564 wxArrayTreeItemIds
&array
) const
1566 if ( item
->IsSelected() )
1567 array
.Add(wxTreeItemId(item
));
1569 if ( item
->HasChildren() )
1571 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1572 size_t count
= children
.GetCount();
1573 for ( size_t n
= 0; n
< count
; ++n
)
1574 FillArray(children
[n
], array
);
1578 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1581 wxTreeItemId idRoot
= GetRootItem();
1582 if ( idRoot
.IsOk() )
1584 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1586 //else: the tree is empty, so no selections
1588 return array
.Count();
1591 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1593 if (!item
.IsOk()) return;
1595 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1597 // first expand all parent branches
1598 wxGenericTreeItem
*parent
= gitem
->GetParent();
1602 parent
= parent
->GetParent();
1605 //if (parent) CalculatePositions();
1610 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1612 if (!item
.IsOk()) return;
1614 // We have to call this here because the label in
1615 // question might just have been added and no screen
1616 // update taken place.
1617 if (m_dirty
) wxYieldIfNeeded();
1619 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1621 // now scroll to the item
1622 int item_y
= gitem
->GetY();
1626 GetViewStart( &start_x
, &start_y
);
1627 start_y
*= PIXELS_PER_UNIT
;
1631 GetClientSize( &client_w
, &client_h
);
1633 if (item_y
< start_y
+3)
1638 m_anchor
->GetSize( x
, y
, this );
1639 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1640 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1641 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1642 // Item should appear at top
1643 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1645 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1650 m_anchor
->GetSize( x
, y
, this );
1651 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1652 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1653 item_y
+= PIXELS_PER_UNIT
+2;
1654 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1655 // Item should appear at bottom
1656 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
);
1660 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1661 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1663 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1664 wxGenericTreeItem
**item2
)
1666 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1668 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1671 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1672 const wxTreeItemId
& item2
)
1674 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1677 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1679 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1681 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1683 wxCHECK_RET( !s_treeBeingSorted
,
1684 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1686 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1687 if ( children
.Count() > 1 )
1691 s_treeBeingSorted
= this;
1692 children
.Sort(tree_ctrl_compare_func
);
1693 s_treeBeingSorted
= NULL
;
1695 //else: don't make the tree dirty as nothing changed
1698 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1700 return m_imageListNormal
;
1703 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1705 return m_imageListButtons
;
1708 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1710 return m_imageListState
;
1713 void wxGenericTreeCtrl::CalculateLineHeight()
1715 wxClientDC
dc(this);
1716 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1718 if ( m_imageListNormal
)
1720 // Calculate a m_lineHeight value from the normal Image sizes.
1721 // May be toggle off. Then wxGenericTreeCtrl will spread when
1722 // necessary (which might look ugly).
1723 int n
= m_imageListNormal
->GetImageCount();
1724 for (int i
= 0; i
< n
; i
++)
1726 int width
= 0, height
= 0;
1727 m_imageListNormal
->GetSize(i
, width
, height
);
1728 if (height
> m_lineHeight
) m_lineHeight
= height
;
1732 if (m_imageListButtons
)
1734 // Calculate a m_lineHeight value from the Button image sizes.
1735 // May be toggle off. Then wxGenericTreeCtrl will spread when
1736 // necessary (which might look ugly).
1737 int n
= m_imageListButtons
->GetImageCount();
1738 for (int i
= 0; i
< n
; i
++)
1740 int width
= 0, height
= 0;
1741 m_imageListButtons
->GetSize(i
, width
, height
);
1742 if (height
> m_lineHeight
) m_lineHeight
= height
;
1746 if (m_lineHeight
< 30)
1747 m_lineHeight
+= 2; // at least 2 pixels
1749 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1752 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1754 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1755 m_imageListNormal
= imageList
;
1756 m_ownsImageListNormal
= FALSE
;
1758 CalculateLineHeight();
1761 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1763 if (m_ownsImageListState
) delete m_imageListState
;
1764 m_imageListState
= imageList
;
1765 m_ownsImageListState
= FALSE
;
1768 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1770 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1771 m_imageListButtons
= imageList
;
1772 m_ownsImageListButtons
= FALSE
;
1774 CalculateLineHeight();
1777 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1779 SetImageList(imageList
);
1780 m_ownsImageListNormal
= TRUE
;
1783 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1785 SetStateImageList(imageList
);
1786 m_ownsImageListState
= TRUE
;
1789 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1791 SetButtonsImageList(imageList
);
1792 m_ownsImageListButtons
= TRUE
;
1795 // -----------------------------------------------------------------------------
1797 // -----------------------------------------------------------------------------
1799 void wxGenericTreeCtrl::AdjustMyScrollbars()
1804 m_anchor
->GetSize( x
, y
, this );
1805 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1806 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1807 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1808 int y_pos
= GetScrollPos( wxVERTICAL
);
1809 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1813 SetScrollbars( 0, 0, 0, 0 );
1817 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1819 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1820 return item
->GetHeight();
1822 return m_lineHeight
;
1825 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1827 // TODO implement "state" icon on items
1829 wxTreeItemAttr
*attr
= item
->GetAttributes();
1830 if ( attr
&& attr
->HasFont() )
1831 dc
.SetFont(attr
->GetFont());
1832 else if (item
->IsBold())
1833 dc
.SetFont(m_boldFont
);
1835 long text_w
= 0, text_h
= 0;
1836 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1838 int image_h
= 0, image_w
= 0;
1839 int image
= item
->GetCurrentImage();
1840 if ( image
!= NO_IMAGE
)
1842 if ( m_imageListNormal
)
1844 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1853 int total_h
= GetLineHeight(item
);
1855 if ( item
->IsSelected() )
1857 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1862 if ( attr
&& attr
->HasBackgroundColour() )
1863 colBg
= attr
->GetBackgroundColour();
1865 colBg
= m_backgroundColour
;
1866 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1869 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1871 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1873 // If it's selected, and there's an image, then we should
1874 // take care to leave the area under the image painted in the
1875 // background colour.
1876 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1877 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1881 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1882 item
->GetWidth()+2, total_h
-offset
);
1885 if ( image
!= NO_IMAGE
)
1887 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1888 m_imageListNormal
->Draw( image
, dc
,
1890 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1891 wxIMAGELIST_DRAW_TRANSPARENT
);
1892 dc
.DestroyClippingRegion();
1895 dc
.SetBackgroundMode(wxTRANSPARENT
);
1896 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1897 dc
.DrawText( item
->GetText(),
1898 (wxCoord
)(image_w
+ item
->GetX()),
1899 (wxCoord
)(item
->GetY() + extraH
));
1901 // restore normal font
1902 dc
.SetFont( m_normalFont
);
1905 // Now y stands for the top of the item, whereas it used to stand for middle !
1906 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1908 int x
= level
*m_indent
;
1909 if (!HasFlag(wxTR_HIDE_ROOT
))
1913 else if (level
== 0)
1915 // always expand hidden root
1917 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1918 int count
= children
.Count();
1924 PaintLevel(children
[n
], dc
, 1, y
);
1925 } while (++n
< count
);
1927 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
1929 // draw line down to last child
1930 origY
+= GetLineHeight(children
[0])>>1;
1931 oldY
+= GetLineHeight(children
[n
-1])>>1;
1932 dc
.DrawLine(3, origY
, 3, oldY
);
1938 item
->SetX(x
+m_spacing
);
1941 int h
= GetLineHeight(item
);
1943 int y_mid
= y_top
+ (h
>>1);
1946 int exposed_x
= dc
.LogicalToDeviceX(0);
1947 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1949 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1951 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1953 if (!HasFlag(wxTR_NO_LINES
))
1955 if (x
> (signed)m_indent
)
1956 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1957 else if (HasFlag(wxTR_LINES_AT_ROOT
))
1958 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
1959 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1962 if (m_imageListButtons
!= NULL
)
1964 // draw the image button here
1965 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
1966 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
1967 if (item
->IsSelected())
1968 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
1969 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1970 int xx
= x
- (image_w
>>1);
1971 int yy
= y_mid
- (image_h
>>1);
1972 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1973 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1974 wxIMAGELIST_DRAW_TRANSPARENT
);
1975 dc
.DestroyClippingRegion();
1977 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1979 // draw the twisty button here
1980 dc
.SetPen(*wxBLACK_PEN
);
1981 dc
.SetBrush(*m_hilightBrush
);
1985 if (item
->IsExpanded())
1988 button
[0].y
= y_mid
-2;
1990 button
[1].y
= y_mid
-2;
1992 button
[2].y
= y_mid
+3;
1996 button
[0].y
= y_mid
-5;
1998 button
[1].y
= y_mid
+5;
2000 button
[2].y
= y_mid
;
2003 dc
.DrawPolygon(3, button
);
2005 dc
.SetPen(m_dottedPen
);
2007 else // if (HasFlag(wxTR_HAS_BUTTONS))
2009 // draw the plus sign here
2010 dc
.SetPen(*wxGREY_PEN
);
2011 dc
.SetBrush(*wxWHITE_BRUSH
);
2012 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2013 dc
.SetPen(*wxBLACK_PEN
);
2014 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2015 if (!item
->IsExpanded())
2016 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2017 dc
.SetPen(m_dottedPen
);
2020 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2022 // draw the horizontal line here
2024 if (x
> (signed)m_indent
)
2025 x_start
-= m_indent
;
2026 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2028 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2033 // don't draw rect outline if we already have the
2034 // background color under Mac
2035 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2036 #endif // !__WXMAC__
2040 if ( item
->IsSelected() )
2042 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2046 wxTreeItemAttr
*attr
= item
->GetAttributes();
2047 if (attr
&& attr
->HasTextColour())
2048 colText
= attr
->GetTextColour();
2050 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2054 dc
.SetTextForeground(colText
);
2058 PaintItem(item
, dc
);
2060 if (HasFlag(wxTR_ROW_LINES
))
2062 // if the background colour is white, choose a
2063 // contrasting color for the lines
2064 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2065 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2066 dc
.DrawLine(0, y_top
, 10000, y_top
);
2067 dc
.DrawLine(0, y
, 10000, y
);
2070 // restore DC objects
2071 dc
.SetBrush(*wxWHITE_BRUSH
);
2072 dc
.SetPen(m_dottedPen
);
2073 dc
.SetTextForeground(*wxBLACK
);
2076 if (item
->IsExpanded())
2078 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2079 int count
= children
.Count();
2086 PaintLevel(children
[n
], dc
, level
, y
);
2087 } while (++n
< count
);
2089 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2091 // draw line down to last child
2092 oldY
+= GetLineHeight(children
[n
-1])>>1;
2093 if (HasButtons()) y_mid
+= 5;
2094 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2100 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2104 if ( item
->HasPlus() )
2106 // it's a folder, indicate it by a border
2111 // draw a line under the drop target because the item will be
2113 DrawLine(item
, TRUE
/* below */);
2116 SetCursor(wxCURSOR_BULLSEYE
);
2121 SetCursor(wxCURSOR_NO_ENTRY
);
2125 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2127 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2129 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2131 wxClientDC
dc(this);
2133 dc
.SetLogicalFunction(wxINVERT
);
2134 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2136 int w
= i
->GetWidth() + 2;
2137 int h
= GetLineHeight(i
) + 2;
2139 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2142 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2144 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2146 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2148 wxClientDC
dc(this);
2150 dc
.SetLogicalFunction(wxINVERT
);
2156 y
+= GetLineHeight(i
) - 1;
2159 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2162 // -----------------------------------------------------------------------------
2163 // wxWindows callbacks
2164 // -----------------------------------------------------------------------------
2166 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2174 dc
.SetFont( m_normalFont
);
2175 dc
.SetPen( m_dottedPen
);
2177 // this is now done dynamically
2178 //if(GetImageList() == NULL)
2179 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2182 PaintLevel( m_anchor
, dc
, 0, y
);
2185 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2194 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2203 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2205 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2206 te
.m_evtKey
= event
;
2207 te
.SetEventObject( this );
2208 if ( GetEventHandler()->ProcessEvent( te
) )
2210 // intercepted by the user code
2214 if ( (m_current
== 0) || (m_key_current
== 0) )
2220 // how should the selection work for this event?
2221 bool is_multiple
, extended_select
, unselect_others
;
2222 EventFlagsToSelType(GetWindowStyleFlag(),
2224 event
.ControlDown(),
2225 is_multiple
, extended_select
, unselect_others
);
2229 // * : Expand all/Collapse all
2230 // ' ' | return : activate
2231 // up : go up (not last children!)
2233 // left : go to parent
2234 // right : open if parent and go next
2235 // home : go to root
2236 // end : go to last item without opening parents
2237 switch (event
.KeyCode())
2241 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2249 if ( !IsExpanded(m_current
) )
2252 ExpandAll(m_current
);
2255 //else: fall through to Collapse() it
2259 if (IsExpanded(m_current
))
2261 Collapse(m_current
);
2268 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2269 event
.m_item
= (long) m_current
;
2270 event
.SetEventObject( this );
2271 GetEventHandler()->ProcessEvent( event
);
2275 // up goes to the previous sibling or to the last
2276 // of its children if it's expanded
2279 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2282 prev
= GetParent( m_key_current
);
2283 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2285 break; // don't go to root if it is hidden
2290 wxTreeItemId current
= m_key_current
;
2291 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2292 if (current
== GetFirstChild( prev
, cookie
))
2294 // otherwise we return to where we came from
2295 SelectItem( prev
, unselect_others
, extended_select
);
2296 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2297 EnsureVisible( prev
);
2304 while ( IsExpanded(prev
) && HasChildren(prev
) )
2306 wxTreeItemId child
= GetLastChild(prev
);
2313 SelectItem( prev
, unselect_others
, extended_select
);
2314 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2315 EnsureVisible( prev
);
2320 // left arrow goes to the parent
2323 wxTreeItemId prev
= GetParent( m_current
);
2324 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2326 // don't go to root if it is hidden
2327 prev
= GetPrevSibling( m_current
);
2331 EnsureVisible( prev
);
2332 SelectItem( prev
, unselect_others
, extended_select
);
2338 // this works the same as the down arrow except that we
2339 // also expand the item if it wasn't expanded yet
2345 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2348 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2349 SelectItem( child
, unselect_others
, extended_select
);
2350 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2351 EnsureVisible( child
);
2355 wxTreeItemId next
= GetNextSibling( m_key_current
);
2358 wxTreeItemId current
= m_key_current
;
2359 while (current
&& !next
)
2361 current
= GetParent( current
);
2362 if (current
) next
= GetNextSibling( current
);
2367 SelectItem( next
, unselect_others
, extended_select
);
2368 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2369 EnsureVisible( next
);
2375 // <End> selects the last visible tree item
2378 wxTreeItemId last
= GetRootItem();
2380 while ( last
.IsOk() && IsExpanded(last
) )
2382 wxTreeItemId lastChild
= GetLastChild(last
);
2384 // it may happen if the item was expanded but then all of
2385 // its children have been deleted - so IsExpanded() returned
2386 // TRUE, but GetLastChild() returned invalid item
2395 EnsureVisible( last
);
2396 SelectItem( last
, unselect_others
, extended_select
);
2401 // <Home> selects the root item
2404 wxTreeItemId prev
= GetRootItem();
2406 if (HasFlag(wxTR_HIDE_ROOT
))
2409 prev
= GetFirstChild(prev
, dummy
);
2412 EnsureVisible( prev
);
2413 SelectItem( prev
, unselect_others
, extended_select
);
2422 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2424 // JACS: removed wxYieldIfNeeded() because it can cause the window
2425 // to be deleted from under us if a close window event is pending
2430 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2431 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2432 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2433 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2434 if (flags
) return wxTreeItemId();
2436 if (m_anchor
== NULL
)
2438 flags
= wxTREE_HITTEST_NOWHERE
;
2439 return wxTreeItemId();
2442 wxClientDC
dc(this);
2444 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2445 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2446 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2452 flags
= wxTREE_HITTEST_NOWHERE
;
2453 return wxTreeItemId();
2458 // get the bounding rectangle of the item (or of its label only)
2459 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2461 bool WXUNUSED(textOnly
)) const
2463 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2465 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2468 GetViewStart(& startX
, & startY
);
2470 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2471 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2472 rect
.width
= i
->GetWidth();
2473 //rect.height = i->GetHeight();
2474 rect
.height
= GetLineHeight(i
);
2481 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2483 if (!item
.IsOk()) return;
2485 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2487 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2488 te
.m_item
= (long) m_currentEdit
;
2489 te
.SetEventObject( this );
2490 GetEventHandler()->ProcessEvent( te
);
2492 if (!te
.IsAllowed()) return;
2494 // We have to call this here because the label in
2495 // question might just have been added and no screen
2496 // update taken place.
2497 if (m_dirty
) wxYieldIfNeeded();
2499 wxString s
= m_currentEdit
->GetText();
2500 int x
= m_currentEdit
->GetX();
2501 int y
= m_currentEdit
->GetY();
2502 int w
= m_currentEdit
->GetWidth();
2503 int h
= m_currentEdit
->GetHeight();
2508 int image
= m_currentEdit
->GetCurrentImage();
2509 if ( image
!= NO_IMAGE
)
2511 if ( m_imageListNormal
)
2513 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2518 wxFAIL_MSG(_T("you must create an image list to use images!"));
2522 w
-= image_w
+ 4; // I don't know why +4 is needed
2524 wxClientDC
dc(this);
2526 x
= dc
.LogicalToDeviceX( x
);
2527 y
= dc
.LogicalToDeviceY( y
);
2529 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2539 void wxGenericTreeCtrl::OnRenameTimer()
2544 void wxGenericTreeCtrl::OnRenameAccept()
2546 // TODO if the validator fails this causes a crash
2547 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2548 le
.m_item
= (long) m_currentEdit
;
2549 le
.SetEventObject( this );
2550 le
.m_label
= m_renameRes
;
2551 GetEventHandler()->ProcessEvent( le
);
2553 if (!le
.IsAllowed()) return;
2555 SetItemText( m_currentEdit
, m_renameRes
);
2558 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2560 if ( !m_anchor
) return;
2562 // we process left mouse up event (enables in-place edit), right down
2563 // (pass to the user code), left dbl click (activate item) and
2564 // dragging/moving events for items drag-and-drop
2565 if ( !(event
.LeftDown() ||
2567 event
.RightDown() ||
2568 event
.LeftDClick() ||
2570 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2577 wxClientDC
dc(this);
2579 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2580 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2583 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2588 if ( event
.Dragging() && !m_isDragging
)
2590 if (m_dragCount
== 0)
2591 m_dragStart
= wxPoint(x
,y
);
2595 if (m_dragCount
!= 3)
2597 // wait until user drags a bit further...
2601 wxEventType command
= event
.RightIsDown()
2602 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2603 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2605 wxTreeEvent
nevent( command
, GetId() );
2606 nevent
.m_item
= (long) m_current
;
2607 nevent
.SetEventObject(this);
2609 // by default the dragging is not supported, the user code must
2610 // explicitly allow the event for it to take place
2613 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2615 // we're going to drag this item
2616 m_isDragging
= TRUE
;
2618 // remember the old cursor because we will change it while
2620 m_oldCursor
= m_cursor
;
2622 // in a single selection control, hide the selection temporarily
2623 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2625 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2627 if ( m_oldSelection
)
2629 m_oldSelection
->SetHilight(FALSE
);
2630 RefreshLine(m_oldSelection
);
2637 else if ( event
.Moving() )
2639 if ( item
!= m_dropTarget
)
2641 // unhighlight the previous drop target
2642 DrawDropEffect(m_dropTarget
);
2644 m_dropTarget
= item
;
2646 // highlight the current drop target if any
2647 DrawDropEffect(m_dropTarget
);
2652 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2654 // erase the highlighting
2655 DrawDropEffect(m_dropTarget
);
2657 if ( m_oldSelection
)
2659 m_oldSelection
->SetHilight(TRUE
);
2660 RefreshLine(m_oldSelection
);
2661 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2664 // generate the drag end event
2665 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2667 event
.m_item
= (long) item
;
2668 event
.m_pointDrag
= wxPoint(x
, y
);
2669 event
.SetEventObject(this);
2671 (void)GetEventHandler()->ProcessEvent(event
);
2673 m_isDragging
= FALSE
;
2674 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2678 SetCursor(m_oldCursor
);
2684 // here we process only the messages which happen on tree items
2688 if (item
== NULL
) return; /* we hit the blank area */
2690 if ( event
.RightDown() )
2692 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2693 nevent
.m_item
= (long) item
;
2694 CalcScrolledPosition(x
, y
,
2695 &nevent
.m_pointDrag
.x
,
2696 &nevent
.m_pointDrag
.y
);
2697 nevent
.SetEventObject(this);
2698 GetEventHandler()->ProcessEvent(nevent
);
2700 else if ( event
.LeftUp() )
2704 if ( (item
== m_current
) &&
2705 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2706 HasFlag(wxTR_EDIT_LABELS
) )
2708 if ( m_renameTimer
->IsRunning() )
2709 m_renameTimer
->Stop();
2711 m_renameTimer
->Start( 100, TRUE
);
2714 m_lastOnSame
= FALSE
;
2717 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2719 if ( event
.LeftDown() )
2721 m_lastOnSame
= item
== m_current
;
2724 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2726 // only toggle the item for a single click, double click on
2727 // the button doesn't do anything (it toggles the item twice)
2728 if ( event
.LeftDown() )
2733 // don't select the item if the button was clicked
2737 // how should the selection work for this event?
2738 bool is_multiple
, extended_select
, unselect_others
;
2739 EventFlagsToSelType(GetWindowStyleFlag(),
2741 event
.ControlDown(),
2742 is_multiple
, extended_select
, unselect_others
);
2744 SelectItem(item
, unselect_others
, extended_select
);
2746 // For some reason, Windows isn't recognizing a left double-click,
2747 // so we need to simulate it here. Allow 200 milliseconds for now.
2748 if ( event
.LeftDClick() )
2750 // double clicking should not start editing the item label
2751 m_renameTimer
->Stop();
2752 m_lastOnSame
= FALSE
;
2754 // send activate event first
2755 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2756 nevent
.m_item
= (long) item
;
2757 CalcScrolledPosition(x
, y
,
2758 &nevent
.m_pointDrag
.x
,
2759 &nevent
.m_pointDrag
.y
);
2760 nevent
.SetEventObject( this );
2761 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2763 // if the user code didn't process the activate event,
2764 // handle it ourselves by toggling the item when it is
2766 if ( item
->HasPlus() )
2776 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2778 /* after all changes have been done to the tree control,
2779 * we actually redraw the tree when everything is over */
2781 if (!m_dirty
) return;
2785 CalculatePositions();
2787 AdjustMyScrollbars();
2790 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2796 dc
.SetFont(m_boldFont
);
2798 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2801 // restore normal font
2802 dc
.SetFont( m_normalFont
);
2806 int image
= item
->GetCurrentImage();
2807 if ( image
!= NO_IMAGE
)
2809 if ( m_imageListNormal
)
2811 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2816 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2819 total_h
+= 2; // at least 2 pixels
2821 total_h
+= total_h
/10; // otherwise 10% extra spacing
2823 item
->SetHeight(total_h
);
2824 if (total_h
>m_lineHeight
)
2825 m_lineHeight
=total_h
;
2827 item
->SetWidth(image_w
+text_w
+2);
2830 // -----------------------------------------------------------------------------
2831 // for developper : y is now the top of the level
2832 // not the middle of it !
2833 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2835 int x
= level
*m_indent
;
2836 if (!HasFlag(wxTR_HIDE_ROOT
))
2840 else if (level
== 0)
2842 // a hidden root is not evaluated, but its
2843 // children are always calculated
2847 CalculateSize( item
, dc
);
2850 item
->SetX( x
+m_spacing
);
2852 y
+= GetLineHeight(item
);
2854 if ( !item
->IsExpanded() )
2856 // we don't need to calculate collapsed branches
2861 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2862 size_t n
, count
= children
.Count();
2864 for (n
= 0; n
< count
; ++n
)
2865 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2868 void wxGenericTreeCtrl::CalculatePositions()
2870 if ( !m_anchor
) return;
2872 wxClientDC
dc(this);
2875 dc
.SetFont( m_normalFont
);
2877 dc
.SetPen( m_dottedPen
);
2878 //if(GetImageList() == NULL)
2879 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2882 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2885 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2887 if (m_dirty
) return;
2889 wxClientDC
dc(this);
2894 GetClientSize( &cw
, &ch
);
2897 rect
.x
= dc
.LogicalToDeviceX( 0 );
2899 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2902 Refresh( TRUE
, &rect
);
2904 AdjustMyScrollbars();
2907 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2909 if (m_dirty
) return;
2911 wxClientDC
dc(this);
2916 GetClientSize( &cw
, &ch
);
2919 rect
.x
= dc
.LogicalToDeviceX( 0 );
2920 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2922 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2924 Refresh( TRUE
, &rect
);
2927 void wxGenericTreeCtrl::RefreshSelected()
2929 // TODO: this is awfully inefficient, we should keep the list of all
2930 // selected items internally, should be much faster
2932 RefreshSelectedUnder(m_anchor
);
2935 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2937 if ( item
->IsSelected() )
2940 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2941 size_t count
= children
.GetCount();
2942 for ( size_t n
= 0; n
< count
; n
++ )
2944 RefreshSelectedUnder(children
[n
]);
2948 // ----------------------------------------------------------------------------
2949 // changing colours: we need to refresh the tree control
2950 // ----------------------------------------------------------------------------
2952 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
2954 if ( !wxWindow::SetBackgroundColour(colour
) )
2962 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
2964 if ( !wxWindow::SetForegroundColour(colour
) )
2972 #endif // wxUSE_TREECTRL