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(__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
;
741 m_lastOnSame
= FALSE
;
743 #if defined( __WXMAC__ ) && __WXMAC_CARBON__
744 m_normalFont
.MacCreateThemeFont( kThemeViewsFont
) ;
746 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
748 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
749 m_normalFont
.GetFamily(),
750 m_normalFont
.GetStyle(),
752 m_normalFont
.GetUnderlined(),
753 m_normalFont
.GetFaceName(),
754 m_normalFont
.GetEncoding());
757 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
762 const wxValidator
& wxVALIDATOR_PARAM(validator
),
763 const wxString
& name
)
767 wxGetOsVersion( &major
, &minor
);
769 style
&= ~wxTR_LINES_AT_ROOT
;
770 style
|= wxTR_NO_LINES
;
772 style
|= wxTR_ROW_LINES
;
775 wxScrolledWindow::Create( parent
, id
, pos
, size
,
776 style
|wxHSCROLL
|wxVSCROLL
, name
);
778 // If the tree display has no buttons, but does have
779 // connecting lines, we can use a narrower layout.
780 // It may not be a good idea to force this...
781 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
788 SetValidator( validator
);
791 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
792 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
794 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
795 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
800 wxGenericTreeCtrl::~wxGenericTreeCtrl()
802 delete m_hilightBrush
;
803 delete m_hilightUnfocusedBrush
;
807 delete m_renameTimer
;
810 if (m_ownsImageListNormal
)
811 delete m_imageListNormal
;
812 if (m_ownsImageListState
)
813 delete m_imageListState
;
814 if (m_ownsImageListButtons
)
815 delete m_imageListButtons
;
818 // -----------------------------------------------------------------------------
820 // -----------------------------------------------------------------------------
822 size_t wxGenericTreeCtrl::GetCount() const
824 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
827 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
829 m_indent
= (unsigned short) indent
;
833 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
835 m_spacing
= (unsigned short) spacing
;
839 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
841 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
843 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
846 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
848 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
850 // if we will hide the root, make sure children are visible
851 m_anchor
->SetHasPlus();
853 CalculatePositions();
856 // right now, just sets the styles. Eventually, we may
857 // want to update the inherited styles, but right now
858 // none of the parents has updatable styles
859 m_windowStyle
= styles
;
863 // -----------------------------------------------------------------------------
864 // functions to work with tree items
865 // -----------------------------------------------------------------------------
867 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
869 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
871 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
874 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
875 wxTreeItemIcon which
) const
877 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
879 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
882 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
884 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
886 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
889 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
891 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
893 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
894 return pItem
->Attr().GetTextColour();
897 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
899 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
901 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
902 return pItem
->Attr().GetBackgroundColour();
905 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
907 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
909 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
910 return pItem
->Attr().GetFont();
913 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
915 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
918 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
919 pItem
->SetText(text
);
920 CalculateSize(pItem
, dc
);
924 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
926 wxTreeItemIcon which
)
928 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
930 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
931 pItem
->SetImage(image
, which
);
934 CalculateSize(pItem
, dc
);
938 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
940 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
945 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
948 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
950 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
952 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
953 pItem
->SetHasPlus(has
);
957 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
959 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
961 // avoid redrawing the tree if no real change
962 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
963 if ( pItem
->IsBold() != bold
)
965 pItem
->SetBold(bold
);
970 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
973 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
975 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
976 pItem
->Attr().SetTextColour(col
);
980 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
983 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
985 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
986 pItem
->Attr().SetBackgroundColour(col
);
990 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
992 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
994 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
995 pItem
->Attr().SetFont(font
);
999 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1001 wxScrolledWindow::SetFont(font
);
1003 m_normalFont
= font
;
1004 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1005 m_normalFont
.GetFamily(),
1006 m_normalFont
.GetStyle(),
1008 m_normalFont
.GetUnderlined(),
1009 m_normalFont
.GetFaceName(),
1010 m_normalFont
.GetEncoding());
1016 // -----------------------------------------------------------------------------
1017 // item status inquiries
1018 // -----------------------------------------------------------------------------
1020 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1022 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1024 // An item is only visible if it's not a descendant of a collapsed item
1025 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1026 wxGenericTreeItem
* parent
= pItem
->GetParent();
1029 if (!parent
->IsExpanded())
1031 parent
= parent
->GetParent();
1035 GetViewStart(& startX
, & startY
);
1037 wxSize clientSize
= GetClientSize();
1040 if (!GetBoundingRect(item
, rect
))
1042 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1044 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1046 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1052 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1054 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1056 // consider that the item does have children if it has the "+" button: it
1057 // might not have them (if it had never been expanded yet) but then it
1058 // could have them as well and it's better to err on this side rather than
1059 // disabling some operations which are restricted to the items with
1060 // children for an item which does have them
1061 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1064 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1066 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1068 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1071 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1073 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1075 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1078 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1080 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1082 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1085 // -----------------------------------------------------------------------------
1087 // -----------------------------------------------------------------------------
1089 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1091 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1093 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1096 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1097 wxTreeItemIdValue
& cookie
) const
1099 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1102 return GetNextChild(item
, cookie
);
1105 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1106 wxTreeItemIdValue
& cookie
) const
1108 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1110 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1112 // it's ok to cast cookie to size_t, we never have indices big enough to
1113 // overflow "void *"
1114 size_t *pIndex
= (size_t *)&cookie
;
1115 if ( *pIndex
< children
.Count() )
1117 return children
.Item((*pIndex
)++);
1121 // there are no more of them
1122 return wxTreeItemId();
1126 #if WXWIN_COMPATIBILITY_2_4
1128 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1131 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1134 return GetNextChild(item
, cookie
);
1137 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1140 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1142 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1143 if ( (size_t)cookie
< children
.Count() )
1145 return children
.Item((size_t)cookie
++);
1149 // there are no more of them
1150 return wxTreeItemId();
1154 #endif // WXWIN_COMPATIBILITY_2_4
1156 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1158 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1160 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1161 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1164 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1166 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1168 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1169 wxGenericTreeItem
*parent
= i
->GetParent();
1170 if ( parent
== NULL
)
1172 // root item doesn't have any siblings
1173 return wxTreeItemId();
1176 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1177 int index
= siblings
.Index(i
);
1178 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1180 size_t n
= (size_t)(index
+ 1);
1181 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1184 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1186 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1188 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1189 wxGenericTreeItem
*parent
= i
->GetParent();
1190 if ( parent
== NULL
)
1192 // root item doesn't have any siblings
1193 return wxTreeItemId();
1196 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1197 int index
= siblings
.Index(i
);
1198 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1200 return index
== 0 ? wxTreeItemId()
1201 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1204 // Only for internal use right now, but should probably be public
1205 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1207 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1209 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1211 // First see if there are any children.
1212 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1213 if (children
.GetCount() > 0)
1215 return children
.Item(0);
1219 // Try a sibling of this or ancestor instead
1220 wxTreeItemId p
= item
;
1221 wxTreeItemId toFind
;
1224 toFind
= GetNextSibling(p
);
1225 p
= GetItemParent(p
);
1226 } while (p
.IsOk() && !toFind
.IsOk());
1231 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1233 wxTreeItemId id
= GetRootItem();
1242 } while (id
.IsOk());
1244 return wxTreeItemId();
1247 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1249 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1251 wxTreeItemId id
= item
;
1254 while (id
= GetNext(id
), id
.IsOk())
1260 return wxTreeItemId();
1263 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1265 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1267 wxFAIL_MSG(wxT("not implemented"));
1269 return wxTreeItemId();
1272 // called by wxTextTreeCtrl when it marks itself for deletion
1273 void wxGenericTreeCtrl::ResetTextControl()
1278 // find the first item starting with the given prefix after the given item
1279 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1280 const wxString
& prefixOrig
) const
1282 // match is case insensitive as this is more convenient to the user: having
1283 // to press Shift-letter to go to the item starting with a capital letter
1284 // would be too bothersome
1285 wxString prefix
= prefixOrig
.Lower();
1287 // determine the starting point: we shouldn't take the current item (this
1288 // allows to switch between two items starting with the same letter just by
1289 // pressing it) but we shouldn't jump to the next one if the user is
1290 // continuing to type as otherwise he might easily skip the item he wanted
1291 wxTreeItemId id
= idParent
;
1292 if ( prefix
.length() == 1 )
1297 // look for the item starting with the given prefix after it
1298 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1303 // if we haven't found anything...
1306 // ... wrap to the beginning
1308 if ( HasFlag(wxTR_HIDE_ROOT
) )
1310 // can't select virtual root
1314 // and try all the items (stop when we get to the one we started from)
1315 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1324 // -----------------------------------------------------------------------------
1326 // -----------------------------------------------------------------------------
1328 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1330 const wxString
& text
,
1331 int image
, int selImage
,
1332 wxTreeItemData
*data
)
1334 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1337 // should we give a warning here?
1338 return AddRoot(text
, image
, selImage
, data
);
1341 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1343 wxGenericTreeItem
*item
=
1344 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1348 data
->m_pItem
= item
;
1351 parent
->Insert( item
, previous
);
1356 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1357 int image
, int selImage
,
1358 wxTreeItemData
*data
)
1360 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1362 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1364 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1365 image
, selImage
, data
);
1368 data
->m_pItem
= m_anchor
;
1371 if (HasFlag(wxTR_HIDE_ROOT
))
1373 // if root is hidden, make sure we can navigate
1375 m_anchor
->SetHasPlus();
1377 CalculatePositions();
1380 if (!HasFlag(wxTR_MULTIPLE
))
1382 m_current
= m_key_current
= m_anchor
;
1383 m_current
->SetHilight( TRUE
);
1389 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1390 const wxString
& text
,
1391 int image
, int selImage
,
1392 wxTreeItemData
*data
)
1394 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1397 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1398 const wxTreeItemId
& idPrevious
,
1399 const wxString
& text
,
1400 int image
, int selImage
,
1401 wxTreeItemData
*data
)
1403 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1406 // should we give a warning here?
1407 return AddRoot(text
, image
, selImage
, data
);
1411 if (idPrevious
.IsOk())
1413 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1414 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1415 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1418 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1421 wxTreeItemId
wxGenericTreeCtrl::InsertItem(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(parentId
, before
, text
, image
, selImage
, data
);
1437 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1438 const wxString
& text
,
1439 int image
, int selImage
,
1440 wxTreeItemData
*data
)
1442 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1445 // should we give a warning here?
1446 return AddRoot(text
, image
, selImage
, data
);
1449 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1450 image
, selImage
, data
);
1453 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1455 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1456 event
.m_item
= item
;
1457 event
.SetEventObject( this );
1458 ProcessEvent( event
);
1461 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1463 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1465 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1466 item
->DeleteChildren(this);
1469 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1471 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1473 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1475 wxGenericTreeItem
*parent
= item
->GetParent();
1477 // don't keep stale pointers around!
1478 if ( IsDescendantOf(item
, m_key_current
) )
1480 // Don't silently change the selection:
1481 // do it properly in idle time, so event
1482 // handlers get called.
1484 // m_key_current = parent;
1485 m_key_current
= NULL
;
1488 // m_select_me records whether we need to select
1489 // a different item, in idle time.
1490 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1492 m_select_me
= parent
;
1495 if ( IsDescendantOf(item
, m_current
) )
1497 // Don't silently change the selection:
1498 // do it properly in idle time, so event
1499 // handlers get called.
1501 // m_current = parent;
1503 m_select_me
= parent
;
1506 // remove the item from the tree
1509 parent
->GetChildren().Remove( item
); // remove by value
1511 else // deleting the root
1513 // nothing will be left in the tree
1517 // and delete all of its children and the item itself now
1518 item
->DeleteChildren(this);
1519 SendDeleteEvent(item
);
1523 void wxGenericTreeCtrl::DeleteAllItems()
1531 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1533 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1535 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1536 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1537 _T("can't expand hidden root") );
1539 if ( !item
->HasPlus() )
1542 if ( item
->IsExpanded() )
1545 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1546 event
.m_item
= item
;
1547 event
.SetEventObject( this );
1549 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1551 // cancelled by program
1556 CalculatePositions();
1558 RefreshSubtree(item
);
1560 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1561 ProcessEvent( event
);
1564 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1566 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1569 if ( !IsExpanded(item
) )
1573 wxTreeItemIdValue cookie
;
1574 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1575 while ( child
.IsOk() )
1579 child
= GetNextChild(item
, cookie
);
1583 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1585 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1586 _T("can't collapse hidden root") );
1588 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1590 if ( !item
->IsExpanded() )
1593 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1594 event
.m_item
= item
;
1595 event
.SetEventObject( this );
1596 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1598 // cancelled by program
1604 #if 0 // TODO why should items be collapsed recursively?
1605 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1606 size_t count
= children
.Count();
1607 for ( size_t n
= 0; n
< count
; n
++ )
1609 Collapse(children
[n
]);
1613 CalculatePositions();
1615 RefreshSubtree(item
);
1617 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1618 ProcessEvent( event
);
1621 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1624 DeleteChildren(item
);
1627 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1629 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1631 if (item
->IsExpanded())
1637 void wxGenericTreeCtrl::Unselect()
1641 m_current
->SetHilight( FALSE
);
1642 RefreshLine( m_current
);
1649 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1651 if (item
->IsSelected())
1653 item
->SetHilight(FALSE
);
1657 if (item
->HasChildren())
1659 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1660 size_t count
= children
.Count();
1661 for ( size_t n
= 0; n
< count
; ++n
)
1663 UnselectAllChildren(children
[n
]);
1668 void wxGenericTreeCtrl::UnselectAll()
1670 wxTreeItemId rootItem
= GetRootItem();
1672 // the tree might not have the root item at all
1675 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1679 // Recursive function !
1680 // To stop we must have crt_item<last_item
1682 // Tag all next children, when no more children,
1683 // Move to parent (not to tag)
1684 // Keep going... if we found last_item, we stop.
1685 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1687 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1689 if (parent
== NULL
) // This is root item
1690 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1692 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1693 int index
= children
.Index(crt_item
);
1694 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1696 size_t count
= children
.Count();
1697 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1699 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1702 return TagNextChildren(parent
, last_item
, select
);
1705 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1707 crt_item
->SetHilight(select
);
1708 RefreshLine(crt_item
);
1710 if (crt_item
==last_item
)
1713 if (crt_item
->HasChildren())
1715 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1716 size_t count
= children
.Count();
1717 for ( size_t n
= 0; n
< count
; ++n
)
1719 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1727 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1731 // item2 is not necessary after item1
1732 // choice first' and 'last' between item1 and item2
1733 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1734 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1736 bool select
= m_current
->IsSelected();
1738 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1741 TagNextChildren(first
,last
,select
);
1744 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1745 bool unselect_others
,
1746 bool extended_select
)
1748 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1752 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1753 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1755 //wxCHECK_RET( ( (!unselect_others) && is_single),
1756 // wxT("this is a single selection tree") );
1758 // to keep going anyhow !!!
1761 if (item
->IsSelected())
1762 return; // nothing to do
1763 unselect_others
= TRUE
;
1764 extended_select
= FALSE
;
1766 else if ( unselect_others
&& item
->IsSelected() )
1768 // selection change if there is more than one item currently selected
1769 wxArrayTreeItemIds selected_items
;
1770 if ( GetSelections(selected_items
) == 1 )
1774 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1775 event
.m_item
= item
;
1776 event
.m_itemOld
= m_current
;
1777 event
.SetEventObject( this );
1778 // TODO : Here we don't send any selection mode yet !
1780 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1783 wxTreeItemId parent
= GetItemParent( itemId
);
1784 while (parent
.IsOk())
1786 if (!IsExpanded(parent
))
1789 parent
= GetItemParent( parent
);
1792 EnsureVisible( itemId
);
1795 if (unselect_others
)
1797 if (is_single
) Unselect(); // to speed up thing
1802 if (extended_select
)
1806 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1809 // don't change the mark (m_current)
1810 SelectItemRange(m_current
, item
);
1814 bool select
=TRUE
; // the default
1816 // Check if we need to toggle hilight (ctrl mode)
1817 if (!unselect_others
)
1818 select
=!item
->IsSelected();
1820 m_current
= m_key_current
= item
;
1821 m_current
->SetHilight(select
);
1822 RefreshLine( m_current
);
1825 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1826 GetEventHandler()->ProcessEvent( event
);
1829 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1833 DoSelectItem(itemId
);
1837 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1838 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1840 item
->SetHilight(FALSE
);
1845 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1846 wxArrayTreeItemIds
&array
) const
1848 if ( item
->IsSelected() )
1849 array
.Add(wxTreeItemId(item
));
1851 if ( item
->HasChildren() )
1853 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1854 size_t count
= children
.GetCount();
1855 for ( size_t n
= 0; n
< count
; ++n
)
1856 FillArray(children
[n
], array
);
1860 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1863 wxTreeItemId idRoot
= GetRootItem();
1864 if ( idRoot
.IsOk() )
1866 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1868 //else: the tree is empty, so no selections
1870 return array
.Count();
1873 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1875 if (!item
.IsOk()) return;
1877 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1879 // first expand all parent branches
1880 wxGenericTreeItem
*parent
= gitem
->GetParent();
1882 if ( HasFlag(wxTR_HIDE_ROOT
) )
1884 while ( parent
&& parent
!= m_anchor
)
1887 parent
= parent
->GetParent();
1895 parent
= parent
->GetParent();
1899 //if (parent) CalculatePositions();
1904 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1906 if (!item
.IsOk()) return;
1908 // We have to call this here because the label in
1909 // question might just have been added and no screen
1910 // update taken place.
1912 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1917 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1919 // now scroll to the item
1920 int item_y
= gitem
->GetY();
1924 GetViewStart( &start_x
, &start_y
);
1925 start_y
*= PIXELS_PER_UNIT
;
1929 GetClientSize( &client_w
, &client_h
);
1931 if (item_y
< start_y
+3)
1936 m_anchor
->GetSize( x
, y
, this );
1937 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1938 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1939 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1940 // Item should appear at top
1941 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1943 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1948 m_anchor
->GetSize( x
, y
, this );
1949 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1950 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1951 item_y
+= PIXELS_PER_UNIT
+2;
1952 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1953 // Item should appear at bottom
1954 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
);
1958 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1959 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1961 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1962 wxGenericTreeItem
**item2
)
1964 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1966 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
1969 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1970 const wxTreeItemId
& item2
)
1972 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1975 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
1977 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1979 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1981 wxCHECK_RET( !s_treeBeingSorted
,
1982 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1984 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1985 if ( children
.Count() > 1 )
1989 s_treeBeingSorted
= this;
1990 children
.Sort(tree_ctrl_compare_func
);
1991 s_treeBeingSorted
= NULL
;
1993 //else: don't make the tree dirty as nothing changed
1996 wxImageList
*wxGenericTreeCtrl::GetImageList() const
1998 return m_imageListNormal
;
2001 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2003 return m_imageListButtons
;
2006 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2008 return m_imageListState
;
2011 void wxGenericTreeCtrl::CalculateLineHeight()
2013 wxClientDC
dc(this);
2014 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2016 if ( m_imageListNormal
)
2018 // Calculate a m_lineHeight value from the normal Image sizes.
2019 // May be toggle off. Then wxGenericTreeCtrl will spread when
2020 // necessary (which might look ugly).
2021 int n
= m_imageListNormal
->GetImageCount();
2022 for (int i
= 0; i
< n
; i
++)
2024 int width
= 0, height
= 0;
2025 m_imageListNormal
->GetSize(i
, width
, height
);
2026 if (height
> m_lineHeight
) m_lineHeight
= height
;
2030 if (m_imageListButtons
)
2032 // Calculate a m_lineHeight value from the Button image sizes.
2033 // May be toggle off. Then wxGenericTreeCtrl will spread when
2034 // necessary (which might look ugly).
2035 int n
= m_imageListButtons
->GetImageCount();
2036 for (int i
= 0; i
< n
; i
++)
2038 int width
= 0, height
= 0;
2039 m_imageListButtons
->GetSize(i
, width
, height
);
2040 if (height
> m_lineHeight
) m_lineHeight
= height
;
2044 if (m_lineHeight
< 30)
2045 m_lineHeight
+= 2; // at least 2 pixels
2047 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2050 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2052 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2053 m_imageListNormal
= imageList
;
2054 m_ownsImageListNormal
= FALSE
;
2056 // Don't do any drawing if we're setting the list to NULL,
2057 // since we may be in the process of deleting the tree control.
2059 CalculateLineHeight();
2062 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2064 if (m_ownsImageListState
) delete m_imageListState
;
2065 m_imageListState
= imageList
;
2066 m_ownsImageListState
= FALSE
;
2069 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2071 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2072 m_imageListButtons
= imageList
;
2073 m_ownsImageListButtons
= FALSE
;
2075 CalculateLineHeight();
2078 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2080 SetImageList(imageList
);
2081 m_ownsImageListNormal
= TRUE
;
2084 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2086 SetStateImageList(imageList
);
2087 m_ownsImageListState
= TRUE
;
2090 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2092 SetButtonsImageList(imageList
);
2093 m_ownsImageListButtons
= TRUE
;
2096 // -----------------------------------------------------------------------------
2098 // -----------------------------------------------------------------------------
2100 void wxGenericTreeCtrl::AdjustMyScrollbars()
2105 m_anchor
->GetSize( x
, y
, this );
2106 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2107 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2108 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2109 int y_pos
= GetScrollPos( wxVERTICAL
);
2110 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2114 SetScrollbars( 0, 0, 0, 0 );
2118 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2120 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2121 return item
->GetHeight();
2123 return m_lineHeight
;
2126 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2128 // TODO implement "state" icon on items
2130 wxTreeItemAttr
*attr
= item
->GetAttributes();
2131 if ( attr
&& attr
->HasFont() )
2132 dc
.SetFont(attr
->GetFont());
2133 else if (item
->IsBold())
2134 dc
.SetFont(m_boldFont
);
2136 long text_w
= 0, text_h
= 0;
2137 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2139 int image_h
= 0, image_w
= 0;
2140 int image
= item
->GetCurrentImage();
2141 if ( image
!= NO_IMAGE
)
2143 if ( m_imageListNormal
)
2145 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2154 int total_h
= GetLineHeight(item
);
2156 if ( item
->IsSelected() )
2158 // under mac selections are only a rectangle in case they don't have the focus
2162 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2163 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2167 dc
.SetBrush( *m_hilightBrush
) ;
2170 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2176 if ( attr
&& attr
->HasBackgroundColour() )
2177 colBg
= attr
->GetBackgroundColour();
2179 colBg
= m_backgroundColour
;
2180 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2183 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2185 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2189 DoGetPosition(&x
, &y
);
2191 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2195 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2197 // If it's selected, and there's an image, then we should
2198 // take care to leave the area under the image painted in the
2199 // background colour.
2200 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2201 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2205 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2206 item
->GetWidth()+2, total_h
-offset
);
2210 if ( image
!= NO_IMAGE
)
2212 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2213 m_imageListNormal
->Draw( image
, dc
,
2215 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2216 wxIMAGELIST_DRAW_TRANSPARENT
);
2217 dc
.DestroyClippingRegion();
2220 dc
.SetBackgroundMode(wxTRANSPARENT
);
2221 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2222 dc
.DrawText( item
->GetText(),
2223 (wxCoord
)(image_w
+ item
->GetX()),
2224 (wxCoord
)(item
->GetY() + extraH
));
2226 // restore normal font
2227 dc
.SetFont( m_normalFont
);
2230 // Now y stands for the top of the item, whereas it used to stand for middle !
2231 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2233 int x
= level
*m_indent
;
2234 if (!HasFlag(wxTR_HIDE_ROOT
))
2238 else if (level
== 0)
2240 // always expand hidden root
2242 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2243 int count
= children
.Count();
2249 PaintLevel(children
[n
], dc
, 1, y
);
2250 } while (++n
< count
);
2252 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2254 // draw line down to last child
2255 origY
+= GetLineHeight(children
[0])>>1;
2256 oldY
+= GetLineHeight(children
[n
-1])>>1;
2257 dc
.DrawLine(3, origY
, 3, oldY
);
2263 item
->SetX(x
+m_spacing
);
2266 int h
= GetLineHeight(item
);
2268 int y_mid
= y_top
+ (h
>>1);
2271 int exposed_x
= dc
.LogicalToDeviceX(0);
2272 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2274 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2278 // don't draw rect outline if we already have the
2279 // background color under Mac
2280 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2281 #endif // !__WXMAC__
2285 if ( item
->IsSelected() )
2287 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2291 wxTreeItemAttr
*attr
= item
->GetAttributes();
2292 if (attr
&& attr
->HasTextColour())
2293 colText
= attr
->GetTextColour();
2295 colText
= GetForegroundColour();
2299 dc
.SetTextForeground(colText
);
2303 PaintItem(item
, dc
);
2305 if (HasFlag(wxTR_ROW_LINES
))
2307 // if the background colour is white, choose a
2308 // contrasting color for the lines
2309 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2310 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2311 dc
.DrawLine(0, y_top
, 10000, y_top
);
2312 dc
.DrawLine(0, y
, 10000, y
);
2315 // restore DC objects
2316 dc
.SetBrush(*wxWHITE_BRUSH
);
2317 dc
.SetPen(m_dottedPen
);
2318 dc
.SetTextForeground(*wxBLACK
);
2320 if ( !HasFlag(wxTR_NO_LINES
) )
2322 // draw the horizontal line here
2324 if (x
> (signed)m_indent
)
2325 x_start
-= m_indent
;
2326 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2328 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2331 // should the item show a button?
2332 if ( item
->HasPlus() && HasButtons() )
2334 if ( m_imageListButtons
)
2336 // draw the image button here
2339 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2340 : wxTreeItemIcon_Normal
;
2341 if ( item
->IsSelected() )
2342 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2344 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2345 int xx
= x
- image_w
/2;
2346 int yy
= y_mid
- image_h
/2;
2348 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2349 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2350 wxIMAGELIST_DRAW_TRANSPARENT
);
2352 else // no custom buttons
2354 static const int wImage
= 9;
2355 static const int hImage
= 9;
2358 if (item
->IsExpanded())
2359 flag
|= wxCONTROL_EXPANDED
;
2360 if (item
== m_underMouse
)
2361 flag
|= wxCONTROL_CURRENT
;
2363 wxRendererNative::Get().DrawTreeItemButton
2367 wxRect(x
- wImage
/2,
2376 if (item
->IsExpanded())
2378 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2379 int count
= children
.Count();
2386 PaintLevel(children
[n
], dc
, level
, y
);
2387 } while (++n
< count
);
2389 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2391 // draw line down to last child
2392 oldY
+= GetLineHeight(children
[n
-1])>>1;
2393 if (HasButtons()) y_mid
+= 5;
2395 // Only draw the portion of the line that is visible, in case it is huge
2396 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2397 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2398 yOrigin
= abs(yOrigin
);
2399 GetClientSize(&width
, &height
);
2401 // Move end points to the begining/end of the view?
2402 if (y_mid
< yOrigin
)
2404 if (oldY
> yOrigin
+ height
)
2405 oldY
= yOrigin
+ height
;
2407 // after the adjustments if y_mid is larger than oldY then the line
2408 // isn't visible at all so don't draw anything
2410 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2416 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2420 if ( item
->HasPlus() )
2422 // it's a folder, indicate it by a border
2427 // draw a line under the drop target because the item will be
2429 DrawLine(item
, TRUE
/* below */);
2432 SetCursor(wxCURSOR_BULLSEYE
);
2437 SetCursor(wxCURSOR_NO_ENTRY
);
2441 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2443 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2445 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2447 wxClientDC
dc(this);
2449 dc
.SetLogicalFunction(wxINVERT
);
2450 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2452 int w
= i
->GetWidth() + 2;
2453 int h
= GetLineHeight(i
) + 2;
2455 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2458 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2460 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2462 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2464 wxClientDC
dc(this);
2466 dc
.SetLogicalFunction(wxINVERT
);
2472 y
+= GetLineHeight(i
) - 1;
2475 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2478 // -----------------------------------------------------------------------------
2479 // wxWindows callbacks
2480 // -----------------------------------------------------------------------------
2482 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2490 dc
.SetFont( m_normalFont
);
2491 dc
.SetPen( m_dottedPen
);
2493 // this is now done dynamically
2494 //if(GetImageList() == NULL)
2495 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2498 PaintLevel( m_anchor
, dc
, 0, y
);
2501 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2510 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2519 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2521 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2522 te
.m_evtKey
= event
;
2523 te
.SetEventObject( this );
2524 if ( GetEventHandler()->ProcessEvent( te
) )
2526 // intercepted by the user code
2530 if ( (m_current
== 0) || (m_key_current
== 0) )
2536 // how should the selection work for this event?
2537 bool is_multiple
, extended_select
, unselect_others
;
2538 EventFlagsToSelType(GetWindowStyleFlag(),
2540 event
.ControlDown(),
2541 is_multiple
, extended_select
, unselect_others
);
2545 // * : Expand all/Collapse all
2546 // ' ' | return : activate
2547 // up : go up (not last children!)
2549 // left : go to parent
2550 // right : open if parent and go next
2551 // home : go to root
2552 // end : go to last item without opening parents
2553 // alnum : start or continue searching for the item with this prefix
2554 int keyCode
= event
.GetKeyCode();
2559 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2567 if ( !IsExpanded(m_current
) )
2570 ExpandAll(m_current
);
2573 //else: fall through to Collapse() it
2577 if (IsExpanded(m_current
))
2579 Collapse(m_current
);
2585 if ( !event
.HasModifiers() )
2587 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2588 event
.m_item
= m_current
;
2589 event
.SetEventObject( this );
2590 GetEventHandler()->ProcessEvent( event
);
2593 // in any case, also generate the normal key event for this key,
2594 // even if we generated the ACTIVATED event above: this is what
2595 // wxMSW does and it makes sense because you might not want to
2596 // process ACTIVATED event at all and handle Space and Return
2597 // directly (and differently) which would be impossible otherwise
2601 // up goes to the previous sibling or to the last
2602 // of its children if it's expanded
2605 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2608 prev
= GetItemParent( m_key_current
);
2609 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2611 break; // don't go to root if it is hidden
2615 wxTreeItemIdValue cookie
;
2616 wxTreeItemId current
= m_key_current
;
2617 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2618 if (current
== GetFirstChild( prev
, cookie
))
2620 // otherwise we return to where we came from
2621 DoSelectItem( prev
, unselect_others
, extended_select
);
2622 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2629 while ( IsExpanded(prev
) && HasChildren(prev
) )
2631 wxTreeItemId child
= GetLastChild(prev
);
2638 DoSelectItem( prev
, unselect_others
, extended_select
);
2639 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2644 // left arrow goes to the parent
2647 wxTreeItemId prev
= GetItemParent( m_current
);
2648 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2650 // don't go to root if it is hidden
2651 prev
= GetPrevSibling( m_current
);
2655 DoSelectItem( prev
, unselect_others
, extended_select
);
2661 // this works the same as the down arrow except that we
2662 // also expand the item if it wasn't expanded yet
2668 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2670 wxTreeItemIdValue cookie
;
2671 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2672 DoSelectItem( child
, unselect_others
, extended_select
);
2673 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2677 wxTreeItemId next
= GetNextSibling( m_key_current
);
2680 wxTreeItemId current
= m_key_current
;
2681 while (current
.IsOk() && !next
)
2683 current
= GetItemParent( current
);
2684 if (current
) next
= GetNextSibling( current
);
2689 DoSelectItem( next
, unselect_others
, extended_select
);
2690 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2696 // <End> selects the last visible tree item
2699 wxTreeItemId last
= GetRootItem();
2701 while ( last
.IsOk() && IsExpanded(last
) )
2703 wxTreeItemId lastChild
= GetLastChild(last
);
2705 // it may happen if the item was expanded but then all of
2706 // its children have been deleted - so IsExpanded() returned
2707 // TRUE, but GetLastChild() returned invalid item
2716 DoSelectItem( last
, unselect_others
, extended_select
);
2721 // <Home> selects the root item
2724 wxTreeItemId prev
= GetRootItem();
2728 if ( HasFlag(wxTR_HIDE_ROOT
) )
2730 wxTreeItemIdValue cookie
;
2731 prev
= GetFirstChild(prev
, cookie
);
2736 DoSelectItem( prev
, unselect_others
, extended_select
);
2741 // do not use wxIsalnum() here
2742 if ( !event
.HasModifiers() &&
2743 ((keyCode
>= '0' && keyCode
<= '9') ||
2744 (keyCode
>= 'a' && keyCode
<= 'z') ||
2745 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2747 // find the next item starting with the given prefix
2748 char ch
= (char)keyCode
;
2750 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2761 // also start the timer to reset the current prefix if the user
2762 // doesn't press any more alnum keys soon -- we wouldn't want
2763 // to use this prefix for a new item search
2766 m_findTimer
= new wxTreeFindTimer(this);
2769 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2778 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2780 // JACS: removed wxYieldIfNeeded() because it can cause the window
2781 // to be deleted from under us if a close window event is pending
2786 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2787 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2788 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2789 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2790 if (flags
) return wxTreeItemId();
2792 if (m_anchor
== NULL
)
2794 flags
= wxTREE_HITTEST_NOWHERE
;
2795 return wxTreeItemId();
2798 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2802 flags
= wxTREE_HITTEST_NOWHERE
;
2803 return wxTreeItemId();
2808 // get the bounding rectangle of the item (or of its label only)
2809 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2811 bool WXUNUSED(textOnly
)) const
2813 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2815 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2818 GetViewStart(& startX
, & startY
);
2820 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2821 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2822 rect
.width
= i
->GetWidth();
2823 //rect.height = i->GetHeight();
2824 rect
.height
= GetLineHeight(i
);
2829 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2831 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2833 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2835 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2836 te
.m_item
= itemEdit
;
2837 te
.SetEventObject( this );
2838 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2844 // We have to call this here because the label in
2845 // question might just have been added and no screen
2846 // update taken place.
2848 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2854 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2856 m_textCtrl
->SetFocus();
2859 // returns a pointer to the text edit control if the item is being
2860 // edited, NULL otherwise (it's assumed that no more than one item may
2861 // be edited simultaneously)
2862 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2867 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2868 const wxString
& value
)
2870 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2872 le
.SetEventObject( this );
2874 le
.m_editCancelled
= FALSE
;
2876 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2879 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2881 // let owner know that the edit was cancelled
2882 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2884 le
.SetEventObject( this );
2885 le
.m_label
= wxEmptyString
;
2886 le
.m_editCancelled
= TRUE
;
2888 GetEventHandler()->ProcessEvent( le
);
2894 void wxGenericTreeCtrl::OnRenameTimer()
2899 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2901 if ( !m_anchor
) return;
2903 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
2905 // Is the mouse over a tree item button?
2907 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
2908 wxGenericTreeItem
*underMouse
= thisItem
;
2910 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
2911 #endif // wxUSE_TOOLTIPS
2914 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
2915 (!event
.LeftIsDown()) &&
2917 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2925 if (underMouse
!= m_underMouse
)
2929 // unhighlight old item
2930 wxGenericTreeItem
*tmp
= m_underMouse
;
2931 m_underMouse
= NULL
;
2935 m_underMouse
= underMouse
;
2937 RefreshLine( m_underMouse
);
2941 // Determines what item we are hovering over and need a tooltip for
2942 wxTreeItemId hoverItem
= thisItem
;
2944 // We do not want a tooltip if we are dragging, or if the rename timer is running
2945 if (underMouseChanged
&& hoverItem
.IsOk() && !m_isDragging
&& (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
2947 // Ask the tree control what tooltip (if any) should be shown
2948 wxTreeEvent
hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, GetId());
2949 hevent
.m_item
= hoverItem
;
2950 hevent
.SetEventObject(this);
2952 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
2954 SetToolTip(hevent
.m_label
);
2959 // we process left mouse up event (enables in-place edit), right down
2960 // (pass to the user code), left dbl click (activate item) and
2961 // dragging/moving events for items drag-and-drop
2962 if ( !(event
.LeftDown() ||
2964 event
.RightDown() ||
2965 event
.LeftDClick() ||
2967 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2976 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
2978 if ( event
.Dragging() && !m_isDragging
)
2980 if (m_dragCount
== 0)
2985 if (m_dragCount
!= 3)
2987 // wait until user drags a bit further...
2991 wxEventType command
= event
.RightIsDown()
2992 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2993 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2995 wxTreeEvent
nevent( command
, GetId() );
2996 nevent
.m_item
= m_current
;
2997 nevent
.SetEventObject(this);
2999 // by default the dragging is not supported, the user code must
3000 // explicitly allow the event for it to take place
3003 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3005 // we're going to drag this item
3006 m_isDragging
= TRUE
;
3008 // remember the old cursor because we will change it while
3010 m_oldCursor
= m_cursor
;
3012 // in a single selection control, hide the selection temporarily
3013 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3015 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3017 if ( m_oldSelection
)
3019 m_oldSelection
->SetHilight(FALSE
);
3020 RefreshLine(m_oldSelection
);
3027 else if ( event
.Moving() )
3029 if ( item
!= m_dropTarget
)
3031 // unhighlight the previous drop target
3032 DrawDropEffect(m_dropTarget
);
3034 m_dropTarget
= item
;
3036 // highlight the current drop target if any
3037 DrawDropEffect(m_dropTarget
);
3039 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3046 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3048 // erase the highlighting
3049 DrawDropEffect(m_dropTarget
);
3051 if ( m_oldSelection
)
3053 m_oldSelection
->SetHilight(TRUE
);
3054 RefreshLine(m_oldSelection
);
3055 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3058 // generate the drag end event
3059 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3061 event
.m_item
= item
;
3062 event
.m_pointDrag
= pt
;
3063 event
.SetEventObject(this);
3065 (void)GetEventHandler()->ProcessEvent(event
);
3067 m_isDragging
= FALSE
;
3068 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3072 SetCursor(m_oldCursor
);
3074 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3082 // here we process only the messages which happen on tree items
3086 if (item
== NULL
) return; /* we hit the blank area */
3088 if ( event
.RightDown() )
3090 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3091 nevent
.m_item
= item
;
3092 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3093 nevent
.SetEventObject(this);
3094 GetEventHandler()->ProcessEvent(nevent
);
3096 else if ( event
.LeftUp() )
3098 // this facilitates multiple-item drag-and-drop
3100 if (item
&& HasFlag(wxTR_MULTIPLE
))
3102 wxArrayTreeItemIds selections
;
3103 size_t count
= GetSelections(selections
);
3106 !event
.ControlDown() &&
3109 DoSelectItem(item
, true, false);
3115 if ( (item
== m_current
) &&
3116 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3117 HasFlag(wxTR_EDIT_LABELS
) )
3119 if ( m_renameTimer
)
3121 if ( m_renameTimer
->IsRunning() )
3122 m_renameTimer
->Stop();
3126 m_renameTimer
= new wxTreeRenameTimer( this );
3129 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3132 m_lastOnSame
= FALSE
;
3135 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3137 if ( event
.LeftDown() )
3139 m_lastOnSame
= item
== m_current
;
3142 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3144 // only toggle the item for a single click, double click on
3145 // the button doesn't do anything (it toggles the item twice)
3146 if ( event
.LeftDown() )
3151 // don't select the item if the button was clicked
3156 // clear the previously selected items, if the
3157 // user clicked outside of the present selection.
3158 // otherwise, perform the deselection on mouse-up.
3159 // this allows multiple drag and drop to work.
3161 if (!IsSelected(item
))
3163 // how should the selection work for this event?
3164 bool is_multiple
, extended_select
, unselect_others
;
3165 EventFlagsToSelType(GetWindowStyleFlag(),
3167 event
.ControlDown(),
3168 is_multiple
, extended_select
, unselect_others
);
3170 DoSelectItem(item
, unselect_others
, extended_select
);
3174 // For some reason, Windows isn't recognizing a left double-click,
3175 // so we need to simulate it here. Allow 200 milliseconds for now.
3176 if ( event
.LeftDClick() )
3178 // double clicking should not start editing the item label
3179 if ( m_renameTimer
)
3180 m_renameTimer
->Stop();
3182 m_lastOnSame
= FALSE
;
3184 // send activate event first
3185 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3186 nevent
.m_item
= item
;
3187 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3188 nevent
.SetEventObject( this );
3189 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3191 // if the user code didn't process the activate event,
3192 // handle it ourselves by toggling the item when it is
3194 if ( item
->HasPlus() )
3204 void wxGenericTreeCtrl::OnInternalIdle()
3206 wxWindow::OnInternalIdle();
3208 // Check if we need to select the root item
3209 // because nothing else has been selected.
3210 // Delaying it means that we can invoke event handlers
3211 // as required, when a first item is selected.
3212 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3215 SelectItem(m_select_me
);
3216 else if (GetRootItem().IsOk())
3217 SelectItem(GetRootItem());
3220 /* after all changes have been done to the tree control,
3221 * we actually redraw the tree when everything is over */
3223 if (!m_dirty
) return;
3224 if (m_freezeCount
) return;
3228 CalculatePositions();
3230 AdjustMyScrollbars();
3233 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3238 wxTreeItemAttr
*attr
= item
->GetAttributes();
3239 if ( attr
&& attr
->HasFont() )
3240 dc
.SetFont(attr
->GetFont());
3241 else if ( item
->IsBold() )
3242 dc
.SetFont(m_boldFont
);
3244 dc
.SetFont(m_normalFont
);
3246 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3249 // restore normal font
3250 dc
.SetFont( m_normalFont
);
3254 int image
= item
->GetCurrentImage();
3255 if ( image
!= NO_IMAGE
)
3257 if ( m_imageListNormal
)
3259 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3264 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3267 total_h
+= 2; // at least 2 pixels
3269 total_h
+= total_h
/10; // otherwise 10% extra spacing
3271 item
->SetHeight(total_h
);
3272 if (total_h
>m_lineHeight
)
3273 m_lineHeight
=total_h
;
3275 item
->SetWidth(image_w
+text_w
+2);
3278 // -----------------------------------------------------------------------------
3279 // for developper : y is now the top of the level
3280 // not the middle of it !
3281 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3283 int x
= level
*m_indent
;
3284 if (!HasFlag(wxTR_HIDE_ROOT
))
3288 else if (level
== 0)
3290 // a hidden root is not evaluated, but its
3291 // children are always calculated
3295 CalculateSize( item
, dc
);
3298 item
->SetX( x
+m_spacing
);
3300 y
+= GetLineHeight(item
);
3302 if ( !item
->IsExpanded() )
3304 // we don't need to calculate collapsed branches
3309 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3310 size_t n
, count
= children
.Count();
3312 for (n
= 0; n
< count
; ++n
)
3313 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3316 void wxGenericTreeCtrl::CalculatePositions()
3318 if ( !m_anchor
) return;
3320 wxClientDC
dc(this);
3323 dc
.SetFont( m_normalFont
);
3325 dc
.SetPen( m_dottedPen
);
3326 //if(GetImageList() == NULL)
3327 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3330 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3333 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3335 if (m_dirty
) return;
3336 if (m_freezeCount
) return;
3338 wxSize client
= GetClientSize();
3341 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3342 rect
.width
= client
.x
;
3343 rect
.height
= client
.y
;
3345 Refresh(TRUE
, &rect
);
3347 AdjustMyScrollbars();
3350 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3352 if (m_dirty
) return;
3353 if (m_freezeCount
) return;
3356 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3357 rect
.width
= GetClientSize().x
;
3358 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3360 Refresh(TRUE
, &rect
);
3363 void wxGenericTreeCtrl::RefreshSelected()
3365 if (m_freezeCount
) return;
3367 // TODO: this is awfully inefficient, we should keep the list of all
3368 // selected items internally, should be much faster
3370 RefreshSelectedUnder(m_anchor
);
3373 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3375 if (m_freezeCount
) return;
3377 if ( item
->IsSelected() )
3380 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3381 size_t count
= children
.GetCount();
3382 for ( size_t n
= 0; n
< count
; n
++ )
3384 RefreshSelectedUnder(children
[n
]);
3388 void wxGenericTreeCtrl::Freeze()
3393 void wxGenericTreeCtrl::Thaw()
3395 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen tree control?") );
3397 if ( !--m_freezeCount
)
3403 // ----------------------------------------------------------------------------
3404 // changing colours: we need to refresh the tree control
3405 // ----------------------------------------------------------------------------
3407 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3409 if ( !wxWindow::SetBackgroundColour(colour
) )
3412 if (m_freezeCount
) return TRUE
;
3419 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3421 if ( !wxWindow::SetForegroundColour(colour
) )
3424 if (m_freezeCount
) return TRUE
;
3431 // Process the tooltip event, to speed up event processing.
3432 // Doesn't actually get a tooltip.
3433 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3438 #endif // wxUSE_TREECTRL