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"
44 #include "wx/mac/private.h"
47 // -----------------------------------------------------------------------------
49 // -----------------------------------------------------------------------------
51 class WXDLLEXPORT wxGenericTreeItem
;
53 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 static const int NO_IMAGE
= -1;
61 static const int PIXELS_PER_UNIT
= 10;
63 // -----------------------------------------------------------------------------
65 // -----------------------------------------------------------------------------
67 // timer used for enabling in-place edit
68 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
71 // start editing the current item after half a second (if the mouse hasn't
72 // been clicked/moved)
75 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
77 virtual void Notify();
80 wxGenericTreeCtrl
*m_owner
;
82 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
85 // control used for in-place edit
86 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
89 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
92 void OnChar( wxKeyEvent
&event
);
93 void OnKeyUp( wxKeyEvent
&event
);
94 void OnKillFocus( wxFocusEvent
&event
);
100 wxGenericTreeCtrl
*m_owner
;
101 wxGenericTreeItem
*m_itemEdited
;
102 wxString m_startValue
;
105 DECLARE_EVENT_TABLE()
106 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
109 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
110 // for a sufficiently long time
111 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
114 // reset the current prefix after half a second of inactivity
115 enum { DELAY
= 500 };
117 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
119 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
122 wxGenericTreeCtrl
*m_owner
;
124 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
128 class WXDLLEXPORT wxGenericTreeItem
132 wxGenericTreeItem() { m_data
= NULL
; }
133 wxGenericTreeItem( wxGenericTreeItem
*parent
,
134 const wxString
& text
,
137 wxTreeItemData
*data
);
139 ~wxGenericTreeItem();
142 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
144 const wxString
& GetText() const { return m_text
; }
145 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
146 { return m_images
[which
]; }
147 wxTreeItemData
*GetData() const { return m_data
; }
149 // returns the current image for the item (depending on its
150 // selected/expanded/whatever state)
151 int GetCurrentImage() const;
153 void SetText( const wxString
&text
);
154 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
155 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
157 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
159 void SetBold(bool bold
) { m_isBold
= bold
; }
161 int GetX() const { return m_x
; }
162 int GetY() const { return m_y
; }
164 void SetX(int x
) { m_x
= x
; }
165 void SetY(int y
) { m_y
= y
; }
167 int GetHeight() const { return m_height
; }
168 int GetWidth() const { return m_width
; }
170 void SetHeight(int h
) { m_height
= h
; }
171 void SetWidth(int w
) { m_width
= w
; }
173 wxGenericTreeItem
*GetParent() const { return m_parent
; }
176 // deletes all children notifying the treectrl about it if !NULL
178 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
180 // get count of all children (and grand children if 'recursively')
181 size_t GetChildrenCount(bool recursively
= TRUE
) const;
183 void Insert(wxGenericTreeItem
*child
, size_t index
)
184 { m_children
.Insert(child
, index
); }
186 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
188 // return the item at given position (or NULL if no item), onButton is
189 // TRUE if the point belongs to the item's button, otherwise it lies
190 // on the item's label
191 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
192 const wxGenericTreeCtrl
*,
196 void Expand() { m_isCollapsed
= FALSE
; }
197 void Collapse() { m_isCollapsed
= TRUE
; }
199 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
202 bool HasChildren() const { return !m_children
.IsEmpty(); }
203 bool IsSelected() const { return m_hasHilight
!= 0; }
204 bool IsExpanded() const { return !m_isCollapsed
; }
205 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
206 bool IsBold() const { return m_isBold
!= 0; }
209 // get them - may be NULL
210 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
211 // get them ensuring that the pointer is not NULL
212 wxTreeItemAttr
& Attr()
216 m_attr
= new wxTreeItemAttr
;
222 void SetAttributes(wxTreeItemAttr
*attr
)
224 if ( m_ownsAttr
) delete m_attr
;
228 // set them and delete when done
229 void AssignAttributes(wxTreeItemAttr
*attr
)
236 // since there can be very many of these, we save size by chosing
237 // the smallest representation for the elements and by ordering
238 // the members to avoid padding.
239 wxString m_text
; // label to be rendered for item
241 wxTreeItemData
*m_data
; // user-provided data
243 wxArrayGenericTreeItems m_children
; // list of children
244 wxGenericTreeItem
*m_parent
; // parent of this item
246 wxTreeItemAttr
*m_attr
; // attributes???
248 // tree ctrl images for the normal, selected, expanded and
249 // expanded+selected states
250 short m_images
[wxTreeItemIcon_Max
];
252 wxCoord m_x
; // (virtual) offset from top
253 wxCoord m_y
; // (virtual) offset from left
254 short m_width
; // width of this item
255 unsigned char m_height
; // height of this item
257 // use bitfields to save size
258 int m_isCollapsed
:1;
259 int m_hasHilight
:1; // same as focused
260 int m_hasPlus
:1; // used for item which doesn't have
261 // children but has a [+] button
262 int m_isBold
:1; // render the label in bold font
263 int m_ownsAttr
:1; // delete attribute when done
265 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
268 // =============================================================================
270 // =============================================================================
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 // translate the key or mouse event flags to the type of selection we're
278 static void EventFlagsToSelType(long style
,
282 bool &extended_select
,
283 bool &unselect_others
)
285 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
286 extended_select
= shiftDown
&& is_multiple
;
287 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
290 // check if the given item is under another one
291 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
295 if ( item
== parent
)
297 // item is a descendant of parent
301 item
= item
->GetParent();
307 // -----------------------------------------------------------------------------
308 // wxTreeRenameTimer (internal)
309 // -----------------------------------------------------------------------------
311 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
316 void wxTreeRenameTimer::Notify()
318 m_owner
->OnRenameTimer();
321 //-----------------------------------------------------------------------------
322 // wxTreeTextCtrl (internal)
323 //-----------------------------------------------------------------------------
325 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
326 EVT_CHAR (wxTreeTextCtrl::OnChar
)
327 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
328 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
331 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
332 wxGenericTreeItem
*item
)
333 : m_itemEdited(item
), m_startValue(item
->GetText())
338 int w
= m_itemEdited
->GetWidth(),
339 h
= m_itemEdited
->GetHeight();
342 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
347 int image
= item
->GetCurrentImage();
348 if ( image
!= NO_IMAGE
)
350 if ( m_owner
->m_imageListNormal
)
352 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
357 wxFAIL_MSG(_T("you must create an image list to use images!"));
361 // FIXME: what are all these hardcoded 4, 8 and 11s really?
365 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
366 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
369 bool wxTreeTextCtrl::AcceptChanges()
371 const wxString value
= GetValue();
373 if ( value
== m_startValue
)
375 // nothing changed, always accept
379 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
381 // vetoed by the user
385 // accepted, do rename the item
386 m_owner
->SetItemText(m_itemEdited
, value
);
391 void wxTreeTextCtrl::Finish()
395 m_owner
->ResetTextControl();
397 wxPendingDelete
.Append(this);
401 m_owner
->SetFocus(); // This doesn't work. TODO.
405 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
407 switch ( event
.m_keyCode
)
410 if ( !AcceptChanges() )
412 // vetoed by the user, don't disappear
419 m_owner
->OnRenameCancelled(m_itemEdited
);
427 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
431 // auto-grow the textctrl:
432 wxSize parentSize
= m_owner
->GetSize();
433 wxPoint myPos
= GetPosition();
434 wxSize mySize
= GetSize();
436 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
437 if (myPos
.x
+ sx
> parentSize
.x
)
438 sx
= parentSize
.x
- myPos
.x
;
447 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
455 if ( AcceptChanges() )
461 // -----------------------------------------------------------------------------
463 // -----------------------------------------------------------------------------
465 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
466 const wxString
& text
,
467 int image
, int selImage
,
468 wxTreeItemData
*data
)
471 m_images
[wxTreeItemIcon_Normal
] = image
;
472 m_images
[wxTreeItemIcon_Selected
] = selImage
;
473 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
474 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
479 m_isCollapsed
= TRUE
;
480 m_hasHilight
= FALSE
;
486 m_attr
= (wxTreeItemAttr
*)NULL
;
489 // We don't know the height here yet.
494 wxGenericTreeItem::~wxGenericTreeItem()
498 if (m_ownsAttr
) delete m_attr
;
500 wxASSERT_MSG( m_children
.IsEmpty(),
501 wxT("please call DeleteChildren() before deleting the item") );
504 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
506 size_t count
= m_children
.Count();
507 for ( size_t n
= 0; n
< count
; n
++ )
509 wxGenericTreeItem
*child
= m_children
[n
];
511 tree
->SendDeleteEvent(child
);
513 child
->DeleteChildren(tree
);
520 void wxGenericTreeItem::SetText( const wxString
&text
)
525 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
527 size_t count
= m_children
.Count();
531 size_t total
= count
;
532 for (size_t n
= 0; n
< count
; ++n
)
534 total
+= m_children
[n
]->GetChildrenCount();
540 void wxGenericTreeItem::GetSize( int &x
, int &y
,
541 const wxGenericTreeCtrl
*theButton
)
543 int bottomY
=m_y
+theButton
->GetLineHeight(this);
544 if ( y
< bottomY
) y
= bottomY
;
545 int width
= m_x
+ m_width
;
546 if ( x
< width
) x
= width
;
550 size_t count
= m_children
.Count();
551 for ( size_t n
= 0; n
< count
; ++n
)
553 m_children
[n
]->GetSize( x
, y
, theButton
);
558 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
559 const wxGenericTreeCtrl
*theCtrl
,
563 // for a hidden root node, don't evaluate it, but do evaluate children
564 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
567 int h
= theCtrl
->GetLineHeight(this);
568 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
570 int y_mid
= m_y
+ h
/2;
571 if (point
.y
< y_mid
)
572 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
574 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
576 int xCross
= m_x
- theCtrl
->GetSpacing();
578 // according to the drawing code the triangels are drawn
579 // at -4 , -4 from the position up to +10/+10 max
580 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
581 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
582 HasPlus() && theCtrl
->HasButtons() )
584 // 5 is the size of the plus sign
585 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
586 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
587 HasPlus() && theCtrl
->HasButtons() )
590 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
594 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
599 // assuming every image (normal and selected) has the same size!
600 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
601 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
604 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
605 flags
|= wxTREE_HITTEST_ONITEMICON
;
607 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
613 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
614 if (point
.x
> m_x
+m_width
)
615 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
620 // if children are expanded, fall through to evaluate them
621 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
625 size_t count
= m_children
.Count();
626 for ( size_t n
= 0; n
< count
; n
++ )
628 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
636 return (wxGenericTreeItem
*) NULL
;
639 int wxGenericTreeItem::GetCurrentImage() const
641 int image
= NO_IMAGE
;
646 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
649 if ( image
== NO_IMAGE
)
651 // we usually fall back to the normal item, but try just the
652 // expanded one (and not selected) first in this case
653 image
= GetImage(wxTreeItemIcon_Expanded
);
659 image
= GetImage(wxTreeItemIcon_Selected
);
662 // maybe it doesn't have the specific image we want,
663 // try the default one instead
664 if ( image
== NO_IMAGE
) image
= GetImage();
669 // -----------------------------------------------------------------------------
670 // wxGenericTreeCtrl implementation
671 // -----------------------------------------------------------------------------
673 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
675 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
676 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
677 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
678 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
679 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
680 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
681 EVT_TREE_ITEM_GETTOOLTIP(-1, wxGenericTreeCtrl::OnGetToolTip
)
684 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
686 * wxTreeCtrl has to be a real class or we have problems with
687 * the run-time information.
690 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
693 // -----------------------------------------------------------------------------
694 // construction/destruction
695 // -----------------------------------------------------------------------------
697 void wxGenericTreeCtrl::Init()
699 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
707 m_hilightBrush
= new wxBrush
709 wxSystemSettings::GetColour
711 wxSYS_COLOUR_HIGHLIGHT
716 m_hilightUnfocusedBrush
= new wxBrush
718 wxSystemSettings::GetColour
720 wxSYS_COLOUR_BTNSHADOW
725 m_imageListNormal
= m_imageListButtons
=
726 m_imageListState
= (wxImageList
*) NULL
;
727 m_ownsImageListNormal
= m_ownsImageListButtons
=
728 m_ownsImageListState
= FALSE
;
731 m_isDragging
= FALSE
;
732 m_dropTarget
= m_oldSelection
= NULL
;
736 m_renameTimer
= NULL
;
739 m_lastOnSame
= FALSE
;
741 #if defined( __WXMAC__ ) && __WXMAC_CARBON__
742 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
744 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
746 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
747 m_normalFont
.GetFamily(),
748 m_normalFont
.GetStyle(),
750 m_normalFont
.GetUnderlined(),
751 m_normalFont
.GetFaceName(),
752 m_normalFont
.GetEncoding());
755 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
760 const wxValidator
& wxVALIDATOR_PARAM(validator
),
761 const wxString
& name
)
765 wxGetOsVersion( &major
, &minor
);
767 style
&= ~wxTR_LINES_AT_ROOT
;
768 style
|= wxTR_NO_LINES
;
770 style
|= wxTR_ROW_LINES
;
773 wxScrolledWindow::Create( parent
, id
, pos
, size
,
774 style
|wxHSCROLL
|wxVSCROLL
, name
);
776 // If the tree display has no buttons, but does have
777 // connecting lines, we can use a narrower layout.
778 // It may not be a good idea to force this...
779 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
786 SetValidator( validator
);
789 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
790 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
792 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
793 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
798 wxGenericTreeCtrl::~wxGenericTreeCtrl()
800 delete m_hilightBrush
;
801 delete m_hilightUnfocusedBrush
;
805 delete m_renameTimer
;
808 if (m_ownsImageListNormal
)
809 delete m_imageListNormal
;
810 if (m_ownsImageListState
)
811 delete m_imageListState
;
812 if (m_ownsImageListButtons
)
813 delete m_imageListButtons
;
816 // -----------------------------------------------------------------------------
818 // -----------------------------------------------------------------------------
820 size_t wxGenericTreeCtrl::GetCount() const
822 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
825 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
827 m_indent
= (unsigned short) indent
;
831 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
833 m_spacing
= (unsigned short) spacing
;
837 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
839 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
841 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
844 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
846 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
848 // if we will hide the root, make sure children are visible
849 m_anchor
->SetHasPlus();
851 CalculatePositions();
854 // right now, just sets the styles. Eventually, we may
855 // want to update the inherited styles, but right now
856 // none of the parents has updatable styles
857 m_windowStyle
= styles
;
861 // -----------------------------------------------------------------------------
862 // functions to work with tree items
863 // -----------------------------------------------------------------------------
865 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
867 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
869 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
872 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
873 wxTreeItemIcon which
) const
875 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
877 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
880 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
882 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
884 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
887 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
889 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
891 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
892 return pItem
->Attr().GetTextColour();
895 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
897 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
899 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
900 return pItem
->Attr().GetBackgroundColour();
903 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
905 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
907 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
908 return pItem
->Attr().GetFont();
911 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
913 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
916 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
917 pItem
->SetText(text
);
918 CalculateSize(pItem
, dc
);
922 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
924 wxTreeItemIcon which
)
926 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
928 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
929 pItem
->SetImage(image
, which
);
932 CalculateSize(pItem
, dc
);
936 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
938 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
943 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
946 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
948 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
950 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
951 pItem
->SetHasPlus(has
);
955 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
957 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
959 // avoid redrawing the tree if no real change
960 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
961 if ( pItem
->IsBold() != bold
)
963 pItem
->SetBold(bold
);
968 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
971 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
973 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
974 pItem
->Attr().SetTextColour(col
);
978 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
981 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
983 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
984 pItem
->Attr().SetBackgroundColour(col
);
988 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
990 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
992 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
993 pItem
->Attr().SetFont(font
);
997 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
999 wxScrolledWindow::SetFont(font
);
1001 m_normalFont
= font
;
1002 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1003 m_normalFont
.GetFamily(),
1004 m_normalFont
.GetStyle(),
1006 m_normalFont
.GetUnderlined(),
1007 m_normalFont
.GetFaceName(),
1008 m_normalFont
.GetEncoding());
1014 // -----------------------------------------------------------------------------
1015 // item status inquiries
1016 // -----------------------------------------------------------------------------
1018 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1020 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1022 // An item is only visible if it's not a descendant of a collapsed item
1023 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1024 wxGenericTreeItem
* parent
= pItem
->GetParent();
1027 if (!parent
->IsExpanded())
1029 parent
= parent
->GetParent();
1033 GetViewStart(& startX
, & startY
);
1035 wxSize clientSize
= GetClientSize();
1038 if (!GetBoundingRect(item
, rect
))
1040 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1042 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1044 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1050 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1052 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1054 // consider that the item does have children if it has the "+" button: it
1055 // might not have them (if it had never been expanded yet) but then it
1056 // could have them as well and it's better to err on this side rather than
1057 // disabling some operations which are restricted to the items with
1058 // children for an item which does have them
1059 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1062 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1064 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1066 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1069 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1071 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1073 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1076 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1078 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1080 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1083 // -----------------------------------------------------------------------------
1085 // -----------------------------------------------------------------------------
1087 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1089 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1091 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1094 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1095 wxTreeItemIdValue
& cookie
) const
1097 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1100 return GetNextChild(item
, cookie
);
1103 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1104 wxTreeItemIdValue
& cookie
) const
1106 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1108 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1110 // it's ok to cast cookie to size_t, we never have indices big enough to
1111 // overflow "void *"
1112 size_t *pIndex
= (size_t *)&cookie
;
1113 if ( *pIndex
< children
.Count() )
1115 return children
.Item((*pIndex
)++);
1119 // there are no more of them
1120 return wxTreeItemId();
1124 #if WXWIN_COMPATIBILITY_2_4
1126 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1129 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1132 return GetNextChild(item
, cookie
);
1135 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1138 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1140 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1141 if ( (size_t)cookie
< children
.Count() )
1143 return children
.Item((size_t)cookie
++);
1147 // there are no more of them
1148 return wxTreeItemId();
1152 #endif // WXWIN_COMPATIBILITY_2_4
1154 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1156 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1158 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1159 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1162 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1164 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1166 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1167 wxGenericTreeItem
*parent
= i
->GetParent();
1168 if ( parent
== NULL
)
1170 // root item doesn't have any siblings
1171 return wxTreeItemId();
1174 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1175 int index
= siblings
.Index(i
);
1176 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1178 size_t n
= (size_t)(index
+ 1);
1179 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1182 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1184 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1186 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1187 wxGenericTreeItem
*parent
= i
->GetParent();
1188 if ( parent
== NULL
)
1190 // root item doesn't have any siblings
1191 return wxTreeItemId();
1194 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1195 int index
= siblings
.Index(i
);
1196 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1198 return index
== 0 ? wxTreeItemId()
1199 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1202 // Only for internal use right now, but should probably be public
1203 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1205 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1207 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1209 // First see if there are any children.
1210 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1211 if (children
.GetCount() > 0)
1213 return children
.Item(0);
1217 // Try a sibling of this or ancestor instead
1218 wxTreeItemId p
= item
;
1219 wxTreeItemId toFind
;
1222 toFind
= GetNextSibling(p
);
1223 p
= GetItemParent(p
);
1224 } while (p
.IsOk() && !toFind
.IsOk());
1229 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1231 wxTreeItemId id
= GetRootItem();
1240 } while (id
.IsOk());
1242 return wxTreeItemId();
1245 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1247 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1249 wxTreeItemId id
= item
;
1252 while (id
= GetNext(id
), id
.IsOk())
1258 return wxTreeItemId();
1261 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1263 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1265 wxFAIL_MSG(wxT("not implemented"));
1267 return wxTreeItemId();
1270 // called by wxTextTreeCtrl when it marks itself for deletion
1271 void wxGenericTreeCtrl::ResetTextControl()
1276 // find the first item starting with the given prefix after the given item
1277 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1278 const wxString
& prefixOrig
) const
1280 // match is case insensitive as this is more convenient to the user: having
1281 // to press Shift-letter to go to the item starting with a capital letter
1282 // would be too bothersome
1283 wxString prefix
= prefixOrig
.Lower();
1285 // determine the starting point: we shouldn't take the current item (this
1286 // allows to switch between two items starting with the same letter just by
1287 // pressing it) but we shouldn't jump to the next one if the user is
1288 // continuing to type as otherwise he might easily skip the item he wanted
1289 wxTreeItemId id
= idParent
;
1290 if ( prefix
.length() == 1 )
1295 // look for the item starting with the given prefix after it
1296 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1301 // if we haven't found anything...
1304 // ... wrap to the beginning
1306 if ( HasFlag(wxTR_HIDE_ROOT
) )
1308 // can't select virtual root
1312 // and try all the items (stop when we get to the one we started from)
1313 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1322 // -----------------------------------------------------------------------------
1324 // -----------------------------------------------------------------------------
1326 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1328 const wxString
& text
,
1329 int image
, int selImage
,
1330 wxTreeItemData
*data
)
1332 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1335 // should we give a warning here?
1336 return AddRoot(text
, image
, selImage
, data
);
1339 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1341 wxGenericTreeItem
*item
=
1342 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1346 data
->m_pItem
= item
;
1349 parent
->Insert( item
, previous
);
1354 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1355 int image
, int selImage
,
1356 wxTreeItemData
*data
)
1358 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1360 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1362 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1363 image
, selImage
, data
);
1366 data
->m_pItem
= m_anchor
;
1369 if (HasFlag(wxTR_HIDE_ROOT
))
1371 // if root is hidden, make sure we can navigate
1373 m_anchor
->SetHasPlus();
1375 CalculatePositions();
1378 if (!HasFlag(wxTR_MULTIPLE
))
1380 m_current
= m_key_current
= m_anchor
;
1381 m_current
->SetHilight( TRUE
);
1387 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1388 const wxString
& text
,
1389 int image
, int selImage
,
1390 wxTreeItemData
*data
)
1392 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1395 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1396 const wxTreeItemId
& idPrevious
,
1397 const wxString
& text
,
1398 int image
, int selImage
,
1399 wxTreeItemData
*data
)
1401 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1404 // should we give a warning here?
1405 return AddRoot(text
, image
, selImage
, data
);
1409 if (idPrevious
.IsOk())
1411 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1412 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1413 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1416 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1419 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1421 const wxString
& text
,
1422 int image
, int selImage
,
1423 wxTreeItemData
*data
)
1425 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1428 // should we give a warning here?
1429 return AddRoot(text
, image
, selImage
, data
);
1432 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1435 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1436 const wxString
& text
,
1437 int image
, int selImage
,
1438 wxTreeItemData
*data
)
1440 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1443 // should we give a warning here?
1444 return AddRoot(text
, image
, selImage
, data
);
1447 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1448 image
, selImage
, data
);
1451 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1453 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1454 event
.m_item
= item
;
1455 event
.SetEventObject( this );
1456 ProcessEvent( event
);
1459 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1461 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1463 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1464 item
->DeleteChildren(this);
1467 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1469 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1471 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1473 wxGenericTreeItem
*parent
= item
->GetParent();
1475 // don't keep stale pointers around!
1476 if ( IsDescendantOf(item
, m_key_current
) )
1478 // Don't silently change the selection:
1479 // do it properly in idle time, so event
1480 // handlers get called.
1482 // m_key_current = parent;
1483 m_key_current
= NULL
;
1486 // m_select_me records whether we need to select
1487 // a different item, in idle time.
1488 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1490 m_select_me
= parent
;
1493 if ( IsDescendantOf(item
, m_current
) )
1495 // Don't silently change the selection:
1496 // do it properly in idle time, so event
1497 // handlers get called.
1499 // m_current = parent;
1501 m_select_me
= parent
;
1504 // remove the item from the tree
1507 parent
->GetChildren().Remove( item
); // remove by value
1509 else // deleting the root
1511 // nothing will be left in the tree
1515 // and delete all of its children and the item itself now
1516 item
->DeleteChildren(this);
1517 SendDeleteEvent(item
);
1521 void wxGenericTreeCtrl::DeleteAllItems()
1529 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1531 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1533 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1534 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1535 _T("can't expand hidden root") );
1537 if ( !item
->HasPlus() )
1540 if ( item
->IsExpanded() )
1543 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1544 event
.m_item
= item
;
1545 event
.SetEventObject( this );
1547 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1549 // cancelled by program
1554 CalculatePositions();
1556 RefreshSubtree(item
);
1558 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1559 ProcessEvent( event
);
1562 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1564 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1567 if ( !IsExpanded(item
) )
1571 wxTreeItemIdValue cookie
;
1572 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1573 while ( child
.IsOk() )
1577 child
= GetNextChild(item
, cookie
);
1581 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1583 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1584 _T("can't collapse hidden root") );
1586 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1588 if ( !item
->IsExpanded() )
1591 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1592 event
.m_item
= item
;
1593 event
.SetEventObject( this );
1594 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1596 // cancelled by program
1602 #if 0 // TODO why should items be collapsed recursively?
1603 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1604 size_t count
= children
.Count();
1605 for ( size_t n
= 0; n
< count
; n
++ )
1607 Collapse(children
[n
]);
1611 CalculatePositions();
1613 RefreshSubtree(item
);
1615 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1616 ProcessEvent( event
);
1619 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1622 DeleteChildren(item
);
1625 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1627 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1629 if (item
->IsExpanded())
1635 void wxGenericTreeCtrl::Unselect()
1639 m_current
->SetHilight( FALSE
);
1640 RefreshLine( m_current
);
1647 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1649 if (item
->IsSelected())
1651 item
->SetHilight(FALSE
);
1655 if (item
->HasChildren())
1657 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1658 size_t count
= children
.Count();
1659 for ( size_t n
= 0; n
< count
; ++n
)
1661 UnselectAllChildren(children
[n
]);
1666 void wxGenericTreeCtrl::UnselectAll()
1668 wxTreeItemId rootItem
= GetRootItem();
1670 // the tree might not have the root item at all
1673 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1677 // Recursive function !
1678 // To stop we must have crt_item<last_item
1680 // Tag all next children, when no more children,
1681 // Move to parent (not to tag)
1682 // Keep going... if we found last_item, we stop.
1683 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1685 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1687 if (parent
== NULL
) // This is root item
1688 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1690 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1691 int index
= children
.Index(crt_item
);
1692 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1694 size_t count
= children
.Count();
1695 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1697 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1700 return TagNextChildren(parent
, last_item
, select
);
1703 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1705 crt_item
->SetHilight(select
);
1706 RefreshLine(crt_item
);
1708 if (crt_item
==last_item
)
1711 if (crt_item
->HasChildren())
1713 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1714 size_t count
= children
.Count();
1715 for ( size_t n
= 0; n
< count
; ++n
)
1717 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1725 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1729 // item2 is not necessary after item1
1730 // choice first' and 'last' between item1 and item2
1731 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1732 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1734 bool select
= m_current
->IsSelected();
1736 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1739 TagNextChildren(first
,last
,select
);
1742 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1743 bool unselect_others
,
1744 bool extended_select
)
1746 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1750 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1751 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1753 //wxCHECK_RET( ( (!unselect_others) && is_single),
1754 // wxT("this is a single selection tree") );
1756 // to keep going anyhow !!!
1759 if (item
->IsSelected())
1760 return; // nothing to do
1761 unselect_others
= TRUE
;
1762 extended_select
= FALSE
;
1764 else if ( unselect_others
&& item
->IsSelected() )
1766 // selection change if there is more than one item currently selected
1767 wxArrayTreeItemIds selected_items
;
1768 if ( GetSelections(selected_items
) == 1 )
1772 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1773 event
.m_item
= item
;
1774 event
.m_itemOld
= m_current
;
1775 event
.SetEventObject( this );
1776 // TODO : Here we don't send any selection mode yet !
1778 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1781 wxTreeItemId parent
= GetItemParent( itemId
);
1782 while (parent
.IsOk())
1784 if (!IsExpanded(parent
))
1787 parent
= GetItemParent( parent
);
1790 EnsureVisible( itemId
);
1793 if (unselect_others
)
1795 if (is_single
) Unselect(); // to speed up thing
1800 if (extended_select
)
1804 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1807 // don't change the mark (m_current)
1808 SelectItemRange(m_current
, item
);
1812 bool select
=TRUE
; // the default
1814 // Check if we need to toggle hilight (ctrl mode)
1815 if (!unselect_others
)
1816 select
=!item
->IsSelected();
1818 m_current
= m_key_current
= item
;
1819 m_current
->SetHilight(select
);
1820 RefreshLine( m_current
);
1823 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1824 GetEventHandler()->ProcessEvent( event
);
1827 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1831 DoSelectItem(itemId
);
1835 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1836 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1838 item
->SetHilight(FALSE
);
1843 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1844 wxArrayTreeItemIds
&array
) const
1846 if ( item
->IsSelected() )
1847 array
.Add(wxTreeItemId(item
));
1849 if ( item
->HasChildren() )
1851 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1852 size_t count
= children
.GetCount();
1853 for ( size_t n
= 0; n
< count
; ++n
)
1854 FillArray(children
[n
], array
);
1858 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1861 wxTreeItemId idRoot
= GetRootItem();
1862 if ( idRoot
.IsOk() )
1864 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1866 //else: the tree is empty, so no selections
1868 return array
.Count();
1871 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1873 if (!item
.IsOk()) return;
1875 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1877 // first expand all parent branches
1878 wxGenericTreeItem
*parent
= gitem
->GetParent();
1880 if ( HasFlag(wxTR_HIDE_ROOT
) )
1882 while ( parent
&& parent
!= m_anchor
)
1885 parent
= parent
->GetParent();
1893 parent
= parent
->GetParent();
1897 //if (parent) CalculatePositions();
1902 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1904 if (!item
.IsOk()) return;
1906 // We have to call this here because the label in
1907 // question might just have been added and no screen
1908 // update taken place.
1910 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1915 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1917 // now scroll to the item
1918 int item_y
= gitem
->GetY();
1922 GetViewStart( &start_x
, &start_y
);
1923 start_y
*= PIXELS_PER_UNIT
;
1927 GetClientSize( &client_w
, &client_h
);
1929 if (item_y
< start_y
+3)
1934 m_anchor
->GetSize( x
, y
, this );
1935 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1936 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1937 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1938 // Item should appear at top
1939 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1941 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1946 m_anchor
->GetSize( x
, y
, this );
1947 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1948 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1949 item_y
+= PIXELS_PER_UNIT
+2;
1950 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1951 // Item should appear at bottom
1952 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
);
1956 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1957 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1959 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1960 wxGenericTreeItem
**item2
)
1962 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1964 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1967 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1968 const wxTreeItemId
& item2
)
1970 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1973 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1975 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1977 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1979 wxCHECK_RET( !s_treeBeingSorted
,
1980 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1982 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1983 if ( children
.Count() > 1 )
1987 s_treeBeingSorted
= this;
1988 children
.Sort(tree_ctrl_compare_func
);
1989 s_treeBeingSorted
= NULL
;
1991 //else: don't make the tree dirty as nothing changed
1994 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1996 return m_imageListNormal
;
1999 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2001 return m_imageListButtons
;
2004 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2006 return m_imageListState
;
2009 void wxGenericTreeCtrl::CalculateLineHeight()
2011 wxClientDC
dc(this);
2012 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2014 if ( m_imageListNormal
)
2016 // Calculate a m_lineHeight value from the normal Image sizes.
2017 // May be toggle off. Then wxGenericTreeCtrl will spread when
2018 // necessary (which might look ugly).
2019 int n
= m_imageListNormal
->GetImageCount();
2020 for (int i
= 0; i
< n
; i
++)
2022 int width
= 0, height
= 0;
2023 m_imageListNormal
->GetSize(i
, width
, height
);
2024 if (height
> m_lineHeight
) m_lineHeight
= height
;
2028 if (m_imageListButtons
)
2030 // Calculate a m_lineHeight value from the Button image sizes.
2031 // May be toggle off. Then wxGenericTreeCtrl will spread when
2032 // necessary (which might look ugly).
2033 int n
= m_imageListButtons
->GetImageCount();
2034 for (int i
= 0; i
< n
; i
++)
2036 int width
= 0, height
= 0;
2037 m_imageListButtons
->GetSize(i
, width
, height
);
2038 if (height
> m_lineHeight
) m_lineHeight
= height
;
2042 if (m_lineHeight
< 30)
2043 m_lineHeight
+= 2; // at least 2 pixels
2045 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2048 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2050 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2051 m_imageListNormal
= imageList
;
2052 m_ownsImageListNormal
= FALSE
;
2054 // Don't do any drawing if we're setting the list to NULL,
2055 // since we may be in the process of deleting the tree control.
2057 CalculateLineHeight();
2060 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2062 if (m_ownsImageListState
) delete m_imageListState
;
2063 m_imageListState
= imageList
;
2064 m_ownsImageListState
= FALSE
;
2067 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2069 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2070 m_imageListButtons
= imageList
;
2071 m_ownsImageListButtons
= FALSE
;
2073 CalculateLineHeight();
2076 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2078 SetImageList(imageList
);
2079 m_ownsImageListNormal
= TRUE
;
2082 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2084 SetStateImageList(imageList
);
2085 m_ownsImageListState
= TRUE
;
2088 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2090 SetButtonsImageList(imageList
);
2091 m_ownsImageListButtons
= TRUE
;
2094 // -----------------------------------------------------------------------------
2096 // -----------------------------------------------------------------------------
2098 void wxGenericTreeCtrl::AdjustMyScrollbars()
2103 m_anchor
->GetSize( x
, y
, this );
2104 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2105 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2106 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2107 int y_pos
= GetScrollPos( wxVERTICAL
);
2108 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2112 SetScrollbars( 0, 0, 0, 0 );
2116 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2118 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2119 return item
->GetHeight();
2121 return m_lineHeight
;
2124 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2126 // TODO implement "state" icon on items
2128 wxTreeItemAttr
*attr
= item
->GetAttributes();
2129 if ( attr
&& attr
->HasFont() )
2130 dc
.SetFont(attr
->GetFont());
2131 else if (item
->IsBold())
2132 dc
.SetFont(m_boldFont
);
2134 long text_w
= 0, text_h
= 0;
2135 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2137 int image_h
= 0, image_w
= 0;
2138 int image
= item
->GetCurrentImage();
2139 if ( image
!= NO_IMAGE
)
2141 if ( m_imageListNormal
)
2143 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2152 int total_h
= GetLineHeight(item
);
2154 if ( item
->IsSelected() )
2156 // under mac selections are only a rectangle in case they don't have the focus
2160 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2161 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2165 dc
.SetBrush( *m_hilightBrush
) ;
2168 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2174 if ( attr
&& attr
->HasBackgroundColour() )
2175 colBg
= attr
->GetBackgroundColour();
2177 colBg
= m_backgroundColour
;
2178 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2181 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2183 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2187 DoGetPosition(&x
, &y
);
2189 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2193 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2195 // If it's selected, and there's an image, then we should
2196 // take care to leave the area under the image painted in the
2197 // background colour.
2198 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2199 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2203 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2204 item
->GetWidth()+2, total_h
-offset
);
2208 if ( image
!= NO_IMAGE
)
2210 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2211 m_imageListNormal
->Draw( image
, dc
,
2213 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2214 wxIMAGELIST_DRAW_TRANSPARENT
);
2215 dc
.DestroyClippingRegion();
2218 dc
.SetBackgroundMode(wxTRANSPARENT
);
2219 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2220 dc
.DrawText( item
->GetText(),
2221 (wxCoord
)(image_w
+ item
->GetX()),
2222 (wxCoord
)(item
->GetY() + extraH
));
2224 // restore normal font
2225 dc
.SetFont( m_normalFont
);
2228 // Now y stands for the top of the item, whereas it used to stand for middle !
2229 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2231 int x
= level
*m_indent
;
2232 if (!HasFlag(wxTR_HIDE_ROOT
))
2236 else if (level
== 0)
2238 // always expand hidden root
2240 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2241 int count
= children
.Count();
2247 PaintLevel(children
[n
], dc
, 1, y
);
2248 } while (++n
< count
);
2250 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2252 // draw line down to last child
2253 origY
+= GetLineHeight(children
[0])>>1;
2254 oldY
+= GetLineHeight(children
[n
-1])>>1;
2255 dc
.DrawLine(3, origY
, 3, oldY
);
2261 item
->SetX(x
+m_spacing
);
2264 int h
= GetLineHeight(item
);
2266 int y_mid
= y_top
+ (h
>>1);
2269 int exposed_x
= dc
.LogicalToDeviceX(0);
2270 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2272 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2276 // don't draw rect outline if we already have the
2277 // background color under Mac
2278 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2279 #endif // !__WXMAC__
2283 if ( item
->IsSelected() )
2285 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2289 wxTreeItemAttr
*attr
= item
->GetAttributes();
2290 if (attr
&& attr
->HasTextColour())
2291 colText
= attr
->GetTextColour();
2293 colText
= GetForegroundColour();
2297 dc
.SetTextForeground(colText
);
2301 PaintItem(item
, dc
);
2303 if (HasFlag(wxTR_ROW_LINES
))
2305 // if the background colour is white, choose a
2306 // contrasting color for the lines
2307 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2308 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2309 dc
.DrawLine(0, y_top
, 10000, y_top
);
2310 dc
.DrawLine(0, y
, 10000, y
);
2313 // restore DC objects
2314 dc
.SetBrush(*wxWHITE_BRUSH
);
2315 dc
.SetPen(m_dottedPen
);
2316 dc
.SetTextForeground(*wxBLACK
);
2318 if ( !HasFlag(wxTR_NO_LINES
) )
2320 // draw the horizontal line here
2322 if (x
> (signed)m_indent
)
2323 x_start
-= m_indent
;
2324 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2326 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2329 // should the item show a button?
2330 if ( item
->HasPlus() && HasButtons() )
2332 if ( m_imageListButtons
)
2334 // draw the image button here
2337 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2338 : wxTreeItemIcon_Normal
;
2339 if ( item
->IsSelected() )
2340 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2342 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2343 int xx
= x
- image_w
/2;
2344 int yy
= y_mid
- image_h
/2;
2346 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2347 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2348 wxIMAGELIST_DRAW_TRANSPARENT
);
2350 else // no custom buttons
2352 static const int wImage
= 9;
2353 static const int hImage
= 9;
2356 if (item
->IsExpanded())
2357 flag
|= wxCONTROL_EXPANDED
;
2358 if (item
== m_underMouse
)
2359 flag
|= wxCONTROL_CURRENT
;
2361 wxRendererNative::Get().DrawTreeItemButton
2365 wxRect(x
- wImage
/2,
2374 if (item
->IsExpanded())
2376 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2377 int count
= children
.Count();
2384 PaintLevel(children
[n
], dc
, level
, y
);
2385 } while (++n
< count
);
2387 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2389 // draw line down to last child
2390 oldY
+= GetLineHeight(children
[n
-1])>>1;
2391 if (HasButtons()) y_mid
+= 5;
2393 // Only draw the portion of the line that is visible, in case it is huge
2394 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2395 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2396 yOrigin
= abs(yOrigin
);
2397 GetClientSize(&width
, &height
);
2399 // Move end points to the begining/end of the view?
2400 if (y_mid
< yOrigin
)
2402 if (oldY
> yOrigin
+ height
)
2403 oldY
= yOrigin
+ height
;
2405 // after the adjustments if y_mid is larger than oldY then the line
2406 // isn't visible at all so don't draw anything
2408 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2414 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2418 if ( item
->HasPlus() )
2420 // it's a folder, indicate it by a border
2425 // draw a line under the drop target because the item will be
2427 DrawLine(item
, TRUE
/* below */);
2430 SetCursor(wxCURSOR_BULLSEYE
);
2435 SetCursor(wxCURSOR_NO_ENTRY
);
2439 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2441 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2443 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2445 wxClientDC
dc(this);
2447 dc
.SetLogicalFunction(wxINVERT
);
2448 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2450 int w
= i
->GetWidth() + 2;
2451 int h
= GetLineHeight(i
) + 2;
2453 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2456 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2458 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2460 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2462 wxClientDC
dc(this);
2464 dc
.SetLogicalFunction(wxINVERT
);
2470 y
+= GetLineHeight(i
) - 1;
2473 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2476 // -----------------------------------------------------------------------------
2477 // wxWindows callbacks
2478 // -----------------------------------------------------------------------------
2480 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2488 dc
.SetFont( m_normalFont
);
2489 dc
.SetPen( m_dottedPen
);
2491 // this is now done dynamically
2492 //if(GetImageList() == NULL)
2493 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2496 PaintLevel( m_anchor
, dc
, 0, y
);
2499 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2508 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2517 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2519 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2520 te
.m_evtKey
= event
;
2521 te
.SetEventObject( this );
2522 if ( GetEventHandler()->ProcessEvent( te
) )
2524 // intercepted by the user code
2528 if ( (m_current
== 0) || (m_key_current
== 0) )
2534 // how should the selection work for this event?
2535 bool is_multiple
, extended_select
, unselect_others
;
2536 EventFlagsToSelType(GetWindowStyleFlag(),
2538 event
.ControlDown(),
2539 is_multiple
, extended_select
, unselect_others
);
2543 // * : Expand all/Collapse all
2544 // ' ' | return : activate
2545 // up : go up (not last children!)
2547 // left : go to parent
2548 // right : open if parent and go next
2549 // home : go to root
2550 // end : go to last item without opening parents
2551 // alnum : start or continue searching for the item with this prefix
2552 int keyCode
= event
.GetKeyCode();
2557 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2565 if ( !IsExpanded(m_current
) )
2568 ExpandAll(m_current
);
2571 //else: fall through to Collapse() it
2575 if (IsExpanded(m_current
))
2577 Collapse(m_current
);
2583 if ( !event
.HasModifiers() )
2585 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2586 event
.m_item
= m_current
;
2587 event
.SetEventObject( this );
2588 GetEventHandler()->ProcessEvent( event
);
2591 // in any case, also generate the normal key event for this key,
2592 // even if we generated the ACTIVATED event above: this is what
2593 // wxMSW does and it makes sense because you might not want to
2594 // process ACTIVATED event at all and handle Space and Return
2595 // directly (and differently) which would be impossible otherwise
2599 // up goes to the previous sibling or to the last
2600 // of its children if it's expanded
2603 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2606 prev
= GetItemParent( m_key_current
);
2607 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2609 break; // don't go to root if it is hidden
2613 wxTreeItemIdValue cookie
;
2614 wxTreeItemId current
= m_key_current
;
2615 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2616 if (current
== GetFirstChild( prev
, cookie
))
2618 // otherwise we return to where we came from
2619 DoSelectItem( prev
, unselect_others
, extended_select
);
2620 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2627 while ( IsExpanded(prev
) && HasChildren(prev
) )
2629 wxTreeItemId child
= GetLastChild(prev
);
2636 DoSelectItem( prev
, unselect_others
, extended_select
);
2637 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2642 // left arrow goes to the parent
2645 wxTreeItemId prev
= GetItemParent( m_current
);
2646 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2648 // don't go to root if it is hidden
2649 prev
= GetPrevSibling( m_current
);
2653 DoSelectItem( prev
, unselect_others
, extended_select
);
2659 // this works the same as the down arrow except that we
2660 // also expand the item if it wasn't expanded yet
2666 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2668 wxTreeItemIdValue cookie
;
2669 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2670 DoSelectItem( child
, unselect_others
, extended_select
);
2671 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2675 wxTreeItemId next
= GetNextSibling( m_key_current
);
2678 wxTreeItemId current
= m_key_current
;
2679 while (current
.IsOk() && !next
)
2681 current
= GetItemParent( current
);
2682 if (current
) next
= GetNextSibling( current
);
2687 DoSelectItem( next
, unselect_others
, extended_select
);
2688 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2694 // <End> selects the last visible tree item
2697 wxTreeItemId last
= GetRootItem();
2699 while ( last
.IsOk() && IsExpanded(last
) )
2701 wxTreeItemId lastChild
= GetLastChild(last
);
2703 // it may happen if the item was expanded but then all of
2704 // its children have been deleted - so IsExpanded() returned
2705 // TRUE, but GetLastChild() returned invalid item
2714 DoSelectItem( last
, unselect_others
, extended_select
);
2719 // <Home> selects the root item
2722 wxTreeItemId prev
= GetRootItem();
2726 if ( HasFlag(wxTR_HIDE_ROOT
) )
2728 wxTreeItemIdValue cookie
;
2729 prev
= GetFirstChild(prev
, cookie
);
2734 DoSelectItem( prev
, unselect_others
, extended_select
);
2739 // do not use wxIsalnum() here
2740 if ( !event
.HasModifiers() &&
2741 ((keyCode
>= '0' && keyCode
<= '9') ||
2742 (keyCode
>= 'a' && keyCode
<= 'z') ||
2743 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2745 // find the next item starting with the given prefix
2746 char ch
= (char)keyCode
;
2748 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2759 // also start the timer to reset the current prefix if the user
2760 // doesn't press any more alnum keys soon -- we wouldn't want
2761 // to use this prefix for a new item search
2764 m_findTimer
= new wxTreeFindTimer(this);
2767 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2776 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2778 // JACS: removed wxYieldIfNeeded() because it can cause the window
2779 // to be deleted from under us if a close window event is pending
2784 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2785 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2786 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2787 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2788 if (flags
) return wxTreeItemId();
2790 if (m_anchor
== NULL
)
2792 flags
= wxTREE_HITTEST_NOWHERE
;
2793 return wxTreeItemId();
2796 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2800 flags
= wxTREE_HITTEST_NOWHERE
;
2801 return wxTreeItemId();
2806 // get the bounding rectangle of the item (or of its label only)
2807 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2809 bool WXUNUSED(textOnly
)) const
2811 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2813 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2816 GetViewStart(& startX
, & startY
);
2818 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2819 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2820 rect
.width
= i
->GetWidth();
2821 //rect.height = i->GetHeight();
2822 rect
.height
= GetLineHeight(i
);
2827 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2829 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2831 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2833 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2834 te
.m_item
= itemEdit
;
2835 te
.SetEventObject( this );
2836 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2842 // We have to call this here because the label in
2843 // question might just have been added and no screen
2844 // update taken place.
2846 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2852 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2854 m_textCtrl
->SetFocus();
2857 // returns a pointer to the text edit control if the item is being
2858 // edited, NULL otherwise (it's assumed that no more than one item may
2859 // be edited simultaneously)
2860 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2865 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2866 const wxString
& value
)
2868 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2870 le
.SetEventObject( this );
2872 le
.m_editCancelled
= FALSE
;
2874 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2877 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2879 // let owner know that the edit was cancelled
2880 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2882 le
.SetEventObject( this );
2883 le
.m_label
= wxEmptyString
;
2884 le
.m_editCancelled
= TRUE
;
2886 GetEventHandler()->ProcessEvent( le
);
2892 void wxGenericTreeCtrl::OnRenameTimer()
2897 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2899 if ( !m_anchor
) return;
2901 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2903 // Is the mouse over a tree item button?
2905 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
2906 wxGenericTreeItem
*underMouse
= thisItem
;
2908 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
2909 #endif // wxUSE_TOOLTIPS
2912 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
2913 (!event
.LeftIsDown()) &&
2915 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2923 if (underMouse
!= m_underMouse
)
2927 // unhighlight old item
2928 wxGenericTreeItem
*tmp
= m_underMouse
;
2929 m_underMouse
= NULL
;
2933 m_underMouse
= underMouse
;
2935 RefreshLine( m_underMouse
);
2939 // Determines what item we are hovering over and need a tooltip for
2940 wxTreeItemId hoverItem
= thisItem
;
2942 // We do not want a tooltip if we are dragging, or if the rename timer is running
2943 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2945 // Ask the tree control what tooltip (if any) should be shown
2946 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
2947 hevent
.m_item
= hoverItem
;
2948 hevent
.SetEventObject(this);
2950 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
2952 SetToolTip(hevent
.m_label
);
2957 // we process left mouse up event (enables in-place edit), right down
2958 // (pass to the user code), left dbl click (activate item) and
2959 // dragging/moving events for items drag-and-drop
2960 if ( !(event
.LeftDown() ||
2962 event
.RightDown() ||
2963 event
.LeftDClick() ||
2965 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2974 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2976 if ( event
.Dragging() && !m_isDragging
)
2978 if (m_dragCount
== 0)
2983 if (m_dragCount
!= 3)
2985 // wait until user drags a bit further...
2989 wxEventType command
= event
.RightIsDown()
2990 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2991 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2993 wxTreeEvent
nevent( command
, GetId() );
2994 nevent
.m_item
= m_current
;
2995 nevent
.SetEventObject(this);
2997 // by default the dragging is not supported, the user code must
2998 // explicitly allow the event for it to take place
3001 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3003 // we're going to drag this item
3004 m_isDragging
= TRUE
;
3006 // remember the old cursor because we will change it while
3008 m_oldCursor
= m_cursor
;
3010 // in a single selection control, hide the selection temporarily
3011 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3013 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3015 if ( m_oldSelection
)
3017 m_oldSelection
->SetHilight(FALSE
);
3018 RefreshLine(m_oldSelection
);
3025 else if ( event
.Moving() )
3027 if ( item
!= m_dropTarget
)
3029 // unhighlight the previous drop target
3030 DrawDropEffect(m_dropTarget
);
3032 m_dropTarget
= item
;
3034 // highlight the current drop target if any
3035 DrawDropEffect(m_dropTarget
);
3037 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3044 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3046 // erase the highlighting
3047 DrawDropEffect(m_dropTarget
);
3049 if ( m_oldSelection
)
3051 m_oldSelection
->SetHilight(TRUE
);
3052 RefreshLine(m_oldSelection
);
3053 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3056 // generate the drag end event
3057 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3059 event
.m_item
= item
;
3060 event
.m_pointDrag
= pt
;
3061 event
.SetEventObject(this);
3063 (void)GetEventHandler()->ProcessEvent(event
);
3065 m_isDragging
= FALSE
;
3066 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3070 SetCursor(m_oldCursor
);
3072 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3080 // here we process only the messages which happen on tree items
3084 if (item
== NULL
) return; /* we hit the blank area */
3086 if ( event
.RightDown() )
3088 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3089 nevent
.m_item
= item
;
3090 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3091 nevent
.SetEventObject(this);
3092 GetEventHandler()->ProcessEvent(nevent
);
3094 else if ( event
.LeftUp() )
3096 // this facilitates multiple-item drag-and-drop
3098 if (item
&& HasFlag(wxTR_MULTIPLE
))
3100 wxArrayTreeItemIds selections
;
3101 size_t count
= GetSelections(selections
);
3104 !event
.ControlDown() &&
3107 DoSelectItem(item
, true, false);
3113 if ( (item
== m_current
) &&
3114 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3115 HasFlag(wxTR_EDIT_LABELS
) )
3117 if ( m_renameTimer
)
3119 if ( m_renameTimer
->IsRunning() )
3120 m_renameTimer
->Stop();
3124 m_renameTimer
= new wxTreeRenameTimer( this );
3127 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3130 m_lastOnSame
= FALSE
;
3133 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3135 if ( event
.LeftDown() )
3137 m_lastOnSame
= item
== m_current
;
3140 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3142 // only toggle the item for a single click, double click on
3143 // the button doesn't do anything (it toggles the item twice)
3144 if ( event
.LeftDown() )
3149 // don't select the item if the button was clicked
3154 // clear the previously selected items, if the
3155 // user clicked outside of the present selection.
3156 // otherwise, perform the deselection on mouse-up.
3157 // this allows multiple drag and drop to work.
3159 if (!IsSelected(item
))
3161 // how should the selection work for this event?
3162 bool is_multiple
, extended_select
, unselect_others
;
3163 EventFlagsToSelType(GetWindowStyleFlag(),
3165 event
.ControlDown(),
3166 is_multiple
, extended_select
, unselect_others
);
3168 DoSelectItem(item
, unselect_others
, extended_select
);
3172 // For some reason, Windows isn't recognizing a left double-click,
3173 // so we need to simulate it here. Allow 200 milliseconds for now.
3174 if ( event
.LeftDClick() )
3176 // double clicking should not start editing the item label
3177 if ( m_renameTimer
)
3178 m_renameTimer
->Stop();
3180 m_lastOnSame
= FALSE
;
3182 // send activate event first
3183 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3184 nevent
.m_item
= item
;
3185 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3186 nevent
.SetEventObject( this );
3187 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3189 // if the user code didn't process the activate event,
3190 // handle it ourselves by toggling the item when it is
3192 if ( item
->HasPlus() )
3202 void wxGenericTreeCtrl::OnInternalIdle()
3204 wxWindow::OnInternalIdle();
3206 // Check if we need to select the root item
3207 // because nothing else has been selected.
3208 // Delaying it means that we can invoke event handlers
3209 // as required, when a first item is selected.
3210 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3213 SelectItem(m_select_me
);
3214 else if (GetRootItem().IsOk())
3215 SelectItem(GetRootItem());
3218 /* after all changes have been done to the tree control,
3219 * we actually redraw the tree when everything is over */
3221 if (!m_dirty
) return;
3225 CalculatePositions();
3227 AdjustMyScrollbars();
3230 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3235 wxTreeItemAttr
*attr
= item
->GetAttributes();
3236 if ( attr
&& attr
->HasFont() )
3237 dc
.SetFont(attr
->GetFont());
3238 else if ( item
->IsBold() )
3239 dc
.SetFont(m_boldFont
);
3241 dc
.SetFont(m_normalFont
);
3243 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3246 // restore normal font
3247 dc
.SetFont( m_normalFont
);
3251 int image
= item
->GetCurrentImage();
3252 if ( image
!= NO_IMAGE
)
3254 if ( m_imageListNormal
)
3256 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3261 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3264 total_h
+= 2; // at least 2 pixels
3266 total_h
+= total_h
/10; // otherwise 10% extra spacing
3268 item
->SetHeight(total_h
);
3269 if (total_h
>m_lineHeight
)
3270 m_lineHeight
=total_h
;
3272 item
->SetWidth(image_w
+text_w
+2);
3275 // -----------------------------------------------------------------------------
3276 // for developper : y is now the top of the level
3277 // not the middle of it !
3278 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3280 int x
= level
*m_indent
;
3281 if (!HasFlag(wxTR_HIDE_ROOT
))
3285 else if (level
== 0)
3287 // a hidden root is not evaluated, but its
3288 // children are always calculated
3292 CalculateSize( item
, dc
);
3295 item
->SetX( x
+m_spacing
);
3297 y
+= GetLineHeight(item
);
3299 if ( !item
->IsExpanded() )
3301 // we don't need to calculate collapsed branches
3306 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3307 size_t n
, count
= children
.Count();
3309 for (n
= 0; n
< count
; ++n
)
3310 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3313 void wxGenericTreeCtrl::CalculatePositions()
3315 if ( !m_anchor
) return;
3317 wxClientDC
dc(this);
3320 dc
.SetFont( m_normalFont
);
3322 dc
.SetPen( m_dottedPen
);
3323 //if(GetImageList() == NULL)
3324 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3327 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3330 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3332 if (m_dirty
) return;
3334 wxSize client
= GetClientSize();
3337 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3338 rect
.width
= client
.x
;
3339 rect
.height
= client
.y
;
3341 Refresh(TRUE
, &rect
);
3343 AdjustMyScrollbars();
3346 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3348 if (m_dirty
) return;
3351 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3352 rect
.width
= GetClientSize().x
;
3353 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3355 Refresh(TRUE
, &rect
);
3358 void wxGenericTreeCtrl::RefreshSelected()
3360 // TODO: this is awfully inefficient, we should keep the list of all
3361 // selected items internally, should be much faster
3363 RefreshSelectedUnder(m_anchor
);
3366 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3368 if ( item
->IsSelected() )
3371 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3372 size_t count
= children
.GetCount();
3373 for ( size_t n
= 0; n
< count
; n
++ )
3375 RefreshSelectedUnder(children
[n
]);
3379 // ----------------------------------------------------------------------------
3380 // changing colours: we need to refresh the tree control
3381 // ----------------------------------------------------------------------------
3383 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3385 if ( !wxWindow::SetBackgroundColour(colour
) )
3393 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3395 if ( !wxWindow::SetForegroundColour(colour
) )
3403 // Process the tooltip event, to speed up event processing.
3404 // Doesn't actually get a tooltip.
3405 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3410 #endif // wxUSE_TREECTRL