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
->ResetFindState(); }
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
*itm
)
427 : m_itemEdited(itm
), m_startValue(itm
->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(wxOSX_USE_CARBON) && wxOSX_USE_CARBON
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());
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() + wxT("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_KEY_DOWN (wxGenericTreeCtrl::OnKeyDown
)
906 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
907 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
908 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
909 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
912 // -----------------------------------------------------------------------------
913 // construction/destruction
914 // -----------------------------------------------------------------------------
916 void wxGenericTreeCtrl::Init()
929 m_hilightBrush
= new wxBrush
931 wxSystemSettings::GetColour
933 wxSYS_COLOUR_HIGHLIGHT
938 m_hilightUnfocusedBrush
= new wxBrush
940 wxSystemSettings::GetColour
942 wxSYS_COLOUR_BTNSHADOW
947 m_imageListButtons
= NULL
;
948 m_ownsImageListButtons
= false;
951 m_isDragging
= false;
952 m_dropTarget
= m_oldSelection
= NULL
;
956 m_renameTimer
= NULL
;
959 m_findBell
= 0; // default is to not ring bell at all
961 m_dropEffectAboveItem
= false;
963 m_dndEffect
= NoEffect
;
964 m_dndEffectItem
= NULL
;
966 m_lastOnSame
= false;
968 #if defined( __WXMAC__ )
969 m_normalFont
= wxFont(wxOSX_SYSTEM_FONT_VIEWS
);
971 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
973 m_boldFont
= m_normalFont
.Bold();
976 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
981 const wxValidator
& validator
,
982 const wxString
& name
)
986 wxGetOsVersion(&major
, &minor
);
989 style
|= wxTR_ROW_LINES
;
991 if (style
& wxTR_HAS_BUTTONS
)
992 style
|= wxTR_NO_LINES
;
996 if (style
& wxTR_HAS_BUTTONS
)
997 style
|= wxTR_NO_LINES
;
1000 if ( !wxControl::Create( parent
, id
, pos
, size
,
1001 style
|wxHSCROLL
|wxVSCROLL
|wxWANTS_CHARS
,
1006 // If the tree display has no buttons, but does have
1007 // connecting lines, we can use a narrower layout.
1008 // It may not be a good idea to force this...
1009 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
1015 wxVisualAttributes attr
= GetDefaultAttributes();
1016 SetOwnForegroundColour( attr
.colFg
);
1017 SetOwnBackgroundColour( attr
.colBg
);
1019 SetOwnFont(attr
.font
);
1021 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1022 // style because we apparently get performance problems when using dotted
1023 // pen for drawing in some ports -- but under MSW it seems to work fine
1025 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
1027 m_dottedPen
= *wxGREY_PEN
;
1030 SetInitialSize(size
);
1035 wxGenericTreeCtrl::~wxGenericTreeCtrl()
1037 delete m_hilightBrush
;
1038 delete m_hilightUnfocusedBrush
;
1042 delete m_renameTimer
;
1045 if (m_ownsImageListButtons
)
1046 delete m_imageListButtons
;
1049 void wxGenericTreeCtrl::EnableBellOnNoMatch( bool on
)
1054 // -----------------------------------------------------------------------------
1056 // -----------------------------------------------------------------------------
1058 unsigned int wxGenericTreeCtrl::GetCount() const
1062 // the tree is empty
1066 unsigned int count
= m_anchor
->GetChildrenCount();
1067 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1069 // take the root itself into account
1076 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
1078 m_indent
= (unsigned short) indent
;
1083 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
1084 bool recursively
) const
1086 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
1088 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
1091 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
1093 // Do not try to expand the root node if it hasn't been created yet
1094 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
1096 // if we will hide the root, make sure children are visible
1097 m_anchor
->SetHasPlus();
1099 CalculatePositions();
1102 // right now, just sets the styles. Eventually, we may
1103 // want to update the inherited styles, but right now
1104 // none of the parents has updatable styles
1105 m_windowStyle
= styles
;
1109 // -----------------------------------------------------------------------------
1110 // functions to work with tree items
1111 // -----------------------------------------------------------------------------
1113 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
1115 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
1117 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
1120 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1121 wxTreeItemIcon which
) const
1123 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1125 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
1128 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1130 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1132 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
1135 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
1137 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
1139 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1140 return pItem
->GetState();
1143 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1145 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1147 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1148 return pItem
->Attr().GetTextColour();
1152 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1154 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1156 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1157 return pItem
->Attr().GetBackgroundColour();
1160 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1162 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1164 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1165 return pItem
->Attr().GetFont();
1169 wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1171 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1173 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1174 pItem
->SetText(text
);
1175 pItem
->CalculateSize(this);
1179 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1181 wxTreeItemIcon which
)
1183 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1185 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1186 pItem
->SetImage(image
, which
);
1187 pItem
->CalculateSize(this);
1192 wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1194 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1197 data
->SetId( item
);
1199 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1202 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1204 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1206 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1207 pItem
->SetState(state
);
1208 pItem
->CalculateSize(this);
1212 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1214 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1216 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1217 pItem
->SetHasPlus(has
);
1221 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1223 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1225 // avoid redrawing the tree if no real change
1226 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1227 if ( pItem
->IsBold() != bold
)
1229 pItem
->SetBold(bold
);
1231 // recalculate the item size as bold and non bold fonts have different
1233 pItem
->CalculateSize(this);
1238 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1241 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1247 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1248 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1251 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1252 pItem
->Attr().SetTextColour(fg
);
1253 pItem
->Attr().SetBackgroundColour(bg
);
1257 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1258 const wxColour
& col
)
1260 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1262 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1263 pItem
->Attr().SetTextColour(col
);
1267 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1268 const wxColour
& col
)
1270 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1272 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1273 pItem
->Attr().SetBackgroundColour(col
);
1278 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1280 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1282 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1283 pItem
->Attr().SetFont(font
);
1284 pItem
->ResetTextSize();
1285 pItem
->CalculateSize(this);
1289 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1291 wxTreeCtrlBase::SetFont(font
);
1293 m_normalFont
= font
;
1294 m_boldFont
= m_normalFont
.Bold();
1297 m_anchor
->RecursiveResetTextSize();
1303 // -----------------------------------------------------------------------------
1304 // item status inquiries
1305 // -----------------------------------------------------------------------------
1307 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1309 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1311 // An item is only visible if it's not a descendant of a collapsed item
1312 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1313 wxGenericTreeItem
* parent
= pItem
->GetParent();
1316 if (!parent
->IsExpanded())
1318 parent
= parent
->GetParent();
1322 GetViewStart(& startX
, & startY
);
1324 wxSize clientSize
= GetClientSize();
1327 if (!GetBoundingRect(item
, rect
))
1329 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1331 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1333 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1339 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1341 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1343 // consider that the item does have children if it has the "+" button: it
1344 // might not have them (if it had never been expanded yet) but then it
1345 // could have them as well and it's better to err on this side rather than
1346 // disabling some operations which are restricted to the items with
1347 // children for an item which does have them
1348 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1351 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1353 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1355 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1358 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1360 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1362 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1365 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1367 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1369 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1372 // -----------------------------------------------------------------------------
1374 // -----------------------------------------------------------------------------
1376 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1378 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1380 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1383 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1384 wxTreeItemIdValue
& cookie
) const
1386 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1389 return GetNextChild(item
, cookie
);
1392 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1393 wxTreeItemIdValue
& cookie
) const
1395 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1397 wxArrayGenericTreeItems
&
1398 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1400 // it's ok to cast cookie to size_t, we never have indices big enough to
1401 // overflow "void *"
1402 size_t *pIndex
= (size_t *)&cookie
;
1403 if ( *pIndex
< children
.GetCount() )
1405 return children
.Item((*pIndex
)++);
1409 // there are no more of them
1410 return wxTreeItemId();
1414 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1416 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1418 wxArrayGenericTreeItems
&
1419 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1420 return children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last());
1423 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1425 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1427 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1428 wxGenericTreeItem
*parent
= i
->GetParent();
1429 if ( parent
== NULL
)
1431 // root item doesn't have any siblings
1432 return wxTreeItemId();
1435 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1436 int index
= siblings
.Index(i
);
1437 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1439 size_t n
= (size_t)(index
+ 1);
1440 return n
== siblings
.GetCount() ? wxTreeItemId()
1441 : wxTreeItemId(siblings
[n
]);
1444 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1446 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1448 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1449 wxGenericTreeItem
*parent
= i
->GetParent();
1450 if ( parent
== NULL
)
1452 // root item doesn't have any siblings
1453 return wxTreeItemId();
1456 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1457 int index
= siblings
.Index(i
);
1458 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1460 return index
== 0 ? wxTreeItemId()
1461 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1464 // Only for internal use right now, but should probably be public
1465 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1467 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1469 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1471 // First see if there are any children.
1472 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1473 if (children
.GetCount() > 0)
1475 return children
.Item(0);
1479 // Try a sibling of this or ancestor instead
1480 wxTreeItemId p
= item
;
1481 wxTreeItemId toFind
;
1484 toFind
= GetNextSibling(p
);
1485 p
= GetItemParent(p
);
1486 } while (p
.IsOk() && !toFind
.IsOk());
1491 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1493 wxTreeItemId itemid
= GetRootItem();
1499 if (IsVisible(itemid
))
1501 itemid
= GetNext(itemid
);
1502 } while (itemid
.IsOk());
1504 return wxTreeItemId();
1507 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1509 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1510 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1512 wxTreeItemId id
= item
;
1515 while (id
= GetNext(id
), id
.IsOk())
1521 return wxTreeItemId();
1524 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1526 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1527 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1529 // find out the starting point
1530 wxTreeItemId prevItem
= GetPrevSibling(item
);
1531 if ( !prevItem
.IsOk() )
1533 prevItem
= GetItemParent(item
);
1536 // find the first visible item after it
1537 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1539 prevItem
= GetNext(prevItem
);
1540 if ( !prevItem
.IsOk() || prevItem
== item
)
1542 // there are no visible items before item
1543 return wxTreeItemId();
1547 // from there we must be able to navigate until this item
1548 while ( prevItem
.IsOk() )
1550 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1551 if ( !nextItem
.IsOk() || nextItem
== item
)
1554 prevItem
= nextItem
;
1560 // called by wxTextTreeCtrl when it marks itself for deletion
1561 void wxGenericTreeCtrl::ResetTextControl()
1566 void wxGenericTreeCtrl::ResetFindState()
1568 m_findPrefix
.clear();
1573 // find the first item starting with the given prefix after the given item
1574 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1575 const wxString
& prefixOrig
) const
1577 // match is case insensitive as this is more convenient to the user: having
1578 // to press Shift-letter to go to the item starting with a capital letter
1579 // would be too bothersome
1580 wxString prefix
= prefixOrig
.Lower();
1582 // determine the starting point: we shouldn't take the current item (this
1583 // allows to switch between two items starting with the same letter just by
1584 // pressing it) but we shouldn't jump to the next one if the user is
1585 // continuing to type as otherwise he might easily skip the item he wanted
1586 wxTreeItemId itemid
= idParent
;
1587 if ( prefix
.length() == 1 )
1589 itemid
= GetNext(itemid
);
1592 // look for the item starting with the given prefix after it
1593 while ( itemid
.IsOk() && !GetItemText(itemid
).Lower().StartsWith(prefix
) )
1595 itemid
= GetNext(itemid
);
1598 // if we haven't found anything...
1599 if ( !itemid
.IsOk() )
1601 // ... wrap to the beginning
1602 itemid
= GetRootItem();
1603 if ( HasFlag(wxTR_HIDE_ROOT
) )
1605 // can't select virtual root
1606 itemid
= GetNext(itemid
);
1609 // and try all the items (stop when we get to the one we started from)
1610 while ( itemid
.IsOk() && itemid
!= idParent
&&
1611 !GetItemText(itemid
).Lower().StartsWith(prefix
) )
1613 itemid
= GetNext(itemid
);
1615 // If we haven't found the item but wrapped back to the one we started
1616 // from, id.IsOk() must be false
1617 if ( itemid
== idParent
)
1619 itemid
= wxTreeItemId();
1626 // -----------------------------------------------------------------------------
1628 // -----------------------------------------------------------------------------
1630 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1632 const wxString
& text
,
1635 wxTreeItemData
*data
)
1637 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1640 // should we give a warning here?
1641 return AddRoot(text
, image
, selImage
, data
);
1644 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1646 wxGenericTreeItem
*item
=
1647 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1651 data
->m_pItem
= item
;
1654 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1657 InvalidateBestSize();
1661 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1664 wxTreeItemData
*data
)
1666 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
1668 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1670 m_anchor
= new wxGenericTreeItem(NULL
, text
,
1671 image
, selImage
, data
);
1674 data
->m_pItem
= m_anchor
;
1677 if (HasFlag(wxTR_HIDE_ROOT
))
1679 // if root is hidden, make sure we can navigate
1681 m_anchor
->SetHasPlus();
1683 CalculatePositions();
1686 if (!HasFlag(wxTR_MULTIPLE
))
1688 m_current
= m_key_current
= m_anchor
;
1689 m_current
->SetHilight( true );
1692 InvalidateBestSize();
1696 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1697 const wxTreeItemId
& idPrevious
,
1698 const wxString
& text
,
1699 int image
, int selImage
,
1700 wxTreeItemData
*data
)
1702 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1705 // should we give a warning here?
1706 return AddRoot(text
, image
, selImage
, data
);
1710 if (idPrevious
.IsOk())
1712 index
= parent
->GetChildren().Index(
1713 (wxGenericTreeItem
*) idPrevious
.m_pItem
);
1714 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1715 "previous item in wxGenericTreeCtrl::InsertItem() "
1716 "is not a sibling" );
1719 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1723 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1725 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1726 GetEventHandler()->ProcessEvent( event
);
1729 // Don't leave edit or selection on a child which is about to disappear
1730 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1732 if ( m_textCtrl
&& item
!= m_textCtrl
->item() &&
1733 IsDescendantOf(item
, m_textCtrl
->item()) )
1735 m_textCtrl
->EndEdit( true );
1738 if ( item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
) )
1740 m_key_current
= NULL
;
1743 if ( IsDescendantOf(item
, m_select_me
) )
1748 if ( item
!= m_current
&& IsDescendantOf(item
, m_current
) )
1750 m_current
->SetHilight( false );
1756 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1758 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1760 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1761 ChildrenClosing(item
);
1762 item
->DeleteChildren(this);
1763 InvalidateBestSize();
1766 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1768 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1770 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1772 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1774 // can't delete the item being edited, cancel editing it first
1775 m_textCtrl
->EndEdit( true );
1778 wxGenericTreeItem
*parent
= item
->GetParent();
1780 // if the selected item will be deleted, select the parent ...
1781 wxGenericTreeItem
*to_be_selected
= parent
;
1784 // .. unless there is a next sibling like wxMSW does it
1785 int pos
= parent
->GetChildren().Index( item
);
1786 if ((int)(parent
->GetChildren().GetCount()) > pos
+1)
1787 to_be_selected
= parent
->GetChildren().Item( pos
+1 );
1790 // don't keep stale pointers around!
1791 if ( IsDescendantOf(item
, m_key_current
) )
1793 // Don't silently change the selection:
1794 // do it properly in idle time, so event
1795 // handlers get called.
1797 // m_key_current = parent;
1798 m_key_current
= NULL
;
1801 // m_select_me records whether we need to select
1802 // a different item, in idle time.
1803 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1805 m_select_me
= to_be_selected
;
1808 if ( IsDescendantOf(item
, m_current
) )
1810 // Don't silently change the selection:
1811 // do it properly in idle time, so event
1812 // handlers get called.
1814 // m_current = parent;
1816 m_select_me
= to_be_selected
;
1819 // remove the item from the tree
1822 parent
->GetChildren().Remove( item
); // remove by value
1824 else // deleting the root
1826 // nothing will be left in the tree
1830 // and delete all of its children and the item itself now
1831 item
->DeleteChildren(this);
1832 SendDeleteEvent(item
);
1834 if (item
== m_select_me
)
1839 InvalidateBestSize();
1842 void wxGenericTreeCtrl::DeleteAllItems()
1850 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1852 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1854 wxCHECK_RET( item
, wxT("invalid item in wxGenericTreeCtrl::Expand") );
1855 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1856 wxT("can't expand hidden root") );
1858 if ( !item
->HasPlus() )
1861 if ( item
->IsExpanded() )
1864 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1866 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1868 // cancelled by program
1875 CalculatePositions();
1877 RefreshSubtree(item
);
1884 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1885 GetEventHandler()->ProcessEvent( event
);
1888 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1890 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1891 wxT("can't collapse hidden root") );
1893 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1895 if ( !item
->IsExpanded() )
1898 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1899 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1901 // cancelled by program
1905 ChildrenClosing(item
);
1908 #if 0 // TODO why should items be collapsed recursively?
1909 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1910 size_t count
= children
.GetCount();
1911 for ( size_t n
= 0; n
< count
; n
++ )
1913 Collapse(children
[n
]);
1917 CalculatePositions();
1919 RefreshSubtree(item
);
1921 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1922 GetEventHandler()->ProcessEvent( event
);
1925 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1928 DeleteChildren(item
);
1931 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1933 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1935 if (item
->IsExpanded())
1941 void wxGenericTreeCtrl::Unselect()
1945 m_current
->SetHilight( false );
1946 RefreshLine( m_current
);
1953 void wxGenericTreeCtrl::ClearFocusedItem()
1955 wxTreeItemId item
= GetFocusedItem();
1957 SelectItem(item
, false);
1960 void wxGenericTreeCtrl::SetFocusedItem(const wxTreeItemId
& item
)
1962 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1964 SelectItem(item
, true);
1967 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1969 if (item
->IsSelected())
1971 item
->SetHilight(false);
1975 if (item
->HasChildren())
1977 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1978 size_t count
= children
.GetCount();
1979 for ( size_t n
= 0; n
< count
; ++n
)
1981 UnselectAllChildren(children
[n
]);
1986 void wxGenericTreeCtrl::UnselectAll()
1988 wxTreeItemId rootItem
= GetRootItem();
1990 // the tree might not have the root item at all
1993 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1997 void wxGenericTreeCtrl::SelectChildren(const wxTreeItemId
& parent
)
1999 wxCHECK_RET( HasFlag(wxTR_MULTIPLE
),
2000 "this only works with multiple selection controls" );
2004 if ( !HasChildren(parent
) )
2008 wxArrayGenericTreeItems
&
2009 children
= ((wxGenericTreeItem
*) parent
.m_pItem
)->GetChildren();
2010 size_t count
= children
.GetCount();
2013 item
= (wxGenericTreeItem
*) ((wxTreeItemId
)children
[0]).m_pItem
;
2014 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2015 event
.m_itemOld
= m_current
;
2017 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2020 for ( size_t n
= 0; n
< count
; ++n
)
2022 m_current
= m_key_current
= children
[n
];
2023 m_current
->SetHilight(true);
2028 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2029 GetEventHandler()->ProcessEvent( event
);
2033 // Recursive function !
2034 // To stop we must have crt_item<last_item
2036 // Tag all next children, when no more children,
2037 // Move to parent (not to tag)
2038 // Keep going... if we found last_item, we stop.
2040 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
,
2041 wxGenericTreeItem
*last_item
,
2044 wxGenericTreeItem
*parent
= crt_item
->GetParent();
2046 if (parent
== NULL
) // This is root item
2047 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
2049 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
2050 int index
= children
.Index(crt_item
);
2051 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
2053 size_t count
= children
.GetCount();
2054 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
2056 if ( TagAllChildrenUntilLast(children
[n
], last_item
, select
) )
2060 return TagNextChildren(parent
, last_item
, select
);
2064 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
,
2065 wxGenericTreeItem
*last_item
,
2068 crt_item
->SetHilight(select
);
2069 RefreshLine(crt_item
);
2071 if (crt_item
==last_item
)
2074 // We should leave the not shown children of collapsed items alone.
2075 if (crt_item
->HasChildren() && crt_item
->IsExpanded())
2077 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
2078 size_t count
= children
.GetCount();
2079 for ( size_t n
= 0; n
< count
; ++n
)
2081 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
2090 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
,
2091 wxGenericTreeItem
*item2
)
2095 // item2 is not necessary after item1
2096 // choice first' and 'last' between item1 and item2
2097 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
2098 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
2100 bool select
= m_current
->IsSelected();
2102 if ( TagAllChildrenUntilLast(first
,last
,select
) )
2105 TagNextChildren(first
,last
,select
);
2108 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
2109 bool unselect_others
,
2110 bool extended_select
)
2112 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2116 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2117 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2119 //wxCHECK_RET( ( (!unselect_others) && is_single),
2120 // wxT("this is a single selection tree") );
2122 // to keep going anyhow !!!
2125 if (item
->IsSelected())
2126 return; // nothing to do
2127 unselect_others
= true;
2128 extended_select
= false;
2130 else if ( unselect_others
&& item
->IsSelected() )
2132 // selection change if there is more than one item currently selected
2133 wxArrayTreeItemIds selected_items
;
2134 if ( GetSelections(selected_items
) == 1 )
2138 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2139 event
.m_itemOld
= m_current
;
2140 // TODO : Here we don't send any selection mode yet !
2142 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2145 wxTreeItemId parent
= GetItemParent( itemId
);
2146 while (parent
.IsOk())
2148 if (!IsExpanded(parent
))
2151 parent
= GetItemParent( parent
);
2155 if (unselect_others
)
2157 if (is_single
) Unselect(); // to speed up thing
2162 if (extended_select
)
2167 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
2170 // don't change the mark (m_current)
2171 SelectItemRange(m_current
, item
);
2175 bool select
= true; // the default
2177 // Check if we need to toggle hilight (ctrl mode)
2178 if (!unselect_others
)
2179 select
=!item
->IsSelected();
2181 m_current
= m_key_current
= item
;
2182 m_current
->SetHilight(select
);
2183 RefreshLine( m_current
);
2186 // This can cause idle processing to select the root
2187 // if no item is selected, so it must be after the
2189 EnsureVisible( itemId
);
2191 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2192 GetEventHandler()->ProcessEvent( event
);
2195 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
2197 wxGenericTreeItem
* const item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2198 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
2202 if ( !item
->IsSelected() )
2203 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
2207 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2208 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2211 item
->SetHilight(false);
2214 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2215 GetEventHandler()->ProcessEvent( event
);
2219 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
2220 wxArrayTreeItemIds
&array
) const
2222 if ( item
->IsSelected() )
2223 array
.Add(wxTreeItemId(item
));
2225 if ( item
->HasChildren() )
2227 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2228 size_t count
= children
.GetCount();
2229 for ( size_t n
= 0; n
< count
; ++n
)
2230 FillArray(children
[n
], array
);
2234 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
2237 wxTreeItemId idRoot
= GetRootItem();
2238 if ( idRoot
.IsOk() )
2240 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
2242 //else: the tree is empty, so no selections
2244 return array
.GetCount();
2247 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2249 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2251 if (!item
.IsOk()) return;
2253 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2255 // first expand all parent branches
2256 wxGenericTreeItem
*parent
= gitem
->GetParent();
2258 if ( HasFlag(wxTR_HIDE_ROOT
) )
2260 while ( parent
&& parent
!= m_anchor
)
2263 parent
= parent
->GetParent();
2271 parent
= parent
->GetParent();
2275 //if (parent) CalculatePositions();
2280 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2285 // update the control before scrolling it
2288 #if defined( __WXMSW__ )
2290 #elif defined(__WXMAC__)
2292 DoDirtyProcessing();
2294 DoDirtyProcessing();
2298 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2300 int itemY
= gitem
->GetY();
2304 GetViewStart( &start_x
, &start_y
);
2306 const int clientHeight
= GetClientSize().y
;
2308 const int itemHeight
= GetLineHeight(gitem
) + 2;
2310 if ( itemY
+ itemHeight
> start_y
*PIXELS_PER_UNIT
+ clientHeight
)
2312 // need to scroll up by enough to show this item fully
2313 itemY
+= itemHeight
- clientHeight
;
2315 // because itemY below will be divided by PIXELS_PER_UNIT it may
2316 // be rounded down, with the result of the item still only being
2317 // partially visible, so make sure we are rounding up
2318 itemY
+= PIXELS_PER_UNIT
-1;
2321 else if ( itemY
> start_y
*PIXELS_PER_UNIT
)
2323 // item is already fully visible, don't do anything
2326 //else: scroll down to make this item the top one displayed
2328 Scroll(-1, itemY
/PIXELS_PER_UNIT
);
2331 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2332 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2334 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2335 wxGenericTreeItem
**item2
)
2337 wxCHECK_MSG( s_treeBeingSorted
, 0,
2338 "bug in wxGenericTreeCtrl::SortChildren()" );
2340 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2343 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2345 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2347 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2349 wxCHECK_RET( !s_treeBeingSorted
,
2350 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2352 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2353 if ( children
.GetCount() > 1 )
2357 s_treeBeingSorted
= this;
2358 children
.Sort(tree_ctrl_compare_func
);
2359 s_treeBeingSorted
= NULL
;
2361 //else: don't make the tree dirty as nothing changed
2364 void wxGenericTreeCtrl::CalculateLineHeight()
2366 wxClientDC
dc(this);
2367 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2369 if ( m_imageListNormal
)
2371 // Calculate a m_lineHeight value from the normal Image sizes.
2372 // May be toggle off. Then wxGenericTreeCtrl will spread when
2373 // necessary (which might look ugly).
2374 int n
= m_imageListNormal
->GetImageCount();
2375 for (int i
= 0; i
< n
; i
++)
2377 int width
= 0, height
= 0;
2378 m_imageListNormal
->GetSize(i
, width
, height
);
2379 if (height
> m_lineHeight
) m_lineHeight
= height
;
2383 if ( m_imageListState
)
2385 // Calculate a m_lineHeight value from the state Image sizes.
2386 // May be toggle off. Then wxGenericTreeCtrl will spread when
2387 // necessary (which might look ugly).
2388 int n
= m_imageListState
->GetImageCount();
2389 for (int i
= 0; i
< n
; i
++)
2391 int width
= 0, height
= 0;
2392 m_imageListState
->GetSize(i
, width
, height
);
2393 if (height
> m_lineHeight
) m_lineHeight
= height
;
2397 if (m_imageListButtons
)
2399 // Calculate a m_lineHeight value from the Button image sizes.
2400 // May be toggle off. Then wxGenericTreeCtrl will spread when
2401 // necessary (which might look ugly).
2402 int n
= m_imageListButtons
->GetImageCount();
2403 for (int i
= 0; i
< n
; i
++)
2405 int width
= 0, height
= 0;
2406 m_imageListButtons
->GetSize(i
, width
, height
);
2407 if (height
> m_lineHeight
) m_lineHeight
= height
;
2411 if (m_lineHeight
< 30)
2412 m_lineHeight
+= 2; // at least 2 pixels
2414 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2417 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2419 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2420 m_imageListNormal
= imageList
;
2421 m_ownsImageListNormal
= false;
2425 m_anchor
->RecursiveResetSize();
2427 // Don't do any drawing if we're setting the list to NULL,
2428 // since we may be in the process of deleting the tree control.
2430 CalculateLineHeight();
2433 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2435 if (m_ownsImageListState
) delete m_imageListState
;
2436 m_imageListState
= imageList
;
2437 m_ownsImageListState
= false;
2441 m_anchor
->RecursiveResetSize();
2443 // Don't do any drawing if we're setting the list to NULL,
2444 // since we may be in the process of deleting the tree control.
2446 CalculateLineHeight();
2449 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2451 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2452 m_imageListButtons
= imageList
;
2453 m_ownsImageListButtons
= false;
2457 m_anchor
->RecursiveResetSize();
2459 CalculateLineHeight();
2462 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2464 SetButtonsImageList(imageList
);
2465 m_ownsImageListButtons
= true;
2468 // -----------------------------------------------------------------------------
2470 // -----------------------------------------------------------------------------
2472 void wxGenericTreeCtrl::AdjustMyScrollbars()
2477 m_anchor
->GetSize( x
, y
, this );
2478 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2479 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2480 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2481 int y_pos
= GetScrollPos( wxVERTICAL
);
2482 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2483 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2488 SetScrollbars( 0, 0, 0, 0 );
2492 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2494 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2495 return item
->GetHeight();
2497 return m_lineHeight
;
2500 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2502 item
->SetFont(this, dc
);
2503 item
->CalculateSize(this, dc
);
2505 wxCoord text_h
= item
->GetTextHeight();
2507 int image_h
= 0, image_w
= 0;
2508 int image
= item
->GetCurrentImage();
2509 if ( image
!= NO_IMAGE
)
2511 if ( m_imageListNormal
)
2513 m_imageListNormal
->GetSize(image
, image_w
, image_h
);
2514 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2522 int state_h
= 0, state_w
= 0;
2523 int state
= item
->GetState();
2524 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2526 if ( m_imageListState
)
2528 m_imageListState
->GetSize(state
, state_w
, state_h
);
2530 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2532 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2536 state
= wxTREE_ITEMSTATE_NONE
;
2540 int total_h
= GetLineHeight(item
);
2541 bool drawItemBackground
= false,
2542 hasBgColour
= false;
2544 if ( item
->IsSelected() )
2546 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2547 drawItemBackground
= true;
2552 wxTreeItemAttr
* const attr
= item
->GetAttributes();
2553 if ( attr
&& attr
->HasBackgroundColour() )
2555 drawItemBackground
=
2557 colBg
= attr
->GetBackgroundColour();
2561 colBg
= GetBackgroundColour();
2563 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2566 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2568 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2572 GetVirtualSize(&w
, &h
);
2573 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2574 if (!item
->IsSelected())
2576 dc
.DrawRectangle(rect
);
2580 int flags
= wxCONTROL_SELECTED
;
2582 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2583 && IsControlActive( (ControlRef
)GetHandle() )
2586 flags
|= wxCONTROL_FOCUSED
;
2587 if ((item
== m_current
) && (m_hasFocus
))
2588 flags
|= wxCONTROL_CURRENT
;
2590 wxRendererNative::Get().
2591 DrawItemSelectionRect(this, dc
, rect
, flags
);
2594 else // no full row highlight
2596 if ( item
->IsSelected() &&
2597 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2599 // If it's selected, and there's an state image or normal image,
2600 // then we should take care to leave the area under the image
2601 // painted in the background colour.
2602 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2603 item
->GetY() + offset
,
2604 item
->GetWidth() - state_w
- image_w
+ 2,
2606 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2607 dc
.DrawRectangle( rect
);
2612 int flags
= wxCONTROL_SELECTED
;
2614 flags
|= wxCONTROL_FOCUSED
;
2615 if ((item
== m_current
) && (m_hasFocus
))
2616 flags
|= wxCONTROL_CURRENT
;
2617 wxRendererNative::Get().
2618 DrawItemSelectionRect(this, dc
, rect
, flags
);
2621 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2622 // don't allow backgrounds to be customized. Not drawing the background,
2623 // except for custom item backgrounds, works for both kinds of theme.
2624 else if (drawItemBackground
)
2626 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2627 item
->GetY() + offset
,
2628 item
->GetWidth() - state_w
- image_w
+ 2,
2632 dc
.DrawRectangle( rect
);
2634 else // no specific background colour
2639 int flags
= wxCONTROL_SELECTED
;
2641 flags
|= wxCONTROL_FOCUSED
;
2642 if ((item
== m_current
) && (m_hasFocus
))
2643 flags
|= wxCONTROL_CURRENT
;
2644 wxRendererNative::Get().
2645 DrawItemSelectionRect(this, dc
, rect
, flags
);
2650 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2652 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2653 m_imageListState
->Draw( state
, dc
,
2656 (total_h
> state_h
? (total_h
-state_h
)/2
2658 wxIMAGELIST_DRAW_TRANSPARENT
);
2659 dc
.DestroyClippingRegion();
2662 if ( image
!= NO_IMAGE
)
2664 dc
.SetClippingRegion(item
->GetX() + state_w
, item
->GetY(),
2666 m_imageListNormal
->Draw( image
, dc
,
2667 item
->GetX() + state_w
,
2669 (total_h
> image_h
? (total_h
-image_h
)/2
2671 wxIMAGELIST_DRAW_TRANSPARENT
);
2672 dc
.DestroyClippingRegion();
2675 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2676 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2677 dc
.DrawText( item
->GetText(),
2678 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2679 (wxCoord
)(item
->GetY() + extraH
));
2681 // restore normal font
2682 dc
.SetFont( m_normalFont
);
2684 if (item
== m_dndEffectItem
)
2686 dc
.SetPen( *wxBLACK_PEN
);
2687 // DnD visual effects
2688 switch (m_dndEffect
)
2692 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2693 int w
= item
->GetWidth() + 2;
2694 int h
= total_h
+ 2;
2695 dc
.DrawRectangle( item
->GetX() - 1, item
->GetY() - 1, w
, h
);
2700 int x
= item
->GetX(),
2702 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2707 int x
= item
->GetX(),
2710 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2720 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem
*item
,
2725 int x
= level
*m_indent
;
2726 if (!HasFlag(wxTR_HIDE_ROOT
))
2730 else if (level
== 0)
2732 // always expand hidden root
2734 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2735 int count
= children
.GetCount();
2741 PaintLevel(children
[n
], dc
, 1, y
);
2742 } while (++n
< count
);
2744 if ( !HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
)
2747 // draw line down to last child
2748 origY
+= GetLineHeight(children
[0])>>1;
2749 oldY
+= GetLineHeight(children
[n
-1])>>1;
2750 dc
.DrawLine(3, origY
, 3, oldY
);
2756 item
->SetX(x
+m_spacing
);
2759 int h
= GetLineHeight(item
);
2761 int y_mid
= y_top
+ (h
>>1);
2764 int exposed_x
= dc
.LogicalToDeviceX(0);
2765 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2767 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2771 // don't draw rect outline if we already have the
2772 // background color under Mac
2773 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2774 #endif // !__WXMAC__
2778 if ( item
->IsSelected()
2779 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2780 // On wxMac, if the tree doesn't have the focus we draw an empty
2781 // rectangle, so we want to make sure that the text is visible
2782 // against the normal background, not the highlightbackground, so
2783 // don't use the highlight text colour unless we have the focus.
2784 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2792 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2794 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT
);
2799 wxTreeItemAttr
*attr
= item
->GetAttributes();
2800 if (attr
&& attr
->HasTextColour())
2801 colText
= attr
->GetTextColour();
2803 colText
= GetForegroundColour();
2807 dc
.SetTextForeground(colText
);
2811 PaintItem(item
, dc
);
2813 if (HasFlag(wxTR_ROW_LINES
))
2815 // if the background colour is white, choose a
2816 // contrasting color for the lines
2817 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2818 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2819 dc
.DrawLine(0, y_top
, 10000, y_top
);
2820 dc
.DrawLine(0, y
, 10000, y
);
2823 // restore DC objects
2824 dc
.SetBrush(*wxWHITE_BRUSH
);
2825 dc
.SetPen(m_dottedPen
);
2826 dc
.SetTextForeground(*wxBLACK
);
2828 if ( !HasFlag(wxTR_NO_LINES
) )
2830 // draw the horizontal line here
2832 if (x
> (signed)m_indent
)
2833 x_start
-= m_indent
;
2834 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2836 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2839 // should the item show a button?
2840 if ( item
->HasPlus() && HasButtons() )
2842 if ( m_imageListButtons
)
2844 // draw the image button here
2847 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2848 : wxTreeItemIcon_Normal
;
2849 if ( item
->IsSelected() )
2850 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2852 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2853 int xx
= x
- image_w
/2;
2854 int yy
= y_mid
- image_h
/2;
2856 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2857 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2858 wxIMAGELIST_DRAW_TRANSPARENT
);
2860 else // no custom buttons
2862 static const int wImage
= 9;
2863 static const int hImage
= 9;
2866 if (item
->IsExpanded())
2867 flag
|= wxCONTROL_EXPANDED
;
2868 if (item
== m_underMouse
)
2869 flag
|= wxCONTROL_CURRENT
;
2871 wxRendererNative::Get().DrawTreeItemButton
2875 wxRect(x
- wImage
/2,
2884 if (item
->IsExpanded())
2886 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2887 int count
= children
.GetCount();
2894 PaintLevel(children
[n
], dc
, level
, y
);
2895 } while (++n
< count
);
2897 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2899 // draw line down to last child
2900 oldY
+= GetLineHeight(children
[n
-1])>>1;
2901 if (HasButtons()) y_mid
+= 5;
2903 // Only draw the portion of the line that is visible, in case
2905 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2906 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2907 yOrigin
= abs(yOrigin
);
2908 GetClientSize(&width
, &height
);
2910 // Move end points to the beginning/end of the view?
2911 if (y_mid
< yOrigin
)
2913 if (oldY
> yOrigin
+ height
)
2914 oldY
= yOrigin
+ height
;
2916 // after the adjustments if y_mid is larger than oldY then the
2917 // line isn't visible at all so don't draw anything
2919 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2925 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2929 if ( item
->HasPlus() )
2931 // it's a folder, indicate it by a border
2936 // draw a line under the drop target because the item will be
2938 DrawLine(item
, !m_dropEffectAboveItem
);
2941 SetCursor(*wxSTANDARD_CURSOR
);
2946 SetCursor(wxCURSOR_NO_ENTRY
);
2950 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2952 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2954 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2956 if (m_dndEffect
== NoEffect
)
2958 m_dndEffect
= BorderEffect
;
2959 m_dndEffectItem
= i
;
2963 m_dndEffect
= NoEffect
;
2964 m_dndEffectItem
= NULL
;
2967 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2968 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2969 RefreshRect( rect
);
2972 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2974 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2976 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2978 if (m_dndEffect
== NoEffect
)
2981 m_dndEffect
= BelowEffect
;
2983 m_dndEffect
= AboveEffect
;
2984 m_dndEffectItem
= i
;
2988 m_dndEffect
= NoEffect
;
2989 m_dndEffectItem
= NULL
;
2992 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2993 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2994 RefreshRect( rect
);
2997 // -----------------------------------------------------------------------------
2998 // wxWidgets callbacks
2999 // -----------------------------------------------------------------------------
3001 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
3004 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
3005 RefreshLine( m_current
);
3011 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3019 dc
.SetFont( m_normalFont
);
3020 dc
.SetPen( m_dottedPen
);
3022 // this is now done dynamically
3023 //if(GetImageList() == NULL)
3024 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3027 PaintLevel( m_anchor
, dc
, 0, y
);
3030 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
3039 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
3048 void wxGenericTreeCtrl::OnKeyDown( wxKeyEvent
&event
)
3050 // send a tree event
3051 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
3052 te
.m_evtKey
= event
;
3053 if ( GetEventHandler()->ProcessEvent( te
) )
3059 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
3061 if ( (m_current
== 0) || (m_key_current
== 0) )
3067 // how should the selection work for this event?
3068 bool is_multiple
, extended_select
, unselect_others
;
3069 EventFlagsToSelType(GetWindowStyleFlag(),
3072 is_multiple
, extended_select
, unselect_others
);
3074 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3076 if (event
.GetKeyCode() == WXK_RIGHT
)
3077 event
.m_keyCode
= WXK_LEFT
;
3078 else if (event
.GetKeyCode() == WXK_LEFT
)
3079 event
.m_keyCode
= WXK_RIGHT
;
3084 // * : Expand all/Collapse all
3085 // ' ' | return : activate
3086 // up : go up (not last children!)
3088 // left : go to parent
3089 // right : open if parent and go next
3090 // home : go to root
3091 // end : go to last item without opening parents
3092 // alnum : start or continue searching for the item with this prefix
3093 int keyCode
= event
.GetKeyCode();
3096 // Make the keys work as they do in the native control:
3098 // left => collapse if current item is expanded
3099 if (keyCode
== WXK_RIGHT
)
3103 else if (keyCode
== WXK_LEFT
&& IsExpanded(m_current
))
3113 if (m_current
->HasPlus() && !IsExpanded(m_current
))
3121 if ( !IsExpanded(m_current
) )
3124 ExpandAllChildren(m_current
);
3127 //else: fall through to Collapse() it
3131 if (IsExpanded(m_current
))
3133 Collapse(m_current
);
3139 // Use the item's bounding rectangle to determine position for
3142 GetBoundingRect(m_current
, ItemRect
, true);
3145 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
3146 // Use the left edge, vertical middle
3147 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
3149 ItemRect
.GetHeight() / 2);
3150 GetEventHandler()->ProcessEvent( eventMenu
);
3156 if ( !event
.HasModifiers() )
3159 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
3160 GetEventHandler()->ProcessEvent( eventAct
);
3163 // in any case, also generate the normal key event for this key,
3164 // even if we generated the ACTIVATED event above: this is what
3165 // wxMSW does and it makes sense because you might not want to
3166 // process ACTIVATED event at all and handle Space and Return
3167 // directly (and differently) which would be impossible otherwise
3171 // up goes to the previous sibling or to the last
3172 // of its children if it's expanded
3175 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
3178 prev
= GetItemParent( m_key_current
);
3179 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3181 break; // don't go to root if it is hidden
3185 wxTreeItemIdValue cookie
;
3186 wxTreeItemId current
= m_key_current
;
3187 // TODO: Huh? If we get here, we'd better be the first
3188 // child of our parent. How else could it be?
3189 if (current
== GetFirstChild( prev
, cookie
))
3191 // otherwise we return to where we came from
3195 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
3202 while ( IsExpanded(prev
) && HasChildren(prev
) )
3204 wxTreeItemId child
= GetLastChild(prev
);
3211 DoSelectItem( prev
, unselect_others
, extended_select
);
3212 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
3217 // left arrow goes to the parent
3220 wxTreeItemId prev
= GetItemParent( m_current
);
3221 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3223 // don't go to root if it is hidden
3224 prev
= GetPrevSibling( m_current
);
3228 DoSelectItem( prev
, unselect_others
, extended_select
);
3234 // this works the same as the down arrow except that we
3235 // also expand the item if it wasn't expanded yet
3236 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
3238 //else: don't try to expand hidden root item (which can be the
3239 // current one when the tree is empty)
3245 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
3247 wxTreeItemIdValue cookie
;
3248 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
3252 DoSelectItem( child
, unselect_others
, extended_select
);
3253 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
3257 wxTreeItemId next
= GetNextSibling( m_key_current
);
3260 wxTreeItemId current
= m_key_current
;
3261 while (current
.IsOk() && !next
)
3263 current
= GetItemParent( current
);
3264 if (current
) next
= GetNextSibling( current
);
3269 DoSelectItem( next
, unselect_others
, extended_select
);
3270 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
3276 // <End> selects the last visible tree item
3279 wxTreeItemId last
= GetRootItem();
3281 while ( last
.IsOk() && IsExpanded(last
) )
3283 wxTreeItemId lastChild
= GetLastChild(last
);
3285 // it may happen if the item was expanded but then all of
3286 // its children have been deleted - so IsExpanded() returned
3287 // true, but GetLastChild() returned invalid item
3296 DoSelectItem( last
, unselect_others
, extended_select
);
3301 // <Home> selects the root item
3304 wxTreeItemId prev
= GetRootItem();
3308 if ( HasFlag(wxTR_HIDE_ROOT
) )
3310 wxTreeItemIdValue cookie
;
3311 prev
= GetFirstChild(prev
, cookie
);
3316 DoSelectItem( prev
, unselect_others
, extended_select
);
3321 // do not use wxIsalnum() here
3322 if ( !event
.HasModifiers() &&
3323 ((keyCode
>= '0' && keyCode
<= '9') ||
3324 (keyCode
>= 'a' && keyCode
<= 'z') ||
3325 (keyCode
>= 'A' && keyCode
<= 'Z') ||
3328 // find the next item starting with the given prefix
3329 wxChar ch
= (wxChar
)keyCode
;
3332 // if the same character is typed multiple times then go to the
3333 // next entry starting with that character instead of searching
3334 // for an item starting with multiple copies of this character,
3335 // this is more useful and is how it works under Windows.
3336 if ( m_findPrefix
.length() == 1 && m_findPrefix
[0] == ch
)
3338 id
= FindItem(m_current
, ch
);
3342 const wxString
newPrefix(m_findPrefix
+ ch
);
3343 id
= FindItem(m_current
, newPrefix
);
3345 m_findPrefix
= newPrefix
;
3348 // also start the timer to reset the current prefix if the user
3349 // doesn't press any more alnum keys soon -- we wouldn't want
3350 // to use this prefix for a new item search
3353 m_findTimer
= new wxTreeFindTimer(this);
3356 // Notice that we should start the timer even if we didn't find
3357 // anything to make sure we reset the search state later.
3358 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
3364 // Reset the bell flag if it had been temporarily disabled
3369 else // No such item
3371 // Signal it with a bell if enabled.
3372 if ( m_findBell
== 1 )
3376 // Disable it for the next unsuccessful match, we only
3377 // beep once, this is usually enough and continuing to
3378 // do it would be annoying.
3391 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
3396 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3397 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3398 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3399 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3400 if (flags
) return wxTreeItemId();
3402 if (m_anchor
== NULL
)
3404 flags
= wxTREE_HITTEST_NOWHERE
;
3405 return wxTreeItemId();
3408 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3412 flags
= wxTREE_HITTEST_NOWHERE
;
3413 return wxTreeItemId();
3418 // get the bounding rectangle of the item (or of its label only)
3419 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3421 bool textOnly
) const
3423 wxCHECK_MSG( item
.IsOk(), false,
3424 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3426 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3430 int image_h
= 0, image_w
= 0;
3431 int image
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetCurrentImage();
3432 if ( image
!= NO_IMAGE
&& m_imageListNormal
)
3434 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3435 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3438 int state_h
= 0, state_w
= 0;
3439 int state
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetState();
3440 if ( state
!= wxTREE_ITEMSTATE_NONE
&& m_imageListState
)
3442 m_imageListState
->GetSize( state
, state_w
, state_h
);
3444 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
3446 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3449 rect
.x
= i
->GetX() + state_w
+ image_w
;
3450 rect
.width
= i
->GetWidth() - state_w
- image_w
;
3453 else // the entire line
3456 rect
.width
= GetClientSize().x
;
3460 rect
.height
= GetLineHeight(i
);
3462 // we have to return the logical coordinates, not physical ones
3463 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3468 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3469 wxClassInfo
* WXUNUSED(textCtrlClass
))
3471 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("can't edit an invalid item") );
3473 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3475 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3476 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3482 // We have to call this here because the label in
3483 // question might just have been added and no screen
3484 // update taken place.
3486 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3489 DoDirtyProcessing();
3492 // TODO: use textCtrlClass here to create the control of correct class
3493 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3495 m_textCtrl
->SetFocus();
3500 // returns a pointer to the text edit control if the item is being
3501 // edited, NULL otherwise (it's assumed that no more than one item may
3502 // be edited simultaneously)
3503 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3508 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3509 bool discardChanges
)
3511 wxCHECK_RET( m_textCtrl
, wxT("not editing label") );
3513 m_textCtrl
->EndEdit(discardChanges
);
3516 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3517 const wxString
& value
)
3519 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3521 le
.m_editCancelled
= false;
3523 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3526 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3528 // let owner know that the edit was cancelled
3529 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3530 le
.m_label
= wxEmptyString
;
3531 le
.m_editCancelled
= true;
3533 GetEventHandler()->ProcessEvent( le
);
3536 void wxGenericTreeCtrl::OnRenameTimer()
3538 EditLabel( m_current
);
3541 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3543 if ( !m_anchor
)return;
3545 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3547 // Is the mouse over a tree item button?
3549 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3550 wxGenericTreeItem
*underMouse
= thisItem
;
3552 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3553 #endif // wxUSE_TOOLTIPS
3556 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3557 (!event
.LeftIsDown()) &&
3559 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3567 if (underMouse
!= m_underMouse
)
3571 // unhighlight old item
3572 wxGenericTreeItem
*tmp
= m_underMouse
;
3573 m_underMouse
= NULL
;
3577 m_underMouse
= underMouse
;
3579 RefreshLine( m_underMouse
);
3583 // Determines what item we are hovering over and need a tooltip for
3584 wxTreeItemId hoverItem
= thisItem
;
3586 // We do not want a tooltip if we are dragging, or if the rename timer is
3588 if ( underMouseChanged
&&
3591 (!m_renameTimer
|| !m_renameTimer
->IsRunning()) )
3593 // Ask the tree control what tooltip (if any) should be shown
3595 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3597 if ( GetEventHandler()->ProcessEvent(hevent
) )
3599 // If the user permitted the tooltip change, update it, otherwise
3600 // remove any old tooltip we might have.
3601 if ( hevent
.IsAllowed() )
3602 SetToolTip(hevent
.m_label
);
3609 // we process left mouse up event (enables in-place edit), middle/right down
3610 // (pass to the user code), left dbl click (activate item) and
3611 // dragging/moving events for items drag-and-drop
3612 if ( !(event
.LeftDown() ||
3614 event
.MiddleDown() ||
3615 event
.RightDown() ||
3616 event
.LeftDClick() ||
3618 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3627 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3629 if ( event
.Dragging() && !m_isDragging
)
3631 if (m_dragCount
== 0)
3636 if (m_dragCount
!= 3)
3638 // wait until user drags a bit further...
3642 wxEventType command
= event
.RightIsDown()
3643 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3644 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3646 wxTreeEvent
nevent(command
, this, m_current
);
3647 nevent
.SetPoint(CalcScrolledPosition(pt
));
3649 // by default the dragging is not supported, the user code must
3650 // explicitly allow the event for it to take place
3653 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3655 // we're going to drag this item
3656 m_isDragging
= true;
3658 // remember the old cursor because we will change it while
3660 m_oldCursor
= m_cursor
;
3662 // in a single selection control, hide the selection temporarily
3663 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3665 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3667 if ( m_oldSelection
)
3669 m_oldSelection
->SetHilight(false);
3670 RefreshLine(m_oldSelection
);
3677 else if ( event
.Dragging() )
3679 if ( item
!= m_dropTarget
)
3681 // unhighlight the previous drop target
3682 DrawDropEffect(m_dropTarget
);
3684 m_dropTarget
= item
;
3686 // highlight the current drop target if any
3687 DrawDropEffect(m_dropTarget
);
3689 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3692 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3693 // instead (needs to be tested!)
3698 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3702 // erase the highlighting
3703 DrawDropEffect(m_dropTarget
);
3705 if ( m_oldSelection
)
3707 m_oldSelection
->SetHilight(true);
3708 RefreshLine(m_oldSelection
);
3709 m_oldSelection
= NULL
;
3712 // generate the drag end event
3713 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3715 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3717 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3719 m_isDragging
= false;
3720 m_dropTarget
= NULL
;
3722 SetCursor(m_oldCursor
);
3724 #if defined( __WXMSW__ ) || defined(__WXMAC__) || defined(__WXGTK20__)
3727 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3728 // instead (needs to be tested!)
3734 // If we got to this point, we are not dragging or moving the mouse.
3735 // Because the code in carbon/toplevel.cpp will only set focus to the
3736 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3738 // We skip even if we didn't hit an item because we still should
3739 // restore focus to the tree control even if we didn't exactly hit an
3741 if ( event
.LeftDown() )
3746 // here we process only the messages which happen on tree items
3750 if (item
== NULL
) return; /* we hit the blank area */
3752 if ( event
.RightDown() )
3754 // If the item is already selected, do not update the selection.
3755 // Multi-selections should not be cleared if a selected item is
3757 if (!IsSelected(item
))
3759 DoSelectItem(item
, true, false);
3763 nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3764 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3765 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3767 // Consistent with MSW (for now), send the ITEM_MENU *after*
3768 // the RIGHT_CLICK event. TODO: This behaviour may change.
3769 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3770 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3771 GetEventHandler()->ProcessEvent(nevent2
);
3773 else if ( event
.MiddleDown() )
3776 nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3777 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3778 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3780 else if ( event
.LeftUp() )
3782 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3785 nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, this, item
);
3786 GetEventHandler()->ProcessEvent(nevent
);
3789 // this facilitates multiple-item drag-and-drop
3791 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3793 wxArrayTreeItemIds selections
;
3794 size_t count
= GetSelections(selections
);
3800 DoSelectItem(item
, true, false);
3806 if ( (item
== m_current
) &&
3807 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3808 HasFlag(wxTR_EDIT_LABELS
) )
3810 if ( m_renameTimer
)
3812 if ( m_renameTimer
->IsRunning() )
3813 m_renameTimer
->Stop();
3817 m_renameTimer
= new wxTreeRenameTimer( this );
3820 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3823 m_lastOnSame
= false;
3826 else // !RightDown() && !MiddleDown() && !LeftUp()
3828 // ==> LeftDown() || LeftDClick()
3829 if ( event
.LeftDown() )
3831 // If we click on an already selected item but do it to return
3832 // the focus to the control, it shouldn't start editing the
3833 // item label because it's too easy to start editing
3834 // accidentally (and also because nobody else does it like
3835 // this). So only set this flag, used to decide whether we
3836 // should start editing the label later, if we already have
3838 m_lastOnSame
= item
== m_current
&& HasFocus();
3841 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3843 // only toggle the item for a single click, double click on
3844 // the button doesn't do anything (it toggles the item twice)
3845 if ( event
.LeftDown() )
3850 // don't select the item if the button was clicked
3855 // clear the previously selected items, if the
3856 // user clicked outside of the present selection.
3857 // otherwise, perform the deselection on mouse-up.
3858 // this allows multiple drag and drop to work.
3859 // but if Cmd is down, toggle selection of the clicked item
3860 if (!IsSelected(item
) || event
.CmdDown())
3862 // how should the selection work for this event?
3863 bool is_multiple
, extended_select
, unselect_others
;
3864 EventFlagsToSelType(GetWindowStyleFlag(),
3871 DoSelectItem(item
, unselect_others
, extended_select
);
3875 // For some reason, Windows isn't recognizing a left double-click,
3876 // so we need to simulate it here. Allow 200 milliseconds for now.
3877 if ( event
.LeftDClick() )
3879 // double clicking should not start editing the item label
3880 if ( m_renameTimer
)
3881 m_renameTimer
->Stop();
3883 m_lastOnSame
= false;
3885 // send activate event first
3887 nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3888 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3889 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3891 // if the user code didn't process the activate event,
3892 // handle it ourselves by toggling the item when it is
3894 if ( item
->HasPlus() )
3904 void wxGenericTreeCtrl::OnInternalIdle()
3906 wxWindow::OnInternalIdle();
3908 // Check if we need to select the root item
3909 // because nothing else has been selected.
3910 // Delaying it means that we can invoke event handlers
3911 // as required, when a first item is selected.
3912 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3915 SelectItem(m_select_me
);
3916 else if (GetRootItem().IsOk())
3917 SelectItem(GetRootItem());
3920 // after all changes have been done to the tree control,
3921 // actually redraw the tree when everything is over
3923 DoDirtyProcessing();
3927 wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem
*item
,
3932 int x
= level
*m_indent
;
3933 if (!HasFlag(wxTR_HIDE_ROOT
))
3937 else if (level
== 0)
3939 // a hidden root is not evaluated, but its
3940 // children are always calculated
3944 item
->CalculateSize(this, dc
);
3947 item
->SetX( x
+m_spacing
);
3949 y
+= GetLineHeight(item
);
3951 if ( !item
->IsExpanded() )
3953 // we don't need to calculate collapsed branches
3958 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3959 size_t n
, count
= children
.GetCount();
3961 for (n
= 0; n
< count
; ++n
)
3962 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3965 void wxGenericTreeCtrl::CalculatePositions()
3967 if ( !m_anchor
) return;
3969 wxClientDC
dc(this);
3972 dc
.SetFont( m_normalFont
);
3974 dc
.SetPen( m_dottedPen
);
3975 //if(GetImageList() == NULL)
3976 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3979 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3982 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3985 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3988 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3990 if (m_dirty
|| IsFrozen() )
3993 wxSize client
= GetClientSize();
3996 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3997 rect
.width
= client
.x
;
3998 rect
.height
= client
.y
;
4000 Refresh(true, &rect
);
4002 AdjustMyScrollbars();
4005 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
4007 if (m_dirty
|| IsFrozen() )
4011 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
4012 rect
.width
= GetClientSize().x
;
4013 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
4015 Refresh(true, &rect
);
4018 void wxGenericTreeCtrl::RefreshSelected()
4023 // TODO: this is awfully inefficient, we should keep the list of all
4024 // selected items internally, should be much faster
4026 RefreshSelectedUnder(m_anchor
);
4029 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
4034 if ( item
->IsSelected() )
4037 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
4038 size_t count
= children
.GetCount();
4039 for ( size_t n
= 0; n
< count
; n
++ )
4041 RefreshSelectedUnder(children
[n
]);
4045 void wxGenericTreeCtrl::DoThaw()
4047 wxTreeCtrlBase::DoThaw();
4050 DoDirtyProcessing();
4055 // ----------------------------------------------------------------------------
4056 // changing colours: we need to refresh the tree control
4057 // ----------------------------------------------------------------------------
4059 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
4061 if ( !wxWindow::SetBackgroundColour(colour
) )
4069 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
4071 if ( !wxWindow::SetForegroundColour(colour
) )
4079 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
4082 wxTreeItemId itemId
= event
.GetItem();
4083 const wxGenericTreeItem
* const pItem
= (wxGenericTreeItem
*)itemId
.m_pItem
;
4085 // Check if the item fits into the client area:
4086 if ( pItem
->GetX() + pItem
->GetWidth() > GetClientSize().x
)
4088 // If it doesn't, show its full text in the tooltip.
4089 event
.SetLabel(pItem
->GetText());
4092 #endif // wxUSE_TOOLTIPS
4094 // veto processing the event, nixing any tooltip
4100 // NOTE: If using the wxListBox visual attributes works everywhere then this can
4101 // be removed, as well as the #else case below.
4102 #define _USE_VISATTR 0
4107 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
4109 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
4113 // Use the same color scheme as wxListBox
4114 return wxListBox::GetClassDefaultAttributes(variant
);
4116 wxVisualAttributes attr
;
4117 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
4118 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
4119 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
4124 void wxGenericTreeCtrl::DoDirtyProcessing()
4131 CalculatePositions();
4133 AdjustMyScrollbars();
4136 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
4138 // make sure all positions are calculated as normally this only done during
4139 // idle time but we need them for base class DoGetBestSize() to return the
4141 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
4143 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
4145 // there seems to be an implicit extra border around the items, although
4146 // I'm not really sure where does it come from -- but without this, the
4147 // scrollbars appear in a tree with default/best size
4150 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
4151 // scrollbars still appear
4152 const wxSize
& borderSize
= GetWindowBorderSize();
4154 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
4156 size
.x
+= PIXELS_PER_UNIT
- dx
;
4157 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
4159 size
.y
+= PIXELS_PER_UNIT
- dy
;
4161 // we need to update the cache too as the base class cached its own value
4162 CacheBestSize(size
);
4167 #endif // wxUSE_TREECTRL