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 and Julian Smart
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 #include "wx/renderer.h"
43 // -----------------------------------------------------------------------------
45 // -----------------------------------------------------------------------------
47 class WXDLLEXPORT wxGenericTreeItem
;
49 WX_DEFINE_EXPORTED_ARRAY_NO_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 static const int NO_IMAGE
= -1;
57 static const int PIXELS_PER_UNIT
= 10;
59 // -----------------------------------------------------------------------------
61 // -----------------------------------------------------------------------------
63 // timer used for enabling in-place edit
64 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
67 // start editing the current item after half a second (if the mouse hasn't
68 // been clicked/moved)
71 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
73 virtual void Notify();
76 wxGenericTreeCtrl
*m_owner
;
78 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
81 // control used for in-place edit
82 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
85 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
88 void OnChar( wxKeyEvent
&event
);
89 void OnKeyUp( wxKeyEvent
&event
);
90 void OnKillFocus( wxFocusEvent
&event
);
96 wxGenericTreeCtrl
*m_owner
;
97 wxGenericTreeItem
*m_itemEdited
;
98 wxString m_startValue
;
101 DECLARE_EVENT_TABLE()
102 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
105 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
106 // for a sufficiently long time
107 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
110 // reset the current prefix after half a second of inactivity
111 enum { DELAY
= 500 };
113 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
115 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
118 wxGenericTreeCtrl
*m_owner
;
120 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
124 class WXDLLEXPORT wxGenericTreeItem
128 wxGenericTreeItem() { m_data
= NULL
; }
129 wxGenericTreeItem( wxGenericTreeItem
*parent
,
130 const wxString
& text
,
133 wxTreeItemData
*data
);
135 ~wxGenericTreeItem();
138 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
140 const wxString
& GetText() const { return m_text
; }
141 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
142 { return m_images
[which
]; }
143 wxTreeItemData
*GetData() const { return m_data
; }
145 // returns the current image for the item (depending on its
146 // selected/expanded/whatever state)
147 int GetCurrentImage() const;
149 void SetText( const wxString
&text
);
150 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
151 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
153 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
155 void SetBold(bool bold
) { m_isBold
= bold
; }
157 int GetX() const { return m_x
; }
158 int GetY() const { return m_y
; }
160 void SetX(int x
) { m_x
= x
; }
161 void SetY(int y
) { m_y
= y
; }
163 int GetHeight() const { return m_height
; }
164 int GetWidth() const { return m_width
; }
166 void SetHeight(int h
) { m_height
= h
; }
167 void SetWidth(int w
) { m_width
= w
; }
169 wxGenericTreeItem
*GetParent() const { return m_parent
; }
172 // deletes all children notifying the treectrl about it if !NULL
174 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
176 // get count of all children (and grand children if 'recursively')
177 size_t GetChildrenCount(bool recursively
= TRUE
) const;
179 void Insert(wxGenericTreeItem
*child
, size_t index
)
180 { m_children
.Insert(child
, index
); }
182 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
184 // return the item at given position (or NULL if no item), onButton is
185 // TRUE if the point belongs to the item's button, otherwise it lies
186 // on the button's label
187 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
188 const wxGenericTreeCtrl
*,
192 void Expand() { m_isCollapsed
= FALSE
; }
193 void Collapse() { m_isCollapsed
= TRUE
; }
195 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
198 bool HasChildren() const { return !m_children
.IsEmpty(); }
199 bool IsSelected() const { return m_hasHilight
!= 0; }
200 bool IsExpanded() const { return !m_isCollapsed
; }
201 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
202 bool IsBold() const { return m_isBold
!= 0; }
205 // get them - may be NULL
206 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
207 // get them ensuring that the pointer is not NULL
208 wxTreeItemAttr
& Attr()
212 m_attr
= new wxTreeItemAttr
;
218 void SetAttributes(wxTreeItemAttr
*attr
)
220 if ( m_ownsAttr
) delete m_attr
;
224 // set them and delete when done
225 void AssignAttributes(wxTreeItemAttr
*attr
)
232 // since there can be very many of these, we save size by chosing
233 // the smallest representation for the elements and by ordering
234 // the members to avoid padding.
235 wxString m_text
; // label to be rendered for item
237 wxTreeItemData
*m_data
; // user-provided data
239 wxArrayGenericTreeItems m_children
; // list of children
240 wxGenericTreeItem
*m_parent
; // parent of this item
242 wxTreeItemAttr
*m_attr
; // attributes???
244 // tree ctrl images for the normal, selected, expanded and
245 // expanded+selected states
246 short m_images
[wxTreeItemIcon_Max
];
248 wxCoord m_x
; // (virtual) offset from top
249 wxCoord m_y
; // (virtual) offset from left
250 short m_width
; // width of this item
251 unsigned char m_height
; // height of this item
253 // use bitfields to save size
254 int m_isCollapsed
:1;
255 int m_hasHilight
:1; // same as focused
256 int m_hasPlus
:1; // used for item which doesn't have
257 // children but has a [+] button
258 int m_isBold
:1; // render the label in bold font
259 int m_ownsAttr
:1; // delete attribute when done
261 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
264 // =============================================================================
266 // =============================================================================
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 // translate the key or mouse event flags to the type of selection we're
274 static void EventFlagsToSelType(long style
,
278 bool &extended_select
,
279 bool &unselect_others
)
281 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
282 extended_select
= shiftDown
&& is_multiple
;
283 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
286 // check if the given item is under another one
287 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
291 if ( item
== parent
)
293 // item is a descendant of parent
297 item
= item
->GetParent();
303 // -----------------------------------------------------------------------------
304 // wxTreeRenameTimer (internal)
305 // -----------------------------------------------------------------------------
307 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
312 void wxTreeRenameTimer::Notify()
314 m_owner
->OnRenameTimer();
317 //-----------------------------------------------------------------------------
318 // wxTreeTextCtrl (internal)
319 //-----------------------------------------------------------------------------
321 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
322 EVT_CHAR (wxTreeTextCtrl::OnChar
)
323 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
324 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
327 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
328 wxGenericTreeItem
*item
)
329 : m_itemEdited(item
), m_startValue(item
->GetText())
334 int w
= m_itemEdited
->GetWidth(),
335 h
= m_itemEdited
->GetHeight();
338 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
343 int image
= item
->GetCurrentImage();
344 if ( image
!= NO_IMAGE
)
346 if ( m_owner
->m_imageListNormal
)
348 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
353 wxFAIL_MSG(_T("you must create an image list to use images!"));
357 // FIXME: what are all these hardcoded 4, 8 and 11s really?
361 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
362 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
365 bool wxTreeTextCtrl::AcceptChanges()
367 const wxString value
= GetValue();
369 if ( value
== m_startValue
)
371 // nothing changed, always accept
375 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
377 // vetoed by the user
381 // accepted, do rename the item
382 m_owner
->SetItemText(m_itemEdited
, value
);
387 void wxTreeTextCtrl::Finish()
391 m_owner
->ResetTextControl();
393 wxPendingDelete
.Append(this);
397 m_owner
->SetFocus(); // This doesn't work. TODO.
401 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
403 switch ( event
.m_keyCode
)
406 if ( !AcceptChanges() )
408 // vetoed by the user, don't disappear
415 m_owner
->OnRenameCancelled(m_itemEdited
);
423 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
427 // auto-grow the textctrl:
428 wxSize parentSize
= m_owner
->GetSize();
429 wxPoint myPos
= GetPosition();
430 wxSize mySize
= GetSize();
432 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
433 if (myPos
.x
+ sx
> parentSize
.x
)
434 sx
= parentSize
.x
- myPos
.x
;
443 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
451 if ( AcceptChanges() )
457 // -----------------------------------------------------------------------------
459 // -----------------------------------------------------------------------------
461 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
462 const wxString
& text
,
463 int image
, int selImage
,
464 wxTreeItemData
*data
)
467 m_images
[wxTreeItemIcon_Normal
] = image
;
468 m_images
[wxTreeItemIcon_Selected
] = selImage
;
469 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
470 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
475 m_isCollapsed
= TRUE
;
476 m_hasHilight
= FALSE
;
482 m_attr
= (wxTreeItemAttr
*)NULL
;
485 // We don't know the height here yet.
490 wxGenericTreeItem::~wxGenericTreeItem()
494 if (m_ownsAttr
) delete m_attr
;
496 wxASSERT_MSG( m_children
.IsEmpty(),
497 wxT("please call DeleteChildren() before deleting the item") );
500 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
502 size_t count
= m_children
.Count();
503 for ( size_t n
= 0; n
< count
; n
++ )
505 wxGenericTreeItem
*child
= m_children
[n
];
507 tree
->SendDeleteEvent(child
);
509 child
->DeleteChildren(tree
);
516 void wxGenericTreeItem::SetText( const wxString
&text
)
521 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
523 size_t count
= m_children
.Count();
527 size_t total
= count
;
528 for (size_t n
= 0; n
< count
; ++n
)
530 total
+= m_children
[n
]->GetChildrenCount();
536 void wxGenericTreeItem::GetSize( int &x
, int &y
,
537 const wxGenericTreeCtrl
*theButton
)
539 int bottomY
=m_y
+theButton
->GetLineHeight(this);
540 if ( y
< bottomY
) y
= bottomY
;
541 int width
= m_x
+ m_width
;
542 if ( x
< width
) x
= width
;
546 size_t count
= m_children
.Count();
547 for ( size_t n
= 0; n
< count
; ++n
)
549 m_children
[n
]->GetSize( x
, y
, theButton
);
554 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
555 const wxGenericTreeCtrl
*theCtrl
,
559 // for a hidden root node, don't evaluate it, but do evaluate children
560 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
563 int h
= theCtrl
->GetLineHeight(this);
564 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
566 int y_mid
= m_y
+ h
/2;
567 if (point
.y
< y_mid
)
568 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
570 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
572 // 5 is the size of the plus sign
573 int xCross
= m_x
- theCtrl
->GetSpacing();
574 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
575 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
576 HasPlus() && theCtrl
->HasButtons() )
578 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
582 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
587 // assuming every image (normal and selected) has the same size!
588 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
589 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
592 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
593 flags
|= wxTREE_HITTEST_ONITEMICON
;
595 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
601 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
602 if (point
.x
> m_x
+m_width
)
603 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
608 // if children are expanded, fall through to evaluate them
609 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
613 size_t count
= m_children
.Count();
614 for ( size_t n
= 0; n
< count
; n
++ )
616 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
624 return (wxGenericTreeItem
*) NULL
;
627 int wxGenericTreeItem::GetCurrentImage() const
629 int image
= NO_IMAGE
;
634 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
637 if ( image
== NO_IMAGE
)
639 // we usually fall back to the normal item, but try just the
640 // expanded one (and not selected) first in this case
641 image
= GetImage(wxTreeItemIcon_Expanded
);
647 image
= GetImage(wxTreeItemIcon_Selected
);
650 // maybe it doesn't have the specific image we want,
651 // try the default one instead
652 if ( image
== NO_IMAGE
) image
= GetImage();
657 // -----------------------------------------------------------------------------
658 // wxGenericTreeCtrl implementation
659 // -----------------------------------------------------------------------------
661 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
663 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
664 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
665 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
666 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
667 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
668 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
671 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
673 * wxTreeCtrl has to be a real class or we have problems with
674 * the run-time information.
677 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
680 // -----------------------------------------------------------------------------
681 // construction/destruction
682 // -----------------------------------------------------------------------------
684 void wxGenericTreeCtrl::Init()
686 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
694 m_hilightBrush
= new wxBrush
696 wxSystemSettings::GetColour
698 wxSYS_COLOUR_HIGHLIGHT
703 m_hilightUnfocusedBrush
= new wxBrush
705 wxSystemSettings::GetColour
707 wxSYS_COLOUR_BTNSHADOW
712 m_imageListNormal
= m_imageListButtons
=
713 m_imageListState
= (wxImageList
*) NULL
;
714 m_ownsImageListNormal
= m_ownsImageListButtons
=
715 m_ownsImageListState
= FALSE
;
718 m_isDragging
= FALSE
;
719 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
722 m_renameTimer
= NULL
;
725 m_lastOnSame
= FALSE
;
727 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
728 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
729 m_normalFont
.GetFamily(),
730 m_normalFont
.GetStyle(),
732 m_normalFont
.GetUnderlined(),
733 m_normalFont
.GetFaceName(),
734 m_normalFont
.GetEncoding());
737 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
742 const wxValidator
&validator
,
743 const wxString
& name
)
747 wxGetOsVersion( &major
, &minor
);
749 style
&= ~wxTR_LINES_AT_ROOT
;
750 style
|= wxTR_NO_LINES
;
752 style
|= wxTR_ROW_LINES
;
755 wxScrolledWindow::Create( parent
, id
, pos
, size
,
756 style
|wxHSCROLL
|wxVSCROLL
, name
);
758 // If the tree display has no buttons, but does have
759 // connecting lines, we can use a narrower layout.
760 // It may not be a good idea to force this...
761 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
768 SetValidator( validator
);
771 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
772 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
774 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
775 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
780 wxGenericTreeCtrl::~wxGenericTreeCtrl()
782 delete m_hilightBrush
;
783 delete m_hilightUnfocusedBrush
;
787 delete m_renameTimer
;
790 if (m_ownsImageListNormal
)
791 delete m_imageListNormal
;
792 if (m_ownsImageListState
)
793 delete m_imageListState
;
794 if (m_ownsImageListButtons
)
795 delete m_imageListButtons
;
798 // -----------------------------------------------------------------------------
800 // -----------------------------------------------------------------------------
802 size_t wxGenericTreeCtrl::GetCount() const
804 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
807 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
809 m_indent
= (unsigned short) indent
;
813 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
815 m_spacing
= (unsigned short) spacing
;
819 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
821 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
823 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
826 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
828 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
830 // if we will hide the root, make sure children are visible
831 m_anchor
->SetHasPlus();
833 CalculatePositions();
836 // right now, just sets the styles. Eventually, we may
837 // want to update the inherited styles, but right now
838 // none of the parents has updatable styles
839 m_windowStyle
= styles
;
843 // -----------------------------------------------------------------------------
844 // functions to work with tree items
845 // -----------------------------------------------------------------------------
847 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
849 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
851 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
854 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
855 wxTreeItemIcon which
) const
857 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
859 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
862 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
864 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
866 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
869 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
871 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
873 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
874 return pItem
->Attr().GetTextColour();
877 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
879 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
881 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
882 return pItem
->Attr().GetBackgroundColour();
885 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
887 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
889 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
890 return pItem
->Attr().GetFont();
893 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
895 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
898 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
899 pItem
->SetText(text
);
900 CalculateSize(pItem
, dc
);
904 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
906 wxTreeItemIcon which
)
908 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
910 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
911 pItem
->SetImage(image
, which
);
914 CalculateSize(pItem
, dc
);
918 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
920 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
922 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
925 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
927 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
929 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
930 pItem
->SetHasPlus(has
);
934 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
936 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
938 // avoid redrawing the tree if no real change
939 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
940 if ( pItem
->IsBold() != bold
)
942 pItem
->SetBold(bold
);
947 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
950 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
952 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
953 pItem
->Attr().SetTextColour(col
);
957 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
960 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
962 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
963 pItem
->Attr().SetBackgroundColour(col
);
967 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
969 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
971 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
972 pItem
->Attr().SetFont(font
);
976 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
978 wxScrolledWindow::SetFont(font
);
980 m_normalFont
= font
;
981 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
982 m_normalFont
.GetFamily(),
983 m_normalFont
.GetStyle(),
985 m_normalFont
.GetUnderlined(),
986 m_normalFont
.GetFaceName(),
987 m_normalFont
.GetEncoding());
993 // -----------------------------------------------------------------------------
994 // item status inquiries
995 // -----------------------------------------------------------------------------
997 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
999 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1001 // An item is only visible if it's not a descendant of a collapsed item
1002 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1003 wxGenericTreeItem
* parent
= pItem
->GetParent();
1006 if (!parent
->IsExpanded())
1008 parent
= parent
->GetParent();
1012 GetViewStart(& startX
, & startY
);
1014 wxSize clientSize
= GetClientSize();
1017 if (!GetBoundingRect(item
, rect
))
1019 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1021 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1023 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1029 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1031 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1033 // consider that the item does have children if it has the "+" button: it
1034 // might not have them (if it had never been expanded yet) but then it
1035 // could have them as well and it's better to err on this side rather than
1036 // disabling some operations which are restricted to the items with
1037 // children for an item which does have them
1038 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1041 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1043 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1045 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1048 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1050 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1052 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1055 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1057 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1059 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1062 // -----------------------------------------------------------------------------
1064 // -----------------------------------------------------------------------------
1066 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1068 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1070 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1073 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1074 wxTreeItemIdValue
& cookie
) const
1076 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1079 return GetNextChild(item
, cookie
);
1082 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1083 wxTreeItemIdValue
& cookie
) const
1085 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1087 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1089 // it's ok to cast cookie to size_t, we never have indices big enough to
1090 // overflow "void *"
1091 size_t *pIndex
= (size_t *)&cookie
;
1092 if ( *pIndex
< children
.Count() )
1094 return children
.Item((*pIndex
)++);
1098 // there are no more of them
1099 return wxTreeItemId();
1103 #if WXWIN_COMPATIBILITY_2_4
1105 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1108 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1111 return GetNextChild(item
, cookie
);
1114 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1117 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1119 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1120 if ( (size_t)cookie
< children
.Count() )
1122 return children
.Item((size_t)cookie
++);
1126 // there are no more of them
1127 return wxTreeItemId();
1131 #endif // WXWIN_COMPATIBILITY_2_4
1133 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1135 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1137 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1138 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1141 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1143 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1145 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1146 wxGenericTreeItem
*parent
= i
->GetParent();
1147 if ( parent
== NULL
)
1149 // root item doesn't have any siblings
1150 return wxTreeItemId();
1153 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1154 int index
= siblings
.Index(i
);
1155 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1157 size_t n
= (size_t)(index
+ 1);
1158 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1161 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1163 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1165 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1166 wxGenericTreeItem
*parent
= i
->GetParent();
1167 if ( parent
== NULL
)
1169 // root item doesn't have any siblings
1170 return wxTreeItemId();
1173 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1174 int index
= siblings
.Index(i
);
1175 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1177 return index
== 0 ? wxTreeItemId()
1178 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1181 // Only for internal use right now, but should probably be public
1182 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1184 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1186 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1188 // First see if there are any children.
1189 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1190 if (children
.GetCount() > 0)
1192 return children
.Item(0);
1196 // Try a sibling of this or ancestor instead
1197 wxTreeItemId p
= item
;
1198 wxTreeItemId toFind
;
1201 toFind
= GetNextSibling(p
);
1202 p
= GetItemParent(p
);
1203 } while (p
.IsOk() && !toFind
.IsOk());
1208 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1210 wxTreeItemId id
= GetRootItem();
1219 } while (id
.IsOk());
1221 return wxTreeItemId();
1224 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1226 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1228 wxTreeItemId id
= item
;
1231 while (id
= GetNext(id
), id
.IsOk())
1237 return wxTreeItemId();
1240 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1242 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1244 wxFAIL_MSG(wxT("not implemented"));
1246 return wxTreeItemId();
1249 // called by wxTextTreeCtrl when it marks itself for deletion
1250 void wxGenericTreeCtrl::ResetTextControl()
1255 // find the first item starting with the given prefix after the given item
1256 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1257 const wxString
& prefixOrig
) const
1259 // match is case insensitive as this is more convenient to the user: having
1260 // to press Shift-letter to go to the item starting with a capital letter
1261 // would be too bothersome
1262 wxString prefix
= prefixOrig
.Lower();
1264 // determine the starting point: we shouldn't take the current item (this
1265 // allows to switch between two items starting with the same letter just by
1266 // pressing it) but we shouldn't jump to the next one if the user is
1267 // continuing to type as otherwise he might easily skip the item he wanted
1268 wxTreeItemId id
= idParent
;
1269 if ( prefix
.length() == 1 )
1274 // look for the item starting with the given prefix after it
1275 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1280 // if we haven't found anything...
1283 // ... wrap to the beginning
1285 if ( HasFlag(wxTR_HIDE_ROOT
) )
1287 // can't select virtual root
1291 // and try all the items (stop when we get to the one we started from)
1292 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1301 // -----------------------------------------------------------------------------
1303 // -----------------------------------------------------------------------------
1305 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1307 const wxString
& text
,
1308 int image
, int selImage
,
1309 wxTreeItemData
*data
)
1311 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1314 // should we give a warning here?
1315 return AddRoot(text
, image
, selImage
, data
);
1318 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1320 wxGenericTreeItem
*item
=
1321 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1325 data
->m_pItem
= item
;
1328 parent
->Insert( item
, previous
);
1333 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1334 int image
, int selImage
,
1335 wxTreeItemData
*data
)
1337 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1339 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1341 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1342 image
, selImage
, data
);
1345 data
->m_pItem
= m_anchor
;
1348 if (HasFlag(wxTR_HIDE_ROOT
))
1350 // if root is hidden, make sure we can navigate
1352 m_anchor
->SetHasPlus();
1354 CalculatePositions();
1357 if (!HasFlag(wxTR_MULTIPLE
))
1359 m_current
= m_key_current
= m_anchor
;
1360 m_current
->SetHilight( TRUE
);
1366 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1367 const wxString
& text
,
1368 int image
, int selImage
,
1369 wxTreeItemData
*data
)
1371 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1374 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1375 const wxTreeItemId
& idPrevious
,
1376 const wxString
& text
,
1377 int image
, int selImage
,
1378 wxTreeItemData
*data
)
1380 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1383 // should we give a warning here?
1384 return AddRoot(text
, image
, selImage
, data
);
1388 if (idPrevious
.IsOk())
1390 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1391 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1392 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1395 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1398 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1400 const wxString
& text
,
1401 int image
, int selImage
,
1402 wxTreeItemData
*data
)
1404 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1407 // should we give a warning here?
1408 return AddRoot(text
, image
, selImage
, data
);
1411 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1414 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1415 const wxString
& text
,
1416 int image
, int selImage
,
1417 wxTreeItemData
*data
)
1419 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1422 // should we give a warning here?
1423 return AddRoot(text
, image
, selImage
, data
);
1426 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1427 image
, selImage
, data
);
1430 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1432 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1433 event
.m_item
= item
;
1434 event
.SetEventObject( this );
1435 ProcessEvent( event
);
1438 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1440 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1442 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1443 item
->DeleteChildren(this);
1446 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1448 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1450 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1452 wxGenericTreeItem
*parent
= item
->GetParent();
1454 // don't keep stale pointers around!
1455 if ( IsDescendantOf(item
, m_key_current
) )
1457 // Don't silently change the selection:
1458 // do it properly in idle time, so event
1459 // handlers get called.
1461 // m_key_current = parent;
1462 m_key_current
= NULL
;
1465 // m_select_me records whether we need to select
1466 // a different item, in idle time.
1467 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1469 m_select_me
= parent
;
1472 if ( IsDescendantOf(item
, m_current
) )
1474 // Don't silently change the selection:
1475 // do it properly in idle time, so event
1476 // handlers get called.
1478 // m_current = parent;
1480 m_select_me
= parent
;
1483 // remove the item from the tree
1486 parent
->GetChildren().Remove( item
); // remove by value
1488 else // deleting the root
1490 // nothing will be left in the tree
1494 // and delete all of its children and the item itself now
1495 item
->DeleteChildren(this);
1496 SendDeleteEvent(item
);
1500 void wxGenericTreeCtrl::DeleteAllItems()
1508 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1510 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1512 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1513 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1514 _T("can't expand hidden root") );
1516 if ( !item
->HasPlus() )
1519 if ( item
->IsExpanded() )
1522 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1523 event
.m_item
= item
;
1524 event
.SetEventObject( this );
1526 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1528 // cancelled by program
1533 CalculatePositions();
1535 RefreshSubtree(item
);
1537 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1538 ProcessEvent( event
);
1541 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1543 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1546 if ( !IsExpanded(item
) )
1550 wxTreeItemIdValue cookie
;
1551 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1552 while ( child
.IsOk() )
1556 child
= GetNextChild(item
, cookie
);
1560 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1562 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1563 _T("can't collapse hidden root") );
1565 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1567 if ( !item
->IsExpanded() )
1570 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1571 event
.m_item
= item
;
1572 event
.SetEventObject( this );
1573 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1575 // cancelled by program
1581 #if 0 // TODO why should items be collapsed recursively?
1582 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1583 size_t count
= children
.Count();
1584 for ( size_t n
= 0; n
< count
; n
++ )
1586 Collapse(children
[n
]);
1590 CalculatePositions();
1592 RefreshSubtree(item
);
1594 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1595 ProcessEvent( event
);
1598 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1601 DeleteChildren(item
);
1604 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1606 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1608 if (item
->IsExpanded())
1614 void wxGenericTreeCtrl::Unselect()
1618 m_current
->SetHilight( FALSE
);
1619 RefreshLine( m_current
);
1626 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1628 if (item
->IsSelected())
1630 item
->SetHilight(FALSE
);
1634 if (item
->HasChildren())
1636 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1637 size_t count
= children
.Count();
1638 for ( size_t n
= 0; n
< count
; ++n
)
1640 UnselectAllChildren(children
[n
]);
1645 void wxGenericTreeCtrl::UnselectAll()
1647 wxTreeItemId rootItem
= GetRootItem();
1649 // the tree might not have the root item at all
1652 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1656 // Recursive function !
1657 // To stop we must have crt_item<last_item
1659 // Tag all next children, when no more children,
1660 // Move to parent (not to tag)
1661 // Keep going... if we found last_item, we stop.
1662 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1664 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1666 if (parent
== NULL
) // This is root item
1667 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1669 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1670 int index
= children
.Index(crt_item
);
1671 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1673 size_t count
= children
.Count();
1674 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1676 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1679 return TagNextChildren(parent
, last_item
, select
);
1682 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1684 crt_item
->SetHilight(select
);
1685 RefreshLine(crt_item
);
1687 if (crt_item
==last_item
)
1690 if (crt_item
->HasChildren())
1692 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1693 size_t count
= children
.Count();
1694 for ( size_t n
= 0; n
< count
; ++n
)
1696 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1704 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1706 // item2 is not necessary after item1
1707 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1710 // choice first' and 'last' between item1 and item2
1711 if (item1
->GetY()<item2
->GetY())
1722 bool select
= m_current
->IsSelected();
1724 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1727 TagNextChildren(first
,last
,select
);
1730 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1731 bool unselect_others
,
1732 bool extended_select
)
1734 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1738 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1739 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1741 //wxCHECK_RET( ( (!unselect_others) && is_single),
1742 // wxT("this is a single selection tree") );
1744 // to keep going anyhow !!!
1747 if (item
->IsSelected())
1748 return; // nothing to do
1749 unselect_others
= TRUE
;
1750 extended_select
= FALSE
;
1752 else if ( unselect_others
&& item
->IsSelected() )
1754 // selection change if there is more than one item currently selected
1755 wxArrayTreeItemIds selected_items
;
1756 if ( GetSelections(selected_items
) == 1 )
1760 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1761 event
.m_item
= item
;
1762 event
.m_itemOld
= m_current
;
1763 event
.SetEventObject( this );
1764 // TODO : Here we don't send any selection mode yet !
1766 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1769 wxTreeItemId parent
= GetItemParent( itemId
);
1770 while (parent
.IsOk())
1772 if (!IsExpanded(parent
))
1775 parent
= GetItemParent( parent
);
1778 EnsureVisible( itemId
);
1781 if (unselect_others
)
1783 if (is_single
) Unselect(); // to speed up thing
1788 if (extended_select
)
1792 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1795 // don't change the mark (m_current)
1796 SelectItemRange(m_current
, item
);
1800 bool select
=TRUE
; // the default
1802 // Check if we need to toggle hilight (ctrl mode)
1803 if (!unselect_others
)
1804 select
=!item
->IsSelected();
1806 m_current
= m_key_current
= item
;
1807 m_current
->SetHilight(select
);
1808 RefreshLine( m_current
);
1811 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1812 GetEventHandler()->ProcessEvent( event
);
1815 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1816 wxArrayTreeItemIds
&array
) const
1818 if ( item
->IsSelected() )
1819 array
.Add(wxTreeItemId(item
));
1821 if ( item
->HasChildren() )
1823 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1824 size_t count
= children
.GetCount();
1825 for ( size_t n
= 0; n
< count
; ++n
)
1826 FillArray(children
[n
], array
);
1830 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1833 wxTreeItemId idRoot
= GetRootItem();
1834 if ( idRoot
.IsOk() )
1836 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1838 //else: the tree is empty, so no selections
1840 return array
.Count();
1843 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1845 if (!item
.IsOk()) return;
1847 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1849 // first expand all parent branches
1850 wxGenericTreeItem
*parent
= gitem
->GetParent();
1852 if ( HasFlag(wxTR_HIDE_ROOT
) )
1854 while ( parent
&& parent
!= m_anchor
)
1857 parent
= parent
->GetParent();
1865 parent
= parent
->GetParent();
1869 //if (parent) CalculatePositions();
1874 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1876 if (!item
.IsOk()) return;
1878 // We have to call this here because the label in
1879 // question might just have been added and no screen
1880 // update taken place.
1881 if (m_dirty
) wxYieldIfNeeded();
1883 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1885 // now scroll to the item
1886 int item_y
= gitem
->GetY();
1890 GetViewStart( &start_x
, &start_y
);
1891 start_y
*= PIXELS_PER_UNIT
;
1895 GetClientSize( &client_w
, &client_h
);
1897 if (item_y
< start_y
+3)
1902 m_anchor
->GetSize( x
, y
, this );
1903 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1904 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1905 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1906 // Item should appear at top
1907 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1909 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1914 m_anchor
->GetSize( x
, y
, this );
1915 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1916 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1917 item_y
+= PIXELS_PER_UNIT
+2;
1918 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1919 // Item should appear at bottom
1920 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
);
1924 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1925 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1927 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1928 wxGenericTreeItem
**item2
)
1930 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1932 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1935 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1936 const wxTreeItemId
& item2
)
1938 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1941 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1943 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1945 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1947 wxCHECK_RET( !s_treeBeingSorted
,
1948 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1950 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1951 if ( children
.Count() > 1 )
1955 s_treeBeingSorted
= this;
1956 children
.Sort(tree_ctrl_compare_func
);
1957 s_treeBeingSorted
= NULL
;
1959 //else: don't make the tree dirty as nothing changed
1962 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1964 return m_imageListNormal
;
1967 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1969 return m_imageListButtons
;
1972 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1974 return m_imageListState
;
1977 void wxGenericTreeCtrl::CalculateLineHeight()
1979 wxClientDC
dc(this);
1980 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1982 if ( m_imageListNormal
)
1984 // Calculate a m_lineHeight value from the normal Image sizes.
1985 // May be toggle off. Then wxGenericTreeCtrl will spread when
1986 // necessary (which might look ugly).
1987 int n
= m_imageListNormal
->GetImageCount();
1988 for (int i
= 0; i
< n
; i
++)
1990 int width
= 0, height
= 0;
1991 m_imageListNormal
->GetSize(i
, width
, height
);
1992 if (height
> m_lineHeight
) m_lineHeight
= height
;
1996 if (m_imageListButtons
)
1998 // Calculate a m_lineHeight value from the Button image sizes.
1999 // May be toggle off. Then wxGenericTreeCtrl will spread when
2000 // necessary (which might look ugly).
2001 int n
= m_imageListButtons
->GetImageCount();
2002 for (int i
= 0; i
< n
; i
++)
2004 int width
= 0, height
= 0;
2005 m_imageListButtons
->GetSize(i
, width
, height
);
2006 if (height
> m_lineHeight
) m_lineHeight
= height
;
2010 if (m_lineHeight
< 30)
2011 m_lineHeight
+= 2; // at least 2 pixels
2013 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2016 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2018 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2019 m_imageListNormal
= imageList
;
2020 m_ownsImageListNormal
= FALSE
;
2022 // Don't do any drawing if we're setting the list to NULL,
2023 // since we may be in the process of deleting the tree control.
2025 CalculateLineHeight();
2028 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2030 if (m_ownsImageListState
) delete m_imageListState
;
2031 m_imageListState
= imageList
;
2032 m_ownsImageListState
= FALSE
;
2035 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2037 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2038 m_imageListButtons
= imageList
;
2039 m_ownsImageListButtons
= FALSE
;
2041 CalculateLineHeight();
2044 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2046 SetImageList(imageList
);
2047 m_ownsImageListNormal
= TRUE
;
2050 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2052 SetStateImageList(imageList
);
2053 m_ownsImageListState
= TRUE
;
2056 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2058 SetButtonsImageList(imageList
);
2059 m_ownsImageListButtons
= TRUE
;
2062 // -----------------------------------------------------------------------------
2064 // -----------------------------------------------------------------------------
2066 void wxGenericTreeCtrl::AdjustMyScrollbars()
2071 m_anchor
->GetSize( x
, y
, this );
2072 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2073 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2074 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2075 int y_pos
= GetScrollPos( wxVERTICAL
);
2076 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2080 SetScrollbars( 0, 0, 0, 0 );
2084 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2086 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2087 return item
->GetHeight();
2089 return m_lineHeight
;
2092 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2094 // TODO implement "state" icon on items
2096 wxTreeItemAttr
*attr
= item
->GetAttributes();
2097 if ( attr
&& attr
->HasFont() )
2098 dc
.SetFont(attr
->GetFont());
2099 else if (item
->IsBold())
2100 dc
.SetFont(m_boldFont
);
2102 long text_w
= 0, text_h
= 0;
2103 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2105 int image_h
= 0, image_w
= 0;
2106 int image
= item
->GetCurrentImage();
2107 if ( image
!= NO_IMAGE
)
2109 if ( m_imageListNormal
)
2111 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2120 int total_h
= GetLineHeight(item
);
2122 if ( item
->IsSelected() )
2124 // under mac selections are only a rectangle in case they don't have the focus
2128 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2129 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2133 dc
.SetBrush( *m_hilightBrush
) ;
2136 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2142 if ( attr
&& attr
->HasBackgroundColour() )
2143 colBg
= attr
->GetBackgroundColour();
2145 colBg
= m_backgroundColour
;
2146 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2149 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2151 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2155 DoGetPosition(&x
, &y
);
2157 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2161 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2163 // If it's selected, and there's an image, then we should
2164 // take care to leave the area under the image painted in the
2165 // background colour.
2166 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2167 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2171 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2172 item
->GetWidth()+2, total_h
-offset
);
2176 if ( image
!= NO_IMAGE
)
2178 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2179 m_imageListNormal
->Draw( image
, dc
,
2181 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2182 wxIMAGELIST_DRAW_TRANSPARENT
);
2183 dc
.DestroyClippingRegion();
2186 dc
.SetBackgroundMode(wxTRANSPARENT
);
2187 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2188 dc
.DrawText( item
->GetText(),
2189 (wxCoord
)(image_w
+ item
->GetX()),
2190 (wxCoord
)(item
->GetY() + extraH
));
2192 // restore normal font
2193 dc
.SetFont( m_normalFont
);
2196 // Now y stands for the top of the item, whereas it used to stand for middle !
2197 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2199 int x
= level
*m_indent
;
2200 if (!HasFlag(wxTR_HIDE_ROOT
))
2204 else if (level
== 0)
2206 // always expand hidden root
2208 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2209 int count
= children
.Count();
2215 PaintLevel(children
[n
], dc
, 1, y
);
2216 } while (++n
< count
);
2218 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2220 // draw line down to last child
2221 origY
+= GetLineHeight(children
[0])>>1;
2222 oldY
+= GetLineHeight(children
[n
-1])>>1;
2223 dc
.DrawLine(3, origY
, 3, oldY
);
2229 item
->SetX(x
+m_spacing
);
2232 int h
= GetLineHeight(item
);
2234 int y_mid
= y_top
+ (h
>>1);
2237 int exposed_x
= dc
.LogicalToDeviceX(0);
2238 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2240 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2244 // don't draw rect outline if we already have the
2245 // background color under Mac
2246 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2247 #endif // !__WXMAC__
2251 if ( item
->IsSelected() )
2253 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2257 wxTreeItemAttr
*attr
= item
->GetAttributes();
2258 if (attr
&& attr
->HasTextColour())
2259 colText
= attr
->GetTextColour();
2261 colText
= GetForegroundColour();
2265 dc
.SetTextForeground(colText
);
2269 PaintItem(item
, dc
);
2271 if (HasFlag(wxTR_ROW_LINES
))
2273 // if the background colour is white, choose a
2274 // contrasting color for the lines
2275 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2276 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2277 dc
.DrawLine(0, y_top
, 10000, y_top
);
2278 dc
.DrawLine(0, y
, 10000, y
);
2281 // restore DC objects
2282 dc
.SetBrush(*wxWHITE_BRUSH
);
2283 dc
.SetPen(m_dottedPen
);
2284 dc
.SetTextForeground(*wxBLACK
);
2286 if ( !HasFlag(wxTR_NO_LINES
) )
2288 // draw the horizontal line here
2290 if (x
> (signed)m_indent
)
2291 x_start
-= m_indent
;
2292 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2294 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2297 // should the item show a button?
2298 if ( item
->HasPlus() && HasButtons() )
2300 if ( m_imageListButtons
)
2302 // draw the image button here
2305 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2306 : wxTreeItemIcon_Normal
;
2307 if ( item
->IsSelected() )
2308 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2310 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2311 int xx
= x
- image_w
/2;
2312 int yy
= y_mid
- image_h
/2;
2314 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2315 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2316 wxIMAGELIST_DRAW_TRANSPARENT
);
2318 else // no custom buttons
2320 static const int wImage
= 10;
2321 static const int hImage
= 12;
2323 wxRendererNative::Get().DrawTreeItemButton
2327 wxRect(x
- wImage
/2,
2331 ? wxCONTROL_EXPANDED
2338 if (item
->IsExpanded())
2340 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2341 int count
= children
.Count();
2348 PaintLevel(children
[n
], dc
, level
, y
);
2349 } while (++n
< count
);
2351 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2353 // draw line down to last child
2354 oldY
+= GetLineHeight(children
[n
-1])>>1;
2355 if (HasButtons()) y_mid
+= 5;
2357 // Only draw the portion of the line that is visible, in case it is huge
2358 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2359 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2360 yOrigin
= abs(yOrigin
);
2361 GetClientSize(&width
, &height
);
2363 // Move end points to the begining/end of the view?
2364 if (y_mid
< yOrigin
)
2366 if (oldY
> yOrigin
+ height
)
2367 oldY
= yOrigin
+ height
;
2369 // after the adjustments if y_mid is larger than oldY then the line
2370 // isn't visible at all so don't draw anything
2372 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2378 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2382 if ( item
->HasPlus() )
2384 // it's a folder, indicate it by a border
2389 // draw a line under the drop target because the item will be
2391 DrawLine(item
, TRUE
/* below */);
2394 SetCursor(wxCURSOR_BULLSEYE
);
2399 SetCursor(wxCURSOR_NO_ENTRY
);
2403 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2405 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2407 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2409 wxClientDC
dc(this);
2411 dc
.SetLogicalFunction(wxINVERT
);
2412 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2414 int w
= i
->GetWidth() + 2;
2415 int h
= GetLineHeight(i
) + 2;
2417 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2420 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2422 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2424 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2426 wxClientDC
dc(this);
2428 dc
.SetLogicalFunction(wxINVERT
);
2434 y
+= GetLineHeight(i
) - 1;
2437 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2440 // -----------------------------------------------------------------------------
2441 // wxWindows callbacks
2442 // -----------------------------------------------------------------------------
2444 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2452 dc
.SetFont( m_normalFont
);
2453 dc
.SetPen( m_dottedPen
);
2455 // this is now done dynamically
2456 //if(GetImageList() == NULL)
2457 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2460 PaintLevel( m_anchor
, dc
, 0, y
);
2463 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2472 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2481 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2483 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2484 te
.m_evtKey
= event
;
2485 te
.SetEventObject( this );
2486 if ( GetEventHandler()->ProcessEvent( te
) )
2488 // intercepted by the user code
2492 if ( (m_current
== 0) || (m_key_current
== 0) )
2498 // how should the selection work for this event?
2499 bool is_multiple
, extended_select
, unselect_others
;
2500 EventFlagsToSelType(GetWindowStyleFlag(),
2502 event
.ControlDown(),
2503 is_multiple
, extended_select
, unselect_others
);
2507 // * : Expand all/Collapse all
2508 // ' ' | return : activate
2509 // up : go up (not last children!)
2511 // left : go to parent
2512 // right : open if parent and go next
2513 // home : go to root
2514 // end : go to last item without opening parents
2515 // alnum : start or continue searching for the item with this prefix
2516 int keyCode
= event
.GetKeyCode();
2521 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2529 if ( !IsExpanded(m_current
) )
2532 ExpandAll(m_current
);
2535 //else: fall through to Collapse() it
2539 if (IsExpanded(m_current
))
2541 Collapse(m_current
);
2547 if ( !event
.HasModifiers() )
2549 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2550 event
.m_item
= m_current
;
2551 event
.SetEventObject( this );
2552 GetEventHandler()->ProcessEvent( event
);
2555 // in any case, also generate the normal key event for this key,
2556 // even if we generated the ACTIVATED event above: this is what
2557 // wxMSW does and it makes sense because you might not want to
2558 // process ACTIVATED event at all and handle Space and Return
2559 // directly (and differently) which would be impossible otherwise
2563 // up goes to the previous sibling or to the last
2564 // of its children if it's expanded
2567 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2570 prev
= GetItemParent( m_key_current
);
2571 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2573 break; // don't go to root if it is hidden
2577 wxTreeItemIdValue cookie
;
2578 wxTreeItemId current
= m_key_current
;
2579 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2580 if (current
== GetFirstChild( prev
, cookie
))
2582 // otherwise we return to where we came from
2583 SelectItem( prev
, unselect_others
, extended_select
);
2584 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2591 while ( IsExpanded(prev
) && HasChildren(prev
) )
2593 wxTreeItemId child
= GetLastChild(prev
);
2600 SelectItem( prev
, unselect_others
, extended_select
);
2601 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2606 // left arrow goes to the parent
2609 wxTreeItemId prev
= GetItemParent( m_current
);
2610 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2612 // don't go to root if it is hidden
2613 prev
= GetPrevSibling( m_current
);
2617 SelectItem( prev
, unselect_others
, extended_select
);
2623 // this works the same as the down arrow except that we
2624 // also expand the item if it wasn't expanded yet
2630 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2632 wxTreeItemIdValue cookie
;
2633 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2634 SelectItem( child
, unselect_others
, extended_select
);
2635 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2639 wxTreeItemId next
= GetNextSibling( m_key_current
);
2642 wxTreeItemId current
= m_key_current
;
2643 while (current
.IsOk() && !next
)
2645 current
= GetItemParent( current
);
2646 if (current
) next
= GetNextSibling( current
);
2651 SelectItem( next
, unselect_others
, extended_select
);
2652 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2658 // <End> selects the last visible tree item
2661 wxTreeItemId last
= GetRootItem();
2663 while ( last
.IsOk() && IsExpanded(last
) )
2665 wxTreeItemId lastChild
= GetLastChild(last
);
2667 // it may happen if the item was expanded but then all of
2668 // its children have been deleted - so IsExpanded() returned
2669 // TRUE, but GetLastChild() returned invalid item
2678 SelectItem( last
, unselect_others
, extended_select
);
2683 // <Home> selects the root item
2686 wxTreeItemId prev
= GetRootItem();
2690 if ( HasFlag(wxTR_HIDE_ROOT
) )
2692 wxTreeItemIdValue cookie
;
2693 prev
= GetFirstChild(prev
, cookie
);
2698 SelectItem( prev
, unselect_others
, extended_select
);
2703 // do not use wxIsalnum() here
2704 if ( !event
.HasModifiers() &&
2705 ((keyCode
>= '0' && keyCode
<= '9') ||
2706 (keyCode
>= 'a' && keyCode
<= 'z') ||
2707 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2709 // find the next item starting with the given prefix
2710 char ch
= (char)keyCode
;
2712 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2723 // also start the timer to reset the current prefix if the user
2724 // doesn't press any more alnum keys soon -- we wouldn't want
2725 // to use this prefix for a new item search
2728 m_findTimer
= new wxTreeFindTimer(this);
2731 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2740 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2742 // JACS: removed wxYieldIfNeeded() because it can cause the window
2743 // to be deleted from under us if a close window event is pending
2748 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2749 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2750 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2751 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2752 if (flags
) return wxTreeItemId();
2754 if (m_anchor
== NULL
)
2756 flags
= wxTREE_HITTEST_NOWHERE
;
2757 return wxTreeItemId();
2760 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2764 flags
= wxTREE_HITTEST_NOWHERE
;
2765 return wxTreeItemId();
2770 // get the bounding rectangle of the item (or of its label only)
2771 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2773 bool WXUNUSED(textOnly
)) const
2775 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2777 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2780 GetViewStart(& startX
, & startY
);
2782 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2783 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2784 rect
.width
= i
->GetWidth();
2785 //rect.height = i->GetHeight();
2786 rect
.height
= GetLineHeight(i
);
2791 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2793 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2795 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2797 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2798 te
.m_item
= itemEdit
;
2799 te
.SetEventObject( this );
2800 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2806 // We have to call this here because the label in
2807 // question might just have been added and no screen
2808 // update taken place.
2812 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2814 m_textCtrl
->SetFocus();
2817 // returns a pointer to the text edit control if the item is being
2818 // edited, NULL otherwise (it's assumed that no more than one item may
2819 // be edited simultaneously)
2820 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2825 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2826 const wxString
& value
)
2828 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2830 le
.SetEventObject( this );
2832 le
.m_editCancelled
= FALSE
;
2834 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2837 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2839 // let owner know that the edit was cancelled
2840 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2842 le
.SetEventObject( this );
2843 le
.m_label
= wxEmptyString
;
2844 le
.m_editCancelled
= TRUE
;
2846 GetEventHandler()->ProcessEvent( le
);
2852 void wxGenericTreeCtrl::OnRenameTimer()
2857 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2859 if ( !m_anchor
) return;
2861 // we process left mouse up event (enables in-place edit), right down
2862 // (pass to the user code), left dbl click (activate item) and
2863 // dragging/moving events for items drag-and-drop
2864 if ( !(event
.LeftDown() ||
2866 event
.RightDown() ||
2867 event
.LeftDClick() ||
2869 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2876 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2879 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2881 if ( event
.Dragging() && !m_isDragging
)
2883 if (m_dragCount
== 0)
2888 if (m_dragCount
!= 3)
2890 // wait until user drags a bit further...
2894 wxEventType command
= event
.RightIsDown()
2895 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2896 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2898 wxTreeEvent
nevent( command
, GetId() );
2899 nevent
.m_item
= m_current
;
2900 nevent
.SetEventObject(this);
2902 // by default the dragging is not supported, the user code must
2903 // explicitly allow the event for it to take place
2906 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2908 // we're going to drag this item
2909 m_isDragging
= TRUE
;
2911 // remember the old cursor because we will change it while
2913 m_oldCursor
= m_cursor
;
2915 // in a single selection control, hide the selection temporarily
2916 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2918 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2920 if ( m_oldSelection
)
2922 m_oldSelection
->SetHilight(FALSE
);
2923 RefreshLine(m_oldSelection
);
2930 else if ( event
.Moving() )
2932 if ( item
!= m_dropTarget
)
2934 // unhighlight the previous drop target
2935 DrawDropEffect(m_dropTarget
);
2937 m_dropTarget
= item
;
2939 // highlight the current drop target if any
2940 DrawDropEffect(m_dropTarget
);
2945 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2947 // erase the highlighting
2948 DrawDropEffect(m_dropTarget
);
2950 if ( m_oldSelection
)
2952 m_oldSelection
->SetHilight(TRUE
);
2953 RefreshLine(m_oldSelection
);
2954 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2957 // generate the drag end event
2958 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2960 event
.m_item
= item
;
2961 event
.m_pointDrag
= pt
;
2962 event
.SetEventObject(this);
2964 (void)GetEventHandler()->ProcessEvent(event
);
2966 m_isDragging
= FALSE
;
2967 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2971 SetCursor(m_oldCursor
);
2977 // here we process only the messages which happen on tree items
2981 if (item
== NULL
) return; /* we hit the blank area */
2983 if ( event
.RightDown() )
2985 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2986 nevent
.m_item
= item
;
2987 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2988 nevent
.SetEventObject(this);
2989 GetEventHandler()->ProcessEvent(nevent
);
2991 else if ( event
.LeftUp() )
2993 // this facilitates multiple-item drag-and-drop
2995 if (item
&& HasFlag(wxTR_MULTIPLE
))
2997 wxArrayTreeItemIds selections
;
2998 size_t count
= GetSelections(selections
);
3001 !event
.ControlDown() &&
3004 SelectItem(item
, true, false);
3010 if ( (item
== m_current
) &&
3011 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3012 HasFlag(wxTR_EDIT_LABELS
) )
3014 if ( m_renameTimer
)
3016 if ( m_renameTimer
->IsRunning() )
3017 m_renameTimer
->Stop();
3021 m_renameTimer
= new wxTreeRenameTimer( this );
3024 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3027 m_lastOnSame
= FALSE
;
3030 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3032 if ( event
.LeftDown() )
3034 m_lastOnSame
= item
== m_current
;
3037 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3039 // only toggle the item for a single click, double click on
3040 // the button doesn't do anything (it toggles the item twice)
3041 if ( event
.LeftDown() )
3046 // don't select the item if the button was clicked
3051 // clear the previously selected items, if the
3052 // user clicked outside of the present selection.
3053 // otherwise, perform the deselection on mouse-up.
3054 // this allows multiple drag and drop to work.
3056 if (!IsSelected(item
))
3058 // how should the selection work for this event?
3059 bool is_multiple
, extended_select
, unselect_others
;
3060 EventFlagsToSelType(GetWindowStyleFlag(),
3062 event
.ControlDown(),
3063 is_multiple
, extended_select
, unselect_others
);
3065 SelectItem(item
, unselect_others
, extended_select
);
3069 // For some reason, Windows isn't recognizing a left double-click,
3070 // so we need to simulate it here. Allow 200 milliseconds for now.
3071 if ( event
.LeftDClick() )
3073 // double clicking should not start editing the item label
3074 if ( m_renameTimer
)
3075 m_renameTimer
->Stop();
3077 m_lastOnSame
= FALSE
;
3079 // send activate event first
3080 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3081 nevent
.m_item
= item
;
3082 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3083 nevent
.SetEventObject( this );
3084 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3086 // if the user code didn't process the activate event,
3087 // handle it ourselves by toggling the item when it is
3089 if ( item
->HasPlus() )
3099 void wxGenericTreeCtrl::OnInternalIdle()
3101 wxWindow::OnInternalIdle();
3103 // Check if we need to select the root item
3104 // because nothing else has been selected.
3105 // Delaying it means that we can invoke event handlers
3106 // as required, when a first item is selected.
3107 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3110 SelectItem(m_select_me
);
3111 else if (GetRootItem().IsOk())
3112 SelectItem(GetRootItem());
3115 /* after all changes have been done to the tree control,
3116 * we actually redraw the tree when everything is over */
3118 if (!m_dirty
) return;
3122 CalculatePositions();
3124 AdjustMyScrollbars();
3127 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3132 wxTreeItemAttr
*attr
= item
->GetAttributes();
3133 if ( attr
&& attr
->HasFont() )
3134 dc
.SetFont(attr
->GetFont());
3135 else if ( item
->IsBold() )
3136 dc
.SetFont(m_boldFont
);
3138 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3141 // restore normal font
3142 dc
.SetFont( m_normalFont
);
3146 int image
= item
->GetCurrentImage();
3147 if ( image
!= NO_IMAGE
)
3149 if ( m_imageListNormal
)
3151 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3156 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3159 total_h
+= 2; // at least 2 pixels
3161 total_h
+= total_h
/10; // otherwise 10% extra spacing
3163 item
->SetHeight(total_h
);
3164 if (total_h
>m_lineHeight
)
3165 m_lineHeight
=total_h
;
3167 item
->SetWidth(image_w
+text_w
+2);
3170 // -----------------------------------------------------------------------------
3171 // for developper : y is now the top of the level
3172 // not the middle of it !
3173 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3175 int x
= level
*m_indent
;
3176 if (!HasFlag(wxTR_HIDE_ROOT
))
3180 else if (level
== 0)
3182 // a hidden root is not evaluated, but its
3183 // children are always calculated
3187 CalculateSize( item
, dc
);
3190 item
->SetX( x
+m_spacing
);
3192 y
+= GetLineHeight(item
);
3194 if ( !item
->IsExpanded() )
3196 // we don't need to calculate collapsed branches
3201 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3202 size_t n
, count
= children
.Count();
3204 for (n
= 0; n
< count
; ++n
)
3205 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3208 void wxGenericTreeCtrl::CalculatePositions()
3210 if ( !m_anchor
) return;
3212 wxClientDC
dc(this);
3215 dc
.SetFont( m_normalFont
);
3217 dc
.SetPen( m_dottedPen
);
3218 //if(GetImageList() == NULL)
3219 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3222 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3225 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3227 if (m_dirty
) return;
3229 wxSize client
= GetClientSize();
3232 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3233 rect
.width
= client
.x
;
3234 rect
.height
= client
.y
;
3236 Refresh(TRUE
, &rect
);
3238 AdjustMyScrollbars();
3241 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3243 if (m_dirty
) return;
3246 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3247 rect
.width
= GetClientSize().x
;
3248 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3250 Refresh(TRUE
, &rect
);
3253 void wxGenericTreeCtrl::RefreshSelected()
3255 // TODO: this is awfully inefficient, we should keep the list of all
3256 // selected items internally, should be much faster
3258 RefreshSelectedUnder(m_anchor
);
3261 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3263 if ( item
->IsSelected() )
3266 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3267 size_t count
= children
.GetCount();
3268 for ( size_t n
= 0; n
< count
; n
++ )
3270 RefreshSelectedUnder(children
[n
]);
3274 // ----------------------------------------------------------------------------
3275 // changing colours: we need to refresh the tree control
3276 // ----------------------------------------------------------------------------
3278 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3280 if ( !wxWindow::SetBackgroundColour(colour
) )
3288 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3290 if ( !wxWindow::SetForegroundColour(colour
) )
3298 #endif // wxUSE_TREECTRL