1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "treectlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/treebase.h"
34 #include "wx/generic/treectlg.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 class WXDLLEXPORT wxGenericTreeItem
;
47 WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
48 //WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const int NO_IMAGE
= -1;
56 #define PIXELS_PER_UNIT 10
58 // -----------------------------------------------------------------------------
60 // -----------------------------------------------------------------------------
62 // timer used for enabling in-place edit
63 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
66 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
71 wxGenericTreeCtrl
*m_owner
;
74 // control used for in-place edit
75 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
78 wxTreeTextCtrl( wxWindow
*parent
,
82 wxGenericTreeCtrl
*owner
,
83 const wxString
&value
= wxEmptyString
,
84 const wxPoint
&pos
= wxDefaultPosition
,
85 const wxSize
&size
= wxDefaultSize
,
86 int style
= wxSIMPLE_BORDER
,
87 const wxValidator
& validator
= wxDefaultValidator
,
88 const wxString
&name
= wxTextCtrlNameStr
);
90 void OnChar( wxKeyEvent
&event
);
91 void OnKeyUp( wxKeyEvent
&event
);
92 void OnKillFocus( wxFocusEvent
&event
);
97 wxGenericTreeCtrl
*m_owner
;
98 wxString m_startValue
;
101 DECLARE_EVENT_TABLE()
105 class WXDLLEXPORT wxGenericTreeItem
109 wxGenericTreeItem() { m_data
= NULL
; }
110 wxGenericTreeItem( wxGenericTreeItem
*parent
,
111 const wxString
& text
,
114 wxTreeItemData
*data
);
116 ~wxGenericTreeItem();
119 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
121 const wxString
& GetText() const { return m_text
; }
122 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
123 { return m_images
[which
]; }
124 wxTreeItemData
*GetData() const { return m_data
; }
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
130 void SetText( const wxString
&text
);
131 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
136 void SetBold(bool bold
) { m_isBold
= bold
; }
138 int GetX() const { return m_x
; }
139 int GetY() const { return m_y
; }
141 void SetX(int x
) { m_x
= x
; }
142 void SetY(int y
) { m_y
= y
; }
144 int GetHeight() const { return m_height
; }
145 int GetWidth() const { return m_width
; }
147 void SetHeight(int h
) { m_height
= h
; }
148 void SetWidth(int w
) { m_width
= w
; }
150 wxGenericTreeItem
*GetParent() const { return m_parent
; }
153 // deletes all children notifying the treectrl about it if !NULL
155 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
157 // get count of all children (and grand children if 'recursively')
158 size_t GetChildrenCount(bool recursively
= TRUE
) const;
160 void Insert(wxGenericTreeItem
*child
, size_t index
)
161 { m_children
.Insert(child
, index
); }
163 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
165 // return the item at given position (or NULL if no item), onButton is
166 // TRUE if the point belongs to the item's button, otherwise it lies
167 // on the button's label
168 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
169 const wxGenericTreeCtrl
*,
173 void Expand() { m_isCollapsed
= FALSE
; }
174 void Collapse() { m_isCollapsed
= TRUE
; }
176 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
179 bool HasChildren() const { return !m_children
.IsEmpty(); }
180 bool IsSelected() const { return m_hasHilight
!= 0; }
181 bool IsExpanded() const { return !m_isCollapsed
; }
182 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
183 bool IsBold() const { return m_isBold
!= 0; }
186 // get them - may be NULL
187 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
188 // get them ensuring that the pointer is not NULL
189 wxTreeItemAttr
& Attr()
193 m_attr
= new wxTreeItemAttr
;
199 void SetAttributes(wxTreeItemAttr
*attr
)
201 if ( m_ownsAttr
) delete m_attr
;
205 // set them and delete when done
206 void AssignAttributes(wxTreeItemAttr
*attr
)
213 // since there can be very many of these, we save size by chosing
214 // the smallest representation for the elements and by ordering
215 // the members to avoid padding.
216 wxString m_text
; // label to be rendered for item
218 wxTreeItemData
*m_data
; // user-provided data
220 wxArrayGenericTreeItems m_children
; // list of children
221 wxGenericTreeItem
*m_parent
; // parent of this item
223 wxTreeItemAttr
*m_attr
; // attributes???
225 // tree ctrl images for the normal, selected, expanded and
226 // expanded+selected states
227 short m_images
[wxTreeItemIcon_Max
];
229 wxCoord m_x
; // (virtual) offset from top
230 short m_y
; // (virtual) offset from left
231 short m_width
; // width of this item
232 unsigned char m_height
; // height of this item
234 // use bitfields to save size
235 int m_isCollapsed
:1;
236 int m_hasHilight
:1; // same as focused
237 int m_hasPlus
:1; // used for item which doesn't have
238 // children but has a [+] button
239 int m_isBold
:1; // render the label in bold font
240 int m_ownsAttr
:1; // delete attribute when done
243 // =============================================================================
245 // =============================================================================
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // translate the key or mouse event flags to the type of selection we're
253 static void EventFlagsToSelType(long style
,
257 bool &extended_select
,
258 bool &unselect_others
)
260 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
261 extended_select
= shiftDown
&& is_multiple
;
262 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
265 // -----------------------------------------------------------------------------
266 // wxTreeRenameTimer (internal)
267 // -----------------------------------------------------------------------------
269 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
274 void wxTreeRenameTimer::Notify()
276 m_owner
->OnRenameTimer();
279 //-----------------------------------------------------------------------------
280 // wxTreeTextCtrl (internal)
281 //-----------------------------------------------------------------------------
283 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
284 EVT_CHAR (wxTreeTextCtrl::OnChar
)
285 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
286 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
289 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow
*parent
,
293 wxGenericTreeCtrl
*owner
,
294 const wxString
&value
,
298 const wxValidator
& validator
,
299 const wxString
&name
)
300 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
306 (*m_res
) = wxEmptyString
;
307 m_startValue
= value
;
311 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
313 if (event
.m_keyCode
== WXK_RETURN
)
316 (*m_res
) = GetValue();
318 if ((*m_res
) != m_startValue
)
319 m_owner
->OnRenameAccept();
321 if (!wxPendingDelete
.Member(this))
322 wxPendingDelete
.Append(this);
325 m_owner
->SetFocus(); // This doesn't work. TODO.
329 if (event
.m_keyCode
== WXK_ESCAPE
)
334 if (!wxPendingDelete
.Member(this))
335 wxPendingDelete
.Append(this);
338 m_owner
->SetFocus(); // This doesn't work. TODO.
345 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
353 // auto-grow the textctrl:
354 wxSize parentSize
= m_owner
->GetSize();
355 wxPoint myPos
= GetPosition();
356 wxSize mySize
= GetSize();
358 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
359 if (myPos
.x
+ sx
> parentSize
.x
) sx
= parentSize
.x
- myPos
.x
;
360 if (mySize
.x
> sx
) sx
= mySize
.x
;
366 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
374 if (!wxPendingDelete
.Member(this))
375 wxPendingDelete
.Append(this);
378 (*m_res
) = GetValue();
380 if ((*m_res
) != m_startValue
)
381 m_owner
->OnRenameAccept();
384 // -----------------------------------------------------------------------------
386 // -----------------------------------------------------------------------------
388 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
389 const wxString
& text
,
390 int image
, int selImage
,
391 wxTreeItemData
*data
)
394 m_images
[wxTreeItemIcon_Normal
] = image
;
395 m_images
[wxTreeItemIcon_Selected
] = selImage
;
396 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
397 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
402 m_isCollapsed
= TRUE
;
403 m_hasHilight
= FALSE
;
409 m_attr
= (wxTreeItemAttr
*)NULL
;
412 // We don't know the height here yet.
417 wxGenericTreeItem::~wxGenericTreeItem()
421 if (m_ownsAttr
) delete m_attr
;
423 wxASSERT_MSG( m_children
.IsEmpty(),
424 wxT("please call DeleteChildren() before deleting the item") );
427 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
429 size_t count
= m_children
.Count();
430 for ( size_t n
= 0; n
< count
; n
++ )
432 wxGenericTreeItem
*child
= m_children
[n
];
434 tree
->SendDeleteEvent(child
);
436 child
->DeleteChildren(tree
);
443 void wxGenericTreeItem::SetText( const wxString
&text
)
448 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
450 size_t count
= m_children
.Count();
454 size_t total
= count
;
455 for (size_t n
= 0; n
< count
; ++n
)
457 total
+= m_children
[n
]->GetChildrenCount();
463 void wxGenericTreeItem::GetSize( int &x
, int &y
,
464 const wxGenericTreeCtrl
*theButton
)
466 int bottomY
=m_y
+theButton
->GetLineHeight(this);
467 if ( y
< bottomY
) y
= bottomY
;
468 int width
= m_x
+ m_width
;
469 if ( x
< width
) x
= width
;
473 size_t count
= m_children
.Count();
474 for ( size_t n
= 0; n
< count
; ++n
)
476 m_children
[n
]->GetSize( x
, y
, theButton
);
481 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
482 const wxGenericTreeCtrl
*theCtrl
,
486 // for a hidden root node, don't evaluate it, but do evaluate children
487 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
490 int h
= theCtrl
->GetLineHeight(this);
491 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
493 int y_mid
= m_y
+ h
/2;
494 if (point
.y
< y_mid
)
495 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
497 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
499 // 5 is the size of the plus sign
500 int xCross
= m_x
- theCtrl
->GetSpacing();
501 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
502 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
503 HasPlus() && theCtrl
->HasButtons() )
505 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
509 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
514 // assuming every image (normal and selected) has the same size!
515 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
516 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
519 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
520 flags
|= wxTREE_HITTEST_ONITEMICON
;
522 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
528 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
529 if (point
.x
> m_x
+m_width
)
530 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
535 // if children are expanded, fall through to evaluate them
536 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
540 size_t count
= m_children
.Count();
541 for ( size_t n
= 0; n
< count
; n
++ )
543 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
551 return (wxGenericTreeItem
*) NULL
;
554 int wxGenericTreeItem::GetCurrentImage() const
556 int image
= NO_IMAGE
;
561 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
564 if ( image
== NO_IMAGE
)
566 // we usually fall back to the normal item, but try just the
567 // expanded one (and not selected) first in this case
568 image
= GetImage(wxTreeItemIcon_Expanded
);
574 image
= GetImage(wxTreeItemIcon_Selected
);
577 // maybe it doesn't have the specific image we want,
578 // try the default one instead
579 if ( image
== NO_IMAGE
) image
= GetImage();
584 // -----------------------------------------------------------------------------
585 // wxGenericTreeCtrl implementation
586 // -----------------------------------------------------------------------------
588 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
590 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
591 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
592 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
593 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
594 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
595 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
596 EVT_IDLE (wxGenericTreeCtrl::OnIdle
)
599 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
601 * wxTreeCtrl has to be a real class or we have problems with
602 * the run-time information.
605 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
608 // -----------------------------------------------------------------------------
609 // construction/destruction
610 // -----------------------------------------------------------------------------
612 void wxGenericTreeCtrl::Init()
614 m_current
= m_key_current
= m_anchor
= (wxGenericTreeItem
*) NULL
;
622 m_hilightBrush
= new wxBrush
624 wxSystemSettings::GetSystemColour
626 wxSYS_COLOUR_HIGHLIGHT
631 m_hilightUnfocusedBrush
= new wxBrush
633 wxSystemSettings::GetSystemColour
635 wxSYS_COLOUR_BTNSHADOW
640 m_imageListNormal
= m_imageListButtons
=
641 m_imageListState
= (wxImageList
*) NULL
;
642 m_ownsImageListNormal
= m_ownsImageListButtons
=
643 m_ownsImageListState
= FALSE
;
646 m_isDragging
= FALSE
;
647 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
649 m_renameTimer
= new wxTreeRenameTimer( this );
650 m_lastOnSame
= FALSE
;
652 m_normalFont
= wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT
);
653 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
654 m_normalFont
.GetFamily(),
655 m_normalFont
.GetStyle(),
657 m_normalFont
.GetUnderlined());
660 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
665 const wxValidator
&validator
,
666 const wxString
& name
)
670 wxGetOsVersion( &major
, &minor
);
672 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
673 style
&= ~wxTR_LINES_AT_ROOT
;
674 style
|= wxTR_NO_LINES
;
676 style
|= wxTR_ROW_LINES
;
679 wxScrolledWindow::Create( parent
, id
, pos
, size
,
680 style
|wxHSCROLL
|wxVSCROLL
, name
);
682 // If the tree display has no buttons, but does have
683 // connecting lines, we can use a narrower layout.
684 // It may not be a good idea to force this...
685 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
692 SetValidator( validator
);
695 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
697 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
698 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
703 wxGenericTreeCtrl::~wxGenericTreeCtrl()
705 delete m_hilightBrush
;
706 delete m_hilightUnfocusedBrush
;
710 delete m_renameTimer
;
711 if (m_ownsImageListNormal
) delete m_imageListNormal
;
712 if (m_ownsImageListState
) delete m_imageListState
;
713 if (m_ownsImageListButtons
) delete m_imageListButtons
;
716 // -----------------------------------------------------------------------------
718 // -----------------------------------------------------------------------------
720 size_t wxGenericTreeCtrl::GetCount() const
722 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
725 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
731 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
737 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
739 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
741 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
744 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
746 // right now, just sets the styles. Eventually, we may
747 // want to update the inherited styles, but right now
748 // none of the parents has updatable styles
749 m_windowStyle
= styles
;
753 // -----------------------------------------------------------------------------
754 // functions to work with tree items
755 // -----------------------------------------------------------------------------
757 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
759 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
761 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
764 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
765 wxTreeItemIcon which
) const
767 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
769 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
772 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
774 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
776 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
779 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
781 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
784 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
785 pItem
->SetText(text
);
786 CalculateSize(pItem
, dc
);
790 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
792 wxTreeItemIcon which
)
794 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
796 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
797 pItem
->SetImage(image
, which
);
800 CalculateSize(pItem
, dc
);
804 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
806 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
808 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
811 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
813 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
815 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
816 pItem
->SetHasPlus(has
);
820 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
822 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
824 // avoid redrawing the tree if no real change
825 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
826 if ( pItem
->IsBold() != bold
)
828 pItem
->SetBold(bold
);
833 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
836 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
838 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
839 pItem
->Attr().SetTextColour(col
);
843 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
846 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
848 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
849 pItem
->Attr().SetBackgroundColour(col
);
853 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
855 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
857 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
858 pItem
->Attr().SetFont(font
);
862 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
864 wxScrolledWindow::SetFont(font
);
866 m_normalFont
= font
;
867 m_boldFont
= wxFont( m_normalFont
.GetPointSize(),
868 m_normalFont
.GetFamily(),
869 m_normalFont
.GetStyle(),
871 m_normalFont
.GetUnderlined());
877 // -----------------------------------------------------------------------------
878 // item status inquiries
879 // -----------------------------------------------------------------------------
881 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
883 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
885 // An item is only visible if it's not a descendant of a collapsed item
886 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
887 wxGenericTreeItem
* parent
= pItem
->GetParent();
890 if (!parent
->IsExpanded())
892 parent
= parent
->GetParent();
896 GetViewStart(& startX
, & startY
);
898 wxSize clientSize
= GetClientSize();
901 if (!GetBoundingRect(item
, rect
))
903 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
905 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
907 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
913 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
915 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
917 // consider that the item does have children if it has the "+" button: it
918 // might not have them (if it had never been expanded yet) but then it
919 // could have them as well and it's better to err on this side rather than
920 // disabling some operations which are restricted to the items with
921 // children for an item which does have them
922 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
925 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
927 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
929 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
932 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
934 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
936 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
939 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
941 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
943 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
946 // -----------------------------------------------------------------------------
948 // -----------------------------------------------------------------------------
950 wxTreeItemId
wxGenericTreeCtrl::GetParent(const wxTreeItemId
& item
) const
952 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
954 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
957 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const
959 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
962 return GetNextChild(item
, cookie
);
965 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
, long& cookie
) const
967 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
969 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
970 if ( (size_t)cookie
< children
.Count() )
972 return children
.Item((size_t)cookie
++);
976 // there are no more of them
977 return wxTreeItemId();
981 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
983 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
985 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
986 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
989 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
991 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
993 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
994 wxGenericTreeItem
*parent
= i
->GetParent();
995 if ( parent
== NULL
)
997 // root item doesn't have any siblings
998 return wxTreeItemId();
1001 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1002 int index
= siblings
.Index(i
);
1003 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1005 size_t n
= (size_t)(index
+ 1);
1006 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1009 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1011 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1013 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1014 wxGenericTreeItem
*parent
= i
->GetParent();
1015 if ( parent
== NULL
)
1017 // root item doesn't have any siblings
1018 return wxTreeItemId();
1021 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1022 int index
= siblings
.Index(i
);
1023 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1025 return index
== 0 ? wxTreeItemId()
1026 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1029 // Only for internal use right now, but should probably be public
1030 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1032 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1034 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1036 // First see if there are any children.
1037 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1038 if (children
.GetCount() > 0)
1040 return children
.Item(0);
1044 // Try a sibling of this or ancestor instead
1045 wxTreeItemId p
= item
;
1046 wxTreeItemId toFind
;
1049 toFind
= GetNextSibling(p
);
1051 } while (p
.IsOk() && !toFind
.IsOk());
1056 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1058 wxTreeItemId id
= GetRootItem();
1067 } while (id
.IsOk());
1069 return wxTreeItemId();
1072 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1074 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1076 wxTreeItemId id
= item
;
1079 while (id
= GetNext(id
), id
.IsOk())
1085 return wxTreeItemId();
1088 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1090 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1092 wxFAIL_MSG(wxT("not implemented"));
1094 return wxTreeItemId();
1097 // -----------------------------------------------------------------------------
1099 // -----------------------------------------------------------------------------
1101 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1103 const wxString
& text
,
1104 int image
, int selImage
,
1105 wxTreeItemData
*data
)
1107 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1110 // should we give a warning here?
1111 return AddRoot(text
, image
, selImage
, data
);
1114 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1116 wxGenericTreeItem
*item
=
1117 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1121 data
->m_pItem
= (long) item
;
1124 parent
->Insert( item
, previous
);
1129 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1130 int image
, int selImage
,
1131 wxTreeItemData
*data
)
1133 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1135 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1137 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1138 image
, selImage
, data
);
1139 if (HasFlag(wxTR_HIDE_ROOT
))
1141 // if root is hidden, make sure we can navigate
1143 m_anchor
->SetHasPlus();
1148 data
->m_pItem
= (long) m_anchor
;
1151 if (!HasFlag(wxTR_MULTIPLE
))
1153 m_current
= m_key_current
= m_anchor
;
1154 m_current
->SetHilight( TRUE
);
1160 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1161 const wxString
& text
,
1162 int image
, int selImage
,
1163 wxTreeItemData
*data
)
1165 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1168 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1169 const wxTreeItemId
& idPrevious
,
1170 const wxString
& text
,
1171 int image
, int selImage
,
1172 wxTreeItemData
*data
)
1174 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1177 // should we give a warning here?
1178 return AddRoot(text
, image
, selImage
, data
);
1181 int index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1182 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1183 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1185 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1188 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1190 const wxString
& text
,
1191 int image
, int selImage
,
1192 wxTreeItemData
*data
)
1194 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1197 // should we give a warning here?
1198 return AddRoot(text
, image
, selImage
, data
);
1201 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1204 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1205 const wxString
& text
,
1206 int image
, int selImage
,
1207 wxTreeItemData
*data
)
1209 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1212 // should we give a warning here?
1213 return AddRoot(text
, image
, selImage
, data
);
1216 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1217 image
, selImage
, data
);
1220 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1222 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1223 event
.m_item
= (long) item
;
1224 event
.SetEventObject( this );
1225 ProcessEvent( event
);
1228 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1230 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1232 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1233 item
->DeleteChildren(this);
1236 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1238 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1240 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1242 // don't stay with invalid m_key_current or we will crash in
1243 // the next call to OnChar()
1244 bool changeKeyCurrent
= FALSE
;
1245 wxGenericTreeItem
*itemKey
= m_key_current
;
1248 if ( itemKey
== item
)
1250 // m_key_current is a descendant of the item being deleted
1251 changeKeyCurrent
= TRUE
;
1254 itemKey
= itemKey
->GetParent();
1257 wxGenericTreeItem
*parent
= item
->GetParent();
1260 parent
->GetChildren().Remove( item
); // remove by value
1263 if ( changeKeyCurrent
)
1265 // may be NULL or not
1266 m_key_current
= parent
;
1269 item
->DeleteChildren(this);
1270 SendDeleteEvent(item
);
1274 void wxGenericTreeCtrl::DeleteAllItems()
1280 m_anchor
->DeleteChildren(this);
1287 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1289 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1291 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1293 if ( !item
->HasPlus() )
1296 if ( item
->IsExpanded() )
1299 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1300 event
.m_item
= (long) item
;
1301 event
.SetEventObject( this );
1303 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1305 // cancelled by program
1310 CalculatePositions();
1312 RefreshSubtree(item
);
1314 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1315 ProcessEvent( event
);
1318 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1321 if ( IsExpanded(item
) )
1324 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1325 while ( child
.IsOk() )
1329 child
= GetNextChild(item
, cookie
);
1334 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1336 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1338 if ( !item
->IsExpanded() )
1341 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1342 event
.m_item
= (long) item
;
1343 event
.SetEventObject( this );
1344 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1346 // cancelled by program
1352 #if 0 // TODO why should items be collapsed recursively?
1353 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1354 size_t count
= children
.Count();
1355 for ( size_t n
= 0; n
< count
; n
++ )
1357 Collapse(children
[n
]);
1361 CalculatePositions();
1363 RefreshSubtree(item
);
1365 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1366 ProcessEvent( event
);
1369 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1372 DeleteChildren(item
);
1375 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1377 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1379 if (item
->IsExpanded())
1385 void wxGenericTreeCtrl::Unselect()
1389 m_current
->SetHilight( FALSE
);
1390 RefreshLine( m_current
);
1394 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1396 if (item
->IsSelected())
1398 item
->SetHilight(FALSE
);
1402 if (item
->HasChildren())
1404 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1405 size_t count
= children
.Count();
1406 for ( size_t n
= 0; n
< count
; ++n
)
1408 UnselectAllChildren(children
[n
]);
1413 void wxGenericTreeCtrl::UnselectAll()
1415 UnselectAllChildren((wxGenericTreeItem
*) GetRootItem().m_pItem
);
1418 // Recursive function !
1419 // To stop we must have crt_item<last_item
1421 // Tag all next children, when no more children,
1422 // Move to parent (not to tag)
1423 // Keep going... if we found last_item, we stop.
1424 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1426 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1428 if (parent
== NULL
) // This is root item
1429 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1431 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1432 int index
= children
.Index(crt_item
);
1433 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1435 size_t count
= children
.Count();
1436 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1438 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1441 return TagNextChildren(parent
, last_item
, select
);
1444 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1446 crt_item
->SetHilight(select
);
1447 RefreshLine(crt_item
);
1449 if (crt_item
==last_item
)
1452 if (crt_item
->HasChildren())
1454 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1455 size_t count
= children
.Count();
1456 for ( size_t n
= 0; n
< count
; ++n
)
1458 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1466 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1468 // item2 is not necessary after item1
1469 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1471 // choice first' and 'last' between item1 and item2
1472 if (item1
->GetY()<item2
->GetY())
1483 bool select
= m_current
->IsSelected();
1485 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1488 TagNextChildren(first
,last
,select
);
1491 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1492 bool unselect_others
,
1493 bool extended_select
)
1495 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1497 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1498 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1500 //wxCHECK_RET( ( (!unselect_others) && is_single),
1501 // wxT("this is a single selection tree") );
1503 // to keep going anyhow !!!
1506 if (item
->IsSelected())
1507 return; // nothing to do
1508 unselect_others
= TRUE
;
1509 extended_select
= FALSE
;
1511 else if ( unselect_others
&& item
->IsSelected() )
1513 // selection change if there is more than one item currently selected
1514 wxArrayTreeItemIds selected_items
;
1515 if ( GetSelections(selected_items
) == 1 )
1519 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1520 event
.m_item
= (long) item
;
1521 event
.m_itemOld
= (long) m_current
;
1522 event
.SetEventObject( this );
1523 // TODO : Here we don't send any selection mode yet !
1525 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1528 wxTreeItemId parent
= GetParent( itemId
);
1529 while (parent
.IsOk())
1531 if (!IsExpanded(parent
))
1534 parent
= GetParent( parent
);
1537 EnsureVisible( itemId
);
1540 if (unselect_others
)
1542 if (is_single
) Unselect(); // to speed up thing
1547 if (extended_select
)
1551 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1554 // don't change the mark (m_current)
1555 SelectItemRange(m_current
, item
);
1559 bool select
=TRUE
; // the default
1561 // Check if we need to toggle hilight (ctrl mode)
1562 if (!unselect_others
)
1563 select
=!item
->IsSelected();
1565 m_current
= m_key_current
= item
;
1566 m_current
->SetHilight(select
);
1567 RefreshLine( m_current
);
1570 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1571 GetEventHandler()->ProcessEvent( event
);
1574 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1575 wxArrayTreeItemIds
&array
) const
1577 if ( item
->IsSelected() )
1578 array
.Add(wxTreeItemId(item
));
1580 if ( item
->HasChildren() )
1582 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1583 size_t count
= children
.GetCount();
1584 for ( size_t n
= 0; n
< count
; ++n
)
1585 FillArray(children
[n
], array
);
1589 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1592 wxTreeItemId idRoot
= GetRootItem();
1593 if ( idRoot
.IsOk() )
1595 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1597 //else: the tree is empty, so no selections
1599 return array
.Count();
1602 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1604 if (!item
.IsOk()) return;
1606 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1608 // first expand all parent branches
1609 wxGenericTreeItem
*parent
= gitem
->GetParent();
1613 parent
= parent
->GetParent();
1616 //if (parent) CalculatePositions();
1621 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1623 if (!item
.IsOk()) return;
1625 // We have to call this here because the label in
1626 // question might just have been added and no screen
1627 // update taken place.
1628 if (m_dirty
) wxYieldIfNeeded();
1630 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1632 // now scroll to the item
1633 int item_y
= gitem
->GetY();
1637 GetViewStart( &start_x
, &start_y
);
1638 start_y
*= PIXELS_PER_UNIT
;
1642 GetClientSize( &client_w
, &client_h
);
1644 if (item_y
< start_y
+3)
1649 m_anchor
->GetSize( x
, y
, this );
1650 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1651 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1652 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1653 // Item should appear at top
1654 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1656 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1661 m_anchor
->GetSize( x
, y
, this );
1662 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1663 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1664 item_y
+= PIXELS_PER_UNIT
+2;
1665 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1666 // Item should appear at bottom
1667 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
);
1671 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1672 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1674 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1675 wxGenericTreeItem
**item2
)
1677 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1679 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1682 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1683 const wxTreeItemId
& item2
)
1685 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1688 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1690 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1692 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1694 wxCHECK_RET( !s_treeBeingSorted
,
1695 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1697 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1698 if ( children
.Count() > 1 )
1702 s_treeBeingSorted
= this;
1703 children
.Sort(tree_ctrl_compare_func
);
1704 s_treeBeingSorted
= NULL
;
1706 //else: don't make the tree dirty as nothing changed
1709 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1711 return m_imageListNormal
;
1714 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1716 return m_imageListButtons
;
1719 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1721 return m_imageListState
;
1724 void wxGenericTreeCtrl::CalculateLineHeight()
1726 wxClientDC
dc(this);
1727 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1729 if ( m_imageListNormal
)
1731 // Calculate a m_lineHeight value from the normal Image sizes.
1732 // May be toggle off. Then wxGenericTreeCtrl will spread when
1733 // necessary (which might look ugly).
1734 int n
= m_imageListNormal
->GetImageCount();
1735 for (int i
= 0; i
< n
; i
++)
1737 int width
= 0, height
= 0;
1738 m_imageListNormal
->GetSize(i
, width
, height
);
1739 if (height
> m_lineHeight
) m_lineHeight
= height
;
1743 if (m_imageListButtons
)
1745 // Calculate a m_lineHeight value from the Button image sizes.
1746 // May be toggle off. Then wxGenericTreeCtrl will spread when
1747 // necessary (which might look ugly).
1748 int n
= m_imageListButtons
->GetImageCount();
1749 for (int i
= 0; i
< n
; i
++)
1751 int width
= 0, height
= 0;
1752 m_imageListButtons
->GetSize(i
, width
, height
);
1753 if (height
> m_lineHeight
) m_lineHeight
= height
;
1757 if (m_lineHeight
< 30)
1758 m_lineHeight
+= 2; // at least 2 pixels
1760 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
1763 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
1765 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1766 m_imageListNormal
= imageList
;
1767 m_ownsImageListNormal
= FALSE
;
1769 CalculateLineHeight();
1772 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
1774 if (m_ownsImageListState
) delete m_imageListState
;
1775 m_imageListState
= imageList
;
1776 m_ownsImageListState
= FALSE
;
1779 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
1781 if (m_ownsImageListButtons
) delete m_imageListButtons
;
1782 m_imageListButtons
= imageList
;
1783 m_ownsImageListButtons
= FALSE
;
1785 CalculateLineHeight();
1788 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
1790 SetImageList(imageList
);
1791 m_ownsImageListNormal
= TRUE
;
1794 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
1796 SetStateImageList(imageList
);
1797 m_ownsImageListState
= TRUE
;
1800 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
1802 SetButtonsImageList(imageList
);
1803 m_ownsImageListButtons
= TRUE
;
1806 // -----------------------------------------------------------------------------
1808 // -----------------------------------------------------------------------------
1810 void wxGenericTreeCtrl::AdjustMyScrollbars()
1815 m_anchor
->GetSize( x
, y
, this );
1816 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1817 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1818 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1819 int y_pos
= GetScrollPos( wxVERTICAL
);
1820 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
1824 SetScrollbars( 0, 0, 0, 0 );
1828 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
1830 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
1831 return item
->GetHeight();
1833 return m_lineHeight
;
1836 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
1838 // TODO implement "state" icon on items
1840 wxTreeItemAttr
*attr
= item
->GetAttributes();
1841 if ( attr
&& attr
->HasFont() )
1842 dc
.SetFont(attr
->GetFont());
1843 else if (item
->IsBold())
1844 dc
.SetFont(m_boldFont
);
1846 long text_w
= 0, text_h
= 0;
1847 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
1849 int image_h
= 0, image_w
= 0;
1850 int image
= item
->GetCurrentImage();
1851 if ( image
!= NO_IMAGE
)
1853 if ( m_imageListNormal
)
1855 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
1864 int total_h
= GetLineHeight(item
);
1866 if ( item
->IsSelected() )
1868 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
1873 if ( attr
&& attr
->HasBackgroundColour() )
1874 colBg
= attr
->GetBackgroundColour();
1876 colBg
= m_backgroundColour
;
1877 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
1880 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
1882 if ( item
->IsSelected() && image
!= NO_IMAGE
)
1884 // If it's selected, and there's an image, then we should
1885 // take care to leave the area under the image painted in the
1886 // background colour.
1887 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
1888 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
1892 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
1893 item
->GetWidth()+2, total_h
-offset
);
1896 if ( image
!= NO_IMAGE
)
1898 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
1899 m_imageListNormal
->Draw( image
, dc
,
1901 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
1902 wxIMAGELIST_DRAW_TRANSPARENT
);
1903 dc
.DestroyClippingRegion();
1906 dc
.SetBackgroundMode(wxTRANSPARENT
);
1907 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
1908 dc
.DrawText( item
->GetText(),
1909 (wxCoord
)(image_w
+ item
->GetX()),
1910 (wxCoord
)(item
->GetY() + extraH
));
1912 // restore normal font
1913 dc
.SetFont( m_normalFont
);
1916 // Now y stands for the top of the item, whereas it used to stand for middle !
1917 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
1919 int x
= level
*m_indent
;
1920 if (!HasFlag(wxTR_HIDE_ROOT
))
1924 else if (level
== 0)
1926 // always expand hidden root
1928 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1929 int count
= children
.Count();
1935 PaintLevel(children
[n
], dc
, 1, y
);
1936 } while (++n
< count
);
1938 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
1940 // draw line down to last child
1941 origY
+= GetLineHeight(children
[0])>>1;
1942 oldY
+= GetLineHeight(children
[n
-1])>>1;
1943 dc
.DrawLine(3, origY
, 3, oldY
);
1949 item
->SetX(x
+m_spacing
);
1952 int h
= GetLineHeight(item
);
1954 int y_mid
= y_top
+ (h
>>1);
1957 int exposed_x
= dc
.LogicalToDeviceX(0);
1958 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
1960 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
1962 if (item
->HasPlus() && HasButtons()) // should the item show a button?
1964 if (!HasFlag(wxTR_NO_LINES
))
1966 if (x
> (signed)m_indent
)
1967 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
1968 else if (HasFlag(wxTR_LINES_AT_ROOT
))
1969 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
1970 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
1973 if (m_imageListButtons
!= NULL
)
1975 // draw the image button here
1976 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
1977 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
1978 if (item
->IsSelected())
1979 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
1980 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
1981 int xx
= x
- (image_w
>>1);
1982 int yy
= y_mid
- (image_h
>>1);
1983 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
1984 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
1985 wxIMAGELIST_DRAW_TRANSPARENT
);
1986 dc
.DestroyClippingRegion();
1988 else if (HasFlag(wxTR_TWIST_BUTTONS
))
1990 // draw the twisty button here
1991 dc
.SetPen(*wxBLACK_PEN
);
1992 dc
.SetBrush(*m_hilightBrush
);
1996 if (item
->IsExpanded())
1999 button
[0].y
= y_mid
-2;
2001 button
[1].y
= y_mid
-2;
2003 button
[2].y
= y_mid
+3;
2007 button
[0].y
= y_mid
-5;
2009 button
[1].y
= y_mid
+5;
2011 button
[2].y
= y_mid
;
2014 dc
.DrawPolygon(3, button
);
2016 dc
.SetPen(m_dottedPen
);
2018 else // if (HasFlag(wxTR_HAS_BUTTONS))
2020 // draw the plus sign here
2021 dc
.SetPen(*wxGREY_PEN
);
2022 dc
.SetBrush(*wxWHITE_BRUSH
);
2023 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2024 dc
.SetPen(*wxBLACK_PEN
);
2025 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2026 if (!item
->IsExpanded())
2027 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2028 dc
.SetPen(m_dottedPen
);
2031 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2033 // draw the horizontal line here
2035 if (x
> (signed)m_indent
)
2036 x_start
-= m_indent
;
2037 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2039 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2044 // don't draw rect outline if we already have the
2045 // background color under Mac
2046 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2047 #endif // !__WXMAC__
2051 if ( item
->IsSelected() )
2053 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2057 wxTreeItemAttr
*attr
= item
->GetAttributes();
2058 if (attr
&& attr
->HasTextColour())
2059 colText
= attr
->GetTextColour();
2061 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
);
2065 dc
.SetTextForeground(colText
);
2069 PaintItem(item
, dc
);
2071 if (HasFlag(wxTR_ROW_LINES
))
2073 // if the background colour is white, choose a
2074 // contrasting color for the lines
2075 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2076 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2077 dc
.DrawLine(0, y_top
, 10000, y_top
);
2078 dc
.DrawLine(0, y
, 10000, y
);
2081 // restore DC objects
2082 dc
.SetBrush(*wxWHITE_BRUSH
);
2083 dc
.SetPen(m_dottedPen
);
2084 dc
.SetTextForeground(*wxBLACK
);
2087 if (item
->IsExpanded())
2089 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2090 int count
= children
.Count();
2097 PaintLevel(children
[n
], dc
, level
, y
);
2098 } while (++n
< count
);
2100 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2102 // draw line down to last child
2103 oldY
+= GetLineHeight(children
[n
-1])>>1;
2104 if (HasButtons()) y_mid
+= 5;
2105 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2111 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2115 if ( item
->HasPlus() )
2117 // it's a folder, indicate it by a border
2122 // draw a line under the drop target because the item will be
2124 DrawLine(item
, TRUE
/* below */);
2127 SetCursor(wxCURSOR_BULLSEYE
);
2132 SetCursor(wxCURSOR_NO_ENTRY
);
2136 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2138 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2140 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2142 wxClientDC
dc(this);
2144 dc
.SetLogicalFunction(wxINVERT
);
2145 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2147 int w
= i
->GetWidth() + 2;
2148 int h
= GetLineHeight(i
) + 2;
2150 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2153 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2155 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2157 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2159 wxClientDC
dc(this);
2161 dc
.SetLogicalFunction(wxINVERT
);
2167 y
+= GetLineHeight(i
) - 1;
2170 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2173 // -----------------------------------------------------------------------------
2174 // wxWindows callbacks
2175 // -----------------------------------------------------------------------------
2177 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2185 dc
.SetFont( m_normalFont
);
2186 dc
.SetPen( m_dottedPen
);
2188 // this is now done dynamically
2189 //if(GetImageList() == NULL)
2190 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2193 PaintLevel( m_anchor
, dc
, 0, y
);
2196 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2205 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2214 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2216 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2217 te
.m_evtKey
= event
;
2218 te
.SetEventObject( this );
2219 if ( GetEventHandler()->ProcessEvent( te
) )
2221 // intercepted by the user code
2225 if ( (m_current
== 0) || (m_key_current
== 0) )
2231 // how should the selection work for this event?
2232 bool is_multiple
, extended_select
, unselect_others
;
2233 EventFlagsToSelType(GetWindowStyleFlag(),
2235 event
.ControlDown(),
2236 is_multiple
, extended_select
, unselect_others
);
2240 // * : Expand all/Collapse all
2241 // ' ' | return : activate
2242 // up : go up (not last children!)
2244 // left : go to parent
2245 // right : open if parent and go next
2246 // home : go to root
2247 // end : go to last item without opening parents
2248 switch (event
.KeyCode())
2252 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2260 if ( !IsExpanded(m_current
) )
2263 ExpandAll(m_current
);
2266 //else: fall through to Collapse() it
2270 if (IsExpanded(m_current
))
2272 Collapse(m_current
);
2279 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2280 event
.m_item
= (long) m_current
;
2281 event
.SetEventObject( this );
2282 GetEventHandler()->ProcessEvent( event
);
2286 // up goes to the previous sibling or to the last
2287 // of its children if it's expanded
2290 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2293 prev
= GetParent( m_key_current
);
2294 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2296 break; // don't go to root if it is hidden
2301 wxTreeItemId current
= m_key_current
;
2302 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2303 if (current
== GetFirstChild( prev
, cookie
))
2305 // otherwise we return to where we came from
2306 SelectItem( prev
, unselect_others
, extended_select
);
2307 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2308 EnsureVisible( prev
);
2315 while ( IsExpanded(prev
) && HasChildren(prev
) )
2317 wxTreeItemId child
= GetLastChild(prev
);
2324 SelectItem( prev
, unselect_others
, extended_select
);
2325 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2326 EnsureVisible( prev
);
2331 // left arrow goes to the parent
2334 wxTreeItemId prev
= GetParent( m_current
);
2335 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2337 // don't go to root if it is hidden
2338 prev
= GetPrevSibling( m_current
);
2342 EnsureVisible( prev
);
2343 SelectItem( prev
, unselect_others
, extended_select
);
2349 // this works the same as the down arrow except that we
2350 // also expand the item if it wasn't expanded yet
2356 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2359 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2360 SelectItem( child
, unselect_others
, extended_select
);
2361 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2362 EnsureVisible( child
);
2366 wxTreeItemId next
= GetNextSibling( m_key_current
);
2369 wxTreeItemId current
= m_key_current
;
2370 while (current
&& !next
)
2372 current
= GetParent( current
);
2373 if (current
) next
= GetNextSibling( current
);
2378 SelectItem( next
, unselect_others
, extended_select
);
2379 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2380 EnsureVisible( next
);
2386 // <End> selects the last visible tree item
2389 wxTreeItemId last
= GetRootItem();
2391 while ( last
.IsOk() && IsExpanded(last
) )
2393 wxTreeItemId lastChild
= GetLastChild(last
);
2395 // it may happen if the item was expanded but then all of
2396 // its children have been deleted - so IsExpanded() returned
2397 // TRUE, but GetLastChild() returned invalid item
2406 EnsureVisible( last
);
2407 SelectItem( last
, unselect_others
, extended_select
);
2412 // <Home> selects the root item
2415 wxTreeItemId prev
= GetRootItem();
2417 if (HasFlag(wxTR_HIDE_ROOT
))
2420 prev
= GetFirstChild(prev
, dummy
);
2423 EnsureVisible( prev
);
2424 SelectItem( prev
, unselect_others
, extended_select
);
2433 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2435 // JACS: removed wxYieldIfNeeded() because it can cause the window
2436 // to be deleted from under us if a close window event is pending
2441 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2442 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2443 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2444 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2445 if (flags
) return wxTreeItemId();
2447 if (m_anchor
== NULL
)
2449 flags
= wxTREE_HITTEST_NOWHERE
;
2450 return wxTreeItemId();
2453 wxClientDC
dc(this);
2455 wxCoord x
= dc
.DeviceToLogicalX( point
.x
);
2456 wxCoord y
= dc
.DeviceToLogicalY( point
.y
);
2457 wxGenericTreeItem
*hit
= m_anchor
->HitTest(wxPoint(x
, y
),
2463 flags
= wxTREE_HITTEST_NOWHERE
;
2464 return wxTreeItemId();
2469 // get the bounding rectangle of the item (or of its label only)
2470 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2472 bool WXUNUSED(textOnly
)) const
2474 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2476 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2479 GetViewStart(& startX
, & startY
);
2481 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2482 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2483 rect
.width
= i
->GetWidth();
2484 //rect.height = i->GetHeight();
2485 rect
.height
= GetLineHeight(i
);
2492 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2494 if (!item
.IsOk()) return;
2496 m_currentEdit
= (wxGenericTreeItem
*) item
.m_pItem
;
2498 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2499 te
.m_item
= (long) m_currentEdit
;
2500 te
.SetEventObject( this );
2501 GetEventHandler()->ProcessEvent( te
);
2503 if (!te
.IsAllowed()) return;
2505 // We have to call this here because the label in
2506 // question might just have been added and no screen
2507 // update taken place.
2508 if (m_dirty
) wxYieldIfNeeded();
2510 wxString s
= m_currentEdit
->GetText();
2511 int x
= m_currentEdit
->GetX();
2512 int y
= m_currentEdit
->GetY();
2513 int w
= m_currentEdit
->GetWidth();
2514 int h
= m_currentEdit
->GetHeight();
2519 int image
= m_currentEdit
->GetCurrentImage();
2520 if ( image
!= NO_IMAGE
)
2522 if ( m_imageListNormal
)
2524 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2529 wxFAIL_MSG(_T("you must create an image list to use images!"));
2533 w
-= image_w
+ 4; // I don't know why +4 is needed
2535 wxClientDC
dc(this);
2537 x
= dc
.LogicalToDeviceX( x
);
2538 y
= dc
.LogicalToDeviceY( y
);
2540 wxTreeTextCtrl
*text
= new wxTreeTextCtrl(this, -1,
2550 void wxGenericTreeCtrl::OnRenameTimer()
2555 void wxGenericTreeCtrl::OnRenameAccept()
2557 // TODO if the validator fails this causes a crash
2558 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2559 le
.m_item
= (long) m_currentEdit
;
2560 le
.SetEventObject( this );
2561 le
.m_label
= m_renameRes
;
2562 GetEventHandler()->ProcessEvent( le
);
2564 if (!le
.IsAllowed()) return;
2566 SetItemText( m_currentEdit
, m_renameRes
);
2569 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2571 if ( !m_anchor
) return;
2573 // we process left mouse up event (enables in-place edit), right down
2574 // (pass to the user code), left dbl click (activate item) and
2575 // dragging/moving events for items drag-and-drop
2576 if ( !(event
.LeftDown() ||
2578 event
.RightDown() ||
2579 event
.LeftDClick() ||
2581 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2588 wxClientDC
dc(this);
2590 wxCoord x
= dc
.DeviceToLogicalX( event
.GetX() );
2591 wxCoord y
= dc
.DeviceToLogicalY( event
.GetY() );
2594 wxGenericTreeItem
*item
= m_anchor
->HitTest( wxPoint(x
,y
),
2599 if ( event
.Dragging() && !m_isDragging
)
2601 if (m_dragCount
== 0)
2602 m_dragStart
= wxPoint(x
,y
);
2606 if (m_dragCount
!= 3)
2608 // wait until user drags a bit further...
2612 wxEventType command
= event
.RightIsDown()
2613 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2614 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2616 wxTreeEvent
nevent( command
, GetId() );
2617 nevent
.m_item
= (long) m_current
;
2618 nevent
.SetEventObject(this);
2620 // by default the dragging is not supported, the user code must
2621 // explicitly allow the event for it to take place
2624 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2626 // we're going to drag this item
2627 m_isDragging
= TRUE
;
2629 // remember the old cursor because we will change it while
2631 m_oldCursor
= m_cursor
;
2633 // in a single selection control, hide the selection temporarily
2634 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2636 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2638 if ( m_oldSelection
)
2640 m_oldSelection
->SetHilight(FALSE
);
2641 RefreshLine(m_oldSelection
);
2648 else if ( event
.Moving() )
2650 if ( item
!= m_dropTarget
)
2652 // unhighlight the previous drop target
2653 DrawDropEffect(m_dropTarget
);
2655 m_dropTarget
= item
;
2657 // highlight the current drop target if any
2658 DrawDropEffect(m_dropTarget
);
2663 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2665 // erase the highlighting
2666 DrawDropEffect(m_dropTarget
);
2668 if ( m_oldSelection
)
2670 m_oldSelection
->SetHilight(TRUE
);
2671 RefreshLine(m_oldSelection
);
2672 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2675 // generate the drag end event
2676 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2678 event
.m_item
= (long) item
;
2679 event
.m_pointDrag
= wxPoint(x
, y
);
2680 event
.SetEventObject(this);
2682 (void)GetEventHandler()->ProcessEvent(event
);
2684 m_isDragging
= FALSE
;
2685 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
2689 SetCursor(m_oldCursor
);
2695 // here we process only the messages which happen on tree items
2699 if (item
== NULL
) return; /* we hit the blank area */
2701 if ( event
.RightDown() )
2703 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
2704 nevent
.m_item
= (long) item
;
2705 CalcScrolledPosition(x
, y
,
2706 &nevent
.m_pointDrag
.x
,
2707 &nevent
.m_pointDrag
.y
);
2708 nevent
.SetEventObject(this);
2709 GetEventHandler()->ProcessEvent(nevent
);
2711 else if ( event
.LeftUp() )
2715 if ( (item
== m_current
) &&
2716 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
2717 HasFlag(wxTR_EDIT_LABELS
) )
2719 if ( m_renameTimer
->IsRunning() )
2720 m_renameTimer
->Stop();
2722 m_renameTimer
->Start( 100, TRUE
);
2725 m_lastOnSame
= FALSE
;
2728 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2730 if ( event
.LeftDown() )
2732 m_lastOnSame
= item
== m_current
;
2735 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
2737 // only toggle the item for a single click, double click on
2738 // the button doesn't do anything (it toggles the item twice)
2739 if ( event
.LeftDown() )
2744 // don't select the item if the button was clicked
2748 // how should the selection work for this event?
2749 bool is_multiple
, extended_select
, unselect_others
;
2750 EventFlagsToSelType(GetWindowStyleFlag(),
2752 event
.ControlDown(),
2753 is_multiple
, extended_select
, unselect_others
);
2755 SelectItem(item
, unselect_others
, extended_select
);
2757 // For some reason, Windows isn't recognizing a left double-click,
2758 // so we need to simulate it here. Allow 200 milliseconds for now.
2759 if ( event
.LeftDClick() )
2761 // double clicking should not start editing the item label
2762 m_renameTimer
->Stop();
2763 m_lastOnSame
= FALSE
;
2765 // send activate event first
2766 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2767 nevent
.m_item
= (long) item
;
2768 CalcScrolledPosition(x
, y
,
2769 &nevent
.m_pointDrag
.x
,
2770 &nevent
.m_pointDrag
.y
);
2771 nevent
.SetEventObject( this );
2772 if ( !GetEventHandler()->ProcessEvent( nevent
) )
2774 // if the user code didn't process the activate event,
2775 // handle it ourselves by toggling the item when it is
2777 if ( item
->HasPlus() )
2787 void wxGenericTreeCtrl::OnIdle( wxIdleEvent
&WXUNUSED(event
) )
2789 /* after all changes have been done to the tree control,
2790 * we actually redraw the tree when everything is over */
2792 if (!m_dirty
) return;
2796 CalculatePositions();
2798 AdjustMyScrollbars();
2801 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2807 dc
.SetFont(m_boldFont
);
2809 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2812 // restore normal font
2813 dc
.SetFont( m_normalFont
);
2817 int image
= item
->GetCurrentImage();
2818 if ( image
!= NO_IMAGE
)
2820 if ( m_imageListNormal
)
2822 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2827 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2830 total_h
+= 2; // at least 2 pixels
2832 total_h
+= total_h
/10; // otherwise 10% extra spacing
2834 item
->SetHeight(total_h
);
2835 if (total_h
>m_lineHeight
)
2836 m_lineHeight
=total_h
;
2838 item
->SetWidth(image_w
+text_w
+2);
2841 // -----------------------------------------------------------------------------
2842 // for developper : y is now the top of the level
2843 // not the middle of it !
2844 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2846 int x
= level
*m_indent
;
2847 if (!HasFlag(wxTR_HIDE_ROOT
))
2851 else if (level
== 0)
2853 // a hidden root is not evaluated, but its
2854 // children are always calculated
2858 CalculateSize( item
, dc
);
2861 item
->SetX( x
+m_spacing
);
2863 y
+= GetLineHeight(item
);
2865 if ( !item
->IsExpanded() )
2867 // we don't need to calculate collapsed branches
2872 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2873 size_t n
, count
= children
.Count();
2875 for (n
= 0; n
< count
; ++n
)
2876 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
2879 void wxGenericTreeCtrl::CalculatePositions()
2881 if ( !m_anchor
) return;
2883 wxClientDC
dc(this);
2886 dc
.SetFont( m_normalFont
);
2888 dc
.SetPen( m_dottedPen
);
2889 //if(GetImageList() == NULL)
2890 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2893 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
2896 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
2898 if (m_dirty
) return;
2900 wxClientDC
dc(this);
2905 GetClientSize( &cw
, &ch
);
2908 rect
.x
= dc
.LogicalToDeviceX( 0 );
2910 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2913 Refresh( TRUE
, &rect
);
2915 AdjustMyScrollbars();
2918 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
2920 if (m_dirty
) return;
2922 wxClientDC
dc(this);
2927 GetClientSize( &cw
, &ch
);
2930 rect
.x
= dc
.LogicalToDeviceX( 0 );
2931 rect
.y
= dc
.LogicalToDeviceY( item
->GetY() );
2933 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
2935 Refresh( TRUE
, &rect
);
2938 void wxGenericTreeCtrl::RefreshSelected()
2940 // TODO: this is awfully inefficient, we should keep the list of all
2941 // selected items internally, should be much faster
2943 RefreshSelectedUnder(m_anchor
);
2946 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
2948 if ( item
->IsSelected() )
2951 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
2952 size_t count
= children
.GetCount();
2953 for ( size_t n
= 0; n
< count
; n
++ )
2955 RefreshSelectedUnder(children
[n
]);
2959 // ----------------------------------------------------------------------------
2960 // changing colours: we need to refresh the tree control
2961 // ----------------------------------------------------------------------------
2963 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
2965 if ( !wxWindow::SetBackgroundColour(colour
) )
2973 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
2975 if ( !wxWindow::SetForegroundColour(colour
) )
2983 #endif // wxUSE_TREECTRL