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_dropEffectAboveItem
= false;
763 m_lastOnSame
= false;
765 #ifdef __WXMAC_CARBON__
766 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
768 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
770 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
771 m_normalFont
.GetFamily(),
772 m_normalFont
.GetStyle(),
774 m_normalFont
.GetUnderlined(),
775 m_normalFont
.GetFaceName(),
776 m_normalFont
.GetEncoding());
779 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
784 const wxValidator
& wxVALIDATOR_PARAM(validator
),
785 const wxString
& name
)
789 wxGetOsVersion( &major
, &minor
);
791 style
&= ~wxTR_LINES_AT_ROOT
;
792 style
|= wxTR_NO_LINES
;
794 style
|= wxTR_ROW_LINES
;
797 wxScrolledWindow::Create( parent
, id
, pos
, size
,
798 style
|wxHSCROLL
|wxVSCROLL
, name
);
800 // If the tree display has no buttons, but does have
801 // connecting lines, we can use a narrower layout.
802 // It may not be a good idea to force this...
803 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
810 SetValidator( validator
);
813 wxVisualAttributes attr
= GetDefaultAttributes();
814 SetOwnForegroundColour( attr
.colFg
);
815 SetOwnBackgroundColour( attr
.colBg
);
817 SetOwnFont(attr
.font
);
819 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
820 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
827 wxGenericTreeCtrl::~wxGenericTreeCtrl()
829 delete m_hilightBrush
;
830 delete m_hilightUnfocusedBrush
;
834 delete m_renameTimer
;
837 if (m_ownsImageListNormal
)
838 delete m_imageListNormal
;
839 if (m_ownsImageListState
)
840 delete m_imageListState
;
841 if (m_ownsImageListButtons
)
842 delete m_imageListButtons
;
845 // -----------------------------------------------------------------------------
847 // -----------------------------------------------------------------------------
849 size_t wxGenericTreeCtrl::GetCount() const
857 size_t count
= m_anchor
->GetChildrenCount();
858 if ( !HasFlag(wxTR_HIDE_ROOT
) )
860 // take the root itself into account
867 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
869 m_indent
= (unsigned short) indent
;
873 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
875 m_spacing
= (unsigned short) spacing
;
880 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
881 bool recursively
) const
883 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
885 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
888 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
890 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
892 // if we will hide the root, make sure children are visible
893 m_anchor
->SetHasPlus();
895 CalculatePositions();
898 // right now, just sets the styles. Eventually, we may
899 // want to update the inherited styles, but right now
900 // none of the parents has updatable styles
901 m_windowStyle
= styles
;
905 // -----------------------------------------------------------------------------
906 // functions to work with tree items
907 // -----------------------------------------------------------------------------
909 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
911 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
913 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
916 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
917 wxTreeItemIcon which
) const
919 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
921 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
924 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
926 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
928 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
931 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
933 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
935 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
936 return pItem
->Attr().GetTextColour();
939 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
941 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
943 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
944 return pItem
->Attr().GetBackgroundColour();
947 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
949 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
951 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
952 return pItem
->Attr().GetFont();
955 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
957 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
960 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
961 pItem
->SetText(text
);
962 CalculateSize(pItem
, dc
);
966 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
968 wxTreeItemIcon which
)
970 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
972 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
973 pItem
->SetImage(image
, which
);
976 CalculateSize(pItem
, dc
);
980 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
982 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
987 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
990 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
992 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
994 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
995 pItem
->SetHasPlus(has
);
999 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1001 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1003 // avoid redrawing the tree if no real change
1004 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1005 if ( pItem
->IsBold() != bold
)
1007 pItem
->SetBold(bold
);
1012 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1013 const wxColour
& col
)
1015 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1017 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1018 pItem
->Attr().SetTextColour(col
);
1022 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1023 const wxColour
& col
)
1025 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1027 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1028 pItem
->Attr().SetBackgroundColour(col
);
1032 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1034 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1036 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1037 pItem
->Attr().SetFont(font
);
1041 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1043 wxScrolledWindow::SetFont(font
);
1045 m_normalFont
= font
;
1046 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1047 m_normalFont
.GetFamily(),
1048 m_normalFont
.GetStyle(),
1050 m_normalFont
.GetUnderlined(),
1051 m_normalFont
.GetFaceName(),
1052 m_normalFont
.GetEncoding());
1058 // -----------------------------------------------------------------------------
1059 // item status inquiries
1060 // -----------------------------------------------------------------------------
1062 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1064 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1066 // An item is only visible if it's not a descendant of a collapsed item
1067 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1068 wxGenericTreeItem
* parent
= pItem
->GetParent();
1071 if (!parent
->IsExpanded())
1073 parent
= parent
->GetParent();
1077 GetViewStart(& startX
, & startY
);
1079 wxSize clientSize
= GetClientSize();
1082 if (!GetBoundingRect(item
, rect
))
1084 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1086 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1088 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1094 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1096 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1098 // consider that the item does have children if it has the "+" button: it
1099 // might not have them (if it had never been expanded yet) but then it
1100 // could have them as well and it's better to err on this side rather than
1101 // disabling some operations which are restricted to the items with
1102 // children for an item which does have them
1103 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1106 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1108 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1110 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1113 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1115 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1117 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1120 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1122 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1124 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1127 // -----------------------------------------------------------------------------
1129 // -----------------------------------------------------------------------------
1131 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1133 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1135 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1138 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1139 wxTreeItemIdValue
& cookie
) const
1141 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1144 return GetNextChild(item
, cookie
);
1147 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1148 wxTreeItemIdValue
& cookie
) const
1150 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1152 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1154 // it's ok to cast cookie to size_t, we never have indices big enough to
1155 // overflow "void *"
1156 size_t *pIndex
= (size_t *)&cookie
;
1157 if ( *pIndex
< children
.Count() )
1159 return children
.Item((*pIndex
)++);
1163 // there are no more of them
1164 return wxTreeItemId();
1168 #if WXWIN_COMPATIBILITY_2_4
1170 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1173 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1176 return GetNextChild(item
, cookie
);
1179 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1182 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1184 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1185 if ( (size_t)cookie
< children
.Count() )
1187 return children
.Item((size_t)cookie
++);
1191 // there are no more of them
1192 return wxTreeItemId();
1196 #endif // WXWIN_COMPATIBILITY_2_4
1198 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1200 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1202 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1203 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1206 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1208 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1210 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1211 wxGenericTreeItem
*parent
= i
->GetParent();
1212 if ( parent
== NULL
)
1214 // root item doesn't have any siblings
1215 return wxTreeItemId();
1218 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1219 int index
= siblings
.Index(i
);
1220 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1222 size_t n
= (size_t)(index
+ 1);
1223 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1226 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1228 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1230 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1231 wxGenericTreeItem
*parent
= i
->GetParent();
1232 if ( parent
== NULL
)
1234 // root item doesn't have any siblings
1235 return wxTreeItemId();
1238 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1239 int index
= siblings
.Index(i
);
1240 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1242 return index
== 0 ? wxTreeItemId()
1243 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1246 // Only for internal use right now, but should probably be public
1247 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1249 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1251 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1253 // First see if there are any children.
1254 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1255 if (children
.GetCount() > 0)
1257 return children
.Item(0);
1261 // Try a sibling of this or ancestor instead
1262 wxTreeItemId p
= item
;
1263 wxTreeItemId toFind
;
1266 toFind
= GetNextSibling(p
);
1267 p
= GetItemParent(p
);
1268 } while (p
.IsOk() && !toFind
.IsOk());
1273 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1275 wxTreeItemId id
= GetRootItem();
1284 } while (id
.IsOk());
1286 return wxTreeItemId();
1289 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1291 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1293 wxTreeItemId id
= item
;
1296 while (id
= GetNext(id
), id
.IsOk())
1302 return wxTreeItemId();
1305 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1307 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1309 wxFAIL_MSG(wxT("not implemented"));
1311 return wxTreeItemId();
1314 // called by wxTextTreeCtrl when it marks itself for deletion
1315 void wxGenericTreeCtrl::ResetTextControl()
1320 // find the first item starting with the given prefix after the given item
1321 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1322 const wxString
& prefixOrig
) const
1324 // match is case insensitive as this is more convenient to the user: having
1325 // to press Shift-letter to go to the item starting with a capital letter
1326 // would be too bothersome
1327 wxString prefix
= prefixOrig
.Lower();
1329 // determine the starting point: we shouldn't take the current item (this
1330 // allows to switch between two items starting with the same letter just by
1331 // pressing it) but we shouldn't jump to the next one if the user is
1332 // continuing to type as otherwise he might easily skip the item he wanted
1333 wxTreeItemId id
= idParent
;
1334 if ( prefix
.length() == 1 )
1339 // look for the item starting with the given prefix after it
1340 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1345 // if we haven't found anything...
1348 // ... wrap to the beginning
1350 if ( HasFlag(wxTR_HIDE_ROOT
) )
1352 // can't select virtual root
1356 // and try all the items (stop when we get to the one we started from)
1357 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1366 // -----------------------------------------------------------------------------
1368 // -----------------------------------------------------------------------------
1370 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1372 const wxString
& text
,
1373 int image
, int selImage
,
1374 wxTreeItemData
*data
)
1376 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1379 // should we give a warning here?
1380 return AddRoot(text
, image
, selImage
, data
);
1383 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1385 wxGenericTreeItem
*item
=
1386 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1390 data
->m_pItem
= item
;
1393 parent
->Insert( item
, previous
);
1398 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1399 int image
, int selImage
,
1400 wxTreeItemData
*data
)
1402 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1404 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1406 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1407 image
, selImage
, data
);
1410 data
->m_pItem
= m_anchor
;
1413 if (HasFlag(wxTR_HIDE_ROOT
))
1415 // if root is hidden, make sure we can navigate
1417 m_anchor
->SetHasPlus();
1419 CalculatePositions();
1422 if (!HasFlag(wxTR_MULTIPLE
))
1424 m_current
= m_key_current
= m_anchor
;
1425 m_current
->SetHilight( true );
1431 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1432 const wxString
& text
,
1433 int image
, int selImage
,
1434 wxTreeItemData
*data
)
1436 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1439 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1440 const wxTreeItemId
& idPrevious
,
1441 const wxString
& text
,
1442 int image
, int selImage
,
1443 wxTreeItemData
*data
)
1445 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1448 // should we give a warning here?
1449 return AddRoot(text
, image
, selImage
, data
);
1453 if (idPrevious
.IsOk())
1455 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1456 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1457 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1460 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1463 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1465 const wxString
& text
,
1466 int image
, int selImage
,
1467 wxTreeItemData
*data
)
1469 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1472 // should we give a warning here?
1473 return AddRoot(text
, image
, selImage
, data
);
1476 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1479 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1480 const wxString
& text
,
1481 int image
, int selImage
,
1482 wxTreeItemData
*data
)
1484 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1487 // should we give a warning here?
1488 return AddRoot(text
, image
, selImage
, data
);
1491 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1492 image
, selImage
, data
);
1495 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1497 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1498 event
.m_item
= item
;
1499 event
.SetEventObject( this );
1500 ProcessEvent( event
);
1503 // Don't leave edit or selection on a child which is about to disappear
1504 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1506 if (m_textCtrl
!= NULL
&& item
!= m_textCtrl
->item() && IsDescendantOf(item
, m_textCtrl
->item())) {
1507 m_textCtrl
->StopEditing();
1509 if (item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
)) {
1510 m_key_current
= NULL
;
1512 if (IsDescendantOf(item
, m_select_me
)) {
1515 if (item
!= m_current
&& IsDescendantOf(item
, m_current
)) {
1516 m_current
->SetHilight( false );
1522 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1524 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1526 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1527 ChildrenClosing(item
);
1528 item
->DeleteChildren(this);
1531 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1533 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1535 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1537 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1539 // can't delete the item being edited, cancel editing it first
1540 m_textCtrl
->StopEditing();
1543 wxGenericTreeItem
*parent
= item
->GetParent();
1545 // don't keep stale pointers around!
1546 if ( IsDescendantOf(item
, m_key_current
) )
1548 // Don't silently change the selection:
1549 // do it properly in idle time, so event
1550 // handlers get called.
1552 // m_key_current = parent;
1553 m_key_current
= NULL
;
1556 // m_select_me records whether we need to select
1557 // a different item, in idle time.
1558 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1560 m_select_me
= parent
;
1563 if ( IsDescendantOf(item
, m_current
) )
1565 // Don't silently change the selection:
1566 // do it properly in idle time, so event
1567 // handlers get called.
1569 // m_current = parent;
1571 m_select_me
= parent
;
1574 // remove the item from the tree
1577 parent
->GetChildren().Remove( item
); // remove by value
1579 else // deleting the root
1581 // nothing will be left in the tree
1585 // and delete all of its children and the item itself now
1586 item
->DeleteChildren(this);
1587 SendDeleteEvent(item
);
1589 if (item
== m_select_me
)
1595 void wxGenericTreeCtrl::DeleteAllItems()
1603 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1605 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1607 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1608 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1609 _T("can't expand hidden root") );
1611 if ( !item
->HasPlus() )
1614 if ( item
->IsExpanded() )
1617 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1618 event
.m_item
= item
;
1619 event
.SetEventObject( this );
1621 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1623 // cancelled by program
1628 CalculatePositions();
1630 RefreshSubtree(item
);
1632 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1633 ProcessEvent( event
);
1636 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1638 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1641 if ( !IsExpanded(item
) )
1645 wxTreeItemIdValue cookie
;
1646 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1647 while ( child
.IsOk() )
1651 child
= GetNextChild(item
, cookie
);
1655 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1657 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1658 _T("can't collapse hidden root") );
1660 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1662 if ( !item
->IsExpanded() )
1665 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1666 event
.m_item
= item
;
1667 event
.SetEventObject( this );
1668 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1670 // cancelled by program
1674 ChildrenClosing(item
);
1677 #if 0 // TODO why should items be collapsed recursively?
1678 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1679 size_t count
= children
.Count();
1680 for ( size_t n
= 0; n
< count
; n
++ )
1682 Collapse(children
[n
]);
1686 CalculatePositions();
1688 RefreshSubtree(item
);
1690 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1691 ProcessEvent( event
);
1694 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1697 DeleteChildren(item
);
1700 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1702 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1704 if (item
->IsExpanded())
1710 void wxGenericTreeCtrl::Unselect()
1714 m_current
->SetHilight( false );
1715 RefreshLine( m_current
);
1722 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1724 if (item
->IsSelected())
1726 item
->SetHilight(false);
1730 if (item
->HasChildren())
1732 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1733 size_t count
= children
.Count();
1734 for ( size_t n
= 0; n
< count
; ++n
)
1736 UnselectAllChildren(children
[n
]);
1741 void wxGenericTreeCtrl::UnselectAll()
1743 wxTreeItemId rootItem
= GetRootItem();
1745 // the tree might not have the root item at all
1748 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1752 // Recursive function !
1753 // To stop we must have crt_item<last_item
1755 // Tag all next children, when no more children,
1756 // Move to parent (not to tag)
1757 // Keep going... if we found last_item, we stop.
1758 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1760 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1762 if (parent
== NULL
) // This is root item
1763 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1765 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1766 int index
= children
.Index(crt_item
);
1767 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1769 size_t count
= children
.Count();
1770 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1772 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return true;
1775 return TagNextChildren(parent
, last_item
, select
);
1778 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1780 crt_item
->SetHilight(select
);
1781 RefreshLine(crt_item
);
1783 if (crt_item
==last_item
)
1786 if (crt_item
->HasChildren())
1788 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1789 size_t count
= children
.Count();
1790 for ( size_t n
= 0; n
< count
; ++n
)
1792 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1800 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1804 // item2 is not necessary after item1
1805 // choice first' and 'last' between item1 and item2
1806 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1807 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1809 bool select
= m_current
->IsSelected();
1811 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1814 TagNextChildren(first
,last
,select
);
1817 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1818 bool unselect_others
,
1819 bool extended_select
)
1821 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1825 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1826 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1828 //wxCHECK_RET( ( (!unselect_others) && is_single),
1829 // wxT("this is a single selection tree") );
1831 // to keep going anyhow !!!
1834 if (item
->IsSelected())
1835 return; // nothing to do
1836 unselect_others
= true;
1837 extended_select
= false;
1839 else if ( unselect_others
&& item
->IsSelected() )
1841 // selection change if there is more than one item currently selected
1842 wxArrayTreeItemIds selected_items
;
1843 if ( GetSelections(selected_items
) == 1 )
1847 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1848 event
.m_item
= item
;
1849 event
.m_itemOld
= m_current
;
1850 event
.SetEventObject( this );
1851 // TODO : Here we don't send any selection mode yet !
1853 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1856 wxTreeItemId parent
= GetItemParent( itemId
);
1857 while (parent
.IsOk())
1859 if (!IsExpanded(parent
))
1862 parent
= GetItemParent( parent
);
1865 EnsureVisible( itemId
);
1868 if (unselect_others
)
1870 if (is_single
) Unselect(); // to speed up thing
1875 if (extended_select
)
1879 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1882 // don't change the mark (m_current)
1883 SelectItemRange(m_current
, item
);
1887 bool select
= true; // the default
1889 // Check if we need to toggle hilight (ctrl mode)
1890 if (!unselect_others
)
1891 select
=!item
->IsSelected();
1893 m_current
= m_key_current
= item
;
1894 m_current
->SetHilight(select
);
1895 RefreshLine( m_current
);
1898 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1899 GetEventHandler()->ProcessEvent( event
);
1902 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1906 DoSelectItem(itemId
);
1910 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1911 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1913 item
->SetHilight(false);
1918 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1919 wxArrayTreeItemIds
&array
) const
1921 if ( item
->IsSelected() )
1922 array
.Add(wxTreeItemId(item
));
1924 if ( item
->HasChildren() )
1926 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1927 size_t count
= children
.GetCount();
1928 for ( size_t n
= 0; n
< count
; ++n
)
1929 FillArray(children
[n
], array
);
1933 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1936 wxTreeItemId idRoot
= GetRootItem();
1937 if ( idRoot
.IsOk() )
1939 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1941 //else: the tree is empty, so no selections
1943 return array
.Count();
1946 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1948 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1950 if (!item
.IsOk()) return;
1952 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1954 // first expand all parent branches
1955 wxGenericTreeItem
*parent
= gitem
->GetParent();
1957 if ( HasFlag(wxTR_HIDE_ROOT
) )
1959 while ( parent
&& parent
!= m_anchor
)
1962 parent
= parent
->GetParent();
1970 parent
= parent
->GetParent();
1974 //if (parent) CalculatePositions();
1979 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1981 if (!item
.IsOk()) return;
1983 // We have to call this here because the label in
1984 // question might just have been added and no screen
1985 // update taken place.
1987 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1992 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1994 // now scroll to the item
1995 int item_y
= gitem
->GetY();
1999 GetViewStart( &start_x
, &start_y
);
2000 start_y
*= PIXELS_PER_UNIT
;
2004 GetClientSize( &client_w
, &client_h
);
2006 if (item_y
< start_y
+3)
2011 m_anchor
->GetSize( x
, y
, this );
2012 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2013 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2014 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2015 // Item should appear at top
2016 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
2018 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2023 m_anchor
->GetSize( x
, y
, this );
2024 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2025 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2026 item_y
+= PIXELS_PER_UNIT
+2;
2027 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2028 // Item should appear at bottom
2029 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
);
2033 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2034 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2036 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2037 wxGenericTreeItem
**item2
)
2039 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2041 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2044 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2045 const wxTreeItemId
& item2
)
2047 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2050 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2052 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2054 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2056 wxCHECK_RET( !s_treeBeingSorted
,
2057 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2059 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2060 if ( children
.Count() > 1 )
2064 s_treeBeingSorted
= this;
2065 children
.Sort(tree_ctrl_compare_func
);
2066 s_treeBeingSorted
= NULL
;
2068 //else: don't make the tree dirty as nothing changed
2071 wxImageList
*wxGenericTreeCtrl::GetImageList() const
2073 return m_imageListNormal
;
2076 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2078 return m_imageListButtons
;
2081 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2083 return m_imageListState
;
2086 void wxGenericTreeCtrl::CalculateLineHeight()
2088 wxClientDC
dc(this);
2089 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2091 if ( m_imageListNormal
)
2093 // Calculate a m_lineHeight value from the normal Image sizes.
2094 // May be toggle off. Then wxGenericTreeCtrl will spread when
2095 // necessary (which might look ugly).
2096 int n
= m_imageListNormal
->GetImageCount();
2097 for (int i
= 0; i
< n
; i
++)
2099 int width
= 0, height
= 0;
2100 m_imageListNormal
->GetSize(i
, width
, height
);
2101 if (height
> m_lineHeight
) m_lineHeight
= height
;
2105 if (m_imageListButtons
)
2107 // Calculate a m_lineHeight value from the Button image sizes.
2108 // May be toggle off. Then wxGenericTreeCtrl will spread when
2109 // necessary (which might look ugly).
2110 int n
= m_imageListButtons
->GetImageCount();
2111 for (int i
= 0; i
< n
; i
++)
2113 int width
= 0, height
= 0;
2114 m_imageListButtons
->GetSize(i
, width
, height
);
2115 if (height
> m_lineHeight
) m_lineHeight
= height
;
2119 if (m_lineHeight
< 30)
2120 m_lineHeight
+= 2; // at least 2 pixels
2122 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2125 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2127 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2128 m_imageListNormal
= imageList
;
2129 m_ownsImageListNormal
= false;
2131 // Don't do any drawing if we're setting the list to NULL,
2132 // since we may be in the process of deleting the tree control.
2134 CalculateLineHeight();
2137 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2139 if (m_ownsImageListState
) delete m_imageListState
;
2140 m_imageListState
= imageList
;
2141 m_ownsImageListState
= false;
2144 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2146 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2147 m_imageListButtons
= imageList
;
2148 m_ownsImageListButtons
= false;
2150 CalculateLineHeight();
2153 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2155 SetImageList(imageList
);
2156 m_ownsImageListNormal
= true;
2159 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2161 SetStateImageList(imageList
);
2162 m_ownsImageListState
= true;
2165 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2167 SetButtonsImageList(imageList
);
2168 m_ownsImageListButtons
= true;
2171 // -----------------------------------------------------------------------------
2173 // -----------------------------------------------------------------------------
2175 void wxGenericTreeCtrl::AdjustMyScrollbars()
2180 m_anchor
->GetSize( x
, y
, this );
2181 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2182 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2183 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2184 int y_pos
= GetScrollPos( wxVERTICAL
);
2185 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2189 SetScrollbars( 0, 0, 0, 0 );
2193 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2195 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2196 return item
->GetHeight();
2198 return m_lineHeight
;
2201 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2203 // TODO implement "state" icon on items
2205 wxTreeItemAttr
*attr
= item
->GetAttributes();
2206 if ( attr
&& attr
->HasFont() )
2207 dc
.SetFont(attr
->GetFont());
2208 else if (item
->IsBold())
2209 dc
.SetFont(m_boldFont
);
2211 long text_w
= 0, text_h
= 0;
2212 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2214 int image_h
= 0, image_w
= 0;
2215 int image
= item
->GetCurrentImage();
2216 if ( image
!= NO_IMAGE
)
2218 if ( m_imageListNormal
)
2220 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2229 int total_h
= GetLineHeight(item
);
2231 if ( item
->IsSelected() )
2233 // under mac selections are only a rectangle in case they don't have the focus
2237 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2238 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2242 dc
.SetBrush( *m_hilightBrush
) ;
2245 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2251 if ( attr
&& attr
->HasBackgroundColour() )
2252 colBg
= attr
->GetBackgroundColour();
2254 colBg
= m_backgroundColour
;
2255 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2258 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2260 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2264 DoGetPosition(&x
, &y
);
2266 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2270 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2272 // If it's selected, and there's an image, then we should
2273 // take care to leave the area under the image painted in the
2274 // background colour.
2275 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2276 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2280 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2281 item
->GetWidth()+2, total_h
-offset
);
2285 if ( image
!= NO_IMAGE
)
2287 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2288 m_imageListNormal
->Draw( image
, dc
,
2290 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2291 wxIMAGELIST_DRAW_TRANSPARENT
);
2292 dc
.DestroyClippingRegion();
2295 dc
.SetBackgroundMode(wxTRANSPARENT
);
2296 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2297 dc
.DrawText( item
->GetText(),
2298 (wxCoord
)(image_w
+ item
->GetX()),
2299 (wxCoord
)(item
->GetY() + extraH
));
2301 // restore normal font
2302 dc
.SetFont( m_normalFont
);
2305 // Now y stands for the top of the item, whereas it used to stand for middle !
2306 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2308 int x
= level
*m_indent
;
2309 if (!HasFlag(wxTR_HIDE_ROOT
))
2313 else if (level
== 0)
2315 // always expand hidden root
2317 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2318 int count
= children
.Count();
2324 PaintLevel(children
[n
], dc
, 1, y
);
2325 } while (++n
< count
);
2327 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2329 // draw line down to last child
2330 origY
+= GetLineHeight(children
[0])>>1;
2331 oldY
+= GetLineHeight(children
[n
-1])>>1;
2332 dc
.DrawLine(3, origY
, 3, oldY
);
2338 item
->SetX(x
+m_spacing
);
2341 int h
= GetLineHeight(item
);
2343 int y_mid
= y_top
+ (h
>>1);
2346 int exposed_x
= dc
.LogicalToDeviceX(0);
2347 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2349 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2353 // don't draw rect outline if we already have the
2354 // background color under Mac
2355 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2356 #endif // !__WXMAC__
2360 if ( item
->IsSelected() )
2362 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2366 wxTreeItemAttr
*attr
= item
->GetAttributes();
2367 if (attr
&& attr
->HasTextColour())
2368 colText
= attr
->GetTextColour();
2370 colText
= GetForegroundColour();
2374 dc
.SetTextForeground(colText
);
2378 PaintItem(item
, dc
);
2380 if (HasFlag(wxTR_ROW_LINES
))
2382 // if the background colour is white, choose a
2383 // contrasting color for the lines
2384 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2385 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2386 dc
.DrawLine(0, y_top
, 10000, y_top
);
2387 dc
.DrawLine(0, y
, 10000, y
);
2390 // restore DC objects
2391 dc
.SetBrush(*wxWHITE_BRUSH
);
2392 dc
.SetPen(m_dottedPen
);
2393 dc
.SetTextForeground(*wxBLACK
);
2395 if ( !HasFlag(wxTR_NO_LINES
) )
2397 // draw the horizontal line here
2399 if (x
> (signed)m_indent
)
2400 x_start
-= m_indent
;
2401 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2403 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2406 // should the item show a button?
2407 if ( item
->HasPlus() && HasButtons() )
2409 if ( m_imageListButtons
)
2411 // draw the image button here
2414 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2415 : wxTreeItemIcon_Normal
;
2416 if ( item
->IsSelected() )
2417 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2419 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2420 int xx
= x
- image_w
/2;
2421 int yy
= y_mid
- image_h
/2;
2423 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2424 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2425 wxIMAGELIST_DRAW_TRANSPARENT
);
2427 else // no custom buttons
2429 static const int wImage
= 9;
2430 static const int hImage
= 9;
2433 if (item
->IsExpanded())
2434 flag
|= wxCONTROL_EXPANDED
;
2435 if (item
== m_underMouse
)
2436 flag
|= wxCONTROL_CURRENT
;
2438 wxRendererNative::Get().DrawTreeItemButton
2442 wxRect(x
- wImage
/2,
2451 if (item
->IsExpanded())
2453 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2454 int count
= children
.Count();
2461 PaintLevel(children
[n
], dc
, level
, y
);
2462 } while (++n
< count
);
2464 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2466 // draw line down to last child
2467 oldY
+= GetLineHeight(children
[n
-1])>>1;
2468 if (HasButtons()) y_mid
+= 5;
2470 // Only draw the portion of the line that is visible, in case it is huge
2471 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2472 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2473 yOrigin
= abs(yOrigin
);
2474 GetClientSize(&width
, &height
);
2476 // Move end points to the begining/end of the view?
2477 if (y_mid
< yOrigin
)
2479 if (oldY
> yOrigin
+ height
)
2480 oldY
= yOrigin
+ height
;
2482 // after the adjustments if y_mid is larger than oldY then the line
2483 // isn't visible at all so don't draw anything
2485 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2491 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2495 if ( item
->HasPlus() )
2497 // it's a folder, indicate it by a border
2502 // draw a line under the drop target because the item will be
2504 DrawLine(item
, !m_dropEffectAboveItem
);
2507 SetCursor(wxCURSOR_BULLSEYE
);
2512 SetCursor(wxCURSOR_NO_ENTRY
);
2516 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2518 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2520 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2522 wxClientDC
dc(this);
2524 dc
.SetLogicalFunction(wxINVERT
);
2525 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2527 int w
= i
->GetWidth() + 2;
2528 int h
= GetLineHeight(i
) + 2;
2530 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2533 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2535 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2537 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2539 wxClientDC
dc(this);
2541 dc
.SetLogicalFunction(wxINVERT
);
2547 y
+= GetLineHeight(i
) - 1;
2550 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2553 // -----------------------------------------------------------------------------
2554 // wxWidgets callbacks
2555 // -----------------------------------------------------------------------------
2557 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2565 dc
.SetFont( m_normalFont
);
2566 dc
.SetPen( m_dottedPen
);
2568 // this is now done dynamically
2569 //if(GetImageList() == NULL)
2570 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2573 PaintLevel( m_anchor
, dc
, 0, y
);
2576 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2585 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2594 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2596 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2597 te
.m_evtKey
= event
;
2598 te
.SetEventObject( this );
2599 if ( GetEventHandler()->ProcessEvent( te
) )
2601 // intercepted by the user code
2605 if ( (m_current
== 0) || (m_key_current
== 0) )
2611 // how should the selection work for this event?
2612 bool is_multiple
, extended_select
, unselect_others
;
2613 EventFlagsToSelType(GetWindowStyleFlag(),
2615 event
.ControlDown(),
2616 is_multiple
, extended_select
, unselect_others
);
2620 // * : Expand all/Collapse all
2621 // ' ' | return : activate
2622 // up : go up (not last children!)
2624 // left : go to parent
2625 // right : open if parent and go next
2626 // home : go to root
2627 // end : go to last item without opening parents
2628 // alnum : start or continue searching for the item with this prefix
2629 int keyCode
= event
.GetKeyCode();
2634 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2642 if ( !IsExpanded(m_current
) )
2645 ExpandAll(m_current
);
2648 //else: fall through to Collapse() it
2652 if (IsExpanded(m_current
))
2654 Collapse(m_current
);
2660 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2661 event
.m_item
= m_current
;
2662 event
.SetEventObject( this );
2663 GetEventHandler()->ProcessEvent( event
);
2668 if ( !event
.HasModifiers() )
2670 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2671 event
.m_item
= m_current
;
2672 event
.SetEventObject( this );
2673 GetEventHandler()->ProcessEvent( event
);
2676 // in any case, also generate the normal key event for this key,
2677 // even if we generated the ACTIVATED event above: this is what
2678 // wxMSW does and it makes sense because you might not want to
2679 // process ACTIVATED event at all and handle Space and Return
2680 // directly (and differently) which would be impossible otherwise
2684 // up goes to the previous sibling or to the last
2685 // of its children if it's expanded
2688 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2691 prev
= GetItemParent( m_key_current
);
2692 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2694 break; // don't go to root if it is hidden
2698 wxTreeItemIdValue cookie
;
2699 wxTreeItemId current
= m_key_current
;
2700 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2701 if (current
== GetFirstChild( prev
, cookie
))
2703 // otherwise we return to where we came from
2704 DoSelectItem( prev
, unselect_others
, extended_select
);
2705 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2712 while ( IsExpanded(prev
) && HasChildren(prev
) )
2714 wxTreeItemId child
= GetLastChild(prev
);
2721 DoSelectItem( prev
, unselect_others
, extended_select
);
2722 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2727 // left arrow goes to the parent
2730 wxTreeItemId prev
= GetItemParent( m_current
);
2731 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2733 // don't go to root if it is hidden
2734 prev
= GetPrevSibling( m_current
);
2738 DoSelectItem( prev
, unselect_others
, extended_select
);
2744 // this works the same as the down arrow except that we
2745 // also expand the item if it wasn't expanded yet
2751 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2753 wxTreeItemIdValue cookie
;
2754 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2755 DoSelectItem( child
, unselect_others
, extended_select
);
2756 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2760 wxTreeItemId next
= GetNextSibling( m_key_current
);
2763 wxTreeItemId current
= m_key_current
;
2764 while (current
.IsOk() && !next
)
2766 current
= GetItemParent( current
);
2767 if (current
) next
= GetNextSibling( current
);
2772 DoSelectItem( next
, unselect_others
, extended_select
);
2773 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2779 // <End> selects the last visible tree item
2782 wxTreeItemId last
= GetRootItem();
2784 while ( last
.IsOk() && IsExpanded(last
) )
2786 wxTreeItemId lastChild
= GetLastChild(last
);
2788 // it may happen if the item was expanded but then all of
2789 // its children have been deleted - so IsExpanded() returned
2790 // true, but GetLastChild() returned invalid item
2799 DoSelectItem( last
, unselect_others
, extended_select
);
2804 // <Home> selects the root item
2807 wxTreeItemId prev
= GetRootItem();
2811 if ( HasFlag(wxTR_HIDE_ROOT
) )
2813 wxTreeItemIdValue cookie
;
2814 prev
= GetFirstChild(prev
, cookie
);
2819 DoSelectItem( prev
, unselect_others
, extended_select
);
2824 // do not use wxIsalnum() here
2825 if ( !event
.HasModifiers() &&
2826 ((keyCode
>= '0' && keyCode
<= '9') ||
2827 (keyCode
>= 'a' && keyCode
<= 'z') ||
2828 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2830 // find the next item starting with the given prefix
2831 wxChar ch
= (wxChar
)keyCode
;
2833 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
2844 // also start the timer to reset the current prefix if the user
2845 // doesn't press any more alnum keys soon -- we wouldn't want
2846 // to use this prefix for a new item search
2849 m_findTimer
= new wxTreeFindTimer(this);
2852 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2861 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2863 // JACS: removed wxYieldIfNeeded() because it can cause the window
2864 // to be deleted from under us if a close window event is pending
2869 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2870 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2871 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2872 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2873 if (flags
) return wxTreeItemId();
2875 if (m_anchor
== NULL
)
2877 flags
= wxTREE_HITTEST_NOWHERE
;
2878 return wxTreeItemId();
2881 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2885 flags
= wxTREE_HITTEST_NOWHERE
;
2886 return wxTreeItemId();
2891 // get the bounding rectangle of the item (or of its label only)
2892 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2894 bool WXUNUSED(textOnly
)) const
2896 wxCHECK_MSG( item
.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2898 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2901 GetViewStart(& startX
, & startY
);
2903 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2904 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2905 rect
.width
= i
->GetWidth();
2906 //rect.height = i->GetHeight();
2907 rect
.height
= GetLineHeight(i
);
2912 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2914 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2916 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2918 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2919 te
.m_item
= itemEdit
;
2920 te
.SetEventObject( this );
2921 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2927 // We have to call this here because the label in
2928 // question might just have been added and no screen
2929 // update taken place.
2931 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2937 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2939 m_textCtrl
->SetFocus();
2942 // returns a pointer to the text edit control if the item is being
2943 // edited, NULL otherwise (it's assumed that no more than one item may
2944 // be edited simultaneously)
2945 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2950 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2951 const wxString
& value
)
2953 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2955 le
.SetEventObject( this );
2957 le
.m_editCancelled
= false;
2959 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2962 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2964 // let owner know that the edit was cancelled
2965 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2967 le
.SetEventObject( this );
2968 le
.m_label
= wxEmptyString
;
2969 le
.m_editCancelled
= true;
2971 GetEventHandler()->ProcessEvent( le
);
2974 void wxGenericTreeCtrl::OnRenameTimer()
2979 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2981 if ( !m_anchor
) return;
2983 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2985 // Is the mouse over a tree item button?
2987 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
2988 wxGenericTreeItem
*underMouse
= thisItem
;
2990 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
2991 #endif // wxUSE_TOOLTIPS
2994 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
2995 (!event
.LeftIsDown()) &&
2997 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3005 if (underMouse
!= m_underMouse
)
3009 // unhighlight old item
3010 wxGenericTreeItem
*tmp
= m_underMouse
;
3011 m_underMouse
= NULL
;
3015 m_underMouse
= underMouse
;
3017 RefreshLine( m_underMouse
);
3021 // Determines what item we are hovering over and need a tooltip for
3022 wxTreeItemId hoverItem
= thisItem
;
3024 // We do not want a tooltip if we are dragging, or if the rename timer is running
3025 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3027 // Ask the tree control what tooltip (if any) should be shown
3028 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
3029 hevent
.m_item
= hoverItem
;
3030 hevent
.SetEventObject(this);
3032 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3034 SetToolTip(hevent
.m_label
);
3039 // we process left mouse up event (enables in-place edit), right down
3040 // (pass to the user code), left dbl click (activate item) and
3041 // dragging/moving events for items drag-and-drop
3042 if ( !(event
.LeftDown() ||
3044 event
.RightDown() ||
3045 event
.LeftDClick() ||
3047 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3056 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3058 if ( event
.Dragging() && !m_isDragging
)
3060 if (m_dragCount
== 0)
3065 if (m_dragCount
!= 3)
3067 // wait until user drags a bit further...
3071 wxEventType command
= event
.RightIsDown()
3072 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3073 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3075 wxTreeEvent
nevent( command
, GetId() );
3076 nevent
.m_item
= m_current
;
3077 nevent
.SetEventObject(this);
3079 // by default the dragging is not supported, the user code must
3080 // explicitly allow the event for it to take place
3083 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3085 // we're going to drag this item
3086 m_isDragging
= true;
3088 // remember the old cursor because we will change it while
3090 m_oldCursor
= m_cursor
;
3092 // in a single selection control, hide the selection temporarily
3093 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3095 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3097 if ( m_oldSelection
)
3099 m_oldSelection
->SetHilight(false);
3100 RefreshLine(m_oldSelection
);
3107 else if ( event
.Dragging() )
3109 if ( item
!= m_dropTarget
)
3111 // unhighlight the previous drop target
3112 DrawDropEffect(m_dropTarget
);
3114 m_dropTarget
= item
;
3116 // highlight the current drop target if any
3117 DrawDropEffect(m_dropTarget
);
3119 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3126 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3128 // erase the highlighting
3129 DrawDropEffect(m_dropTarget
);
3131 if ( m_oldSelection
)
3133 m_oldSelection
->SetHilight(true);
3134 RefreshLine(m_oldSelection
);
3135 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3138 // generate the drag end event
3139 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3141 event
.m_item
= item
;
3142 event
.m_pointDrag
= pt
;
3143 event
.SetEventObject(this);
3145 (void)GetEventHandler()->ProcessEvent(event
);
3147 m_isDragging
= false;
3148 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3152 SetCursor(m_oldCursor
);
3154 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3162 // If we got to this point, we are not dragging or moving the mouse.
3163 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3164 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3165 // We skip even if we didn't hit an item because we still should
3166 // restore focus to the tree control even if we didn't exactly hit an item.
3167 if ( event
.LeftDown() )
3172 // here we process only the messages which happen on tree items
3176 if (item
== NULL
) return; /* we hit the blank area */
3178 if ( event
.RightDown() )
3180 DoSelectItem(item
, true, false);
3181 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3182 nevent
.m_item
= item
;
3183 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3184 nevent
.SetEventObject(this);
3185 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3187 else if ( event
.LeftUp() )
3189 // this facilitates multiple-item drag-and-drop
3191 if (item
&& HasFlag(wxTR_MULTIPLE
))
3193 wxArrayTreeItemIds selections
;
3194 size_t count
= GetSelections(selections
);
3197 !event
.ControlDown() &&
3200 DoSelectItem(item
, true, false);
3206 if ( (item
== m_current
) &&
3207 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3208 HasFlag(wxTR_EDIT_LABELS
) )
3210 if ( m_renameTimer
)
3212 if ( m_renameTimer
->IsRunning() )
3213 m_renameTimer
->Stop();
3217 m_renameTimer
= new wxTreeRenameTimer( this );
3220 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3223 m_lastOnSame
= false;
3226 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3228 if ( event
.LeftDown() )
3230 m_lastOnSame
= item
== m_current
;
3233 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3235 // only toggle the item for a single click, double click on
3236 // the button doesn't do anything (it toggles the item twice)
3237 if ( event
.LeftDown() )
3242 // don't select the item if the button was clicked
3247 // clear the previously selected items, if the
3248 // user clicked outside of the present selection.
3249 // otherwise, perform the deselection on mouse-up.
3250 // this allows multiple drag and drop to work.
3252 if (!IsSelected(item
))
3254 // how should the selection work for this event?
3255 bool is_multiple
, extended_select
, unselect_others
;
3256 EventFlagsToSelType(GetWindowStyleFlag(),
3258 event
.ControlDown(),
3259 is_multiple
, extended_select
, unselect_others
);
3261 DoSelectItem(item
, unselect_others
, extended_select
);
3265 // For some reason, Windows isn't recognizing a left double-click,
3266 // so we need to simulate it here. Allow 200 milliseconds for now.
3267 if ( event
.LeftDClick() )
3269 // double clicking should not start editing the item label
3270 if ( m_renameTimer
)
3271 m_renameTimer
->Stop();
3273 m_lastOnSame
= false;
3275 // send activate event first
3276 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3277 nevent
.m_item
= item
;
3278 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3279 nevent
.SetEventObject( this );
3280 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3282 // if the user code didn't process the activate event,
3283 // handle it ourselves by toggling the item when it is
3285 if ( item
->HasPlus() )
3295 void wxGenericTreeCtrl::OnInternalIdle()
3297 wxWindow::OnInternalIdle();
3299 // Check if we need to select the root item
3300 // because nothing else has been selected.
3301 // Delaying it means that we can invoke event handlers
3302 // as required, when a first item is selected.
3303 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3306 SelectItem(m_select_me
);
3307 else if (GetRootItem().IsOk())
3308 SelectItem(GetRootItem());
3311 /* after all changes have been done to the tree control,
3312 * we actually redraw the tree when everything is over */
3314 if (!m_dirty
) return;
3315 if (m_freezeCount
) return;
3319 CalculatePositions();
3321 AdjustMyScrollbars();
3324 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3329 wxTreeItemAttr
*attr
= item
->GetAttributes();
3330 if ( attr
&& attr
->HasFont() )
3331 dc
.SetFont(attr
->GetFont());
3332 else if ( item
->IsBold() )
3333 dc
.SetFont(m_boldFont
);
3335 dc
.SetFont(m_normalFont
);
3337 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3340 // restore normal font
3341 dc
.SetFont( m_normalFont
);
3345 int image
= item
->GetCurrentImage();
3346 if ( image
!= NO_IMAGE
)
3348 if ( m_imageListNormal
)
3350 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3355 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3358 total_h
+= 2; // at least 2 pixels
3360 total_h
+= total_h
/10; // otherwise 10% extra spacing
3362 item
->SetHeight(total_h
);
3363 if (total_h
>m_lineHeight
)
3364 m_lineHeight
=total_h
;
3366 item
->SetWidth(image_w
+text_w
+2);
3369 // -----------------------------------------------------------------------------
3370 // for developper : y is now the top of the level
3371 // not the middle of it !
3372 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3374 int x
= level
*m_indent
;
3375 if (!HasFlag(wxTR_HIDE_ROOT
))
3379 else if (level
== 0)
3381 // a hidden root is not evaluated, but its
3382 // children are always calculated
3386 CalculateSize( item
, dc
);
3389 item
->SetX( x
+m_spacing
);
3391 y
+= GetLineHeight(item
);
3393 if ( !item
->IsExpanded() )
3395 // we don't need to calculate collapsed branches
3400 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3401 size_t n
, count
= children
.Count();
3403 for (n
= 0; n
< count
; ++n
)
3404 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3407 void wxGenericTreeCtrl::CalculatePositions()
3409 if ( !m_anchor
) return;
3411 wxClientDC
dc(this);
3414 dc
.SetFont( m_normalFont
);
3416 dc
.SetPen( m_dottedPen
);
3417 //if(GetImageList() == NULL)
3418 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3421 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3424 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3426 if (m_dirty
) return;
3427 if (m_freezeCount
) return;
3429 wxSize client
= GetClientSize();
3432 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3433 rect
.width
= client
.x
;
3434 rect
.height
= client
.y
;
3436 Refresh(true, &rect
);
3438 AdjustMyScrollbars();
3441 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3443 if (m_dirty
) return;
3444 if (m_freezeCount
) return;
3447 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3448 rect
.width
= GetClientSize().x
;
3449 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3451 Refresh(true, &rect
);
3454 void wxGenericTreeCtrl::RefreshSelected()
3456 if (m_freezeCount
) return;
3458 // TODO: this is awfully inefficient, we should keep the list of all
3459 // selected items internally, should be much faster
3461 RefreshSelectedUnder(m_anchor
);
3464 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3466 if (m_freezeCount
) return;
3468 if ( item
->IsSelected() )
3471 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3472 size_t count
= children
.GetCount();
3473 for ( size_t n
= 0; n
< count
; n
++ )
3475 RefreshSelectedUnder(children
[n
]);
3479 void wxGenericTreeCtrl::Freeze()
3484 void wxGenericTreeCtrl::Thaw()
3486 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3488 if ( !--m_freezeCount
)
3494 // ----------------------------------------------------------------------------
3495 // changing colours: we need to refresh the tree control
3496 // ----------------------------------------------------------------------------
3498 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3500 if ( !wxWindow::SetBackgroundColour(colour
) )
3503 if (m_freezeCount
) return true;
3510 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3512 if ( !wxWindow::SetForegroundColour(colour
) )
3515 if (m_freezeCount
) return true;
3522 // Process the tooltip event, to speed up event processing.
3523 // Doesn't actually get a tooltip.
3524 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3530 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3532 // something is better than nothing...
3533 // 100x80 is what the MSW version will get from the default
3534 // wxControl::DoGetBestSize
3535 return wxSize(100,80);
3539 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3540 // be removed, as well as the #else case below.
3541 #define _USE_VISATTR 0
3544 #include "wx/listbox.h"
3550 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3552 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3556 // Use the same color scheme as wxListBox
3557 return wxListBox::GetClassDefaultAttributes(variant
);
3559 wxVisualAttributes attr
;
3560 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3561 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3562 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3567 #if WXWIN_COMPATIBILITY_2_4
3569 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
3571 return GetItemImage(item
, wxTreeItemIcon_Selected
);
3574 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
3576 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
3579 #endif // WXWIN_COMPATIBILITY_2_4
3581 #if WXWIN_COMPATIBILITY_2_2
3583 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
3585 return GetItemParent( item
);
3588 #endif // WXWIN_COMPATIBILITY_2_2
3590 #endif // wxUSE_TREECTRL