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_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 OnKillFocus( wxFocusEvent
&event
);
97 wxGenericTreeCtrl
*m_owner
;
98 wxString m_startValue
;
100 DECLARE_EVENT_TABLE()
101 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl
);
105 class WXDLLEXPORT wxGenericTreeItem
109 wxGenericTreeItem() { m_data
= NULL
; }
110 wxGenericTreeItem( wxGenericTreeItem
*parent
,
111 const wxString
& text
,
113 int image
, int selImage
,
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
; }
151 wxGenericTreeItem
*GetParent() const { return m_parent
; }
154 // deletes all children notifying the treectrl about it if !NULL
156 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
157 // FIXME don't know what is it for
160 // get count of all children (and grand children if 'recursively')
161 size_t GetChildrenCount(bool recursively
= TRUE
) const;
163 void Insert(wxGenericTreeItem
*child
, size_t index
)
164 { m_children
.Insert(child
, index
); }
166 void SetCross( int x
, int y
);
167 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
169 // return the item at given position (or NULL if no item), onButton is
170 // TRUE if the point belongs to the item's button, otherwise it lies
171 // on the button's label
172 wxGenericTreeItem
*HitTest( const wxPoint
& point
, const wxGenericTreeCtrl
*, int &flags
);
174 void Expand() { m_isCollapsed
= FALSE
; }
175 void Collapse() { m_isCollapsed
= TRUE
; }
177 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
180 bool HasChildren() const { return !m_children
.IsEmpty(); }
181 bool IsSelected() const { return m_hasHilight
; }
182 bool IsExpanded() const { return !m_isCollapsed
; }
183 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
184 bool IsBold() const { return m_isBold
; }
187 // get them - may be NULL
188 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
189 // get them ensuring that the pointer is not NULL
190 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
201 // tree ctrl images for the normal, selected, expanded and
202 // expanded+selected states
203 int m_images
[wxTreeItemIcon_Max
];
205 wxTreeItemData
*m_data
;
207 // use bitfields to save size
208 int m_isCollapsed
:1;
209 int m_hasHilight
:1; // same as focused
210 int m_hasPlus
:1; // used for item which doesn't have
211 // children but has a [+] button
212 int m_isBold
:1; // render the label in bold font
215 wxCoord m_height
, m_width
;
216 int m_xCross
, m_yCross
;
219 wxArrayGenericTreeItems m_children
;
220 wxGenericTreeItem
*m_parent
;
222 wxTreeItemAttr
*m_attr
;
225 // =============================================================================
227 // =============================================================================
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 // translate the key or mouse event flags to the type of selection we're
235 static void EventFlagsToSelType(long style
,
239 bool &extended_select
,
240 bool &unselect_others
)
242 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
243 extended_select
= shiftDown
&& is_multiple
;
244 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
247 // -----------------------------------------------------------------------------
248 // wxTreeRenameTimer (internal)
249 // -----------------------------------------------------------------------------
251 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
256 void wxTreeRenameTimer::Notify()
258 m_owner
->OnRenameTimer();
261 //-----------------------------------------------------------------------------
262 // wxTreeTextCtrl (internal)
263 //-----------------------------------------------------------------------------
265 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl
,wxTextCtrl
);
267 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
268 EVT_CHAR (wxTreeTextCtrl::OnChar
)
269 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
272 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
276 wxGenericTreeCtrl
*owner
,
277 const wxString
&value
,
281 const wxValidator
& validator
,
282 const wxString
&name
)
283 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
289 (*m_res
) = wxEmptyString
;
290 m_startValue
= value
;
293 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
295 if (event
.m_keyCode
== WXK_RETURN
)
298 (*m_res
) = GetValue();
300 if (!wxPendingDelete
.Member(this))
301 wxPendingDelete
.Append(this);
303 if ((*m_accept
) && ((*m_res
) != m_startValue
))
304 m_owner
->OnRenameAccept();
308 if (event
.m_keyCode
== WXK_ESCAPE
)
313 if (!wxPendingDelete
.Member(this))
314 wxPendingDelete
.Append(this);
321 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
323 if (!wxPendingDelete
.Member(this))
324 wxPendingDelete
.Append(this);
326 if ((*m_accept
) && ((*m_res
) != m_startValue
))
327 m_owner
->OnRenameAccept();
331 // -----------------------------------------------------------------------------
333 // -----------------------------------------------------------------------------
335 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
337 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
338 : wxNotifyEvent( commandType
, id
)
341 m_itemOld
= (wxGenericTreeItem
*)NULL
;
345 // -----------------------------------------------------------------------------
347 // -----------------------------------------------------------------------------
349 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
350 const wxString
& text
,
352 int image
, int selImage
,
353 wxTreeItemData
*data
)
356 m_images
[wxTreeItemIcon_Normal
] = image
;
357 m_images
[wxTreeItemIcon_Selected
] = selImage
;
358 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
359 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
363 m_xCross
= m_yCross
= 0;
367 m_isCollapsed
= TRUE
;
368 m_hasHilight
= FALSE
;
374 m_attr
= (wxTreeItemAttr
*)NULL
;
376 // We don't know the height here yet.
381 wxGenericTreeItem::~wxGenericTreeItem()
387 wxASSERT_MSG( m_children
.IsEmpty(),
388 wxT("please call DeleteChildren() before deleting the item") );
391 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
393 size_t count
= m_children
.Count();
394 for ( size_t n
= 0; n
< count
; n
++ )
396 wxGenericTreeItem
*child
= m_children
[n
];
398 tree
->SendDeleteEvent(child
);
400 child
->DeleteChildren(tree
);
407 void wxGenericTreeItem::SetText( const wxString
&text
)
412 void wxGenericTreeItem::Reset()
415 for ( int i
= 0; i
< wxTreeItemIcon_Max
; i
++ )
417 m_images
[i
] = NO_IMAGE
;
422 m_height
= m_width
= 0;
429 m_isCollapsed
= TRUE
;
431 m_parent
= (wxGenericTreeItem
*)NULL
;
434 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
436 size_t count
= m_children
.Count();
440 size_t total
= count
;
441 for (size_t n
= 0; n
< count
; ++n
)
443 total
+= m_children
[n
]->GetChildrenCount();
449 void wxGenericTreeItem::SetCross( int x
, int y
)
455 void wxGenericTreeItem::GetSize( int &x
, int &y
, const wxGenericTreeCtrl
*theTree
)
457 int bottomY
=m_y
+theTree
->GetLineHeight(this);
458 if ( y
< bottomY
) y
= bottomY
;
459 int width
= m_x
+ m_width
;
460 if ( x
< width
) x
= width
;
464 size_t count
= m_children
.Count();
465 for ( size_t n
= 0; n
< count
; ++n
)
467 m_children
[n
]->GetSize( x
, y
, theTree
);
472 wxGenericTreeItem
*wxGenericTreeItem::HitTest( const wxPoint
& point
,
473 const wxGenericTreeCtrl
*theTree
,
476 if ((point
.y
> m_y
) && (point
.y
< m_y
+ theTree
->GetLineHeight(this)))
478 if (point
.y
< m_y
+theTree
->GetLineHeight(this)/2 )
479 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
481 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
483 // 5 is the size of the plus sign
484 if ((point
.x
> m_xCross
-5) && (point
.x
< m_xCross
+5) &&
485 (point
.y
> m_yCross
-5) && (point
.y
< m_yCross
+5) &&
486 (IsExpanded() || HasPlus()))
488 flags
|=wxTREE_HITTEST_ONITEMBUTTON
;
492 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
497 // assuming every image (normal and selected ) has the same size !
498 if ( (GetImage() != NO_IMAGE
) && theTree
->m_imageListNormal
)
499 theTree
->m_imageListNormal
->GetSize(GetImage(), image_w
, image_h
);
501 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
502 flags
|= wxTREE_HITTEST_ONITEMICON
;
504 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
510 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
511 if (point
.x
> m_x
+m_width
)
512 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
520 size_t count
= m_children
.Count();
521 for ( size_t n
= 0; n
< count
; n
++ )
523 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
, theTree
, flags
);
530 flags
|=wxTREE_HITTEST_NOWHERE
;
532 return (wxGenericTreeItem
*) NULL
;
535 int wxGenericTreeItem::GetCurrentImage() const
537 int image
= NO_IMAGE
;
542 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
545 if ( image
== NO_IMAGE
)
547 // we usually fall back to the normal item, but try just the
548 // expanded one (and not selected) first in this case
549 image
= GetImage(wxTreeItemIcon_Expanded
);
555 image
= GetImage(wxTreeItemIcon_Selected
);
558 // may be it doesn't have the specific image we want, try the default one
560 if ( image
== NO_IMAGE
)
568 // -----------------------------------------------------------------------------
569 // wxGenericTreeCtrl implementation
570 // -----------------------------------------------------------------------------
572 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
574 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
575 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
576 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
577 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
578 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
579 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
580 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
583 #if !defined(__WXMSW__) || defined(__WIN16__)
585 * wxTreeCtrl has to be a real class or we have problems with
586 * the run-time information.
589 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
592 // -----------------------------------------------------------------------------
593 // construction/destruction
594 // -----------------------------------------------------------------------------
596 void wxGenericTreeCtrl::Init()
600 m_anchor
= (wxGenericTreeItem
*) NULL
;
610 m_hilightBrush
= new wxBrush
612 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
617 m_imageListState
= (wxImageList
*) NULL
;
620 m_isDragging
= FALSE
;
622 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
624 m_renameTimer
= new wxTreeRenameTimer( this );
625 m_lastOnSame
= FALSE
;
627 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
628 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
629 m_normalFont
.GetFamily(),
630 m_normalFont
.GetStyle(),
632 m_normalFont
.GetUnderlined());
635 bool wxGenericTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
636 const wxPoint
& pos
, const wxSize
& size
,
638 const wxValidator
&validator
,
639 const wxString
& name
)
643 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
646 SetValidator( validator
);
649 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
650 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
651 m_dottedPen
= wxPen( "grey", 0, 0 );
656 wxGenericTreeCtrl::~wxGenericTreeCtrl()
658 wxDELETE( m_hilightBrush
);
662 delete m_renameTimer
;
665 // -----------------------------------------------------------------------------
667 // -----------------------------------------------------------------------------
669 size_t wxGenericTreeCtrl::GetCount() const
671 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
674 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
680 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
686 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
688 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
690 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
693 // -----------------------------------------------------------------------------
694 // functions to work with tree items
695 // -----------------------------------------------------------------------------
697 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
699 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
701 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
704 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
705 wxTreeItemIcon which
) const
707 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
709 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
712 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
714 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
716 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
719 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
721 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
724 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
725 pItem
->SetText(text
);
726 CalculateSize(pItem
, dc
);
730 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
732 wxTreeItemIcon which
)
734 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
736 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
737 pItem
->SetImage(image
, which
);
740 CalculateSize(pItem
, dc
);
744 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
746 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
748 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
751 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
753 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
755 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
756 pItem
->SetHasPlus(has
);
760 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
762 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
764 // avoid redrawing the tree if no real change
765 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
766 if ( pItem
->IsBold() != bold
)
768 pItem
->SetBold(bold
);
773 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
776 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
778 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
779 pItem
->Attr().SetTextColour(col
);
783 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
786 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
788 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
789 pItem
->Attr().SetBackgroundColour(col
);
793 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
795 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
797 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
798 pItem
->Attr().SetFont(font
);
802 // -----------------------------------------------------------------------------
803 // item status inquiries
804 // -----------------------------------------------------------------------------
806 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
808 wxFAIL_MSG(wxT("not implemented"));
813 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
815 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
817 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
820 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
822 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
824 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
827 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
829 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
831 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
834 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
836 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
838 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
841 // -----------------------------------------------------------------------------
843 // -----------------------------------------------------------------------------
845 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
847 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
849 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
852 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
854 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
857 return GetNextChild(item
, cookie
);
860 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
862 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
864 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
865 if ( (size_t)cookie
< children
.Count() )
867 return children
.Item((size_t)cookie
++);
871 // there are no more of them
872 return wxTreeItemId();
876 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
878 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
880 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
881 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
884 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
886 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
888 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
889 wxGenericTreeItem
*parent
= i
->GetParent();
890 if ( parent
== NULL
)
892 // root item doesn't have any siblings
893 return wxTreeItemId();
896 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
897 int index
= siblings
.Index(i
);
898 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
900 size_t n
= (size_t)(index
+ 1);
901 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
904 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
906 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
908 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
909 wxGenericTreeItem
*parent
= i
->GetParent();
910 if ( parent
== NULL
)
912 // root item doesn't have any siblings
913 return wxTreeItemId();
916 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
917 int index
= siblings
.Index(i
);
918 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
920 return index
== 0 ? wxTreeItemId()
921 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
924 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
926 wxFAIL_MSG(wxT("not implemented"));
928 return wxTreeItemId();
931 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
933 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
935 wxFAIL_MSG(wxT("not implemented"));
937 return wxTreeItemId();
940 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
942 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
944 wxFAIL_MSG(wxT("not implemented"));
946 return wxTreeItemId();
949 // -----------------------------------------------------------------------------
951 // -----------------------------------------------------------------------------
953 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
955 const wxString
& text
,
956 int image
, int selImage
,
957 wxTreeItemData
*data
)
959 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
962 // should we give a warning here?
963 return AddRoot(text
, image
, selImage
, data
);
967 wxGenericTreeItem
*item
=
968 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
972 data
->m_pItem
= item
;
975 parent
->Insert( item
, previous
);
982 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
983 int image
, int selImage
,
984 wxTreeItemData
*data
)
986 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
989 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
990 image
, selImage
, data
);
993 data
->m_pItem
= m_anchor
;
996 if (!HasFlag(wxTR_MULTIPLE
))
998 m_current
= m_key_current
= m_anchor
;
999 m_current
->SetHilight( TRUE
);
1007 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1008 const wxString
& text
,
1009 int image
, int selImage
,
1010 wxTreeItemData
*data
)
1012 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1015 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1016 const wxTreeItemId
& idPrevious
,
1017 const wxString
& text
,
1018 int image
, int selImage
,
1019 wxTreeItemData
*data
)
1021 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1024 // should we give a warning here?
1025 return AddRoot(text
, image
, selImage
, data
);
1028 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1029 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1030 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1032 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1035 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1037 const wxString
& text
,
1038 int image
, int selImage
,
1039 wxTreeItemData
*data
)
1041 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1044 // should we give a warning here?
1045 return AddRoot(text
, image
, selImage
, data
);
1048 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1051 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1052 const wxString
& text
,
1053 int image
, int selImage
,
1054 wxTreeItemData
*data
)
1056 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1059 // should we give a warning here?
1060 return AddRoot(text
, image
, selImage
, data
);
1063 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1064 image
, selImage
, data
);
1067 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1069 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1070 event
.m_item
= item
;
1071 event
.SetEventObject( this );
1072 ProcessEvent( event
);
1075 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1077 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1078 item
->DeleteChildren(this);
1083 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1085 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1087 // don't stay with invalid m_key_current or we will crash in the next call
1089 bool changeKeyCurrent
= FALSE
;
1090 wxGenericTreeItem
*itemKey
= m_key_current
;
1091 while ( itemKey
&& !changeKeyCurrent
)
1093 if ( itemKey
== item
)
1095 // m_key_current is a descendant of the item being deleted
1096 changeKeyCurrent
= TRUE
;
1100 itemKey
= itemKey
->GetParent();
1104 wxGenericTreeItem
*parent
= item
->GetParent();
1107 parent
->GetChildren().Remove( item
); // remove by value
1110 if ( changeKeyCurrent
)
1112 // may be NULL or not
1113 m_key_current
= parent
;
1116 item
->DeleteChildren(this);
1117 SendDeleteEvent(item
);
1123 void wxGenericTreeCtrl::DeleteAllItems()
1127 m_anchor
->DeleteChildren(this);
1136 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1138 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1140 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1142 if ( !item
->HasPlus() )
1145 if ( item
->IsExpanded() )
1148 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1149 event
.m_item
= item
;
1150 event
.SetEventObject( this );
1152 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1154 // cancelled by program
1159 CalculatePositions();
1161 RefreshSubtree(item
);
1163 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1164 ProcessEvent( event
);
1167 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1170 if ( IsExpanded(item
) )
1173 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1174 while ( child
.IsOk() )
1178 child
= GetNextChild(item
, cookie
);
1183 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1185 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1187 if ( !item
->IsExpanded() )
1190 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1191 event
.m_item
= item
;
1192 event
.SetEventObject( this );
1193 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1195 // cancelled by program
1201 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1202 size_t count
= children
.Count();
1203 for ( size_t n
= 0; n
< count
; n
++ )
1205 Collapse(children
[n
]);
1208 CalculatePositions();
1210 RefreshSubtree(item
);
1212 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1213 ProcessEvent( event
);
1216 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1219 DeleteChildren(item
);
1222 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1224 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1226 if (item
->IsExpanded())
1232 void wxGenericTreeCtrl::Unselect()
1236 m_current
->SetHilight( FALSE
);
1237 RefreshLine( m_current
);
1241 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1243 if (item
->IsSelected())
1245 item
->SetHilight(FALSE
);
1249 if (item
->HasChildren())
1251 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1252 size_t count
= children
.Count();
1253 for ( size_t n
= 0; n
< count
; ++n
)
1255 UnselectAllChildren(children
[n
]);
1260 void wxGenericTreeCtrl::UnselectAll()
1262 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1265 // Recursive function !
1266 // To stop we must have crt_item<last_item
1268 // Tag all next children, when no more children,
1269 // Move to parent (not to tag)
1270 // Keep going... if we found last_item, we stop.
1271 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1273 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1275 if (parent
== NULL
) // This is root item
1276 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1278 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1279 int index
= children
.Index(crt_item
);
1280 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1282 size_t count
= children
.Count();
1283 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1285 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1288 return TagNextChildren(parent
, last_item
, select
);
1291 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1293 crt_item
->SetHilight(select
);
1294 RefreshLine(crt_item
);
1296 if (crt_item
==last_item
)
1299 if (crt_item
->HasChildren())
1301 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1302 size_t count
= children
.Count();
1303 for ( size_t n
= 0; n
< count
; ++n
)
1305 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1313 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1315 // item2 is not necessary after item1
1316 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1318 // choice first' and 'last' between item1 and item2
1319 if (item1
->GetY()<item2
->GetY())
1330 bool select
= m_current
->IsSelected();
1332 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1335 TagNextChildren(first
,last
,select
);
1338 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1339 bool unselect_others
,
1340 bool extended_select
)
1342 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1344 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1345 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1347 //wxCHECK_RET( ( (!unselect_others) && is_single),
1348 // wxT("this is a single selection tree") );
1350 // to keep going anyhow !!!
1353 if (item
->IsSelected())
1354 return; // nothing to do
1355 unselect_others
= TRUE
;
1356 extended_select
= FALSE
;
1358 else if ( unselect_others
&& item
->IsSelected() )
1360 // selection change if there is more than one item currently selected
1361 wxArrayTreeItemIds selected_items
;
1362 if ( GetSelections(selected_items
) == 1 )
1366 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1367 event
.m_item
= item
;
1368 event
.m_itemOld
= m_current
;
1369 event
.SetEventObject( this );
1370 // TODO : Here we don't send any selection mode yet !
1372 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1376 if (unselect_others
)
1378 if (is_single
) Unselect(); // to speed up thing
1383 if (extended_select
)
1388 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1391 // don't change the mark (m_current)
1392 SelectItemRange(m_current
, item
);
1396 bool select
=TRUE
; // the default
1398 // Check if we need to toggle hilight (ctrl mode)
1399 if (!unselect_others
)
1400 select
=!item
->IsSelected();
1402 m_current
= m_key_current
= item
;
1403 m_current
->SetHilight(select
);
1404 RefreshLine( m_current
);
1407 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1408 GetEventHandler()->ProcessEvent( event
);
1411 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1412 wxArrayTreeItemIds
&array
) const
1414 if ( item
->IsSelected() )
1415 array
.Add(wxTreeItemId(item
));
1417 if ( item
->HasChildren() )
1419 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1420 size_t count
= children
.GetCount();
1421 for ( size_t n
= 0; n
< count
; ++n
)
1422 FillArray(children
[n
], array
);
1426 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1429 wxTreeItemId idRoot
= GetRootItem();
1430 if ( idRoot
.IsOk() )
1432 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1434 //else: the tree is empty, so no selections
1436 return array
.Count();
1439 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1441 if (!item
.IsOk()) return;
1443 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1445 // first expand all parent branches
1446 wxGenericTreeItem
*parent
= gitem
->GetParent();
1450 parent
= parent
->GetParent();
1453 //if (parent) CalculatePositions();
1458 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1460 if (!item
.IsOk()) return;
1462 // We have to call this here because the label in
1463 // question might just have been added and no screen
1464 // update taken place.
1465 if (m_dirty
) wxYield();
1467 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1469 // now scroll to the item
1470 int item_y
= gitem
->GetY();
1474 ViewStart( &start_x
, &start_y
);
1475 start_y
*= PIXELS_PER_UNIT
;
1479 GetClientSize( &client_w
, &client_h
);
1481 if (item_y
< start_y
+3)
1486 m_anchor
->GetSize( x
, y
, this );
1487 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1488 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1489 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1490 // Item should appear at top
1491 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1493 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1498 m_anchor
->GetSize( x
, y
, this );
1499 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1500 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1501 item_y
+= PIXELS_PER_UNIT
+2;
1502 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1503 // Item should appear at bottom
1504 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
);
1508 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1509 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1511 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1512 wxGenericTreeItem
**item2
)
1514 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1516 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1519 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1520 const wxTreeItemId
& item2
)
1522 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1525 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1527 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1529 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1531 wxCHECK_RET( !s_treeBeingSorted
,
1532 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1534 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1535 if ( children
.Count() > 1 )
1537 s_treeBeingSorted
= this;
1538 children
.Sort(tree_ctrl_compare_func
);
1539 s_treeBeingSorted
= NULL
;
1543 //else: don't make the tree dirty as nothing changed
1546 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1548 return m_imageListNormal
;
1551 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1553 return m_imageListState
;
1556 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1558 m_imageListNormal
= imageList
;
1560 if ( !m_imageListNormal
)
1563 // Calculate a m_lineHeight value from the image sizes.
1564 // May be toggle off. Then wxGenericTreeCtrl will spread when
1565 // necessary (which might look ugly).
1566 wxClientDC
dc(this);
1567 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1568 int width
= 0, height
= 0,
1569 n
= m_imageListNormal
->GetImageCount();
1571 for (int i
= 0; i
< n
; i
++)
1573 m_imageListNormal
->GetSize(i
, width
, height
);
1574 if (height
> m_lineHeight
) m_lineHeight
= height
;
1577 if (m_lineHeight
< 40)
1578 m_lineHeight
+= 2; // at least 2 pixels
1580 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1583 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1585 m_imageListState
= imageList
;
1588 // -----------------------------------------------------------------------------
1590 // -----------------------------------------------------------------------------
1592 void wxGenericTreeCtrl::AdjustMyScrollbars()
1598 m_anchor
->GetSize( x
, y
, this );
1599 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1600 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1601 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1602 int y_pos
= GetScrollPos( wxVERTICAL
);
1603 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1607 SetScrollbars( 0, 0, 0, 0 );
1611 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1613 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1614 return item
->GetHeight();
1616 return m_lineHeight
;
1619 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1621 wxTreeItemAttr
*attr
= item
->GetAttributes();
1622 if ( attr
&& attr
->HasFont() )
1623 dc
.SetFont(attr
->GetFont());
1624 else if (item
->IsBold())
1625 dc
.SetFont(m_boldFont
);
1629 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1633 int image
= item
->GetCurrentImage();
1634 if ( image
!= NO_IMAGE
)
1636 if ( m_imageListNormal
)
1638 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1647 int total_h
= GetLineHeight(item
);
1649 if (item
->IsSelected())
1650 dc
.SetBrush(*m_hilightBrush
);
1654 if ( attr
&& attr
->HasBackgroundColour() )
1655 colBg
= attr
->GetBackgroundColour();
1657 colBg
= m_backgroundColour
;
1658 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1661 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1663 if ( image
!= NO_IMAGE
)
1665 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1666 m_imageListNormal
->Draw( image
, dc
,
1668 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1669 wxIMAGELIST_DRAW_TRANSPARENT
);
1670 dc
.DestroyClippingRegion();
1673 dc
.SetBackgroundMode(wxTRANSPARENT
);
1674 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1675 dc
.DrawText( item
->GetText(),
1676 (wxCoord
)(image_w
+ item
->GetX()),
1677 (wxCoord
)(item
->GetY() + extraH
));
1679 // restore normal font
1680 dc
.SetFont( m_normalFont
);
1683 // Now y stands for the top of the item, whereas it used to stand for middle !
1684 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1686 int horizX
= level
*m_indent
;
1688 item
->SetX( horizX
+m_indent
+m_spacing
);
1692 y
+=GetLineHeight(item
)/2;
1694 item
->SetCross( horizX
+m_indent
, y
);
1696 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1697 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1699 bool drawLines
= ((GetWindowStyle() & wxTR_NO_LINES
) == 0);
1701 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1703 int startX
= horizX
;
1704 int endX
= horizX
+ (m_indent
-5);
1706 // if (!item->HasChildren()) endX += (m_indent+5);
1707 if (!item
->HasChildren()) endX
+= 20;
1710 dc
.DrawLine( startX
, y
, endX
, y
);
1712 if (item
->HasPlus())
1715 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1716 dc
.SetPen( *wxGREY_PEN
);
1717 dc
.SetBrush( *wxWHITE_BRUSH
);
1718 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1720 dc
.SetPen( *wxBLACK_PEN
);
1721 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1722 if (!item
->IsExpanded())
1723 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1725 dc
.SetPen( m_dottedPen
);
1728 wxPen
*pen
= wxTRANSPARENT_PEN
;
1731 if ( item
->IsSelected() )
1733 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1741 wxTreeItemAttr
*attr
= item
->GetAttributes();
1742 if ( attr
&& attr
->HasTextColour() )
1743 colText
= attr
->GetTextColour();
1749 dc
.SetTextForeground(colText
);
1753 PaintItem(item
, dc
);
1755 // restore DC objects
1756 dc
.SetBrush( *wxWHITE_BRUSH
);
1757 dc
.SetPen( m_dottedPen
);
1758 dc
.SetTextForeground( *wxBLACK
);
1761 y
= oldY
+GetLineHeight(item
);
1763 if (item
->IsExpanded())
1765 oldY
+=GetLineHeight(item
)/2;
1768 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1769 size_t n
, count
= children
.Count();
1770 for ( n
= 0; n
< count
; ++n
)
1773 PaintLevel( children
[n
], dc
, level
+1, y
);
1776 // it may happen that the item is expanded but has no items (when you
1777 // delete all its children for example) - don't draw the vertical line
1781 semiOldY
+=GetLineHeight(children
[--n
])/2;
1783 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1788 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1792 if ( item
->HasPlus() )
1794 // it's a folder, indicate it by a border
1799 // draw a line under the drop target because the item will be
1801 DrawLine(item
, TRUE
/* below */);
1804 SetCursor(wxCURSOR_BULLSEYE
);
1809 SetCursor(wxCURSOR_NO_ENTRY
);
1813 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
1815 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1817 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1819 wxClientDC
dc(this);
1821 dc
.SetLogicalFunction(wxINVERT
);
1822 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1824 int w
= i
->GetWidth() + 2;
1825 int h
= GetLineHeight(i
) + 2;
1827 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
1830 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
1832 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1834 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1836 wxClientDC
dc(this);
1838 dc
.SetLogicalFunction(wxINVERT
);
1844 y
+= GetLineHeight(i
) - 1;
1847 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
1850 // -----------------------------------------------------------------------------
1851 // wxWindows callbacks
1852 // -----------------------------------------------------------------------------
1854 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1862 dc
.SetFont( m_normalFont
);
1863 dc
.SetPen( m_dottedPen
);
1865 // this is now done dynamically
1866 //if(GetImageList() == NULL)
1867 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1870 PaintLevel( m_anchor
, dc
, 0, y
);
1873 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1877 if (m_current
) RefreshLine( m_current
);
1880 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1884 if (m_current
) RefreshLine( m_current
);
1887 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
1889 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1890 te
.m_code
= (int)event
.KeyCode();
1891 te
.SetEventObject( this );
1892 GetEventHandler()->ProcessEvent( te
);
1894 if ( (m_current
== 0) || (m_key_current
== 0) )
1900 // how should the selection work for this event?
1901 bool is_multiple
, extended_select
, unselect_others
;
1902 EventFlagsToSelType(GetWindowStyleFlag(),
1904 event
.ControlDown(),
1905 is_multiple
, extended_select
, unselect_others
);
1909 // * : Expand all/Collapse all
1910 // ' ' | return : activate
1911 // up : go up (not last children!)
1913 // left : go to parent
1914 // right : open if parent and go next
1915 // home : go to root
1916 // end : go to last item without opening parents
1917 switch (event
.KeyCode())
1921 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1929 if ( !IsExpanded(m_current
) )
1932 ExpandAll(m_current
);
1935 //else: fall through to Collapse() it
1939 if (IsExpanded(m_current
))
1941 Collapse(m_current
);
1948 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1949 event
.m_item
= m_current
;
1951 event
.SetEventObject( this );
1952 GetEventHandler()->ProcessEvent( event
);
1956 // up goes to the previous sibling or to the last of its children if
1960 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1963 prev
= GetParent( m_key_current
);
1967 wxTreeItemId current
= m_key_current
;
1968 if (current
== GetFirstChild( prev
, cockie
))
1970 // otherwise we return to where we came from
1971 SelectItem( prev
, unselect_others
, extended_select
);
1972 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
1973 EnsureVisible( prev
);
1980 while ( IsExpanded(prev
) && HasChildren(prev
) )
1982 wxTreeItemId child
= GetLastChild(prev
);
1989 SelectItem( prev
, unselect_others
, extended_select
);
1990 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
1991 EnsureVisible( prev
);
1996 // left arrow goes to the parent
1999 wxTreeItemId prev
= GetParent( m_current
);
2002 EnsureVisible( prev
);
2003 SelectItem( prev
, unselect_others
, extended_select
);
2009 // this works the same as the down arrow except that we also expand the
2010 // item if it wasn't expanded yet
2016 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2019 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2020 SelectItem( child
, unselect_others
, extended_select
);
2021 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2022 EnsureVisible( child
);
2026 wxTreeItemId next
= GetNextSibling( m_key_current
);
2029 wxTreeItemId current
= m_key_current
;
2030 while (current
&& !next
)
2032 current
= GetParent( current
);
2033 if (current
) next
= GetNextSibling( current
);
2038 SelectItem( next
, unselect_others
, extended_select
);
2039 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2040 EnsureVisible( next
);
2046 // <End> selects the last visible tree item
2049 wxTreeItemId last
= GetRootItem();
2051 while ( last
.IsOk() && IsExpanded(last
) )
2053 wxTreeItemId lastChild
= GetLastChild(last
);
2055 // it may happen if the item was expanded but then all of
2056 // its children have been deleted - so IsExpanded() returned
2057 // TRUE, but GetLastChild() returned invalid item
2066 EnsureVisible( last
);
2067 SelectItem( last
, unselect_others
, extended_select
);
2072 // <Home> selects the root item
2075 wxTreeItemId prev
= GetRootItem();
2078 EnsureVisible( prev
);
2079 SelectItem( prev
, unselect_others
, extended_select
);
2089 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2091 // We have to call this here because the label in
2092 // question might just have been added and no screen
2093 // update taken place.
2094 if (m_dirty
) wxYield();
2096 wxClientDC
dc(this);
2098 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2099 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2104 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2105 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2106 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2107 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2109 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2112 // get the bounding rectangle of the item (or of its label only)
2113 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2115 bool textOnly
) const
2117 wxCHECK2_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2119 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2122 GetViewStart(& startX
, & startY
);
2124 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
; rect
.y
= i
->GetY()*PIXELS_PER_UNIT
;
2125 rect
.width
= i
->GetWidth(); rect
.height
= i
->GetHeight();
2129 // wxFAIL_MSG(wxT("GetBoundingRect unimplemented"));
2135 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2137 if (!item
.IsOk()) return;
2139 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2141 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2142 te
.m_item
= m_currentEdit
;
2143 te
.SetEventObject( this );
2144 GetEventHandler()->ProcessEvent( te
);
2146 if (!te
.IsAllowed()) return;
2148 // We have to call this here because the label in
2149 // question might just have been added and no screen
2150 // update taken place.
2151 if (m_dirty
) wxYield();
2153 wxString s
= m_currentEdit
->GetText();
2154 int x
= m_currentEdit
->GetX();
2155 int y
= m_currentEdit
->GetY();
2156 int w
= m_currentEdit
->GetWidth();
2157 int h
= m_currentEdit
->GetHeight();
2162 int image
= m_currentEdit
->GetCurrentImage();
2163 if ( image
!= NO_IMAGE
)
2165 if ( m_imageListNormal
)
2167 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2172 wxFAIL_MSG(_T("you must create an image list to use images!"));
2176 w
-= image_w
+ 4; // I don't know why +4 is needed
2178 wxClientDC
dc(this);
2180 x
= dc
.LogicalToDeviceX( x
);
2181 y
= dc
.LogicalToDeviceY( y
);
2183 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
2184 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
2188 void wxGenericTreeCtrl::OnRenameTimer()
2193 void wxGenericTreeCtrl::OnRenameAccept()
2195 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2196 le
.m_item
= m_currentEdit
;
2197 le
.SetEventObject( this );
2198 le
.m_label
= m_renameRes
;
2199 GetEventHandler()->ProcessEvent( le
);
2201 if (!le
.IsAllowed()) return;
2203 SetItemText( m_currentEdit
, m_renameRes
);
2206 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2208 if ( !m_anchor
) return;
2210 // we process left mouse up event (enables in-place edit), right down
2211 // (pass to the user code), left dbl click (activate item) and
2212 // dragging/moving events for items drag-and-drop
2213 if ( !(event
.LeftDown() ||
2215 event
.RightDown() ||
2216 event
.LeftDClick() ||
2218 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2225 wxClientDC
dc(this);
2227 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2228 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2231 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2233 if ( event
.Dragging() && !m_isDragging
)
2235 if (m_dragCount
== 0)
2236 m_dragStart
= wxPoint(x
,y
);
2240 if (m_dragCount
!= 3)
2242 // wait until user drags a bit further...
2246 wxEventType command
= event
.RightIsDown()
2247 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2248 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2250 wxTreeEvent
nevent( command
, GetId() );
2251 nevent
.m_item
= m_current
;
2252 nevent
.SetEventObject(this);
2254 // by default the dragging is not supported, the user code must
2255 // explicitly allow the event for it to take place
2258 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2260 // we're going to drag this item
2261 m_isDragging
= TRUE
;
2263 // remember the old cursor because we will change it while
2265 m_oldCursor
= m_cursor
;
2267 // in a single selection control, hide the selection temporarily
2268 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2270 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2272 if ( m_oldSelection
)
2274 m_oldSelection
->SetHilight(FALSE
);
2275 RefreshLine(m_oldSelection
);
2282 else if ( event
.Moving() )
2284 if ( item
!= m_dropTarget
)
2286 // unhighlight the previous drop target
2287 DrawDropEffect(m_dropTarget
);
2289 m_dropTarget
= item
;
2291 // highlight the current drop target if any
2292 DrawDropEffect(m_dropTarget
);
2297 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2299 // erase the highlighting
2300 DrawDropEffect(m_dropTarget
);
2302 // generate the drag end event
2303 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2305 event
.m_item
= item
;
2306 event
.m_pointDrag
= wxPoint(x
, y
);
2307 event
.SetEventObject(this);
2309 (void)GetEventHandler()->ProcessEvent(event
);
2311 m_isDragging
= FALSE
;
2312 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2314 if ( m_oldSelection
)
2316 m_oldSelection
->SetHilight(TRUE
);
2317 RefreshLine(m_oldSelection
);
2318 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2323 SetCursor(m_oldCursor
);
2329 // here we process only the messages which happen on tree items
2333 if (item
== NULL
) return; /* we hit the blank area */
2335 if ( event
.RightDown() )
2337 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2338 nevent
.m_item
= item
;
2340 nevent
.SetEventObject(this);
2341 GetEventHandler()->ProcessEvent(nevent
);
2343 else if ( event
.LeftUp() && m_lastOnSame
)
2345 if ( (item
== m_current
) &&
2346 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2347 HasFlag(wxTR_EDIT_LABELS
) )
2349 m_renameTimer
->Start( 100, TRUE
);
2352 m_lastOnSame
= FALSE
;
2356 if ( event
.LeftDown() )
2358 m_lastOnSame
= item
== m_current
;
2361 // how should the selection work for this event?
2362 bool is_multiple
, extended_select
, unselect_others
;
2363 EventFlagsToSelType(GetWindowStyleFlag(),
2365 event
.ControlDown(),
2366 is_multiple
, extended_select
, unselect_others
);
2368 if ( (flags
& wxTREE_HITTEST_ONITEMBUTTON
) && event
.LeftDown() )
2375 SelectItem(item
, unselect_others
, extended_select
);
2377 if ( event
.LeftDClick() )
2379 // double clicking should not start editing the item label
2380 m_renameTimer
->Stop();
2381 m_lastOnSame
= FALSE
;
2383 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2384 nevent
.m_item
= item
;
2386 nevent
.SetEventObject( this );
2387 GetEventHandler()->ProcessEvent( nevent
);
2393 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2395 /* after all changes have been done to the tree control,
2396 * we actually redraw the tree when everything is over */
2403 CalculatePositions();
2405 AdjustMyScrollbars();
2408 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2414 dc
.SetFont(m_boldFont
);
2416 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2419 // restore normal font
2420 dc
.SetFont( m_normalFont
);
2424 int image
= item
->GetCurrentImage();
2425 if ( image
!= NO_IMAGE
)
2427 if ( m_imageListNormal
)
2429 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2434 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2437 total_h
+= 2; // at least 2 pixels
2439 total_h
+= total_h
/10; // otherwise 10% extra spacing
2441 item
->SetHeight(total_h
);
2442 if (total_h
>m_lineHeight
)
2443 m_lineHeight
=total_h
;
2445 item
->SetWidth(image_w
+text_w
+2);
2448 // -----------------------------------------------------------------------------
2449 // for developper : y is now the top of the level
2450 // not the middle of it !
2451 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2453 int horizX
= level
*m_indent
;
2455 CalculateSize( item
, dc
);
2458 item
->SetX( horizX
+m_indent
+m_spacing
);
2460 y
+=GetLineHeight(item
);
2462 if ( !item
->IsExpanded() )
2464 // we dont need to calculate collapsed branches
2468 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2469 size_t n
, count
= children
.Count();
2470 for (n
= 0; n
< count
; ++n
)
2471 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2474 void wxGenericTreeCtrl::CalculatePositions()
2476 if ( !m_anchor
) return;
2478 wxClientDC
dc(this);
2481 dc
.SetFont( m_normalFont
);
2483 dc
.SetPen( m_dottedPen
);
2484 //if(GetImageList() == NULL)
2485 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2488 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2491 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2493 if (m_dirty
) return;
2495 wxClientDC
dc(this);
2500 GetClientSize( &cw
, &ch
);
2503 rect
.x
= dc
.LogicalToDeviceX( 0 );
2505 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2508 Refresh( TRUE
, &rect
);
2510 AdjustMyScrollbars();
2513 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2515 if (m_dirty
) return;
2517 wxClientDC
dc(this);
2522 GetClientSize( &cw
, &ch
);
2525 rect
.x
= dc
.LogicalToDeviceX( 0 );
2526 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2528 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2530 Refresh( TRUE
, &rect
);