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
150 wxGenericTreeItem( wxGenericTreeItem
*parent
,
151 const wxString
& text
,
154 wxTreeItemData
*data
);
156 ~wxGenericTreeItem();
159 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
161 const wxString
& GetText() const { return m_text
; }
162 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
163 { return m_images
[which
]; }
164 wxTreeItemData
*GetData() const { return m_data
; }
165 int GetState() const { return m_state
; }
167 // returns the current image for the item (depending on its
168 // selected/expanded/whatever state)
169 int GetCurrentImage() const;
171 void SetText(const wxString
& text
)
178 void SetImage(int image
, wxTreeItemIcon which
)
180 m_images
[which
] = image
;
184 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
185 void SetState(int state
) { m_state
= state
; m_width
= 0; }
187 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
189 void SetBold(bool bold
)
196 int GetX() const { return m_x
; }
197 int GetY() const { return m_y
; }
199 void SetX(int x
) { m_x
= x
; }
200 void SetY(int y
) { m_y
= y
; }
202 int GetHeight() const { return m_height
; }
203 int GetWidth() const { return m_width
; }
205 int GetTextHeight() const
207 wxASSERT_MSG( m_heightText
!= -1, "must call CalculateSize() first" );
212 int GetTextWidth() const
214 wxASSERT_MSG( m_widthText
!= -1, "must call CalculateSize() first" );
219 wxGenericTreeItem
*GetParent() const { return m_parent
; }
221 // sets the items font for the specified DC if it uses any special font or
222 // simply returns false otherwise
223 bool SetFont(wxGenericTreeCtrl
*control
, wxDC
& dc
) const
227 wxTreeItemAttr
* const attr
= GetAttributes();
228 if ( attr
&& attr
->HasFont() )
229 font
= attr
->GetFont();
231 font
= control
->m_boldFont
;
241 // deletes all children notifying the treectrl about it
242 void DeleteChildren(wxGenericTreeCtrl
*tree
);
244 // get count of all children (and grand children if 'recursively')
245 size_t GetChildrenCount(bool recursively
= true) const;
247 void Insert(wxGenericTreeItem
*child
, size_t index
)
248 { m_children
.Insert(child
, index
); }
250 // calculate and cache the item size using either the provided DC (which is
251 // supposed to have wxGenericTreeCtrl::m_normalFont selected into it!) or a
252 // wxClientDC on the control window
253 void CalculateSize(wxGenericTreeCtrl
*control
, wxDC
& dc
)
254 { DoCalculateSize(control
, dc
, true /* dc uses normal font */); }
255 void CalculateSize(wxGenericTreeCtrl
*control
);
257 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
259 void ResetSize() { m_width
= 0; }
260 void ResetTextSize() { m_width
= 0; m_widthText
= -1; }
261 void RecursiveResetSize();
262 void RecursiveResetTextSize();
264 // return the item at given position (or NULL if no item), onButton is
265 // true if the point belongs to the item's button, otherwise it lies
266 // on the item's label
267 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
268 const wxGenericTreeCtrl
*,
272 void Expand() { m_isCollapsed
= false; }
273 void Collapse() { m_isCollapsed
= true; }
275 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
278 bool HasChildren() const { return !m_children
.IsEmpty(); }
279 bool IsSelected() const { return m_hasHilight
!= 0; }
280 bool IsExpanded() const { return !m_isCollapsed
; }
281 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
282 bool IsBold() const { return m_isBold
!= 0; }
285 // get them - may be NULL
286 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
287 // get them ensuring that the pointer is not NULL
288 wxTreeItemAttr
& Attr()
292 m_attr
= new wxTreeItemAttr
;
298 void SetAttributes(wxTreeItemAttr
*attr
)
300 if ( m_ownsAttr
) delete m_attr
;
306 // set them and delete when done
307 void AssignAttributes(wxTreeItemAttr
*attr
)
316 // calculate the size of this item, i.e. set m_width, m_height and
317 // m_widthText and m_heightText properly
319 // if dcUsesNormalFont is true, the current dc font must be the normal tree
321 void DoCalculateSize(wxGenericTreeCtrl
*control
,
323 bool dcUsesNormalFont
);
325 // since there can be very many of these, we save size by chosing
326 // the smallest representation for the elements and by ordering
327 // the members to avoid padding.
328 wxString m_text
; // label to be rendered for item
332 wxTreeItemData
*m_data
; // user-provided data
334 int m_state
; // item state
336 wxArrayGenericTreeItems m_children
; // list of children
337 wxGenericTreeItem
*m_parent
; // parent of this item
339 wxTreeItemAttr
*m_attr
; // attributes???
341 // tree ctrl images for the normal, selected, expanded and
342 // expanded+selected states
343 int m_images
[wxTreeItemIcon_Max
];
345 wxCoord m_x
; // (virtual) offset from top
346 wxCoord m_y
; // (virtual) offset from left
347 int m_width
; // width of this item
348 int m_height
; // height of this item
350 // use bitfields to save size
351 unsigned int m_isCollapsed
:1;
352 unsigned int m_hasHilight
:1; // same as focused
353 unsigned int m_hasPlus
:1; // used for item which doesn't have
354 // children but has a [+] button
355 unsigned int m_isBold
:1; // render the label in bold font
356 unsigned int m_ownsAttr
:1; // delete attribute when done
358 DECLARE_NO_COPY_CLASS(wxGenericTreeItem
)
361 // =============================================================================
363 // =============================================================================
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 // translate the key or mouse event flags to the type of selection we're
371 static void EventFlagsToSelType(long style
,
375 bool &extended_select
,
376 bool &unselect_others
)
378 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
379 extended_select
= shiftDown
&& is_multiple
;
380 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
383 // check if the given item is under another one
385 IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
389 if ( item
== parent
)
391 // item is a descendant of parent
395 item
= item
->GetParent();
401 // -----------------------------------------------------------------------------
402 // wxTreeRenameTimer (internal)
403 // -----------------------------------------------------------------------------
405 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
410 void wxTreeRenameTimer::Notify()
412 m_owner
->OnRenameTimer();
415 //-----------------------------------------------------------------------------
416 // wxTreeTextCtrl (internal)
417 //-----------------------------------------------------------------------------
419 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
420 EVT_CHAR (wxTreeTextCtrl::OnChar
)
421 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
422 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
425 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
426 wxGenericTreeItem
*item
)
427 : m_itemEdited(item
), m_startValue(item
->GetText())
430 m_aboutToFinish
= false;
432 int w
= m_itemEdited
->GetWidth(),
433 h
= m_itemEdited
->GetHeight();
436 m_owner
->CalcScrolledPosition(item
->GetX(), item
->GetY(), &x
, &y
);
441 int image
= item
->GetCurrentImage();
442 if ( image
!= NO_IMAGE
)
444 if ( m_owner
->m_imageListNormal
)
446 m_owner
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
447 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
451 wxFAIL_MSG(_T("you must create an image list to use images!"));
455 // FIXME: what are all these hardcoded 4, 8 and 11s really?
459 wxSize bs
= DoGetBestSize() ;
460 // edit control height
463 int diff
= h
- ( bs
.y
- 8 ) ;
469 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
470 wxPoint(x
- 4, y
- 4), wxSize(w
+ 11, h
+ 8));
473 void wxTreeTextCtrl::EndEdit(bool discardChanges
)
475 m_aboutToFinish
= true;
477 if ( discardChanges
)
479 m_owner
->OnRenameCancelled(m_itemEdited
);
485 // Notify the owner about the changes
488 // Even if vetoed, close the control (consistent with MSW)
493 bool wxTreeTextCtrl::AcceptChanges()
495 const wxString value
= GetValue();
497 if ( value
== m_startValue
)
499 // nothing changed, always accept
500 // when an item remains unchanged, the owner
501 // needs to be notified that the user decided
502 // not to change the tree item label, and that
503 // the edit has been cancelled
505 m_owner
->OnRenameCancelled(m_itemEdited
);
509 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
511 // vetoed by the user
515 // accepted, do rename the item
516 m_owner
->SetItemText(m_itemEdited
, value
);
521 void wxTreeTextCtrl::Finish( bool setfocus
)
523 m_owner
->ResetTextControl();
525 wxPendingDelete
.Append(this);
531 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
533 switch ( event
.m_keyCode
)
548 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
550 if ( !m_aboutToFinish
)
552 // auto-grow the textctrl:
553 wxSize parentSize
= m_owner
->GetSize();
554 wxPoint myPos
= GetPosition();
555 wxSize mySize
= GetSize();
557 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
558 if (myPos
.x
+ sx
> parentSize
.x
)
559 sx
= parentSize
.x
- myPos
.x
;
562 SetSize(sx
, wxDefaultCoord
);
568 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
570 if ( !m_aboutToFinish
)
572 if ( !AcceptChanges() )
573 m_owner
->OnRenameCancelled( m_itemEdited
);
578 // We should let the native text control handle focus, too.
582 // -----------------------------------------------------------------------------
584 // -----------------------------------------------------------------------------
586 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
587 const wxString
& text
,
588 int image
, int selImage
,
589 wxTreeItemData
*data
)
592 m_images
[wxTreeItemIcon_Normal
] = image
;
593 m_images
[wxTreeItemIcon_Selected
] = selImage
;
594 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
595 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
598 m_state
= wxTREE_ITEMSTATE_NONE
;
601 m_isCollapsed
= true;
602 m_hasHilight
= false;
608 m_attr
= (wxTreeItemAttr
*)NULL
;
611 // We don't know the height here yet.
619 wxGenericTreeItem::~wxGenericTreeItem()
623 if (m_ownsAttr
) delete m_attr
;
625 wxASSERT_MSG( m_children
.IsEmpty(),
626 "must call DeleteChildren() before deleting the item" );
629 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
631 size_t count
= m_children
.GetCount();
632 for ( size_t n
= 0; n
< count
; n
++ )
634 wxGenericTreeItem
*child
= m_children
[n
];
635 tree
->SendDeleteEvent(child
);
637 child
->DeleteChildren(tree
);
638 if ( child
== tree
->m_select_me
)
639 tree
->m_select_me
= NULL
;
646 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
648 size_t count
= m_children
.GetCount();
652 size_t total
= count
;
653 for (size_t n
= 0; n
< count
; ++n
)
655 total
+= m_children
[n
]->GetChildrenCount();
661 void wxGenericTreeItem::GetSize( int &x
, int &y
,
662 const wxGenericTreeCtrl
*theButton
)
664 int bottomY
=m_y
+theButton
->GetLineHeight(this);
665 if ( y
< bottomY
) y
= bottomY
;
666 int width
= m_x
+ m_width
;
667 if ( x
< width
) x
= width
;
671 size_t count
= m_children
.GetCount();
672 for ( size_t n
= 0; n
< count
; ++n
)
674 m_children
[n
]->GetSize( x
, y
, theButton
);
679 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
680 const wxGenericTreeCtrl
*theCtrl
,
684 // for a hidden root node, don't evaluate it, but do evaluate children
685 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
688 int h
= theCtrl
->GetLineHeight(this);
689 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
691 int y_mid
= m_y
+ h
/2;
692 if (point
.y
< y_mid
)
693 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
695 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
697 int xCross
= m_x
- theCtrl
->GetSpacing();
699 // according to the drawing code the triangels are drawn
700 // at -4 , -4 from the position up to +10/+10 max
701 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
702 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
703 HasPlus() && theCtrl
->HasButtons() )
705 // 5 is the size of the plus sign
706 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
707 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
708 HasPlus() && theCtrl
->HasButtons() )
711 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
715 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
720 // assuming every image (normal and selected) has the same size!
721 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
723 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
730 if ( (GetState() != wxTREE_ITEMSTATE_NONE
) &&
731 theCtrl
->m_imageListState
)
733 theCtrl
->m_imageListState
->GetSize(GetState(),
737 if ((state_w
!= -1) && (point
.x
<= m_x
+ state_w
+ 1))
738 flags
|= wxTREE_HITTEST_ONITEMSTATEICON
;
739 else if ((image_w
!= -1) &&
741 (state_w
!= -1 ? state_w
+
742 MARGIN_BETWEEN_STATE_AND_IMAGE
745 flags
|= wxTREE_HITTEST_ONITEMICON
;
747 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
753 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
754 if (point
.x
> m_x
+m_width
)
755 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
760 // if children are expanded, fall through to evaluate them
761 if (m_isCollapsed
) return (wxGenericTreeItem
*) NULL
;
765 size_t count
= m_children
.GetCount();
766 for ( size_t n
= 0; n
< count
; n
++ )
768 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
776 return (wxGenericTreeItem
*) NULL
;
779 int wxGenericTreeItem::GetCurrentImage() const
781 int image
= NO_IMAGE
;
786 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
789 if ( image
== NO_IMAGE
)
791 // we usually fall back to the normal item, but try just the
792 // expanded one (and not selected) first in this case
793 image
= GetImage(wxTreeItemIcon_Expanded
);
799 image
= GetImage(wxTreeItemIcon_Selected
);
802 // maybe it doesn't have the specific image we want,
803 // try the default one instead
804 if ( image
== NO_IMAGE
) image
= GetImage();
809 void wxGenericTreeItem::CalculateSize(wxGenericTreeCtrl
* control
)
811 // check if we need to do anything before creating the DC
815 wxClientDC
dc(control
);
816 DoCalculateSize(control
, dc
, false /* normal font not used */);
820 wxGenericTreeItem::DoCalculateSize(wxGenericTreeCtrl
* control
,
822 bool dcUsesNormalFont
)
824 if ( m_width
!= 0 ) // Size known, nothing to do
827 if ( m_widthText
== -1 )
830 if ( SetFont(control
, dc
) )
834 else // we have no special font
836 if ( !dcUsesNormalFont
)
838 // but we do need to ensure that the normal font is used: notice
839 // that this doesn't count as changing the font as we don't need
841 dc
.SetFont(control
->m_normalFont
);
847 dc
.GetTextExtent( GetText(), &m_widthText
, &m_heightText
);
849 // restore normal font if the DC used it previously and we changed it
851 dc
.SetFont(control
->m_normalFont
);
854 int text_h
= m_heightText
+ 2;
858 int image
= GetCurrentImage();
859 if ( image
!= NO_IMAGE
)
861 if ( control
->m_imageListNormal
)
863 control
->m_imageListNormal
->GetSize( image
, image_w
, image_h
);
864 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
868 int state_h
= 0, state_w
= 0;
869 int state
= GetState();
870 if ( state
!= wxTREE_ITEMSTATE_NONE
)
872 if ( control
->m_imageListState
)
874 control
->m_imageListState
->GetSize( state
, state_w
, state_h
);
875 if ( image
!= NO_IMAGE
)
876 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
878 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
882 m_height
= (image_h
> text_h
) ? image_h
: text_h
;
885 m_height
+= 2; // at least 2 pixels
887 m_height
+= m_height
/ 10; // otherwise 10% extra spacing
889 if (m_height
> control
->m_lineHeight
)
890 control
->m_lineHeight
= m_height
;
892 m_width
= state_w
+ image_w
+ m_widthText
+ 2;
895 void wxGenericTreeItem::RecursiveResetSize()
899 const size_t count
= m_children
.Count();
900 for (size_t i
= 0; i
< count
; i
++ )
901 m_children
[i
]->RecursiveResetSize();
904 void wxGenericTreeItem::RecursiveResetTextSize()
909 const size_t count
= m_children
.Count();
910 for (size_t i
= 0; i
< count
; i
++ )
911 m_children
[i
]->RecursiveResetTextSize();
914 // -----------------------------------------------------------------------------
915 // wxGenericTreeCtrl implementation
916 // -----------------------------------------------------------------------------
918 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
920 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
921 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
922 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
923 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
924 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
925 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
926 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
927 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
930 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
932 * wxTreeCtrl has to be a real class or we have problems with
933 * the run-time information.
936 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
939 // -----------------------------------------------------------------------------
940 // construction/destruction
941 // -----------------------------------------------------------------------------
943 void wxGenericTreeCtrl::Init()
948 m_select_me
= (wxGenericTreeItem
*) NULL
;
956 m_hilightBrush
= new wxBrush
958 wxSystemSettings::GetColour
960 wxSYS_COLOUR_HIGHLIGHT
965 m_hilightUnfocusedBrush
= new wxBrush
967 wxSystemSettings::GetColour
969 wxSYS_COLOUR_BTNSHADOW
974 m_imageListButtons
= NULL
;
975 m_ownsImageListButtons
= false;
978 m_isDragging
= false;
979 m_dropTarget
= m_oldSelection
= NULL
;
983 m_renameTimer
= NULL
;
987 m_dropEffectAboveItem
= false;
989 m_dndEffect
= NoEffect
;
990 m_dndEffectItem
= NULL
;
992 m_lastOnSame
= false;
994 #if defined( __WXMAC__ )
995 #if wxOSX_USE_ATSU_TEXT
996 m_normalFont
.MacCreateFromThemeFont( kThemeViewsFont
) ;
998 m_normalFont
.MacCreateFromUIFont( kCTFontViewsFontType
) ;
1001 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
1003 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1004 m_normalFont
.GetFamily(),
1005 m_normalFont
.GetStyle(),
1007 m_normalFont
.GetUnderlined(),
1008 m_normalFont
.GetFaceName(),
1009 m_normalFont
.GetEncoding());
1012 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
1017 const wxValidator
& validator
,
1018 const wxString
& name
)
1022 wxGetOsVersion(&major
, &minor
);
1025 style
|= wxTR_ROW_LINES
;
1028 if ( !wxControl::Create( parent
, id
, pos
, size
,
1029 style
|wxHSCROLL
|wxVSCROLL
,
1034 // If the tree display has no buttons, but does have
1035 // connecting lines, we can use a narrower layout.
1036 // It may not be a good idea to force this...
1037 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
1043 wxVisualAttributes attr
= GetDefaultAttributes();
1044 SetOwnForegroundColour( attr
.colFg
);
1045 SetOwnBackgroundColour( attr
.colBg
);
1047 SetOwnFont(attr
.font
);
1049 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1050 // style because we apparently get performance problems when using dotted
1051 // pen for drawing in some ports -- but under MSW it seems to work fine
1053 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
1055 m_dottedPen
= *wxGREY_PEN
;
1058 SetInitialSize(size
);
1063 wxGenericTreeCtrl::~wxGenericTreeCtrl()
1065 delete m_hilightBrush
;
1066 delete m_hilightUnfocusedBrush
;
1070 delete m_renameTimer
;
1073 if (m_ownsImageListButtons
)
1074 delete m_imageListButtons
;
1077 // -----------------------------------------------------------------------------
1079 // -----------------------------------------------------------------------------
1081 unsigned int wxGenericTreeCtrl::GetCount() const
1085 // the tree is empty
1089 unsigned int count
= m_anchor
->GetChildrenCount();
1090 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1092 // take the root itself into account
1099 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
1101 m_indent
= (unsigned short) indent
;
1106 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
1107 bool recursively
) const
1109 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
1111 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
1114 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
1116 // Do not try to expand the root node if it hasn't been created yet
1117 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
1119 // if we will hide the root, make sure children are visible
1120 m_anchor
->SetHasPlus();
1122 CalculatePositions();
1125 // right now, just sets the styles. Eventually, we may
1126 // want to update the inherited styles, but right now
1127 // none of the parents has updatable styles
1128 m_windowStyle
= styles
;
1132 // -----------------------------------------------------------------------------
1133 // functions to work with tree items
1134 // -----------------------------------------------------------------------------
1136 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
1138 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
1140 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
1143 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1144 wxTreeItemIcon which
) const
1146 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1148 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
1151 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1153 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1155 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
1158 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
1160 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
1162 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1163 return pItem
->GetState();
1166 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1168 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1170 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1171 return pItem
->Attr().GetTextColour();
1175 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1177 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1179 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1180 return pItem
->Attr().GetBackgroundColour();
1183 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1185 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1187 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1188 return pItem
->Attr().GetFont();
1192 wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1194 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1196 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1197 pItem
->SetText(text
);
1198 pItem
->CalculateSize(this);
1202 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1204 wxTreeItemIcon which
)
1206 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1208 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1209 pItem
->SetImage(image
, which
);
1210 pItem
->CalculateSize(this);
1215 wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1217 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1220 data
->SetId( item
);
1222 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1225 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1227 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1229 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1230 pItem
->SetState(state
);
1231 pItem
->CalculateSize(this);
1235 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1237 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1239 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1240 pItem
->SetHasPlus(has
);
1244 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1246 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1248 // avoid redrawing the tree if no real change
1249 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1250 if ( pItem
->IsBold() != bold
)
1252 pItem
->SetBold(bold
);
1254 // recalculate the item size as bold and non bold fonts have different
1256 pItem
->CalculateSize(this);
1261 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1264 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1270 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1271 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1274 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1275 pItem
->Attr().SetTextColour(fg
);
1276 pItem
->Attr().SetBackgroundColour(bg
);
1280 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1281 const wxColour
& col
)
1283 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1285 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1286 pItem
->Attr().SetTextColour(col
);
1290 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1291 const wxColour
& col
)
1293 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1295 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1296 pItem
->Attr().SetBackgroundColour(col
);
1301 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1303 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1305 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1306 pItem
->Attr().SetFont(font
);
1307 pItem
->ResetTextSize();
1308 pItem
->CalculateSize(this);
1312 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1314 wxTreeCtrlBase::SetFont(font
);
1316 m_normalFont
= font
;
1317 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1318 m_normalFont
.GetFamily(),
1319 m_normalFont
.GetStyle(),
1321 m_normalFont
.GetUnderlined(),
1322 m_normalFont
.GetFaceName(),
1323 m_normalFont
.GetEncoding());
1326 m_anchor
->RecursiveResetTextSize();
1332 // -----------------------------------------------------------------------------
1333 // item status inquiries
1334 // -----------------------------------------------------------------------------
1336 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1338 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1340 // An item is only visible if it's not a descendant of a collapsed item
1341 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1342 wxGenericTreeItem
* parent
= pItem
->GetParent();
1345 if (!parent
->IsExpanded())
1347 parent
= parent
->GetParent();
1351 GetViewStart(& startX
, & startY
);
1353 wxSize clientSize
= GetClientSize();
1356 if (!GetBoundingRect(item
, rect
))
1358 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1360 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1362 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1368 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1370 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1372 // consider that the item does have children if it has the "+" button: it
1373 // might not have them (if it had never been expanded yet) but then it
1374 // could have them as well and it's better to err on this side rather than
1375 // disabling some operations which are restricted to the items with
1376 // children for an item which does have them
1377 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1380 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1382 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1384 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1387 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1389 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1391 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1394 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1396 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1398 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1401 // -----------------------------------------------------------------------------
1403 // -----------------------------------------------------------------------------
1405 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1407 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1409 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1412 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1413 wxTreeItemIdValue
& cookie
) const
1415 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1418 return GetNextChild(item
, cookie
);
1421 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1422 wxTreeItemIdValue
& cookie
) const
1424 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1426 wxArrayGenericTreeItems
&
1427 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1429 // it's ok to cast cookie to size_t, we never have indices big enough to
1430 // overflow "void *"
1431 size_t *pIndex
= (size_t *)&cookie
;
1432 if ( *pIndex
< children
.GetCount() )
1434 return children
.Item((*pIndex
)++);
1438 // there are no more of them
1439 return wxTreeItemId();
1443 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1445 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1447 wxArrayGenericTreeItems
&
1448 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1449 return children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last());
1452 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1454 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1456 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1457 wxGenericTreeItem
*parent
= i
->GetParent();
1458 if ( parent
== NULL
)
1460 // root item doesn't have any siblings
1461 return wxTreeItemId();
1464 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1465 int index
= siblings
.Index(i
);
1466 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1468 size_t n
= (size_t)(index
+ 1);
1469 return n
== siblings
.GetCount() ? wxTreeItemId()
1470 : wxTreeItemId(siblings
[n
]);
1473 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1475 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1477 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1478 wxGenericTreeItem
*parent
= i
->GetParent();
1479 if ( parent
== NULL
)
1481 // root item doesn't have any siblings
1482 return wxTreeItemId();
1485 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1486 int index
= siblings
.Index(i
);
1487 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1489 return index
== 0 ? wxTreeItemId()
1490 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1493 // Only for internal use right now, but should probably be public
1494 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1496 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1498 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1500 // First see if there are any children.
1501 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1502 if (children
.GetCount() > 0)
1504 return children
.Item(0);
1508 // Try a sibling of this or ancestor instead
1509 wxTreeItemId p
= item
;
1510 wxTreeItemId toFind
;
1513 toFind
= GetNextSibling(p
);
1514 p
= GetItemParent(p
);
1515 } while (p
.IsOk() && !toFind
.IsOk());
1520 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1522 wxTreeItemId id
= GetRootItem();
1531 } while (id
.IsOk());
1533 return wxTreeItemId();
1536 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1538 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1539 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1541 wxTreeItemId id
= item
;
1544 while (id
= GetNext(id
), id
.IsOk())
1550 return wxTreeItemId();
1553 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1555 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1556 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1558 // find out the starting point
1559 wxTreeItemId prevItem
= GetPrevSibling(item
);
1560 if ( !prevItem
.IsOk() )
1562 prevItem
= GetItemParent(item
);
1565 // find the first visible item after it
1566 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1568 prevItem
= GetNext(prevItem
);
1569 if ( !prevItem
.IsOk() || prevItem
== item
)
1571 // there are no visible items before item
1572 return wxTreeItemId();
1576 // from there we must be able to navigate until this item
1577 while ( prevItem
.IsOk() )
1579 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1580 if ( !nextItem
.IsOk() || nextItem
== item
)
1583 prevItem
= nextItem
;
1589 // called by wxTextTreeCtrl when it marks itself for deletion
1590 void wxGenericTreeCtrl::ResetTextControl()
1595 // find the first item starting with the given prefix after the given item
1596 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1597 const wxString
& prefixOrig
) const
1599 // match is case insensitive as this is more convenient to the user: having
1600 // to press Shift-letter to go to the item starting with a capital letter
1601 // would be too bothersome
1602 wxString prefix
= prefixOrig
.Lower();
1604 // determine the starting point: we shouldn't take the current item (this
1605 // allows to switch between two items starting with the same letter just by
1606 // pressing it) but we shouldn't jump to the next one if the user is
1607 // continuing to type as otherwise he might easily skip the item he wanted
1608 wxTreeItemId id
= idParent
;
1609 if ( prefix
.length() == 1 )
1614 // look for the item starting with the given prefix after it
1615 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1620 // if we haven't found anything...
1623 // ... wrap to the beginning
1625 if ( HasFlag(wxTR_HIDE_ROOT
) )
1627 // can't select virtual root
1631 // and try all the items (stop when we get to the one we started from)
1632 while ( id
.IsOk() && id
!= idParent
&&
1633 !GetItemText(id
).Lower().StartsWith(prefix
) )
1637 // If we haven't found the item, id.IsOk() will be false, as per
1644 // -----------------------------------------------------------------------------
1646 // -----------------------------------------------------------------------------
1648 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1650 const wxString
& text
,
1653 wxTreeItemData
*data
)
1655 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1658 // should we give a warning here?
1659 return AddRoot(text
, image
, selImage
, data
);
1662 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1664 wxGenericTreeItem
*item
=
1665 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1669 data
->m_pItem
= item
;
1672 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1675 InvalidateBestSize();
1679 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1682 wxTreeItemData
*data
)
1684 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
1686 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1688 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1689 image
, selImage
, data
);
1692 data
->m_pItem
= m_anchor
;
1695 if (HasFlag(wxTR_HIDE_ROOT
))
1697 // if root is hidden, make sure we can navigate
1699 m_anchor
->SetHasPlus();
1701 CalculatePositions();
1704 if (!HasFlag(wxTR_MULTIPLE
))
1706 m_current
= m_key_current
= m_anchor
;
1707 m_current
->SetHilight( true );
1710 InvalidateBestSize();
1714 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1715 const wxTreeItemId
& idPrevious
,
1716 const wxString
& text
,
1717 int image
, int selImage
,
1718 wxTreeItemData
*data
)
1720 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1723 // should we give a warning here?
1724 return AddRoot(text
, image
, selImage
, data
);
1728 if (idPrevious
.IsOk())
1730 index
= parent
->GetChildren().Index(
1731 (wxGenericTreeItem
*) idPrevious
.m_pItem
);
1732 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1733 "previous item in wxGenericTreeCtrl::InsertItem() "
1734 "is not a sibling" );
1737 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1741 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1743 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1744 GetEventHandler()->ProcessEvent( event
);
1747 // Don't leave edit or selection on a child which is about to disappear
1748 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1750 if ( m_textCtrl
&& item
!= m_textCtrl
->item() &&
1751 IsDescendantOf(item
, m_textCtrl
->item()) )
1753 m_textCtrl
->EndEdit( true );
1756 if ( item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
) )
1758 m_key_current
= NULL
;
1761 if ( IsDescendantOf(item
, m_select_me
) )
1766 if ( item
!= m_current
&& IsDescendantOf(item
, m_current
) )
1768 m_current
->SetHilight( false );
1774 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1776 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1778 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1779 ChildrenClosing(item
);
1780 item
->DeleteChildren(this);
1781 InvalidateBestSize();
1784 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1786 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1788 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1790 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1792 // can't delete the item being edited, cancel editing it first
1793 m_textCtrl
->EndEdit( true );
1796 wxGenericTreeItem
*parent
= item
->GetParent();
1798 // don't keep stale pointers around!
1799 if ( IsDescendantOf(item
, m_key_current
) )
1801 // Don't silently change the selection:
1802 // do it properly in idle time, so event
1803 // handlers get called.
1805 // m_key_current = parent;
1806 m_key_current
= NULL
;
1809 // m_select_me records whether we need to select
1810 // a different item, in idle time.
1811 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1813 m_select_me
= parent
;
1816 if ( IsDescendantOf(item
, m_current
) )
1818 // Don't silently change the selection:
1819 // do it properly in idle time, so event
1820 // handlers get called.
1822 // m_current = parent;
1824 m_select_me
= parent
;
1827 // remove the item from the tree
1830 parent
->GetChildren().Remove( item
); // remove by value
1832 else // deleting the root
1834 // nothing will be left in the tree
1838 // and delete all of its children and the item itself now
1839 item
->DeleteChildren(this);
1840 SendDeleteEvent(item
);
1842 if (item
== m_select_me
)
1847 InvalidateBestSize();
1850 void wxGenericTreeCtrl::DeleteAllItems()
1858 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1860 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1862 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1863 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1864 _T("can't expand hidden root") );
1866 if ( !item
->HasPlus() )
1869 if ( item
->IsExpanded() )
1872 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1874 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1876 // cancelled by program
1883 CalculatePositions();
1885 RefreshSubtree(item
);
1892 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1893 GetEventHandler()->ProcessEvent( event
);
1896 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1898 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1899 _T("can't collapse hidden root") );
1901 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1903 if ( !item
->IsExpanded() )
1906 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1907 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1909 // cancelled by program
1913 ChildrenClosing(item
);
1916 #if 0 // TODO why should items be collapsed recursively?
1917 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1918 size_t count
= children
.GetCount();
1919 for ( size_t n
= 0; n
< count
; n
++ )
1921 Collapse(children
[n
]);
1925 CalculatePositions();
1927 RefreshSubtree(item
);
1929 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1930 GetEventHandler()->ProcessEvent( event
);
1933 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1936 DeleteChildren(item
);
1939 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1941 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1943 if (item
->IsExpanded())
1949 void wxGenericTreeCtrl::Unselect()
1953 m_current
->SetHilight( false );
1954 RefreshLine( m_current
);
1961 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1963 if (item
->IsSelected())
1965 item
->SetHilight(false);
1969 if (item
->HasChildren())
1971 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1972 size_t count
= children
.GetCount();
1973 for ( size_t n
= 0; n
< count
; ++n
)
1975 UnselectAllChildren(children
[n
]);
1980 void wxGenericTreeCtrl::UnselectAll()
1982 wxTreeItemId rootItem
= GetRootItem();
1984 // the tree might not have the root item at all
1987 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1991 // Recursive function !
1992 // To stop we must have crt_item<last_item
1994 // Tag all next children, when no more children,
1995 // Move to parent (not to tag)
1996 // Keep going... if we found last_item, we stop.
1998 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
,
1999 wxGenericTreeItem
*last_item
,
2002 wxGenericTreeItem
*parent
= crt_item
->GetParent();
2004 if (parent
== NULL
) // This is root item
2005 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
2007 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
2008 int index
= children
.Index(crt_item
);
2009 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
2011 size_t count
= children
.GetCount();
2012 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
2014 if ( TagAllChildrenUntilLast(children
[n
], last_item
, select
) )
2018 return TagNextChildren(parent
, last_item
, select
);
2022 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
,
2023 wxGenericTreeItem
*last_item
,
2026 crt_item
->SetHilight(select
);
2027 RefreshLine(crt_item
);
2029 if (crt_item
==last_item
)
2032 if (crt_item
->HasChildren())
2034 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
2035 size_t count
= children
.GetCount();
2036 for ( size_t n
= 0; n
< count
; ++n
)
2038 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
2047 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
,
2048 wxGenericTreeItem
*item2
)
2052 // item2 is not necessary after item1
2053 // choice first' and 'last' between item1 and item2
2054 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
2055 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
2057 bool select
= m_current
->IsSelected();
2059 if ( TagAllChildrenUntilLast(first
,last
,select
) )
2062 TagNextChildren(first
,last
,select
);
2065 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
2066 bool unselect_others
,
2067 bool extended_select
)
2069 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2073 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2074 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2076 //wxCHECK_RET( ( (!unselect_others) && is_single),
2077 // wxT("this is a single selection tree") );
2079 // to keep going anyhow !!!
2082 if (item
->IsSelected())
2083 return; // nothing to do
2084 unselect_others
= true;
2085 extended_select
= false;
2087 else if ( unselect_others
&& item
->IsSelected() )
2089 // selection change if there is more than one item currently selected
2090 wxArrayTreeItemIds selected_items
;
2091 if ( GetSelections(selected_items
) == 1 )
2095 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2096 event
.m_itemOld
= m_current
;
2097 // TODO : Here we don't send any selection mode yet !
2099 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2102 wxTreeItemId parent
= GetItemParent( itemId
);
2103 while (parent
.IsOk())
2105 if (!IsExpanded(parent
))
2108 parent
= GetItemParent( parent
);
2112 if (unselect_others
)
2114 if (is_single
) Unselect(); // to speed up thing
2119 if (extended_select
)
2124 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
2127 // don't change the mark (m_current)
2128 SelectItemRange(m_current
, item
);
2132 bool select
= true; // the default
2134 // Check if we need to toggle hilight (ctrl mode)
2135 if (!unselect_others
)
2136 select
=!item
->IsSelected();
2138 m_current
= m_key_current
= item
;
2139 m_current
->SetHilight(select
);
2140 RefreshLine( m_current
);
2143 // This can cause idle processing to select the root
2144 // if no item is selected, so it must be after the
2146 EnsureVisible( itemId
);
2148 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2149 GetEventHandler()->ProcessEvent( event
);
2152 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
2156 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
2160 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2161 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
2163 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2164 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2167 item
->SetHilight(false);
2170 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2171 GetEventHandler()->ProcessEvent( event
);
2175 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
2176 wxArrayTreeItemIds
&array
) const
2178 if ( item
->IsSelected() )
2179 array
.Add(wxTreeItemId(item
));
2181 if ( item
->HasChildren() )
2183 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2184 size_t count
= children
.GetCount();
2185 for ( size_t n
= 0; n
< count
; ++n
)
2186 FillArray(children
[n
], array
);
2190 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
2193 wxTreeItemId idRoot
= GetRootItem();
2194 if ( idRoot
.IsOk() )
2196 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
2198 //else: the tree is empty, so no selections
2200 return array
.GetCount();
2203 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2205 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2207 if (!item
.IsOk()) return;
2209 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2211 // first expand all parent branches
2212 wxGenericTreeItem
*parent
= gitem
->GetParent();
2214 if ( HasFlag(wxTR_HIDE_ROOT
) )
2216 while ( parent
&& parent
!= m_anchor
)
2219 parent
= parent
->GetParent();
2227 parent
= parent
->GetParent();
2231 //if (parent) CalculatePositions();
2236 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2238 if (!item
.IsOk()) return;
2240 // We have to call this here because the label in
2241 // question might just have been added and no screen
2242 // update taken place.
2244 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2247 DoDirtyProcessing();
2249 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2251 // now scroll to the item
2252 int item_y
= gitem
->GetY();
2256 GetViewStart( &start_x
, &start_y
);
2257 start_y
*= PIXELS_PER_UNIT
;
2261 GetClientSize( &client_w
, &client_h
);
2263 if (item_y
< start_y
+3)
2268 m_anchor
->GetSize( x
, y
, this );
2269 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2270 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2271 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2272 // Item should appear at top
2273 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2274 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2275 x_pos
, item_y
/PIXELS_PER_UNIT
);
2277 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2282 m_anchor
->GetSize( x
, y
, this );
2283 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2284 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2285 item_y
+= PIXELS_PER_UNIT
+2;
2286 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2287 // Item should appear at bottom
2288 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2289 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2291 (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
2295 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2296 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2298 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2299 wxGenericTreeItem
**item2
)
2301 wxCHECK_MSG( s_treeBeingSorted
, 0,
2302 "bug in wxGenericTreeCtrl::SortChildren()" );
2304 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2307 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2309 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2311 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2313 wxCHECK_RET( !s_treeBeingSorted
,
2314 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2316 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2317 if ( children
.GetCount() > 1 )
2321 s_treeBeingSorted
= this;
2322 children
.Sort(tree_ctrl_compare_func
);
2323 s_treeBeingSorted
= NULL
;
2325 //else: don't make the tree dirty as nothing changed
2328 void wxGenericTreeCtrl::CalculateLineHeight()
2330 wxClientDC
dc(this);
2331 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2333 if ( m_imageListNormal
)
2335 // Calculate a m_lineHeight value from the normal Image sizes.
2336 // May be toggle off. Then wxGenericTreeCtrl will spread when
2337 // necessary (which might look ugly).
2338 int n
= m_imageListNormal
->GetImageCount();
2339 for (int i
= 0; i
< n
; i
++)
2341 int width
= 0, height
= 0;
2342 m_imageListNormal
->GetSize(i
, width
, height
);
2343 if (height
> m_lineHeight
) m_lineHeight
= height
;
2347 if ( m_imageListState
)
2349 // Calculate a m_lineHeight value from the state Image sizes.
2350 // May be toggle off. Then wxGenericTreeCtrl will spread when
2351 // necessary (which might look ugly).
2352 int n
= m_imageListState
->GetImageCount();
2353 for (int i
= 0; i
< n
; i
++)
2355 int width
= 0, height
= 0;
2356 m_imageListState
->GetSize(i
, width
, height
);
2357 if (height
> m_lineHeight
) m_lineHeight
= height
;
2361 if (m_imageListButtons
)
2363 // Calculate a m_lineHeight value from the Button image sizes.
2364 // May be toggle off. Then wxGenericTreeCtrl will spread when
2365 // necessary (which might look ugly).
2366 int n
= m_imageListButtons
->GetImageCount();
2367 for (int i
= 0; i
< n
; i
++)
2369 int width
= 0, height
= 0;
2370 m_imageListButtons
->GetSize(i
, width
, height
);
2371 if (height
> m_lineHeight
) m_lineHeight
= height
;
2375 if (m_lineHeight
< 30)
2376 m_lineHeight
+= 2; // at least 2 pixels
2378 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2381 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2383 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2384 m_imageListNormal
= imageList
;
2385 m_ownsImageListNormal
= false;
2389 m_anchor
->RecursiveResetSize();
2391 // Don't do any drawing if we're setting the list to NULL,
2392 // since we may be in the process of deleting the tree control.
2394 CalculateLineHeight();
2397 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2399 if (m_ownsImageListState
) delete m_imageListState
;
2400 m_imageListState
= imageList
;
2401 m_ownsImageListState
= false;
2405 m_anchor
->RecursiveResetSize();
2407 // Don't do any drawing if we're setting the list to NULL,
2408 // since we may be in the process of deleting the tree control.
2410 CalculateLineHeight();
2413 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2415 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2416 m_imageListButtons
= imageList
;
2417 m_ownsImageListButtons
= false;
2421 m_anchor
->RecursiveResetSize();
2423 CalculateLineHeight();
2426 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2428 SetButtonsImageList(imageList
);
2429 m_ownsImageListButtons
= true;
2432 // -----------------------------------------------------------------------------
2434 // -----------------------------------------------------------------------------
2436 void wxGenericTreeCtrl::AdjustMyScrollbars()
2441 m_anchor
->GetSize( x
, y
, this );
2442 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2443 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2444 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2445 int y_pos
= GetScrollPos( wxVERTICAL
);
2446 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2447 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2452 SetScrollbars( 0, 0, 0, 0 );
2456 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2458 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2459 return item
->GetHeight();
2461 return m_lineHeight
;
2464 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2466 item
->SetFont(this, dc
);
2467 item
->CalculateSize(this, dc
);
2469 wxCoord text_h
= item
->GetTextHeight();
2471 int image_h
= 0, image_w
= 0;
2472 int image
= item
->GetCurrentImage();
2473 if ( image
!= NO_IMAGE
)
2475 if ( m_imageListNormal
)
2477 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2478 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2486 int state_h
= 0, state_w
= 0;
2487 int state
= item
->GetState();
2488 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2490 if ( m_imageListState
)
2492 m_imageListState
->GetSize( state
, state_w
, state_h
);
2493 if ( image
!= NO_IMAGE
)
2494 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2496 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2500 state
= wxTREE_ITEMSTATE_NONE
;
2504 int total_h
= GetLineHeight(item
);
2505 bool drawItemBackground
= false,
2506 hasBgColour
= false;
2508 if ( item
->IsSelected() )
2510 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2511 drawItemBackground
= true;
2516 wxTreeItemAttr
* const attr
= item
->GetAttributes();
2517 if ( attr
&& attr
->HasBackgroundColour() )
2519 drawItemBackground
=
2521 colBg
= attr
->GetBackgroundColour();
2525 colBg
= GetBackgroundColour();
2527 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2530 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2532 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2536 GetVirtualSize(&w
, &h
);
2537 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2538 if (!item
->IsSelected())
2540 dc
.DrawRectangle(rect
);
2544 int flags
= wxCONTROL_SELECTED
;
2546 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2547 && IsControlActive( (ControlRef
)GetHandle() )
2550 flags
|= wxCONTROL_FOCUSED
;
2551 if ((item
== m_current
) && (m_hasFocus
))
2552 flags
|= wxCONTROL_CURRENT
;
2554 wxRendererNative::Get().
2555 DrawItemSelectionRect(this, dc
, rect
, flags
);
2558 else // no full row highlight
2560 if ( item
->IsSelected() &&
2561 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2563 // If it's selected, and there's an state image or normal image,
2564 // then we should take care to leave the area under the image
2565 // painted in the background colour.
2566 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2567 item
->GetY() + offset
,
2568 item
->GetWidth() - state_w
- image_w
+ 2,
2570 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2571 dc
.DrawRectangle( rect
);
2576 int flags
= wxCONTROL_SELECTED
;
2578 flags
|= wxCONTROL_FOCUSED
;
2579 if ((item
== m_current
) && (m_hasFocus
))
2580 flags
|= wxCONTROL_CURRENT
;
2581 wxRendererNative::Get().
2582 DrawItemSelectionRect(this, dc
, rect
, flags
);
2585 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2586 // don't allow backgrounds to be customized. Not drawing the background,
2587 // except for custom item backgrounds, works for both kinds of theme.
2588 else if (drawItemBackground
)
2590 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2591 item
->GetWidth()+2, total_h
-offset
);
2594 dc
.DrawRectangle( rect
);
2596 else // no specific background colour
2601 int flags
= wxCONTROL_SELECTED
;
2603 flags
|= wxCONTROL_FOCUSED
;
2604 if ((item
== m_current
) && (m_hasFocus
))
2605 flags
|= wxCONTROL_CURRENT
;
2606 wxRendererNative::Get().
2607 DrawItemSelectionRect(this, dc
, rect
, flags
);
2612 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2614 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2615 m_imageListState
->Draw( state
, dc
,
2618 (total_h
> state_h
? (total_h
-state_h
)/2
2620 wxIMAGELIST_DRAW_TRANSPARENT
);
2621 dc
.DestroyClippingRegion();
2624 if ( image
!= NO_IMAGE
)
2626 dc
.SetClippingRegion(item
->GetX() + state_w
, item
->GetY(),
2628 m_imageListNormal
->Draw( image
, dc
,
2629 item
->GetX() + state_w
,
2631 (total_h
> image_h
? (total_h
-image_h
)/2
2633 wxIMAGELIST_DRAW_TRANSPARENT
);
2634 dc
.DestroyClippingRegion();
2637 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2638 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2639 dc
.DrawText( item
->GetText(),
2640 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2641 (wxCoord
)(item
->GetY() + extraH
));
2643 // restore normal font
2644 dc
.SetFont( m_normalFont
);
2646 if (item
== m_dndEffectItem
)
2648 dc
.SetPen( *wxBLACK_PEN
);
2649 // DnD visual effects
2650 switch (m_dndEffect
)
2654 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2655 int w
= item
->GetWidth() + 2;
2656 int h
= total_h
+ 2;
2657 dc
.DrawRectangle( item
->GetX() - 1, item
->GetY() - 1, w
, h
);
2662 int x
= item
->GetX(),
2664 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2669 int x
= item
->GetX(),
2672 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2682 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem
*item
,
2687 int x
= level
*m_indent
;
2688 if (!HasFlag(wxTR_HIDE_ROOT
))
2692 else if (level
== 0)
2694 // always expand hidden root
2696 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2697 int count
= children
.GetCount();
2703 PaintLevel(children
[n
], dc
, 1, y
);
2704 } while (++n
< count
);
2706 if ( !HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
)
2709 // draw line down to last child
2710 origY
+= GetLineHeight(children
[0])>>1;
2711 oldY
+= GetLineHeight(children
[n
-1])>>1;
2712 dc
.DrawLine(3, origY
, 3, oldY
);
2718 item
->SetX(x
+m_spacing
);
2721 int h
= GetLineHeight(item
);
2723 int y_mid
= y_top
+ (h
>>1);
2726 int exposed_x
= dc
.LogicalToDeviceX(0);
2727 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2729 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2733 // don't draw rect outline if we already have the
2734 // background color under Mac
2735 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2736 #endif // !__WXMAC__
2740 if ( item
->IsSelected()
2741 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2742 // On wxMac, if the tree doesn't have the focus we draw an empty
2743 // rectangle, so we want to make sure that the text is visible
2744 // against the normal background, not the highlightbackground, so
2745 // don't use the highlight text colour unless we have the focus.
2746 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2753 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2758 wxTreeItemAttr
*attr
= item
->GetAttributes();
2759 if (attr
&& attr
->HasTextColour())
2760 colText
= attr
->GetTextColour();
2762 colText
= GetForegroundColour();
2766 dc
.SetTextForeground(colText
);
2770 PaintItem(item
, dc
);
2772 if (HasFlag(wxTR_ROW_LINES
))
2774 // if the background colour is white, choose a
2775 // contrasting color for the lines
2776 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2777 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2778 dc
.DrawLine(0, y_top
, 10000, y_top
);
2779 dc
.DrawLine(0, y
, 10000, y
);
2782 // restore DC objects
2783 dc
.SetBrush(*wxWHITE_BRUSH
);
2784 dc
.SetPen(m_dottedPen
);
2785 dc
.SetTextForeground(*wxBLACK
);
2787 if ( !HasFlag(wxTR_NO_LINES
) )
2789 // draw the horizontal line here
2791 if (x
> (signed)m_indent
)
2792 x_start
-= m_indent
;
2793 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2795 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2798 // should the item show a button?
2799 if ( item
->HasPlus() && HasButtons() )
2801 if ( m_imageListButtons
)
2803 // draw the image button here
2806 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2807 : wxTreeItemIcon_Normal
;
2808 if ( item
->IsSelected() )
2809 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2811 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2812 int xx
= x
- image_w
/2;
2813 int yy
= y_mid
- image_h
/2;
2815 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2816 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2817 wxIMAGELIST_DRAW_TRANSPARENT
);
2819 else // no custom buttons
2821 static const int wImage
= 9;
2822 static const int hImage
= 9;
2825 if (item
->IsExpanded())
2826 flag
|= wxCONTROL_EXPANDED
;
2827 if (item
== m_underMouse
)
2828 flag
|= wxCONTROL_CURRENT
;
2830 wxRendererNative::Get().DrawTreeItemButton
2834 wxRect(x
- wImage
/2,
2843 if (item
->IsExpanded())
2845 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2846 int count
= children
.GetCount();
2853 PaintLevel(children
[n
], dc
, level
, y
);
2854 } while (++n
< count
);
2856 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2858 // draw line down to last child
2859 oldY
+= GetLineHeight(children
[n
-1])>>1;
2860 if (HasButtons()) y_mid
+= 5;
2862 // Only draw the portion of the line that is visible, in case
2864 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2865 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2866 yOrigin
= abs(yOrigin
);
2867 GetClientSize(&width
, &height
);
2869 // Move end points to the begining/end of the view?
2870 if (y_mid
< yOrigin
)
2872 if (oldY
> yOrigin
+ height
)
2873 oldY
= yOrigin
+ height
;
2875 // after the adjustments if y_mid is larger than oldY then the
2876 // line isn't visible at all so don't draw anything
2878 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2884 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2888 if ( item
->HasPlus() )
2890 // it's a folder, indicate it by a border
2895 // draw a line under the drop target because the item will be
2897 DrawLine(item
, !m_dropEffectAboveItem
);
2900 SetCursor(*wxSTANDARD_CURSOR
);
2905 SetCursor(wxCURSOR_NO_ENTRY
);
2909 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2911 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2913 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2915 if (m_dndEffect
== NoEffect
)
2917 m_dndEffect
= BorderEffect
;
2918 m_dndEffectItem
= i
;
2922 m_dndEffect
= NoEffect
;
2923 m_dndEffectItem
= NULL
;
2926 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2927 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2928 RefreshRect( rect
);
2931 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2933 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2935 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2937 if (m_dndEffect
== NoEffect
)
2940 m_dndEffect
= BelowEffect
;
2942 m_dndEffect
= AboveEffect
;
2943 m_dndEffectItem
= i
;
2947 m_dndEffect
= NoEffect
;
2948 m_dndEffectItem
= NULL
;
2951 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2952 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2953 RefreshRect( rect
);
2956 // -----------------------------------------------------------------------------
2957 // wxWidgets callbacks
2958 // -----------------------------------------------------------------------------
2960 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2963 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2964 RefreshLine( m_current
);
2970 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2978 dc
.SetFont( m_normalFont
);
2979 dc
.SetPen( m_dottedPen
);
2981 // this is now done dynamically
2982 //if(GetImageList() == NULL)
2983 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2986 PaintLevel( m_anchor
, dc
, 0, y
);
2989 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2998 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
3007 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
3009 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
3010 te
.m_evtKey
= event
;
3011 if ( GetEventHandler()->ProcessEvent( te
) )
3013 // intercepted by the user code
3017 if ( (m_current
== 0) || (m_key_current
== 0) )
3023 // how should the selection work for this event?
3024 bool is_multiple
, extended_select
, unselect_others
;
3025 EventFlagsToSelType(GetWindowStyleFlag(),
3028 is_multiple
, extended_select
, unselect_others
);
3030 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3032 if (event
.GetKeyCode() == WXK_RIGHT
)
3033 event
.m_keyCode
= WXK_LEFT
;
3034 else if (event
.GetKeyCode() == WXK_LEFT
)
3035 event
.m_keyCode
= WXK_RIGHT
;
3040 // * : Expand all/Collapse all
3041 // ' ' | return : activate
3042 // up : go up (not last children!)
3044 // left : go to parent
3045 // right : open if parent and go next
3046 // home : go to root
3047 // end : go to last item without opening parents
3048 // alnum : start or continue searching for the item with this prefix
3049 int keyCode
= event
.GetKeyCode();
3054 if (m_current
->HasPlus() && !IsExpanded(m_current
))
3062 if ( !IsExpanded(m_current
) )
3065 ExpandAllChildren(m_current
);
3068 //else: fall through to Collapse() it
3072 if (IsExpanded(m_current
))
3074 Collapse(m_current
);
3080 // Use the item's bounding rectangle to determine position for
3083 GetBoundingRect(m_current
, ItemRect
, true);
3086 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
3087 // Use the left edge, vertical middle
3088 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
3090 ItemRect
.GetHeight() / 2);
3091 GetEventHandler()->ProcessEvent( eventMenu
);
3097 if ( !event
.HasModifiers() )
3100 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
3101 GetEventHandler()->ProcessEvent( eventAct
);
3104 // in any case, also generate the normal key event for this key,
3105 // even if we generated the ACTIVATED event above: this is what
3106 // wxMSW does and it makes sense because you might not want to
3107 // process ACTIVATED event at all and handle Space and Return
3108 // directly (and differently) which would be impossible otherwise
3112 // up goes to the previous sibling or to the last
3113 // of its children if it's expanded
3116 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
3119 prev
= GetItemParent( m_key_current
);
3120 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3122 break; // don't go to root if it is hidden
3126 wxTreeItemIdValue cookie
;
3127 wxTreeItemId current
= m_key_current
;
3128 // TODO: Huh? If we get here, we'd better be the first
3129 // child of our parent. How else could it be?
3130 if (current
== GetFirstChild( prev
, cookie
))
3132 // otherwise we return to where we came from
3136 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
3143 while ( IsExpanded(prev
) && HasChildren(prev
) )
3145 wxTreeItemId child
= GetLastChild(prev
);
3152 DoSelectItem( prev
, unselect_others
, extended_select
);
3153 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
3158 // left arrow goes to the parent
3161 wxTreeItemId prev
= GetItemParent( m_current
);
3162 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3164 // don't go to root if it is hidden
3165 prev
= GetPrevSibling( m_current
);
3169 DoSelectItem( prev
, unselect_others
, extended_select
);
3175 // this works the same as the down arrow except that we
3176 // also expand the item if it wasn't expanded yet
3177 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
3179 //else: don't try to expand hidden root item (which can be the
3180 // current one when the tree is empty)
3186 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
3188 wxTreeItemIdValue cookie
;
3189 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
3193 DoSelectItem( child
, unselect_others
, extended_select
);
3194 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
3198 wxTreeItemId next
= GetNextSibling( m_key_current
);
3201 wxTreeItemId current
= m_key_current
;
3202 while (current
.IsOk() && !next
)
3204 current
= GetItemParent( current
);
3205 if (current
) next
= GetNextSibling( current
);
3210 DoSelectItem( next
, unselect_others
, extended_select
);
3211 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
3217 // <End> selects the last visible tree item
3220 wxTreeItemId last
= GetRootItem();
3222 while ( last
.IsOk() && IsExpanded(last
) )
3224 wxTreeItemId lastChild
= GetLastChild(last
);
3226 // it may happen if the item was expanded but then all of
3227 // its children have been deleted - so IsExpanded() returned
3228 // true, but GetLastChild() returned invalid item
3237 DoSelectItem( last
, unselect_others
, extended_select
);
3242 // <Home> selects the root item
3245 wxTreeItemId prev
= GetRootItem();
3249 if ( HasFlag(wxTR_HIDE_ROOT
) )
3251 wxTreeItemIdValue cookie
;
3252 prev
= GetFirstChild(prev
, cookie
);
3257 DoSelectItem( prev
, unselect_others
, extended_select
);
3262 // do not use wxIsalnum() here
3263 if ( !event
.HasModifiers() &&
3264 ((keyCode
>= '0' && keyCode
<= '9') ||
3265 (keyCode
>= 'a' && keyCode
<= 'z') ||
3266 (keyCode
>= 'A' && keyCode
<= 'Z' )))
3268 // find the next item starting with the given prefix
3269 wxChar ch
= (wxChar
)keyCode
;
3271 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
3282 // also start the timer to reset the current prefix if the user
3283 // doesn't press any more alnum keys soon -- we wouldn't want
3284 // to use this prefix for a new item search
3287 m_findTimer
= new wxTreeFindTimer(this);
3290 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
3300 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
3305 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3306 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3307 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3308 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3309 if (flags
) return wxTreeItemId();
3311 if (m_anchor
== NULL
)
3313 flags
= wxTREE_HITTEST_NOWHERE
;
3314 return wxTreeItemId();
3317 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3321 flags
= wxTREE_HITTEST_NOWHERE
;
3322 return wxTreeItemId();
3327 // get the bounding rectangle of the item (or of its label only)
3328 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3330 bool textOnly
) const
3332 wxCHECK_MSG( item
.IsOk(), false,
3333 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3335 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3340 rect
.width
= i
->GetWidth();
3342 if ( m_imageListNormal
)
3344 int image_w
, image_h
;
3345 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
3346 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3349 else // the entire line
3352 rect
.width
= GetClientSize().x
;
3356 rect
.height
= GetLineHeight(i
);
3358 // we have to return the logical coordinates, not physical ones
3359 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3364 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3365 wxClassInfo
* WXUNUSED(textCtrlClass
))
3367 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
3369 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3371 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3372 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3378 // We have to call this here because the label in
3379 // question might just have been added and no screen
3380 // update taken place.
3382 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3385 DoDirtyProcessing();
3388 // TODO: use textCtrlClass here to create the control of correct class
3389 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3391 m_textCtrl
->SetFocus();
3396 // returns a pointer to the text edit control if the item is being
3397 // edited, NULL otherwise (it's assumed that no more than one item may
3398 // be edited simultaneously)
3399 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3404 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3405 bool discardChanges
)
3407 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3409 m_textCtrl
->EndEdit(discardChanges
);
3412 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3413 const wxString
& value
)
3415 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3417 le
.m_editCancelled
= false;
3419 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3422 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3424 // let owner know that the edit was cancelled
3425 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3426 le
.m_label
= wxEmptyString
;
3427 le
.m_editCancelled
= true;
3429 GetEventHandler()->ProcessEvent( le
);
3432 void wxGenericTreeCtrl::OnRenameTimer()
3434 EditLabel( m_current
);
3437 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3439 if ( !m_anchor
)return;
3441 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3443 // Is the mouse over a tree item button?
3445 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3446 wxGenericTreeItem
*underMouse
= thisItem
;
3448 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3449 #endif // wxUSE_TOOLTIPS
3452 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3453 (!event
.LeftIsDown()) &&
3455 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3463 if (underMouse
!= m_underMouse
)
3467 // unhighlight old item
3468 wxGenericTreeItem
*tmp
= m_underMouse
;
3469 m_underMouse
= NULL
;
3473 m_underMouse
= underMouse
;
3475 RefreshLine( m_underMouse
);
3479 // Determines what item we are hovering over and need a tooltip for
3480 wxTreeItemId hoverItem
= thisItem
;
3482 // We do not want a tooltip if we are dragging, or if the rename timer is
3484 if ( underMouseChanged
&&
3487 (!m_renameTimer
|| !m_renameTimer
->IsRunning()) )
3489 // Ask the tree control what tooltip (if any) should be shown
3491 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3493 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3495 SetToolTip(hevent
.m_label
);
3500 // we process left mouse up event (enables in-place edit), middle/right down
3501 // (pass to the user code), left dbl click (activate item) and
3502 // dragging/moving events for items drag-and-drop
3503 if ( !(event
.LeftDown() ||
3505 event
.MiddleDown() ||
3506 event
.RightDown() ||
3507 event
.LeftDClick() ||
3509 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3518 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3520 if ( event
.Dragging() && !m_isDragging
)
3522 if (m_dragCount
== 0)
3527 if (m_dragCount
!= 3)
3529 // wait until user drags a bit further...
3533 wxEventType command
= event
.RightIsDown()
3534 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3535 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3537 wxTreeEvent
nevent(command
, this, m_current
);
3538 nevent
.SetPoint(CalcScrolledPosition(pt
));
3540 // by default the dragging is not supported, the user code must
3541 // explicitly allow the event for it to take place
3544 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3546 // we're going to drag this item
3547 m_isDragging
= true;
3549 // remember the old cursor because we will change it while
3551 m_oldCursor
= m_cursor
;
3553 // in a single selection control, hide the selection temporarily
3554 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3556 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3558 if ( m_oldSelection
)
3560 m_oldSelection
->SetHilight(false);
3561 RefreshLine(m_oldSelection
);
3568 else if ( event
.Dragging() )
3570 if ( item
!= m_dropTarget
)
3572 // unhighlight the previous drop target
3573 DrawDropEffect(m_dropTarget
);
3575 m_dropTarget
= item
;
3577 // highlight the current drop target if any
3578 DrawDropEffect(m_dropTarget
);
3580 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3587 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3591 // erase the highlighting
3592 DrawDropEffect(m_dropTarget
);
3594 if ( m_oldSelection
)
3596 m_oldSelection
->SetHilight(true);
3597 RefreshLine(m_oldSelection
);
3598 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3601 // generate the drag end event
3602 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3604 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3606 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3608 m_isDragging
= false;
3609 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3611 SetCursor(m_oldCursor
);
3613 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3621 // If we got to this point, we are not dragging or moving the mouse.
3622 // Because the code in carbon/toplevel.cpp will only set focus to the
3623 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3625 // We skip even if we didn't hit an item because we still should
3626 // restore focus to the tree control even if we didn't exactly hit an
3628 if ( event
.LeftDown() )
3633 // here we process only the messages which happen on tree items
3637 if (item
== NULL
) return; /* we hit the blank area */
3639 if ( event
.RightDown() )
3641 // If the item is already selected, do not update the selection.
3642 // Multi-selections should not be cleared if a selected item is
3644 if (!IsSelected(item
))
3646 DoSelectItem(item
, true, false);
3650 nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3651 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3652 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3654 // Consistent with MSW (for now), send the ITEM_MENU *after*
3655 // the RIGHT_CLICK event. TODO: This behavior may change.
3656 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3657 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3658 GetEventHandler()->ProcessEvent(nevent2
);
3660 else if ( event
.MiddleDown() )
3663 nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3664 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3665 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3667 else if ( event
.LeftUp() )
3669 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3672 nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, this, item
);
3673 GetEventHandler()->ProcessEvent(nevent
);
3676 // this facilitates multiple-item drag-and-drop
3678 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3680 wxArrayTreeItemIds selections
;
3681 size_t count
= GetSelections(selections
);
3687 DoSelectItem(item
, true, false);
3693 if ( (item
== m_current
) &&
3694 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3695 HasFlag(wxTR_EDIT_LABELS
) )
3697 if ( m_renameTimer
)
3699 if ( m_renameTimer
->IsRunning() )
3700 m_renameTimer
->Stop();
3704 m_renameTimer
= new wxTreeRenameTimer( this );
3707 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3710 m_lastOnSame
= false;
3713 else // !RightDown() && !MiddleDown() && !LeftUp()
3715 // ==> LeftDown() || LeftDClick()
3716 if ( event
.LeftDown() )
3718 m_lastOnSame
= item
== m_current
;
3721 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3723 // only toggle the item for a single click, double click on
3724 // the button doesn't do anything (it toggles the item twice)
3725 if ( event
.LeftDown() )
3730 // don't select the item if the button was clicked
3735 // clear the previously selected items, if the
3736 // user clicked outside of the present selection.
3737 // otherwise, perform the deselection on mouse-up.
3738 // this allows multiple drag and drop to work.
3739 // but if Cmd is down, toggle selection of the clicked item
3740 if (!IsSelected(item
) || event
.CmdDown())
3742 // how should the selection work for this event?
3743 bool is_multiple
, extended_select
, unselect_others
;
3744 EventFlagsToSelType(GetWindowStyleFlag(),
3751 DoSelectItem(item
, unselect_others
, extended_select
);
3755 // For some reason, Windows isn't recognizing a left double-click,
3756 // so we need to simulate it here. Allow 200 milliseconds for now.
3757 if ( event
.LeftDClick() )
3759 // double clicking should not start editing the item label
3760 if ( m_renameTimer
)
3761 m_renameTimer
->Stop();
3763 m_lastOnSame
= false;
3765 // send activate event first
3767 nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3768 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3769 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3771 // if the user code didn't process the activate event,
3772 // handle it ourselves by toggling the item when it is
3774 if ( item
->HasPlus() )
3784 void wxGenericTreeCtrl::OnInternalIdle()
3786 wxWindow::OnInternalIdle();
3788 // Check if we need to select the root item
3789 // because nothing else has been selected.
3790 // Delaying it means that we can invoke event handlers
3791 // as required, when a first item is selected.
3792 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3795 SelectItem(m_select_me
);
3796 else if (GetRootItem().IsOk())
3797 SelectItem(GetRootItem());
3800 // after all changes have been done to the tree control,
3801 // actually redraw the tree when everything is over
3803 DoDirtyProcessing();
3807 wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem
*item
,
3812 int x
= level
*m_indent
;
3813 if (!HasFlag(wxTR_HIDE_ROOT
))
3817 else if (level
== 0)
3819 // a hidden root is not evaluated, but its
3820 // children are always calculated
3824 item
->CalculateSize(this, dc
);
3827 item
->SetX( x
+m_spacing
);
3829 y
+= GetLineHeight(item
);
3831 if ( !item
->IsExpanded() )
3833 // we don't need to calculate collapsed branches
3838 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3839 size_t n
, count
= children
.GetCount();
3841 for (n
= 0; n
< count
; ++n
)
3842 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3845 void wxGenericTreeCtrl::CalculatePositions()
3847 if ( !m_anchor
) return;
3849 wxClientDC
dc(this);
3852 dc
.SetFont( m_normalFont
);
3854 dc
.SetPen( m_dottedPen
);
3855 //if(GetImageList() == NULL)
3856 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3859 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3862 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3865 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3868 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3870 if (m_dirty
|| IsFrozen() )
3873 wxSize client
= GetClientSize();
3876 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3877 rect
.width
= client
.x
;
3878 rect
.height
= client
.y
;
3880 Refresh(true, &rect
);
3882 AdjustMyScrollbars();
3885 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3887 if (m_dirty
|| IsFrozen() )
3891 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3892 rect
.width
= GetClientSize().x
;
3893 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3895 Refresh(true, &rect
);
3898 void wxGenericTreeCtrl::RefreshSelected()
3903 // TODO: this is awfully inefficient, we should keep the list of all
3904 // selected items internally, should be much faster
3906 RefreshSelectedUnder(m_anchor
);
3909 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3914 if ( item
->IsSelected() )
3917 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3918 size_t count
= children
.GetCount();
3919 for ( size_t n
= 0; n
< count
; n
++ )
3921 RefreshSelectedUnder(children
[n
]);
3925 void wxGenericTreeCtrl::DoThaw()
3927 wxTreeCtrlBase::DoThaw();
3930 DoDirtyProcessing();
3935 // ----------------------------------------------------------------------------
3936 // changing colours: we need to refresh the tree control
3937 // ----------------------------------------------------------------------------
3939 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3941 if ( !wxWindow::SetBackgroundColour(colour
) )
3949 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3951 if ( !wxWindow::SetForegroundColour(colour
) )
3959 // Process the tooltip event, to speed up event processing.
3960 // Doesn't actually get a tooltip.
3961 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3967 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3968 // be removed, as well as the #else case below.
3969 #define _USE_VISATTR 0
3974 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3976 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3980 // Use the same color scheme as wxListBox
3981 return wxListBox::GetClassDefaultAttributes(variant
);
3983 wxVisualAttributes attr
;
3984 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
3985 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3986 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3991 void wxGenericTreeCtrl::DoDirtyProcessing()
3998 CalculatePositions();
4000 AdjustMyScrollbars();
4003 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
4005 // make sure all positions are calculated as normally this only done during
4006 // idle time but we need them for base class DoGetBestSize() to return the
4008 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
4010 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
4012 // there seems to be an implicit extra border around the items, although
4013 // I'm not really sure where does it come from -- but without this, the
4014 // scrollbars appear in a tree with default/best size
4017 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
4018 // scrollbars still appear
4019 const wxSize
& borderSize
= GetWindowBorderSize();
4021 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
4023 size
.x
+= PIXELS_PER_UNIT
- dx
;
4024 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
4026 size
.y
+= PIXELS_PER_UNIT
- dy
;
4028 // we need to update the cache too as the base class cached its own value
4029 CacheBestSize(size
);
4034 #endif // wxUSE_TREECTRL