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 char *aqua_arrow_right
[] = {
64 /* columns rows colors chars-per-pixel */
85 static 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::GetSystemColour
674 wxSYS_COLOUR_HIGHLIGHT
679 m_hilightUnfocusedBrush
= new wxBrush
681 wxSystemSettings::GetSystemColour
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::GetSystemFont( 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 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
759 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
760 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
765 wxGenericTreeCtrl::~wxGenericTreeCtrl()
767 delete m_hilightBrush
;
768 delete m_hilightUnfocusedBrush
;
770 if (m_arrowRight
) delete m_arrowRight
;
771 if (m_arrowDown
) delete m_arrowDown
;
775 delete m_renameTimer
;
776 if (m_ownsImageListNormal
) delete m_imageListNormal
;
777 if (m_ownsImageListState
) delete m_imageListState
;
778 if (m_ownsImageListButtons
) delete m_imageListButtons
;
781 // -----------------------------------------------------------------------------
783 // -----------------------------------------------------------------------------
785 size_t wxGenericTreeCtrl::GetCount() const
787 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
790 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
796 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
802 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
804 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
806 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
809 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
811 // right now, just sets the styles. Eventually, we may
812 // want to update the inherited styles, but right now
813 // none of the parents has updatable styles
814 m_windowStyle
= styles
;
818 // -----------------------------------------------------------------------------
819 // functions to work with tree items
820 // -----------------------------------------------------------------------------
822 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
824 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
826 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
829 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
830 wxTreeItemIcon which
) const
832 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
834 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
837 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
839 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
841 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
844 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
846 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
849 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
850 pItem
->SetText(text
);
851 CalculateSize(pItem
, dc
);
855 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
857 wxTreeItemIcon which
)
859 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
861 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
862 pItem
->SetImage(image
, which
);
865 CalculateSize(pItem
, dc
);
869 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
871 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
873 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
876 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
878 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
880 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
881 pItem
->SetHasPlus(has
);
885 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
887 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
889 // avoid redrawing the tree if no real change
890 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
891 if ( pItem
->IsBold() != bold
)
893 pItem
->SetBold(bold
);
898 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
901 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
903 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
904 pItem
->Attr().SetTextColour(col
);
908 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
911 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
913 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
914 pItem
->Attr().SetBackgroundColour(col
);
918 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
920 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
922 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
923 pItem
->Attr().SetFont(font
);
927 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
929 wxScrolledWindow::SetFont(font
);
931 m_normalFont
= font
;
932 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
933 m_normalFont
.GetFamily(),
934 m_normalFont
.GetStyle(),
936 m_normalFont
.GetUnderlined());
942 // -----------------------------------------------------------------------------
943 // item status inquiries
944 // -----------------------------------------------------------------------------
946 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
948 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
950 // An item is only visible if it's not a descendant of a collapsed item
951 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
952 wxGenericTreeItem
* parent
= pItem
->GetParent();
955 if (!parent
->IsExpanded())
957 parent
= parent
->GetParent();
961 GetViewStart(& startX
, & startY
);
963 wxSize clientSize
= GetClientSize();
966 if (!GetBoundingRect(item
, rect
))
968 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
970 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
972 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
978 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
980 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
982 // consider that the item does have children if it has the "+" button: it
983 // might not have them (if it had never been expanded yet) but then it
984 // could have them as well and it's better to err on this side rather than
985 // disabling some operations which are restricted to the items with
986 // children for an item which does have them
987 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
990 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
992 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
994 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
997 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
999 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1001 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1004 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1006 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1008 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1011 // -----------------------------------------------------------------------------
1013 // -----------------------------------------------------------------------------
1015 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1017 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1019 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1022 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
1024 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1027 return GetNextChild(item
, cookie
);
1030 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
1032 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1034 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1035 if ( (size_t)cookie
< children
.Count() )
1037 return children
.Item((size_t)cookie
++);
1041 // there are no more of them
1042 return wxTreeItemId();
1046 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1048 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1050 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1051 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1054 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1056 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1058 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1059 wxGenericTreeItem
*parent
= i
->GetParent();
1060 if ( parent
== NULL
)
1062 // root item doesn't have any siblings
1063 return wxTreeItemId();
1066 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1067 int index
= siblings
.Index(i
);
1068 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1070 size_t n
= (size_t)(index
+ 1);
1071 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1074 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1076 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1078 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1079 wxGenericTreeItem
*parent
= i
->GetParent();
1080 if ( parent
== NULL
)
1082 // root item doesn't have any siblings
1083 return wxTreeItemId();
1086 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1087 int index
= siblings
.Index(i
);
1088 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1090 return index
== 0 ? wxTreeItemId()
1091 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1094 // Only for internal use right now, but should probably be public
1095 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1097 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1099 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1101 // First see if there are any children.
1102 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1103 if (children
.GetCount() > 0)
1105 return children
.Item(0);
1109 // Try a sibling of this or ancestor instead
1110 wxTreeItemId p
= item
;
1111 wxTreeItemId toFind
;
1114 toFind
= GetNextSibling(p
);
1116 } while (p
.IsOk() && !toFind
.IsOk());
1121 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1123 wxTreeItemId id
= GetRootItem();
1132 } while (id
.IsOk());
1134 return wxTreeItemId();
1137 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1139 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1141 wxTreeItemId id
= item
;
1144 while (id
= GetNext(id
), id
.IsOk())
1150 return wxTreeItemId();
1153 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1155 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1157 wxFAIL_MSG(wxT("not implemented"));
1159 return wxTreeItemId();
1162 // -----------------------------------------------------------------------------
1164 // -----------------------------------------------------------------------------
1166 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1168 const wxString
& text
,
1169 int image
, int selImage
,
1170 wxTreeItemData
*data
)
1172 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1175 // should we give a warning here?
1176 return AddRoot(text
, image
, selImage
, data
);
1179 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1181 wxGenericTreeItem
*item
=
1182 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1186 data
->m_pItem
= (long) item
;
1189 parent
->Insert( item
, previous
);
1194 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1195 int image
, int selImage
,
1196 wxTreeItemData
*data
)
1198 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1200 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1202 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1203 image
, selImage
, data
);
1204 if (HasFlag(wxTR_HIDE_ROOT
))
1206 // if root is hidden, make sure we can navigate
1208 m_anchor
->SetHasPlus();
1213 data
->m_pItem
= (long) m_anchor
;
1216 if (!HasFlag(wxTR_MULTIPLE
))
1218 m_current
= m_key_current
= m_anchor
;
1219 m_current
->SetHilight( TRUE
);
1225 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1226 const wxString
& text
,
1227 int image
, int selImage
,
1228 wxTreeItemData
*data
)
1230 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1233 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1234 const wxTreeItemId
& idPrevious
,
1235 const wxString
& text
,
1236 int image
, int selImage
,
1237 wxTreeItemData
*data
)
1239 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1242 // should we give a warning here?
1243 return AddRoot(text
, image
, selImage
, data
);
1246 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1247 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1248 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1250 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1253 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1255 const wxString
& text
,
1256 int image
, int selImage
,
1257 wxTreeItemData
*data
)
1259 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1262 // should we give a warning here?
1263 return AddRoot(text
, image
, selImage
, data
);
1266 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1269 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1270 const wxString
& text
,
1271 int image
, int selImage
,
1272 wxTreeItemData
*data
)
1274 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1277 // should we give a warning here?
1278 return AddRoot(text
, image
, selImage
, data
);
1281 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1282 image
, selImage
, data
);
1285 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1287 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1288 event
.m_item
= (long) item
;
1289 event
.SetEventObject( this );
1290 ProcessEvent( event
);
1293 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1295 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1297 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1298 item
->DeleteChildren(this);
1301 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1303 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1305 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1307 // don't stay with invalid m_key_current or we will crash in
1308 // the next call to OnChar()
1309 bool changeKeyCurrent
= FALSE
;
1310 wxGenericTreeItem
*itemKey
= m_key_current
;
1313 if ( itemKey
== item
)
1315 // m_key_current is a descendant of the item being deleted
1316 changeKeyCurrent
= TRUE
;
1319 itemKey
= itemKey
->GetParent();
1322 wxGenericTreeItem
*parent
= item
->GetParent();
1325 parent
->GetChildren().Remove( item
); // remove by value
1328 if ( changeKeyCurrent
)
1330 // may be NULL or not
1331 m_key_current
= parent
;
1334 item
->DeleteChildren(this);
1335 SendDeleteEvent(item
);
1339 void wxGenericTreeCtrl::DeleteAllItems()
1345 m_anchor
->DeleteChildren(this);
1352 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1354 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1356 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1358 if ( !item
->HasPlus() )
1361 if ( item
->IsExpanded() )
1364 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1365 event
.m_item
= (long) item
;
1366 event
.SetEventObject( this );
1368 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1370 // cancelled by program
1375 CalculatePositions();
1377 RefreshSubtree(item
);
1379 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1380 ProcessEvent( event
);
1383 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1386 if ( IsExpanded(item
) )
1389 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1390 while ( child
.IsOk() )
1394 child
= GetNextChild(item
, cookie
);
1399 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1401 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1403 if ( !item
->IsExpanded() )
1406 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1407 event
.m_item
= (long) item
;
1408 event
.SetEventObject( this );
1409 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1411 // cancelled by program
1417 #if 0 // TODO why should items be collapsed recursively?
1418 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1419 size_t count
= children
.Count();
1420 for ( size_t n
= 0; n
< count
; n
++ )
1422 Collapse(children
[n
]);
1426 CalculatePositions();
1428 RefreshSubtree(item
);
1430 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1431 ProcessEvent( event
);
1434 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1437 DeleteChildren(item
);
1440 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1442 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1444 if (item
->IsExpanded())
1450 void wxGenericTreeCtrl::Unselect()
1454 m_current
->SetHilight( FALSE
);
1455 RefreshLine( m_current
);
1459 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1461 if (item
->IsSelected())
1463 item
->SetHilight(FALSE
);
1467 if (item
->HasChildren())
1469 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1470 size_t count
= children
.Count();
1471 for ( size_t n
= 0; n
< count
; ++n
)
1473 UnselectAllChildren(children
[n
]);
1478 void wxGenericTreeCtrl::UnselectAll()
1480 wxTreeItemId rootItem
= GetRootItem();
1482 // the tree might not have the root item at all
1485 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1489 // Recursive function !
1490 // To stop we must have crt_item<last_item
1492 // Tag all next children, when no more children,
1493 // Move to parent (not to tag)
1494 // Keep going... if we found last_item, we stop.
1495 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1497 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1499 if (parent
== NULL
) // This is root item
1500 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1502 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1503 int index
= children
.Index(crt_item
);
1504 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1506 size_t count
= children
.Count();
1507 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1509 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1512 return TagNextChildren(parent
, last_item
, select
);
1515 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1517 crt_item
->SetHilight(select
);
1518 RefreshLine(crt_item
);
1520 if (crt_item
==last_item
)
1523 if (crt_item
->HasChildren())
1525 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1526 size_t count
= children
.Count();
1527 for ( size_t n
= 0; n
< count
; ++n
)
1529 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1537 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1539 // item2 is not necessary after item1
1540 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1542 // choice first' and 'last' between item1 and item2
1543 if (item1
->GetY()<item2
->GetY())
1554 bool select
= m_current
->IsSelected();
1556 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1559 TagNextChildren(first
,last
,select
);
1562 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1563 bool unselect_others
,
1564 bool extended_select
)
1566 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1568 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1569 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1571 //wxCHECK_RET( ( (!unselect_others) && is_single),
1572 // wxT("this is a single selection tree") );
1574 // to keep going anyhow !!!
1577 if (item
->IsSelected())
1578 return; // nothing to do
1579 unselect_others
= TRUE
;
1580 extended_select
= FALSE
;
1582 else if ( unselect_others
&& item
->IsSelected() )
1584 // selection change if there is more than one item currently selected
1585 wxArrayTreeItemIds selected_items
;
1586 if ( GetSelections(selected_items
) == 1 )
1590 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1591 event
.m_item
= (long) item
;
1592 event
.m_itemOld
= (long) m_current
;
1593 event
.SetEventObject( this );
1594 // TODO : Here we don't send any selection mode yet !
1596 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1599 wxTreeItemId parent
= GetParent( itemId
);
1600 while (parent
.IsOk())
1602 if (!IsExpanded(parent
))
1605 parent
= GetParent( parent
);
1608 EnsureVisible( itemId
);
1611 if (unselect_others
)
1613 if (is_single
) Unselect(); // to speed up thing
1618 if (extended_select
)
1622 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1625 // don't change the mark (m_current)
1626 SelectItemRange(m_current
, item
);
1630 bool select
=TRUE
; // the default
1632 // Check if we need to toggle hilight (ctrl mode)
1633 if (!unselect_others
)
1634 select
=!item
->IsSelected();
1636 m_current
= m_key_current
= item
;
1637 m_current
->SetHilight(select
);
1638 RefreshLine( m_current
);
1641 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1642 GetEventHandler()->ProcessEvent( event
);
1645 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1646 wxArrayTreeItemIds
&array
) const
1648 if ( item
->IsSelected() )
1649 array
.Add(wxTreeItemId(item
));
1651 if ( item
->HasChildren() )
1653 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1654 size_t count
= children
.GetCount();
1655 for ( size_t n
= 0; n
< count
; ++n
)
1656 FillArray(children
[n
], array
);
1660 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1663 wxTreeItemId idRoot
= GetRootItem();
1664 if ( idRoot
.IsOk() )
1666 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1668 //else: the tree is empty, so no selections
1670 return array
.Count();
1673 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1675 if (!item
.IsOk()) return;
1677 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1679 // first expand all parent branches
1680 wxGenericTreeItem
*parent
= gitem
->GetParent();
1684 parent
= parent
->GetParent();
1687 //if (parent) CalculatePositions();
1692 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1694 if (!item
.IsOk()) return;
1696 // We have to call this here because the label in
1697 // question might just have been added and no screen
1698 // update taken place.
1699 if (m_dirty
) wxYieldIfNeeded();
1701 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1703 // now scroll to the item
1704 int item_y
= gitem
->GetY();
1708 GetViewStart( &start_x
, &start_y
);
1709 start_y
*= PIXELS_PER_UNIT
;
1713 GetClientSize( &client_w
, &client_h
);
1715 if (item_y
< start_y
+3)
1720 m_anchor
->GetSize( x
, y
, this );
1721 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1722 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1723 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1724 // Item should appear at top
1725 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1727 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1732 m_anchor
->GetSize( x
, y
, this );
1733 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1734 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1735 item_y
+= PIXELS_PER_UNIT
+2;
1736 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1737 // Item should appear at bottom
1738 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
);
1742 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1743 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1745 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1746 wxGenericTreeItem
**item2
)
1748 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1750 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1753 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1754 const wxTreeItemId
& item2
)
1756 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1759 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1761 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1763 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1765 wxCHECK_RET( !s_treeBeingSorted
,
1766 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1768 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1769 if ( children
.Count() > 1 )
1773 s_treeBeingSorted
= this;
1774 children
.Sort(tree_ctrl_compare_func
);
1775 s_treeBeingSorted
= NULL
;
1777 //else: don't make the tree dirty as nothing changed
1780 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1782 return m_imageListNormal
;
1785 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1787 return m_imageListButtons
;
1790 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1792 return m_imageListState
;
1795 void wxGenericTreeCtrl::CalculateLineHeight()
1797 wxClientDC
dc(this);
1798 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1800 if ( m_imageListNormal
)
1802 // Calculate a m_lineHeight value from the normal Image sizes.
1803 // May be toggle off. Then wxGenericTreeCtrl will spread when
1804 // necessary (which might look ugly).
1805 int n
= m_imageListNormal
->GetImageCount();
1806 for (int i
= 0; i
< n
; i
++)
1808 int width
= 0, height
= 0;
1809 m_imageListNormal
->GetSize(i
, width
, height
);
1810 if (height
> m_lineHeight
) m_lineHeight
= height
;
1814 if (m_imageListButtons
)
1816 // Calculate a m_lineHeight value from the Button image sizes.
1817 // May be toggle off. Then wxGenericTreeCtrl will spread when
1818 // necessary (which might look ugly).
1819 int n
= m_imageListButtons
->GetImageCount();
1820 for (int i
= 0; i
< n
; i
++)
1822 int width
= 0, height
= 0;
1823 m_imageListButtons
->GetSize(i
, width
, height
);
1824 if (height
> m_lineHeight
) m_lineHeight
= height
;
1828 if (m_lineHeight
< 30)
1829 m_lineHeight
+= 2; // at least 2 pixels
1831 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1834 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1836 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1837 m_imageListNormal
= imageList
;
1838 m_ownsImageListNormal
= FALSE
;
1840 CalculateLineHeight();
1843 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1845 if (m_ownsImageListState
) delete m_imageListState
;
1846 m_imageListState
= imageList
;
1847 m_ownsImageListState
= FALSE
;
1850 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1852 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1853 m_imageListButtons
= imageList
;
1854 m_ownsImageListButtons
= FALSE
;
1856 CalculateLineHeight();
1859 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1861 SetImageList(imageList
);
1862 m_ownsImageListNormal
= TRUE
;
1865 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1867 SetStateImageList(imageList
);
1868 m_ownsImageListState
= TRUE
;
1871 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1873 SetButtonsImageList(imageList
);
1874 m_ownsImageListButtons
= TRUE
;
1877 // -----------------------------------------------------------------------------
1879 // -----------------------------------------------------------------------------
1881 void wxGenericTreeCtrl::AdjustMyScrollbars()
1886 m_anchor
->GetSize( x
, y
, this );
1887 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1888 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1889 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1890 int y_pos
= GetScrollPos( wxVERTICAL
);
1891 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1895 SetScrollbars( 0, 0, 0, 0 );
1899 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1901 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1902 return item
->GetHeight();
1904 return m_lineHeight
;
1907 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1909 // TODO implement "state" icon on items
1911 wxTreeItemAttr
*attr
= item
->GetAttributes();
1912 if ( attr
&& attr
->HasFont() )
1913 dc
.SetFont(attr
->GetFont());
1914 else if (item
->IsBold())
1915 dc
.SetFont(m_boldFont
);
1917 long text_w
= 0, text_h
= 0;
1918 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1920 int image_h
= 0, image_w
= 0;
1921 int image
= item
->GetCurrentImage();
1922 if ( image
!= NO_IMAGE
)
1924 if ( m_imageListNormal
)
1926 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1935 int total_h
= GetLineHeight(item
);
1937 if ( item
->IsSelected() )
1939 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1944 if ( attr
&& attr
->HasBackgroundColour() )
1945 colBg
= attr
->GetBackgroundColour();
1947 colBg
= m_backgroundColour
;
1948 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1951 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1953 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
1957 DoGetPosition(&x
, &y
);
1959 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
1963 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1965 // If it's selected, and there's an image, then we should
1966 // take care to leave the area under the image painted in the
1967 // background colour.
1968 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1969 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1973 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1974 item
->GetWidth()+2, total_h
-offset
);
1978 if ( image
!= NO_IMAGE
)
1980 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1981 m_imageListNormal
->Draw( image
, dc
,
1983 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1984 wxIMAGELIST_DRAW_TRANSPARENT
);
1985 dc
.DestroyClippingRegion();
1988 dc
.SetBackgroundMode(wxTRANSPARENT
);
1989 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1990 dc
.DrawText( item
->GetText(),
1991 (wxCoord
)(image_w
+ item
->GetX()),
1992 (wxCoord
)(item
->GetY() + extraH
));
1994 // restore normal font
1995 dc
.SetFont( m_normalFont
);
1998 // Now y stands for the top of the item, whereas it used to stand for middle !
1999 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2001 int x
= level
*m_indent
;
2002 if (!HasFlag(wxTR_HIDE_ROOT
))
2006 else if (level
== 0)
2008 // always expand hidden root
2010 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2011 int count
= children
.Count();
2017 PaintLevel(children
[n
], dc
, 1, y
);
2018 } while (++n
< count
);
2020 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2022 // draw line down to last child
2023 origY
+= GetLineHeight(children
[0])>>1;
2024 oldY
+= GetLineHeight(children
[n
-1])>>1;
2025 dc
.DrawLine(3, origY
, 3, oldY
);
2031 item
->SetX(x
+m_spacing
);
2034 int h
= GetLineHeight(item
);
2036 int y_mid
= y_top
+ (h
>>1);
2039 int exposed_x
= dc
.LogicalToDeviceX(0);
2040 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2042 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2046 // don't draw rect outline if we already have the
2047 // background color under Mac
2048 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2049 #endif // !__WXMAC__
2053 if ( item
->IsSelected() )
2055 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2059 wxTreeItemAttr
*attr
= item
->GetAttributes();
2060 if (attr
&& attr
->HasTextColour())
2061 colText
= attr
->GetTextColour();
2063 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2067 dc
.SetTextForeground(colText
);
2071 PaintItem(item
, dc
);
2073 if (HasFlag(wxTR_ROW_LINES
))
2075 // if the background colour is white, choose a
2076 // contrasting color for the lines
2077 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2078 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2079 dc
.DrawLine(0, y_top
, 10000, y_top
);
2080 dc
.DrawLine(0, y
, 10000, y
);
2083 // restore DC objects
2084 dc
.SetBrush(*wxWHITE_BRUSH
);
2085 dc
.SetPen(m_dottedPen
);
2086 dc
.SetTextForeground(*wxBLACK
);
2088 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2090 if (!HasFlag(wxTR_NO_LINES
))
2092 if (x
> (signed)m_indent
)
2093 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2094 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2095 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2096 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2099 if (m_imageListButtons
!= NULL
)
2101 // draw the image button here
2102 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2103 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2104 if (item
->IsSelected())
2105 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2106 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2107 int xx
= x
- (image_w
>>1);
2108 int yy
= y_mid
- (image_h
>>1);
2109 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2110 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2111 wxIMAGELIST_DRAW_TRANSPARENT
);
2112 dc
.DestroyClippingRegion();
2114 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2116 // draw the twisty button here
2118 if (HasFlag(wxTR_AQUA_BUTTONS
))
2120 if (item
->IsExpanded())
2121 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2123 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2127 dc
.SetBrush(*m_hilightBrush
);
2128 dc
.SetPen(*wxBLACK_PEN
);
2131 if (item
->IsExpanded())
2134 button
[0].y
= y_mid
-2;
2136 button
[1].y
= y_mid
-2;
2138 button
[2].y
= y_mid
+3;
2142 button
[0].y
= y_mid
-5;
2144 button
[1].y
= y_mid
+5;
2146 button
[2].y
= y_mid
;
2149 dc
.DrawPolygon(3, button
);
2150 dc
.SetPen(m_dottedPen
);
2153 else // if (HasFlag(wxTR_HAS_BUTTONS))
2155 // draw the plus sign here
2156 dc
.SetPen(*wxGREY_PEN
);
2157 dc
.SetBrush(*wxWHITE_BRUSH
);
2158 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2159 dc
.SetPen(*wxBLACK_PEN
);
2160 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2161 if (!item
->IsExpanded())
2162 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2163 dc
.SetPen(m_dottedPen
);
2166 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2168 // draw the horizontal line here
2170 if (x
> (signed)m_indent
)
2171 x_start
-= m_indent
;
2172 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2174 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2178 if (item
->IsExpanded())
2180 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2181 int count
= children
.Count();
2188 PaintLevel(children
[n
], dc
, level
, y
);
2189 } while (++n
< count
);
2191 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2193 // draw line down to last child
2194 oldY
+= GetLineHeight(children
[n
-1])>>1;
2195 if (HasButtons()) y_mid
+= 5;
2196 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2202 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2206 if ( item
->HasPlus() )
2208 // it's a folder, indicate it by a border
2213 // draw a line under the drop target because the item will be
2215 DrawLine(item
, TRUE
/* below */);
2218 SetCursor(wxCURSOR_BULLSEYE
);
2223 SetCursor(wxCURSOR_NO_ENTRY
);
2227 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2229 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2231 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2233 wxClientDC
dc(this);
2235 dc
.SetLogicalFunction(wxINVERT
);
2236 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2238 int w
= i
->GetWidth() + 2;
2239 int h
= GetLineHeight(i
) + 2;
2241 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2244 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2246 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2248 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2250 wxClientDC
dc(this);
2252 dc
.SetLogicalFunction(wxINVERT
);
2258 y
+= GetLineHeight(i
) - 1;
2261 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2264 // -----------------------------------------------------------------------------
2265 // wxWindows callbacks
2266 // -----------------------------------------------------------------------------
2268 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2276 dc
.SetFont( m_normalFont
);
2277 dc
.SetPen( m_dottedPen
);
2279 // this is now done dynamically
2280 //if(GetImageList() == NULL)
2281 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2284 PaintLevel( m_anchor
, dc
, 0, y
);
2287 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2296 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2305 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2307 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2308 te
.m_evtKey
= event
;
2309 te
.SetEventObject( this );
2310 if ( GetEventHandler()->ProcessEvent( te
) )
2312 // intercepted by the user code
2316 if ( (m_current
== 0) || (m_key_current
== 0) )
2322 // how should the selection work for this event?
2323 bool is_multiple
, extended_select
, unselect_others
;
2324 EventFlagsToSelType(GetWindowStyleFlag(),
2326 event
.ControlDown(),
2327 is_multiple
, extended_select
, unselect_others
);
2331 // * : Expand all/Collapse all
2332 // ' ' | return : activate
2333 // up : go up (not last children!)
2335 // left : go to parent
2336 // right : open if parent and go next
2337 // home : go to root
2338 // end : go to last item without opening parents
2339 switch (event
.KeyCode())
2343 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2351 if ( !IsExpanded(m_current
) )
2354 ExpandAll(m_current
);
2357 //else: fall through to Collapse() it
2361 if (IsExpanded(m_current
))
2363 Collapse(m_current
);
2370 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2371 event
.m_item
= (long) m_current
;
2372 event
.SetEventObject( this );
2373 GetEventHandler()->ProcessEvent( event
);
2377 // up goes to the previous sibling or to the last
2378 // of its children if it's expanded
2381 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2384 prev
= GetParent( m_key_current
);
2385 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2387 break; // don't go to root if it is hidden
2392 wxTreeItemId current
= m_key_current
;
2393 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2394 if (current
== GetFirstChild( prev
, cookie
))
2396 // otherwise we return to where we came from
2397 SelectItem( prev
, unselect_others
, extended_select
);
2398 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2399 EnsureVisible( prev
);
2406 while ( IsExpanded(prev
) && HasChildren(prev
) )
2408 wxTreeItemId child
= GetLastChild(prev
);
2415 SelectItem( prev
, unselect_others
, extended_select
);
2416 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2417 EnsureVisible( prev
);
2422 // left arrow goes to the parent
2425 wxTreeItemId prev
= GetParent( m_current
);
2426 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2428 // don't go to root if it is hidden
2429 prev
= GetPrevSibling( m_current
);
2433 EnsureVisible( prev
);
2434 SelectItem( prev
, unselect_others
, extended_select
);
2440 // this works the same as the down arrow except that we
2441 // also expand the item if it wasn't expanded yet
2447 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2450 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2451 SelectItem( child
, unselect_others
, extended_select
);
2452 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2453 EnsureVisible( child
);
2457 wxTreeItemId next
= GetNextSibling( m_key_current
);
2460 wxTreeItemId current
= m_key_current
;
2461 while (current
&& !next
)
2463 current
= GetParent( current
);
2464 if (current
) next
= GetNextSibling( current
);
2469 SelectItem( next
, unselect_others
, extended_select
);
2470 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2471 EnsureVisible( next
);
2477 // <End> selects the last visible tree item
2480 wxTreeItemId last
= GetRootItem();
2482 while ( last
.IsOk() && IsExpanded(last
) )
2484 wxTreeItemId lastChild
= GetLastChild(last
);
2486 // it may happen if the item was expanded but then all of
2487 // its children have been deleted - so IsExpanded() returned
2488 // TRUE, but GetLastChild() returned invalid item
2497 EnsureVisible( last
);
2498 SelectItem( last
, unselect_others
, extended_select
);
2503 // <Home> selects the root item
2506 wxTreeItemId prev
= GetRootItem();
2508 if (HasFlag(wxTR_HIDE_ROOT
))
2511 prev
= GetFirstChild(prev
, dummy
);
2514 EnsureVisible( prev
);
2515 SelectItem( prev
, unselect_others
, extended_select
);
2524 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2526 // JACS: removed wxYieldIfNeeded() because it can cause the window
2527 // to be deleted from under us if a close window event is pending
2532 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2533 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2534 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2535 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2536 if (flags
) return wxTreeItemId();
2538 if (m_anchor
== NULL
)
2540 flags
= wxTREE_HITTEST_NOWHERE
;
2541 return wxTreeItemId();
2544 wxClientDC
dc(this);
2546 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2547 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2548 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2554 flags
= wxTREE_HITTEST_NOWHERE
;
2555 return wxTreeItemId();
2560 // get the bounding rectangle of the item (or of its label only)
2561 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2563 bool WXUNUSED(textOnly
)) const
2565 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2567 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2570 GetViewStart(& startX
, & startY
);
2572 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2573 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2574 rect
.width
= i
->GetWidth();
2575 //rect.height = i->GetHeight();
2576 rect
.height
= GetLineHeight(i
);
2583 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2585 if (!item
.IsOk()) return;
2587 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2589 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2590 te
.m_item
= (long) m_currentEdit
;
2591 te
.SetEventObject( this );
2592 GetEventHandler()->ProcessEvent( te
);
2594 if (!te
.IsAllowed()) return;
2596 // We have to call this here because the label in
2597 // question might just have been added and no screen
2598 // update taken place.
2599 if (m_dirty
) wxYieldIfNeeded();
2601 wxString s
= m_currentEdit
->GetText();
2602 int x
= m_currentEdit
->GetX();
2603 int y
= m_currentEdit
->GetY();
2604 int w
= m_currentEdit
->GetWidth();
2605 int h
= m_currentEdit
->GetHeight();
2610 int image
= m_currentEdit
->GetCurrentImage();
2611 if ( image
!= NO_IMAGE
)
2613 if ( m_imageListNormal
)
2615 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2620 wxFAIL_MSG(_T("you must create an image list to use images!"));
2624 w
-= image_w
+ 4; // I don't know why +4 is needed
2626 wxClientDC
dc(this);
2628 x
= dc
.LogicalToDeviceX( x
);
2629 y
= dc
.LogicalToDeviceY( y
);
2631 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2641 void wxGenericTreeCtrl::OnRenameTimer()
2646 void wxGenericTreeCtrl::OnRenameAccept()
2648 // TODO if the validator fails this causes a crash
2649 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2650 le
.m_item
= (long) m_currentEdit
;
2651 le
.SetEventObject( this );
2652 le
.m_label
= m_renameRes
;
2653 GetEventHandler()->ProcessEvent( le
);
2655 if (!le
.IsAllowed()) return;
2657 SetItemText( m_currentEdit
, m_renameRes
);
2660 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2662 if ( !m_anchor
) return;
2664 // we process left mouse up event (enables in-place edit), right down
2665 // (pass to the user code), left dbl click (activate item) and
2666 // dragging/moving events for items drag-and-drop
2667 if ( !(event
.LeftDown() ||
2669 event
.RightDown() ||
2670 event
.LeftDClick() ||
2672 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2679 wxClientDC
dc(this);
2681 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2682 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2685 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2690 if ( event
.Dragging() && !m_isDragging
)
2692 if (m_dragCount
== 0)
2693 m_dragStart
= wxPoint(x
,y
);
2697 if (m_dragCount
!= 3)
2699 // wait until user drags a bit further...
2703 wxEventType command
= event
.RightIsDown()
2704 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2705 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2707 wxTreeEvent
nevent( command
, GetId() );
2708 nevent
.m_item
= (long) m_current
;
2709 nevent
.SetEventObject(this);
2711 // by default the dragging is not supported, the user code must
2712 // explicitly allow the event for it to take place
2715 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2717 // we're going to drag this item
2718 m_isDragging
= TRUE
;
2720 // remember the old cursor because we will change it while
2722 m_oldCursor
= m_cursor
;
2724 // in a single selection control, hide the selection temporarily
2725 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2727 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2729 if ( m_oldSelection
)
2731 m_oldSelection
->SetHilight(FALSE
);
2732 RefreshLine(m_oldSelection
);
2739 else if ( event
.Moving() )
2741 if ( item
!= m_dropTarget
)
2743 // unhighlight the previous drop target
2744 DrawDropEffect(m_dropTarget
);
2746 m_dropTarget
= item
;
2748 // highlight the current drop target if any
2749 DrawDropEffect(m_dropTarget
);
2754 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2756 // erase the highlighting
2757 DrawDropEffect(m_dropTarget
);
2759 if ( m_oldSelection
)
2761 m_oldSelection
->SetHilight(TRUE
);
2762 RefreshLine(m_oldSelection
);
2763 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2766 // generate the drag end event
2767 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2769 event
.m_item
= (long) item
;
2770 event
.m_pointDrag
= wxPoint(x
, y
);
2771 event
.SetEventObject(this);
2773 (void)GetEventHandler()->ProcessEvent(event
);
2775 m_isDragging
= FALSE
;
2776 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2780 SetCursor(m_oldCursor
);
2786 // here we process only the messages which happen on tree items
2790 if (item
== NULL
) return; /* we hit the blank area */
2792 if ( event
.RightDown() )
2794 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2795 nevent
.m_item
= (long) item
;
2796 CalcScrolledPosition(x
, y
,
2797 &nevent
.m_pointDrag
.x
,
2798 &nevent
.m_pointDrag
.y
);
2799 nevent
.SetEventObject(this);
2800 GetEventHandler()->ProcessEvent(nevent
);
2802 else if ( event
.LeftUp() )
2806 if ( (item
== m_current
) &&
2807 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2808 HasFlag(wxTR_EDIT_LABELS
) )
2810 if ( m_renameTimer
->IsRunning() )
2811 m_renameTimer
->Stop();
2813 m_renameTimer
->Start( 100, TRUE
);
2816 m_lastOnSame
= FALSE
;
2819 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2821 if ( event
.LeftDown() )
2823 m_lastOnSame
= item
== m_current
;
2826 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2828 // only toggle the item for a single click, double click on
2829 // the button doesn't do anything (it toggles the item twice)
2830 if ( event
.LeftDown() )
2835 // don't select the item if the button was clicked
2839 // how should the selection work for this event?
2840 bool is_multiple
, extended_select
, unselect_others
;
2841 EventFlagsToSelType(GetWindowStyleFlag(),
2843 event
.ControlDown(),
2844 is_multiple
, extended_select
, unselect_others
);
2846 SelectItem(item
, unselect_others
, extended_select
);
2848 // For some reason, Windows isn't recognizing a left double-click,
2849 // so we need to simulate it here. Allow 200 milliseconds for now.
2850 if ( event
.LeftDClick() )
2852 // double clicking should not start editing the item label
2853 m_renameTimer
->Stop();
2854 m_lastOnSame
= FALSE
;
2856 // send activate event first
2857 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2858 nevent
.m_item
= (long) item
;
2859 CalcScrolledPosition(x
, y
,
2860 &nevent
.m_pointDrag
.x
,
2861 &nevent
.m_pointDrag
.y
);
2862 nevent
.SetEventObject( this );
2863 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2865 // if the user code didn't process the activate event,
2866 // handle it ourselves by toggling the item when it is
2868 if ( item
->HasPlus() )
2878 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2880 /* after all changes have been done to the tree control,
2881 * we actually redraw the tree when everything is over */
2883 if (!m_dirty
) return;
2887 CalculatePositions();
2889 AdjustMyScrollbars();
2892 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2898 dc
.SetFont(m_boldFont
);
2900 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2903 // restore normal font
2904 dc
.SetFont( m_normalFont
);
2908 int image
= item
->GetCurrentImage();
2909 if ( image
!= NO_IMAGE
)
2911 if ( m_imageListNormal
)
2913 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2918 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2921 total_h
+= 2; // at least 2 pixels
2923 total_h
+= total_h
/10; // otherwise 10% extra spacing
2925 item
->SetHeight(total_h
);
2926 if (total_h
>m_lineHeight
)
2927 m_lineHeight
=total_h
;
2929 item
->SetWidth(image_w
+text_w
+2);
2932 // -----------------------------------------------------------------------------
2933 // for developper : y is now the top of the level
2934 // not the middle of it !
2935 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2937 int x
= level
*m_indent
;
2938 if (!HasFlag(wxTR_HIDE_ROOT
))
2942 else if (level
== 0)
2944 // a hidden root is not evaluated, but its
2945 // children are always calculated
2949 CalculateSize( item
, dc
);
2952 item
->SetX( x
+m_spacing
);
2954 y
+= GetLineHeight(item
);
2956 if ( !item
->IsExpanded() )
2958 // we don't need to calculate collapsed branches
2963 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2964 size_t n
, count
= children
.Count();
2966 for (n
= 0; n
< count
; ++n
)
2967 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2970 void wxGenericTreeCtrl::CalculatePositions()
2972 if ( !m_anchor
) return;
2974 wxClientDC
dc(this);
2977 dc
.SetFont( m_normalFont
);
2979 dc
.SetPen( m_dottedPen
);
2980 //if(GetImageList() == NULL)
2981 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2984 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2987 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2989 if (m_dirty
) return;
2991 wxClientDC
dc(this);
2996 GetClientSize( &cw
, &ch
);
2999 rect
.x
= dc
.LogicalToDeviceX( 0 );
3001 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
3004 Refresh( TRUE
, &rect
);
3006 AdjustMyScrollbars();
3009 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3011 if (m_dirty
) return;
3013 wxClientDC
dc(this);
3018 GetClientSize( &cw
, &ch
);
3021 rect
.x
= dc
.LogicalToDeviceX( 0 );
3022 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
3024 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3026 Refresh( TRUE
, &rect
);
3029 void wxGenericTreeCtrl::RefreshSelected()
3031 // TODO: this is awfully inefficient, we should keep the list of all
3032 // selected items internally, should be much faster
3034 RefreshSelectedUnder(m_anchor
);
3037 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3039 if ( item
->IsSelected() )
3042 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3043 size_t count
= children
.GetCount();
3044 for ( size_t n
= 0; n
< count
; n
++ )
3046 RefreshSelectedUnder(children
[n
]);
3050 // ----------------------------------------------------------------------------
3051 // changing colours: we need to refresh the tree control
3052 // ----------------------------------------------------------------------------
3054 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3056 if ( !wxWindow::SetBackgroundColour(colour
) )
3064 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3066 if ( !wxWindow::SetForegroundColour(colour
) )
3074 #endif // wxUSE_TREECTRL