1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treectlg.cpp
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/listbox.h"
36 #include "wx/textctrl.h"
39 #include "wx/generic/treectlg.h"
40 #include "wx/imaglist.h"
42 #include "wx/renderer.h"
45 #include "wx/osx/private.h"
48 // -----------------------------------------------------------------------------
50 // -----------------------------------------------------------------------------
52 class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem
;
54 WX_DEFINE_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 static const int NO_IMAGE
= -1;
62 static const int PIXELS_PER_UNIT
= 10;
64 // the margin between the item state image and the item normal image
65 static const int MARGIN_BETWEEN_STATE_AND_IMAGE
= 2;
67 // the margin between the item image and the item text
68 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
70 // -----------------------------------------------------------------------------
72 // -----------------------------------------------------------------------------
74 // timer used for enabling in-place edit
75 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
78 // start editing the current item after half a second (if the mouse hasn't
79 // been clicked/moved)
82 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
84 virtual void Notify();
87 wxGenericTreeCtrl
*m_owner
;
89 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer
)
92 // control used for in-place edit
93 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
96 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
98 void EndEdit( bool discardChanges
);
100 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
103 void OnChar( wxKeyEvent
&event
);
104 void OnKeyUp( wxKeyEvent
&event
);
105 void OnKillFocus( wxFocusEvent
&event
);
107 bool AcceptChanges();
108 void Finish( bool setfocus
);
111 wxGenericTreeCtrl
*m_owner
;
112 wxGenericTreeItem
*m_itemEdited
;
113 wxString m_startValue
;
114 bool m_aboutToFinish
;
116 DECLARE_EVENT_TABLE()
117 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl
)
120 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
121 // for a sufficiently long time
122 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
125 // reset the current prefix after half a second of inactivity
126 enum { DELAY
= 500 };
128 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
130 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
133 wxGenericTreeCtrl
*m_owner
;
135 DECLARE_NO_COPY_CLASS(wxTreeFindTimer
)
139 class WXDLLEXPORT wxGenericTreeItem
143 wxGenericTreeItem() { m_data
= NULL
; }
144 wxGenericTreeItem( wxGenericTreeItem
*parent
,
145 const wxString
& text
,
148 wxTreeItemData
*data
);
150 ~wxGenericTreeItem();
153 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
155 const wxString
& GetText() const { return m_text
; }
156 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
157 { return m_images
[which
]; }
158 wxTreeItemData
*GetData() const { return m_data
; }
159 int GetState() const { return m_state
; }
161 // returns the current image for the item (depending on its
162 // selected/expanded/whatever state)
163 int GetCurrentImage() const;
165 void SetText( const wxString
&text
) { m_text
= text
; }
166 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
167 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
168 void SetState(int state
) { m_state
= state
; }
170 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
172 void SetBold(bool bold
) { m_isBold
= bold
; }
174 int GetX() const { return m_x
; }
175 int GetY() const { return m_y
; }
177 void SetX(int x
) { m_x
= x
; }
178 void SetY(int y
) { m_y
= y
; }
180 int GetHeight() const { return m_height
; }
181 int GetWidth() const { return m_width
; }
183 void SetHeight(int h
) { m_height
= h
; }
184 void SetWidth(int w
) { m_width
= w
; }
186 wxGenericTreeItem
*GetParent() const { return m_parent
; }
190 // deletes all children notifying the treectrl about it
191 void DeleteChildren(wxGenericTreeCtrl
*tree
);
193 // get count of all children (and grand children if 'recursively')
194 size_t GetChildrenCount(bool recursively
= true) const;
196 void Insert(wxGenericTreeItem
*child
, size_t index
)
197 { m_children
.Insert(child
, index
); }
199 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
201 // return the item at given position (or NULL if no item), onButton is
202 // true if the point belongs to the item's button, otherwise it lies
203 // on the item's label
204 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
205 const wxGenericTreeCtrl
*,
209 void Expand() { m_isCollapsed
= false; }
210 void Collapse() { m_isCollapsed
= true; }
212 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
215 bool HasChildren() const { return !m_children
.IsEmpty(); }
216 bool IsSelected() const { return m_hasHilight
!= 0; }
217 bool IsExpanded() const { return !m_isCollapsed
; }
218 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
219 bool IsBold() const { return m_isBold
!= 0; }
222 // get them - may be NULL
223 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
224 // get them ensuring that the pointer is not NULL
225 wxTreeItemAttr
& Attr()
229 m_attr
= new wxTreeItemAttr
;
235 void SetAttributes(wxTreeItemAttr
*attr
)
237 if ( m_ownsAttr
) delete m_attr
;
241 // set them and delete when done
242 void AssignAttributes(wxTreeItemAttr
*attr
)
249 // since there can be very many of these, we save size by chosing
250 // the smallest representation for the elements and by ordering
251 // the members to avoid padding.
252 wxString m_text
; // label to be rendered for item
254 wxTreeItemData
*m_data
; // user-provided data
256 int m_state
; // item state
258 wxArrayGenericTreeItems m_children
; // list of children
259 wxGenericTreeItem
*m_parent
; // parent of this item
261 wxTreeItemAttr
*m_attr
; // attributes???
263 // tree ctrl images for the normal, selected, expanded and
264 // expanded+selected states
265 int m_images
[wxTreeItemIcon_Max
];
267 wxCoord m_x
; // (virtual) offset from top
268 wxCoord m_y
; // (virtual) offset from left
269 int m_width
; // width of this item
270 int m_height
; // height of this item
272 // use bitfields to save size
273 unsigned int m_isCollapsed
:1;
274 unsigned int m_hasHilight
:1; // same as focused
275 unsigned int m_hasPlus
:1; // used for item which doesn't have
276 // children but has a [+] button
277 unsigned int m_isBold
:1; // render the label in bold font
278 unsigned int m_ownsAttr
:1; // delete attribute when done
280 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
283 // =============================================================================
285 // =============================================================================
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 // translate the key or mouse event flags to the type of selection we're
293 static void EventFlagsToSelType(long style
,
297 bool &extended_select
,
298 bool &unselect_others
)
300 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
301 extended_select
= shiftDown
&& is_multiple
;
302 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
305 // check if the given item is under another one
307 IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
311 if ( item
== parent
)
313 // item is a descendant of parent
317 item
= item
->GetParent();
323 // -----------------------------------------------------------------------------
324 // wxTreeRenameTimer (internal)
325 // -----------------------------------------------------------------------------
327 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
332 void wxTreeRenameTimer::Notify()
334 m_owner
->OnRenameTimer();
337 //-----------------------------------------------------------------------------
338 // wxTreeTextCtrl (internal)
339 //-----------------------------------------------------------------------------
341 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
342 EVT_CHAR (wxTreeTextCtrl::OnChar
)
343 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
344 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
347 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
348 wxGenericTreeItem
*item
)
349 : m_itemEdited(item
), m_startValue(item
->GetText())
352 m_aboutToFinish
= false;
354 int w
= m_itemEdited
->GetWidth(),
355 h
= m_itemEdited
->GetHeight();
358 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
363 int image
= item
->GetCurrentImage();
364 if ( image
!= NO_IMAGE
)
366 if ( m_owner
->m_imageListNormal
)
368 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
369 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
373 wxFAIL_MSG(_T("you must create an image list to use images!"));
377 // FIXME: what are all these hardcoded 4, 8 and 11s really?
381 wxSize bs
= DoGetBestSize() ;
382 // edit control height
385 int diff
= h
- ( bs
.y
- 8 ) ;
391 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
392 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
395 void wxTreeTextCtrl::EndEdit(bool discardChanges
)
397 m_aboutToFinish
= true;
399 if ( discardChanges
)
401 m_owner
->OnRenameCancelled(m_itemEdited
);
407 // Notify the owner about the changes
410 // Even if vetoed, close the control (consistent with MSW)
415 bool wxTreeTextCtrl::AcceptChanges()
417 const wxString value
= GetValue();
419 if ( value
== m_startValue
)
421 // nothing changed, always accept
422 // when an item remains unchanged, the owner
423 // needs to be notified that the user decided
424 // not to change the tree item label, and that
425 // the edit has been cancelled
427 m_owner
->OnRenameCancelled(m_itemEdited
);
431 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
433 // vetoed by the user
437 // accepted, do rename the item
438 m_owner
->SetItemText(m_itemEdited
, value
);
443 void wxTreeTextCtrl::Finish( bool setfocus
)
445 m_owner
->ResetTextControl();
447 wxPendingDelete
.Append(this);
453 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
455 switch ( event
.m_keyCode
)
470 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
472 if ( !m_aboutToFinish
)
474 // auto-grow the textctrl:
475 wxSize parentSize
= m_owner
->GetSize();
476 wxPoint myPos
= GetPosition();
477 wxSize mySize
= GetSize();
479 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
480 if (myPos
.x
+ sx
> parentSize
.x
)
481 sx
= parentSize
.x
- myPos
.x
;
484 SetSize(sx
, wxDefaultCoord
);
490 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
492 if ( !m_aboutToFinish
)
494 if ( !AcceptChanges() )
495 m_owner
->OnRenameCancelled( m_itemEdited
);
500 // We should let the native text control handle focus, too.
504 // -----------------------------------------------------------------------------
506 // -----------------------------------------------------------------------------
508 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
509 const wxString
& text
,
510 int image
, int selImage
,
511 wxTreeItemData
*data
)
514 m_images
[wxTreeItemIcon_Normal
] = image
;
515 m_images
[wxTreeItemIcon_Selected
] = selImage
;
516 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
517 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
520 m_state
= wxTREE_ITEMSTATE_NONE
;
523 m_isCollapsed
= true;
524 m_hasHilight
= false;
530 m_attr
= (wxTreeItemAttr
*)NULL
;
533 // We don't know the height here yet.
538 wxGenericTreeItem::~wxGenericTreeItem()
542 if (m_ownsAttr
) delete m_attr
;
544 wxASSERT_MSG( m_children
.IsEmpty(),
545 "must call DeleteChildren() before deleting the item" );
548 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
550 size_t count
= m_children
.GetCount();
551 for ( size_t n
= 0; n
< count
; n
++ )
553 wxGenericTreeItem
*child
= m_children
[n
];
554 tree
->SendDeleteEvent(child
);
556 child
->DeleteChildren(tree
);
557 if ( child
== tree
->m_select_me
)
558 tree
->m_select_me
= NULL
;
565 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
567 size_t count
= m_children
.GetCount();
571 size_t total
= count
;
572 for (size_t n
= 0; n
< count
; ++n
)
574 total
+= m_children
[n
]->GetChildrenCount();
580 void wxGenericTreeItem::GetSize( int &x
, int &y
,
581 const wxGenericTreeCtrl
*theButton
)
583 int bottomY
=m_y
+theButton
->GetLineHeight(this);
584 if ( y
< bottomY
) y
= bottomY
;
585 int width
= m_x
+ m_width
;
586 if ( x
< width
) x
= width
;
590 size_t count
= m_children
.GetCount();
591 for ( size_t n
= 0; n
< count
; ++n
)
593 m_children
[n
]->GetSize( x
, y
, theButton
);
598 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
599 const wxGenericTreeCtrl
*theCtrl
,
603 // for a hidden root node, don't evaluate it, but do evaluate children
604 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
607 int h
= theCtrl
->GetLineHeight(this);
608 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
610 int y_mid
= m_y
+ h
/2;
611 if (point
.y
< y_mid
)
612 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
614 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
616 int xCross
= m_x
- theCtrl
->GetSpacing();
618 // according to the drawing code the triangels are drawn
619 // at -4 , -4 from the position up to +10/+10 max
620 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
621 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
622 HasPlus() && theCtrl
->HasButtons() )
624 // 5 is the size of the plus sign
625 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
626 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
627 HasPlus() && theCtrl
->HasButtons() )
630 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
634 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
639 // assuming every image (normal and selected) has the same size!
640 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
642 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
649 if ( (GetState() != wxTREE_ITEMSTATE_NONE
) &&
650 theCtrl
->m_imageListState
)
652 theCtrl
->m_imageListState
->GetSize(GetState(),
656 if ((state_w
!= -1) && (point
.x
<= m_x
+ state_w
+ 1))
657 flags
|= wxTREE_HITTEST_ONITEMSTATEICON
;
658 else if ((image_w
!= -1) &&
660 (state_w
!= -1 ? state_w
+
661 MARGIN_BETWEEN_STATE_AND_IMAGE
664 flags
|= wxTREE_HITTEST_ONITEMICON
;
666 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
672 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
673 if (point
.x
> m_x
+m_width
)
674 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
679 // if children are expanded, fall through to evaluate them
680 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
684 size_t count
= m_children
.GetCount();
685 for ( size_t n
= 0; n
< count
; n
++ )
687 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
695 return (wxGenericTreeItem
*) NULL
;
698 int wxGenericTreeItem::GetCurrentImage() const
700 int image
= NO_IMAGE
;
705 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
708 if ( image
== NO_IMAGE
)
710 // we usually fall back to the normal item, but try just the
711 // expanded one (and not selected) first in this case
712 image
= GetImage(wxTreeItemIcon_Expanded
);
718 image
= GetImage(wxTreeItemIcon_Selected
);
721 // maybe it doesn't have the specific image we want,
722 // try the default one instead
723 if ( image
== NO_IMAGE
) image
= GetImage();
728 // -----------------------------------------------------------------------------
729 // wxGenericTreeCtrl implementation
730 // -----------------------------------------------------------------------------
732 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
734 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
735 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
736 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
737 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
738 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
739 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
740 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
741 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
744 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
746 * wxTreeCtrl has to be a real class or we have problems with
747 * the run-time information.
750 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
753 // -----------------------------------------------------------------------------
754 // construction/destruction
755 // -----------------------------------------------------------------------------
757 void wxGenericTreeCtrl::Init()
762 m_select_me
= (wxGenericTreeItem
*) NULL
;
770 m_hilightBrush
= new wxBrush
772 wxSystemSettings::GetColour
774 wxSYS_COLOUR_HIGHLIGHT
779 m_hilightUnfocusedBrush
= new wxBrush
781 wxSystemSettings::GetColour
783 wxSYS_COLOUR_BTNSHADOW
788 m_imageListButtons
= NULL
;
789 m_ownsImageListButtons
= false;
792 m_isDragging
= false;
793 m_dropTarget
= m_oldSelection
= NULL
;
797 m_renameTimer
= NULL
;
801 m_dropEffectAboveItem
= false;
803 m_lastOnSame
= false;
805 #if defined( __WXMAC__ )
807 m_normalFont
.MacCreateFromThemeFont( kThemeViewsFont
) ;
809 m_normalFont
.MacCreateFromUIFont( kCTFontViewsFontType
) ;
812 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
814 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
815 m_normalFont
.GetFamily(),
816 m_normalFont
.GetStyle(),
818 m_normalFont
.GetUnderlined(),
819 m_normalFont
.GetFaceName(),
820 m_normalFont
.GetEncoding());
823 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
828 const wxValidator
& validator
,
829 const wxString
& name
)
833 wxGetOsVersion(&major
, &minor
);
836 style
|= wxTR_ROW_LINES
;
839 if ( !wxControl::Create( parent
, id
, pos
, size
,
840 style
|wxHSCROLL
|wxVSCROLL
,
845 // If the tree display has no buttons, but does have
846 // connecting lines, we can use a narrower layout.
847 // It may not be a good idea to force this...
848 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
854 wxVisualAttributes attr
= GetDefaultAttributes();
855 SetOwnForegroundColour( attr
.colFg
);
856 SetOwnBackgroundColour( attr
.colBg
);
858 SetOwnFont(attr
.font
);
860 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
861 // style because we apparently get performance problems when using dotted
862 // pen for drawing in some ports -- but under MSW it seems to work fine
864 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
866 m_dottedPen
= *wxGREY_PEN
;
869 SetInitialSize(size
);
874 wxGenericTreeCtrl::~wxGenericTreeCtrl()
876 delete m_hilightBrush
;
877 delete m_hilightUnfocusedBrush
;
881 delete m_renameTimer
;
884 if (m_ownsImageListButtons
)
885 delete m_imageListButtons
;
888 // -----------------------------------------------------------------------------
890 // -----------------------------------------------------------------------------
892 unsigned int wxGenericTreeCtrl::GetCount() const
900 unsigned int count
= m_anchor
->GetChildrenCount();
901 if ( !HasFlag(wxTR_HIDE_ROOT
) )
903 // take the root itself into account
910 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
912 m_indent
= (unsigned short) indent
;
917 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
918 bool recursively
) const
920 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
922 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
925 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
927 // Do not try to expand the root node if it hasn't been created yet
928 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
930 // if we will hide the root, make sure children are visible
931 m_anchor
->SetHasPlus();
933 CalculatePositions();
936 // right now, just sets the styles. Eventually, we may
937 // want to update the inherited styles, but right now
938 // none of the parents has updatable styles
939 m_windowStyle
= styles
;
943 // -----------------------------------------------------------------------------
944 // functions to work with tree items
945 // -----------------------------------------------------------------------------
947 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
949 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
951 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
954 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
955 wxTreeItemIcon which
) const
957 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
959 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
962 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
964 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
966 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
969 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
971 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
973 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
974 return pItem
->GetState();
977 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
979 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
981 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
982 return pItem
->Attr().GetTextColour();
986 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
988 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
990 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
991 return pItem
->Attr().GetBackgroundColour();
994 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
996 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
998 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
999 return pItem
->Attr().GetFont();
1003 wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1005 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1007 wxClientDC
dc(this);
1008 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1009 pItem
->SetText(text
);
1010 CalculateSize(pItem
, dc
);
1014 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1016 wxTreeItemIcon which
)
1018 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1020 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1021 pItem
->SetImage(image
, which
);
1023 wxClientDC
dc(this);
1024 CalculateSize(pItem
, dc
);
1029 wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1031 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1034 data
->SetId( item
);
1036 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1039 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1041 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1043 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1044 pItem
->SetState(state
);
1048 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1050 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1052 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1053 pItem
->SetHasPlus(has
);
1057 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1059 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1061 // avoid redrawing the tree if no real change
1062 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1063 if ( pItem
->IsBold() != bold
)
1065 pItem
->SetBold(bold
);
1067 // recalculate the item size as bold and non bold fonts have different
1069 wxClientDC
dc(this);
1070 CalculateSize(pItem
, dc
);
1076 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1079 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1085 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1086 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1089 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1090 pItem
->Attr().SetTextColour(fg
);
1091 pItem
->Attr().SetBackgroundColour(bg
);
1095 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1096 const wxColour
& col
)
1098 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1100 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1101 pItem
->Attr().SetTextColour(col
);
1105 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1106 const wxColour
& col
)
1108 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1110 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1111 pItem
->Attr().SetBackgroundColour(col
);
1116 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1118 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1120 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1121 pItem
->Attr().SetFont(font
);
1125 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1127 wxTreeCtrlBase::SetFont(font
);
1129 m_normalFont
= font
;
1130 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1131 m_normalFont
.GetFamily(),
1132 m_normalFont
.GetStyle(),
1134 m_normalFont
.GetUnderlined(),
1135 m_normalFont
.GetFaceName(),
1136 m_normalFont
.GetEncoding());
1142 // -----------------------------------------------------------------------------
1143 // item status inquiries
1144 // -----------------------------------------------------------------------------
1146 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1148 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1150 // An item is only visible if it's not a descendant of a collapsed item
1151 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1152 wxGenericTreeItem
* parent
= pItem
->GetParent();
1155 if (!parent
->IsExpanded())
1157 parent
= parent
->GetParent();
1161 GetViewStart(& startX
, & startY
);
1163 wxSize clientSize
= GetClientSize();
1166 if (!GetBoundingRect(item
, rect
))
1168 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1170 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1172 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1178 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1180 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1182 // consider that the item does have children if it has the "+" button: it
1183 // might not have them (if it had never been expanded yet) but then it
1184 // could have them as well and it's better to err on this side rather than
1185 // disabling some operations which are restricted to the items with
1186 // children for an item which does have them
1187 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1190 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1192 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1194 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1197 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1199 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1201 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1204 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1206 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1208 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1211 // -----------------------------------------------------------------------------
1213 // -----------------------------------------------------------------------------
1215 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1217 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1219 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1222 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1223 wxTreeItemIdValue
& cookie
) const
1225 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1228 return GetNextChild(item
, cookie
);
1231 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1232 wxTreeItemIdValue
& cookie
) const
1234 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1236 wxArrayGenericTreeItems
&
1237 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1239 // it's ok to cast cookie to size_t, we never have indices big enough to
1240 // overflow "void *"
1241 size_t *pIndex
= (size_t *)&cookie
;
1242 if ( *pIndex
< children
.GetCount() )
1244 return children
.Item((*pIndex
)++);
1248 // there are no more of them
1249 return wxTreeItemId();
1253 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1255 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1257 wxArrayGenericTreeItems
&
1258 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1259 return children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last());
1262 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1264 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1266 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1267 wxGenericTreeItem
*parent
= i
->GetParent();
1268 if ( parent
== NULL
)
1270 // root item doesn't have any siblings
1271 return wxTreeItemId();
1274 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1275 int index
= siblings
.Index(i
);
1276 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1278 size_t n
= (size_t)(index
+ 1);
1279 return n
== siblings
.GetCount() ? wxTreeItemId()
1280 : wxTreeItemId(siblings
[n
]);
1283 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1285 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1287 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1288 wxGenericTreeItem
*parent
= i
->GetParent();
1289 if ( parent
== NULL
)
1291 // root item doesn't have any siblings
1292 return wxTreeItemId();
1295 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1296 int index
= siblings
.Index(i
);
1297 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1299 return index
== 0 ? wxTreeItemId()
1300 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1303 // Only for internal use right now, but should probably be public
1304 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1306 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1308 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1310 // First see if there are any children.
1311 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1312 if (children
.GetCount() > 0)
1314 return children
.Item(0);
1318 // Try a sibling of this or ancestor instead
1319 wxTreeItemId p
= item
;
1320 wxTreeItemId toFind
;
1323 toFind
= GetNextSibling(p
);
1324 p
= GetItemParent(p
);
1325 } while (p
.IsOk() && !toFind
.IsOk());
1330 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1332 wxTreeItemId id
= GetRootItem();
1341 } while (id
.IsOk());
1343 return wxTreeItemId();
1346 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1348 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1349 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1351 wxTreeItemId id
= item
;
1354 while (id
= GetNext(id
), id
.IsOk())
1360 return wxTreeItemId();
1363 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1365 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1366 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1368 // find out the starting point
1369 wxTreeItemId prevItem
= GetPrevSibling(item
);
1370 if ( !prevItem
.IsOk() )
1372 prevItem
= GetItemParent(item
);
1375 // find the first visible item after it
1376 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1378 prevItem
= GetNext(prevItem
);
1379 if ( !prevItem
.IsOk() || prevItem
== item
)
1381 // there are no visible items before item
1382 return wxTreeItemId();
1386 // from there we must be able to navigate until this item
1387 while ( prevItem
.IsOk() )
1389 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1390 if ( !nextItem
.IsOk() || nextItem
== item
)
1393 prevItem
= nextItem
;
1399 // called by wxTextTreeCtrl when it marks itself for deletion
1400 void wxGenericTreeCtrl::ResetTextControl()
1405 // find the first item starting with the given prefix after the given item
1406 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1407 const wxString
& prefixOrig
) const
1409 // match is case insensitive as this is more convenient to the user: having
1410 // to press Shift-letter to go to the item starting with a capital letter
1411 // would be too bothersome
1412 wxString prefix
= prefixOrig
.Lower();
1414 // determine the starting point: we shouldn't take the current item (this
1415 // allows to switch between two items starting with the same letter just by
1416 // pressing it) but we shouldn't jump to the next one if the user is
1417 // continuing to type as otherwise he might easily skip the item he wanted
1418 wxTreeItemId id
= idParent
;
1419 if ( prefix
.length() == 1 )
1424 // look for the item starting with the given prefix after it
1425 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1430 // if we haven't found anything...
1433 // ... wrap to the beginning
1435 if ( HasFlag(wxTR_HIDE_ROOT
) )
1437 // can't select virtual root
1441 // and try all the items (stop when we get to the one we started from)
1442 while ( id
.IsOk() && id
!= idParent
&&
1443 !GetItemText(id
).Lower().StartsWith(prefix
) )
1447 // If we haven't found the item, id.IsOk() will be false, as per
1454 // -----------------------------------------------------------------------------
1456 // -----------------------------------------------------------------------------
1458 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1460 const wxString
& text
,
1463 wxTreeItemData
*data
)
1465 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1468 // should we give a warning here?
1469 return AddRoot(text
, image
, selImage
, data
);
1472 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1474 wxGenericTreeItem
*item
=
1475 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1479 data
->m_pItem
= item
;
1482 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1485 InvalidateBestSize();
1489 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1492 wxTreeItemData
*data
)
1494 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
1496 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1498 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1499 image
, selImage
, data
);
1502 data
->m_pItem
= m_anchor
;
1505 if (HasFlag(wxTR_HIDE_ROOT
))
1507 // if root is hidden, make sure we can navigate
1509 m_anchor
->SetHasPlus();
1511 CalculatePositions();
1514 if (!HasFlag(wxTR_MULTIPLE
))
1516 m_current
= m_key_current
= m_anchor
;
1517 m_current
->SetHilight( true );
1520 InvalidateBestSize();
1524 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1525 const wxTreeItemId
& idPrevious
,
1526 const wxString
& text
,
1527 int image
, int selImage
,
1528 wxTreeItemData
*data
)
1530 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1533 // should we give a warning here?
1534 return AddRoot(text
, image
, selImage
, data
);
1538 if (idPrevious
.IsOk())
1540 index
= parent
->GetChildren().Index(
1541 (wxGenericTreeItem
*) idPrevious
.m_pItem
);
1542 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1543 "previous item in wxGenericTreeCtrl::InsertItem() "
1544 "is not a sibling" );
1547 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1551 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1553 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1554 GetEventHandler()->ProcessEvent( event
);
1557 // Don't leave edit or selection on a child which is about to disappear
1558 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1560 if ( m_textCtrl
&& item
!= m_textCtrl
->item() &&
1561 IsDescendantOf(item
, m_textCtrl
->item()) )
1563 m_textCtrl
->EndEdit( true );
1566 if ( item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
) )
1568 m_key_current
= NULL
;
1571 if ( IsDescendantOf(item
, m_select_me
) )
1576 if ( item
!= m_current
&& IsDescendantOf(item
, m_current
) )
1578 m_current
->SetHilight( false );
1584 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1586 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1588 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1589 ChildrenClosing(item
);
1590 item
->DeleteChildren(this);
1591 InvalidateBestSize();
1594 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1596 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1598 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1600 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1602 // can't delete the item being edited, cancel editing it first
1603 m_textCtrl
->EndEdit( true );
1606 wxGenericTreeItem
*parent
= item
->GetParent();
1608 // don't keep stale pointers around!
1609 if ( IsDescendantOf(item
, m_key_current
) )
1611 // Don't silently change the selection:
1612 // do it properly in idle time, so event
1613 // handlers get called.
1615 // m_key_current = parent;
1616 m_key_current
= NULL
;
1619 // m_select_me records whether we need to select
1620 // a different item, in idle time.
1621 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1623 m_select_me
= parent
;
1626 if ( IsDescendantOf(item
, m_current
) )
1628 // Don't silently change the selection:
1629 // do it properly in idle time, so event
1630 // handlers get called.
1632 // m_current = parent;
1634 m_select_me
= parent
;
1637 // remove the item from the tree
1640 parent
->GetChildren().Remove( item
); // remove by value
1642 else // deleting the root
1644 // nothing will be left in the tree
1648 // and delete all of its children and the item itself now
1649 item
->DeleteChildren(this);
1650 SendDeleteEvent(item
);
1652 if (item
== m_select_me
)
1657 InvalidateBestSize();
1660 void wxGenericTreeCtrl::DeleteAllItems()
1668 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1670 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1672 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1673 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1674 _T("can't expand hidden root") );
1676 if ( !item
->HasPlus() )
1679 if ( item
->IsExpanded() )
1682 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1684 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1686 // cancelled by program
1693 CalculatePositions();
1695 RefreshSubtree(item
);
1702 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1703 GetEventHandler()->ProcessEvent( event
);
1706 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1708 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1709 _T("can't collapse hidden root") );
1711 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1713 if ( !item
->IsExpanded() )
1716 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1717 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1719 // cancelled by program
1723 ChildrenClosing(item
);
1726 #if 0 // TODO why should items be collapsed recursively?
1727 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1728 size_t count
= children
.GetCount();
1729 for ( size_t n
= 0; n
< count
; n
++ )
1731 Collapse(children
[n
]);
1735 CalculatePositions();
1737 RefreshSubtree(item
);
1739 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1740 GetEventHandler()->ProcessEvent( event
);
1743 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1746 DeleteChildren(item
);
1749 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1751 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1753 if (item
->IsExpanded())
1759 void wxGenericTreeCtrl::Unselect()
1763 m_current
->SetHilight( false );
1764 RefreshLine( m_current
);
1771 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1773 if (item
->IsSelected())
1775 item
->SetHilight(false);
1779 if (item
->HasChildren())
1781 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1782 size_t count
= children
.GetCount();
1783 for ( size_t n
= 0; n
< count
; ++n
)
1785 UnselectAllChildren(children
[n
]);
1790 void wxGenericTreeCtrl::UnselectAll()
1792 wxTreeItemId rootItem
= GetRootItem();
1794 // the tree might not have the root item at all
1797 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1801 // Recursive function !
1802 // To stop we must have crt_item<last_item
1804 // Tag all next children, when no more children,
1805 // Move to parent (not to tag)
1806 // Keep going... if we found last_item, we stop.
1808 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
,
1809 wxGenericTreeItem
*last_item
,
1812 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1814 if (parent
== NULL
) // This is root item
1815 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1817 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1818 int index
= children
.Index(crt_item
);
1819 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1821 size_t count
= children
.GetCount();
1822 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1824 if ( TagAllChildrenUntilLast(children
[n
], last_item
, select
) )
1828 return TagNextChildren(parent
, last_item
, select
);
1832 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
,
1833 wxGenericTreeItem
*last_item
,
1836 crt_item
->SetHilight(select
);
1837 RefreshLine(crt_item
);
1839 if (crt_item
==last_item
)
1842 if (crt_item
->HasChildren())
1844 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
1845 size_t count
= children
.GetCount();
1846 for ( size_t n
= 0; n
< count
; ++n
)
1848 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
1857 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
,
1858 wxGenericTreeItem
*item2
)
1862 // item2 is not necessary after item1
1863 // choice first' and 'last' between item1 and item2
1864 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
1865 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
1867 bool select
= m_current
->IsSelected();
1869 if ( TagAllChildrenUntilLast(first
,last
,select
) )
1872 TagNextChildren(first
,last
,select
);
1875 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
1876 bool unselect_others
,
1877 bool extended_select
)
1879 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
1883 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
1884 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1886 //wxCHECK_RET( ( (!unselect_others) && is_single),
1887 // wxT("this is a single selection tree") );
1889 // to keep going anyhow !!!
1892 if (item
->IsSelected())
1893 return; // nothing to do
1894 unselect_others
= true;
1895 extended_select
= false;
1897 else if ( unselect_others
&& item
->IsSelected() )
1899 // selection change if there is more than one item currently selected
1900 wxArrayTreeItemIds selected_items
;
1901 if ( GetSelections(selected_items
) == 1 )
1905 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1906 event
.m_itemOld
= m_current
;
1907 // TODO : Here we don't send any selection mode yet !
1909 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1912 wxTreeItemId parent
= GetItemParent( itemId
);
1913 while (parent
.IsOk())
1915 if (!IsExpanded(parent
))
1918 parent
= GetItemParent( parent
);
1922 if (unselect_others
)
1924 if (is_single
) Unselect(); // to speed up thing
1929 if (extended_select
)
1934 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
1937 // don't change the mark (m_current)
1938 SelectItemRange(m_current
, item
);
1942 bool select
= true; // the default
1944 // Check if we need to toggle hilight (ctrl mode)
1945 if (!unselect_others
)
1946 select
=!item
->IsSelected();
1948 m_current
= m_key_current
= item
;
1949 m_current
->SetHilight(select
);
1950 RefreshLine( m_current
);
1953 // This can cause idle processing to select the root
1954 // if no item is selected, so it must be after the
1956 EnsureVisible( itemId
);
1958 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1959 GetEventHandler()->ProcessEvent( event
);
1962 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
1966 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
1970 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1971 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
1973 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1974 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1977 item
->SetHilight(false);
1980 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1981 GetEventHandler()->ProcessEvent( event
);
1985 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
1986 wxArrayTreeItemIds
&array
) const
1988 if ( item
->IsSelected() )
1989 array
.Add(wxTreeItemId(item
));
1991 if ( item
->HasChildren() )
1993 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1994 size_t count
= children
.GetCount();
1995 for ( size_t n
= 0; n
< count
; ++n
)
1996 FillArray(children
[n
], array
);
2000 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
2003 wxTreeItemId idRoot
= GetRootItem();
2004 if ( idRoot
.IsOk() )
2006 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
2008 //else: the tree is empty, so no selections
2010 return array
.GetCount();
2013 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2015 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2017 if (!item
.IsOk()) return;
2019 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2021 // first expand all parent branches
2022 wxGenericTreeItem
*parent
= gitem
->GetParent();
2024 if ( HasFlag(wxTR_HIDE_ROOT
) )
2026 while ( parent
&& parent
!= m_anchor
)
2029 parent
= parent
->GetParent();
2037 parent
= parent
->GetParent();
2041 //if (parent) CalculatePositions();
2046 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2048 if (!item
.IsOk()) return;
2050 // We have to call this here because the label in
2051 // question might just have been added and no screen
2052 // update taken place.
2054 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2057 DoDirtyProcessing();
2059 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2061 // now scroll to the item
2062 int item_y
= gitem
->GetY();
2066 GetViewStart( &start_x
, &start_y
);
2067 start_y
*= PIXELS_PER_UNIT
;
2071 GetClientSize( &client_w
, &client_h
);
2073 if (item_y
< start_y
+3)
2078 m_anchor
->GetSize( x
, y
, this );
2079 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2080 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2081 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2082 // Item should appear at top
2083 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2084 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2085 x_pos
, item_y
/PIXELS_PER_UNIT
);
2087 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2092 m_anchor
->GetSize( x
, y
, this );
2093 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2094 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2095 item_y
+= PIXELS_PER_UNIT
+2;
2096 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2097 // Item should appear at bottom
2098 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2099 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2101 (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
2105 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2106 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2108 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2109 wxGenericTreeItem
**item2
)
2111 wxCHECK_MSG( s_treeBeingSorted
, 0,
2112 "bug in wxGenericTreeCtrl::SortChildren()" );
2114 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2117 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2119 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2121 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2123 wxCHECK_RET( !s_treeBeingSorted
,
2124 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2126 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2127 if ( children
.GetCount() > 1 )
2131 s_treeBeingSorted
= this;
2132 children
.Sort(tree_ctrl_compare_func
);
2133 s_treeBeingSorted
= NULL
;
2135 //else: don't make the tree dirty as nothing changed
2138 void wxGenericTreeCtrl::CalculateLineHeight()
2140 wxClientDC
dc(this);
2141 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2143 if ( m_imageListNormal
)
2145 // Calculate a m_lineHeight value from the normal Image sizes.
2146 // May be toggle off. Then wxGenericTreeCtrl will spread when
2147 // necessary (which might look ugly).
2148 int n
= m_imageListNormal
->GetImageCount();
2149 for (int i
= 0; i
< n
; i
++)
2151 int width
= 0, height
= 0;
2152 m_imageListNormal
->GetSize(i
, width
, height
);
2153 if (height
> m_lineHeight
) m_lineHeight
= height
;
2157 if ( m_imageListState
)
2159 // Calculate a m_lineHeight value from the state Image sizes.
2160 // May be toggle off. Then wxGenericTreeCtrl will spread when
2161 // necessary (which might look ugly).
2162 int n
= m_imageListState
->GetImageCount();
2163 for (int i
= 0; i
< n
; i
++)
2165 int width
= 0, height
= 0;
2166 m_imageListState
->GetSize(i
, width
, height
);
2167 if (height
> m_lineHeight
) m_lineHeight
= height
;
2171 if (m_imageListButtons
)
2173 // Calculate a m_lineHeight value from the Button image sizes.
2174 // May be toggle off. Then wxGenericTreeCtrl will spread when
2175 // necessary (which might look ugly).
2176 int n
= m_imageListButtons
->GetImageCount();
2177 for (int i
= 0; i
< n
; i
++)
2179 int width
= 0, height
= 0;
2180 m_imageListButtons
->GetSize(i
, width
, height
);
2181 if (height
> m_lineHeight
) m_lineHeight
= height
;
2185 if (m_lineHeight
< 30)
2186 m_lineHeight
+= 2; // at least 2 pixels
2188 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2191 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2193 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2194 m_imageListNormal
= imageList
;
2195 m_ownsImageListNormal
= false;
2197 // Don't do any drawing if we're setting the list to NULL,
2198 // since we may be in the process of deleting the tree control.
2200 CalculateLineHeight();
2203 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2205 if (m_ownsImageListState
) delete m_imageListState
;
2206 m_imageListState
= imageList
;
2207 m_ownsImageListState
= false;
2209 // Don't do any drawing if we're setting the list to NULL,
2210 // since we may be in the process of deleting the tree control.
2212 CalculateLineHeight();
2215 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2217 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2218 m_imageListButtons
= imageList
;
2219 m_ownsImageListButtons
= false;
2221 CalculateLineHeight();
2224 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2226 SetButtonsImageList(imageList
);
2227 m_ownsImageListButtons
= true;
2230 // -----------------------------------------------------------------------------
2232 // -----------------------------------------------------------------------------
2234 void wxGenericTreeCtrl::AdjustMyScrollbars()
2239 m_anchor
->GetSize( x
, y
, this );
2240 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2241 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2242 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2243 int y_pos
= GetScrollPos( wxVERTICAL
);
2244 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2245 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2250 SetScrollbars( 0, 0, 0, 0 );
2254 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2256 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2257 return item
->GetHeight();
2259 return m_lineHeight
;
2262 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2264 wxTreeItemAttr
*attr
= item
->GetAttributes();
2265 if ( attr
&& attr
->HasFont() )
2266 dc
.SetFont(attr
->GetFont());
2267 else if (item
->IsBold())
2268 dc
.SetFont(m_boldFont
);
2270 wxCoord text_w
= 0, text_h
= 0;
2271 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2273 int image_h
= 0, image_w
= 0;
2274 int image
= item
->GetCurrentImage();
2275 if ( image
!= NO_IMAGE
)
2277 if ( m_imageListNormal
)
2279 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2280 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2288 int state_h
= 0, state_w
= 0;
2289 int state
= item
->GetState();
2290 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2292 if ( m_imageListState
)
2294 m_imageListState
->GetSize( state
, state_w
, state_h
);
2295 if ( image
!= NO_IMAGE
)
2296 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2298 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2302 state
= wxTREE_ITEMSTATE_NONE
;
2306 int total_h
= GetLineHeight(item
);
2307 bool drawItemBackground
= false;
2309 if ( item
->IsSelected() )
2311 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2312 drawItemBackground
= true;
2317 if ( attr
&& attr
->HasBackgroundColour() )
2319 drawItemBackground
= true;
2320 colBg
= attr
->GetBackgroundColour();
2324 colBg
= GetBackgroundColour();
2326 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2329 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2331 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2335 GetVirtualSize(&w
, &h
);
2336 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2337 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2338 dc
.DrawRectangle(rect
);
2340 if (!item
->IsSelected())
2342 dc
.DrawRectangle(rect
);
2346 int flags
= wxCONTROL_SELECTED
;
2348 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2349 && IsControlActive( (ControlRef
)GetHandle() )
2352 flags
|= wxCONTROL_FOCUSED
;
2353 if ((item
== m_current
) && (m_hasFocus
))
2354 flags
|= wxCONTROL_CURRENT
;
2356 wxRendererNative::Get().
2357 DrawItemSelectionRect(this, dc
, rect
, flags
);
2363 if ( item
->IsSelected() &&
2364 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2366 // If it's selected, and there's an state image or normal image,
2367 // then we should take care to leave the area under the image
2368 // painted in the background colour.
2369 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2370 item
->GetY() + offset
,
2371 item
->GetWidth() - state_w
- image_w
+ 2,
2373 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2374 dc
.DrawRectangle( rect
);
2379 int flags
= wxCONTROL_SELECTED
;
2381 flags
|= wxCONTROL_FOCUSED
;
2382 if ((item
== m_current
) && (m_hasFocus
))
2383 flags
|= wxCONTROL_CURRENT
;
2384 wxRendererNative::Get().
2385 DrawItemSelectionRect(this, dc
, rect
, flags
);
2388 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2389 // don't allow backgrounds to be customized. Not drawing the background,
2390 // except for custom item backgrounds, works for both kinds of theme.
2391 else if (drawItemBackground
)
2393 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2394 item
->GetWidth()+2, total_h
-offset
);
2395 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2396 dc
.DrawRectangle( rect
);
2398 if ( attr
&& attr
->HasBackgroundColour() )
2400 dc
.DrawRectangle( rect
);
2407 int flags
= wxCONTROL_SELECTED
;
2409 flags
|= wxCONTROL_FOCUSED
;
2410 if ((item
== m_current
) && (m_hasFocus
))
2411 flags
|= wxCONTROL_CURRENT
;
2412 wxRendererNative::Get().
2413 DrawItemSelectionRect(this, dc
, rect
, flags
);
2419 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2421 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2422 m_imageListState
->Draw( state
, dc
,
2425 (total_h
> state_h
? (total_h
-state_h
)/2
2427 wxIMAGELIST_DRAW_TRANSPARENT
);
2428 dc
.DestroyClippingRegion();
2431 if ( image
!= NO_IMAGE
)
2433 dc
.SetClippingRegion(item
->GetX() + state_w
, item
->GetY(),
2435 m_imageListNormal
->Draw( image
, dc
,
2436 item
->GetX() + state_w
,
2438 (total_h
> image_h
? (total_h
-image_h
)/2
2440 wxIMAGELIST_DRAW_TRANSPARENT
);
2441 dc
.DestroyClippingRegion();
2444 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2445 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2446 dc
.DrawText( item
->GetText(),
2447 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2448 (wxCoord
)(item
->GetY() + extraH
));
2450 // restore normal font
2451 dc
.SetFont( m_normalFont
);
2455 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem
*item
,
2460 int x
= level
*m_indent
;
2461 if (!HasFlag(wxTR_HIDE_ROOT
))
2465 else if (level
== 0)
2467 // always expand hidden root
2469 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2470 int count
= children
.GetCount();
2476 PaintLevel(children
[n
], dc
, 1, y
);
2477 } while (++n
< count
);
2479 if ( !HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
)
2482 // draw line down to last child
2483 origY
+= GetLineHeight(children
[0])>>1;
2484 oldY
+= GetLineHeight(children
[n
-1])>>1;
2485 dc
.DrawLine(3, origY
, 3, oldY
);
2491 item
->SetX(x
+m_spacing
);
2494 int h
= GetLineHeight(item
);
2496 int y_mid
= y_top
+ (h
>>1);
2499 int exposed_x
= dc
.LogicalToDeviceX(0);
2500 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2502 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2506 // don't draw rect outline if we already have the
2507 // background color under Mac
2508 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2509 #endif // !__WXMAC__
2513 if ( item
->IsSelected()
2514 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2515 // On wxMac, if the tree doesn't have the focus we draw an empty
2516 // rectangle, so we want to make sure that the text is visible
2517 // against the normal background, not the highlightbackground, so
2518 // don't use the highlight text colour unless we have the focus.
2519 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2526 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2531 wxTreeItemAttr
*attr
= item
->GetAttributes();
2532 if (attr
&& attr
->HasTextColour())
2533 colText
= attr
->GetTextColour();
2535 colText
= GetForegroundColour();
2539 dc
.SetTextForeground(colText
);
2543 PaintItem(item
, dc
);
2545 if (HasFlag(wxTR_ROW_LINES
))
2547 // if the background colour is white, choose a
2548 // contrasting color for the lines
2549 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2550 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2551 dc
.DrawLine(0, y_top
, 10000, y_top
);
2552 dc
.DrawLine(0, y
, 10000, y
);
2555 // restore DC objects
2556 dc
.SetBrush(*wxWHITE_BRUSH
);
2557 dc
.SetPen(m_dottedPen
);
2558 dc
.SetTextForeground(*wxBLACK
);
2560 if ( !HasFlag(wxTR_NO_LINES
) )
2562 // draw the horizontal line here
2564 if (x
> (signed)m_indent
)
2565 x_start
-= m_indent
;
2566 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2568 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2571 // should the item show a button?
2572 if ( item
->HasPlus() && HasButtons() )
2574 if ( m_imageListButtons
)
2576 // draw the image button here
2579 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2580 : wxTreeItemIcon_Normal
;
2581 if ( item
->IsSelected() )
2582 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2584 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2585 int xx
= x
- image_w
/2;
2586 int yy
= y_mid
- image_h
/2;
2588 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2589 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2590 wxIMAGELIST_DRAW_TRANSPARENT
);
2592 else // no custom buttons
2594 static const int wImage
= 9;
2595 static const int hImage
= 9;
2598 if (item
->IsExpanded())
2599 flag
|= wxCONTROL_EXPANDED
;
2600 if (item
== m_underMouse
)
2601 flag
|= wxCONTROL_CURRENT
;
2603 wxRendererNative::Get().DrawTreeItemButton
2607 wxRect(x
- wImage
/2,
2616 if (item
->IsExpanded())
2618 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2619 int count
= children
.GetCount();
2626 PaintLevel(children
[n
], dc
, level
, y
);
2627 } while (++n
< count
);
2629 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2631 // draw line down to last child
2632 oldY
+= GetLineHeight(children
[n
-1])>>1;
2633 if (HasButtons()) y_mid
+= 5;
2635 // Only draw the portion of the line that is visible, in case
2637 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2638 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2639 yOrigin
= abs(yOrigin
);
2640 GetClientSize(&width
, &height
);
2642 // Move end points to the begining/end of the view?
2643 if (y_mid
< yOrigin
)
2645 if (oldY
> yOrigin
+ height
)
2646 oldY
= yOrigin
+ height
;
2648 // after the adjustments if y_mid is larger than oldY then the
2649 // line isn't visible at all so don't draw anything
2651 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2657 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2661 if ( item
->HasPlus() )
2663 // it's a folder, indicate it by a border
2668 // draw a line under the drop target because the item will be
2670 DrawLine(item
, !m_dropEffectAboveItem
);
2673 SetCursor(wxCURSOR_BULLSEYE
);
2678 SetCursor(wxCURSOR_NO_ENTRY
);
2682 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2684 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2686 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2688 wxClientDC
dc(this);
2690 dc
.SetLogicalFunction(wxINVERT
);
2691 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2693 int w
= i
->GetWidth() + 2;
2694 int h
= GetLineHeight(i
) + 2;
2696 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2699 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2701 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2703 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2705 wxClientDC
dc(this);
2707 dc
.SetLogicalFunction(wxINVERT
);
2713 y
+= GetLineHeight(i
) - 1;
2716 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2719 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem
*item
, wxDC
&dc
)
2724 wxTreeItemAttr
*attr
= item
->GetAttributes();
2725 if ( attr
&& attr
->HasFont() )
2726 dc
.SetFont(attr
->GetFont());
2727 else if ( item
->IsBold() )
2728 dc
.SetFont(m_boldFont
);
2730 dc
.SetFont(m_normalFont
);
2732 dc
.GetTextExtent( item
->GetText(), &text_w
, &text_h
);
2735 // restore normal font
2736 dc
.SetFont( m_normalFont
);
2740 int image
= item
->GetCurrentImage();
2741 if ( image
!= NO_IMAGE
)
2743 if ( m_imageListNormal
)
2745 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2746 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2750 int state_h
= 0, state_w
= 0;
2751 int state
= item
->GetState();
2752 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2754 if ( m_imageListState
)
2756 m_imageListState
->GetSize( state
, state_w
, state_h
);
2757 if ( image
!= NO_IMAGE
)
2758 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2760 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2764 state
= wxTREE_ITEMSTATE_NONE
;
2768 int total_h
= (image_h
> text_h
) ? image_h
: text_h
;
2771 total_h
+= 2; // at least 2 pixels
2773 total_h
+= total_h
/10; // otherwise 10% extra spacing
2775 item
->SetHeight(total_h
);
2776 if (total_h
>m_lineHeight
)
2777 m_lineHeight
=total_h
;
2779 item
->SetWidth(state_w
+ image_w
+ text_w
+ 2);
2782 // -----------------------------------------------------------------------------
2783 // wxWidgets callbacks
2784 // -----------------------------------------------------------------------------
2786 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2789 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2790 RefreshLine( m_current
);
2796 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2804 dc
.SetFont( m_normalFont
);
2805 dc
.SetPen( m_dottedPen
);
2807 // this is now done dynamically
2808 //if(GetImageList() == NULL)
2809 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2812 PaintLevel( m_anchor
, dc
, 0, y
);
2815 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2824 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2833 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2835 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2836 te
.m_evtKey
= event
;
2837 if ( GetEventHandler()->ProcessEvent( te
) )
2839 // intercepted by the user code
2843 if ( (m_current
== 0) || (m_key_current
== 0) )
2849 // how should the selection work for this event?
2850 bool is_multiple
, extended_select
, unselect_others
;
2851 EventFlagsToSelType(GetWindowStyleFlag(),
2854 is_multiple
, extended_select
, unselect_others
);
2856 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2858 if (event
.GetKeyCode() == WXK_RIGHT
)
2859 event
.m_keyCode
= WXK_LEFT
;
2860 else if (event
.GetKeyCode() == WXK_LEFT
)
2861 event
.m_keyCode
= WXK_RIGHT
;
2866 // * : Expand all/Collapse all
2867 // ' ' | return : activate
2868 // up : go up (not last children!)
2870 // left : go to parent
2871 // right : open if parent and go next
2872 // home : go to root
2873 // end : go to last item without opening parents
2874 // alnum : start or continue searching for the item with this prefix
2875 int keyCode
= event
.GetKeyCode();
2880 if (m_current
->HasPlus() && !IsExpanded(m_current
))
2888 if ( !IsExpanded(m_current
) )
2891 ExpandAllChildren(m_current
);
2894 //else: fall through to Collapse() it
2898 if (IsExpanded(m_current
))
2900 Collapse(m_current
);
2906 // Use the item's bounding rectangle to determine position for
2909 GetBoundingRect(m_current
, ItemRect
, true);
2912 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
2913 // Use the left edge, vertical middle
2914 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
2916 ItemRect
.GetHeight() / 2);
2917 GetEventHandler()->ProcessEvent( eventMenu
);
2923 if ( !event
.HasModifiers() )
2926 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
2927 GetEventHandler()->ProcessEvent( eventAct
);
2930 // in any case, also generate the normal key event for this key,
2931 // even if we generated the ACTIVATED event above: this is what
2932 // wxMSW does and it makes sense because you might not want to
2933 // process ACTIVATED event at all and handle Space and Return
2934 // directly (and differently) which would be impossible otherwise
2938 // up goes to the previous sibling or to the last
2939 // of its children if it's expanded
2942 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
2945 prev
= GetItemParent( m_key_current
);
2946 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2948 break; // don't go to root if it is hidden
2952 wxTreeItemIdValue cookie
;
2953 wxTreeItemId current
= m_key_current
;
2954 // TODO: Huh? If we get here, we'd better be the first
2955 // child of our parent. How else could it be?
2956 if (current
== GetFirstChild( prev
, cookie
))
2958 // otherwise we return to where we came from
2962 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
2969 while ( IsExpanded(prev
) && HasChildren(prev
) )
2971 wxTreeItemId child
= GetLastChild(prev
);
2978 DoSelectItem( prev
, unselect_others
, extended_select
);
2979 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
2984 // left arrow goes to the parent
2987 wxTreeItemId prev
= GetItemParent( m_current
);
2988 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
2990 // don't go to root if it is hidden
2991 prev
= GetPrevSibling( m_current
);
2995 DoSelectItem( prev
, unselect_others
, extended_select
);
3001 // this works the same as the down arrow except that we
3002 // also expand the item if it wasn't expanded yet
3003 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
3005 //else: don't try to expand hidden root item (which can be the
3006 // current one when the tree is empty)
3012 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
3014 wxTreeItemIdValue cookie
;
3015 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
3019 DoSelectItem( child
, unselect_others
, extended_select
);
3020 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
3024 wxTreeItemId next
= GetNextSibling( m_key_current
);
3027 wxTreeItemId current
= m_key_current
;
3028 while (current
.IsOk() && !next
)
3030 current
= GetItemParent( current
);
3031 if (current
) next
= GetNextSibling( current
);
3036 DoSelectItem( next
, unselect_others
, extended_select
);
3037 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
3043 // <End> selects the last visible tree item
3046 wxTreeItemId last
= GetRootItem();
3048 while ( last
.IsOk() && IsExpanded(last
) )
3050 wxTreeItemId lastChild
= GetLastChild(last
);
3052 // it may happen if the item was expanded but then all of
3053 // its children have been deleted - so IsExpanded() returned
3054 // true, but GetLastChild() returned invalid item
3063 DoSelectItem( last
, unselect_others
, extended_select
);
3068 // <Home> selects the root item
3071 wxTreeItemId prev
= GetRootItem();
3075 if ( HasFlag(wxTR_HIDE_ROOT
) )
3077 wxTreeItemIdValue cookie
;
3078 prev
= GetFirstChild(prev
, cookie
);
3083 DoSelectItem( prev
, unselect_others
, extended_select
);
3088 // do not use wxIsalnum() here
3089 if ( !event
.HasModifiers() &&
3090 ((keyCode
>= '0' && keyCode
<= '9') ||
3091 (keyCode
>= 'a' && keyCode
<= 'z') ||
3092 (keyCode
>= 'A' && keyCode
<= 'Z' )))
3094 // find the next item starting with the given prefix
3095 wxChar ch
= (wxChar
)keyCode
;
3097 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
3108 // also start the timer to reset the current prefix if the user
3109 // doesn't press any more alnum keys soon -- we wouldn't want
3110 // to use this prefix for a new item search
3113 m_findTimer
= new wxTreeFindTimer(this);
3116 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
3126 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
3131 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3132 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3133 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3134 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3135 if (flags
) return wxTreeItemId();
3137 if (m_anchor
== NULL
)
3139 flags
= wxTREE_HITTEST_NOWHERE
;
3140 return wxTreeItemId();
3143 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3147 flags
= wxTREE_HITTEST_NOWHERE
;
3148 return wxTreeItemId();
3153 // get the bounding rectangle of the item (or of its label only)
3154 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3156 bool textOnly
) const
3158 wxCHECK_MSG( item
.IsOk(), false,
3159 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3161 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3166 rect
.width
= i
->GetWidth();
3168 if ( m_imageListNormal
)
3170 int image_w
, image_h
;
3171 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
3172 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3175 else // the entire line
3178 rect
.width
= GetClientSize().x
;
3182 rect
.height
= GetLineHeight(i
);
3184 // we have to return the logical coordinates, not physical ones
3185 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3190 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3191 wxClassInfo
* WXUNUSED(textCtrlClass
))
3193 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
3195 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3197 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3198 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3204 // We have to call this here because the label in
3205 // question might just have been added and no screen
3206 // update taken place.
3208 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3211 DoDirtyProcessing();
3214 // TODO: use textCtrlClass here to create the control of correct class
3215 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3217 m_textCtrl
->SetFocus();
3222 // returns a pointer to the text edit control if the item is being
3223 // edited, NULL otherwise (it's assumed that no more than one item may
3224 // be edited simultaneously)
3225 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3230 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3231 bool discardChanges
)
3233 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3235 m_textCtrl
->EndEdit(discardChanges
);
3238 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3239 const wxString
& value
)
3241 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3243 le
.m_editCancelled
= false;
3245 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3248 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3250 // let owner know that the edit was cancelled
3251 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3252 le
.m_label
= wxEmptyString
;
3253 le
.m_editCancelled
= true;
3255 GetEventHandler()->ProcessEvent( le
);
3258 void wxGenericTreeCtrl::OnRenameTimer()
3260 EditLabel( m_current
);
3263 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3265 if ( !m_anchor
)return;
3267 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3269 // Is the mouse over a tree item button?
3271 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3272 wxGenericTreeItem
*underMouse
= thisItem
;
3274 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3275 #endif // wxUSE_TOOLTIPS
3278 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3279 (!event
.LeftIsDown()) &&
3281 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3289 if (underMouse
!= m_underMouse
)
3293 // unhighlight old item
3294 wxGenericTreeItem
*tmp
= m_underMouse
;
3295 m_underMouse
= NULL
;
3299 m_underMouse
= underMouse
;
3301 RefreshLine( m_underMouse
);
3305 // Determines what item we are hovering over and need a tooltip for
3306 wxTreeItemId hoverItem
= thisItem
;
3308 // We do not want a tooltip if we are dragging, or if the rename timer is
3310 if ( underMouseChanged
&&
3313 (!m_renameTimer
|| !m_renameTimer
->IsRunning()) )
3315 // Ask the tree control what tooltip (if any) should be shown
3317 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3319 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3321 SetToolTip(hevent
.m_label
);
3326 // we process left mouse up event (enables in-place edit), middle/right down
3327 // (pass to the user code), left dbl click (activate item) and
3328 // dragging/moving events for items drag-and-drop
3329 if ( !(event
.LeftDown() ||
3331 event
.MiddleDown() ||
3332 event
.RightDown() ||
3333 event
.LeftDClick() ||
3335 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3344 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3346 if ( event
.Dragging() && !m_isDragging
)
3348 if (m_dragCount
== 0)
3353 if (m_dragCount
!= 3)
3355 // wait until user drags a bit further...
3359 wxEventType command
= event
.RightIsDown()
3360 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3361 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3363 wxTreeEvent
nevent(command
, this, m_current
);
3364 nevent
.SetPoint(CalcScrolledPosition(pt
));
3366 // by default the dragging is not supported, the user code must
3367 // explicitly allow the event for it to take place
3370 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3372 // we're going to drag this item
3373 m_isDragging
= true;
3375 // remember the old cursor because we will change it while
3377 m_oldCursor
= m_cursor
;
3379 // in a single selection control, hide the selection temporarily
3380 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3382 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3384 if ( m_oldSelection
)
3386 m_oldSelection
->SetHilight(false);
3387 RefreshLine(m_oldSelection
);
3394 else if ( event
.Dragging() )
3396 if ( item
!= m_dropTarget
)
3398 // unhighlight the previous drop target
3399 DrawDropEffect(m_dropTarget
);
3401 m_dropTarget
= item
;
3403 // highlight the current drop target if any
3404 DrawDropEffect(m_dropTarget
);
3406 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3413 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3417 // erase the highlighting
3418 DrawDropEffect(m_dropTarget
);
3420 if ( m_oldSelection
)
3422 m_oldSelection
->SetHilight(true);
3423 RefreshLine(m_oldSelection
);
3424 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3427 // generate the drag end event
3428 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3430 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3432 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3434 m_isDragging
= false;
3435 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3437 SetCursor(m_oldCursor
);
3439 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3447 // If we got to this point, we are not dragging or moving the mouse.
3448 // Because the code in carbon/toplevel.cpp will only set focus to the
3449 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3451 // We skip even if we didn't hit an item because we still should
3452 // restore focus to the tree control even if we didn't exactly hit an
3454 if ( event
.LeftDown() )
3459 // here we process only the messages which happen on tree items
3463 if (item
== NULL
) return; /* we hit the blank area */
3465 if ( event
.RightDown() )
3467 // If the item is already selected, do not update the selection.
3468 // Multi-selections should not be cleared if a selected item is
3470 if (!IsSelected(item
))
3472 DoSelectItem(item
, true, false);
3476 nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3477 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3478 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3480 // Consistent with MSW (for now), send the ITEM_MENU *after*
3481 // the RIGHT_CLICK event. TODO: This behavior may change.
3482 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3483 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3484 GetEventHandler()->ProcessEvent(nevent2
);
3486 else if ( event
.MiddleDown() )
3489 nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3490 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3491 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3493 else if ( event
.LeftUp() )
3495 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3498 nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, this, item
);
3499 GetEventHandler()->ProcessEvent(nevent
);
3502 // this facilitates multiple-item drag-and-drop
3504 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3506 wxArrayTreeItemIds selections
;
3507 size_t count
= GetSelections(selections
);
3513 DoSelectItem(item
, true, false);
3519 if ( (item
== m_current
) &&
3520 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3521 HasFlag(wxTR_EDIT_LABELS
) )
3523 if ( m_renameTimer
)
3525 if ( m_renameTimer
->IsRunning() )
3526 m_renameTimer
->Stop();
3530 m_renameTimer
= new wxTreeRenameTimer( this );
3533 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3536 m_lastOnSame
= false;
3539 else // !RightDown() && !MiddleDown() && !LeftUp()
3541 // ==> LeftDown() || LeftDClick()
3542 if ( event
.LeftDown() )
3544 m_lastOnSame
= item
== m_current
;
3547 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3549 // only toggle the item for a single click, double click on
3550 // the button doesn't do anything (it toggles the item twice)
3551 if ( event
.LeftDown() )
3556 // don't select the item if the button was clicked
3561 // clear the previously selected items, if the
3562 // user clicked outside of the present selection.
3563 // otherwise, perform the deselection on mouse-up.
3564 // this allows multiple drag and drop to work.
3565 // but if Cmd is down, toggle selection of the clicked item
3566 if (!IsSelected(item
) || event
.CmdDown())
3568 // how should the selection work for this event?
3569 bool is_multiple
, extended_select
, unselect_others
;
3570 EventFlagsToSelType(GetWindowStyleFlag(),
3577 DoSelectItem(item
, unselect_others
, extended_select
);
3581 // For some reason, Windows isn't recognizing a left double-click,
3582 // so we need to simulate it here. Allow 200 milliseconds for now.
3583 if ( event
.LeftDClick() )
3585 // double clicking should not start editing the item label
3586 if ( m_renameTimer
)
3587 m_renameTimer
->Stop();
3589 m_lastOnSame
= false;
3591 // send activate event first
3593 nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3594 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3595 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3597 // if the user code didn't process the activate event,
3598 // handle it ourselves by toggling the item when it is
3600 if ( item
->HasPlus() )
3610 void wxGenericTreeCtrl::OnInternalIdle()
3612 wxWindow::OnInternalIdle();
3614 // Check if we need to select the root item
3615 // because nothing else has been selected.
3616 // Delaying it means that we can invoke event handlers
3617 // as required, when a first item is selected.
3618 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3621 SelectItem(m_select_me
);
3622 else if (GetRootItem().IsOk())
3623 SelectItem(GetRootItem());
3626 // after all changes have been done to the tree control,
3627 // actually redraw the tree when everything is over
3629 DoDirtyProcessing();
3633 wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem
*item
,
3638 int x
= level
*m_indent
;
3639 if (!HasFlag(wxTR_HIDE_ROOT
))
3643 else if (level
== 0)
3645 // a hidden root is not evaluated, but its
3646 // children are always calculated
3650 CalculateSize( item
, dc
);
3653 item
->SetX( x
+m_spacing
);
3655 y
+= GetLineHeight(item
);
3657 if ( !item
->IsExpanded() )
3659 // we don't need to calculate collapsed branches
3664 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3665 size_t n
, count
= children
.GetCount();
3667 for (n
= 0; n
< count
; ++n
)
3668 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3671 void wxGenericTreeCtrl::CalculatePositions()
3673 if ( !m_anchor
) return;
3675 wxClientDC
dc(this);
3678 dc
.SetFont( m_normalFont
);
3680 dc
.SetPen( m_dottedPen
);
3681 //if(GetImageList() == NULL)
3682 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3685 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3688 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3691 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3694 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3696 if (m_dirty
|| IsFrozen() )
3699 wxSize client
= GetClientSize();
3702 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3703 rect
.width
= client
.x
;
3704 rect
.height
= client
.y
;
3706 Refresh(true, &rect
);
3708 AdjustMyScrollbars();
3711 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3713 if (m_dirty
|| IsFrozen() )
3717 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3718 rect
.width
= GetClientSize().x
;
3719 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3721 Refresh(true, &rect
);
3724 void wxGenericTreeCtrl::RefreshSelected()
3729 // TODO: this is awfully inefficient, we should keep the list of all
3730 // selected items internally, should be much faster
3732 RefreshSelectedUnder(m_anchor
);
3735 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3740 if ( item
->IsSelected() )
3743 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3744 size_t count
= children
.GetCount();
3745 for ( size_t n
= 0; n
< count
; n
++ )
3747 RefreshSelectedUnder(children
[n
]);
3751 void wxGenericTreeCtrl::DoThaw()
3753 wxTreeCtrlBase::DoThaw();
3756 DoDirtyProcessing();
3761 // ----------------------------------------------------------------------------
3762 // changing colours: we need to refresh the tree control
3763 // ----------------------------------------------------------------------------
3765 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3767 if ( !wxWindow::SetBackgroundColour(colour
) )
3775 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3777 if ( !wxWindow::SetForegroundColour(colour
) )
3785 // Process the tooltip event, to speed up event processing.
3786 // Doesn't actually get a tooltip.
3787 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3793 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3794 // be removed, as well as the #else case below.
3795 #define _USE_VISATTR 0
3800 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3802 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3806 // Use the same color scheme as wxListBox
3807 return wxListBox::GetClassDefaultAttributes(variant
);
3809 wxVisualAttributes attr
;
3810 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
3811 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3812 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3817 void wxGenericTreeCtrl::DoDirtyProcessing()
3824 CalculatePositions();
3826 AdjustMyScrollbars();
3829 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3831 // make sure all positions are calculated as normally this only done during
3832 // idle time but we need them for base class DoGetBestSize() to return the
3834 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
3836 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
3838 // there seems to be an implicit extra border around the items, although
3839 // I'm not really sure where does it come from -- but without this, the
3840 // scrollbars appear in a tree with default/best size
3843 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
3844 // scrollbars still appear
3845 const wxSize
& borderSize
= GetWindowBorderSize();
3847 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
3849 size
.x
+= PIXELS_PER_UNIT
- dx
;
3850 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
3852 size
.y
+= PIXELS_PER_UNIT
- dy
;
3854 // we need to update the cache too as the base class cached its own value
3855 CacheBestSize(size
);
3860 #endif // wxUSE_TREECTRL