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 // -----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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_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 item'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 int xCross
= m_x
- theCtrl
->GetSpacing();
574 // according to the drawing code the triangels are drawn
575 // at -4 , -4 from the position up to +10/+10 max
576 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
577 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
578 HasPlus() && theCtrl
->HasButtons() )
580 // 5 is the size of the plus sign
581 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
582 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
583 HasPlus() && theCtrl
->HasButtons() )
586 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
590 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
595 // assuming every image (normal and selected) has the same size!
596 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
597 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
600 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
601 flags
|= wxTREE_HITTEST_ONITEMICON
;
603 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
609 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
610 if (point
.x
> m_x
+m_width
)
611 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
616 // if children are expanded, fall through to evaluate them
617 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
621 size_t count
= m_children
.Count();
622 for ( size_t n
= 0; n
< count
; n
++ )
624 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
632 return (wxGenericTreeItem
*) NULL
;
635 int wxGenericTreeItem::GetCurrentImage() const
637 int image
= NO_IMAGE
;
642 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
645 if ( image
== NO_IMAGE
)
647 // we usually fall back to the normal item, but try just the
648 // expanded one (and not selected) first in this case
649 image
= GetImage(wxTreeItemIcon_Expanded
);
655 image
= GetImage(wxTreeItemIcon_Selected
);
658 // maybe it doesn't have the specific image we want,
659 // try the default one instead
660 if ( image
== NO_IMAGE
) image
= GetImage();
665 // -----------------------------------------------------------------------------
666 // wxGenericTreeCtrl implementation
667 // -----------------------------------------------------------------------------
669 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
671 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
672 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
673 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
674 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
675 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
676 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
677 EVT_TREE_ITEM_GETTOOLTIP(-1, wxGenericTreeCtrl::OnGetToolTip
)
680 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
682 * wxTreeCtrl has to be a real class or we have problems with
683 * the run-time information.
686 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
689 // -----------------------------------------------------------------------------
690 // construction/destruction
691 // -----------------------------------------------------------------------------
693 void wxGenericTreeCtrl::Init()
695 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
703 m_hilightBrush
= new wxBrush
705 wxSystemSettings::GetColour
707 wxSYS_COLOUR_HIGHLIGHT
712 m_hilightUnfocusedBrush
= new wxBrush
714 wxSystemSettings::GetColour
716 wxSYS_COLOUR_BTNSHADOW
721 m_imageListNormal
= m_imageListButtons
=
722 m_imageListState
= (wxImageList
*) NULL
;
723 m_ownsImageListNormal
= m_ownsImageListButtons
=
724 m_ownsImageListState
= FALSE
;
727 m_isDragging
= FALSE
;
728 m_dropTarget
= m_oldSelection
= NULL
;
732 m_renameTimer
= NULL
;
735 m_lastOnSame
= FALSE
;
737 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
738 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
739 m_normalFont
.GetFamily(),
740 m_normalFont
.GetStyle(),
742 m_normalFont
.GetUnderlined(),
743 m_normalFont
.GetFaceName(),
744 m_normalFont
.GetEncoding());
747 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
752 const wxValidator
& wxVALIDATOR_PARAM(validator
),
753 const wxString
& name
)
757 wxGetOsVersion( &major
, &minor
);
759 style
&= ~wxTR_LINES_AT_ROOT
;
760 style
|= wxTR_NO_LINES
;
762 style
|= wxTR_ROW_LINES
;
765 wxScrolledWindow::Create( parent
, id
, pos
, size
,
766 style
|wxHSCROLL
|wxVSCROLL
, name
);
768 // If the tree display has no buttons, but does have
769 // connecting lines, we can use a narrower layout.
770 // It may not be a good idea to force this...
771 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
778 SetValidator( validator
);
781 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
782 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
784 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
785 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
790 wxGenericTreeCtrl::~wxGenericTreeCtrl()
792 delete m_hilightBrush
;
793 delete m_hilightUnfocusedBrush
;
797 delete m_renameTimer
;
800 if (m_ownsImageListNormal
)
801 delete m_imageListNormal
;
802 if (m_ownsImageListState
)
803 delete m_imageListState
;
804 if (m_ownsImageListButtons
)
805 delete m_imageListButtons
;
808 // -----------------------------------------------------------------------------
810 // -----------------------------------------------------------------------------
812 size_t wxGenericTreeCtrl::GetCount() const
814 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
817 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
819 m_indent
= (unsigned short) indent
;
823 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
825 m_spacing
= (unsigned short) spacing
;
829 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
831 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
833 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
836 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
838 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
840 // if we will hide the root, make sure children are visible
841 m_anchor
->SetHasPlus();
843 CalculatePositions();
846 // right now, just sets the styles. Eventually, we may
847 // want to update the inherited styles, but right now
848 // none of the parents has updatable styles
849 m_windowStyle
= styles
;
853 // -----------------------------------------------------------------------------
854 // functions to work with tree items
855 // -----------------------------------------------------------------------------
857 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
859 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
861 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
864 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
865 wxTreeItemIcon which
) const
867 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
869 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
872 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
874 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
876 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
879 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
881 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
883 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
884 return pItem
->Attr().GetTextColour();
887 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
889 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
891 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
892 return pItem
->Attr().GetBackgroundColour();
895 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
897 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
899 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
900 return pItem
->Attr().GetFont();
903 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
905 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
908 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
909 pItem
->SetText(text
);
910 CalculateSize(pItem
, dc
);
914 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
916 wxTreeItemIcon which
)
918 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
920 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
921 pItem
->SetImage(image
, which
);
924 CalculateSize(pItem
, dc
);
928 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
930 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
932 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
935 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
937 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
939 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
940 pItem
->SetHasPlus(has
);
944 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
946 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
948 // avoid redrawing the tree if no real change
949 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
950 if ( pItem
->IsBold() != bold
)
952 pItem
->SetBold(bold
);
957 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
960 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
962 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
963 pItem
->Attr().SetTextColour(col
);
967 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
970 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
972 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
973 pItem
->Attr().SetBackgroundColour(col
);
977 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
979 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
981 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
982 pItem
->Attr().SetFont(font
);
986 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
988 wxScrolledWindow::SetFont(font
);
990 m_normalFont
= font
;
991 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
992 m_normalFont
.GetFamily(),
993 m_normalFont
.GetStyle(),
995 m_normalFont
.GetUnderlined(),
996 m_normalFont
.GetFaceName(),
997 m_normalFont
.GetEncoding());
1003 // -----------------------------------------------------------------------------
1004 // item status inquiries
1005 // -----------------------------------------------------------------------------
1007 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1009 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1011 // An item is only visible if it's not a descendant of a collapsed item
1012 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1013 wxGenericTreeItem
* parent
= pItem
->GetParent();
1016 if (!parent
->IsExpanded())
1018 parent
= parent
->GetParent();
1022 GetViewStart(& startX
, & startY
);
1024 wxSize clientSize
= GetClientSize();
1027 if (!GetBoundingRect(item
, rect
))
1029 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1031 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1033 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1039 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1041 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1043 // consider that the item does have children if it has the "+" button: it
1044 // might not have them (if it had never been expanded yet) but then it
1045 // could have them as well and it's better to err on this side rather than
1046 // disabling some operations which are restricted to the items with
1047 // children for an item which does have them
1048 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1051 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1053 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1055 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1058 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1060 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1062 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1065 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1067 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1069 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1072 // -----------------------------------------------------------------------------
1074 // -----------------------------------------------------------------------------
1076 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1078 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1080 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1083 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1084 wxTreeItemIdValue
& cookie
) const
1086 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1089 return GetNextChild(item
, cookie
);
1092 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1093 wxTreeItemIdValue
& cookie
) const
1095 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1097 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1099 // it's ok to cast cookie to size_t, we never have indices big enough to
1100 // overflow "void *"
1101 size_t *pIndex
= (size_t *)&cookie
;
1102 if ( *pIndex
< children
.Count() )
1104 return children
.Item((*pIndex
)++);
1108 // there are no more of them
1109 return wxTreeItemId();
1113 #if WXWIN_COMPATIBILITY_2_4
1115 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1118 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1121 return GetNextChild(item
, cookie
);
1124 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1127 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1129 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1130 if ( (size_t)cookie
< children
.Count() )
1132 return children
.Item((size_t)cookie
++);
1136 // there are no more of them
1137 return wxTreeItemId();
1141 #endif // WXWIN_COMPATIBILITY_2_4
1143 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1145 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1147 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1148 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1151 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1153 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1155 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1156 wxGenericTreeItem
*parent
= i
->GetParent();
1157 if ( parent
== NULL
)
1159 // root item doesn't have any siblings
1160 return wxTreeItemId();
1163 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1164 int index
= siblings
.Index(i
);
1165 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1167 size_t n
= (size_t)(index
+ 1);
1168 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1171 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1173 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1175 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1176 wxGenericTreeItem
*parent
= i
->GetParent();
1177 if ( parent
== NULL
)
1179 // root item doesn't have any siblings
1180 return wxTreeItemId();
1183 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1184 int index
= siblings
.Index(i
);
1185 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1187 return index
== 0 ? wxTreeItemId()
1188 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1191 // Only for internal use right now, but should probably be public
1192 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1194 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1196 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1198 // First see if there are any children.
1199 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1200 if (children
.GetCount() > 0)
1202 return children
.Item(0);
1206 // Try a sibling of this or ancestor instead
1207 wxTreeItemId p
= item
;
1208 wxTreeItemId toFind
;
1211 toFind
= GetNextSibling(p
);
1212 p
= GetItemParent(p
);
1213 } while (p
.IsOk() && !toFind
.IsOk());
1218 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1220 wxTreeItemId id
= GetRootItem();
1229 } while (id
.IsOk());
1231 return wxTreeItemId();
1234 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1236 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1238 wxTreeItemId id
= item
;
1241 while (id
= GetNext(id
), id
.IsOk())
1247 return wxTreeItemId();
1250 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1252 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1254 wxFAIL_MSG(wxT("not implemented"));
1256 return wxTreeItemId();
1259 // called by wxTextTreeCtrl when it marks itself for deletion
1260 void wxGenericTreeCtrl::ResetTextControl()
1265 // find the first item starting with the given prefix after the given item
1266 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1267 const wxString
& prefixOrig
) const
1269 // match is case insensitive as this is more convenient to the user: having
1270 // to press Shift-letter to go to the item starting with a capital letter
1271 // would be too bothersome
1272 wxString prefix
= prefixOrig
.Lower();
1274 // determine the starting point: we shouldn't take the current item (this
1275 // allows to switch between two items starting with the same letter just by
1276 // pressing it) but we shouldn't jump to the next one if the user is
1277 // continuing to type as otherwise he might easily skip the item he wanted
1278 wxTreeItemId id
= idParent
;
1279 if ( prefix
.length() == 1 )
1284 // look for the item starting with the given prefix after it
1285 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1290 // if we haven't found anything...
1293 // ... wrap to the beginning
1295 if ( HasFlag(wxTR_HIDE_ROOT
) )
1297 // can't select virtual root
1301 // and try all the items (stop when we get to the one we started from)
1302 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1311 // -----------------------------------------------------------------------------
1313 // -----------------------------------------------------------------------------
1315 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1317 const wxString
& text
,
1318 int image
, int selImage
,
1319 wxTreeItemData
*data
)
1321 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1324 // should we give a warning here?
1325 return AddRoot(text
, image
, selImage
, data
);
1328 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1330 wxGenericTreeItem
*item
=
1331 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1335 data
->m_pItem
= item
;
1338 parent
->Insert( item
, previous
);
1343 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1344 int image
, int selImage
,
1345 wxTreeItemData
*data
)
1347 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1349 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1351 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1352 image
, selImage
, data
);
1355 data
->m_pItem
= m_anchor
;
1358 if (HasFlag(wxTR_HIDE_ROOT
))
1360 // if root is hidden, make sure we can navigate
1362 m_anchor
->SetHasPlus();
1364 CalculatePositions();
1367 if (!HasFlag(wxTR_MULTIPLE
))
1369 m_current
= m_key_current
= m_anchor
;
1370 m_current
->SetHilight( TRUE
);
1376 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1377 const wxString
& text
,
1378 int image
, int selImage
,
1379 wxTreeItemData
*data
)
1381 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1384 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1385 const wxTreeItemId
& idPrevious
,
1386 const wxString
& text
,
1387 int image
, int selImage
,
1388 wxTreeItemData
*data
)
1390 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1393 // should we give a warning here?
1394 return AddRoot(text
, image
, selImage
, data
);
1398 if (idPrevious
.IsOk())
1400 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1401 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1402 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1405 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1408 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1410 const wxString
& text
,
1411 int image
, int selImage
,
1412 wxTreeItemData
*data
)
1414 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1417 // should we give a warning here?
1418 return AddRoot(text
, image
, selImage
, data
);
1421 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1424 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1425 const wxString
& text
,
1426 int image
, int selImage
,
1427 wxTreeItemData
*data
)
1429 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1432 // should we give a warning here?
1433 return AddRoot(text
, image
, selImage
, data
);
1436 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1437 image
, selImage
, data
);
1440 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1442 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1443 event
.m_item
= item
;
1444 event
.SetEventObject( this );
1445 ProcessEvent( event
);
1448 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1450 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1452 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1453 item
->DeleteChildren(this);
1456 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1458 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1460 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1462 wxGenericTreeItem
*parent
= item
->GetParent();
1464 // don't keep stale pointers around!
1465 if ( IsDescendantOf(item
, m_key_current
) )
1467 // Don't silently change the selection:
1468 // do it properly in idle time, so event
1469 // handlers get called.
1471 // m_key_current = parent;
1472 m_key_current
= NULL
;
1475 // m_select_me records whether we need to select
1476 // a different item, in idle time.
1477 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1479 m_select_me
= parent
;
1482 if ( IsDescendantOf(item
, m_current
) )
1484 // Don't silently change the selection:
1485 // do it properly in idle time, so event
1486 // handlers get called.
1488 // m_current = parent;
1490 m_select_me
= parent
;
1493 // remove the item from the tree
1496 parent
->GetChildren().Remove( item
); // remove by value
1498 else // deleting the root
1500 // nothing will be left in the tree
1504 // and delete all of its children and the item itself now
1505 item
->DeleteChildren(this);
1506 SendDeleteEvent(item
);
1510 void wxGenericTreeCtrl::DeleteAllItems()
1518 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1520 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1522 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1523 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1524 _T("can't expand hidden root") );
1526 if ( !item
->HasPlus() )
1529 if ( item
->IsExpanded() )
1532 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1533 event
.m_item
= item
;
1534 event
.SetEventObject( this );
1536 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1538 // cancelled by program
1543 CalculatePositions();
1545 RefreshSubtree(item
);
1547 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1548 ProcessEvent( event
);
1551 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1553 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1556 if ( !IsExpanded(item
) )
1560 wxTreeItemIdValue cookie
;
1561 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1562 while ( child
.IsOk() )
1566 child
= GetNextChild(item
, cookie
);
1570 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1572 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1573 _T("can't collapse hidden root") );
1575 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1577 if ( !item
->IsExpanded() )
1580 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1581 event
.m_item
= item
;
1582 event
.SetEventObject( this );
1583 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1585 // cancelled by program
1591 #if 0 // TODO why should items be collapsed recursively?
1592 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1593 size_t count
= children
.Count();
1594 for ( size_t n
= 0; n
< count
; n
++ )
1596 Collapse(children
[n
]);
1600 CalculatePositions();
1602 RefreshSubtree(item
);
1604 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1605 ProcessEvent( event
);
1608 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1611 DeleteChildren(item
);
1614 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1616 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1618 if (item
->IsExpanded())
1624 void wxGenericTreeCtrl::Unselect()
1628 m_current
->SetHilight( FALSE
);
1629 RefreshLine( m_current
);
1636 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1638 if (item
->IsSelected())
1640 item
->SetHilight(FALSE
);
1644 if (item
->HasChildren())
1646 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1647 size_t count
= children
.Count();
1648 for ( size_t n
= 0; n
< count
; ++n
)
1650 UnselectAllChildren(children
[n
]);
1655 void wxGenericTreeCtrl::UnselectAll()
1657 wxTreeItemId rootItem
= GetRootItem();
1659 // the tree might not have the root item at all
1662 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1666 // Recursive function !
1667 // To stop we must have crt_item<last_item
1669 // Tag all next children, when no more children,
1670 // Move to parent (not to tag)
1671 // Keep going... if we found last_item, we stop.
1672 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1674 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1676 if (parent
== NULL
) // This is root item
1677 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1679 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1680 int index
= children
.Index(crt_item
);
1681 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1683 size_t count
= children
.Count();
1684 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1686 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1689 return TagNextChildren(parent
, last_item
, select
);
1692 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1694 crt_item
->SetHilight(select
);
1695 RefreshLine(crt_item
);
1697 if (crt_item
==last_item
)
1700 if (crt_item
->HasChildren())
1702 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1703 size_t count
= children
.Count();
1704 for ( size_t n
= 0; n
< count
; ++n
)
1706 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1714 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1718 // item2 is not necessary after item1
1719 // choice first' and 'last' between item1 and item2
1720 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1721 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1723 bool select
= m_current
->IsSelected();
1725 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1728 TagNextChildren(first
,last
,select
);
1731 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1732 bool unselect_others
,
1733 bool extended_select
)
1735 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1739 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1740 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1742 //wxCHECK_RET( ( (!unselect_others) && is_single),
1743 // wxT("this is a single selection tree") );
1745 // to keep going anyhow !!!
1748 if (item
->IsSelected())
1749 return; // nothing to do
1750 unselect_others
= TRUE
;
1751 extended_select
= FALSE
;
1753 else if ( unselect_others
&& item
->IsSelected() )
1755 // selection change if there is more than one item currently selected
1756 wxArrayTreeItemIds selected_items
;
1757 if ( GetSelections(selected_items
) == 1 )
1761 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1762 event
.m_item
= item
;
1763 event
.m_itemOld
= m_current
;
1764 event
.SetEventObject( this );
1765 // TODO : Here we don't send any selection mode yet !
1767 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1770 wxTreeItemId parent
= GetItemParent( itemId
);
1771 while (parent
.IsOk())
1773 if (!IsExpanded(parent
))
1776 parent
= GetItemParent( parent
);
1779 EnsureVisible( itemId
);
1782 if (unselect_others
)
1784 if (is_single
) Unselect(); // to speed up thing
1789 if (extended_select
)
1793 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1796 // don't change the mark (m_current)
1797 SelectItemRange(m_current
, item
);
1801 bool select
=TRUE
; // the default
1803 // Check if we need to toggle hilight (ctrl mode)
1804 if (!unselect_others
)
1805 select
=!item
->IsSelected();
1807 m_current
= m_key_current
= item
;
1808 m_current
->SetHilight(select
);
1809 RefreshLine( m_current
);
1812 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1813 GetEventHandler()->ProcessEvent( event
);
1816 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1820 DoSelectItem(itemId
);
1824 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1825 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1827 item
->SetHilight(FALSE
);
1832 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1833 wxArrayTreeItemIds
&array
) const
1835 if ( item
->IsSelected() )
1836 array
.Add(wxTreeItemId(item
));
1838 if ( item
->HasChildren() )
1840 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1841 size_t count
= children
.GetCount();
1842 for ( size_t n
= 0; n
< count
; ++n
)
1843 FillArray(children
[n
], array
);
1847 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1850 wxTreeItemId idRoot
= GetRootItem();
1851 if ( idRoot
.IsOk() )
1853 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1855 //else: the tree is empty, so no selections
1857 return array
.Count();
1860 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1862 if (!item
.IsOk()) return;
1864 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1866 // first expand all parent branches
1867 wxGenericTreeItem
*parent
= gitem
->GetParent();
1869 if ( HasFlag(wxTR_HIDE_ROOT
) )
1871 while ( parent
&& parent
!= m_anchor
)
1874 parent
= parent
->GetParent();
1882 parent
= parent
->GetParent();
1886 //if (parent) CalculatePositions();
1891 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1893 if (!item
.IsOk()) return;
1895 // We have to call this here because the label in
1896 // question might just have been added and no screen
1897 // update taken place.
1898 if (m_dirty
) wxYieldIfNeeded();
1900 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1902 // now scroll to the item
1903 int item_y
= gitem
->GetY();
1907 GetViewStart( &start_x
, &start_y
);
1908 start_y
*= PIXELS_PER_UNIT
;
1912 GetClientSize( &client_w
, &client_h
);
1914 if (item_y
< start_y
+3)
1919 m_anchor
->GetSize( x
, y
, this );
1920 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1921 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1922 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1923 // Item should appear at top
1924 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1926 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1931 m_anchor
->GetSize( x
, y
, this );
1932 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1933 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1934 item_y
+= PIXELS_PER_UNIT
+2;
1935 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1936 // Item should appear at bottom
1937 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
);
1941 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1942 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1944 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1945 wxGenericTreeItem
**item2
)
1947 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1949 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1952 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1953 const wxTreeItemId
& item2
)
1955 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1958 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1960 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1962 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1964 wxCHECK_RET( !s_treeBeingSorted
,
1965 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1967 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1968 if ( children
.Count() > 1 )
1972 s_treeBeingSorted
= this;
1973 children
.Sort(tree_ctrl_compare_func
);
1974 s_treeBeingSorted
= NULL
;
1976 //else: don't make the tree dirty as nothing changed
1979 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1981 return m_imageListNormal
;
1984 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1986 return m_imageListButtons
;
1989 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1991 return m_imageListState
;
1994 void wxGenericTreeCtrl::CalculateLineHeight()
1996 wxClientDC
dc(this);
1997 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1999 if ( m_imageListNormal
)
2001 // Calculate a m_lineHeight value from the normal Image sizes.
2002 // May be toggle off. Then wxGenericTreeCtrl will spread when
2003 // necessary (which might look ugly).
2004 int n
= m_imageListNormal
->GetImageCount();
2005 for (int i
= 0; i
< n
; i
++)
2007 int width
= 0, height
= 0;
2008 m_imageListNormal
->GetSize(i
, width
, height
);
2009 if (height
> m_lineHeight
) m_lineHeight
= height
;
2013 if (m_imageListButtons
)
2015 // Calculate a m_lineHeight value from the Button image sizes.
2016 // May be toggle off. Then wxGenericTreeCtrl will spread when
2017 // necessary (which might look ugly).
2018 int n
= m_imageListButtons
->GetImageCount();
2019 for (int i
= 0; i
< n
; i
++)
2021 int width
= 0, height
= 0;
2022 m_imageListButtons
->GetSize(i
, width
, height
);
2023 if (height
> m_lineHeight
) m_lineHeight
= height
;
2027 if (m_lineHeight
< 30)
2028 m_lineHeight
+= 2; // at least 2 pixels
2030 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2033 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2035 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2036 m_imageListNormal
= imageList
;
2037 m_ownsImageListNormal
= FALSE
;
2039 // Don't do any drawing if we're setting the list to NULL,
2040 // since we may be in the process of deleting the tree control.
2042 CalculateLineHeight();
2045 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2047 if (m_ownsImageListState
) delete m_imageListState
;
2048 m_imageListState
= imageList
;
2049 m_ownsImageListState
= FALSE
;
2052 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2054 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2055 m_imageListButtons
= imageList
;
2056 m_ownsImageListButtons
= FALSE
;
2058 CalculateLineHeight();
2061 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2063 SetImageList(imageList
);
2064 m_ownsImageListNormal
= TRUE
;
2067 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2069 SetStateImageList(imageList
);
2070 m_ownsImageListState
= TRUE
;
2073 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2075 SetButtonsImageList(imageList
);
2076 m_ownsImageListButtons
= TRUE
;
2079 // -----------------------------------------------------------------------------
2081 // -----------------------------------------------------------------------------
2083 void wxGenericTreeCtrl::AdjustMyScrollbars()
2088 m_anchor
->GetSize( x
, y
, this );
2089 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2090 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2091 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2092 int y_pos
= GetScrollPos( wxVERTICAL
);
2093 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2097 SetScrollbars( 0, 0, 0, 0 );
2101 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2103 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2104 return item
->GetHeight();
2106 return m_lineHeight
;
2109 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2111 // TODO implement "state" icon on items
2113 wxTreeItemAttr
*attr
= item
->GetAttributes();
2114 if ( attr
&& attr
->HasFont() )
2115 dc
.SetFont(attr
->GetFont());
2116 else if (item
->IsBold())
2117 dc
.SetFont(m_boldFont
);
2119 long text_w
= 0, text_h
= 0;
2120 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2122 int image_h
= 0, image_w
= 0;
2123 int image
= item
->GetCurrentImage();
2124 if ( image
!= NO_IMAGE
)
2126 if ( m_imageListNormal
)
2128 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2137 int total_h
= GetLineHeight(item
);
2139 if ( item
->IsSelected() )
2141 // under mac selections are only a rectangle in case they don't have the focus
2145 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2146 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2150 dc
.SetBrush( *m_hilightBrush
) ;
2153 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2159 if ( attr
&& attr
->HasBackgroundColour() )
2160 colBg
= attr
->GetBackgroundColour();
2162 colBg
= m_backgroundColour
;
2163 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2166 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2168 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2172 DoGetPosition(&x
, &y
);
2174 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2178 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2180 // If it's selected, and there's an image, then we should
2181 // take care to leave the area under the image painted in the
2182 // background colour.
2183 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2184 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2188 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2189 item
->GetWidth()+2, total_h
-offset
);
2193 if ( image
!= NO_IMAGE
)
2195 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2196 m_imageListNormal
->Draw( image
, dc
,
2198 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2199 wxIMAGELIST_DRAW_TRANSPARENT
);
2200 dc
.DestroyClippingRegion();
2203 dc
.SetBackgroundMode(wxTRANSPARENT
);
2204 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2205 dc
.DrawText( item
->GetText(),
2206 (wxCoord
)(image_w
+ item
->GetX()),
2207 (wxCoord
)(item
->GetY() + extraH
));
2209 // restore normal font
2210 dc
.SetFont( m_normalFont
);
2213 // Now y stands for the top of the item, whereas it used to stand for middle !
2214 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2216 int x
= level
*m_indent
;
2217 if (!HasFlag(wxTR_HIDE_ROOT
))
2221 else if (level
== 0)
2223 // always expand hidden root
2225 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2226 int count
= children
.Count();
2232 PaintLevel(children
[n
], dc
, 1, y
);
2233 } while (++n
< count
);
2235 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2237 // draw line down to last child
2238 origY
+= GetLineHeight(children
[0])>>1;
2239 oldY
+= GetLineHeight(children
[n
-1])>>1;
2240 dc
.DrawLine(3, origY
, 3, oldY
);
2246 item
->SetX(x
+m_spacing
);
2249 int h
= GetLineHeight(item
);
2251 int y_mid
= y_top
+ (h
>>1);
2254 int exposed_x
= dc
.LogicalToDeviceX(0);
2255 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2257 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2261 // don't draw rect outline if we already have the
2262 // background color under Mac
2263 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2264 #endif // !__WXMAC__
2268 if ( item
->IsSelected() )
2270 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2274 wxTreeItemAttr
*attr
= item
->GetAttributes();
2275 if (attr
&& attr
->HasTextColour())
2276 colText
= attr
->GetTextColour();
2278 colText
= GetForegroundColour();
2282 dc
.SetTextForeground(colText
);
2286 PaintItem(item
, dc
);
2288 if (HasFlag(wxTR_ROW_LINES
))
2290 // if the background colour is white, choose a
2291 // contrasting color for the lines
2292 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2293 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2294 dc
.DrawLine(0, y_top
, 10000, y_top
);
2295 dc
.DrawLine(0, y
, 10000, y
);
2298 // restore DC objects
2299 dc
.SetBrush(*wxWHITE_BRUSH
);
2300 dc
.SetPen(m_dottedPen
);
2301 dc
.SetTextForeground(*wxBLACK
);
2303 if ( !HasFlag(wxTR_NO_LINES
) )
2305 // draw the horizontal line here
2307 if (x
> (signed)m_indent
)
2308 x_start
-= m_indent
;
2309 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2311 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2314 // should the item show a button?
2315 if ( item
->HasPlus() && HasButtons() )
2317 if ( m_imageListButtons
)
2319 // draw the image button here
2322 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2323 : wxTreeItemIcon_Normal
;
2324 if ( item
->IsSelected() )
2325 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2327 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2328 int xx
= x
- image_w
/2;
2329 int yy
= y_mid
- image_h
/2;
2331 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2332 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2333 wxIMAGELIST_DRAW_TRANSPARENT
);
2335 else // no custom buttons
2337 static const int wImage
= 9;
2338 static const int hImage
= 9;
2341 if (item
->IsExpanded())
2342 flag
|= wxCONTROL_EXPANDED
;
2343 if (item
== m_underMouse
)
2344 flag
|= wxCONTROL_CURRENT
;
2346 wxRendererNative::Get().DrawTreeItemButton
2350 wxRect(x
- wImage
/2,
2359 if (item
->IsExpanded())
2361 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2362 int count
= children
.Count();
2369 PaintLevel(children
[n
], dc
, level
, y
);
2370 } while (++n
< count
);
2372 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2374 // draw line down to last child
2375 oldY
+= GetLineHeight(children
[n
-1])>>1;
2376 if (HasButtons()) y_mid
+= 5;
2378 // Only draw the portion of the line that is visible, in case it is huge
2379 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2380 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2381 yOrigin
= abs(yOrigin
);
2382 GetClientSize(&width
, &height
);
2384 // Move end points to the begining/end of the view?
2385 if (y_mid
< yOrigin
)
2387 if (oldY
> yOrigin
+ height
)
2388 oldY
= yOrigin
+ height
;
2390 // after the adjustments if y_mid is larger than oldY then the line
2391 // isn't visible at all so don't draw anything
2393 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2399 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2403 if ( item
->HasPlus() )
2405 // it's a folder, indicate it by a border
2410 // draw a line under the drop target because the item will be
2412 DrawLine(item
, TRUE
/* below */);
2415 SetCursor(wxCURSOR_BULLSEYE
);
2420 SetCursor(wxCURSOR_NO_ENTRY
);
2424 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2426 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2428 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2430 wxClientDC
dc(this);
2432 dc
.SetLogicalFunction(wxINVERT
);
2433 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2435 int w
= i
->GetWidth() + 2;
2436 int h
= GetLineHeight(i
) + 2;
2438 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2441 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2443 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2445 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2447 wxClientDC
dc(this);
2449 dc
.SetLogicalFunction(wxINVERT
);
2455 y
+= GetLineHeight(i
) - 1;
2458 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2461 // -----------------------------------------------------------------------------
2462 // wxWindows callbacks
2463 // -----------------------------------------------------------------------------
2465 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2473 dc
.SetFont( m_normalFont
);
2474 dc
.SetPen( m_dottedPen
);
2476 // this is now done dynamically
2477 //if(GetImageList() == NULL)
2478 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2481 PaintLevel( m_anchor
, dc
, 0, y
);
2484 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2493 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2502 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2504 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2505 te
.m_evtKey
= event
;
2506 te
.SetEventObject( this );
2507 if ( GetEventHandler()->ProcessEvent( te
) )
2509 // intercepted by the user code
2513 if ( (m_current
== 0) || (m_key_current
== 0) )
2519 // how should the selection work for this event?
2520 bool is_multiple
, extended_select
, unselect_others
;
2521 EventFlagsToSelType(GetWindowStyleFlag(),
2523 event
.ControlDown(),
2524 is_multiple
, extended_select
, unselect_others
);
2528 // * : Expand all/Collapse all
2529 // ' ' | return : activate
2530 // up : go up (not last children!)
2532 // left : go to parent
2533 // right : open if parent and go next
2534 // home : go to root
2535 // end : go to last item without opening parents
2536 // alnum : start or continue searching for the item with this prefix
2537 int keyCode
= event
.GetKeyCode();
2542 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2550 if ( !IsExpanded(m_current
) )
2553 ExpandAll(m_current
);
2556 //else: fall through to Collapse() it
2560 if (IsExpanded(m_current
))
2562 Collapse(m_current
);
2568 if ( !event
.HasModifiers() )
2570 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2571 event
.m_item
= m_current
;
2572 event
.SetEventObject( this );
2573 GetEventHandler()->ProcessEvent( event
);
2576 // in any case, also generate the normal key event for this key,
2577 // even if we generated the ACTIVATED event above: this is what
2578 // wxMSW does and it makes sense because you might not want to
2579 // process ACTIVATED event at all and handle Space and Return
2580 // directly (and differently) which would be impossible otherwise
2584 // up goes to the previous sibling or to the last
2585 // of its children if it's expanded
2588 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2591 prev
= GetItemParent( m_key_current
);
2592 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2594 break; // don't go to root if it is hidden
2598 wxTreeItemIdValue cookie
;
2599 wxTreeItemId current
= m_key_current
;
2600 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2601 if (current
== GetFirstChild( prev
, cookie
))
2603 // otherwise we return to where we came from
2604 DoSelectItem( prev
, unselect_others
, extended_select
);
2605 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2612 while ( IsExpanded(prev
) && HasChildren(prev
) )
2614 wxTreeItemId child
= GetLastChild(prev
);
2621 DoSelectItem( prev
, unselect_others
, extended_select
);
2622 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2627 // left arrow goes to the parent
2630 wxTreeItemId prev
= GetItemParent( m_current
);
2631 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2633 // don't go to root if it is hidden
2634 prev
= GetPrevSibling( m_current
);
2638 DoSelectItem( prev
, unselect_others
, extended_select
);
2644 // this works the same as the down arrow except that we
2645 // also expand the item if it wasn't expanded yet
2651 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2653 wxTreeItemIdValue cookie
;
2654 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2655 DoSelectItem( child
, unselect_others
, extended_select
);
2656 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2660 wxTreeItemId next
= GetNextSibling( m_key_current
);
2663 wxTreeItemId current
= m_key_current
;
2664 while (current
.IsOk() && !next
)
2666 current
= GetItemParent( current
);
2667 if (current
) next
= GetNextSibling( current
);
2672 DoSelectItem( next
, unselect_others
, extended_select
);
2673 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2679 // <End> selects the last visible tree item
2682 wxTreeItemId last
= GetRootItem();
2684 while ( last
.IsOk() && IsExpanded(last
) )
2686 wxTreeItemId lastChild
= GetLastChild(last
);
2688 // it may happen if the item was expanded but then all of
2689 // its children have been deleted - so IsExpanded() returned
2690 // TRUE, but GetLastChild() returned invalid item
2699 DoSelectItem( last
, unselect_others
, extended_select
);
2704 // <Home> selects the root item
2707 wxTreeItemId prev
= GetRootItem();
2711 if ( HasFlag(wxTR_HIDE_ROOT
) )
2713 wxTreeItemIdValue cookie
;
2714 prev
= GetFirstChild(prev
, cookie
);
2719 DoSelectItem( prev
, unselect_others
, extended_select
);
2724 // do not use wxIsalnum() here
2725 if ( !event
.HasModifiers() &&
2726 ((keyCode
>= '0' && keyCode
<= '9') ||
2727 (keyCode
>= 'a' && keyCode
<= 'z') ||
2728 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2730 // find the next item starting with the given prefix
2731 char ch
= (char)keyCode
;
2733 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2744 // also start the timer to reset the current prefix if the user
2745 // doesn't press any more alnum keys soon -- we wouldn't want
2746 // to use this prefix for a new item search
2749 m_findTimer
= new wxTreeFindTimer(this);
2752 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2761 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2763 // JACS: removed wxYieldIfNeeded() because it can cause the window
2764 // to be deleted from under us if a close window event is pending
2769 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2770 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2771 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2772 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2773 if (flags
) return wxTreeItemId();
2775 if (m_anchor
== NULL
)
2777 flags
= wxTREE_HITTEST_NOWHERE
;
2778 return wxTreeItemId();
2781 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2785 flags
= wxTREE_HITTEST_NOWHERE
;
2786 return wxTreeItemId();
2791 // get the bounding rectangle of the item (or of its label only)
2792 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2794 bool WXUNUSED(textOnly
)) const
2796 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2798 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2801 GetViewStart(& startX
, & startY
);
2803 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2804 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2805 rect
.width
= i
->GetWidth();
2806 //rect.height = i->GetHeight();
2807 rect
.height
= GetLineHeight(i
);
2812 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2814 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2816 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2818 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2819 te
.m_item
= itemEdit
;
2820 te
.SetEventObject( this );
2821 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2827 // We have to call this here because the label in
2828 // question might just have been added and no screen
2829 // update taken place.
2833 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2835 m_textCtrl
->SetFocus();
2838 // returns a pointer to the text edit control if the item is being
2839 // edited, NULL otherwise (it's assumed that no more than one item may
2840 // be edited simultaneously)
2841 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2846 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2847 const wxString
& value
)
2849 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2851 le
.SetEventObject( this );
2853 le
.m_editCancelled
= FALSE
;
2855 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2858 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2860 // let owner know that the edit was cancelled
2861 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2863 le
.SetEventObject( this );
2864 le
.m_label
= wxEmptyString
;
2865 le
.m_editCancelled
= TRUE
;
2867 GetEventHandler()->ProcessEvent( le
);
2873 void wxGenericTreeCtrl::OnRenameTimer()
2878 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2880 if ( !m_anchor
) return;
2882 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2884 // Is the mouse over a tree item button?
2886 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
2887 wxGenericTreeItem
*underMouse
= thisItem
;
2888 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
2891 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
2892 (!event
.LeftIsDown()) &&
2894 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2902 if (underMouse
!= m_underMouse
)
2906 // unhighlight old item
2907 wxGenericTreeItem
*tmp
= m_underMouse
;
2908 m_underMouse
= NULL
;
2912 m_underMouse
= underMouse
;
2914 RefreshLine( m_underMouse
);
2918 // Determines what item we are hovering over and need a tooltip for
2919 wxTreeItemId hoverItem
= thisItem
;
2921 // We do not want a tooltip if we are dragging, or if the rename timer is running
2922 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2924 // Ask the tree control what tooltip (if any) should be shown
2925 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
2926 hevent
.m_item
= hoverItem
;
2927 hevent
.SetEventObject(this);
2929 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
2931 SetToolTip(hevent
.m_label
);
2936 // we process left mouse up event (enables in-place edit), right down
2937 // (pass to the user code), left dbl click (activate item) and
2938 // dragging/moving events for items drag-and-drop
2939 if ( !(event
.LeftDown() ||
2941 event
.RightDown() ||
2942 event
.LeftDClick() ||
2944 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2953 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2955 if ( event
.Dragging() && !m_isDragging
)
2957 if (m_dragCount
== 0)
2962 if (m_dragCount
!= 3)
2964 // wait until user drags a bit further...
2968 wxEventType command
= event
.RightIsDown()
2969 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2970 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2972 wxTreeEvent
nevent( command
, GetId() );
2973 nevent
.m_item
= m_current
;
2974 nevent
.SetEventObject(this);
2976 // by default the dragging is not supported, the user code must
2977 // explicitly allow the event for it to take place
2980 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2982 // we're going to drag this item
2983 m_isDragging
= TRUE
;
2985 // remember the old cursor because we will change it while
2987 m_oldCursor
= m_cursor
;
2989 // in a single selection control, hide the selection temporarily
2990 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2992 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2994 if ( m_oldSelection
)
2996 m_oldSelection
->SetHilight(FALSE
);
2997 RefreshLine(m_oldSelection
);
3004 else if ( event
.Moving() )
3006 if ( item
!= m_dropTarget
)
3008 // unhighlight the previous drop target
3009 DrawDropEffect(m_dropTarget
);
3011 m_dropTarget
= item
;
3013 // highlight the current drop target if any
3014 DrawDropEffect(m_dropTarget
);
3019 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3021 // erase the highlighting
3022 DrawDropEffect(m_dropTarget
);
3024 if ( m_oldSelection
)
3026 m_oldSelection
->SetHilight(TRUE
);
3027 RefreshLine(m_oldSelection
);
3028 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3031 // generate the drag end event
3032 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3034 event
.m_item
= item
;
3035 event
.m_pointDrag
= pt
;
3036 event
.SetEventObject(this);
3038 (void)GetEventHandler()->ProcessEvent(event
);
3040 m_isDragging
= FALSE
;
3041 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3045 SetCursor(m_oldCursor
);
3051 // here we process only the messages which happen on tree items
3055 if (item
== NULL
) return; /* we hit the blank area */
3057 if ( event
.RightDown() )
3059 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3060 nevent
.m_item
= item
;
3061 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3062 nevent
.SetEventObject(this);
3063 GetEventHandler()->ProcessEvent(nevent
);
3065 else if ( event
.LeftUp() )
3067 // this facilitates multiple-item drag-and-drop
3069 if (item
&& HasFlag(wxTR_MULTIPLE
))
3071 wxArrayTreeItemIds selections
;
3072 size_t count
= GetSelections(selections
);
3075 !event
.ControlDown() &&
3078 DoSelectItem(item
, true, false);
3084 if ( (item
== m_current
) &&
3085 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3086 HasFlag(wxTR_EDIT_LABELS
) )
3088 if ( m_renameTimer
)
3090 if ( m_renameTimer
->IsRunning() )
3091 m_renameTimer
->Stop();
3095 m_renameTimer
= new wxTreeRenameTimer( this );
3098 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3101 m_lastOnSame
= FALSE
;
3104 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3106 if ( event
.LeftDown() )
3108 m_lastOnSame
= item
== m_current
;
3111 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3113 // only toggle the item for a single click, double click on
3114 // the button doesn't do anything (it toggles the item twice)
3115 if ( event
.LeftDown() )
3120 // don't select the item if the button was clicked
3125 // clear the previously selected items, if the
3126 // user clicked outside of the present selection.
3127 // otherwise, perform the deselection on mouse-up.
3128 // this allows multiple drag and drop to work.
3130 if (!IsSelected(item
))
3132 // how should the selection work for this event?
3133 bool is_multiple
, extended_select
, unselect_others
;
3134 EventFlagsToSelType(GetWindowStyleFlag(),
3136 event
.ControlDown(),
3137 is_multiple
, extended_select
, unselect_others
);
3139 DoSelectItem(item
, unselect_others
, extended_select
);
3143 // For some reason, Windows isn't recognizing a left double-click,
3144 // so we need to simulate it here. Allow 200 milliseconds for now.
3145 if ( event
.LeftDClick() )
3147 // double clicking should not start editing the item label
3148 if ( m_renameTimer
)
3149 m_renameTimer
->Stop();
3151 m_lastOnSame
= FALSE
;
3153 // send activate event first
3154 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3155 nevent
.m_item
= item
;
3156 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3157 nevent
.SetEventObject( this );
3158 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3160 // if the user code didn't process the activate event,
3161 // handle it ourselves by toggling the item when it is
3163 if ( item
->HasPlus() )
3173 void wxGenericTreeCtrl::OnInternalIdle()
3175 wxWindow::OnInternalIdle();
3177 // Check if we need to select the root item
3178 // because nothing else has been selected.
3179 // Delaying it means that we can invoke event handlers
3180 // as required, when a first item is selected.
3181 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3184 SelectItem(m_select_me
);
3185 else if (GetRootItem().IsOk())
3186 SelectItem(GetRootItem());
3189 /* after all changes have been done to the tree control,
3190 * we actually redraw the tree when everything is over */
3192 if (!m_dirty
) return;
3196 CalculatePositions();
3198 AdjustMyScrollbars();
3201 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3206 wxTreeItemAttr
*attr
= item
->GetAttributes();
3207 if ( attr
&& attr
->HasFont() )
3208 dc
.SetFont(attr
->GetFont());
3209 else if ( item
->IsBold() )
3210 dc
.SetFont(m_boldFont
);
3212 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3215 // restore normal font
3216 dc
.SetFont( m_normalFont
);
3220 int image
= item
->GetCurrentImage();
3221 if ( image
!= NO_IMAGE
)
3223 if ( m_imageListNormal
)
3225 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3230 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3233 total_h
+= 2; // at least 2 pixels
3235 total_h
+= total_h
/10; // otherwise 10% extra spacing
3237 item
->SetHeight(total_h
);
3238 if (total_h
>m_lineHeight
)
3239 m_lineHeight
=total_h
;
3241 item
->SetWidth(image_w
+text_w
+2);
3244 // -----------------------------------------------------------------------------
3245 // for developper : y is now the top of the level
3246 // not the middle of it !
3247 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3249 int x
= level
*m_indent
;
3250 if (!HasFlag(wxTR_HIDE_ROOT
))
3254 else if (level
== 0)
3256 // a hidden root is not evaluated, but its
3257 // children are always calculated
3261 CalculateSize( item
, dc
);
3264 item
->SetX( x
+m_spacing
);
3266 y
+= GetLineHeight(item
);
3268 if ( !item
->IsExpanded() )
3270 // we don't need to calculate collapsed branches
3275 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3276 size_t n
, count
= children
.Count();
3278 for (n
= 0; n
< count
; ++n
)
3279 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3282 void wxGenericTreeCtrl::CalculatePositions()
3284 if ( !m_anchor
) return;
3286 wxClientDC
dc(this);
3289 dc
.SetFont( m_normalFont
);
3291 dc
.SetPen( m_dottedPen
);
3292 //if(GetImageList() == NULL)
3293 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3296 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3299 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3301 if (m_dirty
) return;
3303 wxSize client
= GetClientSize();
3306 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3307 rect
.width
= client
.x
;
3308 rect
.height
= client
.y
;
3310 Refresh(TRUE
, &rect
);
3312 AdjustMyScrollbars();
3315 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3317 if (m_dirty
) return;
3320 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3321 rect
.width
= GetClientSize().x
;
3322 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3324 Refresh(TRUE
, &rect
);
3327 void wxGenericTreeCtrl::RefreshSelected()
3329 // TODO: this is awfully inefficient, we should keep the list of all
3330 // selected items internally, should be much faster
3332 RefreshSelectedUnder(m_anchor
);
3335 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3337 if ( item
->IsSelected() )
3340 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3341 size_t count
= children
.GetCount();
3342 for ( size_t n
= 0; n
< count
; n
++ )
3344 RefreshSelectedUnder(children
[n
]);
3348 // ----------------------------------------------------------------------------
3349 // changing colours: we need to refresh the tree control
3350 // ----------------------------------------------------------------------------
3352 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3354 if ( !wxWindow::SetBackgroundColour(colour
) )
3362 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3364 if ( !wxWindow::SetForegroundColour(colour
) )
3372 // Process the tooltip event, to speed up event processing.
3373 // Doesn't actually get a tooltip.
3374 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3379 #endif // wxUSE_TREECTRL