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_lastOnSame
= false;
991 #if defined( __WXMAC__ )
992 #if wxOSX_USE_ATSU_TEXT
993 m_normalFont
.MacCreateFromThemeFont( kThemeViewsFont
) ;
995 m_normalFont
.MacCreateFromUIFont( kCTFontViewsFontType
) ;
998 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
1000 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1001 m_normalFont
.GetFamily(),
1002 m_normalFont
.GetStyle(),
1004 m_normalFont
.GetUnderlined(),
1005 m_normalFont
.GetFaceName(),
1006 m_normalFont
.GetEncoding());
1009 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
1014 const wxValidator
& validator
,
1015 const wxString
& name
)
1019 wxGetOsVersion(&major
, &minor
);
1022 style
|= wxTR_ROW_LINES
;
1025 if ( !wxControl::Create( parent
, id
, pos
, size
,
1026 style
|wxHSCROLL
|wxVSCROLL
,
1031 // If the tree display has no buttons, but does have
1032 // connecting lines, we can use a narrower layout.
1033 // It may not be a good idea to force this...
1034 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
1040 wxVisualAttributes attr
= GetDefaultAttributes();
1041 SetOwnForegroundColour( attr
.colFg
);
1042 SetOwnBackgroundColour( attr
.colBg
);
1044 SetOwnFont(attr
.font
);
1046 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1047 // style because we apparently get performance problems when using dotted
1048 // pen for drawing in some ports -- but under MSW it seems to work fine
1050 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
1052 m_dottedPen
= *wxGREY_PEN
;
1055 SetInitialSize(size
);
1060 wxGenericTreeCtrl::~wxGenericTreeCtrl()
1062 delete m_hilightBrush
;
1063 delete m_hilightUnfocusedBrush
;
1067 delete m_renameTimer
;
1070 if (m_ownsImageListButtons
)
1071 delete m_imageListButtons
;
1074 // -----------------------------------------------------------------------------
1076 // -----------------------------------------------------------------------------
1078 unsigned int wxGenericTreeCtrl::GetCount() const
1082 // the tree is empty
1086 unsigned int count
= m_anchor
->GetChildrenCount();
1087 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1089 // take the root itself into account
1096 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
1098 m_indent
= (unsigned short) indent
;
1103 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
1104 bool recursively
) const
1106 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
1108 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
1111 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
1113 // Do not try to expand the root node if it hasn't been created yet
1114 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
1116 // if we will hide the root, make sure children are visible
1117 m_anchor
->SetHasPlus();
1119 CalculatePositions();
1122 // right now, just sets the styles. Eventually, we may
1123 // want to update the inherited styles, but right now
1124 // none of the parents has updatable styles
1125 m_windowStyle
= styles
;
1129 // -----------------------------------------------------------------------------
1130 // functions to work with tree items
1131 // -----------------------------------------------------------------------------
1133 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
1135 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
1137 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
1140 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1141 wxTreeItemIcon which
) const
1143 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1145 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
1148 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1150 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1152 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
1155 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
1157 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
1159 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1160 return pItem
->GetState();
1163 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1165 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1167 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1168 return pItem
->Attr().GetTextColour();
1172 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1174 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1176 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1177 return pItem
->Attr().GetBackgroundColour();
1180 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1182 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1184 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1185 return pItem
->Attr().GetFont();
1189 wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1191 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1193 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1194 pItem
->SetText(text
);
1195 pItem
->CalculateSize(this);
1199 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1201 wxTreeItemIcon which
)
1203 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1205 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1206 pItem
->SetImage(image
, which
);
1207 pItem
->CalculateSize(this);
1212 wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1214 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1217 data
->SetId( item
);
1219 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1222 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1224 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1226 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1227 pItem
->SetState(state
);
1228 pItem
->CalculateSize(this);
1232 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1234 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1236 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1237 pItem
->SetHasPlus(has
);
1241 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1243 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1245 // avoid redrawing the tree if no real change
1246 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1247 if ( pItem
->IsBold() != bold
)
1249 pItem
->SetBold(bold
);
1251 // recalculate the item size as bold and non bold fonts have different
1253 pItem
->CalculateSize(this);
1258 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1261 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1267 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1268 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1271 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1272 pItem
->Attr().SetTextColour(fg
);
1273 pItem
->Attr().SetBackgroundColour(bg
);
1277 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1278 const wxColour
& col
)
1280 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1282 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1283 pItem
->Attr().SetTextColour(col
);
1287 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1288 const wxColour
& col
)
1290 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1292 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1293 pItem
->Attr().SetBackgroundColour(col
);
1298 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1300 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1302 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1303 pItem
->Attr().SetFont(font
);
1304 pItem
->ResetTextSize();
1305 pItem
->CalculateSize(this);
1309 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1311 wxTreeCtrlBase::SetFont(font
);
1313 m_normalFont
= font
;
1314 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1315 m_normalFont
.GetFamily(),
1316 m_normalFont
.GetStyle(),
1318 m_normalFont
.GetUnderlined(),
1319 m_normalFont
.GetFaceName(),
1320 m_normalFont
.GetEncoding());
1323 m_anchor
->RecursiveResetTextSize();
1329 // -----------------------------------------------------------------------------
1330 // item status inquiries
1331 // -----------------------------------------------------------------------------
1333 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1335 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1337 // An item is only visible if it's not a descendant of a collapsed item
1338 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1339 wxGenericTreeItem
* parent
= pItem
->GetParent();
1342 if (!parent
->IsExpanded())
1344 parent
= parent
->GetParent();
1348 GetViewStart(& startX
, & startY
);
1350 wxSize clientSize
= GetClientSize();
1353 if (!GetBoundingRect(item
, rect
))
1355 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1357 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1359 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1365 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1367 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1369 // consider that the item does have children if it has the "+" button: it
1370 // might not have them (if it had never been expanded yet) but then it
1371 // could have them as well and it's better to err on this side rather than
1372 // disabling some operations which are restricted to the items with
1373 // children for an item which does have them
1374 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1377 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1379 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1381 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1384 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1386 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1388 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1391 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1393 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1395 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1398 // -----------------------------------------------------------------------------
1400 // -----------------------------------------------------------------------------
1402 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1404 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1406 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1409 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1410 wxTreeItemIdValue
& cookie
) const
1412 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1415 return GetNextChild(item
, cookie
);
1418 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1419 wxTreeItemIdValue
& cookie
) const
1421 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1423 wxArrayGenericTreeItems
&
1424 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1426 // it's ok to cast cookie to size_t, we never have indices big enough to
1427 // overflow "void *"
1428 size_t *pIndex
= (size_t *)&cookie
;
1429 if ( *pIndex
< children
.GetCount() )
1431 return children
.Item((*pIndex
)++);
1435 // there are no more of them
1436 return wxTreeItemId();
1440 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1442 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1444 wxArrayGenericTreeItems
&
1445 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1446 return children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last());
1449 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1451 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1453 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1454 wxGenericTreeItem
*parent
= i
->GetParent();
1455 if ( parent
== NULL
)
1457 // root item doesn't have any siblings
1458 return wxTreeItemId();
1461 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1462 int index
= siblings
.Index(i
);
1463 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1465 size_t n
= (size_t)(index
+ 1);
1466 return n
== siblings
.GetCount() ? wxTreeItemId()
1467 : wxTreeItemId(siblings
[n
]);
1470 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1472 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1474 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1475 wxGenericTreeItem
*parent
= i
->GetParent();
1476 if ( parent
== NULL
)
1478 // root item doesn't have any siblings
1479 return wxTreeItemId();
1482 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1483 int index
= siblings
.Index(i
);
1484 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1486 return index
== 0 ? wxTreeItemId()
1487 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1490 // Only for internal use right now, but should probably be public
1491 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1493 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1495 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1497 // First see if there are any children.
1498 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1499 if (children
.GetCount() > 0)
1501 return children
.Item(0);
1505 // Try a sibling of this or ancestor instead
1506 wxTreeItemId p
= item
;
1507 wxTreeItemId toFind
;
1510 toFind
= GetNextSibling(p
);
1511 p
= GetItemParent(p
);
1512 } while (p
.IsOk() && !toFind
.IsOk());
1517 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1519 wxTreeItemId id
= GetRootItem();
1528 } while (id
.IsOk());
1530 return wxTreeItemId();
1533 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1535 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1536 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1538 wxTreeItemId id
= item
;
1541 while (id
= GetNext(id
), id
.IsOk())
1547 return wxTreeItemId();
1550 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1552 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1553 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1555 // find out the starting point
1556 wxTreeItemId prevItem
= GetPrevSibling(item
);
1557 if ( !prevItem
.IsOk() )
1559 prevItem
= GetItemParent(item
);
1562 // find the first visible item after it
1563 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1565 prevItem
= GetNext(prevItem
);
1566 if ( !prevItem
.IsOk() || prevItem
== item
)
1568 // there are no visible items before item
1569 return wxTreeItemId();
1573 // from there we must be able to navigate until this item
1574 while ( prevItem
.IsOk() )
1576 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1577 if ( !nextItem
.IsOk() || nextItem
== item
)
1580 prevItem
= nextItem
;
1586 // called by wxTextTreeCtrl when it marks itself for deletion
1587 void wxGenericTreeCtrl::ResetTextControl()
1592 // find the first item starting with the given prefix after the given item
1593 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1594 const wxString
& prefixOrig
) const
1596 // match is case insensitive as this is more convenient to the user: having
1597 // to press Shift-letter to go to the item starting with a capital letter
1598 // would be too bothersome
1599 wxString prefix
= prefixOrig
.Lower();
1601 // determine the starting point: we shouldn't take the current item (this
1602 // allows to switch between two items starting with the same letter just by
1603 // pressing it) but we shouldn't jump to the next one if the user is
1604 // continuing to type as otherwise he might easily skip the item he wanted
1605 wxTreeItemId id
= idParent
;
1606 if ( prefix
.length() == 1 )
1611 // look for the item starting with the given prefix after it
1612 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1617 // if we haven't found anything...
1620 // ... wrap to the beginning
1622 if ( HasFlag(wxTR_HIDE_ROOT
) )
1624 // can't select virtual root
1628 // and try all the items (stop when we get to the one we started from)
1629 while ( id
.IsOk() && id
!= idParent
&&
1630 !GetItemText(id
).Lower().StartsWith(prefix
) )
1634 // If we haven't found the item, id.IsOk() will be false, as per
1641 // -----------------------------------------------------------------------------
1643 // -----------------------------------------------------------------------------
1645 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1647 const wxString
& text
,
1650 wxTreeItemData
*data
)
1652 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1655 // should we give a warning here?
1656 return AddRoot(text
, image
, selImage
, data
);
1659 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1661 wxGenericTreeItem
*item
=
1662 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1666 data
->m_pItem
= item
;
1669 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1672 InvalidateBestSize();
1676 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1679 wxTreeItemData
*data
)
1681 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
1683 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1685 m_anchor
= new wxGenericTreeItem((wxGenericTreeItem
*)NULL
, text
,
1686 image
, selImage
, data
);
1689 data
->m_pItem
= m_anchor
;
1692 if (HasFlag(wxTR_HIDE_ROOT
))
1694 // if root is hidden, make sure we can navigate
1696 m_anchor
->SetHasPlus();
1698 CalculatePositions();
1701 if (!HasFlag(wxTR_MULTIPLE
))
1703 m_current
= m_key_current
= m_anchor
;
1704 m_current
->SetHilight( true );
1707 InvalidateBestSize();
1711 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1712 const wxTreeItemId
& idPrevious
,
1713 const wxString
& text
,
1714 int image
, int selImage
,
1715 wxTreeItemData
*data
)
1717 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1720 // should we give a warning here?
1721 return AddRoot(text
, image
, selImage
, data
);
1725 if (idPrevious
.IsOk())
1727 index
= parent
->GetChildren().Index(
1728 (wxGenericTreeItem
*) idPrevious
.m_pItem
);
1729 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1730 "previous item in wxGenericTreeCtrl::InsertItem() "
1731 "is not a sibling" );
1734 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1738 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1740 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1741 GetEventHandler()->ProcessEvent( event
);
1744 // Don't leave edit or selection on a child which is about to disappear
1745 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1747 if ( m_textCtrl
&& item
!= m_textCtrl
->item() &&
1748 IsDescendantOf(item
, m_textCtrl
->item()) )
1750 m_textCtrl
->EndEdit( true );
1753 if ( item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
) )
1755 m_key_current
= NULL
;
1758 if ( IsDescendantOf(item
, m_select_me
) )
1763 if ( item
!= m_current
&& IsDescendantOf(item
, m_current
) )
1765 m_current
->SetHilight( false );
1771 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1773 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1775 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1776 ChildrenClosing(item
);
1777 item
->DeleteChildren(this);
1778 InvalidateBestSize();
1781 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1783 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1785 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1787 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1789 // can't delete the item being edited, cancel editing it first
1790 m_textCtrl
->EndEdit( true );
1793 wxGenericTreeItem
*parent
= item
->GetParent();
1795 // don't keep stale pointers around!
1796 if ( IsDescendantOf(item
, m_key_current
) )
1798 // Don't silently change the selection:
1799 // do it properly in idle time, so event
1800 // handlers get called.
1802 // m_key_current = parent;
1803 m_key_current
= NULL
;
1806 // m_select_me records whether we need to select
1807 // a different item, in idle time.
1808 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1810 m_select_me
= parent
;
1813 if ( IsDescendantOf(item
, m_current
) )
1815 // Don't silently change the selection:
1816 // do it properly in idle time, so event
1817 // handlers get called.
1819 // m_current = parent;
1821 m_select_me
= parent
;
1824 // remove the item from the tree
1827 parent
->GetChildren().Remove( item
); // remove by value
1829 else // deleting the root
1831 // nothing will be left in the tree
1835 // and delete all of its children and the item itself now
1836 item
->DeleteChildren(this);
1837 SendDeleteEvent(item
);
1839 if (item
== m_select_me
)
1844 InvalidateBestSize();
1847 void wxGenericTreeCtrl::DeleteAllItems()
1855 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1857 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1859 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1860 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1861 _T("can't expand hidden root") );
1863 if ( !item
->HasPlus() )
1866 if ( item
->IsExpanded() )
1869 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1871 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1873 // cancelled by program
1880 CalculatePositions();
1882 RefreshSubtree(item
);
1889 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1890 GetEventHandler()->ProcessEvent( event
);
1893 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1895 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1896 _T("can't collapse hidden root") );
1898 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1900 if ( !item
->IsExpanded() )
1903 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1904 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1906 // cancelled by program
1910 ChildrenClosing(item
);
1913 #if 0 // TODO why should items be collapsed recursively?
1914 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1915 size_t count
= children
.GetCount();
1916 for ( size_t n
= 0; n
< count
; n
++ )
1918 Collapse(children
[n
]);
1922 CalculatePositions();
1924 RefreshSubtree(item
);
1926 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1927 GetEventHandler()->ProcessEvent( event
);
1930 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1933 DeleteChildren(item
);
1936 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1938 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1940 if (item
->IsExpanded())
1946 void wxGenericTreeCtrl::Unselect()
1950 m_current
->SetHilight( false );
1951 RefreshLine( m_current
);
1958 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1960 if (item
->IsSelected())
1962 item
->SetHilight(false);
1966 if (item
->HasChildren())
1968 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1969 size_t count
= children
.GetCount();
1970 for ( size_t n
= 0; n
< count
; ++n
)
1972 UnselectAllChildren(children
[n
]);
1977 void wxGenericTreeCtrl::UnselectAll()
1979 wxTreeItemId rootItem
= GetRootItem();
1981 // the tree might not have the root item at all
1984 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1988 // Recursive function !
1989 // To stop we must have crt_item<last_item
1991 // Tag all next children, when no more children,
1992 // Move to parent (not to tag)
1993 // Keep going... if we found last_item, we stop.
1995 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
,
1996 wxGenericTreeItem
*last_item
,
1999 wxGenericTreeItem
*parent
= crt_item
->GetParent();
2001 if (parent
== NULL
) // This is root item
2002 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
2004 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
2005 int index
= children
.Index(crt_item
);
2006 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
2008 size_t count
= children
.GetCount();
2009 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
2011 if ( TagAllChildrenUntilLast(children
[n
], last_item
, select
) )
2015 return TagNextChildren(parent
, last_item
, select
);
2019 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
,
2020 wxGenericTreeItem
*last_item
,
2023 crt_item
->SetHilight(select
);
2024 RefreshLine(crt_item
);
2026 if (crt_item
==last_item
)
2029 if (crt_item
->HasChildren())
2031 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
2032 size_t count
= children
.GetCount();
2033 for ( size_t n
= 0; n
< count
; ++n
)
2035 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
2044 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
,
2045 wxGenericTreeItem
*item2
)
2049 // item2 is not necessary after item1
2050 // choice first' and 'last' between item1 and item2
2051 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
2052 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
2054 bool select
= m_current
->IsSelected();
2056 if ( TagAllChildrenUntilLast(first
,last
,select
) )
2059 TagNextChildren(first
,last
,select
);
2062 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
2063 bool unselect_others
,
2064 bool extended_select
)
2066 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2070 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2071 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2073 //wxCHECK_RET( ( (!unselect_others) && is_single),
2074 // wxT("this is a single selection tree") );
2076 // to keep going anyhow !!!
2079 if (item
->IsSelected())
2080 return; // nothing to do
2081 unselect_others
= true;
2082 extended_select
= false;
2084 else if ( unselect_others
&& item
->IsSelected() )
2086 // selection change if there is more than one item currently selected
2087 wxArrayTreeItemIds selected_items
;
2088 if ( GetSelections(selected_items
) == 1 )
2092 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2093 event
.m_itemOld
= m_current
;
2094 // TODO : Here we don't send any selection mode yet !
2096 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2099 wxTreeItemId parent
= GetItemParent( itemId
);
2100 while (parent
.IsOk())
2102 if (!IsExpanded(parent
))
2105 parent
= GetItemParent( parent
);
2109 if (unselect_others
)
2111 if (is_single
) Unselect(); // to speed up thing
2116 if (extended_select
)
2121 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
2124 // don't change the mark (m_current)
2125 SelectItemRange(m_current
, item
);
2129 bool select
= true; // the default
2131 // Check if we need to toggle hilight (ctrl mode)
2132 if (!unselect_others
)
2133 select
=!item
->IsSelected();
2135 m_current
= m_key_current
= item
;
2136 m_current
->SetHilight(select
);
2137 RefreshLine( m_current
);
2140 // This can cause idle processing to select the root
2141 // if no item is selected, so it must be after the
2143 EnsureVisible( itemId
);
2145 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2146 GetEventHandler()->ProcessEvent( event
);
2149 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
2153 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
2157 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2158 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
2160 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2161 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2164 item
->SetHilight(false);
2167 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2168 GetEventHandler()->ProcessEvent( event
);
2172 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
2173 wxArrayTreeItemIds
&array
) const
2175 if ( item
->IsSelected() )
2176 array
.Add(wxTreeItemId(item
));
2178 if ( item
->HasChildren() )
2180 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2181 size_t count
= children
.GetCount();
2182 for ( size_t n
= 0; n
< count
; ++n
)
2183 FillArray(children
[n
], array
);
2187 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
2190 wxTreeItemId idRoot
= GetRootItem();
2191 if ( idRoot
.IsOk() )
2193 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
2195 //else: the tree is empty, so no selections
2197 return array
.GetCount();
2200 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2202 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2204 if (!item
.IsOk()) return;
2206 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2208 // first expand all parent branches
2209 wxGenericTreeItem
*parent
= gitem
->GetParent();
2211 if ( HasFlag(wxTR_HIDE_ROOT
) )
2213 while ( parent
&& parent
!= m_anchor
)
2216 parent
= parent
->GetParent();
2224 parent
= parent
->GetParent();
2228 //if (parent) CalculatePositions();
2233 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2235 if (!item
.IsOk()) return;
2237 // We have to call this here because the label in
2238 // question might just have been added and no screen
2239 // update taken place.
2241 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2244 DoDirtyProcessing();
2246 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2248 // now scroll to the item
2249 int item_y
= gitem
->GetY();
2253 GetViewStart( &start_x
, &start_y
);
2254 start_y
*= PIXELS_PER_UNIT
;
2258 GetClientSize( &client_w
, &client_h
);
2260 if (item_y
< start_y
+3)
2265 m_anchor
->GetSize( x
, y
, this );
2266 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2267 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2268 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2269 // Item should appear at top
2270 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2271 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2272 x_pos
, item_y
/PIXELS_PER_UNIT
);
2274 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2279 m_anchor
->GetSize( x
, y
, this );
2280 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2281 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2282 item_y
+= PIXELS_PER_UNIT
+2;
2283 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2284 // Item should appear at bottom
2285 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2286 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2288 (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
2292 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2293 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2295 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2296 wxGenericTreeItem
**item2
)
2298 wxCHECK_MSG( s_treeBeingSorted
, 0,
2299 "bug in wxGenericTreeCtrl::SortChildren()" );
2301 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2304 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2306 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2308 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2310 wxCHECK_RET( !s_treeBeingSorted
,
2311 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2313 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2314 if ( children
.GetCount() > 1 )
2318 s_treeBeingSorted
= this;
2319 children
.Sort(tree_ctrl_compare_func
);
2320 s_treeBeingSorted
= NULL
;
2322 //else: don't make the tree dirty as nothing changed
2325 void wxGenericTreeCtrl::CalculateLineHeight()
2327 wxClientDC
dc(this);
2328 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2330 if ( m_imageListNormal
)
2332 // Calculate a m_lineHeight value from the normal Image sizes.
2333 // May be toggle off. Then wxGenericTreeCtrl will spread when
2334 // necessary (which might look ugly).
2335 int n
= m_imageListNormal
->GetImageCount();
2336 for (int i
= 0; i
< n
; i
++)
2338 int width
= 0, height
= 0;
2339 m_imageListNormal
->GetSize(i
, width
, height
);
2340 if (height
> m_lineHeight
) m_lineHeight
= height
;
2344 if ( m_imageListState
)
2346 // Calculate a m_lineHeight value from the state Image sizes.
2347 // May be toggle off. Then wxGenericTreeCtrl will spread when
2348 // necessary (which might look ugly).
2349 int n
= m_imageListState
->GetImageCount();
2350 for (int i
= 0; i
< n
; i
++)
2352 int width
= 0, height
= 0;
2353 m_imageListState
->GetSize(i
, width
, height
);
2354 if (height
> m_lineHeight
) m_lineHeight
= height
;
2358 if (m_imageListButtons
)
2360 // Calculate a m_lineHeight value from the Button image sizes.
2361 // May be toggle off. Then wxGenericTreeCtrl will spread when
2362 // necessary (which might look ugly).
2363 int n
= m_imageListButtons
->GetImageCount();
2364 for (int i
= 0; i
< n
; i
++)
2366 int width
= 0, height
= 0;
2367 m_imageListButtons
->GetSize(i
, width
, height
);
2368 if (height
> m_lineHeight
) m_lineHeight
= height
;
2372 if (m_lineHeight
< 30)
2373 m_lineHeight
+= 2; // at least 2 pixels
2375 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2378 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2380 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2381 m_imageListNormal
= imageList
;
2382 m_ownsImageListNormal
= false;
2386 m_anchor
->RecursiveResetSize();
2388 // Don't do any drawing if we're setting the list to NULL,
2389 // since we may be in the process of deleting the tree control.
2391 CalculateLineHeight();
2394 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2396 if (m_ownsImageListState
) delete m_imageListState
;
2397 m_imageListState
= imageList
;
2398 m_ownsImageListState
= false;
2402 m_anchor
->RecursiveResetSize();
2404 // Don't do any drawing if we're setting the list to NULL,
2405 // since we may be in the process of deleting the tree control.
2407 CalculateLineHeight();
2410 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2412 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2413 m_imageListButtons
= imageList
;
2414 m_ownsImageListButtons
= false;
2418 m_anchor
->RecursiveResetSize();
2420 CalculateLineHeight();
2423 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2425 SetButtonsImageList(imageList
);
2426 m_ownsImageListButtons
= true;
2429 // -----------------------------------------------------------------------------
2431 // -----------------------------------------------------------------------------
2433 void wxGenericTreeCtrl::AdjustMyScrollbars()
2438 m_anchor
->GetSize( x
, y
, this );
2439 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2440 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2441 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2442 int y_pos
= GetScrollPos( wxVERTICAL
);
2443 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2444 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2449 SetScrollbars( 0, 0, 0, 0 );
2453 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2455 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2456 return item
->GetHeight();
2458 return m_lineHeight
;
2461 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2463 item
->SetFont(this, dc
);
2464 item
->CalculateSize(this, dc
);
2466 wxCoord text_h
= item
->GetTextHeight();
2468 int image_h
= 0, image_w
= 0;
2469 int image
= item
->GetCurrentImage();
2470 if ( image
!= NO_IMAGE
)
2472 if ( m_imageListNormal
)
2474 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
2475 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2483 int state_h
= 0, state_w
= 0;
2484 int state
= item
->GetState();
2485 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2487 if ( m_imageListState
)
2489 m_imageListState
->GetSize( state
, state_w
, state_h
);
2490 if ( image
!= NO_IMAGE
)
2491 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2493 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2497 state
= wxTREE_ITEMSTATE_NONE
;
2501 int total_h
= GetLineHeight(item
);
2502 bool drawItemBackground
= false,
2503 hasBgColour
= false;
2505 if ( item
->IsSelected() )
2507 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2508 drawItemBackground
= true;
2513 wxTreeItemAttr
* const attr
= item
->GetAttributes();
2514 if ( attr
&& attr
->HasBackgroundColour() )
2516 drawItemBackground
=
2518 colBg
= attr
->GetBackgroundColour();
2522 colBg
= GetBackgroundColour();
2524 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2527 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2529 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2533 GetVirtualSize(&w
, &h
);
2534 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2535 if (!item
->IsSelected())
2537 dc
.DrawRectangle(rect
);
2541 int flags
= wxCONTROL_SELECTED
;
2543 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2544 && IsControlActive( (ControlRef
)GetHandle() )
2547 flags
|= wxCONTROL_FOCUSED
;
2548 if ((item
== m_current
) && (m_hasFocus
))
2549 flags
|= wxCONTROL_CURRENT
;
2551 wxRendererNative::Get().
2552 DrawItemSelectionRect(this, dc
, rect
, flags
);
2555 else // no full row highlight
2557 if ( item
->IsSelected() &&
2558 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2560 // If it's selected, and there's an state image or normal image,
2561 // then we should take care to leave the area under the image
2562 // painted in the background colour.
2563 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2564 item
->GetY() + offset
,
2565 item
->GetWidth() - state_w
- image_w
+ 2,
2567 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2568 dc
.DrawRectangle( rect
);
2573 int flags
= wxCONTROL_SELECTED
;
2575 flags
|= wxCONTROL_FOCUSED
;
2576 if ((item
== m_current
) && (m_hasFocus
))
2577 flags
|= wxCONTROL_CURRENT
;
2578 wxRendererNative::Get().
2579 DrawItemSelectionRect(this, dc
, rect
, flags
);
2582 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2583 // don't allow backgrounds to be customized. Not drawing the background,
2584 // except for custom item backgrounds, works for both kinds of theme.
2585 else if (drawItemBackground
)
2587 wxRect
rect( item
->GetX()-2, item
->GetY()+offset
,
2588 item
->GetWidth()+2, total_h
-offset
);
2591 dc
.DrawRectangle( rect
);
2593 else // no specific background colour
2598 int flags
= wxCONTROL_SELECTED
;
2600 flags
|= wxCONTROL_FOCUSED
;
2601 if ((item
== m_current
) && (m_hasFocus
))
2602 flags
|= wxCONTROL_CURRENT
;
2603 wxRendererNative::Get().
2604 DrawItemSelectionRect(this, dc
, rect
, flags
);
2609 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2611 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2612 m_imageListState
->Draw( state
, dc
,
2615 (total_h
> state_h
? (total_h
-state_h
)/2
2617 wxIMAGELIST_DRAW_TRANSPARENT
);
2618 dc
.DestroyClippingRegion();
2621 if ( image
!= NO_IMAGE
)
2623 dc
.SetClippingRegion(item
->GetX() + state_w
, item
->GetY(),
2625 m_imageListNormal
->Draw( image
, dc
,
2626 item
->GetX() + state_w
,
2628 (total_h
> image_h
? (total_h
-image_h
)/2
2630 wxIMAGELIST_DRAW_TRANSPARENT
);
2631 dc
.DestroyClippingRegion();
2634 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2635 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2636 dc
.DrawText( item
->GetText(),
2637 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2638 (wxCoord
)(item
->GetY() + extraH
));
2640 // restore normal font
2641 dc
.SetFont( m_normalFont
);
2645 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem
*item
,
2650 int x
= level
*m_indent
;
2651 if (!HasFlag(wxTR_HIDE_ROOT
))
2655 else if (level
== 0)
2657 // always expand hidden root
2659 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2660 int count
= children
.GetCount();
2666 PaintLevel(children
[n
], dc
, 1, y
);
2667 } while (++n
< count
);
2669 if ( !HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
)
2672 // draw line down to last child
2673 origY
+= GetLineHeight(children
[0])>>1;
2674 oldY
+= GetLineHeight(children
[n
-1])>>1;
2675 dc
.DrawLine(3, origY
, 3, oldY
);
2681 item
->SetX(x
+m_spacing
);
2684 int h
= GetLineHeight(item
);
2686 int y_mid
= y_top
+ (h
>>1);
2689 int exposed_x
= dc
.LogicalToDeviceX(0);
2690 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2692 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2696 // don't draw rect outline if we already have the
2697 // background color under Mac
2698 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2699 #endif // !__WXMAC__
2703 if ( item
->IsSelected()
2704 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2705 // On wxMac, if the tree doesn't have the focus we draw an empty
2706 // rectangle, so we want to make sure that the text is visible
2707 // against the normal background, not the highlightbackground, so
2708 // don't use the highlight text colour unless we have the focus.
2709 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2716 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2721 wxTreeItemAttr
*attr
= item
->GetAttributes();
2722 if (attr
&& attr
->HasTextColour())
2723 colText
= attr
->GetTextColour();
2725 colText
= GetForegroundColour();
2729 dc
.SetTextForeground(colText
);
2733 PaintItem(item
, dc
);
2735 if (HasFlag(wxTR_ROW_LINES
))
2737 // if the background colour is white, choose a
2738 // contrasting color for the lines
2739 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2740 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2741 dc
.DrawLine(0, y_top
, 10000, y_top
);
2742 dc
.DrawLine(0, y
, 10000, y
);
2745 // restore DC objects
2746 dc
.SetBrush(*wxWHITE_BRUSH
);
2747 dc
.SetPen(m_dottedPen
);
2748 dc
.SetTextForeground(*wxBLACK
);
2750 if ( !HasFlag(wxTR_NO_LINES
) )
2752 // draw the horizontal line here
2754 if (x
> (signed)m_indent
)
2755 x_start
-= m_indent
;
2756 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2758 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2761 // should the item show a button?
2762 if ( item
->HasPlus() && HasButtons() )
2764 if ( m_imageListButtons
)
2766 // draw the image button here
2769 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2770 : wxTreeItemIcon_Normal
;
2771 if ( item
->IsSelected() )
2772 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2774 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2775 int xx
= x
- image_w
/2;
2776 int yy
= y_mid
- image_h
/2;
2778 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2779 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2780 wxIMAGELIST_DRAW_TRANSPARENT
);
2782 else // no custom buttons
2784 static const int wImage
= 9;
2785 static const int hImage
= 9;
2788 if (item
->IsExpanded())
2789 flag
|= wxCONTROL_EXPANDED
;
2790 if (item
== m_underMouse
)
2791 flag
|= wxCONTROL_CURRENT
;
2793 wxRendererNative::Get().DrawTreeItemButton
2797 wxRect(x
- wImage
/2,
2806 if (item
->IsExpanded())
2808 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2809 int count
= children
.GetCount();
2816 PaintLevel(children
[n
], dc
, level
, y
);
2817 } while (++n
< count
);
2819 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2821 // draw line down to last child
2822 oldY
+= GetLineHeight(children
[n
-1])>>1;
2823 if (HasButtons()) y_mid
+= 5;
2825 // Only draw the portion of the line that is visible, in case
2827 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2828 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2829 yOrigin
= abs(yOrigin
);
2830 GetClientSize(&width
, &height
);
2832 // Move end points to the begining/end of the view?
2833 if (y_mid
< yOrigin
)
2835 if (oldY
> yOrigin
+ height
)
2836 oldY
= yOrigin
+ height
;
2838 // after the adjustments if y_mid is larger than oldY then the
2839 // line isn't visible at all so don't draw anything
2841 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2847 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2851 if ( item
->HasPlus() )
2853 // it's a folder, indicate it by a border
2858 // draw a line under the drop target because the item will be
2860 DrawLine(item
, !m_dropEffectAboveItem
);
2863 SetCursor(wxCURSOR_BULLSEYE
);
2868 SetCursor(wxCURSOR_NO_ENTRY
);
2872 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2874 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2876 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2878 wxClientDC
dc(this);
2880 dc
.SetLogicalFunction(wxINVERT
);
2881 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2883 int w
= i
->GetWidth() + 2;
2884 int h
= GetLineHeight(i
) + 2;
2886 dc
.DrawRectangle( i
->GetX() - 1, i
->GetY() - 1, w
, h
);
2889 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2891 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2893 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2895 wxClientDC
dc(this);
2897 dc
.SetLogicalFunction(wxINVERT
);
2903 y
+= GetLineHeight(i
) - 1;
2906 dc
.DrawLine( x
, y
, x
+ i
->GetWidth(), y
);
2909 // -----------------------------------------------------------------------------
2910 // wxWidgets callbacks
2911 // -----------------------------------------------------------------------------
2913 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2916 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2917 RefreshLine( m_current
);
2923 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2931 dc
.SetFont( m_normalFont
);
2932 dc
.SetPen( m_dottedPen
);
2934 // this is now done dynamically
2935 //if(GetImageList() == NULL)
2936 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2939 PaintLevel( m_anchor
, dc
, 0, y
);
2942 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2951 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2960 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2962 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2963 te
.m_evtKey
= event
;
2964 if ( GetEventHandler()->ProcessEvent( te
) )
2966 // intercepted by the user code
2970 if ( (m_current
== 0) || (m_key_current
== 0) )
2976 // how should the selection work for this event?
2977 bool is_multiple
, extended_select
, unselect_others
;
2978 EventFlagsToSelType(GetWindowStyleFlag(),
2981 is_multiple
, extended_select
, unselect_others
);
2983 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2985 if (event
.GetKeyCode() == WXK_RIGHT
)
2986 event
.m_keyCode
= WXK_LEFT
;
2987 else if (event
.GetKeyCode() == WXK_LEFT
)
2988 event
.m_keyCode
= WXK_RIGHT
;
2993 // * : Expand all/Collapse all
2994 // ' ' | return : activate
2995 // up : go up (not last children!)
2997 // left : go to parent
2998 // right : open if parent and go next
2999 // home : go to root
3000 // end : go to last item without opening parents
3001 // alnum : start or continue searching for the item with this prefix
3002 int keyCode
= event
.GetKeyCode();
3007 if (m_current
->HasPlus() && !IsExpanded(m_current
))
3015 if ( !IsExpanded(m_current
) )
3018 ExpandAllChildren(m_current
);
3021 //else: fall through to Collapse() it
3025 if (IsExpanded(m_current
))
3027 Collapse(m_current
);
3033 // Use the item's bounding rectangle to determine position for
3036 GetBoundingRect(m_current
, ItemRect
, true);
3039 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
3040 // Use the left edge, vertical middle
3041 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
3043 ItemRect
.GetHeight() / 2);
3044 GetEventHandler()->ProcessEvent( eventMenu
);
3050 if ( !event
.HasModifiers() )
3053 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
3054 GetEventHandler()->ProcessEvent( eventAct
);
3057 // in any case, also generate the normal key event for this key,
3058 // even if we generated the ACTIVATED event above: this is what
3059 // wxMSW does and it makes sense because you might not want to
3060 // process ACTIVATED event at all and handle Space and Return
3061 // directly (and differently) which would be impossible otherwise
3065 // up goes to the previous sibling or to the last
3066 // of its children if it's expanded
3069 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
3072 prev
= GetItemParent( m_key_current
);
3073 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3075 break; // don't go to root if it is hidden
3079 wxTreeItemIdValue cookie
;
3080 wxTreeItemId current
= m_key_current
;
3081 // TODO: Huh? If we get here, we'd better be the first
3082 // child of our parent. How else could it be?
3083 if (current
== GetFirstChild( prev
, cookie
))
3085 // otherwise we return to where we came from
3089 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
3096 while ( IsExpanded(prev
) && HasChildren(prev
) )
3098 wxTreeItemId child
= GetLastChild(prev
);
3105 DoSelectItem( prev
, unselect_others
, extended_select
);
3106 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
3111 // left arrow goes to the parent
3114 wxTreeItemId prev
= GetItemParent( m_current
);
3115 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3117 // don't go to root if it is hidden
3118 prev
= GetPrevSibling( m_current
);
3122 DoSelectItem( prev
, unselect_others
, extended_select
);
3128 // this works the same as the down arrow except that we
3129 // also expand the item if it wasn't expanded yet
3130 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
3132 //else: don't try to expand hidden root item (which can be the
3133 // current one when the tree is empty)
3139 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
3141 wxTreeItemIdValue cookie
;
3142 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
3146 DoSelectItem( child
, unselect_others
, extended_select
);
3147 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
3151 wxTreeItemId next
= GetNextSibling( m_key_current
);
3154 wxTreeItemId current
= m_key_current
;
3155 while (current
.IsOk() && !next
)
3157 current
= GetItemParent( current
);
3158 if (current
) next
= GetNextSibling( current
);
3163 DoSelectItem( next
, unselect_others
, extended_select
);
3164 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
3170 // <End> selects the last visible tree item
3173 wxTreeItemId last
= GetRootItem();
3175 while ( last
.IsOk() && IsExpanded(last
) )
3177 wxTreeItemId lastChild
= GetLastChild(last
);
3179 // it may happen if the item was expanded but then all of
3180 // its children have been deleted - so IsExpanded() returned
3181 // true, but GetLastChild() returned invalid item
3190 DoSelectItem( last
, unselect_others
, extended_select
);
3195 // <Home> selects the root item
3198 wxTreeItemId prev
= GetRootItem();
3202 if ( HasFlag(wxTR_HIDE_ROOT
) )
3204 wxTreeItemIdValue cookie
;
3205 prev
= GetFirstChild(prev
, cookie
);
3210 DoSelectItem( prev
, unselect_others
, extended_select
);
3215 // do not use wxIsalnum() here
3216 if ( !event
.HasModifiers() &&
3217 ((keyCode
>= '0' && keyCode
<= '9') ||
3218 (keyCode
>= 'a' && keyCode
<= 'z') ||
3219 (keyCode
>= 'A' && keyCode
<= 'Z' )))
3221 // find the next item starting with the given prefix
3222 wxChar ch
= (wxChar
)keyCode
;
3224 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
3235 // also start the timer to reset the current prefix if the user
3236 // doesn't press any more alnum keys soon -- we wouldn't want
3237 // to use this prefix for a new item search
3240 m_findTimer
= new wxTreeFindTimer(this);
3243 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
3253 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
3258 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3259 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3260 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3261 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3262 if (flags
) return wxTreeItemId();
3264 if (m_anchor
== NULL
)
3266 flags
= wxTREE_HITTEST_NOWHERE
;
3267 return wxTreeItemId();
3270 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3274 flags
= wxTREE_HITTEST_NOWHERE
;
3275 return wxTreeItemId();
3280 // get the bounding rectangle of the item (or of its label only)
3281 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3283 bool textOnly
) const
3285 wxCHECK_MSG( item
.IsOk(), false,
3286 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3288 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3293 rect
.width
= i
->GetWidth();
3295 if ( m_imageListNormal
)
3297 int image_w
, image_h
;
3298 m_imageListNormal
->GetSize( 0, image_w
, image_h
);
3299 rect
.width
+= image_w
+ MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3302 else // the entire line
3305 rect
.width
= GetClientSize().x
;
3309 rect
.height
= GetLineHeight(i
);
3311 // we have to return the logical coordinates, not physical ones
3312 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3317 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3318 wxClassInfo
* WXUNUSED(textCtrlClass
))
3320 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
3322 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3324 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3325 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3331 // We have to call this here because the label in
3332 // question might just have been added and no screen
3333 // update taken place.
3335 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3338 DoDirtyProcessing();
3341 // TODO: use textCtrlClass here to create the control of correct class
3342 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3344 m_textCtrl
->SetFocus();
3349 // returns a pointer to the text edit control if the item is being
3350 // edited, NULL otherwise (it's assumed that no more than one item may
3351 // be edited simultaneously)
3352 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3357 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3358 bool discardChanges
)
3360 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3362 m_textCtrl
->EndEdit(discardChanges
);
3365 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3366 const wxString
& value
)
3368 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3370 le
.m_editCancelled
= false;
3372 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3375 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3377 // let owner know that the edit was cancelled
3378 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3379 le
.m_label
= wxEmptyString
;
3380 le
.m_editCancelled
= true;
3382 GetEventHandler()->ProcessEvent( le
);
3385 void wxGenericTreeCtrl::OnRenameTimer()
3387 EditLabel( m_current
);
3390 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3392 if ( !m_anchor
)return;
3394 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3396 // Is the mouse over a tree item button?
3398 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3399 wxGenericTreeItem
*underMouse
= thisItem
;
3401 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3402 #endif // wxUSE_TOOLTIPS
3405 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3406 (!event
.LeftIsDown()) &&
3408 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3416 if (underMouse
!= m_underMouse
)
3420 // unhighlight old item
3421 wxGenericTreeItem
*tmp
= m_underMouse
;
3422 m_underMouse
= NULL
;
3426 m_underMouse
= underMouse
;
3428 RefreshLine( m_underMouse
);
3432 // Determines what item we are hovering over and need a tooltip for
3433 wxTreeItemId hoverItem
= thisItem
;
3435 // We do not want a tooltip if we are dragging, or if the rename timer is
3437 if ( underMouseChanged
&&
3440 (!m_renameTimer
|| !m_renameTimer
->IsRunning()) )
3442 // Ask the tree control what tooltip (if any) should be shown
3444 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3446 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3448 SetToolTip(hevent
.m_label
);
3453 // we process left mouse up event (enables in-place edit), middle/right down
3454 // (pass to the user code), left dbl click (activate item) and
3455 // dragging/moving events for items drag-and-drop
3456 if ( !(event
.LeftDown() ||
3458 event
.MiddleDown() ||
3459 event
.RightDown() ||
3460 event
.LeftDClick() ||
3462 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3471 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3473 if ( event
.Dragging() && !m_isDragging
)
3475 if (m_dragCount
== 0)
3480 if (m_dragCount
!= 3)
3482 // wait until user drags a bit further...
3486 wxEventType command
= event
.RightIsDown()
3487 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3488 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3490 wxTreeEvent
nevent(command
, this, m_current
);
3491 nevent
.SetPoint(CalcScrolledPosition(pt
));
3493 // by default the dragging is not supported, the user code must
3494 // explicitly allow the event for it to take place
3497 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3499 // we're going to drag this item
3500 m_isDragging
= true;
3502 // remember the old cursor because we will change it while
3504 m_oldCursor
= m_cursor
;
3506 // in a single selection control, hide the selection temporarily
3507 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3509 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3511 if ( m_oldSelection
)
3513 m_oldSelection
->SetHilight(false);
3514 RefreshLine(m_oldSelection
);
3521 else if ( event
.Dragging() )
3523 if ( item
!= m_dropTarget
)
3525 // unhighlight the previous drop target
3526 DrawDropEffect(m_dropTarget
);
3528 m_dropTarget
= item
;
3530 // highlight the current drop target if any
3531 DrawDropEffect(m_dropTarget
);
3533 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3540 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3544 // erase the highlighting
3545 DrawDropEffect(m_dropTarget
);
3547 if ( m_oldSelection
)
3549 m_oldSelection
->SetHilight(true);
3550 RefreshLine(m_oldSelection
);
3551 m_oldSelection
= (wxGenericTreeItem
*)NULL
;
3554 // generate the drag end event
3555 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3557 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3559 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3561 m_isDragging
= false;
3562 m_dropTarget
= (wxGenericTreeItem
*)NULL
;
3564 SetCursor(m_oldCursor
);
3566 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3574 // If we got to this point, we are not dragging or moving the mouse.
3575 // Because the code in carbon/toplevel.cpp will only set focus to the
3576 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3578 // We skip even if we didn't hit an item because we still should
3579 // restore focus to the tree control even if we didn't exactly hit an
3581 if ( event
.LeftDown() )
3586 // here we process only the messages which happen on tree items
3590 if (item
== NULL
) return; /* we hit the blank area */
3592 if ( event
.RightDown() )
3594 // If the item is already selected, do not update the selection.
3595 // Multi-selections should not be cleared if a selected item is
3597 if (!IsSelected(item
))
3599 DoSelectItem(item
, true, false);
3603 nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3604 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3605 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3607 // Consistent with MSW (for now), send the ITEM_MENU *after*
3608 // the RIGHT_CLICK event. TODO: This behavior may change.
3609 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3610 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3611 GetEventHandler()->ProcessEvent(nevent2
);
3613 else if ( event
.MiddleDown() )
3616 nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3617 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3618 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3620 else if ( event
.LeftUp() )
3622 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3625 nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, this, item
);
3626 GetEventHandler()->ProcessEvent(nevent
);
3629 // this facilitates multiple-item drag-and-drop
3631 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3633 wxArrayTreeItemIds selections
;
3634 size_t count
= GetSelections(selections
);
3640 DoSelectItem(item
, true, false);
3646 if ( (item
== m_current
) &&
3647 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3648 HasFlag(wxTR_EDIT_LABELS
) )
3650 if ( m_renameTimer
)
3652 if ( m_renameTimer
->IsRunning() )
3653 m_renameTimer
->Stop();
3657 m_renameTimer
= new wxTreeRenameTimer( this );
3660 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3663 m_lastOnSame
= false;
3666 else // !RightDown() && !MiddleDown() && !LeftUp()
3668 // ==> LeftDown() || LeftDClick()
3669 if ( event
.LeftDown() )
3671 m_lastOnSame
= item
== m_current
;
3674 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3676 // only toggle the item for a single click, double click on
3677 // the button doesn't do anything (it toggles the item twice)
3678 if ( event
.LeftDown() )
3683 // don't select the item if the button was clicked
3688 // clear the previously selected items, if the
3689 // user clicked outside of the present selection.
3690 // otherwise, perform the deselection on mouse-up.
3691 // this allows multiple drag and drop to work.
3692 // but if Cmd is down, toggle selection of the clicked item
3693 if (!IsSelected(item
) || event
.CmdDown())
3695 // how should the selection work for this event?
3696 bool is_multiple
, extended_select
, unselect_others
;
3697 EventFlagsToSelType(GetWindowStyleFlag(),
3704 DoSelectItem(item
, unselect_others
, extended_select
);
3708 // For some reason, Windows isn't recognizing a left double-click,
3709 // so we need to simulate it here. Allow 200 milliseconds for now.
3710 if ( event
.LeftDClick() )
3712 // double clicking should not start editing the item label
3713 if ( m_renameTimer
)
3714 m_renameTimer
->Stop();
3716 m_lastOnSame
= false;
3718 // send activate event first
3720 nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3721 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3722 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3724 // if the user code didn't process the activate event,
3725 // handle it ourselves by toggling the item when it is
3727 if ( item
->HasPlus() )
3737 void wxGenericTreeCtrl::OnInternalIdle()
3739 wxWindow::OnInternalIdle();
3741 // Check if we need to select the root item
3742 // because nothing else has been selected.
3743 // Delaying it means that we can invoke event handlers
3744 // as required, when a first item is selected.
3745 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3748 SelectItem(m_select_me
);
3749 else if (GetRootItem().IsOk())
3750 SelectItem(GetRootItem());
3753 // after all changes have been done to the tree control,
3754 // actually redraw the tree when everything is over
3756 DoDirtyProcessing();
3760 wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem
*item
,
3765 int x
= level
*m_indent
;
3766 if (!HasFlag(wxTR_HIDE_ROOT
))
3770 else if (level
== 0)
3772 // a hidden root is not evaluated, but its
3773 // children are always calculated
3777 item
->CalculateSize(this, dc
);
3780 item
->SetX( x
+m_spacing
);
3782 y
+= GetLineHeight(item
);
3784 if ( !item
->IsExpanded() )
3786 // we don't need to calculate collapsed branches
3791 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3792 size_t n
, count
= children
.GetCount();
3794 for (n
= 0; n
< count
; ++n
)
3795 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3798 void wxGenericTreeCtrl::CalculatePositions()
3800 if ( !m_anchor
) return;
3802 wxClientDC
dc(this);
3805 dc
.SetFont( m_normalFont
);
3807 dc
.SetPen( m_dottedPen
);
3808 //if(GetImageList() == NULL)
3809 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3812 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3815 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3818 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3821 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3823 if (m_dirty
|| IsFrozen() )
3826 wxSize client
= GetClientSize();
3829 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3830 rect
.width
= client
.x
;
3831 rect
.height
= client
.y
;
3833 Refresh(true, &rect
);
3835 AdjustMyScrollbars();
3838 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3840 if (m_dirty
|| IsFrozen() )
3844 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3845 rect
.width
= GetClientSize().x
;
3846 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3848 Refresh(true, &rect
);
3851 void wxGenericTreeCtrl::RefreshSelected()
3856 // TODO: this is awfully inefficient, we should keep the list of all
3857 // selected items internally, should be much faster
3859 RefreshSelectedUnder(m_anchor
);
3862 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3867 if ( item
->IsSelected() )
3870 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3871 size_t count
= children
.GetCount();
3872 for ( size_t n
= 0; n
< count
; n
++ )
3874 RefreshSelectedUnder(children
[n
]);
3878 void wxGenericTreeCtrl::DoThaw()
3880 wxTreeCtrlBase::DoThaw();
3883 DoDirtyProcessing();
3888 // ----------------------------------------------------------------------------
3889 // changing colours: we need to refresh the tree control
3890 // ----------------------------------------------------------------------------
3892 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3894 if ( !wxWindow::SetBackgroundColour(colour
) )
3902 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3904 if ( !wxWindow::SetForegroundColour(colour
) )
3912 // Process the tooltip event, to speed up event processing.
3913 // Doesn't actually get a tooltip.
3914 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3920 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3921 // be removed, as well as the #else case below.
3922 #define _USE_VISATTR 0
3927 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3929 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3933 // Use the same color scheme as wxListBox
3934 return wxListBox::GetClassDefaultAttributes(variant
);
3936 wxVisualAttributes attr
;
3937 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
3938 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
3939 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
3944 void wxGenericTreeCtrl::DoDirtyProcessing()
3951 CalculatePositions();
3953 AdjustMyScrollbars();
3956 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
3958 // make sure all positions are calculated as normally this only done during
3959 // idle time but we need them for base class DoGetBestSize() to return the
3961 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
3963 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
3965 // there seems to be an implicit extra border around the items, although
3966 // I'm not really sure where does it come from -- but without this, the
3967 // scrollbars appear in a tree with default/best size
3970 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
3971 // scrollbars still appear
3972 const wxSize
& borderSize
= GetWindowBorderSize();
3974 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
3976 size
.x
+= PIXELS_PER_UNIT
- dx
;
3977 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
3979 size
.y
+= PIXELS_PER_UNIT
- dy
;
3981 // we need to update the cache too as the base class cached its own value
3982 CacheBestSize(size
);
3987 #endif // wxUSE_TREECTRL