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
;
679 style
|= wxTR_AQUA_BUTTONS
;
683 wxScrolledWindow::Create( parent
, id
, pos
, size
,
684 style
|wxHSCROLL
|wxVSCROLL
, name
);
686 // If the tree display has no buttons, but does have
687 // connecting lines, we can use a narrower layout.
688 // It may not be a good idea to force this...
689 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
696 SetValidator( validator
);
699 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
701 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
702 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
707 wxGenericTreeCtrl::~wxGenericTreeCtrl()
709 delete m_hilightBrush
;
710 delete m_hilightUnfocusedBrush
;
714 delete m_renameTimer
;
715 if (m_ownsImageListNormal
) delete m_imageListNormal
;
716 if (m_ownsImageListState
) delete m_imageListState
;
717 if (m_ownsImageListButtons
) delete m_imageListButtons
;
720 // -----------------------------------------------------------------------------
722 // -----------------------------------------------------------------------------
724 size_t wxGenericTreeCtrl::GetCount() const
726 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
729 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
735 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
741 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
743 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
745 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
748 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
750 // right now, just sets the styles. Eventually, we may
751 // want to update the inherited styles, but right now
752 // none of the parents has updatable styles
753 m_windowStyle
= styles
;
757 // -----------------------------------------------------------------------------
758 // functions to work with tree items
759 // -----------------------------------------------------------------------------
761 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
763 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
765 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
768 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
769 wxTreeItemIcon which
) const
771 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
773 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
776 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
778 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
780 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
783 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
785 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
788 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
789 pItem
->SetText(text
);
790 CalculateSize(pItem
, dc
);
794 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
796 wxTreeItemIcon which
)
798 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
800 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
801 pItem
->SetImage(image
, which
);
804 CalculateSize(pItem
, dc
);
808 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
810 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
812 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
815 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
817 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
819 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
820 pItem
->SetHasPlus(has
);
824 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
826 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
828 // avoid redrawing the tree if no real change
829 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
830 if ( pItem
->IsBold() != bold
)
832 pItem
->SetBold(bold
);
837 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
840 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
842 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
843 pItem
->Attr().SetTextColour(col
);
847 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
850 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
852 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
853 pItem
->Attr().SetBackgroundColour(col
);
857 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
859 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
861 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
862 pItem
->Attr().SetFont(font
);
866 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
868 wxScrolledWindow::SetFont(font
);
870 m_normalFont
= font
;
871 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
872 m_normalFont
.GetFamily(),
873 m_normalFont
.GetStyle(),
875 m_normalFont
.GetUnderlined());
881 // -----------------------------------------------------------------------------
882 // item status inquiries
883 // -----------------------------------------------------------------------------
885 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
887 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
889 // An item is only visible if it's not a descendant of a collapsed item
890 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
891 wxGenericTreeItem
* parent
= pItem
->GetParent();
894 if (!parent
->IsExpanded())
896 parent
= parent
->GetParent();
900 GetViewStart(& startX
, & startY
);
902 wxSize clientSize
= GetClientSize();
905 if (!GetBoundingRect(item
, rect
))
907 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
909 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
911 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
917 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
919 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
921 // consider that the item does have children if it has the "+" button: it
922 // might not have them (if it had never been expanded yet) but then it
923 // could have them as well and it's better to err on this side rather than
924 // disabling some operations which are restricted to the items with
925 // children for an item which does have them
926 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
929 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
931 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
933 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
936 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
938 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
940 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
943 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
945 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
947 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
950 // -----------------------------------------------------------------------------
952 // -----------------------------------------------------------------------------
954 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
956 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
958 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
961 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
963 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
966 return GetNextChild(item
, cookie
);
969 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
971 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
973 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
974 if ( (size_t)cookie
< children
.Count() )
976 return children
.Item((size_t)cookie
++);
980 // there are no more of them
981 return wxTreeItemId();
985 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
987 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
989 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
990 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
993 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
995 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
997 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
998 wxGenericTreeItem
*parent
= i
->GetParent();
999 if ( parent
== NULL
)
1001 // root item doesn't have any siblings
1002 return wxTreeItemId();
1005 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1006 int index
= siblings
.Index(i
);
1007 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1009 size_t n
= (size_t)(index
+ 1);
1010 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1013 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1015 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1017 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1018 wxGenericTreeItem
*parent
= i
->GetParent();
1019 if ( parent
== NULL
)
1021 // root item doesn't have any siblings
1022 return wxTreeItemId();
1025 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1026 int index
= siblings
.Index(i
);
1027 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1029 return index
== 0 ? wxTreeItemId()
1030 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1033 // Only for internal use right now, but should probably be public
1034 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1036 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1038 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1040 // First see if there are any children.
1041 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1042 if (children
.GetCount() > 0)
1044 return children
.Item(0);
1048 // Try a sibling of this or ancestor instead
1049 wxTreeItemId p
= item
;
1050 wxTreeItemId toFind
;
1053 toFind
= GetNextSibling(p
);
1055 } while (p
.IsOk() && !toFind
.IsOk());
1060 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1062 wxTreeItemId id
= GetRootItem();
1071 } while (id
.IsOk());
1073 return wxTreeItemId();
1076 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1078 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1080 wxTreeItemId id
= item
;
1083 while (id
= GetNext(id
), id
.IsOk())
1089 return wxTreeItemId();
1092 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1094 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1096 wxFAIL_MSG(wxT("not implemented"));
1098 return wxTreeItemId();
1101 // -----------------------------------------------------------------------------
1103 // -----------------------------------------------------------------------------
1105 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1107 const wxString
& text
,
1108 int image
, int selImage
,
1109 wxTreeItemData
*data
)
1111 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1114 // should we give a warning here?
1115 return AddRoot(text
, image
, selImage
, data
);
1118 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1120 wxGenericTreeItem
*item
=
1121 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1125 data
->m_pItem
= (long) item
;
1128 parent
->Insert( item
, previous
);
1133 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1134 int image
, int selImage
,
1135 wxTreeItemData
*data
)
1137 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1139 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1141 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1142 image
, selImage
, data
);
1143 if (HasFlag(wxTR_HIDE_ROOT
))
1145 // if root is hidden, make sure we can navigate
1147 m_anchor
->SetHasPlus();
1152 data
->m_pItem
= (long) m_anchor
;
1155 if (!HasFlag(wxTR_MULTIPLE
))
1157 m_current
= m_key_current
= m_anchor
;
1158 m_current
->SetHilight( TRUE
);
1164 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1165 const wxString
& text
,
1166 int image
, int selImage
,
1167 wxTreeItemData
*data
)
1169 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1172 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1173 const wxTreeItemId
& idPrevious
,
1174 const wxString
& text
,
1175 int image
, int selImage
,
1176 wxTreeItemData
*data
)
1178 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1181 // should we give a warning here?
1182 return AddRoot(text
, image
, selImage
, data
);
1185 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1186 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1187 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1189 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1192 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1194 const wxString
& text
,
1195 int image
, int selImage
,
1196 wxTreeItemData
*data
)
1198 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1201 // should we give a warning here?
1202 return AddRoot(text
, image
, selImage
, data
);
1205 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1208 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1209 const wxString
& text
,
1210 int image
, int selImage
,
1211 wxTreeItemData
*data
)
1213 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1216 // should we give a warning here?
1217 return AddRoot(text
, image
, selImage
, data
);
1220 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1221 image
, selImage
, data
);
1224 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1226 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1227 event
.m_item
= (long) item
;
1228 event
.SetEventObject( this );
1229 ProcessEvent( event
);
1232 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1234 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1236 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1237 item
->DeleteChildren(this);
1240 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1242 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1244 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1246 // don't stay with invalid m_key_current or we will crash in
1247 // the next call to OnChar()
1248 bool changeKeyCurrent
= FALSE
;
1249 wxGenericTreeItem
*itemKey
= m_key_current
;
1252 if ( itemKey
== item
)
1254 // m_key_current is a descendant of the item being deleted
1255 changeKeyCurrent
= TRUE
;
1258 itemKey
= itemKey
->GetParent();
1261 wxGenericTreeItem
*parent
= item
->GetParent();
1264 parent
->GetChildren().Remove( item
); // remove by value
1267 if ( changeKeyCurrent
)
1269 // may be NULL or not
1270 m_key_current
= parent
;
1273 item
->DeleteChildren(this);
1274 SendDeleteEvent(item
);
1278 void wxGenericTreeCtrl::DeleteAllItems()
1284 m_anchor
->DeleteChildren(this);
1291 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1293 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1295 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1297 if ( !item
->HasPlus() )
1300 if ( item
->IsExpanded() )
1303 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1304 event
.m_item
= (long) item
;
1305 event
.SetEventObject( this );
1307 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1309 // cancelled by program
1314 CalculatePositions();
1316 RefreshSubtree(item
);
1318 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1319 ProcessEvent( event
);
1322 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1325 if ( IsExpanded(item
) )
1328 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1329 while ( child
.IsOk() )
1333 child
= GetNextChild(item
, cookie
);
1338 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1340 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1342 if ( !item
->IsExpanded() )
1345 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1346 event
.m_item
= (long) item
;
1347 event
.SetEventObject( this );
1348 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1350 // cancelled by program
1356 #if 0 // TODO why should items be collapsed recursively?
1357 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1358 size_t count
= children
.Count();
1359 for ( size_t n
= 0; n
< count
; n
++ )
1361 Collapse(children
[n
]);
1365 CalculatePositions();
1367 RefreshSubtree(item
);
1369 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1370 ProcessEvent( event
);
1373 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1376 DeleteChildren(item
);
1379 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1381 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1383 if (item
->IsExpanded())
1389 void wxGenericTreeCtrl::Unselect()
1393 m_current
->SetHilight( FALSE
);
1394 RefreshLine( m_current
);
1398 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1400 if (item
->IsSelected())
1402 item
->SetHilight(FALSE
);
1406 if (item
->HasChildren())
1408 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1409 size_t count
= children
.Count();
1410 for ( size_t n
= 0; n
< count
; ++n
)
1412 UnselectAllChildren(children
[n
]);
1417 void wxGenericTreeCtrl::UnselectAll()
1419 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1422 // Recursive function !
1423 // To stop we must have crt_item<last_item
1425 // Tag all next children, when no more children,
1426 // Move to parent (not to tag)
1427 // Keep going... if we found last_item, we stop.
1428 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1430 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1432 if (parent
== NULL
) // This is root item
1433 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1435 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1436 int index
= children
.Index(crt_item
);
1437 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1439 size_t count
= children
.Count();
1440 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1442 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1445 return TagNextChildren(parent
, last_item
, select
);
1448 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1450 crt_item
->SetHilight(select
);
1451 RefreshLine(crt_item
);
1453 if (crt_item
==last_item
)
1456 if (crt_item
->HasChildren())
1458 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1459 size_t count
= children
.Count();
1460 for ( size_t n
= 0; n
< count
; ++n
)
1462 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1470 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1472 // item2 is not necessary after item1
1473 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1475 // choice first' and 'last' between item1 and item2
1476 if (item1
->GetY()<item2
->GetY())
1487 bool select
= m_current
->IsSelected();
1489 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1492 TagNextChildren(first
,last
,select
);
1495 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1496 bool unselect_others
,
1497 bool extended_select
)
1499 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1501 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1502 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1504 //wxCHECK_RET( ( (!unselect_others) && is_single),
1505 // wxT("this is a single selection tree") );
1507 // to keep going anyhow !!!
1510 if (item
->IsSelected())
1511 return; // nothing to do
1512 unselect_others
= TRUE
;
1513 extended_select
= FALSE
;
1515 else if ( unselect_others
&& item
->IsSelected() )
1517 // selection change if there is more than one item currently selected
1518 wxArrayTreeItemIds selected_items
;
1519 if ( GetSelections(selected_items
) == 1 )
1523 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1524 event
.m_item
= (long) item
;
1525 event
.m_itemOld
= (long) m_current
;
1526 event
.SetEventObject( this );
1527 // TODO : Here we don't send any selection mode yet !
1529 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1532 wxTreeItemId parent
= GetParent( itemId
);
1533 while (parent
.IsOk())
1535 if (!IsExpanded(parent
))
1538 parent
= GetParent( parent
);
1541 EnsureVisible( itemId
);
1544 if (unselect_others
)
1546 if (is_single
) Unselect(); // to speed up thing
1551 if (extended_select
)
1555 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1558 // don't change the mark (m_current)
1559 SelectItemRange(m_current
, item
);
1563 bool select
=TRUE
; // the default
1565 // Check if we need to toggle hilight (ctrl mode)
1566 if (!unselect_others
)
1567 select
=!item
->IsSelected();
1569 m_current
= m_key_current
= item
;
1570 m_current
->SetHilight(select
);
1571 RefreshLine( m_current
);
1574 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1575 GetEventHandler()->ProcessEvent( event
);
1578 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1579 wxArrayTreeItemIds
&array
) const
1581 if ( item
->IsSelected() )
1582 array
.Add(wxTreeItemId(item
));
1584 if ( item
->HasChildren() )
1586 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1587 size_t count
= children
.GetCount();
1588 for ( size_t n
= 0; n
< count
; ++n
)
1589 FillArray(children
[n
], array
);
1593 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1596 wxTreeItemId idRoot
= GetRootItem();
1597 if ( idRoot
.IsOk() )
1599 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1601 //else: the tree is empty, so no selections
1603 return array
.Count();
1606 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1608 if (!item
.IsOk()) return;
1610 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1612 // first expand all parent branches
1613 wxGenericTreeItem
*parent
= gitem
->GetParent();
1617 parent
= parent
->GetParent();
1620 //if (parent) CalculatePositions();
1625 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1627 if (!item
.IsOk()) return;
1629 // We have to call this here because the label in
1630 // question might just have been added and no screen
1631 // update taken place.
1632 if (m_dirty
) wxYieldIfNeeded();
1634 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1636 // now scroll to the item
1637 int item_y
= gitem
->GetY();
1641 GetViewStart( &start_x
, &start_y
);
1642 start_y
*= PIXELS_PER_UNIT
;
1646 GetClientSize( &client_w
, &client_h
);
1648 if (item_y
< start_y
+3)
1653 m_anchor
->GetSize( x
, y
, this );
1654 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1655 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1656 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1657 // Item should appear at top
1658 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1660 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1665 m_anchor
->GetSize( x
, y
, this );
1666 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1667 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1668 item_y
+= PIXELS_PER_UNIT
+2;
1669 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1670 // Item should appear at bottom
1671 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
);
1675 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1676 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1678 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1679 wxGenericTreeItem
**item2
)
1681 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1683 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1686 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1687 const wxTreeItemId
& item2
)
1689 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1692 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1694 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1696 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1698 wxCHECK_RET( !s_treeBeingSorted
,
1699 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1701 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1702 if ( children
.Count() > 1 )
1706 s_treeBeingSorted
= this;
1707 children
.Sort(tree_ctrl_compare_func
);
1708 s_treeBeingSorted
= NULL
;
1710 //else: don't make the tree dirty as nothing changed
1713 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1715 return m_imageListNormal
;
1718 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1720 return m_imageListButtons
;
1723 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1725 return m_imageListState
;
1728 void wxGenericTreeCtrl::CalculateLineHeight()
1730 wxClientDC
dc(this);
1731 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1733 if ( m_imageListNormal
)
1735 // Calculate a m_lineHeight value from the normal Image sizes.
1736 // May be toggle off. Then wxGenericTreeCtrl will spread when
1737 // necessary (which might look ugly).
1738 int n
= m_imageListNormal
->GetImageCount();
1739 for (int i
= 0; i
< n
; i
++)
1741 int width
= 0, height
= 0;
1742 m_imageListNormal
->GetSize(i
, width
, height
);
1743 if (height
> m_lineHeight
) m_lineHeight
= height
;
1747 if (m_imageListButtons
)
1749 // Calculate a m_lineHeight value from the Button image sizes.
1750 // May be toggle off. Then wxGenericTreeCtrl will spread when
1751 // necessary (which might look ugly).
1752 int n
= m_imageListButtons
->GetImageCount();
1753 for (int i
= 0; i
< n
; i
++)
1755 int width
= 0, height
= 0;
1756 m_imageListButtons
->GetSize(i
, width
, height
);
1757 if (height
> m_lineHeight
) m_lineHeight
= height
;
1761 if (m_lineHeight
< 30)
1762 m_lineHeight
+= 2; // at least 2 pixels
1764 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1767 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1769 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1770 m_imageListNormal
= imageList
;
1771 m_ownsImageListNormal
= FALSE
;
1773 CalculateLineHeight();
1776 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1778 if (m_ownsImageListState
) delete m_imageListState
;
1779 m_imageListState
= imageList
;
1780 m_ownsImageListState
= FALSE
;
1783 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1785 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1786 m_imageListButtons
= imageList
;
1787 m_ownsImageListButtons
= FALSE
;
1789 CalculateLineHeight();
1792 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1794 SetImageList(imageList
);
1795 m_ownsImageListNormal
= TRUE
;
1798 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1800 SetStateImageList(imageList
);
1801 m_ownsImageListState
= TRUE
;
1804 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1806 SetButtonsImageList(imageList
);
1807 m_ownsImageListButtons
= TRUE
;
1810 // -----------------------------------------------------------------------------
1812 // -----------------------------------------------------------------------------
1814 void wxGenericTreeCtrl::AdjustMyScrollbars()
1819 m_anchor
->GetSize( x
, y
, this );
1820 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1821 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1822 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1823 int y_pos
= GetScrollPos( wxVERTICAL
);
1824 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1828 SetScrollbars( 0, 0, 0, 0 );
1832 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1834 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1835 return item
->GetHeight();
1837 return m_lineHeight
;
1840 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1842 // TODO implement "state" icon on items
1844 wxTreeItemAttr
*attr
= item
->GetAttributes();
1845 if ( attr
&& attr
->HasFont() )
1846 dc
.SetFont(attr
->GetFont());
1847 else if (item
->IsBold())
1848 dc
.SetFont(m_boldFont
);
1850 long text_w
= 0, text_h
= 0;
1851 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1853 int image_h
= 0, image_w
= 0;
1854 int image
= item
->GetCurrentImage();
1855 if ( image
!= NO_IMAGE
)
1857 if ( m_imageListNormal
)
1859 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1868 int total_h
= GetLineHeight(item
);
1870 if ( item
->IsSelected() )
1872 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1877 if ( attr
&& attr
->HasBackgroundColour() )
1878 colBg
= attr
->GetBackgroundColour();
1880 colBg
= m_backgroundColour
;
1881 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1884 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1886 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
1890 DoGetPosition(&x
, &y
);
1892 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
1896 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1898 // If it's selected, and there's an image, then we should
1899 // take care to leave the area under the image painted in the
1900 // background colour.
1901 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1902 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1906 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1907 item
->GetWidth()+2, total_h
-offset
);
1911 if ( image
!= NO_IMAGE
)
1913 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1914 m_imageListNormal
->Draw( image
, dc
,
1916 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1917 wxIMAGELIST_DRAW_TRANSPARENT
);
1918 dc
.DestroyClippingRegion();
1921 dc
.SetBackgroundMode(wxTRANSPARENT
);
1922 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1923 dc
.DrawText( item
->GetText(),
1924 (wxCoord
)(image_w
+ item
->GetX()),
1925 (wxCoord
)(item
->GetY() + extraH
));
1927 // restore normal font
1928 dc
.SetFont( m_normalFont
);
1931 // Now y stands for the top of the item, whereas it used to stand for middle !
1932 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1934 int x
= level
*m_indent
;
1935 if (!HasFlag(wxTR_HIDE_ROOT
))
1939 else if (level
== 0)
1941 // always expand hidden root
1943 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1944 int count
= children
.Count();
1950 PaintLevel(children
[n
], dc
, 1, y
);
1951 } while (++n
< count
);
1953 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
1955 // draw line down to last child
1956 origY
+= GetLineHeight(children
[0])>>1;
1957 oldY
+= GetLineHeight(children
[n
-1])>>1;
1958 dc
.DrawLine(3, origY
, 3, oldY
);
1964 item
->SetX(x
+m_spacing
);
1967 int h
= GetLineHeight(item
);
1969 int y_mid
= y_top
+ (h
>>1);
1972 int exposed_x
= dc
.LogicalToDeviceX(0);
1973 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1975 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1979 // don't draw rect outline if we already have the
1980 // background color under Mac
1981 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
1982 #endif // !__WXMAC__
1986 if ( item
->IsSelected() )
1988 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1992 wxTreeItemAttr
*attr
= item
->GetAttributes();
1993 if (attr
&& attr
->HasTextColour())
1994 colText
= attr
->GetTextColour();
1996 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2000 dc
.SetTextForeground(colText
);
2004 PaintItem(item
, dc
);
2006 if (HasFlag(wxTR_ROW_LINES
))
2008 // if the background colour is white, choose a
2009 // contrasting color for the lines
2010 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2011 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2012 dc
.DrawLine(0, y_top
, 10000, y_top
);
2013 dc
.DrawLine(0, y
, 10000, y
);
2016 // restore DC objects
2017 dc
.SetBrush(*wxWHITE_BRUSH
);
2018 dc
.SetPen(m_dottedPen
);
2019 dc
.SetTextForeground(*wxBLACK
);
2021 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2023 if (!HasFlag(wxTR_NO_LINES
))
2025 if (x
> (signed)m_indent
)
2026 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2027 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2028 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2029 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2032 if (m_imageListButtons
!= NULL
)
2034 // draw the image button here
2035 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2036 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2037 if (item
->IsSelected())
2038 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2039 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2040 int xx
= x
- (image_w
>>1);
2041 int yy
= y_mid
- (image_h
>>1);
2042 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2043 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2044 wxIMAGELIST_DRAW_TRANSPARENT
);
2045 dc
.DestroyClippingRegion();
2047 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2049 // draw the twisty button here
2052 dc
.SetBrush(*m_hilightBrush
);
2054 if (HasFlag(wxTR_AQUA_BUTTONS
))
2056 dc
.SetPen(*wxTRANSPARENT_PEN
);
2058 if (item
->IsExpanded())
2061 button
[0].y
= y_mid
-2;
2063 button
[1].y
= y_mid
-2;
2065 button
[2].y
= y_mid
+7;
2069 button
[0].y
= y_mid
-6;
2071 button
[1].y
= y_mid
+6;
2073 button
[2].y
= y_mid
;
2079 dc
.SetPen(*wxBLACK_PEN
);
2081 if (item
->IsExpanded())
2084 button
[0].y
= y_mid
-2;
2086 button
[1].y
= y_mid
-2;
2088 button
[2].y
= y_mid
+3;
2092 button
[0].y
= y_mid
-5;
2094 button
[1].y
= y_mid
+5;
2096 button
[2].y
= y_mid
;
2100 dc
.DrawPolygon(3, button
);
2102 dc
.SetPen(m_dottedPen
);
2104 else // if (HasFlag(wxTR_HAS_BUTTONS))
2106 // draw the plus sign here
2107 dc
.SetPen(*wxGREY_PEN
);
2108 dc
.SetBrush(*wxWHITE_BRUSH
);
2109 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2110 dc
.SetPen(*wxBLACK_PEN
);
2111 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2112 if (!item
->IsExpanded())
2113 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2114 dc
.SetPen(m_dottedPen
);
2117 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2119 // draw the horizontal line here
2121 if (x
> (signed)m_indent
)
2122 x_start
-= m_indent
;
2123 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2125 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2129 if (item
->IsExpanded())
2131 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2132 int count
= children
.Count();
2139 PaintLevel(children
[n
], dc
, level
, y
);
2140 } while (++n
< count
);
2142 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2144 // draw line down to last child
2145 oldY
+= GetLineHeight(children
[n
-1])>>1;
2146 if (HasButtons()) y_mid
+= 5;
2147 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2153 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2157 if ( item
->HasPlus() )
2159 // it's a folder, indicate it by a border
2164 // draw a line under the drop target because the item will be
2166 DrawLine(item
, TRUE
/* below */);
2169 SetCursor(wxCURSOR_BULLSEYE
);
2174 SetCursor(wxCURSOR_NO_ENTRY
);
2178 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2180 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2182 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2184 wxClientDC
dc(this);
2186 dc
.SetLogicalFunction(wxINVERT
);
2187 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2189 int w
= i
->GetWidth() + 2;
2190 int h
= GetLineHeight(i
) + 2;
2192 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2195 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2197 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2199 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2201 wxClientDC
dc(this);
2203 dc
.SetLogicalFunction(wxINVERT
);
2209 y
+= GetLineHeight(i
) - 1;
2212 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2215 // -----------------------------------------------------------------------------
2216 // wxWindows callbacks
2217 // -----------------------------------------------------------------------------
2219 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2227 dc
.SetFont( m_normalFont
);
2228 dc
.SetPen( m_dottedPen
);
2230 // this is now done dynamically
2231 //if(GetImageList() == NULL)
2232 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2235 PaintLevel( m_anchor
, dc
, 0, y
);
2238 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2247 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2256 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2258 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2259 te
.m_evtKey
= event
;
2260 te
.SetEventObject( this );
2261 if ( GetEventHandler()->ProcessEvent( te
) )
2263 // intercepted by the user code
2267 if ( (m_current
== 0) || (m_key_current
== 0) )
2273 // how should the selection work for this event?
2274 bool is_multiple
, extended_select
, unselect_others
;
2275 EventFlagsToSelType(GetWindowStyleFlag(),
2277 event
.ControlDown(),
2278 is_multiple
, extended_select
, unselect_others
);
2282 // * : Expand all/Collapse all
2283 // ' ' | return : activate
2284 // up : go up (not last children!)
2286 // left : go to parent
2287 // right : open if parent and go next
2288 // home : go to root
2289 // end : go to last item without opening parents
2290 switch (event
.KeyCode())
2294 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2302 if ( !IsExpanded(m_current
) )
2305 ExpandAll(m_current
);
2308 //else: fall through to Collapse() it
2312 if (IsExpanded(m_current
))
2314 Collapse(m_current
);
2321 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2322 event
.m_item
= (long) m_current
;
2323 event
.SetEventObject( this );
2324 GetEventHandler()->ProcessEvent( event
);
2328 // up goes to the previous sibling or to the last
2329 // of its children if it's expanded
2332 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2335 prev
= GetParent( m_key_current
);
2336 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2338 break; // don't go to root if it is hidden
2343 wxTreeItemId current
= m_key_current
;
2344 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2345 if (current
== GetFirstChild( prev
, cookie
))
2347 // otherwise we return to where we came from
2348 SelectItem( prev
, unselect_others
, extended_select
);
2349 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2350 EnsureVisible( prev
);
2357 while ( IsExpanded(prev
) && HasChildren(prev
) )
2359 wxTreeItemId child
= GetLastChild(prev
);
2366 SelectItem( prev
, unselect_others
, extended_select
);
2367 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2368 EnsureVisible( prev
);
2373 // left arrow goes to the parent
2376 wxTreeItemId prev
= GetParent( m_current
);
2377 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2379 // don't go to root if it is hidden
2380 prev
= GetPrevSibling( m_current
);
2384 EnsureVisible( prev
);
2385 SelectItem( prev
, unselect_others
, extended_select
);
2391 // this works the same as the down arrow except that we
2392 // also expand the item if it wasn't expanded yet
2398 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2401 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2402 SelectItem( child
, unselect_others
, extended_select
);
2403 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2404 EnsureVisible( child
);
2408 wxTreeItemId next
= GetNextSibling( m_key_current
);
2411 wxTreeItemId current
= m_key_current
;
2412 while (current
&& !next
)
2414 current
= GetParent( current
);
2415 if (current
) next
= GetNextSibling( current
);
2420 SelectItem( next
, unselect_others
, extended_select
);
2421 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2422 EnsureVisible( next
);
2428 // <End> selects the last visible tree item
2431 wxTreeItemId last
= GetRootItem();
2433 while ( last
.IsOk() && IsExpanded(last
) )
2435 wxTreeItemId lastChild
= GetLastChild(last
);
2437 // it may happen if the item was expanded but then all of
2438 // its children have been deleted - so IsExpanded() returned
2439 // TRUE, but GetLastChild() returned invalid item
2448 EnsureVisible( last
);
2449 SelectItem( last
, unselect_others
, extended_select
);
2454 // <Home> selects the root item
2457 wxTreeItemId prev
= GetRootItem();
2459 if (HasFlag(wxTR_HIDE_ROOT
))
2462 prev
= GetFirstChild(prev
, dummy
);
2465 EnsureVisible( prev
);
2466 SelectItem( prev
, unselect_others
, extended_select
);
2475 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2477 // JACS: removed wxYieldIfNeeded() because it can cause the window
2478 // to be deleted from under us if a close window event is pending
2483 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2484 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2485 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2486 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2487 if (flags
) return wxTreeItemId();
2489 if (m_anchor
== NULL
)
2491 flags
= wxTREE_HITTEST_NOWHERE
;
2492 return wxTreeItemId();
2495 wxClientDC
dc(this);
2497 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2498 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2499 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2505 flags
= wxTREE_HITTEST_NOWHERE
;
2506 return wxTreeItemId();
2511 // get the bounding rectangle of the item (or of its label only)
2512 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2514 bool WXUNUSED(textOnly
)) const
2516 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2518 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2521 GetViewStart(& startX
, & startY
);
2523 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2524 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2525 rect
.width
= i
->GetWidth();
2526 //rect.height = i->GetHeight();
2527 rect
.height
= GetLineHeight(i
);
2534 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2536 if (!item
.IsOk()) return;
2538 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2540 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2541 te
.m_item
= (long) m_currentEdit
;
2542 te
.SetEventObject( this );
2543 GetEventHandler()->ProcessEvent( te
);
2545 if (!te
.IsAllowed()) return;
2547 // We have to call this here because the label in
2548 // question might just have been added and no screen
2549 // update taken place.
2550 if (m_dirty
) wxYieldIfNeeded();
2552 wxString s
= m_currentEdit
->GetText();
2553 int x
= m_currentEdit
->GetX();
2554 int y
= m_currentEdit
->GetY();
2555 int w
= m_currentEdit
->GetWidth();
2556 int h
= m_currentEdit
->GetHeight();
2561 int image
= m_currentEdit
->GetCurrentImage();
2562 if ( image
!= NO_IMAGE
)
2564 if ( m_imageListNormal
)
2566 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2571 wxFAIL_MSG(_T("you must create an image list to use images!"));
2575 w
-= image_w
+ 4; // I don't know why +4 is needed
2577 wxClientDC
dc(this);
2579 x
= dc
.LogicalToDeviceX( x
);
2580 y
= dc
.LogicalToDeviceY( y
);
2582 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2592 void wxGenericTreeCtrl::OnRenameTimer()
2597 void wxGenericTreeCtrl::OnRenameAccept()
2599 // TODO if the validator fails this causes a crash
2600 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2601 le
.m_item
= (long) m_currentEdit
;
2602 le
.SetEventObject( this );
2603 le
.m_label
= m_renameRes
;
2604 GetEventHandler()->ProcessEvent( le
);
2606 if (!le
.IsAllowed()) return;
2608 SetItemText( m_currentEdit
, m_renameRes
);
2611 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2613 if ( !m_anchor
) return;
2615 // we process left mouse up event (enables in-place edit), right down
2616 // (pass to the user code), left dbl click (activate item) and
2617 // dragging/moving events for items drag-and-drop
2618 if ( !(event
.LeftDown() ||
2620 event
.RightDown() ||
2621 event
.LeftDClick() ||
2623 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2630 wxClientDC
dc(this);
2632 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2633 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2636 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2641 if ( event
.Dragging() && !m_isDragging
)
2643 if (m_dragCount
== 0)
2644 m_dragStart
= wxPoint(x
,y
);
2648 if (m_dragCount
!= 3)
2650 // wait until user drags a bit further...
2654 wxEventType command
= event
.RightIsDown()
2655 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2656 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2658 wxTreeEvent
nevent( command
, GetId() );
2659 nevent
.m_item
= (long) m_current
;
2660 nevent
.SetEventObject(this);
2662 // by default the dragging is not supported, the user code must
2663 // explicitly allow the event for it to take place
2666 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2668 // we're going to drag this item
2669 m_isDragging
= TRUE
;
2671 // remember the old cursor because we will change it while
2673 m_oldCursor
= m_cursor
;
2675 // in a single selection control, hide the selection temporarily
2676 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2678 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2680 if ( m_oldSelection
)
2682 m_oldSelection
->SetHilight(FALSE
);
2683 RefreshLine(m_oldSelection
);
2690 else if ( event
.Moving() )
2692 if ( item
!= m_dropTarget
)
2694 // unhighlight the previous drop target
2695 DrawDropEffect(m_dropTarget
);
2697 m_dropTarget
= item
;
2699 // highlight the current drop target if any
2700 DrawDropEffect(m_dropTarget
);
2705 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2707 // erase the highlighting
2708 DrawDropEffect(m_dropTarget
);
2710 if ( m_oldSelection
)
2712 m_oldSelection
->SetHilight(TRUE
);
2713 RefreshLine(m_oldSelection
);
2714 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2717 // generate the drag end event
2718 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2720 event
.m_item
= (long) item
;
2721 event
.m_pointDrag
= wxPoint(x
, y
);
2722 event
.SetEventObject(this);
2724 (void)GetEventHandler()->ProcessEvent(event
);
2726 m_isDragging
= FALSE
;
2727 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2731 SetCursor(m_oldCursor
);
2737 // here we process only the messages which happen on tree items
2741 if (item
== NULL
) return; /* we hit the blank area */
2743 if ( event
.RightDown() )
2745 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2746 nevent
.m_item
= (long) item
;
2747 CalcScrolledPosition(x
, y
,
2748 &nevent
.m_pointDrag
.x
,
2749 &nevent
.m_pointDrag
.y
);
2750 nevent
.SetEventObject(this);
2751 GetEventHandler()->ProcessEvent(nevent
);
2753 else if ( event
.LeftUp() )
2757 if ( (item
== m_current
) &&
2758 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2759 HasFlag(wxTR_EDIT_LABELS
) )
2761 if ( m_renameTimer
->IsRunning() )
2762 m_renameTimer
->Stop();
2764 m_renameTimer
->Start( 100, TRUE
);
2767 m_lastOnSame
= FALSE
;
2770 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2772 if ( event
.LeftDown() )
2774 m_lastOnSame
= item
== m_current
;
2777 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2779 // only toggle the item for a single click, double click on
2780 // the button doesn't do anything (it toggles the item twice)
2781 if ( event
.LeftDown() )
2786 // don't select the item if the button was clicked
2790 // how should the selection work for this event?
2791 bool is_multiple
, extended_select
, unselect_others
;
2792 EventFlagsToSelType(GetWindowStyleFlag(),
2794 event
.ControlDown(),
2795 is_multiple
, extended_select
, unselect_others
);
2797 SelectItem(item
, unselect_others
, extended_select
);
2799 // For some reason, Windows isn't recognizing a left double-click,
2800 // so we need to simulate it here. Allow 200 milliseconds for now.
2801 if ( event
.LeftDClick() )
2803 // double clicking should not start editing the item label
2804 m_renameTimer
->Stop();
2805 m_lastOnSame
= FALSE
;
2807 // send activate event first
2808 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2809 nevent
.m_item
= (long) item
;
2810 CalcScrolledPosition(x
, y
,
2811 &nevent
.m_pointDrag
.x
,
2812 &nevent
.m_pointDrag
.y
);
2813 nevent
.SetEventObject( this );
2814 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2816 // if the user code didn't process the activate event,
2817 // handle it ourselves by toggling the item when it is
2819 if ( item
->HasPlus() )
2829 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2831 /* after all changes have been done to the tree control,
2832 * we actually redraw the tree when everything is over */
2834 if (!m_dirty
) return;
2838 CalculatePositions();
2840 AdjustMyScrollbars();
2843 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2849 dc
.SetFont(m_boldFont
);
2851 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2854 // restore normal font
2855 dc
.SetFont( m_normalFont
);
2859 int image
= item
->GetCurrentImage();
2860 if ( image
!= NO_IMAGE
)
2862 if ( m_imageListNormal
)
2864 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2869 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2872 total_h
+= 2; // at least 2 pixels
2874 total_h
+= total_h
/10; // otherwise 10% extra spacing
2876 item
->SetHeight(total_h
);
2877 if (total_h
>m_lineHeight
)
2878 m_lineHeight
=total_h
;
2880 item
->SetWidth(image_w
+text_w
+2);
2883 // -----------------------------------------------------------------------------
2884 // for developper : y is now the top of the level
2885 // not the middle of it !
2886 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2888 int x
= level
*m_indent
;
2889 if (!HasFlag(wxTR_HIDE_ROOT
))
2893 else if (level
== 0)
2895 // a hidden root is not evaluated, but its
2896 // children are always calculated
2900 CalculateSize( item
, dc
);
2903 item
->SetX( x
+m_spacing
);
2905 y
+= GetLineHeight(item
);
2907 if ( !item
->IsExpanded() )
2909 // we don't need to calculate collapsed branches
2914 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2915 size_t n
, count
= children
.Count();
2917 for (n
= 0; n
< count
; ++n
)
2918 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2921 void wxGenericTreeCtrl::CalculatePositions()
2923 if ( !m_anchor
) return;
2925 wxClientDC
dc(this);
2928 dc
.SetFont( m_normalFont
);
2930 dc
.SetPen( m_dottedPen
);
2931 //if(GetImageList() == NULL)
2932 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2935 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2938 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2940 if (m_dirty
) return;
2942 wxClientDC
dc(this);
2947 GetClientSize( &cw
, &ch
);
2950 rect
.x
= dc
.LogicalToDeviceX( 0 );
2952 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2955 Refresh( TRUE
, &rect
);
2957 AdjustMyScrollbars();
2960 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2962 if (m_dirty
) return;
2964 wxClientDC
dc(this);
2969 GetClientSize( &cw
, &ch
);
2972 rect
.x
= dc
.LogicalToDeviceX( 0 );
2973 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2975 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2977 Refresh( TRUE
, &rect
);
2980 void wxGenericTreeCtrl::RefreshSelected()
2982 // TODO: this is awfully inefficient, we should keep the list of all
2983 // selected items internally, should be much faster
2985 RefreshSelectedUnder(m_anchor
);
2988 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2990 if ( item
->IsSelected() )
2993 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2994 size_t count
= children
.GetCount();
2995 for ( size_t n
= 0; n
< count
; n
++ )
2997 RefreshSelectedUnder(children
[n
]);
3001 // ----------------------------------------------------------------------------
3002 // changing colours: we need to refresh the tree control
3003 // ----------------------------------------------------------------------------
3005 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3007 if ( !wxWindow::SetBackgroundColour(colour
) )
3015 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3017 if ( !wxWindow::SetForegroundColour(colour
) )
3025 #endif // wxUSE_TREECTRL