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 // ----------------------------------------------------------------------------
63 static const char *aqua_arrow_right
[] = {
64 /* columns rows colors chars-per-pixel */
85 static const char *aqua_arrow_down
[] = {
86 /* columns rows colors chars-per-pixel */
106 // -----------------------------------------------------------------------------
108 // -----------------------------------------------------------------------------
110 // timer used for enabling in-place edit
111 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
114 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
119 wxGenericTreeCtrl
*m_owner
;
122 // control used for in-place edit
123 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
126 wxTreeTextCtrl( wxWindow
*parent
,
130 wxGenericTreeCtrl
*owner
,
131 const wxString
&value
= wxEmptyString
,
132 const wxPoint
&pos
= wxDefaultPosition
,
133 const wxSize
&size
= wxDefaultSize
,
134 int style
= wxSIMPLE_BORDER
,
135 const wxValidator
& validator
= wxDefaultValidator
,
136 const wxString
&name
= wxTextCtrlNameStr
);
138 void OnChar( wxKeyEvent
&event
);
139 void OnKeyUp( wxKeyEvent
&event
);
140 void OnKillFocus( wxFocusEvent
&event
);
145 wxGenericTreeCtrl
*m_owner
;
146 wxString m_startValue
;
149 DECLARE_EVENT_TABLE()
153 class WXDLLEXPORT wxGenericTreeItem
157 wxGenericTreeItem() { m_data
= NULL
; }
158 wxGenericTreeItem( wxGenericTreeItem
*parent
,
159 const wxString
& text
,
162 wxTreeItemData
*data
);
164 ~wxGenericTreeItem();
167 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
169 const wxString
& GetText() const { return m_text
; }
170 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
171 { return m_images
[which
]; }
172 wxTreeItemData
*GetData() const { return m_data
; }
174 // returns the current image for the item (depending on its
175 // selected/expanded/whatever state)
176 int GetCurrentImage() const;
178 void SetText( const wxString
&text
);
179 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
180 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
182 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
184 void SetBold(bool bold
) { m_isBold
= bold
; }
186 int GetX() const { return m_x
; }
187 int GetY() const { return m_y
; }
189 void SetX(int x
) { m_x
= x
; }
190 void SetY(int y
) { m_y
= y
; }
192 int GetHeight() const { return m_height
; }
193 int GetWidth() const { return m_width
; }
195 void SetHeight(int h
) { m_height
= h
; }
196 void SetWidth(int w
) { m_width
= w
; }
198 wxGenericTreeItem
*GetParent() const { return m_parent
; }
201 // deletes all children notifying the treectrl about it if !NULL
203 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
205 // get count of all children (and grand children if 'recursively')
206 size_t GetChildrenCount(bool recursively
= TRUE
) const;
208 void Insert(wxGenericTreeItem
*child
, size_t index
)
209 { m_children
.Insert(child
, index
); }
211 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
213 // return the item at given position (or NULL if no item), onButton is
214 // TRUE if the point belongs to the item's button, otherwise it lies
215 // on the button's label
216 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
217 const wxGenericTreeCtrl
*,
221 void Expand() { m_isCollapsed
= FALSE
; }
222 void Collapse() { m_isCollapsed
= TRUE
; }
224 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
227 bool HasChildren() const { return !m_children
.IsEmpty(); }
228 bool IsSelected() const { return m_hasHilight
!= 0; }
229 bool IsExpanded() const { return !m_isCollapsed
; }
230 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
231 bool IsBold() const { return m_isBold
!= 0; }
234 // get them - may be NULL
235 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
236 // get them ensuring that the pointer is not NULL
237 wxTreeItemAttr
& Attr()
241 m_attr
= new wxTreeItemAttr
;
247 void SetAttributes(wxTreeItemAttr
*attr
)
249 if ( m_ownsAttr
) delete m_attr
;
253 // set them and delete when done
254 void AssignAttributes(wxTreeItemAttr
*attr
)
261 // since there can be very many of these, we save size by chosing
262 // the smallest representation for the elements and by ordering
263 // the members to avoid padding.
264 wxString m_text
; // label to be rendered for item
266 wxTreeItemData
*m_data
; // user-provided data
268 wxArrayGenericTreeItems m_children
; // list of children
269 wxGenericTreeItem
*m_parent
; // parent of this item
271 wxTreeItemAttr
*m_attr
; // attributes???
273 // tree ctrl images for the normal, selected, expanded and
274 // expanded+selected states
275 short m_images
[wxTreeItemIcon_Max
];
277 wxCoord m_x
; // (virtual) offset from top
278 short m_y
; // (virtual) offset from left
279 short m_width
; // width of this item
280 unsigned char m_height
; // height of this item
282 // use bitfields to save size
283 int m_isCollapsed
:1;
284 int m_hasHilight
:1; // same as focused
285 int m_hasPlus
:1; // used for item which doesn't have
286 // children but has a [+] button
287 int m_isBold
:1; // render the label in bold font
288 int m_ownsAttr
:1; // delete attribute when done
291 // =============================================================================
293 // =============================================================================
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 // translate the key or mouse event flags to the type of selection we're
301 static void EventFlagsToSelType(long style
,
305 bool &extended_select
,
306 bool &unselect_others
)
308 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
309 extended_select
= shiftDown
&& is_multiple
;
310 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
313 // -----------------------------------------------------------------------------
314 // wxTreeRenameTimer (internal)
315 // -----------------------------------------------------------------------------
317 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
322 void wxTreeRenameTimer::Notify()
324 m_owner
->OnRenameTimer();
327 //-----------------------------------------------------------------------------
328 // wxTreeTextCtrl (internal)
329 //-----------------------------------------------------------------------------
331 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
332 EVT_CHAR (wxTreeTextCtrl::OnChar
)
333 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
334 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
337 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
341 wxGenericTreeCtrl
*owner
,
342 const wxString
&value
,
346 const wxValidator
& validator
,
347 const wxString
&name
)
348 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
354 (*m_res
) = wxEmptyString
;
355 m_startValue
= value
;
359 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
361 if (event
.m_keyCode
== WXK_RETURN
)
364 (*m_res
) = GetValue();
366 if ((*m_res
) != m_startValue
)
367 m_owner
->OnRenameAccept();
369 if (!wxPendingDelete
.Member(this))
370 wxPendingDelete
.Append(this);
373 m_owner
->SetFocus(); // This doesn't work. TODO.
377 if (event
.m_keyCode
== WXK_ESCAPE
)
382 if (!wxPendingDelete
.Member(this))
383 wxPendingDelete
.Append(this);
386 m_owner
->SetFocus(); // This doesn't work. TODO.
393 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
401 // auto-grow the textctrl:
402 wxSize parentSize
= m_owner
->GetSize();
403 wxPoint myPos
= GetPosition();
404 wxSize mySize
= GetSize();
406 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
407 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
408 if (mySize
.x
> sx
) sx
= mySize
.x
;
414 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
422 if (!wxPendingDelete
.Member(this))
423 wxPendingDelete
.Append(this);
426 (*m_res
) = GetValue();
428 if ((*m_res
) != m_startValue
)
429 m_owner
->OnRenameAccept();
432 // -----------------------------------------------------------------------------
434 // -----------------------------------------------------------------------------
436 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
437 const wxString
& text
,
438 int image
, int selImage
,
439 wxTreeItemData
*data
)
442 m_images
[wxTreeItemIcon_Normal
] = image
;
443 m_images
[wxTreeItemIcon_Selected
] = selImage
;
444 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
445 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
450 m_isCollapsed
= TRUE
;
451 m_hasHilight
= FALSE
;
457 m_attr
= (wxTreeItemAttr
*)NULL
;
460 // We don't know the height here yet.
465 wxGenericTreeItem::~wxGenericTreeItem()
469 if (m_ownsAttr
) delete m_attr
;
471 wxASSERT_MSG( m_children
.IsEmpty(),
472 wxT("please call DeleteChildren() before deleting the item") );
475 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
477 size_t count
= m_children
.Count();
478 for ( size_t n
= 0; n
< count
; n
++ )
480 wxGenericTreeItem
*child
= m_children
[n
];
482 tree
->SendDeleteEvent(child
);
484 child
->DeleteChildren(tree
);
491 void wxGenericTreeItem::SetText( const wxString
&text
)
496 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
498 size_t count
= m_children
.Count();
502 size_t total
= count
;
503 for (size_t n
= 0; n
< count
; ++n
)
505 total
+= m_children
[n
]->GetChildrenCount();
511 void wxGenericTreeItem::GetSize( int &x
, int &y
,
512 const wxGenericTreeCtrl
*theButton
)
514 int bottomY
=m_y
+theButton
->GetLineHeight(this);
515 if ( y
< bottomY
) y
= bottomY
;
516 int width
= m_x
+ m_width
;
517 if ( x
< width
) x
= width
;
521 size_t count
= m_children
.Count();
522 for ( size_t n
= 0; n
< count
; ++n
)
524 m_children
[n
]->GetSize( x
, y
, theButton
);
529 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
530 const wxGenericTreeCtrl
*theCtrl
,
534 // for a hidden root node, don't evaluate it, but do evaluate children
535 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
538 int h
= theCtrl
->GetLineHeight(this);
539 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
541 int y_mid
= m_y
+ h
/2;
542 if (point
.y
< y_mid
)
543 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
545 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
547 // 5 is the size of the plus sign
548 int xCross
= m_x
- theCtrl
->GetSpacing();
549 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
550 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
551 HasPlus() && theCtrl
->HasButtons() )
553 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
557 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
562 // assuming every image (normal and selected) has the same size!
563 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
564 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
567 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
568 flags
|= wxTREE_HITTEST_ONITEMICON
;
570 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
576 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
577 if (point
.x
> m_x
+m_width
)
578 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
583 // if children are expanded, fall through to evaluate them
584 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
588 size_t count
= m_children
.Count();
589 for ( size_t n
= 0; n
< count
; n
++ )
591 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
599 return (wxGenericTreeItem
*) NULL
;
602 int wxGenericTreeItem::GetCurrentImage() const
604 int image
= NO_IMAGE
;
609 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
612 if ( image
== NO_IMAGE
)
614 // we usually fall back to the normal item, but try just the
615 // expanded one (and not selected) first in this case
616 image
= GetImage(wxTreeItemIcon_Expanded
);
622 image
= GetImage(wxTreeItemIcon_Selected
);
625 // maybe it doesn't have the specific image we want,
626 // try the default one instead
627 if ( image
== NO_IMAGE
) image
= GetImage();
632 // -----------------------------------------------------------------------------
633 // wxGenericTreeCtrl implementation
634 // -----------------------------------------------------------------------------
636 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
638 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
639 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
640 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
641 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
642 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
643 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
644 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
647 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
649 * wxTreeCtrl has to be a real class or we have problems with
650 * the run-time information.
653 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
656 // -----------------------------------------------------------------------------
657 // construction/destruction
658 // -----------------------------------------------------------------------------
660 void wxGenericTreeCtrl::Init()
662 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
670 m_hilightBrush
= new wxBrush
672 wxSystemSettings::GetColour
674 wxSYS_COLOUR_HIGHLIGHT
679 m_hilightUnfocusedBrush
= new wxBrush
681 wxSystemSettings::GetColour
683 wxSYS_COLOUR_BTNSHADOW
688 m_imageListNormal
= m_imageListButtons
=
689 m_imageListState
= (wxImageList
*) NULL
;
690 m_ownsImageListNormal
= m_ownsImageListButtons
=
691 m_ownsImageListState
= FALSE
;
694 m_isDragging
= FALSE
;
695 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
697 m_renameTimer
= new wxTreeRenameTimer( this );
698 m_lastOnSame
= FALSE
;
700 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
701 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
702 m_normalFont
.GetFamily(),
703 m_normalFont
.GetStyle(),
705 m_normalFont
.GetUnderlined());
708 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
713 const wxValidator
&validator
,
714 const wxString
& name
)
718 wxGetOsVersion( &major
, &minor
);
720 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
721 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
722 style
&= ~wxTR_LINES_AT_ROOT
;
723 style
|= wxTR_NO_LINES
;
725 style
|= wxTR_ROW_LINES
;
727 style
|= wxTR_AQUA_BUTTONS
;
730 if (style
& wxTR_AQUA_BUTTONS
)
732 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
733 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
741 wxScrolledWindow::Create( parent
, id
, pos
, size
,
742 style
|wxHSCROLL
|wxVSCROLL
, name
);
744 // If the tree display has no buttons, but does have
745 // connecting lines, we can use a narrower layout.
746 // It may not be a good idea to force this...
747 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
754 SetValidator( validator
);
757 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
758 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
760 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
761 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
766 wxGenericTreeCtrl::~wxGenericTreeCtrl()
768 delete m_hilightBrush
;
769 delete m_hilightUnfocusedBrush
;
771 if (m_arrowRight
) delete m_arrowRight
;
772 if (m_arrowDown
) delete m_arrowDown
;
776 delete m_renameTimer
;
777 if (m_ownsImageListNormal
) delete m_imageListNormal
;
778 if (m_ownsImageListState
) delete m_imageListState
;
779 if (m_ownsImageListButtons
) delete m_imageListButtons
;
782 // -----------------------------------------------------------------------------
784 // -----------------------------------------------------------------------------
786 size_t wxGenericTreeCtrl::GetCount() const
788 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
791 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
793 m_indent
= (unsigned short) indent
;
797 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
799 m_spacing
= (unsigned short) spacing
;
803 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
805 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
807 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
810 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
812 // right now, just sets the styles. Eventually, we may
813 // want to update the inherited styles, but right now
814 // none of the parents has updatable styles
815 m_windowStyle
= styles
;
819 // -----------------------------------------------------------------------------
820 // functions to work with tree items
821 // -----------------------------------------------------------------------------
823 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
825 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
827 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
830 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
831 wxTreeItemIcon which
) const
833 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
835 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
838 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
840 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
842 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
845 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
847 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
850 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
851 pItem
->SetText(text
);
852 CalculateSize(pItem
, dc
);
856 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
858 wxTreeItemIcon which
)
860 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
862 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
863 pItem
->SetImage(image
, which
);
866 CalculateSize(pItem
, dc
);
870 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
872 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
874 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
877 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
879 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
881 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
882 pItem
->SetHasPlus(has
);
886 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
888 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
890 // avoid redrawing the tree if no real change
891 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
892 if ( pItem
->IsBold() != bold
)
894 pItem
->SetBold(bold
);
899 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
902 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
904 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
905 pItem
->Attr().SetTextColour(col
);
909 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
912 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
914 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
915 pItem
->Attr().SetBackgroundColour(col
);
919 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
921 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
923 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
924 pItem
->Attr().SetFont(font
);
928 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
930 wxScrolledWindow::SetFont(font
);
932 m_normalFont
= font
;
933 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
934 m_normalFont
.GetFamily(),
935 m_normalFont
.GetStyle(),
937 m_normalFont
.GetUnderlined());
943 // -----------------------------------------------------------------------------
944 // item status inquiries
945 // -----------------------------------------------------------------------------
947 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
949 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
951 // An item is only visible if it's not a descendant of a collapsed item
952 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
953 wxGenericTreeItem
* parent
= pItem
->GetParent();
956 if (!parent
->IsExpanded())
958 parent
= parent
->GetParent();
962 GetViewStart(& startX
, & startY
);
964 wxSize clientSize
= GetClientSize();
967 if (!GetBoundingRect(item
, rect
))
969 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
971 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
973 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
979 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
981 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
983 // consider that the item does have children if it has the "+" button: it
984 // might not have them (if it had never been expanded yet) but then it
985 // could have them as well and it's better to err on this side rather than
986 // disabling some operations which are restricted to the items with
987 // children for an item which does have them
988 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
991 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
993 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
995 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
998 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1000 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1002 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1005 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1007 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1009 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1012 // -----------------------------------------------------------------------------
1014 // -----------------------------------------------------------------------------
1016 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1018 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1020 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1023 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1025 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1028 return GetNextChild(item
, cookie
);
1031 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1033 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1035 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1036 if ( (size_t)cookie
< children
.Count() )
1038 return children
.Item((size_t)cookie
++);
1042 // there are no more of them
1043 return wxTreeItemId();
1047 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1049 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1051 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1052 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1055 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1057 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1059 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1060 wxGenericTreeItem
*parent
= i
->GetParent();
1061 if ( parent
== NULL
)
1063 // root item doesn't have any siblings
1064 return wxTreeItemId();
1067 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1068 int index
= siblings
.Index(i
);
1069 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1071 size_t n
= (size_t)(index
+ 1);
1072 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1075 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1077 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1079 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1080 wxGenericTreeItem
*parent
= i
->GetParent();
1081 if ( parent
== NULL
)
1083 // root item doesn't have any siblings
1084 return wxTreeItemId();
1087 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1088 int index
= siblings
.Index(i
);
1089 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1091 return index
== 0 ? wxTreeItemId()
1092 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1095 // Only for internal use right now, but should probably be public
1096 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1098 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1100 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1102 // First see if there are any children.
1103 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1104 if (children
.GetCount() > 0)
1106 return children
.Item(0);
1110 // Try a sibling of this or ancestor instead
1111 wxTreeItemId p
= item
;
1112 wxTreeItemId toFind
;
1115 toFind
= GetNextSibling(p
);
1117 } while (p
.IsOk() && !toFind
.IsOk());
1122 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1124 wxTreeItemId id
= GetRootItem();
1133 } while (id
.IsOk());
1135 return wxTreeItemId();
1138 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1140 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1142 wxTreeItemId id
= item
;
1145 while (id
= GetNext(id
), id
.IsOk())
1151 return wxTreeItemId();
1154 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1156 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1158 wxFAIL_MSG(wxT("not implemented"));
1160 return wxTreeItemId();
1163 // -----------------------------------------------------------------------------
1165 // -----------------------------------------------------------------------------
1167 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1169 const wxString
& text
,
1170 int image
, int selImage
,
1171 wxTreeItemData
*data
)
1173 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1176 // should we give a warning here?
1177 return AddRoot(text
, image
, selImage
, data
);
1180 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1182 wxGenericTreeItem
*item
=
1183 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1187 data
->m_pItem
= (long) item
;
1190 parent
->Insert( item
, previous
);
1195 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1196 int image
, int selImage
,
1197 wxTreeItemData
*data
)
1199 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1201 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1203 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1204 image
, selImage
, data
);
1207 data
->m_pItem
= (long) m_anchor
;
1210 if (HasFlag(wxTR_HIDE_ROOT
))
1212 // if root is hidden, make sure we can navigate
1214 m_anchor
->SetHasPlus();
1216 CalculatePositions();
1219 if (!HasFlag(wxTR_MULTIPLE
))
1221 m_current
= m_key_current
= m_anchor
;
1222 m_current
->SetHilight( TRUE
);
1228 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1229 const wxString
& text
,
1230 int image
, int selImage
,
1231 wxTreeItemData
*data
)
1233 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1236 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1237 const wxTreeItemId
& idPrevious
,
1238 const wxString
& text
,
1239 int image
, int selImage
,
1240 wxTreeItemData
*data
)
1242 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1245 // should we give a warning here?
1246 return AddRoot(text
, image
, selImage
, data
);
1250 if (idPrevious
.IsOk())
1252 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1253 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1254 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1257 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1260 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1262 const wxString
& text
,
1263 int image
, int selImage
,
1264 wxTreeItemData
*data
)
1266 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1269 // should we give a warning here?
1270 return AddRoot(text
, image
, selImage
, data
);
1273 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1276 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1277 const wxString
& text
,
1278 int image
, int selImage
,
1279 wxTreeItemData
*data
)
1281 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1284 // should we give a warning here?
1285 return AddRoot(text
, image
, selImage
, data
);
1288 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1289 image
, selImage
, data
);
1292 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1294 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1295 event
.m_item
= (long) item
;
1296 event
.SetEventObject( this );
1297 ProcessEvent( event
);
1300 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1302 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1304 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1305 item
->DeleteChildren(this);
1308 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1310 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1312 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1314 // don't stay with invalid m_key_current or we will crash in
1315 // the next call to OnChar()
1316 bool changeKeyCurrent
= FALSE
;
1317 wxGenericTreeItem
*itemKey
= m_key_current
;
1320 if ( itemKey
== item
)
1322 // m_key_current is a descendant of the item being deleted
1323 changeKeyCurrent
= TRUE
;
1326 itemKey
= itemKey
->GetParent();
1329 wxGenericTreeItem
*parent
= item
->GetParent();
1332 parent
->GetChildren().Remove( item
); // remove by value
1335 if ( changeKeyCurrent
)
1337 // may be NULL or not
1338 m_key_current
= parent
;
1341 item
->DeleteChildren(this);
1342 SendDeleteEvent(item
);
1346 void wxGenericTreeCtrl::DeleteAllItems()
1352 m_anchor
->DeleteChildren(this);
1359 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1361 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1363 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1364 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1365 _T("can't expand hidden root") );
1367 if ( !item
->HasPlus() )
1370 if ( item
->IsExpanded() )
1373 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1374 event
.m_item
= (long) item
;
1375 event
.SetEventObject( this );
1377 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1379 // cancelled by program
1384 CalculatePositions();
1386 RefreshSubtree(item
);
1388 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1389 ProcessEvent( event
);
1392 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1395 if ( IsExpanded(item
) )
1398 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1399 while ( child
.IsOk() )
1403 child
= GetNextChild(item
, cookie
);
1408 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1410 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1411 _T("can't collapse hidden root") );
1413 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1415 if ( !item
->IsExpanded() )
1418 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1419 event
.m_item
= (long) item
;
1420 event
.SetEventObject( this );
1421 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1423 // cancelled by program
1429 #if 0 // TODO why should items be collapsed recursively?
1430 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1431 size_t count
= children
.Count();
1432 for ( size_t n
= 0; n
< count
; n
++ )
1434 Collapse(children
[n
]);
1438 CalculatePositions();
1440 RefreshSubtree(item
);
1442 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1443 ProcessEvent( event
);
1446 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1449 DeleteChildren(item
);
1452 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1454 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1456 if (item
->IsExpanded())
1462 void wxGenericTreeCtrl::Unselect()
1466 m_current
->SetHilight( FALSE
);
1467 RefreshLine( m_current
);
1471 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1473 if (item
->IsSelected())
1475 item
->SetHilight(FALSE
);
1479 if (item
->HasChildren())
1481 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1482 size_t count
= children
.Count();
1483 for ( size_t n
= 0; n
< count
; ++n
)
1485 UnselectAllChildren(children
[n
]);
1490 void wxGenericTreeCtrl::UnselectAll()
1492 wxTreeItemId rootItem
= GetRootItem();
1494 // the tree might not have the root item at all
1497 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1501 // Recursive function !
1502 // To stop we must have crt_item<last_item
1504 // Tag all next children, when no more children,
1505 // Move to parent (not to tag)
1506 // Keep going... if we found last_item, we stop.
1507 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1509 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1511 if (parent
== NULL
) // This is root item
1512 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1514 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1515 int index
= children
.Index(crt_item
);
1516 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1518 size_t count
= children
.Count();
1519 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1521 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1524 return TagNextChildren(parent
, last_item
, select
);
1527 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1529 crt_item
->SetHilight(select
);
1530 RefreshLine(crt_item
);
1532 if (crt_item
==last_item
)
1535 if (crt_item
->HasChildren())
1537 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1538 size_t count
= children
.Count();
1539 for ( size_t n
= 0; n
< count
; ++n
)
1541 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1549 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1551 // item2 is not necessary after item1
1552 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1554 // choice first' and 'last' between item1 and item2
1555 if (item1
->GetY()<item2
->GetY())
1566 bool select
= m_current
->IsSelected();
1568 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1571 TagNextChildren(first
,last
,select
);
1574 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1575 bool unselect_others
,
1576 bool extended_select
)
1578 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1580 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1581 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1583 //wxCHECK_RET( ( (!unselect_others) && is_single),
1584 // wxT("this is a single selection tree") );
1586 // to keep going anyhow !!!
1589 if (item
->IsSelected())
1590 return; // nothing to do
1591 unselect_others
= TRUE
;
1592 extended_select
= FALSE
;
1594 else if ( unselect_others
&& item
->IsSelected() )
1596 // selection change if there is more than one item currently selected
1597 wxArrayTreeItemIds selected_items
;
1598 if ( GetSelections(selected_items
) == 1 )
1602 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1603 event
.m_item
= (long) item
;
1604 event
.m_itemOld
= (long) m_current
;
1605 event
.SetEventObject( this );
1606 // TODO : Here we don't send any selection mode yet !
1608 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1611 wxTreeItemId parent
= GetParent( itemId
);
1612 while (parent
.IsOk())
1614 if (!IsExpanded(parent
))
1617 parent
= GetParent( parent
);
1620 EnsureVisible( itemId
);
1623 if (unselect_others
)
1625 if (is_single
) Unselect(); // to speed up thing
1630 if (extended_select
)
1634 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1637 // don't change the mark (m_current)
1638 SelectItemRange(m_current
, item
);
1642 bool select
=TRUE
; // the default
1644 // Check if we need to toggle hilight (ctrl mode)
1645 if (!unselect_others
)
1646 select
=!item
->IsSelected();
1648 m_current
= m_key_current
= item
;
1649 m_current
->SetHilight(select
);
1650 RefreshLine( m_current
);
1653 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1654 GetEventHandler()->ProcessEvent( event
);
1657 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1658 wxArrayTreeItemIds
&array
) const
1660 if ( item
->IsSelected() )
1661 array
.Add(wxTreeItemId(item
));
1663 if ( item
->HasChildren() )
1665 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1666 size_t count
= children
.GetCount();
1667 for ( size_t n
= 0; n
< count
; ++n
)
1668 FillArray(children
[n
], array
);
1672 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1675 wxTreeItemId idRoot
= GetRootItem();
1676 if ( idRoot
.IsOk() )
1678 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1680 //else: the tree is empty, so no selections
1682 return array
.Count();
1685 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1687 if (!item
.IsOk()) return;
1689 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1691 // first expand all parent branches
1692 wxGenericTreeItem
*parent
= gitem
->GetParent();
1694 if ( HasFlag(wxTR_HIDE_ROOT
) )
1696 while ( parent
!= m_anchor
)
1699 parent
= parent
->GetParent();
1707 parent
= parent
->GetParent();
1711 //if (parent) CalculatePositions();
1716 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1718 if (!item
.IsOk()) return;
1720 // We have to call this here because the label in
1721 // question might just have been added and no screen
1722 // update taken place.
1723 if (m_dirty
) wxYieldIfNeeded();
1725 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1727 // now scroll to the item
1728 int item_y
= gitem
->GetY();
1732 GetViewStart( &start_x
, &start_y
);
1733 start_y
*= PIXELS_PER_UNIT
;
1737 GetClientSize( &client_w
, &client_h
);
1739 if (item_y
< start_y
+3)
1744 m_anchor
->GetSize( x
, y
, this );
1745 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1746 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1747 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1748 // Item should appear at top
1749 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1751 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1756 m_anchor
->GetSize( x
, y
, this );
1757 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1758 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1759 item_y
+= PIXELS_PER_UNIT
+2;
1760 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1761 // Item should appear at bottom
1762 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
);
1766 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1767 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1769 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1770 wxGenericTreeItem
**item2
)
1772 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1774 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1777 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1778 const wxTreeItemId
& item2
)
1780 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1783 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1785 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1787 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1789 wxCHECK_RET( !s_treeBeingSorted
,
1790 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1792 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1793 if ( children
.Count() > 1 )
1797 s_treeBeingSorted
= this;
1798 children
.Sort(tree_ctrl_compare_func
);
1799 s_treeBeingSorted
= NULL
;
1801 //else: don't make the tree dirty as nothing changed
1804 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1806 return m_imageListNormal
;
1809 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1811 return m_imageListButtons
;
1814 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1816 return m_imageListState
;
1819 void wxGenericTreeCtrl::CalculateLineHeight()
1821 wxClientDC
dc(this);
1822 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1824 if ( m_imageListNormal
)
1826 // Calculate a m_lineHeight value from the normal Image sizes.
1827 // May be toggle off. Then wxGenericTreeCtrl will spread when
1828 // necessary (which might look ugly).
1829 int n
= m_imageListNormal
->GetImageCount();
1830 for (int i
= 0; i
< n
; i
++)
1832 int width
= 0, height
= 0;
1833 m_imageListNormal
->GetSize(i
, width
, height
);
1834 if (height
> m_lineHeight
) m_lineHeight
= height
;
1838 if (m_imageListButtons
)
1840 // Calculate a m_lineHeight value from the Button image sizes.
1841 // May be toggle off. Then wxGenericTreeCtrl will spread when
1842 // necessary (which might look ugly).
1843 int n
= m_imageListButtons
->GetImageCount();
1844 for (int i
= 0; i
< n
; i
++)
1846 int width
= 0, height
= 0;
1847 m_imageListButtons
->GetSize(i
, width
, height
);
1848 if (height
> m_lineHeight
) m_lineHeight
= height
;
1852 if (m_lineHeight
< 30)
1853 m_lineHeight
+= 2; // at least 2 pixels
1855 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1858 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1860 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1861 m_imageListNormal
= imageList
;
1862 m_ownsImageListNormal
= FALSE
;
1864 // Don't do any drawing if we're setting the list to NULL,
1865 // since we may be in the process of deleting the tree control.
1867 CalculateLineHeight();
1870 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1872 if (m_ownsImageListState
) delete m_imageListState
;
1873 m_imageListState
= imageList
;
1874 m_ownsImageListState
= FALSE
;
1877 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1879 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1880 m_imageListButtons
= imageList
;
1881 m_ownsImageListButtons
= FALSE
;
1883 CalculateLineHeight();
1886 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1888 SetImageList(imageList
);
1889 m_ownsImageListNormal
= TRUE
;
1892 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1894 SetStateImageList(imageList
);
1895 m_ownsImageListState
= TRUE
;
1898 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1900 SetButtonsImageList(imageList
);
1901 m_ownsImageListButtons
= TRUE
;
1904 // -----------------------------------------------------------------------------
1906 // -----------------------------------------------------------------------------
1908 void wxGenericTreeCtrl::AdjustMyScrollbars()
1913 m_anchor
->GetSize( x
, y
, this );
1914 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1915 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1916 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1917 int y_pos
= GetScrollPos( wxVERTICAL
);
1918 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1922 SetScrollbars( 0, 0, 0, 0 );
1926 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1928 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1929 return item
->GetHeight();
1931 return m_lineHeight
;
1934 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1936 // TODO implement "state" icon on items
1938 wxTreeItemAttr
*attr
= item
->GetAttributes();
1939 if ( attr
&& attr
->HasFont() )
1940 dc
.SetFont(attr
->GetFont());
1941 else if (item
->IsBold())
1942 dc
.SetFont(m_boldFont
);
1944 long text_w
= 0, text_h
= 0;
1945 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1947 int image_h
= 0, image_w
= 0;
1948 int image
= item
->GetCurrentImage();
1949 if ( image
!= NO_IMAGE
)
1951 if ( m_imageListNormal
)
1953 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1962 int total_h
= GetLineHeight(item
);
1964 if ( item
->IsSelected() )
1966 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1971 if ( attr
&& attr
->HasBackgroundColour() )
1972 colBg
= attr
->GetBackgroundColour();
1974 colBg
= m_backgroundColour
;
1975 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1978 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1980 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
1984 DoGetPosition(&x
, &y
);
1986 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
1990 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1992 // If it's selected, and there's an image, then we should
1993 // take care to leave the area under the image painted in the
1994 // background colour.
1995 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1996 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2000 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2001 item
->GetWidth()+2, total_h
-offset
);
2005 if ( image
!= NO_IMAGE
)
2007 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2008 m_imageListNormal
->Draw( image
, dc
,
2010 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2011 wxIMAGELIST_DRAW_TRANSPARENT
);
2012 dc
.DestroyClippingRegion();
2015 dc
.SetBackgroundMode(wxTRANSPARENT
);
2016 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2017 dc
.DrawText( item
->GetText(),
2018 (wxCoord
)(image_w
+ item
->GetX()),
2019 (wxCoord
)(item
->GetY() + extraH
));
2021 // restore normal font
2022 dc
.SetFont( m_normalFont
);
2025 // Now y stands for the top of the item, whereas it used to stand for middle !
2026 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2028 int x
= level
*m_indent
;
2029 if (!HasFlag(wxTR_HIDE_ROOT
))
2033 else if (level
== 0)
2035 // always expand hidden root
2037 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2038 int count
= children
.Count();
2044 PaintLevel(children
[n
], dc
, 1, y
);
2045 } while (++n
< count
);
2047 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2049 // draw line down to last child
2050 origY
+= GetLineHeight(children
[0])>>1;
2051 oldY
+= GetLineHeight(children
[n
-1])>>1;
2052 dc
.DrawLine(3, origY
, 3, oldY
);
2058 item
->SetX(x
+m_spacing
);
2061 int h
= GetLineHeight(item
);
2063 int y_mid
= y_top
+ (h
>>1);
2066 int exposed_x
= dc
.LogicalToDeviceX(0);
2067 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2069 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2073 // don't draw rect outline if we already have the
2074 // background color under Mac
2075 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2076 #endif // !__WXMAC__
2080 if ( item
->IsSelected() )
2082 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2086 wxTreeItemAttr
*attr
= item
->GetAttributes();
2087 if (attr
&& attr
->HasTextColour())
2088 colText
= attr
->GetTextColour();
2090 colText
= GetForegroundColour();
2094 dc
.SetTextForeground(colText
);
2098 PaintItem(item
, dc
);
2100 if (HasFlag(wxTR_ROW_LINES
))
2102 // if the background colour is white, choose a
2103 // contrasting color for the lines
2104 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2105 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2106 dc
.DrawLine(0, y_top
, 10000, y_top
);
2107 dc
.DrawLine(0, y
, 10000, y
);
2110 // restore DC objects
2111 dc
.SetBrush(*wxWHITE_BRUSH
);
2112 dc
.SetPen(m_dottedPen
);
2113 dc
.SetTextForeground(*wxBLACK
);
2115 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2117 if (!HasFlag(wxTR_NO_LINES
))
2119 if (x
> (signed)m_indent
)
2120 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2121 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2122 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2123 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2126 if (m_imageListButtons
!= NULL
)
2128 // draw the image button here
2129 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2130 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2131 if (item
->IsSelected())
2132 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2133 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2134 int xx
= x
- (image_w
>>1);
2135 int yy
= y_mid
- (image_h
>>1);
2136 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2137 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2138 wxIMAGELIST_DRAW_TRANSPARENT
);
2139 dc
.DestroyClippingRegion();
2141 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2143 // draw the twisty button here
2145 if (HasFlag(wxTR_AQUA_BUTTONS
))
2147 if (item
->IsExpanded())
2148 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2150 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2154 dc
.SetBrush(*m_hilightBrush
);
2155 dc
.SetPen(*wxBLACK_PEN
);
2158 if (item
->IsExpanded())
2161 button
[0].y
= y_mid
-2;
2163 button
[1].y
= y_mid
-2;
2165 button
[2].y
= y_mid
+3;
2169 button
[0].y
= y_mid
-5;
2171 button
[1].y
= y_mid
+5;
2173 button
[2].y
= y_mid
;
2176 dc
.DrawPolygon(3, button
);
2177 dc
.SetPen(m_dottedPen
);
2180 else // if (HasFlag(wxTR_HAS_BUTTONS))
2182 // draw the plus sign here
2183 dc
.SetPen(*wxGREY_PEN
);
2184 dc
.SetBrush(*wxWHITE_BRUSH
);
2185 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2186 dc
.SetPen(*wxBLACK_PEN
);
2187 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2188 if (!item
->IsExpanded())
2189 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2190 dc
.SetPen(m_dottedPen
);
2193 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2195 // draw the horizontal line here
2197 if (x
> (signed)m_indent
)
2198 x_start
-= m_indent
;
2199 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2201 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2205 if (item
->IsExpanded())
2207 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2208 int count
= children
.Count();
2215 PaintLevel(children
[n
], dc
, level
, y
);
2216 } while (++n
< count
);
2218 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2220 // draw line down to last child
2221 oldY
+= GetLineHeight(children
[n
-1])>>1;
2222 if (HasButtons()) y_mid
+= 5;
2223 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2229 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2233 if ( item
->HasPlus() )
2235 // it's a folder, indicate it by a border
2240 // draw a line under the drop target because the item will be
2242 DrawLine(item
, TRUE
/* below */);
2245 SetCursor(wxCURSOR_BULLSEYE
);
2250 SetCursor(wxCURSOR_NO_ENTRY
);
2254 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2256 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2258 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2260 wxClientDC
dc(this);
2262 dc
.SetLogicalFunction(wxINVERT
);
2263 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2265 int w
= i
->GetWidth() + 2;
2266 int h
= GetLineHeight(i
) + 2;
2268 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2271 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2273 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2275 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2277 wxClientDC
dc(this);
2279 dc
.SetLogicalFunction(wxINVERT
);
2285 y
+= GetLineHeight(i
) - 1;
2288 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2291 // -----------------------------------------------------------------------------
2292 // wxWindows callbacks
2293 // -----------------------------------------------------------------------------
2295 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2303 dc
.SetFont( m_normalFont
);
2304 dc
.SetPen( m_dottedPen
);
2306 // this is now done dynamically
2307 //if(GetImageList() == NULL)
2308 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2311 PaintLevel( m_anchor
, dc
, 0, y
);
2314 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2323 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2332 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2334 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2335 te
.m_evtKey
= event
;
2336 te
.SetEventObject( this );
2337 if ( GetEventHandler()->ProcessEvent( te
) )
2339 // intercepted by the user code
2343 if ( (m_current
== 0) || (m_key_current
== 0) )
2349 // how should the selection work for this event?
2350 bool is_multiple
, extended_select
, unselect_others
;
2351 EventFlagsToSelType(GetWindowStyleFlag(),
2353 event
.ControlDown(),
2354 is_multiple
, extended_select
, unselect_others
);
2358 // * : Expand all/Collapse all
2359 // ' ' | return : activate
2360 // up : go up (not last children!)
2362 // left : go to parent
2363 // right : open if parent and go next
2364 // home : go to root
2365 // end : go to last item without opening parents
2366 switch (event
.KeyCode())
2370 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2378 if ( !IsExpanded(m_current
) )
2381 ExpandAll(m_current
);
2384 //else: fall through to Collapse() it
2388 if (IsExpanded(m_current
))
2390 Collapse(m_current
);
2397 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2398 event
.m_item
= (long) m_current
;
2399 event
.SetEventObject( this );
2400 GetEventHandler()->ProcessEvent( event
);
2404 // up goes to the previous sibling or to the last
2405 // of its children if it's expanded
2408 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2411 prev
= GetParent( m_key_current
);
2412 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2414 break; // don't go to root if it is hidden
2419 wxTreeItemId current
= m_key_current
;
2420 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2421 if (current
== GetFirstChild( prev
, cookie
))
2423 // otherwise we return to where we came from
2424 SelectItem( prev
, unselect_others
, extended_select
);
2425 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2426 EnsureVisible( prev
);
2433 while ( IsExpanded(prev
) && HasChildren(prev
) )
2435 wxTreeItemId child
= GetLastChild(prev
);
2442 SelectItem( prev
, unselect_others
, extended_select
);
2443 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2444 EnsureVisible( prev
);
2449 // left arrow goes to the parent
2452 wxTreeItemId prev
= GetParent( m_current
);
2453 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2455 // don't go to root if it is hidden
2456 prev
= GetPrevSibling( m_current
);
2460 EnsureVisible( prev
);
2461 SelectItem( prev
, unselect_others
, extended_select
);
2467 // this works the same as the down arrow except that we
2468 // also expand the item if it wasn't expanded yet
2474 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2477 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2478 SelectItem( child
, unselect_others
, extended_select
);
2479 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2480 EnsureVisible( child
);
2484 wxTreeItemId next
= GetNextSibling( m_key_current
);
2487 wxTreeItemId current
= m_key_current
;
2488 while (current
&& !next
)
2490 current
= GetParent( current
);
2491 if (current
) next
= GetNextSibling( current
);
2496 SelectItem( next
, unselect_others
, extended_select
);
2497 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2498 EnsureVisible( next
);
2504 // <End> selects the last visible tree item
2507 wxTreeItemId last
= GetRootItem();
2509 while ( last
.IsOk() && IsExpanded(last
) )
2511 wxTreeItemId lastChild
= GetLastChild(last
);
2513 // it may happen if the item was expanded but then all of
2514 // its children have been deleted - so IsExpanded() returned
2515 // TRUE, but GetLastChild() returned invalid item
2524 EnsureVisible( last
);
2525 SelectItem( last
, unselect_others
, extended_select
);
2530 // <Home> selects the root item
2533 wxTreeItemId prev
= GetRootItem();
2535 if (HasFlag(wxTR_HIDE_ROOT
))
2538 prev
= GetFirstChild(prev
, dummy
);
2541 EnsureVisible( prev
);
2542 SelectItem( prev
, unselect_others
, extended_select
);
2551 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2553 // JACS: removed wxYieldIfNeeded() because it can cause the window
2554 // to be deleted from under us if a close window event is pending
2559 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2560 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2561 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2562 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2563 if (flags
) return wxTreeItemId();
2565 if (m_anchor
== NULL
)
2567 flags
= wxTREE_HITTEST_NOWHERE
;
2568 return wxTreeItemId();
2571 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2575 flags
= wxTREE_HITTEST_NOWHERE
;
2576 return wxTreeItemId();
2581 // get the bounding rectangle of the item (or of its label only)
2582 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2584 bool WXUNUSED(textOnly
)) const
2586 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2588 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2591 GetViewStart(& startX
, & startY
);
2593 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2594 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2595 rect
.width
= i
->GetWidth();
2596 //rect.height = i->GetHeight();
2597 rect
.height
= GetLineHeight(i
);
2604 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2606 if (!item
.IsOk()) return;
2608 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2610 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2611 te
.m_item
= (long) m_currentEdit
;
2612 te
.SetEventObject( this );
2613 GetEventHandler()->ProcessEvent( te
);
2615 if (!te
.IsAllowed()) return;
2617 // We have to call this here because the label in
2618 // question might just have been added and no screen
2619 // update taken place.
2620 if (m_dirty
) wxYieldIfNeeded();
2622 wxString s
= m_currentEdit
->GetText();
2623 int w
= m_currentEdit
->GetWidth();
2624 int h
= m_currentEdit
->GetHeight();
2626 CalcScrolledPosition(m_currentEdit
->GetX(), m_currentEdit
->GetY(), &x
, &y
);
2631 int image
= m_currentEdit
->GetCurrentImage();
2632 if ( image
!= NO_IMAGE
)
2634 if ( m_imageListNormal
)
2636 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2641 wxFAIL_MSG(_T("you must create an image list to use images!"));
2645 w
-= image_w
+ 4; // I don't know why +4 is needed
2647 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2657 void wxGenericTreeCtrl::OnRenameTimer()
2662 void wxGenericTreeCtrl::OnRenameAccept()
2664 // TODO if the validator fails this causes a crash
2665 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2666 le
.m_item
= (long) m_currentEdit
;
2667 le
.SetEventObject( this );
2668 le
.m_label
= m_renameRes
;
2669 GetEventHandler()->ProcessEvent( le
);
2671 if (!le
.IsAllowed()) return;
2673 SetItemText( m_currentEdit
, m_renameRes
);
2676 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2678 if ( !m_anchor
) return;
2680 // we process left mouse up event (enables in-place edit), right down
2681 // (pass to the user code), left dbl click (activate item) and
2682 // dragging/moving events for items drag-and-drop
2683 if ( !(event
.LeftDown() ||
2685 event
.RightDown() ||
2686 event
.LeftDClick() ||
2688 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2695 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2698 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2700 if ( event
.Dragging() && !m_isDragging
)
2702 if (m_dragCount
== 0)
2707 if (m_dragCount
!= 3)
2709 // wait until user drags a bit further...
2713 wxEventType command
= event
.RightIsDown()
2714 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2715 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2717 wxTreeEvent
nevent( command
, GetId() );
2718 nevent
.m_item
= (long) m_current
;
2719 nevent
.SetEventObject(this);
2721 // by default the dragging is not supported, the user code must
2722 // explicitly allow the event for it to take place
2725 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2727 // we're going to drag this item
2728 m_isDragging
= TRUE
;
2730 // remember the old cursor because we will change it while
2732 m_oldCursor
= m_cursor
;
2734 // in a single selection control, hide the selection temporarily
2735 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2737 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2739 if ( m_oldSelection
)
2741 m_oldSelection
->SetHilight(FALSE
);
2742 RefreshLine(m_oldSelection
);
2749 else if ( event
.Moving() )
2751 if ( item
!= m_dropTarget
)
2753 // unhighlight the previous drop target
2754 DrawDropEffect(m_dropTarget
);
2756 m_dropTarget
= item
;
2758 // highlight the current drop target if any
2759 DrawDropEffect(m_dropTarget
);
2764 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2766 // erase the highlighting
2767 DrawDropEffect(m_dropTarget
);
2769 if ( m_oldSelection
)
2771 m_oldSelection
->SetHilight(TRUE
);
2772 RefreshLine(m_oldSelection
);
2773 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2776 // generate the drag end event
2777 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2779 event
.m_item
= (long) item
;
2780 event
.m_pointDrag
= pt
;
2781 event
.SetEventObject(this);
2783 (void)GetEventHandler()->ProcessEvent(event
);
2785 m_isDragging
= FALSE
;
2786 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2790 SetCursor(m_oldCursor
);
2796 // here we process only the messages which happen on tree items
2800 if (item
== NULL
) return; /* we hit the blank area */
2802 if ( event
.RightDown() )
2804 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2805 nevent
.m_item
= (long) item
;
2806 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2807 nevent
.SetEventObject(this);
2808 GetEventHandler()->ProcessEvent(nevent
);
2810 else if ( event
.LeftUp() )
2814 if ( (item
== m_current
) &&
2815 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2816 HasFlag(wxTR_EDIT_LABELS
) )
2818 if ( m_renameTimer
->IsRunning() )
2819 m_renameTimer
->Stop();
2821 m_renameTimer
->Start( 100, TRUE
);
2824 m_lastOnSame
= FALSE
;
2827 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2829 if ( event
.LeftDown() )
2831 m_lastOnSame
= item
== m_current
;
2834 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2836 // only toggle the item for a single click, double click on
2837 // the button doesn't do anything (it toggles the item twice)
2838 if ( event
.LeftDown() )
2843 // don't select the item if the button was clicked
2847 // how should the selection work for this event?
2848 bool is_multiple
, extended_select
, unselect_others
;
2849 EventFlagsToSelType(GetWindowStyleFlag(),
2851 event
.ControlDown(),
2852 is_multiple
, extended_select
, unselect_others
);
2854 SelectItem(item
, unselect_others
, extended_select
);
2856 // For some reason, Windows isn't recognizing a left double-click,
2857 // so we need to simulate it here. Allow 200 milliseconds for now.
2858 if ( event
.LeftDClick() )
2860 // double clicking should not start editing the item label
2861 m_renameTimer
->Stop();
2862 m_lastOnSame
= FALSE
;
2864 // send activate event first
2865 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2866 nevent
.m_item
= (long) item
;
2867 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
2868 nevent
.SetEventObject( this );
2869 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2871 // if the user code didn't process the activate event,
2872 // handle it ourselves by toggling the item when it is
2874 if ( item
->HasPlus() )
2884 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2886 /* after all changes have been done to the tree control,
2887 * we actually redraw the tree when everything is over */
2889 if (!m_dirty
) return;
2893 CalculatePositions();
2895 AdjustMyScrollbars();
2898 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2903 wxTreeItemAttr
*attr
= item
->GetAttributes();
2904 if ( attr
&& attr
->HasFont() )
2905 dc
.SetFont(attr
->GetFont());
2906 else if ( item
->IsBold() )
2907 dc
.SetFont(m_boldFont
);
2909 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2912 // restore normal font
2913 dc
.SetFont( m_normalFont
);
2917 int image
= item
->GetCurrentImage();
2918 if ( image
!= NO_IMAGE
)
2920 if ( m_imageListNormal
)
2922 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2927 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2930 total_h
+= 2; // at least 2 pixels
2932 total_h
+= total_h
/10; // otherwise 10% extra spacing
2934 item
->SetHeight(total_h
);
2935 if (total_h
>m_lineHeight
)
2936 m_lineHeight
=total_h
;
2938 item
->SetWidth(image_w
+text_w
+2);
2941 // -----------------------------------------------------------------------------
2942 // for developper : y is now the top of the level
2943 // not the middle of it !
2944 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2946 int x
= level
*m_indent
;
2947 if (!HasFlag(wxTR_HIDE_ROOT
))
2951 else if (level
== 0)
2953 // a hidden root is not evaluated, but its
2954 // children are always calculated
2958 CalculateSize( item
, dc
);
2961 item
->SetX( x
+m_spacing
);
2963 y
+= GetLineHeight(item
);
2965 if ( !item
->IsExpanded() )
2967 // we don't need to calculate collapsed branches
2972 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2973 size_t n
, count
= children
.Count();
2975 for (n
= 0; n
< count
; ++n
)
2976 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2979 void wxGenericTreeCtrl::CalculatePositions()
2981 if ( !m_anchor
) return;
2983 wxClientDC
dc(this);
2986 dc
.SetFont( m_normalFont
);
2988 dc
.SetPen( m_dottedPen
);
2989 //if(GetImageList() == NULL)
2990 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2993 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2996 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2998 if (m_dirty
) return;
3000 wxSize client
= GetClientSize();
3003 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3004 rect
.width
= client
.x
;
3005 rect
.height
= client
.y
;
3007 Refresh(TRUE
, &rect
);
3009 AdjustMyScrollbars();
3012 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3014 if (m_dirty
) return;
3017 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3018 rect
.width
= GetClientSize().x
;
3019 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3021 Refresh(TRUE
, &rect
);
3024 void wxGenericTreeCtrl::RefreshSelected()
3026 // TODO: this is awfully inefficient, we should keep the list of all
3027 // selected items internally, should be much faster
3029 RefreshSelectedUnder(m_anchor
);
3032 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3034 if ( item
->IsSelected() )
3037 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3038 size_t count
= children
.GetCount();
3039 for ( size_t n
= 0; n
< count
; n
++ )
3041 RefreshSelectedUnder(children
[n
]);
3045 // ----------------------------------------------------------------------------
3046 // changing colours: we need to refresh the tree control
3047 // ----------------------------------------------------------------------------
3049 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3051 if ( !wxWindow::SetBackgroundColour(colour
) )
3059 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3061 if ( !wxWindow::SetForegroundColour(colour
) )
3069 #endif // wxUSE_TREECTRL