1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treectlg.cpp
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/dcclient.h"
35 #include "wx/generic/treectlg.h"
37 #include "wx/textctrl.h"
38 #include "wx/imaglist.h"
39 #include "wx/settings.h"
41 #include "wx/renderer.h"
44 #include "wx/mac/private.h"
47 // -----------------------------------------------------------------------------
49 // -----------------------------------------------------------------------------
51 class WXDLLEXPORT wxGenericTreeItem
;
53 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 static const int NO_IMAGE
= -1;
61 static const int PIXELS_PER_UNIT
= 10;
63 // the margin between the item image and the item text
64 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
66 // -----------------------------------------------------------------------------
68 // -----------------------------------------------------------------------------
70 // timer used for enabling in-place edit
71 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
74 // start editing the current item after half a second (if the mouse hasn't
75 // been clicked/moved)
78 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
80 virtual void Notify();
83 wxGenericTreeCtrl
*m_owner
;
85 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
88 // control used for in-place edit
89 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
92 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
94 void EndEdit(bool discardChanges
= false)
102 m_aboutToFinish
= true;
104 // Notify the owner about the changes
107 // Even if vetoed, close the control (consistent with MSW)
115 m_owner
->OnRenameCancelled(m_itemEdited
);
117 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
120 void OnChar( wxKeyEvent
&event
);
121 void OnKeyUp( wxKeyEvent
&event
);
122 void OnKillFocus( wxFocusEvent
&event
);
124 bool AcceptChanges();
128 wxGenericTreeCtrl
*m_owner
;
129 wxGenericTreeItem
*m_itemEdited
;
130 wxString m_startValue
;
132 bool m_aboutToFinish
;
134 DECLARE_EVENT_TABLE()
135 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
138 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
139 // for a sufficiently long time
140 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
143 // reset the current prefix after half a second of inactivity
144 enum { DELAY
= 500 };
146 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
148 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
151 wxGenericTreeCtrl
*m_owner
;
153 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
157 class WXDLLEXPORT wxGenericTreeItem
161 wxGenericTreeItem() { m_data
= NULL
; }
162 wxGenericTreeItem( wxGenericTreeItem
*parent
,
163 const wxString
& text
,
166 wxTreeItemData
*data
);
168 ~wxGenericTreeItem();
171 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
173 const wxString
& GetText() const { return m_text
; }
174 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
175 { return m_images
[which
]; }
176 wxTreeItemData
*GetData() const { return m_data
; }
178 // returns the current image for the item (depending on its
179 // selected/expanded/whatever state)
180 int GetCurrentImage() const;
182 void SetText( const wxString
&text
);
183 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
184 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
186 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
188 void SetBold(bool bold
) { m_isBold
= bold
; }
190 int GetX() const { return m_x
; }
191 int GetY() const { return m_y
; }
193 void SetX(int x
) { m_x
= x
; }
194 void SetY(int y
) { m_y
= y
; }
196 int GetHeight() const { return m_height
; }
197 int GetWidth() const { return m_width
; }
199 void SetHeight(int h
) { m_height
= h
; }
200 void SetWidth(int w
) { m_width
= w
; }
202 wxGenericTreeItem
*GetParent() const { return m_parent
; }
206 // deletes all children notifying the treectrl about it
207 void DeleteChildren(wxGenericTreeCtrl
*tree
);
209 // get count of all children (and grand children if 'recursively')
210 size_t GetChildrenCount(bool recursively
= true) const;
212 void Insert(wxGenericTreeItem
*child
, size_t index
)
213 { m_children
.Insert(child
, index
); }
215 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
217 // return the item at given position (or NULL if no item), onButton is
218 // true if the point belongs to the item's button, otherwise it lies
219 // on the item's label
220 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
221 const wxGenericTreeCtrl
*,
225 void Expand() { m_isCollapsed
= false; }
226 void Collapse() { m_isCollapsed
= true; }
228 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
231 bool HasChildren() const { return !m_children
.IsEmpty(); }
232 bool IsSelected() const { return m_hasHilight
!= 0; }
233 bool IsExpanded() const { return !m_isCollapsed
; }
234 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
235 bool IsBold() const { return m_isBold
!= 0; }
238 // get them - may be NULL
239 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
240 // get them ensuring that the pointer is not NULL
241 wxTreeItemAttr
& Attr()
245 m_attr
= new wxTreeItemAttr
;
251 void SetAttributes(wxTreeItemAttr
*attr
)
253 if ( m_ownsAttr
) delete m_attr
;
257 // set them and delete when done
258 void AssignAttributes(wxTreeItemAttr
*attr
)
265 // since there can be very many of these, we save size by chosing
266 // the smallest representation for the elements and by ordering
267 // the members to avoid padding.
268 wxString m_text
; // label to be rendered for item
270 wxTreeItemData
*m_data
; // user-provided data
272 wxArrayGenericTreeItems m_children
; // list of children
273 wxGenericTreeItem
*m_parent
; // parent of this item
275 wxTreeItemAttr
*m_attr
; // attributes???
277 // tree ctrl images for the normal, selected, expanded and
278 // expanded+selected states
279 int m_images
[wxTreeItemIcon_Max
];
281 wxCoord m_x
; // (virtual) offset from top
282 wxCoord m_y
; // (virtual) offset from left
283 int m_width
; // width of this item
284 int m_height
; // height of this item
286 // use bitfields to save size
287 unsigned int m_isCollapsed
:1;
288 unsigned int m_hasHilight
:1; // same as focused
289 unsigned int m_hasPlus
:1; // used for item which doesn't have
290 // children but has a [+] button
291 unsigned int m_isBold
:1; // render the label in bold font
292 unsigned int m_ownsAttr
:1; // delete attribute when done
294 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
297 // =============================================================================
299 // =============================================================================
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 // translate the key or mouse event flags to the type of selection we're
307 static void EventFlagsToSelType(long style
,
311 bool &extended_select
,
312 bool &unselect_others
)
314 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
315 extended_select
= shiftDown
&& is_multiple
;
316 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
319 // check if the given item is under another one
320 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
324 if ( item
== parent
)
326 // item is a descendant of parent
330 item
= item
->GetParent();
336 // -----------------------------------------------------------------------------
337 // wxTreeRenameTimer (internal)
338 // -----------------------------------------------------------------------------
340 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
345 void wxTreeRenameTimer::Notify()
347 m_owner
->OnRenameTimer();
350 //-----------------------------------------------------------------------------
351 // wxTreeTextCtrl (internal)
352 //-----------------------------------------------------------------------------
354 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
355 EVT_CHAR (wxTreeTextCtrl::OnChar
)
356 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
357 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
360 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
361 wxGenericTreeItem
*item
)
362 : m_itemEdited(item
), m_startValue(item
->GetText())
366 m_aboutToFinish
= false;
368 int w
= m_itemEdited
->GetWidth(),
369 h
= m_itemEdited
->GetHeight();
372 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
377 int image
= item
->GetCurrentImage();
378 if ( image
!= NO_IMAGE
)
380 if ( m_owner
->m_imageListNormal
)
382 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
383 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
387 wxFAIL_MSG(_T("you must create an image list to use images!"));
391 // FIXME: what are all these hardcoded 4, 8 and 11s really?
395 wxSize bs
= DoGetBestSize() ;
396 // edit control height
399 int diff
= h
- ( bs
.y
- 8 ) ;
405 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
406 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
409 bool wxTreeTextCtrl::AcceptChanges()
411 const wxString value
= GetValue();
413 if ( value
== m_startValue
)
415 // nothing changed, always accept
416 // when an item remains unchanged, the owner
417 // needs to be notified that the user decided
418 // not to change the tree item label, and that
419 // the edit has been cancelled
421 m_owner
->OnRenameCancelled(m_itemEdited
);
425 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
427 // vetoed by the user
431 // accepted, do rename the item
432 m_owner
->SetItemText(m_itemEdited
, value
);
437 void wxTreeTextCtrl::Finish()
441 m_owner
->ResetTextControl();
443 wxPendingDelete
.Append(this);
451 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
453 switch ( event
.m_keyCode
)
468 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
472 // auto-grow the textctrl:
473 wxSize parentSize
= m_owner
->GetSize();
474 wxPoint myPos
= GetPosition();
475 wxSize mySize
= GetSize();
477 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
478 if (myPos
.x
+ sx
> parentSize
.x
)
479 sx
= parentSize
.x
- myPos
.x
;
482 SetSize(sx
, wxDefaultCoord
);
488 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
490 if ( !m_finished
&& !m_aboutToFinish
)
492 // We must finish regardless of success, otherwise we'll get
496 if ( !AcceptChanges() )
497 m_owner
->OnRenameCancelled( m_itemEdited
);
500 // We must let the native text control handle focus, too, otherwise
501 // it could have problems with the cursor (e.g., in wxGTK).
505 // -----------------------------------------------------------------------------
507 // -----------------------------------------------------------------------------
509 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
510 const wxString
& text
,
511 int image
, int selImage
,
512 wxTreeItemData
*data
)
515 m_images
[wxTreeItemIcon_Normal
] = image
;
516 m_images
[wxTreeItemIcon_Selected
] = selImage
;
517 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
518 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
523 m_isCollapsed
= true;
524 m_hasHilight
= false;
530 m_attr
= (wxTreeItemAttr
*)NULL
;
533 // We don't know the height here yet.
538 wxGenericTreeItem::~wxGenericTreeItem()
542 if (m_ownsAttr
) delete m_attr
;
544 wxASSERT_MSG( m_children
.IsEmpty(),
545 wxT("please call DeleteChildren() before deleting the item") );
548 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
550 size_t count
= m_children
.Count();
551 for ( size_t n
= 0; n
< count
; n
++ )
553 wxGenericTreeItem
*child
= m_children
[n
];
554 tree
->SendDeleteEvent(child
);
556 child
->DeleteChildren(tree
);
557 if ( child
== tree
->m_select_me
)
558 tree
->m_select_me
= NULL
;
565 void wxGenericTreeItem::SetText( const wxString
&text
)
570 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
572 size_t count
= m_children
.Count();
576 size_t total
= count
;
577 for (size_t n
= 0; n
< count
; ++n
)
579 total
+= m_children
[n
]->GetChildrenCount();
585 void wxGenericTreeItem::GetSize( int &x
, int &y
,
586 const wxGenericTreeCtrl
*theButton
)
588 int bottomY
=m_y
+theButton
->GetLineHeight(this);
589 if ( y
< bottomY
) y
= bottomY
;
590 int width
= m_x
+ m_width
;
591 if ( x
< width
) x
= width
;
595 size_t count
= m_children
.Count();
596 for ( size_t n
= 0; n
< count
; ++n
)
598 m_children
[n
]->GetSize( x
, y
, theButton
);
603 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
604 const wxGenericTreeCtrl
*theCtrl
,
608 // for a hidden root node, don't evaluate it, but do evaluate children
609 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
612 int h
= theCtrl
->GetLineHeight(this);
613 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
615 int y_mid
= m_y
+ h
/2;
616 if (point
.y
< y_mid
)
617 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
619 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
621 int xCross
= m_x
- theCtrl
->GetSpacing();
623 // according to the drawing code the triangels are drawn
624 // at -4 , -4 from the position up to +10/+10 max
625 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
626 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
627 HasPlus() && theCtrl
->HasButtons() )
629 // 5 is the size of the plus sign
630 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
631 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
632 HasPlus() && theCtrl
->HasButtons() )
635 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
639 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
644 // assuming every image (normal and selected) has the same size!
645 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
646 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
649 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
650 flags
|= wxTREE_HITTEST_ONITEMICON
;
652 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
658 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
659 if (point
.x
> m_x
+m_width
)
660 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
665 // if children are expanded, fall through to evaluate them
666 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
670 size_t count
= m_children
.Count();
671 for ( size_t n
= 0; n
< count
; n
++ )
673 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
681 return (wxGenericTreeItem
*) NULL
;
684 int wxGenericTreeItem::GetCurrentImage() const
686 int image
= NO_IMAGE
;
691 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
694 if ( image
== NO_IMAGE
)
696 // we usually fall back to the normal item, but try just the
697 // expanded one (and not selected) first in this case
698 image
= GetImage(wxTreeItemIcon_Expanded
);
704 image
= GetImage(wxTreeItemIcon_Selected
);
707 // maybe it doesn't have the specific image we want,
708 // try the default one instead
709 if ( image
== NO_IMAGE
) image
= GetImage();
714 // -----------------------------------------------------------------------------
715 // wxGenericTreeCtrl implementation
716 // -----------------------------------------------------------------------------
718 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
720 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
721 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
722 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
723 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
724 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
725 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
726 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
729 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
731 * wxTreeCtrl has to be a real class or we have problems with
732 * the run-time information.
735 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
738 // -----------------------------------------------------------------------------
739 // construction/destruction
740 // -----------------------------------------------------------------------------
742 void wxGenericTreeCtrl::Init()
747 m_select_me
= (wxGenericTreeItem
*) NULL
;
755 m_hilightBrush
= new wxBrush
757 wxSystemSettings::GetColour
759 wxSYS_COLOUR_HIGHLIGHT
764 m_hilightUnfocusedBrush
= new wxBrush
766 wxSystemSettings::GetColour
768 wxSYS_COLOUR_BTNSHADOW
773 m_imageListButtons
= NULL
;
774 m_ownsImageListButtons
= false;
777 m_isDragging
= false;
778 m_dropTarget
= m_oldSelection
= NULL
;
782 m_renameTimer
= NULL
;
787 m_dropEffectAboveItem
= false;
789 m_lastOnSame
= false;
791 #ifdef __WXMAC_CARBON__
792 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
794 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
796 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
797 m_normalFont
.GetFamily(),
798 m_normalFont
.GetStyle(),
800 m_normalFont
.GetUnderlined(),
801 m_normalFont
.GetFaceName(),
802 m_normalFont
.GetEncoding());
805 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
810 const wxValidator
& validator
,
811 const wxString
& name
)
815 wxGetOsVersion( &major
, &minor
);
817 style
&= ~wxTR_LINES_AT_ROOT
;
818 style
|= wxTR_NO_LINES
;
820 style
|= wxTR_ROW_LINES
;
823 if ( !wxControl::Create( parent
, id
, pos
, size
,
824 style
|wxHSCROLL
|wxVSCROLL
,
829 // If the tree display has no buttons, but does have
830 // connecting lines, we can use a narrower layout.
831 // It may not be a good idea to force this...
832 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
838 wxVisualAttributes attr
= GetDefaultAttributes();
839 SetOwnForegroundColour( attr
.colFg
);
840 SetOwnBackgroundColour( attr
.colBg
);
842 SetOwnFont(attr
.font
);
844 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
851 wxGenericTreeCtrl::~wxGenericTreeCtrl()
853 delete m_hilightBrush
;
854 delete m_hilightUnfocusedBrush
;
858 delete m_renameTimer
;
861 if (m_ownsImageListButtons
)
862 delete m_imageListButtons
;
865 // -----------------------------------------------------------------------------
867 // -----------------------------------------------------------------------------
869 unsigned int wxGenericTreeCtrl::GetCount() const
877 unsigned int count
= m_anchor
->GetChildrenCount();
878 if ( !HasFlag(wxTR_HIDE_ROOT
) )
880 // take the root itself into account
887 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
889 m_indent
= (unsigned short) indent
;
894 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
895 bool recursively
) const
897 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
899 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
902 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
904 // Do not try to expand the root node if it hasn't been created yet
905 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
907 // if we will hide the root, make sure children are visible
908 m_anchor
->SetHasPlus();
910 CalculatePositions();
913 // right now, just sets the styles. Eventually, we may
914 // want to update the inherited styles, but right now
915 // none of the parents has updatable styles
916 m_windowStyle
= styles
;
920 // -----------------------------------------------------------------------------
921 // functions to work with tree items
922 // -----------------------------------------------------------------------------
924 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
926 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
928 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
931 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
932 wxTreeItemIcon which
) const
934 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
936 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
939 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
941 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
943 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
946 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
948 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
950 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
951 return pItem
->Attr().GetTextColour();
954 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
956 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
958 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
959 return pItem
->Attr().GetBackgroundColour();
962 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
964 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
966 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
967 return pItem
->Attr().GetFont();
970 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
972 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
975 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
976 pItem
->SetText(text
);
977 CalculateSize(pItem
, dc
);
981 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
983 wxTreeItemIcon which
)
985 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
987 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
988 pItem
->SetImage(image
, which
);
991 CalculateSize(pItem
, dc
);
995 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
997 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1000 data
->SetId( item
);
1002 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1005 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1007 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1009 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1010 pItem
->SetHasPlus(has
);
1014 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1016 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1018 // avoid redrawing the tree if no real change
1019 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1020 if ( pItem
->IsBold() != bold
)
1022 pItem
->SetBold(bold
);
1027 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1030 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1036 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1037 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1040 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1041 pItem
->Attr().SetTextColour(fg
);
1042 pItem
->Attr().SetBackgroundColour(bg
);
1046 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1047 const wxColour
& col
)
1049 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1051 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1052 pItem
->Attr().SetTextColour(col
);
1056 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1057 const wxColour
& col
)
1059 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1061 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1062 pItem
->Attr().SetBackgroundColour(col
);
1066 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1068 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1070 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1071 pItem
->Attr().SetFont(font
);
1075 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1077 wxTreeCtrlBase::SetFont(font
);
1079 m_normalFont
= font
;
1080 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1081 m_normalFont
.GetFamily(),
1082 m_normalFont
.GetStyle(),
1084 m_normalFont
.GetUnderlined(),
1085 m_normalFont
.GetFaceName(),
1086 m_normalFont
.GetEncoding());
1092 // -----------------------------------------------------------------------------
1093 // item status inquiries
1094 // -----------------------------------------------------------------------------
1096 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1098 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1100 // An item is only visible if it's not a descendant of a collapsed item
1101 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1102 wxGenericTreeItem
* parent
= pItem
->GetParent();
1105 if (!parent
->IsExpanded())
1107 parent
= parent
->GetParent();
1111 GetViewStart(& startX
, & startY
);
1113 wxSize clientSize
= GetClientSize();
1116 if (!GetBoundingRect(item
, rect
))
1118 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1120 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1122 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1128 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1130 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1132 // consider that the item does have children if it has the "+" button: it
1133 // might not have them (if it had never been expanded yet) but then it
1134 // could have them as well and it's better to err on this side rather than
1135 // disabling some operations which are restricted to the items with
1136 // children for an item which does have them
1137 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1140 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1142 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1144 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1147 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1149 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1151 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1154 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1156 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1158 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1161 // -----------------------------------------------------------------------------
1163 // -----------------------------------------------------------------------------
1165 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1167 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1169 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1172 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1173 wxTreeItemIdValue
& cookie
) const
1175 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1178 return GetNextChild(item
, cookie
);
1181 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1182 wxTreeItemIdValue
& cookie
) const
1184 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1186 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1188 // it's ok to cast cookie to size_t, we never have indices big enough to
1189 // overflow "void *"
1190 size_t *pIndex
= (size_t *)&cookie
;
1191 if ( *pIndex
< children
.Count() )
1193 return children
.Item((*pIndex
)++);
1197 // there are no more of them
1198 return wxTreeItemId();
1202 #if WXWIN_COMPATIBILITY_2_4
1204 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1207 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1210 return GetNextChild(item
, cookie
);
1213 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1216 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1218 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1219 if ( (size_t)cookie
< children
.Count() )
1221 return children
.Item((size_t)cookie
++);
1225 // there are no more of them
1226 return wxTreeItemId();
1230 #endif // WXWIN_COMPATIBILITY_2_4
1232 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1234 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1236 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1237 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1240 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1242 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1244 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1245 wxGenericTreeItem
*parent
= i
->GetParent();
1246 if ( parent
== NULL
)
1248 // root item doesn't have any siblings
1249 return wxTreeItemId();
1252 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1253 int index
= siblings
.Index(i
);
1254 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1256 size_t n
= (size_t)(index
+ 1);
1257 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1260 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1262 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1264 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1265 wxGenericTreeItem
*parent
= i
->GetParent();
1266 if ( parent
== NULL
)
1268 // root item doesn't have any siblings
1269 return wxTreeItemId();
1272 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1273 int index
= siblings
.Index(i
);
1274 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1276 return index
== 0 ? wxTreeItemId()
1277 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1280 // Only for internal use right now, but should probably be public
1281 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1283 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1285 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1287 // First see if there are any children.
1288 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1289 if (children
.GetCount() > 0)
1291 return children
.Item(0);
1295 // Try a sibling of this or ancestor instead
1296 wxTreeItemId p
= item
;
1297 wxTreeItemId toFind
;
1300 toFind
= GetNextSibling(p
);
1301 p
= GetItemParent(p
);
1302 } while (p
.IsOk() && !toFind
.IsOk());
1307 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1309 wxTreeItemId id
= GetRootItem();
1318 } while (id
.IsOk());
1320 return wxTreeItemId();
1323 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1325 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1327 wxTreeItemId id
= item
;
1330 while (id
= GetNext(id
), id
.IsOk())
1336 return wxTreeItemId();
1339 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1341 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1343 wxFAIL_MSG(wxT("not implemented"));
1345 return wxTreeItemId();
1348 // called by wxTextTreeCtrl when it marks itself for deletion
1349 void wxGenericTreeCtrl::ResetTextControl()
1354 // find the first item starting with the given prefix after the given item
1355 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1356 const wxString
& prefixOrig
) const
1358 // match is case insensitive as this is more convenient to the user: having
1359 // to press Shift-letter to go to the item starting with a capital letter
1360 // would be too bothersome
1361 wxString prefix
= prefixOrig
.Lower();
1363 // determine the starting point: we shouldn't take the current item (this
1364 // allows to switch between two items starting with the same letter just by
1365 // pressing it) but we shouldn't jump to the next one if the user is
1366 // continuing to type as otherwise he might easily skip the item he wanted
1367 wxTreeItemId id
= idParent
;
1368 if ( prefix
.length() == 1 )
1373 // look for the item starting with the given prefix after it
1374 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1379 // if we haven't found anything...
1382 // ... wrap to the beginning
1384 if ( HasFlag(wxTR_HIDE_ROOT
) )
1386 // can't select virtual root
1390 // and try all the items (stop when we get to the one we started from)
1391 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1400 // -----------------------------------------------------------------------------
1402 // -----------------------------------------------------------------------------
1404 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1406 const wxString
& text
,
1409 wxTreeItemData
*data
)
1411 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1414 // should we give a warning here?
1415 return AddRoot(text
, image
, selImage
, data
);
1418 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1420 wxGenericTreeItem
*item
=
1421 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1425 data
->m_pItem
= item
;
1428 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1434 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1437 wxTreeItemData
*data
)
1439 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1441 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1443 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1444 image
, selImage
, data
);
1447 data
->m_pItem
= m_anchor
;
1450 if (HasFlag(wxTR_HIDE_ROOT
))
1452 // if root is hidden, make sure we can navigate
1454 m_anchor
->SetHasPlus();
1456 CalculatePositions();
1459 if (!HasFlag(wxTR_MULTIPLE
))
1461 m_current
= m_key_current
= m_anchor
;
1462 m_current
->SetHilight( true );
1468 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1469 const wxTreeItemId
& idPrevious
,
1470 const wxString
& text
,
1471 int image
, int selImage
,
1472 wxTreeItemData
*data
)
1474 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1477 // should we give a warning here?
1478 return AddRoot(text
, image
, selImage
, data
);
1482 if (idPrevious
.IsOk())
1484 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1485 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1486 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1489 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1493 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1495 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1496 event
.m_item
= item
;
1497 event
.SetEventObject( this );
1498 ProcessEvent( event
);
1501 // Don't leave edit or selection on a child which is about to disappear
1502 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1504 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1505 m_textCtrl
->StopEditing();
1507 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1508 m_key_current
= NULL
;
1510 if (IsDescendantOf(item
, m_select_me
)) {
1513 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1514 m_current
->SetHilight( false );
1520 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1522 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1524 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1525 ChildrenClosing(item
);
1526 item
->DeleteChildren(this);
1529 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1531 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1533 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1535 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1537 // can't delete the item being edited, cancel editing it first
1538 m_textCtrl
->StopEditing();
1541 wxGenericTreeItem
*parent
= item
->GetParent();
1543 // don't keep stale pointers around!
1544 if ( IsDescendantOf(item
, m_key_current
) )
1546 // Don't silently change the selection:
1547 // do it properly in idle time, so event
1548 // handlers get called.
1550 // m_key_current = parent;
1551 m_key_current
= NULL
;
1554 // m_select_me records whether we need to select
1555 // a different item, in idle time.
1556 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1558 m_select_me
= parent
;
1561 if ( IsDescendantOf(item
, m_current
) )
1563 // Don't silently change the selection:
1564 // do it properly in idle time, so event
1565 // handlers get called.
1567 // m_current = parent;
1569 m_select_me
= parent
;
1572 // remove the item from the tree
1575 parent
->GetChildren().Remove( item
); // remove by value
1577 else // deleting the root
1579 // nothing will be left in the tree
1583 // and delete all of its children and the item itself now
1584 item
->DeleteChildren(this);
1585 SendDeleteEvent(item
);
1587 if (item
== m_select_me
)
1593 void wxGenericTreeCtrl::DeleteAllItems()
1601 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1603 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1605 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1606 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1607 _T("can't expand hidden root") );
1609 if ( !item
->HasPlus() )
1612 if ( item
->IsExpanded() )
1615 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1616 event
.m_item
= item
;
1617 event
.SetEventObject( this );
1619 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1621 // cancelled by program
1626 CalculatePositions();
1628 RefreshSubtree(item
);
1630 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1631 ProcessEvent( event
);
1634 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1636 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1639 if ( !IsExpanded(item
) )
1643 wxTreeItemIdValue cookie
;
1644 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1645 while ( child
.IsOk() )
1649 child
= GetNextChild(item
, cookie
);
1653 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1655 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1656 _T("can't collapse hidden root") );
1658 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1660 if ( !item
->IsExpanded() )
1663 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1664 event
.m_item
= item
;
1665 event
.SetEventObject( this );
1666 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1668 // cancelled by program
1672 ChildrenClosing(item
);
1675 #if 0 // TODO why should items be collapsed recursively?
1676 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1677 size_t count
= children
.Count();
1678 for ( size_t n
= 0; n
< count
; n
++ )
1680 Collapse(children
[n
]);
1684 CalculatePositions();
1686 RefreshSubtree(item
);
1688 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1689 ProcessEvent( event
);
1692 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1695 DeleteChildren(item
);
1698 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1700 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1702 if (item
->IsExpanded())
1708 void wxGenericTreeCtrl::Unselect()
1712 m_current
->SetHilight( false );
1713 RefreshLine( m_current
);
1720 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1722 if (item
->IsSelected())
1724 item
->SetHilight(false);
1728 if (item
->HasChildren())
1730 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1731 size_t count
= children
.Count();
1732 for ( size_t n
= 0; n
< count
; ++n
)
1734 UnselectAllChildren(children
[n
]);
1739 void wxGenericTreeCtrl::UnselectAll()
1741 wxTreeItemId rootItem
= GetRootItem();
1743 // the tree might not have the root item at all
1746 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1750 // Recursive function !
1751 // To stop we must have crt_item<last_item
1753 // Tag all next children, when no more children,
1754 // Move to parent (not to tag)
1755 // Keep going... if we found last_item, we stop.
1756 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1758 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1760 if (parent
== NULL
) // This is root item
1761 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1763 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1764 int index
= children
.Index(crt_item
);
1765 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1767 size_t count
= children
.Count();
1768 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1770 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1773 return TagNextChildren(parent
, last_item
, select
);
1776 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1778 crt_item
->SetHilight(select
);
1779 RefreshLine(crt_item
);
1781 if (crt_item
==last_item
)
1784 if (crt_item
->HasChildren())
1786 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1787 size_t count
= children
.Count();
1788 for ( size_t n
= 0; n
< count
; ++n
)
1790 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1798 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1802 // item2 is not necessary after item1
1803 // choice first' and 'last' between item1 and item2
1804 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1805 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1807 bool select
= m_current
->IsSelected();
1809 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1812 TagNextChildren(first
,last
,select
);
1815 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1816 bool unselect_others
,
1817 bool extended_select
)
1819 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1823 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1824 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1826 //wxCHECK_RET( ( (!unselect_others) && is_single),
1827 // wxT("this is a single selection tree") );
1829 // to keep going anyhow !!!
1832 if (item
->IsSelected())
1833 return; // nothing to do
1834 unselect_others
= true;
1835 extended_select
= false;
1837 else if ( unselect_others
&& item
->IsSelected() )
1839 // selection change if there is more than one item currently selected
1840 wxArrayTreeItemIds selected_items
;
1841 if ( GetSelections(selected_items
) == 1 )
1845 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1846 event
.m_item
= item
;
1847 event
.m_itemOld
= m_current
;
1848 event
.SetEventObject( this );
1849 // TODO : Here we don't send any selection mode yet !
1851 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1854 wxTreeItemId parent
= GetItemParent( itemId
);
1855 while (parent
.IsOk())
1857 if (!IsExpanded(parent
))
1860 parent
= GetItemParent( parent
);
1864 if (unselect_others
)
1866 if (is_single
) Unselect(); // to speed up thing
1871 if (extended_select
)
1875 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1878 // don't change the mark (m_current)
1879 SelectItemRange(m_current
, item
);
1883 bool select
= true; // the default
1885 // Check if we need to toggle hilight (ctrl mode)
1886 if (!unselect_others
)
1887 select
=!item
->IsSelected();
1889 m_current
= m_key_current
= item
;
1890 m_current
->SetHilight(select
);
1891 RefreshLine( m_current
);
1894 // This can cause idle processing to select the root
1895 // if no item is selected, so it must be after the
1897 EnsureVisible( itemId
);
1899 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1900 GetEventHandler()->ProcessEvent( event
);
1903 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1907 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1911 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1912 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1914 item
->SetHilight(false);
1919 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1920 wxArrayTreeItemIds
&array
) const
1922 if ( item
->IsSelected() )
1923 array
.Add(wxTreeItemId(item
));
1925 if ( item
->HasChildren() )
1927 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1928 size_t count
= children
.GetCount();
1929 for ( size_t n
= 0; n
< count
; ++n
)
1930 FillArray(children
[n
], array
);
1934 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1937 wxTreeItemId idRoot
= GetRootItem();
1938 if ( idRoot
.IsOk() )
1940 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1942 //else: the tree is empty, so no selections
1944 return array
.Count();
1947 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1949 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1951 if (!item
.IsOk()) return;
1953 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1955 // first expand all parent branches
1956 wxGenericTreeItem
*parent
= gitem
->GetParent();
1958 if ( HasFlag(wxTR_HIDE_ROOT
) )
1960 while ( parent
&& parent
!= m_anchor
)
1963 parent
= parent
->GetParent();
1971 parent
= parent
->GetParent();
1975 //if (parent) CalculatePositions();
1980 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1982 if (!item
.IsOk()) return;
1984 // We have to call this here because the label in
1985 // question might just have been added and no screen
1986 // update taken place.
1988 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1993 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1995 // now scroll to the item
1996 int item_y
= gitem
->GetY();
2000 GetViewStart( &start_x
, &start_y
);
2001 start_y
*= PIXELS_PER_UNIT
;
2005 GetClientSize( &client_w
, &client_h
);
2007 if (item_y
< start_y
+3)
2012 m_anchor
->GetSize( x
, y
, this );
2013 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2014 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2015 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2016 // Item should appear at top
2017 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2019 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2024 m_anchor
->GetSize( x
, y
, this );
2025 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2026 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2027 item_y
+= PIXELS_PER_UNIT
+2;
2028 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2029 // Item should appear at bottom
2030 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
);
2034 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2035 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2037 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2038 wxGenericTreeItem
**item2
)
2040 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2042 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2045 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2047 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2049 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2051 wxCHECK_RET( !s_treeBeingSorted
,
2052 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2054 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2055 if ( children
.Count() > 1 )
2059 s_treeBeingSorted
= this;
2060 children
.Sort(tree_ctrl_compare_func
);
2061 s_treeBeingSorted
= NULL
;
2063 //else: don't make the tree dirty as nothing changed
2066 void wxGenericTreeCtrl::CalculateLineHeight()
2068 wxClientDC
dc(this);
2069 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2071 if ( m_imageListNormal
)
2073 // Calculate a m_lineHeight value from the normal Image sizes.
2074 // May be toggle off. Then wxGenericTreeCtrl will spread when
2075 // necessary (which might look ugly).
2076 int n
= m_imageListNormal
->GetImageCount();
2077 for (int i
= 0; i
< n
; i
++)
2079 int width
= 0, height
= 0;
2080 m_imageListNormal
->GetSize(i
, width
, height
);
2081 if (height
> m_lineHeight
) m_lineHeight
= height
;
2085 if (m_imageListButtons
)
2087 // Calculate a m_lineHeight value from the Button image sizes.
2088 // May be toggle off. Then wxGenericTreeCtrl will spread when
2089 // necessary (which might look ugly).
2090 int n
= m_imageListButtons
->GetImageCount();
2091 for (int i
= 0; i
< n
; i
++)
2093 int width
= 0, height
= 0;
2094 m_imageListButtons
->GetSize(i
, width
, height
);
2095 if (height
> m_lineHeight
) m_lineHeight
= height
;
2099 if (m_lineHeight
< 30)
2100 m_lineHeight
+= 2; // at least 2 pixels
2102 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2105 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2107 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2108 m_imageListNormal
= imageList
;
2109 m_ownsImageListNormal
= false;
2111 // Don't do any drawing if we're setting the list to NULL,
2112 // since we may be in the process of deleting the tree control.
2114 CalculateLineHeight();
2117 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2119 if (m_ownsImageListState
) delete m_imageListState
;
2120 m_imageListState
= imageList
;
2121 m_ownsImageListState
= false;
2124 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2126 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2127 m_imageListButtons
= imageList
;
2128 m_ownsImageListButtons
= false;
2130 CalculateLineHeight();
2133 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2135 SetButtonsImageList(imageList
);
2136 m_ownsImageListButtons
= true;
2139 // -----------------------------------------------------------------------------
2141 // -----------------------------------------------------------------------------
2143 void wxGenericTreeCtrl::AdjustMyScrollbars()
2148 m_anchor
->GetSize( x
, y
, this );
2149 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2150 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2151 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2152 int y_pos
= GetScrollPos( wxVERTICAL
);
2153 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2157 SetScrollbars( 0, 0, 0, 0 );
2161 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2163 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2164 return item
->GetHeight();
2166 return m_lineHeight
;
2169 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2171 // TODO implement "state" icon on items
2173 wxTreeItemAttr
*attr
= item
->GetAttributes();
2174 if ( attr
&& attr
->HasFont() )
2175 dc
.SetFont(attr
->GetFont());
2176 else if (item
->IsBold())
2177 dc
.SetFont(m_boldFont
);
2179 long text_w
= 0, text_h
= 0;
2180 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2182 int image_h
= 0, image_w
= 0;
2183 int image
= item
->GetCurrentImage();
2184 if ( image
!= NO_IMAGE
)
2186 if ( m_imageListNormal
)
2188 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2189 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2197 int total_h
= GetLineHeight(item
);
2198 bool drawItemBackground
= false;
2200 if ( item
->IsSelected() )
2202 // under mac selections are only a rectangle in case they don't have the focus
2206 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2207 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2211 dc
.SetBrush( *m_hilightBrush
) ;
2214 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2216 drawItemBackground
= true;
2221 if ( attr
&& attr
->HasBackgroundColour() )
2223 drawItemBackground
= true;
2224 colBg
= attr
->GetBackgroundColour();
2228 colBg
= GetBackgroundColour();
2230 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2233 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2235 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2239 DoGetPosition(&x
, &y
);
2241 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2245 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2247 // If it's selected, and there's an image, then we should
2248 // take care to leave the area under the image painted in the
2249 // background colour.
2250 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2251 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2253 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2254 // don't allow backgrounds to be customized. Not drawing the background,
2255 // except for custom item backgrounds, works for both kinds of theme.
2256 else if (drawItemBackground
)
2258 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2259 item
->GetWidth()+2, total_h
-offset
);
2263 if ( image
!= NO_IMAGE
)
2265 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2266 m_imageListNormal
->Draw( image
, dc
,
2268 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2269 wxIMAGELIST_DRAW_TRANSPARENT
);
2270 dc
.DestroyClippingRegion();
2273 dc
.SetBackgroundMode(wxTRANSPARENT
);
2274 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2275 dc
.DrawText( item
->GetText(),
2276 (wxCoord
)(image_w
+ item
->GetX()),
2277 (wxCoord
)(item
->GetY() + extraH
));
2279 // restore normal font
2280 dc
.SetFont( m_normalFont
);
2283 // Now y stands for the top of the item, whereas it used to stand for middle !
2284 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2286 int x
= level
*m_indent
;
2287 if (!HasFlag(wxTR_HIDE_ROOT
))
2291 else if (level
== 0)
2293 // always expand hidden root
2295 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2296 int count
= children
.Count();
2302 PaintLevel(children
[n
], dc
, 1, y
);
2303 } while (++n
< count
);
2305 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2307 // draw line down to last child
2308 origY
+= GetLineHeight(children
[0])>>1;
2309 oldY
+= GetLineHeight(children
[n
-1])>>1;
2310 dc
.DrawLine(3, origY
, 3, oldY
);
2316 item
->SetX(x
+m_spacing
);
2319 int h
= GetLineHeight(item
);
2321 int y_mid
= y_top
+ (h
>>1);
2324 int exposed_x
= dc
.LogicalToDeviceX(0);
2325 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2327 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2331 // don't draw rect outline if we already have the
2332 // background color under Mac
2333 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2334 #endif // !__WXMAC__
2338 if ( item
->IsSelected()
2340 // On wxMac, if the tree doesn't have the focus we draw an empty
2341 // rectangle, so we want to make sure that the text is visible
2342 // against the normal background, not the highlightbackground, so
2343 // don't use the highlight text colour unless we have the focus.
2348 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2352 wxTreeItemAttr
*attr
= item
->GetAttributes();
2353 if (attr
&& attr
->HasTextColour())
2354 colText
= attr
->GetTextColour();
2356 colText
= GetForegroundColour();
2360 dc
.SetTextForeground(colText
);
2364 PaintItem(item
, dc
);
2366 if (HasFlag(wxTR_ROW_LINES
))
2368 // if the background colour is white, choose a
2369 // contrasting color for the lines
2370 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2371 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2372 dc
.DrawLine(0, y_top
, 10000, y_top
);
2373 dc
.DrawLine(0, y
, 10000, y
);
2376 // restore DC objects
2377 dc
.SetBrush(*wxWHITE_BRUSH
);
2378 dc
.SetPen(m_dottedPen
);
2379 dc
.SetTextForeground(*wxBLACK
);
2381 if ( !HasFlag(wxTR_NO_LINES
) )
2383 // draw the horizontal line here
2385 if (x
> (signed)m_indent
)
2386 x_start
-= m_indent
;
2387 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2389 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2392 // should the item show a button?
2393 if ( item
->HasPlus() && HasButtons() )
2395 if ( m_imageListButtons
)
2397 // draw the image button here
2400 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2401 : wxTreeItemIcon_Normal
;
2402 if ( item
->IsSelected() )
2403 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2405 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2406 int xx
= x
- image_w
/2;
2407 int yy
= y_mid
- image_h
/2;
2409 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2410 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2411 wxIMAGELIST_DRAW_TRANSPARENT
);
2413 else // no custom buttons
2415 static const int wImage
= 9;
2416 static const int hImage
= 9;
2419 if (item
->IsExpanded())
2420 flag
|= wxCONTROL_EXPANDED
;
2421 if (item
== m_underMouse
)
2422 flag
|= wxCONTROL_CURRENT
;
2424 wxRendererNative::Get().DrawTreeItemButton
2428 wxRect(x
- wImage
/2,
2437 if (item
->IsExpanded())
2439 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2440 int count
= children
.Count();
2447 PaintLevel(children
[n
], dc
, level
, y
);
2448 } while (++n
< count
);
2450 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2452 // draw line down to last child
2453 oldY
+= GetLineHeight(children
[n
-1])>>1;
2454 if (HasButtons()) y_mid
+= 5;
2456 // Only draw the portion of the line that is visible, in case it is huge
2457 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2458 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2459 yOrigin
= abs(yOrigin
);
2460 GetClientSize(&width
, &height
);
2462 // Move end points to the begining/end of the view?
2463 if (y_mid
< yOrigin
)
2465 if (oldY
> yOrigin
+ height
)
2466 oldY
= yOrigin
+ height
;
2468 // after the adjustments if y_mid is larger than oldY then the line
2469 // isn't visible at all so don't draw anything
2471 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2477 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2481 if ( item
->HasPlus() )
2483 // it's a folder, indicate it by a border
2488 // draw a line under the drop target because the item will be
2490 DrawLine(item
, !m_dropEffectAboveItem
);
2493 SetCursor(wxCURSOR_BULLSEYE
);
2498 SetCursor(wxCURSOR_NO_ENTRY
);
2502 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2504 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2506 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2508 wxClientDC
dc(this);
2510 dc
.SetLogicalFunction(wxINVERT
);
2511 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2513 int w
= i
->GetWidth() + 2;
2514 int h
= GetLineHeight(i
) + 2;
2516 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2519 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2521 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2523 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2525 wxClientDC
dc(this);
2527 dc
.SetLogicalFunction(wxINVERT
);
2533 y
+= GetLineHeight(i
) - 1;
2536 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2539 // -----------------------------------------------------------------------------
2540 // wxWidgets callbacks
2541 // -----------------------------------------------------------------------------
2543 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2551 dc
.SetFont( m_normalFont
);
2552 dc
.SetPen( m_dottedPen
);
2554 // this is now done dynamically
2555 //if(GetImageList() == NULL)
2556 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2559 PaintLevel( m_anchor
, dc
, 0, y
);
2562 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2571 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2580 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2582 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2583 te
.m_evtKey
= event
;
2584 te
.SetEventObject( this );
2585 if ( GetEventHandler()->ProcessEvent( te
) )
2587 // intercepted by the user code
2591 if ( (m_current
== 0) || (m_key_current
== 0) )
2597 // how should the selection work for this event?
2598 bool is_multiple
, extended_select
, unselect_others
;
2599 EventFlagsToSelType(GetWindowStyleFlag(),
2602 is_multiple
, extended_select
, unselect_others
);
2606 // * : Expand all/Collapse all
2607 // ' ' | return : activate
2608 // up : go up (not last children!)
2610 // left : go to parent
2611 // right : open if parent and go next
2612 // home : go to root
2613 // end : go to last item without opening parents
2614 // alnum : start or continue searching for the item with this prefix
2615 int keyCode
= event
.GetKeyCode();
2620 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2628 if ( !IsExpanded(m_current
) )
2631 ExpandAll(m_current
);
2634 //else: fall through to Collapse() it
2638 if (IsExpanded(m_current
))
2640 Collapse(m_current
);
2646 // Use the item's bounding rectangle to determine position for the event
2648 GetBoundingRect(m_current
, ItemRect
, true);
2650 wxTreeEvent
eventMenu( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2651 eventMenu
.m_item
= m_current
;
2652 // Use the left edge, vertical middle
2653 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2654 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2655 eventMenu
.SetEventObject( this );
2656 GetEventHandler()->ProcessEvent( eventMenu
);
2661 if ( !event
.HasModifiers() )
2663 wxTreeEvent
eventAct( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2664 eventAct
.m_item
= m_current
;
2665 eventAct
.SetEventObject( this );
2666 GetEventHandler()->ProcessEvent( eventAct
);
2669 // in any case, also generate the normal key event for this key,
2670 // even if we generated the ACTIVATED event above: this is what
2671 // wxMSW does and it makes sense because you might not want to
2672 // process ACTIVATED event at all and handle Space and Return
2673 // directly (and differently) which would be impossible otherwise
2677 // up goes to the previous sibling or to the last
2678 // of its children if it's expanded
2681 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2684 prev
= GetItemParent( m_key_current
);
2685 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2687 break; // don't go to root if it is hidden
2691 wxTreeItemIdValue cookie
;
2692 wxTreeItemId current
= m_key_current
;
2693 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2694 if (current
== GetFirstChild( prev
, cookie
))
2696 // otherwise we return to where we came from
2697 DoSelectItem( prev
, unselect_others
, extended_select
);
2698 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2705 while ( IsExpanded(prev
) && HasChildren(prev
) )
2707 wxTreeItemId child
= GetLastChild(prev
);
2714 DoSelectItem( prev
, unselect_others
, extended_select
);
2715 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2720 // left arrow goes to the parent
2723 wxTreeItemId prev
= GetItemParent( m_current
);
2724 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2726 // don't go to root if it is hidden
2727 prev
= GetPrevSibling( m_current
);
2731 DoSelectItem( prev
, unselect_others
, extended_select
);
2737 // this works the same as the down arrow except that we
2738 // also expand the item if it wasn't expanded yet
2744 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2746 wxTreeItemIdValue cookie
;
2747 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2748 DoSelectItem( child
, unselect_others
, extended_select
);
2749 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2753 wxTreeItemId next
= GetNextSibling( m_key_current
);
2756 wxTreeItemId current
= m_key_current
;
2757 while (current
.IsOk() && !next
)
2759 current
= GetItemParent( current
);
2760 if (current
) next
= GetNextSibling( current
);
2765 DoSelectItem( next
, unselect_others
, extended_select
);
2766 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2772 // <End> selects the last visible tree item
2775 wxTreeItemId last
= GetRootItem();
2777 while ( last
.IsOk() && IsExpanded(last
) )
2779 wxTreeItemId lastChild
= GetLastChild(last
);
2781 // it may happen if the item was expanded but then all of
2782 // its children have been deleted - so IsExpanded() returned
2783 // true, but GetLastChild() returned invalid item
2792 DoSelectItem( last
, unselect_others
, extended_select
);
2797 // <Home> selects the root item
2800 wxTreeItemId prev
= GetRootItem();
2804 if ( HasFlag(wxTR_HIDE_ROOT
) )
2806 wxTreeItemIdValue cookie
;
2807 prev
= GetFirstChild(prev
, cookie
);
2812 DoSelectItem( prev
, unselect_others
, extended_select
);
2817 // do not use wxIsalnum() here
2818 if ( !event
.HasModifiers() &&
2819 ((keyCode
>= '0' && keyCode
<= '9') ||
2820 (keyCode
>= 'a' && keyCode
<= 'z') ||
2821 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2823 // find the next item starting with the given prefix
2824 wxChar ch
= (wxChar
)keyCode
;
2826 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2837 // also start the timer to reset the current prefix if the user
2838 // doesn't press any more alnum keys soon -- we wouldn't want
2839 // to use this prefix for a new item search
2842 m_findTimer
= new wxTreeFindTimer(this);
2845 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2854 wxTreeItemId
wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
)
2856 // JACS: removed wxYieldIfNeeded() because it can cause the window
2857 // to be deleted from under us if a close window event is pending
2862 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2863 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2864 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2865 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2866 if (flags
) return wxTreeItemId();
2868 if (m_anchor
== NULL
)
2870 flags
= wxTREE_HITTEST_NOWHERE
;
2871 return wxTreeItemId();
2874 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2878 flags
= wxTREE_HITTEST_NOWHERE
;
2879 return wxTreeItemId();
2884 // get the bounding rectangle of the item (or of its label only)
2885 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2887 bool textOnly
) const
2889 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2891 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2896 rect
.width
= i
->GetWidth();
2898 if ( m_imageListNormal
)
2900 int image_w
, image_h
;
2901 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
2902 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2905 else // the entire line
2908 rect
.width
= GetClientSize().x
;
2912 rect
.height
= GetLineHeight(i
);
2914 // we have to return the logical coordinates, not physical ones
2915 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
2920 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2921 wxClassInfo
* WXUNUSED(textCtrlClass
))
2923 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
2925 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2927 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2928 te
.m_item
= itemEdit
;
2929 te
.SetEventObject( this );
2930 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2936 // We have to call this here because the label in
2937 // question might just have been added and no screen
2938 // update taken place.
2940 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2946 // TODO: use textCtrlClass here to create the control of correct class
2947 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2949 m_textCtrl
->SetFocus();
2954 // returns a pointer to the text edit control if the item is being
2955 // edited, NULL otherwise (it's assumed that no more than one item may
2956 // be edited simultaneously)
2957 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2962 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
2963 bool discardChanges
)
2965 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
2967 m_textCtrl
->EndEdit(discardChanges
);
2970 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2971 const wxString
& value
)
2973 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2975 le
.SetEventObject( this );
2977 le
.m_editCancelled
= false;
2979 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2982 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2984 // let owner know that the edit was cancelled
2985 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2987 le
.SetEventObject( this );
2988 le
.m_label
= wxEmptyString
;
2989 le
.m_editCancelled
= true;
2991 GetEventHandler()->ProcessEvent( le
);
2994 void wxGenericTreeCtrl::OnRenameTimer()
2996 EditLabel( m_current
);
2999 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3001 if ( !m_anchor
) return;
3003 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3005 // Is the mouse over a tree item button?
3007 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3008 wxGenericTreeItem
*underMouse
= thisItem
;
3010 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3011 #endif // wxUSE_TOOLTIPS
3014 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3015 (!event
.LeftIsDown()) &&
3017 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3025 if (underMouse
!= m_underMouse
)
3029 // unhighlight old item
3030 wxGenericTreeItem
*tmp
= m_underMouse
;
3031 m_underMouse
= NULL
;
3035 m_underMouse
= underMouse
;
3037 RefreshLine( m_underMouse
);
3041 // Determines what item we are hovering over and need a tooltip for
3042 wxTreeItemId hoverItem
= thisItem
;
3044 // We do not want a tooltip if we are dragging, or if the rename timer is running
3045 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3047 // Ask the tree control what tooltip (if any) should be shown
3048 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
3049 hevent
.m_item
= hoverItem
;
3050 hevent
.SetEventObject(this);
3052 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3054 SetToolTip(hevent
.m_label
);
3059 // we process left mouse up event (enables in-place edit), right down
3060 // (pass to the user code), left dbl click (activate item) and
3061 // dragging/moving events for items drag-and-drop
3062 if ( !(event
.LeftDown() ||
3064 event
.RightDown() ||
3065 event
.LeftDClick() ||
3067 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3076 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3078 if ( event
.Dragging() && !m_isDragging
)
3080 if (m_dragCount
== 0)
3085 if (m_dragCount
!= 3)
3087 // wait until user drags a bit further...
3091 wxEventType command
= event
.RightIsDown()
3092 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3093 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3095 wxTreeEvent
nevent( command
, GetId() );
3096 nevent
.m_item
= m_current
;
3097 nevent
.SetEventObject(this);
3098 nevent
.SetPoint(CalcScrolledPosition(pt
));
3100 // by default the dragging is not supported, the user code must
3101 // explicitly allow the event for it to take place
3104 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3106 // we're going to drag this item
3107 m_isDragging
= true;
3109 // remember the old cursor because we will change it while
3111 m_oldCursor
= m_cursor
;
3113 // in a single selection control, hide the selection temporarily
3114 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3116 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3118 if ( m_oldSelection
)
3120 m_oldSelection
->SetHilight(false);
3121 RefreshLine(m_oldSelection
);
3128 else if ( event
.Dragging() )
3130 if ( item
!= m_dropTarget
)
3132 // unhighlight the previous drop target
3133 DrawDropEffect(m_dropTarget
);
3135 m_dropTarget
= item
;
3137 // highlight the current drop target if any
3138 DrawDropEffect(m_dropTarget
);
3140 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3147 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3151 // erase the highlighting
3152 DrawDropEffect(m_dropTarget
);
3154 if ( m_oldSelection
)
3156 m_oldSelection
->SetHilight(true);
3157 RefreshLine(m_oldSelection
);
3158 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3161 // generate the drag end event
3162 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3164 eventEndDrag
.m_item
= item
;
3165 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3166 eventEndDrag
.SetEventObject(this);
3168 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3170 m_isDragging
= false;
3171 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3173 SetCursor(m_oldCursor
);
3175 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3183 // If we got to this point, we are not dragging or moving the mouse.
3184 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3185 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3186 // We skip even if we didn't hit an item because we still should
3187 // restore focus to the tree control even if we didn't exactly hit an item.
3188 if ( event
.LeftDown() )
3193 // here we process only the messages which happen on tree items
3197 if (item
== NULL
) return; /* we hit the blank area */
3199 if ( event
.RightDown() )
3201 // If the item is already selected, do not update the selection.
3202 // Multi-selections should not be cleared if a selected item is clicked.
3203 if (!IsSelected(item
))
3205 DoSelectItem(item
, true, false);
3208 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3209 nevent
.m_item
= item
;
3210 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3211 nevent
.SetEventObject(this);
3212 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3214 // Consistent with MSW (for now), send the ITEM_MENU *after*
3215 // the RIGHT_CLICK event. TODO: This behavior may change.
3216 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, GetId());
3217 nevent2
.m_item
= item
;
3218 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3219 nevent2
.SetEventObject(this);
3220 GetEventHandler()->ProcessEvent(nevent2
);
3222 else if ( event
.LeftUp() )
3224 // this facilitates multiple-item drag-and-drop
3226 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3228 wxArrayTreeItemIds selections
;
3229 size_t count
= GetSelections(selections
);
3235 DoSelectItem(item
, true, false);
3241 if ( (item
== m_current
) &&
3242 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3243 HasFlag(wxTR_EDIT_LABELS
) )
3245 if ( m_renameTimer
)
3247 if ( m_renameTimer
->IsRunning() )
3248 m_renameTimer
->Stop();
3252 m_renameTimer
= new wxTreeRenameTimer( this );
3255 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3258 m_lastOnSame
= false;
3261 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3263 if ( event
.LeftDown() )
3265 m_lastOnSame
= item
== m_current
;
3268 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3270 // only toggle the item for a single click, double click on
3271 // the button doesn't do anything (it toggles the item twice)
3272 if ( event
.LeftDown() )
3277 // don't select the item if the button was clicked
3282 // clear the previously selected items, if the
3283 // user clicked outside of the present selection.
3284 // otherwise, perform the deselection on mouse-up.
3285 // this allows multiple drag and drop to work.
3286 // but if Cmd is down, toggle selection of the clicked item
3287 if (!IsSelected(item
) || event
.CmdDown())
3289 // how should the selection work for this event?
3290 bool is_multiple
, extended_select
, unselect_others
;
3291 EventFlagsToSelType(GetWindowStyleFlag(),
3294 is_multiple
, extended_select
, unselect_others
);
3296 DoSelectItem(item
, unselect_others
, extended_select
);
3300 // For some reason, Windows isn't recognizing a left double-click,
3301 // so we need to simulate it here. Allow 200 milliseconds for now.
3302 if ( event
.LeftDClick() )
3304 // double clicking should not start editing the item label
3305 if ( m_renameTimer
)
3306 m_renameTimer
->Stop();
3308 m_lastOnSame
= false;
3310 // send activate event first
3311 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3312 nevent
.m_item
= item
;
3313 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3314 nevent
.SetEventObject( this );
3315 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3317 // if the user code didn't process the activate event,
3318 // handle it ourselves by toggling the item when it is
3320 if ( item
->HasPlus() )
3330 void wxGenericTreeCtrl::OnInternalIdle()
3332 wxWindow::OnInternalIdle();
3334 // Check if we need to select the root item
3335 // because nothing else has been selected.
3336 // Delaying it means that we can invoke event handlers
3337 // as required, when a first item is selected.
3338 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3341 SelectItem(m_select_me
);
3342 else if (GetRootItem().IsOk())
3343 SelectItem(GetRootItem());
3346 /* after all changes have been done to the tree control,
3347 * we actually redraw the tree when everything is over */
3349 if (!m_dirty
) return;
3350 if (m_freezeCount
) return;
3354 CalculatePositions();
3356 AdjustMyScrollbars();
3359 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3364 wxTreeItemAttr
*attr
= item
->GetAttributes();
3365 if ( attr
&& attr
->HasFont() )
3366 dc
.SetFont(attr
->GetFont());
3367 else if ( item
->IsBold() )
3368 dc
.SetFont(m_boldFont
);
3370 dc
.SetFont(m_normalFont
);
3372 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3375 // restore normal font
3376 dc
.SetFont( m_normalFont
);
3380 int image
= item
->GetCurrentImage();
3381 if ( image
!= NO_IMAGE
)
3383 if ( m_imageListNormal
)
3385 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3386 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3390 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3393 total_h
+= 2; // at least 2 pixels
3395 total_h
+= total_h
/10; // otherwise 10% extra spacing
3397 item
->SetHeight(total_h
);
3398 if (total_h
>m_lineHeight
)
3399 m_lineHeight
=total_h
;
3401 item
->SetWidth(image_w
+text_w
+2);
3404 // -----------------------------------------------------------------------------
3405 // for developper : y is now the top of the level
3406 // not the middle of it !
3407 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3409 int x
= level
*m_indent
;
3410 if (!HasFlag(wxTR_HIDE_ROOT
))
3414 else if (level
== 0)
3416 // a hidden root is not evaluated, but its
3417 // children are always calculated
3421 CalculateSize( item
, dc
);
3424 item
->SetX( x
+m_spacing
);
3426 y
+= GetLineHeight(item
);
3428 if ( !item
->IsExpanded() )
3430 // we don't need to calculate collapsed branches
3435 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3436 size_t n
, count
= children
.Count();
3438 for (n
= 0; n
< count
; ++n
)
3439 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3442 void wxGenericTreeCtrl::CalculatePositions()
3444 if ( !m_anchor
) return;
3446 wxClientDC
dc(this);
3449 dc
.SetFont( m_normalFont
);
3451 dc
.SetPen( m_dottedPen
);
3452 //if(GetImageList() == NULL)
3453 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3456 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3459 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3461 if (m_dirty
) return;
3462 if (m_freezeCount
) return;
3464 wxSize client
= GetClientSize();
3467 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3468 rect
.width
= client
.x
;
3469 rect
.height
= client
.y
;
3471 Refresh(true, &rect
);
3473 AdjustMyScrollbars();
3476 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3478 if (m_dirty
) return;
3479 if (m_freezeCount
) return;
3482 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3483 rect
.width
= GetClientSize().x
;
3484 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3486 Refresh(true, &rect
);
3489 void wxGenericTreeCtrl::RefreshSelected()
3491 if (m_freezeCount
) return;
3493 // TODO: this is awfully inefficient, we should keep the list of all
3494 // selected items internally, should be much faster
3496 RefreshSelectedUnder(m_anchor
);
3499 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3501 if (m_freezeCount
) return;
3503 if ( item
->IsSelected() )
3506 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3507 size_t count
= children
.GetCount();
3508 for ( size_t n
= 0; n
< count
; n
++ )
3510 RefreshSelectedUnder(children
[n
]);
3514 void wxGenericTreeCtrl::Freeze()
3519 void wxGenericTreeCtrl::Thaw()
3521 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3523 if ( --m_freezeCount
== 0 )
3529 // ----------------------------------------------------------------------------
3530 // changing colours: we need to refresh the tree control
3531 // ----------------------------------------------------------------------------
3533 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3535 if ( !wxWindow::SetBackgroundColour(colour
) )
3538 if (m_freezeCount
) return true;
3545 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3547 if ( !wxWindow::SetForegroundColour(colour
) )
3550 if (m_freezeCount
) return true;
3557 // Process the tooltip event, to speed up event processing.
3558 // Doesn't actually get a tooltip.
3559 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3565 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3566 // be removed, as well as the #else case below.
3567 #define _USE_VISATTR 0
3570 #include "wx/listbox.h"
3576 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3578 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3582 // Use the same color scheme as wxListBox
3583 return wxListBox::GetClassDefaultAttributes(variant
);
3585 wxVisualAttributes attr
;
3586 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3587 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3588 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3593 #if WXWIN_COMPATIBILITY_2_4
3595 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3597 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3600 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3602 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3605 #endif // WXWIN_COMPATIBILITY_2_4
3607 #endif // wxUSE_TREECTRL