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 wxDECLARE_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 wxDECLARE_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 wxDECLARE_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 wxDECLARE_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;
433 m_owner
->GetBoundingRect(m_itemEdited
, rect
, true);
435 // corrects position and size for better appearance
439 #elif defined(__WXGTK__)
444 #elif defined(__WXMAC__)
445 int bestHeight
= GetBestSize().y
- 8;
446 if ( rect
.height
> bestHeight
)
448 int diff
= rect
.height
- bestHeight
;
454 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
455 rect
.GetPosition(), rect
.GetSize());
457 SetSelection(-1, -1);
460 void wxTreeTextCtrl::EndEdit(bool discardChanges
)
462 m_aboutToFinish
= true;
464 if ( discardChanges
)
466 m_owner
->OnRenameCancelled(m_itemEdited
);
472 // Notify the owner about the changes
475 // Even if vetoed, close the control (consistent with MSW)
480 bool wxTreeTextCtrl::AcceptChanges()
482 const wxString value
= GetValue();
484 if ( value
== m_startValue
)
486 // nothing changed, always accept
487 // when an item remains unchanged, the owner
488 // needs to be notified that the user decided
489 // not to change the tree item label, and that
490 // the edit has been cancelled
492 m_owner
->OnRenameCancelled(m_itemEdited
);
496 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
498 // vetoed by the user
502 // accepted, do rename the item
503 m_owner
->SetItemText(m_itemEdited
, value
);
508 void wxTreeTextCtrl::Finish( bool setfocus
)
510 m_owner
->ResetTextControl();
512 wxPendingDelete
.Append(this);
518 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
520 switch ( event
.m_keyCode
)
535 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
537 if ( !m_aboutToFinish
)
539 // auto-grow the textctrl:
540 wxSize parentSize
= m_owner
->GetSize();
541 wxPoint myPos
= GetPosition();
542 wxSize mySize
= GetSize();
544 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
545 if (myPos
.x
+ sx
> parentSize
.x
)
546 sx
= parentSize
.x
- myPos
.x
;
549 SetSize(sx
, wxDefaultCoord
);
555 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
557 if ( !m_aboutToFinish
)
559 if ( !AcceptChanges() )
560 m_owner
->OnRenameCancelled( m_itemEdited
);
565 // We should let the native text control handle focus, too.
569 // -----------------------------------------------------------------------------
571 // -----------------------------------------------------------------------------
573 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
574 const wxString
& text
,
575 int image
, int selImage
,
576 wxTreeItemData
*data
)
579 m_images
[wxTreeItemIcon_Normal
] = image
;
580 m_images
[wxTreeItemIcon_Selected
] = selImage
;
581 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
582 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
585 m_state
= wxTREE_ITEMSTATE_NONE
;
588 m_isCollapsed
= true;
589 m_hasHilight
= false;
598 // We don't know the height here yet.
606 wxGenericTreeItem::~wxGenericTreeItem()
610 if (m_ownsAttr
) delete m_attr
;
612 wxASSERT_MSG( m_children
.IsEmpty(),
613 "must call DeleteChildren() before deleting the item" );
616 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
618 size_t count
= m_children
.GetCount();
619 for ( size_t n
= 0; n
< count
; n
++ )
621 wxGenericTreeItem
*child
= m_children
[n
];
622 tree
->SendDeleteEvent(child
);
624 child
->DeleteChildren(tree
);
625 if ( child
== tree
->m_select_me
)
626 tree
->m_select_me
= NULL
;
633 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
635 size_t count
= m_children
.GetCount();
639 size_t total
= count
;
640 for (size_t n
= 0; n
< count
; ++n
)
642 total
+= m_children
[n
]->GetChildrenCount();
648 void wxGenericTreeItem::GetSize( int &x
, int &y
,
649 const wxGenericTreeCtrl
*theButton
)
651 int bottomY
=m_y
+theButton
->GetLineHeight(this);
652 if ( y
< bottomY
) y
= bottomY
;
653 int width
= m_x
+ m_width
;
654 if ( x
< width
) x
= width
;
658 size_t count
= m_children
.GetCount();
659 for ( size_t n
= 0; n
< count
; ++n
)
661 m_children
[n
]->GetSize( x
, y
, theButton
);
666 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
667 const wxGenericTreeCtrl
*theCtrl
,
671 // for a hidden root node, don't evaluate it, but do evaluate children
672 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
675 int h
= theCtrl
->GetLineHeight(this);
676 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
678 int y_mid
= m_y
+ h
/2;
679 if (point
.y
< y_mid
)
680 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
682 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
684 int xCross
= m_x
- theCtrl
->GetSpacing();
686 // according to the drawing code the triangels are drawn
687 // at -4 , -4 from the position up to +10/+10 max
688 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
689 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
690 HasPlus() && theCtrl
->HasButtons() )
692 // 5 is the size of the plus sign
693 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
694 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
695 HasPlus() && theCtrl
->HasButtons() )
698 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
702 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
707 // assuming every image (normal and selected) has the same size!
708 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
710 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
717 if ( (GetState() != wxTREE_ITEMSTATE_NONE
) &&
718 theCtrl
->m_imageListState
)
720 theCtrl
->m_imageListState
->GetSize(GetState(),
724 if ((state_w
!= -1) && (point
.x
<= m_x
+ state_w
+ 1))
725 flags
|= wxTREE_HITTEST_ONITEMSTATEICON
;
726 else if ((image_w
!= -1) &&
728 (state_w
!= -1 ? state_w
+
729 MARGIN_BETWEEN_STATE_AND_IMAGE
732 flags
|= wxTREE_HITTEST_ONITEMICON
;
734 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
740 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
741 if (point
.x
> m_x
+m_width
)
742 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
747 // if children are expanded, fall through to evaluate them
748 if (m_isCollapsed
) return NULL
;
752 size_t count
= m_children
.GetCount();
753 for ( size_t n
= 0; n
< count
; n
++ )
755 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
766 int wxGenericTreeItem::GetCurrentImage() const
768 int image
= NO_IMAGE
;
773 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
776 if ( image
== NO_IMAGE
)
778 // we usually fall back to the normal item, but try just the
779 // expanded one (and not selected) first in this case
780 image
= GetImage(wxTreeItemIcon_Expanded
);
786 image
= GetImage(wxTreeItemIcon_Selected
);
789 // maybe it doesn't have the specific image we want,
790 // try the default one instead
791 if ( image
== NO_IMAGE
) image
= GetImage();
796 void wxGenericTreeItem::CalculateSize(wxGenericTreeCtrl
* control
)
798 // check if we need to do anything before creating the DC
802 wxClientDC
dc(control
);
803 DoCalculateSize(control
, dc
, false /* normal font not used */);
807 wxGenericTreeItem::DoCalculateSize(wxGenericTreeCtrl
* control
,
809 bool dcUsesNormalFont
)
811 if ( m_width
!= 0 ) // Size known, nothing to do
814 if ( m_widthText
== -1 )
817 if ( SetFont(control
, dc
) )
821 else // we have no special font
823 if ( !dcUsesNormalFont
)
825 // but we do need to ensure that the normal font is used: notice
826 // that this doesn't count as changing the font as we don't need
828 dc
.SetFont(control
->m_normalFont
);
834 dc
.GetTextExtent( GetText(), &m_widthText
, &m_heightText
);
836 // restore normal font if the DC used it previously and we changed it
838 dc
.SetFont(control
->m_normalFont
);
841 int text_h
= m_heightText
+ 2;
843 int image_h
= 0, image_w
= 0;
844 int image
= GetCurrentImage();
845 if ( image
!= NO_IMAGE
&& control
->m_imageListNormal
)
847 control
->m_imageListNormal
->GetSize(image
, image_w
, image_h
);
848 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
851 int state_h
= 0, state_w
= 0;
852 int state
= GetState();
853 if ( state
!= wxTREE_ITEMSTATE_NONE
&& control
->m_imageListState
)
855 control
->m_imageListState
->GetSize(state
, state_w
, state_h
);
857 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
859 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
862 int img_h
= wxMax(state_h
, image_h
);
863 m_height
= wxMax(img_h
, text_h
);
866 m_height
+= 2; // at least 2 pixels
868 m_height
+= m_height
/ 10; // otherwise 10% extra spacing
870 if (m_height
> control
->m_lineHeight
)
871 control
->m_lineHeight
= m_height
;
873 m_width
= state_w
+ image_w
+ m_widthText
+ 2;
876 void wxGenericTreeItem::RecursiveResetSize()
880 const size_t count
= m_children
.Count();
881 for (size_t i
= 0; i
< count
; i
++ )
882 m_children
[i
]->RecursiveResetSize();
885 void wxGenericTreeItem::RecursiveResetTextSize()
890 const size_t count
= m_children
.Count();
891 for (size_t i
= 0; i
< count
; i
++ )
892 m_children
[i
]->RecursiveResetTextSize();
895 // -----------------------------------------------------------------------------
896 // wxGenericTreeCtrl implementation
897 // -----------------------------------------------------------------------------
899 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
901 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
902 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
903 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
904 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
905 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
906 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
907 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
908 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
911 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
913 * wxTreeCtrl has to be a real class or we have problems with
914 * the run-time information.
917 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
920 // -----------------------------------------------------------------------------
921 // construction/destruction
922 // -----------------------------------------------------------------------------
924 void wxGenericTreeCtrl::Init()
937 m_hilightBrush
= new wxBrush
939 wxSystemSettings::GetColour
941 wxSYS_COLOUR_HIGHLIGHT
946 m_hilightUnfocusedBrush
= new wxBrush
948 wxSystemSettings::GetColour
950 wxSYS_COLOUR_BTNSHADOW
955 m_imageListButtons
= NULL
;
956 m_ownsImageListButtons
= false;
959 m_isDragging
= false;
960 m_dropTarget
= m_oldSelection
= NULL
;
964 m_renameTimer
= NULL
;
968 m_dropEffectAboveItem
= false;
970 m_dndEffect
= NoEffect
;
971 m_dndEffectItem
= NULL
;
973 m_lastOnSame
= false;
975 #if defined( __WXMAC__ )
976 #if wxOSX_USE_ATSU_TEXT
977 m_normalFont
.MacCreateFromThemeFont( kThemeViewsFont
) ;
979 m_normalFont
.MacCreateFromUIFont( kCTFontViewsFontType
) ;
982 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
984 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
985 m_normalFont
.GetFamily(),
986 m_normalFont
.GetStyle(),
988 m_normalFont
.GetUnderlined(),
989 m_normalFont
.GetFaceName(),
990 m_normalFont
.GetEncoding());
993 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
998 const wxValidator
& validator
,
999 const wxString
& name
)
1003 wxGetOsVersion(&major
, &minor
);
1006 style
|= wxTR_ROW_LINES
;
1009 if ( !wxControl::Create( parent
, id
, pos
, size
,
1010 style
|wxHSCROLL
|wxVSCROLL
,
1015 // If the tree display has no buttons, but does have
1016 // connecting lines, we can use a narrower layout.
1017 // It may not be a good idea to force this...
1018 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
1024 wxVisualAttributes attr
= GetDefaultAttributes();
1025 SetOwnForegroundColour( attr
.colFg
);
1026 SetOwnBackgroundColour( attr
.colBg
);
1028 SetOwnFont(attr
.font
);
1030 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1031 // style because we apparently get performance problems when using dotted
1032 // pen for drawing in some ports -- but under MSW it seems to work fine
1034 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
1036 m_dottedPen
= *wxGREY_PEN
;
1039 SetInitialSize(size
);
1044 wxGenericTreeCtrl::~wxGenericTreeCtrl()
1046 delete m_hilightBrush
;
1047 delete m_hilightUnfocusedBrush
;
1051 delete m_renameTimer
;
1054 if (m_ownsImageListButtons
)
1055 delete m_imageListButtons
;
1058 // -----------------------------------------------------------------------------
1060 // -----------------------------------------------------------------------------
1062 unsigned int wxGenericTreeCtrl::GetCount() const
1066 // the tree is empty
1070 unsigned int count
= m_anchor
->GetChildrenCount();
1071 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1073 // take the root itself into account
1080 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
1082 m_indent
= (unsigned short) indent
;
1087 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
1088 bool recursively
) const
1090 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
1092 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
1095 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
1097 // Do not try to expand the root node if it hasn't been created yet
1098 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
1100 // if we will hide the root, make sure children are visible
1101 m_anchor
->SetHasPlus();
1103 CalculatePositions();
1106 // right now, just sets the styles. Eventually, we may
1107 // want to update the inherited styles, but right now
1108 // none of the parents has updatable styles
1109 m_windowStyle
= styles
;
1113 // -----------------------------------------------------------------------------
1114 // functions to work with tree items
1115 // -----------------------------------------------------------------------------
1117 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
1119 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
1121 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
1124 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1125 wxTreeItemIcon which
) const
1127 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1129 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
1132 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1134 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1136 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
1139 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
1141 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
1143 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1144 return pItem
->GetState();
1147 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1149 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1151 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1152 return pItem
->Attr().GetTextColour();
1156 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1158 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1160 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1161 return pItem
->Attr().GetBackgroundColour();
1164 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1166 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1168 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1169 return pItem
->Attr().GetFont();
1173 wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1175 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1177 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1178 pItem
->SetText(text
);
1179 pItem
->CalculateSize(this);
1183 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1185 wxTreeItemIcon which
)
1187 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1189 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1190 pItem
->SetImage(image
, which
);
1191 pItem
->CalculateSize(this);
1196 wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1198 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1201 data
->SetId( item
);
1203 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1206 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1208 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1210 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1211 pItem
->SetState(state
);
1212 pItem
->CalculateSize(this);
1216 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1218 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1220 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1221 pItem
->SetHasPlus(has
);
1225 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1227 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1229 // avoid redrawing the tree if no real change
1230 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1231 if ( pItem
->IsBold() != bold
)
1233 pItem
->SetBold(bold
);
1235 // recalculate the item size as bold and non bold fonts have different
1237 pItem
->CalculateSize(this);
1242 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1245 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1251 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1252 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1255 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1256 pItem
->Attr().SetTextColour(fg
);
1257 pItem
->Attr().SetBackgroundColour(bg
);
1261 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1262 const wxColour
& col
)
1264 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1266 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1267 pItem
->Attr().SetTextColour(col
);
1271 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1272 const wxColour
& col
)
1274 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1276 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1277 pItem
->Attr().SetBackgroundColour(col
);
1282 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1284 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1286 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1287 pItem
->Attr().SetFont(font
);
1288 pItem
->ResetTextSize();
1289 pItem
->CalculateSize(this);
1293 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1295 wxTreeCtrlBase::SetFont(font
);
1297 m_normalFont
= font
;
1298 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1299 m_normalFont
.GetFamily(),
1300 m_normalFont
.GetStyle(),
1302 m_normalFont
.GetUnderlined(),
1303 m_normalFont
.GetFaceName(),
1304 m_normalFont
.GetEncoding());
1307 m_anchor
->RecursiveResetTextSize();
1313 // -----------------------------------------------------------------------------
1314 // item status inquiries
1315 // -----------------------------------------------------------------------------
1317 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1319 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1321 // An item is only visible if it's not a descendant of a collapsed item
1322 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1323 wxGenericTreeItem
* parent
= pItem
->GetParent();
1326 if (!parent
->IsExpanded())
1328 parent
= parent
->GetParent();
1332 GetViewStart(& startX
, & startY
);
1334 wxSize clientSize
= GetClientSize();
1337 if (!GetBoundingRect(item
, rect
))
1339 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1341 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1343 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1349 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1351 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1353 // consider that the item does have children if it has the "+" button: it
1354 // might not have them (if it had never been expanded yet) but then it
1355 // could have them as well and it's better to err on this side rather than
1356 // disabling some operations which are restricted to the items with
1357 // children for an item which does have them
1358 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1361 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1363 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1365 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1368 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1370 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1372 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1375 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1377 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1379 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1382 // -----------------------------------------------------------------------------
1384 // -----------------------------------------------------------------------------
1386 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1388 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1390 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1393 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1394 wxTreeItemIdValue
& cookie
) const
1396 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1399 return GetNextChild(item
, cookie
);
1402 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1403 wxTreeItemIdValue
& cookie
) const
1405 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1407 wxArrayGenericTreeItems
&
1408 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1410 // it's ok to cast cookie to size_t, we never have indices big enough to
1411 // overflow "void *"
1412 size_t *pIndex
= (size_t *)&cookie
;
1413 if ( *pIndex
< children
.GetCount() )
1415 return children
.Item((*pIndex
)++);
1419 // there are no more of them
1420 return wxTreeItemId();
1424 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1426 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1428 wxArrayGenericTreeItems
&
1429 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1430 return children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last());
1433 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1435 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1437 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1438 wxGenericTreeItem
*parent
= i
->GetParent();
1439 if ( parent
== NULL
)
1441 // root item doesn't have any siblings
1442 return wxTreeItemId();
1445 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1446 int index
= siblings
.Index(i
);
1447 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1449 size_t n
= (size_t)(index
+ 1);
1450 return n
== siblings
.GetCount() ? wxTreeItemId()
1451 : wxTreeItemId(siblings
[n
]);
1454 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1456 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1458 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1459 wxGenericTreeItem
*parent
= i
->GetParent();
1460 if ( parent
== NULL
)
1462 // root item doesn't have any siblings
1463 return wxTreeItemId();
1466 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1467 int index
= siblings
.Index(i
);
1468 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1470 return index
== 0 ? wxTreeItemId()
1471 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1474 // Only for internal use right now, but should probably be public
1475 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1477 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1479 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1481 // First see if there are any children.
1482 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1483 if (children
.GetCount() > 0)
1485 return children
.Item(0);
1489 // Try a sibling of this or ancestor instead
1490 wxTreeItemId p
= item
;
1491 wxTreeItemId toFind
;
1494 toFind
= GetNextSibling(p
);
1495 p
= GetItemParent(p
);
1496 } while (p
.IsOk() && !toFind
.IsOk());
1501 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1503 wxTreeItemId id
= GetRootItem();
1512 } while (id
.IsOk());
1514 return wxTreeItemId();
1517 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1519 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1520 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1522 wxTreeItemId id
= item
;
1525 while (id
= GetNext(id
), id
.IsOk())
1531 return wxTreeItemId();
1534 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1536 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1537 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1539 // find out the starting point
1540 wxTreeItemId prevItem
= GetPrevSibling(item
);
1541 if ( !prevItem
.IsOk() )
1543 prevItem
= GetItemParent(item
);
1546 // find the first visible item after it
1547 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1549 prevItem
= GetNext(prevItem
);
1550 if ( !prevItem
.IsOk() || prevItem
== item
)
1552 // there are no visible items before item
1553 return wxTreeItemId();
1557 // from there we must be able to navigate until this item
1558 while ( prevItem
.IsOk() )
1560 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1561 if ( !nextItem
.IsOk() || nextItem
== item
)
1564 prevItem
= nextItem
;
1570 // called by wxTextTreeCtrl when it marks itself for deletion
1571 void wxGenericTreeCtrl::ResetTextControl()
1576 // find the first item starting with the given prefix after the given item
1577 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1578 const wxString
& prefixOrig
) const
1580 // match is case insensitive as this is more convenient to the user: having
1581 // to press Shift-letter to go to the item starting with a capital letter
1582 // would be too bothersome
1583 wxString prefix
= prefixOrig
.Lower();
1585 // determine the starting point: we shouldn't take the current item (this
1586 // allows to switch between two items starting with the same letter just by
1587 // pressing it) but we shouldn't jump to the next one if the user is
1588 // continuing to type as otherwise he might easily skip the item he wanted
1589 wxTreeItemId id
= idParent
;
1590 if ( prefix
.length() == 1 )
1595 // look for the item starting with the given prefix after it
1596 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1601 // if we haven't found anything...
1604 // ... wrap to the beginning
1606 if ( HasFlag(wxTR_HIDE_ROOT
) )
1608 // can't select virtual root
1612 // and try all the items (stop when we get to the one we started from)
1613 while ( id
.IsOk() && id
!= idParent
&&
1614 !GetItemText(id
).Lower().StartsWith(prefix
) )
1618 // If we haven't found the item, id.IsOk() will be false, as per
1625 // -----------------------------------------------------------------------------
1627 // -----------------------------------------------------------------------------
1629 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1631 const wxString
& text
,
1634 wxTreeItemData
*data
)
1636 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1639 // should we give a warning here?
1640 return AddRoot(text
, image
, selImage
, data
);
1643 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1645 wxGenericTreeItem
*item
=
1646 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1650 data
->m_pItem
= item
;
1653 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1656 InvalidateBestSize();
1660 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1663 wxTreeItemData
*data
)
1665 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
1667 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1669 m_anchor
= new wxGenericTreeItem(NULL
, text
,
1670 image
, selImage
, data
);
1673 data
->m_pItem
= m_anchor
;
1676 if (HasFlag(wxTR_HIDE_ROOT
))
1678 // if root is hidden, make sure we can navigate
1680 m_anchor
->SetHasPlus();
1682 CalculatePositions();
1685 if (!HasFlag(wxTR_MULTIPLE
))
1687 m_current
= m_key_current
= m_anchor
;
1688 m_current
->SetHilight( true );
1691 InvalidateBestSize();
1695 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1696 const wxTreeItemId
& idPrevious
,
1697 const wxString
& text
,
1698 int image
, int selImage
,
1699 wxTreeItemData
*data
)
1701 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1704 // should we give a warning here?
1705 return AddRoot(text
, image
, selImage
, data
);
1709 if (idPrevious
.IsOk())
1711 index
= parent
->GetChildren().Index(
1712 (wxGenericTreeItem
*) idPrevious
.m_pItem
);
1713 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1714 "previous item in wxGenericTreeCtrl::InsertItem() "
1715 "is not a sibling" );
1718 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1722 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1724 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1725 GetEventHandler()->ProcessEvent( event
);
1728 // Don't leave edit or selection on a child which is about to disappear
1729 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1731 if ( m_textCtrl
&& item
!= m_textCtrl
->item() &&
1732 IsDescendantOf(item
, m_textCtrl
->item()) )
1734 m_textCtrl
->EndEdit( true );
1737 if ( item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
) )
1739 m_key_current
= NULL
;
1742 if ( IsDescendantOf(item
, m_select_me
) )
1747 if ( item
!= m_current
&& IsDescendantOf(item
, m_current
) )
1749 m_current
->SetHilight( false );
1755 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1757 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1759 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1760 ChildrenClosing(item
);
1761 item
->DeleteChildren(this);
1762 InvalidateBestSize();
1765 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1767 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1769 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1771 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1773 // can't delete the item being edited, cancel editing it first
1774 m_textCtrl
->EndEdit( true );
1777 wxGenericTreeItem
*parent
= item
->GetParent();
1779 // don't keep stale pointers around!
1780 if ( IsDescendantOf(item
, m_key_current
) )
1782 // Don't silently change the selection:
1783 // do it properly in idle time, so event
1784 // handlers get called.
1786 // m_key_current = parent;
1787 m_key_current
= NULL
;
1790 // m_select_me records whether we need to select
1791 // a different item, in idle time.
1792 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1794 m_select_me
= parent
;
1797 if ( IsDescendantOf(item
, m_current
) )
1799 // Don't silently change the selection:
1800 // do it properly in idle time, so event
1801 // handlers get called.
1803 // m_current = parent;
1805 m_select_me
= parent
;
1808 // remove the item from the tree
1811 parent
->GetChildren().Remove( item
); // remove by value
1813 else // deleting the root
1815 // nothing will be left in the tree
1819 // and delete all of its children and the item itself now
1820 item
->DeleteChildren(this);
1821 SendDeleteEvent(item
);
1823 if (item
== m_select_me
)
1828 InvalidateBestSize();
1831 void wxGenericTreeCtrl::DeleteAllItems()
1839 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1841 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1843 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1844 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1845 _T("can't expand hidden root") );
1847 if ( !item
->HasPlus() )
1850 if ( item
->IsExpanded() )
1853 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1855 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1857 // cancelled by program
1864 CalculatePositions();
1866 RefreshSubtree(item
);
1873 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1874 GetEventHandler()->ProcessEvent( event
);
1877 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1879 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1880 _T("can't collapse hidden root") );
1882 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1884 if ( !item
->IsExpanded() )
1887 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1888 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1890 // cancelled by program
1894 ChildrenClosing(item
);
1897 #if 0 // TODO why should items be collapsed recursively?
1898 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1899 size_t count
= children
.GetCount();
1900 for ( size_t n
= 0; n
< count
; n
++ )
1902 Collapse(children
[n
]);
1906 CalculatePositions();
1908 RefreshSubtree(item
);
1910 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1911 GetEventHandler()->ProcessEvent( event
);
1914 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1917 DeleteChildren(item
);
1920 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1922 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1924 if (item
->IsExpanded())
1930 void wxGenericTreeCtrl::Unselect()
1934 m_current
->SetHilight( false );
1935 RefreshLine( m_current
);
1942 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1944 if (item
->IsSelected())
1946 item
->SetHilight(false);
1950 if (item
->HasChildren())
1952 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1953 size_t count
= children
.GetCount();
1954 for ( size_t n
= 0; n
< count
; ++n
)
1956 UnselectAllChildren(children
[n
]);
1961 void wxGenericTreeCtrl::UnselectAll()
1963 wxTreeItemId rootItem
= GetRootItem();
1965 // the tree might not have the root item at all
1968 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1972 // Recursive function !
1973 // To stop we must have crt_item<last_item
1975 // Tag all next children, when no more children,
1976 // Move to parent (not to tag)
1977 // Keep going... if we found last_item, we stop.
1979 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
,
1980 wxGenericTreeItem
*last_item
,
1983 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1985 if (parent
== NULL
) // This is root item
1986 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
1988 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
1989 int index
= children
.Index(crt_item
);
1990 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1992 size_t count
= children
.GetCount();
1993 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
1995 if ( TagAllChildrenUntilLast(children
[n
], last_item
, select
) )
1999 return TagNextChildren(parent
, last_item
, select
);
2003 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
,
2004 wxGenericTreeItem
*last_item
,
2007 crt_item
->SetHilight(select
);
2008 RefreshLine(crt_item
);
2010 if (crt_item
==last_item
)
2013 if (crt_item
->HasChildren())
2015 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
2016 size_t count
= children
.GetCount();
2017 for ( size_t n
= 0; n
< count
; ++n
)
2019 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
2028 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
,
2029 wxGenericTreeItem
*item2
)
2033 // item2 is not necessary after item1
2034 // choice first' and 'last' between item1 and item2
2035 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
2036 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
2038 bool select
= m_current
->IsSelected();
2040 if ( TagAllChildrenUntilLast(first
,last
,select
) )
2043 TagNextChildren(first
,last
,select
);
2046 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
2047 bool unselect_others
,
2048 bool extended_select
)
2050 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2054 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2055 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2057 //wxCHECK_RET( ( (!unselect_others) && is_single),
2058 // wxT("this is a single selection tree") );
2060 // to keep going anyhow !!!
2063 if (item
->IsSelected())
2064 return; // nothing to do
2065 unselect_others
= true;
2066 extended_select
= false;
2068 else if ( unselect_others
&& item
->IsSelected() )
2070 // selection change if there is more than one item currently selected
2071 wxArrayTreeItemIds selected_items
;
2072 if ( GetSelections(selected_items
) == 1 )
2076 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2077 event
.m_itemOld
= m_current
;
2078 // TODO : Here we don't send any selection mode yet !
2080 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2083 wxTreeItemId parent
= GetItemParent( itemId
);
2084 while (parent
.IsOk())
2086 if (!IsExpanded(parent
))
2089 parent
= GetItemParent( parent
);
2093 if (unselect_others
)
2095 if (is_single
) Unselect(); // to speed up thing
2100 if (extended_select
)
2105 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
2108 // don't change the mark (m_current)
2109 SelectItemRange(m_current
, item
);
2113 bool select
= true; // the default
2115 // Check if we need to toggle hilight (ctrl mode)
2116 if (!unselect_others
)
2117 select
=!item
->IsSelected();
2119 m_current
= m_key_current
= item
;
2120 m_current
->SetHilight(select
);
2121 RefreshLine( m_current
);
2124 // This can cause idle processing to select the root
2125 // if no item is selected, so it must be after the
2127 EnsureVisible( itemId
);
2129 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2130 GetEventHandler()->ProcessEvent( event
);
2133 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
2137 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
2141 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2142 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
2144 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2145 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2148 item
->SetHilight(false);
2151 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2152 GetEventHandler()->ProcessEvent( event
);
2156 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
2157 wxArrayTreeItemIds
&array
) const
2159 if ( item
->IsSelected() )
2160 array
.Add(wxTreeItemId(item
));
2162 if ( item
->HasChildren() )
2164 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2165 size_t count
= children
.GetCount();
2166 for ( size_t n
= 0; n
< count
; ++n
)
2167 FillArray(children
[n
], array
);
2171 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
2174 wxTreeItemId idRoot
= GetRootItem();
2175 if ( idRoot
.IsOk() )
2177 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
2179 //else: the tree is empty, so no selections
2181 return array
.GetCount();
2184 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2186 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2188 if (!item
.IsOk()) return;
2190 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2192 // first expand all parent branches
2193 wxGenericTreeItem
*parent
= gitem
->GetParent();
2195 if ( HasFlag(wxTR_HIDE_ROOT
) )
2197 while ( parent
&& parent
!= m_anchor
)
2200 parent
= parent
->GetParent();
2208 parent
= parent
->GetParent();
2212 //if (parent) CalculatePositions();
2217 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2219 if (!item
.IsOk()) return;
2221 // We have to call this here because the label in
2222 // question might just have been added and no screen
2223 // update taken place.
2225 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2228 DoDirtyProcessing();
2230 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2232 // now scroll to the item
2233 int item_y
= gitem
->GetY();
2237 GetViewStart( &start_x
, &start_y
);
2238 start_y
*= PIXELS_PER_UNIT
;
2242 GetClientSize( &client_w
, &client_h
);
2244 if (item_y
< start_y
+3)
2249 m_anchor
->GetSize( x
, y
, this );
2250 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2251 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2252 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2253 // Item should appear at top
2254 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2255 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2256 x_pos
, item_y
/PIXELS_PER_UNIT
);
2258 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2263 m_anchor
->GetSize( x
, y
, this );
2264 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2265 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2266 item_y
+= PIXELS_PER_UNIT
+2;
2267 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2268 // Item should appear at bottom
2269 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2270 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2272 (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
2276 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2277 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2279 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2280 wxGenericTreeItem
**item2
)
2282 wxCHECK_MSG( s_treeBeingSorted
, 0,
2283 "bug in wxGenericTreeCtrl::SortChildren()" );
2285 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2288 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2290 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2292 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2294 wxCHECK_RET( !s_treeBeingSorted
,
2295 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2297 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2298 if ( children
.GetCount() > 1 )
2302 s_treeBeingSorted
= this;
2303 children
.Sort(tree_ctrl_compare_func
);
2304 s_treeBeingSorted
= NULL
;
2306 //else: don't make the tree dirty as nothing changed
2309 void wxGenericTreeCtrl::CalculateLineHeight()
2311 wxClientDC
dc(this);
2312 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2314 if ( m_imageListNormal
)
2316 // Calculate a m_lineHeight value from the normal Image sizes.
2317 // May be toggle off. Then wxGenericTreeCtrl will spread when
2318 // necessary (which might look ugly).
2319 int n
= m_imageListNormal
->GetImageCount();
2320 for (int i
= 0; i
< n
; i
++)
2322 int width
= 0, height
= 0;
2323 m_imageListNormal
->GetSize(i
, width
, height
);
2324 if (height
> m_lineHeight
) m_lineHeight
= height
;
2328 if ( m_imageListState
)
2330 // Calculate a m_lineHeight value from the state Image sizes.
2331 // May be toggle off. Then wxGenericTreeCtrl will spread when
2332 // necessary (which might look ugly).
2333 int n
= m_imageListState
->GetImageCount();
2334 for (int i
= 0; i
< n
; i
++)
2336 int width
= 0, height
= 0;
2337 m_imageListState
->GetSize(i
, width
, height
);
2338 if (height
> m_lineHeight
) m_lineHeight
= height
;
2342 if (m_imageListButtons
)
2344 // Calculate a m_lineHeight value from the Button image sizes.
2345 // May be toggle off. Then wxGenericTreeCtrl will spread when
2346 // necessary (which might look ugly).
2347 int n
= m_imageListButtons
->GetImageCount();
2348 for (int i
= 0; i
< n
; i
++)
2350 int width
= 0, height
= 0;
2351 m_imageListButtons
->GetSize(i
, width
, height
);
2352 if (height
> m_lineHeight
) m_lineHeight
= height
;
2356 if (m_lineHeight
< 30)
2357 m_lineHeight
+= 2; // at least 2 pixels
2359 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2362 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2364 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2365 m_imageListNormal
= imageList
;
2366 m_ownsImageListNormal
= false;
2370 m_anchor
->RecursiveResetSize();
2372 // Don't do any drawing if we're setting the list to NULL,
2373 // since we may be in the process of deleting the tree control.
2375 CalculateLineHeight();
2378 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2380 if (m_ownsImageListState
) delete m_imageListState
;
2381 m_imageListState
= imageList
;
2382 m_ownsImageListState
= 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::SetButtonsImageList(wxImageList
*imageList
)
2396 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2397 m_imageListButtons
= imageList
;
2398 m_ownsImageListButtons
= false;
2402 m_anchor
->RecursiveResetSize();
2404 CalculateLineHeight();
2407 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2409 SetButtonsImageList(imageList
);
2410 m_ownsImageListButtons
= true;
2413 // -----------------------------------------------------------------------------
2415 // -----------------------------------------------------------------------------
2417 void wxGenericTreeCtrl::AdjustMyScrollbars()
2422 m_anchor
->GetSize( x
, y
, this );
2423 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2424 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2425 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2426 int y_pos
= GetScrollPos( wxVERTICAL
);
2427 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2428 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2433 SetScrollbars( 0, 0, 0, 0 );
2437 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2439 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2440 return item
->GetHeight();
2442 return m_lineHeight
;
2445 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2447 item
->SetFont(this, dc
);
2448 item
->CalculateSize(this, dc
);
2450 wxCoord text_h
= item
->GetTextHeight();
2452 int image_h
= 0, image_w
= 0;
2453 int image
= item
->GetCurrentImage();
2454 if ( image
!= NO_IMAGE
)
2456 if ( m_imageListNormal
)
2458 m_imageListNormal
->GetSize(image
, image_w
, image_h
);
2459 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2467 int state_h
= 0, state_w
= 0;
2468 int state
= item
->GetState();
2469 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2471 if ( m_imageListState
)
2473 m_imageListState
->GetSize(state
, state_w
, state_h
);
2475 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2477 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2481 state
= wxTREE_ITEMSTATE_NONE
;
2485 int total_h
= GetLineHeight(item
);
2486 bool drawItemBackground
= false,
2487 hasBgColour
= false;
2489 if ( item
->IsSelected() )
2491 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2492 drawItemBackground
= true;
2497 wxTreeItemAttr
* const attr
= item
->GetAttributes();
2498 if ( attr
&& attr
->HasBackgroundColour() )
2500 drawItemBackground
=
2502 colBg
= attr
->GetBackgroundColour();
2506 colBg
= GetBackgroundColour();
2508 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2511 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2513 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2517 GetVirtualSize(&w
, &h
);
2518 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2519 if (!item
->IsSelected())
2521 dc
.DrawRectangle(rect
);
2525 int flags
= wxCONTROL_SELECTED
;
2527 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2528 && IsControlActive( (ControlRef
)GetHandle() )
2531 flags
|= wxCONTROL_FOCUSED
;
2532 if ((item
== m_current
) && (m_hasFocus
))
2533 flags
|= wxCONTROL_CURRENT
;
2535 wxRendererNative::Get().
2536 DrawItemSelectionRect(this, dc
, rect
, flags
);
2539 else // no full row highlight
2541 if ( item
->IsSelected() &&
2542 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2544 // If it's selected, and there's an state image or normal image,
2545 // then we should take care to leave the area under the image
2546 // painted in the background colour.
2547 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2548 item
->GetY() + offset
,
2549 item
->GetWidth() - state_w
- image_w
+ 2,
2551 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2552 dc
.DrawRectangle( rect
);
2557 int flags
= wxCONTROL_SELECTED
;
2559 flags
|= wxCONTROL_FOCUSED
;
2560 if ((item
== m_current
) && (m_hasFocus
))
2561 flags
|= wxCONTROL_CURRENT
;
2562 wxRendererNative::Get().
2563 DrawItemSelectionRect(this, dc
, rect
, flags
);
2566 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2567 // don't allow backgrounds to be customized. Not drawing the background,
2568 // except for custom item backgrounds, works for both kinds of theme.
2569 else if (drawItemBackground
)
2571 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2572 item
->GetY() + offset
,
2573 item
->GetWidth() - state_w
- image_w
+ 2,
2577 dc
.DrawRectangle( rect
);
2579 else // no specific background colour
2584 int flags
= wxCONTROL_SELECTED
;
2586 flags
|= wxCONTROL_FOCUSED
;
2587 if ((item
== m_current
) && (m_hasFocus
))
2588 flags
|= wxCONTROL_CURRENT
;
2589 wxRendererNative::Get().
2590 DrawItemSelectionRect(this, dc
, rect
, flags
);
2595 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2597 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2598 m_imageListState
->Draw( state
, dc
,
2601 (total_h
> state_h
? (total_h
-state_h
)/2
2603 wxIMAGELIST_DRAW_TRANSPARENT
);
2604 dc
.DestroyClippingRegion();
2607 if ( image
!= NO_IMAGE
)
2609 dc
.SetClippingRegion(item
->GetX() + state_w
, item
->GetY(),
2611 m_imageListNormal
->Draw( image
, dc
,
2612 item
->GetX() + state_w
,
2614 (total_h
> image_h
? (total_h
-image_h
)/2
2616 wxIMAGELIST_DRAW_TRANSPARENT
);
2617 dc
.DestroyClippingRegion();
2620 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2621 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2622 dc
.DrawText( item
->GetText(),
2623 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2624 (wxCoord
)(item
->GetY() + extraH
));
2626 // restore normal font
2627 dc
.SetFont( m_normalFont
);
2629 if (item
== m_dndEffectItem
)
2631 dc
.SetPen( *wxBLACK_PEN
);
2632 // DnD visual effects
2633 switch (m_dndEffect
)
2637 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2638 int w
= item
->GetWidth() + 2;
2639 int h
= total_h
+ 2;
2640 dc
.DrawRectangle( item
->GetX() - 1, item
->GetY() - 1, w
, h
);
2645 int x
= item
->GetX(),
2647 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2652 int x
= item
->GetX(),
2655 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2665 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem
*item
,
2670 int x
= level
*m_indent
;
2671 if (!HasFlag(wxTR_HIDE_ROOT
))
2675 else if (level
== 0)
2677 // always expand hidden root
2679 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2680 int count
= children
.GetCount();
2686 PaintLevel(children
[n
], dc
, 1, y
);
2687 } while (++n
< count
);
2689 if ( !HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
)
2692 // draw line down to last child
2693 origY
+= GetLineHeight(children
[0])>>1;
2694 oldY
+= GetLineHeight(children
[n
-1])>>1;
2695 dc
.DrawLine(3, origY
, 3, oldY
);
2701 item
->SetX(x
+m_spacing
);
2704 int h
= GetLineHeight(item
);
2706 int y_mid
= y_top
+ (h
>>1);
2709 int exposed_x
= dc
.LogicalToDeviceX(0);
2710 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2712 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2716 // don't draw rect outline if we already have the
2717 // background color under Mac
2718 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2719 #endif // !__WXMAC__
2723 if ( item
->IsSelected()
2724 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2725 // On wxMac, if the tree doesn't have the focus we draw an empty
2726 // rectangle, so we want to make sure that the text is visible
2727 // against the normal background, not the highlightbackground, so
2728 // don't use the highlight text colour unless we have the focus.
2729 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2736 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2741 wxTreeItemAttr
*attr
= item
->GetAttributes();
2742 if (attr
&& attr
->HasTextColour())
2743 colText
= attr
->GetTextColour();
2745 colText
= GetForegroundColour();
2749 dc
.SetTextForeground(colText
);
2753 PaintItem(item
, dc
);
2755 if (HasFlag(wxTR_ROW_LINES
))
2757 // if the background colour is white, choose a
2758 // contrasting color for the lines
2759 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2760 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2761 dc
.DrawLine(0, y_top
, 10000, y_top
);
2762 dc
.DrawLine(0, y
, 10000, y
);
2765 // restore DC objects
2766 dc
.SetBrush(*wxWHITE_BRUSH
);
2767 dc
.SetPen(m_dottedPen
);
2768 dc
.SetTextForeground(*wxBLACK
);
2770 if ( !HasFlag(wxTR_NO_LINES
) )
2772 // draw the horizontal line here
2774 if (x
> (signed)m_indent
)
2775 x_start
-= m_indent
;
2776 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2778 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2781 // should the item show a button?
2782 if ( item
->HasPlus() && HasButtons() )
2784 if ( m_imageListButtons
)
2786 // draw the image button here
2789 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2790 : wxTreeItemIcon_Normal
;
2791 if ( item
->IsSelected() )
2792 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2794 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2795 int xx
= x
- image_w
/2;
2796 int yy
= y_mid
- image_h
/2;
2798 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2799 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2800 wxIMAGELIST_DRAW_TRANSPARENT
);
2802 else // no custom buttons
2804 static const int wImage
= 9;
2805 static const int hImage
= 9;
2808 if (item
->IsExpanded())
2809 flag
|= wxCONTROL_EXPANDED
;
2810 if (item
== m_underMouse
)
2811 flag
|= wxCONTROL_CURRENT
;
2813 wxRendererNative::Get().DrawTreeItemButton
2817 wxRect(x
- wImage
/2,
2826 if (item
->IsExpanded())
2828 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2829 int count
= children
.GetCount();
2836 PaintLevel(children
[n
], dc
, level
, y
);
2837 } while (++n
< count
);
2839 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2841 // draw line down to last child
2842 oldY
+= GetLineHeight(children
[n
-1])>>1;
2843 if (HasButtons()) y_mid
+= 5;
2845 // Only draw the portion of the line that is visible, in case
2847 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2848 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2849 yOrigin
= abs(yOrigin
);
2850 GetClientSize(&width
, &height
);
2852 // Move end points to the begining/end of the view?
2853 if (y_mid
< yOrigin
)
2855 if (oldY
> yOrigin
+ height
)
2856 oldY
= yOrigin
+ height
;
2858 // after the adjustments if y_mid is larger than oldY then the
2859 // line isn't visible at all so don't draw anything
2861 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2867 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2871 if ( item
->HasPlus() )
2873 // it's a folder, indicate it by a border
2878 // draw a line under the drop target because the item will be
2880 DrawLine(item
, !m_dropEffectAboveItem
);
2883 SetCursor(*wxSTANDARD_CURSOR
);
2888 SetCursor(wxCURSOR_NO_ENTRY
);
2892 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2894 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2896 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2898 if (m_dndEffect
== NoEffect
)
2900 m_dndEffect
= BorderEffect
;
2901 m_dndEffectItem
= i
;
2905 m_dndEffect
= NoEffect
;
2906 m_dndEffectItem
= NULL
;
2909 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2910 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2911 RefreshRect( rect
);
2914 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2916 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2918 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2920 if (m_dndEffect
== NoEffect
)
2923 m_dndEffect
= BelowEffect
;
2925 m_dndEffect
= AboveEffect
;
2926 m_dndEffectItem
= i
;
2930 m_dndEffect
= NoEffect
;
2931 m_dndEffectItem
= NULL
;
2934 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2935 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2936 RefreshRect( rect
);
2939 // -----------------------------------------------------------------------------
2940 // wxWidgets callbacks
2941 // -----------------------------------------------------------------------------
2943 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2946 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2947 RefreshLine( m_current
);
2953 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2961 dc
.SetFont( m_normalFont
);
2962 dc
.SetPen( m_dottedPen
);
2964 // this is now done dynamically
2965 //if(GetImageList() == NULL)
2966 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2969 PaintLevel( m_anchor
, dc
, 0, y
);
2972 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2981 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
2990 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
2992 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
2993 te
.m_evtKey
= event
;
2994 if ( GetEventHandler()->ProcessEvent( te
) )
2996 // intercepted by the user code
3000 if ( (m_current
== 0) || (m_key_current
== 0) )
3006 // how should the selection work for this event?
3007 bool is_multiple
, extended_select
, unselect_others
;
3008 EventFlagsToSelType(GetWindowStyleFlag(),
3011 is_multiple
, extended_select
, unselect_others
);
3013 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3015 if (event
.GetKeyCode() == WXK_RIGHT
)
3016 event
.m_keyCode
= WXK_LEFT
;
3017 else if (event
.GetKeyCode() == WXK_LEFT
)
3018 event
.m_keyCode
= WXK_RIGHT
;
3023 // * : Expand all/Collapse all
3024 // ' ' | return : activate
3025 // up : go up (not last children!)
3027 // left : go to parent
3028 // right : open if parent and go next
3029 // home : go to root
3030 // end : go to last item without opening parents
3031 // alnum : start or continue searching for the item with this prefix
3032 int keyCode
= event
.GetKeyCode();
3037 if (m_current
->HasPlus() && !IsExpanded(m_current
))
3045 if ( !IsExpanded(m_current
) )
3048 ExpandAllChildren(m_current
);
3051 //else: fall through to Collapse() it
3055 if (IsExpanded(m_current
))
3057 Collapse(m_current
);
3063 // Use the item's bounding rectangle to determine position for
3066 GetBoundingRect(m_current
, ItemRect
, true);
3069 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
3070 // Use the left edge, vertical middle
3071 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
3073 ItemRect
.GetHeight() / 2);
3074 GetEventHandler()->ProcessEvent( eventMenu
);
3080 if ( !event
.HasModifiers() )
3083 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
3084 GetEventHandler()->ProcessEvent( eventAct
);
3087 // in any case, also generate the normal key event for this key,
3088 // even if we generated the ACTIVATED event above: this is what
3089 // wxMSW does and it makes sense because you might not want to
3090 // process ACTIVATED event at all and handle Space and Return
3091 // directly (and differently) which would be impossible otherwise
3095 // up goes to the previous sibling or to the last
3096 // of its children if it's expanded
3099 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
3102 prev
= GetItemParent( m_key_current
);
3103 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3105 break; // don't go to root if it is hidden
3109 wxTreeItemIdValue cookie
;
3110 wxTreeItemId current
= m_key_current
;
3111 // TODO: Huh? If we get here, we'd better be the first
3112 // child of our parent. How else could it be?
3113 if (current
== GetFirstChild( prev
, cookie
))
3115 // otherwise we return to where we came from
3119 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
3126 while ( IsExpanded(prev
) && HasChildren(prev
) )
3128 wxTreeItemId child
= GetLastChild(prev
);
3135 DoSelectItem( prev
, unselect_others
, extended_select
);
3136 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
3141 // left arrow goes to the parent
3144 wxTreeItemId prev
= GetItemParent( m_current
);
3145 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3147 // don't go to root if it is hidden
3148 prev
= GetPrevSibling( m_current
);
3152 DoSelectItem( prev
, unselect_others
, extended_select
);
3158 // this works the same as the down arrow except that we
3159 // also expand the item if it wasn't expanded yet
3160 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
3162 //else: don't try to expand hidden root item (which can be the
3163 // current one when the tree is empty)
3169 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
3171 wxTreeItemIdValue cookie
;
3172 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
3176 DoSelectItem( child
, unselect_others
, extended_select
);
3177 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
3181 wxTreeItemId next
= GetNextSibling( m_key_current
);
3184 wxTreeItemId current
= m_key_current
;
3185 while (current
.IsOk() && !next
)
3187 current
= GetItemParent( current
);
3188 if (current
) next
= GetNextSibling( current
);
3193 DoSelectItem( next
, unselect_others
, extended_select
);
3194 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
3200 // <End> selects the last visible tree item
3203 wxTreeItemId last
= GetRootItem();
3205 while ( last
.IsOk() && IsExpanded(last
) )
3207 wxTreeItemId lastChild
= GetLastChild(last
);
3209 // it may happen if the item was expanded but then all of
3210 // its children have been deleted - so IsExpanded() returned
3211 // true, but GetLastChild() returned invalid item
3220 DoSelectItem( last
, unselect_others
, extended_select
);
3225 // <Home> selects the root item
3228 wxTreeItemId prev
= GetRootItem();
3232 if ( HasFlag(wxTR_HIDE_ROOT
) )
3234 wxTreeItemIdValue cookie
;
3235 prev
= GetFirstChild(prev
, cookie
);
3240 DoSelectItem( prev
, unselect_others
, extended_select
);
3245 // do not use wxIsalnum() here
3246 if ( !event
.HasModifiers() &&
3247 ((keyCode
>= '0' && keyCode
<= '9') ||
3248 (keyCode
>= 'a' && keyCode
<= 'z') ||
3249 (keyCode
>= 'A' && keyCode
<= 'Z' )))
3251 // find the next item starting with the given prefix
3252 wxChar ch
= (wxChar
)keyCode
;
3254 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
3265 // also start the timer to reset the current prefix if the user
3266 // doesn't press any more alnum keys soon -- we wouldn't want
3267 // to use this prefix for a new item search
3270 m_findTimer
= new wxTreeFindTimer(this);
3273 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
3283 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
3288 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3289 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3290 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3291 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3292 if (flags
) return wxTreeItemId();
3294 if (m_anchor
== NULL
)
3296 flags
= wxTREE_HITTEST_NOWHERE
;
3297 return wxTreeItemId();
3300 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3304 flags
= wxTREE_HITTEST_NOWHERE
;
3305 return wxTreeItemId();
3310 // get the bounding rectangle of the item (or of its label only)
3311 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3313 bool textOnly
) const
3315 wxCHECK_MSG( item
.IsOk(), false,
3316 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3318 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3322 int image_h
= 0, image_w
= 0;
3323 int image
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetCurrentImage();
3324 if ( image
!= NO_IMAGE
&& m_imageListNormal
)
3326 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3327 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3330 int state_h
= 0, state_w
= 0;
3331 int state
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetState();
3332 if ( state
!= wxTREE_ITEMSTATE_NONE
&& m_imageListState
)
3334 m_imageListState
->GetSize( state
, state_w
, state_h
);
3336 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
3338 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3341 rect
.x
= i
->GetX() + state_w
+ image_w
;
3342 rect
.width
= i
->GetWidth() - state_w
- image_w
;
3345 else // the entire line
3348 rect
.width
= GetClientSize().x
;
3352 rect
.height
= GetLineHeight(i
);
3354 // we have to return the logical coordinates, not physical ones
3355 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3360 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3361 wxClassInfo
* WXUNUSED(textCtrlClass
))
3363 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
3365 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3367 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3368 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3374 // We have to call this here because the label in
3375 // question might just have been added and no screen
3376 // update taken place.
3378 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3381 DoDirtyProcessing();
3384 // TODO: use textCtrlClass here to create the control of correct class
3385 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3387 m_textCtrl
->SetFocus();
3392 // returns a pointer to the text edit control if the item is being
3393 // edited, NULL otherwise (it's assumed that no more than one item may
3394 // be edited simultaneously)
3395 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3400 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3401 bool discardChanges
)
3403 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3405 m_textCtrl
->EndEdit(discardChanges
);
3408 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3409 const wxString
& value
)
3411 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3413 le
.m_editCancelled
= false;
3415 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3418 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3420 // let owner know that the edit was cancelled
3421 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3422 le
.m_label
= wxEmptyString
;
3423 le
.m_editCancelled
= true;
3425 GetEventHandler()->ProcessEvent( le
);
3428 void wxGenericTreeCtrl::OnRenameTimer()
3430 EditLabel( m_current
);
3433 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3435 if ( !m_anchor
)return;
3437 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3439 // Is the mouse over a tree item button?
3441 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3442 wxGenericTreeItem
*underMouse
= thisItem
;
3444 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3445 #endif // wxUSE_TOOLTIPS
3448 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3449 (!event
.LeftIsDown()) &&
3451 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3459 if (underMouse
!= m_underMouse
)
3463 // unhighlight old item
3464 wxGenericTreeItem
*tmp
= m_underMouse
;
3465 m_underMouse
= NULL
;
3469 m_underMouse
= underMouse
;
3471 RefreshLine( m_underMouse
);
3475 // Determines what item we are hovering over and need a tooltip for
3476 wxTreeItemId hoverItem
= thisItem
;
3478 // We do not want a tooltip if we are dragging, or if the rename timer is
3480 if ( underMouseChanged
&&
3483 (!m_renameTimer
|| !m_renameTimer
->IsRunning()) )
3485 // Ask the tree control what tooltip (if any) should be shown
3487 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3489 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3491 SetToolTip(hevent
.m_label
);
3496 // we process left mouse up event (enables in-place edit), middle/right down
3497 // (pass to the user code), left dbl click (activate item) and
3498 // dragging/moving events for items drag-and-drop
3499 if ( !(event
.LeftDown() ||
3501 event
.MiddleDown() ||
3502 event
.RightDown() ||
3503 event
.LeftDClick() ||
3505 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3514 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3516 if ( event
.Dragging() && !m_isDragging
)
3518 if (m_dragCount
== 0)
3523 if (m_dragCount
!= 3)
3525 // wait until user drags a bit further...
3529 wxEventType command
= event
.RightIsDown()
3530 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3531 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3533 wxTreeEvent
nevent(command
, this, m_current
);
3534 nevent
.SetPoint(CalcScrolledPosition(pt
));
3536 // by default the dragging is not supported, the user code must
3537 // explicitly allow the event for it to take place
3540 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3542 // we're going to drag this item
3543 m_isDragging
= true;
3545 // remember the old cursor because we will change it while
3547 m_oldCursor
= m_cursor
;
3549 // in a single selection control, hide the selection temporarily
3550 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3552 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3554 if ( m_oldSelection
)
3556 m_oldSelection
->SetHilight(false);
3557 RefreshLine(m_oldSelection
);
3564 else if ( event
.Dragging() )
3566 if ( item
!= m_dropTarget
)
3568 // unhighlight the previous drop target
3569 DrawDropEffect(m_dropTarget
);
3571 m_dropTarget
= item
;
3573 // highlight the current drop target if any
3574 DrawDropEffect(m_dropTarget
);
3576 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3579 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3580 // instead (needs to be tested!)
3585 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3589 // erase the highlighting
3590 DrawDropEffect(m_dropTarget
);
3592 if ( m_oldSelection
)
3594 m_oldSelection
->SetHilight(true);
3595 RefreshLine(m_oldSelection
);
3596 m_oldSelection
= NULL
;
3599 // generate the drag end event
3600 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3602 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3604 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3606 m_isDragging
= false;
3607 m_dropTarget
= NULL
;
3609 SetCursor(m_oldCursor
);
3611 #if defined( __WXMSW__ ) || defined(__WXMAC__) || defined(__WXGTK20__)
3614 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3615 // instead (needs to be tested!)
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