1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/treebase.h"
34 #include "wx/generic/treectlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
48 //WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int NO_IMAGE
= -1;
56 #define PIXELS_PER_UNIT 10
58 // -----------------------------------------------------------------------------
60 // -----------------------------------------------------------------------------
62 // timer used for enabling in-place edit
63 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
66 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
71 wxGenericTreeCtrl
*m_owner
;
74 // control used for in-place edit
75 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
78 wxTreeTextCtrl( wxWindow
*parent
,
82 wxGenericTreeCtrl
*owner
,
83 const wxString
&value
= wxEmptyString
,
84 const wxPoint
&pos
= wxDefaultPosition
,
85 const wxSize
&size
= wxDefaultSize
,
86 int style
= wxSIMPLE_BORDER
,
87 const wxValidator
& validator
= wxDefaultValidator
,
88 const wxString
&name
= wxTextCtrlNameStr
);
90 void OnChar( wxKeyEvent
&event
);
91 void OnKeyUp( wxKeyEvent
&event
);
92 void OnKillFocus( wxFocusEvent
&event
);
97 wxGenericTreeCtrl
*m_owner
;
98 wxString m_startValue
;
100 DECLARE_EVENT_TABLE()
104 class WXDLLEXPORT wxGenericTreeItem
108 wxGenericTreeItem() { m_data
= NULL
; }
109 wxGenericTreeItem( wxGenericTreeItem
*parent
,
110 const wxString
& text
,
114 wxTreeItemData
*data
);
116 ~wxGenericTreeItem();
119 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
121 const wxString
& GetText() const { return m_text
; }
122 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
123 { return m_images
[which
]; }
124 wxTreeItemData
*GetData() const { return m_data
; }
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
130 void SetText( const wxString
&text
);
131 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
136 void SetBold(bool bold
) { m_isBold
= bold
; }
138 int GetX() const { return m_x
; }
139 int GetY() const { return m_y
; }
141 void SetX(int x
) { m_x
= x
; }
142 void SetY(int y
) { m_y
= y
; }
144 int GetHeight() const { return m_height
; }
145 int GetWidth() const { return m_width
; }
147 void SetHeight(int h
) { m_height
= h
; }
148 void SetWidth(int w
) { m_width
= w
; }
150 wxGenericTreeItem
*GetParent() const { return m_parent
; }
153 // deletes all children notifying the treectrl about it if !NULL
155 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
157 // get count of all children (and grand children if 'recursively')
158 size_t GetChildrenCount(bool recursively
= TRUE
) const;
160 void Insert(wxGenericTreeItem
*child
, size_t index
)
161 { m_children
.Insert(child
, index
); }
163 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
165 // return the item at given position (or NULL if no item), onButton is
166 // TRUE if the point belongs to the item's button, otherwise it lies
167 // on the button's label
168 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
169 const wxGenericTreeCtrl
*,
173 void Expand() { m_isCollapsed
= FALSE
; }
174 void Collapse() { m_isCollapsed
= TRUE
; }
176 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
179 bool HasChildren() const { return !m_children
.IsEmpty(); }
180 bool IsSelected() const { return m_hasHilight
!= 0; }
181 bool IsExpanded() const { return !m_isCollapsed
; }
182 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
183 bool IsBold() const { return m_isBold
!= 0; }
186 // get them - may be NULL
187 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
188 // get them ensuring that the pointer is not NULL
189 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
199 void SetAttributes(wxTreeItemAttr
*attr
)
201 if ( m_ownsAttr
) delete m_attr
;
205 // set them and delete when done
206 void AssignAttributes(wxTreeItemAttr
*attr
)
213 // since there can be very many of these, we save size by chosing
214 // the smallest representation for the elements and by ordering
215 // the members to avoid padding.
216 wxString m_text
; // label to be rendered for item
218 wxTreeItemData
*m_data
; // user-provided data
220 wxArrayGenericTreeItems m_children
; // list of children
221 wxGenericTreeItem
*m_parent
; // parent of this item
223 wxTreeItemAttr
*m_attr
; // attributes???
225 // tree ctrl images for the normal, selected, expanded and
226 // expanded+selected states
227 short m_images
[wxTreeItemIcon_Max
];
229 wxCoord m_x
; // (virtual) offset from top
230 short m_y
; // (virtual) offset from left
231 short m_width
; // width of this item
232 unsigned char m_height
; // height of this item
234 // use bitfields to save size
235 int m_isCollapsed
:1;
236 int m_hasHilight
:1; // same as focused
237 int m_hasPlus
:1; // used for item which doesn't have
238 // children but has a [+] button
239 int m_isBold
:1; // render the label in bold font
240 int m_ownsAttr
:1; // delete attribute when done
243 // =============================================================================
245 // =============================================================================
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // translate the key or mouse event flags to the type of selection we're
253 static void EventFlagsToSelType(long style
,
257 bool &extended_select
,
258 bool &unselect_others
)
260 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
261 extended_select
= shiftDown
&& is_multiple
;
262 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
265 // -----------------------------------------------------------------------------
266 // wxTreeRenameTimer (internal)
267 // -----------------------------------------------------------------------------
269 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
274 void wxTreeRenameTimer::Notify()
276 m_owner
->OnRenameTimer();
279 //-----------------------------------------------------------------------------
280 // wxTreeTextCtrl (internal)
281 //-----------------------------------------------------------------------------
283 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
284 EVT_CHAR (wxTreeTextCtrl::OnChar
)
285 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
286 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
289 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
293 wxGenericTreeCtrl
*owner
,
294 const wxString
&value
,
298 const wxValidator
& validator
,
299 const wxString
&name
)
300 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
306 (*m_res
) = wxEmptyString
;
307 m_startValue
= value
;
310 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
312 // TODO focus doesn't return to the wxTextCtrl when this closes...
313 if (event
.m_keyCode
== WXK_RETURN
)
316 (*m_res
) = GetValue();
318 if ((*m_accept
) && ((*m_res
) != m_startValue
))
319 m_owner
->OnRenameAccept();
321 if (!wxPendingDelete
.Member(this))
322 wxPendingDelete
.Append(this);
326 if (event
.m_keyCode
== WXK_ESCAPE
)
331 if (!wxPendingDelete
.Member(this))
332 wxPendingDelete
.Append(this);
339 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
341 // auto-grow the textctrl:
342 wxSize parentSize
= m_owner
->GetSize();
343 wxPoint myPos
= GetPosition();
344 wxSize mySize
= GetSize();
346 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
347 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
348 if (mySize
.x
> sx
) sx
= mySize
.x
;
354 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
356 if (!wxPendingDelete
.Member(this))
357 wxPendingDelete
.Append(this);
359 if ((*m_accept
) && ((*m_res
) != m_startValue
))
360 m_owner
->OnRenameAccept();
364 // -----------------------------------------------------------------------------
366 // -----------------------------------------------------------------------------
368 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
370 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
371 : wxNotifyEvent( commandType
, id
)
374 m_itemOld
= (wxGenericTreeItem
*)NULL
;
378 // -----------------------------------------------------------------------------
380 // -----------------------------------------------------------------------------
382 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
383 const wxString
& text
,
385 int image
, int selImage
,
386 wxTreeItemData
*data
)
389 m_images
[wxTreeItemIcon_Normal
] = image
;
390 m_images
[wxTreeItemIcon_Selected
] = selImage
;
391 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
392 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
397 m_isCollapsed
= TRUE
;
398 m_hasHilight
= FALSE
;
404 m_attr
= (wxTreeItemAttr
*)NULL
;
407 // We don't know the height here yet.
412 wxGenericTreeItem::~wxGenericTreeItem()
416 if (m_ownsAttr
) delete m_attr
;
418 wxASSERT_MSG( m_children
.IsEmpty(),
419 wxT("please call DeleteChildren() before deleting the item") );
422 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
424 size_t count
= m_children
.Count();
425 for ( size_t n
= 0; n
< count
; n
++ )
427 wxGenericTreeItem
*child
= m_children
[n
];
429 tree
->SendDeleteEvent(child
);
431 child
->DeleteChildren(tree
);
438 void wxGenericTreeItem::SetText( const wxString
&text
)
443 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
445 size_t count
= m_children
.Count();
449 size_t total
= count
;
450 for (size_t n
= 0; n
< count
; ++n
)
452 total
+= m_children
[n
]->GetChildrenCount();
458 void wxGenericTreeItem::GetSize( int &x
, int &y
,
459 const wxGenericTreeCtrl
*theButton
)
461 int bottomY
=m_y
+theButton
->GetLineHeight(this);
462 if ( y
< bottomY
) y
= bottomY
;
463 int width
= m_x
+ m_width
;
464 if ( x
< width
) x
= width
;
468 size_t count
= m_children
.Count();
469 for ( size_t n
= 0; n
< count
; ++n
)
471 m_children
[n
]->GetSize( x
, y
, theButton
);
476 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
477 const wxGenericTreeCtrl
*theCtrl
,
481 // for a hidden root node, don't evaluate it, but do evaluate children
482 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
485 int h
= theCtrl
->GetLineHeight(this);
486 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
488 int y_mid
= m_y
+ h
/2;
489 if (point
.y
< y_mid
)
490 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
492 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
494 // 5 is the size of the plus sign
495 int xCross
= m_x
- theCtrl
->GetSpacing();
496 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
497 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
498 HasPlus() && theCtrl
->HasButtons() )
500 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
504 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
509 // assuming every image (normal and selected) has the same size!
510 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
511 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
514 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
515 flags
|= wxTREE_HITTEST_ONITEMICON
;
517 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
523 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
524 if (point
.x
> m_x
+m_width
)
525 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
530 // if children are expanded, fall through to evaluate them
531 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
535 size_t count
= m_children
.Count();
536 for ( size_t n
= 0; n
< count
; n
++ )
538 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
546 return (wxGenericTreeItem
*) NULL
;
549 int wxGenericTreeItem::GetCurrentImage() const
551 int image
= NO_IMAGE
;
556 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
559 if ( image
== NO_IMAGE
)
561 // we usually fall back to the normal item, but try just the
562 // expanded one (and not selected) first in this case
563 image
= GetImage(wxTreeItemIcon_Expanded
);
569 image
= GetImage(wxTreeItemIcon_Selected
);
572 // maybe it doesn't have the specific image we want,
573 // try the default one instead
574 if ( image
== NO_IMAGE
) image
= GetImage();
579 // -----------------------------------------------------------------------------
580 // wxGenericTreeCtrl implementation
581 // -----------------------------------------------------------------------------
583 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
585 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
586 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
587 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
588 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
589 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
590 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
591 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
594 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
596 * wxTreeCtrl has to be a real class or we have problems with
597 * the run-time information.
600 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
603 // -----------------------------------------------------------------------------
604 // construction/destruction
605 // -----------------------------------------------------------------------------
607 void wxGenericTreeCtrl::Init()
609 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
617 m_hilightBrush
= new wxBrush(
618 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
621 m_imageListNormal
= m_imageListButtons
=
622 m_imageListState
= (wxImageList
*) NULL
;
623 m_ownsImageListNormal
= m_ownsImageListButtons
=
624 m_ownsImageListState
= FALSE
;
627 m_isDragging
= FALSE
;
628 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
630 m_renameTimer
= new wxTreeRenameTimer( this );
631 m_lastOnSame
= FALSE
;
633 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
634 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
635 m_normalFont
.GetFamily(),
636 m_normalFont
.GetStyle(),
638 m_normalFont
.GetUnderlined());
641 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
646 const wxValidator
&validator
,
647 const wxString
& name
)
649 wxScrolledWindow::Create( parent
, id
, pos
, size
,
650 style
|wxHSCROLL
|wxVSCROLL
, name
);
652 // If the tree display has no buttons, but does have
653 // connecting lines, we can use a narrower layout.
654 // It may not be a good idea to force this...
655 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
662 SetValidator( validator
);
665 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
;
682 if (m_ownsImageListButtons
) delete m_imageListButtons
;
685 // -----------------------------------------------------------------------------
687 // -----------------------------------------------------------------------------
689 size_t wxGenericTreeCtrl::GetCount() const
691 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
694 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
700 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
706 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
708 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
710 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
713 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
715 // right now, just sets the styles. Eventually, we may
716 // want to update the inherited styles, but right now
717 // none of the parents has updatable styles
718 m_windowStyle
= styles
;
722 // -----------------------------------------------------------------------------
723 // functions to work with tree items
724 // -----------------------------------------------------------------------------
726 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
728 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
730 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
733 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
734 wxTreeItemIcon which
) const
736 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
738 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
741 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
743 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
745 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
748 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
750 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
753 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
754 pItem
->SetText(text
);
755 CalculateSize(pItem
, dc
);
759 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
761 wxTreeItemIcon which
)
763 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
765 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
766 pItem
->SetImage(image
, which
);
769 CalculateSize(pItem
, dc
);
773 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
775 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
777 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
780 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
782 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
784 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
785 pItem
->SetHasPlus(has
);
789 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
791 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
793 // avoid redrawing the tree if no real change
794 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
795 if ( pItem
->IsBold() != bold
)
797 pItem
->SetBold(bold
);
802 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
805 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
807 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
808 pItem
->Attr().SetTextColour(col
);
812 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
815 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
817 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
818 pItem
->Attr().SetBackgroundColour(col
);
822 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
824 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
826 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
827 pItem
->Attr().SetFont(font
);
831 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
833 wxScrolledWindow::SetFont(font
);
835 m_normalFont
= font
;
836 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
837 m_normalFont
.GetFamily(),
838 m_normalFont
.GetStyle(),
840 m_normalFont
.GetUnderlined());
846 // -----------------------------------------------------------------------------
847 // item status inquiries
848 // -----------------------------------------------------------------------------
850 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
852 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
854 // An item is only visible if it's not a descendant of a collapsed item
855 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
856 wxGenericTreeItem
* parent
= pItem
->GetParent();
859 if (!parent
->IsExpanded())
861 parent
= parent
->GetParent();
865 GetViewStart(& startX
, & startY
);
867 wxSize clientSize
= GetClientSize();
870 if (!GetBoundingRect(item
, rect
))
872 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
874 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
876 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
882 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
884 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
886 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
889 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
891 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
893 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
896 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
898 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
900 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
903 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
905 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
907 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
910 // -----------------------------------------------------------------------------
912 // -----------------------------------------------------------------------------
914 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
916 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
918 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
921 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
923 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
926 return GetNextChild(item
, cookie
);
929 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
931 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
933 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
934 if ( (size_t)cookie
< children
.Count() )
936 return children
.Item((size_t)cookie
++);
940 // there are no more of them
941 return wxTreeItemId();
945 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
947 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
949 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
950 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
953 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
955 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
957 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
958 wxGenericTreeItem
*parent
= i
->GetParent();
959 if ( parent
== NULL
)
961 // root item doesn't have any siblings
962 return wxTreeItemId();
965 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
966 int index
= siblings
.Index(i
);
967 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
969 size_t n
= (size_t)(index
+ 1);
970 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
973 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
975 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
977 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
978 wxGenericTreeItem
*parent
= i
->GetParent();
979 if ( parent
== NULL
)
981 // root item doesn't have any siblings
982 return wxTreeItemId();
985 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
986 int index
= siblings
.Index(i
);
987 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
989 return index
== 0 ? wxTreeItemId()
990 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
993 // Only for internal use right now, but should probably be public
994 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
996 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
998 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1000 // First see if there are any children.
1001 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1002 if (children
.GetCount() > 0)
1004 return children
.Item(0);
1008 // Try a sibling of this or ancestor instead
1009 wxTreeItemId p
= item
;
1010 wxTreeItemId toFind
;
1013 toFind
= GetNextSibling(p
);
1015 } while (p
.IsOk() && !toFind
.IsOk());
1020 wxTreeItemId
wxGenericTreeCtrl::GetPrev(const wxTreeItemId
& item
) const
1022 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1024 wxFAIL_MSG(wxT("not implemented"));
1026 return wxTreeItemId();
1029 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1031 wxTreeItemId id
= GetRootItem();
1040 } while (id
.IsOk());
1042 return wxTreeItemId();
1045 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1047 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1049 wxTreeItemId id
= item
;
1052 while (id
= GetNext(id
), id
.IsOk())
1058 return wxTreeItemId();
1061 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1063 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1065 wxFAIL_MSG(wxT("not implemented"));
1067 return wxTreeItemId();
1070 // -----------------------------------------------------------------------------
1072 // -----------------------------------------------------------------------------
1074 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1076 const wxString
& text
,
1077 int image
, int selImage
,
1078 wxTreeItemData
*data
)
1080 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1083 // should we give a warning here?
1084 return AddRoot(text
, image
, selImage
, data
);
1087 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1089 wxClientDC
dc(this);
1090 wxGenericTreeItem
*item
=
1091 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1095 data
->m_pItem
= (long) item
;
1098 parent
->Insert( item
, previous
);
1103 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1104 int image
, int selImage
,
1105 wxTreeItemData
*data
)
1107 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1109 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1111 wxClientDC
dc(this);
1112 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1113 image
, selImage
, data
);
1114 if (HasFlag(wxTR_HIDE_ROOT
))
1116 // if root is hidden, make sure we can navigate
1118 m_anchor
->SetHasPlus();
1123 data
->m_pItem
= (long) m_anchor
;
1126 if (!HasFlag(wxTR_MULTIPLE
))
1128 m_current
= m_key_current
= m_anchor
;
1129 m_current
->SetHilight( TRUE
);
1135 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1136 const wxString
& text
,
1137 int image
, int selImage
,
1138 wxTreeItemData
*data
)
1140 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1143 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1144 const wxTreeItemId
& idPrevious
,
1145 const wxString
& text
,
1146 int image
, int selImage
,
1147 wxTreeItemData
*data
)
1149 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1152 // should we give a warning here?
1153 return AddRoot(text
, image
, selImage
, data
);
1156 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1157 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1158 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1160 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1163 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1165 const wxString
& text
,
1166 int image
, int selImage
,
1167 wxTreeItemData
*data
)
1169 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1172 // should we give a warning here?
1173 return AddRoot(text
, image
, selImage
, data
);
1176 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1179 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1180 const wxString
& text
,
1181 int image
, int selImage
,
1182 wxTreeItemData
*data
)
1184 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1187 // should we give a warning here?
1188 return AddRoot(text
, image
, selImage
, data
);
1191 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1192 image
, selImage
, data
);
1195 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1197 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1198 event
.m_item
= (long) item
;
1199 event
.SetEventObject( this );
1200 ProcessEvent( event
);
1203 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1205 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1207 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1208 item
->DeleteChildren(this);
1211 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1213 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1215 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1217 // don't stay with invalid m_key_current or we will crash in
1218 // the next call to OnChar()
1219 bool changeKeyCurrent
= FALSE
;
1220 wxGenericTreeItem
*itemKey
= m_key_current
;
1223 if ( itemKey
== item
)
1225 // m_key_current is a descendant of the item being deleted
1226 changeKeyCurrent
= TRUE
;
1229 itemKey
= itemKey
->GetParent();
1232 wxGenericTreeItem
*parent
= item
->GetParent();
1235 parent
->GetChildren().Remove( item
); // remove by value
1238 if ( changeKeyCurrent
)
1240 // may be NULL or not
1241 m_key_current
= parent
;
1244 item
->DeleteChildren(this);
1245 SendDeleteEvent(item
);
1249 void wxGenericTreeCtrl::DeleteAllItems()
1255 m_anchor
->DeleteChildren(this);
1262 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1264 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1266 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1268 if ( !item
->HasPlus() )
1271 if ( item
->IsExpanded() )
1274 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1275 event
.m_item
= (long) item
;
1276 event
.SetEventObject( this );
1278 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1280 // cancelled by program
1285 CalculatePositions();
1287 RefreshSubtree(item
);
1289 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1290 ProcessEvent( event
);
1293 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1296 if ( IsExpanded(item
) )
1299 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1300 while ( child
.IsOk() )
1304 child
= GetNextChild(item
, cookie
);
1309 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1311 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1313 if ( !item
->IsExpanded() )
1316 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1317 event
.m_item
= (long) item
;
1318 event
.SetEventObject( this );
1319 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1321 // cancelled by program
1327 #if 0 // TODO why should items be collapsed recursively?
1328 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1329 size_t count
= children
.Count();
1330 for ( size_t n
= 0; n
< count
; n
++ )
1332 Collapse(children
[n
]);
1336 CalculatePositions();
1338 RefreshSubtree(item
);
1340 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1341 ProcessEvent( event
);
1344 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1347 DeleteChildren(item
);
1350 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1352 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1354 if (item
->IsExpanded())
1360 void wxGenericTreeCtrl::Unselect()
1364 m_current
->SetHilight( FALSE
);
1365 RefreshLine( m_current
);
1369 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1371 if (item
->IsSelected())
1373 item
->SetHilight(FALSE
);
1377 if (item
->HasChildren())
1379 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1380 size_t count
= children
.Count();
1381 for ( size_t n
= 0; n
< count
; ++n
)
1383 UnselectAllChildren(children
[n
]);
1388 void wxGenericTreeCtrl::UnselectAll()
1390 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1393 // Recursive function !
1394 // To stop we must have crt_item<last_item
1396 // Tag all next children, when no more children,
1397 // Move to parent (not to tag)
1398 // Keep going... if we found last_item, we stop.
1399 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1401 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1403 if (parent
== NULL
) // This is root item
1404 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1406 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1407 int index
= children
.Index(crt_item
);
1408 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1410 size_t count
= children
.Count();
1411 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1413 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1416 return TagNextChildren(parent
, last_item
, select
);
1419 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1421 crt_item
->SetHilight(select
);
1422 RefreshLine(crt_item
);
1424 if (crt_item
==last_item
)
1427 if (crt_item
->HasChildren())
1429 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1430 size_t count
= children
.Count();
1431 for ( size_t n
= 0; n
< count
; ++n
)
1433 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1441 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1443 // item2 is not necessary after item1
1444 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1446 // choice first' and 'last' between item1 and item2
1447 if (item1
->GetY()<item2
->GetY())
1458 bool select
= m_current
->IsSelected();
1460 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1463 TagNextChildren(first
,last
,select
);
1466 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1467 bool unselect_others
,
1468 bool extended_select
)
1470 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1472 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1473 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1475 //wxCHECK_RET( ( (!unselect_others) && is_single),
1476 // wxT("this is a single selection tree") );
1478 // to keep going anyhow !!!
1481 if (item
->IsSelected())
1482 return; // nothing to do
1483 unselect_others
= TRUE
;
1484 extended_select
= FALSE
;
1486 else if ( unselect_others
&& item
->IsSelected() )
1488 // selection change if there is more than one item currently selected
1489 wxArrayTreeItemIds selected_items
;
1490 if ( GetSelections(selected_items
) == 1 )
1494 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1495 event
.m_item
= (long) item
;
1496 event
.m_itemOld
= (long) m_current
;
1497 event
.SetEventObject( this );
1498 // TODO : Here we don't send any selection mode yet !
1500 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1503 wxTreeItemId parent
= GetParent( itemId
);
1504 while (parent
.IsOk())
1506 if (!IsExpanded(parent
))
1509 parent
= GetParent( parent
);
1512 EnsureVisible( itemId
);
1515 if (unselect_others
)
1517 if (is_single
) Unselect(); // to speed up thing
1522 if (extended_select
)
1526 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1529 // don't change the mark (m_current)
1530 SelectItemRange(m_current
, item
);
1534 bool select
=TRUE
; // the default
1536 // Check if we need to toggle hilight (ctrl mode)
1537 if (!unselect_others
)
1538 select
=!item
->IsSelected();
1540 m_current
= m_key_current
= item
;
1541 m_current
->SetHilight(select
);
1542 RefreshLine( m_current
);
1545 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1546 GetEventHandler()->ProcessEvent( event
);
1549 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1550 wxArrayTreeItemIds
&array
) const
1552 if ( item
->IsSelected() )
1553 array
.Add(wxTreeItemId(item
));
1555 if ( item
->HasChildren() )
1557 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1558 size_t count
= children
.GetCount();
1559 for ( size_t n
= 0; n
< count
; ++n
)
1560 FillArray(children
[n
], array
);
1564 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1567 wxTreeItemId idRoot
= GetRootItem();
1568 if ( idRoot
.IsOk() )
1570 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1572 //else: the tree is empty, so no selections
1574 return array
.Count();
1577 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1579 if (!item
.IsOk()) return;
1581 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1583 // first expand all parent branches
1584 wxGenericTreeItem
*parent
= gitem
->GetParent();
1588 parent
= parent
->GetParent();
1591 //if (parent) CalculatePositions();
1596 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1598 if (!item
.IsOk()) return;
1600 // We have to call this here because the label in
1601 // question might just have been added and no screen
1602 // update taken place.
1603 if (m_dirty
) wxYieldIfNeeded();
1605 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1607 // now scroll to the item
1608 int item_y
= gitem
->GetY();
1612 GetViewStart( &start_x
, &start_y
);
1613 start_y
*= PIXELS_PER_UNIT
;
1617 GetClientSize( &client_w
, &client_h
);
1619 if (item_y
< start_y
+3)
1624 m_anchor
->GetSize( x
, y
, this );
1625 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1626 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1627 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1628 // Item should appear at top
1629 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1631 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1636 m_anchor
->GetSize( x
, y
, this );
1637 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1638 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1639 item_y
+= PIXELS_PER_UNIT
+2;
1640 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1641 // Item should appear at bottom
1642 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
);
1646 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1647 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1649 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1650 wxGenericTreeItem
**item2
)
1652 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1654 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1657 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1658 const wxTreeItemId
& item2
)
1660 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1663 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1665 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1667 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1669 wxCHECK_RET( !s_treeBeingSorted
,
1670 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1672 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1673 if ( children
.Count() > 1 )
1677 s_treeBeingSorted
= this;
1678 children
.Sort(tree_ctrl_compare_func
);
1679 s_treeBeingSorted
= NULL
;
1681 //else: don't make the tree dirty as nothing changed
1684 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1686 return m_imageListNormal
;
1689 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1691 return m_imageListButtons
;
1694 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1696 return m_imageListState
;
1699 void wxGenericTreeCtrl::CalculateLineHeight()
1701 wxClientDC
dc(this);
1702 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1704 if ( m_imageListNormal
)
1706 // Calculate a m_lineHeight value from the normal Image sizes.
1707 // May be toggle off. Then wxGenericTreeCtrl will spread when
1708 // necessary (which might look ugly).
1709 int n
= m_imageListNormal
->GetImageCount();
1710 for (int i
= 0; i
< n
; i
++)
1712 int width
= 0, height
= 0;
1713 m_imageListNormal
->GetSize(i
, width
, height
);
1714 if (height
> m_lineHeight
) m_lineHeight
= height
;
1718 if (m_imageListButtons
)
1720 // Calculate a m_lineHeight value from the Button image sizes.
1721 // May be toggle off. Then wxGenericTreeCtrl will spread when
1722 // necessary (which might look ugly).
1723 int n
= m_imageListButtons
->GetImageCount();
1724 for (int i
= 0; i
< n
; i
++)
1726 int width
= 0, height
= 0;
1727 m_imageListButtons
->GetSize(i
, width
, height
);
1728 if (height
> m_lineHeight
) m_lineHeight
= height
;
1732 if (m_lineHeight
< 30)
1733 m_lineHeight
+= 2; // at least 2 pixels
1735 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1738 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1740 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1741 m_imageListNormal
= imageList
;
1742 m_ownsImageListNormal
= FALSE
;
1744 CalculateLineHeight();
1747 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1749 if (m_ownsImageListState
) delete m_imageListState
;
1750 m_imageListState
= imageList
;
1751 m_ownsImageListState
= FALSE
;
1754 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1756 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1757 m_imageListButtons
= imageList
;
1758 m_ownsImageListButtons
= FALSE
;
1760 CalculateLineHeight();
1763 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1765 SetImageList(imageList
);
1766 m_ownsImageListNormal
= TRUE
;
1769 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1771 SetStateImageList(imageList
);
1772 m_ownsImageListState
= TRUE
;
1775 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1777 SetButtonsImageList(imageList
);
1778 m_ownsImageListButtons
= TRUE
;
1781 // -----------------------------------------------------------------------------
1783 // -----------------------------------------------------------------------------
1785 void wxGenericTreeCtrl::AdjustMyScrollbars()
1790 m_anchor
->GetSize( x
, y
, this );
1791 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1792 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1793 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1794 int y_pos
= GetScrollPos( wxVERTICAL
);
1795 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1799 SetScrollbars( 0, 0, 0, 0 );
1803 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1805 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1806 return item
->GetHeight();
1808 return m_lineHeight
;
1811 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1813 // TODO implement "state" icon on items
1815 wxTreeItemAttr
*attr
= item
->GetAttributes();
1816 if ( attr
&& attr
->HasFont() )
1817 dc
.SetFont(attr
->GetFont());
1818 else if (item
->IsBold())
1819 dc
.SetFont(m_boldFont
);
1821 long text_w
= 0, text_h
= 0;
1822 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1824 int image_h
= 0, image_w
= 0;
1825 int image
= item
->GetCurrentImage();
1826 if ( image
!= NO_IMAGE
)
1828 if ( m_imageListNormal
)
1830 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1839 int total_h
= GetLineHeight(item
);
1841 bool paintBg
= item
->IsSelected() && m_hasFocus
;
1844 dc
.SetBrush(*m_hilightBrush
);
1849 if ( attr
&& attr
->HasBackgroundColour() )
1850 colBg
= attr
->GetBackgroundColour();
1852 colBg
= m_backgroundColour
;
1853 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1856 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1858 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1860 // If it's selected, and there's an image, then we should
1861 // take care to leave the area under the image painted in the
1862 // background colour.
1863 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1864 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1868 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1869 item
->GetWidth()+2, total_h
-offset
);
1872 if ( image
!= NO_IMAGE
)
1874 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1875 m_imageListNormal
->Draw( image
, dc
,
1877 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1878 wxIMAGELIST_DRAW_TRANSPARENT
);
1879 dc
.DestroyClippingRegion();
1882 dc
.SetBackgroundMode(wxTRANSPARENT
);
1883 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1884 dc
.DrawText( item
->GetText(),
1885 (wxCoord
)(image_w
+ item
->GetX()),
1886 (wxCoord
)(item
->GetY() + extraH
));
1888 // restore normal font
1889 dc
.SetFont( m_normalFont
);
1892 // Now y stands for the top of the item, whereas it used to stand for middle !
1893 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1895 int x
= level
*m_indent
;
1896 if (!HasFlag(wxTR_HIDE_ROOT
))
1900 else if (level
== 0)
1902 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1903 size_t n
, count
= children
.Count();
1904 for (n
= 0; n
< count
; ++n
)
1905 PaintLevel(children
[n
], dc
, 1, y
);
1909 item
->SetX(x
+m_spacing
);
1912 int h
= GetLineHeight(item
);
1914 int y_mid
= y_top
+ (h
>>1);
1917 int exposed_x
= dc
.LogicalToDeviceX(0);
1918 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1920 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1922 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1924 if (!HasFlag(wxTR_NO_LINES
))
1926 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
1927 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1928 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1931 if (m_imageListButtons
!= NULL
)
1933 // draw the image button here
1934 int image_h
= 0, image_w
= 0, image
= wxCLOSED_BUTTON
;
1935 if (item
->IsExpanded()) image
= wxOPEN_BUTTON
;
1936 if (item
->IsSelected())
1937 image
+= wxOPEN_BUTTON_SELECTED
- wxOPEN_BUTTON
;
1938 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1939 int xx
= x
- (image_w
>>1);
1940 int yy
= y_mid
- (image_h
>>1);
1941 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1942 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1943 wxIMAGELIST_DRAW_TRANSPARENT
);
1944 dc
.DestroyClippingRegion();
1946 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1948 // draw the twisty button here
1949 dc
.SetPen(*wxBLACK_PEN
);
1950 dc
.SetBrush(*m_hilightBrush
);
1954 if (item
->IsExpanded())
1957 button
[0].y
= y_mid
-2;
1959 button
[1].y
= y_mid
-2;
1961 button
[2].y
= y_mid
+3;
1965 button
[0].y
= y_mid
-5;
1967 button
[1].y
= y_mid
+5;
1969 button
[2].y
= y_mid
;
1972 dc
.DrawPolygon(3, button
);
1974 dc
.SetPen(m_dottedPen
);
1976 else // if (HasFlag(wxTR_HAS_BUTTONS))
1978 // draw the plus sign here
1979 dc
.SetPen(*wxGREY_PEN
);
1980 dc
.SetBrush(*wxWHITE_BRUSH
);
1981 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
1982 dc
.SetPen(*wxBLACK_PEN
);
1983 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
1984 if (!item
->IsExpanded())
1985 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
1986 dc
.SetPen(m_dottedPen
);
1989 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
1991 // draw the horizontal line here
1993 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
1994 x_start
-= m_indent
;
1995 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2000 // don't draw rect outline if we already have the
2001 // background color under Mac
2002 (item
->IsSelected()) ? wxBLACK_PEN
:
2003 #endif // !__WXMAC__
2007 if (item
->IsSelected() && m_hasFocus
)
2009 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2013 wxTreeItemAttr
*attr
= item
->GetAttributes();
2014 if (attr
&& attr
->HasTextColour())
2015 colText
= attr
->GetTextColour();
2017 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2021 dc
.SetTextForeground(colText
);
2025 PaintItem(item
, dc
);
2027 if (HasFlag(wxTR_ROW_LINES
))
2029 dc
.SetPen(*wxWHITE_PEN
);
2030 dc
.DrawLine(0, y_top
, 10000, y_top
);
2031 dc
.DrawLine(0, y
, 10000, y
);
2034 // restore DC objects
2035 dc
.SetBrush(*wxWHITE_BRUSH
);
2036 dc
.SetPen(m_dottedPen
);
2037 dc
.SetTextForeground(*wxBLACK
);
2040 if (item
->IsExpanded())
2042 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2043 int count
= children
.Count();
2050 PaintLevel(children
[n
], dc
, level
, y
);
2051 } while (++n
< count
);
2053 if (!HasFlag(wxTR_NO_LINES
))
2055 // draw line down to last child
2056 oldY
+= GetLineHeight(children
[n
-1])/2;
2057 if (HasButtons()) y_mid
+= 5;
2058 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2064 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2068 if ( item
->HasPlus() )
2070 // it's a folder, indicate it by a border
2075 // draw a line under the drop target because the item will be
2077 DrawLine(item
, TRUE
/* below */);
2080 SetCursor(wxCURSOR_BULLSEYE
);
2085 SetCursor(wxCURSOR_NO_ENTRY
);
2089 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2091 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2093 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2095 wxClientDC
dc(this);
2097 dc
.SetLogicalFunction(wxINVERT
);
2098 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2100 int w
= i
->GetWidth() + 2;
2101 int h
= GetLineHeight(i
) + 2;
2103 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2106 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2108 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2110 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2112 wxClientDC
dc(this);
2114 dc
.SetLogicalFunction(wxINVERT
);
2120 y
+= GetLineHeight(i
) - 1;
2123 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2126 // -----------------------------------------------------------------------------
2127 // wxWindows callbacks
2128 // -----------------------------------------------------------------------------
2130 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2138 dc
.SetFont( m_normalFont
);
2139 dc
.SetPen( m_dottedPen
);
2141 // this is now done dynamically
2142 //if(GetImageList() == NULL)
2143 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2146 PaintLevel( m_anchor
, dc
, 0, y
);
2149 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2154 RefreshLine( m_current
);
2157 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2162 RefreshLine( m_current
);
2165 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2167 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2168 te
.m_code
= (int)event
.KeyCode();
2169 te
.SetEventObject( this );
2170 GetEventHandler()->ProcessEvent( te
);
2172 if ( (m_current
== 0) || (m_key_current
== 0) )
2178 // how should the selection work for this event?
2179 bool is_multiple
, extended_select
, unselect_others
;
2180 EventFlagsToSelType(GetWindowStyleFlag(),
2182 event
.ControlDown(),
2183 is_multiple
, extended_select
, unselect_others
);
2187 // * : Expand all/Collapse all
2188 // ' ' | return : activate
2189 // up : go up (not last children!)
2191 // left : go to parent
2192 // right : open if parent and go next
2193 // home : go to root
2194 // end : go to last item without opening parents
2195 switch (event
.KeyCode())
2199 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2207 if ( !IsExpanded(m_current
) )
2210 ExpandAll(m_current
);
2213 //else: fall through to Collapse() it
2217 if (IsExpanded(m_current
))
2219 Collapse(m_current
);
2226 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2227 event
.m_item
= (long) m_current
;
2229 event
.SetEventObject( this );
2230 GetEventHandler()->ProcessEvent( event
);
2234 // up goes to the previous sibling or to the last
2235 // of its children if it's expanded
2238 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2241 prev
= GetParent( m_key_current
);
2242 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2244 break; // don't go to root if it is hidden
2249 wxTreeItemId current
= m_key_current
;
2250 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2251 if (current
== GetFirstChild( prev
, cookie
))
2253 // otherwise we return to where we came from
2254 SelectItem( prev
, unselect_others
, extended_select
);
2255 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2256 EnsureVisible( prev
);
2263 while ( IsExpanded(prev
) && HasChildren(prev
) )
2265 wxTreeItemId child
= GetLastChild(prev
);
2272 SelectItem( prev
, unselect_others
, extended_select
);
2273 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2274 EnsureVisible( prev
);
2279 // left arrow goes to the parent
2282 wxTreeItemId prev
= GetParent( m_current
);
2283 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2285 // don't go to root if it is hidden
2286 prev
= GetPrevSibling( m_current
);
2290 EnsureVisible( prev
);
2291 SelectItem( prev
, unselect_others
, extended_select
);
2297 // this works the same as the down arrow except that we
2298 // also expand the item if it wasn't expanded yet
2304 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2307 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2308 SelectItem( child
, unselect_others
, extended_select
);
2309 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2310 EnsureVisible( child
);
2314 wxTreeItemId next
= GetNextSibling( m_key_current
);
2317 wxTreeItemId current
= m_key_current
;
2318 while (current
&& !next
)
2320 current
= GetParent( current
);
2321 if (current
) next
= GetNextSibling( current
);
2326 SelectItem( next
, unselect_others
, extended_select
);
2327 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2328 EnsureVisible( next
);
2334 // <End> selects the last visible tree item
2337 wxTreeItemId last
= GetRootItem();
2339 while ( last
.IsOk() && IsExpanded(last
) )
2341 wxTreeItemId lastChild
= GetLastChild(last
);
2343 // it may happen if the item was expanded but then all of
2344 // its children have been deleted - so IsExpanded() returned
2345 // TRUE, but GetLastChild() returned invalid item
2354 EnsureVisible( last
);
2355 SelectItem( last
, unselect_others
, extended_select
);
2360 // <Home> selects the root item
2363 wxTreeItemId prev
= GetRootItem();
2365 if (HasFlag(wxTR_HIDE_ROOT
))
2368 prev
= GetFirstChild(prev
, dummy
);
2371 EnsureVisible( prev
);
2372 SelectItem( prev
, unselect_others
, extended_select
);
2381 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2383 // JACS: removed wxYieldIfNeeded() because it can cause the window
2384 // to be deleted from under us if a close window event is pending
2389 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2390 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2391 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2392 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2393 if (flags
) return wxTreeItemId();
2395 if (m_anchor
== NULL
)
2397 flags
= wxTREE_HITTEST_NOWHERE
;
2398 return wxTreeItemId();
2401 wxClientDC
dc(this);
2403 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2404 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2405 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2411 flags
= wxTREE_HITTEST_NOWHERE
;
2412 return wxTreeItemId();
2417 // get the bounding rectangle of the item (or of its label only)
2418 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2420 bool WXUNUSED(textOnly
)) const
2422 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2424 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2427 GetViewStart(& startX
, & startY
);
2429 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2430 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2431 rect
.width
= i
->GetWidth();
2432 //rect.height = i->GetHeight();
2433 rect
.height
= GetLineHeight(i
);
2440 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2442 if (!item
.IsOk()) return;
2444 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2446 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2447 te
.m_item
= (long) m_currentEdit
;
2448 te
.SetEventObject( this );
2449 GetEventHandler()->ProcessEvent( te
);
2451 if (!te
.IsAllowed()) return;
2453 // We have to call this here because the label in
2454 // question might just have been added and no screen
2455 // update taken place.
2456 if (m_dirty
) wxYieldIfNeeded();
2458 wxString s
= m_currentEdit
->GetText();
2459 int x
= m_currentEdit
->GetX();
2460 int y
= m_currentEdit
->GetY();
2461 int w
= m_currentEdit
->GetWidth();
2462 int h
= m_currentEdit
->GetHeight();
2467 int image
= m_currentEdit
->GetCurrentImage();
2468 if ( image
!= NO_IMAGE
)
2470 if ( m_imageListNormal
)
2472 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2477 wxFAIL_MSG(_T("you must create an image list to use images!"));
2481 w
-= image_w
+ 4; // I don't know why +4 is needed
2483 wxClientDC
dc(this);
2485 x
= dc
.LogicalToDeviceX( x
);
2486 y
= dc
.LogicalToDeviceY( y
);
2488 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2498 void wxGenericTreeCtrl::OnRenameTimer()
2503 void wxGenericTreeCtrl::OnRenameAccept()
2505 // TODO if the validator fails this causes a crash
2506 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2507 le
.m_item
= (long) m_currentEdit
;
2508 le
.SetEventObject( this );
2509 le
.m_label
= m_renameRes
;
2510 GetEventHandler()->ProcessEvent( le
);
2512 if (!le
.IsAllowed()) return;
2514 SetItemText( m_currentEdit
, m_renameRes
);
2517 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2519 if ( !m_anchor
) return;
2521 // we process left mouse up event (enables in-place edit), right down
2522 // (pass to the user code), left dbl click (activate item) and
2523 // dragging/moving events for items drag-and-drop
2524 if ( !(event
.LeftDown() ||
2526 event
.RightDown() ||
2527 event
.LeftDClick() ||
2529 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2536 wxClientDC
dc(this);
2538 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2539 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2542 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2547 if ( event
.Dragging() && !m_isDragging
)
2549 if (m_dragCount
== 0)
2550 m_dragStart
= wxPoint(x
,y
);
2554 if (m_dragCount
!= 3)
2556 // wait until user drags a bit further...
2560 wxEventType command
= event
.RightIsDown()
2561 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2562 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2564 wxTreeEvent
nevent( command
, GetId() );
2565 nevent
.m_item
= (long) m_current
;
2566 nevent
.SetEventObject(this);
2568 // by default the dragging is not supported, the user code must
2569 // explicitly allow the event for it to take place
2572 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2574 // we're going to drag this item
2575 m_isDragging
= TRUE
;
2577 // remember the old cursor because we will change it while
2579 m_oldCursor
= m_cursor
;
2581 // in a single selection control, hide the selection temporarily
2582 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2584 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2586 if ( m_oldSelection
)
2588 m_oldSelection
->SetHilight(FALSE
);
2589 RefreshLine(m_oldSelection
);
2596 else if ( event
.Moving() )
2598 if ( item
!= m_dropTarget
)
2600 // unhighlight the previous drop target
2601 DrawDropEffect(m_dropTarget
);
2603 m_dropTarget
= item
;
2605 // highlight the current drop target if any
2606 DrawDropEffect(m_dropTarget
);
2611 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2613 // erase the highlighting
2614 DrawDropEffect(m_dropTarget
);
2616 if ( m_oldSelection
)
2618 m_oldSelection
->SetHilight(TRUE
);
2619 RefreshLine(m_oldSelection
);
2620 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2623 // generate the drag end event
2624 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2626 event
.m_item
= (long) item
;
2627 event
.m_pointDrag
= wxPoint(x
, y
);
2628 event
.SetEventObject(this);
2630 (void)GetEventHandler()->ProcessEvent(event
);
2632 m_isDragging
= FALSE
;
2633 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2637 SetCursor(m_oldCursor
);
2643 // here we process only the messages which happen on tree items
2647 if (item
== NULL
) return; /* we hit the blank area */
2649 if ( event
.RightDown() )
2651 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2652 nevent
.m_item
= (long) item
;
2654 CalcScrolledPosition(x
, y
,
2655 &nevent
.m_pointDrag
.x
,
2656 &nevent
.m_pointDrag
.y
);
2657 nevent
.SetEventObject(this);
2658 GetEventHandler()->ProcessEvent(nevent
);
2660 else if ( event
.LeftUp() )
2664 if ( (item
== m_current
) &&
2665 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2666 HasFlag(wxTR_EDIT_LABELS
) )
2668 if ( m_renameTimer
->IsRunning() )
2669 m_renameTimer
->Stop();
2671 m_renameTimer
->Start( 100, TRUE
);
2674 m_lastOnSame
= FALSE
;
2677 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2679 if ( event
.LeftDown() )
2681 m_lastOnSame
= item
== m_current
;
2684 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2686 // only toggle the item for a single click, double click on
2687 // the button doesn't do anything (it toggles the item twice)
2688 if ( event
.LeftDown() )
2693 // don't select the item if the button was clicked
2697 // how should the selection work for this event?
2698 bool is_multiple
, extended_select
, unselect_others
;
2699 EventFlagsToSelType(GetWindowStyleFlag(),
2701 event
.ControlDown(),
2702 is_multiple
, extended_select
, unselect_others
);
2704 SelectItem(item
, unselect_others
, extended_select
);
2706 // For some reason, Windows isn't recognizing a left double-click,
2707 // so we need to simulate it here. Allow 200 milliseconds for now.
2708 if ( event
.LeftDClick() )
2710 // double clicking should not start editing the item label
2711 m_renameTimer
->Stop();
2712 m_lastOnSame
= FALSE
;
2714 if (item
->HasPlus())
2716 // for a "directory" node, toggle expansion
2721 // for a "file" node, activate it
2722 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2724 nevent
.m_item
= (long) item
;
2726 CalcScrolledPosition(x
, y
,
2727 &nevent
.m_pointDrag
.x
,
2728 &nevent
.m_pointDrag
.y
);
2729 nevent
.SetEventObject( this );
2730 GetEventHandler()->ProcessEvent( nevent
);
2737 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2739 /* after all changes have been done to the tree control,
2740 * we actually redraw the tree when everything is over */
2742 if (!m_dirty
) return;
2746 CalculatePositions();
2748 AdjustMyScrollbars();
2751 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2757 dc
.SetFont(m_boldFont
);
2759 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2762 // restore normal font
2763 dc
.SetFont( m_normalFont
);
2767 int image
= item
->GetCurrentImage();
2768 if ( image
!= NO_IMAGE
)
2770 if ( m_imageListNormal
)
2772 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2777 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2780 total_h
+= 2; // at least 2 pixels
2782 total_h
+= total_h
/10; // otherwise 10% extra spacing
2784 item
->SetHeight(total_h
);
2785 if (total_h
>m_lineHeight
)
2786 m_lineHeight
=total_h
;
2788 item
->SetWidth(image_w
+text_w
+2);
2791 // -----------------------------------------------------------------------------
2792 // for developper : y is now the top of the level
2793 // not the middle of it !
2794 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2796 int x
= level
*m_indent
;
2797 if (!HasFlag(wxTR_HIDE_ROOT
))
2801 else if (level
== 0)
2803 // a hidden root is not evaluated, but its
2804 // children are always calculated
2808 CalculateSize( item
, dc
);
2811 item
->SetX( x
+m_spacing
);
2813 y
+= GetLineHeight(item
);
2815 if ( !item
->IsExpanded() )
2817 // we don't need to calculate collapsed branches
2822 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2823 size_t n
, count
= children
.Count();
2825 for (n
= 0; n
< count
; ++n
)
2826 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2829 void wxGenericTreeCtrl::CalculatePositions()
2831 if ( !m_anchor
) return;
2833 wxClientDC
dc(this);
2836 dc
.SetFont( m_normalFont
);
2838 dc
.SetPen( m_dottedPen
);
2839 //if(GetImageList() == NULL)
2840 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2843 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2846 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2848 if (m_dirty
) return;
2850 wxClientDC
dc(this);
2855 GetClientSize( &cw
, &ch
);
2858 rect
.x
= dc
.LogicalToDeviceX( 0 );
2860 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2863 Refresh( TRUE
, &rect
);
2865 AdjustMyScrollbars();
2868 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2870 if (m_dirty
) return;
2872 wxClientDC
dc(this);
2877 GetClientSize( &cw
, &ch
);
2880 rect
.x
= dc
.LogicalToDeviceX( 0 );
2881 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2883 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2885 Refresh( TRUE
, &rect
);
2888 #endif // wxUSE_TREECTRL