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::GetPrev(const wxTreeItemId
& item
) const
1037 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1039 wxFAIL_MSG(wxT("not implemented"));
1041 return wxTreeItemId();
1044 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1046 wxTreeItemId id
= GetRootItem();
1055 } while (id
.IsOk());
1057 return wxTreeItemId();
1060 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1062 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1064 wxTreeItemId id
= item
;
1067 while (id
= GetNext(id
), id
.IsOk())
1073 return wxTreeItemId();
1076 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1078 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1080 wxFAIL_MSG(wxT("not implemented"));
1082 return wxTreeItemId();
1085 // -----------------------------------------------------------------------------
1087 // -----------------------------------------------------------------------------
1089 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1091 const wxString
& text
,
1092 int image
, int selImage
,
1093 wxTreeItemData
*data
)
1095 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1098 // should we give a warning here?
1099 return AddRoot(text
, image
, selImage
, data
);
1102 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1104 wxClientDC
dc(this);
1105 wxGenericTreeItem
*item
=
1106 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1110 data
->m_pItem
= (long) item
;
1113 parent
->Insert( item
, previous
);
1118 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1119 int image
, int selImage
,
1120 wxTreeItemData
*data
)
1122 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1124 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1126 wxClientDC
dc(this);
1127 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1128 image
, selImage
, data
);
1129 if (HasFlag(wxTR_HIDE_ROOT
))
1131 // if root is hidden, make sure we can navigate
1133 m_anchor
->SetHasPlus();
1138 data
->m_pItem
= (long) m_anchor
;
1141 if (!HasFlag(wxTR_MULTIPLE
))
1143 m_current
= m_key_current
= m_anchor
;
1144 m_current
->SetHilight( TRUE
);
1150 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1151 const wxString
& text
,
1152 int image
, int selImage
,
1153 wxTreeItemData
*data
)
1155 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1158 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1159 const wxTreeItemId
& idPrevious
,
1160 const wxString
& text
,
1161 int image
, int selImage
,
1162 wxTreeItemData
*data
)
1164 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1167 // should we give a warning here?
1168 return AddRoot(text
, image
, selImage
, data
);
1171 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1172 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1173 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1175 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1178 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1180 const wxString
& text
,
1181 int image
, int selImage
,
1182 wxTreeItemData
*data
)
1184 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1187 // should we give a warning here?
1188 return AddRoot(text
, image
, selImage
, data
);
1191 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1194 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1195 const wxString
& text
,
1196 int image
, int selImage
,
1197 wxTreeItemData
*data
)
1199 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1202 // should we give a warning here?
1203 return AddRoot(text
, image
, selImage
, data
);
1206 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1207 image
, selImage
, data
);
1210 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1212 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1213 event
.m_item
= (long) item
;
1214 event
.SetEventObject( this );
1215 ProcessEvent( event
);
1218 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1220 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1222 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1223 item
->DeleteChildren(this);
1226 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1228 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1230 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1232 // don't stay with invalid m_key_current or we will crash in
1233 // the next call to OnChar()
1234 bool changeKeyCurrent
= FALSE
;
1235 wxGenericTreeItem
*itemKey
= m_key_current
;
1238 if ( itemKey
== item
)
1240 // m_key_current is a descendant of the item being deleted
1241 changeKeyCurrent
= TRUE
;
1244 itemKey
= itemKey
->GetParent();
1247 wxGenericTreeItem
*parent
= item
->GetParent();
1250 parent
->GetChildren().Remove( item
); // remove by value
1253 if ( changeKeyCurrent
)
1255 // may be NULL or not
1256 m_key_current
= parent
;
1259 item
->DeleteChildren(this);
1260 SendDeleteEvent(item
);
1264 void wxGenericTreeCtrl::DeleteAllItems()
1270 m_anchor
->DeleteChildren(this);
1277 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1279 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1281 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1283 if ( !item
->HasPlus() )
1286 if ( item
->IsExpanded() )
1289 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1290 event
.m_item
= (long) item
;
1291 event
.SetEventObject( this );
1293 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1295 // cancelled by program
1300 CalculatePositions();
1302 RefreshSubtree(item
);
1304 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1305 ProcessEvent( event
);
1308 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1311 if ( IsExpanded(item
) )
1314 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1315 while ( child
.IsOk() )
1319 child
= GetNextChild(item
, cookie
);
1324 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1326 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1328 if ( !item
->IsExpanded() )
1331 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1332 event
.m_item
= (long) item
;
1333 event
.SetEventObject( this );
1334 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1336 // cancelled by program
1342 #if 0 // TODO why should items be collapsed recursively?
1343 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1344 size_t count
= children
.Count();
1345 for ( size_t n
= 0; n
< count
; n
++ )
1347 Collapse(children
[n
]);
1351 CalculatePositions();
1353 RefreshSubtree(item
);
1355 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1356 ProcessEvent( event
);
1359 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1362 DeleteChildren(item
);
1365 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1367 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1369 if (item
->IsExpanded())
1375 void wxGenericTreeCtrl::Unselect()
1379 m_current
->SetHilight( FALSE
);
1380 RefreshLine( m_current
);
1384 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1386 if (item
->IsSelected())
1388 item
->SetHilight(FALSE
);
1392 if (item
->HasChildren())
1394 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1395 size_t count
= children
.Count();
1396 for ( size_t n
= 0; n
< count
; ++n
)
1398 UnselectAllChildren(children
[n
]);
1403 void wxGenericTreeCtrl::UnselectAll()
1405 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1408 // Recursive function !
1409 // To stop we must have crt_item<last_item
1411 // Tag all next children, when no more children,
1412 // Move to parent (not to tag)
1413 // Keep going... if we found last_item, we stop.
1414 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1416 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1418 if (parent
== NULL
) // This is root item
1419 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1421 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1422 int index
= children
.Index(crt_item
);
1423 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1425 size_t count
= children
.Count();
1426 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1428 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1431 return TagNextChildren(parent
, last_item
, select
);
1434 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1436 crt_item
->SetHilight(select
);
1437 RefreshLine(crt_item
);
1439 if (crt_item
==last_item
)
1442 if (crt_item
->HasChildren())
1444 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1445 size_t count
= children
.Count();
1446 for ( size_t n
= 0; n
< count
; ++n
)
1448 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1456 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1458 // item2 is not necessary after item1
1459 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1461 // choice first' and 'last' between item1 and item2
1462 if (item1
->GetY()<item2
->GetY())
1473 bool select
= m_current
->IsSelected();
1475 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1478 TagNextChildren(first
,last
,select
);
1481 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1482 bool unselect_others
,
1483 bool extended_select
)
1485 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1487 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1488 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1490 //wxCHECK_RET( ( (!unselect_others) && is_single),
1491 // wxT("this is a single selection tree") );
1493 // to keep going anyhow !!!
1496 if (item
->IsSelected())
1497 return; // nothing to do
1498 unselect_others
= TRUE
;
1499 extended_select
= FALSE
;
1501 else if ( unselect_others
&& item
->IsSelected() )
1503 // selection change if there is more than one item currently selected
1504 wxArrayTreeItemIds selected_items
;
1505 if ( GetSelections(selected_items
) == 1 )
1509 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1510 event
.m_item
= (long) item
;
1511 event
.m_itemOld
= (long) m_current
;
1512 event
.SetEventObject( this );
1513 // TODO : Here we don't send any selection mode yet !
1515 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1518 wxTreeItemId parent
= GetParent( itemId
);
1519 while (parent
.IsOk())
1521 if (!IsExpanded(parent
))
1524 parent
= GetParent( parent
);
1527 EnsureVisible( itemId
);
1530 if (unselect_others
)
1532 if (is_single
) Unselect(); // to speed up thing
1537 if (extended_select
)
1541 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1544 // don't change the mark (m_current)
1545 SelectItemRange(m_current
, item
);
1549 bool select
=TRUE
; // the default
1551 // Check if we need to toggle hilight (ctrl mode)
1552 if (!unselect_others
)
1553 select
=!item
->IsSelected();
1555 m_current
= m_key_current
= item
;
1556 m_current
->SetHilight(select
);
1557 RefreshLine( m_current
);
1560 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1561 GetEventHandler()->ProcessEvent( event
);
1564 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1565 wxArrayTreeItemIds
&array
) const
1567 if ( item
->IsSelected() )
1568 array
.Add(wxTreeItemId(item
));
1570 if ( item
->HasChildren() )
1572 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1573 size_t count
= children
.GetCount();
1574 for ( size_t n
= 0; n
< count
; ++n
)
1575 FillArray(children
[n
], array
);
1579 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1582 wxTreeItemId idRoot
= GetRootItem();
1583 if ( idRoot
.IsOk() )
1585 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1587 //else: the tree is empty, so no selections
1589 return array
.Count();
1592 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1594 if (!item
.IsOk()) return;
1596 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1598 // first expand all parent branches
1599 wxGenericTreeItem
*parent
= gitem
->GetParent();
1603 parent
= parent
->GetParent();
1606 //if (parent) CalculatePositions();
1611 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1613 if (!item
.IsOk()) return;
1615 // We have to call this here because the label in
1616 // question might just have been added and no screen
1617 // update taken place.
1618 if (m_dirty
) wxYieldIfNeeded();
1620 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1622 // now scroll to the item
1623 int item_y
= gitem
->GetY();
1627 GetViewStart( &start_x
, &start_y
);
1628 start_y
*= PIXELS_PER_UNIT
;
1632 GetClientSize( &client_w
, &client_h
);
1634 if (item_y
< start_y
+3)
1639 m_anchor
->GetSize( x
, y
, this );
1640 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1641 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1642 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1643 // Item should appear at top
1644 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1646 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1651 m_anchor
->GetSize( x
, y
, this );
1652 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1653 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1654 item_y
+= PIXELS_PER_UNIT
+2;
1655 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1656 // Item should appear at bottom
1657 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
);
1661 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1662 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1664 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1665 wxGenericTreeItem
**item2
)
1667 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1669 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1672 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1673 const wxTreeItemId
& item2
)
1675 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1678 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1680 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1682 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1684 wxCHECK_RET( !s_treeBeingSorted
,
1685 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1687 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1688 if ( children
.Count() > 1 )
1692 s_treeBeingSorted
= this;
1693 children
.Sort(tree_ctrl_compare_func
);
1694 s_treeBeingSorted
= NULL
;
1696 //else: don't make the tree dirty as nothing changed
1699 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1701 return m_imageListNormal
;
1704 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1706 return m_imageListButtons
;
1709 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1711 return m_imageListState
;
1714 void wxGenericTreeCtrl::CalculateLineHeight()
1716 wxClientDC
dc(this);
1717 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1719 if ( m_imageListNormal
)
1721 // Calculate a m_lineHeight value from the normal Image sizes.
1722 // May be toggle off. Then wxGenericTreeCtrl will spread when
1723 // necessary (which might look ugly).
1724 int n
= m_imageListNormal
->GetImageCount();
1725 for (int i
= 0; i
< n
; i
++)
1727 int width
= 0, height
= 0;
1728 m_imageListNormal
->GetSize(i
, width
, height
);
1729 if (height
> m_lineHeight
) m_lineHeight
= height
;
1733 if (m_imageListButtons
)
1735 // Calculate a m_lineHeight value from the Button image sizes.
1736 // May be toggle off. Then wxGenericTreeCtrl will spread when
1737 // necessary (which might look ugly).
1738 int n
= m_imageListButtons
->GetImageCount();
1739 for (int i
= 0; i
< n
; i
++)
1741 int width
= 0, height
= 0;
1742 m_imageListButtons
->GetSize(i
, width
, height
);
1743 if (height
> m_lineHeight
) m_lineHeight
= height
;
1747 if (m_lineHeight
< 30)
1748 m_lineHeight
+= 2; // at least 2 pixels
1750 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1753 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1755 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1756 m_imageListNormal
= imageList
;
1757 m_ownsImageListNormal
= FALSE
;
1759 CalculateLineHeight();
1762 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1764 if (m_ownsImageListState
) delete m_imageListState
;
1765 m_imageListState
= imageList
;
1766 m_ownsImageListState
= FALSE
;
1769 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1771 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1772 m_imageListButtons
= imageList
;
1773 m_ownsImageListButtons
= FALSE
;
1775 CalculateLineHeight();
1778 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1780 SetImageList(imageList
);
1781 m_ownsImageListNormal
= TRUE
;
1784 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1786 SetStateImageList(imageList
);
1787 m_ownsImageListState
= TRUE
;
1790 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1792 SetButtonsImageList(imageList
);
1793 m_ownsImageListButtons
= TRUE
;
1796 // -----------------------------------------------------------------------------
1798 // -----------------------------------------------------------------------------
1800 void wxGenericTreeCtrl::AdjustMyScrollbars()
1805 m_anchor
->GetSize( x
, y
, this );
1806 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1807 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1808 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1809 int y_pos
= GetScrollPos( wxVERTICAL
);
1810 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1814 SetScrollbars( 0, 0, 0, 0 );
1818 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1820 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1821 return item
->GetHeight();
1823 return m_lineHeight
;
1826 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1828 // TODO implement "state" icon on items
1830 wxTreeItemAttr
*attr
= item
->GetAttributes();
1831 if ( attr
&& attr
->HasFont() )
1832 dc
.SetFont(attr
->GetFont());
1833 else if (item
->IsBold())
1834 dc
.SetFont(m_boldFont
);
1836 long text_w
= 0, text_h
= 0;
1837 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1839 int image_h
= 0, image_w
= 0;
1840 int image
= item
->GetCurrentImage();
1841 if ( image
!= NO_IMAGE
)
1843 if ( m_imageListNormal
)
1845 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1854 int total_h
= GetLineHeight(item
);
1856 if ( item
->IsSelected() )
1858 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1863 if ( attr
&& attr
->HasBackgroundColour() )
1864 colBg
= attr
->GetBackgroundColour();
1866 colBg
= m_backgroundColour
;
1867 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1870 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1872 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1874 // If it's selected, and there's an image, then we should
1875 // take care to leave the area under the image painted in the
1876 // background colour.
1877 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1878 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1882 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1883 item
->GetWidth()+2, total_h
-offset
);
1886 if ( image
!= NO_IMAGE
)
1888 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1889 m_imageListNormal
->Draw( image
, dc
,
1891 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1892 wxIMAGELIST_DRAW_TRANSPARENT
);
1893 dc
.DestroyClippingRegion();
1896 dc
.SetBackgroundMode(wxTRANSPARENT
);
1897 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1898 dc
.DrawText( item
->GetText(),
1899 (wxCoord
)(image_w
+ item
->GetX()),
1900 (wxCoord
)(item
->GetY() + extraH
));
1902 // restore normal font
1903 dc
.SetFont( m_normalFont
);
1906 // Now y stands for the top of the item, whereas it used to stand for middle !
1907 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1909 int x
= level
*m_indent
;
1910 if (!HasFlag(wxTR_HIDE_ROOT
))
1914 else if (level
== 0)
1916 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1917 size_t n
, count
= children
.Count();
1918 for (n
= 0; n
< count
; ++n
)
1919 PaintLevel(children
[n
], dc
, 1, y
);
1923 item
->SetX(x
+m_spacing
);
1926 int h
= GetLineHeight(item
);
1928 int y_mid
= y_top
+ (h
>>1);
1931 int exposed_x
= dc
.LogicalToDeviceX(0);
1932 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1934 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1936 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1938 if (!HasFlag(wxTR_NO_LINES
))
1940 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
1941 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1942 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1945 if (m_imageListButtons
!= NULL
)
1947 // draw the image button here
1948 int image_h
= 0, image_w
= 0, image
= wxCLOSED_BUTTON
;
1949 if (item
->IsExpanded()) image
= wxOPEN_BUTTON
;
1950 if (item
->IsSelected())
1951 image
+= wxOPEN_BUTTON_SELECTED
- wxOPEN_BUTTON
;
1952 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1953 int xx
= x
- (image_w
>>1);
1954 int yy
= y_mid
- (image_h
>>1);
1955 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1956 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1957 wxIMAGELIST_DRAW_TRANSPARENT
);
1958 dc
.DestroyClippingRegion();
1960 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1962 // draw the twisty button here
1963 dc
.SetPen(*wxBLACK_PEN
);
1964 dc
.SetBrush(*m_hilightBrush
);
1968 if (item
->IsExpanded())
1971 button
[0].y
= y_mid
-2;
1973 button
[1].y
= y_mid
-2;
1975 button
[2].y
= y_mid
+3;
1979 button
[0].y
= y_mid
-5;
1981 button
[1].y
= y_mid
+5;
1983 button
[2].y
= y_mid
;
1986 dc
.DrawPolygon(3, button
);
1988 dc
.SetPen(m_dottedPen
);
1990 else // if (HasFlag(wxTR_HAS_BUTTONS))
1992 // draw the plus sign here
1993 dc
.SetPen(*wxGREY_PEN
);
1994 dc
.SetBrush(*wxWHITE_BRUSH
);
1995 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
1996 dc
.SetPen(*wxBLACK_PEN
);
1997 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
1998 if (!item
->IsExpanded())
1999 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2000 dc
.SetPen(m_dottedPen
);
2003 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2005 // draw the horizontal line here
2007 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
2008 x_start
-= m_indent
;
2009 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2014 // don't draw rect outline if we already have the
2015 // background color under Mac
2016 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2017 #endif // !__WXMAC__
2021 if ( item
->IsSelected() )
2023 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2027 wxTreeItemAttr
*attr
= item
->GetAttributes();
2028 if (attr
&& attr
->HasTextColour())
2029 colText
= attr
->GetTextColour();
2031 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2035 dc
.SetTextForeground(colText
);
2039 PaintItem(item
, dc
);
2041 if (HasFlag(wxTR_ROW_LINES
))
2043 dc
.SetPen(*wxWHITE_PEN
);
2044 dc
.DrawLine(0, y_top
, 10000, y_top
);
2045 dc
.DrawLine(0, y
, 10000, y
);
2048 // restore DC objects
2049 dc
.SetBrush(*wxWHITE_BRUSH
);
2050 dc
.SetPen(m_dottedPen
);
2051 dc
.SetTextForeground(*wxBLACK
);
2054 if (item
->IsExpanded())
2056 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2057 int count
= children
.Count();
2064 PaintLevel(children
[n
], dc
, level
, y
);
2065 } while (++n
< count
);
2067 if (!HasFlag(wxTR_NO_LINES
))
2069 // draw line down to last child
2070 oldY
+= GetLineHeight(children
[n
-1])/2;
2071 if (HasButtons()) y_mid
+= 5;
2072 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2078 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2082 if ( item
->HasPlus() )
2084 // it's a folder, indicate it by a border
2089 // draw a line under the drop target because the item will be
2091 DrawLine(item
, TRUE
/* below */);
2094 SetCursor(wxCURSOR_BULLSEYE
);
2099 SetCursor(wxCURSOR_NO_ENTRY
);
2103 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2105 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2107 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2109 wxClientDC
dc(this);
2111 dc
.SetLogicalFunction(wxINVERT
);
2112 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2114 int w
= i
->GetWidth() + 2;
2115 int h
= GetLineHeight(i
) + 2;
2117 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2120 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2122 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2124 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2126 wxClientDC
dc(this);
2128 dc
.SetLogicalFunction(wxINVERT
);
2134 y
+= GetLineHeight(i
) - 1;
2137 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2140 // -----------------------------------------------------------------------------
2141 // wxWindows callbacks
2142 // -----------------------------------------------------------------------------
2144 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2152 dc
.SetFont( m_normalFont
);
2153 dc
.SetPen( m_dottedPen
);
2155 // this is now done dynamically
2156 //if(GetImageList() == NULL)
2157 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2160 PaintLevel( m_anchor
, dc
, 0, y
);
2163 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2172 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2181 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2183 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2184 te
.m_code
= (int)event
.KeyCode();
2185 te
.SetEventObject( this );
2186 if ( GetEventHandler()->ProcessEvent( te
) )
2188 // intercepted by the user code
2192 if ( (m_current
== 0) || (m_key_current
== 0) )
2198 // how should the selection work for this event?
2199 bool is_multiple
, extended_select
, unselect_others
;
2200 EventFlagsToSelType(GetWindowStyleFlag(),
2202 event
.ControlDown(),
2203 is_multiple
, extended_select
, unselect_others
);
2207 // * : Expand all/Collapse all
2208 // ' ' | return : activate
2209 // up : go up (not last children!)
2211 // left : go to parent
2212 // right : open if parent and go next
2213 // home : go to root
2214 // end : go to last item without opening parents
2215 switch (event
.KeyCode())
2219 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2227 if ( !IsExpanded(m_current
) )
2230 ExpandAll(m_current
);
2233 //else: fall through to Collapse() it
2237 if (IsExpanded(m_current
))
2239 Collapse(m_current
);
2246 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2247 event
.m_item
= (long) m_current
;
2249 event
.SetEventObject( this );
2250 GetEventHandler()->ProcessEvent( event
);
2254 // up goes to the previous sibling or to the last
2255 // of its children if it's expanded
2258 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2261 prev
= GetParent( m_key_current
);
2262 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2264 break; // don't go to root if it is hidden
2269 wxTreeItemId current
= m_key_current
;
2270 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2271 if (current
== GetFirstChild( prev
, cookie
))
2273 // otherwise we return to where we came from
2274 SelectItem( prev
, unselect_others
, extended_select
);
2275 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2276 EnsureVisible( prev
);
2283 while ( IsExpanded(prev
) && HasChildren(prev
) )
2285 wxTreeItemId child
= GetLastChild(prev
);
2292 SelectItem( prev
, unselect_others
, extended_select
);
2293 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2294 EnsureVisible( prev
);
2299 // left arrow goes to the parent
2302 wxTreeItemId prev
= GetParent( m_current
);
2303 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2305 // don't go to root if it is hidden
2306 prev
= GetPrevSibling( m_current
);
2310 EnsureVisible( prev
);
2311 SelectItem( prev
, unselect_others
, extended_select
);
2317 // this works the same as the down arrow except that we
2318 // also expand the item if it wasn't expanded yet
2324 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2327 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2328 SelectItem( child
, unselect_others
, extended_select
);
2329 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2330 EnsureVisible( child
);
2334 wxTreeItemId next
= GetNextSibling( m_key_current
);
2337 wxTreeItemId current
= m_key_current
;
2338 while (current
&& !next
)
2340 current
= GetParent( current
);
2341 if (current
) next
= GetNextSibling( current
);
2346 SelectItem( next
, unselect_others
, extended_select
);
2347 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2348 EnsureVisible( next
);
2354 // <End> selects the last visible tree item
2357 wxTreeItemId last
= GetRootItem();
2359 while ( last
.IsOk() && IsExpanded(last
) )
2361 wxTreeItemId lastChild
= GetLastChild(last
);
2363 // it may happen if the item was expanded but then all of
2364 // its children have been deleted - so IsExpanded() returned
2365 // TRUE, but GetLastChild() returned invalid item
2374 EnsureVisible( last
);
2375 SelectItem( last
, unselect_others
, extended_select
);
2380 // <Home> selects the root item
2383 wxTreeItemId prev
= GetRootItem();
2385 if (HasFlag(wxTR_HIDE_ROOT
))
2388 prev
= GetFirstChild(prev
, dummy
);
2391 EnsureVisible( prev
);
2392 SelectItem( prev
, unselect_others
, extended_select
);
2401 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2403 // JACS: removed wxYieldIfNeeded() because it can cause the window
2404 // to be deleted from under us if a close window event is pending
2409 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2410 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2411 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2412 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2413 if (flags
) return wxTreeItemId();
2415 if (m_anchor
== NULL
)
2417 flags
= wxTREE_HITTEST_NOWHERE
;
2418 return wxTreeItemId();
2421 wxClientDC
dc(this);
2423 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2424 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2425 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2431 flags
= wxTREE_HITTEST_NOWHERE
;
2432 return wxTreeItemId();
2437 // get the bounding rectangle of the item (or of its label only)
2438 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2440 bool WXUNUSED(textOnly
)) const
2442 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2444 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2447 GetViewStart(& startX
, & startY
);
2449 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2450 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2451 rect
.width
= i
->GetWidth();
2452 //rect.height = i->GetHeight();
2453 rect
.height
= GetLineHeight(i
);
2460 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2462 if (!item
.IsOk()) return;
2464 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2466 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2467 te
.m_item
= (long) m_currentEdit
;
2468 te
.SetEventObject( this );
2469 GetEventHandler()->ProcessEvent( te
);
2471 if (!te
.IsAllowed()) return;
2473 // We have to call this here because the label in
2474 // question might just have been added and no screen
2475 // update taken place.
2476 if (m_dirty
) wxYieldIfNeeded();
2478 wxString s
= m_currentEdit
->GetText();
2479 int x
= m_currentEdit
->GetX();
2480 int y
= m_currentEdit
->GetY();
2481 int w
= m_currentEdit
->GetWidth();
2482 int h
= m_currentEdit
->GetHeight();
2487 int image
= m_currentEdit
->GetCurrentImage();
2488 if ( image
!= NO_IMAGE
)
2490 if ( m_imageListNormal
)
2492 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2497 wxFAIL_MSG(_T("you must create an image list to use images!"));
2501 w
-= image_w
+ 4; // I don't know why +4 is needed
2503 wxClientDC
dc(this);
2505 x
= dc
.LogicalToDeviceX( x
);
2506 y
= dc
.LogicalToDeviceY( y
);
2508 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2518 void wxGenericTreeCtrl::OnRenameTimer()
2523 void wxGenericTreeCtrl::OnRenameAccept()
2525 // TODO if the validator fails this causes a crash
2526 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2527 le
.m_item
= (long) m_currentEdit
;
2528 le
.SetEventObject( this );
2529 le
.m_label
= m_renameRes
;
2530 GetEventHandler()->ProcessEvent( le
);
2532 if (!le
.IsAllowed()) return;
2534 SetItemText( m_currentEdit
, m_renameRes
);
2537 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2539 if ( !m_anchor
) return;
2541 // we process left mouse up event (enables in-place edit), right down
2542 // (pass to the user code), left dbl click (activate item) and
2543 // dragging/moving events for items drag-and-drop
2544 if ( !(event
.LeftDown() ||
2546 event
.RightDown() ||
2547 event
.LeftDClick() ||
2549 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2556 wxClientDC
dc(this);
2558 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2559 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2562 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2567 if ( event
.Dragging() && !m_isDragging
)
2569 if (m_dragCount
== 0)
2570 m_dragStart
= wxPoint(x
,y
);
2574 if (m_dragCount
!= 3)
2576 // wait until user drags a bit further...
2580 wxEventType command
= event
.RightIsDown()
2581 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2582 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2584 wxTreeEvent
nevent( command
, GetId() );
2585 nevent
.m_item
= (long) m_current
;
2586 nevent
.SetEventObject(this);
2588 // by default the dragging is not supported, the user code must
2589 // explicitly allow the event for it to take place
2592 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2594 // we're going to drag this item
2595 m_isDragging
= TRUE
;
2597 // remember the old cursor because we will change it while
2599 m_oldCursor
= m_cursor
;
2601 // in a single selection control, hide the selection temporarily
2602 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2604 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2606 if ( m_oldSelection
)
2608 m_oldSelection
->SetHilight(FALSE
);
2609 RefreshLine(m_oldSelection
);
2616 else if ( event
.Moving() )
2618 if ( item
!= m_dropTarget
)
2620 // unhighlight the previous drop target
2621 DrawDropEffect(m_dropTarget
);
2623 m_dropTarget
= item
;
2625 // highlight the current drop target if any
2626 DrawDropEffect(m_dropTarget
);
2631 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2633 // erase the highlighting
2634 DrawDropEffect(m_dropTarget
);
2636 if ( m_oldSelection
)
2638 m_oldSelection
->SetHilight(TRUE
);
2639 RefreshLine(m_oldSelection
);
2640 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2643 // generate the drag end event
2644 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2646 event
.m_item
= (long) item
;
2647 event
.m_pointDrag
= wxPoint(x
, y
);
2648 event
.SetEventObject(this);
2650 (void)GetEventHandler()->ProcessEvent(event
);
2652 m_isDragging
= FALSE
;
2653 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2657 SetCursor(m_oldCursor
);
2663 // here we process only the messages which happen on tree items
2667 if (item
== NULL
) return; /* we hit the blank area */
2669 if ( event
.RightDown() )
2671 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2672 nevent
.m_item
= (long) item
;
2674 CalcScrolledPosition(x
, y
,
2675 &nevent
.m_pointDrag
.x
,
2676 &nevent
.m_pointDrag
.y
);
2677 nevent
.SetEventObject(this);
2678 GetEventHandler()->ProcessEvent(nevent
);
2680 else if ( event
.LeftUp() )
2684 if ( (item
== m_current
) &&
2685 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2686 HasFlag(wxTR_EDIT_LABELS
) )
2688 if ( m_renameTimer
->IsRunning() )
2689 m_renameTimer
->Stop();
2691 m_renameTimer
->Start( 100, TRUE
);
2694 m_lastOnSame
= FALSE
;
2697 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2699 if ( event
.LeftDown() )
2701 m_lastOnSame
= item
== m_current
;
2704 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2706 // only toggle the item for a single click, double click on
2707 // the button doesn't do anything (it toggles the item twice)
2708 if ( event
.LeftDown() )
2713 // don't select the item if the button was clicked
2717 // how should the selection work for this event?
2718 bool is_multiple
, extended_select
, unselect_others
;
2719 EventFlagsToSelType(GetWindowStyleFlag(),
2721 event
.ControlDown(),
2722 is_multiple
, extended_select
, unselect_others
);
2724 SelectItem(item
, unselect_others
, extended_select
);
2726 // For some reason, Windows isn't recognizing a left double-click,
2727 // so we need to simulate it here. Allow 200 milliseconds for now.
2728 if ( event
.LeftDClick() )
2730 // double clicking should not start editing the item label
2731 m_renameTimer
->Stop();
2732 m_lastOnSame
= FALSE
;
2734 // send activate event first
2735 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2736 nevent
.m_item
= (long) item
;
2738 CalcScrolledPosition(x
, y
,
2739 &nevent
.m_pointDrag
.x
,
2740 &nevent
.m_pointDrag
.y
);
2741 nevent
.SetEventObject( this );
2742 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2744 // if the user code didn't process the activate event,
2745 // handle it ourselves by toggling the item when it is
2747 if ( item
->HasPlus() )
2757 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2759 /* after all changes have been done to the tree control,
2760 * we actually redraw the tree when everything is over */
2762 if (!m_dirty
) return;
2766 CalculatePositions();
2768 AdjustMyScrollbars();
2771 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2777 dc
.SetFont(m_boldFont
);
2779 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2782 // restore normal font
2783 dc
.SetFont( m_normalFont
);
2787 int image
= item
->GetCurrentImage();
2788 if ( image
!= NO_IMAGE
)
2790 if ( m_imageListNormal
)
2792 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2797 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2800 total_h
+= 2; // at least 2 pixels
2802 total_h
+= total_h
/10; // otherwise 10% extra spacing
2804 item
->SetHeight(total_h
);
2805 if (total_h
>m_lineHeight
)
2806 m_lineHeight
=total_h
;
2808 item
->SetWidth(image_w
+text_w
+2);
2811 // -----------------------------------------------------------------------------
2812 // for developper : y is now the top of the level
2813 // not the middle of it !
2814 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2816 int x
= level
*m_indent
;
2817 if (!HasFlag(wxTR_HIDE_ROOT
))
2821 else if (level
== 0)
2823 // a hidden root is not evaluated, but its
2824 // children are always calculated
2828 CalculateSize( item
, dc
);
2831 item
->SetX( x
+m_spacing
);
2833 y
+= GetLineHeight(item
);
2835 if ( !item
->IsExpanded() )
2837 // we don't need to calculate collapsed branches
2842 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2843 size_t n
, count
= children
.Count();
2845 for (n
= 0; n
< count
; ++n
)
2846 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2849 void wxGenericTreeCtrl::CalculatePositions()
2851 if ( !m_anchor
) return;
2853 wxClientDC
dc(this);
2856 dc
.SetFont( m_normalFont
);
2858 dc
.SetPen( m_dottedPen
);
2859 //if(GetImageList() == NULL)
2860 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2863 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2866 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2868 if (m_dirty
) return;
2870 wxClientDC
dc(this);
2875 GetClientSize( &cw
, &ch
);
2878 rect
.x
= dc
.LogicalToDeviceX( 0 );
2880 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2883 Refresh( TRUE
, &rect
);
2885 AdjustMyScrollbars();
2888 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2890 if (m_dirty
) return;
2892 wxClientDC
dc(this);
2897 GetClientSize( &cw
, &ch
);
2900 rect
.x
= dc
.LogicalToDeviceX( 0 );
2901 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2903 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2905 Refresh( TRUE
, &rect
);
2908 void wxGenericTreeCtrl::RefreshSelected()
2910 // TODO: this is awfully inefficient, we should keep the list of all
2911 // selected items internally, should be much faster
2913 RefreshSelectedUnder(m_anchor
);
2916 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2918 if ( item
->IsSelected() )
2921 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2922 size_t count
= children
.GetCount();
2923 for ( size_t n
= 0; n
< count
; n
++ )
2925 RefreshSelectedUnder(children
[n
]);
2929 #endif // wxUSE_TREECTRL