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 and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #include "wx/renderer.h"
43 // -----------------------------------------------------------------------------
45 // -----------------------------------------------------------------------------
47 class WXDLLEXPORT wxGenericTreeItem
;
49 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 static const int NO_IMAGE
= -1;
57 static const int PIXELS_PER_UNIT
= 10;
59 // -----------------------------------------------------------------------------
61 // -----------------------------------------------------------------------------
63 // timer used for enabling in-place edit
64 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
67 // start editing the current item after half a second (if the mouse hasn't
68 // been clicked/moved)
71 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
73 virtual void Notify();
76 wxGenericTreeCtrl
*m_owner
;
78 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
81 // control used for in-place edit
82 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
85 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
88 void OnChar( wxKeyEvent
&event
);
89 void OnKeyUp( wxKeyEvent
&event
);
90 void OnKillFocus( wxFocusEvent
&event
);
96 wxGenericTreeCtrl
*m_owner
;
97 wxGenericTreeItem
*m_itemEdited
;
98 wxString m_startValue
;
101 DECLARE_EVENT_TABLE()
102 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
105 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
106 // for a sufficiently long time
107 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
110 // reset the current prefix after half a second of inactivity
111 enum { DELAY
= 500 };
113 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
115 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
118 wxGenericTreeCtrl
*m_owner
;
120 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
124 class WXDLLEXPORT wxGenericTreeItem
128 wxGenericTreeItem() { m_data
= NULL
; }
129 wxGenericTreeItem( wxGenericTreeItem
*parent
,
130 const wxString
& text
,
133 wxTreeItemData
*data
);
135 ~wxGenericTreeItem();
138 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
140 const wxString
& GetText() const { return m_text
; }
141 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
142 { return m_images
[which
]; }
143 wxTreeItemData
*GetData() const { return m_data
; }
145 // returns the current image for the item (depending on its
146 // selected/expanded/whatever state)
147 int GetCurrentImage() const;
149 void SetText( const wxString
&text
);
150 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
151 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
153 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
155 void SetBold(bool bold
) { m_isBold
= bold
; }
157 int GetX() const { return m_x
; }
158 int GetY() const { return m_y
; }
160 void SetX(int x
) { m_x
= x
; }
161 void SetY(int y
) { m_y
= y
; }
163 int GetHeight() const { return m_height
; }
164 int GetWidth() const { return m_width
; }
166 void SetHeight(int h
) { m_height
= h
; }
167 void SetWidth(int w
) { m_width
= w
; }
169 wxGenericTreeItem
*GetParent() const { return m_parent
; }
172 // deletes all children notifying the treectrl about it if !NULL
174 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
176 // get count of all children (and grand children if 'recursively')
177 size_t GetChildrenCount(bool recursively
= TRUE
) const;
179 void Insert(wxGenericTreeItem
*child
, size_t index
)
180 { m_children
.Insert(child
, index
); }
182 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
184 // return the item at given position (or NULL if no item), onButton is
185 // TRUE if the point belongs to the item's button, otherwise it lies
186 // on the button's label
187 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
188 const wxGenericTreeCtrl
*,
192 void Expand() { m_isCollapsed
= FALSE
; }
193 void Collapse() { m_isCollapsed
= TRUE
; }
195 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
198 bool HasChildren() const { return !m_children
.IsEmpty(); }
199 bool IsSelected() const { return m_hasHilight
!= 0; }
200 bool IsExpanded() const { return !m_isCollapsed
; }
201 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
202 bool IsBold() const { return m_isBold
!= 0; }
205 // get them - may be NULL
206 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
207 // get them ensuring that the pointer is not NULL
208 wxTreeItemAttr
& Attr()
212 m_attr
= new wxTreeItemAttr
;
218 void SetAttributes(wxTreeItemAttr
*attr
)
220 if ( m_ownsAttr
) delete m_attr
;
224 // set them and delete when done
225 void AssignAttributes(wxTreeItemAttr
*attr
)
232 // since there can be very many of these, we save size by chosing
233 // the smallest representation for the elements and by ordering
234 // the members to avoid padding.
235 wxString m_text
; // label to be rendered for item
237 wxTreeItemData
*m_data
; // user-provided data
239 wxArrayGenericTreeItems m_children
; // list of children
240 wxGenericTreeItem
*m_parent
; // parent of this item
242 wxTreeItemAttr
*m_attr
; // attributes???
244 // tree ctrl images for the normal, selected, expanded and
245 // expanded+selected states
246 short m_images
[wxTreeItemIcon_Max
];
248 wxCoord m_x
; // (virtual) offset from top
249 wxCoord m_y
; // (virtual) offset from left
250 short m_width
; // width of this item
251 unsigned char m_height
; // height of this item
253 // use bitfields to save size
254 int m_isCollapsed
:1;
255 int m_hasHilight
:1; // same as focused
256 int m_hasPlus
:1; // used for item which doesn't have
257 // children but has a [+] button
258 int m_isBold
:1; // render the label in bold font
259 int m_ownsAttr
:1; // delete attribute when done
261 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
264 // =============================================================================
266 // =============================================================================
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 // translate the key or mouse event flags to the type of selection we're
274 static void EventFlagsToSelType(long style
,
278 bool &extended_select
,
279 bool &unselect_others
)
281 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
282 extended_select
= shiftDown
&& is_multiple
;
283 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
286 // check if the given item is under another one
287 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
291 if ( item
== parent
)
293 // item is a descendant of parent
297 item
= item
->GetParent();
303 // -----------------------------------------------------------------------------
304 // wxTreeRenameTimer (internal)
305 // -----------------------------------------------------------------------------
307 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
312 void wxTreeRenameTimer::Notify()
314 m_owner
->OnRenameTimer();
317 //-----------------------------------------------------------------------------
318 // wxTreeTextCtrl (internal)
319 //-----------------------------------------------------------------------------
321 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
322 EVT_CHAR (wxTreeTextCtrl::OnChar
)
323 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
324 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
327 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
328 wxGenericTreeItem
*item
)
329 : m_itemEdited(item
), m_startValue(item
->GetText())
334 int w
= m_itemEdited
->GetWidth(),
335 h
= m_itemEdited
->GetHeight();
338 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
343 int image
= item
->GetCurrentImage();
344 if ( image
!= NO_IMAGE
)
346 if ( m_owner
->m_imageListNormal
)
348 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
353 wxFAIL_MSG(_T("you must create an image list to use images!"));
357 // FIXME: what are all these hardcoded 4, 8 and 11s really?
361 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
362 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
365 bool wxTreeTextCtrl::AcceptChanges()
367 const wxString value
= GetValue();
369 if ( value
== m_startValue
)
371 // nothing changed, always accept
375 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
377 // vetoed by the user
381 // accepted, do rename the item
382 m_owner
->SetItemText(m_itemEdited
, value
);
387 void wxTreeTextCtrl::Finish()
391 m_owner
->ResetTextControl();
393 wxPendingDelete
.Append(this);
397 m_owner
->SetFocus(); // This doesn't work. TODO.
401 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
403 switch ( event
.m_keyCode
)
406 if ( !AcceptChanges() )
408 // vetoed by the user, don't disappear
415 m_owner
->OnRenameCancelled(m_itemEdited
);
423 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
427 // auto-grow the textctrl:
428 wxSize parentSize
= m_owner
->GetSize();
429 wxPoint myPos
= GetPosition();
430 wxSize mySize
= GetSize();
432 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
433 if (myPos
.x
+ sx
> parentSize
.x
)
434 sx
= parentSize
.x
- myPos
.x
;
443 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
451 if ( AcceptChanges() )
457 // -----------------------------------------------------------------------------
459 // -----------------------------------------------------------------------------
461 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
462 const wxString
& text
,
463 int image
, int selImage
,
464 wxTreeItemData
*data
)
467 m_images
[wxTreeItemIcon_Normal
] = image
;
468 m_images
[wxTreeItemIcon_Selected
] = selImage
;
469 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
470 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
475 m_isCollapsed
= TRUE
;
476 m_hasHilight
= FALSE
;
482 m_attr
= (wxTreeItemAttr
*)NULL
;
485 // We don't know the height here yet.
490 wxGenericTreeItem::~wxGenericTreeItem()
494 if (m_ownsAttr
) delete m_attr
;
496 wxASSERT_MSG( m_children
.IsEmpty(),
497 wxT("please call DeleteChildren() before deleting the item") );
500 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
502 size_t count
= m_children
.Count();
503 for ( size_t n
= 0; n
< count
; n
++ )
505 wxGenericTreeItem
*child
= m_children
[n
];
507 tree
->SendDeleteEvent(child
);
509 child
->DeleteChildren(tree
);
516 void wxGenericTreeItem::SetText( const wxString
&text
)
521 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
523 size_t count
= m_children
.Count();
527 size_t total
= count
;
528 for (size_t n
= 0; n
< count
; ++n
)
530 total
+= m_children
[n
]->GetChildrenCount();
536 void wxGenericTreeItem::GetSize( int &x
, int &y
,
537 const wxGenericTreeCtrl
*theButton
)
539 int bottomY
=m_y
+theButton
->GetLineHeight(this);
540 if ( y
< bottomY
) y
= bottomY
;
541 int width
= m_x
+ m_width
;
542 if ( x
< width
) x
= width
;
546 size_t count
= m_children
.Count();
547 for ( size_t n
= 0; n
< count
; ++n
)
549 m_children
[n
]->GetSize( x
, y
, theButton
);
554 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
555 const wxGenericTreeCtrl
*theCtrl
,
559 // for a hidden root node, don't evaluate it, but do evaluate children
560 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
563 int h
= theCtrl
->GetLineHeight(this);
564 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
566 int y_mid
= m_y
+ h
/2;
567 if (point
.y
< y_mid
)
568 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
570 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
572 int xCross
= m_x
- theCtrl
->GetSpacing();
574 // according to the drawing code the triangels are drawn
575 // at -4 , -4 from the position up to +10/+10 max
576 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
577 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
578 HasPlus() && theCtrl
->HasButtons() )
580 // 5 is the size of the plus sign
581 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
582 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
583 HasPlus() && theCtrl
->HasButtons() )
586 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
590 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
595 // assuming every image (normal and selected) has the same size!
596 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
597 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
600 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
601 flags
|= wxTREE_HITTEST_ONITEMICON
;
603 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
609 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
610 if (point
.x
> m_x
+m_width
)
611 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
616 // if children are expanded, fall through to evaluate them
617 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
621 size_t count
= m_children
.Count();
622 for ( size_t n
= 0; n
< count
; n
++ )
624 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
632 return (wxGenericTreeItem
*) NULL
;
635 int wxGenericTreeItem::GetCurrentImage() const
637 int image
= NO_IMAGE
;
642 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
645 if ( image
== NO_IMAGE
)
647 // we usually fall back to the normal item, but try just the
648 // expanded one (and not selected) first in this case
649 image
= GetImage(wxTreeItemIcon_Expanded
);
655 image
= GetImage(wxTreeItemIcon_Selected
);
658 // maybe it doesn't have the specific image we want,
659 // try the default one instead
660 if ( image
== NO_IMAGE
) image
= GetImage();
665 // -----------------------------------------------------------------------------
666 // wxGenericTreeCtrl implementation
667 // -----------------------------------------------------------------------------
669 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
671 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
672 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
673 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
674 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
675 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
676 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
679 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
681 * wxTreeCtrl has to be a real class or we have problems with
682 * the run-time information.
685 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
688 // -----------------------------------------------------------------------------
689 // construction/destruction
690 // -----------------------------------------------------------------------------
692 void wxGenericTreeCtrl::Init()
694 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
702 m_hilightBrush
= new wxBrush
704 wxSystemSettings::GetColour
706 wxSYS_COLOUR_HIGHLIGHT
711 m_hilightUnfocusedBrush
= new wxBrush
713 wxSystemSettings::GetColour
715 wxSYS_COLOUR_BTNSHADOW
720 m_imageListNormal
= m_imageListButtons
=
721 m_imageListState
= (wxImageList
*) NULL
;
722 m_ownsImageListNormal
= m_ownsImageListButtons
=
723 m_ownsImageListState
= FALSE
;
726 m_isDragging
= FALSE
;
727 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
730 m_renameTimer
= NULL
;
733 m_lastOnSame
= FALSE
;
735 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
736 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
737 m_normalFont
.GetFamily(),
738 m_normalFont
.GetStyle(),
740 m_normalFont
.GetUnderlined(),
741 m_normalFont
.GetFaceName(),
742 m_normalFont
.GetEncoding());
745 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
750 const wxValidator
& wxVALIDATOR_PARAM(validator
),
751 const wxString
& name
)
755 wxGetOsVersion( &major
, &minor
);
757 style
&= ~wxTR_LINES_AT_ROOT
;
758 style
|= wxTR_NO_LINES
;
760 style
|= wxTR_ROW_LINES
;
763 wxScrolledWindow::Create( parent
, id
, pos
, size
,
764 style
|wxHSCROLL
|wxVSCROLL
, name
);
766 // If the tree display has no buttons, but does have
767 // connecting lines, we can use a narrower layout.
768 // It may not be a good idea to force this...
769 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
776 SetValidator( validator
);
779 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
780 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
782 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
783 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
788 wxGenericTreeCtrl::~wxGenericTreeCtrl()
790 delete m_hilightBrush
;
791 delete m_hilightUnfocusedBrush
;
795 delete m_renameTimer
;
798 if (m_ownsImageListNormal
)
799 delete m_imageListNormal
;
800 if (m_ownsImageListState
)
801 delete m_imageListState
;
802 if (m_ownsImageListButtons
)
803 delete m_imageListButtons
;
806 // -----------------------------------------------------------------------------
808 // -----------------------------------------------------------------------------
810 size_t wxGenericTreeCtrl::GetCount() const
812 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
815 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
817 m_indent
= (unsigned short) indent
;
821 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
823 m_spacing
= (unsigned short) spacing
;
827 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
829 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
831 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
834 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
836 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
838 // if we will hide the root, make sure children are visible
839 m_anchor
->SetHasPlus();
841 CalculatePositions();
844 // right now, just sets the styles. Eventually, we may
845 // want to update the inherited styles, but right now
846 // none of the parents has updatable styles
847 m_windowStyle
= styles
;
851 // -----------------------------------------------------------------------------
852 // functions to work with tree items
853 // -----------------------------------------------------------------------------
855 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
857 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
859 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
862 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
863 wxTreeItemIcon which
) const
865 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
867 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
870 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
872 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
874 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
877 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
879 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
881 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
882 return pItem
->Attr().GetTextColour();
885 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
887 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
889 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
890 return pItem
->Attr().GetBackgroundColour();
893 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
895 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
897 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
898 return pItem
->Attr().GetFont();
901 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
903 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
906 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
907 pItem
->SetText(text
);
908 CalculateSize(pItem
, dc
);
912 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
914 wxTreeItemIcon which
)
916 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
918 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
919 pItem
->SetImage(image
, which
);
922 CalculateSize(pItem
, dc
);
926 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
928 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
930 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
933 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
935 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
937 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
938 pItem
->SetHasPlus(has
);
942 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
944 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
946 // avoid redrawing the tree if no real change
947 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
948 if ( pItem
->IsBold() != bold
)
950 pItem
->SetBold(bold
);
955 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
958 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
960 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
961 pItem
->Attr().SetTextColour(col
);
965 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
968 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
970 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
971 pItem
->Attr().SetBackgroundColour(col
);
975 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
977 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
979 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
980 pItem
->Attr().SetFont(font
);
984 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
986 wxScrolledWindow::SetFont(font
);
988 m_normalFont
= font
;
989 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
990 m_normalFont
.GetFamily(),
991 m_normalFont
.GetStyle(),
993 m_normalFont
.GetUnderlined(),
994 m_normalFont
.GetFaceName(),
995 m_normalFont
.GetEncoding());
1001 // -----------------------------------------------------------------------------
1002 // item status inquiries
1003 // -----------------------------------------------------------------------------
1005 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1007 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1009 // An item is only visible if it's not a descendant of a collapsed item
1010 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1011 wxGenericTreeItem
* parent
= pItem
->GetParent();
1014 if (!parent
->IsExpanded())
1016 parent
= parent
->GetParent();
1020 GetViewStart(& startX
, & startY
);
1022 wxSize clientSize
= GetClientSize();
1025 if (!GetBoundingRect(item
, rect
))
1027 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1029 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1031 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1037 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1039 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1041 // consider that the item does have children if it has the "+" button: it
1042 // might not have them (if it had never been expanded yet) but then it
1043 // could have them as well and it's better to err on this side rather than
1044 // disabling some operations which are restricted to the items with
1045 // children for an item which does have them
1046 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1049 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1051 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1053 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1056 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1058 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1060 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1063 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1065 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1067 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1070 // -----------------------------------------------------------------------------
1072 // -----------------------------------------------------------------------------
1074 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1076 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1078 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1081 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1082 wxTreeItemIdValue
& cookie
) const
1084 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1087 return GetNextChild(item
, cookie
);
1090 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1091 wxTreeItemIdValue
& cookie
) const
1093 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1095 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1097 // it's ok to cast cookie to size_t, we never have indices big enough to
1098 // overflow "void *"
1099 size_t *pIndex
= (size_t *)&cookie
;
1100 if ( *pIndex
< children
.Count() )
1102 return children
.Item((*pIndex
)++);
1106 // there are no more of them
1107 return wxTreeItemId();
1111 #if WXWIN_COMPATIBILITY_2_4
1113 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1116 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1119 return GetNextChild(item
, cookie
);
1122 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1125 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1127 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1128 if ( (size_t)cookie
< children
.Count() )
1130 return children
.Item((size_t)cookie
++);
1134 // there are no more of them
1135 return wxTreeItemId();
1139 #endif // WXWIN_COMPATIBILITY_2_4
1141 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1143 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1145 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1146 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1149 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1151 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1153 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1154 wxGenericTreeItem
*parent
= i
->GetParent();
1155 if ( parent
== NULL
)
1157 // root item doesn't have any siblings
1158 return wxTreeItemId();
1161 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1162 int index
= siblings
.Index(i
);
1163 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1165 size_t n
= (size_t)(index
+ 1);
1166 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1169 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1171 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1173 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1174 wxGenericTreeItem
*parent
= i
->GetParent();
1175 if ( parent
== NULL
)
1177 // root item doesn't have any siblings
1178 return wxTreeItemId();
1181 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1182 int index
= siblings
.Index(i
);
1183 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1185 return index
== 0 ? wxTreeItemId()
1186 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1189 // Only for internal use right now, but should probably be public
1190 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1192 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1194 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1196 // First see if there are any children.
1197 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1198 if (children
.GetCount() > 0)
1200 return children
.Item(0);
1204 // Try a sibling of this or ancestor instead
1205 wxTreeItemId p
= item
;
1206 wxTreeItemId toFind
;
1209 toFind
= GetNextSibling(p
);
1210 p
= GetItemParent(p
);
1211 } while (p
.IsOk() && !toFind
.IsOk());
1216 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1218 wxTreeItemId id
= GetRootItem();
1227 } while (id
.IsOk());
1229 return wxTreeItemId();
1232 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1234 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1236 wxTreeItemId id
= item
;
1239 while (id
= GetNext(id
), id
.IsOk())
1245 return wxTreeItemId();
1248 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1250 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1252 wxFAIL_MSG(wxT("not implemented"));
1254 return wxTreeItemId();
1257 // called by wxTextTreeCtrl when it marks itself for deletion
1258 void wxGenericTreeCtrl::ResetTextControl()
1263 // find the first item starting with the given prefix after the given item
1264 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1265 const wxString
& prefixOrig
) const
1267 // match is case insensitive as this is more convenient to the user: having
1268 // to press Shift-letter to go to the item starting with a capital letter
1269 // would be too bothersome
1270 wxString prefix
= prefixOrig
.Lower();
1272 // determine the starting point: we shouldn't take the current item (this
1273 // allows to switch between two items starting with the same letter just by
1274 // pressing it) but we shouldn't jump to the next one if the user is
1275 // continuing to type as otherwise he might easily skip the item he wanted
1276 wxTreeItemId id
= idParent
;
1277 if ( prefix
.length() == 1 )
1282 // look for the item starting with the given prefix after it
1283 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1288 // if we haven't found anything...
1291 // ... wrap to the beginning
1293 if ( HasFlag(wxTR_HIDE_ROOT
) )
1295 // can't select virtual root
1299 // and try all the items (stop when we get to the one we started from)
1300 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1309 // -----------------------------------------------------------------------------
1311 // -----------------------------------------------------------------------------
1313 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1315 const wxString
& text
,
1316 int image
, int selImage
,
1317 wxTreeItemData
*data
)
1319 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1322 // should we give a warning here?
1323 return AddRoot(text
, image
, selImage
, data
);
1326 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1328 wxGenericTreeItem
*item
=
1329 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1333 data
->m_pItem
= item
;
1336 parent
->Insert( item
, previous
);
1341 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1342 int image
, int selImage
,
1343 wxTreeItemData
*data
)
1345 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1347 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1349 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1350 image
, selImage
, data
);
1353 data
->m_pItem
= m_anchor
;
1356 if (HasFlag(wxTR_HIDE_ROOT
))
1358 // if root is hidden, make sure we can navigate
1360 m_anchor
->SetHasPlus();
1362 CalculatePositions();
1365 if (!HasFlag(wxTR_MULTIPLE
))
1367 m_current
= m_key_current
= m_anchor
;
1368 m_current
->SetHilight( TRUE
);
1374 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1375 const wxString
& text
,
1376 int image
, int selImage
,
1377 wxTreeItemData
*data
)
1379 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1382 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1383 const wxTreeItemId
& idPrevious
,
1384 const wxString
& text
,
1385 int image
, int selImage
,
1386 wxTreeItemData
*data
)
1388 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1391 // should we give a warning here?
1392 return AddRoot(text
, image
, selImage
, data
);
1396 if (idPrevious
.IsOk())
1398 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1399 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1400 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1403 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1406 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1408 const wxString
& text
,
1409 int image
, int selImage
,
1410 wxTreeItemData
*data
)
1412 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1415 // should we give a warning here?
1416 return AddRoot(text
, image
, selImage
, data
);
1419 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1422 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1423 const wxString
& text
,
1424 int image
, int selImage
,
1425 wxTreeItemData
*data
)
1427 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1430 // should we give a warning here?
1431 return AddRoot(text
, image
, selImage
, data
);
1434 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1435 image
, selImage
, data
);
1438 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1440 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1441 event
.m_item
= item
;
1442 event
.SetEventObject( this );
1443 ProcessEvent( event
);
1446 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1448 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1450 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1451 item
->DeleteChildren(this);
1454 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1456 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1458 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1460 wxGenericTreeItem
*parent
= item
->GetParent();
1462 // don't keep stale pointers around!
1463 if ( IsDescendantOf(item
, m_key_current
) )
1465 // Don't silently change the selection:
1466 // do it properly in idle time, so event
1467 // handlers get called.
1469 // m_key_current = parent;
1470 m_key_current
= NULL
;
1473 // m_select_me records whether we need to select
1474 // a different item, in idle time.
1475 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1477 m_select_me
= parent
;
1480 if ( IsDescendantOf(item
, m_current
) )
1482 // Don't silently change the selection:
1483 // do it properly in idle time, so event
1484 // handlers get called.
1486 // m_current = parent;
1488 m_select_me
= parent
;
1491 // remove the item from the tree
1494 parent
->GetChildren().Remove( item
); // remove by value
1496 else // deleting the root
1498 // nothing will be left in the tree
1502 // and delete all of its children and the item itself now
1503 item
->DeleteChildren(this);
1504 SendDeleteEvent(item
);
1508 void wxGenericTreeCtrl::DeleteAllItems()
1516 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1518 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1520 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1521 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1522 _T("can't expand hidden root") );
1524 if ( !item
->HasPlus() )
1527 if ( item
->IsExpanded() )
1530 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1531 event
.m_item
= item
;
1532 event
.SetEventObject( this );
1534 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1536 // cancelled by program
1541 CalculatePositions();
1543 RefreshSubtree(item
);
1545 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1546 ProcessEvent( event
);
1549 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1551 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1554 if ( !IsExpanded(item
) )
1558 wxTreeItemIdValue cookie
;
1559 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1560 while ( child
.IsOk() )
1564 child
= GetNextChild(item
, cookie
);
1568 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1570 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1571 _T("can't collapse hidden root") );
1573 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1575 if ( !item
->IsExpanded() )
1578 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1579 event
.m_item
= item
;
1580 event
.SetEventObject( this );
1581 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1583 // cancelled by program
1589 #if 0 // TODO why should items be collapsed recursively?
1590 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1591 size_t count
= children
.Count();
1592 for ( size_t n
= 0; n
< count
; n
++ )
1594 Collapse(children
[n
]);
1598 CalculatePositions();
1600 RefreshSubtree(item
);
1602 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1603 ProcessEvent( event
);
1606 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1609 DeleteChildren(item
);
1612 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1614 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1616 if (item
->IsExpanded())
1622 void wxGenericTreeCtrl::Unselect()
1626 m_current
->SetHilight( FALSE
);
1627 RefreshLine( m_current
);
1634 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1636 if (item
->IsSelected())
1638 item
->SetHilight(FALSE
);
1642 if (item
->HasChildren())
1644 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1645 size_t count
= children
.Count();
1646 for ( size_t n
= 0; n
< count
; ++n
)
1648 UnselectAllChildren(children
[n
]);
1653 void wxGenericTreeCtrl::UnselectAll()
1655 wxTreeItemId rootItem
= GetRootItem();
1657 // the tree might not have the root item at all
1660 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1664 // Recursive function !
1665 // To stop we must have crt_item<last_item
1667 // Tag all next children, when no more children,
1668 // Move to parent (not to tag)
1669 // Keep going... if we found last_item, we stop.
1670 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1672 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1674 if (parent
== NULL
) // This is root item
1675 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1677 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1678 int index
= children
.Index(crt_item
);
1679 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1681 size_t count
= children
.Count();
1682 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1684 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1687 return TagNextChildren(parent
, last_item
, select
);
1690 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1692 crt_item
->SetHilight(select
);
1693 RefreshLine(crt_item
);
1695 if (crt_item
==last_item
)
1698 if (crt_item
->HasChildren())
1700 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1701 size_t count
= children
.Count();
1702 for ( size_t n
= 0; n
< count
; ++n
)
1704 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1712 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1716 // item2 is not necessary after item1
1717 // choice first' and 'last' between item1 and item2
1718 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1719 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1721 bool select
= m_current
->IsSelected();
1723 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1726 TagNextChildren(first
,last
,select
);
1729 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1730 bool unselect_others
,
1731 bool extended_select
)
1733 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1737 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1738 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1740 //wxCHECK_RET( ( (!unselect_others) && is_single),
1741 // wxT("this is a single selection tree") );
1743 // to keep going anyhow !!!
1746 if (item
->IsSelected())
1747 return; // nothing to do
1748 unselect_others
= TRUE
;
1749 extended_select
= FALSE
;
1751 else if ( unselect_others
&& item
->IsSelected() )
1753 // selection change if there is more than one item currently selected
1754 wxArrayTreeItemIds selected_items
;
1755 if ( GetSelections(selected_items
) == 1 )
1759 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1760 event
.m_item
= item
;
1761 event
.m_itemOld
= m_current
;
1762 event
.SetEventObject( this );
1763 // TODO : Here we don't send any selection mode yet !
1765 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1768 wxTreeItemId parent
= GetItemParent( itemId
);
1769 while (parent
.IsOk())
1771 if (!IsExpanded(parent
))
1774 parent
= GetItemParent( parent
);
1777 EnsureVisible( itemId
);
1780 if (unselect_others
)
1782 if (is_single
) Unselect(); // to speed up thing
1787 if (extended_select
)
1791 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1794 // don't change the mark (m_current)
1795 SelectItemRange(m_current
, item
);
1799 bool select
=TRUE
; // the default
1801 // Check if we need to toggle hilight (ctrl mode)
1802 if (!unselect_others
)
1803 select
=!item
->IsSelected();
1805 m_current
= m_key_current
= item
;
1806 m_current
->SetHilight(select
);
1807 RefreshLine( m_current
);
1810 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1811 GetEventHandler()->ProcessEvent( event
);
1814 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1818 DoSelectItem(itemId
);
1822 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1823 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1825 item
->SetHilight(FALSE
);
1830 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1831 wxArrayTreeItemIds
&array
) const
1833 if ( item
->IsSelected() )
1834 array
.Add(wxTreeItemId(item
));
1836 if ( item
->HasChildren() )
1838 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1839 size_t count
= children
.GetCount();
1840 for ( size_t n
= 0; n
< count
; ++n
)
1841 FillArray(children
[n
], array
);
1845 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1848 wxTreeItemId idRoot
= GetRootItem();
1849 if ( idRoot
.IsOk() )
1851 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1853 //else: the tree is empty, so no selections
1855 return array
.Count();
1858 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1860 if (!item
.IsOk()) return;
1862 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1864 // first expand all parent branches
1865 wxGenericTreeItem
*parent
= gitem
->GetParent();
1867 if ( HasFlag(wxTR_HIDE_ROOT
) )
1869 while ( parent
&& parent
!= m_anchor
)
1872 parent
= parent
->GetParent();
1880 parent
= parent
->GetParent();
1884 //if (parent) CalculatePositions();
1889 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1891 if (!item
.IsOk()) return;
1893 // We have to call this here because the label in
1894 // question might just have been added and no screen
1895 // update taken place.
1896 if (m_dirty
) wxYieldIfNeeded();
1898 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1900 // now scroll to the item
1901 int item_y
= gitem
->GetY();
1905 GetViewStart( &start_x
, &start_y
);
1906 start_y
*= PIXELS_PER_UNIT
;
1910 GetClientSize( &client_w
, &client_h
);
1912 if (item_y
< start_y
+3)
1917 m_anchor
->GetSize( x
, y
, this );
1918 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1919 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1920 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1921 // Item should appear at top
1922 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1924 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1929 m_anchor
->GetSize( x
, y
, this );
1930 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1931 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1932 item_y
+= PIXELS_PER_UNIT
+2;
1933 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1934 // Item should appear at bottom
1935 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
);
1939 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1940 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1942 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1943 wxGenericTreeItem
**item2
)
1945 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1947 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1950 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1951 const wxTreeItemId
& item2
)
1953 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1956 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1958 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1960 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1962 wxCHECK_RET( !s_treeBeingSorted
,
1963 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1965 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1966 if ( children
.Count() > 1 )
1970 s_treeBeingSorted
= this;
1971 children
.Sort(tree_ctrl_compare_func
);
1972 s_treeBeingSorted
= NULL
;
1974 //else: don't make the tree dirty as nothing changed
1977 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1979 return m_imageListNormal
;
1982 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
1984 return m_imageListButtons
;
1987 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
1989 return m_imageListState
;
1992 void wxGenericTreeCtrl::CalculateLineHeight()
1994 wxClientDC
dc(this);
1995 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
1997 if ( m_imageListNormal
)
1999 // Calculate a m_lineHeight value from the normal Image sizes.
2000 // May be toggle off. Then wxGenericTreeCtrl will spread when
2001 // necessary (which might look ugly).
2002 int n
= m_imageListNormal
->GetImageCount();
2003 for (int i
= 0; i
< n
; i
++)
2005 int width
= 0, height
= 0;
2006 m_imageListNormal
->GetSize(i
, width
, height
);
2007 if (height
> m_lineHeight
) m_lineHeight
= height
;
2011 if (m_imageListButtons
)
2013 // Calculate a m_lineHeight value from the Button image sizes.
2014 // May be toggle off. Then wxGenericTreeCtrl will spread when
2015 // necessary (which might look ugly).
2016 int n
= m_imageListButtons
->GetImageCount();
2017 for (int i
= 0; i
< n
; i
++)
2019 int width
= 0, height
= 0;
2020 m_imageListButtons
->GetSize(i
, width
, height
);
2021 if (height
> m_lineHeight
) m_lineHeight
= height
;
2025 if (m_lineHeight
< 30)
2026 m_lineHeight
+= 2; // at least 2 pixels
2028 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2031 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2033 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2034 m_imageListNormal
= imageList
;
2035 m_ownsImageListNormal
= FALSE
;
2037 // Don't do any drawing if we're setting the list to NULL,
2038 // since we may be in the process of deleting the tree control.
2040 CalculateLineHeight();
2043 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2045 if (m_ownsImageListState
) delete m_imageListState
;
2046 m_imageListState
= imageList
;
2047 m_ownsImageListState
= FALSE
;
2050 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2052 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2053 m_imageListButtons
= imageList
;
2054 m_ownsImageListButtons
= FALSE
;
2056 CalculateLineHeight();
2059 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2061 SetImageList(imageList
);
2062 m_ownsImageListNormal
= TRUE
;
2065 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2067 SetStateImageList(imageList
);
2068 m_ownsImageListState
= TRUE
;
2071 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2073 SetButtonsImageList(imageList
);
2074 m_ownsImageListButtons
= TRUE
;
2077 // -----------------------------------------------------------------------------
2079 // -----------------------------------------------------------------------------
2081 void wxGenericTreeCtrl::AdjustMyScrollbars()
2086 m_anchor
->GetSize( x
, y
, this );
2087 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2088 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2089 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2090 int y_pos
= GetScrollPos( wxVERTICAL
);
2091 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2095 SetScrollbars( 0, 0, 0, 0 );
2099 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2101 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2102 return item
->GetHeight();
2104 return m_lineHeight
;
2107 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2109 // TODO implement "state" icon on items
2111 wxTreeItemAttr
*attr
= item
->GetAttributes();
2112 if ( attr
&& attr
->HasFont() )
2113 dc
.SetFont(attr
->GetFont());
2114 else if (item
->IsBold())
2115 dc
.SetFont(m_boldFont
);
2117 long text_w
= 0, text_h
= 0;
2118 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2120 int image_h
= 0, image_w
= 0;
2121 int image
= item
->GetCurrentImage();
2122 if ( image
!= NO_IMAGE
)
2124 if ( m_imageListNormal
)
2126 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2135 int total_h
= GetLineHeight(item
);
2137 if ( item
->IsSelected() )
2139 // under mac selections are only a rectangle in case they don't have the focus
2143 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2144 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2148 dc
.SetBrush( *m_hilightBrush
) ;
2151 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2157 if ( attr
&& attr
->HasBackgroundColour() )
2158 colBg
= attr
->GetBackgroundColour();
2160 colBg
= m_backgroundColour
;
2161 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2164 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2166 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2170 DoGetPosition(&x
, &y
);
2172 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2176 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2178 // If it's selected, and there's an image, then we should
2179 // take care to leave the area under the image painted in the
2180 // background colour.
2181 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2182 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2186 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2187 item
->GetWidth()+2, total_h
-offset
);
2191 if ( image
!= NO_IMAGE
)
2193 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2194 m_imageListNormal
->Draw( image
, dc
,
2196 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2197 wxIMAGELIST_DRAW_TRANSPARENT
);
2198 dc
.DestroyClippingRegion();
2201 dc
.SetBackgroundMode(wxTRANSPARENT
);
2202 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2203 dc
.DrawText( item
->GetText(),
2204 (wxCoord
)(image_w
+ item
->GetX()),
2205 (wxCoord
)(item
->GetY() + extraH
));
2207 // restore normal font
2208 dc
.SetFont( m_normalFont
);
2211 // Now y stands for the top of the item, whereas it used to stand for middle !
2212 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2214 int x
= level
*m_indent
;
2215 if (!HasFlag(wxTR_HIDE_ROOT
))
2219 else if (level
== 0)
2221 // always expand hidden root
2223 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2224 int count
= children
.Count();
2230 PaintLevel(children
[n
], dc
, 1, y
);
2231 } while (++n
< count
);
2233 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2235 // draw line down to last child
2236 origY
+= GetLineHeight(children
[0])>>1;
2237 oldY
+= GetLineHeight(children
[n
-1])>>1;
2238 dc
.DrawLine(3, origY
, 3, oldY
);
2244 item
->SetX(x
+m_spacing
);
2247 int h
= GetLineHeight(item
);
2249 int y_mid
= y_top
+ (h
>>1);
2252 int exposed_x
= dc
.LogicalToDeviceX(0);
2253 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2255 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2259 // don't draw rect outline if we already have the
2260 // background color under Mac
2261 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2262 #endif // !__WXMAC__
2266 if ( item
->IsSelected() )
2268 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2272 wxTreeItemAttr
*attr
= item
->GetAttributes();
2273 if (attr
&& attr
->HasTextColour())
2274 colText
= attr
->GetTextColour();
2276 colText
= GetForegroundColour();
2280 dc
.SetTextForeground(colText
);
2284 PaintItem(item
, dc
);
2286 if (HasFlag(wxTR_ROW_LINES
))
2288 // if the background colour is white, choose a
2289 // contrasting color for the lines
2290 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2291 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2292 dc
.DrawLine(0, y_top
, 10000, y_top
);
2293 dc
.DrawLine(0, y
, 10000, y
);
2296 // restore DC objects
2297 dc
.SetBrush(*wxWHITE_BRUSH
);
2298 dc
.SetPen(m_dottedPen
);
2299 dc
.SetTextForeground(*wxBLACK
);
2301 if ( !HasFlag(wxTR_NO_LINES
) )
2303 // draw the horizontal line here
2305 if (x
> (signed)m_indent
)
2306 x_start
-= m_indent
;
2307 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2309 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2312 // should the item show a button?
2313 if ( item
->HasPlus() && HasButtons() )
2315 if ( m_imageListButtons
)
2317 // draw the image button here
2320 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2321 : wxTreeItemIcon_Normal
;
2322 if ( item
->IsSelected() )
2323 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2325 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2326 int xx
= x
- image_w
/2;
2327 int yy
= y_mid
- image_h
/2;
2329 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2330 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2331 wxIMAGELIST_DRAW_TRANSPARENT
);
2333 else // no custom buttons
2335 static const int wImage
= 9;
2336 static const int hImage
= 9;
2338 wxRendererNative::Get().DrawTreeItemButton
2342 wxRect(x
- wImage
/2,
2346 ? wxCONTROL_EXPANDED
2353 if (item
->IsExpanded())
2355 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2356 int count
= children
.Count();
2363 PaintLevel(children
[n
], dc
, level
, y
);
2364 } while (++n
< count
);
2366 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2368 // draw line down to last child
2369 oldY
+= GetLineHeight(children
[n
-1])>>1;
2370 if (HasButtons()) y_mid
+= 5;
2372 // Only draw the portion of the line that is visible, in case it is huge
2373 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2374 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2375 yOrigin
= abs(yOrigin
);
2376 GetClientSize(&width
, &height
);
2378 // Move end points to the begining/end of the view?
2379 if (y_mid
< yOrigin
)
2381 if (oldY
> yOrigin
+ height
)
2382 oldY
= yOrigin
+ height
;
2384 // after the adjustments if y_mid is larger than oldY then the line
2385 // isn't visible at all so don't draw anything
2387 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2393 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2397 if ( item
->HasPlus() )
2399 // it's a folder, indicate it by a border
2404 // draw a line under the drop target because the item will be
2406 DrawLine(item
, TRUE
/* below */);
2409 SetCursor(wxCURSOR_BULLSEYE
);
2414 SetCursor(wxCURSOR_NO_ENTRY
);
2418 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2420 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2422 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2424 wxClientDC
dc(this);
2426 dc
.SetLogicalFunction(wxINVERT
);
2427 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2429 int w
= i
->GetWidth() + 2;
2430 int h
= GetLineHeight(i
) + 2;
2432 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2435 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2437 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2439 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2441 wxClientDC
dc(this);
2443 dc
.SetLogicalFunction(wxINVERT
);
2449 y
+= GetLineHeight(i
) - 1;
2452 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2455 // -----------------------------------------------------------------------------
2456 // wxWindows callbacks
2457 // -----------------------------------------------------------------------------
2459 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2467 dc
.SetFont( m_normalFont
);
2468 dc
.SetPen( m_dottedPen
);
2470 // this is now done dynamically
2471 //if(GetImageList() == NULL)
2472 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2475 PaintLevel( m_anchor
, dc
, 0, y
);
2478 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2487 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2496 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2498 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2499 te
.m_evtKey
= event
;
2500 te
.SetEventObject( this );
2501 if ( GetEventHandler()->ProcessEvent( te
) )
2503 // intercepted by the user code
2507 if ( (m_current
== 0) || (m_key_current
== 0) )
2513 // how should the selection work for this event?
2514 bool is_multiple
, extended_select
, unselect_others
;
2515 EventFlagsToSelType(GetWindowStyleFlag(),
2517 event
.ControlDown(),
2518 is_multiple
, extended_select
, unselect_others
);
2522 // * : Expand all/Collapse all
2523 // ' ' | return : activate
2524 // up : go up (not last children!)
2526 // left : go to parent
2527 // right : open if parent and go next
2528 // home : go to root
2529 // end : go to last item without opening parents
2530 // alnum : start or continue searching for the item with this prefix
2531 int keyCode
= event
.GetKeyCode();
2536 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2544 if ( !IsExpanded(m_current
) )
2547 ExpandAll(m_current
);
2550 //else: fall through to Collapse() it
2554 if (IsExpanded(m_current
))
2556 Collapse(m_current
);
2562 if ( !event
.HasModifiers() )
2564 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2565 event
.m_item
= m_current
;
2566 event
.SetEventObject( this );
2567 GetEventHandler()->ProcessEvent( event
);
2570 // in any case, also generate the normal key event for this key,
2571 // even if we generated the ACTIVATED event above: this is what
2572 // wxMSW does and it makes sense because you might not want to
2573 // process ACTIVATED event at all and handle Space and Return
2574 // directly (and differently) which would be impossible otherwise
2578 // up goes to the previous sibling or to the last
2579 // of its children if it's expanded
2582 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2585 prev
= GetItemParent( m_key_current
);
2586 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2588 break; // don't go to root if it is hidden
2592 wxTreeItemIdValue cookie
;
2593 wxTreeItemId current
= m_key_current
;
2594 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2595 if (current
== GetFirstChild( prev
, cookie
))
2597 // otherwise we return to where we came from
2598 DoSelectItem( prev
, unselect_others
, extended_select
);
2599 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2606 while ( IsExpanded(prev
) && HasChildren(prev
) )
2608 wxTreeItemId child
= GetLastChild(prev
);
2615 DoSelectItem( prev
, unselect_others
, extended_select
);
2616 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2621 // left arrow goes to the parent
2624 wxTreeItemId prev
= GetItemParent( m_current
);
2625 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2627 // don't go to root if it is hidden
2628 prev
= GetPrevSibling( m_current
);
2632 DoSelectItem( prev
, unselect_others
, extended_select
);
2638 // this works the same as the down arrow except that we
2639 // also expand the item if it wasn't expanded yet
2645 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2647 wxTreeItemIdValue cookie
;
2648 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2649 DoSelectItem( child
, unselect_others
, extended_select
);
2650 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2654 wxTreeItemId next
= GetNextSibling( m_key_current
);
2657 wxTreeItemId current
= m_key_current
;
2658 while (current
.IsOk() && !next
)
2660 current
= GetItemParent( current
);
2661 if (current
) next
= GetNextSibling( current
);
2666 DoSelectItem( next
, unselect_others
, extended_select
);
2667 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2673 // <End> selects the last visible tree item
2676 wxTreeItemId last
= GetRootItem();
2678 while ( last
.IsOk() && IsExpanded(last
) )
2680 wxTreeItemId lastChild
= GetLastChild(last
);
2682 // it may happen if the item was expanded but then all of
2683 // its children have been deleted - so IsExpanded() returned
2684 // TRUE, but GetLastChild() returned invalid item
2693 DoSelectItem( last
, unselect_others
, extended_select
);
2698 // <Home> selects the root item
2701 wxTreeItemId prev
= GetRootItem();
2705 if ( HasFlag(wxTR_HIDE_ROOT
) )
2707 wxTreeItemIdValue cookie
;
2708 prev
= GetFirstChild(prev
, cookie
);
2713 DoSelectItem( prev
, unselect_others
, extended_select
);
2718 // do not use wxIsalnum() here
2719 if ( !event
.HasModifiers() &&
2720 ((keyCode
>= '0' && keyCode
<= '9') ||
2721 (keyCode
>= 'a' && keyCode
<= 'z') ||
2722 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2724 // find the next item starting with the given prefix
2725 char ch
= (char)keyCode
;
2727 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2738 // also start the timer to reset the current prefix if the user
2739 // doesn't press any more alnum keys soon -- we wouldn't want
2740 // to use this prefix for a new item search
2743 m_findTimer
= new wxTreeFindTimer(this);
2746 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2755 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2757 // JACS: removed wxYieldIfNeeded() because it can cause the window
2758 // to be deleted from under us if a close window event is pending
2763 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2764 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2765 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2766 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2767 if (flags
) return wxTreeItemId();
2769 if (m_anchor
== NULL
)
2771 flags
= wxTREE_HITTEST_NOWHERE
;
2772 return wxTreeItemId();
2775 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2779 flags
= wxTREE_HITTEST_NOWHERE
;
2780 return wxTreeItemId();
2785 // get the bounding rectangle of the item (or of its label only)
2786 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2788 bool WXUNUSED(textOnly
)) const
2790 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2792 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2795 GetViewStart(& startX
, & startY
);
2797 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2798 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2799 rect
.width
= i
->GetWidth();
2800 //rect.height = i->GetHeight();
2801 rect
.height
= GetLineHeight(i
);
2806 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2808 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2810 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2812 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2813 te
.m_item
= itemEdit
;
2814 te
.SetEventObject( this );
2815 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2821 // We have to call this here because the label in
2822 // question might just have been added and no screen
2823 // update taken place.
2827 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2829 m_textCtrl
->SetFocus();
2832 // returns a pointer to the text edit control if the item is being
2833 // edited, NULL otherwise (it's assumed that no more than one item may
2834 // be edited simultaneously)
2835 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2840 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2841 const wxString
& value
)
2843 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2845 le
.SetEventObject( this );
2847 le
.m_editCancelled
= FALSE
;
2849 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2852 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2854 // let owner know that the edit was cancelled
2855 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2857 le
.SetEventObject( this );
2858 le
.m_label
= wxEmptyString
;
2859 le
.m_editCancelled
= TRUE
;
2861 GetEventHandler()->ProcessEvent( le
);
2867 void wxGenericTreeCtrl::OnRenameTimer()
2872 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2874 if ( !m_anchor
) return;
2877 // Determines what item we are hovering over and need a tooltip for
2878 wxTreeItemId hoverItem
= HitTest(ScreenToClient(wxGetMousePosition()));
2880 // We do not want a tooltip if we are dragging, or if the rename timer is running
2881 if (hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2883 // Ask the tree control what tooltip (if any) should be shown
2884 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
2885 hevent
.m_item
= hoverItem
;
2886 hevent
.SetEventObject(this);
2888 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
2890 SetToolTip(hevent
.m_label
);
2895 // we process left mouse up event (enables in-place edit), right down
2896 // (pass to the user code), left dbl click (activate item) and
2897 // dragging/moving events for items drag-and-drop
2898 if ( !(event
.LeftDown() ||
2900 event
.RightDown() ||
2901 event
.LeftDClick() ||
2903 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2910 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2913 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2915 if ( event
.Dragging() && !m_isDragging
)
2917 if (m_dragCount
== 0)
2922 if (m_dragCount
!= 3)
2924 // wait until user drags a bit further...
2928 wxEventType command
= event
.RightIsDown()
2929 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2930 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2932 wxTreeEvent
nevent( command
, GetId() );
2933 nevent
.m_item
= m_current
;
2934 nevent
.SetEventObject(this);
2936 // by default the dragging is not supported, the user code must
2937 // explicitly allow the event for it to take place
2940 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
2942 // we're going to drag this item
2943 m_isDragging
= TRUE
;
2945 // remember the old cursor because we will change it while
2947 m_oldCursor
= m_cursor
;
2949 // in a single selection control, hide the selection temporarily
2950 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
2952 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
2954 if ( m_oldSelection
)
2956 m_oldSelection
->SetHilight(FALSE
);
2957 RefreshLine(m_oldSelection
);
2964 else if ( event
.Moving() )
2966 if ( item
!= m_dropTarget
)
2968 // unhighlight the previous drop target
2969 DrawDropEffect(m_dropTarget
);
2971 m_dropTarget
= item
;
2973 // highlight the current drop target if any
2974 DrawDropEffect(m_dropTarget
);
2979 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
2981 // erase the highlighting
2982 DrawDropEffect(m_dropTarget
);
2984 if ( m_oldSelection
)
2986 m_oldSelection
->SetHilight(TRUE
);
2987 RefreshLine(m_oldSelection
);
2988 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
2991 // generate the drag end event
2992 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
2994 event
.m_item
= item
;
2995 event
.m_pointDrag
= pt
;
2996 event
.SetEventObject(this);
2998 (void)GetEventHandler()->ProcessEvent(event
);
3000 m_isDragging
= FALSE
;
3001 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3005 SetCursor(m_oldCursor
);
3011 // here we process only the messages which happen on tree items
3015 if (item
== NULL
) return; /* we hit the blank area */
3017 if ( event
.RightDown() )
3019 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3020 nevent
.m_item
= item
;
3021 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3022 nevent
.SetEventObject(this);
3023 GetEventHandler()->ProcessEvent(nevent
);
3025 else if ( event
.LeftUp() )
3027 // this facilitates multiple-item drag-and-drop
3029 if (item
&& HasFlag(wxTR_MULTIPLE
))
3031 wxArrayTreeItemIds selections
;
3032 size_t count
= GetSelections(selections
);
3035 !event
.ControlDown() &&
3038 DoSelectItem(item
, true, false);
3044 if ( (item
== m_current
) &&
3045 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3046 HasFlag(wxTR_EDIT_LABELS
) )
3048 if ( m_renameTimer
)
3050 if ( m_renameTimer
->IsRunning() )
3051 m_renameTimer
->Stop();
3055 m_renameTimer
= new wxTreeRenameTimer( this );
3058 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3061 m_lastOnSame
= FALSE
;
3064 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3066 if ( event
.LeftDown() )
3068 m_lastOnSame
= item
== m_current
;
3071 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3073 // only toggle the item for a single click, double click on
3074 // the button doesn't do anything (it toggles the item twice)
3075 if ( event
.LeftDown() )
3080 // don't select the item if the button was clicked
3085 // clear the previously selected items, if the
3086 // user clicked outside of the present selection.
3087 // otherwise, perform the deselection on mouse-up.
3088 // this allows multiple drag and drop to work.
3090 if (!IsSelected(item
))
3092 // how should the selection work for this event?
3093 bool is_multiple
, extended_select
, unselect_others
;
3094 EventFlagsToSelType(GetWindowStyleFlag(),
3096 event
.ControlDown(),
3097 is_multiple
, extended_select
, unselect_others
);
3099 DoSelectItem(item
, unselect_others
, extended_select
);
3103 // For some reason, Windows isn't recognizing a left double-click,
3104 // so we need to simulate it here. Allow 200 milliseconds for now.
3105 if ( event
.LeftDClick() )
3107 // double clicking should not start editing the item label
3108 if ( m_renameTimer
)
3109 m_renameTimer
->Stop();
3111 m_lastOnSame
= FALSE
;
3113 // send activate event first
3114 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3115 nevent
.m_item
= item
;
3116 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3117 nevent
.SetEventObject( this );
3118 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3120 // if the user code didn't process the activate event,
3121 // handle it ourselves by toggling the item when it is
3123 if ( item
->HasPlus() )
3133 void wxGenericTreeCtrl::OnInternalIdle()
3135 wxWindow::OnInternalIdle();
3137 // Check if we need to select the root item
3138 // because nothing else has been selected.
3139 // Delaying it means that we can invoke event handlers
3140 // as required, when a first item is selected.
3141 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3144 SelectItem(m_select_me
);
3145 else if (GetRootItem().IsOk())
3146 SelectItem(GetRootItem());
3149 /* after all changes have been done to the tree control,
3150 * we actually redraw the tree when everything is over */
3152 if (!m_dirty
) return;
3156 CalculatePositions();
3158 AdjustMyScrollbars();
3161 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3166 wxTreeItemAttr
*attr
= item
->GetAttributes();
3167 if ( attr
&& attr
->HasFont() )
3168 dc
.SetFont(attr
->GetFont());
3169 else if ( item
->IsBold() )
3170 dc
.SetFont(m_boldFont
);
3172 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3175 // restore normal font
3176 dc
.SetFont( m_normalFont
);
3180 int image
= item
->GetCurrentImage();
3181 if ( image
!= NO_IMAGE
)
3183 if ( m_imageListNormal
)
3185 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3190 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3193 total_h
+= 2; // at least 2 pixels
3195 total_h
+= total_h
/10; // otherwise 10% extra spacing
3197 item
->SetHeight(total_h
);
3198 if (total_h
>m_lineHeight
)
3199 m_lineHeight
=total_h
;
3201 item
->SetWidth(image_w
+text_w
+2);
3204 // -----------------------------------------------------------------------------
3205 // for developper : y is now the top of the level
3206 // not the middle of it !
3207 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3209 int x
= level
*m_indent
;
3210 if (!HasFlag(wxTR_HIDE_ROOT
))
3214 else if (level
== 0)
3216 // a hidden root is not evaluated, but its
3217 // children are always calculated
3221 CalculateSize( item
, dc
);
3224 item
->SetX( x
+m_spacing
);
3226 y
+= GetLineHeight(item
);
3228 if ( !item
->IsExpanded() )
3230 // we don't need to calculate collapsed branches
3235 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3236 size_t n
, count
= children
.Count();
3238 for (n
= 0; n
< count
; ++n
)
3239 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3242 void wxGenericTreeCtrl::CalculatePositions()
3244 if ( !m_anchor
) return;
3246 wxClientDC
dc(this);
3249 dc
.SetFont( m_normalFont
);
3251 dc
.SetPen( m_dottedPen
);
3252 //if(GetImageList() == NULL)
3253 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3256 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3259 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3261 if (m_dirty
) return;
3263 wxSize client
= GetClientSize();
3266 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3267 rect
.width
= client
.x
;
3268 rect
.height
= client
.y
;
3270 Refresh(TRUE
, &rect
);
3272 AdjustMyScrollbars();
3275 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3277 if (m_dirty
) return;
3280 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3281 rect
.width
= GetClientSize().x
;
3282 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3284 Refresh(TRUE
, &rect
);
3287 void wxGenericTreeCtrl::RefreshSelected()
3289 // TODO: this is awfully inefficient, we should keep the list of all
3290 // selected items internally, should be much faster
3292 RefreshSelectedUnder(m_anchor
);
3295 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3297 if ( item
->IsSelected() )
3300 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3301 size_t count
= children
.GetCount();
3302 for ( size_t n
= 0; n
< count
; n
++ )
3304 RefreshSelectedUnder(children
[n
]);
3308 // ----------------------------------------------------------------------------
3309 // changing colours: we need to refresh the tree control
3310 // ----------------------------------------------------------------------------
3312 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3314 if ( !wxWindow::SetBackgroundColour(colour
) )
3322 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3324 if ( !wxWindow::SetForegroundColour(colour
) )
3332 #endif // wxUSE_TREECTRL