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, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
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 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 static const int NO_IMAGE
= -1;
55 static const int PIXELS_PER_UNIT
= 10;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
62 static const char *aqua_arrow_right
[] = {
63 /* columns rows colors chars-per-pixel */
84 static const char *aqua_arrow_down
[] = {
85 /* columns rows colors chars-per-pixel */
105 // -----------------------------------------------------------------------------
107 // -----------------------------------------------------------------------------
109 // timer used for enabling in-place edit
110 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
113 // start editing the current item after half a second (if the mouse hasn't
114 // been clicked/moved)
115 enum { DELAY
= 500 };
117 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
119 virtual void Notify();
122 wxGenericTreeCtrl
*m_owner
;
125 // control used for in-place edit
126 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
129 wxTreeTextCtrl( wxWindow
*parent
,
133 wxGenericTreeCtrl
*owner
,
134 const wxString
&value
= wxEmptyString
,
135 const wxPoint
&pos
= wxDefaultPosition
,
136 const wxSize
&size
= wxDefaultSize
,
137 int style
= wxSIMPLE_BORDER
,
138 const wxValidator
& validator
= wxDefaultValidator
,
139 const wxString
&name
= wxTextCtrlNameStr
);
141 void OnChar( wxKeyEvent
&event
);
142 void OnKeyUp( wxKeyEvent
&event
);
143 void OnKillFocus( wxFocusEvent
&event
);
148 wxGenericTreeCtrl
*m_owner
;
149 wxString m_startValue
;
152 DECLARE_EVENT_TABLE()
155 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
156 // for a sufficiently long time
157 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
160 // reset the current prefix after half a second of inactivity
161 enum { DELAY
= 500 };
163 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
165 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
168 wxGenericTreeCtrl
*m_owner
;
172 class WXDLLEXPORT wxGenericTreeItem
176 wxGenericTreeItem() { m_data
= NULL
; }
177 wxGenericTreeItem( wxGenericTreeItem
*parent
,
178 const wxString
& text
,
181 wxTreeItemData
*data
);
183 ~wxGenericTreeItem();
186 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
188 const wxString
& GetText() const { return m_text
; }
189 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
190 { return m_images
[which
]; }
191 wxTreeItemData
*GetData() const { return m_data
; }
193 // returns the current image for the item (depending on its
194 // selected/expanded/whatever state)
195 int GetCurrentImage() const;
197 void SetText( const wxString
&text
);
198 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
199 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
201 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
203 void SetBold(bool bold
) { m_isBold
= bold
; }
205 int GetX() const { return m_x
; }
206 int GetY() const { return m_y
; }
208 void SetX(int x
) { m_x
= x
; }
209 void SetY(int y
) { m_y
= y
; }
211 int GetHeight() const { return m_height
; }
212 int GetWidth() const { return m_width
; }
214 void SetHeight(int h
) { m_height
= h
; }
215 void SetWidth(int w
) { m_width
= w
; }
217 wxGenericTreeItem
*GetParent() const { return m_parent
; }
220 // deletes all children notifying the treectrl about it if !NULL
222 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
224 // get count of all children (and grand children if 'recursively')
225 size_t GetChildrenCount(bool recursively
= TRUE
) const;
227 void Insert(wxGenericTreeItem
*child
, size_t index
)
228 { m_children
.Insert(child
, index
); }
230 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
232 // return the item at given position (or NULL if no item), onButton is
233 // TRUE if the point belongs to the item's button, otherwise it lies
234 // on the button's label
235 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
236 const wxGenericTreeCtrl
*,
240 void Expand() { m_isCollapsed
= FALSE
; }
241 void Collapse() { m_isCollapsed
= TRUE
; }
243 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
246 bool HasChildren() const { return !m_children
.IsEmpty(); }
247 bool IsSelected() const { return m_hasHilight
!= 0; }
248 bool IsExpanded() const { return !m_isCollapsed
; }
249 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
250 bool IsBold() const { return m_isBold
!= 0; }
253 // get them - may be NULL
254 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
255 // get them ensuring that the pointer is not NULL
256 wxTreeItemAttr
& Attr()
260 m_attr
= new wxTreeItemAttr
;
266 void SetAttributes(wxTreeItemAttr
*attr
)
268 if ( m_ownsAttr
) delete m_attr
;
272 // set them and delete when done
273 void AssignAttributes(wxTreeItemAttr
*attr
)
280 // since there can be very many of these, we save size by chosing
281 // the smallest representation for the elements and by ordering
282 // the members to avoid padding.
283 wxString m_text
; // label to be rendered for item
285 wxTreeItemData
*m_data
; // user-provided data
287 wxArrayGenericTreeItems m_children
; // list of children
288 wxGenericTreeItem
*m_parent
; // parent of this item
290 wxTreeItemAttr
*m_attr
; // attributes???
292 // tree ctrl images for the normal, selected, expanded and
293 // expanded+selected states
294 short m_images
[wxTreeItemIcon_Max
];
296 wxCoord m_x
; // (virtual) offset from top
297 short m_y
; // (virtual) offset from left
298 short m_width
; // width of this item
299 unsigned char m_height
; // height of this item
301 // use bitfields to save size
302 int m_isCollapsed
:1;
303 int m_hasHilight
:1; // same as focused
304 int m_hasPlus
:1; // used for item which doesn't have
305 // children but has a [+] button
306 int m_isBold
:1; // render the label in bold font
307 int m_ownsAttr
:1; // delete attribute when done
310 // =============================================================================
312 // =============================================================================
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 // translate the key or mouse event flags to the type of selection we're
320 static void EventFlagsToSelType(long style
,
324 bool &extended_select
,
325 bool &unselect_others
)
327 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
328 extended_select
= shiftDown
&& is_multiple
;
329 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
332 // -----------------------------------------------------------------------------
333 // wxTreeRenameTimer (internal)
334 // -----------------------------------------------------------------------------
336 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
341 void wxTreeRenameTimer::Notify()
343 m_owner
->OnRenameTimer();
346 //-----------------------------------------------------------------------------
347 // wxTreeTextCtrl (internal)
348 //-----------------------------------------------------------------------------
350 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
351 EVT_CHAR (wxTreeTextCtrl::OnChar
)
352 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
353 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
356 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
360 wxGenericTreeCtrl
*owner
,
361 const wxString
&value
,
365 const wxValidator
& validator
,
366 const wxString
&name
)
367 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
373 (*m_res
) = wxEmptyString
;
374 m_startValue
= value
;
378 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
380 if (event
.m_keyCode
== WXK_RETURN
)
383 (*m_res
) = GetValue();
385 if ((*m_res
) != m_startValue
)
386 m_owner
->OnRenameAccept();
388 if (!wxPendingDelete
.Member(this))
389 wxPendingDelete
.Append(this);
392 m_owner
->SetFocus(); // This doesn't work. TODO.
396 if (event
.m_keyCode
== WXK_ESCAPE
)
401 if (!wxPendingDelete
.Member(this))
402 wxPendingDelete
.Append(this);
405 m_owner
->SetFocus(); // This doesn't work. TODO.
412 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
420 // auto-grow the textctrl:
421 wxSize parentSize
= m_owner
->GetSize();
422 wxPoint myPos
= GetPosition();
423 wxSize mySize
= GetSize();
425 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
426 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
427 if (mySize
.x
> sx
) sx
= mySize
.x
;
433 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
441 if (!wxPendingDelete
.Member(this))
442 wxPendingDelete
.Append(this);
445 (*m_res
) = GetValue();
447 if ((*m_res
) != m_startValue
)
448 m_owner
->OnRenameAccept();
451 // -----------------------------------------------------------------------------
453 // -----------------------------------------------------------------------------
455 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
456 const wxString
& text
,
457 int image
, int selImage
,
458 wxTreeItemData
*data
)
461 m_images
[wxTreeItemIcon_Normal
] = image
;
462 m_images
[wxTreeItemIcon_Selected
] = selImage
;
463 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
464 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
469 m_isCollapsed
= TRUE
;
470 m_hasHilight
= FALSE
;
476 m_attr
= (wxTreeItemAttr
*)NULL
;
479 // We don't know the height here yet.
484 wxGenericTreeItem::~wxGenericTreeItem()
488 if (m_ownsAttr
) delete m_attr
;
490 wxASSERT_MSG( m_children
.IsEmpty(),
491 wxT("please call DeleteChildren() before deleting the item") );
494 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
496 size_t count
= m_children
.Count();
497 for ( size_t n
= 0; n
< count
; n
++ )
499 wxGenericTreeItem
*child
= m_children
[n
];
501 tree
->SendDeleteEvent(child
);
503 child
->DeleteChildren(tree
);
510 void wxGenericTreeItem::SetText( const wxString
&text
)
515 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
517 size_t count
= m_children
.Count();
521 size_t total
= count
;
522 for (size_t n
= 0; n
< count
; ++n
)
524 total
+= m_children
[n
]->GetChildrenCount();
530 void wxGenericTreeItem::GetSize( int &x
, int &y
,
531 const wxGenericTreeCtrl
*theButton
)
533 int bottomY
=m_y
+theButton
->GetLineHeight(this);
534 if ( y
< bottomY
) y
= bottomY
;
535 int width
= m_x
+ m_width
;
536 if ( x
< width
) x
= width
;
540 size_t count
= m_children
.Count();
541 for ( size_t n
= 0; n
< count
; ++n
)
543 m_children
[n
]->GetSize( x
, y
, theButton
);
548 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
549 const wxGenericTreeCtrl
*theCtrl
,
553 // for a hidden root node, don't evaluate it, but do evaluate children
554 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
557 int h
= theCtrl
->GetLineHeight(this);
558 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
560 int y_mid
= m_y
+ h
/2;
561 if (point
.y
< y_mid
)
562 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
564 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
566 // 5 is the size of the plus sign
567 int xCross
= m_x
- theCtrl
->GetSpacing();
568 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
569 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
570 HasPlus() && theCtrl
->HasButtons() )
572 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
576 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
581 // assuming every image (normal and selected) has the same size!
582 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
583 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
586 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
587 flags
|= wxTREE_HITTEST_ONITEMICON
;
589 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
595 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
596 if (point
.x
> m_x
+m_width
)
597 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
602 // if children are expanded, fall through to evaluate them
603 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
607 size_t count
= m_children
.Count();
608 for ( size_t n
= 0; n
< count
; n
++ )
610 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
618 return (wxGenericTreeItem
*) NULL
;
621 int wxGenericTreeItem::GetCurrentImage() const
623 int image
= NO_IMAGE
;
628 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
631 if ( image
== NO_IMAGE
)
633 // we usually fall back to the normal item, but try just the
634 // expanded one (and not selected) first in this case
635 image
= GetImage(wxTreeItemIcon_Expanded
);
641 image
= GetImage(wxTreeItemIcon_Selected
);
644 // maybe it doesn't have the specific image we want,
645 // try the default one instead
646 if ( image
== NO_IMAGE
) image
= GetImage();
651 // -----------------------------------------------------------------------------
652 // wxGenericTreeCtrl implementation
653 // -----------------------------------------------------------------------------
655 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
657 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
658 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
659 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
660 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
661 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
662 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
663 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
666 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
668 * wxTreeCtrl has to be a real class or we have problems with
669 * the run-time information.
672 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
675 // -----------------------------------------------------------------------------
676 // construction/destruction
677 // -----------------------------------------------------------------------------
679 void wxGenericTreeCtrl::Init()
681 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
689 m_hilightBrush
= new wxBrush
691 wxSystemSettings::GetColour
693 wxSYS_COLOUR_HIGHLIGHT
698 m_hilightUnfocusedBrush
= new wxBrush
700 wxSystemSettings::GetColour
702 wxSYS_COLOUR_BTNSHADOW
707 m_imageListNormal
= m_imageListButtons
=
708 m_imageListState
= (wxImageList
*) NULL
;
709 m_ownsImageListNormal
= m_ownsImageListButtons
=
710 m_ownsImageListState
= FALSE
;
713 m_isDragging
= FALSE
;
714 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
716 m_renameTimer
= NULL
;
719 m_lastOnSame
= FALSE
;
721 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
722 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
723 m_normalFont
.GetFamily(),
724 m_normalFont
.GetStyle(),
726 m_normalFont
.GetUnderlined());
729 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
734 const wxValidator
&validator
,
735 const wxString
& name
)
739 wxGetOsVersion( &major
, &minor
);
741 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
742 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
743 style
&= ~wxTR_LINES_AT_ROOT
;
744 style
|= wxTR_NO_LINES
;
746 style
|= wxTR_ROW_LINES
;
748 style
|= wxTR_AQUA_BUTTONS
;
751 if (style
& wxTR_AQUA_BUTTONS
)
753 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
754 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
762 wxScrolledWindow::Create( parent
, id
, pos
, size
,
763 style
|wxHSCROLL
|wxVSCROLL
, name
);
765 // If the tree display has no buttons, but does have
766 // connecting lines, we can use a narrower layout.
767 // It may not be a good idea to force this...
768 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
775 SetValidator( validator
);
778 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
779 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
781 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
782 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
787 wxGenericTreeCtrl::~wxGenericTreeCtrl()
789 delete m_hilightBrush
;
790 delete m_hilightUnfocusedBrush
;
797 delete m_renameTimer
;
800 if (m_ownsImageListNormal
)
801 delete m_imageListNormal
;
802 if (m_ownsImageListState
)
803 delete m_imageListState
;
804 if (m_ownsImageListButtons
)
805 delete m_imageListButtons
;
808 // -----------------------------------------------------------------------------
810 // -----------------------------------------------------------------------------
812 size_t wxGenericTreeCtrl::GetCount() const
814 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
817 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
819 m_indent
= (unsigned short) indent
;
823 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
825 m_spacing
= (unsigned short) spacing
;
829 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
831 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
833 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
836 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
838 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
840 // if we will hide the root, make sure children are visible
841 m_anchor
->SetHasPlus();
843 CalculatePositions();
846 // right now, just sets the styles. Eventually, we may
847 // want to update the inherited styles, but right now
848 // none of the parents has updatable styles
849 m_windowStyle
= styles
;
853 // -----------------------------------------------------------------------------
854 // functions to work with tree items
855 // -----------------------------------------------------------------------------
857 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
859 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
861 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
864 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
865 wxTreeItemIcon which
) const
867 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
869 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
872 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
874 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
876 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
879 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
881 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
884 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
885 pItem
->SetText(text
);
886 CalculateSize(pItem
, dc
);
890 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
892 wxTreeItemIcon which
)
894 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
896 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
897 pItem
->SetImage(image
, which
);
900 CalculateSize(pItem
, dc
);
904 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
906 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
908 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
911 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
913 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
915 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
916 pItem
->SetHasPlus(has
);
920 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
922 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
924 // avoid redrawing the tree if no real change
925 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
926 if ( pItem
->IsBold() != bold
)
928 pItem
->SetBold(bold
);
933 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
936 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
938 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
939 pItem
->Attr().SetTextColour(col
);
943 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
946 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
948 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
949 pItem
->Attr().SetBackgroundColour(col
);
953 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
955 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
957 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
958 pItem
->Attr().SetFont(font
);
962 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
964 wxScrolledWindow::SetFont(font
);
966 m_normalFont
= font
;
967 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
968 m_normalFont
.GetFamily(),
969 m_normalFont
.GetStyle(),
971 m_normalFont
.GetUnderlined());
977 // -----------------------------------------------------------------------------
978 // item status inquiries
979 // -----------------------------------------------------------------------------
981 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
983 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
985 // An item is only visible if it's not a descendant of a collapsed item
986 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
987 wxGenericTreeItem
* parent
= pItem
->GetParent();
990 if (!parent
->IsExpanded())
992 parent
= parent
->GetParent();
996 GetViewStart(& startX
, & startY
);
998 wxSize clientSize
= GetClientSize();
1001 if (!GetBoundingRect(item
, rect
))
1003 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1005 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1007 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1013 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1015 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1017 // consider that the item does have children if it has the "+" button: it
1018 // might not have them (if it had never been expanded yet) but then it
1019 // could have them as well and it's better to err on this side rather than
1020 // disabling some operations which are restricted to the items with
1021 // children for an item which does have them
1022 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1025 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1027 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1029 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1032 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1034 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1036 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1039 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1041 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1043 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1046 // -----------------------------------------------------------------------------
1048 // -----------------------------------------------------------------------------
1050 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1052 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1054 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1057 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1059 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1062 return GetNextChild(item
, cookie
);
1065 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1067 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1069 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1070 if ( (size_t)cookie
< children
.Count() )
1072 return children
.Item((size_t)cookie
++);
1076 // there are no more of them
1077 return wxTreeItemId();
1081 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1083 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1085 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1086 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1089 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1091 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1093 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1094 wxGenericTreeItem
*parent
= i
->GetParent();
1095 if ( parent
== NULL
)
1097 // root item doesn't have any siblings
1098 return wxTreeItemId();
1101 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1102 int index
= siblings
.Index(i
);
1103 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1105 size_t n
= (size_t)(index
+ 1);
1106 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1109 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1111 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1113 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1114 wxGenericTreeItem
*parent
= i
->GetParent();
1115 if ( parent
== NULL
)
1117 // root item doesn't have any siblings
1118 return wxTreeItemId();
1121 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1122 int index
= siblings
.Index(i
);
1123 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1125 return index
== 0 ? wxTreeItemId()
1126 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1129 // Only for internal use right now, but should probably be public
1130 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1132 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1134 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1136 // First see if there are any children.
1137 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1138 if (children
.GetCount() > 0)
1140 return children
.Item(0);
1144 // Try a sibling of this or ancestor instead
1145 wxTreeItemId p
= item
;
1146 wxTreeItemId toFind
;
1149 toFind
= GetNextSibling(p
);
1151 } while (p
.IsOk() && !toFind
.IsOk());
1156 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1158 wxTreeItemId id
= GetRootItem();
1167 } while (id
.IsOk());
1169 return wxTreeItemId();
1172 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1174 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1176 wxTreeItemId id
= item
;
1179 while (id
= GetNext(id
), id
.IsOk())
1185 return wxTreeItemId();
1188 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1190 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1192 wxFAIL_MSG(wxT("not implemented"));
1194 return wxTreeItemId();
1197 // find the first item starting with the given prefix after the given item
1198 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1199 const wxString
& prefixOrig
) const
1201 // match is case insensitive as this is more convenient to the user: having
1202 // to press Shift-letter to go to the item starting with a capital letter
1203 // would be too bothersome
1204 wxString prefix
= prefixOrig
.Lower();
1206 // determine the starting point: we shouldn't take the current item (this
1207 // allows to switch between two items starting with the same letter just by
1208 // pressing it) but we shouldn't jump to the next one if the user is
1209 // continuing to type as otherwise he might easily skip the item he wanted
1210 wxTreeItemId id
= idParent
;
1211 if ( prefix
.length() == 1 )
1216 // look for the item starting with the given prefix after it
1217 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1222 // if we haven't found anything...
1225 // ... wrap to the beginning
1227 if ( HasFlag(wxTR_HIDE_ROOT
) )
1229 // can't select virtual root
1233 // and try all the items (stop when we get to the one we started from)
1234 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1243 // -----------------------------------------------------------------------------
1245 // -----------------------------------------------------------------------------
1247 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1249 const wxString
& text
,
1250 int image
, int selImage
,
1251 wxTreeItemData
*data
)
1253 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1256 // should we give a warning here?
1257 return AddRoot(text
, image
, selImage
, data
);
1260 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1262 wxGenericTreeItem
*item
=
1263 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1267 data
->m_pItem
= (long) item
;
1270 parent
->Insert( item
, previous
);
1275 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1276 int image
, int selImage
,
1277 wxTreeItemData
*data
)
1279 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1281 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1283 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1284 image
, selImage
, data
);
1287 data
->m_pItem
= (long) m_anchor
;
1290 if (HasFlag(wxTR_HIDE_ROOT
))
1292 // if root is hidden, make sure we can navigate
1294 m_anchor
->SetHasPlus();
1296 CalculatePositions();
1299 if (!HasFlag(wxTR_MULTIPLE
))
1301 m_current
= m_key_current
= m_anchor
;
1302 m_current
->SetHilight( TRUE
);
1308 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1309 const wxString
& text
,
1310 int image
, int selImage
,
1311 wxTreeItemData
*data
)
1313 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1316 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1317 const wxTreeItemId
& idPrevious
,
1318 const wxString
& text
,
1319 int image
, int selImage
,
1320 wxTreeItemData
*data
)
1322 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1325 // should we give a warning here?
1326 return AddRoot(text
, image
, selImage
, data
);
1330 if (idPrevious
.IsOk())
1332 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1333 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1334 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1337 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1340 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1342 const wxString
& text
,
1343 int image
, int selImage
,
1344 wxTreeItemData
*data
)
1346 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1349 // should we give a warning here?
1350 return AddRoot(text
, image
, selImage
, data
);
1353 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1356 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1357 const wxString
& text
,
1358 int image
, int selImage
,
1359 wxTreeItemData
*data
)
1361 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1364 // should we give a warning here?
1365 return AddRoot(text
, image
, selImage
, data
);
1368 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1369 image
, selImage
, data
);
1372 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1374 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1375 event
.m_item
= (long) item
;
1376 event
.SetEventObject( this );
1377 ProcessEvent( event
);
1380 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1382 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1384 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1385 item
->DeleteChildren(this);
1388 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1390 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1392 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1394 // don't stay with invalid m_key_current or we will crash in
1395 // the next call to OnChar()
1396 bool changeKeyCurrent
= FALSE
;
1397 wxGenericTreeItem
*itemKey
= m_key_current
;
1400 if ( itemKey
== item
)
1402 // m_key_current is a descendant of the item being deleted
1403 changeKeyCurrent
= TRUE
;
1406 itemKey
= itemKey
->GetParent();
1409 wxGenericTreeItem
*parent
= item
->GetParent();
1412 parent
->GetChildren().Remove( item
); // remove by value
1415 if ( changeKeyCurrent
)
1417 // may be NULL or not
1418 m_key_current
= parent
;
1421 item
->DeleteChildren(this);
1422 SendDeleteEvent(item
);
1426 void wxGenericTreeCtrl::DeleteAllItems()
1432 m_anchor
->DeleteChildren(this);
1439 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1441 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1443 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1444 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1445 _T("can't expand hidden root") );
1447 if ( !item
->HasPlus() )
1450 if ( item
->IsExpanded() )
1453 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1454 event
.m_item
= (long) item
;
1455 event
.SetEventObject( this );
1457 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1459 // cancelled by program
1464 CalculatePositions();
1466 RefreshSubtree(item
);
1468 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1469 ProcessEvent( event
);
1472 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1475 if ( IsExpanded(item
) )
1478 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1479 while ( child
.IsOk() )
1483 child
= GetNextChild(item
, cookie
);
1488 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1490 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1491 _T("can't collapse hidden root") );
1493 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1495 if ( !item
->IsExpanded() )
1498 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1499 event
.m_item
= (long) item
;
1500 event
.SetEventObject( this );
1501 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1503 // cancelled by program
1509 #if 0 // TODO why should items be collapsed recursively?
1510 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1511 size_t count
= children
.Count();
1512 for ( size_t n
= 0; n
< count
; n
++ )
1514 Collapse(children
[n
]);
1518 CalculatePositions();
1520 RefreshSubtree(item
);
1522 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1523 ProcessEvent( event
);
1526 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1529 DeleteChildren(item
);
1532 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1534 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1536 if (item
->IsExpanded())
1542 void wxGenericTreeCtrl::Unselect()
1546 m_current
->SetHilight( FALSE
);
1547 RefreshLine( m_current
);
1551 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1553 if (item
->IsSelected())
1555 item
->SetHilight(FALSE
);
1559 if (item
->HasChildren())
1561 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1562 size_t count
= children
.Count();
1563 for ( size_t n
= 0; n
< count
; ++n
)
1565 UnselectAllChildren(children
[n
]);
1570 void wxGenericTreeCtrl::UnselectAll()
1572 wxTreeItemId rootItem
= GetRootItem();
1574 // the tree might not have the root item at all
1577 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1581 // Recursive function !
1582 // To stop we must have crt_item<last_item
1584 // Tag all next children, when no more children,
1585 // Move to parent (not to tag)
1586 // Keep going... if we found last_item, we stop.
1587 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1589 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1591 if (parent
== NULL
) // This is root item
1592 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1594 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1595 int index
= children
.Index(crt_item
);
1596 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1598 size_t count
= children
.Count();
1599 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1601 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1604 return TagNextChildren(parent
, last_item
, select
);
1607 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1609 crt_item
->SetHilight(select
);
1610 RefreshLine(crt_item
);
1612 if (crt_item
==last_item
)
1615 if (crt_item
->HasChildren())
1617 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1618 size_t count
= children
.Count();
1619 for ( size_t n
= 0; n
< count
; ++n
)
1621 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1629 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1631 // item2 is not necessary after item1
1632 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1634 // choice first' and 'last' between item1 and item2
1635 if (item1
->GetY()<item2
->GetY())
1646 bool select
= m_current
->IsSelected();
1648 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1651 TagNextChildren(first
,last
,select
);
1654 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1655 bool unselect_others
,
1656 bool extended_select
)
1658 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1660 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1661 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1663 //wxCHECK_RET( ( (!unselect_others) && is_single),
1664 // wxT("this is a single selection tree") );
1666 // to keep going anyhow !!!
1669 if (item
->IsSelected())
1670 return; // nothing to do
1671 unselect_others
= TRUE
;
1672 extended_select
= FALSE
;
1674 else if ( unselect_others
&& item
->IsSelected() )
1676 // selection change if there is more than one item currently selected
1677 wxArrayTreeItemIds selected_items
;
1678 if ( GetSelections(selected_items
) == 1 )
1682 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1683 event
.m_item
= (long) item
;
1684 event
.m_itemOld
= (long) m_current
;
1685 event
.SetEventObject( this );
1686 // TODO : Here we don't send any selection mode yet !
1688 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1691 wxTreeItemId parent
= GetParent( itemId
);
1692 while (parent
.IsOk())
1694 if (!IsExpanded(parent
))
1697 parent
= GetParent( parent
);
1700 EnsureVisible( itemId
);
1703 if (unselect_others
)
1705 if (is_single
) Unselect(); // to speed up thing
1710 if (extended_select
)
1714 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1717 // don't change the mark (m_current)
1718 SelectItemRange(m_current
, item
);
1722 bool select
=TRUE
; // the default
1724 // Check if we need to toggle hilight (ctrl mode)
1725 if (!unselect_others
)
1726 select
=!item
->IsSelected();
1728 m_current
= m_key_current
= item
;
1729 m_current
->SetHilight(select
);
1730 RefreshLine( m_current
);
1733 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1734 GetEventHandler()->ProcessEvent( event
);
1737 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1738 wxArrayTreeItemIds
&array
) const
1740 if ( item
->IsSelected() )
1741 array
.Add(wxTreeItemId(item
));
1743 if ( item
->HasChildren() )
1745 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1746 size_t count
= children
.GetCount();
1747 for ( size_t n
= 0; n
< count
; ++n
)
1748 FillArray(children
[n
], array
);
1752 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1755 wxTreeItemId idRoot
= GetRootItem();
1756 if ( idRoot
.IsOk() )
1758 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1760 //else: the tree is empty, so no selections
1762 return array
.Count();
1765 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1767 if (!item
.IsOk()) return;
1769 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1771 // first expand all parent branches
1772 wxGenericTreeItem
*parent
= gitem
->GetParent();
1774 if ( HasFlag(wxTR_HIDE_ROOT
) )
1776 while ( parent
!= m_anchor
)
1779 parent
= parent
->GetParent();
1787 parent
= parent
->GetParent();
1791 //if (parent) CalculatePositions();
1796 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1798 if (!item
.IsOk()) return;
1800 // We have to call this here because the label in
1801 // question might just have been added and no screen
1802 // update taken place.
1803 if (m_dirty
) wxYieldIfNeeded();
1805 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1807 // now scroll to the item
1808 int item_y
= gitem
->GetY();
1812 GetViewStart( &start_x
, &start_y
);
1813 start_y
*= PIXELS_PER_UNIT
;
1817 GetClientSize( &client_w
, &client_h
);
1819 if (item_y
< start_y
+3)
1824 m_anchor
->GetSize( x
, y
, this );
1825 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1826 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1827 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1828 // Item should appear at top
1829 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1831 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1836 m_anchor
->GetSize( x
, y
, this );
1837 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1838 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1839 item_y
+= PIXELS_PER_UNIT
+2;
1840 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1841 // Item should appear at bottom
1842 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
);
1846 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1847 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1849 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1850 wxGenericTreeItem
**item2
)
1852 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1854 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1857 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1858 const wxTreeItemId
& item2
)
1860 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1863 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1865 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1867 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1869 wxCHECK_RET( !s_treeBeingSorted
,
1870 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1872 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1873 if ( children
.Count() > 1 )
1877 s_treeBeingSorted
= this;
1878 children
.Sort(tree_ctrl_compare_func
);
1879 s_treeBeingSorted
= NULL
;
1881 //else: don't make the tree dirty as nothing changed
1884 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1886 return m_imageListNormal
;
1889 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1891 return m_imageListButtons
;
1894 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1896 return m_imageListState
;
1899 void wxGenericTreeCtrl::CalculateLineHeight()
1901 wxClientDC
dc(this);
1902 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1904 if ( m_imageListNormal
)
1906 // Calculate a m_lineHeight value from the normal Image sizes.
1907 // May be toggle off. Then wxGenericTreeCtrl will spread when
1908 // necessary (which might look ugly).
1909 int n
= m_imageListNormal
->GetImageCount();
1910 for (int i
= 0; i
< n
; i
++)
1912 int width
= 0, height
= 0;
1913 m_imageListNormal
->GetSize(i
, width
, height
);
1914 if (height
> m_lineHeight
) m_lineHeight
= height
;
1918 if (m_imageListButtons
)
1920 // Calculate a m_lineHeight value from the Button image sizes.
1921 // May be toggle off. Then wxGenericTreeCtrl will spread when
1922 // necessary (which might look ugly).
1923 int n
= m_imageListButtons
->GetImageCount();
1924 for (int i
= 0; i
< n
; i
++)
1926 int width
= 0, height
= 0;
1927 m_imageListButtons
->GetSize(i
, width
, height
);
1928 if (height
> m_lineHeight
) m_lineHeight
= height
;
1932 if (m_lineHeight
< 30)
1933 m_lineHeight
+= 2; // at least 2 pixels
1935 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1938 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1940 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1941 m_imageListNormal
= imageList
;
1942 m_ownsImageListNormal
= FALSE
;
1944 // Don't do any drawing if we're setting the list to NULL,
1945 // since we may be in the process of deleting the tree control.
1947 CalculateLineHeight();
1950 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1952 if (m_ownsImageListState
) delete m_imageListState
;
1953 m_imageListState
= imageList
;
1954 m_ownsImageListState
= FALSE
;
1957 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1959 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1960 m_imageListButtons
= imageList
;
1961 m_ownsImageListButtons
= FALSE
;
1963 CalculateLineHeight();
1966 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1968 SetImageList(imageList
);
1969 m_ownsImageListNormal
= TRUE
;
1972 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1974 SetStateImageList(imageList
);
1975 m_ownsImageListState
= TRUE
;
1978 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1980 SetButtonsImageList(imageList
);
1981 m_ownsImageListButtons
= TRUE
;
1984 // -----------------------------------------------------------------------------
1986 // -----------------------------------------------------------------------------
1988 void wxGenericTreeCtrl::AdjustMyScrollbars()
1993 m_anchor
->GetSize( x
, y
, this );
1994 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1995 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1996 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1997 int y_pos
= GetScrollPos( wxVERTICAL
);
1998 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2002 SetScrollbars( 0, 0, 0, 0 );
2006 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2008 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2009 return item
->GetHeight();
2011 return m_lineHeight
;
2014 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2016 // TODO implement "state" icon on items
2018 wxTreeItemAttr
*attr
= item
->GetAttributes();
2019 if ( attr
&& attr
->HasFont() )
2020 dc
.SetFont(attr
->GetFont());
2021 else if (item
->IsBold())
2022 dc
.SetFont(m_boldFont
);
2024 long text_w
= 0, text_h
= 0;
2025 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2027 int image_h
= 0, image_w
= 0;
2028 int image
= item
->GetCurrentImage();
2029 if ( image
!= NO_IMAGE
)
2031 if ( m_imageListNormal
)
2033 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2042 int total_h
= GetLineHeight(item
);
2044 if ( item
->IsSelected() )
2046 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2051 if ( attr
&& attr
->HasBackgroundColour() )
2052 colBg
= attr
->GetBackgroundColour();
2054 colBg
= m_backgroundColour
;
2055 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2058 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2060 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2064 DoGetPosition(&x
, &y
);
2066 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2070 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2072 // If it's selected, and there's an image, then we should
2073 // take care to leave the area under the image painted in the
2074 // background colour.
2075 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2076 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2080 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2081 item
->GetWidth()+2, total_h
-offset
);
2085 if ( image
!= NO_IMAGE
)
2087 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2088 m_imageListNormal
->Draw( image
, dc
,
2090 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2091 wxIMAGELIST_DRAW_TRANSPARENT
);
2092 dc
.DestroyClippingRegion();
2095 dc
.SetBackgroundMode(wxTRANSPARENT
);
2096 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2097 dc
.DrawText( item
->GetText(),
2098 (wxCoord
)(image_w
+ item
->GetX()),
2099 (wxCoord
)(item
->GetY() + extraH
));
2101 // restore normal font
2102 dc
.SetFont( m_normalFont
);
2105 // Now y stands for the top of the item, whereas it used to stand for middle !
2106 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2108 int x
= level
*m_indent
;
2109 if (!HasFlag(wxTR_HIDE_ROOT
))
2113 else if (level
== 0)
2115 // always expand hidden root
2117 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2118 int count
= children
.Count();
2124 PaintLevel(children
[n
], dc
, 1, y
);
2125 } while (++n
< count
);
2127 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2129 // draw line down to last child
2130 origY
+= GetLineHeight(children
[0])>>1;
2131 oldY
+= GetLineHeight(children
[n
-1])>>1;
2132 dc
.DrawLine(3, origY
, 3, oldY
);
2138 item
->SetX(x
+m_spacing
);
2141 int h
= GetLineHeight(item
);
2143 int y_mid
= y_top
+ (h
>>1);
2146 int exposed_x
= dc
.LogicalToDeviceX(0);
2147 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2149 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2153 // don't draw rect outline if we already have the
2154 // background color under Mac
2155 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2156 #endif // !__WXMAC__
2160 if ( item
->IsSelected() )
2162 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2166 wxTreeItemAttr
*attr
= item
->GetAttributes();
2167 if (attr
&& attr
->HasTextColour())
2168 colText
= attr
->GetTextColour();
2170 colText
= GetForegroundColour();
2174 dc
.SetTextForeground(colText
);
2178 PaintItem(item
, dc
);
2180 if (HasFlag(wxTR_ROW_LINES
))
2182 // if the background colour is white, choose a
2183 // contrasting color for the lines
2184 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2185 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2186 dc
.DrawLine(0, y_top
, 10000, y_top
);
2187 dc
.DrawLine(0, y
, 10000, y
);
2190 // restore DC objects
2191 dc
.SetBrush(*wxWHITE_BRUSH
);
2192 dc
.SetPen(m_dottedPen
);
2193 dc
.SetTextForeground(*wxBLACK
);
2195 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2197 if (!HasFlag(wxTR_NO_LINES
))
2199 if (x
> (signed)m_indent
)
2200 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2201 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2202 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2203 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2206 if (m_imageListButtons
!= NULL
)
2208 // draw the image button here
2209 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2210 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2211 if (item
->IsSelected())
2212 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2213 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2214 int xx
= x
- (image_w
>>1);
2215 int yy
= y_mid
- (image_h
>>1);
2216 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2217 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2218 wxIMAGELIST_DRAW_TRANSPARENT
);
2219 dc
.DestroyClippingRegion();
2221 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2223 // draw the twisty button here
2225 if (HasFlag(wxTR_AQUA_BUTTONS
))
2227 if (item
->IsExpanded())
2228 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2230 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2234 dc
.SetBrush(*m_hilightBrush
);
2235 dc
.SetPen(*wxBLACK_PEN
);
2238 if (item
->IsExpanded())
2241 button
[0].y
= y_mid
-2;
2243 button
[1].y
= y_mid
-2;
2245 button
[2].y
= y_mid
+3;
2249 button
[0].y
= y_mid
-5;
2251 button
[1].y
= y_mid
+5;
2253 button
[2].y
= y_mid
;
2256 dc
.DrawPolygon(3, button
);
2257 dc
.SetPen(m_dottedPen
);
2260 else // if (HasFlag(wxTR_HAS_BUTTONS))
2262 // draw the plus sign here
2263 dc
.SetPen(*wxGREY_PEN
);
2264 dc
.SetBrush(*wxWHITE_BRUSH
);
2265 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2266 dc
.SetPen(*wxBLACK_PEN
);
2267 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2268 if (!item
->IsExpanded())
2269 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2270 dc
.SetPen(m_dottedPen
);
2273 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2275 // draw the horizontal line here
2277 if (x
> (signed)m_indent
)
2278 x_start
-= m_indent
;
2279 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2281 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2285 if (item
->IsExpanded())
2287 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2288 int count
= children
.Count();
2295 PaintLevel(children
[n
], dc
, level
, y
);
2296 } while (++n
< count
);
2298 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2300 // draw line down to last child
2301 oldY
+= GetLineHeight(children
[n
-1])>>1;
2302 if (HasButtons()) y_mid
+= 5;
2303 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2309 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2313 if ( item
->HasPlus() )
2315 // it's a folder, indicate it by a border
2320 // draw a line under the drop target because the item will be
2322 DrawLine(item
, TRUE
/* below */);
2325 SetCursor(wxCURSOR_BULLSEYE
);
2330 SetCursor(wxCURSOR_NO_ENTRY
);
2334 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2336 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2338 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2340 wxClientDC
dc(this);
2342 dc
.SetLogicalFunction(wxINVERT
);
2343 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2345 int w
= i
->GetWidth() + 2;
2346 int h
= GetLineHeight(i
) + 2;
2348 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2351 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2353 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2355 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2357 wxClientDC
dc(this);
2359 dc
.SetLogicalFunction(wxINVERT
);
2365 y
+= GetLineHeight(i
) - 1;
2368 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2371 // -----------------------------------------------------------------------------
2372 // wxWindows callbacks
2373 // -----------------------------------------------------------------------------
2375 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2383 dc
.SetFont( m_normalFont
);
2384 dc
.SetPen( m_dottedPen
);
2386 // this is now done dynamically
2387 //if(GetImageList() == NULL)
2388 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2391 PaintLevel( m_anchor
, dc
, 0, y
);
2394 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2403 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2412 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2414 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2415 te
.m_evtKey
= event
;
2416 te
.SetEventObject( this );
2417 if ( GetEventHandler()->ProcessEvent( te
) )
2419 // intercepted by the user code
2423 if ( (m_current
== 0) || (m_key_current
== 0) )
2429 // how should the selection work for this event?
2430 bool is_multiple
, extended_select
, unselect_others
;
2431 EventFlagsToSelType(GetWindowStyleFlag(),
2433 event
.ControlDown(),
2434 is_multiple
, extended_select
, unselect_others
);
2438 // * : Expand all/Collapse all
2439 // ' ' | return : activate
2440 // up : go up (not last children!)
2442 // left : go to parent
2443 // right : open if parent and go next
2444 // home : go to root
2445 // end : go to last item without opening parents
2446 // alnum : start or continue searching for the item with this prefix
2447 int keyCode
= event
.KeyCode();
2452 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2460 if ( !IsExpanded(m_current
) )
2463 ExpandAll(m_current
);
2466 //else: fall through to Collapse() it
2470 if (IsExpanded(m_current
))
2472 Collapse(m_current
);
2479 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2480 event
.m_item
= (long) m_current
;
2481 event
.SetEventObject( this );
2482 GetEventHandler()->ProcessEvent( event
);
2486 // up goes to the previous sibling or to the last
2487 // of its children if it's expanded
2490 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2493 prev
= GetParent( m_key_current
);
2494 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2496 break; // don't go to root if it is hidden
2501 wxTreeItemId current
= m_key_current
;
2502 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2503 if (current
== GetFirstChild( prev
, cookie
))
2505 // otherwise we return to where we came from
2506 SelectItem( prev
, unselect_others
, extended_select
);
2507 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2514 while ( IsExpanded(prev
) && HasChildren(prev
) )
2516 wxTreeItemId child
= GetLastChild(prev
);
2523 SelectItem( prev
, unselect_others
, extended_select
);
2524 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2529 // left arrow goes to the parent
2532 wxTreeItemId prev
= GetParent( m_current
);
2533 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2535 // don't go to root if it is hidden
2536 prev
= GetPrevSibling( m_current
);
2540 SelectItem( prev
, unselect_others
, extended_select
);
2546 // this works the same as the down arrow except that we
2547 // also expand the item if it wasn't expanded yet
2553 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2556 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2557 SelectItem( child
, unselect_others
, extended_select
);
2558 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2562 wxTreeItemId next
= GetNextSibling( m_key_current
);
2565 wxTreeItemId current
= m_key_current
;
2566 while (current
&& !next
)
2568 current
= GetParent( current
);
2569 if (current
) next
= GetNextSibling( current
);
2574 SelectItem( next
, unselect_others
, extended_select
);
2575 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2581 // <End> selects the last visible tree item
2584 wxTreeItemId last
= GetRootItem();
2586 while ( last
.IsOk() && IsExpanded(last
) )
2588 wxTreeItemId lastChild
= GetLastChild(last
);
2590 // it may happen if the item was expanded but then all of
2591 // its children have been deleted - so IsExpanded() returned
2592 // TRUE, but GetLastChild() returned invalid item
2601 SelectItem( last
, unselect_others
, extended_select
);
2606 // <Home> selects the root item
2609 wxTreeItemId prev
= GetRootItem();
2613 if ( HasFlag(wxTR_HIDE_ROOT
) )
2616 prev
= GetFirstChild(prev
, dummy
);
2621 SelectItem( prev
, unselect_others
, extended_select
);
2626 // do not use wxIsalnum() here
2627 if ( !event
.HasModifiers() &&
2628 ((keyCode
>= '0' && keyCode
<= '9') ||
2629 (keyCode
>= 'a' && keyCode
<= 'z') ||
2630 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2632 // find the next item starting with the given prefix
2633 char ch
= (char)keyCode
;
2635 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2646 // also start the timer to reset the current prefix if the user
2647 // doesn't press any more alnum keys soon -- we wouldn't want
2648 // to use this prefix for a new item search
2651 m_findTimer
= new wxTreeFindTimer(this);
2654 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2663 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2665 // JACS: removed wxYieldIfNeeded() because it can cause the window
2666 // to be deleted from under us if a close window event is pending
2671 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2672 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2673 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2674 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2675 if (flags
) return wxTreeItemId();
2677 if (m_anchor
== NULL
)
2679 flags
= wxTREE_HITTEST_NOWHERE
;
2680 return wxTreeItemId();
2683 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2687 flags
= wxTREE_HITTEST_NOWHERE
;
2688 return wxTreeItemId();
2693 // get the bounding rectangle of the item (or of its label only)
2694 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2696 bool WXUNUSED(textOnly
)) const
2698 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2700 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2703 GetViewStart(& startX
, & startY
);
2705 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2706 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2707 rect
.width
= i
->GetWidth();
2708 //rect.height = i->GetHeight();
2709 rect
.height
= GetLineHeight(i
);
2716 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2718 if (!item
.IsOk()) return;
2720 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2722 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2723 te
.m_item
= (long) m_currentEdit
;
2724 te
.SetEventObject( this );
2725 GetEventHandler()->ProcessEvent( te
);
2727 if (!te
.IsAllowed()) return;
2729 // We have to call this here because the label in
2730 // question might just have been added and no screen
2731 // update taken place.
2732 if (m_dirty
) wxYieldIfNeeded();
2734 wxString s
= m_currentEdit
->GetText();
2735 int w
= m_currentEdit
->GetWidth();
2736 int h
= m_currentEdit
->GetHeight();
2738 CalcScrolledPosition(m_currentEdit
->GetX(), m_currentEdit
->GetY(), &x
, &y
);
2743 int image
= m_currentEdit
->GetCurrentImage();
2744 if ( image
!= NO_IMAGE
)
2746 if ( m_imageListNormal
)
2748 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2753 wxFAIL_MSG(_T("you must create an image list to use images!"));
2757 w
-= image_w
+ 4; // I don't know why +4 is needed
2759 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2769 void wxGenericTreeCtrl::OnRenameTimer()
2774 void wxGenericTreeCtrl::OnRenameAccept()
2776 // TODO if the validator fails this causes a crash
2777 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2778 le
.m_item
= (long) m_currentEdit
;
2779 le
.SetEventObject( this );
2780 le
.m_label
= m_renameRes
;
2781 GetEventHandler()->ProcessEvent( le
);
2783 if (!le
.IsAllowed()) return;
2785 SetItemText( m_currentEdit
, m_renameRes
);
2788 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2790 if ( !m_anchor
) return;
2792 // we process left mouse up event (enables in-place edit), right down
2793 // (pass to the user code), left dbl click (activate item) and
2794 // dragging/moving events for items drag-and-drop
2795 if ( !(event
.LeftDown() ||
2797 event
.RightDown() ||
2798 event
.LeftDClick() ||
2800 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2807 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2810 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2812 if ( event
.Dragging() && !m_isDragging
)
2814 if (m_dragCount
== 0)
2819 if (m_dragCount
!= 3)
2821 // wait until user drags a bit further...
2825 wxEventType command
= event
.RightIsDown()
2826 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2827 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2829 wxTreeEvent
nevent( command
, GetId() );
2830 nevent
.m_item
= (long) m_current
;
2831 nevent
.SetEventObject(this);
2833 // by default the dragging is not supported, the user code must
2834 // explicitly allow the event for it to take place
2837 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2839 // we're going to drag this item
2840 m_isDragging
= TRUE
;
2842 // remember the old cursor because we will change it while
2844 m_oldCursor
= m_cursor
;
2846 // in a single selection control, hide the selection temporarily
2847 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2849 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2851 if ( m_oldSelection
)
2853 m_oldSelection
->SetHilight(FALSE
);
2854 RefreshLine(m_oldSelection
);
2861 else if ( event
.Moving() )
2863 if ( item
!= m_dropTarget
)
2865 // unhighlight the previous drop target
2866 DrawDropEffect(m_dropTarget
);
2868 m_dropTarget
= item
;
2870 // highlight the current drop target if any
2871 DrawDropEffect(m_dropTarget
);
2876 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2878 // erase the highlighting
2879 DrawDropEffect(m_dropTarget
);
2881 if ( m_oldSelection
)
2883 m_oldSelection
->SetHilight(TRUE
);
2884 RefreshLine(m_oldSelection
);
2885 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2888 // generate the drag end event
2889 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2891 event
.m_item
= (long) item
;
2892 event
.m_pointDrag
= pt
;
2893 event
.SetEventObject(this);
2895 (void)GetEventHandler()->ProcessEvent(event
);
2897 m_isDragging
= FALSE
;
2898 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2902 SetCursor(m_oldCursor
);
2908 // here we process only the messages which happen on tree items
2912 if (item
== NULL
) return; /* we hit the blank area */
2914 if ( event
.RightDown() )
2916 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2917 nevent
.m_item
= (long) item
;
2918 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2919 nevent
.SetEventObject(this);
2920 GetEventHandler()->ProcessEvent(nevent
);
2922 else if ( event
.LeftUp() )
2926 if ( (item
== m_current
) &&
2927 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2928 HasFlag(wxTR_EDIT_LABELS
) )
2930 if ( m_renameTimer
)
2932 if ( m_renameTimer
->IsRunning() )
2933 m_renameTimer
->Stop();
2937 m_renameTimer
= new wxTreeRenameTimer( this );
2940 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
2943 m_lastOnSame
= FALSE
;
2946 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2948 if ( event
.LeftDown() )
2950 m_lastOnSame
= item
== m_current
;
2953 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2955 // only toggle the item for a single click, double click on
2956 // the button doesn't do anything (it toggles the item twice)
2957 if ( event
.LeftDown() )
2962 // don't select the item if the button was clicked
2966 // how should the selection work for this event?
2967 bool is_multiple
, extended_select
, unselect_others
;
2968 EventFlagsToSelType(GetWindowStyleFlag(),
2970 event
.ControlDown(),
2971 is_multiple
, extended_select
, unselect_others
);
2973 SelectItem(item
, unselect_others
, extended_select
);
2975 // For some reason, Windows isn't recognizing a left double-click,
2976 // so we need to simulate it here. Allow 200 milliseconds for now.
2977 if ( event
.LeftDClick() )
2979 // double clicking should not start editing the item label
2980 if ( m_renameTimer
)
2981 m_renameTimer
->Stop();
2983 m_lastOnSame
= FALSE
;
2985 // send activate event first
2986 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2987 nevent
.m_item
= (long) item
;
2988 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2989 nevent
.SetEventObject( this );
2990 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2992 // if the user code didn't process the activate event,
2993 // handle it ourselves by toggling the item when it is
2995 if ( item
->HasPlus() )
3005 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
3007 /* after all changes have been done to the tree control,
3008 * we actually redraw the tree when everything is over */
3010 if (!m_dirty
) return;
3014 CalculatePositions();
3016 AdjustMyScrollbars();
3019 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3024 wxTreeItemAttr
*attr
= item
->GetAttributes();
3025 if ( attr
&& attr
->HasFont() )
3026 dc
.SetFont(attr
->GetFont());
3027 else if ( item
->IsBold() )
3028 dc
.SetFont(m_boldFont
);
3030 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3033 // restore normal font
3034 dc
.SetFont( m_normalFont
);
3038 int image
= item
->GetCurrentImage();
3039 if ( image
!= NO_IMAGE
)
3041 if ( m_imageListNormal
)
3043 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3048 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3051 total_h
+= 2; // at least 2 pixels
3053 total_h
+= total_h
/10; // otherwise 10% extra spacing
3055 item
->SetHeight(total_h
);
3056 if (total_h
>m_lineHeight
)
3057 m_lineHeight
=total_h
;
3059 item
->SetWidth(image_w
+text_w
+2);
3062 // -----------------------------------------------------------------------------
3063 // for developper : y is now the top of the level
3064 // not the middle of it !
3065 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3067 int x
= level
*m_indent
;
3068 if (!HasFlag(wxTR_HIDE_ROOT
))
3072 else if (level
== 0)
3074 // a hidden root is not evaluated, but its
3075 // children are always calculated
3079 CalculateSize( item
, dc
);
3082 item
->SetX( x
+m_spacing
);
3084 y
+= GetLineHeight(item
);
3086 if ( !item
->IsExpanded() )
3088 // we don't need to calculate collapsed branches
3093 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3094 size_t n
, count
= children
.Count();
3096 for (n
= 0; n
< count
; ++n
)
3097 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3100 void wxGenericTreeCtrl::CalculatePositions()
3102 if ( !m_anchor
) return;
3104 wxClientDC
dc(this);
3107 dc
.SetFont( m_normalFont
);
3109 dc
.SetPen( m_dottedPen
);
3110 //if(GetImageList() == NULL)
3111 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3114 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3117 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3119 if (m_dirty
) return;
3121 wxSize client
= GetClientSize();
3124 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3125 rect
.width
= client
.x
;
3126 rect
.height
= client
.y
;
3128 Refresh(TRUE
, &rect
);
3130 AdjustMyScrollbars();
3133 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3135 if (m_dirty
) return;
3138 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3139 rect
.width
= GetClientSize().x
;
3140 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3142 Refresh(TRUE
, &rect
);
3145 void wxGenericTreeCtrl::RefreshSelected()
3147 // TODO: this is awfully inefficient, we should keep the list of all
3148 // selected items internally, should be much faster
3150 RefreshSelectedUnder(m_anchor
);
3153 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3155 if ( item
->IsSelected() )
3158 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3159 size_t count
= children
.GetCount();
3160 for ( size_t n
= 0; n
< count
; n
++ )
3162 RefreshSelectedUnder(children
[n
]);
3166 // ----------------------------------------------------------------------------
3167 // changing colours: we need to refresh the tree control
3168 // ----------------------------------------------------------------------------
3170 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3172 if ( !wxWindow::SetBackgroundColour(colour
) )
3180 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3182 if ( !wxWindow::SetForegroundColour(colour
) )
3190 #endif // wxUSE_TREECTRL