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
;
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGenericTreeItem
109 wxGenericTreeItem() { m_data
= NULL
; }
110 wxGenericTreeItem( wxGenericTreeItem
*parent
,
111 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
;
311 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
313 if (event
.m_keyCode
== WXK_RETURN
)
316 (*m_res
) = GetValue();
318 if ((*m_res
) != m_startValue
)
319 m_owner
->OnRenameAccept();
321 if (!wxPendingDelete
.Member(this))
322 wxPendingDelete
.Append(this);
325 m_owner
->SetFocus(); // This doesn't work. TODO.
329 if (event
.m_keyCode
== WXK_ESCAPE
)
334 if (!wxPendingDelete
.Member(this))
335 wxPendingDelete
.Append(this);
338 m_owner
->SetFocus(); // This doesn't work. TODO.
345 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
353 // auto-grow the textctrl:
354 wxSize parentSize
= m_owner
->GetSize();
355 wxPoint myPos
= GetPosition();
356 wxSize mySize
= GetSize();
358 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
359 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
360 if (mySize
.x
> sx
) sx
= mySize
.x
;
366 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
374 if (!wxPendingDelete
.Member(this))
375 wxPendingDelete
.Append(this);
378 (*m_res
) = GetValue();
380 if ((*m_res
) != m_startValue
)
381 m_owner
->OnRenameAccept();
384 // -----------------------------------------------------------------------------
386 // -----------------------------------------------------------------------------
388 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
389 const wxString
& text
,
390 int image
, int selImage
,
391 wxTreeItemData
*data
)
394 m_images
[wxTreeItemIcon_Normal
] = image
;
395 m_images
[wxTreeItemIcon_Selected
] = selImage
;
396 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
397 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
402 m_isCollapsed
= TRUE
;
403 m_hasHilight
= FALSE
;
409 m_attr
= (wxTreeItemAttr
*)NULL
;
412 // We don't know the height here yet.
417 wxGenericTreeItem::~wxGenericTreeItem()
421 if (m_ownsAttr
) delete m_attr
;
423 wxASSERT_MSG( m_children
.IsEmpty(),
424 wxT("please call DeleteChildren() before deleting the item") );
427 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
429 size_t count
= m_children
.Count();
430 for ( size_t n
= 0; n
< count
; n
++ )
432 wxGenericTreeItem
*child
= m_children
[n
];
434 tree
->SendDeleteEvent(child
);
436 child
->DeleteChildren(tree
);
443 void wxGenericTreeItem::SetText( const wxString
&text
)
448 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
450 size_t count
= m_children
.Count();
454 size_t total
= count
;
455 for (size_t n
= 0; n
< count
; ++n
)
457 total
+= m_children
[n
]->GetChildrenCount();
463 void wxGenericTreeItem::GetSize( int &x
, int &y
,
464 const wxGenericTreeCtrl
*theButton
)
466 int bottomY
=m_y
+theButton
->GetLineHeight(this);
467 if ( y
< bottomY
) y
= bottomY
;
468 int width
= m_x
+ m_width
;
469 if ( x
< width
) x
= width
;
473 size_t count
= m_children
.Count();
474 for ( size_t n
= 0; n
< count
; ++n
)
476 m_children
[n
]->GetSize( x
, y
, theButton
);
481 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
482 const wxGenericTreeCtrl
*theCtrl
,
486 // for a hidden root node, don't evaluate it, but do evaluate children
487 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
490 int h
= theCtrl
->GetLineHeight(this);
491 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
493 int y_mid
= m_y
+ h
/2;
494 if (point
.y
< y_mid
)
495 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
497 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
499 // 5 is the size of the plus sign
500 int xCross
= m_x
- theCtrl
->GetSpacing();
501 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
502 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
503 HasPlus() && theCtrl
->HasButtons() )
505 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
509 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
514 // assuming every image (normal and selected) has the same size!
515 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
516 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
519 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
520 flags
|= wxTREE_HITTEST_ONITEMICON
;
522 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
528 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
529 if (point
.x
> m_x
+m_width
)
530 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
535 // if children are expanded, fall through to evaluate them
536 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
540 size_t count
= m_children
.Count();
541 for ( size_t n
= 0; n
< count
; n
++ )
543 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
551 return (wxGenericTreeItem
*) NULL
;
554 int wxGenericTreeItem::GetCurrentImage() const
556 int image
= NO_IMAGE
;
561 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
564 if ( image
== NO_IMAGE
)
566 // we usually fall back to the normal item, but try just the
567 // expanded one (and not selected) first in this case
568 image
= GetImage(wxTreeItemIcon_Expanded
);
574 image
= GetImage(wxTreeItemIcon_Selected
);
577 // maybe it doesn't have the specific image we want,
578 // try the default one instead
579 if ( image
== NO_IMAGE
) image
= GetImage();
584 // -----------------------------------------------------------------------------
585 // wxGenericTreeCtrl implementation
586 // -----------------------------------------------------------------------------
588 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
590 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
591 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
592 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
593 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
594 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
595 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
596 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
599 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
601 * wxTreeCtrl has to be a real class or we have problems with
602 * the run-time information.
605 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
608 // -----------------------------------------------------------------------------
609 // construction/destruction
610 // -----------------------------------------------------------------------------
612 void wxGenericTreeCtrl::Init()
614 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
622 m_hilightBrush
= new wxBrush
624 wxSystemSettings::GetSystemColour
626 wxSYS_COLOUR_HIGHLIGHT
631 m_hilightUnfocusedBrush
= new wxBrush
633 wxSystemSettings::GetSystemColour
635 wxSYS_COLOUR_BTNSHADOW
640 m_imageListNormal
= m_imageListButtons
=
641 m_imageListState
= (wxImageList
*) NULL
;
642 m_ownsImageListNormal
= m_ownsImageListButtons
=
643 m_ownsImageListState
= FALSE
;
646 m_isDragging
= FALSE
;
647 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
649 m_renameTimer
= new wxTreeRenameTimer( this );
650 m_lastOnSame
= FALSE
;
652 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
653 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
654 m_normalFont
.GetFamily(),
655 m_normalFont
.GetStyle(),
657 m_normalFont
.GetUnderlined());
660 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
665 const wxValidator
&validator
,
666 const wxString
& name
)
670 wxGetOsVersion( &major
, &minor
);
672 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
673 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
674 style
&= ~wxTR_LINES_AT_ROOT
;
675 style
|= wxTR_NO_LINES
;
677 style
|= wxTR_ROW_LINES
;
680 wxScrolledWindow::Create( parent
, id
, pos
, size
,
681 style
|wxHSCROLL
|wxVSCROLL
, name
);
683 // If the tree display has no buttons, but does have
684 // connecting lines, we can use a narrower layout.
685 // It may not be a good idea to force this...
686 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
693 SetValidator( validator
);
696 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
698 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
699 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
704 wxGenericTreeCtrl::~wxGenericTreeCtrl()
706 delete m_hilightBrush
;
707 delete m_hilightUnfocusedBrush
;
711 delete m_renameTimer
;
712 if (m_ownsImageListNormal
) delete m_imageListNormal
;
713 if (m_ownsImageListState
) delete m_imageListState
;
714 if (m_ownsImageListButtons
) delete m_imageListButtons
;
717 // -----------------------------------------------------------------------------
719 // -----------------------------------------------------------------------------
721 size_t wxGenericTreeCtrl::GetCount() const
723 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
726 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
732 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
738 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
740 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
742 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
745 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
747 // right now, just sets the styles. Eventually, we may
748 // want to update the inherited styles, but right now
749 // none of the parents has updatable styles
750 m_windowStyle
= styles
;
754 // -----------------------------------------------------------------------------
755 // functions to work with tree items
756 // -----------------------------------------------------------------------------
758 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
760 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
762 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
765 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
766 wxTreeItemIcon which
) const
768 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
770 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
773 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
775 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
777 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
780 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
782 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
785 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
786 pItem
->SetText(text
);
787 CalculateSize(pItem
, dc
);
791 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
793 wxTreeItemIcon which
)
795 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
797 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
798 pItem
->SetImage(image
, which
);
801 CalculateSize(pItem
, dc
);
805 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
807 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
809 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
812 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
814 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
816 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
817 pItem
->SetHasPlus(has
);
821 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
823 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
825 // avoid redrawing the tree if no real change
826 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
827 if ( pItem
->IsBold() != bold
)
829 pItem
->SetBold(bold
);
834 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
837 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
839 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
840 pItem
->Attr().SetTextColour(col
);
844 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
847 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
849 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
850 pItem
->Attr().SetBackgroundColour(col
);
854 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
856 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
858 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
859 pItem
->Attr().SetFont(font
);
863 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
865 wxScrolledWindow::SetFont(font
);
867 m_normalFont
= font
;
868 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
869 m_normalFont
.GetFamily(),
870 m_normalFont
.GetStyle(),
872 m_normalFont
.GetUnderlined());
878 // -----------------------------------------------------------------------------
879 // item status inquiries
880 // -----------------------------------------------------------------------------
882 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
884 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
886 // An item is only visible if it's not a descendant of a collapsed item
887 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
888 wxGenericTreeItem
* parent
= pItem
->GetParent();
891 if (!parent
->IsExpanded())
893 parent
= parent
->GetParent();
897 GetViewStart(& startX
, & startY
);
899 wxSize clientSize
= GetClientSize();
902 if (!GetBoundingRect(item
, rect
))
904 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
906 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
908 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
914 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
916 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
918 // consider that the item does have children if it has the "+" button: it
919 // might not have them (if it had never been expanded yet) but then it
920 // could have them as well and it's better to err on this side rather than
921 // disabling some operations which are restricted to the items with
922 // children for an item which does have them
923 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
926 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
928 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
930 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
933 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
935 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
937 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
940 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
942 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
944 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
947 // -----------------------------------------------------------------------------
949 // -----------------------------------------------------------------------------
951 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
953 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
955 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
958 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
960 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
963 return GetNextChild(item
, cookie
);
966 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
968 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
970 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
971 if ( (size_t)cookie
< children
.Count() )
973 return children
.Item((size_t)cookie
++);
977 // there are no more of them
978 return wxTreeItemId();
982 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
984 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
986 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
987 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
990 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
992 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
994 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
995 wxGenericTreeItem
*parent
= i
->GetParent();
996 if ( parent
== NULL
)
998 // root item doesn't have any siblings
999 return wxTreeItemId();
1002 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1003 int index
= siblings
.Index(i
);
1004 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1006 size_t n
= (size_t)(index
+ 1);
1007 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1010 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1012 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1014 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1015 wxGenericTreeItem
*parent
= i
->GetParent();
1016 if ( parent
== NULL
)
1018 // root item doesn't have any siblings
1019 return wxTreeItemId();
1022 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1023 int index
= siblings
.Index(i
);
1024 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1026 return index
== 0 ? wxTreeItemId()
1027 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1030 // Only for internal use right now, but should probably be public
1031 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1033 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1035 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1037 // First see if there are any children.
1038 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1039 if (children
.GetCount() > 0)
1041 return children
.Item(0);
1045 // Try a sibling of this or ancestor instead
1046 wxTreeItemId p
= item
;
1047 wxTreeItemId toFind
;
1050 toFind
= GetNextSibling(p
);
1052 } while (p
.IsOk() && !toFind
.IsOk());
1057 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1059 wxTreeItemId id
= GetRootItem();
1068 } while (id
.IsOk());
1070 return wxTreeItemId();
1073 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1075 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1077 wxTreeItemId id
= item
;
1080 while (id
= GetNext(id
), id
.IsOk())
1086 return wxTreeItemId();
1089 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1091 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1093 wxFAIL_MSG(wxT("not implemented"));
1095 return wxTreeItemId();
1098 // -----------------------------------------------------------------------------
1100 // -----------------------------------------------------------------------------
1102 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1104 const wxString
& text
,
1105 int image
, int selImage
,
1106 wxTreeItemData
*data
)
1108 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1111 // should we give a warning here?
1112 return AddRoot(text
, image
, selImage
, data
);
1115 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1117 wxGenericTreeItem
*item
=
1118 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1122 data
->m_pItem
= (long) item
;
1125 parent
->Insert( item
, previous
);
1130 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1131 int image
, int selImage
,
1132 wxTreeItemData
*data
)
1134 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1136 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1138 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1139 image
, selImage
, data
);
1140 if (HasFlag(wxTR_HIDE_ROOT
))
1142 // if root is hidden, make sure we can navigate
1144 m_anchor
->SetHasPlus();
1149 data
->m_pItem
= (long) m_anchor
;
1152 if (!HasFlag(wxTR_MULTIPLE
))
1154 m_current
= m_key_current
= m_anchor
;
1155 m_current
->SetHilight( TRUE
);
1161 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1162 const wxString
& text
,
1163 int image
, int selImage
,
1164 wxTreeItemData
*data
)
1166 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1169 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1170 const wxTreeItemId
& idPrevious
,
1171 const wxString
& text
,
1172 int image
, int selImage
,
1173 wxTreeItemData
*data
)
1175 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1178 // should we give a warning here?
1179 return AddRoot(text
, image
, selImage
, data
);
1182 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1183 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1184 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1186 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1189 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1191 const wxString
& text
,
1192 int image
, int selImage
,
1193 wxTreeItemData
*data
)
1195 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1198 // should we give a warning here?
1199 return AddRoot(text
, image
, selImage
, data
);
1202 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1205 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1206 const wxString
& text
,
1207 int image
, int selImage
,
1208 wxTreeItemData
*data
)
1210 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1213 // should we give a warning here?
1214 return AddRoot(text
, image
, selImage
, data
);
1217 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1218 image
, selImage
, data
);
1221 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1223 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1224 event
.m_item
= (long) item
;
1225 event
.SetEventObject( this );
1226 ProcessEvent( event
);
1229 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1231 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1233 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1234 item
->DeleteChildren(this);
1237 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1239 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1241 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1243 // don't stay with invalid m_key_current or we will crash in
1244 // the next call to OnChar()
1245 bool changeKeyCurrent
= FALSE
;
1246 wxGenericTreeItem
*itemKey
= m_key_current
;
1249 if ( itemKey
== item
)
1251 // m_key_current is a descendant of the item being deleted
1252 changeKeyCurrent
= TRUE
;
1255 itemKey
= itemKey
->GetParent();
1258 wxGenericTreeItem
*parent
= item
->GetParent();
1261 parent
->GetChildren().Remove( item
); // remove by value
1264 if ( changeKeyCurrent
)
1266 // may be NULL or not
1267 m_key_current
= parent
;
1270 item
->DeleteChildren(this);
1271 SendDeleteEvent(item
);
1275 void wxGenericTreeCtrl::DeleteAllItems()
1281 m_anchor
->DeleteChildren(this);
1288 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1290 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1292 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1294 if ( !item
->HasPlus() )
1297 if ( item
->IsExpanded() )
1300 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1301 event
.m_item
= (long) item
;
1302 event
.SetEventObject( this );
1304 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1306 // cancelled by program
1311 CalculatePositions();
1313 RefreshSubtree(item
);
1315 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1316 ProcessEvent( event
);
1319 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1322 if ( IsExpanded(item
) )
1325 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1326 while ( child
.IsOk() )
1330 child
= GetNextChild(item
, cookie
);
1335 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1337 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1339 if ( !item
->IsExpanded() )
1342 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1343 event
.m_item
= (long) item
;
1344 event
.SetEventObject( this );
1345 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1347 // cancelled by program
1353 #if 0 // TODO why should items be collapsed recursively?
1354 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1355 size_t count
= children
.Count();
1356 for ( size_t n
= 0; n
< count
; n
++ )
1358 Collapse(children
[n
]);
1362 CalculatePositions();
1364 RefreshSubtree(item
);
1366 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1367 ProcessEvent( event
);
1370 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1373 DeleteChildren(item
);
1376 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1378 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1380 if (item
->IsExpanded())
1386 void wxGenericTreeCtrl::Unselect()
1390 m_current
->SetHilight( FALSE
);
1391 RefreshLine( m_current
);
1395 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1397 if (item
->IsSelected())
1399 item
->SetHilight(FALSE
);
1403 if (item
->HasChildren())
1405 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1406 size_t count
= children
.Count();
1407 for ( size_t n
= 0; n
< count
; ++n
)
1409 UnselectAllChildren(children
[n
]);
1414 void wxGenericTreeCtrl::UnselectAll()
1416 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1419 // Recursive function !
1420 // To stop we must have crt_item<last_item
1422 // Tag all next children, when no more children,
1423 // Move to parent (not to tag)
1424 // Keep going... if we found last_item, we stop.
1425 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1427 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1429 if (parent
== NULL
) // This is root item
1430 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1432 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1433 int index
= children
.Index(crt_item
);
1434 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1436 size_t count
= children
.Count();
1437 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1439 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1442 return TagNextChildren(parent
, last_item
, select
);
1445 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1447 crt_item
->SetHilight(select
);
1448 RefreshLine(crt_item
);
1450 if (crt_item
==last_item
)
1453 if (crt_item
->HasChildren())
1455 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1456 size_t count
= children
.Count();
1457 for ( size_t n
= 0; n
< count
; ++n
)
1459 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1467 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1469 // item2 is not necessary after item1
1470 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1472 // choice first' and 'last' between item1 and item2
1473 if (item1
->GetY()<item2
->GetY())
1484 bool select
= m_current
->IsSelected();
1486 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1489 TagNextChildren(first
,last
,select
);
1492 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1493 bool unselect_others
,
1494 bool extended_select
)
1496 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1498 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1499 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1501 //wxCHECK_RET( ( (!unselect_others) && is_single),
1502 // wxT("this is a single selection tree") );
1504 // to keep going anyhow !!!
1507 if (item
->IsSelected())
1508 return; // nothing to do
1509 unselect_others
= TRUE
;
1510 extended_select
= FALSE
;
1512 else if ( unselect_others
&& item
->IsSelected() )
1514 // selection change if there is more than one item currently selected
1515 wxArrayTreeItemIds selected_items
;
1516 if ( GetSelections(selected_items
) == 1 )
1520 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1521 event
.m_item
= (long) item
;
1522 event
.m_itemOld
= (long) m_current
;
1523 event
.SetEventObject( this );
1524 // TODO : Here we don't send any selection mode yet !
1526 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1529 wxTreeItemId parent
= GetParent( itemId
);
1530 while (parent
.IsOk())
1532 if (!IsExpanded(parent
))
1535 parent
= GetParent( parent
);
1538 EnsureVisible( itemId
);
1541 if (unselect_others
)
1543 if (is_single
) Unselect(); // to speed up thing
1548 if (extended_select
)
1552 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1555 // don't change the mark (m_current)
1556 SelectItemRange(m_current
, item
);
1560 bool select
=TRUE
; // the default
1562 // Check if we need to toggle hilight (ctrl mode)
1563 if (!unselect_others
)
1564 select
=!item
->IsSelected();
1566 m_current
= m_key_current
= item
;
1567 m_current
->SetHilight(select
);
1568 RefreshLine( m_current
);
1571 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1572 GetEventHandler()->ProcessEvent( event
);
1575 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1576 wxArrayTreeItemIds
&array
) const
1578 if ( item
->IsSelected() )
1579 array
.Add(wxTreeItemId(item
));
1581 if ( item
->HasChildren() )
1583 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1584 size_t count
= children
.GetCount();
1585 for ( size_t n
= 0; n
< count
; ++n
)
1586 FillArray(children
[n
], array
);
1590 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1593 wxTreeItemId idRoot
= GetRootItem();
1594 if ( idRoot
.IsOk() )
1596 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1598 //else: the tree is empty, so no selections
1600 return array
.Count();
1603 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1605 if (!item
.IsOk()) return;
1607 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1609 // first expand all parent branches
1610 wxGenericTreeItem
*parent
= gitem
->GetParent();
1614 parent
= parent
->GetParent();
1617 //if (parent) CalculatePositions();
1622 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1624 if (!item
.IsOk()) return;
1626 // We have to call this here because the label in
1627 // question might just have been added and no screen
1628 // update taken place.
1629 if (m_dirty
) wxYieldIfNeeded();
1631 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1633 // now scroll to the item
1634 int item_y
= gitem
->GetY();
1638 GetViewStart( &start_x
, &start_y
);
1639 start_y
*= PIXELS_PER_UNIT
;
1643 GetClientSize( &client_w
, &client_h
);
1645 if (item_y
< start_y
+3)
1650 m_anchor
->GetSize( x
, y
, this );
1651 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1652 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1653 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1654 // Item should appear at top
1655 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1657 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1662 m_anchor
->GetSize( x
, y
, this );
1663 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1664 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1665 item_y
+= PIXELS_PER_UNIT
+2;
1666 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1667 // Item should appear at bottom
1668 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
);
1672 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1673 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1675 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1676 wxGenericTreeItem
**item2
)
1678 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1680 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1683 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1684 const wxTreeItemId
& item2
)
1686 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1689 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1691 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1693 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1695 wxCHECK_RET( !s_treeBeingSorted
,
1696 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1698 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1699 if ( children
.Count() > 1 )
1703 s_treeBeingSorted
= this;
1704 children
.Sort(tree_ctrl_compare_func
);
1705 s_treeBeingSorted
= NULL
;
1707 //else: don't make the tree dirty as nothing changed
1710 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1712 return m_imageListNormal
;
1715 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1717 return m_imageListButtons
;
1720 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1722 return m_imageListState
;
1725 void wxGenericTreeCtrl::CalculateLineHeight()
1727 wxClientDC
dc(this);
1728 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1730 if ( m_imageListNormal
)
1732 // Calculate a m_lineHeight value from the normal Image sizes.
1733 // May be toggle off. Then wxGenericTreeCtrl will spread when
1734 // necessary (which might look ugly).
1735 int n
= m_imageListNormal
->GetImageCount();
1736 for (int i
= 0; i
< n
; i
++)
1738 int width
= 0, height
= 0;
1739 m_imageListNormal
->GetSize(i
, width
, height
);
1740 if (height
> m_lineHeight
) m_lineHeight
= height
;
1744 if (m_imageListButtons
)
1746 // Calculate a m_lineHeight value from the Button image sizes.
1747 // May be toggle off. Then wxGenericTreeCtrl will spread when
1748 // necessary (which might look ugly).
1749 int n
= m_imageListButtons
->GetImageCount();
1750 for (int i
= 0; i
< n
; i
++)
1752 int width
= 0, height
= 0;
1753 m_imageListButtons
->GetSize(i
, width
, height
);
1754 if (height
> m_lineHeight
) m_lineHeight
= height
;
1758 if (m_lineHeight
< 30)
1759 m_lineHeight
+= 2; // at least 2 pixels
1761 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1764 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1766 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1767 m_imageListNormal
= imageList
;
1768 m_ownsImageListNormal
= FALSE
;
1770 CalculateLineHeight();
1773 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1775 if (m_ownsImageListState
) delete m_imageListState
;
1776 m_imageListState
= imageList
;
1777 m_ownsImageListState
= FALSE
;
1780 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1782 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1783 m_imageListButtons
= imageList
;
1784 m_ownsImageListButtons
= FALSE
;
1786 CalculateLineHeight();
1789 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1791 SetImageList(imageList
);
1792 m_ownsImageListNormal
= TRUE
;
1795 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1797 SetStateImageList(imageList
);
1798 m_ownsImageListState
= TRUE
;
1801 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1803 SetButtonsImageList(imageList
);
1804 m_ownsImageListButtons
= TRUE
;
1807 // -----------------------------------------------------------------------------
1809 // -----------------------------------------------------------------------------
1811 void wxGenericTreeCtrl::AdjustMyScrollbars()
1816 m_anchor
->GetSize( x
, y
, this );
1817 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1818 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1819 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1820 int y_pos
= GetScrollPos( wxVERTICAL
);
1821 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1825 SetScrollbars( 0, 0, 0, 0 );
1829 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1831 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1832 return item
->GetHeight();
1834 return m_lineHeight
;
1837 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1839 // TODO implement "state" icon on items
1841 wxTreeItemAttr
*attr
= item
->GetAttributes();
1842 if ( attr
&& attr
->HasFont() )
1843 dc
.SetFont(attr
->GetFont());
1844 else if (item
->IsBold())
1845 dc
.SetFont(m_boldFont
);
1847 long text_w
= 0, text_h
= 0;
1848 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1850 int image_h
= 0, image_w
= 0;
1851 int image
= item
->GetCurrentImage();
1852 if ( image
!= NO_IMAGE
)
1854 if ( m_imageListNormal
)
1856 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1865 int total_h
= GetLineHeight(item
);
1867 if ( item
->IsSelected() )
1869 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1874 if ( attr
&& attr
->HasBackgroundColour() )
1875 colBg
= attr
->GetBackgroundColour();
1877 colBg
= m_backgroundColour
;
1878 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1881 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1883 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1885 // If it's selected, and there's an image, then we should
1886 // take care to leave the area under the image painted in the
1887 // background colour.
1888 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1889 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1893 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1894 item
->GetWidth()+2, total_h
-offset
);
1897 if ( image
!= NO_IMAGE
)
1899 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1900 m_imageListNormal
->Draw( image
, dc
,
1902 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1903 wxIMAGELIST_DRAW_TRANSPARENT
);
1904 dc
.DestroyClippingRegion();
1907 dc
.SetBackgroundMode(wxTRANSPARENT
);
1908 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1909 dc
.DrawText( item
->GetText(),
1910 (wxCoord
)(image_w
+ item
->GetX()),
1911 (wxCoord
)(item
->GetY() + extraH
));
1913 // restore normal font
1914 dc
.SetFont( m_normalFont
);
1917 // Now y stands for the top of the item, whereas it used to stand for middle !
1918 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1920 int x
= level
*m_indent
;
1921 if (!HasFlag(wxTR_HIDE_ROOT
))
1925 else if (level
== 0)
1927 // always expand hidden root
1929 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1930 int count
= children
.Count();
1936 PaintLevel(children
[n
], dc
, 1, y
);
1937 } while (++n
< count
);
1939 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
1941 // draw line down to last child
1942 origY
+= GetLineHeight(children
[0])>>1;
1943 oldY
+= GetLineHeight(children
[n
-1])>>1;
1944 dc
.DrawLine(3, origY
, 3, oldY
);
1950 item
->SetX(x
+m_spacing
);
1953 int h
= GetLineHeight(item
);
1955 int y_mid
= y_top
+ (h
>>1);
1958 int exposed_x
= dc
.LogicalToDeviceX(0);
1959 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1961 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1963 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1965 if (!HasFlag(wxTR_NO_LINES
))
1967 if (x
> (signed)m_indent
)
1968 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1969 else if (HasFlag(wxTR_LINES_AT_ROOT
))
1970 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
1971 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1974 if (m_imageListButtons
!= NULL
)
1976 // draw the image button here
1977 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
1978 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
1979 if (item
->IsSelected())
1980 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
1981 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1982 int xx
= x
- (image_w
>>1);
1983 int yy
= y_mid
- (image_h
>>1);
1984 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1985 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1986 wxIMAGELIST_DRAW_TRANSPARENT
);
1987 dc
.DestroyClippingRegion();
1989 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1991 // draw the twisty button here
1992 dc
.SetPen(*wxBLACK_PEN
);
1993 dc
.SetBrush(*m_hilightBrush
);
1997 if (item
->IsExpanded())
2000 button
[0].y
= y_mid
-2;
2002 button
[1].y
= y_mid
-2;
2004 button
[2].y
= y_mid
+3;
2008 button
[0].y
= y_mid
-5;
2010 button
[1].y
= y_mid
+5;
2012 button
[2].y
= y_mid
;
2015 dc
.DrawPolygon(3, button
);
2017 dc
.SetPen(m_dottedPen
);
2019 else // if (HasFlag(wxTR_HAS_BUTTONS))
2021 // draw the plus sign here
2022 dc
.SetPen(*wxGREY_PEN
);
2023 dc
.SetBrush(*wxWHITE_BRUSH
);
2024 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2025 dc
.SetPen(*wxBLACK_PEN
);
2026 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2027 if (!item
->IsExpanded())
2028 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2029 dc
.SetPen(m_dottedPen
);
2032 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2034 // draw the horizontal line here
2036 if (x
> (signed)m_indent
)
2037 x_start
-= m_indent
;
2038 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2040 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2045 // don't draw rect outline if we already have the
2046 // background color under Mac
2047 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2048 #endif // !__WXMAC__
2052 if ( item
->IsSelected() )
2054 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2058 wxTreeItemAttr
*attr
= item
->GetAttributes();
2059 if (attr
&& attr
->HasTextColour())
2060 colText
= attr
->GetTextColour();
2062 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2066 dc
.SetTextForeground(colText
);
2070 PaintItem(item
, dc
);
2072 if (HasFlag(wxTR_ROW_LINES
))
2074 // if the background colour is white, choose a
2075 // contrasting color for the lines
2076 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2077 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2078 dc
.DrawLine(0, y_top
, 10000, y_top
);
2079 dc
.DrawLine(0, y
, 10000, y
);
2082 // restore DC objects
2083 dc
.SetBrush(*wxWHITE_BRUSH
);
2084 dc
.SetPen(m_dottedPen
);
2085 dc
.SetTextForeground(*wxBLACK
);
2088 if (item
->IsExpanded())
2090 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2091 int count
= children
.Count();
2098 PaintLevel(children
[n
], dc
, level
, y
);
2099 } while (++n
< count
);
2101 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2103 // draw line down to last child
2104 oldY
+= GetLineHeight(children
[n
-1])>>1;
2105 if (HasButtons()) y_mid
+= 5;
2106 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2112 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2116 if ( item
->HasPlus() )
2118 // it's a folder, indicate it by a border
2123 // draw a line under the drop target because the item will be
2125 DrawLine(item
, TRUE
/* below */);
2128 SetCursor(wxCURSOR_BULLSEYE
);
2133 SetCursor(wxCURSOR_NO_ENTRY
);
2137 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2139 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2141 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2143 wxClientDC
dc(this);
2145 dc
.SetLogicalFunction(wxINVERT
);
2146 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2148 int w
= i
->GetWidth() + 2;
2149 int h
= GetLineHeight(i
) + 2;
2151 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2154 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2156 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2158 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2160 wxClientDC
dc(this);
2162 dc
.SetLogicalFunction(wxINVERT
);
2168 y
+= GetLineHeight(i
) - 1;
2171 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2174 // -----------------------------------------------------------------------------
2175 // wxWindows callbacks
2176 // -----------------------------------------------------------------------------
2178 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2186 dc
.SetFont( m_normalFont
);
2187 dc
.SetPen( m_dottedPen
);
2189 // this is now done dynamically
2190 //if(GetImageList() == NULL)
2191 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2194 PaintLevel( m_anchor
, dc
, 0, y
);
2197 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2206 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2215 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2217 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2218 te
.m_evtKey
= event
;
2219 te
.SetEventObject( this );
2220 if ( GetEventHandler()->ProcessEvent( te
) )
2222 // intercepted by the user code
2226 if ( (m_current
== 0) || (m_key_current
== 0) )
2232 // how should the selection work for this event?
2233 bool is_multiple
, extended_select
, unselect_others
;
2234 EventFlagsToSelType(GetWindowStyleFlag(),
2236 event
.ControlDown(),
2237 is_multiple
, extended_select
, unselect_others
);
2241 // * : Expand all/Collapse all
2242 // ' ' | return : activate
2243 // up : go up (not last children!)
2245 // left : go to parent
2246 // right : open if parent and go next
2247 // home : go to root
2248 // end : go to last item without opening parents
2249 switch (event
.KeyCode())
2253 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2261 if ( !IsExpanded(m_current
) )
2264 ExpandAll(m_current
);
2267 //else: fall through to Collapse() it
2271 if (IsExpanded(m_current
))
2273 Collapse(m_current
);
2280 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2281 event
.m_item
= (long) m_current
;
2282 event
.SetEventObject( this );
2283 GetEventHandler()->ProcessEvent( event
);
2287 // up goes to the previous sibling or to the last
2288 // of its children if it's expanded
2291 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2294 prev
= GetParent( m_key_current
);
2295 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2297 break; // don't go to root if it is hidden
2302 wxTreeItemId current
= m_key_current
;
2303 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2304 if (current
== GetFirstChild( prev
, cookie
))
2306 // otherwise we return to where we came from
2307 SelectItem( prev
, unselect_others
, extended_select
);
2308 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2309 EnsureVisible( prev
);
2316 while ( IsExpanded(prev
) && HasChildren(prev
) )
2318 wxTreeItemId child
= GetLastChild(prev
);
2325 SelectItem( prev
, unselect_others
, extended_select
);
2326 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2327 EnsureVisible( prev
);
2332 // left arrow goes to the parent
2335 wxTreeItemId prev
= GetParent( m_current
);
2336 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2338 // don't go to root if it is hidden
2339 prev
= GetPrevSibling( m_current
);
2343 EnsureVisible( prev
);
2344 SelectItem( prev
, unselect_others
, extended_select
);
2350 // this works the same as the down arrow except that we
2351 // also expand the item if it wasn't expanded yet
2357 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2360 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2361 SelectItem( child
, unselect_others
, extended_select
);
2362 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2363 EnsureVisible( child
);
2367 wxTreeItemId next
= GetNextSibling( m_key_current
);
2370 wxTreeItemId current
= m_key_current
;
2371 while (current
&& !next
)
2373 current
= GetParent( current
);
2374 if (current
) next
= GetNextSibling( current
);
2379 SelectItem( next
, unselect_others
, extended_select
);
2380 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2381 EnsureVisible( next
);
2387 // <End> selects the last visible tree item
2390 wxTreeItemId last
= GetRootItem();
2392 while ( last
.IsOk() && IsExpanded(last
) )
2394 wxTreeItemId lastChild
= GetLastChild(last
);
2396 // it may happen if the item was expanded but then all of
2397 // its children have been deleted - so IsExpanded() returned
2398 // TRUE, but GetLastChild() returned invalid item
2407 EnsureVisible( last
);
2408 SelectItem( last
, unselect_others
, extended_select
);
2413 // <Home> selects the root item
2416 wxTreeItemId prev
= GetRootItem();
2418 if (HasFlag(wxTR_HIDE_ROOT
))
2421 prev
= GetFirstChild(prev
, dummy
);
2424 EnsureVisible( prev
);
2425 SelectItem( prev
, unselect_others
, extended_select
);
2434 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2436 // JACS: removed wxYieldIfNeeded() because it can cause the window
2437 // to be deleted from under us if a close window event is pending
2442 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2443 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2444 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2445 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2446 if (flags
) return wxTreeItemId();
2448 if (m_anchor
== NULL
)
2450 flags
= wxTREE_HITTEST_NOWHERE
;
2451 return wxTreeItemId();
2454 wxClientDC
dc(this);
2456 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2457 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2458 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2464 flags
= wxTREE_HITTEST_NOWHERE
;
2465 return wxTreeItemId();
2470 // get the bounding rectangle of the item (or of its label only)
2471 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2473 bool WXUNUSED(textOnly
)) const
2475 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2477 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2480 GetViewStart(& startX
, & startY
);
2482 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2483 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2484 rect
.width
= i
->GetWidth();
2485 //rect.height = i->GetHeight();
2486 rect
.height
= GetLineHeight(i
);
2493 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2495 if (!item
.IsOk()) return;
2497 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2499 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2500 te
.m_item
= (long) m_currentEdit
;
2501 te
.SetEventObject( this );
2502 GetEventHandler()->ProcessEvent( te
);
2504 if (!te
.IsAllowed()) return;
2506 // We have to call this here because the label in
2507 // question might just have been added and no screen
2508 // update taken place.
2509 if (m_dirty
) wxYieldIfNeeded();
2511 wxString s
= m_currentEdit
->GetText();
2512 int x
= m_currentEdit
->GetX();
2513 int y
= m_currentEdit
->GetY();
2514 int w
= m_currentEdit
->GetWidth();
2515 int h
= m_currentEdit
->GetHeight();
2520 int image
= m_currentEdit
->GetCurrentImage();
2521 if ( image
!= NO_IMAGE
)
2523 if ( m_imageListNormal
)
2525 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2530 wxFAIL_MSG(_T("you must create an image list to use images!"));
2534 w
-= image_w
+ 4; // I don't know why +4 is needed
2536 wxClientDC
dc(this);
2538 x
= dc
.LogicalToDeviceX( x
);
2539 y
= dc
.LogicalToDeviceY( y
);
2541 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2551 void wxGenericTreeCtrl::OnRenameTimer()
2556 void wxGenericTreeCtrl::OnRenameAccept()
2558 // TODO if the validator fails this causes a crash
2559 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2560 le
.m_item
= (long) m_currentEdit
;
2561 le
.SetEventObject( this );
2562 le
.m_label
= m_renameRes
;
2563 GetEventHandler()->ProcessEvent( le
);
2565 if (!le
.IsAllowed()) return;
2567 SetItemText( m_currentEdit
, m_renameRes
);
2570 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2572 if ( !m_anchor
) return;
2574 // we process left mouse up event (enables in-place edit), right down
2575 // (pass to the user code), left dbl click (activate item) and
2576 // dragging/moving events for items drag-and-drop
2577 if ( !(event
.LeftDown() ||
2579 event
.RightDown() ||
2580 event
.LeftDClick() ||
2582 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2589 wxClientDC
dc(this);
2591 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2592 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2595 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2600 if ( event
.Dragging() && !m_isDragging
)
2602 if (m_dragCount
== 0)
2603 m_dragStart
= wxPoint(x
,y
);
2607 if (m_dragCount
!= 3)
2609 // wait until user drags a bit further...
2613 wxEventType command
= event
.RightIsDown()
2614 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2615 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2617 wxTreeEvent
nevent( command
, GetId() );
2618 nevent
.m_item
= (long) m_current
;
2619 nevent
.SetEventObject(this);
2621 // by default the dragging is not supported, the user code must
2622 // explicitly allow the event for it to take place
2625 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2627 // we're going to drag this item
2628 m_isDragging
= TRUE
;
2630 // remember the old cursor because we will change it while
2632 m_oldCursor
= m_cursor
;
2634 // in a single selection control, hide the selection temporarily
2635 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2637 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2639 if ( m_oldSelection
)
2641 m_oldSelection
->SetHilight(FALSE
);
2642 RefreshLine(m_oldSelection
);
2649 else if ( event
.Moving() )
2651 if ( item
!= m_dropTarget
)
2653 // unhighlight the previous drop target
2654 DrawDropEffect(m_dropTarget
);
2656 m_dropTarget
= item
;
2658 // highlight the current drop target if any
2659 DrawDropEffect(m_dropTarget
);
2664 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2666 // erase the highlighting
2667 DrawDropEffect(m_dropTarget
);
2669 if ( m_oldSelection
)
2671 m_oldSelection
->SetHilight(TRUE
);
2672 RefreshLine(m_oldSelection
);
2673 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2676 // generate the drag end event
2677 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2679 event
.m_item
= (long) item
;
2680 event
.m_pointDrag
= wxPoint(x
, y
);
2681 event
.SetEventObject(this);
2683 (void)GetEventHandler()->ProcessEvent(event
);
2685 m_isDragging
= FALSE
;
2686 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2690 SetCursor(m_oldCursor
);
2696 // here we process only the messages which happen on tree items
2700 if (item
== NULL
) return; /* we hit the blank area */
2702 if ( event
.RightDown() )
2704 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2705 nevent
.m_item
= (long) item
;
2706 CalcScrolledPosition(x
, y
,
2707 &nevent
.m_pointDrag
.x
,
2708 &nevent
.m_pointDrag
.y
);
2709 nevent
.SetEventObject(this);
2710 GetEventHandler()->ProcessEvent(nevent
);
2712 else if ( event
.LeftUp() )
2716 if ( (item
== m_current
) &&
2717 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2718 HasFlag(wxTR_EDIT_LABELS
) )
2720 if ( m_renameTimer
->IsRunning() )
2721 m_renameTimer
->Stop();
2723 m_renameTimer
->Start( 100, TRUE
);
2726 m_lastOnSame
= FALSE
;
2729 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2731 if ( event
.LeftDown() )
2733 m_lastOnSame
= item
== m_current
;
2736 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2738 // only toggle the item for a single click, double click on
2739 // the button doesn't do anything (it toggles the item twice)
2740 if ( event
.LeftDown() )
2745 // don't select the item if the button was clicked
2749 // how should the selection work for this event?
2750 bool is_multiple
, extended_select
, unselect_others
;
2751 EventFlagsToSelType(GetWindowStyleFlag(),
2753 event
.ControlDown(),
2754 is_multiple
, extended_select
, unselect_others
);
2756 SelectItem(item
, unselect_others
, extended_select
);
2758 // For some reason, Windows isn't recognizing a left double-click,
2759 // so we need to simulate it here. Allow 200 milliseconds for now.
2760 if ( event
.LeftDClick() )
2762 // double clicking should not start editing the item label
2763 m_renameTimer
->Stop();
2764 m_lastOnSame
= FALSE
;
2766 // send activate event first
2767 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2768 nevent
.m_item
= (long) item
;
2769 CalcScrolledPosition(x
, y
,
2770 &nevent
.m_pointDrag
.x
,
2771 &nevent
.m_pointDrag
.y
);
2772 nevent
.SetEventObject( this );
2773 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2775 // if the user code didn't process the activate event,
2776 // handle it ourselves by toggling the item when it is
2778 if ( item
->HasPlus() )
2788 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2790 /* after all changes have been done to the tree control,
2791 * we actually redraw the tree when everything is over */
2793 if (!m_dirty
) return;
2797 CalculatePositions();
2799 AdjustMyScrollbars();
2802 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2808 dc
.SetFont(m_boldFont
);
2810 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2813 // restore normal font
2814 dc
.SetFont( m_normalFont
);
2818 int image
= item
->GetCurrentImage();
2819 if ( image
!= NO_IMAGE
)
2821 if ( m_imageListNormal
)
2823 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2828 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2831 total_h
+= 2; // at least 2 pixels
2833 total_h
+= total_h
/10; // otherwise 10% extra spacing
2835 item
->SetHeight(total_h
);
2836 if (total_h
>m_lineHeight
)
2837 m_lineHeight
=total_h
;
2839 item
->SetWidth(image_w
+text_w
+2);
2842 // -----------------------------------------------------------------------------
2843 // for developper : y is now the top of the level
2844 // not the middle of it !
2845 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2847 int x
= level
*m_indent
;
2848 if (!HasFlag(wxTR_HIDE_ROOT
))
2852 else if (level
== 0)
2854 // a hidden root is not evaluated, but its
2855 // children are always calculated
2859 CalculateSize( item
, dc
);
2862 item
->SetX( x
+m_spacing
);
2864 y
+= GetLineHeight(item
);
2866 if ( !item
->IsExpanded() )
2868 // we don't need to calculate collapsed branches
2873 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2874 size_t n
, count
= children
.Count();
2876 for (n
= 0; n
< count
; ++n
)
2877 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2880 void wxGenericTreeCtrl::CalculatePositions()
2882 if ( !m_anchor
) return;
2884 wxClientDC
dc(this);
2887 dc
.SetFont( m_normalFont
);
2889 dc
.SetPen( m_dottedPen
);
2890 //if(GetImageList() == NULL)
2891 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2894 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2897 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2899 if (m_dirty
) return;
2901 wxClientDC
dc(this);
2906 GetClientSize( &cw
, &ch
);
2909 rect
.x
= dc
.LogicalToDeviceX( 0 );
2911 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2914 Refresh( TRUE
, &rect
);
2916 AdjustMyScrollbars();
2919 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2921 if (m_dirty
) return;
2923 wxClientDC
dc(this);
2928 GetClientSize( &cw
, &ch
);
2931 rect
.x
= dc
.LogicalToDeviceX( 0 );
2932 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2934 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2936 Refresh( TRUE
, &rect
);
2939 void wxGenericTreeCtrl::RefreshSelected()
2941 // TODO: this is awfully inefficient, we should keep the list of all
2942 // selected items internally, should be much faster
2944 RefreshSelectedUnder(m_anchor
);
2947 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2949 if ( item
->IsSelected() )
2952 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2953 size_t count
= children
.GetCount();
2954 for ( size_t n
= 0; n
< count
; n
++ )
2956 RefreshSelectedUnder(children
[n
]);
2960 // ----------------------------------------------------------------------------
2961 // changing colours: we need to refresh the tree control
2962 // ----------------------------------------------------------------------------
2964 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
2966 if ( !wxWindow::SetBackgroundColour(colour
) )
2974 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
2976 if ( !wxWindow::SetForegroundColour(colour
) )
2984 #endif // wxUSE_TREECTRL