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 // -----------------------------------------------------------------------------
62 // -----------------------------------------------------------------------------
64 // timer used for enabling in-place edit
65 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
68 // start editing the current item after half a second (if the mouse hasn't
69 // been clicked/moved)
72 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
74 virtual void Notify();
77 wxGenericTreeCtrl
*m_owner
;
79 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
82 // control used for in-place edit
83 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
86 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
88 void EndEdit(bool discardChanges
= false)
96 m_aboutToFinish
= true;
98 // Notify the owner about the changes
101 // Even if vetoed, close the control (consistent with MSW)
109 m_owner
->OnRenameCancelled(m_itemEdited
);
111 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
114 void OnChar( wxKeyEvent
&event
);
115 void OnKeyUp( wxKeyEvent
&event
);
116 void OnKillFocus( wxFocusEvent
&event
);
118 bool AcceptChanges();
122 wxGenericTreeCtrl
*m_owner
;
123 wxGenericTreeItem
*m_itemEdited
;
124 wxString m_startValue
;
126 bool m_aboutToFinish
;
128 DECLARE_EVENT_TABLE()
129 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
132 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
133 // for a sufficiently long time
134 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
137 // reset the current prefix after half a second of inactivity
138 enum { DELAY
= 500 };
140 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
142 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
145 wxGenericTreeCtrl
*m_owner
;
147 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
151 class WXDLLEXPORT wxGenericTreeItem
155 wxGenericTreeItem() { m_data
= NULL
; }
156 wxGenericTreeItem( wxGenericTreeItem
*parent
,
157 const wxString
& text
,
160 wxTreeItemData
*data
);
162 ~wxGenericTreeItem();
165 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
167 const wxString
& GetText() const { return m_text
; }
168 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
169 { return m_images
[which
]; }
170 wxTreeItemData
*GetData() const { return m_data
; }
172 // returns the current image for the item (depending on its
173 // selected/expanded/whatever state)
174 int GetCurrentImage() const;
176 void SetText( const wxString
&text
);
177 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
178 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
180 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
182 void SetBold(bool bold
) { m_isBold
= bold
; }
184 int GetX() const { return m_x
; }
185 int GetY() const { return m_y
; }
187 void SetX(int x
) { m_x
= x
; }
188 void SetY(int y
) { m_y
= y
; }
190 int GetHeight() const { return m_height
; }
191 int GetWidth() const { return m_width
; }
193 void SetHeight(int h
) { m_height
= h
; }
194 void SetWidth(int w
) { m_width
= w
; }
196 wxGenericTreeItem
*GetParent() const { return m_parent
; }
199 // deletes all children notifying the treectrl about it if !NULL
201 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
203 // get count of all children (and grand children if 'recursively')
204 size_t GetChildrenCount(bool recursively
= true) const;
206 void Insert(wxGenericTreeItem
*child
, size_t index
)
207 { m_children
.Insert(child
, index
); }
209 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
211 // return the item at given position (or NULL if no item), onButton is
212 // true if the point belongs to the item's button, otherwise it lies
213 // on the item's label
214 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
215 const wxGenericTreeCtrl
*,
219 void Expand() { m_isCollapsed
= false; }
220 void Collapse() { m_isCollapsed
= true; }
222 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
225 bool HasChildren() const { return !m_children
.IsEmpty(); }
226 bool IsSelected() const { return m_hasHilight
!= 0; }
227 bool IsExpanded() const { return !m_isCollapsed
; }
228 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
229 bool IsBold() const { return m_isBold
!= 0; }
232 // get them - may be NULL
233 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
234 // get them ensuring that the pointer is not NULL
235 wxTreeItemAttr
& Attr()
239 m_attr
= new wxTreeItemAttr
;
245 void SetAttributes(wxTreeItemAttr
*attr
)
247 if ( m_ownsAttr
) delete m_attr
;
251 // set them and delete when done
252 void AssignAttributes(wxTreeItemAttr
*attr
)
259 // since there can be very many of these, we save size by chosing
260 // the smallest representation for the elements and by ordering
261 // the members to avoid padding.
262 wxString m_text
; // label to be rendered for item
264 wxTreeItemData
*m_data
; // user-provided data
266 wxArrayGenericTreeItems m_children
; // list of children
267 wxGenericTreeItem
*m_parent
; // parent of this item
269 wxTreeItemAttr
*m_attr
; // attributes???
271 // tree ctrl images for the normal, selected, expanded and
272 // expanded+selected states
273 int m_images
[wxTreeItemIcon_Max
];
275 wxCoord m_x
; // (virtual) offset from top
276 wxCoord m_y
; // (virtual) offset from left
277 int m_width
; // width of this item
278 int m_height
; // height of this item
280 // use bitfields to save size
281 unsigned int m_isCollapsed
:1;
282 unsigned int m_hasHilight
:1; // same as focused
283 unsigned int m_hasPlus
:1; // used for item which doesn't have
284 // children but has a [+] button
285 unsigned int m_isBold
:1; // render the label in bold font
286 unsigned int m_ownsAttr
:1; // delete attribute when done
288 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
291 // =============================================================================
293 // =============================================================================
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 // translate the key or mouse event flags to the type of selection we're
301 static void EventFlagsToSelType(long style
,
305 bool &extended_select
,
306 bool &unselect_others
)
308 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
309 extended_select
= shiftDown
&& is_multiple
;
310 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
313 // check if the given item is under another one
314 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
318 if ( item
== parent
)
320 // item is a descendant of parent
324 item
= item
->GetParent();
330 // -----------------------------------------------------------------------------
331 // wxTreeRenameTimer (internal)
332 // -----------------------------------------------------------------------------
334 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
339 void wxTreeRenameTimer::Notify()
341 m_owner
->OnRenameTimer();
344 //-----------------------------------------------------------------------------
345 // wxTreeTextCtrl (internal)
346 //-----------------------------------------------------------------------------
348 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
349 EVT_CHAR (wxTreeTextCtrl::OnChar
)
350 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
351 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
354 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
355 wxGenericTreeItem
*item
)
356 : m_itemEdited(item
), m_startValue(item
->GetText())
360 m_aboutToFinish
= false;
362 int w
= m_itemEdited
->GetWidth(),
363 h
= m_itemEdited
->GetHeight();
366 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
371 int image
= item
->GetCurrentImage();
372 if ( image
!= NO_IMAGE
)
374 if ( m_owner
->m_imageListNormal
)
376 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
381 wxFAIL_MSG(_T("you must create an image list to use images!"));
385 // FIXME: what are all these hardcoded 4, 8 and 11s really?
389 wxSize bs
= DoGetBestSize() ;
390 // edit control height
393 int diff
= h
- ( bs
.y
- 8 ) ;
399 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
400 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
403 bool wxTreeTextCtrl::AcceptChanges()
405 const wxString value
= GetValue();
407 if ( value
== m_startValue
)
409 // nothing changed, always accept
410 // when an item remains unchanged, the owner
411 // needs to be notified that the user decided
412 // not to change the tree item label, and that
413 // the edit has been cancelled
415 m_owner
->OnRenameCancelled(m_itemEdited
);
419 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
421 // vetoed by the user
425 // accepted, do rename the item
426 m_owner
->SetItemText(m_itemEdited
, value
);
431 void wxTreeTextCtrl::Finish()
435 m_owner
->ResetTextControl();
437 wxPendingDelete
.Append(this);
445 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
447 switch ( event
.m_keyCode
)
462 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
466 // auto-grow the textctrl:
467 wxSize parentSize
= m_owner
->GetSize();
468 wxPoint myPos
= GetPosition();
469 wxSize mySize
= GetSize();
471 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
472 if (myPos
.x
+ sx
> parentSize
.x
)
473 sx
= parentSize
.x
- myPos
.x
;
476 SetSize(sx
, wxDefaultCoord
);
482 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
484 if ( !m_finished
&& !m_aboutToFinish
)
486 // We must finish regardless of success, otherwise we'll get
490 if ( !AcceptChanges() )
491 m_owner
->OnRenameCancelled( m_itemEdited
);
494 // We must let the native text control handle focus, too, otherwise
495 // it could have problems with the cursor (e.g., in wxGTK).
499 // -----------------------------------------------------------------------------
501 // -----------------------------------------------------------------------------
503 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
504 const wxString
& text
,
505 int image
, int selImage
,
506 wxTreeItemData
*data
)
509 m_images
[wxTreeItemIcon_Normal
] = image
;
510 m_images
[wxTreeItemIcon_Selected
] = selImage
;
511 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
512 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
517 m_isCollapsed
= true;
518 m_hasHilight
= false;
524 m_attr
= (wxTreeItemAttr
*)NULL
;
527 // We don't know the height here yet.
532 wxGenericTreeItem::~wxGenericTreeItem()
536 if (m_ownsAttr
) delete m_attr
;
538 wxASSERT_MSG( m_children
.IsEmpty(),
539 wxT("please call DeleteChildren() before deleting the item") );
542 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
544 size_t count
= m_children
.Count();
545 for ( size_t n
= 0; n
< count
; n
++ )
547 wxGenericTreeItem
*child
= m_children
[n
];
549 tree
->SendDeleteEvent(child
);
551 child
->DeleteChildren(tree
);
552 if (child
== tree
->m_select_me
)
553 tree
->m_select_me
= NULL
;
560 void wxGenericTreeItem::SetText( const wxString
&text
)
565 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
567 size_t count
= m_children
.Count();
571 size_t total
= count
;
572 for (size_t n
= 0; n
< count
; ++n
)
574 total
+= m_children
[n
]->GetChildrenCount();
580 void wxGenericTreeItem::GetSize( int &x
, int &y
,
581 const wxGenericTreeCtrl
*theButton
)
583 int bottomY
=m_y
+theButton
->GetLineHeight(this);
584 if ( y
< bottomY
) y
= bottomY
;
585 int width
= m_x
+ m_width
;
586 if ( x
< width
) x
= width
;
590 size_t count
= m_children
.Count();
591 for ( size_t n
= 0; n
< count
; ++n
)
593 m_children
[n
]->GetSize( x
, y
, theButton
);
598 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
599 const wxGenericTreeCtrl
*theCtrl
,
603 // for a hidden root node, don't evaluate it, but do evaluate children
604 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
607 int h
= theCtrl
->GetLineHeight(this);
608 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
610 int y_mid
= m_y
+ h
/2;
611 if (point
.y
< y_mid
)
612 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
614 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
616 int xCross
= m_x
- theCtrl
->GetSpacing();
618 // according to the drawing code the triangels are drawn
619 // at -4 , -4 from the position up to +10/+10 max
620 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
621 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
622 HasPlus() && theCtrl
->HasButtons() )
624 // 5 is the size of the plus sign
625 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
626 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
627 HasPlus() && theCtrl
->HasButtons() )
630 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
634 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
639 // assuming every image (normal and selected) has the same size!
640 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
641 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
644 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
645 flags
|= wxTREE_HITTEST_ONITEMICON
;
647 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
653 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
654 if (point
.x
> m_x
+m_width
)
655 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
660 // if children are expanded, fall through to evaluate them
661 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
665 size_t count
= m_children
.Count();
666 for ( size_t n
= 0; n
< count
; n
++ )
668 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
676 return (wxGenericTreeItem
*) NULL
;
679 int wxGenericTreeItem::GetCurrentImage() const
681 int image
= NO_IMAGE
;
686 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
689 if ( image
== NO_IMAGE
)
691 // we usually fall back to the normal item, but try just the
692 // expanded one (and not selected) first in this case
693 image
= GetImage(wxTreeItemIcon_Expanded
);
699 image
= GetImage(wxTreeItemIcon_Selected
);
702 // maybe it doesn't have the specific image we want,
703 // try the default one instead
704 if ( image
== NO_IMAGE
) image
= GetImage();
709 // -----------------------------------------------------------------------------
710 // wxGenericTreeCtrl implementation
711 // -----------------------------------------------------------------------------
713 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
715 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
716 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
717 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
718 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
719 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
720 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
721 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
724 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
726 * wxTreeCtrl has to be a real class or we have problems with
727 * the run-time information.
730 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
733 // -----------------------------------------------------------------------------
734 // construction/destruction
735 // -----------------------------------------------------------------------------
737 void wxGenericTreeCtrl::Init()
742 m_select_me
= (wxGenericTreeItem
*) NULL
;
750 m_hilightBrush
= new wxBrush
752 wxSystemSettings::GetColour
754 wxSYS_COLOUR_HIGHLIGHT
759 m_hilightUnfocusedBrush
= new wxBrush
761 wxSystemSettings::GetColour
763 wxSYS_COLOUR_BTNSHADOW
768 m_imageListButtons
= NULL
;
769 m_ownsImageListButtons
= false;
772 m_isDragging
= false;
773 m_dropTarget
= m_oldSelection
= NULL
;
777 m_renameTimer
= NULL
;
782 m_dropEffectAboveItem
= false;
784 m_lastOnSame
= false;
786 #ifdef __WXMAC_CARBON__
787 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
789 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
791 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
792 m_normalFont
.GetFamily(),
793 m_normalFont
.GetStyle(),
795 m_normalFont
.GetUnderlined(),
796 m_normalFont
.GetFaceName(),
797 m_normalFont
.GetEncoding());
800 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
805 const wxValidator
& validator
,
806 const wxString
& name
)
810 wxGetOsVersion( &major
, &minor
);
812 style
&= ~wxTR_LINES_AT_ROOT
;
813 style
|= wxTR_NO_LINES
;
815 style
|= wxTR_ROW_LINES
;
818 if ( !wxControl::Create( parent
, id
, pos
, size
,
819 style
|wxHSCROLL
|wxVSCROLL
,
824 // If the tree display has no buttons, but does have
825 // connecting lines, we can use a narrower layout.
826 // It may not be a good idea to force this...
827 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
833 wxVisualAttributes attr
= GetDefaultAttributes();
834 SetOwnForegroundColour( attr
.colFg
);
835 SetOwnBackgroundColour( attr
.colBg
);
837 SetOwnFont(attr
.font
);
839 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
846 wxGenericTreeCtrl::~wxGenericTreeCtrl()
848 delete m_hilightBrush
;
849 delete m_hilightUnfocusedBrush
;
853 delete m_renameTimer
;
856 if (m_ownsImageListButtons
)
857 delete m_imageListButtons
;
860 // -----------------------------------------------------------------------------
862 // -----------------------------------------------------------------------------
864 size_t wxGenericTreeCtrl::GetCount() const
872 size_t count
= m_anchor
->GetChildrenCount();
873 if ( !HasFlag(wxTR_HIDE_ROOT
) )
875 // take the root itself into account
882 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
884 m_indent
= (unsigned short) indent
;
889 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
890 bool recursively
) const
892 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
894 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
897 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
899 // Do not try to expand the root node if it hasn't been created yet
900 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
902 // if we will hide the root, make sure children are visible
903 m_anchor
->SetHasPlus();
905 CalculatePositions();
908 // right now, just sets the styles. Eventually, we may
909 // want to update the inherited styles, but right now
910 // none of the parents has updatable styles
911 m_windowStyle
= styles
;
915 // -----------------------------------------------------------------------------
916 // functions to work with tree items
917 // -----------------------------------------------------------------------------
919 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
921 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
923 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
926 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
927 wxTreeItemIcon which
) const
929 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
931 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
934 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
936 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
938 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
941 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
943 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
945 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
946 return pItem
->Attr().GetTextColour();
949 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
951 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
953 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
954 return pItem
->Attr().GetBackgroundColour();
957 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
959 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
961 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
962 return pItem
->Attr().GetFont();
965 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
967 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
970 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
971 pItem
->SetText(text
);
972 CalculateSize(pItem
, dc
);
976 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
978 wxTreeItemIcon which
)
980 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
982 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
983 pItem
->SetImage(image
, which
);
986 CalculateSize(pItem
, dc
);
990 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
992 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
997 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1000 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1002 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1004 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1005 pItem
->SetHasPlus(has
);
1009 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1011 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1013 // avoid redrawing the tree if no real change
1014 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1015 if ( pItem
->IsBold() != bold
)
1017 pItem
->SetBold(bold
);
1022 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1025 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1031 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1032 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1035 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1036 pItem
->Attr().SetTextColour(fg
);
1037 pItem
->Attr().SetBackgroundColour(bg
);
1041 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1042 const wxColour
& col
)
1044 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1046 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1047 pItem
->Attr().SetTextColour(col
);
1051 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1052 const wxColour
& col
)
1054 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1056 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1057 pItem
->Attr().SetBackgroundColour(col
);
1061 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1063 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1065 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1066 pItem
->Attr().SetFont(font
);
1070 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1072 wxTreeCtrlBase::SetFont(font
);
1074 m_normalFont
= font
;
1075 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1076 m_normalFont
.GetFamily(),
1077 m_normalFont
.GetStyle(),
1079 m_normalFont
.GetUnderlined(),
1080 m_normalFont
.GetFaceName(),
1081 m_normalFont
.GetEncoding());
1087 // -----------------------------------------------------------------------------
1088 // item status inquiries
1089 // -----------------------------------------------------------------------------
1091 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1093 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1095 // An item is only visible if it's not a descendant of a collapsed item
1096 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1097 wxGenericTreeItem
* parent
= pItem
->GetParent();
1100 if (!parent
->IsExpanded())
1102 parent
= parent
->GetParent();
1106 GetViewStart(& startX
, & startY
);
1108 wxSize clientSize
= GetClientSize();
1111 if (!GetBoundingRect(item
, rect
))
1113 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1115 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1117 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1123 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1125 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1127 // consider that the item does have children if it has the "+" button: it
1128 // might not have them (if it had never been expanded yet) but then it
1129 // could have them as well and it's better to err on this side rather than
1130 // disabling some operations which are restricted to the items with
1131 // children for an item which does have them
1132 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1135 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1137 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1139 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1142 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1144 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1146 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1149 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1151 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1153 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1156 // -----------------------------------------------------------------------------
1158 // -----------------------------------------------------------------------------
1160 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1162 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1164 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1167 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1168 wxTreeItemIdValue
& cookie
) const
1170 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1173 return GetNextChild(item
, cookie
);
1176 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1177 wxTreeItemIdValue
& cookie
) const
1179 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1181 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1183 // it's ok to cast cookie to size_t, we never have indices big enough to
1184 // overflow "void *"
1185 size_t *pIndex
= (size_t *)&cookie
;
1186 if ( *pIndex
< children
.Count() )
1188 return children
.Item((*pIndex
)++);
1192 // there are no more of them
1193 return wxTreeItemId();
1197 #if WXWIN_COMPATIBILITY_2_4
1199 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1202 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1205 return GetNextChild(item
, cookie
);
1208 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1211 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1213 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1214 if ( (size_t)cookie
< children
.Count() )
1216 return children
.Item((size_t)cookie
++);
1220 // there are no more of them
1221 return wxTreeItemId();
1225 #endif // WXWIN_COMPATIBILITY_2_4
1227 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1229 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1231 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1232 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1235 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1237 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1239 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1240 wxGenericTreeItem
*parent
= i
->GetParent();
1241 if ( parent
== NULL
)
1243 // root item doesn't have any siblings
1244 return wxTreeItemId();
1247 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1248 int index
= siblings
.Index(i
);
1249 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1251 size_t n
= (size_t)(index
+ 1);
1252 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1255 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1257 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1259 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1260 wxGenericTreeItem
*parent
= i
->GetParent();
1261 if ( parent
== NULL
)
1263 // root item doesn't have any siblings
1264 return wxTreeItemId();
1267 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1268 int index
= siblings
.Index(i
);
1269 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1271 return index
== 0 ? wxTreeItemId()
1272 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1275 // Only for internal use right now, but should probably be public
1276 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1278 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1280 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1282 // First see if there are any children.
1283 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1284 if (children
.GetCount() > 0)
1286 return children
.Item(0);
1290 // Try a sibling of this or ancestor instead
1291 wxTreeItemId p
= item
;
1292 wxTreeItemId toFind
;
1295 toFind
= GetNextSibling(p
);
1296 p
= GetItemParent(p
);
1297 } while (p
.IsOk() && !toFind
.IsOk());
1302 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1304 wxTreeItemId id
= GetRootItem();
1313 } while (id
.IsOk());
1315 return wxTreeItemId();
1318 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1320 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1322 wxTreeItemId id
= item
;
1325 while (id
= GetNext(id
), id
.IsOk())
1331 return wxTreeItemId();
1334 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1336 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1338 wxFAIL_MSG(wxT("not implemented"));
1340 return wxTreeItemId();
1343 // called by wxTextTreeCtrl when it marks itself for deletion
1344 void wxGenericTreeCtrl::ResetTextControl()
1349 // find the first item starting with the given prefix after the given item
1350 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1351 const wxString
& prefixOrig
) const
1353 // match is case insensitive as this is more convenient to the user: having
1354 // to press Shift-letter to go to the item starting with a capital letter
1355 // would be too bothersome
1356 wxString prefix
= prefixOrig
.Lower();
1358 // determine the starting point: we shouldn't take the current item (this
1359 // allows to switch between two items starting with the same letter just by
1360 // pressing it) but we shouldn't jump to the next one if the user is
1361 // continuing to type as otherwise he might easily skip the item he wanted
1362 wxTreeItemId id
= idParent
;
1363 if ( prefix
.length() == 1 )
1368 // look for the item starting with the given prefix after it
1369 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1374 // if we haven't found anything...
1377 // ... wrap to the beginning
1379 if ( HasFlag(wxTR_HIDE_ROOT
) )
1381 // can't select virtual root
1385 // and try all the items (stop when we get to the one we started from)
1386 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1395 // -----------------------------------------------------------------------------
1397 // -----------------------------------------------------------------------------
1399 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1401 const wxString
& text
,
1404 wxTreeItemData
*data
)
1406 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1409 // should we give a warning here?
1410 return AddRoot(text
, image
, selImage
, data
);
1413 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1415 wxGenericTreeItem
*item
=
1416 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1420 data
->m_pItem
= item
;
1423 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1429 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1432 wxTreeItemData
*data
)
1434 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1436 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1438 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1439 image
, selImage
, data
);
1442 data
->m_pItem
= m_anchor
;
1445 if (HasFlag(wxTR_HIDE_ROOT
))
1447 // if root is hidden, make sure we can navigate
1449 m_anchor
->SetHasPlus();
1451 CalculatePositions();
1454 if (!HasFlag(wxTR_MULTIPLE
))
1456 m_current
= m_key_current
= m_anchor
;
1457 m_current
->SetHilight( true );
1463 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1464 const wxTreeItemId
& idPrevious
,
1465 const wxString
& text
,
1466 int image
, int selImage
,
1467 wxTreeItemData
*data
)
1469 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1472 // should we give a warning here?
1473 return AddRoot(text
, image
, selImage
, data
);
1477 if (idPrevious
.IsOk())
1479 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1480 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1481 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1484 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1488 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1490 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1491 event
.m_item
= item
;
1492 event
.SetEventObject( this );
1493 ProcessEvent( event
);
1496 // Don't leave edit or selection on a child which is about to disappear
1497 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1499 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1500 m_textCtrl
->StopEditing();
1502 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1503 m_key_current
= NULL
;
1505 if (IsDescendantOf(item
, m_select_me
)) {
1508 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1509 m_current
->SetHilight( false );
1515 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1517 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1519 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1520 ChildrenClosing(item
);
1521 item
->DeleteChildren(this);
1524 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1526 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1528 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1530 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1532 // can't delete the item being edited, cancel editing it first
1533 m_textCtrl
->StopEditing();
1536 wxGenericTreeItem
*parent
= item
->GetParent();
1538 // don't keep stale pointers around!
1539 if ( IsDescendantOf(item
, m_key_current
) )
1541 // Don't silently change the selection:
1542 // do it properly in idle time, so event
1543 // handlers get called.
1545 // m_key_current = parent;
1546 m_key_current
= NULL
;
1549 // m_select_me records whether we need to select
1550 // a different item, in idle time.
1551 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1553 m_select_me
= parent
;
1556 if ( IsDescendantOf(item
, m_current
) )
1558 // Don't silently change the selection:
1559 // do it properly in idle time, so event
1560 // handlers get called.
1562 // m_current = parent;
1564 m_select_me
= parent
;
1567 // remove the item from the tree
1570 parent
->GetChildren().Remove( item
); // remove by value
1572 else // deleting the root
1574 // nothing will be left in the tree
1578 // and delete all of its children and the item itself now
1579 item
->DeleteChildren(this);
1580 SendDeleteEvent(item
);
1582 if (item
== m_select_me
)
1588 void wxGenericTreeCtrl::DeleteAllItems()
1596 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1598 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1600 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1601 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1602 _T("can't expand hidden root") );
1604 if ( !item
->HasPlus() )
1607 if ( item
->IsExpanded() )
1610 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1611 event
.m_item
= item
;
1612 event
.SetEventObject( this );
1614 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1616 // cancelled by program
1621 CalculatePositions();
1623 RefreshSubtree(item
);
1625 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1626 ProcessEvent( event
);
1629 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1631 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1634 if ( !IsExpanded(item
) )
1638 wxTreeItemIdValue cookie
;
1639 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1640 while ( child
.IsOk() )
1644 child
= GetNextChild(item
, cookie
);
1648 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1650 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1651 _T("can't collapse hidden root") );
1653 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1655 if ( !item
->IsExpanded() )
1658 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1659 event
.m_item
= item
;
1660 event
.SetEventObject( this );
1661 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1663 // cancelled by program
1667 ChildrenClosing(item
);
1670 #if 0 // TODO why should items be collapsed recursively?
1671 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1672 size_t count
= children
.Count();
1673 for ( size_t n
= 0; n
< count
; n
++ )
1675 Collapse(children
[n
]);
1679 CalculatePositions();
1681 RefreshSubtree(item
);
1683 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1684 ProcessEvent( event
);
1687 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1690 DeleteChildren(item
);
1693 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1695 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1697 if (item
->IsExpanded())
1703 void wxGenericTreeCtrl::Unselect()
1707 m_current
->SetHilight( false );
1708 RefreshLine( m_current
);
1715 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1717 if (item
->IsSelected())
1719 item
->SetHilight(false);
1723 if (item
->HasChildren())
1725 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1726 size_t count
= children
.Count();
1727 for ( size_t n
= 0; n
< count
; ++n
)
1729 UnselectAllChildren(children
[n
]);
1734 void wxGenericTreeCtrl::UnselectAll()
1736 wxTreeItemId rootItem
= GetRootItem();
1738 // the tree might not have the root item at all
1741 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1745 // Recursive function !
1746 // To stop we must have crt_item<last_item
1748 // Tag all next children, when no more children,
1749 // Move to parent (not to tag)
1750 // Keep going... if we found last_item, we stop.
1751 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1753 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1755 if (parent
== NULL
) // This is root item
1756 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1758 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1759 int index
= children
.Index(crt_item
);
1760 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1762 size_t count
= children
.Count();
1763 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1765 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1768 return TagNextChildren(parent
, last_item
, select
);
1771 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1773 crt_item
->SetHilight(select
);
1774 RefreshLine(crt_item
);
1776 if (crt_item
==last_item
)
1779 if (crt_item
->HasChildren())
1781 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1782 size_t count
= children
.Count();
1783 for ( size_t n
= 0; n
< count
; ++n
)
1785 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1793 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1797 // item2 is not necessary after item1
1798 // choice first' and 'last' between item1 and item2
1799 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1800 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1802 bool select
= m_current
->IsSelected();
1804 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1807 TagNextChildren(first
,last
,select
);
1810 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1811 bool unselect_others
,
1812 bool extended_select
)
1814 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1818 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1819 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1821 //wxCHECK_RET( ( (!unselect_others) && is_single),
1822 // wxT("this is a single selection tree") );
1824 // to keep going anyhow !!!
1827 if (item
->IsSelected())
1828 return; // nothing to do
1829 unselect_others
= true;
1830 extended_select
= false;
1832 else if ( unselect_others
&& item
->IsSelected() )
1834 // selection change if there is more than one item currently selected
1835 wxArrayTreeItemIds selected_items
;
1836 if ( GetSelections(selected_items
) == 1 )
1840 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1841 event
.m_item
= item
;
1842 event
.m_itemOld
= m_current
;
1843 event
.SetEventObject( this );
1844 // TODO : Here we don't send any selection mode yet !
1846 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1849 wxTreeItemId parent
= GetItemParent( itemId
);
1850 while (parent
.IsOk())
1852 if (!IsExpanded(parent
))
1855 parent
= GetItemParent( parent
);
1858 EnsureVisible( itemId
);
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 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1892 GetEventHandler()->ProcessEvent( event
);
1895 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1899 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1903 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1904 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1906 item
->SetHilight(false);
1911 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1912 wxArrayTreeItemIds
&array
) const
1914 if ( item
->IsSelected() )
1915 array
.Add(wxTreeItemId(item
));
1917 if ( item
->HasChildren() )
1919 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1920 size_t count
= children
.GetCount();
1921 for ( size_t n
= 0; n
< count
; ++n
)
1922 FillArray(children
[n
], array
);
1926 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1929 wxTreeItemId idRoot
= GetRootItem();
1930 if ( idRoot
.IsOk() )
1932 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1934 //else: the tree is empty, so no selections
1936 return array
.Count();
1939 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1941 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1943 if (!item
.IsOk()) return;
1945 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1947 // first expand all parent branches
1948 wxGenericTreeItem
*parent
= gitem
->GetParent();
1950 if ( HasFlag(wxTR_HIDE_ROOT
) )
1952 while ( parent
&& parent
!= m_anchor
)
1955 parent
= parent
->GetParent();
1963 parent
= parent
->GetParent();
1967 //if (parent) CalculatePositions();
1972 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1974 if (!item
.IsOk()) return;
1976 // We have to call this here because the label in
1977 // question might just have been added and no screen
1978 // update taken place.
1980 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1985 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1987 // now scroll to the item
1988 int item_y
= gitem
->GetY();
1992 GetViewStart( &start_x
, &start_y
);
1993 start_y
*= PIXELS_PER_UNIT
;
1997 GetClientSize( &client_w
, &client_h
);
1999 if (item_y
< start_y
+3)
2004 m_anchor
->GetSize( x
, y
, this );
2005 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2006 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2007 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2008 // Item should appear at top
2009 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2011 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2016 m_anchor
->GetSize( x
, y
, this );
2017 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2018 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2019 item_y
+= PIXELS_PER_UNIT
+2;
2020 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2021 // Item should appear at bottom
2022 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
);
2026 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2027 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2029 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2030 wxGenericTreeItem
**item2
)
2032 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2034 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2037 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2039 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2041 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2043 wxCHECK_RET( !s_treeBeingSorted
,
2044 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2046 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2047 if ( children
.Count() > 1 )
2051 s_treeBeingSorted
= this;
2052 children
.Sort(tree_ctrl_compare_func
);
2053 s_treeBeingSorted
= NULL
;
2055 //else: don't make the tree dirty as nothing changed
2058 void wxGenericTreeCtrl::CalculateLineHeight()
2060 wxClientDC
dc(this);
2061 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2063 if ( m_imageListNormal
)
2065 // Calculate a m_lineHeight value from the normal Image sizes.
2066 // May be toggle off. Then wxGenericTreeCtrl will spread when
2067 // necessary (which might look ugly).
2068 int n
= m_imageListNormal
->GetImageCount();
2069 for (int i
= 0; i
< n
; i
++)
2071 int width
= 0, height
= 0;
2072 m_imageListNormal
->GetSize(i
, width
, height
);
2073 if (height
> m_lineHeight
) m_lineHeight
= height
;
2077 if (m_imageListButtons
)
2079 // Calculate a m_lineHeight value from the Button image sizes.
2080 // May be toggle off. Then wxGenericTreeCtrl will spread when
2081 // necessary (which might look ugly).
2082 int n
= m_imageListButtons
->GetImageCount();
2083 for (int i
= 0; i
< n
; i
++)
2085 int width
= 0, height
= 0;
2086 m_imageListButtons
->GetSize(i
, width
, height
);
2087 if (height
> m_lineHeight
) m_lineHeight
= height
;
2091 if (m_lineHeight
< 30)
2092 m_lineHeight
+= 2; // at least 2 pixels
2094 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2097 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2099 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2100 m_imageListNormal
= imageList
;
2101 m_ownsImageListNormal
= false;
2103 // Don't do any drawing if we're setting the list to NULL,
2104 // since we may be in the process of deleting the tree control.
2106 CalculateLineHeight();
2109 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2111 if (m_ownsImageListState
) delete m_imageListState
;
2112 m_imageListState
= imageList
;
2113 m_ownsImageListState
= false;
2116 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2118 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2119 m_imageListButtons
= imageList
;
2120 m_ownsImageListButtons
= false;
2122 CalculateLineHeight();
2125 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2127 SetButtonsImageList(imageList
);
2128 m_ownsImageListButtons
= true;
2131 // -----------------------------------------------------------------------------
2133 // -----------------------------------------------------------------------------
2135 void wxGenericTreeCtrl::AdjustMyScrollbars()
2140 m_anchor
->GetSize( x
, y
, this );
2141 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2142 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2143 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2144 int y_pos
= GetScrollPos( wxVERTICAL
);
2145 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2149 SetScrollbars( 0, 0, 0, 0 );
2153 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2155 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2156 return item
->GetHeight();
2158 return m_lineHeight
;
2161 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2163 // TODO implement "state" icon on items
2165 wxTreeItemAttr
*attr
= item
->GetAttributes();
2166 if ( attr
&& attr
->HasFont() )
2167 dc
.SetFont(attr
->GetFont());
2168 else if (item
->IsBold())
2169 dc
.SetFont(m_boldFont
);
2171 long text_w
= 0, text_h
= 0;
2172 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2174 int image_h
= 0, image_w
= 0;
2175 int image
= item
->GetCurrentImage();
2176 if ( image
!= NO_IMAGE
)
2178 if ( m_imageListNormal
)
2180 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2189 int total_h
= GetLineHeight(item
);
2190 bool drawItemBackground
= false;
2192 if ( item
->IsSelected() )
2194 // under mac selections are only a rectangle in case they don't have the focus
2198 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2199 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2203 dc
.SetBrush( *m_hilightBrush
) ;
2206 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2208 drawItemBackground
= true;
2213 if ( attr
&& attr
->HasBackgroundColour() )
2215 drawItemBackground
= true;
2216 colBg
= attr
->GetBackgroundColour();
2220 colBg
= GetBackgroundColour();
2222 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2225 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2227 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2231 DoGetPosition(&x
, &y
);
2233 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2237 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2239 // If it's selected, and there's an image, then we should
2240 // take care to leave the area under the image painted in the
2241 // background colour.
2242 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2243 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2245 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2246 // don't allow backgrounds to be customized. Not drawing the background,
2247 // except for custom item backgrounds, works for both kinds of theme.
2248 else if (drawItemBackground
)
2250 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2251 item
->GetWidth()+2, total_h
-offset
);
2255 if ( image
!= NO_IMAGE
)
2257 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2258 m_imageListNormal
->Draw( image
, dc
,
2260 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2261 wxIMAGELIST_DRAW_TRANSPARENT
);
2262 dc
.DestroyClippingRegion();
2265 dc
.SetBackgroundMode(wxTRANSPARENT
);
2266 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2267 dc
.DrawText( item
->GetText(),
2268 (wxCoord
)(image_w
+ item
->GetX()),
2269 (wxCoord
)(item
->GetY() + extraH
));
2271 // restore normal font
2272 dc
.SetFont( m_normalFont
);
2275 // Now y stands for the top of the item, whereas it used to stand for middle !
2276 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2278 int x
= level
*m_indent
;
2279 if (!HasFlag(wxTR_HIDE_ROOT
))
2283 else if (level
== 0)
2285 // always expand hidden root
2287 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2288 int count
= children
.Count();
2294 PaintLevel(children
[n
], dc
, 1, y
);
2295 } while (++n
< count
);
2297 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2299 // draw line down to last child
2300 origY
+= GetLineHeight(children
[0])>>1;
2301 oldY
+= GetLineHeight(children
[n
-1])>>1;
2302 dc
.DrawLine(3, origY
, 3, oldY
);
2308 item
->SetX(x
+m_spacing
);
2311 int h
= GetLineHeight(item
);
2313 int y_mid
= y_top
+ (h
>>1);
2316 int exposed_x
= dc
.LogicalToDeviceX(0);
2317 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2319 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2323 // don't draw rect outline if we already have the
2324 // background color under Mac
2325 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2326 #endif // !__WXMAC__
2330 if ( item
->IsSelected() )
2332 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2336 wxTreeItemAttr
*attr
= item
->GetAttributes();
2337 if (attr
&& attr
->HasTextColour())
2338 colText
= attr
->GetTextColour();
2340 colText
= GetForegroundColour();
2344 dc
.SetTextForeground(colText
);
2348 PaintItem(item
, dc
);
2350 if (HasFlag(wxTR_ROW_LINES
))
2352 // if the background colour is white, choose a
2353 // contrasting color for the lines
2354 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2355 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2356 dc
.DrawLine(0, y_top
, 10000, y_top
);
2357 dc
.DrawLine(0, y
, 10000, y
);
2360 // restore DC objects
2361 dc
.SetBrush(*wxWHITE_BRUSH
);
2362 dc
.SetPen(m_dottedPen
);
2363 dc
.SetTextForeground(*wxBLACK
);
2365 if ( !HasFlag(wxTR_NO_LINES
) )
2367 // draw the horizontal line here
2369 if (x
> (signed)m_indent
)
2370 x_start
-= m_indent
;
2371 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2373 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2376 // should the item show a button?
2377 if ( item
->HasPlus() && HasButtons() )
2379 if ( m_imageListButtons
)
2381 // draw the image button here
2384 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2385 : wxTreeItemIcon_Normal
;
2386 if ( item
->IsSelected() )
2387 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2389 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2390 int xx
= x
- image_w
/2;
2391 int yy
= y_mid
- image_h
/2;
2393 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2394 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2395 wxIMAGELIST_DRAW_TRANSPARENT
);
2397 else // no custom buttons
2399 static const int wImage
= 9;
2400 static const int hImage
= 9;
2403 if (item
->IsExpanded())
2404 flag
|= wxCONTROL_EXPANDED
;
2405 if (item
== m_underMouse
)
2406 flag
|= wxCONTROL_CURRENT
;
2408 wxRendererNative::Get().DrawTreeItemButton
2412 wxRect(x
- wImage
/2,
2421 if (item
->IsExpanded())
2423 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2424 int count
= children
.Count();
2431 PaintLevel(children
[n
], dc
, level
, y
);
2432 } while (++n
< count
);
2434 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2436 // draw line down to last child
2437 oldY
+= GetLineHeight(children
[n
-1])>>1;
2438 if (HasButtons()) y_mid
+= 5;
2440 // Only draw the portion of the line that is visible, in case it is huge
2441 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2442 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2443 yOrigin
= abs(yOrigin
);
2444 GetClientSize(&width
, &height
);
2446 // Move end points to the begining/end of the view?
2447 if (y_mid
< yOrigin
)
2449 if (oldY
> yOrigin
+ height
)
2450 oldY
= yOrigin
+ height
;
2452 // after the adjustments if y_mid is larger than oldY then the line
2453 // isn't visible at all so don't draw anything
2455 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2461 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2465 if ( item
->HasPlus() )
2467 // it's a folder, indicate it by a border
2472 // draw a line under the drop target because the item will be
2474 DrawLine(item
, !m_dropEffectAboveItem
);
2477 SetCursor(wxCURSOR_BULLSEYE
);
2482 SetCursor(wxCURSOR_NO_ENTRY
);
2486 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2488 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2490 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2492 wxClientDC
dc(this);
2494 dc
.SetLogicalFunction(wxINVERT
);
2495 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2497 int w
= i
->GetWidth() + 2;
2498 int h
= GetLineHeight(i
) + 2;
2500 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2503 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2505 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2507 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2509 wxClientDC
dc(this);
2511 dc
.SetLogicalFunction(wxINVERT
);
2517 y
+= GetLineHeight(i
) - 1;
2520 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2523 // -----------------------------------------------------------------------------
2524 // wxWidgets callbacks
2525 // -----------------------------------------------------------------------------
2527 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2535 dc
.SetFont( m_normalFont
);
2536 dc
.SetPen( m_dottedPen
);
2538 // this is now done dynamically
2539 //if(GetImageList() == NULL)
2540 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2543 PaintLevel( m_anchor
, dc
, 0, y
);
2546 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2555 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2564 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2566 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2567 te
.m_evtKey
= event
;
2568 te
.SetEventObject( this );
2569 if ( GetEventHandler()->ProcessEvent( te
) )
2571 // intercepted by the user code
2575 if ( (m_current
== 0) || (m_key_current
== 0) )
2581 // how should the selection work for this event?
2582 bool is_multiple
, extended_select
, unselect_others
;
2583 EventFlagsToSelType(GetWindowStyleFlag(),
2586 is_multiple
, extended_select
, unselect_others
);
2590 // * : Expand all/Collapse all
2591 // ' ' | return : activate
2592 // up : go up (not last children!)
2594 // left : go to parent
2595 // right : open if parent and go next
2596 // home : go to root
2597 // end : go to last item without opening parents
2598 // alnum : start or continue searching for the item with this prefix
2599 int keyCode
= event
.GetKeyCode();
2604 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2612 if ( !IsExpanded(m_current
) )
2615 ExpandAll(m_current
);
2618 //else: fall through to Collapse() it
2622 if (IsExpanded(m_current
))
2624 Collapse(m_current
);
2630 // Use the item's bounding rectangle to determine position for the event
2632 GetBoundingRect(m_current
, ItemRect
, true);
2634 wxTreeEvent
eventMenu( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2635 eventMenu
.m_item
= m_current
;
2636 // Use the left edge, vertical middle
2637 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2638 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2639 eventMenu
.SetEventObject( this );
2640 GetEventHandler()->ProcessEvent( eventMenu
);
2645 if ( !event
.HasModifiers() )
2647 wxTreeEvent
eventAct( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2648 eventAct
.m_item
= m_current
;
2649 eventAct
.SetEventObject( this );
2650 GetEventHandler()->ProcessEvent( eventAct
);
2653 // in any case, also generate the normal key event for this key,
2654 // even if we generated the ACTIVATED event above: this is what
2655 // wxMSW does and it makes sense because you might not want to
2656 // process ACTIVATED event at all and handle Space and Return
2657 // directly (and differently) which would be impossible otherwise
2661 // up goes to the previous sibling or to the last
2662 // of its children if it's expanded
2665 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2668 prev
= GetItemParent( m_key_current
);
2669 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2671 break; // don't go to root if it is hidden
2675 wxTreeItemIdValue cookie
;
2676 wxTreeItemId current
= m_key_current
;
2677 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2678 if (current
== GetFirstChild( prev
, cookie
))
2680 // otherwise we return to where we came from
2681 DoSelectItem( prev
, unselect_others
, extended_select
);
2682 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2689 while ( IsExpanded(prev
) && HasChildren(prev
) )
2691 wxTreeItemId child
= GetLastChild(prev
);
2698 DoSelectItem( prev
, unselect_others
, extended_select
);
2699 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2704 // left arrow goes to the parent
2707 wxTreeItemId prev
= GetItemParent( m_current
);
2708 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2710 // don't go to root if it is hidden
2711 prev
= GetPrevSibling( m_current
);
2715 DoSelectItem( prev
, unselect_others
, extended_select
);
2721 // this works the same as the down arrow except that we
2722 // also expand the item if it wasn't expanded yet
2728 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2730 wxTreeItemIdValue cookie
;
2731 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2732 DoSelectItem( child
, unselect_others
, extended_select
);
2733 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2737 wxTreeItemId next
= GetNextSibling( m_key_current
);
2740 wxTreeItemId current
= m_key_current
;
2741 while (current
.IsOk() && !next
)
2743 current
= GetItemParent( current
);
2744 if (current
) next
= GetNextSibling( current
);
2749 DoSelectItem( next
, unselect_others
, extended_select
);
2750 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2756 // <End> selects the last visible tree item
2759 wxTreeItemId last
= GetRootItem();
2761 while ( last
.IsOk() && IsExpanded(last
) )
2763 wxTreeItemId lastChild
= GetLastChild(last
);
2765 // it may happen if the item was expanded but then all of
2766 // its children have been deleted - so IsExpanded() returned
2767 // true, but GetLastChild() returned invalid item
2776 DoSelectItem( last
, unselect_others
, extended_select
);
2781 // <Home> selects the root item
2784 wxTreeItemId prev
= GetRootItem();
2788 if ( HasFlag(wxTR_HIDE_ROOT
) )
2790 wxTreeItemIdValue cookie
;
2791 prev
= GetFirstChild(prev
, cookie
);
2796 DoSelectItem( prev
, unselect_others
, extended_select
);
2801 // do not use wxIsalnum() here
2802 if ( !event
.HasModifiers() &&
2803 ((keyCode
>= '0' && keyCode
<= '9') ||
2804 (keyCode
>= 'a' && keyCode
<= 'z') ||
2805 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2807 // find the next item starting with the given prefix
2808 wxChar ch
= (wxChar
)keyCode
;
2810 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2821 // also start the timer to reset the current prefix if the user
2822 // doesn't press any more alnum keys soon -- we wouldn't want
2823 // to use this prefix for a new item search
2826 m_findTimer
= new wxTreeFindTimer(this);
2829 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2838 wxTreeItemId
wxGenericTreeCtrl::DoHitTest(const wxPoint
& point
, int& flags
)
2840 // JACS: removed wxYieldIfNeeded() because it can cause the window
2841 // to be deleted from under us if a close window event is pending
2846 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2847 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2848 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2849 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2850 if (flags
) return wxTreeItemId();
2852 if (m_anchor
== NULL
)
2854 flags
= wxTREE_HITTEST_NOWHERE
;
2855 return wxTreeItemId();
2858 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2862 flags
= wxTREE_HITTEST_NOWHERE
;
2863 return wxTreeItemId();
2868 // get the bounding rectangle of the item (or of its label only)
2869 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2871 bool WXUNUSED(textOnly
)) const
2873 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2875 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2878 GetViewStart(& startX
, & startY
);
2880 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2881 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2882 rect
.width
= i
->GetWidth();
2883 //rect.height = i->GetHeight();
2884 rect
.height
= GetLineHeight(i
);
2889 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2890 wxClassInfo
* WXUNUSED(textCtrlClass
))
2892 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
2894 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2896 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2897 te
.m_item
= itemEdit
;
2898 te
.SetEventObject( this );
2899 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2905 // We have to call this here because the label in
2906 // question might just have been added and no screen
2907 // update taken place.
2909 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2915 // TODO: use textCtrlClass here to create the control of correct class
2916 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2918 m_textCtrl
->SetFocus();
2923 // returns a pointer to the text edit control if the item is being
2924 // edited, NULL otherwise (it's assumed that no more than one item may
2925 // be edited simultaneously)
2926 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2931 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
2932 bool discardChanges
)
2934 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
2936 m_textCtrl
->EndEdit(discardChanges
);
2939 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2940 const wxString
& value
)
2942 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2944 le
.SetEventObject( this );
2946 le
.m_editCancelled
= false;
2948 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2951 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2953 // let owner know that the edit was cancelled
2954 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2956 le
.SetEventObject( this );
2957 le
.m_label
= wxEmptyString
;
2958 le
.m_editCancelled
= true;
2960 GetEventHandler()->ProcessEvent( le
);
2963 void wxGenericTreeCtrl::OnRenameTimer()
2965 EditLabel( m_current
);
2968 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2970 if ( !m_anchor
) return;
2972 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2974 // Is the mouse over a tree item button?
2976 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
2977 wxGenericTreeItem
*underMouse
= thisItem
;
2979 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
2980 #endif // wxUSE_TOOLTIPS
2983 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
2984 (!event
.LeftIsDown()) &&
2986 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2994 if (underMouse
!= m_underMouse
)
2998 // unhighlight old item
2999 wxGenericTreeItem
*tmp
= m_underMouse
;
3000 m_underMouse
= NULL
;
3004 m_underMouse
= underMouse
;
3006 RefreshLine( m_underMouse
);
3010 // Determines what item we are hovering over and need a tooltip for
3011 wxTreeItemId hoverItem
= thisItem
;
3013 // We do not want a tooltip if we are dragging, or if the rename timer is running
3014 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3016 // Ask the tree control what tooltip (if any) should be shown
3017 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
3018 hevent
.m_item
= hoverItem
;
3019 hevent
.SetEventObject(this);
3021 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3023 SetToolTip(hevent
.m_label
);
3028 // we process left mouse up event (enables in-place edit), right down
3029 // (pass to the user code), left dbl click (activate item) and
3030 // dragging/moving events for items drag-and-drop
3031 if ( !(event
.LeftDown() ||
3033 event
.RightDown() ||
3034 event
.LeftDClick() ||
3036 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3045 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3047 if ( event
.Dragging() && !m_isDragging
)
3049 if (m_dragCount
== 0)
3054 if (m_dragCount
!= 3)
3056 // wait until user drags a bit further...
3060 wxEventType command
= event
.RightIsDown()
3061 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3062 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3064 wxTreeEvent
nevent( command
, GetId() );
3065 nevent
.m_item
= m_current
;
3066 nevent
.SetEventObject(this);
3067 nevent
.SetPoint(pt
);
3069 // by default the dragging is not supported, the user code must
3070 // explicitly allow the event for it to take place
3073 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3075 // we're going to drag this item
3076 m_isDragging
= true;
3078 // remember the old cursor because we will change it while
3080 m_oldCursor
= m_cursor
;
3082 // in a single selection control, hide the selection temporarily
3083 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3085 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3087 if ( m_oldSelection
)
3089 m_oldSelection
->SetHilight(false);
3090 RefreshLine(m_oldSelection
);
3097 else if ( event
.Dragging() )
3099 if ( item
!= m_dropTarget
)
3101 // unhighlight the previous drop target
3102 DrawDropEffect(m_dropTarget
);
3104 m_dropTarget
= item
;
3106 // highlight the current drop target if any
3107 DrawDropEffect(m_dropTarget
);
3109 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3116 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3118 // erase the highlighting
3119 DrawDropEffect(m_dropTarget
);
3121 if ( m_oldSelection
)
3123 m_oldSelection
->SetHilight(true);
3124 RefreshLine(m_oldSelection
);
3125 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3128 // generate the drag end event
3129 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3131 eventEndDrag
.m_item
= item
;
3132 eventEndDrag
.m_pointDrag
= pt
;
3133 eventEndDrag
.SetEventObject(this);
3135 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3137 m_isDragging
= false;
3138 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3142 SetCursor(m_oldCursor
);
3144 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3152 // If we got to this point, we are not dragging or moving the mouse.
3153 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3154 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3155 // We skip even if we didn't hit an item because we still should
3156 // restore focus to the tree control even if we didn't exactly hit an item.
3157 if ( event
.LeftDown() )
3162 // here we process only the messages which happen on tree items
3166 if (item
== NULL
) return; /* we hit the blank area */
3168 if ( event
.RightDown() )
3170 // If the item is already selected, do not update the selection.
3171 // Multi-selections should not be cleared if a selected item is clicked.
3172 if (!IsSelected(item
))
3174 DoSelectItem(item
, true, false);
3177 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3178 nevent
.m_item
= item
;
3179 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3180 nevent
.SetEventObject(this);
3181 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3183 // Consistent with MSW (for now), send the ITEM_MENU *after*
3184 // the RIGHT_CLICK event. TODO: This behavior may change.
3185 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, GetId());
3186 nevent2
.m_item
= item
;
3187 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3188 nevent2
.SetEventObject(this);
3189 GetEventHandler()->ProcessEvent(nevent2
);
3191 else if ( event
.LeftUp() )
3193 // this facilitates multiple-item drag-and-drop
3195 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3197 wxArrayTreeItemIds selections
;
3198 size_t count
= GetSelections(selections
);
3204 DoSelectItem(item
, true, false);
3210 if ( (item
== m_current
) &&
3211 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3212 HasFlag(wxTR_EDIT_LABELS
) )
3214 if ( m_renameTimer
)
3216 if ( m_renameTimer
->IsRunning() )
3217 m_renameTimer
->Stop();
3221 m_renameTimer
= new wxTreeRenameTimer( this );
3224 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3227 m_lastOnSame
= false;
3230 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3232 if ( event
.LeftDown() )
3234 m_lastOnSame
= item
== m_current
;
3237 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3239 // only toggle the item for a single click, double click on
3240 // the button doesn't do anything (it toggles the item twice)
3241 if ( event
.LeftDown() )
3246 // don't select the item if the button was clicked
3251 // clear the previously selected items, if the
3252 // user clicked outside of the present selection.
3253 // otherwise, perform the deselection on mouse-up.
3254 // this allows multiple drag and drop to work.
3255 // but if Cmd is down, toggle selection of the clicked item
3256 if (!IsSelected(item
) || event
.CmdDown())
3258 // how should the selection work for this event?
3259 bool is_multiple
, extended_select
, unselect_others
;
3260 EventFlagsToSelType(GetWindowStyleFlag(),
3263 is_multiple
, extended_select
, unselect_others
);
3265 DoSelectItem(item
, unselect_others
, extended_select
);
3269 // For some reason, Windows isn't recognizing a left double-click,
3270 // so we need to simulate it here. Allow 200 milliseconds for now.
3271 if ( event
.LeftDClick() )
3273 // double clicking should not start editing the item label
3274 if ( m_renameTimer
)
3275 m_renameTimer
->Stop();
3277 m_lastOnSame
= false;
3279 // send activate event first
3280 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3281 nevent
.m_item
= item
;
3282 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3283 nevent
.SetEventObject( this );
3284 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3286 // if the user code didn't process the activate event,
3287 // handle it ourselves by toggling the item when it is
3289 if ( item
->HasPlus() )
3299 void wxGenericTreeCtrl::OnInternalIdle()
3301 wxWindow::OnInternalIdle();
3303 // Check if we need to select the root item
3304 // because nothing else has been selected.
3305 // Delaying it means that we can invoke event handlers
3306 // as required, when a first item is selected.
3307 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3310 SelectItem(m_select_me
);
3311 else if (GetRootItem().IsOk())
3312 SelectItem(GetRootItem());
3315 /* after all changes have been done to the tree control,
3316 * we actually redraw the tree when everything is over */
3318 if (!m_dirty
) return;
3319 if (m_freezeCount
) return;
3323 CalculatePositions();
3325 AdjustMyScrollbars();
3328 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3333 wxTreeItemAttr
*attr
= item
->GetAttributes();
3334 if ( attr
&& attr
->HasFont() )
3335 dc
.SetFont(attr
->GetFont());
3336 else if ( item
->IsBold() )
3337 dc
.SetFont(m_boldFont
);
3339 dc
.SetFont(m_normalFont
);
3341 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3344 // restore normal font
3345 dc
.SetFont( m_normalFont
);
3349 int image
= item
->GetCurrentImage();
3350 if ( image
!= NO_IMAGE
)
3352 if ( m_imageListNormal
)
3354 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3359 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3362 total_h
+= 2; // at least 2 pixels
3364 total_h
+= total_h
/10; // otherwise 10% extra spacing
3366 item
->SetHeight(total_h
);
3367 if (total_h
>m_lineHeight
)
3368 m_lineHeight
=total_h
;
3370 item
->SetWidth(image_w
+text_w
+2);
3373 // -----------------------------------------------------------------------------
3374 // for developper : y is now the top of the level
3375 // not the middle of it !
3376 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3378 int x
= level
*m_indent
;
3379 if (!HasFlag(wxTR_HIDE_ROOT
))
3383 else if (level
== 0)
3385 // a hidden root is not evaluated, but its
3386 // children are always calculated
3390 CalculateSize( item
, dc
);
3393 item
->SetX( x
+m_spacing
);
3395 y
+= GetLineHeight(item
);
3397 if ( !item
->IsExpanded() )
3399 // we don't need to calculate collapsed branches
3404 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3405 size_t n
, count
= children
.Count();
3407 for (n
= 0; n
< count
; ++n
)
3408 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3411 void wxGenericTreeCtrl::CalculatePositions()
3413 if ( !m_anchor
) return;
3415 wxClientDC
dc(this);
3418 dc
.SetFont( m_normalFont
);
3420 dc
.SetPen( m_dottedPen
);
3421 //if(GetImageList() == NULL)
3422 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3425 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3428 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3430 if (m_dirty
) return;
3431 if (m_freezeCount
) return;
3433 wxSize client
= GetClientSize();
3436 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3437 rect
.width
= client
.x
;
3438 rect
.height
= client
.y
;
3440 Refresh(true, &rect
);
3442 AdjustMyScrollbars();
3445 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3447 if (m_dirty
) return;
3448 if (m_freezeCount
) return;
3451 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3452 rect
.width
= GetClientSize().x
;
3453 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3455 Refresh(true, &rect
);
3458 void wxGenericTreeCtrl::RefreshSelected()
3460 if (m_freezeCount
) return;
3462 // TODO: this is awfully inefficient, we should keep the list of all
3463 // selected items internally, should be much faster
3465 RefreshSelectedUnder(m_anchor
);
3468 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3470 if (m_freezeCount
) return;
3472 if ( item
->IsSelected() )
3475 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3476 size_t count
= children
.GetCount();
3477 for ( size_t n
= 0; n
< count
; n
++ )
3479 RefreshSelectedUnder(children
[n
]);
3483 void wxGenericTreeCtrl::Freeze()
3488 void wxGenericTreeCtrl::Thaw()
3490 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3492 if ( !--m_freezeCount
)
3498 // ----------------------------------------------------------------------------
3499 // changing colours: we need to refresh the tree control
3500 // ----------------------------------------------------------------------------
3502 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3504 if ( !wxWindow::SetBackgroundColour(colour
) )
3507 if (m_freezeCount
) return true;
3514 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3516 if ( !wxWindow::SetForegroundColour(colour
) )
3519 if (m_freezeCount
) return true;
3526 // Process the tooltip event, to speed up event processing.
3527 // Doesn't actually get a tooltip.
3528 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3534 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3536 // something is better than nothing...
3537 // 100x80 is what the MSW version will get from the default
3538 // wxControl::DoGetBestSize
3539 return wxSize(100,80);
3543 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3544 // be removed, as well as the #else case below.
3545 #define _USE_VISATTR 0
3548 #include "wx/listbox.h"
3554 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3556 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3560 // Use the same color scheme as wxListBox
3561 return wxListBox::GetClassDefaultAttributes(variant
);
3563 wxVisualAttributes attr
;
3564 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3565 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3566 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3571 #if WXWIN_COMPATIBILITY_2_4
3573 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3575 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3578 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3580 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3583 #endif // WXWIN_COMPATIBILITY_2_4
3585 #endif // wxUSE_TREECTRL