1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/treebase.h"
34 #include "wx/generic/treectlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
41 #include "wx/renderer.h"
43 // -----------------------------------------------------------------------------
45 // -----------------------------------------------------------------------------
47 class WXDLLEXPORT wxGenericTreeItem
;
49 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 static const int NO_IMAGE
= -1;
57 static const int PIXELS_PER_UNIT
= 10;
59 // -----------------------------------------------------------------------------
61 // -----------------------------------------------------------------------------
63 // timer used for enabling in-place edit
64 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
67 // start editing the current item after half a second (if the mouse hasn't
68 // been clicked/moved)
71 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
73 virtual void Notify();
76 wxGenericTreeCtrl
*m_owner
;
78 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
81 // control used for in-place edit
82 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
85 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
88 void OnChar( wxKeyEvent
&event
);
89 void OnKeyUp( wxKeyEvent
&event
);
90 void OnKillFocus( wxFocusEvent
&event
);
96 wxGenericTreeCtrl
*m_owner
;
97 wxGenericTreeItem
*m_itemEdited
;
98 wxString m_startValue
;
101 DECLARE_EVENT_TABLE()
102 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
105 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
106 // for a sufficiently long time
107 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
110 // reset the current prefix after half a second of inactivity
111 enum { DELAY
= 500 };
113 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
115 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
118 wxGenericTreeCtrl
*m_owner
;
120 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
124 class WXDLLEXPORT wxGenericTreeItem
128 wxGenericTreeItem() { m_data
= NULL
; }
129 wxGenericTreeItem( wxGenericTreeItem
*parent
,
130 const wxString
& text
,
133 wxTreeItemData
*data
);
135 ~wxGenericTreeItem();
138 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
140 const wxString
& GetText() const { return m_text
; }
141 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
142 { return m_images
[which
]; }
143 wxTreeItemData
*GetData() const { return m_data
; }
145 // returns the current image for the item (depending on its
146 // selected/expanded/whatever state)
147 int GetCurrentImage() const;
149 void SetText( const wxString
&text
);
150 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
151 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
153 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
155 void SetBold(bool bold
) { m_isBold
= bold
; }
157 int GetX() const { return m_x
; }
158 int GetY() const { return m_y
; }
160 void SetX(int x
) { m_x
= x
; }
161 void SetY(int y
) { m_y
= y
; }
163 int GetHeight() const { return m_height
; }
164 int GetWidth() const { return m_width
; }
166 void SetHeight(int h
) { m_height
= h
; }
167 void SetWidth(int w
) { m_width
= w
; }
169 wxGenericTreeItem
*GetParent() const { return m_parent
; }
172 // deletes all children notifying the treectrl about it if !NULL
174 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
176 // get count of all children (and grand children if 'recursively')
177 size_t GetChildrenCount(bool recursively
= TRUE
) const;
179 void Insert(wxGenericTreeItem
*child
, size_t index
)
180 { m_children
.Insert(child
, index
); }
182 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
184 // return the item at given position (or NULL if no item), onButton is
185 // TRUE if the point belongs to the item's button, otherwise it lies
186 // on the item's label
187 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
188 const wxGenericTreeCtrl
*,
192 void Expand() { m_isCollapsed
= FALSE
; }
193 void Collapse() { m_isCollapsed
= TRUE
; }
195 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
198 bool HasChildren() const { return !m_children
.IsEmpty(); }
199 bool IsSelected() const { return m_hasHilight
!= 0; }
200 bool IsExpanded() const { return !m_isCollapsed
; }
201 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
202 bool IsBold() const { return m_isBold
!= 0; }
205 // get them - may be NULL
206 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
207 // get them ensuring that the pointer is not NULL
208 wxTreeItemAttr
& Attr()
212 m_attr
= new wxTreeItemAttr
;
218 void SetAttributes(wxTreeItemAttr
*attr
)
220 if ( m_ownsAttr
) delete m_attr
;
224 // set them and delete when done
225 void AssignAttributes(wxTreeItemAttr
*attr
)
232 // since there can be very many of these, we save size by chosing
233 // the smallest representation for the elements and by ordering
234 // the members to avoid padding.
235 wxString m_text
; // label to be rendered for item
237 wxTreeItemData
*m_data
; // user-provided data
239 wxArrayGenericTreeItems m_children
; // list of children
240 wxGenericTreeItem
*m_parent
; // parent of this item
242 wxTreeItemAttr
*m_attr
; // attributes???
244 // tree ctrl images for the normal, selected, expanded and
245 // expanded+selected states
246 short m_images
[wxTreeItemIcon_Max
];
248 wxCoord m_x
; // (virtual) offset from top
249 wxCoord m_y
; // (virtual) offset from left
250 short m_width
; // width of this item
251 unsigned char m_height
; // height of this item
253 // use bitfields to save size
254 int m_isCollapsed
:1;
255 int m_hasHilight
:1; // same as focused
256 int m_hasPlus
:1; // used for item which doesn't have
257 // children but has a [+] button
258 int m_isBold
:1; // render the label in bold font
259 int m_ownsAttr
:1; // delete attribute when done
261 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
264 // =============================================================================
266 // =============================================================================
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 // translate the key or mouse event flags to the type of selection we're
274 static void EventFlagsToSelType(long style
,
278 bool &extended_select
,
279 bool &unselect_others
)
281 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
282 extended_select
= shiftDown
&& is_multiple
;
283 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
286 // check if the given item is under another one
287 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
291 if ( item
== parent
)
293 // item is a descendant of parent
297 item
= item
->GetParent();
303 // -----------------------------------------------------------------------------
304 // wxTreeRenameTimer (internal)
305 // -----------------------------------------------------------------------------
307 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
312 void wxTreeRenameTimer::Notify()
314 m_owner
->OnRenameTimer();
317 //-----------------------------------------------------------------------------
318 // wxTreeTextCtrl (internal)
319 //-----------------------------------------------------------------------------
321 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
322 EVT_CHAR (wxTreeTextCtrl::OnChar
)
323 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
324 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
327 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
328 wxGenericTreeItem
*item
)
329 : m_itemEdited(item
), m_startValue(item
->GetText())
334 int w
= m_itemEdited
->GetWidth(),
335 h
= m_itemEdited
->GetHeight();
338 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
343 int image
= item
->GetCurrentImage();
344 if ( image
!= NO_IMAGE
)
346 if ( m_owner
->m_imageListNormal
)
348 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
353 wxFAIL_MSG(_T("you must create an image list to use images!"));
357 // FIXME: what are all these hardcoded 4, 8 and 11s really?
361 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
362 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
365 bool wxTreeTextCtrl::AcceptChanges()
367 const wxString value
= GetValue();
369 if ( value
== m_startValue
)
371 // nothing changed, always accept
375 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
377 // vetoed by the user
381 // accepted, do rename the item
382 m_owner
->SetItemText(m_itemEdited
, value
);
387 void wxTreeTextCtrl::Finish()
391 m_owner
->ResetTextControl();
393 wxPendingDelete
.Append(this);
397 m_owner
->SetFocus(); // This doesn't work. TODO.
401 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
403 switch ( event
.m_keyCode
)
406 if ( !AcceptChanges() )
408 // vetoed by the user, don't disappear
415 m_owner
->OnRenameCancelled(m_itemEdited
);
423 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
427 // auto-grow the textctrl:
428 wxSize parentSize
= m_owner
->GetSize();
429 wxPoint myPos
= GetPosition();
430 wxSize mySize
= GetSize();
432 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
433 if (myPos
.x
+ sx
> parentSize
.x
)
434 sx
= parentSize
.x
- myPos
.x
;
443 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
451 if ( AcceptChanges() )
457 // -----------------------------------------------------------------------------
459 // -----------------------------------------------------------------------------
461 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
462 const wxString
& text
,
463 int image
, int selImage
,
464 wxTreeItemData
*data
)
467 m_images
[wxTreeItemIcon_Normal
] = image
;
468 m_images
[wxTreeItemIcon_Selected
] = selImage
;
469 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
470 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
475 m_isCollapsed
= TRUE
;
476 m_hasHilight
= FALSE
;
482 m_attr
= (wxTreeItemAttr
*)NULL
;
485 // We don't know the height here yet.
490 wxGenericTreeItem::~wxGenericTreeItem()
494 if (m_ownsAttr
) delete m_attr
;
496 wxASSERT_MSG( m_children
.IsEmpty(),
497 wxT("please call DeleteChildren() before deleting the item") );
500 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
502 size_t count
= m_children
.Count();
503 for ( size_t n
= 0; n
< count
; n
++ )
505 wxGenericTreeItem
*child
= m_children
[n
];
507 tree
->SendDeleteEvent(child
);
509 child
->DeleteChildren(tree
);
516 void wxGenericTreeItem::SetText( const wxString
&text
)
521 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
523 size_t count
= m_children
.Count();
527 size_t total
= count
;
528 for (size_t n
= 0; n
< count
; ++n
)
530 total
+= m_children
[n
]->GetChildrenCount();
536 void wxGenericTreeItem::GetSize( int &x
, int &y
,
537 const wxGenericTreeCtrl
*theButton
)
539 int bottomY
=m_y
+theButton
->GetLineHeight(this);
540 if ( y
< bottomY
) y
= bottomY
;
541 int width
= m_x
+ m_width
;
542 if ( x
< width
) x
= width
;
546 size_t count
= m_children
.Count();
547 for ( size_t n
= 0; n
< count
; ++n
)
549 m_children
[n
]->GetSize( x
, y
, theButton
);
554 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
555 const wxGenericTreeCtrl
*theCtrl
,
559 // for a hidden root node, don't evaluate it, but do evaluate children
560 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
563 int h
= theCtrl
->GetLineHeight(this);
564 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
566 int y_mid
= m_y
+ h
/2;
567 if (point
.y
< y_mid
)
568 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
570 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
572 int xCross
= m_x
- theCtrl
->GetSpacing();
574 // according to the drawing code the triangels are drawn
575 // at -4 , -4 from the position up to +10/+10 max
576 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
577 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
578 HasPlus() && theCtrl
->HasButtons() )
580 // 5 is the size of the plus sign
581 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
582 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
583 HasPlus() && theCtrl
->HasButtons() )
586 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
590 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
595 // assuming every image (normal and selected) has the same size!
596 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
597 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
600 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
601 flags
|= wxTREE_HITTEST_ONITEMICON
;
603 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
609 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
610 if (point
.x
> m_x
+m_width
)
611 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
616 // if children are expanded, fall through to evaluate them
617 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
621 size_t count
= m_children
.Count();
622 for ( size_t n
= 0; n
< count
; n
++ )
624 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
632 return (wxGenericTreeItem
*) NULL
;
635 int wxGenericTreeItem::GetCurrentImage() const
637 int image
= NO_IMAGE
;
642 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
645 if ( image
== NO_IMAGE
)
647 // we usually fall back to the normal item, but try just the
648 // expanded one (and not selected) first in this case
649 image
= GetImage(wxTreeItemIcon_Expanded
);
655 image
= GetImage(wxTreeItemIcon_Selected
);
658 // maybe it doesn't have the specific image we want,
659 // try the default one instead
660 if ( image
== NO_IMAGE
) image
= GetImage();
665 // -----------------------------------------------------------------------------
666 // wxGenericTreeCtrl implementation
667 // -----------------------------------------------------------------------------
669 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
671 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
672 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
673 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
674 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
675 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
676 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
677 EVT_TREE_ITEM_GETTOOLTIP(-1, wxGenericTreeCtrl::OnGetToolTip
)
680 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
682 * wxTreeCtrl has to be a real class or we have problems with
683 * the run-time information.
686 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
689 // -----------------------------------------------------------------------------
690 // construction/destruction
691 // -----------------------------------------------------------------------------
693 void wxGenericTreeCtrl::Init()
695 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
703 m_hilightBrush
= new wxBrush
705 wxSystemSettings::GetColour
707 wxSYS_COLOUR_HIGHLIGHT
712 m_hilightUnfocusedBrush
= new wxBrush
714 wxSystemSettings::GetColour
716 wxSYS_COLOUR_BTNSHADOW
721 m_imageListNormal
= m_imageListButtons
=
722 m_imageListState
= (wxImageList
*) NULL
;
723 m_ownsImageListNormal
= m_ownsImageListButtons
=
724 m_ownsImageListState
= FALSE
;
727 m_isDragging
= FALSE
;
728 m_dropTarget
= m_oldSelection
= NULL
;
732 m_renameTimer
= NULL
;
735 m_lastOnSame
= FALSE
;
737 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
738 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
739 m_normalFont
.GetFamily(),
740 m_normalFont
.GetStyle(),
742 m_normalFont
.GetUnderlined(),
743 m_normalFont
.GetFaceName(),
744 m_normalFont
.GetEncoding());
747 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
752 const wxValidator
& wxVALIDATOR_PARAM(validator
),
753 const wxString
& name
)
757 wxGetOsVersion( &major
, &minor
);
759 style
&= ~wxTR_LINES_AT_ROOT
;
760 style
|= wxTR_NO_LINES
;
762 style
|= wxTR_ROW_LINES
;
765 wxScrolledWindow::Create( parent
, id
, pos
, size
,
766 style
|wxHSCROLL
|wxVSCROLL
, name
);
768 // If the tree display has no buttons, but does have
769 // connecting lines, we can use a narrower layout.
770 // It may not be a good idea to force this...
771 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
778 SetValidator( validator
);
781 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
782 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
784 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
785 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
790 wxGenericTreeCtrl::~wxGenericTreeCtrl()
792 delete m_hilightBrush
;
793 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 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
881 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
883 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
884 return pItem
->Attr().GetTextColour();
887 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
889 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
891 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
892 return pItem
->Attr().GetBackgroundColour();
895 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
897 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
899 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
900 return pItem
->Attr().GetFont();
903 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
905 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
908 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
909 pItem
->SetText(text
);
910 CalculateSize(pItem
, dc
);
914 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
916 wxTreeItemIcon which
)
918 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
920 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
921 pItem
->SetImage(image
, which
);
924 CalculateSize(pItem
, dc
);
928 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
930 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
935 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
938 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
940 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
942 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
943 pItem
->SetHasPlus(has
);
947 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
949 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
951 // avoid redrawing the tree if no real change
952 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
953 if ( pItem
->IsBold() != bold
)
955 pItem
->SetBold(bold
);
960 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
963 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
965 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
966 pItem
->Attr().SetTextColour(col
);
970 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
973 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
975 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
976 pItem
->Attr().SetBackgroundColour(col
);
980 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
982 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
984 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
985 pItem
->Attr().SetFont(font
);
989 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
991 wxScrolledWindow::SetFont(font
);
993 m_normalFont
= font
;
994 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
995 m_normalFont
.GetFamily(),
996 m_normalFont
.GetStyle(),
998 m_normalFont
.GetUnderlined(),
999 m_normalFont
.GetFaceName(),
1000 m_normalFont
.GetEncoding());
1006 // -----------------------------------------------------------------------------
1007 // item status inquiries
1008 // -----------------------------------------------------------------------------
1010 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1012 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1014 // An item is only visible if it's not a descendant of a collapsed item
1015 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1016 wxGenericTreeItem
* parent
= pItem
->GetParent();
1019 if (!parent
->IsExpanded())
1021 parent
= parent
->GetParent();
1025 GetViewStart(& startX
, & startY
);
1027 wxSize clientSize
= GetClientSize();
1030 if (!GetBoundingRect(item
, rect
))
1032 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1034 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1036 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1042 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1044 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1046 // consider that the item does have children if it has the "+" button: it
1047 // might not have them (if it had never been expanded yet) but then it
1048 // could have them as well and it's better to err on this side rather than
1049 // disabling some operations which are restricted to the items with
1050 // children for an item which does have them
1051 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1054 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1056 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1058 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1061 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1063 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1065 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1068 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1070 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1072 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1075 // -----------------------------------------------------------------------------
1077 // -----------------------------------------------------------------------------
1079 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1081 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1083 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1086 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1087 wxTreeItemIdValue
& cookie
) const
1089 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1092 return GetNextChild(item
, cookie
);
1095 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1096 wxTreeItemIdValue
& cookie
) const
1098 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1100 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1102 // it's ok to cast cookie to size_t, we never have indices big enough to
1103 // overflow "void *"
1104 size_t *pIndex
= (size_t *)&cookie
;
1105 if ( *pIndex
< children
.Count() )
1107 return children
.Item((*pIndex
)++);
1111 // there are no more of them
1112 return wxTreeItemId();
1116 #if WXWIN_COMPATIBILITY_2_4
1118 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1121 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1124 return GetNextChild(item
, cookie
);
1127 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1130 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1132 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1133 if ( (size_t)cookie
< children
.Count() )
1135 return children
.Item((size_t)cookie
++);
1139 // there are no more of them
1140 return wxTreeItemId();
1144 #endif // WXWIN_COMPATIBILITY_2_4
1146 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1148 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1150 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1151 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1154 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1156 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1158 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1159 wxGenericTreeItem
*parent
= i
->GetParent();
1160 if ( parent
== NULL
)
1162 // root item doesn't have any siblings
1163 return wxTreeItemId();
1166 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1167 int index
= siblings
.Index(i
);
1168 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1170 size_t n
= (size_t)(index
+ 1);
1171 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1174 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1176 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1178 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1179 wxGenericTreeItem
*parent
= i
->GetParent();
1180 if ( parent
== NULL
)
1182 // root item doesn't have any siblings
1183 return wxTreeItemId();
1186 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1187 int index
= siblings
.Index(i
);
1188 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1190 return index
== 0 ? wxTreeItemId()
1191 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1194 // Only for internal use right now, but should probably be public
1195 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1197 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1199 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1201 // First see if there are any children.
1202 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1203 if (children
.GetCount() > 0)
1205 return children
.Item(0);
1209 // Try a sibling of this or ancestor instead
1210 wxTreeItemId p
= item
;
1211 wxTreeItemId toFind
;
1214 toFind
= GetNextSibling(p
);
1215 p
= GetItemParent(p
);
1216 } while (p
.IsOk() && !toFind
.IsOk());
1221 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1223 wxTreeItemId id
= GetRootItem();
1232 } while (id
.IsOk());
1234 return wxTreeItemId();
1237 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1239 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1241 wxTreeItemId id
= item
;
1244 while (id
= GetNext(id
), id
.IsOk())
1250 return wxTreeItemId();
1253 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1255 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1257 wxFAIL_MSG(wxT("not implemented"));
1259 return wxTreeItemId();
1262 // called by wxTextTreeCtrl when it marks itself for deletion
1263 void wxGenericTreeCtrl::ResetTextControl()
1268 // find the first item starting with the given prefix after the given item
1269 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1270 const wxString
& prefixOrig
) const
1272 // match is case insensitive as this is more convenient to the user: having
1273 // to press Shift-letter to go to the item starting with a capital letter
1274 // would be too bothersome
1275 wxString prefix
= prefixOrig
.Lower();
1277 // determine the starting point: we shouldn't take the current item (this
1278 // allows to switch between two items starting with the same letter just by
1279 // pressing it) but we shouldn't jump to the next one if the user is
1280 // continuing to type as otherwise he might easily skip the item he wanted
1281 wxTreeItemId id
= idParent
;
1282 if ( prefix
.length() == 1 )
1287 // look for the item starting with the given prefix after it
1288 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1293 // if we haven't found anything...
1296 // ... wrap to the beginning
1298 if ( HasFlag(wxTR_HIDE_ROOT
) )
1300 // can't select virtual root
1304 // and try all the items (stop when we get to the one we started from)
1305 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1314 // -----------------------------------------------------------------------------
1316 // -----------------------------------------------------------------------------
1318 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1320 const wxString
& text
,
1321 int image
, int selImage
,
1322 wxTreeItemData
*data
)
1324 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1327 // should we give a warning here?
1328 return AddRoot(text
, image
, selImage
, data
);
1331 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1333 wxGenericTreeItem
*item
=
1334 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1338 data
->m_pItem
= item
;
1341 parent
->Insert( item
, previous
);
1346 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1347 int image
, int selImage
,
1348 wxTreeItemData
*data
)
1350 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1352 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1354 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1355 image
, selImage
, data
);
1358 data
->m_pItem
= m_anchor
;
1361 if (HasFlag(wxTR_HIDE_ROOT
))
1363 // if root is hidden, make sure we can navigate
1365 m_anchor
->SetHasPlus();
1367 CalculatePositions();
1370 if (!HasFlag(wxTR_MULTIPLE
))
1372 m_current
= m_key_current
= m_anchor
;
1373 m_current
->SetHilight( TRUE
);
1379 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1380 const wxString
& text
,
1381 int image
, int selImage
,
1382 wxTreeItemData
*data
)
1384 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1387 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1388 const wxTreeItemId
& idPrevious
,
1389 const wxString
& text
,
1390 int image
, int selImage
,
1391 wxTreeItemData
*data
)
1393 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1396 // should we give a warning here?
1397 return AddRoot(text
, image
, selImage
, data
);
1401 if (idPrevious
.IsOk())
1403 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1404 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1405 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1408 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1411 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1413 const wxString
& text
,
1414 int image
, int selImage
,
1415 wxTreeItemData
*data
)
1417 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1420 // should we give a warning here?
1421 return AddRoot(text
, image
, selImage
, data
);
1424 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1427 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1428 const wxString
& text
,
1429 int image
, int selImage
,
1430 wxTreeItemData
*data
)
1432 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1435 // should we give a warning here?
1436 return AddRoot(text
, image
, selImage
, data
);
1439 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1440 image
, selImage
, data
);
1443 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1445 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1446 event
.m_item
= item
;
1447 event
.SetEventObject( this );
1448 ProcessEvent( event
);
1451 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1453 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1455 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1456 item
->DeleteChildren(this);
1459 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1461 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1463 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1465 wxGenericTreeItem
*parent
= item
->GetParent();
1467 // don't keep stale pointers around!
1468 if ( IsDescendantOf(item
, m_key_current
) )
1470 // Don't silently change the selection:
1471 // do it properly in idle time, so event
1472 // handlers get called.
1474 // m_key_current = parent;
1475 m_key_current
= NULL
;
1478 // m_select_me records whether we need to select
1479 // a different item, in idle time.
1480 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1482 m_select_me
= parent
;
1485 if ( IsDescendantOf(item
, m_current
) )
1487 // Don't silently change the selection:
1488 // do it properly in idle time, so event
1489 // handlers get called.
1491 // m_current = parent;
1493 m_select_me
= parent
;
1496 // remove the item from the tree
1499 parent
->GetChildren().Remove( item
); // remove by value
1501 else // deleting the root
1503 // nothing will be left in the tree
1507 // and delete all of its children and the item itself now
1508 item
->DeleteChildren(this);
1509 SendDeleteEvent(item
);
1513 void wxGenericTreeCtrl::DeleteAllItems()
1521 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1523 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1525 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1526 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1527 _T("can't expand hidden root") );
1529 if ( !item
->HasPlus() )
1532 if ( item
->IsExpanded() )
1535 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1536 event
.m_item
= item
;
1537 event
.SetEventObject( this );
1539 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1541 // cancelled by program
1546 CalculatePositions();
1548 RefreshSubtree(item
);
1550 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1551 ProcessEvent( event
);
1554 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1556 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1559 if ( !IsExpanded(item
) )
1563 wxTreeItemIdValue cookie
;
1564 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1565 while ( child
.IsOk() )
1569 child
= GetNextChild(item
, cookie
);
1573 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1575 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1576 _T("can't collapse hidden root") );
1578 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1580 if ( !item
->IsExpanded() )
1583 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1584 event
.m_item
= item
;
1585 event
.SetEventObject( this );
1586 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1588 // cancelled by program
1594 #if 0 // TODO why should items be collapsed recursively?
1595 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1596 size_t count
= children
.Count();
1597 for ( size_t n
= 0; n
< count
; n
++ )
1599 Collapse(children
[n
]);
1603 CalculatePositions();
1605 RefreshSubtree(item
);
1607 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1608 ProcessEvent( event
);
1611 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1614 DeleteChildren(item
);
1617 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1619 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1621 if (item
->IsExpanded())
1627 void wxGenericTreeCtrl::Unselect()
1631 m_current
->SetHilight( FALSE
);
1632 RefreshLine( m_current
);
1639 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1641 if (item
->IsSelected())
1643 item
->SetHilight(FALSE
);
1647 if (item
->HasChildren())
1649 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1650 size_t count
= children
.Count();
1651 for ( size_t n
= 0; n
< count
; ++n
)
1653 UnselectAllChildren(children
[n
]);
1658 void wxGenericTreeCtrl::UnselectAll()
1660 wxTreeItemId rootItem
= GetRootItem();
1662 // the tree might not have the root item at all
1665 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1669 // Recursive function !
1670 // To stop we must have crt_item<last_item
1672 // Tag all next children, when no more children,
1673 // Move to parent (not to tag)
1674 // Keep going... if we found last_item, we stop.
1675 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1677 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1679 if (parent
== NULL
) // This is root item
1680 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1682 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1683 int index
= children
.Index(crt_item
);
1684 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1686 size_t count
= children
.Count();
1687 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1689 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1692 return TagNextChildren(parent
, last_item
, select
);
1695 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1697 crt_item
->SetHilight(select
);
1698 RefreshLine(crt_item
);
1700 if (crt_item
==last_item
)
1703 if (crt_item
->HasChildren())
1705 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1706 size_t count
= children
.Count();
1707 for ( size_t n
= 0; n
< count
; ++n
)
1709 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1717 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1721 // item2 is not necessary after item1
1722 // choice first' and 'last' between item1 and item2
1723 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1724 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1726 bool select
= m_current
->IsSelected();
1728 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1731 TagNextChildren(first
,last
,select
);
1734 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1735 bool unselect_others
,
1736 bool extended_select
)
1738 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1742 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1743 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1745 //wxCHECK_RET( ( (!unselect_others) && is_single),
1746 // wxT("this is a single selection tree") );
1748 // to keep going anyhow !!!
1751 if (item
->IsSelected())
1752 return; // nothing to do
1753 unselect_others
= TRUE
;
1754 extended_select
= FALSE
;
1756 else if ( unselect_others
&& item
->IsSelected() )
1758 // selection change if there is more than one item currently selected
1759 wxArrayTreeItemIds selected_items
;
1760 if ( GetSelections(selected_items
) == 1 )
1764 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1765 event
.m_item
= item
;
1766 event
.m_itemOld
= m_current
;
1767 event
.SetEventObject( this );
1768 // TODO : Here we don't send any selection mode yet !
1770 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1773 wxTreeItemId parent
= GetItemParent( itemId
);
1774 while (parent
.IsOk())
1776 if (!IsExpanded(parent
))
1779 parent
= GetItemParent( parent
);
1782 EnsureVisible( itemId
);
1785 if (unselect_others
)
1787 if (is_single
) Unselect(); // to speed up thing
1792 if (extended_select
)
1796 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1799 // don't change the mark (m_current)
1800 SelectItemRange(m_current
, item
);
1804 bool select
=TRUE
; // the default
1806 // Check if we need to toggle hilight (ctrl mode)
1807 if (!unselect_others
)
1808 select
=!item
->IsSelected();
1810 m_current
= m_key_current
= item
;
1811 m_current
->SetHilight(select
);
1812 RefreshLine( m_current
);
1815 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1816 GetEventHandler()->ProcessEvent( event
);
1819 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1823 DoSelectItem(itemId
);
1827 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1828 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1830 item
->SetHilight(FALSE
);
1835 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1836 wxArrayTreeItemIds
&array
) const
1838 if ( item
->IsSelected() )
1839 array
.Add(wxTreeItemId(item
));
1841 if ( item
->HasChildren() )
1843 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1844 size_t count
= children
.GetCount();
1845 for ( size_t n
= 0; n
< count
; ++n
)
1846 FillArray(children
[n
], array
);
1850 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1853 wxTreeItemId idRoot
= GetRootItem();
1854 if ( idRoot
.IsOk() )
1856 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1858 //else: the tree is empty, so no selections
1860 return array
.Count();
1863 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1865 if (!item
.IsOk()) return;
1867 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1869 // first expand all parent branches
1870 wxGenericTreeItem
*parent
= gitem
->GetParent();
1872 if ( HasFlag(wxTR_HIDE_ROOT
) )
1874 while ( parent
&& parent
!= m_anchor
)
1877 parent
= parent
->GetParent();
1885 parent
= parent
->GetParent();
1889 //if (parent) CalculatePositions();
1894 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1896 if (!item
.IsOk()) return;
1898 // We have to call this here because the label in
1899 // question might just have been added and no screen
1900 // update taken place.
1901 if (m_dirty
) wxYieldIfNeeded();
1903 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1905 // now scroll to the item
1906 int item_y
= gitem
->GetY();
1910 GetViewStart( &start_x
, &start_y
);
1911 start_y
*= PIXELS_PER_UNIT
;
1915 GetClientSize( &client_w
, &client_h
);
1917 if (item_y
< start_y
+3)
1922 m_anchor
->GetSize( x
, y
, this );
1923 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1924 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1925 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1926 // Item should appear at top
1927 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1929 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1934 m_anchor
->GetSize( x
, y
, this );
1935 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1936 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1937 item_y
+= PIXELS_PER_UNIT
+2;
1938 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1939 // Item should appear at bottom
1940 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
);
1944 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1945 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1947 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1948 wxGenericTreeItem
**item2
)
1950 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1952 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1955 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1956 const wxTreeItemId
& item2
)
1958 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1961 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1963 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1965 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1967 wxCHECK_RET( !s_treeBeingSorted
,
1968 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1970 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1971 if ( children
.Count() > 1 )
1975 s_treeBeingSorted
= this;
1976 children
.Sort(tree_ctrl_compare_func
);
1977 s_treeBeingSorted
= NULL
;
1979 //else: don't make the tree dirty as nothing changed
1982 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1984 return m_imageListNormal
;
1987 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1989 return m_imageListButtons
;
1992 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1994 return m_imageListState
;
1997 void wxGenericTreeCtrl::CalculateLineHeight()
1999 wxClientDC
dc(this);
2000 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2002 if ( m_imageListNormal
)
2004 // Calculate a m_lineHeight value from the normal Image sizes.
2005 // May be toggle off. Then wxGenericTreeCtrl will spread when
2006 // necessary (which might look ugly).
2007 int n
= m_imageListNormal
->GetImageCount();
2008 for (int i
= 0; i
< n
; i
++)
2010 int width
= 0, height
= 0;
2011 m_imageListNormal
->GetSize(i
, width
, height
);
2012 if (height
> m_lineHeight
) m_lineHeight
= height
;
2016 if (m_imageListButtons
)
2018 // Calculate a m_lineHeight value from the Button image sizes.
2019 // May be toggle off. Then wxGenericTreeCtrl will spread when
2020 // necessary (which might look ugly).
2021 int n
= m_imageListButtons
->GetImageCount();
2022 for (int i
= 0; i
< n
; i
++)
2024 int width
= 0, height
= 0;
2025 m_imageListButtons
->GetSize(i
, width
, height
);
2026 if (height
> m_lineHeight
) m_lineHeight
= height
;
2030 if (m_lineHeight
< 30)
2031 m_lineHeight
+= 2; // at least 2 pixels
2033 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2036 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2038 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2039 m_imageListNormal
= imageList
;
2040 m_ownsImageListNormal
= FALSE
;
2042 // Don't do any drawing if we're setting the list to NULL,
2043 // since we may be in the process of deleting the tree control.
2045 CalculateLineHeight();
2048 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2050 if (m_ownsImageListState
) delete m_imageListState
;
2051 m_imageListState
= imageList
;
2052 m_ownsImageListState
= FALSE
;
2055 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2057 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2058 m_imageListButtons
= imageList
;
2059 m_ownsImageListButtons
= FALSE
;
2061 CalculateLineHeight();
2064 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2066 SetImageList(imageList
);
2067 m_ownsImageListNormal
= TRUE
;
2070 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2072 SetStateImageList(imageList
);
2073 m_ownsImageListState
= TRUE
;
2076 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2078 SetButtonsImageList(imageList
);
2079 m_ownsImageListButtons
= TRUE
;
2082 // -----------------------------------------------------------------------------
2084 // -----------------------------------------------------------------------------
2086 void wxGenericTreeCtrl::AdjustMyScrollbars()
2091 m_anchor
->GetSize( x
, y
, this );
2092 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2093 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2094 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2095 int y_pos
= GetScrollPos( wxVERTICAL
);
2096 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2100 SetScrollbars( 0, 0, 0, 0 );
2104 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2106 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2107 return item
->GetHeight();
2109 return m_lineHeight
;
2112 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2114 // TODO implement "state" icon on items
2116 wxTreeItemAttr
*attr
= item
->GetAttributes();
2117 if ( attr
&& attr
->HasFont() )
2118 dc
.SetFont(attr
->GetFont());
2119 else if (item
->IsBold())
2120 dc
.SetFont(m_boldFont
);
2122 long text_w
= 0, text_h
= 0;
2123 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2125 int image_h
= 0, image_w
= 0;
2126 int image
= item
->GetCurrentImage();
2127 if ( image
!= NO_IMAGE
)
2129 if ( m_imageListNormal
)
2131 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2140 int total_h
= GetLineHeight(item
);
2142 if ( item
->IsSelected() )
2144 // under mac selections are only a rectangle in case they don't have the focus
2148 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2149 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2153 dc
.SetBrush( *m_hilightBrush
) ;
2156 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2162 if ( attr
&& attr
->HasBackgroundColour() )
2163 colBg
= attr
->GetBackgroundColour();
2165 colBg
= m_backgroundColour
;
2166 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2169 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2171 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2175 DoGetPosition(&x
, &y
);
2177 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2181 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2183 // If it's selected, and there's an image, then we should
2184 // take care to leave the area under the image painted in the
2185 // background colour.
2186 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2187 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2191 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2192 item
->GetWidth()+2, total_h
-offset
);
2196 if ( image
!= NO_IMAGE
)
2198 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2199 m_imageListNormal
->Draw( image
, dc
,
2201 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2202 wxIMAGELIST_DRAW_TRANSPARENT
);
2203 dc
.DestroyClippingRegion();
2206 dc
.SetBackgroundMode(wxTRANSPARENT
);
2207 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2208 dc
.DrawText( item
->GetText(),
2209 (wxCoord
)(image_w
+ item
->GetX()),
2210 (wxCoord
)(item
->GetY() + extraH
));
2212 // restore normal font
2213 dc
.SetFont( m_normalFont
);
2216 // Now y stands for the top of the item, whereas it used to stand for middle !
2217 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2219 int x
= level
*m_indent
;
2220 if (!HasFlag(wxTR_HIDE_ROOT
))
2224 else if (level
== 0)
2226 // always expand hidden root
2228 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2229 int count
= children
.Count();
2235 PaintLevel(children
[n
], dc
, 1, y
);
2236 } while (++n
< count
);
2238 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2240 // draw line down to last child
2241 origY
+= GetLineHeight(children
[0])>>1;
2242 oldY
+= GetLineHeight(children
[n
-1])>>1;
2243 dc
.DrawLine(3, origY
, 3, oldY
);
2249 item
->SetX(x
+m_spacing
);
2252 int h
= GetLineHeight(item
);
2254 int y_mid
= y_top
+ (h
>>1);
2257 int exposed_x
= dc
.LogicalToDeviceX(0);
2258 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2260 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2264 // don't draw rect outline if we already have the
2265 // background color under Mac
2266 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2267 #endif // !__WXMAC__
2271 if ( item
->IsSelected() )
2273 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2277 wxTreeItemAttr
*attr
= item
->GetAttributes();
2278 if (attr
&& attr
->HasTextColour())
2279 colText
= attr
->GetTextColour();
2281 colText
= GetForegroundColour();
2285 dc
.SetTextForeground(colText
);
2289 PaintItem(item
, dc
);
2291 if (HasFlag(wxTR_ROW_LINES
))
2293 // if the background colour is white, choose a
2294 // contrasting color for the lines
2295 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2296 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2297 dc
.DrawLine(0, y_top
, 10000, y_top
);
2298 dc
.DrawLine(0, y
, 10000, y
);
2301 // restore DC objects
2302 dc
.SetBrush(*wxWHITE_BRUSH
);
2303 dc
.SetPen(m_dottedPen
);
2304 dc
.SetTextForeground(*wxBLACK
);
2306 if ( !HasFlag(wxTR_NO_LINES
) )
2308 // draw the horizontal line here
2310 if (x
> (signed)m_indent
)
2311 x_start
-= m_indent
;
2312 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2314 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2317 // should the item show a button?
2318 if ( item
->HasPlus() && HasButtons() )
2320 if ( m_imageListButtons
)
2322 // draw the image button here
2325 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2326 : wxTreeItemIcon_Normal
;
2327 if ( item
->IsSelected() )
2328 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2330 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2331 int xx
= x
- image_w
/2;
2332 int yy
= y_mid
- image_h
/2;
2334 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2335 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2336 wxIMAGELIST_DRAW_TRANSPARENT
);
2338 else // no custom buttons
2340 static const int wImage
= 9;
2341 static const int hImage
= 9;
2344 if (item
->IsExpanded())
2345 flag
|= wxCONTROL_EXPANDED
;
2346 if (item
== m_underMouse
)
2347 flag
|= wxCONTROL_CURRENT
;
2349 wxRendererNative::Get().DrawTreeItemButton
2353 wxRect(x
- wImage
/2,
2362 if (item
->IsExpanded())
2364 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2365 int count
= children
.Count();
2372 PaintLevel(children
[n
], dc
, level
, y
);
2373 } while (++n
< count
);
2375 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2377 // draw line down to last child
2378 oldY
+= GetLineHeight(children
[n
-1])>>1;
2379 if (HasButtons()) y_mid
+= 5;
2381 // Only draw the portion of the line that is visible, in case it is huge
2382 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2383 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2384 yOrigin
= abs(yOrigin
);
2385 GetClientSize(&width
, &height
);
2387 // Move end points to the begining/end of the view?
2388 if (y_mid
< yOrigin
)
2390 if (oldY
> yOrigin
+ height
)
2391 oldY
= yOrigin
+ height
;
2393 // after the adjustments if y_mid is larger than oldY then the line
2394 // isn't visible at all so don't draw anything
2396 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2402 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2406 if ( item
->HasPlus() )
2408 // it's a folder, indicate it by a border
2413 // draw a line under the drop target because the item will be
2415 DrawLine(item
, TRUE
/* below */);
2418 SetCursor(wxCURSOR_BULLSEYE
);
2423 SetCursor(wxCURSOR_NO_ENTRY
);
2427 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2429 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2431 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2433 wxClientDC
dc(this);
2435 dc
.SetLogicalFunction(wxINVERT
);
2436 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2438 int w
= i
->GetWidth() + 2;
2439 int h
= GetLineHeight(i
) + 2;
2441 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2444 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2446 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2448 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2450 wxClientDC
dc(this);
2452 dc
.SetLogicalFunction(wxINVERT
);
2458 y
+= GetLineHeight(i
) - 1;
2461 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2464 // -----------------------------------------------------------------------------
2465 // wxWindows callbacks
2466 // -----------------------------------------------------------------------------
2468 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2476 dc
.SetFont( m_normalFont
);
2477 dc
.SetPen( m_dottedPen
);
2479 // this is now done dynamically
2480 //if(GetImageList() == NULL)
2481 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2484 PaintLevel( m_anchor
, dc
, 0, y
);
2487 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2496 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2505 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2507 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2508 te
.m_evtKey
= event
;
2509 te
.SetEventObject( this );
2510 if ( GetEventHandler()->ProcessEvent( te
) )
2512 // intercepted by the user code
2516 if ( (m_current
== 0) || (m_key_current
== 0) )
2522 // how should the selection work for this event?
2523 bool is_multiple
, extended_select
, unselect_others
;
2524 EventFlagsToSelType(GetWindowStyleFlag(),
2526 event
.ControlDown(),
2527 is_multiple
, extended_select
, unselect_others
);
2531 // * : Expand all/Collapse all
2532 // ' ' | return : activate
2533 // up : go up (not last children!)
2535 // left : go to parent
2536 // right : open if parent and go next
2537 // home : go to root
2538 // end : go to last item without opening parents
2539 // alnum : start or continue searching for the item with this prefix
2540 int keyCode
= event
.GetKeyCode();
2545 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2553 if ( !IsExpanded(m_current
) )
2556 ExpandAll(m_current
);
2559 //else: fall through to Collapse() it
2563 if (IsExpanded(m_current
))
2565 Collapse(m_current
);
2571 if ( !event
.HasModifiers() )
2573 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2574 event
.m_item
= m_current
;
2575 event
.SetEventObject( this );
2576 GetEventHandler()->ProcessEvent( event
);
2579 // in any case, also generate the normal key event for this key,
2580 // even if we generated the ACTIVATED event above: this is what
2581 // wxMSW does and it makes sense because you might not want to
2582 // process ACTIVATED event at all and handle Space and Return
2583 // directly (and differently) which would be impossible otherwise
2587 // up goes to the previous sibling or to the last
2588 // of its children if it's expanded
2591 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2594 prev
= GetItemParent( m_key_current
);
2595 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2597 break; // don't go to root if it is hidden
2601 wxTreeItemIdValue cookie
;
2602 wxTreeItemId current
= m_key_current
;
2603 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2604 if (current
== GetFirstChild( prev
, cookie
))
2606 // otherwise we return to where we came from
2607 DoSelectItem( prev
, unselect_others
, extended_select
);
2608 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2615 while ( IsExpanded(prev
) && HasChildren(prev
) )
2617 wxTreeItemId child
= GetLastChild(prev
);
2624 DoSelectItem( prev
, unselect_others
, extended_select
);
2625 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2630 // left arrow goes to the parent
2633 wxTreeItemId prev
= GetItemParent( m_current
);
2634 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2636 // don't go to root if it is hidden
2637 prev
= GetPrevSibling( m_current
);
2641 DoSelectItem( prev
, unselect_others
, extended_select
);
2647 // this works the same as the down arrow except that we
2648 // also expand the item if it wasn't expanded yet
2654 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2656 wxTreeItemIdValue cookie
;
2657 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2658 DoSelectItem( child
, unselect_others
, extended_select
);
2659 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2663 wxTreeItemId next
= GetNextSibling( m_key_current
);
2666 wxTreeItemId current
= m_key_current
;
2667 while (current
.IsOk() && !next
)
2669 current
= GetItemParent( current
);
2670 if (current
) next
= GetNextSibling( current
);
2675 DoSelectItem( next
, unselect_others
, extended_select
);
2676 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2682 // <End> selects the last visible tree item
2685 wxTreeItemId last
= GetRootItem();
2687 while ( last
.IsOk() && IsExpanded(last
) )
2689 wxTreeItemId lastChild
= GetLastChild(last
);
2691 // it may happen if the item was expanded but then all of
2692 // its children have been deleted - so IsExpanded() returned
2693 // TRUE, but GetLastChild() returned invalid item
2702 DoSelectItem( last
, unselect_others
, extended_select
);
2707 // <Home> selects the root item
2710 wxTreeItemId prev
= GetRootItem();
2714 if ( HasFlag(wxTR_HIDE_ROOT
) )
2716 wxTreeItemIdValue cookie
;
2717 prev
= GetFirstChild(prev
, cookie
);
2722 DoSelectItem( prev
, unselect_others
, extended_select
);
2727 // do not use wxIsalnum() here
2728 if ( !event
.HasModifiers() &&
2729 ((keyCode
>= '0' && keyCode
<= '9') ||
2730 (keyCode
>= 'a' && keyCode
<= 'z') ||
2731 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2733 // find the next item starting with the given prefix
2734 char ch
= (char)keyCode
;
2736 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2747 // also start the timer to reset the current prefix if the user
2748 // doesn't press any more alnum keys soon -- we wouldn't want
2749 // to use this prefix for a new item search
2752 m_findTimer
= new wxTreeFindTimer(this);
2755 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2764 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2766 // JACS: removed wxYieldIfNeeded() because it can cause the window
2767 // to be deleted from under us if a close window event is pending
2772 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2773 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2774 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2775 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2776 if (flags
) return wxTreeItemId();
2778 if (m_anchor
== NULL
)
2780 flags
= wxTREE_HITTEST_NOWHERE
;
2781 return wxTreeItemId();
2784 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2788 flags
= wxTREE_HITTEST_NOWHERE
;
2789 return wxTreeItemId();
2794 // get the bounding rectangle of the item (or of its label only)
2795 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2797 bool WXUNUSED(textOnly
)) const
2799 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2801 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2804 GetViewStart(& startX
, & startY
);
2806 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2807 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2808 rect
.width
= i
->GetWidth();
2809 //rect.height = i->GetHeight();
2810 rect
.height
= GetLineHeight(i
);
2815 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2817 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2819 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2821 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2822 te
.m_item
= itemEdit
;
2823 te
.SetEventObject( this );
2824 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2830 // We have to call this here because the label in
2831 // question might just have been added and no screen
2832 // update taken place.
2836 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2838 m_textCtrl
->SetFocus();
2841 // returns a pointer to the text edit control if the item is being
2842 // edited, NULL otherwise (it's assumed that no more than one item may
2843 // be edited simultaneously)
2844 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2849 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2850 const wxString
& value
)
2852 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2854 le
.SetEventObject( this );
2856 le
.m_editCancelled
= FALSE
;
2858 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2861 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2863 // let owner know that the edit was cancelled
2864 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2866 le
.SetEventObject( this );
2867 le
.m_label
= wxEmptyString
;
2868 le
.m_editCancelled
= TRUE
;
2870 GetEventHandler()->ProcessEvent( le
);
2876 void wxGenericTreeCtrl::OnRenameTimer()
2881 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2883 if ( !m_anchor
) return;
2885 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2887 // Is the mouse over a tree item button?
2889 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
2890 wxGenericTreeItem
*underMouse
= thisItem
;
2891 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
2894 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
2895 (!event
.LeftIsDown()) &&
2897 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2905 if (underMouse
!= m_underMouse
)
2909 // unhighlight old item
2910 wxGenericTreeItem
*tmp
= m_underMouse
;
2911 m_underMouse
= NULL
;
2915 m_underMouse
= underMouse
;
2917 RefreshLine( m_underMouse
);
2921 // Determines what item we are hovering over and need a tooltip for
2922 wxTreeItemId hoverItem
= thisItem
;
2924 // We do not want a tooltip if we are dragging, or if the rename timer is running
2925 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2927 // Ask the tree control what tooltip (if any) should be shown
2928 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
2929 hevent
.m_item
= hoverItem
;
2930 hevent
.SetEventObject(this);
2932 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
2934 SetToolTip(hevent
.m_label
);
2939 // we process left mouse up event (enables in-place edit), right down
2940 // (pass to the user code), left dbl click (activate item) and
2941 // dragging/moving events for items drag-and-drop
2942 if ( !(event
.LeftDown() ||
2944 event
.RightDown() ||
2945 event
.LeftDClick() ||
2947 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2956 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2958 if ( event
.Dragging() && !m_isDragging
)
2960 if (m_dragCount
== 0)
2965 if (m_dragCount
!= 3)
2967 // wait until user drags a bit further...
2971 wxEventType command
= event
.RightIsDown()
2972 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2973 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2975 wxTreeEvent
nevent( command
, GetId() );
2976 nevent
.m_item
= m_current
;
2977 nevent
.SetEventObject(this);
2979 // by default the dragging is not supported, the user code must
2980 // explicitly allow the event for it to take place
2983 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2985 // we're going to drag this item
2986 m_isDragging
= TRUE
;
2988 // remember the old cursor because we will change it while
2990 m_oldCursor
= m_cursor
;
2992 // in a single selection control, hide the selection temporarily
2993 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2995 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2997 if ( m_oldSelection
)
2999 m_oldSelection
->SetHilight(FALSE
);
3000 RefreshLine(m_oldSelection
);
3007 else if ( event
.Moving() )
3009 if ( item
!= m_dropTarget
)
3011 // unhighlight the previous drop target
3012 DrawDropEffect(m_dropTarget
);
3014 m_dropTarget
= item
;
3016 // highlight the current drop target if any
3017 DrawDropEffect(m_dropTarget
);
3022 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3024 // erase the highlighting
3025 DrawDropEffect(m_dropTarget
);
3027 if ( m_oldSelection
)
3029 m_oldSelection
->SetHilight(TRUE
);
3030 RefreshLine(m_oldSelection
);
3031 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3034 // generate the drag end event
3035 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3037 event
.m_item
= item
;
3038 event
.m_pointDrag
= pt
;
3039 event
.SetEventObject(this);
3041 (void)GetEventHandler()->ProcessEvent(event
);
3043 m_isDragging
= FALSE
;
3044 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3048 SetCursor(m_oldCursor
);
3054 // here we process only the messages which happen on tree items
3058 if (item
== NULL
) return; /* we hit the blank area */
3060 if ( event
.RightDown() )
3062 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3063 nevent
.m_item
= item
;
3064 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3065 nevent
.SetEventObject(this);
3066 GetEventHandler()->ProcessEvent(nevent
);
3068 else if ( event
.LeftUp() )
3070 // this facilitates multiple-item drag-and-drop
3072 if (item
&& HasFlag(wxTR_MULTIPLE
))
3074 wxArrayTreeItemIds selections
;
3075 size_t count
= GetSelections(selections
);
3078 !event
.ControlDown() &&
3081 DoSelectItem(item
, true, false);
3087 if ( (item
== m_current
) &&
3088 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3089 HasFlag(wxTR_EDIT_LABELS
) )
3091 if ( m_renameTimer
)
3093 if ( m_renameTimer
->IsRunning() )
3094 m_renameTimer
->Stop();
3098 m_renameTimer
= new wxTreeRenameTimer( this );
3101 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3104 m_lastOnSame
= FALSE
;
3107 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3109 if ( event
.LeftDown() )
3111 m_lastOnSame
= item
== m_current
;
3114 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3116 // only toggle the item for a single click, double click on
3117 // the button doesn't do anything (it toggles the item twice)
3118 if ( event
.LeftDown() )
3123 // don't select the item if the button was clicked
3128 // clear the previously selected items, if the
3129 // user clicked outside of the present selection.
3130 // otherwise, perform the deselection on mouse-up.
3131 // this allows multiple drag and drop to work.
3133 if (!IsSelected(item
))
3135 // how should the selection work for this event?
3136 bool is_multiple
, extended_select
, unselect_others
;
3137 EventFlagsToSelType(GetWindowStyleFlag(),
3139 event
.ControlDown(),
3140 is_multiple
, extended_select
, unselect_others
);
3142 DoSelectItem(item
, unselect_others
, extended_select
);
3146 // For some reason, Windows isn't recognizing a left double-click,
3147 // so we need to simulate it here. Allow 200 milliseconds for now.
3148 if ( event
.LeftDClick() )
3150 // double clicking should not start editing the item label
3151 if ( m_renameTimer
)
3152 m_renameTimer
->Stop();
3154 m_lastOnSame
= FALSE
;
3156 // send activate event first
3157 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3158 nevent
.m_item
= item
;
3159 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3160 nevent
.SetEventObject( this );
3161 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3163 // if the user code didn't process the activate event,
3164 // handle it ourselves by toggling the item when it is
3166 if ( item
->HasPlus() )
3176 void wxGenericTreeCtrl::OnInternalIdle()
3178 wxWindow::OnInternalIdle();
3180 // Check if we need to select the root item
3181 // because nothing else has been selected.
3182 // Delaying it means that we can invoke event handlers
3183 // as required, when a first item is selected.
3184 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3187 SelectItem(m_select_me
);
3188 else if (GetRootItem().IsOk())
3189 SelectItem(GetRootItem());
3192 /* after all changes have been done to the tree control,
3193 * we actually redraw the tree when everything is over */
3195 if (!m_dirty
) return;
3199 CalculatePositions();
3201 AdjustMyScrollbars();
3204 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3209 wxTreeItemAttr
*attr
= item
->GetAttributes();
3210 if ( attr
&& attr
->HasFont() )
3211 dc
.SetFont(attr
->GetFont());
3212 else if ( item
->IsBold() )
3213 dc
.SetFont(m_boldFont
);
3215 dc
.SetFont(m_normalFont
);
3217 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3220 // restore normal font
3221 dc
.SetFont( m_normalFont
);
3225 int image
= item
->GetCurrentImage();
3226 if ( image
!= NO_IMAGE
)
3228 if ( m_imageListNormal
)
3230 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3235 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3238 total_h
+= 2; // at least 2 pixels
3240 total_h
+= total_h
/10; // otherwise 10% extra spacing
3242 item
->SetHeight(total_h
);
3243 if (total_h
>m_lineHeight
)
3244 m_lineHeight
=total_h
;
3246 item
->SetWidth(image_w
+text_w
+2);
3249 // -----------------------------------------------------------------------------
3250 // for developper : y is now the top of the level
3251 // not the middle of it !
3252 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3254 int x
= level
*m_indent
;
3255 if (!HasFlag(wxTR_HIDE_ROOT
))
3259 else if (level
== 0)
3261 // a hidden root is not evaluated, but its
3262 // children are always calculated
3266 CalculateSize( item
, dc
);
3269 item
->SetX( x
+m_spacing
);
3271 y
+= GetLineHeight(item
);
3273 if ( !item
->IsExpanded() )
3275 // we don't need to calculate collapsed branches
3280 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3281 size_t n
, count
= children
.Count();
3283 for (n
= 0; n
< count
; ++n
)
3284 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3287 void wxGenericTreeCtrl::CalculatePositions()
3289 if ( !m_anchor
) return;
3291 wxClientDC
dc(this);
3294 dc
.SetFont( m_normalFont
);
3296 dc
.SetPen( m_dottedPen
);
3297 //if(GetImageList() == NULL)
3298 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3301 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3304 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3306 if (m_dirty
) return;
3308 wxSize client
= GetClientSize();
3311 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3312 rect
.width
= client
.x
;
3313 rect
.height
= client
.y
;
3315 Refresh(TRUE
, &rect
);
3317 AdjustMyScrollbars();
3320 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3322 if (m_dirty
) return;
3325 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3326 rect
.width
= GetClientSize().x
;
3327 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3329 Refresh(TRUE
, &rect
);
3332 void wxGenericTreeCtrl::RefreshSelected()
3334 // TODO: this is awfully inefficient, we should keep the list of all
3335 // selected items internally, should be much faster
3337 RefreshSelectedUnder(m_anchor
);
3340 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3342 if ( item
->IsSelected() )
3345 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3346 size_t count
= children
.GetCount();
3347 for ( size_t n
= 0; n
< count
; n
++ )
3349 RefreshSelectedUnder(children
[n
]);
3353 // ----------------------------------------------------------------------------
3354 // changing colours: we need to refresh the tree control
3355 // ----------------------------------------------------------------------------
3357 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3359 if ( !wxWindow::SetBackgroundColour(colour
) )
3367 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3369 if ( !wxWindow::SetForegroundColour(colour
) )
3377 // Process the tooltip event, to speed up event processing.
3378 // Doesn't actually get a tooltip.
3379 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3384 #endif // wxUSE_TREECTRL