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 // -----------------------------------------------------------------------------
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"
42 #include "wx/mac/private.h"
45 // -----------------------------------------------------------------------------
47 // -----------------------------------------------------------------------------
49 class WXDLLEXPORT wxGenericTreeItem
;
51 WX_DEFINE_EXPORTED_ARRAY_NO_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 static const int NO_IMAGE
= -1;
59 static const int PIXELS_PER_UNIT
= 10;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
66 static const char *aqua_arrow_right
[] = {
67 /* columns rows colors chars-per-pixel */
88 static const char *aqua_arrow_down
[] = {
89 /* columns rows colors chars-per-pixel */
109 // -----------------------------------------------------------------------------
111 // -----------------------------------------------------------------------------
113 // timer used for enabling in-place edit
114 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
117 // start editing the current item after half a second (if the mouse hasn't
118 // been clicked/moved)
119 enum { DELAY
= 500 };
121 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
123 virtual void Notify();
126 wxGenericTreeCtrl
*m_owner
;
128 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
131 // control used for in-place edit
132 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
135 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
138 void OnChar( wxKeyEvent
&event
);
139 void OnKeyUp( wxKeyEvent
&event
);
140 void OnKillFocus( wxFocusEvent
&event
);
142 bool AcceptChanges();
146 wxGenericTreeCtrl
*m_owner
;
147 wxGenericTreeItem
*m_itemEdited
;
148 wxString m_startValue
;
151 DECLARE_EVENT_TABLE()
152 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
155 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
156 // for a sufficiently long time
157 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
160 // reset the current prefix after half a second of inactivity
161 enum { DELAY
= 500 };
163 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
165 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
168 wxGenericTreeCtrl
*m_owner
;
170 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
174 class WXDLLEXPORT wxGenericTreeItem
178 wxGenericTreeItem() { m_data
= NULL
; }
179 wxGenericTreeItem( wxGenericTreeItem
*parent
,
180 const wxString
& text
,
183 wxTreeItemData
*data
);
185 ~wxGenericTreeItem();
188 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
190 const wxString
& GetText() const { return m_text
; }
191 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
192 { return m_images
[which
]; }
193 wxTreeItemData
*GetData() const { return m_data
; }
195 // returns the current image for the item (depending on its
196 // selected/expanded/whatever state)
197 int GetCurrentImage() const;
199 void SetText( const wxString
&text
);
200 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
201 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
203 void SetHasPlus(bool has
= TRUE
) { m_hasPlus
= has
; }
205 void SetBold(bool bold
) { m_isBold
= bold
; }
207 int GetX() const { return m_x
; }
208 int GetY() const { return m_y
; }
210 void SetX(int x
) { m_x
= x
; }
211 void SetY(int y
) { m_y
= y
; }
213 int GetHeight() const { return m_height
; }
214 int GetWidth() const { return m_width
; }
216 void SetHeight(int h
) { m_height
= h
; }
217 void SetWidth(int w
) { m_width
= w
; }
219 wxGenericTreeItem
*GetParent() const { return m_parent
; }
222 // deletes all children notifying the treectrl about it if !NULL
224 void DeleteChildren(wxGenericTreeCtrl
*tree
= NULL
);
226 // get count of all children (and grand children if 'recursively')
227 size_t GetChildrenCount(bool recursively
= TRUE
) const;
229 void Insert(wxGenericTreeItem
*child
, size_t index
)
230 { m_children
.Insert(child
, index
); }
232 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
234 // return the item at given position (or NULL if no item), onButton is
235 // TRUE if the point belongs to the item's button, otherwise it lies
236 // on the button's label
237 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
238 const wxGenericTreeCtrl
*,
242 void Expand() { m_isCollapsed
= FALSE
; }
243 void Collapse() { m_isCollapsed
= TRUE
; }
245 void SetHilight( bool set
= TRUE
) { m_hasHilight
= set
; }
248 bool HasChildren() const { return !m_children
.IsEmpty(); }
249 bool IsSelected() const { return m_hasHilight
!= 0; }
250 bool IsExpanded() const { return !m_isCollapsed
; }
251 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
252 bool IsBold() const { return m_isBold
!= 0; }
255 // get them - may be NULL
256 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
257 // get them ensuring that the pointer is not NULL
258 wxTreeItemAttr
& Attr()
262 m_attr
= new wxTreeItemAttr
;
268 void SetAttributes(wxTreeItemAttr
*attr
)
270 if ( m_ownsAttr
) delete m_attr
;
274 // set them and delete when done
275 void AssignAttributes(wxTreeItemAttr
*attr
)
282 // since there can be very many of these, we save size by chosing
283 // the smallest representation for the elements and by ordering
284 // the members to avoid padding.
285 wxString m_text
; // label to be rendered for item
287 wxTreeItemData
*m_data
; // user-provided data
289 wxArrayGenericTreeItems m_children
; // list of children
290 wxGenericTreeItem
*m_parent
; // parent of this item
292 wxTreeItemAttr
*m_attr
; // attributes???
294 // tree ctrl images for the normal, selected, expanded and
295 // expanded+selected states
296 short m_images
[wxTreeItemIcon_Max
];
298 wxCoord m_x
; // (virtual) offset from top
299 wxCoord m_y
; // (virtual) offset from left
300 short m_width
; // width of this item
301 unsigned char m_height
; // height of this item
303 // use bitfields to save size
304 int m_isCollapsed
:1;
305 int m_hasHilight
:1; // same as focused
306 int m_hasPlus
:1; // used for item which doesn't have
307 // children but has a [+] button
308 int m_isBold
:1; // render the label in bold font
309 int m_ownsAttr
:1; // delete attribute when done
311 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
314 // =============================================================================
316 // =============================================================================
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 // translate the key or mouse event flags to the type of selection we're
324 static void EventFlagsToSelType(long style
,
328 bool &extended_select
,
329 bool &unselect_others
)
331 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
332 extended_select
= shiftDown
&& is_multiple
;
333 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
336 // check if the given item is under another one
337 static bool IsDescendantOf(wxGenericTreeItem
*parent
, wxGenericTreeItem
*item
)
341 if ( item
== parent
)
343 // item is a descendant of parent
347 item
= item
->GetParent();
353 // -----------------------------------------------------------------------------
354 // wxTreeRenameTimer (internal)
355 // -----------------------------------------------------------------------------
357 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
362 void wxTreeRenameTimer::Notify()
364 m_owner
->OnRenameTimer();
367 //-----------------------------------------------------------------------------
368 // wxTreeTextCtrl (internal)
369 //-----------------------------------------------------------------------------
371 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
372 EVT_CHAR (wxTreeTextCtrl::OnChar
)
373 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
374 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
377 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
378 wxGenericTreeItem
*item
)
379 : m_itemEdited(item
), m_startValue(item
->GetText())
384 int w
= m_itemEdited
->GetWidth(),
385 h
= m_itemEdited
->GetHeight();
388 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
393 int image
= item
->GetCurrentImage();
394 if ( image
!= NO_IMAGE
)
396 if ( m_owner
->m_imageListNormal
)
398 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
403 wxFAIL_MSG(_T("you must create an image list to use images!"));
407 // FIXME: what are all these hardcoded 4, 8 and 11s really?
411 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
412 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
415 bool wxTreeTextCtrl::AcceptChanges()
417 const wxString value
= GetValue();
419 if ( value
== m_startValue
)
421 // nothing changed, always accept
425 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
427 // vetoed by the user
431 // accepted, do rename the item
432 m_owner
->SetItemText(m_itemEdited
, value
);
437 void wxTreeTextCtrl::Finish()
441 m_owner
->ResetTextControl();
443 wxPendingDelete
.Append(this);
447 m_owner
->SetFocus(); // This doesn't work. TODO.
451 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
453 switch ( event
.m_keyCode
)
456 if ( !AcceptChanges() )
458 // vetoed by the user, don't disappear
465 m_owner
->OnRenameCancelled(m_itemEdited
);
473 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
477 // auto-grow the textctrl:
478 wxSize parentSize
= m_owner
->GetSize();
479 wxPoint myPos
= GetPosition();
480 wxSize mySize
= GetSize();
482 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
483 if (myPos
.x
+ sx
> parentSize
.x
)
484 sx
= parentSize
.x
- myPos
.x
;
493 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
501 if ( AcceptChanges() )
507 // -----------------------------------------------------------------------------
509 // -----------------------------------------------------------------------------
511 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
512 const wxString
& text
,
513 int image
, int selImage
,
514 wxTreeItemData
*data
)
517 m_images
[wxTreeItemIcon_Normal
] = image
;
518 m_images
[wxTreeItemIcon_Selected
] = selImage
;
519 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
520 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
525 m_isCollapsed
= TRUE
;
526 m_hasHilight
= FALSE
;
532 m_attr
= (wxTreeItemAttr
*)NULL
;
535 // We don't know the height here yet.
540 wxGenericTreeItem::~wxGenericTreeItem()
544 if (m_ownsAttr
) delete m_attr
;
546 wxASSERT_MSG( m_children
.IsEmpty(),
547 wxT("please call DeleteChildren() before deleting the item") );
550 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
552 size_t count
= m_children
.Count();
553 for ( size_t n
= 0; n
< count
; n
++ )
555 wxGenericTreeItem
*child
= m_children
[n
];
557 tree
->SendDeleteEvent(child
);
559 child
->DeleteChildren(tree
);
566 void wxGenericTreeItem::SetText( const wxString
&text
)
571 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
573 size_t count
= m_children
.Count();
577 size_t total
= count
;
578 for (size_t n
= 0; n
< count
; ++n
)
580 total
+= m_children
[n
]->GetChildrenCount();
586 void wxGenericTreeItem::GetSize( int &x
, int &y
,
587 const wxGenericTreeCtrl
*theButton
)
589 int bottomY
=m_y
+theButton
->GetLineHeight(this);
590 if ( y
< bottomY
) y
= bottomY
;
591 int width
= m_x
+ m_width
;
592 if ( x
< width
) x
= width
;
596 size_t count
= m_children
.Count();
597 for ( size_t n
= 0; n
< count
; ++n
)
599 m_children
[n
]->GetSize( x
, y
, theButton
);
604 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
605 const wxGenericTreeCtrl
*theCtrl
,
609 // for a hidden root node, don't evaluate it, but do evaluate children
610 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
613 int h
= theCtrl
->GetLineHeight(this);
614 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
616 int y_mid
= m_y
+ h
/2;
617 if (point
.y
< y_mid
)
618 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
620 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
622 // 5 is the size of the plus sign
623 int xCross
= m_x
- theCtrl
->GetSpacing();
624 if ((point
.x
> xCross
-5) && (point
.x
< xCross
+5) &&
625 (point
.y
> y_mid
-5) && (point
.y
< y_mid
+5) &&
626 HasPlus() && theCtrl
->HasButtons() )
628 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
632 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
637 // assuming every image (normal and selected) has the same size!
638 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
639 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
642 if ((image_w
!= -1) && (point
.x
<= m_x
+ image_w
+ 1))
643 flags
|= wxTREE_HITTEST_ONITEMICON
;
645 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
651 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
652 if (point
.x
> m_x
+m_width
)
653 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
658 // if children are expanded, fall through to evaluate them
659 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
663 size_t count
= m_children
.Count();
664 for ( size_t n
= 0; n
< count
; n
++ )
666 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
674 return (wxGenericTreeItem
*) NULL
;
677 int wxGenericTreeItem::GetCurrentImage() const
679 int image
= NO_IMAGE
;
684 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
687 if ( image
== NO_IMAGE
)
689 // we usually fall back to the normal item, but try just the
690 // expanded one (and not selected) first in this case
691 image
= GetImage(wxTreeItemIcon_Expanded
);
697 image
= GetImage(wxTreeItemIcon_Selected
);
700 // maybe it doesn't have the specific image we want,
701 // try the default one instead
702 if ( image
== NO_IMAGE
) image
= GetImage();
707 // -----------------------------------------------------------------------------
708 // wxGenericTreeCtrl implementation
709 // -----------------------------------------------------------------------------
711 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxScrolledWindow
)
713 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
,wxScrolledWindow
)
714 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
715 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
716 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
717 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
718 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
721 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
723 * wxTreeCtrl has to be a real class or we have problems with
724 * the run-time information.
727 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
730 // -----------------------------------------------------------------------------
731 // construction/destruction
732 // -----------------------------------------------------------------------------
734 void wxGenericTreeCtrl::Init()
736 m_current
= m_key_current
= m_anchor
= m_select_me
= (wxGenericTreeItem
*) NULL
;
744 m_hilightBrush
= new wxBrush
746 wxSystemSettings::GetColour
748 wxSYS_COLOUR_HIGHLIGHT
753 m_hilightUnfocusedBrush
= new wxBrush
755 wxSystemSettings::GetColour
757 wxSYS_COLOUR_BTNSHADOW
762 m_imageListNormal
= m_imageListButtons
=
763 m_imageListState
= (wxImageList
*) NULL
;
764 m_ownsImageListNormal
= m_ownsImageListButtons
=
765 m_ownsImageListState
= FALSE
;
768 m_isDragging
= FALSE
;
769 m_dropTarget
= m_oldSelection
= (wxGenericTreeItem
*)NULL
;
772 m_renameTimer
= NULL
;
775 m_lastOnSame
= FALSE
;
777 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
778 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
779 m_normalFont
.GetFamily(),
780 m_normalFont
.GetStyle(),
782 m_normalFont
.GetUnderlined(),
783 m_normalFont
.GetFaceName(),
784 m_normalFont
.GetEncoding());
787 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
792 const wxValidator
&validator
,
793 const wxString
& name
)
797 wxGetOsVersion( &major
, &minor
);
799 if (style
& wxTR_HAS_BUTTONS
) style
|= wxTR_MAC_BUTTONS
;
800 if (style
& wxTR_HAS_BUTTONS
) style
&= ~wxTR_HAS_BUTTONS
;
801 style
&= ~wxTR_LINES_AT_ROOT
;
802 style
|= wxTR_NO_LINES
;
804 style
|= wxTR_ROW_LINES
;
806 style
|= wxTR_AQUA_BUTTONS
;
809 if (style
& wxTR_AQUA_BUTTONS
)
811 m_arrowRight
= new wxBitmap( aqua_arrow_right
);
812 m_arrowDown
= new wxBitmap( aqua_arrow_down
);
820 wxScrolledWindow::Create( parent
, id
, pos
, size
,
821 style
|wxHSCROLL
|wxVSCROLL
, name
);
823 // If the tree display has no buttons, but does have
824 // connecting lines, we can use a narrower layout.
825 // It may not be a good idea to force this...
826 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
833 SetValidator( validator
);
836 SetForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
837 SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
) );
839 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
840 m_dottedPen
= wxPen( wxT("grey"), 0, 0 );
845 wxGenericTreeCtrl::~wxGenericTreeCtrl()
847 delete m_hilightBrush
;
848 delete m_hilightUnfocusedBrush
;
855 delete m_renameTimer
;
858 if (m_ownsImageListNormal
)
859 delete m_imageListNormal
;
860 if (m_ownsImageListState
)
861 delete m_imageListState
;
862 if (m_ownsImageListButtons
)
863 delete m_imageListButtons
;
866 // -----------------------------------------------------------------------------
868 // -----------------------------------------------------------------------------
870 size_t wxGenericTreeCtrl::GetCount() const
872 return m_anchor
== NULL
? 0u : m_anchor
->GetChildrenCount();
875 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
877 m_indent
= (unsigned short) indent
;
881 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing
)
883 m_spacing
= (unsigned short) spacing
;
887 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
, bool recursively
)
889 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
891 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
894 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
896 if (!HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
898 // if we will hide the root, make sure children are visible
899 m_anchor
->SetHasPlus();
901 CalculatePositions();
904 // right now, just sets the styles. Eventually, we may
905 // want to update the inherited styles, but right now
906 // none of the parents has updatable styles
907 m_windowStyle
= styles
;
911 // -----------------------------------------------------------------------------
912 // functions to work with tree items
913 // -----------------------------------------------------------------------------
915 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
917 wxCHECK_MSG( item
.IsOk(), wxT(""), wxT("invalid tree item") );
919 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
922 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
923 wxTreeItemIcon which
) const
925 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
927 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
930 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
932 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
934 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
937 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
939 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
941 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
942 return pItem
->Attr().GetTextColour();
945 wxColour
wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
947 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
949 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
950 return pItem
->Attr().GetBackgroundColour();
953 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
955 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
957 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
958 return pItem
->Attr().GetFont();
961 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
963 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
966 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
967 pItem
->SetText(text
);
968 CalculateSize(pItem
, dc
);
972 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
974 wxTreeItemIcon which
)
976 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
978 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
979 pItem
->SetImage(image
, which
);
982 CalculateSize(pItem
, dc
);
986 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
988 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
990 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
993 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
995 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
997 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
998 pItem
->SetHasPlus(has
);
1002 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1004 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1006 // avoid redrawing the tree if no real change
1007 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1008 if ( pItem
->IsBold() != bold
)
1010 pItem
->SetBold(bold
);
1015 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1016 const wxColour
& col
)
1018 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1020 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1021 pItem
->Attr().SetTextColour(col
);
1025 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1026 const wxColour
& col
)
1028 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1030 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1031 pItem
->Attr().SetBackgroundColour(col
);
1035 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1037 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1039 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1040 pItem
->Attr().SetFont(font
);
1044 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1046 wxScrolledWindow::SetFont(font
);
1048 m_normalFont
= font
;
1049 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1050 m_normalFont
.GetFamily(),
1051 m_normalFont
.GetStyle(),
1053 m_normalFont
.GetUnderlined(),
1054 m_normalFont
.GetFaceName(),
1055 m_normalFont
.GetEncoding());
1061 // -----------------------------------------------------------------------------
1062 // item status inquiries
1063 // -----------------------------------------------------------------------------
1065 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1067 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1069 // An item is only visible if it's not a descendant of a collapsed item
1070 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1071 wxGenericTreeItem
* parent
= pItem
->GetParent();
1074 if (!parent
->IsExpanded())
1076 parent
= parent
->GetParent();
1080 GetViewStart(& startX
, & startY
);
1082 wxSize clientSize
= GetClientSize();
1085 if (!GetBoundingRect(item
, rect
))
1087 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1089 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1091 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1097 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1099 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1101 // consider that the item does have children if it has the "+" button: it
1102 // might not have them (if it had never been expanded yet) but then it
1103 // could have them as well and it's better to err on this side rather than
1104 // disabling some operations which are restricted to the items with
1105 // children for an item which does have them
1106 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1109 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1111 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1113 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1116 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1118 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1120 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1123 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1125 wxCHECK_MSG( item
.IsOk(), FALSE
, wxT("invalid tree item") );
1127 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1130 // -----------------------------------------------------------------------------
1132 // -----------------------------------------------------------------------------
1134 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1136 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1138 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1141 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1142 wxTreeItemIdValue
& cookie
) const
1144 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1147 return GetNextChild(item
, cookie
);
1150 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1151 wxTreeItemIdValue
& cookie
) const
1153 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1155 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1157 // it's ok to cast cookie to size_t, we never have indices big enough to
1158 // overflow "void *"
1159 size_t *pIndex
= (size_t *)&cookie
;
1160 if ( *pIndex
< children
.Count() )
1162 return children
.Item(*pIndex
++);
1166 // there are no more of them
1167 return wxTreeItemId();
1171 #if WXWIN_COMPATIBILITY_2_4
1173 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1176 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1179 return GetNextChild(item
, cookie
);
1182 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1185 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1187 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1188 if ( (size_t)cookie
< children
.Count() )
1190 return children
.Item((size_t)cookie
++);
1194 // there are no more of them
1195 return wxTreeItemId();
1199 #endif // WXWIN_COMPATIBILITY_2_4
1201 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1203 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1205 wxArrayGenericTreeItems
& children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1206 return (children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last()));
1209 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1211 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1213 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1214 wxGenericTreeItem
*parent
= i
->GetParent();
1215 if ( parent
== NULL
)
1217 // root item doesn't have any siblings
1218 return wxTreeItemId();
1221 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1222 int index
= siblings
.Index(i
);
1223 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1225 size_t n
= (size_t)(index
+ 1);
1226 return n
== siblings
.Count() ? wxTreeItemId() : wxTreeItemId(siblings
[n
]);
1229 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1231 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1233 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1234 wxGenericTreeItem
*parent
= i
->GetParent();
1235 if ( parent
== NULL
)
1237 // root item doesn't have any siblings
1238 return wxTreeItemId();
1241 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1242 int index
= siblings
.Index(i
);
1243 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1245 return index
== 0 ? wxTreeItemId()
1246 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1249 // Only for internal use right now, but should probably be public
1250 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1252 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1254 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1256 // First see if there are any children.
1257 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1258 if (children
.GetCount() > 0)
1260 return children
.Item(0);
1264 // Try a sibling of this or ancestor instead
1265 wxTreeItemId p
= item
;
1266 wxTreeItemId toFind
;
1269 toFind
= GetNextSibling(p
);
1270 p
= GetItemParent(p
);
1271 } while (p
.IsOk() && !toFind
.IsOk());
1276 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1278 wxTreeItemId id
= GetRootItem();
1287 } while (id
.IsOk());
1289 return wxTreeItemId();
1292 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1294 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1296 wxTreeItemId id
= item
;
1299 while (id
= GetNext(id
), id
.IsOk())
1305 return wxTreeItemId();
1308 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1310 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1312 wxFAIL_MSG(wxT("not implemented"));
1314 return wxTreeItemId();
1317 // called by wxTextTreeCtrl when it marks itself for deletion
1318 void wxGenericTreeCtrl::ResetTextControl()
1323 // find the first item starting with the given prefix after the given item
1324 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1325 const wxString
& prefixOrig
) const
1327 // match is case insensitive as this is more convenient to the user: having
1328 // to press Shift-letter to go to the item starting with a capital letter
1329 // would be too bothersome
1330 wxString prefix
= prefixOrig
.Lower();
1332 // determine the starting point: we shouldn't take the current item (this
1333 // allows to switch between two items starting with the same letter just by
1334 // pressing it) but we shouldn't jump to the next one if the user is
1335 // continuing to type as otherwise he might easily skip the item he wanted
1336 wxTreeItemId id
= idParent
;
1337 if ( prefix
.length() == 1 )
1342 // look for the item starting with the given prefix after it
1343 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1348 // if we haven't found anything...
1351 // ... wrap to the beginning
1353 if ( HasFlag(wxTR_HIDE_ROOT
) )
1355 // can't select virtual root
1359 // and try all the items (stop when we get to the one we started from)
1360 while ( id
!= idParent
&& !GetItemText(id
).Lower().StartsWith(prefix
) )
1369 // -----------------------------------------------------------------------------
1371 // -----------------------------------------------------------------------------
1373 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1375 const wxString
& text
,
1376 int image
, int selImage
,
1377 wxTreeItemData
*data
)
1379 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1382 // should we give a warning here?
1383 return AddRoot(text
, image
, selImage
, data
);
1386 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1388 wxGenericTreeItem
*item
=
1389 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1393 data
->m_pItem
= item
;
1396 parent
->Insert( item
, previous
);
1401 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1402 int image
, int selImage
,
1403 wxTreeItemData
*data
)
1405 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), wxT("tree can have only one root") );
1407 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1409 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1410 image
, selImage
, data
);
1413 data
->m_pItem
= m_anchor
;
1416 if (HasFlag(wxTR_HIDE_ROOT
))
1418 // if root is hidden, make sure we can navigate
1420 m_anchor
->SetHasPlus();
1422 CalculatePositions();
1425 if (!HasFlag(wxTR_MULTIPLE
))
1427 m_current
= m_key_current
= m_anchor
;
1428 m_current
->SetHilight( TRUE
);
1434 wxTreeItemId
wxGenericTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1435 const wxString
& text
,
1436 int image
, int selImage
,
1437 wxTreeItemData
*data
)
1439 return DoInsertItem(parent
, 0u, text
, image
, selImage
, data
);
1442 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1443 const wxTreeItemId
& idPrevious
,
1444 const wxString
& text
,
1445 int image
, int selImage
,
1446 wxTreeItemData
*data
)
1448 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1451 // should we give a warning here?
1452 return AddRoot(text
, image
, selImage
, data
);
1456 if (idPrevious
.IsOk())
1458 index
= parent
->GetChildren().Index((wxGenericTreeItem
*) idPrevious
.m_pItem
);
1459 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1460 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1463 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1466 wxTreeItemId
wxGenericTreeCtrl::InsertItem(const wxTreeItemId
& parentId
,
1468 const wxString
& text
,
1469 int image
, int selImage
,
1470 wxTreeItemData
*data
)
1472 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1475 // should we give a warning here?
1476 return AddRoot(text
, image
, selImage
, data
);
1479 return DoInsertItem(parentId
, before
, text
, image
, selImage
, data
);
1482 wxTreeItemId
wxGenericTreeCtrl::AppendItem(const wxTreeItemId
& parentId
,
1483 const wxString
& text
,
1484 int image
, int selImage
,
1485 wxTreeItemData
*data
)
1487 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1490 // should we give a warning here?
1491 return AddRoot(text
, image
, selImage
, data
);
1494 return DoInsertItem( parent
, parent
->GetChildren().Count(), text
,
1495 image
, selImage
, data
);
1498 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1500 wxTreeEvent
event( wxEVT_COMMAND_TREE_DELETE_ITEM
, GetId() );
1501 event
.m_item
= item
;
1502 event
.SetEventObject( this );
1503 ProcessEvent( event
);
1506 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1508 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1510 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1511 item
->DeleteChildren(this);
1514 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1516 m_dirty
= TRUE
; // do this first so stuff below doesn't cause flicker
1518 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1520 wxGenericTreeItem
*parent
= item
->GetParent();
1522 // don't keep stale pointers around!
1523 if ( IsDescendantOf(item
, m_key_current
) )
1525 // Don't silently change the selection:
1526 // do it properly in idle time, so event
1527 // handlers get called.
1529 // m_key_current = parent;
1530 m_key_current
= NULL
;
1533 // m_select_me records whether we need to select
1534 // a different item, in idle time.
1535 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1537 m_select_me
= parent
;
1540 if ( IsDescendantOf(item
, m_current
) )
1542 // Don't silently change the selection:
1543 // do it properly in idle time, so event
1544 // handlers get called.
1546 // m_current = parent;
1548 m_select_me
= parent
;
1551 // remove the item from the tree
1554 parent
->GetChildren().Remove( item
); // remove by value
1556 else // deleting the root
1558 // nothing will be left in the tree
1562 // and delete all of its children and the item itself now
1563 item
->DeleteChildren(this);
1564 SendDeleteEvent(item
);
1568 void wxGenericTreeCtrl::DeleteAllItems()
1576 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1578 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1580 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1581 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1582 _T("can't expand hidden root") );
1584 if ( !item
->HasPlus() )
1587 if ( item
->IsExpanded() )
1590 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_EXPANDING
, GetId() );
1591 event
.m_item
= item
;
1592 event
.SetEventObject( this );
1594 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1596 // cancelled by program
1601 CalculatePositions();
1603 RefreshSubtree(item
);
1605 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1606 ProcessEvent( event
);
1609 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId
& item
)
1611 if ( !HasFlag(wxTR_HIDE_ROOT
) || item
!= GetRootItem())
1614 if ( !IsExpanded(item
) )
1618 wxTreeItemIdValue cookie
;
1619 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1620 while ( child
.IsOk() )
1624 child
= GetNextChild(item
, cookie
);
1628 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1630 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1631 _T("can't collapse hidden root") );
1633 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1635 if ( !item
->IsExpanded() )
1638 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, GetId() );
1639 event
.m_item
= item
;
1640 event
.SetEventObject( this );
1641 if ( ProcessEvent( event
) && !event
.IsAllowed() )
1643 // cancelled by program
1649 #if 0 // TODO why should items be collapsed recursively?
1650 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1651 size_t count
= children
.Count();
1652 for ( size_t n
= 0; n
< count
; n
++ )
1654 Collapse(children
[n
]);
1658 CalculatePositions();
1660 RefreshSubtree(item
);
1662 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1663 ProcessEvent( event
);
1666 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1669 DeleteChildren(item
);
1672 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1674 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1676 if (item
->IsExpanded())
1682 void wxGenericTreeCtrl::Unselect()
1686 m_current
->SetHilight( FALSE
);
1687 RefreshLine( m_current
);
1694 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1696 if (item
->IsSelected())
1698 item
->SetHilight(FALSE
);
1702 if (item
->HasChildren())
1704 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1705 size_t count
= children
.Count();
1706 for ( size_t n
= 0; n
< count
; ++n
)
1708 UnselectAllChildren(children
[n
]);
1713 void wxGenericTreeCtrl::UnselectAll()
1715 wxTreeItemId rootItem
= GetRootItem();
1717 // the tree might not have the root item at all
1720 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1724 // Recursive function !
1725 // To stop we must have crt_item<last_item
1727 // Tag all next children, when no more children,
1728 // Move to parent (not to tag)
1729 // Keep going... if we found last_item, we stop.
1730 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1732 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1734 if (parent
== NULL
) // This is root item
1735 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1737 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1738 int index
= children
.Index(crt_item
);
1739 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1741 size_t count
= children
.Count();
1742 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1744 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
)) return TRUE
;
1747 return TagNextChildren(parent
, last_item
, select
);
1750 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
, wxGenericTreeItem
*last_item
, bool select
)
1752 crt_item
->SetHilight(select
);
1753 RefreshLine(crt_item
);
1755 if (crt_item
==last_item
)
1758 if (crt_item
->HasChildren())
1760 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1761 size_t count
= children
.Count();
1762 for ( size_t n
= 0; n
< count
; ++n
)
1764 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1772 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
, wxGenericTreeItem
*item2
)
1774 // item2 is not necessary after item1
1775 wxGenericTreeItem
*first
=NULL
, *last
=NULL
;
1778 // choice first' and 'last' between item1 and item2
1779 if (item1
->GetY()<item2
->GetY())
1790 bool select
= m_current
->IsSelected();
1792 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1795 TagNextChildren(first
,last
,select
);
1798 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
,
1799 bool unselect_others
,
1800 bool extended_select
)
1802 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1806 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1807 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1809 //wxCHECK_RET( ( (!unselect_others) && is_single),
1810 // wxT("this is a single selection tree") );
1812 // to keep going anyhow !!!
1815 if (item
->IsSelected())
1816 return; // nothing to do
1817 unselect_others
= TRUE
;
1818 extended_select
= FALSE
;
1820 else if ( unselect_others
&& item
->IsSelected() )
1822 // selection change if there is more than one item currently selected
1823 wxArrayTreeItemIds selected_items
;
1824 if ( GetSelections(selected_items
) == 1 )
1828 wxTreeEvent
event( wxEVT_COMMAND_TREE_SEL_CHANGING
, GetId() );
1829 event
.m_item
= item
;
1830 event
.m_itemOld
= m_current
;
1831 event
.SetEventObject( this );
1832 // TODO : Here we don't send any selection mode yet !
1834 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1837 wxTreeItemId parent
= GetItemParent( itemId
);
1838 while (parent
.IsOk())
1840 if (!IsExpanded(parent
))
1843 parent
= GetItemParent( parent
);
1846 EnsureVisible( itemId
);
1849 if (unselect_others
)
1851 if (is_single
) Unselect(); // to speed up thing
1856 if (extended_select
)
1860 m_current
= m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1863 // don't change the mark (m_current)
1864 SelectItemRange(m_current
, item
);
1868 bool select
=TRUE
; // the default
1870 // Check if we need to toggle hilight (ctrl mode)
1871 if (!unselect_others
)
1872 select
=!item
->IsSelected();
1874 m_current
= m_key_current
= item
;
1875 m_current
->SetHilight(select
);
1876 RefreshLine( m_current
);
1879 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1880 GetEventHandler()->ProcessEvent( event
);
1883 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1884 wxArrayTreeItemIds
&array
) const
1886 if ( item
->IsSelected() )
1887 array
.Add(wxTreeItemId(item
));
1889 if ( item
->HasChildren() )
1891 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1892 size_t count
= children
.GetCount();
1893 for ( size_t n
= 0; n
< count
; ++n
)
1894 FillArray(children
[n
], array
);
1898 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
1901 wxTreeItemId idRoot
= GetRootItem();
1902 if ( idRoot
.IsOk() )
1904 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
1906 //else: the tree is empty, so no selections
1908 return array
.Count();
1911 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1913 if (!item
.IsOk()) return;
1915 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1917 // first expand all parent branches
1918 wxGenericTreeItem
*parent
= gitem
->GetParent();
1920 if ( HasFlag(wxTR_HIDE_ROOT
) )
1922 while ( parent
&& parent
!= m_anchor
)
1925 parent
= parent
->GetParent();
1933 parent
= parent
->GetParent();
1937 //if (parent) CalculatePositions();
1942 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
1944 if (!item
.IsOk()) return;
1946 // We have to call this here because the label in
1947 // question might just have been added and no screen
1948 // update taken place.
1949 if (m_dirty
) wxYieldIfNeeded();
1951 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
1953 // now scroll to the item
1954 int item_y
= gitem
->GetY();
1958 GetViewStart( &start_x
, &start_y
);
1959 start_y
*= PIXELS_PER_UNIT
;
1963 GetClientSize( &client_w
, &client_h
);
1965 if (item_y
< start_y
+3)
1970 m_anchor
->GetSize( x
, y
, this );
1971 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1972 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1973 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1974 // Item should appear at top
1975 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, item_y
/PIXELS_PER_UNIT
);
1977 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
1982 m_anchor
->GetSize( x
, y
, this );
1983 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1984 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
1985 item_y
+= PIXELS_PER_UNIT
+2;
1986 int x_pos
= GetScrollPos( wxHORIZONTAL
);
1987 // Item should appear at bottom
1988 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
);
1992 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1993 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
1995 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
1996 wxGenericTreeItem
**item2
)
1998 wxCHECK_MSG( s_treeBeingSorted
, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2000 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2003 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2004 const wxTreeItemId
& item2
)
2006 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2009 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2011 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2013 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2015 wxCHECK_RET( !s_treeBeingSorted
,
2016 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2018 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2019 if ( children
.Count() > 1 )
2023 s_treeBeingSorted
= this;
2024 children
.Sort(tree_ctrl_compare_func
);
2025 s_treeBeingSorted
= NULL
;
2027 //else: don't make the tree dirty as nothing changed
2030 wxImageList
*wxGenericTreeCtrl::GetImageList() const
2032 return m_imageListNormal
;
2035 wxImageList
*wxGenericTreeCtrl::GetButtonsImageList() const
2037 return m_imageListButtons
;
2040 wxImageList
*wxGenericTreeCtrl::GetStateImageList() const
2042 return m_imageListState
;
2045 void wxGenericTreeCtrl::CalculateLineHeight()
2047 wxClientDC
dc(this);
2048 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2050 if ( m_imageListNormal
)
2052 // Calculate a m_lineHeight value from the normal Image sizes.
2053 // May be toggle off. Then wxGenericTreeCtrl will spread when
2054 // necessary (which might look ugly).
2055 int n
= m_imageListNormal
->GetImageCount();
2056 for (int i
= 0; i
< n
; i
++)
2058 int width
= 0, height
= 0;
2059 m_imageListNormal
->GetSize(i
, width
, height
);
2060 if (height
> m_lineHeight
) m_lineHeight
= height
;
2064 if (m_imageListButtons
)
2066 // Calculate a m_lineHeight value from the Button image sizes.
2067 // May be toggle off. Then wxGenericTreeCtrl will spread when
2068 // necessary (which might look ugly).
2069 int n
= m_imageListButtons
->GetImageCount();
2070 for (int i
= 0; i
< n
; i
++)
2072 int width
= 0, height
= 0;
2073 m_imageListButtons
->GetSize(i
, width
, height
);
2074 if (height
> m_lineHeight
) m_lineHeight
= height
;
2078 if (m_lineHeight
< 30)
2079 m_lineHeight
+= 2; // at least 2 pixels
2081 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2084 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2086 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2087 m_imageListNormal
= imageList
;
2088 m_ownsImageListNormal
= FALSE
;
2090 // Don't do any drawing if we're setting the list to NULL,
2091 // since we may be in the process of deleting the tree control.
2093 CalculateLineHeight();
2096 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2098 if (m_ownsImageListState
) delete m_imageListState
;
2099 m_imageListState
= imageList
;
2100 m_ownsImageListState
= FALSE
;
2103 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2105 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2106 m_imageListButtons
= imageList
;
2107 m_ownsImageListButtons
= FALSE
;
2109 CalculateLineHeight();
2112 void wxGenericTreeCtrl::AssignImageList(wxImageList
*imageList
)
2114 SetImageList(imageList
);
2115 m_ownsImageListNormal
= TRUE
;
2118 void wxGenericTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
2120 SetStateImageList(imageList
);
2121 m_ownsImageListState
= TRUE
;
2124 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2126 SetButtonsImageList(imageList
);
2127 m_ownsImageListButtons
= TRUE
;
2130 // -----------------------------------------------------------------------------
2132 // -----------------------------------------------------------------------------
2134 void wxGenericTreeCtrl::AdjustMyScrollbars()
2139 m_anchor
->GetSize( x
, y
, this );
2140 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2141 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2142 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2143 int y_pos
= GetScrollPos( wxVERTICAL
);
2144 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
, x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
, x_pos
, y_pos
);
2148 SetScrollbars( 0, 0, 0, 0 );
2152 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2154 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2155 return item
->GetHeight();
2157 return m_lineHeight
;
2160 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2162 // TODO implement "state" icon on items
2164 wxTreeItemAttr
*attr
= item
->GetAttributes();
2165 if ( attr
&& attr
->HasFont() )
2166 dc
.SetFont(attr
->GetFont());
2167 else if (item
->IsBold())
2168 dc
.SetFont(m_boldFont
);
2170 long text_w
= 0, text_h
= 0;
2171 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2173 int image_h
= 0, image_w
= 0;
2174 int image
= item
->GetCurrentImage();
2175 if ( image
!= NO_IMAGE
)
2177 if ( m_imageListNormal
)
2179 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2188 int total_h
= GetLineHeight(item
);
2190 if ( item
->IsSelected() )
2192 // under mac selections are only a rectangle in case they don't have the focus
2196 dc
.SetBrush( *wxTRANSPARENT_BRUSH
) ;
2197 dc
.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT
) , 1 , wxSOLID
) ) ;
2201 dc
.SetBrush( *m_hilightBrush
) ;
2204 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2210 if ( attr
&& attr
->HasBackgroundColour() )
2211 colBg
= attr
->GetBackgroundColour();
2213 colBg
= m_backgroundColour
;
2214 dc
.SetBrush(wxBrush(colBg
, wxSOLID
));
2217 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2219 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2223 DoGetPosition(&x
, &y
);
2225 dc
.DrawRectangle(x
, item
->GetY()+offset
, w
, total_h
-offset
);
2229 if ( item
->IsSelected() && image
!= NO_IMAGE
)
2231 // If it's selected, and there's an image, then we should
2232 // take care to leave the area under the image painted in the
2233 // background colour.
2234 dc
.DrawRectangle( item
->GetX() + image_w
- 2, item
->GetY()+offset
,
2235 item
->GetWidth() - image_w
+ 2, total_h
-offset
);
2239 dc
.DrawRectangle( item
->GetX()-2, item
->GetY()+offset
,
2240 item
->GetWidth()+2, total_h
-offset
);
2244 if ( image
!= NO_IMAGE
)
2246 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), image_w
-2, total_h
);
2247 m_imageListNormal
->Draw( image
, dc
,
2249 item
->GetY() +((total_h
> image_h
)?((total_h
-image_h
)/2):0),
2250 wxIMAGELIST_DRAW_TRANSPARENT
);
2251 dc
.DestroyClippingRegion();
2254 dc
.SetBackgroundMode(wxTRANSPARENT
);
2255 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2256 dc
.DrawText( item
->GetText(),
2257 (wxCoord
)(image_w
+ item
->GetX()),
2258 (wxCoord
)(item
->GetY() + extraH
));
2260 // restore normal font
2261 dc
.SetFont( m_normalFont
);
2264 // Now y stands for the top of the item, whereas it used to stand for middle !
2265 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
2267 int x
= level
*m_indent
;
2268 if (!HasFlag(wxTR_HIDE_ROOT
))
2272 else if (level
== 0)
2274 // always expand hidden root
2276 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2277 int count
= children
.Count();
2283 PaintLevel(children
[n
], dc
, 1, y
);
2284 } while (++n
< count
);
2286 if (!HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
) && count
> 0)
2288 // draw line down to last child
2289 origY
+= GetLineHeight(children
[0])>>1;
2290 oldY
+= GetLineHeight(children
[n
-1])>>1;
2291 dc
.DrawLine(3, origY
, 3, oldY
);
2297 item
->SetX(x
+m_spacing
);
2300 int h
= GetLineHeight(item
);
2302 int y_mid
= y_top
+ (h
>>1);
2305 int exposed_x
= dc
.LogicalToDeviceX(0);
2306 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2308 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2312 // don't draw rect outline if we already have the
2313 // background color under Mac
2314 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2315 #endif // !__WXMAC__
2319 if ( item
->IsSelected() )
2321 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2325 wxTreeItemAttr
*attr
= item
->GetAttributes();
2326 if (attr
&& attr
->HasTextColour())
2327 colText
= attr
->GetTextColour();
2329 colText
= GetForegroundColour();
2333 dc
.SetTextForeground(colText
);
2337 PaintItem(item
, dc
);
2339 if (HasFlag(wxTR_ROW_LINES
))
2341 // if the background colour is white, choose a
2342 // contrasting color for the lines
2343 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2344 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2345 dc
.DrawLine(0, y_top
, 10000, y_top
);
2346 dc
.DrawLine(0, y
, 10000, y
);
2349 // restore DC objects
2350 dc
.SetBrush(*wxWHITE_BRUSH
);
2351 dc
.SetPen(m_dottedPen
);
2352 dc
.SetTextForeground(*wxBLACK
);
2354 if (item
->HasPlus() && HasButtons()) // should the item show a button?
2356 if (!HasFlag(wxTR_NO_LINES
))
2358 if (x
> (signed)m_indent
)
2359 dc
.DrawLine(x
- m_indent
, y_mid
, x
- 5, y_mid
);
2360 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2361 dc
.DrawLine(3, y_mid
, x
- 5, y_mid
);
2362 dc
.DrawLine(x
+ 5, y_mid
, x
+ m_spacing
, y_mid
);
2365 if (m_imageListButtons
!= NULL
)
2367 // draw the image button here
2368 int image_h
= 0, image_w
= 0, image
= wxTreeItemIcon_Normal
;
2369 if (item
->IsExpanded()) image
= wxTreeItemIcon_Expanded
;
2370 if (item
->IsSelected())
2371 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2372 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2373 int xx
= x
- (image_w
>>1);
2374 int yy
= y_mid
- (image_h
>>1);
2375 dc
.SetClippingRegion(xx
, yy
, image_w
, image_h
);
2376 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2377 wxIMAGELIST_DRAW_TRANSPARENT
);
2378 dc
.DestroyClippingRegion();
2380 else if (HasFlag(wxTR_TWIST_BUTTONS
))
2382 // draw the twisty button here
2384 if (HasFlag(wxTR_AQUA_BUTTONS
))
2386 // This causes update problems, so disabling for now.
2387 #if 0 // def __WXMAC__
2388 wxMacPortSetter
helper(&dc
) ;
2389 wxMacWindowClipper
clipper(this) ;
2390 wxDC::MacSetupBackgroundForCurrentPort( MacGetBackgroundBrush() ) ;
2393 int loc_y
= y_mid
- 6 ;
2394 MacWindowToRootWindow( & loc_x
, & loc_y
) ;
2395 Rect bounds
= { loc_y
, loc_x
, loc_y
+ 18 , loc_x
+ 12 } ;
2396 ThemeButtonDrawInfo info
= { kThemeStateActive
, item
->IsExpanded() ? kThemeDisclosureDown
: kThemeDisclosureRight
,
2397 kThemeAdornmentNone
};
2398 DrawThemeButton( &bounds
, kThemeDisclosureButton
,
2399 &info
, NULL
, NULL
, NULL
, NULL
) ;
2401 if (item
->IsExpanded())
2402 dc
.DrawBitmap( *m_arrowDown
, x
-5, y_mid
-6, TRUE
);
2404 dc
.DrawBitmap( *m_arrowRight
, x
-5, y_mid
-6, TRUE
);
2409 dc
.SetBrush(*m_hilightBrush
);
2410 dc
.SetPen(*wxBLACK_PEN
);
2413 if (item
->IsExpanded())
2416 button
[0].y
= y_mid
-2;
2418 button
[1].y
= y_mid
-2;
2420 button
[2].y
= y_mid
+3;
2424 button
[0].y
= y_mid
-5;
2426 button
[1].y
= y_mid
+5;
2428 button
[2].y
= y_mid
;
2431 dc
.DrawPolygon(3, button
);
2432 dc
.SetPen(m_dottedPen
);
2435 else // if (HasFlag(wxTR_HAS_BUTTONS))
2437 // draw the plus sign here
2438 dc
.SetPen(*wxGREY_PEN
);
2439 dc
.SetBrush(*wxWHITE_BRUSH
);
2440 dc
.DrawRectangle(x
-5, y_mid
-4, 11, 9);
2441 dc
.SetPen(*wxBLACK_PEN
);
2442 dc
.DrawLine(x
-2, y_mid
, x
+3, y_mid
);
2443 if (!item
->IsExpanded())
2444 dc
.DrawLine(x
, y_mid
-2, x
, y_mid
+3);
2445 dc
.SetPen(m_dottedPen
);
2448 else if (!HasFlag(wxTR_NO_LINES
)) // no button; maybe a line?
2450 // draw the horizontal line here
2452 if (x
> (signed)m_indent
)
2453 x_start
-= m_indent
;
2454 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2456 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2460 if (item
->IsExpanded())
2462 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2463 int count
= children
.Count();
2470 PaintLevel(children
[n
], dc
, level
, y
);
2471 } while (++n
< count
);
2473 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2475 // draw line down to last child
2476 oldY
+= GetLineHeight(children
[n
-1])>>1;
2477 if (HasButtons()) y_mid
+= 5;
2479 // Only draw the portion of the line that is visible, in case it is huge
2480 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2481 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2482 yOrigin
= abs(yOrigin
);
2483 GetClientSize(&width
, &height
);
2485 // Move end points to the begining/end of the view?
2486 if (y_mid
< yOrigin
)
2488 if (oldY
> yOrigin
+ height
)
2489 oldY
= yOrigin
+ height
;
2491 // after the adjustments if y_mid is larger than oldY then the line
2492 // isn't visible at all so don't draw anything
2494 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2500 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2504 if ( item
->HasPlus() )
2506 // it's a folder, indicate it by a border
2511 // draw a line under the drop target because the item will be
2513 DrawLine(item
, TRUE
/* below */);
2516 SetCursor(wxCURSOR_BULLSEYE
);
2521 SetCursor(wxCURSOR_NO_ENTRY
);
2525 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2527 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2529 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2531 wxClientDC
dc(this);
2533 dc
.SetLogicalFunction(wxINVERT
);
2534 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2536 int w
= i
->GetWidth() + 2;
2537 int h
= GetLineHeight(i
) + 2;
2539 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2542 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2544 wxCHECK_RET( item
.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2546 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2548 wxClientDC
dc(this);
2550 dc
.SetLogicalFunction(wxINVERT
);
2556 y
+= GetLineHeight(i
) - 1;
2559 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2562 // -----------------------------------------------------------------------------
2563 // wxWindows callbacks
2564 // -----------------------------------------------------------------------------
2566 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2574 dc
.SetFont( m_normalFont
);
2575 dc
.SetPen( m_dottedPen
);
2577 // this is now done dynamically
2578 //if(GetImageList() == NULL)
2579 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2582 PaintLevel( m_anchor
, dc
, 0, y
);
2585 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2594 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2603 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2605 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, GetId() );
2606 te
.m_evtKey
= event
;
2607 te
.SetEventObject( this );
2608 if ( GetEventHandler()->ProcessEvent( te
) )
2610 // intercepted by the user code
2614 if ( (m_current
== 0) || (m_key_current
== 0) )
2620 // how should the selection work for this event?
2621 bool is_multiple
, extended_select
, unselect_others
;
2622 EventFlagsToSelType(GetWindowStyleFlag(),
2624 event
.ControlDown(),
2625 is_multiple
, extended_select
, unselect_others
);
2629 // * : Expand all/Collapse all
2630 // ' ' | return : activate
2631 // up : go up (not last children!)
2633 // left : go to parent
2634 // right : open if parent and go next
2635 // home : go to root
2636 // end : go to last item without opening parents
2637 // alnum : start or continue searching for the item with this prefix
2638 int keyCode
= event
.GetKeyCode();
2643 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2651 if ( !IsExpanded(m_current
) )
2654 ExpandAll(m_current
);
2657 //else: fall through to Collapse() it
2661 if (IsExpanded(m_current
))
2663 Collapse(m_current
);
2669 if ( !event
.HasModifiers() )
2671 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
2672 event
.m_item
= m_current
;
2673 event
.SetEventObject( this );
2674 GetEventHandler()->ProcessEvent( event
);
2677 // in any case, also generate the normal key event for this key,
2678 // even if we generated the ACTIVATED event above: this is what
2679 // wxMSW does and it makes sense because you might not want to
2680 // process ACTIVATED event at all and handle Space and Return
2681 // directly (and differently) which would be impossible otherwise
2685 // up goes to the previous sibling or to the last
2686 // of its children if it's expanded
2689 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2692 prev
= GetItemParent( m_key_current
);
2693 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2695 break; // don't go to root if it is hidden
2699 wxTreeItemIdValue cookie
;
2700 wxTreeItemId current
= m_key_current
;
2701 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2702 if (current
== GetFirstChild( prev
, cookie
))
2704 // otherwise we return to where we came from
2705 SelectItem( prev
, unselect_others
, extended_select
);
2706 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2713 while ( IsExpanded(prev
) && HasChildren(prev
) )
2715 wxTreeItemId child
= GetLastChild(prev
);
2722 SelectItem( prev
, unselect_others
, extended_select
);
2723 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2728 // left arrow goes to the parent
2731 wxTreeItemId prev
= GetItemParent( m_current
);
2732 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2734 // don't go to root if it is hidden
2735 prev
= GetPrevSibling( m_current
);
2739 SelectItem( prev
, unselect_others
, extended_select
);
2745 // this works the same as the down arrow except that we
2746 // also expand the item if it wasn't expanded yet
2752 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
2754 wxTreeItemIdValue cookie
;
2755 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
2756 SelectItem( child
, unselect_others
, extended_select
);
2757 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
2761 wxTreeItemId next
= GetNextSibling( m_key_current
);
2764 wxTreeItemId current
= m_key_current
;
2765 while (current
&& !next
)
2767 current
= GetItemParent( current
);
2768 if (current
) next
= GetNextSibling( current
);
2773 SelectItem( next
, unselect_others
, extended_select
);
2774 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
2780 // <End> selects the last visible tree item
2783 wxTreeItemId last
= GetRootItem();
2785 while ( last
.IsOk() && IsExpanded(last
) )
2787 wxTreeItemId lastChild
= GetLastChild(last
);
2789 // it may happen if the item was expanded but then all of
2790 // its children have been deleted - so IsExpanded() returned
2791 // TRUE, but GetLastChild() returned invalid item
2800 SelectItem( last
, unselect_others
, extended_select
);
2805 // <Home> selects the root item
2808 wxTreeItemId prev
= GetRootItem();
2812 if ( HasFlag(wxTR_HIDE_ROOT
) )
2814 wxTreeItemIdValue cookie
;
2815 prev
= GetFirstChild(prev
, cookie
);
2820 SelectItem( prev
, unselect_others
, extended_select
);
2825 // do not use wxIsalnum() here
2826 if ( !event
.HasModifiers() &&
2827 ((keyCode
>= '0' && keyCode
<= '9') ||
2828 (keyCode
>= 'a' && keyCode
<= 'z') ||
2829 (keyCode
>= 'A' && keyCode
<= 'Z' )))
2831 // find the next item starting with the given prefix
2832 char ch
= (char)keyCode
;
2834 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ (wxChar
)ch
);
2845 // also start the timer to reset the current prefix if the user
2846 // doesn't press any more alnum keys soon -- we wouldn't want
2847 // to use this prefix for a new item search
2850 m_findTimer
= new wxTreeFindTimer(this);
2853 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
2862 wxTreeItemId
wxGenericTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2864 // JACS: removed wxYieldIfNeeded() because it can cause the window
2865 // to be deleted from under us if a close window event is pending
2870 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
2871 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
2872 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
2873 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
2874 if (flags
) return wxTreeItemId();
2876 if (m_anchor
== NULL
)
2878 flags
= wxTREE_HITTEST_NOWHERE
;
2879 return wxTreeItemId();
2882 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
2886 flags
= wxTREE_HITTEST_NOWHERE
;
2887 return wxTreeItemId();
2892 // get the bounding rectangle of the item (or of its label only)
2893 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2895 bool WXUNUSED(textOnly
)) const
2897 wxCHECK_MSG( item
.IsOk(), FALSE
, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2899 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2902 GetViewStart(& startX
, & startY
);
2904 rect
.x
= i
->GetX() - startX
*PIXELS_PER_UNIT
;
2905 rect
.y
= i
->GetY() - startY
*PIXELS_PER_UNIT
;
2906 rect
.width
= i
->GetWidth();
2907 //rect.height = i->GetHeight();
2908 rect
.height
= GetLineHeight(i
);
2913 void wxGenericTreeCtrl::Edit( const wxTreeItemId
& item
)
2915 wxCHECK_RET( item
.IsOk(), _T("can't edit an invalid item") );
2917 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
2919 wxTreeEvent
te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, GetId() );
2920 te
.m_item
= itemEdit
;
2921 te
.SetEventObject( this );
2922 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
2928 // We have to call this here because the label in
2929 // question might just have been added and no screen
2930 // update taken place.
2934 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
2936 m_textCtrl
->SetFocus();
2939 // returns a pointer to the text edit control if the item is being
2940 // edited, NULL otherwise (it's assumed that no more than one item may
2941 // be edited simultaneously)
2942 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
2947 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
2948 const wxString
& value
)
2950 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2952 le
.SetEventObject( this );
2954 le
.m_editCancelled
= FALSE
;
2956 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2959 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
2961 // let owner know that the edit was cancelled
2962 wxTreeEvent
le( wxEVT_COMMAND_TREE_END_LABEL_EDIT
, GetId() );
2964 le
.SetEventObject( this );
2965 le
.m_label
= wxEmptyString
;
2966 le
.m_editCancelled
= TRUE
;
2968 GetEventHandler()->ProcessEvent( le
);
2974 void wxGenericTreeCtrl::OnRenameTimer()
2979 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
2981 if ( !m_anchor
) return;
2983 // we process left mouse up event (enables in-place edit), right down
2984 // (pass to the user code), left dbl click (activate item) and
2985 // dragging/moving events for items drag-and-drop
2986 if ( !(event
.LeftDown() ||
2988 event
.RightDown() ||
2989 event
.LeftDClick() ||
2991 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
2998 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3001 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3003 if ( event
.Dragging() && !m_isDragging
)
3005 if (m_dragCount
== 0)
3010 if (m_dragCount
!= 3)
3012 // wait until user drags a bit further...
3016 wxEventType command
= event
.RightIsDown()
3017 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3018 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3020 wxTreeEvent
nevent( command
, GetId() );
3021 nevent
.m_item
= m_current
;
3022 nevent
.SetEventObject(this);
3024 // by default the dragging is not supported, the user code must
3025 // explicitly allow the event for it to take place
3028 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3030 // we're going to drag this item
3031 m_isDragging
= TRUE
;
3033 // remember the old cursor because we will change it while
3035 m_oldCursor
= m_cursor
;
3037 // in a single selection control, hide the selection temporarily
3038 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3040 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3042 if ( m_oldSelection
)
3044 m_oldSelection
->SetHilight(FALSE
);
3045 RefreshLine(m_oldSelection
);
3052 else if ( event
.Moving() )
3054 if ( item
!= m_dropTarget
)
3056 // unhighlight the previous drop target
3057 DrawDropEffect(m_dropTarget
);
3059 m_dropTarget
= item
;
3061 // highlight the current drop target if any
3062 DrawDropEffect(m_dropTarget
);
3067 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3069 // erase the highlighting
3070 DrawDropEffect(m_dropTarget
);
3072 if ( m_oldSelection
)
3074 m_oldSelection
->SetHilight(TRUE
);
3075 RefreshLine(m_oldSelection
);
3076 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3079 // generate the drag end event
3080 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, GetId());
3082 event
.m_item
= item
;
3083 event
.m_pointDrag
= pt
;
3084 event
.SetEventObject(this);
3086 (void)GetEventHandler()->ProcessEvent(event
);
3088 m_isDragging
= FALSE
;
3089 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3093 SetCursor(m_oldCursor
);
3099 // here we process only the messages which happen on tree items
3103 if (item
== NULL
) return; /* we hit the blank area */
3105 if ( event
.RightDown() )
3107 wxTreeEvent
nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, GetId());
3108 nevent
.m_item
= item
;
3109 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3110 nevent
.SetEventObject(this);
3111 GetEventHandler()->ProcessEvent(nevent
);
3113 else if ( event
.LeftUp() )
3115 // this facilitates multiple-item drag-and-drop
3117 if (item
&& HasFlag(wxTR_MULTIPLE
))
3119 wxArrayTreeItemIds selections
;
3120 size_t count
= GetSelections(selections
);
3123 !event
.ControlDown() &&
3126 SelectItem(item
, true, false);
3132 if ( (item
== m_current
) &&
3133 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3134 HasFlag(wxTR_EDIT_LABELS
) )
3136 if ( m_renameTimer
)
3138 if ( m_renameTimer
->IsRunning() )
3139 m_renameTimer
->Stop();
3143 m_renameTimer
= new wxTreeRenameTimer( this );
3146 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, TRUE
);
3149 m_lastOnSame
= FALSE
;
3152 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3154 if ( event
.LeftDown() )
3156 m_lastOnSame
= item
== m_current
;
3159 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3161 // only toggle the item for a single click, double click on
3162 // the button doesn't do anything (it toggles the item twice)
3163 if ( event
.LeftDown() )
3168 // don't select the item if the button was clicked
3173 // clear the previously selected items, if the
3174 // user clicked outside of the present selection.
3175 // otherwise, perform the deselection on mouse-up.
3176 // this allows multiple drag and drop to work.
3178 if (!IsSelected(item
))
3180 // how should the selection work for this event?
3181 bool is_multiple
, extended_select
, unselect_others
;
3182 EventFlagsToSelType(GetWindowStyleFlag(),
3184 event
.ControlDown(),
3185 is_multiple
, extended_select
, unselect_others
);
3187 SelectItem(item
, unselect_others
, extended_select
);
3191 // For some reason, Windows isn't recognizing a left double-click,
3192 // so we need to simulate it here. Allow 200 milliseconds for now.
3193 if ( event
.LeftDClick() )
3195 // double clicking should not start editing the item label
3196 if ( m_renameTimer
)
3197 m_renameTimer
->Stop();
3199 m_lastOnSame
= FALSE
;
3201 // send activate event first
3202 wxTreeEvent
nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, GetId() );
3203 nevent
.m_item
= item
;
3204 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3205 nevent
.SetEventObject( this );
3206 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3208 // if the user code didn't process the activate event,
3209 // handle it ourselves by toggling the item when it is
3211 if ( item
->HasPlus() )
3221 void wxGenericTreeCtrl::OnInternalIdle()
3223 wxWindow::OnInternalIdle();
3225 // Check if we need to select the root item
3226 // because nothing else has been selected.
3227 // Delaying it means that we can invoke event handlers
3228 // as required, when a first item is selected.
3229 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3232 SelectItem(m_select_me
);
3233 else if (GetRootItem().IsOk())
3234 SelectItem(GetRootItem());
3237 /* after all changes have been done to the tree control,
3238 * we actually redraw the tree when everything is over */
3240 if (!m_dirty
) return;
3244 CalculatePositions();
3246 AdjustMyScrollbars();
3249 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
3254 wxTreeItemAttr
*attr
= item
->GetAttributes();
3255 if ( attr
&& attr
->HasFont() )
3256 dc
.SetFont(attr
->GetFont());
3257 else if ( item
->IsBold() )
3258 dc
.SetFont(m_boldFont
);
3260 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
3263 // restore normal font
3264 dc
.SetFont( m_normalFont
);
3268 int image
= item
->GetCurrentImage();
3269 if ( image
!= NO_IMAGE
)
3271 if ( m_imageListNormal
)
3273 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3278 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
3281 total_h
+= 2; // at least 2 pixels
3283 total_h
+= total_h
/10; // otherwise 10% extra spacing
3285 item
->SetHeight(total_h
);
3286 if (total_h
>m_lineHeight
)
3287 m_lineHeight
=total_h
;
3289 item
->SetWidth(image_w
+text_w
+2);
3292 // -----------------------------------------------------------------------------
3293 // for developper : y is now the top of the level
3294 // not the middle of it !
3295 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem
*item
, wxDC
&dc
, int level
, int &y
)
3297 int x
= level
*m_indent
;
3298 if (!HasFlag(wxTR_HIDE_ROOT
))
3302 else if (level
== 0)
3304 // a hidden root is not evaluated, but its
3305 // children are always calculated
3309 CalculateSize( item
, dc
);
3312 item
->SetX( x
+m_spacing
);
3314 y
+= GetLineHeight(item
);
3316 if ( !item
->IsExpanded() )
3318 // we don't need to calculate collapsed branches
3323 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3324 size_t n
, count
= children
.Count();
3326 for (n
= 0; n
< count
; ++n
)
3327 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3330 void wxGenericTreeCtrl::CalculatePositions()
3332 if ( !m_anchor
) return;
3334 wxClientDC
dc(this);
3337 dc
.SetFont( m_normalFont
);
3339 dc
.SetPen( m_dottedPen
);
3340 //if(GetImageList() == NULL)
3341 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3344 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3347 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3349 if (m_dirty
) return;
3351 wxSize client
= GetClientSize();
3354 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3355 rect
.width
= client
.x
;
3356 rect
.height
= client
.y
;
3358 Refresh(TRUE
, &rect
);
3360 AdjustMyScrollbars();
3363 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3365 if (m_dirty
) return;
3368 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3369 rect
.width
= GetClientSize().x
;
3370 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3372 Refresh(TRUE
, &rect
);
3375 void wxGenericTreeCtrl::RefreshSelected()
3377 // TODO: this is awfully inefficient, we should keep the list of all
3378 // selected items internally, should be much faster
3380 RefreshSelectedUnder(m_anchor
);
3383 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3385 if ( item
->IsSelected() )
3388 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3389 size_t count
= children
.GetCount();
3390 for ( size_t n
= 0; n
< count
; n
++ )
3392 RefreshSelectedUnder(children
[n
]);
3396 // ----------------------------------------------------------------------------
3397 // changing colours: we need to refresh the tree control
3398 // ----------------------------------------------------------------------------
3400 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3402 if ( !wxWindow::SetBackgroundColour(colour
) )
3410 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3412 if ( !wxWindow::SetForegroundColour(colour
) )
3420 #endif // wxUSE_TREECTRL