1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/treebase.h"
34 #include "wx/generic/treectlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
48 //WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int NO_IMAGE
= -1;
56 #define PIXELS_PER_UNIT 10
58 // -----------------------------------------------------------------------------
60 // -----------------------------------------------------------------------------
62 // timer used for enabling in-place edit
63 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
66 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
71 wxGenericTreeCtrl
*m_owner
;
74 // control used for in-place edit
75 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
78 wxTreeTextCtrl( wxWindow
*parent
,
82 wxGenericTreeCtrl
*owner
,
83 const wxString
&value
= wxEmptyString
,
84 const wxPoint
&pos
= wxDefaultPosition
,
85 const wxSize
&size
= wxDefaultSize
,
86 int style
= wxSIMPLE_BORDER
,
87 const wxValidator
& validator
= wxDefaultValidator
,
88 const wxString
&name
= wxTextCtrlNameStr
);
90 void OnChar( wxKeyEvent
&event
);
91 void OnKeyUp( wxKeyEvent
&event
);
92 void OnKillFocus( wxFocusEvent
&event
);
97 wxGenericTreeCtrl
*m_owner
;
98 wxString m_startValue
;
100 DECLARE_EVENT_TABLE()
104 class WXDLLEXPORT wxGenericTreeItem
108 wxGenericTreeItem() { m_data
= NULL
; }
109 wxGenericTreeItem( wxGenericTreeItem
*parent
,
110 const wxString
& text
,
114 wxTreeItemData
*data
);
116 ~wxGenericTreeItem();
119 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
121 const wxString
& GetText() const { return m_text
; }
122 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
123 { return m_images
[which
]; }
124 wxTreeItemData
*GetData() const { return m_data
; }
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
130 void SetText( const wxString
&text
);
131 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
136 void SetBold(bool bold
) { m_isBold
= bold
; }
138 int GetX() const { return m_x
; }
139 int GetY() const { return m_y
; }
141 void SetX(int x
) { m_x
= x
; }
142 void SetY(int y
) { m_y
= y
; }
144 int GetHeight() const { return m_height
; }
145 int GetWidth() const { return m_width
; }
147 void SetHeight(int h
) { m_height
= h
; }
148 void SetWidth(int w
) { m_width
= w
; }
150 wxGenericTreeItem
*GetParent() const { return m_parent
; }
153 // deletes all children notifying the treectrl about it if !NULL
155 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
157 // get count of all children (and grand children if 'recursively')
158 size_t GetChildrenCount(bool recursively
= TRUE
) const;
160 void Insert(wxGenericTreeItem
*child
, size_t index
)
161 { m_children
.Insert(child
, index
); }
163 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
165 // return the item at given position (or NULL if no item), onButton is
166 // TRUE if the point belongs to the item's button, otherwise it lies
167 // on the button's label
168 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
169 const wxGenericTreeCtrl
*,
173 void Expand() { m_isCollapsed
= FALSE
; }
174 void Collapse() { m_isCollapsed
= TRUE
; }
176 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
179 bool HasChildren() const { return !m_children
.IsEmpty(); }
180 bool IsSelected() const { return m_hasHilight
!= 0; }
181 bool IsExpanded() const { return !m_isCollapsed
; }
182 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
183 bool IsBold() const { return m_isBold
!= 0; }
186 // get them - may be NULL
187 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
188 // get them ensuring that the pointer is not NULL
189 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
199 void SetAttributes(wxTreeItemAttr
*attr
)
201 if ( m_ownsAttr
) delete m_attr
;
205 // set them and delete when done
206 void AssignAttributes(wxTreeItemAttr
*attr
)
213 // since there can be very many of these, we save size by chosing
214 // the smallest representation for the elements and by ordering
215 // the members to avoid padding.
216 wxString m_text
; // label to be rendered for item
218 wxTreeItemData
*m_data
; // user-provided data
220 wxArrayGenericTreeItems m_children
; // list of children
221 wxGenericTreeItem
*m_parent
; // parent of this item
223 wxTreeItemAttr
*m_attr
; // attributes???
225 // tree ctrl images for the normal, selected, expanded and
226 // expanded+selected states
227 short m_images
[wxTreeItemIcon_Max
];
229 wxCoord m_x
; // (virtual) offset from top
230 short m_y
; // (virtual) offset from left
231 short m_width
; // width of this item
232 unsigned char m_height
; // height of this item
234 // use bitfields to save size
235 int m_isCollapsed
:1;
236 int m_hasHilight
:1; // same as focused
237 int m_hasPlus
:1; // used for item which doesn't have
238 // children but has a [+] button
239 int m_isBold
:1; // render the label in bold font
240 int m_ownsAttr
:1; // delete attribute when done
243 // =============================================================================
245 // =============================================================================
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // translate the key or mouse event flags to the type of selection we're
253 static void EventFlagsToSelType(long style
,
257 bool &extended_select
,
258 bool &unselect_others
)
260 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
261 extended_select
= shiftDown
&& is_multiple
;
262 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
265 // -----------------------------------------------------------------------------
266 // wxTreeRenameTimer (internal)
267 // -----------------------------------------------------------------------------
269 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
274 void wxTreeRenameTimer::Notify()
276 m_owner
->OnRenameTimer();
279 //-----------------------------------------------------------------------------
280 // wxTreeTextCtrl (internal)
281 //-----------------------------------------------------------------------------
283 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
284 EVT_CHAR (wxTreeTextCtrl::OnChar
)
285 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
286 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
289 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
293 wxGenericTreeCtrl
*owner
,
294 const wxString
&value
,
298 const wxValidator
& validator
,
299 const wxString
&name
)
300 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
306 (*m_res
) = wxEmptyString
;
307 m_startValue
= value
;
310 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
312 // TODO focus doesn't return to the wxTextCtrl when this closes...
313 if (event
.m_keyCode
== WXK_RETURN
)
316 (*m_res
) = GetValue();
318 if ((*m_accept
) && ((*m_res
) != m_startValue
))
319 m_owner
->OnRenameAccept();
321 if (!wxPendingDelete
.Member(this))
322 wxPendingDelete
.Append(this);
326 if (event
.m_keyCode
== WXK_ESCAPE
)
331 if (!wxPendingDelete
.Member(this))
332 wxPendingDelete
.Append(this);
339 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
341 // auto-grow the textctrl:
342 wxSize parentSize
= m_owner
->GetSize();
343 wxPoint myPos
= GetPosition();
344 wxSize mySize
= GetSize();
346 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
347 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
348 if (mySize
.x
> sx
) sx
= mySize
.x
;
354 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
356 if (!wxPendingDelete
.Member(this))
357 wxPendingDelete
.Append(this);
359 if ((*m_accept
) && ((*m_res
) != m_startValue
))
360 m_owner
->OnRenameAccept();
364 // -----------------------------------------------------------------------------
366 // -----------------------------------------------------------------------------
368 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
370 wxTreeEvent::wxTreeEvent( wxEventType commandType
, int id
)
371 : wxNotifyEvent( commandType
, id
)
374 m_itemOld
= (wxGenericTreeItem
*)NULL
;
378 // -----------------------------------------------------------------------------
380 // -----------------------------------------------------------------------------
382 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
383 const wxString
& text
,
385 int image
, int selImage
,
386 wxTreeItemData
*data
)
389 m_images
[wxTreeItemIcon_Normal
] = image
;
390 m_images
[wxTreeItemIcon_Selected
] = selImage
;
391 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
392 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
397 m_isCollapsed
= TRUE
;
398 m_hasHilight
= FALSE
;
404 m_attr
= (wxTreeItemAttr
*)NULL
;
407 // We don't know the height here yet.
412 wxGenericTreeItem::~wxGenericTreeItem()
416 if (m_ownsAttr
) delete m_attr
;
418 wxASSERT_MSG( m_children
.IsEmpty(),
419 wxT("please call DeleteChildren() before deleting the item") );
422 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
424 size_t count
= m_children
.Count();
425 for ( size_t n
= 0; n
< count
; n
++ )
427 wxGenericTreeItem
*child
= m_children
[n
];
429 tree
->SendDeleteEvent(child
);
431 child
->DeleteChildren(tree
);
438 void wxGenericTreeItem::SetText( const wxString
&text
)
443 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
445 size_t count
= m_children
.Count();
449 size_t total
= count
;
450 for (size_t n
= 0; n
< count
; ++n
)
452 total
+= m_children
[n
]->GetChildrenCount();
458 void wxGenericTreeItem::GetSize( int &x
, int &y
,
459 const wxGenericTreeCtrl
*theButton
)
461 int bottomY
=m_y
+theButton
->GetLineHeight(this);
462 if ( y
< bottomY
) y
= bottomY
;
463 int width
= m_x
+ m_width
;
464 if ( x
< width
) x
= width
;
468 size_t count
= m_children
.Count();
469 for ( size_t n
= 0; n
< count
; ++n
)
471 m_children
[n
]->GetSize( x
, y
, theButton
);
476 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
477 const wxGenericTreeCtrl
*theCtrl
,
481 // for a hidden root node, don't evaluate it, but do evaluate children
482 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
485 int h
= theCtrl
->GetLineHeight(this);
486 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
488 int y_mid
= m_y
+ h
/2;
489 if (point
.y
< y_mid
)
490 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
492 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
494 // 5 is the size of the plus sign
495 int xCross
= m_x
- theCtrl
->GetSpacing();
496 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
497 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
498 HasPlus() && theCtrl
->HasButtons() )
500 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
504 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
509 // assuming every image (normal and selected) has the same size!
510 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
511 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
514 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
515 flags
|= wxTREE_HITTEST_ONITEMICON
;
517 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
523 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
524 if (point
.x
> m_x
+m_width
)
525 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
530 // if children are expanded, fall through to evaluate them
531 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
535 size_t count
= m_children
.Count();
536 for ( size_t n
= 0; n
< count
; n
++ )
538 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
546 return (wxGenericTreeItem
*) NULL
;
549 int wxGenericTreeItem::GetCurrentImage() const
551 int image
= NO_IMAGE
;
556 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
559 if ( image
== NO_IMAGE
)
561 // we usually fall back to the normal item, but try just the
562 // expanded one (and not selected) first in this case
563 image
= GetImage(wxTreeItemIcon_Expanded
);
569 image
= GetImage(wxTreeItemIcon_Selected
);
572 // maybe it doesn't have the specific image we want,
573 // try the default one instead
574 if ( image
== NO_IMAGE
) image
= GetImage();
579 // -----------------------------------------------------------------------------
580 // wxGenericTreeCtrl implementation
581 // -----------------------------------------------------------------------------
583 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
585 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
586 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
587 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
588 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
589 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
590 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
591 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
594 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
596 * wxTreeCtrl has to be a real class or we have problems with
597 * the run-time information.
600 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
603 // -----------------------------------------------------------------------------
604 // construction/destruction
605 // -----------------------------------------------------------------------------
607 void wxGenericTreeCtrl::Init()
609 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
617 m_hilightBrush
= new wxBrush
619 wxSystemSettings::GetSystemColour
621 wxSYS_COLOUR_HIGHLIGHT
626 m_hilightUnfocusedBrush
= new wxBrush
628 wxSystemSettings::GetSystemColour
630 wxSYS_COLOUR_BTNSHADOW
635 m_imageListNormal
= m_imageListButtons
=
636 m_imageListState
= (wxImageList
*) NULL
;
637 m_ownsImageListNormal
= m_ownsImageListButtons
=
638 m_ownsImageListState
= FALSE
;
641 m_isDragging
= FALSE
;
642 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
644 m_renameTimer
= new wxTreeRenameTimer( this );
645 m_lastOnSame
= FALSE
;
647 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
648 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
649 m_normalFont
.GetFamily(),
650 m_normalFont
.GetStyle(),
652 m_normalFont
.GetUnderlined());
655 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
660 const wxValidator
&validator
,
661 const wxString
& name
)
663 wxScrolledWindow::Create( parent
, id
, pos
, size
,
664 style
|wxHSCROLL
|wxVSCROLL
, name
);
666 // If the tree display has no buttons, but does have
667 // connecting lines, we can use a narrower layout.
668 // It may not be a good idea to force this...
669 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
676 SetValidator( validator
);
679 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
681 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
682 m_dottedPen
= wxPen( "grey", 0, 0 );
687 wxGenericTreeCtrl::~wxGenericTreeCtrl()
689 delete m_hilightBrush
;
690 delete m_hilightUnfocusedBrush
;
694 delete m_renameTimer
;
695 if (m_ownsImageListNormal
) delete m_imageListNormal
;
696 if (m_ownsImageListState
) delete m_imageListState
;
697 if (m_ownsImageListButtons
) delete m_imageListButtons
;
700 // -----------------------------------------------------------------------------
702 // -----------------------------------------------------------------------------
704 size_t wxGenericTreeCtrl::GetCount() const
706 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
709 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
715 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
721 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
723 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
725 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
728 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
730 // right now, just sets the styles. Eventually, we may
731 // want to update the inherited styles, but right now
732 // none of the parents has updatable styles
733 m_windowStyle
= styles
;
737 // -----------------------------------------------------------------------------
738 // functions to work with tree items
739 // -----------------------------------------------------------------------------
741 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
743 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
745 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
748 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
749 wxTreeItemIcon which
) const
751 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
753 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
756 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
758 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
760 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
763 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
765 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
768 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
769 pItem
->SetText(text
);
770 CalculateSize(pItem
, dc
);
774 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
776 wxTreeItemIcon which
)
778 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
780 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
781 pItem
->SetImage(image
, which
);
784 CalculateSize(pItem
, dc
);
788 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
790 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
792 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
795 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
797 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
799 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
800 pItem
->SetHasPlus(has
);
804 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
806 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
808 // avoid redrawing the tree if no real change
809 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
810 if ( pItem
->IsBold() != bold
)
812 pItem
->SetBold(bold
);
817 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
820 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
822 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
823 pItem
->Attr().SetTextColour(col
);
827 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
830 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
832 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
833 pItem
->Attr().SetBackgroundColour(col
);
837 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
839 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
841 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
842 pItem
->Attr().SetFont(font
);
846 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
848 wxScrolledWindow::SetFont(font
);
850 m_normalFont
= font
;
851 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
852 m_normalFont
.GetFamily(),
853 m_normalFont
.GetStyle(),
855 m_normalFont
.GetUnderlined());
861 // -----------------------------------------------------------------------------
862 // item status inquiries
863 // -----------------------------------------------------------------------------
865 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
867 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
869 // An item is only visible if it's not a descendant of a collapsed item
870 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
871 wxGenericTreeItem
* parent
= pItem
->GetParent();
874 if (!parent
->IsExpanded())
876 parent
= parent
->GetParent();
880 GetViewStart(& startX
, & startY
);
882 wxSize clientSize
= GetClientSize();
885 if (!GetBoundingRect(item
, rect
))
887 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
889 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
891 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
897 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
899 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
901 return !((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren().IsEmpty();
904 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
906 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
908 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
911 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
913 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
915 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
918 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
920 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
922 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
925 // -----------------------------------------------------------------------------
927 // -----------------------------------------------------------------------------
929 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
931 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
933 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
936 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
938 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
941 return GetNextChild(item
, cookie
);
944 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
946 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
948 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
949 if ( (size_t)cookie
< children
.Count() )
951 return children
.Item((size_t)cookie
++);
955 // there are no more of them
956 return wxTreeItemId();
960 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
962 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
964 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
965 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
968 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
970 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
972 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
973 wxGenericTreeItem
*parent
= i
->GetParent();
974 if ( parent
== NULL
)
976 // root item doesn't have any siblings
977 return wxTreeItemId();
980 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
981 int index
= siblings
.Index(i
);
982 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
984 size_t n
= (size_t)(index
+ 1);
985 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
988 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
990 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
992 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
993 wxGenericTreeItem
*parent
= i
->GetParent();
994 if ( parent
== NULL
)
996 // root item doesn't have any siblings
997 return wxTreeItemId();
1000 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1001 int index
= siblings
.Index(i
);
1002 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1004 return index
== 0 ? wxTreeItemId()
1005 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1008 // Only for internal use right now, but should probably be public
1009 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1011 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1013 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1015 // First see if there are any children.
1016 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1017 if (children
.GetCount() > 0)
1019 return children
.Item(0);
1023 // Try a sibling of this or ancestor instead
1024 wxTreeItemId p
= item
;
1025 wxTreeItemId toFind
;
1028 toFind
= GetNextSibling(p
);
1030 } while (p
.IsOk() && !toFind
.IsOk());
1035 wxTreeItemId
wxGenericTreeCtrl::GetPrev(const wxTreeItemId
& item
) const
1037 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1039 wxFAIL_MSG(wxT("not implemented"));
1041 return wxTreeItemId();
1044 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1046 wxTreeItemId id
= GetRootItem();
1055 } while (id
.IsOk());
1057 return wxTreeItemId();
1060 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1062 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1064 wxTreeItemId id
= item
;
1067 while (id
= GetNext(id
), id
.IsOk())
1073 return wxTreeItemId();
1076 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1078 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1080 wxFAIL_MSG(wxT("not implemented"));
1082 return wxTreeItemId();
1085 // -----------------------------------------------------------------------------
1087 // -----------------------------------------------------------------------------
1089 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1091 const wxString
& text
,
1092 int image
, int selImage
,
1093 wxTreeItemData
*data
)
1095 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1098 // should we give a warning here?
1099 return AddRoot(text
, image
, selImage
, data
);
1102 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1104 wxClientDC
dc(this);
1105 wxGenericTreeItem
*item
=
1106 new wxGenericTreeItem( parent
, text
, dc
, image
, selImage
, data
);
1110 data
->m_pItem
= (long) item
;
1113 parent
->Insert( item
, previous
);
1118 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1119 int image
, int selImage
,
1120 wxTreeItemData
*data
)
1122 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1124 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1126 wxClientDC
dc(this);
1127 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
, dc
,
1128 image
, selImage
, data
);
1129 if (HasFlag(wxTR_HIDE_ROOT
))
1131 // if root is hidden, make sure we can navigate
1133 m_anchor
->SetHasPlus();
1138 data
->m_pItem
= (long) m_anchor
;
1141 if (!HasFlag(wxTR_MULTIPLE
))
1143 m_current
= m_key_current
= m_anchor
;
1144 m_current
->SetHilight( TRUE
);
1150 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1151 const wxString
& text
,
1152 int image
, int selImage
,
1153 wxTreeItemData
*data
)
1155 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1158 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1159 const wxTreeItemId
& idPrevious
,
1160 const wxString
& text
,
1161 int image
, int selImage
,
1162 wxTreeItemData
*data
)
1164 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1167 // should we give a warning here?
1168 return AddRoot(text
, image
, selImage
, data
);
1171 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1172 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1173 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1175 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1178 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1180 const wxString
& text
,
1181 int image
, int selImage
,
1182 wxTreeItemData
*data
)
1184 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1187 // should we give a warning here?
1188 return AddRoot(text
, image
, selImage
, data
);
1191 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1194 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1195 const wxString
& text
,
1196 int image
, int selImage
,
1197 wxTreeItemData
*data
)
1199 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1202 // should we give a warning here?
1203 return AddRoot(text
, image
, selImage
, data
);
1206 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1207 image
, selImage
, data
);
1210 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1212 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1213 event
.m_item
= (long) item
;
1214 event
.SetEventObject( this );
1215 ProcessEvent( event
);
1218 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1220 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1222 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1223 item
->DeleteChildren(this);
1226 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1228 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1230 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1232 // don't stay with invalid m_key_current or we will crash in
1233 // the next call to OnChar()
1234 bool changeKeyCurrent
= FALSE
;
1235 wxGenericTreeItem
*itemKey
= m_key_current
;
1238 if ( itemKey
== item
)
1240 // m_key_current is a descendant of the item being deleted
1241 changeKeyCurrent
= TRUE
;
1244 itemKey
= itemKey
->GetParent();
1247 wxGenericTreeItem
*parent
= item
->GetParent();
1250 parent
->GetChildren().Remove( item
); // remove by value
1253 if ( changeKeyCurrent
)
1255 // may be NULL or not
1256 m_key_current
= parent
;
1259 item
->DeleteChildren(this);
1260 SendDeleteEvent(item
);
1264 void wxGenericTreeCtrl::DeleteAllItems()
1270 m_anchor
->DeleteChildren(this);
1277 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1279 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1281 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1283 if ( !item
->HasPlus() )
1286 if ( item
->IsExpanded() )
1289 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1290 event
.m_item
= (long) item
;
1291 event
.SetEventObject( this );
1293 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1295 // cancelled by program
1300 CalculatePositions();
1302 RefreshSubtree(item
);
1304 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1305 ProcessEvent( event
);
1308 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1311 if ( IsExpanded(item
) )
1314 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1315 while ( child
.IsOk() )
1319 child
= GetNextChild(item
, cookie
);
1324 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1326 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1328 if ( !item
->IsExpanded() )
1331 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1332 event
.m_item
= (long) item
;
1333 event
.SetEventObject( this );
1334 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1336 // cancelled by program
1342 #if 0 // TODO why should items be collapsed recursively?
1343 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1344 size_t count
= children
.Count();
1345 for ( size_t n
= 0; n
< count
; n
++ )
1347 Collapse(children
[n
]);
1351 CalculatePositions();
1353 RefreshSubtree(item
);
1355 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1356 ProcessEvent( event
);
1359 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1362 DeleteChildren(item
);
1365 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1367 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1369 if (item
->IsExpanded())
1375 void wxGenericTreeCtrl::Unselect()
1379 m_current
->SetHilight( FALSE
);
1380 RefreshLine( m_current
);
1384 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1386 if (item
->IsSelected())
1388 item
->SetHilight(FALSE
);
1392 if (item
->HasChildren())
1394 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1395 size_t count
= children
.Count();
1396 for ( size_t n
= 0; n
< count
; ++n
)
1398 UnselectAllChildren(children
[n
]);
1403 void wxGenericTreeCtrl::UnselectAll()
1405 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1408 // Recursive function !
1409 // To stop we must have crt_item<last_item
1411 // Tag all next children, when no more children,
1412 // Move to parent (not to tag)
1413 // Keep going... if we found last_item, we stop.
1414 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1416 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1418 if (parent
== NULL
) // This is root item
1419 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1421 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1422 int index
= children
.Index(crt_item
);
1423 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1425 size_t count
= children
.Count();
1426 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1428 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1431 return TagNextChildren(parent
, last_item
, select
);
1434 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1436 crt_item
->SetHilight(select
);
1437 RefreshLine(crt_item
);
1439 if (crt_item
==last_item
)
1442 if (crt_item
->HasChildren())
1444 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1445 size_t count
= children
.Count();
1446 for ( size_t n
= 0; n
< count
; ++n
)
1448 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1456 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1458 // item2 is not necessary after item1
1459 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1461 // choice first' and 'last' between item1 and item2
1462 if (item1
->GetY()<item2
->GetY())
1473 bool select
= m_current
->IsSelected();
1475 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1478 TagNextChildren(first
,last
,select
);
1481 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1482 bool unselect_others
,
1483 bool extended_select
)
1485 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1487 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1488 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1490 //wxCHECK_RET( ( (!unselect_others) && is_single),
1491 // wxT("this is a single selection tree") );
1493 // to keep going anyhow !!!
1496 if (item
->IsSelected())
1497 return; // nothing to do
1498 unselect_others
= TRUE
;
1499 extended_select
= FALSE
;
1501 else if ( unselect_others
&& item
->IsSelected() )
1503 // selection change if there is more than one item currently selected
1504 wxArrayTreeItemIds selected_items
;
1505 if ( GetSelections(selected_items
) == 1 )
1509 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1510 event
.m_item
= (long) item
;
1511 event
.m_itemOld
= (long) m_current
;
1512 event
.SetEventObject( this );
1513 // TODO : Here we don't send any selection mode yet !
1515 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1518 wxTreeItemId parent
= GetParent( itemId
);
1519 while (parent
.IsOk())
1521 if (!IsExpanded(parent
))
1524 parent
= GetParent( parent
);
1527 EnsureVisible( itemId
);
1530 if (unselect_others
)
1532 if (is_single
) Unselect(); // to speed up thing
1537 if (extended_select
)
1541 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1544 // don't change the mark (m_current)
1545 SelectItemRange(m_current
, item
);
1549 bool select
=TRUE
; // the default
1551 // Check if we need to toggle hilight (ctrl mode)
1552 if (!unselect_others
)
1553 select
=!item
->IsSelected();
1555 m_current
= m_key_current
= item
;
1556 m_current
->SetHilight(select
);
1557 RefreshLine( m_current
);
1560 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1561 GetEventHandler()->ProcessEvent( event
);
1564 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1565 wxArrayTreeItemIds
&array
) const
1567 if ( item
->IsSelected() )
1568 array
.Add(wxTreeItemId(item
));
1570 if ( item
->HasChildren() )
1572 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1573 size_t count
= children
.GetCount();
1574 for ( size_t n
= 0; n
< count
; ++n
)
1575 FillArray(children
[n
], array
);
1579 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1582 wxTreeItemId idRoot
= GetRootItem();
1583 if ( idRoot
.IsOk() )
1585 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1587 //else: the tree is empty, so no selections
1589 return array
.Count();
1592 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1594 if (!item
.IsOk()) return;
1596 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1598 // first expand all parent branches
1599 wxGenericTreeItem
*parent
= gitem
->GetParent();
1603 parent
= parent
->GetParent();
1606 //if (parent) CalculatePositions();
1611 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1613 if (!item
.IsOk()) return;
1615 // We have to call this here because the label in
1616 // question might just have been added and no screen
1617 // update taken place.
1618 if (m_dirty
) wxYieldIfNeeded();
1620 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1622 // now scroll to the item
1623 int item_y
= gitem
->GetY();
1627 GetViewStart( &start_x
, &start_y
);
1628 start_y
*= PIXELS_PER_UNIT
;
1632 GetClientSize( &client_w
, &client_h
);
1634 if (item_y
< start_y
+3)
1639 m_anchor
->GetSize( x
, y
, this );
1640 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1641 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1642 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1643 // Item should appear at top
1644 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1646 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1651 m_anchor
->GetSize( x
, y
, this );
1652 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1653 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1654 item_y
+= PIXELS_PER_UNIT
+2;
1655 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1656 // Item should appear at bottom
1657 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
);
1661 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1662 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1664 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1665 wxGenericTreeItem
**item2
)
1667 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1669 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1672 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1673 const wxTreeItemId
& item2
)
1675 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1678 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1680 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1682 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1684 wxCHECK_RET( !s_treeBeingSorted
,
1685 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1687 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1688 if ( children
.Count() > 1 )
1692 s_treeBeingSorted
= this;
1693 children
.Sort(tree_ctrl_compare_func
);
1694 s_treeBeingSorted
= NULL
;
1696 //else: don't make the tree dirty as nothing changed
1699 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1701 return m_imageListNormal
;
1704 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1706 return m_imageListButtons
;
1709 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1711 return m_imageListState
;
1714 void wxGenericTreeCtrl::CalculateLineHeight()
1716 wxClientDC
dc(this);
1717 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1719 if ( m_imageListNormal
)
1721 // Calculate a m_lineHeight value from the normal Image sizes.
1722 // May be toggle off. Then wxGenericTreeCtrl will spread when
1723 // necessary (which might look ugly).
1724 int n
= m_imageListNormal
->GetImageCount();
1725 for (int i
= 0; i
< n
; i
++)
1727 int width
= 0, height
= 0;
1728 m_imageListNormal
->GetSize(i
, width
, height
);
1729 if (height
> m_lineHeight
) m_lineHeight
= height
;
1733 if (m_imageListButtons
)
1735 // Calculate a m_lineHeight value from the Button image sizes.
1736 // May be toggle off. Then wxGenericTreeCtrl will spread when
1737 // necessary (which might look ugly).
1738 int n
= m_imageListButtons
->GetImageCount();
1739 for (int i
= 0; i
< n
; i
++)
1741 int width
= 0, height
= 0;
1742 m_imageListButtons
->GetSize(i
, width
, height
);
1743 if (height
> m_lineHeight
) m_lineHeight
= height
;
1747 if (m_lineHeight
< 30)
1748 m_lineHeight
+= 2; // at least 2 pixels
1750 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1753 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1755 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1756 m_imageListNormal
= imageList
;
1757 m_ownsImageListNormal
= FALSE
;
1759 CalculateLineHeight();
1762 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1764 if (m_ownsImageListState
) delete m_imageListState
;
1765 m_imageListState
= imageList
;
1766 m_ownsImageListState
= FALSE
;
1769 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1771 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1772 m_imageListButtons
= imageList
;
1773 m_ownsImageListButtons
= FALSE
;
1775 CalculateLineHeight();
1778 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1780 SetImageList(imageList
);
1781 m_ownsImageListNormal
= TRUE
;
1784 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1786 SetStateImageList(imageList
);
1787 m_ownsImageListState
= TRUE
;
1790 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1792 SetButtonsImageList(imageList
);
1793 m_ownsImageListButtons
= TRUE
;
1796 // -----------------------------------------------------------------------------
1798 // -----------------------------------------------------------------------------
1800 void wxGenericTreeCtrl::AdjustMyScrollbars()
1805 m_anchor
->GetSize( x
, y
, this );
1806 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1807 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1808 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1809 int y_pos
= GetScrollPos( wxVERTICAL
);
1810 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1814 SetScrollbars( 0, 0, 0, 0 );
1818 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1820 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1821 return item
->GetHeight();
1823 return m_lineHeight
;
1826 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1828 // TODO implement "state" icon on items
1830 wxTreeItemAttr
*attr
= item
->GetAttributes();
1831 if ( attr
&& attr
->HasFont() )
1832 dc
.SetFont(attr
->GetFont());
1833 else if (item
->IsBold())
1834 dc
.SetFont(m_boldFont
);
1836 long text_w
= 0, text_h
= 0;
1837 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1839 int image_h
= 0, image_w
= 0;
1840 int image
= item
->GetCurrentImage();
1841 if ( image
!= NO_IMAGE
)
1843 if ( m_imageListNormal
)
1845 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1854 int total_h
= GetLineHeight(item
);
1856 if ( item
->IsSelected() )
1858 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1863 if ( attr
&& attr
->HasBackgroundColour() )
1864 colBg
= attr
->GetBackgroundColour();
1866 colBg
= m_backgroundColour
;
1867 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1870 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1872 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1874 // If it's selected, and there's an image, then we should
1875 // take care to leave the area under the image painted in the
1876 // background colour.
1877 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1878 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1882 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1883 item
->GetWidth()+2, total_h
-offset
);
1886 if ( image
!= NO_IMAGE
)
1888 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1889 m_imageListNormal
->Draw( image
, dc
,
1891 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1892 wxIMAGELIST_DRAW_TRANSPARENT
);
1893 dc
.DestroyClippingRegion();
1896 dc
.SetBackgroundMode(wxTRANSPARENT
);
1897 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1898 dc
.DrawText( item
->GetText(),
1899 (wxCoord
)(image_w
+ item
->GetX()),
1900 (wxCoord
)(item
->GetY() + extraH
));
1902 // restore normal font
1903 dc
.SetFont( m_normalFont
);
1906 // Now y stands for the top of the item, whereas it used to stand for middle !
1907 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1909 int x
= level
*m_indent
;
1910 if (!HasFlag(wxTR_HIDE_ROOT
))
1914 else if (level
== 0)
1916 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1917 size_t n
, count
= children
.Count();
1918 for (n
= 0; n
< count
; ++n
)
1919 PaintLevel(children
[n
], dc
, 1, y
);
1923 item
->SetX(x
+m_spacing
);
1926 int h
= GetLineHeight(item
);
1928 int y_mid
= y_top
+ (h
>>1);
1931 int exposed_x
= dc
.LogicalToDeviceX(0);
1932 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1934 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1936 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1938 if (!HasFlag(wxTR_NO_LINES
))
1940 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
1941 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1942 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1945 if (m_imageListButtons
!= NULL
)
1947 // draw the image button here
1948 int image_h
= 0, image_w
= 0, image
= wxCLOSED_BUTTON
;
1949 if (item
->IsExpanded()) image
= wxOPEN_BUTTON
;
1950 if (item
->IsSelected())
1951 image
+= wxOPEN_BUTTON_SELECTED
- wxOPEN_BUTTON
;
1952 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1953 int xx
= x
- (image_w
>>1);
1954 int yy
= y_mid
- (image_h
>>1);
1955 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1956 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1957 wxIMAGELIST_DRAW_TRANSPARENT
);
1958 dc
.DestroyClippingRegion();
1960 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1962 // draw the twisty button here
1963 dc
.SetPen(*wxBLACK_PEN
);
1964 dc
.SetBrush(*m_hilightBrush
);
1968 if (item
->IsExpanded())
1971 button
[0].y
= y_mid
-2;
1973 button
[1].y
= y_mid
-2;
1975 button
[2].y
= y_mid
+3;
1979 button
[0].y
= y_mid
-5;
1981 button
[1].y
= y_mid
+5;
1983 button
[2].y
= y_mid
;
1986 dc
.DrawPolygon(3, button
);
1988 dc
.SetPen(m_dottedPen
);
1990 else // if (HasFlag(wxTR_HAS_BUTTONS))
1992 // draw the plus sign here
1993 dc
.SetPen(*wxGREY_PEN
);
1994 dc
.SetBrush(*wxWHITE_BRUSH
);
1995 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
1996 dc
.SetPen(*wxBLACK_PEN
);
1997 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
1998 if (!item
->IsExpanded())
1999 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2000 dc
.SetPen(m_dottedPen
);
2003 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2005 // draw the horizontal line here
2007 if (x
> (signed)m_indent
|| HasFlag(wxTR_LINES_AT_ROOT
))
2008 x_start
-= m_indent
;
2009 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2014 // don't draw rect outline if we already have the
2015 // background color under Mac
2016 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2017 #endif // !__WXMAC__
2021 if ( item
->IsSelected() )
2023 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2027 wxTreeItemAttr
*attr
= item
->GetAttributes();
2028 if (attr
&& attr
->HasTextColour())
2029 colText
= attr
->GetTextColour();
2031 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2035 dc
.SetTextForeground(colText
);
2039 PaintItem(item
, dc
);
2041 if (HasFlag(wxTR_ROW_LINES
))
2043 dc
.SetPen(*wxWHITE_PEN
);
2044 dc
.DrawLine(0, y_top
, 10000, y_top
);
2045 dc
.DrawLine(0, y
, 10000, y
);
2048 // restore DC objects
2049 dc
.SetBrush(*wxWHITE_BRUSH
);
2050 dc
.SetPen(m_dottedPen
);
2051 dc
.SetTextForeground(*wxBLACK
);
2054 if (item
->IsExpanded())
2056 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2057 int count
= children
.Count();
2064 PaintLevel(children
[n
], dc
, level
, y
);
2065 } while (++n
< count
);
2067 if (!HasFlag(wxTR_NO_LINES
))
2069 // draw line down to last child
2070 oldY
+= GetLineHeight(children
[n
-1])/2;
2071 if (HasButtons()) y_mid
+= 5;
2072 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2078 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2082 if ( item
->HasPlus() )
2084 // it's a folder, indicate it by a border
2089 // draw a line under the drop target because the item will be
2091 DrawLine(item
, TRUE
/* below */);
2094 SetCursor(wxCURSOR_BULLSEYE
);
2099 SetCursor(wxCURSOR_NO_ENTRY
);
2103 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2105 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2107 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2109 wxClientDC
dc(this);
2111 dc
.SetLogicalFunction(wxINVERT
);
2112 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2114 int w
= i
->GetWidth() + 2;
2115 int h
= GetLineHeight(i
) + 2;
2117 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2120 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2122 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2124 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2126 wxClientDC
dc(this);
2128 dc
.SetLogicalFunction(wxINVERT
);
2134 y
+= GetLineHeight(i
) - 1;
2137 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2140 // -----------------------------------------------------------------------------
2141 // wxWindows callbacks
2142 // -----------------------------------------------------------------------------
2144 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2152 dc
.SetFont( m_normalFont
);
2153 dc
.SetPen( m_dottedPen
);
2155 // this is now done dynamically
2156 //if(GetImageList() == NULL)
2157 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2160 PaintLevel( m_anchor
, dc
, 0, y
);
2163 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2172 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2181 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2183 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2184 te
.m_code
= (int)event
.KeyCode();
2185 te
.SetEventObject( this );
2186 GetEventHandler()->ProcessEvent( te
);
2188 if ( (m_current
== 0) || (m_key_current
== 0) )
2194 // how should the selection work for this event?
2195 bool is_multiple
, extended_select
, unselect_others
;
2196 EventFlagsToSelType(GetWindowStyleFlag(),
2198 event
.ControlDown(),
2199 is_multiple
, extended_select
, unselect_others
);
2203 // * : Expand all/Collapse all
2204 // ' ' | return : activate
2205 // up : go up (not last children!)
2207 // left : go to parent
2208 // right : open if parent and go next
2209 // home : go to root
2210 // end : go to last item without opening parents
2211 switch (event
.KeyCode())
2215 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2223 if ( !IsExpanded(m_current
) )
2226 ExpandAll(m_current
);
2229 //else: fall through to Collapse() it
2233 if (IsExpanded(m_current
))
2235 Collapse(m_current
);
2242 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2243 event
.m_item
= (long) m_current
;
2245 event
.SetEventObject( this );
2246 GetEventHandler()->ProcessEvent( event
);
2250 // up goes to the previous sibling or to the last
2251 // of its children if it's expanded
2254 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2257 prev
= GetParent( m_key_current
);
2258 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2260 break; // don't go to root if it is hidden
2265 wxTreeItemId current
= m_key_current
;
2266 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2267 if (current
== GetFirstChild( prev
, cookie
))
2269 // otherwise we return to where we came from
2270 SelectItem( prev
, unselect_others
, extended_select
);
2271 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2272 EnsureVisible( prev
);
2279 while ( IsExpanded(prev
) && HasChildren(prev
) )
2281 wxTreeItemId child
= GetLastChild(prev
);
2288 SelectItem( prev
, unselect_others
, extended_select
);
2289 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2290 EnsureVisible( prev
);
2295 // left arrow goes to the parent
2298 wxTreeItemId prev
= GetParent( m_current
);
2299 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2301 // don't go to root if it is hidden
2302 prev
= GetPrevSibling( m_current
);
2306 EnsureVisible( prev
);
2307 SelectItem( prev
, unselect_others
, extended_select
);
2313 // this works the same as the down arrow except that we
2314 // also expand the item if it wasn't expanded yet
2320 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2323 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2324 SelectItem( child
, unselect_others
, extended_select
);
2325 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2326 EnsureVisible( child
);
2330 wxTreeItemId next
= GetNextSibling( m_key_current
);
2333 wxTreeItemId current
= m_key_current
;
2334 while (current
&& !next
)
2336 current
= GetParent( current
);
2337 if (current
) next
= GetNextSibling( current
);
2342 SelectItem( next
, unselect_others
, extended_select
);
2343 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2344 EnsureVisible( next
);
2350 // <End> selects the last visible tree item
2353 wxTreeItemId last
= GetRootItem();
2355 while ( last
.IsOk() && IsExpanded(last
) )
2357 wxTreeItemId lastChild
= GetLastChild(last
);
2359 // it may happen if the item was expanded but then all of
2360 // its children have been deleted - so IsExpanded() returned
2361 // TRUE, but GetLastChild() returned invalid item
2370 EnsureVisible( last
);
2371 SelectItem( last
, unselect_others
, extended_select
);
2376 // <Home> selects the root item
2379 wxTreeItemId prev
= GetRootItem();
2381 if (HasFlag(wxTR_HIDE_ROOT
))
2384 prev
= GetFirstChild(prev
, dummy
);
2387 EnsureVisible( prev
);
2388 SelectItem( prev
, unselect_others
, extended_select
);
2397 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2399 // JACS: removed wxYieldIfNeeded() because it can cause the window
2400 // to be deleted from under us if a close window event is pending
2405 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2406 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2407 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2408 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2409 if (flags
) return wxTreeItemId();
2411 if (m_anchor
== NULL
)
2413 flags
= wxTREE_HITTEST_NOWHERE
;
2414 return wxTreeItemId();
2417 wxClientDC
dc(this);
2419 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2420 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2421 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2427 flags
= wxTREE_HITTEST_NOWHERE
;
2428 return wxTreeItemId();
2433 // get the bounding rectangle of the item (or of its label only)
2434 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2436 bool WXUNUSED(textOnly
)) const
2438 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2440 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2443 GetViewStart(& startX
, & startY
);
2445 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2446 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2447 rect
.width
= i
->GetWidth();
2448 //rect.height = i->GetHeight();
2449 rect
.height
= GetLineHeight(i
);
2456 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2458 if (!item
.IsOk()) return;
2460 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2462 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2463 te
.m_item
= (long) m_currentEdit
;
2464 te
.SetEventObject( this );
2465 GetEventHandler()->ProcessEvent( te
);
2467 if (!te
.IsAllowed()) return;
2469 // We have to call this here because the label in
2470 // question might just have been added and no screen
2471 // update taken place.
2472 if (m_dirty
) wxYieldIfNeeded();
2474 wxString s
= m_currentEdit
->GetText();
2475 int x
= m_currentEdit
->GetX();
2476 int y
= m_currentEdit
->GetY();
2477 int w
= m_currentEdit
->GetWidth();
2478 int h
= m_currentEdit
->GetHeight();
2483 int image
= m_currentEdit
->GetCurrentImage();
2484 if ( image
!= NO_IMAGE
)
2486 if ( m_imageListNormal
)
2488 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2493 wxFAIL_MSG(_T("you must create an image list to use images!"));
2497 w
-= image_w
+ 4; // I don't know why +4 is needed
2499 wxClientDC
dc(this);
2501 x
= dc
.LogicalToDeviceX( x
);
2502 y
= dc
.LogicalToDeviceY( y
);
2504 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2514 void wxGenericTreeCtrl::OnRenameTimer()
2519 void wxGenericTreeCtrl::OnRenameAccept()
2521 // TODO if the validator fails this causes a crash
2522 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2523 le
.m_item
= (long) m_currentEdit
;
2524 le
.SetEventObject( this );
2525 le
.m_label
= m_renameRes
;
2526 GetEventHandler()->ProcessEvent( le
);
2528 if (!le
.IsAllowed()) return;
2530 SetItemText( m_currentEdit
, m_renameRes
);
2533 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2535 if ( !m_anchor
) return;
2537 // we process left mouse up event (enables in-place edit), right down
2538 // (pass to the user code), left dbl click (activate item) and
2539 // dragging/moving events for items drag-and-drop
2540 if ( !(event
.LeftDown() ||
2542 event
.RightDown() ||
2543 event
.LeftDClick() ||
2545 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2552 wxClientDC
dc(this);
2554 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2555 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2558 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2563 if ( event
.Dragging() && !m_isDragging
)
2565 if (m_dragCount
== 0)
2566 m_dragStart
= wxPoint(x
,y
);
2570 if (m_dragCount
!= 3)
2572 // wait until user drags a bit further...
2576 wxEventType command
= event
.RightIsDown()
2577 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2578 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2580 wxTreeEvent
nevent( command
, GetId() );
2581 nevent
.m_item
= (long) m_current
;
2582 nevent
.SetEventObject(this);
2584 // by default the dragging is not supported, the user code must
2585 // explicitly allow the event for it to take place
2588 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2590 // we're going to drag this item
2591 m_isDragging
= TRUE
;
2593 // remember the old cursor because we will change it while
2595 m_oldCursor
= m_cursor
;
2597 // in a single selection control, hide the selection temporarily
2598 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2600 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2602 if ( m_oldSelection
)
2604 m_oldSelection
->SetHilight(FALSE
);
2605 RefreshLine(m_oldSelection
);
2612 else if ( event
.Moving() )
2614 if ( item
!= m_dropTarget
)
2616 // unhighlight the previous drop target
2617 DrawDropEffect(m_dropTarget
);
2619 m_dropTarget
= item
;
2621 // highlight the current drop target if any
2622 DrawDropEffect(m_dropTarget
);
2627 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2629 // erase the highlighting
2630 DrawDropEffect(m_dropTarget
);
2632 if ( m_oldSelection
)
2634 m_oldSelection
->SetHilight(TRUE
);
2635 RefreshLine(m_oldSelection
);
2636 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2639 // generate the drag end event
2640 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2642 event
.m_item
= (long) item
;
2643 event
.m_pointDrag
= wxPoint(x
, y
);
2644 event
.SetEventObject(this);
2646 (void)GetEventHandler()->ProcessEvent(event
);
2648 m_isDragging
= FALSE
;
2649 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2653 SetCursor(m_oldCursor
);
2659 // here we process only the messages which happen on tree items
2663 if (item
== NULL
) return; /* we hit the blank area */
2665 if ( event
.RightDown() )
2667 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2668 nevent
.m_item
= (long) item
;
2670 CalcScrolledPosition(x
, y
,
2671 &nevent
.m_pointDrag
.x
,
2672 &nevent
.m_pointDrag
.y
);
2673 nevent
.SetEventObject(this);
2674 GetEventHandler()->ProcessEvent(nevent
);
2676 else if ( event
.LeftUp() )
2680 if ( (item
== m_current
) &&
2681 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2682 HasFlag(wxTR_EDIT_LABELS
) )
2684 if ( m_renameTimer
->IsRunning() )
2685 m_renameTimer
->Stop();
2687 m_renameTimer
->Start( 100, TRUE
);
2690 m_lastOnSame
= FALSE
;
2693 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2695 if ( event
.LeftDown() )
2697 m_lastOnSame
= item
== m_current
;
2700 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2702 // only toggle the item for a single click, double click on
2703 // the button doesn't do anything (it toggles the item twice)
2704 if ( event
.LeftDown() )
2709 // don't select the item if the button was clicked
2713 // how should the selection work for this event?
2714 bool is_multiple
, extended_select
, unselect_others
;
2715 EventFlagsToSelType(GetWindowStyleFlag(),
2717 event
.ControlDown(),
2718 is_multiple
, extended_select
, unselect_others
);
2720 SelectItem(item
, unselect_others
, extended_select
);
2722 // For some reason, Windows isn't recognizing a left double-click,
2723 // so we need to simulate it here. Allow 200 milliseconds for now.
2724 if ( event
.LeftDClick() )
2726 // double clicking should not start editing the item label
2727 m_renameTimer
->Stop();
2728 m_lastOnSame
= FALSE
;
2730 if (item
->HasPlus())
2732 // for a "directory" node, toggle expansion
2737 // for a "file" node, activate it
2738 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2740 nevent
.m_item
= (long) item
;
2742 CalcScrolledPosition(x
, y
,
2743 &nevent
.m_pointDrag
.x
,
2744 &nevent
.m_pointDrag
.y
);
2745 nevent
.SetEventObject( this );
2746 GetEventHandler()->ProcessEvent( nevent
);
2753 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2755 /* after all changes have been done to the tree control,
2756 * we actually redraw the tree when everything is over */
2758 if (!m_dirty
) return;
2762 CalculatePositions();
2764 AdjustMyScrollbars();
2767 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2773 dc
.SetFont(m_boldFont
);
2775 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2778 // restore normal font
2779 dc
.SetFont( m_normalFont
);
2783 int image
= item
->GetCurrentImage();
2784 if ( image
!= NO_IMAGE
)
2786 if ( m_imageListNormal
)
2788 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2793 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2796 total_h
+= 2; // at least 2 pixels
2798 total_h
+= total_h
/10; // otherwise 10% extra spacing
2800 item
->SetHeight(total_h
);
2801 if (total_h
>m_lineHeight
)
2802 m_lineHeight
=total_h
;
2804 item
->SetWidth(image_w
+text_w
+2);
2807 // -----------------------------------------------------------------------------
2808 // for developper : y is now the top of the level
2809 // not the middle of it !
2810 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2812 int x
= level
*m_indent
;
2813 if (!HasFlag(wxTR_HIDE_ROOT
))
2817 else if (level
== 0)
2819 // a hidden root is not evaluated, but its
2820 // children are always calculated
2824 CalculateSize( item
, dc
);
2827 item
->SetX( x
+m_spacing
);
2829 y
+= GetLineHeight(item
);
2831 if ( !item
->IsExpanded() )
2833 // we don't need to calculate collapsed branches
2838 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2839 size_t n
, count
= children
.Count();
2841 for (n
= 0; n
< count
; ++n
)
2842 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2845 void wxGenericTreeCtrl::CalculatePositions()
2847 if ( !m_anchor
) return;
2849 wxClientDC
dc(this);
2852 dc
.SetFont( m_normalFont
);
2854 dc
.SetPen( m_dottedPen
);
2855 //if(GetImageList() == NULL)
2856 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2859 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2862 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2864 if (m_dirty
) return;
2866 wxClientDC
dc(this);
2871 GetClientSize( &cw
, &ch
);
2874 rect
.x
= dc
.LogicalToDeviceX( 0 );
2876 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2879 Refresh( TRUE
, &rect
);
2881 AdjustMyScrollbars();
2884 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2886 if (m_dirty
) return;
2888 wxClientDC
dc(this);
2893 GetClientSize( &cw
, &ch
);
2896 rect
.x
= dc
.LogicalToDeviceX( 0 );
2897 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2899 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2901 Refresh( TRUE
, &rect
);
2904 void wxGenericTreeCtrl::RefreshSelected()
2906 // TODO: this is awfully inefficient, we should keep the list of all
2907 // selected items internally, should be much faster
2909 RefreshSelectedUnder(m_anchor
);
2912 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2914 if ( item
->IsSelected() )
2917 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2918 size_t count
= children
.GetCount();
2919 for ( size_t n
= 0; n
< count
; n
++ )
2921 RefreshSelectedUnder(children
[n
]);
2925 #endif // wxUSE_TREECTRL