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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treebase.h"
30 #include "wx/treectrl.h"
31 #include "wx/generic/treectlg.h"
33 #include "wx/textctrl.h"
34 #include "wx/imaglist.h"
35 #include "wx/settings.h"
36 #include "wx/dcclient.h"
38 #include "wx/renderer.h"
41 #include "wx/mac/private.h"
44 // -----------------------------------------------------------------------------
46 // -----------------------------------------------------------------------------
48 class WXDLLEXPORT wxGenericTreeItem
;
50 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 static const int NO_IMAGE
= -1;
58 static const int PIXELS_PER_UNIT
= 10;
60 // the margin between the item image and the item text
61 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
63 // -----------------------------------------------------------------------------
65 // -----------------------------------------------------------------------------
67 // timer used for enabling in-place edit
68 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
71 // start editing the current item after half a second (if the mouse hasn't
72 // been clicked/moved)
75 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
77 virtual void Notify();
80 wxGenericTreeCtrl
*m_owner
;
82 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
85 // control used for in-place edit
86 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
89 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
91 void EndEdit(bool discardChanges
= false)
99 m_aboutToFinish
= true;
101 // Notify the owner about the changes
104 // Even if vetoed, close the control (consistent with MSW)
112 m_owner
->OnRenameCancelled(m_itemEdited
);
114 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
117 void OnChar( wxKeyEvent
&event
);
118 void OnKeyUp( wxKeyEvent
&event
);
119 void OnKillFocus( wxFocusEvent
&event
);
121 bool AcceptChanges();
125 wxGenericTreeCtrl
*m_owner
;
126 wxGenericTreeItem
*m_itemEdited
;
127 wxString m_startValue
;
129 bool m_aboutToFinish
;
131 DECLARE_EVENT_TABLE()
132 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
135 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
136 // for a sufficiently long time
137 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
140 // reset the current prefix after half a second of inactivity
141 enum { DELAY
= 500 };
143 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
145 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
148 wxGenericTreeCtrl
*m_owner
;
150 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
154 class WXDLLEXPORT wxGenericTreeItem
158 wxGenericTreeItem() { m_data
= NULL
; }
159 wxGenericTreeItem( wxGenericTreeItem
*parent
,
160 const wxString
& text
,
163 wxTreeItemData
*data
);
165 ~wxGenericTreeItem();
168 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
170 const wxString
& GetText() const { return m_text
; }
171 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
172 { return m_images
[which
]; }
173 wxTreeItemData
*GetData() const { return m_data
; }
175 // returns the current image for the item (depending on its
176 // selected/expanded/whatever state)
177 int GetCurrentImage() const;
179 void SetText( const wxString
&text
);
180 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
181 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
183 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
185 void SetBold(bool bold
) { m_isBold
= bold
; }
187 int GetX() const { return m_x
; }
188 int GetY() const { return m_y
; }
190 void SetX(int x
) { m_x
= x
; }
191 void SetY(int y
) { m_y
= y
; }
193 int GetHeight() const { return m_height
; }
194 int GetWidth() const { return m_width
; }
196 void SetHeight(int h
) { m_height
= h
; }
197 void SetWidth(int w
) { m_width
= w
; }
199 wxGenericTreeItem
*GetParent() const { return m_parent
; }
203 // deletes all children notifying the treectrl about it
204 void DeleteChildren(wxGenericTreeCtrl
*tree
);
206 // get count of all children (and grand children if 'recursively')
207 size_t GetChildrenCount(bool recursively
= true) const;
209 void Insert(wxGenericTreeItem
*child
, size_t index
)
210 { m_children
.Insert(child
, index
); }
212 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
214 // return the item at given position (or NULL if no item), onButton is
215 // true if the point belongs to the item's button, otherwise it lies
216 // on the item's label
217 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
218 const wxGenericTreeCtrl
*,
222 void Expand() { m_isCollapsed
= false; }
223 void Collapse() { m_isCollapsed
= true; }
225 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
228 bool HasChildren() const { return !m_children
.IsEmpty(); }
229 bool IsSelected() const { return m_hasHilight
!= 0; }
230 bool IsExpanded() const { return !m_isCollapsed
; }
231 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
232 bool IsBold() const { return m_isBold
!= 0; }
235 // get them - may be NULL
236 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
237 // get them ensuring that the pointer is not NULL
238 wxTreeItemAttr
& Attr()
242 m_attr
= new wxTreeItemAttr
;
248 void SetAttributes(wxTreeItemAttr
*attr
)
250 if ( m_ownsAttr
) delete m_attr
;
254 // set them and delete when done
255 void AssignAttributes(wxTreeItemAttr
*attr
)
262 // since there can be very many of these, we save size by chosing
263 // the smallest representation for the elements and by ordering
264 // the members to avoid padding.
265 wxString m_text
; // label to be rendered for item
267 wxTreeItemData
*m_data
; // user-provided data
269 wxArrayGenericTreeItems m_children
; // list of children
270 wxGenericTreeItem
*m_parent
; // parent of this item
272 wxTreeItemAttr
*m_attr
; // attributes???
274 // tree ctrl images for the normal, selected, expanded and
275 // expanded+selected states
276 int m_images
[wxTreeItemIcon_Max
];
278 wxCoord m_x
; // (virtual) offset from top
279 wxCoord m_y
; // (virtual) offset from left
280 int m_width
; // width of this item
281 int m_height
; // height of this item
283 // use bitfields to save size
284 unsigned int m_isCollapsed
:1;
285 unsigned int m_hasHilight
:1; // same as focused
286 unsigned int m_hasPlus
:1; // used for item which doesn't have
287 // children but has a [+] button
288 unsigned int m_isBold
:1; // render the label in bold font
289 unsigned int m_ownsAttr
:1; // delete attribute when done
291 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
294 // =============================================================================
296 // =============================================================================
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
302 // translate the key or mouse event flags to the type of selection we're
304 static void EventFlagsToSelType(long style
,
308 bool &extended_select
,
309 bool &unselect_others
)
311 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
312 extended_select
= shiftDown
&& is_multiple
;
313 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
316 // check if the given item is under another one
317 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
321 if ( item
== parent
)
323 // item is a descendant of parent
327 item
= item
->GetParent();
333 // -----------------------------------------------------------------------------
334 // wxTreeRenameTimer (internal)
335 // -----------------------------------------------------------------------------
337 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
342 void wxTreeRenameTimer::Notify()
344 m_owner
->OnRenameTimer();
347 //-----------------------------------------------------------------------------
348 // wxTreeTextCtrl (internal)
349 //-----------------------------------------------------------------------------
351 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
352 EVT_CHAR (wxTreeTextCtrl::OnChar
)
353 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
354 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
357 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
358 wxGenericTreeItem
*item
)
359 : m_itemEdited(item
), m_startValue(item
->GetText())
363 m_aboutToFinish
= false;
365 int w
= m_itemEdited
->GetWidth(),
366 h
= m_itemEdited
->GetHeight();
369 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
374 int image
= item
->GetCurrentImage();
375 if ( image
!= NO_IMAGE
)
377 if ( m_owner
->m_imageListNormal
)
379 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
380 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
384 wxFAIL_MSG(_T("you must create an image list to use images!"));
388 // FIXME: what are all these hardcoded 4, 8 and 11s really?
392 wxSize bs
= DoGetBestSize() ;
393 // edit control height
396 int diff
= h
- ( bs
.y
- 8 ) ;
402 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
403 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
406 bool wxTreeTextCtrl::AcceptChanges()
408 const wxString value
= GetValue();
410 if ( value
== m_startValue
)
412 // nothing changed, always accept
413 // when an item remains unchanged, the owner
414 // needs to be notified that the user decided
415 // not to change the tree item label, and that
416 // the edit has been cancelled
418 m_owner
->OnRenameCancelled(m_itemEdited
);
422 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
424 // vetoed by the user
428 // accepted, do rename the item
429 m_owner
->SetItemText(m_itemEdited
, value
);
434 void wxTreeTextCtrl::Finish()
438 m_owner
->ResetTextControl();
440 wxPendingDelete
.Append(this);
448 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
450 switch ( event
.m_keyCode
)
465 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
469 // auto-grow the textctrl:
470 wxSize parentSize
= m_owner
->GetSize();
471 wxPoint myPos
= GetPosition();
472 wxSize mySize
= GetSize();
474 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
475 if (myPos
.x
+ sx
> parentSize
.x
)
476 sx
= parentSize
.x
- myPos
.x
;
479 SetSize(sx
, wxDefaultCoord
);
485 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
487 if ( !m_finished
&& !m_aboutToFinish
)
489 // We must finish regardless of success, otherwise we'll get
493 if ( !AcceptChanges() )
494 m_owner
->OnRenameCancelled( m_itemEdited
);
497 // We must let the native text control handle focus, too, otherwise
498 // it could have problems with the cursor (e.g., in wxGTK).
502 // -----------------------------------------------------------------------------
504 // -----------------------------------------------------------------------------
506 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
507 const wxString
& text
,
508 int image
, int selImage
,
509 wxTreeItemData
*data
)
512 m_images
[wxTreeItemIcon_Normal
] = image
;
513 m_images
[wxTreeItemIcon_Selected
] = selImage
;
514 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
515 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
520 m_isCollapsed
= true;
521 m_hasHilight
= false;
527 m_attr
= (wxTreeItemAttr
*)NULL
;
530 // We don't know the height here yet.
535 wxGenericTreeItem::~wxGenericTreeItem()
539 if (m_ownsAttr
) delete m_attr
;
541 wxASSERT_MSG( m_children
.IsEmpty(),
542 wxT("please call DeleteChildren() before deleting the item") );
545 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
547 size_t count
= m_children
.Count();
548 for ( size_t n
= 0; n
< count
; n
++ )
550 wxGenericTreeItem
*child
= m_children
[n
];
551 tree
->SendDeleteEvent(child
);
553 child
->DeleteChildren(tree
);
554 if ( child
== tree
->m_select_me
)
555 tree
->m_select_me
= NULL
;
562 void wxGenericTreeItem::SetText( const wxString
&text
)
567 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
569 size_t count
= m_children
.Count();
573 size_t total
= count
;
574 for (size_t n
= 0; n
< count
; ++n
)
576 total
+= m_children
[n
]->GetChildrenCount();
582 void wxGenericTreeItem::GetSize( int &x
, int &y
,
583 const wxGenericTreeCtrl
*theButton
)
585 int bottomY
=m_y
+theButton
->GetLineHeight(this);
586 if ( y
< bottomY
) y
= bottomY
;
587 int width
= m_x
+ m_width
;
588 if ( x
< width
) x
= width
;
592 size_t count
= m_children
.Count();
593 for ( size_t n
= 0; n
< count
; ++n
)
595 m_children
[n
]->GetSize( x
, y
, theButton
);
600 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
601 const wxGenericTreeCtrl
*theCtrl
,
605 // for a hidden root node, don't evaluate it, but do evaluate children
606 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
609 int h
= theCtrl
->GetLineHeight(this);
610 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
612 int y_mid
= m_y
+ h
/2;
613 if (point
.y
< y_mid
)
614 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
616 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
618 int xCross
= m_x
- theCtrl
->GetSpacing();
620 // according to the drawing code the triangels are drawn
621 // at -4 , -4 from the position up to +10/+10 max
622 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
623 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
624 HasPlus() && theCtrl
->HasButtons() )
626 // 5 is the size of the plus sign
627 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
628 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
629 HasPlus() && theCtrl
->HasButtons() )
632 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
636 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
641 // assuming every image (normal and selected) has the same size!
642 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
643 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
646 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
647 flags
|= wxTREE_HITTEST_ONITEMICON
;
649 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
655 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
656 if (point
.x
> m_x
+m_width
)
657 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
662 // if children are expanded, fall through to evaluate them
663 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
667 size_t count
= m_children
.Count();
668 for ( size_t n
= 0; n
< count
; n
++ )
670 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
678 return (wxGenericTreeItem
*) NULL
;
681 int wxGenericTreeItem::GetCurrentImage() const
683 int image
= NO_IMAGE
;
688 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
691 if ( image
== NO_IMAGE
)
693 // we usually fall back to the normal item, but try just the
694 // expanded one (and not selected) first in this case
695 image
= GetImage(wxTreeItemIcon_Expanded
);
701 image
= GetImage(wxTreeItemIcon_Selected
);
704 // maybe it doesn't have the specific image we want,
705 // try the default one instead
706 if ( image
== NO_IMAGE
) image
= GetImage();
711 // -----------------------------------------------------------------------------
712 // wxGenericTreeCtrl implementation
713 // -----------------------------------------------------------------------------
715 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
717 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
718 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
719 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
720 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
721 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
722 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
723 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
726 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
728 * wxTreeCtrl has to be a real class or we have problems with
729 * the run-time information.
732 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
735 // -----------------------------------------------------------------------------
736 // construction/destruction
737 // -----------------------------------------------------------------------------
739 void wxGenericTreeCtrl::Init()
744 m_select_me
= (wxGenericTreeItem
*) NULL
;
752 m_hilightBrush
= new wxBrush
754 wxSystemSettings::GetColour
756 wxSYS_COLOUR_HIGHLIGHT
761 m_hilightUnfocusedBrush
= new wxBrush
763 wxSystemSettings::GetColour
765 wxSYS_COLOUR_BTNSHADOW
770 m_imageListButtons
= NULL
;
771 m_ownsImageListButtons
= false;
774 m_isDragging
= false;
775 m_dropTarget
= m_oldSelection
= NULL
;
779 m_renameTimer
= NULL
;
784 m_dropEffectAboveItem
= false;
786 m_lastOnSame
= false;
788 #ifdef __WXMAC_CARBON__
789 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
791 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
793 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
794 m_normalFont
.GetFamily(),
795 m_normalFont
.GetStyle(),
797 m_normalFont
.GetUnderlined(),
798 m_normalFont
.GetFaceName(),
799 m_normalFont
.GetEncoding());
802 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
807 const wxValidator
& validator
,
808 const wxString
& name
)
812 wxGetOsVersion( &major
, &minor
);
814 style
&= ~wxTR_LINES_AT_ROOT
;
815 style
|= wxTR_NO_LINES
;
817 style
|= wxTR_ROW_LINES
;
820 if ( !wxControl::Create( parent
, id
, pos
, size
,
821 style
|wxHSCROLL
|wxVSCROLL
,
826 // If the tree display has no buttons, but does have
827 // connecting lines, we can use a narrower layout.
828 // It may not be a good idea to force this...
829 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
835 wxVisualAttributes attr
= GetDefaultAttributes();
836 SetOwnForegroundColour( attr
.colFg
);
837 SetOwnBackgroundColour( attr
.colBg
);
839 SetOwnFont(attr
.font
);
841 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
848 wxGenericTreeCtrl::~wxGenericTreeCtrl()
850 delete m_hilightBrush
;
851 delete m_hilightUnfocusedBrush
;
855 delete m_renameTimer
;
858 if (m_ownsImageListButtons
)
859 delete m_imageListButtons
;
862 // -----------------------------------------------------------------------------
864 // -----------------------------------------------------------------------------
866 size_t wxGenericTreeCtrl::GetCount() const
874 size_t count
= m_anchor
->GetChildrenCount();
875 if ( !HasFlag(wxTR_HIDE_ROOT
) )
877 // take the root itself into account
884 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
886 m_indent
= (unsigned short) indent
;
891 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
892 bool recursively
) const
894 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
896 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
899 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
901 // Do not try to expand the root node if it hasn't been created yet
902 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
904 // if we will hide the root, make sure children are visible
905 m_anchor
->SetHasPlus();
907 CalculatePositions();
910 // right now, just sets the styles. Eventually, we may
911 // want to update the inherited styles, but right now
912 // none of the parents has updatable styles
913 m_windowStyle
= styles
;
917 // -----------------------------------------------------------------------------
918 // functions to work with tree items
919 // -----------------------------------------------------------------------------
921 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
923 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
925 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
928 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
929 wxTreeItemIcon which
) const
931 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
933 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
936 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
938 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
940 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
943 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
945 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
947 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
948 return pItem
->Attr().GetTextColour();
951 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
953 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
955 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
956 return pItem
->Attr().GetBackgroundColour();
959 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
961 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
963 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
964 return pItem
->Attr().GetFont();
967 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
969 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
972 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
973 pItem
->SetText(text
);
974 CalculateSize(pItem
, dc
);
978 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
980 wxTreeItemIcon which
)
982 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
984 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
985 pItem
->SetImage(image
, which
);
988 CalculateSize(pItem
, dc
);
992 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
994 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
999 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1002 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1004 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1006 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1007 pItem
->SetHasPlus(has
);
1011 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1013 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1015 // avoid redrawing the tree if no real change
1016 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1017 if ( pItem
->IsBold() != bold
)
1019 pItem
->SetBold(bold
);
1024 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1027 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1033 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1034 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1037 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1038 pItem
->Attr().SetTextColour(fg
);
1039 pItem
->Attr().SetBackgroundColour(bg
);
1043 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1044 const wxColour
& col
)
1046 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1048 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1049 pItem
->Attr().SetTextColour(col
);
1053 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1054 const wxColour
& col
)
1056 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1058 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1059 pItem
->Attr().SetBackgroundColour(col
);
1063 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1065 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1067 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1068 pItem
->Attr().SetFont(font
);
1072 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1074 wxTreeCtrlBase::SetFont(font
);
1076 m_normalFont
= font
;
1077 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1078 m_normalFont
.GetFamily(),
1079 m_normalFont
.GetStyle(),
1081 m_normalFont
.GetUnderlined(),
1082 m_normalFont
.GetFaceName(),
1083 m_normalFont
.GetEncoding());
1089 // -----------------------------------------------------------------------------
1090 // item status inquiries
1091 // -----------------------------------------------------------------------------
1093 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1095 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1097 // An item is only visible if it's not a descendant of a collapsed item
1098 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1099 wxGenericTreeItem
* parent
= pItem
->GetParent();
1102 if (!parent
->IsExpanded())
1104 parent
= parent
->GetParent();
1108 GetViewStart(& startX
, & startY
);
1110 wxSize clientSize
= GetClientSize();
1113 if (!GetBoundingRect(item
, rect
))
1115 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1117 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1119 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1125 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1127 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1129 // consider that the item does have children if it has the "+" button: it
1130 // might not have them (if it had never been expanded yet) but then it
1131 // could have them as well and it's better to err on this side rather than
1132 // disabling some operations which are restricted to the items with
1133 // children for an item which does have them
1134 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1137 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1139 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1141 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1144 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1146 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1148 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1151 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1153 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1155 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1158 // -----------------------------------------------------------------------------
1160 // -----------------------------------------------------------------------------
1162 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1164 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1166 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1169 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1170 wxTreeItemIdValue
& cookie
) const
1172 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1175 return GetNextChild(item
, cookie
);
1178 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1179 wxTreeItemIdValue
& cookie
) const
1181 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1183 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1185 // it's ok to cast cookie to size_t, we never have indices big enough to
1186 // overflow "void *"
1187 size_t *pIndex
= (size_t *)&cookie
;
1188 if ( *pIndex
< children
.Count() )
1190 return children
.Item((*pIndex
)++);
1194 // there are no more of them
1195 return wxTreeItemId();
1199 #if WXWIN_COMPATIBILITY_2_4
1201 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1204 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1207 return GetNextChild(item
, cookie
);
1210 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1213 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1215 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1216 if ( (size_t)cookie
< children
.Count() )
1218 return children
.Item((size_t)cookie
++);
1222 // there are no more of them
1223 return wxTreeItemId();
1227 #endif // WXWIN_COMPATIBILITY_2_4
1229 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1231 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1233 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1234 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1237 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1239 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1241 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1242 wxGenericTreeItem
*parent
= i
->GetParent();
1243 if ( parent
== NULL
)
1245 // root item doesn't have any siblings
1246 return wxTreeItemId();
1249 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1250 int index
= siblings
.Index(i
);
1251 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1253 size_t n
= (size_t)(index
+ 1);
1254 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1257 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1259 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1261 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1262 wxGenericTreeItem
*parent
= i
->GetParent();
1263 if ( parent
== NULL
)
1265 // root item doesn't have any siblings
1266 return wxTreeItemId();
1269 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1270 int index
= siblings
.Index(i
);
1271 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1273 return index
== 0 ? wxTreeItemId()
1274 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1277 // Only for internal use right now, but should probably be public
1278 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1280 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1282 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1284 // First see if there are any children.
1285 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1286 if (children
.GetCount() > 0)
1288 return children
.Item(0);
1292 // Try a sibling of this or ancestor instead
1293 wxTreeItemId p
= item
;
1294 wxTreeItemId toFind
;
1297 toFind
= GetNextSibling(p
);
1298 p
= GetItemParent(p
);
1299 } while (p
.IsOk() && !toFind
.IsOk());
1304 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1306 wxTreeItemId id
= GetRootItem();
1315 } while (id
.IsOk());
1317 return wxTreeItemId();
1320 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1322 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1324 wxTreeItemId id
= item
;
1327 while (id
= GetNext(id
), id
.IsOk())
1333 return wxTreeItemId();
1336 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1338 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1340 wxFAIL_MSG(wxT("not implemented"));
1342 return wxTreeItemId();
1345 // called by wxTextTreeCtrl when it marks itself for deletion
1346 void wxGenericTreeCtrl::ResetTextControl()
1351 // find the first item starting with the given prefix after the given item
1352 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1353 const wxString
& prefixOrig
) const
1355 // match is case insensitive as this is more convenient to the user: having
1356 // to press Shift-letter to go to the item starting with a capital letter
1357 // would be too bothersome
1358 wxString prefix
= prefixOrig
.Lower();
1360 // determine the starting point: we shouldn't take the current item (this
1361 // allows to switch between two items starting with the same letter just by
1362 // pressing it) but we shouldn't jump to the next one if the user is
1363 // continuing to type as otherwise he might easily skip the item he wanted
1364 wxTreeItemId id
= idParent
;
1365 if ( prefix
.length() == 1 )
1370 // look for the item starting with the given prefix after it
1371 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1376 // if we haven't found anything...
1379 // ... wrap to the beginning
1381 if ( HasFlag(wxTR_HIDE_ROOT
) )
1383 // can't select virtual root
1387 // and try all the items (stop when we get to the one we started from)
1388 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1397 // -----------------------------------------------------------------------------
1399 // -----------------------------------------------------------------------------
1401 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1403 const wxString
& text
,
1406 wxTreeItemData
*data
)
1408 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1411 // should we give a warning here?
1412 return AddRoot(text
, image
, selImage
, data
);
1415 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1417 wxGenericTreeItem
*item
=
1418 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1422 data
->m_pItem
= item
;
1425 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1431 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1434 wxTreeItemData
*data
)
1436 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1438 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1440 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1441 image
, selImage
, data
);
1444 data
->m_pItem
= m_anchor
;
1447 if (HasFlag(wxTR_HIDE_ROOT
))
1449 // if root is hidden, make sure we can navigate
1451 m_anchor
->SetHasPlus();
1453 CalculatePositions();
1456 if (!HasFlag(wxTR_MULTIPLE
))
1458 m_current
= m_key_current
= m_anchor
;
1459 m_current
->SetHilight( true );
1465 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1466 const wxTreeItemId
& idPrevious
,
1467 const wxString
& text
,
1468 int image
, int selImage
,
1469 wxTreeItemData
*data
)
1471 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1474 // should we give a warning here?
1475 return AddRoot(text
, image
, selImage
, data
);
1479 if (idPrevious
.IsOk())
1481 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1482 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1483 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1486 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1490 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1492 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1493 event
.m_item
= item
;
1494 event
.SetEventObject( this );
1495 ProcessEvent( event
);
1498 // Don't leave edit or selection on a child which is about to disappear
1499 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1501 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1502 m_textCtrl
->StopEditing();
1504 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1505 m_key_current
= NULL
;
1507 if (IsDescendantOf(item
, m_select_me
)) {
1510 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1511 m_current
->SetHilight( false );
1517 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1519 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1521 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1522 ChildrenClosing(item
);
1523 item
->DeleteChildren(this);
1526 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1528 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1530 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1532 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1534 // can't delete the item being edited, cancel editing it first
1535 m_textCtrl
->StopEditing();
1538 wxGenericTreeItem
*parent
= item
->GetParent();
1540 // don't keep stale pointers around!
1541 if ( IsDescendantOf(item
, m_key_current
) )
1543 // Don't silently change the selection:
1544 // do it properly in idle time, so event
1545 // handlers get called.
1547 // m_key_current = parent;
1548 m_key_current
= NULL
;
1551 // m_select_me records whether we need to select
1552 // a different item, in idle time.
1553 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1555 m_select_me
= parent
;
1558 if ( IsDescendantOf(item
, m_current
) )
1560 // Don't silently change the selection:
1561 // do it properly in idle time, so event
1562 // handlers get called.
1564 // m_current = parent;
1566 m_select_me
= parent
;
1569 // remove the item from the tree
1572 parent
->GetChildren().Remove( item
); // remove by value
1574 else // deleting the root
1576 // nothing will be left in the tree
1580 // and delete all of its children and the item itself now
1581 item
->DeleteChildren(this);
1582 SendDeleteEvent(item
);
1584 if (item
== m_select_me
)
1590 void wxGenericTreeCtrl::DeleteAllItems()
1598 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1600 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1602 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1603 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1604 _T("can't expand hidden root") );
1606 if ( !item
->HasPlus() )
1609 if ( item
->IsExpanded() )
1612 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1613 event
.m_item
= item
;
1614 event
.SetEventObject( this );
1616 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1618 // cancelled by program
1623 CalculatePositions();
1625 RefreshSubtree(item
);
1627 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1628 ProcessEvent( event
);
1631 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1633 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1636 if ( !IsExpanded(item
) )
1640 wxTreeItemIdValue cookie
;
1641 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1642 while ( child
.IsOk() )
1646 child
= GetNextChild(item
, cookie
);
1650 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1652 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1653 _T("can't collapse hidden root") );
1655 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1657 if ( !item
->IsExpanded() )
1660 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1661 event
.m_item
= item
;
1662 event
.SetEventObject( this );
1663 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1665 // cancelled by program
1669 ChildrenClosing(item
);
1672 #if 0 // TODO why should items be collapsed recursively?
1673 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1674 size_t count
= children
.Count();
1675 for ( size_t n
= 0; n
< count
; n
++ )
1677 Collapse(children
[n
]);
1681 CalculatePositions();
1683 RefreshSubtree(item
);
1685 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1686 ProcessEvent( event
);
1689 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1692 DeleteChildren(item
);
1695 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1697 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1699 if (item
->IsExpanded())
1705 void wxGenericTreeCtrl::Unselect()
1709 m_current
->SetHilight( false );
1710 RefreshLine( m_current
);
1717 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1719 if (item
->IsSelected())
1721 item
->SetHilight(false);
1725 if (item
->HasChildren())
1727 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1728 size_t count
= children
.Count();
1729 for ( size_t n
= 0; n
< count
; ++n
)
1731 UnselectAllChildren(children
[n
]);
1736 void wxGenericTreeCtrl::UnselectAll()
1738 wxTreeItemId rootItem
= GetRootItem();
1740 // the tree might not have the root item at all
1743 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1747 // Recursive function !
1748 // To stop we must have crt_item<last_item
1750 // Tag all next children, when no more children,
1751 // Move to parent (not to tag)
1752 // Keep going... if we found last_item, we stop.
1753 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1755 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1757 if (parent
== NULL
) // This is root item
1758 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1760 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1761 int index
= children
.Index(crt_item
);
1762 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1764 size_t count
= children
.Count();
1765 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1767 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1770 return TagNextChildren(parent
, last_item
, select
);
1773 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1775 crt_item
->SetHilight(select
);
1776 RefreshLine(crt_item
);
1778 if (crt_item
==last_item
)
1781 if (crt_item
->HasChildren())
1783 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1784 size_t count
= children
.Count();
1785 for ( size_t n
= 0; n
< count
; ++n
)
1787 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1795 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1799 // item2 is not necessary after item1
1800 // choice first' and 'last' between item1 and item2
1801 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1802 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1804 bool select
= m_current
->IsSelected();
1806 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1809 TagNextChildren(first
,last
,select
);
1812 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1813 bool unselect_others
,
1814 bool extended_select
)
1816 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1820 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1821 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1823 //wxCHECK_RET( ( (!unselect_others) && is_single),
1824 // wxT("this is a single selection tree") );
1826 // to keep going anyhow !!!
1829 if (item
->IsSelected())
1830 return; // nothing to do
1831 unselect_others
= true;
1832 extended_select
= false;
1834 else if ( unselect_others
&& item
->IsSelected() )
1836 // selection change if there is more than one item currently selected
1837 wxArrayTreeItemIds selected_items
;
1838 if ( GetSelections(selected_items
) == 1 )
1842 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1843 event
.m_item
= item
;
1844 event
.m_itemOld
= m_current
;
1845 event
.SetEventObject( this );
1846 // TODO : Here we don't send any selection mode yet !
1848 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1851 wxTreeItemId parent
= GetItemParent( itemId
);
1852 while (parent
.IsOk())
1854 if (!IsExpanded(parent
))
1857 parent
= GetItemParent( parent
);
1861 if (unselect_others
)
1863 if (is_single
) Unselect(); // to speed up thing
1868 if (extended_select
)
1872 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1875 // don't change the mark (m_current)
1876 SelectItemRange(m_current
, item
);
1880 bool select
= true; // the default
1882 // Check if we need to toggle hilight (ctrl mode)
1883 if (!unselect_others
)
1884 select
=!item
->IsSelected();
1886 m_current
= m_key_current
= item
;
1887 m_current
->SetHilight(select
);
1888 RefreshLine( m_current
);
1891 // This can cause idle processing to select the root
1892 // if no item is selected, so it must be after the
1894 EnsureVisible( itemId
);
1896 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1897 GetEventHandler()->ProcessEvent( event
);
1900 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1904 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1908 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1909 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1911 item
->SetHilight(false);
1916 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1917 wxArrayTreeItemIds
&array
) const
1919 if ( item
->IsSelected() )
1920 array
.Add(wxTreeItemId(item
));
1922 if ( item
->HasChildren() )
1924 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1925 size_t count
= children
.GetCount();
1926 for ( size_t n
= 0; n
< count
; ++n
)
1927 FillArray(children
[n
], array
);
1931 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1934 wxTreeItemId idRoot
= GetRootItem();
1935 if ( idRoot
.IsOk() )
1937 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1939 //else: the tree is empty, so no selections
1941 return array
.Count();
1944 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1946 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1948 if (!item
.IsOk()) return;
1950 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1952 // first expand all parent branches
1953 wxGenericTreeItem
*parent
= gitem
->GetParent();
1955 if ( HasFlag(wxTR_HIDE_ROOT
) )
1957 while ( parent
&& parent
!= m_anchor
)
1960 parent
= parent
->GetParent();
1968 parent
= parent
->GetParent();
1972 //if (parent) CalculatePositions();
1977 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1979 if (!item
.IsOk()) return;
1981 // We have to call this here because the label in
1982 // question might just have been added and no screen
1983 // update taken place.
1985 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1990 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1992 // now scroll to the item
1993 int item_y
= gitem
->GetY();
1997 GetViewStart( &start_x
, &start_y
);
1998 start_y
*= PIXELS_PER_UNIT
;
2002 GetClientSize( &client_w
, &client_h
);
2004 if (item_y
< start_y
+3)
2009 m_anchor
->GetSize( x
, y
, this );
2010 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2011 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2012 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2013 // Item should appear at top
2014 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2016 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2021 m_anchor
->GetSize( x
, y
, this );
2022 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2023 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2024 item_y
+= PIXELS_PER_UNIT
+2;
2025 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2026 // Item should appear at bottom
2027 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
);
2031 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2032 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2034 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2035 wxGenericTreeItem
**item2
)
2037 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2039 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2042 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2044 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2046 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2048 wxCHECK_RET( !s_treeBeingSorted
,
2049 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2051 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2052 if ( children
.Count() > 1 )
2056 s_treeBeingSorted
= this;
2057 children
.Sort(tree_ctrl_compare_func
);
2058 s_treeBeingSorted
= NULL
;
2060 //else: don't make the tree dirty as nothing changed
2063 void wxGenericTreeCtrl::CalculateLineHeight()
2065 wxClientDC
dc(this);
2066 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2068 if ( m_imageListNormal
)
2070 // Calculate a m_lineHeight value from the normal Image sizes.
2071 // May be toggle off. Then wxGenericTreeCtrl will spread when
2072 // necessary (which might look ugly).
2073 int n
= m_imageListNormal
->GetImageCount();
2074 for (int i
= 0; i
< n
; i
++)
2076 int width
= 0, height
= 0;
2077 m_imageListNormal
->GetSize(i
, width
, height
);
2078 if (height
> m_lineHeight
) m_lineHeight
= height
;
2082 if (m_imageListButtons
)
2084 // Calculate a m_lineHeight value from the Button image sizes.
2085 // May be toggle off. Then wxGenericTreeCtrl will spread when
2086 // necessary (which might look ugly).
2087 int n
= m_imageListButtons
->GetImageCount();
2088 for (int i
= 0; i
< n
; i
++)
2090 int width
= 0, height
= 0;
2091 m_imageListButtons
->GetSize(i
, width
, height
);
2092 if (height
> m_lineHeight
) m_lineHeight
= height
;
2096 if (m_lineHeight
< 30)
2097 m_lineHeight
+= 2; // at least 2 pixels
2099 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2102 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2104 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2105 m_imageListNormal
= imageList
;
2106 m_ownsImageListNormal
= false;
2108 // Don't do any drawing if we're setting the list to NULL,
2109 // since we may be in the process of deleting the tree control.
2111 CalculateLineHeight();
2114 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2116 if (m_ownsImageListState
) delete m_imageListState
;
2117 m_imageListState
= imageList
;
2118 m_ownsImageListState
= false;
2121 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2123 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2124 m_imageListButtons
= imageList
;
2125 m_ownsImageListButtons
= false;
2127 CalculateLineHeight();
2130 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2132 SetButtonsImageList(imageList
);
2133 m_ownsImageListButtons
= true;
2136 // -----------------------------------------------------------------------------
2138 // -----------------------------------------------------------------------------
2140 void wxGenericTreeCtrl::AdjustMyScrollbars()
2145 m_anchor
->GetSize( x
, y
, this );
2146 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2147 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2148 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2149 int y_pos
= GetScrollPos( wxVERTICAL
);
2150 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2154 SetScrollbars( 0, 0, 0, 0 );
2158 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2160 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2161 return item
->GetHeight();
2163 return m_lineHeight
;
2166 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2168 // TODO implement "state" icon on items
2170 wxTreeItemAttr
*attr
= item
->GetAttributes();
2171 if ( attr
&& attr
->HasFont() )
2172 dc
.SetFont(attr
->GetFont());
2173 else if (item
->IsBold())
2174 dc
.SetFont(m_boldFont
);
2176 long text_w
= 0, text_h
= 0;
2177 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2179 int image_h
= 0, image_w
= 0;
2180 int image
= item
->GetCurrentImage();
2181 if ( image
!= NO_IMAGE
)
2183 if ( m_imageListNormal
)
2185 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2186 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2194 int total_h
= GetLineHeight(item
);
2195 bool drawItemBackground
= false;
2197 if ( item
->IsSelected() )
2199 // under mac selections are only a rectangle in case they don't have the focus
2203 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2204 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2208 dc
.SetBrush( *m_hilightBrush
) ;
2211 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2213 drawItemBackground
= true;
2218 if ( attr
&& attr
->HasBackgroundColour() )
2220 drawItemBackground
= true;
2221 colBg
= attr
->GetBackgroundColour();
2225 colBg
= GetBackgroundColour();
2227 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2230 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2232 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2236 DoGetPosition(&x
, &y
);
2238 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2242 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2244 // If it's selected, and there's an image, then we should
2245 // take care to leave the area under the image painted in the
2246 // background colour.
2247 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2248 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2250 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2251 // don't allow backgrounds to be customized. Not drawing the background,
2252 // except for custom item backgrounds, works for both kinds of theme.
2253 else if (drawItemBackground
)
2255 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2256 item
->GetWidth()+2, total_h
-offset
);
2260 if ( image
!= NO_IMAGE
)
2262 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2263 m_imageListNormal
->Draw( image
, dc
,
2265 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2266 wxIMAGELIST_DRAW_TRANSPARENT
);
2267 dc
.DestroyClippingRegion();
2270 dc
.SetBackgroundMode(wxTRANSPARENT
);
2271 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2272 dc
.DrawText( item
->GetText(),
2273 (wxCoord
)(image_w
+ item
->GetX()),
2274 (wxCoord
)(item
->GetY() + extraH
));
2276 // restore normal font
2277 dc
.SetFont( m_normalFont
);
2280 // Now y stands for the top of the item, whereas it used to stand for middle !
2281 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2283 int x
= level
*m_indent
;
2284 if (!HasFlag(wxTR_HIDE_ROOT
))
2288 else if (level
== 0)
2290 // always expand hidden root
2292 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2293 int count
= children
.Count();
2299 PaintLevel(children
[n
], dc
, 1, y
);
2300 } while (++n
< count
);
2302 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2304 // draw line down to last child
2305 origY
+= GetLineHeight(children
[0])>>1;
2306 oldY
+= GetLineHeight(children
[n
-1])>>1;
2307 dc
.DrawLine(3, origY
, 3, oldY
);
2313 item
->SetX(x
+m_spacing
);
2316 int h
= GetLineHeight(item
);
2318 int y_mid
= y_top
+ (h
>>1);
2321 int exposed_x
= dc
.LogicalToDeviceX(0);
2322 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2324 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2328 // don't draw rect outline if we already have the
2329 // background color under Mac
2330 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2331 #endif // !__WXMAC__
2335 if ( item
->IsSelected()
2337 // On wxMac, if the tree doesn't have the focus we draw an empty
2338 // rectangle, so we want to make sure that the text is visible
2339 // against the normal background, not the highlightbackground, so
2340 // don't use the highlight text colour unless we have the focus.
2345 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2349 wxTreeItemAttr
*attr
= item
->GetAttributes();
2350 if (attr
&& attr
->HasTextColour())
2351 colText
= attr
->GetTextColour();
2353 colText
= GetForegroundColour();
2357 dc
.SetTextForeground(colText
);
2361 PaintItem(item
, dc
);
2363 if (HasFlag(wxTR_ROW_LINES
))
2365 // if the background colour is white, choose a
2366 // contrasting color for the lines
2367 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2368 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2369 dc
.DrawLine(0, y_top
, 10000, y_top
);
2370 dc
.DrawLine(0, y
, 10000, y
);
2373 // restore DC objects
2374 dc
.SetBrush(*wxWHITE_BRUSH
);
2375 dc
.SetPen(m_dottedPen
);
2376 dc
.SetTextForeground(*wxBLACK
);
2378 if ( !HasFlag(wxTR_NO_LINES
) )
2380 // draw the horizontal line here
2382 if (x
> (signed)m_indent
)
2383 x_start
-= m_indent
;
2384 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2386 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2389 // should the item show a button?
2390 if ( item
->HasPlus() && HasButtons() )
2392 if ( m_imageListButtons
)
2394 // draw the image button here
2397 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2398 : wxTreeItemIcon_Normal
;
2399 if ( item
->IsSelected() )
2400 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2402 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2403 int xx
= x
- image_w
/2;
2404 int yy
= y_mid
- image_h
/2;
2406 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2407 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2408 wxIMAGELIST_DRAW_TRANSPARENT
);
2410 else // no custom buttons
2412 static const int wImage
= 9;
2413 static const int hImage
= 9;
2416 if (item
->IsExpanded())
2417 flag
|= wxCONTROL_EXPANDED
;
2418 if (item
== m_underMouse
)
2419 flag
|= wxCONTROL_CURRENT
;
2421 wxRendererNative::Get().DrawTreeItemButton
2425 wxRect(x
- wImage
/2,
2434 if (item
->IsExpanded())
2436 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2437 int count
= children
.Count();
2444 PaintLevel(children
[n
], dc
, level
, y
);
2445 } while (++n
< count
);
2447 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2449 // draw line down to last child
2450 oldY
+= GetLineHeight(children
[n
-1])>>1;
2451 if (HasButtons()) y_mid
+= 5;
2453 // Only draw the portion of the line that is visible, in case it is huge
2454 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2455 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2456 yOrigin
= abs(yOrigin
);
2457 GetClientSize(&width
, &height
);
2459 // Move end points to the begining/end of the view?
2460 if (y_mid
< yOrigin
)
2462 if (oldY
> yOrigin
+ height
)
2463 oldY
= yOrigin
+ height
;
2465 // after the adjustments if y_mid is larger than oldY then the line
2466 // isn't visible at all so don't draw anything
2468 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2474 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2478 if ( item
->HasPlus() )
2480 // it's a folder, indicate it by a border
2485 // draw a line under the drop target because the item will be
2487 DrawLine(item
, !m_dropEffectAboveItem
);
2490 SetCursor(wxCURSOR_BULLSEYE
);
2495 SetCursor(wxCURSOR_NO_ENTRY
);
2499 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2501 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2503 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2505 wxClientDC
dc(this);
2507 dc
.SetLogicalFunction(wxINVERT
);
2508 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2510 int w
= i
->GetWidth() + 2;
2511 int h
= GetLineHeight(i
) + 2;
2513 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2516 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2518 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2520 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2522 wxClientDC
dc(this);
2524 dc
.SetLogicalFunction(wxINVERT
);
2530 y
+= GetLineHeight(i
) - 1;
2533 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2536 // -----------------------------------------------------------------------------
2537 // wxWidgets callbacks
2538 // -----------------------------------------------------------------------------
2540 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2548 dc
.SetFont( m_normalFont
);
2549 dc
.SetPen( m_dottedPen
);
2551 // this is now done dynamically
2552 //if(GetImageList() == NULL)
2553 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2556 PaintLevel( m_anchor
, dc
, 0, y
);
2559 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2568 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2577 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2579 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2580 te
.m_evtKey
= event
;
2581 te
.SetEventObject( this );
2582 if ( GetEventHandler()->ProcessEvent( te
) )
2584 // intercepted by the user code
2588 if ( (m_current
== 0) || (m_key_current
== 0) )
2594 // how should the selection work for this event?
2595 bool is_multiple
, extended_select
, unselect_others
;
2596 EventFlagsToSelType(GetWindowStyleFlag(),
2599 is_multiple
, extended_select
, unselect_others
);
2603 // * : Expand all/Collapse all
2604 // ' ' | return : activate
2605 // up : go up (not last children!)
2607 // left : go to parent
2608 // right : open if parent and go next
2609 // home : go to root
2610 // end : go to last item without opening parents
2611 // alnum : start or continue searching for the item with this prefix
2612 int keyCode
= event
.GetKeyCode();
2617 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2625 if ( !IsExpanded(m_current
) )
2628 ExpandAll(m_current
);
2631 //else: fall through to Collapse() it
2635 if (IsExpanded(m_current
))
2637 Collapse(m_current
);
2643 // Use the item's bounding rectangle to determine position for the event
2645 GetBoundingRect(m_current
, ItemRect
, true);
2647 wxTreeEvent
eventMenu( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2648 eventMenu
.m_item
= m_current
;
2649 // Use the left edge, vertical middle
2650 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2651 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2652 eventMenu
.SetEventObject( this );
2653 GetEventHandler()->ProcessEvent( eventMenu
);
2658 if ( !event
.HasModifiers() )
2660 wxTreeEvent
eventAct( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2661 eventAct
.m_item
= m_current
;
2662 eventAct
.SetEventObject( this );
2663 GetEventHandler()->ProcessEvent( eventAct
);
2666 // in any case, also generate the normal key event for this key,
2667 // even if we generated the ACTIVATED event above: this is what
2668 // wxMSW does and it makes sense because you might not want to
2669 // process ACTIVATED event at all and handle Space and Return
2670 // directly (and differently) which would be impossible otherwise
2674 // up goes to the previous sibling or to the last
2675 // of its children if it's expanded
2678 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2681 prev
= GetItemParent( m_key_current
);
2682 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2684 break; // don't go to root if it is hidden
2688 wxTreeItemIdValue cookie
;
2689 wxTreeItemId current
= m_key_current
;
2690 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2691 if (current
== GetFirstChild( prev
, cookie
))
2693 // otherwise we return to where we came from
2694 DoSelectItem( prev
, unselect_others
, extended_select
);
2695 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2702 while ( IsExpanded(prev
) && HasChildren(prev
) )
2704 wxTreeItemId child
= GetLastChild(prev
);
2711 DoSelectItem( prev
, unselect_others
, extended_select
);
2712 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2717 // left arrow goes to the parent
2720 wxTreeItemId prev
= GetItemParent( m_current
);
2721 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2723 // don't go to root if it is hidden
2724 prev
= GetPrevSibling( m_current
);
2728 DoSelectItem( prev
, unselect_others
, extended_select
);
2734 // this works the same as the down arrow except that we
2735 // also expand the item if it wasn't expanded yet
2741 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2743 wxTreeItemIdValue cookie
;
2744 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2745 DoSelectItem( child
, unselect_others
, extended_select
);
2746 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2750 wxTreeItemId next
= GetNextSibling( m_key_current
);
2753 wxTreeItemId current
= m_key_current
;
2754 while (current
.IsOk() && !next
)
2756 current
= GetItemParent( current
);
2757 if (current
) next
= GetNextSibling( current
);
2762 DoSelectItem( next
, unselect_others
, extended_select
);
2763 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2769 // <End> selects the last visible tree item
2772 wxTreeItemId last
= GetRootItem();
2774 while ( last
.IsOk() && IsExpanded(last
) )
2776 wxTreeItemId lastChild
= GetLastChild(last
);
2778 // it may happen if the item was expanded but then all of
2779 // its children have been deleted - so IsExpanded() returned
2780 // true, but GetLastChild() returned invalid item
2789 DoSelectItem( last
, unselect_others
, extended_select
);
2794 // <Home> selects the root item
2797 wxTreeItemId prev
= GetRootItem();
2801 if ( HasFlag(wxTR_HIDE_ROOT
) )
2803 wxTreeItemIdValue cookie
;
2804 prev
= GetFirstChild(prev
, cookie
);
2809 DoSelectItem( prev
, unselect_others
, extended_select
);
2814 // do not use wxIsalnum() here
2815 if ( !event
.HasModifiers() &&
2816 ((keyCode
>= '0' && keyCode
<= '9') ||
2817 (keyCode
>= 'a' && keyCode
<= 'z') ||
2818 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2820 // find the next item starting with the given prefix
2821 wxChar ch
= (wxChar
)keyCode
;
2823 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2834 // also start the timer to reset the current prefix if the user
2835 // doesn't press any more alnum keys soon -- we wouldn't want
2836 // to use this prefix for a new item search
2839 m_findTimer
= new wxTreeFindTimer(this);
2842 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2851 wxTreeItemId
wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
)
2853 // JACS: removed wxYieldIfNeeded() because it can cause the window
2854 // to be deleted from under us if a close window event is pending
2859 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2860 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2861 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2862 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2863 if (flags
) return wxTreeItemId();
2865 if (m_anchor
== NULL
)
2867 flags
= wxTREE_HITTEST_NOWHERE
;
2868 return wxTreeItemId();
2871 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2875 flags
= wxTREE_HITTEST_NOWHERE
;
2876 return wxTreeItemId();
2881 // get the bounding rectangle of the item (or of its label only)
2882 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2884 bool textOnly
) const
2886 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2888 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2891 GetViewStart(& startX
, & startY
);
2895 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
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
;
2911 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2912 rect
.height
= GetLineHeight(i
);
2917 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2918 wxClassInfo
* WXUNUSED(textCtrlClass
))
2920 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
2922 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2924 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2925 te
.m_item
= itemEdit
;
2926 te
.SetEventObject( this );
2927 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2933 // We have to call this here because the label in
2934 // question might just have been added and no screen
2935 // update taken place.
2937 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2943 // TODO: use textCtrlClass here to create the control of correct class
2944 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2946 m_textCtrl
->SetFocus();
2951 // returns a pointer to the text edit control if the item is being
2952 // edited, NULL otherwise (it's assumed that no more than one item may
2953 // be edited simultaneously)
2954 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2959 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
2960 bool discardChanges
)
2962 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
2964 m_textCtrl
->EndEdit(discardChanges
);
2967 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2968 const wxString
& value
)
2970 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2972 le
.SetEventObject( this );
2974 le
.m_editCancelled
= false;
2976 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2979 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2981 // let owner know that the edit was cancelled
2982 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2984 le
.SetEventObject( this );
2985 le
.m_label
= wxEmptyString
;
2986 le
.m_editCancelled
= true;
2988 GetEventHandler()->ProcessEvent( le
);
2991 void wxGenericTreeCtrl::OnRenameTimer()
2993 EditLabel( m_current
);
2996 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2998 if ( !m_anchor
) return;
3000 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3002 // Is the mouse over a tree item button?
3004 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3005 wxGenericTreeItem
*underMouse
= thisItem
;
3007 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3008 #endif // wxUSE_TOOLTIPS
3011 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3012 (!event
.LeftIsDown()) &&
3014 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3022 if (underMouse
!= m_underMouse
)
3026 // unhighlight old item
3027 wxGenericTreeItem
*tmp
= m_underMouse
;
3028 m_underMouse
= NULL
;
3032 m_underMouse
= underMouse
;
3034 RefreshLine( m_underMouse
);
3038 // Determines what item we are hovering over and need a tooltip for
3039 wxTreeItemId hoverItem
= thisItem
;
3041 // We do not want a tooltip if we are dragging, or if the rename timer is running
3042 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3044 // Ask the tree control what tooltip (if any) should be shown
3045 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
3046 hevent
.m_item
= hoverItem
;
3047 hevent
.SetEventObject(this);
3049 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3051 SetToolTip(hevent
.m_label
);
3056 // we process left mouse up event (enables in-place edit), right down
3057 // (pass to the user code), left dbl click (activate item) and
3058 // dragging/moving events for items drag-and-drop
3059 if ( !(event
.LeftDown() ||
3061 event
.RightDown() ||
3062 event
.LeftDClick() ||
3064 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3073 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3075 if ( event
.Dragging() && !m_isDragging
)
3077 if (m_dragCount
== 0)
3082 if (m_dragCount
!= 3)
3084 // wait until user drags a bit further...
3088 wxEventType command
= event
.RightIsDown()
3089 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3090 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3092 wxTreeEvent
nevent( command
, GetId() );
3093 nevent
.m_item
= m_current
;
3094 nevent
.SetEventObject(this);
3095 nevent
.SetPoint(CalcScrolledPosition(pt
));
3097 // by default the dragging is not supported, the user code must
3098 // explicitly allow the event for it to take place
3101 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3103 // we're going to drag this item
3104 m_isDragging
= true;
3106 // remember the old cursor because we will change it while
3108 m_oldCursor
= m_cursor
;
3110 // in a single selection control, hide the selection temporarily
3111 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3113 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3115 if ( m_oldSelection
)
3117 m_oldSelection
->SetHilight(false);
3118 RefreshLine(m_oldSelection
);
3125 else if ( event
.Dragging() )
3127 if ( item
!= m_dropTarget
)
3129 // unhighlight the previous drop target
3130 DrawDropEffect(m_dropTarget
);
3132 m_dropTarget
= item
;
3134 // highlight the current drop target if any
3135 DrawDropEffect(m_dropTarget
);
3137 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3144 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3148 // erase the highlighting
3149 DrawDropEffect(m_dropTarget
);
3151 if ( m_oldSelection
)
3153 m_oldSelection
->SetHilight(true);
3154 RefreshLine(m_oldSelection
);
3155 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3158 // generate the drag end event
3159 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3161 eventEndDrag
.m_item
= item
;
3162 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3163 eventEndDrag
.SetEventObject(this);
3165 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3167 m_isDragging
= false;
3168 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3170 SetCursor(m_oldCursor
);
3172 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3180 // If we got to this point, we are not dragging or moving the mouse.
3181 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3182 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3183 // We skip even if we didn't hit an item because we still should
3184 // restore focus to the tree control even if we didn't exactly hit an item.
3185 if ( event
.LeftDown() )
3190 // here we process only the messages which happen on tree items
3194 if (item
== NULL
) return; /* we hit the blank area */
3196 if ( event
.RightDown() )
3198 // If the item is already selected, do not update the selection.
3199 // Multi-selections should not be cleared if a selected item is clicked.
3200 if (!IsSelected(item
))
3202 DoSelectItem(item
, true, false);
3205 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3206 nevent
.m_item
= item
;
3207 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3208 nevent
.SetEventObject(this);
3209 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3211 // Consistent with MSW (for now), send the ITEM_MENU *after*
3212 // the RIGHT_CLICK event. TODO: This behavior may change.
3213 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, GetId());
3214 nevent2
.m_item
= item
;
3215 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3216 nevent2
.SetEventObject(this);
3217 GetEventHandler()->ProcessEvent(nevent2
);
3219 else if ( event
.LeftUp() )
3221 // this facilitates multiple-item drag-and-drop
3223 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3225 wxArrayTreeItemIds selections
;
3226 size_t count
= GetSelections(selections
);
3232 DoSelectItem(item
, true, false);
3238 if ( (item
== m_current
) &&
3239 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3240 HasFlag(wxTR_EDIT_LABELS
) )
3242 if ( m_renameTimer
)
3244 if ( m_renameTimer
->IsRunning() )
3245 m_renameTimer
->Stop();
3249 m_renameTimer
= new wxTreeRenameTimer( this );
3252 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3255 m_lastOnSame
= false;
3258 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3260 if ( event
.LeftDown() )
3262 m_lastOnSame
= item
== m_current
;
3265 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3267 // only toggle the item for a single click, double click on
3268 // the button doesn't do anything (it toggles the item twice)
3269 if ( event
.LeftDown() )
3274 // don't select the item if the button was clicked
3279 // clear the previously selected items, if the
3280 // user clicked outside of the present selection.
3281 // otherwise, perform the deselection on mouse-up.
3282 // this allows multiple drag and drop to work.
3283 // but if Cmd is down, toggle selection of the clicked item
3284 if (!IsSelected(item
) || event
.CmdDown())
3286 // how should the selection work for this event?
3287 bool is_multiple
, extended_select
, unselect_others
;
3288 EventFlagsToSelType(GetWindowStyleFlag(),
3291 is_multiple
, extended_select
, unselect_others
);
3293 DoSelectItem(item
, unselect_others
, extended_select
);
3297 // For some reason, Windows isn't recognizing a left double-click,
3298 // so we need to simulate it here. Allow 200 milliseconds for now.
3299 if ( event
.LeftDClick() )
3301 // double clicking should not start editing the item label
3302 if ( m_renameTimer
)
3303 m_renameTimer
->Stop();
3305 m_lastOnSame
= false;
3307 // send activate event first
3308 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3309 nevent
.m_item
= item
;
3310 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3311 nevent
.SetEventObject( this );
3312 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3314 // if the user code didn't process the activate event,
3315 // handle it ourselves by toggling the item when it is
3317 if ( item
->HasPlus() )
3327 void wxGenericTreeCtrl::OnInternalIdle()
3329 wxWindow::OnInternalIdle();
3331 // Check if we need to select the root item
3332 // because nothing else has been selected.
3333 // Delaying it means that we can invoke event handlers
3334 // as required, when a first item is selected.
3335 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3338 SelectItem(m_select_me
);
3339 else if (GetRootItem().IsOk())
3340 SelectItem(GetRootItem());
3343 /* after all changes have been done to the tree control,
3344 * we actually redraw the tree when everything is over */
3346 if (!m_dirty
) return;
3347 if (m_freezeCount
) return;
3351 CalculatePositions();
3353 AdjustMyScrollbars();
3356 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3361 wxTreeItemAttr
*attr
= item
->GetAttributes();
3362 if ( attr
&& attr
->HasFont() )
3363 dc
.SetFont(attr
->GetFont());
3364 else if ( item
->IsBold() )
3365 dc
.SetFont(m_boldFont
);
3367 dc
.SetFont(m_normalFont
);
3369 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3372 // restore normal font
3373 dc
.SetFont( m_normalFont
);
3377 int image
= item
->GetCurrentImage();
3378 if ( image
!= NO_IMAGE
)
3380 if ( m_imageListNormal
)
3382 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3383 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3387 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3390 total_h
+= 2; // at least 2 pixels
3392 total_h
+= total_h
/10; // otherwise 10% extra spacing
3394 item
->SetHeight(total_h
);
3395 if (total_h
>m_lineHeight
)
3396 m_lineHeight
=total_h
;
3398 item
->SetWidth(image_w
+text_w
+2);
3401 // -----------------------------------------------------------------------------
3402 // for developper : y is now the top of the level
3403 // not the middle of it !
3404 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3406 int x
= level
*m_indent
;
3407 if (!HasFlag(wxTR_HIDE_ROOT
))
3411 else if (level
== 0)
3413 // a hidden root is not evaluated, but its
3414 // children are always calculated
3418 CalculateSize( item
, dc
);
3421 item
->SetX( x
+m_spacing
);
3423 y
+= GetLineHeight(item
);
3425 if ( !item
->IsExpanded() )
3427 // we don't need to calculate collapsed branches
3432 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3433 size_t n
, count
= children
.Count();
3435 for (n
= 0; n
< count
; ++n
)
3436 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3439 void wxGenericTreeCtrl::CalculatePositions()
3441 if ( !m_anchor
) return;
3443 wxClientDC
dc(this);
3446 dc
.SetFont( m_normalFont
);
3448 dc
.SetPen( m_dottedPen
);
3449 //if(GetImageList() == NULL)
3450 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3453 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3456 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3458 if (m_dirty
) return;
3459 if (m_freezeCount
) return;
3461 wxSize client
= GetClientSize();
3464 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3465 rect
.width
= client
.x
;
3466 rect
.height
= client
.y
;
3468 Refresh(true, &rect
);
3470 AdjustMyScrollbars();
3473 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3475 if (m_dirty
) return;
3476 if (m_freezeCount
) return;
3479 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3480 rect
.width
= GetClientSize().x
;
3481 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3483 Refresh(true, &rect
);
3486 void wxGenericTreeCtrl::RefreshSelected()
3488 if (m_freezeCount
) return;
3490 // TODO: this is awfully inefficient, we should keep the list of all
3491 // selected items internally, should be much faster
3493 RefreshSelectedUnder(m_anchor
);
3496 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3498 if (m_freezeCount
) return;
3500 if ( item
->IsSelected() )
3503 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3504 size_t count
= children
.GetCount();
3505 for ( size_t n
= 0; n
< count
; n
++ )
3507 RefreshSelectedUnder(children
[n
]);
3511 void wxGenericTreeCtrl::Freeze()
3516 void wxGenericTreeCtrl::Thaw()
3518 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3520 if ( --m_freezeCount
== 0 )
3526 // ----------------------------------------------------------------------------
3527 // changing colours: we need to refresh the tree control
3528 // ----------------------------------------------------------------------------
3530 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3532 if ( !wxWindow::SetBackgroundColour(colour
) )
3535 if (m_freezeCount
) return true;
3542 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3544 if ( !wxWindow::SetForegroundColour(colour
) )
3547 if (m_freezeCount
) return true;
3554 // Process the tooltip event, to speed up event processing.
3555 // Doesn't actually get a tooltip.
3556 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3562 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3563 // be removed, as well as the #else case below.
3564 #define _USE_VISATTR 0
3567 #include "wx/listbox.h"
3573 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3575 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3579 // Use the same color scheme as wxListBox
3580 return wxListBox::GetClassDefaultAttributes(variant
);
3582 wxVisualAttributes attr
;
3583 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3584 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3585 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3590 #if WXWIN_COMPATIBILITY_2_4
3592 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3594 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3597 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3599 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3602 #endif // WXWIN_COMPATIBILITY_2_4
3604 #endif // wxUSE_TREECTRL