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"
31 #include "wx/generic/treectlg.h"
32 #include "wx/imaglist.h"
33 #include "wx/settings.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_DRAG
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_RDRAG
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_LABEL_EDIT
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_DELETE_ITEM
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_GET_INFO
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SET_INFO
)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDED
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDING
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGED
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGING
)
58 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_KEY_DOWN
)
59 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
)
60 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
)
61 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
)
62 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_DRAG
)
64 // -----------------------------------------------------------------------------
66 // -----------------------------------------------------------------------------
68 class WXDLLEXPORT wxGenericTreeItem
;
70 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
71 //WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 static const int NO_IMAGE
= -1;
79 #define PIXELS_PER_UNIT 10
81 // -----------------------------------------------------------------------------
83 // -----------------------------------------------------------------------------
85 // timer used for enabling in-place edit
86 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
89 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
94 wxGenericTreeCtrl
*m_owner
;
97 // control used for in-place edit
98 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
102 wxTreeTextCtrl( wxWindow
*parent
,
106 wxGenericTreeCtrl
*owner
,
107 const wxString
&value
= wxEmptyString
,
108 const wxPoint
&pos
= wxDefaultPosition
,
109 const wxSize
&size
= wxDefaultSize
,
110 int style
= wxSIMPLE_BORDER
,
111 const wxValidator
& validator
= wxDefaultValidator
,
112 const wxString
&name
= wxTextCtrlNameStr
);
114 void OnChar( wxKeyEvent
&event
);
115 void OnKeyUp( wxKeyEvent
&event
);
116 void OnKillFocus( wxFocusEvent
&event
);
121 wxGenericTreeCtrl
*m_owner
;
122 wxString m_startValue
;
124 DECLARE_EVENT_TABLE()
125 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
129 class WXDLLEXPORT wxGenericTreeItem
133 wxGenericTreeItem() { m_data
= NULL
; }
134 wxGenericTreeItem( wxGenericTreeItem
*parent
,
135 const wxString
& text
,
137 int image
, int selImage
,
138 wxTreeItemData
*data
);
140 ~wxGenericTreeItem();
143 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
145 const wxString
& GetText() const { return m_text
; }
146 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
147 { return m_images
[which
]; }
148 wxTreeItemData
*GetData() const { return m_data
; }
150 // returns the current image for the item (depending on its
151 // selected/expanded/whatever state)
152 int GetCurrentImage() const;
154 void SetText( const wxString
&text
);
155 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
156 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
158 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
160 void SetBold(bool bold
) { m_isBold
= bold
; }
162 int GetX() const { return m_x
; }
163 int GetY() const { return m_y
; }
165 void SetX(int x
) { m_x
= x
; }
166 void SetY(int y
) { m_y
= y
; }
168 int GetHeight() const { return m_height
; }
169 int GetWidth() const { return m_width
; }
171 void SetHeight(int h
) { m_height
= h
; }
172 void SetWidth(int w
) { m_width
= w
; }
175 wxGenericTreeItem
*GetParent() const { return m_parent
; }
178 // deletes all children notifying the treectrl about it if !NULL
180 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
181 // FIXME don't know what is it for
184 // get count of all children (and grand children if 'recursively')
185 size_t GetChildrenCount(bool recursively
= TRUE
) const;
187 void Insert(wxGenericTreeItem
*child
, size_t index
)
188 { m_children
.Insert(child
, index
); }
190 void SetCross( int x
, int y
);
191 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
193 // return the item at given position (or NULL if no item), onButton is
194 // TRUE if the point belongs to the item's button, otherwise it lies
195 // on the button's label
196 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxGenericTreeCtrl
*, int &flags
);
198 void Expand() { m_isCollapsed
= FALSE
; }
199 void Collapse() { m_isCollapsed
= TRUE
; }
201 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
204 bool HasChildren() const { return !m_children
.IsEmpty(); }
205 bool IsSelected() const { return m_hasHilight
!= 0; }
206 bool IsExpanded() const { return !m_isCollapsed
; }
207 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
208 bool IsBold() const { return m_isBold
!= 0; }
211 // get them - may be NULL
212 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
213 // get them ensuring that the pointer is not NULL
214 wxTreeItemAttr
& Attr()
217 m_attr
= new wxTreeItemAttr
;
225 // tree ctrl images for the normal, selected, expanded and
226 // expanded+selected states
227 int m_images
[wxTreeItemIcon_Max
];
229 wxTreeItemData
*m_data
;
231 // use bitfields to save size
232 int m_isCollapsed
:1;
233 int m_hasHilight
:1; // same as focused
234 int m_hasPlus
:1; // used for item which doesn't have
235 // children but has a [+] button
236 int m_isBold
:1; // render the label in bold font
239 wxCoord m_height
, m_width
;
240 int m_xCross
, m_yCross
;
243 wxArrayGenericTreeItems m_children
;
244 wxGenericTreeItem
*m_parent
;
246 wxTreeItemAttr
*m_attr
;
249 // =============================================================================
251 // =============================================================================
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 // translate the key or mouse event flags to the type of selection we're
259 static void EventFlagsToSelType(long style
,
263 bool &extended_select
,
264 bool &unselect_others
)
266 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
267 extended_select
= shiftDown
&& is_multiple
;
268 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
271 // -----------------------------------------------------------------------------
272 // wxTreeRenameTimer (internal)
273 // -----------------------------------------------------------------------------
275 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
280 void wxTreeRenameTimer::Notify()
282 m_owner
->OnRenameTimer();
285 //-----------------------------------------------------------------------------
286 // wxTreeTextCtrl (internal)
287 //-----------------------------------------------------------------------------
289 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
291 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
292 EVT_CHAR (wxTreeTextCtrl::OnChar
)
293 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
294 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
297 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
301 wxGenericTreeCtrl
*owner
,
302 const wxString
&value
,
306 const wxValidator
& validator
,
307 const wxString
&name
)
308 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
314 (*m_res
) = wxEmptyString
;
315 m_startValue
= value
;
318 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
320 if (event
.m_keyCode
== WXK_RETURN
)
323 (*m_res
) = GetValue();
325 if (!wxPendingDelete
.Member(this))
326 wxPendingDelete
.Append(this);
328 if ((*m_accept
) && ((*m_res
) != m_startValue
))
329 m_owner
->OnRenameAccept();
333 if (event
.m_keyCode
== WXK_ESCAPE
)
338 if (!wxPendingDelete
.Member(this))
339 wxPendingDelete
.Append(this);
346 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
348 // auto-grow the textctrl:
349 wxSize parentSize
= m_owner
->GetSize();
350 wxPoint myPos
= GetPosition();
351 wxSize mySize
= GetSize();
353 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
354 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
355 if (mySize
.x
> sx
) sx
= mySize
.x
;
361 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
363 if (!wxPendingDelete
.Member(this))
364 wxPendingDelete
.Append(this);
366 if ((*m_accept
) && ((*m_res
) != m_startValue
))
367 m_owner
->OnRenameAccept();
371 // -----------------------------------------------------------------------------
373 // -----------------------------------------------------------------------------
375 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
377 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
378 : wxNotifyEvent( commandType
, id
)
381 m_itemOld
= (wxGenericTreeItem
*)NULL
;
385 // -----------------------------------------------------------------------------
387 // -----------------------------------------------------------------------------
389 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
390 const wxString
& text
,
392 int image
, int selImage
,
393 wxTreeItemData
*data
)
396 m_images
[wxTreeItemIcon_Normal
] = image
;
397 m_images
[wxTreeItemIcon_Selected
] = selImage
;
398 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
399 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
403 m_xCross
= m_yCross
= 0;
407 m_isCollapsed
= TRUE
;
408 m_hasHilight
= FALSE
;
414 m_attr
= (wxTreeItemAttr
*)NULL
;
416 // We don't know the height here yet.
421 wxGenericTreeItem::~wxGenericTreeItem()
427 wxASSERT_MSG( m_children
.IsEmpty(),
428 wxT("please call DeleteChildren() before deleting the item") );
431 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
433 size_t count
= m_children
.Count();
434 for ( size_t n
= 0; n
< count
; n
++ )
436 wxGenericTreeItem
*child
= m_children
[n
];
438 tree
->SendDeleteEvent(child
);
440 child
->DeleteChildren(tree
);
447 void wxGenericTreeItem::SetText( const wxString
&text
)
452 void wxGenericTreeItem::Reset()
455 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
457 m_images
[i
] = NO_IMAGE
;
462 m_height
= m_width
= 0;
469 m_isCollapsed
= TRUE
;
471 m_parent
= (wxGenericTreeItem
*)NULL
;
474 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
476 size_t count
= m_children
.Count();
480 size_t total
= count
;
481 for (size_t n
= 0; n
< count
; ++n
)
483 total
+= m_children
[n
]->GetChildrenCount();
489 void wxGenericTreeItem::SetCross( int x
, int y
)
495 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxGenericTreeCtrl
*theTree
)
497 int bottomY
=m_y
+theTree
->GetLineHeight(this);
498 if ( y
< bottomY
) y
= bottomY
;
499 int width
= m_x
+ m_width
;
500 if ( x
< width
) x
= width
;
504 size_t count
= m_children
.Count();
505 for ( size_t n
= 0; n
< count
; ++n
)
507 m_children
[n
]->GetSize( x
, y
, theTree
);
512 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
513 const wxGenericTreeCtrl
*theTree
,
516 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
518 if (point
.y
< m_y
+theTree
->GetLineHeight(this)/2 )
519 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
521 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
523 // 5 is the size of the plus sign
524 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
525 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
526 (IsExpanded() || HasPlus()))
528 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
532 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
537 // assuming every image (normal and selected ) has the same size !
538 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
539 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
541 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
542 flags
|= wxTREE_HITTEST_ONITEMICON
;
544 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
550 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
551 if (point
.x
> m_x
+m_width
)
552 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
560 size_t count
= m_children
.Count();
561 for ( size_t n
= 0; n
< count
; n
++ )
563 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
570 flags
|=wxTREE_HITTEST_NOWHERE
;
572 return (wxGenericTreeItem
*) NULL
;
575 int wxGenericTreeItem::GetCurrentImage() const
577 int image
= NO_IMAGE
;
582 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
585 if ( image
== NO_IMAGE
)
587 // we usually fall back to the normal item, but try just the
588 // expanded one (and not selected) first in this case
589 image
= GetImage(wxTreeItemIcon_Expanded
);
595 image
= GetImage(wxTreeItemIcon_Selected
);
598 // may be it doesn't have the specific image we want, try the default one
600 if ( image
== NO_IMAGE
)
608 // -----------------------------------------------------------------------------
609 // wxGenericTreeCtrl implementation
610 // -----------------------------------------------------------------------------
612 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
614 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
615 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
616 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
617 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
618 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
619 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
620 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
623 #if !defined(__WXMSW__) || defined(__WIN16__)
625 * wxTreeCtrl has to be a real class or we have problems with
626 * the run-time information.
629 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
632 // -----------------------------------------------------------------------------
633 // construction/destruction
634 // -----------------------------------------------------------------------------
636 void wxGenericTreeCtrl::Init()
640 m_anchor
= (wxGenericTreeItem
*) NULL
;
650 m_hilightBrush
= new wxBrush
652 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
657 m_imageListState
= (wxImageList
*) NULL
;
658 m_ownsImageListNormal
=
659 m_ownsImageListState
= FALSE
;
662 m_isDragging
= FALSE
;
664 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
666 m_renameTimer
= new wxTreeRenameTimer( this );
667 m_lastOnSame
= FALSE
;
669 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
670 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
671 m_normalFont
.GetFamily(),
672 m_normalFont
.GetStyle(),
674 m_normalFont
.GetUnderlined());
677 bool wxGenericTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
678 const wxPoint
& pos
, const wxSize
& size
,
680 const wxValidator
&validator
,
681 const wxString
& name
)
683 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
686 SetValidator( validator
);
689 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
690 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
691 m_dottedPen
= wxPen( "grey", 0, 0 );
696 wxGenericTreeCtrl::~wxGenericTreeCtrl()
698 wxDELETE( m_hilightBrush
);
702 delete m_renameTimer
;
703 if (m_ownsImageListNormal
) delete m_imageListNormal
;
704 if (m_ownsImageListState
) delete m_imageListState
;
707 // -----------------------------------------------------------------------------
709 // -----------------------------------------------------------------------------
711 size_t wxGenericTreeCtrl::GetCount() const
713 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
716 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
722 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
728 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
730 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
732 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
735 // -----------------------------------------------------------------------------
736 // functions to work with tree items
737 // -----------------------------------------------------------------------------
739 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
741 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
743 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
746 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
747 wxTreeItemIcon which
) const
749 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
751 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
754 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
756 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
758 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
761 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
763 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
766 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
767 pItem
->SetText(text
);
768 CalculateSize(pItem
, dc
);
772 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
774 wxTreeItemIcon which
)
776 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
778 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
779 pItem
->SetImage(image
, which
);
782 CalculateSize(pItem
, dc
);
786 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
788 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
790 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
793 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
795 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
797 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
798 pItem
->SetHasPlus(has
);
802 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
804 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
806 // avoid redrawing the tree if no real change
807 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
808 if ( pItem
->IsBold() != bold
)
810 pItem
->SetBold(bold
);
815 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
818 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
820 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
821 pItem
->Attr().SetTextColour(col
);
825 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
828 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
830 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
831 pItem
->Attr().SetBackgroundColour(col
);
835 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
837 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
839 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
840 pItem
->Attr().SetFont(font
);
844 // -----------------------------------------------------------------------------
845 // item status inquiries
846 // -----------------------------------------------------------------------------
848 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
850 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
852 // An item is only visible if it's not a descendant of a collapsed item
853 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
854 wxGenericTreeItem
* parent
= pItem
->GetParent();
857 if (!parent
->IsExpanded())
859 parent
= parent
->GetParent();
863 GetViewStart(& startX
, & startY
);
865 wxSize clientSize
= GetClientSize();
868 if (!GetBoundingRect(item
, rect
))
870 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
872 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
874 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
880 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
882 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
884 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
887 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
889 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
891 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
894 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
896 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
898 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
901 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
903 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
905 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
908 // -----------------------------------------------------------------------------
910 // -----------------------------------------------------------------------------
912 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
914 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
916 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
919 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
921 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
924 return GetNextChild(item
, cookie
);
927 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
929 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
931 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
932 if ( (size_t)cookie
< children
.Count() )
934 return children
.Item((size_t)cookie
++);
938 // there are no more of them
939 return wxTreeItemId();
943 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
945 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
947 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
948 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
951 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
953 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
955 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
956 wxGenericTreeItem
*parent
= i
->GetParent();
957 if ( parent
== NULL
)
959 // root item doesn't have any siblings
960 return wxTreeItemId();
963 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
964 int index
= siblings
.Index(i
);
965 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
967 size_t n
= (size_t)(index
+ 1);
968 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
971 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
973 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
975 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
976 wxGenericTreeItem
*parent
= i
->GetParent();
977 if ( parent
== NULL
)
979 // root item doesn't have any siblings
980 return wxTreeItemId();
983 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
984 int index
= siblings
.Index(i
);
985 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
987 return index
== 0 ? wxTreeItemId()
988 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
991 // Only for internal use right now, but should probably be public
992 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
994 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
996 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
998 // First see if there are any children.
999 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1000 if (children
.GetCount() > 0)
1002 return children
.Item(0);
1006 // Try a sibling of this or ancestor instead
1007 wxTreeItemId p
= item
;
1008 wxTreeItemId toFind
;
1011 toFind
= GetNextSibling(p
);
1013 } while (p
.IsOk() && !toFind
.IsOk());
1018 wxTreeItemId
wxGenericTreeCtrl::GetPrev(const wxTreeItemId
& item
) const
1020 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1022 wxFAIL_MSG(wxT("not implemented"));
1024 return wxTreeItemId();
1027 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1029 wxTreeItemId id
= GetRootItem();
1038 } while (id
.IsOk());
1040 return wxTreeItemId();
1043 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1045 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1047 wxTreeItemId id
= item
;
1052 if (id
.IsOk() && IsVisible(id
))
1055 return wxTreeItemId();
1058 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1060 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1062 wxFAIL_MSG(wxT("not implemented"));
1064 return wxTreeItemId();
1067 // -----------------------------------------------------------------------------
1069 // -----------------------------------------------------------------------------
1071 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1073 const wxString
& text
,
1074 int image
, int selImage
,
1075 wxTreeItemData
*data
)
1077 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1080 // should we give a warning here?
1081 return AddRoot(text
, image
, selImage
, data
);
1084 wxClientDC
dc(this);
1085 wxGenericTreeItem
*item
=
1086 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1090 data
->m_pItem
= (long) item
;
1093 parent
->Insert( item
, previous
);
1100 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1101 int image
, int selImage
,
1102 wxTreeItemData
*data
)
1104 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1106 wxClientDC
dc(this);
1107 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1108 image
, selImage
, data
);
1111 data
->m_pItem
= (long) m_anchor
;
1114 if (!HasFlag(wxTR_MULTIPLE
))
1116 m_current
= m_key_current
= m_anchor
;
1117 m_current
->SetHilight( TRUE
);
1125 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1126 const wxString
& text
,
1127 int image
, int selImage
,
1128 wxTreeItemData
*data
)
1130 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1133 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1134 const wxTreeItemId
& idPrevious
,
1135 const wxString
& text
,
1136 int image
, int selImage
,
1137 wxTreeItemData
*data
)
1139 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1142 // should we give a warning here?
1143 return AddRoot(text
, image
, selImage
, data
);
1146 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1147 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1148 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1150 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1153 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1155 const wxString
& text
,
1156 int image
, int selImage
,
1157 wxTreeItemData
*data
)
1159 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1162 // should we give a warning here?
1163 return AddRoot(text
, image
, selImage
, data
);
1166 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1169 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1170 const wxString
& text
,
1171 int image
, int selImage
,
1172 wxTreeItemData
*data
)
1174 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1177 // should we give a warning here?
1178 return AddRoot(text
, image
, selImage
, data
);
1181 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1182 image
, selImage
, data
);
1185 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1187 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1188 event
.m_item
= (long) item
;
1189 event
.SetEventObject( this );
1190 ProcessEvent( event
);
1193 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1195 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1196 item
->DeleteChildren(this);
1201 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1203 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1205 // don't stay with invalid m_key_current or we will crash in the next call
1207 bool changeKeyCurrent
= FALSE
;
1208 wxGenericTreeItem
*itemKey
= m_key_current
;
1209 while ( itemKey
&& !changeKeyCurrent
)
1211 if ( itemKey
== item
)
1213 // m_key_current is a descendant of the item being deleted
1214 changeKeyCurrent
= TRUE
;
1218 itemKey
= itemKey
->GetParent();
1222 wxGenericTreeItem
*parent
= item
->GetParent();
1225 parent
->GetChildren().Remove( item
); // remove by value
1228 if ( changeKeyCurrent
)
1230 // may be NULL or not
1231 m_key_current
= parent
;
1234 item
->DeleteChildren(this);
1235 SendDeleteEvent(item
);
1241 void wxGenericTreeCtrl::DeleteAllItems()
1245 m_anchor
->DeleteChildren(this);
1254 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1256 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1258 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1260 if ( !item
->HasPlus() )
1263 if ( item
->IsExpanded() )
1266 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1267 event
.m_item
= (long) item
;
1268 event
.SetEventObject( this );
1270 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1272 // cancelled by program
1277 CalculatePositions();
1279 RefreshSubtree(item
);
1281 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1282 ProcessEvent( event
);
1285 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1288 if ( IsExpanded(item
) )
1291 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1292 while ( child
.IsOk() )
1296 child
= GetNextChild(item
, cookie
);
1301 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1303 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1305 if ( !item
->IsExpanded() )
1308 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1309 event
.m_item
= (long) item
;
1310 event
.SetEventObject( this );
1311 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1313 // cancelled by program
1319 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1320 size_t count
= children
.Count();
1321 for ( size_t n
= 0; n
< count
; n
++ )
1323 Collapse(children
[n
]);
1326 CalculatePositions();
1328 RefreshSubtree(item
);
1330 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1331 ProcessEvent( event
);
1334 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1337 DeleteChildren(item
);
1340 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1342 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1344 if (item
->IsExpanded())
1350 void wxGenericTreeCtrl::Unselect()
1354 m_current
->SetHilight( FALSE
);
1355 RefreshLine( m_current
);
1359 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1361 if (item
->IsSelected())
1363 item
->SetHilight(FALSE
);
1367 if (item
->HasChildren())
1369 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1370 size_t count
= children
.Count();
1371 for ( size_t n
= 0; n
< count
; ++n
)
1373 UnselectAllChildren(children
[n
]);
1378 void wxGenericTreeCtrl::UnselectAll()
1380 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1383 // Recursive function !
1384 // To stop we must have crt_item<last_item
1386 // Tag all next children, when no more children,
1387 // Move to parent (not to tag)
1388 // Keep going... if we found last_item, we stop.
1389 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1391 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1393 if (parent
== NULL
) // This is root item
1394 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1396 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1397 int index
= children
.Index(crt_item
);
1398 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1400 size_t count
= children
.Count();
1401 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1403 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1406 return TagNextChildren(parent
, last_item
, select
);
1409 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1411 crt_item
->SetHilight(select
);
1412 RefreshLine(crt_item
);
1414 if (crt_item
==last_item
)
1417 if (crt_item
->HasChildren())
1419 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1420 size_t count
= children
.Count();
1421 for ( size_t n
= 0; n
< count
; ++n
)
1423 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1431 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1433 // item2 is not necessary after item1
1434 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1436 // choice first' and 'last' between item1 and item2
1437 if (item1
->GetY()<item2
->GetY())
1448 bool select
= m_current
->IsSelected();
1450 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1453 TagNextChildren(first
,last
,select
);
1456 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1457 bool unselect_others
,
1458 bool extended_select
)
1460 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1462 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1463 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1465 //wxCHECK_RET( ( (!unselect_others) && is_single),
1466 // wxT("this is a single selection tree") );
1468 // to keep going anyhow !!!
1471 if (item
->IsSelected())
1472 return; // nothing to do
1473 unselect_others
= TRUE
;
1474 extended_select
= FALSE
;
1476 else if ( unselect_others
&& item
->IsSelected() )
1478 // selection change if there is more than one item currently selected
1479 wxArrayTreeItemIds selected_items
;
1480 if ( GetSelections(selected_items
) == 1 )
1484 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1485 event
.m_item
= (long) item
;
1486 event
.m_itemOld
= (long) m_current
;
1487 event
.SetEventObject( this );
1488 // TODO : Here we don't send any selection mode yet !
1490 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1494 if (unselect_others
)
1496 if (is_single
) Unselect(); // to speed up thing
1501 if (extended_select
)
1506 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1509 // don't change the mark (m_current)
1510 SelectItemRange(m_current
, item
);
1514 bool select
=TRUE
; // the default
1516 // Check if we need to toggle hilight (ctrl mode)
1517 if (!unselect_others
)
1518 select
=!item
->IsSelected();
1520 m_current
= m_key_current
= item
;
1521 m_current
->SetHilight(select
);
1522 RefreshLine( m_current
);
1525 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1526 GetEventHandler()->ProcessEvent( event
);
1529 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1530 wxArrayTreeItemIds
&array
) const
1532 if ( item
->IsSelected() )
1533 array
.Add(wxTreeItemId(item
));
1535 if ( item
->HasChildren() )
1537 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1538 size_t count
= children
.GetCount();
1539 for ( size_t n
= 0; n
< count
; ++n
)
1540 FillArray(children
[n
], array
);
1544 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1547 wxTreeItemId idRoot
= GetRootItem();
1548 if ( idRoot
.IsOk() )
1550 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1552 //else: the tree is empty, so no selections
1554 return array
.Count();
1557 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1559 if (!item
.IsOk()) return;
1561 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1563 // first expand all parent branches
1564 wxGenericTreeItem
*parent
= gitem
->GetParent();
1568 parent
= parent
->GetParent();
1571 //if (parent) CalculatePositions();
1576 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1578 if (!item
.IsOk()) return;
1580 // We have to call this here because the label in
1581 // question might just have been added and no screen
1582 // update taken place.
1583 if (m_dirty
) wxYield();
1585 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1587 // now scroll to the item
1588 int item_y
= gitem
->GetY();
1592 ViewStart( &start_x
, &start_y
);
1593 start_y
*= PIXELS_PER_UNIT
;
1597 GetClientSize( &client_w
, &client_h
);
1599 if (item_y
< start_y
+3)
1604 m_anchor
->GetSize( x
, y
, this );
1605 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1606 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1607 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1608 // Item should appear at top
1609 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1611 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1616 m_anchor
->GetSize( x
, y
, this );
1617 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1618 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1619 item_y
+= PIXELS_PER_UNIT
+2;
1620 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1621 // Item should appear at bottom
1622 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
);
1626 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1627 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1629 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1630 wxGenericTreeItem
**item2
)
1632 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1634 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1637 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1638 const wxTreeItemId
& item2
)
1640 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1643 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1645 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1647 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1649 wxCHECK_RET( !s_treeBeingSorted
,
1650 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1652 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1653 if ( children
.Count() > 1 )
1655 s_treeBeingSorted
= this;
1656 children
.Sort(tree_ctrl_compare_func
);
1657 s_treeBeingSorted
= NULL
;
1661 //else: don't make the tree dirty as nothing changed
1664 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1666 return m_imageListNormal
;
1669 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1671 return m_imageListState
;
1674 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1676 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1678 m_imageListNormal
= imageList
;
1679 m_ownsImageListNormal
= FALSE
;
1681 if ( !m_imageListNormal
)
1684 // Calculate a m_lineHeight value from the image sizes.
1685 // May be toggle off. Then wxGenericTreeCtrl will spread when
1686 // necessary (which might look ugly).
1687 wxClientDC
dc(this);
1688 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1689 int width
= 0, height
= 0,
1690 n
= m_imageListNormal
->GetImageCount();
1692 for (int i
= 0; i
< n
; i
++)
1694 m_imageListNormal
->GetSize(i
, width
, height
);
1695 if (height
> m_lineHeight
) m_lineHeight
= height
;
1698 if (m_lineHeight
< 40)
1699 m_lineHeight
+= 2; // at least 2 pixels
1701 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1704 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1706 if (m_ownsImageListState
) delete m_imageListState
;
1707 m_imageListState
= imageList
;
1708 m_ownsImageListState
= FALSE
;
1711 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1713 SetImageList(imageList
);
1714 m_ownsImageListNormal
= TRUE
;
1717 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1719 SetStateImageList(imageList
);
1720 m_ownsImageListState
= TRUE
;
1723 // -----------------------------------------------------------------------------
1725 // -----------------------------------------------------------------------------
1727 void wxGenericTreeCtrl::AdjustMyScrollbars()
1733 m_anchor
->GetSize( x
, y
, this );
1734 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1735 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1736 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1737 int y_pos
= GetScrollPos( wxVERTICAL
);
1738 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1742 SetScrollbars( 0, 0, 0, 0 );
1746 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1748 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1749 return item
->GetHeight();
1751 return m_lineHeight
;
1754 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1756 wxTreeItemAttr
*attr
= item
->GetAttributes();
1757 if ( attr
&& attr
->HasFont() )
1758 dc
.SetFont(attr
->GetFont());
1759 else if (item
->IsBold())
1760 dc
.SetFont(m_boldFont
);
1764 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1768 int image
= item
->GetCurrentImage();
1769 if ( image
!= NO_IMAGE
)
1771 if ( m_imageListNormal
)
1773 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1782 int total_h
= GetLineHeight(item
);
1784 if (item
->IsSelected())
1785 dc
.SetBrush(*m_hilightBrush
);
1789 if ( attr
&& attr
->HasBackgroundColour() )
1790 colBg
= attr
->GetBackgroundColour();
1792 colBg
= m_backgroundColour
;
1793 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1796 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1798 if ( image
!= NO_IMAGE
)
1800 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1801 m_imageListNormal
->Draw( image
, dc
,
1803 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1804 wxIMAGELIST_DRAW_TRANSPARENT
);
1805 dc
.DestroyClippingRegion();
1808 dc
.SetBackgroundMode(wxTRANSPARENT
);
1809 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1810 dc
.DrawText( item
->GetText(),
1811 (wxCoord
)(image_w
+ item
->GetX()),
1812 (wxCoord
)(item
->GetY() + extraH
));
1814 // restore normal font
1815 dc
.SetFont( m_normalFont
);
1818 // Now y stands for the top of the item, whereas it used to stand for middle !
1819 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1821 int horizX
= level
*m_indent
;
1823 item
->SetX( horizX
+m_indent
+m_spacing
);
1827 y
+=GetLineHeight(item
)/2;
1829 item
->SetCross( horizX
+m_indent
, y
);
1831 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1832 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1834 bool drawLines
= ((GetWindowStyle() & wxTR_NO_LINES
) == 0);
1836 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1838 int startX
= horizX
;
1839 int endX
= horizX
+ (m_indent
-5);
1841 // if (!item->HasChildren()) endX += (m_indent+5);
1842 if (!item
->HasChildren()) endX
+= 20;
1845 dc
.DrawLine( startX
, y
, endX
, y
);
1847 if (item
->HasPlus())
1850 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1851 dc
.SetPen( *wxGREY_PEN
);
1852 dc
.SetBrush( *wxWHITE_BRUSH
);
1853 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1855 dc
.SetPen( *wxBLACK_PEN
);
1856 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1857 if (!item
->IsExpanded())
1858 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1860 dc
.SetPen( m_dottedPen
);
1863 wxPen
*pen
= wxTRANSPARENT_PEN
;
1866 if ( item
->IsSelected() )
1868 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1876 wxTreeItemAttr
*attr
= item
->GetAttributes();
1877 if ( attr
&& attr
->HasTextColour() )
1878 colText
= attr
->GetTextColour();
1880 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
);
1884 dc
.SetTextForeground(colText
);
1888 PaintItem(item
, dc
);
1890 // restore DC objects
1891 dc
.SetBrush( *wxWHITE_BRUSH
);
1892 dc
.SetPen( m_dottedPen
);
1893 dc
.SetTextForeground( *wxBLACK
);
1896 y
= oldY
+GetLineHeight(item
);
1898 if (item
->IsExpanded())
1900 oldY
+=GetLineHeight(item
)/2;
1903 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1904 size_t n
, count
= children
.Count();
1905 for ( n
= 0; n
< count
; ++n
)
1908 PaintLevel( children
[n
], dc
, level
+1, y
);
1911 // it may happen that the item is expanded but has no items (when you
1912 // delete all its children for example) - don't draw the vertical line
1916 semiOldY
+=GetLineHeight(children
[--n
])/2;
1918 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1923 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1927 if ( item
->HasPlus() )
1929 // it's a folder, indicate it by a border
1934 // draw a line under the drop target because the item will be
1936 DrawLine(item
, TRUE
/* below */);
1939 SetCursor(wxCURSOR_BULLSEYE
);
1944 SetCursor(wxCURSOR_NO_ENTRY
);
1948 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
1950 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1952 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1954 wxClientDC
dc(this);
1956 dc
.SetLogicalFunction(wxINVERT
);
1957 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1959 int w
= i
->GetWidth() + 2;
1960 int h
= GetLineHeight(i
) + 2;
1962 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
1965 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
1967 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1969 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1971 wxClientDC
dc(this);
1973 dc
.SetLogicalFunction(wxINVERT
);
1979 y
+= GetLineHeight(i
) - 1;
1982 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
1985 // -----------------------------------------------------------------------------
1986 // wxWindows callbacks
1987 // -----------------------------------------------------------------------------
1989 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1997 dc
.SetFont( m_normalFont
);
1998 dc
.SetPen( m_dottedPen
);
2000 // this is now done dynamically
2001 //if(GetImageList() == NULL)
2002 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2005 PaintLevel( m_anchor
, dc
, 0, y
);
2008 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2012 if (m_current
) RefreshLine( m_current
);
2015 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2019 if (m_current
) RefreshLine( m_current
);
2022 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2024 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2025 te
.m_code
= (int)event
.KeyCode();
2026 te
.SetEventObject( this );
2027 GetEventHandler()->ProcessEvent( te
);
2029 if ( (m_current
== 0) || (m_key_current
== 0) )
2035 // how should the selection work for this event?
2036 bool is_multiple
, extended_select
, unselect_others
;
2037 EventFlagsToSelType(GetWindowStyleFlag(),
2039 event
.ControlDown(),
2040 is_multiple
, extended_select
, unselect_others
);
2044 // * : Expand all/Collapse all
2045 // ' ' | return : activate
2046 // up : go up (not last children!)
2048 // left : go to parent
2049 // right : open if parent and go next
2050 // home : go to root
2051 // end : go to last item without opening parents
2052 switch (event
.KeyCode())
2056 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2064 if ( !IsExpanded(m_current
) )
2067 ExpandAll(m_current
);
2070 //else: fall through to Collapse() it
2074 if (IsExpanded(m_current
))
2076 Collapse(m_current
);
2083 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2084 event
.m_item
= (long) m_current
;
2086 event
.SetEventObject( this );
2087 GetEventHandler()->ProcessEvent( event
);
2091 // up goes to the previous sibling or to the last of its children if
2095 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2098 prev
= GetParent( m_key_current
);
2102 wxTreeItemId current
= m_key_current
;
2103 if (current
== GetFirstChild( prev
, cockie
))
2105 // otherwise we return to where we came from
2106 SelectItem( prev
, unselect_others
, extended_select
);
2107 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2108 EnsureVisible( prev
);
2115 while ( IsExpanded(prev
) && HasChildren(prev
) )
2117 wxTreeItemId child
= GetLastChild(prev
);
2124 SelectItem( prev
, unselect_others
, extended_select
);
2125 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2126 EnsureVisible( prev
);
2131 // left arrow goes to the parent
2134 wxTreeItemId prev
= GetParent( m_current
);
2137 EnsureVisible( prev
);
2138 SelectItem( prev
, unselect_others
, extended_select
);
2144 // this works the same as the down arrow except that we also expand the
2145 // item if it wasn't expanded yet
2151 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2154 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2155 SelectItem( child
, unselect_others
, extended_select
);
2156 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2157 EnsureVisible( child
);
2161 wxTreeItemId next
= GetNextSibling( m_key_current
);
2164 wxTreeItemId current
= m_key_current
;
2165 while (current
&& !next
)
2167 current
= GetParent( current
);
2168 if (current
) next
= GetNextSibling( current
);
2173 SelectItem( next
, unselect_others
, extended_select
);
2174 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2175 EnsureVisible( next
);
2181 // <End> selects the last visible tree item
2184 wxTreeItemId last
= GetRootItem();
2186 while ( last
.IsOk() && IsExpanded(last
) )
2188 wxTreeItemId lastChild
= GetLastChild(last
);
2190 // it may happen if the item was expanded but then all of
2191 // its children have been deleted - so IsExpanded() returned
2192 // TRUE, but GetLastChild() returned invalid item
2201 EnsureVisible( last
);
2202 SelectItem( last
, unselect_others
, extended_select
);
2207 // <Home> selects the root item
2210 wxTreeItemId prev
= GetRootItem();
2213 EnsureVisible( prev
);
2214 SelectItem( prev
, unselect_others
, extended_select
);
2224 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2226 // We have to call this here because the label in
2227 // question might just have been added and no screen
2228 // update taken place.
2229 if (m_dirty
) wxYield();
2231 wxClientDC
dc(this);
2233 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2234 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2239 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2240 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2241 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2242 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2245 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2247 return wxTreeItemId();
2250 // get the bounding rectangle of the item (or of its label only)
2251 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2253 bool textOnly
) const
2255 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2257 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2260 GetViewStart(& startX
, & startY
);
2262 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2263 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2264 rect
.width
= i
->GetWidth();
2265 //rect.height = i->GetHeight();
2266 rect
.height
= GetLineHeight(i
);
2273 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2275 if (!item
.IsOk()) return;
2277 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2279 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2280 te
.m_item
= (long) m_currentEdit
;
2281 te
.SetEventObject( this );
2282 GetEventHandler()->ProcessEvent( te
);
2284 if (!te
.IsAllowed()) return;
2286 // We have to call this here because the label in
2287 // question might just have been added and no screen
2288 // update taken place.
2289 if (m_dirty
) wxYield();
2291 wxString s
= m_currentEdit
->GetText();
2292 int x
= m_currentEdit
->GetX();
2293 int y
= m_currentEdit
->GetY();
2294 int w
= m_currentEdit
->GetWidth();
2295 int h
= m_currentEdit
->GetHeight();
2300 int image
= m_currentEdit
->GetCurrentImage();
2301 if ( image
!= NO_IMAGE
)
2303 if ( m_imageListNormal
)
2305 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2310 wxFAIL_MSG(_T("you must create an image list to use images!"));
2314 w
-= image_w
+ 4; // I don't know why +4 is needed
2316 wxClientDC
dc(this);
2318 x
= dc
.LogicalToDeviceX( x
);
2319 y
= dc
.LogicalToDeviceY( y
);
2321 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
2322 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
2326 void wxGenericTreeCtrl::OnRenameTimer()
2331 void wxGenericTreeCtrl::OnRenameAccept()
2333 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2334 le
.m_item
= (long) m_currentEdit
;
2335 le
.SetEventObject( this );
2336 le
.m_label
= m_renameRes
;
2337 GetEventHandler()->ProcessEvent( le
);
2339 if (!le
.IsAllowed()) return;
2341 SetItemText( m_currentEdit
, m_renameRes
);
2344 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2346 if ( !m_anchor
) return;
2348 // we process left mouse up event (enables in-place edit), right down
2349 // (pass to the user code), left dbl click (activate item) and
2350 // dragging/moving events for items drag-and-drop
2351 if ( !(event
.LeftDown() ||
2353 event
.RightDown() ||
2354 event
.LeftDClick() ||
2356 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2363 wxClientDC
dc(this);
2365 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2366 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2369 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2371 if ( event
.Dragging() && !m_isDragging
)
2373 if (m_dragCount
== 0)
2374 m_dragStart
= wxPoint(x
,y
);
2378 if (m_dragCount
!= 3)
2380 // wait until user drags a bit further...
2384 wxEventType command
= event
.RightIsDown()
2385 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2386 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2388 wxTreeEvent
nevent( command
, GetId() );
2389 nevent
.m_item
= (long) m_current
;
2390 nevent
.SetEventObject(this);
2392 // by default the dragging is not supported, the user code must
2393 // explicitly allow the event for it to take place
2396 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2398 // we're going to drag this item
2399 m_isDragging
= TRUE
;
2401 // remember the old cursor because we will change it while
2403 m_oldCursor
= m_cursor
;
2405 // in a single selection control, hide the selection temporarily
2406 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2408 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2410 if ( m_oldSelection
)
2412 m_oldSelection
->SetHilight(FALSE
);
2413 RefreshLine(m_oldSelection
);
2420 else if ( event
.Moving() )
2422 if ( item
!= m_dropTarget
)
2424 // unhighlight the previous drop target
2425 DrawDropEffect(m_dropTarget
);
2427 m_dropTarget
= item
;
2429 // highlight the current drop target if any
2430 DrawDropEffect(m_dropTarget
);
2435 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2437 // erase the highlighting
2438 DrawDropEffect(m_dropTarget
);
2440 // generate the drag end event
2441 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2443 event
.m_item
= (long) item
;
2444 event
.m_pointDrag
= wxPoint(x
, y
);
2445 event
.SetEventObject(this);
2447 (void)GetEventHandler()->ProcessEvent(event
);
2449 m_isDragging
= FALSE
;
2450 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2452 if ( m_oldSelection
)
2454 m_oldSelection
->SetHilight(TRUE
);
2455 RefreshLine(m_oldSelection
);
2456 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2461 SetCursor(m_oldCursor
);
2467 // here we process only the messages which happen on tree items
2471 if (item
== NULL
) return; /* we hit the blank area */
2473 if ( event
.RightDown() )
2475 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2476 nevent
.m_item
= (long) item
;
2478 CalcScrolledPosition(x
, y
,
2479 &nevent
.m_pointDrag
.x
,
2480 &nevent
.m_pointDrag
.y
);
2481 nevent
.SetEventObject(this);
2482 GetEventHandler()->ProcessEvent(nevent
);
2484 else if ( event
.LeftUp() )
2488 if ( (item
== m_current
) &&
2489 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2490 HasFlag(wxTR_EDIT_LABELS
) )
2492 if ( m_renameTimer
->IsRunning() )
2493 m_renameTimer
->Stop();
2495 m_renameTimer
->Start( 100, TRUE
);
2498 m_lastOnSame
= FALSE
;
2503 if ( event
.LeftDown() )
2505 m_lastOnSame
= item
== m_current
;
2508 // how should the selection work for this event?
2509 bool is_multiple
, extended_select
, unselect_others
;
2510 EventFlagsToSelType(GetWindowStyleFlag(),
2512 event
.ControlDown(),
2513 is_multiple
, extended_select
, unselect_others
);
2515 if ( (flags
& wxTREE_HITTEST_ONITEMBUTTON
) && event
.LeftDown() )
2522 SelectItem(item
, unselect_others
, extended_select
);
2524 if ( event
.LeftDClick() )
2526 // double clicking should not start editing the item label
2527 m_renameTimer
->Stop();
2528 m_lastOnSame
= FALSE
;
2530 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2531 nevent
.m_item
= (long) item
;
2533 CalcScrolledPosition(x
, y
,
2534 &nevent
.m_pointDrag
.x
,
2535 &nevent
.m_pointDrag
.y
);
2536 nevent
.SetEventObject( this );
2537 GetEventHandler()->ProcessEvent( nevent
);
2543 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2545 /* after all changes have been done to the tree control,
2546 * we actually redraw the tree when everything is over */
2553 CalculatePositions();
2555 AdjustMyScrollbars();
2558 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2564 dc
.SetFont(m_boldFont
);
2566 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2569 // restore normal font
2570 dc
.SetFont( m_normalFont
);
2574 int image
= item
->GetCurrentImage();
2575 if ( image
!= NO_IMAGE
)
2577 if ( m_imageListNormal
)
2579 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2584 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2587 total_h
+= 2; // at least 2 pixels
2589 total_h
+= total_h
/10; // otherwise 10% extra spacing
2591 item
->SetHeight(total_h
);
2592 if (total_h
>m_lineHeight
)
2593 m_lineHeight
=total_h
;
2595 item
->SetWidth(image_w
+text_w
+2);
2598 // -----------------------------------------------------------------------------
2599 // for developper : y is now the top of the level
2600 // not the middle of it !
2601 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2603 int horizX
= level
*m_indent
;
2605 CalculateSize( item
, dc
);
2608 item
->SetX( horizX
+m_indent
+m_spacing
);
2610 y
+=GetLineHeight(item
);
2612 if ( !item
->IsExpanded() )
2614 // we dont need to calculate collapsed branches
2618 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2619 size_t n
, count
= children
.Count();
2620 for (n
= 0; n
< count
; ++n
)
2621 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2624 void wxGenericTreeCtrl::CalculatePositions()
2626 if ( !m_anchor
) return;
2628 wxClientDC
dc(this);
2631 dc
.SetFont( m_normalFont
);
2633 dc
.SetPen( m_dottedPen
);
2634 //if(GetImageList() == NULL)
2635 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2638 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2641 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2643 if (m_dirty
) return;
2645 wxClientDC
dc(this);
2650 GetClientSize( &cw
, &ch
);
2653 rect
.x
= dc
.LogicalToDeviceX( 0 );
2655 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2658 Refresh( TRUE
, &rect
);
2660 AdjustMyScrollbars();
2663 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2665 if (m_dirty
) return;
2667 wxClientDC
dc(this);
2672 GetClientSize( &cw
, &ch
);
2675 rect
.x
= dc
.LogicalToDeviceX( 0 );
2676 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2678 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2680 Refresh( TRUE
, &rect
);