1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/treebase.h"
34 #include "wx/generic/treectlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
41 #include "wx/renderer.h"
44 #include "wx/mac/private.h"
47 // -----------------------------------------------------------------------------
49 // -----------------------------------------------------------------------------
51 class WXDLLEXPORT wxGenericTreeItem
;
53 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 static const int NO_IMAGE
= -1;
61 static const int PIXELS_PER_UNIT
= 10;
63 // -----------------------------------------------------------------------------
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
);
94 m_owner
->OnRenameCancelled(m_itemEdited
);
96 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
99 void OnChar( wxKeyEvent
&event
);
100 void OnKeyUp( wxKeyEvent
&event
);
101 void OnKillFocus( wxFocusEvent
&event
);
103 bool AcceptChanges();
107 wxGenericTreeCtrl
*m_owner
;
108 wxGenericTreeItem
*m_itemEdited
;
109 wxString m_startValue
;
112 DECLARE_EVENT_TABLE()
113 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
116 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
117 // for a sufficiently long time
118 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
121 // reset the current prefix after half a second of inactivity
122 enum { DELAY
= 500 };
124 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
126 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
129 wxGenericTreeCtrl
*m_owner
;
131 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
135 class WXDLLEXPORT wxGenericTreeItem
139 wxGenericTreeItem() { m_data
= NULL
; }
140 wxGenericTreeItem( wxGenericTreeItem
*parent
,
141 const wxString
& text
,
144 wxTreeItemData
*data
);
146 ~wxGenericTreeItem();
149 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
151 const wxString
& GetText() const { return m_text
; }
152 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
153 { return m_images
[which
]; }
154 wxTreeItemData
*GetData() const { return m_data
; }
156 // returns the current image for the item (depending on its
157 // selected/expanded/whatever state)
158 int GetCurrentImage() const;
160 void SetText( const wxString
&text
);
161 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
162 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
164 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
166 void SetBold(bool bold
) { m_isBold
= bold
; }
168 int GetX() const { return m_x
; }
169 int GetY() const { return m_y
; }
171 void SetX(int x
) { m_x
= x
; }
172 void SetY(int y
) { m_y
= y
; }
174 int GetHeight() const { return m_height
; }
175 int GetWidth() const { return m_width
; }
177 void SetHeight(int h
) { m_height
= h
; }
178 void SetWidth(int w
) { m_width
= w
; }
180 wxGenericTreeItem
*GetParent() const { return m_parent
; }
183 // deletes all children notifying the treectrl about it if !NULL
185 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
187 // get count of all children (and grand children if 'recursively')
188 size_t GetChildrenCount(bool recursively
= true) const;
190 void Insert(wxGenericTreeItem
*child
, size_t index
)
191 { m_children
.Insert(child
, index
); }
193 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
195 // return the item at given position (or NULL if no item), onButton is
196 // true if the point belongs to the item's button, otherwise it lies
197 // on the item's label
198 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
199 const wxGenericTreeCtrl
*,
203 void Expand() { m_isCollapsed
= false; }
204 void Collapse() { m_isCollapsed
= true; }
206 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
209 bool HasChildren() const { return !m_children
.IsEmpty(); }
210 bool IsSelected() const { return m_hasHilight
!= 0; }
211 bool IsExpanded() const { return !m_isCollapsed
; }
212 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
213 bool IsBold() const { return m_isBold
!= 0; }
216 // get them - may be NULL
217 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
218 // get them ensuring that the pointer is not NULL
219 wxTreeItemAttr
& Attr()
223 m_attr
= new wxTreeItemAttr
;
229 void SetAttributes(wxTreeItemAttr
*attr
)
231 if ( m_ownsAttr
) delete m_attr
;
235 // set them and delete when done
236 void AssignAttributes(wxTreeItemAttr
*attr
)
243 // since there can be very many of these, we save size by chosing
244 // the smallest representation for the elements and by ordering
245 // the members to avoid padding.
246 wxString m_text
; // label to be rendered for item
248 wxTreeItemData
*m_data
; // user-provided data
250 wxArrayGenericTreeItems m_children
; // list of children
251 wxGenericTreeItem
*m_parent
; // parent of this item
253 wxTreeItemAttr
*m_attr
; // attributes???
255 // tree ctrl images for the normal, selected, expanded and
256 // expanded+selected states
257 int m_images
[wxTreeItemIcon_Max
];
259 wxCoord m_x
; // (virtual) offset from top
260 wxCoord m_y
; // (virtual) offset from left
261 int m_width
; // width of this item
262 int m_height
; // height of this item
264 // use bitfields to save size
265 int m_isCollapsed
:1;
266 int m_hasHilight
:1; // same as focused
267 int m_hasPlus
:1; // used for item which doesn't have
268 // children but has a [+] button
269 int m_isBold
:1; // render the label in bold font
270 int m_ownsAttr
:1; // delete attribute when done
272 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
275 // =============================================================================
277 // =============================================================================
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 // translate the key or mouse event flags to the type of selection we're
285 static void EventFlagsToSelType(long style
,
289 bool &extended_select
,
290 bool &unselect_others
)
292 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
293 extended_select
= shiftDown
&& is_multiple
;
294 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
297 // check if the given item is under another one
298 static bool IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
302 if ( item
== parent
)
304 // item is a descendant of parent
308 item
= item
->GetParent();
314 // -----------------------------------------------------------------------------
315 // wxTreeRenameTimer (internal)
316 // -----------------------------------------------------------------------------
318 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
323 void wxTreeRenameTimer::Notify()
325 m_owner
->OnRenameTimer();
328 //-----------------------------------------------------------------------------
329 // wxTreeTextCtrl (internal)
330 //-----------------------------------------------------------------------------
332 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
333 EVT_CHAR (wxTreeTextCtrl::OnChar
)
334 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
335 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
338 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
339 wxGenericTreeItem
*item
)
340 : m_itemEdited(item
), m_startValue(item
->GetText())
345 int w
= m_itemEdited
->GetWidth(),
346 h
= m_itemEdited
->GetHeight();
349 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
354 int image
= item
->GetCurrentImage();
355 if ( image
!= NO_IMAGE
)
357 if ( m_owner
->m_imageListNormal
)
359 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
364 wxFAIL_MSG(_T("you must create an image list to use images!"));
368 // FIXME: what are all these hardcoded 4, 8 and 11s really?
372 wxSize bs
= DoGetBestSize() ;
373 // edit control height
376 int diff
= h
- ( bs
.y
- 8 ) ;
382 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
383 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
386 bool wxTreeTextCtrl::AcceptChanges()
388 const wxString value
= GetValue();
390 if ( value
== m_startValue
)
392 // nothing changed, always accept
396 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
398 // vetoed by the user
402 // accepted, do rename the item
403 m_owner
->SetItemText(m_itemEdited
, value
);
408 void wxTreeTextCtrl::Finish()
412 m_owner
->ResetTextControl();
414 wxPendingDelete
.Append(this);
418 m_owner
->SetFocusIgnoringChildren();
422 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
424 switch ( event
.m_keyCode
)
427 if ( AcceptChanges() )
429 // Close the text control, changes were accepted
432 // else do nothing, do not accept and do not close
444 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
448 // auto-grow the textctrl:
449 wxSize parentSize
= m_owner
->GetSize();
450 wxPoint myPos
= GetPosition();
451 wxSize mySize
= GetSize();
453 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
454 if (myPos
.x
+ sx
> parentSize
.x
)
455 sx
= parentSize
.x
- myPos
.x
;
458 SetSize(sx
, wxDefaultCoord
);
464 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
469 // We must finish regardless of success, otherwise we'll get
474 // We must let the native text control handle focus, too, otherwise
475 // it could have problems with the cursor (e.g., in wxGTK):
479 // -----------------------------------------------------------------------------
481 // -----------------------------------------------------------------------------
483 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
484 const wxString
& text
,
485 int image
, int selImage
,
486 wxTreeItemData
*data
)
489 m_images
[wxTreeItemIcon_Normal
] = image
;
490 m_images
[wxTreeItemIcon_Selected
] = selImage
;
491 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
492 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
497 m_isCollapsed
= true;
498 m_hasHilight
= false;
504 m_attr
= (wxTreeItemAttr
*)NULL
;
507 // We don't know the height here yet.
512 wxGenericTreeItem::~wxGenericTreeItem()
516 if (m_ownsAttr
) delete m_attr
;
518 wxASSERT_MSG( m_children
.IsEmpty(),
519 wxT("please call DeleteChildren() before deleting the item") );
522 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
524 size_t count
= m_children
.Count();
525 for ( size_t n
= 0; n
< count
; n
++ )
527 wxGenericTreeItem
*child
= m_children
[n
];
529 tree
->SendDeleteEvent(child
);
531 child
->DeleteChildren(tree
);
532 if (child
== tree
->m_select_me
)
533 tree
->m_select_me
= NULL
;
540 void wxGenericTreeItem::SetText( const wxString
&text
)
545 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
547 size_t count
= m_children
.Count();
551 size_t total
= count
;
552 for (size_t n
= 0; n
< count
; ++n
)
554 total
+= m_children
[n
]->GetChildrenCount();
560 void wxGenericTreeItem::GetSize( int &x
, int &y
,
561 const wxGenericTreeCtrl
*theButton
)
563 int bottomY
=m_y
+theButton
->GetLineHeight(this);
564 if ( y
< bottomY
) y
= bottomY
;
565 int width
= m_x
+ m_width
;
566 if ( x
< width
) x
= width
;
570 size_t count
= m_children
.Count();
571 for ( size_t n
= 0; n
< count
; ++n
)
573 m_children
[n
]->GetSize( x
, y
, theButton
);
578 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
579 const wxGenericTreeCtrl
*theCtrl
,
583 // for a hidden root node, don't evaluate it, but do evaluate children
584 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
587 int h
= theCtrl
->GetLineHeight(this);
588 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
590 int y_mid
= m_y
+ h
/2;
591 if (point
.y
< y_mid
)
592 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
594 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
596 int xCross
= m_x
- theCtrl
->GetSpacing();
598 // according to the drawing code the triangels are drawn
599 // at -4 , -4 from the position up to +10/+10 max
600 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
601 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
602 HasPlus() && theCtrl
->HasButtons() )
604 // 5 is the size of the plus sign
605 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
606 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
607 HasPlus() && theCtrl
->HasButtons() )
610 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
614 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
619 // assuming every image (normal and selected) has the same size!
620 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
621 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
624 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
625 flags
|= wxTREE_HITTEST_ONITEMICON
;
627 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
633 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
634 if (point
.x
> m_x
+m_width
)
635 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
640 // if children are expanded, fall through to evaluate them
641 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
645 size_t count
= m_children
.Count();
646 for ( size_t n
= 0; n
< count
; n
++ )
648 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
656 return (wxGenericTreeItem
*) NULL
;
659 int wxGenericTreeItem::GetCurrentImage() const
661 int image
= NO_IMAGE
;
666 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
669 if ( image
== NO_IMAGE
)
671 // we usually fall back to the normal item, but try just the
672 // expanded one (and not selected) first in this case
673 image
= GetImage(wxTreeItemIcon_Expanded
);
679 image
= GetImage(wxTreeItemIcon_Selected
);
682 // maybe it doesn't have the specific image we want,
683 // try the default one instead
684 if ( image
== NO_IMAGE
) image
= GetImage();
689 // -----------------------------------------------------------------------------
690 // wxGenericTreeCtrl implementation
691 // -----------------------------------------------------------------------------
693 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
695 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
696 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
697 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
698 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
699 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
700 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
701 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
704 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
706 * wxTreeCtrl has to be a real class or we have problems with
707 * the run-time information.
710 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
713 // -----------------------------------------------------------------------------
714 // construction/destruction
715 // -----------------------------------------------------------------------------
717 void wxGenericTreeCtrl::Init()
719 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
727 m_hilightBrush
= new wxBrush
729 wxSystemSettings::GetColour
731 wxSYS_COLOUR_HIGHLIGHT
736 m_hilightUnfocusedBrush
= new wxBrush
738 wxSystemSettings::GetColour
740 wxSYS_COLOUR_BTNSHADOW
745 m_imageListNormal
= m_imageListButtons
=
746 m_imageListState
= (wxImageList
*) NULL
;
747 m_ownsImageListNormal
= m_ownsImageListButtons
=
748 m_ownsImageListState
= false;
751 m_isDragging
= false;
752 m_dropTarget
= m_oldSelection
= NULL
;
756 m_renameTimer
= NULL
;
761 m_lastOnSame
= false;
763 #ifdef __WXMAC_CARBON__
764 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
766 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
768 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
769 m_normalFont
.GetFamily(),
770 m_normalFont
.GetStyle(),
772 m_normalFont
.GetUnderlined(),
773 m_normalFont
.GetFaceName(),
774 m_normalFont
.GetEncoding());
777 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
782 const wxValidator
& wxVALIDATOR_PARAM(validator
),
783 const wxString
& name
)
787 wxGetOsVersion( &major
, &minor
);
789 style
&= ~wxTR_LINES_AT_ROOT
;
790 style
|= wxTR_NO_LINES
;
792 style
|= wxTR_ROW_LINES
;
795 wxScrolledWindow::Create( parent
, id
, pos
, size
,
796 style
|wxHSCROLL
|wxVSCROLL
, name
);
798 // If the tree display has no buttons, but does have
799 // connecting lines, we can use a narrower layout.
800 // It may not be a good idea to force this...
801 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
808 SetValidator( validator
);
811 wxVisualAttributes attr
= GetDefaultAttributes();
812 SetOwnForegroundColour( attr
.colFg
);
813 SetOwnBackgroundColour( attr
.colBg
);
815 SetOwnFont(attr
.font
);
817 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
818 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
825 wxGenericTreeCtrl::~wxGenericTreeCtrl()
827 delete m_hilightBrush
;
828 delete m_hilightUnfocusedBrush
;
832 delete m_renameTimer
;
835 if (m_ownsImageListNormal
)
836 delete m_imageListNormal
;
837 if (m_ownsImageListState
)
838 delete m_imageListState
;
839 if (m_ownsImageListButtons
)
840 delete m_imageListButtons
;
843 // -----------------------------------------------------------------------------
845 // -----------------------------------------------------------------------------
847 size_t wxGenericTreeCtrl::GetCount() const
855 size_t count
= m_anchor
->GetChildrenCount();
856 if ( !HasFlag(wxTR_HIDE_ROOT
) )
858 // take the root itself into account
865 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
867 m_indent
= (unsigned short) indent
;
871 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
873 m_spacing
= (unsigned short) spacing
;
878 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
879 bool recursively
) const
881 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
883 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
886 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
888 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
890 // if we will hide the root, make sure children are visible
891 m_anchor
->SetHasPlus();
893 CalculatePositions();
896 // right now, just sets the styles. Eventually, we may
897 // want to update the inherited styles, but right now
898 // none of the parents has updatable styles
899 m_windowStyle
= styles
;
903 // -----------------------------------------------------------------------------
904 // functions to work with tree items
905 // -----------------------------------------------------------------------------
907 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
909 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
911 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
914 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
915 wxTreeItemIcon which
) const
917 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
919 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
922 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
924 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
926 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
929 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
931 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
933 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
934 return pItem
->Attr().GetTextColour();
937 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
939 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
941 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
942 return pItem
->Attr().GetBackgroundColour();
945 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
947 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
949 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
950 return pItem
->Attr().GetFont();
953 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
955 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
958 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
959 pItem
->SetText(text
);
960 CalculateSize(pItem
, dc
);
964 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
966 wxTreeItemIcon which
)
968 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
970 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
971 pItem
->SetImage(image
, which
);
974 CalculateSize(pItem
, dc
);
978 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
980 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
985 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
988 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
990 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
992 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
993 pItem
->SetHasPlus(has
);
997 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
999 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1001 // avoid redrawing the tree if no real change
1002 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1003 if ( pItem
->IsBold() != bold
)
1005 pItem
->SetBold(bold
);
1010 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1011 const wxColour
& col
)
1013 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1015 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1016 pItem
->Attr().SetTextColour(col
);
1020 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1021 const wxColour
& col
)
1023 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1025 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1026 pItem
->Attr().SetBackgroundColour(col
);
1030 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1032 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1034 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1035 pItem
->Attr().SetFont(font
);
1039 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1041 wxScrolledWindow::SetFont(font
);
1043 m_normalFont
= font
;
1044 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1045 m_normalFont
.GetFamily(),
1046 m_normalFont
.GetStyle(),
1048 m_normalFont
.GetUnderlined(),
1049 m_normalFont
.GetFaceName(),
1050 m_normalFont
.GetEncoding());
1056 // -----------------------------------------------------------------------------
1057 // item status inquiries
1058 // -----------------------------------------------------------------------------
1060 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1062 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1064 // An item is only visible if it's not a descendant of a collapsed item
1065 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1066 wxGenericTreeItem
* parent
= pItem
->GetParent();
1069 if (!parent
->IsExpanded())
1071 parent
= parent
->GetParent();
1075 GetViewStart(& startX
, & startY
);
1077 wxSize clientSize
= GetClientSize();
1080 if (!GetBoundingRect(item
, rect
))
1082 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1084 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1086 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1092 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1094 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1096 // consider that the item does have children if it has the "+" button: it
1097 // might not have them (if it had never been expanded yet) but then it
1098 // could have them as well and it's better to err on this side rather than
1099 // disabling some operations which are restricted to the items with
1100 // children for an item which does have them
1101 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1104 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1106 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1108 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1111 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1113 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1115 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1118 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1120 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1122 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1125 // -----------------------------------------------------------------------------
1127 // -----------------------------------------------------------------------------
1129 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1131 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1133 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1136 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1137 wxTreeItemIdValue
& cookie
) const
1139 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1142 return GetNextChild(item
, cookie
);
1145 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1146 wxTreeItemIdValue
& cookie
) const
1148 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1150 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1152 // it's ok to cast cookie to size_t, we never have indices big enough to
1153 // overflow "void *"
1154 size_t *pIndex
= (size_t *)&cookie
;
1155 if ( *pIndex
< children
.Count() )
1157 return children
.Item((*pIndex
)++);
1161 // there are no more of them
1162 return wxTreeItemId();
1166 #if WXWIN_COMPATIBILITY_2_4
1168 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1171 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1174 return GetNextChild(item
, cookie
);
1177 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1180 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1182 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1183 if ( (size_t)cookie
< children
.Count() )
1185 return children
.Item((size_t)cookie
++);
1189 // there are no more of them
1190 return wxTreeItemId();
1194 #endif // WXWIN_COMPATIBILITY_2_4
1196 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1198 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1200 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1201 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1204 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1206 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1208 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1209 wxGenericTreeItem
*parent
= i
->GetParent();
1210 if ( parent
== NULL
)
1212 // root item doesn't have any siblings
1213 return wxTreeItemId();
1216 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1217 int index
= siblings
.Index(i
);
1218 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1220 size_t n
= (size_t)(index
+ 1);
1221 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1224 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1226 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1228 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1229 wxGenericTreeItem
*parent
= i
->GetParent();
1230 if ( parent
== NULL
)
1232 // root item doesn't have any siblings
1233 return wxTreeItemId();
1236 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1237 int index
= siblings
.Index(i
);
1238 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1240 return index
== 0 ? wxTreeItemId()
1241 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1244 // Only for internal use right now, but should probably be public
1245 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1247 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1249 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1251 // First see if there are any children.
1252 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1253 if (children
.GetCount() > 0)
1255 return children
.Item(0);
1259 // Try a sibling of this or ancestor instead
1260 wxTreeItemId p
= item
;
1261 wxTreeItemId toFind
;
1264 toFind
= GetNextSibling(p
);
1265 p
= GetItemParent(p
);
1266 } while (p
.IsOk() && !toFind
.IsOk());
1271 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1273 wxTreeItemId id
= GetRootItem();
1282 } while (id
.IsOk());
1284 return wxTreeItemId();
1287 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1289 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1291 wxTreeItemId id
= item
;
1294 while (id
= GetNext(id
), id
.IsOk())
1300 return wxTreeItemId();
1303 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1305 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1307 wxFAIL_MSG(wxT("not implemented"));
1309 return wxTreeItemId();
1312 // called by wxTextTreeCtrl when it marks itself for deletion
1313 void wxGenericTreeCtrl::ResetTextControl()
1318 // find the first item starting with the given prefix after the given item
1319 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1320 const wxString
& prefixOrig
) const
1322 // match is case insensitive as this is more convenient to the user: having
1323 // to press Shift-letter to go to the item starting with a capital letter
1324 // would be too bothersome
1325 wxString prefix
= prefixOrig
.Lower();
1327 // determine the starting point: we shouldn't take the current item (this
1328 // allows to switch between two items starting with the same letter just by
1329 // pressing it) but we shouldn't jump to the next one if the user is
1330 // continuing to type as otherwise he might easily skip the item he wanted
1331 wxTreeItemId id
= idParent
;
1332 if ( prefix
.length() == 1 )
1337 // look for the item starting with the given prefix after it
1338 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1343 // if we haven't found anything...
1346 // ... wrap to the beginning
1348 if ( HasFlag(wxTR_HIDE_ROOT
) )
1350 // can't select virtual root
1354 // and try all the items (stop when we get to the one we started from)
1355 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1364 // -----------------------------------------------------------------------------
1366 // -----------------------------------------------------------------------------
1368 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1370 const wxString
& text
,
1371 int image
, int selImage
,
1372 wxTreeItemData
*data
)
1374 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1377 // should we give a warning here?
1378 return AddRoot(text
, image
, selImage
, data
);
1381 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1383 wxGenericTreeItem
*item
=
1384 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1388 data
->m_pItem
= item
;
1391 parent
->Insert( item
, previous
);
1396 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1397 int image
, int selImage
,
1398 wxTreeItemData
*data
)
1400 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1402 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1404 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1405 image
, selImage
, data
);
1408 data
->m_pItem
= m_anchor
;
1411 if (HasFlag(wxTR_HIDE_ROOT
))
1413 // if root is hidden, make sure we can navigate
1415 m_anchor
->SetHasPlus();
1417 CalculatePositions();
1420 if (!HasFlag(wxTR_MULTIPLE
))
1422 m_current
= m_key_current
= m_anchor
;
1423 m_current
->SetHilight( true );
1429 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1430 const wxString
& text
,
1431 int image
, int selImage
,
1432 wxTreeItemData
*data
)
1434 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1437 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1438 const wxTreeItemId
& idPrevious
,
1439 const wxString
& text
,
1440 int image
, int selImage
,
1441 wxTreeItemData
*data
)
1443 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1446 // should we give a warning here?
1447 return AddRoot(text
, image
, selImage
, data
);
1451 if (idPrevious
.IsOk())
1453 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1454 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1455 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1458 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1461 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1463 const wxString
& text
,
1464 int image
, int selImage
,
1465 wxTreeItemData
*data
)
1467 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1470 // should we give a warning here?
1471 return AddRoot(text
, image
, selImage
, data
);
1474 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1477 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1478 const wxString
& text
,
1479 int image
, int selImage
,
1480 wxTreeItemData
*data
)
1482 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1485 // should we give a warning here?
1486 return AddRoot(text
, image
, selImage
, data
);
1489 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1490 image
, selImage
, data
);
1493 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1495 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1496 event
.m_item
= item
;
1497 event
.SetEventObject( this );
1498 ProcessEvent( event
);
1501 // Don't leave edit or selection on a child which is about to disappear
1502 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1504 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1505 m_textCtrl
->StopEditing();
1507 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1508 m_key_current
= NULL
;
1510 if (IsDescendantOf(item
, m_select_me
)) {
1513 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1514 m_current
->SetHilight( false );
1520 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1522 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1524 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1525 ChildrenClosing(item
);
1526 item
->DeleteChildren(this);
1529 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1531 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1533 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1535 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1537 // can't delete the item being edited, cancel editing it first
1538 m_textCtrl
->StopEditing();
1541 wxGenericTreeItem
*parent
= item
->GetParent();
1543 // don't keep stale pointers around!
1544 if ( IsDescendantOf(item
, m_key_current
) )
1546 // Don't silently change the selection:
1547 // do it properly in idle time, so event
1548 // handlers get called.
1550 // m_key_current = parent;
1551 m_key_current
= NULL
;
1554 // m_select_me records whether we need to select
1555 // a different item, in idle time.
1556 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1558 m_select_me
= parent
;
1561 if ( IsDescendantOf(item
, m_current
) )
1563 // Don't silently change the selection:
1564 // do it properly in idle time, so event
1565 // handlers get called.
1567 // m_current = parent;
1569 m_select_me
= parent
;
1572 // remove the item from the tree
1575 parent
->GetChildren().Remove( item
); // remove by value
1577 else // deleting the root
1579 // nothing will be left in the tree
1583 // and delete all of its children and the item itself now
1584 item
->DeleteChildren(this);
1585 SendDeleteEvent(item
);
1587 if (item
== m_select_me
)
1593 void wxGenericTreeCtrl::DeleteAllItems()
1601 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1603 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1605 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1606 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1607 _T("can't expand hidden root") );
1609 if ( !item
->HasPlus() )
1612 if ( item
->IsExpanded() )
1615 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1616 event
.m_item
= item
;
1617 event
.SetEventObject( this );
1619 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1621 // cancelled by program
1626 CalculatePositions();
1628 RefreshSubtree(item
);
1630 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1631 ProcessEvent( event
);
1634 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1636 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1639 if ( !IsExpanded(item
) )
1643 wxTreeItemIdValue cookie
;
1644 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1645 while ( child
.IsOk() )
1649 child
= GetNextChild(item
, cookie
);
1653 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1655 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1656 _T("can't collapse hidden root") );
1658 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1660 if ( !item
->IsExpanded() )
1663 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1664 event
.m_item
= item
;
1665 event
.SetEventObject( this );
1666 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1668 // cancelled by program
1672 ChildrenClosing(item
);
1675 #if 0 // TODO why should items be collapsed recursively?
1676 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1677 size_t count
= children
.Count();
1678 for ( size_t n
= 0; n
< count
; n
++ )
1680 Collapse(children
[n
]);
1684 CalculatePositions();
1686 RefreshSubtree(item
);
1688 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1689 ProcessEvent( event
);
1692 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1695 DeleteChildren(item
);
1698 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1700 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1702 if (item
->IsExpanded())
1708 void wxGenericTreeCtrl::Unselect()
1712 m_current
->SetHilight( false );
1713 RefreshLine( m_current
);
1720 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1722 if (item
->IsSelected())
1724 item
->SetHilight(false);
1728 if (item
->HasChildren())
1730 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1731 size_t count
= children
.Count();
1732 for ( size_t n
= 0; n
< count
; ++n
)
1734 UnselectAllChildren(children
[n
]);
1739 void wxGenericTreeCtrl::UnselectAll()
1741 wxTreeItemId rootItem
= GetRootItem();
1743 // the tree might not have the root item at all
1746 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1750 // Recursive function !
1751 // To stop we must have crt_item<last_item
1753 // Tag all next children, when no more children,
1754 // Move to parent (not to tag)
1755 // Keep going... if we found last_item, we stop.
1756 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1758 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1760 if (parent
== NULL
) // This is root item
1761 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1763 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1764 int index
= children
.Index(crt_item
);
1765 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1767 size_t count
= children
.Count();
1768 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1770 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1773 return TagNextChildren(parent
, last_item
, select
);
1776 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1778 crt_item
->SetHilight(select
);
1779 RefreshLine(crt_item
);
1781 if (crt_item
==last_item
)
1784 if (crt_item
->HasChildren())
1786 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1787 size_t count
= children
.Count();
1788 for ( size_t n
= 0; n
< count
; ++n
)
1790 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1798 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1802 // item2 is not necessary after item1
1803 // choice first' and 'last' between item1 and item2
1804 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1805 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1807 bool select
= m_current
->IsSelected();
1809 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1812 TagNextChildren(first
,last
,select
);
1815 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1816 bool unselect_others
,
1817 bool extended_select
)
1819 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1823 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1824 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1826 //wxCHECK_RET( ( (!unselect_others) && is_single),
1827 // wxT("this is a single selection tree") );
1829 // to keep going anyhow !!!
1832 if (item
->IsSelected())
1833 return; // nothing to do
1834 unselect_others
= true;
1835 extended_select
= false;
1837 else if ( unselect_others
&& item
->IsSelected() )
1839 // selection change if there is more than one item currently selected
1840 wxArrayTreeItemIds selected_items
;
1841 if ( GetSelections(selected_items
) == 1 )
1845 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1846 event
.m_item
= item
;
1847 event
.m_itemOld
= m_current
;
1848 event
.SetEventObject( this );
1849 // TODO : Here we don't send any selection mode yet !
1851 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1854 wxTreeItemId parent
= GetItemParent( itemId
);
1855 while (parent
.IsOk())
1857 if (!IsExpanded(parent
))
1860 parent
= GetItemParent( parent
);
1863 EnsureVisible( itemId
);
1866 if (unselect_others
)
1868 if (is_single
) Unselect(); // to speed up thing
1873 if (extended_select
)
1877 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1880 // don't change the mark (m_current)
1881 SelectItemRange(m_current
, item
);
1885 bool select
= true; // the default
1887 // Check if we need to toggle hilight (ctrl mode)
1888 if (!unselect_others
)
1889 select
=!item
->IsSelected();
1891 m_current
= m_key_current
= item
;
1892 m_current
->SetHilight(select
);
1893 RefreshLine( m_current
);
1896 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1897 GetEventHandler()->ProcessEvent( event
);
1900 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1904 DoSelectItem(itemId
);
1908 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1909 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1911 item
->SetHilight(false);
1916 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1917 wxArrayTreeItemIds
&array
) const
1919 if ( item
->IsSelected() )
1920 array
.Add(wxTreeItemId(item
));
1922 if ( item
->HasChildren() )
1924 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1925 size_t count
= children
.GetCount();
1926 for ( size_t n
= 0; n
< count
; ++n
)
1927 FillArray(children
[n
], array
);
1931 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1934 wxTreeItemId idRoot
= GetRootItem();
1935 if ( idRoot
.IsOk() )
1937 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1939 //else: the tree is empty, so no selections
1941 return array
.Count();
1944 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1946 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1948 if (!item
.IsOk()) return;
1950 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1952 // first expand all parent branches
1953 wxGenericTreeItem
*parent
= gitem
->GetParent();
1955 if ( HasFlag(wxTR_HIDE_ROOT
) )
1957 while ( parent
&& parent
!= m_anchor
)
1960 parent
= parent
->GetParent();
1968 parent
= parent
->GetParent();
1972 //if (parent) CalculatePositions();
1977 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1979 if (!item
.IsOk()) return;
1981 // We have to call this here because the label in
1982 // question might just have been added and no screen
1983 // update taken place.
1985 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1990 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1992 // now scroll to the item
1993 int item_y
= gitem
->GetY();
1997 GetViewStart( &start_x
, &start_y
);
1998 start_y
*= PIXELS_PER_UNIT
;
2002 GetClientSize( &client_w
, &client_h
);
2004 if (item_y
< start_y
+3)
2009 m_anchor
->GetSize( x
, y
, this );
2010 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2011 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2012 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2013 // Item should appear at top
2014 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2016 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2021 m_anchor
->GetSize( x
, y
, this );
2022 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2023 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2024 item_y
+= PIXELS_PER_UNIT
+2;
2025 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2026 // Item should appear at bottom
2027 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
2031 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2032 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2034 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2035 wxGenericTreeItem
**item2
)
2037 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2039 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2042 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2043 const wxTreeItemId
& item2
)
2045 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2048 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2050 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2052 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2054 wxCHECK_RET( !s_treeBeingSorted
,
2055 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2057 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2058 if ( children
.Count() > 1 )
2062 s_treeBeingSorted
= this;
2063 children
.Sort(tree_ctrl_compare_func
);
2064 s_treeBeingSorted
= NULL
;
2066 //else: don't make the tree dirty as nothing changed
2069 wxImageList
*wxGenericTreeCtrl::GetImageList() const
2071 return m_imageListNormal
;
2074 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2076 return m_imageListButtons
;
2079 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2081 return m_imageListState
;
2084 void wxGenericTreeCtrl::CalculateLineHeight()
2086 wxClientDC
dc(this);
2087 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2089 if ( m_imageListNormal
)
2091 // Calculate a m_lineHeight value from the normal Image sizes.
2092 // May be toggle off. Then wxGenericTreeCtrl will spread when
2093 // necessary (which might look ugly).
2094 int n
= m_imageListNormal
->GetImageCount();
2095 for (int i
= 0; i
< n
; i
++)
2097 int width
= 0, height
= 0;
2098 m_imageListNormal
->GetSize(i
, width
, height
);
2099 if (height
> m_lineHeight
) m_lineHeight
= height
;
2103 if (m_imageListButtons
)
2105 // Calculate a m_lineHeight value from the Button image sizes.
2106 // May be toggle off. Then wxGenericTreeCtrl will spread when
2107 // necessary (which might look ugly).
2108 int n
= m_imageListButtons
->GetImageCount();
2109 for (int i
= 0; i
< n
; i
++)
2111 int width
= 0, height
= 0;
2112 m_imageListButtons
->GetSize(i
, width
, height
);
2113 if (height
> m_lineHeight
) m_lineHeight
= height
;
2117 if (m_lineHeight
< 30)
2118 m_lineHeight
+= 2; // at least 2 pixels
2120 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2123 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2125 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2126 m_imageListNormal
= imageList
;
2127 m_ownsImageListNormal
= false;
2129 // Don't do any drawing if we're setting the list to NULL,
2130 // since we may be in the process of deleting the tree control.
2132 CalculateLineHeight();
2135 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2137 if (m_ownsImageListState
) delete m_imageListState
;
2138 m_imageListState
= imageList
;
2139 m_ownsImageListState
= false;
2142 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2144 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2145 m_imageListButtons
= imageList
;
2146 m_ownsImageListButtons
= false;
2148 CalculateLineHeight();
2151 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2153 SetImageList(imageList
);
2154 m_ownsImageListNormal
= true;
2157 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2159 SetStateImageList(imageList
);
2160 m_ownsImageListState
= true;
2163 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2165 SetButtonsImageList(imageList
);
2166 m_ownsImageListButtons
= true;
2169 // -----------------------------------------------------------------------------
2171 // -----------------------------------------------------------------------------
2173 void wxGenericTreeCtrl::AdjustMyScrollbars()
2178 m_anchor
->GetSize( x
, y
, this );
2179 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2180 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2181 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2182 int y_pos
= GetScrollPos( wxVERTICAL
);
2183 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2187 SetScrollbars( 0, 0, 0, 0 );
2191 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2193 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2194 return item
->GetHeight();
2196 return m_lineHeight
;
2199 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2201 // TODO implement "state" icon on items
2203 wxTreeItemAttr
*attr
= item
->GetAttributes();
2204 if ( attr
&& attr
->HasFont() )
2205 dc
.SetFont(attr
->GetFont());
2206 else if (item
->IsBold())
2207 dc
.SetFont(m_boldFont
);
2209 long text_w
= 0, text_h
= 0;
2210 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2212 int image_h
= 0, image_w
= 0;
2213 int image
= item
->GetCurrentImage();
2214 if ( image
!= NO_IMAGE
)
2216 if ( m_imageListNormal
)
2218 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2227 int total_h
= GetLineHeight(item
);
2229 if ( item
->IsSelected() )
2231 // under mac selections are only a rectangle in case they don't have the focus
2235 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2236 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2240 dc
.SetBrush( *m_hilightBrush
) ;
2243 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2249 if ( attr
&& attr
->HasBackgroundColour() )
2250 colBg
= attr
->GetBackgroundColour();
2252 colBg
= m_backgroundColour
;
2253 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2256 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2258 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2262 DoGetPosition(&x
, &y
);
2264 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2268 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2270 // If it's selected, and there's an image, then we should
2271 // take care to leave the area under the image painted in the
2272 // background colour.
2273 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2274 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2278 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2279 item
->GetWidth()+2, total_h
-offset
);
2283 if ( image
!= NO_IMAGE
)
2285 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2286 m_imageListNormal
->Draw( image
, dc
,
2288 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2289 wxIMAGELIST_DRAW_TRANSPARENT
);
2290 dc
.DestroyClippingRegion();
2293 dc
.SetBackgroundMode(wxTRANSPARENT
);
2294 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2295 dc
.DrawText( item
->GetText(),
2296 (wxCoord
)(image_w
+ item
->GetX()),
2297 (wxCoord
)(item
->GetY() + extraH
));
2299 // restore normal font
2300 dc
.SetFont( m_normalFont
);
2303 // Now y stands for the top of the item, whereas it used to stand for middle !
2304 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2306 int x
= level
*m_indent
;
2307 if (!HasFlag(wxTR_HIDE_ROOT
))
2311 else if (level
== 0)
2313 // always expand hidden root
2315 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2316 int count
= children
.Count();
2322 PaintLevel(children
[n
], dc
, 1, y
);
2323 } while (++n
< count
);
2325 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2327 // draw line down to last child
2328 origY
+= GetLineHeight(children
[0])>>1;
2329 oldY
+= GetLineHeight(children
[n
-1])>>1;
2330 dc
.DrawLine(3, origY
, 3, oldY
);
2336 item
->SetX(x
+m_spacing
);
2339 int h
= GetLineHeight(item
);
2341 int y_mid
= y_top
+ (h
>>1);
2344 int exposed_x
= dc
.LogicalToDeviceX(0);
2345 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2347 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2351 // don't draw rect outline if we already have the
2352 // background color under Mac
2353 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2354 #endif // !__WXMAC__
2358 if ( item
->IsSelected() )
2360 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2364 wxTreeItemAttr
*attr
= item
->GetAttributes();
2365 if (attr
&& attr
->HasTextColour())
2366 colText
= attr
->GetTextColour();
2368 colText
= GetForegroundColour();
2372 dc
.SetTextForeground(colText
);
2376 PaintItem(item
, dc
);
2378 if (HasFlag(wxTR_ROW_LINES
))
2380 // if the background colour is white, choose a
2381 // contrasting color for the lines
2382 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2383 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2384 dc
.DrawLine(0, y_top
, 10000, y_top
);
2385 dc
.DrawLine(0, y
, 10000, y
);
2388 // restore DC objects
2389 dc
.SetBrush(*wxWHITE_BRUSH
);
2390 dc
.SetPen(m_dottedPen
);
2391 dc
.SetTextForeground(*wxBLACK
);
2393 if ( !HasFlag(wxTR_NO_LINES
) )
2395 // draw the horizontal line here
2397 if (x
> (signed)m_indent
)
2398 x_start
-= m_indent
;
2399 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2401 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2404 // should the item show a button?
2405 if ( item
->HasPlus() && HasButtons() )
2407 if ( m_imageListButtons
)
2409 // draw the image button here
2412 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2413 : wxTreeItemIcon_Normal
;
2414 if ( item
->IsSelected() )
2415 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2417 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2418 int xx
= x
- image_w
/2;
2419 int yy
= y_mid
- image_h
/2;
2421 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2422 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2423 wxIMAGELIST_DRAW_TRANSPARENT
);
2425 else // no custom buttons
2427 static const int wImage
= 9;
2428 static const int hImage
= 9;
2431 if (item
->IsExpanded())
2432 flag
|= wxCONTROL_EXPANDED
;
2433 if (item
== m_underMouse
)
2434 flag
|= wxCONTROL_CURRENT
;
2436 wxRendererNative::Get().DrawTreeItemButton
2440 wxRect(x
- wImage
/2,
2449 if (item
->IsExpanded())
2451 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2452 int count
= children
.Count();
2459 PaintLevel(children
[n
], dc
, level
, y
);
2460 } while (++n
< count
);
2462 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2464 // draw line down to last child
2465 oldY
+= GetLineHeight(children
[n
-1])>>1;
2466 if (HasButtons()) y_mid
+= 5;
2468 // Only draw the portion of the line that is visible, in case it is huge
2469 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2470 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2471 yOrigin
= abs(yOrigin
);
2472 GetClientSize(&width
, &height
);
2474 // Move end points to the begining/end of the view?
2475 if (y_mid
< yOrigin
)
2477 if (oldY
> yOrigin
+ height
)
2478 oldY
= yOrigin
+ height
;
2480 // after the adjustments if y_mid is larger than oldY then the line
2481 // isn't visible at all so don't draw anything
2483 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2489 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2493 if ( item
->HasPlus() )
2495 // it's a folder, indicate it by a border
2500 // draw a line under the drop target because the item will be
2502 DrawLine(item
, true /* below */);
2505 SetCursor(wxCURSOR_BULLSEYE
);
2510 SetCursor(wxCURSOR_NO_ENTRY
);
2514 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2516 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2518 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2520 wxClientDC
dc(this);
2522 dc
.SetLogicalFunction(wxINVERT
);
2523 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2525 int w
= i
->GetWidth() + 2;
2526 int h
= GetLineHeight(i
) + 2;
2528 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2531 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2533 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2535 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2537 wxClientDC
dc(this);
2539 dc
.SetLogicalFunction(wxINVERT
);
2545 y
+= GetLineHeight(i
) - 1;
2548 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2551 // -----------------------------------------------------------------------------
2552 // wxWidgets callbacks
2553 // -----------------------------------------------------------------------------
2555 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2563 dc
.SetFont( m_normalFont
);
2564 dc
.SetPen( m_dottedPen
);
2566 // this is now done dynamically
2567 //if(GetImageList() == NULL)
2568 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2571 PaintLevel( m_anchor
, dc
, 0, y
);
2574 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2583 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2592 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2594 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2595 te
.m_evtKey
= event
;
2596 te
.SetEventObject( this );
2597 if ( GetEventHandler()->ProcessEvent( te
) )
2599 // intercepted by the user code
2603 if ( (m_current
== 0) || (m_key_current
== 0) )
2609 // how should the selection work for this event?
2610 bool is_multiple
, extended_select
, unselect_others
;
2611 EventFlagsToSelType(GetWindowStyleFlag(),
2613 event
.ControlDown(),
2614 is_multiple
, extended_select
, unselect_others
);
2618 // * : Expand all/Collapse all
2619 // ' ' | return : activate
2620 // up : go up (not last children!)
2622 // left : go to parent
2623 // right : open if parent and go next
2624 // home : go to root
2625 // end : go to last item without opening parents
2626 // alnum : start or continue searching for the item with this prefix
2627 int keyCode
= event
.GetKeyCode();
2632 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2640 if ( !IsExpanded(m_current
) )
2643 ExpandAll(m_current
);
2646 //else: fall through to Collapse() it
2650 if (IsExpanded(m_current
))
2652 Collapse(m_current
);
2658 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2659 event
.m_item
= m_current
;
2660 event
.SetEventObject( this );
2661 GetEventHandler()->ProcessEvent( event
);
2666 if ( !event
.HasModifiers() )
2668 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2669 event
.m_item
= m_current
;
2670 event
.SetEventObject( this );
2671 GetEventHandler()->ProcessEvent( event
);
2674 // in any case, also generate the normal key event for this key,
2675 // even if we generated the ACTIVATED event above: this is what
2676 // wxMSW does and it makes sense because you might not want to
2677 // process ACTIVATED event at all and handle Space and Return
2678 // directly (and differently) which would be impossible otherwise
2682 // up goes to the previous sibling or to the last
2683 // of its children if it's expanded
2686 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2689 prev
= GetItemParent( m_key_current
);
2690 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2692 break; // don't go to root if it is hidden
2696 wxTreeItemIdValue cookie
;
2697 wxTreeItemId current
= m_key_current
;
2698 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2699 if (current
== GetFirstChild( prev
, cookie
))
2701 // otherwise we return to where we came from
2702 DoSelectItem( prev
, unselect_others
, extended_select
);
2703 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2710 while ( IsExpanded(prev
) && HasChildren(prev
) )
2712 wxTreeItemId child
= GetLastChild(prev
);
2719 DoSelectItem( prev
, unselect_others
, extended_select
);
2720 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2725 // left arrow goes to the parent
2728 wxTreeItemId prev
= GetItemParent( m_current
);
2729 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2731 // don't go to root if it is hidden
2732 prev
= GetPrevSibling( m_current
);
2736 DoSelectItem( prev
, unselect_others
, extended_select
);
2742 // this works the same as the down arrow except that we
2743 // also expand the item if it wasn't expanded yet
2749 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2751 wxTreeItemIdValue cookie
;
2752 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2753 DoSelectItem( child
, unselect_others
, extended_select
);
2754 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2758 wxTreeItemId next
= GetNextSibling( m_key_current
);
2761 wxTreeItemId current
= m_key_current
;
2762 while (current
.IsOk() && !next
)
2764 current
= GetItemParent( current
);
2765 if (current
) next
= GetNextSibling( current
);
2770 DoSelectItem( next
, unselect_others
, extended_select
);
2771 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2777 // <End> selects the last visible tree item
2780 wxTreeItemId last
= GetRootItem();
2782 while ( last
.IsOk() && IsExpanded(last
) )
2784 wxTreeItemId lastChild
= GetLastChild(last
);
2786 // it may happen if the item was expanded but then all of
2787 // its children have been deleted - so IsExpanded() returned
2788 // true, but GetLastChild() returned invalid item
2797 DoSelectItem( last
, unselect_others
, extended_select
);
2802 // <Home> selects the root item
2805 wxTreeItemId prev
= GetRootItem();
2809 if ( HasFlag(wxTR_HIDE_ROOT
) )
2811 wxTreeItemIdValue cookie
;
2812 prev
= GetFirstChild(prev
, cookie
);
2817 DoSelectItem( prev
, unselect_others
, extended_select
);
2822 // do not use wxIsalnum() here
2823 if ( !event
.HasModifiers() &&
2824 ((keyCode
>= '0' && keyCode
<= '9') ||
2825 (keyCode
>= 'a' && keyCode
<= 'z') ||
2826 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2828 // find the next item starting with the given prefix
2829 wxChar ch
= (wxChar
)keyCode
;
2831 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2842 // also start the timer to reset the current prefix if the user
2843 // doesn't press any more alnum keys soon -- we wouldn't want
2844 // to use this prefix for a new item search
2847 m_findTimer
= new wxTreeFindTimer(this);
2850 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2859 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2861 // JACS: removed wxYieldIfNeeded() because it can cause the window
2862 // to be deleted from under us if a close window event is pending
2867 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2868 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2869 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2870 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2871 if (flags
) return wxTreeItemId();
2873 if (m_anchor
== NULL
)
2875 flags
= wxTREE_HITTEST_NOWHERE
;
2876 return wxTreeItemId();
2879 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2883 flags
= wxTREE_HITTEST_NOWHERE
;
2884 return wxTreeItemId();
2889 // get the bounding rectangle of the item (or of its label only)
2890 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2892 bool WXUNUSED(textOnly
)) const
2894 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2896 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2899 GetViewStart(& startX
, & startY
);
2901 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2902 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2903 rect
.width
= i
->GetWidth();
2904 //rect.height = i->GetHeight();
2905 rect
.height
= GetLineHeight(i
);
2910 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2912 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2914 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2916 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2917 te
.m_item
= itemEdit
;
2918 te
.SetEventObject( this );
2919 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2925 // We have to call this here because the label in
2926 // question might just have been added and no screen
2927 // update taken place.
2929 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2935 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2937 m_textCtrl
->SetFocus();
2940 // returns a pointer to the text edit control if the item is being
2941 // edited, NULL otherwise (it's assumed that no more than one item may
2942 // be edited simultaneously)
2943 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2948 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2949 const wxString
& value
)
2951 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2953 le
.SetEventObject( this );
2955 le
.m_editCancelled
= false;
2957 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2960 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2962 // let owner know that the edit was cancelled
2963 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2965 le
.SetEventObject( this );
2966 le
.m_label
= wxEmptyString
;
2967 le
.m_editCancelled
= true;
2969 GetEventHandler()->ProcessEvent( le
);
2972 void wxGenericTreeCtrl::OnRenameTimer()
2977 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2979 if ( !m_anchor
) return;
2981 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2983 // Is the mouse over a tree item button?
2985 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
2986 wxGenericTreeItem
*underMouse
= thisItem
;
2988 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
2989 #endif // wxUSE_TOOLTIPS
2992 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
2993 (!event
.LeftIsDown()) &&
2995 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3003 if (underMouse
!= m_underMouse
)
3007 // unhighlight old item
3008 wxGenericTreeItem
*tmp
= m_underMouse
;
3009 m_underMouse
= NULL
;
3013 m_underMouse
= underMouse
;
3015 RefreshLine( m_underMouse
);
3019 // Determines what item we are hovering over and need a tooltip for
3020 wxTreeItemId hoverItem
= thisItem
;
3022 // We do not want a tooltip if we are dragging, or if the rename timer is running
3023 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3025 // Ask the tree control what tooltip (if any) should be shown
3026 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
3027 hevent
.m_item
= hoverItem
;
3028 hevent
.SetEventObject(this);
3030 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3032 SetToolTip(hevent
.m_label
);
3037 // we process left mouse up event (enables in-place edit), right down
3038 // (pass to the user code), left dbl click (activate item) and
3039 // dragging/moving events for items drag-and-drop
3040 if ( !(event
.LeftDown() ||
3042 event
.RightDown() ||
3043 event
.LeftDClick() ||
3045 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3054 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3056 if ( event
.Dragging() && !m_isDragging
)
3058 if (m_dragCount
== 0)
3063 if (m_dragCount
!= 3)
3065 // wait until user drags a bit further...
3069 wxEventType command
= event
.RightIsDown()
3070 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3071 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3073 wxTreeEvent
nevent( command
, GetId() );
3074 nevent
.m_item
= m_current
;
3075 nevent
.SetEventObject(this);
3077 // by default the dragging is not supported, the user code must
3078 // explicitly allow the event for it to take place
3081 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3083 // we're going to drag this item
3084 m_isDragging
= true;
3086 // remember the old cursor because we will change it while
3088 m_oldCursor
= m_cursor
;
3090 // in a single selection control, hide the selection temporarily
3091 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3093 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3095 if ( m_oldSelection
)
3097 m_oldSelection
->SetHilight(false);
3098 RefreshLine(m_oldSelection
);
3105 else if ( event
.Moving() )
3107 if ( item
!= m_dropTarget
)
3109 // unhighlight the previous drop target
3110 DrawDropEffect(m_dropTarget
);
3112 m_dropTarget
= item
;
3114 // highlight the current drop target if any
3115 DrawDropEffect(m_dropTarget
);
3117 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3124 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3126 // erase the highlighting
3127 DrawDropEffect(m_dropTarget
);
3129 if ( m_oldSelection
)
3131 m_oldSelection
->SetHilight(true);
3132 RefreshLine(m_oldSelection
);
3133 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3136 // generate the drag end event
3137 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3139 event
.m_item
= item
;
3140 event
.m_pointDrag
= pt
;
3141 event
.SetEventObject(this);
3143 (void)GetEventHandler()->ProcessEvent(event
);
3145 m_isDragging
= false;
3146 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3150 SetCursor(m_oldCursor
);
3152 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3160 // If we got to this point, we are not dragging or moving the mouse.
3161 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3162 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3163 // We skip even if we didn't hit an item because we still should
3164 // restore focus to the tree control even if we didn't exactly hit an item.
3165 if ( event
.LeftDown() )
3170 // here we process only the messages which happen on tree items
3174 if (item
== NULL
) return; /* we hit the blank area */
3176 if ( event
.RightDown() )
3178 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3179 nevent
.m_item
= item
;
3180 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3181 nevent
.SetEventObject(this);
3182 GetEventHandler()->ProcessEvent(nevent
);
3184 else if ( event
.LeftUp() )
3186 // this facilitates multiple-item drag-and-drop
3188 if (item
&& HasFlag(wxTR_MULTIPLE
))
3190 wxArrayTreeItemIds selections
;
3191 size_t count
= GetSelections(selections
);
3194 !event
.ControlDown() &&
3197 DoSelectItem(item
, true, false);
3203 if ( (item
== m_current
) &&
3204 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3205 HasFlag(wxTR_EDIT_LABELS
) )
3207 if ( m_renameTimer
)
3209 if ( m_renameTimer
->IsRunning() )
3210 m_renameTimer
->Stop();
3214 m_renameTimer
= new wxTreeRenameTimer( this );
3217 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3220 m_lastOnSame
= false;
3223 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3225 if ( event
.LeftDown() )
3227 m_lastOnSame
= item
== m_current
;
3230 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3232 // only toggle the item for a single click, double click on
3233 // the button doesn't do anything (it toggles the item twice)
3234 if ( event
.LeftDown() )
3239 // don't select the item if the button was clicked
3244 // clear the previously selected items, if the
3245 // user clicked outside of the present selection.
3246 // otherwise, perform the deselection on mouse-up.
3247 // this allows multiple drag and drop to work.
3249 if (!IsSelected(item
))
3251 // how should the selection work for this event?
3252 bool is_multiple
, extended_select
, unselect_others
;
3253 EventFlagsToSelType(GetWindowStyleFlag(),
3255 event
.ControlDown(),
3256 is_multiple
, extended_select
, unselect_others
);
3258 DoSelectItem(item
, unselect_others
, extended_select
);
3262 // For some reason, Windows isn't recognizing a left double-click,
3263 // so we need to simulate it here. Allow 200 milliseconds for now.
3264 if ( event
.LeftDClick() )
3266 // double clicking should not start editing the item label
3267 if ( m_renameTimer
)
3268 m_renameTimer
->Stop();
3270 m_lastOnSame
= false;
3272 // send activate event first
3273 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3274 nevent
.m_item
= item
;
3275 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3276 nevent
.SetEventObject( this );
3277 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3279 // if the user code didn't process the activate event,
3280 // handle it ourselves by toggling the item when it is
3282 if ( item
->HasPlus() )
3292 void wxGenericTreeCtrl::OnInternalIdle()
3294 wxWindow::OnInternalIdle();
3296 // Check if we need to select the root item
3297 // because nothing else has been selected.
3298 // Delaying it means that we can invoke event handlers
3299 // as required, when a first item is selected.
3300 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3303 SelectItem(m_select_me
);
3304 else if (GetRootItem().IsOk())
3305 SelectItem(GetRootItem());
3308 /* after all changes have been done to the tree control,
3309 * we actually redraw the tree when everything is over */
3311 if (!m_dirty
) return;
3312 if (m_freezeCount
) return;
3316 CalculatePositions();
3318 AdjustMyScrollbars();
3321 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3326 wxTreeItemAttr
*attr
= item
->GetAttributes();
3327 if ( attr
&& attr
->HasFont() )
3328 dc
.SetFont(attr
->GetFont());
3329 else if ( item
->IsBold() )
3330 dc
.SetFont(m_boldFont
);
3332 dc
.SetFont(m_normalFont
);
3334 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3337 // restore normal font
3338 dc
.SetFont( m_normalFont
);
3342 int image
= item
->GetCurrentImage();
3343 if ( image
!= NO_IMAGE
)
3345 if ( m_imageListNormal
)
3347 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3352 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3355 total_h
+= 2; // at least 2 pixels
3357 total_h
+= total_h
/10; // otherwise 10% extra spacing
3359 item
->SetHeight(total_h
);
3360 if (total_h
>m_lineHeight
)
3361 m_lineHeight
=total_h
;
3363 item
->SetWidth(image_w
+text_w
+2);
3366 // -----------------------------------------------------------------------------
3367 // for developper : y is now the top of the level
3368 // not the middle of it !
3369 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3371 int x
= level
*m_indent
;
3372 if (!HasFlag(wxTR_HIDE_ROOT
))
3376 else if (level
== 0)
3378 // a hidden root is not evaluated, but its
3379 // children are always calculated
3383 CalculateSize( item
, dc
);
3386 item
->SetX( x
+m_spacing
);
3388 y
+= GetLineHeight(item
);
3390 if ( !item
->IsExpanded() )
3392 // we don't need to calculate collapsed branches
3397 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3398 size_t n
, count
= children
.Count();
3400 for (n
= 0; n
< count
; ++n
)
3401 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3404 void wxGenericTreeCtrl::CalculatePositions()
3406 if ( !m_anchor
) return;
3408 wxClientDC
dc(this);
3411 dc
.SetFont( m_normalFont
);
3413 dc
.SetPen( m_dottedPen
);
3414 //if(GetImageList() == NULL)
3415 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3418 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3421 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3423 if (m_dirty
) return;
3424 if (m_freezeCount
) return;
3426 wxSize client
= GetClientSize();
3429 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3430 rect
.width
= client
.x
;
3431 rect
.height
= client
.y
;
3433 Refresh(true, &rect
);
3435 AdjustMyScrollbars();
3438 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3440 if (m_dirty
) return;
3441 if (m_freezeCount
) return;
3444 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3445 rect
.width
= GetClientSize().x
;
3446 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3448 Refresh(true, &rect
);
3451 void wxGenericTreeCtrl::RefreshSelected()
3453 if (m_freezeCount
) return;
3455 // TODO: this is awfully inefficient, we should keep the list of all
3456 // selected items internally, should be much faster
3458 RefreshSelectedUnder(m_anchor
);
3461 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3463 if (m_freezeCount
) return;
3465 if ( item
->IsSelected() )
3468 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3469 size_t count
= children
.GetCount();
3470 for ( size_t n
= 0; n
< count
; n
++ )
3472 RefreshSelectedUnder(children
[n
]);
3476 void wxGenericTreeCtrl::Freeze()
3481 void wxGenericTreeCtrl::Thaw()
3483 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3485 if ( !--m_freezeCount
)
3491 // ----------------------------------------------------------------------------
3492 // changing colours: we need to refresh the tree control
3493 // ----------------------------------------------------------------------------
3495 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3497 if ( !wxWindow::SetBackgroundColour(colour
) )
3500 if (m_freezeCount
) return true;
3507 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3509 if ( !wxWindow::SetForegroundColour(colour
) )
3512 if (m_freezeCount
) return true;
3519 // Process the tooltip event, to speed up event processing.
3520 // Doesn't actually get a tooltip.
3521 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3527 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3529 // something is better than nothing...
3530 // 100x80 is what the MSW version will get from the default
3531 // wxControl::DoGetBestSize
3532 return wxSize(100,80);
3536 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3537 // be removed, as well as the #else case below.
3538 #define _USE_VISATTR 0
3541 #include "wx/listbox.h"
3547 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3549 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3553 // Use the same color scheme as wxListBox
3554 return wxListBox::GetClassDefaultAttributes(variant
);
3556 wxVisualAttributes attr
;
3557 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3558 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3559 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3564 #if WXWIN_COMPATIBILITY_2_2
3566 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
3568 return GetItemParent( item
);
3571 #endif // WXWIN_COMPATIBILITY_2_2
3573 #endif // wxUSE_TREECTRL