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 // right now, just sets the styles. Eventually, we may
839 // want to update the inherited styles, but right now
840 // none of the parents has updatable styles
841 m_windowStyle
= styles
;
845 // -----------------------------------------------------------------------------
846 // functions to work with tree items
847 // -----------------------------------------------------------------------------
849 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
851 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
853 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
856 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
857 wxTreeItemIcon which
) const
859 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
861 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
864 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
866 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
868 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
871 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
873 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
876 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
877 pItem
->SetText(text
);
878 CalculateSize(pItem
, dc
);
882 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
884 wxTreeItemIcon which
)
886 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
888 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
889 pItem
->SetImage(image
, which
);
892 CalculateSize(pItem
, dc
);
896 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
898 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
900 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
903 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
905 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
907 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
908 pItem
->SetHasPlus(has
);
912 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
914 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
916 // avoid redrawing the tree if no real change
917 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
918 if ( pItem
->IsBold() != bold
)
920 pItem
->SetBold(bold
);
925 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
928 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
930 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
931 pItem
->Attr().SetTextColour(col
);
935 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
938 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
940 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
941 pItem
->Attr().SetBackgroundColour(col
);
945 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
947 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
949 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
950 pItem
->Attr().SetFont(font
);
954 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
956 wxScrolledWindow::SetFont(font
);
958 m_normalFont
= font
;
959 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
960 m_normalFont
.GetFamily(),
961 m_normalFont
.GetStyle(),
963 m_normalFont
.GetUnderlined());
969 // -----------------------------------------------------------------------------
970 // item status inquiries
971 // -----------------------------------------------------------------------------
973 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
975 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
977 // An item is only visible if it's not a descendant of a collapsed item
978 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
979 wxGenericTreeItem
* parent
= pItem
->GetParent();
982 if (!parent
->IsExpanded())
984 parent
= parent
->GetParent();
988 GetViewStart(& startX
, & startY
);
990 wxSize clientSize
= GetClientSize();
993 if (!GetBoundingRect(item
, rect
))
995 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
997 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
999 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1005 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1007 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1009 // consider that the item does have children if it has the "+" button: it
1010 // might not have them (if it had never been expanded yet) but then it
1011 // could have them as well and it's better to err on this side rather than
1012 // disabling some operations which are restricted to the items with
1013 // children for an item which does have them
1014 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1017 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1019 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1021 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1024 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1026 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1028 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1031 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1033 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1035 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1038 // -----------------------------------------------------------------------------
1040 // -----------------------------------------------------------------------------
1042 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1044 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1046 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1049 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1051 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1054 return GetNextChild(item
, cookie
);
1057 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1059 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1061 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1062 if ( (size_t)cookie
< children
.Count() )
1064 return children
.Item((size_t)cookie
++);
1068 // there are no more of them
1069 return wxTreeItemId();
1073 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1075 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1077 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1078 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1081 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1083 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1085 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1086 wxGenericTreeItem
*parent
= i
->GetParent();
1087 if ( parent
== NULL
)
1089 // root item doesn't have any siblings
1090 return wxTreeItemId();
1093 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1094 int index
= siblings
.Index(i
);
1095 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1097 size_t n
= (size_t)(index
+ 1);
1098 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1101 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1103 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1105 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1106 wxGenericTreeItem
*parent
= i
->GetParent();
1107 if ( parent
== NULL
)
1109 // root item doesn't have any siblings
1110 return wxTreeItemId();
1113 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1114 int index
= siblings
.Index(i
);
1115 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1117 return index
== 0 ? wxTreeItemId()
1118 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1121 // Only for internal use right now, but should probably be public
1122 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1124 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1126 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1128 // First see if there are any children.
1129 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1130 if (children
.GetCount() > 0)
1132 return children
.Item(0);
1136 // Try a sibling of this or ancestor instead
1137 wxTreeItemId p
= item
;
1138 wxTreeItemId toFind
;
1141 toFind
= GetNextSibling(p
);
1143 } while (p
.IsOk() && !toFind
.IsOk());
1148 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1150 wxTreeItemId id
= GetRootItem();
1159 } while (id
.IsOk());
1161 return wxTreeItemId();
1164 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1166 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1168 wxTreeItemId id
= item
;
1171 while (id
= GetNext(id
), id
.IsOk())
1177 return wxTreeItemId();
1180 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1182 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1184 wxFAIL_MSG(wxT("not implemented"));
1186 return wxTreeItemId();
1189 // find the first item starting with the given prefix after the given item
1190 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1191 const wxString
& prefixOrig
) const
1193 // match is case insensitive as this is more convenient to the user: having
1194 // to press Shift-letter to go to the item starting with a capital letter
1195 // would be too bothersome
1196 wxString prefix
= prefixOrig
.Lower();
1198 // determine the starting point: we shouldn't take the current item (this
1199 // allows to switch between two items starting with the same letter just by
1200 // pressing it) but we shouldn't jump to the next one if the user is
1201 // continuing to type as otherwise he might easily skip the item he wanted
1202 wxTreeItemId id
= idParent
;
1203 if ( prefix
.length() == 1 )
1208 // look for the item starting with the given prefix after it
1209 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1214 // if we haven't found anything...
1217 // ... wrap to the beginning
1219 if ( HasFlag(wxTR_HIDE_ROOT
) )
1221 // can't select virtual root
1225 // and try all the items (stop when we get to the one we started from)
1226 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1235 // -----------------------------------------------------------------------------
1237 // -----------------------------------------------------------------------------
1239 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1241 const wxString
& text
,
1242 int image
, int selImage
,
1243 wxTreeItemData
*data
)
1245 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1248 // should we give a warning here?
1249 return AddRoot(text
, image
, selImage
, data
);
1252 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1254 wxGenericTreeItem
*item
=
1255 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1259 data
->m_pItem
= (long) item
;
1262 parent
->Insert( item
, previous
);
1267 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1268 int image
, int selImage
,
1269 wxTreeItemData
*data
)
1271 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1273 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1275 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1276 image
, selImage
, data
);
1279 data
->m_pItem
= (long) m_anchor
;
1282 if (HasFlag(wxTR_HIDE_ROOT
))
1284 // if root is hidden, make sure we can navigate
1286 m_anchor
->SetHasPlus();
1288 CalculatePositions();
1291 if (!HasFlag(wxTR_MULTIPLE
))
1293 m_current
= m_key_current
= m_anchor
;
1294 m_current
->SetHilight( TRUE
);
1300 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1301 const wxString
& text
,
1302 int image
, int selImage
,
1303 wxTreeItemData
*data
)
1305 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1308 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1309 const wxTreeItemId
& idPrevious
,
1310 const wxString
& text
,
1311 int image
, int selImage
,
1312 wxTreeItemData
*data
)
1314 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1317 // should we give a warning here?
1318 return AddRoot(text
, image
, selImage
, data
);
1322 if (idPrevious
.IsOk())
1324 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1325 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1326 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1329 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1332 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1334 const wxString
& text
,
1335 int image
, int selImage
,
1336 wxTreeItemData
*data
)
1338 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1341 // should we give a warning here?
1342 return AddRoot(text
, image
, selImage
, data
);
1345 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1348 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1349 const wxString
& text
,
1350 int image
, int selImage
,
1351 wxTreeItemData
*data
)
1353 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1356 // should we give a warning here?
1357 return AddRoot(text
, image
, selImage
, data
);
1360 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1361 image
, selImage
, data
);
1364 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1366 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1367 event
.m_item
= (long) item
;
1368 event
.SetEventObject( this );
1369 ProcessEvent( event
);
1372 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1374 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1376 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1377 item
->DeleteChildren(this);
1380 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1382 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1384 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1386 // don't stay with invalid m_key_current or we will crash in
1387 // the next call to OnChar()
1388 bool changeKeyCurrent
= FALSE
;
1389 wxGenericTreeItem
*itemKey
= m_key_current
;
1392 if ( itemKey
== item
)
1394 // m_key_current is a descendant of the item being deleted
1395 changeKeyCurrent
= TRUE
;
1398 itemKey
= itemKey
->GetParent();
1401 wxGenericTreeItem
*parent
= item
->GetParent();
1404 parent
->GetChildren().Remove( item
); // remove by value
1407 if ( changeKeyCurrent
)
1409 // may be NULL or not
1410 m_key_current
= parent
;
1413 item
->DeleteChildren(this);
1414 SendDeleteEvent(item
);
1418 void wxGenericTreeCtrl::DeleteAllItems()
1424 m_anchor
->DeleteChildren(this);
1431 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1433 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1435 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1436 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1437 _T("can't expand hidden root") );
1439 if ( !item
->HasPlus() )
1442 if ( item
->IsExpanded() )
1445 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1446 event
.m_item
= (long) item
;
1447 event
.SetEventObject( this );
1449 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1451 // cancelled by program
1456 CalculatePositions();
1458 RefreshSubtree(item
);
1460 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1461 ProcessEvent( event
);
1464 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1467 if ( IsExpanded(item
) )
1470 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1471 while ( child
.IsOk() )
1475 child
= GetNextChild(item
, cookie
);
1480 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1482 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1483 _T("can't collapse hidden root") );
1485 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1487 if ( !item
->IsExpanded() )
1490 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1491 event
.m_item
= (long) item
;
1492 event
.SetEventObject( this );
1493 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1495 // cancelled by program
1501 #if 0 // TODO why should items be collapsed recursively?
1502 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1503 size_t count
= children
.Count();
1504 for ( size_t n
= 0; n
< count
; n
++ )
1506 Collapse(children
[n
]);
1510 CalculatePositions();
1512 RefreshSubtree(item
);
1514 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1515 ProcessEvent( event
);
1518 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1521 DeleteChildren(item
);
1524 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1526 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1528 if (item
->IsExpanded())
1534 void wxGenericTreeCtrl::Unselect()
1538 m_current
->SetHilight( FALSE
);
1539 RefreshLine( m_current
);
1543 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1545 if (item
->IsSelected())
1547 item
->SetHilight(FALSE
);
1551 if (item
->HasChildren())
1553 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1554 size_t count
= children
.Count();
1555 for ( size_t n
= 0; n
< count
; ++n
)
1557 UnselectAllChildren(children
[n
]);
1562 void wxGenericTreeCtrl::UnselectAll()
1564 wxTreeItemId rootItem
= GetRootItem();
1566 // the tree might not have the root item at all
1569 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1573 // Recursive function !
1574 // To stop we must have crt_item<last_item
1576 // Tag all next children, when no more children,
1577 // Move to parent (not to tag)
1578 // Keep going... if we found last_item, we stop.
1579 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1581 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1583 if (parent
== NULL
) // This is root item
1584 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1586 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1587 int index
= children
.Index(crt_item
);
1588 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1590 size_t count
= children
.Count();
1591 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1593 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1596 return TagNextChildren(parent
, last_item
, select
);
1599 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1601 crt_item
->SetHilight(select
);
1602 RefreshLine(crt_item
);
1604 if (crt_item
==last_item
)
1607 if (crt_item
->HasChildren())
1609 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1610 size_t count
= children
.Count();
1611 for ( size_t n
= 0; n
< count
; ++n
)
1613 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1621 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1623 // item2 is not necessary after item1
1624 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1626 // choice first' and 'last' between item1 and item2
1627 if (item1
->GetY()<item2
->GetY())
1638 bool select
= m_current
->IsSelected();
1640 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1643 TagNextChildren(first
,last
,select
);
1646 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1647 bool unselect_others
,
1648 bool extended_select
)
1650 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1652 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1653 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1655 //wxCHECK_RET( ( (!unselect_others) && is_single),
1656 // wxT("this is a single selection tree") );
1658 // to keep going anyhow !!!
1661 if (item
->IsSelected())
1662 return; // nothing to do
1663 unselect_others
= TRUE
;
1664 extended_select
= FALSE
;
1666 else if ( unselect_others
&& item
->IsSelected() )
1668 // selection change if there is more than one item currently selected
1669 wxArrayTreeItemIds selected_items
;
1670 if ( GetSelections(selected_items
) == 1 )
1674 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1675 event
.m_item
= (long) item
;
1676 event
.m_itemOld
= (long) m_current
;
1677 event
.SetEventObject( this );
1678 // TODO : Here we don't send any selection mode yet !
1680 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1683 wxTreeItemId parent
= GetParent( itemId
);
1684 while (parent
.IsOk())
1686 if (!IsExpanded(parent
))
1689 parent
= GetParent( parent
);
1692 EnsureVisible( itemId
);
1695 if (unselect_others
)
1697 if (is_single
) Unselect(); // to speed up thing
1702 if (extended_select
)
1706 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1709 // don't change the mark (m_current)
1710 SelectItemRange(m_current
, item
);
1714 bool select
=TRUE
; // the default
1716 // Check if we need to toggle hilight (ctrl mode)
1717 if (!unselect_others
)
1718 select
=!item
->IsSelected();
1720 m_current
= m_key_current
= item
;
1721 m_current
->SetHilight(select
);
1722 RefreshLine( m_current
);
1725 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1726 GetEventHandler()->ProcessEvent( event
);
1729 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1730 wxArrayTreeItemIds
&array
) const
1732 if ( item
->IsSelected() )
1733 array
.Add(wxTreeItemId(item
));
1735 if ( item
->HasChildren() )
1737 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1738 size_t count
= children
.GetCount();
1739 for ( size_t n
= 0; n
< count
; ++n
)
1740 FillArray(children
[n
], array
);
1744 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1747 wxTreeItemId idRoot
= GetRootItem();
1748 if ( idRoot
.IsOk() )
1750 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1752 //else: the tree is empty, so no selections
1754 return array
.Count();
1757 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1759 if (!item
.IsOk()) return;
1761 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1763 // first expand all parent branches
1764 wxGenericTreeItem
*parent
= gitem
->GetParent();
1766 if ( HasFlag(wxTR_HIDE_ROOT
) )
1768 while ( parent
!= m_anchor
)
1771 parent
= parent
->GetParent();
1779 parent
= parent
->GetParent();
1783 //if (parent) CalculatePositions();
1788 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1790 if (!item
.IsOk()) return;
1792 // We have to call this here because the label in
1793 // question might just have been added and no screen
1794 // update taken place.
1795 if (m_dirty
) wxYieldIfNeeded();
1797 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1799 // now scroll to the item
1800 int item_y
= gitem
->GetY();
1804 GetViewStart( &start_x
, &start_y
);
1805 start_y
*= PIXELS_PER_UNIT
;
1809 GetClientSize( &client_w
, &client_h
);
1811 if (item_y
< start_y
+3)
1816 m_anchor
->GetSize( x
, y
, this );
1817 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1818 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1819 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1820 // Item should appear at top
1821 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1823 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1828 m_anchor
->GetSize( x
, y
, this );
1829 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1830 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1831 item_y
+= PIXELS_PER_UNIT
+2;
1832 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1833 // Item should appear at bottom
1834 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
);
1838 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1839 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1841 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1842 wxGenericTreeItem
**item2
)
1844 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1846 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1849 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1850 const wxTreeItemId
& item2
)
1852 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1855 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1857 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1859 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1861 wxCHECK_RET( !s_treeBeingSorted
,
1862 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1864 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1865 if ( children
.Count() > 1 )
1869 s_treeBeingSorted
= this;
1870 children
.Sort(tree_ctrl_compare_func
);
1871 s_treeBeingSorted
= NULL
;
1873 //else: don't make the tree dirty as nothing changed
1876 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1878 return m_imageListNormal
;
1881 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1883 return m_imageListButtons
;
1886 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1888 return m_imageListState
;
1891 void wxGenericTreeCtrl::CalculateLineHeight()
1893 wxClientDC
dc(this);
1894 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1896 if ( m_imageListNormal
)
1898 // Calculate a m_lineHeight value from the normal Image sizes.
1899 // May be toggle off. Then wxGenericTreeCtrl will spread when
1900 // necessary (which might look ugly).
1901 int n
= m_imageListNormal
->GetImageCount();
1902 for (int i
= 0; i
< n
; i
++)
1904 int width
= 0, height
= 0;
1905 m_imageListNormal
->GetSize(i
, width
, height
);
1906 if (height
> m_lineHeight
) m_lineHeight
= height
;
1910 if (m_imageListButtons
)
1912 // Calculate a m_lineHeight value from the Button image sizes.
1913 // May be toggle off. Then wxGenericTreeCtrl will spread when
1914 // necessary (which might look ugly).
1915 int n
= m_imageListButtons
->GetImageCount();
1916 for (int i
= 0; i
< n
; i
++)
1918 int width
= 0, height
= 0;
1919 m_imageListButtons
->GetSize(i
, width
, height
);
1920 if (height
> m_lineHeight
) m_lineHeight
= height
;
1924 if (m_lineHeight
< 30)
1925 m_lineHeight
+= 2; // at least 2 pixels
1927 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1930 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1932 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1933 m_imageListNormal
= imageList
;
1934 m_ownsImageListNormal
= FALSE
;
1936 // Don't do any drawing if we're setting the list to NULL,
1937 // since we may be in the process of deleting the tree control.
1939 CalculateLineHeight();
1942 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1944 if (m_ownsImageListState
) delete m_imageListState
;
1945 m_imageListState
= imageList
;
1946 m_ownsImageListState
= FALSE
;
1949 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1951 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1952 m_imageListButtons
= imageList
;
1953 m_ownsImageListButtons
= FALSE
;
1955 CalculateLineHeight();
1958 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1960 SetImageList(imageList
);
1961 m_ownsImageListNormal
= TRUE
;
1964 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1966 SetStateImageList(imageList
);
1967 m_ownsImageListState
= TRUE
;
1970 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1972 SetButtonsImageList(imageList
);
1973 m_ownsImageListButtons
= TRUE
;
1976 // -----------------------------------------------------------------------------
1978 // -----------------------------------------------------------------------------
1980 void wxGenericTreeCtrl::AdjustMyScrollbars()
1985 m_anchor
->GetSize( x
, y
, this );
1986 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1987 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1988 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1989 int y_pos
= GetScrollPos( wxVERTICAL
);
1990 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1994 SetScrollbars( 0, 0, 0, 0 );
1998 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2000 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2001 return item
->GetHeight();
2003 return m_lineHeight
;
2006 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2008 // TODO implement "state" icon on items
2010 wxTreeItemAttr
*attr
= item
->GetAttributes();
2011 if ( attr
&& attr
->HasFont() )
2012 dc
.SetFont(attr
->GetFont());
2013 else if (item
->IsBold())
2014 dc
.SetFont(m_boldFont
);
2016 long text_w
= 0, text_h
= 0;
2017 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2019 int image_h
= 0, image_w
= 0;
2020 int image
= item
->GetCurrentImage();
2021 if ( image
!= NO_IMAGE
)
2023 if ( m_imageListNormal
)
2025 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2034 int total_h
= GetLineHeight(item
);
2036 if ( item
->IsSelected() )
2038 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2043 if ( attr
&& attr
->HasBackgroundColour() )
2044 colBg
= attr
->GetBackgroundColour();
2046 colBg
= m_backgroundColour
;
2047 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2050 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2052 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2056 DoGetPosition(&x
, &y
);
2058 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2062 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2064 // If it's selected, and there's an image, then we should
2065 // take care to leave the area under the image painted in the
2066 // background colour.
2067 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2068 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2072 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2073 item
->GetWidth()+2, total_h
-offset
);
2077 if ( image
!= NO_IMAGE
)
2079 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2080 m_imageListNormal
->Draw( image
, dc
,
2082 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2083 wxIMAGELIST_DRAW_TRANSPARENT
);
2084 dc
.DestroyClippingRegion();
2087 dc
.SetBackgroundMode(wxTRANSPARENT
);
2088 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2089 dc
.DrawText( item
->GetText(),
2090 (wxCoord
)(image_w
+ item
->GetX()),
2091 (wxCoord
)(item
->GetY() + extraH
));
2093 // restore normal font
2094 dc
.SetFont( m_normalFont
);
2097 // Now y stands for the top of the item, whereas it used to stand for middle !
2098 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2100 int x
= level
*m_indent
;
2101 if (!HasFlag(wxTR_HIDE_ROOT
))
2105 else if (level
== 0)
2107 // always expand hidden root
2109 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2110 int count
= children
.Count();
2116 PaintLevel(children
[n
], dc
, 1, y
);
2117 } while (++n
< count
);
2119 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2121 // draw line down to last child
2122 origY
+= GetLineHeight(children
[0])>>1;
2123 oldY
+= GetLineHeight(children
[n
-1])>>1;
2124 dc
.DrawLine(3, origY
, 3, oldY
);
2130 item
->SetX(x
+m_spacing
);
2133 int h
= GetLineHeight(item
);
2135 int y_mid
= y_top
+ (h
>>1);
2138 int exposed_x
= dc
.LogicalToDeviceX(0);
2139 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2141 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2145 // don't draw rect outline if we already have the
2146 // background color under Mac
2147 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2148 #endif // !__WXMAC__
2152 if ( item
->IsSelected() )
2154 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2158 wxTreeItemAttr
*attr
= item
->GetAttributes();
2159 if (attr
&& attr
->HasTextColour())
2160 colText
= attr
->GetTextColour();
2162 colText
= GetForegroundColour();
2166 dc
.SetTextForeground(colText
);
2170 PaintItem(item
, dc
);
2172 if (HasFlag(wxTR_ROW_LINES
))
2174 // if the background colour is white, choose a
2175 // contrasting color for the lines
2176 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2177 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2178 dc
.DrawLine(0, y_top
, 10000, y_top
);
2179 dc
.DrawLine(0, y
, 10000, y
);
2182 // restore DC objects
2183 dc
.SetBrush(*wxWHITE_BRUSH
);
2184 dc
.SetPen(m_dottedPen
);
2185 dc
.SetTextForeground(*wxBLACK
);
2187 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2189 if (!HasFlag(wxTR_NO_LINES
))
2191 if (x
> (signed)m_indent
)
2192 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2193 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2194 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2195 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2198 if (m_imageListButtons
!= NULL
)
2200 // draw the image button here
2201 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2202 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2203 if (item
->IsSelected())
2204 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2205 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2206 int xx
= x
- (image_w
>>1);
2207 int yy
= y_mid
- (image_h
>>1);
2208 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2209 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2210 wxIMAGELIST_DRAW_TRANSPARENT
);
2211 dc
.DestroyClippingRegion();
2213 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2215 // draw the twisty button here
2217 if (HasFlag(wxTR_AQUA_BUTTONS
))
2219 if (item
->IsExpanded())
2220 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2222 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2226 dc
.SetBrush(*m_hilightBrush
);
2227 dc
.SetPen(*wxBLACK_PEN
);
2230 if (item
->IsExpanded())
2233 button
[0].y
= y_mid
-2;
2235 button
[1].y
= y_mid
-2;
2237 button
[2].y
= y_mid
+3;
2241 button
[0].y
= y_mid
-5;
2243 button
[1].y
= y_mid
+5;
2245 button
[2].y
= y_mid
;
2248 dc
.DrawPolygon(3, button
);
2249 dc
.SetPen(m_dottedPen
);
2252 else // if (HasFlag(wxTR_HAS_BUTTONS))
2254 // draw the plus sign here
2255 dc
.SetPen(*wxGREY_PEN
);
2256 dc
.SetBrush(*wxWHITE_BRUSH
);
2257 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2258 dc
.SetPen(*wxBLACK_PEN
);
2259 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2260 if (!item
->IsExpanded())
2261 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2262 dc
.SetPen(m_dottedPen
);
2265 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2267 // draw the horizontal line here
2269 if (x
> (signed)m_indent
)
2270 x_start
-= m_indent
;
2271 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2273 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2277 if (item
->IsExpanded())
2279 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2280 int count
= children
.Count();
2287 PaintLevel(children
[n
], dc
, level
, y
);
2288 } while (++n
< count
);
2290 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2292 // draw line down to last child
2293 oldY
+= GetLineHeight(children
[n
-1])>>1;
2294 if (HasButtons()) y_mid
+= 5;
2295 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2301 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2305 if ( item
->HasPlus() )
2307 // it's a folder, indicate it by a border
2312 // draw a line under the drop target because the item will be
2314 DrawLine(item
, TRUE
/* below */);
2317 SetCursor(wxCURSOR_BULLSEYE
);
2322 SetCursor(wxCURSOR_NO_ENTRY
);
2326 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2328 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2330 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2332 wxClientDC
dc(this);
2334 dc
.SetLogicalFunction(wxINVERT
);
2335 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2337 int w
= i
->GetWidth() + 2;
2338 int h
= GetLineHeight(i
) + 2;
2340 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2343 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2345 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2347 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2349 wxClientDC
dc(this);
2351 dc
.SetLogicalFunction(wxINVERT
);
2357 y
+= GetLineHeight(i
) - 1;
2360 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2363 // -----------------------------------------------------------------------------
2364 // wxWindows callbacks
2365 // -----------------------------------------------------------------------------
2367 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2375 dc
.SetFont( m_normalFont
);
2376 dc
.SetPen( m_dottedPen
);
2378 // this is now done dynamically
2379 //if(GetImageList() == NULL)
2380 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2383 PaintLevel( m_anchor
, dc
, 0, y
);
2386 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2395 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2404 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2406 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2407 te
.m_evtKey
= event
;
2408 te
.SetEventObject( this );
2409 if ( GetEventHandler()->ProcessEvent( te
) )
2411 // intercepted by the user code
2415 if ( (m_current
== 0) || (m_key_current
== 0) )
2421 // how should the selection work for this event?
2422 bool is_multiple
, extended_select
, unselect_others
;
2423 EventFlagsToSelType(GetWindowStyleFlag(),
2425 event
.ControlDown(),
2426 is_multiple
, extended_select
, unselect_others
);
2430 // * : Expand all/Collapse all
2431 // ' ' | return : activate
2432 // up : go up (not last children!)
2434 // left : go to parent
2435 // right : open if parent and go next
2436 // home : go to root
2437 // end : go to last item without opening parents
2438 // alnum : start or continue searching for the item with this prefix
2439 int keyCode
= event
.KeyCode();
2444 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2452 if ( !IsExpanded(m_current
) )
2455 ExpandAll(m_current
);
2458 //else: fall through to Collapse() it
2462 if (IsExpanded(m_current
))
2464 Collapse(m_current
);
2471 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2472 event
.m_item
= (long) m_current
;
2473 event
.SetEventObject( this );
2474 GetEventHandler()->ProcessEvent( event
);
2478 // up goes to the previous sibling or to the last
2479 // of its children if it's expanded
2482 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2485 prev
= GetParent( m_key_current
);
2486 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2488 break; // don't go to root if it is hidden
2493 wxTreeItemId current
= m_key_current
;
2494 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2495 if (current
== GetFirstChild( prev
, cookie
))
2497 // otherwise we return to where we came from
2498 SelectItem( prev
, unselect_others
, extended_select
);
2499 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2506 while ( IsExpanded(prev
) && HasChildren(prev
) )
2508 wxTreeItemId child
= GetLastChild(prev
);
2515 SelectItem( prev
, unselect_others
, extended_select
);
2516 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2521 // left arrow goes to the parent
2524 wxTreeItemId prev
= GetParent( m_current
);
2525 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2527 // don't go to root if it is hidden
2528 prev
= GetPrevSibling( m_current
);
2532 SelectItem( prev
, unselect_others
, extended_select
);
2538 // this works the same as the down arrow except that we
2539 // also expand the item if it wasn't expanded yet
2545 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2548 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2549 SelectItem( child
, unselect_others
, extended_select
);
2550 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2554 wxTreeItemId next
= GetNextSibling( m_key_current
);
2557 wxTreeItemId current
= m_key_current
;
2558 while (current
&& !next
)
2560 current
= GetParent( current
);
2561 if (current
) next
= GetNextSibling( current
);
2566 SelectItem( next
, unselect_others
, extended_select
);
2567 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2573 // <End> selects the last visible tree item
2576 wxTreeItemId last
= GetRootItem();
2578 while ( last
.IsOk() && IsExpanded(last
) )
2580 wxTreeItemId lastChild
= GetLastChild(last
);
2582 // it may happen if the item was expanded but then all of
2583 // its children have been deleted - so IsExpanded() returned
2584 // TRUE, but GetLastChild() returned invalid item
2593 SelectItem( last
, unselect_others
, extended_select
);
2598 // <Home> selects the root item
2601 wxTreeItemId prev
= GetRootItem();
2605 if ( HasFlag(wxTR_HIDE_ROOT
) )
2608 prev
= GetFirstChild(prev
, dummy
);
2613 SelectItem( prev
, unselect_others
, extended_select
);
2618 // do not use wxIsalnum() here
2619 if ( !event
.HasModifiers() &&
2620 ((keyCode
>= '0' && keyCode
<= '9') ||
2621 (keyCode
>= 'a' && keyCode
<= 'z') ||
2622 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2624 // find the next item starting with the given prefix
2625 char ch
= (char)keyCode
;
2627 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2638 // also start the timer to reset the current prefix if the user
2639 // doesn't press any more alnum keys soon -- we wouldn't want
2640 // to use this prefix for a new item search
2643 m_findTimer
= new wxTreeFindTimer(this);
2646 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2655 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2657 // JACS: removed wxYieldIfNeeded() because it can cause the window
2658 // to be deleted from under us if a close window event is pending
2663 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2664 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2665 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2666 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2667 if (flags
) return wxTreeItemId();
2669 if (m_anchor
== NULL
)
2671 flags
= wxTREE_HITTEST_NOWHERE
;
2672 return wxTreeItemId();
2675 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2679 flags
= wxTREE_HITTEST_NOWHERE
;
2680 return wxTreeItemId();
2685 // get the bounding rectangle of the item (or of its label only)
2686 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2688 bool WXUNUSED(textOnly
)) const
2690 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2692 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2695 GetViewStart(& startX
, & startY
);
2697 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2698 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2699 rect
.width
= i
->GetWidth();
2700 //rect.height = i->GetHeight();
2701 rect
.height
= GetLineHeight(i
);
2708 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2710 if (!item
.IsOk()) return;
2712 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2714 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2715 te
.m_item
= (long) m_currentEdit
;
2716 te
.SetEventObject( this );
2717 GetEventHandler()->ProcessEvent( te
);
2719 if (!te
.IsAllowed()) return;
2721 // We have to call this here because the label in
2722 // question might just have been added and no screen
2723 // update taken place.
2724 if (m_dirty
) wxYieldIfNeeded();
2726 wxString s
= m_currentEdit
->GetText();
2727 int w
= m_currentEdit
->GetWidth();
2728 int h
= m_currentEdit
->GetHeight();
2730 CalcScrolledPosition(m_currentEdit
->GetX(), m_currentEdit
->GetY(), &x
, &y
);
2735 int image
= m_currentEdit
->GetCurrentImage();
2736 if ( image
!= NO_IMAGE
)
2738 if ( m_imageListNormal
)
2740 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2745 wxFAIL_MSG(_T("you must create an image list to use images!"));
2749 w
-= image_w
+ 4; // I don't know why +4 is needed
2751 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2761 void wxGenericTreeCtrl::OnRenameTimer()
2766 void wxGenericTreeCtrl::OnRenameAccept()
2768 // TODO if the validator fails this causes a crash
2769 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2770 le
.m_item
= (long) m_currentEdit
;
2771 le
.SetEventObject( this );
2772 le
.m_label
= m_renameRes
;
2773 GetEventHandler()->ProcessEvent( le
);
2775 if (!le
.IsAllowed()) return;
2777 SetItemText( m_currentEdit
, m_renameRes
);
2780 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2782 if ( !m_anchor
) return;
2784 // we process left mouse up event (enables in-place edit), right down
2785 // (pass to the user code), left dbl click (activate item) and
2786 // dragging/moving events for items drag-and-drop
2787 if ( !(event
.LeftDown() ||
2789 event
.RightDown() ||
2790 event
.LeftDClick() ||
2792 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2799 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2802 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2804 if ( event
.Dragging() && !m_isDragging
)
2806 if (m_dragCount
== 0)
2811 if (m_dragCount
!= 3)
2813 // wait until user drags a bit further...
2817 wxEventType command
= event
.RightIsDown()
2818 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2819 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2821 wxTreeEvent
nevent( command
, GetId() );
2822 nevent
.m_item
= (long) m_current
;
2823 nevent
.SetEventObject(this);
2825 // by default the dragging is not supported, the user code must
2826 // explicitly allow the event for it to take place
2829 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2831 // we're going to drag this item
2832 m_isDragging
= TRUE
;
2834 // remember the old cursor because we will change it while
2836 m_oldCursor
= m_cursor
;
2838 // in a single selection control, hide the selection temporarily
2839 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2841 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2843 if ( m_oldSelection
)
2845 m_oldSelection
->SetHilight(FALSE
);
2846 RefreshLine(m_oldSelection
);
2853 else if ( event
.Moving() )
2855 if ( item
!= m_dropTarget
)
2857 // unhighlight the previous drop target
2858 DrawDropEffect(m_dropTarget
);
2860 m_dropTarget
= item
;
2862 // highlight the current drop target if any
2863 DrawDropEffect(m_dropTarget
);
2868 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2870 // erase the highlighting
2871 DrawDropEffect(m_dropTarget
);
2873 if ( m_oldSelection
)
2875 m_oldSelection
->SetHilight(TRUE
);
2876 RefreshLine(m_oldSelection
);
2877 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2880 // generate the drag end event
2881 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2883 event
.m_item
= (long) item
;
2884 event
.m_pointDrag
= pt
;
2885 event
.SetEventObject(this);
2887 (void)GetEventHandler()->ProcessEvent(event
);
2889 m_isDragging
= FALSE
;
2890 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2894 SetCursor(m_oldCursor
);
2900 // here we process only the messages which happen on tree items
2904 if (item
== NULL
) return; /* we hit the blank area */
2906 if ( event
.RightDown() )
2908 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2909 nevent
.m_item
= (long) item
;
2910 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2911 nevent
.SetEventObject(this);
2912 GetEventHandler()->ProcessEvent(nevent
);
2914 else if ( event
.LeftUp() )
2918 if ( (item
== m_current
) &&
2919 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2920 HasFlag(wxTR_EDIT_LABELS
) )
2922 if ( m_renameTimer
)
2924 if ( m_renameTimer
->IsRunning() )
2925 m_renameTimer
->Stop();
2929 m_renameTimer
= new wxTreeRenameTimer( this );
2932 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
2935 m_lastOnSame
= FALSE
;
2938 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2940 if ( event
.LeftDown() )
2942 m_lastOnSame
= item
== m_current
;
2945 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2947 // only toggle the item for a single click, double click on
2948 // the button doesn't do anything (it toggles the item twice)
2949 if ( event
.LeftDown() )
2954 // don't select the item if the button was clicked
2958 // how should the selection work for this event?
2959 bool is_multiple
, extended_select
, unselect_others
;
2960 EventFlagsToSelType(GetWindowStyleFlag(),
2962 event
.ControlDown(),
2963 is_multiple
, extended_select
, unselect_others
);
2965 SelectItem(item
, unselect_others
, extended_select
);
2967 // For some reason, Windows isn't recognizing a left double-click,
2968 // so we need to simulate it here. Allow 200 milliseconds for now.
2969 if ( event
.LeftDClick() )
2971 // double clicking should not start editing the item label
2972 if ( m_renameTimer
)
2973 m_renameTimer
->Stop();
2975 m_lastOnSame
= FALSE
;
2977 // send activate event first
2978 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2979 nevent
.m_item
= (long) item
;
2980 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2981 nevent
.SetEventObject( this );
2982 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2984 // if the user code didn't process the activate event,
2985 // handle it ourselves by toggling the item when it is
2987 if ( item
->HasPlus() )
2997 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2999 /* after all changes have been done to the tree control,
3000 * we actually redraw the tree when everything is over */
3002 if (!m_dirty
) return;
3006 CalculatePositions();
3008 AdjustMyScrollbars();
3011 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3016 wxTreeItemAttr
*attr
= item
->GetAttributes();
3017 if ( attr
&& attr
->HasFont() )
3018 dc
.SetFont(attr
->GetFont());
3019 else if ( item
->IsBold() )
3020 dc
.SetFont(m_boldFont
);
3022 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3025 // restore normal font
3026 dc
.SetFont( m_normalFont
);
3030 int image
= item
->GetCurrentImage();
3031 if ( image
!= NO_IMAGE
)
3033 if ( m_imageListNormal
)
3035 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3040 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3043 total_h
+= 2; // at least 2 pixels
3045 total_h
+= total_h
/10; // otherwise 10% extra spacing
3047 item
->SetHeight(total_h
);
3048 if (total_h
>m_lineHeight
)
3049 m_lineHeight
=total_h
;
3051 item
->SetWidth(image_w
+text_w
+2);
3054 // -----------------------------------------------------------------------------
3055 // for developper : y is now the top of the level
3056 // not the middle of it !
3057 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3059 int x
= level
*m_indent
;
3060 if (!HasFlag(wxTR_HIDE_ROOT
))
3064 else if (level
== 0)
3066 // a hidden root is not evaluated, but its
3067 // children are always calculated
3071 CalculateSize( item
, dc
);
3074 item
->SetX( x
+m_spacing
);
3076 y
+= GetLineHeight(item
);
3078 if ( !item
->IsExpanded() )
3080 // we don't need to calculate collapsed branches
3085 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3086 size_t n
, count
= children
.Count();
3088 for (n
= 0; n
< count
; ++n
)
3089 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3092 void wxGenericTreeCtrl::CalculatePositions()
3094 if ( !m_anchor
) return;
3096 wxClientDC
dc(this);
3099 dc
.SetFont( m_normalFont
);
3101 dc
.SetPen( m_dottedPen
);
3102 //if(GetImageList() == NULL)
3103 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3106 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3109 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3111 if (m_dirty
) return;
3113 wxSize client
= GetClientSize();
3116 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3117 rect
.width
= client
.x
;
3118 rect
.height
= client
.y
;
3120 Refresh(TRUE
, &rect
);
3122 AdjustMyScrollbars();
3125 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3127 if (m_dirty
) return;
3130 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3131 rect
.width
= GetClientSize().x
;
3132 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3134 Refresh(TRUE
, &rect
);
3137 void wxGenericTreeCtrl::RefreshSelected()
3139 // TODO: this is awfully inefficient, we should keep the list of all
3140 // selected items internally, should be much faster
3142 RefreshSelectedUnder(m_anchor
);
3145 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3147 if ( item
->IsSelected() )
3150 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3151 size_t count
= children
.GetCount();
3152 for ( size_t n
= 0; n
< count
; n
++ )
3154 RefreshSelectedUnder(children
[n
]);
3158 // ----------------------------------------------------------------------------
3159 // changing colours: we need to refresh the tree control
3160 // ----------------------------------------------------------------------------
3162 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3164 if ( !wxWindow::SetBackgroundColour(colour
) )
3172 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3174 if ( !wxWindow::SetForegroundColour(colour
) )
3182 #endif // wxUSE_TREECTRL