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"
43 // -----------------------------------------------------------------------------
45 // -----------------------------------------------------------------------------
47 class WXDLLEXPORT wxGenericTreeItem
;
49 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 static const int NO_IMAGE
= -1;
57 static const int PIXELS_PER_UNIT
= 10;
59 // -----------------------------------------------------------------------------
61 // -----------------------------------------------------------------------------
63 // timer used for enabling in-place edit
64 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
67 // start editing the current item after half a second (if the mouse hasn't
68 // been clicked/moved)
71 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
73 virtual void Notify();
76 wxGenericTreeCtrl
*m_owner
;
78 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
81 // control used for in-place edit
82 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
85 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
88 void OnChar( wxKeyEvent
&event
);
89 void OnKeyUp( wxKeyEvent
&event
);
90 void OnKillFocus( wxFocusEvent
&event
);
96 wxGenericTreeCtrl
*m_owner
;
97 wxGenericTreeItem
*m_itemEdited
;
98 wxString m_startValue
;
101 DECLARE_EVENT_TABLE()
102 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
105 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
106 // for a sufficiently long time
107 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
110 // reset the current prefix after half a second of inactivity
111 enum { DELAY
= 500 };
113 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
115 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
118 wxGenericTreeCtrl
*m_owner
;
120 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
124 class WXDLLEXPORT wxGenericTreeItem
128 wxGenericTreeItem() { m_data
= NULL
; }
129 wxGenericTreeItem( wxGenericTreeItem
*parent
,
130 const wxString
& text
,
133 wxTreeItemData
*data
);
135 ~wxGenericTreeItem();
138 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
140 const wxString
& GetText() const { return m_text
; }
141 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
142 { return m_images
[which
]; }
143 wxTreeItemData
*GetData() const { return m_data
; }
145 // returns the current image for the item (depending on its
146 // selected/expanded/whatever state)
147 int GetCurrentImage() const;
149 void SetText( const wxString
&text
);
150 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
151 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
153 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
155 void SetBold(bool bold
) { m_isBold
= bold
; }
157 int GetX() const { return m_x
; }
158 int GetY() const { return m_y
; }
160 void SetX(int x
) { m_x
= x
; }
161 void SetY(int y
) { m_y
= y
; }
163 int GetHeight() const { return m_height
; }
164 int GetWidth() const { return m_width
; }
166 void SetHeight(int h
) { m_height
= h
; }
167 void SetWidth(int w
) { m_width
= w
; }
169 wxGenericTreeItem
*GetParent() const { return m_parent
; }
172 // deletes all children notifying the treectrl about it if !NULL
174 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
176 // get count of all children (and grand children if 'recursively')
177 size_t GetChildrenCount(bool recursively
= TRUE
) const;
179 void Insert(wxGenericTreeItem
*child
, size_t index
)
180 { m_children
.Insert(child
, index
); }
182 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
184 // return the item at given position (or NULL if no item), onButton is
185 // TRUE if the point belongs to the item's button, otherwise it lies
186 // on the item's label
187 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
188 const wxGenericTreeCtrl
*,
192 void Expand() { m_isCollapsed
= FALSE
; }
193 void Collapse() { m_isCollapsed
= TRUE
; }
195 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
198 bool HasChildren() const { return !m_children
.IsEmpty(); }
199 bool IsSelected() const { return m_hasHilight
!= 0; }
200 bool IsExpanded() const { return !m_isCollapsed
; }
201 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
202 bool IsBold() const { return m_isBold
!= 0; }
205 // get them - may be NULL
206 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
207 // get them ensuring that the pointer is not NULL
208 wxTreeItemAttr
& Attr()
212 m_attr
= new wxTreeItemAttr
;
218 void SetAttributes(wxTreeItemAttr
*attr
)
220 if ( m_ownsAttr
) delete m_attr
;
224 // set them and delete when done
225 void AssignAttributes(wxTreeItemAttr
*attr
)
232 // since there can be very many of these, we save size by chosing
233 // the smallest representation for the elements and by ordering
234 // the members to avoid padding.
235 wxString m_text
; // label to be rendered for item
237 wxTreeItemData
*m_data
; // user-provided data
239 wxArrayGenericTreeItems m_children
; // list of children
240 wxGenericTreeItem
*m_parent
; // parent of this item
242 wxTreeItemAttr
*m_attr
; // attributes???
244 // tree ctrl images for the normal, selected, expanded and
245 // expanded+selected states
246 short m_images
[wxTreeItemIcon_Max
];
248 wxCoord m_x
; // (virtual) offset from top
249 wxCoord m_y
; // (virtual) offset from left
250 short m_width
; // width of this item
251 unsigned char m_height
; // height of this item
253 // use bitfields to save size
254 int m_isCollapsed
:1;
255 int m_hasHilight
:1; // same as focused
256 int m_hasPlus
:1; // used for item which doesn't have
257 // children but has a [+] button
258 int m_isBold
:1; // render the label in bold font
259 int m_ownsAttr
:1; // delete attribute when done
261 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
264 // =============================================================================
266 // =============================================================================
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 // translate the key or mouse event flags to the type of selection we're
274 static void EventFlagsToSelType(long style
,
278 bool &extended_select
,
279 bool &unselect_others
)
281 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
282 extended_select
= shiftDown
&& is_multiple
;
283 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
286 // check if the given item is under another one
287 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
291 if ( item
== parent
)
293 // item is a descendant of parent
297 item
= item
->GetParent();
303 // -----------------------------------------------------------------------------
304 // wxTreeRenameTimer (internal)
305 // -----------------------------------------------------------------------------
307 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
312 void wxTreeRenameTimer::Notify()
314 m_owner
->OnRenameTimer();
317 //-----------------------------------------------------------------------------
318 // wxTreeTextCtrl (internal)
319 //-----------------------------------------------------------------------------
321 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
322 EVT_CHAR (wxTreeTextCtrl::OnChar
)
323 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
324 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
327 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
328 wxGenericTreeItem
*item
)
329 : m_itemEdited(item
), m_startValue(item
->GetText())
334 int w
= m_itemEdited
->GetWidth(),
335 h
= m_itemEdited
->GetHeight();
338 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
343 int image
= item
->GetCurrentImage();
344 if ( image
!= NO_IMAGE
)
346 if ( m_owner
->m_imageListNormal
)
348 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
353 wxFAIL_MSG(_T("you must create an image list to use images!"));
357 // FIXME: what are all these hardcoded 4, 8 and 11s really?
361 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
362 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
365 bool wxTreeTextCtrl::AcceptChanges()
367 const wxString value
= GetValue();
369 if ( value
== m_startValue
)
371 // nothing changed, always accept
375 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
377 // vetoed by the user
381 // accepted, do rename the item
382 m_owner
->SetItemText(m_itemEdited
, value
);
387 void wxTreeTextCtrl::Finish()
391 m_owner
->ResetTextControl();
393 wxPendingDelete
.Append(this);
397 m_owner
->SetFocus(); // This doesn't work. TODO.
401 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
403 switch ( event
.m_keyCode
)
406 if ( !AcceptChanges() )
408 // vetoed by the user, don't disappear
415 m_owner
->OnRenameCancelled(m_itemEdited
);
423 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
427 // auto-grow the textctrl:
428 wxSize parentSize
= m_owner
->GetSize();
429 wxPoint myPos
= GetPosition();
430 wxSize mySize
= GetSize();
432 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
433 if (myPos
.x
+ sx
> parentSize
.x
)
434 sx
= parentSize
.x
- myPos
.x
;
443 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
451 if ( AcceptChanges() )
457 // -----------------------------------------------------------------------------
459 // -----------------------------------------------------------------------------
461 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
462 const wxString
& text
,
463 int image
, int selImage
,
464 wxTreeItemData
*data
)
467 m_images
[wxTreeItemIcon_Normal
] = image
;
468 m_images
[wxTreeItemIcon_Selected
] = selImage
;
469 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
470 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
475 m_isCollapsed
= TRUE
;
476 m_hasHilight
= FALSE
;
482 m_attr
= (wxTreeItemAttr
*)NULL
;
485 // We don't know the height here yet.
490 wxGenericTreeItem::~wxGenericTreeItem()
494 if (m_ownsAttr
) delete m_attr
;
496 wxASSERT_MSG( m_children
.IsEmpty(),
497 wxT("please call DeleteChildren() before deleting the item") );
500 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
502 size_t count
= m_children
.Count();
503 for ( size_t n
= 0; n
< count
; n
++ )
505 wxGenericTreeItem
*child
= m_children
[n
];
507 tree
->SendDeleteEvent(child
);
509 child
->DeleteChildren(tree
);
516 void wxGenericTreeItem::SetText( const wxString
&text
)
521 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
523 size_t count
= m_children
.Count();
527 size_t total
= count
;
528 for (size_t n
= 0; n
< count
; ++n
)
530 total
+= m_children
[n
]->GetChildrenCount();
536 void wxGenericTreeItem::GetSize( int &x
, int &y
,
537 const wxGenericTreeCtrl
*theButton
)
539 int bottomY
=m_y
+theButton
->GetLineHeight(this);
540 if ( y
< bottomY
) y
= bottomY
;
541 int width
= m_x
+ m_width
;
542 if ( x
< width
) x
= width
;
546 size_t count
= m_children
.Count();
547 for ( size_t n
= 0; n
< count
; ++n
)
549 m_children
[n
]->GetSize( x
, y
, theButton
);
554 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
555 const wxGenericTreeCtrl
*theCtrl
,
559 // for a hidden root node, don't evaluate it, but do evaluate children
560 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
563 int h
= theCtrl
->GetLineHeight(this);
564 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
566 int y_mid
= m_y
+ h
/2;
567 if (point
.y
< y_mid
)
568 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
570 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
572 int xCross
= m_x
- theCtrl
->GetSpacing();
574 // according to the drawing code the triangels are drawn
575 // at -4 , -4 from the position up to +10/+10 max
576 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
577 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
578 HasPlus() && theCtrl
->HasButtons() )
580 // 5 is the size of the plus sign
581 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
582 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
583 HasPlus() && theCtrl
->HasButtons() )
586 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
590 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
595 // assuming every image (normal and selected) has the same size!
596 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
597 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
600 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
601 flags
|= wxTREE_HITTEST_ONITEMICON
;
603 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
609 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
610 if (point
.x
> m_x
+m_width
)
611 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
616 // if children are expanded, fall through to evaluate them
617 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
621 size_t count
= m_children
.Count();
622 for ( size_t n
= 0; n
< count
; n
++ )
624 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
632 return (wxGenericTreeItem
*) NULL
;
635 int wxGenericTreeItem::GetCurrentImage() const
637 int image
= NO_IMAGE
;
642 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
645 if ( image
== NO_IMAGE
)
647 // we usually fall back to the normal item, but try just the
648 // expanded one (and not selected) first in this case
649 image
= GetImage(wxTreeItemIcon_Expanded
);
655 image
= GetImage(wxTreeItemIcon_Selected
);
658 // maybe it doesn't have the specific image we want,
659 // try the default one instead
660 if ( image
== NO_IMAGE
) image
= GetImage();
665 // -----------------------------------------------------------------------------
666 // wxGenericTreeCtrl implementation
667 // -----------------------------------------------------------------------------
669 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
671 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
672 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
673 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
674 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
675 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
676 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
679 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
681 * wxTreeCtrl has to be a real class or we have problems with
682 * the run-time information.
685 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
688 // -----------------------------------------------------------------------------
689 // construction/destruction
690 // -----------------------------------------------------------------------------
692 void wxGenericTreeCtrl::Init()
694 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
702 m_hilightBrush
= new wxBrush
704 wxSystemSettings::GetColour
706 wxSYS_COLOUR_HIGHLIGHT
711 m_hilightUnfocusedBrush
= new wxBrush
713 wxSystemSettings::GetColour
715 wxSYS_COLOUR_BTNSHADOW
720 m_imageListNormal
= m_imageListButtons
=
721 m_imageListState
= (wxImageList
*) NULL
;
722 m_ownsImageListNormal
= m_ownsImageListButtons
=
723 m_ownsImageListState
= FALSE
;
726 m_isDragging
= FALSE
;
727 m_dropTarget
= m_oldSelection
= NULL
;
731 m_renameTimer
= NULL
;
734 m_lastOnSame
= FALSE
;
736 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
737 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
738 m_normalFont
.GetFamily(),
739 m_normalFont
.GetStyle(),
741 m_normalFont
.GetUnderlined(),
742 m_normalFont
.GetFaceName(),
743 m_normalFont
.GetEncoding());
746 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
751 const wxValidator
& wxVALIDATOR_PARAM(validator
),
752 const wxString
& name
)
756 wxGetOsVersion( &major
, &minor
);
758 style
&= ~wxTR_LINES_AT_ROOT
;
759 style
|= wxTR_NO_LINES
;
761 style
|= wxTR_ROW_LINES
;
764 wxScrolledWindow::Create( parent
, id
, pos
, size
,
765 style
|wxHSCROLL
|wxVSCROLL
, name
);
767 // If the tree display has no buttons, but does have
768 // connecting lines, we can use a narrower layout.
769 // It may not be a good idea to force this...
770 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
777 SetValidator( validator
);
780 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
781 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
783 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
784 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
789 wxGenericTreeCtrl::~wxGenericTreeCtrl()
791 delete m_hilightBrush
;
792 delete m_hilightUnfocusedBrush
;
796 delete m_renameTimer
;
799 if (m_ownsImageListNormal
)
800 delete m_imageListNormal
;
801 if (m_ownsImageListState
)
802 delete m_imageListState
;
803 if (m_ownsImageListButtons
)
804 delete m_imageListButtons
;
807 // -----------------------------------------------------------------------------
809 // -----------------------------------------------------------------------------
811 size_t wxGenericTreeCtrl::GetCount() const
813 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
816 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
818 m_indent
= (unsigned short) indent
;
822 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
824 m_spacing
= (unsigned short) spacing
;
828 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
830 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
832 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
835 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
837 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
839 // if we will hide the root, make sure children are visible
840 m_anchor
->SetHasPlus();
842 CalculatePositions();
845 // right now, just sets the styles. Eventually, we may
846 // want to update the inherited styles, but right now
847 // none of the parents has updatable styles
848 m_windowStyle
= styles
;
852 // -----------------------------------------------------------------------------
853 // functions to work with tree items
854 // -----------------------------------------------------------------------------
856 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
858 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
860 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
863 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
864 wxTreeItemIcon which
) const
866 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
868 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
871 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
873 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
875 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
878 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
880 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
882 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
883 return pItem
->Attr().GetTextColour();
886 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
888 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
890 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
891 return pItem
->Attr().GetBackgroundColour();
894 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
896 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
898 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
899 return pItem
->Attr().GetFont();
902 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
904 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
907 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
908 pItem
->SetText(text
);
909 CalculateSize(pItem
, dc
);
913 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
915 wxTreeItemIcon which
)
917 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
919 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
920 pItem
->SetImage(image
, which
);
923 CalculateSize(pItem
, dc
);
927 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
929 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
931 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
934 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
936 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
938 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
939 pItem
->SetHasPlus(has
);
943 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
945 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
947 // avoid redrawing the tree if no real change
948 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
949 if ( pItem
->IsBold() != bold
)
951 pItem
->SetBold(bold
);
956 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
959 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
961 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
962 pItem
->Attr().SetTextColour(col
);
966 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
969 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
971 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
972 pItem
->Attr().SetBackgroundColour(col
);
976 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
978 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
980 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
981 pItem
->Attr().SetFont(font
);
985 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
987 wxScrolledWindow::SetFont(font
);
989 m_normalFont
= font
;
990 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
991 m_normalFont
.GetFamily(),
992 m_normalFont
.GetStyle(),
994 m_normalFont
.GetUnderlined(),
995 m_normalFont
.GetFaceName(),
996 m_normalFont
.GetEncoding());
1002 // -----------------------------------------------------------------------------
1003 // item status inquiries
1004 // -----------------------------------------------------------------------------
1006 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1008 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1010 // An item is only visible if it's not a descendant of a collapsed item
1011 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1012 wxGenericTreeItem
* parent
= pItem
->GetParent();
1015 if (!parent
->IsExpanded())
1017 parent
= parent
->GetParent();
1021 GetViewStart(& startX
, & startY
);
1023 wxSize clientSize
= GetClientSize();
1026 if (!GetBoundingRect(item
, rect
))
1028 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1030 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1032 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1038 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1040 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1042 // consider that the item does have children if it has the "+" button: it
1043 // might not have them (if it had never been expanded yet) but then it
1044 // could have them as well and it's better to err on this side rather than
1045 // disabling some operations which are restricted to the items with
1046 // children for an item which does have them
1047 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1050 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1052 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1054 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1057 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1059 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1061 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1064 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1066 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1068 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1071 // -----------------------------------------------------------------------------
1073 // -----------------------------------------------------------------------------
1075 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1077 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1079 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1082 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1083 wxTreeItemIdValue
& cookie
) const
1085 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1088 return GetNextChild(item
, cookie
);
1091 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1092 wxTreeItemIdValue
& cookie
) const
1094 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1096 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1098 // it's ok to cast cookie to size_t, we never have indices big enough to
1099 // overflow "void *"
1100 size_t *pIndex
= (size_t *)&cookie
;
1101 if ( *pIndex
< children
.Count() )
1103 return children
.Item((*pIndex
)++);
1107 // there are no more of them
1108 return wxTreeItemId();
1112 #if WXWIN_COMPATIBILITY_2_4
1114 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1117 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1120 return GetNextChild(item
, cookie
);
1123 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1126 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1128 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1129 if ( (size_t)cookie
< children
.Count() )
1131 return children
.Item((size_t)cookie
++);
1135 // there are no more of them
1136 return wxTreeItemId();
1140 #endif // WXWIN_COMPATIBILITY_2_4
1142 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1144 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1146 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1147 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1150 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1152 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1154 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1155 wxGenericTreeItem
*parent
= i
->GetParent();
1156 if ( parent
== NULL
)
1158 // root item doesn't have any siblings
1159 return wxTreeItemId();
1162 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1163 int index
= siblings
.Index(i
);
1164 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1166 size_t n
= (size_t)(index
+ 1);
1167 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1170 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1172 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1174 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1175 wxGenericTreeItem
*parent
= i
->GetParent();
1176 if ( parent
== NULL
)
1178 // root item doesn't have any siblings
1179 return wxTreeItemId();
1182 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1183 int index
= siblings
.Index(i
);
1184 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1186 return index
== 0 ? wxTreeItemId()
1187 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1190 // Only for internal use right now, but should probably be public
1191 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1193 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1195 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1197 // First see if there are any children.
1198 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1199 if (children
.GetCount() > 0)
1201 return children
.Item(0);
1205 // Try a sibling of this or ancestor instead
1206 wxTreeItemId p
= item
;
1207 wxTreeItemId toFind
;
1210 toFind
= GetNextSibling(p
);
1211 p
= GetItemParent(p
);
1212 } while (p
.IsOk() && !toFind
.IsOk());
1217 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1219 wxTreeItemId id
= GetRootItem();
1228 } while (id
.IsOk());
1230 return wxTreeItemId();
1233 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1235 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1237 wxTreeItemId id
= item
;
1240 while (id
= GetNext(id
), id
.IsOk())
1246 return wxTreeItemId();
1249 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1251 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1253 wxFAIL_MSG(wxT("not implemented"));
1255 return wxTreeItemId();
1258 // called by wxTextTreeCtrl when it marks itself for deletion
1259 void wxGenericTreeCtrl::ResetTextControl()
1264 // find the first item starting with the given prefix after the given item
1265 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1266 const wxString
& prefixOrig
) const
1268 // match is case insensitive as this is more convenient to the user: having
1269 // to press Shift-letter to go to the item starting with a capital letter
1270 // would be too bothersome
1271 wxString prefix
= prefixOrig
.Lower();
1273 // determine the starting point: we shouldn't take the current item (this
1274 // allows to switch between two items starting with the same letter just by
1275 // pressing it) but we shouldn't jump to the next one if the user is
1276 // continuing to type as otherwise he might easily skip the item he wanted
1277 wxTreeItemId id
= idParent
;
1278 if ( prefix
.length() == 1 )
1283 // look for the item starting with the given prefix after it
1284 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1289 // if we haven't found anything...
1292 // ... wrap to the beginning
1294 if ( HasFlag(wxTR_HIDE_ROOT
) )
1296 // can't select virtual root
1300 // and try all the items (stop when we get to the one we started from)
1301 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1310 // -----------------------------------------------------------------------------
1312 // -----------------------------------------------------------------------------
1314 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1316 const wxString
& text
,
1317 int image
, int selImage
,
1318 wxTreeItemData
*data
)
1320 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1323 // should we give a warning here?
1324 return AddRoot(text
, image
, selImage
, data
);
1327 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1329 wxGenericTreeItem
*item
=
1330 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1334 data
->m_pItem
= item
;
1337 parent
->Insert( item
, previous
);
1342 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1343 int image
, int selImage
,
1344 wxTreeItemData
*data
)
1346 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1348 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1350 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1351 image
, selImage
, data
);
1354 data
->m_pItem
= m_anchor
;
1357 if (HasFlag(wxTR_HIDE_ROOT
))
1359 // if root is hidden, make sure we can navigate
1361 m_anchor
->SetHasPlus();
1363 CalculatePositions();
1366 if (!HasFlag(wxTR_MULTIPLE
))
1368 m_current
= m_key_current
= m_anchor
;
1369 m_current
->SetHilight( TRUE
);
1375 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1376 const wxString
& text
,
1377 int image
, int selImage
,
1378 wxTreeItemData
*data
)
1380 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1383 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1384 const wxTreeItemId
& idPrevious
,
1385 const wxString
& text
,
1386 int image
, int selImage
,
1387 wxTreeItemData
*data
)
1389 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1392 // should we give a warning here?
1393 return AddRoot(text
, image
, selImage
, data
);
1397 if (idPrevious
.IsOk())
1399 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1400 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1401 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1404 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1407 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1409 const wxString
& text
,
1410 int image
, int selImage
,
1411 wxTreeItemData
*data
)
1413 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1416 // should we give a warning here?
1417 return AddRoot(text
, image
, selImage
, data
);
1420 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1423 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1424 const wxString
& text
,
1425 int image
, int selImage
,
1426 wxTreeItemData
*data
)
1428 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1431 // should we give a warning here?
1432 return AddRoot(text
, image
, selImage
, data
);
1435 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1436 image
, selImage
, data
);
1439 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1441 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1442 event
.m_item
= item
;
1443 event
.SetEventObject( this );
1444 ProcessEvent( event
);
1447 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1449 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1451 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1452 item
->DeleteChildren(this);
1455 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1457 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1459 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1461 wxGenericTreeItem
*parent
= item
->GetParent();
1463 // don't keep stale pointers around!
1464 if ( IsDescendantOf(item
, m_key_current
) )
1466 // Don't silently change the selection:
1467 // do it properly in idle time, so event
1468 // handlers get called.
1470 // m_key_current = parent;
1471 m_key_current
= NULL
;
1474 // m_select_me records whether we need to select
1475 // a different item, in idle time.
1476 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1478 m_select_me
= parent
;
1481 if ( IsDescendantOf(item
, m_current
) )
1483 // Don't silently change the selection:
1484 // do it properly in idle time, so event
1485 // handlers get called.
1487 // m_current = parent;
1489 m_select_me
= parent
;
1492 // remove the item from the tree
1495 parent
->GetChildren().Remove( item
); // remove by value
1497 else // deleting the root
1499 // nothing will be left in the tree
1503 // and delete all of its children and the item itself now
1504 item
->DeleteChildren(this);
1505 SendDeleteEvent(item
);
1509 void wxGenericTreeCtrl::DeleteAllItems()
1517 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1519 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1521 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1522 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1523 _T("can't expand hidden root") );
1525 if ( !item
->HasPlus() )
1528 if ( item
->IsExpanded() )
1531 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1532 event
.m_item
= item
;
1533 event
.SetEventObject( this );
1535 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1537 // cancelled by program
1542 CalculatePositions();
1544 RefreshSubtree(item
);
1546 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1547 ProcessEvent( event
);
1550 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1552 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1555 if ( !IsExpanded(item
) )
1559 wxTreeItemIdValue cookie
;
1560 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1561 while ( child
.IsOk() )
1565 child
= GetNextChild(item
, cookie
);
1569 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1571 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1572 _T("can't collapse hidden root") );
1574 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1576 if ( !item
->IsExpanded() )
1579 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1580 event
.m_item
= item
;
1581 event
.SetEventObject( this );
1582 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1584 // cancelled by program
1590 #if 0 // TODO why should items be collapsed recursively?
1591 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1592 size_t count
= children
.Count();
1593 for ( size_t n
= 0; n
< count
; n
++ )
1595 Collapse(children
[n
]);
1599 CalculatePositions();
1601 RefreshSubtree(item
);
1603 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1604 ProcessEvent( event
);
1607 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1610 DeleteChildren(item
);
1613 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1615 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1617 if (item
->IsExpanded())
1623 void wxGenericTreeCtrl::Unselect()
1627 m_current
->SetHilight( FALSE
);
1628 RefreshLine( m_current
);
1635 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1637 if (item
->IsSelected())
1639 item
->SetHilight(FALSE
);
1643 if (item
->HasChildren())
1645 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1646 size_t count
= children
.Count();
1647 for ( size_t n
= 0; n
< count
; ++n
)
1649 UnselectAllChildren(children
[n
]);
1654 void wxGenericTreeCtrl::UnselectAll()
1656 wxTreeItemId rootItem
= GetRootItem();
1658 // the tree might not have the root item at all
1661 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1665 // Recursive function !
1666 // To stop we must have crt_item<last_item
1668 // Tag all next children, when no more children,
1669 // Move to parent (not to tag)
1670 // Keep going... if we found last_item, we stop.
1671 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1673 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1675 if (parent
== NULL
) // This is root item
1676 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1678 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1679 int index
= children
.Index(crt_item
);
1680 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1682 size_t count
= children
.Count();
1683 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1685 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1688 return TagNextChildren(parent
, last_item
, select
);
1691 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1693 crt_item
->SetHilight(select
);
1694 RefreshLine(crt_item
);
1696 if (crt_item
==last_item
)
1699 if (crt_item
->HasChildren())
1701 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1702 size_t count
= children
.Count();
1703 for ( size_t n
= 0; n
< count
; ++n
)
1705 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1713 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1717 // item2 is not necessary after item1
1718 // choice first' and 'last' between item1 and item2
1719 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1720 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1722 bool select
= m_current
->IsSelected();
1724 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1727 TagNextChildren(first
,last
,select
);
1730 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1731 bool unselect_others
,
1732 bool extended_select
)
1734 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1738 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1739 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1741 //wxCHECK_RET( ( (!unselect_others) && is_single),
1742 // wxT("this is a single selection tree") );
1744 // to keep going anyhow !!!
1747 if (item
->IsSelected())
1748 return; // nothing to do
1749 unselect_others
= TRUE
;
1750 extended_select
= FALSE
;
1752 else if ( unselect_others
&& item
->IsSelected() )
1754 // selection change if there is more than one item currently selected
1755 wxArrayTreeItemIds selected_items
;
1756 if ( GetSelections(selected_items
) == 1 )
1760 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1761 event
.m_item
= item
;
1762 event
.m_itemOld
= m_current
;
1763 event
.SetEventObject( this );
1764 // TODO : Here we don't send any selection mode yet !
1766 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1769 wxTreeItemId parent
= GetItemParent( itemId
);
1770 while (parent
.IsOk())
1772 if (!IsExpanded(parent
))
1775 parent
= GetItemParent( parent
);
1778 EnsureVisible( itemId
);
1781 if (unselect_others
)
1783 if (is_single
) Unselect(); // to speed up thing
1788 if (extended_select
)
1792 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1795 // don't change the mark (m_current)
1796 SelectItemRange(m_current
, item
);
1800 bool select
=TRUE
; // the default
1802 // Check if we need to toggle hilight (ctrl mode)
1803 if (!unselect_others
)
1804 select
=!item
->IsSelected();
1806 m_current
= m_key_current
= item
;
1807 m_current
->SetHilight(select
);
1808 RefreshLine( m_current
);
1811 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1812 GetEventHandler()->ProcessEvent( event
);
1815 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1819 DoSelectItem(itemId
);
1823 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1824 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1826 item
->SetHilight(FALSE
);
1831 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1832 wxArrayTreeItemIds
&array
) const
1834 if ( item
->IsSelected() )
1835 array
.Add(wxTreeItemId(item
));
1837 if ( item
->HasChildren() )
1839 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1840 size_t count
= children
.GetCount();
1841 for ( size_t n
= 0; n
< count
; ++n
)
1842 FillArray(children
[n
], array
);
1846 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1849 wxTreeItemId idRoot
= GetRootItem();
1850 if ( idRoot
.IsOk() )
1852 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1854 //else: the tree is empty, so no selections
1856 return array
.Count();
1859 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1861 if (!item
.IsOk()) return;
1863 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1865 // first expand all parent branches
1866 wxGenericTreeItem
*parent
= gitem
->GetParent();
1868 if ( HasFlag(wxTR_HIDE_ROOT
) )
1870 while ( parent
&& parent
!= m_anchor
)
1873 parent
= parent
->GetParent();
1881 parent
= parent
->GetParent();
1885 //if (parent) CalculatePositions();
1890 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1892 if (!item
.IsOk()) return;
1894 // We have to call this here because the label in
1895 // question might just have been added and no screen
1896 // update taken place.
1897 if (m_dirty
) wxYieldIfNeeded();
1899 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1901 // now scroll to the item
1902 int item_y
= gitem
->GetY();
1906 GetViewStart( &start_x
, &start_y
);
1907 start_y
*= PIXELS_PER_UNIT
;
1911 GetClientSize( &client_w
, &client_h
);
1913 if (item_y
< start_y
+3)
1918 m_anchor
->GetSize( x
, y
, this );
1919 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1920 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1921 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1922 // Item should appear at top
1923 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1925 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1930 m_anchor
->GetSize( x
, y
, this );
1931 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1932 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1933 item_y
+= PIXELS_PER_UNIT
+2;
1934 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1935 // Item should appear at bottom
1936 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
);
1940 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1941 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1943 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1944 wxGenericTreeItem
**item2
)
1946 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1948 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1951 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1952 const wxTreeItemId
& item2
)
1954 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1957 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1959 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1961 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1963 wxCHECK_RET( !s_treeBeingSorted
,
1964 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1966 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1967 if ( children
.Count() > 1 )
1971 s_treeBeingSorted
= this;
1972 children
.Sort(tree_ctrl_compare_func
);
1973 s_treeBeingSorted
= NULL
;
1975 //else: don't make the tree dirty as nothing changed
1978 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1980 return m_imageListNormal
;
1983 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1985 return m_imageListButtons
;
1988 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1990 return m_imageListState
;
1993 void wxGenericTreeCtrl::CalculateLineHeight()
1995 wxClientDC
dc(this);
1996 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1998 if ( m_imageListNormal
)
2000 // Calculate a m_lineHeight value from the normal Image sizes.
2001 // May be toggle off. Then wxGenericTreeCtrl will spread when
2002 // necessary (which might look ugly).
2003 int n
= m_imageListNormal
->GetImageCount();
2004 for (int i
= 0; i
< n
; i
++)
2006 int width
= 0, height
= 0;
2007 m_imageListNormal
->GetSize(i
, width
, height
);
2008 if (height
> m_lineHeight
) m_lineHeight
= height
;
2012 if (m_imageListButtons
)
2014 // Calculate a m_lineHeight value from the Button image sizes.
2015 // May be toggle off. Then wxGenericTreeCtrl will spread when
2016 // necessary (which might look ugly).
2017 int n
= m_imageListButtons
->GetImageCount();
2018 for (int i
= 0; i
< n
; i
++)
2020 int width
= 0, height
= 0;
2021 m_imageListButtons
->GetSize(i
, width
, height
);
2022 if (height
> m_lineHeight
) m_lineHeight
= height
;
2026 if (m_lineHeight
< 30)
2027 m_lineHeight
+= 2; // at least 2 pixels
2029 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2032 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2034 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2035 m_imageListNormal
= imageList
;
2036 m_ownsImageListNormal
= FALSE
;
2038 // Don't do any drawing if we're setting the list to NULL,
2039 // since we may be in the process of deleting the tree control.
2041 CalculateLineHeight();
2044 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2046 if (m_ownsImageListState
) delete m_imageListState
;
2047 m_imageListState
= imageList
;
2048 m_ownsImageListState
= FALSE
;
2051 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2053 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2054 m_imageListButtons
= imageList
;
2055 m_ownsImageListButtons
= FALSE
;
2057 CalculateLineHeight();
2060 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2062 SetImageList(imageList
);
2063 m_ownsImageListNormal
= TRUE
;
2066 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2068 SetStateImageList(imageList
);
2069 m_ownsImageListState
= TRUE
;
2072 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2074 SetButtonsImageList(imageList
);
2075 m_ownsImageListButtons
= TRUE
;
2078 // -----------------------------------------------------------------------------
2080 // -----------------------------------------------------------------------------
2082 void wxGenericTreeCtrl::AdjustMyScrollbars()
2087 m_anchor
->GetSize( x
, y
, this );
2088 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2089 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2090 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2091 int y_pos
= GetScrollPos( wxVERTICAL
);
2092 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2096 SetScrollbars( 0, 0, 0, 0 );
2100 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2102 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2103 return item
->GetHeight();
2105 return m_lineHeight
;
2108 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2110 // TODO implement "state" icon on items
2112 wxTreeItemAttr
*attr
= item
->GetAttributes();
2113 if ( attr
&& attr
->HasFont() )
2114 dc
.SetFont(attr
->GetFont());
2115 else if (item
->IsBold())
2116 dc
.SetFont(m_boldFont
);
2118 long text_w
= 0, text_h
= 0;
2119 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2121 int image_h
= 0, image_w
= 0;
2122 int image
= item
->GetCurrentImage();
2123 if ( image
!= NO_IMAGE
)
2125 if ( m_imageListNormal
)
2127 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2136 int total_h
= GetLineHeight(item
);
2138 if ( item
->IsSelected() )
2140 // under mac selections are only a rectangle in case they don't have the focus
2144 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2145 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2149 dc
.SetBrush( *m_hilightBrush
) ;
2152 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2158 if ( attr
&& attr
->HasBackgroundColour() )
2159 colBg
= attr
->GetBackgroundColour();
2161 colBg
= m_backgroundColour
;
2162 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2165 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2167 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2171 DoGetPosition(&x
, &y
);
2173 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2177 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2179 // If it's selected, and there's an image, then we should
2180 // take care to leave the area under the image painted in the
2181 // background colour.
2182 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2183 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2187 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2188 item
->GetWidth()+2, total_h
-offset
);
2192 if ( image
!= NO_IMAGE
)
2194 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2195 m_imageListNormal
->Draw( image
, dc
,
2197 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2198 wxIMAGELIST_DRAW_TRANSPARENT
);
2199 dc
.DestroyClippingRegion();
2202 dc
.SetBackgroundMode(wxTRANSPARENT
);
2203 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2204 dc
.DrawText( item
->GetText(),
2205 (wxCoord
)(image_w
+ item
->GetX()),
2206 (wxCoord
)(item
->GetY() + extraH
));
2208 // restore normal font
2209 dc
.SetFont( m_normalFont
);
2212 // Now y stands for the top of the item, whereas it used to stand for middle !
2213 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2215 int x
= level
*m_indent
;
2216 if (!HasFlag(wxTR_HIDE_ROOT
))
2220 else if (level
== 0)
2222 // always expand hidden root
2224 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2225 int count
= children
.Count();
2231 PaintLevel(children
[n
], dc
, 1, y
);
2232 } while (++n
< count
);
2234 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2236 // draw line down to last child
2237 origY
+= GetLineHeight(children
[0])>>1;
2238 oldY
+= GetLineHeight(children
[n
-1])>>1;
2239 dc
.DrawLine(3, origY
, 3, oldY
);
2245 item
->SetX(x
+m_spacing
);
2248 int h
= GetLineHeight(item
);
2250 int y_mid
= y_top
+ (h
>>1);
2253 int exposed_x
= dc
.LogicalToDeviceX(0);
2254 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2256 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2260 // don't draw rect outline if we already have the
2261 // background color under Mac
2262 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2263 #endif // !__WXMAC__
2267 if ( item
->IsSelected() )
2269 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2273 wxTreeItemAttr
*attr
= item
->GetAttributes();
2274 if (attr
&& attr
->HasTextColour())
2275 colText
= attr
->GetTextColour();
2277 colText
= GetForegroundColour();
2281 dc
.SetTextForeground(colText
);
2285 PaintItem(item
, dc
);
2287 if (HasFlag(wxTR_ROW_LINES
))
2289 // if the background colour is white, choose a
2290 // contrasting color for the lines
2291 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2292 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2293 dc
.DrawLine(0, y_top
, 10000, y_top
);
2294 dc
.DrawLine(0, y
, 10000, y
);
2297 // restore DC objects
2298 dc
.SetBrush(*wxWHITE_BRUSH
);
2299 dc
.SetPen(m_dottedPen
);
2300 dc
.SetTextForeground(*wxBLACK
);
2302 if ( !HasFlag(wxTR_NO_LINES
) )
2304 // draw the horizontal line here
2306 if (x
> (signed)m_indent
)
2307 x_start
-= m_indent
;
2308 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2310 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2313 // should the item show a button?
2314 if ( item
->HasPlus() && HasButtons() )
2316 if ( m_imageListButtons
)
2318 // draw the image button here
2321 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2322 : wxTreeItemIcon_Normal
;
2323 if ( item
->IsSelected() )
2324 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2326 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2327 int xx
= x
- image_w
/2;
2328 int yy
= y_mid
- image_h
/2;
2330 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2331 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2332 wxIMAGELIST_DRAW_TRANSPARENT
);
2334 else // no custom buttons
2336 static const int wImage
= 9;
2337 static const int hImage
= 9;
2340 if (item
->IsExpanded())
2341 flag
|= wxCONTROL_EXPANDED
;
2342 if (item
== m_underMouse
)
2343 flag
|= wxCONTROL_CURRENT
;
2345 wxRendererNative::Get().DrawTreeItemButton
2349 wxRect(x
- wImage
/2,
2358 if (item
->IsExpanded())
2360 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2361 int count
= children
.Count();
2368 PaintLevel(children
[n
], dc
, level
, y
);
2369 } while (++n
< count
);
2371 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2373 // draw line down to last child
2374 oldY
+= GetLineHeight(children
[n
-1])>>1;
2375 if (HasButtons()) y_mid
+= 5;
2377 // Only draw the portion of the line that is visible, in case it is huge
2378 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2379 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2380 yOrigin
= abs(yOrigin
);
2381 GetClientSize(&width
, &height
);
2383 // Move end points to the begining/end of the view?
2384 if (y_mid
< yOrigin
)
2386 if (oldY
> yOrigin
+ height
)
2387 oldY
= yOrigin
+ height
;
2389 // after the adjustments if y_mid is larger than oldY then the line
2390 // isn't visible at all so don't draw anything
2392 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2398 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2402 if ( item
->HasPlus() )
2404 // it's a folder, indicate it by a border
2409 // draw a line under the drop target because the item will be
2411 DrawLine(item
, TRUE
/* below */);
2414 SetCursor(wxCURSOR_BULLSEYE
);
2419 SetCursor(wxCURSOR_NO_ENTRY
);
2423 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2425 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2427 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2429 wxClientDC
dc(this);
2431 dc
.SetLogicalFunction(wxINVERT
);
2432 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2434 int w
= i
->GetWidth() + 2;
2435 int h
= GetLineHeight(i
) + 2;
2437 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2440 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2442 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2444 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2446 wxClientDC
dc(this);
2448 dc
.SetLogicalFunction(wxINVERT
);
2454 y
+= GetLineHeight(i
) - 1;
2457 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2460 // -----------------------------------------------------------------------------
2461 // wxWindows callbacks
2462 // -----------------------------------------------------------------------------
2464 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2472 dc
.SetFont( m_normalFont
);
2473 dc
.SetPen( m_dottedPen
);
2475 // this is now done dynamically
2476 //if(GetImageList() == NULL)
2477 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2480 PaintLevel( m_anchor
, dc
, 0, y
);
2483 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2492 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2501 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2503 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2504 te
.m_evtKey
= event
;
2505 te
.SetEventObject( this );
2506 if ( GetEventHandler()->ProcessEvent( te
) )
2508 // intercepted by the user code
2512 if ( (m_current
== 0) || (m_key_current
== 0) )
2518 // how should the selection work for this event?
2519 bool is_multiple
, extended_select
, unselect_others
;
2520 EventFlagsToSelType(GetWindowStyleFlag(),
2522 event
.ControlDown(),
2523 is_multiple
, extended_select
, unselect_others
);
2527 // * : Expand all/Collapse all
2528 // ' ' | return : activate
2529 // up : go up (not last children!)
2531 // left : go to parent
2532 // right : open if parent and go next
2533 // home : go to root
2534 // end : go to last item without opening parents
2535 // alnum : start or continue searching for the item with this prefix
2536 int keyCode
= event
.GetKeyCode();
2541 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2549 if ( !IsExpanded(m_current
) )
2552 ExpandAll(m_current
);
2555 //else: fall through to Collapse() it
2559 if (IsExpanded(m_current
))
2561 Collapse(m_current
);
2567 if ( !event
.HasModifiers() )
2569 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2570 event
.m_item
= m_current
;
2571 event
.SetEventObject( this );
2572 GetEventHandler()->ProcessEvent( event
);
2575 // in any case, also generate the normal key event for this key,
2576 // even if we generated the ACTIVATED event above: this is what
2577 // wxMSW does and it makes sense because you might not want to
2578 // process ACTIVATED event at all and handle Space and Return
2579 // directly (and differently) which would be impossible otherwise
2583 // up goes to the previous sibling or to the last
2584 // of its children if it's expanded
2587 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2590 prev
= GetItemParent( m_key_current
);
2591 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2593 break; // don't go to root if it is hidden
2597 wxTreeItemIdValue cookie
;
2598 wxTreeItemId current
= m_key_current
;
2599 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2600 if (current
== GetFirstChild( prev
, cookie
))
2602 // otherwise we return to where we came from
2603 DoSelectItem( prev
, unselect_others
, extended_select
);
2604 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2611 while ( IsExpanded(prev
) && HasChildren(prev
) )
2613 wxTreeItemId child
= GetLastChild(prev
);
2620 DoSelectItem( prev
, unselect_others
, extended_select
);
2621 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2626 // left arrow goes to the parent
2629 wxTreeItemId prev
= GetItemParent( m_current
);
2630 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2632 // don't go to root if it is hidden
2633 prev
= GetPrevSibling( m_current
);
2637 DoSelectItem( prev
, unselect_others
, extended_select
);
2643 // this works the same as the down arrow except that we
2644 // also expand the item if it wasn't expanded yet
2650 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2652 wxTreeItemIdValue cookie
;
2653 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2654 DoSelectItem( child
, unselect_others
, extended_select
);
2655 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2659 wxTreeItemId next
= GetNextSibling( m_key_current
);
2662 wxTreeItemId current
= m_key_current
;
2663 while (current
.IsOk() && !next
)
2665 current
= GetItemParent( current
);
2666 if (current
) next
= GetNextSibling( current
);
2671 DoSelectItem( next
, unselect_others
, extended_select
);
2672 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2678 // <End> selects the last visible tree item
2681 wxTreeItemId last
= GetRootItem();
2683 while ( last
.IsOk() && IsExpanded(last
) )
2685 wxTreeItemId lastChild
= GetLastChild(last
);
2687 // it may happen if the item was expanded but then all of
2688 // its children have been deleted - so IsExpanded() returned
2689 // TRUE, but GetLastChild() returned invalid item
2698 DoSelectItem( last
, unselect_others
, extended_select
);
2703 // <Home> selects the root item
2706 wxTreeItemId prev
= GetRootItem();
2710 if ( HasFlag(wxTR_HIDE_ROOT
) )
2712 wxTreeItemIdValue cookie
;
2713 prev
= GetFirstChild(prev
, cookie
);
2718 DoSelectItem( prev
, unselect_others
, extended_select
);
2723 // do not use wxIsalnum() here
2724 if ( !event
.HasModifiers() &&
2725 ((keyCode
>= '0' && keyCode
<= '9') ||
2726 (keyCode
>= 'a' && keyCode
<= 'z') ||
2727 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2729 // find the next item starting with the given prefix
2730 char ch
= (char)keyCode
;
2732 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2743 // also start the timer to reset the current prefix if the user
2744 // doesn't press any more alnum keys soon -- we wouldn't want
2745 // to use this prefix for a new item search
2748 m_findTimer
= new wxTreeFindTimer(this);
2751 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2760 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2762 // JACS: removed wxYieldIfNeeded() because it can cause the window
2763 // to be deleted from under us if a close window event is pending
2768 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2769 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2770 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2771 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2772 if (flags
) return wxTreeItemId();
2774 if (m_anchor
== NULL
)
2776 flags
= wxTREE_HITTEST_NOWHERE
;
2777 return wxTreeItemId();
2780 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2784 flags
= wxTREE_HITTEST_NOWHERE
;
2785 return wxTreeItemId();
2790 // get the bounding rectangle of the item (or of its label only)
2791 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2793 bool WXUNUSED(textOnly
)) const
2795 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2797 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2800 GetViewStart(& startX
, & startY
);
2802 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2803 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2804 rect
.width
= i
->GetWidth();
2805 //rect.height = i->GetHeight();
2806 rect
.height
= GetLineHeight(i
);
2811 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2813 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2815 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2817 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2818 te
.m_item
= itemEdit
;
2819 te
.SetEventObject( this );
2820 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2826 // We have to call this here because the label in
2827 // question might just have been added and no screen
2828 // update taken place.
2832 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2834 m_textCtrl
->SetFocus();
2837 // returns a pointer to the text edit control if the item is being
2838 // edited, NULL otherwise (it's assumed that no more than one item may
2839 // be edited simultaneously)
2840 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2845 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2846 const wxString
& value
)
2848 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2850 le
.SetEventObject( this );
2852 le
.m_editCancelled
= FALSE
;
2854 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2857 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2859 // let owner know that the edit was cancelled
2860 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2862 le
.SetEventObject( this );
2863 le
.m_label
= wxEmptyString
;
2864 le
.m_editCancelled
= TRUE
;
2866 GetEventHandler()->ProcessEvent( le
);
2872 void wxGenericTreeCtrl::OnRenameTimer()
2877 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2879 if ( !m_anchor
) return;
2881 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2883 // Is the mouse over a tree item button?
2885 wxGenericTreeItem
*underMouse
= m_anchor
->HitTest(pt
, this, flags
, 0);
2887 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
2888 (!event
.LeftIsDown()) &&
2890 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2898 if (underMouse
!= m_underMouse
)
2902 // unhighlight old item
2903 wxGenericTreeItem
*tmp
= m_underMouse
;
2904 m_underMouse
= NULL
;
2908 m_underMouse
= underMouse
;
2910 RefreshLine( m_underMouse
);
2914 // Determines what item we are hovering over and need a tooltip for
2915 wxTreeItemId hoverItem
= HitTest(ScreenToClient(wxGetMousePosition()));
2917 // We do not want a tooltip if we are dragging, or if the rename timer is running
2918 if (hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2920 // Ask the tree control what tooltip (if any) should be shown
2921 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
2922 hevent
.m_item
= hoverItem
;
2923 hevent
.SetEventObject(this);
2925 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
2927 SetToolTip(hevent
.m_label
);
2932 // we process left mouse up event (enables in-place edit), right down
2933 // (pass to the user code), left dbl click (activate item) and
2934 // dragging/moving events for items drag-and-drop
2935 if ( !(event
.LeftDown() ||
2937 event
.RightDown() ||
2938 event
.LeftDClick() ||
2940 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2949 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2951 if ( event
.Dragging() && !m_isDragging
)
2953 if (m_dragCount
== 0)
2958 if (m_dragCount
!= 3)
2960 // wait until user drags a bit further...
2964 wxEventType command
= event
.RightIsDown()
2965 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2966 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2968 wxTreeEvent
nevent( command
, GetId() );
2969 nevent
.m_item
= m_current
;
2970 nevent
.SetEventObject(this);
2972 // by default the dragging is not supported, the user code must
2973 // explicitly allow the event for it to take place
2976 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2978 // we're going to drag this item
2979 m_isDragging
= TRUE
;
2981 // remember the old cursor because we will change it while
2983 m_oldCursor
= m_cursor
;
2985 // in a single selection control, hide the selection temporarily
2986 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2988 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2990 if ( m_oldSelection
)
2992 m_oldSelection
->SetHilight(FALSE
);
2993 RefreshLine(m_oldSelection
);
3000 else if ( event
.Moving() )
3002 if ( item
!= m_dropTarget
)
3004 // unhighlight the previous drop target
3005 DrawDropEffect(m_dropTarget
);
3007 m_dropTarget
= item
;
3009 // highlight the current drop target if any
3010 DrawDropEffect(m_dropTarget
);
3015 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3017 // erase the highlighting
3018 DrawDropEffect(m_dropTarget
);
3020 if ( m_oldSelection
)
3022 m_oldSelection
->SetHilight(TRUE
);
3023 RefreshLine(m_oldSelection
);
3024 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3027 // generate the drag end event
3028 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3030 event
.m_item
= item
;
3031 event
.m_pointDrag
= pt
;
3032 event
.SetEventObject(this);
3034 (void)GetEventHandler()->ProcessEvent(event
);
3036 m_isDragging
= FALSE
;
3037 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3041 SetCursor(m_oldCursor
);
3047 // here we process only the messages which happen on tree items
3051 if (item
== NULL
) return; /* we hit the blank area */
3053 if ( event
.RightDown() )
3055 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3056 nevent
.m_item
= item
;
3057 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3058 nevent
.SetEventObject(this);
3059 GetEventHandler()->ProcessEvent(nevent
);
3061 else if ( event
.LeftUp() )
3063 // this facilitates multiple-item drag-and-drop
3065 if (item
&& HasFlag(wxTR_MULTIPLE
))
3067 wxArrayTreeItemIds selections
;
3068 size_t count
= GetSelections(selections
);
3071 !event
.ControlDown() &&
3074 DoSelectItem(item
, true, false);
3080 if ( (item
== m_current
) &&
3081 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3082 HasFlag(wxTR_EDIT_LABELS
) )
3084 if ( m_renameTimer
)
3086 if ( m_renameTimer
->IsRunning() )
3087 m_renameTimer
->Stop();
3091 m_renameTimer
= new wxTreeRenameTimer( this );
3094 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3097 m_lastOnSame
= FALSE
;
3100 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3102 if ( event
.LeftDown() )
3104 m_lastOnSame
= item
== m_current
;
3107 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3109 // only toggle the item for a single click, double click on
3110 // the button doesn't do anything (it toggles the item twice)
3111 if ( event
.LeftDown() )
3116 // don't select the item if the button was clicked
3121 // clear the previously selected items, if the
3122 // user clicked outside of the present selection.
3123 // otherwise, perform the deselection on mouse-up.
3124 // this allows multiple drag and drop to work.
3126 if (!IsSelected(item
))
3128 // how should the selection work for this event?
3129 bool is_multiple
, extended_select
, unselect_others
;
3130 EventFlagsToSelType(GetWindowStyleFlag(),
3132 event
.ControlDown(),
3133 is_multiple
, extended_select
, unselect_others
);
3135 DoSelectItem(item
, unselect_others
, extended_select
);
3139 // For some reason, Windows isn't recognizing a left double-click,
3140 // so we need to simulate it here. Allow 200 milliseconds for now.
3141 if ( event
.LeftDClick() )
3143 // double clicking should not start editing the item label
3144 if ( m_renameTimer
)
3145 m_renameTimer
->Stop();
3147 m_lastOnSame
= FALSE
;
3149 // send activate event first
3150 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3151 nevent
.m_item
= item
;
3152 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3153 nevent
.SetEventObject( this );
3154 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3156 // if the user code didn't process the activate event,
3157 // handle it ourselves by toggling the item when it is
3159 if ( item
->HasPlus() )
3169 void wxGenericTreeCtrl::OnInternalIdle()
3171 wxWindow::OnInternalIdle();
3173 // Check if we need to select the root item
3174 // because nothing else has been selected.
3175 // Delaying it means that we can invoke event handlers
3176 // as required, when a first item is selected.
3177 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3180 SelectItem(m_select_me
);
3181 else if (GetRootItem().IsOk())
3182 SelectItem(GetRootItem());
3185 /* after all changes have been done to the tree control,
3186 * we actually redraw the tree when everything is over */
3188 if (!m_dirty
) return;
3192 CalculatePositions();
3194 AdjustMyScrollbars();
3197 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3202 wxTreeItemAttr
*attr
= item
->GetAttributes();
3203 if ( attr
&& attr
->HasFont() )
3204 dc
.SetFont(attr
->GetFont());
3205 else if ( item
->IsBold() )
3206 dc
.SetFont(m_boldFont
);
3208 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3211 // restore normal font
3212 dc
.SetFont( m_normalFont
);
3216 int image
= item
->GetCurrentImage();
3217 if ( image
!= NO_IMAGE
)
3219 if ( m_imageListNormal
)
3221 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3226 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3229 total_h
+= 2; // at least 2 pixels
3231 total_h
+= total_h
/10; // otherwise 10% extra spacing
3233 item
->SetHeight(total_h
);
3234 if (total_h
>m_lineHeight
)
3235 m_lineHeight
=total_h
;
3237 item
->SetWidth(image_w
+text_w
+2);
3240 // -----------------------------------------------------------------------------
3241 // for developper : y is now the top of the level
3242 // not the middle of it !
3243 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3245 int x
= level
*m_indent
;
3246 if (!HasFlag(wxTR_HIDE_ROOT
))
3250 else if (level
== 0)
3252 // a hidden root is not evaluated, but its
3253 // children are always calculated
3257 CalculateSize( item
, dc
);
3260 item
->SetX( x
+m_spacing
);
3262 y
+= GetLineHeight(item
);
3264 if ( !item
->IsExpanded() )
3266 // we don't need to calculate collapsed branches
3271 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3272 size_t n
, count
= children
.Count();
3274 for (n
= 0; n
< count
; ++n
)
3275 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3278 void wxGenericTreeCtrl::CalculatePositions()
3280 if ( !m_anchor
) return;
3282 wxClientDC
dc(this);
3285 dc
.SetFont( m_normalFont
);
3287 dc
.SetPen( m_dottedPen
);
3288 //if(GetImageList() == NULL)
3289 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3292 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3295 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3297 if (m_dirty
) return;
3299 wxSize client
= GetClientSize();
3302 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3303 rect
.width
= client
.x
;
3304 rect
.height
= client
.y
;
3306 Refresh(TRUE
, &rect
);
3308 AdjustMyScrollbars();
3311 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3313 if (m_dirty
) return;
3316 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3317 rect
.width
= GetClientSize().x
;
3318 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3320 Refresh(TRUE
, &rect
);
3323 void wxGenericTreeCtrl::RefreshSelected()
3325 // TODO: this is awfully inefficient, we should keep the list of all
3326 // selected items internally, should be much faster
3328 RefreshSelectedUnder(m_anchor
);
3331 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3333 if ( item
->IsSelected() )
3336 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3337 size_t count
= children
.GetCount();
3338 for ( size_t n
= 0; n
< count
; n
++ )
3340 RefreshSelectedUnder(children
[n
]);
3344 // ----------------------------------------------------------------------------
3345 // changing colours: we need to refresh the tree control
3346 // ----------------------------------------------------------------------------
3348 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3350 if ( !wxWindow::SetBackgroundColour(colour
) )
3358 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3360 if ( !wxWindow::SetForegroundColour(colour
) )
3368 #endif // wxUSE_TREECTRL