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
; }
202 // deletes all children notifying the treectrl about it if !NULL
204 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
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
];
552 tree
->SendDeleteEvent(child
);
554 child
->DeleteChildren(tree
);
555 if (child
== tree
->m_select_me
)
556 tree
->m_select_me
= NULL
;
563 void wxGenericTreeItem::SetText( const wxString
&text
)
568 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
570 size_t count
= m_children
.Count();
574 size_t total
= count
;
575 for (size_t n
= 0; n
< count
; ++n
)
577 total
+= m_children
[n
]->GetChildrenCount();
583 void wxGenericTreeItem::GetSize( int &x
, int &y
,
584 const wxGenericTreeCtrl
*theButton
)
586 int bottomY
=m_y
+theButton
->GetLineHeight(this);
587 if ( y
< bottomY
) y
= bottomY
;
588 int width
= m_x
+ m_width
;
589 if ( x
< width
) x
= width
;
593 size_t count
= m_children
.Count();
594 for ( size_t n
= 0; n
< count
; ++n
)
596 m_children
[n
]->GetSize( x
, y
, theButton
);
601 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
602 const wxGenericTreeCtrl
*theCtrl
,
606 // for a hidden root node, don't evaluate it, but do evaluate children
607 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
610 int h
= theCtrl
->GetLineHeight(this);
611 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
613 int y_mid
= m_y
+ h
/2;
614 if (point
.y
< y_mid
)
615 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
617 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
619 int xCross
= m_x
- theCtrl
->GetSpacing();
621 // according to the drawing code the triangels are drawn
622 // at -4 , -4 from the position up to +10/+10 max
623 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
624 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
625 HasPlus() && theCtrl
->HasButtons() )
627 // 5 is the size of the plus sign
628 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
629 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
630 HasPlus() && theCtrl
->HasButtons() )
633 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
637 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
642 // assuming every image (normal and selected) has the same size!
643 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
644 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
647 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
648 flags
|= wxTREE_HITTEST_ONITEMICON
;
650 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
656 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
657 if (point
.x
> m_x
+m_width
)
658 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
663 // if children are expanded, fall through to evaluate them
664 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
668 size_t count
= m_children
.Count();
669 for ( size_t n
= 0; n
< count
; n
++ )
671 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
679 return (wxGenericTreeItem
*) NULL
;
682 int wxGenericTreeItem::GetCurrentImage() const
684 int image
= NO_IMAGE
;
689 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
692 if ( image
== NO_IMAGE
)
694 // we usually fall back to the normal item, but try just the
695 // expanded one (and not selected) first in this case
696 image
= GetImage(wxTreeItemIcon_Expanded
);
702 image
= GetImage(wxTreeItemIcon_Selected
);
705 // maybe it doesn't have the specific image we want,
706 // try the default one instead
707 if ( image
== NO_IMAGE
) image
= GetImage();
712 // -----------------------------------------------------------------------------
713 // wxGenericTreeCtrl implementation
714 // -----------------------------------------------------------------------------
716 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
718 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
719 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
720 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
721 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
722 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
723 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
724 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
727 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
729 * wxTreeCtrl has to be a real class or we have problems with
730 * the run-time information.
733 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
736 // -----------------------------------------------------------------------------
737 // construction/destruction
738 // -----------------------------------------------------------------------------
740 void wxGenericTreeCtrl::Init()
745 m_select_me
= (wxGenericTreeItem
*) NULL
;
753 m_hilightBrush
= new wxBrush
755 wxSystemSettings::GetColour
757 wxSYS_COLOUR_HIGHLIGHT
762 m_hilightUnfocusedBrush
= new wxBrush
764 wxSystemSettings::GetColour
766 wxSYS_COLOUR_BTNSHADOW
771 m_imageListButtons
= NULL
;
772 m_ownsImageListButtons
= false;
775 m_isDragging
= false;
776 m_dropTarget
= m_oldSelection
= NULL
;
780 m_renameTimer
= NULL
;
785 m_dropEffectAboveItem
= false;
787 m_lastOnSame
= false;
789 #ifdef __WXMAC_CARBON__
790 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
792 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
794 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
795 m_normalFont
.GetFamily(),
796 m_normalFont
.GetStyle(),
798 m_normalFont
.GetUnderlined(),
799 m_normalFont
.GetFaceName(),
800 m_normalFont
.GetEncoding());
803 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
808 const wxValidator
& validator
,
809 const wxString
& name
)
813 wxGetOsVersion( &major
, &minor
);
815 style
&= ~wxTR_LINES_AT_ROOT
;
816 style
|= wxTR_NO_LINES
;
818 style
|= wxTR_ROW_LINES
;
821 if ( !wxControl::Create( parent
, id
, pos
, size
,
822 style
|wxHSCROLL
|wxVSCROLL
,
827 // If the tree display has no buttons, but does have
828 // connecting lines, we can use a narrower layout.
829 // It may not be a good idea to force this...
830 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
836 wxVisualAttributes attr
= GetDefaultAttributes();
837 SetOwnForegroundColour( attr
.colFg
);
838 SetOwnBackgroundColour( attr
.colBg
);
840 SetOwnFont(attr
.font
);
842 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
849 wxGenericTreeCtrl::~wxGenericTreeCtrl()
851 delete m_hilightBrush
;
852 delete m_hilightUnfocusedBrush
;
856 delete m_renameTimer
;
859 if (m_ownsImageListButtons
)
860 delete m_imageListButtons
;
863 // -----------------------------------------------------------------------------
865 // -----------------------------------------------------------------------------
867 size_t wxGenericTreeCtrl::GetCount() const
875 size_t count
= m_anchor
->GetChildrenCount();
876 if ( !HasFlag(wxTR_HIDE_ROOT
) )
878 // take the root itself into account
885 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
887 m_indent
= (unsigned short) indent
;
892 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
893 bool recursively
) const
895 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
897 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
900 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
902 // Do not try to expand the root node if it hasn't been created yet
903 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
905 // if we will hide the root, make sure children are visible
906 m_anchor
->SetHasPlus();
908 CalculatePositions();
911 // right now, just sets the styles. Eventually, we may
912 // want to update the inherited styles, but right now
913 // none of the parents has updatable styles
914 m_windowStyle
= styles
;
918 // -----------------------------------------------------------------------------
919 // functions to work with tree items
920 // -----------------------------------------------------------------------------
922 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
924 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
926 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
929 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
930 wxTreeItemIcon which
) const
932 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
934 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
937 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
939 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
941 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
944 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
946 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
948 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
949 return pItem
->Attr().GetTextColour();
952 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
954 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
956 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
957 return pItem
->Attr().GetBackgroundColour();
960 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
962 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
964 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
965 return pItem
->Attr().GetFont();
968 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
970 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
973 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
974 pItem
->SetText(text
);
975 CalculateSize(pItem
, dc
);
979 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
981 wxTreeItemIcon which
)
983 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
985 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
986 pItem
->SetImage(image
, which
);
989 CalculateSize(pItem
, dc
);
993 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
995 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1000 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1003 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1005 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1007 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1008 pItem
->SetHasPlus(has
);
1012 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1014 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1016 // avoid redrawing the tree if no real change
1017 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1018 if ( pItem
->IsBold() != bold
)
1020 pItem
->SetBold(bold
);
1025 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1028 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1034 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1035 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1038 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1039 pItem
->Attr().SetTextColour(fg
);
1040 pItem
->Attr().SetBackgroundColour(bg
);
1044 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1045 const wxColour
& col
)
1047 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1049 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1050 pItem
->Attr().SetTextColour(col
);
1054 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1055 const wxColour
& col
)
1057 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1059 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1060 pItem
->Attr().SetBackgroundColour(col
);
1064 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1066 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1068 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1069 pItem
->Attr().SetFont(font
);
1073 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1075 wxTreeCtrlBase::SetFont(font
);
1077 m_normalFont
= font
;
1078 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1079 m_normalFont
.GetFamily(),
1080 m_normalFont
.GetStyle(),
1082 m_normalFont
.GetUnderlined(),
1083 m_normalFont
.GetFaceName(),
1084 m_normalFont
.GetEncoding());
1090 // -----------------------------------------------------------------------------
1091 // item status inquiries
1092 // -----------------------------------------------------------------------------
1094 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1096 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1098 // An item is only visible if it's not a descendant of a collapsed item
1099 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1100 wxGenericTreeItem
* parent
= pItem
->GetParent();
1103 if (!parent
->IsExpanded())
1105 parent
= parent
->GetParent();
1109 GetViewStart(& startX
, & startY
);
1111 wxSize clientSize
= GetClientSize();
1114 if (!GetBoundingRect(item
, rect
))
1116 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1118 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1120 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1126 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1128 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1130 // consider that the item does have children if it has the "+" button: it
1131 // might not have them (if it had never been expanded yet) but then it
1132 // could have them as well and it's better to err on this side rather than
1133 // disabling some operations which are restricted to the items with
1134 // children for an item which does have them
1135 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1138 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1140 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1142 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1145 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1147 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1149 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1152 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1154 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1156 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1159 // -----------------------------------------------------------------------------
1161 // -----------------------------------------------------------------------------
1163 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1165 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1167 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1170 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1171 wxTreeItemIdValue
& cookie
) const
1173 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1176 return GetNextChild(item
, cookie
);
1179 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1180 wxTreeItemIdValue
& cookie
) const
1182 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1184 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1186 // it's ok to cast cookie to size_t, we never have indices big enough to
1187 // overflow "void *"
1188 size_t *pIndex
= (size_t *)&cookie
;
1189 if ( *pIndex
< children
.Count() )
1191 return children
.Item((*pIndex
)++);
1195 // there are no more of them
1196 return wxTreeItemId();
1200 #if WXWIN_COMPATIBILITY_2_4
1202 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1205 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1208 return GetNextChild(item
, cookie
);
1211 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1214 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1216 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1217 if ( (size_t)cookie
< children
.Count() )
1219 return children
.Item((size_t)cookie
++);
1223 // there are no more of them
1224 return wxTreeItemId();
1228 #endif // WXWIN_COMPATIBILITY_2_4
1230 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1232 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1234 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1235 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1238 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1240 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1242 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1243 wxGenericTreeItem
*parent
= i
->GetParent();
1244 if ( parent
== NULL
)
1246 // root item doesn't have any siblings
1247 return wxTreeItemId();
1250 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1251 int index
= siblings
.Index(i
);
1252 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1254 size_t n
= (size_t)(index
+ 1);
1255 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1258 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1260 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1262 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1263 wxGenericTreeItem
*parent
= i
->GetParent();
1264 if ( parent
== NULL
)
1266 // root item doesn't have any siblings
1267 return wxTreeItemId();
1270 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1271 int index
= siblings
.Index(i
);
1272 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1274 return index
== 0 ? wxTreeItemId()
1275 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1278 // Only for internal use right now, but should probably be public
1279 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1281 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1283 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1285 // First see if there are any children.
1286 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1287 if (children
.GetCount() > 0)
1289 return children
.Item(0);
1293 // Try a sibling of this or ancestor instead
1294 wxTreeItemId p
= item
;
1295 wxTreeItemId toFind
;
1298 toFind
= GetNextSibling(p
);
1299 p
= GetItemParent(p
);
1300 } while (p
.IsOk() && !toFind
.IsOk());
1305 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1307 wxTreeItemId id
= GetRootItem();
1316 } while (id
.IsOk());
1318 return wxTreeItemId();
1321 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1323 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1325 wxTreeItemId id
= item
;
1328 while (id
= GetNext(id
), id
.IsOk())
1334 return wxTreeItemId();
1337 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1339 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1341 wxFAIL_MSG(wxT("not implemented"));
1343 return wxTreeItemId();
1346 // called by wxTextTreeCtrl when it marks itself for deletion
1347 void wxGenericTreeCtrl::ResetTextControl()
1352 // find the first item starting with the given prefix after the given item
1353 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1354 const wxString
& prefixOrig
) const
1356 // match is case insensitive as this is more convenient to the user: having
1357 // to press Shift-letter to go to the item starting with a capital letter
1358 // would be too bothersome
1359 wxString prefix
= prefixOrig
.Lower();
1361 // determine the starting point: we shouldn't take the current item (this
1362 // allows to switch between two items starting with the same letter just by
1363 // pressing it) but we shouldn't jump to the next one if the user is
1364 // continuing to type as otherwise he might easily skip the item he wanted
1365 wxTreeItemId id
= idParent
;
1366 if ( prefix
.length() == 1 )
1371 // look for the item starting with the given prefix after it
1372 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1377 // if we haven't found anything...
1380 // ... wrap to the beginning
1382 if ( HasFlag(wxTR_HIDE_ROOT
) )
1384 // can't select virtual root
1388 // and try all the items (stop when we get to the one we started from)
1389 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1398 // -----------------------------------------------------------------------------
1400 // -----------------------------------------------------------------------------
1402 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1404 const wxString
& text
,
1407 wxTreeItemData
*data
)
1409 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1412 // should we give a warning here?
1413 return AddRoot(text
, image
, selImage
, data
);
1416 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1418 wxGenericTreeItem
*item
=
1419 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1423 data
->m_pItem
= item
;
1426 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1432 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1435 wxTreeItemData
*data
)
1437 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1439 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1441 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1442 image
, selImage
, data
);
1445 data
->m_pItem
= m_anchor
;
1448 if (HasFlag(wxTR_HIDE_ROOT
))
1450 // if root is hidden, make sure we can navigate
1452 m_anchor
->SetHasPlus();
1454 CalculatePositions();
1457 if (!HasFlag(wxTR_MULTIPLE
))
1459 m_current
= m_key_current
= m_anchor
;
1460 m_current
->SetHilight( true );
1466 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1467 const wxTreeItemId
& idPrevious
,
1468 const wxString
& text
,
1469 int image
, int selImage
,
1470 wxTreeItemData
*data
)
1472 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1475 // should we give a warning here?
1476 return AddRoot(text
, image
, selImage
, data
);
1480 if (idPrevious
.IsOk())
1482 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1483 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1484 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1487 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1491 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1493 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1494 event
.m_item
= item
;
1495 event
.SetEventObject( this );
1496 ProcessEvent( event
);
1499 // Don't leave edit or selection on a child which is about to disappear
1500 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1502 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1503 m_textCtrl
->StopEditing();
1505 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1506 m_key_current
= NULL
;
1508 if (IsDescendantOf(item
, m_select_me
)) {
1511 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1512 m_current
->SetHilight( false );
1518 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1520 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1522 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1523 ChildrenClosing(item
);
1524 item
->DeleteChildren(this);
1527 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1529 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1531 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1533 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1535 // can't delete the item being edited, cancel editing it first
1536 m_textCtrl
->StopEditing();
1539 wxGenericTreeItem
*parent
= item
->GetParent();
1541 // don't keep stale pointers around!
1542 if ( IsDescendantOf(item
, m_key_current
) )
1544 // Don't silently change the selection:
1545 // do it properly in idle time, so event
1546 // handlers get called.
1548 // m_key_current = parent;
1549 m_key_current
= NULL
;
1552 // m_select_me records whether we need to select
1553 // a different item, in idle time.
1554 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1556 m_select_me
= parent
;
1559 if ( IsDescendantOf(item
, m_current
) )
1561 // Don't silently change the selection:
1562 // do it properly in idle time, so event
1563 // handlers get called.
1565 // m_current = parent;
1567 m_select_me
= parent
;
1570 // remove the item from the tree
1573 parent
->GetChildren().Remove( item
); // remove by value
1575 else // deleting the root
1577 // nothing will be left in the tree
1581 // and delete all of its children and the item itself now
1582 item
->DeleteChildren(this);
1583 SendDeleteEvent(item
);
1585 if (item
== m_select_me
)
1591 void wxGenericTreeCtrl::DeleteAllItems()
1599 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1601 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1603 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1604 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1605 _T("can't expand hidden root") );
1607 if ( !item
->HasPlus() )
1610 if ( item
->IsExpanded() )
1613 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1614 event
.m_item
= item
;
1615 event
.SetEventObject( this );
1617 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1619 // cancelled by program
1624 CalculatePositions();
1626 RefreshSubtree(item
);
1628 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1629 ProcessEvent( event
);
1632 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1634 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1637 if ( !IsExpanded(item
) )
1641 wxTreeItemIdValue cookie
;
1642 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1643 while ( child
.IsOk() )
1647 child
= GetNextChild(item
, cookie
);
1651 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1653 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1654 _T("can't collapse hidden root") );
1656 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1658 if ( !item
->IsExpanded() )
1661 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1662 event
.m_item
= item
;
1663 event
.SetEventObject( this );
1664 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1666 // cancelled by program
1670 ChildrenClosing(item
);
1673 #if 0 // TODO why should items be collapsed recursively?
1674 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1675 size_t count
= children
.Count();
1676 for ( size_t n
= 0; n
< count
; n
++ )
1678 Collapse(children
[n
]);
1682 CalculatePositions();
1684 RefreshSubtree(item
);
1686 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1687 ProcessEvent( event
);
1690 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1693 DeleteChildren(item
);
1696 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1698 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1700 if (item
->IsExpanded())
1706 void wxGenericTreeCtrl::Unselect()
1710 m_current
->SetHilight( false );
1711 RefreshLine( m_current
);
1718 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1720 if (item
->IsSelected())
1722 item
->SetHilight(false);
1726 if (item
->HasChildren())
1728 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1729 size_t count
= children
.Count();
1730 for ( size_t n
= 0; n
< count
; ++n
)
1732 UnselectAllChildren(children
[n
]);
1737 void wxGenericTreeCtrl::UnselectAll()
1739 wxTreeItemId rootItem
= GetRootItem();
1741 // the tree might not have the root item at all
1744 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1748 // Recursive function !
1749 // To stop we must have crt_item<last_item
1751 // Tag all next children, when no more children,
1752 // Move to parent (not to tag)
1753 // Keep going... if we found last_item, we stop.
1754 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1756 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1758 if (parent
== NULL
) // This is root item
1759 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1761 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1762 int index
= children
.Index(crt_item
);
1763 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1765 size_t count
= children
.Count();
1766 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1768 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1771 return TagNextChildren(parent
, last_item
, select
);
1774 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1776 crt_item
->SetHilight(select
);
1777 RefreshLine(crt_item
);
1779 if (crt_item
==last_item
)
1782 if (crt_item
->HasChildren())
1784 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1785 size_t count
= children
.Count();
1786 for ( size_t n
= 0; n
< count
; ++n
)
1788 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1796 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1800 // item2 is not necessary after item1
1801 // choice first' and 'last' between item1 and item2
1802 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1803 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1805 bool select
= m_current
->IsSelected();
1807 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1810 TagNextChildren(first
,last
,select
);
1813 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1814 bool unselect_others
,
1815 bool extended_select
)
1817 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1821 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1822 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1824 //wxCHECK_RET( ( (!unselect_others) && is_single),
1825 // wxT("this is a single selection tree") );
1827 // to keep going anyhow !!!
1830 if (item
->IsSelected())
1831 return; // nothing to do
1832 unselect_others
= true;
1833 extended_select
= false;
1835 else if ( unselect_others
&& item
->IsSelected() )
1837 // selection change if there is more than one item currently selected
1838 wxArrayTreeItemIds selected_items
;
1839 if ( GetSelections(selected_items
) == 1 )
1843 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1844 event
.m_item
= item
;
1845 event
.m_itemOld
= m_current
;
1846 event
.SetEventObject( this );
1847 // TODO : Here we don't send any selection mode yet !
1849 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1852 wxTreeItemId parent
= GetItemParent( itemId
);
1853 while (parent
.IsOk())
1855 if (!IsExpanded(parent
))
1858 parent
= GetItemParent( parent
);
1861 EnsureVisible( itemId
);
1864 if (unselect_others
)
1866 if (is_single
) Unselect(); // to speed up thing
1871 if (extended_select
)
1875 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1878 // don't change the mark (m_current)
1879 SelectItemRange(m_current
, item
);
1883 bool select
= true; // the default
1885 // Check if we need to toggle hilight (ctrl mode)
1886 if (!unselect_others
)
1887 select
=!item
->IsSelected();
1889 m_current
= m_key_current
= item
;
1890 m_current
->SetHilight(select
);
1891 RefreshLine( m_current
);
1894 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1895 GetEventHandler()->ProcessEvent( event
);
1898 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1902 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1906 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1907 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1909 item
->SetHilight(false);
1914 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1915 wxArrayTreeItemIds
&array
) const
1917 if ( item
->IsSelected() )
1918 array
.Add(wxTreeItemId(item
));
1920 if ( item
->HasChildren() )
1922 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1923 size_t count
= children
.GetCount();
1924 for ( size_t n
= 0; n
< count
; ++n
)
1925 FillArray(children
[n
], array
);
1929 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1932 wxTreeItemId idRoot
= GetRootItem();
1933 if ( idRoot
.IsOk() )
1935 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1937 //else: the tree is empty, so no selections
1939 return array
.Count();
1942 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1944 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1946 if (!item
.IsOk()) return;
1948 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1950 // first expand all parent branches
1951 wxGenericTreeItem
*parent
= gitem
->GetParent();
1953 if ( HasFlag(wxTR_HIDE_ROOT
) )
1955 while ( parent
&& parent
!= m_anchor
)
1958 parent
= parent
->GetParent();
1966 parent
= parent
->GetParent();
1970 //if (parent) CalculatePositions();
1975 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1977 if (!item
.IsOk()) return;
1979 // We have to call this here because the label in
1980 // question might just have been added and no screen
1981 // update taken place.
1983 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1988 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1990 // now scroll to the item
1991 int item_y
= gitem
->GetY();
1995 GetViewStart( &start_x
, &start_y
);
1996 start_y
*= PIXELS_PER_UNIT
;
2000 GetClientSize( &client_w
, &client_h
);
2002 if (item_y
< start_y
+3)
2007 m_anchor
->GetSize( x
, y
, this );
2008 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2009 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2010 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2011 // Item should appear at top
2012 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2014 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2019 m_anchor
->GetSize( x
, y
, this );
2020 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2021 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2022 item_y
+= PIXELS_PER_UNIT
+2;
2023 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2024 // Item should appear at bottom
2025 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
);
2029 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2030 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2032 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2033 wxGenericTreeItem
**item2
)
2035 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2037 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2040 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2042 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2044 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2046 wxCHECK_RET( !s_treeBeingSorted
,
2047 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2049 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2050 if ( children
.Count() > 1 )
2054 s_treeBeingSorted
= this;
2055 children
.Sort(tree_ctrl_compare_func
);
2056 s_treeBeingSorted
= NULL
;
2058 //else: don't make the tree dirty as nothing changed
2061 void wxGenericTreeCtrl::CalculateLineHeight()
2063 wxClientDC
dc(this);
2064 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2066 if ( m_imageListNormal
)
2068 // Calculate a m_lineHeight value from the normal Image sizes.
2069 // May be toggle off. Then wxGenericTreeCtrl will spread when
2070 // necessary (which might look ugly).
2071 int n
= m_imageListNormal
->GetImageCount();
2072 for (int i
= 0; i
< n
; i
++)
2074 int width
= 0, height
= 0;
2075 m_imageListNormal
->GetSize(i
, width
, height
);
2076 if (height
> m_lineHeight
) m_lineHeight
= height
;
2080 if (m_imageListButtons
)
2082 // Calculate a m_lineHeight value from the Button image sizes.
2083 // May be toggle off. Then wxGenericTreeCtrl will spread when
2084 // necessary (which might look ugly).
2085 int n
= m_imageListButtons
->GetImageCount();
2086 for (int i
= 0; i
< n
; i
++)
2088 int width
= 0, height
= 0;
2089 m_imageListButtons
->GetSize(i
, width
, height
);
2090 if (height
> m_lineHeight
) m_lineHeight
= height
;
2094 if (m_lineHeight
< 30)
2095 m_lineHeight
+= 2; // at least 2 pixels
2097 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2100 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2102 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2103 m_imageListNormal
= imageList
;
2104 m_ownsImageListNormal
= false;
2106 // Don't do any drawing if we're setting the list to NULL,
2107 // since we may be in the process of deleting the tree control.
2109 CalculateLineHeight();
2112 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2114 if (m_ownsImageListState
) delete m_imageListState
;
2115 m_imageListState
= imageList
;
2116 m_ownsImageListState
= false;
2119 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2121 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2122 m_imageListButtons
= imageList
;
2123 m_ownsImageListButtons
= false;
2125 CalculateLineHeight();
2128 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2130 SetButtonsImageList(imageList
);
2131 m_ownsImageListButtons
= true;
2134 // -----------------------------------------------------------------------------
2136 // -----------------------------------------------------------------------------
2138 void wxGenericTreeCtrl::AdjustMyScrollbars()
2143 m_anchor
->GetSize( x
, y
, this );
2144 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2145 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2146 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2147 int y_pos
= GetScrollPos( wxVERTICAL
);
2148 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2152 SetScrollbars( 0, 0, 0, 0 );
2156 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2158 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2159 return item
->GetHeight();
2161 return m_lineHeight
;
2164 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2166 // TODO implement "state" icon on items
2168 wxTreeItemAttr
*attr
= item
->GetAttributes();
2169 if ( attr
&& attr
->HasFont() )
2170 dc
.SetFont(attr
->GetFont());
2171 else if (item
->IsBold())
2172 dc
.SetFont(m_boldFont
);
2174 long text_w
= 0, text_h
= 0;
2175 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2177 int image_h
= 0, image_w
= 0;
2178 int image
= item
->GetCurrentImage();
2179 if ( image
!= NO_IMAGE
)
2181 if ( m_imageListNormal
)
2183 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2184 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2192 int total_h
= GetLineHeight(item
);
2193 bool drawItemBackground
= false;
2195 if ( item
->IsSelected() )
2197 // under mac selections are only a rectangle in case they don't have the focus
2201 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2202 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2206 dc
.SetBrush( *m_hilightBrush
) ;
2209 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2211 drawItemBackground
= true;
2216 if ( attr
&& attr
->HasBackgroundColour() )
2218 drawItemBackground
= true;
2219 colBg
= attr
->GetBackgroundColour();
2223 colBg
= GetBackgroundColour();
2225 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2228 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2230 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2234 DoGetPosition(&x
, &y
);
2236 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2240 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2242 // If it's selected, and there's an image, then we should
2243 // take care to leave the area under the image painted in the
2244 // background colour.
2245 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2246 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2248 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2249 // don't allow backgrounds to be customized. Not drawing the background,
2250 // except for custom item backgrounds, works for both kinds of theme.
2251 else if (drawItemBackground
)
2253 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2254 item
->GetWidth()+2, total_h
-offset
);
2258 if ( image
!= NO_IMAGE
)
2260 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2261 m_imageListNormal
->Draw( image
, dc
,
2263 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2264 wxIMAGELIST_DRAW_TRANSPARENT
);
2265 dc
.DestroyClippingRegion();
2268 dc
.SetBackgroundMode(wxTRANSPARENT
);
2269 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2270 dc
.DrawText( item
->GetText(),
2271 (wxCoord
)(image_w
+ item
->GetX()),
2272 (wxCoord
)(item
->GetY() + extraH
));
2274 // restore normal font
2275 dc
.SetFont( m_normalFont
);
2278 // Now y stands for the top of the item, whereas it used to stand for middle !
2279 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2281 int x
= level
*m_indent
;
2282 if (!HasFlag(wxTR_HIDE_ROOT
))
2286 else if (level
== 0)
2288 // always expand hidden root
2290 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2291 int count
= children
.Count();
2297 PaintLevel(children
[n
], dc
, 1, y
);
2298 } while (++n
< count
);
2300 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2302 // draw line down to last child
2303 origY
+= GetLineHeight(children
[0])>>1;
2304 oldY
+= GetLineHeight(children
[n
-1])>>1;
2305 dc
.DrawLine(3, origY
, 3, oldY
);
2311 item
->SetX(x
+m_spacing
);
2314 int h
= GetLineHeight(item
);
2316 int y_mid
= y_top
+ (h
>>1);
2319 int exposed_x
= dc
.LogicalToDeviceX(0);
2320 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2322 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2326 // don't draw rect outline if we already have the
2327 // background color under Mac
2328 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2329 #endif // !__WXMAC__
2333 if ( item
->IsSelected() )
2335 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2339 wxTreeItemAttr
*attr
= item
->GetAttributes();
2340 if (attr
&& attr
->HasTextColour())
2341 colText
= attr
->GetTextColour();
2343 colText
= GetForegroundColour();
2347 dc
.SetTextForeground(colText
);
2351 PaintItem(item
, dc
);
2353 if (HasFlag(wxTR_ROW_LINES
))
2355 // if the background colour is white, choose a
2356 // contrasting color for the lines
2357 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2358 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2359 dc
.DrawLine(0, y_top
, 10000, y_top
);
2360 dc
.DrawLine(0, y
, 10000, y
);
2363 // restore DC objects
2364 dc
.SetBrush(*wxWHITE_BRUSH
);
2365 dc
.SetPen(m_dottedPen
);
2366 dc
.SetTextForeground(*wxBLACK
);
2368 if ( !HasFlag(wxTR_NO_LINES
) )
2370 // draw the horizontal line here
2372 if (x
> (signed)m_indent
)
2373 x_start
-= m_indent
;
2374 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2376 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2379 // should the item show a button?
2380 if ( item
->HasPlus() && HasButtons() )
2382 if ( m_imageListButtons
)
2384 // draw the image button here
2387 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2388 : wxTreeItemIcon_Normal
;
2389 if ( item
->IsSelected() )
2390 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2392 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2393 int xx
= x
- image_w
/2;
2394 int yy
= y_mid
- image_h
/2;
2396 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2397 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2398 wxIMAGELIST_DRAW_TRANSPARENT
);
2400 else // no custom buttons
2402 static const int wImage
= 9;
2403 static const int hImage
= 9;
2406 if (item
->IsExpanded())
2407 flag
|= wxCONTROL_EXPANDED
;
2408 if (item
== m_underMouse
)
2409 flag
|= wxCONTROL_CURRENT
;
2411 wxRendererNative::Get().DrawTreeItemButton
2415 wxRect(x
- wImage
/2,
2424 if (item
->IsExpanded())
2426 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2427 int count
= children
.Count();
2434 PaintLevel(children
[n
], dc
, level
, y
);
2435 } while (++n
< count
);
2437 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2439 // draw line down to last child
2440 oldY
+= GetLineHeight(children
[n
-1])>>1;
2441 if (HasButtons()) y_mid
+= 5;
2443 // Only draw the portion of the line that is visible, in case it is huge
2444 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2445 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2446 yOrigin
= abs(yOrigin
);
2447 GetClientSize(&width
, &height
);
2449 // Move end points to the begining/end of the view?
2450 if (y_mid
< yOrigin
)
2452 if (oldY
> yOrigin
+ height
)
2453 oldY
= yOrigin
+ height
;
2455 // after the adjustments if y_mid is larger than oldY then the line
2456 // isn't visible at all so don't draw anything
2458 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2464 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2468 if ( item
->HasPlus() )
2470 // it's a folder, indicate it by a border
2475 // draw a line under the drop target because the item will be
2477 DrawLine(item
, !m_dropEffectAboveItem
);
2480 SetCursor(wxCURSOR_BULLSEYE
);
2485 SetCursor(wxCURSOR_NO_ENTRY
);
2489 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2491 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2493 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2495 wxClientDC
dc(this);
2497 dc
.SetLogicalFunction(wxINVERT
);
2498 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2500 int w
= i
->GetWidth() + 2;
2501 int h
= GetLineHeight(i
) + 2;
2503 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2506 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2508 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2510 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2512 wxClientDC
dc(this);
2514 dc
.SetLogicalFunction(wxINVERT
);
2520 y
+= GetLineHeight(i
) - 1;
2523 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2526 // -----------------------------------------------------------------------------
2527 // wxWidgets callbacks
2528 // -----------------------------------------------------------------------------
2530 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2538 dc
.SetFont( m_normalFont
);
2539 dc
.SetPen( m_dottedPen
);
2541 // this is now done dynamically
2542 //if(GetImageList() == NULL)
2543 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2546 PaintLevel( m_anchor
, dc
, 0, y
);
2549 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2558 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2567 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2569 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2570 te
.m_evtKey
= event
;
2571 te
.SetEventObject( this );
2572 if ( GetEventHandler()->ProcessEvent( te
) )
2574 // intercepted by the user code
2578 if ( (m_current
== 0) || (m_key_current
== 0) )
2584 // how should the selection work for this event?
2585 bool is_multiple
, extended_select
, unselect_others
;
2586 EventFlagsToSelType(GetWindowStyleFlag(),
2589 is_multiple
, extended_select
, unselect_others
);
2593 // * : Expand all/Collapse all
2594 // ' ' | return : activate
2595 // up : go up (not last children!)
2597 // left : go to parent
2598 // right : open if parent and go next
2599 // home : go to root
2600 // end : go to last item without opening parents
2601 // alnum : start or continue searching for the item with this prefix
2602 int keyCode
= event
.GetKeyCode();
2607 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2615 if ( !IsExpanded(m_current
) )
2618 ExpandAll(m_current
);
2621 //else: fall through to Collapse() it
2625 if (IsExpanded(m_current
))
2627 Collapse(m_current
);
2633 // Use the item's bounding rectangle to determine position for the event
2635 GetBoundingRect(m_current
, ItemRect
, true);
2637 wxTreeEvent
eventMenu( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2638 eventMenu
.m_item
= m_current
;
2639 // Use the left edge, vertical middle
2640 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2641 ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2642 eventMenu
.SetEventObject( this );
2643 GetEventHandler()->ProcessEvent( eventMenu
);
2648 if ( !event
.HasModifiers() )
2650 wxTreeEvent
eventAct( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2651 eventAct
.m_item
= m_current
;
2652 eventAct
.SetEventObject( this );
2653 GetEventHandler()->ProcessEvent( eventAct
);
2656 // in any case, also generate the normal key event for this key,
2657 // even if we generated the ACTIVATED event above: this is what
2658 // wxMSW does and it makes sense because you might not want to
2659 // process ACTIVATED event at all and handle Space and Return
2660 // directly (and differently) which would be impossible otherwise
2664 // up goes to the previous sibling or to the last
2665 // of its children if it's expanded
2668 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2671 prev
= GetItemParent( m_key_current
);
2672 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2674 break; // don't go to root if it is hidden
2678 wxTreeItemIdValue cookie
;
2679 wxTreeItemId current
= m_key_current
;
2680 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2681 if (current
== GetFirstChild( prev
, cookie
))
2683 // otherwise we return to where we came from
2684 DoSelectItem( prev
, unselect_others
, extended_select
);
2685 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2692 while ( IsExpanded(prev
) && HasChildren(prev
) )
2694 wxTreeItemId child
= GetLastChild(prev
);
2701 DoSelectItem( prev
, unselect_others
, extended_select
);
2702 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2707 // left arrow goes to the parent
2710 wxTreeItemId prev
= GetItemParent( m_current
);
2711 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2713 // don't go to root if it is hidden
2714 prev
= GetPrevSibling( m_current
);
2718 DoSelectItem( prev
, unselect_others
, extended_select
);
2724 // this works the same as the down arrow except that we
2725 // also expand the item if it wasn't expanded yet
2731 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2733 wxTreeItemIdValue cookie
;
2734 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2735 DoSelectItem( child
, unselect_others
, extended_select
);
2736 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2740 wxTreeItemId next
= GetNextSibling( m_key_current
);
2743 wxTreeItemId current
= m_key_current
;
2744 while (current
.IsOk() && !next
)
2746 current
= GetItemParent( current
);
2747 if (current
) next
= GetNextSibling( current
);
2752 DoSelectItem( next
, unselect_others
, extended_select
);
2753 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2759 // <End> selects the last visible tree item
2762 wxTreeItemId last
= GetRootItem();
2764 while ( last
.IsOk() && IsExpanded(last
) )
2766 wxTreeItemId lastChild
= GetLastChild(last
);
2768 // it may happen if the item was expanded but then all of
2769 // its children have been deleted - so IsExpanded() returned
2770 // true, but GetLastChild() returned invalid item
2779 DoSelectItem( last
, unselect_others
, extended_select
);
2784 // <Home> selects the root item
2787 wxTreeItemId prev
= GetRootItem();
2791 if ( HasFlag(wxTR_HIDE_ROOT
) )
2793 wxTreeItemIdValue cookie
;
2794 prev
= GetFirstChild(prev
, cookie
);
2799 DoSelectItem( prev
, unselect_others
, extended_select
);
2804 // do not use wxIsalnum() here
2805 if ( !event
.HasModifiers() &&
2806 ((keyCode
>= '0' && keyCode
<= '9') ||
2807 (keyCode
>= 'a' && keyCode
<= 'z') ||
2808 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2810 // find the next item starting with the given prefix
2811 wxChar ch
= (wxChar
)keyCode
;
2813 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2824 // also start the timer to reset the current prefix if the user
2825 // doesn't press any more alnum keys soon -- we wouldn't want
2826 // to use this prefix for a new item search
2829 m_findTimer
= new wxTreeFindTimer(this);
2832 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2841 wxTreeItemId
wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
)
2843 // JACS: removed wxYieldIfNeeded() because it can cause the window
2844 // to be deleted from under us if a close window event is pending
2849 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2850 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2851 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2852 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2853 if (flags
) return wxTreeItemId();
2855 if (m_anchor
== NULL
)
2857 flags
= wxTREE_HITTEST_NOWHERE
;
2858 return wxTreeItemId();
2861 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2865 flags
= wxTREE_HITTEST_NOWHERE
;
2866 return wxTreeItemId();
2871 // get the bounding rectangle of the item (or of its label only)
2872 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2874 bool textOnly
) const
2876 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2878 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2881 GetViewStart(& startX
, & startY
);
2885 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2886 rect
.width
= i
->GetWidth();
2888 if ( m_imageListNormal
)
2890 int image_w
, image_h
;
2891 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
2892 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2895 else // the entire line
2898 rect
.width
= GetClientSize().x
;
2901 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2902 rect
.height
= GetLineHeight(i
);
2907 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2908 wxClassInfo
* WXUNUSED(textCtrlClass
))
2910 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
2912 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2914 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2915 te
.m_item
= itemEdit
;
2916 te
.SetEventObject( this );
2917 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2923 // We have to call this here because the label in
2924 // question might just have been added and no screen
2925 // update taken place.
2927 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2933 // TODO: use textCtrlClass here to create the control of correct class
2934 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2936 m_textCtrl
->SetFocus();
2941 // returns a pointer to the text edit control if the item is being
2942 // edited, NULL otherwise (it's assumed that no more than one item may
2943 // be edited simultaneously)
2944 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2949 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
2950 bool discardChanges
)
2952 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
2954 m_textCtrl
->EndEdit(discardChanges
);
2957 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2958 const wxString
& value
)
2960 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2962 le
.SetEventObject( this );
2964 le
.m_editCancelled
= false;
2966 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2969 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2971 // let owner know that the edit was cancelled
2972 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2974 le
.SetEventObject( this );
2975 le
.m_label
= wxEmptyString
;
2976 le
.m_editCancelled
= true;
2978 GetEventHandler()->ProcessEvent( le
);
2981 void wxGenericTreeCtrl::OnRenameTimer()
2983 EditLabel( m_current
);
2986 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2988 if ( !m_anchor
) return;
2990 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2992 // Is the mouse over a tree item button?
2994 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
2995 wxGenericTreeItem
*underMouse
= thisItem
;
2997 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
2998 #endif // wxUSE_TOOLTIPS
3001 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3002 (!event
.LeftIsDown()) &&
3004 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3012 if (underMouse
!= m_underMouse
)
3016 // unhighlight old item
3017 wxGenericTreeItem
*tmp
= m_underMouse
;
3018 m_underMouse
= NULL
;
3022 m_underMouse
= underMouse
;
3024 RefreshLine( m_underMouse
);
3028 // Determines what item we are hovering over and need a tooltip for
3029 wxTreeItemId hoverItem
= thisItem
;
3031 // We do not want a tooltip if we are dragging, or if the rename timer is running
3032 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3034 // Ask the tree control what tooltip (if any) should be shown
3035 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
3036 hevent
.m_item
= hoverItem
;
3037 hevent
.SetEventObject(this);
3039 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3041 SetToolTip(hevent
.m_label
);
3046 // we process left mouse up event (enables in-place edit), right down
3047 // (pass to the user code), left dbl click (activate item) and
3048 // dragging/moving events for items drag-and-drop
3049 if ( !(event
.LeftDown() ||
3051 event
.RightDown() ||
3052 event
.LeftDClick() ||
3054 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3063 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3065 if ( event
.Dragging() && !m_isDragging
)
3067 if (m_dragCount
== 0)
3072 if (m_dragCount
!= 3)
3074 // wait until user drags a bit further...
3078 wxEventType command
= event
.RightIsDown()
3079 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3080 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3082 wxTreeEvent
nevent( command
, GetId() );
3083 nevent
.m_item
= m_current
;
3084 nevent
.SetEventObject(this);
3085 nevent
.SetPoint(pt
);
3087 // by default the dragging is not supported, the user code must
3088 // explicitly allow the event for it to take place
3091 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3093 // we're going to drag this item
3094 m_isDragging
= true;
3096 // remember the old cursor because we will change it while
3098 m_oldCursor
= m_cursor
;
3100 // in a single selection control, hide the selection temporarily
3101 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3103 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3105 if ( m_oldSelection
)
3107 m_oldSelection
->SetHilight(false);
3108 RefreshLine(m_oldSelection
);
3115 else if ( event
.Dragging() )
3117 if ( item
!= m_dropTarget
)
3119 // unhighlight the previous drop target
3120 DrawDropEffect(m_dropTarget
);
3122 m_dropTarget
= item
;
3124 // highlight the current drop target if any
3125 DrawDropEffect(m_dropTarget
);
3127 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3134 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3136 // erase the highlighting
3137 DrawDropEffect(m_dropTarget
);
3139 if ( m_oldSelection
)
3141 m_oldSelection
->SetHilight(true);
3142 RefreshLine(m_oldSelection
);
3143 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3146 // generate the drag end event
3147 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3149 eventEndDrag
.m_item
= item
;
3150 eventEndDrag
.m_pointDrag
= pt
;
3151 eventEndDrag
.SetEventObject(this);
3153 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3155 m_isDragging
= false;
3156 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3160 SetCursor(m_oldCursor
);
3162 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3170 // If we got to this point, we are not dragging or moving the mouse.
3171 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3172 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3173 // We skip even if we didn't hit an item because we still should
3174 // restore focus to the tree control even if we didn't exactly hit an item.
3175 if ( event
.LeftDown() )
3180 // here we process only the messages which happen on tree items
3184 if (item
== NULL
) return; /* we hit the blank area */
3186 if ( event
.RightDown() )
3188 // If the item is already selected, do not update the selection.
3189 // Multi-selections should not be cleared if a selected item is clicked.
3190 if (!IsSelected(item
))
3192 DoSelectItem(item
, true, false);
3195 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3196 nevent
.m_item
= item
;
3197 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3198 nevent
.SetEventObject(this);
3199 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3201 // Consistent with MSW (for now), send the ITEM_MENU *after*
3202 // the RIGHT_CLICK event. TODO: This behavior may change.
3203 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, GetId());
3204 nevent2
.m_item
= item
;
3205 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3206 nevent2
.SetEventObject(this);
3207 GetEventHandler()->ProcessEvent(nevent2
);
3209 else if ( event
.LeftUp() )
3211 // this facilitates multiple-item drag-and-drop
3213 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3215 wxArrayTreeItemIds selections
;
3216 size_t count
= GetSelections(selections
);
3222 DoSelectItem(item
, true, false);
3228 if ( (item
== m_current
) &&
3229 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3230 HasFlag(wxTR_EDIT_LABELS
) )
3232 if ( m_renameTimer
)
3234 if ( m_renameTimer
->IsRunning() )
3235 m_renameTimer
->Stop();
3239 m_renameTimer
= new wxTreeRenameTimer( this );
3242 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3245 m_lastOnSame
= false;
3248 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3250 if ( event
.LeftDown() )
3252 m_lastOnSame
= item
== m_current
;
3255 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3257 // only toggle the item for a single click, double click on
3258 // the button doesn't do anything (it toggles the item twice)
3259 if ( event
.LeftDown() )
3264 // don't select the item if the button was clicked
3269 // clear the previously selected items, if the
3270 // user clicked outside of the present selection.
3271 // otherwise, perform the deselection on mouse-up.
3272 // this allows multiple drag and drop to work.
3273 // but if Cmd is down, toggle selection of the clicked item
3274 if (!IsSelected(item
) || event
.CmdDown())
3276 // how should the selection work for this event?
3277 bool is_multiple
, extended_select
, unselect_others
;
3278 EventFlagsToSelType(GetWindowStyleFlag(),
3281 is_multiple
, extended_select
, unselect_others
);
3283 DoSelectItem(item
, unselect_others
, extended_select
);
3287 // For some reason, Windows isn't recognizing a left double-click,
3288 // so we need to simulate it here. Allow 200 milliseconds for now.
3289 if ( event
.LeftDClick() )
3291 // double clicking should not start editing the item label
3292 if ( m_renameTimer
)
3293 m_renameTimer
->Stop();
3295 m_lastOnSame
= false;
3297 // send activate event first
3298 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3299 nevent
.m_item
= item
;
3300 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3301 nevent
.SetEventObject( this );
3302 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3304 // if the user code didn't process the activate event,
3305 // handle it ourselves by toggling the item when it is
3307 if ( item
->HasPlus() )
3317 void wxGenericTreeCtrl::OnInternalIdle()
3319 wxWindow::OnInternalIdle();
3321 // Check if we need to select the root item
3322 // because nothing else has been selected.
3323 // Delaying it means that we can invoke event handlers
3324 // as required, when a first item is selected.
3325 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3328 SelectItem(m_select_me
);
3329 else if (GetRootItem().IsOk())
3330 SelectItem(GetRootItem());
3333 /* after all changes have been done to the tree control,
3334 * we actually redraw the tree when everything is over */
3336 if (!m_dirty
) return;
3337 if (m_freezeCount
) return;
3341 CalculatePositions();
3343 AdjustMyScrollbars();
3346 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3351 wxTreeItemAttr
*attr
= item
->GetAttributes();
3352 if ( attr
&& attr
->HasFont() )
3353 dc
.SetFont(attr
->GetFont());
3354 else if ( item
->IsBold() )
3355 dc
.SetFont(m_boldFont
);
3357 dc
.SetFont(m_normalFont
);
3359 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3362 // restore normal font
3363 dc
.SetFont( m_normalFont
);
3367 int image
= item
->GetCurrentImage();
3368 if ( image
!= NO_IMAGE
)
3370 if ( m_imageListNormal
)
3372 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3373 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3377 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3380 total_h
+= 2; // at least 2 pixels
3382 total_h
+= total_h
/10; // otherwise 10% extra spacing
3384 item
->SetHeight(total_h
);
3385 if (total_h
>m_lineHeight
)
3386 m_lineHeight
=total_h
;
3388 item
->SetWidth(image_w
+text_w
+2);
3391 // -----------------------------------------------------------------------------
3392 // for developper : y is now the top of the level
3393 // not the middle of it !
3394 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3396 int x
= level
*m_indent
;
3397 if (!HasFlag(wxTR_HIDE_ROOT
))
3401 else if (level
== 0)
3403 // a hidden root is not evaluated, but its
3404 // children are always calculated
3408 CalculateSize( item
, dc
);
3411 item
->SetX( x
+m_spacing
);
3413 y
+= GetLineHeight(item
);
3415 if ( !item
->IsExpanded() )
3417 // we don't need to calculate collapsed branches
3422 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3423 size_t n
, count
= children
.Count();
3425 for (n
= 0; n
< count
; ++n
)
3426 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3429 void wxGenericTreeCtrl::CalculatePositions()
3431 if ( !m_anchor
) return;
3433 wxClientDC
dc(this);
3436 dc
.SetFont( m_normalFont
);
3438 dc
.SetPen( m_dottedPen
);
3439 //if(GetImageList() == NULL)
3440 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3443 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3446 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3448 if (m_dirty
) return;
3449 if (m_freezeCount
) return;
3451 wxSize client
= GetClientSize();
3454 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3455 rect
.width
= client
.x
;
3456 rect
.height
= client
.y
;
3458 Refresh(true, &rect
);
3460 AdjustMyScrollbars();
3463 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3465 if (m_dirty
) return;
3466 if (m_freezeCount
) return;
3469 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3470 rect
.width
= GetClientSize().x
;
3471 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3473 Refresh(true, &rect
);
3476 void wxGenericTreeCtrl::RefreshSelected()
3478 if (m_freezeCount
) return;
3480 // TODO: this is awfully inefficient, we should keep the list of all
3481 // selected items internally, should be much faster
3483 RefreshSelectedUnder(m_anchor
);
3486 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3488 if (m_freezeCount
) return;
3490 if ( item
->IsSelected() )
3493 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3494 size_t count
= children
.GetCount();
3495 for ( size_t n
= 0; n
< count
; n
++ )
3497 RefreshSelectedUnder(children
[n
]);
3501 void wxGenericTreeCtrl::Freeze()
3506 void wxGenericTreeCtrl::Thaw()
3508 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3510 if ( !--m_freezeCount
)
3516 // ----------------------------------------------------------------------------
3517 // changing colours: we need to refresh the tree control
3518 // ----------------------------------------------------------------------------
3520 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3522 if ( !wxWindow::SetBackgroundColour(colour
) )
3525 if (m_freezeCount
) return true;
3532 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3534 if ( !wxWindow::SetForegroundColour(colour
) )
3537 if (m_freezeCount
) return true;
3544 // Process the tooltip event, to speed up event processing.
3545 // Doesn't actually get a tooltip.
3546 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3552 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3553 // be removed, as well as the #else case below.
3554 #define _USE_VISATTR 0
3557 #include "wx/listbox.h"
3563 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3565 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3569 // Use the same color scheme as wxListBox
3570 return wxListBox::GetClassDefaultAttributes(variant
);
3572 wxVisualAttributes attr
;
3573 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3574 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3575 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3580 #if WXWIN_COMPATIBILITY_2_4
3582 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3584 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3587 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3589 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3592 #endif // WXWIN_COMPATIBILITY_2_4
3594 #endif // wxUSE_TREECTRL