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 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
79 wxTreeTextCtrl( wxWindow
*parent
,
83 wxGenericTreeCtrl
*owner
,
84 const wxString
&value
= wxEmptyString
,
85 const wxPoint
&pos
= wxDefaultPosition
,
86 const wxSize
&size
= wxDefaultSize
,
87 int style
= wxSIMPLE_BORDER
,
88 const wxValidator
& validator
= wxDefaultValidator
,
89 const wxString
&name
= wxTextCtrlNameStr
);
91 void OnChar( wxKeyEvent
&event
);
92 void OnKeyUp( wxKeyEvent
&event
);
93 void OnKillFocus( wxFocusEvent
&event
);
98 wxGenericTreeCtrl
*m_owner
;
99 wxString m_startValue
;
101 DECLARE_EVENT_TABLE()
102 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
106 class WXDLLEXPORT wxGenericTreeItem
110 wxGenericTreeItem() { m_data
= NULL
; }
111 wxGenericTreeItem( wxGenericTreeItem
*parent
,
112 const wxString
& text
,
114 int image
, int selImage
,
115 wxTreeItemData
*data
);
117 ~wxGenericTreeItem();
120 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
122 const wxString
& GetText() const { return m_text
; }
123 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
124 { return m_images
[which
]; }
125 wxTreeItemData
*GetData() const { return m_data
; }
127 // returns the current image for the item (depending on its
128 // selected/expanded/whatever state)
129 int GetCurrentImage() const;
131 void SetText( const wxString
&text
);
132 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
133 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
135 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
137 void SetBold(bool bold
) { m_isBold
= bold
; }
139 int GetX() const { return m_x
; }
140 int GetY() const { return m_y
; }
142 void SetX(int x
) { m_x
= x
; }
143 void SetY(int y
) { m_y
= y
; }
145 int GetHeight() const { return m_height
; }
146 int GetWidth() const { return m_width
; }
148 void SetHeight(int h
) { m_height
= h
; }
149 void SetWidth(int w
) { m_width
= w
; }
152 wxGenericTreeItem
*GetParent() const { return m_parent
; }
155 // deletes all children notifying the treectrl about it if !NULL
157 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
158 // FIXME don't know what is it for
161 // get count of all children (and grand children if 'recursively')
162 size_t GetChildrenCount(bool recursively
= TRUE
) const;
164 void Insert(wxGenericTreeItem
*child
, size_t index
)
165 { m_children
.Insert(child
, index
); }
167 void SetCross( int x
, int y
);
168 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
170 // return the item at given position (or NULL if no item), onButton is
171 // TRUE if the point belongs to the item's button, otherwise it lies
172 // on the button's label
173 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxGenericTreeCtrl
*, int &flags
);
175 void Expand() { m_isCollapsed
= FALSE
; }
176 void Collapse() { m_isCollapsed
= TRUE
; }
178 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
181 bool HasChildren() const { return !m_children
.IsEmpty(); }
182 bool IsSelected() const { return m_hasHilight
!= 0; }
183 bool IsExpanded() const { return !m_isCollapsed
; }
184 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
185 bool IsBold() const { return m_isBold
!= 0; }
188 // get them - may be NULL
189 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
190 // get them ensuring that the pointer is not NULL
191 wxTreeItemAttr
& Attr()
194 m_attr
= new wxTreeItemAttr
;
202 // tree ctrl images for the normal, selected, expanded and
203 // expanded+selected states
204 int m_images
[wxTreeItemIcon_Max
];
206 wxTreeItemData
*m_data
;
208 // use bitfields to save size
209 int m_isCollapsed
:1;
210 int m_hasHilight
:1; // same as focused
211 int m_hasPlus
:1; // used for item which doesn't have
212 // children but has a [+] button
213 int m_isBold
:1; // render the label in bold font
216 wxCoord m_height
, m_width
;
217 int m_xCross
, m_yCross
;
220 wxArrayGenericTreeItems m_children
;
221 wxGenericTreeItem
*m_parent
;
223 wxTreeItemAttr
*m_attr
;
226 // =============================================================================
228 // =============================================================================
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 // translate the key or mouse event flags to the type of selection we're
236 static void EventFlagsToSelType(long style
,
240 bool &extended_select
,
241 bool &unselect_others
)
243 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
244 extended_select
= shiftDown
&& is_multiple
;
245 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
248 // -----------------------------------------------------------------------------
249 // wxTreeRenameTimer (internal)
250 // -----------------------------------------------------------------------------
252 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
257 void wxTreeRenameTimer::Notify()
259 m_owner
->OnRenameTimer();
262 //-----------------------------------------------------------------------------
263 // wxTreeTextCtrl (internal)
264 //-----------------------------------------------------------------------------
266 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
268 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
269 EVT_CHAR (wxTreeTextCtrl::OnChar
)
270 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
271 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
274 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
278 wxGenericTreeCtrl
*owner
,
279 const wxString
&value
,
283 const wxValidator
& validator
,
284 const wxString
&name
)
285 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
291 (*m_res
) = wxEmptyString
;
292 m_startValue
= value
;
295 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
297 if (event
.m_keyCode
== WXK_RETURN
)
300 (*m_res
) = GetValue();
302 if (!wxPendingDelete
.Member(this))
303 wxPendingDelete
.Append(this);
305 if ((*m_accept
) && ((*m_res
) != m_startValue
))
306 m_owner
->OnRenameAccept();
310 if (event
.m_keyCode
== WXK_ESCAPE
)
315 if (!wxPendingDelete
.Member(this))
316 wxPendingDelete
.Append(this);
323 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
325 // auto-grow the textctrl:
326 wxSize parentSize
= m_owner
->GetSize();
327 wxPoint myPos
= GetPosition();
328 wxSize mySize
= GetSize();
330 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
331 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
332 if (mySize
.x
> sx
) sx
= mySize
.x
;
338 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
340 if (!wxPendingDelete
.Member(this))
341 wxPendingDelete
.Append(this);
343 if ((*m_accept
) && ((*m_res
) != m_startValue
))
344 m_owner
->OnRenameAccept();
348 // -----------------------------------------------------------------------------
350 // -----------------------------------------------------------------------------
352 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
354 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
355 : wxNotifyEvent( commandType
, id
)
358 m_itemOld
= (wxGenericTreeItem
*)NULL
;
362 // -----------------------------------------------------------------------------
364 // -----------------------------------------------------------------------------
366 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
367 const wxString
& text
,
369 int image
, int selImage
,
370 wxTreeItemData
*data
)
373 m_images
[wxTreeItemIcon_Normal
] = image
;
374 m_images
[wxTreeItemIcon_Selected
] = selImage
;
375 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
376 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
380 m_xCross
= m_yCross
= 0;
384 m_isCollapsed
= TRUE
;
385 m_hasHilight
= FALSE
;
391 m_attr
= (wxTreeItemAttr
*)NULL
;
393 // We don't know the height here yet.
398 wxGenericTreeItem::~wxGenericTreeItem()
404 wxASSERT_MSG( m_children
.IsEmpty(),
405 wxT("please call DeleteChildren() before deleting the item") );
408 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
410 size_t count
= m_children
.Count();
411 for ( size_t n
= 0; n
< count
; n
++ )
413 wxGenericTreeItem
*child
= m_children
[n
];
415 tree
->SendDeleteEvent(child
);
417 child
->DeleteChildren(tree
);
424 void wxGenericTreeItem::SetText( const wxString
&text
)
429 void wxGenericTreeItem::Reset()
432 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
434 m_images
[i
] = NO_IMAGE
;
439 m_height
= m_width
= 0;
446 m_isCollapsed
= TRUE
;
448 m_parent
= (wxGenericTreeItem
*)NULL
;
451 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
453 size_t count
= m_children
.Count();
457 size_t total
= count
;
458 for (size_t n
= 0; n
< count
; ++n
)
460 total
+= m_children
[n
]->GetChildrenCount();
466 void wxGenericTreeItem::SetCross( int x
, int y
)
472 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxGenericTreeCtrl
*theTree
)
474 int bottomY
=m_y
+theTree
->GetLineHeight(this);
475 if ( y
< bottomY
) y
= bottomY
;
476 int width
= m_x
+ m_width
;
477 if ( x
< width
) x
= width
;
481 size_t count
= m_children
.Count();
482 for ( size_t n
= 0; n
< count
; ++n
)
484 m_children
[n
]->GetSize( x
, y
, theTree
);
489 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
490 const wxGenericTreeCtrl
*theTree
,
493 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
495 if (point
.y
< m_y
+theTree
->GetLineHeight(this)/2 )
496 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
498 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
500 // 5 is the size of the plus sign
501 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
502 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
503 (IsExpanded() || HasPlus()))
505 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
509 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
514 // assuming every image (normal and selected ) has the same size !
515 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
516 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
518 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
519 flags
|= wxTREE_HITTEST_ONITEMICON
;
521 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
527 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
528 if (point
.x
> m_x
+m_width
)
529 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
537 size_t count
= m_children
.Count();
538 for ( size_t n
= 0; n
< count
; n
++ )
540 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
547 flags
|=wxTREE_HITTEST_NOWHERE
;
549 return (wxGenericTreeItem
*) NULL
;
552 int wxGenericTreeItem::GetCurrentImage() const
554 int image
= NO_IMAGE
;
559 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
562 if ( image
== NO_IMAGE
)
564 // we usually fall back to the normal item, but try just the
565 // expanded one (and not selected) first in this case
566 image
= GetImage(wxTreeItemIcon_Expanded
);
572 image
= GetImage(wxTreeItemIcon_Selected
);
575 // may be it doesn't have the specific image we want, try the default one
577 if ( image
== NO_IMAGE
)
585 // -----------------------------------------------------------------------------
586 // wxGenericTreeCtrl implementation
587 // -----------------------------------------------------------------------------
589 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
591 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
592 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
593 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
594 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
595 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
596 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
597 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
600 #if !defined(__WXMSW__) || defined(__WIN16__)
602 * wxTreeCtrl has to be a real class or we have problems with
603 * the run-time information.
606 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
609 // -----------------------------------------------------------------------------
610 // construction/destruction
611 // -----------------------------------------------------------------------------
613 void wxGenericTreeCtrl::Init()
617 m_anchor
= (wxGenericTreeItem
*) NULL
;
627 m_hilightBrush
= new wxBrush
629 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
634 m_imageListState
= (wxImageList
*) NULL
;
635 m_ownsImageListNormal
=
636 m_ownsImageListState
= FALSE
;
639 m_isDragging
= FALSE
;
641 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
643 m_renameTimer
= new wxTreeRenameTimer( this );
644 m_lastOnSame
= FALSE
;
646 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
647 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
648 m_normalFont
.GetFamily(),
649 m_normalFont
.GetStyle(),
651 m_normalFont
.GetUnderlined());
654 bool wxGenericTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
655 const wxPoint
& pos
, const wxSize
& size
,
657 const wxValidator
&validator
,
658 const wxString
& name
)
660 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
663 SetValidator( validator
);
666 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
667 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
668 m_dottedPen
= wxPen( "grey", 0, 0 );
673 wxGenericTreeCtrl::~wxGenericTreeCtrl()
675 wxDELETE( m_hilightBrush
);
679 delete m_renameTimer
;
680 if (m_ownsImageListNormal
) delete m_imageListNormal
;
681 if (m_ownsImageListState
) delete m_imageListState
;
684 // -----------------------------------------------------------------------------
686 // -----------------------------------------------------------------------------
688 size_t wxGenericTreeCtrl::GetCount() const
690 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
693 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
699 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
705 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
707 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
709 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
712 // -----------------------------------------------------------------------------
713 // functions to work with tree items
714 // -----------------------------------------------------------------------------
716 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
718 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
720 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
723 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
724 wxTreeItemIcon which
) const
726 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
728 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
731 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
733 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
735 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
738 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
740 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
743 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
744 pItem
->SetText(text
);
745 CalculateSize(pItem
, dc
);
749 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
751 wxTreeItemIcon which
)
753 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
755 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
756 pItem
->SetImage(image
, which
);
759 CalculateSize(pItem
, dc
);
763 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
765 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
767 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
770 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
772 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
774 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
775 pItem
->SetHasPlus(has
);
779 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
781 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
783 // avoid redrawing the tree if no real change
784 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
785 if ( pItem
->IsBold() != bold
)
787 pItem
->SetBold(bold
);
792 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
795 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
797 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
798 pItem
->Attr().SetTextColour(col
);
802 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
805 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
807 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
808 pItem
->Attr().SetBackgroundColour(col
);
812 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
814 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
816 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
817 pItem
->Attr().SetFont(font
);
821 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
823 wxScrolledWindow::SetFont(font
);
825 m_normalFont
= font
;
826 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
827 m_normalFont
.GetFamily(),
828 m_normalFont
.GetStyle(),
830 m_normalFont
.GetUnderlined());
836 // -----------------------------------------------------------------------------
837 // item status inquiries
838 // -----------------------------------------------------------------------------
840 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
842 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
844 // An item is only visible if it's not a descendant of a collapsed item
845 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
846 wxGenericTreeItem
* parent
= pItem
->GetParent();
849 if (!parent
->IsExpanded())
851 parent
= parent
->GetParent();
855 GetViewStart(& startX
, & startY
);
857 wxSize clientSize
= GetClientSize();
860 if (!GetBoundingRect(item
, rect
))
862 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
864 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
866 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
872 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
874 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
876 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
879 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
881 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
883 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
886 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
888 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
890 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
893 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
895 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
897 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
900 // -----------------------------------------------------------------------------
902 // -----------------------------------------------------------------------------
904 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
906 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
908 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
911 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
913 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
916 return GetNextChild(item
, cookie
);
919 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
921 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
923 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
924 if ( (size_t)cookie
< children
.Count() )
926 return children
.Item((size_t)cookie
++);
930 // there are no more of them
931 return wxTreeItemId();
935 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
937 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
939 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
940 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
943 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
945 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
947 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
948 wxGenericTreeItem
*parent
= i
->GetParent();
949 if ( parent
== NULL
)
951 // root item doesn't have any siblings
952 return wxTreeItemId();
955 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
956 int index
= siblings
.Index(i
);
957 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
959 size_t n
= (size_t)(index
+ 1);
960 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
963 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
965 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
967 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
968 wxGenericTreeItem
*parent
= i
->GetParent();
969 if ( parent
== NULL
)
971 // root item doesn't have any siblings
972 return wxTreeItemId();
975 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
976 int index
= siblings
.Index(i
);
977 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
979 return index
== 0 ? wxTreeItemId()
980 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
983 // Only for internal use right now, but should probably be public
984 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
986 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
988 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
990 // First see if there are any children.
991 wxArrayGenericTreeItems
& children
= i
->GetChildren();
992 if (children
.GetCount() > 0)
994 return children
.Item(0);
998 // Try a sibling of this or ancestor instead
999 wxTreeItemId p
= item
;
1000 wxTreeItemId toFind
;
1003 toFind
= GetNextSibling(p
);
1005 } while (p
.IsOk() && !toFind
.IsOk());
1010 wxTreeItemId
wxGenericTreeCtrl::GetPrev(const wxTreeItemId
& item
) const
1012 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1014 wxFAIL_MSG(wxT("not implemented"));
1016 return wxTreeItemId();
1019 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1021 wxTreeItemId id
= GetRootItem();
1030 } while (id
.IsOk());
1032 return wxTreeItemId();
1035 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1037 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1039 wxTreeItemId id
= item
;
1044 if (id
.IsOk() && IsVisible(id
))
1047 return wxTreeItemId();
1050 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1052 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1054 wxFAIL_MSG(wxT("not implemented"));
1056 return wxTreeItemId();
1059 // -----------------------------------------------------------------------------
1061 // -----------------------------------------------------------------------------
1063 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1065 const wxString
& text
,
1066 int image
, int selImage
,
1067 wxTreeItemData
*data
)
1069 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1072 // should we give a warning here?
1073 return AddRoot(text
, image
, selImage
, data
);
1076 wxClientDC
dc(this);
1077 wxGenericTreeItem
*item
=
1078 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1082 data
->m_pItem
= (long) item
;
1085 parent
->Insert( item
, previous
);
1092 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1093 int image
, int selImage
,
1094 wxTreeItemData
*data
)
1096 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1098 wxClientDC
dc(this);
1099 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1100 image
, selImage
, data
);
1103 data
->m_pItem
= (long) m_anchor
;
1106 if (!HasFlag(wxTR_MULTIPLE
))
1108 m_current
= m_key_current
= m_anchor
;
1109 m_current
->SetHilight( TRUE
);
1117 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1118 const wxString
& text
,
1119 int image
, int selImage
,
1120 wxTreeItemData
*data
)
1122 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1125 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1126 const wxTreeItemId
& idPrevious
,
1127 const wxString
& text
,
1128 int image
, int selImage
,
1129 wxTreeItemData
*data
)
1131 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1134 // should we give a warning here?
1135 return AddRoot(text
, image
, selImage
, data
);
1138 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1139 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1140 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1142 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1145 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1147 const wxString
& text
,
1148 int image
, int selImage
,
1149 wxTreeItemData
*data
)
1151 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1154 // should we give a warning here?
1155 return AddRoot(text
, image
, selImage
, data
);
1158 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1161 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1162 const wxString
& text
,
1163 int image
, int selImage
,
1164 wxTreeItemData
*data
)
1166 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1169 // should we give a warning here?
1170 return AddRoot(text
, image
, selImage
, data
);
1173 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1174 image
, selImage
, data
);
1177 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1179 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1180 event
.m_item
= (long) item
;
1181 event
.SetEventObject( this );
1182 ProcessEvent( event
);
1185 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1187 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1188 item
->DeleteChildren(this);
1193 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1195 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1197 // don't stay with invalid m_key_current or we will crash in the next call
1199 bool changeKeyCurrent
= FALSE
;
1200 wxGenericTreeItem
*itemKey
= m_key_current
;
1201 while ( itemKey
&& !changeKeyCurrent
)
1203 if ( itemKey
== item
)
1205 // m_key_current is a descendant of the item being deleted
1206 changeKeyCurrent
= TRUE
;
1210 itemKey
= itemKey
->GetParent();
1214 wxGenericTreeItem
*parent
= item
->GetParent();
1217 parent
->GetChildren().Remove( item
); // remove by value
1220 if ( changeKeyCurrent
)
1222 // may be NULL or not
1223 m_key_current
= parent
;
1226 item
->DeleteChildren(this);
1227 SendDeleteEvent(item
);
1233 void wxGenericTreeCtrl::DeleteAllItems()
1237 m_anchor
->DeleteChildren(this);
1246 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1248 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1250 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1252 if ( !item
->HasPlus() )
1255 if ( item
->IsExpanded() )
1258 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1259 event
.m_item
= (long) item
;
1260 event
.SetEventObject( this );
1262 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1264 // cancelled by program
1269 CalculatePositions();
1271 RefreshSubtree(item
);
1273 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1274 ProcessEvent( event
);
1277 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1280 if ( IsExpanded(item
) )
1283 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1284 while ( child
.IsOk() )
1288 child
= GetNextChild(item
, cookie
);
1293 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1295 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1297 if ( !item
->IsExpanded() )
1300 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1301 event
.m_item
= (long) item
;
1302 event
.SetEventObject( this );
1303 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1305 // cancelled by program
1311 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1312 size_t count
= children
.Count();
1313 for ( size_t n
= 0; n
< count
; n
++ )
1315 Collapse(children
[n
]);
1318 CalculatePositions();
1320 RefreshSubtree(item
);
1322 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1323 ProcessEvent( event
);
1326 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1329 DeleteChildren(item
);
1332 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1334 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1336 if (item
->IsExpanded())
1342 void wxGenericTreeCtrl::Unselect()
1346 m_current
->SetHilight( FALSE
);
1347 RefreshLine( m_current
);
1351 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1353 if (item
->IsSelected())
1355 item
->SetHilight(FALSE
);
1359 if (item
->HasChildren())
1361 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1362 size_t count
= children
.Count();
1363 for ( size_t n
= 0; n
< count
; ++n
)
1365 UnselectAllChildren(children
[n
]);
1370 void wxGenericTreeCtrl::UnselectAll()
1372 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1375 // Recursive function !
1376 // To stop we must have crt_item<last_item
1378 // Tag all next children, when no more children,
1379 // Move to parent (not to tag)
1380 // Keep going... if we found last_item, we stop.
1381 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1383 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1385 if (parent
== NULL
) // This is root item
1386 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1388 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1389 int index
= children
.Index(crt_item
);
1390 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1392 size_t count
= children
.Count();
1393 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1395 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1398 return TagNextChildren(parent
, last_item
, select
);
1401 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1403 crt_item
->SetHilight(select
);
1404 RefreshLine(crt_item
);
1406 if (crt_item
==last_item
)
1409 if (crt_item
->HasChildren())
1411 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1412 size_t count
= children
.Count();
1413 for ( size_t n
= 0; n
< count
; ++n
)
1415 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1423 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1425 // item2 is not necessary after item1
1426 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1428 // choice first' and 'last' between item1 and item2
1429 if (item1
->GetY()<item2
->GetY())
1440 bool select
= m_current
->IsSelected();
1442 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1445 TagNextChildren(first
,last
,select
);
1448 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1449 bool unselect_others
,
1450 bool extended_select
)
1452 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1454 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1455 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1457 //wxCHECK_RET( ( (!unselect_others) && is_single),
1458 // wxT("this is a single selection tree") );
1460 // to keep going anyhow !!!
1463 if (item
->IsSelected())
1464 return; // nothing to do
1465 unselect_others
= TRUE
;
1466 extended_select
= FALSE
;
1468 else if ( unselect_others
&& item
->IsSelected() )
1470 // selection change if there is more than one item currently selected
1471 wxArrayTreeItemIds selected_items
;
1472 if ( GetSelections(selected_items
) == 1 )
1476 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1477 event
.m_item
= (long) item
;
1478 event
.m_itemOld
= (long) m_current
;
1479 event
.SetEventObject( this );
1480 // TODO : Here we don't send any selection mode yet !
1482 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1486 if (unselect_others
)
1488 if (is_single
) Unselect(); // to speed up thing
1493 if (extended_select
)
1498 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1501 // don't change the mark (m_current)
1502 SelectItemRange(m_current
, item
);
1506 bool select
=TRUE
; // the default
1508 // Check if we need to toggle hilight (ctrl mode)
1509 if (!unselect_others
)
1510 select
=!item
->IsSelected();
1512 m_current
= m_key_current
= item
;
1513 m_current
->SetHilight(select
);
1514 RefreshLine( m_current
);
1517 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1518 GetEventHandler()->ProcessEvent( event
);
1521 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1522 wxArrayTreeItemIds
&array
) const
1524 if ( item
->IsSelected() )
1525 array
.Add(wxTreeItemId(item
));
1527 if ( item
->HasChildren() )
1529 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1530 size_t count
= children
.GetCount();
1531 for ( size_t n
= 0; n
< count
; ++n
)
1532 FillArray(children
[n
], array
);
1536 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1539 wxTreeItemId idRoot
= GetRootItem();
1540 if ( idRoot
.IsOk() )
1542 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1544 //else: the tree is empty, so no selections
1546 return array
.Count();
1549 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1551 if (!item
.IsOk()) return;
1553 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1555 // first expand all parent branches
1556 wxGenericTreeItem
*parent
= gitem
->GetParent();
1560 parent
= parent
->GetParent();
1563 //if (parent) CalculatePositions();
1568 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1570 if (!item
.IsOk()) return;
1572 // We have to call this here because the label in
1573 // question might just have been added and no screen
1574 // update taken place.
1575 if (m_dirty
) wxYieldIfNeeded();
1577 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1579 // now scroll to the item
1580 int item_y
= gitem
->GetY();
1584 ViewStart( &start_x
, &start_y
);
1585 start_y
*= PIXELS_PER_UNIT
;
1589 GetClientSize( &client_w
, &client_h
);
1591 if (item_y
< start_y
+3)
1596 m_anchor
->GetSize( x
, y
, this );
1597 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1598 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1599 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1600 // Item should appear at top
1601 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1603 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1608 m_anchor
->GetSize( x
, y
, this );
1609 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1610 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1611 item_y
+= PIXELS_PER_UNIT
+2;
1612 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1613 // Item should appear at bottom
1614 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
);
1618 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1619 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1621 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1622 wxGenericTreeItem
**item2
)
1624 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1626 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1629 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1630 const wxTreeItemId
& item2
)
1632 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1635 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1637 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1639 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1641 wxCHECK_RET( !s_treeBeingSorted
,
1642 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1644 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1645 if ( children
.Count() > 1 )
1647 s_treeBeingSorted
= this;
1648 children
.Sort(tree_ctrl_compare_func
);
1649 s_treeBeingSorted
= NULL
;
1653 //else: don't make the tree dirty as nothing changed
1656 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1658 return m_imageListNormal
;
1661 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1663 return m_imageListState
;
1666 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1668 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1670 m_imageListNormal
= imageList
;
1671 m_ownsImageListNormal
= FALSE
;
1673 if ( !m_imageListNormal
)
1676 // Calculate a m_lineHeight value from the image sizes.
1677 // May be toggle off. Then wxGenericTreeCtrl will spread when
1678 // necessary (which might look ugly).
1679 wxClientDC
dc(this);
1680 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1681 int width
= 0, height
= 0,
1682 n
= m_imageListNormal
->GetImageCount();
1684 for (int i
= 0; i
< n
; i
++)
1686 m_imageListNormal
->GetSize(i
, width
, height
);
1687 if (height
> m_lineHeight
) m_lineHeight
= height
;
1690 if (m_lineHeight
< 40)
1691 m_lineHeight
+= 2; // at least 2 pixels
1693 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1696 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1698 if (m_ownsImageListState
) delete m_imageListState
;
1699 m_imageListState
= imageList
;
1700 m_ownsImageListState
= FALSE
;
1703 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1705 SetImageList(imageList
);
1706 m_ownsImageListNormal
= TRUE
;
1709 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1711 SetStateImageList(imageList
);
1712 m_ownsImageListState
= TRUE
;
1715 // -----------------------------------------------------------------------------
1717 // -----------------------------------------------------------------------------
1719 void wxGenericTreeCtrl::AdjustMyScrollbars()
1725 m_anchor
->GetSize( x
, y
, this );
1726 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1727 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1728 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1729 int y_pos
= GetScrollPos( wxVERTICAL
);
1730 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1734 SetScrollbars( 0, 0, 0, 0 );
1738 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1740 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1741 return item
->GetHeight();
1743 return m_lineHeight
;
1746 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1748 wxTreeItemAttr
*attr
= item
->GetAttributes();
1749 if ( attr
&& attr
->HasFont() )
1750 dc
.SetFont(attr
->GetFont());
1751 else if (item
->IsBold())
1752 dc
.SetFont(m_boldFont
);
1756 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1760 int image
= item
->GetCurrentImage();
1761 if ( image
!= NO_IMAGE
)
1763 if ( m_imageListNormal
)
1765 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1774 int total_h
= GetLineHeight(item
);
1776 if (item
->IsSelected())
1777 dc
.SetBrush(*m_hilightBrush
);
1781 if ( attr
&& attr
->HasBackgroundColour() )
1782 colBg
= attr
->GetBackgroundColour();
1784 colBg
= m_backgroundColour
;
1785 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1788 if (item
->IsSelected() && image
!= NO_IMAGE
)
1790 // If it's selected, and there's an image, then we should
1791 // take care to leave the area under the image painted in the
1792 // background colour.
1793 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY(), item
->GetWidth() - image_w
+ 2, total_h
);
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 // JACS: removed this because the yield can cause the window to be
2230 // deleted from under us if a close window event is pending
2231 // if (m_dirty) wxYieldIfNeeded();
2233 wxClientDC
dc(this);
2235 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2236 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2241 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2242 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2243 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2244 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2247 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2249 return wxTreeItemId();
2252 // get the bounding rectangle of the item (or of its label only)
2253 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2255 bool WXUNUSED(textOnly
)) const
2257 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2259 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2262 GetViewStart(& startX
, & startY
);
2264 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2265 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2266 rect
.width
= i
->GetWidth();
2267 //rect.height = i->GetHeight();
2268 rect
.height
= GetLineHeight(i
);
2275 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2277 if (!item
.IsOk()) return;
2279 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2281 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2282 te
.m_item
= (long) m_currentEdit
;
2283 te
.SetEventObject( this );
2284 GetEventHandler()->ProcessEvent( te
);
2286 if (!te
.IsAllowed()) return;
2288 // We have to call this here because the label in
2289 // question might just have been added and no screen
2290 // update taken place.
2291 if (m_dirty
) wxYieldIfNeeded();
2293 wxString s
= m_currentEdit
->GetText();
2294 int x
= m_currentEdit
->GetX();
2295 int y
= m_currentEdit
->GetY();
2296 int w
= m_currentEdit
->GetWidth();
2297 int h
= m_currentEdit
->GetHeight();
2302 int image
= m_currentEdit
->GetCurrentImage();
2303 if ( image
!= NO_IMAGE
)
2305 if ( m_imageListNormal
)
2307 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2312 wxFAIL_MSG(_T("you must create an image list to use images!"));
2316 w
-= image_w
+ 4; // I don't know why +4 is needed
2318 wxClientDC
dc(this);
2320 x
= dc
.LogicalToDeviceX( x
);
2321 y
= dc
.LogicalToDeviceY( y
);
2323 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2333 void wxGenericTreeCtrl::OnRenameTimer()
2338 void wxGenericTreeCtrl::OnRenameAccept()
2340 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2341 le
.m_item
= (long) m_currentEdit
;
2342 le
.SetEventObject( this );
2343 le
.m_label
= m_renameRes
;
2344 GetEventHandler()->ProcessEvent( le
);
2346 if (!le
.IsAllowed()) return;
2348 SetItemText( m_currentEdit
, m_renameRes
);
2351 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2353 if ( !m_anchor
) return;
2355 // we process left mouse up event (enables in-place edit), right down
2356 // (pass to the user code), left dbl click (activate item) and
2357 // dragging/moving events for items drag-and-drop
2358 if ( !(event
.LeftDown() ||
2360 event
.RightDown() ||
2361 event
.LeftDClick() ||
2363 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2370 wxClientDC
dc(this);
2372 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2373 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2376 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2378 if ( event
.Dragging() && !m_isDragging
)
2380 if (m_dragCount
== 0)
2381 m_dragStart
= wxPoint(x
,y
);
2385 if (m_dragCount
!= 3)
2387 // wait until user drags a bit further...
2391 wxEventType command
= event
.RightIsDown()
2392 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2393 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2395 wxTreeEvent
nevent( command
, GetId() );
2396 nevent
.m_item
= (long) m_current
;
2397 nevent
.SetEventObject(this);
2399 // by default the dragging is not supported, the user code must
2400 // explicitly allow the event for it to take place
2403 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2405 // we're going to drag this item
2406 m_isDragging
= TRUE
;
2408 // remember the old cursor because we will change it while
2410 m_oldCursor
= m_cursor
;
2412 // in a single selection control, hide the selection temporarily
2413 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2415 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2417 if ( m_oldSelection
)
2419 m_oldSelection
->SetHilight(FALSE
);
2420 RefreshLine(m_oldSelection
);
2427 else if ( event
.Moving() )
2429 if ( item
!= m_dropTarget
)
2431 // unhighlight the previous drop target
2432 DrawDropEffect(m_dropTarget
);
2434 m_dropTarget
= item
;
2436 // highlight the current drop target if any
2437 DrawDropEffect(m_dropTarget
);
2442 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2444 // erase the highlighting
2445 DrawDropEffect(m_dropTarget
);
2447 // generate the drag end event
2448 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2450 event
.m_item
= (long) item
;
2451 event
.m_pointDrag
= wxPoint(x
, y
);
2452 event
.SetEventObject(this);
2454 (void)GetEventHandler()->ProcessEvent(event
);
2456 m_isDragging
= FALSE
;
2457 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2459 if ( m_oldSelection
)
2461 m_oldSelection
->SetHilight(TRUE
);
2462 RefreshLine(m_oldSelection
);
2463 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2468 SetCursor(m_oldCursor
);
2474 // here we process only the messages which happen on tree items
2478 if (item
== NULL
) return; /* we hit the blank area */
2480 if ( event
.RightDown() )
2482 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2483 nevent
.m_item
= (long) item
;
2485 CalcScrolledPosition(x
, y
,
2486 &nevent
.m_pointDrag
.x
,
2487 &nevent
.m_pointDrag
.y
);
2488 nevent
.SetEventObject(this);
2489 GetEventHandler()->ProcessEvent(nevent
);
2491 else if ( event
.LeftUp() )
2495 if ( (item
== m_current
) &&
2496 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2497 HasFlag(wxTR_EDIT_LABELS
) )
2499 if ( m_renameTimer
->IsRunning() )
2500 m_renameTimer
->Stop();
2502 m_renameTimer
->Start( 100, TRUE
);
2505 m_lastOnSame
= FALSE
;
2508 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2510 if ( event
.LeftDown() )
2512 m_lastOnSame
= item
== m_current
;
2515 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2517 // only toggle the item for a single click, double click on
2518 // the button doesn't do anything (it toggles the item twice)
2519 if ( event
.LeftDown() )
2524 // don't select the item if the button was clicked
2528 // how should the selection work for this event?
2529 bool is_multiple
, extended_select
, unselect_others
;
2530 EventFlagsToSelType(GetWindowStyleFlag(),
2532 event
.ControlDown(),
2533 is_multiple
, extended_select
, unselect_others
);
2535 SelectItem(item
, unselect_others
, extended_select
);
2537 if ( event
.LeftDClick() )
2539 // double clicking should not start editing the item label
2540 m_renameTimer
->Stop();
2541 m_lastOnSame
= FALSE
;
2543 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2544 nevent
.m_item
= (long) item
;
2546 CalcScrolledPosition(x
, y
,
2547 &nevent
.m_pointDrag
.x
,
2548 &nevent
.m_pointDrag
.y
);
2549 nevent
.SetEventObject( this );
2550 GetEventHandler()->ProcessEvent( nevent
);
2556 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2558 /* after all changes have been done to the tree control,
2559 * we actually redraw the tree when everything is over */
2566 CalculatePositions();
2568 AdjustMyScrollbars();
2571 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2577 dc
.SetFont(m_boldFont
);
2579 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2582 // restore normal font
2583 dc
.SetFont( m_normalFont
);
2587 int image
= item
->GetCurrentImage();
2588 if ( image
!= NO_IMAGE
)
2590 if ( m_imageListNormal
)
2592 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2597 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2600 total_h
+= 2; // at least 2 pixels
2602 total_h
+= total_h
/10; // otherwise 10% extra spacing
2604 item
->SetHeight(total_h
);
2605 if (total_h
>m_lineHeight
)
2606 m_lineHeight
=total_h
;
2608 item
->SetWidth(image_w
+text_w
+2);
2611 // -----------------------------------------------------------------------------
2612 // for developper : y is now the top of the level
2613 // not the middle of it !
2614 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2616 int horizX
= level
*m_indent
;
2618 CalculateSize( item
, dc
);
2621 item
->SetX( horizX
+m_indent
+m_spacing
);
2623 y
+=GetLineHeight(item
);
2625 if ( !item
->IsExpanded() )
2627 // we dont need to calculate collapsed branches
2631 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2632 size_t n
, count
= children
.Count();
2633 for (n
= 0; n
< count
; ++n
)
2634 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2637 void wxGenericTreeCtrl::CalculatePositions()
2639 if ( !m_anchor
) return;
2641 wxClientDC
dc(this);
2644 dc
.SetFont( m_normalFont
);
2646 dc
.SetPen( m_dottedPen
);
2647 //if(GetImageList() == NULL)
2648 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2651 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2654 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2656 if (m_dirty
) return;
2658 wxClientDC
dc(this);
2663 GetClientSize( &cw
, &ch
);
2666 rect
.x
= dc
.LogicalToDeviceX( 0 );
2668 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2671 Refresh( TRUE
, &rect
);
2673 AdjustMyScrollbars();
2676 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2678 if (m_dirty
) return;
2680 wxClientDC
dc(this);
2685 GetClientSize( &cw
, &ch
);
2688 rect
.x
= dc
.LogicalToDeviceX( 0 );
2689 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2691 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2693 Refresh( TRUE
, &rect
);