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
);
48 //WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int NO_IMAGE
= -1;
56 #define PIXELS_PER_UNIT 10
58 // -----------------------------------------------------------------------------
60 // -----------------------------------------------------------------------------
62 // timer used for enabling in-place edit
63 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
66 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
71 wxGenericTreeCtrl
*m_owner
;
74 // control used for in-place edit
75 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
78 wxTreeTextCtrl( wxWindow
*parent
,
82 wxGenericTreeCtrl
*owner
,
83 const wxString
&value
= wxEmptyString
,
84 const wxPoint
&pos
= wxDefaultPosition
,
85 const wxSize
&size
= wxDefaultSize
,
86 int style
= wxSIMPLE_BORDER
,
87 const wxValidator
& validator
= wxDefaultValidator
,
88 const wxString
&name
= wxTextCtrlNameStr
);
90 void OnChar( wxKeyEvent
&event
);
91 void OnKeyUp( wxKeyEvent
&event
);
92 void OnKillFocus( wxFocusEvent
&event
);
97 wxGenericTreeCtrl
*m_owner
;
98 wxString m_startValue
;
100 DECLARE_EVENT_TABLE()
104 class WXDLLEXPORT wxGenericTreeItem
108 wxGenericTreeItem() { m_data
= NULL
; }
109 wxGenericTreeItem( wxGenericTreeItem
*parent
,
110 const wxString
& text
,
114 wxTreeItemData
*data
);
116 ~wxGenericTreeItem();
119 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
121 const wxString
& GetText() const { return m_text
; }
122 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
123 { return m_images
[which
]; }
124 wxTreeItemData
*GetData() const { return m_data
; }
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
130 void SetText( const wxString
&text
);
131 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
136 void SetBold(bool bold
) { m_isBold
= bold
; }
138 int GetX() const { return m_x
; }
139 int GetY() const { return m_y
; }
141 void SetX(int x
) { m_x
= x
; }
142 void SetY(int y
) { m_y
= y
; }
144 int GetHeight() const { return m_height
; }
145 int GetWidth() const { return m_width
; }
147 void SetHeight(int h
) { m_height
= h
; }
148 void SetWidth(int w
) { m_width
= w
; }
150 wxGenericTreeItem
*GetParent() const { return m_parent
; }
153 // deletes all children notifying the treectrl about it if !NULL
155 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
157 // get count of all children (and grand children if 'recursively')
158 size_t GetChildrenCount(bool recursively
= TRUE
) const;
160 void Insert(wxGenericTreeItem
*child
, size_t index
)
161 { m_children
.Insert(child
, index
); }
163 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
165 // return the item at given position (or NULL if no item), onButton is
166 // TRUE if the point belongs to the item's button, otherwise it lies
167 // on the button's label
168 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
169 const wxGenericTreeCtrl
*,
173 void Expand() { m_isCollapsed
= FALSE
; }
174 void Collapse() { m_isCollapsed
= TRUE
; }
176 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
179 bool HasChildren() const { return !m_children
.IsEmpty(); }
180 bool IsSelected() const { return m_hasHilight
!= 0; }
181 bool IsExpanded() const { return !m_isCollapsed
; }
182 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
183 bool IsBold() const { return m_isBold
!= 0; }
186 // get them - may be NULL
187 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
188 // get them ensuring that the pointer is not NULL
189 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
199 void SetAttributes(wxTreeItemAttr
*attr
)
201 if ( m_ownsAttr
) delete m_attr
;
205 // set them and delete when done
206 void AssignAttributes(wxTreeItemAttr
*attr
)
213 // since there can be very many of these, we save size by chosing
214 // the smallest representation for the elements and by ordering
215 // the members to avoid padding.
216 wxString m_text
; // label to be rendered for item
218 wxTreeItemData
*m_data
; // user-provided data
220 wxArrayGenericTreeItems m_children
; // list of children
221 wxGenericTreeItem
*m_parent
; // parent of this item
223 wxTreeItemAttr
*m_attr
; // attributes???
225 // tree ctrl images for the normal, selected, expanded and
226 // expanded+selected states
227 short m_images
[wxTreeItemIcon_Max
];
229 wxCoord m_x
; // (virtual) offset from top
230 short m_y
; // (virtual) offset from left
231 short m_width
; // width of this item
232 unsigned char m_height
; // height of this item
234 // use bitfields to save size
235 int m_isCollapsed
:1;
236 int m_hasHilight
:1; // same as focused
237 int m_hasPlus
:1; // used for item which doesn't have
238 // children but has a [+] button
239 int m_isBold
:1; // render the label in bold font
240 int m_ownsAttr
:1; // delete attribute when done
243 // =============================================================================
245 // =============================================================================
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // translate the key or mouse event flags to the type of selection we're
253 static void EventFlagsToSelType(long style
,
257 bool &extended_select
,
258 bool &unselect_others
)
260 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
261 extended_select
= shiftDown
&& is_multiple
;
262 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
265 // -----------------------------------------------------------------------------
266 // wxTreeRenameTimer (internal)
267 // -----------------------------------------------------------------------------
269 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
274 void wxTreeRenameTimer::Notify()
276 m_owner
->OnRenameTimer();
279 //-----------------------------------------------------------------------------
280 // wxTreeTextCtrl (internal)
281 //-----------------------------------------------------------------------------
283 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
284 EVT_CHAR (wxTreeTextCtrl::OnChar
)
285 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
286 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
289 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
293 wxGenericTreeCtrl
*owner
,
294 const wxString
&value
,
298 const wxValidator
& validator
,
299 const wxString
&name
)
300 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
306 (*m_res
) = wxEmptyString
;
307 m_startValue
= value
;
310 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
312 // TODO focus doesn't return to the wxTextCtrl when this closes...
313 if (event
.m_keyCode
== WXK_RETURN
)
316 (*m_res
) = GetValue();
318 if ((*m_accept
) && ((*m_res
) != m_startValue
))
319 m_owner
->OnRenameAccept();
321 if (!wxPendingDelete
.Member(this))
322 wxPendingDelete
.Append(this);
326 if (event
.m_keyCode
== WXK_ESCAPE
)
331 if (!wxPendingDelete
.Member(this))
332 wxPendingDelete
.Append(this);
339 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
341 // auto-grow the textctrl:
342 wxSize parentSize
= m_owner
->GetSize();
343 wxPoint myPos
= GetPosition();
344 wxSize mySize
= GetSize();
346 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
347 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
348 if (mySize
.x
> sx
) sx
= mySize
.x
;
354 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
356 if (!wxPendingDelete
.Member(this))
357 wxPendingDelete
.Append(this);
359 if ((*m_accept
) && ((*m_res
) != m_startValue
))
360 m_owner
->OnRenameAccept();
364 // -----------------------------------------------------------------------------
366 // -----------------------------------------------------------------------------
368 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
370 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
371 : wxNotifyEvent( commandType
, id
)
374 m_itemOld
= (wxGenericTreeItem
*)NULL
;
378 // -----------------------------------------------------------------------------
380 // -----------------------------------------------------------------------------
382 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
383 const wxString
& text
,
385 int image
, int selImage
,
386 wxTreeItemData
*data
)
389 m_images
[wxTreeItemIcon_Normal
] = image
;
390 m_images
[wxTreeItemIcon_Selected
] = selImage
;
391 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
392 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
397 m_isCollapsed
= TRUE
;
398 m_hasHilight
= FALSE
;
404 m_attr
= (wxTreeItemAttr
*)NULL
;
407 // We don't know the height here yet.
412 wxGenericTreeItem::~wxGenericTreeItem()
416 if (m_ownsAttr
) delete m_attr
;
418 wxASSERT_MSG( m_children
.IsEmpty(),
419 wxT("please call DeleteChildren() before deleting the item") );
422 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
424 size_t count
= m_children
.Count();
425 for ( size_t n
= 0; n
< count
; n
++ )
427 wxGenericTreeItem
*child
= m_children
[n
];
429 tree
->SendDeleteEvent(child
);
431 child
->DeleteChildren(tree
);
438 void wxGenericTreeItem::SetText( const wxString
&text
)
443 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
445 size_t count
= m_children
.Count();
449 size_t total
= count
;
450 for (size_t n
= 0; n
< count
; ++n
)
452 total
+= m_children
[n
]->GetChildrenCount();
458 void wxGenericTreeItem::GetSize( int &x
, int &y
,
459 const wxGenericTreeCtrl
*theButton
)
461 int bottomY
=m_y
+theButton
->GetLineHeight(this);
462 if ( y
< bottomY
) y
= bottomY
;
463 int width
= m_x
+ m_width
;
464 if ( x
< width
) x
= width
;
468 size_t count
= m_children
.Count();
469 for ( size_t n
= 0; n
< count
; ++n
)
471 m_children
[n
]->GetSize( x
, y
, theButton
);
476 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
477 const wxGenericTreeCtrl
*theCtrl
,
481 // for a hidden root node, don't evaluate it, but do evaluate children
482 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
485 int h
= theCtrl
->GetLineHeight(this);
486 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
488 int y_mid
= m_y
+ h
/2;
489 if (point
.y
< y_mid
)
490 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
492 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
494 // 5 is the size of the plus sign
495 int xCross
= m_x
- theCtrl
->GetSpacing();
496 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
497 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
498 HasPlus() && theCtrl
->HasButtons() )
500 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
504 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
509 // assuming every image (normal and selected) has the same size!
510 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
511 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
514 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
515 flags
|= wxTREE_HITTEST_ONITEMICON
;
517 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
523 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
524 if (point
.x
> m_x
+m_width
)
525 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
530 // if children are expanded, fall through to evaluate them
531 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
535 size_t count
= m_children
.Count();
536 for ( size_t n
= 0; n
< count
; n
++ )
538 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
546 return (wxGenericTreeItem
*) NULL
;
549 int wxGenericTreeItem::GetCurrentImage() const
551 int image
= NO_IMAGE
;
556 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
559 if ( image
== NO_IMAGE
)
561 // we usually fall back to the normal item, but try just the
562 // expanded one (and not selected) first in this case
563 image
= GetImage(wxTreeItemIcon_Expanded
);
569 image
= GetImage(wxTreeItemIcon_Selected
);
572 // maybe it doesn't have the specific image we want,
573 // try the default one instead
574 if ( image
== NO_IMAGE
) image
= GetImage();
579 // -----------------------------------------------------------------------------
580 // wxGenericTreeCtrl implementation
581 // -----------------------------------------------------------------------------
583 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
585 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
586 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
587 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
588 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
589 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
590 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
591 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
594 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
596 * wxTreeCtrl has to be a real class or we have problems with
597 * the run-time information.
600 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
603 // -----------------------------------------------------------------------------
604 // construction/destruction
605 // -----------------------------------------------------------------------------
607 void wxGenericTreeCtrl::Init()
609 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
617 m_hilightBrush
= new wxBrush
619 wxSystemSettings::GetSystemColour
621 wxSYS_COLOUR_HIGHLIGHT
626 m_hilightUnfocusedBrush
= new wxBrush
628 wxSystemSettings::GetSystemColour
630 wxSYS_COLOUR_BTNSHADOW
635 m_imageListNormal
= m_imageListButtons
=
636 m_imageListState
= (wxImageList
*) NULL
;
637 m_ownsImageListNormal
= m_ownsImageListButtons
=
638 m_ownsImageListState
= FALSE
;
641 m_isDragging
= FALSE
;
642 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
644 m_renameTimer
= new wxTreeRenameTimer( this );
645 m_lastOnSame
= FALSE
;
647 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
648 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
649 m_normalFont
.GetFamily(),
650 m_normalFont
.GetStyle(),
652 m_normalFont
.GetUnderlined());
655 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
660 const wxValidator
&validator
,
661 const wxString
& name
)
663 wxScrolledWindow::Create( parent
, id
, pos
, size
,
664 style
|wxHSCROLL
|wxVSCROLL
, name
);
666 // If the tree display has no buttons, but does have
667 // connecting lines, we can use a narrower layout.
668 // It may not be a good idea to force this...
669 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
676 SetValidator( validator
);
679 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
681 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
682 m_dottedPen
= wxPen( "grey", 0, 0 );
687 wxGenericTreeCtrl::~wxGenericTreeCtrl()
689 delete m_hilightBrush
;
690 delete m_hilightUnfocusedBrush
;
694 delete m_renameTimer
;
695 if (m_ownsImageListNormal
) delete m_imageListNormal
;
696 if (m_ownsImageListState
) delete m_imageListState
;
697 if (m_ownsImageListButtons
) delete m_imageListButtons
;
700 // -----------------------------------------------------------------------------
702 // -----------------------------------------------------------------------------
704 size_t wxGenericTreeCtrl::GetCount() const
706 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
709 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
715 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
721 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
723 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
725 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
728 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
730 // right now, just sets the styles. Eventually, we may
731 // want to update the inherited styles, but right now
732 // none of the parents has updatable styles
733 m_windowStyle
= styles
;
737 // -----------------------------------------------------------------------------
738 // functions to work with tree items
739 // -----------------------------------------------------------------------------
741 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
743 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
745 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
748 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
749 wxTreeItemIcon which
) const
751 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
753 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
756 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
758 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
760 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
763 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
765 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
768 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
769 pItem
->SetText(text
);
770 CalculateSize(pItem
, dc
);
774 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
776 wxTreeItemIcon which
)
778 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
780 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
781 pItem
->SetImage(image
, which
);
784 CalculateSize(pItem
, dc
);
788 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
790 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
792 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
795 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
797 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
799 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
800 pItem
->SetHasPlus(has
);
804 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
806 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
808 // avoid redrawing the tree if no real change
809 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
810 if ( pItem
->IsBold() != bold
)
812 pItem
->SetBold(bold
);
817 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
820 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
822 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
823 pItem
->Attr().SetTextColour(col
);
827 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
830 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
832 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
833 pItem
->Attr().SetBackgroundColour(col
);
837 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
839 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
841 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
842 pItem
->Attr().SetFont(font
);
846 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
848 wxScrolledWindow::SetFont(font
);
850 m_normalFont
= font
;
851 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
852 m_normalFont
.GetFamily(),
853 m_normalFont
.GetStyle(),
855 m_normalFont
.GetUnderlined());
861 // -----------------------------------------------------------------------------
862 // item status inquiries
863 // -----------------------------------------------------------------------------
865 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
867 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
869 // An item is only visible if it's not a descendant of a collapsed item
870 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
871 wxGenericTreeItem
* parent
= pItem
->GetParent();
874 if (!parent
->IsExpanded())
876 parent
= parent
->GetParent();
880 GetViewStart(& startX
, & startY
);
882 wxSize clientSize
= GetClientSize();
885 if (!GetBoundingRect(item
, rect
))
887 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
889 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
891 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
897 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
899 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
901 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
904 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
906 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
908 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
911 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
913 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
915 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
918 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
920 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
922 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
925 // -----------------------------------------------------------------------------
927 // -----------------------------------------------------------------------------
929 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
931 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
933 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
936 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
938 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
941 return GetNextChild(item
, cookie
);
944 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
946 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
948 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
949 if ( (size_t)cookie
< children
.Count() )
951 return children
.Item((size_t)cookie
++);
955 // there are no more of them
956 return wxTreeItemId();
960 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
962 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
964 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
965 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
968 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
970 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
972 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
973 wxGenericTreeItem
*parent
= i
->GetParent();
974 if ( parent
== NULL
)
976 // root item doesn't have any siblings
977 return wxTreeItemId();
980 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
981 int index
= siblings
.Index(i
);
982 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
984 size_t n
= (size_t)(index
+ 1);
985 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
988 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
990 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
992 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
993 wxGenericTreeItem
*parent
= i
->GetParent();
994 if ( parent
== NULL
)
996 // root item doesn't have any siblings
997 return wxTreeItemId();
1000 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1001 int index
= siblings
.Index(i
);
1002 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1004 return index
== 0 ? wxTreeItemId()
1005 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1008 // Only for internal use right now, but should probably be public
1009 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1011 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1013 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1015 // First see if there are any children.
1016 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1017 if (children
.GetCount() > 0)
1019 return children
.Item(0);
1023 // Try a sibling of this or ancestor instead
1024 wxTreeItemId p
= item
;
1025 wxTreeItemId toFind
;
1028 toFind
= GetNextSibling(p
);
1030 } while (p
.IsOk() && !toFind
.IsOk());
1035 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1037 wxTreeItemId id
= GetRootItem();
1046 } while (id
.IsOk());
1048 return wxTreeItemId();
1051 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1053 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1055 wxTreeItemId id
= item
;
1058 while (id
= GetNext(id
), id
.IsOk())
1064 return wxTreeItemId();
1067 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1069 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1071 wxFAIL_MSG(wxT("not implemented"));
1073 return wxTreeItemId();
1076 // -----------------------------------------------------------------------------
1078 // -----------------------------------------------------------------------------
1080 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1082 const wxString
& text
,
1083 int image
, int selImage
,
1084 wxTreeItemData
*data
)
1086 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1089 // should we give a warning here?
1090 return AddRoot(text
, image
, selImage
, data
);
1093 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1095 wxClientDC
dc(this);
1096 wxGenericTreeItem
*item
=
1097 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1101 data
->m_pItem
= (long) item
;
1104 parent
->Insert( item
, previous
);
1109 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1110 int image
, int selImage
,
1111 wxTreeItemData
*data
)
1113 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1115 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1117 wxClientDC
dc(this);
1118 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1119 image
, selImage
, data
);
1120 if (HasFlag(wxTR_HIDE_ROOT
))
1122 // if root is hidden, make sure we can navigate
1124 m_anchor
->SetHasPlus();
1129 data
->m_pItem
= (long) m_anchor
;
1132 if (!HasFlag(wxTR_MULTIPLE
))
1134 m_current
= m_key_current
= m_anchor
;
1135 m_current
->SetHilight( TRUE
);
1141 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1142 const wxString
& text
,
1143 int image
, int selImage
,
1144 wxTreeItemData
*data
)
1146 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1149 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1150 const wxTreeItemId
& idPrevious
,
1151 const wxString
& text
,
1152 int image
, int selImage
,
1153 wxTreeItemData
*data
)
1155 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1158 // should we give a warning here?
1159 return AddRoot(text
, image
, selImage
, data
);
1162 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1163 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1164 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1166 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1169 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1171 const wxString
& text
,
1172 int image
, int selImage
,
1173 wxTreeItemData
*data
)
1175 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1178 // should we give a warning here?
1179 return AddRoot(text
, image
, selImage
, data
);
1182 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1185 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1186 const wxString
& text
,
1187 int image
, int selImage
,
1188 wxTreeItemData
*data
)
1190 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1193 // should we give a warning here?
1194 return AddRoot(text
, image
, selImage
, data
);
1197 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1198 image
, selImage
, data
);
1201 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1203 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1204 event
.m_item
= (long) item
;
1205 event
.SetEventObject( this );
1206 ProcessEvent( event
);
1209 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1211 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1213 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1214 item
->DeleteChildren(this);
1217 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1219 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1221 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1223 // don't stay with invalid m_key_current or we will crash in
1224 // the next call to OnChar()
1225 bool changeKeyCurrent
= FALSE
;
1226 wxGenericTreeItem
*itemKey
= m_key_current
;
1229 if ( itemKey
== item
)
1231 // m_key_current is a descendant of the item being deleted
1232 changeKeyCurrent
= TRUE
;
1235 itemKey
= itemKey
->GetParent();
1238 wxGenericTreeItem
*parent
= item
->GetParent();
1241 parent
->GetChildren().Remove( item
); // remove by value
1244 if ( changeKeyCurrent
)
1246 // may be NULL or not
1247 m_key_current
= parent
;
1250 item
->DeleteChildren(this);
1251 SendDeleteEvent(item
);
1255 void wxGenericTreeCtrl::DeleteAllItems()
1261 m_anchor
->DeleteChildren(this);
1268 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1270 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1272 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1274 if ( !item
->HasPlus() )
1277 if ( item
->IsExpanded() )
1280 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1281 event
.m_item
= (long) item
;
1282 event
.SetEventObject( this );
1284 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1286 // cancelled by program
1291 CalculatePositions();
1293 RefreshSubtree(item
);
1295 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1296 ProcessEvent( event
);
1299 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1302 if ( IsExpanded(item
) )
1305 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1306 while ( child
.IsOk() )
1310 child
= GetNextChild(item
, cookie
);
1315 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1317 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1319 if ( !item
->IsExpanded() )
1322 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1323 event
.m_item
= (long) item
;
1324 event
.SetEventObject( this );
1325 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1327 // cancelled by program
1333 #if 0 // TODO why should items be collapsed recursively?
1334 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1335 size_t count
= children
.Count();
1336 for ( size_t n
= 0; n
< count
; n
++ )
1338 Collapse(children
[n
]);
1342 CalculatePositions();
1344 RefreshSubtree(item
);
1346 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1347 ProcessEvent( event
);
1350 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1353 DeleteChildren(item
);
1356 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1358 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1360 if (item
->IsExpanded())
1366 void wxGenericTreeCtrl::Unselect()
1370 m_current
->SetHilight( FALSE
);
1371 RefreshLine( m_current
);
1375 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1377 if (item
->IsSelected())
1379 item
->SetHilight(FALSE
);
1383 if (item
->HasChildren())
1385 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1386 size_t count
= children
.Count();
1387 for ( size_t n
= 0; n
< count
; ++n
)
1389 UnselectAllChildren(children
[n
]);
1394 void wxGenericTreeCtrl::UnselectAll()
1396 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1399 // Recursive function !
1400 // To stop we must have crt_item<last_item
1402 // Tag all next children, when no more children,
1403 // Move to parent (not to tag)
1404 // Keep going... if we found last_item, we stop.
1405 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1407 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1409 if (parent
== NULL
) // This is root item
1410 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1412 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1413 int index
= children
.Index(crt_item
);
1414 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1416 size_t count
= children
.Count();
1417 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1419 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1422 return TagNextChildren(parent
, last_item
, select
);
1425 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1427 crt_item
->SetHilight(select
);
1428 RefreshLine(crt_item
);
1430 if (crt_item
==last_item
)
1433 if (crt_item
->HasChildren())
1435 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1436 size_t count
= children
.Count();
1437 for ( size_t n
= 0; n
< count
; ++n
)
1439 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1447 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1449 // item2 is not necessary after item1
1450 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1452 // choice first' and 'last' between item1 and item2
1453 if (item1
->GetY()<item2
->GetY())
1464 bool select
= m_current
->IsSelected();
1466 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1469 TagNextChildren(first
,last
,select
);
1472 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1473 bool unselect_others
,
1474 bool extended_select
)
1476 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1478 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1479 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1481 //wxCHECK_RET( ( (!unselect_others) && is_single),
1482 // wxT("this is a single selection tree") );
1484 // to keep going anyhow !!!
1487 if (item
->IsSelected())
1488 return; // nothing to do
1489 unselect_others
= TRUE
;
1490 extended_select
= FALSE
;
1492 else if ( unselect_others
&& item
->IsSelected() )
1494 // selection change if there is more than one item currently selected
1495 wxArrayTreeItemIds selected_items
;
1496 if ( GetSelections(selected_items
) == 1 )
1500 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1501 event
.m_item
= (long) item
;
1502 event
.m_itemOld
= (long) m_current
;
1503 event
.SetEventObject( this );
1504 // TODO : Here we don't send any selection mode yet !
1506 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1509 wxTreeItemId parent
= GetParent( itemId
);
1510 while (parent
.IsOk())
1512 if (!IsExpanded(parent
))
1515 parent
= GetParent( parent
);
1518 EnsureVisible( itemId
);
1521 if (unselect_others
)
1523 if (is_single
) Unselect(); // to speed up thing
1528 if (extended_select
)
1532 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1535 // don't change the mark (m_current)
1536 SelectItemRange(m_current
, item
);
1540 bool select
=TRUE
; // the default
1542 // Check if we need to toggle hilight (ctrl mode)
1543 if (!unselect_others
)
1544 select
=!item
->IsSelected();
1546 m_current
= m_key_current
= item
;
1547 m_current
->SetHilight(select
);
1548 RefreshLine( m_current
);
1551 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1552 GetEventHandler()->ProcessEvent( event
);
1555 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1556 wxArrayTreeItemIds
&array
) const
1558 if ( item
->IsSelected() )
1559 array
.Add(wxTreeItemId(item
));
1561 if ( item
->HasChildren() )
1563 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1564 size_t count
= children
.GetCount();
1565 for ( size_t n
= 0; n
< count
; ++n
)
1566 FillArray(children
[n
], array
);
1570 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1573 wxTreeItemId idRoot
= GetRootItem();
1574 if ( idRoot
.IsOk() )
1576 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1578 //else: the tree is empty, so no selections
1580 return array
.Count();
1583 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1585 if (!item
.IsOk()) return;
1587 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1589 // first expand all parent branches
1590 wxGenericTreeItem
*parent
= gitem
->GetParent();
1594 parent
= parent
->GetParent();
1597 //if (parent) CalculatePositions();
1602 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1604 if (!item
.IsOk()) return;
1606 // We have to call this here because the label in
1607 // question might just have been added and no screen
1608 // update taken place.
1609 if (m_dirty
) wxYieldIfNeeded();
1611 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1613 // now scroll to the item
1614 int item_y
= gitem
->GetY();
1618 GetViewStart( &start_x
, &start_y
);
1619 start_y
*= PIXELS_PER_UNIT
;
1623 GetClientSize( &client_w
, &client_h
);
1625 if (item_y
< start_y
+3)
1630 m_anchor
->GetSize( x
, y
, this );
1631 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1632 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1633 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1634 // Item should appear at top
1635 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1637 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1642 m_anchor
->GetSize( x
, y
, this );
1643 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1644 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1645 item_y
+= PIXELS_PER_UNIT
+2;
1646 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1647 // Item should appear at bottom
1648 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
);
1652 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1653 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1655 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1656 wxGenericTreeItem
**item2
)
1658 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1660 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1663 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1664 const wxTreeItemId
& item2
)
1666 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1669 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1671 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1673 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1675 wxCHECK_RET( !s_treeBeingSorted
,
1676 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1678 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1679 if ( children
.Count() > 1 )
1683 s_treeBeingSorted
= this;
1684 children
.Sort(tree_ctrl_compare_func
);
1685 s_treeBeingSorted
= NULL
;
1687 //else: don't make the tree dirty as nothing changed
1690 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1692 return m_imageListNormal
;
1695 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1697 return m_imageListButtons
;
1700 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1702 return m_imageListState
;
1705 void wxGenericTreeCtrl::CalculateLineHeight()
1707 wxClientDC
dc(this);
1708 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1710 if ( m_imageListNormal
)
1712 // Calculate a m_lineHeight value from the normal Image sizes.
1713 // May be toggle off. Then wxGenericTreeCtrl will spread when
1714 // necessary (which might look ugly).
1715 int n
= m_imageListNormal
->GetImageCount();
1716 for (int i
= 0; i
< n
; i
++)
1718 int width
= 0, height
= 0;
1719 m_imageListNormal
->GetSize(i
, width
, height
);
1720 if (height
> m_lineHeight
) m_lineHeight
= height
;
1724 if (m_imageListButtons
)
1726 // Calculate a m_lineHeight value from the Button image sizes.
1727 // May be toggle off. Then wxGenericTreeCtrl will spread when
1728 // necessary (which might look ugly).
1729 int n
= m_imageListButtons
->GetImageCount();
1730 for (int i
= 0; i
< n
; i
++)
1732 int width
= 0, height
= 0;
1733 m_imageListButtons
->GetSize(i
, width
, height
);
1734 if (height
> m_lineHeight
) m_lineHeight
= height
;
1738 if (m_lineHeight
< 30)
1739 m_lineHeight
+= 2; // at least 2 pixels
1741 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1744 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1746 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1747 m_imageListNormal
= imageList
;
1748 m_ownsImageListNormal
= FALSE
;
1750 CalculateLineHeight();
1753 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1755 if (m_ownsImageListState
) delete m_imageListState
;
1756 m_imageListState
= imageList
;
1757 m_ownsImageListState
= FALSE
;
1760 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1762 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1763 m_imageListButtons
= imageList
;
1764 m_ownsImageListButtons
= FALSE
;
1766 CalculateLineHeight();
1769 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1771 SetImageList(imageList
);
1772 m_ownsImageListNormal
= TRUE
;
1775 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1777 SetStateImageList(imageList
);
1778 m_ownsImageListState
= TRUE
;
1781 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1783 SetButtonsImageList(imageList
);
1784 m_ownsImageListButtons
= TRUE
;
1787 // -----------------------------------------------------------------------------
1789 // -----------------------------------------------------------------------------
1791 void wxGenericTreeCtrl::AdjustMyScrollbars()
1796 m_anchor
->GetSize( x
, y
, this );
1797 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1798 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1799 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1800 int y_pos
= GetScrollPos( wxVERTICAL
);
1801 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1805 SetScrollbars( 0, 0, 0, 0 );
1809 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1811 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1812 return item
->GetHeight();
1814 return m_lineHeight
;
1817 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1819 // TODO implement "state" icon on items
1821 wxTreeItemAttr
*attr
= item
->GetAttributes();
1822 if ( attr
&& attr
->HasFont() )
1823 dc
.SetFont(attr
->GetFont());
1824 else if (item
->IsBold())
1825 dc
.SetFont(m_boldFont
);
1827 long text_w
= 0, text_h
= 0;
1828 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1830 int image_h
= 0, image_w
= 0;
1831 int image
= item
->GetCurrentImage();
1832 if ( image
!= NO_IMAGE
)
1834 if ( m_imageListNormal
)
1836 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1845 int total_h
= GetLineHeight(item
);
1847 if ( item
->IsSelected() )
1849 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1854 if ( attr
&& attr
->HasBackgroundColour() )
1855 colBg
= attr
->GetBackgroundColour();
1857 colBg
= m_backgroundColour
;
1858 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1861 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1863 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1865 // If it's selected, and there's an image, then we should
1866 // take care to leave the area under the image painted in the
1867 // background colour.
1868 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1869 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1873 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1874 item
->GetWidth()+2, total_h
-offset
);
1877 if ( image
!= NO_IMAGE
)
1879 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1880 m_imageListNormal
->Draw( image
, dc
,
1882 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1883 wxIMAGELIST_DRAW_TRANSPARENT
);
1884 dc
.DestroyClippingRegion();
1887 dc
.SetBackgroundMode(wxTRANSPARENT
);
1888 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1889 dc
.DrawText( item
->GetText(),
1890 (wxCoord
)(image_w
+ item
->GetX()),
1891 (wxCoord
)(item
->GetY() + extraH
));
1893 // restore normal font
1894 dc
.SetFont( m_normalFont
);
1897 // Now y stands for the top of the item, whereas it used to stand for middle !
1898 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1900 int x
= level
*m_indent
;
1901 if (!HasFlag(wxTR_HIDE_ROOT
))
1905 else if (level
== 0)
1907 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1908 size_t n
, count
= children
.Count();
1909 for (n
= 0; n
< count
; ++n
)
1910 PaintLevel(children
[n
], dc
, 1, y
);
1914 item
->SetX(x
+m_spacing
);
1917 int h
= GetLineHeight(item
);
1919 int y_mid
= y_top
+ (h
>>1);
1922 int exposed_x
= dc
.LogicalToDeviceX(0);
1923 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1925 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1927 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1929 if (!HasFlag(wxTR_NO_LINES
))
1931 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
1932 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1933 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1936 if (m_imageListButtons
!= NULL
)
1938 // draw the image button here
1939 int image_h
= 0, image_w
= 0, image
= wxCLOSED_BUTTON
;
1940 if (item
->IsExpanded()) image
= wxOPEN_BUTTON
;
1941 if (item
->IsSelected())
1942 image
+= wxOPEN_BUTTON_SELECTED
- wxOPEN_BUTTON
;
1943 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1944 int xx
= x
- (image_w
>>1);
1945 int yy
= y_mid
- (image_h
>>1);
1946 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1947 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1948 wxIMAGELIST_DRAW_TRANSPARENT
);
1949 dc
.DestroyClippingRegion();
1951 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1953 // draw the twisty button here
1954 dc
.SetPen(*wxBLACK_PEN
);
1955 dc
.SetBrush(*m_hilightBrush
);
1959 if (item
->IsExpanded())
1962 button
[0].y
= y_mid
-2;
1964 button
[1].y
= y_mid
-2;
1966 button
[2].y
= y_mid
+3;
1970 button
[0].y
= y_mid
-5;
1972 button
[1].y
= y_mid
+5;
1974 button
[2].y
= y_mid
;
1977 dc
.DrawPolygon(3, button
);
1979 dc
.SetPen(m_dottedPen
);
1981 else // if (HasFlag(wxTR_HAS_BUTTONS))
1983 // draw the plus sign here
1984 dc
.SetPen(*wxGREY_PEN
);
1985 dc
.SetBrush(*wxWHITE_BRUSH
);
1986 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
1987 dc
.SetPen(*wxBLACK_PEN
);
1988 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
1989 if (!item
->IsExpanded())
1990 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
1991 dc
.SetPen(m_dottedPen
);
1994 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
1996 // draw the horizontal line here
1998 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
1999 x_start
-= m_indent
;
2000 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2005 // don't draw rect outline if we already have the
2006 // background color under Mac
2007 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2008 #endif // !__WXMAC__
2012 if ( item
->IsSelected() )
2014 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2018 wxTreeItemAttr
*attr
= item
->GetAttributes();
2019 if (attr
&& attr
->HasTextColour())
2020 colText
= attr
->GetTextColour();
2022 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2026 dc
.SetTextForeground(colText
);
2030 PaintItem(item
, dc
);
2032 if (HasFlag(wxTR_ROW_LINES
))
2034 dc
.SetPen(*wxWHITE_PEN
);
2035 dc
.DrawLine(0, y_top
, 10000, y_top
);
2036 dc
.DrawLine(0, y
, 10000, y
);
2039 // restore DC objects
2040 dc
.SetBrush(*wxWHITE_BRUSH
);
2041 dc
.SetPen(m_dottedPen
);
2042 dc
.SetTextForeground(*wxBLACK
);
2045 if (item
->IsExpanded())
2047 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2048 int count
= children
.Count();
2055 PaintLevel(children
[n
], dc
, level
, y
);
2056 } while (++n
< count
);
2058 if (!HasFlag(wxTR_NO_LINES
))
2060 // draw line down to last child
2061 oldY
+= GetLineHeight(children
[n
-1])/2;
2062 if (HasButtons()) y_mid
+= 5;
2063 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2069 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2073 if ( item
->HasPlus() )
2075 // it's a folder, indicate it by a border
2080 // draw a line under the drop target because the item will be
2082 DrawLine(item
, TRUE
/* below */);
2085 SetCursor(wxCURSOR_BULLSEYE
);
2090 SetCursor(wxCURSOR_NO_ENTRY
);
2094 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2096 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2098 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2100 wxClientDC
dc(this);
2102 dc
.SetLogicalFunction(wxINVERT
);
2103 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2105 int w
= i
->GetWidth() + 2;
2106 int h
= GetLineHeight(i
) + 2;
2108 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2111 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2113 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2115 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2117 wxClientDC
dc(this);
2119 dc
.SetLogicalFunction(wxINVERT
);
2125 y
+= GetLineHeight(i
) - 1;
2128 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2131 // -----------------------------------------------------------------------------
2132 // wxWindows callbacks
2133 // -----------------------------------------------------------------------------
2135 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2143 dc
.SetFont( m_normalFont
);
2144 dc
.SetPen( m_dottedPen
);
2146 // this is now done dynamically
2147 //if(GetImageList() == NULL)
2148 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2151 PaintLevel( m_anchor
, dc
, 0, y
);
2154 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2163 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2172 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2174 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2175 te
.m_code
= (int)event
.KeyCode();
2176 te
.SetEventObject( this );
2177 if ( GetEventHandler()->ProcessEvent( te
) )
2179 // intercepted by the user code
2183 if ( (m_current
== 0) || (m_key_current
== 0) )
2189 // how should the selection work for this event?
2190 bool is_multiple
, extended_select
, unselect_others
;
2191 EventFlagsToSelType(GetWindowStyleFlag(),
2193 event
.ControlDown(),
2194 is_multiple
, extended_select
, unselect_others
);
2198 // * : Expand all/Collapse all
2199 // ' ' | return : activate
2200 // up : go up (not last children!)
2202 // left : go to parent
2203 // right : open if parent and go next
2204 // home : go to root
2205 // end : go to last item without opening parents
2206 switch (event
.KeyCode())
2210 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2218 if ( !IsExpanded(m_current
) )
2221 ExpandAll(m_current
);
2224 //else: fall through to Collapse() it
2228 if (IsExpanded(m_current
))
2230 Collapse(m_current
);
2237 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2238 event
.m_item
= (long) m_current
;
2240 event
.SetEventObject( this );
2241 GetEventHandler()->ProcessEvent( event
);
2245 // up goes to the previous sibling or to the last
2246 // of its children if it's expanded
2249 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2252 prev
= GetParent( m_key_current
);
2253 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2255 break; // don't go to root if it is hidden
2260 wxTreeItemId current
= m_key_current
;
2261 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2262 if (current
== GetFirstChild( prev
, cookie
))
2264 // otherwise we return to where we came from
2265 SelectItem( prev
, unselect_others
, extended_select
);
2266 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2267 EnsureVisible( prev
);
2274 while ( IsExpanded(prev
) && HasChildren(prev
) )
2276 wxTreeItemId child
= GetLastChild(prev
);
2283 SelectItem( prev
, unselect_others
, extended_select
);
2284 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2285 EnsureVisible( prev
);
2290 // left arrow goes to the parent
2293 wxTreeItemId prev
= GetParent( m_current
);
2294 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2296 // don't go to root if it is hidden
2297 prev
= GetPrevSibling( m_current
);
2301 EnsureVisible( prev
);
2302 SelectItem( prev
, unselect_others
, extended_select
);
2308 // this works the same as the down arrow except that we
2309 // also expand the item if it wasn't expanded yet
2315 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2318 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2319 SelectItem( child
, unselect_others
, extended_select
);
2320 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2321 EnsureVisible( child
);
2325 wxTreeItemId next
= GetNextSibling( m_key_current
);
2328 wxTreeItemId current
= m_key_current
;
2329 while (current
&& !next
)
2331 current
= GetParent( current
);
2332 if (current
) next
= GetNextSibling( current
);
2337 SelectItem( next
, unselect_others
, extended_select
);
2338 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2339 EnsureVisible( next
);
2345 // <End> selects the last visible tree item
2348 wxTreeItemId last
= GetRootItem();
2350 while ( last
.IsOk() && IsExpanded(last
) )
2352 wxTreeItemId lastChild
= GetLastChild(last
);
2354 // it may happen if the item was expanded but then all of
2355 // its children have been deleted - so IsExpanded() returned
2356 // TRUE, but GetLastChild() returned invalid item
2365 EnsureVisible( last
);
2366 SelectItem( last
, unselect_others
, extended_select
);
2371 // <Home> selects the root item
2374 wxTreeItemId prev
= GetRootItem();
2376 if (HasFlag(wxTR_HIDE_ROOT
))
2379 prev
= GetFirstChild(prev
, dummy
);
2382 EnsureVisible( prev
);
2383 SelectItem( prev
, unselect_others
, extended_select
);
2392 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2394 // JACS: removed wxYieldIfNeeded() because it can cause the window
2395 // to be deleted from under us if a close window event is pending
2400 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2401 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2402 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2403 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2404 if (flags
) return wxTreeItemId();
2406 if (m_anchor
== NULL
)
2408 flags
= wxTREE_HITTEST_NOWHERE
;
2409 return wxTreeItemId();
2412 wxClientDC
dc(this);
2414 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2415 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2416 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2422 flags
= wxTREE_HITTEST_NOWHERE
;
2423 return wxTreeItemId();
2428 // get the bounding rectangle of the item (or of its label only)
2429 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2431 bool WXUNUSED(textOnly
)) const
2433 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2435 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2438 GetViewStart(& startX
, & startY
);
2440 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2441 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2442 rect
.width
= i
->GetWidth();
2443 //rect.height = i->GetHeight();
2444 rect
.height
= GetLineHeight(i
);
2451 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2453 if (!item
.IsOk()) return;
2455 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2457 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2458 te
.m_item
= (long) m_currentEdit
;
2459 te
.SetEventObject( this );
2460 GetEventHandler()->ProcessEvent( te
);
2462 if (!te
.IsAllowed()) return;
2464 // We have to call this here because the label in
2465 // question might just have been added and no screen
2466 // update taken place.
2467 if (m_dirty
) wxYieldIfNeeded();
2469 wxString s
= m_currentEdit
->GetText();
2470 int x
= m_currentEdit
->GetX();
2471 int y
= m_currentEdit
->GetY();
2472 int w
= m_currentEdit
->GetWidth();
2473 int h
= m_currentEdit
->GetHeight();
2478 int image
= m_currentEdit
->GetCurrentImage();
2479 if ( image
!= NO_IMAGE
)
2481 if ( m_imageListNormal
)
2483 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2488 wxFAIL_MSG(_T("you must create an image list to use images!"));
2492 w
-= image_w
+ 4; // I don't know why +4 is needed
2494 wxClientDC
dc(this);
2496 x
= dc
.LogicalToDeviceX( x
);
2497 y
= dc
.LogicalToDeviceY( y
);
2499 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2509 void wxGenericTreeCtrl::OnRenameTimer()
2514 void wxGenericTreeCtrl::OnRenameAccept()
2516 // TODO if the validator fails this causes a crash
2517 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2518 le
.m_item
= (long) m_currentEdit
;
2519 le
.SetEventObject( this );
2520 le
.m_label
= m_renameRes
;
2521 GetEventHandler()->ProcessEvent( le
);
2523 if (!le
.IsAllowed()) return;
2525 SetItemText( m_currentEdit
, m_renameRes
);
2528 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2530 if ( !m_anchor
) return;
2532 // we process left mouse up event (enables in-place edit), right down
2533 // (pass to the user code), left dbl click (activate item) and
2534 // dragging/moving events for items drag-and-drop
2535 if ( !(event
.LeftDown() ||
2537 event
.RightDown() ||
2538 event
.LeftDClick() ||
2540 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2547 wxClientDC
dc(this);
2549 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2550 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2553 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2558 if ( event
.Dragging() && !m_isDragging
)
2560 if (m_dragCount
== 0)
2561 m_dragStart
= wxPoint(x
,y
);
2565 if (m_dragCount
!= 3)
2567 // wait until user drags a bit further...
2571 wxEventType command
= event
.RightIsDown()
2572 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2573 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2575 wxTreeEvent
nevent( command
, GetId() );
2576 nevent
.m_item
= (long) m_current
;
2577 nevent
.SetEventObject(this);
2579 // by default the dragging is not supported, the user code must
2580 // explicitly allow the event for it to take place
2583 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2585 // we're going to drag this item
2586 m_isDragging
= TRUE
;
2588 // remember the old cursor because we will change it while
2590 m_oldCursor
= m_cursor
;
2592 // in a single selection control, hide the selection temporarily
2593 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2595 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2597 if ( m_oldSelection
)
2599 m_oldSelection
->SetHilight(FALSE
);
2600 RefreshLine(m_oldSelection
);
2607 else if ( event
.Moving() )
2609 if ( item
!= m_dropTarget
)
2611 // unhighlight the previous drop target
2612 DrawDropEffect(m_dropTarget
);
2614 m_dropTarget
= item
;
2616 // highlight the current drop target if any
2617 DrawDropEffect(m_dropTarget
);
2622 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2624 // erase the highlighting
2625 DrawDropEffect(m_dropTarget
);
2627 if ( m_oldSelection
)
2629 m_oldSelection
->SetHilight(TRUE
);
2630 RefreshLine(m_oldSelection
);
2631 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2634 // generate the drag end event
2635 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2637 event
.m_item
= (long) item
;
2638 event
.m_pointDrag
= wxPoint(x
, y
);
2639 event
.SetEventObject(this);
2641 (void)GetEventHandler()->ProcessEvent(event
);
2643 m_isDragging
= FALSE
;
2644 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2648 SetCursor(m_oldCursor
);
2654 // here we process only the messages which happen on tree items
2658 if (item
== NULL
) return; /* we hit the blank area */
2660 if ( event
.RightDown() )
2662 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2663 nevent
.m_item
= (long) item
;
2665 CalcScrolledPosition(x
, y
,
2666 &nevent
.m_pointDrag
.x
,
2667 &nevent
.m_pointDrag
.y
);
2668 nevent
.SetEventObject(this);
2669 GetEventHandler()->ProcessEvent(nevent
);
2671 else if ( event
.LeftUp() )
2675 if ( (item
== m_current
) &&
2676 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2677 HasFlag(wxTR_EDIT_LABELS
) )
2679 if ( m_renameTimer
->IsRunning() )
2680 m_renameTimer
->Stop();
2682 m_renameTimer
->Start( 100, TRUE
);
2685 m_lastOnSame
= FALSE
;
2688 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2690 if ( event
.LeftDown() )
2692 m_lastOnSame
= item
== m_current
;
2695 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2697 // only toggle the item for a single click, double click on
2698 // the button doesn't do anything (it toggles the item twice)
2699 if ( event
.LeftDown() )
2704 // don't select the item if the button was clicked
2708 // how should the selection work for this event?
2709 bool is_multiple
, extended_select
, unselect_others
;
2710 EventFlagsToSelType(GetWindowStyleFlag(),
2712 event
.ControlDown(),
2713 is_multiple
, extended_select
, unselect_others
);
2715 SelectItem(item
, unselect_others
, extended_select
);
2717 // For some reason, Windows isn't recognizing a left double-click,
2718 // so we need to simulate it here. Allow 200 milliseconds for now.
2719 if ( event
.LeftDClick() )
2721 // double clicking should not start editing the item label
2722 m_renameTimer
->Stop();
2723 m_lastOnSame
= FALSE
;
2725 // send activate event first
2726 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2727 nevent
.m_item
= (long) item
;
2729 CalcScrolledPosition(x
, y
,
2730 &nevent
.m_pointDrag
.x
,
2731 &nevent
.m_pointDrag
.y
);
2732 nevent
.SetEventObject( this );
2733 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2735 // if the user code didn't process the activate event,
2736 // handle it ourselves by toggling the item when it is
2738 if ( item
->HasPlus() )
2748 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2750 /* after all changes have been done to the tree control,
2751 * we actually redraw the tree when everything is over */
2753 if (!m_dirty
) return;
2757 CalculatePositions();
2759 AdjustMyScrollbars();
2762 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2768 dc
.SetFont(m_boldFont
);
2770 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2773 // restore normal font
2774 dc
.SetFont( m_normalFont
);
2778 int image
= item
->GetCurrentImage();
2779 if ( image
!= NO_IMAGE
)
2781 if ( m_imageListNormal
)
2783 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2788 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2791 total_h
+= 2; // at least 2 pixels
2793 total_h
+= total_h
/10; // otherwise 10% extra spacing
2795 item
->SetHeight(total_h
);
2796 if (total_h
>m_lineHeight
)
2797 m_lineHeight
=total_h
;
2799 item
->SetWidth(image_w
+text_w
+2);
2802 // -----------------------------------------------------------------------------
2803 // for developper : y is now the top of the level
2804 // not the middle of it !
2805 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2807 int x
= level
*m_indent
;
2808 if (!HasFlag(wxTR_HIDE_ROOT
))
2812 else if (level
== 0)
2814 // a hidden root is not evaluated, but its
2815 // children are always calculated
2819 CalculateSize( item
, dc
);
2822 item
->SetX( x
+m_spacing
);
2824 y
+= GetLineHeight(item
);
2826 if ( !item
->IsExpanded() )
2828 // we don't need to calculate collapsed branches
2833 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2834 size_t n
, count
= children
.Count();
2836 for (n
= 0; n
< count
; ++n
)
2837 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2840 void wxGenericTreeCtrl::CalculatePositions()
2842 if ( !m_anchor
) return;
2844 wxClientDC
dc(this);
2847 dc
.SetFont( m_normalFont
);
2849 dc
.SetPen( m_dottedPen
);
2850 //if(GetImageList() == NULL)
2851 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2854 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2857 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2859 if (m_dirty
) return;
2861 wxClientDC
dc(this);
2866 GetClientSize( &cw
, &ch
);
2869 rect
.x
= dc
.LogicalToDeviceX( 0 );
2871 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2874 Refresh( TRUE
, &rect
);
2876 AdjustMyScrollbars();
2879 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2881 if (m_dirty
) return;
2883 wxClientDC
dc(this);
2888 GetClientSize( &cw
, &ch
);
2891 rect
.x
= dc
.LogicalToDeviceX( 0 );
2892 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2894 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2896 Refresh( TRUE
, &rect
);
2899 void wxGenericTreeCtrl::RefreshSelected()
2901 // TODO: this is awfully inefficient, we should keep the list of all
2902 // selected items internally, should be much faster
2904 RefreshSelectedUnder(m_anchor
);
2907 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2909 if ( item
->IsSelected() )
2912 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2913 size_t count
= children
.GetCount();
2914 for ( size_t n
= 0; n
< count
; n
++ )
2916 RefreshSelectedUnder(children
[n
]);
2920 // ----------------------------------------------------------------------------
2921 // changing colours: we need to refresh the tree control
2922 // ----------------------------------------------------------------------------
2924 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
2926 if ( !wxWindow::SetBackgroundColour(colour
) )
2934 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
2936 if ( !wxWindow::SetForegroundColour(colour
) )
2944 #endif // wxUSE_TREECTRL