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
,
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 // -----------------------------------------------------------------------------
584 // construction/destruction
585 // -----------------------------------------------------------------------------
587 void wxGenericTreeCtrl::Init()
591 m_anchor
= (wxGenericTreeItem
*) NULL
;
601 m_hilightBrush
= new wxBrush
603 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
),
608 m_imageListState
= (wxImageList
*) NULL
;
611 m_isDragging
= FALSE
;
613 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
615 m_renameTimer
= new wxTreeRenameTimer( this );
616 m_lastOnSame
= FALSE
;
618 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
619 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
620 m_normalFont
.GetFamily(),
621 m_normalFont
.GetStyle(),
623 m_normalFont
.GetUnderlined());
626 bool wxGenericTreeCtrl::Create(wxWindow
*parent
, wxWindowID id
,
627 const wxPoint
& pos
, const wxSize
& size
,
629 const wxValidator
&validator
,
630 const wxString
& name
)
634 wxScrolledWindow::Create( parent
, id
, pos
, size
, style
|wxHSCROLL
|wxVSCROLL
, name
);
637 SetValidator( validator
);
640 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
641 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
642 m_dottedPen
= wxPen( "grey", 0, 0 );
647 wxGenericTreeCtrl::~wxGenericTreeCtrl()
649 wxDELETE( m_hilightBrush
);
653 delete m_renameTimer
;
656 // -----------------------------------------------------------------------------
658 // -----------------------------------------------------------------------------
660 size_t wxGenericTreeCtrl::GetCount() const
662 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
665 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
671 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
677 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
679 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
681 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
684 // -----------------------------------------------------------------------------
685 // functions to work with tree items
686 // -----------------------------------------------------------------------------
688 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
690 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
692 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
695 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
696 wxTreeItemIcon which
) const
698 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
700 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
703 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
705 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
707 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
710 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
712 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
715 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
716 pItem
->SetText(text
);
717 CalculateSize(pItem
, dc
);
721 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
723 wxTreeItemIcon which
)
725 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
727 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
728 pItem
->SetImage(image
, which
);
731 CalculateSize(pItem
, dc
);
735 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
737 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
739 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
742 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
744 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
746 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
747 pItem
->SetHasPlus(has
);
751 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
753 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
755 // avoid redrawing the tree if no real change
756 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
757 if ( pItem
->IsBold() != bold
)
759 pItem
->SetBold(bold
);
764 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
767 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
769 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
770 pItem
->Attr().SetTextColour(col
);
774 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
777 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
779 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
780 pItem
->Attr().SetBackgroundColour(col
);
784 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
786 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
788 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
789 pItem
->Attr().SetFont(font
);
793 // -----------------------------------------------------------------------------
794 // item status inquiries
795 // -----------------------------------------------------------------------------
797 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& WXUNUSED(item
)) const
799 wxFAIL_MSG(wxT("not implemented"));
804 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
806 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
808 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
811 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
813 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
815 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
818 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
820 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
822 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
825 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
827 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
829 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
832 // -----------------------------------------------------------------------------
834 // -----------------------------------------------------------------------------
836 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
838 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
840 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
843 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
845 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
848 return GetNextChild(item
, cookie
);
851 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
853 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
855 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
856 if ( (size_t)cookie
< children
.Count() )
858 return children
.Item((size_t)cookie
++);
862 // there are no more of them
863 return wxTreeItemId();
867 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
869 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
871 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
872 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
875 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
877 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
879 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
880 wxGenericTreeItem
*parent
= i
->GetParent();
881 if ( parent
== NULL
)
883 // root item doesn't have any siblings
884 return wxTreeItemId();
887 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
888 int index
= siblings
.Index(i
);
889 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
891 size_t n
= (size_t)(index
+ 1);
892 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
895 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
897 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
899 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
900 wxGenericTreeItem
*parent
= i
->GetParent();
901 if ( parent
== NULL
)
903 // root item doesn't have any siblings
904 return wxTreeItemId();
907 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
908 int index
= siblings
.Index(i
);
909 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
911 return index
== 0 ? wxTreeItemId()
912 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
915 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
917 wxFAIL_MSG(wxT("not implemented"));
919 return wxTreeItemId();
922 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
924 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
926 wxFAIL_MSG(wxT("not implemented"));
928 return wxTreeItemId();
931 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
933 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
935 wxFAIL_MSG(wxT("not implemented"));
937 return wxTreeItemId();
940 // -----------------------------------------------------------------------------
942 // -----------------------------------------------------------------------------
944 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
946 const wxString
& text
,
947 int image
, int selImage
,
948 wxTreeItemData
*data
)
950 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
953 // should we give a warning here?
954 return AddRoot(text
, image
, selImage
, data
);
958 wxGenericTreeItem
*item
=
959 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
963 data
->m_pItem
= item
;
966 parent
->Insert( item
, previous
);
973 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
974 int image
, int selImage
,
975 wxTreeItemData
*data
)
977 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
980 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
981 image
, selImage
, data
);
984 data
->m_pItem
= m_anchor
;
987 if (!HasFlag(wxTR_MULTIPLE
))
989 m_current
= m_key_current
= m_anchor
;
990 m_current
->SetHilight( TRUE
);
998 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
999 const wxString
& text
,
1000 int image
, int selImage
,
1001 wxTreeItemData
*data
)
1003 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1006 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1007 const wxTreeItemId
& idPrevious
,
1008 const wxString
& text
,
1009 int image
, int selImage
,
1010 wxTreeItemData
*data
)
1012 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1015 // should we give a warning here?
1016 return AddRoot(text
, image
, selImage
, data
);
1019 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1020 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1021 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1023 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1026 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1028 const wxString
& text
,
1029 int image
, int selImage
,
1030 wxTreeItemData
*data
)
1032 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1035 // should we give a warning here?
1036 return AddRoot(text
, image
, selImage
, data
);
1039 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1042 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1043 const wxString
& text
,
1044 int image
, int selImage
,
1045 wxTreeItemData
*data
)
1047 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1050 // should we give a warning here?
1051 return AddRoot(text
, image
, selImage
, data
);
1054 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1055 image
, selImage
, data
);
1058 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1060 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1061 event
.m_item
= item
;
1062 event
.SetEventObject( this );
1063 ProcessEvent( event
);
1066 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1068 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1069 item
->DeleteChildren(this);
1074 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1076 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1078 // don't stay with invalid m_key_current or we will crash in the next call
1080 bool changeKeyCurrent
= FALSE
;
1081 wxGenericTreeItem
*itemKey
= m_key_current
;
1082 while ( itemKey
&& !changeKeyCurrent
)
1084 if ( itemKey
== item
)
1086 // m_key_current is a descendant of the item being deleted
1087 changeKeyCurrent
= TRUE
;
1091 itemKey
= itemKey
->GetParent();
1095 wxGenericTreeItem
*parent
= item
->GetParent();
1098 parent
->GetChildren().Remove( item
); // remove by value
1101 if ( changeKeyCurrent
)
1103 // may be NULL or not
1104 m_key_current
= parent
;
1107 item
->DeleteChildren(this);
1108 SendDeleteEvent(item
);
1114 void wxGenericTreeCtrl::DeleteAllItems()
1118 m_anchor
->DeleteChildren(this);
1127 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1129 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1131 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1133 if ( !item
->HasPlus() )
1136 if ( item
->IsExpanded() )
1139 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1140 event
.m_item
= item
;
1141 event
.SetEventObject( this );
1143 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1145 // cancelled by program
1150 CalculatePositions();
1152 RefreshSubtree(item
);
1154 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1155 ProcessEvent( event
);
1158 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1161 if ( IsExpanded(item
) )
1164 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1165 while ( child
.IsOk() )
1169 child
= GetNextChild(item
, cookie
);
1174 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1176 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1178 if ( !item
->IsExpanded() )
1181 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1182 event
.m_item
= item
;
1183 event
.SetEventObject( this );
1184 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1186 // cancelled by program
1192 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1193 size_t count
= children
.Count();
1194 for ( size_t n
= 0; n
< count
; n
++ )
1196 Collapse(children
[n
]);
1199 CalculatePositions();
1201 RefreshSubtree(item
);
1203 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1204 ProcessEvent( event
);
1207 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1210 DeleteChildren(item
);
1213 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1215 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1217 if (item
->IsExpanded())
1223 void wxGenericTreeCtrl::Unselect()
1227 m_current
->SetHilight( FALSE
);
1228 RefreshLine( m_current
);
1232 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1234 if (item
->IsSelected())
1236 item
->SetHilight(FALSE
);
1240 if (item
->HasChildren())
1242 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1243 size_t count
= children
.Count();
1244 for ( size_t n
= 0; n
< count
; ++n
)
1246 UnselectAllChildren(children
[n
]);
1251 void wxGenericTreeCtrl::UnselectAll()
1253 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1256 // Recursive function !
1257 // To stop we must have crt_item<last_item
1259 // Tag all next children, when no more children,
1260 // Move to parent (not to tag)
1261 // Keep going... if we found last_item, we stop.
1262 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1264 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1266 if (parent
== NULL
) // This is root item
1267 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1269 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1270 int index
= children
.Index(crt_item
);
1271 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1273 size_t count
= children
.Count();
1274 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1276 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1279 return TagNextChildren(parent
, last_item
, select
);
1282 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1284 crt_item
->SetHilight(select
);
1285 RefreshLine(crt_item
);
1287 if (crt_item
==last_item
)
1290 if (crt_item
->HasChildren())
1292 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1293 size_t count
= children
.Count();
1294 for ( size_t n
= 0; n
< count
; ++n
)
1296 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1304 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1306 // item2 is not necessary after item1
1307 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1309 // choice first' and 'last' between item1 and item2
1310 if (item1
->GetY()<item2
->GetY())
1321 bool select
= m_current
->IsSelected();
1323 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1326 TagNextChildren(first
,last
,select
);
1329 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1330 bool unselect_others
,
1331 bool extended_select
)
1333 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1335 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1336 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1338 //wxCHECK_RET( ( (!unselect_others) && is_single),
1339 // wxT("this is a single selection tree") );
1341 // to keep going anyhow !!!
1344 if (item
->IsSelected())
1345 return; // nothing to do
1346 unselect_others
= TRUE
;
1347 extended_select
= FALSE
;
1349 else if ( unselect_others
&& item
->IsSelected() )
1351 // selection change if there is more than one item currently selected
1352 wxArrayTreeItemIds selected_items
;
1353 if ( GetSelections(selected_items
) == 1 )
1357 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1358 event
.m_item
= item
;
1359 event
.m_itemOld
= m_current
;
1360 event
.SetEventObject( this );
1361 // TODO : Here we don't send any selection mode yet !
1363 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1367 if (unselect_others
)
1369 if (is_single
) Unselect(); // to speed up thing
1374 if (extended_select
)
1379 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1382 // don't change the mark (m_current)
1383 SelectItemRange(m_current
, item
);
1387 bool select
=TRUE
; // the default
1389 // Check if we need to toggle hilight (ctrl mode)
1390 if (!unselect_others
)
1391 select
=!item
->IsSelected();
1393 m_current
= m_key_current
= item
;
1394 m_current
->SetHilight(select
);
1395 RefreshLine( m_current
);
1398 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1399 GetEventHandler()->ProcessEvent( event
);
1402 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1403 wxArrayTreeItemIds
&array
) const
1405 if ( item
->IsSelected() )
1406 array
.Add(wxTreeItemId(item
));
1408 if ( item
->HasChildren() )
1410 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1411 size_t count
= children
.GetCount();
1412 for ( size_t n
= 0; n
< count
; ++n
)
1413 FillArray(children
[n
], array
);
1417 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1420 wxTreeItemId idRoot
= GetRootItem();
1421 if ( idRoot
.IsOk() )
1423 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1425 //else: the tree is empty, so no selections
1427 return array
.Count();
1430 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1432 if (!item
.IsOk()) return;
1434 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1436 // first expand all parent branches
1437 wxGenericTreeItem
*parent
= gitem
->GetParent();
1441 parent
= parent
->GetParent();
1444 //if (parent) CalculatePositions();
1449 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1451 if (!item
.IsOk()) return;
1453 // We have to call this here because the label in
1454 // question might just have been added and no screen
1455 // update taken place.
1456 if (m_dirty
) wxYield();
1458 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1460 // now scroll to the item
1461 int item_y
= gitem
->GetY();
1465 ViewStart( &start_x
, &start_y
);
1466 start_y
*= PIXELS_PER_UNIT
;
1470 GetClientSize( &client_w
, &client_h
);
1472 if (item_y
< start_y
+3)
1477 m_anchor
->GetSize( x
, y
, this );
1478 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1479 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1480 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1481 // Item should appear at top
1482 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1484 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1489 m_anchor
->GetSize( x
, y
, this );
1490 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1491 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1492 item_y
+= PIXELS_PER_UNIT
+2;
1493 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1494 // Item should appear at bottom
1495 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
);
1499 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1500 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1502 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1503 wxGenericTreeItem
**item2
)
1505 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1507 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1510 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1511 const wxTreeItemId
& item2
)
1513 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1516 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1518 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1520 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1522 wxCHECK_RET( !s_treeBeingSorted
,
1523 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1525 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1526 if ( children
.Count() > 1 )
1528 s_treeBeingSorted
= this;
1529 children
.Sort(tree_ctrl_compare_func
);
1530 s_treeBeingSorted
= NULL
;
1534 //else: don't make the tree dirty as nothing changed
1537 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1539 return m_imageListNormal
;
1542 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1544 return m_imageListState
;
1547 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1549 m_imageListNormal
= imageList
;
1551 if ( !m_imageListNormal
)
1554 // Calculate a m_lineHeight value from the image sizes.
1555 // May be toggle off. Then wxGenericTreeCtrl will spread when
1556 // necessary (which might look ugly).
1557 wxClientDC
dc(this);
1558 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1559 int width
= 0, height
= 0,
1560 n
= m_imageListNormal
->GetImageCount();
1562 for (int i
= 0; i
< n
; i
++)
1564 m_imageListNormal
->GetSize(i
, width
, height
);
1565 if (height
> m_lineHeight
) m_lineHeight
= height
;
1568 if (m_lineHeight
< 40)
1569 m_lineHeight
+= 2; // at least 2 pixels
1571 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1574 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1576 m_imageListState
= imageList
;
1579 // -----------------------------------------------------------------------------
1581 // -----------------------------------------------------------------------------
1583 void wxGenericTreeCtrl::AdjustMyScrollbars()
1589 m_anchor
->GetSize( x
, y
, this );
1590 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1591 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1592 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1593 int y_pos
= GetScrollPos( wxVERTICAL
);
1594 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1598 SetScrollbars( 0, 0, 0, 0 );
1602 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1604 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1605 return item
->GetHeight();
1607 return m_lineHeight
;
1610 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1612 wxTreeItemAttr
*attr
= item
->GetAttributes();
1613 if ( attr
&& attr
->HasFont() )
1614 dc
.SetFont(attr
->GetFont());
1615 else if (item
->IsBold())
1616 dc
.SetFont(m_boldFont
);
1620 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1624 int image
= item
->GetCurrentImage();
1625 if ( image
!= NO_IMAGE
)
1627 if ( m_imageListNormal
)
1629 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1638 int total_h
= GetLineHeight(item
);
1640 if (item
->IsSelected())
1641 dc
.SetBrush(*m_hilightBrush
);
1645 if ( attr
&& attr
->HasBackgroundColour() )
1646 colBg
= attr
->GetBackgroundColour();
1648 colBg
= m_backgroundColour
;
1649 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1652 dc
.DrawRectangle( item
->GetX()-2, item
->GetY(), item
->GetWidth()+2, total_h
);
1654 if ( image
!= NO_IMAGE
)
1656 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1657 m_imageListNormal
->Draw( image
, dc
,
1659 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1660 wxIMAGELIST_DRAW_TRANSPARENT
);
1661 dc
.DestroyClippingRegion();
1664 dc
.SetBackgroundMode(wxTRANSPARENT
);
1665 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1666 dc
.DrawText( item
->GetText(),
1667 (wxCoord
)(image_w
+ item
->GetX()),
1668 (wxCoord
)(item
->GetY() + extraH
));
1670 // restore normal font
1671 dc
.SetFont( m_normalFont
);
1674 // Now y stands for the top of the item, whereas it used to stand for middle !
1675 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1677 int horizX
= level
*m_indent
;
1679 item
->SetX( horizX
+m_indent
+m_spacing
);
1683 y
+=GetLineHeight(item
)/2;
1685 item
->SetCross( horizX
+m_indent
, y
);
1687 int exposed_x
= dc
.LogicalToDeviceX( 0 );
1688 int exposed_y
= dc
.LogicalToDeviceY( item
->GetY() );
1690 if (IsExposed( exposed_x
, exposed_y
, 10000, GetLineHeight(item
) )) // 10000 = very much
1692 int startX
= horizX
;
1693 int endX
= horizX
+ (m_indent
-5);
1695 // if (!item->HasChildren()) endX += (m_indent+5);
1696 if (!item
->HasChildren()) endX
+= 20;
1698 dc
.DrawLine( startX
, y
, endX
, y
);
1700 if (item
->HasPlus())
1702 dc
.DrawLine( horizX
+(m_indent
+5), y
, horizX
+(m_indent
+15), y
);
1703 dc
.SetPen( *wxGREY_PEN
);
1704 dc
.SetBrush( *wxWHITE_BRUSH
);
1705 dc
.DrawRectangle( horizX
+(m_indent
-5), y
-4, 11, 9 );
1707 dc
.SetPen( *wxBLACK_PEN
);
1708 dc
.DrawLine( horizX
+(m_indent
-2), y
, horizX
+(m_indent
+3), y
);
1709 if (!item
->IsExpanded())
1710 dc
.DrawLine( horizX
+m_indent
, y
-2, horizX
+m_indent
, y
+3 );
1712 dc
.SetPen( m_dottedPen
);
1715 wxPen
*pen
= wxTRANSPARENT_PEN
;
1718 if ( item
->IsSelected() )
1720 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1728 wxTreeItemAttr
*attr
= item
->GetAttributes();
1729 if ( attr
&& attr
->HasTextColour() )
1730 colText
= attr
->GetTextColour();
1736 dc
.SetTextForeground(colText
);
1740 PaintItem(item
, dc
);
1742 // restore DC objects
1743 dc
.SetBrush( *wxWHITE_BRUSH
);
1744 dc
.SetPen( m_dottedPen
);
1745 dc
.SetTextForeground( *wxBLACK
);
1748 y
= oldY
+GetLineHeight(item
);
1750 if (item
->IsExpanded())
1752 oldY
+=GetLineHeight(item
)/2;
1755 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1756 size_t n
, count
= children
.Count();
1757 for ( n
= 0; n
< count
; ++n
)
1760 PaintLevel( children
[n
], dc
, level
+1, y
);
1763 // it may happen that the item is expanded but has no items (when you
1764 // delete all its children for example) - don't draw the vertical line
1768 semiOldY
+=GetLineHeight(children
[--n
])/2;
1769 dc
.DrawLine( horizX
+m_indent
, oldY
+5, horizX
+m_indent
, semiOldY
);
1774 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
1778 if ( item
->HasPlus() )
1780 // it's a folder, indicate it by a border
1785 // draw a line under the drop target because the item will be
1787 DrawLine(item
, TRUE
/* below */);
1790 SetCursor(wxCURSOR_BULLSEYE
);
1795 SetCursor(wxCURSOR_NO_ENTRY
);
1799 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
1801 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1803 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1805 wxClientDC
dc(this);
1807 dc
.SetLogicalFunction(wxINVERT
);
1808 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1810 int w
= i
->GetWidth() + 2;
1811 int h
= GetLineHeight(i
) + 2;
1813 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
1816 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
1818 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1820 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1822 wxClientDC
dc(this);
1824 dc
.SetLogicalFunction(wxINVERT
);
1830 y
+= GetLineHeight(i
) - 1;
1833 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
1836 // -----------------------------------------------------------------------------
1837 // wxWindows callbacks
1838 // -----------------------------------------------------------------------------
1840 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1848 dc
.SetFont( m_normalFont
);
1849 dc
.SetPen( m_dottedPen
);
1851 // this is now done dynamically
1852 //if(GetImageList() == NULL)
1853 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1856 PaintLevel( m_anchor
, dc
, 0, y
);
1859 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1863 if (m_current
) RefreshLine( m_current
);
1866 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1870 if (m_current
) RefreshLine( m_current
);
1873 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
1875 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
1876 te
.m_code
= (int)event
.KeyCode();
1877 te
.SetEventObject( this );
1878 GetEventHandler()->ProcessEvent( te
);
1880 if ( (m_current
== 0) || (m_key_current
== 0) )
1886 // how should the selection work for this event?
1887 bool is_multiple
, extended_select
, unselect_others
;
1888 EventFlagsToSelType(GetWindowStyleFlag(),
1890 event
.ControlDown(),
1891 is_multiple
, extended_select
, unselect_others
);
1895 // * : Expand all/Collapse all
1896 // ' ' | return : activate
1897 // up : go up (not last children!)
1899 // left : go to parent
1900 // right : open if parent and go next
1901 // home : go to root
1902 // end : go to last item without opening parents
1903 switch (event
.KeyCode())
1907 if (m_current
->HasPlus() && !IsExpanded(m_current
))
1915 if ( !IsExpanded(m_current
) )
1918 ExpandAll(m_current
);
1921 //else: fall through to Collapse() it
1925 if (IsExpanded(m_current
))
1927 Collapse(m_current
);
1934 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
1935 event
.m_item
= m_current
;
1937 event
.SetEventObject( this );
1938 GetEventHandler()->ProcessEvent( event
);
1942 // up goes to the previous sibling or to the last of its children if
1946 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
1949 prev
= GetParent( m_key_current
);
1953 wxTreeItemId current
= m_key_current
;
1954 if (current
== GetFirstChild( prev
, cockie
))
1956 // otherwise we return to where we came from
1957 SelectItem( prev
, unselect_others
, extended_select
);
1958 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
1959 EnsureVisible( prev
);
1966 while ( IsExpanded(prev
) && HasChildren(prev
) )
1968 wxTreeItemId child
= GetLastChild(prev
);
1975 SelectItem( prev
, unselect_others
, extended_select
);
1976 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
1977 EnsureVisible( prev
);
1982 // left arrow goes to the parent
1985 wxTreeItemId prev
= GetParent( m_current
);
1988 EnsureVisible( prev
);
1989 SelectItem( prev
, unselect_others
, extended_select
);
1995 // this works the same as the down arrow except that we also expand the
1996 // item if it wasn't expanded yet
2002 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2005 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2006 SelectItem( child
, unselect_others
, extended_select
);
2007 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2008 EnsureVisible( child
);
2012 wxTreeItemId next
= GetNextSibling( m_key_current
);
2015 wxTreeItemId current
= m_key_current
;
2016 while (current
&& !next
)
2018 current
= GetParent( current
);
2019 if (current
) next
= GetNextSibling( current
);
2024 SelectItem( next
, unselect_others
, extended_select
);
2025 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2026 EnsureVisible( next
);
2032 // <End> selects the last visible tree item
2035 wxTreeItemId last
= GetRootItem();
2037 while ( last
.IsOk() && IsExpanded(last
) )
2039 wxTreeItemId lastChild
= GetLastChild(last
);
2041 // it may happen if the item was expanded but then all of
2042 // its children have been deleted - so IsExpanded() returned
2043 // TRUE, but GetLastChild() returned invalid item
2052 EnsureVisible( last
);
2053 SelectItem( last
, unselect_others
, extended_select
);
2058 // <Home> selects the root item
2061 wxTreeItemId prev
= GetRootItem();
2064 EnsureVisible( prev
);
2065 SelectItem( prev
, unselect_others
, extended_select
);
2075 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2077 // We have to call this here because the label in
2078 // question might just have been added and no screen
2079 // update taken place.
2080 if (m_dirty
) wxYield();
2082 wxClientDC
dc(this);
2084 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2085 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2090 if (point
.x
<0) flags
|=wxTREE_HITTEST_TOLEFT
;
2091 if (point
.x
>w
) flags
|=wxTREE_HITTEST_TORIGHT
;
2092 if (point
.y
<0) flags
|=wxTREE_HITTEST_ABOVE
;
2093 if (point
.y
>h
) flags
|=wxTREE_HITTEST_BELOW
;
2095 return m_anchor
->HitTest( wxPoint(x
, y
), this, flags
);
2100 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2102 if (!item
.IsOk()) return;
2104 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2106 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2107 te
.m_item
= m_currentEdit
;
2108 te
.SetEventObject( this );
2109 GetEventHandler()->ProcessEvent( te
);
2111 if (!te
.IsAllowed()) return;
2113 // We have to call this here because the label in
2114 // question might just have been added and no screen
2115 // update taken place.
2116 if (m_dirty
) wxYield();
2118 wxString s
= m_currentEdit
->GetText();
2119 int x
= m_currentEdit
->GetX();
2120 int y
= m_currentEdit
->GetY();
2121 int w
= m_currentEdit
->GetWidth();
2122 int h
= m_currentEdit
->GetHeight();
2127 int image
= m_currentEdit
->GetCurrentImage();
2128 if ( image
!= NO_IMAGE
)
2130 if ( m_imageListNormal
)
2132 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2137 wxFAIL_MSG(_T("you must create an image list to use images!"));
2141 w
-= image_w
+ 4; // I don't know why +4 is needed
2143 wxClientDC
dc(this);
2145 x
= dc
.LogicalToDeviceX( x
);
2146 y
= dc
.LogicalToDeviceY( y
);
2148 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(
2149 this, -1, &m_renameAccept
, &m_renameRes
, this, s
, wxPoint(x
-4,y
-4), wxSize(w
+11,h
+8) );
2153 void wxGenericTreeCtrl::OnRenameTimer()
2158 void wxGenericTreeCtrl::OnRenameAccept()
2160 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2161 le
.m_item
= m_currentEdit
;
2162 le
.SetEventObject( this );
2163 le
.m_label
= m_renameRes
;
2164 GetEventHandler()->ProcessEvent( le
);
2166 if (!le
.IsAllowed()) return;
2168 SetItemText( m_currentEdit
, m_renameRes
);
2171 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2173 if ( !m_anchor
) return;
2175 // we process left mouse up event (enables in-place edit), right down
2176 // (pass to the user code), left dbl click (activate item) and
2177 // dragging/moving events for items drag-and-drop
2178 if ( !(event
.LeftDown() ||
2180 event
.RightDown() ||
2181 event
.LeftDClick() ||
2183 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2190 wxClientDC
dc(this);
2192 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2193 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2196 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
), this, flags
);
2198 if ( event
.Dragging() && !m_isDragging
)
2200 if (m_dragCount
== 0)
2201 m_dragStart
= wxPoint(x
,y
);
2205 if (m_dragCount
!= 3)
2207 // wait until user drags a bit further...
2211 wxEventType command
= event
.RightIsDown()
2212 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2213 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2215 wxTreeEvent
nevent( command
, GetId() );
2216 nevent
.m_item
= m_current
;
2217 nevent
.SetEventObject(this);
2219 // by default the dragging is not supported, the user code must
2220 // explicitly allow the event for it to take place
2223 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2225 // we're going to drag this item
2226 m_isDragging
= TRUE
;
2228 // remember the old cursor because we will change it while
2230 m_oldCursor
= m_cursor
;
2232 // in a single selection control, hide the selection temporarily
2233 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2235 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2237 if ( m_oldSelection
)
2239 m_oldSelection
->SetHilight(FALSE
);
2240 RefreshLine(m_oldSelection
);
2247 else if ( event
.Moving() )
2249 if ( item
!= m_dropTarget
)
2251 // unhighlight the previous drop target
2252 DrawDropEffect(m_dropTarget
);
2254 m_dropTarget
= item
;
2256 // highlight the current drop target if any
2257 DrawDropEffect(m_dropTarget
);
2262 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2264 // erase the highlighting
2265 DrawDropEffect(m_dropTarget
);
2267 // generate the drag end event
2268 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2270 event
.m_item
= item
;
2271 event
.m_pointDrag
= wxPoint(x
, y
);
2272 event
.SetEventObject(this);
2274 (void)GetEventHandler()->ProcessEvent(event
);
2276 m_isDragging
= FALSE
;
2277 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2279 if ( m_oldSelection
)
2281 m_oldSelection
->SetHilight(TRUE
);
2282 RefreshLine(m_oldSelection
);
2283 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2288 SetCursor(m_oldCursor
);
2294 // here we process only the messages which happen on tree items
2298 if (item
== NULL
) return; /* we hit the blank area */
2300 if ( event
.RightDown() )
2302 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2303 nevent
.m_item
= item
;
2305 nevent
.SetEventObject(this);
2306 GetEventHandler()->ProcessEvent(nevent
);
2308 else if ( event
.LeftUp() && m_lastOnSame
)
2310 if ( (item
== m_current
) &&
2311 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2312 HasFlag(wxTR_EDIT_LABELS
) )
2314 m_renameTimer
->Start( 100, TRUE
);
2317 m_lastOnSame
= FALSE
;
2321 if ( event
.LeftDown() )
2323 m_lastOnSame
= item
== m_current
;
2326 // how should the selection work for this event?
2327 bool is_multiple
, extended_select
, unselect_others
;
2328 EventFlagsToSelType(GetWindowStyleFlag(),
2330 event
.ControlDown(),
2331 is_multiple
, extended_select
, unselect_others
);
2333 if ( (flags
& wxTREE_HITTEST_ONITEMBUTTON
) && event
.LeftDown() )
2340 SelectItem(item
, unselect_others
, extended_select
);
2342 if ( event
.LeftDClick() )
2344 // double clicking should not start editing the item label
2345 m_renameTimer
->Stop();
2346 m_lastOnSame
= FALSE
;
2348 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2349 nevent
.m_item
= item
;
2351 nevent
.SetEventObject( this );
2352 GetEventHandler()->ProcessEvent( nevent
);
2358 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2360 /* after all changes have been done to the tree control,
2361 * we actually redraw the tree when everything is over */
2368 CalculatePositions();
2370 AdjustMyScrollbars();
2373 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2379 dc
.SetFont(m_boldFont
);
2381 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2384 // restore normal font
2385 dc
.SetFont( m_normalFont
);
2389 int image
= item
->GetCurrentImage();
2390 if ( image
!= NO_IMAGE
)
2392 if ( m_imageListNormal
)
2394 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2399 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2402 total_h
+= 2; // at least 2 pixels
2404 total_h
+= total_h
/10; // otherwise 10% extra spacing
2406 item
->SetHeight(total_h
);
2407 if (total_h
>m_lineHeight
)
2408 m_lineHeight
=total_h
;
2410 item
->SetWidth(image_w
+text_w
+2);
2413 // -----------------------------------------------------------------------------
2414 // for developper : y is now the top of the level
2415 // not the middle of it !
2416 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2418 int horizX
= level
*m_indent
;
2420 CalculateSize( item
, dc
);
2423 item
->SetX( horizX
+m_indent
+m_spacing
);
2425 y
+=GetLineHeight(item
);
2427 if ( !item
->IsExpanded() )
2429 // we dont need to calculate collapsed branches
2433 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2434 size_t n
, count
= children
.Count();
2435 for (n
= 0; n
< count
; ++n
)
2436 CalculateLevel( children
[n
], dc
, level
+1, y
); // recurse
2439 void wxGenericTreeCtrl::CalculatePositions()
2441 if ( !m_anchor
) return;
2443 wxClientDC
dc(this);
2446 dc
.SetFont( m_normalFont
);
2448 dc
.SetPen( m_dottedPen
);
2449 //if(GetImageList() == NULL)
2450 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2453 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2456 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2458 if (m_dirty
) return;
2460 wxClientDC
dc(this);
2465 GetClientSize( &cw
, &ch
);
2468 rect
.x
= dc
.LogicalToDeviceX( 0 );
2470 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2473 Refresh( TRUE
, &rect
);
2475 AdjustMyScrollbars();
2478 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2480 if (m_dirty
) return;
2482 wxClientDC
dc(this);
2487 GetClientSize( &cw
, &ch
);
2490 rect
.x
= dc
.LogicalToDeviceX( 0 );
2491 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2493 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2495 Refresh( TRUE
, &rect
);