1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treectlg.cpp
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
8 // Copyright: (c) 1998 Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/listbox.h"
36 #include "wx/textctrl.h"
39 #include "wx/generic/treectlg.h"
40 #include "wx/imaglist.h"
42 #include "wx/renderer.h"
45 #include "wx/osx/private.h"
48 // -----------------------------------------------------------------------------
50 // -----------------------------------------------------------------------------
52 class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem
;
54 WX_DEFINE_ARRAY_PTR(wxGenericTreeItem
*, wxArrayGenericTreeItems
);
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 static const int NO_IMAGE
= -1;
62 static const int PIXELS_PER_UNIT
= 10;
64 // the margin between the item state image and the item normal image
65 static const int MARGIN_BETWEEN_STATE_AND_IMAGE
= 2;
67 // the margin between the item image and the item text
68 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT
= 4;
70 // -----------------------------------------------------------------------------
72 // -----------------------------------------------------------------------------
74 // timer used for enabling in-place edit
75 class WXDLLEXPORT wxTreeRenameTimer
: public wxTimer
78 // start editing the current item after half a second (if the mouse hasn't
79 // been clicked/moved)
82 wxTreeRenameTimer( wxGenericTreeCtrl
*owner
);
84 virtual void Notify();
87 wxGenericTreeCtrl
*m_owner
;
89 wxDECLARE_NO_COPY_CLASS(wxTreeRenameTimer
);
92 // control used for in-place edit
93 class WXDLLEXPORT wxTreeTextCtrl
: public wxTextCtrl
96 wxTreeTextCtrl(wxGenericTreeCtrl
*owner
, wxGenericTreeItem
*item
);
98 void EndEdit( bool discardChanges
);
100 const wxGenericTreeItem
* item() const { return m_itemEdited
; }
103 void OnChar( wxKeyEvent
&event
);
104 void OnKeyUp( wxKeyEvent
&event
);
105 void OnKillFocus( wxFocusEvent
&event
);
107 bool AcceptChanges();
108 void Finish( bool setfocus
);
111 wxGenericTreeCtrl
*m_owner
;
112 wxGenericTreeItem
*m_itemEdited
;
113 wxString m_startValue
;
114 bool m_aboutToFinish
;
116 DECLARE_EVENT_TABLE()
117 wxDECLARE_NO_COPY_CLASS(wxTreeTextCtrl
);
120 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
121 // for a sufficiently long time
122 class WXDLLEXPORT wxTreeFindTimer
: public wxTimer
125 // reset the current prefix after half a second of inactivity
126 enum { DELAY
= 500 };
128 wxTreeFindTimer( wxGenericTreeCtrl
*owner
) { m_owner
= owner
; }
130 virtual void Notify() { m_owner
->m_findPrefix
.clear(); }
133 wxGenericTreeCtrl
*m_owner
;
135 wxDECLARE_NO_COPY_CLASS(wxTreeFindTimer
);
139 class WXDLLEXPORT wxGenericTreeItem
150 wxGenericTreeItem( wxGenericTreeItem
*parent
,
151 const wxString
& text
,
154 wxTreeItemData
*data
);
156 ~wxGenericTreeItem();
159 wxArrayGenericTreeItems
& GetChildren() { return m_children
; }
161 const wxString
& GetText() const { return m_text
; }
162 int GetImage(wxTreeItemIcon which
= wxTreeItemIcon_Normal
) const
163 { return m_images
[which
]; }
164 wxTreeItemData
*GetData() const { return m_data
; }
165 int GetState() const { return m_state
; }
167 // returns the current image for the item (depending on its
168 // selected/expanded/whatever state)
169 int GetCurrentImage() const;
171 void SetText(const wxString
& text
)
178 void SetImage(int image
, wxTreeItemIcon which
)
180 m_images
[which
] = image
;
184 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
185 void SetState(int state
) { m_state
= state
; m_width
= 0; }
187 void SetHasPlus(bool has
= true) { m_hasPlus
= has
; }
189 void SetBold(bool bold
)
196 int GetX() const { return m_x
; }
197 int GetY() const { return m_y
; }
199 void SetX(int x
) { m_x
= x
; }
200 void SetY(int y
) { m_y
= y
; }
202 int GetHeight() const { return m_height
; }
203 int GetWidth() const { return m_width
; }
205 int GetTextHeight() const
207 wxASSERT_MSG( m_heightText
!= -1, "must call CalculateSize() first" );
212 int GetTextWidth() const
214 wxASSERT_MSG( m_widthText
!= -1, "must call CalculateSize() first" );
219 wxGenericTreeItem
*GetParent() const { return m_parent
; }
221 // sets the items font for the specified DC if it uses any special font or
222 // simply returns false otherwise
223 bool SetFont(wxGenericTreeCtrl
*control
, wxDC
& dc
) const
227 wxTreeItemAttr
* const attr
= GetAttributes();
228 if ( attr
&& attr
->HasFont() )
229 font
= attr
->GetFont();
231 font
= control
->m_boldFont
;
241 // deletes all children notifying the treectrl about it
242 void DeleteChildren(wxGenericTreeCtrl
*tree
);
244 // get count of all children (and grand children if 'recursively')
245 size_t GetChildrenCount(bool recursively
= true) const;
247 void Insert(wxGenericTreeItem
*child
, size_t index
)
248 { m_children
.Insert(child
, index
); }
250 // calculate and cache the item size using either the provided DC (which is
251 // supposed to have wxGenericTreeCtrl::m_normalFont selected into it!) or a
252 // wxClientDC on the control window
253 void CalculateSize(wxGenericTreeCtrl
*control
, wxDC
& dc
)
254 { DoCalculateSize(control
, dc
, true /* dc uses normal font */); }
255 void CalculateSize(wxGenericTreeCtrl
*control
);
257 void GetSize( int &x
, int &y
, const wxGenericTreeCtrl
* );
259 void ResetSize() { m_width
= 0; }
260 void ResetTextSize() { m_width
= 0; m_widthText
= -1; }
261 void RecursiveResetSize();
262 void RecursiveResetTextSize();
264 // return the item at given position (or NULL if no item), onButton is
265 // true if the point belongs to the item's button, otherwise it lies
266 // on the item's label
267 wxGenericTreeItem
*HitTest( const wxPoint
& point
,
268 const wxGenericTreeCtrl
*,
272 void Expand() { m_isCollapsed
= false; }
273 void Collapse() { m_isCollapsed
= true; }
275 void SetHilight( bool set
= true ) { m_hasHilight
= set
; }
278 bool HasChildren() const { return !m_children
.IsEmpty(); }
279 bool IsSelected() const { return m_hasHilight
!= 0; }
280 bool IsExpanded() const { return !m_isCollapsed
; }
281 bool HasPlus() const { return m_hasPlus
|| HasChildren(); }
282 bool IsBold() const { return m_isBold
!= 0; }
285 // get them - may be NULL
286 wxTreeItemAttr
*GetAttributes() const { return m_attr
; }
287 // get them ensuring that the pointer is not NULL
288 wxTreeItemAttr
& Attr()
292 m_attr
= new wxTreeItemAttr
;
298 void SetAttributes(wxTreeItemAttr
*attr
)
300 if ( m_ownsAttr
) delete m_attr
;
306 // set them and delete when done
307 void AssignAttributes(wxTreeItemAttr
*attr
)
316 // calculate the size of this item, i.e. set m_width, m_height and
317 // m_widthText and m_heightText properly
319 // if dcUsesNormalFont is true, the current dc font must be the normal tree
321 void DoCalculateSize(wxGenericTreeCtrl
*control
,
323 bool dcUsesNormalFont
);
325 // since there can be very many of these, we save size by chosing
326 // the smallest representation for the elements and by ordering
327 // the members to avoid padding.
328 wxString m_text
; // label to be rendered for item
332 wxTreeItemData
*m_data
; // user-provided data
334 int m_state
; // item state
336 wxArrayGenericTreeItems m_children
; // list of children
337 wxGenericTreeItem
*m_parent
; // parent of this item
339 wxTreeItemAttr
*m_attr
; // attributes???
341 // tree ctrl images for the normal, selected, expanded and
342 // expanded+selected states
343 int m_images
[wxTreeItemIcon_Max
];
345 wxCoord m_x
; // (virtual) offset from top
346 wxCoord m_y
; // (virtual) offset from left
347 int m_width
; // width of this item
348 int m_height
; // height of this item
350 // use bitfields to save size
351 unsigned int m_isCollapsed
:1;
352 unsigned int m_hasHilight
:1; // same as focused
353 unsigned int m_hasPlus
:1; // used for item which doesn't have
354 // children but has a [+] button
355 unsigned int m_isBold
:1; // render the label in bold font
356 unsigned int m_ownsAttr
:1; // delete attribute when done
358 wxDECLARE_NO_COPY_CLASS(wxGenericTreeItem
);
361 // =============================================================================
363 // =============================================================================
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 // translate the key or mouse event flags to the type of selection we're
371 static void EventFlagsToSelType(long style
,
375 bool &extended_select
,
376 bool &unselect_others
)
378 is_multiple
= (style
& wxTR_MULTIPLE
) != 0;
379 extended_select
= shiftDown
&& is_multiple
;
380 unselect_others
= !(extended_select
|| (ctrlDown
&& is_multiple
));
383 // check if the given item is under another one
385 IsDescendantOf(const wxGenericTreeItem
*parent
, const wxGenericTreeItem
*item
)
389 if ( item
== parent
)
391 // item is a descendant of parent
395 item
= item
->GetParent();
401 // -----------------------------------------------------------------------------
402 // wxTreeRenameTimer (internal)
403 // -----------------------------------------------------------------------------
405 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl
*owner
)
410 void wxTreeRenameTimer::Notify()
412 m_owner
->OnRenameTimer();
415 //-----------------------------------------------------------------------------
416 // wxTreeTextCtrl (internal)
417 //-----------------------------------------------------------------------------
419 BEGIN_EVENT_TABLE(wxTreeTextCtrl
,wxTextCtrl
)
420 EVT_CHAR (wxTreeTextCtrl::OnChar
)
421 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp
)
422 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus
)
425 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl
*owner
,
426 wxGenericTreeItem
*item
)
427 : m_itemEdited(item
), m_startValue(item
->GetText())
430 m_aboutToFinish
= false;
433 m_owner
->GetBoundingRect(m_itemEdited
, rect
, true);
435 // corrects position and size for better appearance
439 #elif defined(__WXGTK__)
444 #elif defined(__WXMAC__)
445 int bestHeight
= GetBestSize().y
- 8;
446 if ( rect
.height
> bestHeight
)
448 int diff
= rect
.height
- bestHeight
;
454 (void)Create(m_owner
, wxID_ANY
, m_startValue
,
455 rect
.GetPosition(), rect
.GetSize());
457 SetSelection(-1, -1);
460 void wxTreeTextCtrl::EndEdit(bool discardChanges
)
462 m_aboutToFinish
= true;
464 if ( discardChanges
)
466 m_owner
->OnRenameCancelled(m_itemEdited
);
472 // Notify the owner about the changes
475 // Even if vetoed, close the control (consistent with MSW)
480 bool wxTreeTextCtrl::AcceptChanges()
482 const wxString value
= GetValue();
484 if ( value
== m_startValue
)
486 // nothing changed, always accept
487 // when an item remains unchanged, the owner
488 // needs to be notified that the user decided
489 // not to change the tree item label, and that
490 // the edit has been cancelled
492 m_owner
->OnRenameCancelled(m_itemEdited
);
496 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
498 // vetoed by the user
502 // accepted, do rename the item
503 m_owner
->SetItemText(m_itemEdited
, value
);
508 void wxTreeTextCtrl::Finish( bool setfocus
)
510 m_owner
->ResetTextControl();
512 wxPendingDelete
.Append(this);
518 void wxTreeTextCtrl::OnChar( wxKeyEvent
&event
)
520 switch ( event
.m_keyCode
)
535 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent
&event
)
537 if ( !m_aboutToFinish
)
539 // auto-grow the textctrl:
540 wxSize parentSize
= m_owner
->GetSize();
541 wxPoint myPos
= GetPosition();
542 wxSize mySize
= GetSize();
544 GetTextExtent(GetValue() + _T("M"), &sx
, &sy
);
545 if (myPos
.x
+ sx
> parentSize
.x
)
546 sx
= parentSize
.x
- myPos
.x
;
549 SetSize(sx
, wxDefaultCoord
);
555 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent
&event
)
557 if ( !m_aboutToFinish
)
559 if ( !AcceptChanges() )
560 m_owner
->OnRenameCancelled( m_itemEdited
);
565 // We should let the native text control handle focus, too.
569 // -----------------------------------------------------------------------------
571 // -----------------------------------------------------------------------------
573 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem
*parent
,
574 const wxString
& text
,
575 int image
, int selImage
,
576 wxTreeItemData
*data
)
579 m_images
[wxTreeItemIcon_Normal
] = image
;
580 m_images
[wxTreeItemIcon_Selected
] = selImage
;
581 m_images
[wxTreeItemIcon_Expanded
] = NO_IMAGE
;
582 m_images
[wxTreeItemIcon_SelectedExpanded
] = NO_IMAGE
;
585 m_state
= wxTREE_ITEMSTATE_NONE
;
588 m_isCollapsed
= true;
589 m_hasHilight
= false;
598 // We don't know the height here yet.
606 wxGenericTreeItem::~wxGenericTreeItem()
610 if (m_ownsAttr
) delete m_attr
;
612 wxASSERT_MSG( m_children
.IsEmpty(),
613 "must call DeleteChildren() before deleting the item" );
616 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl
*tree
)
618 size_t count
= m_children
.GetCount();
619 for ( size_t n
= 0; n
< count
; n
++ )
621 wxGenericTreeItem
*child
= m_children
[n
];
622 tree
->SendDeleteEvent(child
);
624 child
->DeleteChildren(tree
);
625 if ( child
== tree
->m_select_me
)
626 tree
->m_select_me
= NULL
;
633 size_t wxGenericTreeItem::GetChildrenCount(bool recursively
) const
635 size_t count
= m_children
.GetCount();
639 size_t total
= count
;
640 for (size_t n
= 0; n
< count
; ++n
)
642 total
+= m_children
[n
]->GetChildrenCount();
648 void wxGenericTreeItem::GetSize( int &x
, int &y
,
649 const wxGenericTreeCtrl
*theButton
)
651 int bottomY
=m_y
+theButton
->GetLineHeight(this);
652 if ( y
< bottomY
) y
= bottomY
;
653 int width
= m_x
+ m_width
;
654 if ( x
< width
) x
= width
;
658 size_t count
= m_children
.GetCount();
659 for ( size_t n
= 0; n
< count
; ++n
)
661 m_children
[n
]->GetSize( x
, y
, theButton
);
666 wxGenericTreeItem
*wxGenericTreeItem::HitTest(const wxPoint
& point
,
667 const wxGenericTreeCtrl
*theCtrl
,
671 // for a hidden root node, don't evaluate it, but do evaluate children
672 if ( !(level
== 0 && theCtrl
->HasFlag(wxTR_HIDE_ROOT
)) )
675 int h
= theCtrl
->GetLineHeight(this);
676 if ((point
.y
> m_y
) && (point
.y
< m_y
+ h
))
678 int y_mid
= m_y
+ h
/2;
679 if (point
.y
< y_mid
)
680 flags
|= wxTREE_HITTEST_ONITEMUPPERPART
;
682 flags
|= wxTREE_HITTEST_ONITEMLOWERPART
;
684 int xCross
= m_x
- theCtrl
->GetSpacing();
686 // according to the drawing code the triangels are drawn
687 // at -4 , -4 from the position up to +10/+10 max
688 if ((point
.x
> xCross
-4) && (point
.x
< xCross
+10) &&
689 (point
.y
> y_mid
-4) && (point
.y
< y_mid
+10) &&
690 HasPlus() && theCtrl
->HasButtons() )
692 // 5 is the size of the plus sign
693 if ((point
.x
> xCross
-6) && (point
.x
< xCross
+6) &&
694 (point
.y
> y_mid
-6) && (point
.y
< y_mid
+6) &&
695 HasPlus() && theCtrl
->HasButtons() )
698 flags
|= wxTREE_HITTEST_ONITEMBUTTON
;
702 if ((point
.x
>= m_x
) && (point
.x
<= m_x
+m_width
))
707 // assuming every image (normal and selected) has the same size!
708 if ( (GetImage() != NO_IMAGE
) && theCtrl
->m_imageListNormal
)
710 theCtrl
->m_imageListNormal
->GetSize(GetImage(),
717 if ( (GetState() != wxTREE_ITEMSTATE_NONE
) &&
718 theCtrl
->m_imageListState
)
720 theCtrl
->m_imageListState
->GetSize(GetState(),
724 if ((state_w
!= -1) && (point
.x
<= m_x
+ state_w
+ 1))
725 flags
|= wxTREE_HITTEST_ONITEMSTATEICON
;
726 else if ((image_w
!= -1) &&
728 (state_w
!= -1 ? state_w
+
729 MARGIN_BETWEEN_STATE_AND_IMAGE
732 flags
|= wxTREE_HITTEST_ONITEMICON
;
734 flags
|= wxTREE_HITTEST_ONITEMLABEL
;
740 flags
|= wxTREE_HITTEST_ONITEMINDENT
;
741 if (point
.x
> m_x
+m_width
)
742 flags
|= wxTREE_HITTEST_ONITEMRIGHT
;
747 // if children are expanded, fall through to evaluate them
748 if (m_isCollapsed
) return NULL
;
752 size_t count
= m_children
.GetCount();
753 for ( size_t n
= 0; n
< count
; n
++ )
755 wxGenericTreeItem
*res
= m_children
[n
]->HitTest( point
,
766 int wxGenericTreeItem::GetCurrentImage() const
768 int image
= NO_IMAGE
;
773 image
= GetImage(wxTreeItemIcon_SelectedExpanded
);
776 if ( image
== NO_IMAGE
)
778 // we usually fall back to the normal item, but try just the
779 // expanded one (and not selected) first in this case
780 image
= GetImage(wxTreeItemIcon_Expanded
);
786 image
= GetImage(wxTreeItemIcon_Selected
);
789 // maybe it doesn't have the specific image we want,
790 // try the default one instead
791 if ( image
== NO_IMAGE
) image
= GetImage();
796 void wxGenericTreeItem::CalculateSize(wxGenericTreeCtrl
* control
)
798 // check if we need to do anything before creating the DC
802 wxClientDC
dc(control
);
803 DoCalculateSize(control
, dc
, false /* normal font not used */);
807 wxGenericTreeItem::DoCalculateSize(wxGenericTreeCtrl
* control
,
809 bool dcUsesNormalFont
)
811 if ( m_width
!= 0 ) // Size known, nothing to do
814 if ( m_widthText
== -1 )
817 if ( SetFont(control
, dc
) )
821 else // we have no special font
823 if ( !dcUsesNormalFont
)
825 // but we do need to ensure that the normal font is used: notice
826 // that this doesn't count as changing the font as we don't need
828 dc
.SetFont(control
->m_normalFont
);
834 dc
.GetTextExtent( GetText(), &m_widthText
, &m_heightText
);
836 // restore normal font if the DC used it previously and we changed it
838 dc
.SetFont(control
->m_normalFont
);
841 int text_h
= m_heightText
+ 2;
843 int image_h
= 0, image_w
= 0;
844 int image
= GetCurrentImage();
845 if ( image
!= NO_IMAGE
&& control
->m_imageListNormal
)
847 control
->m_imageListNormal
->GetSize(image
, image_w
, image_h
);
848 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
851 int state_h
= 0, state_w
= 0;
852 int state
= GetState();
853 if ( state
!= wxTREE_ITEMSTATE_NONE
&& control
->m_imageListState
)
855 control
->m_imageListState
->GetSize(state
, state_w
, state_h
);
857 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
859 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
862 int img_h
= wxMax(state_h
, image_h
);
863 m_height
= wxMax(img_h
, text_h
);
866 m_height
+= 2; // at least 2 pixels
868 m_height
+= m_height
/ 10; // otherwise 10% extra spacing
870 if (m_height
> control
->m_lineHeight
)
871 control
->m_lineHeight
= m_height
;
873 m_width
= state_w
+ image_w
+ m_widthText
+ 2;
876 void wxGenericTreeItem::RecursiveResetSize()
880 const size_t count
= m_children
.Count();
881 for (size_t i
= 0; i
< count
; i
++ )
882 m_children
[i
]->RecursiveResetSize();
885 void wxGenericTreeItem::RecursiveResetTextSize()
890 const size_t count
= m_children
.Count();
891 for (size_t i
= 0; i
< count
; i
++ )
892 m_children
[i
]->RecursiveResetTextSize();
895 // -----------------------------------------------------------------------------
896 // wxGenericTreeCtrl implementation
897 // -----------------------------------------------------------------------------
899 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl
, wxControl
)
901 BEGIN_EVENT_TABLE(wxGenericTreeCtrl
, wxTreeCtrlBase
)
902 EVT_PAINT (wxGenericTreeCtrl::OnPaint
)
903 EVT_SIZE (wxGenericTreeCtrl::OnSize
)
904 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse
)
905 EVT_CHAR (wxGenericTreeCtrl::OnChar
)
906 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus
)
907 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus
)
908 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY
, wxGenericTreeCtrl::OnGetToolTip
)
911 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
913 * wxTreeCtrl has to be a real class or we have problems with
914 * the run-time information.
917 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxGenericTreeCtrl
)
920 // -----------------------------------------------------------------------------
921 // construction/destruction
922 // -----------------------------------------------------------------------------
924 void wxGenericTreeCtrl::Init()
937 m_hilightBrush
= new wxBrush
939 wxSystemSettings::GetColour
941 wxSYS_COLOUR_HIGHLIGHT
946 m_hilightUnfocusedBrush
= new wxBrush
948 wxSystemSettings::GetColour
950 wxSYS_COLOUR_BTNSHADOW
955 m_imageListButtons
= NULL
;
956 m_ownsImageListButtons
= false;
959 m_isDragging
= false;
960 m_dropTarget
= m_oldSelection
= NULL
;
964 m_renameTimer
= NULL
;
968 m_dropEffectAboveItem
= false;
970 m_dndEffect
= NoEffect
;
971 m_dndEffectItem
= NULL
;
973 m_lastOnSame
= false;
975 #if defined( __WXMAC__ )
976 m_normalFont
.CreateSystemFont(wxOSX_SYSTEM_FONT_VIEWS
);
978 m_normalFont
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
980 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
981 m_normalFont
.GetFamily(),
982 m_normalFont
.GetStyle(),
984 m_normalFont
.GetUnderlined(),
985 m_normalFont
.GetFaceName(),
986 m_normalFont
.GetEncoding());
989 bool wxGenericTreeCtrl::Create(wxWindow
*parent
,
994 const wxValidator
& validator
,
995 const wxString
& name
)
999 wxGetOsVersion(&major
, &minor
);
1002 style
|= wxTR_ROW_LINES
;
1004 if (style
& wxTR_HAS_BUTTONS
)
1005 style
|= wxTR_NO_LINES
;
1009 if (style
& wxTR_HAS_BUTTONS
)
1010 style
|= wxTR_NO_LINES
;
1013 if ( !wxControl::Create( parent
, id
, pos
, size
,
1014 style
|wxHSCROLL
|wxVSCROLL
,
1019 // If the tree display has no buttons, but does have
1020 // connecting lines, we can use a narrower layout.
1021 // It may not be a good idea to force this...
1022 if (!HasButtons() && !HasFlag(wxTR_NO_LINES
))
1028 wxVisualAttributes attr
= GetDefaultAttributes();
1029 SetOwnForegroundColour( attr
.colFg
);
1030 SetOwnBackgroundColour( attr
.colBg
);
1032 SetOwnFont(attr
.font
);
1034 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1035 // style because we apparently get performance problems when using dotted
1036 // pen for drawing in some ports -- but under MSW it seems to work fine
1038 m_dottedPen
= wxPen(*wxLIGHT_GREY
, 0, wxPENSTYLE_DOT
);
1040 m_dottedPen
= *wxGREY_PEN
;
1043 SetInitialSize(size
);
1048 wxGenericTreeCtrl::~wxGenericTreeCtrl()
1050 delete m_hilightBrush
;
1051 delete m_hilightUnfocusedBrush
;
1055 delete m_renameTimer
;
1058 if (m_ownsImageListButtons
)
1059 delete m_imageListButtons
;
1062 // -----------------------------------------------------------------------------
1064 // -----------------------------------------------------------------------------
1066 unsigned int wxGenericTreeCtrl::GetCount() const
1070 // the tree is empty
1074 unsigned int count
= m_anchor
->GetChildrenCount();
1075 if ( !HasFlag(wxTR_HIDE_ROOT
) )
1077 // take the root itself into account
1084 void wxGenericTreeCtrl::SetIndent(unsigned int indent
)
1086 m_indent
= (unsigned short) indent
;
1091 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
1092 bool recursively
) const
1094 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
1096 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildrenCount(recursively
);
1099 void wxGenericTreeCtrl::SetWindowStyle(const long styles
)
1101 // Do not try to expand the root node if it hasn't been created yet
1102 if (m_anchor
&& !HasFlag(wxTR_HIDE_ROOT
) && (styles
& wxTR_HIDE_ROOT
))
1104 // if we will hide the root, make sure children are visible
1105 m_anchor
->SetHasPlus();
1107 CalculatePositions();
1110 // right now, just sets the styles. Eventually, we may
1111 // want to update the inherited styles, but right now
1112 // none of the parents has updatable styles
1113 m_windowStyle
= styles
;
1117 // -----------------------------------------------------------------------------
1118 // functions to work with tree items
1119 // -----------------------------------------------------------------------------
1121 wxString
wxGenericTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
1123 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
1125 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetText();
1128 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1129 wxTreeItemIcon which
) const
1131 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1133 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetImage(which
);
1136 wxTreeItemData
*wxGenericTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1138 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1140 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetData();
1143 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId
& item
) const
1145 wxCHECK_MSG( item
.IsOk(), wxTREE_ITEMSTATE_NONE
, wxT("invalid tree item") );
1147 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1148 return pItem
->GetState();
1151 wxColour
wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1153 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1155 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1156 return pItem
->Attr().GetTextColour();
1160 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1162 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1164 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1165 return pItem
->Attr().GetBackgroundColour();
1168 wxFont
wxGenericTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1170 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1172 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1173 return pItem
->Attr().GetFont();
1177 wxGenericTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
1179 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1181 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1182 pItem
->SetText(text
);
1183 pItem
->CalculateSize(this);
1187 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId
& item
,
1189 wxTreeItemIcon which
)
1191 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1193 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1194 pItem
->SetImage(image
, which
);
1195 pItem
->CalculateSize(this);
1200 wxGenericTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1202 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1205 data
->SetId( item
);
1207 ((wxGenericTreeItem
*) item
.m_pItem
)->SetData(data
);
1210 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId
& item
, int state
)
1212 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1214 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1215 pItem
->SetState(state
);
1216 pItem
->CalculateSize(this);
1220 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1222 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1224 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1225 pItem
->SetHasPlus(has
);
1229 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1231 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1233 // avoid redrawing the tree if no real change
1234 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1235 if ( pItem
->IsBold() != bold
)
1237 pItem
->SetBold(bold
);
1239 // recalculate the item size as bold and non bold fonts have different
1241 pItem
->CalculateSize(this);
1246 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
,
1249 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1255 bg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
1256 fg
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1259 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1260 pItem
->Attr().SetTextColour(fg
);
1261 pItem
->Attr().SetBackgroundColour(bg
);
1265 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1266 const wxColour
& col
)
1268 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1270 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1271 pItem
->Attr().SetTextColour(col
);
1275 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1276 const wxColour
& col
)
1278 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1280 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1281 pItem
->Attr().SetBackgroundColour(col
);
1286 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1288 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1290 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1291 pItem
->Attr().SetFont(font
);
1292 pItem
->ResetTextSize();
1293 pItem
->CalculateSize(this);
1297 bool wxGenericTreeCtrl::SetFont( const wxFont
&font
)
1299 wxTreeCtrlBase::SetFont(font
);
1301 m_normalFont
= font
;
1302 m_boldFont
= wxFont(m_normalFont
.GetPointSize(),
1303 m_normalFont
.GetFamily(),
1304 m_normalFont
.GetStyle(),
1306 m_normalFont
.GetUnderlined(),
1307 m_normalFont
.GetFaceName(),
1308 m_normalFont
.GetEncoding());
1311 m_anchor
->RecursiveResetTextSize();
1317 // -----------------------------------------------------------------------------
1318 // item status inquiries
1319 // -----------------------------------------------------------------------------
1321 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1323 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1325 // An item is only visible if it's not a descendant of a collapsed item
1326 wxGenericTreeItem
*pItem
= (wxGenericTreeItem
*) item
.m_pItem
;
1327 wxGenericTreeItem
* parent
= pItem
->GetParent();
1330 if (!parent
->IsExpanded())
1332 parent
= parent
->GetParent();
1336 GetViewStart(& startX
, & startY
);
1338 wxSize clientSize
= GetClientSize();
1341 if (!GetBoundingRect(item
, rect
))
1343 if (rect
.GetWidth() == 0 || rect
.GetHeight() == 0)
1345 if (rect
.GetBottom() < 0 || rect
.GetTop() > clientSize
.y
)
1347 if (rect
.GetRight() < 0 || rect
.GetLeft() > clientSize
.x
)
1353 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1355 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1357 // consider that the item does have children if it has the "+" button: it
1358 // might not have them (if it had never been expanded yet) but then it
1359 // could have them as well and it's better to err on this side rather than
1360 // disabling some operations which are restricted to the items with
1361 // children for an item which does have them
1362 return ((wxGenericTreeItem
*) item
.m_pItem
)->HasPlus();
1365 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1367 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1369 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsExpanded();
1372 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1374 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1376 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsSelected();
1379 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1381 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1383 return ((wxGenericTreeItem
*) item
.m_pItem
)->IsBold();
1386 // -----------------------------------------------------------------------------
1388 // -----------------------------------------------------------------------------
1390 wxTreeItemId
wxGenericTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1392 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1394 return ((wxGenericTreeItem
*) item
.m_pItem
)->GetParent();
1397 wxTreeItemId
wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1398 wxTreeItemIdValue
& cookie
) const
1400 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1403 return GetNextChild(item
, cookie
);
1406 wxTreeItemId
wxGenericTreeCtrl::GetNextChild(const wxTreeItemId
& item
,
1407 wxTreeItemIdValue
& cookie
) const
1409 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1411 wxArrayGenericTreeItems
&
1412 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1414 // it's ok to cast cookie to size_t, we never have indices big enough to
1415 // overflow "void *"
1416 size_t *pIndex
= (size_t *)&cookie
;
1417 if ( *pIndex
< children
.GetCount() )
1419 return children
.Item((*pIndex
)++);
1423 // there are no more of them
1424 return wxTreeItemId();
1428 wxTreeItemId
wxGenericTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1430 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1432 wxArrayGenericTreeItems
&
1433 children
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetChildren();
1434 return children
.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children
.Last());
1437 wxTreeItemId
wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1439 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1441 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1442 wxGenericTreeItem
*parent
= i
->GetParent();
1443 if ( parent
== NULL
)
1445 // root item doesn't have any siblings
1446 return wxTreeItemId();
1449 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1450 int index
= siblings
.Index(i
);
1451 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1453 size_t n
= (size_t)(index
+ 1);
1454 return n
== siblings
.GetCount() ? wxTreeItemId()
1455 : wxTreeItemId(siblings
[n
]);
1458 wxTreeItemId
wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1460 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1462 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1463 wxGenericTreeItem
*parent
= i
->GetParent();
1464 if ( parent
== NULL
)
1466 // root item doesn't have any siblings
1467 return wxTreeItemId();
1470 wxArrayGenericTreeItems
& siblings
= parent
->GetChildren();
1471 int index
= siblings
.Index(i
);
1472 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
1474 return index
== 0 ? wxTreeItemId()
1475 : wxTreeItemId(siblings
[(size_t)(index
- 1)]);
1478 // Only for internal use right now, but should probably be public
1479 wxTreeItemId
wxGenericTreeCtrl::GetNext(const wxTreeItemId
& item
) const
1481 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1483 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
1485 // First see if there are any children.
1486 wxArrayGenericTreeItems
& children
= i
->GetChildren();
1487 if (children
.GetCount() > 0)
1489 return children
.Item(0);
1493 // Try a sibling of this or ancestor instead
1494 wxTreeItemId p
= item
;
1495 wxTreeItemId toFind
;
1498 toFind
= GetNextSibling(p
);
1499 p
= GetItemParent(p
);
1500 } while (p
.IsOk() && !toFind
.IsOk());
1505 wxTreeItemId
wxGenericTreeCtrl::GetFirstVisibleItem() const
1507 wxTreeItemId id
= GetRootItem();
1516 } while (id
.IsOk());
1518 return wxTreeItemId();
1521 wxTreeItemId
wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1523 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1524 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1526 wxTreeItemId id
= item
;
1529 while (id
= GetNext(id
), id
.IsOk())
1535 return wxTreeItemId();
1538 wxTreeItemId
wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1540 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1541 wxASSERT_MSG( IsVisible(item
), wxT("this item itself should be visible") );
1543 // find out the starting point
1544 wxTreeItemId prevItem
= GetPrevSibling(item
);
1545 if ( !prevItem
.IsOk() )
1547 prevItem
= GetItemParent(item
);
1550 // find the first visible item after it
1551 while ( prevItem
.IsOk() && !IsVisible(prevItem
) )
1553 prevItem
= GetNext(prevItem
);
1554 if ( !prevItem
.IsOk() || prevItem
== item
)
1556 // there are no visible items before item
1557 return wxTreeItemId();
1561 // from there we must be able to navigate until this item
1562 while ( prevItem
.IsOk() )
1564 const wxTreeItemId nextItem
= GetNextVisible(prevItem
);
1565 if ( !nextItem
.IsOk() || nextItem
== item
)
1568 prevItem
= nextItem
;
1574 // called by wxTextTreeCtrl when it marks itself for deletion
1575 void wxGenericTreeCtrl::ResetTextControl()
1580 // find the first item starting with the given prefix after the given item
1581 wxTreeItemId
wxGenericTreeCtrl::FindItem(const wxTreeItemId
& idParent
,
1582 const wxString
& prefixOrig
) const
1584 // match is case insensitive as this is more convenient to the user: having
1585 // to press Shift-letter to go to the item starting with a capital letter
1586 // would be too bothersome
1587 wxString prefix
= prefixOrig
.Lower();
1589 // determine the starting point: we shouldn't take the current item (this
1590 // allows to switch between two items starting with the same letter just by
1591 // pressing it) but we shouldn't jump to the next one if the user is
1592 // continuing to type as otherwise he might easily skip the item he wanted
1593 wxTreeItemId id
= idParent
;
1594 if ( prefix
.length() == 1 )
1599 // look for the item starting with the given prefix after it
1600 while ( id
.IsOk() && !GetItemText(id
).Lower().StartsWith(prefix
) )
1605 // if we haven't found anything...
1608 // ... wrap to the beginning
1610 if ( HasFlag(wxTR_HIDE_ROOT
) )
1612 // can't select virtual root
1616 // and try all the items (stop when we get to the one we started from)
1617 while ( id
.IsOk() && id
!= idParent
&&
1618 !GetItemText(id
).Lower().StartsWith(prefix
) )
1622 // If we haven't found the item, id.IsOk() will be false, as per
1629 // -----------------------------------------------------------------------------
1631 // -----------------------------------------------------------------------------
1633 wxTreeItemId
wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId
& parentId
,
1635 const wxString
& text
,
1638 wxTreeItemData
*data
)
1640 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1643 // should we give a warning here?
1644 return AddRoot(text
, image
, selImage
, data
);
1647 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1649 wxGenericTreeItem
*item
=
1650 new wxGenericTreeItem( parent
, text
, image
, selImage
, data
);
1654 data
->m_pItem
= item
;
1657 parent
->Insert( item
, previous
== (size_t)-1 ? parent
->GetChildren().size()
1660 InvalidateBestSize();
1664 wxTreeItemId
wxGenericTreeCtrl::AddRoot(const wxString
& text
,
1667 wxTreeItemData
*data
)
1669 wxCHECK_MSG( !m_anchor
, wxTreeItemId(), "tree can have only one root" );
1671 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1673 m_anchor
= new wxGenericTreeItem(NULL
, text
,
1674 image
, selImage
, data
);
1677 data
->m_pItem
= m_anchor
;
1680 if (HasFlag(wxTR_HIDE_ROOT
))
1682 // if root is hidden, make sure we can navigate
1684 m_anchor
->SetHasPlus();
1686 CalculatePositions();
1689 if (!HasFlag(wxTR_MULTIPLE
))
1691 m_current
= m_key_current
= m_anchor
;
1692 m_current
->SetHilight( true );
1695 InvalidateBestSize();
1699 wxTreeItemId
wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId
& parentId
,
1700 const wxTreeItemId
& idPrevious
,
1701 const wxString
& text
,
1702 int image
, int selImage
,
1703 wxTreeItemData
*data
)
1705 wxGenericTreeItem
*parent
= (wxGenericTreeItem
*) parentId
.m_pItem
;
1708 // should we give a warning here?
1709 return AddRoot(text
, image
, selImage
, data
);
1713 if (idPrevious
.IsOk())
1715 index
= parent
->GetChildren().Index(
1716 (wxGenericTreeItem
*) idPrevious
.m_pItem
);
1717 wxASSERT_MSG( index
!= wxNOT_FOUND
,
1718 "previous item in wxGenericTreeCtrl::InsertItem() "
1719 "is not a sibling" );
1722 return DoInsertItem(parentId
, (size_t)++index
, text
, image
, selImage
, data
);
1726 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem
*item
)
1728 wxTreeEvent
event(wxEVT_COMMAND_TREE_DELETE_ITEM
, this, item
);
1729 GetEventHandler()->ProcessEvent( event
);
1732 // Don't leave edit or selection on a child which is about to disappear
1733 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem
* item
)
1735 if ( m_textCtrl
&& item
!= m_textCtrl
->item() &&
1736 IsDescendantOf(item
, m_textCtrl
->item()) )
1738 m_textCtrl
->EndEdit( true );
1741 if ( item
!= m_key_current
&& IsDescendantOf(item
, m_key_current
) )
1743 m_key_current
= NULL
;
1746 if ( IsDescendantOf(item
, m_select_me
) )
1751 if ( item
!= m_current
&& IsDescendantOf(item
, m_current
) )
1753 m_current
->SetHilight( false );
1759 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId
& itemId
)
1761 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1763 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1764 ChildrenClosing(item
);
1765 item
->DeleteChildren(this);
1766 InvalidateBestSize();
1769 void wxGenericTreeCtrl::Delete(const wxTreeItemId
& itemId
)
1771 m_dirty
= true; // do this first so stuff below doesn't cause flicker
1773 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1775 if (m_textCtrl
!= NULL
&& IsDescendantOf(item
, m_textCtrl
->item()))
1777 // can't delete the item being edited, cancel editing it first
1778 m_textCtrl
->EndEdit( true );
1781 wxGenericTreeItem
*parent
= item
->GetParent();
1783 // if the selected item will be deleted, select the parent ...
1784 wxGenericTreeItem
*to_be_selected
= parent
;
1787 // .. unless there is a next sibling like wxMSW does it
1788 int pos
= parent
->GetChildren().Index( item
);
1789 if ((int)(parent
->GetChildren().GetCount()) > pos
+1)
1790 to_be_selected
= parent
->GetChildren().Item( pos
+1 );
1793 // don't keep stale pointers around!
1794 if ( IsDescendantOf(item
, m_key_current
) )
1796 // Don't silently change the selection:
1797 // do it properly in idle time, so event
1798 // handlers get called.
1800 // m_key_current = parent;
1801 m_key_current
= NULL
;
1804 // m_select_me records whether we need to select
1805 // a different item, in idle time.
1806 if ( m_select_me
&& IsDescendantOf(item
, m_select_me
) )
1808 m_select_me
= to_be_selected
;
1811 if ( IsDescendantOf(item
, m_current
) )
1813 // Don't silently change the selection:
1814 // do it properly in idle time, so event
1815 // handlers get called.
1817 // m_current = parent;
1819 m_select_me
= to_be_selected
;
1822 // remove the item from the tree
1825 parent
->GetChildren().Remove( item
); // remove by value
1827 else // deleting the root
1829 // nothing will be left in the tree
1833 // and delete all of its children and the item itself now
1834 item
->DeleteChildren(this);
1835 SendDeleteEvent(item
);
1837 if (item
== m_select_me
)
1842 InvalidateBestSize();
1845 void wxGenericTreeCtrl::DeleteAllItems()
1853 void wxGenericTreeCtrl::Expand(const wxTreeItemId
& itemId
)
1855 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1857 wxCHECK_RET( item
, _T("invalid item in wxGenericTreeCtrl::Expand") );
1858 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1859 _T("can't expand hidden root") );
1861 if ( !item
->HasPlus() )
1864 if ( item
->IsExpanded() )
1867 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_EXPANDING
, this, item
);
1869 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1871 // cancelled by program
1878 CalculatePositions();
1880 RefreshSubtree(item
);
1887 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED
);
1888 GetEventHandler()->ProcessEvent( event
);
1891 void wxGenericTreeCtrl::Collapse(const wxTreeItemId
& itemId
)
1893 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT
) || itemId
!= GetRootItem(),
1894 _T("can't collapse hidden root") );
1896 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1898 if ( !item
->IsExpanded() )
1901 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
, this, item
);
1902 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
1904 // cancelled by program
1908 ChildrenClosing(item
);
1911 #if 0 // TODO why should items be collapsed recursively?
1912 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1913 size_t count
= children
.GetCount();
1914 for ( size_t n
= 0; n
< count
; n
++ )
1916 Collapse(children
[n
]);
1920 CalculatePositions();
1922 RefreshSubtree(item
);
1924 event
.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
);
1925 GetEventHandler()->ProcessEvent( event
);
1928 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1931 DeleteChildren(item
);
1934 void wxGenericTreeCtrl::Toggle(const wxTreeItemId
& itemId
)
1936 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
1938 if (item
->IsExpanded())
1944 void wxGenericTreeCtrl::Unselect()
1948 m_current
->SetHilight( false );
1949 RefreshLine( m_current
);
1956 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem
*item
)
1958 if (item
->IsSelected())
1960 item
->SetHilight(false);
1964 if (item
->HasChildren())
1966 wxArrayGenericTreeItems
& children
= item
->GetChildren();
1967 size_t count
= children
.GetCount();
1968 for ( size_t n
= 0; n
< count
; ++n
)
1970 UnselectAllChildren(children
[n
]);
1975 void wxGenericTreeCtrl::UnselectAll()
1977 wxTreeItemId rootItem
= GetRootItem();
1979 // the tree might not have the root item at all
1982 UnselectAllChildren((wxGenericTreeItem
*) rootItem
.m_pItem
);
1986 // Recursive function !
1987 // To stop we must have crt_item<last_item
1989 // Tag all next children, when no more children,
1990 // Move to parent (not to tag)
1991 // Keep going... if we found last_item, we stop.
1993 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem
*crt_item
,
1994 wxGenericTreeItem
*last_item
,
1997 wxGenericTreeItem
*parent
= crt_item
->GetParent();
1999 if (parent
== NULL
) // This is root item
2000 return TagAllChildrenUntilLast(crt_item
, last_item
, select
);
2002 wxArrayGenericTreeItems
& children
= parent
->GetChildren();
2003 int index
= children
.Index(crt_item
);
2004 wxASSERT( index
!= wxNOT_FOUND
); // I'm not a child of my parent?
2006 size_t count
= children
.GetCount();
2007 for (size_t n
=(size_t)(index
+1); n
<count
; ++n
)
2009 if ( TagAllChildrenUntilLast(children
[n
], last_item
, select
) )
2013 return TagNextChildren(parent
, last_item
, select
);
2017 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem
*crt_item
,
2018 wxGenericTreeItem
*last_item
,
2021 crt_item
->SetHilight(select
);
2022 RefreshLine(crt_item
);
2024 if (crt_item
==last_item
)
2027 if (crt_item
->HasChildren())
2029 wxArrayGenericTreeItems
& children
= crt_item
->GetChildren();
2030 size_t count
= children
.GetCount();
2031 for ( size_t n
= 0; n
< count
; ++n
)
2033 if (TagAllChildrenUntilLast(children
[n
], last_item
, select
))
2042 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem
*item1
,
2043 wxGenericTreeItem
*item2
)
2047 // item2 is not necessary after item1
2048 // choice first' and 'last' between item1 and item2
2049 wxGenericTreeItem
*first
= (item1
->GetY()<item2
->GetY()) ? item1
: item2
;
2050 wxGenericTreeItem
*last
= (item1
->GetY()<item2
->GetY()) ? item2
: item1
;
2052 bool select
= m_current
->IsSelected();
2054 if ( TagAllChildrenUntilLast(first
,last
,select
) )
2057 TagNextChildren(first
,last
,select
);
2060 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId
& itemId
,
2061 bool unselect_others
,
2062 bool extended_select
)
2064 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2068 bool is_single
=!(GetWindowStyleFlag() & wxTR_MULTIPLE
);
2069 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2071 //wxCHECK_RET( ( (!unselect_others) && is_single),
2072 // wxT("this is a single selection tree") );
2074 // to keep going anyhow !!!
2077 if (item
->IsSelected())
2078 return; // nothing to do
2079 unselect_others
= true;
2080 extended_select
= false;
2082 else if ( unselect_others
&& item
->IsSelected() )
2084 // selection change if there is more than one item currently selected
2085 wxArrayTreeItemIds selected_items
;
2086 if ( GetSelections(selected_items
) == 1 )
2090 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2091 event
.m_itemOld
= m_current
;
2092 // TODO : Here we don't send any selection mode yet !
2094 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2097 wxTreeItemId parent
= GetItemParent( itemId
);
2098 while (parent
.IsOk())
2100 if (!IsExpanded(parent
))
2103 parent
= GetItemParent( parent
);
2107 if (unselect_others
)
2109 if (is_single
) Unselect(); // to speed up thing
2114 if (extended_select
)
2119 m_key_current
= (wxGenericTreeItem
*) GetRootItem().m_pItem
;
2122 // don't change the mark (m_current)
2123 SelectItemRange(m_current
, item
);
2127 bool select
= true; // the default
2129 // Check if we need to toggle hilight (ctrl mode)
2130 if (!unselect_others
)
2131 select
=!item
->IsSelected();
2133 m_current
= m_key_current
= item
;
2134 m_current
->SetHilight(select
);
2135 RefreshLine( m_current
);
2138 // This can cause idle processing to select the root
2139 // if no item is selected, so it must be after the
2141 EnsureVisible( itemId
);
2143 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2144 GetEventHandler()->ProcessEvent( event
);
2147 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId
& itemId
, bool select
)
2149 wxGenericTreeItem
* const item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2150 wxCHECK_RET( item
, wxT("SelectItem(): invalid tree item") );
2154 if ( !item
->IsSelected() )
2155 DoSelectItem(itemId
, !HasFlag(wxTR_MULTIPLE
));
2159 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
2160 if ( GetEventHandler()->ProcessEvent( event
) && !event
.IsAllowed() )
2163 item
->SetHilight(false);
2166 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2167 GetEventHandler()->ProcessEvent( event
);
2171 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem
*item
,
2172 wxArrayTreeItemIds
&array
) const
2174 if ( item
->IsSelected() )
2175 array
.Add(wxTreeItemId(item
));
2177 if ( item
->HasChildren() )
2179 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2180 size_t count
= children
.GetCount();
2181 for ( size_t n
= 0; n
< count
; ++n
)
2182 FillArray(children
[n
], array
);
2186 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds
&array
) const
2189 wxTreeItemId idRoot
= GetRootItem();
2190 if ( idRoot
.IsOk() )
2192 FillArray((wxGenericTreeItem
*) idRoot
.m_pItem
, array
);
2194 //else: the tree is empty, so no selections
2196 return array
.GetCount();
2199 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2201 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2203 if (!item
.IsOk()) return;
2205 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2207 // first expand all parent branches
2208 wxGenericTreeItem
*parent
= gitem
->GetParent();
2210 if ( HasFlag(wxTR_HIDE_ROOT
) )
2212 while ( parent
&& parent
!= m_anchor
)
2215 parent
= parent
->GetParent();
2223 parent
= parent
->GetParent();
2227 //if (parent) CalculatePositions();
2232 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId
&item
)
2234 if (!item
.IsOk()) return;
2236 // We have to call this here because the label in
2237 // question might just have been added and no screen
2238 // update taken place.
2240 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2243 DoDirtyProcessing();
2245 wxGenericTreeItem
*gitem
= (wxGenericTreeItem
*) item
.m_pItem
;
2247 // now scroll to the item
2248 int item_y
= gitem
->GetY();
2252 GetViewStart( &start_x
, &start_y
);
2253 start_y
*= PIXELS_PER_UNIT
;
2257 GetClientSize( &client_w
, &client_h
);
2259 if (item_y
< start_y
+3)
2264 m_anchor
->GetSize( x
, y
, this );
2265 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2266 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2267 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2268 // Item should appear at top
2269 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2270 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2271 x_pos
, item_y
/PIXELS_PER_UNIT
);
2273 else if (item_y
+GetLineHeight(gitem
) > start_y
+client_h
)
2278 m_anchor
->GetSize( x
, y
, this );
2279 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2280 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2281 item_y
+= PIXELS_PER_UNIT
+2;
2282 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2283 // Item should appear at bottom
2284 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2285 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2287 (item_y
+GetLineHeight(gitem
)-client_h
)/PIXELS_PER_UNIT
);
2291 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2292 static wxGenericTreeCtrl
*s_treeBeingSorted
= NULL
;
2294 static int LINKAGEMODE
tree_ctrl_compare_func(wxGenericTreeItem
**item1
,
2295 wxGenericTreeItem
**item2
)
2297 wxCHECK_MSG( s_treeBeingSorted
, 0,
2298 "bug in wxGenericTreeCtrl::SortChildren()" );
2300 return s_treeBeingSorted
->OnCompareItems(*item1
, *item2
);
2303 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId
& itemId
)
2305 wxCHECK_RET( itemId
.IsOk(), wxT("invalid tree item") );
2307 wxGenericTreeItem
*item
= (wxGenericTreeItem
*) itemId
.m_pItem
;
2309 wxCHECK_RET( !s_treeBeingSorted
,
2310 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2312 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2313 if ( children
.GetCount() > 1 )
2317 s_treeBeingSorted
= this;
2318 children
.Sort(tree_ctrl_compare_func
);
2319 s_treeBeingSorted
= NULL
;
2321 //else: don't make the tree dirty as nothing changed
2324 void wxGenericTreeCtrl::CalculateLineHeight()
2326 wxClientDC
dc(this);
2327 m_lineHeight
= (int)(dc
.GetCharHeight() + 4);
2329 if ( m_imageListNormal
)
2331 // Calculate a m_lineHeight value from the normal Image sizes.
2332 // May be toggle off. Then wxGenericTreeCtrl will spread when
2333 // necessary (which might look ugly).
2334 int n
= m_imageListNormal
->GetImageCount();
2335 for (int i
= 0; i
< n
; i
++)
2337 int width
= 0, height
= 0;
2338 m_imageListNormal
->GetSize(i
, width
, height
);
2339 if (height
> m_lineHeight
) m_lineHeight
= height
;
2343 if ( m_imageListState
)
2345 // Calculate a m_lineHeight value from the state Image sizes.
2346 // May be toggle off. Then wxGenericTreeCtrl will spread when
2347 // necessary (which might look ugly).
2348 int n
= m_imageListState
->GetImageCount();
2349 for (int i
= 0; i
< n
; i
++)
2351 int width
= 0, height
= 0;
2352 m_imageListState
->GetSize(i
, width
, height
);
2353 if (height
> m_lineHeight
) m_lineHeight
= height
;
2357 if (m_imageListButtons
)
2359 // Calculate a m_lineHeight value from the Button image sizes.
2360 // May be toggle off. Then wxGenericTreeCtrl will spread when
2361 // necessary (which might look ugly).
2362 int n
= m_imageListButtons
->GetImageCount();
2363 for (int i
= 0; i
< n
; i
++)
2365 int width
= 0, height
= 0;
2366 m_imageListButtons
->GetSize(i
, width
, height
);
2367 if (height
> m_lineHeight
) m_lineHeight
= height
;
2371 if (m_lineHeight
< 30)
2372 m_lineHeight
+= 2; // at least 2 pixels
2374 m_lineHeight
+= m_lineHeight
/10; // otherwise 10% extra spacing
2377 void wxGenericTreeCtrl::SetImageList(wxImageList
*imageList
)
2379 if (m_ownsImageListNormal
) delete m_imageListNormal
;
2380 m_imageListNormal
= imageList
;
2381 m_ownsImageListNormal
= false;
2385 m_anchor
->RecursiveResetSize();
2387 // Don't do any drawing if we're setting the list to NULL,
2388 // since we may be in the process of deleting the tree control.
2390 CalculateLineHeight();
2393 void wxGenericTreeCtrl::SetStateImageList(wxImageList
*imageList
)
2395 if (m_ownsImageListState
) delete m_imageListState
;
2396 m_imageListState
= imageList
;
2397 m_ownsImageListState
= false;
2401 m_anchor
->RecursiveResetSize();
2403 // Don't do any drawing if we're setting the list to NULL,
2404 // since we may be in the process of deleting the tree control.
2406 CalculateLineHeight();
2409 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList
*imageList
)
2411 if (m_ownsImageListButtons
) delete m_imageListButtons
;
2412 m_imageListButtons
= imageList
;
2413 m_ownsImageListButtons
= false;
2417 m_anchor
->RecursiveResetSize();
2419 CalculateLineHeight();
2422 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList
*imageList
)
2424 SetButtonsImageList(imageList
);
2425 m_ownsImageListButtons
= true;
2428 // -----------------------------------------------------------------------------
2430 // -----------------------------------------------------------------------------
2432 void wxGenericTreeCtrl::AdjustMyScrollbars()
2437 m_anchor
->GetSize( x
, y
, this );
2438 y
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2439 x
+= PIXELS_PER_UNIT
+2; // one more scrollbar unit + 2 pixels
2440 int x_pos
= GetScrollPos( wxHORIZONTAL
);
2441 int y_pos
= GetScrollPos( wxVERTICAL
);
2442 SetScrollbars( PIXELS_PER_UNIT
, PIXELS_PER_UNIT
,
2443 x
/PIXELS_PER_UNIT
, y
/PIXELS_PER_UNIT
,
2448 SetScrollbars( 0, 0, 0, 0 );
2452 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem
*item
) const
2454 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT
)
2455 return item
->GetHeight();
2457 return m_lineHeight
;
2460 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem
*item
, wxDC
& dc
)
2462 item
->SetFont(this, dc
);
2463 item
->CalculateSize(this, dc
);
2465 wxCoord text_h
= item
->GetTextHeight();
2467 int image_h
= 0, image_w
= 0;
2468 int image
= item
->GetCurrentImage();
2469 if ( image
!= NO_IMAGE
)
2471 if ( m_imageListNormal
)
2473 m_imageListNormal
->GetSize(image
, image_w
, image_h
);
2474 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2482 int state_h
= 0, state_w
= 0;
2483 int state
= item
->GetState();
2484 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2486 if ( m_imageListState
)
2488 m_imageListState
->GetSize(state
, state_w
, state_h
);
2490 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
2492 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
2496 state
= wxTREE_ITEMSTATE_NONE
;
2500 int total_h
= GetLineHeight(item
);
2501 bool drawItemBackground
= false,
2502 hasBgColour
= false;
2504 if ( item
->IsSelected() )
2506 dc
.SetBrush(*(m_hasFocus
? m_hilightBrush
: m_hilightUnfocusedBrush
));
2507 drawItemBackground
= true;
2512 wxTreeItemAttr
* const attr
= item
->GetAttributes();
2513 if ( attr
&& attr
->HasBackgroundColour() )
2515 drawItemBackground
=
2517 colBg
= attr
->GetBackgroundColour();
2521 colBg
= GetBackgroundColour();
2523 dc
.SetBrush(wxBrush(colBg
, wxBRUSHSTYLE_SOLID
));
2526 int offset
= HasFlag(wxTR_ROW_LINES
) ? 1 : 0;
2528 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT
) )
2532 GetVirtualSize(&w
, &h
);
2533 wxRect
rect( x
, item
->GetY()+offset
, w
, total_h
-offset
);
2534 if (!item
->IsSelected())
2536 dc
.DrawRectangle(rect
);
2540 int flags
= wxCONTROL_SELECTED
;
2542 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2543 && IsControlActive( (ControlRef
)GetHandle() )
2546 flags
|= wxCONTROL_FOCUSED
;
2547 if ((item
== m_current
) && (m_hasFocus
))
2548 flags
|= wxCONTROL_CURRENT
;
2550 wxRendererNative::Get().
2551 DrawItemSelectionRect(this, dc
, rect
, flags
);
2554 else // no full row highlight
2556 if ( item
->IsSelected() &&
2557 (state
!= wxTREE_ITEMSTATE_NONE
|| image
!= NO_IMAGE
) )
2559 // If it's selected, and there's an state image or normal image,
2560 // then we should take care to leave the area under the image
2561 // painted in the background colour.
2562 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2563 item
->GetY() + offset
,
2564 item
->GetWidth() - state_w
- image_w
+ 2,
2566 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2567 dc
.DrawRectangle( rect
);
2572 int flags
= wxCONTROL_SELECTED
;
2574 flags
|= wxCONTROL_FOCUSED
;
2575 if ((item
== m_current
) && (m_hasFocus
))
2576 flags
|= wxCONTROL_CURRENT
;
2577 wxRendererNative::Get().
2578 DrawItemSelectionRect(this, dc
, rect
, flags
);
2581 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2582 // don't allow backgrounds to be customized. Not drawing the background,
2583 // except for custom item backgrounds, works for both kinds of theme.
2584 else if (drawItemBackground
)
2586 wxRect
rect( item
->GetX() + state_w
+ image_w
- 2,
2587 item
->GetY() + offset
,
2588 item
->GetWidth() - state_w
- image_w
+ 2,
2592 dc
.DrawRectangle( rect
);
2594 else // no specific background colour
2599 int flags
= wxCONTROL_SELECTED
;
2601 flags
|= wxCONTROL_FOCUSED
;
2602 if ((item
== m_current
) && (m_hasFocus
))
2603 flags
|= wxCONTROL_CURRENT
;
2604 wxRendererNative::Get().
2605 DrawItemSelectionRect(this, dc
, rect
, flags
);
2610 if ( state
!= wxTREE_ITEMSTATE_NONE
)
2612 dc
.SetClippingRegion( item
->GetX(), item
->GetY(), state_w
, total_h
);
2613 m_imageListState
->Draw( state
, dc
,
2616 (total_h
> state_h
? (total_h
-state_h
)/2
2618 wxIMAGELIST_DRAW_TRANSPARENT
);
2619 dc
.DestroyClippingRegion();
2622 if ( image
!= NO_IMAGE
)
2624 dc
.SetClippingRegion(item
->GetX() + state_w
, item
->GetY(),
2626 m_imageListNormal
->Draw( image
, dc
,
2627 item
->GetX() + state_w
,
2629 (total_h
> image_h
? (total_h
-image_h
)/2
2631 wxIMAGELIST_DRAW_TRANSPARENT
);
2632 dc
.DestroyClippingRegion();
2635 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
2636 int extraH
= (total_h
> text_h
) ? (total_h
- text_h
)/2 : 0;
2637 dc
.DrawText( item
->GetText(),
2638 (wxCoord
)(state_w
+ image_w
+ item
->GetX()),
2639 (wxCoord
)(item
->GetY() + extraH
));
2641 // restore normal font
2642 dc
.SetFont( m_normalFont
);
2644 if (item
== m_dndEffectItem
)
2646 dc
.SetPen( *wxBLACK_PEN
);
2647 // DnD visual effects
2648 switch (m_dndEffect
)
2652 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2653 int w
= item
->GetWidth() + 2;
2654 int h
= total_h
+ 2;
2655 dc
.DrawRectangle( item
->GetX() - 1, item
->GetY() - 1, w
, h
);
2660 int x
= item
->GetX(),
2662 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2667 int x
= item
->GetX(),
2670 dc
.DrawLine( x
, y
, x
+ item
->GetWidth(), y
);
2680 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem
*item
,
2685 int x
= level
*m_indent
;
2686 if (!HasFlag(wxTR_HIDE_ROOT
))
2690 else if (level
== 0)
2692 // always expand hidden root
2694 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2695 int count
= children
.GetCount();
2701 PaintLevel(children
[n
], dc
, 1, y
);
2702 } while (++n
< count
);
2704 if ( !HasFlag(wxTR_NO_LINES
) && HasFlag(wxTR_LINES_AT_ROOT
)
2707 // draw line down to last child
2708 origY
+= GetLineHeight(children
[0])>>1;
2709 oldY
+= GetLineHeight(children
[n
-1])>>1;
2710 dc
.DrawLine(3, origY
, 3, oldY
);
2716 item
->SetX(x
+m_spacing
);
2719 int h
= GetLineHeight(item
);
2721 int y_mid
= y_top
+ (h
>>1);
2724 int exposed_x
= dc
.LogicalToDeviceX(0);
2725 int exposed_y
= dc
.LogicalToDeviceY(y_top
);
2727 if (IsExposed(exposed_x
, exposed_y
, 10000, h
)) // 10000 = very much
2731 // don't draw rect outline if we already have the
2732 // background color under Mac
2733 (item
->IsSelected() && m_hasFocus
) ? wxBLACK_PEN
:
2734 #endif // !__WXMAC__
2738 if ( item
->IsSelected()
2739 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2740 // On wxMac, if the tree doesn't have the focus we draw an empty
2741 // rectangle, so we want to make sure that the text is visible
2742 // against the normal background, not the highlightbackground, so
2743 // don't use the highlight text colour unless we have the focus.
2744 && m_hasFocus
&& IsControlActive( (ControlRef
)GetHandle() )
2751 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2756 wxTreeItemAttr
*attr
= item
->GetAttributes();
2757 if (attr
&& attr
->HasTextColour())
2758 colText
= attr
->GetTextColour();
2760 colText
= GetForegroundColour();
2764 dc
.SetTextForeground(colText
);
2768 PaintItem(item
, dc
);
2770 if (HasFlag(wxTR_ROW_LINES
))
2772 // if the background colour is white, choose a
2773 // contrasting color for the lines
2774 dc
.SetPen(*((GetBackgroundColour() == *wxWHITE
)
2775 ? wxMEDIUM_GREY_PEN
: wxWHITE_PEN
));
2776 dc
.DrawLine(0, y_top
, 10000, y_top
);
2777 dc
.DrawLine(0, y
, 10000, y
);
2780 // restore DC objects
2781 dc
.SetBrush(*wxWHITE_BRUSH
);
2782 dc
.SetPen(m_dottedPen
);
2783 dc
.SetTextForeground(*wxBLACK
);
2785 if ( !HasFlag(wxTR_NO_LINES
) )
2787 // draw the horizontal line here
2789 if (x
> (signed)m_indent
)
2790 x_start
-= m_indent
;
2791 else if (HasFlag(wxTR_LINES_AT_ROOT
))
2793 dc
.DrawLine(x_start
, y_mid
, x
+ m_spacing
, y_mid
);
2796 // should the item show a button?
2797 if ( item
->HasPlus() && HasButtons() )
2799 if ( m_imageListButtons
)
2801 // draw the image button here
2804 int image
= item
->IsExpanded() ? wxTreeItemIcon_Expanded
2805 : wxTreeItemIcon_Normal
;
2806 if ( item
->IsSelected() )
2807 image
+= wxTreeItemIcon_Selected
- wxTreeItemIcon_Normal
;
2809 m_imageListButtons
->GetSize(image
, image_w
, image_h
);
2810 int xx
= x
- image_w
/2;
2811 int yy
= y_mid
- image_h
/2;
2813 wxDCClipper
clip(dc
, xx
, yy
, image_w
, image_h
);
2814 m_imageListButtons
->Draw(image
, dc
, xx
, yy
,
2815 wxIMAGELIST_DRAW_TRANSPARENT
);
2817 else // no custom buttons
2819 static const int wImage
= 9;
2820 static const int hImage
= 9;
2823 if (item
->IsExpanded())
2824 flag
|= wxCONTROL_EXPANDED
;
2825 if (item
== m_underMouse
)
2826 flag
|= wxCONTROL_CURRENT
;
2828 wxRendererNative::Get().DrawTreeItemButton
2832 wxRect(x
- wImage
/2,
2841 if (item
->IsExpanded())
2843 wxArrayGenericTreeItems
& children
= item
->GetChildren();
2844 int count
= children
.GetCount();
2851 PaintLevel(children
[n
], dc
, level
, y
);
2852 } while (++n
< count
);
2854 if (!HasFlag(wxTR_NO_LINES
) && count
> 0)
2856 // draw line down to last child
2857 oldY
+= GetLineHeight(children
[n
-1])>>1;
2858 if (HasButtons()) y_mid
+= 5;
2860 // Only draw the portion of the line that is visible, in case
2862 wxCoord xOrigin
=0, yOrigin
=0, width
, height
;
2863 dc
.GetDeviceOrigin(&xOrigin
, &yOrigin
);
2864 yOrigin
= abs(yOrigin
);
2865 GetClientSize(&width
, &height
);
2867 // Move end points to the begining/end of the view?
2868 if (y_mid
< yOrigin
)
2870 if (oldY
> yOrigin
+ height
)
2871 oldY
= yOrigin
+ height
;
2873 // after the adjustments if y_mid is larger than oldY then the
2874 // line isn't visible at all so don't draw anything
2876 dc
.DrawLine(x
, y_mid
, x
, oldY
);
2882 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem
*item
)
2886 if ( item
->HasPlus() )
2888 // it's a folder, indicate it by a border
2893 // draw a line under the drop target because the item will be
2895 DrawLine(item
, !m_dropEffectAboveItem
);
2898 SetCursor(*wxSTANDARD_CURSOR
);
2903 SetCursor(wxCURSOR_NO_ENTRY
);
2907 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId
&item
)
2909 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2911 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2913 if (m_dndEffect
== NoEffect
)
2915 m_dndEffect
= BorderEffect
;
2916 m_dndEffectItem
= i
;
2920 m_dndEffect
= NoEffect
;
2921 m_dndEffectItem
= NULL
;
2924 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2925 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2926 RefreshRect( rect
);
2929 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId
&item
, bool below
)
2931 wxCHECK_RET( item
.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2933 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
2935 if (m_dndEffect
== NoEffect
)
2938 m_dndEffect
= BelowEffect
;
2940 m_dndEffect
= AboveEffect
;
2941 m_dndEffectItem
= i
;
2945 m_dndEffect
= NoEffect
;
2946 m_dndEffectItem
= NULL
;
2949 wxRect
rect( i
->GetX()-1, i
->GetY()-1, i
->GetWidth()+2, GetLineHeight(i
)+2 );
2950 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2951 RefreshRect( rect
);
2954 // -----------------------------------------------------------------------------
2955 // wxWidgets callbacks
2956 // -----------------------------------------------------------------------------
2958 void wxGenericTreeCtrl::OnSize( wxSizeEvent
&event
)
2961 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT
) && m_current
)
2962 RefreshLine( m_current
);
2968 void wxGenericTreeCtrl::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2976 dc
.SetFont( m_normalFont
);
2977 dc
.SetPen( m_dottedPen
);
2979 // this is now done dynamically
2980 //if(GetImageList() == NULL)
2981 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2984 PaintLevel( m_anchor
, dc
, 0, y
);
2987 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent
&event
)
2996 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent
&event
)
3005 void wxGenericTreeCtrl::OnChar( wxKeyEvent
&event
)
3007 wxTreeEvent
te( wxEVT_COMMAND_TREE_KEY_DOWN
, this);
3008 te
.m_evtKey
= event
;
3009 if ( GetEventHandler()->ProcessEvent( te
) )
3011 // intercepted by the user code
3015 if ( (m_current
== 0) || (m_key_current
== 0) )
3021 // how should the selection work for this event?
3022 bool is_multiple
, extended_select
, unselect_others
;
3023 EventFlagsToSelType(GetWindowStyleFlag(),
3026 is_multiple
, extended_select
, unselect_others
);
3028 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3030 if (event
.GetKeyCode() == WXK_RIGHT
)
3031 event
.m_keyCode
= WXK_LEFT
;
3032 else if (event
.GetKeyCode() == WXK_LEFT
)
3033 event
.m_keyCode
= WXK_RIGHT
;
3038 // * : Expand all/Collapse all
3039 // ' ' | return : activate
3040 // up : go up (not last children!)
3042 // left : go to parent
3043 // right : open if parent and go next
3044 // home : go to root
3045 // end : go to last item without opening parents
3046 // alnum : start or continue searching for the item with this prefix
3047 int keyCode
= event
.GetKeyCode();
3052 if (m_current
->HasPlus() && !IsExpanded(m_current
))
3060 if ( !IsExpanded(m_current
) )
3063 ExpandAllChildren(m_current
);
3066 //else: fall through to Collapse() it
3070 if (IsExpanded(m_current
))
3072 Collapse(m_current
);
3078 // Use the item's bounding rectangle to determine position for
3081 GetBoundingRect(m_current
, ItemRect
, true);
3084 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU
, this, m_current
);
3085 // Use the left edge, vertical middle
3086 eventMenu
.m_pointDrag
= wxPoint(ItemRect
.GetX(),
3088 ItemRect
.GetHeight() / 2);
3089 GetEventHandler()->ProcessEvent( eventMenu
);
3095 if ( !event
.HasModifiers() )
3098 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, m_current
);
3099 GetEventHandler()->ProcessEvent( eventAct
);
3102 // in any case, also generate the normal key event for this key,
3103 // even if we generated the ACTIVATED event above: this is what
3104 // wxMSW does and it makes sense because you might not want to
3105 // process ACTIVATED event at all and handle Space and Return
3106 // directly (and differently) which would be impossible otherwise
3110 // up goes to the previous sibling or to the last
3111 // of its children if it's expanded
3114 wxTreeItemId prev
= GetPrevSibling( m_key_current
);
3117 prev
= GetItemParent( m_key_current
);
3118 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3120 break; // don't go to root if it is hidden
3124 wxTreeItemIdValue cookie
;
3125 wxTreeItemId current
= m_key_current
;
3126 // TODO: Huh? If we get here, we'd better be the first
3127 // child of our parent. How else could it be?
3128 if (current
== GetFirstChild( prev
, cookie
))
3130 // otherwise we return to where we came from
3134 m_key_current
= (wxGenericTreeItem
*) prev
.m_pItem
;
3141 while ( IsExpanded(prev
) && HasChildren(prev
) )
3143 wxTreeItemId child
= GetLastChild(prev
);
3150 DoSelectItem( prev
, unselect_others
, extended_select
);
3151 m_key_current
=(wxGenericTreeItem
*) prev
.m_pItem
;
3156 // left arrow goes to the parent
3159 wxTreeItemId prev
= GetItemParent( m_current
);
3160 if ((prev
== GetRootItem()) && HasFlag(wxTR_HIDE_ROOT
))
3162 // don't go to root if it is hidden
3163 prev
= GetPrevSibling( m_current
);
3167 DoSelectItem( prev
, unselect_others
, extended_select
);
3173 // this works the same as the down arrow except that we
3174 // also expand the item if it wasn't expanded yet
3175 if (m_current
!= GetRootItem().m_pItem
|| !HasFlag(wxTR_HIDE_ROOT
))
3177 //else: don't try to expand hidden root item (which can be the
3178 // current one when the tree is empty)
3184 if (IsExpanded(m_key_current
) && HasChildren(m_key_current
))
3186 wxTreeItemIdValue cookie
;
3187 wxTreeItemId child
= GetFirstChild( m_key_current
, cookie
);
3191 DoSelectItem( child
, unselect_others
, extended_select
);
3192 m_key_current
=(wxGenericTreeItem
*) child
.m_pItem
;
3196 wxTreeItemId next
= GetNextSibling( m_key_current
);
3199 wxTreeItemId current
= m_key_current
;
3200 while (current
.IsOk() && !next
)
3202 current
= GetItemParent( current
);
3203 if (current
) next
= GetNextSibling( current
);
3208 DoSelectItem( next
, unselect_others
, extended_select
);
3209 m_key_current
=(wxGenericTreeItem
*) next
.m_pItem
;
3215 // <End> selects the last visible tree item
3218 wxTreeItemId last
= GetRootItem();
3220 while ( last
.IsOk() && IsExpanded(last
) )
3222 wxTreeItemId lastChild
= GetLastChild(last
);
3224 // it may happen if the item was expanded but then all of
3225 // its children have been deleted - so IsExpanded() returned
3226 // true, but GetLastChild() returned invalid item
3235 DoSelectItem( last
, unselect_others
, extended_select
);
3240 // <Home> selects the root item
3243 wxTreeItemId prev
= GetRootItem();
3247 if ( HasFlag(wxTR_HIDE_ROOT
) )
3249 wxTreeItemIdValue cookie
;
3250 prev
= GetFirstChild(prev
, cookie
);
3255 DoSelectItem( prev
, unselect_others
, extended_select
);
3260 // do not use wxIsalnum() here
3261 if ( !event
.HasModifiers() &&
3262 ((keyCode
>= '0' && keyCode
<= '9') ||
3263 (keyCode
>= 'a' && keyCode
<= 'z') ||
3264 (keyCode
>= 'A' && keyCode
<= 'Z' )))
3266 // find the next item starting with the given prefix
3267 wxChar ch
= (wxChar
)keyCode
;
3269 wxTreeItemId id
= FindItem(m_current
, m_findPrefix
+ ch
);
3280 // also start the timer to reset the current prefix if the user
3281 // doesn't press any more alnum keys soon -- we wouldn't want
3282 // to use this prefix for a new item search
3285 m_findTimer
= new wxTreeFindTimer(this);
3288 m_findTimer
->Start(wxTreeFindTimer::DELAY
, wxTIMER_ONE_SHOT
);
3298 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
3303 if (point
.x
<0) flags
|= wxTREE_HITTEST_TOLEFT
;
3304 if (point
.x
>w
) flags
|= wxTREE_HITTEST_TORIGHT
;
3305 if (point
.y
<0) flags
|= wxTREE_HITTEST_ABOVE
;
3306 if (point
.y
>h
) flags
|= wxTREE_HITTEST_BELOW
;
3307 if (flags
) return wxTreeItemId();
3309 if (m_anchor
== NULL
)
3311 flags
= wxTREE_HITTEST_NOWHERE
;
3312 return wxTreeItemId();
3315 wxGenericTreeItem
*hit
= m_anchor
->HitTest(CalcUnscrolledPosition(point
),
3319 flags
= wxTREE_HITTEST_NOWHERE
;
3320 return wxTreeItemId();
3325 // get the bounding rectangle of the item (or of its label only)
3326 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
3328 bool textOnly
) const
3330 wxCHECK_MSG( item
.IsOk(), false,
3331 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3333 wxGenericTreeItem
*i
= (wxGenericTreeItem
*) item
.m_pItem
;
3337 int image_h
= 0, image_w
= 0;
3338 int image
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetCurrentImage();
3339 if ( image
!= NO_IMAGE
&& m_imageListNormal
)
3341 m_imageListNormal
->GetSize( image
, image_w
, image_h
);
3342 image_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3345 int state_h
= 0, state_w
= 0;
3346 int state
= ((wxGenericTreeItem
*) item
.m_pItem
)->GetState();
3347 if ( state
!= wxTREE_ITEMSTATE_NONE
&& m_imageListState
)
3349 m_imageListState
->GetSize( state
, state_w
, state_h
);
3351 state_w
+= MARGIN_BETWEEN_STATE_AND_IMAGE
;
3353 state_w
+= MARGIN_BETWEEN_IMAGE_AND_TEXT
;
3356 rect
.x
= i
->GetX() + state_w
+ image_w
;
3357 rect
.width
= i
->GetWidth() - state_w
- image_w
;
3360 else // the entire line
3363 rect
.width
= GetClientSize().x
;
3367 rect
.height
= GetLineHeight(i
);
3369 // we have to return the logical coordinates, not physical ones
3370 rect
.SetTopLeft(CalcScrolledPosition(rect
.GetTopLeft()));
3375 wxTextCtrl
*wxGenericTreeCtrl::EditLabel(const wxTreeItemId
& item
,
3376 wxClassInfo
* WXUNUSED(textCtrlClass
))
3378 wxCHECK_MSG( item
.IsOk(), NULL
, _T("can't edit an invalid item") );
3380 wxGenericTreeItem
*itemEdit
= (wxGenericTreeItem
*)item
.m_pItem
;
3382 wxTreeEvent
te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
, this, itemEdit
);
3383 if ( GetEventHandler()->ProcessEvent( te
) && !te
.IsAllowed() )
3389 // We have to call this here because the label in
3390 // question might just have been added and no screen
3391 // update taken place.
3393 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3396 DoDirtyProcessing();
3399 // TODO: use textCtrlClass here to create the control of correct class
3400 m_textCtrl
= new wxTreeTextCtrl(this, itemEdit
);
3402 m_textCtrl
->SetFocus();
3407 // returns a pointer to the text edit control if the item is being
3408 // edited, NULL otherwise (it's assumed that no more than one item may
3409 // be edited simultaneously)
3410 wxTextCtrl
* wxGenericTreeCtrl::GetEditControl() const
3415 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
),
3416 bool discardChanges
)
3418 wxCHECK_RET( m_textCtrl
, _T("not editing label") );
3420 m_textCtrl
->EndEdit(discardChanges
);
3423 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem
*item
,
3424 const wxString
& value
)
3426 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3428 le
.m_editCancelled
= false;
3430 return !GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
3433 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem
*item
)
3435 // let owner know that the edit was cancelled
3436 wxTreeEvent
le(wxEVT_COMMAND_TREE_END_LABEL_EDIT
, this, item
);
3437 le
.m_label
= wxEmptyString
;
3438 le
.m_editCancelled
= true;
3440 GetEventHandler()->ProcessEvent( le
);
3443 void wxGenericTreeCtrl::OnRenameTimer()
3445 EditLabel( m_current
);
3448 void wxGenericTreeCtrl::OnMouse( wxMouseEvent
&event
)
3450 if ( !m_anchor
)return;
3452 wxPoint pt
= CalcUnscrolledPosition(event
.GetPosition());
3454 // Is the mouse over a tree item button?
3456 wxGenericTreeItem
*thisItem
= m_anchor
->HitTest(pt
, this, flags
, 0);
3457 wxGenericTreeItem
*underMouse
= thisItem
;
3459 bool underMouseChanged
= (underMouse
!= m_underMouse
) ;
3460 #endif // wxUSE_TOOLTIPS
3463 (flags
& wxTREE_HITTEST_ONITEMBUTTON
) &&
3464 (!event
.LeftIsDown()) &&
3466 (!m_renameTimer
|| !m_renameTimer
->IsRunning()))
3474 if (underMouse
!= m_underMouse
)
3478 // unhighlight old item
3479 wxGenericTreeItem
*tmp
= m_underMouse
;
3480 m_underMouse
= NULL
;
3484 m_underMouse
= underMouse
;
3486 RefreshLine( m_underMouse
);
3490 // Determines what item we are hovering over and need a tooltip for
3491 wxTreeItemId hoverItem
= thisItem
;
3493 // We do not want a tooltip if we are dragging, or if the rename timer is
3495 if ( underMouseChanged
&&
3498 (!m_renameTimer
|| !m_renameTimer
->IsRunning()) )
3500 // Ask the tree control what tooltip (if any) should be shown
3502 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
, this, hoverItem
);
3504 if ( GetEventHandler()->ProcessEvent(hevent
) && hevent
.IsAllowed() )
3506 SetToolTip(hevent
.m_label
);
3511 // we process left mouse up event (enables in-place edit), middle/right down
3512 // (pass to the user code), left dbl click (activate item) and
3513 // dragging/moving events for items drag-and-drop
3514 if ( !(event
.LeftDown() ||
3516 event
.MiddleDown() ||
3517 event
.RightDown() ||
3518 event
.LeftDClick() ||
3520 ((event
.Moving() || event
.RightUp()) && m_isDragging
)) )
3529 wxGenericTreeItem
*item
= m_anchor
->HitTest(pt
, this, flags
, 0);
3531 if ( event
.Dragging() && !m_isDragging
)
3533 if (m_dragCount
== 0)
3538 if (m_dragCount
!= 3)
3540 // wait until user drags a bit further...
3544 wxEventType command
= event
.RightIsDown()
3545 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3546 : wxEVT_COMMAND_TREE_BEGIN_DRAG
;
3548 wxTreeEvent
nevent(command
, this, m_current
);
3549 nevent
.SetPoint(CalcScrolledPosition(pt
));
3551 // by default the dragging is not supported, the user code must
3552 // explicitly allow the event for it to take place
3555 if ( GetEventHandler()->ProcessEvent(nevent
) && nevent
.IsAllowed() )
3557 // we're going to drag this item
3558 m_isDragging
= true;
3560 // remember the old cursor because we will change it while
3562 m_oldCursor
= m_cursor
;
3564 // in a single selection control, hide the selection temporarily
3565 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE
) )
3567 m_oldSelection
= (wxGenericTreeItem
*) GetSelection().m_pItem
;
3569 if ( m_oldSelection
)
3571 m_oldSelection
->SetHilight(false);
3572 RefreshLine(m_oldSelection
);
3579 else if ( event
.Dragging() )
3581 if ( item
!= m_dropTarget
)
3583 // unhighlight the previous drop target
3584 DrawDropEffect(m_dropTarget
);
3586 m_dropTarget
= item
;
3588 // highlight the current drop target if any
3589 DrawDropEffect(m_dropTarget
);
3591 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3594 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3595 // instead (needs to be tested!)
3600 else if ( (event
.LeftUp() || event
.RightUp()) && m_isDragging
)
3604 // erase the highlighting
3605 DrawDropEffect(m_dropTarget
);
3607 if ( m_oldSelection
)
3609 m_oldSelection
->SetHilight(true);
3610 RefreshLine(m_oldSelection
);
3611 m_oldSelection
= NULL
;
3614 // generate the drag end event
3615 wxTreeEvent
eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG
, this, item
);
3617 eventEndDrag
.m_pointDrag
= CalcScrolledPosition(pt
);
3619 (void)GetEventHandler()->ProcessEvent(eventEndDrag
);
3621 m_isDragging
= false;
3622 m_dropTarget
= NULL
;
3624 SetCursor(m_oldCursor
);
3626 #if defined( __WXMSW__ ) || defined(__WXMAC__) || defined(__WXGTK20__)
3629 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3630 // instead (needs to be tested!)
3636 // If we got to this point, we are not dragging or moving the mouse.
3637 // Because the code in carbon/toplevel.cpp will only set focus to the
3638 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3640 // We skip even if we didn't hit an item because we still should
3641 // restore focus to the tree control even if we didn't exactly hit an
3643 if ( event
.LeftDown() )
3648 // here we process only the messages which happen on tree items
3652 if (item
== NULL
) return; /* we hit the blank area */
3654 if ( event
.RightDown() )
3656 // If the item is already selected, do not update the selection.
3657 // Multi-selections should not be cleared if a selected item is
3659 if (!IsSelected(item
))
3661 DoSelectItem(item
, true, false);
3665 nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
, this, item
);
3666 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3667 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3669 // Consistent with MSW (for now), send the ITEM_MENU *after*
3670 // the RIGHT_CLICK event. TODO: This behavior may change.
3671 wxTreeEvent
nevent2(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
3672 nevent2
.m_pointDrag
= CalcScrolledPosition(pt
);
3673 GetEventHandler()->ProcessEvent(nevent2
);
3675 else if ( event
.MiddleDown() )
3678 nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
, this, item
);
3679 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3680 event
.Skip(!GetEventHandler()->ProcessEvent(nevent
));
3682 else if ( event
.LeftUp() )
3684 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
3687 nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, this, item
);
3688 GetEventHandler()->ProcessEvent(nevent
);
3691 // this facilitates multiple-item drag-and-drop
3693 if ( /* item && */ HasFlag(wxTR_MULTIPLE
))
3695 wxArrayTreeItemIds selections
;
3696 size_t count
= GetSelections(selections
);
3702 DoSelectItem(item
, true, false);
3708 if ( (item
== m_current
) &&
3709 (flags
& wxTREE_HITTEST_ONITEMLABEL
) &&
3710 HasFlag(wxTR_EDIT_LABELS
) )
3712 if ( m_renameTimer
)
3714 if ( m_renameTimer
->IsRunning() )
3715 m_renameTimer
->Stop();
3719 m_renameTimer
= new wxTreeRenameTimer( this );
3722 m_renameTimer
->Start( wxTreeRenameTimer::DELAY
, true );
3725 m_lastOnSame
= false;
3728 else // !RightDown() && !MiddleDown() && !LeftUp()
3730 // ==> LeftDown() || LeftDClick()
3731 if ( event
.LeftDown() )
3733 m_lastOnSame
= item
== m_current
;
3736 if ( flags
& wxTREE_HITTEST_ONITEMBUTTON
)
3738 // only toggle the item for a single click, double click on
3739 // the button doesn't do anything (it toggles the item twice)
3740 if ( event
.LeftDown() )
3745 // don't select the item if the button was clicked
3750 // clear the previously selected items, if the
3751 // user clicked outside of the present selection.
3752 // otherwise, perform the deselection on mouse-up.
3753 // this allows multiple drag and drop to work.
3754 // but if Cmd is down, toggle selection of the clicked item
3755 if (!IsSelected(item
) || event
.CmdDown())
3757 // how should the selection work for this event?
3758 bool is_multiple
, extended_select
, unselect_others
;
3759 EventFlagsToSelType(GetWindowStyleFlag(),
3766 DoSelectItem(item
, unselect_others
, extended_select
);
3770 // For some reason, Windows isn't recognizing a left double-click,
3771 // so we need to simulate it here. Allow 200 milliseconds for now.
3772 if ( event
.LeftDClick() )
3774 // double clicking should not start editing the item label
3775 if ( m_renameTimer
)
3776 m_renameTimer
->Stop();
3778 m_lastOnSame
= false;
3780 // send activate event first
3782 nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
, this, item
);
3783 nevent
.m_pointDrag
= CalcScrolledPosition(pt
);
3784 if ( !GetEventHandler()->ProcessEvent( nevent
) )
3786 // if the user code didn't process the activate event,
3787 // handle it ourselves by toggling the item when it is
3789 if ( item
->HasPlus() )
3799 void wxGenericTreeCtrl::OnInternalIdle()
3801 wxWindow::OnInternalIdle();
3803 // Check if we need to select the root item
3804 // because nothing else has been selected.
3805 // Delaying it means that we can invoke event handlers
3806 // as required, when a first item is selected.
3807 if (!HasFlag(wxTR_MULTIPLE
) && !GetSelection().IsOk())
3810 SelectItem(m_select_me
);
3811 else if (GetRootItem().IsOk())
3812 SelectItem(GetRootItem());
3815 // after all changes have been done to the tree control,
3816 // actually redraw the tree when everything is over
3818 DoDirtyProcessing();
3822 wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem
*item
,
3827 int x
= level
*m_indent
;
3828 if (!HasFlag(wxTR_HIDE_ROOT
))
3832 else if (level
== 0)
3834 // a hidden root is not evaluated, but its
3835 // children are always calculated
3839 item
->CalculateSize(this, dc
);
3842 item
->SetX( x
+m_spacing
);
3844 y
+= GetLineHeight(item
);
3846 if ( !item
->IsExpanded() )
3848 // we don't need to calculate collapsed branches
3853 wxArrayGenericTreeItems
& children
= item
->GetChildren();
3854 size_t n
, count
= children
.GetCount();
3856 for (n
= 0; n
< count
; ++n
)
3857 CalculateLevel( children
[n
], dc
, level
, y
); // recurse
3860 void wxGenericTreeCtrl::CalculatePositions()
3862 if ( !m_anchor
) return;
3864 wxClientDC
dc(this);
3867 dc
.SetFont( m_normalFont
);
3869 dc
.SetPen( m_dottedPen
);
3870 //if(GetImageList() == NULL)
3871 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3874 CalculateLevel( m_anchor
, dc
, 0, y
); // start recursion
3877 void wxGenericTreeCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
3880 wxTreeCtrlBase::Refresh(eraseBackground
, rect
);
3883 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem
*item
)
3885 if (m_dirty
|| IsFrozen() )
3888 wxSize client
= GetClientSize();
3891 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3892 rect
.width
= client
.x
;
3893 rect
.height
= client
.y
;
3895 Refresh(true, &rect
);
3897 AdjustMyScrollbars();
3900 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem
*item
)
3902 if (m_dirty
|| IsFrozen() )
3906 CalcScrolledPosition(0, item
->GetY(), NULL
, &rect
.y
);
3907 rect
.width
= GetClientSize().x
;
3908 rect
.height
= GetLineHeight(item
); //dc.GetCharHeight() + 6;
3910 Refresh(true, &rect
);
3913 void wxGenericTreeCtrl::RefreshSelected()
3918 // TODO: this is awfully inefficient, we should keep the list of all
3919 // selected items internally, should be much faster
3921 RefreshSelectedUnder(m_anchor
);
3924 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem
*item
)
3929 if ( item
->IsSelected() )
3932 const wxArrayGenericTreeItems
& children
= item
->GetChildren();
3933 size_t count
= children
.GetCount();
3934 for ( size_t n
= 0; n
< count
; n
++ )
3936 RefreshSelectedUnder(children
[n
]);
3940 void wxGenericTreeCtrl::DoThaw()
3942 wxTreeCtrlBase::DoThaw();
3945 DoDirtyProcessing();
3950 // ----------------------------------------------------------------------------
3951 // changing colours: we need to refresh the tree control
3952 // ----------------------------------------------------------------------------
3954 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour
& colour
)
3956 if ( !wxWindow::SetBackgroundColour(colour
) )
3964 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour
& colour
)
3966 if ( !wxWindow::SetForegroundColour(colour
) )
3974 // Process the tooltip event, to speed up event processing.
3975 // Doesn't actually get a tooltip.
3976 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent
&event
)
3982 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3983 // be removed, as well as the #else case below.
3984 #define _USE_VISATTR 0
3989 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
3991 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
3995 // Use the same color scheme as wxListBox
3996 return wxListBox::GetClassDefaultAttributes(variant
);
3998 wxVisualAttributes attr
;
3999 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
4000 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
4001 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
4006 void wxGenericTreeCtrl::DoDirtyProcessing()
4013 CalculatePositions();
4015 AdjustMyScrollbars();
4018 wxSize
wxGenericTreeCtrl::DoGetBestSize() const
4020 // make sure all positions are calculated as normally this only done during
4021 // idle time but we need them for base class DoGetBestSize() to return the
4023 wxConstCast(this, wxGenericTreeCtrl
)->CalculatePositions();
4025 wxSize size
= wxTreeCtrlBase::DoGetBestSize();
4027 // there seems to be an implicit extra border around the items, although
4028 // I'm not really sure where does it come from -- but without this, the
4029 // scrollbars appear in a tree with default/best size
4032 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
4033 // scrollbars still appear
4034 const wxSize
& borderSize
= GetWindowBorderSize();
4036 int dx
= (size
.x
- borderSize
.x
) % PIXELS_PER_UNIT
;
4038 size
.x
+= PIXELS_PER_UNIT
- dx
;
4039 int dy
= (size
.y
- borderSize
.y
) % PIXELS_PER_UNIT
;
4041 size
.y
+= PIXELS_PER_UNIT
- dy
;
4043 // we need to update the cache too as the base class cached its own value
4044 CacheBestSize(size
);
4049 #endif // wxUSE_TREECTRL